LLVM 24.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"
48
49using namespace llvm;
50
51#define DEBUG_TYPE "gcn-dpp-combine"
52
53STATISTIC(NumDPPMovsCombined, "Number of DPP moves combined.");
54
55namespace {
56
57class GCNDPPCombine {
59 const SIInstrInfo *TII;
60 const GCNSubtarget *ST;
61
63
64 MachineOperand *getOldOpndValue(MachineOperand &OldOpnd) const;
65
66 MachineInstr *createDPPInst(MachineInstr &OrigMI, MachineInstr &MovMI,
67 RegSubRegPair CombOldVGPR,
68 MachineOperand *OldOpnd, bool CombBCZ,
69 bool IsShrinkable) const;
70
71 MachineInstr *createDPPInst(MachineInstr &OrigMI, MachineInstr &MovMI,
72 RegSubRegPair CombOldVGPR, bool CombBCZ,
73 bool IsShrinkable) const;
74
75 bool hasNoImmOrEqual(MachineInstr &MI, AMDGPU::OpName OpndName, int64_t Value,
76 int64_t Mask = -1) const;
77
78 bool combineDPPMov(MachineInstr &MI) const;
79
80 int getDPPOp(unsigned Op, bool IsShrinkable) const;
81 bool isShrinkable(MachineInstr &MI) const;
82
83public:
84 bool run(MachineFunction &MF);
85};
86
87class GCNDPPCombineLegacy : public MachineFunctionPass {
88public:
89 static char ID;
90
91 GCNDPPCombineLegacy() : MachineFunctionPass(ID) {}
92
93 bool runOnMachineFunction(MachineFunction &MF) override;
94
95 StringRef getPassName() const override { return "GCN DPP Combine"; }
96
97 void getAnalysisUsage(AnalysisUsage &AU) const override {
98 AU.setPreservesCFG();
99 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
101 }
102
103 MachineFunctionProperties getRequiredProperties() const override {
104 return MachineFunctionProperties().setIsSSA();
105 }
106};
107
108} // end anonymous namespace
109
110INITIALIZE_PASS(GCNDPPCombineLegacy, DEBUG_TYPE, "GCN DPP Combine", false,
111 false)
112
113char GCNDPPCombineLegacy::ID = 0;
114
115char &llvm::GCNDPPCombineLegacyID = GCNDPPCombineLegacy::ID;
116
118 return new GCNDPPCombineLegacy();
119}
120
121bool GCNDPPCombine::isShrinkable(MachineInstr &MI) const {
122 unsigned Op = MI.getOpcode();
123 if (!TII->isVOP3(Op)) {
124 return false;
125 }
126 if (!TII->hasVALU32BitEncoding(Op)) {
127 LLVM_DEBUG(dbgs() << " Inst hasn't e32 equivalent\n");
128 return false;
129 }
130 // Do not shrink True16 instructions pre-RA to avoid the restriction in
131 // register allocation from only being able to use 128 VGPRs
133 return false;
134 if (const auto *SDst = TII->getNamedOperand(MI, AMDGPU::OpName::sdst)) {
135 // Give up if there are any uses of the sdst in carry-out or VOPC.
136 // The shrunken form of the instruction would write it to vcc instead of to
137 // a virtual register. If we rewrote the uses the shrinking would be
138 // possible.
139 if (!MRI->use_nodbg_empty(SDst->getReg()))
140 return false;
141 }
142 // check if other than abs|neg modifiers are set (opsel for example)
143 const int64_t Mask = ~(SISrcMods::ABS | SISrcMods::NEG);
144 if (!hasNoImmOrEqual(MI, AMDGPU::OpName::src0_modifiers, 0, Mask) ||
145 !hasNoImmOrEqual(MI, AMDGPU::OpName::src1_modifiers, 0, Mask) ||
146 !hasNoImmOrEqual(MI, AMDGPU::OpName::clamp, 0) ||
147 !hasNoImmOrEqual(MI, AMDGPU::OpName::omod, 0) ||
148 !hasNoImmOrEqual(MI, AMDGPU::OpName::byte_sel, 0)) {
149 LLVM_DEBUG(dbgs() << " Inst has non-default modifiers\n");
150 return false;
151 }
152 return true;
153}
154
155int GCNDPPCombine::getDPPOp(unsigned Op, bool IsShrinkable) const {
156 int DPP32 = AMDGPU::getDPPOp32(Op);
157 if (IsShrinkable) {
158 assert(DPP32 == -1);
159 int E32 = AMDGPU::getVOPe32(Op);
160 DPP32 = (E32 == -1) ? -1 : AMDGPU::getDPPOp32(E32);
161 }
162 if (DPP32 != -1 && TII->pseudoToMCOpcode(DPP32) != -1)
163 return DPP32;
164 int DPP64 = -1;
165 if (ST->hasVOP3DPP())
166 DPP64 = AMDGPU::getDPPOp64(Op);
167 if (DPP64 != -1 && TII->pseudoToMCOpcode(DPP64) != -1)
168 return DPP64;
169 return -1;
170}
171
172// tracks the register operand definition and returns:
173// 1. immediate operand used to initialize the register if found
174// 2. nullptr if the register operand is undef
175// 3. the operand itself otherwise
176MachineOperand *GCNDPPCombine::getOldOpndValue(MachineOperand &OldOpnd) const {
177 auto *Def = getVRegSubRegDef(getRegSubRegPair(OldOpnd), *MRI);
178 if (!Def)
179 return nullptr;
180
181 switch(Def->getOpcode()) {
182 default: break;
183 case AMDGPU::IMPLICIT_DEF:
184 return nullptr;
185 case AMDGPU::COPY:
186 case AMDGPU::V_MOV_B32_e32:
187 case AMDGPU::V_MOV_B64_PSEUDO:
188 case AMDGPU::V_MOV_B64_e32:
189 case AMDGPU::V_MOV_B64_e64: {
190 auto &Op1 = Def->getOperand(1);
191 if (Op1.isImm())
192 return &Op1;
193 break;
194 }
195 }
196 return &OldOpnd;
197}
198
199MachineInstr *GCNDPPCombine::createDPPInst(MachineInstr &OrigMI,
200 MachineInstr &MovMI,
201 RegSubRegPair CombOldVGPR,
202 bool CombBCZ,
203 bool IsShrinkable) const {
204 assert(MovMI.getOpcode() == AMDGPU::V_MOV_B32_dpp ||
205 MovMI.getOpcode() == AMDGPU::V_MOV_B64_dpp ||
206 MovMI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO);
207
208 bool HasVOP3DPP = ST->hasVOP3DPP();
209 auto OrigOp = OrigMI.getOpcode();
210 if (ST->useRealTrue16Insts() && AMDGPU::isTrue16Inst(OrigOp)) {
212 dbgs() << " failed: Did not expect any 16-bit uses of dpp values\n");
213 return nullptr;
214 }
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 (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::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, getUndefRegState(!Def),
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 [[maybe_unused]] int Src0Idx = NumOperands;
292
293 DPPInst.add(*Src0);
294 DPPInst->getOperand(NumOperands).setIsKill(false);
295 ++NumOperands;
296
297 auto *Mod1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1_modifiers);
298 if (Mod1) {
299 assert(NumOperands == AMDGPU::getNamedOperandIdx(DPPOp,
300 AMDGPU::OpName::src1_modifiers));
301 assert(HasVOP3DPP ||
302 (0LL == (Mod1->getImm() & ~(SISrcMods::ABS | SISrcMods::NEG))));
303 DPPInst.addImm(Mod1->getImm());
304 ++NumOperands;
305 } else if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::src1_modifiers)) {
306 DPPInst.addImm(0);
307 ++NumOperands;
308 }
309 auto *Src1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1);
310 if (Src1) {
311 assert(AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::src1) &&
312 "dpp version of instruction missing src1");
313
314 DPPInst.add(*Src1);
315 ++NumOperands;
316 }
317
318 auto *Mod2 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src2_modifiers);
319 if (Mod2) {
320 assert(NumOperands ==
321 AMDGPU::getNamedOperandIdx(DPPOp, AMDGPU::OpName::src2_modifiers));
322 assert(HasVOP3DPP ||
323 (0LL == (Mod2->getImm() & ~(SISrcMods::ABS | SISrcMods::NEG))));
324 DPPInst.addImm(Mod2->getImm());
325 ++NumOperands;
326 }
327 auto *Src2 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src2);
328 if (Src2) {
329 if (!AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::src2)) {
330 LLVM_DEBUG(dbgs() << " failed: dpp does not have src2\n");
331 Fail = true;
332 break;
333 }
334 DPPInst.add(*Src2);
335 ++NumOperands;
336 }
337
338 if (HasVOP3DPP) {
339 auto *ClampOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::clamp);
340 if (ClampOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::clamp)) {
341 DPPInst.addImm(ClampOpr->getImm());
342 }
343 auto *VdstInOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::vdst_in);
344 if (VdstInOpr &&
345 AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::vdst_in)) {
346 DPPInst.add(*VdstInOpr);
347 }
348 auto *OmodOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::omod);
349 if (OmodOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::omod)) {
350 DPPInst.addImm(OmodOpr->getImm());
351 }
352 // Validate OP_SEL has to be set to all 0 and OP_SEL_HI has to be set to
353 // all 1.
354 if (TII->getNamedOperand(OrigMI, AMDGPU::OpName::op_sel)) {
355 int64_t OpSel = 0;
356 OpSel |= (Mod0 ? (!!(Mod0->getImm() & SISrcMods::OP_SEL_0) << 0) : 0);
357 OpSel |= (Mod1 ? (!!(Mod1->getImm() & SISrcMods::OP_SEL_0) << 1) : 0);
358 OpSel |= (Mod2 ? (!!(Mod2->getImm() & SISrcMods::OP_SEL_0) << 2) : 0);
359 if (Mod0 && TII->isVOP3(OrigMI) && !TII->isVOP3P(OrigMI))
360 OpSel |= !!(Mod0->getImm() & SISrcMods::DST_OP_SEL) << 3;
361
362 if (OpSel != 0) {
363 LLVM_DEBUG(dbgs() << " failed: op_sel must be zero\n");
364 Fail = true;
365 break;
366 }
367 if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::op_sel))
368 DPPInst.addImm(OpSel);
369 }
370 if (TII->getNamedOperand(OrigMI, AMDGPU::OpName::op_sel_hi)) {
371 int64_t OpSelHi = 0;
372 OpSelHi |= (Mod0 ? (!!(Mod0->getImm() & SISrcMods::OP_SEL_1) << 0) : 0);
373 OpSelHi |= (Mod1 ? (!!(Mod1->getImm() & SISrcMods::OP_SEL_1) << 1) : 0);
374 OpSelHi |= (Mod2 ? (!!(Mod2->getImm() & SISrcMods::OP_SEL_1) << 2) : 0);
375
376 // Only vop3p has op_sel_hi, and all vop3p have 3 operands, so check
377 // the bitmask for 3 op_sel_hi bits set
378 assert(Src2 && "Expected vop3p with 3 operands");
379 if (OpSelHi != 7) {
380 LLVM_DEBUG(dbgs() << " failed: op_sel_hi must be all set to one\n");
381 Fail = true;
382 break;
383 }
384 if (AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::op_sel_hi))
385 DPPInst.addImm(OpSelHi);
386 }
387 auto *NegOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::neg_lo);
388 if (NegOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::neg_lo)) {
389 DPPInst.addImm(NegOpr->getImm());
390 }
391 auto *NegHiOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::neg_hi);
392 if (NegHiOpr && AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::neg_hi)) {
393 DPPInst.addImm(NegHiOpr->getImm());
394 }
395 auto *ByteSelOpr = TII->getNamedOperand(OrigMI, AMDGPU::OpName::byte_sel);
396 if (ByteSelOpr &&
397 AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::byte_sel)) {
398 DPPInst.addImm(ByteSelOpr->getImm());
399 }
400 if (MachineOperand *BitOp3 =
401 TII->getNamedOperand(OrigMI, AMDGPU::OpName::bitop3)) {
402 assert(AMDGPU::hasNamedOperand(DPPOp, AMDGPU::OpName::bitop3));
403 DPPInst.add(*BitOp3);
404 }
405 }
406 DPPInst.add(*TII->getNamedOperand(MovMI, AMDGPU::OpName::dpp_ctrl));
407 DPPInst.add(*TII->getNamedOperand(MovMI, AMDGPU::OpName::row_mask));
408 DPPInst.add(*TII->getNamedOperand(MovMI, AMDGPU::OpName::bank_mask));
409 DPPInst.addImm(CombBCZ ? 1 : 0);
410
411 constexpr AMDGPU::OpName Srcs[] = {
412 AMDGPU::OpName::src0, AMDGPU::OpName::src1, AMDGPU::OpName::src2};
413
414 // FIXME: isOperandLegal expects to operate on an completely built
415 // instruction. We should have better legality APIs to check if the
416 // candidate operands will be legal without building the instruction first.
417 for (auto [I, OpName] : enumerate(Srcs)) {
418 int OpIdx = AMDGPU::getNamedOperandIdx(DPPOp, OpName);
419 if (OpIdx == -1)
420 break;
421
422 if (!TII->isOperandLegal(*DPPInst, OpIdx)) {
423 LLVM_DEBUG(dbgs() << " failed: src" << I << " operand is illegal\n");
424 Fail = true;
425 break;
426 }
427 }
428 } while (false);
429
430 if (Fail) {
431 DPPInst.getInstr()->eraseFromParent();
432 return nullptr;
433 }
434 LLVM_DEBUG(dbgs() << " combined: " << *DPPInst.getInstr());
435 return DPPInst.getInstr();
436}
437
438static bool isIdentityValue(unsigned OrigMIOp, MachineOperand *OldOpnd) {
439 assert(OldOpnd->isImm());
440 switch (OrigMIOp) {
441 default: break;
442 case AMDGPU::V_ADD_U32_e32:
443 case AMDGPU::V_ADD_U32_e64:
444 case AMDGPU::V_ADD_CO_U32_e32:
445 case AMDGPU::V_ADD_CO_U32_e64:
446 case AMDGPU::V_OR_B32_e32:
447 case AMDGPU::V_OR_B32_e64:
448 case AMDGPU::V_SUBREV_U32_e32:
449 case AMDGPU::V_SUBREV_U32_e64:
450 case AMDGPU::V_SUBREV_CO_U32_e32:
451 case AMDGPU::V_SUBREV_CO_U32_e64:
452 case AMDGPU::V_MAX_U32_e32:
453 case AMDGPU::V_MAX_U32_e64:
454 case AMDGPU::V_XOR_B32_e32:
455 case AMDGPU::V_XOR_B32_e64:
456 if (OldOpnd->getImm() == 0)
457 return true;
458 break;
459 case AMDGPU::V_AND_B32_e32:
460 case AMDGPU::V_AND_B32_e64:
461 case AMDGPU::V_MIN_U32_e32:
462 case AMDGPU::V_MIN_U32_e64:
463 if (static_cast<uint32_t>(OldOpnd->getImm()) ==
464 std::numeric_limits<uint32_t>::max())
465 return true;
466 break;
467 case AMDGPU::V_MIN_I32_e32:
468 case AMDGPU::V_MIN_I32_e64:
469 if (static_cast<int32_t>(OldOpnd->getImm()) ==
470 std::numeric_limits<int32_t>::max())
471 return true;
472 break;
473 case AMDGPU::V_MAX_I32_e32:
474 case AMDGPU::V_MAX_I32_e64:
475 if (static_cast<int32_t>(OldOpnd->getImm()) ==
476 std::numeric_limits<int32_t>::min())
477 return true;
478 break;
479 case AMDGPU::V_MUL_I32_I24_e32:
480 case AMDGPU::V_MUL_I32_I24_e64:
481 case AMDGPU::V_MUL_U32_U24_e32:
482 case AMDGPU::V_MUL_U32_U24_e64:
483 if (OldOpnd->getImm() == 1)
484 return true;
485 break;
486 case AMDGPU::V_MIN_F32_e32:
487 case AMDGPU::V_MIN_F32_e64:
488 if (static_cast<uint32_t>(OldOpnd->getImm()) == /*+inf=*/0x7F800000)
489 return true;
490 break;
491 case AMDGPU::V_MAX_F32_e32:
492 case AMDGPU::V_MAX_F32_e64:
493 if (static_cast<uint32_t>(OldOpnd->getImm()) == /*-inf=*/0xFF800000)
494 return true;
495 break;
496 case AMDGPU::V_MIN_F64_e64:
497 case AMDGPU::V_MIN_NUM_F64_e64:
498 if (static_cast<uint64_t>(OldOpnd->getImm()) == /*+inf=*/0x7FF0000000000000)
499 return true;
500 break;
501 case AMDGPU::V_MAX_F64_e64:
502 case AMDGPU::V_MAX_NUM_F64_e64:
503 if (static_cast<uint64_t>(OldOpnd->getImm()) == /*-inf=*/0xFFF0000000000000)
504 return true;
505 break;
506 case AMDGPU::V_MIN_F16_e32:
507 case AMDGPU::V_MIN_F16_e64:
508 case AMDGPU::V_MIN_F16_t16_e32:
509 case AMDGPU::V_MIN_F16_t16_e64:
510 case AMDGPU::V_MIN_F16_fake16_e32:
511 case AMDGPU::V_MIN_F16_fake16_e64:
512 if (static_cast<uint16_t>(OldOpnd->getImm()) == /*+inf=*/0x7C00)
513 return true;
514 break;
515 case AMDGPU::V_MAX_F16_e32:
516 case AMDGPU::V_MAX_F16_e64:
517 case AMDGPU::V_MAX_F16_t16_e32:
518 case AMDGPU::V_MAX_F16_t16_e64:
519 case AMDGPU::V_MAX_F16_fake16_e32:
520 case AMDGPU::V_MAX_F16_fake16_e64:
521 if (static_cast<uint16_t>(OldOpnd->getImm()) == /*-inf=*/0xFC00)
522 return true;
523 break;
524 }
525 return false;
526}
527
528MachineInstr *GCNDPPCombine::createDPPInst(
529 MachineInstr &OrigMI, MachineInstr &MovMI, RegSubRegPair CombOldVGPR,
530 MachineOperand *OldOpndValue, bool CombBCZ, bool IsShrinkable) const {
531 assert(CombOldVGPR.Reg);
532 if (!CombBCZ && OldOpndValue && OldOpndValue->isImm()) {
533 auto *Src1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1);
534 if (!Src1 || !Src1->isReg()) {
535 LLVM_DEBUG(dbgs() << " failed: no src1 or it isn't a register\n");
536 return nullptr;
537 }
538 if (!isIdentityValue(OrigMI.getOpcode(), OldOpndValue)) {
539 LLVM_DEBUG(dbgs() << " failed: old immediate isn't an identity\n");
540 return nullptr;
541 }
542 CombOldVGPR = getRegSubRegPair(*Src1);
543 auto *MovDst = TII->getNamedOperand(MovMI, AMDGPU::OpName::vdst);
544 const TargetRegisterClass *RC = MRI->getRegClass(MovDst->getReg());
545 if (!isOfRegClass(CombOldVGPR, *RC, *MRI)) {
546 LLVM_DEBUG(dbgs() << " failed: src1 has wrong register class\n");
547 return nullptr;
548 }
549 }
550 return createDPPInst(OrigMI, MovMI, CombOldVGPR, CombBCZ, IsShrinkable);
551}
552
553// returns true if MI doesn't have OpndName immediate operand or the
554// operand has Value
555bool GCNDPPCombine::hasNoImmOrEqual(MachineInstr &MI, AMDGPU::OpName OpndName,
556 int64_t Value, int64_t Mask) const {
557 auto *Imm = TII->getNamedOperand(MI, OpndName);
558 if (!Imm)
559 return true;
560
561 assert(Imm->isImm());
562 return (Imm->getImm() & Mask) == Value;
563}
564
565bool GCNDPPCombine::combineDPPMov(MachineInstr &MovMI) const {
566 assert(MovMI.getOpcode() == AMDGPU::V_MOV_B32_dpp ||
567 MovMI.getOpcode() == AMDGPU::V_MOV_B64_dpp ||
568 MovMI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO);
569 LLVM_DEBUG(dbgs() << "\nDPP combine: " << MovMI);
570
571 auto *DstOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::vdst);
572 assert(DstOpnd && DstOpnd->isReg());
573 auto DPPMovReg = DstOpnd->getReg();
574 if (DPPMovReg.isPhysical()) {
575 LLVM_DEBUG(dbgs() << " failed: dpp move writes physreg\n");
576 return false;
577 }
578 if (execMayBeModifiedBeforeAnyUse(*MRI, DPPMovReg, MovMI)) {
579 LLVM_DEBUG(dbgs() << " failed: EXEC mask should remain the same"
580 " for all uses\n");
581 return false;
582 }
583
584 auto *DppCtrl = TII->getNamedOperand(MovMI, AMDGPU::OpName::dpp_ctrl);
585 assert(DppCtrl && DppCtrl->isImm());
586 unsigned DppCtrlVal = DppCtrl->getImm();
587 if ((MovMI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO ||
588 MovMI.getOpcode() == AMDGPU::V_MOV_B64_dpp)) {
589 if (!ST->hasFeature(AMDGPU::FeatureDPALU_DPP)) {
590 LLVM_DEBUG(dbgs() << " failed: 64 bit dpp move is unsupported\n");
591 // Split it.
592 return false;
593 }
594 if (!AMDGPU::isLegalDPALU_DPPControl(*ST, DppCtrlVal)) {
595 LLVM_DEBUG(dbgs() << " failed: 64 bit dpp move uses unsupported"
596 " control value\n");
597 // Let it split, then control may become legal.
598 return false;
599 }
600 }
601
602 auto *RowMaskOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::row_mask);
603 assert(RowMaskOpnd && RowMaskOpnd->isImm());
604 auto *BankMaskOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::bank_mask);
605 assert(BankMaskOpnd && BankMaskOpnd->isImm());
606 const bool MaskAllLanes = RowMaskOpnd->getImm() == 0xF &&
607 BankMaskOpnd->getImm() == 0xF;
608
609 auto *BCZOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::bound_ctrl);
610 assert(BCZOpnd && BCZOpnd->isImm());
611 bool BoundCtrlZero = BCZOpnd->getImm();
612
613 auto *OldOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::old);
614 auto *SrcOpnd = TII->getNamedOperand(MovMI, AMDGPU::OpName::src0);
615 assert(OldOpnd && OldOpnd->isReg());
616 assert(SrcOpnd && SrcOpnd->isReg());
617 if (OldOpnd->getReg().isPhysical() || SrcOpnd->getReg().isPhysical()) {
618 LLVM_DEBUG(dbgs() << " failed: dpp move reads physreg\n");
619 return false;
620 }
621
622 auto * const OldOpndValue = getOldOpndValue(*OldOpnd);
623 // OldOpndValue is either undef (IMPLICIT_DEF) or immediate or something else
624 // We could use: assert(!OldOpndValue || OldOpndValue->isImm())
625 // but the third option is used to distinguish undef from non-immediate
626 // to reuse IMPLICIT_DEF instruction later
627 assert(!OldOpndValue || OldOpndValue->isImm() || OldOpndValue == OldOpnd);
628
629 bool CombBCZ = false;
630
631 if (MaskAllLanes && BoundCtrlZero) { // [1]
632 CombBCZ = true;
633 } else {
634 if (!OldOpndValue || !OldOpndValue->isImm()) {
635 LLVM_DEBUG(dbgs() << " failed: the DPP mov isn't combinable\n");
636 return false;
637 }
638
639 if (OldOpndValue->getImm() == 0) {
640 if (MaskAllLanes) {
641 assert(!BoundCtrlZero); // by check [1]
642 CombBCZ = true;
643 }
644 } else if (BoundCtrlZero) {
645 assert(!MaskAllLanes); // by check [1]
646 LLVM_DEBUG(dbgs() <<
647 " failed: old!=0 and bctrl:0 and not all lanes isn't combinable\n");
648 return false;
649 }
650 }
651
652 LLVM_DEBUG(dbgs() << " old=";
653 if (!OldOpndValue)
654 dbgs() << "undef";
655 else
656 dbgs() << *OldOpndValue;
657 dbgs() << ", bound_ctrl=" << CombBCZ << '\n');
658
659 SmallVector<MachineInstr*, 4> OrigMIs, DPPMIs;
660 DenseMap<MachineInstr*, SmallVector<unsigned, 4>> RegSeqWithOpNos;
661 auto CombOldVGPR = getRegSubRegPair(*OldOpnd);
662 // try to reuse previous old reg if its undefined (IMPLICIT_DEF)
663 if (CombBCZ && OldOpndValue) { // CombOldVGPR should be undef
664 const TargetRegisterClass *RC = MRI->getRegClass(DPPMovReg);
665 CombOldVGPR = RegSubRegPair(
666 MRI->createVirtualRegister(RC));
667 auto UndefInst = BuildMI(*MovMI.getParent(), MovMI, MovMI.getDebugLoc(),
668 TII->get(AMDGPU::IMPLICIT_DEF), CombOldVGPR.Reg);
669 DPPMIs.push_back(UndefInst.getInstr());
670 }
671
672 OrigMIs.push_back(&MovMI);
673 bool Rollback = true;
676
677 while (!Uses.empty()) {
678 MachineOperand *Use = Uses.pop_back_val();
679 Rollback = true;
680
681 auto &OrigMI = *Use->getParent();
682 LLVM_DEBUG(dbgs() << " try: " << OrigMI);
683
684 auto OrigOp = OrigMI.getOpcode();
685 assert((TII->get(OrigOp).getSize() != 4 || !AMDGPU::isTrue16Inst(OrigOp)) &&
686 "There should not be e32 True16 instructions pre-RA");
687 if (OrigOp == AMDGPU::REG_SEQUENCE) {
688 Register FwdReg = OrigMI.getOperand(0).getReg();
689 unsigned FwdSubReg = 0;
690
691 if (execMayBeModifiedBeforeAnyUse(*MRI, FwdReg, OrigMI)) {
692 LLVM_DEBUG(dbgs() << " failed: EXEC mask should remain the same"
693 " for all uses\n");
694 break;
695 }
696
697 unsigned OpNo, E = OrigMI.getNumOperands();
698 for (OpNo = 1; OpNo < E; OpNo += 2) {
699 if (OrigMI.getOperand(OpNo).getReg() == DPPMovReg) {
700 FwdSubReg = OrigMI.getOperand(OpNo + 1).getImm();
701 break;
702 }
703 }
704
705 if (!FwdSubReg)
706 break;
707
708 for (auto &Op : MRI->use_nodbg_operands(FwdReg)) {
709 if (Op.getSubReg() == FwdSubReg)
710 Uses.push_back(&Op);
711 }
712 RegSeqWithOpNos[&OrigMI].push_back(OpNo);
713 continue;
714 }
715
716 bool IsShrinkable = isShrinkable(OrigMI);
717 if (!(IsShrinkable ||
718 ((TII->isVOP3P(OrigOp) || TII->isVOPC(OrigOp) ||
719 TII->isVOP3(OrigOp)) &&
720 ST->hasVOP3DPP()) ||
721 TII->isVOP1(OrigOp) || TII->isVOP2(OrigOp))) {
722 LLVM_DEBUG(dbgs() << " failed: not VOP1/2/3/3P/C\n");
723 break;
724 }
725 if (OrigMI.modifiesRegister(AMDGPU::EXEC, ST->getRegisterInfo())) {
726 LLVM_DEBUG(dbgs() << " failed: can't combine v_cmpx\n");
727 break;
728 }
729
730 auto *Src0 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src0);
731 auto *Src1 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src1);
732 if (Use != Src0 && !(Use == Src1 && OrigMI.isCommutable())) { // [1]
733 LLVM_DEBUG(dbgs() << " failed: no suitable operands\n");
734 break;
735 }
736
737 auto *Src2 = TII->getNamedOperand(OrigMI, AMDGPU::OpName::src2);
738 assert(Src0 && "Src1 without Src0?");
739 if ((Use == Src0 && ((Src1 && Src1->isIdenticalTo(*Src0)) ||
740 (Src2 && Src2->isIdenticalTo(*Src0)))) ||
741 (Use == Src1 && (Src1->isIdenticalTo(*Src0) ||
742 (Src2 && Src2->isIdenticalTo(*Src1))))) {
744 dbgs()
745 << " " << OrigMI
746 << " failed: DPP register is used more than once per instruction\n");
747 break;
748 }
749
750 if (!ST->hasFeature(AMDGPU::FeatureDPALU_DPP) &&
752 LLVM_DEBUG(dbgs() << " " << OrigMI
753 << " failed: DPP ALU DPP is not supported\n");
754 break;
755 }
756
757 if (!AMDGPU::isLegalDPALU_DPPControl(*ST, DppCtrlVal) &&
758 AMDGPU::isDPALU_DPP(TII->get(OrigOp), *TII, *ST)) {
759 LLVM_DEBUG(dbgs() << " " << OrigMI
760 << " failed: not valid 64-bit DPP control value\n");
761 break;
762 }
763
764 LLVM_DEBUG(dbgs() << " combining: " << OrigMI);
765 if (Use == Src0) {
766 if (auto *DPPInst = createDPPInst(OrigMI, MovMI, CombOldVGPR,
767 OldOpndValue, CombBCZ, IsShrinkable)) {
768 DPPMIs.push_back(DPPInst);
769 Rollback = false;
770 }
771 } else {
772 assert(Use == Src1 && OrigMI.isCommutable()); // by check [1]
773 auto *BB = OrigMI.getParent();
774 auto *NewMI = BB->getParent()->CloneMachineInstr(&OrigMI);
775 BB->insert(OrigMI, NewMI);
776 if (TII->commuteInstruction(*NewMI)) {
777 LLVM_DEBUG(dbgs() << " commuted: " << *NewMI);
778 if (auto *DPPInst =
779 createDPPInst(*NewMI, MovMI, CombOldVGPR, OldOpndValue, CombBCZ,
780 IsShrinkable)) {
781 DPPMIs.push_back(DPPInst);
782 Rollback = false;
783 }
784 } else
785 LLVM_DEBUG(dbgs() << " failed: cannot be commuted\n");
786 NewMI->eraseFromParent();
787 }
788 if (Rollback)
789 break;
790 OrigMIs.push_back(&OrigMI);
791 }
792
793 Rollback |= !Uses.empty();
794
795 for (auto *MI : *(Rollback? &DPPMIs : &OrigMIs))
796 MI->eraseFromParent();
797
798 if (!Rollback) {
799 for (auto &S : RegSeqWithOpNos) {
800 if (MRI->use_nodbg_empty(S.first->getOperand(0).getReg())) {
801 S.first->eraseFromParent();
802 continue;
803 }
804 while (!S.second.empty())
805 S.first->getOperand(S.second.pop_back_val()).setIsUndef();
806 }
807 }
808
809 return !Rollback;
810}
811
812bool GCNDPPCombineLegacy::runOnMachineFunction(MachineFunction &MF) {
813 if (skipFunction(MF.getFunction()))
814 return false;
815
816 return GCNDPPCombine().run(MF);
817}
818
819bool GCNDPPCombine::run(MachineFunction &MF) {
820 ST = &MF.getSubtarget<GCNSubtarget>();
821 if (!ST->hasDPP())
822 return false;
823
824 MRI = &MF.getRegInfo();
825 TII = ST->getInstrInfo();
826
827 bool Changed = false;
828 for (auto &MBB : MF) {
829 for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(MBB))) {
830 if (MI.getOpcode() == AMDGPU::V_MOV_B32_dpp && combineDPPMov(MI)) {
831 Changed = true;
832 ++NumDPPMovsCombined;
833 } else if (MI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO ||
834 MI.getOpcode() == AMDGPU::V_MOV_B64_dpp) {
835 if (ST->hasDPALU_DPP() && combineDPPMov(MI)) {
836 Changed = true;
837 ++NumDPPMovsCombined;
838 } else {
839 auto Split = TII->expandMovDPP64(MI);
840 for (auto *M : {Split.first, Split.second}) {
841 if (M && combineDPPMov(*M))
842 ++NumDPPMovsCombined;
843 }
844 Changed = true;
845 }
846 }
847 }
848 }
849 return Changed;
850}
851
854 MFPropsModifier _(*this, MF);
855
856 if (MF.getFunction().hasOptNone())
857 return PreservedAnalyses::all();
858
859 bool Changed = GCNDPPCombine().run(MF);
860 if (!Changed)
861 return PreservedAnalyses::all();
862
864 PA.preserveSet<CFGAnalyses>();
865 return PA;
866}
#define Fail
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Provides AMDGPU specific target descriptions.
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool isIdentityValue(unsigned OrigMIOp, MachineOperand *OldOpnd)
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
TargetInstrInfo::RegSubRegPair RegSubRegPair
Promote Memory to Register
Definition Mem2Reg.cpp:110
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:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
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:682
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MAM)
const SIInstrInfo * getInstrInfo() const override
const SIRegisterInfo * getRegisterInfo() const override
bool useRealTrue16Insts() const
Return true if real (non-fake) variants of True16 instructions using 16-bit registers should be code-...
bool hasVOP3DPP() const
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.
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.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
unsigned getNumOperands() const
Retuns the total number of operands.
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,...
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
uint32_t getFlags() const
Return the MI flags bitvector.
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,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
iterator_range< use_nodbg_iterator > use_nodbg_operands(Register Reg) const
bool use_nodbg_empty(Register RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
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
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
LLVM Value Representation.
Definition Value.h:75
Changed
LLVM_READONLY int32_t getDPPOp32(uint32_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)
LLVM_READONLY int32_t getVOPe32(uint32_t Opcode)
LLVM_READONLY int32_t getDPPOp64(uint32_t Opcode)
bool isDPALU_DPP(const MCInstrDesc &OpDesc, const MCInstrInfo &MII, const MCSubtargetInfo &ST)
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
This is an optimization pass for GlobalISel generic memory operations.
TargetInstrInfo::RegSubRegPair getRegSubRegPair(const MachineOperand &O)
Create RegSubRegPair from a register MachineOperand.
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:2554
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:633
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
MachineInstr * getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P, const MachineRegisterInfo &MRI)
Return the defining instruction for a given reg:subreg pair skipping copy like instructions and subre...
char & GCNDPPCombineLegacyID
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
DWARFExpression::Operation Op
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:368
bool isOfRegClass(const TargetInstrInfo::RegSubRegPair &P, const TargetRegisterClass &TRC, MachineRegisterInfo &MRI)
Returns true if a reg:subreg pair P has a TRC class.
FunctionPass * createGCNDPPCombinePass()
constexpr RegState getUndefRegState(bool B)
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.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
A pair composed of a register and a sub-register index.