basic.ll
5.52 KB
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
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S %s -passes=loop-instsimplify | FileCheck %s
; RUN: opt -S %s -passes='loop-mssa(loop-instsimplify)' -verify-memoryssa | FileCheck %s
; Test very basic folding and propagation occurs within a loop body. This should
; collapse to the loop iteration structure and the LCSSA PHI node.
define i32 @test1(i32 %n, i32 %x) {
; CHECK-LABEL: @test1(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[I_NEXT:%.*]], [[LOOP]] ]
; CHECK-NEXT: [[I_NEXT]] = add nsw i32 [[I]], 1
; CHECK-NEXT: [[I_CMP:%.*]] = icmp slt i32 [[I_NEXT]], [[N:%.*]]
; CHECK-NEXT: br i1 [[I_CMP]], label [[LOOP]], label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: [[X_LCSSA:%.*]] = phi i32 [ [[X:%.*]], [[LOOP]] ]
; CHECK-NEXT: ret i32 [[X_LCSSA]]
;
entry:
br label %loop
loop:
%i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
%x.add = add nsw i32 %x, 0
%x.sub = sub i32 %x.add, 0
%x.and = and i32 %x.sub, -1
%i.next = add nsw i32 %i, 1
%i.cmp = icmp slt i32 %i.next, %n
br i1 %i.cmp, label %loop, label %exit
exit:
%x.lcssa = phi i32 [ %x.and, %loop ]
ret i32 %x.lcssa
}
; Test basic loop structure that still has a simplification feed a prior PHI.
define i32 @test2(i32 %n, i32 %x) {
; CHECK-LABEL: @test2(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[I_NEXT:%.*]], [[LOOP]] ]
; CHECK-NEXT: [[I_NEXT]] = add nsw i32 [[I]], 1
; CHECK-NEXT: [[I_CMP:%.*]] = icmp slt i32 [[I_NEXT]], [[N:%.*]]
; CHECK-NEXT: br i1 [[I_CMP]], label [[LOOP]], label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: [[X_LCSSA:%.*]] = phi i32 [ [[X:%.*]], [[LOOP]] ]
; CHECK-NEXT: ret i32 [[X_LCSSA]]
;
entry:
br label %loop
loop:
%i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
%x.loop = phi i32 [ %x, %entry ], [ %x.next, %loop ]
%x.next = add nsw i32 %x.loop, 0
%i.next = add nsw i32 %i, 1
%i.cmp = icmp slt i32 %i.next, %n
br i1 %i.cmp, label %loop, label %exit
exit:
%x.lcssa = phi i32 [ %x.loop, %loop ]
ret i32 %x.lcssa
}
; Test a diamond CFG with inner PHI nodes.
define i32 @test3(i32 %n, i32 %x) {
; CHECK-LABEL: @test3(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[I_NEXT:%.*]], [[LOOP_LATCH:%.*]] ]
; CHECK-NEXT: [[X_CMP:%.*]] = icmp slt i32 [[I]], 42
; CHECK-NEXT: br i1 [[X_CMP]], label [[LOOP_LHS:%.*]], label [[LOOP_RHS:%.*]]
; CHECK: loop.lhs:
; CHECK-NEXT: br label [[LOOP_LATCH]]
; CHECK: loop.rhs:
; CHECK-NEXT: br label [[LOOP_LATCH]]
; CHECK: loop.latch:
; CHECK-NEXT: [[I_NEXT]] = add nsw i32 [[I]], 1
; CHECK-NEXT: [[I_CMP:%.*]] = icmp slt i32 [[I_NEXT]], [[N:%.*]]
; CHECK-NEXT: br i1 [[I_CMP]], label [[LOOP]], label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: [[X_LCSSA:%.*]] = phi i32 [ [[X:%.*]], [[LOOP_LATCH]] ]
; CHECK-NEXT: ret i32 [[X_LCSSA]]
;
entry:
br label %loop
loop:
%i = phi i32 [ 0, %entry ], [ %i.next, %loop.latch ]
%x.loop = phi i32 [ %x, %entry ], [ %x.phi, %loop.latch ]
%x.add = add nsw i32 %x.loop, 0
%x.cmp = icmp slt i32 %i, 42
br i1 %x.cmp, label %loop.lhs, label %loop.rhs
loop.lhs:
%x.l.add = add nsw i32 %x.add, 0
br label %loop.latch
loop.rhs:
%x.r.sub = sub nsw i32 %x.add, 0
br label %loop.latch
loop.latch:
%x.phi = phi i32 [ %x.l.add, %loop.lhs ], [ %x.r.sub, %loop.rhs ]
%i.next = add nsw i32 %i, 1
%i.cmp = icmp slt i32 %i.next, %n
br i1 %i.cmp, label %loop, label %exit
exit:
%x.lcssa = phi i32 [ %x.loop, %loop.latch ]
ret i32 %x.lcssa
}
; Test an inner loop that is only simplified when processing the outer loop, and
; an outer loop only simplified when processing the inner loop.
define i32 @test4(i32 %n, i32 %m, i32 %x) {
; CHECK-LABEL: @test4(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[I_NEXT:%.*]], [[LOOP_LATCH:%.*]] ]
; CHECK-NEXT: br label [[LOOP_INNER:%.*]]
; CHECK: loop.inner:
; CHECK-NEXT: [[J:%.*]] = phi i32 [ 0, [[LOOP]] ], [ [[J_NEXT:%.*]], [[LOOP_INNER]] ]
; CHECK-NEXT: [[J_NEXT]] = add nsw i32 [[J]], 1
; CHECK-NEXT: [[J_CMP:%.*]] = icmp slt i32 [[J_NEXT]], [[M:%.*]]
; CHECK-NEXT: br i1 [[J_CMP]], label [[LOOP_INNER]], label [[LOOP_LATCH]]
; CHECK: loop.latch:
; CHECK-NEXT: [[I_NEXT]] = add nsw i32 [[I]], 1
; CHECK-NEXT: [[I_CMP:%.*]] = icmp slt i32 [[I_NEXT]], [[N:%.*]]
; CHECK-NEXT: br i1 [[I_CMP]], label [[LOOP]], label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: [[X_LCSSA:%.*]] = phi i32 [ [[X:%.*]], [[LOOP_LATCH]] ]
; CHECK-NEXT: ret i32 [[X_LCSSA]]
;
entry:
br label %loop
loop:
%i = phi i32 [ 0, %entry ], [ %i.next, %loop.latch ]
%x.loop = phi i32 [ %x, %entry ], [ %x.inner.lcssa, %loop.latch ]
%x.add = add nsw i32 %x.loop, 0
br label %loop.inner
loop.inner:
%j = phi i32 [ 0, %loop ], [ %j.next, %loop.inner ]
%x.inner.loop = phi i32 [ %x.add, %loop ], [ %x.inner.add, %loop.inner ]
%x.inner.add = add nsw i32 %x.inner.loop, 0
%j.next = add nsw i32 %j, 1
%j.cmp = icmp slt i32 %j.next, %m
br i1 %j.cmp, label %loop.inner, label %loop.latch
loop.latch:
%x.inner.lcssa = phi i32 [ %x.inner.loop, %loop.inner ]
%i.next = add nsw i32 %i, 1
%i.cmp = icmp slt i32 %i.next, %n
br i1 %i.cmp, label %loop, label %exit
exit:
%x.lcssa = phi i32 [ %x.loop, %loop.latch ]
ret i32 %x.lcssa
}