1 ; RUN: opt < %s -indvars -S | FileCheck %s
3 ; This tests that the IV is not recomputed outside of the loop when it is known
4 ; to be computed by the loop and used in the loop any way. In the example below
5 ; although a's value can be computed outside of the loop, there is no benefit
6 ; in doing so as it has to be computed by the loop anyway.
8 ; extern void func(unsigned val);
10 ; void test(unsigned m)
14 ; for (int i=0; i<186; i++) {
22 declare void @func(i32)
25 define void @test(i32 %m) nounwind uwtable {
29 for.body: ; preds = %for.body, %entry
30 %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
31 %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
32 %add = add i32 %a.05, %m
33 ; CHECK: tail call void @func(i32 %add)
34 tail call void @func(i32 %add)
35 %inc = add nsw i32 %i.06, 1
36 %exitcond = icmp eq i32 %inc, 186
37 br i1 %exitcond, label %for.end, label %for.body
39 for.end: ; preds = %for.body
41 ; CHECK-NOT: mul i32 %m, 186
42 ; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
43 ; CHECK-NEXT: tail call void @func(i32 %add.lcssa)
44 tail call void @func(i32 %add)
48 ; CHECK-LABEL: @test2(
49 define i32 @test2(i32 %m) nounwind uwtable {
53 for.body: ; preds = %for.body, %entry
54 %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
55 %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
56 %add = add i32 %a.05, %m
57 ; CHECK: tail call void @func(i32 %add)
58 tail call void @func(i32 %add)
59 %inc = add nsw i32 %i.06, 1
60 %exitcond = icmp eq i32 %inc, 186
61 br i1 %exitcond, label %for.end, label %for.body
63 for.end: ; preds = %for.body
65 ; CHECK-NOT: mul i32 %m, 186
66 ; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
67 ; CHECK-NEXT: ret i32 %add.lcssa
71 ; CHECK-LABEL: @test3(
72 define void @test3(i32 %m) nounwind uwtable {
76 for.body: ; preds = %for.body, %entry
77 %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
78 %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
79 %add = add i32 %a.05, %m
86 ; CHECK: tail call void @func(i32 %add)
87 tail call void @func(i32 %add)
88 %inc = add nsw i32 %i.06, 1
89 %exitcond = icmp eq i32 %inc, 186
90 br i1 %exitcond, label %for.end, label %for.body
92 for.end: ; preds = %for.body
94 ; CHECK-NOT: mul i32 %m, 186
95 ; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
96 ; CHECK-NEXT: tail call void @func(i32 %add.lcssa)
97 tail call void @func(i32 %add)
101 ; CHECK-LABEL: @test4(
102 define void @test4(i32 %m) nounwind uwtable {
106 for.body: ; preds = %for.body, %entry
107 %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
108 %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
109 %add = add i32 %a.05, %m
110 ; CHECK: tail call void @func(i32 %add)
111 tail call void @func(i32 %add)
112 %inc = add nsw i32 %i.06, 1
113 %exitcond = icmp eq i32 %inc, 186
114 br i1 %exitcond, label %for.end, label %for.body
116 for.end: ; preds = %for.body
118 ; CHECK-NOT: mul i32 %m, 186
119 ; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
120 ; CHECK-NEXT: %soft_use = add i32 %add.lcssa, 123
121 ; CHECK-NEXT: tail call void @func(i32 %soft_use)
122 %soft_use = add i32 %add, 123
123 tail call void @func(i32 %soft_use)
127 ; CHECK-LABEL: @test5(
128 define void @test5(i32 %m) nounwind uwtable {
132 for.body: ; preds = %for.body, %entry
133 %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
134 %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
135 %add = add i32 %a.05, %m
136 %soft_use = add i32 %add, 123
137 ; CHECK: tail call void @func(i32 %soft_use)
138 tail call void @func(i32 %soft_use)
139 %inc = add nsw i32 %i.06, 1
140 %exitcond = icmp eq i32 %inc, 186
141 br i1 %exitcond, label %for.end, label %for.body
143 for.end: ; preds = %for.body
145 ; CHECK-NOT: mul i32 %m, 186
146 ; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
147 ; CHECK-NEXT: tail call void @func(i32 %add.lcssa)
148 tail call void @func(i32 %add)
152 ; CHECK-LABEL: @test6(
153 define void @test6(i32 %m, i32* %p) nounwind uwtable {
157 for.body: ; preds = %for.body, %entry
158 %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
159 %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
160 %add = add i32 %a.05, %m
161 %soft_use = add i32 %add, 123
162 ; CHECK: store i32 %soft_use, i32* %pidx
163 %pidx = getelementptr i32, i32* %p, i32 %add
164 store i32 %soft_use, i32* %pidx
165 %inc = add nsw i32 %i.06, 1
166 %exitcond = icmp eq i32 %inc, 186
167 br i1 %exitcond, label %for.end, label %for.body
169 for.end: ; preds = %for.body
171 ; CHECK-NOT: mul i32 %m, 186
172 ; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
173 ; CHECK-NEXT: tail call void @func(i32 %add.lcssa)
174 tail call void @func(i32 %add)