1 ; RUN: opt -indvars -S < %s | FileCheck %s
3 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
5 @X = external global [0 x double]
7 ; Indvars should be able to simplify simple comparisons involving
11 ; CHECK: %cond = and i1 %tobool.not, true
13 define void @foo(i64 %n, i32* nocapture %p) nounwind {
15 %cmp9 = icmp sgt i64 %n, 0
16 br i1 %cmp9, label %pre, label %return
20 %tobool.not = icmp ne i32 %t3, 0
24 %i = phi i64 [ 0, %pre ], [ %inc, %for.inc ]
25 %cmp6 = icmp slt i64 %i, %n
26 %cond = and i1 %tobool.not, %cmp6
27 br i1 %cond, label %if.then, label %for.inc
30 %arrayidx = getelementptr [0 x double]* @X, i64 0, i64 %i
31 store double 3.200000e+00, double* %arrayidx
35 %inc = add nsw i64 %i, 1
36 %exitcond = icmp sge i64 %inc, %n
37 br i1 %exitcond, label %return, label %loop
43 ; Don't eliminate an icmp that's contributing to the loop exit test though.
45 ; CHECK-LABEL: @_ZNK4llvm5APInt3ultERKS0_(
46 ; CHECK: %tmp99 = icmp sgt i32 %i, -1
48 define i32 @_ZNK4llvm5APInt3ultERKS0_(i32 %tmp2.i1, i64** %tmp65, i64** %tmp73, i64** %tmp82, i64** %tmp90) {
53 %tmp66 = load i64** %tmp65, align 4
54 %tmp68 = getelementptr inbounds i64* %tmp66, i32 %i
55 %tmp69 = load i64* %tmp68, align 4
56 %tmp74 = load i64** %tmp73, align 4
57 %tmp76 = getelementptr inbounds i64* %tmp74, i32 %i
58 %tmp77 = load i64* %tmp76, align 4
59 %tmp78 = icmp ugt i64 %tmp69, %tmp77
60 br i1 %tmp78, label %bb20.loopexit, label %bb15
63 %tmp83 = load i64** %tmp82, align 4
64 %tmp85 = getelementptr inbounds i64* %tmp83, i32 %i
65 %tmp86 = load i64* %tmp85, align 4
66 %tmp91 = load i64** %tmp90, align 4
67 %tmp93 = getelementptr inbounds i64* %tmp91, i32 %i
68 %tmp94 = load i64* %tmp93, align 4
69 %tmp95 = icmp ult i64 %tmp86, %tmp94
70 br i1 %tmp95, label %bb20.loopexit, label %bb17
73 %tmp97 = add nsw i32 %i, -1
77 %i = phi i32 [ %tmp2.i1, %entry ], [ %tmp97, %bb17 ]
78 %tmp99 = icmp sgt i32 %i, -1
79 br i1 %tmp99, label %bb13, label %bb20.loopexit
82 %tmp.0.ph = phi i32 [ 0, %bb18 ], [ 1, %bb15 ], [ 0, %bb13 ]
86 ; Indvars should eliminate the icmp here.
88 ; CHECK-LABEL: @func_10(
92 define void @func_10() nounwind {
97 %i = phi i32 [ %i.next, %loop ], [ 0, %entry ]
98 %t0 = icmp slt i32 %i, 0
99 %t1 = zext i1 %t0 to i32
100 %t2 = add i32 %t1, %i
101 %u3 = zext i32 %t2 to i64
102 store i64 %u3, i64* null
103 %i.next = add i32 %i, 1
104 br i1 undef, label %loop, label %return
111 ; Indvars should not turn the second loop into an infinite one.
113 ; CHECK-LABEL: @func_11(
114 ; CHECK: %tmp5 = icmp slt i32 %__key6.0, 10
115 ; CHECK-NOT: br i1 true, label %noassert68, label %unrolledend
117 define i32 @func_11() nounwind uwtable {
121 forcond: ; preds = %noassert, %entry
122 %__key6.0 = phi i32 [ 2, %entry ], [ %tmp37, %noassert ]
123 %tmp5 = icmp slt i32 %__key6.0, 10
124 br i1 %tmp5, label %noassert, label %forcond38.preheader
126 forcond38.preheader: ; preds = %forcond
129 noassert: ; preds = %forbody
130 %tmp13 = sdiv i32 -32768, %__key6.0
131 %tmp2936 = shl i32 %tmp13, 24
132 %sext23 = shl i32 %tmp13, 24
133 %tmp32 = icmp eq i32 %tmp2936, %sext23
134 %tmp37 = add i32 %__key6.0, 1
135 br i1 %tmp32, label %forcond, label %assert33
137 assert33: ; preds = %noassert
138 tail call void @llvm.trap()
141 forcond38: ; preds = %noassert68, %forcond38.preheader
142 %__key8.0 = phi i32 [ %tmp81, %noassert68 ], [ 2, %forcond38.preheader ]
143 %tmp46 = icmp slt i32 %__key8.0, 10
144 br i1 %tmp46, label %noassert68, label %unrolledend
146 noassert68: ; preds = %forbody39
147 %tmp57 = sdiv i32 -32768, %__key8.0
148 %sext34 = shl i32 %tmp57, 16
149 %sext21 = shl i32 %tmp57, 16
150 %tmp76 = icmp eq i32 %sext34, %sext21
151 %tmp81 = add i32 %__key8.0, 1
152 br i1 %tmp76, label %forcond38, label %assert77
154 assert77: ; preds = %noassert68
155 tail call void @llvm.trap()
158 unrolledend: ; preds = %forcond38
162 declare void @llvm.trap() noreturn nounwind
164 ; In this case the second loop only has a single iteration, fold the header away
165 ; CHECK-LABEL: @func_12(
166 ; CHECK: %tmp5 = icmp slt i32 %__key6.0, 10
167 ; CHECK: br i1 true, label %noassert68, label %unrolledend
168 define i32 @func_12() nounwind uwtable {
172 forcond: ; preds = %noassert, %entry
173 %__key6.0 = phi i32 [ 2, %entry ], [ %tmp37, %noassert ]
174 %tmp5 = icmp slt i32 %__key6.0, 10
175 br i1 %tmp5, label %noassert, label %forcond38.preheader
177 forcond38.preheader: ; preds = %forcond
180 noassert: ; preds = %forbody
181 %tmp13 = sdiv i32 -32768, %__key6.0
182 %tmp2936 = shl i32 %tmp13, 24
183 %sext23 = shl i32 %tmp13, 24
184 %tmp32 = icmp eq i32 %tmp2936, %sext23
185 %tmp37 = add i32 %__key6.0, 1
186 br i1 %tmp32, label %forcond, label %assert33
188 assert33: ; preds = %noassert
189 tail call void @llvm.trap()
192 forcond38: ; preds = %noassert68, %forcond38.preheader
193 %__key8.0 = phi i32 [ %tmp81, %noassert68 ], [ 2, %forcond38.preheader ]
194 %tmp46 = icmp slt i32 %__key8.0, 10
195 br i1 %tmp46, label %noassert68, label %unrolledend
197 noassert68: ; preds = %forbody39
198 %tmp57 = sdiv i32 -32768, %__key8.0
199 %sext34 = shl i32 %tmp57, 16
200 %sext21 = shl i32 %tmp57, 16
201 %tmp76 = icmp ne i32 %sext34, %sext21
202 %tmp81 = add i32 %__key8.0, 1
203 br i1 %tmp76, label %forcond38, label %assert77
205 assert77: ; preds = %noassert68
206 tail call void @llvm.trap()
209 unrolledend: ; preds = %forcond38