1 //==-- AArch64CompressJumpTables.cpp - Compress jump tables for AArch64 --====//
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
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.
12 //===----------------------------------------------------------------------===//
15 #include "AArch64MachineFunctionInfo.h"
16 #include "AArch64Subtarget.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineJumpTableInfo.h"
20 #include "llvm/CodeGen/TargetInstrInfo.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/Support/Debug.h"
27 #define DEBUG_TYPE "aarch64-jump-tables"
29 STATISTIC(NumJT8
, "Number of jump-tables with 1-byte entries");
30 STATISTIC(NumJT16
, "Number of jump-tables with 2-byte entries");
31 STATISTIC(NumJT32
, "Number of jump-tables with 4-byte entries");
34 class AArch64CompressJumpTables
: public MachineFunctionPass
{
35 const TargetInstrInfo
*TII
;
37 SmallVector
<int, 8> BlockInfo
;
39 int computeBlockSize(MachineBasicBlock
&MBB
);
42 bool compressJumpTable(MachineInstr
&MI
, int Offset
);
46 AArch64CompressJumpTables() : MachineFunctionPass(ID
) {
47 initializeAArch64CompressJumpTablesPass(*PassRegistry::getPassRegistry());
50 bool runOnMachineFunction(MachineFunction
&MF
) override
;
52 MachineFunctionProperties
getRequiredProperties() const override
{
53 return MachineFunctionProperties().set(
54 MachineFunctionProperties::Property::NoVRegs
);
56 StringRef
getPassName() const override
{
57 return "AArch64 Compress Jump Tables";
60 char AArch64CompressJumpTables::ID
= 0;
63 INITIALIZE_PASS(AArch64CompressJumpTables
, DEBUG_TYPE
,
64 "AArch64 compress jump tables pass", false, false)
66 int AArch64CompressJumpTables::computeBlockSize(MachineBasicBlock
&MBB
) {
68 for (const MachineInstr
&MI
: MBB
)
69 Size
+= TII
->getInstSizeInBytes(MI
);
73 void AArch64CompressJumpTables::scanFunction() {
75 BlockInfo
.resize(MF
->getNumBlockIDs());
78 for (MachineBasicBlock
&MBB
: *MF
) {
79 BlockInfo
[MBB
.getNumber()] = Offset
;
80 Offset
+= computeBlockSize(MBB
);
84 bool AArch64CompressJumpTables::compressJumpTable(MachineInstr
&MI
,
86 if (MI
.getOpcode() != AArch64::JumpTableDest32
)
89 int JTIdx
= MI
.getOperand(4).getIndex();
90 auto &JTInfo
= *MF
->getJumpTableInfo();
91 const MachineJumpTableEntry
&JT
= JTInfo
.getJumpTables()[JTIdx
];
93 // The jump-table might have been optimized away.
97 int MaxOffset
= std::numeric_limits
<int>::min(),
98 MinOffset
= std::numeric_limits
<int>::max();
99 MachineBasicBlock
*MinBlock
= nullptr;
100 for (auto Block
: JT
.MBBs
) {
101 int BlockOffset
= BlockInfo
[Block
->getNumber()];
102 assert(BlockOffset
% 4 == 0 && "misaligned basic block");
104 MaxOffset
= std::max(MaxOffset
, BlockOffset
);
105 if (BlockOffset
<= MinOffset
) {
106 MinOffset
= BlockOffset
;
111 // The ADR instruction needed to calculate the address of the first reachable
112 // basic block can address +/-1MB.
113 if (!isInt
<21>(MinOffset
- Offset
)) {
118 int Span
= MaxOffset
- MinOffset
;
119 auto AFI
= MF
->getInfo
<AArch64FunctionInfo
>();
120 if (isUInt
<8>(Span
/ 4)) {
121 AFI
->setJumpTableEntryInfo(JTIdx
, 1, MinBlock
->getSymbol());
122 MI
.setDesc(TII
->get(AArch64::JumpTableDest8
));
125 } else if (isUInt
<16>(Span
/ 4)) {
126 AFI
->setJumpTableEntryInfo(JTIdx
, 2, MinBlock
->getSymbol());
127 MI
.setDesc(TII
->get(AArch64::JumpTableDest16
));
136 bool AArch64CompressJumpTables::runOnMachineFunction(MachineFunction
&MFIn
) {
137 bool Changed
= false;
140 const auto &ST
= MF
->getSubtarget
<AArch64Subtarget
>();
141 TII
= ST
.getInstrInfo();
143 if (ST
.force32BitJumpTables() && !MF
->getFunction().optForMinSize())
148 for (MachineBasicBlock
&MBB
: *MF
) {
149 int Offset
= BlockInfo
[MBB
.getNumber()];
150 for (MachineInstr
&MI
: MBB
) {
151 Changed
|= compressJumpTable(MI
, Offset
);
152 Offset
+= TII
->getInstSizeInBytes(MI
);
159 FunctionPass
*llvm::createAArch64CompressJumpTablesPass() {
160 return new AArch64CompressJumpTables();