blob: 56aaf23cae9b817be0bd41dd0446bda575817b95 [file] [log] [blame]
Tom Stellardc0b0c672013-02-06 17:32:29 +00001//===-- AMDGPUIndirectAddressing.cpp - Indirect Adressing Support ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11///
12/// Instructions can use indirect addressing to index the register file as if it
13/// were memory. This pass lowers RegisterLoad and RegisterStore instructions
14/// to either a COPY or a MOV that uses indirect addressing.
15//
16//===----------------------------------------------------------------------===//
17
18#include "AMDGPU.h"
19#include "R600InstrInfo.h"
20#include "R600MachineFunctionInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/Support/Debug.h"
26
27using namespace llvm;
28
29namespace {
30
31class AMDGPUIndirectAddressingPass : public MachineFunctionPass {
32
33private:
34 static char ID;
35 const AMDGPUInstrInfo *TII;
36
37 bool regHasExplicitDef(MachineRegisterInfo &MRI, unsigned Reg) const;
38
39public:
40 AMDGPUIndirectAddressingPass(TargetMachine &tm) :
41 MachineFunctionPass(ID),
42 TII(static_cast<const AMDGPUInstrInfo*>(tm.getInstrInfo()))
43 { }
44
45 virtual bool runOnMachineFunction(MachineFunction &MF);
46
47 const char *getPassName() const { return "R600 Handle indirect addressing"; }
48
49};
50
51} // End anonymous namespace
52
53char AMDGPUIndirectAddressingPass::ID = 0;
54
55FunctionPass *llvm::createAMDGPUIndirectAddressingPass(TargetMachine &tm) {
56 return new AMDGPUIndirectAddressingPass(tm);
57}
58
59bool AMDGPUIndirectAddressingPass::runOnMachineFunction(MachineFunction &MF) {
60 MachineRegisterInfo &MRI = MF.getRegInfo();
61
62 int IndirectBegin = TII->getIndirectIndexBegin(MF);
63 int IndirectEnd = TII->getIndirectIndexEnd(MF);
64
65 if (IndirectBegin == -1) {
66 // No indirect addressing, we can skip this pass
67 assert(IndirectEnd == -1);
68 return false;
69 }
70
71 // The map keeps track of the indirect address that is represented by
72 // each virtual register. The key is the register and the value is the
73 // indirect address it uses.
74 std::map<unsigned, unsigned> RegisterAddressMap;
75
76 // First pass - Lower all of the RegisterStore instructions and track which
77 // registers are live.
78 for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
79 BB != BB_E; ++BB) {
80 // This map keeps track of the current live indirect registers.
81 // The key is the address and the value is the register
82 std::map<unsigned, unsigned> LiveAddressRegisterMap;
83 MachineBasicBlock &MBB = *BB;
84
85 for (MachineBasicBlock::iterator I = MBB.begin(), Next = llvm::next(I);
86 I != MBB.end(); I = Next) {
87 Next = llvm::next(I);
88 MachineInstr &MI = *I;
89
90 if (!TII->isRegisterStore(MI)) {
91 continue;
92 }
93
94 // Lower RegisterStore
95
96 unsigned RegIndex = MI.getOperand(2).getImm();
97 unsigned Channel = MI.getOperand(3).getImm();
98 unsigned Address = TII->calculateIndirectAddress(RegIndex, Channel);
99 const TargetRegisterClass *IndirectStoreRegClass =
100 TII->getIndirectAddrStoreRegClass(MI.getOperand(0).getReg());
101
102 if (MI.getOperand(1).getReg() == AMDGPU::INDIRECT_BASE_ADDR) {
103 // Direct register access.
104 unsigned DstReg = MRI.createVirtualRegister(IndirectStoreRegClass);
105
106 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::COPY), DstReg)
107 .addOperand(MI.getOperand(0));
108
109 RegisterAddressMap[DstReg] = Address;
110 LiveAddressRegisterMap[Address] = DstReg;
111 } else {
112 // Indirect register access.
113 MachineInstrBuilder MOV = TII->buildIndirectWrite(BB, I,
114 MI.getOperand(0).getReg(), // Value
115 Address,
116 MI.getOperand(1).getReg()); // Offset
117 for (int i = IndirectBegin; i <= IndirectEnd; ++i) {
118 unsigned Addr = TII->calculateIndirectAddress(i, Channel);
119 unsigned DstReg = MRI.createVirtualRegister(IndirectStoreRegClass);
120 MOV.addReg(DstReg, RegState::Define | RegState::Implicit);
121 RegisterAddressMap[DstReg] = Addr;
122 LiveAddressRegisterMap[Addr] = DstReg;
123 }
124 }
125 MI.eraseFromParent();
126 }
127
128 // Update the live-ins of the succesor blocks
129 for (MachineBasicBlock::succ_iterator Succ = MBB.succ_begin(),
130 SuccEnd = MBB.succ_end();
131 SuccEnd != Succ; ++Succ) {
132 std::map<unsigned, unsigned>::const_iterator Key, KeyEnd;
133 for (Key = LiveAddressRegisterMap.begin(),
134 KeyEnd = LiveAddressRegisterMap.end(); KeyEnd != Key; ++Key) {
135 (*Succ)->addLiveIn(Key->second);
136 }
137 }
138 }
139
140 // Second pass - Lower the RegisterLoad instructions
141 for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
142 BB != BB_E; ++BB) {
143 // Key is the address and the value is the register
144 std::map<unsigned, unsigned> LiveAddressRegisterMap;
145 MachineBasicBlock &MBB = *BB;
146
147 MachineBasicBlock::livein_iterator LI = MBB.livein_begin();
148 while (LI != MBB.livein_end()) {
149 std::vector<unsigned> PhiRegisters;
150
151 // Make sure this live in is used for indirect addressing
152 if (RegisterAddressMap.find(*LI) == RegisterAddressMap.end()) {
153 ++LI;
154 continue;
155 }
156
157 unsigned Address = RegisterAddressMap[*LI];
158 LiveAddressRegisterMap[Address] = *LI;
159 PhiRegisters.push_back(*LI);
160
161 // Check if there are other live in registers which map to the same
162 // indirect address.
163 for (MachineBasicBlock::livein_iterator LJ = llvm::next(LI),
164 LE = MBB.livein_end();
165 LJ != LE; ++LJ) {
166 unsigned Reg = *LJ;
167 if (RegisterAddressMap.find(Reg) == RegisterAddressMap.end()) {
168 continue;
169 }
170
171 if (RegisterAddressMap[Reg] == Address) {
172 if (!regHasExplicitDef(MRI, Reg)) {
173 continue;
174 }
175 PhiRegisters.push_back(Reg);
176 }
177 }
178
179 if (PhiRegisters.size() == 1) {
180 // We don't need to insert a Phi instruction, so we can just add the
181 // registers to the live list for the block.
182 LiveAddressRegisterMap[Address] = *LI;
183 MBB.removeLiveIn(*LI);
184 } else {
185 // We need to insert a PHI, because we have the same address being
186 // written in multiple predecessor blocks.
187 const TargetRegisterClass *PhiDstClass =
188 TII->getIndirectAddrStoreRegClass(*(PhiRegisters.begin()));
189 unsigned PhiDstReg = MRI.createVirtualRegister(PhiDstClass);
190 MachineInstrBuilder Phi = BuildMI(MBB, MBB.begin(),
191 MBB.findDebugLoc(MBB.begin()),
192 TII->get(AMDGPU::PHI), PhiDstReg);
193
194 for (std::vector<unsigned>::const_iterator RI = PhiRegisters.begin(),
195 RE = PhiRegisters.end();
196 RI != RE; ++RI) {
197 unsigned Reg = *RI;
198 MachineInstr *DefInst = MRI.getVRegDef(Reg);
199 assert(DefInst);
200 MachineBasicBlock *RegBlock = DefInst->getParent();
201 Phi.addReg(Reg);
202 Phi.addMBB(RegBlock);
203 MBB.removeLiveIn(Reg);
204 }
205 RegisterAddressMap[PhiDstReg] = Address;
206 LiveAddressRegisterMap[Address] = PhiDstReg;
207 }
208 LI = MBB.livein_begin();
209 }
210
211 for (MachineBasicBlock::iterator I = MBB.begin(), Next = llvm::next(I);
212 I != MBB.end(); I = Next) {
213 Next = llvm::next(I);
214 MachineInstr &MI = *I;
215
216 if (!TII->isRegisterLoad(MI)) {
217 if (MI.getOpcode() == AMDGPU::PHI) {
218 continue;
219 }
220 // Check for indirect register defs
221 for (unsigned OpIdx = 0, NumOperands = MI.getNumOperands();
222 OpIdx < NumOperands; ++OpIdx) {
223 MachineOperand &MO = MI.getOperand(OpIdx);
224 if (MO.isReg() && MO.isDef() &&
225 RegisterAddressMap.find(MO.getReg()) != RegisterAddressMap.end()) {
226 unsigned Reg = MO.getReg();
227 unsigned LiveAddress = RegisterAddressMap[Reg];
228 // Chain the live-ins
229 if (LiveAddressRegisterMap.find(LiveAddress) !=
230 RegisterAddressMap.end()) {
231 MI.addOperand(MachineOperand::CreateReg(
232 LiveAddressRegisterMap[LiveAddress],
233 false, // isDef
234 true, // isImp
235 true)); // isKill
236 }
237 LiveAddressRegisterMap[LiveAddress] = Reg;
238 }
239 }
240 continue;
241 }
242
243 const TargetRegisterClass *SuperIndirectRegClass =
244 TII->getSuperIndirectRegClass();
245 const TargetRegisterClass *IndirectLoadRegClass =
246 TII->getIndirectAddrLoadRegClass();
247 unsigned IndirectReg = MRI.createVirtualRegister(SuperIndirectRegClass);
248
249 unsigned RegIndex = MI.getOperand(2).getImm();
250 unsigned Channel = MI.getOperand(3).getImm();
251 unsigned Address = TII->calculateIndirectAddress(RegIndex, Channel);
252
253 if (MI.getOperand(1).getReg() == AMDGPU::INDIRECT_BASE_ADDR) {
254 // Direct register access
255 unsigned Reg = LiveAddressRegisterMap[Address];
256 unsigned AddrReg = IndirectLoadRegClass->getRegister(Address);
257
258 if (regHasExplicitDef(MRI, Reg)) {
259 // If the register we are reading from has an explicit def, then that
260 // means it was written via a direct register access (i.e. COPY
261 // or other instruction that doesn't use indirect addressing). In
262 // this case we know where the value has been stored, so we can just
263 // issue a copy.
264 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::COPY),
265 MI.getOperand(0).getReg())
266 .addReg(Reg);
267 } else {
268 // If the register we are reading has an implicit def, then that
269 // means it was written by an indirect register access (i.e. An
270 // instruction that uses indirect addressing.
271 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::COPY),
272 MI.getOperand(0).getReg())
273 .addReg(AddrReg);
274 }
275 } else {
276 // Indirect register access
277
278 // Note on REQ_SEQUENCE instructons: You can't actually use the register
279 // it defines unless you have an instruction that takes the defined
280 // register class as an operand.
281
282 MachineInstrBuilder Sequence = BuildMI(MBB, I, MBB.findDebugLoc(I),
283 TII->get(AMDGPU::REG_SEQUENCE),
284 IndirectReg);
285 for (int i = IndirectBegin; i <= IndirectEnd; ++i) {
286 unsigned Addr = TII->calculateIndirectAddress(i, Channel);
287 if (LiveAddressRegisterMap.find(Addr) == LiveAddressRegisterMap.end()) {
288 continue;
289 }
290 unsigned Reg = LiveAddressRegisterMap[Addr];
291
292 // We only need to use REG_SEQUENCE for explicit defs, since the
293 // register coalescer won't do anything with the implicit defs.
294 MachineInstr *DefInstr = MRI.getVRegDef(Reg);
295 if (!DefInstr->getOperand(0).isReg() ||
296 DefInstr->getOperand(0).getReg() != Reg) {
297 continue;
298 }
299
300 // Insert a REQ_SEQUENCE instruction to force the register allocator
301 // to allocate the virtual register to the correct physical register.
302 Sequence.addReg(LiveAddressRegisterMap[Addr]);
303 Sequence.addImm(TII->getRegisterInfo().getIndirectSubReg(Addr));
304 }
305 MachineInstrBuilder Mov = TII->buildIndirectRead(BB, I,
306 MI.getOperand(0).getReg(), // Value
307 Address,
308 MI.getOperand(1).getReg()); // Offset
309
310
311
312 Mov.addReg(IndirectReg, RegState::Implicit | RegState::Kill);
313
314 }
315 MI.eraseFromParent();
316 }
317 }
318 return false;
319}
320
321bool AMDGPUIndirectAddressingPass::regHasExplicitDef(MachineRegisterInfo &MRI,
322 unsigned Reg) const {
323 MachineInstr *DefInstr = MRI.getVRegDef(Reg);
324 return DefInstr && DefInstr->getOperand(0).isReg() &&
325 DefInstr->getOperand(0).getReg() == Reg;
326}