Skip to content

Commit 7ef433f

Browse files
authored
[NFC][RemoveDIs] Switch ConstantExpr::getAsInstruction to not insert (#84737)
Because the RemoveDIs work is putting a debug-info bit into BasicBlock::iterator and iterators are needed for insertion, the getAsInstruction method declaration would need to use a fully defined instruction-iterator, which leads to a complicated header-inclusion-order problem. Much simpler to instead just not insert, and make it the callers problem to insert. This is proportionate because there are only four call-sites to getAsInstruction -- it would suck if we did this everywhere. --------- Merged by: Stephen Tozer <[email protected]>
1 parent 4aec143 commit 7ef433f

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

llvm/include/llvm/IR/Constants.h

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

12911291
/// Returns an Instruction which implements the same operation as this
1292-
/// ConstantExpr. If \p InsertBefore is not null, the new instruction is
1293-
/// inserted before it, otherwise it is not inserted into any basic block.
1292+
/// ConstantExpr. It is not inserted into any basic block.
12941293
///
12951294
/// A better approach to this could be to have a constructor for Instruction
12961295
/// which would take a ConstantExpr parameter, but that would have spread
12971296
/// implementation details of ConstantExpr outside of Constants.cpp, which
12981297
/// would make it harder to remove ConstantExprs altogether.
1299-
Instruction *getAsInstruction(Instruction *InsertBefore = nullptr) const;
1298+
Instruction *getAsInstruction() const;
13001299

13011300
/// Whether creating a constant expression for this binary operator is
13021301
/// desirable.

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5073,10 +5073,15 @@ FunctionCallee OpenMPIRBuilder::createDispatchFiniFunction(unsigned IVSize,
50735073

50745074
static void replaceConstatExprUsesInFuncWithInstr(ConstantExpr *ConstExpr,
50755075
Function *Func) {
5076-
for (User *User : make_early_inc_range(ConstExpr->users()))
5077-
if (auto *Instr = dyn_cast<Instruction>(User))
5078-
if (Instr->getFunction() == Func)
5079-
Instr->replaceUsesOfWith(ConstExpr, ConstExpr->getAsInstruction(Instr));
5076+
for (User *User : make_early_inc_range(ConstExpr->users())) {
5077+
if (auto *Instr = dyn_cast<Instruction>(User)) {
5078+
if (Instr->getFunction() == Func) {
5079+
Instruction *ConstInst = ConstExpr->getAsInstruction();
5080+
ConstInst->insertBefore(*Instr->getParent(), Instr->getIterator());
5081+
Instr->replaceUsesOfWith(ConstExpr, ConstInst);
5082+
}
5083+
}
5084+
}
50805085
}
50815086

50825087
static void replaceConstantValueUsesInFuncWithInstr(llvm::Value *Input,

llvm/lib/IR/Constants.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,7 +3303,7 @@ Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
33033303
NewOps, this, From, To, NumUpdated, OperandNo);
33043304
}
33053305

3306-
Instruction *ConstantExpr::getAsInstruction(Instruction *InsertBefore) const {
3306+
Instruction *ConstantExpr::getAsInstruction() const {
33073307
SmallVector<Value *, 4> ValueOperands(operands());
33083308
ArrayRef<Value*> Ops(ValueOperands);
33093309

@@ -3322,32 +3322,31 @@ Instruction *ConstantExpr::getAsInstruction(Instruction *InsertBefore) const {
33223322
case Instruction::BitCast:
33233323
case Instruction::AddrSpaceCast:
33243324
return CastInst::Create((Instruction::CastOps)getOpcode(), Ops[0],
3325-
getType(), "", InsertBefore);
3325+
getType(), "");
33263326
case Instruction::InsertElement:
3327-
return InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "", InsertBefore);
3327+
return InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "");
33283328
case Instruction::ExtractElement:
3329-
return ExtractElementInst::Create(Ops[0], Ops[1], "", InsertBefore);
3329+
return ExtractElementInst::Create(Ops[0], Ops[1], "");
33303330
case Instruction::ShuffleVector:
3331-
return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "",
3332-
InsertBefore);
3331+
return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "");
33333332

33343333
case Instruction::GetElementPtr: {
33353334
const auto *GO = cast<GEPOperator>(this);
33363335
if (GO->isInBounds())
3337-
return GetElementPtrInst::CreateInBounds(
3338-
GO->getSourceElementType(), Ops[0], Ops.slice(1), "", InsertBefore);
3336+
return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
3337+
Ops[0], Ops.slice(1), "");
33393338
return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
3340-
Ops.slice(1), "", InsertBefore);
3339+
Ops.slice(1), "");
33413340
}
33423341
case Instruction::ICmp:
33433342
case Instruction::FCmp:
33443343
return CmpInst::Create((Instruction::OtherOps)getOpcode(),
33453344
(CmpInst::Predicate)getPredicate(), Ops[0], Ops[1],
3346-
"", InsertBefore);
3345+
"");
33473346
default:
33483347
assert(getNumOperands() == 2 && "Must be binary operator?");
33493348
BinaryOperator *BO = BinaryOperator::Create(
3350-
(Instruction::BinaryOps)getOpcode(), Ops[0], Ops[1], "", InsertBefore);
3349+
(Instruction::BinaryOps)getOpcode(), Ops[0], Ops[1], "");
33513350
if (isa<OverflowingBinaryOperator>(BO)) {
33523351
BO->setHasNoUnsignedWrap(SubclassOptionalData &
33533352
OverflowingBinaryOperator::NoUnsignedWrap);

llvm/lib/IR/ReplaceConstant.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ static bool isExpandableUser(User *U) {
2222
return isa<ConstantExpr>(U) || isa<ConstantAggregate>(U);
2323
}
2424

25-
static SmallVector<Instruction *, 4> expandUser(Instruction *InsertPt,
25+
static SmallVector<Instruction *, 4> expandUser(BasicBlock::iterator InsertPt,
2626
Constant *C) {
2727
SmallVector<Instruction *, 4> NewInsts;
2828
if (auto *CE = dyn_cast<ConstantExpr>(C)) {
29-
NewInsts.push_back(CE->getAsInstruction(InsertPt));
29+
Instruction *ConstInst = CE->getAsInstruction();
30+
ConstInst->insertBefore(*InsertPt->getParent(), InsertPt);
31+
NewInsts.push_back(ConstInst);
3032
} else if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
3133
Value *V = PoisonValue::get(C->getType());
3234
for (auto [Idx, Op] : enumerate(C->operands())) {
@@ -80,12 +82,11 @@ bool convertUsersOfConstantsToInstructions(ArrayRef<Constant *> Consts) {
8082
Instruction *I = InstructionWorklist.pop_back_val();
8183
DebugLoc Loc = I->getDebugLoc();
8284
for (Use &U : I->operands()) {
83-
auto *BI = I;
85+
BasicBlock::iterator BI = I->getIterator();
8486
if (auto *Phi = dyn_cast<PHINode>(I)) {
8587
BasicBlock *BB = Phi->getIncomingBlock(U);
86-
BasicBlock::iterator It = BB->getFirstInsertionPt();
87-
assert(It != BB->end() && "Unexpected empty basic block");
88-
BI = &*It;
88+
BI = BB->getFirstInsertionPt();
89+
assert(BI != BB->end() && "Unexpected empty basic block");
8990
}
9091

9192
if (auto *C = dyn_cast<Constant>(U.get())) {

llvm/lib/Target/XCore/XCoreLowerThreadLocal.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,15 @@ static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) {
8888
BasicBlock *PredBB = PN->getIncomingBlock(I);
8989
if (PredBB->getTerminator()->getNumSuccessors() > 1)
9090
PredBB = SplitEdge(PredBB, PN->getParent());
91-
Instruction *InsertPos = PredBB->getTerminator();
92-
Instruction *NewInst = CE->getAsInstruction(InsertPos);
91+
BasicBlock::iterator InsertPos =
92+
PredBB->getTerminator()->getIterator();
93+
Instruction *NewInst = CE->getAsInstruction();
94+
NewInst->insertBefore(*PredBB, InsertPos);
9395
PN->setOperand(I, NewInst);
9496
}
9597
} else if (Instruction *Instr = dyn_cast<Instruction>(WU)) {
96-
Instruction *NewInst = CE->getAsInstruction(Instr);
98+
Instruction *NewInst = CE->getAsInstruction();
99+
NewInst->insertBefore(*Instr->getParent(), Instr->getIterator());
97100
Instr->replaceUsesOfWith(CE, NewInst);
98101
} else {
99102
ConstantExpr *CExpr = dyn_cast<ConstantExpr>(WU);

0 commit comments

Comments
 (0)