diff --git a/llvm/include/llvm/Transforms/Utils/SCCPSolver.h b/llvm/include/llvm/Transforms/Utils/SCCPSolver.h --- a/llvm/include/llvm/Transforms/Utils/SCCPSolver.h +++ b/llvm/include/llvm/Transforms/Utils/SCCPSolver.h @@ -51,6 +51,8 @@ ~SCCPSolver(); + std::function getTLI; + void addAnalysis(Function &F, AnalysisResultsForFn A); /// markBlockExecutable - This method can be used by clients to mark all of diff --git a/llvm/lib/Transforms/Scalar/SCCP.cpp b/llvm/lib/Transforms/Scalar/SCCP.cpp --- a/llvm/lib/Transforms/Scalar/SCCP.cpp +++ b/llvm/lib/Transforms/Scalar/SCCP.cpp @@ -97,6 +97,18 @@ return !LV.isUnknownOrUndef() && !isConstant(LV); } +static bool canSCCPRemoveInstruction(Instruction *I, + const TargetLibraryInfo *TLI) { + if (wouldInstructionBeTriviallyDead(I, TLI)) + return true; + + // Defer to wouldInstructionBeTriviallyDead() for constrained fp. + if (isa(I)) + return false; + + return (!isa(I) || !I->mayHaveSideEffects()) && !I->isTerminator(); +} + static bool tryToReplaceWithConstant(SCCPSolver &Solver, Value *V) { Constant *Const = nullptr; if (V->getType()->isStructTy()) { @@ -127,7 +139,11 @@ // Calls with "clang.arc.attachedcall" implicitly use the return value and // those uses cannot be updated with a constant. CallBase *CB = dyn_cast(V); - if (CB && ((CB->isMustTailCall() && !CB->isSafeToRemove()) || + Function *CBF = nullptr; + if (CB) + CBF = CB->getFunction(); + if (CB && ((CB->isMustTailCall() && + !canSCCPRemoveInstruction(CB, &Solver.getTLI(*CBF))) || CB->getOperandBundle(LLVMContext::OB_clang_arc_attachedcall))) { Function *F = CB->getCalledFunction(); @@ -156,7 +172,7 @@ if (Inst.getType()->isVoidTy()) continue; if (tryToReplaceWithConstant(Solver, &Inst)) { - if (Inst.isSafeToRemove()) + if (canSCCPRemoveInstruction(&Inst, &Solver.getTLI(*BB.getParent()))) Inst.eraseFromParent(); MadeChanges = true; diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp --- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp +++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp @@ -1616,7 +1616,8 @@ const DataLayout &DL, std::function GetTLI, LLVMContext &Ctx) - : Visitor(new SCCPInstVisitor(DL, std::move(GetTLI), Ctx)) {} + : Visitor(new SCCPInstVisitor(DL, std::move(GetTLI), Ctx)), getTLI(GetTLI) { +} SCCPSolver::~SCCPSolver() {}