xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/SIModeRegister.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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"
17e8d8bef9SDimitry Andric #include "GCNSubtarget.h"
18e8d8bef9SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
190b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
2081ad6265SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
210b57cec5SDimitry Andric #include <queue>
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #define DEBUG_TYPE "si-mode-register"
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric STATISTIC(NumSetregInserted, "Number of setreg of mode register inserted.");
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric using namespace llvm;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric struct Status {
300b57cec5SDimitry Andric   // Mask is a bitmask where a '1' indicates the corresponding Mode bit has a
310b57cec5SDimitry Andric   // known value
3206c3fb27SDimitry Andric   unsigned Mask = 0;
3306c3fb27SDimitry Andric   unsigned Mode = 0;
340b57cec5SDimitry Andric 
3506c3fb27SDimitry Andric   Status() = default;
360b57cec5SDimitry Andric 
StatusStatus370b57cec5SDimitry Andric   Status(unsigned NewMask, unsigned NewMode) : Mask(NewMask), Mode(NewMode) {
380b57cec5SDimitry Andric     Mode &= Mask;
390b57cec5SDimitry Andric   };
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   // merge two status values such that only values that don't conflict are
420b57cec5SDimitry Andric   // preserved
mergeStatus430b57cec5SDimitry Andric   Status merge(const Status &S) const {
440b57cec5SDimitry Andric     return Status((Mask | S.Mask), ((Mode & ~S.Mask) | (S.Mode & S.Mask)));
450b57cec5SDimitry Andric   }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   // merge an unknown value by using the unknown value's mask to remove bits
480b57cec5SDimitry Andric   // from the result
mergeUnknownStatus490b57cec5SDimitry Andric   Status mergeUnknown(unsigned newMask) {
500b57cec5SDimitry Andric     return Status(Mask & ~newMask, Mode & ~newMask);
510b57cec5SDimitry Andric   }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   // intersect two Status values to produce a mode and mask that is a subset
540b57cec5SDimitry Andric   // of both values
intersectStatus550b57cec5SDimitry Andric   Status intersect(const Status &S) const {
560b57cec5SDimitry Andric     unsigned NewMask = (Mask & S.Mask) & (Mode ^ ~S.Mode);
570b57cec5SDimitry Andric     unsigned NewMode = (Mode & NewMask);
580b57cec5SDimitry Andric     return Status(NewMask, NewMode);
590b57cec5SDimitry Andric   }
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   // produce the delta required to change the Mode to the required Mode
deltaStatus620b57cec5SDimitry Andric   Status delta(const Status &S) const {
630b57cec5SDimitry Andric     return Status((S.Mask & (Mode ^ S.Mode)) | (~Mask & S.Mask), S.Mode);
640b57cec5SDimitry Andric   }
650b57cec5SDimitry Andric 
operator ==Status660b57cec5SDimitry Andric   bool operator==(const Status &S) const {
670b57cec5SDimitry Andric     return (Mask == S.Mask) && (Mode == S.Mode);
680b57cec5SDimitry Andric   }
690b57cec5SDimitry Andric 
operator !=Status700b57cec5SDimitry Andric   bool operator!=(const Status &S) const { return !(*this == S); }
710b57cec5SDimitry Andric 
isCompatibleStatus720b57cec5SDimitry Andric   bool isCompatible(Status &S) {
730b57cec5SDimitry Andric     return ((Mask & S.Mask) == S.Mask) && ((Mode & S.Mask) == S.Mode);
740b57cec5SDimitry Andric   }
750b57cec5SDimitry Andric 
isCombinableStatus765ffd83dbSDimitry Andric   bool isCombinable(Status &S) { return !(Mask & S.Mask) || isCompatible(S); }
770b57cec5SDimitry Andric };
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric class BlockData {
800b57cec5SDimitry Andric public:
810b57cec5SDimitry Andric   // The Status that represents the mode register settings required by the
820b57cec5SDimitry Andric   // FirstInsertionPoint (if any) in this block. Calculated in Phase 1.
830b57cec5SDimitry Andric   Status Require;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   // The Status that represents the net changes to the Mode register made by
860b57cec5SDimitry Andric   // this block, Calculated in Phase 1.
870b57cec5SDimitry Andric   Status Change;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   // The Status that represents the mode register settings on exit from this
900b57cec5SDimitry Andric   // block. Calculated in Phase 2.
910b57cec5SDimitry Andric   Status Exit;
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   // The Status that represents the intersection of exit Mode register settings
940b57cec5SDimitry Andric   // from all predecessor blocks. Calculated in Phase 2, and used by Phase 3.
950b57cec5SDimitry Andric   Status Pred;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   // In Phase 1 we record the first instruction that has a mode requirement,
980b57cec5SDimitry Andric   // which is used in Phase 3 if we need to insert a mode change.
9906c3fb27SDimitry Andric   MachineInstr *FirstInsertionPoint = nullptr;
1000b57cec5SDimitry Andric 
1015ffd83dbSDimitry Andric   // A flag to indicate whether an Exit value has been set (we can't tell by
1025ffd83dbSDimitry Andric   // examining the Exit value itself as all values may be valid results).
10306c3fb27SDimitry Andric   bool ExitSet = false;
1045ffd83dbSDimitry Andric 
10506c3fb27SDimitry Andric   BlockData() = default;
1060b57cec5SDimitry Andric };
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric namespace {
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric class SIModeRegister : public MachineFunctionPass {
1110b57cec5SDimitry Andric public:
1120b57cec5SDimitry Andric   static char ID;
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   std::vector<std::unique_ptr<BlockData>> BlockInfo;
1150b57cec5SDimitry Andric   std::queue<MachineBasicBlock *> Phase2List;
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   // The default mode register setting currently only caters for the floating
1180b57cec5SDimitry Andric   // point double precision rounding mode.
1190b57cec5SDimitry Andric   // We currently assume the default rounding mode is Round to Nearest
1200b57cec5SDimitry Andric   // NOTE: this should come from a per function rounding mode setting once such
1210b57cec5SDimitry Andric   // a setting exists.
1220b57cec5SDimitry Andric   unsigned DefaultMode = FP_ROUND_ROUND_TO_NEAREST;
1230b57cec5SDimitry Andric   Status DefaultStatus =
1240b57cec5SDimitry Andric       Status(FP_ROUND_MODE_DP(0x3), FP_ROUND_MODE_DP(DefaultMode));
1250b57cec5SDimitry Andric 
1265ffd83dbSDimitry Andric   bool Changed = false;
1275ffd83dbSDimitry Andric 
1280b57cec5SDimitry Andric public:
SIModeRegister()1290b57cec5SDimitry Andric   SIModeRegister() : MachineFunctionPass(ID) {}
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
1320b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const1330b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
1340b57cec5SDimitry Andric     AU.setPreservesCFG();
1350b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
1360b57cec5SDimitry Andric   }
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   void processBlockPhase1(MachineBasicBlock &MBB, const SIInstrInfo *TII);
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   void processBlockPhase2(MachineBasicBlock &MBB, const SIInstrInfo *TII);
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   void processBlockPhase3(MachineBasicBlock &MBB, const SIInstrInfo *TII);
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   Status getInstructionMode(MachineInstr &MI, const SIInstrInfo *TII);
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   void insertSetreg(MachineBasicBlock &MBB, MachineInstr *I,
1470b57cec5SDimitry Andric                     const SIInstrInfo *TII, Status InstrMode);
1480b57cec5SDimitry Andric };
1490b57cec5SDimitry Andric } // End anonymous namespace.
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric INITIALIZE_PASS(SIModeRegister, DEBUG_TYPE,
1520b57cec5SDimitry Andric                 "Insert required mode register values", false, false)
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric char SIModeRegister::ID = 0;
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric char &llvm::SIModeRegisterID = SIModeRegister::ID;
1570b57cec5SDimitry Andric 
createSIModeRegisterPass()1580b57cec5SDimitry Andric FunctionPass *llvm::createSIModeRegisterPass() { return new SIModeRegister(); }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric // Determine the Mode register setting required for this instruction.
1610b57cec5SDimitry Andric // Instructions which don't use the Mode register return a null Status.
1620b57cec5SDimitry Andric // Note this currently only deals with instructions that use the floating point
1630b57cec5SDimitry Andric // double precision setting.
getInstructionMode(MachineInstr & MI,const SIInstrInfo * TII)1640b57cec5SDimitry Andric Status SIModeRegister::getInstructionMode(MachineInstr &MI,
1650b57cec5SDimitry Andric                                           const SIInstrInfo *TII) {
16681ad6265SDimitry Andric   if (TII->usesFPDPRounding(MI) ||
16781ad6265SDimitry Andric       MI.getOpcode() == AMDGPU::FPTRUNC_UPWARD_PSEUDO ||
16881ad6265SDimitry Andric       MI.getOpcode() == AMDGPU::FPTRUNC_DOWNWARD_PSEUDO) {
1690b57cec5SDimitry Andric     switch (MI.getOpcode()) {
1700b57cec5SDimitry Andric     case AMDGPU::V_INTERP_P1LL_F16:
1710b57cec5SDimitry Andric     case AMDGPU::V_INTERP_P1LV_F16:
1720b57cec5SDimitry Andric     case AMDGPU::V_INTERP_P2_F16:
1730b57cec5SDimitry Andric       // f16 interpolation instructions need double precision round to zero
1740b57cec5SDimitry Andric       return Status(FP_ROUND_MODE_DP(3),
1750b57cec5SDimitry Andric                     FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_ZERO));
17681ad6265SDimitry Andric     case AMDGPU::FPTRUNC_UPWARD_PSEUDO: {
177bdd1243dSDimitry Andric       // Replacing the pseudo by a real instruction in place
178bdd1243dSDimitry Andric       if (TII->getSubtarget().hasTrue16BitInsts()) {
179bdd1243dSDimitry Andric         MachineBasicBlock &MBB = *MI.getParent();
180bdd1243dSDimitry Andric         MachineInstrBuilder B(*MBB.getParent(), MI);
181bdd1243dSDimitry Andric         MI.setDesc(TII->get(AMDGPU::V_CVT_F16_F32_t16_e64));
182bdd1243dSDimitry Andric         MachineOperand Src0 = MI.getOperand(1);
183bdd1243dSDimitry Andric         MI.removeOperand(1);
184bdd1243dSDimitry Andric         B.addImm(0); // src0_modifiers
185bdd1243dSDimitry Andric         B.add(Src0); // re-add src0 operand
186bdd1243dSDimitry Andric         B.addImm(0); // clamp
187bdd1243dSDimitry Andric         B.addImm(0); // omod
188bdd1243dSDimitry Andric       } else
18981ad6265SDimitry Andric         MI.setDesc(TII->get(AMDGPU::V_CVT_F16_F32_e32));
19081ad6265SDimitry Andric       return Status(FP_ROUND_MODE_DP(3),
19181ad6265SDimitry Andric                     FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_INF));
19281ad6265SDimitry Andric     }
19381ad6265SDimitry Andric     case AMDGPU::FPTRUNC_DOWNWARD_PSEUDO: {
194bdd1243dSDimitry Andric       // Replacing the pseudo by a real instruction in place
195bdd1243dSDimitry Andric       if (TII->getSubtarget().hasTrue16BitInsts()) {
196bdd1243dSDimitry Andric         MachineBasicBlock &MBB = *MI.getParent();
197bdd1243dSDimitry Andric         MachineInstrBuilder B(*MBB.getParent(), MI);
198bdd1243dSDimitry Andric         MI.setDesc(TII->get(AMDGPU::V_CVT_F16_F32_t16_e64));
199bdd1243dSDimitry Andric         MachineOperand Src0 = MI.getOperand(1);
200bdd1243dSDimitry Andric         MI.removeOperand(1);
201bdd1243dSDimitry Andric         B.addImm(0); // src0_modifiers
202bdd1243dSDimitry Andric         B.add(Src0); // re-add src0 operand
203bdd1243dSDimitry Andric         B.addImm(0); // clamp
204bdd1243dSDimitry Andric         B.addImm(0); // omod
205bdd1243dSDimitry Andric       } else
20681ad6265SDimitry Andric         MI.setDesc(TII->get(AMDGPU::V_CVT_F16_F32_e32));
20781ad6265SDimitry Andric       return Status(FP_ROUND_MODE_DP(3),
20881ad6265SDimitry Andric                     FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEGINF));
20981ad6265SDimitry Andric     }
2100b57cec5SDimitry Andric     default:
2110b57cec5SDimitry Andric       return DefaultStatus;
2120b57cec5SDimitry Andric     }
2130b57cec5SDimitry Andric   }
2140b57cec5SDimitry Andric   return Status();
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric // Insert a setreg instruction to update the Mode register.
2180b57cec5SDimitry Andric // It is possible (though unlikely) for an instruction to require a change to
2190b57cec5SDimitry Andric // the value of disjoint parts of the Mode register when we don't know the
2200b57cec5SDimitry Andric // value of the intervening bits. In that case we need to use more than one
2210b57cec5SDimitry Andric // setreg instruction.
insertSetreg(MachineBasicBlock & MBB,MachineInstr * MI,const SIInstrInfo * TII,Status InstrMode)2220b57cec5SDimitry Andric void SIModeRegister::insertSetreg(MachineBasicBlock &MBB, MachineInstr *MI,
2230b57cec5SDimitry Andric                                   const SIInstrInfo *TII, Status InstrMode) {
2240b57cec5SDimitry Andric   while (InstrMode.Mask) {
22506c3fb27SDimitry Andric     unsigned Offset = llvm::countr_zero<unsigned>(InstrMode.Mask);
22606c3fb27SDimitry Andric     unsigned Width = llvm::countr_one<unsigned>(InstrMode.Mask >> Offset);
2270b57cec5SDimitry Andric     unsigned Value = (InstrMode.Mode >> Offset) & ((1 << Width) - 1);
228*0fca6ea1SDimitry Andric     using namespace AMDGPU::Hwreg;
22904eeddc0SDimitry Andric     BuildMI(MBB, MI, nullptr, TII->get(AMDGPU::S_SETREG_IMM32_B32))
2300b57cec5SDimitry Andric         .addImm(Value)
231*0fca6ea1SDimitry Andric         .addImm(HwregEncoding::encode(ID_MODE, Offset, Width));
2320b57cec5SDimitry Andric     ++NumSetregInserted;
2335ffd83dbSDimitry Andric     Changed = true;
2340b57cec5SDimitry Andric     InstrMode.Mask &= ~(((1 << Width) - 1) << Offset);
2350b57cec5SDimitry Andric   }
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric // In Phase 1 we iterate through the instructions of the block and for each
2390b57cec5SDimitry Andric // instruction we get its mode usage. If the instruction uses the Mode register
2400b57cec5SDimitry Andric // we:
2410b57cec5SDimitry Andric // - update the Change status, which tracks the changes to the Mode register
2420b57cec5SDimitry Andric //   made by this block
2430b57cec5SDimitry Andric // - if this instruction's requirements are compatible with the current setting
2440b57cec5SDimitry Andric //   of the Mode register we merge the modes
2450b57cec5SDimitry Andric // - if it isn't compatible and an InsertionPoint isn't set, then we set the
2460b57cec5SDimitry Andric //   InsertionPoint to the current instruction, and we remember the current
2470b57cec5SDimitry Andric //   mode
2480b57cec5SDimitry Andric // - if it isn't compatible and InsertionPoint is set we insert a seteg before
2490b57cec5SDimitry Andric //   that instruction (unless this instruction forms part of the block's
2500b57cec5SDimitry Andric //   entry requirements in which case the insertion is deferred until Phase 3
2510b57cec5SDimitry Andric //   when predecessor exit values are known), and move the insertion point to
2520b57cec5SDimitry Andric //   this instruction
2530b57cec5SDimitry Andric // - if this is a setreg instruction we treat it as an incompatible instruction.
2540b57cec5SDimitry Andric //   This is sub-optimal but avoids some nasty corner cases, and is expected to
2550b57cec5SDimitry Andric //   occur very rarely.
2560b57cec5SDimitry Andric // - on exit we have set the Require, Change, and initial Exit modes.
processBlockPhase1(MachineBasicBlock & MBB,const SIInstrInfo * TII)2570b57cec5SDimitry Andric void SIModeRegister::processBlockPhase1(MachineBasicBlock &MBB,
2580b57cec5SDimitry Andric                                         const SIInstrInfo *TII) {
2598bcb0991SDimitry Andric   auto NewInfo = std::make_unique<BlockData>();
2600b57cec5SDimitry Andric   MachineInstr *InsertionPoint = nullptr;
2610b57cec5SDimitry Andric   // RequirePending is used to indicate whether we are collecting the initial
2620b57cec5SDimitry Andric   // requirements for the block, and need to defer the first InsertionPoint to
2630b57cec5SDimitry Andric   // Phase 3. It is set to false once we have set FirstInsertionPoint, or when
264349cc55cSDimitry Andric   // we discover an explicit setreg that means this block doesn't have any
2650b57cec5SDimitry Andric   // initial requirements.
2660b57cec5SDimitry Andric   bool RequirePending = true;
2670b57cec5SDimitry Andric   Status IPChange;
2680b57cec5SDimitry Andric   for (MachineInstr &MI : MBB) {
2690b57cec5SDimitry Andric     Status InstrMode = getInstructionMode(MI, TII);
270e8d8bef9SDimitry Andric     if (MI.getOpcode() == AMDGPU::S_SETREG_B32 ||
271e8d8bef9SDimitry Andric         MI.getOpcode() == AMDGPU::S_SETREG_B32_mode ||
272e8d8bef9SDimitry Andric         MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
273e8d8bef9SDimitry Andric         MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32_mode) {
2740b57cec5SDimitry Andric       // We preserve any explicit mode register setreg instruction we encounter,
2750b57cec5SDimitry Andric       // as we assume it has been inserted by a higher authority (this is
2760b57cec5SDimitry Andric       // likely to be a very rare occurrence).
2770b57cec5SDimitry Andric       unsigned Dst = TII->getNamedOperand(MI, AMDGPU::OpName::simm16)->getImm();
278*0fca6ea1SDimitry Andric       using namespace AMDGPU::Hwreg;
279*0fca6ea1SDimitry Andric       auto [Id, Offset, Width] = HwregEncoding::decode(Dst);
280*0fca6ea1SDimitry Andric       if (Id != ID_MODE)
2810b57cec5SDimitry Andric         continue;
2820b57cec5SDimitry Andric 
2837a6dacacSDimitry Andric       unsigned Mask = maskTrailingOnes<unsigned>(Width) << Offset;
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric       // If an InsertionPoint is set we will insert a setreg there.
2860b57cec5SDimitry Andric       if (InsertionPoint) {
2870b57cec5SDimitry Andric         insertSetreg(MBB, InsertionPoint, TII, IPChange.delta(NewInfo->Change));
2880b57cec5SDimitry Andric         InsertionPoint = nullptr;
2890b57cec5SDimitry Andric       }
2900b57cec5SDimitry Andric       // If this is an immediate then we know the value being set, but if it is
2910b57cec5SDimitry Andric       // not an immediate then we treat the modified bits of the mode register
2920b57cec5SDimitry Andric       // as unknown.
293e8d8bef9SDimitry Andric       if (MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
294e8d8bef9SDimitry Andric           MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32_mode) {
2950b57cec5SDimitry Andric         unsigned Val = TII->getNamedOperand(MI, AMDGPU::OpName::imm)->getImm();
2960b57cec5SDimitry Andric         unsigned Mode = (Val << Offset) & Mask;
2970b57cec5SDimitry Andric         Status Setreg = Status(Mask, Mode);
2980b57cec5SDimitry Andric         // If we haven't already set the initial requirements for the block we
2990b57cec5SDimitry Andric         // don't need to as the requirements start from this explicit setreg.
3000b57cec5SDimitry Andric         RequirePending = false;
3010b57cec5SDimitry Andric         NewInfo->Change = NewInfo->Change.merge(Setreg);
3020b57cec5SDimitry Andric       } else {
3030b57cec5SDimitry Andric         NewInfo->Change = NewInfo->Change.mergeUnknown(Mask);
3040b57cec5SDimitry Andric       }
3050b57cec5SDimitry Andric     } else if (!NewInfo->Change.isCompatible(InstrMode)) {
3060b57cec5SDimitry Andric       // This instruction uses the Mode register and its requirements aren't
3070b57cec5SDimitry Andric       // compatible with the current mode.
3080b57cec5SDimitry Andric       if (InsertionPoint) {
3090b57cec5SDimitry Andric         // If the required mode change cannot be included in the current
3100b57cec5SDimitry Andric         // InsertionPoint changes, we need a setreg and start a new
3110b57cec5SDimitry Andric         // InsertionPoint.
3120b57cec5SDimitry Andric         if (!IPChange.delta(NewInfo->Change).isCombinable(InstrMode)) {
3130b57cec5SDimitry Andric           if (RequirePending) {
3140b57cec5SDimitry Andric             // This is the first insertionPoint in the block so we will defer
3150b57cec5SDimitry Andric             // the insertion of the setreg to Phase 3 where we know whether or
3160b57cec5SDimitry Andric             // not it is actually needed.
3170b57cec5SDimitry Andric             NewInfo->FirstInsertionPoint = InsertionPoint;
3180b57cec5SDimitry Andric             NewInfo->Require = NewInfo->Change;
3190b57cec5SDimitry Andric             RequirePending = false;
3200b57cec5SDimitry Andric           } else {
3210b57cec5SDimitry Andric             insertSetreg(MBB, InsertionPoint, TII,
3220b57cec5SDimitry Andric                          IPChange.delta(NewInfo->Change));
3230b57cec5SDimitry Andric             IPChange = NewInfo->Change;
3240b57cec5SDimitry Andric           }
3250b57cec5SDimitry Andric           // Set the new InsertionPoint
3260b57cec5SDimitry Andric           InsertionPoint = &MI;
3270b57cec5SDimitry Andric         }
3280b57cec5SDimitry Andric         NewInfo->Change = NewInfo->Change.merge(InstrMode);
3290b57cec5SDimitry Andric       } else {
3300b57cec5SDimitry Andric         // No InsertionPoint is currently set - this is either the first in
3310b57cec5SDimitry Andric         // the block or we have previously seen an explicit setreg.
3320b57cec5SDimitry Andric         InsertionPoint = &MI;
3330b57cec5SDimitry Andric         IPChange = NewInfo->Change;
3340b57cec5SDimitry Andric         NewInfo->Change = NewInfo->Change.merge(InstrMode);
3350b57cec5SDimitry Andric       }
3360b57cec5SDimitry Andric     }
3370b57cec5SDimitry Andric   }
3380b57cec5SDimitry Andric   if (RequirePending) {
3390b57cec5SDimitry Andric     // If we haven't yet set the initial requirements for the block we set them
3400b57cec5SDimitry Andric     // now.
3410b57cec5SDimitry Andric     NewInfo->FirstInsertionPoint = InsertionPoint;
3420b57cec5SDimitry Andric     NewInfo->Require = NewInfo->Change;
3430b57cec5SDimitry Andric   } else if (InsertionPoint) {
3440b57cec5SDimitry Andric     // We need to insert a setreg at the InsertionPoint
3450b57cec5SDimitry Andric     insertSetreg(MBB, InsertionPoint, TII, IPChange.delta(NewInfo->Change));
3460b57cec5SDimitry Andric   }
3470b57cec5SDimitry Andric   NewInfo->Exit = NewInfo->Change;
3480b57cec5SDimitry Andric   BlockInfo[MBB.getNumber()] = std::move(NewInfo);
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric // In Phase 2 we revisit each block and calculate the common Mode register
3520b57cec5SDimitry Andric // value provided by all predecessor blocks. If the Exit value for the block
3530b57cec5SDimitry Andric // is changed, then we add the successor blocks to the worklist so that the
3540b57cec5SDimitry Andric // exit value is propagated.
processBlockPhase2(MachineBasicBlock & MBB,const SIInstrInfo * TII)3550b57cec5SDimitry Andric void SIModeRegister::processBlockPhase2(MachineBasicBlock &MBB,
3560b57cec5SDimitry Andric                                         const SIInstrInfo *TII) {
3575ffd83dbSDimitry Andric   bool RevisitRequired = false;
3585ffd83dbSDimitry Andric   bool ExitSet = false;
3590b57cec5SDimitry Andric   unsigned ThisBlock = MBB.getNumber();
3600b57cec5SDimitry Andric   if (MBB.pred_empty()) {
3610b57cec5SDimitry Andric     // There are no predecessors, so use the default starting status.
3620b57cec5SDimitry Andric     BlockInfo[ThisBlock]->Pred = DefaultStatus;
3635ffd83dbSDimitry Andric     ExitSet = true;
3640b57cec5SDimitry Andric   } else {
3650b57cec5SDimitry Andric     // Build a status that is common to all the predecessors by intersecting
3660b57cec5SDimitry Andric     // all the predecessor exit status values.
3675ffd83dbSDimitry Andric     // Mask bits (which represent the Mode bits with a known value) can only be
3685ffd83dbSDimitry Andric     // added by explicit SETREG instructions or the initial default value -
3695ffd83dbSDimitry Andric     // the intersection process may remove Mask bits.
3705ffd83dbSDimitry Andric     // If we find a predecessor that has not yet had an exit value determined
3715ffd83dbSDimitry Andric     // (this can happen for example if a block is its own predecessor) we defer
3725ffd83dbSDimitry Andric     // use of that value as the Mask will be all zero, and we will revisit this
3735ffd83dbSDimitry Andric     // block again later (unless the only predecessor without an exit value is
3745ffd83dbSDimitry Andric     // this block).
3750b57cec5SDimitry Andric     MachineBasicBlock::pred_iterator P = MBB.pred_begin(), E = MBB.pred_end();
3760b57cec5SDimitry Andric     MachineBasicBlock &PB = *(*P);
3775ffd83dbSDimitry Andric     unsigned PredBlock = PB.getNumber();
3785ffd83dbSDimitry Andric     if ((ThisBlock == PredBlock) && (std::next(P) == E)) {
3795ffd83dbSDimitry Andric       BlockInfo[ThisBlock]->Pred = DefaultStatus;
3805ffd83dbSDimitry Andric       ExitSet = true;
3815ffd83dbSDimitry Andric     } else if (BlockInfo[PredBlock]->ExitSet) {
3825ffd83dbSDimitry Andric       BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
3835ffd83dbSDimitry Andric       ExitSet = true;
3845ffd83dbSDimitry Andric     } else if (PredBlock != ThisBlock)
3855ffd83dbSDimitry Andric       RevisitRequired = true;
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric     for (P = std::next(P); P != E; P = std::next(P)) {
3880b57cec5SDimitry Andric       MachineBasicBlock *Pred = *P;
3895ffd83dbSDimitry Andric       unsigned PredBlock = Pred->getNumber();
3905ffd83dbSDimitry Andric       if (BlockInfo[PredBlock]->ExitSet) {
3915ffd83dbSDimitry Andric         if (BlockInfo[ThisBlock]->ExitSet) {
3925ffd83dbSDimitry Andric           BlockInfo[ThisBlock]->Pred =
3935ffd83dbSDimitry Andric               BlockInfo[ThisBlock]->Pred.intersect(BlockInfo[PredBlock]->Exit);
3945ffd83dbSDimitry Andric         } else {
3955ffd83dbSDimitry Andric           BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
3965ffd83dbSDimitry Andric         }
3975ffd83dbSDimitry Andric         ExitSet = true;
3985ffd83dbSDimitry Andric       } else if (PredBlock != ThisBlock)
3995ffd83dbSDimitry Andric         RevisitRequired = true;
4000b57cec5SDimitry Andric     }
4010b57cec5SDimitry Andric   }
4025ffd83dbSDimitry Andric   Status TmpStatus =
4035ffd83dbSDimitry Andric       BlockInfo[ThisBlock]->Pred.merge(BlockInfo[ThisBlock]->Change);
4040b57cec5SDimitry Andric   if (BlockInfo[ThisBlock]->Exit != TmpStatus) {
4050b57cec5SDimitry Andric     BlockInfo[ThisBlock]->Exit = TmpStatus;
4060b57cec5SDimitry Andric     // Add the successors to the work list so we can propagate the changed exit
4070b57cec5SDimitry Andric     // status.
408349cc55cSDimitry Andric     for (MachineBasicBlock *Succ : MBB.successors())
409349cc55cSDimitry Andric       Phase2List.push(Succ);
4100b57cec5SDimitry Andric   }
4115ffd83dbSDimitry Andric   BlockInfo[ThisBlock]->ExitSet = ExitSet;
4125ffd83dbSDimitry Andric   if (RevisitRequired)
4135ffd83dbSDimitry Andric     Phase2List.push(&MBB);
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric // In Phase 3 we revisit each block and if it has an insertion point defined we
4170b57cec5SDimitry Andric // check whether the predecessor mode meets the block's entry requirements. If
4180b57cec5SDimitry Andric // not we insert an appropriate setreg instruction to modify the Mode register.
processBlockPhase3(MachineBasicBlock & MBB,const SIInstrInfo * TII)4190b57cec5SDimitry Andric void SIModeRegister::processBlockPhase3(MachineBasicBlock &MBB,
4200b57cec5SDimitry Andric                                         const SIInstrInfo *TII) {
4210b57cec5SDimitry Andric   unsigned ThisBlock = MBB.getNumber();
4220b57cec5SDimitry Andric   if (!BlockInfo[ThisBlock]->Pred.isCompatible(BlockInfo[ThisBlock]->Require)) {
4235ffd83dbSDimitry Andric     Status Delta =
4245ffd83dbSDimitry Andric         BlockInfo[ThisBlock]->Pred.delta(BlockInfo[ThisBlock]->Require);
4250b57cec5SDimitry Andric     if (BlockInfo[ThisBlock]->FirstInsertionPoint)
4260b57cec5SDimitry Andric       insertSetreg(MBB, BlockInfo[ThisBlock]->FirstInsertionPoint, TII, Delta);
4270b57cec5SDimitry Andric     else
4280b57cec5SDimitry Andric       insertSetreg(MBB, &MBB.instr_front(), TII, Delta);
4290b57cec5SDimitry Andric   }
4300b57cec5SDimitry Andric }
4310b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)4320b57cec5SDimitry Andric bool SIModeRegister::runOnMachineFunction(MachineFunction &MF) {
433*0fca6ea1SDimitry Andric   // Constrained FP intrinsics are used to support non-default rounding modes.
434*0fca6ea1SDimitry Andric   // strictfp attribute is required to mark functions with strict FP semantics
435*0fca6ea1SDimitry Andric   // having constrained FP intrinsics. This pass fixes up operations that uses
436*0fca6ea1SDimitry Andric   // a non-default rounding mode for non-strictfp functions. But it should not
437*0fca6ea1SDimitry Andric   // assume or modify any default rounding modes in case of strictfp functions.
438*0fca6ea1SDimitry Andric   const Function &F = MF.getFunction();
439*0fca6ea1SDimitry Andric   if (F.hasFnAttribute(llvm::Attribute::StrictFP))
440*0fca6ea1SDimitry Andric     return Changed;
4410b57cec5SDimitry Andric   BlockInfo.resize(MF.getNumBlockIDs());
4420b57cec5SDimitry Andric   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
4430b57cec5SDimitry Andric   const SIInstrInfo *TII = ST.getInstrInfo();
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric   // Processing is performed in a number of phases
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric   // Phase 1 - determine the initial mode required by each block, and add setreg
4480b57cec5SDimitry Andric   // instructions for intra block requirements.
4490b57cec5SDimitry Andric   for (MachineBasicBlock &BB : MF)
4500b57cec5SDimitry Andric     processBlockPhase1(BB, TII);
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric   // Phase 2 - determine the exit mode from each block. We add all blocks to the
4530b57cec5SDimitry Andric   // list here, but will also add any that need to be revisited during Phase 2
4540b57cec5SDimitry Andric   // processing.
4550b57cec5SDimitry Andric   for (MachineBasicBlock &BB : MF)
4560b57cec5SDimitry Andric     Phase2List.push(&BB);
4570b57cec5SDimitry Andric   while (!Phase2List.empty()) {
4580b57cec5SDimitry Andric     processBlockPhase2(*Phase2List.front(), TII);
4590b57cec5SDimitry Andric     Phase2List.pop();
4600b57cec5SDimitry Andric   }
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric   // Phase 3 - add an initial setreg to each block where the required entry mode
4630b57cec5SDimitry Andric   // is not satisfied by the exit mode of all its predecessors.
4640b57cec5SDimitry Andric   for (MachineBasicBlock &BB : MF)
4650b57cec5SDimitry Andric     processBlockPhase3(BB, TII);
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   BlockInfo.clear();
4680b57cec5SDimitry Andric 
4695ffd83dbSDimitry Andric   return Changed;
4700b57cec5SDimitry Andric }
471