LLVM 23.0.0git
AArch64CompressJumpTables.cpp
Go to the documentation of this file.
1//==-- AArch64CompressJumpTables.cpp - Compress jump tables for AArch64 --====//
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// This pass looks at the basic blocks each jump-table refers to and works out
8// whether they can be emitted in a compressed form (with 8 or 16-bit
9// entries). If so, it changes the opcode and flags them in the associated
10// AArch64FunctionInfo.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AArch64.h"
16#include "AArch64Subtarget.h"
17#include "llvm/ADT/Statistic.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "aarch64-jump-tables"
27
28STATISTIC(NumJT8, "Number of jump-tables with 1-byte entries");
29STATISTIC(NumJT16, "Number of jump-tables with 2-byte entries");
30STATISTIC(NumJT32, "Number of jump-tables with 4-byte entries");
31
32namespace {
33class AArch64CompressJumpTablesImpl {
34 const TargetInstrInfo *TII = nullptr;
35 MachineFunction *MF = nullptr;
36 SmallVector<int, 8> BlockInfo;
37
38 /// Returns the size of instructions in the block \p MBB, or std::nullopt if
39 /// we couldn't get a safe upper bound.
40 std::optional<int> computeBlockSize(MachineBasicBlock &MBB);
41
42 /// Gather information about the function, returns false if we can't perform
43 /// this optimization for some reason.
44 bool scanFunction();
45
46 bool compressJumpTable(MachineInstr &MI, int Offset);
47
48public:
49 bool run(MachineFunction &MF);
50};
51
52class AArch64CompressJumpTablesLegacy : public MachineFunctionPass {
53public:
54 static char ID;
55 AArch64CompressJumpTablesLegacy() : MachineFunctionPass(ID) {}
56
57 bool runOnMachineFunction(MachineFunction &MF) override;
58
59 MachineFunctionProperties getRequiredProperties() const override {
60 return MachineFunctionProperties().setNoVRegs();
61 }
62 StringRef getPassName() const override {
63 return "AArch64 Compress Jump Tables";
64 }
65};
66char AArch64CompressJumpTablesLegacy::ID = 0;
67} // namespace
68
69INITIALIZE_PASS(AArch64CompressJumpTablesLegacy, DEBUG_TYPE,
70 "AArch64 compress jump tables pass", false, false)
71
72std::optional<int>
73AArch64CompressJumpTablesImpl::computeBlockSize(MachineBasicBlock &MBB) {
74 int Size = 0;
75 for (const MachineInstr &MI : MBB) {
76 // Inline asm may contain some directives like .bytes which we don't
77 // currently have the ability to parse accurately. To be safe, just avoid
78 // computing a size and bail out.
79 if (MI.getOpcode() == AArch64::INLINEASM ||
80 MI.getOpcode() == AArch64::INLINEASM_BR)
81 return std::nullopt;
82 Size += TII->getInstSizeInBytes(MI);
83 }
84 return Size;
85}
86
87bool AArch64CompressJumpTablesImpl::scanFunction() {
88 BlockInfo.clear();
89 BlockInfo.resize(MF->getNumBlockIDs());
90
91 // NOTE: BlockSize, Offset, OffsetAfterAlignment are all upper bounds.
92
93 unsigned Offset = 0;
94 for (MachineBasicBlock &MBB : *MF) {
95 const Align Alignment = MBB.getAlignment();
96 unsigned OffsetAfterAlignment = Offset;
97 // We don't know the exact size of MBB so assume worse case padding.
98 if (Alignment > Align(4))
99 OffsetAfterAlignment += Alignment.value() - 4;
100 BlockInfo[MBB.getNumber()] = OffsetAfterAlignment;
101 auto BlockSize = computeBlockSize(MBB);
102 if (!BlockSize)
103 return false;
104 Offset = OffsetAfterAlignment + *BlockSize;
105 }
106 return true;
107}
108
109bool AArch64CompressJumpTablesImpl::compressJumpTable(MachineInstr &MI,
110 int Offset) {
111 if (MI.getOpcode() != AArch64::JumpTableDest32)
112 return false;
113
114 int JTIdx = MI.getOperand(4).getIndex();
115 auto &JTInfo = *MF->getJumpTableInfo();
116 const MachineJumpTableEntry &JT = JTInfo.getJumpTables()[JTIdx];
117
118 // The jump-table might have been optimized away.
119 if (JT.MBBs.empty())
120 return false;
121
122 int MaxOffset = std::numeric_limits<int>::min(),
123 MinOffset = std::numeric_limits<int>::max();
124 MachineBasicBlock *MinBlock = nullptr;
125 for (auto *Block : JT.MBBs) {
126 int BlockOffset = BlockInfo[Block->getNumber()];
127 assert(BlockOffset % 4 == 0 && "misaligned basic block");
128
129 MaxOffset = std::max(MaxOffset, BlockOffset);
130 if (BlockOffset <= MinOffset) {
131 MinOffset = BlockOffset;
132 MinBlock = Block;
133 }
134 }
135 assert(MinBlock && "Failed to find minimum offset block");
136
137 // The ADR instruction needed to calculate the address of the first reachable
138 // basic block can address +/-1MB.
139 if (!isInt<21>(MinOffset - Offset)) {
140 ++NumJT32;
141 return false;
142 }
143
144 int Span = MaxOffset - MinOffset;
145 auto *AFI = MF->getInfo<AArch64FunctionInfo>();
146 if (isUInt<8>(Span / 4)) {
147 AFI->setJumpTableEntryInfo(JTIdx, 1, MinBlock->getSymbol());
148 MI.setDesc(TII->get(AArch64::JumpTableDest8));
149 ++NumJT8;
150 return true;
151 }
152 if (isUInt<16>(Span / 4)) {
153 AFI->setJumpTableEntryInfo(JTIdx, 2, MinBlock->getSymbol());
154 MI.setDesc(TII->get(AArch64::JumpTableDest16));
155 ++NumJT16;
156 return true;
157 }
158
159 ++NumJT32;
160 return false;
161}
162
163bool AArch64CompressJumpTablesImpl::run(MachineFunction &MFIn) {
164 bool Changed = false;
165 MF = &MFIn;
166
167 const auto &ST = MF->getSubtarget<AArch64Subtarget>();
168 TII = ST.getInstrInfo();
169
170 if (ST.force32BitJumpTables() && !MF->getFunction().hasMinSize())
171 return false;
172
173 if (!scanFunction())
174 return false;
175
176 for (MachineBasicBlock &MBB : *MF) {
177 int Offset = BlockInfo[MBB.getNumber()];
178 for (MachineInstr &MI : MBB) {
179 Changed |= compressJumpTable(MI, Offset);
180 Offset += TII->getInstSizeInBytes(MI);
181 }
182 }
183
184 return Changed;
185}
186
187bool AArch64CompressJumpTablesLegacy::runOnMachineFunction(
188 MachineFunction &MF) {
189 return AArch64CompressJumpTablesImpl().run(MF);
190}
191
192PreservedAnalyses
195 const bool Changed = AArch64CompressJumpTablesImpl().run(MF);
196 if (!Changed)
197 return PreservedAnalyses::all();
200 return PA;
201}
202
204 return new AArch64CompressJumpTablesLegacy();
205}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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
static const int BlockSize
Definition TarWriter.cpp:33
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
Align getAlignment() const
Return alignment of the basic block.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
Representation of each machine instruction.
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 & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
void resize(size_type N)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
Changed
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createAArch64CompressJumpTablesPass()
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
std::vector< MachineBasicBlock * > MBBs
MBBs - The vector of basic blocks from which to create the jump table.