Index: include/llvm/InitializePasses.h =================================================================== --- include/llvm/InitializePasses.h +++ include/llvm/InitializePasses.h @@ -92,6 +92,7 @@ void initializeCallGraphViewerPass(PassRegistry&); void initializeCallGraphWrapperPassPass(PassRegistry &); void initializeCodeGenPreparePass(PassRegistry&); +void initializeCondCallDCELegacyPassPass(PassRegistry&); void initializeCountingFunctionInserterPass(PassRegistry&); void initializeConstantHoistingLegacyPassPass(PassRegistry&); void initializeConstantMergeLegacyPassPass(PassRegistry &); Index: include/llvm/LinkAllPasses.h =================================================================== --- include/llvm/LinkAllPasses.h +++ include/llvm/LinkAllPasses.h @@ -78,6 +78,7 @@ (void) llvm::createCFLAndersAAWrapperPass(); (void) llvm::createCFLSteensAAWrapperPass(); (void) llvm::createStructurizeCFGPass(); + (void) llvm::createCondCallDCEPass(); (void) llvm::createConstantMergePass(); (void) llvm::createConstantPropagationPass(); (void) llvm::createCostModelAnalysisPass(); Index: include/llvm/Transforms/Scalar.h =================================================================== --- include/llvm/Transforms/Scalar.h +++ include/llvm/Transforms/Scalar.h @@ -518,6 +518,11 @@ ///===---------------------------------------------------------------------===// ModulePass *createNameAnonGlobalPass(); +//===----------------------------------------------------------------------===// +// +// CondCallDCE - Shrink-wraps a call to function if the result is not used. +// +FunctionPass *createCondCallDCEPass(bool OptimizeForSize = false); } // End llvm namespace #endif Index: include/llvm/Transforms/Utils/CondDeadCallElimination.h =================================================================== --- /dev/null +++ include/llvm/Transforms/Utils/CondDeadCallElimination.h @@ -0,0 +1,32 @@ +//===- CondDeadCallElimination.h - Conditionally Eliminate Dead Library Calls --===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_UTILS_CONDDEADCALLELIMINATION_H +#define LLVM_TRANSFORMS_UTILS_CONDDEADCALLELIMINATION_H + +#include "llvm/IR/PassManager.h" + +namespace llvm { + +class CondCallDCEPass : public PassInfoMixin { +public: + static StringRef name() { return "CondCallDCEPass"; } + + PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM); + CondCallDCEPass(bool OptimizeForSize = false) + : OptimizeForSize(OptimizeForSize) {} + +private: + bool OptimizeForSize; +}; +} // end namespace llvm + +#endif // LLVM_TRANSFORMS_UTILS_CONDDEADcALLELIMINATION_H Index: lib/Passes/PassBuilder.cpp =================================================================== --- lib/Passes/PassBuilder.cpp +++ lib/Passes/PassBuilder.cpp @@ -120,6 +120,7 @@ #include "llvm/Transforms/Scalar/TailRecursionElimination.h" #include "llvm/Transforms/Utils/AddDiscriminators.h" #include "llvm/Transforms/Utils/BreakCriticalEdges.h" +#include "llvm/Transforms/Utils/CondDeadCallElimination.h" #include "llvm/Transforms/Utils/LCSSA.h" #include "llvm/Transforms/Utils/LoopSimplify.h" #include "llvm/Transforms/Utils/LowerInvoke.h" Index: lib/Passes/PassRegistry.def =================================================================== --- lib/Passes/PassRegistry.def +++ lib/Passes/PassRegistry.def @@ -135,6 +135,7 @@ FUNCTION_PASS("alignment-from-assumptions", AlignmentFromAssumptionsPass()) FUNCTION_PASS("bdce", BDCEPass()) FUNCTION_PASS("break-crit-edges", BreakCriticalEdgesPass()) +FUNCTION_PASS("cond-call-dce", CondCallDCEPass()) FUNCTION_PASS("consthoist", ConstantHoistingPass()) FUNCTION_PASS("correlated-propagation", CorrelatedValuePropagationPass()) FUNCTION_PASS("dce", DCEPass()) Index: lib/Transforms/IPO/PassManagerBuilder.cpp =================================================================== --- lib/Transforms/IPO/PassManagerBuilder.cpp +++ lib/Transforms/IPO/PassManagerBuilder.cpp @@ -298,6 +298,7 @@ MPM.add(createCFGSimplificationPass()); // Merge & remove BBs // Combine silly seq's addInstructionCombiningPass(MPM); + MPM.add(createCondCallDCEPass(SizeLevel > 0)); addExtensionsToPM(EP_Peephole, MPM); MPM.add(createTailCallEliminationPass()); // Eliminate tail calls Index: lib/Transforms/Utils/CMakeLists.txt =================================================================== --- lib/Transforms/Utils/CMakeLists.txt +++ lib/Transforms/Utils/CMakeLists.txt @@ -9,6 +9,7 @@ CloneModule.cpp CmpInstAnalysis.cpp CodeExtractor.cpp + CondDeadCallElimination.cpp CtorUtils.cpp DemoteRegToStack.cpp Evaluator.cpp Index: lib/Transforms/Utils/CondDeadCallElimination.cpp =================================================================== --- /dev/null +++ lib/Transforms/Utils/CondDeadCallElimination.cpp @@ -0,0 +1,569 @@ +//===-- CondDeadCallElimination.cpp -----------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This pass shrink-wraps a call to function if the result is not used. +// The call can set errno but is otherwise side effect free. For example: +// sqrt(val); +// is transformed to +// if (val < 0) +// sqrt(val); +// Even if the result of library call is not being used, the compiler cannot +// safely delete the call because the function can set errno on error +// conditions. +// Note in many functions, the error condition solely depends on the incoming +// parameter. In this optimization, we can generate the condition can lead to +// the errno to shrink-wrap the call. Since the chances of hitting the error +// condition is low, the runtime call is effectively eliminated. +// +// These partially dead calls are usually results of C++ abstraction penalty +// exposed by inlining. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Transforms/Utils/CondDeadCallElimination.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/Statistic.h" +#include "llvm/Analysis/TargetLibraryInfo.h" +#include "llvm/IR/CFG.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/InstVisitor.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/MDBuilder.h" +#include "llvm/Pass.h" +#include "llvm/Transforms/Utils/BasicBlockUtils.h" +using namespace llvm; + +#define DEBUG_TYPE "cond-call-dce" + +STATISTIC(NumWrappedOneCond, "Number of One-Condition Wrappers Inserted"); +STATISTIC(NumWrappedTwoCond, "Number of Two-Condition Wrappers Inserted"); + +static cl::opt CondCallDCEDisable( + "cond-call-dce-disable", cl::init(false), cl::Hidden, + cl::desc("Disable conditional call Dead Code Elimination")); +static cl::opt CondCallDCEDoDomainError( + "cond-call-dce-domain-error", cl::init(true), cl::Hidden, + cl::desc("Perform conditional call DCE on libs call with domain errors")); +static cl::opt CondCallDCEDoRangeError( + "cond-call-dce-range-error", cl::init(true), cl::Hidden, + cl::desc("Perform conditional call DCE on libs call with range errors")); +static cl::opt CondCallDCEDoPoleError( + "cond-call-dce-pole-error", cl::init(true), cl::Hidden, + cl::desc("Perform conditional call DCE on libs call with pole errors")); + +namespace { +class CondCallDCELegacyPass : public FunctionPass { +public: + static char ID; // Pass identification, replacement for typeid + explicit CondCallDCELegacyPass(bool OptForSize = false) + : FunctionPass(ID), OptimizeForSize(OptForSize) { + initializeCondCallDCELegacyPassPass(*PassRegistry::getPassRegistry()); + } + void getAnalysisUsage(AnalysisUsage &AU) const override; + bool runOnFunction(Function &F) override; + +private: + bool OptimizeForSize; +}; +} + +char CondCallDCELegacyPass::ID = 0; +INITIALIZE_PASS_BEGIN(CondCallDCELegacyPass, "cond-call-dce", + "Conditionally eliminate dead library calls", false, + false) +INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) +INITIALIZE_PASS_END(CondCallDCELegacyPass, "cond-call-dce", + "Conditionally eliminate dead library calls", false, false) + +class CondCallDCE : public InstVisitor { +public: + CondCallDCE(const TargetLibraryInfo &TLI) : TLI(TLI), Changed(false){}; + bool isChanged() const { return Changed; } + void visitCallInst(CallInst &CI) { checkCandidate(CI); } + void perform() { + for (auto &CI : WorkList) { + DEBUG(dbgs() << "CDCE calls: " << CI->getCalledFunction()->getName() + << "\n"); + if (perform(CI)) { + Changed = true; + DEBUG(dbgs() << "Transformed\n"); + } + } + } + +private: + bool perform(CallInst *CI); + void checkCandidate(CallInst &CI); + void shrinkWrapCI(CallInst *CI, Value *Cond); + bool performCallNoErrNo(CallInst *CI, const LibFunc::Func &Func); + bool performCallDomainErrorOnly(CallInst *CI, const LibFunc::Func &Func); + bool performCallErrors(CallInst *CI, const LibFunc::Func &Func); + bool performCallRangeErrorOnly(CallInst *CI, const LibFunc::Func &Func); + Value *generateOneRangeCond(CallInst *CI, const LibFunc::Func &Func); + Value *generateTwoRangeCond(CallInst *CI, const LibFunc::Func &Func); + Value *generateCondForPow(CallInst *CI, const LibFunc::Func &Func); + + // Create an OR of two conditions. + Value *createOrCond(CallInst *CI, CmpInst::Predicate Cmp, float Val, + CmpInst::Predicate Cmp2, float Val2) { + IRBuilder<> BBBuilder(CI); + Value *Arg = CI->getArgOperand(0); + auto Cond2 = createCond(BBBuilder, Arg, Cmp2, Val2); + auto Cond1 = createCond(BBBuilder, Arg, Cmp, Val); + return BBBuilder.CreateOr(Cond1, Cond2); + } + + // Create a single condition using IRBuilder. + Value *createCond(IRBuilder<> &BBBuilder, Value *Arg, CmpInst::Predicate Cmp, + float Val) { + Constant *V = ConstantFP::get(BBBuilder.getContext(), APFloat(Val)); + if (!Arg->getType()->isFloatTy()) + V = ConstantExpr::getFPExtend(V, Arg->getType()); + return BBBuilder.CreateFCmp(Cmp, Arg, V); + } + + // Create a single condition. + Value *createCond(CallInst *CI, CmpInst::Predicate Cmp, float Val) { + IRBuilder<> BBBuilder(CI); + Value *Arg = CI->getArgOperand(0); + return createCond(BBBuilder, Arg, Cmp, Val); + } + + const TargetLibraryInfo &TLI; + SmallVector WorkList; + bool Changed; +}; + +// Perform the transformation to calls with errno set by domain error. +bool CondCallDCE::performCallDomainErrorOnly(CallInst *CI, + const LibFunc::Func &Func) { + Value *Cond = nullptr; + + switch (Func) { + case LibFunc::acos: // DomainError: (x < -1 || x > 1) + case LibFunc::acosf: // Same as acos + case LibFunc::acosl: // Same as acos + case LibFunc::asin: // DomainError: (x < -1 || x > 1) + case LibFunc::asinf: // Same as asin + case LibFunc::asinl: // Same as asin + { + ++NumWrappedTwoCond; + Cond = createOrCond(CI, CmpInst::FCMP_OLT, -1.0f, CmpInst::FCMP_OGT, 1.0f); + break; + } + case LibFunc::cos: // DomainError: (x == +inf || x == -inf) + case LibFunc::cosf: // Same as cos + case LibFunc::cosl: // Same as cos + case LibFunc::sin: // DomainError: (x == +inf || x == -inf) + case LibFunc::sinf: // Same as sin + case LibFunc::sinl: // Same as sin + { + ++NumWrappedTwoCond; + Cond = createOrCond(CI, CmpInst::FCMP_OEQ, INFINITY, CmpInst::FCMP_OEQ, + -INFINITY); + break; + } + case LibFunc::acosh: // DomainError: (x < 1) + case LibFunc::acoshf: // Same as acosh + case LibFunc::acoshl: // Same as acosh + { + ++NumWrappedOneCond; + Cond = createCond(CI, CmpInst::FCMP_OLT, 1.0f); + break; + } + case LibFunc::sqrt: // DomainError: (x < 0) + case LibFunc::sqrtf: // Same as sqrt + case LibFunc::sqrtl: // Same as sqrt + { + ++NumWrappedOneCond; + Cond = createCond(CI, CmpInst::FCMP_OLT, 0.0f); + break; + } + default: + return false; + } + assert(Cond != nullptr); + shrinkWrapCI(CI, Cond); + return true; +} + +// Perform the transformation to calls with errno set by range error. +bool CondCallDCE::performCallRangeErrorOnly(CallInst *CI, + const LibFunc::Func &Func) { + Value *Cond = nullptr; + + switch (Func) { + case LibFunc::cosh: + case LibFunc::coshf: + case LibFunc::coshl: + case LibFunc::exp: + case LibFunc::expf: + case LibFunc::expl: + case LibFunc::exp10: + case LibFunc::exp10f: + case LibFunc::exp10l: + case LibFunc::exp2: + case LibFunc::exp2f: + case LibFunc::exp2l: + case LibFunc::sinh: + case LibFunc::sinhf: + case LibFunc::sinhl: { + Cond = generateTwoRangeCond(CI, Func); + break; + } + case LibFunc::expm1: // RangeError: (709, inf) + case LibFunc::expm1f: // RangeError: (88, inf) + case LibFunc::expm1l: // RangeError: (11356, inf) + { + Cond = generateOneRangeCond(CI, Func); + break; + } + default: + return false; + } + assert(Cond != nullptr); + shrinkWrapCI(CI, Cond); + return true; +} + +// Perform the transformation to calls with errno set by combination of errors. +bool CondCallDCE::performCallErrors(CallInst *CI, const LibFunc::Func &Func) { + Value *Cond = nullptr; + + switch (Func) { + case LibFunc::atanh: // DomainError: (x < -1 || x > 1) + // PoleError: (x == -1 || x == 1) + // Overall Cond: (x <= -1 || x >= 1) + case LibFunc::atanhf: // Same as atanh + case LibFunc::atanhl: // Same as atanh + { + if (!CondCallDCEDoDomainError || !CondCallDCEDoPoleError) + return false; + ++NumWrappedTwoCond; + Cond = createOrCond(CI, CmpInst::FCMP_OLE, -1.0f, CmpInst::FCMP_OGE, 1.0f); + break; + } + case LibFunc::log: // DomainError: (x < 0) + // PoleError: (x == 0) + // Overall Cond: (x <= 0) + case LibFunc::logf: // Same as log + case LibFunc::logl: // Same as log + case LibFunc::log10: // Same as log + case LibFunc::log10f: // Same as log + case LibFunc::log10l: // Same as log + case LibFunc::log2: // Same as log + case LibFunc::log2f: // Same as log + case LibFunc::log2l: // Same as log + case LibFunc::logb: // Same as log + case LibFunc::logbf: // Same as log + case LibFunc::logbl: // Same as log + { + if (!CondCallDCEDoDomainError || !CondCallDCEDoPoleError) + return false; + ++NumWrappedOneCond; + Cond = createCond(CI, CmpInst::FCMP_OLE, 0.0f); + break; + } + case LibFunc::log1p: // DomainError: (x < -1) + // PoleError: (x == -1) + // Overall Cond: (x <= -1) + case LibFunc::log1pf: // Same as log1p + case LibFunc::log1pl: // Same as log1p + { + if (!CondCallDCEDoDomainError || !CondCallDCEDoPoleError) + return false; + ++NumWrappedOneCond; + Cond = createCond(CI, CmpInst::FCMP_OLE, -1.0f); + break; + } + case LibFunc::pow: // DomainError: x < 0 and y is noninteger + // PoleError: x == 0 and y < 0 + // RangeError: overflow or underflow + case LibFunc::powf: + case LibFunc::powl: { + if (!CondCallDCEDoDomainError || !CondCallDCEDoPoleError || + !CondCallDCEDoRangeError) + return false; + Cond = generateCondForPow(CI, Func); + if (Cond == nullptr) + return false; + break; + } + default: + return false; + } + assert(Cond != nullptr); + shrinkWrapCI(CI, Cond); + return true; +} + +// Checks if CI is a candidate for shrinkwrapping and put it into work list if +// true. +void CondCallDCE::checkCandidate(CallInst &CI) { + if (CI.isNoBuiltin()) + return; + // A possible improvement is to handle the calls with return valued being + // used, if there is API for fast libcall implementation (without setting + // errno). We can use the same framework to direct calls that known + // errno-free to the new API calls. + if (!CI.use_empty()) + return; + + LibFunc::Func Func; + Function *Callee = CI.getCalledFunction(); + if (!Callee) + return; + if (!TLI.getLibFunc(*Callee, Func) || !TLI.has(Func)) + return; + if (CI.getCallingConv() != llvm::CallingConv::C) + return; + WorkList.push_back(&CI); +} + +// Generate the upper bound condition for RangeError. +Value *CondCallDCE::generateOneRangeCond(CallInst *CI, + const LibFunc::Func &Func) { + float UpperBound; + switch (Func) { + case LibFunc::expm1: // RangeError: (709, inf) + UpperBound = 709.0f; + break; + case LibFunc::expm1f: // RangeError: (88, inf) + UpperBound = 88.0f; + break; + case LibFunc::expm1l: // RangeError: (11356, inf) + UpperBound = 11356.0f; + break; + default: + llvm_unreachable("Should be reach here"); + } + + ++NumWrappedOneCond; + return createCond(CI, CmpInst::FCMP_OGT, UpperBound); +} + +// Generate the lower and upper bound condition for RangeError. +Value *CondCallDCE::generateTwoRangeCond(CallInst *CI, + const LibFunc::Func &Func) { + float UpperBound, LowerBound; + switch (Func) { + case LibFunc::cosh: // RangeError: (x < -710 || x > 710) + case LibFunc::sinh: // Same as cosh + LowerBound = -710.0f; + UpperBound = 710.0f; + break; + case LibFunc::coshf: // RangeError: (x < -89 || x > 89) + case LibFunc::sinhf: // Same as coshf + LowerBound = -89.0f; + UpperBound = 89.0f; + break; + case LibFunc::coshl: // RangeError: (x < -11357 || x > 11357) + case LibFunc::sinhl: // Same as coshl + LowerBound = -11357.0f; + UpperBound = 11357.0f; + break; + case LibFunc::exp: // RangeError: (x < -745 || x > 709) + LowerBound = -745.0f; + UpperBound = 709.0f; + break; + case LibFunc::expf: // RangeError: (x < -103 || x > 88) + LowerBound = -103.0f; + UpperBound = 88.0f; + break; + case LibFunc::expl: // RangeError: (x < -11399 || x > 11356) + LowerBound = -11399.0f; + UpperBound = 11356.0f; + break; + case LibFunc::exp10: // RangeError: (x < -323 || x > 308) + LowerBound = -323.0f; + UpperBound = 308.0f; + break; + case LibFunc::exp10f: // RangeError: (x < -45 || x > 38) + LowerBound = -45.0f; + UpperBound = 38.0f; + break; + case LibFunc::exp10l: // RangeError: (x < -4950 || x > 4932) + LowerBound = -4950.0f; + UpperBound = 4932.0f; + break; + case LibFunc::exp2: // RangeError: (x < -1074 || x > 1023) + LowerBound = -1074.0f; + UpperBound = 1023.0f; + break; + case LibFunc::exp2f: // RangeError: (x < -149 || x > 127) + LowerBound = -149.0f; + UpperBound = 127.0f; + break; + case LibFunc::exp2l: // RangeError: (x < -16445 || x > 11383) + LowerBound = -16445.0f; + UpperBound = 11383.0f; + break; + default: + llvm_unreachable("Should be reach here"); + } + + ++NumWrappedTwoCond; + return createOrCond(CI, CmpInst::FCMP_OGT, UpperBound, CmpInst::FCMP_OLT, + LowerBound); +} + +// For pow(x,y), We only handle the following cases: +// (1) x is a constant && (x >= 1) && (x < MaxUInt8) +// Cond is: (y > 127) +// (2) x is a value coming from an integer type. +// (2.1) if x's bit_size == 8 +// Cond: (x <= 0 || y > 128) +// (2.2) if x's bit_size is 16 +// Cond: (x <= 0 || y > 64) +// (2.3) if x's bit_size is 32 +// Cond: (x <= 0 || y > 32) +// Support for powl(x,y) and powf(x,y) are TBD. +// +// Note that condition can be more conservative than the actual condition +// (i.e. we might invoke the calls that will not set the errno.). +// +Value *CondCallDCE::generateCondForPow(CallInst *CI, + const LibFunc::Func &Func) { + // FIXME: LibFunc::powf and powl TBD. + if (Func != LibFunc::pow) { + DEBUG(dbgs() << "Not handled powf() and powl()\n"); + return nullptr; + } + + Value *Base = CI->getArgOperand(0); + Value *Exp = CI->getArgOperand(1); + IRBuilder<> BBBuilder(CI); + + // Constant Base case. + if (ConstantFP *CF = dyn_cast(Base)) { + double D = CF->getValueAPF().convertToDouble(); + if (D < 1.0f || D > APInt::getMaxValue(8).getZExtValue()) { + DEBUG(dbgs() << "Not handled pow(): constant base out of range\n"); + return nullptr; + } + + ++NumWrappedOneCond; + Constant *V = ConstantFP::get(CI->getContext(), APFloat(127.0f)); + if (!Exp->getType()->isFloatTy()) + V = ConstantExpr::getFPExtend(V, Exp->getType()); + return BBBuilder.CreateFCmp(CmpInst::FCMP_OGT, Exp, V); + } + + // If the Base value coming from an integer type. + Instruction *I = dyn_cast(Base); + if (!I) { + DEBUG(dbgs() << "Not handled pow(): FP type base\n"); + return nullptr; + } + unsigned Opcode = I->getOpcode(); + if (Opcode == Instruction::UIToFP || Opcode == Instruction::SIToFP) { + unsigned BW = I->getOperand(0)->getType()->getPrimitiveSizeInBits(); + float UpperV = 0.0f; + if (BW == 8) + UpperV = 128.0f; + else if (BW == 16) + UpperV = 64.0f; + else if (BW == 32) + UpperV = 32.0f; + else { + DEBUG(dbgs() << "Not handled pow(): type too wide\n"); + return nullptr; + } + + ++NumWrappedTwoCond; + Constant *V = ConstantFP::get(CI->getContext(), APFloat(UpperV)); + Constant *V0 = ConstantFP::get(CI->getContext(), APFloat(0.0f)); + if (!Exp->getType()->isFloatTy()) + V = ConstantExpr::getFPExtend(V, Exp->getType()); + if (!Base->getType()->isFloatTy()) + V0 = ConstantExpr::getFPExtend(V0, Exp->getType()); + + Value *Cond = BBBuilder.CreateFCmp(CmpInst::FCMP_OGT, Exp, V); + Value *Cond0 = BBBuilder.CreateFCmp(CmpInst::FCMP_OLE, Base, V0); + return BBBuilder.CreateOr(Cond0, Cond); + } + DEBUG(dbgs() << "Not handled pow(): base not from integer convert\n"); + return nullptr; +} + +// Wrap conditions that can potentially generate errno to the library call. +void CondCallDCE::shrinkWrapCI(CallInst *CI, Value *Cond) { + MDNode *BranchWeights = + MDBuilder(CI->getContext()).createBranchWeights(1, 2000); + TerminatorInst *NewInst = + SplitBlockAndInsertIfThen(Cond, CI, false, BranchWeights); + BasicBlock *CallBB = NewInst->getParent(); + CallBB->setName("cdce.call"); + CallBB->getSingleSuccessor()->setName("cdce.end"); + CI->removeFromParent(); + CallBB->getInstList().insert(CallBB->getFirstInsertionPt(), CI); + DEBUG(dbgs() << "== Basic Block After =="); + DEBUG(dbgs() << *CallBB->getSinglePredecessor() << *CallBB + << *CallBB->getSingleSuccessor() << "\n"); +} + +// Perform the transformation to a single candidate. +bool CondCallDCE::perform(CallInst *CI) { + LibFunc::Func Func; + Function *Callee = CI->getCalledFunction(); + assert(Callee); + TLI.getLibFunc(*Callee, Func); + assert(Func); + + if (CondCallDCEDoDomainError && performCallDomainErrorOnly(CI, Func)) + return true; + + if (CondCallDCEDoRangeError && performCallRangeErrorOnly(CI, Func)) + return true; + + return performCallErrors(CI, Func); +} + +void CondCallDCELegacyPass::getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesCFG(); + AU.addRequired(); +} + +bool runImpl(Function &F, const TargetLibraryInfo &TLI, + bool OptForSize = false) { + if (CondCallDCEDisable) + return false; + if (OptForSize || F.hasFnAttribute(Attribute::OptimizeForSize)) + return false; + CondCallDCE CCDCE(TLI); + CCDCE.visit(F); + CCDCE.perform(); + return CCDCE.isChanged(); +} + +bool CondCallDCELegacyPass::runOnFunction(Function &F) { + auto &TLI = getAnalysis().getTLI(); + return runImpl(F, TLI, OptimizeForSize); +} + +namespace llvm { +char &CondCallDCEPassID = CondCallDCELegacyPass::ID; + +// Public interface to CondCallDCE pass. +FunctionPass *createCondCallDCEPass(bool OptimizeForSize) { + return new CondCallDCELegacyPass(OptimizeForSize); +} + +PreservedAnalyses CondCallDCEPass::run(Function &F, + FunctionAnalysisManager &FAM) { + auto &TLI = FAM.getResult(F); + bool Changed = runImpl(F, TLI, OptimizeForSize); + if (!Changed) + return PreservedAnalyses::all(); + return PreservedAnalyses::none(); +} +} Index: lib/Transforms/Utils/Utils.cpp =================================================================== --- lib/Transforms/Utils/Utils.cpp +++ lib/Transforms/Utils/Utils.cpp @@ -24,6 +24,7 @@ initializeAddDiscriminatorsLegacyPassPass(Registry); initializeBreakCriticalEdgesPass(Registry); initializeInstNamerPass(Registry); + initializeCondCallDCELegacyPassPass(Registry); initializeLCSSAWrapperPassPass(Registry); initializeLoopSimplifyPass(Registry); initializeLowerInvokeLegacyPassPass(Registry); Index: test/Transforms/Util/cond-call-dce-double.ll =================================================================== --- /dev/null +++ test/Transforms/Util/cond-call-dce-double.ll @@ -0,0 +1,241 @@ +; RUN: opt < %s -cond-call-dce -S | FileCheck %s +; New PM +; RUN: opt < %s -passes=cond-call-dce -S | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @test_range_error(double %value) { +entry: + %call_0 = call double @cosh(double %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt double %value, -7.100000e+02 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt double %value, 7.100000e+02 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_0 = call double @cosh(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_1 = call double @exp(double %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt double %value, -7.450000e+02 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt double %value, 7.090000e+02 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_1 = call double @exp(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_3 = call double @exp2(double %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt double %value, -1.074000e+03 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt double %value, 1.023000e+03 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_3 = call double @exp2(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_4 = call double @sinh(double %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt double %value, -7.100000e+02 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt double %value, 7.100000e+02 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_4 = call double @sinh(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_5 = call double @expm1(double %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ogt double %value, 7.090000e+02 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_5 = call double @expm1(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + ret void +} + +declare double @cosh(double) +declare double @exp(double) +declare double @exp2(double) +declare double @sinh(double) +declare double @expm1(double) + +define void @test_domain_error(double %value) { +entry: + %call_00 = call double @acos(double %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp ogt double %value, 1.000000e+00 +; CHECK: [[COND2:%[0-9]+]] = fcmp olt double %value, -1.000000e+00 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_00 = call double @acos(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_01 = call double @asin(double %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp ogt double %value, 1.000000e+00 +; CHECK: [[COND2:%[0-9]+]] = fcmp olt double %value, -1.000000e+00 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_01 = call double @asin(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_02 = call double @cos(double %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp oeq double %value, 0xFFF0000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp oeq double %value, 0x7FF0000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_02 = call double @cos(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_03 = call double @sin(double %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp oeq double %value, 0xFFF0000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp oeq double %value, 0x7FF0000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_03 = call double @sin(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_04 = call double @acosh(double %value) +; CHECK: [[COND:%[0-9]+]] = fcmp olt double %value, 1.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_04 = call double @acosh(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_05 = call double @sqrt(double %value) +; CHECK: [[COND:%[0-9]+]] = fcmp olt double %value, 0.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_05 = call double @sqrt(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_06 = call double @atanh(double %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp oge double %value, 1.000000e+00 +; CHECK: [[COND2:%[0-9]+]] = fcmp ole double %value, -1.000000e+00 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_06 = call double @atanh(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_07 = call double @log(double %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole double %value, 0.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_07 = call double @log(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_08 = call double @log10(double %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole double %value, 0.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_08 = call double @log10(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_09 = call double @log2(double %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole double %value, 0.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_09 = call double @log2(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_10 = call double @logb(double %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole double %value, 0.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_10 = call double @logb(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_11 = call double @log1p(double %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole double %value, -1.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_11 = call double @log1p(double %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + ret void +} + +declare double @acos(double) +declare double @asin(double) +declare double @cos(double) +declare double @sin(double) +declare double @acosh(double) +declare double @sqrt(double) +declare double @atanh(double) +declare double @log(double) +declare double @log10(double) +declare double @log2(double) +declare double @logb(double) +declare double @log1p(double) + +define void @test_pow(i32 %int_val, double %exp) { + %call = call double @pow(double 2.500000e+00, double %exp) +; CHECK: [[COND:%[0-9]+]] = fcmp ogt double %exp, 1.270000e+02 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call = call double @pow(double 2.500000e+00, double %exp) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %conv = sitofp i32 %int_val to double + %call1 = call double @pow(double %conv, double %exp) +; CHECK: [[COND1:%[0-9]+]] = fcmp ogt double %exp, 3.200000e+01 +; CHECK: [[COND2:%[0-9]+]] = fcmp ole double %conv, 0.000000e+00 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call1 = call double @pow(double %conv, double %exp) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %conv2 = trunc i32 %int_val to i8 + %conv3 = uitofp i8 %conv2 to double + %call4 = call double @pow(double %conv3, double %exp) +; CHECK: [[COND1:%[0-9]+]] = fcmp ogt double %exp, 1.280000e+02 +; CHECK: [[COND2:%[0-9]+]] = fcmp ole double %conv3, 0.000000e+00 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call4 = call double @pow(double %conv3, double %exp) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + + %conv5 = trunc i32 %int_val to i16 + %conv6 = uitofp i16 %conv5 to double + %call7 = call double @pow(double %conv6, double %exp) +; CHECK: [[COND1:%[0-9]+]] = fcmp ogt double %exp, 6.400000e+01 +; CHECK: [[COND2:%[0-9]+]] = fcmp ole double %conv6, 0.000000e+00 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call7 = call double @pow(double %conv6, double %exp) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + ret void +} + +declare double @pow(double, double) + +; CHECK: ![[BRANCH_WEIGHT]] = !{!"branch_weights", i32 1, i32 2000} Index: test/Transforms/Util/cond-call-dce-float.ll =================================================================== --- /dev/null +++ test/Transforms/Util/cond-call-dce-float.ll @@ -0,0 +1,191 @@ +; RUN: opt < %s -cond-call-dce -S | FileCheck %s +; New PM +; RUN: opt < %s -passes=cond-call-dce -S | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @test_range_error(float %value) { +entry: + %call_0 = call float @coshf(float %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt float %value, -8.900000e+01 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt float %value, 8.900000e+01 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_0 = call float @coshf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_1 = call float @expf(float %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt float %value, -1.030000e+02 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt float %value, 8.800000e+01 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_1 = call float @expf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_3 = call float @exp2f(float %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt float %value, -1.490000e+02 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt float %value, 1.270000e+02 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_3 = call float @exp2f(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_4 = call float @sinhf(float %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt float %value, -8.900000e+01 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt float %value, 8.900000e+01 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_4 = call float @sinhf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_5 = call float @expm1f(float %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ogt float %value, 8.800000e+01 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_5 = call float @expm1f(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + ret void +} + +declare float @coshf(float) +declare float @expf(float) +declare float @exp2f(float) +declare float @sinhf(float) +declare float @expm1f(float) + +define void @test_domain_error(float %value) { +entry: + + %call_00 = call float @acosf(float %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp ogt float %value, 1.000000e+00 +; CHECK: [[COND2:%[0-9]+]] = fcmp olt float %value, -1.000000e+00 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_00 = call float @acosf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_01 = call float @asinf(float %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp ogt float %value, 1.000000e+00 +; CHECK: [[COND2:%[0-9]+]] = fcmp olt float %value, -1.000000e+00 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_01 = call float @asinf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_02 = call float @cosf(float %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp oeq float %value, 0xFFF0000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp oeq float %value, 0x7FF0000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_02 = call float @cosf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_03 = call float @sinf(float %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp oeq float %value, 0xFFF0000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp oeq float %value, 0x7FF0000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_03 = call float @sinf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_04 = call float @acoshf(float %value) +; CHECK: [[COND:%[0-9]+]] = fcmp olt float %value, 1.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_04 = call float @acoshf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_05 = call float @sqrtf(float %value) +; CHECK: [[COND:%[0-9]+]] = fcmp olt float %value, 0.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_05 = call float @sqrtf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_06 = call float @atanhf(float %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp oge float %value, 1.000000e+00 +; CHECK: [[COND2:%[0-9]+]] = fcmp ole float %value, -1.000000e+00 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_06 = call float @atanhf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_07 = call float @logf(float %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole float %value, 0.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_07 = call float @logf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_08 = call float @log10f(float %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole float %value, 0.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_08 = call float @log10f(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_09 = call float @log2f(float %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole float %value, 0.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_09 = call float @log2f(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_10 = call float @logbf(float %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole float %value, 0.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_10 = call float @logbf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_11 = call float @log1pf(float %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole float %value, -1.000000e+00 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_11 = call float @log1pf(float %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + ret void +} + +declare float @acosf(float) +declare float @asinf(float) +declare float @cosf(float) +declare float @sinf(float) +declare float @acoshf(float) +declare float @sqrtf(float) +declare float @atanhf(float) +declare float @logf(float) +declare float @log10f(float) +declare float @log2f(float) +declare float @logbf(float) +declare float @log1pf(float) + +; CHECK: ![[BRANCH_WEIGHT]] = !{!"branch_weights", i32 1, i32 2000} Index: test/Transforms/Util/cond-call-dce-long-double.ll =================================================================== --- /dev/null +++ test/Transforms/Util/cond-call-dce-long-double.ll @@ -0,0 +1,192 @@ +; RUN: opt < %s -cond-call-dce -S | FileCheck %s +; New PM +; RUN: opt < %s -passes=cond-call-dce -S | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @test_range_error(x86_fp80 %value) { +entry: + %call_0 = call x86_fp80 @coshl(x86_fp80 %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt x86_fp80 %value, 0xKC00CB174000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt x86_fp80 %value, 0xK400CB174000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_0 = call x86_fp80 @coshl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_1 = call x86_fp80 @expl(x86_fp80 %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt x86_fp80 %value, 0xKC00CB21C000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt x86_fp80 %value, 0xK400CB170000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_1 = call x86_fp80 @expl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_3 = call x86_fp80 @exp2l(x86_fp80 %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt x86_fp80 %value, 0xKC00D807A000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt x86_fp80 %value, 0xK400CB1DC000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_3 = call x86_fp80 @exp2l(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_4 = call x86_fp80 @sinhl(x86_fp80 %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp olt x86_fp80 %value, 0xKC00CB174000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp ogt x86_fp80 %value, 0xK400CB174000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_4 = call x86_fp80 @sinhl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_5 = call x86_fp80 @expm1l(x86_fp80 %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ogt x86_fp80 %value, 0xK400CB170000000000000 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_5 = call x86_fp80 @expm1l(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + ret void +} + +declare x86_fp80 @coshl(x86_fp80) +declare x86_fp80 @expl(x86_fp80) +declare x86_fp80 @exp10l(x86_fp80) +declare x86_fp80 @exp2l(x86_fp80) +declare x86_fp80 @sinhl(x86_fp80) +declare x86_fp80 @expm1l(x86_fp80) + +define void @test_domain_error(x86_fp80 %value) { +entry: + %call_00 = call x86_fp80 @acosl(x86_fp80 %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp ogt x86_fp80 %value, 0xK3FFF8000000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp olt x86_fp80 %value, 0xKBFFF8000000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_00 = call x86_fp80 @acosl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_01 = call x86_fp80 @asinl(x86_fp80 %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp ogt x86_fp80 %value, 0xK3FFF8000000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp olt x86_fp80 %value, 0xKBFFF8000000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_01 = call x86_fp80 @asinl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_02 = call x86_fp80 @cosl(x86_fp80 %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp oeq x86_fp80 %value, 0xKFFFF8000000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp oeq x86_fp80 %value, 0xK7FFF8000000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_02 = call x86_fp80 @cosl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_03 = call x86_fp80 @sinl(x86_fp80 %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp oeq x86_fp80 %value, 0xKFFFF8000000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp oeq x86_fp80 %value, 0xK7FFF8000000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_03 = call x86_fp80 @sinl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_04 = call x86_fp80 @acoshl(x86_fp80 %value) +; CHECK: [[COND:%[0-9]+]] = fcmp olt x86_fp80 %value, 0xK3FFF8000000000000000 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_04 = call x86_fp80 @acoshl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_05 = call x86_fp80 @sqrtl(x86_fp80 %value) +; CHECK: [[COND:%[0-9]+]] = fcmp olt x86_fp80 %value, 0xK00000000000000000000 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_05 = call x86_fp80 @sqrtl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_06 = call x86_fp80 @atanhl(x86_fp80 %value) +; CHECK: [[COND1:%[0-9]+]] = fcmp oge x86_fp80 %value, 0xK3FFF8000000000000000 +; CHECK: [[COND2:%[0-9]+]] = fcmp ole x86_fp80 %value, 0xKBFFF8000000000000000 +; CHECK: [[COND:%[0-9]+]] = or i1 [[COND2]], [[COND1]] +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_06 = call x86_fp80 @atanhl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_07 = call x86_fp80 @logl(x86_fp80 %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole x86_fp80 %value, 0xK00000000000000000000 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_07 = call x86_fp80 @logl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_08 = call x86_fp80 @log10l(x86_fp80 %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole x86_fp80 %value, 0xK00000000000000000000 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_08 = call x86_fp80 @log10l(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_09 = call x86_fp80 @log2l(x86_fp80 %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole x86_fp80 %value, 0xK00000000000000000000 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_09 = call x86_fp80 @log2l(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_10 = call x86_fp80 @logbl(x86_fp80 %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole x86_fp80 %value, 0xK00000000000000000000 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_10 = call x86_fp80 @logbl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + %call_11 = call x86_fp80 @log1pl(x86_fp80 %value) +; CHECK: [[COND:%[0-9]+]] = fcmp ole x86_fp80 %value, 0xKBFFF8000000000000000 +; CHECK: br i1 [[COND]], label %[[CALL_LABEL:cdce.call[0-9]*]], label %[[END_LABEL:cdce.end[0-9]*]], !prof ![[BRANCH_WEIGHT:[0-9]+]] +; CHECK: [[CALL_LABEL]]: +; CHECK-NEXT: %call_11 = call x86_fp80 @log1pl(x86_fp80 %value) +; CHECK-NEXT: br label %[[END_LABEL]] +; CHECK: [[END_LABEL]]: + + ret void +} + +declare x86_fp80 @acosl(x86_fp80) +declare x86_fp80 @asinl(x86_fp80) +declare x86_fp80 @cosl(x86_fp80) +declare x86_fp80 @sinl(x86_fp80) +declare x86_fp80 @acoshl(x86_fp80) +declare x86_fp80 @sqrtl(x86_fp80) +declare x86_fp80 @atanhl(x86_fp80) +declare x86_fp80 @logl(x86_fp80) +declare x86_fp80 @log10l(x86_fp80) +declare x86_fp80 @log2l(x86_fp80) +declare x86_fp80 @logbl(x86_fp80) +declare x86_fp80 @log1pl(x86_fp80) + +; CHECK: ![[BRANCH_WEIGHT]] = !{!"branch_weights", i32 1, i32 2000}