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;
39 LibCallOptimization() { }
40 virtual ~LibCallOptimization() {}
42 /// callOptimizer - This pure virtual method is implemented by base classes to
43 /// do various optimizations. If this returns null then no transformation was
44 /// performed. If it returns CI, then it transformed the call and CI is to be
45 /// deleted. If it returns something else, replace CI with the new value and
47 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
50 Value *optimizeCall(CallInst *CI, const DataLayout *TD,
51 const TargetLibraryInfo *TLI, IRBuilder<> &B) {
52 Caller = CI->getParent()->getParent();
55 if (CI->getCalledFunction())
56 Context = &CI->getCalledFunction()->getContext();
58 // We never change the calling convention.
59 if (CI->getCallingConv() != llvm::CallingConv::C)
62 return callOptimizer(CI->getCalledFunction(), CI, B);
66 //===----------------------------------------------------------------------===//
68 //===----------------------------------------------------------------------===//
70 /// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
71 /// value is equal or not-equal to zero.
72 static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
73 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
75 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
77 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
80 // Unknown instruction.
86 //===----------------------------------------------------------------------===//
87 // Fortified Library Call Optimizations
88 //===----------------------------------------------------------------------===//
90 struct FortifiedLibCallOptimization : public LibCallOptimization {
92 virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
93 bool isString) const = 0;
96 struct InstFortifiedLibCallOptimization : public FortifiedLibCallOptimization {
99 bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const {
100 if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
102 if (ConstantInt *SizeCI =
103 dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
104 if (SizeCI->isAllOnesValue())
107 uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
108 // If the length is 0 we don't know how long it is and so we can't
110 if (Len == 0) return false;
111 return SizeCI->getZExtValue() >= Len;
113 if (ConstantInt *Arg = dyn_cast<ConstantInt>(
114 CI->getArgOperand(SizeArgOp)))
115 return SizeCI->getZExtValue() >= Arg->getZExtValue();
121 struct MemCpyChkOpt : public InstFortifiedLibCallOptimization {
122 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
124 FunctionType *FT = Callee->getFunctionType();
126 // Check if this has the right signature.
127 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
128 !FT->getParamType(0)->isPointerTy() ||
129 !FT->getParamType(1)->isPointerTy() ||
130 FT->getParamType(2) != TD->getIntPtrType(FT->getParamType(0)) ||
131 FT->getParamType(3) != TD->getIntPtrType(FT->getParamType(1)))
134 if (isFoldable(3, 2, false)) {
135 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
136 CI->getArgOperand(2), 1);
137 return CI->getArgOperand(0);
143 struct MemMoveChkOpt : public InstFortifiedLibCallOptimization {
144 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
146 FunctionType *FT = Callee->getFunctionType();
148 // Check if this has the right signature.
149 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
150 !FT->getParamType(0)->isPointerTy() ||
151 !FT->getParamType(1)->isPointerTy() ||
152 FT->getParamType(2) != TD->getIntPtrType(FT->getParamType(0)) ||
153 FT->getParamType(3) != TD->getIntPtrType(FT->getParamType(1)))
156 if (isFoldable(3, 2, false)) {
157 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
158 CI->getArgOperand(2), 1);
159 return CI->getArgOperand(0);
165 struct MemSetChkOpt : public InstFortifiedLibCallOptimization {
166 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
168 FunctionType *FT = Callee->getFunctionType();
170 // Check if this has the right signature.
171 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
172 !FT->getParamType(0)->isPointerTy() ||
173 !FT->getParamType(1)->isIntegerTy() ||
174 FT->getParamType(2) != TD->getIntPtrType(FT->getParamType(0)) ||
175 FT->getParamType(3) != TD->getIntPtrType(FT->getParamType(0)))
178 if (isFoldable(3, 2, false)) {
179 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
181 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
182 return CI->getArgOperand(0);
188 struct StrCpyChkOpt : public InstFortifiedLibCallOptimization {
189 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
191 StringRef Name = Callee->getName();
192 FunctionType *FT = Callee->getFunctionType();
193 LLVMContext &Context = CI->getParent()->getContext();
195 // Check if this has the right signature.
196 if (FT->getNumParams() != 3 ||
197 FT->getReturnType() != FT->getParamType(0) ||
198 FT->getParamType(0) != FT->getParamType(1) ||
199 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
200 FT->getParamType(2) != TD->getIntPtrType(FT->getParamType(0)))
203 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
204 if (Dst == Src) // __strcpy_chk(x,x) -> x
207 // If a) we don't have any length information, or b) we know this will
208 // fit then just lower to a plain strcpy. Otherwise we'll keep our
209 // strcpy_chk call which may fail at runtime if the size is too long.
210 // TODO: It might be nice to get a maximum length out of the possible
211 // string lengths for varying.
212 if (isFoldable(2, 1, true)) {
213 Value *Ret = EmitStrCpy(Dst, Src, B, TD, TLI, Name.substr(2, 6));
216 // Maybe we can stil fold __strcpy_chk to __memcpy_chk.
217 uint64_t Len = GetStringLength(Src);
218 if (Len == 0) return 0;
220 // This optimization require DataLayout.
224 EmitMemCpyChk(Dst, Src,
225 ConstantInt::get(TD->getIntPtrType(Dst->getType()),
226 Len), CI->getArgOperand(2), B, TD, TLI);
233 struct StpCpyChkOpt : public InstFortifiedLibCallOptimization {
234 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
236 StringRef Name = Callee->getName();
237 FunctionType *FT = Callee->getFunctionType();
238 LLVMContext &Context = CI->getParent()->getContext();
240 // Check if this has the right signature.
241 if (FT->getNumParams() != 3 ||
242 FT->getReturnType() != FT->getParamType(0) ||
243 FT->getParamType(0) != FT->getParamType(1) ||
244 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
245 FT->getParamType(2) != TD->getIntPtrType(FT->getParamType(0)))
248 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
249 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
250 Value *StrLen = EmitStrLen(Src, B, TD, TLI);
251 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
254 // If a) we don't have any length information, or b) we know this will
255 // fit then just lower to a plain stpcpy. Otherwise we'll keep our
256 // stpcpy_chk call which may fail at runtime if the size is too long.
257 // TODO: It might be nice to get a maximum length out of the possible
258 // string lengths for varying.
259 if (isFoldable(2, 1, true)) {
260 Value *Ret = EmitStrCpy(Dst, Src, B, TD, TLI, Name.substr(2, 6));
263 // Maybe we can stil fold __stpcpy_chk to __memcpy_chk.
264 uint64_t Len = GetStringLength(Src);
265 if (Len == 0) return 0;
267 // This optimization require DataLayout.
270 Type *PT = FT->getParamType(0);
271 Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
272 Value *DstEnd = B.CreateGEP(Dst,
273 ConstantInt::get(TD->getIntPtrType(PT),
275 if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, TD, TLI))
283 struct StrNCpyChkOpt : public InstFortifiedLibCallOptimization {
284 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
286 StringRef Name = Callee->getName();
287 FunctionType *FT = Callee->getFunctionType();
288 LLVMContext &Context = CI->getParent()->getContext();
290 // Check if this has the right signature.
291 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
292 FT->getParamType(0) != FT->getParamType(1) ||
293 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
294 !FT->getParamType(2)->isIntegerTy() ||
295 FT->getParamType(3) != TD->getIntPtrType(FT->getParamType(0)))
298 if (isFoldable(3, 2, false)) {
299 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
300 CI->getArgOperand(2), B, TD, TLI,
308 //===----------------------------------------------------------------------===//
309 // String and Memory Library Call Optimizations
310 //===----------------------------------------------------------------------===//
312 struct StrCatOpt : public LibCallOptimization {
313 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
314 // Verify the "strcat" function prototype.
315 FunctionType *FT = Callee->getFunctionType();
316 if (FT->getNumParams() != 2 ||
317 FT->getReturnType() != B.getInt8PtrTy() ||
318 FT->getParamType(0) != FT->getReturnType() ||
319 FT->getParamType(1) != FT->getReturnType())
322 // Extract some information from the instruction
323 Value *Dst = CI->getArgOperand(0);
324 Value *Src = CI->getArgOperand(1);
326 // See if we can get the length of the input string.
327 uint64_t Len = GetStringLength(Src);
328 if (Len == 0) return 0;
329 --Len; // Unbias length.
331 // Handle the simple, do-nothing case: strcat(x, "") -> x
335 // These optimizations require DataLayout.
338 return emitStrLenMemCpy(Src, Dst, Len, B);
341 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
343 // We need to find the end of the destination string. That's where the
344 // memory is to be moved to. We just generate a call to strlen.
345 Value *DstLen = EmitStrLen(Dst, B, TD, TLI);
349 // Now that we have the destination's length, we must index into the
350 // destination's pointer to get the actual memcpy destination (end of
351 // the string .. we're concatenating).
352 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
354 // We have enough information to now generate the memcpy call to do the
355 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
356 B.CreateMemCpy(CpyDst, Src,
357 ConstantInt::get(TD->getIntPtrType(Src->getType()),
363 struct StrNCatOpt : public StrCatOpt {
364 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
365 // Verify the "strncat" function prototype.
366 FunctionType *FT = Callee->getFunctionType();
367 if (FT->getNumParams() != 3 ||
368 FT->getReturnType() != B.getInt8PtrTy() ||
369 FT->getParamType(0) != FT->getReturnType() ||
370 FT->getParamType(1) != FT->getReturnType() ||
371 !FT->getParamType(2)->isIntegerTy())
374 // Extract some information from the instruction
375 Value *Dst = CI->getArgOperand(0);
376 Value *Src = CI->getArgOperand(1);
379 // We don't do anything if length is not constant
380 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
381 Len = LengthArg->getZExtValue();
385 // See if we can get the length of the input string.
386 uint64_t SrcLen = GetStringLength(Src);
387 if (SrcLen == 0) return 0;
388 --SrcLen; // Unbias length.
390 // Handle the simple, do-nothing cases:
391 // strncat(x, "", c) -> x
392 // strncat(x, c, 0) -> x
393 if (SrcLen == 0 || Len == 0) return Dst;
395 // These optimizations require DataLayout.
398 // We don't optimize this case
399 if (Len < SrcLen) return 0;
401 // strncat(x, s, c) -> strcat(x, s)
402 // s is constant so the strcat can be optimized further
403 return emitStrLenMemCpy(Src, Dst, SrcLen, B);
407 struct StrChrOpt : public LibCallOptimization {
408 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
409 // Verify the "strchr" function prototype.
410 FunctionType *FT = Callee->getFunctionType();
411 if (FT->getNumParams() != 2 ||
412 FT->getReturnType() != B.getInt8PtrTy() ||
413 FT->getParamType(0) != FT->getReturnType() ||
414 !FT->getParamType(1)->isIntegerTy(32))
417 Value *SrcStr = CI->getArgOperand(0);
419 // If the second operand is non-constant, see if we can compute the length
420 // of the input string and turn this into memchr.
421 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
423 // These optimizations require DataLayout.
426 uint64_t Len = GetStringLength(SrcStr);
427 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
430 Type *PT = FT->getParamType(0);
431 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
432 ConstantInt::get(TD->getIntPtrType(PT), Len),
436 // Otherwise, the character is a constant, see if the first argument is
437 // a string literal. If so, we can constant fold.
439 if (!getConstantStringInfo(SrcStr, Str))
442 // Compute the offset, make sure to handle the case when we're searching for
443 // zero (a weird way to spell strlen).
444 size_t I = CharC->getSExtValue() == 0 ?
445 Str.size() : Str.find(CharC->getSExtValue());
446 if (I == StringRef::npos) // Didn't find the char. strchr returns null.
447 return Constant::getNullValue(CI->getType());
449 // strchr(s+n,c) -> gep(s+n+i,c)
450 return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
454 struct StrRChrOpt : public LibCallOptimization {
455 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
456 // Verify the "strrchr" function prototype.
457 FunctionType *FT = Callee->getFunctionType();
458 if (FT->getNumParams() != 2 ||
459 FT->getReturnType() != B.getInt8PtrTy() ||
460 FT->getParamType(0) != FT->getReturnType() ||
461 !FT->getParamType(1)->isIntegerTy(32))
464 Value *SrcStr = CI->getArgOperand(0);
465 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
467 // Cannot fold anything if we're not looking for a constant.
472 if (!getConstantStringInfo(SrcStr, Str)) {
473 // strrchr(s, 0) -> strchr(s, 0)
474 if (TD && CharC->isZero())
475 return EmitStrChr(SrcStr, '\0', B, TD, TLI);
479 // Compute the offset.
480 size_t I = CharC->getSExtValue() == 0 ?
481 Str.size() : Str.rfind(CharC->getSExtValue());
482 if (I == StringRef::npos) // Didn't find the char. Return null.
483 return Constant::getNullValue(CI->getType());
485 // strrchr(s+n,c) -> gep(s+n+i,c)
486 return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
490 struct StrCmpOpt : public LibCallOptimization {
491 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
492 // Verify the "strcmp" function prototype.
493 FunctionType *FT = Callee->getFunctionType();
494 if (FT->getNumParams() != 2 ||
495 !FT->getReturnType()->isIntegerTy(32) ||
496 FT->getParamType(0) != FT->getParamType(1) ||
497 FT->getParamType(0) != B.getInt8PtrTy())
500 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
501 if (Str1P == Str2P) // strcmp(x,x) -> 0
502 return ConstantInt::get(CI->getType(), 0);
504 StringRef Str1, Str2;
505 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
506 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
508 // strcmp(x, y) -> cnst (if both x and y are constant strings)
509 if (HasStr1 && HasStr2)
510 return ConstantInt::get(CI->getType(), Str1.compare(Str2));
512 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
513 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
516 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
517 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
519 // strcmp(P, "x") -> memcmp(P, "x", 2)
520 uint64_t Len1 = GetStringLength(Str1P);
521 uint64_t Len2 = GetStringLength(Str2P);
523 // These optimizations require DataLayout.
526 Type *PT = FT->getParamType(0);
527 return EmitMemCmp(Str1P, Str2P,
528 ConstantInt::get(TD->getIntPtrType(PT),
529 std::min(Len1, Len2)), B, TD, TLI);
536 struct StrNCmpOpt : public LibCallOptimization {
537 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
538 // Verify the "strncmp" function prototype.
539 FunctionType *FT = Callee->getFunctionType();
540 if (FT->getNumParams() != 3 ||
541 !FT->getReturnType()->isIntegerTy(32) ||
542 FT->getParamType(0) != FT->getParamType(1) ||
543 FT->getParamType(0) != B.getInt8PtrTy() ||
544 !FT->getParamType(2)->isIntegerTy())
547 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
548 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
549 return ConstantInt::get(CI->getType(), 0);
551 // Get the length argument if it is constant.
553 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
554 Length = LengthArg->getZExtValue();
558 if (Length == 0) // strncmp(x,y,0) -> 0
559 return ConstantInt::get(CI->getType(), 0);
561 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
562 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD, TLI);
564 StringRef Str1, Str2;
565 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
566 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
568 // strncmp(x, y) -> cnst (if both x and y are constant strings)
569 if (HasStr1 && HasStr2) {
570 StringRef SubStr1 = Str1.substr(0, Length);
571 StringRef SubStr2 = Str2.substr(0, Length);
572 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
575 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
576 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
579 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
580 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
586 struct StrCpyOpt : public LibCallOptimization {
587 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
588 // Verify the "strcpy" function prototype.
589 FunctionType *FT = Callee->getFunctionType();
590 if (FT->getNumParams() != 2 ||
591 FT->getReturnType() != FT->getParamType(0) ||
592 FT->getParamType(0) != FT->getParamType(1) ||
593 FT->getParamType(0) != B.getInt8PtrTy())
596 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
597 if (Dst == Src) // strcpy(x,x) -> x
600 // These optimizations require DataLayout.
603 // See if we can get the length of the input string.
604 uint64_t Len = GetStringLength(Src);
605 if (Len == 0) return 0;
607 // We have enough information to now generate the memcpy call to do the
608 // copy for us. Make a memcpy to copy the nul byte with align = 1.
609 B.CreateMemCpy(Dst, Src,
610 ConstantInt::get(TD->getIntPtrType(Dst->getType()), Len), 1);
615 struct StpCpyOpt: public LibCallOptimization {
616 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
617 // Verify the "stpcpy" function prototype.
618 FunctionType *FT = Callee->getFunctionType();
619 if (FT->getNumParams() != 2 ||
620 FT->getReturnType() != FT->getParamType(0) ||
621 FT->getParamType(0) != FT->getParamType(1) ||
622 FT->getParamType(0) != B.getInt8PtrTy())
625 // These optimizations require DataLayout.
628 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
629 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
630 Value *StrLen = EmitStrLen(Src, B, TD, TLI);
631 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
634 // See if we can get the length of the input string.
635 uint64_t Len = GetStringLength(Src);
636 if (Len == 0) return 0;
638 Type *PT = FT->getParamType(0);
639 Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
640 Value *DstEnd = B.CreateGEP(Dst,
641 ConstantInt::get(TD->getIntPtrType(PT),
644 // We have enough information to now generate the memcpy call to do the
645 // copy for us. Make a memcpy to copy the nul byte with align = 1.
646 B.CreateMemCpy(Dst, Src, LenV, 1);
651 struct StrNCpyOpt : public LibCallOptimization {
652 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
653 FunctionType *FT = Callee->getFunctionType();
654 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
655 FT->getParamType(0) != FT->getParamType(1) ||
656 FT->getParamType(0) != B.getInt8PtrTy() ||
657 !FT->getParamType(2)->isIntegerTy())
660 Value *Dst = CI->getArgOperand(0);
661 Value *Src = CI->getArgOperand(1);
662 Value *LenOp = CI->getArgOperand(2);
664 // See if we can get the length of the input string.
665 uint64_t SrcLen = GetStringLength(Src);
666 if (SrcLen == 0) return 0;
670 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
671 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
676 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
677 Len = LengthArg->getZExtValue();
681 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
683 // These optimizations require DataLayout.
686 // Let strncpy handle the zero padding
687 if (Len > SrcLen+1) return 0;
689 Type *PT = FT->getParamType(0);
690 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
691 B.CreateMemCpy(Dst, Src,
692 ConstantInt::get(TD->getIntPtrType(PT), Len), 1);
698 struct StrLenOpt : public LibCallOptimization {
699 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
700 FunctionType *FT = Callee->getFunctionType();
701 if (FT->getNumParams() != 1 ||
702 FT->getParamType(0) != B.getInt8PtrTy() ||
703 !FT->getReturnType()->isIntegerTy())
706 Value *Src = CI->getArgOperand(0);
708 // Constant folding: strlen("xyz") -> 3
709 if (uint64_t Len = GetStringLength(Src))
710 return ConstantInt::get(CI->getType(), Len-1);
712 // strlen(x) != 0 --> *x != 0
713 // strlen(x) == 0 --> *x == 0
714 if (isOnlyUsedInZeroEqualityComparison(CI))
715 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
720 struct StrPBrkOpt : public LibCallOptimization {
721 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
722 FunctionType *FT = Callee->getFunctionType();
723 if (FT->getNumParams() != 2 ||
724 FT->getParamType(0) != B.getInt8PtrTy() ||
725 FT->getParamType(1) != FT->getParamType(0) ||
726 FT->getReturnType() != FT->getParamType(0))
730 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
731 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
733 // strpbrk(s, "") -> NULL
734 // strpbrk("", s) -> NULL
735 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
736 return Constant::getNullValue(CI->getType());
739 if (HasS1 && HasS2) {
740 size_t I = S1.find_first_of(S2);
741 if (I == std::string::npos) // No match.
742 return Constant::getNullValue(CI->getType());
744 return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
747 // strpbrk(s, "a") -> strchr(s, 'a')
748 if (TD && HasS2 && S2.size() == 1)
749 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD, TLI);
755 struct StrToOpt : public LibCallOptimization {
756 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
757 FunctionType *FT = Callee->getFunctionType();
758 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
759 !FT->getParamType(0)->isPointerTy() ||
760 !FT->getParamType(1)->isPointerTy())
763 Value *EndPtr = CI->getArgOperand(1);
764 if (isa<ConstantPointerNull>(EndPtr)) {
765 // With a null EndPtr, this function won't capture the main argument.
766 // It would be readonly too, except that it still may write to errno.
767 CI->addAttribute(1, Attributes::get(Callee->getContext(),
768 Attributes::NoCapture));
775 } // End anonymous namespace.
779 class LibCallSimplifierImpl {
780 const DataLayout *TD;
781 const TargetLibraryInfo *TLI;
782 StringMap<LibCallOptimization*> Optimizations;
784 // Fortified library call optimizations.
785 MemCpyChkOpt MemCpyChk;
786 MemMoveChkOpt MemMoveChk;
787 MemSetChkOpt MemSetChk;
788 StrCpyChkOpt StrCpyChk;
789 StpCpyChkOpt StpCpyChk;
790 StrNCpyChkOpt StrNCpyChk;
792 // String and memory library call optimizations.
806 void initOptimizations();
808 LibCallSimplifierImpl(const DataLayout *TD, const TargetLibraryInfo *TLI) {
813 Value *optimizeCall(CallInst *CI);
816 void LibCallSimplifierImpl::initOptimizations() {
817 // Fortified library call optimizations.
818 Optimizations["__memcpy_chk"] = &MemCpyChk;
819 Optimizations["__memmove_chk"] = &MemMoveChk;
820 Optimizations["__memset_chk"] = &MemSetChk;
821 Optimizations["__strcpy_chk"] = &StrCpyChk;
822 Optimizations["__stpcpy_chk"] = &StpCpyChk;
823 Optimizations["__strncpy_chk"] = &StrNCpyChk;
824 Optimizations["__stpncpy_chk"] = &StrNCpyChk;
826 // String and memory library call optimizations.
827 Optimizations["strcat"] = &StrCat;
828 Optimizations["strncat"] = &StrNCat;
829 Optimizations["strchr"] = &StrChr;
830 Optimizations["strrchr"] = &StrRChr;
831 Optimizations["strcmp"] = &StrCmp;
832 Optimizations["strncmp"] = &StrNCmp;
833 Optimizations["strcpy"] = &StrCpy;
834 Optimizations["stpcpy"] = &StpCpy;
835 Optimizations["strncpy"] = &StrNCpy;
836 Optimizations["strlen"] = &StrLen;
837 Optimizations["strpbrk"] = &StrPBrk;
838 Optimizations["strtol"] = &StrTo;
839 Optimizations["strtod"] = &StrTo;
840 Optimizations["strtof"] = &StrTo;
841 Optimizations["strtoul"] = &StrTo;
842 Optimizations["strtoll"] = &StrTo;
843 Optimizations["strtold"] = &StrTo;
844 Optimizations["strtoull"] = &StrTo;
847 Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {
848 if (Optimizations.empty())
851 Function *Callee = CI->getCalledFunction();
852 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
854 IRBuilder<> Builder(CI);
855 return LCO->optimizeCall(CI, TD, TLI, Builder);
860 LibCallSimplifier::LibCallSimplifier(const DataLayout *TD,
861 const TargetLibraryInfo *TLI) {
862 Impl = new LibCallSimplifierImpl(TD, TLI);
865 LibCallSimplifier::~LibCallSimplifier() {
869 Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
870 return Impl->optimizeCall(CI);