Skip to content

Commit b44548c

Browse files
committed
resolve comments
1 parent 4ab77c4 commit b44548c

File tree

3 files changed

+182
-23
lines changed

3 files changed

+182
-23
lines changed

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,43 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
13651365
InstructionCost
13661366
getCFInstrCost(unsigned Opcode, TTI::TargetCostKind CostKind,
13671367
const Instruction *I = nullptr) const override {
1368+
if (Opcode == Instruction::Switch && CostKind == TTI::TCK_CodeSize && I) {
1369+
const SwitchInst *SI = cast<SwitchInst>(I);
1370+
unsigned JumpTableSize, NumSuccs = I->getNumSuccessors();
1371+
auto BrCost = thisT()->getCFInstrCost(Instruction::Br, CostKind);
1372+
if (SI->defaultDestUnreachable())
1373+
NumSuccs--;
1374+
1375+
// An unreachable switch
1376+
if (NumSuccs == 0)
1377+
return TTI::TCC_Free;
1378+
1379+
// A trivial unconditional branch.
1380+
if (NumSuccs == 1)
1381+
return BrCost;
1382+
1383+
thisT()->getEstimatedNumberOfCaseClusters(*SI, JumpTableSize, nullptr,
1384+
nullptr);
1385+
Type *BoolTy = IntegerType::get(SI->getContext(), 1);
1386+
Type *CondTy = SI->getCondition()->getType();
1387+
auto CmpCost = thisT()->getCmpSelInstrCost(
1388+
BinaryOperator::ICmp, BoolTy, CondTy, CmpInst::ICMP_UGT, CostKind);
1389+
1390+
// Assume that lowering the switch block is implemented by binary search
1391+
// if no jump table is generated.
1392+
if (JumpTableSize == 0)
1393+
return llvm::Log2_32_Ceil(NumSuccs) * (CmpCost + BrCost);
1394+
1395+
// Cost for jump table: load + jump + default compare + default jump
1396+
Type *EntryTy = PointerType::get(SI->getContext(), 0);
1397+
Align Alignment = thisT()->DL.getABITypeAlign(EntryTy);
1398+
auto LoadCost = thisT()->getMemoryOpCost(Instruction::Load, EntryTy,
1399+
Alignment, 0, CostKind);
1400+
1401+
return LoadCost + BrCost +
1402+
(SI->defaultDestUnreachable() ? 0 : (CmpCost + BrCost));
1403+
}
1404+
13681405
return BaseT::getCFInstrCost(Opcode, CostKind, I);
13691406
}
13701407

llvm/lib/Target/X86/X86TargetTransformInfo.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6155,22 +6155,8 @@ X86TTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
61556155
InstructionCost X86TTIImpl::getCFInstrCost(unsigned Opcode,
61566156
TTI::TargetCostKind CostKind,
61576157
const Instruction *I) const {
6158-
if (Opcode == Instruction::Switch && CostKind == TTI::TCK_CodeSize) {
6159-
unsigned JumpTableSize, NumSuccs = I->getNumSuccessors();
6160-
getEstimatedNumberOfCaseClusters(*cast<SwitchInst>(I), JumpTableSize,
6161-
nullptr, nullptr);
6162-
// A trivial unconditional branch.
6163-
if (NumSuccs == 1)
6164-
return TTI::TCC_Basic;
6165-
6166-
// Assume that lowering the switch block is implemented by binary search if
6167-
// no jump table is generated.
6168-
if (JumpTableSize == 0)
6169-
return llvm::Log2_32_Ceil(NumSuccs) * 2 * TTI::TCC_Basic;
6170-
6171-
// Indirect branch + default compare + default jump
6172-
return 3 * TTI::TCC_Basic;
6173-
}
6158+
if (Opcode == Instruction::Switch)
6159+
return BaseT::getCFInstrCost(Opcode, CostKind, I);
61746160

61756161
if (CostKind != TTI::TCK_RecipThroughput)
61766162
return Opcode == Instruction::PHI ? TTI::TCC_Free : TTI::TCC_Basic;

llvm/test/Analysis/CostModel/X86/switch.ll

Lines changed: 143 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3
77

88
define i32 @single_succ_switch(i32 %x) {
99
; THROUGHPUT-LABEL: 'single_succ_switch'
10-
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: switch i32 %x, label %default [
10+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: switch i32 %x, label %default [
1111
; THROUGHPUT-NEXT: ]
1212
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 1
1313
;
@@ -30,7 +30,7 @@ default:
3030

3131
define i32 @dense_switch(i32 %x) {
3232
; THROUGHPUT-LABEL: 'dense_switch'
33-
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: switch i32 %x, label %default [
33+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: switch i32 %x, label %default [
3434
; THROUGHPUT-NEXT: i32 0, label %bb0
3535
; THROUGHPUT-NEXT: i32 1, label %bb1
3636
; THROUGHPUT-NEXT: i32 2, label %bb2
@@ -60,7 +60,7 @@ define i32 @dense_switch(i32 %x) {
6060
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: unreachable
6161
;
6262
; CODESIZE-LABEL: 'dense_switch'
63-
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: switch i32 %x, label %default [
63+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 2 for instruction: switch i32 %x, label %default [
6464
; CODESIZE-NEXT: i32 0, label %bb0
6565
; CODESIZE-NEXT: i32 1, label %bb1
6666
; CODESIZE-NEXT: i32 2, label %bb2
@@ -96,9 +96,77 @@ default:
9696
unreachable
9797
}
9898

99+
define i32 @dense_switch_reachable_default(i32 %x) {
100+
; THROUGHPUT-LABEL: 'dense_switch_reachable_default'
101+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: switch i32 %x, label %default [
102+
; THROUGHPUT-NEXT: i32 0, label %bb0
103+
; THROUGHPUT-NEXT: i32 1, label %bb1
104+
; THROUGHPUT-NEXT: i32 2, label %bb2
105+
; THROUGHPUT-NEXT: i32 3, label %bb3
106+
; THROUGHPUT-NEXT: i32 4, label %bb4
107+
; THROUGHPUT-NEXT: ]
108+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 0
109+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 1
110+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 2
111+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 3
112+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 4
113+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 5
114+
;
115+
; LATENCY-LABEL: 'dense_switch_reachable_default'
116+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: switch i32 %x, label %default [
117+
; LATENCY-NEXT: i32 0, label %bb0
118+
; LATENCY-NEXT: i32 1, label %bb1
119+
; LATENCY-NEXT: i32 2, label %bb2
120+
; LATENCY-NEXT: i32 3, label %bb3
121+
; LATENCY-NEXT: i32 4, label %bb4
122+
; LATENCY-NEXT: ]
123+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 0
124+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 1
125+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 2
126+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 3
127+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 4
128+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 5
129+
;
130+
; CODESIZE-LABEL: 'dense_switch_reachable_default'
131+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 4 for instruction: switch i32 %x, label %default [
132+
; CODESIZE-NEXT: i32 0, label %bb0
133+
; CODESIZE-NEXT: i32 1, label %bb1
134+
; CODESIZE-NEXT: i32 2, label %bb2
135+
; CODESIZE-NEXT: i32 3, label %bb3
136+
; CODESIZE-NEXT: i32 4, label %bb4
137+
; CODESIZE-NEXT: ]
138+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 0
139+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 1
140+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 2
141+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 3
142+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 4
143+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 5
144+
;
145+
entry:
146+
switch i32 %x, label %default [
147+
i32 0, label %bb0
148+
i32 1, label %bb1
149+
i32 2, label %bb2
150+
i32 3, label %bb3
151+
i32 4, label %bb4
152+
]
153+
bb0:
154+
ret i32 0
155+
bb1:
156+
ret i32 1
157+
bb2:
158+
ret i32 2
159+
bb3:
160+
ret i32 3
161+
bb4:
162+
ret i32 4
163+
default:
164+
ret i32 5
165+
}
166+
99167
define i32 @sparse_switch(i32 %x) {
100168
; THROUGHPUT-LABEL: 'sparse_switch'
101-
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: switch i32 %x, label %default [
169+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: switch i32 %x, label %default [
102170
; THROUGHPUT-NEXT: i32 0, label %bb0
103171
; THROUGHPUT-NEXT: i32 100, label %bb1
104172
; THROUGHPUT-NEXT: i32 200, label %bb2
@@ -164,9 +232,77 @@ default:
164232
unreachable
165233
}
166234

235+
define i32 @sparse_switch_reachable_default(i32 %x) {
236+
; THROUGHPUT-LABEL: 'sparse_switch_reachable_default'
237+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: switch i32 %x, label %default [
238+
; THROUGHPUT-NEXT: i32 0, label %bb0
239+
; THROUGHPUT-NEXT: i32 100, label %bb1
240+
; THROUGHPUT-NEXT: i32 200, label %bb2
241+
; THROUGHPUT-NEXT: i32 300, label %bb3
242+
; THROUGHPUT-NEXT: i32 400, label %bb4
243+
; THROUGHPUT-NEXT: ]
244+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 0
245+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 1
246+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 2
247+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 3
248+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 4
249+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret i32 5
250+
;
251+
; LATENCY-LABEL: 'sparse_switch_reachable_default'
252+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: switch i32 %x, label %default [
253+
; LATENCY-NEXT: i32 0, label %bb0
254+
; LATENCY-NEXT: i32 100, label %bb1
255+
; LATENCY-NEXT: i32 200, label %bb2
256+
; LATENCY-NEXT: i32 300, label %bb3
257+
; LATENCY-NEXT: i32 400, label %bb4
258+
; LATENCY-NEXT: ]
259+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 0
260+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 1
261+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 2
262+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 3
263+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 4
264+
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 5
265+
;
266+
; CODESIZE-LABEL: 'sparse_switch_reachable_default'
267+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 6 for instruction: switch i32 %x, label %default [
268+
; CODESIZE-NEXT: i32 0, label %bb0
269+
; CODESIZE-NEXT: i32 100, label %bb1
270+
; CODESIZE-NEXT: i32 200, label %bb2
271+
; CODESIZE-NEXT: i32 300, label %bb3
272+
; CODESIZE-NEXT: i32 400, label %bb4
273+
; CODESIZE-NEXT: ]
274+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 0
275+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 1
276+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 2
277+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 3
278+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 4
279+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret i32 5
280+
;
281+
entry:
282+
switch i32 %x, label %default [
283+
i32 0, label %bb0
284+
i32 100, label %bb1
285+
i32 200, label %bb2
286+
i32 300, label %bb3
287+
i32 400, label %bb4
288+
]
289+
bb0:
290+
ret i32 0
291+
bb1:
292+
ret i32 1
293+
bb2:
294+
ret i32 2
295+
bb3:
296+
ret i32 3
297+
bb4:
298+
ret i32 4
299+
default:
300+
ret i32 5
301+
}
302+
167303
define i32 @dense_big_switch(i32 %x) {
168304
; THROUGHPUT-LABEL: 'dense_big_switch'
169-
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: switch i32 %x, label %default [
305+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: switch i32 %x, label %default [
170306
; THROUGHPUT-NEXT: i32 0, label %bb0
171307
; THROUGHPUT-NEXT: i32 1, label %bb1
172308
; THROUGHPUT-NEXT: i32 2, label %bb2
@@ -220,7 +356,7 @@ define i32 @dense_big_switch(i32 %x) {
220356
; LATENCY-NEXT: Cost Model: Found an estimated cost of 1 for instruction: unreachable
221357
;
222358
; CODESIZE-LABEL: 'dense_big_switch'
223-
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: switch i32 %x, label %default [
359+
; CODESIZE-NEXT: Cost Model: Found an estimated cost of 2 for instruction: switch i32 %x, label %default [
224360
; CODESIZE-NEXT: i32 0, label %bb0
225361
; CODESIZE-NEXT: i32 1, label %bb1
226362
; CODESIZE-NEXT: i32 2, label %bb2
@@ -288,7 +424,7 @@ default:
288424

289425
define i32 @sparse_big_switch(i32 %x) {
290426
; THROUGHPUT-LABEL: 'sparse_big_switch'
291-
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: switch i32 %x, label %default [
427+
; THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: switch i32 %x, label %default [
292428
; THROUGHPUT-NEXT: i32 0, label %bb0
293429
; THROUGHPUT-NEXT: i32 100, label %bb1
294430
; THROUGHPUT-NEXT: i32 200, label %bb2

0 commit comments

Comments
 (0)