xref: /aosp_15_r20/external/llvm/test/Transforms/IndVarSimplify/dont-recompute.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -indvars -S | FileCheck %s
2*9880d681SAndroid Build Coastguard Worker
3*9880d681SAndroid Build Coastguard Worker; This tests that the IV is not recomputed outside of the loop when it is known
4*9880d681SAndroid Build Coastguard Worker; to be computed by the loop and used in the loop any way. In the example below
5*9880d681SAndroid Build Coastguard Worker; although a's value can be computed outside of the loop, there is no benefit
6*9880d681SAndroid Build Coastguard Worker; in doing so as it has to be computed by the loop anyway.
7*9880d681SAndroid Build Coastguard Worker;
8*9880d681SAndroid Build Coastguard Worker; extern void func(unsigned val);
9*9880d681SAndroid Build Coastguard Worker;
10*9880d681SAndroid Build Coastguard Worker; void test(unsigned m)
11*9880d681SAndroid Build Coastguard Worker; {
12*9880d681SAndroid Build Coastguard Worker;   unsigned a = 0;
13*9880d681SAndroid Build Coastguard Worker;
14*9880d681SAndroid Build Coastguard Worker;   for (int i=0; i<186; i++) {
15*9880d681SAndroid Build Coastguard Worker;     a += m;
16*9880d681SAndroid Build Coastguard Worker;     func(a);
17*9880d681SAndroid Build Coastguard Worker;   }
18*9880d681SAndroid Build Coastguard Worker;
19*9880d681SAndroid Build Coastguard Worker;   func(a);
20*9880d681SAndroid Build Coastguard Worker; }
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Workerdeclare void @func(i32)
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test(
25*9880d681SAndroid Build Coastguard Workerdefine void @test(i32 %m) nounwind uwtable {
26*9880d681SAndroid Build Coastguard Workerentry:
27*9880d681SAndroid Build Coastguard Worker  br label %for.body
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard Workerfor.body:                                         ; preds = %for.body, %entry
30*9880d681SAndroid Build Coastguard Worker  %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
31*9880d681SAndroid Build Coastguard Worker  %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
32*9880d681SAndroid Build Coastguard Worker  %add = add i32 %a.05, %m
33*9880d681SAndroid Build Coastguard Worker; CHECK: tail call void @func(i32 %add)
34*9880d681SAndroid Build Coastguard Worker  tail call void @func(i32 %add)
35*9880d681SAndroid Build Coastguard Worker  %inc = add nsw i32 %i.06, 1
36*9880d681SAndroid Build Coastguard Worker  %exitcond = icmp eq i32 %inc, 186
37*9880d681SAndroid Build Coastguard Worker  br i1 %exitcond, label %for.end, label %for.body
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Workerfor.end:                                          ; preds = %for.body
40*9880d681SAndroid Build Coastguard Worker; CHECK: for.end:
41*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: mul i32 %m, 186
42*9880d681SAndroid Build Coastguard Worker; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
43*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: tail call void @func(i32 %add.lcssa)
44*9880d681SAndroid Build Coastguard Worker  tail call void @func(i32 %add)
45*9880d681SAndroid Build Coastguard Worker  ret void
46*9880d681SAndroid Build Coastguard Worker}
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test2(
49*9880d681SAndroid Build Coastguard Workerdefine i32 @test2(i32 %m) nounwind uwtable {
50*9880d681SAndroid Build Coastguard Workerentry:
51*9880d681SAndroid Build Coastguard Worker  br label %for.body
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Workerfor.body:                                         ; preds = %for.body, %entry
54*9880d681SAndroid Build Coastguard Worker  %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
55*9880d681SAndroid Build Coastguard Worker  %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
56*9880d681SAndroid Build Coastguard Worker  %add = add i32 %a.05, %m
57*9880d681SAndroid Build Coastguard Worker; CHECK: tail call void @func(i32 %add)
58*9880d681SAndroid Build Coastguard Worker  tail call void @func(i32 %add)
59*9880d681SAndroid Build Coastguard Worker  %inc = add nsw i32 %i.06, 1
60*9880d681SAndroid Build Coastguard Worker  %exitcond = icmp eq i32 %inc, 186
61*9880d681SAndroid Build Coastguard Worker  br i1 %exitcond, label %for.end, label %for.body
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Workerfor.end:                                          ; preds = %for.body
64*9880d681SAndroid Build Coastguard Worker; CHECK: for.end:
65*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: mul i32 %m, 186
66*9880d681SAndroid Build Coastguard Worker; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
67*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i32 %add.lcssa
68*9880d681SAndroid Build Coastguard Worker  ret i32 %add
69*9880d681SAndroid Build Coastguard Worker}
70