LLVM 24.0.0git
AMDGPUPrepareAGPRAlloc.cpp
Go to the documentation of this file.
1//===-- AMDGPUPrepareAGPRAlloc.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//
9// Make simple transformations to relax register constraints for cases which can
10// allocate to AGPRs or VGPRs. Replace materialize of inline immediates into
11// AGPR or VGPR with a pseudo with an AV_* class register constraint. This
12// allows later passes to inflate the register class if necessary. The register
13// allocator does not know to replace instructions to relax constraints.
14//
15//===----------------------------------------------------------------------===//
16
18#include "AMDGPU.h"
19#include "GCNSubtarget.h"
21#include "SIRegisterInfo.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "amdgpu-prepare-agpr-alloc"
28
29namespace {
30
31class AMDGPUPrepareAGPRAllocImpl {
32private:
33 const SIInstrInfo &TII;
35
36 bool isAV64Imm(const MachineOperand &MO) const;
37
38public:
39 AMDGPUPrepareAGPRAllocImpl(const GCNSubtarget &ST, MachineRegisterInfo &MRI)
40 : TII(*ST.getInstrInfo()), MRI(MRI) {}
41 bool run(MachineFunction &MF);
42};
43
44class AMDGPUPrepareAGPRAllocLegacy : public MachineFunctionPass {
45public:
46 static char ID;
47
48 AMDGPUPrepareAGPRAllocLegacy() : MachineFunctionPass(ID) {}
49
50 bool runOnMachineFunction(MachineFunction &MF) override;
51
52 StringRef getPassName() const override { return "AMDGPU Prepare AGPR Alloc"; }
53
54 void getAnalysisUsage(AnalysisUsage &AU) const override {
55 AU.setPreservesAll();
57 }
58};
59} // End anonymous namespace.
60
61INITIALIZE_PASS(AMDGPUPrepareAGPRAllocLegacy, DEBUG_TYPE,
62 "AMDGPU Prepare AGPR Alloc", false, false)
63
64char AMDGPUPrepareAGPRAllocLegacy::ID = 0;
65
66char &llvm::AMDGPUPrepareAGPRAllocLegacyID = AMDGPUPrepareAGPRAllocLegacy::ID;
67
68bool AMDGPUPrepareAGPRAllocLegacy::runOnMachineFunction(MachineFunction &MF) {
69 if (skipFunction(MF.getFunction()))
70 return false;
71
72 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
73 return AMDGPUPrepareAGPRAllocImpl(ST, MF.getRegInfo()).run(MF);
74}
75
79 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
80 AMDGPUPrepareAGPRAllocImpl(ST, MF.getRegInfo()).run(MF);
82}
83
84bool AMDGPUPrepareAGPRAllocImpl::isAV64Imm(const MachineOperand &MO) const {
85 return MO.isImm() && TII.isLegalAV64PseudoImm(MO.getImm());
86}
87
88bool AMDGPUPrepareAGPRAllocImpl::run(MachineFunction &MF) {
89 if (MRI.isReserved(AMDGPU::AGPR0))
90 return false;
91
92 const MCInstrDesc &AVImmPseudo32 = TII.get(AMDGPU::AV_MOV_B32_IMM_PSEUDO);
93 const MCInstrDesc &AVImmPseudo64 = TII.get(AMDGPU::AV_MOV_B64_IMM_PSEUDO);
94
95 bool Changed = false;
96 for (MachineBasicBlock &MBB : MF) {
97 for (MachineInstr &MI : MBB) {
98 if ((MI.getOpcode() == AMDGPU::V_MOV_B32_e32 &&
99 TII.isInlineConstant(MI, 1)) ||
100 (MI.getOpcode() == AMDGPU::V_ACCVGPR_WRITE_B32_e64 &&
101 MI.getOperand(1).isImm())) {
102 MI.setDesc(AVImmPseudo32);
103 Changed = true;
104 continue;
105 }
106
107 // TODO: If only half of the value is rewritable, is it worth splitting it
108 // up?
109 if ((MI.getOpcode() == AMDGPU::V_MOV_B64_e64 ||
110 MI.getOpcode() == AMDGPU::V_MOV_B64_PSEUDO) &&
111 isAV64Imm(MI.getOperand(1))) {
112 MI.setDesc(AVImmPseudo64);
113 Changed = true;
114 continue;
115 }
116 }
117 }
118
119 return Changed;
120}
MachineBasicBlock & MBB
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
Interface definition for SIRegisterInfo.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Describe properties that are true of each instruction in the target description file.
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.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Changed
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
char & AMDGPUPrepareAGPRAllocLegacyID