1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -instcombine -S < %s | FileCheck %s
4 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:64:64-v128:128:128-a0:0:64"
6 define i32 *@test1(i32* %A, i32 %Offset) {
9 ; CHECK-NEXT: br label [[BB:%.*]]
11 ; CHECK-NEXT: [[RHS_IDX:%.*]] = phi i32 [ [[RHS_ADD:%.*]], [[BB]] ], [ [[OFFSET:%.*]], [[ENTRY:%.*]] ]
12 ; CHECK-NEXT: [[RHS_ADD]] = add nsw i32 [[RHS_IDX]], 1
13 ; CHECK-NEXT: [[COND:%.*]] = icmp sgt i32 [[RHS_IDX]], 100
14 ; CHECK-NEXT: br i1 [[COND]], label [[BB2:%.*]], label [[BB]]
16 ; CHECK-NEXT: [[RHS_PTR:%.*]] = getelementptr inbounds i32, i32* [[A:%.*]], i32 [[RHS_IDX]]
17 ; CHECK-NEXT: ret i32* [[RHS_PTR]]
20 %tmp = getelementptr inbounds i32, i32* %A, i32 %Offset
24 %RHS = phi i32* [ %RHS.next, %bb ], [ %tmp, %entry ]
25 %LHS = getelementptr inbounds i32, i32* %A, i32 100
26 %RHS.next = getelementptr inbounds i32, i32* %RHS, i64 1
27 %cond = icmp ult i32 * %LHS, %RHS
28 br i1 %cond, label %bb2, label %bb
35 define i32 *@test2(i32 %A, i32 %Offset) {
36 ; CHECK-LABEL: @test2(
38 ; CHECK-NEXT: br label [[BB:%.*]]
40 ; CHECK-NEXT: [[RHS_IDX:%.*]] = phi i32 [ [[RHS_ADD:%.*]], [[BB]] ], [ [[OFFSET:%.*]], [[ENTRY:%.*]] ]
41 ; CHECK-NEXT: [[RHS_ADD]] = add nsw i32 [[RHS_IDX]], 1
42 ; CHECK-NEXT: [[COND:%.*]] = icmp sgt i32 [[RHS_IDX]], 100
43 ; CHECK-NEXT: br i1 [[COND]], label [[BB2:%.*]], label [[BB]]
45 ; CHECK-NEXT: [[RHSTO_PTR:%.*]] = inttoptr i32 [[A:%.*]] to i32*
46 ; CHECK-NEXT: [[RHS_PTR:%.*]] = getelementptr inbounds i32, i32* [[RHSTO_PTR]], i32 [[RHS_IDX]]
47 ; CHECK-NEXT: ret i32* [[RHS_PTR]]
50 %A.ptr = inttoptr i32 %A to i32*
51 %tmp = getelementptr inbounds i32, i32* %A.ptr, i32 %Offset
55 %RHS = phi i32* [ %RHS.next, %bb ], [ %tmp, %entry ]
56 %LHS = getelementptr inbounds i32, i32* %A.ptr, i32 100
57 %RHS.next = getelementptr inbounds i32, i32* %RHS, i64 1
58 %cmp0 = ptrtoint i32 *%LHS to i32
59 %cmp1 = ptrtoint i32 *%RHS to i32
60 %cond = icmp ult i32 %cmp0, %cmp1
61 br i1 %cond, label %bb2, label %bb
68 ; Perform the transformation only if we know that the GEPs used are inbounds.
69 define i32 *@test3(i32* %A, i32 %Offset) {
70 ; CHECK-LABEL: @test3(
72 ; CHECK-NEXT: [[TMP:%.*]] = getelementptr i32, i32* [[A:%.*]], i32 [[OFFSET:%.*]]
73 ; CHECK-NEXT: br label [[BB:%.*]]
75 ; CHECK-NEXT: [[RHS:%.*]] = phi i32* [ [[RHS_NEXT:%.*]], [[BB]] ], [ [[TMP]], [[ENTRY:%.*]] ]
76 ; CHECK-NEXT: [[LHS:%.*]] = getelementptr i32, i32* [[A]], i32 100
77 ; CHECK-NEXT: [[RHS_NEXT]] = getelementptr i32, i32* [[RHS]], i32 1
78 ; CHECK-NEXT: [[COND:%.*]] = icmp ult i32* [[LHS]], [[RHS]]
79 ; CHECK-NEXT: br i1 [[COND]], label [[BB2:%.*]], label [[BB]]
81 ; CHECK-NEXT: ret i32* [[RHS]]
84 %tmp = getelementptr i32, i32* %A, i32 %Offset
88 %RHS = phi i32* [ %RHS.next, %bb ], [ %tmp, %entry ]
89 %LHS = getelementptr i32, i32* %A, i32 100
90 %RHS.next = getelementptr i32, i32* %RHS, i64 1
91 %cond = icmp ult i32 * %LHS, %RHS
92 br i1 %cond, label %bb2, label %bb
99 ; An inttoptr that requires an extension or truncation will be opaque when determining
100 ; the base pointer. In this case we can still perform the transformation by considering
101 ; A.ptr as being the base pointer.
102 define i32 *@test4(i16 %A, i32 %Offset) {
103 ; CHECK-LABEL: @test4(
105 ; CHECK-NEXT: br label [[BB:%.*]]
107 ; CHECK-NEXT: [[RHS_IDX:%.*]] = phi i32 [ [[RHS_ADD:%.*]], [[BB]] ], [ [[OFFSET:%.*]], [[ENTRY:%.*]] ]
108 ; CHECK-NEXT: [[RHS_ADD]] = add nsw i32 [[RHS_IDX]], 1
109 ; CHECK-NEXT: [[COND:%.*]] = icmp sgt i32 [[RHS_IDX]], 100
110 ; CHECK-NEXT: br i1 [[COND]], label [[BB2:%.*]], label [[BB]]
112 ; CHECK-NEXT: [[TMP0:%.*]] = zext i16 [[A:%.*]] to i32
113 ; CHECK-NEXT: [[RHSTO_PTR:%.*]] = inttoptr i32 [[TMP0]] to i32*
114 ; CHECK-NEXT: [[RHS_PTR:%.*]] = getelementptr inbounds i32, i32* [[RHSTO_PTR]], i32 [[RHS_IDX]]
115 ; CHECK-NEXT: ret i32* [[RHS_PTR]]
118 %A.ptr = inttoptr i16 %A to i32*
119 %tmp = getelementptr inbounds i32, i32* %A.ptr, i32 %Offset
123 %RHS = phi i32* [ %RHS.next, %bb ], [ %tmp, %entry ]
124 %LHS = getelementptr inbounds i32, i32* %A.ptr, i32 100
125 %RHS.next = getelementptr inbounds i32, i32* %RHS, i64 1
126 %cmp0 = ptrtoint i32 *%LHS to i32
127 %cmp1 = ptrtoint i32 *%RHS to i32
128 %cond = icmp ult i32 %cmp0, %cmp1
129 br i1 %cond, label %bb2, label %bb
136 declare i32* @fun_ptr()
138 define i32 *@test5(i32 %Offset) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
139 ; CHECK-LABEL: @test5(
141 ; CHECK-NEXT: [[A:%.*]] = invoke i32* @fun_ptr()
142 ; CHECK-NEXT: to label [[CONT:%.*]] unwind label [[LPAD:%.*]]
144 ; CHECK-NEXT: br label [[BB:%.*]]
146 ; CHECK-NEXT: [[RHS_IDX:%.*]] = phi i32 [ [[RHS_ADD:%.*]], [[BB]] ], [ [[OFFSET:%.*]], [[CONT]] ]
147 ; CHECK-NEXT: [[RHS_ADD]] = add nsw i32 [[RHS_IDX]], 1
148 ; CHECK-NEXT: [[COND:%.*]] = icmp sgt i32 [[RHS_IDX]], 100
149 ; CHECK-NEXT: br i1 [[COND]], label [[BB2:%.*]], label [[BB]]
151 ; CHECK-NEXT: [[RHS_PTR:%.*]] = getelementptr inbounds i32, i32* [[A]], i32 [[RHS_IDX]]
152 ; CHECK-NEXT: ret i32* [[RHS_PTR]]
154 ; CHECK-NEXT: [[L:%.*]] = landingpad { i8*, i32 }
155 ; CHECK-NEXT: cleanup
156 ; CHECK-NEXT: ret i32* null
159 %A = invoke i32 *@fun_ptr() to label %cont unwind label %lpad
162 %tmp = getelementptr inbounds i32, i32* %A, i32 %Offset
166 %RHS = phi i32* [ %RHS.next, %bb ], [ %tmp, %cont ]
167 %LHS = getelementptr inbounds i32, i32* %A, i32 100
168 %RHS.next = getelementptr inbounds i32, i32* %RHS, i64 1
169 %cond = icmp ult i32 * %LHS, %RHS
170 br i1 %cond, label %bb2, label %bb
176 %l = landingpad { i8*, i32 } cleanup
181 declare i32 @fun_i32()
183 define i32 *@test6(i32 %Offset) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
184 ; CHECK-LABEL: @test6(
186 ; CHECK-NEXT: [[A:%.*]] = invoke i32 @fun_i32()
187 ; CHECK-NEXT: to label [[CONT:%.*]] unwind label [[LPAD:%.*]]
189 ; CHECK-NEXT: br label [[BB:%.*]]
191 ; CHECK-NEXT: [[RHS_IDX:%.*]] = phi i32 [ [[RHS_ADD:%.*]], [[BB]] ], [ [[OFFSET:%.*]], [[CONT]] ]
192 ; CHECK-NEXT: [[RHS_ADD]] = add nsw i32 [[RHS_IDX]], 1
193 ; CHECK-NEXT: [[COND:%.*]] = icmp sgt i32 [[RHS_IDX]], 100
194 ; CHECK-NEXT: br i1 [[COND]], label [[BB2:%.*]], label [[BB]]
196 ; CHECK-NEXT: [[RHSTO_PTR:%.*]] = inttoptr i32 [[A]] to i32*
197 ; CHECK-NEXT: [[RHS_PTR:%.*]] = getelementptr inbounds i32, i32* [[RHSTO_PTR]], i32 [[RHS_IDX]]
198 ; CHECK-NEXT: ret i32* [[RHS_PTR]]
200 ; CHECK-NEXT: [[L:%.*]] = landingpad { i8*, i32 }
201 ; CHECK-NEXT: cleanup
202 ; CHECK-NEXT: ret i32* null
205 %A = invoke i32 @fun_i32() to label %cont unwind label %lpad
208 %A.ptr = inttoptr i32 %A to i32*
209 %tmp = getelementptr inbounds i32, i32* %A.ptr, i32 %Offset
213 %RHS = phi i32* [ %RHS.next, %bb ], [ %tmp, %cont ]
214 %LHS = getelementptr inbounds i32, i32* %A.ptr, i32 100
215 %RHS.next = getelementptr inbounds i32, i32* %RHS, i64 1
216 %cond = icmp ult i32 * %LHS, %RHS
217 br i1 %cond, label %bb2, label %bb
223 %l = landingpad { i8*, i32 } cleanup
229 @pr30402 = constant i64 3
231 ; CHECK-LABEL: @test7(
233 ; CHECK-NEXT: br label [[BB7:%.*]]
235 ; CHECK-NEXT: [[CMP:%.*]] = phi i1 [ true, [[BB7]] ], [ false, [[ENTRY:%.*]] ]
236 ; CHECK-NEXT: br i1 [[CMP]], label [[BB10:%.*]], label [[BB7]]
238 ; CHECK-NEXT: ret i1 [[CMP]]
243 bb7: ; preds = %bb10, %entry-block
244 %phi = phi i64* [ @pr30402, %entry ], [ getelementptr inbounds (i64, i64* @pr30402, i32 1), %bb7 ]
245 %cmp = icmp eq i64* %phi, getelementptr inbounds (i64, i64* @pr30402, i32 1)
246 br i1 %cmp, label %bb10, label %bb7
252 ; It is not generally safe to hoist an expression (sdiv) that may trap.
254 define i1 @PR50906() {
255 ; CHECK-LABEL: @PR50906(
257 ; CHECK-NEXT: br label [[LOOP:%.*]]
259 ; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 1, [[NEXT:%.*]] ]
260 ; CHECK-NEXT: br label [[NEXT]]
262 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[PHI]], sdiv (i32 7, i32 ptrtoint (i1 ()* @PR50906 to i32))
263 ; CHECK-NEXT: br i1 [[CMP]], label [[EXIT:%.*]], label [[LOOP]]
265 ; CHECK-NEXT: ret i1 [[CMP]]
271 %phi = phi i32 [ 0, %entry ], [ 1, %next ]
275 %cmp = icmp sgt i32 sdiv (i32 7, i32 ptrtoint (i1 ()* @PR50906 to i32)), %phi
276 br i1 %cmp, label %exit, label %loop
282 declare i32 @__gxx_personality_v0(...)
284 define i1 @test8(i64* %in, i64 %offset) {
285 ; CHECK-LABEL: @test8(
287 ; CHECK-NEXT: [[LD:%.*]] = load i64, i64* [[IN:%.*]], align 8
288 ; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[LD]] to i32
289 ; CHECK-NEXT: [[CASTI8:%.*]] = inttoptr i32 [[TMP0]] to i8*
290 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[OFFSET:%.*]] to i32
291 ; CHECK-NEXT: [[GEPI8:%.*]] = getelementptr inbounds i8, i8* [[CASTI8]], i32 [[TMP1]]
292 ; CHECK-NEXT: [[CAST:%.*]] = bitcast i8* [[GEPI8]] to i32**
293 ; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[LD]] to i32
294 ; CHECK-NEXT: [[PTRCAST:%.*]] = inttoptr i32 [[TMP2]] to i32**
295 ; CHECK-NEXT: [[GEPI32:%.*]] = getelementptr inbounds i32*, i32** [[PTRCAST]], i32 1
296 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32** [[GEPI32]], [[CAST]]
297 ; CHECK-NEXT: ret i1 [[CMP]]
301 %ld = load i64, i64* %in, align 8
302 %casti8 = inttoptr i64 %ld to i8*
303 %gepi8 = getelementptr inbounds i8, i8* %casti8, i64 %offset
304 %cast = bitcast i8* %gepi8 to i32**
305 %ptrcast = inttoptr i64 %ld to i32**
306 %gepi32 = getelementptr inbounds i32*, i32** %ptrcast, i64 1
307 %cmp = icmp eq i32** %gepi32, %cast