LLVM 19.0.0git
GCNDPPCombine.cpp
Go to the documentation of this file.
1//=======- GCNDPPCombine.cpp - optimization for DPP instructions ---==========//
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// The pass combines V_MOV_B32_dpp instruction with its VALU uses as a DPP src0
9// operand. If any of the use instruction cannot be combined with the mov the
10// whole sequence is reverted.
11//
12// $old = ...
13// $dpp_value = V_MOV_B32_dpp $old, $vgpr_to_be_read_from_other_lane,
14// dpp_controls..., $row_mask, $bank_mask, $bound_ctrl
15// $res = VALU $dpp_value [, src1]
16//
17// to
18//
19// $res = VALU_DPP $combined_old, $vgpr_to_be_read_from_other_lane, [src1,]
20// dpp_controls..., $row_mask, $bank_mask, $combined_bound_ctrl
21//
22// Combining rules :
23//
24// if $row_mask and $bank_mask are fully enabled (0xF) and
25// $bound_ctrl==DPP_BOUND_ZERO or $old==0
26// -> $combined_old = undef,
27// $combined_bound_ctrl = DPP_BOUND_ZERO
28//
29// if the VALU op is binary and
30// $bound_ctrl==DPP_BOUND_OFF and
31// $old==identity value (immediate) for the VALU op
32// -> $combined_old = src1,
33// $combined_bound_ctrl = DPP_BOUND_OFF
34//
35// Otherwise cancel.
36//
37// The mov_dpp instruction should reside in the same BB as all its uses
38//===----------------------------------------------------------------------===//
39
40#include "AMDGPU.h"
41#include "GCNSubtarget.h"
43#include "llvm/ADT/Statistic.h"
45
46using namespace llvm;
47
48#define DEBUG_TYPE "gcn-dpp-combine"
49
50STATISTIC(NumDPPMovsCombined, "Number of DPP moves combined.");
51
52namespace {
53
54class GCNDPPCombine : public MachineFunctionPass {
56 const SIInstrInfo *TII;
57 const GCNSubtarget *ST;
58
60
61 MachineOperand *getOldOpndValue(MachineOperand &OldOpnd) const;
62
63 MachineInstr *createDPPInst(MachineInstr &OrigMI, MachineInstr &MovMI,
64 RegSubRegPair CombOldVGPR,
65 MachineOperand *OldOpnd, bool CombBCZ,
66 bool IsShrinkable) const;
67
68 MachineInstr *createDPPInst(MachineInstr &OrigMI, MachineInstr &MovMI,
69 RegSubRegPair CombOldVGPR, bool CombBCZ,
70 bool IsShrinkable) const;
71
72 bool hasNoImmOrEqual(MachineInstr &MI,
73 unsigned OpndName,
74 int64_t Value,
75 int64_t Mask = -1) const;
76
77 bool combineDPPMov(MachineInstr &MI) const;
78
79public:
80 static char ID;
81
82 GCNDPPCombine() : MachineFunctionPass(ID) {
84 }
85
86 bool runOnMachineFunction(MachineFunction &MF) override;
87
88 StringRef getPassName() const override { return "GCN DPP Combine"; }
89
90 void getAnalysisUsage(AnalysisUsage &AU) const override {
91 AU.setPreservesCFG();
93 }
94
97 .set(MachineFunctionProperties::Property::IsSSA);
98 }
99
100private:
101 int getDPPOp(unsigned Op, bool IsShrinkable) const;
102 bool isShrinkable(MachineInstr &MI) const;
103};
104
105} // end anonymous namespace
106
107INITIALIZE_PASS(GCNDPPCombine, DEBUG_TYPE, "GCN DPP Combine", false, false)
108
109char GCNDPPCombine::ID = 0;
110
111char &llvm::GCNDPPCombineID = GCNDPPCombine::ID;
112
114 return new GCNDPPCombine();
115}
116
117bool GCNDPPCombine::isShrinkable(MachineInstr &MI) const {
118 unsigned Op = MI.getOpcode();
119 if (!TII->isVOP3(Op)) {
120 return false;
121 }
122 if (!TII->hasVALU32BitEncoding(Op)) {
123 LLVM_DEBUG(dbgs() << " Inst hasn't e32 equivalent\n");
124 return false;
125 }
126 // Do not shrink True16 instructions pre-RA to avoid the restriction in
127 // register allocation from only being able to use 128 VGPRs
129 return false;
130 if (const auto *SDst = TII->getNamedOperand(MI, AMDGPU::OpName::sdst)) {
131 // Give up if there are any uses of the sdst in carry-out or VOPC.
132 // The shrunken form of the instruction would write it to vcc instead of to
133 // a virtual register. If we rewrote the uses the shrinking would be
134 // possible.
135 if (!MRI->use_nodbg_empty(SDst->getReg()))
136 return false;
137 }
138 // check if other than abs|neg modifiers are set (opsel for example)
139 const int64_t Mask = ~(SISrcMods::ABS | SISrcMods::NEG);
140 if (!hasNoImmOrEqual(MI, AMDGPU::OpName::src0_modifiers, 0, Mask) ||
141 !hasNoImmOrEqual(MI, AMDGPU::OpName::src1_modifiers, 0, Mask) ||
142 !hasNoImmOrEqual(MI, AMDGPU::OpName::clamp, 0) ||
143 !hasNoImmOrEqual(MI, AMDGPU::OpName::omod, 0)) {
144 LLVM_DEBUG(dbgs() << " Inst has non-default modifiers\n");
145 return false;
146 }
147 return true;
148}
149
150int GCNDPPCombine::getDPPOp(unsigned Op, bool IsShrinkable) const {
151 int DPP32 = AMDGPU::getDPPOp32(Op);
152 if (IsShrinkable) {
153 assert(DPP32 == -1);
154 int E32 = AMDGPU::getVOPe32(Op);
155 DPP32 = (E32 == -1) ? -1 : AMDGPU::getDPPOp32(E32);
156 }
157 if (DPP32 != -1 && TII->pseudoToMCOpcode(DPP32) != -1)
158 return DPP32;
159 int DPP64 = -1;
160 if (ST->hasVOP3DPP())
161 DPP64 = AMDGPU::getDPPOp64(Op);
162 if (DPP64 != -1 && TII->pseudoToMCOpcode(DPP64) != -1)
163 return DPP64;
164 return -1;
165}
166
167// tracks the register operand definition and returns:
168// 1. immediate operand used to initialize the register if found
169// 2. nullptr if the register operand is undef
170// 3. the operand itself otherwise
171MachineOperand *GCNDPPCombine::getOldOpndValue(MachineOperand &OldOpnd) const {
172 auto *Def = getVRegSubRegDef(getRegSubRegPair(OldOpnd), *MRI);
173 if (!Def)
174 return nullptr;
175
176 switch(Def->getOpcode()) {
177 default: break;
178 case AMDGPU::IMPLICIT_DEF:
179 return nullptr;
180 case AMDGPU::COPY:
181 case AMDGPU::V_MOV_B32_e32:
182 case AMDGPU::V_MOV_B64_PSEUDO:
183 case AMDGPU::V_MOV_B64_e32:
184 case AMDGPU::V_MOV_B64_e64: {
185 auto &Op1 = Def->getOperand(1);
186 if (Op1.isImm())
187 return &Op1;
188 break;
189 }
190 }
191 return &OldOpnd;
192}
193
194[[maybe_unused]] static unsigned getOperandSize(MachineInstr &MI, unsigned Idx,
196 int16_t RegClass = MI.getDesc().operands()[Idx].RegClass;
197 if (RegClass == -1)
198 return 0;
199
200 const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
201 return TRI->getRegSizeInBits(*TRI->getRegClass(RegClass));
202}
203
204MachineInstr *GCNDPPCombine::createDPPInst(MachineInstr &OrigMI,
205 MachineInstr &MovMI,
206 RegSubRegPair CombOldVGPR,
207 bool CombBCZ,
208 bool IsShrinkable) const {
209 assert(MovMI.getOpcode() == AMDGPU::V_MOV_B32_dpp ||
210 MovMI.getOpcode() == AMDGPU::V_MOV_B64_dpp ||
211 MovMI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO);
212
213 bool HasVOP3DPP = ST->hasVOP3DPP();
214 auto OrigOp = OrigMI.getOpcode();
215 auto DPPOp = getDPPOp(OrigOp, IsShrinkable);
216 if (DPPOp == -1) {
217 LLVM_DEBUG(dbgs() << " failed: no DPP opcode\n");
218 return nullptr;
219 }
220 int OrigOpE32 = AMDGPU::getVOPe32(OrigOp);
221 // Prior checks cover Mask with VOPC condition, but not on purpose
222 auto *RowMaskOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::row_mask);
223 assert(RowMaskOpnd && RowMaskOpnd->isImm());
224 auto *BankMaskOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::bank_mask);
225 assert(BankMaskOpnd && BankMaskOpnd->isImm());
226 const bool MaskAllLanes =
227 RowMaskOpnd->getImm() == 0xF && BankMaskOpnd->getImm() == 0xF;
228 (void)MaskAllLanes;
229 assert((MaskAllLanes ||
230 !(TII->isVOPC(DPPOp) || (TII->isVOP3(DPPOp) && OrigOpE32 != -1 &&
231 TII->isVOPC(OrigOpE32)))) &&
232 "VOPC cannot form DPP unless mask is full");
233
234 auto DPPInst = BuildMI(*OrigMI.getParent(), OrigMI,
235 OrigMI.getDebugLoc(), TII->get(DPPOp))
236 .setMIFlags(OrigMI.getFlags());
237
238 bool Fail = false;
239 do {
240 int NumOperands = 0;
241 if (auto *Dst = TII->getNamedOperand(OrigMI, AMDGPU::OpName::vdst)) {
242 DPPInst.add(*Dst);
243 ++NumOperands;
244 }
245 if (auto *SDst = TII->getNamedOperand(OrigMI, AMDGPU::OpName::sdst)) {
246 if (TII->isOperandLegal(*DPPInst.getInstr(), NumOperands, SDst)) {
247 DPPInst.add(*SDst);
248 ++NumOperands;
249 }
250 // If we shrunk a 64bit vop3b to 32bits, just ignore the sdst
251 }
252
253 const int OldIdx = AMDGPU::getNamedOperandIdx(DPPOp, AMDGPU::OpName::old);
254 if (OldIdx != -1) {
255 assert(OldIdx == NumOperands);
257 CombOldVGPR,
258 *MRI->getRegClass(
259 TII->getNamedOperand(MovMI, AMDGPU::OpName::vdst)->getReg()),
260 *MRI));
261 auto *Def = getVRegSubRegDef(CombOldVGPR, *MRI);
262 DPPInst.addReg(CombOldVGPR.Reg, Def ? 0 : RegState::Undef,
263 CombOldVGPR.SubReg);
264 ++NumOperands;
265 } else if (TII->isVOPC(DPPOp) || (TII->isVOP3(DPPOp) && OrigOpE32 != -1 &&
266 TII->isVOPC(OrigOpE32))) {
267 // VOPC DPP and VOPC promoted to VOP3 DPP do not have an old operand
268 // because they write to SGPRs not VGPRs
269 } else {
270 // TODO: this discards MAC/FMA instructions for now, let's add it later
271 LLVM_DEBUG(dbgs() << " failed: no old operand in DPP instruction,"
272 " TBD\n");
273 Fail = true;
274 break;
275 }
276
277 auto *Mod0 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src0_modifiers);
278 if (Mod0) {
279 assert(NumOperands == AMDGPU::getNamedOperandIdx(DPPOp,
280 AMDGPU::OpName::src0_modifiers));
281 assert(HasVOP3DPP ||
282 (0LL == (Mod0->getImm() & ~(SISrcMods::ABS | SISrcMods::NEG))));
283 DPPInst.addImm(Mod0->getImm());
284 ++NumOperands;
285 } else if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::src0_modifiers)) {
286 DPPInst.addImm(0);
287 ++NumOperands;
288 }
289 auto *Src0 = TII->getNamedOperand(MovMI, AMDGPU::OpName::src0);
290 assert(Src0);
291 int Src0Idx = NumOperands;
292 if (!TII->isOperandLegal(*DPPInst.getInstr(), NumOperands, Src0)) {
293 LLVM_DEBUG(dbgs() << " failed: src0 is illegal\n");
294 Fail = true;
295 break;
296 }
297 DPPInst.add(*Src0);
298 DPPInst->getOperand(NumOperands).setIsKill(false);
299 ++NumOperands;
300
301 auto *Mod1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1_modifiers);
302 if (Mod1) {
303 assert(NumOperands == AMDGPU::getNamedOperandIdx(DPPOp,
304 AMDGPU::OpName::src1_modifiers));
305 assert(HasVOP3DPP ||
306 (0LL == (Mod1->getImm() & ~(SISrcMods::ABS | SISrcMods::NEG))));
307 DPPInst.addImm(Mod1->getImm());
308 ++NumOperands;
309 } else if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::src1_modifiers)) {
310 DPPInst.addImm(0);
311 ++NumOperands;
312 }
313 auto *Src1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1);
314 if (Src1) {
315 int OpNum = NumOperands;
316 // If subtarget does not support SGPRs for src1 operand then the
317 // requirements are the same as for src0. We check src0 instead because
318 // pseudos are shared between subtargets and allow SGPR for src1 on all.
319 if (!ST->hasDPPSrc1SGPR()) {
320 assert(getOperandSize(*DPPInst, Src0Idx, *MRI) ==
321 getOperandSize(*DPPInst, NumOperands, *MRI) &&
322 "Src0 and Src1 operands should have the same size");
323 OpNum = Src0Idx;
324 }
325 if (!TII->isOperandLegal(*DPPInst.getInstr(), OpNum, Src1)) {
326 LLVM_DEBUG(dbgs() << " failed: src1 is illegal\n");
327 Fail = true;
328 break;
329 }
330 DPPInst.add(*Src1);
331 ++NumOperands;
332 }
333
334 auto *Mod2 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src2_modifiers);
335 if (Mod2) {
336 assert(NumOperands ==
337 AMDGPU::getNamedOperandIdx(DPPOp, AMDGPU::OpName::src2_modifiers));
338 assert(HasVOP3DPP ||
339 (0LL == (Mod2->getImm() & ~(SISrcMods::ABS | SISrcMods::NEG))));
340 DPPInst.addImm(Mod2->getImm());
341 ++NumOperands;
342 }
343 auto *Src2 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src2);
344 if (Src2) {
345 if (!TII->getNamedOperand(*DPPInst.getInstr(), AMDGPU::OpName::src2) ||
346 !TII->isOperandLegal(*DPPInst.getInstr(), NumOperands, Src2)) {
347 LLVM_DEBUG(dbgs() << " failed: src2 is illegal\n");
348 Fail = true;
349 break;
350 }
351 DPPInst.add(*Src2);
352 ++NumOperands;
353 }
354
355 if (HasVOP3DPP) {
356 auto *ClampOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::clamp);
357 if (ClampOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::clamp)) {
358 DPPInst.addImm(ClampOpr->getImm());
359 }
360 auto *VdstInOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::vdst_in);
361 if (VdstInOpr &&
362 AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::vdst_in)) {
363 DPPInst.add(*VdstInOpr);
364 }
365 auto *OmodOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::omod);
366 if (OmodOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::omod)) {
367 DPPInst.addImm(OmodOpr->getImm());
368 }
369 // Validate OP_SEL has to be set to all 0 and OP_SEL_HI has to be set to
370 // all 1.
371 if (TII->getNamedOperand(OrigMI, AMDGPU::OpName::op_sel)) {
372 int64_t OpSel = 0;
373 OpSel |= (Mod0 ? (!!(Mod0->getImm() & SISrcMods::OP_SEL_0) << 0) : 0);
374 OpSel |= (Mod1 ? (!!(Mod1->getImm() & SISrcMods::OP_SEL_0) << 1) : 0);
375 OpSel |= (Mod2 ? (!!(Mod2->getImm() & SISrcMods::OP_SEL_0) << 2) : 0);
376 if (Mod0 && TII->isVOP3(OrigMI) && !TII->isVOP3P(OrigMI))
377 OpSel |= !!(Mod0->getImm() & SISrcMods::DST_OP_SEL) << 3;
378
379 if (OpSel != 0) {
380 LLVM_DEBUG(dbgs() << " failed: op_sel must be zero\n");
381 Fail = true;
382 break;
383 }
384 if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::op_sel))
385 DPPInst.addImm(OpSel);
386 }
387 if (TII->getNamedOperand(OrigMI, AMDGPU::OpName::op_sel_hi)) {
388 int64_t OpSelHi = 0;
389 OpSelHi |= (Mod0 ? (!!(Mod0->getImm() & SISrcMods::OP_SEL_1) << 0) : 0);
390 OpSelHi |= (Mod1 ? (!!(Mod1->getImm() & SISrcMods::OP_SEL_1) << 1) : 0);
391 OpSelHi |= (Mod2 ? (!!(Mod2->getImm() & SISrcMods::OP_SEL_1) << 2) : 0);
392
393 // Only vop3p has op_sel_hi, and all vop3p have 3 operands, so check
394 // the bitmask for 3 op_sel_hi bits set
395 assert(Src2 && "Expected vop3p with 3 operands");
396 if (OpSelHi != 7) {
397 LLVM_DEBUG(dbgs() << " failed: op_sel_hi must be all set to one\n");
398 Fail = true;
399 break;
400 }
401 if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::op_sel_hi))
402 DPPInst.addImm(OpSelHi);
403 }
404 auto *NegOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::neg_lo);
405 if (NegOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::neg_lo)) {
406 DPPInst.addImm(NegOpr->getImm());
407 }
408 auto *NegHiOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::neg_hi);
409 if (NegHiOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::neg_hi)) {
410 DPPInst.addImm(NegHiOpr->getImm());
411 }
412 auto *ByteSelOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::byte_sel);
413 if (ByteSelOpr &&
414 AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::byte_sel)) {
415 DPPInst.addImm(ByteSelOpr->getImm());
416 }
417 }
418 DPPInst.add(*TII->getNamedOperand(MovMI, AMDGPU::OpName::dpp_ctrl));
419 DPPInst.add(*TII->getNamedOperand(MovMI, AMDGPU::OpName::row_mask));
420 DPPInst.add(*TII->getNamedOperand(MovMI, AMDGPU::OpName::bank_mask));
421 DPPInst.addImm(CombBCZ ? 1 : 0);
422 } while (false);
423
424 if (Fail) {
425 DPPInst.getInstr()->eraseFromParent();
426 return nullptr;
427 }
428 LLVM_DEBUG(dbgs() << " combined: " << *DPPInst.getInstr());
429 return DPPInst.getInstr();
430}
431
432static bool isIdentityValue(unsigned OrigMIOp, MachineOperand *OldOpnd) {
433 assert(OldOpnd->isImm());
434 switch (OrigMIOp) {
435 default: break;
436 case AMDGPU::V_ADD_U32_e32:
437 case AMDGPU::V_ADD_U32_e64:
438 case AMDGPU::V_ADD_CO_U32_e32:
439 case AMDGPU::V_ADD_CO_U32_e64:
440 case AMDGPU::V_OR_B32_e32:
441 case AMDGPU::V_OR_B32_e64:
442 case AMDGPU::V_SUBREV_U32_e32:
443 case AMDGPU::V_SUBREV_U32_e64:
444 case AMDGPU::V_SUBREV_CO_U32_e32:
445 case AMDGPU::V_SUBREV_CO_U32_e64:
446 case AMDGPU::V_MAX_U32_e32:
447 case AMDGPU::V_MAX_U32_e64:
448 case AMDGPU::V_XOR_B32_e32:
449 case AMDGPU::V_XOR_B32_e64:
450 if (OldOpnd->getImm() == 0)
451 return true;
452 break;
453 case AMDGPU::V_AND_B32_e32:
454 case AMDGPU::V_AND_B32_e64:
455 case AMDGPU::V_MIN_U32_e32:
456 case AMDGPU::V_MIN_U32_e64:
457 if (static_cast<uint32_t>(OldOpnd->getImm()) ==
458 std::numeric_limits<uint32_t>::max())
459 return true;
460 break;
461 case AMDGPU::V_MIN_I32_e32:
462 case AMDGPU::V_MIN_I32_e64:
463 if (static_cast<int32_t>(OldOpnd->getImm()) ==
464 std::numeric_limits<int32_t>::max())
465 return true;
466 break;
467 case AMDGPU::V_MAX_I32_e32:
468 case AMDGPU::V_MAX_I32_e64:
469 if (static_cast<int32_t>(OldOpnd->getImm()) ==
470 std::numeric_limits<int32_t>::min())
471 return true;
472 break;
473 case AMDGPU::V_MUL_I32_I24_e32:
474 case AMDGPU::V_MUL_I32_I24_e64:
475 case AMDGPU::V_MUL_U32_U24_e32:
476 case AMDGPU::V_MUL_U32_U24_e64:
477 if (OldOpnd->getImm() == 1)
478 return true;
479 break;
480 }
481 return false;
482}
483
484MachineInstr *GCNDPPCombine::createDPPInst(
485 MachineInstr &OrigMI, MachineInstr &MovMI, RegSubRegPair CombOldVGPR,
486 MachineOperand *OldOpndValue, bool CombBCZ, bool IsShrinkable) const {
487 assert(CombOldVGPR.Reg);
488 if (!CombBCZ && OldOpndValue && OldOpndValue->isImm()) {
489 auto *Src1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1);
490 if (!Src1 || !Src1->isReg()) {
491 LLVM_DEBUG(dbgs() << " failed: no src1 or it isn't a register\n");
492 return nullptr;
493 }
494 if (!isIdentityValue(OrigMI.getOpcode(), OldOpndValue)) {
495 LLVM_DEBUG(dbgs() << " failed: old immediate isn't an identity\n");
496 return nullptr;
497 }
498 CombOldVGPR = getRegSubRegPair(*Src1);
499 auto MovDst = TII->getNamedOperand(MovMI, AMDGPU::OpName::vdst);
500 const TargetRegisterClass *RC = MRI->getRegClass(MovDst->getReg());
501 if (!isOfRegClass(CombOldVGPR, *RC, *MRI)) {
502 LLVM_DEBUG(dbgs() << " failed: src1 has wrong register class\n");
503 return nullptr;
504 }
505 }
506 return createDPPInst(OrigMI, MovMI, CombOldVGPR, CombBCZ, IsShrinkable);
507}
508
509// returns true if MI doesn't have OpndName immediate operand or the
510// operand has Value
511bool GCNDPPCombine::hasNoImmOrEqual(MachineInstr &MI, unsigned OpndName,
512 int64_t Value, int64_t Mask) const {
513 auto *Imm = TII->getNamedOperand(MI, OpndName);
514 if (!Imm)
515 return true;
516
517 assert(Imm->isImm());
518 return (Imm->getImm() & Mask) == Value;
519}
520
521bool GCNDPPCombine::combineDPPMov(MachineInstr &MovMI) const {
522 assert(MovMI.getOpcode() == AMDGPU::V_MOV_B32_dpp ||
523 MovMI.getOpcode() == AMDGPU::V_MOV_B64_dpp ||
524 MovMI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO);
525 LLVM_DEBUG(dbgs() << "\nDPP combine: " << MovMI);
526
527 auto *DstOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::vdst);
528 assert(DstOpnd && DstOpnd->isReg());
529 auto DPPMovReg = DstOpnd->getReg();
530 if (DPPMovReg.isPhysical()) {
531 LLVM_DEBUG(dbgs() << " failed: dpp move writes physreg\n");
532 return false;
533 }
534 if (execMayBeModifiedBeforeAnyUse(*MRI, DPPMovReg, MovMI)) {
535 LLVM_DEBUG(dbgs() << " failed: EXEC mask should remain the same"
536 " for all uses\n");
537 return false;
538 }
539
540 if (MovMI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO ||
541 MovMI.getOpcode() == AMDGPU::V_MOV_B64_dpp) {
542 auto *DppCtrl = TII->getNamedOperand(MovMI, AMDGPU::OpName::dpp_ctrl);
543 assert(DppCtrl && DppCtrl->isImm());
545 LLVM_DEBUG(dbgs() << " failed: 64 bit dpp move uses unsupported"
546 " control value\n");
547 // Let it split, then control may become legal.
548 return false;
549 }
550 }
551
552 auto *RowMaskOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::row_mask);
553 assert(RowMaskOpnd && RowMaskOpnd->isImm());
554 auto *BankMaskOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::bank_mask);
555 assert(BankMaskOpnd && BankMaskOpnd->isImm());
556 const bool MaskAllLanes = RowMaskOpnd->getImm() == 0xF &&
557 BankMaskOpnd->getImm() == 0xF;
558
559 auto *BCZOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::bound_ctrl);
560 assert(BCZOpnd && BCZOpnd->isImm());
561 bool BoundCtrlZero = BCZOpnd->getImm();
562
563 auto *OldOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::old);
564 auto *SrcOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::src0);
565 assert(OldOpnd && OldOpnd->isReg());
566 assert(SrcOpnd && SrcOpnd->isReg());
567 if (OldOpnd->getReg().isPhysical() || SrcOpnd->getReg().isPhysical()) {
568 LLVM_DEBUG(dbgs() << " failed: dpp move reads physreg\n");
569 return false;
570 }
571
572 auto * const OldOpndValue = getOldOpndValue(*OldOpnd);
573 // OldOpndValue is either undef (IMPLICIT_DEF) or immediate or something else
574 // We could use: assert(!OldOpndValue || OldOpndValue->isImm())
575 // but the third option is used to distinguish undef from non-immediate
576 // to reuse IMPLICIT_DEF instruction later
577 assert(!OldOpndValue || OldOpndValue->isImm() || OldOpndValue == OldOpnd);
578
579 bool CombBCZ = false;
580
581 if (MaskAllLanes && BoundCtrlZero) { // [1]
582 CombBCZ = true;
583 } else {
584 if (!OldOpndValue || !OldOpndValue->isImm()) {
585 LLVM_DEBUG(dbgs() << " failed: the DPP mov isn't combinable\n");
586 return false;
587 }
588
589 if (OldOpndValue->getImm() == 0) {
590 if (MaskAllLanes) {
591 assert(!BoundCtrlZero); // by check [1]
592 CombBCZ = true;
593 }
594 } else if (BoundCtrlZero) {
595 assert(!MaskAllLanes); // by check [1]
596 LLVM_DEBUG(dbgs() <<
597 " failed: old!=0 and bctrl:0 and not all lanes isn't combinable\n");
598 return false;
599 }
600 }
601
602 LLVM_DEBUG(dbgs() << " old=";
603 if (!OldOpndValue)
604 dbgs() << "undef";
605 else
606 dbgs() << *OldOpndValue;
607 dbgs() << ", bound_ctrl=" << CombBCZ << '\n');
608
609 SmallVector<MachineInstr*, 4> OrigMIs, DPPMIs;
611 auto CombOldVGPR = getRegSubRegPair(*OldOpnd);
612 // try to reuse previous old reg if its undefined (IMPLICIT_DEF)
613 if (CombBCZ && OldOpndValue) { // CombOldVGPR should be undef
614 const TargetRegisterClass *RC = MRI->getRegClass(DPPMovReg);
615 CombOldVGPR = RegSubRegPair(
616 MRI->createVirtualRegister(RC));
617 auto UndefInst = BuildMI(*MovMI.getParent(), MovMI, MovMI.getDebugLoc(),
618 TII->get(AMDGPU::IMPLICIT_DEF), CombOldVGPR.Reg);
619 DPPMIs.push_back(UndefInst.getInstr());
620 }
621
622 OrigMIs.push_back(&MovMI);
623 bool Rollback = true;
625
626 for (auto &Use : MRI->use_nodbg_operands(DPPMovReg)) {
627 Uses.push_back(&Use);
628 }
629
630 while (!Uses.empty()) {
631 MachineOperand *Use = Uses.pop_back_val();
632 Rollback = true;
633
634 auto &OrigMI = *Use->getParent();
635 LLVM_DEBUG(dbgs() << " try: " << OrigMI);
636
637 auto OrigOp = OrigMI.getOpcode();
638 assert((TII->get(OrigOp).getSize() != 4 || !AMDGPU::isTrue16Inst(OrigOp)) &&
639 "There should not be e32 True16 instructions pre-RA");
640 if (OrigOp == AMDGPU::REG_SEQUENCE) {
641 Register FwdReg = OrigMI.getOperand(0).getReg();
642 unsigned FwdSubReg = 0;
643
644 if (execMayBeModifiedBeforeAnyUse(*MRI, FwdReg, OrigMI)) {
645 LLVM_DEBUG(dbgs() << " failed: EXEC mask should remain the same"
646 " for all uses\n");
647 break;
648 }
649
650 unsigned OpNo, E = OrigMI.getNumOperands();
651 for (OpNo = 1; OpNo < E; OpNo += 2) {
652 if (OrigMI.getOperand(OpNo).getReg() == DPPMovReg) {
653 FwdSubReg = OrigMI.getOperand(OpNo + 1).getImm();
654 break;
655 }
656 }
657
658 if (!FwdSubReg)
659 break;
660
661 for (auto &Op : MRI->use_nodbg_operands(FwdReg)) {
662 if (Op.getSubReg() == FwdSubReg)
663 Uses.push_back(&Op);
664 }
665 RegSeqWithOpNos[&OrigMI].push_back(OpNo);
666 continue;
667 }
668
669 bool IsShrinkable = isShrinkable(OrigMI);
670 if (!(IsShrinkable ||
671 ((TII->isVOP3P(OrigOp) || TII->isVOPC(OrigOp) ||
672 TII->isVOP3(OrigOp)) &&
673 ST->hasVOP3DPP()) ||
674 TII->isVOP1(OrigOp) || TII->isVOP2(OrigOp))) {
675 LLVM_DEBUG(dbgs() << " failed: not VOP1/2/3/3P/C\n");
676 break;
677 }
678 if (OrigMI.modifiesRegister(AMDGPU::EXEC, ST->getRegisterInfo())) {
679 LLVM_DEBUG(dbgs() << " failed: can't combine v_cmpx\n");
680 break;
681 }
682
683 auto *Src0 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src0);
684 auto *Src1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1);
685 if (Use != Src0 && !(Use == Src1 && OrigMI.isCommutable())) { // [1]
686 LLVM_DEBUG(dbgs() << " failed: no suitable operands\n");
687 break;
688 }
689
690 auto *Src2 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src2);
691 assert(Src0 && "Src1 without Src0?");
692 if ((Use == Src0 && ((Src1 && Src1->isIdenticalTo(*Src0)) ||
693 (Src2 && Src2->isIdenticalTo(*Src0)))) ||
694 (Use == Src1 && (Src1->isIdenticalTo(*Src0) ||
695 (Src2 && Src2->isIdenticalTo(*Src1))))) {
697 dbgs()
698 << " " << OrigMI
699 << " failed: DPP register is used more than once per instruction\n");
700 break;
701 }
702
703 LLVM_DEBUG(dbgs() << " combining: " << OrigMI);
704 if (Use == Src0) {
705 if (auto *DPPInst = createDPPInst(OrigMI, MovMI, CombOldVGPR,
706 OldOpndValue, CombBCZ, IsShrinkable)) {
707 DPPMIs.push_back(DPPInst);
708 Rollback = false;
709 }
710 } else {
711 assert(Use == Src1 && OrigMI.isCommutable()); // by check [1]
712 auto *BB = OrigMI.getParent();
713 auto *NewMI = BB->getParent()->CloneMachineInstr(&OrigMI);
714 BB->insert(OrigMI, NewMI);
715 if (TII->commuteInstruction(*NewMI)) {
716 LLVM_DEBUG(dbgs() << " commuted: " << *NewMI);
717 if (auto *DPPInst =
718 createDPPInst(*NewMI, MovMI, CombOldVGPR, OldOpndValue, CombBCZ,
719 IsShrinkable)) {
720 DPPMIs.push_back(DPPInst);
721 Rollback = false;
722 }
723 } else
724 LLVM_DEBUG(dbgs() << " failed: cannot be commuted\n");
725 NewMI->eraseFromParent();
726 }
727 if (Rollback)
728 break;
729 OrigMIs.push_back(&OrigMI);
730 }
731
732 Rollback |= !Uses.empty();
733
734 for (auto *MI : *(Rollback? &DPPMIs : &OrigMIs))
735 MI->eraseFromParent();
736
737 if (!Rollback) {
738 for (auto &S : RegSeqWithOpNos) {
739 if (MRI->use_nodbg_empty(S.first->getOperand(0).getReg())) {
740 S.first->eraseFromParent();
741 continue;
742 }
743 while (!S.second.empty())
744 S.first->getOperand(S.second.pop_back_val()).setIsUndef();
745 }
746 }
747
748 return !Rollback;
749}
750
751bool GCNDPPCombine::runOnMachineFunction(MachineFunction &MF) {
753 if (!ST->hasDPP() || skipFunction(MF.getFunction()))
754 return false;
755
756 MRI = &MF.getRegInfo();
757 TII = ST->getInstrInfo();
758
759 bool Changed = false;
760 for (auto &MBB : MF) {
762 if (MI.getOpcode() == AMDGPU::V_MOV_B32_dpp && combineDPPMov(MI)) {
763 Changed = true;
764 ++NumDPPMovsCombined;
765 } else if (MI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO ||
766 MI.getOpcode() == AMDGPU::V_MOV_B64_dpp) {
767 if (ST->hasDPALU_DPP() && combineDPPMov(MI)) {
768 Changed = true;
769 ++NumDPPMovsCombined;
770 } else {
771 auto Split = TII->expandMovDPP64(MI);
772 for (auto *M : {Split.first, Split.second}) {
773 if (M && combineDPPMov(*M))
774 ++NumDPPMovsCombined;
775 }
776 Changed = true;
777 }
778 }
779 }
780 }
781 return Changed;
782}
unsigned const MachineRegisterInfo * MRI
#define Fail
MachineBasicBlock & MBB
Provides AMDGPU specific target descriptions.
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
static bool isIdentityValue(unsigned OrigMIOp, MachineOperand *OldOpnd)
static unsigned getOperandSize(MachineInstr &MI, unsigned Idx, MachineRegisterInfo &MRI)
#define DEBUG_TYPE
Rewrite Partial Register Uses
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
TargetInstrInfo::RegSubRegPair RegSubRegPair
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:269
This class represents an Operation in the Expression.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
unsigned getSize(const MachineInstr &MI) const
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineInstr * CloneMachineInstr(const MachineInstr *Orig)
Create a new MachineInstr which is a copy of Orig, identical in all ways except the instruction has n...
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
Representation of each machine instruction.
Definition: MachineInstr.h:69
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:558
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:341
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:561
bool modifiesRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr modifies (fully define or partially define) the specified register.
bool isCommutable(QueryType Type=IgnoreBundle) const
Return true if this may be a 2- or 3-address instruction (of the form "X = op Y, Z,...
void insert(mop_iterator InsertBefore, ArrayRef< MachineOperand > Ops)
Inserts Ops BEFORE It. Can untie/retie tied operands.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:487
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:568
uint32_t getFlags() const
Return the MI flags bitvector.
Definition: MachineInstr.h:386
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:95
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
LLVM_READONLY int getVOPe32(uint16_t Opcode)
LLVM_READNONE bool isLegalDPALU_DPPControl(unsigned DC)
LLVM_READONLY int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
LLVM_READONLY int getDPPOp32(uint16_t Opcode)
bool isTrue16Inst(unsigned Opc)
LLVM_READONLY bool hasNamedOperand(uint64_t Opcode, uint64_t NamedIdx)
LLVM_READONLY int getDPPOp64(uint16_t Opcode)
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:121
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ Undef
Value of the register doesn't matter.
NodeAddr< DefNode * > Def
Definition: RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
TargetInstrInfo::RegSubRegPair getRegSubRegPair(const MachineOperand &O)
Create RegSubRegPair from a register MachineOperand.
Definition: SIInstrInfo.h:1404
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:419
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
MachineInstr * getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P, MachineRegisterInfo &MRI)
Return the defining instruction for a given reg:subreg pair skipping copy like instructions and subre...
char & GCNDPPCombineID
bool isOfRegClass(const TargetInstrInfo::RegSubRegPair &P, const TargetRegisterClass &TRC, MachineRegisterInfo &MRI)
Returns true if a reg:subreg pair P has a TRC class.
Definition: SIInstrInfo.h:1392
void initializeGCNDPPCombinePass(PassRegistry &)
FunctionPass * createGCNDPPCombinePass()
bool execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo &MRI, Register VReg, const MachineInstr &DefMI)
Return false if EXEC is not changed between the def of VReg at DefMI and all its uses.
A pair composed of a register and a sub-register index.