LLVM 24.0.0git
JumpTableToSwitch.cpp
Go to the documentation of this file.
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
10#include "ScalarOptions.h"
11#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/Statistic.h"
18#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/LLVMContext.h"
23#include <limits>
24
25using namespace llvm;
26
27#define DEBUG_TYPE "jump-table-to-switch"
28
29STATISTIC(NumEligibleJumpTables, "The number of jump tables seen by the pass "
30 "that can be converted if deemed profitable.");
31STATISTIC(NumJumpTablesConverted,
32 "The number of jump tables converted into switches.");
33
34namespace {
35struct JumpTableTy {
36 Value *Index;
38};
39} // anonymous namespace
40
41static std::optional<JumpTableTy> parseJumpTable(const ScalarOptions &Opts,
43 PointerType *PtrTy,
44 FunctionType *CallFTy) {
45 Constant *Ptr = dyn_cast<Constant>(GEP->getPointerOperand());
46 if (!Ptr)
47 return std::nullopt;
48
50 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
51 return std::nullopt;
52
53 Function &F = *GEP->getParent()->getParent();
54 const DataLayout &DL = F.getDataLayout();
55 const unsigned BitWidth =
56 DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
58 APInt ConstantOffset(BitWidth, 0);
59 if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
60 return std::nullopt;
61 if (VariableOffsets.size() != 1)
62 return std::nullopt;
63 // TODO: consider supporting more general patterns
64 if (!ConstantOffset.isZero())
65 return std::nullopt;
66 APInt StrideBytes = VariableOffsets.front().second;
67 const uint64_t JumpTableSizeBytes = GV->getGlobalSize(DL);
68 if (JumpTableSizeBytes % StrideBytes.getZExtValue() != 0)
69 return std::nullopt;
70 ++NumEligibleJumpTables;
71 const uint64_t N = JumpTableSizeBytes / StrideBytes.getZExtValue();
72 if (N > Opts.jump_table_to_switch_size_threshold)
73 return std::nullopt;
74
75 JumpTableTy JumpTable;
76 JumpTable.Index = VariableOffsets.front().first;
77 JumpTable.Funcs.reserve(N);
78 for (uint64_t Index = 0; Index < N; ++Index) {
79 // ConstantOffset is zero.
80 APInt Offset = Index * StrideBytes;
81 Constant *C =
83 if (!C)
84 return std::nullopt;
86 !NullPointerIsDefined(&F, PtrTy->getAddressSpace())) ||
88 continue;
89 auto *Func = dyn_cast<Function>(C);
90 if (!Func || Func->isDeclaration() || Func->getFunctionType() != CallFTy ||
91 Func->getInstructionCount() >
92 Opts.jump_table_to_switch_function_size_threshold)
93 return std::nullopt;
94 JumpTable.Funcs.push_back({Index, Func});
95 }
96 // Empty table, or only values that are UB when called
97 if (JumpTable.Funcs.empty())
98 return std::nullopt;
99 return JumpTable;
100}
101
102static BasicBlock *
103expandToSwitch(CallBase *CB, const JumpTableTy &JT, DomTreeUpdater &DTU,
106 GetGuidForFunction) {
107 ++NumJumpTablesConverted;
108 const bool IsVoid = CB->getType() == Type::getVoidTy(CB->getContext());
109
111 BasicBlock *BB = CB->getParent();
112 BasicBlock *Tail = SplitBlock(BB, CB, &DTU, nullptr, nullptr,
113 BB->getName() + Twine(".tail"));
114 DTUpdates.push_back({DominatorTree::Delete, BB, Tail});
116
117 Function &F = *BB->getParent();
118 BasicBlock *BBUnreachable = BasicBlock::Create(
119 F.getContext(), "default.switch.case.unreachable", &F, Tail);
120 IRBuilder<> BuilderUnreachable(BBUnreachable);
121 BuilderUnreachable.CreateUnreachable();
122
123 IRBuilder<> Builder(BB);
124 SwitchInst *Switch = Builder.CreateSwitch(JT.Index, BBUnreachable);
125 DTUpdates.push_back({DominatorTree::Insert, BB, BBUnreachable});
126
127 IRBuilder<> BuilderTail(CB);
128 PHINode *PHI =
129 IsVoid ? nullptr : BuilderTail.CreatePHI(CB->getType(), JT.Funcs.size());
130 const auto *ProfMD = CB->getMetadata(LLVMContext::MD_prof);
131
132 SmallVector<uint64_t> BranchWeights;
134 const bool HadProfile = isValueProfileMD(ProfMD);
135 if (HadProfile) {
136 // The assumptions, coming in, are that the functions in JT.Funcs are
137 // defined in this module (from parseJumpTable).
138 assert(llvm::all_of(JT.Funcs, [](auto &F) {
139 return F.second && !F.second->isDeclaration();
140 }));
141 BranchWeights.reserve(JT.Funcs.size() + 1);
142 // The first is the default target, which is the unreachable block created
143 // above.
144 BranchWeights.push_back(0U);
145 uint64_t TotalCount = 0;
146 auto Targets = getValueProfDataFromInst(
147 *CB, InstrProfValueKind::IPVK_IndirectCallTarget,
148 std::numeric_limits<uint32_t>::max(), TotalCount);
149
150 for (const auto &[G, C] : Targets) {
151 [[maybe_unused]] auto It = GuidToCounter.insert({G, C});
152 // We should always be inserting as it is verifier-enforced IR invariant
153 // that VP metadata does not have duplicate values.
154 assert(It.second);
155 }
156 }
157 for (auto [Index, Func] : JT.Funcs) {
158 BasicBlock *B = BasicBlock::Create(Func->getContext(),
159 "call." + Twine(Index), &F, Tail);
160 DTUpdates.push_back({DominatorTree::Insert, BB, B});
161 DTUpdates.push_back({DominatorTree::Insert, B, Tail});
162
164 // The MD_prof metadata (VP kind), if it existed, can be dropped, it doesn't
165 // make sense on a direct call. Note that the values are used for the branch
166 // weights of the switch.
167 Call->setMetadata(LLVMContext::MD_prof, nullptr);
168 Call->setCalledFunction(Func);
169 Call->insertInto(B, B->end());
170 Switch->addCase(
171 cast<ConstantInt>(ConstantInt::get(JT.Index->getType(), Index)), B);
172 GlobalValue::GUID FctID = GetGuidForFunction(*Func);
173 // It'd be OK to _not_ find target functions in GuidToCounter, e.g. suppose
174 // just some of the jump targets are taken (for the given profile).
175 BranchWeights.push_back(FctID == 0U ? 0U
176 : GuidToCounter.lookup_or(FctID, 0U));
177 UncondBrInst::Create(Tail, B);
178 if (PHI)
179 PHI->addIncoming(Call, B);
180 }
181 DTU.applyUpdates(DTUpdates);
182 ORE.emit([&]() {
183 return OptimizationRemark(DEBUG_TYPE, "ReplacedJumpTableWithSwitch", CB)
184 << "expanded indirect call into switch";
185 });
186 // Only set branch weights on the switch if we have non-zero branch weights.
187 // We can have no non-zero branch weights while having VP metadata if for
188 // example, all of the functions are external and not instrumented.
189 if (HadProfile && llvm::any_of(BranchWeights, not_equal_to(0))) {
190 setBranchWeights(*Switch, downscaleWeights(BranchWeights),
191 /*IsExpected=*/false);
192 } else
194 if (PHI)
196 CB->eraseFromParent();
197 return Tail;
198}
199
206 DomTreeUpdater DTU(DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy);
207 const ScalarOptions &Opts = getScalarOptions(F);
208 bool Changed = false;
209 auto FuncToGuid = [&](const Function &Fct) {
210 if (const auto MaybeGUID = Fct.getGUIDIfAssigned(); MaybeGUID)
211 return *MaybeGUID;
212
214 getIRPGOObjectName(Fct, InLTO));
215 };
216
217 for (BasicBlock &BB : make_early_inc_range(F)) {
218 BasicBlock *CurrentBB = &BB;
219 while (CurrentBB) {
220 BasicBlock *SplittedOutTail = nullptr;
221 for (Instruction &I : make_early_inc_range(*CurrentBB)) {
222 auto *Call = dyn_cast<CallInst>(&I);
223 if (!Call || Call->getCalledFunction() || Call->isMustTailCall())
224 continue;
225 auto *L = dyn_cast<LoadInst>(Call->getCalledOperand());
226 // Skip atomic or volatile loads.
227 if (!L || !L->isSimple())
228 continue;
229 auto *GEP = dyn_cast<GetElementPtrInst>(L->getPointerOperand());
230 if (!GEP ||
231 (!GEP->hasNoUnsignedWrap() && !GEP->hasNoUnsignedSignedWrap()))
232 continue;
233 auto *PtrTy = dyn_cast<PointerType>(L->getType());
234 assert(PtrTy && "call operand must be a pointer");
235 std::optional<JumpTableTy> JumpTable =
236 parseJumpTable(Opts, GEP, PtrTy, Call->getFunctionType());
237 if (!JumpTable)
238 continue;
239 SplittedOutTail =
240 expandToSwitch(Call, *JumpTable, DTU, ORE, FuncToGuid);
241 Changed = true;
242 break;
243 }
244 CurrentBB = SplittedOutTail ? SplittedOutTail : nullptr;
245 }
246 }
247
248 if (!Changed)
249 return PreservedAnalyses::all();
250
252 if (DT)
254 if (PDT)
256 return PA;
257}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define DEBUG_TYPE
Hexagon Common GEP
static BasicBlock * expandToSwitch(CallBase *CB, const JumpTableTy &JT, DomTreeUpdater &DTU, OptimizationRemarkEmitter &ORE, llvm::function_ref< GlobalValue::GUID(const Function &)> GetGuidForFunction)
static std::optional< JumpTableTy > parseJumpTable(const ScalarOptions &Opts, GetElementPtrInst *GEP, PointerType *PtrTy, FunctionType *CallFTy)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file contains the declarations for profiling metadata utility functions.
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:376
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This is an important base class in LLVM.
Definition Constant.h:44
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
ValueT lookup_or(const_arg_type_t< KeyT > Val, U &&Default) const
Definition DenseMap.h:803
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:827
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:80
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ABI uint64_t getGlobalSize(const DataLayout &DL) const
Get the size of this global variable in bytes.
Definition Globals.cpp:640
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
UnreachableInst * CreateUnreachable()
Definition IRBuilder.h:1354
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition IRBuilder.h:2547
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2909
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Run the pass over the function.
size_type size() const
Definition MapVector.h:58
std::pair< KeyT, ValueT > & front()
Definition MapVector.h:81
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Diagnostic information for applied optimization remarks.
Analysis pass which computes a PostDominatorTree.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Multiway switch.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:272
static UncondBrInst * Create(BasicBlock *Target, InsertPosition InsertBefore=nullptr)
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:549
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:315
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
CallInst * Call
Changed
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
constexpr auto not_equal_to(T &&Arg)
Functor variant of std::not_equal_to that can be used as a UnaryPredicate in functional algorithms li...
Definition STLExtras.h:2196
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
LLVM_ABI std::string getIRPGOObjectName(const GlobalObject &GO, bool InLTO=false)
const ScalarOptions & getScalarOptions(const LLVMContext &C)
LLVM_ABI void setExplicitlyUnknownBranchWeights(Instruction &I, StringRef PassName)
Specify that the branch weights for this terminator cannot be known at compile time.
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected, bool ElideAllZero=false)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
LLVM_ABI SmallVector< InstrProfValueData, 4 > getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind, uint32_t MaxNumValueData, uint64_t &TotalC, bool GetNoICPValue=false)
Extract the value profile data from Inst and returns them if Inst is annotated with value profile dat...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool isValueProfileMD(const MDNode *ProfileData)
Checks if an MDNode contains value profiling Metadata.
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the specified block at the specified instruction.
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI SmallVector< uint32_t > downscaleWeights(ArrayRef< uint64_t > Weights, std::optional< uint64_t > KnownMaxCount=std::nullopt)
downscale the given weights preserving the ratio.
#define N
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:342