1 //===-- R600EmitClauseMarkers.cpp - Emit CF_ALU ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
18 #include "AMDGPUSubtarget.h"
19 #include "R600Defines.h"
20 #include "R600InstrInfo.h"
21 #include "R600RegisterInfo.h"
22 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineOperand.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/ErrorHandling.h"
42 void initializeR600EmitClauseMarkersPass(PassRegistry
&);
44 } // end namespace llvm
48 class R600EmitClauseMarkers
: public MachineFunctionPass
{
50 const R600InstrInfo
*TII
= nullptr;
53 unsigned OccupiedDwords(MachineInstr
&MI
) const {
54 switch (MI
.getOpcode()) {
55 case R600::INTERP_PAIR_XY
:
56 case R600::INTERP_PAIR_ZW
:
57 case R600::INTERP_VEC_LOAD
:
66 // These will be expanded to two ALU instructions in the
67 // ExpandSpecialInstructions pass.
68 if (TII
->isLDSRetInstr(MI
.getOpcode()))
71 if (TII
->isVector(MI
) || TII
->isCubeOp(MI
.getOpcode()) ||
72 TII
->isReductionOp(MI
.getOpcode()))
75 unsigned NumLiteral
= 0;
76 for (MachineInstr::mop_iterator It
= MI
.operands_begin(),
77 E
= MI
.operands_end();
79 MachineOperand
&MO
= *It
;
80 if (MO
.isReg() && MO
.getReg() == R600::ALU_LITERAL_X
)
83 return 1 + NumLiteral
;
86 bool isALU(const MachineInstr
&MI
) const {
87 if (TII
->isALUInstr(MI
.getOpcode()))
89 if (TII
->isVector(MI
) || TII
->isCubeOp(MI
.getOpcode()))
91 switch (MI
.getOpcode()) {
93 case R600::INTERP_PAIR_XY
:
94 case R600::INTERP_PAIR_ZW
:
95 case R600::INTERP_VEC_LOAD
:
104 bool IsTrivialInst(MachineInstr
&MI
) const {
105 switch (MI
.getOpcode()) {
108 case R600::IMPLICIT_DEF
:
115 std::pair
<unsigned, unsigned> getAccessedBankLine(unsigned Sel
) const {
116 // Sel is (512 + (kc_bank << 12) + ConstIndex) << 2
117 // (See also R600ISelLowering.cpp)
118 // ConstIndex value is in [0, 4095];
119 return std::pair
<unsigned, unsigned>(
120 ((Sel
>> 2) - 512) >> 12, // KC_BANK
121 // Line Number of ConstIndex
122 // A line contains 16 constant registers however KCX bank can lock
123 // two line at the same time ; thus we want to get an even line number.
124 // Line number can be retrieved with (>>4), using (>>5) <<1 generates
126 ((((Sel
>> 2) - 512) & 4095) >> 5) << 1);
130 SubstituteKCacheBank(MachineInstr
&MI
,
131 std::vector
<std::pair
<unsigned, unsigned>> &CachedConsts
,
132 bool UpdateInstr
= true) const {
133 std::vector
<std::pair
<unsigned, unsigned>> UsedKCache
;
135 if (!TII
->isALUInstr(MI
.getOpcode()) && MI
.getOpcode() != R600::DOT_4
)
138 const SmallVectorImpl
<std::pair
<MachineOperand
*, int64_t>> &Consts
=
141 (TII
->isALUInstr(MI
.getOpcode()) || MI
.getOpcode() == R600::DOT_4
) &&
142 "Can't assign Const");
143 for (unsigned i
= 0, n
= Consts
.size(); i
< n
; ++i
) {
144 if (Consts
[i
].first
->getReg() != R600::ALU_CONST
)
146 unsigned Sel
= Consts
[i
].second
;
147 unsigned Chan
= Sel
& 3, Index
= ((Sel
>> 2) - 512) & 31;
148 unsigned KCacheIndex
= Index
* 4 + Chan
;
149 const std::pair
<unsigned, unsigned> &BankLine
= getAccessedBankLine(Sel
);
150 if (CachedConsts
.empty()) {
151 CachedConsts
.push_back(BankLine
);
152 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(0, KCacheIndex
));
155 if (CachedConsts
[0] == BankLine
) {
156 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(0, KCacheIndex
));
159 if (CachedConsts
.size() == 1) {
160 CachedConsts
.push_back(BankLine
);
161 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(1, KCacheIndex
));
164 if (CachedConsts
[1] == BankLine
) {
165 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(1, KCacheIndex
));
174 for (unsigned i
= 0, j
= 0, n
= Consts
.size(); i
< n
; ++i
) {
175 if (Consts
[i
].first
->getReg() != R600::ALU_CONST
)
177 switch(UsedKCache
[j
].first
) {
179 Consts
[i
].first
->setReg(
180 R600::R600_KC0RegClass
.getRegister(UsedKCache
[j
].second
));
183 Consts
[i
].first
->setReg(
184 R600::R600_KC1RegClass
.getRegister(UsedKCache
[j
].second
));
187 llvm_unreachable("Wrong Cache Line");
194 bool canClauseLocalKillFitInClause(
195 unsigned AluInstCount
,
196 std::vector
<std::pair
<unsigned, unsigned>> KCacheBanks
,
197 MachineBasicBlock::iterator Def
,
198 MachineBasicBlock::iterator BBEnd
) {
199 const R600RegisterInfo
&TRI
= TII
->getRegisterInfo();
200 //TODO: change this to defs?
201 for (MachineInstr::const_mop_iterator
202 MOI
= Def
->operands_begin(),
203 MOE
= Def
->operands_end(); MOI
!= MOE
; ++MOI
) {
204 if (!MOI
->isReg() || !MOI
->isDef() ||
205 TRI
.isPhysRegLiveAcrossClauses(MOI
->getReg()))
208 // Def defines a clause local register, so check that its use will fit
210 unsigned LastUseCount
= 0;
211 for (MachineBasicBlock::iterator UseI
= Def
; UseI
!= BBEnd
; ++UseI
) {
212 AluInstCount
+= OccupiedDwords(*UseI
);
213 // Make sure we won't need to end the clause due to KCache limitations.
214 if (!SubstituteKCacheBank(*UseI
, KCacheBanks
, false))
217 // We have reached the maximum instruction limit before finding the
218 // use that kills this register, so we cannot use this def in the
220 if (AluInstCount
>= TII
->getMaxAlusPerClause())
223 // TODO: Is this true? kill flag appears to work OK below
224 // Register kill flags have been cleared by the time we get to this
225 // pass, but it is safe to assume that all uses of this register
226 // occur in the same basic block as its definition, because
227 // it is illegal for the scheduler to schedule them in
229 if (UseI
->readsRegister(MOI
->getReg()))
230 LastUseCount
= AluInstCount
;
232 // Exit early if the current use kills the register
233 if (UseI
!= Def
&& UseI
->killsRegister(MOI
->getReg()))
237 return LastUseCount
<= TII
->getMaxAlusPerClause();
238 llvm_unreachable("Clause local register live at end of clause.");
243 MachineBasicBlock::iterator
244 MakeALUClause(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator I
) {
245 MachineBasicBlock::iterator ClauseHead
= I
;
246 std::vector
<std::pair
<unsigned, unsigned>> KCacheBanks
;
247 bool PushBeforeModifier
= false;
248 unsigned AluInstCount
= 0;
249 for (MachineBasicBlock::iterator E
= MBB
.end(); I
!= E
; ++I
) {
250 if (IsTrivialInst(*I
))
254 if (AluInstCount
> TII
->getMaxAlusPerClause())
256 if (I
->getOpcode() == R600::PRED_X
) {
257 // We put PRED_X in its own clause to ensure that ifcvt won't create
258 // clauses with more than 128 insts.
259 // IfCvt is indeed checking that "then" and "else" branches of an if
260 // statement have less than ~60 insts thus converted clauses can't be
261 // bigger than ~121 insts (predicate setter needs to be in the same
262 // clause as predicated alus).
263 if (AluInstCount
> 0)
265 if (TII
->getFlagOp(*I
).getImm() & MO_FLAG_PUSH
)
266 PushBeforeModifier
= true;
270 // XXX: GROUP_BARRIER instructions cannot be in the same ALU clause as:
272 // * KILL or INTERP instructions
273 // * Any instruction that sets UPDATE_EXEC_MASK or UPDATE_PRED bits
274 // * Uses waterfalling (i.e. INDEX_MODE = AR.X)
276 // XXX: These checks have not been implemented yet.
277 if (TII
->mustBeLastInClause(I
->getOpcode())) {
282 // If this instruction defines a clause local register, make sure
283 // its use can fit in this clause.
284 if (!canClauseLocalKillFitInClause(AluInstCount
, KCacheBanks
, I
, E
))
287 if (!SubstituteKCacheBank(*I
, KCacheBanks
))
289 AluInstCount
+= OccupiedDwords(*I
);
291 unsigned Opcode
= PushBeforeModifier
?
292 R600::CF_ALU_PUSH_BEFORE
: R600::CF_ALU
;
293 BuildMI(MBB
, ClauseHead
, MBB
.findDebugLoc(ClauseHead
), TII
->get(Opcode
))
294 // We don't use the ADDR field until R600ControlFlowFinalizer pass, where
295 // it is safe to assume it is 0. However if we always put 0 here, the ifcvt
296 // pass may assume that identical ALU clause starter at the beginning of a
297 // true and false branch can be factorized which is not the case.
298 .addImm(Address
++) // ADDR
299 .addImm(KCacheBanks
.empty()?0:KCacheBanks
[0].first
) // KB0
300 .addImm((KCacheBanks
.size() < 2)?0:KCacheBanks
[1].first
) // KB1
301 .addImm(KCacheBanks
.empty()?0:2) // KM0
302 .addImm((KCacheBanks
.size() < 2)?0:2) // KM1
303 .addImm(KCacheBanks
.empty()?0:KCacheBanks
[0].second
) // KLINE0
304 .addImm((KCacheBanks
.size() < 2)?0:KCacheBanks
[1].second
) // KLINE1
305 .addImm(AluInstCount
) // COUNT
306 .addImm(1); // Enabled
313 R600EmitClauseMarkers() : MachineFunctionPass(ID
) {
314 initializeR600EmitClauseMarkersPass(*PassRegistry::getPassRegistry());
317 bool runOnMachineFunction(MachineFunction
&MF
) override
{
318 const R600Subtarget
&ST
= MF
.getSubtarget
<R600Subtarget
>();
319 TII
= ST
.getInstrInfo();
321 for (MachineFunction::iterator BB
= MF
.begin(), BB_E
= MF
.end();
323 MachineBasicBlock
&MBB
= *BB
;
324 MachineBasicBlock::iterator I
= MBB
.begin();
325 if (I
!= MBB
.end() && I
->getOpcode() == R600::CF_ALU
)
326 continue; // BB was already parsed
327 for (MachineBasicBlock::iterator E
= MBB
.end(); I
!= E
;) {
329 auto next
= MakeALUClause(MBB
, I
);
339 StringRef
getPassName() const override
{
340 return "R600 Emit Clause Markers Pass";
344 char R600EmitClauseMarkers::ID
= 0;
346 } // end anonymous namespace
348 INITIALIZE_PASS_BEGIN(R600EmitClauseMarkers
, "emitclausemarkers",
349 "R600 Emit Clause Markters", false, false)
350 INITIALIZE_PASS_END(R600EmitClauseMarkers
, "emitclausemarkers",
351 "R600 Emit Clause Markters", false, false)
353 FunctionPass
*llvm::createR600EmitClauseMarkers() {
354 return new R600EmitClauseMarkers();