1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
| ; RUN: opt -passes='print<scalar-evolution>,loop(unswitch<nontrivial>,loop-instsimplify),print<scalar-evolution>' -S < %s 2>%t.scev | FileCheck %s
; RUN: opt -verify-memoryssa -passes='print<scalar-evolution>,loop-mssa(unswitch<nontrivial>,loop-instsimplify),print<scalar-evolution>' -S < %s 2>%t.scev | FileCheck %s
; RUN: FileCheck %s --check-prefix=SCEV < %t.scev
target triple = "x86_64-unknown-linux-gnu"
declare void @f()
; Check that trivially unswitching an inner loop resets both the inner and outer
; loop trip count.
define void @test1(i32 %n, i32 %m, i1 %cond) {
; Check that SCEV has no trip count before unswitching.
; SCEV-LABEL: Determining loop execution counts for: @test1
; SCEV: Loop %inner_loop_begin: <multiple exits> Unpredictable backedge-taken count.
; SCEV: Loop %outer_loop_begin: Unpredictable backedge-taken count.
;
; Now check that after unswitching and simplifying instructions we get clean
; backedge-taken counts.
; SCEV-LABEL: Determining loop execution counts for: @test1
; SCEV: Loop %inner_loop_begin: backedge-taken count is (-1 + (1 smax %m))<nsw>
; SCEV: Loop %outer_loop_begin: backedge-taken count is (-1 + (1 smax %n))<nsw>
;
; And verify the code matches what we expect.
; CHECK-LABEL: define void @test1(
entry:
br label %outer_loop_begin
; Ensure the outer loop didn't get unswitched.
; CHECK: entry:
; CHECK-NEXT: br label %outer_loop_begin
outer_loop_begin:
%i = phi i32 [ %i.next, %outer_loop_latch ], [ 0, %entry ]
; Block unswitching of the outer loop with a noduplicate call.
call void @f() noduplicate
br label %inner_loop_begin
; Ensure the inner loop got unswitched into the outer loop.
; CHECK: outer_loop_begin:
; CHECK-NEXT: %{{.*}} = phi i32
; CHECK-NEXT: call void @f()
; CHECK-NEXT: br i1 %cond,
inner_loop_begin:
%j = phi i32 [ %j.next, %inner_loop_latch ], [ 0, %outer_loop_begin ]
br i1 %cond, label %inner_loop_latch, label %inner_loop_early_exit
inner_loop_latch:
%j.next = add nsw i32 %j, 1
%j.cmp = icmp slt i32 %j.next, %m
br i1 %j.cmp, label %inner_loop_begin, label %inner_loop_late_exit
inner_loop_early_exit:
%j.lcssa = phi i32 [ %i, %inner_loop_begin ]
br label %outer_loop_latch
inner_loop_late_exit:
br label %outer_loop_latch
outer_loop_latch:
%i.phi = phi i32 [ %j.lcssa, %inner_loop_early_exit ], [ %i, %inner_loop_late_exit ]
%i.next = add nsw i32 %i.phi, 1
%i.cmp = icmp slt i32 %i.next, %n
br i1 %i.cmp, label %outer_loop_begin, label %exit
exit:
ret void
}
; Check that trivially unswitching an inner loop resets both the inner and outer
; loop trip count.
define void @test2(i32 %n, i32 %m, i32 %cond) {
; Check that SCEV has no trip count before unswitching.
; SCEV-LABEL: Determining loop execution counts for: @test2
; SCEV: Loop %inner_loop_begin: <multiple exits> Unpredictable backedge-taken count.
; SCEV: Loop %outer_loop_begin: Unpredictable backedge-taken count.
;
; Now check that after unswitching and simplifying instructions we get clean
; backedge-taken counts.
; SCEV-LABEL: Determining loop execution counts for: @test2
; SCEV: Loop %inner_loop_begin: backedge-taken count is (-1 + (1 smax %m))<nsw>
; SCEV: Loop %outer_loop_begin: backedge-taken count is (-1 + (1 smax %n))<nsw>
;
; CHECK-LABEL: define void @test2(
entry:
br label %outer_loop_begin
; Ensure the outer loop didn't get unswitched.
; CHECK: entry:
; CHECK-NEXT: br label %outer_loop_begin
outer_loop_begin:
%i = phi i32 [ %i.next, %outer_loop_latch ], [ 0, %entry ]
; Block unswitching of the outer loop with a noduplicate call.
call void @f() noduplicate
br label %inner_loop_begin
; Ensure the inner loop got unswitched into the outer loop.
; CHECK: outer_loop_begin:
; CHECK-NEXT: %{{.*}} = phi i32
; CHECK-NEXT: call void @f()
; CHECK-NEXT: switch i32 %cond,
inner_loop_begin:
%j = phi i32 [ %j.next, %inner_loop_latch ], [ 0, %outer_loop_begin ]
switch i32 %cond, label %inner_loop_early_exit [
i32 1, label %inner_loop_latch
i32 2, label %inner_loop_latch
]
inner_loop_latch:
%j.next = add nsw i32 %j, 1
%j.cmp = icmp slt i32 %j.next, %m
br i1 %j.cmp, label %inner_loop_begin, label %inner_loop_late_exit
inner_loop_early_exit:
%j.lcssa = phi i32 [ %i, %inner_loop_begin ]
br label %outer_loop_latch
inner_loop_late_exit:
br label %outer_loop_latch
outer_loop_latch:
%i.phi = phi i32 [ %j.lcssa, %inner_loop_early_exit ], [ %i, %inner_loop_late_exit ]
%i.next = add nsw i32 %i.phi, 1
%i.cmp = icmp slt i32 %i.next, %n
br i1 %i.cmp, label %outer_loop_begin, label %exit
exit:
ret void
}
; Check that non-trivial unswitching of a branch in an inner loop into the outer
; loop invalidates both inner and outer.
define void @test3(i32 %n, i32 %m, i1 %cond) {
; Check that SCEV has no trip count before unswitching.
; SCEV-LABEL: Determining loop execution counts for: @test3
; SCEV: Loop %inner_loop_begin: <multiple exits> Unpredictable backedge-taken count.
; SCEV: Loop %outer_loop_begin: Unpredictable backedge-taken count.
;
; Now check that after unswitching and simplifying instructions we get clean
; backedge-taken counts.
; SCEV-LABEL: Determining loop execution counts for: @test3
; SCEV: Loop %inner_loop_begin{{.*}}: backedge-taken count is (-1 + (1 smax %m))<nsw>
; SCEV: Loop %outer_loop_begin: backedge-taken count is (-1 + (1 smax %n))<nsw>
;
; And verify the code matches what we expect.
; CHECK-LABEL: define void @test3(
entry:
br label %outer_loop_begin
; Ensure the outer loop didn't get unswitched.
; CHECK: entry:
; CHECK-NEXT: br label %outer_loop_begin
outer_loop_begin:
%i = phi i32 [ %i.next, %outer_loop_latch ], [ 0, %entry ]
; Block unswitching of the outer loop with a noduplicate call.
call void @f() noduplicate
br label %inner_loop_begin
; Ensure the inner loop got unswitched into the outer loop.
; CHECK: outer_loop_begin:
; CHECK-NEXT: %{{.*}} = phi i32
; CHECK-NEXT: call void @f()
; CHECK-NEXT: br i1 %cond,
inner_loop_begin:
%j = phi i32 [ %j.next, %inner_loop_latch ], [ 0, %outer_loop_begin ]
%j.tmp = add nsw i32 %j, 1
br i1 %cond, label %inner_loop_latch, label %inner_loop_early_exit
inner_loop_latch:
%j.next = add nsw i32 %j, 1
%j.cmp = icmp slt i32 %j.next, %m
br i1 %j.cmp, label %inner_loop_begin, label %inner_loop_late_exit
inner_loop_early_exit:
%j.lcssa = phi i32 [ %j.tmp, %inner_loop_begin ]
br label %outer_loop_latch
inner_loop_late_exit:
br label %outer_loop_latch
outer_loop_latch:
%inc.phi = phi i32 [ %j.lcssa, %inner_loop_early_exit ], [ 1, %inner_loop_late_exit ]
%i.next = add nsw i32 %i, %inc.phi
%i.cmp = icmp slt i32 %i.next, %n
br i1 %i.cmp, label %outer_loop_begin, label %exit
exit:
ret void
}
|