diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp --- a/clang/lib/CodeGen/CGCUDANV.cpp +++ b/clang/lib/CodeGen/CGCUDANV.cpp @@ -472,7 +472,7 @@ // variable with instructions. for (auto &&Op : WorkItem) { auto *CE = cast(Op); - auto *NewInst = llvm::createReplacementInstr(CE, I); + auto *NewInst = CE->getAsInstruction(I); NewInst->replaceUsesOfWith(OldV, NewV); OldV = CE; NewV = NewInst; diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h --- a/llvm/include/llvm/IR/Constants.h +++ b/llvm/include/llvm/IR/Constants.h @@ -1308,13 +1308,14 @@ Type *SrcTy = nullptr) const; /// Returns an Instruction which implements the same operation as this - /// ConstantExpr. The instruction is not linked to any basic block. + /// ConstantExpr. If \p InsertBefore is not null, the new instruction is + /// inserted before it, otherwise it is not inserted into any basic block. /// /// A better approach to this could be to have a constructor for Instruction /// which would take a ConstantExpr parameter, but that would have spread /// implementation details of ConstantExpr outside of Constants.cpp, which /// would make it harder to remove ConstantExprs altogether. - Instruction *getAsInstruction() const; + Instruction *getAsInstruction(Instruction *InsertBefore = nullptr) const; /// Methods for support type inquiry through isa, cast, and dyn_cast: static bool classof(const Value *V) { diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -3492,7 +3492,7 @@ NewOps, this, From, To, NumUpdated, OperandNo); } -Instruction *ConstantExpr::getAsInstruction() const { +Instruction *ConstantExpr::getAsInstruction(Instruction *InsertBefore) const { SmallVector ValueOperands(operands()); ArrayRef Ops(ValueOperands); @@ -3510,40 +3510,43 @@ case Instruction::IntToPtr: case Instruction::BitCast: case Instruction::AddrSpaceCast: - return CastInst::Create((Instruction::CastOps)getOpcode(), - Ops[0], getType()); + return CastInst::Create((Instruction::CastOps)getOpcode(), Ops[0], + getType(), "", InsertBefore); case Instruction::Select: - return SelectInst::Create(Ops[0], Ops[1], Ops[2]); + return SelectInst::Create(Ops[0], Ops[1], Ops[2], "", InsertBefore); case Instruction::InsertElement: - return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]); + return InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "", InsertBefore); case Instruction::ExtractElement: - return ExtractElementInst::Create(Ops[0], Ops[1]); + return ExtractElementInst::Create(Ops[0], Ops[1], "", InsertBefore); case Instruction::InsertValue: - return InsertValueInst::Create(Ops[0], Ops[1], getIndices()); + return InsertValueInst::Create(Ops[0], Ops[1], getIndices(), "", + InsertBefore); case Instruction::ExtractValue: - return ExtractValueInst::Create(Ops[0], getIndices()); + return ExtractValueInst::Create(Ops[0], getIndices(), "", InsertBefore); case Instruction::ShuffleVector: - return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask()); + return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "", + InsertBefore); case Instruction::GetElementPtr: { const auto *GO = cast(this); if (GO->isInBounds()) - return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(), - Ops[0], Ops.slice(1)); + return GetElementPtrInst::CreateInBounds( + GO->getSourceElementType(), Ops[0], Ops.slice(1), "", InsertBefore); return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0], - Ops.slice(1)); + Ops.slice(1), "", InsertBefore); } case Instruction::ICmp: case Instruction::FCmp: return CmpInst::Create((Instruction::OtherOps)getOpcode(), - (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]); + (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1], + "", InsertBefore); case Instruction::FNeg: - return UnaryOperator::Create((Instruction::UnaryOps)getOpcode(), Ops[0]); + return UnaryOperator::Create((Instruction::UnaryOps)getOpcode(), Ops[0], "", + InsertBefore); default: assert(getNumOperands() == 2 && "Must be binary operator?"); - BinaryOperator *BO = - BinaryOperator::Create((Instruction::BinaryOps)getOpcode(), - Ops[0], Ops[1]); + BinaryOperator *BO = BinaryOperator::Create( + (Instruction::BinaryOps)getOpcode(), Ops[0], Ops[1], "", InsertBefore); if (isa(BO)) { BO->setHasNoUnsignedWrap(SubclassOptionalData & OverflowingBinaryOperator::NoUnsignedWrap); diff --git a/llvm/lib/IR/ReplaceConstant.cpp b/llvm/lib/IR/ReplaceConstant.cpp --- a/llvm/lib/IR/ReplaceConstant.cpp +++ b/llvm/lib/IR/ReplaceConstant.cpp @@ -20,9 +20,7 @@ // Replace a constant expression by instructions with equivalent operations at // a specified location. Instruction *createReplacementInstr(ConstantExpr *CE, Instruction *Instr) { - auto *CEInstr = CE->getAsInstruction(); - CEInstr->insertBefore(Instr); - return CEInstr; + return CE->getAsInstruction(Instr); } void convertConstantExprsToInstructions(Instruction *I, ConstantExpr *CE, @@ -63,8 +61,7 @@ for (auto *CE : Path) { if (!Visited.insert(CE).second) continue; - auto *NI = CE->getAsInstruction(); - NI->insertBefore(BI); + auto *NI = CE->getAsInstruction(BI); II->replaceUsesOfWith(CE, NI); CE->removeDeadConstantUsers(); BI = II = NI; diff --git a/llvm/lib/Target/XCore/XCoreLowerThreadLocal.cpp b/llvm/lib/Target/XCore/XCoreLowerThreadLocal.cpp --- a/llvm/lib/Target/XCore/XCoreLowerThreadLocal.cpp +++ b/llvm/lib/Target/XCore/XCoreLowerThreadLocal.cpp @@ -21,7 +21,6 @@ #include "llvm/IR/IntrinsicsXCore.h" #include "llvm/IR/Module.h" #include "llvm/IR/NoFolder.h" -#include "llvm/IR/ReplaceConstant.h" #include "llvm/IR/ValueHandle.h" #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" @@ -90,11 +89,11 @@ if (PredBB->getTerminator()->getNumSuccessors() > 1) PredBB = SplitEdge(PredBB, PN->getParent()); Instruction *InsertPos = PredBB->getTerminator(); - Instruction *NewInst = createReplacementInstr(CE, InsertPos); + Instruction *NewInst = CE->getAsInstruction(InsertPos); PN->setOperand(I, NewInst); } } else if (Instruction *Instr = dyn_cast(WU)) { - Instruction *NewInst = createReplacementInstr(CE, Instr); + Instruction *NewInst = CE->getAsInstruction(Instr); Instr->replaceUsesOfWith(CE, NewInst); } else { ConstantExpr *CExpr = dyn_cast(WU); @@ -103,7 +102,7 @@ } } } while (CE->hasNUsesOrMore(1)); // We need to check because a recursive - // sibling may have used 'CE' when createReplacementInstr was called. + // sibling may have used 'CE' when getAsInstruction was called. CE->destroyConstant(); return true; } diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -1490,8 +1490,7 @@ append_range(UUsers, U->users()); for (auto *UU : UUsers) { Instruction *UI = cast(UU); - Instruction *NewU = U->getAsInstruction(); - NewU->insertBefore(UI); + Instruction *NewU = U->getAsInstruction(UI); UI->replaceUsesOfWith(U, NewU); } // We've replaced all the uses, so destroy the constant. (destroyConstant diff --git a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp --- a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp +++ b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp @@ -819,10 +819,9 @@ // Aside from constant GEPs, only constant cast expressions are collected. assert(ConstExpr->isCast() && "ConstExpr should be a cast"); - Instruction *ConstExprInst = ConstExpr->getAsInstruction(); + Instruction *ConstExprInst = ConstExpr->getAsInstruction( + findMatInsertPt(ConstUser.Inst, ConstUser.OpndIdx)); ConstExprInst->setOperand(0, Mat); - ConstExprInst->insertBefore(findMatInsertPt(ConstUser.Inst, - ConstUser.OpndIdx)); // Use the same debug location as the instruction we are about to update. ConstExprInst->setDebugLoc(ConstUser.Inst->getDebugLoc());