LLVM 24.0.0git
GCNPreRALongBranchReg.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
18#include "AMDGPU.h"
19#include "GCNSubtarget.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,
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();
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(
74
75 BlockInfo.resize(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(std::next(MachineFunction::iterator((&MF)->begin())),
95 (&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, &AMDGPU::SGPR_64RegClass, MF, ReserveHighestRegister);
117 if (!LongBranchReservedReg)
118 return false;
119
120 // Approximate code size and offsets of each basic block
122 generateBlockInfo(MF, BlockInfo);
123
124 for (const MachineBasicBlock &MBB : MF) {
126 if (Last == MBB.end() || !Last->isUnconditionalBranch())
127 continue;
128 MachineBasicBlock *DestBB = TII->getBranchDestBlock(*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(Last->getOpcode(), BlockDistance)) {
135 MFI->setLongBranchReservedReg(LongBranchReservedReg);
136 return true;
137 }
138 }
139 return false;
140}
141
142PreservedAnalyses
145 GCNPreRALongBranchReg().run(MF);
146 return PreservedAnalyses::all();
147}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
uint64_t uint64_t
MachineBasicBlock & MBB
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
MachineRegisterInfo & MRI
const AMDGPUOptions & getCLOpts() const
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
const SIInstrInfo * getInstrInfo() const override
const SIRegisterInfo * getRegisterInfo() const override
MachineInstrBundleIterator< const MachineInstr > const_iterator
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
LLVM_ABI iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
BasicBlockListType::iterator iterator
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Representation of each machine instruction.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void setLongBranchReservedReg(Register Reg)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
iterator end() const
Definition BasicBlock.h:89
LLVM_ABI iterator begin() const
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
char & GCNPreRALongBranchRegID
BasicBlockInfo - Information about the offset and size of a single basic block.