aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/ConstantFold.cpp
AgeCommit message (Collapse)AuthorFilesLines
2023-11-03[ConstantFold] Remove unnecessary BitCastConstantVector() (NFCI)Nikita Popov1-52/+1
ConstantFoldCastInstruction() already has generic code to perform lane-wise casts for vectors. There is no need to repeate it specifically for bitcasts.
2023-11-03[IR] Remove zext and sext constant expressions (#71040)Nikita Popov1-72/+0
Remove support for zext and sext constant expressions. All places creating them have been removed beforehand, so this just removes the APIs and uses of these constant expressions in tests. There is some additional cleanup that can be done on top of this, e.g. we can remove the ZExtInst vs ZExtOperator footgun. This is part of https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179.
2023-11-01[ConstantFold] Avoid creating undesirable cast expressionsNikita Popov1-6/+17
Similar to what we do for binops, for undesirable casts we should call the constant folding API instead of the constant expr API, to avoid indirect creation of undesirable cast ops.
2023-10-09[ConstantFold] Avoid some uses of ConstantExpr::getSExt() (NFC)Nikita Popov1-4/+11
Use the (internal) constant folding API instead.
2023-08-22[IR] Remove support for and/or constant expressionsNikita Popov1-29/+0
As part of https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179, this removes support for and and or constant expressions. Places creating such expressions have been migrated in advance, so this is mostly API removal and test updates. Differential Revision: https://reviews.llvm.org/D155924
2023-07-31Reapply [IR] Mark and/or constant expressions as undesirableNikita Popov1-2/+0
Reapply after D156401, which stops PatternMatch from recognizing binop constant expressions, which should avoid the infinite loops and assertion failures this patch previously exposed. ----- In preparation for removing support for and/or expressions, mark them as undesirable. As such, we will no longer implicitly create such expressions, but they still exist.
2023-07-27Reapply [ConstantFold] Avoid creation of undesirable binopNikita Popov1-1/+3
This was reverted together with another commit due to a test conflict. Reapply without functional changes. ----- When commuting the operands, don't create a constant expression for undesirable binops. Only invoke the constant folding function in that case.
2023-07-26Revert "[ConstantFold] Avoid creation of undesirable binop"Matthew Voss1-3/+1
This reverts commit 673a4671f3e8b7158d990f6456428175a6eac38c. Depends on reverted commit 0cab8d20417c0e2ccc1ffc5505e080126f5de8e6. That commit was reverted due to an LTO crash. I've put a reduced test case here: https://github.com/llvm/llvm-project/issues/64114
2023-07-26Revert "Reapply [IR] Mark and/or constant expressions as undesirable"Matthew Voss1-0/+2
This reverts commit 0cab8d20417c0e2ccc1ffc5505e080126f5de8e6. Reverted due to an LTO crash. I've put a reduced test case here: https://github.com/llvm/llvm-project/issues/64114
2023-07-25[ConstantFold] Avoid creation of undesirable binopNikita Popov1-1/+3
When commuting the operands, don't create a constant expression for undesirable binops. Only invoke the constant folding function in that case.
2023-07-25Reapply [IR] Mark and/or constant expressions as undesirableNikita Popov1-2/+0
This reapplies the change for and, but also marks or as undesirable at the same time. Only handling one of them can cause infinite combine loops due to the asymmetric handling. ----- In preparation for removing support for and/or expressions, mark them as undesirable. As such, we will no longer implicitly create such expressions, but they still exist.
2023-07-21Revert "Reapply [IR] Mark and constant expressions as undesirable"Nathan Chancellor1-0/+2
This reverts commit 086ee99564afbb11449c08ea2e094f7f49fadde5. This patch causes an infinite loop when building arch/mips/mm/c-r4k.c in the Linux kernel. See the comment in Phabricator for a reduced reproducer: https://reviews.llvm.org/rG086ee99564afbb11449c08ea2e094f7f49fadde5
2023-07-21Reapply [IR] Mark and constant expressions as undesirableNikita Popov1-2/+0
Reapply after fixing an issue in canonicalizeLogicFirst() exposed by this change (218f97578b26f7a89f7f8ed0748c31ef0181f80a). ----- In preparation for removing support for and expressions, mark them as undesirable. As such, we will no longer implicitly create such expressions, but they still exist.
2023-07-20Revert "[IR] Mark add constant expressions as undesirable"Nikita Popov1-0/+2
This reverts commit f8a36d8c3e264c4fccf8058e699201a452ea7bb7. I believe this is causing an assertion failure on the sanitizer-x86_64-linux buildbot: clang++: /b/sanitizer-x86_64-linux/build/llvm-project/llvm/include/llvm/Support/Casting.h:578: decltype(auto) llvm::cast(From *) [To = llvm::BinaryOperator, From = llvm::Value]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed. #10 0x000055bdd7e82408 canonicalizeLogicFirst(llvm::BinaryOperator&, llvm::IRBuilder<llvm::TargetFolder, llvm::IRBuilderCallbackInserter>&) /b/sanitizer-x86_64-linux/build/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp:2131:5 #11 0x000055bdd7e80183 llvm::InstCombinerImpl::visitAnd(llvm::BinaryOperator&) /b/sanitizer-x86_64-linux/build/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp:2661:20 Likely the code is encountering a constant expression in a case it didn't before.
2023-07-20[IR] Mark add constant expressions as undesirableNikita Popov1-2/+0
In preparation for removing support for add expressions, mark them as undesirable. As such, we will no longer implicitly create such expressions, but they still exist.
2023-07-18[IR] Remove typed pointer handling from getGEPReturnType() (NFC)Nikita Popov1-1/+1
2023-07-12[ConstantFold] Remove typed pointer specific folds (NFC)Nikita Popov1-59/+1
2023-06-13[IR] Update to use new shufflevector semanticsManuelJBrito1-2/+2
Update to use new shufflevector semantics for undefined values in the mask Differential Revision: https://reviews.llvm.org/D149548
2023-04-27[IR][NFC] Change UndefMaskElem to PoisonMaskElemManuelJBrito1-1/+1
Following the change in shufflevector semantics, poison will be used to represent undefined elements in shufflevector masks. Differential Revision: https://reviews.llvm.org/D149256
2023-03-16[ConstExpr] Remove select constant expressionNikita Popov1-11/+0
This removes the select constant expression, as part of https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179. Uses of this expressions have already been removed in advance, so this just removes related infrastructure and updates tests. Differential Revision: https://reviews.llvm.org/D145382
2023-02-19Use APInt::getSignificantBits instead of APInt::getMinSignedBits (NFC)Kazu Hirata1-1/+1
Note that getMinSignedBits has been soft-deprecated in favor of getSignificantBits.
2023-02-03[NFC] Remove dead codeGuillaume Chatelet1-4/+0
2023-02-03[NFC] Simplify logic in ConstantFoldGuillaume Chatelet1-5/+5
Differential Revision: https://reviews.llvm.org/D143271
2023-02-01[ConstantFold] Fix incorrect inbounds inference for [0 x T] GEPsNikita Popov1-3/+9
Previously all indices into [0 x T] arrays were considered in range, which resulted in us incorrectly inferring inbounds for all GEPs of that form. We should not consider them in range here, and instead bail out of the rewriting logic (which would divide by zero). Do continue to consider 0 always in range, to avoid changing behavior for zero-index GEPs.
2023-01-31[ConstantFold] Fix inbounds inference on mismatching source element typeNikita Popov1-1/+2
When inferring that a GEP of a global variable is inbounds because there is no notional overindexing, we need to check that the global value type and the GEP source element type match. This was not necessary with typed pointers (because we would have a bitcast in between), but is necessary with opaque pointers. We should be able to recover some of the safe cases by performing an offset based inbounds inference in DL-aware ConstantFolding.
2023-01-06[ConstantFold] Don't drop zero index gep with inrange attributeNikita Popov1-0/+4
This may cause GlobalSplit to fail if opaque pointers are used. inrange really needs a new representation, but for now restore the pre-opaque pointers status.
2023-01-05Move from llvm::makeArrayRef to ArrayRef deduction guides - llvm/ partserge-sans-paille1-1/+1
Use deduction guides instead of helper functions. The only non-automatic changes have been: 1. ArrayRef(some_uint8_pointer, 0) needs to be changed into ArrayRef(some_uint8_pointer, (size_t)0) to avoid an ambiguous call with ArrayRef((uint8_t*), (uint8_t*)) 2. CVSymbol sym(makeArrayRef(symStorage)); needed to be rewritten as CVSymbol sym{ArrayRef(symStorage)}; otherwise the compiler is confused and thinks we have a (bad) function prototype. There was a few similar situation across the codebase. 3. ADL doesn't seem to work the same for deduction-guides and functions, so at some point the llvm namespace must be explicitly stated. 4. The "reference mode" of makeArrayRef(ArrayRef<T> &) that acts as no-op is not supported (a constructor cannot achieve that). Per reviewers' comment, some useless makeArrayRef have been removed in the process. This is a follow-up to https://reviews.llvm.org/D140896 that introduced the deduction guides. Differential Revision: https://reviews.llvm.org/D140955
2022-12-05[IR] llvm::Optional => std::optionalFangrui Song1-2/+2
Many llvm/IR/* files have been migrated by other contributors. This migrates most remaining files.
2022-12-02[IR] Use std::nullopt instead of None (NFC)Kazu Hirata1-2/+2
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-10-26ConstantFold: Reduce code duplication for checking commuted compareMatt Arsenault1-23/+25
2022-09-07[ConstantFold] Avoid unary ConstantExpr::get()Nikita Popov1-6/+7
Call ConstantFoldUnaryInstruction() instead, to only produce a result if it folds.
2022-08-08[llvm] LLVM_FALLTHROUGH => [[fallthrough]]. NFCFangrui Song1-3/+3
With C++17 there is no Clang pedantic warning or MSVC C5051.
2022-07-05[ConstExpr] Don't create div/rem expressionsNikita Popov1-6/+21
This removes creation of udiv/sdiv/urem/srem constant expressions, in preparation for their removal. I've added a ConstantExpr::isDesirableBinOp() predicate to determine whether an expression should be created for a certain operator. With this patch, div/rem expressions can still be created through explicit IR/bitcode, forbidding them entirely will be the next step. Differential Revision: https://reviews.llvm.org/D128820
2022-06-23[IR] Export ConstantFold.h header (NFC)Nikita Popov1-1/+1
This is in preparation for https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179. As part of that change, we'll want to invoke some of these constant folding APIs explicitly, as it won't happen as part of ConstantExpr::getXYZ() anymore. Ideally, we'd merge these with the DL-aware constant folding APIs and only call those, but this is not easily possible for some current usages (most important IRBuilder, which uses DL-independent constant folding by default, and some major layering changes would be needed to change that). This is basically a reboot of D115035 with different motivation. Differential Revision: https://reviews.llvm.org/D128213
2022-06-20Don't use Optional::hasValue (NFC)Kazu Hirata1-1/+1
2022-05-05[ConstantFold] Use getFltSemantics instead of manually checking the typeBenjamin Kramer1-8/+2
Simplifies the code and makes fpext/fptrunc constant folding not crash when the result is bf16.
2022-04-15[IR] Allow constant folding (insertelement <vscale x 2 x i32> ↵Craig Topper1-0/+5
zeroinitializer, i32 0, i32 i32 0. Most of insertelement constant folding is blocked if the vector type is scalable. I believe we can make an exception for inserting null into an all zeros vector. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D123413
2022-04-04[ConstantFold] Fold zero-index GEPs with opaque pointersNikita Popov1-2/+13
With opaque pointers, we can eliminate zero-index GEPs even if they have multiple indices, as this no longer impacts the result type of the GEP. This optimization is already done for instructions in InstSimplify, but we were missing the corresponding constant expression handling. The constexpr transform is a bit more powerful, because it can produce a vector splat constant and also handles undef values -- it is an extension of an existing single-index transform.
2022-03-11[ConstantFold] Fix scalable shufflevector fold with all-undef maskNikita Popov1-1/+1
If the input is scalable, we should not be returning a fixed-width vector as a result.
2022-02-02Cleanup header dependencies in LLVMCoreserge-sans-paille1-2/+0
Based on the output of include-what-you-use. This is a big chunk of changes. It is very likely to break downstream code unless they took a lot of care in avoiding hidden ehader dependencies, something the LLVM codebase doesn't do that well :-/ I've tried to summarize the biggest change below: - llvm/include/llvm-c/Core.h: no longer includes llvm-c/ErrorHandling.h - llvm/IR/DIBuilder.h no longer includes llvm/IR/DebugInfo.h - llvm/IR/IRBuilder.h no longer includes llvm/IR/IntrinsicInst.h - llvm/IR/LLVMRemarkStreamer.h no longer includes llvm/Support/ToolOutputFile.h - llvm/IR/LegacyPassManager.h no longer include llvm/Pass.h - llvm/IR/Type.h no longer includes llvm/ADT/SmallPtrSet.h - llvm/IR/PassManager.h no longer includes llvm/Pass.h nor llvm/Support/Debug.h And the usual count of preprocessed lines: $ clang++ -E -Iinclude -I../llvm/include ../llvm/lib/IR/*.cpp -std=c++14 -fno-rtti -fno-exceptions | wc -l before: 6400831 after: 6189948 200k lines less to process is no that bad ;-) Discourse thread on the topic: https://llvm.discourse.group/t/include-what-you-use-include-cleanup Differential Revision: https://reviews.llvm.org/D118652
2022-01-27[ConstantFold] Disable gep of array bitcast fold with opaque pointersNikita Popov1-3/+4
Once again, this fold is meaningless with opaque pointers, as there is no pointer element type to canonicalize. At some point, we may want to do GEP type canonicalizations.
2022-01-25[NFC] Remove uses of PointerType::getElementType()Nikita Popov1-8/+8
Instead use either Type::getPointerElementType() or Type::getNonOpaquePointerElementType(). This is part of D117885, in preparation for deprecating the API.
2022-01-06[ConstFold] Add missing check for inbounds gepNikita Popov1-1/+1
If the gep is not inbounds, then the gep might compute a null value even if the base pointer is non-null.
2022-01-04[ConstantFold] Remove another incorrect icmp of gep foldNikita Popov1-6/+1
This folded (null + X) == g to false, but of course this is incorrect if X == g. Possibly this got confused with the null == g case, which is already handled elsewhere.
2022-01-04[ConstantFold] Remove unnecessary bounded index restrictionNikita Popov1-21/+3
The fold for merging a GEP of GEP into a single GEP currently bails if doing so would result in notional overindexing. The justification given in the comment above this check is dangerously incorrect: GEPs with notional overindexing are perfectly fine, and if some code treats them incorrectly, then that code is broken, not the GEP. Such a GEP might legally appear in source IR, so only preventing its creation cannot be sufficient. (The constant folder also ends up canonicalizing the GEP to remove the notional overindexing, but that's neither here nor there.) This check dates back to https://github.com/llvm/llvm-project/commit/bd4fef4a8939db18f39b108e19097b25e2c7c47a, and as far as I can tell the original issue this was trying to patch around has since been resolved. Differential Revision: https://reviews.llvm.org/D116587
2022-01-04[ConstFold] Slightly clean up icmp of two geps fold (NFC)Nikita Popov1-20/+11
As we're only dealing with one type of constant expression here, try to directly cast to GEPOperator.
2022-01-04[ConstantFold] Remove another incorrect icmp of GEP foldNikita Popov1-10/+0
This fold is not correct, because indices might evaluate to zero even if they are not a literal zero integer. Additionally, this fold would be wrong (in the general case) for non-i8 types as well, due to index overflow. Drop this fold and instead let the target-dependent constant folder compute the actual offset and fold the comparison based on that.
2022-01-03[ConstantFold] Drop unused function (NFC)Nikita Popov1-17/+0
isMaybeZeroSizeType() is no longer used after 5afbfe33e7d6ce40af0ad6d99421b443c45b351b.
2022-01-03[ConstantFold] Make icmp of gep fold offset basedNikita Popov1-92/+1
We can fold an equality or unsigned icmp between base+offset1 and base+offset2 with inbounds offsets by comparing the offsets directly. This replaces a pair of specialized folds that tried to reason based on the GEP structure instead. One of those folds was plain wrong (because it does not account for negative offsets), while the other is unnecessarily complicated and limited (e.g. it will fail with bitcasts involved). The disadvantage of this change is that it requires data layout, so the fold is no longer performed by datalayout-independent constant folding. I don't think this is a loss in practice, but it does regress the ConstantExprFold.ll test, which checks folding without running any passes. Differential Revision: https://reviews.llvm.org/D116332
2021-12-30[ConstantFolding] Use ICmpInst::Predicate instead of plain integerSerge Pavlov1-56/+76
The function `ConstantFoldCompareInstruction` uses `unsigned short` to represent compare predicate, although all usesrs of the respective include file use definition of CmpInst also. This change replaces predicate argument type in this function to `ICmpInst::Predicate`, which allows to make code a bit clearer and simpler. No functional changes. Differential Revision: https://reviews.llvm.org/D116379