LLVM 24.0.0git
SIOptimizeExecMasking.cpp
Go to the documentation of this file.
1//===-- SIOptimizeExecMasking.cpp -----------------------------------------===//
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
10#include "AMDGPU.h"
11#include "AMDGPULaneMaskUtils.h"
12#include "GCNSubtarget.h"
13#include "SIRegisterInfo.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "si-optimize-exec-masking"
24
25namespace {
26
27class SIOptimizeExecMasking {
28public:
29 SIOptimizeExecMasking(MachineFunction *MF)
30 : MF(MF), ST(&MF->getSubtarget<GCNSubtarget>()), TII(ST->getInstrInfo()),
31 TRI(&TII->getRegisterInfo()), MRI(&MF->getRegInfo()),
33 bool run();
34
35private:
37 const GCNSubtarget *ST;
38 const SIInstrInfo *TII;
39 const SIRegisterInfo *TRI;
42
45
46 Register isCopyFromExec(const MachineInstr &MI) const;
47 Register isCopyToExec(const MachineInstr &MI) const;
48 bool removeTerminatorBit(MachineInstr &MI) const;
50 fixTerminators(MachineBasicBlock &MBB) const;
52 findExecCopy(MachineBasicBlock &MBB,
54 bool isRegisterInUseBetween(MachineInstr &Stop, MachineInstr &Start,
55 MCRegister Reg, bool UseLiveOuts = false,
56 bool IgnoreStart = false) const;
57 bool isRegisterInUseAfter(MachineInstr &Stop, MCRegister Reg) const;
58 MachineInstr *findInstrBackwards(
59 MachineInstr &Origin, std::function<bool(MachineInstr *)> Pred,
60 ArrayRef<MCRegister> NonModifiableRegs,
61 unsigned MaxInstructions = 20) const;
62 bool optimizeExecSequence();
63 bool blocksAndN2Sink(const MachineInstr &MI, Register Dst) const;
64 bool optimizeAndN2WrExecSequence(MachineInstr &CopyToExecInst,
65 Register Dst) const;
66 void tryRecordVCmpxAndSaveexecSequence(MachineInstr &MI);
67 bool optimizeVCMPSaveExecSequence(MachineInstr &SaveExecInstr,
68 MachineInstr &VCmp) const;
69
70 void tryRecordOrSaveexecXorSequence(MachineInstr &MI);
71 bool optimizeOrSaveexecXorSequences();
72};
73
74class SIOptimizeExecMaskingLegacy : public MachineFunctionPass {
75public:
76 static char ID;
77
78 SIOptimizeExecMaskingLegacy() : MachineFunctionPass(ID) {}
79
80 bool runOnMachineFunction(MachineFunction &MF) override;
81
82 StringRef getPassName() const override {
83 return "SI optimize exec mask operations";
84 }
85
86 void getAnalysisUsage(AnalysisUsage &AU) const override {
87 AU.setPreservesCFG();
89 }
90};
91
92} // End anonymous namespace.
93
97 SIOptimizeExecMasking Impl(&MF);
98
99 if (!Impl.run())
100 return PreservedAnalyses::all();
101
103 PA.preserveSet<CFGAnalyses>();
104 return PA;
105}
106
107INITIALIZE_PASS_BEGIN(SIOptimizeExecMaskingLegacy, DEBUG_TYPE,
108 "SI optimize exec mask operations", false, false)
110INITIALIZE_PASS_END(SIOptimizeExecMaskingLegacy, DEBUG_TYPE,
111 "SI optimize exec mask operations", false, false)
112
113char SIOptimizeExecMaskingLegacy::ID = 0;
114
115char &llvm::SIOptimizeExecMaskingLegacyID = SIOptimizeExecMaskingLegacy::ID;
116
117/// If \p MI is a copy from exec, return the register copied to.
118Register SIOptimizeExecMasking::isCopyFromExec(const MachineInstr &MI) const {
119 switch (MI.getOpcode()) {
120 case AMDGPU::COPY:
121 case AMDGPU::S_MOV_B64:
122 case AMDGPU::S_MOV_B64_term:
123 case AMDGPU::S_MOV_B32:
124 case AMDGPU::S_MOV_B32_term: {
125 const MachineOperand &Src = MI.getOperand(1);
126 if (Src.isReg() && Src.getReg() == LMC.ExecReg)
127 return MI.getOperand(0).getReg();
128 }
129 }
130
131 return AMDGPU::NoRegister;
132}
133
134/// If \p MI is a copy to exec, return the register copied from.
135Register SIOptimizeExecMasking::isCopyToExec(const MachineInstr &MI) const {
136 switch (MI.getOpcode()) {
137 case AMDGPU::COPY:
138 case AMDGPU::S_MOV_B64:
139 case AMDGPU::S_MOV_B32: {
140 const MachineOperand &Dst = MI.getOperand(0);
141 if (Dst.isReg() && Dst.getReg() == LMC.ExecReg && MI.getOperand(1).isReg())
142 return MI.getOperand(1).getReg();
143 break;
144 }
145 case AMDGPU::S_MOV_B64_term:
146 case AMDGPU::S_MOV_B32_term:
147 llvm_unreachable("should have been replaced");
148 }
149
150 return Register();
151}
152
153/// If \p MI is a logical operation on an exec value,
154/// return the register copied to.
156 switch (MI.getOpcode()) {
157 case AMDGPU::S_AND_B64:
158 case AMDGPU::S_OR_B64:
159 case AMDGPU::S_XOR_B64:
160 case AMDGPU::S_ANDN2_B64:
161 case AMDGPU::S_ORN2_B64:
162 case AMDGPU::S_NAND_B64:
163 case AMDGPU::S_NOR_B64:
164 case AMDGPU::S_XNOR_B64: {
165 const MachineOperand &Src1 = MI.getOperand(1);
166 if (Src1.isReg() && Src1.getReg() == AMDGPU::EXEC)
167 return MI.getOperand(0).getReg();
168 const MachineOperand &Src2 = MI.getOperand(2);
169 if (Src2.isReg() && Src2.getReg() == AMDGPU::EXEC)
170 return MI.getOperand(0).getReg();
171 break;
172 }
173 case AMDGPU::S_AND_B32:
174 case AMDGPU::S_OR_B32:
175 case AMDGPU::S_XOR_B32:
176 case AMDGPU::S_ANDN2_B32:
177 case AMDGPU::S_ORN2_B32:
178 case AMDGPU::S_NAND_B32:
179 case AMDGPU::S_NOR_B32:
180 case AMDGPU::S_XNOR_B32: {
181 const MachineOperand &Src1 = MI.getOperand(1);
182 if (Src1.isReg() && Src1.getReg() == AMDGPU::EXEC_LO)
183 return MI.getOperand(0).getReg();
184 const MachineOperand &Src2 = MI.getOperand(2);
185 if (Src2.isReg() && Src2.getReg() == AMDGPU::EXEC_LO)
186 return MI.getOperand(0).getReg();
187 break;
188 }
189 }
190
191 return AMDGPU::NoRegister;
192}
193
194static unsigned getSaveExecOp(unsigned Opc) {
195 switch (Opc) {
196 case AMDGPU::S_AND_B64:
197 return AMDGPU::S_AND_SAVEEXEC_B64;
198 case AMDGPU::S_OR_B64:
199 return AMDGPU::S_OR_SAVEEXEC_B64;
200 case AMDGPU::S_XOR_B64:
201 return AMDGPU::S_XOR_SAVEEXEC_B64;
202 case AMDGPU::S_ANDN2_B64:
203 return AMDGPU::S_ANDN2_SAVEEXEC_B64;
204 case AMDGPU::S_ORN2_B64:
205 return AMDGPU::S_ORN2_SAVEEXEC_B64;
206 case AMDGPU::S_NAND_B64:
207 return AMDGPU::S_NAND_SAVEEXEC_B64;
208 case AMDGPU::S_NOR_B64:
209 return AMDGPU::S_NOR_SAVEEXEC_B64;
210 case AMDGPU::S_XNOR_B64:
211 return AMDGPU::S_XNOR_SAVEEXEC_B64;
212 case AMDGPU::S_AND_B32:
213 return AMDGPU::S_AND_SAVEEXEC_B32;
214 case AMDGPU::S_OR_B32:
215 return AMDGPU::S_OR_SAVEEXEC_B32;
216 case AMDGPU::S_XOR_B32:
217 return AMDGPU::S_XOR_SAVEEXEC_B32;
218 case AMDGPU::S_ANDN2_B32:
219 return AMDGPU::S_ANDN2_SAVEEXEC_B32;
220 case AMDGPU::S_ORN2_B32:
221 return AMDGPU::S_ORN2_SAVEEXEC_B32;
222 case AMDGPU::S_NAND_B32:
223 return AMDGPU::S_NAND_SAVEEXEC_B32;
224 case AMDGPU::S_NOR_B32:
225 return AMDGPU::S_NOR_SAVEEXEC_B32;
226 case AMDGPU::S_XNOR_B32:
227 return AMDGPU::S_XNOR_SAVEEXEC_B32;
228 default:
229 return AMDGPU::INSTRUCTION_LIST_END;
230 }
231}
232
233// These are only terminators to get correct spill code placement during
234// register allocation, so turn them back into normal instructions.
235bool SIOptimizeExecMasking::removeTerminatorBit(MachineInstr &MI) const {
236 switch (MI.getOpcode()) {
237 case AMDGPU::S_MOV_B32_term: {
238 bool RegSrc = MI.getOperand(1).isReg();
239 MI.setDesc(TII->get(RegSrc ? AMDGPU::COPY : AMDGPU::S_MOV_B32));
240 return true;
241 }
242 case AMDGPU::S_MOV_B64_term: {
243 bool RegSrc = MI.getOperand(1).isReg();
244 MI.setDesc(TII->get(RegSrc ? AMDGPU::COPY : AMDGPU::S_MOV_B64));
245 return true;
246 }
247 case AMDGPU::S_XOR_B64_term: {
248 // This is only a terminator to get the correct spill code placement during
249 // register allocation.
250 MI.setDesc(TII->get(AMDGPU::S_XOR_B64));
251 return true;
252 }
253 case AMDGPU::S_XOR_B32_term: {
254 // This is only a terminator to get the correct spill code placement during
255 // register allocation.
256 MI.setDesc(TII->get(AMDGPU::S_XOR_B32));
257 return true;
258 }
259 case AMDGPU::S_OR_B64_term: {
260 // This is only a terminator to get the correct spill code placement during
261 // register allocation.
262 MI.setDesc(TII->get(AMDGPU::S_OR_B64));
263 return true;
264 }
265 case AMDGPU::S_OR_B32_term: {
266 // This is only a terminator to get the correct spill code placement during
267 // register allocation.
268 MI.setDesc(TII->get(AMDGPU::S_OR_B32));
269 return true;
270 }
271 case AMDGPU::S_ANDN2_B64_term: {
272 // This is only a terminator to get the correct spill code placement during
273 // register allocation.
274 MI.setDesc(TII->get(AMDGPU::S_ANDN2_B64));
275 return true;
276 }
277 case AMDGPU::S_ANDN2_B32_term: {
278 // This is only a terminator to get the correct spill code placement during
279 // register allocation.
280 MI.setDesc(TII->get(AMDGPU::S_ANDN2_B32));
281 return true;
282 }
283 case AMDGPU::S_AND_B64_term: {
284 // This is only a terminator to get the correct spill code placement during
285 // register allocation.
286 MI.setDesc(TII->get(AMDGPU::S_AND_B64));
287 return true;
288 }
289 case AMDGPU::S_AND_B32_term: {
290 // This is only a terminator to get the correct spill code placement during
291 // register allocation.
292 MI.setDesc(TII->get(AMDGPU::S_AND_B32));
293 return true;
294 }
295 case AMDGPU::V_CMPX_EQ_U32_nosdst_e32_term:
296 MI.setDesc(TII->get(AMDGPU::V_CMPX_EQ_U32_nosdst_e32));
297 return true;
298 case AMDGPU::V_CMPX_EQ_U64_nosdst_e32_term:
299 MI.setDesc(TII->get(AMDGPU::V_CMPX_EQ_U64_nosdst_e32));
300 return true;
301 default:
302 return false;
303 }
304}
305
306// Turn all pseudoterminators in the block into their equivalent non-terminator
307// instructions. Returns the reverse iterator to the first non-terminator
308// instruction in the block.
310SIOptimizeExecMasking::fixTerminators(MachineBasicBlock &MBB) const {
312
313 bool Seen = false;
315 for (; I != E; ++I) {
316 if (!I->isTerminator())
317 return Seen ? FirstNonTerm : I;
318
319 if (removeTerminatorBit(*I)) {
320 if (!Seen) {
321 FirstNonTerm = I;
322 Seen = true;
323 }
324 }
325 }
326
327 return FirstNonTerm;
328}
329
330MachineBasicBlock::reverse_iterator SIOptimizeExecMasking::findExecCopy(
332 const unsigned InstLimit = 25;
333
334 auto E = MBB.rend();
335 for (unsigned N = 0; N <= InstLimit && I != E; ++I) {
336 if (I->isDebugInstr())
337 continue;
338 ++N;
339
340 Register CopyFromExec = isCopyFromExec(*I);
341 if (CopyFromExec.isValid())
342 return I;
343 }
344
345 return E;
346}
347
348// XXX - Seems LiveRegUnits doesn't work correctly since it will incorrectly
349// report the register as unavailable because a super-register with a lane mask
350// is unavailable.
351static bool isLiveOut(const MachineBasicBlock &MBB, unsigned Reg) {
352 for (MachineBasicBlock *Succ : MBB.successors()) {
353 if (Succ->isLiveIn(Reg))
354 return true;
355 }
356
357 return false;
358}
359
360// Backwards-iterate from Origin (for n=MaxInstructions iterations) until either
361// the beginning of the BB is reached or Pred evaluates to true - which can be
362// an arbitrary condition based on the current MachineInstr, for instance an
363// target instruction. Breaks prematurely by returning nullptr if one of the
364// registers given in NonModifiableRegs is modified by the current instruction.
365MachineInstr *SIOptimizeExecMasking::findInstrBackwards(
366 MachineInstr &Origin, std::function<bool(MachineInstr *)> Pred,
367 ArrayRef<MCRegister> NonModifiableRegs, unsigned MaxInstructions) const {
369 E = Origin.getParent()->rend();
370 unsigned CurrentIteration = 0;
371
372 for (++A; CurrentIteration < MaxInstructions && A != E; ++A) {
373 if (A->isDebugInstr())
374 continue;
375
376 if (Pred(&*A))
377 return &*A;
378
379 for (MCRegister Reg : NonModifiableRegs) {
380 if (A->modifiesRegister(Reg, TRI))
381 return nullptr;
382 }
383
384 ++CurrentIteration;
385 }
386
387 return nullptr;
388}
389
390// Determine if a register Reg is not re-defined and still in use
391// in the range (Stop..Start].
392// It does so by backwards calculating liveness from the end of the BB until
393// either Stop or the beginning of the BB is reached.
394// After liveness is calculated, we can determine if Reg is still in use and not
395// defined inbetween the instructions.
396bool SIOptimizeExecMasking::isRegisterInUseBetween(MachineInstr &Stop,
397 MachineInstr &Start,
399 bool UseLiveOuts,
400 bool IgnoreStart) const {
401 LiveRegUnits LR(*TRI);
402 if (UseLiveOuts)
403 LR.addLiveOuts(*Stop.getParent());
404
406
407 if (IgnoreStart)
408 ++A;
409
410 for (; A != Stop.getParent()->rend() && A != Stop; ++A) {
411 if (!A->isDebugInstr())
412 LR.stepBackward(*A);
413 }
414
415 return !LR.available(Reg) || MRI->isReserved(Reg);
416}
417
418// Determine if a register Reg is not re-defined and still in use
419// in the range (Stop..BB.end].
420bool SIOptimizeExecMasking::isRegisterInUseAfter(MachineInstr &Stop,
421 MCRegister Reg) const {
422 return isRegisterInUseBetween(Stop, *Stop.getParent()->rbegin(), Reg, true);
423}
424
425// Optimize sequences emitted for control flow lowering. They are originally
426// emitted as the separate operations because spill code may need to be
427// inserted for the saved copy of exec.
428//
429// x = copy exec
430// z = s_<op>_b64 x, y
431// exec = copy z
432// =>
433// x = s_<op>_saveexec_b64 y
434//
435bool SIOptimizeExecMasking::optimizeExecSequence() {
436 bool Changed = false;
437 for (MachineBasicBlock &MBB : *MF) {
438 MachineBasicBlock::reverse_iterator I = fixTerminators(MBB);
440 if (I == E)
441 continue;
442
443 // It's possible to see other terminator copies after the exec copy. This
444 // can happen if control flow pseudos had their outputs used by phis.
445 Register CopyToExec;
446
447 for (unsigned SearchCount = 0, SearchLimit = 5;
448 I != E && SearchCount < SearchLimit; ++I) {
449 if (I->isDebugInstr())
450 continue;
451 ++SearchCount;
452 CopyToExec = isCopyToExec(*I);
453 if (CopyToExec)
454 break;
455 }
456
457 if (!CopyToExec)
458 continue;
459
460 // Scan backwards to find the def.
461 auto *CopyToExecInst = &*I;
462 auto CopyFromExecInst = findExecCopy(MBB, I);
463 if (CopyFromExecInst == E) {
464 auto PrepareExecInst = next_nodbg(I, E);
465 if (PrepareExecInst == E)
466 continue;
467 // Fold exec = COPY (S_AND_B64 reg, exec) -> exec = S_AND_B64 reg, exec
468 if (CopyToExecInst->getOperand(1).isKill() &&
469 isLogicalOpOnExec(*PrepareExecInst) == CopyToExec) {
470 LLVM_DEBUG(dbgs() << "Fold exec copy: " << *PrepareExecInst);
471
472 PrepareExecInst->getOperand(0).setReg(LMC.ExecReg);
473
474 LLVM_DEBUG(dbgs() << "into: " << *PrepareExecInst << '\n');
475
476 CopyToExecInst->eraseFromParent();
477 Changed = true;
478 } else if (optimizeAndN2WrExecSequence(*CopyToExecInst, CopyToExec)) {
479 Changed = true;
480 }
481
482 continue;
483 }
484
485 if (isLiveOut(MBB, CopyToExec)) {
486 // The copied register is live out and has a second use in another block.
487 LLVM_DEBUG(dbgs() << "Exec copy source register is live out\n");
488 continue;
489 }
490
491 Register CopyFromExec = CopyFromExecInst->getOperand(0).getReg();
492 MachineInstr *SaveExecInst = nullptr;
493 SmallVector<MachineInstr *, 4> OtherUseInsts;
494
496 J = std::next(CopyFromExecInst->getIterator()),
497 JE = I->getIterator();
498 J != JE; ++J) {
499 if (J->isDebugInstr())
500 continue;
501
502 if (SaveExecInst && J->readsRegister(LMC.ExecReg, TRI)) {
503 LLVM_DEBUG(dbgs() << "exec read prevents saveexec: " << *J << '\n');
504 // Make sure this is inserted after any VALU ops that may have been
505 // scheduled in between.
506 SaveExecInst = nullptr;
507 break;
508 }
509
510 bool ReadsCopyFromExec = J->readsRegister(CopyFromExec, TRI);
511
512 if (J->modifiesRegister(CopyToExec, TRI)) {
513 if (SaveExecInst) {
514 LLVM_DEBUG(dbgs() << "Multiple instructions modify "
515 << printReg(CopyToExec, TRI) << '\n');
516 SaveExecInst = nullptr;
517 break;
518 }
519
520 unsigned SaveExecOp = getSaveExecOp(J->getOpcode());
521 if (SaveExecOp == AMDGPU::INSTRUCTION_LIST_END)
522 break;
523
524 if (ReadsCopyFromExec) {
525 SaveExecInst = &*J;
526 LLVM_DEBUG(dbgs() << "Found save exec op: " << *SaveExecInst << '\n');
527 continue;
528 }
529 LLVM_DEBUG(dbgs() << "Instruction does not read exec copy: " << *J
530 << '\n');
531 break;
532 }
533 if (ReadsCopyFromExec && !SaveExecInst) {
534 // Make sure no other instruction is trying to use this copy, before it
535 // will be rewritten by the saveexec, i.e. hasOneUse. There may have
536 // been another use, such as an inserted spill. For example:
537 //
538 // %sgpr0_sgpr1 = COPY %exec
539 // spill %sgpr0_sgpr1
540 // %sgpr2_sgpr3 = S_AND_B64 %sgpr0_sgpr1
541 //
542 LLVM_DEBUG(dbgs() << "Found second use of save inst candidate: " << *J
543 << '\n');
544 break;
545 }
546
547 if (SaveExecInst && J->readsRegister(CopyToExec, TRI)) {
548 assert(SaveExecInst != &*J);
549 OtherUseInsts.push_back(&*J);
550 }
551 }
552
553 if (!SaveExecInst)
554 continue;
555
556 LLVM_DEBUG(dbgs() << "Insert save exec op: " << *SaveExecInst << '\n');
557
558 MachineOperand &Src0 = SaveExecInst->getOperand(1);
559 MachineOperand &Src1 = SaveExecInst->getOperand(2);
560
561 MachineOperand *OtherOp = nullptr;
562
563 if (Src0.isReg() && Src0.getReg() == CopyFromExec) {
564 OtherOp = &Src1;
565 } else if (Src1.isReg() && Src1.getReg() == CopyFromExec) {
566 if (!SaveExecInst->isCommutable())
567 break;
568
569 OtherOp = &Src0;
570 } else
571 llvm_unreachable("unexpected");
572
573 CopyFromExecInst->eraseFromParent();
574
575 auto InsPt = SaveExecInst->getIterator();
576 const DebugLoc &DL = SaveExecInst->getDebugLoc();
577
578 BuildMI(MBB, InsPt, DL, TII->get(getSaveExecOp(SaveExecInst->getOpcode())),
579 CopyFromExec)
580 .addReg(OtherOp->getReg());
581 SaveExecInst->eraseFromParent();
582
583 CopyToExecInst->eraseFromParent();
584
585 for (MachineInstr *OtherInst : OtherUseInsts) {
586 OtherInst->substituteRegister(CopyToExec, LMC.ExecReg,
587 AMDGPU::NoSubRegister, *TRI);
588 }
589
590 Changed = true;
591 }
592
593 return Changed;
594}
595
596bool SIOptimizeExecMasking::blocksAndN2Sink(const MachineInstr &MI,
597 Register Dst) const {
598 for (const MachineOperand &MO : MI.operands()) {
599 if (!MO.isReg())
600 continue;
601 // Any access to Dst would see a stale value once the def sinks past it,
602 // and a use of SCC would see the s_andn2 result.
603 if (TRI->regsOverlap(MO.getReg(), Dst))
604 return true;
605 if (MO.isUse() && MO.getReg() == AMDGPU::SCC)
606 return true;
607 }
608 return false;
609}
610
611// Fold
612//
613// sdst = S_ANDN2_B32 ssrc, exec
614// exec = COPY sdst
615// =>
616// sdst = S_ANDN2_WREXEC_B32 ssrc
617//
618// The waterfall loop emits the two operations separately so that spill code
619// for sdst can be inserted before exec is narrowed.
620bool SIOptimizeExecMasking::optimizeAndN2WrExecSequence(
621 MachineInstr &CopyToExecInst, Register Dst) const {
622 if (!ST->hasNoSdstCMPX() || TII->pseudoToMCOpcode(LMC.AndN2WrExecOpc) == -1)
623 return false;
624
625 MachineBasicBlock &MBB = *CopyToExecInst.getParent();
626
627 // Keep the fused instruction ahead of any trailing debug instructions, so
628 // DBG_VALUEs of Dst stay after its def.
629 MachineBasicBlock::iterator InsertPt = CopyToExecInst.getIterator();
630 if (InsertPt != MBB.begin())
631 InsertPt = std::next(prev_nodbg(InsertPt, MBB.begin()));
632
633 // Scan back for the s_andn2 computing the new exec value. The scheduler may
634 // have moved it away from the exec write, so allow instructions in between
635 // as long as sinking the s_andn2 past them is safe.
636 auto IsAndN2Def = [&](const MachineInstr &MI) {
637 return MI.getOpcode() == LMC.AndN2Opc && MI.getOperand(0).getReg() == Dst;
638 };
639 MachineInstr *AndN2Inst = findInstrBackwards(
640 CopyToExecInst,
641 [&](MachineInstr *Check) {
642 return IsAndN2Def(*Check) || blocksAndN2Sink(*Check, Dst);
643 },
644 /*NonModifiableRegs=*/{});
645 if (!AndN2Inst || !IsAndN2Def(*AndN2Inst))
646 return false;
647
648 const MachineOperand &Src = AndN2Inst->getOperand(1);
649 const MachineOperand &Mask = AndN2Inst->getOperand(2);
650 if (!Src.isReg() || !Mask.isReg() || Mask.getReg() != LMC.ExecReg)
651 return false;
652
653 for (MachineInstr &MI : make_range(
654 std::next(MachineBasicBlock::iterator(AndN2Inst)), InsertPt)) {
655 if (MI.modifiesRegister(Src.getReg(), TRI))
656 return false;
657 }
658
659 // The fused form also clobbers SCC, at the point the s_andn2 is moved to.
660 if (isRegisterInUseAfter(CopyToExecInst, AMDGPU::SCC))
661 return false;
662
663 LLVM_DEBUG(dbgs() << "Fold exec write: " << *AndN2Inst);
664
665 BuildMI(MBB, InsertPt, AndN2Inst->getDebugLoc(), TII->get(LMC.AndN2WrExecOpc),
666 Dst)
667 .addReg(Src.getReg());
668
669 AndN2Inst->eraseFromParent();
670 CopyToExecInst.eraseFromParent();
671 return true;
672}
673
674// Inserts the optimized s_mov_b32 / v_cmpx sequence based on the
675// operands extracted from a v_cmp ..., s_and_saveexec pattern.
676bool SIOptimizeExecMasking::optimizeVCMPSaveExecSequence(
677 MachineInstr &SaveExecInstr, MachineInstr &VCmp) const {
678 const int NewOpcode = AMDGPU::getVCMPXOpFromVCMP(VCmp.getOpcode());
679
680 if (NewOpcode == -1)
681 return false;
682
683 MachineOperand *Src0 = TII->getNamedOperand(VCmp, AMDGPU::OpName::src0);
684 MachineOperand *Src1 = TII->getNamedOperand(VCmp, AMDGPU::OpName::src1);
685
686 // Kills of the sources between the v_cmp and the saveexec are no longer
687 // valid once the compare is moved to the saveexec.
688 for (MachineInstr &MI :
689 make_range(std::next(VCmp.getIterator()), SaveExecInstr.getIterator())) {
690 for (MachineOperand &MO : MI.all_uses()) {
691 if (MO.isKill() &&
692 any_of(std::array{Src0, Src1}, [&](const MachineOperand *Src) {
693 return Src->isReg() && TRI->regsOverlap(MO.getReg(), Src->getReg());
694 }))
695 MO.setIsKill(false);
696 }
697 }
698
699 Register MoveDest = SaveExecInstr.getOperand(0).getReg();
700
701 MachineBasicBlock::instr_iterator InsertPosIt = SaveExecInstr.getIterator();
702 if (!SaveExecInstr.uses().empty()) {
703 bool IsSGPR32 = TRI->getRegSizeInBits(MoveDest, *MRI) == 32;
704 unsigned MovOpcode = IsSGPR32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
705 BuildMI(*SaveExecInstr.getParent(), InsertPosIt,
706 SaveExecInstr.getDebugLoc(), TII->get(MovOpcode), MoveDest)
707 .addReg(LMC.ExecReg);
708 }
709
710 // Omit dst as V_CMPX is implicitly writing to EXEC.
711 // Add dummy src and clamp modifiers, if needed.
712 auto Builder = BuildMI(*VCmp.getParent(), std::next(InsertPosIt),
713 VCmp.getDebugLoc(), TII->get(NewOpcode));
714
715 auto TryAddImmediateValueFromNamedOperand =
716 [&](AMDGPU::OpName OperandName) -> void {
717 if (auto *Mod = TII->getNamedOperand(VCmp, OperandName))
718 Builder.addImm(Mod->getImm());
719 };
720
721 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::src0_modifiers);
722 Builder.add(*Src0);
723
724 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::src1_modifiers);
725 Builder.add(*Src1);
726
727 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::clamp);
728
729 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::op_sel);
730
731 SaveExecInstr.eraseFromParent();
732 VCmp.eraseFromParent();
733
734 return true;
735}
736
737// Record (on GFX10.3 and later) occurences of
738// v_cmp_* SGPR, IMM, VGPR
739// s_and_saveexec_b32 EXEC_SGPR_DEST, SGPR
740// to be replaced with
741// s_mov_b32 EXEC_SGPR_DEST, exec_lo
742// v_cmpx_* IMM, VGPR
743// to reduce pipeline stalls.
744void SIOptimizeExecMasking::tryRecordVCmpxAndSaveexecSequence(
745 MachineInstr &MI) {
746 if (!ST->hasGFX10_3Insts())
747 return;
748
749 if (MI.getOpcode() != LMC.AndSaveExecOpc)
750 return;
751
752 Register SaveExecDest = MI.getOperand(0).getReg();
753 if (!TRI->isSGPRReg(*MRI, SaveExecDest))
754 return;
755
756 MachineOperand *SaveExecSrc0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
757 if (!SaveExecSrc0->isReg())
758 return;
759
760 // Tries to find a possibility to optimize a v_cmp ..., s_and_saveexec
761 // sequence by looking at an instance of an s_and_saveexec instruction.
762 // Returns a pointer to the v_cmp instruction if it is safe to replace the
763 // sequence (see the conditions in the function body). This is after register
764 // allocation, so some checks on operand dependencies need to be considered.
765 MachineInstr *VCmp = nullptr;
766
767 // Try to find the last v_cmp instruction that defs the saveexec input
768 // operand without any write to Exec or the saveexec input operand inbetween.
769 VCmp = findInstrBackwards(
770 MI,
771 [&](MachineInstr *Check) {
772 return AMDGPU::getVCMPXOpFromVCMP(Check->getOpcode()) != -1 &&
773 Check->modifiesRegister(SaveExecSrc0->getReg(), TRI);
774 },
775 {LMC.ExecReg, SaveExecSrc0->getReg()});
776
777 if (!VCmp)
778 return;
779
780 MachineOperand *VCmpDest = TII->getNamedOperand(*VCmp, AMDGPU::OpName::sdst);
781 assert(VCmpDest && "Should have an sdst operand!");
782
783 // Check if any of the v_cmp source operands is written by the saveexec.
784 MachineOperand *Src0 = TII->getNamedOperand(*VCmp, AMDGPU::OpName::src0);
785 if (Src0->isReg() && TRI->isSGPRReg(*MRI, Src0->getReg()) &&
786 MI.modifiesRegister(Src0->getReg(), TRI))
787 return;
788
789 MachineOperand *Src1 = TII->getNamedOperand(*VCmp, AMDGPU::OpName::src1);
790 if (Src1->isReg() && TRI->isSGPRReg(*MRI, Src1->getReg()) &&
791 MI.modifiesRegister(Src1->getReg(), TRI))
792 return;
793
794 // Don't do the transformation if the destination operand is included in
795 // it's MBB Live-outs, meaning it's used in any of its successors, leading
796 // to incorrect code if the v_cmp and therefore the def of
797 // the dest operand is removed.
798 if (isLiveOut(*VCmp->getParent(), VCmpDest->getReg()))
799 return;
800
801 // If the v_cmp target is in use between v_cmp and s_and_saveexec or after the
802 // s_and_saveexec, skip the optimization.
803 if (isRegisterInUseBetween(*VCmp, MI, VCmpDest->getReg(), false, true) ||
804 isRegisterInUseAfter(MI, VCmpDest->getReg()))
805 return;
806
807 // Try to determine if there is a write to any of the VCmp
808 // operands between the saveexec and the vcmp.
809 // If yes, additional VGPR spilling might need to be inserted. In this case,
810 // it's not worth replacing the instruction sequence.
812 if (Src0->isReg())
813 NonDefRegs.push_back(Src0->getReg());
814
815 if (Src1->isReg())
816 NonDefRegs.push_back(Src1->getReg());
817
818 if (!findInstrBackwards(
819 MI, [&](MachineInstr *Check) { return Check == VCmp; }, NonDefRegs))
820 return;
821
822 SaveExecVCmpMapping[&MI] = VCmp;
823}
824
825// Record occurences of
826// s_or_saveexec s_o, s_i
827// s_xor exec, exec, s_o
828// to be replaced with
829// s_andn2_saveexec s_o, s_i.
830void SIOptimizeExecMasking::tryRecordOrSaveexecXorSequence(MachineInstr &MI) {
831 if (MI.getOpcode() == LMC.XorOpc && &MI != &MI.getParent()->front()) {
832 const MachineOperand &XorDst = MI.getOperand(0);
833 const MachineOperand &XorSrc0 = MI.getOperand(1);
834 const MachineOperand &XorSrc1 = MI.getOperand(2);
835
836 if (XorDst.isReg() && XorDst.getReg() == LMC.ExecReg && XorSrc0.isReg() &&
837 XorSrc1.isReg() &&
838 (XorSrc0.getReg() == LMC.ExecReg || XorSrc1.getReg() == LMC.ExecReg)) {
839
840 // Peek at the previous non-debug instruction and check if this is a
841 // relevant s_or_saveexec instruction.
842 auto It = prev_nodbg(MI.getIterator(), MI.getParent()->instr_begin());
843 MachineInstr &PossibleOrSaveexec = *It;
844 if (PossibleOrSaveexec.getOpcode() != LMC.OrSaveExecOpc)
845 return;
846
847 const MachineOperand &OrDst = PossibleOrSaveexec.getOperand(0);
848 const MachineOperand &OrSrc0 = PossibleOrSaveexec.getOperand(1);
849 if (OrDst.isReg() && OrSrc0.isReg()) {
850 if ((XorSrc0.getReg() == LMC.ExecReg &&
851 XorSrc1.getReg() == OrDst.getReg()) ||
852 (XorSrc0.getReg() == OrDst.getReg() &&
853 XorSrc1.getReg() == LMC.ExecReg)) {
854 OrXors.emplace_back(&PossibleOrSaveexec, &MI);
855 }
856 }
857 }
858 }
859}
860
861bool SIOptimizeExecMasking::optimizeOrSaveexecXorSequences() {
862 if (OrXors.empty()) {
863 return false;
864 }
865
866 bool Changed = false;
867
868 for (const auto &Pair : OrXors) {
869 MachineInstr *Or = nullptr;
870 MachineInstr *Xor = nullptr;
871 std::tie(Or, Xor) = Pair;
872 BuildMI(*Or->getParent(), Or->getIterator(), Or->getDebugLoc(),
873 TII->get(LMC.AndN2SaveExecOpc), Or->getOperand(0).getReg())
874 .addReg(Or->getOperand(1).getReg());
875
876 Or->eraseFromParent();
877 Xor->eraseFromParent();
878
879 Changed = true;
880 }
881
882 return Changed;
883}
884
885bool SIOptimizeExecMaskingLegacy::runOnMachineFunction(MachineFunction &MF) {
886 if (skipFunction(MF.getFunction()))
887 return false;
888
889 return SIOptimizeExecMasking(&MF).run();
890}
891
892bool SIOptimizeExecMasking::run() {
893 bool Changed = optimizeExecSequence();
894
895 OrXors.clear();
896 SaveExecVCmpMapping.clear();
897 static unsigned SearchWindow = 10;
898 for (MachineBasicBlock &MBB : *MF) {
899 unsigned SearchCount = 0;
900
901 for (auto &MI : llvm::reverse(MBB)) {
902 if (MI.isDebugInstr())
903 continue;
904
905 if (SearchCount >= SearchWindow) {
906 break;
907 }
908
909 tryRecordOrSaveexecXorSequence(MI);
910 tryRecordVCmpxAndSaveexecSequence(MI);
911
912 if (MI.modifiesRegister(LMC.ExecReg, TRI)) {
913 break;
914 }
915
916 ++SearchCount;
917 }
918 }
919
920 Changed |= optimizeOrSaveexecXorSequences();
921 for (const auto &Entry : SaveExecVCmpMapping) {
922 MachineInstr *SaveExecInstr = Entry.getFirst();
923 MachineInstr *VCmpInstr = Entry.getSecond();
924
925 Changed |= optimizeVCMPSaveExecSequence(*SaveExecInstr, *VCmpInstr);
926 }
927
928 return Changed;
929}
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
A set of register units.
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
MachineRegisterInfo & MRI
static unsigned getSaveExecOp(unsigned Opc)
static Register isLogicalOpOnExec(const MachineInstr &MI)
If MI is a logical operation on an exec value, return the register copied to.
static bool isLiveOut(const MachineBasicBlock &MBB, unsigned Reg)
Interface definition for SIRegisterInfo.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static const LaneMaskConstants & get(const GCNSubtarget &ST)
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:280
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
A debug info location.
Definition DebugLoc.h:126
A set of register units used to track register liveness.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Instructions::iterator instr_iterator
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
reverse_iterator rbegin()
MachineInstrBundleIterator< MachineInstr > iterator
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.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
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,...
mop_range uses()
Returns all operands which may be register uses.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
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:20
constexpr bool isValid() const
Definition Register.h:112
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
reverse_self_iterator getReverseIterator()
Definition ilist_node.h:126
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_READONLY int32_t getVCMPXOpFromVCMP(uint32_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.
@ Entry
Definition COFF.h:862
std::reverse_iterator< iterator > rend() const
Definition BasicBlock.h:96
This is an optimization pass for GlobalISel generic memory operations.
IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It, then continue incrementing it while it points to a debug instruction.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
char & SIOptimizeExecMaskingLegacyID
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
@ Xor
Bitwise or logical XOR of integers.
IterT prev_nodbg(IterT It, IterT Begin, bool SkipPseudoOp=true)
Decrement It, then continue decrementing it while it points to a debug instruction.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
#define N