Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,32 @@ class TargetTransformInfoImplBase {
virtual InstructionCost getCFInstrCost(unsigned Opcode,
TTI::TargetCostKind CostKind,
const Instruction *I = nullptr) const {
if (Opcode == Instruction::Switch && CostKind == TTI::TCK_CodeSize && I) {
const SwitchInst *SI = cast<SwitchInst>(I);
unsigned JumpTableSize, NumSuccs = I->getNumSuccessors();
if (SI->defaultDestUnreachable())
NumSuccs--;

// An unreachable switch
if (NumSuccs == 0)
return TTI::TCC_Free;

// A trivial unconditional branch.
if (NumSuccs == 1)
return TTI::TCC_Basic;

getEstimatedNumberOfCaseClusters(*SI, JumpTableSize, nullptr, nullptr);

// Assume that lowering the switch block is implemented by binary search
// if no jump table is generated.
if (JumpTableSize == 0)
return llvm::Log2_32_Ceil(NumSuccs) * 2 * TTI::TCC_Basic;

// Cost for jump table: load + jump + default compare + default jump
return 2 * TTI::TCC_Basic +
(SI->defaultDestUnreachable() ? 0 : 2 * TTI::TCC_Basic);
}

// A phi would be free, unless we're costing the throughput because it
// will require a register.
if (Opcode == Instruction::PHI && CostKind != TTI::TCK_RecipThroughput)
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/X86/X86TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6155,6 +6155,9 @@ X86TTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
InstructionCost X86TTIImpl::getCFInstrCost(unsigned Opcode,
TTI::TargetCostKind CostKind,
const Instruction *I) const {
if (Opcode == Instruction::Switch)
return BaseT::getCFInstrCost(Opcode, CostKind, I);

if (CostKind != TTI::TCK_RecipThroughput)
return Opcode == Instruction::PHI ? TTI::TCC_Free : TTI::TCC_Basic;
// Branches are assumed to be predicted.
Expand Down
Loading