1 //===------ SimplifyLibCalls.cpp - Library calls simplifier ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Utils/SimplifyLibCalls.h"
18 #include "llvm/DataLayout.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Analysis/ValueTracking.h"
21 #include "llvm/Function.h"
22 #include "llvm/IRBuilder.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/Target/TargetLibraryInfo.h"
25 #include "llvm/Transforms/Utils/BuildLibCalls.h"
29 /// This class is the abstract base class for the set of optimizations that
30 /// corresponds to one library call.
32 class LibCallOptimization {
36 const TargetLibraryInfo *TLI;
37 const LibCallSimplifier *LCS;
40 LibCallOptimization() { }
41 virtual ~LibCallOptimization() {}
43 /// callOptimizer - This pure virtual method is implemented by base classes to
44 /// do various optimizations. If this returns null then no transformation was
45 /// performed. If it returns CI, then it transformed the call and CI is to be
46 /// deleted. If it returns something else, replace CI with the new value and
48 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
51 Value *optimizeCall(CallInst *CI, const DataLayout *TD,
52 const TargetLibraryInfo *TLI,
53 const LibCallSimplifier *LCS, IRBuilder<> &B) {
54 Caller = CI->getParent()->getParent();
58 if (CI->getCalledFunction())
59 Context = &CI->getCalledFunction()->getContext();
61 // We never change the calling convention.
62 if (CI->getCallingConv() != llvm::CallingConv::C)
65 return callOptimizer(CI->getCalledFunction(), CI, B);
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
73 /// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
74 /// value is equal or not-equal to zero.
75 static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
76 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
78 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
80 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
83 // Unknown instruction.
89 /// isOnlyUsedInEqualityComparison - Return true if it is only used in equality
90 /// comparisons with With.
91 static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
92 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
94 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
95 if (IC->isEquality() && IC->getOperand(1) == With)
97 // Unknown instruction.
103 //===----------------------------------------------------------------------===//
104 // Fortified Library Call Optimizations
105 //===----------------------------------------------------------------------===//
107 struct FortifiedLibCallOptimization : public LibCallOptimization {
109 virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
110 bool isString) const = 0;
113 struct InstFortifiedLibCallOptimization : public FortifiedLibCallOptimization {
116 bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const {
117 if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
119 if (ConstantInt *SizeCI =
120 dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
121 if (SizeCI->isAllOnesValue())
124 uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
125 // If the length is 0 we don't know how long it is and so we can't
127 if (Len == 0) return false;
128 return SizeCI->getZExtValue() >= Len;
130 if (ConstantInt *Arg = dyn_cast<ConstantInt>(
131 CI->getArgOperand(SizeArgOp)))
132 return SizeCI->getZExtValue() >= Arg->getZExtValue();
138 struct MemCpyChkOpt : public InstFortifiedLibCallOptimization {
139 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
141 FunctionType *FT = Callee->getFunctionType();
142 LLVMContext &Context = CI->getParent()->getContext();
144 // Check if this has the right signature.
145 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
146 !FT->getParamType(0)->isPointerTy() ||
147 !FT->getParamType(1)->isPointerTy() ||
148 FT->getParamType(2) != TD->getIntPtrType(Context) ||
149 FT->getParamType(3) != TD->getIntPtrType(Context))
152 if (isFoldable(3, 2, false)) {
153 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
154 CI->getArgOperand(2), 1);
155 return CI->getArgOperand(0);
161 struct MemMoveChkOpt : public InstFortifiedLibCallOptimization {
162 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
164 FunctionType *FT = Callee->getFunctionType();
165 LLVMContext &Context = CI->getParent()->getContext();
167 // Check if this has the right signature.
168 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
169 !FT->getParamType(0)->isPointerTy() ||
170 !FT->getParamType(1)->isPointerTy() ||
171 FT->getParamType(2) != TD->getIntPtrType(Context) ||
172 FT->getParamType(3) != TD->getIntPtrType(Context))
175 if (isFoldable(3, 2, false)) {
176 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
177 CI->getArgOperand(2), 1);
178 return CI->getArgOperand(0);
184 struct MemSetChkOpt : public InstFortifiedLibCallOptimization {
185 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
187 FunctionType *FT = Callee->getFunctionType();
188 LLVMContext &Context = CI->getParent()->getContext();
190 // Check if this has the right signature.
191 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
192 !FT->getParamType(0)->isPointerTy() ||
193 !FT->getParamType(1)->isIntegerTy() ||
194 FT->getParamType(2) != TD->getIntPtrType(Context) ||
195 FT->getParamType(3) != TD->getIntPtrType(Context))
198 if (isFoldable(3, 2, false)) {
199 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
201 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
202 return CI->getArgOperand(0);
208 struct StrCpyChkOpt : public InstFortifiedLibCallOptimization {
209 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
211 StringRef Name = Callee->getName();
212 FunctionType *FT = Callee->getFunctionType();
213 LLVMContext &Context = CI->getParent()->getContext();
215 // Check if this has the right signature.
216 if (FT->getNumParams() != 3 ||
217 FT->getReturnType() != FT->getParamType(0) ||
218 FT->getParamType(0) != FT->getParamType(1) ||
219 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
220 FT->getParamType(2) != TD->getIntPtrType(Context))
223 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
224 if (Dst == Src) // __strcpy_chk(x,x) -> x
227 // If a) we don't have any length information, or b) we know this will
228 // fit then just lower to a plain strcpy. Otherwise we'll keep our
229 // strcpy_chk call which may fail at runtime if the size is too long.
230 // TODO: It might be nice to get a maximum length out of the possible
231 // string lengths for varying.
232 if (isFoldable(2, 1, true)) {
233 Value *Ret = EmitStrCpy(Dst, Src, B, TD, TLI, Name.substr(2, 6));
236 // Maybe we can stil fold __strcpy_chk to __memcpy_chk.
237 uint64_t Len = GetStringLength(Src);
238 if (Len == 0) return 0;
240 // This optimization require DataLayout.
244 EmitMemCpyChk(Dst, Src,
245 ConstantInt::get(TD->getIntPtrType(Context), Len),
246 CI->getArgOperand(2), B, TD, TLI);
253 struct StpCpyChkOpt : public InstFortifiedLibCallOptimization {
254 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
256 StringRef Name = Callee->getName();
257 FunctionType *FT = Callee->getFunctionType();
258 LLVMContext &Context = CI->getParent()->getContext();
260 // Check if this has the right signature.
261 if (FT->getNumParams() != 3 ||
262 FT->getReturnType() != FT->getParamType(0) ||
263 FT->getParamType(0) != FT->getParamType(1) ||
264 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
265 FT->getParamType(2) != TD->getIntPtrType(FT->getParamType(0)))
268 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
269 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
270 Value *StrLen = EmitStrLen(Src, B, TD, TLI);
271 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
274 // If a) we don't have any length information, or b) we know this will
275 // fit then just lower to a plain stpcpy. Otherwise we'll keep our
276 // stpcpy_chk call which may fail at runtime if the size is too long.
277 // TODO: It might be nice to get a maximum length out of the possible
278 // string lengths for varying.
279 if (isFoldable(2, 1, true)) {
280 Value *Ret = EmitStrCpy(Dst, Src, B, TD, TLI, Name.substr(2, 6));
283 // Maybe we can stil fold __stpcpy_chk to __memcpy_chk.
284 uint64_t Len = GetStringLength(Src);
285 if (Len == 0) return 0;
287 // This optimization require DataLayout.
290 Type *PT = FT->getParamType(0);
291 Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
292 Value *DstEnd = B.CreateGEP(Dst,
293 ConstantInt::get(TD->getIntPtrType(PT),
295 if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, TD, TLI))
303 struct StrNCpyChkOpt : public InstFortifiedLibCallOptimization {
304 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
306 StringRef Name = Callee->getName();
307 FunctionType *FT = Callee->getFunctionType();
308 LLVMContext &Context = CI->getParent()->getContext();
310 // Check if this has the right signature.
311 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
312 FT->getParamType(0) != FT->getParamType(1) ||
313 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
314 !FT->getParamType(2)->isIntegerTy() ||
315 FT->getParamType(3) != TD->getIntPtrType(Context))
318 if (isFoldable(3, 2, false)) {
319 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
320 CI->getArgOperand(2), B, TD, TLI,
328 //===----------------------------------------------------------------------===//
329 // String and Memory Library Call Optimizations
330 //===----------------------------------------------------------------------===//
332 struct StrCatOpt : public LibCallOptimization {
333 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
334 // Verify the "strcat" function prototype.
335 FunctionType *FT = Callee->getFunctionType();
336 if (FT->getNumParams() != 2 ||
337 FT->getReturnType() != B.getInt8PtrTy() ||
338 FT->getParamType(0) != FT->getReturnType() ||
339 FT->getParamType(1) != FT->getReturnType())
342 // Extract some information from the instruction
343 Value *Dst = CI->getArgOperand(0);
344 Value *Src = CI->getArgOperand(1);
346 // See if we can get the length of the input string.
347 uint64_t Len = GetStringLength(Src);
348 if (Len == 0) return 0;
349 --Len; // Unbias length.
351 // Handle the simple, do-nothing case: strcat(x, "") -> x
355 // These optimizations require DataLayout.
358 return emitStrLenMemCpy(Src, Dst, Len, B);
361 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
363 // We need to find the end of the destination string. That's where the
364 // memory is to be moved to. We just generate a call to strlen.
365 Value *DstLen = EmitStrLen(Dst, B, TD, TLI);
369 // Now that we have the destination's length, we must index into the
370 // destination's pointer to get the actual memcpy destination (end of
371 // the string .. we're concatenating).
372 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
374 // We have enough information to now generate the memcpy call to do the
375 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
376 B.CreateMemCpy(CpyDst, Src,
377 ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1);
382 struct StrNCatOpt : public StrCatOpt {
383 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
384 // Verify the "strncat" function prototype.
385 FunctionType *FT = Callee->getFunctionType();
386 if (FT->getNumParams() != 3 ||
387 FT->getReturnType() != B.getInt8PtrTy() ||
388 FT->getParamType(0) != FT->getReturnType() ||
389 FT->getParamType(1) != FT->getReturnType() ||
390 !FT->getParamType(2)->isIntegerTy())
393 // Extract some information from the instruction
394 Value *Dst = CI->getArgOperand(0);
395 Value *Src = CI->getArgOperand(1);
398 // We don't do anything if length is not constant
399 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
400 Len = LengthArg->getZExtValue();
404 // See if we can get the length of the input string.
405 uint64_t SrcLen = GetStringLength(Src);
406 if (SrcLen == 0) return 0;
407 --SrcLen; // Unbias length.
409 // Handle the simple, do-nothing cases:
410 // strncat(x, "", c) -> x
411 // strncat(x, c, 0) -> x
412 if (SrcLen == 0 || Len == 0) return Dst;
414 // These optimizations require DataLayout.
417 // We don't optimize this case
418 if (Len < SrcLen) return 0;
420 // strncat(x, s, c) -> strcat(x, s)
421 // s is constant so the strcat can be optimized further
422 return emitStrLenMemCpy(Src, Dst, SrcLen, B);
426 struct StrChrOpt : public LibCallOptimization {
427 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
428 // Verify the "strchr" function prototype.
429 FunctionType *FT = Callee->getFunctionType();
430 if (FT->getNumParams() != 2 ||
431 FT->getReturnType() != B.getInt8PtrTy() ||
432 FT->getParamType(0) != FT->getReturnType() ||
433 !FT->getParamType(1)->isIntegerTy(32))
436 Value *SrcStr = CI->getArgOperand(0);
438 // If the second operand is non-constant, see if we can compute the length
439 // of the input string and turn this into memchr.
440 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
442 // These optimizations require DataLayout.
445 uint64_t Len = GetStringLength(SrcStr);
446 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
449 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
450 ConstantInt::get(TD->getIntPtrType(*Context), Len),
454 // Otherwise, the character is a constant, see if the first argument is
455 // a string literal. If so, we can constant fold.
457 if (!getConstantStringInfo(SrcStr, Str))
460 // Compute the offset, make sure to handle the case when we're searching for
461 // zero (a weird way to spell strlen).
462 size_t I = CharC->getSExtValue() == 0 ?
463 Str.size() : Str.find(CharC->getSExtValue());
464 if (I == StringRef::npos) // Didn't find the char. strchr returns null.
465 return Constant::getNullValue(CI->getType());
467 // strchr(s+n,c) -> gep(s+n+i,c)
468 return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
472 struct StrRChrOpt : public LibCallOptimization {
473 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
474 // Verify the "strrchr" function prototype.
475 FunctionType *FT = Callee->getFunctionType();
476 if (FT->getNumParams() != 2 ||
477 FT->getReturnType() != B.getInt8PtrTy() ||
478 FT->getParamType(0) != FT->getReturnType() ||
479 !FT->getParamType(1)->isIntegerTy(32))
482 Value *SrcStr = CI->getArgOperand(0);
483 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
485 // Cannot fold anything if we're not looking for a constant.
490 if (!getConstantStringInfo(SrcStr, Str)) {
491 // strrchr(s, 0) -> strchr(s, 0)
492 if (TD && CharC->isZero())
493 return EmitStrChr(SrcStr, '\0', B, TD, TLI);
497 // Compute the offset.
498 size_t I = CharC->getSExtValue() == 0 ?
499 Str.size() : Str.rfind(CharC->getSExtValue());
500 if (I == StringRef::npos) // Didn't find the char. Return null.
501 return Constant::getNullValue(CI->getType());
503 // strrchr(s+n,c) -> gep(s+n+i,c)
504 return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
508 struct StrCmpOpt : public LibCallOptimization {
509 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
510 // Verify the "strcmp" function prototype.
511 FunctionType *FT = Callee->getFunctionType();
512 if (FT->getNumParams() != 2 ||
513 !FT->getReturnType()->isIntegerTy(32) ||
514 FT->getParamType(0) != FT->getParamType(1) ||
515 FT->getParamType(0) != B.getInt8PtrTy())
518 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
519 if (Str1P == Str2P) // strcmp(x,x) -> 0
520 return ConstantInt::get(CI->getType(), 0);
522 StringRef Str1, Str2;
523 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
524 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
526 // strcmp(x, y) -> cnst (if both x and y are constant strings)
527 if (HasStr1 && HasStr2)
528 return ConstantInt::get(CI->getType(), Str1.compare(Str2));
530 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
531 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
534 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
535 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
537 // strcmp(P, "x") -> memcmp(P, "x", 2)
538 uint64_t Len1 = GetStringLength(Str1P);
539 uint64_t Len2 = GetStringLength(Str2P);
541 // These optimizations require DataLayout.
544 return EmitMemCmp(Str1P, Str2P,
545 ConstantInt::get(TD->getIntPtrType(*Context),
546 std::min(Len1, Len2)), B, TD, TLI);
553 struct StrNCmpOpt : public LibCallOptimization {
554 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
555 // Verify the "strncmp" function prototype.
556 FunctionType *FT = Callee->getFunctionType();
557 if (FT->getNumParams() != 3 ||
558 !FT->getReturnType()->isIntegerTy(32) ||
559 FT->getParamType(0) != FT->getParamType(1) ||
560 FT->getParamType(0) != B.getInt8PtrTy() ||
561 !FT->getParamType(2)->isIntegerTy())
564 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
565 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
566 return ConstantInt::get(CI->getType(), 0);
568 // Get the length argument if it is constant.
570 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
571 Length = LengthArg->getZExtValue();
575 if (Length == 0) // strncmp(x,y,0) -> 0
576 return ConstantInt::get(CI->getType(), 0);
578 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
579 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD, TLI);
581 StringRef Str1, Str2;
582 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
583 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
585 // strncmp(x, y) -> cnst (if both x and y are constant strings)
586 if (HasStr1 && HasStr2) {
587 StringRef SubStr1 = Str1.substr(0, Length);
588 StringRef SubStr2 = Str2.substr(0, Length);
589 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
592 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
593 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
596 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
597 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
603 struct StrCpyOpt : public LibCallOptimization {
604 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
605 // Verify the "strcpy" function prototype.
606 FunctionType *FT = Callee->getFunctionType();
607 if (FT->getNumParams() != 2 ||
608 FT->getReturnType() != FT->getParamType(0) ||
609 FT->getParamType(0) != FT->getParamType(1) ||
610 FT->getParamType(0) != B.getInt8PtrTy())
613 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
614 if (Dst == Src) // strcpy(x,x) -> x
617 // These optimizations require DataLayout.
620 // See if we can get the length of the input string.
621 uint64_t Len = GetStringLength(Src);
622 if (Len == 0) return 0;
624 // We have enough information to now generate the memcpy call to do the
625 // copy for us. Make a memcpy to copy the nul byte with align = 1.
626 B.CreateMemCpy(Dst, Src,
627 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
632 struct StpCpyOpt: public LibCallOptimization {
633 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
634 // Verify the "stpcpy" function prototype.
635 FunctionType *FT = Callee->getFunctionType();
636 if (FT->getNumParams() != 2 ||
637 FT->getReturnType() != FT->getParamType(0) ||
638 FT->getParamType(0) != FT->getParamType(1) ||
639 FT->getParamType(0) != B.getInt8PtrTy())
642 // These optimizations require DataLayout.
645 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
646 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
647 Value *StrLen = EmitStrLen(Src, B, TD, TLI);
648 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
651 // See if we can get the length of the input string.
652 uint64_t Len = GetStringLength(Src);
653 if (Len == 0) return 0;
655 Type *PT = FT->getParamType(0);
656 Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
657 Value *DstEnd = B.CreateGEP(Dst,
658 ConstantInt::get(TD->getIntPtrType(PT),
661 // We have enough information to now generate the memcpy call to do the
662 // copy for us. Make a memcpy to copy the nul byte with align = 1.
663 B.CreateMemCpy(Dst, Src, LenV, 1);
668 struct StrNCpyOpt : public LibCallOptimization {
669 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
670 FunctionType *FT = Callee->getFunctionType();
671 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
672 FT->getParamType(0) != FT->getParamType(1) ||
673 FT->getParamType(0) != B.getInt8PtrTy() ||
674 !FT->getParamType(2)->isIntegerTy())
677 Value *Dst = CI->getArgOperand(0);
678 Value *Src = CI->getArgOperand(1);
679 Value *LenOp = CI->getArgOperand(2);
681 // See if we can get the length of the input string.
682 uint64_t SrcLen = GetStringLength(Src);
683 if (SrcLen == 0) return 0;
687 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
688 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
693 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
694 Len = LengthArg->getZExtValue();
698 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
700 // These optimizations require DataLayout.
703 // Let strncpy handle the zero padding
704 if (Len > SrcLen+1) return 0;
706 Type *PT = FT->getParamType(0);
707 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
708 B.CreateMemCpy(Dst, Src,
709 ConstantInt::get(TD->getIntPtrType(PT), Len), 1);
715 struct StrLenOpt : public LibCallOptimization {
716 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
717 FunctionType *FT = Callee->getFunctionType();
718 if (FT->getNumParams() != 1 ||
719 FT->getParamType(0) != B.getInt8PtrTy() ||
720 !FT->getReturnType()->isIntegerTy())
723 Value *Src = CI->getArgOperand(0);
725 // Constant folding: strlen("xyz") -> 3
726 if (uint64_t Len = GetStringLength(Src))
727 return ConstantInt::get(CI->getType(), Len-1);
729 // strlen(x) != 0 --> *x != 0
730 // strlen(x) == 0 --> *x == 0
731 if (isOnlyUsedInZeroEqualityComparison(CI))
732 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
737 struct StrPBrkOpt : public LibCallOptimization {
738 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
739 FunctionType *FT = Callee->getFunctionType();
740 if (FT->getNumParams() != 2 ||
741 FT->getParamType(0) != B.getInt8PtrTy() ||
742 FT->getParamType(1) != FT->getParamType(0) ||
743 FT->getReturnType() != FT->getParamType(0))
747 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
748 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
750 // strpbrk(s, "") -> NULL
751 // strpbrk("", s) -> NULL
752 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
753 return Constant::getNullValue(CI->getType());
756 if (HasS1 && HasS2) {
757 size_t I = S1.find_first_of(S2);
758 if (I == std::string::npos) // No match.
759 return Constant::getNullValue(CI->getType());
761 return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
764 // strpbrk(s, "a") -> strchr(s, 'a')
765 if (TD && HasS2 && S2.size() == 1)
766 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD, TLI);
772 struct StrToOpt : public LibCallOptimization {
773 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
774 FunctionType *FT = Callee->getFunctionType();
775 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
776 !FT->getParamType(0)->isPointerTy() ||
777 !FT->getParamType(1)->isPointerTy())
780 Value *EndPtr = CI->getArgOperand(1);
781 if (isa<ConstantPointerNull>(EndPtr)) {
782 // With a null EndPtr, this function won't capture the main argument.
783 // It would be readonly too, except that it still may write to errno.
784 CI->addAttribute(1, Attributes::get(Callee->getContext(),
785 Attributes::NoCapture));
792 struct StrSpnOpt : public LibCallOptimization {
793 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
794 FunctionType *FT = Callee->getFunctionType();
795 if (FT->getNumParams() != 2 ||
796 FT->getParamType(0) != B.getInt8PtrTy() ||
797 FT->getParamType(1) != FT->getParamType(0) ||
798 !FT->getReturnType()->isIntegerTy())
802 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
803 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
805 // strspn(s, "") -> 0
806 // strspn("", s) -> 0
807 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
808 return Constant::getNullValue(CI->getType());
811 if (HasS1 && HasS2) {
812 size_t Pos = S1.find_first_not_of(S2);
813 if (Pos == StringRef::npos) Pos = S1.size();
814 return ConstantInt::get(CI->getType(), Pos);
821 struct StrCSpnOpt : public LibCallOptimization {
822 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
823 FunctionType *FT = Callee->getFunctionType();
824 if (FT->getNumParams() != 2 ||
825 FT->getParamType(0) != B.getInt8PtrTy() ||
826 FT->getParamType(1) != FT->getParamType(0) ||
827 !FT->getReturnType()->isIntegerTy())
831 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
832 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
834 // strcspn("", s) -> 0
835 if (HasS1 && S1.empty())
836 return Constant::getNullValue(CI->getType());
839 if (HasS1 && HasS2) {
840 size_t Pos = S1.find_first_of(S2);
841 if (Pos == StringRef::npos) Pos = S1.size();
842 return ConstantInt::get(CI->getType(), Pos);
845 // strcspn(s, "") -> strlen(s)
846 if (TD && HasS2 && S2.empty())
847 return EmitStrLen(CI->getArgOperand(0), B, TD, TLI);
853 struct StrStrOpt : public LibCallOptimization {
854 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
855 FunctionType *FT = Callee->getFunctionType();
856 if (FT->getNumParams() != 2 ||
857 !FT->getParamType(0)->isPointerTy() ||
858 !FT->getParamType(1)->isPointerTy() ||
859 !FT->getReturnType()->isPointerTy())
862 // fold strstr(x, x) -> x.
863 if (CI->getArgOperand(0) == CI->getArgOperand(1))
864 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
866 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
867 if (TD && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
868 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD, TLI);
871 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
875 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
877 ICmpInst *Old = cast<ICmpInst>(*UI++);
878 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
879 ConstantInt::getNullValue(StrNCmp->getType()),
881 LCS->replaceAllUsesWith(Old, Cmp);
886 // See if either input string is a constant string.
887 StringRef SearchStr, ToFindStr;
888 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
889 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
891 // fold strstr(x, "") -> x.
892 if (HasStr2 && ToFindStr.empty())
893 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
895 // If both strings are known, constant fold it.
896 if (HasStr1 && HasStr2) {
897 std::string::size_type Offset = SearchStr.find(ToFindStr);
899 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
900 return Constant::getNullValue(CI->getType());
902 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
903 Value *Result = CastToCStr(CI->getArgOperand(0), B);
904 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
905 return B.CreateBitCast(Result, CI->getType());
908 // fold strstr(x, "y") -> strchr(x, 'y').
909 if (HasStr2 && ToFindStr.size() == 1) {
910 Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TD, TLI);
911 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0;
917 struct MemCmpOpt : public LibCallOptimization {
918 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
919 FunctionType *FT = Callee->getFunctionType();
920 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
921 !FT->getParamType(1)->isPointerTy() ||
922 !FT->getReturnType()->isIntegerTy(32))
925 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
927 if (LHS == RHS) // memcmp(s,s,x) -> 0
928 return Constant::getNullValue(CI->getType());
930 // Make sure we have a constant length.
931 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
933 uint64_t Len = LenC->getZExtValue();
935 if (Len == 0) // memcmp(s1,s2,0) -> 0
936 return Constant::getNullValue(CI->getType());
938 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
940 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
941 CI->getType(), "lhsv");
942 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
943 CI->getType(), "rhsv");
944 return B.CreateSub(LHSV, RHSV, "chardiff");
947 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
948 StringRef LHSStr, RHSStr;
949 if (getConstantStringInfo(LHS, LHSStr) &&
950 getConstantStringInfo(RHS, RHSStr)) {
951 // Make sure we're not reading out-of-bounds memory.
952 if (Len > LHSStr.size() || Len > RHSStr.size())
954 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
955 return ConstantInt::get(CI->getType(), Ret);
962 struct MemCpyOpt : public LibCallOptimization {
963 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
964 // These optimizations require DataLayout.
967 FunctionType *FT = Callee->getFunctionType();
968 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
969 !FT->getParamType(0)->isPointerTy() ||
970 !FT->getParamType(1)->isPointerTy() ||
971 FT->getParamType(2) != TD->getIntPtrType(*Context))
974 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
975 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
976 CI->getArgOperand(2), 1);
977 return CI->getArgOperand(0);
981 struct MemMoveOpt : public LibCallOptimization {
982 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
983 // These optimizations require DataLayout.
986 FunctionType *FT = Callee->getFunctionType();
987 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
988 !FT->getParamType(0)->isPointerTy() ||
989 !FT->getParamType(1)->isPointerTy() ||
990 FT->getParamType(2) != TD->getIntPtrType(*Context))
993 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
994 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
995 CI->getArgOperand(2), 1);
996 return CI->getArgOperand(0);
1000 struct MemSetOpt : public LibCallOptimization {
1001 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1002 // These optimizations require DataLayout.
1005 FunctionType *FT = Callee->getFunctionType();
1006 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1007 !FT->getParamType(0)->isPointerTy() ||
1008 !FT->getParamType(1)->isIntegerTy() ||
1009 FT->getParamType(2) != TD->getIntPtrType(*Context))
1012 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
1013 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1014 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
1015 return CI->getArgOperand(0);
1019 } // End anonymous namespace.
1023 class LibCallSimplifierImpl {
1024 const DataLayout *TD;
1025 const TargetLibraryInfo *TLI;
1026 const LibCallSimplifier *LCS;
1027 StringMap<LibCallOptimization*> Optimizations;
1029 // Fortified library call optimizations.
1030 MemCpyChkOpt MemCpyChk;
1031 MemMoveChkOpt MemMoveChk;
1032 MemSetChkOpt MemSetChk;
1033 StrCpyChkOpt StrCpyChk;
1034 StpCpyChkOpt StpCpyChk;
1035 StrNCpyChkOpt StrNCpyChk;
1037 // String library call optimizations.
1054 // Memory library call optimizations.
1060 void initOptimizations();
1061 void addOpt(LibFunc::Func F, LibCallOptimization* Opt);
1063 LibCallSimplifierImpl(const DataLayout *TD, const TargetLibraryInfo *TLI,
1064 const LibCallSimplifier *LCS) {
1070 Value *optimizeCall(CallInst *CI);
1073 void LibCallSimplifierImpl::initOptimizations() {
1074 // Fortified library call optimizations.
1075 Optimizations["__memcpy_chk"] = &MemCpyChk;
1076 Optimizations["__memmove_chk"] = &MemMoveChk;
1077 Optimizations["__memset_chk"] = &MemSetChk;
1078 Optimizations["__strcpy_chk"] = &StrCpyChk;
1079 Optimizations["__stpcpy_chk"] = &StpCpyChk;
1080 Optimizations["__strncpy_chk"] = &StrNCpyChk;
1081 Optimizations["__stpncpy_chk"] = &StrNCpyChk;
1083 // String library call optimizations.
1084 addOpt(LibFunc::strcat, &StrCat);
1085 addOpt(LibFunc::strncat, &StrNCat);
1086 addOpt(LibFunc::strchr, &StrChr);
1087 addOpt(LibFunc::strrchr, &StrRChr);
1088 addOpt(LibFunc::strcmp, &StrCmp);
1089 addOpt(LibFunc::strncmp, &StrNCmp);
1090 addOpt(LibFunc::strcpy, &StrCpy);
1091 addOpt(LibFunc::stpcpy, &StpCpy);
1092 addOpt(LibFunc::strncpy, &StrNCpy);
1093 addOpt(LibFunc::strlen, &StrLen);
1094 addOpt(LibFunc::strpbrk, &StrPBrk);
1095 addOpt(LibFunc::strtol, &StrTo);
1096 addOpt(LibFunc::strtod, &StrTo);
1097 addOpt(LibFunc::strtof, &StrTo);
1098 addOpt(LibFunc::strtoul, &StrTo);
1099 addOpt(LibFunc::strtoll, &StrTo);
1100 addOpt(LibFunc::strtold, &StrTo);
1101 addOpt(LibFunc::strtoull, &StrTo);
1102 addOpt(LibFunc::strspn, &StrSpn);
1103 addOpt(LibFunc::strcspn, &StrCSpn);
1104 addOpt(LibFunc::strstr, &StrStr);
1106 // Memory library call optimizations.
1107 addOpt(LibFunc::memcmp, &MemCmp);
1108 addOpt(LibFunc::memcpy, &MemCpy);
1109 addOpt(LibFunc::memmove, &MemMove);
1110 addOpt(LibFunc::memset, &MemSet);
1113 Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {
1114 if (Optimizations.empty())
1115 initOptimizations();
1117 Function *Callee = CI->getCalledFunction();
1118 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1120 IRBuilder<> Builder(CI);
1121 return LCO->optimizeCall(CI, TD, TLI, LCS, Builder);
1126 void LibCallSimplifierImpl::addOpt(LibFunc::Func F, LibCallOptimization* Opt) {
1128 Optimizations[TLI->getName(F)] = Opt;
1131 LibCallSimplifier::LibCallSimplifier(const DataLayout *TD,
1132 const TargetLibraryInfo *TLI) {
1133 Impl = new LibCallSimplifierImpl(TD, TLI, this);
1136 LibCallSimplifier::~LibCallSimplifier() {
1140 Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
1141 return Impl->optimizeCall(CI);
1144 void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const {
1145 I->replaceAllUsesWith(With);
1146 I->eraseFromParent();