diff --git a/llvm/lib/Transforms/Scalar/NewGVN.cpp b/llvm/lib/Transforms/Scalar/NewGVN.cpp index bea04706e41fce..876ef3c427a6c7 100644 --- a/llvm/lib/Transforms/Scalar/NewGVN.cpp +++ b/llvm/lib/Transforms/Scalar/NewGVN.cpp @@ -1457,10 +1457,12 @@ NewGVN::performSymbolicLoadCoercion(Type *LoadType, Value *LoadPtr, if (Offset >= 0) { if (auto *C = dyn_cast( lookupOperandLeader(DepSI->getValueOperand()))) { - LLVM_DEBUG(dbgs() << "Coercing load from store " << *DepSI - << " to constant " << *C << "\n"); - return createConstantExpression( - getConstantStoreValueForLoad(C, Offset, LoadType, DL)); + if (Constant *Res = + getConstantStoreValueForLoad(C, Offset, LoadType, DL)) { + LLVM_DEBUG(dbgs() << "Coercing load from store " << *DepSI + << " to constant " << *Res << "\n"); + return createConstantExpression(Res); + } } } } else if (auto *DepLI = dyn_cast(DepInst)) { diff --git a/llvm/lib/Transforms/Utils/VNCoercion.cpp b/llvm/lib/Transforms/Utils/VNCoercion.cpp index 89a2c374c7efc5..c99dc7836610ed 100644 --- a/llvm/lib/Transforms/Utils/VNCoercion.cpp +++ b/llvm/lib/Transforms/Utils/VNCoercion.cpp @@ -400,10 +400,9 @@ int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr, return -1; } -template -static T *getStoreValueForLoadHelper(T *SrcVal, unsigned Offset, Type *LoadTy, - HelperClass &Helper, - const DataLayout &DL) { +static Value *getStoreValueForLoadHelper(Value *SrcVal, unsigned Offset, + Type *LoadTy, IRBuilderBase &Builder, + const DataLayout &DL) { LLVMContext &Ctx = SrcVal->getType()->getContext(); // If two pointers are in the same address space, they have the same size, @@ -421,9 +420,11 @@ static T *getStoreValueForLoadHelper(T *SrcVal, unsigned Offset, Type *LoadTy, // Compute which bits of the stored value are being used by the load. Convert // to an integer type to start with. if (SrcVal->getType()->isPtrOrPtrVectorTy()) - SrcVal = Helper.CreatePtrToInt(SrcVal, DL.getIntPtrType(SrcVal->getType())); + SrcVal = + Builder.CreatePtrToInt(SrcVal, DL.getIntPtrType(SrcVal->getType())); if (!SrcVal->getType()->isIntegerTy()) - SrcVal = Helper.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize * 8)); + SrcVal = + Builder.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize * 8)); // Shift the bits to the least significant depending on endianness. unsigned ShiftAmt; @@ -432,12 +433,12 @@ static T *getStoreValueForLoadHelper(T *SrcVal, unsigned Offset, Type *LoadTy, else ShiftAmt = (StoreSize - LoadSize - Offset) * 8; if (ShiftAmt) - SrcVal = Helper.CreateLShr(SrcVal, - ConstantInt::get(SrcVal->getType(), ShiftAmt)); + SrcVal = Builder.CreateLShr(SrcVal, + ConstantInt::get(SrcVal->getType(), ShiftAmt)); if (LoadSize != StoreSize) - SrcVal = Helper.CreateTruncOrBitCast(SrcVal, - IntegerType::get(Ctx, LoadSize * 8)); + SrcVal = Builder.CreateTruncOrBitCast(SrcVal, + IntegerType::get(Ctx, LoadSize * 8)); return SrcVal; } @@ -455,9 +456,7 @@ Value *getStoreValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy, Constant *getConstantStoreValueForLoad(Constant *SrcVal, unsigned Offset, Type *LoadTy, const DataLayout &DL) { - ConstantFolder F; - SrcVal = getStoreValueForLoadHelper(SrcVal, Offset, LoadTy, F, DL); - return coerceAvailableValueToLoadTypeHelper(SrcVal, LoadTy, F, DL); + return ConstantFoldLoadFromConst(SrcVal, LoadTy, APInt(32, Offset), DL); } /// This function is called when we have a memdep query of a load that ends up