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: [[CMP:%.*]] = fcmp ogt float [[A:%.*]], 1.000000e+00
31 ; CHECK-NEXT: br i1 [[CMP]], label [[SELECT_TRUE_SINK:%.*]], label [[SELECT_END:%.*]]
32 ; CHECK: select.true.sink:
33 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]]
34 ; CHECK-NEXT: br label [[SELECT_END]]
36 ; CHECK-NEXT: [[SEL:%.*]] = phi float [ [[DIV]], [[SELECT_TRUE_SINK]] ], [ 2.000000e+00, [[ENTRY:%.*]] ]
37 ; CHECK-NEXT: ret float [[SEL]]
39 ; DEBUG-LABEL: @fdiv_true_sink(
41 ; DEBUG-NEXT: [[CMP:%.*]] = fcmp ogt float [[A:%.*]], 1.000000e+00
42 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata i1 [[CMP]]
43 ; DEBUG-NEXT: br i1 [[CMP]], label [[SELECT_TRUE_SINK:%.*]], label [[SELECT_END:%.*]], !dbg
44 ; DEBUG: select.true.sink:
45 ; DEBUG-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]]
46 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[DIV]]
47 ; DEBUG-NEXT: br label [[SELECT_END]], !dbg
49 ; DEBUG-NEXT: [[SEL:%.*]] = phi float [ [[DIV]], [[SELECT_TRUE_SINK]] ], [ 2.000000e+00, [[ENTRY:%.*]] ], !dbg
50 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[SEL]]
51 ; DEBUG-NEXT: ret float [[SEL]]
54 %div = fdiv float %a, %b
55 %cmp = fcmp ogt float %a, 1.0
56 %sel = select i1 %cmp, float %div, float 2.0
60 define float @fdiv_false_sink(float %a, float %b) {
61 ; CHECK-LABEL: @fdiv_false_sink(
63 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[A:%.*]], 3.000000e+00
64 ; CHECK-NEXT: br i1 [[CMP]], label [[SELECT_END:%.*]], label [[SELECT_FALSE_SINK:%.*]]
65 ; CHECK: select.false.sink:
66 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]]
67 ; CHECK-NEXT: br label [[SELECT_END]]
69 ; CHECK-NEXT: [[SEL:%.*]] = phi float [ 4.000000e+00, [[ENTRY:%.*]] ], [ [[DIV]], [[SELECT_FALSE_SINK]] ]
70 ; CHECK-NEXT: ret float [[SEL]]
72 ; DEBUG-LABEL: @fdiv_false_sink(
74 ; DEBUG-NEXT: [[CMP:%.*]] = fcmp ogt float [[A:%.*]], 3.000000e+00
75 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata i1 [[CMP]]
76 ; DEBUG-NEXT: br i1 [[CMP]], label [[SELECT_END:%.*]], label [[SELECT_FALSE_SINK:%.*]], !dbg
77 ; DEBUG: select.false.sink:
78 ; DEBUG-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]]
79 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[DIV]]
80 ; DEBUG-NEXT: br label [[SELECT_END]], !dbg
82 ; DEBUG-NEXT: [[SEL:%.*]] = phi float [ 4.000000e+00, [[ENTRY:%.*]] ], [ [[DIV]], [[SELECT_FALSE_SINK]] ], !dbg
83 ; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[SEL]]
84 ; DEBUG-NEXT: ret float [[SEL]], !dbg
87 %div = fdiv float %a, %b
88 %cmp = fcmp ogt float %a, 3.0
89 %sel = select i1 %cmp, float 4.0, float %div
93 define float @fdiv_both_sink(float %a, float %b) {
94 ; CHECK-LABEL: @fdiv_both_sink(
96 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[A:%.*]], 5.000000e+00
97 ; CHECK-NEXT: br i1 [[CMP]], label [[SELECT_TRUE_SINK:%.*]], label [[SELECT_FALSE_SINK:%.*]]
98 ; CHECK: select.true.sink:
99 ; CHECK-NEXT: [[DIV1:%.*]] = fdiv float [[A]], [[B:%.*]]
100 ; CHECK-NEXT: br label [[SELECT_END:%.*]]
101 ; CHECK: select.false.sink:
102 ; CHECK-NEXT: [[DIV2:%.*]] = fdiv float [[B]], [[A]]
103 ; CHECK-NEXT: br label [[SELECT_END]]
105 ; CHECK-NEXT: [[SEL:%.*]] = phi float [ [[DIV1]], [[SELECT_TRUE_SINK]] ], [ [[DIV2]], [[SELECT_FALSE_SINK]] ]
106 ; CHECK-NEXT: ret float [[SEL]]
109 %div1 = fdiv float %a, %b
110 %div2 = fdiv float %b, %a
111 %cmp = fcmp ogt float %a, 5.0
112 %sel = select i1 %cmp, float %div1, float %div2
116 ; But if the select is marked unpredictable, then don't turn it into a branch.
118 define float @unpredictable_select(float %a, float %b) {
119 ; CHECK-LABEL: @unpredictable_select(
121 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A:%.*]], [[B:%.*]]
122 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[A]], 1.000000e+00
123 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[DIV]], float 2.000000e+00, !unpredictable !0
124 ; CHECK-NEXT: ret float [[SEL]]
127 %div = fdiv float %a, %b
128 %cmp = fcmp ogt float %a, 1.0
129 %sel = select i1 %cmp, float %div, float 2.0, !unpredictable !0
135 ; An 'fadd' is not too expensive, so it's ok to speculate.
137 define float @fadd_no_sink(float %a, float %b) {
138 ; CHECK-LABEL: @fadd_no_sink(
139 ; CHECK-NEXT: [[ADD:%.*]] = fadd float [[A:%.*]], [[B:%.*]]
140 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float 6.000000e+00, [[A]]
141 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[ADD]], float 7.000000e+00
142 ; CHECK-NEXT: ret float [[SEL]]
144 %add = fadd float %a, %b
145 %cmp = fcmp ogt float 6.0, %a
146 %sel = select i1 %cmp, float %add, float 7.0
150 ; Possible enhancement: sinkability is only calculated with the direct
151 ; operand of the select, so we don't try to sink this. The fdiv cost is not
152 ; taken into account.
154 define float @fdiv_no_sink(float %a, float %b) {
155 ; CHECK-LABEL: @fdiv_no_sink(
157 ; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A:%.*]], [[B:%.*]]
158 ; CHECK-NEXT: [[ADD:%.*]] = fadd float [[DIV]], [[B]]
159 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[A]], 1.000000e+00
160 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[ADD]], float 8.000000e+00
161 ; CHECK-NEXT: ret float [[SEL]]
164 %div = fdiv float %a, %b
165 %add = fadd float %div, %b
166 %cmp = fcmp ogt float %a, 1.0
167 %sel = select i1 %cmp, float %add, float 8.0
171 ; Do not transform the CFG if the select operands may have side effects.
173 declare i64* @bar(i32, i32, i32)
174 declare i64* @baz(i32, i32, i32)
176 define i64* @calls_no_sink(i32 %in) {
177 ; CHECK-LABEL: @calls_no_sink(
178 ; CHECK-NEXT: [[CALL1:%.*]] = call i64* @bar(i32 1, i32 2, i32 3)
179 ; CHECK-NEXT: [[CALL2:%.*]] = call i64* @baz(i32 1, i32 2, i32 3)
180 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32 [[IN:%.*]], 0
181 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TOBOOL]], i64* [[CALL1]], i64* [[CALL2]]
182 ; CHECK-NEXT: ret i64* [[SEL]]
184 %call1 = call i64* @bar(i32 1, i32 2, i32 3)
185 %call2 = call i64* @baz(i32 1, i32 2, i32 3)
186 %tobool = icmp ne i32 %in, 0
187 %sel = select i1 %tobool, i64* %call1, i64* %call2
191 define i32 @sdiv_no_sink(i32 %a, i32 %b) {
192 ; CHECK-LABEL: @sdiv_no_sink(
193 ; CHECK-NEXT: [[DIV1:%.*]] = sdiv i32 [[A:%.*]], [[B:%.*]]
194 ; CHECK-NEXT: [[DIV2:%.*]] = sdiv i32 [[B]], [[A]]
195 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A]], 5
196 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[DIV1]], i32 [[DIV2]]
197 ; CHECK-NEXT: ret i32 [[SEL]]
199 %div1 = sdiv i32 %a, %b
200 %div2 = sdiv i32 %b, %a
201 %cmp = icmp sgt i32 %a, 5
202 %sel = select i1 %cmp, i32 %div1, i32 %div2