Skip to content

Commit 1b75892

Browse files
committed
[IR] Merge createReplacementInstr into ConstantExpr::getAsInstruction
createReplacementInstr was a trivial wrapper around ConstantExpr::getAsInstruction, which also inserted the new instruction into a basic block. Implement this directly in getAsInstruction by adding an InsertBefore parameter and change all callers to use it. NFC. A follow-up patch will remove createReplacementInstr. Differential Revision: https://reviews.llvm.org/D112791
1 parent 21a1d4c commit 1b75892

File tree

7 files changed

+32
-34
lines changed

7 files changed

+32
-34
lines changed

clang/lib/CodeGen/CGCUDANV.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ static void replaceManagedVar(llvm::GlobalVariable *Var,
472472
// variable with instructions.
473473
for (auto &&Op : WorkItem) {
474474
auto *CE = cast<llvm::ConstantExpr>(Op);
475-
auto *NewInst = llvm::createReplacementInstr(CE, I);
475+
auto *NewInst = CE->getAsInstruction(I);
476476
NewInst->replaceUsesOfWith(OldV, NewV);
477477
OldV = CE;
478478
NewV = NewInst;

llvm/include/llvm/IR/Constants.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,13 +1308,14 @@ class ConstantExpr : public Constant {
13081308
Type *SrcTy = nullptr) const;
13091309

13101310
/// Returns an Instruction which implements the same operation as this
1311-
/// ConstantExpr. The instruction is not linked to any basic block.
1311+
/// ConstantExpr. If \p InsertBefore is not null, the new instruction is
1312+
/// inserted before it, otherwise it is not inserted into any basic block.
13121313
///
13131314
/// A better approach to this could be to have a constructor for Instruction
13141315
/// which would take a ConstantExpr parameter, but that would have spread
13151316
/// implementation details of ConstantExpr outside of Constants.cpp, which
13161317
/// would make it harder to remove ConstantExprs altogether.
1317-
Instruction *getAsInstruction() const;
1318+
Instruction *getAsInstruction(Instruction *InsertBefore = nullptr) const;
13181319

13191320
/// Methods for support type inquiry through isa, cast, and dyn_cast:
13201321
static bool classof(const Value *V) {

llvm/lib/IR/Constants.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,7 +3492,7 @@ Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
34923492
NewOps, this, From, To, NumUpdated, OperandNo);
34933493
}
34943494

3495-
Instruction *ConstantExpr::getAsInstruction() const {
3495+
Instruction *ConstantExpr::getAsInstruction(Instruction *InsertBefore) const {
34963496
SmallVector<Value *, 4> ValueOperands(operands());
34973497
ArrayRef<Value*> Ops(ValueOperands);
34983498

@@ -3510,40 +3510,43 @@ Instruction *ConstantExpr::getAsInstruction() const {
35103510
case Instruction::IntToPtr:
35113511
case Instruction::BitCast:
35123512
case Instruction::AddrSpaceCast:
3513-
return CastInst::Create((Instruction::CastOps)getOpcode(),
3514-
Ops[0], getType());
3513+
return CastInst::Create((Instruction::CastOps)getOpcode(), Ops[0],
3514+
getType(), "", InsertBefore);
35153515
case Instruction::Select:
3516-
return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
3516+
return SelectInst::Create(Ops[0], Ops[1], Ops[2], "", InsertBefore);
35173517
case Instruction::InsertElement:
3518-
return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
3518+
return InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "", InsertBefore);
35193519
case Instruction::ExtractElement:
3520-
return ExtractElementInst::Create(Ops[0], Ops[1]);
3520+
return ExtractElementInst::Create(Ops[0], Ops[1], "", InsertBefore);
35213521
case Instruction::InsertValue:
3522-
return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
3522+
return InsertValueInst::Create(Ops[0], Ops[1], getIndices(), "",
3523+
InsertBefore);
35233524
case Instruction::ExtractValue:
3524-
return ExtractValueInst::Create(Ops[0], getIndices());
3525+
return ExtractValueInst::Create(Ops[0], getIndices(), "", InsertBefore);
35253526
case Instruction::ShuffleVector:
3526-
return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask());
3527+
return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "",
3528+
InsertBefore);
35273529

35283530
case Instruction::GetElementPtr: {
35293531
const auto *GO = cast<GEPOperator>(this);
35303532
if (GO->isInBounds())
3531-
return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
3532-
Ops[0], Ops.slice(1));
3533+
return GetElementPtrInst::CreateInBounds(
3534+
GO->getSourceElementType(), Ops[0], Ops.slice(1), "", InsertBefore);
35333535
return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
3534-
Ops.slice(1));
3536+
Ops.slice(1), "", InsertBefore);
35353537
}
35363538
case Instruction::ICmp:
35373539
case Instruction::FCmp:
35383540
return CmpInst::Create((Instruction::OtherOps)getOpcode(),
3539-
(CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
3541+
(CmpInst::Predicate)getPredicate(), Ops[0], Ops[1],
3542+
"", InsertBefore);
35403543
case Instruction::FNeg:
3541-
return UnaryOperator::Create((Instruction::UnaryOps)getOpcode(), Ops[0]);
3544+
return UnaryOperator::Create((Instruction::UnaryOps)getOpcode(), Ops[0], "",
3545+
InsertBefore);
35423546
default:
35433547
assert(getNumOperands() == 2 && "Must be binary operator?");
3544-
BinaryOperator *BO =
3545-
BinaryOperator::Create((Instruction::BinaryOps)getOpcode(),
3546-
Ops[0], Ops[1]);
3548+
BinaryOperator *BO = BinaryOperator::Create(
3549+
(Instruction::BinaryOps)getOpcode(), Ops[0], Ops[1], "", InsertBefore);
35473550
if (isa<OverflowingBinaryOperator>(BO)) {
35483551
BO->setHasNoUnsignedWrap(SubclassOptionalData &
35493552
OverflowingBinaryOperator::NoUnsignedWrap);

llvm/lib/IR/ReplaceConstant.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ namespace llvm {
2020
// Replace a constant expression by instructions with equivalent operations at
2121
// a specified location.
2222
Instruction *createReplacementInstr(ConstantExpr *CE, Instruction *Instr) {
23-
auto *CEInstr = CE->getAsInstruction();
24-
CEInstr->insertBefore(Instr);
25-
return CEInstr;
23+
return CE->getAsInstruction(Instr);
2624
}
2725

2826
void convertConstantExprsToInstructions(Instruction *I, ConstantExpr *CE,
@@ -63,8 +61,7 @@ void convertConstantExprsToInstructions(
6361
for (auto *CE : Path) {
6462
if (!Visited.insert(CE).second)
6563
continue;
66-
auto *NI = CE->getAsInstruction();
67-
NI->insertBefore(BI);
64+
auto *NI = CE->getAsInstruction(BI);
6865
II->replaceUsesOfWith(CE, NI);
6966
CE->removeDeadConstantUsers();
7067
BI = II = NI;

llvm/lib/Target/XCore/XCoreLowerThreadLocal.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "llvm/IR/IntrinsicsXCore.h"
2222
#include "llvm/IR/Module.h"
2323
#include "llvm/IR/NoFolder.h"
24-
#include "llvm/IR/ReplaceConstant.h"
2524
#include "llvm/IR/ValueHandle.h"
2625
#include "llvm/Pass.h"
2726
#include "llvm/Support/CommandLine.h"
@@ -90,11 +89,11 @@ static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) {
9089
if (PredBB->getTerminator()->getNumSuccessors() > 1)
9190
PredBB = SplitEdge(PredBB, PN->getParent());
9291
Instruction *InsertPos = PredBB->getTerminator();
93-
Instruction *NewInst = createReplacementInstr(CE, InsertPos);
92+
Instruction *NewInst = CE->getAsInstruction(InsertPos);
9493
PN->setOperand(I, NewInst);
9594
}
9695
} else if (Instruction *Instr = dyn_cast<Instruction>(WU)) {
97-
Instruction *NewInst = createReplacementInstr(CE, Instr);
96+
Instruction *NewInst = CE->getAsInstruction(Instr);
9897
Instr->replaceUsesOfWith(CE, NewInst);
9998
} else {
10099
ConstantExpr *CExpr = dyn_cast<ConstantExpr>(WU);
@@ -103,7 +102,7 @@ static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) {
103102
}
104103
}
105104
} while (CE->hasNUsesOrMore(1)); // We need to check because a recursive
106-
// sibling may have used 'CE' when createReplacementInstr was called.
105+
// sibling may have used 'CE' when getAsInstruction was called.
107106
CE->destroyConstant();
108107
return true;
109108
}

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,8 +1490,7 @@ static void makeAllConstantUsesInstructions(Constant *C) {
14901490
append_range(UUsers, U->users());
14911491
for (auto *UU : UUsers) {
14921492
Instruction *UI = cast<Instruction>(UU);
1493-
Instruction *NewU = U->getAsInstruction();
1494-
NewU->insertBefore(UI);
1493+
Instruction *NewU = U->getAsInstruction(UI);
14951494
UI->replaceUsesOfWith(U, NewU);
14961495
}
14971496
// We've replaced all the uses, so destroy the constant. (destroyConstant

llvm/lib/Transforms/Scalar/ConstantHoisting.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,9 @@ void ConstantHoistingPass::emitBaseConstants(Instruction *Base,
819819

820820
// Aside from constant GEPs, only constant cast expressions are collected.
821821
assert(ConstExpr->isCast() && "ConstExpr should be a cast");
822-
Instruction *ConstExprInst = ConstExpr->getAsInstruction();
822+
Instruction *ConstExprInst = ConstExpr->getAsInstruction(
823+
findMatInsertPt(ConstUser.Inst, ConstUser.OpndIdx));
823824
ConstExprInst->setOperand(0, Mat);
824-
ConstExprInst->insertBefore(findMatInsertPt(ConstUser.Inst,
825-
ConstUser.OpndIdx));
826825

827826
// Use the same debug location as the instruction we are about to update.
828827
ConstExprInst->setDebugLoc(ConstUser.Inst->getDebugLoc());

0 commit comments

Comments
 (0)