[llvm-commits] [llvm] r86141 - in /llvm/trunk: lib/Transforms/Scalar/SimplifyLibCalls.cpp test/Transforms/SimplifyLibCalls/memcmp.ll

Benjamin Kramer benny.kra at googlemail.com
Thu Nov 5 09:44:23 PST 2009


Author: d0k
Date: Thu Nov  5 11:44:22 2009
New Revision: 86141

URL: http://llvm.org/viewvc/llvm-project?rev=86141&view=rev
Log:
Teach SimplifyLibCalls to fold memcmp calls with constant arguments.

Modified:
    llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp
    llvm/trunk/test/Transforms/SimplifyLibCalls/memcmp.ll

Modified: llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp?rev=86141&r1=86140&r2=86141&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp Thu Nov  5 11:44:22 2009
@@ -955,6 +955,17 @@
       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 @@
 // 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)

Modified: llvm/trunk/test/Transforms/SimplifyLibCalls/memcmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyLibCalls/memcmp.ll?rev=86141&r1=86140&r2=86141&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/SimplifyLibCalls/memcmp.ll (original)
+++ llvm/trunk/test/Transforms/SimplifyLibCalls/memcmp.ll Thu Nov  5 11:44:22 2009
@@ -17,6 +17,10 @@
 	%D = call i32 @memcmp( i8* %P, i8* %Q, i32 2 )		; <i32> [#uses=1]
 	%E = icmp eq i32 %D, 0		; <i1> [#uses=1]
 	volatile store i1 %E, i1* %BP
+        %F = call i32 @memcmp(i8* getelementptr ([4 x i8]* @hel, i32 0, i32 0),
+                              i8* getelementptr ([8 x i8]* @hello_u, i32 0, i32 0),
+                              i32 3)
+        volatile store i32 %F, i32* %IP
 	ret void
 }
 





More information about the llvm-commits mailing list