Skip to content

Commit 635db5e

Browse files
authored
[SandboxIR] Implement InsertPosition (#110730)
This patch implements the InsertPosition class that is used to specify where an instruction should be placed. It also switches a couple of create() functions from the old API to the new one that uses InsertPosition.
1 parent ae635d6 commit 635db5e

File tree

3 files changed

+46
-63
lines changed

3 files changed

+46
-63
lines changed

llvm/include/llvm/SandboxIR/Instruction.h

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ namespace llvm::sandboxir {
2121
// Forward declaration for MSVC.
2222
class IntrinsicInst;
2323

24+
class InsertPosition {
25+
BBIterator InsertAt;
26+
27+
public:
28+
InsertPosition(BasicBlock *InsertAtEnd) {
29+
assert(InsertAtEnd != nullptr && "Expected non-null!");
30+
InsertAt = InsertAtEnd->end();
31+
}
32+
InsertPosition(BBIterator InsertAt) : InsertAt(InsertAt) {}
33+
operator BBIterator() { return InsertAt; }
34+
const BBIterator &getIterator() const { return InsertAt; }
35+
Instruction &operator*() { return *InsertAt; }
36+
BasicBlock *getBasicBlock() const { return InsertAt.getNodeParent(); }
37+
};
38+
2439
/// A sandboxir::User with operands, opcode and linked with previous/next
2540
/// instructions in an instruction list.
2641
class Instruction : public User {
@@ -82,6 +97,20 @@ class Instruction : public User {
8297
virtual SmallVector<llvm::Instruction *, 1> getLLVMInstrs() const = 0;
8398
friend class EraseFromParent; // For getLLVMInstrs().
8499

100+
/// Helper function for create(). It sets the builder's insert position
101+
/// according to \p Pos.
102+
static IRBuilder<> &setInsertPos(InsertPosition Pos) {
103+
auto *WhereBB = Pos.getBasicBlock();
104+
auto WhereIt = Pos.getIterator();
105+
auto &Ctx = WhereBB->getContext();
106+
auto &Builder = Ctx.getLLVMIRBuilder();
107+
if (WhereIt != WhereBB->end())
108+
Builder.SetInsertPoint((*Pos).getTopmostLLVMInstruction());
109+
else
110+
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
111+
return Builder;
112+
}
113+
85114
public:
86115
static const char *getOpcodeName(Opcode Opc);
87116
/// This is used by BasicBlock::iterator.
@@ -384,8 +413,8 @@ class FenceInst : public SingleLLVMInstructionImpl<llvm::FenceInst> {
384413
friend Context; // For constructor;
385414

386415
public:
387-
static FenceInst *create(AtomicOrdering Ordering, BBIterator WhereIt,
388-
BasicBlock *WhereBB, Context &Ctx,
416+
static FenceInst *create(AtomicOrdering Ordering, InsertPosition Pos,
417+
Context &Ctx,
389418
SyncScope::ID SSID = SyncScope::System);
390419
/// Returns the ordering constraint of this fence instruction.
391420
AtomicOrdering getOrdering() const {
@@ -411,16 +440,10 @@ class SelectInst : public SingleLLVMInstructionImpl<llvm::SelectInst> {
411440
SelectInst(llvm::SelectInst *CI, Context &Ctx)
412441
: SingleLLVMInstructionImpl(ClassID::Select, Opcode::Select, CI, Ctx) {}
413442
friend Context; // for SelectInst()
414-
static Value *createCommon(Value *Cond, Value *True, Value *False,
415-
const Twine &Name, IRBuilder<> &Builder,
416-
Context &Ctx);
417443

418444
public:
419445
static Value *create(Value *Cond, Value *True, Value *False,
420-
Instruction *InsertBefore, Context &Ctx,
421-
const Twine &Name = "");
422-
static Value *create(Value *Cond, Value *True, Value *False,
423-
BasicBlock *InsertAtEnd, Context &Ctx,
446+
InsertPosition Pos, Context &Ctx,
424447
const Twine &Name = "");
425448

426449
const Value *getCondition() const { return getOperand(0); }
@@ -457,10 +480,7 @@ class InsertElementInst final
457480

458481
public:
459482
static Value *create(Value *Vec, Value *NewElt, Value *Idx,
460-
Instruction *InsertBefore, Context &Ctx,
461-
const Twine &Name = "");
462-
static Value *create(Value *Vec, Value *NewElt, Value *Idx,
463-
BasicBlock *InsertAtEnd, Context &Ctx,
483+
InsertPosition Pos, Context &Ctx,
464484
const Twine &Name = "");
465485
static bool classof(const Value *From) {
466486
return From->getSubclassID() == ClassID::InsertElement;

llvm/lib/SandboxIR/Instruction.cpp

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,9 @@ FreezeInst *FreezeInst::create(Value *V, BBIterator WhereIt,
315315
return Ctx.createFreezeInst(LLVMI);
316316
}
317317

318-
FenceInst *FenceInst::create(AtomicOrdering Ordering, BBIterator WhereIt,
319-
BasicBlock *WhereBB, Context &Ctx,
320-
SyncScope::ID SSID) {
321-
auto &Builder = Ctx.getLLVMIRBuilder();
322-
if (WhereIt != WhereBB->end())
323-
Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
324-
else
325-
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
318+
FenceInst *FenceInst::create(AtomicOrdering Ordering, InsertPosition Pos,
319+
Context &Ctx, SyncScope::ID SSID) {
320+
auto &Builder = Instruction::setInsertPos(Pos);
326321
llvm::FenceInst *LLVMI = Builder.CreateFence(Ordering, SSID);
327322
return Ctx.createFenceInst(LLVMI);
328323
}
@@ -342,9 +337,9 @@ void FenceInst::setSyncScopeID(SyncScope::ID SSID) {
342337
cast<llvm::FenceInst>(Val)->setSyncScopeID(SSID);
343338
}
344339

345-
Value *SelectInst::createCommon(Value *Cond, Value *True, Value *False,
346-
const Twine &Name, IRBuilder<> &Builder,
347-
Context &Ctx) {
340+
Value *SelectInst::create(Value *Cond, Value *True, Value *False,
341+
InsertPosition Pos, Context &Ctx, const Twine &Name) {
342+
auto &Builder = Instruction::setInsertPos(Pos);
348343
llvm::Value *NewV =
349344
Builder.CreateSelect(Cond->Val, True->Val, False->Val, Name);
350345
if (auto *NewSI = dyn_cast<llvm::SelectInst>(NewV))
@@ -353,24 +348,6 @@ Value *SelectInst::createCommon(Value *Cond, Value *True, Value *False,
353348
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
354349
}
355350

356-
Value *SelectInst::create(Value *Cond, Value *True, Value *False,
357-
Instruction *InsertBefore, Context &Ctx,
358-
const Twine &Name) {
359-
llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction();
360-
auto &Builder = Ctx.getLLVMIRBuilder();
361-
Builder.SetInsertPoint(BeforeIR);
362-
return createCommon(Cond, True, False, Name, Builder, Ctx);
363-
}
364-
365-
Value *SelectInst::create(Value *Cond, Value *True, Value *False,
366-
BasicBlock *InsertAtEnd, Context &Ctx,
367-
const Twine &Name) {
368-
auto *IRInsertAtEnd = cast<llvm::BasicBlock>(InsertAtEnd->Val);
369-
auto &Builder = Ctx.getLLVMIRBuilder();
370-
Builder.SetInsertPoint(IRInsertAtEnd);
371-
return createCommon(Cond, True, False, Name, Builder, Ctx);
372-
}
373-
374351
void SelectInst::swapValues() {
375352
Ctx.getTracker().emplaceIfTracking<UseSwap>(getOperandUse(1),
376353
getOperandUse(2));
@@ -1791,23 +1768,9 @@ void PossiblyNonNegInst::setNonNeg(bool B) {
17911768
}
17921769

17931770
Value *InsertElementInst::create(Value *Vec, Value *NewElt, Value *Idx,
1794-
Instruction *InsertBefore, Context &Ctx,
1771+
InsertPosition Pos, Context &Ctx,
17951772
const Twine &Name) {
1796-
auto &Builder = Ctx.getLLVMIRBuilder();
1797-
Builder.SetInsertPoint(InsertBefore->getTopmostLLVMInstruction());
1798-
llvm::Value *NewV =
1799-
Builder.CreateInsertElement(Vec->Val, NewElt->Val, Idx->Val, Name);
1800-
if (auto *NewInsert = dyn_cast<llvm::InsertElementInst>(NewV))
1801-
return Ctx.createInsertElementInst(NewInsert);
1802-
assert(isa<llvm::Constant>(NewV) && "Expected constant");
1803-
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
1804-
}
1805-
1806-
Value *InsertElementInst::create(Value *Vec, Value *NewElt, Value *Idx,
1807-
BasicBlock *InsertAtEnd, Context &Ctx,
1808-
const Twine &Name) {
1809-
auto &Builder = Ctx.getLLVMIRBuilder();
1810-
Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
1773+
auto &Builder = Instruction::setInsertPos(Pos);
18111774
llvm::Value *NewV =
18121775
Builder.CreateInsertElement(Vec->Val, NewElt->Val, Idx->Val, Name);
18131776
if (auto *NewInsert = dyn_cast<llvm::InsertElementInst>(NewV))

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@ define void @foo() {
20372037
// Check create().
20382038
auto *NewFence =
20392039
sandboxir::FenceInst::create(AtomicOrdering::Release, Ret->getIterator(),
2040-
BB, Ctx, SyncScope::SingleThread);
2040+
Ctx, SyncScope::SingleThread);
20412041
EXPECT_EQ(NewFence->getNextNode(), Ret);
20422042
EXPECT_EQ(NewFence->getOrdering(), AtomicOrdering::Release);
20432043
EXPECT_EQ(NewFence->getSyncScopeID(), SyncScope::SingleThread);
@@ -2092,7 +2092,7 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) {
20922092
{
20932093
// Check SelectInst::create() InsertBefore.
20942094
auto *NewSel = cast<sandboxir::SelectInst>(sandboxir::SelectInst::create(
2095-
Cond0, V0, V1, /*InsertBefore=*/Ret, Ctx));
2095+
Cond0, V0, V1, /*InsertBefore=*/Ret->getIterator(), Ctx));
20962096
EXPECT_EQ(NewSel->getCondition(), Cond0);
20972097
EXPECT_EQ(NewSel->getTrueValue(), V0);
20982098
EXPECT_EQ(NewSel->getFalseValue(), V1);
@@ -2114,8 +2114,8 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) {
21142114
auto *FortyTwo =
21152115
sandboxir::ConstantInt::get(sandboxir::Type::getInt1Ty(Ctx), 42,
21162116
/*IsSigned=*/false);
2117-
auto *NewSel =
2118-
sandboxir::SelectInst::create(False, FortyTwo, FortyTwo, Ret, Ctx);
2117+
auto *NewSel = sandboxir::SelectInst::create(False, FortyTwo, FortyTwo,
2118+
Ret->getIterator(), Ctx);
21192119
EXPECT_TRUE(isa<sandboxir::Constant>(NewSel));
21202120
EXPECT_EQ(NewSel, FortyTwo);
21212121
}
@@ -2193,7 +2193,7 @@ define void @foo(i8 %v0, i8 %v1, <2 x i8> %vec) {
21932193
auto *Idx = Ins0->getOperand(2);
21942194
auto *NewI1 =
21952195
cast<sandboxir::InsertElementInst>(sandboxir::InsertElementInst::create(
2196-
Poison, Arg0, Idx, Ret, Ctx, "NewIns1"));
2196+
Poison, Arg0, Idx, Ret->getIterator(), Ctx, "NewIns1"));
21972197
EXPECT_EQ(NewI1->getOperand(0), Poison);
21982198
EXPECT_EQ(NewI1->getNextNode(), Ret);
21992199

0 commit comments

Comments
 (0)