xref: /llvm-project/llvm/test/Transforms/SpeculativeExecution/spec.ll (revision a42b0ca3b82f18803fc8a3135a13dd75ef77e8d0)
157bd67abSBjorn Pettersson; RUN: opt < %s -S -passes=speculative-execution \
2cee313d2SEric Christopher; RUN:   -spec-exec-max-speculation-cost 4 -spec-exec-max-not-hoisted 3 \
3cee313d2SEric Christopher; RUN:   | FileCheck %s
4cee313d2SEric Christopher
5cee313d2SEric Christophertarget datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
6cee313d2SEric Christopher
7cee313d2SEric Christopher; Hoist in if-then pattern.
8cee313d2SEric Christopherdefine void @ifThen() {
9cee313d2SEric Christopher; CHECK-LABEL: @ifThen(
10cee313d2SEric Christopher; CHECK: %x = add i32 2, 3
11cee313d2SEric Christopher; CHECK: br i1 true
12cee313d2SEric Christopher  br i1 true, label %a, label %b
13cee313d2SEric Christopher; CHECK: a:
14cee313d2SEric Christophera:
15cee313d2SEric Christopher  %x = add i32 2, 3
16cee313d2SEric Christopher; CHECK: br label
17cee313d2SEric Christopher  br label %b
18cee313d2SEric Christopher; CHECK: b:
19cee313d2SEric Christopherb:
20cee313d2SEric Christopher; CHECK: ret void
21cee313d2SEric Christopher  ret void
22cee313d2SEric Christopher}
23cee313d2SEric Christopher
24cee313d2SEric Christopher; Hoist in if-else pattern.
25cee313d2SEric Christopherdefine void @ifElse() {
26cee313d2SEric Christopher; CHECK-LABEL: @ifElse(
27cee313d2SEric Christopher; CHECK: %x = add i32 2, 3
28cee313d2SEric Christopher; CHECK: br i1 true
29cee313d2SEric Christopher  br i1 true, label %b, label %a
30cee313d2SEric Christopher; CHECK: a:
31cee313d2SEric Christophera:
32cee313d2SEric Christopher  %x = add i32 2, 3
33cee313d2SEric Christopher; CHECK: br label
34cee313d2SEric Christopher  br label %b
35cee313d2SEric Christopher; CHECK: b:
36cee313d2SEric Christopherb:
37cee313d2SEric Christopher; CHECK: ret void
38cee313d2SEric Christopher  ret void
39cee313d2SEric Christopher}
40cee313d2SEric Christopher
41cee313d2SEric Christopher; Hoist in if-then-else pattern if it is equivalent to if-then.
42cee313d2SEric Christopherdefine void @ifElseThenAsIfThen() {
43cee313d2SEric Christopher; CHECK-LABEL: @ifElseThenAsIfThen(
44cee313d2SEric Christopher; CHECK: %x = add i32 2, 3
45cee313d2SEric Christopher; CHECK: br
46cee313d2SEric Christopher  br i1 true, label %a, label %b
47cee313d2SEric Christopher; CHECK: a:
48cee313d2SEric Christophera:
49cee313d2SEric Christopher  %x = add i32 2, 3
50cee313d2SEric Christopher; CHECK: br label
51cee313d2SEric Christopher  br label %c
52cee313d2SEric Christopher; CHECK: b:
53cee313d2SEric Christopherb:
54cee313d2SEric Christopher  br label %c
55cee313d2SEric Christopher; CHECK: c
56cee313d2SEric Christopherc:
57cee313d2SEric Christopher  ret void
58cee313d2SEric Christopher}
59cee313d2SEric Christopher
60cee313d2SEric Christopher; Hoist in if-then-else pattern if it is equivalent to if-else.
61cee313d2SEric Christopherdefine void @ifElseThenAsIfElse() {
62cee313d2SEric Christopher; CHECK-LABEL: @ifElseThenAsIfElse(
63cee313d2SEric Christopher; CHECK: %x = add i32 2, 3
64cee313d2SEric Christopher; CHECK: br
65cee313d2SEric Christopher  br i1 true, label %b, label %a
66cee313d2SEric Christopher; CHECK: a:
67cee313d2SEric Christophera:
68cee313d2SEric Christopher  %x = add i32 2, 3
69cee313d2SEric Christopher; CHECK: br label
70cee313d2SEric Christopher  br label %c
71cee313d2SEric Christopher; CHECK: b:
72cee313d2SEric Christopherb:
73cee313d2SEric Christopher  br label %c
74cee313d2SEric Christopher; CHECK: c
75cee313d2SEric Christopherc:
76cee313d2SEric Christopher  ret void
77cee313d2SEric Christopher}
78cee313d2SEric Christopher
79cee313d2SEric Christopher; Do not hoist if-then-else pattern if it is not equivalent to if-then
80cee313d2SEric Christopher; or if-else.
81cee313d2SEric Christopherdefine void @ifElseThen() {
82cee313d2SEric Christopher; CHECK-LABEL: @ifElseThen(
83cee313d2SEric Christopher; CHECK: br
84cee313d2SEric Christopher  br i1 true, label %a, label %b
85cee313d2SEric Christopher; CHECK: a:
86cee313d2SEric Christophera:
87cee313d2SEric Christopher; CHECK: %x = add
88cee313d2SEric Christopher  %x = add i32 2, 3
89cee313d2SEric Christopher; CHECK: br label
90cee313d2SEric Christopher  br label %c
91cee313d2SEric Christopher; CHECK: b:
92cee313d2SEric Christopherb:
93cee313d2SEric Christopher; CHECK: %y = add
94cee313d2SEric Christopher  %y = add i32 2, 3
95cee313d2SEric Christopher  br label %c
96cee313d2SEric Christopher; CHECK: c
97cee313d2SEric Christopherc:
98cee313d2SEric Christopher  ret void
99cee313d2SEric Christopher}
100cee313d2SEric Christopher
101cee313d2SEric Christopher; Do not hoist loads and do not hoist an instruction past a definition of
102cee313d2SEric Christopher; an operand.
103cee313d2SEric Christopherdefine void @doNotHoistPastDef() {
104cee313d2SEric Christopher; CHECK-LABEL: @doNotHoistPastDef(
105cee313d2SEric Christopher  br i1 true, label %b, label %a
106cee313d2SEric Christopher; CHECK-NOT: load
107cee313d2SEric Christopher; CHECK-NOT: add
108cee313d2SEric Christopher; CHECK: a:
109cee313d2SEric Christophera:
110cee313d2SEric Christopher; CHECK: %def = load
111*a42b0ca3SMatt Arsenault  %def = load i32, ptr null
112cee313d2SEric Christopher; CHECK: %use = add
113cee313d2SEric Christopher  %use = add i32 %def, 0
114cee313d2SEric Christopher  br label %b
115cee313d2SEric Christopher; CHECK: b:
116cee313d2SEric Christopherb:
117cee313d2SEric Christopher  ret void
118cee313d2SEric Christopher}
119cee313d2SEric Christopher
120cee313d2SEric Christopher; Case with nothing to speculate.
121cee313d2SEric Christopherdefine void @nothingToSpeculate() {
122cee313d2SEric Christopher; CHECK-LABEL: @nothingToSpeculate(
123cee313d2SEric Christopher  br i1 true, label %b, label %a
124cee313d2SEric Christopher; CHECK: a:
125cee313d2SEric Christophera:
126cee313d2SEric Christopher; CHECK: %def = load
127*a42b0ca3SMatt Arsenault  %def = load i32, ptr null
128cee313d2SEric Christopher  br label %b
129cee313d2SEric Christopher; CHECK: b:
130cee313d2SEric Christopherb:
131cee313d2SEric Christopher  ret void
132cee313d2SEric Christopher}
133cee313d2SEric Christopher
134cee313d2SEric Christopher; Still hoist if an operand is defined before the block or is itself hoisted.
135cee313d2SEric Christopherdefine void @hoistIfNotPastDef() {
136cee313d2SEric Christopher; CHECK-LABEL: @hoistIfNotPastDef(
137cee313d2SEric Christopher; CHECK: %x = load
138*a42b0ca3SMatt Arsenault  %x = load i32, ptr null
139cee313d2SEric Christopher; CHECK: %y = add i32 %x, 1
140cee313d2SEric Christopher; CHECK: %z = add i32 %y, 1
141cee313d2SEric Christopher; CHECK: br
142cee313d2SEric Christopher  br i1 true, label %b, label %a
143cee313d2SEric Christopher; CHECK: a:
144cee313d2SEric Christophera:
145cee313d2SEric Christopher  %y = add i32 %x, 1
146cee313d2SEric Christopher  %z = add i32 %y, 1
147cee313d2SEric Christopher  br label %b
148cee313d2SEric Christopher; CHECK: b:
149cee313d2SEric Christopherb:
150cee313d2SEric Christopher  ret void
151cee313d2SEric Christopher}
152cee313d2SEric Christopher
153cee313d2SEric Christopher; Do not hoist if the speculation cost is too high.
154cee313d2SEric Christopherdefine void @costTooHigh() {
155cee313d2SEric Christopher; CHECK-LABEL: @costTooHigh(
156cee313d2SEric Christopher; CHECK: br
157cee313d2SEric Christopher  br i1 true, label %b, label %a
158cee313d2SEric Christopher; CHECK: a:
159cee313d2SEric Christophera:
160cee313d2SEric Christopher; CHECK: %r1 = add
161cee313d2SEric Christopher  %r1 = add i32 1, 1
162cee313d2SEric Christopher; CHECK: %r2 = add
163cee313d2SEric Christopher  %r2 = add i32 1, 1
164cee313d2SEric Christopher; CHECK: %r3 = add
165cee313d2SEric Christopher  %r3 = add i32 1, 1
166cee313d2SEric Christopher; CHECK: %r4 = add
167cee313d2SEric Christopher  %r4 = add i32 1, 1
168cee313d2SEric Christopher; CHECK: %r5 = add
169cee313d2SEric Christopher  %r5 = add i32 1, 1
170cee313d2SEric Christopher  br label %b
171cee313d2SEric Christopher; CHECK: b:
172cee313d2SEric Christopherb:
173cee313d2SEric Christopher  ret void
174cee313d2SEric Christopher}
175cee313d2SEric Christopher
176cee313d2SEric Christopher; Do not hoist if too many instructions are left behind.
177cee313d2SEric Christopherdefine void @tooMuchLeftBehind() {
178cee313d2SEric Christopher; CHECK-LABEL: @tooMuchLeftBehind(
179cee313d2SEric Christopher; CHECK: br
180cee313d2SEric Christopher  br i1 true, label %b, label %a
181cee313d2SEric Christopher; CHECK: a:
182cee313d2SEric Christophera:
183cee313d2SEric Christopher; CHECK: %x = load
184*a42b0ca3SMatt Arsenault  %x = load i32, ptr null
185cee313d2SEric Christopher; CHECK: %r1 = add
186cee313d2SEric Christopher  %r1 = add i32 %x, 1
187cee313d2SEric Christopher; CHECK: %r2 = add
188cee313d2SEric Christopher  %r2 = add i32 %x, 1
189cee313d2SEric Christopher; CHECK: %r3 = add
190cee313d2SEric Christopher  %r3 = add i32 %x, 1
191cee313d2SEric Christopher  br label %b
192cee313d2SEric Christopher; CHECK: b:
193cee313d2SEric Christopherb:
194cee313d2SEric Christopher  ret void
195cee313d2SEric Christopher}
196