Index: include/llvm/Analysis/ConstantFolding.h =================================================================== --- include/llvm/Analysis/ConstantFolding.h +++ include/llvm/Analysis/ConstantFolding.h @@ -95,7 +95,7 @@ /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would /// produce if it is constant and determinable. If this is not determinable, /// return null. -Constant *ConstantFoldLoadFromConstPtr(Constant *C, const DataLayout &DL); +Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL); /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a /// getelementptr constantexpr, return the constant value being addressed by the Index: lib/Analysis/ConstantFolding.cpp =================================================================== --- lib/Analysis/ConstantFolding.cpp +++ lib/Analysis/ConstantFolding.cpp @@ -399,9 +399,9 @@ } static Constant *FoldReinterpretLoadFromConstPtr(Constant *C, + Type *LoadTy, const DataLayout &DL) { PointerType *PTy = cast(C->getType()); - Type *LoadTy = PTy->getElementType(); IntegerType *IntType = dyn_cast(LoadTy); // If this isn't an integer load we can't fold it directly. @@ -414,19 +414,19 @@ // 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; } @@ -479,13 +479,15 @@ } static Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE, + Type *DestTy, const DataLayout &DL) { - auto *DestPtrTy = dyn_cast(CE->getType()); - if (!DestPtrTy) + auto *SrcPtr = CE->getOperand(0); + auto *SrcPtrTy = dyn_cast(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; @@ -524,7 +526,7 @@ /// 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, +Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL) { // First, try the easy cases: if (GlobalVariable *GV = dyn_cast(C)) @@ -533,7 +535,7 @@ if (auto *GA = dyn_cast(C)) if (GA->getAliasee() && !GA->mayBeOverridden()) - return ConstantFoldLoadFromConstPtr(GA->getAliasee(), DL); + return ConstantFoldLoadFromConstPtr(GA->getAliasee(), Ty, DL); // If the loaded value isn't a constant expr, we can't handle it. ConstantExpr *CE = dyn_cast(C); @@ -551,7 +553,7 @@ } 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 @@ -559,7 +561,6 @@ StringRef Str; if (getConstantStringInfo(CE, Str) && !Str.empty()) { unsigned StrLen = Str.size(); - Type *Ty = cast(CE->getType())->getElementType(); unsigned NumBits = Ty->getPrimitiveSizeInBits(); // Replace load with immediate integer if the result is an integer or fp // value. @@ -594,16 +595,15 @@ if (GlobalVariable *GV = dyn_cast(GetUnderlyingObject(CE, DL))) { if (GV->isConstant() && GV->hasDefinitiveInitializer()) { - Type *ResTy = cast(C->getType())->getElementType(); if (GV->getInitializer()->isNullValue()) - return Constant::getNullValue(ResTy); + return Constant::getNullValue(Ty); if (isa(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, @@ -611,7 +611,7 @@ if (LI->isVolatile()) return nullptr; if (Constant *C = dyn_cast(LI->getOperand(0))) - return ConstantFoldLoadFromConstPtr(C, DL); + return ConstantFoldLoadFromConstPtr(C, LI->getType(), DL); return nullptr; } Index: lib/Analysis/InstructionSimplify.cpp =================================================================== --- lib/Analysis/InstructionSimplify.cpp +++ lib/Analysis/InstructionSimplify.cpp @@ -3259,7 +3259,7 @@ if (LoadInst *LI = dyn_cast(I)) if (!LI->isVolatile()) - return ConstantFoldLoadFromConstPtr(ConstOps[0], Q.DL); + return ConstantFoldLoadFromConstPtr(ConstOps[0], LI->getType(), Q.DL); return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI); } Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp +++ lib/Analysis/ScalarEvolution.cpp @@ -5915,7 +5915,7 @@ Operands[1], DL, TLI); if (LoadInst *LI = dyn_cast(I)) { if (!LI->isVolatile()) - return ConstantFoldLoadFromConstPtr(Operands[0], DL); + return ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL); } return ConstantFoldInstOperands(I, Operands, DL, TLI); } @@ -6303,7 +6303,7 @@ Operands[1], DL, &TLI); else if (const LoadInst *LI = dyn_cast(I)) { if (!LI->isVolatile()) - C = ConstantFoldLoadFromConstPtr(Operands[0], DL); + C = ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL); } else C = ConstantFoldInstOperands(I, Operands, DL, &TLI); if (!C) return V; Index: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -5450,7 +5450,7 @@ PointerType::getUnqual(LoadTy)); if (const Constant *LoadCst = ConstantFoldLoadFromConstPtr( - const_cast(LoadInput), *Builder.DL)) + const_cast(LoadInput), LoadTy, *Builder.DL)) return Builder.getValue(LoadCst); } Index: lib/Transforms/Scalar/GVN.cpp =================================================================== --- lib/Transforms/Scalar/GVN.cpp +++ lib/Transforms/Scalar/GVN.cpp @@ -1117,7 +1117,7 @@ Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src, OffsetCst); Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS)); - if (ConstantFoldLoadFromConstPtr(Src, DL)) + if (ConstantFoldLoadFromConstPtr(Src, LoadTy, DL)) return Offset; return -1; } @@ -1279,7 +1279,7 @@ Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src, OffsetCst); Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS)); - return ConstantFoldLoadFromConstPtr(Src, DL); + return ConstantFoldLoadFromConstPtr(Src, LoadTy, DL); } Index: lib/Transforms/Scalar/SCCP.cpp =================================================================== --- lib/Transforms/Scalar/SCCP.cpp +++ lib/Transforms/Scalar/SCCP.cpp @@ -1094,7 +1094,7 @@ } // Transform load from a constant into a constant if possible. - if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, DL)) { + if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL)) { if (isa(C)) return; return markConstant(IV, &I, C);