diff options
Diffstat (limited to 'lib/Transforms/Utils/SimplifyLibCalls.cpp')
| -rw-r--r-- | lib/Transforms/Utils/SimplifyLibCalls.cpp | 80 | 
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index e3e9266e52..b5ea46dc0d 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -86,6 +86,20 @@ static bool isOnlyUsedInZeroEqualityComparison(Value *V) {    return true;  } +/// isOnlyUsedInEqualityComparison - Return true if it is only used in equality +/// comparisons with With. +static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) { +  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); +       UI != E; ++UI) { +    if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI)) +      if (IC->isEquality() && IC->getOperand(1) == With) +        continue; +    // Unknown instruction. +    return false; +  } +  return true; +} +  //===----------------------------------------------------------------------===//  // Fortified Library Call Optimizations  //===----------------------------------------------------------------------===// @@ -836,6 +850,70 @@ struct StrCSpnOpt : public LibCallOptimization {    }  }; +struct StrStrOpt : public LibCallOptimization { +  virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { +    FunctionType *FT = Callee->getFunctionType(); +    if (FT->getNumParams() != 2 || +        !FT->getParamType(0)->isPointerTy() || +        !FT->getParamType(1)->isPointerTy() || +        !FT->getReturnType()->isPointerTy()) +      return 0; + +    // fold strstr(x, x) -> x. +    if (CI->getArgOperand(0) == CI->getArgOperand(1)) +      return B.CreateBitCast(CI->getArgOperand(0), CI->getType()); + +    // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0 +    if (TD && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) { +      Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD, TLI); +      if (!StrLen) +        return 0; +      Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1), +                                   StrLen, B, TD, TLI); +      if (!StrNCmp) +        return 0; +      for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end(); +           UI != UE; ) { +        ICmpInst *Old = cast<ICmpInst>(*UI++); +        Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp, +                                  ConstantInt::getNullValue(StrNCmp->getType()), +                                  "cmp"); +        LCS->replaceAllUsesWith(Old, Cmp); +      } +      return CI; +    } + +    // See if either input string is a constant string. +    StringRef SearchStr, ToFindStr; +    bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr); +    bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr); + +    // fold strstr(x, "") -> x. +    if (HasStr2 && ToFindStr.empty()) +      return B.CreateBitCast(CI->getArgOperand(0), CI->getType()); + +    // If both strings are known, constant fold it. +    if (HasStr1 && HasStr2) { +      std::string::size_type Offset = SearchStr.find(ToFindStr); + +      if (Offset == StringRef::npos) // strstr("foo", "bar") -> null +        return Constant::getNullValue(CI->getType()); + +      // strstr("abcd", "bc") -> gep((char*)"abcd", 1) +      Value *Result = CastToCStr(CI->getArgOperand(0), B); +      Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr"); +      return B.CreateBitCast(Result, CI->getType()); +    } + +    // fold strstr(x, "y") -> strchr(x, 'y'). +    if (HasStr2 && ToFindStr.size() == 1) { +      Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TD, TLI); +      return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0; +    } +    return 0; +  } +}; +  } // End anonymous namespace.  namespace llvm { @@ -869,6 +947,7 @@ class LibCallSimplifierImpl {    StrToOpt StrTo;    StrSpnOpt StrSpn;    StrCSpnOpt StrCSpn; +  StrStrOpt StrStr;    void initOptimizations();    void addOpt(LibFunc::Func F, LibCallOptimization* Opt); @@ -914,6 +993,7 @@ void LibCallSimplifierImpl::initOptimizations() {    addOpt(LibFunc::strtoull, &StrTo);    addOpt(LibFunc::strspn, &StrSpn);    addOpt(LibFunc::strcspn, &StrCSpn); +  addOpt(LibFunc::strstr, &StrStr);  }  Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {  | 
