LLVM 22.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 "GCNDPPCombine.h"
41#include "AMDGPU.h"
42#include "GCNSubtarget.h"
44#include "llvm/ADT/Statistic.h"
46
47using namespace llvm;
48
49#define DEBUG_TYPE "gcn-dpp-combine"
50
51STATISTIC(NumDPPMovsCombined, "Number of DPP moves combined.");
52
53namespace {
54
55class GCNDPPCombine {
57 const SIInstrInfo *TII;
58 const GCNSubtarget *ST;
59
61
62 MachineOperand *getOldOpndValue(MachineOperand &OldOpnd) const;
63
64 MachineInstr *createDPPInst(MachineInstr &OrigMI, MachineInstr &MovMI,
65 RegSubRegPair CombOldVGPR,
66 MachineOperand *OldOpnd, bool CombBCZ,
67 bool IsShrinkable) const;
68
69 MachineInstr *createDPPInst(MachineInstr &OrigMI, MachineInstr &MovMI,
70 RegSubRegPair CombOldVGPR, bool CombBCZ,
71 bool IsShrinkable) const;
72
73 bool hasNoImmOrEqual(MachineInstr &MI, AMDGPU::OpName OpndName, int64_t Value,
74 int64_t Mask = -1) const;
75
76 bool combineDPPMov(MachineInstr &MI) const;
77
78 int getDPPOp(unsigned Op, bool IsShrinkable) const;
79 bool isShrinkable(MachineInstr &MI) const;
80
81public:
82 bool run(MachineFunction &MF);
83};
84
85class GCNDPPCombineLegacy : public MachineFunctionPass {
86public:
87 static char ID;
88
89 GCNDPPCombineLegacy() : MachineFunctionPass(ID) {}
90
91 bool runOnMachineFunction(MachineFunction &MF) override;
92
93 StringRef getPassName() const override { return "GCN DPP Combine"; }
94
95 void getAnalysisUsage(AnalysisUsage &AU) const override {
96 AU.setPreservesCFG();
98 }
99
101 return MachineFunctionProperties().setIsSSA();
102 }
103};
104
105} // end anonymous namespace
106
107INITIALIZE_PASS(GCNDPPCombineLegacy, DEBUG_TYPE, "GCN DPP Combine", false,
108 false)
109
110char GCNDPPCombineLegacy::ID = 0;
111
112char &llvm::GCNDPPCombineLegacyID = GCNDPPCombineLegacy::ID;
113
115 return new GCNDPPCombineLegacy();
116}
117
118bool GCNDPPCombine::isShrinkable(MachineInstr &MI) const {
119 unsigned Op = MI.getOpcode();
120 if (!TII->isVOP3(Op)) {
121 return false;
122 }
123 if (!TII->hasVALU32BitEncoding(Op)) {
124 LLVM_DEBUG(dbgs() << " Inst hasn't e32 equivalent\n");
125 return false;
126 }
127 // Do not shrink True16 instructions pre-RA to avoid the restriction in
128 // register allocation from only being able to use 128 VGPRs
130 return false;
131 if (const auto *SDst = TII->getNamedOperand(MI, AMDGPU::OpName::sdst)) {
132 // Give up if there are any uses of the sdst in carry-out or VOPC.
133 // The shrunken form of the instruction would write it to vcc instead of to
134 // a virtual register. If we rewrote the uses the shrinking would be
135 // possible.
136 if (!MRI->use_nodbg_empty(SDst->getReg()))
137 return false;
138 }
139 // check if other than abs|neg modifiers are set (opsel for example)
140 const int64_t Mask = ~(SISrcMods::ABS | SISrcMods::NEG);
141 if (!hasNoImmOrEqual(MI, AMDGPU::OpName::src0_modifiers, 0, Mask) ||
142 !hasNoImmOrEqual(MI, AMDGPU::OpName::src1_modifiers, 0, Mask) ||
143 !hasNoImmOrEqual(MI, AMDGPU::OpName::clamp, 0) ||
144 !hasNoImmOrEqual(MI, AMDGPU::OpName::omod, 0) ||
145 !hasNoImmOrEqual(MI, AMDGPU::OpName::byte_sel, 0)) {
146 LLVM_DEBUG(dbgs() << " Inst has non-default modifiers\n");
147 return false;
148 }
149 return true;
150}
151
152int GCNDPPCombine::getDPPOp(unsigned Op, bool IsShrinkable) const {
153 int DPP32 = AMDGPU::getDPPOp32(Op);
154 if (IsShrinkable) {
155 assert(DPP32 == -1);
156 int E32 = AMDGPU::getVOPe32(Op);
157 DPP32 = (E32 == -1) ? -1 : AMDGPU::getDPPOp32(E32);
158 }
159 if (DPP32 != -1 && TII->pseudoToMCOpcode(DPP32) != -1)
160 return DPP32;
161 int DPP64 = -1;
162 if (ST->hasVOP3DPP())
163 DPP64 = AMDGPU::getDPPOp64(Op);
164 if (DPP64 != -1 && TII->pseudoToMCOpcode(DPP64) != -1)
165 return DPP64;
166 return -1;
167}
168
169// tracks the register operand definition and returns:
170// 1. immediate operand used to initialize the register if found
171// 2. nullptr if the register operand is undef
172// 3. the operand itself otherwise
173MachineOperand *GCNDPPCombine::getOldOpndValue(MachineOperand &OldOpnd) const {
174 auto *Def = getVRegSubRegDef(getRegSubRegPair(OldOpnd), *MRI);
175 if (!Def)
176 return nullptr;
177
178 switch(Def->getOpcode()) {
179 default: break;
180 case AMDGPU::IMPLICIT_DEF:
181 return nullptr;
182 case AMDGPU::COPY:
183 case AMDGPU::V_MOV_B32_e32:
184 case AMDGPU::V_MOV_B64_PSEUDO:
185 case AMDGPU::V_MOV_B64_e32:
186 case AMDGPU::V_MOV_B64_e64: {
187 auto &Op1 = Def->getOperand(1);
188 if (Op1.isImm())
189 return &Op1;
190 break;
191 }
192 }
193 return &OldOpnd;
194}
195
196[[maybe_unused]] static unsigned getOperandSize(MachineInstr &MI, unsigned Idx,
198 int16_t RegClass = MI.getDesc().operands()[Idx].RegClass;
199 if (RegClass == -1)
200 return 0;
201
202 const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
203 return TRI->getRegSizeInBits(*TRI->getRegClass(RegClass));
204}
205
206MachineInstr *GCNDPPCombine::createDPPInst(MachineInstr &OrigMI,
207 MachineInstr &MovMI,
208 RegSubRegPair CombOldVGPR,
209 bool CombBCZ,
210 bool IsShrinkable) const {
211 assert(MovMI.getOpcode() == AMDGPU::V_MOV_B32_dpp ||
212 MovMI.getOpcode() == AMDGPU::V_MOV_B64_dpp ||
213 MovMI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO);
214
215 bool HasVOP3DPP = ST->hasVOP3DPP();
216 auto OrigOp = OrigMI.getOpcode();
217 if (ST->useRealTrue16Insts() && AMDGPU::isTrue16Inst(OrigOp)) {
219 dbgs() << " failed: Did not expect any 16-bit uses of dpp values\n");
220 return nullptr;
221 }
222 auto DPPOp = getDPPOp(OrigOp, IsShrinkable);
223 if (DPPOp == -1) {
224 LLVM_DEBUG(dbgs() << " failed: no DPP opcode\n");
225 return nullptr;
226 }
227 int OrigOpE32 = AMDGPU::getVOPe32(OrigOp);
228 // Prior checks cover Mask with VOPC condition, but not on purpose
229 auto *RowMaskOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::row_mask);
230 assert(RowMaskOpnd && RowMaskOpnd->isImm());
231 auto *BankMaskOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::bank_mask);
232 assert(BankMaskOpnd && BankMaskOpnd->isImm());
233 const bool MaskAllLanes =
234 RowMaskOpnd->getImm() == 0xF && BankMaskOpnd->getImm() == 0xF;
235 (void)MaskAllLanes;
236 assert((MaskAllLanes ||
237 !(TII->isVOPC(DPPOp) || (TII->isVOP3(DPPOp) && OrigOpE32 != -1 &&
238 TII->isVOPC(OrigOpE32)))) &&
239 "VOPC cannot form DPP unless mask is full");
240
241 auto DPPInst = BuildMI(*OrigMI.getParent(), OrigMI,
242 OrigMI.getDebugLoc(), TII->get(DPPOp))
243 .setMIFlags(OrigMI.getFlags());
244
245 bool Fail = false;
246 do {
247 int NumOperands = 0;
248 if (auto *Dst = TII->getNamedOperand(OrigMI, AMDGPU::OpName::vdst)) {
249 DPPInst.add(*Dst);
250 ++NumOperands;
251 }
252 if (auto *SDst = TII->getNamedOperand(OrigMI, AMDGPU::OpName::sdst)) {
253 if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::sdst)) {
254 DPPInst.add(*SDst);
255 ++NumOperands;
256 }
257 // If we shrunk a 64bit vop3b to 32bits, just ignore the sdst
258 }
259
260 const int OldIdx = AMDGPU::getNamedOperandIdx(DPPOp, AMDGPU::OpName::old);
261 if (OldIdx != -1) {
262 assert(OldIdx == NumOperands);
264 CombOldVGPR,
265 *MRI->getRegClass(
266 TII->getNamedOperand(MovMI, AMDGPU::OpName::vdst)->getReg()),
267 *MRI));
268 auto *Def = getVRegSubRegDef(CombOldVGPR, *MRI);
269 DPPInst.addReg(CombOldVGPR.Reg, Def ? 0 : RegState::Undef,
270 CombOldVGPR.SubReg);
271 ++NumOperands;
272 } else if (TII->isVOPC(DPPOp) || (TII->isVOP3(DPPOp) && OrigOpE32 != -1 &&
273 TII->isVOPC(OrigOpE32))) {
274 // VOPC DPP and VOPC promoted to VOP3 DPP do not have an old operand
275 // because they write to SGPRs not VGPRs
276 } else {
277 // TODO: this discards MAC/FMA instructions for now, let's add it later
278 LLVM_DEBUG(dbgs() << " failed: no old operand in DPP instruction,"
279 " TBD\n");
280 Fail = true;
281 break;
282 }
283
284 auto *Mod0 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src0_modifiers);
285 if (Mod0) {
286 assert(NumOperands == AMDGPU::getNamedOperandIdx(DPPOp,
287 AMDGPU::OpName::src0_modifiers));
288 assert(HasVOP3DPP ||
289 (0LL == (Mod0->getImm() & ~(SISrcMods::ABS | SISrcMods::NEG))));
290 DPPInst.addImm(Mod0->getImm());
291 ++NumOperands;
292 } else if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::src0_modifiers)) {
293 DPPInst.addImm(0);
294 ++NumOperands;
295 }
296 auto *Src0 = TII->getNamedOperand(MovMI, AMDGPU::OpName::src0);
297 assert(Src0);
298 [[maybe_unused]] int Src0Idx = NumOperands;
299
300 DPPInst.add(*Src0);
301 DPPInst->getOperand(NumOperands).setIsKill(false);
302 ++NumOperands;
303
304 auto *Mod1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1_modifiers);
305 if (Mod1) {
306 assert(NumOperands == AMDGPU::getNamedOperandIdx(DPPOp,
307 AMDGPU::OpName::src1_modifiers));
308 assert(HasVOP3DPP ||
309 (0LL == (Mod1->getImm() & ~(SISrcMods::ABS | SISrcMods::NEG))));
310 DPPInst.addImm(Mod1->getImm());
311 ++NumOperands;
312 } else if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::src1_modifiers)) {
313 DPPInst.addImm(0);
314 ++NumOperands;
315 }
316 auto *Src1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1);
317 if (Src1) {
318 assert(AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::src1) &&
319 "dpp version of instruction missing src1");
320 // If subtarget does not support SGPRs for src1 operand then the
321 // requirements are the same as for src0. We check src0 instead because
322 // pseudos are shared between subtargets and allow SGPR for src1 on all.
323 if (!ST->hasDPPSrc1SGPR()) {
324 assert(getOperandSize(*DPPInst, Src0Idx, *MRI) ==
325 getOperandSize(*DPPInst, NumOperands, *MRI) &&
326 "Src0 and Src1 operands should have the same size");
327 }
328
329 DPPInst.add(*Src1);
330 ++NumOperands;
331 }
332
333 auto *Mod2 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src2_modifiers);
334 if (Mod2) {
335 assert(NumOperands ==
336 AMDGPU::getNamedOperandIdx(DPPOp, AMDGPU::OpName::src2_modifiers));
337 assert(HasVOP3DPP ||
338 (0LL == (Mod2->getImm() & ~(SISrcMods::ABS | SISrcMods::NEG))));
339 DPPInst.addImm(Mod2->getImm());
340 ++NumOperands;
341 }
342 auto *Src2 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src2);
343 if (Src2) {
344 if (!AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::src2)) {
345 LLVM_DEBUG(dbgs() << " failed: dpp does not have src2\n");
346 Fail = true;
347 break;
348 }
349 DPPInst.add(*Src2);
350 ++NumOperands;
351 }
352
353 if (HasVOP3DPP) {
354 auto *ClampOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::clamp);
355 if (ClampOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::clamp)) {
356 DPPInst.addImm(ClampOpr->getImm());
357 }
358 auto *VdstInOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::vdst_in);
359 if (VdstInOpr &&
360 AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::vdst_in)) {
361 DPPInst.add(*VdstInOpr);
362 }
363 auto *OmodOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::omod);
364 if (OmodOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::omod)) {
365 DPPInst.addImm(OmodOpr->getImm());
366 }
367 // Validate OP_SEL has to be set to all 0 and OP_SEL_HI has to be set to
368 // all 1.
369 if (TII->getNamedOperand(OrigMI, AMDGPU::OpName::op_sel)) {
370 int64_t OpSel = 0;
371 OpSel |= (Mod0 ? (!!(Mod0->getImm() & SISrcMods::OP_SEL_0) << 0) : 0);
372 OpSel |= (Mod1 ? (!!(Mod1->getImm() & SISrcMods::OP_SEL_0) << 1) : 0);
373 OpSel |= (Mod2 ? (!!(Mod2->getImm() & SISrcMods::OP_SEL_0) << 2) : 0);
374 if (Mod0 && TII->isVOP3(OrigMI) && !TII->isVOP3P(OrigMI))
375 OpSel |= !!(Mod0->getImm() & SISrcMods::DST_OP_SEL) << 3;
376
377 if (OpSel != 0) {
378 LLVM_DEBUG(dbgs() << " failed: op_sel must be zero\n");
379 Fail = true;
380 break;
381 }
382 if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::op_sel))
383 DPPInst.addImm(OpSel);
384 }
385 if (TII->getNamedOperand(OrigMI, AMDGPU::OpName::op_sel_hi)) {
386 int64_t OpSelHi = 0;
387 OpSelHi |= (Mod0 ? (!!(Mod0->getImm() & SISrcMods::OP_SEL_1) << 0) : 0);
388 OpSelHi |= (Mod1 ? (!!(Mod1->getImm() & SISrcMods::OP_SEL_1) << 1) : 0);
389 OpSelHi |= (Mod2 ? (!!(Mod2->getImm() & SISrcMods::OP_SEL_1) << 2) : 0);
390
391 // Only vop3p has op_sel_hi, and all vop3p have 3 operands, so check
392 // the bitmask for 3 op_sel_hi bits set
393 assert(Src2 && "Expected vop3p with 3 operands");
394 if (OpSelHi != 7) {
395 LLVM_DEBUG(dbgs() << " failed: op_sel_hi must be all set to one\n");
396 Fail = true;
397 break;
398 }
399 if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::op_sel_hi))
400 DPPInst.addImm(OpSelHi);
401 }
402 auto *NegOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::neg_lo);
403 if (NegOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::neg_lo)) {
404 DPPInst.addImm(NegOpr->getImm());
405 }
406 auto *NegHiOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::neg_hi);
407 if (NegHiOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::neg_hi)) {
408 DPPInst.addImm(NegHiOpr->getImm());
409 }
410 auto *ByteSelOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::byte_sel);
411 if (ByteSelOpr &&
412 AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::byte_sel)) {
413 DPPInst.addImm(ByteSelOpr->getImm());
414 }
415 if (MachineOperand *BitOp3 =
416 TII->getNamedOperand(OrigMI, AMDGPU::OpName::bitop3)) {
417 assert(AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::bitop3));
418 DPPInst.add(*BitOp3);
419 }
420 }
421 DPPInst.add(*TII->getNamedOperand(MovMI, AMDGPU::OpName::dpp_ctrl));
422 DPPInst.add(*TII->getNamedOperand(MovMI, AMDGPU::OpName::row_mask));
423 DPPInst.add(*TII->getNamedOperand(MovMI, AMDGPU::OpName::bank_mask));
424 DPPInst.addImm(CombBCZ ? 1 : 0);
425
426 constexpr AMDGPU::OpName Srcs[] = {
427 AMDGPU::OpName::src0, AMDGPU::OpName::src1, AMDGPU::OpName::src2};
428
429 // FIXME: isOperandLegal expects to operate on an completely built
430 // instruction. We should have better legality APIs to check if the
431 // candidate operands will be legal without building the instruction first.
432 for (auto [I, OpName] : enumerate(Srcs)) {
433 int OpIdx = AMDGPU::getNamedOperandIdx(DPPOp, OpName);
434 if (OpIdx == -1)
435 break;
436
437 if (!TII->isOperandLegal(*DPPInst, OpIdx)) {
438 LLVM_DEBUG(dbgs() << " failed: src" << I << " operand is illegal\n");
439 Fail = true;
440 break;
441 }
442 }
443 } while (false);
444
445 if (Fail) {
446 DPPInst.getInstr()->eraseFromParent();
447 return nullptr;
448 }
449 LLVM_DEBUG(dbgs() << " combined: " << *DPPInst.getInstr());
450 return DPPInst.getInstr();
451}
452
453static bool isIdentityValue(unsigned OrigMIOp, MachineOperand *OldOpnd) {
454 assert(OldOpnd->isImm());
455 switch (OrigMIOp) {
456 default: break;
457 case AMDGPU::V_ADD_U32_e32:
458 case AMDGPU::V_ADD_U32_e64:
459 case AMDGPU::V_ADD_CO_U32_e32:
460 case AMDGPU::V_ADD_CO_U32_e64:
461 case AMDGPU::V_OR_B32_e32:
462 case AMDGPU::V_OR_B32_e64:
463 case AMDGPU::V_SUBREV_U32_e32:
464 case AMDGPU::V_SUBREV_U32_e64:
465 case AMDGPU::V_SUBREV_CO_U32_e32:
466 case AMDGPU::V_SUBREV_CO_U32_e64:
467 case AMDGPU::V_MAX_U32_e32:
468 case AMDGPU::V_MAX_U32_e64:
469 case AMDGPU::V_XOR_B32_e32:
470 case AMDGPU::V_XOR_B32_e64:
471 if (OldOpnd->getImm() == 0)
472 return true;
473 break;
474 case AMDGPU::V_AND_B32_e32:
475 case AMDGPU::V_AND_B32_e64:
476 case AMDGPU::V_MIN_U32_e32:
477 case AMDGPU::V_MIN_U32_e64:
478 if (static_cast<uint32_t>(OldOpnd->getImm()) ==
479 std::numeric_limits<uint32_t>::max())
480 return true;
481 break;
482 case AMDGPU::V_MIN_I32_e32:
483 case AMDGPU::V_MIN_I32_e64:
484 if (static_cast<int32_t>(OldOpnd->getImm()) ==
485 std::numeric_limits<int32_t>::max())
486 return true;
487 break;
488 case AMDGPU::V_MAX_I32_e32:
489 case AMDGPU::V_MAX_I32_e64:
490 if (static_cast<int32_t>(OldOpnd->getImm()) ==
491 std::numeric_limits<int32_t>::min())
492 return true;
493 break;
494 case AMDGPU::V_MUL_I32_I24_e32:
495 case AMDGPU::V_MUL_I32_I24_e64:
496 case AMDGPU::V_MUL_U32_U24_e32:
497 case AMDGPU::V_MUL_U32_U24_e64:
498 if (OldOpnd->getImm() == 1)
499 return true;
500 break;
501 }
502 return false;
503}
504
505MachineInstr *GCNDPPCombine::createDPPInst(
506 MachineInstr &OrigMI, MachineInstr &MovMI, RegSubRegPair CombOldVGPR,
507 MachineOperand *OldOpndValue, bool CombBCZ, bool IsShrinkable) const {
508 assert(CombOldVGPR.Reg);
509 if (!CombBCZ && OldOpndValue && OldOpndValue->isImm()) {
510 auto *Src1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1);
511 if (!Src1 || !Src1->isReg()) {
512 LLVM_DEBUG(dbgs() << " failed: no src1 or it isn't a register\n");
513 return nullptr;
514 }
515 if (!isIdentityValue(OrigMI.getOpcode(), OldOpndValue)) {
516 LLVM_DEBUG(dbgs() << " failed: old immediate isn't an identity\n");
517 return nullptr;
518 }
519 CombOldVGPR = getRegSubRegPair(*Src1);
520 auto *MovDst = TII->getNamedOperand(MovMI, AMDGPU::OpName::vdst);
521 const TargetRegisterClass *RC = MRI->getRegClass(MovDst->getReg());
522 if (!isOfRegClass(CombOldVGPR, *RC, *MRI)) {
523 LLVM_DEBUG(dbgs() << " failed: src1 has wrong register class\n");
524 return nullptr;
525 }
526 }
527 return createDPPInst(OrigMI, MovMI, CombOldVGPR, CombBCZ, IsShrinkable);
528}
529
530// returns true if MI doesn't have OpndName immediate operand or the
531// operand has Value
532bool GCNDPPCombine::hasNoImmOrEqual(MachineInstr &MI, AMDGPU::OpName OpndName,
533 int64_t Value, int64_t Mask) const {
534 auto *Imm = TII->getNamedOperand(MI, OpndName);
535 if (!Imm)
536 return true;
537
538 assert(Imm->isImm());
539 return (Imm->getImm() & Mask) == Value;
540}
541
542bool GCNDPPCombine::combineDPPMov(MachineInstr &MovMI) const {
543 assert(MovMI.getOpcode() == AMDGPU::V_MOV_B32_dpp ||
544 MovMI.getOpcode() == AMDGPU::V_MOV_B64_dpp ||
545 MovMI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO);
546 LLVM_DEBUG(dbgs() << "\nDPP combine: " << MovMI);
547
548 auto *DstOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::vdst);
549 assert(DstOpnd && DstOpnd->isReg());
550 auto DPPMovReg = DstOpnd->getReg();
551 if (DPPMovReg.isPhysical()) {
552 LLVM_DEBUG(dbgs() << " failed: dpp move writes physreg\n");
553 return false;
554 }
555 if (execMayBeModifiedBeforeAnyUse(*MRI, DPPMovReg, MovMI)) {
556 LLVM_DEBUG(dbgs() << " failed: EXEC mask should remain the same"
557 " for all uses\n");
558 return false;
559 }
560
561 auto *DppCtrl = TII->getNamedOperand(MovMI, AMDGPU::OpName::dpp_ctrl);
562 assert(DppCtrl && DppCtrl->isImm());
563 unsigned DppCtrlVal = DppCtrl->getImm();
564 if ((MovMI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO ||
565 MovMI.getOpcode() == AMDGPU::V_MOV_B64_dpp)) {
566 if (!ST->hasFeature(AMDGPU::FeatureDPALU_DPP)) {
567 LLVM_DEBUG(dbgs() << " failed: 64 bit dpp move is unsupported\n");
568 // Split it.
569 return false;
570 }
571 if (!AMDGPU::isLegalDPALU_DPPControl(*ST, DppCtrlVal)) {
572 LLVM_DEBUG(dbgs() << " failed: 64 bit dpp move uses unsupported"
573 " control value\n");
574 // Let it split, then control may become legal.
575 return false;
576 }
577 }
578
579 auto *RowMaskOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::row_mask);
580 assert(RowMaskOpnd && RowMaskOpnd->isImm());
581 auto *BankMaskOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::bank_mask);
582 assert(BankMaskOpnd && BankMaskOpnd->isImm());
583 const bool MaskAllLanes = RowMaskOpnd->getImm() == 0xF &&
584 BankMaskOpnd->getImm() == 0xF;
585
586 auto *BCZOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::bound_ctrl);
587 assert(BCZOpnd && BCZOpnd->isImm());
588 bool BoundCtrlZero = BCZOpnd->getImm();
589
590 auto *OldOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::old);
591 auto *SrcOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::src0);
592 assert(OldOpnd && OldOpnd->isReg());
593 assert(SrcOpnd && SrcOpnd->isReg());
594 if (OldOpnd->getReg().isPhysical() || SrcOpnd->getReg().isPhysical()) {
595 LLVM_DEBUG(dbgs() << " failed: dpp move reads physreg\n");
596 return false;
597 }
598
599 auto * const OldOpndValue = getOldOpndValue(*OldOpnd);
600 // OldOpndValue is either undef (IMPLICIT_DEF) or immediate or something else
601 // We could use: assert(!OldOpndValue || OldOpndValue->isImm())
602 // but the third option is used to distinguish undef from non-immediate
603 // to reuse IMPLICIT_DEF instruction later
604 assert(!OldOpndValue || OldOpndValue->isImm() || OldOpndValue == OldOpnd);
605
606 bool CombBCZ = false;
607
608 if (MaskAllLanes && BoundCtrlZero) { // [1]
609 CombBCZ = true;
610 } else {
611 if (!OldOpndValue || !OldOpndValue->isImm()) {
612 LLVM_DEBUG(dbgs() << " failed: the DPP mov isn't combinable\n");
613 return false;
614 }
615
616 if (OldOpndValue->getImm() == 0) {
617 if (MaskAllLanes) {
618 assert(!BoundCtrlZero); // by check [1]
619 CombBCZ = true;
620 }
621 } else if (BoundCtrlZero) {
622 assert(!MaskAllLanes); // by check [1]
623 LLVM_DEBUG(dbgs() <<
624 " failed: old!=0 and bctrl:0 and not all lanes isn't combinable\n");
625 return false;
626 }
627 }
628
629 LLVM_DEBUG(dbgs() << " old=";
630 if (!OldOpndValue)
631 dbgs() << "undef";
632 else
633 dbgs() << *OldOpndValue;
634 dbgs() << ", bound_ctrl=" << CombBCZ << '\n');
635
636 SmallVector<MachineInstr*, 4> OrigMIs, DPPMIs;
638 auto CombOldVGPR = getRegSubRegPair(*OldOpnd);
639 // try to reuse previous old reg if its undefined (IMPLICIT_DEF)
640 if (CombBCZ && OldOpndValue) { // CombOldVGPR should be undef
641 const TargetRegisterClass *RC = MRI->getRegClass(DPPMovReg);
642 CombOldVGPR = RegSubRegPair(
643 MRI->createVirtualRegister(RC));
644 auto UndefInst = BuildMI(*MovMI.getParent(), MovMI, MovMI.getDebugLoc(),
645 TII->get(AMDGPU::IMPLICIT_DEF), CombOldVGPR.Reg);
646 DPPMIs.push_back(UndefInst.getInstr());
647 }
648
649 OrigMIs.push_back(&MovMI);
650 bool Rollback = true;
652 llvm::make_pointer_range(MRI->use_nodbg_operands(DPPMovReg)));
653
654 while (!Uses.empty()) {
655 MachineOperand *Use = Uses.pop_back_val();
656 Rollback = true;
657
658 auto &OrigMI = *Use->getParent();
659 LLVM_DEBUG(dbgs() << " try: " << OrigMI);
660
661 auto OrigOp = OrigMI.getOpcode();
662 assert((TII->get(OrigOp).getSize() != 4 || !AMDGPU::isTrue16Inst(OrigOp)) &&
663 "There should not be e32 True16 instructions pre-RA");
664 if (OrigOp == AMDGPU::REG_SEQUENCE) {
665 Register FwdReg = OrigMI.getOperand(0).getReg();
666 unsigned FwdSubReg = 0;
667
668 if (execMayBeModifiedBeforeAnyUse(*MRI, FwdReg, OrigMI)) {
669 LLVM_DEBUG(dbgs() << " failed: EXEC mask should remain the same"
670 " for all uses\n");
671 break;
672 }
673
674 unsigned OpNo, E = OrigMI.getNumOperands();
675 for (OpNo = 1; OpNo < E; OpNo += 2) {
676 if (OrigMI.getOperand(OpNo).getReg() == DPPMovReg) {
677 FwdSubReg = OrigMI.getOperand(OpNo + 1).getImm();
678 break;
679 }
680 }
681
682 if (!FwdSubReg)
683 break;
684
685 for (auto &Op : MRI->use_nodbg_operands(FwdReg)) {
686 if (Op.getSubReg() == FwdSubReg)
687 Uses.push_back(&Op);
688 }
689 RegSeqWithOpNos[&OrigMI].push_back(OpNo);
690 continue;
691 }
692
693 bool IsShrinkable = isShrinkable(OrigMI);
694 if (!(IsShrinkable ||
695 ((TII->isVOP3P(OrigOp) || TII->isVOPC(OrigOp) ||
696 TII->isVOP3(OrigOp)) &&
697 ST->hasVOP3DPP()) ||
698 TII->isVOP1(OrigOp) || TII->isVOP2(OrigOp))) {
699 LLVM_DEBUG(dbgs() << " failed: not VOP1/2/3/3P/C\n");
700 break;
701 }
702 if (OrigMI.modifiesRegister(AMDGPU::EXEC, ST->getRegisterInfo())) {
703 LLVM_DEBUG(dbgs() << " failed: can't combine v_cmpx\n");
704 break;
705 }
706
707 auto *Src0 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src0);
708 auto *Src1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1);
709 if (Use != Src0 && !(Use == Src1 && OrigMI.isCommutable())) { // [1]
710 LLVM_DEBUG(dbgs() << " failed: no suitable operands\n");
711 break;
712 }
713
714 auto *Src2 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src2);
715 assert(Src0 && "Src1 without Src0?");
716 if ((Use == Src0 && ((Src1 && Src1->isIdenticalTo(*Src0)) ||
717 (Src2 && Src2->isIdenticalTo(*Src0)))) ||
718 (Use == Src1 && (Src1->isIdenticalTo(*Src0) ||
719 (Src2 && Src2->isIdenticalTo(*Src1))))) {
721 dbgs()
722 << " " << OrigMI
723 << " failed: DPP register is used more than once per instruction\n");
724 break;
725 }
726
727 if (!ST->hasFeature(AMDGPU::FeatureDPALU_DPP) &&
729 LLVM_DEBUG(dbgs() << " " << OrigMI
730 << " failed: DPP ALU DPP is not supported\n");
731 break;
732 }
733
734 if (!AMDGPU::isLegalDPALU_DPPControl(*ST, DppCtrlVal) &&
735 AMDGPU::isDPALU_DPP(TII->get(OrigOp), *ST)) {
736 LLVM_DEBUG(dbgs() << " " << OrigMI
737 << " failed: not valid 64-bit DPP control value\n");
738 break;
739 }
740
741 LLVM_DEBUG(dbgs() << " combining: " << OrigMI);
742 if (Use == Src0) {
743 if (auto *DPPInst = createDPPInst(OrigMI, MovMI, CombOldVGPR,
744 OldOpndValue, CombBCZ, IsShrinkable)) {
745 DPPMIs.push_back(DPPInst);
746 Rollback = false;
747 }
748 } else {
749 assert(Use == Src1 && OrigMI.isCommutable()); // by check [1]
750 auto *BB = OrigMI.getParent();
751 auto *NewMI = BB->getParent()->CloneMachineInstr(&OrigMI);
752 BB->insert(OrigMI, NewMI);
753 if (TII->commuteInstruction(*NewMI)) {
754 LLVM_DEBUG(dbgs() << " commuted: " << *NewMI);
755 if (auto *DPPInst =
756 createDPPInst(*NewMI, MovMI, CombOldVGPR, OldOpndValue, CombBCZ,
757 IsShrinkable)) {
758 DPPMIs.push_back(DPPInst);
759 Rollback = false;
760 }
761 } else
762 LLVM_DEBUG(dbgs() << " failed: cannot be commuted\n");
763 NewMI->eraseFromParent();
764 }
765 if (Rollback)
766 break;
767 OrigMIs.push_back(&OrigMI);
768 }
769
770 Rollback |= !Uses.empty();
771
772 for (auto *MI : *(Rollback? &DPPMIs : &OrigMIs))
773 MI->eraseFromParent();
774
775 if (!Rollback) {
776 for (auto &S : RegSeqWithOpNos) {
777 if (MRI->use_nodbg_empty(S.first->getOperand(0).getReg())) {
778 S.first->eraseFromParent();
779 continue;
780 }
781 while (!S.second.empty())
782 S.first->getOperand(S.second.pop_back_val()).setIsUndef();
783 }
784 }
785
786 return !Rollback;
787}
788
789bool GCNDPPCombineLegacy::runOnMachineFunction(MachineFunction &MF) {
790 if (skipFunction(MF.getFunction()))
791 return false;
792
793 return GCNDPPCombine().run(MF);
794}
795
796bool GCNDPPCombine::run(MachineFunction &MF) {
798 if (!ST->hasDPP())
799 return false;
800
801 MRI = &MF.getRegInfo();
802 TII = ST->getInstrInfo();
803
804 bool Changed = false;
805 for (auto &MBB : MF) {
807 if (MI.getOpcode() == AMDGPU::V_MOV_B32_dpp && combineDPPMov(MI)) {
808 Changed = true;
809 ++NumDPPMovsCombined;
810 } else if (MI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO ||
811 MI.getOpcode() == AMDGPU::V_MOV_B64_dpp) {
812 if (ST->hasDPALU_DPP() && combineDPPMov(MI)) {
813 Changed = true;
814 ++NumDPPMovsCombined;
815 } else {
816 auto Split = TII->expandMovDPP64(MI);
817 for (auto *M : {Split.first, Split.second}) {
818 if (M && combineDPPMov(*M))
819 ++NumDPPMovsCombined;
820 }
821 Changed = true;
822 }
823 }
824 }
825 }
826 return Changed;
827}
828
831 MFPropsModifier _(*this, MF);
832
833 if (MF.getFunction().hasOptNone())
834 return PreservedAnalyses::all();
835
836 bool Changed = GCNDPPCombine().run(MF);
837 if (!Changed)
838 return PreservedAnalyses::all();
839
841 PA.preserveSet<CFGAnalyses>();
842 return PA;
843}
unsigned const MachineRegisterInfo * MRI
#define Fail
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Provides AMDGPU specific target descriptions.
MachineBasicBlock & MBB
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
static bool isIdentityValue(unsigned OrigMIOp, MachineOperand *OldOpnd)
static unsigned getOperandSize(MachineInstr &MI, unsigned Idx, MachineRegisterInfo &MRI)
#define DEBUG_TYPE
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
TargetInstrInfo::RegSubRegPair RegSubRegPair
Register const TargetRegisterInfo * TRI
MachineInstr unsigned OpIdx
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
Remove Loads Into Fake Uses
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
#define LLVM_DEBUG(...)
Definition: Debug.h:119
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:270
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:73
This class represents an Operation in the Expression.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
bool hasOptNone() const
Do not optimize this function (-O0).
Definition: Function.h:700
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MAM)
unsigned getSize(const MachineInstr &MI) const
An RAII based helper class to modify MachineFunctionProperties when running pass.
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.
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:72
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:587
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:359
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:590
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,...
LLVM_ABI 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:511
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:595
uint32_t getFlags() const
Return the MI flags bitvector.
Definition: MachineInstr.h:404
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,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:85
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
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:78
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
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:35
LLVM Value Representation.
Definition: Value.h:75
LLVM_READONLY int getVOPe32(uint16_t Opcode)
LLVM_READONLY int getDPPOp32(uint16_t Opcode)
LLVM_READNONE bool isLegalDPALU_DPPControl(const MCSubtargetInfo &ST, unsigned DC)
LLVM_READONLY bool hasNamedOperand(uint64_t Opcode, OpName NamedIdx)
bool isDPALU_DPP32BitOpc(unsigned Opc)
bool isTrue16Inst(unsigned Opc)
bool isDPALU_DPP(const MCInstrDesc &OpDesc, const MCSubtargetInfo &ST)
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:126
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:1584
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2491
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:663
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:428
char & GCNDPPCombineLegacyID
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
MachineInstr * getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P, MachineRegisterInfo &MRI)
Return the defining instruction for a given reg:subreg pair skipping copy like instructions and subre...
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition: iterator.h:363
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:1572
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.