1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test that the strcmp library call simplifier works correctly.
3 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
5 target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
7 @hello = constant [6 x i8] c"hello\00"
8 @hell = constant [5 x i8] c"hell\00"
9 @bell = constant [5 x i8] c"bell\00"
10 @null = constant [1 x i8] zeroinitializer
12 declare i32 @strcmp(ptr, ptr)
14 ; strcmp("", x) -> -*x
15 define arm_aapcscc i32 @test1(ptr %str2) {
16 ; CHECK-LABEL: @test1(
17 ; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, ptr [[STR2:%.*]], align 1
18 ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32
19 ; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]]
20 ; CHECK-NEXT: ret i32 [[TMP2]]
23 %temp1 = call arm_apcscc i32 @strcmp(ptr @null, ptr %str2)
29 define arm_aapcscc i32 @test2(ptr %str1) {
30 ; CHECK-LABEL: @test2(
31 ; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, ptr [[STR1:%.*]], align 1
32 ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32
33 ; CHECK-NEXT: ret i32 [[TMP1]]
36 %temp1 = call arm_aapcscc i32 @strcmp(ptr %str1, ptr @null)
40 ; strcmp(x, y) -> cnst
41 define arm_aapcscc i32 @test3() {
42 ; CHECK-LABEL: @test3(
43 ; CHECK-NEXT: ret i32 -1
46 %temp1 = call arm_aapcscc i32 @strcmp(ptr @hell, ptr @hello)
50 define arm_aapcscc i32 @test4() {
51 ; CHECK-LABEL: @test4(
52 ; CHECK-NEXT: ret i32 1
55 %temp1 = call arm_aapcscc i32 @strcmp(ptr @hell, ptr @null)
59 ; strcmp(x, y) -> memcmp(x, y, <known length>)
60 ; (This transform is rather difficult to trigger in a useful manner)
61 define arm_aapcscc i32 @test5(i1 %b) {
62 ; CHECK-LABEL: @test5(
63 ; CHECK-NEXT: [[STR2:%.*]] = select i1 [[B:%.*]], ptr @hell, ptr @bell
64 ; CHECK-NEXT: [[MEMCMP:%.*]] = call i32 @memcmp(ptr noundef nonnull dereferenceable(5) @hello, ptr noundef nonnull dereferenceable(5) [[STR2]], i32 5)
65 ; CHECK-NEXT: ret i32 [[MEMCMP]]
68 %str2 = select i1 %b, ptr @hell, ptr @bell
69 %temp3 = call arm_aapcscc i32 @strcmp(ptr @hello, ptr %str2)
74 define arm_aapcscc i32 @test6(ptr %str) {
75 ; CHECK-LABEL: @test6(
76 ; CHECK-NEXT: ret i32 0
79 %temp1 = call arm_aapcscc i32 @strcmp(ptr %str, ptr %str)
83 ; strcmp("", x) -> -*x
84 define arm_aapcs_vfpcc i32 @test1_vfp(ptr %str2) {
85 ; CHECK-LABEL: @test1_vfp(
86 ; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, ptr [[STR2:%.*]], align 1
87 ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32
88 ; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]]
89 ; CHECK-NEXT: ret i32 [[TMP2]]
92 %temp1 = call arm_aapcs_vfpcc i32 @strcmp(ptr @null, ptr %str2)
98 define arm_aapcs_vfpcc i32 @test2_vfp(ptr %str1) {
99 ; CHECK-LABEL: @test2_vfp(
100 ; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, ptr [[STR1:%.*]], align 1
101 ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32
102 ; CHECK-NEXT: ret i32 [[TMP1]]
105 %temp1 = call arm_aapcs_vfpcc i32 @strcmp(ptr %str1, ptr @null)
109 ; strcmp(x, y) -> cnst
110 define arm_aapcs_vfpcc i32 @test3_vfp() {
111 ; CHECK-LABEL: @test3_vfp(
112 ; CHECK-NEXT: ret i32 -1
115 %temp1 = call arm_aapcs_vfpcc i32 @strcmp(ptr @hell, ptr @hello)
119 define arm_aapcs_vfpcc i32 @test4_vfp() {
120 ; CHECK-LABEL: @test4_vfp(
121 ; CHECK-NEXT: ret i32 1
124 %temp1 = call arm_aapcs_vfpcc i32 @strcmp(ptr @hell, ptr @null)
128 ; strcmp(x, y) -> memcmp(x, y, <known length>)
129 ; (This transform is rather difficult to trigger in a useful manner)
130 define arm_aapcs_vfpcc i32 @test5_vfp(i1 %b) {
131 ; CHECK-LABEL: @test5_vfp(
132 ; CHECK-NEXT: [[STR2:%.*]] = select i1 [[B:%.*]], ptr @hell, ptr @bell
133 ; CHECK-NEXT: [[MEMCMP:%.*]] = call i32 @memcmp(ptr noundef nonnull dereferenceable(5) @hello, ptr noundef nonnull dereferenceable(5) [[STR2]], i32 5)
134 ; CHECK-NEXT: ret i32 [[MEMCMP]]
137 %str2 = select i1 %b, ptr @hell, ptr @bell
138 %temp3 = call arm_aapcs_vfpcc i32 @strcmp(ptr @hello, ptr %str2)
143 define arm_aapcs_vfpcc i32 @test6_vfp(ptr %str) {
144 ; CHECK-LABEL: @test6_vfp(
145 ; CHECK-NEXT: ret i32 0
148 %temp1 = call arm_aapcs_vfpcc i32 @strcmp(ptr %str, ptr %str)