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

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 4 13:40:50 PDT 2024


https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/111208

None

>From 7f373a580b5592a8e9d779d310aa6e884e166dff Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Tue, 1 Oct 2024 15:13:04 -0700
Subject: [PATCH] [SandboxIR] Switch more Instruction::create() to
 InsertPosition

---
 llvm/include/llvm/SandboxIR/Instruction.h  | 48 ++---------
 llvm/lib/SandboxIR/Instruction.cpp         | 92 +++-------------------
 llvm/unittests/SandboxIR/SandboxIRTest.cpp | 43 +++++-----
 3 files changed, 40 insertions(+), 143 deletions(-)

diff --git a/llvm/include/llvm/SandboxIR/Instruction.h b/llvm/include/llvm/SandboxIR/Instruction.h
index 82356f282e141c..036882d8b13995 100644
--- a/llvm/include/llvm/SandboxIR/Instruction.h
+++ b/llvm/include/llvm/SandboxIR/Instruction.h
@@ -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) {
@@ -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.
@@ -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);
@@ -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) {
@@ -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);
diff --git a/llvm/lib/SandboxIR/Instruction.cpp b/llvm/lib/SandboxIR/Instruction.cpp
index 4fe1f54d3ec623..6bf58c71a7c9f0 100644
--- a/llvm/lib/SandboxIR/Instruction.cpp
+++ b/llvm/lib/SandboxIR/Instruction.cpp
@@ -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);
 }
 
@@ -1260,14 +1259,9 @@ 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);
@@ -1275,29 +1269,6 @@ AtomicCmpXchgInst::create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
   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,
@@ -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());
 }
@@ -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))
@@ -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;
 }
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 3e3db4fe81c8a5..26eed0175dbae9 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -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().
@@ -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().
@@ -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().
@@ -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().
@@ -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().
@@ -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().
@@ -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().
@@ -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().
@@ -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().
@@ -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
   }
@@ -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().
@@ -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().
@@ -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");



More information about the llvm-commits mailing list