Skip to content

Commit 3a455b3

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

File tree

13 files changed

+742
-0
lines changed

13 files changed

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

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)