blob: caae06c4211e1bf349fec1147f09bee7e17445ac [file] [log] [blame]
Meador Ingedf796f82012-10-13 16:45:24 +00001//===------ SimplifyLibCalls.cpp - Library calls simplifier ---------------===//
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 is a utility pass used for testing the InstructionSimplify analysis.
11// The analysis is applied to every instruction, and if it simplifies then the
12// instruction is replaced by the simplification. If you are looking for a pass
13// that performs serious instruction folding, use the instcombine pass instead.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
Meador Inge20255ef2013-03-12 00:08:29 +000018#include "llvm/ADT/SmallString.h"
Meador Ingedf796f82012-10-13 16:45:24 +000019#include "llvm/ADT/StringMap.h"
Bob Wilsond8d92d92013-11-03 06:48:38 +000020#include "llvm/ADT/Triple.h"
Meador Ingedf796f82012-10-13 16:45:24 +000021#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Diego Novillo7f8af8b2014-05-22 14:19:46 +000023#include "llvm/IR/DiagnosticInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
25#include "llvm/IR/IRBuilder.h"
Meador Inge20255ef2013-03-12 00:08:29 +000026#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Intrinsics.h"
28#include "llvm/IR/LLVMContext.h"
29#include "llvm/IR/Module.h"
Sanjay Patelc699a612014-10-16 18:48:17 +000030#include "llvm/IR/PatternMatch.h"
Nadav Rotem464e8072013-02-27 05:53:43 +000031#include "llvm/Support/Allocator.h"
Hal Finkel66cd3f12013-11-17 02:06:35 +000032#include "llvm/Support/CommandLine.h"
Meador Ingedf796f82012-10-13 16:45:24 +000033#include "llvm/Target/TargetLibraryInfo.h"
34#include "llvm/Transforms/Utils/BuildLibCalls.h"
35
36using namespace llvm;
Sanjay Patelc699a612014-10-16 18:48:17 +000037using namespace PatternMatch;
Meador Ingedf796f82012-10-13 16:45:24 +000038
Hal Finkel66cd3f12013-11-17 02:06:35 +000039static cl::opt<bool>
Chris Bienemanad070d02014-09-17 20:55:46 +000040 ColdErrorCalls("error-reporting-is-cold", cl::init(true), cl::Hidden,
41 cl::desc("Treat error-reporting calls as cold"));
Meador Ingedf796f82012-10-13 16:45:24 +000042
Sanjay Patela92fa442014-10-22 15:29:23 +000043static cl::opt<bool>
44 EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
45 cl::init(false),
46 cl::desc("Enable unsafe double to float "
47 "shrinking for math lib calls"));
48
49
Meador Ingedf796f82012-10-13 16:45:24 +000050//===----------------------------------------------------------------------===//
Meador Inged589ac62012-10-31 03:33:06 +000051// Helper Functions
52//===----------------------------------------------------------------------===//
53
Chris Bienemanad070d02014-09-17 20:55:46 +000054static bool ignoreCallingConv(LibFunc::Func Func) {
55 switch (Func) {
56 case LibFunc::abs:
57 case LibFunc::labs:
58 case LibFunc::llabs:
59 case LibFunc::strlen:
60 return true;
61 default:
62 return false;
63 }
Chris Bienemancf93cbb2014-09-17 21:06:59 +000064 llvm_unreachable("All cases should be covered in the switch.");
Chris Bienemanad070d02014-09-17 20:55:46 +000065}
66
Meador Inged589ac62012-10-31 03:33:06 +000067/// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
68/// value is equal or not-equal to zero.
69static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000070 for (User *U : V->users()) {
71 if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
Meador Inged589ac62012-10-31 03:33:06 +000072 if (IC->isEquality())
73 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
74 if (C->isNullValue())
75 continue;
76 // Unknown instruction.
77 return false;
78 }
79 return true;
80}
81
Meador Inge56edbc92012-11-11 03:51:48 +000082/// isOnlyUsedInEqualityComparison - Return true if it is only used in equality
83/// comparisons with With.
84static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000085 for (User *U : V->users()) {
86 if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
Meador Inge56edbc92012-11-11 03:51:48 +000087 if (IC->isEquality() && IC->getOperand(1) == With)
88 continue;
89 // Unknown instruction.
90 return false;
91 }
92 return true;
93}
94
Meador Inge08ca1152012-11-26 20:37:20 +000095static bool callHasFloatingPointArgument(const CallInst *CI) {
96 for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
97 it != e; ++it) {
98 if ((*it)->getType()->isFloatingPointTy())
99 return true;
100 }
101 return false;
102}
103
Benjamin Kramer2702caa2013-08-31 18:19:35 +0000104/// \brief Check whether the overloaded unary floating point function
105/// corresponing to \a Ty is available.
106static bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
107 LibFunc::Func DoubleFn, LibFunc::Func FloatFn,
108 LibFunc::Func LongDoubleFn) {
109 switch (Ty->getTypeID()) {
110 case Type::FloatTyID:
111 return TLI->has(FloatFn);
112 case Type::DoubleTyID:
113 return TLI->has(DoubleFn);
114 default:
115 return TLI->has(LongDoubleFn);
116 }
117}
118
Meador Inged589ac62012-10-31 03:33:06 +0000119//===----------------------------------------------------------------------===//
Meador Ingedf796f82012-10-13 16:45:24 +0000120// Fortified Library Call Optimizations
121//===----------------------------------------------------------------------===//
122
Chris Bienemanad070d02014-09-17 20:55:46 +0000123static bool isFortifiedCallFoldable(CallInst *CI, unsigned SizeCIOp, unsigned SizeArgOp,
124 bool isString) {
125 if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
126 return true;
127 if (ConstantInt *SizeCI =
128 dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
129 if (SizeCI->isAllOnesValue())
Meador Ingedf796f82012-10-13 16:45:24 +0000130 return true;
Chris Bienemanad070d02014-09-17 20:55:46 +0000131 if (isString) {
132 uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
133 // If the length is 0 we don't know how long it is and so we can't
134 // remove the check.
135 if (Len == 0)
136 return false;
137 return SizeCI->getZExtValue() >= Len;
Meador Ingedf796f82012-10-13 16:45:24 +0000138 }
Chris Bienemanad070d02014-09-17 20:55:46 +0000139 if (ConstantInt *Arg = dyn_cast<ConstantInt>(CI->getArgOperand(SizeArgOp)))
140 return SizeCI->getZExtValue() >= Arg->getZExtValue();
Meador Ingedf796f82012-10-13 16:45:24 +0000141 }
Chris Bienemanad070d02014-09-17 20:55:46 +0000142 return false;
143}
Meador Ingedf796f82012-10-13 16:45:24 +0000144
Chris Bienemanad070d02014-09-17 20:55:46 +0000145Value *LibCallSimplifier::optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B) {
146 Function *Callee = CI->getCalledFunction();
147 FunctionType *FT = Callee->getFunctionType();
148 LLVMContext &Context = CI->getContext();
Meador Ingedf796f82012-10-13 16:45:24 +0000149
Chris Bienemanad070d02014-09-17 20:55:46 +0000150 // Check if this has the right signature.
151 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
152 !FT->getParamType(0)->isPointerTy() ||
153 !FT->getParamType(1)->isPointerTy() ||
154 FT->getParamType(2) != DL->getIntPtrType(Context) ||
155 FT->getParamType(3) != DL->getIntPtrType(Context))
156 return nullptr;
157
158 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
159 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
160 CI->getArgOperand(2), 1);
161 return CI->getArgOperand(0);
162 }
163 return nullptr;
164}
165
166Value *LibCallSimplifier::optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B) {
167 Function *Callee = CI->getCalledFunction();
168 FunctionType *FT = Callee->getFunctionType();
169 LLVMContext &Context = CI->getContext();
170
171 // Check if this has the right signature.
172 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
173 !FT->getParamType(0)->isPointerTy() ||
174 !FT->getParamType(1)->isPointerTy() ||
175 FT->getParamType(2) != DL->getIntPtrType(Context) ||
176 FT->getParamType(3) != DL->getIntPtrType(Context))
177 return nullptr;
178
179 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
180 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
181 CI->getArgOperand(2), 1);
182 return CI->getArgOperand(0);
183 }
184 return nullptr;
185}
186
187Value *LibCallSimplifier::optimizeMemSetChk(CallInst *CI, IRBuilder<> &B) {
188 Function *Callee = CI->getCalledFunction();
189 FunctionType *FT = Callee->getFunctionType();
190 LLVMContext &Context = CI->getContext();
191
192 // Check if this has the right signature.
193 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
194 !FT->getParamType(0)->isPointerTy() ||
195 !FT->getParamType(1)->isIntegerTy() ||
196 FT->getParamType(2) != DL->getIntPtrType(Context) ||
197 FT->getParamType(3) != DL->getIntPtrType(Context))
198 return nullptr;
199
200 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
201 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
202 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
203 return CI->getArgOperand(0);
204 }
205 return nullptr;
206}
207
208Value *LibCallSimplifier::optimizeStrCpyChk(CallInst *CI, IRBuilder<> &B) {
209 Function *Callee = CI->getCalledFunction();
210 StringRef Name = Callee->getName();
211 FunctionType *FT = Callee->getFunctionType();
212 LLVMContext &Context = CI->getContext();
213
214 // Check if this has the right signature.
215 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
216 FT->getParamType(0) != FT->getParamType(1) ||
217 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
218 FT->getParamType(2) != DL->getIntPtrType(Context))
219 return nullptr;
220
221 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
222 if (Dst == Src) // __strcpy_chk(x,x) -> x
223 return Src;
224
225 // If a) we don't have any length information, or b) we know this will
226 // fit then just lower to a plain strcpy. Otherwise we'll keep our
227 // strcpy_chk call which may fail at runtime if the size is too long.
228 // TODO: It might be nice to get a maximum length out of the possible
229 // string lengths for varying.
230 if (isFortifiedCallFoldable(CI, 2, 1, true)) {
231 Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6));
232 return Ret;
233 } else {
234 // Maybe we can stil fold __strcpy_chk to __memcpy_chk.
235 uint64_t Len = GetStringLength(Src);
236 if (Len == 0)
Craig Topperf40110f2014-04-25 05:29:35 +0000237 return nullptr;
Meador Ingedf796f82012-10-13 16:45:24 +0000238
Chris Bienemanad070d02014-09-17 20:55:46 +0000239 // This optimization require DataLayout.
240 if (!DL)
Craig Topperf40110f2014-04-25 05:29:35 +0000241 return nullptr;
Meador Ingedf796f82012-10-13 16:45:24 +0000242
Chris Bienemanad070d02014-09-17 20:55:46 +0000243 Value *Ret = EmitMemCpyChk(
244 Dst, Src, ConstantInt::get(DL->getIntPtrType(Context), Len),
245 CI->getArgOperand(2), B, DL, TLI);
246 return Ret;
Meador Ingedf796f82012-10-13 16:45:24 +0000247 }
Chris Bienemanad070d02014-09-17 20:55:46 +0000248 return nullptr;
249}
Meador Ingedf796f82012-10-13 16:45:24 +0000250
Chris Bienemanad070d02014-09-17 20:55:46 +0000251Value *LibCallSimplifier::optimizeStpCpyChk(CallInst *CI, IRBuilder<> &B) {
252 Function *Callee = CI->getCalledFunction();
253 StringRef Name = Callee->getName();
254 FunctionType *FT = Callee->getFunctionType();
255 LLVMContext &Context = CI->getContext();
Meador Ingedf796f82012-10-13 16:45:24 +0000256
Chris Bienemanad070d02014-09-17 20:55:46 +0000257 // Check if this has the right signature.
258 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
259 FT->getParamType(0) != FT->getParamType(1) ||
260 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
261 FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0)))
262 return nullptr;
263
264 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
265 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
266 Value *StrLen = EmitStrLen(Src, B, DL, TLI);
267 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : nullptr;
268 }
269
270 // If a) we don't have any length information, or b) we know this will
271 // fit then just lower to a plain stpcpy. Otherwise we'll keep our
272 // stpcpy_chk call which may fail at runtime if the size is too long.
273 // TODO: It might be nice to get a maximum length out of the possible
274 // string lengths for varying.
275 if (isFortifiedCallFoldable(CI, 2, 1, true)) {
276 Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6));
277 return Ret;
278 } else {
279 // Maybe we can stil fold __stpcpy_chk to __memcpy_chk.
280 uint64_t Len = GetStringLength(Src);
281 if (Len == 0)
Craig Topperf40110f2014-04-25 05:29:35 +0000282 return nullptr;
Meador Ingedf796f82012-10-13 16:45:24 +0000283
Chris Bienemanad070d02014-09-17 20:55:46 +0000284 // This optimization require DataLayout.
285 if (!DL)
Craig Topperf40110f2014-04-25 05:29:35 +0000286 return nullptr;
Meador Ingedf796f82012-10-13 16:45:24 +0000287
Chris Bienemanad070d02014-09-17 20:55:46 +0000288 Type *PT = FT->getParamType(0);
289 Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len);
290 Value *DstEnd =
291 B.CreateGEP(Dst, ConstantInt::get(DL->getIntPtrType(PT), Len - 1));
292 if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, DL, TLI))
Craig Topperf40110f2014-04-25 05:29:35 +0000293 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000294 return DstEnd;
Meador Ingecdb2ca52012-10-31 00:20:51 +0000295 }
Chris Bienemanad070d02014-09-17 20:55:46 +0000296 return nullptr;
297}
Meador Ingecdb2ca52012-10-31 00:20:51 +0000298
Chris Bienemanad070d02014-09-17 20:55:46 +0000299Value *LibCallSimplifier::optimizeStrNCpyChk(CallInst *CI, IRBuilder<> &B) {
300 Function *Callee = CI->getCalledFunction();
301 StringRef Name = Callee->getName();
302 FunctionType *FT = Callee->getFunctionType();
303 LLVMContext &Context = CI->getContext();
Meador Ingedf796f82012-10-13 16:45:24 +0000304
Chris Bienemanad070d02014-09-17 20:55:46 +0000305 // Check if this has the right signature.
306 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
307 FT->getParamType(0) != FT->getParamType(1) ||
308 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
309 !FT->getParamType(2)->isIntegerTy() ||
310 FT->getParamType(3) != DL->getIntPtrType(Context))
Craig Topperf40110f2014-04-25 05:29:35 +0000311 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000312
313 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
314 Value *Ret =
315 EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
316 CI->getArgOperand(2), B, DL, TLI, Name.substr(2, 7));
317 return Ret;
Meador Ingedf796f82012-10-13 16:45:24 +0000318 }
Chris Bienemanad070d02014-09-17 20:55:46 +0000319 return nullptr;
320}
Meador Ingedf796f82012-10-13 16:45:24 +0000321
Meador Inge7fb2f732012-10-13 16:45:32 +0000322//===----------------------------------------------------------------------===//
323// String and Memory Library Call Optimizations
324//===----------------------------------------------------------------------===//
325
Chris Bienemanad070d02014-09-17 20:55:46 +0000326Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilder<> &B) {
327 Function *Callee = CI->getCalledFunction();
328 // Verify the "strcat" function prototype.
329 FunctionType *FT = Callee->getFunctionType();
330 if (FT->getNumParams() != 2||
331 FT->getReturnType() != B.getInt8PtrTy() ||
332 FT->getParamType(0) != FT->getReturnType() ||
333 FT->getParamType(1) != FT->getReturnType())
334 return nullptr;
335
336 // Extract some information from the instruction
337 Value *Dst = CI->getArgOperand(0);
338 Value *Src = CI->getArgOperand(1);
339
340 // See if we can get the length of the input string.
341 uint64_t Len = GetStringLength(Src);
342 if (Len == 0)
343 return nullptr;
344 --Len; // Unbias length.
345
346 // Handle the simple, do-nothing case: strcat(x, "") -> x
347 if (Len == 0)
348 return Dst;
349
350 // These optimizations require DataLayout.
351 if (!DL)
352 return nullptr;
353
354 return emitStrLenMemCpy(Src, Dst, Len, B);
355}
356
357Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
358 IRBuilder<> &B) {
359 // We need to find the end of the destination string. That's where the
360 // memory is to be moved to. We just generate a call to strlen.
361 Value *DstLen = EmitStrLen(Dst, B, DL, TLI);
362 if (!DstLen)
363 return nullptr;
364
365 // Now that we have the destination's length, we must index into the
366 // destination's pointer to get the actual memcpy destination (end of
367 // the string .. we're concatenating).
368 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
369
370 // We have enough information to now generate the memcpy call to do the
371 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
372 B.CreateMemCpy(
373 CpyDst, Src,
374 ConstantInt::get(DL->getIntPtrType(Src->getContext()), Len + 1), 1);
375 return Dst;
376}
377
378Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilder<> &B) {
379 Function *Callee = CI->getCalledFunction();
380 // Verify the "strncat" function prototype.
381 FunctionType *FT = Callee->getFunctionType();
382 if (FT->getNumParams() != 3 || FT->getReturnType() != B.getInt8PtrTy() ||
383 FT->getParamType(0) != FT->getReturnType() ||
384 FT->getParamType(1) != FT->getReturnType() ||
385 !FT->getParamType(2)->isIntegerTy())
386 return nullptr;
387
388 // Extract some information from the instruction
389 Value *Dst = CI->getArgOperand(0);
390 Value *Src = CI->getArgOperand(1);
391 uint64_t Len;
392
393 // We don't do anything if length is not constant
394 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
395 Len = LengthArg->getZExtValue();
396 else
397 return nullptr;
398
399 // See if we can get the length of the input string.
400 uint64_t SrcLen = GetStringLength(Src);
401 if (SrcLen == 0)
402 return nullptr;
403 --SrcLen; // Unbias length.
404
405 // Handle the simple, do-nothing cases:
406 // strncat(x, "", c) -> x
407 // strncat(x, c, 0) -> x
408 if (SrcLen == 0 || Len == 0)
409 return Dst;
410
411 // These optimizations require DataLayout.
412 if (!DL)
413 return nullptr;
414
415 // We don't optimize this case
416 if (Len < SrcLen)
417 return nullptr;
418
419 // strncat(x, s, c) -> strcat(x, s)
420 // s is constant so the strcat can be optimized further
421 return emitStrLenMemCpy(Src, Dst, SrcLen, B);
422}
423
424Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) {
425 Function *Callee = CI->getCalledFunction();
426 // Verify the "strchr" function prototype.
427 FunctionType *FT = Callee->getFunctionType();
428 if (FT->getNumParams() != 2 || FT->getReturnType() != B.getInt8PtrTy() ||
429 FT->getParamType(0) != FT->getReturnType() ||
430 !FT->getParamType(1)->isIntegerTy(32))
431 return nullptr;
432
433 Value *SrcStr = CI->getArgOperand(0);
434
435 // If the second operand is non-constant, see if we can compute the length
436 // of the input string and turn this into memchr.
437 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
438 if (!CharC) {
439 // These optimizations require DataLayout.
440 if (!DL)
Craig Topperf40110f2014-04-25 05:29:35 +0000441 return nullptr;
Meador Inge7fb2f732012-10-13 16:45:32 +0000442
Chris Bienemanad070d02014-09-17 20:55:46 +0000443 uint64_t Len = GetStringLength(SrcStr);
444 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32.
445 return nullptr;
Meador Inge7fb2f732012-10-13 16:45:32 +0000446
Chris Bienemanad070d02014-09-17 20:55:46 +0000447 return EmitMemChr(
448 SrcStr, CI->getArgOperand(1), // include nul.
449 ConstantInt::get(DL->getIntPtrType(CI->getContext()), Len), B, DL, TLI);
Meador Inge7fb2f732012-10-13 16:45:32 +0000450 }
451
Chris Bienemanad070d02014-09-17 20:55:46 +0000452 // Otherwise, the character is a constant, see if the first argument is
453 // a string literal. If so, we can constant fold.
454 StringRef Str;
455 if (!getConstantStringInfo(SrcStr, Str)) {
456 if (DL && CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
457 return B.CreateGEP(SrcStr, EmitStrLen(SrcStr, B, DL, TLI), "strchr");
458 return nullptr;
459 }
460
461 // Compute the offset, make sure to handle the case when we're searching for
462 // zero (a weird way to spell strlen).
463 size_t I = (0xFF & CharC->getSExtValue()) == 0
464 ? Str.size()
465 : Str.find(CharC->getSExtValue());
466 if (I == StringRef::npos) // Didn't find the char. strchr returns null.
467 return Constant::getNullValue(CI->getType());
468
469 // strchr(s+n,c) -> gep(s+n+i,c)
470 return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
471}
472
473Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilder<> &B) {
474 Function *Callee = CI->getCalledFunction();
475 // Verify the "strrchr" function prototype.
476 FunctionType *FT = Callee->getFunctionType();
477 if (FT->getNumParams() != 2 || FT->getReturnType() != B.getInt8PtrTy() ||
478 FT->getParamType(0) != FT->getReturnType() ||
479 !FT->getParamType(1)->isIntegerTy(32))
480 return nullptr;
481
482 Value *SrcStr = CI->getArgOperand(0);
483 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
484
485 // Cannot fold anything if we're not looking for a constant.
486 if (!CharC)
487 return nullptr;
488
489 StringRef Str;
490 if (!getConstantStringInfo(SrcStr, Str)) {
491 // strrchr(s, 0) -> strchr(s, 0)
492 if (DL && CharC->isZero())
493 return EmitStrChr(SrcStr, '\0', B, DL, TLI);
494 return nullptr;
495 }
496
497 // Compute the offset.
498 size_t I = (0xFF & CharC->getSExtValue()) == 0
499 ? Str.size()
500 : Str.rfind(CharC->getSExtValue());
501 if (I == StringRef::npos) // Didn't find the char. Return null.
502 return Constant::getNullValue(CI->getType());
503
504 // strrchr(s+n,c) -> gep(s+n+i,c)
505 return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
506}
507
508Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilder<> &B) {
509 Function *Callee = CI->getCalledFunction();
510 // Verify the "strcmp" function prototype.
511 FunctionType *FT = Callee->getFunctionType();
512 if (FT->getNumParams() != 2 || !FT->getReturnType()->isIntegerTy(32) ||
513 FT->getParamType(0) != FT->getParamType(1) ||
514 FT->getParamType(0) != B.getInt8PtrTy())
515 return nullptr;
516
517 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
518 if (Str1P == Str2P) // strcmp(x,x) -> 0
519 return ConstantInt::get(CI->getType(), 0);
520
521 StringRef Str1, Str2;
522 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
523 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
524
525 // strcmp(x, y) -> cnst (if both x and y are constant strings)
526 if (HasStr1 && HasStr2)
527 return ConstantInt::get(CI->getType(), Str1.compare(Str2));
528
529 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
530 return B.CreateNeg(
531 B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()));
532
533 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
534 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
535
536 // strcmp(P, "x") -> memcmp(P, "x", 2)
537 uint64_t Len1 = GetStringLength(Str1P);
538 uint64_t Len2 = GetStringLength(Str2P);
539 if (Len1 && Len2) {
540 // These optimizations require DataLayout.
541 if (!DL)
Craig Topperf40110f2014-04-25 05:29:35 +0000542 return nullptr;
Meador Inge7fb2f732012-10-13 16:45:32 +0000543
Chris Bienemanad070d02014-09-17 20:55:46 +0000544 return EmitMemCmp(Str1P, Str2P,
545 ConstantInt::get(DL->getIntPtrType(CI->getContext()),
546 std::min(Len1, Len2)),
547 B, DL, TLI);
548 }
Meador Inge7fb2f732012-10-13 16:45:32 +0000549
Chris Bienemanad070d02014-09-17 20:55:46 +0000550 return nullptr;
551}
552
553Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilder<> &B) {
554 Function *Callee = CI->getCalledFunction();
555 // Verify the "strncmp" function prototype.
556 FunctionType *FT = Callee->getFunctionType();
557 if (FT->getNumParams() != 3 || !FT->getReturnType()->isIntegerTy(32) ||
558 FT->getParamType(0) != FT->getParamType(1) ||
559 FT->getParamType(0) != B.getInt8PtrTy() ||
560 !FT->getParamType(2)->isIntegerTy())
561 return nullptr;
562
563 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
564 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
565 return ConstantInt::get(CI->getType(), 0);
566
567 // Get the length argument if it is constant.
568 uint64_t Length;
569 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
570 Length = LengthArg->getZExtValue();
571 else
572 return nullptr;
573
574 if (Length == 0) // strncmp(x,y,0) -> 0
575 return ConstantInt::get(CI->getType(), 0);
576
577 if (DL && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
578 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI);
579
580 StringRef Str1, Str2;
581 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
582 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
583
584 // strncmp(x, y) -> cnst (if both x and y are constant strings)
585 if (HasStr1 && HasStr2) {
586 StringRef SubStr1 = Str1.substr(0, Length);
587 StringRef SubStr2 = Str2.substr(0, Length);
588 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
589 }
590
591 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
592 return B.CreateNeg(
593 B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()));
594
595 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
596 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
597
598 return nullptr;
599}
600
601Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilder<> &B) {
602 Function *Callee = CI->getCalledFunction();
603 // Verify the "strcpy" function prototype.
604 FunctionType *FT = Callee->getFunctionType();
605 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
606 FT->getParamType(0) != FT->getParamType(1) ||
607 FT->getParamType(0) != B.getInt8PtrTy())
608 return nullptr;
609
610 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
611 if (Dst == Src) // strcpy(x,x) -> x
612 return Src;
613
614 // These optimizations require DataLayout.
615 if (!DL)
616 return nullptr;
617
618 // See if we can get the length of the input string.
619 uint64_t Len = GetStringLength(Src);
620 if (Len == 0)
621 return nullptr;
622
623 // We have enough information to now generate the memcpy call to do the
624 // copy for us. Make a memcpy to copy the nul byte with align = 1.
625 B.CreateMemCpy(Dst, Src,
626 ConstantInt::get(DL->getIntPtrType(CI->getContext()), Len), 1);
627 return Dst;
628}
629
630Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilder<> &B) {
631 Function *Callee = CI->getCalledFunction();
632 // Verify the "stpcpy" function prototype.
633 FunctionType *FT = Callee->getFunctionType();
634 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
635 FT->getParamType(0) != FT->getParamType(1) ||
636 FT->getParamType(0) != B.getInt8PtrTy())
637 return nullptr;
638
639 // These optimizations require DataLayout.
640 if (!DL)
641 return nullptr;
642
643 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
644 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
645 Value *StrLen = EmitStrLen(Src, B, DL, TLI);
646 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : nullptr;
647 }
648
649 // See if we can get the length of the input string.
650 uint64_t Len = GetStringLength(Src);
651 if (Len == 0)
652 return nullptr;
653
654 Type *PT = FT->getParamType(0);
655 Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len);
656 Value *DstEnd =
657 B.CreateGEP(Dst, ConstantInt::get(DL->getIntPtrType(PT), Len - 1));
658
659 // We have enough information to now generate the memcpy call to do the
660 // copy for us. Make a memcpy to copy the nul byte with align = 1.
661 B.CreateMemCpy(Dst, Src, LenV, 1);
662 return DstEnd;
663}
664
665Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) {
666 Function *Callee = CI->getCalledFunction();
667 FunctionType *FT = Callee->getFunctionType();
668 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
669 FT->getParamType(0) != FT->getParamType(1) ||
670 FT->getParamType(0) != B.getInt8PtrTy() ||
671 !FT->getParamType(2)->isIntegerTy())
672 return nullptr;
673
674 Value *Dst = CI->getArgOperand(0);
675 Value *Src = CI->getArgOperand(1);
676 Value *LenOp = CI->getArgOperand(2);
677
678 // See if we can get the length of the input string.
679 uint64_t SrcLen = GetStringLength(Src);
680 if (SrcLen == 0)
681 return nullptr;
682 --SrcLen;
683
684 if (SrcLen == 0) {
685 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
686 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
Meador Inge7fb2f732012-10-13 16:45:32 +0000687 return Dst;
688 }
Meador Inge7fb2f732012-10-13 16:45:32 +0000689
Chris Bienemanad070d02014-09-17 20:55:46 +0000690 uint64_t Len;
691 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
692 Len = LengthArg->getZExtValue();
693 else
694 return nullptr;
Meador Inge7fb2f732012-10-13 16:45:32 +0000695
Chris Bienemanad070d02014-09-17 20:55:46 +0000696 if (Len == 0)
697 return Dst; // strncpy(x, y, 0) -> x
Meador Inge7fb2f732012-10-13 16:45:32 +0000698
Chris Bienemanad070d02014-09-17 20:55:46 +0000699 // These optimizations require DataLayout.
700 if (!DL)
701 return nullptr;
Meador Inge7fb2f732012-10-13 16:45:32 +0000702
Chris Bienemanad070d02014-09-17 20:55:46 +0000703 // Let strncpy handle the zero padding
704 if (Len > SrcLen + 1)
705 return nullptr;
Meador Inge7fb2f732012-10-13 16:45:32 +0000706
Chris Bienemanad070d02014-09-17 20:55:46 +0000707 Type *PT = FT->getParamType(0);
708 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
709 B.CreateMemCpy(Dst, Src, ConstantInt::get(DL->getIntPtrType(PT), Len), 1);
Meador Inge7fb2f732012-10-13 16:45:32 +0000710
Chris Bienemanad070d02014-09-17 20:55:46 +0000711 return Dst;
712}
Meador Inge7fb2f732012-10-13 16:45:32 +0000713
Chris Bienemanad070d02014-09-17 20:55:46 +0000714Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilder<> &B) {
715 Function *Callee = CI->getCalledFunction();
716 FunctionType *FT = Callee->getFunctionType();
717 if (FT->getNumParams() != 1 || FT->getParamType(0) != B.getInt8PtrTy() ||
718 !FT->getReturnType()->isIntegerTy())
719 return nullptr;
Meador Inge7fb2f732012-10-13 16:45:32 +0000720
Chris Bienemanad070d02014-09-17 20:55:46 +0000721 Value *Src = CI->getArgOperand(0);
722
723 // Constant folding: strlen("xyz") -> 3
724 if (uint64_t Len = GetStringLength(Src))
725 return ConstantInt::get(CI->getType(), Len - 1);
726
727 // strlen(x?"foo":"bars") --> x ? 3 : 4
728 if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {
729 uint64_t LenTrue = GetStringLength(SI->getTrueValue());
730 uint64_t LenFalse = GetStringLength(SI->getFalseValue());
731 if (LenTrue && LenFalse) {
732 Function *Caller = CI->getParent()->getParent();
733 emitOptimizationRemark(CI->getContext(), "simplify-libcalls", *Caller,
734 SI->getDebugLoc(),
735 "folded strlen(select) to select of constants");
736 return B.CreateSelect(SI->getCondition(),
737 ConstantInt::get(CI->getType(), LenTrue - 1),
738 ConstantInt::get(CI->getType(), LenFalse - 1));
739 }
Meador Inge7fb2f732012-10-13 16:45:32 +0000740 }
Meador Inge7fb2f732012-10-13 16:45:32 +0000741
Chris Bienemanad070d02014-09-17 20:55:46 +0000742 // strlen(x) != 0 --> *x != 0
743 // strlen(x) == 0 --> *x == 0
744 if (isOnlyUsedInZeroEqualityComparison(CI))
745 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
Meador Inge17418502012-10-13 16:45:37 +0000746
Chris Bienemanad070d02014-09-17 20:55:46 +0000747 return nullptr;
748}
Meador Inge17418502012-10-13 16:45:37 +0000749
Chris Bienemanad070d02014-09-17 20:55:46 +0000750Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilder<> &B) {
751 Function *Callee = CI->getCalledFunction();
752 FunctionType *FT = Callee->getFunctionType();
753 if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||
754 FT->getParamType(1) != FT->getParamType(0) ||
755 FT->getReturnType() != FT->getParamType(0))
756 return nullptr;
Meador Inge17418502012-10-13 16:45:37 +0000757
Chris Bienemanad070d02014-09-17 20:55:46 +0000758 StringRef S1, S2;
759 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
760 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
Meador Inge17418502012-10-13 16:45:37 +0000761
Chris Bienemanad070d02014-09-17 20:55:46 +0000762 // strpbrk(s, "") -> NULL
763 // strpbrk("", s) -> NULL
764 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
765 return Constant::getNullValue(CI->getType());
Meador Inge17418502012-10-13 16:45:37 +0000766
Chris Bienemanad070d02014-09-17 20:55:46 +0000767 // Constant folding.
768 if (HasS1 && HasS2) {
769 size_t I = S1.find_first_of(S2);
770 if (I == StringRef::npos) // No match.
Meador Inge17418502012-10-13 16:45:37 +0000771 return Constant::getNullValue(CI->getType());
772
Chris Bienemanad070d02014-09-17 20:55:46 +0000773 return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
Meador Inge17418502012-10-13 16:45:37 +0000774 }
Meador Inge17418502012-10-13 16:45:37 +0000775
Chris Bienemanad070d02014-09-17 20:55:46 +0000776 // strpbrk(s, "a") -> strchr(s, 'a')
777 if (DL && HasS2 && S2.size() == 1)
778 return EmitStrChr(CI->getArgOperand(0), S2[0], B, DL, TLI);
779
780 return nullptr;
781}
782
783Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilder<> &B) {
784 Function *Callee = CI->getCalledFunction();
785 FunctionType *FT = Callee->getFunctionType();
786 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
787 !FT->getParamType(0)->isPointerTy() ||
788 !FT->getParamType(1)->isPointerTy())
789 return nullptr;
790
791 Value *EndPtr = CI->getArgOperand(1);
792 if (isa<ConstantPointerNull>(EndPtr)) {
793 // With a null EndPtr, this function won't capture the main argument.
794 // It would be readonly too, except that it still may write to errno.
795 CI->addAttribute(1, Attribute::NoCapture);
796 }
797
798 return nullptr;
799}
800
801Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilder<> &B) {
802 Function *Callee = CI->getCalledFunction();
803 FunctionType *FT = Callee->getFunctionType();
804 if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||
805 FT->getParamType(1) != FT->getParamType(0) ||
806 !FT->getReturnType()->isIntegerTy())
807 return nullptr;
808
809 StringRef S1, S2;
810 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
811 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
812
813 // strspn(s, "") -> 0
814 // strspn("", s) -> 0
815 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
816 return Constant::getNullValue(CI->getType());
817
818 // Constant folding.
819 if (HasS1 && HasS2) {
820 size_t Pos = S1.find_first_not_of(S2);
821 if (Pos == StringRef::npos)
822 Pos = S1.size();
823 return ConstantInt::get(CI->getType(), Pos);
824 }
825
826 return nullptr;
827}
828
829Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilder<> &B) {
830 Function *Callee = CI->getCalledFunction();
831 FunctionType *FT = Callee->getFunctionType();
832 if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||
833 FT->getParamType(1) != FT->getParamType(0) ||
834 !FT->getReturnType()->isIntegerTy())
835 return nullptr;
836
837 StringRef S1, S2;
838 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
839 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
840
841 // strcspn("", s) -> 0
842 if (HasS1 && S1.empty())
843 return Constant::getNullValue(CI->getType());
844
845 // Constant folding.
846 if (HasS1 && HasS2) {
847 size_t Pos = S1.find_first_of(S2);
848 if (Pos == StringRef::npos)
849 Pos = S1.size();
850 return ConstantInt::get(CI->getType(), Pos);
851 }
852
853 // strcspn(s, "") -> strlen(s)
854 if (DL && HasS2 && S2.empty())
855 return EmitStrLen(CI->getArgOperand(0), B, DL, TLI);
856
857 return nullptr;
858}
859
860Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) {
861 Function *Callee = CI->getCalledFunction();
862 FunctionType *FT = Callee->getFunctionType();
863 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
864 !FT->getParamType(1)->isPointerTy() ||
865 !FT->getReturnType()->isPointerTy())
866 return nullptr;
867
868 // fold strstr(x, x) -> x.
869 if (CI->getArgOperand(0) == CI->getArgOperand(1))
870 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
871
872 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
873 if (DL && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
874 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, DL, TLI);
875 if (!StrLen)
Craig Topperf40110f2014-04-25 05:29:35 +0000876 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000877 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
878 StrLen, B, DL, TLI);
879 if (!StrNCmp)
Craig Topperf40110f2014-04-25 05:29:35 +0000880 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000881 for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) {
882 ICmpInst *Old = cast<ICmpInst>(*UI++);
883 Value *Cmp =
884 B.CreateICmp(Old->getPredicate(), StrNCmp,
885 ConstantInt::getNullValue(StrNCmp->getType()), "cmp");
886 replaceAllUsesWith(Old, Cmp);
Meador Inge17418502012-10-13 16:45:37 +0000887 }
Chris Bienemanad070d02014-09-17 20:55:46 +0000888 return CI;
889 }
Meador Inge17418502012-10-13 16:45:37 +0000890
Chris Bienemanad070d02014-09-17 20:55:46 +0000891 // See if either input string is a constant string.
892 StringRef SearchStr, ToFindStr;
893 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
894 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
895
896 // fold strstr(x, "") -> x.
897 if (HasStr2 && ToFindStr.empty())
898 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
899
900 // If both strings are known, constant fold it.
901 if (HasStr1 && HasStr2) {
902 size_t Offset = SearchStr.find(ToFindStr);
903
904 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
Meador Inge17418502012-10-13 16:45:37 +0000905 return Constant::getNullValue(CI->getType());
906
Chris Bienemanad070d02014-09-17 20:55:46 +0000907 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
908 Value *Result = CastToCStr(CI->getArgOperand(0), B);
909 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
910 return B.CreateBitCast(Result, CI->getType());
Meador Inge17418502012-10-13 16:45:37 +0000911 }
Meador Inge17418502012-10-13 16:45:37 +0000912
Chris Bienemanad070d02014-09-17 20:55:46 +0000913 // fold strstr(x, "y") -> strchr(x, 'y').
914 if (HasStr2 && ToFindStr.size() == 1) {
915 Value *StrChr = EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, DL, TLI);
916 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr;
917 }
918 return nullptr;
919}
Meador Inge40b6fac2012-10-15 03:47:37 +0000920
Chris Bienemanad070d02014-09-17 20:55:46 +0000921Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) {
922 Function *Callee = CI->getCalledFunction();
923 FunctionType *FT = Callee->getFunctionType();
924 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
925 !FT->getParamType(1)->isPointerTy() ||
926 !FT->getReturnType()->isIntegerTy(32))
Craig Topperf40110f2014-04-25 05:29:35 +0000927 return nullptr;
Meador Inge40b6fac2012-10-15 03:47:37 +0000928
Chris Bienemanad070d02014-09-17 20:55:46 +0000929 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
Meador Inge40b6fac2012-10-15 03:47:37 +0000930
Chris Bienemanad070d02014-09-17 20:55:46 +0000931 if (LHS == RHS) // memcmp(s,s,x) -> 0
932 return Constant::getNullValue(CI->getType());
Meador Inge40b6fac2012-10-15 03:47:37 +0000933
Chris Bienemanad070d02014-09-17 20:55:46 +0000934 // Make sure we have a constant length.
935 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
936 if (!LenC)
Craig Topperf40110f2014-04-25 05:29:35 +0000937 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000938 uint64_t Len = LenC->getZExtValue();
939
940 if (Len == 0) // memcmp(s1,s2,0) -> 0
941 return Constant::getNullValue(CI->getType());
942
943 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
944 if (Len == 1) {
945 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
946 CI->getType(), "lhsv");
947 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
948 CI->getType(), "rhsv");
949 return B.CreateSub(LHSV, RHSV, "chardiff");
Meador Inge40b6fac2012-10-15 03:47:37 +0000950 }
Meador Inge40b6fac2012-10-15 03:47:37 +0000951
Chris Bienemanad070d02014-09-17 20:55:46 +0000952 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
953 StringRef LHSStr, RHSStr;
954 if (getConstantStringInfo(LHS, LHSStr) &&
955 getConstantStringInfo(RHS, RHSStr)) {
956 // Make sure we're not reading out-of-bounds memory.
957 if (Len > LHSStr.size() || Len > RHSStr.size())
Craig Topperf40110f2014-04-25 05:29:35 +0000958 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000959 // Fold the memcmp and normalize the result. This way we get consistent
960 // results across multiple platforms.
961 uint64_t Ret = 0;
962 int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
963 if (Cmp < 0)
964 Ret = -1;
965 else if (Cmp > 0)
966 Ret = 1;
967 return ConstantInt::get(CI->getType(), Ret);
Meador Inge000dbcc2012-10-18 18:12:40 +0000968 }
Meador Inge000dbcc2012-10-18 18:12:40 +0000969
Chris Bienemanad070d02014-09-17 20:55:46 +0000970 return nullptr;
971}
Meador Inge9a6a1902012-10-31 00:20:56 +0000972
Chris Bienemanad070d02014-09-17 20:55:46 +0000973Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B) {
974 Function *Callee = CI->getCalledFunction();
975 // These optimizations require DataLayout.
976 if (!DL)
Craig Topperf40110f2014-04-25 05:29:35 +0000977 return nullptr;
Meador Inged589ac62012-10-31 03:33:06 +0000978
Chris Bienemanad070d02014-09-17 20:55:46 +0000979 FunctionType *FT = Callee->getFunctionType();
980 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
981 !FT->getParamType(0)->isPointerTy() ||
982 !FT->getParamType(1)->isPointerTy() ||
983 FT->getParamType(2) != DL->getIntPtrType(CI->getContext()))
Craig Topperf40110f2014-04-25 05:29:35 +0000984 return nullptr;
Meador Inge6f8e0112012-10-31 04:29:58 +0000985
Chris Bienemanad070d02014-09-17 20:55:46 +0000986 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
987 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
988 CI->getArgOperand(2), 1);
989 return CI->getArgOperand(0);
990}
Meador Inge05a625a2012-10-31 14:58:26 +0000991
Chris Bienemanad070d02014-09-17 20:55:46 +0000992Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilder<> &B) {
993 Function *Callee = CI->getCalledFunction();
994 // These optimizations require DataLayout.
995 if (!DL)
Craig Topperf40110f2014-04-25 05:29:35 +0000996 return nullptr;
Meador Inge05a625a2012-10-31 14:58:26 +0000997
Chris Bienemanad070d02014-09-17 20:55:46 +0000998 FunctionType *FT = Callee->getFunctionType();
999 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1000 !FT->getParamType(0)->isPointerTy() ||
1001 !FT->getParamType(1)->isPointerTy() ||
1002 FT->getParamType(2) != DL->getIntPtrType(CI->getContext()))
Craig Topperf40110f2014-04-25 05:29:35 +00001003 return nullptr;
Meador Inge489b5d62012-11-08 01:33:50 +00001004
Chris Bienemanad070d02014-09-17 20:55:46 +00001005 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
1006 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
1007 CI->getArgOperand(2), 1);
1008 return CI->getArgOperand(0);
1009}
Meador Ingebcd88ef72012-11-10 15:16:48 +00001010
Chris Bienemanad070d02014-09-17 20:55:46 +00001011Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B) {
1012 Function *Callee = CI->getCalledFunction();
1013 // These optimizations require DataLayout.
1014 if (!DL)
Craig Topperf40110f2014-04-25 05:29:35 +00001015 return nullptr;
Meador Ingebcd88ef72012-11-10 15:16:48 +00001016
Chris Bienemanad070d02014-09-17 20:55:46 +00001017 FunctionType *FT = Callee->getFunctionType();
1018 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1019 !FT->getParamType(0)->isPointerTy() ||
1020 !FT->getParamType(1)->isIntegerTy() ||
1021 FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0)))
Craig Topperf40110f2014-04-25 05:29:35 +00001022 return nullptr;
Meador Inge56edbc92012-11-11 03:51:48 +00001023
Chris Bienemanad070d02014-09-17 20:55:46 +00001024 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
1025 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1026 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
1027 return CI->getArgOperand(0);
1028}
Meador Inged4825782012-11-11 06:49:03 +00001029
Meador Inge193e0352012-11-13 04:16:17 +00001030//===----------------------------------------------------------------------===//
1031// Math Library Optimizations
1032//===----------------------------------------------------------------------===//
1033
1034//===----------------------------------------------------------------------===//
1035// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1036
Chris Bienemanad070d02014-09-17 20:55:46 +00001037Value *LibCallSimplifier::optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B,
1038 bool CheckRetType) {
1039 Function *Callee = CI->getCalledFunction();
1040 FunctionType *FT = Callee->getFunctionType();
1041 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1042 !FT->getParamType(0)->isDoubleTy())
1043 return nullptr;
Meador Inge193e0352012-11-13 04:16:17 +00001044
Chris Bienemanad070d02014-09-17 20:55:46 +00001045 if (CheckRetType) {
1046 // Check if all the uses for function like 'sin' are converted to float.
1047 for (User *U : CI->users()) {
1048 FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
1049 if (!Cast || !Cast->getType()->isFloatTy())
1050 return nullptr;
Meador Inge193e0352012-11-13 04:16:17 +00001051 }
Meador Inge193e0352012-11-13 04:16:17 +00001052 }
Chris Bienemanad070d02014-09-17 20:55:46 +00001053
1054 // If this is something like 'floor((double)floatval)', convert to floorf.
1055 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
1056 if (!Cast || !Cast->getOperand(0)->getType()->isFloatTy())
1057 return nullptr;
1058
1059 // floor((double)floatval) -> (double)floorf(floatval)
1060 Value *V = Cast->getOperand(0);
1061 V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
1062 return B.CreateFPExt(V, B.getDoubleTy());
1063}
Meador Inge193e0352012-11-13 04:16:17 +00001064
Yi Jiang6ab044e2013-12-16 22:42:40 +00001065// Double -> Float Shrinking Optimizations for Binary Functions like 'fmin/fmax'
Chris Bienemanad070d02014-09-17 20:55:46 +00001066Value *LibCallSimplifier::optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B) {
1067 Function *Callee = CI->getCalledFunction();
1068 FunctionType *FT = Callee->getFunctionType();
1069 // Just make sure this has 2 arguments of the same FP type, which match the
1070 // result type.
1071 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1072 FT->getParamType(0) != FT->getParamType(1) ||
1073 !FT->getParamType(0)->isFloatingPointTy())
Craig Topperf40110f2014-04-25 05:29:35 +00001074 return nullptr;
Meador Inge193e0352012-11-13 04:16:17 +00001075
Chris Bienemanad070d02014-09-17 20:55:46 +00001076 // If this is something like 'fmin((double)floatval1, (double)floatval2)',
1077 // we convert it to fminf.
1078 FPExtInst *Cast1 = dyn_cast<FPExtInst>(CI->getArgOperand(0));
1079 FPExtInst *Cast2 = dyn_cast<FPExtInst>(CI->getArgOperand(1));
1080 if (!Cast1 || !Cast1->getOperand(0)->getType()->isFloatTy() || !Cast2 ||
1081 !Cast2->getOperand(0)->getType()->isFloatTy())
Craig Topperf40110f2014-04-25 05:29:35 +00001082 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +00001083
1084 // fmin((double)floatval1, (double)floatval2)
1085 // -> (double)fmin(floatval1, floatval2)
1086 Value *V = nullptr;
1087 Value *V1 = Cast1->getOperand(0);
1088 Value *V2 = Cast2->getOperand(0);
1089 V = EmitBinaryFloatFnCall(V1, V2, Callee->getName(), B,
1090 Callee->getAttributes());
1091 return B.CreateFPExt(V, B.getDoubleTy());
1092}
1093
1094Value *LibCallSimplifier::optimizeCos(CallInst *CI, IRBuilder<> &B) {
1095 Function *Callee = CI->getCalledFunction();
1096 Value *Ret = nullptr;
1097 if (UnsafeFPShrink && Callee->getName() == "cos" && TLI->has(LibFunc::cosf)) {
1098 Ret = optimizeUnaryDoubleFP(CI, B, true);
Bob Wilsond8d92d92013-11-03 06:48:38 +00001099 }
1100
Chris Bienemanad070d02014-09-17 20:55:46 +00001101 FunctionType *FT = Callee->getFunctionType();
1102 // Just make sure this has 1 argument of FP type, which matches the
1103 // result type.
1104 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1105 !FT->getParamType(0)->isFloatingPointTy())
1106 return Ret;
Bob Wilsond8d92d92013-11-03 06:48:38 +00001107
Chris Bienemanad070d02014-09-17 20:55:46 +00001108 // cos(-x) -> cos(x)
1109 Value *Op1 = CI->getArgOperand(0);
1110 if (BinaryOperator::isFNeg(Op1)) {
1111 BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
1112 return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
1113 }
1114 return Ret;
1115}
Bob Wilsond8d92d92013-11-03 06:48:38 +00001116
Chris Bienemanad070d02014-09-17 20:55:46 +00001117Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {
1118 Function *Callee = CI->getCalledFunction();
1119
1120 Value *Ret = nullptr;
1121 if (UnsafeFPShrink && Callee->getName() == "pow" && TLI->has(LibFunc::powf)) {
1122 Ret = optimizeUnaryDoubleFP(CI, B, true);
Bob Wilsond8d92d92013-11-03 06:48:38 +00001123 }
1124
Chris Bienemanad070d02014-09-17 20:55:46 +00001125 FunctionType *FT = Callee->getFunctionType();
1126 // Just make sure this has 2 arguments of the same FP type, which match the
1127 // result type.
1128 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1129 FT->getParamType(0) != FT->getParamType(1) ||
1130 !FT->getParamType(0)->isFloatingPointTy())
1131 return Ret;
Bob Wilsond8d92d92013-11-03 06:48:38 +00001132
Chris Bienemanad070d02014-09-17 20:55:46 +00001133 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
1134 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1135 // pow(1.0, x) -> 1.0
1136 if (Op1C->isExactlyValue(1.0))
1137 return Op1C;
1138 // pow(2.0, x) -> exp2(x)
1139 if (Op1C->isExactlyValue(2.0) &&
1140 hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp2, LibFunc::exp2f,
1141 LibFunc::exp2l))
1142 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
1143 // pow(10.0, x) -> exp10(x)
1144 if (Op1C->isExactlyValue(10.0) &&
1145 hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f,
1146 LibFunc::exp10l))
1147 return EmitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B,
1148 Callee->getAttributes());
Bob Wilsond8d92d92013-11-03 06:48:38 +00001149 }
1150
Chris Bienemanad070d02014-09-17 20:55:46 +00001151 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1152 if (!Op2C)
1153 return Ret;
1154
1155 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
1156 return ConstantFP::get(CI->getType(), 1.0);
1157
1158 if (Op2C->isExactlyValue(0.5) &&
1159 hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::sqrt, LibFunc::sqrtf,
1160 LibFunc::sqrtl) &&
1161 hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::fabs, LibFunc::fabsf,
1162 LibFunc::fabsl)) {
1163 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1164 // This is faster than calling pow, and still handles negative zero
1165 // and negative infinity correctly.
1166 // TODO: In fast-math mode, this could be just sqrt(x).
1167 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
1168 Value *Inf = ConstantFP::getInfinity(CI->getType());
1169 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
1170 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B, Callee->getAttributes());
1171 Value *FAbs =
1172 EmitUnaryFloatFnCall(Sqrt, "fabs", B, Callee->getAttributes());
1173 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
1174 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
1175 return Sel;
Bob Wilsond8d92d92013-11-03 06:48:38 +00001176 }
1177
Chris Bienemanad070d02014-09-17 20:55:46 +00001178 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1179 return Op1;
1180 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
1181 return B.CreateFMul(Op1, Op1, "pow2");
1182 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
1183 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip");
1184 return nullptr;
1185}
Bob Wilsond8d92d92013-11-03 06:48:38 +00001186
Chris Bienemanad070d02014-09-17 20:55:46 +00001187Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) {
1188 Function *Callee = CI->getCalledFunction();
1189 Function *Caller = CI->getParent()->getParent();
Bob Wilsond8d92d92013-11-03 06:48:38 +00001190
Chris Bienemanad070d02014-09-17 20:55:46 +00001191 Value *Ret = nullptr;
1192 if (UnsafeFPShrink && Callee->getName() == "exp2" &&
1193 TLI->has(LibFunc::exp2f)) {
1194 Ret = optimizeUnaryDoubleFP(CI, B, true);
Bob Wilsond8d92d92013-11-03 06:48:38 +00001195 }
1196
Chris Bienemanad070d02014-09-17 20:55:46 +00001197 FunctionType *FT = Callee->getFunctionType();
1198 // Just make sure this has 1 argument of FP type, which matches the
1199 // result type.
1200 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1201 !FT->getParamType(0)->isFloatingPointTy())
1202 return Ret;
1203
1204 Value *Op = CI->getArgOperand(0);
1205 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1206 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1207 LibFunc::Func LdExp = LibFunc::ldexpl;
1208 if (Op->getType()->isFloatTy())
1209 LdExp = LibFunc::ldexpf;
1210 else if (Op->getType()->isDoubleTy())
1211 LdExp = LibFunc::ldexp;
1212
1213 if (TLI->has(LdExp)) {
1214 Value *LdExpArg = nullptr;
1215 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1216 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
1217 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
1218 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1219 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
1220 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
1221 }
1222
1223 if (LdExpArg) {
1224 Constant *One = ConstantFP::get(CI->getContext(), APFloat(1.0f));
1225 if (!Op->getType()->isFloatTy())
1226 One = ConstantExpr::getFPExtend(One, Op->getType());
1227
1228 Module *M = Caller->getParent();
1229 Value *Callee =
1230 M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),
1231 Op->getType(), B.getInt32Ty(), NULL);
1232 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1233 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1234 CI->setCallingConv(F->getCallingConv());
1235
1236 return CI;
1237 }
1238 }
1239 return Ret;
1240}
1241
Sanjay Patel0ca42bb2014-10-14 20:43:11 +00001242Value *LibCallSimplifier::optimizeFabs(CallInst *CI, IRBuilder<> &B) {
1243 Function *Callee = CI->getCalledFunction();
1244
1245 Value *Ret = nullptr;
1246 if (Callee->getName() == "fabs" && TLI->has(LibFunc::fabsf)) {
1247 Ret = optimizeUnaryDoubleFP(CI, B, false);
1248 }
1249
1250 FunctionType *FT = Callee->getFunctionType();
1251 // Make sure this has 1 argument of FP type which matches the result type.
1252 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1253 !FT->getParamType(0)->isFloatingPointTy())
1254 return Ret;
1255
1256 Value *Op = CI->getArgOperand(0);
1257 if (Instruction *I = dyn_cast<Instruction>(Op)) {
1258 // Fold fabs(x * x) -> x * x; any squared FP value must already be positive.
1259 if (I->getOpcode() == Instruction::FMul)
1260 if (I->getOperand(0) == I->getOperand(1))
1261 return Op;
1262 }
1263 return Ret;
1264}
1265
Sanjay Patelc699a612014-10-16 18:48:17 +00001266Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilder<> &B) {
1267 Function *Callee = CI->getCalledFunction();
1268
1269 Value *Ret = nullptr;
1270 if (UnsafeFPShrink && Callee->getName() == "sqrt" &&
1271 TLI->has(LibFunc::sqrtf)) {
1272 Ret = optimizeUnaryDoubleFP(CI, B, true);
1273 }
1274
1275 // FIXME: For finer-grain optimization, we need intrinsics to have the same
1276 // fast-math flag decorations that are applied to FP instructions. For now,
1277 // we have to rely on the function-level unsafe-fp-math attribute to do this
1278 // optimization because there's no other way to express that the sqrt can be
1279 // reassociated.
1280 Function *F = CI->getParent()->getParent();
1281 if (F->hasFnAttribute("unsafe-fp-math")) {
1282 // Check for unsafe-fp-math = true.
1283 Attribute Attr = F->getFnAttribute("unsafe-fp-math");
1284 if (Attr.getValueAsString() != "true")
1285 return Ret;
1286 }
1287 Value *Op = CI->getArgOperand(0);
1288 if (Instruction *I = dyn_cast<Instruction>(Op)) {
1289 if (I->getOpcode() == Instruction::FMul && I->hasUnsafeAlgebra()) {
1290 // We're looking for a repeated factor in a multiplication tree,
1291 // so we can do this fold: sqrt(x * x) -> fabs(x);
1292 // or this fold: sqrt(x * x * y) -> fabs(x) * sqrt(y).
1293 Value *Op0 = I->getOperand(0);
1294 Value *Op1 = I->getOperand(1);
1295 Value *RepeatOp = nullptr;
1296 Value *OtherOp = nullptr;
1297 if (Op0 == Op1) {
1298 // Simple match: the operands of the multiply are identical.
1299 RepeatOp = Op0;
1300 } else {
1301 // Look for a more complicated pattern: one of the operands is itself
1302 // a multiply, so search for a common factor in that multiply.
1303 // Note: We don't bother looking any deeper than this first level or for
1304 // variations of this pattern because instcombine's visitFMUL and/or the
1305 // reassociation pass should give us this form.
1306 Value *OtherMul0, *OtherMul1;
1307 if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) {
1308 // Pattern: sqrt((x * y) * z)
1309 if (OtherMul0 == OtherMul1) {
1310 // Matched: sqrt((x * x) * z)
1311 RepeatOp = OtherMul0;
1312 OtherOp = Op1;
1313 }
1314 }
1315 }
1316 if (RepeatOp) {
1317 // Fast math flags for any created instructions should match the sqrt
1318 // and multiply.
1319 // FIXME: We're not checking the sqrt because it doesn't have
1320 // fast-math-flags (see earlier comment).
1321 IRBuilder<true, ConstantFolder,
1322 IRBuilderDefaultInserter<true> >::FastMathFlagGuard Guard(B);
1323 B.SetFastMathFlags(I->getFastMathFlags());
1324 // If we found a repeated factor, hoist it out of the square root and
1325 // replace it with the fabs of that factor.
1326 Module *M = Callee->getParent();
1327 Type *ArgType = Op->getType();
1328 Value *Fabs = Intrinsic::getDeclaration(M, Intrinsic::fabs, ArgType);
1329 Value *FabsCall = B.CreateCall(Fabs, RepeatOp, "fabs");
1330 if (OtherOp) {
1331 // If we found a non-repeated factor, we still need to get its square
1332 // root. We then multiply that by the value that was simplified out
1333 // of the square root calculation.
1334 Value *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, ArgType);
1335 Value *SqrtCall = B.CreateCall(Sqrt, OtherOp, "sqrt");
1336 return B.CreateFMul(FabsCall, SqrtCall);
1337 }
1338 return FabsCall;
1339 }
1340 }
1341 }
1342 return Ret;
1343}
1344
Chris Bienemanad070d02014-09-17 20:55:46 +00001345static bool isTrigLibCall(CallInst *CI);
1346static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
1347 bool UseFloat, Value *&Sin, Value *&Cos,
1348 Value *&SinCos);
1349
1350Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilder<> &B) {
1351
1352 // Make sure the prototype is as expected, otherwise the rest of the
1353 // function is probably invalid and likely to abort.
1354 if (!isTrigLibCall(CI))
1355 return nullptr;
1356
1357 Value *Arg = CI->getArgOperand(0);
1358 SmallVector<CallInst *, 1> SinCalls;
1359 SmallVector<CallInst *, 1> CosCalls;
1360 SmallVector<CallInst *, 1> SinCosCalls;
1361
1362 bool IsFloat = Arg->getType()->isFloatTy();
1363
1364 // Look for all compatible sinpi, cospi and sincospi calls with the same
1365 // argument. If there are enough (in some sense) we can make the
1366 // substitution.
1367 for (User *U : Arg->users())
1368 classifyArgUse(U, CI->getParent(), IsFloat, SinCalls, CosCalls,
1369 SinCosCalls);
1370
1371 // It's only worthwhile if both sinpi and cospi are actually used.
1372 if (SinCosCalls.empty() && (SinCalls.empty() || CosCalls.empty()))
1373 return nullptr;
1374
1375 Value *Sin, *Cos, *SinCos;
1376 insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos, SinCos);
1377
1378 replaceTrigInsts(SinCalls, Sin);
1379 replaceTrigInsts(CosCalls, Cos);
1380 replaceTrigInsts(SinCosCalls, SinCos);
1381
1382 return nullptr;
1383}
1384
1385static bool isTrigLibCall(CallInst *CI) {
1386 Function *Callee = CI->getCalledFunction();
1387 FunctionType *FT = Callee->getFunctionType();
1388
1389 // We can only hope to do anything useful if we can ignore things like errno
1390 // and floating-point exceptions.
1391 bool AttributesSafe =
1392 CI->hasFnAttr(Attribute::NoUnwind) && CI->hasFnAttr(Attribute::ReadNone);
1393
1394 // Other than that we need float(float) or double(double)
1395 return AttributesSafe && FT->getNumParams() == 1 &&
1396 FT->getReturnType() == FT->getParamType(0) &&
1397 (FT->getParamType(0)->isFloatTy() ||
1398 FT->getParamType(0)->isDoubleTy());
1399}
1400
1401void
1402LibCallSimplifier::classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
1403 SmallVectorImpl<CallInst *> &SinCalls,
1404 SmallVectorImpl<CallInst *> &CosCalls,
1405 SmallVectorImpl<CallInst *> &SinCosCalls) {
1406 CallInst *CI = dyn_cast<CallInst>(Val);
1407
1408 if (!CI)
1409 return;
1410
1411 Function *Callee = CI->getCalledFunction();
1412 StringRef FuncName = Callee->getName();
1413 LibFunc::Func Func;
1414 if (!TLI->getLibFunc(FuncName, Func) || !TLI->has(Func) || !isTrigLibCall(CI))
1415 return;
1416
1417 if (IsFloat) {
1418 if (Func == LibFunc::sinpif)
1419 SinCalls.push_back(CI);
1420 else if (Func == LibFunc::cospif)
1421 CosCalls.push_back(CI);
1422 else if (Func == LibFunc::sincospif_stret)
1423 SinCosCalls.push_back(CI);
1424 } else {
1425 if (Func == LibFunc::sinpi)
1426 SinCalls.push_back(CI);
1427 else if (Func == LibFunc::cospi)
1428 CosCalls.push_back(CI);
1429 else if (Func == LibFunc::sincospi_stret)
1430 SinCosCalls.push_back(CI);
1431 }
1432}
1433
1434void LibCallSimplifier::replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls,
1435 Value *Res) {
1436 for (SmallVectorImpl<CallInst *>::iterator I = Calls.begin(), E = Calls.end();
1437 I != E; ++I) {
1438 replaceAllUsesWith(*I, Res);
1439 }
1440}
1441
1442void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
1443 bool UseFloat, Value *&Sin, Value *&Cos, Value *&SinCos) {
1444 Type *ArgTy = Arg->getType();
1445 Type *ResTy;
1446 StringRef Name;
1447
1448 Triple T(OrigCallee->getParent()->getTargetTriple());
1449 if (UseFloat) {
1450 Name = "__sincospif_stret";
1451
1452 assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
1453 // x86_64 can't use {float, float} since that would be returned in both
1454 // xmm0 and xmm1, which isn't what a real struct would do.
1455 ResTy = T.getArch() == Triple::x86_64
1456 ? static_cast<Type *>(VectorType::get(ArgTy, 2))
1457 : static_cast<Type *>(StructType::get(ArgTy, ArgTy, NULL));
1458 } else {
1459 Name = "__sincospi_stret";
1460 ResTy = StructType::get(ArgTy, ArgTy, NULL);
1461 }
1462
1463 Module *M = OrigCallee->getParent();
1464 Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),
1465 ResTy, ArgTy, NULL);
1466
1467 if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
1468 // If the argument is an instruction, it must dominate all uses so put our
1469 // sincos call there.
1470 BasicBlock::iterator Loc = ArgInst;
1471 B.SetInsertPoint(ArgInst->getParent(), ++Loc);
1472 } else {
1473 // Otherwise (e.g. for a constant) the beginning of the function is as
1474 // good a place as any.
1475 BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
1476 B.SetInsertPoint(&EntryBB, EntryBB.begin());
1477 }
1478
1479 SinCos = B.CreateCall(Callee, Arg, "sincospi");
1480
1481 if (SinCos->getType()->isStructTy()) {
1482 Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
1483 Cos = B.CreateExtractValue(SinCos, 1, "cospi");
1484 } else {
1485 Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
1486 "sinpi");
1487 Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
1488 "cospi");
1489 }
1490}
Bob Wilsond8d92d92013-11-03 06:48:38 +00001491
Meador Inge7415f842012-11-25 20:45:27 +00001492//===----------------------------------------------------------------------===//
1493// Integer Library Call Optimizations
1494//===----------------------------------------------------------------------===//
1495
Chris Bienemanad070d02014-09-17 20:55:46 +00001496Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilder<> &B) {
1497 Function *Callee = CI->getCalledFunction();
1498 FunctionType *FT = Callee->getFunctionType();
1499 // Just make sure this has 2 arguments of the same FP type, which match the
1500 // result type.
1501 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy(32) ||
1502 !FT->getParamType(0)->isIntegerTy())
1503 return nullptr;
Meador Inge7415f842012-11-25 20:45:27 +00001504
Chris Bienemanad070d02014-09-17 20:55:46 +00001505 Value *Op = CI->getArgOperand(0);
Meador Inge7415f842012-11-25 20:45:27 +00001506
Chris Bienemanad070d02014-09-17 20:55:46 +00001507 // Constant fold.
1508 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1509 if (CI->isZero()) // ffs(0) -> 0.
1510 return B.getInt32(0);
1511 // ffs(c) -> cttz(c)+1
1512 return B.getInt32(CI->getValue().countTrailingZeros() + 1);
Meador Inge7415f842012-11-25 20:45:27 +00001513 }
Meador Inge7415f842012-11-25 20:45:27 +00001514
Chris Bienemanad070d02014-09-17 20:55:46 +00001515 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1516 Type *ArgType = Op->getType();
1517 Value *F =
1518 Intrinsic::getDeclaration(Callee->getParent(), Intrinsic::cttz, ArgType);
1519 Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");
1520 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
1521 V = B.CreateIntCast(V, B.getInt32Ty(), false);
Meador Ingea0b6d872012-11-26 00:24:07 +00001522
Chris Bienemanad070d02014-09-17 20:55:46 +00001523 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
1524 return B.CreateSelect(Cond, V, B.getInt32(0));
1525}
Meador Ingea0b6d872012-11-26 00:24:07 +00001526
Chris Bienemanad070d02014-09-17 20:55:46 +00001527Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) {
1528 Function *Callee = CI->getCalledFunction();
1529 FunctionType *FT = Callee->getFunctionType();
1530 // We require integer(integer) where the types agree.
1531 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1532 FT->getParamType(0) != FT->getReturnType())
1533 return nullptr;
Meador Inge9a59ab62012-11-26 02:31:59 +00001534
Chris Bienemanad070d02014-09-17 20:55:46 +00001535 // abs(x) -> x >s -1 ? x : -x
1536 Value *Op = CI->getArgOperand(0);
1537 Value *Pos =
1538 B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()), "ispos");
1539 Value *Neg = B.CreateNeg(Op, "neg");
1540 return B.CreateSelect(Pos, Op, Neg);
1541}
Meador Inge9a59ab62012-11-26 02:31:59 +00001542
Chris Bienemanad070d02014-09-17 20:55:46 +00001543Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilder<> &B) {
1544 Function *Callee = CI->getCalledFunction();
1545 FunctionType *FT = Callee->getFunctionType();
1546 // We require integer(i32)
1547 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1548 !FT->getParamType(0)->isIntegerTy(32))
1549 return nullptr;
Meador Ingea62a39e2012-11-26 03:10:07 +00001550
Chris Bienemanad070d02014-09-17 20:55:46 +00001551 // isdigit(c) -> (c-'0') <u 10
1552 Value *Op = CI->getArgOperand(0);
1553 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1554 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
1555 return B.CreateZExt(Op, CI->getType());
1556}
Meador Ingea62a39e2012-11-26 03:10:07 +00001557
Chris Bienemanad070d02014-09-17 20:55:46 +00001558Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilder<> &B) {
1559 Function *Callee = CI->getCalledFunction();
1560 FunctionType *FT = Callee->getFunctionType();
1561 // We require integer(i32)
1562 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1563 !FT->getParamType(0)->isIntegerTy(32))
1564 return nullptr;
Meador Inge604937d2012-11-26 03:38:52 +00001565
Chris Bienemanad070d02014-09-17 20:55:46 +00001566 // isascii(c) -> c <u 128
1567 Value *Op = CI->getArgOperand(0);
1568 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
1569 return B.CreateZExt(Op, CI->getType());
1570}
1571
1572Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilder<> &B) {
1573 Function *Callee = CI->getCalledFunction();
1574 FunctionType *FT = Callee->getFunctionType();
1575 // We require i32(i32)
1576 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1577 !FT->getParamType(0)->isIntegerTy(32))
1578 return nullptr;
1579
1580 // toascii(c) -> c & 0x7f
1581 return B.CreateAnd(CI->getArgOperand(0),
1582 ConstantInt::get(CI->getType(), 0x7F));
1583}
Meador Inge604937d2012-11-26 03:38:52 +00001584
Meador Inge08ca1152012-11-26 20:37:20 +00001585//===----------------------------------------------------------------------===//
1586// Formatting and IO Library Call Optimizations
1587//===----------------------------------------------------------------------===//
1588
Chris Bienemanad070d02014-09-17 20:55:46 +00001589static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
Hal Finkel66cd3f12013-11-17 02:06:35 +00001590
Chris Bienemanad070d02014-09-17 20:55:46 +00001591Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
1592 int StreamArg) {
1593 // Error reporting calls should be cold, mark them as such.
1594 // This applies even to non-builtin calls: it is only a hint and applies to
1595 // functions that the frontend might not understand as builtins.
Hal Finkel66cd3f12013-11-17 02:06:35 +00001596
Chris Bienemanad070d02014-09-17 20:55:46 +00001597 // This heuristic was suggested in:
1598 // Improving Static Branch Prediction in a Compiler
1599 // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
1600 // Proceedings of PACT'98, Oct. 1998, IEEE
1601 Function *Callee = CI->getCalledFunction();
Hal Finkel66cd3f12013-11-17 02:06:35 +00001602
Chris Bienemanad070d02014-09-17 20:55:46 +00001603 if (!CI->hasFnAttr(Attribute::Cold) &&
1604 isReportingError(Callee, CI, StreamArg)) {
1605 CI->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);
1606 }
Hal Finkel66cd3f12013-11-17 02:06:35 +00001607
Chris Bienemanad070d02014-09-17 20:55:46 +00001608 return nullptr;
1609}
1610
1611static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
1612 if (!ColdErrorCalls)
1613 return false;
1614
1615 if (!Callee || !Callee->isDeclaration())
1616 return false;
1617
1618 if (StreamArg < 0)
1619 return true;
1620
1621 // These functions might be considered cold, but only if their stream
1622 // argument is stderr.
1623
1624 if (StreamArg >= (int)CI->getNumArgOperands())
1625 return false;
1626 LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
1627 if (!LI)
1628 return false;
1629 GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
1630 if (!GV || !GV->isDeclaration())
1631 return false;
1632 return GV->getName() == "stderr";
1633}
1634
1635Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
1636 // Check for a fixed format string.
1637 StringRef FormatStr;
1638 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
Craig Topperf40110f2014-04-25 05:29:35 +00001639 return nullptr;
Hal Finkel66cd3f12013-11-17 02:06:35 +00001640
Chris Bienemanad070d02014-09-17 20:55:46 +00001641 // Empty format string -> noop.
1642 if (FormatStr.empty()) // Tolerate printf's declared void.
1643 return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
Hal Finkel66cd3f12013-11-17 02:06:35 +00001644
Chris Bienemanad070d02014-09-17 20:55:46 +00001645 // Do not do any of the following transformations if the printf return value
1646 // is used, in general the printf return value is not compatible with either
1647 // putchar() or puts().
1648 if (!CI->use_empty())
Craig Topperf40110f2014-04-25 05:29:35 +00001649 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +00001650
1651 // printf("x") -> putchar('x'), even for '%'.
1652 if (FormatStr.size() == 1) {
1653 Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, DL, TLI);
1654 if (CI->use_empty() || !Res)
1655 return Res;
1656 return B.CreateIntCast(Res, CI->getType(), true);
Meador Inge08ca1152012-11-26 20:37:20 +00001657 }
1658
Chris Bienemanad070d02014-09-17 20:55:46 +00001659 // printf("foo\n") --> puts("foo")
1660 if (FormatStr[FormatStr.size() - 1] == '\n' &&
1661 FormatStr.find('%') == StringRef::npos) { // No format characters.
1662 // Create a string literal with no \n on it. We expect the constant merge
1663 // pass to be run after this pass, to merge duplicate strings.
1664 FormatStr = FormatStr.drop_back();
1665 Value *GV = B.CreateGlobalString(FormatStr, "str");
1666 Value *NewCI = EmitPutS(GV, B, DL, TLI);
1667 return (CI->use_empty() || !NewCI)
1668 ? NewCI
1669 : ConstantInt::get(CI->getType(), FormatStr.size() + 1);
1670 }
Meador Inge08ca1152012-11-26 20:37:20 +00001671
Chris Bienemanad070d02014-09-17 20:55:46 +00001672 // Optimize specific format strings.
1673 // printf("%c", chr) --> putchar(chr)
1674 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
1675 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1676 Value *Res = EmitPutChar(CI->getArgOperand(1), B, DL, TLI);
Meador Inge08ca1152012-11-26 20:37:20 +00001677
Chris Bienemanad070d02014-09-17 20:55:46 +00001678 if (CI->use_empty() || !Res)
1679 return Res;
1680 return B.CreateIntCast(Res, CI->getType(), true);
1681 }
1682
1683 // printf("%s\n", str) --> puts(str)
1684 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
1685 CI->getArgOperand(1)->getType()->isPointerTy()) {
1686 return EmitPutS(CI->getArgOperand(1), B, DL, TLI);
1687 }
1688 return nullptr;
1689}
1690
1691Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) {
1692
1693 Function *Callee = CI->getCalledFunction();
1694 // Require one fixed pointer argument and an integer/void result.
1695 FunctionType *FT = Callee->getFunctionType();
1696 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1697 !(FT->getReturnType()->isIntegerTy() || FT->getReturnType()->isVoidTy()))
1698 return nullptr;
1699
1700 if (Value *V = optimizePrintFString(CI, B)) {
1701 return V;
1702 }
1703
1704 // printf(format, ...) -> iprintf(format, ...) if no floating point
1705 // arguments.
1706 if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {
1707 Module *M = B.GetInsertBlock()->getParent()->getParent();
1708 Constant *IPrintFFn =
Meador Inge08ca1152012-11-26 20:37:20 +00001709 M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
Chris Bienemanad070d02014-09-17 20:55:46 +00001710 CallInst *New = cast<CallInst>(CI->clone());
1711 New->setCalledFunction(IPrintFFn);
1712 B.Insert(New);
1713 return New;
Meador Inge08ca1152012-11-26 20:37:20 +00001714 }
Chris Bienemanad070d02014-09-17 20:55:46 +00001715 return nullptr;
1716}
Meador Inge08ca1152012-11-26 20:37:20 +00001717
Chris Bienemanad070d02014-09-17 20:55:46 +00001718Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
1719 // Check for a fixed format string.
1720 StringRef FormatStr;
1721 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
Craig Topperf40110f2014-04-25 05:29:35 +00001722 return nullptr;
Meador Inge25c9b3b2012-11-27 05:57:54 +00001723
Chris Bienemanad070d02014-09-17 20:55:46 +00001724 // If we just have a format string (nothing else crazy) transform it.
1725 if (CI->getNumArgOperands() == 2) {
1726 // Make sure there's no % in the constant array. We could try to handle
1727 // %% -> % in the future if we cared.
1728 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1729 if (FormatStr[i] == '%')
1730 return nullptr; // we found a format specifier, bail out.
Hal Finkel66cd3f12013-11-17 02:06:35 +00001731
Meador Ingef8e72502012-11-29 15:45:43 +00001732 // These optimizations require DataLayout.
Chris Bienemanad070d02014-09-17 20:55:46 +00001733 if (!DL)
Craig Topperf40110f2014-04-25 05:29:35 +00001734 return nullptr;
Meador Ingef8e72502012-11-29 15:45:43 +00001735
Chris Bienemanad070d02014-09-17 20:55:46 +00001736 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1737 B.CreateMemCpy(
1738 CI->getArgOperand(0), CI->getArgOperand(1),
1739 ConstantInt::get(DL->getIntPtrType(CI->getContext()),
1740 FormatStr.size() + 1),
1741 1); // Copy the null byte.
1742 return ConstantInt::get(CI->getType(), FormatStr.size());
Meador Ingef8e72502012-11-29 15:45:43 +00001743 }
Meador Ingef8e72502012-11-29 15:45:43 +00001744
Chris Bienemanad070d02014-09-17 20:55:46 +00001745 // The remaining optimizations require the format string to be "%s" or "%c"
1746 // and have an extra operand.
1747 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1748 CI->getNumArgOperands() < 3)
Craig Topperf40110f2014-04-25 05:29:35 +00001749 return nullptr;
Meador Inge75798bb2012-11-29 19:15:17 +00001750
Chris Bienemanad070d02014-09-17 20:55:46 +00001751 // Decode the second character of the format string.
1752 if (FormatStr[1] == 'c') {
1753 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1754 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
1755 return nullptr;
1756 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
1757 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
1758 B.CreateStore(V, Ptr);
1759 Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
1760 B.CreateStore(B.getInt8(0), Ptr);
Meador Ingedf796f82012-10-13 16:45:24 +00001761
Chris Bienemanad070d02014-09-17 20:55:46 +00001762 return ConstantInt::get(CI->getType(), 1);
Meador Ingedf796f82012-10-13 16:45:24 +00001763 }
1764
Chris Bienemanad070d02014-09-17 20:55:46 +00001765 if (FormatStr[1] == 's') {
1766 // These optimizations require DataLayout.
1767 if (!DL)
1768 return nullptr;
Meador Ingedf796f82012-10-13 16:45:24 +00001769
Chris Bienemanad070d02014-09-17 20:55:46 +00001770 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1771 if (!CI->getArgOperand(2)->getType()->isPointerTy())
1772 return nullptr;
1773
1774 Value *Len = EmitStrLen(CI->getArgOperand(2), B, DL, TLI);
1775 if (!Len)
1776 return nullptr;
1777 Value *IncLen =
1778 B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
1779 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
1780
1781 // The sprintf result is the unincremented number of bytes in the string.
1782 return B.CreateIntCast(Len, CI->getType(), false);
1783 }
1784 return nullptr;
1785}
1786
1787Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilder<> &B) {
1788 Function *Callee = CI->getCalledFunction();
1789 // Require two fixed pointer arguments and an integer result.
1790 FunctionType *FT = Callee->getFunctionType();
1791 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1792 !FT->getParamType(1)->isPointerTy() ||
1793 !FT->getReturnType()->isIntegerTy())
1794 return nullptr;
1795
1796 if (Value *V = optimizeSPrintFString(CI, B)) {
1797 return V;
1798 }
1799
1800 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
1801 // point arguments.
1802 if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {
1803 Module *M = B.GetInsertBlock()->getParent()->getParent();
1804 Constant *SIPrintFFn =
1805 M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
1806 CallInst *New = cast<CallInst>(CI->clone());
1807 New->setCalledFunction(SIPrintFFn);
1808 B.Insert(New);
1809 return New;
1810 }
1811 return nullptr;
1812}
1813
1814Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) {
1815 optimizeErrorReporting(CI, B, 0);
1816
1817 // All the optimizations depend on the format string.
1818 StringRef FormatStr;
1819 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1820 return nullptr;
1821
1822 // Do not do any of the following transformations if the fprintf return
1823 // value is used, in general the fprintf return value is not compatible
1824 // with fwrite(), fputc() or fputs().
1825 if (!CI->use_empty())
1826 return nullptr;
1827
1828 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1829 if (CI->getNumArgOperands() == 2) {
1830 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1831 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1832 return nullptr; // We found a format specifier.
1833
1834 // These optimizations require DataLayout.
1835 if (!DL)
1836 return nullptr;
1837
1838 return EmitFWrite(
1839 CI->getArgOperand(1),
1840 ConstantInt::get(DL->getIntPtrType(CI->getContext()), FormatStr.size()),
1841 CI->getArgOperand(0), B, DL, TLI);
1842 }
1843
1844 // The remaining optimizations require the format string to be "%s" or "%c"
1845 // and have an extra operand.
1846 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1847 CI->getNumArgOperands() < 3)
1848 return nullptr;
1849
1850 // Decode the second character of the format string.
1851 if (FormatStr[1] == 'c') {
1852 // fprintf(F, "%c", chr) --> fputc(chr, F)
1853 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
1854 return nullptr;
1855 return EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI);
1856 }
1857
1858 if (FormatStr[1] == 's') {
1859 // fprintf(F, "%s", str) --> fputs(str, F)
1860 if (!CI->getArgOperand(2)->getType()->isPointerTy())
1861 return nullptr;
1862 return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI);
1863 }
1864 return nullptr;
1865}
1866
1867Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilder<> &B) {
1868 Function *Callee = CI->getCalledFunction();
1869 // Require two fixed paramters as pointers and integer result.
1870 FunctionType *FT = Callee->getFunctionType();
1871 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1872 !FT->getParamType(1)->isPointerTy() ||
1873 !FT->getReturnType()->isIntegerTy())
1874 return nullptr;
1875
1876 if (Value *V = optimizeFPrintFString(CI, B)) {
1877 return V;
1878 }
1879
1880 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
1881 // floating point arguments.
1882 if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {
1883 Module *M = B.GetInsertBlock()->getParent()->getParent();
1884 Constant *FIPrintFFn =
1885 M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
1886 CallInst *New = cast<CallInst>(CI->clone());
1887 New->setCalledFunction(FIPrintFFn);
1888 B.Insert(New);
1889 return New;
1890 }
1891 return nullptr;
1892}
1893
1894Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilder<> &B) {
1895 optimizeErrorReporting(CI, B, 3);
1896
1897 Function *Callee = CI->getCalledFunction();
1898 // Require a pointer, an integer, an integer, a pointer, returning integer.
1899 FunctionType *FT = Callee->getFunctionType();
1900 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1901 !FT->getParamType(1)->isIntegerTy() ||
1902 !FT->getParamType(2)->isIntegerTy() ||
1903 !FT->getParamType(3)->isPointerTy() ||
1904 !FT->getReturnType()->isIntegerTy())
1905 return nullptr;
1906
1907 // Get the element size and count.
1908 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1909 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1910 if (!SizeC || !CountC)
1911 return nullptr;
1912 uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();
1913
1914 // If this is writing zero records, remove the call (it's a noop).
1915 if (Bytes == 0)
1916 return ConstantInt::get(CI->getType(), 0);
1917
1918 // If this is writing one byte, turn it into fputc.
1919 // This optimisation is only valid, if the return value is unused.
1920 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1921 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1922 Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, DL, TLI);
1923 return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
1924 }
1925
1926 return nullptr;
1927}
1928
1929Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) {
1930 optimizeErrorReporting(CI, B, 1);
1931
1932 Function *Callee = CI->getCalledFunction();
1933
1934 // These optimizations require DataLayout.
1935 if (!DL)
1936 return nullptr;
1937
1938 // Require two pointers. Also, we can't optimize if return value is used.
1939 FunctionType *FT = Callee->getFunctionType();
1940 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1941 !FT->getParamType(1)->isPointerTy() || !CI->use_empty())
1942 return nullptr;
1943
1944 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1945 uint64_t Len = GetStringLength(CI->getArgOperand(0));
1946 if (!Len)
1947 return nullptr;
1948
1949 // Known to have no uses (see above).
1950 return EmitFWrite(
1951 CI->getArgOperand(0),
1952 ConstantInt::get(DL->getIntPtrType(CI->getContext()), Len - 1),
1953 CI->getArgOperand(1), B, DL, TLI);
1954}
1955
1956Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilder<> &B) {
1957 Function *Callee = CI->getCalledFunction();
1958 // Require one fixed pointer argument and an integer/void result.
1959 FunctionType *FT = Callee->getFunctionType();
1960 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1961 !(FT->getReturnType()->isIntegerTy() || FT->getReturnType()->isVoidTy()))
1962 return nullptr;
1963
1964 // Check for a constant string.
1965 StringRef Str;
1966 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
1967 return nullptr;
1968
1969 if (Str.empty() && CI->use_empty()) {
1970 // puts("") -> putchar('\n')
1971 Value *Res = EmitPutChar(B.getInt32('\n'), B, DL, TLI);
1972 if (CI->use_empty() || !Res)
1973 return Res;
1974 return B.CreateIntCast(Res, CI->getType(), true);
1975 }
1976
1977 return nullptr;
1978}
1979
1980bool LibCallSimplifier::hasFloatVersion(StringRef FuncName) {
Meador Inge20255ef2013-03-12 00:08:29 +00001981 LibFunc::Func Func;
1982 SmallString<20> FloatFuncName = FuncName;
1983 FloatFuncName += 'f';
1984 if (TLI->getLibFunc(FloatFuncName, Func))
1985 return TLI->has(Func);
1986 return false;
1987}
Meador Inge7fb2f732012-10-13 16:45:32 +00001988
Chris Bienemanad070d02014-09-17 20:55:46 +00001989Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
1990 if (CI->isNoBuiltin())
1991 return nullptr;
Meador Inge4d2827c2012-11-11 05:11:20 +00001992
Meador Inge20255ef2013-03-12 00:08:29 +00001993 LibFunc::Func Func;
1994 Function *Callee = CI->getCalledFunction();
1995 StringRef FuncName = Callee->getName();
Chris Bienemanad070d02014-09-17 20:55:46 +00001996 IRBuilder<> Builder(CI);
1997 bool isCallingConvC = CI->getCallingConv() == llvm::CallingConv::C;
Meador Inge20255ef2013-03-12 00:08:29 +00001998
Sanjay Patela92fa442014-10-22 15:29:23 +00001999 // Command-line parameter overrides function attribute.
2000 if (EnableUnsafeFPShrink.getNumOccurrences() > 0)
2001 UnsafeFPShrink = EnableUnsafeFPShrink;
2002 else if (Callee->hasFnAttribute("unsafe-fp-math")) {
2003 // FIXME: This is the same problem as described in optimizeSqrt().
2004 // If calls gain access to IR-level FMF, then use that instead of a
2005 // function attribute.
2006
2007 // Check for unsafe-fp-math = true.
2008 Attribute Attr = Callee->getFnAttribute("unsafe-fp-math");
2009 if (Attr.getValueAsString() == "true")
2010 UnsafeFPShrink = true;
2011 }
2012
Meador Inge20255ef2013-03-12 00:08:29 +00002013 // Next check for intrinsics.
2014 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
Chris Bienemanad070d02014-09-17 20:55:46 +00002015 if (!isCallingConvC)
2016 return nullptr;
Meador Inge20255ef2013-03-12 00:08:29 +00002017 switch (II->getIntrinsicID()) {
2018 case Intrinsic::pow:
Chris Bienemanad070d02014-09-17 20:55:46 +00002019 return optimizePow(CI, Builder);
Meador Inge20255ef2013-03-12 00:08:29 +00002020 case Intrinsic::exp2:
Chris Bienemanad070d02014-09-17 20:55:46 +00002021 return optimizeExp2(CI, Builder);
Sanjay Patel0ca42bb2014-10-14 20:43:11 +00002022 case Intrinsic::fabs:
2023 return optimizeFabs(CI, Builder);
Sanjay Patelc699a612014-10-16 18:48:17 +00002024 case Intrinsic::sqrt:
2025 return optimizeSqrt(CI, Builder);
Meador Inge20255ef2013-03-12 00:08:29 +00002026 default:
Chris Bienemanad070d02014-09-17 20:55:46 +00002027 return nullptr;
Meador Inge20255ef2013-03-12 00:08:29 +00002028 }
2029 }
2030
2031 // Then check for known library functions.
2032 if (TLI->getLibFunc(FuncName, Func) && TLI->has(Func)) {
Chris Bienemanad070d02014-09-17 20:55:46 +00002033 // We never change the calling convention.
2034 if (!ignoreCallingConv(Func) && !isCallingConvC)
2035 return nullptr;
Meador Inge20255ef2013-03-12 00:08:29 +00002036 switch (Func) {
Chris Bienemanad070d02014-09-17 20:55:46 +00002037 case LibFunc::strcat:
2038 return optimizeStrCat(CI, Builder);
2039 case LibFunc::strncat:
2040 return optimizeStrNCat(CI, Builder);
2041 case LibFunc::strchr:
2042 return optimizeStrChr(CI, Builder);
2043 case LibFunc::strrchr:
2044 return optimizeStrRChr(CI, Builder);
2045 case LibFunc::strcmp:
2046 return optimizeStrCmp(CI, Builder);
2047 case LibFunc::strncmp:
2048 return optimizeStrNCmp(CI, Builder);
2049 case LibFunc::strcpy:
2050 return optimizeStrCpy(CI, Builder);
2051 case LibFunc::stpcpy:
2052 return optimizeStpCpy(CI, Builder);
2053 case LibFunc::strncpy:
2054 return optimizeStrNCpy(CI, Builder);
2055 case LibFunc::strlen:
2056 return optimizeStrLen(CI, Builder);
2057 case LibFunc::strpbrk:
2058 return optimizeStrPBrk(CI, Builder);
2059 case LibFunc::strtol:
2060 case LibFunc::strtod:
2061 case LibFunc::strtof:
2062 case LibFunc::strtoul:
2063 case LibFunc::strtoll:
2064 case LibFunc::strtold:
2065 case LibFunc::strtoull:
2066 return optimizeStrTo(CI, Builder);
2067 case LibFunc::strspn:
2068 return optimizeStrSpn(CI, Builder);
2069 case LibFunc::strcspn:
2070 return optimizeStrCSpn(CI, Builder);
2071 case LibFunc::strstr:
2072 return optimizeStrStr(CI, Builder);
2073 case LibFunc::memcmp:
2074 return optimizeMemCmp(CI, Builder);
2075 case LibFunc::memcpy:
2076 return optimizeMemCpy(CI, Builder);
2077 case LibFunc::memmove:
2078 return optimizeMemMove(CI, Builder);
2079 case LibFunc::memset:
2080 return optimizeMemSet(CI, Builder);
2081 case LibFunc::cosf:
2082 case LibFunc::cos:
2083 case LibFunc::cosl:
2084 return optimizeCos(CI, Builder);
2085 case LibFunc::sinpif:
2086 case LibFunc::sinpi:
2087 case LibFunc::cospif:
2088 case LibFunc::cospi:
2089 return optimizeSinCosPi(CI, Builder);
2090 case LibFunc::powf:
2091 case LibFunc::pow:
2092 case LibFunc::powl:
2093 return optimizePow(CI, Builder);
2094 case LibFunc::exp2l:
2095 case LibFunc::exp2:
2096 case LibFunc::exp2f:
2097 return optimizeExp2(CI, Builder);
Sanjay Patel0ca42bb2014-10-14 20:43:11 +00002098 case LibFunc::fabsf:
2099 case LibFunc::fabs:
2100 case LibFunc::fabsl:
2101 return optimizeFabs(CI, Builder);
Sanjay Patelc699a612014-10-16 18:48:17 +00002102 case LibFunc::sqrtf:
2103 case LibFunc::sqrt:
2104 case LibFunc::sqrtl:
2105 return optimizeSqrt(CI, Builder);
Chris Bienemanad070d02014-09-17 20:55:46 +00002106 case LibFunc::ffs:
2107 case LibFunc::ffsl:
2108 case LibFunc::ffsll:
2109 return optimizeFFS(CI, Builder);
2110 case LibFunc::abs:
2111 case LibFunc::labs:
2112 case LibFunc::llabs:
2113 return optimizeAbs(CI, Builder);
2114 case LibFunc::isdigit:
2115 return optimizeIsDigit(CI, Builder);
2116 case LibFunc::isascii:
2117 return optimizeIsAscii(CI, Builder);
2118 case LibFunc::toascii:
2119 return optimizeToAscii(CI, Builder);
2120 case LibFunc::printf:
2121 return optimizePrintF(CI, Builder);
2122 case LibFunc::sprintf:
2123 return optimizeSPrintF(CI, Builder);
2124 case LibFunc::fprintf:
2125 return optimizeFPrintF(CI, Builder);
2126 case LibFunc::fwrite:
2127 return optimizeFWrite(CI, Builder);
2128 case LibFunc::fputs:
2129 return optimizeFPuts(CI, Builder);
2130 case LibFunc::puts:
2131 return optimizePuts(CI, Builder);
2132 case LibFunc::perror:
2133 return optimizeErrorReporting(CI, Builder);
2134 case LibFunc::vfprintf:
2135 case LibFunc::fiprintf:
2136 return optimizeErrorReporting(CI, Builder, 0);
2137 case LibFunc::fputc:
2138 return optimizeErrorReporting(CI, Builder, 1);
2139 case LibFunc::ceil:
Chris Bienemanad070d02014-09-17 20:55:46 +00002140 case LibFunc::floor:
2141 case LibFunc::rint:
2142 case LibFunc::round:
2143 case LibFunc::nearbyint:
2144 case LibFunc::trunc:
2145 if (hasFloatVersion(FuncName))
2146 return optimizeUnaryDoubleFP(CI, Builder, false);
2147 return nullptr;
2148 case LibFunc::acos:
2149 case LibFunc::acosh:
2150 case LibFunc::asin:
2151 case LibFunc::asinh:
2152 case LibFunc::atan:
2153 case LibFunc::atanh:
2154 case LibFunc::cbrt:
2155 case LibFunc::cosh:
2156 case LibFunc::exp:
2157 case LibFunc::exp10:
2158 case LibFunc::expm1:
2159 case LibFunc::log:
2160 case LibFunc::log10:
2161 case LibFunc::log1p:
2162 case LibFunc::log2:
2163 case LibFunc::logb:
2164 case LibFunc::sin:
2165 case LibFunc::sinh:
Chris Bienemanad070d02014-09-17 20:55:46 +00002166 case LibFunc::tan:
2167 case LibFunc::tanh:
2168 if (UnsafeFPShrink && hasFloatVersion(FuncName))
2169 return optimizeUnaryDoubleFP(CI, Builder, true);
2170 return nullptr;
2171 case LibFunc::fmin:
2172 case LibFunc::fmax:
2173 if (hasFloatVersion(FuncName))
2174 return optimizeBinaryDoubleFP(CI, Builder);
2175 return nullptr;
2176 case LibFunc::memcpy_chk:
2177 return optimizeMemCpyChk(CI, Builder);
2178 default:
2179 return nullptr;
2180 }
Meador Inge20255ef2013-03-12 00:08:29 +00002181 }
2182
Chris Bienemanad070d02014-09-17 20:55:46 +00002183 if (!isCallingConvC)
2184 return nullptr;
2185
Meador Inge20255ef2013-03-12 00:08:29 +00002186 // Finally check for fortified library calls.
2187 if (FuncName.endswith("_chk")) {
2188 if (FuncName == "__memmove_chk")
Chris Bienemanad070d02014-09-17 20:55:46 +00002189 return optimizeMemMoveChk(CI, Builder);
Meador Inge20255ef2013-03-12 00:08:29 +00002190 else if (FuncName == "__memset_chk")
Chris Bienemanad070d02014-09-17 20:55:46 +00002191 return optimizeMemSetChk(CI, Builder);
Meador Inge20255ef2013-03-12 00:08:29 +00002192 else if (FuncName == "__strcpy_chk")
Chris Bienemanad070d02014-09-17 20:55:46 +00002193 return optimizeStrCpyChk(CI, Builder);
Meador Inge20255ef2013-03-12 00:08:29 +00002194 else if (FuncName == "__stpcpy_chk")
Chris Bienemanad070d02014-09-17 20:55:46 +00002195 return optimizeStpCpyChk(CI, Builder);
Meador Inge20255ef2013-03-12 00:08:29 +00002196 else if (FuncName == "__strncpy_chk")
Chris Bienemanad070d02014-09-17 20:55:46 +00002197 return optimizeStrNCpyChk(CI, Builder);
Meador Inge20255ef2013-03-12 00:08:29 +00002198 else if (FuncName == "__stpncpy_chk")
Chris Bienemanad070d02014-09-17 20:55:46 +00002199 return optimizeStrNCpyChk(CI, Builder);
Meador Inge20255ef2013-03-12 00:08:29 +00002200 }
2201
Craig Topperf40110f2014-04-25 05:29:35 +00002202 return nullptr;
Meador Ingedf796f82012-10-13 16:45:24 +00002203}
2204
Rafael Espindola37dc9e12014-02-21 00:06:31 +00002205LibCallSimplifier::LibCallSimplifier(const DataLayout *DL,
Sanjay Patela92fa442014-10-22 15:29:23 +00002206 const TargetLibraryInfo *TLI) :
Chris Bienemanad070d02014-09-17 20:55:46 +00002207 DL(DL),
2208 TLI(TLI),
Sanjay Patela92fa442014-10-22 15:29:23 +00002209 UnsafeFPShrink(false) {
Meador Ingedf796f82012-10-13 16:45:24 +00002210}
2211
Meador Inge76fc1a42012-11-11 03:51:43 +00002212void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const {
2213 I->replaceAllUsesWith(With);
2214 I->eraseFromParent();
2215}
2216
Meador Ingedfb08a22013-06-20 19:48:07 +00002217// TODO:
2218// Additional cases that we need to add to this file:
2219//
2220// cbrt:
2221// * cbrt(expN(X)) -> expN(x/3)
2222// * cbrt(sqrt(x)) -> pow(x,1/6)
2223// * cbrt(sqrt(x)) -> pow(x,1/9)
2224//
2225// exp, expf, expl:
2226// * exp(log(x)) -> x
2227//
2228// log, logf, logl:
2229// * log(exp(x)) -> x
2230// * log(x**y) -> y*log(x)
2231// * log(exp(y)) -> y*log(e)
2232// * log(exp2(y)) -> y*log(2)
2233// * log(exp10(y)) -> y*log(10)
2234// * log(sqrt(x)) -> 0.5*log(x)
2235// * log(pow(x,y)) -> y*log(x)
2236//
2237// lround, lroundf, lroundl:
2238// * lround(cnst) -> cnst'
2239//
2240// pow, powf, powl:
2241// * pow(exp(x),y) -> exp(x*y)
2242// * pow(sqrt(x),y) -> pow(x,y*0.5)
2243// * pow(pow(x,y),z)-> pow(x,y*z)
2244//
2245// round, roundf, roundl:
2246// * round(cnst) -> cnst'
2247//
2248// signbit:
2249// * signbit(cnst) -> cnst'
2250// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2251//
2252// sqrt, sqrtf, sqrtl:
2253// * sqrt(expN(x)) -> expN(x*0.5)
2254// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2255// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2256//
Meador Ingedfb08a22013-06-20 19:48:07 +00002257// tan, tanf, tanl:
2258// * tan(atan(x)) -> x
2259//
2260// trunc, truncf, truncl:
2261// * trunc(cnst) -> cnst'
2262//
2263//