Skip to content

Commit e031383

Browse files
Add a pass to convert jump tables to switches
1 parent 2a869ce commit e031383

16 files changed

+571
-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
@@ -342,6 +342,7 @@ FUNCTION_PASS("interleaved-load-combine", InterleavedLoadCombinePass(TM))
342342
FUNCTION_PASS("invalidate<all>", InvalidateAllAnalysesPass())
343343
FUNCTION_PASS("irce", IRCEPass())
344344
FUNCTION_PASS("jump-threading", JumpThreadingPass())
345+
FUNCTION_PASS("jump-table-to-switch", JumpTableToSwitchPass());
345346
FUNCTION_PASS("kcfi", KCFIPass())
346347
FUNCTION_PASS("lcssa", LCSSAPass())
347348
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: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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/PostDominators.h"
15+
#include "llvm/Analysis/TargetLibraryInfo.h"
16+
#include "llvm/Analysis/TargetTransformInfo.h"
17+
#include "llvm/Analysis/ValueTracking.h"
18+
#include "llvm/IR/IRBuilder.h"
19+
#include "llvm/IR/IntrinsicInst.h"
20+
#include "llvm/IR/PatternMatch.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+
#define DEBUG_TYPE "jump-table-to-switch"
37+
38+
namespace {
39+
struct JumpTableTy {
40+
Value *Index;
41+
SmallVector<Function *, 5> Funcs;
42+
};
43+
} // anonymous namespace
44+
45+
static std::optional<JumpTableTy> parseJumpTable(GetElementPtrInst *GEP,
46+
PointerType *PtrTy) {
47+
if (!GEP->isInBounds())
48+
return std::nullopt;
49+
50+
Constant *Ptr = dyn_cast<Constant>(GEP->getPointerOperand());
51+
if (!Ptr || !Ptr->getNumOperands())
52+
return std::nullopt;
53+
54+
GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr);
55+
if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
56+
return std::nullopt;
57+
58+
Function &F = *GEP->getParent()->getParent();
59+
const DataLayout &DL = F.getParent()->getDataLayout();
60+
const unsigned PtrSizeBytes = DL.getPointerTypeSize(PtrTy);
61+
const uint64_t JumpTableSizeBytes = DL.getTypeAllocSize(GV->getValueType());
62+
if (JumpTableSizeBytes % PtrSizeBytes != 0)
63+
return std::nullopt;
64+
const uint64_t N = JumpTableSizeBytes / PtrSizeBytes;
65+
if (N > JumpTableSizeThreshold)
66+
return std::nullopt;
67+
68+
const unsigned BitWidth =
69+
DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
70+
MapVector<Value *, APInt> VariableOffsets;
71+
APInt ConstantOffset(BitWidth, 0);
72+
if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
73+
return std::nullopt;
74+
if (VariableOffsets.empty() || VariableOffsets.size() > 1)
75+
return std::nullopt;
76+
// TODO: consider supporting more general patterns
77+
if (!ConstantOffset.isZero())
78+
return std::nullopt;
79+
80+
JumpTableTy JumpTable;
81+
JumpTable.Index = VariableOffsets.front().first;
82+
JumpTable.Funcs.assign(N, nullptr);
83+
const unsigned PtrSizeBits = DL.getPointerTypeSizeInBits(PtrTy);
84+
for (uint64_t Index = 0; Index < N; ++Index) {
85+
APInt Offset(PtrSizeBits, Index * PtrSizeBytes);
86+
Offset += ConstantOffset;
87+
Constant *C = ConstantFoldLoadFromConst(cast<Constant>(GV->getOperand(0)),
88+
PtrTy, Offset, DL);
89+
auto *Func = dyn_cast_or_null<Function>(C);
90+
if (!Func || Func->isDeclaration())
91+
return std::nullopt;
92+
JumpTable.Funcs[Index] = Func;
93+
}
94+
return JumpTable;
95+
}
96+
97+
static BasicBlock *split(CallBase *CB, const JumpTableTy &JT,
98+
DomTreeUpdater &DTU) {
99+
const bool IsVoid = CB->getType() == Type::getVoidTy(CB->getContext());
100+
101+
SmallVector<DominatorTree::UpdateType, 8> DTUpdates;
102+
BasicBlock *BB = CB->getParent();
103+
BasicBlock *Tail = SplitBlock(BB, CB, &DTU, nullptr, nullptr,
104+
BB->getName() + Twine(".tail"));
105+
DTUpdates.push_back({DominatorTree::Delete, BB, Tail});
106+
BB->getTerminator()->eraseFromParent();
107+
108+
Function &F = *BB->getParent();
109+
BasicBlock *BBUnreachable = BasicBlock::Create(
110+
F.getContext(), "default.switch.case.unreachable", &F, Tail);
111+
IRBuilder<> BuilderUnreachable(BBUnreachable);
112+
BuilderUnreachable.CreateUnreachable();
113+
114+
IRBuilder<> Builder(BB);
115+
SwitchInst *Switch = Builder.CreateSwitch(JT.Index, BBUnreachable);
116+
DTUpdates.push_back({DominatorTree::Insert, BB, BBUnreachable});
117+
118+
IRBuilder<> BuilderTail(CB);
119+
PHINode *PHI =
120+
IsVoid ? nullptr : BuilderTail.CreatePHI(CB->getType(), JT.Funcs.size());
121+
122+
for (auto [Index, Func] : llvm::enumerate(JT.Funcs)) {
123+
BasicBlock *B = BasicBlock::Create(Func->getContext(),
124+
"call." + Twine(Index), &F, Tail);
125+
DTUpdates.push_back({DominatorTree::Insert, BB, B});
126+
DTUpdates.push_back({DominatorTree::Insert, B, Tail});
127+
128+
CallBase *Call = cast<CallBase>(CB->clone());
129+
Call->setCalledFunction(Func);
130+
Call->insertInto(B, B->end());
131+
Switch->addCase(
132+
cast<ConstantInt>(ConstantInt::get(JT.Index->getType(), Index)), B);
133+
BranchInst::Create(Tail, B);
134+
if (PHI)
135+
PHI->addIncoming(Call, B);
136+
}
137+
DTU.applyUpdates(DTUpdates);
138+
if (PHI)
139+
CB->replaceAllUsesWith(PHI);
140+
CB->eraseFromParent();
141+
return Tail;
142+
}
143+
144+
PreservedAnalyses JumpTableToSwitchPass::run(Function &F,
145+
FunctionAnalysisManager &AM) {
146+
DominatorTree *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
147+
PostDominatorTree *PDT = AM.getCachedResult<PostDominatorTreeAnalysis>(F);
148+
DomTreeUpdater DTU(DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy);
149+
bool Changed = false;
150+
for (BasicBlock &BB : make_early_inc_range(F)) {
151+
BasicBlock *CurrentBB = &BB;
152+
while (CurrentBB) {
153+
BasicBlock *SplittedOutTail = nullptr;
154+
for (Instruction &I : make_early_inc_range(*CurrentBB)) {
155+
auto *Call = dyn_cast<CallInst>(&I);
156+
if (!Call || isa<IntrinsicInst>(Call) || Call->getCalledFunction() ||
157+
Call->isMustTailCall())
158+
continue;
159+
160+
Value *V;
161+
if (!match(Call->getCalledOperand(), m_Load(m_Value(V))))
162+
continue;
163+
auto *L = cast<LoadInst>(Call->getCalledOperand());
164+
// Skip atomic or volatile loads.
165+
if (!L->isSimple())
166+
continue;
167+
auto *GEP = dyn_cast<GetElementPtrInst>(V);
168+
if (!GEP)
169+
continue;
170+
PointerType *PtrTy = dyn_cast<PointerType>(L->getType());
171+
if (!PtrTy)
172+
continue;
173+
std::optional<JumpTableTy> JumpTable = parseJumpTable(GEP, PtrTy);
174+
if (!JumpTable)
175+
continue;
176+
SplittedOutTail = split(Call, *JumpTable, DTU);
177+
Changed = true;
178+
break;
179+
}
180+
CurrentBB = SplittedOutTail ? SplittedOutTail : nullptr;
181+
}
182+
}
183+
184+
if (!Changed)
185+
return PreservedAnalyses::all();
186+
187+
PreservedAnalyses PA;
188+
if (DT)
189+
PA.preserve<DominatorTreeAnalysis>();
190+
if (PDT)
191+
PA.preserve<PostDominatorTreeAnalysis>();
192+
return PA;
193+
}

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)