109467b48Spatrick //==-- AArch64CompressJumpTables.cpp - Compress jump tables for AArch64 --====//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick // This pass looks at the basic blocks each jump-table refers to and works out
809467b48Spatrick // whether they can be emitted in a compressed form (with 8 or 16-bit
909467b48Spatrick // entries). If so, it changes the opcode and flags them in the associated
1009467b48Spatrick // AArch64FunctionInfo.
1109467b48Spatrick //
1209467b48Spatrick //===----------------------------------------------------------------------===//
1309467b48Spatrick
1409467b48Spatrick #include "AArch64.h"
1509467b48Spatrick #include "AArch64MachineFunctionInfo.h"
1609467b48Spatrick #include "AArch64Subtarget.h"
1709467b48Spatrick #include "llvm/ADT/Statistic.h"
1809467b48Spatrick #include "llvm/CodeGen/MachineFunctionPass.h"
1909467b48Spatrick #include "llvm/CodeGen/MachineJumpTableInfo.h"
2009467b48Spatrick #include "llvm/CodeGen/TargetInstrInfo.h"
2109467b48Spatrick #include "llvm/CodeGen/TargetSubtargetInfo.h"
2209467b48Spatrick #include "llvm/MC/MCContext.h"
2309467b48Spatrick #include "llvm/Support/Alignment.h"
2409467b48Spatrick #include "llvm/Support/Debug.h"
2509467b48Spatrick
2609467b48Spatrick using namespace llvm;
2709467b48Spatrick
2809467b48Spatrick #define DEBUG_TYPE "aarch64-jump-tables"
2909467b48Spatrick
3009467b48Spatrick STATISTIC(NumJT8, "Number of jump-tables with 1-byte entries");
3109467b48Spatrick STATISTIC(NumJT16, "Number of jump-tables with 2-byte entries");
3209467b48Spatrick STATISTIC(NumJT32, "Number of jump-tables with 4-byte entries");
3309467b48Spatrick
3409467b48Spatrick namespace {
3509467b48Spatrick class AArch64CompressJumpTables : public MachineFunctionPass {
3609467b48Spatrick const TargetInstrInfo *TII;
3709467b48Spatrick MachineFunction *MF;
3809467b48Spatrick SmallVector<int, 8> BlockInfo;
3909467b48Spatrick
40*d415bd75Srobert /// Returns the size in instructions of the block \p MBB, or std::nullopt if
41*d415bd75Srobert /// we couldn't get a safe upper bound.
42*d415bd75Srobert std::optional<int> computeBlockSize(MachineBasicBlock &MBB);
4373471bf0Spatrick
4473471bf0Spatrick /// Gather information about the function, returns false if we can't perform
4573471bf0Spatrick /// this optimization for some reason.
4673471bf0Spatrick bool scanFunction();
4709467b48Spatrick
4809467b48Spatrick bool compressJumpTable(MachineInstr &MI, int Offset);
4909467b48Spatrick
5009467b48Spatrick public:
5109467b48Spatrick static char ID;
AArch64CompressJumpTables()5209467b48Spatrick AArch64CompressJumpTables() : MachineFunctionPass(ID) {
5309467b48Spatrick initializeAArch64CompressJumpTablesPass(*PassRegistry::getPassRegistry());
5409467b48Spatrick }
5509467b48Spatrick
5609467b48Spatrick bool runOnMachineFunction(MachineFunction &MF) override;
5709467b48Spatrick
getRequiredProperties() const5809467b48Spatrick MachineFunctionProperties getRequiredProperties() const override {
5909467b48Spatrick return MachineFunctionProperties().set(
6009467b48Spatrick MachineFunctionProperties::Property::NoVRegs);
6109467b48Spatrick }
getPassName() const6209467b48Spatrick StringRef getPassName() const override {
6309467b48Spatrick return "AArch64 Compress Jump Tables";
6409467b48Spatrick }
6509467b48Spatrick };
6609467b48Spatrick char AArch64CompressJumpTables::ID = 0;
6773471bf0Spatrick } // namespace
6809467b48Spatrick
6909467b48Spatrick INITIALIZE_PASS(AArch64CompressJumpTables, DEBUG_TYPE,
7009467b48Spatrick "AArch64 compress jump tables pass", false, false)
7109467b48Spatrick
72*d415bd75Srobert std::optional<int>
computeBlockSize(MachineBasicBlock & MBB)7373471bf0Spatrick AArch64CompressJumpTables::computeBlockSize(MachineBasicBlock &MBB) {
7409467b48Spatrick int Size = 0;
7573471bf0Spatrick for (const MachineInstr &MI : MBB) {
7673471bf0Spatrick // Inline asm may contain some directives like .bytes which we don't
7773471bf0Spatrick // currently have the ability to parse accurately. To be safe, just avoid
7873471bf0Spatrick // computing a size and bail out.
7973471bf0Spatrick if (MI.getOpcode() == AArch64::INLINEASM ||
8073471bf0Spatrick MI.getOpcode() == AArch64::INLINEASM_BR)
81*d415bd75Srobert return std::nullopt;
8209467b48Spatrick Size += TII->getInstSizeInBytes(MI);
8373471bf0Spatrick }
8409467b48Spatrick return Size;
8509467b48Spatrick }
8609467b48Spatrick
scanFunction()8773471bf0Spatrick bool AArch64CompressJumpTables::scanFunction() {
8809467b48Spatrick BlockInfo.clear();
8909467b48Spatrick BlockInfo.resize(MF->getNumBlockIDs());
9009467b48Spatrick
9109467b48Spatrick unsigned Offset = 0;
9209467b48Spatrick for (MachineBasicBlock &MBB : *MF) {
9309467b48Spatrick const Align Alignment = MBB.getAlignment();
9409467b48Spatrick unsigned AlignedOffset;
95097a140dSpatrick if (Alignment == Align(1))
9609467b48Spatrick AlignedOffset = Offset;
9709467b48Spatrick else
9809467b48Spatrick AlignedOffset = alignTo(Offset, Alignment);
9909467b48Spatrick BlockInfo[MBB.getNumber()] = AlignedOffset;
10073471bf0Spatrick auto BlockSize = computeBlockSize(MBB);
10173471bf0Spatrick if (!BlockSize)
10273471bf0Spatrick return false;
10373471bf0Spatrick Offset = AlignedOffset + *BlockSize;
10409467b48Spatrick }
10573471bf0Spatrick return true;
10609467b48Spatrick }
10709467b48Spatrick
compressJumpTable(MachineInstr & MI,int Offset)10809467b48Spatrick bool AArch64CompressJumpTables::compressJumpTable(MachineInstr &MI,
10909467b48Spatrick int Offset) {
11009467b48Spatrick if (MI.getOpcode() != AArch64::JumpTableDest32)
11109467b48Spatrick return false;
11209467b48Spatrick
11309467b48Spatrick int JTIdx = MI.getOperand(4).getIndex();
11409467b48Spatrick auto &JTInfo = *MF->getJumpTableInfo();
11509467b48Spatrick const MachineJumpTableEntry &JT = JTInfo.getJumpTables()[JTIdx];
11609467b48Spatrick
11709467b48Spatrick // The jump-table might have been optimized away.
11809467b48Spatrick if (JT.MBBs.empty())
11909467b48Spatrick return false;
12009467b48Spatrick
12109467b48Spatrick int MaxOffset = std::numeric_limits<int>::min(),
12209467b48Spatrick MinOffset = std::numeric_limits<int>::max();
12309467b48Spatrick MachineBasicBlock *MinBlock = nullptr;
12473471bf0Spatrick for (auto *Block : JT.MBBs) {
12509467b48Spatrick int BlockOffset = BlockInfo[Block->getNumber()];
12609467b48Spatrick assert(BlockOffset % 4 == 0 && "misaligned basic block");
12709467b48Spatrick
12809467b48Spatrick MaxOffset = std::max(MaxOffset, BlockOffset);
12909467b48Spatrick if (BlockOffset <= MinOffset) {
13009467b48Spatrick MinOffset = BlockOffset;
13109467b48Spatrick MinBlock = Block;
13209467b48Spatrick }
13309467b48Spatrick }
13409467b48Spatrick assert(MinBlock && "Failed to find minimum offset block");
13509467b48Spatrick
13609467b48Spatrick // The ADR instruction needed to calculate the address of the first reachable
13709467b48Spatrick // basic block can address +/-1MB.
13809467b48Spatrick if (!isInt<21>(MinOffset - Offset)) {
13909467b48Spatrick ++NumJT32;
14009467b48Spatrick return false;
14109467b48Spatrick }
14209467b48Spatrick
14309467b48Spatrick int Span = MaxOffset - MinOffset;
14473471bf0Spatrick auto *AFI = MF->getInfo<AArch64FunctionInfo>();
14509467b48Spatrick if (isUInt<8>(Span / 4)) {
14609467b48Spatrick AFI->setJumpTableEntryInfo(JTIdx, 1, MinBlock->getSymbol());
14709467b48Spatrick MI.setDesc(TII->get(AArch64::JumpTableDest8));
14809467b48Spatrick ++NumJT8;
14909467b48Spatrick return true;
15073471bf0Spatrick }
15173471bf0Spatrick if (isUInt<16>(Span / 4)) {
15209467b48Spatrick AFI->setJumpTableEntryInfo(JTIdx, 2, MinBlock->getSymbol());
15309467b48Spatrick MI.setDesc(TII->get(AArch64::JumpTableDest16));
15409467b48Spatrick ++NumJT16;
15509467b48Spatrick return true;
15609467b48Spatrick }
15709467b48Spatrick
15809467b48Spatrick ++NumJT32;
15909467b48Spatrick return false;
16009467b48Spatrick }
16109467b48Spatrick
runOnMachineFunction(MachineFunction & MFIn)16209467b48Spatrick bool AArch64CompressJumpTables::runOnMachineFunction(MachineFunction &MFIn) {
16309467b48Spatrick bool Changed = false;
16409467b48Spatrick MF = &MFIn;
16509467b48Spatrick
16609467b48Spatrick const auto &ST = MF->getSubtarget<AArch64Subtarget>();
16709467b48Spatrick TII = ST.getInstrInfo();
16809467b48Spatrick
16909467b48Spatrick if (ST.force32BitJumpTables() && !MF->getFunction().hasMinSize())
17009467b48Spatrick return false;
17109467b48Spatrick
17273471bf0Spatrick if (!scanFunction())
17373471bf0Spatrick return false;
17409467b48Spatrick
17509467b48Spatrick for (MachineBasicBlock &MBB : *MF) {
17609467b48Spatrick int Offset = BlockInfo[MBB.getNumber()];
17709467b48Spatrick for (MachineInstr &MI : MBB) {
17809467b48Spatrick Changed |= compressJumpTable(MI, Offset);
17909467b48Spatrick Offset += TII->getInstSizeInBytes(MI);
18009467b48Spatrick }
18109467b48Spatrick }
18209467b48Spatrick
18309467b48Spatrick return Changed;
18409467b48Spatrick }
18509467b48Spatrick
createAArch64CompressJumpTablesPass()18609467b48Spatrick FunctionPass *llvm::createAArch64CompressJumpTablesPass() {
18709467b48Spatrick return new AArch64CompressJumpTables();
18809467b48Spatrick }
189