LLVM 20.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 AArch64CompressJumpTables : public MachineFunctionPass {
34 const TargetInstrInfo *TII;
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 static char ID;
50 AArch64CompressJumpTables() : MachineFunctionPass(ID) {
52 }
53
54 bool runOnMachineFunction(MachineFunction &MF) override;
55
58 MachineFunctionProperties::Property::NoVRegs);
59 }
60 StringRef getPassName() const override {
61 return "AArch64 Compress Jump Tables";
62 }
63};
64char AArch64CompressJumpTables::ID = 0;
65} // namespace
66
67INITIALIZE_PASS(AArch64CompressJumpTables, DEBUG_TYPE,
68 "AArch64 compress jump tables pass", false, false)
69
70std::optional<int>
71AArch64CompressJumpTables::computeBlockSize(MachineBasicBlock &MBB) {
72 int Size = 0;
73 for (const MachineInstr &MI : MBB) {
74 // Inline asm may contain some directives like .bytes which we don't
75 // currently have the ability to parse accurately. To be safe, just avoid
76 // computing a size and bail out.
77 if (MI.getOpcode() == AArch64::INLINEASM ||
78 MI.getOpcode() == AArch64::INLINEASM_BR)
79 return std::nullopt;
80 Size += TII->getInstSizeInBytes(MI);
81 }
82 return Size;
83}
84
85bool AArch64CompressJumpTables::scanFunction() {
86 BlockInfo.clear();
87 BlockInfo.resize(MF->getNumBlockIDs());
88
89 // NOTE: BlockSize, Offset, OffsetAfterAlignment are all upper bounds.
90
91 unsigned Offset = 0;
92 for (MachineBasicBlock &MBB : *MF) {
93 const Align Alignment = MBB.getAlignment();
94 unsigned OffsetAfterAlignment = Offset;
95 // We don't know the exact size of MBB so assume worse case padding.
96 if (Alignment > Align(4))
97 OffsetAfterAlignment += Alignment.value() - 4;
98 BlockInfo[MBB.getNumber()] = OffsetAfterAlignment;
99 auto BlockSize = computeBlockSize(MBB);
100 if (!BlockSize)
101 return false;
102 Offset = OffsetAfterAlignment + *BlockSize;
103 }
104 return true;
105}
106
107bool AArch64CompressJumpTables::compressJumpTable(MachineInstr &MI,
108 int Offset) {
109 if (MI.getOpcode() != AArch64::JumpTableDest32)
110 return false;
111
112 int JTIdx = MI.getOperand(4).getIndex();
113 auto &JTInfo = *MF->getJumpTableInfo();
114 const MachineJumpTableEntry &JT = JTInfo.getJumpTables()[JTIdx];
115
116 // The jump-table might have been optimized away.
117 if (JT.MBBs.empty())
118 return false;
119
120 int MaxOffset = std::numeric_limits<int>::min(),
121 MinOffset = std::numeric_limits<int>::max();
122 MachineBasicBlock *MinBlock = nullptr;
123 for (auto *Block : JT.MBBs) {
124 int BlockOffset = BlockInfo[Block->getNumber()];
125 assert(BlockOffset % 4 == 0 && "misaligned basic block");
126
127 MaxOffset = std::max(MaxOffset, BlockOffset);
128 if (BlockOffset <= MinOffset) {
129 MinOffset = BlockOffset;
130 MinBlock = Block;
131 }
132 }
133 assert(MinBlock && "Failed to find minimum offset block");
134
135 // The ADR instruction needed to calculate the address of the first reachable
136 // basic block can address +/-1MB.
137 if (!isInt<21>(MinOffset - Offset)) {
138 ++NumJT32;
139 return false;
140 }
141
142 int Span = MaxOffset - MinOffset;
143 auto *AFI = MF->getInfo<AArch64FunctionInfo>();
144 if (isUInt<8>(Span / 4)) {
145 AFI->setJumpTableEntryInfo(JTIdx, 1, MinBlock->getSymbol());
146 MI.setDesc(TII->get(AArch64::JumpTableDest8));
147 ++NumJT8;
148 return true;
149 }
150 if (isUInt<16>(Span / 4)) {
151 AFI->setJumpTableEntryInfo(JTIdx, 2, MinBlock->getSymbol());
152 MI.setDesc(TII->get(AArch64::JumpTableDest16));
153 ++NumJT16;
154 return true;
155 }
156
157 ++NumJT32;
158 return false;
159}
160
161bool AArch64CompressJumpTables::runOnMachineFunction(MachineFunction &MFIn) {
162 bool Changed = false;
163 MF = &MFIn;
164
165 const auto &ST = MF->getSubtarget<AArch64Subtarget>();
166 TII = ST.getInstrInfo();
167
168 if (ST.force32BitJumpTables() && !MF->getFunction().hasMinSize())
169 return false;
170
171 if (!scanFunction())
172 return false;
173
174 for (MachineBasicBlock &MBB : *MF) {
175 int Offset = BlockInfo[MBB.getNumber()];
176 for (MachineInstr &MI : MBB) {
177 Changed |= compressJumpTable(MI, Offset);
178 Offset += TII->getInstSizeInBytes(MI);
179 }
180 }
181
182 return Changed;
183}
184
186 return new AArch64CompressJumpTables();
187}
#define DEBUG_TYPE
MachineBasicBlock & MBB
uint64_t Size
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:166
static const int BlockSize
Definition: TarWriter.cpp:33
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
void setJumpTableEntryInfo(int Idx, unsigned Size, MCSymbol *PCRelSym)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
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...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
Representation of each machine instruction.
Definition: MachineInstr.h:69
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
void resize(size_type N)
Definition: SmallVector.h:638
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
TargetInstrInfo - Interface to description of machine instruction set.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
FunctionPass * createAArch64CompressJumpTablesPass()
void initializeAArch64CompressJumpTablesPass(PassRegistry &)
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
MachineJumpTableEntry - One jump table in the jump table info.