10b57cec5SDimitry Andric //===-- SIModeRegister.cpp - Mode Register --------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric /// \file
90b57cec5SDimitry Andric /// This pass inserts changes to the Mode register settings as required.
100b57cec5SDimitry Andric /// Note that currently it only deals with the Double Precision Floating Point
110b57cec5SDimitry Andric /// rounding mode setting, but is intended to be generic enough to be easily
120b57cec5SDimitry Andric /// expanded.
130b57cec5SDimitry Andric ///
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric #include "AMDGPU.h"
17*af732203SDimitry Andric #include "GCNSubtarget.h"
18*af732203SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
190b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
200b57cec5SDimitry Andric #include <queue>
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #define DEBUG_TYPE "si-mode-register"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric STATISTIC(NumSetregInserted, "Number of setreg of mode register inserted.");
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric struct Status {
290b57cec5SDimitry Andric   // Mask is a bitmask where a '1' indicates the corresponding Mode bit has a
300b57cec5SDimitry Andric   // known value
310b57cec5SDimitry Andric   unsigned Mask;
320b57cec5SDimitry Andric   unsigned Mode;
330b57cec5SDimitry Andric 
StatusStatus340b57cec5SDimitry Andric   Status() : Mask(0), Mode(0){};
350b57cec5SDimitry Andric 
StatusStatus360b57cec5SDimitry Andric   Status(unsigned NewMask, unsigned NewMode) : Mask(NewMask), Mode(NewMode) {
370b57cec5SDimitry Andric     Mode &= Mask;
380b57cec5SDimitry Andric   };
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric   // merge two status values such that only values that don't conflict are
410b57cec5SDimitry Andric   // preserved
mergeStatus420b57cec5SDimitry Andric   Status merge(const Status &S) const {
430b57cec5SDimitry Andric     return Status((Mask | S.Mask), ((Mode & ~S.Mask) | (S.Mode & S.Mask)));
440b57cec5SDimitry Andric   }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   // merge an unknown value by using the unknown value's mask to remove bits
470b57cec5SDimitry Andric   // from the result
mergeUnknownStatus480b57cec5SDimitry Andric   Status mergeUnknown(unsigned newMask) {
490b57cec5SDimitry Andric     return Status(Mask & ~newMask, Mode & ~newMask);
500b57cec5SDimitry Andric   }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   // intersect two Status values to produce a mode and mask that is a subset
530b57cec5SDimitry Andric   // of both values
intersectStatus540b57cec5SDimitry Andric   Status intersect(const Status &S) const {
550b57cec5SDimitry Andric     unsigned NewMask = (Mask & S.Mask) & (Mode ^ ~S.Mode);
560b57cec5SDimitry Andric     unsigned NewMode = (Mode & NewMask);
570b57cec5SDimitry Andric     return Status(NewMask, NewMode);
580b57cec5SDimitry Andric   }
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   // produce the delta required to change the Mode to the required Mode
deltaStatus610b57cec5SDimitry Andric   Status delta(const Status &S) const {
620b57cec5SDimitry Andric     return Status((S.Mask & (Mode ^ S.Mode)) | (~Mask & S.Mask), S.Mode);
630b57cec5SDimitry Andric   }
640b57cec5SDimitry Andric 
operator ==Status650b57cec5SDimitry Andric   bool operator==(const Status &S) const {
660b57cec5SDimitry Andric     return (Mask == S.Mask) && (Mode == S.Mode);
670b57cec5SDimitry Andric   }
680b57cec5SDimitry Andric 
operator !=Status690b57cec5SDimitry Andric   bool operator!=(const Status &S) const { return !(*this == S); }
700b57cec5SDimitry Andric 
isCompatibleStatus710b57cec5SDimitry Andric   bool isCompatible(Status &S) {
720b57cec5SDimitry Andric     return ((Mask & S.Mask) == S.Mask) && ((Mode & S.Mask) == S.Mode);
730b57cec5SDimitry Andric   }
740b57cec5SDimitry Andric 
isCombinableStatus755ffd83dbSDimitry Andric   bool isCombinable(Status &S) { return !(Mask & S.Mask) || isCompatible(S); }
760b57cec5SDimitry Andric };
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric class BlockData {
790b57cec5SDimitry Andric public:
800b57cec5SDimitry Andric   // The Status that represents the mode register settings required by the
810b57cec5SDimitry Andric   // FirstInsertionPoint (if any) in this block. Calculated in Phase 1.
820b57cec5SDimitry Andric   Status Require;
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   // The Status that represents the net changes to the Mode register made by
850b57cec5SDimitry Andric   // this block, Calculated in Phase 1.
860b57cec5SDimitry Andric   Status Change;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   // The Status that represents the mode register settings on exit from this
890b57cec5SDimitry Andric   // block. Calculated in Phase 2.
900b57cec5SDimitry Andric   Status Exit;
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   // The Status that represents the intersection of exit Mode register settings
930b57cec5SDimitry Andric   // from all predecessor blocks. Calculated in Phase 2, and used by Phase 3.
940b57cec5SDimitry Andric   Status Pred;
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   // In Phase 1 we record the first instruction that has a mode requirement,
970b57cec5SDimitry Andric   // which is used in Phase 3 if we need to insert a mode change.
980b57cec5SDimitry Andric   MachineInstr *FirstInsertionPoint;
990b57cec5SDimitry Andric 
1005ffd83dbSDimitry Andric   // A flag to indicate whether an Exit value has been set (we can't tell by
1015ffd83dbSDimitry Andric   // examining the Exit value itself as all values may be valid results).
1025ffd83dbSDimitry Andric   bool ExitSet;
1035ffd83dbSDimitry Andric 
BlockData()1045ffd83dbSDimitry Andric   BlockData() : FirstInsertionPoint(nullptr), ExitSet(false){};
1050b57cec5SDimitry Andric };
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric namespace {
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric class SIModeRegister : public MachineFunctionPass {
1100b57cec5SDimitry Andric public:
1110b57cec5SDimitry Andric   static char ID;
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   std::vector<std::unique_ptr<BlockData>> BlockInfo;
1140b57cec5SDimitry Andric   std::queue<MachineBasicBlock *> Phase2List;
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   // The default mode register setting currently only caters for the floating
1170b57cec5SDimitry Andric   // point double precision rounding mode.
1180b57cec5SDimitry Andric   // We currently assume the default rounding mode is Round to Nearest
1190b57cec5SDimitry Andric   // NOTE: this should come from a per function rounding mode setting once such
1200b57cec5SDimitry Andric   // a setting exists.
1210b57cec5SDimitry Andric   unsigned DefaultMode = FP_ROUND_ROUND_TO_NEAREST;
1220b57cec5SDimitry Andric   Status DefaultStatus =
1230b57cec5SDimitry Andric       Status(FP_ROUND_MODE_DP(0x3), FP_ROUND_MODE_DP(DefaultMode));
1240b57cec5SDimitry Andric 
1255ffd83dbSDimitry Andric   bool Changed = false;
1265ffd83dbSDimitry Andric 
1270b57cec5SDimitry Andric public:
SIModeRegister()1280b57cec5SDimitry Andric   SIModeRegister() : MachineFunctionPass(ID) {}
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
1310b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const1320b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
1330b57cec5SDimitry Andric     AU.setPreservesCFG();
1340b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
1350b57cec5SDimitry Andric   }
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   void processBlockPhase1(MachineBasicBlock &MBB, const SIInstrInfo *TII);
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   void processBlockPhase2(MachineBasicBlock &MBB, const SIInstrInfo *TII);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   void processBlockPhase3(MachineBasicBlock &MBB, const SIInstrInfo *TII);
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   Status getInstructionMode(MachineInstr &MI, const SIInstrInfo *TII);
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   void insertSetreg(MachineBasicBlock &MBB, MachineInstr *I,
1460b57cec5SDimitry Andric                     const SIInstrInfo *TII, Status InstrMode);
1470b57cec5SDimitry Andric };
1480b57cec5SDimitry Andric } // End anonymous namespace.
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric INITIALIZE_PASS(SIModeRegister, DEBUG_TYPE,
1510b57cec5SDimitry Andric                 "Insert required mode register values", false, false)
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric char SIModeRegister::ID = 0;
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric char &llvm::SIModeRegisterID = SIModeRegister::ID;
1560b57cec5SDimitry Andric 
createSIModeRegisterPass()1570b57cec5SDimitry Andric FunctionPass *llvm::createSIModeRegisterPass() { return new SIModeRegister(); }
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric // Determine the Mode register setting required for this instruction.
1600b57cec5SDimitry Andric // Instructions which don't use the Mode register return a null Status.
1610b57cec5SDimitry Andric // Note this currently only deals with instructions that use the floating point
1620b57cec5SDimitry Andric // double precision setting.
getInstructionMode(MachineInstr & MI,const SIInstrInfo * TII)1630b57cec5SDimitry Andric Status SIModeRegister::getInstructionMode(MachineInstr &MI,
1640b57cec5SDimitry Andric                                           const SIInstrInfo *TII) {
1650b57cec5SDimitry Andric   if (TII->usesFPDPRounding(MI)) {
1660b57cec5SDimitry Andric     switch (MI.getOpcode()) {
1670b57cec5SDimitry Andric     case AMDGPU::V_INTERP_P1LL_F16:
1680b57cec5SDimitry Andric     case AMDGPU::V_INTERP_P1LV_F16:
1690b57cec5SDimitry Andric     case AMDGPU::V_INTERP_P2_F16:
1700b57cec5SDimitry Andric       // f16 interpolation instructions need double precision round to zero
1710b57cec5SDimitry Andric       return Status(FP_ROUND_MODE_DP(3),
1720b57cec5SDimitry Andric                     FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_ZERO));
1730b57cec5SDimitry Andric     default:
1740b57cec5SDimitry Andric       return DefaultStatus;
1750b57cec5SDimitry Andric     }
1760b57cec5SDimitry Andric   }
1770b57cec5SDimitry Andric   return Status();
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric // Insert a setreg instruction to update the Mode register.
1810b57cec5SDimitry Andric // It is possible (though unlikely) for an instruction to require a change to
1820b57cec5SDimitry Andric // the value of disjoint parts of the Mode register when we don't know the
1830b57cec5SDimitry Andric // value of the intervening bits. In that case we need to use more than one
1840b57cec5SDimitry Andric // setreg instruction.
insertSetreg(MachineBasicBlock & MBB,MachineInstr * MI,const SIInstrInfo * TII,Status InstrMode)1850b57cec5SDimitry Andric void SIModeRegister::insertSetreg(MachineBasicBlock &MBB, MachineInstr *MI,
1860b57cec5SDimitry Andric                                   const SIInstrInfo *TII, Status InstrMode) {
1870b57cec5SDimitry Andric   while (InstrMode.Mask) {
1880b57cec5SDimitry Andric     unsigned Offset = countTrailingZeros<unsigned>(InstrMode.Mask);
1890b57cec5SDimitry Andric     unsigned Width = countTrailingOnes<unsigned>(InstrMode.Mask >> Offset);
1900b57cec5SDimitry Andric     unsigned Value = (InstrMode.Mode >> Offset) & ((1 << Width) - 1);
1910b57cec5SDimitry Andric     BuildMI(MBB, MI, 0, TII->get(AMDGPU::S_SETREG_IMM32_B32))
1920b57cec5SDimitry Andric         .addImm(Value)
1930b57cec5SDimitry Andric         .addImm(((Width - 1) << AMDGPU::Hwreg::WIDTH_M1_SHIFT_) |
1940b57cec5SDimitry Andric                 (Offset << AMDGPU::Hwreg::OFFSET_SHIFT_) |
1950b57cec5SDimitry Andric                 (AMDGPU::Hwreg::ID_MODE << AMDGPU::Hwreg::ID_SHIFT_));
1960b57cec5SDimitry Andric     ++NumSetregInserted;
1975ffd83dbSDimitry Andric     Changed = true;
1980b57cec5SDimitry Andric     InstrMode.Mask &= ~(((1 << Width) - 1) << Offset);
1990b57cec5SDimitry Andric   }
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric // In Phase 1 we iterate through the instructions of the block and for each
2030b57cec5SDimitry Andric // instruction we get its mode usage. If the instruction uses the Mode register
2040b57cec5SDimitry Andric // we:
2050b57cec5SDimitry Andric // - update the Change status, which tracks the changes to the Mode register
2060b57cec5SDimitry Andric //   made by this block
2070b57cec5SDimitry Andric // - if this instruction's requirements are compatible with the current setting
2080b57cec5SDimitry Andric //   of the Mode register we merge the modes
2090b57cec5SDimitry Andric // - if it isn't compatible and an InsertionPoint isn't set, then we set the
2100b57cec5SDimitry Andric //   InsertionPoint to the current instruction, and we remember the current
2110b57cec5SDimitry Andric //   mode
2120b57cec5SDimitry Andric // - if it isn't compatible and InsertionPoint is set we insert a seteg before
2130b57cec5SDimitry Andric //   that instruction (unless this instruction forms part of the block's
2140b57cec5SDimitry Andric //   entry requirements in which case the insertion is deferred until Phase 3
2150b57cec5SDimitry Andric //   when predecessor exit values are known), and move the insertion point to
2160b57cec5SDimitry Andric //   this instruction
2170b57cec5SDimitry Andric // - if this is a setreg instruction we treat it as an incompatible instruction.
2180b57cec5SDimitry Andric //   This is sub-optimal but avoids some nasty corner cases, and is expected to
2190b57cec5SDimitry Andric //   occur very rarely.
2200b57cec5SDimitry Andric // - on exit we have set the Require, Change, and initial Exit modes.
processBlockPhase1(MachineBasicBlock & MBB,const SIInstrInfo * TII)2210b57cec5SDimitry Andric void SIModeRegister::processBlockPhase1(MachineBasicBlock &MBB,
2220b57cec5SDimitry Andric                                         const SIInstrInfo *TII) {
2238bcb0991SDimitry Andric   auto NewInfo = std::make_unique<BlockData>();
2240b57cec5SDimitry Andric   MachineInstr *InsertionPoint = nullptr;
2250b57cec5SDimitry Andric   // RequirePending is used to indicate whether we are collecting the initial
2260b57cec5SDimitry Andric   // requirements for the block, and need to defer the first InsertionPoint to
2270b57cec5SDimitry Andric   // Phase 3. It is set to false once we have set FirstInsertionPoint, or when
2280b57cec5SDimitry Andric   // we discover an explict setreg that means this block doesn't have any
2290b57cec5SDimitry Andric   // initial requirements.
2300b57cec5SDimitry Andric   bool RequirePending = true;
2310b57cec5SDimitry Andric   Status IPChange;
2320b57cec5SDimitry Andric   for (MachineInstr &MI : MBB) {
2330b57cec5SDimitry Andric     Status InstrMode = getInstructionMode(MI, TII);
234*af732203SDimitry Andric     if (MI.getOpcode() == AMDGPU::S_SETREG_B32 ||
235*af732203SDimitry Andric         MI.getOpcode() == AMDGPU::S_SETREG_B32_mode ||
236*af732203SDimitry Andric         MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
237*af732203SDimitry Andric         MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32_mode) {
2380b57cec5SDimitry Andric       // We preserve any explicit mode register setreg instruction we encounter,
2390b57cec5SDimitry Andric       // as we assume it has been inserted by a higher authority (this is
2400b57cec5SDimitry Andric       // likely to be a very rare occurrence).
2410b57cec5SDimitry Andric       unsigned Dst = TII->getNamedOperand(MI, AMDGPU::OpName::simm16)->getImm();
2420b57cec5SDimitry Andric       if (((Dst & AMDGPU::Hwreg::ID_MASK_) >> AMDGPU::Hwreg::ID_SHIFT_) !=
2430b57cec5SDimitry Andric           AMDGPU::Hwreg::ID_MODE)
2440b57cec5SDimitry Andric         continue;
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric       unsigned Width = ((Dst & AMDGPU::Hwreg::WIDTH_M1_MASK_) >>
2470b57cec5SDimitry Andric                         AMDGPU::Hwreg::WIDTH_M1_SHIFT_) +
2480b57cec5SDimitry Andric                        1;
2490b57cec5SDimitry Andric       unsigned Offset =
2500b57cec5SDimitry Andric           (Dst & AMDGPU::Hwreg::OFFSET_MASK_) >> AMDGPU::Hwreg::OFFSET_SHIFT_;
2510b57cec5SDimitry Andric       unsigned Mask = ((1 << Width) - 1) << Offset;
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric       // If an InsertionPoint is set we will insert a setreg there.
2540b57cec5SDimitry Andric       if (InsertionPoint) {
2550b57cec5SDimitry Andric         insertSetreg(MBB, InsertionPoint, TII, IPChange.delta(NewInfo->Change));
2560b57cec5SDimitry Andric         InsertionPoint = nullptr;
2570b57cec5SDimitry Andric       }
2580b57cec5SDimitry Andric       // If this is an immediate then we know the value being set, but if it is
2590b57cec5SDimitry Andric       // not an immediate then we treat the modified bits of the mode register
2600b57cec5SDimitry Andric       // as unknown.
261*af732203SDimitry Andric       if (MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
262*af732203SDimitry Andric           MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32_mode) {
2630b57cec5SDimitry Andric         unsigned Val = TII->getNamedOperand(MI, AMDGPU::OpName::imm)->getImm();
2640b57cec5SDimitry Andric         unsigned Mode = (Val << Offset) & Mask;
2650b57cec5SDimitry Andric         Status Setreg = Status(Mask, Mode);
2660b57cec5SDimitry Andric         // If we haven't already set the initial requirements for the block we
2670b57cec5SDimitry Andric         // don't need to as the requirements start from this explicit setreg.
2680b57cec5SDimitry Andric         RequirePending = false;
2690b57cec5SDimitry Andric         NewInfo->Change = NewInfo->Change.merge(Setreg);
2700b57cec5SDimitry Andric       } else {
2710b57cec5SDimitry Andric         NewInfo->Change = NewInfo->Change.mergeUnknown(Mask);
2720b57cec5SDimitry Andric       }
2730b57cec5SDimitry Andric     } else if (!NewInfo->Change.isCompatible(InstrMode)) {
2740b57cec5SDimitry Andric       // This instruction uses the Mode register and its requirements aren't
2750b57cec5SDimitry Andric       // compatible with the current mode.
2760b57cec5SDimitry Andric       if (InsertionPoint) {
2770b57cec5SDimitry Andric         // If the required mode change cannot be included in the current
2780b57cec5SDimitry Andric         // InsertionPoint changes, we need a setreg and start a new
2790b57cec5SDimitry Andric         // InsertionPoint.
2800b57cec5SDimitry Andric         if (!IPChange.delta(NewInfo->Change).isCombinable(InstrMode)) {
2810b57cec5SDimitry Andric           if (RequirePending) {
2820b57cec5SDimitry Andric             // This is the first insertionPoint in the block so we will defer
2830b57cec5SDimitry Andric             // the insertion of the setreg to Phase 3 where we know whether or
2840b57cec5SDimitry Andric             // not it is actually needed.
2850b57cec5SDimitry Andric             NewInfo->FirstInsertionPoint = InsertionPoint;
2860b57cec5SDimitry Andric             NewInfo->Require = NewInfo->Change;
2870b57cec5SDimitry Andric             RequirePending = false;
2880b57cec5SDimitry Andric           } else {
2890b57cec5SDimitry Andric             insertSetreg(MBB, InsertionPoint, TII,
2900b57cec5SDimitry Andric                          IPChange.delta(NewInfo->Change));
2910b57cec5SDimitry Andric             IPChange = NewInfo->Change;
2920b57cec5SDimitry Andric           }
2930b57cec5SDimitry Andric           // Set the new InsertionPoint
2940b57cec5SDimitry Andric           InsertionPoint = &MI;
2950b57cec5SDimitry Andric         }
2960b57cec5SDimitry Andric         NewInfo->Change = NewInfo->Change.merge(InstrMode);
2970b57cec5SDimitry Andric       } else {
2980b57cec5SDimitry Andric         // No InsertionPoint is currently set - this is either the first in
2990b57cec5SDimitry Andric         // the block or we have previously seen an explicit setreg.
3000b57cec5SDimitry Andric         InsertionPoint = &MI;
3010b57cec5SDimitry Andric         IPChange = NewInfo->Change;
3020b57cec5SDimitry Andric         NewInfo->Change = NewInfo->Change.merge(InstrMode);
3030b57cec5SDimitry Andric       }
3040b57cec5SDimitry Andric     }
3050b57cec5SDimitry Andric   }
3060b57cec5SDimitry Andric   if (RequirePending) {
3070b57cec5SDimitry Andric     // If we haven't yet set the initial requirements for the block we set them
3080b57cec5SDimitry Andric     // now.
3090b57cec5SDimitry Andric     NewInfo->FirstInsertionPoint = InsertionPoint;
3100b57cec5SDimitry Andric     NewInfo->Require = NewInfo->Change;
3110b57cec5SDimitry Andric   } else if (InsertionPoint) {
3120b57cec5SDimitry Andric     // We need to insert a setreg at the InsertionPoint
3130b57cec5SDimitry Andric     insertSetreg(MBB, InsertionPoint, TII, IPChange.delta(NewInfo->Change));
3140b57cec5SDimitry Andric   }
3150b57cec5SDimitry Andric   NewInfo->Exit = NewInfo->Change;
3160b57cec5SDimitry Andric   BlockInfo[MBB.getNumber()] = std::move(NewInfo);
3170b57cec5SDimitry Andric }
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric // In Phase 2 we revisit each block and calculate the common Mode register
3200b57cec5SDimitry Andric // value provided by all predecessor blocks. If the Exit value for the block
3210b57cec5SDimitry Andric // is changed, then we add the successor blocks to the worklist so that the
3220b57cec5SDimitry Andric // exit value is propagated.
processBlockPhase2(MachineBasicBlock & MBB,const SIInstrInfo * TII)3230b57cec5SDimitry Andric void SIModeRegister::processBlockPhase2(MachineBasicBlock &MBB,
3240b57cec5SDimitry Andric                                         const SIInstrInfo *TII) {
3255ffd83dbSDimitry Andric   bool RevisitRequired = false;
3265ffd83dbSDimitry Andric   bool ExitSet = false;
3270b57cec5SDimitry Andric   unsigned ThisBlock = MBB.getNumber();
3280b57cec5SDimitry Andric   if (MBB.pred_empty()) {
3290b57cec5SDimitry Andric     // There are no predecessors, so use the default starting status.
3300b57cec5SDimitry Andric     BlockInfo[ThisBlock]->Pred = DefaultStatus;
3315ffd83dbSDimitry Andric     ExitSet = true;
3320b57cec5SDimitry Andric   } else {
3330b57cec5SDimitry Andric     // Build a status that is common to all the predecessors by intersecting
3340b57cec5SDimitry Andric     // all the predecessor exit status values.
3355ffd83dbSDimitry Andric     // Mask bits (which represent the Mode bits with a known value) can only be
3365ffd83dbSDimitry Andric     // added by explicit SETREG instructions or the initial default value -
3375ffd83dbSDimitry Andric     // the intersection process may remove Mask bits.
3385ffd83dbSDimitry Andric     // If we find a predecessor that has not yet had an exit value determined
3395ffd83dbSDimitry Andric     // (this can happen for example if a block is its own predecessor) we defer
3405ffd83dbSDimitry Andric     // use of that value as the Mask will be all zero, and we will revisit this
3415ffd83dbSDimitry Andric     // block again later (unless the only predecessor without an exit value is
3425ffd83dbSDimitry Andric     // this block).
3430b57cec5SDimitry Andric     MachineBasicBlock::pred_iterator P = MBB.pred_begin(), E = MBB.pred_end();
3440b57cec5SDimitry Andric     MachineBasicBlock &PB = *(*P);
3455ffd83dbSDimitry Andric     unsigned PredBlock = PB.getNumber();
3465ffd83dbSDimitry Andric     if ((ThisBlock == PredBlock) && (std::next(P) == E)) {
3475ffd83dbSDimitry Andric       BlockInfo[ThisBlock]->Pred = DefaultStatus;
3485ffd83dbSDimitry Andric       ExitSet = true;
3495ffd83dbSDimitry Andric     } else if (BlockInfo[PredBlock]->ExitSet) {
3505ffd83dbSDimitry Andric       BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
3515ffd83dbSDimitry Andric       ExitSet = true;
3525ffd83dbSDimitry Andric     } else if (PredBlock != ThisBlock)
3535ffd83dbSDimitry Andric       RevisitRequired = true;
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric     for (P = std::next(P); P != E; P = std::next(P)) {
3560b57cec5SDimitry Andric       MachineBasicBlock *Pred = *P;
3575ffd83dbSDimitry Andric       unsigned PredBlock = Pred->getNumber();
3585ffd83dbSDimitry Andric       if (BlockInfo[PredBlock]->ExitSet) {
3595ffd83dbSDimitry Andric         if (BlockInfo[ThisBlock]->ExitSet) {
3605ffd83dbSDimitry Andric           BlockInfo[ThisBlock]->Pred =
3615ffd83dbSDimitry Andric               BlockInfo[ThisBlock]->Pred.intersect(BlockInfo[PredBlock]->Exit);
3625ffd83dbSDimitry Andric         } else {
3635ffd83dbSDimitry Andric           BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
3645ffd83dbSDimitry Andric         }
3655ffd83dbSDimitry Andric         ExitSet = true;
3665ffd83dbSDimitry Andric       } else if (PredBlock != ThisBlock)
3675ffd83dbSDimitry Andric         RevisitRequired = true;
3680b57cec5SDimitry Andric     }
3690b57cec5SDimitry Andric   }
3705ffd83dbSDimitry Andric   Status TmpStatus =
3715ffd83dbSDimitry Andric       BlockInfo[ThisBlock]->Pred.merge(BlockInfo[ThisBlock]->Change);
3720b57cec5SDimitry Andric   if (BlockInfo[ThisBlock]->Exit != TmpStatus) {
3730b57cec5SDimitry Andric     BlockInfo[ThisBlock]->Exit = TmpStatus;
3740b57cec5SDimitry Andric     // Add the successors to the work list so we can propagate the changed exit
3750b57cec5SDimitry Andric     // status.
3760b57cec5SDimitry Andric     for (MachineBasicBlock::succ_iterator S = MBB.succ_begin(),
3770b57cec5SDimitry Andric                                           E = MBB.succ_end();
3780b57cec5SDimitry Andric          S != E; S = std::next(S)) {
3790b57cec5SDimitry Andric       MachineBasicBlock &B = *(*S);
3800b57cec5SDimitry Andric       Phase2List.push(&B);
3810b57cec5SDimitry Andric     }
3820b57cec5SDimitry Andric   }
3835ffd83dbSDimitry Andric   BlockInfo[ThisBlock]->ExitSet = ExitSet;
3845ffd83dbSDimitry Andric   if (RevisitRequired)
3855ffd83dbSDimitry Andric     Phase2List.push(&MBB);
3860b57cec5SDimitry Andric }
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric // In Phase 3 we revisit each block and if it has an insertion point defined we
3890b57cec5SDimitry Andric // check whether the predecessor mode meets the block's entry requirements. If
3900b57cec5SDimitry Andric // not we insert an appropriate setreg instruction to modify the Mode register.
processBlockPhase3(MachineBasicBlock & MBB,const SIInstrInfo * TII)3910b57cec5SDimitry Andric void SIModeRegister::processBlockPhase3(MachineBasicBlock &MBB,
3920b57cec5SDimitry Andric                                         const SIInstrInfo *TII) {
3930b57cec5SDimitry Andric   unsigned ThisBlock = MBB.getNumber();
3940b57cec5SDimitry Andric   if (!BlockInfo[ThisBlock]->Pred.isCompatible(BlockInfo[ThisBlock]->Require)) {
3955ffd83dbSDimitry Andric     Status Delta =
3965ffd83dbSDimitry Andric         BlockInfo[ThisBlock]->Pred.delta(BlockInfo[ThisBlock]->Require);
3970b57cec5SDimitry Andric     if (BlockInfo[ThisBlock]->FirstInsertionPoint)
3980b57cec5SDimitry Andric       insertSetreg(MBB, BlockInfo[ThisBlock]->FirstInsertionPoint, TII, Delta);
3990b57cec5SDimitry Andric     else
4000b57cec5SDimitry Andric       insertSetreg(MBB, &MBB.instr_front(), TII, Delta);
4010b57cec5SDimitry Andric   }
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)4040b57cec5SDimitry Andric bool SIModeRegister::runOnMachineFunction(MachineFunction &MF) {
4050b57cec5SDimitry Andric   BlockInfo.resize(MF.getNumBlockIDs());
4060b57cec5SDimitry Andric   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
4070b57cec5SDimitry Andric   const SIInstrInfo *TII = ST.getInstrInfo();
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   // Processing is performed in a number of phases
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   // Phase 1 - determine the initial mode required by each block, and add setreg
4120b57cec5SDimitry Andric   // instructions for intra block requirements.
4130b57cec5SDimitry Andric   for (MachineBasicBlock &BB : MF)
4140b57cec5SDimitry Andric     processBlockPhase1(BB, TII);
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric   // Phase 2 - determine the exit mode from each block. We add all blocks to the
4170b57cec5SDimitry Andric   // list here, but will also add any that need to be revisited during Phase 2
4180b57cec5SDimitry Andric   // processing.
4190b57cec5SDimitry Andric   for (MachineBasicBlock &BB : MF)
4200b57cec5SDimitry Andric     Phase2List.push(&BB);
4210b57cec5SDimitry Andric   while (!Phase2List.empty()) {
4220b57cec5SDimitry Andric     processBlockPhase2(*Phase2List.front(), TII);
4230b57cec5SDimitry Andric     Phase2List.pop();
4240b57cec5SDimitry Andric   }
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric   // Phase 3 - add an initial setreg to each block where the required entry mode
4270b57cec5SDimitry Andric   // is not satisfied by the exit mode of all its predecessors.
4280b57cec5SDimitry Andric   for (MachineBasicBlock &BB : MF)
4290b57cec5SDimitry Andric     processBlockPhase3(BB, TII);
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   BlockInfo.clear();
4320b57cec5SDimitry Andric 
4335ffd83dbSDimitry Andric   return Changed;
4340b57cec5SDimitry Andric }
435