blob: d3eae6e3d875926dda26a83a473d656b1c83f313 [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"
Weiming Zhao45d4cb92015-11-24 18:57:06 +000021#include "llvm/Analysis/TargetLibraryInfo.h"
Meador Ingedf796f82012-10-13 16:45:24 +000022#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
Diego Novillo7f8af8b2014-05-22 14:19:46 +000024#include "llvm/IR/DiagnosticInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Function.h"
26#include "llvm/IR/IRBuilder.h"
Meador Inge20255ef2013-03-12 00:08:29 +000027#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Intrinsics.h"
29#include "llvm/IR/LLVMContext.h"
30#include "llvm/IR/Module.h"
Sanjay Patelc699a612014-10-16 18:48:17 +000031#include "llvm/IR/PatternMatch.h"
Hal Finkel66cd3f12013-11-17 02:06:35 +000032#include "llvm/Support/CommandLine.h"
Meador Ingedf796f82012-10-13 16:45:24 +000033#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chad Rosierdc655322015-08-28 18:30:18 +000034#include "llvm/Transforms/Utils/Local.h"
Meador Ingedf796f82012-10-13 16:45:24 +000035
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) {
Davide Italianob883b012015-11-12 23:39:00 +000055 return Func == LibFunc::abs || Func == LibFunc::labs ||
56 Func == LibFunc::llabs || Func == LibFunc::strlen;
Chris Bienemanad070d02014-09-17 20:55:46 +000057}
58
Sam Parker214f7bf2016-09-13 12:10:14 +000059static bool isCallingConvCCompatible(CallInst *CI) {
60 switch(CI->getCallingConv()) {
61 default:
62 return false;
63 case llvm::CallingConv::C:
64 return true;
65 case llvm::CallingConv::ARM_APCS:
66 case llvm::CallingConv::ARM_AAPCS:
67 case llvm::CallingConv::ARM_AAPCS_VFP: {
68
69 // The iOS ABI diverges from the standard in some cases, so for now don't
70 // try to simplify those calls.
71 if (Triple(CI->getModule()->getTargetTriple()).isiOS())
72 return false;
73
74 auto *FuncTy = CI->getFunctionType();
75
76 if (!FuncTy->getReturnType()->isPointerTy() &&
77 !FuncTy->getReturnType()->isIntegerTy() &&
78 !FuncTy->getReturnType()->isVoidTy())
79 return false;
80
81 for (auto Param : FuncTy->params()) {
82 if (!Param->isPointerTy() && !Param->isIntegerTy())
83 return false;
84 }
85 return true;
86 }
87 }
88 return false;
89}
90
Sanjay Pateld707db92015-12-31 16:10:49 +000091/// Return true if it only matters that the value is equal or not-equal to zero.
Meador Inged589ac62012-10-31 03:33:06 +000092static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000093 for (User *U : V->users()) {
94 if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
Meador Inged589ac62012-10-31 03:33:06 +000095 if (IC->isEquality())
96 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
97 if (C->isNullValue())
98 continue;
99 // Unknown instruction.
100 return false;
101 }
102 return true;
103}
104
Sanjay Pateld707db92015-12-31 16:10:49 +0000105/// Return true if it is only used in equality comparisons with With.
Meador Inge56edbc92012-11-11 03:51:48 +0000106static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000107 for (User *U : V->users()) {
108 if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
Meador Inge56edbc92012-11-11 03:51:48 +0000109 if (IC->isEquality() && IC->getOperand(1) == With)
110 continue;
111 // Unknown instruction.
112 return false;
113 }
114 return true;
115}
116
Meador Inge08ca1152012-11-26 20:37:20 +0000117static bool callHasFloatingPointArgument(const CallInst *CI) {
David Majnemer0a16c222016-08-11 21:15:00 +0000118 return any_of(CI->operands(), [](const Use &OI) {
Davide Italianoda3beeb2015-11-28 22:27:48 +0000119 return OI->getType()->isFloatingPointTy();
120 });
Meador Inge08ca1152012-11-26 20:37:20 +0000121}
122
Benjamin Kramer2702caa2013-08-31 18:19:35 +0000123/// \brief Check whether the overloaded unary floating point function
Sanjay Patele24c60e2015-08-12 20:36:18 +0000124/// corresponding to \a Ty is available.
Benjamin Kramer2702caa2013-08-31 18:19:35 +0000125static bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
126 LibFunc::Func DoubleFn, LibFunc::Func FloatFn,
127 LibFunc::Func LongDoubleFn) {
128 switch (Ty->getTypeID()) {
129 case Type::FloatTyID:
130 return TLI->has(FloatFn);
131 case Type::DoubleTyID:
132 return TLI->has(DoubleFn);
133 default:
134 return TLI->has(LongDoubleFn);
135 }
136}
137
Meador Inged589ac62012-10-31 03:33:06 +0000138//===----------------------------------------------------------------------===//
Meador Inge7fb2f732012-10-13 16:45:32 +0000139// String and Memory Library Call Optimizations
140//===----------------------------------------------------------------------===//
141
Chris Bienemanad070d02014-09-17 20:55:46 +0000142Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000143 // Extract some information from the instruction
144 Value *Dst = CI->getArgOperand(0);
145 Value *Src = CI->getArgOperand(1);
146
147 // See if we can get the length of the input string.
148 uint64_t Len = GetStringLength(Src);
149 if (Len == 0)
150 return nullptr;
151 --Len; // Unbias length.
152
153 // Handle the simple, do-nothing case: strcat(x, "") -> x
154 if (Len == 0)
155 return Dst;
156
Chris Bienemanad070d02014-09-17 20:55:46 +0000157 return emitStrLenMemCpy(Src, Dst, Len, B);
158}
159
160Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
161 IRBuilder<> &B) {
162 // We need to find the end of the destination string. That's where the
163 // memory is to be moved to. We just generate a call to strlen.
Sanjay Pateld3112a52016-01-19 19:46:10 +0000164 Value *DstLen = emitStrLen(Dst, B, DL, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +0000165 if (!DstLen)
166 return nullptr;
167
168 // Now that we have the destination's length, we must index into the
169 // destination's pointer to get the actual memcpy destination (end of
170 // the string .. we're concatenating).
David Blaikie3909da72015-03-30 20:42:56 +0000171 Value *CpyDst = B.CreateGEP(B.getInt8Ty(), Dst, DstLen, "endptr");
Chris Bienemanad070d02014-09-17 20:55:46 +0000172
173 // We have enough information to now generate the memcpy call to do the
174 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000175 B.CreateMemCpy(CpyDst, Src,
176 ConstantInt::get(DL.getIntPtrType(Src->getContext()), Len + 1),
Pete Cooper67cf9a72015-11-19 05:56:52 +0000177 1);
Chris Bienemanad070d02014-09-17 20:55:46 +0000178 return Dst;
179}
180
181Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilder<> &B) {
Sanjay Pateld707db92015-12-31 16:10:49 +0000182 // Extract some information from the instruction.
Chris Bienemanad070d02014-09-17 20:55:46 +0000183 Value *Dst = CI->getArgOperand(0);
184 Value *Src = CI->getArgOperand(1);
185 uint64_t Len;
186
Sanjay Pateld707db92015-12-31 16:10:49 +0000187 // We don't do anything if length is not constant.
Chris Bienemanad070d02014-09-17 20:55:46 +0000188 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
189 Len = LengthArg->getZExtValue();
190 else
191 return nullptr;
192
193 // See if we can get the length of the input string.
194 uint64_t SrcLen = GetStringLength(Src);
195 if (SrcLen == 0)
196 return nullptr;
197 --SrcLen; // Unbias length.
198
199 // Handle the simple, do-nothing cases:
200 // strncat(x, "", c) -> x
201 // strncat(x, c, 0) -> x
202 if (SrcLen == 0 || Len == 0)
203 return Dst;
204
Sanjay Pateld707db92015-12-31 16:10:49 +0000205 // We don't optimize this case.
Chris Bienemanad070d02014-09-17 20:55:46 +0000206 if (Len < SrcLen)
207 return nullptr;
208
209 // strncat(x, s, c) -> strcat(x, s)
Sanjay Pateld707db92015-12-31 16:10:49 +0000210 // s is constant so the strcat can be optimized further.
Chris Bienemanad070d02014-09-17 20:55:46 +0000211 return emitStrLenMemCpy(Src, Dst, SrcLen, B);
212}
213
214Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) {
215 Function *Callee = CI->getCalledFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +0000216 FunctionType *FT = Callee->getFunctionType();
Chris Bienemanad070d02014-09-17 20:55:46 +0000217 Value *SrcStr = CI->getArgOperand(0);
218
219 // If the second operand is non-constant, see if we can compute the length
220 // of the input string and turn this into memchr.
221 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
222 if (!CharC) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000223 uint64_t Len = GetStringLength(SrcStr);
224 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32.
225 return nullptr;
Meador Inge7fb2f732012-10-13 16:45:32 +0000226
Sanjay Pateld3112a52016-01-19 19:46:10 +0000227 return emitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000228 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len),
229 B, DL, TLI);
Meador Inge7fb2f732012-10-13 16:45:32 +0000230 }
231
Chris Bienemanad070d02014-09-17 20:55:46 +0000232 // Otherwise, the character is a constant, see if the first argument is
233 // a string literal. If so, we can constant fold.
234 StringRef Str;
235 if (!getConstantStringInfo(SrcStr, Str)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000236 if (CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
Sanjay Pateld3112a52016-01-19 19:46:10 +0000237 return B.CreateGEP(B.getInt8Ty(), SrcStr, emitStrLen(SrcStr, B, DL, TLI),
Sanjay Pateld707db92015-12-31 16:10:49 +0000238 "strchr");
Chris Bienemanad070d02014-09-17 20:55:46 +0000239 return nullptr;
240 }
241
242 // Compute the offset, make sure to handle the case when we're searching for
243 // zero (a weird way to spell strlen).
244 size_t I = (0xFF & CharC->getSExtValue()) == 0
245 ? Str.size()
246 : Str.find(CharC->getSExtValue());
247 if (I == StringRef::npos) // Didn't find the char. strchr returns null.
248 return Constant::getNullValue(CI->getType());
249
250 // strchr(s+n,c) -> gep(s+n+i,c)
David Blaikie3909da72015-03-30 20:42:56 +0000251 return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strchr");
Chris Bienemanad070d02014-09-17 20:55:46 +0000252}
253
254Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000255 Value *SrcStr = CI->getArgOperand(0);
256 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
257
258 // Cannot fold anything if we're not looking for a constant.
259 if (!CharC)
260 return nullptr;
261
262 StringRef Str;
263 if (!getConstantStringInfo(SrcStr, Str)) {
264 // strrchr(s, 0) -> strchr(s, 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000265 if (CharC->isZero())
Sanjay Pateld3112a52016-01-19 19:46:10 +0000266 return emitStrChr(SrcStr, '\0', B, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +0000267 return nullptr;
268 }
269
270 // Compute the offset.
271 size_t I = (0xFF & CharC->getSExtValue()) == 0
272 ? Str.size()
273 : Str.rfind(CharC->getSExtValue());
274 if (I == StringRef::npos) // Didn't find the char. Return null.
275 return Constant::getNullValue(CI->getType());
276
277 // strrchr(s+n,c) -> gep(s+n+i,c)
David Blaikie3909da72015-03-30 20:42:56 +0000278 return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strrchr");
Chris Bienemanad070d02014-09-17 20:55:46 +0000279}
280
281Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000282 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
283 if (Str1P == Str2P) // strcmp(x,x) -> 0
284 return ConstantInt::get(CI->getType(), 0);
285
286 StringRef Str1, Str2;
287 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
288 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
289
290 // strcmp(x, y) -> cnst (if both x and y are constant strings)
291 if (HasStr1 && HasStr2)
292 return ConstantInt::get(CI->getType(), Str1.compare(Str2));
293
294 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
295 return B.CreateNeg(
296 B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()));
297
298 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
299 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
300
301 // strcmp(P, "x") -> memcmp(P, "x", 2)
302 uint64_t Len1 = GetStringLength(Str1P);
303 uint64_t Len2 = GetStringLength(Str2P);
304 if (Len1 && Len2) {
Sanjay Pateld3112a52016-01-19 19:46:10 +0000305 return emitMemCmp(Str1P, Str2P,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000306 ConstantInt::get(DL.getIntPtrType(CI->getContext()),
Chris Bienemanad070d02014-09-17 20:55:46 +0000307 std::min(Len1, Len2)),
308 B, DL, TLI);
309 }
Meador Inge7fb2f732012-10-13 16:45:32 +0000310
Chris Bienemanad070d02014-09-17 20:55:46 +0000311 return nullptr;
312}
313
314Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000315 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
316 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
317 return ConstantInt::get(CI->getType(), 0);
318
319 // Get the length argument if it is constant.
320 uint64_t Length;
321 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
322 Length = LengthArg->getZExtValue();
323 else
324 return nullptr;
325
326 if (Length == 0) // strncmp(x,y,0) -> 0
327 return ConstantInt::get(CI->getType(), 0);
328
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000329 if (Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
Sanjay Pateld3112a52016-01-19 19:46:10 +0000330 return emitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +0000331
332 StringRef Str1, Str2;
333 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
334 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
335
336 // strncmp(x, y) -> cnst (if both x and y are constant strings)
337 if (HasStr1 && HasStr2) {
338 StringRef SubStr1 = Str1.substr(0, Length);
339 StringRef SubStr2 = Str2.substr(0, Length);
340 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
341 }
342
343 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
344 return B.CreateNeg(
345 B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()));
346
347 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
348 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
349
350 return nullptr;
351}
352
353Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000354 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
355 if (Dst == Src) // strcpy(x,x) -> x
356 return Src;
357
Chris Bienemanad070d02014-09-17 20:55:46 +0000358 // See if we can get the length of the input string.
359 uint64_t Len = GetStringLength(Src);
360 if (Len == 0)
361 return nullptr;
362
363 // We have enough information to now generate the memcpy call to do the
364 // copy for us. Make a memcpy to copy the nul byte with align = 1.
365 B.CreateMemCpy(Dst, Src,
Pete Cooper67cf9a72015-11-19 05:56:52 +0000366 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len), 1);
Chris Bienemanad070d02014-09-17 20:55:46 +0000367 return Dst;
368}
369
370Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilder<> &B) {
371 Function *Callee = CI->getCalledFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +0000372 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
373 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
Sanjay Pateld3112a52016-01-19 19:46:10 +0000374 Value *StrLen = emitStrLen(Src, B, DL, TLI);
David Blaikieaa41cd52015-04-03 21:33:42 +0000375 return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000376 }
377
378 // See if we can get the length of the input string.
379 uint64_t Len = GetStringLength(Src);
380 if (Len == 0)
381 return nullptr;
382
Davide Italianob7487e62015-11-02 23:07:14 +0000383 Type *PT = Callee->getFunctionType()->getParamType(0);
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000384 Value *LenV = ConstantInt::get(DL.getIntPtrType(PT), Len);
Sanjay Pateld707db92015-12-31 16:10:49 +0000385 Value *DstEnd = B.CreateGEP(B.getInt8Ty(), Dst,
386 ConstantInt::get(DL.getIntPtrType(PT), Len - 1));
Chris Bienemanad070d02014-09-17 20:55:46 +0000387
388 // We have enough information to now generate the memcpy call to do the
389 // copy for us. Make a memcpy to copy the nul byte with align = 1.
Pete Cooper67cf9a72015-11-19 05:56:52 +0000390 B.CreateMemCpy(Dst, Src, LenV, 1);
Chris Bienemanad070d02014-09-17 20:55:46 +0000391 return DstEnd;
392}
393
394Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) {
395 Function *Callee = CI->getCalledFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +0000396 Value *Dst = CI->getArgOperand(0);
397 Value *Src = CI->getArgOperand(1);
398 Value *LenOp = CI->getArgOperand(2);
399
400 // See if we can get the length of the input string.
401 uint64_t SrcLen = GetStringLength(Src);
402 if (SrcLen == 0)
403 return nullptr;
404 --SrcLen;
405
406 if (SrcLen == 0) {
407 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
408 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
Meador Inge7fb2f732012-10-13 16:45:32 +0000409 return Dst;
410 }
Meador Inge7fb2f732012-10-13 16:45:32 +0000411
Chris Bienemanad070d02014-09-17 20:55:46 +0000412 uint64_t Len;
413 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
414 Len = LengthArg->getZExtValue();
415 else
416 return nullptr;
Meador Inge7fb2f732012-10-13 16:45:32 +0000417
Chris Bienemanad070d02014-09-17 20:55:46 +0000418 if (Len == 0)
419 return Dst; // strncpy(x, y, 0) -> x
Meador Inge7fb2f732012-10-13 16:45:32 +0000420
Chris Bienemanad070d02014-09-17 20:55:46 +0000421 // Let strncpy handle the zero padding
422 if (Len > SrcLen + 1)
423 return nullptr;
Meador Inge7fb2f732012-10-13 16:45:32 +0000424
Davide Italianob7487e62015-11-02 23:07:14 +0000425 Type *PT = Callee->getFunctionType()->getParamType(0);
Chris Bienemanad070d02014-09-17 20:55:46 +0000426 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
Pete Cooper67cf9a72015-11-19 05:56:52 +0000427 B.CreateMemCpy(Dst, Src, ConstantInt::get(DL.getIntPtrType(PT), Len), 1);
Meador Inge7fb2f732012-10-13 16:45:32 +0000428
Chris Bienemanad070d02014-09-17 20:55:46 +0000429 return Dst;
430}
Meador Inge7fb2f732012-10-13 16:45:32 +0000431
Chris Bienemanad070d02014-09-17 20:55:46 +0000432Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000433 Value *Src = CI->getArgOperand(0);
434
435 // Constant folding: strlen("xyz") -> 3
436 if (uint64_t Len = GetStringLength(Src))
437 return ConstantInt::get(CI->getType(), Len - 1);
438
David L Kreitzer752c1442016-04-13 14:31:06 +0000439 // If s is a constant pointer pointing to a string literal, we can fold
440 // strlen(s + x) to strlen(s) - x, when x is known to be in the range
441 // [0, strlen(s)] or the string has a single null terminator '\0' at the end.
442 // We only try to simplify strlen when the pointer s points to an array
443 // of i8. Otherwise, we would need to scale the offset x before doing the
444 // subtraction. This will make the optimization more complex, and it's not
445 // very useful because calling strlen for a pointer of other types is
446 // very uncommon.
447 if (GEPOperator *GEP = dyn_cast<GEPOperator>(Src)) {
448 if (!isGEPBasedOnPointerToString(GEP))
449 return nullptr;
450
451 StringRef Str;
452 if (getConstantStringInfo(GEP->getOperand(0), Str, 0, false)) {
453 size_t NullTermIdx = Str.find('\0');
454
455 // If the string does not have '\0', leave it to strlen to compute
456 // its length.
457 if (NullTermIdx == StringRef::npos)
458 return nullptr;
459
460 Value *Offset = GEP->getOperand(2);
461 unsigned BitWidth = Offset->getType()->getIntegerBitWidth();
462 APInt KnownZero(BitWidth, 0);
463 APInt KnownOne(BitWidth, 0);
Hal Finkel3ca4a6b2016-12-15 03:02:15 +0000464 computeKnownBits(Offset, KnownZero, KnownOne, DL, 0, CI, nullptr);
David L Kreitzer752c1442016-04-13 14:31:06 +0000465 KnownZero.flipAllBits();
466 size_t ArrSize =
467 cast<ArrayType>(GEP->getSourceElementType())->getNumElements();
468
469 // KnownZero's bits are flipped, so zeros in KnownZero now represent
470 // bits known to be zeros in Offset, and ones in KnowZero represent
471 // bits unknown in Offset. Therefore, Offset is known to be in range
472 // [0, NullTermIdx] when the flipped KnownZero is non-negative and
473 // unsigned-less-than NullTermIdx.
474 //
475 // If Offset is not provably in the range [0, NullTermIdx], we can still
476 // optimize if we can prove that the program has undefined behavior when
477 // Offset is outside that range. That is the case when GEP->getOperand(0)
478 // is a pointer to an object whose memory extent is NullTermIdx+1.
479 if ((KnownZero.isNonNegative() && KnownZero.ule(NullTermIdx)) ||
480 (GEP->isInBounds() && isa<GlobalVariable>(GEP->getOperand(0)) &&
481 NullTermIdx == ArrSize - 1))
482 return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx),
483 Offset);
484 }
485
486 return nullptr;
487 }
488
Chris Bienemanad070d02014-09-17 20:55:46 +0000489 // strlen(x?"foo":"bars") --> x ? 3 : 4
490 if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {
491 uint64_t LenTrue = GetStringLength(SI->getTrueValue());
492 uint64_t LenFalse = GetStringLength(SI->getFalseValue());
493 if (LenTrue && LenFalse) {
494 Function *Caller = CI->getParent()->getParent();
495 emitOptimizationRemark(CI->getContext(), "simplify-libcalls", *Caller,
496 SI->getDebugLoc(),
497 "folded strlen(select) to select of constants");
498 return B.CreateSelect(SI->getCondition(),
499 ConstantInt::get(CI->getType(), LenTrue - 1),
500 ConstantInt::get(CI->getType(), LenFalse - 1));
501 }
Meador Inge7fb2f732012-10-13 16:45:32 +0000502 }
Meador Inge7fb2f732012-10-13 16:45:32 +0000503
Chris Bienemanad070d02014-09-17 20:55:46 +0000504 // strlen(x) != 0 --> *x != 0
505 // strlen(x) == 0 --> *x == 0
506 if (isOnlyUsedInZeroEqualityComparison(CI))
507 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
Meador Inge17418502012-10-13 16:45:37 +0000508
Chris Bienemanad070d02014-09-17 20:55:46 +0000509 return nullptr;
510}
Meador Inge17418502012-10-13 16:45:37 +0000511
Chris Bienemanad070d02014-09-17 20:55:46 +0000512Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000513 StringRef S1, S2;
514 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
515 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
Meador Inge17418502012-10-13 16:45:37 +0000516
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000517 // strpbrk(s, "") -> nullptr
518 // strpbrk("", s) -> nullptr
Chris Bienemanad070d02014-09-17 20:55:46 +0000519 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
520 return Constant::getNullValue(CI->getType());
Meador Inge17418502012-10-13 16:45:37 +0000521
Chris Bienemanad070d02014-09-17 20:55:46 +0000522 // Constant folding.
523 if (HasS1 && HasS2) {
524 size_t I = S1.find_first_of(S2);
525 if (I == StringRef::npos) // No match.
Meador Inge17418502012-10-13 16:45:37 +0000526 return Constant::getNullValue(CI->getType());
527
Sanjay Pateld707db92015-12-31 16:10:49 +0000528 return B.CreateGEP(B.getInt8Ty(), CI->getArgOperand(0), B.getInt64(I),
529 "strpbrk");
Meador Inge17418502012-10-13 16:45:37 +0000530 }
Meador Inge17418502012-10-13 16:45:37 +0000531
Chris Bienemanad070d02014-09-17 20:55:46 +0000532 // strpbrk(s, "a") -> strchr(s, 'a')
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000533 if (HasS2 && S2.size() == 1)
Sanjay Pateld3112a52016-01-19 19:46:10 +0000534 return emitStrChr(CI->getArgOperand(0), S2[0], B, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +0000535
536 return nullptr;
537}
538
539Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000540 Value *EndPtr = CI->getArgOperand(1);
541 if (isa<ConstantPointerNull>(EndPtr)) {
542 // With a null EndPtr, this function won't capture the main argument.
543 // It would be readonly too, except that it still may write to errno.
544 CI->addAttribute(1, Attribute::NoCapture);
545 }
546
547 return nullptr;
548}
549
550Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000551 StringRef S1, S2;
552 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
553 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
554
555 // strspn(s, "") -> 0
556 // strspn("", s) -> 0
557 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
558 return Constant::getNullValue(CI->getType());
559
560 // Constant folding.
561 if (HasS1 && HasS2) {
562 size_t Pos = S1.find_first_not_of(S2);
563 if (Pos == StringRef::npos)
564 Pos = S1.size();
565 return ConstantInt::get(CI->getType(), Pos);
566 }
567
568 return nullptr;
569}
570
571Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000572 StringRef S1, S2;
573 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
574 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
575
576 // strcspn("", s) -> 0
577 if (HasS1 && S1.empty())
578 return Constant::getNullValue(CI->getType());
579
580 // Constant folding.
581 if (HasS1 && HasS2) {
582 size_t Pos = S1.find_first_of(S2);
583 if (Pos == StringRef::npos)
584 Pos = S1.size();
585 return ConstantInt::get(CI->getType(), Pos);
586 }
587
588 // strcspn(s, "") -> strlen(s)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000589 if (HasS2 && S2.empty())
Sanjay Pateld3112a52016-01-19 19:46:10 +0000590 return emitStrLen(CI->getArgOperand(0), B, DL, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +0000591
592 return nullptr;
593}
594
595Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000596 // fold strstr(x, x) -> x.
597 if (CI->getArgOperand(0) == CI->getArgOperand(1))
598 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
599
600 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000601 if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
Sanjay Pateld3112a52016-01-19 19:46:10 +0000602 Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +0000603 if (!StrLen)
Craig Topperf40110f2014-04-25 05:29:35 +0000604 return nullptr;
Sanjay Pateld3112a52016-01-19 19:46:10 +0000605 Value *StrNCmp = emitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
Chris Bienemanad070d02014-09-17 20:55:46 +0000606 StrLen, B, DL, TLI);
607 if (!StrNCmp)
Craig Topperf40110f2014-04-25 05:29:35 +0000608 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000609 for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) {
610 ICmpInst *Old = cast<ICmpInst>(*UI++);
611 Value *Cmp =
612 B.CreateICmp(Old->getPredicate(), StrNCmp,
613 ConstantInt::getNullValue(StrNCmp->getType()), "cmp");
614 replaceAllUsesWith(Old, Cmp);
Meador Inge17418502012-10-13 16:45:37 +0000615 }
Chris Bienemanad070d02014-09-17 20:55:46 +0000616 return CI;
617 }
Meador Inge17418502012-10-13 16:45:37 +0000618
Chris Bienemanad070d02014-09-17 20:55:46 +0000619 // See if either input string is a constant string.
620 StringRef SearchStr, ToFindStr;
621 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
622 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
623
624 // fold strstr(x, "") -> x.
625 if (HasStr2 && ToFindStr.empty())
626 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
627
628 // If both strings are known, constant fold it.
629 if (HasStr1 && HasStr2) {
630 size_t Offset = SearchStr.find(ToFindStr);
631
632 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
Meador Inge17418502012-10-13 16:45:37 +0000633 return Constant::getNullValue(CI->getType());
634
Chris Bienemanad070d02014-09-17 20:55:46 +0000635 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
Sanjay Pateld3112a52016-01-19 19:46:10 +0000636 Value *Result = castToCStr(CI->getArgOperand(0), B);
Chris Bienemanad070d02014-09-17 20:55:46 +0000637 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
638 return B.CreateBitCast(Result, CI->getType());
Meador Inge17418502012-10-13 16:45:37 +0000639 }
Meador Inge17418502012-10-13 16:45:37 +0000640
Chris Bienemanad070d02014-09-17 20:55:46 +0000641 // fold strstr(x, "y") -> strchr(x, 'y').
642 if (HasStr2 && ToFindStr.size() == 1) {
Sanjay Pateld3112a52016-01-19 19:46:10 +0000643 Value *StrChr = emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +0000644 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr;
645 }
646 return nullptr;
647}
Meador Inge40b6fac2012-10-15 03:47:37 +0000648
Benjamin Kramer691363e2015-03-21 15:36:21 +0000649Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilder<> &B) {
Benjamin Kramer691363e2015-03-21 15:36:21 +0000650 Value *SrcStr = CI->getArgOperand(0);
651 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
652 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
653
654 // memchr(x, y, 0) -> null
655 if (LenC && LenC->isNullValue())
656 return Constant::getNullValue(CI->getType());
657
Benjamin Kramer7857d722015-03-21 21:09:33 +0000658 // From now on we need at least constant length and string.
Benjamin Kramer691363e2015-03-21 15:36:21 +0000659 StringRef Str;
Benjamin Kramer7857d722015-03-21 21:09:33 +0000660 if (!LenC || !getConstantStringInfo(SrcStr, Str, 0, /*TrimAtNul=*/false))
Benjamin Kramer691363e2015-03-21 15:36:21 +0000661 return nullptr;
662
663 // Truncate the string to LenC. If Str is smaller than LenC we will still only
664 // scan the string, as reading past the end of it is undefined and we can just
665 // return null if we don't find the char.
666 Str = Str.substr(0, LenC->getZExtValue());
667
Benjamin Kramer7857d722015-03-21 21:09:33 +0000668 // If the char is variable but the input str and length are not we can turn
669 // this memchr call into a simple bit field test. Of course this only works
670 // when the return value is only checked against null.
671 //
672 // It would be really nice to reuse switch lowering here but we can't change
673 // the CFG at this point.
674 //
675 // memchr("\r\n", C, 2) != nullptr -> (C & ((1 << '\r') | (1 << '\n'))) != 0
676 // after bounds check.
677 if (!CharC && !Str.empty() && isOnlyUsedInZeroEqualityComparison(CI)) {
Benjamin Kramerd6aa0ec2015-03-21 22:04:26 +0000678 unsigned char Max =
679 *std::max_element(reinterpret_cast<const unsigned char *>(Str.begin()),
680 reinterpret_cast<const unsigned char *>(Str.end()));
Benjamin Kramer7857d722015-03-21 21:09:33 +0000681
682 // Make sure the bit field we're about to create fits in a register on the
683 // target.
684 // FIXME: On a 64 bit architecture this prevents us from using the
685 // interesting range of alpha ascii chars. We could do better by emitting
686 // two bitfields or shifting the range by 64 if no lower chars are used.
687 if (!DL.fitsInLegalInteger(Max + 1))
688 return nullptr;
689
690 // For the bit field use a power-of-2 type with at least 8 bits to avoid
691 // creating unnecessary illegal types.
692 unsigned char Width = NextPowerOf2(std::max((unsigned char)7, Max));
693
694 // Now build the bit field.
695 APInt Bitfield(Width, 0);
696 for (char C : Str)
697 Bitfield.setBit((unsigned char)C);
698 Value *BitfieldC = B.getInt(Bitfield);
699
700 // First check that the bit field access is within bounds.
701 Value *C = B.CreateZExtOrTrunc(CI->getArgOperand(1), BitfieldC->getType());
702 Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width),
703 "memchr.bounds");
704
705 // Create code that checks if the given bit is set in the field.
706 Value *Shl = B.CreateShl(B.getIntN(Width, 1ULL), C);
707 Value *Bits = B.CreateIsNotNull(B.CreateAnd(Shl, BitfieldC), "memchr.bits");
708
709 // Finally merge both checks and cast to pointer type. The inttoptr
710 // implicitly zexts the i1 to intptr type.
711 return B.CreateIntToPtr(B.CreateAnd(Bounds, Bits, "memchr"), CI->getType());
712 }
713
714 // Check if all arguments are constants. If so, we can constant fold.
715 if (!CharC)
716 return nullptr;
717
Benjamin Kramer691363e2015-03-21 15:36:21 +0000718 // Compute the offset.
719 size_t I = Str.find(CharC->getSExtValue() & 0xFF);
720 if (I == StringRef::npos) // Didn't find the char. memchr returns null.
721 return Constant::getNullValue(CI->getType());
722
723 // memchr(s+n,c,l) -> gep(s+n+i,c)
David Blaikie3909da72015-03-30 20:42:56 +0000724 return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "memchr");
Benjamin Kramer691363e2015-03-21 15:36:21 +0000725}
726
Chris Bienemanad070d02014-09-17 20:55:46 +0000727Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000728 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
Meador Inge40b6fac2012-10-15 03:47:37 +0000729
Chris Bienemanad070d02014-09-17 20:55:46 +0000730 if (LHS == RHS) // memcmp(s,s,x) -> 0
731 return Constant::getNullValue(CI->getType());
Meador Inge40b6fac2012-10-15 03:47:37 +0000732
Chris Bienemanad070d02014-09-17 20:55:46 +0000733 // Make sure we have a constant length.
734 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
735 if (!LenC)
Craig Topperf40110f2014-04-25 05:29:35 +0000736 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000737 uint64_t Len = LenC->getZExtValue();
738
739 if (Len == 0) // memcmp(s1,s2,0) -> 0
740 return Constant::getNullValue(CI->getType());
741
742 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
743 if (Len == 1) {
Sanjay Pateld3112a52016-01-19 19:46:10 +0000744 Value *LHSV = B.CreateZExt(B.CreateLoad(castToCStr(LHS, B), "lhsc"),
Chris Bienemanad070d02014-09-17 20:55:46 +0000745 CI->getType(), "lhsv");
Sanjay Pateld3112a52016-01-19 19:46:10 +0000746 Value *RHSV = B.CreateZExt(B.CreateLoad(castToCStr(RHS, B), "rhsc"),
Chris Bienemanad070d02014-09-17 20:55:46 +0000747 CI->getType(), "rhsv");
748 return B.CreateSub(LHSV, RHSV, "chardiff");
Meador Inge40b6fac2012-10-15 03:47:37 +0000749 }
Meador Inge40b6fac2012-10-15 03:47:37 +0000750
Chad Rosierdc655322015-08-28 18:30:18 +0000751 // memcmp(S1,S2,N/8)==0 -> (*(intN_t*)S1 != *(intN_t*)S2)==0
752 if (DL.isLegalInteger(Len * 8) && isOnlyUsedInZeroEqualityComparison(CI)) {
753
754 IntegerType *IntType = IntegerType::get(CI->getContext(), Len * 8);
755 unsigned PrefAlignment = DL.getPrefTypeAlignment(IntType);
756
757 if (getKnownAlignment(LHS, DL, CI) >= PrefAlignment &&
758 getKnownAlignment(RHS, DL, CI) >= PrefAlignment) {
759
760 Type *LHSPtrTy =
761 IntType->getPointerTo(LHS->getType()->getPointerAddressSpace());
762 Type *RHSPtrTy =
763 IntType->getPointerTo(RHS->getType()->getPointerAddressSpace());
764
Sanjay Pateld707db92015-12-31 16:10:49 +0000765 Value *LHSV =
766 B.CreateLoad(B.CreateBitCast(LHS, LHSPtrTy, "lhsc"), "lhsv");
767 Value *RHSV =
768 B.CreateLoad(B.CreateBitCast(RHS, RHSPtrTy, "rhsc"), "rhsv");
Chad Rosierdc655322015-08-28 18:30:18 +0000769
770 return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp");
771 }
772 }
773
Chris Bienemanad070d02014-09-17 20:55:46 +0000774 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
775 StringRef LHSStr, RHSStr;
776 if (getConstantStringInfo(LHS, LHSStr) &&
777 getConstantStringInfo(RHS, RHSStr)) {
778 // Make sure we're not reading out-of-bounds memory.
779 if (Len > LHSStr.size() || Len > RHSStr.size())
Craig Topperf40110f2014-04-25 05:29:35 +0000780 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000781 // Fold the memcmp and normalize the result. This way we get consistent
782 // results across multiple platforms.
783 uint64_t Ret = 0;
784 int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
785 if (Cmp < 0)
786 Ret = -1;
787 else if (Cmp > 0)
788 Ret = 1;
789 return ConstantInt::get(CI->getType(), Ret);
Meador Inge000dbcc2012-10-18 18:12:40 +0000790 }
Meador Inge000dbcc2012-10-18 18:12:40 +0000791
Chris Bienemanad070d02014-09-17 20:55:46 +0000792 return nullptr;
793}
Meador Inge9a6a1902012-10-31 00:20:56 +0000794
Chris Bienemanad070d02014-09-17 20:55:46 +0000795Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000796 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
797 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
Pete Cooper67cf9a72015-11-19 05:56:52 +0000798 CI->getArgOperand(2), 1);
Chris Bienemanad070d02014-09-17 20:55:46 +0000799 return CI->getArgOperand(0);
800}
Meador Inge05a625a2012-10-31 14:58:26 +0000801
Chris Bienemanad070d02014-09-17 20:55:46 +0000802Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000803 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
804 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
Pete Cooper67cf9a72015-11-19 05:56:52 +0000805 CI->getArgOperand(2), 1);
Chris Bienemanad070d02014-09-17 20:55:46 +0000806 return CI->getArgOperand(0);
807}
Meador Ingebcd88ef72012-11-10 15:16:48 +0000808
Sanjay Patel980b2802016-01-26 16:17:24 +0000809// TODO: Does this belong in BuildLibCalls or should all of those similar
810// functions be moved here?
811static Value *emitCalloc(Value *Num, Value *Size, const AttributeSet &Attrs,
812 IRBuilder<> &B, const TargetLibraryInfo &TLI) {
813 LibFunc::Func Func;
814 if (!TLI.getLibFunc("calloc", Func) || !TLI.has(Func))
815 return nullptr;
816
817 Module *M = B.GetInsertBlock()->getModule();
818 const DataLayout &DL = M->getDataLayout();
819 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
820 Value *Calloc = M->getOrInsertFunction("calloc", Attrs, B.getInt8PtrTy(),
821 PtrType, PtrType, nullptr);
822 CallInst *CI = B.CreateCall(Calloc, { Num, Size }, "calloc");
823
824 if (const auto *F = dyn_cast<Function>(Calloc->stripPointerCasts()))
825 CI->setCallingConv(F->getCallingConv());
826
827 return CI;
828}
829
830/// Fold memset[_chk](malloc(n), 0, n) --> calloc(1, n).
831static Value *foldMallocMemset(CallInst *Memset, IRBuilder<> &B,
832 const TargetLibraryInfo &TLI) {
833 // This has to be a memset of zeros (bzero).
834 auto *FillValue = dyn_cast<ConstantInt>(Memset->getArgOperand(1));
835 if (!FillValue || FillValue->getZExtValue() != 0)
836 return nullptr;
837
838 // TODO: We should handle the case where the malloc has more than one use.
839 // This is necessary to optimize common patterns such as when the result of
840 // the malloc is checked against null or when a memset intrinsic is used in
841 // place of a memset library call.
842 auto *Malloc = dyn_cast<CallInst>(Memset->getArgOperand(0));
843 if (!Malloc || !Malloc->hasOneUse())
844 return nullptr;
845
846 // Is the inner call really malloc()?
847 Function *InnerCallee = Malloc->getCalledFunction();
848 LibFunc::Func Func;
Ahmed Bougachad765a822016-04-27 19:04:35 +0000849 if (!TLI.getLibFunc(*InnerCallee, Func) || !TLI.has(Func) ||
Sanjay Patel980b2802016-01-26 16:17:24 +0000850 Func != LibFunc::malloc)
851 return nullptr;
852
Sanjay Patel980b2802016-01-26 16:17:24 +0000853 // The memset must cover the same number of bytes that are malloc'd.
854 if (Memset->getArgOperand(2) != Malloc->getArgOperand(0))
855 return nullptr;
856
857 // Replace the malloc with a calloc. We need the data layout to know what the
858 // actual size of a 'size_t' parameter is.
859 B.SetInsertPoint(Malloc->getParent(), ++Malloc->getIterator());
860 const DataLayout &DL = Malloc->getModule()->getDataLayout();
861 IntegerType *SizeType = DL.getIntPtrType(B.GetInsertBlock()->getContext());
862 Value *Calloc = emitCalloc(ConstantInt::get(SizeType, 1),
863 Malloc->getArgOperand(0), Malloc->getAttributes(),
864 B, TLI);
865 if (!Calloc)
866 return nullptr;
867
868 Malloc->replaceAllUsesWith(Calloc);
869 Malloc->eraseFromParent();
870
871 return Calloc;
872}
873
Chris Bienemanad070d02014-09-17 20:55:46 +0000874Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B) {
Sanjay Patel980b2802016-01-26 16:17:24 +0000875 if (auto *Calloc = foldMallocMemset(CI, B, *TLI))
876 return Calloc;
877
Chris Bienemanad070d02014-09-17 20:55:46 +0000878 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
879 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
880 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
881 return CI->getArgOperand(0);
882}
Meador Inged4825782012-11-11 06:49:03 +0000883
Meador Inge193e0352012-11-13 04:16:17 +0000884//===----------------------------------------------------------------------===//
885// Math Library Optimizations
886//===----------------------------------------------------------------------===//
887
Matthias Braund34e4d22014-12-03 21:46:33 +0000888/// Return a variant of Val with float type.
889/// Currently this works in two cases: If Val is an FPExtension of a float
890/// value to something bigger, simply return the operand.
891/// If Val is a ConstantFP but can be converted to a float ConstantFP without
892/// loss of precision do so.
893static Value *valueHasFloatPrecision(Value *Val) {
894 if (FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) {
895 Value *Op = Cast->getOperand(0);
896 if (Op->getType()->isFloatTy())
897 return Op;
898 }
899 if (ConstantFP *Const = dyn_cast<ConstantFP>(Val)) {
900 APFloat F = Const->getValueAPF();
Matthias Braun395a82f2014-12-03 22:10:39 +0000901 bool losesInfo;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000902 (void)F.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
Matthias Braun395a82f2014-12-03 22:10:39 +0000903 &losesInfo);
904 if (!losesInfo)
Matthias Braund34e4d22014-12-03 21:46:33 +0000905 return ConstantFP::get(Const->getContext(), F);
906 }
907 return nullptr;
908}
909
Sanjay Patel4e971da2016-01-21 18:01:57 +0000910/// Shrink double -> float for unary functions like 'floor'.
911static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B,
912 bool CheckRetType) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000913 Function *Callee = CI->getCalledFunction();
Ahmed Bougachad765a822016-04-27 19:04:35 +0000914 // We know this libcall has a valid prototype, but we don't know which.
915 if (!CI->getType()->isDoubleTy())
Chris Bienemanad070d02014-09-17 20:55:46 +0000916 return nullptr;
Meador Inge193e0352012-11-13 04:16:17 +0000917
Chris Bienemanad070d02014-09-17 20:55:46 +0000918 if (CheckRetType) {
919 // Check if all the uses for function like 'sin' are converted to float.
920 for (User *U : CI->users()) {
921 FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
922 if (!Cast || !Cast->getType()->isFloatTy())
923 return nullptr;
Meador Inge193e0352012-11-13 04:16:17 +0000924 }
Meador Inge193e0352012-11-13 04:16:17 +0000925 }
Chris Bienemanad070d02014-09-17 20:55:46 +0000926
927 // If this is something like 'floor((double)floatval)', convert to floorf.
Matthias Braund34e4d22014-12-03 21:46:33 +0000928 Value *V = valueHasFloatPrecision(CI->getArgOperand(0));
929 if (V == nullptr)
Chris Bienemanad070d02014-09-17 20:55:46 +0000930 return nullptr;
Sanjay Patelaa231142015-12-31 21:52:31 +0000931
932 // Propagate fast-math flags from the existing call to the new call.
933 IRBuilder<>::FastMathFlagGuard Guard(B);
Sanjay Patela2528152016-01-12 18:03:37 +0000934 B.setFastMathFlags(CI->getFastMathFlags());
Chris Bienemanad070d02014-09-17 20:55:46 +0000935
936 // floor((double)floatval) -> (double)floorf(floatval)
Sanjay Patel848309d2014-10-23 21:52:45 +0000937 if (Callee->isIntrinsic()) {
Sanjay Patelaf674fb2015-12-14 17:24:23 +0000938 Module *M = CI->getModule();
Pete Cooper9e1d3352015-05-20 17:16:39 +0000939 Intrinsic::ID IID = Callee->getIntrinsicID();
Sanjay Patel848309d2014-10-23 21:52:45 +0000940 Function *F = Intrinsic::getDeclaration(M, IID, B.getFloatTy());
941 V = B.CreateCall(F, V);
942 } else {
943 // The call is a library call rather than an intrinsic.
Sanjay Pateld3112a52016-01-19 19:46:10 +0000944 V = emitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
Sanjay Patel848309d2014-10-23 21:52:45 +0000945 }
946
Chris Bienemanad070d02014-09-17 20:55:46 +0000947 return B.CreateFPExt(V, B.getDoubleTy());
948}
Meador Inge193e0352012-11-13 04:16:17 +0000949
Sanjay Patel4e971da2016-01-21 18:01:57 +0000950/// Shrink double -> float for binary functions like 'fmin/fmax'.
951static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +0000952 Function *Callee = CI->getCalledFunction();
Ahmed Bougachad765a822016-04-27 19:04:35 +0000953 // We know this libcall has a valid prototype, but we don't know which.
954 if (!CI->getType()->isDoubleTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000955 return nullptr;
Meador Inge193e0352012-11-13 04:16:17 +0000956
Chris Bienemanad070d02014-09-17 20:55:46 +0000957 // If this is something like 'fmin((double)floatval1, (double)floatval2)',
Matthias Braund34e4d22014-12-03 21:46:33 +0000958 // or fmin(1.0, (double)floatval), then we convert it to fminf.
959 Value *V1 = valueHasFloatPrecision(CI->getArgOperand(0));
960 if (V1 == nullptr)
961 return nullptr;
962 Value *V2 = valueHasFloatPrecision(CI->getArgOperand(1));
963 if (V2 == nullptr)
Craig Topperf40110f2014-04-25 05:29:35 +0000964 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +0000965
Sanjay Patelbee05ca2015-12-31 23:40:59 +0000966 // Propagate fast-math flags from the existing call to the new call.
967 IRBuilder<>::FastMathFlagGuard Guard(B);
Sanjay Patela2528152016-01-12 18:03:37 +0000968 B.setFastMathFlags(CI->getFastMathFlags());
Sanjay Patelbee05ca2015-12-31 23:40:59 +0000969
Chris Bienemanad070d02014-09-17 20:55:46 +0000970 // fmin((double)floatval1, (double)floatval2)
Matthias Braund34e4d22014-12-03 21:46:33 +0000971 // -> (double)fminf(floatval1, floatval2)
Sanjay Patel848309d2014-10-23 21:52:45 +0000972 // TODO: Handle intrinsics in the same way as in optimizeUnaryDoubleFP().
Sanjay Pateld3112a52016-01-19 19:46:10 +0000973 Value *V = emitBinaryFloatFnCall(V1, V2, Callee->getName(), B,
Matthias Braund34e4d22014-12-03 21:46:33 +0000974 Callee->getAttributes());
Chris Bienemanad070d02014-09-17 20:55:46 +0000975 return B.CreateFPExt(V, B.getDoubleTy());
976}
977
978Value *LibCallSimplifier::optimizeCos(CallInst *CI, IRBuilder<> &B) {
979 Function *Callee = CI->getCalledFunction();
980 Value *Ret = nullptr;
Davide Italianoa3458772015-11-05 19:18:23 +0000981 StringRef Name = Callee->getName();
982 if (UnsafeFPShrink && Name == "cos" && hasFloatVersion(Name))
Chris Bienemanad070d02014-09-17 20:55:46 +0000983 Ret = optimizeUnaryDoubleFP(CI, B, true);
Bob Wilsond8d92d92013-11-03 06:48:38 +0000984
Chris Bienemanad070d02014-09-17 20:55:46 +0000985 // cos(-x) -> cos(x)
986 Value *Op1 = CI->getArgOperand(0);
987 if (BinaryOperator::isFNeg(Op1)) {
988 BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
989 return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
990 }
991 return Ret;
992}
Bob Wilsond8d92d92013-11-03 06:48:38 +0000993
Weiming Zhao82130722015-12-04 22:00:47 +0000994static Value *getPow(Value *InnerChain[33], unsigned Exp, IRBuilder<> &B) {
995 // Multiplications calculated using Addition Chains.
996 // Refer: http://wwwhomes.uni-bielefeld.de/achim/addition_chain.html
997
998 assert(Exp != 0 && "Incorrect exponent 0 not handled");
999
1000 if (InnerChain[Exp])
1001 return InnerChain[Exp];
1002
1003 static const unsigned AddChain[33][2] = {
1004 {0, 0}, // Unused.
1005 {0, 0}, // Unused (base case = pow1).
1006 {1, 1}, // Unused (pre-computed).
1007 {1, 2}, {2, 2}, {2, 3}, {3, 3}, {2, 5}, {4, 4},
1008 {1, 8}, {5, 5}, {1, 10}, {6, 6}, {4, 9}, {7, 7},
1009 {3, 12}, {8, 8}, {8, 9}, {2, 16}, {1, 18}, {10, 10},
1010 {6, 15}, {11, 11}, {3, 20}, {12, 12}, {8, 17}, {13, 13},
1011 {3, 24}, {14, 14}, {4, 25}, {15, 15}, {3, 28}, {16, 16},
1012 };
1013
1014 InnerChain[Exp] = B.CreateFMul(getPow(InnerChain, AddChain[Exp][0], B),
1015 getPow(InnerChain, AddChain[Exp][1], B));
1016 return InnerChain[Exp];
1017}
1018
Chris Bienemanad070d02014-09-17 20:55:46 +00001019Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {
1020 Function *Callee = CI->getCalledFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +00001021 Value *Ret = nullptr;
Davide Italianoa3458772015-11-05 19:18:23 +00001022 StringRef Name = Callee->getName();
1023 if (UnsafeFPShrink && Name == "pow" && hasFloatVersion(Name))
Chris Bienemanad070d02014-09-17 20:55:46 +00001024 Ret = optimizeUnaryDoubleFP(CI, B, true);
Bob Wilsond8d92d92013-11-03 06:48:38 +00001025
Chris Bienemanad070d02014-09-17 20:55:46 +00001026 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
Davide Italiano27da1312016-08-07 20:27:03 +00001027
1028 // pow(1.0, x) -> 1.0
1029 if (match(Op1, m_SpecificFP(1.0)))
1030 return Op1;
1031 // pow(2.0, x) -> llvm.exp2(x)
1032 if (match(Op1, m_SpecificFP(2.0))) {
1033 Value *Exp2 = Intrinsic::getDeclaration(CI->getModule(), Intrinsic::exp2,
1034 CI->getType());
1035 return B.CreateCall(Exp2, Op2, "exp2");
1036 }
1037
1038 // There's no llvm.exp10 intrinsic yet, but, maybe, some day there will
1039 // be one.
Chris Bienemanad070d02014-09-17 20:55:46 +00001040 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
Chris Bienemanad070d02014-09-17 20:55:46 +00001041 // pow(10.0, x) -> exp10(x)
1042 if (Op1C->isExactlyValue(10.0) &&
1043 hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f,
1044 LibFunc::exp10l))
Sanjay Pateld3112a52016-01-19 19:46:10 +00001045 return emitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B,
Chris Bienemanad070d02014-09-17 20:55:46 +00001046 Callee->getAttributes());
Bob Wilsond8d92d92013-11-03 06:48:38 +00001047 }
1048
Sanjay Patel6002e782016-01-12 17:30:37 +00001049 // pow(exp(x), y) -> exp(x * y)
Davide Italianoc8a79132015-11-03 20:32:23 +00001050 // pow(exp2(x), y) -> exp2(x * y)
Sanjay Patel6002e782016-01-12 17:30:37 +00001051 // We enable these only with fast-math. Besides rounding differences, the
1052 // transformation changes overflow and underflow behavior quite dramatically.
Davide Italianoc8a79132015-11-03 20:32:23 +00001053 // Example: x = 1000, y = 0.001.
1054 // pow(exp(x), y) = pow(inf, 0.001) = inf, whereas exp(x*y) = exp(1).
Sanjay Patel6002e782016-01-12 17:30:37 +00001055 auto *OpC = dyn_cast<CallInst>(Op1);
1056 if (OpC && OpC->hasUnsafeAlgebra() && CI->hasUnsafeAlgebra()) {
1057 LibFunc::Func Func;
1058 Function *OpCCallee = OpC->getCalledFunction();
1059 if (OpCCallee && TLI->getLibFunc(OpCCallee->getName(), Func) &&
1060 TLI->has(Func) && (Func == LibFunc::exp || Func == LibFunc::exp2)) {
Davide Italianoc8a79132015-11-03 20:32:23 +00001061 IRBuilder<>::FastMathFlagGuard Guard(B);
Sanjay Patela2528152016-01-12 18:03:37 +00001062 B.setFastMathFlags(CI->getFastMathFlags());
Sanjay Patel6002e782016-01-12 17:30:37 +00001063 Value *FMul = B.CreateFMul(OpC->getArgOperand(0), Op2, "mul");
Sanjay Pateld3112a52016-01-19 19:46:10 +00001064 return emitUnaryFloatFnCall(FMul, OpCCallee->getName(), B,
Sanjay Patel6002e782016-01-12 17:30:37 +00001065 OpCCallee->getAttributes());
Davide Italianoc8a79132015-11-03 20:32:23 +00001066 }
1067 }
1068
Chris Bienemanad070d02014-09-17 20:55:46 +00001069 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1070 if (!Op2C)
1071 return Ret;
1072
1073 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
1074 return ConstantFP::get(CI->getType(), 1.0);
1075
1076 if (Op2C->isExactlyValue(0.5) &&
1077 hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::sqrt, LibFunc::sqrtf,
1078 LibFunc::sqrtl) &&
1079 hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::fabs, LibFunc::fabsf,
1080 LibFunc::fabsl)) {
Davide Italianoc5cedd12015-11-18 23:21:32 +00001081
1082 // In -ffast-math, pow(x, 0.5) -> sqrt(x).
Sanjay Patel53ba88d2016-01-12 19:06:35 +00001083 if (CI->hasUnsafeAlgebra()) {
1084 IRBuilder<>::FastMathFlagGuard Guard(B);
1085 B.setFastMathFlags(CI->getFastMathFlags());
Davide Italiano873219c2016-08-10 06:33:32 +00001086
1087 // Unlike other math intrinsics, sqrt has differerent semantics
1088 // from the libc function. See LangRef for details.
1089 return emitUnaryFloatFnCall(Op1, TLI->getName(LibFunc::sqrt), B,
1090 Callee->getAttributes());
Sanjay Patel53ba88d2016-01-12 19:06:35 +00001091 }
Davide Italianoc5cedd12015-11-18 23:21:32 +00001092
Chris Bienemanad070d02014-09-17 20:55:46 +00001093 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1094 // This is faster than calling pow, and still handles negative zero
1095 // and negative infinity correctly.
Chris Bienemanad070d02014-09-17 20:55:46 +00001096 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
1097 Value *Inf = ConstantFP::getInfinity(CI->getType());
1098 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
Sanjay Pateld3112a52016-01-19 19:46:10 +00001099 Value *Sqrt = emitUnaryFloatFnCall(Op1, "sqrt", B, Callee->getAttributes());
Chris Bienemanad070d02014-09-17 20:55:46 +00001100 Value *FAbs =
Sanjay Pateld3112a52016-01-19 19:46:10 +00001101 emitUnaryFloatFnCall(Sqrt, "fabs", B, Callee->getAttributes());
Chris Bienemanad070d02014-09-17 20:55:46 +00001102 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
1103 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
1104 return Sel;
Bob Wilsond8d92d92013-11-03 06:48:38 +00001105 }
1106
Chris Bienemanad070d02014-09-17 20:55:46 +00001107 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1108 return Op1;
1109 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
1110 return B.CreateFMul(Op1, Op1, "pow2");
1111 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
1112 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip");
Weiming Zhao82130722015-12-04 22:00:47 +00001113
1114 // In -ffast-math, generate repeated fmul instead of generating pow(x, n).
Sanjay Patel81a63cd2016-01-19 18:15:12 +00001115 if (CI->hasUnsafeAlgebra()) {
Weiming Zhao82130722015-12-04 22:00:47 +00001116 APFloat V = abs(Op2C->getValueAPF());
1117 // We limit to a max of 7 fmul(s). Thus max exponent is 32.
1118 // This transformation applies to integer exponents only.
1119 if (V.compare(APFloat(V.getSemantics(), 32.0)) == APFloat::cmpGreaterThan ||
1120 !V.isInteger())
1121 return nullptr;
1122
1123 // We will memoize intermediate products of the Addition Chain.
1124 Value *InnerChain[33] = {nullptr};
1125 InnerChain[1] = Op1;
1126 InnerChain[2] = B.CreateFMul(Op1, Op1);
1127
1128 // We cannot readily convert a non-double type (like float) to a double.
1129 // So we first convert V to something which could be converted to double.
1130 bool ignored;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001131 V.convert(APFloat::IEEEdouble(), APFloat::rmTowardZero, &ignored);
Sanjay Patel81a63cd2016-01-19 18:15:12 +00001132
1133 // TODO: Should the new instructions propagate the 'fast' flag of the pow()?
Weiming Zhao82130722015-12-04 22:00:47 +00001134 Value *FMul = getPow(InnerChain, V.convertToDouble(), B);
1135 // For negative exponents simply compute the reciprocal.
1136 if (Op2C->isNegative())
1137 FMul = B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), FMul);
1138 return FMul;
1139 }
1140
Chris Bienemanad070d02014-09-17 20:55:46 +00001141 return nullptr;
1142}
Bob Wilsond8d92d92013-11-03 06:48:38 +00001143
Chris Bienemanad070d02014-09-17 20:55:46 +00001144Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) {
1145 Function *Callee = CI->getCalledFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +00001146 Value *Ret = nullptr;
Davide Italianoa3458772015-11-05 19:18:23 +00001147 StringRef Name = Callee->getName();
1148 if (UnsafeFPShrink && Name == "exp2" && hasFloatVersion(Name))
Chris Bienemanad070d02014-09-17 20:55:46 +00001149 Ret = optimizeUnaryDoubleFP(CI, B, true);
Bob Wilsond8d92d92013-11-03 06:48:38 +00001150
Chris Bienemanad070d02014-09-17 20:55:46 +00001151 Value *Op = CI->getArgOperand(0);
1152 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1153 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1154 LibFunc::Func LdExp = LibFunc::ldexpl;
1155 if (Op->getType()->isFloatTy())
1156 LdExp = LibFunc::ldexpf;
1157 else if (Op->getType()->isDoubleTy())
1158 LdExp = LibFunc::ldexp;
1159
1160 if (TLI->has(LdExp)) {
1161 Value *LdExpArg = nullptr;
1162 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1163 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
1164 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
1165 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1166 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
1167 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
1168 }
1169
1170 if (LdExpArg) {
1171 Constant *One = ConstantFP::get(CI->getContext(), APFloat(1.0f));
1172 if (!Op->getType()->isFloatTy())
1173 One = ConstantExpr::getFPExtend(One, Op->getType());
1174
Sanjay Patel0e603fc2016-01-21 22:31:18 +00001175 Module *M = CI->getModule();
Sanjay Patel042aed902016-01-21 22:41:16 +00001176 Value *NewCallee =
Chris Bienemanad070d02014-09-17 20:55:46 +00001177 M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +00001178 Op->getType(), B.getInt32Ty(), nullptr);
Sanjay Patel042aed902016-01-21 22:41:16 +00001179 CallInst *CI = B.CreateCall(NewCallee, {One, LdExpArg});
Chris Bienemanad070d02014-09-17 20:55:46 +00001180 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1181 CI->setCallingConv(F->getCallingConv());
1182
1183 return CI;
1184 }
1185 }
1186 return Ret;
1187}
1188
Sanjay Patel0ca42bb2014-10-14 20:43:11 +00001189Value *LibCallSimplifier::optimizeFabs(CallInst *CI, IRBuilder<> &B) {
1190 Function *Callee = CI->getCalledFunction();
Sanjay Patel0ca42bb2014-10-14 20:43:11 +00001191 Value *Ret = nullptr;
Davide Italianoa3458772015-11-05 19:18:23 +00001192 StringRef Name = Callee->getName();
1193 if (Name == "fabs" && hasFloatVersion(Name))
Sanjay Patel0ca42bb2014-10-14 20:43:11 +00001194 Ret = optimizeUnaryDoubleFP(CI, B, false);
Sanjay Patel0ca42bb2014-10-14 20:43:11 +00001195
Sanjay Patel0ca42bb2014-10-14 20:43:11 +00001196 Value *Op = CI->getArgOperand(0);
1197 if (Instruction *I = dyn_cast<Instruction>(Op)) {
1198 // Fold fabs(x * x) -> x * x; any squared FP value must already be positive.
1199 if (I->getOpcode() == Instruction::FMul)
1200 if (I->getOperand(0) == I->getOperand(1))
1201 return Op;
1202 }
1203 return Ret;
1204}
1205
Sanjay Patel57fd1dc2015-08-16 20:18:19 +00001206Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilder<> &B) {
Sanjay Patel9beec212016-01-21 22:58:01 +00001207 Function *Callee = CI->getCalledFunction();
Sanjay Patel57fd1dc2015-08-16 20:18:19 +00001208 // If we can shrink the call to a float function rather than a double
1209 // function, do that first.
Davide Italianoa3458772015-11-05 19:18:23 +00001210 StringRef Name = Callee->getName();
Sanjay Patelc7ddb7f2016-01-06 00:32:15 +00001211 if ((Name == "fmin" || Name == "fmax") && hasFloatVersion(Name))
1212 if (Value *Ret = optimizeBinaryDoubleFP(CI, B))
Sanjay Patel57fd1dc2015-08-16 20:18:19 +00001213 return Ret;
Sanjay Patel57fd1dc2015-08-16 20:18:19 +00001214
Benjamin Kramerbb70d752015-08-16 21:16:37 +00001215 IRBuilder<>::FastMathFlagGuard Guard(B);
Sanjay Patel57fd1dc2015-08-16 20:18:19 +00001216 FastMathFlags FMF;
Sanjay Patel29095ea2016-01-05 20:46:19 +00001217 if (CI->hasUnsafeAlgebra()) {
Sanjay Patel57fd1dc2015-08-16 20:18:19 +00001218 // Unsafe algebra sets all fast-math-flags to true.
1219 FMF.setUnsafeAlgebra();
1220 } else {
1221 // At a minimum, no-nans-fp-math must be true.
Sanjay Patel29095ea2016-01-05 20:46:19 +00001222 if (!CI->hasNoNaNs())
Sanjay Patel57fd1dc2015-08-16 20:18:19 +00001223 return nullptr;
1224 // No-signed-zeros is implied by the definitions of fmax/fmin themselves:
1225 // "Ideally, fmax would be sensitive to the sign of zero, for example
NAKAMURA Takumi0d725392015-09-07 00:26:54 +00001226 // fmax(-0. 0, +0. 0) would return +0; however, implementation in software
Sanjay Patel57fd1dc2015-08-16 20:18:19 +00001227 // might be impractical."
1228 FMF.setNoSignedZeros();
1229 FMF.setNoNaNs();
1230 }
Sanjay Patela2528152016-01-12 18:03:37 +00001231 B.setFastMathFlags(FMF);
Sanjay Patel57fd1dc2015-08-16 20:18:19 +00001232
1233 // We have a relaxed floating-point environment. We can ignore NaN-handling
1234 // and transform to a compare and select. We do not have to consider errno or
1235 // exceptions, because fmin/fmax do not have those.
1236 Value *Op0 = CI->getArgOperand(0);
1237 Value *Op1 = CI->getArgOperand(1);
1238 Value *Cmp = Callee->getName().startswith("fmin") ?
1239 B.CreateFCmpOLT(Op0, Op1) : B.CreateFCmpOGT(Op0, Op1);
1240 return B.CreateSelect(Cmp, Op0, Op1);
1241}
1242
Davide Italianob8b71332015-11-29 20:58:04 +00001243Value *LibCallSimplifier::optimizeLog(CallInst *CI, IRBuilder<> &B) {
1244 Function *Callee = CI->getCalledFunction();
1245 Value *Ret = nullptr;
1246 StringRef Name = Callee->getName();
1247 if (UnsafeFPShrink && hasFloatVersion(Name))
1248 Ret = optimizeUnaryDoubleFP(CI, B, true);
Davide Italianob8b71332015-11-29 20:58:04 +00001249
Sanjay Patele896ede2016-01-11 23:31:48 +00001250 if (!CI->hasUnsafeAlgebra())
Davide Italianob8b71332015-11-29 20:58:04 +00001251 return Ret;
1252 Value *Op1 = CI->getArgOperand(0);
1253 auto *OpC = dyn_cast<CallInst>(Op1);
Sanjay Patele896ede2016-01-11 23:31:48 +00001254
1255 // The earlier call must also be unsafe in order to do these transforms.
1256 if (!OpC || !OpC->hasUnsafeAlgebra())
Davide Italianob8b71332015-11-29 20:58:04 +00001257 return Ret;
1258
1259 // log(pow(x,y)) -> y*log(x)
1260 // This is only applicable to log, log2, log10.
1261 if (Name != "log" && Name != "log2" && Name != "log10")
1262 return Ret;
1263
1264 IRBuilder<>::FastMathFlagGuard Guard(B);
1265 FastMathFlags FMF;
1266 FMF.setUnsafeAlgebra();
Sanjay Patela2528152016-01-12 18:03:37 +00001267 B.setFastMathFlags(FMF);
Davide Italianob8b71332015-11-29 20:58:04 +00001268
1269 LibFunc::Func Func;
1270 Function *F = OpC->getCalledFunction();
Davide Italiano0b14f292015-11-29 21:58:56 +00001271 if (F && ((TLI->getLibFunc(F->getName(), Func) && TLI->has(Func) &&
1272 Func == LibFunc::pow) || F->getIntrinsicID() == Intrinsic::pow))
Davide Italianob8b71332015-11-29 20:58:04 +00001273 return B.CreateFMul(OpC->getArgOperand(1),
Sanjay Pateld3112a52016-01-19 19:46:10 +00001274 emitUnaryFloatFnCall(OpC->getOperand(0), Callee->getName(), B,
Davide Italianob8b71332015-11-29 20:58:04 +00001275 Callee->getAttributes()), "mul");
Davide Italiano1aeed6a2015-11-30 19:36:35 +00001276
1277 // log(exp2(y)) -> y*log(2)
1278 if (F && Name == "log" && TLI->getLibFunc(F->getName(), Func) &&
1279 TLI->has(Func) && Func == LibFunc::exp2)
1280 return B.CreateFMul(
1281 OpC->getArgOperand(0),
Sanjay Pateld3112a52016-01-19 19:46:10 +00001282 emitUnaryFloatFnCall(ConstantFP::get(CI->getType(), 2.0),
Davide Italiano1aeed6a2015-11-30 19:36:35 +00001283 Callee->getName(), B, Callee->getAttributes()),
1284 "logmul");
Davide Italianob8b71332015-11-29 20:58:04 +00001285 return Ret;
1286}
1287
Sanjay Patelc699a612014-10-16 18:48:17 +00001288Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilder<> &B) {
1289 Function *Callee = CI->getCalledFunction();
Sanjay Patelc699a612014-10-16 18:48:17 +00001290 Value *Ret = nullptr;
Sanjay Patel848309d2014-10-23 21:52:45 +00001291 if (TLI->has(LibFunc::sqrtf) && (Callee->getName() == "sqrt" ||
1292 Callee->getIntrinsicID() == Intrinsic::sqrt))
Sanjay Patelc699a612014-10-16 18:48:17 +00001293 Ret = optimizeUnaryDoubleFP(CI, B, true);
Sanjay Patel683f2972016-01-11 22:34:19 +00001294
1295 if (!CI->hasUnsafeAlgebra())
Davide Italianoa904e522015-10-29 02:58:44 +00001296 return Ret;
Sanjay Patelc699a612014-10-16 18:48:17 +00001297
Sanjay Patelc2d64612016-01-06 20:52:21 +00001298 Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0));
1299 if (!I || I->getOpcode() != Instruction::FMul || !I->hasUnsafeAlgebra())
1300 return Ret;
1301
1302 // We're looking for a repeated factor in a multiplication tree,
1303 // so we can do this fold: sqrt(x * x) -> fabs(x);
Sanjay Patel683f2972016-01-11 22:34:19 +00001304 // or this fold: sqrt((x * x) * y) -> fabs(x) * sqrt(y).
Sanjay Patelc2d64612016-01-06 20:52:21 +00001305 Value *Op0 = I->getOperand(0);
1306 Value *Op1 = I->getOperand(1);
1307 Value *RepeatOp = nullptr;
1308 Value *OtherOp = nullptr;
1309 if (Op0 == Op1) {
1310 // Simple match: the operands of the multiply are identical.
1311 RepeatOp = Op0;
1312 } else {
1313 // Look for a more complicated pattern: one of the operands is itself
1314 // a multiply, so search for a common factor in that multiply.
1315 // Note: We don't bother looking any deeper than this first level or for
1316 // variations of this pattern because instcombine's visitFMUL and/or the
1317 // reassociation pass should give us this form.
1318 Value *OtherMul0, *OtherMul1;
1319 if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) {
1320 // Pattern: sqrt((x * y) * z)
Sanjay Patel6c1ddbb2016-01-11 22:50:36 +00001321 if (OtherMul0 == OtherMul1 &&
1322 cast<Instruction>(Op0)->hasUnsafeAlgebra()) {
Sanjay Patelc2d64612016-01-06 20:52:21 +00001323 // Matched: sqrt((x * x) * z)
1324 RepeatOp = OtherMul0;
1325 OtherOp = Op1;
Sanjay Patelc699a612014-10-16 18:48:17 +00001326 }
1327 }
1328 }
Sanjay Patelc2d64612016-01-06 20:52:21 +00001329 if (!RepeatOp)
1330 return Ret;
1331
1332 // Fast math flags for any created instructions should match the sqrt
1333 // and multiply.
Sanjay Patelc2d64612016-01-06 20:52:21 +00001334 IRBuilder<>::FastMathFlagGuard Guard(B);
Sanjay Patela2528152016-01-12 18:03:37 +00001335 B.setFastMathFlags(I->getFastMathFlags());
Sanjay Patel9f67dad2016-01-11 22:35:39 +00001336
Sanjay Patelc2d64612016-01-06 20:52:21 +00001337 // If we found a repeated factor, hoist it out of the square root and
1338 // replace it with the fabs of that factor.
1339 Module *M = Callee->getParent();
1340 Type *ArgType = I->getType();
1341 Value *Fabs = Intrinsic::getDeclaration(M, Intrinsic::fabs, ArgType);
1342 Value *FabsCall = B.CreateCall(Fabs, RepeatOp, "fabs");
1343 if (OtherOp) {
1344 // If we found a non-repeated factor, we still need to get its square
1345 // root. We then multiply that by the value that was simplified out
1346 // of the square root calculation.
1347 Value *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, ArgType);
1348 Value *SqrtCall = B.CreateCall(Sqrt, OtherOp, "sqrt");
1349 return B.CreateFMul(FabsCall, SqrtCall);
1350 }
1351 return FabsCall;
Sanjay Patelc699a612014-10-16 18:48:17 +00001352}
1353
Sanjay Patelcddcd722016-01-06 19:23:35 +00001354// TODO: Generalize to handle any trig function and its inverse.
Davide Italiano51507d22015-11-04 23:36:56 +00001355Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilder<> &B) {
1356 Function *Callee = CI->getCalledFunction();
1357 Value *Ret = nullptr;
Davide Italianoa3458772015-11-05 19:18:23 +00001358 StringRef Name = Callee->getName();
1359 if (UnsafeFPShrink && Name == "tan" && hasFloatVersion(Name))
Davide Italiano51507d22015-11-04 23:36:56 +00001360 Ret = optimizeUnaryDoubleFP(CI, B, true);
Davide Italiano51507d22015-11-04 23:36:56 +00001361
Davide Italiano51507d22015-11-04 23:36:56 +00001362 Value *Op1 = CI->getArgOperand(0);
1363 auto *OpC = dyn_cast<CallInst>(Op1);
1364 if (!OpC)
1365 return Ret;
1366
Sanjay Patelcddcd722016-01-06 19:23:35 +00001367 // Both calls must allow unsafe optimizations in order to remove them.
1368 if (!CI->hasUnsafeAlgebra() || !OpC->hasUnsafeAlgebra())
1369 return Ret;
1370
Davide Italiano51507d22015-11-04 23:36:56 +00001371 // tan(atan(x)) -> x
1372 // tanf(atanf(x)) -> x
1373 // tanl(atanl(x)) -> x
1374 LibFunc::Func Func;
1375 Function *F = OpC->getCalledFunction();
Benjamin Kramerfb419e72015-11-26 09:51:17 +00001376 if (F && TLI->getLibFunc(F->getName(), Func) && TLI->has(Func) &&
Davide Italiano51507d22015-11-04 23:36:56 +00001377 ((Func == LibFunc::atan && Callee->getName() == "tan") ||
1378 (Func == LibFunc::atanf && Callee->getName() == "tanf") ||
1379 (Func == LibFunc::atanl && Callee->getName() == "tanl")))
1380 Ret = OpC->getArgOperand(0);
1381 return Ret;
1382}
1383
Sanjay Patel57747212016-01-21 23:38:43 +00001384static bool isTrigLibCall(CallInst *CI) {
Sanjay Patel57747212016-01-21 23:38:43 +00001385 // We can only hope to do anything useful if we can ignore things like errno
1386 // and floating-point exceptions.
Ahmed Bougachad765a822016-04-27 19:04:35 +00001387 // We already checked the prototype.
1388 return CI->hasFnAttr(Attribute::NoUnwind) &&
1389 CI->hasFnAttr(Attribute::ReadNone);
Sanjay Patel57747212016-01-21 23:38:43 +00001390}
1391
Chris Bienemanad070d02014-09-17 20:55:46 +00001392static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
1393 bool UseFloat, Value *&Sin, Value *&Cos,
Sanjay Patel57747212016-01-21 23:38:43 +00001394 Value *&SinCos) {
1395 Type *ArgTy = Arg->getType();
1396 Type *ResTy;
1397 StringRef Name;
1398
1399 Triple T(OrigCallee->getParent()->getTargetTriple());
1400 if (UseFloat) {
1401 Name = "__sincospif_stret";
1402
1403 assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
1404 // x86_64 can't use {float, float} since that would be returned in both
1405 // xmm0 and xmm1, which isn't what a real struct would do.
1406 ResTy = T.getArch() == Triple::x86_64
1407 ? static_cast<Type *>(VectorType::get(ArgTy, 2))
1408 : static_cast<Type *>(StructType::get(ArgTy, ArgTy, nullptr));
1409 } else {
1410 Name = "__sincospi_stret";
1411 ResTy = StructType::get(ArgTy, ArgTy, nullptr);
1412 }
1413
1414 Module *M = OrigCallee->getParent();
1415 Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),
1416 ResTy, ArgTy, nullptr);
1417
1418 if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
1419 // If the argument is an instruction, it must dominate all uses so put our
1420 // sincos call there.
1421 B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator());
1422 } else {
1423 // Otherwise (e.g. for a constant) the beginning of the function is as
1424 // good a place as any.
1425 BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
1426 B.SetInsertPoint(&EntryBB, EntryBB.begin());
1427 }
1428
1429 SinCos = B.CreateCall(Callee, Arg, "sincospi");
1430
1431 if (SinCos->getType()->isStructTy()) {
1432 Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
1433 Cos = B.CreateExtractValue(SinCos, 1, "cospi");
1434 } else {
1435 Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
1436 "sinpi");
1437 Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
1438 "cospi");
1439 }
1440}
Chris Bienemanad070d02014-09-17 20:55:46 +00001441
1442Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +00001443 // Make sure the prototype is as expected, otherwise the rest of the
1444 // function is probably invalid and likely to abort.
1445 if (!isTrigLibCall(CI))
1446 return nullptr;
1447
1448 Value *Arg = CI->getArgOperand(0);
1449 SmallVector<CallInst *, 1> SinCalls;
1450 SmallVector<CallInst *, 1> CosCalls;
1451 SmallVector<CallInst *, 1> SinCosCalls;
1452
1453 bool IsFloat = Arg->getType()->isFloatTy();
1454
1455 // Look for all compatible sinpi, cospi and sincospi calls with the same
1456 // argument. If there are enough (in some sense) we can make the
1457 // substitution.
David Majnemerabae6b52016-03-19 04:53:02 +00001458 Function *F = CI->getFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +00001459 for (User *U : Arg->users())
David Majnemerabae6b52016-03-19 04:53:02 +00001460 classifyArgUse(U, F, IsFloat, SinCalls, CosCalls, SinCosCalls);
Chris Bienemanad070d02014-09-17 20:55:46 +00001461
1462 // It's only worthwhile if both sinpi and cospi are actually used.
1463 if (SinCosCalls.empty() && (SinCalls.empty() || CosCalls.empty()))
1464 return nullptr;
1465
1466 Value *Sin, *Cos, *SinCos;
1467 insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos, SinCos);
1468
1469 replaceTrigInsts(SinCalls, Sin);
1470 replaceTrigInsts(CosCalls, Cos);
1471 replaceTrigInsts(SinCosCalls, SinCos);
1472
1473 return nullptr;
1474}
1475
David Majnemerabae6b52016-03-19 04:53:02 +00001476void LibCallSimplifier::classifyArgUse(
1477 Value *Val, Function *F, bool IsFloat,
1478 SmallVectorImpl<CallInst *> &SinCalls,
1479 SmallVectorImpl<CallInst *> &CosCalls,
1480 SmallVectorImpl<CallInst *> &SinCosCalls) {
Chris Bienemanad070d02014-09-17 20:55:46 +00001481 CallInst *CI = dyn_cast<CallInst>(Val);
1482
1483 if (!CI)
1484 return;
1485
David Majnemerabae6b52016-03-19 04:53:02 +00001486 // Don't consider calls in other functions.
1487 if (CI->getFunction() != F)
1488 return;
1489
Chris Bienemanad070d02014-09-17 20:55:46 +00001490 Function *Callee = CI->getCalledFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +00001491 LibFunc::Func Func;
Ahmed Bougachad765a822016-04-27 19:04:35 +00001492 if (!Callee || !TLI->getLibFunc(*Callee, Func) || !TLI->has(Func) ||
Benjamin Kramer89766e52015-11-28 21:43:12 +00001493 !isTrigLibCall(CI))
Chris Bienemanad070d02014-09-17 20:55:46 +00001494 return;
1495
1496 if (IsFloat) {
1497 if (Func == LibFunc::sinpif)
1498 SinCalls.push_back(CI);
1499 else if (Func == LibFunc::cospif)
1500 CosCalls.push_back(CI);
1501 else if (Func == LibFunc::sincospif_stret)
1502 SinCosCalls.push_back(CI);
1503 } else {
1504 if (Func == LibFunc::sinpi)
1505 SinCalls.push_back(CI);
1506 else if (Func == LibFunc::cospi)
1507 CosCalls.push_back(CI);
1508 else if (Func == LibFunc::sincospi_stret)
1509 SinCosCalls.push_back(CI);
1510 }
1511}
1512
1513void LibCallSimplifier::replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls,
1514 Value *Res) {
Davide Italianoc6926882015-10-27 04:17:51 +00001515 for (CallInst *C : Calls)
1516 replaceAllUsesWith(C, Res);
Chris Bienemanad070d02014-09-17 20:55:46 +00001517}
1518
Meador Inge7415f842012-11-25 20:45:27 +00001519//===----------------------------------------------------------------------===//
1520// Integer Library Call Optimizations
1521//===----------------------------------------------------------------------===//
1522
Chris Bienemanad070d02014-09-17 20:55:46 +00001523Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +00001524 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
Davide Italiano890e8502016-12-15 23:11:00 +00001525 Value *Op = CI->getArgOperand(0);
Chris Bienemanad070d02014-09-17 20:55:46 +00001526 Type *ArgType = Op->getType();
Davide Italiano890e8502016-12-15 23:11:00 +00001527 Value *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(),
1528 Intrinsic::cttz, ArgType);
Davide Italianoa1953862015-08-13 20:34:26 +00001529 Value *V = B.CreateCall(F, {Op, B.getTrue()}, "cttz");
Chris Bienemanad070d02014-09-17 20:55:46 +00001530 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
1531 V = B.CreateIntCast(V, B.getInt32Ty(), false);
Meador Ingea0b6d872012-11-26 00:24:07 +00001532
Chris Bienemanad070d02014-09-17 20:55:46 +00001533 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
1534 return B.CreateSelect(Cond, V, B.getInt32(0));
1535}
Meador Ingea0b6d872012-11-26 00:24:07 +00001536
Chris Bienemanad070d02014-09-17 20:55:46 +00001537Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +00001538 // abs(x) -> x >s -1 ? x : -x
1539 Value *Op = CI->getArgOperand(0);
1540 Value *Pos =
1541 B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()), "ispos");
1542 Value *Neg = B.CreateNeg(Op, "neg");
1543 return B.CreateSelect(Pos, Op, Neg);
1544}
Meador Inge9a59ab62012-11-26 02:31:59 +00001545
Chris Bienemanad070d02014-09-17 20:55:46 +00001546Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +00001547 // isdigit(c) -> (c-'0') <u 10
1548 Value *Op = CI->getArgOperand(0);
1549 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1550 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
1551 return B.CreateZExt(Op, CI->getType());
1552}
Meador Ingea62a39e2012-11-26 03:10:07 +00001553
Chris Bienemanad070d02014-09-17 20:55:46 +00001554Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +00001555 // isascii(c) -> c <u 128
1556 Value *Op = CI->getArgOperand(0);
1557 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
1558 return B.CreateZExt(Op, CI->getType());
1559}
1560
1561Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +00001562 // toascii(c) -> c & 0x7f
1563 return B.CreateAnd(CI->getArgOperand(0),
1564 ConstantInt::get(CI->getType(), 0x7F));
1565}
Meador Inge604937d2012-11-26 03:38:52 +00001566
Meador Inge08ca1152012-11-26 20:37:20 +00001567//===----------------------------------------------------------------------===//
1568// Formatting and IO Library Call Optimizations
1569//===----------------------------------------------------------------------===//
1570
Chris Bienemanad070d02014-09-17 20:55:46 +00001571static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
Hal Finkel66cd3f12013-11-17 02:06:35 +00001572
Chris Bienemanad070d02014-09-17 20:55:46 +00001573Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
1574 int StreamArg) {
Ahmed Bougachad765a822016-04-27 19:04:35 +00001575 Function *Callee = CI->getCalledFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +00001576 // Error reporting calls should be cold, mark them as such.
1577 // This applies even to non-builtin calls: it is only a hint and applies to
1578 // functions that the frontend might not understand as builtins.
Hal Finkel66cd3f12013-11-17 02:06:35 +00001579
Chris Bienemanad070d02014-09-17 20:55:46 +00001580 // This heuristic was suggested in:
1581 // Improving Static Branch Prediction in a Compiler
1582 // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
1583 // Proceedings of PACT'98, Oct. 1998, IEEE
Chris Bienemanad070d02014-09-17 20:55:46 +00001584 if (!CI->hasFnAttr(Attribute::Cold) &&
1585 isReportingError(Callee, CI, StreamArg)) {
1586 CI->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);
1587 }
Hal Finkel66cd3f12013-11-17 02:06:35 +00001588
Chris Bienemanad070d02014-09-17 20:55:46 +00001589 return nullptr;
1590}
1591
1592static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
Davide Italianoe84d4da2015-11-02 22:33:26 +00001593 if (!ColdErrorCalls || !Callee || !Callee->isDeclaration())
Chris Bienemanad070d02014-09-17 20:55:46 +00001594 return false;
1595
1596 if (StreamArg < 0)
1597 return true;
1598
1599 // These functions might be considered cold, but only if their stream
1600 // argument is stderr.
1601
1602 if (StreamArg >= (int)CI->getNumArgOperands())
1603 return false;
1604 LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
1605 if (!LI)
1606 return false;
1607 GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
1608 if (!GV || !GV->isDeclaration())
1609 return false;
1610 return GV->getName() == "stderr";
1611}
1612
1613Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
1614 // Check for a fixed format string.
1615 StringRef FormatStr;
1616 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
Craig Topperf40110f2014-04-25 05:29:35 +00001617 return nullptr;
Hal Finkel66cd3f12013-11-17 02:06:35 +00001618
Chris Bienemanad070d02014-09-17 20:55:46 +00001619 // Empty format string -> noop.
1620 if (FormatStr.empty()) // Tolerate printf's declared void.
1621 return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
Hal Finkel66cd3f12013-11-17 02:06:35 +00001622
Chris Bienemanad070d02014-09-17 20:55:46 +00001623 // Do not do any of the following transformations if the printf return value
1624 // is used, in general the printf return value is not compatible with either
1625 // putchar() or puts().
1626 if (!CI->use_empty())
Craig Topperf40110f2014-04-25 05:29:35 +00001627 return nullptr;
Chris Bienemanad070d02014-09-17 20:55:46 +00001628
Joerg Sonnenberger8ffe7ab2016-05-09 14:36:16 +00001629 // printf("x") -> putchar('x'), even for "%" and "%%".
1630 if (FormatStr.size() == 1 || FormatStr == "%%")
Davide Italianod4f5a052016-04-03 01:46:52 +00001631 return emitPutChar(B.getInt32(FormatStr[0]), B, TLI);
Meador Inge08ca1152012-11-26 20:37:20 +00001632
Davide Italiano6db1dcb2016-03-28 15:54:01 +00001633 // printf("%s", "a") --> putchar('a')
1634 if (FormatStr == "%s" && CI->getNumArgOperands() > 1) {
1635 StringRef ChrStr;
1636 if (!getConstantStringInfo(CI->getOperand(1), ChrStr))
1637 return nullptr;
1638 if (ChrStr.size() != 1)
1639 return nullptr;
Davide Italianod4f5a052016-04-03 01:46:52 +00001640 return emitPutChar(B.getInt32(ChrStr[0]), B, TLI);
Davide Italiano6db1dcb2016-03-28 15:54:01 +00001641 }
1642
Chris Bienemanad070d02014-09-17 20:55:46 +00001643 // printf("foo\n") --> puts("foo")
1644 if (FormatStr[FormatStr.size() - 1] == '\n' &&
1645 FormatStr.find('%') == StringRef::npos) { // No format characters.
1646 // Create a string literal with no \n on it. We expect the constant merge
1647 // pass to be run after this pass, to merge duplicate strings.
1648 FormatStr = FormatStr.drop_back();
1649 Value *GV = B.CreateGlobalString(FormatStr, "str");
Davide Italianod4f5a052016-04-03 01:46:52 +00001650 return emitPutS(GV, B, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +00001651 }
Meador Inge08ca1152012-11-26 20:37:20 +00001652
Chris Bienemanad070d02014-09-17 20:55:46 +00001653 // Optimize specific format strings.
1654 // printf("%c", chr) --> putchar(chr)
1655 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
Davide Italianod4f5a052016-04-03 01:46:52 +00001656 CI->getArgOperand(1)->getType()->isIntegerTy())
1657 return emitPutChar(CI->getArgOperand(1), B, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +00001658
1659 // printf("%s\n", str) --> puts(str)
1660 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
Davide Italianod4f5a052016-04-03 01:46:52 +00001661 CI->getArgOperand(1)->getType()->isPointerTy())
Sanjay Pateld3112a52016-01-19 19:46:10 +00001662 return emitPutS(CI->getArgOperand(1), B, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +00001663 return nullptr;
1664}
1665
1666Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) {
1667
1668 Function *Callee = CI->getCalledFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +00001669 FunctionType *FT = Callee->getFunctionType();
Chris Bienemanad070d02014-09-17 20:55:46 +00001670 if (Value *V = optimizePrintFString(CI, B)) {
1671 return V;
1672 }
1673
1674 // printf(format, ...) -> iprintf(format, ...) if no floating point
1675 // arguments.
1676 if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {
1677 Module *M = B.GetInsertBlock()->getParent()->getParent();
1678 Constant *IPrintFFn =
Meador Inge08ca1152012-11-26 20:37:20 +00001679 M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
Chris Bienemanad070d02014-09-17 20:55:46 +00001680 CallInst *New = cast<CallInst>(CI->clone());
1681 New->setCalledFunction(IPrintFFn);
1682 B.Insert(New);
1683 return New;
Meador Inge08ca1152012-11-26 20:37:20 +00001684 }
Chris Bienemanad070d02014-09-17 20:55:46 +00001685 return nullptr;
1686}
Meador Inge08ca1152012-11-26 20:37:20 +00001687
Chris Bienemanad070d02014-09-17 20:55:46 +00001688Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
1689 // Check for a fixed format string.
1690 StringRef FormatStr;
1691 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
Craig Topperf40110f2014-04-25 05:29:35 +00001692 return nullptr;
Meador Inge25c9b3b2012-11-27 05:57:54 +00001693
Chris Bienemanad070d02014-09-17 20:55:46 +00001694 // If we just have a format string (nothing else crazy) transform it.
1695 if (CI->getNumArgOperands() == 2) {
1696 // Make sure there's no % in the constant array. We could try to handle
1697 // %% -> % in the future if we cared.
1698 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1699 if (FormatStr[i] == '%')
1700 return nullptr; // we found a format specifier, bail out.
Hal Finkel66cd3f12013-11-17 02:06:35 +00001701
Chris Bienemanad070d02014-09-17 20:55:46 +00001702 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001703 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1704 ConstantInt::get(DL.getIntPtrType(CI->getContext()),
1705 FormatStr.size() + 1),
Pete Cooper67cf9a72015-11-19 05:56:52 +00001706 1); // Copy the null byte.
Chris Bienemanad070d02014-09-17 20:55:46 +00001707 return ConstantInt::get(CI->getType(), FormatStr.size());
Meador Ingef8e72502012-11-29 15:45:43 +00001708 }
Meador Ingef8e72502012-11-29 15:45:43 +00001709
Chris Bienemanad070d02014-09-17 20:55:46 +00001710 // The remaining optimizations require the format string to be "%s" or "%c"
1711 // and have an extra operand.
1712 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1713 CI->getNumArgOperands() < 3)
Craig Topperf40110f2014-04-25 05:29:35 +00001714 return nullptr;
Meador Inge75798bb2012-11-29 19:15:17 +00001715
Chris Bienemanad070d02014-09-17 20:55:46 +00001716 // Decode the second character of the format string.
1717 if (FormatStr[1] == 'c') {
1718 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1719 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
1720 return nullptr;
1721 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
Sanjay Pateld3112a52016-01-19 19:46:10 +00001722 Value *Ptr = castToCStr(CI->getArgOperand(0), B);
Chris Bienemanad070d02014-09-17 20:55:46 +00001723 B.CreateStore(V, Ptr);
David Blaikie3909da72015-03-30 20:42:56 +00001724 Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
Chris Bienemanad070d02014-09-17 20:55:46 +00001725 B.CreateStore(B.getInt8(0), Ptr);
Meador Ingedf796f82012-10-13 16:45:24 +00001726
Chris Bienemanad070d02014-09-17 20:55:46 +00001727 return ConstantInt::get(CI->getType(), 1);
Meador Ingedf796f82012-10-13 16:45:24 +00001728 }
1729
Chris Bienemanad070d02014-09-17 20:55:46 +00001730 if (FormatStr[1] == 's') {
Chris Bienemanad070d02014-09-17 20:55:46 +00001731 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1732 if (!CI->getArgOperand(2)->getType()->isPointerTy())
1733 return nullptr;
1734
Sanjay Pateld3112a52016-01-19 19:46:10 +00001735 Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +00001736 if (!Len)
1737 return nullptr;
David Majnemerabb9f552016-04-26 21:04:47 +00001738 Value *IncLen =
1739 B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
1740 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
Chris Bienemanad070d02014-09-17 20:55:46 +00001741
1742 // The sprintf result is the unincremented number of bytes in the string.
1743 return B.CreateIntCast(Len, CI->getType(), false);
1744 }
1745 return nullptr;
1746}
1747
1748Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilder<> &B) {
1749 Function *Callee = CI->getCalledFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +00001750 FunctionType *FT = Callee->getFunctionType();
Chris Bienemanad070d02014-09-17 20:55:46 +00001751 if (Value *V = optimizeSPrintFString(CI, B)) {
1752 return V;
1753 }
1754
1755 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
1756 // point arguments.
1757 if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {
1758 Module *M = B.GetInsertBlock()->getParent()->getParent();
1759 Constant *SIPrintFFn =
1760 M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
1761 CallInst *New = cast<CallInst>(CI->clone());
1762 New->setCalledFunction(SIPrintFFn);
1763 B.Insert(New);
1764 return New;
1765 }
1766 return nullptr;
1767}
1768
1769Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) {
1770 optimizeErrorReporting(CI, B, 0);
1771
1772 // All the optimizations depend on the format string.
1773 StringRef FormatStr;
1774 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1775 return nullptr;
1776
1777 // Do not do any of the following transformations if the fprintf return
1778 // value is used, in general the fprintf return value is not compatible
1779 // with fwrite(), fputc() or fputs().
1780 if (!CI->use_empty())
1781 return nullptr;
1782
1783 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1784 if (CI->getNumArgOperands() == 2) {
1785 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1786 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1787 return nullptr; // We found a format specifier.
1788
Sanjay Pateld3112a52016-01-19 19:46:10 +00001789 return emitFWrite(
Chris Bienemanad070d02014-09-17 20:55:46 +00001790 CI->getArgOperand(1),
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001791 ConstantInt::get(DL.getIntPtrType(CI->getContext()), FormatStr.size()),
Chris Bienemanad070d02014-09-17 20:55:46 +00001792 CI->getArgOperand(0), B, DL, TLI);
1793 }
1794
1795 // The remaining optimizations require the format string to be "%s" or "%c"
1796 // and have an extra operand.
1797 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1798 CI->getNumArgOperands() < 3)
1799 return nullptr;
1800
1801 // Decode the second character of the format string.
1802 if (FormatStr[1] == 'c') {
1803 // fprintf(F, "%c", chr) --> fputc(chr, F)
1804 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
1805 return nullptr;
Sanjay Pateld3112a52016-01-19 19:46:10 +00001806 return emitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +00001807 }
1808
1809 if (FormatStr[1] == 's') {
1810 // fprintf(F, "%s", str) --> fputs(str, F)
1811 if (!CI->getArgOperand(2)->getType()->isPointerTy())
1812 return nullptr;
Sanjay Pateld3112a52016-01-19 19:46:10 +00001813 return emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +00001814 }
1815 return nullptr;
1816}
1817
1818Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilder<> &B) {
1819 Function *Callee = CI->getCalledFunction();
Chris Bienemanad070d02014-09-17 20:55:46 +00001820 FunctionType *FT = Callee->getFunctionType();
Chris Bienemanad070d02014-09-17 20:55:46 +00001821 if (Value *V = optimizeFPrintFString(CI, B)) {
1822 return V;
1823 }
1824
1825 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
1826 // floating point arguments.
1827 if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {
1828 Module *M = B.GetInsertBlock()->getParent()->getParent();
1829 Constant *FIPrintFFn =
1830 M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
1831 CallInst *New = cast<CallInst>(CI->clone());
1832 New->setCalledFunction(FIPrintFFn);
1833 B.Insert(New);
1834 return New;
1835 }
1836 return nullptr;
1837}
1838
1839Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilder<> &B) {
1840 optimizeErrorReporting(CI, B, 3);
1841
Chris Bienemanad070d02014-09-17 20:55:46 +00001842 // Get the element size and count.
1843 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1844 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1845 if (!SizeC || !CountC)
1846 return nullptr;
1847 uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();
1848
1849 // If this is writing zero records, remove the call (it's a noop).
1850 if (Bytes == 0)
1851 return ConstantInt::get(CI->getType(), 0);
1852
1853 // If this is writing one byte, turn it into fputc.
1854 // This optimisation is only valid, if the return value is unused.
1855 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Sanjay Pateld3112a52016-01-19 19:46:10 +00001856 Value *Char = B.CreateLoad(castToCStr(CI->getArgOperand(0), B), "char");
1857 Value *NewCI = emitFPutC(Char, CI->getArgOperand(3), B, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +00001858 return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
1859 }
1860
1861 return nullptr;
1862}
1863
1864Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) {
1865 optimizeErrorReporting(CI, B, 1);
1866
Sjoerd Meijer7435a912016-07-07 14:31:19 +00001867 // Don't rewrite fputs to fwrite when optimising for size because fwrite
1868 // requires more arguments and thus extra MOVs are required.
1869 if (CI->getParent()->getParent()->optForSize())
1870 return nullptr;
1871
Ahmed Bougachad765a822016-04-27 19:04:35 +00001872 // We can't optimize if return value is used.
1873 if (!CI->use_empty())
Chris Bienemanad070d02014-09-17 20:55:46 +00001874 return nullptr;
1875
1876 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1877 uint64_t Len = GetStringLength(CI->getArgOperand(0));
1878 if (!Len)
1879 return nullptr;
1880
1881 // Known to have no uses (see above).
Sanjay Pateld3112a52016-01-19 19:46:10 +00001882 return emitFWrite(
Chris Bienemanad070d02014-09-17 20:55:46 +00001883 CI->getArgOperand(0),
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001884 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len - 1),
Chris Bienemanad070d02014-09-17 20:55:46 +00001885 CI->getArgOperand(1), B, DL, TLI);
1886}
1887
1888Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilder<> &B) {
Chris Bienemanad070d02014-09-17 20:55:46 +00001889 // Check for a constant string.
1890 StringRef Str;
1891 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
1892 return nullptr;
1893
1894 if (Str.empty() && CI->use_empty()) {
1895 // puts("") -> putchar('\n')
Sanjay Pateld3112a52016-01-19 19:46:10 +00001896 Value *Res = emitPutChar(B.getInt32('\n'), B, TLI);
Chris Bienemanad070d02014-09-17 20:55:46 +00001897 if (CI->use_empty() || !Res)
1898 return Res;
1899 return B.CreateIntCast(Res, CI->getType(), true);
1900 }
1901
1902 return nullptr;
1903}
1904
1905bool LibCallSimplifier::hasFloatVersion(StringRef FuncName) {
Meador Inge20255ef2013-03-12 00:08:29 +00001906 LibFunc::Func Func;
1907 SmallString<20> FloatFuncName = FuncName;
1908 FloatFuncName += 'f';
1909 if (TLI->getLibFunc(FloatFuncName, Func))
1910 return TLI->has(Func);
1911 return false;
1912}
Meador Inge7fb2f732012-10-13 16:45:32 +00001913
Ahmed Bougacha6722f5e2015-01-12 17:20:06 +00001914Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
1915 IRBuilder<> &Builder) {
1916 LibFunc::Func Func;
1917 Function *Callee = CI->getCalledFunction();
Ahmed Bougacha6722f5e2015-01-12 17:20:06 +00001918 // Check for string/memory library functions.
Ahmed Bougachad765a822016-04-27 19:04:35 +00001919 if (TLI->getLibFunc(*Callee, Func) && TLI->has(Func)) {
Ahmed Bougacha6722f5e2015-01-12 17:20:06 +00001920 // Make sure we never change the calling convention.
1921 assert((ignoreCallingConv(Func) ||
Sam Parker214f7bf2016-09-13 12:10:14 +00001922 isCallingConvCCompatible(CI)) &&
Ahmed Bougacha6722f5e2015-01-12 17:20:06 +00001923 "Optimizing string/memory libcall would change the calling convention");
1924 switch (Func) {
1925 case LibFunc::strcat:
1926 return optimizeStrCat(CI, Builder);
1927 case LibFunc::strncat:
1928 return optimizeStrNCat(CI, Builder);
1929 case LibFunc::strchr:
1930 return optimizeStrChr(CI, Builder);
1931 case LibFunc::strrchr:
1932 return optimizeStrRChr(CI, Builder);
1933 case LibFunc::strcmp:
1934 return optimizeStrCmp(CI, Builder);
1935 case LibFunc::strncmp:
1936 return optimizeStrNCmp(CI, Builder);
1937 case LibFunc::strcpy:
1938 return optimizeStrCpy(CI, Builder);
1939 case LibFunc::stpcpy:
1940 return optimizeStpCpy(CI, Builder);
1941 case LibFunc::strncpy:
1942 return optimizeStrNCpy(CI, Builder);
1943 case LibFunc::strlen:
1944 return optimizeStrLen(CI, Builder);
1945 case LibFunc::strpbrk:
1946 return optimizeStrPBrk(CI, Builder);
1947 case LibFunc::strtol:
1948 case LibFunc::strtod:
1949 case LibFunc::strtof:
1950 case LibFunc::strtoul:
1951 case LibFunc::strtoll:
1952 case LibFunc::strtold:
1953 case LibFunc::strtoull:
1954 return optimizeStrTo(CI, Builder);
1955 case LibFunc::strspn:
1956 return optimizeStrSpn(CI, Builder);
1957 case LibFunc::strcspn:
1958 return optimizeStrCSpn(CI, Builder);
1959 case LibFunc::strstr:
1960 return optimizeStrStr(CI, Builder);
Benjamin Kramer691363e2015-03-21 15:36:21 +00001961 case LibFunc::memchr:
1962 return optimizeMemChr(CI, Builder);
Ahmed Bougacha6722f5e2015-01-12 17:20:06 +00001963 case LibFunc::memcmp:
1964 return optimizeMemCmp(CI, Builder);
1965 case LibFunc::memcpy:
1966 return optimizeMemCpy(CI, Builder);
1967 case LibFunc::memmove:
1968 return optimizeMemMove(CI, Builder);
1969 case LibFunc::memset:
1970 return optimizeMemSet(CI, Builder);
1971 default:
1972 break;
1973 }
1974 }
1975 return nullptr;
1976}
1977
Chris Bienemanad070d02014-09-17 20:55:46 +00001978Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
1979 if (CI->isNoBuiltin())
1980 return nullptr;
Meador Inge4d2827c2012-11-11 05:11:20 +00001981
Meador Inge20255ef2013-03-12 00:08:29 +00001982 LibFunc::Func Func;
1983 Function *Callee = CI->getCalledFunction();
1984 StringRef FuncName = Callee->getName();
David Majnemerb70e23c2016-01-06 05:01:34 +00001985
1986 SmallVector<OperandBundleDef, 2> OpBundles;
1987 CI->getOperandBundlesAsDefs(OpBundles);
1988 IRBuilder<> Builder(CI, /*FPMathTag=*/nullptr, OpBundles);
Sam Parker214f7bf2016-09-13 12:10:14 +00001989 bool isCallingConvC = isCallingConvCCompatible(CI);
Meador Inge20255ef2013-03-12 00:08:29 +00001990
Sanjay Pateld1f4f032016-01-19 18:38:52 +00001991 // Command-line parameter overrides instruction attribute.
Sanjay Patela92fa442014-10-22 15:29:23 +00001992 if (EnableUnsafeFPShrink.getNumOccurrences() > 0)
1993 UnsafeFPShrink = EnableUnsafeFPShrink;
Sanjay Pateld1f4f032016-01-19 18:38:52 +00001994 else if (isa<FPMathOperator>(CI) && CI->hasUnsafeAlgebra())
Davide Italianoa904e522015-10-29 02:58:44 +00001995 UnsafeFPShrink = true;
Sanjay Patela92fa442014-10-22 15:29:23 +00001996
Sanjay Patel848309d2014-10-23 21:52:45 +00001997 // First, check for intrinsics.
Meador Inge20255ef2013-03-12 00:08:29 +00001998 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
Chris Bienemanad070d02014-09-17 20:55:46 +00001999 if (!isCallingConvC)
2000 return nullptr;
Meador Inge20255ef2013-03-12 00:08:29 +00002001 switch (II->getIntrinsicID()) {
2002 case Intrinsic::pow:
Chris Bienemanad070d02014-09-17 20:55:46 +00002003 return optimizePow(CI, Builder);
Meador Inge20255ef2013-03-12 00:08:29 +00002004 case Intrinsic::exp2:
Chris Bienemanad070d02014-09-17 20:55:46 +00002005 return optimizeExp2(CI, Builder);
Sanjay Patel0ca42bb2014-10-14 20:43:11 +00002006 case Intrinsic::fabs:
2007 return optimizeFabs(CI, Builder);
Davide Italianob8b71332015-11-29 20:58:04 +00002008 case Intrinsic::log:
2009 return optimizeLog(CI, Builder);
Sanjay Patelc699a612014-10-16 18:48:17 +00002010 case Intrinsic::sqrt:
2011 return optimizeSqrt(CI, Builder);
Sanjay Patel980b2802016-01-26 16:17:24 +00002012 // TODO: Use foldMallocMemset() with memset intrinsic.
Meador Inge20255ef2013-03-12 00:08:29 +00002013 default:
Chris Bienemanad070d02014-09-17 20:55:46 +00002014 return nullptr;
Meador Inge20255ef2013-03-12 00:08:29 +00002015 }
2016 }
2017
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002018 // Also try to simplify calls to fortified library functions.
2019 if (Value *SimplifiedFortifiedCI = FortifiedSimplifier.optimizeCall(CI)) {
2020 // Try to further simplify the result.
Ahmed Bougacha71d7b182015-01-14 00:55:05 +00002021 CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI);
Bruno Cardoso Lopesb491a2d2015-10-01 22:43:53 +00002022 if (SimplifiedCI && SimplifiedCI->getCalledFunction()) {
2023 // Use an IR Builder from SimplifiedCI if available instead of CI
2024 // to guarantee we reach all uses we might replace later on.
2025 IRBuilder<> TmpBuilder(SimplifiedCI);
2026 if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, TmpBuilder)) {
Ahmed Bougacha1ac93562015-01-27 21:52:16 +00002027 // If we were able to further simplify, remove the now redundant call.
2028 SimplifiedCI->replaceAllUsesWith(V);
2029 SimplifiedCI->eraseFromParent();
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002030 return V;
Ahmed Bougacha1ac93562015-01-27 21:52:16 +00002031 }
Bruno Cardoso Lopesb491a2d2015-10-01 22:43:53 +00002032 }
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002033 return SimplifiedFortifiedCI;
2034 }
2035
Meador Inge20255ef2013-03-12 00:08:29 +00002036 // Then check for known library functions.
Ahmed Bougachad765a822016-04-27 19:04:35 +00002037 if (TLI->getLibFunc(*Callee, Func) && TLI->has(Func)) {
Chris Bienemanad070d02014-09-17 20:55:46 +00002038 // We never change the calling convention.
2039 if (!ignoreCallingConv(Func) && !isCallingConvC)
2040 return nullptr;
Ahmed Bougacha6722f5e2015-01-12 17:20:06 +00002041 if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
2042 return V;
Meador Inge20255ef2013-03-12 00:08:29 +00002043 switch (Func) {
Chris Bienemanad070d02014-09-17 20:55:46 +00002044 case LibFunc::cosf:
2045 case LibFunc::cos:
2046 case LibFunc::cosl:
2047 return optimizeCos(CI, Builder);
2048 case LibFunc::sinpif:
2049 case LibFunc::sinpi:
2050 case LibFunc::cospif:
2051 case LibFunc::cospi:
2052 return optimizeSinCosPi(CI, Builder);
2053 case LibFunc::powf:
2054 case LibFunc::pow:
2055 case LibFunc::powl:
2056 return optimizePow(CI, Builder);
2057 case LibFunc::exp2l:
2058 case LibFunc::exp2:
2059 case LibFunc::exp2f:
2060 return optimizeExp2(CI, Builder);
Sanjay Patel0ca42bb2014-10-14 20:43:11 +00002061 case LibFunc::fabsf:
2062 case LibFunc::fabs:
2063 case LibFunc::fabsl:
2064 return optimizeFabs(CI, Builder);
Sanjay Patelc699a612014-10-16 18:48:17 +00002065 case LibFunc::sqrtf:
2066 case LibFunc::sqrt:
2067 case LibFunc::sqrtl:
2068 return optimizeSqrt(CI, Builder);
Chris Bienemanad070d02014-09-17 20:55:46 +00002069 case LibFunc::ffs:
2070 case LibFunc::ffsl:
2071 case LibFunc::ffsll:
2072 return optimizeFFS(CI, Builder);
2073 case LibFunc::abs:
2074 case LibFunc::labs:
2075 case LibFunc::llabs:
2076 return optimizeAbs(CI, Builder);
2077 case LibFunc::isdigit:
2078 return optimizeIsDigit(CI, Builder);
2079 case LibFunc::isascii:
2080 return optimizeIsAscii(CI, Builder);
2081 case LibFunc::toascii:
2082 return optimizeToAscii(CI, Builder);
2083 case LibFunc::printf:
2084 return optimizePrintF(CI, Builder);
2085 case LibFunc::sprintf:
2086 return optimizeSPrintF(CI, Builder);
2087 case LibFunc::fprintf:
2088 return optimizeFPrintF(CI, Builder);
2089 case LibFunc::fwrite:
2090 return optimizeFWrite(CI, Builder);
2091 case LibFunc::fputs:
2092 return optimizeFPuts(CI, Builder);
Davide Italianob8b71332015-11-29 20:58:04 +00002093 case LibFunc::log:
2094 case LibFunc::log10:
2095 case LibFunc::log1p:
2096 case LibFunc::log2:
2097 case LibFunc::logb:
2098 return optimizeLog(CI, Builder);
Chris Bienemanad070d02014-09-17 20:55:46 +00002099 case LibFunc::puts:
2100 return optimizePuts(CI, Builder);
Davide Italiano51507d22015-11-04 23:36:56 +00002101 case LibFunc::tan:
2102 case LibFunc::tanf:
2103 case LibFunc::tanl:
2104 return optimizeTan(CI, Builder);
Chris Bienemanad070d02014-09-17 20:55:46 +00002105 case LibFunc::perror:
2106 return optimizeErrorReporting(CI, Builder);
2107 case LibFunc::vfprintf:
2108 case LibFunc::fiprintf:
2109 return optimizeErrorReporting(CI, Builder, 0);
2110 case LibFunc::fputc:
2111 return optimizeErrorReporting(CI, Builder, 1);
2112 case LibFunc::ceil:
Chris Bienemanad070d02014-09-17 20:55:46 +00002113 case LibFunc::floor:
2114 case LibFunc::rint:
2115 case LibFunc::round:
2116 case LibFunc::nearbyint:
2117 case LibFunc::trunc:
2118 if (hasFloatVersion(FuncName))
2119 return optimizeUnaryDoubleFP(CI, Builder, false);
2120 return nullptr;
2121 case LibFunc::acos:
2122 case LibFunc::acosh:
2123 case LibFunc::asin:
2124 case LibFunc::asinh:
2125 case LibFunc::atan:
2126 case LibFunc::atanh:
2127 case LibFunc::cbrt:
2128 case LibFunc::cosh:
2129 case LibFunc::exp:
2130 case LibFunc::exp10:
2131 case LibFunc::expm1:
Chris Bienemanad070d02014-09-17 20:55:46 +00002132 case LibFunc::sin:
2133 case LibFunc::sinh:
Chris Bienemanad070d02014-09-17 20:55:46 +00002134 case LibFunc::tanh:
2135 if (UnsafeFPShrink && hasFloatVersion(FuncName))
2136 return optimizeUnaryDoubleFP(CI, Builder, true);
2137 return nullptr;
Matthias Braun892c9232014-12-03 21:46:29 +00002138 case LibFunc::copysign:
Chris Bienemanad070d02014-09-17 20:55:46 +00002139 if (hasFloatVersion(FuncName))
2140 return optimizeBinaryDoubleFP(CI, Builder);
2141 return nullptr;
Sanjay Patel57fd1dc2015-08-16 20:18:19 +00002142 case LibFunc::fminf:
2143 case LibFunc::fmin:
2144 case LibFunc::fminl:
2145 case LibFunc::fmaxf:
2146 case LibFunc::fmax:
2147 case LibFunc::fmaxl:
2148 return optimizeFMinFMax(CI, Builder);
Chris Bienemanad070d02014-09-17 20:55:46 +00002149 default:
2150 return nullptr;
2151 }
Meador Inge20255ef2013-03-12 00:08:29 +00002152 }
Craig Topperf40110f2014-04-25 05:29:35 +00002153 return nullptr;
Meador Ingedf796f82012-10-13 16:45:24 +00002154}
2155
Chandler Carruth92803822015-01-21 02:11:59 +00002156LibCallSimplifier::LibCallSimplifier(
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002157 const DataLayout &DL, const TargetLibraryInfo *TLI,
Chandler Carruth92803822015-01-21 02:11:59 +00002158 function_ref<void(Instruction *, Value *)> Replacer)
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002159 : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), UnsafeFPShrink(false),
Chandler Carruth92803822015-01-21 02:11:59 +00002160 Replacer(Replacer) {}
2161
2162void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
2163 // Indirect through the replacer used in this instance.
2164 Replacer(I, With);
Meador Ingedf796f82012-10-13 16:45:24 +00002165}
2166
Meador Ingedfb08a22013-06-20 19:48:07 +00002167// TODO:
2168// Additional cases that we need to add to this file:
2169//
2170// cbrt:
2171// * cbrt(expN(X)) -> expN(x/3)
2172// * cbrt(sqrt(x)) -> pow(x,1/6)
David Majnemer3354fe42015-08-26 18:30:16 +00002173// * cbrt(cbrt(x)) -> pow(x,1/9)
Meador Ingedfb08a22013-06-20 19:48:07 +00002174//
2175// exp, expf, expl:
2176// * exp(log(x)) -> x
2177//
2178// log, logf, logl:
2179// * log(exp(x)) -> x
Meador Ingedfb08a22013-06-20 19:48:07 +00002180// * log(exp(y)) -> y*log(e)
Meador Ingedfb08a22013-06-20 19:48:07 +00002181// * log(exp10(y)) -> y*log(10)
2182// * log(sqrt(x)) -> 0.5*log(x)
Meador Ingedfb08a22013-06-20 19:48:07 +00002183//
2184// lround, lroundf, lroundl:
2185// * lround(cnst) -> cnst'
2186//
2187// pow, powf, powl:
Meador Ingedfb08a22013-06-20 19:48:07 +00002188// * pow(sqrt(x),y) -> pow(x,y*0.5)
2189// * pow(pow(x,y),z)-> pow(x,y*z)
2190//
2191// round, roundf, roundl:
2192// * round(cnst) -> cnst'
2193//
2194// signbit:
2195// * signbit(cnst) -> cnst'
2196// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2197//
2198// sqrt, sqrtf, sqrtl:
2199// * sqrt(expN(x)) -> expN(x*0.5)
2200// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2201// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2202//
Meador Ingedfb08a22013-06-20 19:48:07 +00002203// trunc, truncf, truncl:
2204// * trunc(cnst) -> cnst'
2205//
2206//
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002207
2208//===----------------------------------------------------------------------===//
2209// Fortified Library Call Optimizations
2210//===----------------------------------------------------------------------===//
2211
2212bool FortifiedLibCallSimplifier::isFortifiedCallFoldable(CallInst *CI,
2213 unsigned ObjSizeOp,
2214 unsigned SizeOp,
2215 bool isString) {
2216 if (CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(SizeOp))
2217 return true;
2218 if (ConstantInt *ObjSizeCI =
2219 dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
2220 if (ObjSizeCI->isAllOnesValue())
2221 return true;
2222 // If the object size wasn't -1 (unknown), bail out if we were asked to.
2223 if (OnlyLowerUnknownSize)
2224 return false;
2225 if (isString) {
2226 uint64_t Len = GetStringLength(CI->getArgOperand(SizeOp));
2227 // If the length is 0 we don't know how long it is and so we can't
2228 // remove the check.
2229 if (Len == 0)
2230 return false;
2231 return ObjSizeCI->getZExtValue() >= Len;
2232 }
2233 if (ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getArgOperand(SizeOp)))
2234 return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
2235 }
2236 return false;
2237}
2238
Sanjay Pateld707db92015-12-31 16:10:49 +00002239Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI,
2240 IRBuilder<> &B) {
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002241 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2242 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
Pete Cooper67cf9a72015-11-19 05:56:52 +00002243 CI->getArgOperand(2), 1);
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002244 return CI->getArgOperand(0);
2245 }
2246 return nullptr;
2247}
2248
Sanjay Pateld707db92015-12-31 16:10:49 +00002249Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
2250 IRBuilder<> &B) {
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002251 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2252 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
Pete Cooper67cf9a72015-11-19 05:56:52 +00002253 CI->getArgOperand(2), 1);
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002254 return CI->getArgOperand(0);
2255 }
2256 return nullptr;
2257}
2258
Sanjay Pateld707db92015-12-31 16:10:49 +00002259Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
2260 IRBuilder<> &B) {
Sanjay Patel980b2802016-01-26 16:17:24 +00002261 // TODO: Try foldMallocMemset() here.
2262
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002263 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2264 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
2265 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
2266 return CI->getArgOperand(0);
2267 }
2268 return nullptr;
2269}
2270
Ahmed Bougacha1ac93562015-01-27 21:52:16 +00002271Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
2272 IRBuilder<> &B,
2273 LibFunc::Func Func) {
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002274 Function *Callee = CI->getCalledFunction();
2275 StringRef Name = Callee->getName();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002276 const DataLayout &DL = CI->getModule()->getDataLayout();
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002277 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
2278 *ObjSize = CI->getArgOperand(2);
2279
2280 // __stpcpy_chk(x,x,...) -> x+strlen(x)
Ahmed Bougacha1ac93562015-01-27 21:52:16 +00002281 if (Func == LibFunc::stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
Sanjay Pateld3112a52016-01-19 19:46:10 +00002282 Value *StrLen = emitStrLen(Src, B, DL, TLI);
David Blaikieaa41cd52015-04-03 21:33:42 +00002283 return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002284 }
2285
2286 // If a) we don't have any length information, or b) we know this will
2287 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
2288 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
2289 // TODO: It might be nice to get a maximum length out of the possible
2290 // string lengths for varying.
David Blaikie65fab6d2015-04-03 21:32:06 +00002291 if (isFortifiedCallFoldable(CI, 2, 1, true))
Sanjay Pateld3112a52016-01-19 19:46:10 +00002292 return emitStrCpy(Dst, Src, B, TLI, Name.substr(2, 6));
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002293
David Blaikie65fab6d2015-04-03 21:32:06 +00002294 if (OnlyLowerUnknownSize)
2295 return nullptr;
2296
2297 // Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk.
2298 uint64_t Len = GetStringLength(Src);
2299 if (Len == 0)
2300 return nullptr;
2301
2302 Type *SizeTTy = DL.getIntPtrType(CI->getContext());
2303 Value *LenV = ConstantInt::get(SizeTTy, Len);
Sanjay Pateld3112a52016-01-19 19:46:10 +00002304 Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
David Blaikie65fab6d2015-04-03 21:32:06 +00002305 // If the function was an __stpcpy_chk, and we were able to fold it into
2306 // a __memcpy_chk, we still need to return the correct end pointer.
2307 if (Ret && Func == LibFunc::stpcpy_chk)
2308 return B.CreateGEP(B.getInt8Ty(), Dst, ConstantInt::get(SizeTTy, Len - 1));
2309 return Ret;
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002310}
2311
Ahmed Bougacha1ac93562015-01-27 21:52:16 +00002312Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
2313 IRBuilder<> &B,
2314 LibFunc::Func Func) {
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002315 Function *Callee = CI->getCalledFunction();
2316 StringRef Name = Callee->getName();
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002317 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
Sanjay Pateld3112a52016-01-19 19:46:10 +00002318 Value *Ret = emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002319 CI->getArgOperand(2), B, TLI, Name.substr(2, 7));
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002320 return Ret;
2321 }
2322 return nullptr;
2323}
2324
2325Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI) {
Ahmed Bougacha408d0102015-04-01 00:45:09 +00002326 // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here.
2327 // Some clang users checked for _chk libcall availability using:
2328 // __has_builtin(__builtin___memcpy_chk)
2329 // When compiling with -fno-builtin, this is always true.
2330 // When passing -ffreestanding/-mkernel, which both imply -fno-builtin, we
2331 // end up with fortified libcalls, which isn't acceptable in a freestanding
2332 // environment which only provides their non-fortified counterparts.
2333 //
2334 // Until we change clang and/or teach external users to check for availability
2335 // differently, disregard the "nobuiltin" attribute and TLI::has.
2336 //
2337 // PR23093.
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002338
2339 LibFunc::Func Func;
2340 Function *Callee = CI->getCalledFunction();
David Majnemerb70e23c2016-01-06 05:01:34 +00002341
2342 SmallVector<OperandBundleDef, 2> OpBundles;
2343 CI->getOperandBundlesAsDefs(OpBundles);
2344 IRBuilder<> Builder(CI, /*FPMathTag=*/nullptr, OpBundles);
Sam Parker214f7bf2016-09-13 12:10:14 +00002345 bool isCallingConvC = isCallingConvCCompatible(CI);
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002346
Ahmed Bougachad765a822016-04-27 19:04:35 +00002347 // First, check that this is a known library functions and that the prototype
2348 // is correct.
2349 if (!TLI->getLibFunc(*Callee, Func))
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002350 return nullptr;
2351
2352 // We never change the calling convention.
2353 if (!ignoreCallingConv(Func) && !isCallingConvC)
2354 return nullptr;
2355
2356 switch (Func) {
2357 case LibFunc::memcpy_chk:
2358 return optimizeMemCpyChk(CI, Builder);
2359 case LibFunc::memmove_chk:
2360 return optimizeMemMoveChk(CI, Builder);
2361 case LibFunc::memset_chk:
2362 return optimizeMemSetChk(CI, Builder);
2363 case LibFunc::stpcpy_chk:
2364 case LibFunc::strcpy_chk:
Ahmed Bougacha1ac93562015-01-27 21:52:16 +00002365 return optimizeStrpCpyChk(CI, Builder, Func);
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002366 case LibFunc::stpncpy_chk:
2367 case LibFunc::strncpy_chk:
Ahmed Bougacha1ac93562015-01-27 21:52:16 +00002368 return optimizeStrpNCpyChk(CI, Builder, Func);
Ahmed Bougachae03bef72015-01-12 17:22:43 +00002369 default:
2370 break;
2371 }
2372 return nullptr;
2373}
2374
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002375FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
2376 const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
2377 : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}