aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/Constants.cpp
AgeCommit message (Collapse)AuthorFilesLines
2022-04-25Don't replace Undef with null value for Constants Differential ↵YASHASVI KHATAVKAR1-2/+6
Revision:https://reviews.llvm.org/D124098
2022-03-12Cleanup includes: DebugInfo & CodeGenserge-sans-paille1-1/+0
Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup Differential Revision: https://reviews.llvm.org/D121332
2022-02-02Cleanup header dependencies in LLVMCoreserge-sans-paille1-2/+5
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-24[llvm] Do not replace dead constant references in metadata with undefStephen Tozer1-8/+1
This patch removes an incorrect behaviour in Constants.cpp, which would replace dead constant references in metadata with an undef value. This blanket replacement resulted in undef values being inserted into metadata that would not accept them. The replacement was intended for debug info metadata, but this is now instead handled in the RAUW handler. Differential Revision: https://reviews.llvm.org/D117300
2022-01-19[Constants] Remove unused isGEPWithNoNotionalOverIndexing() methodNikita Popov1-22/+0
Since d56b0ad441a34ae6c353c823969397b451f053a6, this method is no longer used -- and shouldn't be used.
2022-01-13[Inliner] Don't removeDeadConstantUsers() when checking if a function is deadArthur Eubanks1-5/+9
If a function has many uses, this can take a good chunk of compile times. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D117236
2021-12-30[ConstantFolding] Use ICmpInst::Predicate instead of plain integerSerge Pavlov1-8/+8
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
2021-12-20[llvm][IR] Add no_cfi constantSami Tolvanen1-0/+44
With Control-Flow Integrity (CFI), the LowerTypeTests pass replaces function references with CFI jump table references, which is a problem for low-level code that needs the address of the actual function body. For example, in the Linux kernel, the code that sets up interrupt handlers needs to take the address of the interrupt handler function instead of the CFI jump table, as the jump table may not even be mapped into memory when an interrupt is triggered. This change adds the no_cfi constant type, which wraps function references in a value that LowerTypeTestsModule::replaceCfiUses does not replace. Link: https://github.com/ClangBuiltLinux/linux/issues/1353 Reviewed By: nickdesaulniers, pcc Differential Revision: https://reviews.llvm.org/D108478
2021-12-08[llvm] Use range-based for loops (NFC)Kazu Hirata1-6/+7
2021-10-29[IR] Merge createReplacementInstr into ConstantExpr::getAsInstructionJay Foad1-17/+20
createReplacementInstr was a trivial wrapper around ConstantExpr::getAsInstruction, which also inserted the new instruction into a basic block. Implement this directly in getAsInstruction by adding an InsertBefore parameter and change all callers to use it. NFC. A follow-up patch will remove createReplacementInstr. Differential Revision: https://reviews.llvm.org/D112791
2021-10-23[llvm] Use StringRef::contains (NFC)Kazu Hirata1-1/+1
2021-10-07[Inline] Introduce Constant::hasOneLiveUse, use it instead of hasOneUse in ↵Erik Desjardins1-14/+40
inline cost model (PR51667) Otherwise, inlining costs may be pessimized by dead constants. Fixes https://bugs.llvm.org/show_bug.cgi?id=51667. Reviewed By: mtrofin, aeubanks Differential Revision: https://reviews.llvm.org/D109294
2021-10-04[APFloat] Remove BitWidth argument from getAllOnesValueJay Foad1-2/+1
There's no need to pass this in explicitly because it is trivially available from the semantics.
2021-10-04[APInt] Stop using soft-deprecated constructors and methods in llvm. NFC.Jay Foad1-3/+3
Stop using APInt constructors and methods that were soft-deprecated in D109483. This fixes all the uses I found in llvm, except for the APInt unit tests which should still test the deprecated methods. Differential Revision: https://reviews.llvm.org/D110807
2021-09-09[IR] Handle constant expressions in containsUndefinedElement()Nikita Popov1-3/+5
If the constant is a constant expression, then getAggregateElement() will return null. Guard against this before calling HasFn().
2021-09-09[APInt] Normalize naming on keep constructors / predicate methods.Chris Lattner1-4/+3
This renames the primary methods for creating a zero value to `getZero` instead of `getNullValue` and renames predicates like `isAllOnesValue` to simply `isAllOnes`. This achieves two things: 1) This starts standardizing predicates across the LLVM codebase, following (in this case) ConstantInt. The word "Value" doesn't convey anything of merit, and is missing in some of the other things. 2) Calling an integer "null" doesn't make any sense. The original sin here is mine and I've regretted it for years. This moves us to calling it "zero" instead, which is correct! APInt is widely used and I don't think anyone is keen to take massive source breakage on anything so core, at least not all in one go. As such, this doesn't actually delete any entrypoints, it "soft deprecates" them with a comment. Included in this patch are changes to a bunch of the codebase, but there are more. We should normalize SelectionDAG and other APIs as well, which would make the API change more mechanical. Differential Revision: https://reviews.llvm.org/D109483
2021-08-27[IR] Remove getWithOperandReplaced (NFC)Kazu Hirata1-14/+0
The function hasn't been used for at least 10 years.
2021-08-10[IR] Let ConstantVector::getSplat use poison instead of undefDavid Sherwood1-3/+3
This patch updates ConstantVector::getSplat to use poison instead of undef when using insertelement/shufflevector to splat. This follows on from D93793. Differential Revision: https://reviews.llvm.org/D107751
2021-07-17[IR] Don't accept null type in ConstantExpr::getGetElementPtr()Nikita Popov1-4/+2
This is the same change as D105653, but for the constant expression version of the API.
2021-07-04[NFC] Fix a few whitespace issues and typos.Paul Walker1-0/+1
2021-07-02Place the BlockAddress type in the address space of the containing functionAlex Richardson1-2/+2
While this should not matter for most architectures (where the program address space is 0), it is important for CHERI (and therefore Arm Morello). We use address space 200 for all of our code pointers and without this change we assert in the SelectionDAG handling of BlockAddress nodes. It is also useful for AVR: previously programs targeting AVR that attempt to read their own machine code via a pointer to a label would instead read from RAM using a pointer relative to the the start of program flash. Reviewed By: dylanmckay, theraven Differential Revision: https://reviews.llvm.org/D48803
2021-06-23[Constants] Handle addrspacecast with opaque pointer typeNikita Popov1-3/+3
This is the same change as D104668, but for constant expression addrspacecasts.
2021-06-21[OpaquePtr] Support opaque constant expression GEPNikita Popov1-5/+7
Adjust assertions to use isOpaqueOrPointeeTypeMatches() and make it return an opaque pointer result for an opaque base pointer. We also need to enumerate the element type, as it is no longer implicitly enumerated through the pointer type. Differential Revision: https://reviews.llvm.org/D104655
2021-06-07[Constants] Extend support for scalable-vector splatsFraser Cormack1-98/+95
This patch extends the various "isXXX" functions of the `Constant` class to include scalable-vector splats. In several "isXXX" functions, code that was separately inspecting `ConstantVector` and `ConstantDataVector` was unified to use `getSplatValue`, which already includes support for said splats. In the varous "isNotXXX" functions, code was added to check whether the scalar splat value -- if any -- satisfies the predicate. An extra fix for `isNotMinSignedValue` was included, as it previously crashed when passed a scalable-vector type because it unconditionally cast to `FixedVectorType` These changes address numerous missed optimizations, a compiler crash mentioned above and -- perhaps most egregiously -- an infinite loop in InstCombine due to the compiler breaking canonical form when it failed to pick up on a splat in a select instruction. Test cases have been added to cover as many of these functions as possible, though existing coverage is slim; it doesn't appear that there are any in-tree uses of `Constant::isNegativeZeroValue`, for example. Reviewed By: RKSimon Differential Revision: https://reviews.llvm.org/D103421
2021-06-03[Constants][PowerPC] Check exactlyValue for ppc_fp128 in isNullValueJinsong Ji1-1/+3
PPC_FP128 determines isZero/isNan/isInf using high-order double value only. Checking isZero/isNegative might return the isNullValue unexpectedly. eg: 0xM0000000000000000FFFFFFFFFFFFFFFFF isZero, but it is not NullValue. Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D103634
2021-05-14[LowerConstantIntrinsics] reuse isManifestLogic from ConstantFoldingNick Desaulniers1-0/+12
GlobalVariables are Constants, yet should not unconditionally be considered true for __builtin_constant_p. Via the LangRef https://llvm.org/docs/LangRef.html#llvm-is-constant-intrinsic: This intrinsic generates no code. If its argument is known to be a manifest compile-time constant value, then the intrinsic will be converted to a constant true value. Otherwise, it will be converted to a constant false value. In particular, note that if the argument is a constant expression which refers to a global (the address of which _is_ a constant, but not manifest during the compile), then the intrinsic evaluates to false. Move isManifestConstant from ConstantFolding to be a method of Constant so that we can reuse the same logic in LowerConstantIntrinsics. pr/41459 Reviewed By: rsmith, george.burgess.iv Differential Revision: https://reviews.llvm.org/D102367
2021-05-10[Constant] Allow ConstantAggregateZero a scalable element countFraser Cormack1-7/+9
A ConstantAggregateZero may be created from a scalable vector type. However, it still assumed fixed number of elements when queried for them. This patch changes ConstantAggregateZero to correctly report its element count. This change fixes a couple of issues. Firstly, it fixes a crash in Constant::getUniqueValue when called on a scalable-vector zeroinitializer constant. Secondly, it fixes a latent bug in GlobalISel's IRTranslator in which translating a scalable-vector zeroinitializer would hit the assertion in ConstantAggregateZero::getNumElements when casting to a FixedVectorType, rather than reporting an error more gracefully. This is currently hypothetical as the IRTranslator has deeper issues preventing the use of scalable vector types. Reviewed By: RKSimon Differential Revision: https://reviews.llvm.org/D102082
2021-04-08[Constant] ConstantStruct/Array should not lower poison to undefJuneyoung Lee1-1/+11
This is a (late) follow-up patch of 8871a4b4cab8a56fd6ff12fd024002c3c79128b3 and c95f39891a282ebf36199c73b705d4a2c78a46ce to make ConstantStruct::get/ConstantArray::getImpl correctly return PoisonValue if all elements are poison. This was found while discussing about the elements of a vector-typed UndefValue (D99853)
2021-03-21Reapply [ConstantFold] Handle vectors in ConstantFoldLoadThroughBitcast()Nikita Popov1-0/+3
There seems to be an impedance mismatch between what the type system considers an aggregate (structs and arrays) and what constants consider an aggregate (structs, arrays and vectors). Adjust the type check to consider vectors as well. The previous version of the patch dropped the type check entirely, but it turns out that getAggregateElement() does require the constant to be an aggregate in some edge cases: For Poison/Undef the getNumElements() API is called, without checking in advance that we're dealing with an aggregate. Possibly the implementation should avoid doing that, but for now I'm adding an assert so the next person doesn't fall into this trap.
2021-03-09[llvm] Change DSOLocalEquivalent type if the underlying global value type ↵Leonard Chan1-0/+6
changes We encountered an issue where LTO running on IR that used the DSOLocalEquivalent constant would result in bad codegen. The underlying issue was ValueMapper wasn't properly handling DSOLocalEquivalent, so this just adds the machinery for handling it. This code path is triggered by a fix to DSOLocalEquivalent::handleOperandChangeImpl where DSOLocalEquivalent could potentially not have the same type as its underlying GV. This updates DSOLocalEquivalent::handleOperandChangeImpl to change the type if the GV type changes and handles this constant in ValueMapper. Differential Revision: https://reviews.llvm.org/D97978
2021-03-08[DebugInfo] Support DIArgList in DbgVariableIntrinsicgbtozers1-0/+6
This patch updates DbgVariableIntrinsics to support use of a DIArgList for the location operand, resulting in a significant change to its interface. This patch does not update all IR passes to support multiple location operands in a dbg.value; the only change is to update the DbgVariableIntrinsic interface and its uses. All code outside of the intrinsic classes assumes that an intrinsic will always have exactly one location operand; they will still support DIArgLists, but only if they contain exactly one Value. Among other changes, the setOperand and setArgOperand functions in DbgVariableIntrinsic have been made private. This is to prevent code from setting the operands of these intrinsics directly, which could easily result in incorrect/invalid operands being set. This does not prevent these functions from being called on a debug intrinsic at all, as they can still be called on any CallInst pointer; it is assumed that any code directly setting the operands on a generic call instruction is doing so safely. The intention for making these functions private is to prevent DIArgLists from being overwritten by code that's naively trying to replace one of the Values it points to, and also to fail fast if a DbgVariableIntrinsic is updated to use a DIArgList without a valid corresponding DIExpression.
2021-02-18[llvm][IR] Do not place constants with static relocations in a mergeable sectionLeonard Chan1-7/+16
This patch provides two major changes: 1. Add getRelocationInfo to check if a constant will have static, dynamic, or no relocations. (Also rename the original needsRelocation to needsDynamicRelocation.) 2. Only allow a constant with no relocations (static or dynamic) to be placed in a mergeable section. This will allow unused symbols that contain static relocations and happen to fit in mergeable constant sections (.rodata.cstN) to instead be placed in unique-named sections if -fdata-sections is used and subsequently garbage collected by --gc-sections. See https://lists.llvm.org/pipermail/llvm-dev/2021-February/148281.html. Differential Revision: https://reviews.llvm.org/D95960
2021-01-06[llvm] Use llvm::all_of (NFC)Kazu Hirata1-1/+1
2021-01-07[Constant] Update ConstantVector::get to return poison if all input elems ↵Juneyoung Lee1-1/+4
are poison The diff was reviewed at D93994
2021-01-06[Constant] Add containsPoisonElementJuneyoung Lee1-7/+18
This patch - Adds containsPoisonElement that checks existence of poison in constant vector elements, - Renames containsUndefElement to containsUndefOrPoisonElement to clarify its behavior & updates its uses properly With this patch, isGuaranteedNotToBeUndefOrPoison's tests w.r.t constant vectors are added because its analysis is improved. Thanks! Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D94053
2021-01-05[IR] Add ConstantInt::getBool helpers to wrap getTrue/getFalse.Simon Pilgrim1-0/+8
2021-01-04[llvm] Construct SmallVector with iterator ranges (NFC)Kazu Hirata1-1/+1
2020-12-01[llvm] Fix for failing test from fdbd84c6c819d4462546961f6086c1524d5d5ae8Leonard Chan1-2/+1
When handling a DSOLocalEquivalent operand change: - Remove assertion checking that the `To` type and current type are the same type. This is not always a requirement. - Add a missing bitcast from an old DSOLocalEquivalent to the type of the new one.
2020-11-30[llvm] Fix for failing test from cf8ff75bade763b054476321dcb82dcb2e7744c7Leonard Chan1-0/+5
Handle null values when handling operand changes for DSOLocalEquivalent.
2020-11-29[ConstantFold] Fold operations to poison if possibleJuneyoung Lee1-0/+3
This patch updates ConstantFold, so operations are folded into poison if possible. <alive2 proofs> casts: https://alive2.llvm.org/ce/z/WSj7rw binary operations (arithmetic): https://alive2.llvm.org/ce/z/_7dEyJ binary operations (bitwise): https://alive2.llvm.org/ce/z/cezjVN vector/aggregate operations: https://alive2.llvm.org/ce/z/BQ7hWz unary ops: https://alive2.llvm.org/ce/z/yBRs4q other ops: https://alive2.llvm.org/ce/z/iXbcFD Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D92203
2020-11-26Fix use-of-uninitialized-value in rG75f50e15bf8fZhengyang Liu1-1/+9
Differential Revision: https://reviews.llvm.org/D71126
2020-11-25Adding PoisonValue for representing poison value explicitly in IRZhengyang Liu1-0/+40
Define ConstantData::PoisonValue. Add support for poison value to LLLexer/LLParser/BitcodeReader/BitcodeWriter. Add support for poison value to llvm-c interface. Add support for poison value to OCaml binding. Add m_Poison in PatternMatch. Differential Revision: https://reviews.llvm.org/D71126
2020-11-24[IR] Constant::getAggregateElement - early-out for ScalableVectorTypeSimon Pilgrim1-4/+8
We can't call getNumElements() for ScalableVectorType types - just bail for now, although ConstantAggregateZero/UndefValue could return a reasonable value. Fixes crash shown in OSS-Fuzz #25272 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=25272
2020-11-19[llvm][IR] Add dso_local_equivalent ConstantLeonard Chan1-2/+60
The `dso_local_equivalent` constant is a wrapper for functions that represents a value which is functionally equivalent to the global passed to this. That is, if this accepts a function, calling this constant should have the same effects as calling the function directly. This could be a direct reference to the function, the `@plt` modifier on X86/AArch64, a thunk, or anything that's equivalent to the resolved function as a call target. When lowered, the returned address must have a constant offset at link time from some other symbol defined within the same binary. The address of this value is also insignificant. The name is leveraged from `dso_local` where use of a function or variable is resolved to a symbol in the same linkage unit. In this patch: - Addition of `dso_local_equivalent` and handling it - Update Constant::needsRelocation() to strip constant inbound GEPs and take advantage of `dso_local_equivalent` for relative references This is useful for the [Relative VTables C++ ABI](https://reviews.llvm.org/D72959) which makes vtables readonly. This works by replacing the dynamic relocations for function pointers in them with static relocations that represent the offset between the vtable and virtual functions. If a function is externally defined, `dso_local_equivalent` can be used as a generic wrapper for the function to still allow for this static offset calculation to be done. See [RFC](http://lists.llvm.org/pipermail/llvm-dev/2020-August/144469.html) for more details. Differential Revision: https://reviews.llvm.org/D77248
2020-10-26IR: Simplify two loops walking ConstantDataSequential, NFCDuncan P. N. Exon Smith1-8/+9
Follow-up to b2b7cf39d596b1528cd64015575b3f5d1461c011. Differential Revision: https://reviews.llvm.org/D90198
2020-10-26IR: Add a comment at missing std::make_unique calls from ↵Duncan P. N. Exon Smith1-0/+2
b2b7cf39d596b1528cd64015575b3f5d1461c011, NFC
2020-10-26IR: Clarify ownership of ConstantDataSequentials, NFCDuncan P. N. Exon Smith1-27/+26
Change `ConstantDataSequential::Next` to a `unique_ptr<ConstantDataSequential>` and update `CDSConstants` to a `StringMap<unique_ptr<ConstantDataSequential>>`, making the ownership more obvious. Differential Revision: https://reviews.llvm.org/D90083
2020-10-13[InstCombine] visitTrunc - pass through undefs for ↵Simon Pilgrim1-0/+34
trunc(shift(trunc/ext(x),c)) patterns Based on the recent patches D88475 and D88429 where we are losing undef values due to extension/comparisons. I've added a Constant::mergeUndefsWith method that merges the undef scalar/elements from another Constant into a specific Constant. Differential Revision: https://reviews.llvm.org/D88687
2020-10-11[InstCombine] Replace getLogBase2 internal helper with ↵Simon Pilgrim1-0/+29
ConstantExpr::getExactLogBase2. NFCI. This exposes the helper for other power-of-2 instcombine folds that I'm intending to add vector support to. The helper only operated on power-of-2 constants so getExactLogBase2 is a more accurate name.
2020-10-09[IR] Add Type::getFloatingPointTy.Bevin Hansson1-44/+9
It is possible to get a fltSemantics of a particular Type, but there is no way to produce a Type based on a fltSemantics. This adds the function Type::getFloatingPointTy, which will return the appropriate floating point Type for a given fltSemantics. ConstantFP is modified to use this function instead of implementing it itself. Also some minor refactors to use Type::getFltSemantics instead of a hand-rolled version. Differential Revision: https://reviews.llvm.org/D87512