[llvm] r200736 - Add strchr(p, 0) -> p + strlen(p) to SimplifyLibCalls

Kai Nacke kai.nacke at redstar.de
Mon Feb 3 21:55:17 PST 2014


Author: redstar
Date: Mon Feb  3 23:55:16 2014
New Revision: 200736

URL: http://llvm.org/viewvc/llvm-project?rev=200736&view=rev
Log:
Add strchr(p, 0) -> p + strlen(p) to SimplifyLibCalls

Add the missing transformation strchr(p, 0) -> p + strlen(p) to SimplifyLibCalls
and remove the ToDo comment.

Reviewer: Duncan P.N. Exan Smith

Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/trunk/test/Transforms/InstCombine/strchr-1.ll

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=200736&r1=200735&r2=200736&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Mon Feb  3 23:55:16 2014
@@ -493,8 +493,11 @@ struct StrChrOpt : public LibCallOptimiz
     // Otherwise, the character is a constant, see if the first argument is
     // a string literal.  If so, we can constant fold.
     StringRef Str;
-    if (!getConstantStringInfo(SrcStr, Str))
+    if (!getConstantStringInfo(SrcStr, Str)) {
+      if (TD && CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
+        return B.CreateGEP(SrcStr, EmitStrLen(SrcStr, B, TD, TLI), "strchr");
       return 0;
+    }
 
     // Compute the offset, make sure to handle the case when we're searching for
     // zero (a weird way to spell strlen).
@@ -2297,8 +2300,6 @@ void LibCallSimplifier::replaceAllUsesWi
 //   * sqrt(Nroot(x)) -> pow(x,1/(2*N))
 //   * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
 //
-// strchr:
-//   * strchr(p, 0) -> strlen(p)
 // tan, tanf, tanl:
 //   * tan(atan(x)) -> x
 //

Modified: llvm/trunk/test/Transforms/InstCombine/strchr-1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/strchr-1.ll?rev=200736&r1=200735&r2=200736&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/strchr-1.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/strchr-1.ll Mon Feb  3 23:55:16 2014
@@ -63,3 +63,16 @@ define void @test_simplify5() {
   store i8* %dst, i8** @chp
   ret void
 }
+
+; Check transformation strchr(p, 0) -> p + strlen(p)
+define void @test_simplify6(i8* %str) {
+; CHECK: %strlen = call i32 @strlen(i8* %str)
+; CHECK-NOT: call i8* @strchr
+; CHECK: %strchr = getelementptr i8* %str, i32 %strlen
+; CHECK: store i8* %strchr, i8** @chp, align 4
+; CHECK: ret void
+
+  %dst = call i8* @strchr(i8* %str, i32 0)
+  store i8* %dst, i8** @chp
+  ret void
+}





More information about the llvm-commits mailing list