blob: e7e28827242eede31884c80b613da6254b6f75d3 [file] [log] [blame]
Rong Xu1c0e9b92016-10-18 21:36:27 +00001//===-- 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 Italiano1e77aac2016-11-08 19:18:20 +000032#include "llvm/Analysis/GlobalsModRef.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +000033#include "llvm/Analysis/TargetLibraryInfo.h"
34#include "llvm/IR/CFG.h"
35#include "llvm/IR/Constants.h"
Davide Italiano6abada82017-04-26 21:05:40 +000036#include "llvm/IR/Dominators.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +000037#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"
45using namespace llvm;
46
47#define DEBUG_TYPE "libcalls-shrinkwrap"
48
49STATISTIC(NumWrappedOneCond, "Number of One-Condition Wrappers Inserted");
50STATISTIC(NumWrappedTwoCond, "Number of Two-Condition Wrappers Inserted");
51
52static 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"));
55static 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"));
58static 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
62namespace {
63class LibCallsShrinkWrapLegacyPass : public FunctionPass {
64public:
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
75char LibCallsShrinkWrapLegacyPass::ID = 0;
76INITIALIZE_PASS_BEGIN(LibCallsShrinkWrapLegacyPass, "libcalls-shrinkwrap",
77 "Conditionally eliminate dead library calls", false,
78 false)
79INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
80INITIALIZE_PASS_END(LibCallsShrinkWrapLegacyPass, "libcalls-shrinkwrap",
81 "Conditionally eliminate dead library calls", false, false)
82
Benjamin Kramerffd37152016-11-19 20:44:26 +000083namespace {
Rong Xu1c0e9b92016-10-18 21:36:27 +000084class LibCallsShrinkWrap : public InstVisitor<LibCallsShrinkWrap> {
85public:
Davide Italiano6abada82017-04-26 21:05:40 +000086 LibCallsShrinkWrap(const TargetLibraryInfo &TLI, DominatorTree *DT)
87 : TLI(TLI), DT(DT), Changed(false){};
Rong Xu1c0e9b92016-10-18 21:36:27 +000088 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
101private:
102 bool perform(CallInst *CI);
103 void checkCandidate(CallInst &CI);
104 void shrinkWrapCI(CallInst *CI, Value *Cond);
David L. Jonesd21529f2017-01-23 23:16:46 +0000105 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 Xu1c0e9b92016-10-18 21:36:27 +0000111
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 Italiano6abada82017-04-26 21:05:40 +0000139 DominatorTree *DT;
Rong Xu1c0e9b92016-10-18 21:36:27 +0000140 SmallVector<CallInst *, 16> WorkList;
141 bool Changed;
142};
Benjamin Kramerffd37152016-11-19 20:44:26 +0000143} // end anonymous namespace
Rong Xu1c0e9b92016-10-18 21:36:27 +0000144
145// Perform the transformation to calls with errno set by domain error.
146bool LibCallsShrinkWrap::performCallDomainErrorOnly(CallInst *CI,
David L. Jonesd21529f2017-01-23 23:16:46 +0000147 const LibFunc &Func) {
Rong Xu1c0e9b92016-10-18 21:36:27 +0000148 Value *Cond = nullptr;
149
150 switch (Func) {
David L. Jonesd21529f2017-01-23 23:16:46 +0000151 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 Xu1c0e9b92016-10-18 21:36:27 +0000157 {
158 ++NumWrappedTwoCond;
159 Cond = createOrCond(CI, CmpInst::FCMP_OLT, -1.0f, CmpInst::FCMP_OGT, 1.0f);
160 break;
161 }
David L. Jonesd21529f2017-01-23 23:16:46 +0000162 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 Xu1c0e9b92016-10-18 21:36:27 +0000168 {
169 ++NumWrappedTwoCond;
170 Cond = createOrCond(CI, CmpInst::FCMP_OEQ, INFINITY, CmpInst::FCMP_OEQ,
171 -INFINITY);
172 break;
173 }
David L. Jonesd21529f2017-01-23 23:16:46 +0000174 case LibFunc_acosh: // DomainError: (x < 1)
175 case LibFunc_acoshf: // Same as acosh
176 case LibFunc_acoshl: // Same as acosh
Rong Xu1c0e9b92016-10-18 21:36:27 +0000177 {
178 ++NumWrappedOneCond;
179 Cond = createCond(CI, CmpInst::FCMP_OLT, 1.0f);
180 break;
181 }
David L. Jonesd21529f2017-01-23 23:16:46 +0000182 case LibFunc_sqrt: // DomainError: (x < 0)
183 case LibFunc_sqrtf: // Same as sqrt
184 case LibFunc_sqrtl: // Same as sqrt
Rong Xu1c0e9b92016-10-18 21:36:27 +0000185 {
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.
198bool LibCallsShrinkWrap::performCallRangeErrorOnly(CallInst *CI,
David L. Jonesd21529f2017-01-23 23:16:46 +0000199 const LibFunc &Func) {
Rong Xu1c0e9b92016-10-18 21:36:27 +0000200 Value *Cond = nullptr;
201
202 switch (Func) {
David L. Jonesd21529f2017-01-23 23:16:46 +0000203 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 Xu1c0e9b92016-10-18 21:36:27 +0000218 Cond = generateTwoRangeCond(CI, Func);
219 break;
220 }
David L. Jonesd21529f2017-01-23 23:16:46 +0000221 case LibFunc_expm1: // RangeError: (709, inf)
222 case LibFunc_expm1f: // RangeError: (88, inf)
223 case LibFunc_expm1l: // RangeError: (11356, inf)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000224 {
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.
236bool LibCallsShrinkWrap::performCallErrors(CallInst *CI,
David L. Jonesd21529f2017-01-23 23:16:46 +0000237 const LibFunc &Func) {
Rong Xu1c0e9b92016-10-18 21:36:27 +0000238 Value *Cond = nullptr;
239
240 switch (Func) {
David L. Jonesd21529f2017-01-23 23:16:46 +0000241 case LibFunc_atanh: // DomainError: (x < -1 || x > 1)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000242 // PoleError: (x == -1 || x == 1)
243 // Overall Cond: (x <= -1 || x >= 1)
David L. Jonesd21529f2017-01-23 23:16:46 +0000244 case LibFunc_atanhf: // Same as atanh
245 case LibFunc_atanhl: // Same as atanh
Rong Xu1c0e9b92016-10-18 21:36:27 +0000246 {
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. Jonesd21529f2017-01-23 23:16:46 +0000253 case LibFunc_log: // DomainError: (x < 0)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000254 // PoleError: (x == 0)
255 // Overall Cond: (x <= 0)
David L. Jonesd21529f2017-01-23 23:16:46 +0000256 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 Xu1c0e9b92016-10-18 21:36:27 +0000267 {
268 if (!LibCallsShrinkWrapDoDomainError || !LibCallsShrinkWrapDoPoleError)
269 return false;
270 ++NumWrappedOneCond;
271 Cond = createCond(CI, CmpInst::FCMP_OLE, 0.0f);
272 break;
273 }
David L. Jonesd21529f2017-01-23 23:16:46 +0000274 case LibFunc_log1p: // DomainError: (x < -1)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000275 // PoleError: (x == -1)
276 // Overall Cond: (x <= -1)
David L. Jonesd21529f2017-01-23 23:16:46 +0000277 case LibFunc_log1pf: // Same as log1p
278 case LibFunc_log1pl: // Same as log1p
Rong Xu1c0e9b92016-10-18 21:36:27 +0000279 {
280 if (!LibCallsShrinkWrapDoDomainError || !LibCallsShrinkWrapDoPoleError)
281 return false;
282 ++NumWrappedOneCond;
283 Cond = createCond(CI, CmpInst::FCMP_OLE, -1.0f);
284 break;
285 }
David L. Jonesd21529f2017-01-23 23:16:46 +0000286 case LibFunc_pow: // DomainError: x < 0 and y is noninteger
Rong Xu1c0e9b92016-10-18 21:36:27 +0000287 // PoleError: x == 0 and y < 0
288 // RangeError: overflow or underflow
David L. Jonesd21529f2017-01-23 23:16:46 +0000289 case LibFunc_powf:
290 case LibFunc_powl: {
Rong Xu1c0e9b92016-10-18 21:36:27 +0000291 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.
309void 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. Jonesd21529f2017-01-23 23:16:46 +0000319 LibFunc Func;
Rong Xu1c0e9b92016-10-18 21:36:27 +0000320 Function *Callee = CI.getCalledFunction();
321 if (!Callee)
322 return;
323 if (!TLI.getLibFunc(*Callee, Func) || !TLI.has(Func))
324 return;
325
Rong Xub05bac92016-10-24 16:50:12 +0000326 if (CI.getNumArgOperands() == 0)
327 return;
Rong Xu1c0e9b92016-10-18 21:36:27 +0000328 // 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.
338Value *LibCallsShrinkWrap::generateOneRangeCond(CallInst *CI,
David L. Jonesd21529f2017-01-23 23:16:46 +0000339 const LibFunc &Func) {
Rong Xu1c0e9b92016-10-18 21:36:27 +0000340 float UpperBound;
341 switch (Func) {
David L. Jonesd21529f2017-01-23 23:16:46 +0000342 case LibFunc_expm1: // RangeError: (709, inf)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000343 UpperBound = 709.0f;
344 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000345 case LibFunc_expm1f: // RangeError: (88, inf)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000346 UpperBound = 88.0f;
347 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000348 case LibFunc_expm1l: // RangeError: (11356, inf)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000349 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.
360Value *LibCallsShrinkWrap::generateTwoRangeCond(CallInst *CI,
David L. Jonesd21529f2017-01-23 23:16:46 +0000361 const LibFunc &Func) {
Rong Xu1c0e9b92016-10-18 21:36:27 +0000362 float UpperBound, LowerBound;
363 switch (Func) {
David L. Jonesd21529f2017-01-23 23:16:46 +0000364 case LibFunc_cosh: // RangeError: (x < -710 || x > 710)
365 case LibFunc_sinh: // Same as cosh
Rong Xu1c0e9b92016-10-18 21:36:27 +0000366 LowerBound = -710.0f;
367 UpperBound = 710.0f;
368 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000369 case LibFunc_coshf: // RangeError: (x < -89 || x > 89)
370 case LibFunc_sinhf: // Same as coshf
Rong Xu1c0e9b92016-10-18 21:36:27 +0000371 LowerBound = -89.0f;
372 UpperBound = 89.0f;
373 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000374 case LibFunc_coshl: // RangeError: (x < -11357 || x > 11357)
375 case LibFunc_sinhl: // Same as coshl
Rong Xu1c0e9b92016-10-18 21:36:27 +0000376 LowerBound = -11357.0f;
377 UpperBound = 11357.0f;
378 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000379 case LibFunc_exp: // RangeError: (x < -745 || x > 709)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000380 LowerBound = -745.0f;
381 UpperBound = 709.0f;
382 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000383 case LibFunc_expf: // RangeError: (x < -103 || x > 88)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000384 LowerBound = -103.0f;
385 UpperBound = 88.0f;
386 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000387 case LibFunc_expl: // RangeError: (x < -11399 || x > 11356)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000388 LowerBound = -11399.0f;
389 UpperBound = 11356.0f;
390 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000391 case LibFunc_exp10: // RangeError: (x < -323 || x > 308)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000392 LowerBound = -323.0f;
393 UpperBound = 308.0f;
394 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000395 case LibFunc_exp10f: // RangeError: (x < -45 || x > 38)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000396 LowerBound = -45.0f;
397 UpperBound = 38.0f;
398 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000399 case LibFunc_exp10l: // RangeError: (x < -4950 || x > 4932)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000400 LowerBound = -4950.0f;
401 UpperBound = 4932.0f;
402 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000403 case LibFunc_exp2: // RangeError: (x < -1074 || x > 1023)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000404 LowerBound = -1074.0f;
405 UpperBound = 1023.0f;
406 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000407 case LibFunc_exp2f: // RangeError: (x < -149 || x > 127)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000408 LowerBound = -149.0f;
409 UpperBound = 127.0f;
410 break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000411 case LibFunc_exp2l: // RangeError: (x < -16445 || x > 11383)
Rong Xu1c0e9b92016-10-18 21:36:27 +0000412 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//
439Value *LibCallsShrinkWrap::generateCondForPow(CallInst *CI,
David L. Jonesd21529f2017-01-23 23:16:46 +0000440 const LibFunc &Func) {
441 // FIXME: LibFunc_powf and powl TBD.
442 if (Func != LibFunc_pow) {
Rong Xu1c0e9b92016-10-18 21:36:27 +0000443 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.
504void 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 Italiano6abada82017-04-26 21:05:40 +0000508
Rong Xu1c0e9b92016-10-18 21:36:27 +0000509 TerminatorInst *NewInst =
Davide Italiano6abada82017-04-26 21:05:40 +0000510 SplitBlockAndInsertIfThen(Cond, CI, false, BranchWeights, DT);
Rong Xu1c0e9b92016-10-18 21:36:27 +0000511 BasicBlock *CallBB = NewInst->getParent();
512 CallBB->setName("cdce.call");
Davide Italiano6abada82017-04-26 21:05:40 +0000513 BasicBlock *SuccBB = CallBB->getSingleSuccessor();
514 assert(SuccBB && "The split block should have a single successor");
515 SuccBB->setName("cdce.end");
Rong Xu1c0e9b92016-10-18 21:36:27 +0000516 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.
524bool LibCallsShrinkWrap::perform(CallInst *CI) {
David L. Jonesd21529f2017-01-23 23:16:46 +0000525 LibFunc Func;
Rong Xu1c0e9b92016-10-18 21:36:27 +0000526 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
540void LibCallsShrinkWrapLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
Davide Italiano6abada82017-04-26 21:05:40 +0000541 AU.addPreserved<DominatorTreeWrapperPass>();
Davide Italiano1e77aac2016-11-08 19:18:20 +0000542 AU.addPreserved<GlobalsAAWrapperPass>();
Rong Xu1c0e9b92016-10-18 21:36:27 +0000543 AU.addRequired<TargetLibraryInfoWrapperPass>();
544}
545
Davide Italiano6abada82017-04-26 21:05:40 +0000546static bool runImpl(Function &F, const TargetLibraryInfo &TLI,
547 DominatorTree *DT) {
Rong Xu1c0e9b92016-10-18 21:36:27 +0000548 if (F.hasFnAttribute(Attribute::OptimizeForSize))
549 return false;
Davide Italiano6abada82017-04-26 21:05:40 +0000550 LibCallsShrinkWrap CCDCE(TLI, DT);
Rong Xu1c0e9b92016-10-18 21:36:27 +0000551 CCDCE.visit(F);
552 CCDCE.perform();
Davide Italiano6abada82017-04-26 21:05:40 +0000553
554// Verify the dominator after we've updated it locally.
555#ifndef NDEBUG
556 if (DT)
557 DT->verifyDomTree();
558#endif
Rong Xu1c0e9b92016-10-18 21:36:27 +0000559 return CCDCE.isChanged();
560}
561
562bool LibCallsShrinkWrapLegacyPass::runOnFunction(Function &F) {
563 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Davide Italiano6abada82017-04-26 21:05:40 +0000564 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
565 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
566 return runImpl(F, TLI, DT);
Rong Xu1c0e9b92016-10-18 21:36:27 +0000567}
568
569namespace llvm {
570char &LibCallsShrinkWrapPassID = LibCallsShrinkWrapLegacyPass::ID;
571
572// Public interface to LibCallsShrinkWrap pass.
573FunctionPass *createLibCallsShrinkWrapPass() {
574 return new LibCallsShrinkWrapLegacyPass();
575}
576
577PreservedAnalyses LibCallsShrinkWrapPass::run(Function &F,
578 FunctionAnalysisManager &FAM) {
579 auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
Davide Italiano6abada82017-04-26 21:05:40 +0000580 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
581 bool Changed = runImpl(F, TLI, DT);
Rong Xu1c0e9b92016-10-18 21:36:27 +0000582 if (!Changed)
583 return PreservedAnalyses::all();
Davide Italiano1e77aac2016-11-08 19:18:20 +0000584 auto PA = PreservedAnalyses();
585 PA.preserve<GlobalsAA>();
Davide Italiano6abada82017-04-26 21:05:40 +0000586 PA.preserve<DominatorTreeAnalysis>();
Davide Italiano1e77aac2016-11-08 19:18:20 +0000587 return PA;
Rong Xu1c0e9b92016-10-18 21:36:27 +0000588}
589}