diff options
| author | Benjamin Kramer <benny.kra@googlemail.com> | 2009-11-05 17:44:22 +0000 | 
|---|---|---|
| committer | Benjamin Kramer <benny.kra@googlemail.com> | 2009-11-05 17:44:22 +0000 | 
| commit | b971445ab7f8369cf8a3b6b59d5ab6259ef51ae1 (patch) | |
| tree | aa0ff08b1a0fbcf0b251de513951d772502fcc99 /llvm/lib/Transforms | |
| parent | 65ae200a131d0ec00d9a7f7979102305059cef38 (diff) | |
| download | bcm5719-llvm-b971445ab7f8369cf8a3b6b59d5ab6259ef51ae1.tar.gz bcm5719-llvm-b971445ab7f8369cf8a3b6b59d5ab6259ef51ae1.zip  | |
Teach SimplifyLibCalls to fold memcmp calls with constant arguments.
llvm-svn: 86141
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp | 15 | 
1 files changed, 11 insertions, 4 deletions
diff --git a/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp index 575c93b9dd3..ecca3c73b90 100644 --- a/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp @@ -955,6 +955,17 @@ struct MemCmpOpt : public LibCallOptimization {        return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());      } +    // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant) +    std::string LHSStr, RHSStr; +    if (GetConstantStringInfo(LHS, LHSStr) && +        GetConstantStringInfo(RHS, RHSStr)) { +      // Make sure we're not reading out-of-bounds memory. +      if (Len > LHSStr.length() || Len > RHSStr.length()) +        return 0; +      uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len); +      return ConstantInt::get(CI->getType(), Ret); +    } +      return 0;    }  }; @@ -2479,10 +2490,6 @@ bool SimplifyLibCalls::doInitialization(Module &M) {  // lround, lroundf, lroundl:  //   * lround(cnst) -> cnst'  // -// memcmp: -//   * memcmp(x,y,l)   -> cnst -//      (if all arguments are constant and strlen(x) <= l and strlen(y) <= l) -//  // pow, powf, powl:  //   * pow(exp(x),y)  -> exp(x*y)  //   * pow(sqrt(x),y) -> pow(x,y*0.5)  | 

