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