Skip to content

Commit ae5fd1b

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

18 files changed

+699
-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: 2 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"
@@ -558,6 +559,7 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
558559
// Optimize based on known information about branches, and cleanup afterward.
559560
FPM.addPass(JumpThreadingPass());
560561
FPM.addPass(CorrelatedValuePropagationPass());
562+
FPM.addPass(JumpTableToSwitchPass());
561563

562564
FPM.addPass(
563565
SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true)));

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
; CHECK-O23SZ-NEXT: Running analysis: LazyValueAnalysis
152152
; CHECK-O23SZ-NEXT: Running pass: CorrelatedValuePropagationPass
153153
; CHECK-O23SZ-NEXT: Invalidating analysis: LazyValueAnalysis
154+
; CHECK-O23SZ-NEXT: Running pass: JumpTableToSwitchPass
154155
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
155156
; CHECK-O-NEXT: Running pass: InstCombinePass
156157
; CHECK-O23SZ-NEXT: Running pass: AggressiveInstCombinePass

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
; CHECK-O23SZ-NEXT: Running analysis: LazyValueAnalysis
9191
; CHECK-O23SZ-NEXT: Running pass: CorrelatedValuePropagationPass
9292
; CHECK-O23SZ-NEXT: Invalidating analysis: LazyValueAnalysis
93+
; CHECK-O23SZ-NEXT: Running pass: JumpTableToSwitchPass
9394
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
9495
; CHECK-O-NEXT: Running pass: InstCombinePass
9596
; CHECK-O23SZ-NEXT: Running pass: AggressiveInstCombinePass

llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
; CHECK-O23SZ-NEXT: Running analysis: LazyValueAnalysis
7979
; CHECK-O23SZ-NEXT: Running pass: CorrelatedValuePropagationPass
8080
; CHECK-O23SZ-NEXT: Invalidating analysis: LazyValueAnalysis
81+
; CHECK-O23SZ-NEXT: Running pass: JumpTableToSwitchPass
8182
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
8283
; CHECK-O-NEXT: Running pass: InstCombinePass
8384
; CHECK-O23SZ-NEXT: Running pass: AggressiveInstCombinePass

llvm/test/Other/new-pm-thinlto-postlink-samplepgo-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
; CHECK-O23SZ-NEXT: Running analysis: LazyValueAnalysis
8787
; CHECK-O23SZ-NEXT: Running pass: CorrelatedValuePropagationPass
8888
; CHECK-O23SZ-NEXT: Invalidating analysis: LazyValueAnalysis
89+
; CHECK-O23SZ-NEXT: Running pass: JumpTableToSwitchPass
8990
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
9091
; CHECK-O-NEXT: Running pass: InstCombinePass
9192
; CHECK-O23SZ-NEXT: Running pass: AggressiveInstCombinePass

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
; CHECK-O23SZ-NEXT: Running analysis: LazyValueAnalysis
122122
; CHECK-O23SZ-NEXT: Running pass: CorrelatedValuePropagationPass
123123
; CHECK-O23SZ-NEXT: Invalidating analysis: LazyValueAnalysis
124+
; CHECK-O23SZ-NEXT: Running pass: JumpTableToSwitchPass
124125
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
125126
; CHECK-O-NEXT: Running pass: InstCombinePass
126127
; CHECK-O23SZ-NEXT: Running pass: AggressiveInstCombinePass

llvm/test/Other/new-pm-thinlto-prelink-pgo-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
; CHECK-O23SZ-NEXT: Running analysis: LazyValueAnalysis
119119
; CHECK-O23SZ-NEXT: Running pass: CorrelatedValuePropagationPass
120120
; CHECK-O23SZ-NEXT: Invalidating analysis: LazyValueAnalysis
121+
; CHECK-O23SZ-NEXT: Running pass: JumpTableToSwitchPass
121122
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
122123
; CHECK-O-NEXT: Running pass: InstCombinePass
123124
; CHECK-O-NEXT: Running analysis: BlockFrequencyAnalysis on foo

llvm/test/Other/new-pm-thinlto-prelink-samplepgo-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
; CHECK-O23SZ-NEXT: Running analysis: LazyValueAnalysis
9191
; CHECK-O23SZ-NEXT: Running pass: CorrelatedValuePropagationPass
9292
; CHECK-O23SZ-NEXT: Invalidating analysis: LazyValueAnalysis
93+
; CHECK-O23SZ-NEXT: Running pass: JumpTableToSwitchPass
9394
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
9495
; CHECK-O-NEXT: Running pass: InstCombinePass
9596
; CHECK-O23SZ-NEXT: Running pass: AggressiveInstCombinePass

0 commit comments

Comments
 (0)