Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 1 | //===-- LibCallsShrinkWrap.cpp ----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass shrink-wraps a call to function if the result is not used. |
| 11 | // The call can set errno but is otherwise side effect free. For example: |
| 12 | // sqrt(val); |
| 13 | // is transformed to |
| 14 | // if (val < 0) |
| 15 | // sqrt(val); |
| 16 | // Even if the result of library call is not being used, the compiler cannot |
| 17 | // safely delete the call because the function can set errno on error |
| 18 | // conditions. |
| 19 | // Note in many functions, the error condition solely depends on the incoming |
| 20 | // parameter. In this optimization, we can generate the condition can lead to |
| 21 | // the errno to shrink-wrap the call. Since the chances of hitting the error |
| 22 | // condition is low, the runtime call is effectively eliminated. |
| 23 | // |
| 24 | // These partially dead calls are usually results of C++ abstraction penalty |
| 25 | // exposed by inlining. |
| 26 | // |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h" |
| 30 | #include "llvm/ADT/SmallVector.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
Davide Italiano | 1e77aac | 2016-11-08 19:18:20 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/GlobalsModRef.h" |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 34 | #include "llvm/IR/CFG.h" |
| 35 | #include "llvm/IR/Constants.h" |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 36 | #include "llvm/IR/Dominators.h" |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Function.h" |
| 38 | #include "llvm/IR/IRBuilder.h" |
| 39 | #include "llvm/IR/InstVisitor.h" |
| 40 | #include "llvm/IR/Instructions.h" |
| 41 | #include "llvm/IR/LLVMContext.h" |
| 42 | #include "llvm/IR/MDBuilder.h" |
| 43 | #include "llvm/Pass.h" |
| 44 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 45 | using namespace llvm; |
| 46 | |
| 47 | #define DEBUG_TYPE "libcalls-shrinkwrap" |
| 48 | |
| 49 | STATISTIC(NumWrappedOneCond, "Number of One-Condition Wrappers Inserted"); |
| 50 | STATISTIC(NumWrappedTwoCond, "Number of Two-Condition Wrappers Inserted"); |
| 51 | |
| 52 | static cl::opt<bool> LibCallsShrinkWrapDoDomainError( |
| 53 | "libcalls-shrinkwrap-domain-error", cl::init(true), cl::Hidden, |
| 54 | cl::desc("Perform shrink-wrap on lib calls with domain errors")); |
| 55 | static cl::opt<bool> LibCallsShrinkWrapDoRangeError( |
| 56 | "libcalls-shrinkwrap-range-error", cl::init(true), cl::Hidden, |
| 57 | cl::desc("Perform shrink-wrap on lib calls with range errors")); |
| 58 | static cl::opt<bool> LibCallsShrinkWrapDoPoleError( |
| 59 | "libcalls-shrinkwrap-pole-error", cl::init(true), cl::Hidden, |
| 60 | cl::desc("Perform shrink-wrap on lib calls with pole errors")); |
| 61 | |
| 62 | namespace { |
| 63 | class LibCallsShrinkWrapLegacyPass : public FunctionPass { |
| 64 | public: |
| 65 | static char ID; // Pass identification, replacement for typeid |
| 66 | explicit LibCallsShrinkWrapLegacyPass() : FunctionPass(ID) { |
| 67 | initializeLibCallsShrinkWrapLegacyPassPass( |
| 68 | *PassRegistry::getPassRegistry()); |
| 69 | } |
| 70 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 71 | bool runOnFunction(Function &F) override; |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | char LibCallsShrinkWrapLegacyPass::ID = 0; |
| 76 | INITIALIZE_PASS_BEGIN(LibCallsShrinkWrapLegacyPass, "libcalls-shrinkwrap", |
| 77 | "Conditionally eliminate dead library calls", false, |
| 78 | false) |
| 79 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 80 | INITIALIZE_PASS_END(LibCallsShrinkWrapLegacyPass, "libcalls-shrinkwrap", |
| 81 | "Conditionally eliminate dead library calls", false, false) |
| 82 | |
Benjamin Kramer | ffd3715 | 2016-11-19 20:44:26 +0000 | [diff] [blame] | 83 | namespace { |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 84 | class LibCallsShrinkWrap : public InstVisitor<LibCallsShrinkWrap> { |
| 85 | public: |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 86 | LibCallsShrinkWrap(const TargetLibraryInfo &TLI, DominatorTree *DT) |
| 87 | : TLI(TLI), DT(DT), Changed(false){}; |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 88 | bool isChanged() const { return Changed; } |
| 89 | void visitCallInst(CallInst &CI) { checkCandidate(CI); } |
| 90 | void perform() { |
| 91 | for (auto &CI : WorkList) { |
| 92 | DEBUG(dbgs() << "CDCE calls: " << CI->getCalledFunction()->getName() |
| 93 | << "\n"); |
| 94 | if (perform(CI)) { |
| 95 | Changed = true; |
| 96 | DEBUG(dbgs() << "Transformed\n"); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | bool perform(CallInst *CI); |
| 103 | void checkCandidate(CallInst &CI); |
| 104 | void shrinkWrapCI(CallInst *CI, Value *Cond); |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 105 | bool performCallDomainErrorOnly(CallInst *CI, const LibFunc &Func); |
| 106 | bool performCallErrors(CallInst *CI, const LibFunc &Func); |
| 107 | bool performCallRangeErrorOnly(CallInst *CI, const LibFunc &Func); |
| 108 | Value *generateOneRangeCond(CallInst *CI, const LibFunc &Func); |
| 109 | Value *generateTwoRangeCond(CallInst *CI, const LibFunc &Func); |
| 110 | Value *generateCondForPow(CallInst *CI, const LibFunc &Func); |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 111 | |
| 112 | // Create an OR of two conditions. |
| 113 | Value *createOrCond(CallInst *CI, CmpInst::Predicate Cmp, float Val, |
| 114 | CmpInst::Predicate Cmp2, float Val2) { |
| 115 | IRBuilder<> BBBuilder(CI); |
| 116 | Value *Arg = CI->getArgOperand(0); |
| 117 | auto Cond2 = createCond(BBBuilder, Arg, Cmp2, Val2); |
| 118 | auto Cond1 = createCond(BBBuilder, Arg, Cmp, Val); |
| 119 | return BBBuilder.CreateOr(Cond1, Cond2); |
| 120 | } |
| 121 | |
| 122 | // Create a single condition using IRBuilder. |
| 123 | Value *createCond(IRBuilder<> &BBBuilder, Value *Arg, CmpInst::Predicate Cmp, |
| 124 | float Val) { |
| 125 | Constant *V = ConstantFP::get(BBBuilder.getContext(), APFloat(Val)); |
| 126 | if (!Arg->getType()->isFloatTy()) |
| 127 | V = ConstantExpr::getFPExtend(V, Arg->getType()); |
| 128 | return BBBuilder.CreateFCmp(Cmp, Arg, V); |
| 129 | } |
| 130 | |
| 131 | // Create a single condition. |
| 132 | Value *createCond(CallInst *CI, CmpInst::Predicate Cmp, float Val) { |
| 133 | IRBuilder<> BBBuilder(CI); |
| 134 | Value *Arg = CI->getArgOperand(0); |
| 135 | return createCond(BBBuilder, Arg, Cmp, Val); |
| 136 | } |
| 137 | |
| 138 | const TargetLibraryInfo &TLI; |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 139 | DominatorTree *DT; |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 140 | SmallVector<CallInst *, 16> WorkList; |
| 141 | bool Changed; |
| 142 | }; |
Benjamin Kramer | ffd3715 | 2016-11-19 20:44:26 +0000 | [diff] [blame] | 143 | } // end anonymous namespace |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 144 | |
| 145 | // Perform the transformation to calls with errno set by domain error. |
| 146 | bool LibCallsShrinkWrap::performCallDomainErrorOnly(CallInst *CI, |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 147 | const LibFunc &Func) { |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 148 | Value *Cond = nullptr; |
| 149 | |
| 150 | switch (Func) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 151 | case LibFunc_acos: // DomainError: (x < -1 || x > 1) |
| 152 | case LibFunc_acosf: // Same as acos |
| 153 | case LibFunc_acosl: // Same as acos |
| 154 | case LibFunc_asin: // DomainError: (x < -1 || x > 1) |
| 155 | case LibFunc_asinf: // Same as asin |
| 156 | case LibFunc_asinl: // Same as asin |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 157 | { |
| 158 | ++NumWrappedTwoCond; |
| 159 | Cond = createOrCond(CI, CmpInst::FCMP_OLT, -1.0f, CmpInst::FCMP_OGT, 1.0f); |
| 160 | break; |
| 161 | } |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 162 | case LibFunc_cos: // DomainError: (x == +inf || x == -inf) |
| 163 | case LibFunc_cosf: // Same as cos |
| 164 | case LibFunc_cosl: // Same as cos |
| 165 | case LibFunc_sin: // DomainError: (x == +inf || x == -inf) |
| 166 | case LibFunc_sinf: // Same as sin |
| 167 | case LibFunc_sinl: // Same as sin |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 168 | { |
| 169 | ++NumWrappedTwoCond; |
| 170 | Cond = createOrCond(CI, CmpInst::FCMP_OEQ, INFINITY, CmpInst::FCMP_OEQ, |
| 171 | -INFINITY); |
| 172 | break; |
| 173 | } |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 174 | case LibFunc_acosh: // DomainError: (x < 1) |
| 175 | case LibFunc_acoshf: // Same as acosh |
| 176 | case LibFunc_acoshl: // Same as acosh |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 177 | { |
| 178 | ++NumWrappedOneCond; |
| 179 | Cond = createCond(CI, CmpInst::FCMP_OLT, 1.0f); |
| 180 | break; |
| 181 | } |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 182 | case LibFunc_sqrt: // DomainError: (x < 0) |
| 183 | case LibFunc_sqrtf: // Same as sqrt |
| 184 | case LibFunc_sqrtl: // Same as sqrt |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 185 | { |
| 186 | ++NumWrappedOneCond; |
| 187 | Cond = createCond(CI, CmpInst::FCMP_OLT, 0.0f); |
| 188 | break; |
| 189 | } |
| 190 | default: |
| 191 | return false; |
| 192 | } |
| 193 | shrinkWrapCI(CI, Cond); |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | // Perform the transformation to calls with errno set by range error. |
| 198 | bool LibCallsShrinkWrap::performCallRangeErrorOnly(CallInst *CI, |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 199 | const LibFunc &Func) { |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 200 | Value *Cond = nullptr; |
| 201 | |
| 202 | switch (Func) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 203 | case LibFunc_cosh: |
| 204 | case LibFunc_coshf: |
| 205 | case LibFunc_coshl: |
| 206 | case LibFunc_exp: |
| 207 | case LibFunc_expf: |
| 208 | case LibFunc_expl: |
| 209 | case LibFunc_exp10: |
| 210 | case LibFunc_exp10f: |
| 211 | case LibFunc_exp10l: |
| 212 | case LibFunc_exp2: |
| 213 | case LibFunc_exp2f: |
| 214 | case LibFunc_exp2l: |
| 215 | case LibFunc_sinh: |
| 216 | case LibFunc_sinhf: |
| 217 | case LibFunc_sinhl: { |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 218 | Cond = generateTwoRangeCond(CI, Func); |
| 219 | break; |
| 220 | } |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 221 | case LibFunc_expm1: // RangeError: (709, inf) |
| 222 | case LibFunc_expm1f: // RangeError: (88, inf) |
| 223 | case LibFunc_expm1l: // RangeError: (11356, inf) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 224 | { |
| 225 | Cond = generateOneRangeCond(CI, Func); |
| 226 | break; |
| 227 | } |
| 228 | default: |
| 229 | return false; |
| 230 | } |
| 231 | shrinkWrapCI(CI, Cond); |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | // Perform the transformation to calls with errno set by combination of errors. |
| 236 | bool LibCallsShrinkWrap::performCallErrors(CallInst *CI, |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 237 | const LibFunc &Func) { |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 238 | Value *Cond = nullptr; |
| 239 | |
| 240 | switch (Func) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 241 | case LibFunc_atanh: // DomainError: (x < -1 || x > 1) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 242 | // PoleError: (x == -1 || x == 1) |
| 243 | // Overall Cond: (x <= -1 || x >= 1) |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 244 | case LibFunc_atanhf: // Same as atanh |
| 245 | case LibFunc_atanhl: // Same as atanh |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 246 | { |
| 247 | if (!LibCallsShrinkWrapDoDomainError || !LibCallsShrinkWrapDoPoleError) |
| 248 | return false; |
| 249 | ++NumWrappedTwoCond; |
| 250 | Cond = createOrCond(CI, CmpInst::FCMP_OLE, -1.0f, CmpInst::FCMP_OGE, 1.0f); |
| 251 | break; |
| 252 | } |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 253 | case LibFunc_log: // DomainError: (x < 0) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 254 | // PoleError: (x == 0) |
| 255 | // Overall Cond: (x <= 0) |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 256 | case LibFunc_logf: // Same as log |
| 257 | case LibFunc_logl: // Same as log |
| 258 | case LibFunc_log10: // Same as log |
| 259 | case LibFunc_log10f: // Same as log |
| 260 | case LibFunc_log10l: // Same as log |
| 261 | case LibFunc_log2: // Same as log |
| 262 | case LibFunc_log2f: // Same as log |
| 263 | case LibFunc_log2l: // Same as log |
| 264 | case LibFunc_logb: // Same as log |
| 265 | case LibFunc_logbf: // Same as log |
| 266 | case LibFunc_logbl: // Same as log |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 267 | { |
| 268 | if (!LibCallsShrinkWrapDoDomainError || !LibCallsShrinkWrapDoPoleError) |
| 269 | return false; |
| 270 | ++NumWrappedOneCond; |
| 271 | Cond = createCond(CI, CmpInst::FCMP_OLE, 0.0f); |
| 272 | break; |
| 273 | } |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 274 | case LibFunc_log1p: // DomainError: (x < -1) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 275 | // PoleError: (x == -1) |
| 276 | // Overall Cond: (x <= -1) |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 277 | case LibFunc_log1pf: // Same as log1p |
| 278 | case LibFunc_log1pl: // Same as log1p |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 279 | { |
| 280 | if (!LibCallsShrinkWrapDoDomainError || !LibCallsShrinkWrapDoPoleError) |
| 281 | return false; |
| 282 | ++NumWrappedOneCond; |
| 283 | Cond = createCond(CI, CmpInst::FCMP_OLE, -1.0f); |
| 284 | break; |
| 285 | } |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 286 | case LibFunc_pow: // DomainError: x < 0 and y is noninteger |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 287 | // PoleError: x == 0 and y < 0 |
| 288 | // RangeError: overflow or underflow |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 289 | case LibFunc_powf: |
| 290 | case LibFunc_powl: { |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 291 | if (!LibCallsShrinkWrapDoDomainError || !LibCallsShrinkWrapDoPoleError || |
| 292 | !LibCallsShrinkWrapDoRangeError) |
| 293 | return false; |
| 294 | Cond = generateCondForPow(CI, Func); |
| 295 | if (Cond == nullptr) |
| 296 | return false; |
| 297 | break; |
| 298 | } |
| 299 | default: |
| 300 | return false; |
| 301 | } |
| 302 | assert(Cond && "performCallErrors should not see an empty condition"); |
| 303 | shrinkWrapCI(CI, Cond); |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | // Checks if CI is a candidate for shrinkwrapping and put it into work list if |
| 308 | // true. |
| 309 | void LibCallsShrinkWrap::checkCandidate(CallInst &CI) { |
| 310 | if (CI.isNoBuiltin()) |
| 311 | return; |
| 312 | // A possible improvement is to handle the calls with the return value being |
| 313 | // used. If there is API for fast libcall implementation without setting |
| 314 | // errno, we can use the same framework to direct/wrap the call to the fast |
| 315 | // API in the error free path, and leave the original call in the slow path. |
| 316 | if (!CI.use_empty()) |
| 317 | return; |
| 318 | |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 319 | LibFunc Func; |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 320 | Function *Callee = CI.getCalledFunction(); |
| 321 | if (!Callee) |
| 322 | return; |
| 323 | if (!TLI.getLibFunc(*Callee, Func) || !TLI.has(Func)) |
| 324 | return; |
| 325 | |
Rong Xu | b05bac9 | 2016-10-24 16:50:12 +0000 | [diff] [blame] | 326 | if (CI.getNumArgOperands() == 0) |
| 327 | return; |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 328 | // TODO: Handle long double in other formats. |
| 329 | Type *ArgType = CI.getArgOperand(0)->getType(); |
| 330 | if (!(ArgType->isFloatTy() || ArgType->isDoubleTy() || |
| 331 | ArgType->isX86_FP80Ty())) |
| 332 | return; |
| 333 | |
| 334 | WorkList.push_back(&CI); |
| 335 | } |
| 336 | |
| 337 | // Generate the upper bound condition for RangeError. |
| 338 | Value *LibCallsShrinkWrap::generateOneRangeCond(CallInst *CI, |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 339 | const LibFunc &Func) { |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 340 | float UpperBound; |
| 341 | switch (Func) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 342 | case LibFunc_expm1: // RangeError: (709, inf) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 343 | UpperBound = 709.0f; |
| 344 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 345 | case LibFunc_expm1f: // RangeError: (88, inf) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 346 | UpperBound = 88.0f; |
| 347 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 348 | case LibFunc_expm1l: // RangeError: (11356, inf) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 349 | UpperBound = 11356.0f; |
| 350 | break; |
| 351 | default: |
| 352 | llvm_unreachable("Should be reach here"); |
| 353 | } |
| 354 | |
| 355 | ++NumWrappedOneCond; |
| 356 | return createCond(CI, CmpInst::FCMP_OGT, UpperBound); |
| 357 | } |
| 358 | |
| 359 | // Generate the lower and upper bound condition for RangeError. |
| 360 | Value *LibCallsShrinkWrap::generateTwoRangeCond(CallInst *CI, |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 361 | const LibFunc &Func) { |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 362 | float UpperBound, LowerBound; |
| 363 | switch (Func) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 364 | case LibFunc_cosh: // RangeError: (x < -710 || x > 710) |
| 365 | case LibFunc_sinh: // Same as cosh |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 366 | LowerBound = -710.0f; |
| 367 | UpperBound = 710.0f; |
| 368 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 369 | case LibFunc_coshf: // RangeError: (x < -89 || x > 89) |
| 370 | case LibFunc_sinhf: // Same as coshf |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 371 | LowerBound = -89.0f; |
| 372 | UpperBound = 89.0f; |
| 373 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 374 | case LibFunc_coshl: // RangeError: (x < -11357 || x > 11357) |
| 375 | case LibFunc_sinhl: // Same as coshl |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 376 | LowerBound = -11357.0f; |
| 377 | UpperBound = 11357.0f; |
| 378 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 379 | case LibFunc_exp: // RangeError: (x < -745 || x > 709) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 380 | LowerBound = -745.0f; |
| 381 | UpperBound = 709.0f; |
| 382 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 383 | case LibFunc_expf: // RangeError: (x < -103 || x > 88) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 384 | LowerBound = -103.0f; |
| 385 | UpperBound = 88.0f; |
| 386 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 387 | case LibFunc_expl: // RangeError: (x < -11399 || x > 11356) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 388 | LowerBound = -11399.0f; |
| 389 | UpperBound = 11356.0f; |
| 390 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 391 | case LibFunc_exp10: // RangeError: (x < -323 || x > 308) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 392 | LowerBound = -323.0f; |
| 393 | UpperBound = 308.0f; |
| 394 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 395 | case LibFunc_exp10f: // RangeError: (x < -45 || x > 38) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 396 | LowerBound = -45.0f; |
| 397 | UpperBound = 38.0f; |
| 398 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 399 | case LibFunc_exp10l: // RangeError: (x < -4950 || x > 4932) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 400 | LowerBound = -4950.0f; |
| 401 | UpperBound = 4932.0f; |
| 402 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 403 | case LibFunc_exp2: // RangeError: (x < -1074 || x > 1023) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 404 | LowerBound = -1074.0f; |
| 405 | UpperBound = 1023.0f; |
| 406 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 407 | case LibFunc_exp2f: // RangeError: (x < -149 || x > 127) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 408 | LowerBound = -149.0f; |
| 409 | UpperBound = 127.0f; |
| 410 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 411 | case LibFunc_exp2l: // RangeError: (x < -16445 || x > 11383) |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 412 | LowerBound = -16445.0f; |
| 413 | UpperBound = 11383.0f; |
| 414 | break; |
| 415 | default: |
| 416 | llvm_unreachable("Should be reach here"); |
| 417 | } |
| 418 | |
| 419 | ++NumWrappedTwoCond; |
| 420 | return createOrCond(CI, CmpInst::FCMP_OGT, UpperBound, CmpInst::FCMP_OLT, |
| 421 | LowerBound); |
| 422 | } |
| 423 | |
| 424 | // For pow(x,y), We only handle the following cases: |
| 425 | // (1) x is a constant && (x >= 1) && (x < MaxUInt8) |
| 426 | // Cond is: (y > 127) |
| 427 | // (2) x is a value coming from an integer type. |
| 428 | // (2.1) if x's bit_size == 8 |
| 429 | // Cond: (x <= 0 || y > 128) |
| 430 | // (2.2) if x's bit_size is 16 |
| 431 | // Cond: (x <= 0 || y > 64) |
| 432 | // (2.3) if x's bit_size is 32 |
| 433 | // Cond: (x <= 0 || y > 32) |
| 434 | // Support for powl(x,y) and powf(x,y) are TBD. |
| 435 | // |
| 436 | // Note that condition can be more conservative than the actual condition |
| 437 | // (i.e. we might invoke the calls that will not set the errno.). |
| 438 | // |
| 439 | Value *LibCallsShrinkWrap::generateCondForPow(CallInst *CI, |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 440 | const LibFunc &Func) { |
| 441 | // FIXME: LibFunc_powf and powl TBD. |
| 442 | if (Func != LibFunc_pow) { |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 443 | DEBUG(dbgs() << "Not handled powf() and powl()\n"); |
| 444 | return nullptr; |
| 445 | } |
| 446 | |
| 447 | Value *Base = CI->getArgOperand(0); |
| 448 | Value *Exp = CI->getArgOperand(1); |
| 449 | IRBuilder<> BBBuilder(CI); |
| 450 | |
| 451 | // Constant Base case. |
| 452 | if (ConstantFP *CF = dyn_cast<ConstantFP>(Base)) { |
| 453 | double D = CF->getValueAPF().convertToDouble(); |
| 454 | if (D < 1.0f || D > APInt::getMaxValue(8).getZExtValue()) { |
| 455 | DEBUG(dbgs() << "Not handled pow(): constant base out of range\n"); |
| 456 | return nullptr; |
| 457 | } |
| 458 | |
| 459 | ++NumWrappedOneCond; |
| 460 | Constant *V = ConstantFP::get(CI->getContext(), APFloat(127.0f)); |
| 461 | if (!Exp->getType()->isFloatTy()) |
| 462 | V = ConstantExpr::getFPExtend(V, Exp->getType()); |
| 463 | return BBBuilder.CreateFCmp(CmpInst::FCMP_OGT, Exp, V); |
| 464 | } |
| 465 | |
| 466 | // If the Base value coming from an integer type. |
| 467 | Instruction *I = dyn_cast<Instruction>(Base); |
| 468 | if (!I) { |
| 469 | DEBUG(dbgs() << "Not handled pow(): FP type base\n"); |
| 470 | return nullptr; |
| 471 | } |
| 472 | unsigned Opcode = I->getOpcode(); |
| 473 | if (Opcode == Instruction::UIToFP || Opcode == Instruction::SIToFP) { |
| 474 | unsigned BW = I->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 475 | float UpperV = 0.0f; |
| 476 | if (BW == 8) |
| 477 | UpperV = 128.0f; |
| 478 | else if (BW == 16) |
| 479 | UpperV = 64.0f; |
| 480 | else if (BW == 32) |
| 481 | UpperV = 32.0f; |
| 482 | else { |
| 483 | DEBUG(dbgs() << "Not handled pow(): type too wide\n"); |
| 484 | return nullptr; |
| 485 | } |
| 486 | |
| 487 | ++NumWrappedTwoCond; |
| 488 | Constant *V = ConstantFP::get(CI->getContext(), APFloat(UpperV)); |
| 489 | Constant *V0 = ConstantFP::get(CI->getContext(), APFloat(0.0f)); |
| 490 | if (!Exp->getType()->isFloatTy()) |
| 491 | V = ConstantExpr::getFPExtend(V, Exp->getType()); |
| 492 | if (!Base->getType()->isFloatTy()) |
| 493 | V0 = ConstantExpr::getFPExtend(V0, Exp->getType()); |
| 494 | |
| 495 | Value *Cond = BBBuilder.CreateFCmp(CmpInst::FCMP_OGT, Exp, V); |
| 496 | Value *Cond0 = BBBuilder.CreateFCmp(CmpInst::FCMP_OLE, Base, V0); |
| 497 | return BBBuilder.CreateOr(Cond0, Cond); |
| 498 | } |
| 499 | DEBUG(dbgs() << "Not handled pow(): base not from integer convert\n"); |
| 500 | return nullptr; |
| 501 | } |
| 502 | |
| 503 | // Wrap conditions that can potentially generate errno to the library call. |
| 504 | void LibCallsShrinkWrap::shrinkWrapCI(CallInst *CI, Value *Cond) { |
| 505 | assert(Cond != nullptr && "hrinkWrapCI is not expecting an empty call inst"); |
| 506 | MDNode *BranchWeights = |
| 507 | MDBuilder(CI->getContext()).createBranchWeights(1, 2000); |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 508 | |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 509 | TerminatorInst *NewInst = |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 510 | SplitBlockAndInsertIfThen(Cond, CI, false, BranchWeights, DT); |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 511 | BasicBlock *CallBB = NewInst->getParent(); |
| 512 | CallBB->setName("cdce.call"); |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 513 | BasicBlock *SuccBB = CallBB->getSingleSuccessor(); |
| 514 | assert(SuccBB && "The split block should have a single successor"); |
| 515 | SuccBB->setName("cdce.end"); |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 516 | CI->removeFromParent(); |
| 517 | CallBB->getInstList().insert(CallBB->getFirstInsertionPt(), CI); |
| 518 | DEBUG(dbgs() << "== Basic Block After =="); |
| 519 | DEBUG(dbgs() << *CallBB->getSinglePredecessor() << *CallBB |
| 520 | << *CallBB->getSingleSuccessor() << "\n"); |
| 521 | } |
| 522 | |
| 523 | // Perform the transformation to a single candidate. |
| 524 | bool LibCallsShrinkWrap::perform(CallInst *CI) { |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 525 | LibFunc Func; |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 526 | Function *Callee = CI->getCalledFunction(); |
| 527 | assert(Callee && "perform() should apply to a non-empty callee"); |
| 528 | TLI.getLibFunc(*Callee, Func); |
| 529 | assert(Func && "perform() is not expecting an empty function"); |
| 530 | |
| 531 | if (LibCallsShrinkWrapDoDomainError && performCallDomainErrorOnly(CI, Func)) |
| 532 | return true; |
| 533 | |
| 534 | if (LibCallsShrinkWrapDoRangeError && performCallRangeErrorOnly(CI, Func)) |
| 535 | return true; |
| 536 | |
| 537 | return performCallErrors(CI, Func); |
| 538 | } |
| 539 | |
| 540 | void LibCallsShrinkWrapLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 541 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Davide Italiano | 1e77aac | 2016-11-08 19:18:20 +0000 | [diff] [blame] | 542 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 543 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 544 | } |
| 545 | |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 546 | static bool runImpl(Function &F, const TargetLibraryInfo &TLI, |
| 547 | DominatorTree *DT) { |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 548 | if (F.hasFnAttribute(Attribute::OptimizeForSize)) |
| 549 | return false; |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 550 | LibCallsShrinkWrap CCDCE(TLI, DT); |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 551 | CCDCE.visit(F); |
| 552 | CCDCE.perform(); |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 553 | |
| 554 | // Verify the dominator after we've updated it locally. |
| 555 | #ifndef NDEBUG |
| 556 | if (DT) |
| 557 | DT->verifyDomTree(); |
| 558 | #endif |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 559 | return CCDCE.isChanged(); |
| 560 | } |
| 561 | |
| 562 | bool LibCallsShrinkWrapLegacyPass::runOnFunction(Function &F) { |
| 563 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 564 | auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 565 | auto *DT = DTWP ? &DTWP->getDomTree() : nullptr; |
| 566 | return runImpl(F, TLI, DT); |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | namespace llvm { |
| 570 | char &LibCallsShrinkWrapPassID = LibCallsShrinkWrapLegacyPass::ID; |
| 571 | |
| 572 | // Public interface to LibCallsShrinkWrap pass. |
| 573 | FunctionPass *createLibCallsShrinkWrapPass() { |
| 574 | return new LibCallsShrinkWrapLegacyPass(); |
| 575 | } |
| 576 | |
| 577 | PreservedAnalyses LibCallsShrinkWrapPass::run(Function &F, |
| 578 | FunctionAnalysisManager &FAM) { |
| 579 | auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F); |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 580 | auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F); |
| 581 | bool Changed = runImpl(F, TLI, DT); |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 582 | if (!Changed) |
| 583 | return PreservedAnalyses::all(); |
Davide Italiano | 1e77aac | 2016-11-08 19:18:20 +0000 | [diff] [blame] | 584 | auto PA = PreservedAnalyses(); |
| 585 | PA.preserve<GlobalsAA>(); |
Davide Italiano | 6abada8 | 2017-04-26 21:05:40 +0000 | [diff] [blame^] | 586 | PA.preserve<DominatorTreeAnalysis>(); |
Davide Italiano | 1e77aac | 2016-11-08 19:18:20 +0000 | [diff] [blame] | 587 | return PA; |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 588 | } |
| 589 | } |