[llvm] r207828 - Fold strlen(expr ? "str1" : "str2") to x ? len1 : len2. This fires about 330 times in a bootstrap of clang.

Benjamin Kramer benny.kra at gmail.com
Thu May 1 23:53:14 PDT 2014


On 02.05.2014, at 06:11, Nick Lewycky <nicholas at mxc.ca> wrote:

> Author: nicholas
> Date: Thu May  1 23:11:45 2014
> New Revision: 207828
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=207828&view=rev
> Log:
> Fold strlen(expr ? "str1" : "str2") to x ? len1 : len2. This fires about 330 times in a bootstrap of clang.

Is there anything preventing the strlen folding from being hoisted into the general constant folding logic where we also handle math functions? I think instcombine would give us this kind of folding for free then.

- Ben
> 
> Modified:
>    llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
>    llvm/trunk/test/Transforms/InstCombine/strlen-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=207828&r1=207827&r2=207828&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
> +++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Thu May  1 23:11:45 2014
> @@ -784,10 +784,25 @@ struct StrLenOpt : public LibCallOptimiz
>     if (uint64_t Len = GetStringLength(Src))
>       return ConstantInt::get(CI->getType(), Len-1);
> 
> +    // strlen(x?"foo":"bars") --> x ? 3 : 4
> +    if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {
> +      uint64_t LenTrue = GetStringLength(SI->getTrueValue());
> +      uint64_t LenFalse = GetStringLength(SI->getFalseValue());
> +      if (LenTrue && LenFalse) {
> +        Context->emitOptimizationRemark(
> +            "simplify-libcalls", *Caller, SI->getDebugLoc(),
> +            "folded strlen(select) to select of constants");
> +        return B.CreateSelect(SI->getCondition(),
> +                              ConstantInt::get(CI->getType(), LenTrue-1),
> +                              ConstantInt::get(CI->getType(), LenFalse-1));
> +      }
> +    }
> +
>     // strlen(x) != 0 --> *x != 0
>     // strlen(x) == 0 --> *x == 0
>     if (isOnlyUsedInZeroEqualityComparison(CI))
>       return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
> +
>     return nullptr;
>   }
> };
> 
> Modified: llvm/trunk/test/Transforms/InstCombine/strlen-1.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/strlen-1.ll?rev=207828&r1=207827&r2=207828&view=diff
> ==============================================================================
> --- llvm/trunk/test/Transforms/InstCombine/strlen-1.ll (original)
> +++ llvm/trunk/test/Transforms/InstCombine/strlen-1.ll Thu May  1 23:11:45 2014
> @@ -5,6 +5,7 @@
> target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
> 
> @hello = constant [6 x i8] c"hello\00"
> + at longer = constant [7 x i8] c"longer\00"
> @null = constant [1 x i8] zeroinitializer
> @null_hello = constant [7 x i8] c"\00hello\00"
> @nullstring = constant i8 0
> @@ -85,6 +86,17 @@ define i1 @test_simplify8() {
> ; CHECK-NEXT: ret i1 false
> }
> 
> +define i32 @test_simplify9(i1 %x) {
> +; CHECK-LABEL: @test_simplify9
> +  %hello = getelementptr [6 x i8]* @hello, i32 0, i32 0
> +  %longer = getelementptr [7 x i8]* @longer, i32 0, i32 0
> +  %s = select i1 %x, i8* %hello, i8* %longer
> +  %l = call i32 @strlen(i8* %s)
> +; CHECK-NEXT: select i1 %x, i32 5, i32 6
> +  ret i32 %l
> +; CHECK-NEXT: ret
> +}
> +
> ; Check cases that shouldn't be simplified.
> 
> define i32 @test_no_simplify1() {
> 
> 
> _______________________________________________
> 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