diff options
Diffstat (limited to 'contrib/llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r-- | contrib/llvm/lib/Analysis/ConstantFolding.cpp | 751 |
1 files changed, 423 insertions, 328 deletions
diff --git a/contrib/llvm/lib/Analysis/ConstantFolding.cpp b/contrib/llvm/lib/Analysis/ConstantFolding.cpp index ccb5663..c9adaa7 100644 --- a/contrib/llvm/lib/Analysis/ConstantFolding.cpp +++ b/contrib/llvm/lib/Analysis/ConstantFolding.cpp @@ -17,6 +17,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Analysis/ConstantFolding.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" @@ -34,15 +35,16 @@ #include "llvm/IR/Operator.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" +#include <cassert> #include <cerrno> +#include <cfenv> #include <cmath> - -#ifdef HAVE_FENV_H -#include <fenv.h> -#endif +#include <limits> using namespace llvm; +namespace { + //===----------------------------------------------------------------------===// // Constant Folding internal helper functions //===----------------------------------------------------------------------===// @@ -50,7 +52,7 @@ using namespace llvm; /// Constant fold bitcast, symbolically evaluating it with DataLayout. /// This always returns a non-null constant, but it may be a /// ConstantExpr if unfoldable. -static Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { +Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { // Catch the obvious splat cases. if (C->isNullValue() && !DestTy->isX86_MMXTy()) return Constant::getNullValue(DestTy); @@ -59,8 +61,8 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { return Constant::getAllOnesValue(DestTy); // Handle a vector->integer cast. - if (IntegerType *IT = dyn_cast<IntegerType>(DestTy)) { - VectorType *VTy = dyn_cast<VectorType>(C->getType()); + if (auto *IT = dyn_cast<IntegerType>(DestTy)) { + auto *VTy = dyn_cast<VectorType>(C->getType()); if (!VTy) return ConstantExpr::getBitCast(C, DestTy); @@ -77,27 +79,30 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { C = ConstantExpr::getBitCast(C, SrcIVTy); } - ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C); - if (!CDV) - return ConstantExpr::getBitCast(C, DestTy); - // Now that we know that the input value is a vector of integers, just shift // and insert them into our result. - unsigned BitShift = DL.getTypeAllocSizeInBits(SrcEltTy); + unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy); APInt Result(IT->getBitWidth(), 0); for (unsigned i = 0; i != NumSrcElts; ++i) { - Result <<= BitShift; + Constant *Element; if (DL.isLittleEndian()) - Result |= CDV->getElementAsInteger(NumSrcElts-i-1); + Element = C->getAggregateElement(NumSrcElts-i-1); else - Result |= CDV->getElementAsInteger(i); + Element = C->getAggregateElement(i); + + auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element); + if (!ElementCI) + return ConstantExpr::getBitCast(C, DestTy); + + Result <<= BitShift; + Result |= ElementCI->getValue().zextOrSelf(IT->getBitWidth()); } return ConstantInt::get(IT, Result); } // The code below only handles casts to vectors currently. - VectorType *DestVTy = dyn_cast<VectorType>(DestTy); + auto *DestVTy = dyn_cast<VectorType>(DestTy); if (!DestVTy) return ConstantExpr::getBitCast(C, DestTy); @@ -175,7 +180,7 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { Constant *Elt = Zero; unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1); for (unsigned j = 0; j != Ratio; ++j) { - Constant *Src =dyn_cast<ConstantInt>(C->getAggregateElement(SrcElt++)); + Constant *Src = dyn_cast<ConstantInt>(C->getAggregateElement(SrcElt++)); if (!Src) // Reject constantexpr elements. return ConstantExpr::getBitCast(C, DestTy); @@ -201,7 +206,7 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { // Loop over each source value, expanding into multiple results. for (unsigned i = 0; i != NumSrcElt; ++i) { - Constant *Src = dyn_cast<ConstantInt>(C->getAggregateElement(i)); + auto *Src = dyn_cast<ConstantInt>(C->getAggregateElement(i)); if (!Src) // Reject constantexpr elements. return ConstantExpr::getBitCast(C, DestTy); @@ -230,11 +235,12 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { return ConstantVector::get(Result); } +} // end anonymous namespace /// If this constant is a constant offset from a global, return the global and /// the constant. Because of constantexprs, this function is recursive. -static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, - APInt &Offset, const DataLayout &DL) { +bool llvm::IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, + APInt &Offset, const DataLayout &DL) { // Trivial case, constant is the global. if ((GV = dyn_cast<GlobalValue>(C))) { unsigned BitWidth = DL.getPointerTypeSizeInBits(GV->getType()); @@ -243,7 +249,7 @@ static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, } // Otherwise, if this isn't a constant expr, bail out. - ConstantExpr *CE = dyn_cast<ConstantExpr>(C); + auto *CE = dyn_cast<ConstantExpr>(C); if (!CE) return false; // Look through ptr->int and ptr->ptr casts. @@ -252,7 +258,7 @@ static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL); // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) - GEPOperator *GEP = dyn_cast<GEPOperator>(CE); + auto *GEP = dyn_cast<GEPOperator>(CE); if (!GEP) return false; @@ -271,13 +277,14 @@ static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, return true; } +namespace { + /// Recursive helper to read bits out of global. C is the constant being copied /// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy /// results into and BytesLeft is the number of bytes left in /// the CurPtr buffer. DL is the DataLayout. -static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, - unsigned char *CurPtr, unsigned BytesLeft, - const DataLayout &DL) { +bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr, + unsigned BytesLeft, const DataLayout &DL) { assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) && "Out of range access"); @@ -286,7 +293,7 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) return true; - if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { + if (auto *CI = dyn_cast<ConstantInt>(C)) { if (CI->getBitWidth() > 64 || (CI->getBitWidth() & 7) != 0) return false; @@ -304,7 +311,7 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, return true; } - if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { + if (auto *CFP = dyn_cast<ConstantFP>(C)) { if (CFP->getType()->isDoubleTy()) { C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL); return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL); @@ -320,7 +327,7 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, return false; } - if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { + if (auto *CS = dyn_cast<ConstantStruct>(C)) { const StructLayout *SL = DL.getStructLayout(CS->getType()); unsigned Index = SL->getElementContainingOffset(ByteOffset); uint64_t CurEltOffset = SL->getElementOffset(Index); @@ -364,7 +371,7 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, uint64_t Index = ByteOffset / EltSize; uint64_t Offset = ByteOffset - Index * EltSize; uint64_t NumElts; - if (ArrayType *AT = dyn_cast<ArrayType>(C->getType())) + if (auto *AT = dyn_cast<ArrayType>(C->getType())) NumElts = AT->getNumElements(); else NumElts = C->getType()->getVectorNumElements(); @@ -386,7 +393,7 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, return true; } - if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { + if (auto *CE = dyn_cast<ConstantExpr>(C)) { if (CE->getOpcode() == Instruction::IntToPtr && CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) { return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr, @@ -398,11 +405,10 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, return false; } -static Constant *FoldReinterpretLoadFromConstPtr(Constant *C, - const DataLayout &DL) { - PointerType *PTy = cast<PointerType>(C->getType()); - Type *LoadTy = PTy->getElementType(); - IntegerType *IntType = dyn_cast<IntegerType>(LoadTy); +Constant *FoldReinterpretLoadFromConstPtr(Constant *C, Type *LoadTy, + const DataLayout &DL) { + auto *PTy = cast<PointerType>(C->getType()); + auto *IntType = dyn_cast<IntegerType>(LoadTy); // If this isn't an integer load we can't fold it directly. if (!IntType) { @@ -414,19 +420,19 @@ static Constant *FoldReinterpretLoadFromConstPtr(Constant *C, // an actual new load. Type *MapTy; if (LoadTy->isHalfTy()) - MapTy = Type::getInt16PtrTy(C->getContext(), AS); + MapTy = Type::getInt16Ty(C->getContext()); else if (LoadTy->isFloatTy()) - MapTy = Type::getInt32PtrTy(C->getContext(), AS); + MapTy = Type::getInt32Ty(C->getContext()); else if (LoadTy->isDoubleTy()) - MapTy = Type::getInt64PtrTy(C->getContext(), AS); + MapTy = Type::getInt64Ty(C->getContext()); else if (LoadTy->isVectorTy()) { - MapTy = PointerType::getIntNPtrTy(C->getContext(), - DL.getTypeAllocSizeInBits(LoadTy), AS); + MapTy = PointerType::getIntNTy(C->getContext(), + DL.getTypeAllocSizeInBits(LoadTy)); } else return nullptr; - C = FoldBitCast(C, MapTy, DL); - if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, DL)) + C = FoldBitCast(C, MapTy->getPointerTo(AS), DL); + if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, MapTy, DL)) return FoldBitCast(Res, LoadTy, DL); return nullptr; } @@ -436,28 +442,38 @@ static Constant *FoldReinterpretLoadFromConstPtr(Constant *C, return nullptr; GlobalValue *GVal; - APInt Offset; - if (!IsConstantOffsetFromGlobal(C, GVal, Offset, DL)) + APInt OffsetAI; + if (!IsConstantOffsetFromGlobal(C, GVal, OffsetAI, DL)) return nullptr; - GlobalVariable *GV = dyn_cast<GlobalVariable>(GVal); + auto *GV = dyn_cast<GlobalVariable>(GVal); if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || !GV->getInitializer()->getType()->isSized()) return nullptr; - // If we're loading off the beginning of the global, some bytes may be valid, - // but we don't try to handle this. - if (Offset.isNegative()) - return nullptr; + int64_t Offset = OffsetAI.getSExtValue(); + int64_t InitializerSize = DL.getTypeAllocSize(GV->getInitializer()->getType()); + + // If we're not accessing anything in this constant, the result is undefined. + if (Offset + BytesLoaded <= 0) + return UndefValue::get(IntType); // If we're not accessing anything in this constant, the result is undefined. - if (Offset.getZExtValue() >= - DL.getTypeAllocSize(GV->getInitializer()->getType())) + if (Offset >= InitializerSize) return UndefValue::get(IntType); unsigned char RawBytes[32] = {0}; - if (!ReadDataFromGlobal(GV->getInitializer(), Offset.getZExtValue(), RawBytes, - BytesLoaded, DL)) + unsigned char *CurPtr = RawBytes; + unsigned BytesLeft = BytesLoaded; + + // If we're loading off the beginning of the global, some bytes may be valid. + if (Offset < 0) { + CurPtr += -Offset; + BytesLeft += Offset; + Offset = 0; + } + + if (!ReadDataFromGlobal(GV->getInitializer(), Offset, CurPtr, BytesLeft, DL)) return nullptr; APInt ResultVal = APInt(IntType->getBitWidth(), 0); @@ -478,14 +494,15 @@ static Constant *FoldReinterpretLoadFromConstPtr(Constant *C, return ConstantInt::get(IntType->getContext(), ResultVal); } -static Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE, - const DataLayout &DL) { - auto *DestPtrTy = dyn_cast<PointerType>(CE->getType()); - if (!DestPtrTy) +Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE, Type *DestTy, + const DataLayout &DL) { + auto *SrcPtr = CE->getOperand(0); + auto *SrcPtrTy = dyn_cast<PointerType>(SrcPtr->getType()); + if (!SrcPtrTy) return nullptr; - Type *DestTy = DestPtrTy->getElementType(); + Type *SrcTy = SrcPtrTy->getPointerElementType(); - Constant *C = ConstantFoldLoadFromConstPtr(CE->getOperand(0), DL); + Constant *C = ConstantFoldLoadFromConstPtr(SrcPtr, SrcTy, DL); if (!C) return nullptr; @@ -522,26 +539,26 @@ static Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE, return nullptr; } -/// Return the value that a load from C would produce if it is constant and -/// determinable. If this is not determinable, return null. -Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, +} // end anonymous namespace + +Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL) { // First, try the easy cases: - if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) + if (auto *GV = dyn_cast<GlobalVariable>(C)) if (GV->isConstant() && GV->hasDefinitiveInitializer()) return GV->getInitializer(); if (auto *GA = dyn_cast<GlobalAlias>(C)) - if (GA->getAliasee() && !GA->mayBeOverridden()) - return ConstantFoldLoadFromConstPtr(GA->getAliasee(), DL); + if (GA->getAliasee() && !GA->isInterposable()) + return ConstantFoldLoadFromConstPtr(GA->getAliasee(), Ty, DL); // If the loaded value isn't a constant expr, we can't handle it. - ConstantExpr *CE = dyn_cast<ConstantExpr>(C); + auto *CE = dyn_cast<ConstantExpr>(C); if (!CE) return nullptr; if (CE->getOpcode() == Instruction::GetElementPtr) { - if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) { + if (auto *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) { if (GV->isConstant() && GV->hasDefinitiveInitializer()) { if (Constant *V = ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) @@ -551,15 +568,14 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, } if (CE->getOpcode() == Instruction::BitCast) - if (Constant *LoadedC = ConstantFoldLoadThroughBitcast(CE, DL)) + if (Constant *LoadedC = ConstantFoldLoadThroughBitcast(CE, Ty, DL)) return LoadedC; // Instead of loading constant c string, use corresponding integer value // directly if string length is small enough. StringRef Str; if (getConstantStringInfo(CE, Str) && !Str.empty()) { - unsigned StrLen = Str.size(); - Type *Ty = cast<PointerType>(CE->getType())->getElementType(); + size_t StrLen = Str.size(); unsigned NumBits = Ty->getPrimitiveSizeInBits(); // Replace load with immediate integer if the result is an integer or fp // value. @@ -568,13 +584,13 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, APInt StrVal(NumBits, 0); APInt SingleChar(NumBits, 0); if (DL.isLittleEndian()) { - for (signed i = StrLen-1; i >= 0; i--) { - SingleChar = (uint64_t) Str[i] & UCHAR_MAX; + for (unsigned char C : reverse(Str.bytes())) { + SingleChar = static_cast<uint64_t>(C); StrVal = (StrVal << 8) | SingleChar; } } else { - for (unsigned i = 0; i < StrLen; i++) { - SingleChar = (uint64_t) Str[i] & UCHAR_MAX; + for (unsigned char C : Str.bytes()) { + SingleChar = static_cast<uint64_t>(C); StrVal = (StrVal << 8) | SingleChar; } // Append NULL at the end. @@ -591,27 +607,26 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, // If this load comes from anywhere in a constant global, and if the global // is all undef or zero, we know what it loads. - if (GlobalVariable *GV = - dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, DL))) { + if (auto *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, DL))) { if (GV->isConstant() && GV->hasDefinitiveInitializer()) { - Type *ResTy = cast<PointerType>(C->getType())->getElementType(); if (GV->getInitializer()->isNullValue()) - return Constant::getNullValue(ResTy); + return Constant::getNullValue(Ty); if (isa<UndefValue>(GV->getInitializer())) - return UndefValue::get(ResTy); + return UndefValue::get(Ty); } } // Try hard to fold loads from bitcasted strange and non-type-safe things. - return FoldReinterpretLoadFromConstPtr(CE, DL); + return FoldReinterpretLoadFromConstPtr(CE, Ty, DL); } -static Constant *ConstantFoldLoadInst(const LoadInst *LI, - const DataLayout &DL) { +namespace { + +Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout &DL) { if (LI->isVolatile()) return nullptr; - if (Constant *C = dyn_cast<Constant>(LI->getOperand(0))) - return ConstantFoldLoadFromConstPtr(C, DL); + if (auto *C = dyn_cast<Constant>(LI->getOperand(0))) + return ConstantFoldLoadFromConstPtr(C, LI->getType(), DL); return nullptr; } @@ -620,9 +635,8 @@ static Constant *ConstantFoldLoadInst(const LoadInst *LI, /// Attempt to symbolically evaluate the result of a binary operator merging /// these together. If target data info is available, it is provided as DL, /// otherwise DL is null. -static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, - Constant *Op1, - const DataLayout &DL) { +Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1, + const DataLayout &DL) { // SROA // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl. @@ -674,18 +688,16 @@ static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, /// If array indices are not pointer-sized integers, explicitly cast them so /// that they aren't implicitly casted by the getelementptr. -static Constant *CastGEPIndices(Type *SrcTy, ArrayRef<Constant *> Ops, - Type *ResultTy, const DataLayout &DL, - const TargetLibraryInfo *TLI) { +Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops, + Type *ResultTy, const DataLayout &DL, + const TargetLibraryInfo *TLI) { Type *IntPtrTy = DL.getIntPtrType(ResultTy); bool Any = false; SmallVector<Constant*, 32> NewIdxs; for (unsigned i = 1, e = Ops.size(); i != e; ++i) { if ((i == 1 || - !isa<StructType>(GetElementPtrInst::getIndexedType( - cast<PointerType>(Ops[0]->getType()->getScalarType()) - ->getElementType(), + !isa<StructType>(GetElementPtrInst::getIndexedType(SrcElemTy, Ops.slice(1, i - 1)))) && Ops[i]->getType() != IntPtrTy) { Any = true; @@ -701,8 +713,8 @@ static Constant *CastGEPIndices(Type *SrcTy, ArrayRef<Constant *> Ops, if (!Any) return nullptr; - Constant *C = ConstantExpr::getGetElementPtr(SrcTy, Ops[0], NewIdxs); - if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { + Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ops[0], NewIdxs); + if (auto *CE = dyn_cast<ConstantExpr>(C)) { if (Constant *Folded = ConstantFoldConstantExpression(CE, DL, TLI)) C = Folded; } @@ -711,32 +723,41 @@ static Constant *CastGEPIndices(Type *SrcTy, ArrayRef<Constant *> Ops, } /// Strip the pointer casts, but preserve the address space information. -static Constant* StripPtrCastKeepAS(Constant* Ptr) { +Constant* StripPtrCastKeepAS(Constant* Ptr, Type *&ElemTy) { assert(Ptr->getType()->isPointerTy() && "Not a pointer type"); - PointerType *OldPtrTy = cast<PointerType>(Ptr->getType()); + auto *OldPtrTy = cast<PointerType>(Ptr->getType()); Ptr = Ptr->stripPointerCasts(); - PointerType *NewPtrTy = cast<PointerType>(Ptr->getType()); + auto *NewPtrTy = cast<PointerType>(Ptr->getType()); + + ElemTy = NewPtrTy->getPointerElementType(); // Preserve the address space number of the pointer. if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) { - NewPtrTy = NewPtrTy->getElementType()->getPointerTo( - OldPtrTy->getAddressSpace()); + NewPtrTy = ElemTy->getPointerTo(OldPtrTy->getAddressSpace()); Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy); } return Ptr; } /// If we can symbolically evaluate the GEP constant expression, do so. -static Constant *SymbolicallyEvaluateGEP(Type *SrcTy, ArrayRef<Constant *> Ops, - Type *ResultTy, const DataLayout &DL, - const TargetLibraryInfo *TLI) { +Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP, + ArrayRef<Constant *> Ops, + const DataLayout &DL, + const TargetLibraryInfo *TLI) { + Type *SrcElemTy = GEP->getSourceElementType(); + Type *ResElemTy = GEP->getResultElementType(); + Type *ResTy = GEP->getType(); + if (!SrcElemTy->isSized()) + return nullptr; + + if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy, DL, TLI)) + return C; + Constant *Ptr = Ops[0]; - if (!Ptr->getType()->getPointerElementType()->isSized() || - !Ptr->getType()->isPointerTy()) + if (!Ptr->getType()->isPointerTy()) return nullptr; Type *IntPtrTy = DL.getIntPtrType(Ptr->getType()); - Type *ResultElementTy = ResultTy->getPointerElementType(); // If this is a constant expr gep that is effectively computing an // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12' @@ -745,16 +766,16 @@ static Constant *SymbolicallyEvaluateGEP(Type *SrcTy, ArrayRef<Constant *> Ops, // If this is "gep i8* Ptr, (sub 0, V)", fold this as: // "inttoptr (sub (ptrtoint Ptr), V)" - if (Ops.size() == 2 && ResultElementTy->isIntegerTy(8)) { - ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[1]); + if (Ops.size() == 2 && ResElemTy->isIntegerTy(8)) { + auto *CE = dyn_cast<ConstantExpr>(Ops[1]); assert((!CE || CE->getType() == IntPtrTy) && "CastGEPIndices didn't canonicalize index types!"); if (CE && CE->getOpcode() == Instruction::Sub && CE->getOperand(0)->isNullValue()) { Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType()); Res = ConstantExpr::getSub(Res, CE->getOperand(1)); - Res = ConstantExpr::getIntToPtr(Res, ResultTy); - if (ConstantExpr *ResCE = dyn_cast<ConstantExpr>(Res)) + Res = ConstantExpr::getIntToPtr(Res, ResTy); + if (auto *ResCE = dyn_cast<ConstantExpr>(Res)) Res = ConstantFoldConstantExpression(ResCE, DL, TLI); return Res; } @@ -765,19 +786,19 @@ static Constant *SymbolicallyEvaluateGEP(Type *SrcTy, ArrayRef<Constant *> Ops, unsigned BitWidth = DL.getTypeSizeInBits(IntPtrTy); APInt Offset = APInt(BitWidth, - DL.getIndexedOffset( - Ptr->getType(), + DL.getIndexedOffsetInType( + SrcElemTy, makeArrayRef((Value * const *)Ops.data() + 1, Ops.size() - 1))); - Ptr = StripPtrCastKeepAS(Ptr); + Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy); // If this is a GEP of a GEP, fold it all into a single GEP. - while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) { + while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) { SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end()); // Do not try the incorporate the sub-GEP if some index is not a number. bool AllConstantInt = true; - for (unsigned i = 0, e = NestedOps.size(); i != e; ++i) - if (!isa<ConstantInt>(NestedOps[i])) { + for (Value *NestedOp : NestedOps) + if (!isa<ConstantInt>(NestedOp)) { AllConstantInt = false; break; } @@ -785,23 +806,24 @@ static Constant *SymbolicallyEvaluateGEP(Type *SrcTy, ArrayRef<Constant *> Ops, break; Ptr = cast<Constant>(GEP->getOperand(0)); - Offset += APInt(BitWidth, DL.getIndexedOffset(Ptr->getType(), NestedOps)); - Ptr = StripPtrCastKeepAS(Ptr); + SrcElemTy = GEP->getSourceElementType(); + Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps)); + Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy); } // If the base value for this address is a literal integer value, fold the // getelementptr to the resulting integer value casted to the pointer type. APInt BasePtr(BitWidth, 0); - if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { + if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) { if (CE->getOpcode() == Instruction::IntToPtr) { - if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0))) + if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0))) BasePtr = Base->getValue().zextOrTrunc(BitWidth); } } if (Ptr->isNullValue() || BasePtr != 0) { Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr); - return ConstantExpr::getIntToPtr(C, ResultTy); + return ConstantExpr::getIntToPtr(C, ResTy); } // Otherwise form a regular getelementptr. Recompute the indices so that @@ -813,39 +835,49 @@ static Constant *SymbolicallyEvaluateGEP(Type *SrcTy, ArrayRef<Constant *> Ops, SmallVector<Constant *, 32> NewIdxs; do { - if (SequentialType *ATy = dyn_cast<SequentialType>(Ty)) { - if (ATy->isPointerTy()) { + if (!Ty->isStructTy()) { + if (Ty->isPointerTy()) { // The only pointer indexing we'll do is on the first index of the GEP. if (!NewIdxs.empty()) break; + Ty = SrcElemTy; + // Only handle pointers to sized types, not pointers to functions. - if (!ATy->getElementType()->isSized()) + if (!Ty->isSized()) return nullptr; + } else if (auto *ATy = dyn_cast<SequentialType>(Ty)) { + Ty = ATy->getElementType(); + } else { + // We've reached some non-indexable type. + break; } // Determine which element of the array the offset points into. - APInt ElemSize(BitWidth, DL.getTypeAllocSize(ATy->getElementType())); - if (ElemSize == 0) + APInt ElemSize(BitWidth, DL.getTypeAllocSize(Ty)); + if (ElemSize == 0) { // The element size is 0. This may be [0 x Ty]*, so just use a zero // index for this level and proceed to the next level to see if it can // accommodate the offset. NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0)); - else { + } else { // The element size is non-zero divide the offset by the element // size (rounding down), to compute the index at this level. - APInt NewIdx = Offset.udiv(ElemSize); + bool Overflow; + APInt NewIdx = Offset.sdiv_ov(ElemSize, Overflow); + if (Overflow) + break; Offset -= NewIdx * ElemSize; NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx)); } - Ty = ATy->getElementType(); - } else if (StructType *STy = dyn_cast<StructType>(Ty)) { + } else { + auto *STy = cast<StructType>(Ty); // If we end up with an offset that isn't valid for this struct type, we // can't re-form this GEP in a regular form, so bail out. The pointer // operand likely went through casts that are necessary to make the GEP // sensible. const StructLayout &SL = *DL.getStructLayout(STy); - if (Offset.uge(SL.getSizeInBytes())) + if (Offset.isNegative() || Offset.uge(SL.getSizeInBytes())) break; // Determine which field of the struct the offset points into. The @@ -856,11 +888,8 @@ static Constant *SymbolicallyEvaluateGEP(Type *SrcTy, ArrayRef<Constant *> Ops, ElIdx)); Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx)); Ty = STy->getTypeAtIndex(ElIdx); - } else { - // We've reached some non-indexable type. - break; } - } while (Ty != ResultElementTy); + } while (Ty != ResElemTy); // If we haven't used up the entire offset by descending the static // type, then the offset is pointing into the middle of an indivisible @@ -869,33 +898,78 @@ static Constant *SymbolicallyEvaluateGEP(Type *SrcTy, ArrayRef<Constant *> Ops, return nullptr; // Create a GEP. - Constant *C = ConstantExpr::getGetElementPtr(SrcTy, Ptr, NewIdxs); + Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs); assert(C->getType()->getPointerElementType() == Ty && "Computed GetElementPtr has unexpected type!"); // If we ended up indexing a member with a type that doesn't match // the type of what the original indices indexed, add a cast. - if (Ty != ResultElementTy) - C = FoldBitCast(C, ResultTy, DL); + if (Ty != ResElemTy) + C = FoldBitCast(C, ResTy, DL); return C; } +/// Attempt to constant fold an instruction with the +/// specified opcode and operands. If successful, the constant result is +/// returned, if not, null is returned. Note that this function can fail when +/// attempting to fold instructions like loads and stores, which have no +/// constant expression form. +/// +/// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/etc +/// information, due to only being passed an opcode and operands. Constant +/// folding using this function strips this information. +/// +Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, Type *DestTy, + unsigned Opcode, + ArrayRef<Constant *> Ops, + const DataLayout &DL, + const TargetLibraryInfo *TLI) { + // Handle easy binops first. + if (Instruction::isBinaryOp(Opcode)) + return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL); + + if (Instruction::isCast(Opcode)) + return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL); + + if (auto *GEP = dyn_cast<GEPOperator>(InstOrCE)) { + if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI)) + return C; + + return ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), + Ops[0], Ops.slice(1)); + } + + switch (Opcode) { + default: return nullptr; + case Instruction::ICmp: + case Instruction::FCmp: llvm_unreachable("Invalid for compares"); + case Instruction::Call: + if (auto *F = dyn_cast<Function>(Ops.back())) + if (canConstantFoldCallTo(F)) + return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI); + return nullptr; + case Instruction::Select: + return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); + case Instruction::ExtractElement: + return ConstantExpr::getExtractElement(Ops[0], Ops[1]); + case Instruction::InsertElement: + return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); + case Instruction::ShuffleVector: + return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); + } +} +} // end anonymous namespace //===----------------------------------------------------------------------===// // Constant Folding public APIs //===----------------------------------------------------------------------===// -/// Try to constant fold the specified instruction. -/// If successful, the constant result is returned, if not, null is returned. -/// Note that this fails if not all of the operands are constant. Otherwise, -/// this function can only fail when attempting to fold instructions like loads -/// and stores, which have no constant expression form. Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI) { // Handle PHI nodes quickly here... - if (PHINode *PN = dyn_cast<PHINode>(I)) { + if (auto *PN = dyn_cast<PHINode>(I)) { Constant *CommonValue = nullptr; for (Value *Incoming : PN->incoming_values()) { @@ -906,11 +980,11 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL, if (isa<UndefValue>(Incoming)) continue; // If the incoming value is not a constant, then give up. - Constant *C = dyn_cast<Constant>(Incoming); + auto *C = dyn_cast<Constant>(Incoming); if (!C) return nullptr; // Fold the PHI's operands. - if (ConstantExpr *NewC = dyn_cast<ConstantExpr>(C)) + if (auto *NewC = dyn_cast<ConstantExpr>(C)) C = ConstantFoldConstantExpression(NewC, DL, TLI); // If the incoming value is a different constant to // the one we saw previously, then give up. @@ -925,54 +999,55 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL, } // Scan the operand list, checking to see if they are all constants, if so, - // hand off to ConstantFoldInstOperands. - SmallVector<Constant*, 8> Ops; - for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) { - Constant *Op = dyn_cast<Constant>(*i); - if (!Op) - return nullptr; // All operands not constant! + // hand off to ConstantFoldInstOperandsImpl. + if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); })) + return nullptr; + SmallVector<Constant *, 8> Ops; + for (const Use &OpU : I->operands()) { + auto *Op = cast<Constant>(&OpU); // Fold the Instruction's operands. - if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(Op)) + if (auto *NewCE = dyn_cast<ConstantExpr>(Op)) Op = ConstantFoldConstantExpression(NewCE, DL, TLI); Ops.push_back(Op); } - if (const CmpInst *CI = dyn_cast<CmpInst>(I)) + if (const auto *CI = dyn_cast<CmpInst>(I)) return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1], DL, TLI); - if (const LoadInst *LI = dyn_cast<LoadInst>(I)) + if (const auto *LI = dyn_cast<LoadInst>(I)) return ConstantFoldLoadInst(LI, DL); - if (InsertValueInst *IVI = dyn_cast<InsertValueInst>(I)) { + if (auto *IVI = dyn_cast<InsertValueInst>(I)) { return ConstantExpr::getInsertValue( cast<Constant>(IVI->getAggregateOperand()), cast<Constant>(IVI->getInsertedValueOperand()), IVI->getIndices()); } - if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I)) { + if (auto *EVI = dyn_cast<ExtractValueInst>(I)) { return ConstantExpr::getExtractValue( cast<Constant>(EVI->getAggregateOperand()), EVI->getIndices()); } - return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Ops, DL, TLI); + return ConstantFoldInstOperands(I, Ops, DL, TLI); } -static Constant * +namespace { + +Constant * ConstantFoldConstantExpressionImpl(const ConstantExpr *CE, const DataLayout &DL, const TargetLibraryInfo *TLI, SmallPtrSetImpl<ConstantExpr *> &FoldedOps) { SmallVector<Constant *, 8> Ops; - for (User::const_op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; - ++i) { - Constant *NewC = cast<Constant>(*i); + for (const Use &NewU : CE->operands()) { + auto *NewC = cast<Constant>(&NewU); // Recursively fold the ConstantExpr's operands. If we have already folded // a ConstantExpr, we don't have to process it again. - if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC)) { + if (auto *NewCE = dyn_cast<ConstantExpr>(NewC)) { if (FoldedOps.insert(NewCE).second) NewC = ConstantFoldConstantExpressionImpl(NewCE, DL, TLI, FoldedOps); } @@ -982,12 +1057,13 @@ ConstantFoldConstantExpressionImpl(const ConstantExpr *CE, const DataLayout &DL, if (CE->isCompare()) return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1], DL, TLI); - return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), Ops, DL, TLI); + + return ConstantFoldInstOperandsImpl(CE, CE->getType(), CE->getOpcode(), Ops, + DL, TLI); } -/// Attempt to fold the constant expression -/// using the specified DataLayout. If successful, the constant result is -/// result is returned, if not, null is returned. +} // end anonymous namespace + Constant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout &DL, const TargetLibraryInfo *TLI) { @@ -995,114 +1071,22 @@ Constant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE, return ConstantFoldConstantExpressionImpl(CE, DL, TLI, FoldedOps); } -/// Attempt to constant fold an instruction with the -/// specified opcode and operands. If successful, the constant result is -/// returned, if not, null is returned. Note that this function can fail when -/// attempting to fold instructions like loads and stores, which have no -/// constant expression form. -/// -/// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/etc -/// information, due to only being passed an opcode and operands. Constant -/// folding using this function strips this information. -/// -Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy, +Constant *llvm::ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops, const DataLayout &DL, const TargetLibraryInfo *TLI) { - // Handle easy binops first. - if (Instruction::isBinaryOp(Opcode)) { - if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1])) { - if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], DL)) - return C; - } - - return ConstantExpr::get(Opcode, Ops[0], Ops[1]); - } - - switch (Opcode) { - default: return nullptr; - case Instruction::ICmp: - case Instruction::FCmp: llvm_unreachable("Invalid for compares"); - case Instruction::Call: - if (Function *F = dyn_cast<Function>(Ops.back())) - if (canConstantFoldCallTo(F)) - return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI); - return nullptr; - case Instruction::PtrToInt: - // If the input is a inttoptr, eliminate the pair. This requires knowing - // the width of a pointer, so it can't be done in ConstantExpr::getCast. - if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { - if (CE->getOpcode() == Instruction::IntToPtr) { - Constant *Input = CE->getOperand(0); - unsigned InWidth = Input->getType()->getScalarSizeInBits(); - unsigned PtrWidth = DL.getPointerTypeSizeInBits(CE->getType()); - if (PtrWidth < InWidth) { - Constant *Mask = - ConstantInt::get(CE->getContext(), - APInt::getLowBitsSet(InWidth, PtrWidth)); - Input = ConstantExpr::getAnd(Input, Mask); - } - // Do a zext or trunc to get to the dest size. - return ConstantExpr::getIntegerCast(Input, DestTy, false); - } - } - return ConstantExpr::getCast(Opcode, Ops[0], DestTy); - case Instruction::IntToPtr: - // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if - // the int size is >= the ptr size and the address spaces are the same. - // This requires knowing the width of a pointer, so it can't be done in - // ConstantExpr::getCast. - if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { - if (CE->getOpcode() == Instruction::PtrToInt) { - Constant *SrcPtr = CE->getOperand(0); - unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType()); - unsigned MidIntSize = CE->getType()->getScalarSizeInBits(); - - if (MidIntSize >= SrcPtrSize) { - unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace(); - if (SrcAS == DestTy->getPointerAddressSpace()) - return FoldBitCast(CE->getOperand(0), DestTy, DL); - } - } - } - - return ConstantExpr::getCast(Opcode, Ops[0], DestTy); - case Instruction::Trunc: - case Instruction::ZExt: - case Instruction::SExt: - case Instruction::FPTrunc: - case Instruction::FPExt: - case Instruction::UIToFP: - case Instruction::SIToFP: - case Instruction::FPToUI: - case Instruction::FPToSI: - case Instruction::AddrSpaceCast: - return ConstantExpr::getCast(Opcode, Ops[0], DestTy); - case Instruction::BitCast: - return FoldBitCast(Ops[0], DestTy, DL); - case Instruction::Select: - return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); - case Instruction::ExtractElement: - return ConstantExpr::getExtractElement(Ops[0], Ops[1]); - case Instruction::InsertElement: - return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); - case Instruction::ShuffleVector: - return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); - case Instruction::GetElementPtr: { - Type *SrcTy = nullptr; - if (Constant *C = CastGEPIndices(SrcTy, Ops, DestTy, DL, TLI)) - return C; - if (Constant *C = SymbolicallyEvaluateGEP(SrcTy, Ops, DestTy, DL, TLI)) - return C; + return ConstantFoldInstOperandsImpl(I, I->getType(), I->getOpcode(), Ops, DL, + TLI); +} - return ConstantExpr::getGetElementPtr(SrcTy, Ops[0], Ops.slice(1)); - } - } +Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy, + ArrayRef<Constant *> Ops, + const DataLayout &DL, + const TargetLibraryInfo *TLI) { + assert(Opcode != Instruction::GetElementPtr && "Invalid for GEPs"); + return ConstantFoldInstOperandsImpl(nullptr, DestTy, Opcode, Ops, DL, TLI); } -/// Attempt to constant fold a compare -/// instruction (icmp/fcmp) with the specified operands. If it fails, it -/// returns a constant expression of the specified operands. Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, Constant *Ops0, Constant *Ops1, const DataLayout &DL, @@ -1115,7 +1099,7 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, // FIXME: The following comment is out of data and the DataLayout is here now. // ConstantExpr::getCompare cannot do this, because it doesn't have DL // around to know if bit truncation is happening. - if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops0)) { + if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) { if (Ops1->isNullValue()) { if (CE0->getOpcode() == Instruction::IntToPtr) { Type *IntPtrTy = DL.getIntPtrType(CE0->getType()); @@ -1139,7 +1123,7 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, } } - if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops1)) { + if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) { if (CE0->getOpcode() == CE1->getOpcode()) { if (CE0->getOpcode() == Instruction::IntToPtr) { Type *IntPtrTy = DL.getIntPtrType(CE0->getType()); @@ -1176,18 +1160,85 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, Predicate, CE0->getOperand(1), Ops1, DL, TLI); unsigned OpC = Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; - Constant *Ops[] = { LHS, RHS }; - return ConstantFoldInstOperands(OpC, LHS->getType(), Ops, DL, TLI); + return ConstantFoldBinaryOpOperands(OpC, LHS, RHS, DL); } } return ConstantExpr::getCompare(Predicate, Ops0, Ops1); } +Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, + Constant *RHS, + const DataLayout &DL) { + assert(Instruction::isBinaryOp(Opcode)); + if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS)) + if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL)) + return C; + + return ConstantExpr::get(Opcode, LHS, RHS); +} + +Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C, + Type *DestTy, const DataLayout &DL) { + assert(Instruction::isCast(Opcode)); + switch (Opcode) { + default: + llvm_unreachable("Missing case"); + case Instruction::PtrToInt: + // If the input is a inttoptr, eliminate the pair. This requires knowing + // the width of a pointer, so it can't be done in ConstantExpr::getCast. + if (auto *CE = dyn_cast<ConstantExpr>(C)) { + if (CE->getOpcode() == Instruction::IntToPtr) { + Constant *Input = CE->getOperand(0); + unsigned InWidth = Input->getType()->getScalarSizeInBits(); + unsigned PtrWidth = DL.getPointerTypeSizeInBits(CE->getType()); + if (PtrWidth < InWidth) { + Constant *Mask = + ConstantInt::get(CE->getContext(), + APInt::getLowBitsSet(InWidth, PtrWidth)); + Input = ConstantExpr::getAnd(Input, Mask); + } + // Do a zext or trunc to get to the dest size. + return ConstantExpr::getIntegerCast(Input, DestTy, false); + } + } + return ConstantExpr::getCast(Opcode, C, DestTy); + case Instruction::IntToPtr: + // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if + // the int size is >= the ptr size and the address spaces are the same. + // This requires knowing the width of a pointer, so it can't be done in + // ConstantExpr::getCast. + if (auto *CE = dyn_cast<ConstantExpr>(C)) { + if (CE->getOpcode() == Instruction::PtrToInt) { + Constant *SrcPtr = CE->getOperand(0); + unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType()); + unsigned MidIntSize = CE->getType()->getScalarSizeInBits(); + + if (MidIntSize >= SrcPtrSize) { + unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace(); + if (SrcAS == DestTy->getPointerAddressSpace()) + return FoldBitCast(CE->getOperand(0), DestTy, DL); + } + } + } + + return ConstantExpr::getCast(Opcode, C, DestTy); + case Instruction::Trunc: + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::FPTrunc: + case Instruction::FPExt: + case Instruction::UIToFP: + case Instruction::SIToFP: + case Instruction::FPToUI: + case Instruction::FPToSI: + case Instruction::AddrSpaceCast: + return ConstantExpr::getCast(Opcode, C, DestTy); + case Instruction::BitCast: + return FoldBitCast(C, DestTy, DL); + } +} -/// Given a constant and a getelementptr constantexpr, return the constant value -/// being addressed by the constant expression, or null if something is funny -/// and we can't decide. Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE) { if (!CE->getOperand(1)->isNullValue()) @@ -1203,27 +1254,23 @@ Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, return C; } -/// Given a constant and getelementptr indices (with an *implied* zero pointer -/// index that is not in the list), return the constant value being addressed by -/// a virtual load, or null if something is funny and we can't decide. -Constant *llvm::ConstantFoldLoadThroughGEPIndices(Constant *C, - ArrayRef<Constant*> Indices) { +Constant * +llvm::ConstantFoldLoadThroughGEPIndices(Constant *C, + ArrayRef<Constant *> Indices) { // Loop over all of the operands, tracking down which value we are // addressing. - for (unsigned i = 0, e = Indices.size(); i != e; ++i) { - C = C->getAggregateElement(Indices[i]); + for (Constant *Index : Indices) { + C = C->getAggregateElement(Index); if (!C) return nullptr; } return C; } - //===----------------------------------------------------------------------===// // Constant Folding for Calls // -/// Return true if it's even possible to fold a call to the specified function. bool llvm::canConstantFoldCallTo(const Function *F) { switch (F->getIntrinsicID()) { case Intrinsic::fabs: @@ -1252,6 +1299,7 @@ bool llvm::canConstantFoldCallTo(const Function *F) { case Intrinsic::fmuladd: case Intrinsic::copysign: case Intrinsic::round: + case Intrinsic::masked_load: case Intrinsic::sadd_with_overflow: case Intrinsic::uadd_with_overflow: case Intrinsic::ssub_with_overflow: @@ -1260,6 +1308,7 @@ bool llvm::canConstantFoldCallTo(const Function *F) { case Intrinsic::umul_with_overflow: case Intrinsic::convert_from_fp16: case Intrinsic::convert_to_fp16: + case Intrinsic::bitreverse: case Intrinsic::x86_sse_cvtss2si: case Intrinsic::x86_sse_cvtss2si64: case Intrinsic::x86_sse_cvttss2si: @@ -1309,7 +1358,9 @@ bool llvm::canConstantFoldCallTo(const Function *F) { } } -static Constant *GetConstantFoldFPValue(double V, Type *Ty) { +namespace { + +Constant *GetConstantFoldFPValue(double V, Type *Ty) { if (Ty->isHalfTy()) { APFloat APF(V); bool unused; @@ -1321,12 +1372,10 @@ static Constant *GetConstantFoldFPValue(double V, Type *Ty) { if (Ty->isDoubleTy()) return ConstantFP::get(Ty->getContext(), APFloat(V)); llvm_unreachable("Can only constant fold half/float/double"); - } -namespace { /// Clear the floating-point exception state. -static inline void llvm_fenv_clearexcept() { +inline void llvm_fenv_clearexcept() { #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT feclearexcept(FE_ALL_EXCEPT); #endif @@ -1334,7 +1383,7 @@ static inline void llvm_fenv_clearexcept() { } /// Test if a floating-point exception was raised. -static inline bool llvm_fenv_testexcept() { +inline bool llvm_fenv_testexcept() { int errno_val = errno; if (errno_val == ERANGE || errno_val == EDOM) return true; @@ -1344,10 +1393,8 @@ static inline bool llvm_fenv_testexcept() { #endif return false; } -} // End namespace -static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, - Type *Ty) { +Constant *ConstantFoldFP(double (*NativeFP)(double), double V, Type *Ty) { llvm_fenv_clearexcept(); V = NativeFP(V); if (llvm_fenv_testexcept()) { @@ -1358,8 +1405,8 @@ static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, return GetConstantFoldFPValue(V, Ty); } -static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), - double V, double W, Type *Ty) { +Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), double V, + double W, Type *Ty) { llvm_fenv_clearexcept(); V = NativeFP(V, W); if (llvm_fenv_testexcept()) { @@ -1377,8 +1424,8 @@ static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), /// integer type Ty is used to select how many bits are available for the /// result. Returns null if the conversion cannot be performed, otherwise /// returns the Constant value resulting from the conversion. -static Constant *ConstantFoldConvertToInt(const APFloat &Val, - bool roundTowardZero, Type *Ty) { +Constant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero, + Type *Ty) { // All of these conversion intrinsics form an integer of at most 64bits. unsigned ResultWidth = Ty->getIntegerBitWidth(); assert(ResultWidth <= 64 && @@ -1391,12 +1438,13 @@ static Constant *ConstantFoldConvertToInt(const APFloat &Val, APFloat::opStatus status = Val.convertToInteger(&UIntVal, ResultWidth, /*isSigned=*/true, mode, &isExact); - if (status != APFloat::opOK && status != APFloat::opInexact) + if (status != APFloat::opOK && + (!roundTowardZero || status != APFloat::opInexact)) return nullptr; return ConstantInt::get(Ty, UIntVal, /*isSigned=*/true); } -static double getValueAsDouble(ConstantFP *Op) { +double getValueAsDouble(ConstantFP *Op) { Type *Ty = Op->getType(); if (Ty->isFloatTy()) @@ -1411,11 +1459,16 @@ static double getValueAsDouble(ConstantFP *Op) { return APF.convertToDouble(); } -static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, - Type *Ty, ArrayRef<Constant *> Operands, - const TargetLibraryInfo *TLI) { +Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, Type *Ty, + ArrayRef<Constant *> Operands, + const TargetLibraryInfo *TLI) { if (Operands.size() == 1) { - if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) { + if (isa<UndefValue>(Operands[0])) { + // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN + if (IntrinsicID == Intrinsic::cos) + return Constant::getNullValue(Ty); + } + if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) { if (IntrinsicID == Intrinsic::convert_to_fp16) { APFloat Val(Op->getValueAPF()); @@ -1586,12 +1639,14 @@ static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, return nullptr; } - if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) { + if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) { switch (IntrinsicID) { case Intrinsic::bswap: return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap()); case Intrinsic::ctpop: return ConstantInt::get(Ty, Op->getValue().countPopulation()); + case Intrinsic::bitreverse: + return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits()); case Intrinsic::convert_from_fp16: { APFloat Val(APFloat::IEEEhalf, Op->getValue()); @@ -1614,7 +1669,7 @@ static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, // Support ConstantVector in case we have an Undef in the top. if (isa<ConstantVector>(Operands[0]) || isa<ConstantDataVector>(Operands[0])) { - Constant *Op = cast<Constant>(Operands[0]); + auto *Op = cast<Constant>(Operands[0]); switch (IntrinsicID) { default: break; case Intrinsic::x86_sse_cvtss2si: @@ -1622,17 +1677,17 @@ static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, case Intrinsic::x86_sse2_cvtsd2si: case Intrinsic::x86_sse2_cvtsd2si64: if (ConstantFP *FPOp = - dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) - return ConstantFoldConvertToInt(FPOp->getValueAPF(), - /*roundTowardZero=*/false, Ty); + dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) + return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), + /*roundTowardZero=*/false, Ty); case Intrinsic::x86_sse_cvttss2si: case Intrinsic::x86_sse_cvttss2si64: case Intrinsic::x86_sse2_cvttsd2si: case Intrinsic::x86_sse2_cvttsd2si64: if (ConstantFP *FPOp = - dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) - return ConstantFoldConvertToInt(FPOp->getValueAPF(), - /*roundTowardZero=*/true, Ty); + dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) + return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), + /*roundTowardZero=*/true, Ty); } } @@ -1646,12 +1701,12 @@ static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, } if (Operands.size() == 2) { - if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) { + if (auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) { if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy()) return nullptr; double Op1V = getValueAsDouble(Op1); - if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) { + if (auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) { if (Op2->getType() != Op1->getType()) return nullptr; @@ -1661,7 +1716,7 @@ static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, } if (IntrinsicID == Intrinsic::copysign) { APFloat V1 = Op1->getValueAPF(); - APFloat V2 = Op2->getValueAPF(); + const APFloat &V2 = Op2->getValueAPF(); V1.copySign(V2); return ConstantFP::get(Ty->getContext(), V1); } @@ -1689,7 +1744,7 @@ static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, if ((Name == "atan2" && TLI->has(LibFunc::atan2)) || (Name == "atan2f" && TLI->has(LibFunc::atan2f))) return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty); - } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) { + } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) { if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy()) return ConstantFP::get(Ty->getContext(), APFloat((float)std::pow((float)Op1V, @@ -1706,8 +1761,8 @@ static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, return nullptr; } - if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) { - if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) { + if (auto *Op1 = dyn_cast<ConstantInt>(Operands[0])) { + if (auto *Op2 = dyn_cast<ConstantInt>(Operands[1])) { switch (IntrinsicID) { default: break; case Intrinsic::sadd_with_overflow: @@ -1764,9 +1819,9 @@ static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, if (Operands.size() != 3) return nullptr; - if (const ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) { - if (const ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) { - if (const ConstantFP *Op3 = dyn_cast<ConstantFP>(Operands[2])) { + if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) { + if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) { + if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) { switch (IntrinsicID) { default: break; case Intrinsic::fma: @@ -1788,14 +1843,53 @@ static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, return nullptr; } -static Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID, - VectorType *VTy, - ArrayRef<Constant *> Operands, - const TargetLibraryInfo *TLI) { +Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID, + VectorType *VTy, ArrayRef<Constant *> Operands, + const DataLayout &DL, + const TargetLibraryInfo *TLI) { SmallVector<Constant *, 4> Result(VTy->getNumElements()); SmallVector<Constant *, 4> Lane(Operands.size()); Type *Ty = VTy->getElementType(); + if (IntrinsicID == Intrinsic::masked_load) { + auto *SrcPtr = Operands[0]; + auto *Mask = Operands[2]; + auto *Passthru = Operands[3]; + + Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, VTy, DL); + + SmallVector<Constant *, 32> NewElements; + for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { + auto *MaskElt = Mask->getAggregateElement(I); + if (!MaskElt) + break; + auto *PassthruElt = Passthru->getAggregateElement(I); + auto *VecElt = VecData ? VecData->getAggregateElement(I) : nullptr; + if (isa<UndefValue>(MaskElt)) { + if (PassthruElt) + NewElements.push_back(PassthruElt); + else if (VecElt) + NewElements.push_back(VecElt); + else + return nullptr; + } + if (MaskElt->isNullValue()) { + if (!PassthruElt) + return nullptr; + NewElements.push_back(PassthruElt); + } else if (MaskElt->isOneValue()) { + if (!VecElt) + return nullptr; + NewElements.push_back(VecElt); + } else { + return nullptr; + } + } + if (NewElements.size() != VTy->getNumElements()) + return nullptr; + return ConstantVector::get(NewElements); + } + for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { // Gather a column of constants. for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) { @@ -1816,8 +1910,8 @@ static Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID, return ConstantVector::get(Result); } -/// Attempt to constant fold a call to the specified function -/// with the specified arguments, returning null if unsuccessful. +} // end anonymous namespace + Constant * llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands, const TargetLibraryInfo *TLI) { @@ -1827,8 +1921,9 @@ llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands, Type *Ty = F->getReturnType(); - if (VectorType *VTy = dyn_cast<VectorType>(Ty)) - return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands, TLI); + if (auto *VTy = dyn_cast<VectorType>(Ty)) + return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands, + F->getParent()->getDataLayout(), TLI); return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI); } |