diff --git a/llvm/include/llvm/SandboxIR/Instruction.h b/llvm/include/llvm/SandboxIR/Instruction.h index 82356f282e141..036882d8b1399 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 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 { 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 4fe1f54d3ec62..6bf58c71a7c9f 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(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(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(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(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(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(NewV)) @@ -1413,20 +1361,6 @@ Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand, return Ctx.getOrCreateConstant(cast(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 3e3db4fe81c8a..26eed0175dbae 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::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::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::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::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::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::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::create( - Ti64, sandboxir::Instruction::Opcode::SExt, Arg, /*WhereIt=*/BB->end(), - /*WhereBB=*/BB, Ctx, "SExt")); + auto *NewI = cast( + 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::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::create(Ti64, sandboxir::Instruction::Opcode::ZExt, - Arg, /*InsertAtEnd=*/BB, Ctx, "ZExt")); + auto *NewI = cast(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::create(Arg, DstTy, /*WhereIt=*/BB->end(), - /*WhereBB=*/BB, Ctx, "NewCI")); + cast(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::create(Arg, DstTy, - /*InsertBefore=*/Ret, Ctx, "NewCI")); + auto *NewI = cast( + 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::create(PHI->getType(), 0, Br, Ctx, "NewPHI")); + auto *NewPHI = cast(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");