1//===-- GCNPreRALongBranchReg.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// \file
9// \brief Pass to estimate pre RA branch size and reserve a pair of SGPRs if
10// there is a long branch. Branch size at this point is difficult to track since
11// we have no idea what spills will be inserted later on. We just assume 8 bytes
12// per instruction to compute approximations without computing the actual
13// instruction size to see if we're in the neighborhood of the maximum branch
14// distrance threshold tuning of what is considered "long" is handled through
15// amdgpu-long-branch-factor cl argument which sets LongBranchFactor.
16//===----------------------------------------------------------------------===//
17#include "GCNPreRALongBranchReg.h"
18#include "AMDGPU.h"
19#include "GCNSubtarget.h"
20#include "SIMachineFunctionInfo.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "amdgpu-pre-ra-long-branch-reg"
26
27namespace {
28
29class GCNPreRALongBranchReg {
30
31 struct BasicBlockInfo {
32 // Offset - Distance from the beginning of the function to the beginning
33 // of this basic block.
34 uint64_t Offset = 0;
35 // Size - Size of the basic block in bytes
36 uint64_t Size = 0;
37 };
38 void generateBlockInfo(MachineFunction &MF,
39 SmallVectorImpl<BasicBlockInfo> &BlockInfo);
40
41public:
42 GCNPreRALongBranchReg() = default;
43 bool run(MachineFunction &MF);
44};
45
46class GCNPreRALongBranchRegLegacy : public MachineFunctionPass {
47public:
48 static char ID;
49 GCNPreRALongBranchRegLegacy() : MachineFunctionPass(ID) {}
50
51 bool runOnMachineFunction(MachineFunction &MF) override {
52 return GCNPreRALongBranchReg().run(MF);
53 }
54
55 StringRef getPassName() const override {
56 return "AMDGPU Pre-RA Long Branch Reg";
57 }
58
59 void getAnalysisUsage(AnalysisUsage &AU) const override {
60 AU.setPreservesAll();
61 MachineFunctionPass::getAnalysisUsage(AU);
62 }
63};
64} // End anonymous namespace.
65
66char GCNPreRALongBranchRegLegacy::ID = 0;
67
68INITIALIZE_PASS(GCNPreRALongBranchRegLegacy, DEBUG_TYPE,
69 "AMDGPU Pre-RA Long Branch Reg", false, false)
70
71char &llvm::GCNPreRALongBranchRegID = GCNPreRALongBranchRegLegacy::ID;
72void GCNPreRALongBranchReg::generateBlockInfo(
73 MachineFunction &MF, SmallVectorImpl<BasicBlockInfo> &BlockInfo) {
74
75 BlockInfo.resize(N: MF.getNumBlockIDs());
76
77 // Approximate the size of all basic blocks by just
78 // assuming 8 bytes per instruction
79 for (const MachineBasicBlock &MBB : MF) {
80 uint64_t NumInstr = 0;
81 // Loop through the basic block and add up all non-debug
82 // non-meta instructions
83 for (const MachineInstr &MI : MBB) {
84 // isMetaInstruction is a superset of isDebugIstr
85 if (MI.isMetaInstruction())
86 continue;
87 NumInstr += 1;
88 }
89 // Approximate size as just 8 bytes per instruction
90 BlockInfo[MBB.getNumber()].Size = 8 * NumInstr;
91 }
92 uint64_t PrevNum = (&MF)->begin()->getNumber();
93 for (auto &MBB :
94 make_range(x: std::next(x: MachineFunction::iterator((&MF)->begin())),
95 y: (&MF)->end())) {
96 uint64_t Num = MBB.getNumber();
97 // Compute the offset immediately following this block.
98 BlockInfo[Num].Offset = BlockInfo[PrevNum].Offset + BlockInfo[PrevNum].Size;
99 PrevNum = Num;
100 }
101}
102
103bool GCNPreRALongBranchReg::run(MachineFunction &MF) {
104 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
105 const SIInstrInfo *TII = STM.getInstrInfo();
106 const SIRegisterInfo *TRI = STM.getRegisterInfo();
107 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
108 MachineRegisterInfo &MRI = MF.getRegInfo();
109
110 // For now, reserve highest available SGPR pair. After RA,
111 // shift down to a lower unused pair of SGPRs
112 // If all registers are used, then findUnusedRegister will return
113 // AMDGPU::NoRegister.
114 constexpr bool ReserveHighestRegister = true;
115 Register LongBranchReservedReg = TRI->findUnusedRegister(
116 MRI, RC: &AMDGPU::SGPR_64RegClass, MF, ReserveHighestVGPR: ReserveHighestRegister);
117 if (!LongBranchReservedReg)
118 return false;
119
120 // Approximate code size and offsets of each basic block
121 SmallVector<BasicBlockInfo, 16> BlockInfo;
122 generateBlockInfo(MF, BlockInfo);
123
124 for (const MachineBasicBlock &MBB : MF) {
125 MachineBasicBlock::const_iterator Last = MBB.getLastNonDebugInstr();
126 if (Last == MBB.end() || !Last->isUnconditionalBranch())
127 continue;
128 MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI: *Last);
129 uint64_t BlockDistance =
130 static_cast<uint64_t>(STM.getCLOpts().long_branch_factor *
131 BlockInfo[DestBB->getNumber()].Offset);
132 // If the distance falls outside the threshold assume it is a long branch
133 // and we need to reserve the registers
134 if (!TII->isBranchOffsetInRange(BranchOpc: Last->getOpcode(), BrOffset: BlockDistance)) {
135 MFI->setLongBranchReservedReg(LongBranchReservedReg);
136 return true;
137 }
138 }
139 return false;
140}
141
142PreservedAnalyses
143GCNPreRALongBranchRegPass::run(MachineFunction &MF,
144 MachineFunctionAnalysisManager &MFAM) {
145 GCNPreRALongBranchReg().run(MF);
146 return PreservedAnalyses::all();
147}
148