235 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; RUN: opt < %s -passes=instcombine -S | FileCheck %s3;4; Exercise folding of memcmp calls with constant arrays and nonconstant5; sizes.6 7declare i32 @memcmp(ptr, ptr, i64)8 9@ax = external constant [8 x i8]10@a01230123 = constant [8 x i8] c"01230123"11@b01230123 = constant [8 x i8] c"01230123"12@c01230129 = constant [8 x i8] c"01230129"13@d9123012 = constant [7 x i8] c"9123012"14 15 16; Exercise memcmp(A, B, N) folding of arrays with the same bytes.17 18define void @fold_memcmp_a_b_n(ptr %pcmp, i64 %n) {19; CHECK-LABEL: @fold_memcmp_a_b_n(20; CHECK-NEXT: store i32 0, ptr [[PCMP:%.*]], align 421; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i64 [[N:%.*]], 022; CHECK-NEXT: [[C0_1:%.*]] = sext i1 [[TMP1]] to i3223; CHECK-NEXT: [[S0_1:%.*]] = getelementptr i8, ptr [[PCMP]], i64 424; CHECK-NEXT: store i32 [[C0_1]], ptr [[S0_1]], align 425; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i64 [[N]], 026; CHECK-NEXT: [[C0_2:%.*]] = sext i1 [[TMP2]] to i3227; CHECK-NEXT: [[S0_2:%.*]] = getelementptr i8, ptr [[PCMP]], i64 828; CHECK-NEXT: store i32 [[C0_2]], ptr [[S0_2]], align 429; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i64 [[N]], 030; CHECK-NEXT: [[C0_3:%.*]] = sext i1 [[TMP3]] to i3231; CHECK-NEXT: [[S0_3:%.*]] = getelementptr i8, ptr [[PCMP]], i64 1232; CHECK-NEXT: store i32 [[C0_3]], ptr [[S0_3]], align 433; CHECK-NEXT: [[S0_4:%.*]] = getelementptr i8, ptr [[PCMP]], i64 1634; CHECK-NEXT: store i32 0, ptr [[S0_4]], align 435; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i64 [[N]], 036; CHECK-NEXT: [[C0_5:%.*]] = sext i1 [[TMP4]] to i3237; CHECK-NEXT: [[S0_5:%.*]] = getelementptr i8, ptr [[PCMP]], i64 2038; CHECK-NEXT: store i32 [[C0_5]], ptr [[S0_5]], align 439; CHECK-NEXT: ret void40;41 42 43 %q1 = getelementptr [8 x i8], ptr @b01230123, i64 0, i64 144 %q2 = getelementptr [8 x i8], ptr @b01230123, i64 0, i64 245 %q3 = getelementptr [8 x i8], ptr @b01230123, i64 0, i64 346 %q4 = getelementptr [8 x i8], ptr @b01230123, i64 0, i64 447 %q5 = getelementptr [8 x i8], ptr @b01230123, i64 0, i64 548 49 ; Fold memcmp(a, b, n) to 0.50 %c0_0 = call i32 @memcmp(ptr @a01230123, ptr @b01230123, i64 %n)51 store i32 %c0_0, ptr %pcmp52 53 ; Fold memcmp(a, b + 1, n) to N != 0 ? -1 : 0.54 %c0_1 = call i32 @memcmp(ptr @a01230123, ptr %q1, i64 %n)55 %s0_1 = getelementptr i32, ptr %pcmp, i64 156 store i32 %c0_1, ptr %s0_157 58 ; Fold memcmp(a, b + 2, n) to N != 0 ? -1 : 0.59 %c0_2 = call i32 @memcmp(ptr @a01230123, ptr %q2, i64 %n)60 %s0_2 = getelementptr i32, ptr %pcmp, i64 261 store i32 %c0_2, ptr %s0_262 63 ; Fold memcmp(a, b + 3, n) to N != 0 ? -1 : 0.64 %c0_3 = call i32 @memcmp(ptr @a01230123, ptr %q3, i64 %n)65 %s0_3 = getelementptr i32, ptr %pcmp, i64 366 store i32 %c0_3, ptr %s0_367 68 ; Fold memcmp(a, b + 4, n) to 0.69 %c0_4 = call i32 @memcmp(ptr @a01230123, ptr %q4, i64 %n)70 %s0_4 = getelementptr i32, ptr %pcmp, i64 471 store i32 %c0_4, ptr %s0_472 73 ; Fold memcmp(a, b + 5, n) to N != 0 ? -1 : 0.74 %c0_5 = call i32 @memcmp(ptr @a01230123, ptr %q5, i64 %n)75 %s0_5 = getelementptr i32, ptr %pcmp, i64 576 store i32 %c0_5, ptr %s0_577 78 ret void79}80 81; Vefify that a memcmp() call involving a constant array with unknown82; contents is not folded.83 84define void @call_memcmp_a_ax_n(ptr %pcmp, i64 %n) {85; CHECK-LABEL: @call_memcmp_a_ax_n(86; CHECK-NEXT: [[C0_0:%.*]] = call i32 @memcmp(ptr nonnull @a01230123, ptr nonnull @ax, i64 [[N:%.*]])87; CHECK-NEXT: store i32 [[C0_0]], ptr [[PCMP:%.*]], align 488; CHECK-NEXT: ret void89;90 91 92 ; Do not fold memcmp(a, ax, n).93 %c0_0 = call i32 @memcmp(ptr @a01230123, ptr @ax, i64 %n)94 store i32 %c0_0, ptr %pcmp95 96 ret void97}98 99 100; Exercise memcmp(A, C, N) folding of arrays with the same leading bytes101; but a difference in the trailing byte.102 103define void @fold_memcmp_a_c_n(ptr %pcmp, i64 %n) {104; CHECK-LABEL: @fold_memcmp_a_c_n(105; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i64 [[N:%.*]], 7106; CHECK-NEXT: [[C0_0:%.*]] = sext i1 [[TMP1]] to i32107; CHECK-NEXT: store i32 [[C0_0]], ptr [[PCMP:%.*]], align 4108; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i64 [[N]], 0109; CHECK-NEXT: [[C0_1:%.*]] = sext i1 [[TMP2]] to i32110; CHECK-NEXT: [[S0_1:%.*]] = getelementptr i8, ptr [[PCMP]], i64 4111; CHECK-NEXT: store i32 [[C0_1]], ptr [[S0_1]], align 4112; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i64 [[N]], 0113; CHECK-NEXT: [[C0_2:%.*]] = sext i1 [[TMP3]] to i32114; CHECK-NEXT: [[S0_2:%.*]] = getelementptr i8, ptr [[PCMP]], i64 8115; CHECK-NEXT: store i32 [[C0_2]], ptr [[S0_2]], align 4116; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i64 [[N]], 0117; CHECK-NEXT: [[C0_3:%.*]] = sext i1 [[TMP4]] to i32118; CHECK-NEXT: [[S0_3:%.*]] = getelementptr i8, ptr [[PCMP]], i64 12119; CHECK-NEXT: store i32 [[C0_3]], ptr [[S0_3]], align 4120; CHECK-NEXT: [[TMP5:%.*]] = icmp ugt i64 [[N]], 3121; CHECK-NEXT: [[C0_4:%.*]] = sext i1 [[TMP5]] to i32122; CHECK-NEXT: [[S0_4:%.*]] = getelementptr i8, ptr [[PCMP]], i64 16123; CHECK-NEXT: store i32 [[C0_4]], ptr [[S0_4]], align 4124; CHECK-NEXT: [[TMP6:%.*]] = icmp ugt i64 [[N]], 3125; CHECK-NEXT: [[C0_5:%.*]] = sext i1 [[TMP6]] to i32126; CHECK-NEXT: [[S0_5:%.*]] = getelementptr i8, ptr [[PCMP]], i64 20127; CHECK-NEXT: store i32 [[C0_5]], ptr [[S0_5]], align 4128; CHECK-NEXT: ret void129;130 131 132 %q1 = getelementptr [8 x i8], ptr @c01230129, i64 0, i64 1133 %q2 = getelementptr [8 x i8], ptr @c01230129, i64 0, i64 2134 %q3 = getelementptr [8 x i8], ptr @c01230129, i64 0, i64 3135 %q4 = getelementptr [8 x i8], ptr @c01230129, i64 0, i64 4136 %q5 = getelementptr [8 x i8], ptr @c01230129, i64 0, i64 5137 138 ; Fold memcmp(a, c, n) to N > 7 ? -1 : 0.139 %c0_0 = call i32 @memcmp(ptr @a01230123, ptr @c01230129, i64 %n)140 store i32 %c0_0, ptr %pcmp141 142 ; Fold memcmp(a, c + 1, n) to N != 0 ? -1 : 0.143 %c0_1 = call i32 @memcmp(ptr @a01230123, ptr %q1, i64 %n)144 %s0_1 = getelementptr i32, ptr %pcmp, i64 1145 store i32 %c0_1, ptr %s0_1146 147 ; Fold memcmp(a, c + 2, n) to N != 0 ? -1 : 0.148 %c0_2 = call i32 @memcmp(ptr @a01230123, ptr %q2, i64 %n)149 %s0_2 = getelementptr i32, ptr %pcmp, i64 2150 store i32 %c0_2, ptr %s0_2151 152 ; Fold memcmp(a, c + 3, n) to N != 0 ? -1 : 0.153 %c0_3 = call i32 @memcmp(ptr @a01230123, ptr %q3, i64 %n)154 %s0_3 = getelementptr i32, ptr %pcmp, i64 3155 store i32 %c0_3, ptr %s0_3156 157 ; Fold memcmp(a, c + 4, n) to N > 3 ? -1 : 0.158 %c0_4 = call i32 @memcmp(ptr @a01230123, ptr %q4, i64 %n)159 %s0_4 = getelementptr i32, ptr %pcmp, i64 4160 store i32 %c0_4, ptr %s0_4161 162 ; Fold memcmp(a, c + 5, n) to N != 0 ? -1 : 0.163 %c0_5 = call i32 @memcmp(ptr @a01230123, ptr %q4, i64 %n)164 %s0_5 = getelementptr i32, ptr %pcmp, i64 5165 store i32 %c0_5, ptr %s0_5166 167 ret void168}169 170 171; Exercise memcmp(A, D, N) folding of arrays of different sizes and172; a difference in the leading byte.173 174define void @fold_memcmp_a_d_n(ptr %pcmp, i64 %n) {175; CHECK-LABEL: @fold_memcmp_a_d_n(176; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i64 [[N:%.*]], 0177; CHECK-NEXT: [[C0_0:%.*]] = sext i1 [[TMP1]] to i32178; CHECK-NEXT: store i32 [[C0_0]], ptr [[PCMP:%.*]], align 4179; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i64 [[N]], 0180; CHECK-NEXT: [[C0_1:%.*]] = sext i1 [[TMP2]] to i32181; CHECK-NEXT: [[S0_1:%.*]] = getelementptr i8, ptr [[PCMP]], i64 4182; CHECK-NEXT: store i32 [[C0_1]], ptr [[S0_1]], align 4183; CHECK-NEXT: [[S1_1:%.*]] = getelementptr i8, ptr [[PCMP]], i64 8184; CHECK-NEXT: store i32 0, ptr [[S1_1]], align 4185; CHECK-NEXT: [[S6_6:%.*]] = getelementptr i8, ptr [[PCMP]], i64 12186; CHECK-NEXT: store i32 0, ptr [[S6_6]], align 4187; CHECK-NEXT: ret void188;189 190 %p1 = getelementptr [8 x i8], ptr @a01230123, i64 0, i64 1191 %p6 = getelementptr [8 x i8], ptr @a01230123, i64 0, i64 6192 193 %q1 = getelementptr [7 x i8], ptr @d9123012, i64 0, i64 1194 %q6 = getelementptr [7 x i8], ptr @d9123012, i64 0, i64 6195 196 ; Fold memcmp(a, d, n) to N != 0 ? -1 : 0.197 %c0_0 = call i32 @memcmp(ptr @a01230123, ptr @d9123012, i64 %n)198 store i32 %c0_0, ptr %pcmp199 200 ; Fold memcmp(a, d + 1, n) to N != 0 -1 : 0.201 %c0_1 = call i32 @memcmp(ptr @a01230123, ptr %q1, i64 %n)202 %s0_1 = getelementptr i32, ptr %pcmp, i64 1203 store i32 %c0_1, ptr %s0_1204 205 ; Fold memcmp(a + 1, d + 1, n) to 0.206 %c1_1 = call i32 @memcmp(ptr %p1, ptr %q1, i64 %n)207 %s1_1 = getelementptr i32, ptr %pcmp, i64 2208 store i32 %c1_1, ptr %s1_1209 210 ; Fold memcmp(a + 6, d + 6, n) to 0.211 %c6_6 = call i32 @memcmp(ptr %p6, ptr %q6, i64 %n)212 %s6_6 = getelementptr i32, ptr %pcmp, i64 3213 store i32 %c6_6, ptr %s6_6214 215 ret void216}217 218 219; Exercise memcmp(A, D, N) folding of arrays with the same bytes and220; a nonzero size.221 222define void @fold_memcmp_a_d_nz(ptr %pcmp, i64 %n) {223; CHECK-LABEL: @fold_memcmp_a_d_nz(224; CHECK-NEXT: store i32 -1, ptr [[PCMP:%.*]], align 4225; CHECK-NEXT: ret void226;227 228 %nz = or i64 %n, 1229 230 %c0_0 = call i32 @memcmp(ptr @a01230123, ptr @d9123012, i64 %nz)231 store i32 %c0_0, ptr %pcmp232 233 ret void234}235