Skip to content

Commit ff11d0e

Browse files
Add a pass to convert jump tables to switches
1 parent a6161a2 commit ff11d0e

File tree

13 files changed

+733
-0
lines changed

13 files changed

+733
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===- JumpTableToSwitch.h - ------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_TRANSFORMS_SCALAR_JUMP_TABLE_TO_SWITCH_H
10+
#define LLVM_TRANSFORMS_SCALAR_JUMP_TABLE_TO_SWITCH_H
11+
12+
#include "llvm/IR/PassManager.h"
13+
14+
namespace llvm {
15+
16+
class Function;
17+
18+
struct JumpTableToSwitchPass : PassInfoMixin<JumpTableToSwitchPass> {
19+
/// Run the pass over the function.
20+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
21+
};
22+
} // end namespace llvm
23+
24+
#endif // LLVM_TRANSFORMS_SCALAR_JUMP_TABLE_TO_SWITCH_H

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
#include "llvm/Transforms/Scalar/InferAddressSpaces.h"
199199
#include "llvm/Transforms/Scalar/InferAlignment.h"
200200
#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
201+
#include "llvm/Transforms/Scalar/JumpTableToSwitch.h"
201202
#include "llvm/Transforms/Scalar/JumpThreading.h"
202203
#include "llvm/Transforms/Scalar/LICM.h"
203204
#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
#include "llvm/Transforms/Scalar/IndVarSimplify.h"
9292
#include "llvm/Transforms/Scalar/InferAlignment.h"
9393
#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
94+
#include "llvm/Transforms/Scalar/JumpTableToSwitch.h"
9495
#include "llvm/Transforms/Scalar/JumpThreading.h"
9596
#include "llvm/Transforms/Scalar/LICM.h"
9697
#include "llvm/Transforms/Scalar/LoopDeletion.h"
@@ -237,6 +238,10 @@ static cl::opt<bool>
237238
EnableGVNSink("enable-gvn-sink",
238239
cl::desc("Enable the GVN sinking pass (default = off)"));
239240

241+
static cl::opt<bool>
242+
EnableJumpTableToSwitch("enable-jump-table-to-switch",
243+
cl::desc("Enable JumpTableToSwitch pass (default = off)"));
244+
240245
// This option is used in simplifying testing SampleFDO optimizations for
241246
// profile loading.
242247
static cl::opt<bool>
@@ -559,6 +564,10 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
559564
FPM.addPass(JumpThreadingPass());
560565
FPM.addPass(CorrelatedValuePropagationPass());
561566

567+
// Jump table to switch conversion.
568+
if (EnableJumpTableToSwitch)
569+
FPM.addPass(JumpTableToSwitchPass());
570+
562571
FPM.addPass(
563572
SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true)));
564573
FPM.addPass(InstCombinePass());

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ FUNCTION_PASS("interleaved-load-combine", InterleavedLoadCombinePass(TM))
348348
FUNCTION_PASS("invalidate<all>", InvalidateAllAnalysesPass())
349349
FUNCTION_PASS("irce", IRCEPass())
350350
FUNCTION_PASS("jump-threading", JumpThreadingPass())
351+
FUNCTION_PASS("jump-table-to-switch", JumpTableToSwitchPass());
351352
FUNCTION_PASS("kcfi", KCFIPass())
352353
FUNCTION_PASS("lcssa", LCSSAPass())
353354
FUNCTION_PASS("libcalls-shrinkwrap", LibCallsShrinkWrapPass())

llvm/lib/Transforms/Scalar/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ add_llvm_component_library(LLVMScalarOpts
2525
InferAlignment.cpp
2626
InstSimplifyPass.cpp
2727
JumpThreading.cpp
28+
JumpTableToSwitch.cpp
2829
LICM.cpp
2930
LoopAccessAnalysisPrinter.cpp
3031
LoopBoundSplit.cpp
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
//===- JumpTableToSwitch.cpp ----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Transforms/Scalar/JumpTableToSwitch.h"
10+
#include "llvm/ADT/SmallVector.h"
11+
#include "llvm/Analysis/ConstantFolding.h"
12+
#include "llvm/Analysis/DomTreeUpdater.h"
13+
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
14+
#include "llvm/Analysis/PostDominators.h"
15+
#include "llvm/IR/IRBuilder.h"
16+
#include "llvm/Support/CommandLine.h"
17+
#include "llvm/Support/Debug.h"
18+
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
19+
20+
using namespace llvm;
21+
22+
// TODO: Consider adding a cost model for profitability analysis of this
23+
// transformation. Currently we replace a jump table with a switch if all the
24+
// functions in the jump table are smaller than the provided threshold.
25+
static cl::opt<unsigned>
26+
JumpTableSizeThreshold("jump-table-to-switch-size-threshold", cl::Hidden,
27+
cl::desc("Only split jump tables with size less or "
28+
"equal than JumpTableSizeThreshold."),
29+
cl::init(10));
30+
31+
static cl::opt<unsigned> FunctionSizeThreshold(
32+
"jump-table-to-switch-function-size-threshold", cl::Hidden,
33+
cl::desc("Only split jump tables containing functions whose sizes are less "
34+
"or equal than this threshold."),
35+
cl::init(50));
36+
37+
#define DEBUG_TYPE "jump-table-to-switch"
38+
39+
namespace {
40+
struct JumpTableTy {
41+
Value *Index;
42+
SmallVector<Function *, 10> Funcs;
43+
};
44+
} // anonymous namespace
45+
46+
static std::optional<JumpTableTy> parseJumpTable(GetElementPtrInst *GEP,
47+
PointerType *PtrTy) {
48+
Constant *Ptr = dyn_cast<Constant>(GEP->getPointerOperand());
49+
if (!Ptr)
50+
return std::nullopt;
51+
52+
GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr);
53+
if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
54+
return std::nullopt;
55+
56+
Function &F = *GEP->getParent()->getParent();
57+
const DataLayout &DL = F.getParent()->getDataLayout();
58+
const unsigned BitWidth =
59+
DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
60+
MapVector<Value *, APInt> VariableOffsets;
61+
APInt ConstantOffset(BitWidth, 0);
62+
if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
63+
return std::nullopt;
64+
if (VariableOffsets.size() != 1)
65+
return std::nullopt;
66+
// TODO: consider supporting more general patterns
67+
if (!ConstantOffset.isZero())
68+
return std::nullopt;
69+
APInt StrideBytes = VariableOffsets.front().second;
70+
const uint64_t JumpTableSizeBytes = DL.getTypeAllocSize(GV->getValueType());
71+
if (JumpTableSizeBytes % StrideBytes.getZExtValue() != 0)
72+
return std::nullopt;
73+
const uint64_t N = JumpTableSizeBytes / StrideBytes.getZExtValue();
74+
if (N > JumpTableSizeThreshold)
75+
return std::nullopt;
76+
77+
JumpTableTy JumpTable;
78+
JumpTable.Index = VariableOffsets.front().first;
79+
JumpTable.Funcs.reserve(N);
80+
for (uint64_t Index = 0; Index < N; ++Index) {
81+
// ConstantOffset is zero.
82+
APInt Offset = Index * StrideBytes;
83+
Constant *C = ConstantFoldLoadFromConst(
84+
cast<Constant>(GV->getInitializer()), PtrTy, Offset, DL);
85+
auto *Func = dyn_cast_or_null<Function>(C);
86+
if (!Func || Func->isDeclaration() ||
87+
Func->getInstructionCount() > FunctionSizeThreshold)
88+
return std::nullopt;
89+
JumpTable.Funcs.push_back(Func);
90+
}
91+
return JumpTable;
92+
}
93+
94+
static BasicBlock *expandToSwitch(CallBase *CB, const JumpTableTy &JT,
95+
DomTreeUpdater &DTU,
96+
OptimizationRemarkEmitter &ORE) {
97+
const bool IsVoid = CB->getType() == Type::getVoidTy(CB->getContext());
98+
99+
SmallVector<DominatorTree::UpdateType, 8> DTUpdates;
100+
BasicBlock *BB = CB->getParent();
101+
BasicBlock *Tail = SplitBlock(BB, CB, &DTU, nullptr, nullptr,
102+
BB->getName() + Twine(".tail"));
103+
DTUpdates.push_back({DominatorTree::Delete, BB, Tail});
104+
BB->getTerminator()->eraseFromParent();
105+
106+
Function &F = *BB->getParent();
107+
BasicBlock *BBUnreachable = BasicBlock::Create(
108+
F.getContext(), "default.switch.case.unreachable", &F, Tail);
109+
IRBuilder<> BuilderUnreachable(BBUnreachable);
110+
BuilderUnreachable.CreateUnreachable();
111+
112+
IRBuilder<> Builder(BB);
113+
SwitchInst *Switch = Builder.CreateSwitch(JT.Index, BBUnreachable);
114+
DTUpdates.push_back({DominatorTree::Insert, BB, BBUnreachable});
115+
116+
IRBuilder<> BuilderTail(CB);
117+
PHINode *PHI =
118+
IsVoid ? nullptr : BuilderTail.CreatePHI(CB->getType(), JT.Funcs.size());
119+
120+
for (auto [Index, Func] : llvm::enumerate(JT.Funcs)) {
121+
BasicBlock *B = BasicBlock::Create(Func->getContext(),
122+
"call." + Twine(Index), &F, Tail);
123+
DTUpdates.push_back({DominatorTree::Insert, BB, B});
124+
DTUpdates.push_back({DominatorTree::Insert, B, Tail});
125+
126+
CallBase *Call = cast<CallBase>(CB->clone());
127+
Call->setCalledFunction(Func);
128+
Call->insertInto(B, B->end());
129+
Switch->addCase(
130+
cast<ConstantInt>(ConstantInt::get(JT.Index->getType(), Index)), B);
131+
BranchInst::Create(Tail, B);
132+
if (PHI)
133+
PHI->addIncoming(Call, B);
134+
}
135+
DTU.applyUpdates(DTUpdates);
136+
ORE.emit([&]() {
137+
return OptimizationRemark(DEBUG_TYPE, "ReplacedJumpTableWithSwitch", CB)
138+
<< "expanded indirect call into switch";
139+
});
140+
if (PHI)
141+
CB->replaceAllUsesWith(PHI);
142+
CB->eraseFromParent();
143+
return Tail;
144+
}
145+
146+
PreservedAnalyses JumpTableToSwitchPass::run(Function &F,
147+
FunctionAnalysisManager &AM) {
148+
OptimizationRemarkEmitter &ORE =
149+
AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
150+
DominatorTree *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
151+
PostDominatorTree *PDT = AM.getCachedResult<PostDominatorTreeAnalysis>(F);
152+
DomTreeUpdater DTU(DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy);
153+
bool Changed = false;
154+
for (BasicBlock &BB : make_early_inc_range(F)) {
155+
BasicBlock *CurrentBB = &BB;
156+
while (CurrentBB) {
157+
BasicBlock *SplittedOutTail = nullptr;
158+
for (Instruction &I : make_early_inc_range(*CurrentBB)) {
159+
auto *Call = dyn_cast<CallInst>(&I);
160+
if (!Call || Call->getCalledFunction() || Call->isMustTailCall())
161+
continue;
162+
auto *L = dyn_cast<LoadInst>(Call->getCalledOperand());
163+
// Skip atomic or volatile loads.
164+
if (!L || !L->isSimple())
165+
continue;
166+
auto *GEP = dyn_cast<GetElementPtrInst>(L->getPointerOperand());
167+
if (!GEP)
168+
continue;
169+
auto *PtrTy = dyn_cast<PointerType>(L->getType());
170+
assert(PtrTy && "call operand must be a pointer");
171+
std::optional<JumpTableTy> JumpTable = parseJumpTable(GEP, PtrTy);
172+
if (!JumpTable)
173+
continue;
174+
SplittedOutTail = expandToSwitch(Call, *JumpTable, DTU, ORE);
175+
Changed = true;
176+
break;
177+
}
178+
CurrentBB = SplittedOutTail ? SplittedOutTail : nullptr;
179+
}
180+
}
181+
182+
if (!Changed)
183+
return PreservedAnalyses::all();
184+
185+
PreservedAnalyses PA;
186+
if (DT)
187+
PA.preserve<DominatorTreeAnalysis>();
188+
if (PDT)
189+
PA.preserve<PostDominatorTreeAnalysis>();
190+
return PA;
191+
}

llvm/test/Other/new-pm-defaults.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
; RUN: -passes='default<O3>' -S %s 2>&1 \
7272
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-DEFAULT,CHECK-O3,%llvmcheckext,CHECK-EP-OPTIMIZER-LAST,CHECK-O23SZ
7373

74+
; RUN: opt -disable-verify -verify-analysis-invalidation=0 -eagerly-invalidate-analyses=0 -debug-pass-manager \
75+
; RUN: -passes='default<O3>' -enable-jump-table-to-switch -S %s 2>&1 \
76+
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-DEFAULT,CHECK-O3,CHECK-JUMP-TABLE-TO-SWITCH,CHECK-O23SZ,%llvmcheckext
77+
7478
; RUN: opt -disable-verify -verify-analysis-invalidation=0 -eagerly-invalidate-analyses=0 -debug-pass-manager \
7579
; RUN: -passes='default<O3>' -enable-matrix -S %s 2>&1 \
7680
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-DEFAULT,CHECK-O3,CHECK-O23SZ,%llvmcheckext,CHECK-MATRIX
@@ -151,6 +155,7 @@
151155
; CHECK-O23SZ-NEXT: Running analysis: LazyValueAnalysis
152156
; CHECK-O23SZ-NEXT: Running pass: CorrelatedValuePropagationPass
153157
; CHECK-O23SZ-NEXT: Invalidating analysis: LazyValueAnalysis
158+
; CHECK-JUMP-TABLE-TO-SWITCH-NEXT: Running pass: JumpTableToSwitchPass
154159
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
155160
; CHECK-O-NEXT: Running pass: InstCombinePass
156161
; CHECK-O23SZ-NEXT: Running pass: AggressiveInstCombinePass

0 commit comments

Comments
 (0)