LLVM 24.0.0git
SILowerControlFlow.cpp
Go to the documentation of this file.
1//===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===//
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//
9/// \file
10/// This pass lowers the pseudo control flow instructions to real
11/// machine instructions.
12///
13/// All control flow is handled using predicated instructions and
14/// a predicate stack. Each Scalar ALU controls the operations of 64 Vector
15/// ALUs. The Scalar ALU can update the predicate for any of the Vector ALUs
16/// by writing to the 64-bit EXEC register (each bit corresponds to a
17/// single vector ALU). Typically, for predicates, a vector ALU will write
18/// to its bit of the VCC register (like EXEC VCC is 64-bits, one for each
19/// Vector ALU) and then the ScalarALU will AND the VCC register with the
20/// EXEC to update the predicates.
21///
22/// For example:
23/// %vcc = V_CMP_GT_F32 %vgpr1, %vgpr2
24/// %sgpr0 = SI_IF %vcc
25/// %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0
26/// %sgpr0 = SI_ELSE %sgpr0
27/// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr0
28/// SI_END_CF %sgpr0
29///
30/// becomes:
31///
32/// %sgpr0 = S_AND_SAVEEXEC_B64 %vcc // Save and update the exec mask
33/// %sgpr0 = S_XOR_B64 %sgpr0, %exec // Clear live bits from saved exec mask
34/// S_CBRANCH_EXECZ label0 // This instruction is an optional
35/// // optimization which allows us to
36/// // branch if all the bits of
37/// // EXEC are zero.
38/// %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0 // Do the IF block of the branch
39///
40/// label0:
41/// %sgpr0 = S_OR_SAVEEXEC_B64 %sgpr0 // Restore the exec mask for the Then
42/// // block
43/// %exec = S_XOR_B64 %sgpr0, %exec // Update the exec mask
44/// S_CBRANCH_EXECZ label1 // Use our branch optimization
45/// // instruction again.
46/// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr // Do the ELSE block
47/// label1:
48/// %exec = S_OR_B64 %exec, %sgpr0 // Re-enable saved exec mask bits
49//===----------------------------------------------------------------------===//
50
51#include "SILowerControlFlow.h"
52#include "AMDGPU.h"
53#include "AMDGPULaneMaskUtils.h"
54#include "GCNSubtarget.h"
62
63using namespace llvm;
64
65#define DEBUG_TYPE "si-lower-control-flow"
66
67namespace {
68
69class SILowerControlFlow {
70private:
71 const SIRegisterInfo *TRI = nullptr;
72 const SIInstrInfo *TII = nullptr;
73 LiveIntervals *LIS = nullptr;
74 MachineDominatorTree *MDT = nullptr;
75 MachinePostDominatorTree *PDT = nullptr;
76 MachineRegisterInfo *MRI = nullptr;
77 SetVector<MachineInstr*> LoweredEndCf;
78 DenseSet<Register> LoweredIf;
80 SmallSet<Register, 8> RecomputeRegs;
81
82 const TargetRegisterClass *BoolRC = nullptr;
84
85 bool EnableOptimizeEndCf = false;
86
87 bool hasKill(const MachineBasicBlock *Begin, const MachineBasicBlock *End);
88
89 void emitIf(MachineInstr &MI);
90 void emitElse(MachineInstr &MI);
91 void emitIfBreak(MachineInstr &MI);
92 void emitLoop(MachineInstr &MI);
93
95
96 void findMaskOperands(MachineInstr &MI, unsigned OpNo,
98
99 void combineMasks(MachineInstr &MI);
100
101 bool removeMBBifRedundant(MachineBasicBlock &MBB);
102
104
105 // Skip to the next instruction, ignoring debug instructions, and trivial
106 // block boundaries (blocks that have one (typically fallthrough) successor,
107 // and the successor has one predecessor.
109 skipIgnoreExecInstsTrivialSucc(MachineBasicBlock &MBB,
111
112 /// Find the insertion point for a new conditional branch.
114 skipToUncondBrOrEnd(MachineBasicBlock &MBB,
116 assert(I->isTerminator());
117
118 // FIXME: What if we had multiple pre-existing conditional branches?
120 while (I != End && !I->isUnconditionalBranch())
121 ++I;
122 return I;
123 }
124
125 // Remove redundant SI_END_CF instructions.
126 void optimizeEndCf();
127
128public:
129 SILowerControlFlow(const GCNSubtarget *ST, LiveIntervals *LIS,
131 : LIS(LIS), MDT(MDT), PDT(PDT), LMC(AMDGPU::LaneMaskConstants::get(*ST)) {
132 }
133 bool run(MachineFunction &MF);
134};
135
136class SILowerControlFlowLegacy : public MachineFunctionPass {
137public:
138 static char ID;
139
140 SILowerControlFlowLegacy() : MachineFunctionPass(ID) {}
141
142 bool runOnMachineFunction(MachineFunction &MF) override;
143
144 StringRef getPassName() const override {
145 return "SI Lower control flow pseudo instructions";
146 }
147
148 void getAnalysisUsage(AnalysisUsage &AU) const override {
150 // Should preserve the same set that TwoAddressInstructions does.
158 }
159};
160
161} // end anonymous namespace
162
163char SILowerControlFlowLegacy::ID = 0;
164
165INITIALIZE_PASS(SILowerControlFlowLegacy, DEBUG_TYPE, "SI lower control flow",
166 false, false)
167
168static void setImpSCCDefDead(MachineInstr &MI, bool IsDead = true) {
169 MachineOperand &ImpDefSCC = MI.getOperand(3);
170 assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef());
171
172 ImpDefSCC.setIsDead(IsDead);
173}
174
175static void copySCCDefDead(MachineInstr &MI, const MachineOperand &OrigSCCDef) {
176 assert(OrigSCCDef.getReg() == AMDGPU::SCC && OrigSCCDef.isDef());
177 setImpSCCDefDead(MI, OrigSCCDef.isDead());
178}
179
180char &llvm::SILowerControlFlowLegacyID = SILowerControlFlowLegacy::ID;
181
182bool SILowerControlFlow::hasKill(const MachineBasicBlock *Begin,
183 const MachineBasicBlock *End) {
184 DenseSet<const MachineBasicBlock*> Visited;
185 SmallVector<MachineBasicBlock *, 4> Worklist(Begin->successors());
186
187 while (!Worklist.empty()) {
188 MachineBasicBlock *MBB = Worklist.pop_back_val();
189
190 if (MBB == End || !Visited.insert(MBB).second)
191 continue;
192 if (KillBlocks.contains(MBB))
193 return true;
194
195 Worklist.append(MBB->succ_begin(), MBB->succ_end());
196 }
197
198 return false;
199}
200
201static bool isSimpleIf(const MachineInstr &MI, const MachineRegisterInfo *MRI) {
202 Register SaveExecReg = MI.getOperand(0).getReg();
203 auto U = MRI->use_instr_nodbg_begin(SaveExecReg);
204
205 if (U == MRI->use_instr_nodbg_end() ||
206 std::next(U) != MRI->use_instr_nodbg_end() ||
207 U->getOpcode() != AMDGPU::SI_END_CF)
208 return false;
209
210 return true;
211}
212
213void SILowerControlFlow::emitIf(MachineInstr &MI) {
214 MachineBasicBlock &MBB = *MI.getParent();
215 const DebugLoc &DL = MI.getDebugLoc();
217 Register SaveExecReg = MI.getOperand(0).getReg();
218 MachineOperand& Cond = MI.getOperand(1);
219 assert(Cond.getSubReg() == AMDGPU::NoSubRegister);
220
221 // If there is only one use of save exec register and that use is SI_END_CF,
222 // we can optimize SI_IF by returning the full saved exec mask instead of
223 // just cleared bits.
224 bool SimpleIf = isSimpleIf(MI, MRI);
225
226 if (SimpleIf) {
227 // Check for SI_KILL_*_TERMINATOR on path from if to endif.
228 // if there is any such terminator simplifications are not safe.
229 auto UseMI = MRI->use_instr_nodbg_begin(SaveExecReg);
230 SimpleIf = !hasKill(MI.getParent(), UseMI->getParent());
231 }
232
233 // Add an implicit def of exec to discourage scheduling VALU after this which
234 // will interfere with trying to form s_and_saveexec_b64 later.
235 Register CopyReg = SimpleIf ? SaveExecReg
236 : MRI->createVirtualRegister(BoolRC);
237 MachineInstr *CopyExec = BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), CopyReg)
238 .addReg(LMC.ExecReg)
239 .addReg(LMC.ExecReg, RegState::ImplicitDefine);
240 LoweredIf.insert(CopyReg);
241
242 Register Tmp = MRI->createVirtualRegister(BoolRC);
243
244 MachineInstr *And =
245 BuildMI(MBB, I, DL, TII->get(LMC.AndOpc), Tmp).addReg(CopyReg).add(Cond);
246 setImpSCCDefDead(*And);
247
248 MachineInstr *Xor = nullptr;
249 if (!SimpleIf) {
250 Xor = BuildMI(MBB, I, DL, TII->get(LMC.XorOpc), SaveExecReg)
251 .addReg(Tmp)
252 .addReg(CopyReg);
253 copySCCDefDead(*Xor, MI.getOperand(4));
254 }
255
256 // Use a copy that is a terminator to get correct spill code placement it with
257 // fast regalloc.
258 MachineInstr *SetExec =
259 BuildMI(MBB, I, DL, TII->get(LMC.MovTermOpc), LMC.ExecReg).addReg(Tmp);
260
261 // Skip ahead to the unconditional branch in case there are other terminators
262 // present.
263 I = skipToUncondBrOrEnd(MBB, I);
264
265 // Insert the S_CBRANCH_EXECZ instruction which will be optimized later
266 // during SIPreEmitPeephole.
267 MachineInstr *NewBr = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
268 .add(MI.getOperand(2));
269
270 if (!LIS) {
271 MI.eraseFromParent();
272 return;
273 }
274
275 LIS->InsertMachineInstrInMaps(*CopyExec);
276
277 // Replace with and so we don't need to fix the live interval for condition
278 // register.
280
281 if (!SimpleIf)
283 LIS->InsertMachineInstrInMaps(*SetExec);
284 LIS->InsertMachineInstrInMaps(*NewBr);
285
286 MI.eraseFromParent();
287
288 // FIXME: Is there a better way of adjusting the liveness? It shouldn't be
289 // hard to add another def here but I'm not sure how to correctly update the
290 // valno.
291 RecomputeRegs.insert(SaveExecReg);
293 if (!SimpleIf)
295}
296
297void SILowerControlFlow::emitElse(MachineInstr &MI) {
298 MachineBasicBlock &MBB = *MI.getParent();
299 const DebugLoc &DL = MI.getDebugLoc();
300
301 Register DstReg = MI.getOperand(0).getReg();
302 Register SrcReg = MI.getOperand(1).getReg();
303
305
306 // This must be inserted before phis and any spill code inserted before the
307 // else.
308 Register SaveReg = MRI->createVirtualRegister(BoolRC);
309 MachineInstr *OrSaveExec =
310 BuildMI(MBB, Start, DL, TII->get(LMC.OrSaveExecOpc), SaveReg)
311 .add(MI.getOperand(1)); // Saved EXEC
312 setImpSCCDefDead(*OrSaveExec, /*IsDead=*/true);
313
314 MachineBasicBlock *DestBB = MI.getOperand(2).getMBB();
315
317
318 // This accounts for any modification of the EXEC mask within the block and
319 // can be optimized out pre-RA when not required.
320 MachineInstr *And = BuildMI(MBB, ElsePt, DL, TII->get(LMC.AndOpc), DstReg)
321 .addReg(LMC.ExecReg)
322 .addReg(SaveReg);
323 setImpSCCDefDead(*And, /*IsDead=*/true);
324
325 MachineInstr *Xor =
326 BuildMI(MBB, ElsePt, DL, TII->get(LMC.XorTermOpc), LMC.ExecReg)
327 .addReg(LMC.ExecReg)
328 .addReg(DstReg);
329 copySCCDefDead(*Xor, MI.getOperand(4));
330
331 // Skip ahead to the unconditional branch in case there are other terminators
332 // present.
333 ElsePt = skipToUncondBrOrEnd(MBB, ElsePt);
334
335 MachineInstr *Branch =
336 BuildMI(MBB, ElsePt, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
337 .addMBB(DestBB);
338
339 if (!LIS) {
340 MI.eraseFromParent();
341 return;
342 }
343
345 MI.eraseFromParent();
346
347 LIS->InsertMachineInstrInMaps(*OrSaveExec);
349
351 LIS->InsertMachineInstrInMaps(*Branch);
352
353 RecomputeRegs.insert(SrcReg);
354 RecomputeRegs.insert(DstReg);
356}
357
358void SILowerControlFlow::emitIfBreak(MachineInstr &MI) {
359 MachineBasicBlock &MBB = *MI.getParent();
360 const DebugLoc &DL = MI.getDebugLoc();
361 auto Dst = MI.getOperand(0).getReg();
362
363 // Skip ANDing with exec if the break condition is already masked by exec
364 // because it is a V_CMP in the same basic block. (We know the break
365 // condition operand was an i1 in IR, so if it is a VALU instruction it must
366 // be one with a carry-out.)
367 bool SkipAnding = false;
368 if (MI.getOperand(1).isReg()) {
369 if (MachineInstr *Def = MRI->getUniqueVRegDef(MI.getOperand(1).getReg())) {
370 SkipAnding = Def->getParent() == MI.getParent() &&
371 SIInstrInfo::isVALU(*Def, /*AllowLDSDMA=*/false);
372 }
373 }
374
375 // AND the break condition operand with exec, then OR that into the "loop
376 // exit" mask.
377 MachineInstr *And = nullptr, *Or = nullptr;
378 Register AndReg;
379 if (!SkipAnding) {
380 AndReg = MRI->createVirtualRegister(BoolRC);
381 And = BuildMI(MBB, &MI, DL, TII->get(LMC.AndOpc), AndReg)
382 .addReg(LMC.ExecReg)
383 .add(MI.getOperand(1));
384 setImpSCCDefDead(*And, /*IsDead=*/true);
385 Or = BuildMI(MBB, &MI, DL, TII->get(LMC.OrOpc), Dst)
386 .addReg(AndReg)
387 .add(MI.getOperand(2));
388 } else {
389 Or = BuildMI(MBB, &MI, DL, TII->get(LMC.OrOpc), Dst)
390 .add(MI.getOperand(1))
391 .add(MI.getOperand(2));
392 }
393
394 copySCCDefDead(*Or, MI.getOperand(3));
395
396 if (LIS) {
398 if (And) {
399 // Read of original operand 1 is on And now not Or.
400 RecomputeRegs.insert(And->getOperand(2).getReg());
403 }
404 }
405
406 MI.eraseFromParent();
407}
408
409void SILowerControlFlow::emitLoop(MachineInstr &MI) {
410 MachineBasicBlock &MBB = *MI.getParent();
411 const DebugLoc &DL = MI.getDebugLoc();
412
413 MachineInstr *AndN2 =
414 BuildMI(MBB, &MI, DL, TII->get(LMC.AndN2TermOpc), LMC.ExecReg)
415 .addReg(LMC.ExecReg)
416 .add(MI.getOperand(0));
417 copySCCDefDead(*AndN2, MI.getOperand(3));
418
419 auto BranchPt = skipToUncondBrOrEnd(MBB, MI.getIterator());
420 MachineInstr *Branch =
421 BuildMI(MBB, BranchPt, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
422 .add(MI.getOperand(1));
423
424 if (LIS) {
425 RecomputeRegs.insert(MI.getOperand(0).getReg());
426 LIS->ReplaceMachineInstrInMaps(MI, *AndN2);
427 LIS->InsertMachineInstrInMaps(*Branch);
428 }
429
430 MI.eraseFromParent();
431}
432
434SILowerControlFlow::skipIgnoreExecInstsTrivialSucc(
435 MachineBasicBlock &MBB, MachineBasicBlock::iterator It) const {
436
437 SmallPtrSet<const MachineBasicBlock *, 4> Visited;
438 MachineBasicBlock *B = &MBB;
439 do {
440 if (!Visited.insert(B).second)
441 return MBB.end();
442
443 auto E = B->end();
444 for ( ; It != E; ++It) {
445 if (TII->mayReadEXEC(*MRI, *It))
446 break;
447 }
448
449 if (It != E)
450 return It;
451
452 if (B->succ_size() != 1)
453 return MBB.end();
454
455 // If there is one trivial successor, advance to the next block.
456 MachineBasicBlock *Succ = *B->succ_begin();
457
458 It = Succ->begin();
459 B = Succ;
460 } while (true);
461}
462
463MachineBasicBlock *SILowerControlFlow::emitEndCf(MachineInstr &MI) {
464 MachineBasicBlock &MBB = *MI.getParent();
465 const DebugLoc &DL = MI.getDebugLoc();
466
468
469 // If we have instructions that aren't prolog instructions, split the block
470 // and emit a terminator instruction. This ensures correct spill placement.
471 // FIXME: We should unconditionally split the block here.
472 bool NeedBlockSplit = false;
473 Register DataReg = MI.getOperand(0).getReg();
474 for (MachineBasicBlock::iterator I = InsPt, E = MI.getIterator();
475 I != E; ++I) {
476 if (I->modifiesRegister(DataReg, TRI)) {
477 NeedBlockSplit = true;
478 break;
479 }
480 }
481
482 unsigned Opcode = LMC.OrOpc;
483 MachineBasicBlock *SplitBB = &MBB;
484 if (NeedBlockSplit) {
485 SplitBB = MBB.splitAt(MI, /*UpdateLiveIns*/true, LIS);
486 if (SplitBB != &MBB && (MDT || PDT)) {
487 using DomTreeT = DomTreeBase<MachineBasicBlock>;
489 for (MachineBasicBlock *Succ : SplitBB->successors()) {
490 DTUpdates.push_back({DomTreeT::Insert, SplitBB, Succ});
491 DTUpdates.push_back({DomTreeT::Delete, &MBB, Succ});
492 }
493 DTUpdates.push_back({DomTreeT::Insert, &MBB, SplitBB});
494 if (MDT)
495 MDT->applyUpdates(DTUpdates);
496 if (PDT)
497 PDT->applyUpdates(DTUpdates);
498 }
499 Opcode = LMC.OrTermOpc;
500 InsPt = MI;
501 }
502
503 MachineInstr *NewMI = BuildMI(MBB, InsPt, DL, TII->get(Opcode), LMC.ExecReg)
504 .addReg(LMC.ExecReg)
505 .add(MI.getOperand(0));
506 copySCCDefDead(*NewMI, MI.getOperand(2));
507
508 LoweredEndCf.insert(NewMI);
509
510 if (LIS)
511 LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
512
513 MI.eraseFromParent();
514
515 if (LIS)
516 LIS->handleMove(*NewMI);
517 return SplitBB;
518}
519
520// Returns replace operands for a logical operation, either single result
521// for exec or two operands if source was another equivalent operation.
522void SILowerControlFlow::findMaskOperands(
523 MachineInstr &MI, unsigned OpNo,
524 SmallVectorImpl<MachineOperand *> &Src) const {
525 MachineOperand &Op = MI.getOperand(OpNo);
526 if (!Op.isReg() || !Op.getReg().isVirtual()) {
527 Src.push_back(&Op);
528 return;
529 }
530
531 MachineInstr *Def = MRI->getUniqueVRegDef(Op.getReg());
532 if (!Def || Def->getParent() != MI.getParent() ||
533 !(Def->isFullCopy() || (Def->getOpcode() == MI.getOpcode())))
534 return;
535
536 // Make sure we do not modify exec between def and use.
537 // A copy with implicitly defined exec inserted earlier is an exclusion, it
538 // does not really modify exec.
539 for (auto I = Def->getIterator(); I != MI.getIterator(); ++I)
540 if (I->modifiesRegister(AMDGPU::EXEC, TRI) &&
541 !(I->isCopy() && I->getOperand(0).getReg() != LMC.ExecReg))
542 return;
543
544 for (MachineOperand &SrcOp : Def->explicit_operands())
545 if (SrcOp.isReg() && SrcOp.isUse() &&
546 (SrcOp.getReg().isVirtual() || SrcOp.getReg() == LMC.ExecReg))
547 Src.push_back(&SrcOp);
548}
549
550// Search and combine pairs of equivalent instructions, like
551// S_AND_B64 x, (S_AND_B64 x, y) => S_AND_B64 x, y
552// S_OR_B64 x, (S_OR_B64 x, y) => S_OR_B64 x, y
553// One of the operands is exec mask.
554void SILowerControlFlow::combineMasks(MachineInstr &MI) {
555 assert(MI.getNumExplicitOperands() == 3);
557 findMaskOperands(MI, 1, Src1);
558 findMaskOperands(MI, 2, Src2);
559
560 // Exactly one of the two operands must resolve to the nested LHS and RHS.
561 // Another one must resolve to a single value, exec or its copy.
562 unsigned OpToReplace;
563 MachineOperand *Leaf, *NestedLHS, *NestedRHS;
564 if (Src1.size() == 2 && Src2.size() == 1) {
565 OpToReplace = 1;
566 NestedLHS = Src1[0];
567 NestedRHS = Src1[1];
568 Leaf = Src2[0];
569 } else if (Src1.size() == 1 && Src2.size() == 2) {
570 OpToReplace = 2;
571 Leaf = Src1[0];
572 NestedLHS = Src2[0];
573 NestedRHS = Src2[1];
574 } else {
575 return;
576 }
577
578 // Always keep a nested operand, never the leaf operand.
579 MachineOperand *KeepOp;
580 if (Leaf->isIdenticalTo(*NestedLHS))
581 KeepOp = NestedRHS;
582 else if (Leaf->isIdenticalTo(*NestedRHS) ||
583 NestedLHS->isIdenticalTo(*NestedRHS))
584 KeepOp = NestedLHS;
585 else
586 return;
587
588 Register Reg = MI.getOperand(OpToReplace).getReg();
589 MI.removeOperand(OpToReplace);
590 MI.addOperand(*KeepOp);
591 if (MRI->use_empty(Reg))
593}
594
595void SILowerControlFlow::optimizeEndCf() {
596 // If the only instruction immediately following this END_CF is another
597 // END_CF in the only successor we can avoid emitting exec mask restore here.
598 if (!EnableOptimizeEndCf)
599 return;
600
601 for (MachineInstr *MI : reverse(LoweredEndCf)) {
602 MachineBasicBlock &MBB = *MI->getParent();
603 auto Next =
604 skipIgnoreExecInstsTrivialSucc(MBB, std::next(MI->getIterator()));
605 if (Next == MBB.end() || !LoweredEndCf.count(&*Next))
606 continue;
607 // Only skip inner END_CF if outer ENDCF belongs to SI_IF.
608 // If that belongs to SI_ELSE then saved mask has an inverted value.
609 Register SavedExec
610 = TII->getNamedOperand(*Next, AMDGPU::OpName::src1)->getReg();
611 assert(SavedExec.isVirtual() && "Expected saved exec to be src1!");
612
613 const MachineInstr *Def = MRI->getUniqueVRegDef(SavedExec);
614 if (Def && LoweredIf.count(SavedExec)) {
615 LLVM_DEBUG(dbgs() << "Skip redundant "; MI->dump());
616 if (LIS)
618 MI->eraseFromParent();
619 removeMBBifRedundant(MBB);
620 }
621 }
622}
623
624MachineBasicBlock *SILowerControlFlow::process(MachineInstr &MI) {
625 MachineBasicBlock &MBB = *MI.getParent();
627 MachineInstr *Prev = (I != MBB.begin()) ? &*(std::prev(I)) : nullptr;
628
629 MachineBasicBlock *SplitBB = &MBB;
630
631 switch (MI.getOpcode()) {
632 case AMDGPU::SI_IF:
633 emitIf(MI);
634 break;
635
636 case AMDGPU::SI_ELSE:
637 emitElse(MI);
638 break;
639
640 case AMDGPU::SI_IF_BREAK:
641 emitIfBreak(MI);
642 break;
643
644 case AMDGPU::SI_LOOP:
645 emitLoop(MI);
646 break;
647
648 case AMDGPU::SI_WATERFALL_LOOP:
649 MI.setDesc(TII->get(AMDGPU::S_CBRANCH_EXECNZ));
650 break;
651
652 case AMDGPU::SI_END_CF:
653 SplitBB = emitEndCf(MI);
654 break;
655
656 default:
657 assert(false && "Attempt to process unsupported instruction");
658 break;
659 }
660
662 for (I = Prev ? Prev->getIterator() : MBB.begin(); I != MBB.end(); I = Next) {
663 Next = std::next(I);
664 MachineInstr &MaskMI = *I;
665 switch (MaskMI.getOpcode()) {
666 case AMDGPU::S_AND_B64:
667 case AMDGPU::S_OR_B64:
668 case AMDGPU::S_AND_B32:
669 case AMDGPU::S_OR_B32:
670 // Cleanup bit manipulations on exec mask
671 combineMasks(MaskMI);
672 break;
673 default:
674 I = MBB.end();
675 break;
676 }
677 }
678
679 return SplitBB;
680}
681
682bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) {
683 for (auto &I : MBB.instrs()) {
684 if (!I.isDebugInstr() && !I.isUnconditionalBranch())
685 return false;
686 }
687
688 assert(MBB.succ_size() == 1 && "MBB has more than one successor");
689
690 MachineBasicBlock *Succ = *MBB.succ_begin();
691 MachineBasicBlock *FallThrough = nullptr;
692
693 using DomTreeT = DomTreeBase<MachineBasicBlock>;
695
696 while (!MBB.predecessors().empty()) {
697 MachineBasicBlock *P = *MBB.pred_begin();
698 if (P->getFallThrough(false) == &MBB)
699 FallThrough = P;
701 DTUpdates.push_back({DomTreeT::Insert, P, Succ});
702 DTUpdates.push_back({DomTreeT::Delete, P, &MBB});
703 }
704 MBB.removeSuccessor(Succ);
705 if (LIS) {
706 // Registers live across MBB have intervals spanning it, which must be
707 // recomputed once it is erased. removeMBBifRedundant only runs from
708 // optimizeEndCf, so defer to the pass-wide RecomputeRegs handling.
709 SlotIndex StartIdx = LIS->getMBBStartIdx(&MBB);
710 for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) {
711 Register Reg = Register::index2VirtReg(I);
712 if (!LIS->hasInterval(Reg))
713 continue;
714 const LiveInterval &LI = LIS->getInterval(Reg);
715 if (LI.liveAt(StartIdx) || LI.liveAt(StartIdx.getPrevSlot()))
716 RecomputeRegs.insert(Reg);
717 }
718
719 for (auto &I : MBB.instrs())
721
722 // Drop MBB from the slot index maps before it is erased.
724 }
725 if (MDT)
726 MDT->applyUpdates(DTUpdates);
727 if (PDT)
728 PDT->applyUpdates(DTUpdates);
729
730 if (MDT && MDT->getNode(&MBB))
731 MDT->eraseNode(&MBB);
732 if (PDT && PDT->getNode(&MBB))
733 PDT->eraseNode(&MBB);
734
735 MBB.clear();
737 if (FallThrough && !FallThrough->isLayoutSuccessor(Succ)) {
738 // Note: we cannot update block layout and preserve live intervals;
739 // hence we must insert a branch.
740 MachineInstr *BranchMI = BuildMI(*FallThrough, FallThrough->end(),
741 FallThrough->findBranchDebugLoc(), TII->get(AMDGPU::S_BRANCH))
742 .addMBB(Succ);
743 if (LIS)
744 LIS->InsertMachineInstrInMaps(*BranchMI);
745 }
746
747 return true;
748}
749
750bool SILowerControlFlow::run(MachineFunction &MF) {
751 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
752 TII = ST.getInstrInfo();
753 TRI = &TII->getRegisterInfo();
754 EnableOptimizeEndCf = ST.getCLOpts().remove_redundant_endcf &&
755 MF.getTarget().getOptLevel() > CodeGenOptLevel::None;
756
757 MRI = &MF.getRegInfo();
758 BoolRC = TRI->getBoolRC();
759
760 // Compute set of blocks with kills
761 const bool CanDemote =
762 MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS;
763 for (auto &MBB : MF) {
764 bool IsKillBlock = false;
765 for (auto &Term : MBB.terminators()) {
766 if (TII->isKillTerminator(Term.getOpcode())) {
767 KillBlocks.insert(&MBB);
768 IsKillBlock = true;
769 break;
770 }
771 }
772 if (CanDemote && !IsKillBlock) {
773 for (auto &MI : MBB) {
774 if (MI.getOpcode() == AMDGPU::SI_DEMOTE_I1) {
775 KillBlocks.insert(&MBB);
776 break;
777 }
778 }
779 }
780 }
781
782 bool Changed = false;
784 for (MachineFunction::iterator BI = MF.begin();
785 BI != MF.end(); BI = NextBB) {
786 NextBB = std::next(BI);
787 MachineBasicBlock *MBB = &*BI;
788
790 E = MBB->end();
791 for (I = MBB->begin(); I != E; I = Next) {
792 Next = std::next(I);
793 MachineInstr &MI = *I;
794 MachineBasicBlock *SplitMBB = MBB;
795
796 switch (MI.getOpcode()) {
797 case AMDGPU::SI_IF:
798 case AMDGPU::SI_ELSE:
799 case AMDGPU::SI_IF_BREAK:
800 case AMDGPU::SI_WATERFALL_LOOP:
801 case AMDGPU::SI_LOOP:
802 case AMDGPU::SI_END_CF:
803 SplitMBB = process(MI);
804 Changed = true;
805 break;
806 }
807
808 if (SplitMBB != MBB) {
809 MBB = Next->getParent();
810 E = MBB->end();
811 }
812 }
813 }
814
815 optimizeEndCf();
816
817 if (LIS && Changed) {
818 // These will need to be recomputed for insertions and removals.
819 LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC);
820 LIS->removeAllRegUnitsForPhysReg(AMDGPU::SCC);
821 for (Register Reg : RecomputeRegs) {
822 LIS->removeInterval(Reg);
824 }
825 }
826
827 RecomputeRegs.clear();
828 LoweredEndCf.clear();
829 LoweredIf.clear();
830 KillBlocks.clear();
831
832 return Changed;
833}
834
835bool SILowerControlFlowLegacy::runOnMachineFunction(MachineFunction &MF) {
836 const GCNSubtarget *ST = &MF.getSubtarget<GCNSubtarget>();
837 // This doesn't actually need LiveIntervals, but we can preserve them.
838 auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
839 LiveIntervals *LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
840 auto *MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
841 MachineDominatorTree *MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
842 auto *PDTWrapper =
843 getAnalysisIfAvailable<MachinePostDominatorTreeWrapperPass>();
844 MachinePostDominatorTree *PDT =
845 PDTWrapper ? &PDTWrapper->getPostDomTree() : nullptr;
846 return SILowerControlFlow(ST, LIS, MDT, PDT).run(MF);
847}
848
849PreservedAnalyses
852 const GCNSubtarget *ST = &MF.getSubtarget<GCNSubtarget>();
858
859 bool Changed = SILowerControlFlow(ST, LIS, MDT, PDT).run(MF);
860 if (!Changed)
861 return PreservedAnalyses::all();
862
864 PA.preserve<MachineDominatorTreeAnalysis>();
866 PA.preserve<SlotIndexesAnalysis>();
867 PA.preserve<LiveIntervalsAnalysis>();
868 PA.preserve<MachineBlockFrequencyAnalysis>();
869 return PA;
870}
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
SmallVector< MachineOperand, 4 > Cond
MachineRegisterInfo & MRI
bool IsDead
static bool isSimpleIf(const MachineInstr &MI, const MachineRegisterInfo *MRI)
#define LLVM_DEBUG(...)
Definition Debug.h:119
static const LaneMaskConstants & get(const GCNSubtarget &ST)
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Implements a dense probed hash-table based set.
Definition DenseSet.h:283
void applyUpdates(ArrayRef< UpdateType > Updates)
Inform the dominator tree about a sequence of CFG edge insertions and deletions and perform a batch u...
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:273
const HexagonRegisterInfo & getRegisterInfo() const
void removeAllRegUnitsForPhysReg(MCRegister Reg)
Remove associated live ranges for the register units associated with Reg.
bool hasInterval(Register Reg) const
SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const
Return the first index in the given basic block.
SlotIndex InsertMachineInstrInMaps(MachineInstr &MI)
LLVM_ABI void handleMove(MachineInstr &MI, bool UpdateFlags=false)
Call this method to notify LiveIntervals that instruction MI has been moved within a basic block.
SlotIndexes * getSlotIndexes() const
void RemoveMachineInstrFromMaps(MachineInstr &MI)
LiveInterval & getInterval(Register Reg)
void removeInterval(Register Reg)
Interval removal.
LiveInterval & createAndComputeVirtRegInterval(Register Reg)
SlotIndex ReplaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI)
bool liveAt(SlotIndex index) const
LLVM_ABI void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
LLVM_ABI void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New)
Given a machine basic block that branched to 'Old', change the code and CFG so that it branches to 'N...
LLVM_ABI bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
LLVM_ABI MachineBasicBlock * splitAt(MachineInstr &SplitInst, bool UpdateLiveIns=true, LiveIntervals *LIS=nullptr)
Split a basic block into 2 pieces at SplitPoint.
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< iterator > terminators()
LLVM_ABI DebugLoc findBranchDebugLoc()
Find and return the merged DebugLoc of the branch instructions of the block.
iterator_range< succ_iterator > successors()
iterator_range< pred_iterator > predecessors()
MachineInstrBundleIterator< MachineInstr > iterator
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
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.
BasicBlockListType::iterator iterator
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
MachineOperand class - Representation of each machine instruction operand.
void setIsDead(bool Val=true)
Register getReg() const
getReg - Returns the register number.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
MachinePostDominatorTree - an analysis pass wrapper for DominatorTree used to compute the post-domina...
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
use_instr_nodbg_iterator use_instr_nodbg_begin(Register RegNo) const
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
bool use_empty(Register RegNo) const
use_empty - Return true if there are no instructions using the specified register.
LLVM_ABI LLVM_READONLY MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
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 isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
static bool isVALU(const MachineInstr &MI, bool AllowLDSDMA)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
A vector that has set insertion semantics.
Definition SetVector.h:57
size_type count(const_arg_type key) const
Count the number of elements of a given key in the SetVector.
Definition SetVector.h:268
void clear()
Completely clear the SetVector.
Definition SetVector.h:273
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
SlotIndex getPrevSlot() const
Returns the previous slot in the index list.
LLVM_ABI void removeMBBFromMaps(MachineBasicBlock &MBB)
Inverse of insertMBBInMaps: merge MBB's slot range into its layout predecessor and drop it from the m...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:210
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:188
self_iterator getIterator()
Definition ilist_node.h:123
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
char & SILowerControlFlowLegacyID
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
DominatorTreeBase< T, false > DomTreeBase
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...
@ Or
Bitwise or logical OR of integers.
@ Xor
Bitwise or logical XOR of integers.
@ And
Bitwise or logical AND of integers.
DWARFExpression::Operation Op
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58