diff --git a/llvm/include/llvm/SandboxIR/Instruction.h b/llvm/include/llvm/SandboxIR/Instruction.h index 036882d8b1399..d9642365908d2 100644 --- a/llvm/include/llvm/SandboxIR/Instruction.h +++ b/llvm/include/llvm/SandboxIR/Instruction.h @@ -2475,11 +2475,11 @@ class CmpInst : public SingleLLVMInstructionImpl { using Predicate = llvm::CmpInst::Predicate; static CmpInst *create(Predicate Pred, Value *S1, Value *S2, - Instruction *InsertBefore, Context &Ctx, + InsertPosition Pos, Context &Ctx, const Twine &Name = ""); static CmpInst *createWithCopiedFlags(Predicate Pred, Value *S1, Value *S2, const Instruction *FlagsSource, - Instruction *InsertBefore, Context &Ctx, + InsertPosition Pos, Context &Ctx, const Twine &Name = ""); void setPredicate(Predicate P); void swapOperands(); diff --git a/llvm/lib/SandboxIR/Context.cpp b/llvm/lib/SandboxIR/Context.cpp index f5b3d2733344f..486e935bc35fb 100644 --- a/llvm/lib/SandboxIR/Context.cpp +++ b/llvm/lib/SandboxIR/Context.cpp @@ -595,72 +595,6 @@ FCmpInst *Context::createFCmpInst(llvm::FCmpInst *I) { auto NewPtr = std::unique_ptr(new FCmpInst(I, *this)); return cast(registerValue(std::move(NewPtr))); } -CmpInst *CmpInst::create(Predicate P, Value *S1, Value *S2, - Instruction *InsertBefore, Context &Ctx, - const Twine &Name) { - auto &Builder = Ctx.getLLVMIRBuilder(); - Builder.SetInsertPoint(InsertBefore->getTopmostLLVMInstruction()); - auto *LLVMI = Builder.CreateCmp(P, S1->Val, S2->Val, Name); - if (dyn_cast(LLVMI)) - return Ctx.createICmpInst(cast(LLVMI)); - return Ctx.createFCmpInst(cast(LLVMI)); -} -CmpInst *CmpInst::createWithCopiedFlags(Predicate P, Value *S1, Value *S2, - const Instruction *F, - Instruction *InsertBefore, Context &Ctx, - const Twine &Name) { - CmpInst *Inst = create(P, S1, S2, InsertBefore, Ctx, Name); - cast(Inst->Val)->copyIRFlags(F->Val); - return Inst; -} - -Type *CmpInst::makeCmpResultType(Type *OpndType) { - if (auto *VT = dyn_cast(OpndType)) { - // TODO: Cleanup when we have more complete support for - // sandboxir::VectorType - return OpndType->getContext().getType(llvm::VectorType::get( - llvm::Type::getInt1Ty(OpndType->getContext().LLVMCtx), - cast(VT->LLVMTy)->getElementCount())); - } - return Type::getInt1Ty(OpndType->getContext()); -} - -void CmpInst::setPredicate(Predicate P) { - Ctx.getTracker() - .emplaceIfTracking< - GenericSetter<&CmpInst::getPredicate, &CmpInst::setPredicate>>(this); - cast(Val)->setPredicate(P); -} - -void CmpInst::swapOperands() { - if (ICmpInst *IC = dyn_cast(this)) - IC->swapOperands(); - else - cast(this)->swapOperands(); -} - -void ICmpInst::swapOperands() { - Ctx.getTracker().emplaceIfTracking(this); - cast(Val)->swapOperands(); -} - -void FCmpInst::swapOperands() { - Ctx.getTracker().emplaceIfTracking(this); - cast(Val)->swapOperands(); -} - -#ifndef NDEBUG -void CmpInst::dumpOS(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void CmpInst::dump() const { - dumpOS(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - Value *Context::getValue(llvm::Value *V) const { auto It = LLVMValueToValueMap.find(V); if (It != LLVMValueToValueMap.end()) diff --git a/llvm/lib/SandboxIR/Instruction.cpp b/llvm/lib/SandboxIR/Instruction.cpp index 6bf58c71a7c9f..d80d10370e32d 100644 --- a/llvm/lib/SandboxIR/Instruction.cpp +++ b/llvm/lib/SandboxIR/Instruction.cpp @@ -927,6 +927,70 @@ void PHINode::removeIncomingValueIf(function_ref Predicate) { } } +CmpInst *CmpInst::create(Predicate P, Value *S1, Value *S2, InsertPosition Pos, + Context &Ctx, const Twine &Name) { + auto &Builder = setInsertPos(Pos); + auto *LLVMI = Builder.CreateCmp(P, S1->Val, S2->Val, Name); + if (dyn_cast(LLVMI)) + return Ctx.createICmpInst(cast(LLVMI)); + return Ctx.createFCmpInst(cast(LLVMI)); +} +CmpInst *CmpInst::createWithCopiedFlags(Predicate P, Value *S1, Value *S2, + const Instruction *F, + InsertPosition Pos, Context &Ctx, + const Twine &Name) { + CmpInst *Inst = create(P, S1, S2, Pos, Ctx, Name); + cast(Inst->Val)->copyIRFlags(F->Val); + return Inst; +} + +Type *CmpInst::makeCmpResultType(Type *OpndType) { + if (auto *VT = dyn_cast(OpndType)) { + // TODO: Cleanup when we have more complete support for + // sandboxir::VectorType + return OpndType->getContext().getType(llvm::VectorType::get( + llvm::Type::getInt1Ty(OpndType->getContext().LLVMCtx), + cast(VT->LLVMTy)->getElementCount())); + } + return Type::getInt1Ty(OpndType->getContext()); +} + +void CmpInst::setPredicate(Predicate P) { + Ctx.getTracker() + .emplaceIfTracking< + GenericSetter<&CmpInst::getPredicate, &CmpInst::setPredicate>>(this); + cast(Val)->setPredicate(P); +} + +void CmpInst::swapOperands() { + if (ICmpInst *IC = dyn_cast(this)) + IC->swapOperands(); + else + cast(this)->swapOperands(); +} + +void ICmpInst::swapOperands() { + Ctx.getTracker().emplaceIfTracking(this); + cast(Val)->swapOperands(); +} + +void FCmpInst::swapOperands() { + Ctx.getTracker().emplaceIfTracking(this); + cast(Val)->swapOperands(); +} + +#ifndef NDEBUG +void CmpInst::dumpOS(raw_ostream &OS) const { + dumpCommonPrefix(OS); + dumpCommonSuffix(OS); +} + +void CmpInst::dump() const { + dumpOS(dbgs()); + dbgs() << "\n"; +} +#endif // NDEBUG + static llvm::Instruction::CastOps getLLVMCastOp(Instruction::Opcode Opc) { switch (Opc) { case Instruction::Opcode::ZExt: diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp index 26eed0175dbae..97113b303f72e 100644 --- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp +++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp @@ -5843,7 +5843,7 @@ define void @foo(i32 %i0, i32 %i1) { } auto *NewCmp = sandboxir::CmpInst::create(llvm::CmpInst::ICMP_ULE, F.getArg(0), - F.getArg(1), &*BB->begin(), Ctx, "NewCmp"); + F.getArg(1), BB->begin(), Ctx, "NewCmp"); EXPECT_EQ(NewCmp, &*BB->begin()); EXPECT_EQ(NewCmp->getPredicate(), llvm::CmpInst::ICMP_ULE); EXPECT_EQ(NewCmp->getOperand(0), F.getArg(0)); @@ -5907,7 +5907,7 @@ define void @foo(float %f0, float %f1) { // create with default flags auto *NewFCmp = sandboxir::CmpInst::create( - llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), &*It1, Ctx, "NewFCmp"); + llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), It1, Ctx, "NewFCmp"); EXPECT_EQ(NewFCmp->getPredicate(), llvm::CmpInst::FCMP_ONE); EXPECT_EQ(NewFCmp->getOperand(0), F.getArg(0)); EXPECT_EQ(NewFCmp->getOperand(1), F.getArg(1)); @@ -5918,7 +5918,7 @@ define void @foo(float %f0, float %f1) { EXPECT_TRUE(CopyFrom->getFastMathFlags() != DefaultFMF); // create with copied flags auto *NewFCmpFlags = sandboxir::CmpInst::createWithCopiedFlags( - llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), CopyFrom, &*It1, Ctx, + llvm::CmpInst::FCMP_ONE, F.getArg(0), F.getArg(1), CopyFrom, It1, Ctx, "NewFCmpFlags"); EXPECT_FALSE(NewFCmpFlags->getFastMathFlags() != CopyFrom->getFastMathFlags());