Skip to content

Commit 4b25258

Browse files
committed
move to TTIImpl
1 parent f2640e0 commit 4b25258

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,32 @@ class TargetTransformInfoImplBase {
770770
virtual InstructionCost getCFInstrCost(unsigned Opcode,
771771
TTI::TargetCostKind CostKind,
772772
const Instruction *I = nullptr) const {
773+
if (Opcode == Instruction::Switch && CostKind == TTI::TCK_CodeSize && I) {
774+
const SwitchInst *SI = cast<SwitchInst>(I);
775+
unsigned JumpTableSize, NumSuccs = I->getNumSuccessors();
776+
if (SI->defaultDestUnreachable())
777+
NumSuccs--;
778+
779+
// An unreachable switch
780+
if (NumSuccs == 0)
781+
return TTI::TCC_Free;
782+
783+
// A trivial unconditional branch.
784+
if (NumSuccs == 1)
785+
return TTI::TCC_Basic;
786+
787+
getEstimatedNumberOfCaseClusters(*SI, JumpTableSize, nullptr, nullptr);
788+
789+
// Assume that lowering the switch block is implemented by binary search
790+
// if no jump table is generated.
791+
if (JumpTableSize == 0)
792+
return llvm::Log2_32_Ceil(NumSuccs) * 2 * TTI::TCC_Basic;
793+
794+
// Cost for jump table: load + jump + default compare + default jump
795+
return 2 * TTI::TCC_Basic +
796+
(SI->defaultDestUnreachable() ? 0 : 2 * TTI::TCC_Basic);
797+
}
798+
773799
// A phi would be free, unless we're costing the throughput because it
774800
// will require a register.
775801
if (Opcode == Instruction::PHI && CostKind != TTI::TCK_RecipThroughput)

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,43 +1365,6 @@ 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-
14051368
return BaseT::getCFInstrCost(Opcode, CostKind, I);
14061369
}
14071370

0 commit comments

Comments
 (0)