Index: clang/lib/CodeGen/ItaniumCXXABI.cpp =================================================================== --- clang/lib/CodeGen/ItaniumCXXABI.cpp +++ clang/lib/CodeGen/ItaniumCXXABI.cpp @@ -962,7 +962,9 @@ else dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj); - return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1); + llvm::Constant *res = ConstantFoldInsertValueInstruction(src, dstAdj, 1); + assert(res != nullptr && "Folding must succeed"); + return res; } llvm::Constant * Index: llvm/include/llvm/Analysis/TargetFolder.h =================================================================== --- llvm/include/llvm/Analysis/TargetFolder.h +++ llvm/include/llvm/Analysis/TargetFolder.h @@ -118,7 +118,7 @@ auto *CAgg = dyn_cast(Agg); auto *CVal = dyn_cast(Val); if (CAgg && CVal) - return Fold(ConstantExpr::getInsertValue(CAgg, CVal, IdxList)); + return ConstantFoldInsertValueInstruction(CAgg, CVal, IdxList); return nullptr; } Index: llvm/include/llvm/IR/ConstantFolder.h =================================================================== --- llvm/include/llvm/IR/ConstantFolder.h +++ llvm/include/llvm/IR/ConstantFolder.h @@ -107,7 +107,7 @@ auto *CAgg = dyn_cast(Agg); auto *CVal = dyn_cast(Val); if (CAgg && CVal) - return ConstantExpr::getInsertValue(CAgg, CVal, IdxList); + return ConstantFoldInsertValueInstruction(CAgg, CVal, IdxList); return nullptr; } Index: llvm/lib/Analysis/ConstantFolding.cpp =================================================================== --- llvm/lib/Analysis/ConstantFolding.cpp +++ llvm/lib/Analysis/ConstantFolding.cpp @@ -1195,7 +1195,8 @@ } if (auto *IVI = dyn_cast(I)) - return ConstantExpr::getInsertValue(Ops[0], Ops[1], IVI->getIndices()); + return ConstantFoldInsertValueInstruction(Ops[0], Ops[1], + IVI->getIndices()); if (auto *EVI = dyn_cast(I)) return ConstantFoldExtractValueInstruction(Ops[0], EVI->getIndices()); Index: llvm/lib/Analysis/InlineCost.cpp =================================================================== --- llvm/lib/Analysis/InlineCost.cpp +++ llvm/lib/Analysis/InlineCost.cpp @@ -2089,9 +2089,9 @@ bool CallAnalyzer::visitInsertValue(InsertValueInst &I) { // Constant folding for insert value is trivial. if (simplifyInstruction(I, [&](SmallVectorImpl &COps) { - return ConstantExpr::getInsertValue(/*AggregateOperand*/ COps[0], - /*InsertedValueOperand*/ COps[1], - I.getIndices()); + return ConstantFoldInsertValueInstruction( + /*AggregateOperand*/ COps[0], /*InsertedValueOperand*/ COps[1], + I.getIndices()); })) return true; Index: llvm/lib/Transforms/Utils/Evaluator.cpp =================================================================== --- llvm/lib/Transforms/Utils/Evaluator.cpp +++ llvm/lib/Transforms/Utils/Evaluator.cpp @@ -368,9 +368,11 @@ LLVM_DEBUG(dbgs() << "Found an ExtractValueInst! Simplifying: " << *InstResult << "\n"); } else if (auto *IVI = dyn_cast(CurInst)) { - InstResult = ConstantExpr::getInsertValue( + InstResult = ConstantFoldInsertValueInstruction( getVal(IVI->getAggregateOperand()), getVal(IVI->getInsertedValueOperand()), IVI->getIndices()); + if (!InstResult) + return false; LLVM_DEBUG(dbgs() << "Found an InsertValueInst! Simplifying: " << *InstResult << "\n"); } else if (GetElementPtrInst *GEP = dyn_cast(CurInst)) {