blob: 8ff87eee24ee364b61cb76bef1a270cadbae155f [file] [log] [blame]
Meador Inge5e890452012-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 Inge5e890452012-10-13 16:45:24 +000018#include "llvm/ADT/StringMap.h"
19#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/IRBuilder.h"
23#include "llvm/IR/Intrinsics.h"
24#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/Module.h"
Nadav Rotemf26b4f02013-02-27 05:53:43 +000026#include "llvm/Support/Allocator.h"
Meador Inge5e890452012-10-13 16:45:24 +000027#include "llvm/Target/TargetLibraryInfo.h"
28#include "llvm/Transforms/Utils/BuildLibCalls.h"
29
30using namespace llvm;
31
32/// This class is the abstract base class for the set of optimizations that
33/// corresponds to one library call.
34namespace {
35class LibCallOptimization {
36protected:
37 Function *Caller;
38 const DataLayout *TD;
39 const TargetLibraryInfo *TLI;
Meador Ingeb69bf6b2012-11-11 03:51:43 +000040 const LibCallSimplifier *LCS;
Meador Inge5e890452012-10-13 16:45:24 +000041 LLVMContext* Context;
42public:
43 LibCallOptimization() { }
44 virtual ~LibCallOptimization() {}
45
46 /// callOptimizer - This pure virtual method is implemented by base classes to
47 /// do various optimizations. If this returns null then no transformation was
48 /// performed. If it returns CI, then it transformed the call and CI is to be
49 /// deleted. If it returns something else, replace CI with the new value and
50 /// delete CI.
51 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
52 =0;
53
Chad Rosier33daeab2013-02-08 18:00:14 +000054 /// ignoreCallingConv - Returns false if this transformation could possibly
55 /// change the calling convention.
56 virtual bool ignoreCallingConv() { return false; }
57
Meador Inge5e890452012-10-13 16:45:24 +000058 Value *optimizeCall(CallInst *CI, const DataLayout *TD,
Meador Ingeb69bf6b2012-11-11 03:51:43 +000059 const TargetLibraryInfo *TLI,
60 const LibCallSimplifier *LCS, IRBuilder<> &B) {
Meador Inge5e890452012-10-13 16:45:24 +000061 Caller = CI->getParent()->getParent();
62 this->TD = TD;
63 this->TLI = TLI;
Meador Ingeb69bf6b2012-11-11 03:51:43 +000064 this->LCS = LCS;
Meador Inge5e890452012-10-13 16:45:24 +000065 if (CI->getCalledFunction())
66 Context = &CI->getCalledFunction()->getContext();
67
68 // We never change the calling convention.
Chad Rosier33daeab2013-02-08 18:00:14 +000069 if (!ignoreCallingConv() && CI->getCallingConv() != llvm::CallingConv::C)
Meador Inge5e890452012-10-13 16:45:24 +000070 return NULL;
71
72 return callOptimizer(CI->getCalledFunction(), CI, B);
73 }
74};
75
76//===----------------------------------------------------------------------===//
Meador Inge57cfd712012-10-31 03:33:06 +000077// Helper Functions
78//===----------------------------------------------------------------------===//
79
80/// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
81/// value is equal or not-equal to zero.
82static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
83 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
84 UI != E; ++UI) {
85 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
86 if (IC->isEquality())
87 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
88 if (C->isNullValue())
89 continue;
90 // Unknown instruction.
91 return false;
92 }
93 return true;
94}
95
Meador Inge6e1591a2012-11-11 03:51:48 +000096/// isOnlyUsedInEqualityComparison - Return true if it is only used in equality
97/// comparisons with With.
98static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
99 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
100 UI != E; ++UI) {
101 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
102 if (IC->isEquality() && IC->getOperand(1) == With)
103 continue;
104 // Unknown instruction.
105 return false;
106 }
107 return true;
108}
109
Meador Inged7aa3232012-11-26 20:37:20 +0000110static bool callHasFloatingPointArgument(const CallInst *CI) {
111 for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
112 it != e; ++it) {
113 if ((*it)->getType()->isFloatingPointTy())
114 return true;
115 }
116 return false;
117}
118
Meador Inge57cfd712012-10-31 03:33:06 +0000119//===----------------------------------------------------------------------===//
Meador Inge5e890452012-10-13 16:45:24 +0000120// Fortified Library Call Optimizations
121//===----------------------------------------------------------------------===//
122
123struct FortifiedLibCallOptimization : public LibCallOptimization {
124protected:
125 virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
126 bool isString) const = 0;
127};
128
129struct InstFortifiedLibCallOptimization : public FortifiedLibCallOptimization {
130 CallInst *CI;
131
132 bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const {
133 if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
134 return true;
135 if (ConstantInt *SizeCI =
136 dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
137 if (SizeCI->isAllOnesValue())
138 return true;
139 if (isString) {
140 uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
141 // If the length is 0 we don't know how long it is and so we can't
142 // remove the check.
143 if (Len == 0) return false;
144 return SizeCI->getZExtValue() >= Len;
145 }
146 if (ConstantInt *Arg = dyn_cast<ConstantInt>(
147 CI->getArgOperand(SizeArgOp)))
148 return SizeCI->getZExtValue() >= Arg->getZExtValue();
149 }
150 return false;
151 }
152};
153
154struct MemCpyChkOpt : public InstFortifiedLibCallOptimization {
155 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
156 this->CI = CI;
157 FunctionType *FT = Callee->getFunctionType();
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000158 LLVMContext &Context = CI->getParent()->getContext();
Meador Inge5e890452012-10-13 16:45:24 +0000159
160 // Check if this has the right signature.
161 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
162 !FT->getParamType(0)->isPointerTy() ||
163 !FT->getParamType(1)->isPointerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000164 FT->getParamType(2) != TD->getIntPtrType(Context) ||
165 FT->getParamType(3) != TD->getIntPtrType(Context))
Meador Inge5e890452012-10-13 16:45:24 +0000166 return 0;
167
168 if (isFoldable(3, 2, false)) {
169 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
170 CI->getArgOperand(2), 1);
171 return CI->getArgOperand(0);
172 }
173 return 0;
174 }
175};
176
177struct MemMoveChkOpt : public InstFortifiedLibCallOptimization {
178 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
179 this->CI = CI;
180 FunctionType *FT = Callee->getFunctionType();
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000181 LLVMContext &Context = CI->getParent()->getContext();
Meador Inge5e890452012-10-13 16:45:24 +0000182
183 // Check if this has the right signature.
184 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
185 !FT->getParamType(0)->isPointerTy() ||
186 !FT->getParamType(1)->isPointerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000187 FT->getParamType(2) != TD->getIntPtrType(Context) ||
188 FT->getParamType(3) != TD->getIntPtrType(Context))
Meador Inge5e890452012-10-13 16:45:24 +0000189 return 0;
190
191 if (isFoldable(3, 2, false)) {
192 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
193 CI->getArgOperand(2), 1);
194 return CI->getArgOperand(0);
195 }
196 return 0;
197 }
198};
199
200struct MemSetChkOpt : public InstFortifiedLibCallOptimization {
201 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
202 this->CI = CI;
203 FunctionType *FT = Callee->getFunctionType();
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000204 LLVMContext &Context = CI->getParent()->getContext();
Meador Inge5e890452012-10-13 16:45:24 +0000205
206 // Check if this has the right signature.
207 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
208 !FT->getParamType(0)->isPointerTy() ||
209 !FT->getParamType(1)->isIntegerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000210 FT->getParamType(2) != TD->getIntPtrType(Context) ||
211 FT->getParamType(3) != TD->getIntPtrType(Context))
Meador Inge5e890452012-10-13 16:45:24 +0000212 return 0;
213
214 if (isFoldable(3, 2, false)) {
215 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
216 false);
217 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
218 return CI->getArgOperand(0);
219 }
220 return 0;
221 }
222};
223
224struct StrCpyChkOpt : public InstFortifiedLibCallOptimization {
225 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
226 this->CI = CI;
227 StringRef Name = Callee->getName();
228 FunctionType *FT = Callee->getFunctionType();
229 LLVMContext &Context = CI->getParent()->getContext();
230
231 // Check if this has the right signature.
232 if (FT->getNumParams() != 3 ||
233 FT->getReturnType() != FT->getParamType(0) ||
234 FT->getParamType(0) != FT->getParamType(1) ||
235 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000236 FT->getParamType(2) != TD->getIntPtrType(Context))
Meador Inge5e890452012-10-13 16:45:24 +0000237 return 0;
238
Meador Inge0c41d572012-10-18 18:12:40 +0000239 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
240 if (Dst == Src) // __strcpy_chk(x,x) -> x
241 return Src;
242
Meador Inge5e890452012-10-13 16:45:24 +0000243 // If a) we don't have any length information, or b) we know this will
Meador Ingefa9d1372012-10-31 00:20:51 +0000244 // fit then just lower to a plain strcpy. Otherwise we'll keep our
245 // strcpy_chk call which may fail at runtime if the size is too long.
Meador Inge5e890452012-10-13 16:45:24 +0000246 // TODO: It might be nice to get a maximum length out of the possible
247 // string lengths for varying.
248 if (isFoldable(2, 1, true)) {
Meador Inge0c41d572012-10-18 18:12:40 +0000249 Value *Ret = EmitStrCpy(Dst, Src, B, TD, TLI, Name.substr(2, 6));
250 return Ret;
251 } else {
252 // Maybe we can stil fold __strcpy_chk to __memcpy_chk.
253 uint64_t Len = GetStringLength(Src);
254 if (Len == 0) return 0;
255
256 // This optimization require DataLayout.
257 if (!TD) return 0;
258
259 Value *Ret =
260 EmitMemCpyChk(Dst, Src,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000261 ConstantInt::get(TD->getIntPtrType(Context), Len),
262 CI->getArgOperand(2), B, TD, TLI);
Meador Inge5e890452012-10-13 16:45:24 +0000263 return Ret;
264 }
265 return 0;
266 }
267};
268
Meador Ingefa9d1372012-10-31 00:20:51 +0000269struct StpCpyChkOpt : public InstFortifiedLibCallOptimization {
270 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
271 this->CI = CI;
272 StringRef Name = Callee->getName();
273 FunctionType *FT = Callee->getFunctionType();
274 LLVMContext &Context = CI->getParent()->getContext();
275
276 // Check if this has the right signature.
277 if (FT->getNumParams() != 3 ||
278 FT->getReturnType() != FT->getParamType(0) ||
279 FT->getParamType(0) != FT->getParamType(1) ||
280 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
281 FT->getParamType(2) != TD->getIntPtrType(FT->getParamType(0)))
282 return 0;
283
284 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
285 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
286 Value *StrLen = EmitStrLen(Src, B, TD, TLI);
287 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
288 }
289
290 // If a) we don't have any length information, or b) we know this will
291 // fit then just lower to a plain stpcpy. Otherwise we'll keep our
292 // stpcpy_chk call which may fail at runtime if the size is too long.
293 // TODO: It might be nice to get a maximum length out of the possible
294 // string lengths for varying.
295 if (isFoldable(2, 1, true)) {
296 Value *Ret = EmitStrCpy(Dst, Src, B, TD, TLI, Name.substr(2, 6));
297 return Ret;
298 } else {
299 // Maybe we can stil fold __stpcpy_chk to __memcpy_chk.
300 uint64_t Len = GetStringLength(Src);
301 if (Len == 0) return 0;
302
303 // This optimization require DataLayout.
304 if (!TD) return 0;
305
306 Type *PT = FT->getParamType(0);
307 Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
308 Value *DstEnd = B.CreateGEP(Dst,
309 ConstantInt::get(TD->getIntPtrType(PT),
310 Len - 1));
311 if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, TD, TLI))
312 return 0;
313 return DstEnd;
314 }
315 return 0;
316 }
317};
318
Meador Inge5e890452012-10-13 16:45:24 +0000319struct StrNCpyChkOpt : public InstFortifiedLibCallOptimization {
320 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
321 this->CI = CI;
322 StringRef Name = Callee->getName();
323 FunctionType *FT = Callee->getFunctionType();
324 LLVMContext &Context = CI->getParent()->getContext();
325
326 // Check if this has the right signature.
327 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
328 FT->getParamType(0) != FT->getParamType(1) ||
329 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
330 !FT->getParamType(2)->isIntegerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000331 FT->getParamType(3) != TD->getIntPtrType(Context))
Meador Inge5e890452012-10-13 16:45:24 +0000332 return 0;
333
334 if (isFoldable(3, 2, false)) {
335 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
336 CI->getArgOperand(2), B, TD, TLI,
337 Name.substr(2, 7));
338 return Ret;
339 }
340 return 0;
341 }
342};
343
Meador Inge73d8a582012-10-13 16:45:32 +0000344//===----------------------------------------------------------------------===//
345// String and Memory Library Call Optimizations
346//===----------------------------------------------------------------------===//
347
348struct StrCatOpt : public LibCallOptimization {
349 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
350 // Verify the "strcat" function prototype.
351 FunctionType *FT = Callee->getFunctionType();
352 if (FT->getNumParams() != 2 ||
353 FT->getReturnType() != B.getInt8PtrTy() ||
354 FT->getParamType(0) != FT->getReturnType() ||
355 FT->getParamType(1) != FT->getReturnType())
356 return 0;
357
358 // Extract some information from the instruction
359 Value *Dst = CI->getArgOperand(0);
360 Value *Src = CI->getArgOperand(1);
361
362 // See if we can get the length of the input string.
363 uint64_t Len = GetStringLength(Src);
364 if (Len == 0) return 0;
365 --Len; // Unbias length.
366
367 // Handle the simple, do-nothing case: strcat(x, "") -> x
368 if (Len == 0)
369 return Dst;
370
371 // These optimizations require DataLayout.
372 if (!TD) return 0;
373
374 return emitStrLenMemCpy(Src, Dst, Len, B);
375 }
376
377 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
378 IRBuilder<> &B) {
379 // We need to find the end of the destination string. That's where the
380 // memory is to be moved to. We just generate a call to strlen.
381 Value *DstLen = EmitStrLen(Dst, B, TD, TLI);
382 if (!DstLen)
383 return 0;
384
385 // Now that we have the destination's length, we must index into the
386 // destination's pointer to get the actual memcpy destination (end of
387 // the string .. we're concatenating).
388 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
389
390 // We have enough information to now generate the memcpy call to do the
391 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
392 B.CreateMemCpy(CpyDst, Src,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000393 ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1);
Meador Inge73d8a582012-10-13 16:45:32 +0000394 return Dst;
395 }
396};
397
398struct StrNCatOpt : public StrCatOpt {
399 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
400 // Verify the "strncat" function prototype.
401 FunctionType *FT = Callee->getFunctionType();
402 if (FT->getNumParams() != 3 ||
403 FT->getReturnType() != B.getInt8PtrTy() ||
404 FT->getParamType(0) != FT->getReturnType() ||
405 FT->getParamType(1) != FT->getReturnType() ||
406 !FT->getParamType(2)->isIntegerTy())
407 return 0;
408
409 // Extract some information from the instruction
410 Value *Dst = CI->getArgOperand(0);
411 Value *Src = CI->getArgOperand(1);
412 uint64_t Len;
413
414 // We don't do anything if length is not constant
415 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
416 Len = LengthArg->getZExtValue();
417 else
418 return 0;
419
420 // See if we can get the length of the input string.
421 uint64_t SrcLen = GetStringLength(Src);
422 if (SrcLen == 0) return 0;
423 --SrcLen; // Unbias length.
424
425 // Handle the simple, do-nothing cases:
426 // strncat(x, "", c) -> x
427 // strncat(x, c, 0) -> x
428 if (SrcLen == 0 || Len == 0) return Dst;
429
430 // These optimizations require DataLayout.
431 if (!TD) return 0;
432
433 // We don't optimize this case
434 if (Len < SrcLen) return 0;
435
436 // strncat(x, s, c) -> strcat(x, s)
437 // s is constant so the strcat can be optimized further
438 return emitStrLenMemCpy(Src, Dst, SrcLen, B);
439 }
440};
441
Meador Inge186f8d92012-10-13 16:45:37 +0000442struct StrChrOpt : public LibCallOptimization {
443 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
444 // Verify the "strchr" function prototype.
445 FunctionType *FT = Callee->getFunctionType();
446 if (FT->getNumParams() != 2 ||
447 FT->getReturnType() != B.getInt8PtrTy() ||
448 FT->getParamType(0) != FT->getReturnType() ||
449 !FT->getParamType(1)->isIntegerTy(32))
450 return 0;
451
452 Value *SrcStr = CI->getArgOperand(0);
453
454 // If the second operand is non-constant, see if we can compute the length
455 // of the input string and turn this into memchr.
456 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
457 if (CharC == 0) {
458 // These optimizations require DataLayout.
459 if (!TD) return 0;
460
461 uint64_t Len = GetStringLength(SrcStr);
462 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
463 return 0;
464
465 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000466 ConstantInt::get(TD->getIntPtrType(*Context), Len),
Meador Inge186f8d92012-10-13 16:45:37 +0000467 B, TD, TLI);
468 }
469
470 // Otherwise, the character is a constant, see if the first argument is
471 // a string literal. If so, we can constant fold.
472 StringRef Str;
473 if (!getConstantStringInfo(SrcStr, Str))
474 return 0;
475
476 // Compute the offset, make sure to handle the case when we're searching for
477 // zero (a weird way to spell strlen).
478 size_t I = CharC->getSExtValue() == 0 ?
479 Str.size() : Str.find(CharC->getSExtValue());
480 if (I == StringRef::npos) // Didn't find the char. strchr returns null.
481 return Constant::getNullValue(CI->getType());
482
483 // strchr(s+n,c) -> gep(s+n+i,c)
484 return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
485 }
486};
487
488struct StrRChrOpt : public LibCallOptimization {
489 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
490 // Verify the "strrchr" function prototype.
491 FunctionType *FT = Callee->getFunctionType();
492 if (FT->getNumParams() != 2 ||
493 FT->getReturnType() != B.getInt8PtrTy() ||
494 FT->getParamType(0) != FT->getReturnType() ||
495 !FT->getParamType(1)->isIntegerTy(32))
496 return 0;
497
498 Value *SrcStr = CI->getArgOperand(0);
499 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
500
501 // Cannot fold anything if we're not looking for a constant.
502 if (!CharC)
503 return 0;
504
505 StringRef Str;
506 if (!getConstantStringInfo(SrcStr, Str)) {
507 // strrchr(s, 0) -> strchr(s, 0)
508 if (TD && CharC->isZero())
509 return EmitStrChr(SrcStr, '\0', B, TD, TLI);
510 return 0;
511 }
512
513 // Compute the offset.
514 size_t I = CharC->getSExtValue() == 0 ?
515 Str.size() : Str.rfind(CharC->getSExtValue());
516 if (I == StringRef::npos) // Didn't find the char. Return null.
517 return Constant::getNullValue(CI->getType());
518
519 // strrchr(s+n,c) -> gep(s+n+i,c)
520 return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
521 }
522};
523
Meador Ingea239c2e2012-10-15 03:47:37 +0000524struct StrCmpOpt : public LibCallOptimization {
525 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
526 // Verify the "strcmp" function prototype.
527 FunctionType *FT = Callee->getFunctionType();
528 if (FT->getNumParams() != 2 ||
529 !FT->getReturnType()->isIntegerTy(32) ||
530 FT->getParamType(0) != FT->getParamType(1) ||
531 FT->getParamType(0) != B.getInt8PtrTy())
532 return 0;
533
534 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
535 if (Str1P == Str2P) // strcmp(x,x) -> 0
536 return ConstantInt::get(CI->getType(), 0);
537
538 StringRef Str1, Str2;
539 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
540 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
541
542 // strcmp(x, y) -> cnst (if both x and y are constant strings)
543 if (HasStr1 && HasStr2)
544 return ConstantInt::get(CI->getType(), Str1.compare(Str2));
545
546 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
547 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
548 CI->getType()));
549
550 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
551 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
552
553 // strcmp(P, "x") -> memcmp(P, "x", 2)
554 uint64_t Len1 = GetStringLength(Str1P);
555 uint64_t Len2 = GetStringLength(Str2P);
556 if (Len1 && Len2) {
557 // These optimizations require DataLayout.
558 if (!TD) return 0;
559
560 return EmitMemCmp(Str1P, Str2P,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000561 ConstantInt::get(TD->getIntPtrType(*Context),
Meador Ingea239c2e2012-10-15 03:47:37 +0000562 std::min(Len1, Len2)), B, TD, TLI);
563 }
564
565 return 0;
566 }
567};
568
569struct StrNCmpOpt : public LibCallOptimization {
570 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
571 // Verify the "strncmp" function prototype.
572 FunctionType *FT = Callee->getFunctionType();
573 if (FT->getNumParams() != 3 ||
574 !FT->getReturnType()->isIntegerTy(32) ||
575 FT->getParamType(0) != FT->getParamType(1) ||
576 FT->getParamType(0) != B.getInt8PtrTy() ||
577 !FT->getParamType(2)->isIntegerTy())
578 return 0;
579
580 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
581 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
582 return ConstantInt::get(CI->getType(), 0);
583
584 // Get the length argument if it is constant.
585 uint64_t Length;
586 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
587 Length = LengthArg->getZExtValue();
588 else
589 return 0;
590
591 if (Length == 0) // strncmp(x,y,0) -> 0
592 return ConstantInt::get(CI->getType(), 0);
593
594 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
595 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD, TLI);
596
597 StringRef Str1, Str2;
598 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
599 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
600
601 // strncmp(x, y) -> cnst (if both x and y are constant strings)
602 if (HasStr1 && HasStr2) {
603 StringRef SubStr1 = Str1.substr(0, Length);
604 StringRef SubStr2 = Str2.substr(0, Length);
605 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
606 }
607
608 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
609 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
610 CI->getType()));
611
612 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
613 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
614
615 return 0;
616 }
617};
618
Meador Inge0c41d572012-10-18 18:12:40 +0000619struct StrCpyOpt : public LibCallOptimization {
620 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
621 // Verify the "strcpy" function prototype.
622 FunctionType *FT = Callee->getFunctionType();
623 if (FT->getNumParams() != 2 ||
624 FT->getReturnType() != FT->getParamType(0) ||
625 FT->getParamType(0) != FT->getParamType(1) ||
626 FT->getParamType(0) != B.getInt8PtrTy())
627 return 0;
628
629 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
630 if (Dst == Src) // strcpy(x,x) -> x
631 return Src;
632
633 // These optimizations require DataLayout.
634 if (!TD) return 0;
635
636 // See if we can get the length of the input string.
637 uint64_t Len = GetStringLength(Src);
638 if (Len == 0) return 0;
639
640 // We have enough information to now generate the memcpy call to do the
641 // copy for us. Make a memcpy to copy the nul byte with align = 1.
642 B.CreateMemCpy(Dst, Src,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000643 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
Meador Inge0c41d572012-10-18 18:12:40 +0000644 return Dst;
645 }
646};
647
Meador Ingee6d781f2012-10-31 00:20:56 +0000648struct StpCpyOpt: public LibCallOptimization {
649 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
650 // Verify the "stpcpy" function prototype.
651 FunctionType *FT = Callee->getFunctionType();
652 if (FT->getNumParams() != 2 ||
653 FT->getReturnType() != FT->getParamType(0) ||
654 FT->getParamType(0) != FT->getParamType(1) ||
655 FT->getParamType(0) != B.getInt8PtrTy())
656 return 0;
657
658 // These optimizations require DataLayout.
659 if (!TD) return 0;
660
661 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
662 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
663 Value *StrLen = EmitStrLen(Src, B, TD, TLI);
664 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
665 }
666
667 // See if we can get the length of the input string.
668 uint64_t Len = GetStringLength(Src);
669 if (Len == 0) return 0;
670
671 Type *PT = FT->getParamType(0);
672 Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
673 Value *DstEnd = B.CreateGEP(Dst,
674 ConstantInt::get(TD->getIntPtrType(PT),
675 Len - 1));
676
677 // We have enough information to now generate the memcpy call to do the
678 // copy for us. Make a memcpy to copy the nul byte with align = 1.
679 B.CreateMemCpy(Dst, Src, LenV, 1);
680 return DstEnd;
681 }
682};
683
Meador Ingea0885fb2012-10-31 03:33:00 +0000684struct StrNCpyOpt : public LibCallOptimization {
685 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
686 FunctionType *FT = Callee->getFunctionType();
687 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
688 FT->getParamType(0) != FT->getParamType(1) ||
689 FT->getParamType(0) != B.getInt8PtrTy() ||
690 !FT->getParamType(2)->isIntegerTy())
691 return 0;
692
693 Value *Dst = CI->getArgOperand(0);
694 Value *Src = CI->getArgOperand(1);
695 Value *LenOp = CI->getArgOperand(2);
696
697 // See if we can get the length of the input string.
698 uint64_t SrcLen = GetStringLength(Src);
699 if (SrcLen == 0) return 0;
700 --SrcLen;
701
702 if (SrcLen == 0) {
703 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
704 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
705 return Dst;
706 }
707
708 uint64_t Len;
709 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
710 Len = LengthArg->getZExtValue();
711 else
712 return 0;
713
714 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
715
716 // These optimizations require DataLayout.
717 if (!TD) return 0;
718
719 // Let strncpy handle the zero padding
720 if (Len > SrcLen+1) return 0;
721
722 Type *PT = FT->getParamType(0);
723 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
724 B.CreateMemCpy(Dst, Src,
725 ConstantInt::get(TD->getIntPtrType(PT), Len), 1);
726
727 return Dst;
728 }
729};
730
Meador Inge57cfd712012-10-31 03:33:06 +0000731struct StrLenOpt : public LibCallOptimization {
Chad Rosier33daeab2013-02-08 18:00:14 +0000732 virtual bool ignoreCallingConv() { return true; }
Meador Inge57cfd712012-10-31 03:33:06 +0000733 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
734 FunctionType *FT = Callee->getFunctionType();
735 if (FT->getNumParams() != 1 ||
736 FT->getParamType(0) != B.getInt8PtrTy() ||
737 !FT->getReturnType()->isIntegerTy())
738 return 0;
739
740 Value *Src = CI->getArgOperand(0);
741
742 // Constant folding: strlen("xyz") -> 3
743 if (uint64_t Len = GetStringLength(Src))
744 return ConstantInt::get(CI->getType(), Len-1);
745
746 // strlen(x) != 0 --> *x != 0
747 // strlen(x) == 0 --> *x == 0
748 if (isOnlyUsedInZeroEqualityComparison(CI))
749 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
750 return 0;
751 }
752};
753
Meador Inge08684d12012-10-31 04:29:58 +0000754struct StrPBrkOpt : public LibCallOptimization {
755 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
756 FunctionType *FT = Callee->getFunctionType();
757 if (FT->getNumParams() != 2 ||
758 FT->getParamType(0) != B.getInt8PtrTy() ||
759 FT->getParamType(1) != FT->getParamType(0) ||
760 FT->getReturnType() != FT->getParamType(0))
761 return 0;
762
763 StringRef S1, S2;
764 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
765 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
766
767 // strpbrk(s, "") -> NULL
768 // strpbrk("", s) -> NULL
769 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
770 return Constant::getNullValue(CI->getType());
771
772 // Constant folding.
773 if (HasS1 && HasS2) {
774 size_t I = S1.find_first_of(S2);
775 if (I == std::string::npos) // No match.
776 return Constant::getNullValue(CI->getType());
777
778 return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
779 }
780
781 // strpbrk(s, "a") -> strchr(s, 'a')
782 if (TD && HasS2 && S2.size() == 1)
783 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD, TLI);
784
785 return 0;
786 }
787};
788
Meador Ingee0f1dca2012-10-31 14:58:26 +0000789struct StrToOpt : public LibCallOptimization {
790 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
791 FunctionType *FT = Callee->getFunctionType();
792 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
793 !FT->getParamType(0)->isPointerTy() ||
794 !FT->getParamType(1)->isPointerTy())
795 return 0;
796
797 Value *EndPtr = CI->getArgOperand(1);
798 if (isa<ConstantPointerNull>(EndPtr)) {
799 // With a null EndPtr, this function won't capture the main argument.
800 // It would be readonly too, except that it still may write to errno.
Peter Collingbourne328d1b62013-03-02 01:20:18 +0000801 CI->addAttribute(1, Attribute::NoCapture);
Meador Ingee0f1dca2012-10-31 14:58:26 +0000802 }
803
804 return 0;
805 }
806};
807
Meador Inge7629de32012-11-08 01:33:50 +0000808struct StrSpnOpt : public LibCallOptimization {
809 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
810 FunctionType *FT = Callee->getFunctionType();
811 if (FT->getNumParams() != 2 ||
812 FT->getParamType(0) != B.getInt8PtrTy() ||
813 FT->getParamType(1) != FT->getParamType(0) ||
814 !FT->getReturnType()->isIntegerTy())
815 return 0;
816
817 StringRef S1, S2;
818 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
819 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
820
821 // strspn(s, "") -> 0
822 // strspn("", s) -> 0
823 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
824 return Constant::getNullValue(CI->getType());
825
826 // Constant folding.
827 if (HasS1 && HasS2) {
828 size_t Pos = S1.find_first_not_of(S2);
829 if (Pos == StringRef::npos) Pos = S1.size();
830 return ConstantInt::get(CI->getType(), Pos);
831 }
832
833 return 0;
834 }
835};
836
Meador Inge5464ee72012-11-10 15:16:48 +0000837struct StrCSpnOpt : public LibCallOptimization {
838 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
839 FunctionType *FT = Callee->getFunctionType();
840 if (FT->getNumParams() != 2 ||
841 FT->getParamType(0) != B.getInt8PtrTy() ||
842 FT->getParamType(1) != FT->getParamType(0) ||
843 !FT->getReturnType()->isIntegerTy())
844 return 0;
845
846 StringRef S1, S2;
847 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
848 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
849
850 // strcspn("", s) -> 0
851 if (HasS1 && S1.empty())
852 return Constant::getNullValue(CI->getType());
853
854 // Constant folding.
855 if (HasS1 && HasS2) {
856 size_t Pos = S1.find_first_of(S2);
857 if (Pos == StringRef::npos) Pos = S1.size();
858 return ConstantInt::get(CI->getType(), Pos);
859 }
860
861 // strcspn(s, "") -> strlen(s)
862 if (TD && HasS2 && S2.empty())
863 return EmitStrLen(CI->getArgOperand(0), B, TD, TLI);
864
865 return 0;
866 }
867};
868
Meador Inge6e1591a2012-11-11 03:51:48 +0000869struct StrStrOpt : public LibCallOptimization {
870 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
871 FunctionType *FT = Callee->getFunctionType();
872 if (FT->getNumParams() != 2 ||
873 !FT->getParamType(0)->isPointerTy() ||
874 !FT->getParamType(1)->isPointerTy() ||
875 !FT->getReturnType()->isPointerTy())
876 return 0;
877
878 // fold strstr(x, x) -> x.
879 if (CI->getArgOperand(0) == CI->getArgOperand(1))
880 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
881
882 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
883 if (TD && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
884 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD, TLI);
885 if (!StrLen)
886 return 0;
887 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
888 StrLen, B, TD, TLI);
889 if (!StrNCmp)
890 return 0;
891 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
892 UI != UE; ) {
893 ICmpInst *Old = cast<ICmpInst>(*UI++);
894 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
895 ConstantInt::getNullValue(StrNCmp->getType()),
896 "cmp");
897 LCS->replaceAllUsesWith(Old, Cmp);
898 }
899 return CI;
900 }
901
902 // See if either input string is a constant string.
903 StringRef SearchStr, ToFindStr;
904 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
905 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
906
907 // fold strstr(x, "") -> x.
908 if (HasStr2 && ToFindStr.empty())
909 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
910
911 // If both strings are known, constant fold it.
912 if (HasStr1 && HasStr2) {
913 std::string::size_type Offset = SearchStr.find(ToFindStr);
914
915 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
916 return Constant::getNullValue(CI->getType());
917
918 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
919 Value *Result = CastToCStr(CI->getArgOperand(0), B);
920 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
921 return B.CreateBitCast(Result, CI->getType());
922 }
923
924 // fold strstr(x, "y") -> strchr(x, 'y').
925 if (HasStr2 && ToFindStr.size() == 1) {
926 Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TD, TLI);
927 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0;
928 }
929 return 0;
930 }
931};
932
Meador Ingebb51ec82012-11-11 05:11:20 +0000933struct MemCmpOpt : public LibCallOptimization {
934 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
935 FunctionType *FT = Callee->getFunctionType();
936 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
937 !FT->getParamType(1)->isPointerTy() ||
938 !FT->getReturnType()->isIntegerTy(32))
939 return 0;
940
941 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
942
943 if (LHS == RHS) // memcmp(s,s,x) -> 0
944 return Constant::getNullValue(CI->getType());
945
946 // Make sure we have a constant length.
947 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
948 if (!LenC) return 0;
949 uint64_t Len = LenC->getZExtValue();
950
951 if (Len == 0) // memcmp(s1,s2,0) -> 0
952 return Constant::getNullValue(CI->getType());
953
954 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
955 if (Len == 1) {
956 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
957 CI->getType(), "lhsv");
958 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
959 CI->getType(), "rhsv");
960 return B.CreateSub(LHSV, RHSV, "chardiff");
961 }
962
963 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
964 StringRef LHSStr, RHSStr;
965 if (getConstantStringInfo(LHS, LHSStr) &&
966 getConstantStringInfo(RHS, RHSStr)) {
967 // Make sure we're not reading out-of-bounds memory.
968 if (Len > LHSStr.size() || Len > RHSStr.size())
969 return 0;
Meador Inge30d8f0e2012-11-12 14:00:45 +0000970 // Fold the memcmp and normalize the result. This way we get consistent
971 // results across multiple platforms.
972 uint64_t Ret = 0;
973 int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
974 if (Cmp < 0)
975 Ret = -1;
976 else if (Cmp > 0)
977 Ret = 1;
Meador Ingebb51ec82012-11-11 05:11:20 +0000978 return ConstantInt::get(CI->getType(), Ret);
979 }
980
981 return 0;
982 }
983};
984
Meador Inge11b04b42012-11-11 05:54:34 +0000985struct MemCpyOpt : public LibCallOptimization {
986 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
987 // These optimizations require DataLayout.
988 if (!TD) return 0;
989
990 FunctionType *FT = Callee->getFunctionType();
991 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
992 !FT->getParamType(0)->isPointerTy() ||
993 !FT->getParamType(1)->isPointerTy() ||
994 FT->getParamType(2) != TD->getIntPtrType(*Context))
995 return 0;
996
997 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
998 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
999 CI->getArgOperand(2), 1);
1000 return CI->getArgOperand(0);
1001 }
1002};
1003
Meador Inged7cb6002012-11-11 06:22:40 +00001004struct MemMoveOpt : public LibCallOptimization {
1005 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1006 // These optimizations require DataLayout.
1007 if (!TD) return 0;
1008
1009 FunctionType *FT = Callee->getFunctionType();
1010 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1011 !FT->getParamType(0)->isPointerTy() ||
1012 !FT->getParamType(1)->isPointerTy() ||
1013 FT->getParamType(2) != TD->getIntPtrType(*Context))
1014 return 0;
1015
1016 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
1017 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
1018 CI->getArgOperand(2), 1);
1019 return CI->getArgOperand(0);
1020 }
1021};
1022
Meador Inge26ebe392012-11-11 06:49:03 +00001023struct MemSetOpt : public LibCallOptimization {
1024 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1025 // These optimizations require DataLayout.
1026 if (!TD) return 0;
1027
1028 FunctionType *FT = Callee->getFunctionType();
1029 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1030 !FT->getParamType(0)->isPointerTy() ||
1031 !FT->getParamType(1)->isIntegerTy() ||
1032 FT->getParamType(2) != TD->getIntPtrType(*Context))
1033 return 0;
1034
1035 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
1036 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1037 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
1038 return CI->getArgOperand(0);
1039 }
1040};
1041
Meador Inge2920a712012-11-13 04:16:17 +00001042//===----------------------------------------------------------------------===//
1043// Math Library Optimizations
1044//===----------------------------------------------------------------------===//
1045
1046//===----------------------------------------------------------------------===//
1047// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1048
1049struct UnaryDoubleFPOpt : public LibCallOptimization {
1050 bool CheckRetType;
1051 UnaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}
1052 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1053 FunctionType *FT = Callee->getFunctionType();
1054 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1055 !FT->getParamType(0)->isDoubleTy())
1056 return 0;
1057
1058 if (CheckRetType) {
1059 // Check if all the uses for function like 'sin' are converted to float.
1060 for (Value::use_iterator UseI = CI->use_begin(); UseI != CI->use_end();
1061 ++UseI) {
1062 FPTruncInst *Cast = dyn_cast<FPTruncInst>(*UseI);
1063 if (Cast == 0 || !Cast->getType()->isFloatTy())
1064 return 0;
1065 }
1066 }
1067
1068 // If this is something like 'floor((double)floatval)', convert to floorf.
1069 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
1070 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
1071 return 0;
1072
1073 // floor((double)floatval) -> (double)floorf(floatval)
1074 Value *V = Cast->getOperand(0);
1075 V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
1076 return B.CreateFPExt(V, B.getDoubleTy());
1077 }
1078};
1079
1080struct UnsafeFPLibCallOptimization : public LibCallOptimization {
1081 bool UnsafeFPShrink;
1082 UnsafeFPLibCallOptimization(bool UnsafeFPShrink) {
1083 this->UnsafeFPShrink = UnsafeFPShrink;
1084 }
1085};
1086
1087struct CosOpt : public UnsafeFPLibCallOptimization {
1088 CosOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1089 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1090 Value *Ret = NULL;
1091 if (UnsafeFPShrink && Callee->getName() == "cos" &&
1092 TLI->has(LibFunc::cosf)) {
1093 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1094 Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1095 }
1096
1097 FunctionType *FT = Callee->getFunctionType();
1098 // Just make sure this has 1 argument of FP type, which matches the
1099 // result type.
1100 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1101 !FT->getParamType(0)->isFloatingPointTy())
1102 return Ret;
1103
1104 // cos(-x) -> cos(x)
1105 Value *Op1 = CI->getArgOperand(0);
1106 if (BinaryOperator::isFNeg(Op1)) {
1107 BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
1108 return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
1109 }
1110 return Ret;
1111 }
1112};
1113
1114struct PowOpt : public UnsafeFPLibCallOptimization {
1115 PowOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1116 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1117 Value *Ret = NULL;
1118 if (UnsafeFPShrink && Callee->getName() == "pow" &&
1119 TLI->has(LibFunc::powf)) {
1120 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1121 Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1122 }
1123
1124 FunctionType *FT = Callee->getFunctionType();
1125 // Just make sure this has 2 arguments of the same FP type, which match the
1126 // result type.
1127 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1128 FT->getParamType(0) != FT->getParamType(1) ||
1129 !FT->getParamType(0)->isFloatingPointTy())
1130 return Ret;
1131
1132 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
1133 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1134 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1135 return Op1C;
1136 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
1137 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
1138 }
1139
1140 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1141 if (Op2C == 0) return Ret;
1142
1143 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
1144 return ConstantFP::get(CI->getType(), 1.0);
1145
1146 if (Op2C->isExactlyValue(0.5)) {
1147 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1148 // This is faster than calling pow, and still handles negative zero
1149 // and negative infinity correctly.
1150 // TODO: In fast-math mode, this could be just sqrt(x).
1151 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
1152 Value *Inf = ConstantFP::getInfinity(CI->getType());
1153 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
1154 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1155 Callee->getAttributes());
1156 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1157 Callee->getAttributes());
1158 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
1159 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
1160 return Sel;
1161 }
1162
1163 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1164 return Op1;
1165 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
1166 return B.CreateFMul(Op1, Op1, "pow2");
1167 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
1168 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
1169 Op1, "powrecip");
1170 return 0;
1171 }
1172};
1173
1174struct Exp2Opt : public UnsafeFPLibCallOptimization {
1175 Exp2Opt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1176 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1177 Value *Ret = NULL;
1178 if (UnsafeFPShrink && Callee->getName() == "exp2" &&
1179 TLI->has(LibFunc::exp2)) {
1180 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1181 Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1182 }
1183
1184 FunctionType *FT = Callee->getFunctionType();
1185 // Just make sure this has 1 argument of FP type, which matches the
1186 // result type.
1187 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1188 !FT->getParamType(0)->isFloatingPointTy())
1189 return Ret;
1190
1191 Value *Op = CI->getArgOperand(0);
1192 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1193 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1194 Value *LdExpArg = 0;
1195 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1196 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
1197 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
1198 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1199 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
1200 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
1201 }
1202
1203 if (LdExpArg) {
1204 const char *Name;
1205 if (Op->getType()->isFloatTy())
1206 Name = "ldexpf";
1207 else if (Op->getType()->isDoubleTy())
1208 Name = "ldexp";
1209 else
1210 Name = "ldexpl";
1211
1212 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
1213 if (!Op->getType()->isFloatTy())
1214 One = ConstantExpr::getFPExtend(One, Op->getType());
1215
1216 Module *M = Caller->getParent();
1217 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
1218 Op->getType(),
1219 B.getInt32Ty(), NULL);
1220 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1221 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1222 CI->setCallingConv(F->getCallingConv());
1223
1224 return CI;
1225 }
1226 return Ret;
1227 }
1228};
1229
Meador Inge15d099a2012-11-25 20:45:27 +00001230//===----------------------------------------------------------------------===//
1231// Integer Library Call Optimizations
1232//===----------------------------------------------------------------------===//
1233
1234struct FFSOpt : public LibCallOptimization {
1235 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1236 FunctionType *FT = Callee->getFunctionType();
1237 // Just make sure this has 2 arguments of the same FP type, which match the
1238 // result type.
1239 if (FT->getNumParams() != 1 ||
1240 !FT->getReturnType()->isIntegerTy(32) ||
1241 !FT->getParamType(0)->isIntegerTy())
1242 return 0;
1243
1244 Value *Op = CI->getArgOperand(0);
1245
1246 // Constant fold.
1247 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1248 if (CI->isZero()) // ffs(0) -> 0.
1249 return B.getInt32(0);
1250 // ffs(c) -> cttz(c)+1
1251 return B.getInt32(CI->getValue().countTrailingZeros() + 1);
1252 }
1253
1254 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1255 Type *ArgType = Op->getType();
1256 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1257 Intrinsic::cttz, ArgType);
1258 Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");
1259 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
1260 V = B.CreateIntCast(V, B.getInt32Ty(), false);
1261
1262 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
1263 return B.CreateSelect(Cond, V, B.getInt32(0));
1264 }
1265};
1266
Meador Ingedfb3b1a2012-11-26 00:24:07 +00001267struct AbsOpt : public LibCallOptimization {
Chad Rosier33daeab2013-02-08 18:00:14 +00001268 virtual bool ignoreCallingConv() { return true; }
Meador Ingedfb3b1a2012-11-26 00:24:07 +00001269 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1270 FunctionType *FT = Callee->getFunctionType();
1271 // We require integer(integer) where the types agree.
1272 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1273 FT->getParamType(0) != FT->getReturnType())
1274 return 0;
1275
1276 // abs(x) -> x >s -1 ? x : -x
1277 Value *Op = CI->getArgOperand(0);
1278 Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
1279 "ispos");
1280 Value *Neg = B.CreateNeg(Op, "neg");
1281 return B.CreateSelect(Pos, Op, Neg);
1282 }
1283};
1284
Meador Ingea0798ec2012-11-26 02:31:59 +00001285struct IsDigitOpt : public LibCallOptimization {
1286 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1287 FunctionType *FT = Callee->getFunctionType();
1288 // We require integer(i32)
1289 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1290 !FT->getParamType(0)->isIntegerTy(32))
1291 return 0;
1292
1293 // isdigit(c) -> (c-'0') <u 10
1294 Value *Op = CI->getArgOperand(0);
1295 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1296 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
1297 return B.CreateZExt(Op, CI->getType());
1298 }
1299};
1300
Meador Inge017bb752012-11-26 03:10:07 +00001301struct IsAsciiOpt : public LibCallOptimization {
1302 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1303 FunctionType *FT = Callee->getFunctionType();
1304 // We require integer(i32)
1305 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1306 !FT->getParamType(0)->isIntegerTy(32))
1307 return 0;
1308
1309 // isascii(c) -> c <u 128
1310 Value *Op = CI->getArgOperand(0);
1311 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
1312 return B.CreateZExt(Op, CI->getType());
1313 }
1314};
1315
Meador Inge38c44412012-11-26 03:38:52 +00001316struct ToAsciiOpt : public LibCallOptimization {
1317 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1318 FunctionType *FT = Callee->getFunctionType();
1319 // We require i32(i32)
1320 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1321 !FT->getParamType(0)->isIntegerTy(32))
1322 return 0;
1323
Meador Inged6d68592012-11-26 20:37:23 +00001324 // toascii(c) -> c & 0x7f
Meador Inge38c44412012-11-26 03:38:52 +00001325 return B.CreateAnd(CI->getArgOperand(0),
1326 ConstantInt::get(CI->getType(),0x7F));
1327 }
1328};
1329
Meador Inged7aa3232012-11-26 20:37:20 +00001330//===----------------------------------------------------------------------===//
1331// Formatting and IO Library Call Optimizations
1332//===----------------------------------------------------------------------===//
1333
1334struct PrintFOpt : public LibCallOptimization {
1335 Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
1336 IRBuilder<> &B) {
1337 // Check for a fixed format string.
1338 StringRef FormatStr;
1339 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
1340 return 0;
1341
1342 // Empty format string -> noop.
1343 if (FormatStr.empty()) // Tolerate printf's declared void.
1344 return CI->use_empty() ? (Value*)CI :
1345 ConstantInt::get(CI->getType(), 0);
1346
1347 // Do not do any of the following transformations if the printf return value
1348 // is used, in general the printf return value is not compatible with either
1349 // putchar() or puts().
1350 if (!CI->use_empty())
1351 return 0;
1352
1353 // printf("x") -> putchar('x'), even for '%'.
1354 if (FormatStr.size() == 1) {
1355 Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI);
1356 if (CI->use_empty() || !Res) return Res;
1357 return B.CreateIntCast(Res, CI->getType(), true);
1358 }
1359
1360 // printf("foo\n") --> puts("foo")
1361 if (FormatStr[FormatStr.size()-1] == '\n' &&
1362 FormatStr.find('%') == std::string::npos) { // no format characters.
1363 // Create a string literal with no \n on it. We expect the constant merge
1364 // pass to be run after this pass, to merge duplicate strings.
1365 FormatStr = FormatStr.drop_back();
1366 Value *GV = B.CreateGlobalString(FormatStr, "str");
1367 Value *NewCI = EmitPutS(GV, B, TD, TLI);
1368 return (CI->use_empty() || !NewCI) ?
1369 NewCI :
1370 ConstantInt::get(CI->getType(), FormatStr.size()+1);
1371 }
1372
1373 // Optimize specific format strings.
1374 // printf("%c", chr) --> putchar(chr)
1375 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
1376 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1377 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI);
1378
1379 if (CI->use_empty() || !Res) return Res;
1380 return B.CreateIntCast(Res, CI->getType(), true);
1381 }
1382
1383 // printf("%s\n", str) --> puts(str)
1384 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
1385 CI->getArgOperand(1)->getType()->isPointerTy()) {
1386 return EmitPutS(CI->getArgOperand(1), B, TD, TLI);
1387 }
1388 return 0;
1389 }
1390
1391 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1392 // Require one fixed pointer argument and an integer/void result.
1393 FunctionType *FT = Callee->getFunctionType();
1394 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1395 !(FT->getReturnType()->isIntegerTy() ||
1396 FT->getReturnType()->isVoidTy()))
1397 return 0;
1398
1399 if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {
1400 return V;
1401 }
1402
1403 // printf(format, ...) -> iprintf(format, ...) if no floating point
1404 // arguments.
1405 if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {
1406 Module *M = B.GetInsertBlock()->getParent()->getParent();
1407 Constant *IPrintFFn =
1408 M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
1409 CallInst *New = cast<CallInst>(CI->clone());
1410 New->setCalledFunction(IPrintFFn);
1411 B.Insert(New);
1412 return New;
1413 }
1414 return 0;
1415 }
1416};
1417
Meador Inge69ea0272012-11-27 05:57:54 +00001418struct SPrintFOpt : public LibCallOptimization {
1419 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
1420 IRBuilder<> &B) {
1421 // Check for a fixed format string.
1422 StringRef FormatStr;
1423 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1424 return 0;
1425
1426 // If we just have a format string (nothing else crazy) transform it.
1427 if (CI->getNumArgOperands() == 2) {
1428 // Make sure there's no % in the constant array. We could try to handle
1429 // %% -> % in the future if we cared.
1430 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1431 if (FormatStr[i] == '%')
1432 return 0; // we found a format specifier, bail out.
1433
1434 // These optimizations require DataLayout.
1435 if (!TD) return 0;
1436
1437 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1438 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1439 ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
1440 FormatStr.size() + 1), 1); // nul byte.
1441 return ConstantInt::get(CI->getType(), FormatStr.size());
1442 }
1443
1444 // The remaining optimizations require the format string to be "%s" or "%c"
1445 // and have an extra operand.
1446 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1447 CI->getNumArgOperands() < 3)
1448 return 0;
1449
1450 // Decode the second character of the format string.
1451 if (FormatStr[1] == 'c') {
1452 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1453 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1454 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
1455 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
1456 B.CreateStore(V, Ptr);
1457 Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
1458 B.CreateStore(B.getInt8(0), Ptr);
1459
1460 return ConstantInt::get(CI->getType(), 1);
1461 }
1462
1463 if (FormatStr[1] == 's') {
1464 // These optimizations require DataLayout.
1465 if (!TD) return 0;
1466
1467 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1468 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
1469
1470 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD, TLI);
1471 if (!Len)
1472 return 0;
1473 Value *IncLen = B.CreateAdd(Len,
1474 ConstantInt::get(Len->getType(), 1),
1475 "leninc");
1476 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
1477
1478 // The sprintf result is the unincremented number of bytes in the string.
1479 return B.CreateIntCast(Len, CI->getType(), false);
1480 }
1481 return 0;
1482 }
1483
1484 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1485 // Require two fixed pointer arguments and an integer result.
1486 FunctionType *FT = Callee->getFunctionType();
1487 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1488 !FT->getParamType(1)->isPointerTy() ||
1489 !FT->getReturnType()->isIntegerTy())
1490 return 0;
1491
1492 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
1493 return V;
1494 }
1495
1496 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
1497 // point arguments.
1498 if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {
1499 Module *M = B.GetInsertBlock()->getParent()->getParent();
1500 Constant *SIPrintFFn =
1501 M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
1502 CallInst *New = cast<CallInst>(CI->clone());
1503 New->setCalledFunction(SIPrintFFn);
1504 B.Insert(New);
1505 return New;
1506 }
1507 return 0;
1508 }
1509};
1510
Meador Inge28d52912012-11-29 15:45:33 +00001511struct FPrintFOpt : public LibCallOptimization {
1512 Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
1513 IRBuilder<> &B) {
1514 // All the optimizations depend on the format string.
1515 StringRef FormatStr;
1516 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1517 return 0;
1518
1519 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1520 if (CI->getNumArgOperands() == 2) {
1521 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1522 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1523 return 0; // We found a format specifier.
1524
1525 // These optimizations require DataLayout.
1526 if (!TD) return 0;
1527
1528 Value *NewCI = EmitFWrite(CI->getArgOperand(1),
1529 ConstantInt::get(TD->getIntPtrType(*Context),
1530 FormatStr.size()),
1531 CI->getArgOperand(0), B, TD, TLI);
1532 return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0;
1533 }
1534
1535 // The remaining optimizations require the format string to be "%s" or "%c"
1536 // and have an extra operand.
1537 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1538 CI->getNumArgOperands() < 3)
1539 return 0;
1540
1541 // Decode the second character of the format string.
1542 if (FormatStr[1] == 'c') {
1543 // fprintf(F, "%c", chr) --> fputc(chr, F)
1544 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1545 Value *NewCI = EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B,
1546 TD, TLI);
1547 return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
1548 }
1549
1550 if (FormatStr[1] == 's') {
1551 // fprintf(F, "%s", str) --> fputs(str, F)
1552 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
1553 return 0;
1554 return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD, TLI);
1555 }
1556 return 0;
1557 }
1558
1559 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1560 // Require two fixed paramters as pointers and integer result.
1561 FunctionType *FT = Callee->getFunctionType();
1562 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1563 !FT->getParamType(1)->isPointerTy() ||
1564 !FT->getReturnType()->isIntegerTy())
1565 return 0;
1566
1567 if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {
1568 return V;
1569 }
1570
1571 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
1572 // floating point arguments.
1573 if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {
1574 Module *M = B.GetInsertBlock()->getParent()->getParent();
1575 Constant *FIPrintFFn =
1576 M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
1577 CallInst *New = cast<CallInst>(CI->clone());
1578 New->setCalledFunction(FIPrintFFn);
1579 B.Insert(New);
1580 return New;
1581 }
1582 return 0;
1583 }
1584};
1585
Meador Ingec2e33122012-11-29 15:45:39 +00001586struct FWriteOpt : public LibCallOptimization {
1587 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1588 // Require a pointer, an integer, an integer, a pointer, returning integer.
1589 FunctionType *FT = Callee->getFunctionType();
1590 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1591 !FT->getParamType(1)->isIntegerTy() ||
1592 !FT->getParamType(2)->isIntegerTy() ||
1593 !FT->getParamType(3)->isPointerTy() ||
1594 !FT->getReturnType()->isIntegerTy())
1595 return 0;
1596
1597 // Get the element size and count.
1598 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1599 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1600 if (!SizeC || !CountC) return 0;
1601 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
1602
1603 // If this is writing zero records, remove the call (it's a noop).
1604 if (Bytes == 0)
1605 return ConstantInt::get(CI->getType(), 0);
1606
1607 // If this is writing one byte, turn it into fputc.
1608 // This optimisation is only valid, if the return value is unused.
1609 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1610 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1611 Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, TD, TLI);
1612 return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
1613 }
1614
1615 return 0;
1616 }
1617};
1618
Meador Inge5c5e2302012-11-29 15:45:43 +00001619struct FPutsOpt : public LibCallOptimization {
1620 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1621 // These optimizations require DataLayout.
1622 if (!TD) return 0;
1623
1624 // Require two pointers. Also, we can't optimize if return value is used.
1625 FunctionType *FT = Callee->getFunctionType();
1626 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1627 !FT->getParamType(1)->isPointerTy() ||
1628 !CI->use_empty())
1629 return 0;
1630
1631 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1632 uint64_t Len = GetStringLength(CI->getArgOperand(0));
1633 if (!Len) return 0;
1634 // Known to have no uses (see above).
1635 return EmitFWrite(CI->getArgOperand(0),
1636 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
1637 CI->getArgOperand(1), B, TD, TLI);
1638 }
1639};
1640
Meador Ingeaa8cccf2012-11-29 19:15:17 +00001641struct PutsOpt : public LibCallOptimization {
1642 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1643 // Require one fixed pointer argument and an integer/void result.
1644 FunctionType *FT = Callee->getFunctionType();
1645 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1646 !(FT->getReturnType()->isIntegerTy() ||
1647 FT->getReturnType()->isVoidTy()))
1648 return 0;
1649
1650 // Check for a constant string.
1651 StringRef Str;
1652 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
1653 return 0;
1654
1655 if (Str.empty() && CI->use_empty()) {
1656 // puts("") -> putchar('\n')
1657 Value *Res = EmitPutChar(B.getInt32('\n'), B, TD, TLI);
1658 if (CI->use_empty() || !Res) return Res;
1659 return B.CreateIntCast(Res, CI->getType(), true);
1660 }
1661
1662 return 0;
1663 }
1664};
1665
Meador Inge5e890452012-10-13 16:45:24 +00001666} // End anonymous namespace.
1667
1668namespace llvm {
1669
1670class LibCallSimplifierImpl {
Meador Inge5e890452012-10-13 16:45:24 +00001671 const DataLayout *TD;
1672 const TargetLibraryInfo *TLI;
Meador Ingeb69bf6b2012-11-11 03:51:43 +00001673 const LibCallSimplifier *LCS;
Meador Inge2920a712012-11-13 04:16:17 +00001674 bool UnsafeFPShrink;
Nadav Rotemf26b4f02013-02-27 05:53:43 +00001675 StringMap<LibCallOptimization*, BumpPtrAllocator> Optimizations;
Meador Inge5e890452012-10-13 16:45:24 +00001676
1677 // Fortified library call optimizations.
1678 MemCpyChkOpt MemCpyChk;
1679 MemMoveChkOpt MemMoveChk;
1680 MemSetChkOpt MemSetChk;
1681 StrCpyChkOpt StrCpyChk;
Meador Ingefa9d1372012-10-31 00:20:51 +00001682 StpCpyChkOpt StpCpyChk;
Meador Inge5e890452012-10-13 16:45:24 +00001683 StrNCpyChkOpt StrNCpyChk;
1684
Meador Ingebb51ec82012-11-11 05:11:20 +00001685 // String library call optimizations.
Meador Inge73d8a582012-10-13 16:45:32 +00001686 StrCatOpt StrCat;
1687 StrNCatOpt StrNCat;
Meador Inge186f8d92012-10-13 16:45:37 +00001688 StrChrOpt StrChr;
1689 StrRChrOpt StrRChr;
Meador Ingea239c2e2012-10-15 03:47:37 +00001690 StrCmpOpt StrCmp;
1691 StrNCmpOpt StrNCmp;
Meador Inge0c41d572012-10-18 18:12:40 +00001692 StrCpyOpt StrCpy;
Meador Ingee6d781f2012-10-31 00:20:56 +00001693 StpCpyOpt StpCpy;
Meador Ingea0885fb2012-10-31 03:33:00 +00001694 StrNCpyOpt StrNCpy;
Meador Inge57cfd712012-10-31 03:33:06 +00001695 StrLenOpt StrLen;
Meador Inge08684d12012-10-31 04:29:58 +00001696 StrPBrkOpt StrPBrk;
Meador Ingee0f1dca2012-10-31 14:58:26 +00001697 StrToOpt StrTo;
Meador Inge7629de32012-11-08 01:33:50 +00001698 StrSpnOpt StrSpn;
Meador Inge5464ee72012-11-10 15:16:48 +00001699 StrCSpnOpt StrCSpn;
Meador Inge6e1591a2012-11-11 03:51:48 +00001700 StrStrOpt StrStr;
Meador Inge73d8a582012-10-13 16:45:32 +00001701
Meador Ingebb51ec82012-11-11 05:11:20 +00001702 // Memory library call optimizations.
1703 MemCmpOpt MemCmp;
Meador Inge11b04b42012-11-11 05:54:34 +00001704 MemCpyOpt MemCpy;
Meador Inged7cb6002012-11-11 06:22:40 +00001705 MemMoveOpt MemMove;
Meador Inge26ebe392012-11-11 06:49:03 +00001706 MemSetOpt MemSet;
Meador Ingebb51ec82012-11-11 05:11:20 +00001707
Meador Inge2920a712012-11-13 04:16:17 +00001708 // Math library call optimizations.
1709 UnaryDoubleFPOpt UnaryDoubleFP, UnsafeUnaryDoubleFP;
1710 CosOpt Cos; PowOpt Pow; Exp2Opt Exp2;
1711
Meador Inge15d099a2012-11-25 20:45:27 +00001712 // Integer library call optimizations.
1713 FFSOpt FFS;
Meador Ingedfb3b1a2012-11-26 00:24:07 +00001714 AbsOpt Abs;
Meador Ingea0798ec2012-11-26 02:31:59 +00001715 IsDigitOpt IsDigit;
Meador Inge017bb752012-11-26 03:10:07 +00001716 IsAsciiOpt IsAscii;
Meador Inge38c44412012-11-26 03:38:52 +00001717 ToAsciiOpt ToAscii;
Meador Inge15d099a2012-11-25 20:45:27 +00001718
Meador Inged7aa3232012-11-26 20:37:20 +00001719 // Formatting and IO library call optimizations.
1720 PrintFOpt PrintF;
Meador Inge69ea0272012-11-27 05:57:54 +00001721 SPrintFOpt SPrintF;
Meador Inge28d52912012-11-29 15:45:33 +00001722 FPrintFOpt FPrintF;
Meador Ingec2e33122012-11-29 15:45:39 +00001723 FWriteOpt FWrite;
Meador Inge5c5e2302012-11-29 15:45:43 +00001724 FPutsOpt FPuts;
Meador Ingeaa8cccf2012-11-29 19:15:17 +00001725 PutsOpt Puts;
Meador Inged7aa3232012-11-26 20:37:20 +00001726
Meador Inge5e890452012-10-13 16:45:24 +00001727 void initOptimizations();
Meador Ingee29c8802012-11-10 03:11:10 +00001728 void addOpt(LibFunc::Func F, LibCallOptimization* Opt);
Meador Inge2920a712012-11-13 04:16:17 +00001729 void addOpt(LibFunc::Func F1, LibFunc::Func F2, LibCallOptimization* Opt);
Meador Inge5e890452012-10-13 16:45:24 +00001730public:
Meador Ingeb69bf6b2012-11-11 03:51:43 +00001731 LibCallSimplifierImpl(const DataLayout *TD, const TargetLibraryInfo *TLI,
Meador Inge2920a712012-11-13 04:16:17 +00001732 const LibCallSimplifier *LCS,
1733 bool UnsafeFPShrink = false)
1734 : UnaryDoubleFP(false), UnsafeUnaryDoubleFP(true),
1735 Cos(UnsafeFPShrink), Pow(UnsafeFPShrink), Exp2(UnsafeFPShrink) {
Meador Inge5e890452012-10-13 16:45:24 +00001736 this->TD = TD;
1737 this->TLI = TLI;
Meador Ingeb69bf6b2012-11-11 03:51:43 +00001738 this->LCS = LCS;
Meador Inge2920a712012-11-13 04:16:17 +00001739 this->UnsafeFPShrink = UnsafeFPShrink;
Meador Inge5e890452012-10-13 16:45:24 +00001740 }
1741
1742 Value *optimizeCall(CallInst *CI);
1743};
1744
1745void LibCallSimplifierImpl::initOptimizations() {
1746 // Fortified library call optimizations.
1747 Optimizations["__memcpy_chk"] = &MemCpyChk;
1748 Optimizations["__memmove_chk"] = &MemMoveChk;
1749 Optimizations["__memset_chk"] = &MemSetChk;
1750 Optimizations["__strcpy_chk"] = &StrCpyChk;
Meador Ingefa9d1372012-10-31 00:20:51 +00001751 Optimizations["__stpcpy_chk"] = &StpCpyChk;
Meador Inge5e890452012-10-13 16:45:24 +00001752 Optimizations["__strncpy_chk"] = &StrNCpyChk;
1753 Optimizations["__stpncpy_chk"] = &StrNCpyChk;
Meador Inge73d8a582012-10-13 16:45:32 +00001754
Meador Ingebb51ec82012-11-11 05:11:20 +00001755 // String library call optimizations.
Meador Ingee29c8802012-11-10 03:11:10 +00001756 addOpt(LibFunc::strcat, &StrCat);
1757 addOpt(LibFunc::strncat, &StrNCat);
1758 addOpt(LibFunc::strchr, &StrChr);
1759 addOpt(LibFunc::strrchr, &StrRChr);
1760 addOpt(LibFunc::strcmp, &StrCmp);
1761 addOpt(LibFunc::strncmp, &StrNCmp);
1762 addOpt(LibFunc::strcpy, &StrCpy);
1763 addOpt(LibFunc::stpcpy, &StpCpy);
1764 addOpt(LibFunc::strncpy, &StrNCpy);
1765 addOpt(LibFunc::strlen, &StrLen);
1766 addOpt(LibFunc::strpbrk, &StrPBrk);
1767 addOpt(LibFunc::strtol, &StrTo);
1768 addOpt(LibFunc::strtod, &StrTo);
1769 addOpt(LibFunc::strtof, &StrTo);
1770 addOpt(LibFunc::strtoul, &StrTo);
1771 addOpt(LibFunc::strtoll, &StrTo);
1772 addOpt(LibFunc::strtold, &StrTo);
1773 addOpt(LibFunc::strtoull, &StrTo);
1774 addOpt(LibFunc::strspn, &StrSpn);
Meador Inge5464ee72012-11-10 15:16:48 +00001775 addOpt(LibFunc::strcspn, &StrCSpn);
Meador Inge6e1591a2012-11-11 03:51:48 +00001776 addOpt(LibFunc::strstr, &StrStr);
Meador Ingebb51ec82012-11-11 05:11:20 +00001777
1778 // Memory library call optimizations.
1779 addOpt(LibFunc::memcmp, &MemCmp);
Meador Inge11b04b42012-11-11 05:54:34 +00001780 addOpt(LibFunc::memcpy, &MemCpy);
Meador Inged7cb6002012-11-11 06:22:40 +00001781 addOpt(LibFunc::memmove, &MemMove);
Meador Inge26ebe392012-11-11 06:49:03 +00001782 addOpt(LibFunc::memset, &MemSet);
Meador Inge2920a712012-11-13 04:16:17 +00001783
1784 // Math library call optimizations.
1785 addOpt(LibFunc::ceil, LibFunc::ceilf, &UnaryDoubleFP);
1786 addOpt(LibFunc::fabs, LibFunc::fabsf, &UnaryDoubleFP);
1787 addOpt(LibFunc::floor, LibFunc::floorf, &UnaryDoubleFP);
1788 addOpt(LibFunc::rint, LibFunc::rintf, &UnaryDoubleFP);
1789 addOpt(LibFunc::round, LibFunc::roundf, &UnaryDoubleFP);
1790 addOpt(LibFunc::nearbyint, LibFunc::nearbyintf, &UnaryDoubleFP);
1791 addOpt(LibFunc::trunc, LibFunc::truncf, &UnaryDoubleFP);
1792
1793 if(UnsafeFPShrink) {
1794 addOpt(LibFunc::acos, LibFunc::acosf, &UnsafeUnaryDoubleFP);
1795 addOpt(LibFunc::acosh, LibFunc::acoshf, &UnsafeUnaryDoubleFP);
1796 addOpt(LibFunc::asin, LibFunc::asinf, &UnsafeUnaryDoubleFP);
1797 addOpt(LibFunc::asinh, LibFunc::asinhf, &UnsafeUnaryDoubleFP);
1798 addOpt(LibFunc::atan, LibFunc::atanf, &UnsafeUnaryDoubleFP);
1799 addOpt(LibFunc::atanh, LibFunc::atanhf, &UnsafeUnaryDoubleFP);
1800 addOpt(LibFunc::cbrt, LibFunc::cbrtf, &UnsafeUnaryDoubleFP);
1801 addOpt(LibFunc::cosh, LibFunc::coshf, &UnsafeUnaryDoubleFP);
1802 addOpt(LibFunc::exp, LibFunc::expf, &UnsafeUnaryDoubleFP);
1803 addOpt(LibFunc::exp10, LibFunc::exp10f, &UnsafeUnaryDoubleFP);
1804 addOpt(LibFunc::expm1, LibFunc::expm1f, &UnsafeUnaryDoubleFP);
1805 addOpt(LibFunc::log, LibFunc::logf, &UnsafeUnaryDoubleFP);
1806 addOpt(LibFunc::log10, LibFunc::log10f, &UnsafeUnaryDoubleFP);
1807 addOpt(LibFunc::log1p, LibFunc::log1pf, &UnsafeUnaryDoubleFP);
1808 addOpt(LibFunc::log2, LibFunc::log2f, &UnsafeUnaryDoubleFP);
1809 addOpt(LibFunc::logb, LibFunc::logbf, &UnsafeUnaryDoubleFP);
1810 addOpt(LibFunc::sin, LibFunc::sinf, &UnsafeUnaryDoubleFP);
1811 addOpt(LibFunc::sinh, LibFunc::sinhf, &UnsafeUnaryDoubleFP);
1812 addOpt(LibFunc::sqrt, LibFunc::sqrtf, &UnsafeUnaryDoubleFP);
1813 addOpt(LibFunc::tan, LibFunc::tanf, &UnsafeUnaryDoubleFP);
1814 addOpt(LibFunc::tanh, LibFunc::tanhf, &UnsafeUnaryDoubleFP);
1815 }
1816
1817 addOpt(LibFunc::cosf, &Cos);
1818 addOpt(LibFunc::cos, &Cos);
1819 addOpt(LibFunc::cosl, &Cos);
1820 addOpt(LibFunc::powf, &Pow);
1821 addOpt(LibFunc::pow, &Pow);
1822 addOpt(LibFunc::powl, &Pow);
1823 Optimizations["llvm.pow.f32"] = &Pow;
1824 Optimizations["llvm.pow.f64"] = &Pow;
1825 Optimizations["llvm.pow.f80"] = &Pow;
1826 Optimizations["llvm.pow.f128"] = &Pow;
1827 Optimizations["llvm.pow.ppcf128"] = &Pow;
1828 addOpt(LibFunc::exp2l, &Exp2);
1829 addOpt(LibFunc::exp2, &Exp2);
1830 addOpt(LibFunc::exp2f, &Exp2);
1831 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1832 Optimizations["llvm.exp2.f128"] = &Exp2;
1833 Optimizations["llvm.exp2.f80"] = &Exp2;
1834 Optimizations["llvm.exp2.f64"] = &Exp2;
1835 Optimizations["llvm.exp2.f32"] = &Exp2;
Meador Inge15d099a2012-11-25 20:45:27 +00001836
1837 // Integer library call optimizations.
1838 addOpt(LibFunc::ffs, &FFS);
1839 addOpt(LibFunc::ffsl, &FFS);
1840 addOpt(LibFunc::ffsll, &FFS);
Meador Ingedfb3b1a2012-11-26 00:24:07 +00001841 addOpt(LibFunc::abs, &Abs);
1842 addOpt(LibFunc::labs, &Abs);
1843 addOpt(LibFunc::llabs, &Abs);
Meador Ingea0798ec2012-11-26 02:31:59 +00001844 addOpt(LibFunc::isdigit, &IsDigit);
Meador Inge017bb752012-11-26 03:10:07 +00001845 addOpt(LibFunc::isascii, &IsAscii);
Meador Inge38c44412012-11-26 03:38:52 +00001846 addOpt(LibFunc::toascii, &ToAscii);
Meador Inged7aa3232012-11-26 20:37:20 +00001847
1848 // Formatting and IO library call optimizations.
1849 addOpt(LibFunc::printf, &PrintF);
Meador Inge69ea0272012-11-27 05:57:54 +00001850 addOpt(LibFunc::sprintf, &SPrintF);
Meador Inge28d52912012-11-29 15:45:33 +00001851 addOpt(LibFunc::fprintf, &FPrintF);
Meador Ingec2e33122012-11-29 15:45:39 +00001852 addOpt(LibFunc::fwrite, &FWrite);
Meador Inge5c5e2302012-11-29 15:45:43 +00001853 addOpt(LibFunc::fputs, &FPuts);
Meador Ingeaa8cccf2012-11-29 19:15:17 +00001854 addOpt(LibFunc::puts, &Puts);
Meador Inge5e890452012-10-13 16:45:24 +00001855}
1856
1857Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {
1858 if (Optimizations.empty())
1859 initOptimizations();
1860
1861 Function *Callee = CI->getCalledFunction();
1862 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1863 if (LCO) {
1864 IRBuilder<> Builder(CI);
Meador Ingeb69bf6b2012-11-11 03:51:43 +00001865 return LCO->optimizeCall(CI, TD, TLI, LCS, Builder);
Meador Inge5e890452012-10-13 16:45:24 +00001866 }
1867 return 0;
1868}
1869
Meador Ingee29c8802012-11-10 03:11:10 +00001870void LibCallSimplifierImpl::addOpt(LibFunc::Func F, LibCallOptimization* Opt) {
1871 if (TLI->has(F))
1872 Optimizations[TLI->getName(F)] = Opt;
1873}
1874
Meador Inge2920a712012-11-13 04:16:17 +00001875void LibCallSimplifierImpl::addOpt(LibFunc::Func F1, LibFunc::Func F2,
1876 LibCallOptimization* Opt) {
1877 if (TLI->has(F1) && TLI->has(F2))
1878 Optimizations[TLI->getName(F1)] = Opt;
1879}
1880
Meador Inge5e890452012-10-13 16:45:24 +00001881LibCallSimplifier::LibCallSimplifier(const DataLayout *TD,
Meador Inge2920a712012-11-13 04:16:17 +00001882 const TargetLibraryInfo *TLI,
1883 bool UnsafeFPShrink) {
1884 Impl = new LibCallSimplifierImpl(TD, TLI, this, UnsafeFPShrink);
Meador Inge5e890452012-10-13 16:45:24 +00001885}
1886
1887LibCallSimplifier::~LibCallSimplifier() {
1888 delete Impl;
1889}
1890
1891Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
Bill Wendling143d4642013-02-22 00:12:35 +00001892 if (CI->hasFnAttr(Attribute::NoBuiltin)) return 0;
Meador Inge5e890452012-10-13 16:45:24 +00001893 return Impl->optimizeCall(CI);
1894}
1895
Meador Ingeb69bf6b2012-11-11 03:51:43 +00001896void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const {
1897 I->replaceAllUsesWith(With);
1898 I->eraseFromParent();
1899}
1900
Meador Inge5e890452012-10-13 16:45:24 +00001901}