1; RUN: opt < %s -indvars -S | FileCheck %s
2
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.
7;
8; extern void func(unsigned val);
9;
10; void test(unsigned m)
11; {
12;   unsigned a = 0;
13;
14;   for (int i=0; i<186; i++) {
15;     a += m;
16;     func(a);
17;   }
18;
19;   func(a);
20; }
21
22declare void @func(i32)
23
24; CHECK-LABEL: @test(
25define void @test(i32 %m) nounwind uwtable {
26entry:
27  br label %for.body
28
29for.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
38
39for.end:                                          ; preds = %for.body
40; CHECK: for.end:
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)
45  ret void
46}
47
48; CHECK-LABEL: @test2(
49define i32 @test2(i32 %m) nounwind uwtable {
50entry:
51  br label %for.body
52
53for.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
62
63for.end:                                          ; preds = %for.body
64; CHECK: for.end:
65; CHECK-NOT: mul i32 %m, 186
66; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
67; CHECK-NEXT: ret i32 %add.lcssa
68  ret i32 %add
69}
70