1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 ; Exercise folding of memcmp calls with addition expressions involving
5 ; pointers into constant arrays of types larger than char and fractional
8 declare i32 @memcmp(ptr, ptr, i64)
10 @i32a = constant [2 x i16] [i16 4386, i16 13124]
11 @i32b = constant [2 x i16] [i16 4386, i16 13124]
14 define void @fold_memcmp_i32a_i32b_pIb(i32 %I, ptr %pcmp)
15 ; CHECK-LABEL: @fold_memcmp_i32a_i32b_pIb(
16 ; CHECK-NEXT: store i32 0, ptr [[PCMP:%.*]], align 4
17 ; CHECK-NEXT: [[PST_1_1_2:%.*]] = getelementptr i32, ptr [[PCMP]], i64 1
18 ; CHECK-NEXT: store i32 0, ptr [[PST_1_1_2]], align 4
19 ; CHECK-NEXT: [[PST_1_1_3:%.*]] = getelementptr i32, ptr [[PCMP]], i64 2
20 ; CHECK-NEXT: store i32 0, ptr [[PST_1_1_3]], align 4
21 ; CHECK-NEXT: ret void
26 %pi8ap1 = getelementptr i8, ptr @i32a, i32 1
27 %pi8bp1 = getelementptr i8, ptr @i32b, i32 1
29 %cmp_1_1_1 = call i32 @memcmp(ptr %pi8ap1, ptr %pi8ap1, i64 1)
30 store i32 %cmp_1_1_1, ptr %pcmp
32 %pst_1_1_2 = getelementptr i32, ptr %pcmp, i32 1
33 %cmp_1_1_2 = call i32 @memcmp(ptr %pi8ap1, ptr %pi8ap1, i64 2)
34 store i32 %cmp_1_1_2, ptr %pst_1_1_2
36 %pst_1_1_3 = getelementptr i32, ptr %pcmp, i32 2
37 %cmp_1_1_3 = call i32 @memcmp(ptr %pi8ap1, ptr %pi8ap1, i64 3)
38 store i32 %cmp_1_1_3, ptr %pst_1_1_3
44 %struct.A = type { [4 x i8] }
45 %struct.B = type { [2 x i8], [2 x i8] }
47 @a = constant [1 x %struct.A] [%struct.A { [4 x i8] [i8 1, i8 2, i8 3, i8 4] }]
48 @b = constant [1 x %struct.B] [%struct.B { [2 x i8] [i8 1, i8 2], [2 x i8] [i8 3, i8 4]}]
50 define void @fold_memcmp_A_B_pIb(i32 %I, ptr %pcmp) {
51 ; CHECK-LABEL: @fold_memcmp_A_B_pIb(
52 ; CHECK-NEXT: store i32 0, ptr [[PCMP:%.*]], align 4
53 ; CHECK-NEXT: [[PST_0_0_2:%.*]] = getelementptr i32, ptr [[PCMP]], i64 1
54 ; CHECK-NEXT: store i32 0, ptr [[PST_0_0_2]], align 4
55 ; CHECK-NEXT: [[PST_0_0_3:%.*]] = getelementptr i32, ptr [[PCMP]], i64 2
56 ; CHECK-NEXT: store i32 0, ptr [[PST_0_0_3]], align 4
57 ; CHECK-NEXT: [[PST_0_0_4:%.*]] = getelementptr i32, ptr [[PCMP]], i64 3
58 ; CHECK-NEXT: store i32 0, ptr [[PST_0_0_4]], align 4
59 ; CHECK-NEXT: [[PST_0_1_1:%.*]] = getelementptr i32, ptr [[PCMP]], i64 4
60 ; CHECK-NEXT: store i32 -1, ptr [[PST_0_1_1]], align 4
61 ; CHECK-NEXT: [[PST_0_1_2:%.*]] = getelementptr i32, ptr [[PCMP]], i64 5
62 ; CHECK-NEXT: store i32 -1, ptr [[PST_0_1_2]], align 4
63 ; CHECK-NEXT: [[PST_0_1_3:%.*]] = getelementptr i32, ptr [[PCMP]], i64 6
64 ; CHECK-NEXT: store i32 -1, ptr [[PST_0_1_3]], align 4
65 ; CHECK-NEXT: [[PST_1_0_1:%.*]] = getelementptr i32, ptr [[PCMP]], i64 4
66 ; CHECK-NEXT: store i32 1, ptr [[PST_1_0_1]], align 4
67 ; CHECK-NEXT: [[PST_1_0_2:%.*]] = getelementptr i32, ptr [[PCMP]], i64 5
68 ; CHECK-NEXT: store i32 1, ptr [[PST_1_0_2]], align 4
69 ; CHECK-NEXT: [[PST_1_0_3:%.*]] = getelementptr i32, ptr [[PCMP]], i64 6
70 ; CHECK-NEXT: store i32 1, ptr [[PST_1_0_3]], align 4
71 ; CHECK-NEXT: ret void
76 ; Fold memcmp(&a, &b, 1) to 0;
77 %cmp_0_0_1 = call i32 @memcmp(ptr @a, ptr @b, i64 1)
78 store i32 %cmp_0_0_1, ptr %pcmp
80 ; Fold memcmp(&a, &b, 2) to 0;
81 %pst_0_0_2 = getelementptr i32, ptr %pcmp, i32 1
82 %cmp_0_0_2 = call i32 @memcmp(ptr @a, ptr @b, i64 2)
83 store i32 %cmp_0_0_2, ptr %pst_0_0_2
85 ; Fold memcmp(&a, &b, 3) to 0;
86 %pst_0_0_3 = getelementptr i32, ptr %pcmp, i32 2
87 %cmp_0_0_3 = call i32 @memcmp(ptr @a, ptr @b, i64 3)
88 store i32 %cmp_0_0_3, ptr %pst_0_0_3
90 ; Fold memcmp(&a, &b, 4) to 0;
91 %pst_0_0_4 = getelementptr i32, ptr %pcmp, i32 3
92 %cmp_0_0_4 = call i32 @memcmp(ptr @a, ptr @b, i64 4)
93 store i32 %cmp_0_0_4, ptr %pst_0_0_4
96 %pi8bp1 = getelementptr i8, ptr @b, i32 1
98 ; Fold memcmp(&a, (char*)&b + 1, 1) to -1;
99 %pst_0_1_1 = getelementptr i32, ptr %pcmp, i32 4
100 %cmp_0_1_1 = call i32 @memcmp(ptr @a, ptr %pi8bp1, i64 1)
101 store i32 %cmp_0_1_1, ptr %pst_0_1_1
103 ; Fold memcmp(&a, (char*)&b + 1, 2) to -1;
104 %pst_0_1_2 = getelementptr i32, ptr %pcmp, i32 5
105 %cmp_0_1_2 = call i32 @memcmp(ptr @a, ptr %pi8bp1, i64 2)
106 store i32 %cmp_0_1_2, ptr %pst_0_1_2
108 ; Fold memcmp(&a, (char*)&b + 1, 3) to -1;
109 %pst_0_1_3 = getelementptr i32, ptr %pcmp, i32 6
110 %cmp_0_1_3 = call i32 @memcmp(ptr @a, ptr %pi8bp1, i64 3)
111 store i32 %cmp_0_1_3, ptr %pst_0_1_3
114 %pi8ap1 = getelementptr i8, ptr @a, i32 1
116 ; Fold memcmp((char*)&a + 1, &b, 1) to +1;
117 %pst_1_0_1 = getelementptr i32, ptr %pcmp, i32 4
118 %cmp_1_0_1 = call i32 @memcmp(ptr %pi8ap1, ptr @b, i64 1)
119 store i32 %cmp_1_0_1, ptr %pst_1_0_1
121 ; Fold memcmp((char*)&a + 1, &b, 2) to +1;
122 %pst_1_0_2 = getelementptr i32, ptr %pcmp, i32 5
123 %cmp_1_0_2 = call i32 @memcmp(ptr %pi8ap1, ptr @b, i64 2)
124 store i32 %cmp_1_0_2, ptr %pst_1_0_2
126 ; Fold memcmp((char*)&a + 1, &b, 3) to +1;
127 %pst_1_0_3 = getelementptr i32, ptr %pcmp, i32 6
128 %cmp_1_0_3 = call i32 @memcmp(ptr %pi8ap1, ptr @b, i64 3)
129 store i32 %cmp_1_0_3, ptr %pst_1_0_3