1 //===-- ConstantFolding.cpp - Fold instructions into constants ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines routines for folding instructions into constants.
12 // Also, to supplement the basic IR ConstantExpr simplifications,
13 // this file defines some additional folding routines that can make use of
14 // DataLayout information. These functions cannot go in IR due to library
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Analysis/ConstantFolding.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/Analysis/ValueTracking.h"
24 #include "llvm/Config/config.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/GetElementPtrTypeIterator.h"
30 #include "llvm/IR/GlobalVariable.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/Intrinsics.h"
33 #include "llvm/IR/Operator.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Target/TargetLibraryInfo.h"
46 //===----------------------------------------------------------------------===//
47 // Constant Folding internal helper functions
48 //===----------------------------------------------------------------------===//
50 /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with
51 /// DataLayout. This always returns a non-null constant, but it may be a
52 /// ConstantExpr if unfoldable.
53 static Constant *FoldBitCast(Constant *C, Type *DestTy,
54 const DataLayout &TD) {
55 // Catch the obvious splat cases.
56 if (C->isNullValue() && !DestTy->isX86_MMXTy())
57 return Constant::getNullValue(DestTy);
58 if (C->isAllOnesValue() && !DestTy->isX86_MMXTy())
59 return Constant::getAllOnesValue(DestTy);
61 // Handle a vector->integer cast.
62 if (IntegerType *IT = dyn_cast<IntegerType>(DestTy)) {
63 VectorType *VTy = dyn_cast<VectorType>(C->getType());
65 return ConstantExpr::getBitCast(C, DestTy);
67 unsigned NumSrcElts = VTy->getNumElements();
68 Type *SrcEltTy = VTy->getElementType();
70 // If the vector is a vector of floating point, convert it to vector of int
71 // to simplify things.
72 if (SrcEltTy->isFloatingPointTy()) {
73 unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
75 VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElts);
76 // Ask IR to do the conversion now that #elts line up.
77 C = ConstantExpr::getBitCast(C, SrcIVTy);
80 ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
82 return ConstantExpr::getBitCast(C, DestTy);
84 // Now that we know that the input value is a vector of integers, just shift
85 // and insert them into our result.
86 unsigned BitShift = TD.getTypeAllocSizeInBits(SrcEltTy);
87 APInt Result(IT->getBitWidth(), 0);
88 for (unsigned i = 0; i != NumSrcElts; ++i) {
90 if (TD.isLittleEndian())
91 Result |= CDV->getElementAsInteger(NumSrcElts-i-1);
93 Result |= CDV->getElementAsInteger(i);
96 return ConstantInt::get(IT, Result);
99 // The code below only handles casts to vectors currently.
100 VectorType *DestVTy = dyn_cast<VectorType>(DestTy);
102 return ConstantExpr::getBitCast(C, DestTy);
104 // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
105 // vector so the code below can handle it uniformly.
106 if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) {
107 Constant *Ops = C; // don't take the address of C!
108 return FoldBitCast(ConstantVector::get(Ops), DestTy, TD);
111 // If this is a bitcast from constant vector -> vector, fold it.
112 if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
113 return ConstantExpr::getBitCast(C, DestTy);
115 // If the element types match, IR can fold it.
116 unsigned NumDstElt = DestVTy->getNumElements();
117 unsigned NumSrcElt = C->getType()->getVectorNumElements();
118 if (NumDstElt == NumSrcElt)
119 return ConstantExpr::getBitCast(C, DestTy);
121 Type *SrcEltTy = C->getType()->getVectorElementType();
122 Type *DstEltTy = DestVTy->getElementType();
124 // Otherwise, we're changing the number of elements in a vector, which
125 // requires endianness information to do the right thing. For example,
126 // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
127 // folds to (little endian):
128 // <4 x i32> <i32 0, i32 0, i32 1, i32 0>
129 // and to (big endian):
130 // <4 x i32> <i32 0, i32 0, i32 0, i32 1>
132 // First thing is first. We only want to think about integer here, so if
133 // we have something in FP form, recast it as integer.
134 if (DstEltTy->isFloatingPointTy()) {
135 // Fold to an vector of integers with same size as our FP type.
136 unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
138 VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt);
139 // Recursively handle this integer conversion, if possible.
140 C = FoldBitCast(C, DestIVTy, TD);
142 // Finally, IR can handle this now that #elts line up.
143 return ConstantExpr::getBitCast(C, DestTy);
146 // Okay, we know the destination is integer, if the input is FP, convert
147 // it to integer first.
148 if (SrcEltTy->isFloatingPointTy()) {
149 unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
151 VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
152 // Ask IR to do the conversion now that #elts line up.
153 C = ConstantExpr::getBitCast(C, SrcIVTy);
154 // If IR wasn't able to fold it, bail out.
155 if (!isa<ConstantVector>(C) && // FIXME: Remove ConstantVector.
156 !isa<ConstantDataVector>(C))
160 // Now we know that the input and output vectors are both integer vectors
161 // of the same size, and that their #elements is not the same. Do the
162 // conversion here, which depends on whether the input or output has
164 bool isLittleEndian = TD.isLittleEndian();
166 SmallVector<Constant*, 32> Result;
167 if (NumDstElt < NumSrcElt) {
168 // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
169 Constant *Zero = Constant::getNullValue(DstEltTy);
170 unsigned Ratio = NumSrcElt/NumDstElt;
171 unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
173 for (unsigned i = 0; i != NumDstElt; ++i) {
174 // Build each element of the result.
175 Constant *Elt = Zero;
176 unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
177 for (unsigned j = 0; j != Ratio; ++j) {
178 Constant *Src =dyn_cast<ConstantInt>(C->getAggregateElement(SrcElt++));
179 if (!Src) // Reject constantexpr elements.
180 return ConstantExpr::getBitCast(C, DestTy);
182 // Zero extend the element to the right size.
183 Src = ConstantExpr::getZExt(Src, Elt->getType());
185 // Shift it to the right place, depending on endianness.
186 Src = ConstantExpr::getShl(Src,
187 ConstantInt::get(Src->getType(), ShiftAmt));
188 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
191 Elt = ConstantExpr::getOr(Elt, Src);
193 Result.push_back(Elt);
195 return ConstantVector::get(Result);
198 // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
199 unsigned Ratio = NumDstElt/NumSrcElt;
200 unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits();
202 // Loop over each source value, expanding into multiple results.
203 for (unsigned i = 0; i != NumSrcElt; ++i) {
204 Constant *Src = dyn_cast<ConstantInt>(C->getAggregateElement(i));
205 if (!Src) // Reject constantexpr elements.
206 return ConstantExpr::getBitCast(C, DestTy);
208 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
209 for (unsigned j = 0; j != Ratio; ++j) {
210 // Shift the piece of the value into the right place, depending on
212 Constant *Elt = ConstantExpr::getLShr(Src,
213 ConstantInt::get(Src->getType(), ShiftAmt));
214 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
216 // Truncate and remember this piece.
217 Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
221 return ConstantVector::get(Result);
225 /// IsConstantOffsetFromGlobal - If this constant is actually a constant offset
226 /// from a global, return the global and the constant. Because of
227 /// constantexprs, this function is recursive.
228 static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
229 APInt &Offset, const DataLayout &TD) {
230 // Trivial case, constant is the global.
231 if ((GV = dyn_cast<GlobalValue>(C))) {
232 unsigned BitWidth = TD.getPointerTypeSizeInBits(GV->getType());
233 Offset = APInt(BitWidth, 0);
237 // Otherwise, if this isn't a constant expr, bail out.
238 ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
239 if (!CE) return false;
241 // Look through ptr->int and ptr->ptr casts.
242 if (CE->getOpcode() == Instruction::PtrToInt ||
243 CE->getOpcode() == Instruction::BitCast ||
244 CE->getOpcode() == Instruction::AddrSpaceCast)
245 return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD);
247 // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
248 GEPOperator *GEP = dyn_cast<GEPOperator>(CE);
252 unsigned BitWidth = TD.getPointerTypeSizeInBits(GEP->getType());
253 APInt TmpOffset(BitWidth, 0);
255 // If the base isn't a global+constant, we aren't either.
256 if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, TD))
259 // Otherwise, add any offset that our operands provide.
260 if (!GEP->accumulateConstantOffset(TD, TmpOffset))
267 /// ReadDataFromGlobal - Recursive helper to read bits out of global. C is the
268 /// constant being copied out of. ByteOffset is an offset into C. CurPtr is the
269 /// pointer to copy results into and BytesLeft is the number of bytes left in
270 /// the CurPtr buffer. TD is the target data.
271 static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset,
272 unsigned char *CurPtr, unsigned BytesLeft,
273 const DataLayout &TD) {
274 assert(ByteOffset <= TD.getTypeAllocSize(C->getType()) &&
275 "Out of range access");
277 // If this element is zero or undefined, we can just return since *CurPtr is
279 if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
282 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
283 if (CI->getBitWidth() > 64 ||
284 (CI->getBitWidth() & 7) != 0)
287 uint64_t Val = CI->getZExtValue();
288 unsigned IntBytes = unsigned(CI->getBitWidth()/8);
290 for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
292 if (!TD.isLittleEndian())
293 n = IntBytes - n - 1;
294 CurPtr[i] = (unsigned char)(Val >> (n * 8));
300 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
301 if (CFP->getType()->isDoubleTy()) {
302 C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), TD);
303 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
305 if (CFP->getType()->isFloatTy()){
306 C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), TD);
307 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
309 if (CFP->getType()->isHalfTy()){
310 C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), TD);
311 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
316 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
317 const StructLayout *SL = TD.getStructLayout(CS->getType());
318 unsigned Index = SL->getElementContainingOffset(ByteOffset);
319 uint64_t CurEltOffset = SL->getElementOffset(Index);
320 ByteOffset -= CurEltOffset;
323 // If the element access is to the element itself and not to tail padding,
324 // read the bytes from the element.
325 uint64_t EltSize = TD.getTypeAllocSize(CS->getOperand(Index)->getType());
327 if (ByteOffset < EltSize &&
328 !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
334 // Check to see if we read from the last struct element, if so we're done.
335 if (Index == CS->getType()->getNumElements())
338 // If we read all of the bytes we needed from this element we're done.
339 uint64_t NextEltOffset = SL->getElementOffset(Index);
341 if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
344 // Move to the next element of the struct.
345 CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
346 BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
348 CurEltOffset = NextEltOffset;
353 if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
354 isa<ConstantDataSequential>(C)) {
355 Type *EltTy = C->getType()->getSequentialElementType();
356 uint64_t EltSize = TD.getTypeAllocSize(EltTy);
357 uint64_t Index = ByteOffset / EltSize;
358 uint64_t Offset = ByteOffset - Index * EltSize;
360 if (ArrayType *AT = dyn_cast<ArrayType>(C->getType()))
361 NumElts = AT->getNumElements();
363 NumElts = C->getType()->getVectorNumElements();
365 for (; Index != NumElts; ++Index) {
366 if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
370 uint64_t BytesWritten = EltSize - Offset;
371 assert(BytesWritten <= EltSize && "Not indexing into this element?");
372 if (BytesWritten >= BytesLeft)
376 BytesLeft -= BytesWritten;
377 CurPtr += BytesWritten;
382 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
383 if (CE->getOpcode() == Instruction::IntToPtr &&
384 CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getType())) {
385 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
390 // Otherwise, unknown initializer type.
394 static Constant *FoldReinterpretLoadFromConstPtr(Constant *C,
395 const DataLayout &TD) {
396 PointerType *PTy = cast<PointerType>(C->getType());
397 Type *LoadTy = PTy->getElementType();
398 IntegerType *IntType = dyn_cast<IntegerType>(LoadTy);
400 // If this isn't an integer load we can't fold it directly.
402 unsigned AS = PTy->getAddressSpace();
404 // If this is a float/double load, we can try folding it as an int32/64 load
405 // and then bitcast the result. This can be useful for union cases. Note
406 // that address spaces don't matter here since we're not going to result in
407 // an actual new load.
409 if (LoadTy->isHalfTy())
410 MapTy = Type::getInt16PtrTy(C->getContext(), AS);
411 else if (LoadTy->isFloatTy())
412 MapTy = Type::getInt32PtrTy(C->getContext(), AS);
413 else if (LoadTy->isDoubleTy())
414 MapTy = Type::getInt64PtrTy(C->getContext(), AS);
415 else if (LoadTy->isVectorTy()) {
416 MapTy = PointerType::getIntNPtrTy(C->getContext(),
417 TD.getTypeAllocSizeInBits(LoadTy),
422 C = FoldBitCast(C, MapTy, TD);
423 if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, TD))
424 return FoldBitCast(Res, LoadTy, TD);
428 unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
429 if (BytesLoaded > 32 || BytesLoaded == 0)
434 if (!IsConstantOffsetFromGlobal(C, GVal, Offset, TD))
437 GlobalVariable *GV = dyn_cast<GlobalVariable>(GVal);
438 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
439 !GV->getInitializer()->getType()->isSized())
442 // If we're loading off the beginning of the global, some bytes may be valid,
443 // but we don't try to handle this.
444 if (Offset.isNegative())
447 // If we're not accessing anything in this constant, the result is undefined.
448 if (Offset.getZExtValue() >=
449 TD.getTypeAllocSize(GV->getInitializer()->getType()))
450 return UndefValue::get(IntType);
452 unsigned char RawBytes[32] = {0};
453 if (!ReadDataFromGlobal(GV->getInitializer(), Offset.getZExtValue(), RawBytes,
457 APInt ResultVal = APInt(IntType->getBitWidth(), 0);
458 if (TD.isLittleEndian()) {
459 ResultVal = RawBytes[BytesLoaded - 1];
460 for (unsigned i = 1; i != BytesLoaded; ++i) {
462 ResultVal |= RawBytes[BytesLoaded - 1 - i];
465 ResultVal = RawBytes[0];
466 for (unsigned i = 1; i != BytesLoaded; ++i) {
468 ResultVal |= RawBytes[i];
472 return ConstantInt::get(IntType->getContext(), ResultVal);
475 static Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE,
476 const DataLayout *DL) {
479 auto *DestPtrTy = dyn_cast<PointerType>(CE->getType());
482 Type *DestTy = DestPtrTy->getElementType();
484 Constant *C = ConstantFoldLoadFromConstPtr(CE->getOperand(0), DL);
489 Type *SrcTy = C->getType();
491 // If the type sizes are the same and a cast is legal, just directly
492 // cast the constant.
493 if (DL->getTypeSizeInBits(DestTy) == DL->getTypeSizeInBits(SrcTy)) {
494 Instruction::CastOps Cast = Instruction::BitCast;
495 // If we are going from a pointer to int or vice versa, we spell the cast
497 if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
498 Cast = Instruction::IntToPtr;
499 else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
500 Cast = Instruction::PtrToInt;
502 if (CastInst::castIsValid(Cast, C, DestTy))
503 return ConstantExpr::getCast(Cast, C, DestTy);
506 // If this isn't an aggregate type, there is nothing we can do to drill down
507 // and find a bitcastable constant.
508 if (!SrcTy->isAggregateType())
511 // We're simulating a load through a pointer that was bitcast to point to
512 // a different type, so we can try to walk down through the initial
513 // elements of an aggregate to see if some part of th e aggregate is
514 // castable to implement the "load" semantic model.
515 C = C->getAggregateElement(0u);
521 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
522 /// produce if it is constant and determinable. If this is not determinable,
524 Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C,
525 const DataLayout *TD) {
526 // First, try the easy cases:
527 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
528 if (GV->isConstant() && GV->hasDefinitiveInitializer())
529 return GV->getInitializer();
531 // If the loaded value isn't a constant expr, we can't handle it.
532 ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
536 if (CE->getOpcode() == Instruction::GetElementPtr) {
537 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
538 if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
540 ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
546 if (CE->getOpcode() == Instruction::BitCast)
547 if (Constant *LoadedC = ConstantFoldLoadThroughBitcast(CE, TD))
550 // Instead of loading constant c string, use corresponding integer value
551 // directly if string length is small enough.
553 if (TD && getConstantStringInfo(CE, Str) && !Str.empty()) {
554 unsigned StrLen = Str.size();
555 Type *Ty = cast<PointerType>(CE->getType())->getElementType();
556 unsigned NumBits = Ty->getPrimitiveSizeInBits();
557 // Replace load with immediate integer if the result is an integer or fp
559 if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
560 (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) {
561 APInt StrVal(NumBits, 0);
562 APInt SingleChar(NumBits, 0);
563 if (TD->isLittleEndian()) {
564 for (signed i = StrLen-1; i >= 0; i--) {
565 SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
566 StrVal = (StrVal << 8) | SingleChar;
569 for (unsigned i = 0; i < StrLen; i++) {
570 SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
571 StrVal = (StrVal << 8) | SingleChar;
573 // Append NULL at the end.
575 StrVal = (StrVal << 8) | SingleChar;
578 Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
579 if (Ty->isFloatingPointTy())
580 Res = ConstantExpr::getBitCast(Res, Ty);
585 // If this load comes from anywhere in a constant global, and if the global
586 // is all undef or zero, we know what it loads.
587 if (GlobalVariable *GV =
588 dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, TD))) {
589 if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
590 Type *ResTy = cast<PointerType>(C->getType())->getElementType();
591 if (GV->getInitializer()->isNullValue())
592 return Constant::getNullValue(ResTy);
593 if (isa<UndefValue>(GV->getInitializer()))
594 return UndefValue::get(ResTy);
598 // Try hard to fold loads from bitcasted strange and non-type-safe things.
600 return FoldReinterpretLoadFromConstPtr(CE, *TD);
604 static Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout *TD){
605 if (LI->isVolatile()) return nullptr;
607 if (Constant *C = dyn_cast<Constant>(LI->getOperand(0)))
608 return ConstantFoldLoadFromConstPtr(C, TD);
613 /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression.
614 /// Attempt to symbolically evaluate the result of a binary operator merging
615 /// these together. If target data info is available, it is provided as DL,
616 /// otherwise DL is null.
617 static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
618 Constant *Op1, const DataLayout *DL){
621 // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
622 // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
626 if (Opc == Instruction::And && DL) {
627 unsigned BitWidth = DL->getTypeSizeInBits(Op0->getType()->getScalarType());
628 APInt KnownZero0(BitWidth, 0), KnownOne0(BitWidth, 0);
629 APInt KnownZero1(BitWidth, 0), KnownOne1(BitWidth, 0);
630 computeKnownBits(Op0, KnownZero0, KnownOne0, DL);
631 computeKnownBits(Op1, KnownZero1, KnownOne1, DL);
632 if ((KnownOne1 | KnownZero0).isAllOnesValue()) {
633 // All the bits of Op0 that the 'and' could be masking are already zero.
636 if ((KnownOne0 | KnownZero1).isAllOnesValue()) {
637 // All the bits of Op1 that the 'and' could be masking are already zero.
641 APInt KnownZero = KnownZero0 | KnownZero1;
642 APInt KnownOne = KnownOne0 & KnownOne1;
643 if ((KnownZero | KnownOne).isAllOnesValue()) {
644 return ConstantInt::get(Op0->getType(), KnownOne);
648 // If the constant expr is something like &A[123] - &A[4].f, fold this into a
649 // constant. This happens frequently when iterating over a global array.
650 if (Opc == Instruction::Sub && DL) {
651 GlobalValue *GV1, *GV2;
654 if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *DL))
655 if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *DL) &&
657 unsigned OpSize = DL->getTypeSizeInBits(Op0->getType());
659 // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
660 // PtrToInt may change the bitwidth so we have convert to the right size
662 return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
663 Offs2.zextOrTrunc(OpSize));
670 /// CastGEPIndices - If array indices are not pointer-sized integers,
671 /// explicitly cast them so that they aren't implicitly casted by the
673 static Constant *CastGEPIndices(ArrayRef<Constant *> Ops,
674 Type *ResultTy, const DataLayout *TD,
675 const TargetLibraryInfo *TLI) {
679 Type *IntPtrTy = TD->getIntPtrType(ResultTy);
682 SmallVector<Constant*, 32> NewIdxs;
683 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
685 !isa<StructType>(GetElementPtrInst::getIndexedType(
687 Ops.slice(1, i - 1)))) &&
688 Ops[i]->getType() != IntPtrTy) {
690 NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i],
696 NewIdxs.push_back(Ops[i]);
702 Constant *C = ConstantExpr::getGetElementPtr(Ops[0], NewIdxs);
703 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
704 if (Constant *Folded = ConstantFoldConstantExpression(CE, TD, TLI))
711 /// Strip the pointer casts, but preserve the address space information.
712 static Constant* StripPtrCastKeepAS(Constant* Ptr) {
713 assert(Ptr->getType()->isPointerTy() && "Not a pointer type");
714 PointerType *OldPtrTy = cast<PointerType>(Ptr->getType());
715 Ptr = Ptr->stripPointerCasts();
716 PointerType *NewPtrTy = cast<PointerType>(Ptr->getType());
718 // Preserve the address space number of the pointer.
719 if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
720 NewPtrTy = NewPtrTy->getElementType()->getPointerTo(
721 OldPtrTy->getAddressSpace());
722 Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy);
727 /// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP
728 /// constant expression, do so.
729 static Constant *SymbolicallyEvaluateGEP(ArrayRef<Constant *> Ops,
730 Type *ResultTy, const DataLayout *TD,
731 const TargetLibraryInfo *TLI) {
732 Constant *Ptr = Ops[0];
733 if (!TD || !Ptr->getType()->getPointerElementType()->isSized() ||
734 !Ptr->getType()->isPointerTy())
737 Type *IntPtrTy = TD->getIntPtrType(Ptr->getType());
738 Type *ResultElementTy = ResultTy->getPointerElementType();
740 // If this is a constant expr gep that is effectively computing an
741 // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
742 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
743 if (!isa<ConstantInt>(Ops[i])) {
745 // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
746 // "inttoptr (sub (ptrtoint Ptr), V)"
747 if (Ops.size() == 2 && ResultElementTy->isIntegerTy(8)) {
748 ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[1]);
749 assert((!CE || CE->getType() == IntPtrTy) &&
750 "CastGEPIndices didn't canonicalize index types!");
751 if (CE && CE->getOpcode() == Instruction::Sub &&
752 CE->getOperand(0)->isNullValue()) {
753 Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
754 Res = ConstantExpr::getSub(Res, CE->getOperand(1));
755 Res = ConstantExpr::getIntToPtr(Res, ResultTy);
756 if (ConstantExpr *ResCE = dyn_cast<ConstantExpr>(Res))
757 Res = ConstantFoldConstantExpression(ResCE, TD, TLI);
764 unsigned BitWidth = TD->getTypeSizeInBits(IntPtrTy);
766 APInt(BitWidth, TD->getIndexedOffset(Ptr->getType(),
767 makeArrayRef((Value *const*)
770 Ptr = StripPtrCastKeepAS(Ptr);
772 // If this is a GEP of a GEP, fold it all into a single GEP.
773 while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
774 SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end());
776 // Do not try the incorporate the sub-GEP if some index is not a number.
777 bool AllConstantInt = true;
778 for (unsigned i = 0, e = NestedOps.size(); i != e; ++i)
779 if (!isa<ConstantInt>(NestedOps[i])) {
780 AllConstantInt = false;
786 Ptr = cast<Constant>(GEP->getOperand(0));
787 Offset += APInt(BitWidth,
788 TD->getIndexedOffset(Ptr->getType(), NestedOps));
789 Ptr = StripPtrCastKeepAS(Ptr);
792 // If the base value for this address is a literal integer value, fold the
793 // getelementptr to the resulting integer value casted to the pointer type.
794 APInt BasePtr(BitWidth, 0);
795 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
796 if (CE->getOpcode() == Instruction::IntToPtr) {
797 if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
798 BasePtr = Base->getValue().zextOrTrunc(BitWidth);
802 if (Ptr->isNullValue() || BasePtr != 0) {
803 Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr);
804 return ConstantExpr::getIntToPtr(C, ResultTy);
807 // Otherwise form a regular getelementptr. Recompute the indices so that
808 // we eliminate over-indexing of the notional static type array bounds.
809 // This makes it easy to determine if the getelementptr is "inbounds".
810 // Also, this helps GlobalOpt do SROA on GlobalVariables.
811 Type *Ty = Ptr->getType();
812 assert(Ty->isPointerTy() && "Forming regular GEP of non-pointer type");
813 SmallVector<Constant *, 32> NewIdxs;
816 if (SequentialType *ATy = dyn_cast<SequentialType>(Ty)) {
817 if (ATy->isPointerTy()) {
818 // The only pointer indexing we'll do is on the first index of the GEP.
819 if (!NewIdxs.empty())
822 // Only handle pointers to sized types, not pointers to functions.
823 if (!ATy->getElementType()->isSized())
827 // Determine which element of the array the offset points into.
828 APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType()));
830 // The element size is 0. This may be [0 x Ty]*, so just use a zero
831 // index for this level and proceed to the next level to see if it can
832 // accommodate the offset.
833 NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0));
835 // The element size is non-zero divide the offset by the element
836 // size (rounding down), to compute the index at this level.
837 APInt NewIdx = Offset.udiv(ElemSize);
838 Offset -= NewIdx * ElemSize;
839 NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
841 Ty = ATy->getElementType();
842 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
843 // If we end up with an offset that isn't valid for this struct type, we
844 // can't re-form this GEP in a regular form, so bail out. The pointer
845 // operand likely went through casts that are necessary to make the GEP
847 const StructLayout &SL = *TD->getStructLayout(STy);
848 if (Offset.uge(SL.getSizeInBytes()))
851 // Determine which field of the struct the offset points into. The
852 // getZExtValue is fine as we've already ensured that the offset is
853 // within the range representable by the StructLayout API.
854 unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
855 NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
857 Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
858 Ty = STy->getTypeAtIndex(ElIdx);
860 // We've reached some non-indexable type.
863 } while (Ty != ResultElementTy);
865 // If we haven't used up the entire offset by descending the static
866 // type, then the offset is pointing into the middle of an indivisible
867 // member, so we can't simplify it.
872 Constant *C = ConstantExpr::getGetElementPtr(Ptr, NewIdxs);
873 assert(C->getType()->getPointerElementType() == Ty &&
874 "Computed GetElementPtr has unexpected type!");
876 // If we ended up indexing a member with a type that doesn't match
877 // the type of what the original indices indexed, add a cast.
878 if (Ty != ResultElementTy)
879 C = FoldBitCast(C, ResultTy, *TD);
886 //===----------------------------------------------------------------------===//
887 // Constant Folding public APIs
888 //===----------------------------------------------------------------------===//
890 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
891 /// If successful, the constant result is returned, if not, null is returned.
892 /// Note that this fails if not all of the operands are constant. Otherwise,
893 /// this function can only fail when attempting to fold instructions like loads
894 /// and stores, which have no constant expression form.
895 Constant *llvm::ConstantFoldInstruction(Instruction *I,
896 const DataLayout *TD,
897 const TargetLibraryInfo *TLI) {
898 // Handle PHI nodes quickly here...
899 if (PHINode *PN = dyn_cast<PHINode>(I)) {
900 Constant *CommonValue = nullptr;
902 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
903 Value *Incoming = PN->getIncomingValue(i);
904 // If the incoming value is undef then skip it. Note that while we could
905 // skip the value if it is equal to the phi node itself we choose not to
906 // because that would break the rule that constant folding only applies if
907 // all operands are constants.
908 if (isa<UndefValue>(Incoming))
910 // If the incoming value is not a constant, then give up.
911 Constant *C = dyn_cast<Constant>(Incoming);
914 // Fold the PHI's operands.
915 if (ConstantExpr *NewC = dyn_cast<ConstantExpr>(C))
916 C = ConstantFoldConstantExpression(NewC, TD, TLI);
917 // If the incoming value is a different constant to
918 // the one we saw previously, then give up.
919 if (CommonValue && C != CommonValue)
925 // If we reach here, all incoming values are the same constant or undef.
926 return CommonValue ? CommonValue : UndefValue::get(PN->getType());
929 // Scan the operand list, checking to see if they are all constants, if so,
930 // hand off to ConstantFoldInstOperands.
931 SmallVector<Constant*, 8> Ops;
932 for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
933 Constant *Op = dyn_cast<Constant>(*i);
935 return nullptr; // All operands not constant!
937 // Fold the Instruction's operands.
938 if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(Op))
939 Op = ConstantFoldConstantExpression(NewCE, TD, TLI);
944 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
945 return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
948 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
949 return ConstantFoldLoadInst(LI, TD);
951 if (InsertValueInst *IVI = dyn_cast<InsertValueInst>(I)) {
952 return ConstantExpr::getInsertValue(
953 cast<Constant>(IVI->getAggregateOperand()),
954 cast<Constant>(IVI->getInsertedValueOperand()),
958 if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I)) {
959 return ConstantExpr::getExtractValue(
960 cast<Constant>(EVI->getAggregateOperand()),
964 return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Ops, TD, TLI);
968 ConstantFoldConstantExpressionImpl(const ConstantExpr *CE, const DataLayout *TD,
969 const TargetLibraryInfo *TLI,
970 SmallPtrSetImpl<ConstantExpr *> &FoldedOps) {
971 SmallVector<Constant *, 8> Ops;
972 for (User::const_op_iterator i = CE->op_begin(), e = CE->op_end(); i != e;
974 Constant *NewC = cast<Constant>(*i);
975 // Recursively fold the ConstantExpr's operands. If we have already folded
976 // a ConstantExpr, we don't have to process it again.
977 if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC)) {
978 if (FoldedOps.insert(NewCE))
979 NewC = ConstantFoldConstantExpressionImpl(NewCE, TD, TLI, FoldedOps);
985 return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
987 return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), Ops, TD, TLI);
990 /// ConstantFoldConstantExpression - Attempt to fold the constant expression
991 /// using the specified DataLayout. If successful, the constant result is
992 /// result is returned, if not, null is returned.
993 Constant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE,
994 const DataLayout *TD,
995 const TargetLibraryInfo *TLI) {
996 SmallPtrSet<ConstantExpr *, 4> FoldedOps;
997 return ConstantFoldConstantExpressionImpl(CE, TD, TLI, FoldedOps);
1000 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
1001 /// specified opcode and operands. If successful, the constant result is
1002 /// returned, if not, null is returned. Note that this function can fail when
1003 /// attempting to fold instructions like loads and stores, which have no
1004 /// constant expression form.
1006 /// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/etc
1007 /// information, due to only being passed an opcode and operands. Constant
1008 /// folding using this function strips this information.
1010 Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
1011 ArrayRef<Constant *> Ops,
1012 const DataLayout *TD,
1013 const TargetLibraryInfo *TLI) {
1014 // Handle easy binops first.
1015 if (Instruction::isBinaryOp(Opcode)) {
1016 if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1])) {
1017 if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD))
1021 return ConstantExpr::get(Opcode, Ops[0], Ops[1]);
1025 default: return nullptr;
1026 case Instruction::ICmp:
1027 case Instruction::FCmp: llvm_unreachable("Invalid for compares");
1028 case Instruction::Call:
1029 if (Function *F = dyn_cast<Function>(Ops.back()))
1030 if (canConstantFoldCallTo(F))
1031 return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI);
1033 case Instruction::PtrToInt:
1034 // If the input is a inttoptr, eliminate the pair. This requires knowing
1035 // the width of a pointer, so it can't be done in ConstantExpr::getCast.
1036 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
1037 if (TD && CE->getOpcode() == Instruction::IntToPtr) {
1038 Constant *Input = CE->getOperand(0);
1039 unsigned InWidth = Input->getType()->getScalarSizeInBits();
1040 unsigned PtrWidth = TD->getPointerTypeSizeInBits(CE->getType());
1041 if (PtrWidth < InWidth) {
1043 ConstantInt::get(CE->getContext(),
1044 APInt::getLowBitsSet(InWidth, PtrWidth));
1045 Input = ConstantExpr::getAnd(Input, Mask);
1047 // Do a zext or trunc to get to the dest size.
1048 return ConstantExpr::getIntegerCast(Input, DestTy, false);
1051 return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
1052 case Instruction::IntToPtr:
1053 // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
1054 // the int size is >= the ptr size and the address spaces are the same.
1055 // This requires knowing the width of a pointer, so it can't be done in
1056 // ConstantExpr::getCast.
1057 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
1058 if (TD && CE->getOpcode() == Instruction::PtrToInt) {
1059 Constant *SrcPtr = CE->getOperand(0);
1060 unsigned SrcPtrSize = TD->getPointerTypeSizeInBits(SrcPtr->getType());
1061 unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1063 if (MidIntSize >= SrcPtrSize) {
1064 unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
1065 if (SrcAS == DestTy->getPointerAddressSpace())
1066 return FoldBitCast(CE->getOperand(0), DestTy, *TD);
1071 return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
1072 case Instruction::Trunc:
1073 case Instruction::ZExt:
1074 case Instruction::SExt:
1075 case Instruction::FPTrunc:
1076 case Instruction::FPExt:
1077 case Instruction::UIToFP:
1078 case Instruction::SIToFP:
1079 case Instruction::FPToUI:
1080 case Instruction::FPToSI:
1081 case Instruction::AddrSpaceCast:
1082 return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
1083 case Instruction::BitCast:
1085 return FoldBitCast(Ops[0], DestTy, *TD);
1086 return ConstantExpr::getBitCast(Ops[0], DestTy);
1087 case Instruction::Select:
1088 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
1089 case Instruction::ExtractElement:
1090 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1091 case Instruction::InsertElement:
1092 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1093 case Instruction::ShuffleVector:
1094 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
1095 case Instruction::GetElementPtr:
1096 if (Constant *C = CastGEPIndices(Ops, DestTy, TD, TLI))
1098 if (Constant *C = SymbolicallyEvaluateGEP(Ops, DestTy, TD, TLI))
1101 return ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1));
1105 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
1106 /// instruction (icmp/fcmp) with the specified operands. If it fails, it
1107 /// returns a constant expression of the specified operands.
1109 Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
1110 Constant *Ops0, Constant *Ops1,
1111 const DataLayout *TD,
1112 const TargetLibraryInfo *TLI) {
1113 // fold: icmp (inttoptr x), null -> icmp x, 0
1114 // fold: icmp (ptrtoint x), 0 -> icmp x, null
1115 // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
1116 // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
1118 // ConstantExpr::getCompare cannot do this, because it doesn't have TD
1119 // around to know if bit truncation is happening.
1120 if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
1121 if (TD && Ops1->isNullValue()) {
1122 if (CE0->getOpcode() == Instruction::IntToPtr) {
1123 Type *IntPtrTy = TD->getIntPtrType(CE0->getType());
1124 // Convert the integer value to the right size to ensure we get the
1125 // proper extension or truncation.
1126 Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1128 Constant *Null = Constant::getNullValue(C->getType());
1129 return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
1132 // Only do this transformation if the int is intptrty in size, otherwise
1133 // there is a truncation or extension that we aren't modeling.
1134 if (CE0->getOpcode() == Instruction::PtrToInt) {
1135 Type *IntPtrTy = TD->getIntPtrType(CE0->getOperand(0)->getType());
1136 if (CE0->getType() == IntPtrTy) {
1137 Constant *C = CE0->getOperand(0);
1138 Constant *Null = Constant::getNullValue(C->getType());
1139 return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
1144 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
1145 if (TD && CE0->getOpcode() == CE1->getOpcode()) {
1146 if (CE0->getOpcode() == Instruction::IntToPtr) {
1147 Type *IntPtrTy = TD->getIntPtrType(CE0->getType());
1149 // Convert the integer value to the right size to ensure we get the
1150 // proper extension or truncation.
1151 Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1153 Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
1155 return ConstantFoldCompareInstOperands(Predicate, C0, C1, TD, TLI);
1158 // Only do this transformation if the int is intptrty in size, otherwise
1159 // there is a truncation or extension that we aren't modeling.
1160 if (CE0->getOpcode() == Instruction::PtrToInt) {
1161 Type *IntPtrTy = TD->getIntPtrType(CE0->getOperand(0)->getType());
1162 if (CE0->getType() == IntPtrTy &&
1163 CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1164 return ConstantFoldCompareInstOperands(Predicate,
1174 // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
1175 // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
1176 if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
1177 CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
1179 ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0), Ops1,
1182 ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(1), Ops1,
1185 Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
1186 Constant *Ops[] = { LHS, RHS };
1187 return ConstantFoldInstOperands(OpC, LHS->getType(), Ops, TD, TLI);
1191 return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
1195 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
1196 /// getelementptr constantexpr, return the constant value being addressed by the
1197 /// constant expression, or null if something is funny and we can't decide.
1198 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
1200 if (!CE->getOperand(1)->isNullValue())
1201 return nullptr; // Do not allow stepping over the value!
1203 // Loop over all of the operands, tracking down which value we are
1205 for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) {
1206 C = C->getAggregateElement(CE->getOperand(i));
1213 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
1214 /// indices (with an *implied* zero pointer index that is not in the list),
1215 /// return the constant value being addressed by a virtual load, or null if
1216 /// something is funny and we can't decide.
1217 Constant *llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
1218 ArrayRef<Constant*> Indices) {
1219 // Loop over all of the operands, tracking down which value we are
1221 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1222 C = C->getAggregateElement(Indices[i]);
1230 //===----------------------------------------------------------------------===//
1231 // Constant Folding for Calls
1234 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
1235 /// the specified function.
1236 bool llvm::canConstantFoldCallTo(const Function *F) {
1237 switch (F->getIntrinsicID()) {
1238 case Intrinsic::fabs:
1239 case Intrinsic::log:
1240 case Intrinsic::log2:
1241 case Intrinsic::log10:
1242 case Intrinsic::exp:
1243 case Intrinsic::exp2:
1244 case Intrinsic::floor:
1245 case Intrinsic::ceil:
1246 case Intrinsic::sqrt:
1247 case Intrinsic::pow:
1248 case Intrinsic::powi:
1249 case Intrinsic::bswap:
1250 case Intrinsic::ctpop:
1251 case Intrinsic::ctlz:
1252 case Intrinsic::cttz:
1253 case Intrinsic::fma:
1254 case Intrinsic::fmuladd:
1255 case Intrinsic::copysign:
1256 case Intrinsic::round:
1257 case Intrinsic::sadd_with_overflow:
1258 case Intrinsic::uadd_with_overflow:
1259 case Intrinsic::ssub_with_overflow:
1260 case Intrinsic::usub_with_overflow:
1261 case Intrinsic::smul_with_overflow:
1262 case Intrinsic::umul_with_overflow:
1263 case Intrinsic::convert_from_fp16:
1264 case Intrinsic::convert_to_fp16:
1265 case Intrinsic::x86_sse_cvtss2si:
1266 case Intrinsic::x86_sse_cvtss2si64:
1267 case Intrinsic::x86_sse_cvttss2si:
1268 case Intrinsic::x86_sse_cvttss2si64:
1269 case Intrinsic::x86_sse2_cvtsd2si:
1270 case Intrinsic::x86_sse2_cvtsd2si64:
1271 case Intrinsic::x86_sse2_cvttsd2si:
1272 case Intrinsic::x86_sse2_cvttsd2si64:
1281 StringRef Name = F->getName();
1283 // In these cases, the check of the length is required. We don't want to
1284 // return true for a name like "cos\0blah" which strcmp would return equal to
1285 // "cos", but has length 8.
1287 default: return false;
1289 return Name == "acos" || Name == "asin" || Name == "atan" || Name =="atan2";
1291 return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh";
1293 return Name == "exp" || Name == "exp2";
1295 return Name == "fabs" || Name == "fmod" || Name == "floor";
1297 return Name == "log" || Name == "log10";
1299 return Name == "pow";
1301 return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
1302 Name == "sinf" || Name == "sqrtf";
1304 return Name == "tan" || Name == "tanh";
1308 static Constant *GetConstantFoldFPValue(double V, Type *Ty) {
1309 if (Ty->isHalfTy()) {
1312 APF.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &unused);
1313 return ConstantFP::get(Ty->getContext(), APF);
1315 if (Ty->isFloatTy())
1316 return ConstantFP::get(Ty->getContext(), APFloat((float)V));
1317 if (Ty->isDoubleTy())
1318 return ConstantFP::get(Ty->getContext(), APFloat(V));
1319 llvm_unreachable("Can only constant fold half/float/double");
1324 /// llvm_fenv_clearexcept - Clear the floating-point exception state.
1325 static inline void llvm_fenv_clearexcept() {
1326 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT
1327 feclearexcept(FE_ALL_EXCEPT);
1332 /// llvm_fenv_testexcept - Test if a floating-point exception was raised.
1333 static inline bool llvm_fenv_testexcept() {
1334 int errno_val = errno;
1335 if (errno_val == ERANGE || errno_val == EDOM)
1337 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT
1338 if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
1345 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
1347 llvm_fenv_clearexcept();
1349 if (llvm_fenv_testexcept()) {
1350 llvm_fenv_clearexcept();
1354 return GetConstantFoldFPValue(V, Ty);
1357 static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
1358 double V, double W, Type *Ty) {
1359 llvm_fenv_clearexcept();
1361 if (llvm_fenv_testexcept()) {
1362 llvm_fenv_clearexcept();
1366 return GetConstantFoldFPValue(V, Ty);
1369 /// ConstantFoldConvertToInt - Attempt to an SSE floating point to integer
1370 /// conversion of a constant floating point. If roundTowardZero is false, the
1371 /// default IEEE rounding is used (toward nearest, ties to even). This matches
1372 /// the behavior of the non-truncating SSE instructions in the default rounding
1373 /// mode. The desired integer type Ty is used to select how many bits are
1374 /// available for the result. Returns null if the conversion cannot be
1375 /// performed, otherwise returns the Constant value resulting from the
1377 static Constant *ConstantFoldConvertToInt(const APFloat &Val,
1378 bool roundTowardZero, Type *Ty) {
1379 // All of these conversion intrinsics form an integer of at most 64bits.
1380 unsigned ResultWidth = Ty->getIntegerBitWidth();
1381 assert(ResultWidth <= 64 &&
1382 "Can only constant fold conversions to 64 and 32 bit ints");
1385 bool isExact = false;
1386 APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
1387 : APFloat::rmNearestTiesToEven;
1388 APFloat::opStatus status = Val.convertToInteger(&UIntVal, ResultWidth,
1389 /*isSigned=*/true, mode,
1391 if (status != APFloat::opOK && status != APFloat::opInexact)
1393 return ConstantInt::get(Ty, UIntVal, /*isSigned=*/true);
1396 static double getValueAsDouble(ConstantFP *Op) {
1397 Type *Ty = Op->getType();
1399 if (Ty->isFloatTy())
1400 return Op->getValueAPF().convertToFloat();
1402 if (Ty->isDoubleTy())
1403 return Op->getValueAPF().convertToDouble();
1406 APFloat APF = Op->getValueAPF();
1407 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &unused);
1408 return APF.convertToDouble();
1411 static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID,
1412 Type *Ty, ArrayRef<Constant *> Operands,
1413 const TargetLibraryInfo *TLI) {
1414 if (Operands.size() == 1) {
1415 if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
1416 if (IntrinsicID == Intrinsic::convert_to_fp16) {
1417 APFloat Val(Op->getValueAPF());
1420 Val.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &lost);
1422 return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt());
1425 if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1428 if (IntrinsicID == Intrinsic::round) {
1429 APFloat V = Op->getValueAPF();
1430 V.roundToIntegral(APFloat::rmNearestTiesToAway);
1431 return ConstantFP::get(Ty->getContext(), V);
1434 /// We only fold functions with finite arguments. Folding NaN and inf is
1435 /// likely to be aborted with an exception anyway, and some host libms
1436 /// have known errors raising exceptions.
1437 if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity())
1440 /// Currently APFloat versions of these functions do not exist, so we use
1441 /// the host native double versions. Float versions are not called
1442 /// directly but for all these it is true (float)(f((double)arg)) ==
1443 /// f(arg). Long double not supported yet.
1444 double V = getValueAsDouble(Op);
1446 switch (IntrinsicID) {
1448 case Intrinsic::fabs:
1449 return ConstantFoldFP(fabs, V, Ty);
1451 case Intrinsic::log2:
1452 return ConstantFoldFP(log2, V, Ty);
1455 case Intrinsic::log:
1456 return ConstantFoldFP(log, V, Ty);
1459 case Intrinsic::log10:
1460 return ConstantFoldFP(log10, V, Ty);
1463 case Intrinsic::exp:
1464 return ConstantFoldFP(exp, V, Ty);
1467 case Intrinsic::exp2:
1468 return ConstantFoldFP(exp2, V, Ty);
1470 case Intrinsic::floor:
1471 return ConstantFoldFP(floor, V, Ty);
1472 case Intrinsic::ceil:
1473 return ConstantFoldFP(ceil, V, Ty);
1481 if (Name == "acos" && TLI->has(LibFunc::acos))
1482 return ConstantFoldFP(acos, V, Ty);
1483 else if (Name == "asin" && TLI->has(LibFunc::asin))
1484 return ConstantFoldFP(asin, V, Ty);
1485 else if (Name == "atan" && TLI->has(LibFunc::atan))
1486 return ConstantFoldFP(atan, V, Ty);
1489 if (Name == "ceil" && TLI->has(LibFunc::ceil))
1490 return ConstantFoldFP(ceil, V, Ty);
1491 else if (Name == "cos" && TLI->has(LibFunc::cos))
1492 return ConstantFoldFP(cos, V, Ty);
1493 else if (Name == "cosh" && TLI->has(LibFunc::cosh))
1494 return ConstantFoldFP(cosh, V, Ty);
1495 else if (Name == "cosf" && TLI->has(LibFunc::cosf))
1496 return ConstantFoldFP(cos, V, Ty);
1499 if (Name == "exp" && TLI->has(LibFunc::exp))
1500 return ConstantFoldFP(exp, V, Ty);
1502 if (Name == "exp2" && TLI->has(LibFunc::exp2)) {
1503 // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
1505 return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
1509 if (Name == "fabs" && TLI->has(LibFunc::fabs))
1510 return ConstantFoldFP(fabs, V, Ty);
1511 else if (Name == "floor" && TLI->has(LibFunc::floor))
1512 return ConstantFoldFP(floor, V, Ty);
1515 if (Name == "log" && V > 0 && TLI->has(LibFunc::log))
1516 return ConstantFoldFP(log, V, Ty);
1517 else if (Name == "log10" && V > 0 && TLI->has(LibFunc::log10))
1518 return ConstantFoldFP(log10, V, Ty);
1519 else if (IntrinsicID == Intrinsic::sqrt &&
1520 (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) {
1522 return ConstantFoldFP(sqrt, V, Ty);
1524 return Constant::getNullValue(Ty);
1528 if (Name == "sin" && TLI->has(LibFunc::sin))
1529 return ConstantFoldFP(sin, V, Ty);
1530 else if (Name == "sinh" && TLI->has(LibFunc::sinh))
1531 return ConstantFoldFP(sinh, V, Ty);
1532 else if (Name == "sqrt" && V >= 0 && TLI->has(LibFunc::sqrt))
1533 return ConstantFoldFP(sqrt, V, Ty);
1534 else if (Name == "sqrtf" && V >= 0 && TLI->has(LibFunc::sqrtf))
1535 return ConstantFoldFP(sqrt, V, Ty);
1536 else if (Name == "sinf" && TLI->has(LibFunc::sinf))
1537 return ConstantFoldFP(sin, V, Ty);
1540 if (Name == "tan" && TLI->has(LibFunc::tan))
1541 return ConstantFoldFP(tan, V, Ty);
1542 else if (Name == "tanh" && TLI->has(LibFunc::tanh))
1543 return ConstantFoldFP(tanh, V, Ty);
1551 if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
1552 switch (IntrinsicID) {
1553 case Intrinsic::bswap:
1554 return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
1555 case Intrinsic::ctpop:
1556 return ConstantInt::get(Ty, Op->getValue().countPopulation());
1557 case Intrinsic::convert_from_fp16: {
1558 APFloat Val(APFloat::IEEEhalf, Op->getValue());
1561 APFloat::opStatus status =
1562 Val.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &lost);
1564 // Conversion is always precise.
1566 assert(status == APFloat::opOK && !lost &&
1567 "Precision lost during fp16 constfolding");
1569 return ConstantFP::get(Ty->getContext(), Val);
1576 // Support ConstantVector in case we have an Undef in the top.
1577 if (isa<ConstantVector>(Operands[0]) ||
1578 isa<ConstantDataVector>(Operands[0])) {
1579 Constant *Op = cast<Constant>(Operands[0]);
1580 switch (IntrinsicID) {
1582 case Intrinsic::x86_sse_cvtss2si:
1583 case Intrinsic::x86_sse_cvtss2si64:
1584 case Intrinsic::x86_sse2_cvtsd2si:
1585 case Intrinsic::x86_sse2_cvtsd2si64:
1586 if (ConstantFP *FPOp =
1587 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
1588 return ConstantFoldConvertToInt(FPOp->getValueAPF(),
1589 /*roundTowardZero=*/false, Ty);
1590 case Intrinsic::x86_sse_cvttss2si:
1591 case Intrinsic::x86_sse_cvttss2si64:
1592 case Intrinsic::x86_sse2_cvttsd2si:
1593 case Intrinsic::x86_sse2_cvttsd2si64:
1594 if (ConstantFP *FPOp =
1595 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
1596 return ConstantFoldConvertToInt(FPOp->getValueAPF(),
1597 /*roundTowardZero=*/true, Ty);
1601 if (isa<UndefValue>(Operands[0])) {
1602 if (IntrinsicID == Intrinsic::bswap)
1610 if (Operands.size() == 2) {
1611 if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
1612 if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1614 double Op1V = getValueAsDouble(Op1);
1616 if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
1617 if (Op2->getType() != Op1->getType())
1620 double Op2V = getValueAsDouble(Op2);
1621 if (IntrinsicID == Intrinsic::pow) {
1622 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
1624 if (IntrinsicID == Intrinsic::copysign) {
1625 APFloat V1 = Op1->getValueAPF();
1626 APFloat V2 = Op2->getValueAPF();
1628 return ConstantFP::get(Ty->getContext(), V1);
1632 if (Name == "pow" && TLI->has(LibFunc::pow))
1633 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
1634 if (Name == "fmod" && TLI->has(LibFunc::fmod))
1635 return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
1636 if (Name == "atan2" && TLI->has(LibFunc::atan2))
1637 return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
1638 } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
1639 if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
1640 return ConstantFP::get(Ty->getContext(),
1641 APFloat((float)std::pow((float)Op1V,
1642 (int)Op2C->getZExtValue())));
1643 if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy())
1644 return ConstantFP::get(Ty->getContext(),
1645 APFloat((float)std::pow((float)Op1V,
1646 (int)Op2C->getZExtValue())));
1647 if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy())
1648 return ConstantFP::get(Ty->getContext(),
1649 APFloat((double)std::pow((double)Op1V,
1650 (int)Op2C->getZExtValue())));
1655 if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
1656 if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
1657 switch (IntrinsicID) {
1659 case Intrinsic::sadd_with_overflow:
1660 case Intrinsic::uadd_with_overflow:
1661 case Intrinsic::ssub_with_overflow:
1662 case Intrinsic::usub_with_overflow:
1663 case Intrinsic::smul_with_overflow:
1664 case Intrinsic::umul_with_overflow: {
1667 switch (IntrinsicID) {
1668 default: llvm_unreachable("Invalid case");
1669 case Intrinsic::sadd_with_overflow:
1670 Res = Op1->getValue().sadd_ov(Op2->getValue(), Overflow);
1672 case Intrinsic::uadd_with_overflow:
1673 Res = Op1->getValue().uadd_ov(Op2->getValue(), Overflow);
1675 case Intrinsic::ssub_with_overflow:
1676 Res = Op1->getValue().ssub_ov(Op2->getValue(), Overflow);
1678 case Intrinsic::usub_with_overflow:
1679 Res = Op1->getValue().usub_ov(Op2->getValue(), Overflow);
1681 case Intrinsic::smul_with_overflow:
1682 Res = Op1->getValue().smul_ov(Op2->getValue(), Overflow);
1684 case Intrinsic::umul_with_overflow:
1685 Res = Op1->getValue().umul_ov(Op2->getValue(), Overflow);
1689 ConstantInt::get(Ty->getContext(), Res),
1690 ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow)
1692 return ConstantStruct::get(cast<StructType>(Ty), Ops);
1694 case Intrinsic::cttz:
1695 if (Op2->isOne() && Op1->isZero()) // cttz(0, 1) is undef.
1696 return UndefValue::get(Ty);
1697 return ConstantInt::get(Ty, Op1->getValue().countTrailingZeros());
1698 case Intrinsic::ctlz:
1699 if (Op2->isOne() && Op1->isZero()) // ctlz(0, 1) is undef.
1700 return UndefValue::get(Ty);
1701 return ConstantInt::get(Ty, Op1->getValue().countLeadingZeros());
1710 if (Operands.size() != 3)
1713 if (const ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
1714 if (const ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
1715 if (const ConstantFP *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
1716 switch (IntrinsicID) {
1718 case Intrinsic::fma:
1719 case Intrinsic::fmuladd: {
1720 APFloat V = Op1->getValueAPF();
1721 APFloat::opStatus s = V.fusedMultiplyAdd(Op2->getValueAPF(),
1723 APFloat::rmNearestTiesToEven);
1724 if (s != APFloat::opInvalidOp)
1725 return ConstantFP::get(Ty->getContext(), V);
1737 static Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID,
1739 ArrayRef<Constant *> Operands,
1740 const TargetLibraryInfo *TLI) {
1741 SmallVector<Constant *, 4> Result(VTy->getNumElements());
1742 SmallVector<Constant *, 4> Lane(Operands.size());
1743 Type *Ty = VTy->getElementType();
1745 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
1746 // Gather a column of constants.
1747 for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
1748 Constant *Agg = Operands[J]->getAggregateElement(I);
1755 // Use the regular scalar folding to simplify this column.
1756 Constant *Folded = ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI);
1762 return ConstantVector::get(Result);
1765 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
1766 /// with the specified arguments, returning null if unsuccessful.
1768 llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
1769 const TargetLibraryInfo *TLI) {
1772 StringRef Name = F->getName();
1774 Type *Ty = F->getReturnType();
1776 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1777 return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands, TLI);
1779 return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI);