Skip to content

[SandboxIR] Switch more Instruction::create() to InsertPosition #111208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 8 additions & 40 deletions llvm/include/llvm/SandboxIR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2171,17 +2171,7 @@ class AtomicCmpXchgInst
static AtomicCmpXchgInst *
create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
SyncScope::ID SSID = SyncScope::System, const Twine &Name = "");
static AtomicCmpXchgInst *
create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
Instruction *InsertBefore, Context &Ctx,
SyncScope::ID SSID = SyncScope::System, const Twine &Name = "");
static AtomicCmpXchgInst *
create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
BasicBlock *InsertAtEnd, Context &Ctx,
InsertPosition Pos, Context &Ctx,
SyncScope::ID SSID = SyncScope::System, const Twine &Name = "");

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

public:
static AllocaInst *create(Type *Ty, unsigned AddrSpace, BBIterator WhereIt,
BasicBlock *WhereBB, Context &Ctx,
Value *ArraySize = nullptr, const Twine &Name = "");
static AllocaInst *create(Type *Ty, unsigned AddrSpace,
Instruction *InsertBefore, Context &Ctx,
Value *ArraySize = nullptr, const Twine &Name = "");
static AllocaInst *create(Type *Ty, unsigned AddrSpace,
BasicBlock *InsertAtEnd, Context &Ctx,
Value *ArraySize = nullptr, const Twine &Name = "");
static AllocaInst *create(Type *Ty, unsigned AddrSpace, InsertPosition Pos,
Context &Ctx, Value *ArraySize = nullptr,
const Twine &Name = "");

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

public:
static Value *create(Type *DestTy, Opcode Op, Value *Operand,
BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
const Twine &Name = "");
static Value *create(Type *DestTy, Opcode Op, Value *Operand,
Instruction *InsertBefore, Context &Ctx,
const Twine &Name = "");
static Value *create(Type *DestTy, Opcode Op, Value *Operand,
BasicBlock *InsertAtEnd, Context &Ctx,
InsertPosition Pos, Context &Ctx,
const Twine &Name = "");
/// For isa/dyn_cast.
static bool classof(const Value *From);
Expand Down Expand Up @@ -2345,19 +2323,9 @@ class PossiblyNonNegInst : public CastInst {
// Helper class to simplify stamping out CastInst subclasses.
template <Instruction::Opcode Op> class CastInstImpl : public CastInst {
public:
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
BasicBlock *WhereBB, Context &Ctx,
const Twine &Name = "") {
return CastInst::create(DestTy, Op, Src, WhereIt, WhereBB, Ctx, Name);
}
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
Context &Ctx, const Twine &Name = "") {
return create(Src, DestTy, InsertBefore->getIterator(),
InsertBefore->getParent(), Ctx, Name);
}
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
static Value *create(Value *Src, Type *DestTy, InsertPosition Pos,
Context &Ctx, const Twine &Name = "") {
return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
return CastInst::create(DestTy, Op, Src, Pos, Ctx, Name);
}

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

public:
static PHINode *create(Type *Ty, unsigned NumReservedValues,
Instruction *InsertBefore, Context &Ctx,
InsertPosition Pos, Context &Ctx,
const Twine &Name = "");
/// For isa/dyn_cast.
static bool classof(const Value *From);
Expand Down
92 changes: 13 additions & 79 deletions llvm/lib/SandboxIR/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,10 @@ BasicBlock *PHINode::LLVMBBToBB::operator()(llvm::BasicBlock *LLVMBB) const {
}

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

Expand Down Expand Up @@ -1260,44 +1259,16 @@ Value *AtomicCmpXchgInst::getNewValOperand() {
AtomicCmpXchgInst *
AtomicCmpXchgInst::create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
AtomicOrdering SuccessOrdering,
AtomicOrdering FailureOrdering, BBIterator WhereIt,
BasicBlock *WhereBB, Context &Ctx, SyncScope::ID SSID,
const Twine &Name) {
auto &Builder = Ctx.getLLVMIRBuilder();
if (WhereIt == WhereBB->end())
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
else
Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
AtomicOrdering FailureOrdering, InsertPosition Pos,
Context &Ctx, SyncScope::ID SSID, const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *LLVMAtomicCmpXchg =
Builder.CreateAtomicCmpXchg(Ptr->Val, Cmp->Val, New->Val, Align,
SuccessOrdering, FailureOrdering, SSID);
LLVMAtomicCmpXchg->setName(Name);
return Ctx.createAtomicCmpXchgInst(LLVMAtomicCmpXchg);
}

AtomicCmpXchgInst *AtomicCmpXchgInst::create(Value *Ptr, Value *Cmp, Value *New,
MaybeAlign Align,
AtomicOrdering SuccessOrdering,
AtomicOrdering FailureOrdering,
Instruction *InsertBefore,
Context &Ctx, SyncScope::ID SSID,
const Twine &Name) {
return create(Ptr, Cmp, New, Align, SuccessOrdering, FailureOrdering,
InsertBefore->getIterator(), InsertBefore->getParent(), Ctx,
SSID, Name);
}

AtomicCmpXchgInst *AtomicCmpXchgInst::create(Value *Ptr, Value *Cmp, Value *New,
MaybeAlign Align,
AtomicOrdering SuccessOrdering,
AtomicOrdering FailureOrdering,
BasicBlock *InsertAtEnd,
Context &Ctx, SyncScope::ID SSID,
const Twine &Name) {
return create(Ptr, Cmp, New, Align, SuccessOrdering, FailureOrdering,
InsertAtEnd->end(), InsertAtEnd, Ctx, SSID, Name);
}

void AtomicCmpXchgInst::setAlignment(Align Align) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicCmpXchgInst::getAlign,
Expand Down Expand Up @@ -1335,33 +1306,15 @@ void AtomicCmpXchgInst::setFailureOrdering(AtomicOrdering Ordering) {
cast<llvm::AtomicCmpXchgInst>(Val)->setFailureOrdering(Ordering);
}

AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace, BBIterator WhereIt,
BasicBlock *WhereBB, Context &Ctx,
Value *ArraySize, const Twine &Name) {
auto &Builder = Ctx.getLLVMIRBuilder();
if (WhereIt == WhereBB->end())
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
else
Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace, InsertPosition Pos,
Context &Ctx, Value *ArraySize,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *NewAlloca =
Builder.CreateAlloca(Ty->LLVMTy, AddrSpace, ArraySize->Val, Name);
return Ctx.createAllocaInst(NewAlloca);
}

AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace,
Instruction *InsertBefore, Context &Ctx,
Value *ArraySize, const Twine &Name) {
return create(Ty, AddrSpace, InsertBefore->getIterator(),
InsertBefore->getParent(), Ctx, ArraySize, Name);
}

AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace,
BasicBlock *InsertAtEnd, Context &Ctx,
Value *ArraySize, const Twine &Name) {
return create(Ty, AddrSpace, InsertAtEnd->end(), InsertAtEnd, Ctx, ArraySize,
Name);
}

Type *AllocaInst::getAllocatedType() const {
return Ctx.getType(cast<llvm::AllocaInst>(Val)->getAllocatedType());
}
Expand Down Expand Up @@ -1397,14 +1350,9 @@ PointerType *AllocaInst::getType() const {
}

Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand,
BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
const Twine &Name) {
InsertPosition Pos, Context &Ctx, const Twine &Name) {
assert(getLLVMCastOp(Op) && "Opcode not suitable for CastInst!");
auto &Builder = Ctx.getLLVMIRBuilder();
if (WhereIt == WhereBB->end())
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
else
Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
auto &Builder = setInsertPos(Pos);
auto *NewV =
Builder.CreateCast(getLLVMCastOp(Op), Operand->Val, DestTy->LLVMTy, Name);
if (auto *NewCI = dyn_cast<llvm::CastInst>(NewV))
Expand All @@ -1413,20 +1361,6 @@ Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand,
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}

Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand,
Instruction *InsertBefore, Context &Ctx,
const Twine &Name) {
return create(DestTy, Op, Operand, InsertBefore->getIterator(),
InsertBefore->getParent(), Ctx, Name);
}

Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand,
BasicBlock *InsertAtEnd, Context &Ctx,
const Twine &Name) {
return create(DestTy, Op, Operand, InsertAtEnd->end(), InsertAtEnd, Ctx,
Name);
}

bool CastInst::classof(const Value *From) {
return From->getSubclassID() == ClassID::Cast;
}
Expand Down
43 changes: 19 additions & 24 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4899,8 +4899,7 @@ define void @foo(ptr %ptr, i8 %cmp, i8 %new) {
auto *NewI =
cast<sandboxir::AtomicCmpXchgInst>(sandboxir::AtomicCmpXchgInst::create(
Ptr, Cmp, New, Align, SuccOrdering, FailOrdering,
/*WhereIt=*/Ret->getIterator(),
/*WhereBB=*/Ret->getParent(), Ctx, SSID, "NewAtomicCmpXchg1"));
Ret->getIterator(), Ctx, SSID, "NewAtomicCmpXchg1"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::AtomicCmpXchg);
// Check getAlign().
Expand All @@ -4927,7 +4926,7 @@ define void @foo(ptr %ptr, i8 %cmp, i8 %new) {
auto *NewI =
cast<sandboxir::AtomicCmpXchgInst>(sandboxir::AtomicCmpXchgInst::create(
Ptr, Cmp, New, Align, SuccOrdering, FailOrdering,
/*InsertBefore=*/Ret, Ctx, SSID, "NewAtomicCmpXchg2"));
Ret->getIterator(), Ctx, SSID, "NewAtomicCmpXchg2"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::AtomicCmpXchg);
// Check getAlign().
Expand All @@ -4953,8 +4952,8 @@ define void @foo(ptr %ptr, i8 %cmp, i8 %new) {
// Check create() InsertAtEnd.
auto *NewI =
cast<sandboxir::AtomicCmpXchgInst>(sandboxir::AtomicCmpXchgInst::create(
Ptr, Cmp, New, Align, SuccOrdering, FailOrdering,
/*InsertAtEnd=*/BB, Ctx, SSID, "NewAtomicCmpXchg3"));
Ptr, Cmp, New, Align, SuccOrdering, FailOrdering, BB, Ctx, SSID,
"NewAtomicCmpXchg3"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::AtomicCmpXchg);
// Check getAlign().
Expand Down Expand Up @@ -5074,8 +5073,7 @@ define void @foo() {
{
// Check create() WhereIt, WhereBB.
auto *NewI = cast<sandboxir::AllocaInst>(sandboxir::AllocaInst::create(
Ty, AddrSpace, /*WhereIt=*/Ret->getIterator(),
/*WhereBB=*/Ret->getParent(), Ctx, ArraySize, "NewAlloca1"));
Ty, AddrSpace, Ret->getIterator(), Ctx, ArraySize, "NewAlloca1"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::Alloca);
// Check getType().
Expand All @@ -5090,7 +5088,7 @@ define void @foo() {
{
// Check create() InsertBefore.
auto *NewI = cast<sandboxir::AllocaInst>(sandboxir::AllocaInst::create(
Ty, AddrSpace, /*InsertBefore=*/Ret, Ctx, ArraySize, "NewAlloca2"));
Ty, AddrSpace, Ret->getIterator(), Ctx, ArraySize, "NewAlloca2"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::Alloca);
// Check getType().
Expand All @@ -5105,7 +5103,7 @@ define void @foo() {
{
// Check create() InsertAtEnd.
auto *NewI = cast<sandboxir::AllocaInst>(sandboxir::AllocaInst::create(
Ty, AddrSpace, /*InsertAtEnd=*/BB, Ctx, ArraySize, "NewAlloca3"));
Ty, AddrSpace, BB, Ctx, ArraySize, "NewAlloca3"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::Alloca);
// Check getType().
Expand Down Expand Up @@ -5265,9 +5263,9 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {

{
// Check create() WhereIt, WhereBB
auto *NewI = cast<sandboxir::CastInst>(sandboxir::CastInst::create(
Ti64, sandboxir::Instruction::Opcode::SExt, Arg, /*WhereIt=*/BB->end(),
/*WhereBB=*/BB, Ctx, "SExt"));
auto *NewI = cast<sandboxir::CastInst>(
sandboxir::CastInst::create(Ti64, sandboxir::Instruction::Opcode::SExt,
Arg, BB->end(), Ctx, "SExt"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::SExt);
// Check getSrcTy().
Expand All @@ -5283,7 +5281,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
// Check create() InsertBefore.
auto *NewI = cast<sandboxir::CastInst>(
sandboxir::CastInst::create(Ti64, sandboxir::Instruction::Opcode::ZExt,
Arg, /*InsertBefore=*/Ret, Ctx, "ZExt"));
Arg, Ret->getIterator(), Ctx, "ZExt"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::ZExt);
// Check getSrcTy().
Expand All @@ -5295,9 +5293,8 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
}
{
// Check create() InsertAtEnd.
auto *NewI = cast<sandboxir::CastInst>(
sandboxir::CastInst::create(Ti64, sandboxir::Instruction::Opcode::ZExt,
Arg, /*InsertAtEnd=*/BB, Ctx, "ZExt"));
auto *NewI = cast<sandboxir::CastInst>(sandboxir::CastInst::create(
Ti64, sandboxir::Instruction::Opcode::ZExt, Arg, BB, Ctx, "ZExt"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::ZExt);
// Check getSrcTy().
Expand All @@ -5314,7 +5311,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
// Check that passing a non-cast opcode crashes.
EXPECT_DEATH(
sandboxir::CastInst::create(Ti64, sandboxir::Instruction::Opcode::Store,
Arg, /*InsertBefore=*/Ret, Ctx, "Bad"),
Arg, Ret->getIterator(), Ctx, "Bad"),
".*Opcode.*");
#endif // NDEBUG
}
Expand Down Expand Up @@ -5386,8 +5383,7 @@ void testCastInst(llvm::Module &M, llvm::Type *LLVMSrcTy,
{
// Check create() WhereIt, WhereBB
auto *NewI =
cast<SubclassT>(SubclassT::create(Arg, DstTy, /*WhereIt=*/BB->end(),
/*WhereBB=*/BB, Ctx, "NewCI"));
cast<SubclassT>(SubclassT::create(Arg, DstTy, BB->end(), Ctx, "NewCI"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), OpcodeT);
// Check getSrcTy().
Expand All @@ -5402,9 +5398,8 @@ void testCastInst(llvm::Module &M, llvm::Type *LLVMSrcTy,
}
{
// Check create() InsertBefore.
auto *NewI =
cast<SubclassT>(SubclassT::create(Arg, DstTy,
/*InsertBefore=*/Ret, Ctx, "NewCI"));
auto *NewI = cast<SubclassT>(
SubclassT::create(Arg, DstTy, Ret->getIterator(), Ctx, "NewCI"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), OpcodeT);
// Check getSrcTy().
Expand Down Expand Up @@ -5744,8 +5739,8 @@ define void @foo(i32 %arg) {
EXPECT_EQ(PHI->getIncomingBlock(1), RemainBB1);
EXPECT_EQ(PHI->getIncomingBlock(2), RemainBB2);
// Check create().
auto *NewPHI = cast<sandboxir::PHINode>(
sandboxir::PHINode::create(PHI->getType(), 0, Br, Ctx, "NewPHI"));
auto *NewPHI = cast<sandboxir::PHINode>(sandboxir::PHINode::create(
PHI->getType(), 0, Br->getIterator(), Ctx, "NewPHI"));
EXPECT_EQ(NewPHI->getType(), PHI->getType());
EXPECT_EQ(NewPHI->getNextNode(), Br);
EXPECT_EQ(NewPHI->getName(), "NewPHI");
Expand Down
Loading