Skip to content

Commit c029702

Browse files
authored
[SandboxIR] Switch more Instruction::create() to InsertPosition (#111208)
1 parent fda2fea commit c029702

File tree

3 files changed

+40
-143
lines changed

3 files changed

+40
-143
lines changed

llvm/include/llvm/SandboxIR/Instruction.h

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,17 +2171,7 @@ class AtomicCmpXchgInst
21712171
static AtomicCmpXchgInst *
21722172
create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
21732173
AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
2174-
BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
2175-
SyncScope::ID SSID = SyncScope::System, const Twine &Name = "");
2176-
static AtomicCmpXchgInst *
2177-
create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
2178-
AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
2179-
Instruction *InsertBefore, Context &Ctx,
2180-
SyncScope::ID SSID = SyncScope::System, const Twine &Name = "");
2181-
static AtomicCmpXchgInst *
2182-
create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
2183-
AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
2184-
BasicBlock *InsertAtEnd, Context &Ctx,
2174+
InsertPosition Pos, Context &Ctx,
21852175
SyncScope::ID SSID = SyncScope::System, const Twine &Name = "");
21862176

21872177
static bool classof(const Value *From) {
@@ -2196,15 +2186,9 @@ class AllocaInst final : public UnaryInstruction {
21962186
friend class Context; // For constructor.
21972187

21982188
public:
2199-
static AllocaInst *create(Type *Ty, unsigned AddrSpace, BBIterator WhereIt,
2200-
BasicBlock *WhereBB, Context &Ctx,
2201-
Value *ArraySize = nullptr, const Twine &Name = "");
2202-
static AllocaInst *create(Type *Ty, unsigned AddrSpace,
2203-
Instruction *InsertBefore, Context &Ctx,
2204-
Value *ArraySize = nullptr, const Twine &Name = "");
2205-
static AllocaInst *create(Type *Ty, unsigned AddrSpace,
2206-
BasicBlock *InsertAtEnd, Context &Ctx,
2207-
Value *ArraySize = nullptr, const Twine &Name = "");
2189+
static AllocaInst *create(Type *Ty, unsigned AddrSpace, InsertPosition Pos,
2190+
Context &Ctx, Value *ArraySize = nullptr,
2191+
const Twine &Name = "");
22082192

22092193
/// Return true if there is an allocation size parameter to the allocation
22102194
/// instruction that is not 1.
@@ -2306,13 +2290,7 @@ class CastInst : public UnaryInstruction {
23062290

23072291
public:
23082292
static Value *create(Type *DestTy, Opcode Op, Value *Operand,
2309-
BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
2310-
const Twine &Name = "");
2311-
static Value *create(Type *DestTy, Opcode Op, Value *Operand,
2312-
Instruction *InsertBefore, Context &Ctx,
2313-
const Twine &Name = "");
2314-
static Value *create(Type *DestTy, Opcode Op, Value *Operand,
2315-
BasicBlock *InsertAtEnd, Context &Ctx,
2293+
InsertPosition Pos, Context &Ctx,
23162294
const Twine &Name = "");
23172295
/// For isa/dyn_cast.
23182296
static bool classof(const Value *From);
@@ -2345,19 +2323,9 @@ class PossiblyNonNegInst : public CastInst {
23452323
// Helper class to simplify stamping out CastInst subclasses.
23462324
template <Instruction::Opcode Op> class CastInstImpl : public CastInst {
23472325
public:
2348-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
2349-
BasicBlock *WhereBB, Context &Ctx,
2350-
const Twine &Name = "") {
2351-
return CastInst::create(DestTy, Op, Src, WhereIt, WhereBB, Ctx, Name);
2352-
}
2353-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
2354-
Context &Ctx, const Twine &Name = "") {
2355-
return create(Src, DestTy, InsertBefore->getIterator(),
2356-
InsertBefore->getParent(), Ctx, Name);
2357-
}
2358-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
2326+
static Value *create(Value *Src, Type *DestTy, InsertPosition Pos,
23592327
Context &Ctx, const Twine &Name = "") {
2360-
return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
2328+
return CastInst::create(DestTy, Op, Src, Pos, Ctx, Name);
23612329
}
23622330

23632331
static bool classof(const Value *From) {
@@ -2416,7 +2384,7 @@ class PHINode final : public SingleLLVMInstructionImpl<llvm::PHINode> {
24162384

24172385
public:
24182386
static PHINode *create(Type *Ty, unsigned NumReservedValues,
2419-
Instruction *InsertBefore, Context &Ctx,
2387+
InsertPosition Pos, Context &Ctx,
24202388
const Twine &Name = "");
24212389
/// For isa/dyn_cast.
24222390
static bool classof(const Value *From);

llvm/lib/SandboxIR/Instruction.cpp

Lines changed: 13 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -825,11 +825,10 @@ BasicBlock *PHINode::LLVMBBToBB::operator()(llvm::BasicBlock *LLVMBB) const {
825825
}
826826

827827
PHINode *PHINode::create(Type *Ty, unsigned NumReservedValues,
828-
Instruction *InsertBefore, Context &Ctx,
829-
const Twine &Name) {
830-
llvm::PHINode *NewPHI = llvm::PHINode::Create(
831-
Ty->LLVMTy, NumReservedValues, Name,
832-
InsertBefore->getTopmostLLVMInstruction()->getIterator());
828+
InsertPosition Pos, Context &Ctx, const Twine &Name) {
829+
auto &Builder = setInsertPos(Pos);
830+
llvm::PHINode *NewPHI =
831+
Builder.CreatePHI(Ty->LLVMTy, NumReservedValues, Name);
833832
return Ctx.createPHINode(NewPHI);
834833
}
835834

@@ -1260,44 +1259,16 @@ Value *AtomicCmpXchgInst::getNewValOperand() {
12601259
AtomicCmpXchgInst *
12611260
AtomicCmpXchgInst::create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
12621261
AtomicOrdering SuccessOrdering,
1263-
AtomicOrdering FailureOrdering, BBIterator WhereIt,
1264-
BasicBlock *WhereBB, Context &Ctx, SyncScope::ID SSID,
1265-
const Twine &Name) {
1266-
auto &Builder = Ctx.getLLVMIRBuilder();
1267-
if (WhereIt == WhereBB->end())
1268-
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
1269-
else
1270-
Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
1262+
AtomicOrdering FailureOrdering, InsertPosition Pos,
1263+
Context &Ctx, SyncScope::ID SSID, const Twine &Name) {
1264+
auto &Builder = setInsertPos(Pos);
12711265
auto *LLVMAtomicCmpXchg =
12721266
Builder.CreateAtomicCmpXchg(Ptr->Val, Cmp->Val, New->Val, Align,
12731267
SuccessOrdering, FailureOrdering, SSID);
12741268
LLVMAtomicCmpXchg->setName(Name);
12751269
return Ctx.createAtomicCmpXchgInst(LLVMAtomicCmpXchg);
12761270
}
12771271

1278-
AtomicCmpXchgInst *AtomicCmpXchgInst::create(Value *Ptr, Value *Cmp, Value *New,
1279-
MaybeAlign Align,
1280-
AtomicOrdering SuccessOrdering,
1281-
AtomicOrdering FailureOrdering,
1282-
Instruction *InsertBefore,
1283-
Context &Ctx, SyncScope::ID SSID,
1284-
const Twine &Name) {
1285-
return create(Ptr, Cmp, New, Align, SuccessOrdering, FailureOrdering,
1286-
InsertBefore->getIterator(), InsertBefore->getParent(), Ctx,
1287-
SSID, Name);
1288-
}
1289-
1290-
AtomicCmpXchgInst *AtomicCmpXchgInst::create(Value *Ptr, Value *Cmp, Value *New,
1291-
MaybeAlign Align,
1292-
AtomicOrdering SuccessOrdering,
1293-
AtomicOrdering FailureOrdering,
1294-
BasicBlock *InsertAtEnd,
1295-
Context &Ctx, SyncScope::ID SSID,
1296-
const Twine &Name) {
1297-
return create(Ptr, Cmp, New, Align, SuccessOrdering, FailureOrdering,
1298-
InsertAtEnd->end(), InsertAtEnd, Ctx, SSID, Name);
1299-
}
1300-
13011272
void AtomicCmpXchgInst::setAlignment(Align Align) {
13021273
Ctx.getTracker()
13031274
.emplaceIfTracking<GenericSetter<&AtomicCmpXchgInst::getAlign,
@@ -1335,33 +1306,15 @@ void AtomicCmpXchgInst::setFailureOrdering(AtomicOrdering Ordering) {
13351306
cast<llvm::AtomicCmpXchgInst>(Val)->setFailureOrdering(Ordering);
13361307
}
13371308

1338-
AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace, BBIterator WhereIt,
1339-
BasicBlock *WhereBB, Context &Ctx,
1340-
Value *ArraySize, const Twine &Name) {
1341-
auto &Builder = Ctx.getLLVMIRBuilder();
1342-
if (WhereIt == WhereBB->end())
1343-
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
1344-
else
1345-
Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
1309+
AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace, InsertPosition Pos,
1310+
Context &Ctx, Value *ArraySize,
1311+
const Twine &Name) {
1312+
auto &Builder = setInsertPos(Pos);
13461313
auto *NewAlloca =
13471314
Builder.CreateAlloca(Ty->LLVMTy, AddrSpace, ArraySize->Val, Name);
13481315
return Ctx.createAllocaInst(NewAlloca);
13491316
}
13501317

1351-
AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace,
1352-
Instruction *InsertBefore, Context &Ctx,
1353-
Value *ArraySize, const Twine &Name) {
1354-
return create(Ty, AddrSpace, InsertBefore->getIterator(),
1355-
InsertBefore->getParent(), Ctx, ArraySize, Name);
1356-
}
1357-
1358-
AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace,
1359-
BasicBlock *InsertAtEnd, Context &Ctx,
1360-
Value *ArraySize, const Twine &Name) {
1361-
return create(Ty, AddrSpace, InsertAtEnd->end(), InsertAtEnd, Ctx, ArraySize,
1362-
Name);
1363-
}
1364-
13651318
Type *AllocaInst::getAllocatedType() const {
13661319
return Ctx.getType(cast<llvm::AllocaInst>(Val)->getAllocatedType());
13671320
}
@@ -1397,14 +1350,9 @@ PointerType *AllocaInst::getType() const {
13971350
}
13981351

13991352
Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand,
1400-
BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
1401-
const Twine &Name) {
1353+
InsertPosition Pos, Context &Ctx, const Twine &Name) {
14021354
assert(getLLVMCastOp(Op) && "Opcode not suitable for CastInst!");
1403-
auto &Builder = Ctx.getLLVMIRBuilder();
1404-
if (WhereIt == WhereBB->end())
1405-
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
1406-
else
1407-
Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
1355+
auto &Builder = setInsertPos(Pos);
14081356
auto *NewV =
14091357
Builder.CreateCast(getLLVMCastOp(Op), Operand->Val, DestTy->LLVMTy, Name);
14101358
if (auto *NewCI = dyn_cast<llvm::CastInst>(NewV))
@@ -1413,20 +1361,6 @@ Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand,
14131361
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
14141362
}
14151363

1416-
Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand,
1417-
Instruction *InsertBefore, Context &Ctx,
1418-
const Twine &Name) {
1419-
return create(DestTy, Op, Operand, InsertBefore->getIterator(),
1420-
InsertBefore->getParent(), Ctx, Name);
1421-
}
1422-
1423-
Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand,
1424-
BasicBlock *InsertAtEnd, Context &Ctx,
1425-
const Twine &Name) {
1426-
return create(DestTy, Op, Operand, InsertAtEnd->end(), InsertAtEnd, Ctx,
1427-
Name);
1428-
}
1429-
14301364
bool CastInst::classof(const Value *From) {
14311365
return From->getSubclassID() == ClassID::Cast;
14321366
}

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4899,8 +4899,7 @@ define void @foo(ptr %ptr, i8 %cmp, i8 %new) {
48994899
auto *NewI =
49004900
cast<sandboxir::AtomicCmpXchgInst>(sandboxir::AtomicCmpXchgInst::create(
49014901
Ptr, Cmp, New, Align, SuccOrdering, FailOrdering,
4902-
/*WhereIt=*/Ret->getIterator(),
4903-
/*WhereBB=*/Ret->getParent(), Ctx, SSID, "NewAtomicCmpXchg1"));
4902+
Ret->getIterator(), Ctx, SSID, "NewAtomicCmpXchg1"));
49044903
// Check getOpcode().
49054904
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::AtomicCmpXchg);
49064905
// Check getAlign().
@@ -4927,7 +4926,7 @@ define void @foo(ptr %ptr, i8 %cmp, i8 %new) {
49274926
auto *NewI =
49284927
cast<sandboxir::AtomicCmpXchgInst>(sandboxir::AtomicCmpXchgInst::create(
49294928
Ptr, Cmp, New, Align, SuccOrdering, FailOrdering,
4930-
/*InsertBefore=*/Ret, Ctx, SSID, "NewAtomicCmpXchg2"));
4929+
Ret->getIterator(), Ctx, SSID, "NewAtomicCmpXchg2"));
49314930
// Check getOpcode().
49324931
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::AtomicCmpXchg);
49334932
// Check getAlign().
@@ -4953,8 +4952,8 @@ define void @foo(ptr %ptr, i8 %cmp, i8 %new) {
49534952
// Check create() InsertAtEnd.
49544953
auto *NewI =
49554954
cast<sandboxir::AtomicCmpXchgInst>(sandboxir::AtomicCmpXchgInst::create(
4956-
Ptr, Cmp, New, Align, SuccOrdering, FailOrdering,
4957-
/*InsertAtEnd=*/BB, Ctx, SSID, "NewAtomicCmpXchg3"));
4955+
Ptr, Cmp, New, Align, SuccOrdering, FailOrdering, BB, Ctx, SSID,
4956+
"NewAtomicCmpXchg3"));
49584957
// Check getOpcode().
49594958
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::AtomicCmpXchg);
49604959
// Check getAlign().
@@ -5074,8 +5073,7 @@ define void @foo() {
50745073
{
50755074
// Check create() WhereIt, WhereBB.
50765075
auto *NewI = cast<sandboxir::AllocaInst>(sandboxir::AllocaInst::create(
5077-
Ty, AddrSpace, /*WhereIt=*/Ret->getIterator(),
5078-
/*WhereBB=*/Ret->getParent(), Ctx, ArraySize, "NewAlloca1"));
5076+
Ty, AddrSpace, Ret->getIterator(), Ctx, ArraySize, "NewAlloca1"));
50795077
// Check getOpcode().
50805078
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::Alloca);
50815079
// Check getType().
@@ -5090,7 +5088,7 @@ define void @foo() {
50905088
{
50915089
// Check create() InsertBefore.
50925090
auto *NewI = cast<sandboxir::AllocaInst>(sandboxir::AllocaInst::create(
5093-
Ty, AddrSpace, /*InsertBefore=*/Ret, Ctx, ArraySize, "NewAlloca2"));
5091+
Ty, AddrSpace, Ret->getIterator(), Ctx, ArraySize, "NewAlloca2"));
50945092
// Check getOpcode().
50955093
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::Alloca);
50965094
// Check getType().
@@ -5105,7 +5103,7 @@ define void @foo() {
51055103
{
51065104
// Check create() InsertAtEnd.
51075105
auto *NewI = cast<sandboxir::AllocaInst>(sandboxir::AllocaInst::create(
5108-
Ty, AddrSpace, /*InsertAtEnd=*/BB, Ctx, ArraySize, "NewAlloca3"));
5106+
Ty, AddrSpace, BB, Ctx, ArraySize, "NewAlloca3"));
51095107
// Check getOpcode().
51105108
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::Alloca);
51115109
// Check getType().
@@ -5265,9 +5263,9 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
52655263

52665264
{
52675265
// Check create() WhereIt, WhereBB
5268-
auto *NewI = cast<sandboxir::CastInst>(sandboxir::CastInst::create(
5269-
Ti64, sandboxir::Instruction::Opcode::SExt, Arg, /*WhereIt=*/BB->end(),
5270-
/*WhereBB=*/BB, Ctx, "SExt"));
5266+
auto *NewI = cast<sandboxir::CastInst>(
5267+
sandboxir::CastInst::create(Ti64, sandboxir::Instruction::Opcode::SExt,
5268+
Arg, BB->end(), Ctx, "SExt"));
52715269
// Check getOpcode().
52725270
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::SExt);
52735271
// Check getSrcTy().
@@ -5283,7 +5281,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
52835281
// Check create() InsertBefore.
52845282
auto *NewI = cast<sandboxir::CastInst>(
52855283
sandboxir::CastInst::create(Ti64, sandboxir::Instruction::Opcode::ZExt,
5286-
Arg, /*InsertBefore=*/Ret, Ctx, "ZExt"));
5284+
Arg, Ret->getIterator(), Ctx, "ZExt"));
52875285
// Check getOpcode().
52885286
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::ZExt);
52895287
// Check getSrcTy().
@@ -5295,9 +5293,8 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
52955293
}
52965294
{
52975295
// Check create() InsertAtEnd.
5298-
auto *NewI = cast<sandboxir::CastInst>(
5299-
sandboxir::CastInst::create(Ti64, sandboxir::Instruction::Opcode::ZExt,
5300-
Arg, /*InsertAtEnd=*/BB, Ctx, "ZExt"));
5296+
auto *NewI = cast<sandboxir::CastInst>(sandboxir::CastInst::create(
5297+
Ti64, sandboxir::Instruction::Opcode::ZExt, Arg, BB, Ctx, "ZExt"));
53015298
// Check getOpcode().
53025299
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::ZExt);
53035300
// Check getSrcTy().
@@ -5314,7 +5311,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
53145311
// Check that passing a non-cast opcode crashes.
53155312
EXPECT_DEATH(
53165313
sandboxir::CastInst::create(Ti64, sandboxir::Instruction::Opcode::Store,
5317-
Arg, /*InsertBefore=*/Ret, Ctx, "Bad"),
5314+
Arg, Ret->getIterator(), Ctx, "Bad"),
53185315
".*Opcode.*");
53195316
#endif // NDEBUG
53205317
}
@@ -5386,8 +5383,7 @@ void testCastInst(llvm::Module &M, llvm::Type *LLVMSrcTy,
53865383
{
53875384
// Check create() WhereIt, WhereBB
53885385
auto *NewI =
5389-
cast<SubclassT>(SubclassT::create(Arg, DstTy, /*WhereIt=*/BB->end(),
5390-
/*WhereBB=*/BB, Ctx, "NewCI"));
5386+
cast<SubclassT>(SubclassT::create(Arg, DstTy, BB->end(), Ctx, "NewCI"));
53915387
// Check getOpcode().
53925388
EXPECT_EQ(NewI->getOpcode(), OpcodeT);
53935389
// Check getSrcTy().
@@ -5402,9 +5398,8 @@ void testCastInst(llvm::Module &M, llvm::Type *LLVMSrcTy,
54025398
}
54035399
{
54045400
// Check create() InsertBefore.
5405-
auto *NewI =
5406-
cast<SubclassT>(SubclassT::create(Arg, DstTy,
5407-
/*InsertBefore=*/Ret, Ctx, "NewCI"));
5401+
auto *NewI = cast<SubclassT>(
5402+
SubclassT::create(Arg, DstTy, Ret->getIterator(), Ctx, "NewCI"));
54085403
// Check getOpcode().
54095404
EXPECT_EQ(NewI->getOpcode(), OpcodeT);
54105405
// Check getSrcTy().
@@ -5744,8 +5739,8 @@ define void @foo(i32 %arg) {
57445739
EXPECT_EQ(PHI->getIncomingBlock(1), RemainBB1);
57455740
EXPECT_EQ(PHI->getIncomingBlock(2), RemainBB2);
57465741
// Check create().
5747-
auto *NewPHI = cast<sandboxir::PHINode>(
5748-
sandboxir::PHINode::create(PHI->getType(), 0, Br, Ctx, "NewPHI"));
5742+
auto *NewPHI = cast<sandboxir::PHINode>(sandboxir::PHINode::create(
5743+
PHI->getType(), 0, Br->getIterator(), Ctx, "NewPHI"));
57495744
EXPECT_EQ(NewPHI->getType(), PHI->getType());
57505745
EXPECT_EQ(NewPHI->getNextNode(), Br);
57515746
EXPECT_EQ(NewPHI->getName(), "NewPHI");

0 commit comments

Comments
 (0)