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

Duncan P. N. Exon Smith dexonsmith at apple.com
Sat Feb 1 18:26:52 PST 2014


LGTM, with some style fix-ups:

> diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp
> index 36d2462..7e1abf1 100644
> --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp
> +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp
> @@ -493,8 +493,12 @@ struct StrChrOpt : public LibCallOptimization {
>      // 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)

Remove the braces on the inner if.

> +        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 +2301,6 @@ void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const {
>  //   * 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
>  //
> diff --git a/test/Transforms/InstCombine/strchr-1.ll b/test/Transforms/InstCombine/strchr-1.ll
> index d2c9894..f8860e3 100644
> --- a/test/Transforms/InstCombine/strchr-1.ll
> +++ b/test/Transforms/InstCombine/strchr-1.ll
> @@ -63,3 +63,15 @@ define void @test_simplify5() {
>    store i8* %dst, i8** @chp
>    ret void
>  }
> +
> +define void @test_simplify6(i8* %str) {

I think you should add a comment along the lines of:

  ; strchr(p, 0) -> p + strlen(p)

> +; 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
> +}


On 2014 Feb 1, at 15:07, Kai Nacke <kai.nacke at redstar.de> wrote:

> Hi everyone!
> 
> This patch adds the transformation strchr(p, 0) -> p + strlen(p) to SimplifyLibCalls and removes the ToDo comment.
> 
> Please review.
> 
> Regards,
> Kai
> <strchr.diff>_______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list