Lines Matching full:constant
32 #include "llvm/IR/Constant.h"
68 // Constant Folding internal helper functions
71 static Constant *foldConstVectorToAPInt(APInt &Result, Type *DestTy, in foldConstVectorToAPInt()
72 Constant *C, Type *SrcEltTy, in foldConstVectorToAPInt()
79 Constant *Element; in foldConstVectorToAPInt()
101 /// Constant fold bitcast, symbolically evaluating it with DataLayout.
102 /// This always returns a non-null constant, but it may be a
104 Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { in FoldBitCast()
109 if (Constant *Res = ConstantFoldLoadFromUniformValue(C, DestTy, DL)) in FoldBitCast()
129 if (Constant *CE = foldConstVectorToAPInt(Result, DestTy, C, in FoldBitCast()
149 Constant *Ops = C; // don't take the address of C! in FoldBitCast()
153 // If this is a bitcast from constant vector -> vector, fold it. in FoldBitCast()
208 SmallVector<Constant*, 32> Result; in FoldBitCast()
211 Constant *Zero = Constant::getNullValue(DstEltTy); in FoldBitCast()
217 Constant *Elt = Zero; in FoldBitCast()
220 Constant *Src = C->getAggregateElement(SrcElt++); in FoldBitCast()
222 Src = Constant::getNullValue( in FoldBitCast()
232 assert(Src && "Constant folding cannot fail on plain integers"); in FoldBitCast()
238 assert(Src && "Constant folding cannot fail on plain integers"); in FoldBitCast()
244 assert(Elt && "Constant folding cannot fail on plain integers"); in FoldBitCast()
289 /// If this constant is a constant offset from a global, return the global and
290 /// the constant. Because of constantexprs, this function is recursive.
291 bool llvm::IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, in IsConstantOffsetFromGlobal()
297 // Trivial case, constant is the global. in IsConstantOffsetFromGlobal()
313 // Otherwise, if this isn't a constant expr, bail out. in IsConstantOffsetFromGlobal()
331 // If the base isn't a global+constant, we aren't either. in IsConstantOffsetFromGlobal()
344 Constant *llvm::ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, in ConstantFoldLoadThroughBitcast()
358 if (Constant *Res = ConstantFoldLoadFromUniformValue(C, DestTy, DL)) in ConstantFoldLoadThroughBitcast()
362 // cast the constant. in ConstantFoldLoadThroughBitcast()
380 // and find a bitcastable constant. in ConstantFoldLoadThroughBitcast()
392 Constant *ElemC; in ConstantFoldLoadThroughBitcast()
413 /// Recursive helper to read bits out of global. C is the constant being copied
417 bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr, in ReadDataFromGlobal()
546 Constant *FoldReinterpretLoadFromConst(Constant *C, Type *LoadTy, in FoldReinterpretLoadFromConst()
566 if (Constant *Res = FoldReinterpretLoadFromConst(C, MapTy, Offset, DL)) { in FoldReinterpretLoadFromConst()
570 return Constant::getNullValue(LoadTy); in FoldReinterpretLoadFromConst()
577 return Constant::getNullValue(LoadTy); in FoldReinterpretLoadFromConst()
592 // If we're not accessing anything in this constant, the result is undefined. in FoldReinterpretLoadFromConst()
601 // If we're not accessing anything in this constant, the result is undefined. in FoldReinterpretLoadFromConst()
639 // If GV is a constant with an initializer read its representation starting
640 // at Offset and return it as a constant array of unsigned char. Otherwise
642 Constant *llvm::ReadByteArrayFromGlobal(const GlobalVariable *GV, in ReadByteArrayFromGlobal()
648 Constant *Init = const_cast<Constant *>(GV->getInitializer()); in ReadByteArrayFromGlobal()
672 Constant *getConstantAtOffset(Constant *Base, APInt Offset, in getConstantAtOffset()
685 Constant *C = Base; in getConstantAtOffset()
698 Constant *llvm::ConstantFoldLoadFromConst(Constant *C, Type *Ty, in ConstantFoldLoadFromConst()
701 if (Constant *AtOffset = getConstantAtOffset(C, Offset, DL)) in ConstantFoldLoadFromConst()
702 if (Constant *Result = ConstantFoldLoadThroughBitcast(AtOffset, Ty, DL)) in ConstantFoldLoadFromConst()
706 // constant is a uniform value. in ConstantFoldLoadFromConst()
712 if (Constant *Result = ConstantFoldLoadFromUniformValue(C, Ty, DL)) in ConstantFoldLoadFromConst()
717 if (Constant *Result = in ConstantFoldLoadFromConst()
724 Constant *llvm::ConstantFoldLoadFromConst(Constant *C, Type *Ty, in ConstantFoldLoadFromConst()
729 Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, in ConstantFoldLoadFromConstPtr()
732 // We can only fold loads from constant globals with a definitive initializer. in ConstantFoldLoadFromConstPtr()
738 C = cast<Constant>(C->stripAndAccumulateConstantOffsets( in ConstantFoldLoadFromConstPtr()
742 if (Constant *Result = ConstantFoldLoadFromConst(GV->getInitializer(), Ty, in ConstantFoldLoadFromConstPtr()
746 // If this load comes from anywhere in a uniform constant global, the value in ConstantFoldLoadFromConstPtr()
751 Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, in ConstantFoldLoadFromConstPtr()
757 Constant *llvm::ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, in ConstantFoldLoadFromUniformValue()
768 return Constant::getNullValue(Ty); in ConstantFoldLoadFromUniformValue()
771 return Constant::getAllOnesValue(Ty); in ConstantFoldLoadFromUniformValue()
777 /// One of Op0/Op1 is a constant expression.
781 Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1, in SymbolicallyEvaluateBinop()
806 // If the constant expr is something like &A[123] - &A[4].f, fold this into a in SymbolicallyEvaluateBinop()
807 // constant. This happens frequently when iterating over a global array. in SymbolicallyEvaluateBinop()
829 Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops, in CastGEPIndices()
837 SmallVector<Constant*, 32> NewIdxs; in CastGEPIndices()
846 Constant *NewIdx = ConstantFoldCastOperand( in CastGEPIndices()
859 Constant *C = in CastGEPIndices()
864 /// If we can symbolically evaluate the GEP constant expression, do so.
865 Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP, in SymbolicallyEvaluateGEP()
866 ArrayRef<Constant *> Ops, in SymbolicallyEvaluateGEP()
874 if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy, GEP->getNoWrapFlags(), in SymbolicallyEvaluateGEP()
878 Constant *Ptr = Ops[0]; in SymbolicallyEvaluateGEP()
924 Ptr = cast<Constant>(GEP->getOperand(0)); in SymbolicallyEvaluateGEP()
949 Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr); in SymbolicallyEvaluateGEP()
970 /// Attempt to constant fold an instruction with the
971 /// specified opcode and operands. If successful, the constant result is
974 /// constant expression form.
975 Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode, in ConstantFoldInstOperandsImpl()
976 ArrayRef<Constant *> Ops, in ConstantFoldInstOperandsImpl()
995 // TODO: If a constant expression is being folded rather than an in ConstantFoldInstOperandsImpl()
1013 if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI)) in ConstantFoldInstOperandsImpl()
1069 // Constant Folding public APIs
1074 Constant *
1075 ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL, in ConstantFoldConstantImpl()
1077 SmallDenseMap<Constant *, Constant *> &FoldedOps) { in ConstantFoldConstantImpl() argument
1079 return const_cast<Constant *>(C); in ConstantFoldConstantImpl()
1081 SmallVector<Constant *, 8> Ops; in ConstantFoldConstantImpl()
1083 Constant *OldC = cast<Constant>(&OldU); in ConstantFoldConstantImpl()
1084 Constant *NewC = OldC; in ConstantFoldConstantImpl()
1100 if (Constant *Res = ConstantFoldInstOperandsImpl( in ConstantFoldConstantImpl()
1103 return const_cast<Constant *>(C); in ConstantFoldConstantImpl()
1112 Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL, in ConstantFoldInstruction()
1116 Constant *CommonValue = nullptr; in ConstantFoldInstruction()
1118 SmallDenseMap<Constant *, Constant *> FoldedOps; in ConstantFoldInstruction()
1122 // because that would break the rule that constant folding only applies if in ConstantFoldInstruction()
1126 // If the incoming value is not a constant, then give up. in ConstantFoldInstruction()
1127 auto *C = dyn_cast<Constant>(Incoming); in ConstantFoldInstruction()
1132 // If the incoming value is a different constant to in ConstantFoldInstruction()
1139 // If we reach here, all incoming values are the same constant or undef. in ConstantFoldInstruction()
1145 if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); })) in ConstantFoldInstruction()
1148 SmallDenseMap<Constant *, Constant *> FoldedOps; in ConstantFoldInstruction()
1149 SmallVector<Constant *, 8> Ops; in ConstantFoldInstruction()
1151 auto *Op = cast<Constant>(&OpU); in ConstantFoldInstruction()
1160 Constant *llvm::ConstantFoldConstant(const Constant *C, const DataLayout &DL, in ConstantFoldConstant()
1162 SmallDenseMap<Constant *, Constant *> FoldedOps; in ConstantFoldConstant()
1166 Constant *llvm::ConstantFoldInstOperands(Instruction *I, in ConstantFoldInstOperands()
1167 ArrayRef<Constant *> Ops, in ConstantFoldInstOperands()
1175 Constant *llvm::ConstantFoldCompareInstOperands( in ConstantFoldCompareInstOperands()
1176 unsigned IntPredicate, Constant *Ops0, Constant *Ops1, const DataLayout &DL, in ConstantFoldCompareInstOperands()
1195 if (Constant *C = ConstantFoldIntegerCast(CE0->getOperand(0), IntPtrTy, in ConstantFoldCompareInstOperands()
1197 Constant *Null = Constant::getNullValue(C->getType()); in ConstantFoldCompareInstOperands()
1207 Constant *C = CE0->getOperand(0); in ConstantFoldCompareInstOperands()
1208 Constant *Null = Constant::getNullValue(C->getType()); in ConstantFoldCompareInstOperands()
1221 Constant *C0 = ConstantFoldIntegerCast(CE0->getOperand(0), IntPtrTy, in ConstantFoldCompareInstOperands()
1223 Constant *C1 = ConstantFoldIntegerCast(CE1->getOperand(0), IntPtrTy, in ConstantFoldCompareInstOperands()
1262 // If RHS is a constant expression, but the left side isn't, swap the in ConstantFoldCompareInstOperands()
1268 // Flush any denormal constant float input according to denormal handling in ConstantFoldCompareInstOperands()
1280 Constant *llvm::ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, in ConstantFoldUnaryOpOperand()
1287 Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, in ConstantFoldBinaryOpOperands()
1288 Constant *RHS, in ConstantFoldBinaryOpOperands()
1292 if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL)) in ConstantFoldBinaryOpOperands()
1300 Constant *llvm::FlushFPConstant(Constant *Operand, const Instruction *I, in FlushFPConstant()
1343 Constant *llvm::ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, in ConstantFoldFPInstOperands()
1344 Constant *RHS, const DataLayout &DL, in ConstantFoldFPInstOperands()
1349 Constant *Op0 = FlushFPConstant(LHS, I, /* IsOutput */ false); in ConstantFoldFPInstOperands()
1352 Constant *Op1 = FlushFPConstant(RHS, I, /* IsOutput */ false); in ConstantFoldFPInstOperands()
1357 // may change due to future optimization. Don't constant fold them if in ConstantFoldFPInstOperands()
1365 // Calculate constant result. in ConstantFoldFPInstOperands()
1366 Constant *C = ConstantFoldBinaryOpOperands(Opcode, Op0, Op1, DL); in ConstantFoldFPInstOperands()
1386 Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C, in ConstantFoldCastOperand()
1394 Constant *FoldedValue = nullptr; in ConstantFoldCastOperand()
1408 auto *Base = cast<Constant>(GEP->stripAndAccumulateConstantOffsets( in ConstantFoldCastOperand()
1416 auto *Ptr = cast<Constant>(GEP->getPointerOperand()); in ConstantFoldCastOperand()
1441 Constant *SrcPtr = CE->getOperand(0); in ConstantFoldCastOperand()
1473 Constant *llvm::ConstantFoldIntegerCast(Constant *C, Type *DestTy, in ConstantFoldIntegerCast()
1486 // Constant Folding for Calls
1734 Constant *GetConstantFoldFPValue(double V, Type *Ty) { in GetConstantFoldFPValue()
1743 llvm_unreachable("Can only constant fold half/float/double"); in GetConstantFoldFPValue()
1747 Constant *GetConstantFoldFPValue128(float128 V, Type *Ty) { in GetConstantFoldFPValue128()
1750 llvm_unreachable("Can only constant fold fp128"); in GetConstantFoldFPValue128()
1774 Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V, in ConstantFoldFP()
1787 Constant *ConstantFoldFP128(float128 (*NativeFP)(float128), const APFloat &V, in ConstantFoldFP128()
1800 Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), in ConstantFoldBinaryFP()
1812 Constant *constantFoldVectorReduce(Intrinsic::ID IID, Constant *Op) { in constantFoldVectorReduce()
1873 /// Attempt to fold an SSE floating point to integer conversion of a constant
1879 /// returns the Constant value resulting from the conversion.
1880 Constant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero, in ConstantFoldSSEConvertToInt()
1885 "Can only constant fold conversions to 64 and 32 bit ints"); in ConstantFoldSSEConvertToInt()
1924 /// Checks if the given intrinsic call, which evaluates to constant, is allowed
1928 /// \param St Exception flags raised during constant evaluation.
1954 /// Returns the rounding mode that should be used for constant evaluation.
1967 /// Try to constant fold llvm.canonicalize for the given caller and value.
1968 static Constant *constantFoldCanonicalize(const Type *Ty, const CallBase *CI, in constantFoldCanonicalize()
2017 static Constant *ConstantFoldScalarCall1(StringRef Name, in ConstantFoldScalarCall1()
2020 ArrayRef<Constant *> Operands, in ConstantFoldScalarCall1()
2026 // We know we have a "Constant" argument. But we want to only in ConstantFoldScalarCall1()
2049 return Constant::getNullValue(Ty); in ConstantFoldScalarCall1()
2520 if (Constant *C = constantFoldVectorReduce(IntrinsicID, Operands[0])) in ConstantFoldScalarCall1()
2528 auto *Op = cast<Constant>(Operands[0]); in ConstantFoldScalarCall1()
2557 static Constant *evaluateCompare(const APFloat &Op1, const APFloat &Op2, in evaluateCompare()
2575 static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty, in ConstantFoldLibCall2()
2576 ArrayRef<Constant *> Operands, in ConstantFoldLibCall2()
2639 static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty, in ConstantFoldIntrinsicCall2()
2640 ArrayRef<Constant *> Operands, in ConstantFoldIntrinsicCall2()
2831 return Constant::getNullValue(Ty); in ConstantFoldIntrinsicCall2()
2840 {Constant::getAllOnesValue(Ty->getStructElementType(0)), in ConstantFoldIntrinsicCall2()
2841 Constant::getNullValue(Ty->getStructElementType(1))}); in ConstantFoldIntrinsicCall2()
2849 return Constant::getNullValue(Ty); in ConstantFoldIntrinsicCall2()
2874 Constant *Ops[] = { in ConstantFoldIntrinsicCall2()
2890 return Constant::getAllOnesValue(Ty); in ConstantFoldIntrinsicCall2()
2905 return Constant::getNullValue(Ty); in ConstantFoldIntrinsicCall2()
2912 assert(C1 && "Must be constant int"); in ConstantFoldIntrinsicCall2()
2918 return Constant::getNullValue(Ty); in ConstantFoldIntrinsicCall2()
2925 assert(C1 && "Must be constant int"); in ConstantFoldIntrinsicCall2()
2934 return Constant::getNullValue(Ty); in ConstantFoldIntrinsicCall2()
2939 return dyn_cast<Constant>(Operands[0]); in ConstantFoldIntrinsicCall2()
2952 auto *Op = cast<Constant>(Operands[0]); in ConstantFoldIntrinsicCall2()
3055 static Constant *ConstantFoldAMDGCNPermIntrinsic(ArrayRef<Constant *> Operands, in ConstantFoldAMDGCNPermIntrinsic()
3095 static Constant *ConstantFoldScalarCall3(StringRef Name, in ConstantFoldScalarCall3()
3098 ArrayRef<Constant *> Operands, in ConstantFoldScalarCall3()
3173 return Constant::getNullValue(Ty); in ConstantFoldScalarCall3()
3232 static Constant *ConstantFoldScalarCall(StringRef Name, in ConstantFoldScalarCall()
3235 ArrayRef<Constant *> Operands, in ConstantFoldScalarCall()
3242 if (Constant *FoldedLibCall = in ConstantFoldScalarCall()
3255 static Constant *ConstantFoldFixedVectorCall( in ConstantFoldFixedVectorCall()
3257 ArrayRef<Constant *> Operands, const DataLayout &DL, in ConstantFoldFixedVectorCall()
3259 SmallVector<Constant *, 4> Result(FVTy->getNumElements()); in ConstantFoldFixedVectorCall()
3260 SmallVector<Constant *, 4> Lane(Operands.size()); in ConstantFoldFixedVectorCall()
3269 Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, FVTy, DL); in ConstantFoldFixedVectorCall()
3271 SmallVector<Constant *, 32> NewElements; in ConstantFoldFixedVectorCall()
3310 SmallVector<Constant *, 16> NCs; in ConstantFoldFixedVectorCall()
3329 SmallVector<Constant *, 16> NCs; in ConstantFoldFixedVectorCall()
3353 Constant *Agg = Operands[J]->getAggregateElement(I); in ConstantFoldFixedVectorCall()
3361 Constant *Folded = in ConstantFoldFixedVectorCall()
3371 static Constant *ConstantFoldScalableVectorCall( in ConstantFoldScalableVectorCall()
3373 ArrayRef<Constant *> Operands, const DataLayout &DL, in ConstantFoldScalableVectorCall()
3377 auto *Src = dyn_cast<Constant>(Operands[0]); in ConstantFoldScalableVectorCall()
3389 static std::pair<Constant *, Constant *>
3390 ConstantFoldScalarFrexpCall(Constant *Op, Type *IntTy) { in ConstantFoldScalarFrexpCall()
3401 Constant *Result0 = ConstantFP::get(ConstFP->getType(), FrexpMant); in ConstantFoldScalarFrexpCall()
3405 Constant *Result1 = FrexpMant.isFinite() ? ConstantInt::get(IntTy, FrexpExp) in ConstantFoldScalarFrexpCall()
3411 static Constant *
3413 StructType *StTy, ArrayRef<Constant *> Operands, in ConstantFoldStructCall()
3423 SmallVector<Constant *, 4> Results0(FVTy0->getNumElements()); in ConstantFoldStructCall()
3424 SmallVector<Constant *, 4> Results1(FVTy0->getNumElements()); in ConstantFoldStructCall()
3427 Constant *Lane = Operands[0]->getAggregateElement(I); in ConstantFoldStructCall()
3444 // TODO: Constant folding of vector intrinsics that fall through here does in ConstantFoldStructCall()
3454 Constant *llvm::ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS, in ConstantFoldBinaryIntrinsic()
3455 Constant *RHS, Type *Ty, in ConstantFoldBinaryIntrinsic()
3461 Constant *llvm::ConstantFoldCall(const CallBase *Call, Function *F, in ConstantFoldCall()
3462 ArrayRef<Constant *> Operands, in ConstantFoldCall()