blob: 928c0e3ba6df79970ec600240d491fbd885edf60 [file] [log] [blame]
Vincent Lejeunef43bc572013-04-01 21:47:42 +00001//===-- R600EmitClauseMarkers.cpp - Emit CF_ALU ---------------------------===//
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/// Add CF_ALU. R600 Alu instructions are grouped in clause which can hold
12/// 128 Alu instructions ; these instructions can access up to 4 prefetched
13/// 4 lines of 16 registers from constant buffers. Such ALU clauses are
14/// initiated by CF_ALU instructions.
15//===----------------------------------------------------------------------===//
16
17#include "AMDGPU.h"
18#include "R600Defines.h"
19#include "R600InstrInfo.h"
20#include "R600MachineFunctionInfo.h"
21#include "R600RegisterInfo.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25
Benjamin Kramerd78bb462013-05-23 17:10:37 +000026using namespace llvm;
27
28namespace {
Vincent Lejeunef43bc572013-04-01 21:47:42 +000029
30class R600EmitClauseMarkersPass : public MachineFunctionPass {
31
32private:
33 static char ID;
34 const R600InstrInfo *TII;
Vincent Lejeunece499742013-07-09 15:03:33 +000035 int Address;
Vincent Lejeunef43bc572013-04-01 21:47:42 +000036
37 unsigned OccupiedDwords(MachineInstr *MI) const {
38 switch (MI->getOpcode()) {
39 case AMDGPU::INTERP_PAIR_XY:
40 case AMDGPU::INTERP_PAIR_ZW:
41 case AMDGPU::INTERP_VEC_LOAD:
Vincent Lejeune519f21e2013-05-17 16:50:32 +000042 case AMDGPU::DOT_4:
Vincent Lejeunef43bc572013-04-01 21:47:42 +000043 return 4;
44 case AMDGPU::KILL:
45 return 0;
46 default:
47 break;
48 }
49
50 if(TII->isVector(*MI) ||
51 TII->isCubeOp(MI->getOpcode()) ||
52 TII->isReductionOp(MI->getOpcode()))
53 return 4;
54
55 unsigned NumLiteral = 0;
56 for (MachineInstr::mop_iterator It = MI->operands_begin(),
57 E = MI->operands_end(); It != E; ++It) {
58 MachineOperand &MO = *It;
59 if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X)
60 ++NumLiteral;
61 }
62 return 1 + NumLiteral;
63 }
64
65 bool isALU(const MachineInstr *MI) const {
Vincent Lejeunef43bc572013-04-01 21:47:42 +000066 if (TII->isALUInstr(MI->getOpcode()))
67 return true;
68 if (TII->isVector(*MI) || TII->isCubeOp(MI->getOpcode()))
69 return true;
70 switch (MI->getOpcode()) {
71 case AMDGPU::PRED_X:
72 case AMDGPU::INTERP_PAIR_XY:
73 case AMDGPU::INTERP_PAIR_ZW:
74 case AMDGPU::INTERP_VEC_LOAD:
75 case AMDGPU::COPY:
Vincent Lejeune519f21e2013-05-17 16:50:32 +000076 case AMDGPU::DOT_4:
Vincent Lejeunef43bc572013-04-01 21:47:42 +000077 return true;
78 default:
79 return false;
80 }
81 }
82
83 bool IsTrivialInst(MachineInstr *MI) const {
84 switch (MI->getOpcode()) {
85 case AMDGPU::KILL:
86 case AMDGPU::RETURN:
Tom Stellarded0ceec2013-10-10 17:11:12 +000087 case AMDGPU::IMPLICIT_DEF:
Vincent Lejeunef43bc572013-04-01 21:47:42 +000088 return true;
89 default:
90 return false;
91 }
92 }
93
Vincent Lejeunef43bc572013-04-01 21:47:42 +000094 std::pair<unsigned, unsigned> getAccessedBankLine(unsigned Sel) const {
95 // Sel is (512 + (kc_bank << 12) + ConstIndex) << 2
96 // (See also R600ISelLowering.cpp)
97 // ConstIndex value is in [0, 4095];
98 return std::pair<unsigned, unsigned>(
99 ((Sel >> 2) - 512) >> 12, // KC_BANK
100 // Line Number of ConstIndex
101 // A line contains 16 constant registers however KCX bank can lock
102 // two line at the same time ; thus we want to get an even line number.
103 // Line number can be retrieved with (>>4), using (>>5) <<1 generates
104 // an even number.
105 ((((Sel >> 2) - 512) & 4095) >> 5) << 1);
106 }
107
108 bool SubstituteKCacheBank(MachineInstr *MI,
109 std::vector<std::pair<unsigned, unsigned> > &CachedConsts) const {
110 std::vector<std::pair<unsigned, unsigned> > UsedKCache;
Craig Topperb94011f2013-07-14 04:42:23 +0000111 const SmallVectorImpl<std::pair<MachineOperand *, int64_t> > &Consts =
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000112 TII->getSrcs(MI);
Vincent Lejeunec6896792013-06-04 23:17:15 +0000113 assert((TII->isALUInstr(MI->getOpcode()) ||
114 MI->getOpcode() == AMDGPU::DOT_4) && "Can't assign Const");
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000115 for (unsigned i = 0, n = Consts.size(); i < n; ++i) {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000116 if (Consts[i].first->getReg() != AMDGPU::ALU_CONST)
117 continue;
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000118 unsigned Sel = Consts[i].second;
119 unsigned Chan = Sel & 3, Index = ((Sel >> 2) - 512) & 31;
120 unsigned KCacheIndex = Index * 4 + Chan;
121 const std::pair<unsigned, unsigned> &BankLine = getAccessedBankLine(Sel);
122 if (CachedConsts.empty()) {
123 CachedConsts.push_back(BankLine);
124 UsedKCache.push_back(std::pair<unsigned, unsigned>(0, KCacheIndex));
125 continue;
126 }
127 if (CachedConsts[0] == BankLine) {
128 UsedKCache.push_back(std::pair<unsigned, unsigned>(0, KCacheIndex));
129 continue;
130 }
131 if (CachedConsts.size() == 1) {
132 CachedConsts.push_back(BankLine);
133 UsedKCache.push_back(std::pair<unsigned, unsigned>(1, KCacheIndex));
134 continue;
135 }
136 if (CachedConsts[1] == BankLine) {
137 UsedKCache.push_back(std::pair<unsigned, unsigned>(1, KCacheIndex));
138 continue;
139 }
140 return false;
141 }
142
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000143 for (unsigned i = 0, j = 0, n = Consts.size(); i < n; ++i) {
144 if (Consts[i].first->getReg() != AMDGPU::ALU_CONST)
145 continue;
146 switch(UsedKCache[j].first) {
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000147 case 0:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000148 Consts[i].first->setReg(
149 AMDGPU::R600_KC0RegClass.getRegister(UsedKCache[j].second));
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000150 break;
151 case 1:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000152 Consts[i].first->setReg(
153 AMDGPU::R600_KC1RegClass.getRegister(UsedKCache[j].second));
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000154 break;
155 default:
156 llvm_unreachable("Wrong Cache Line");
157 }
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000158 j++;
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000159 }
160 return true;
161 }
162
163 MachineBasicBlock::iterator
Vincent Lejeunece499742013-07-09 15:03:33 +0000164 MakeALUClause(MachineBasicBlock &MBB, MachineBasicBlock::iterator I) {
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000165 MachineBasicBlock::iterator ClauseHead = I;
166 std::vector<std::pair<unsigned, unsigned> > KCacheBanks;
167 bool PushBeforeModifier = false;
168 unsigned AluInstCount = 0;
169 for (MachineBasicBlock::iterator E = MBB.end(); I != E; ++I) {
170 if (IsTrivialInst(I))
171 continue;
172 if (!isALU(I))
173 break;
Vincent Lejeunec3d3f9b2013-04-03 18:24:47 +0000174 if (AluInstCount > TII->getMaxAlusPerClause())
175 break;
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000176 if (I->getOpcode() == AMDGPU::PRED_X) {
Vincent Lejeune0b342d62013-10-01 19:32:49 +0000177 // We put PRED_X in its own clause to ensure that ifcvt won't create
178 // clauses with more than 128 insts.
179 // IfCvt is indeed checking that "then" and "else" branches of an if
180 // statement have less than ~60 insts thus converted clauses can't be
181 // bigger than ~121 insts (predicate setter needs to be in the same
182 // clause as predicated alus).
183 if (AluInstCount > 0)
184 break;
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000185 if (TII->getFlagOp(I).getImm() & MO_FLAG_PUSH)
186 PushBeforeModifier = true;
187 AluInstCount ++;
188 continue;
189 }
Tom Stellardce540332013-06-28 15:46:59 +0000190 // XXX: GROUP_BARRIER instructions cannot be in the same ALU clause as:
191 //
192 // * KILL or INTERP instructions
193 // * Any instruction that sets UPDATE_EXEC_MASK or UPDATE_PRED bits
194 // * Uses waterfalling (i.e. INDEX_MODE = AR.X)
195 //
196 // XXX: These checks have not been implemented yet.
197 if (TII->mustBeLastInClause(I->getOpcode())) {
Vincent Lejeune99312982013-04-03 16:24:04 +0000198 I++;
199 break;
200 }
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000201 if (TII->isALUInstr(I->getOpcode()) &&
202 !SubstituteKCacheBank(I, KCacheBanks))
203 break;
Vincent Lejeunec6896792013-06-04 23:17:15 +0000204 if (I->getOpcode() == AMDGPU::DOT_4 &&
205 !SubstituteKCacheBank(I, KCacheBanks))
206 break;
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000207 AluInstCount += OccupiedDwords(I);
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000208 }
209 unsigned Opcode = PushBeforeModifier ?
210 AMDGPU::CF_ALU_PUSH_BEFORE : AMDGPU::CF_ALU;
211 BuildMI(MBB, ClauseHead, MBB.findDebugLoc(ClauseHead), TII->get(Opcode))
Vincent Lejeunece499742013-07-09 15:03:33 +0000212 // We don't use the ADDR field until R600ControlFlowFinalizer pass, where
213 // it is safe to assume it is 0. However if we always put 0 here, the ifcvt
214 // pass may assume that identical ALU clause starter at the beginning of a
215 // true and false branch can be factorized which is not the case.
216 .addImm(Address++) // ADDR
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000217 .addImm(KCacheBanks.empty()?0:KCacheBanks[0].first) // KB0
218 .addImm((KCacheBanks.size() < 2)?0:KCacheBanks[1].first) // KB1
219 .addImm(KCacheBanks.empty()?0:2) // KM0
220 .addImm((KCacheBanks.size() < 2)?0:2) // KM1
221 .addImm(KCacheBanks.empty()?0:KCacheBanks[0].second) // KLINE0
222 .addImm((KCacheBanks.size() < 2)?0:KCacheBanks[1].second) // KLINE1
Vincent Lejeunece499742013-07-09 15:03:33 +0000223 .addImm(AluInstCount) // COUNT
224 .addImm(1); // Enabled
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000225 return I;
226 }
227
228public:
229 R600EmitClauseMarkersPass(TargetMachine &tm) : MachineFunctionPass(ID),
Vincent Lejeunece499742013-07-09 15:03:33 +0000230 TII(0), Address(0) { }
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000231
232 virtual bool runOnMachineFunction(MachineFunction &MF) {
Bill Wendling37e9adb2013-06-07 20:28:55 +0000233 TII = static_cast<const R600InstrInfo *>(MF.getTarget().getInstrInfo());
234
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000235 for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
236 BB != BB_E; ++BB) {
237 MachineBasicBlock &MBB = *BB;
238 MachineBasicBlock::iterator I = MBB.begin();
239 if (I->getOpcode() == AMDGPU::CF_ALU)
240 continue; // BB was already parsed
241 for (MachineBasicBlock::iterator E = MBB.end(); I != E;) {
242 if (isALU(I))
243 I = MakeALUClause(MBB, I);
244 else
245 ++I;
246 }
247 }
248 return false;
249 }
250
251 const char *getPassName() const {
252 return "R600 Emit Clause Markers Pass";
253 }
254};
255
256char R600EmitClauseMarkersPass::ID = 0;
257
Benjamin Kramerd78bb462013-05-23 17:10:37 +0000258} // end anonymous namespace
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000259
260
261llvm::FunctionPass *llvm::createR600EmitClauseMarkers(TargetMachine &TM) {
262 return new R600EmitClauseMarkersPass(TM);
263}
264