1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -codegenprepare -S < %s | FileCheck %s
3 ; RUN: opt -debugify -codegenprepare -S < %s | FileCheck %s -check-prefix=DEBUG
5 target triple = "x86_64-unknown-unknown"
7 ; Nothing to sink and convert here.
9 define i32 @no_sink(double %a, double* %b, i32 %x, i32 %y) {
10 ; CHECK-LABEL: @no_sink(
12 ; CHECK-NEXT: [[LOAD:%.*]] = load double, double* [[B:%.*]], align 8
13 ; CHECK-NEXT: [[CMP:%.*]] = fcmp olt double [[LOAD]], [[A:%.*]]
14 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[X:%.*]], i32 [[Y:%.*]]
15 ; CHECK-NEXT: ret i32 [[SEL]]
18 %load = load double, double* %b, align 8
19 %cmp = fcmp olt double %load, %a
20 %sel = select i1 %cmp, i32 %x, i32 %y
25 ; An 'fdiv' is expensive, so sink it rather than speculatively execute it.
27 define float @fdiv_true_sink(float %a, float %b) {
28 ; CHECK-LABEL: @fdiv_true_sink(
30 ; CHECK-NEXT: [[SEL_FR:%.*]] = freeze float [[A:%.*]]
31 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[SEL_FR]], 1.000000e+00
32 ; CHECK-NEXT: br i1 [[CMP]], label [[SELECT_TRUE_SINK:%.*]], label [[SELECT_END:%.*]]
33 ; CHECK: select.true.sink:
34 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]]
35 ; CHECK-NEXT: br label [[SELECT_END]]
37 ; CHECK-NEXT: [[SEL:%.*]] = phi float [ [[DIV]], [[SELECT_TRUE_SINK]] ], [ 2.000000e+00, [[ENTRY:%.*]] ]
38 ; CHECK-NEXT: ret float [[SEL]]
40 ; DEBUG-LABEL: @fdiv_true_sink(
42 ; DEBUG-NEXT: [[SEL_FR:%.*]] = freeze float [[A:%.*]]
43 ; DEBUG-NEXT: [[CMP:%.*]] = fcmp ogt float [[SEL_FR]], 1.000000e+00, !dbg !24
44 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata i1 [[CMP]]
45 ; DEBUG-NEXT: br i1 [[CMP]], label [[SELECT_TRUE_SINK:%.*]], label [[SELECT_END:%.*]], !dbg
46 ; DEBUG: select.true.sink:
47 ; DEBUG-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]]
48 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[DIV]]
49 ; DEBUG-NEXT: br label [[SELECT_END]], !dbg
51 ; DEBUG-NEXT: [[SEL:%.*]] = phi float [ [[DIV]], [[SELECT_TRUE_SINK]] ], [ 2.000000e+00, [[ENTRY:%.*]] ], !dbg
52 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[SEL]]
53 ; DEBUG-NEXT: ret float [[SEL]]
56 %div = fdiv float %a, %b
57 %cmp = fcmp ogt float %a, 1.0
58 %sel = select i1 %cmp, float %div, float 2.0
62 define float @fdiv_false_sink(float %a, float %b) {
63 ; CHECK-LABEL: @fdiv_false_sink(
65 ; CHECK-NEXT: [[SEL_FR:%.*]] = freeze float [[A:%.*]]
66 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[SEL_FR]], 3.000000e+00
67 ; CHECK-NEXT: br i1 [[CMP]], label [[SELECT_END:%.*]], label [[SELECT_FALSE_SINK:%.*]]
68 ; CHECK: select.false.sink:
69 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]]
70 ; CHECK-NEXT: br label [[SELECT_END]]
72 ; CHECK-NEXT: [[SEL:%.*]] = phi float [ 4.000000e+00, [[ENTRY:%.*]] ], [ [[DIV]], [[SELECT_FALSE_SINK]] ]
73 ; CHECK-NEXT: ret float [[SEL]]
75 ; DEBUG-LABEL: @fdiv_false_sink(
77 ; DEBUG-NEXT: [[SEL_FR:%.*]] = freeze float [[A:%.*]]
78 ; DEBUG-NEXT: [[CMP:%.*]] = fcmp ogt float [[SEL_FR]], 3.000000e+00, !dbg !33
79 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata i1 [[CMP]]
80 ; DEBUG-NEXT: br i1 [[CMP]], label [[SELECT_END:%.*]], label [[SELECT_FALSE_SINK:%.*]], !dbg
81 ; DEBUG: select.false.sink:
82 ; DEBUG-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]]
83 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[DIV]]
84 ; DEBUG-NEXT: br label [[SELECT_END]], !dbg
86 ; DEBUG-NEXT: [[SEL:%.*]] = phi float [ 4.000000e+00, [[ENTRY:%.*]] ], [ [[DIV]], [[SELECT_FALSE_SINK]] ], !dbg
87 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[SEL]]
88 ; DEBUG-NEXT: ret float [[SEL]], !dbg
91 %div = fdiv float %a, %b
92 %cmp = fcmp ogt float %a, 3.0
93 %sel = select i1 %cmp, float 4.0, float %div
97 define float @fdiv_both_sink(float %a, float %b) {
98 ; CHECK-LABEL: @fdiv_both_sink(
100 ; CHECK-NEXT: [[SEL_FR:%.*]] = freeze float [[A:%.*]]
101 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[SEL_FR]], 5.000000e+00
102 ; CHECK-NEXT: br i1 [[CMP]], label [[SELECT_TRUE_SINK:%.*]], label [[SELECT_FALSE_SINK:%.*]]
103 ; CHECK: select.true.sink:
104 ; CHECK-NEXT: [[DIV1:%.*]] = fdiv float [[A]], [[B:%.*]]
105 ; CHECK-NEXT: br label [[SELECT_END:%.*]]
106 ; CHECK: select.false.sink:
107 ; CHECK-NEXT: [[DIV2:%.*]] = fdiv float [[B]], [[A]]
108 ; CHECK-NEXT: br label [[SELECT_END]]
110 ; CHECK-NEXT: [[SEL:%.*]] = phi float [ [[DIV1]], [[SELECT_TRUE_SINK]] ], [ [[DIV2]], [[SELECT_FALSE_SINK]] ]
111 ; CHECK-NEXT: ret float [[SEL]]
114 %div1 = fdiv float %a, %b
115 %div2 = fdiv float %b, %a
116 %cmp = fcmp ogt float %a, 5.0
117 %sel = select i1 %cmp, float %div1, float %div2
121 ; But if the select is marked unpredictable, then don't turn it into a branch.
123 define float @unpredictable_select(float %a, float %b) {
124 ; CHECK-LABEL: @unpredictable_select(
126 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A:%.*]], [[B:%.*]]
127 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[A]], 1.000000e+00
128 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[DIV]], float 2.000000e+00, !unpredictable !0
129 ; CHECK-NEXT: ret float [[SEL]]
132 %div = fdiv float %a, %b
133 %cmp = fcmp ogt float %a, 1.0
134 %sel = select i1 %cmp, float %div, float 2.0, !unpredictable !0
140 ; An 'fadd' is not too expensive, so it's ok to speculate.
142 define float @fadd_no_sink(float %a, float %b) {
143 ; CHECK-LABEL: @fadd_no_sink(
144 ; CHECK-NEXT: [[ADD:%.*]] = fadd float [[A:%.*]], [[B:%.*]]
145 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float 6.000000e+00, [[A]]
146 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[ADD]], float 7.000000e+00
147 ; CHECK-NEXT: ret float [[SEL]]
149 %add = fadd float %a, %b
150 %cmp = fcmp ogt float 6.0, %a
151 %sel = select i1 %cmp, float %add, float 7.0
155 ; Possible enhancement: sinkability is only calculated with the direct
156 ; operand of the select, so we don't try to sink this. The fdiv cost is not
157 ; taken into account.
159 define float @fdiv_no_sink(float %a, float %b) {
160 ; CHECK-LABEL: @fdiv_no_sink(
162 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A:%.*]], [[B:%.*]]
163 ; CHECK-NEXT: [[ADD:%.*]] = fadd float [[DIV]], [[B]]
164 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[A]], 1.000000e+00
165 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[ADD]], float 8.000000e+00
166 ; CHECK-NEXT: ret float [[SEL]]
169 %div = fdiv float %a, %b
170 %add = fadd float %div, %b
171 %cmp = fcmp ogt float %a, 1.0
172 %sel = select i1 %cmp, float %add, float 8.0
176 ; Do not transform the CFG if the select operands may have side effects.
178 declare i64* @bar(i32, i32, i32)
179 declare i64* @baz(i32, i32, i32)
181 define i64* @calls_no_sink(i32 %in) {
182 ; CHECK-LABEL: @calls_no_sink(
183 ; CHECK-NEXT: [[CALL1:%.*]] = call i64* @bar(i32 1, i32 2, i32 3)
184 ; CHECK-NEXT: [[CALL2:%.*]] = call i64* @baz(i32 1, i32 2, i32 3)
185 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32 [[IN:%.*]], 0
186 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TOBOOL]], i64* [[CALL1]], i64* [[CALL2]]
187 ; CHECK-NEXT: ret i64* [[SEL]]
189 %call1 = call i64* @bar(i32 1, i32 2, i32 3)
190 %call2 = call i64* @baz(i32 1, i32 2, i32 3)
191 %tobool = icmp ne i32 %in, 0
192 %sel = select i1 %tobool, i64* %call1, i64* %call2
196 define i32 @sdiv_no_sink(i32 %a, i32 %b) {
197 ; CHECK-LABEL: @sdiv_no_sink(
198 ; CHECK-NEXT: [[DIV1:%.*]] = sdiv i32 [[A:%.*]], [[B:%.*]]
199 ; CHECK-NEXT: [[DIV2:%.*]] = sdiv i32 [[B]], [[A]]
200 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A]], 5
201 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[DIV1]], i32 [[DIV2]]
202 ; CHECK-NEXT: ret i32 [[SEL]]
204 %div1 = sdiv i32 %a, %b
205 %div2 = sdiv i32 %b, %a
206 %cmp = icmp sgt i32 %a, 5
207 %sel = select i1 %cmp, i32 %div1, i32 %div2