Index: lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- lib/Transforms/Utils/SimplifyLibCalls.cpp +++ lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -322,6 +322,12 @@ B, DL, TLI); } + // strcmp(x, "x") -> *x == 'x' + if ((HasStr1 && Str1.size() == 1) || (HasStr2 && Str2.size() == 1)) + return emitMemCmp(Str1P, Str2P, + ConstantInt::get(DL.getIntPtrType(CI->getContext()), 1), + B, DL, TLI); + return nullptr; } Index: test/Transforms/InstCombine/strcmp-1.ll =================================================================== --- test/Transforms/InstCombine/strcmp-1.ll +++ test/Transforms/InstCombine/strcmp-1.ll @@ -1,3 +1,4 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; Test that the strcmp library call simplifier works correctly. ; RUN: opt < %s -instcombine -S | FileCheck %s @@ -6,6 +7,7 @@ @hello = constant [6 x i8] c"hello\00" @hell = constant [5 x i8] c"hell\00" @bell = constant [5 x i8] c"bell\00" +@str_single_char = constant [2 x i8] c".\00" @null = constant [1 x i8] zeroinitializer declare i32 @strcmp(i8*, i8*) @@ -13,10 +15,11 @@ ; strcmp("", x) -> -*x define i32 @test1(i8* %str2) { ; CHECK-LABEL: @test1( -; CHECK: %strcmpload = load i8, i8* %str -; CHECK: %1 = zext i8 %strcmpload to i32 -; CHECK: %2 = sub nsw i32 0, %1 -; CHECK: ret i32 %2 +; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, i8* [[STR2:%.*]], align 1 +; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32 +; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]] +; CHECK-NEXT: ret i32 [[TMP2]] +; %str1 = getelementptr inbounds [1 x i8], [1 x i8]* @null, i32 0, i32 0 %temp1 = call i32 @strcmp(i8* %str1, i8* %str2) @@ -27,9 +30,10 @@ ; strcmp(x, "") -> *x define i32 @test2(i8* %str1) { ; CHECK-LABEL: @test2( -; CHECK: %strcmpload = load i8, i8* %str -; CHECK: %1 = zext i8 %strcmpload to i32 -; CHECK: ret i32 %1 +; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, i8* [[STR1:%.*]], align 1 +; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32 +; CHECK-NEXT: ret i32 [[TMP1]] +; %str2 = getelementptr inbounds [1 x i8], [1 x i8]* @null, i32 0, i32 0 %temp1 = call i32 @strcmp(i8* %str1, i8* %str2) @@ -39,7 +43,8 @@ ; strcmp(x, y) -> cnst define i32 @test3() { ; CHECK-LABEL: @test3( -; CHECK: ret i32 -1 +; CHECK-NEXT: ret i32 -1 +; %str1 = getelementptr inbounds [5 x i8], [5 x i8]* @hell, i32 0, i32 0 %str2 = getelementptr inbounds [6 x i8], [6 x i8]* @hello, i32 0, i32 0 @@ -49,7 +54,8 @@ define i32 @test4() { ; CHECK-LABEL: @test4( -; CHECK: ret i32 1 +; CHECK-NEXT: ret i32 1 +; %str1 = getelementptr inbounds [5 x i8], [5 x i8]* @hell, i32 0, i32 0 %str2 = getelementptr inbounds [1 x i8], [1 x i8]* @null, i32 0, i32 0 @@ -61,8 +67,10 @@ ; (This transform is rather difficult to trigger in a useful manner) define i32 @test5(i1 %b) { ; CHECK-LABEL: @test5( -; CHECK: %memcmp = call i32 @memcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i8* %str2, i32 5) -; CHECK: ret i32 %memcmp +; CHECK-NEXT: [[STR2:%.*]] = select i1 [[B:%.*]], i8* getelementptr inbounds ([5 x i8], [5 x i8]* @hell, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @bell, i32 0, i32 0) +; CHECK-NEXT: [[MEMCMP:%.*]] = call i32 @memcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i8* [[STR2]], i32 5) +; CHECK-NEXT: ret i32 [[MEMCMP]] +; %str1 = getelementptr inbounds [6 x i8], [6 x i8]* @hello, i32 0, i32 0 %temp1 = getelementptr inbounds [5 x i8], [5 x i8]* @hell, i32 0, i32 0 @@ -75,8 +83,37 @@ ; strcmp(x,x) -> 0 define i32 @test6(i8* %str) { ; CHECK-LABEL: @test6( -; CHECK: ret i32 0 +; CHECK-NEXT: ret i32 0 +; %temp1 = call i32 @strcmp(i8* %str, i8* %str) ret i32 %temp1 } + +; strcmp(x,"x") -> *x == 'x' +define i32 @compare_single_char(i8* %s) { +; CHECK-LABEL: @compare_single_char( +; CHECK-NEXT: [[LHSC:%.*]] = load i8, i8* [[S:%.*]], align 1 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[LHSC]], 46 +; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP]] to i32 +; CHECK-NEXT: ret i32 [[CONV]] +; + %call = tail call i32 @strcmp(i8* %s, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @str_single_char, i64 0, i64 0)) #2 + %cmp = icmp eq i32 %call, 0 + %conv = zext i1 %cmp to i32 + ret i32 %conv +} + +; strcmp("x", x) -> *x == 'x' +define i32 @compare_single_char_rev(i8* %s) { +; CHECK-LABEL: @compare_single_char_rev( +; CHECK-NEXT: [[RHSC:%.*]] = load i8, i8* [[S:%.*]], align 1 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[RHSC]], 46 +; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP]] to i32 +; CHECK-NEXT: ret i32 [[CONV]] +; + %call = tail call i32 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @str_single_char, i64 0, i64 0), i8* %s) #2 + %cmp = icmp eq i32 %call, 0 + %conv = zext i1 %cmp to i32 + ret i32 %conv +}