Skip to content

Commit d54ec4d

Browse files
author
Vasileios Porpodas
committed
[SandboxIR][NFC] Implement InsertPosition
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 InsertPositoin.
1 parent c214af8 commit d54ec4d

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
@@ -18,6 +18,21 @@
1818

1919
namespace llvm::sandboxir {
2020

21+
class InsertPosition {
22+
BBIterator InsertAt;
23+
24+
public:
25+
InsertPosition(BasicBlock *InsertAtEnd) {
26+
assert(InsertAtEnd != nullptr && "Expected non-null!");
27+
InsertAt = InsertAtEnd->end();
28+
}
29+
InsertPosition(BBIterator InsertAt) : InsertAt(InsertAt) {}
30+
operator BBIterator() { return InsertAt; }
31+
const BBIterator &getIterator() const { return InsertAt; }
32+
Instruction &operator*() { return *InsertAt; }
33+
BasicBlock *getBasicBlock() const { return InsertAt.getNodeParent(); }
34+
};
35+
2136
/// A sandboxir::User with operands, opcode and linked with previous/next
2237
/// instructions in an instruction list.
2338
class Instruction : public User {
@@ -79,6 +94,20 @@ class Instruction : public User {
7994
virtual SmallVector<llvm::Instruction *, 1> getLLVMInstrs() const = 0;
8095
friend class EraseFromParent; // For getLLVMInstrs().
8196

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

400429
public:
401-
static FenceInst *create(AtomicOrdering Ordering, BBIterator WhereIt,
402-
BasicBlock *WhereBB, Context &Ctx,
430+
static FenceInst *create(AtomicOrdering Ordering, InsertPosition Pos,
431+
Context &Ctx,
403432
SyncScope::ID SSID = SyncScope::System);
404433
/// Returns the ordering constraint of this fence instruction.
405434
AtomicOrdering getOrdering() const {
@@ -425,16 +454,10 @@ class SelectInst : public SingleLLVMInstructionImpl<llvm::SelectInst> {
425454
SelectInst(llvm::SelectInst *CI, Context &Ctx)
426455
: SingleLLVMInstructionImpl(ClassID::Select, Opcode::Select, CI, Ctx) {}
427456
friend Context; // for SelectInst()
428-
static Value *createCommon(Value *Cond, Value *True, Value *False,
429-
const Twine &Name, IRBuilder<> &Builder,
430-
Context &Ctx);
431457

432458
public:
433459
static Value *create(Value *Cond, Value *True, Value *False,
434-
Instruction *InsertBefore, Context &Ctx,
435-
const Twine &Name = "");
436-
static Value *create(Value *Cond, Value *True, Value *False,
437-
BasicBlock *InsertAtEnd, Context &Ctx,
460+
InsertPosition Pos, Context &Ctx,
438461
const Twine &Name = "");
439462

440463
const Value *getCondition() const { return getOperand(0); }
@@ -471,10 +494,7 @@ class InsertElementInst final
471494

472495
public:
473496
static Value *create(Value *Vec, Value *NewElt, Value *Idx,
474-
Instruction *InsertBefore, Context &Ctx,
475-
const Twine &Name = "");
476-
static Value *create(Value *Vec, Value *NewElt, Value *Idx,
477-
BasicBlock *InsertAtEnd, Context &Ctx,
497+
InsertPosition Pos, Context &Ctx,
478498
const Twine &Name = "");
479499
static bool classof(const Value *From) {
480500
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
@@ -2169,7 +2169,7 @@ define void @foo() {
21692169
// Check create().
21702170
auto *NewFence =
21712171
sandboxir::FenceInst::create(AtomicOrdering::Release, Ret->getIterator(),
2172-
BB, Ctx, SyncScope::SingleThread);
2172+
Ctx, SyncScope::SingleThread);
21732173
EXPECT_EQ(NewFence->getNextNode(), Ret);
21742174
EXPECT_EQ(NewFence->getOrdering(), AtomicOrdering::Release);
21752175
EXPECT_EQ(NewFence->getSyncScopeID(), SyncScope::SingleThread);
@@ -2224,7 +2224,7 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) {
22242224
{
22252225
// Check SelectInst::create() InsertBefore.
22262226
auto *NewSel = cast<sandboxir::SelectInst>(sandboxir::SelectInst::create(
2227-
Cond0, V0, V1, /*InsertBefore=*/Ret, Ctx));
2227+
Cond0, V0, V1, /*InsertBefore=*/Ret->getIterator(), Ctx));
22282228
EXPECT_EQ(NewSel->getCondition(), Cond0);
22292229
EXPECT_EQ(NewSel->getTrueValue(), V0);
22302230
EXPECT_EQ(NewSel->getFalseValue(), V1);
@@ -2246,8 +2246,8 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) {
22462246
auto *FortyTwo =
22472247
sandboxir::ConstantInt::get(sandboxir::Type::getInt1Ty(Ctx), 42,
22482248
/*IsSigned=*/false);
2249-
auto *NewSel =
2250-
sandboxir::SelectInst::create(False, FortyTwo, FortyTwo, Ret, Ctx);
2249+
auto *NewSel = sandboxir::SelectInst::create(False, FortyTwo, FortyTwo,
2250+
Ret->getIterator(), Ctx);
22512251
EXPECT_TRUE(isa<sandboxir::Constant>(NewSel));
22522252
EXPECT_EQ(NewSel, FortyTwo);
22532253
}
@@ -2325,7 +2325,7 @@ define void @foo(i8 %v0, i8 %v1, <2 x i8> %vec) {
23252325
auto *Idx = Ins0->getOperand(2);
23262326
auto *NewI1 =
23272327
cast<sandboxir::InsertElementInst>(sandboxir::InsertElementInst::create(
2328-
Poison, Arg0, Idx, Ret, Ctx, "NewIns1"));
2328+
Poison, Arg0, Idx, Ret->getIterator(), Ctx, "NewIns1"));
23292329
EXPECT_EQ(NewI1->getOperand(0), Poison);
23302330
EXPECT_EQ(NewI1->getNextNode(), Ret);
23312331

0 commit comments

Comments
 (0)