xref: /src/contrib/llvm-project/llvm/lib/Target/AMDGPU/SIModeRegister.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1d8e91e46SDimitry Andric //===-- SIModeRegister.cpp - Mode Register --------------------------------===//
2d8e91e46SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d8e91e46SDimitry Andric //
7d8e91e46SDimitry Andric //===----------------------------------------------------------------------===//
8d8e91e46SDimitry Andric /// \file
9d8e91e46SDimitry Andric /// This pass inserts changes to the Mode register settings as required.
10d8e91e46SDimitry Andric /// Note that currently it only deals with the Double Precision Floating Point
11d8e91e46SDimitry Andric /// rounding mode setting, but is intended to be generic enough to be easily
12d8e91e46SDimitry Andric /// expanded.
13d8e91e46SDimitry Andric ///
14d8e91e46SDimitry Andric //===----------------------------------------------------------------------===//
15d8e91e46SDimitry Andric //
16d8e91e46SDimitry Andric #include "AMDGPU.h"
17b60736ecSDimitry Andric #include "GCNSubtarget.h"
18b60736ecSDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
19d8e91e46SDimitry Andric #include "llvm/ADT/Statistic.h"
20145449b1SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
21d8e91e46SDimitry Andric #include <queue>
22d8e91e46SDimitry Andric 
23d8e91e46SDimitry Andric #define DEBUG_TYPE "si-mode-register"
24d8e91e46SDimitry Andric 
25d8e91e46SDimitry Andric STATISTIC(NumSetregInserted, "Number of setreg of mode register inserted.");
26d8e91e46SDimitry Andric 
27d8e91e46SDimitry Andric using namespace llvm;
28d8e91e46SDimitry Andric 
29d8e91e46SDimitry Andric struct Status {
30d8e91e46SDimitry Andric   // Mask is a bitmask where a '1' indicates the corresponding Mode bit has a
31d8e91e46SDimitry Andric   // known value
327fa27ce4SDimitry Andric   unsigned Mask = 0;
337fa27ce4SDimitry Andric   unsigned Mode = 0;
34d8e91e46SDimitry Andric 
357fa27ce4SDimitry Andric   Status() = default;
36d8e91e46SDimitry Andric 
StatusStatus37e6d15924SDimitry Andric   Status(unsigned NewMask, unsigned NewMode) : Mask(NewMask), Mode(NewMode) {
38d8e91e46SDimitry Andric     Mode &= Mask;
39d8e91e46SDimitry Andric   };
40d8e91e46SDimitry Andric 
41d8e91e46SDimitry Andric   // merge two status values such that only values that don't conflict are
42d8e91e46SDimitry Andric   // preserved
mergeStatus43d8e91e46SDimitry Andric   Status merge(const Status &S) const {
44d8e91e46SDimitry Andric     return Status((Mask | S.Mask), ((Mode & ~S.Mask) | (S.Mode & S.Mask)));
45d8e91e46SDimitry Andric   }
46d8e91e46SDimitry Andric 
47d8e91e46SDimitry Andric   // merge an unknown value by using the unknown value's mask to remove bits
48d8e91e46SDimitry Andric   // from the result
mergeUnknownStatus49d8e91e46SDimitry Andric   Status mergeUnknown(unsigned newMask) {
50d8e91e46SDimitry Andric     return Status(Mask & ~newMask, Mode & ~newMask);
51d8e91e46SDimitry Andric   }
52d8e91e46SDimitry Andric 
53d8e91e46SDimitry Andric   // intersect two Status values to produce a mode and mask that is a subset
54d8e91e46SDimitry Andric   // of both values
intersectStatus55d8e91e46SDimitry Andric   Status intersect(const Status &S) const {
56d8e91e46SDimitry Andric     unsigned NewMask = (Mask & S.Mask) & (Mode ^ ~S.Mode);
57d8e91e46SDimitry Andric     unsigned NewMode = (Mode & NewMask);
58d8e91e46SDimitry Andric     return Status(NewMask, NewMode);
59d8e91e46SDimitry Andric   }
60d8e91e46SDimitry Andric 
61d8e91e46SDimitry Andric   // produce the delta required to change the Mode to the required Mode
deltaStatus62d8e91e46SDimitry Andric   Status delta(const Status &S) const {
63d8e91e46SDimitry Andric     return Status((S.Mask & (Mode ^ S.Mode)) | (~Mask & S.Mask), S.Mode);
64d8e91e46SDimitry Andric   }
65d8e91e46SDimitry Andric 
operator ==Status66d8e91e46SDimitry Andric   bool operator==(const Status &S) const {
67d8e91e46SDimitry Andric     return (Mask == S.Mask) && (Mode == S.Mode);
68d8e91e46SDimitry Andric   }
69d8e91e46SDimitry Andric 
operator !=Status70d8e91e46SDimitry Andric   bool operator!=(const Status &S) const { return !(*this == S); }
71d8e91e46SDimitry Andric 
isCompatibleStatus72d8e91e46SDimitry Andric   bool isCompatible(Status &S) {
73d8e91e46SDimitry Andric     return ((Mask & S.Mask) == S.Mask) && ((Mode & S.Mask) == S.Mode);
74d8e91e46SDimitry Andric   }
75d8e91e46SDimitry Andric 
isCombinableStatus76cfca06d7SDimitry Andric   bool isCombinable(Status &S) { return !(Mask & S.Mask) || isCompatible(S); }
77d8e91e46SDimitry Andric };
78d8e91e46SDimitry Andric 
79d8e91e46SDimitry Andric class BlockData {
80d8e91e46SDimitry Andric public:
81d8e91e46SDimitry Andric   // The Status that represents the mode register settings required by the
82d8e91e46SDimitry Andric   // FirstInsertionPoint (if any) in this block. Calculated in Phase 1.
83d8e91e46SDimitry Andric   Status Require;
84d8e91e46SDimitry Andric 
85d8e91e46SDimitry Andric   // The Status that represents the net changes to the Mode register made by
86d8e91e46SDimitry Andric   // this block, Calculated in Phase 1.
87d8e91e46SDimitry Andric   Status Change;
88d8e91e46SDimitry Andric 
89d8e91e46SDimitry Andric   // The Status that represents the mode register settings on exit from this
90d8e91e46SDimitry Andric   // block. Calculated in Phase 2.
91d8e91e46SDimitry Andric   Status Exit;
92d8e91e46SDimitry Andric 
93d8e91e46SDimitry Andric   // The Status that represents the intersection of exit Mode register settings
94d8e91e46SDimitry Andric   // from all predecessor blocks. Calculated in Phase 2, and used by Phase 3.
95d8e91e46SDimitry Andric   Status Pred;
96d8e91e46SDimitry Andric 
97d8e91e46SDimitry Andric   // In Phase 1 we record the first instruction that has a mode requirement,
98d8e91e46SDimitry Andric   // which is used in Phase 3 if we need to insert a mode change.
997fa27ce4SDimitry Andric   MachineInstr *FirstInsertionPoint = nullptr;
100d8e91e46SDimitry Andric 
101cfca06d7SDimitry Andric   // A flag to indicate whether an Exit value has been set (we can't tell by
102cfca06d7SDimitry Andric   // examining the Exit value itself as all values may be valid results).
1037fa27ce4SDimitry Andric   bool ExitSet = false;
104cfca06d7SDimitry Andric 
1057fa27ce4SDimitry Andric   BlockData() = default;
106d8e91e46SDimitry Andric };
107d8e91e46SDimitry Andric 
108d8e91e46SDimitry Andric namespace {
109d8e91e46SDimitry Andric 
110d8e91e46SDimitry Andric class SIModeRegister : public MachineFunctionPass {
111d8e91e46SDimitry Andric public:
112d8e91e46SDimitry Andric   static char ID;
113d8e91e46SDimitry Andric 
114d8e91e46SDimitry Andric   std::vector<std::unique_ptr<BlockData>> BlockInfo;
115d8e91e46SDimitry Andric   std::queue<MachineBasicBlock *> Phase2List;
116d8e91e46SDimitry Andric 
117d8e91e46SDimitry Andric   // The default mode register setting currently only caters for the floating
118d8e91e46SDimitry Andric   // point double precision rounding mode.
119d8e91e46SDimitry Andric   // We currently assume the default rounding mode is Round to Nearest
120d8e91e46SDimitry Andric   // NOTE: this should come from a per function rounding mode setting once such
121d8e91e46SDimitry Andric   // a setting exists.
122d8e91e46SDimitry Andric   unsigned DefaultMode = FP_ROUND_ROUND_TO_NEAREST;
123d8e91e46SDimitry Andric   Status DefaultStatus =
124d8e91e46SDimitry Andric       Status(FP_ROUND_MODE_DP(0x3), FP_ROUND_MODE_DP(DefaultMode));
125d8e91e46SDimitry Andric 
126cfca06d7SDimitry Andric   bool Changed = false;
127cfca06d7SDimitry Andric 
128d8e91e46SDimitry Andric public:
SIModeRegister()129d8e91e46SDimitry Andric   SIModeRegister() : MachineFunctionPass(ID) {}
130d8e91e46SDimitry Andric 
131d8e91e46SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
132d8e91e46SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const133d8e91e46SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
134d8e91e46SDimitry Andric     AU.setPreservesCFG();
135d8e91e46SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
136d8e91e46SDimitry Andric   }
137d8e91e46SDimitry Andric 
138d8e91e46SDimitry Andric   void processBlockPhase1(MachineBasicBlock &MBB, const SIInstrInfo *TII);
139d8e91e46SDimitry Andric 
140d8e91e46SDimitry Andric   void processBlockPhase2(MachineBasicBlock &MBB, const SIInstrInfo *TII);
141d8e91e46SDimitry Andric 
142d8e91e46SDimitry Andric   void processBlockPhase3(MachineBasicBlock &MBB, const SIInstrInfo *TII);
143d8e91e46SDimitry Andric 
144d8e91e46SDimitry Andric   Status getInstructionMode(MachineInstr &MI, const SIInstrInfo *TII);
145d8e91e46SDimitry Andric 
146d8e91e46SDimitry Andric   void insertSetreg(MachineBasicBlock &MBB, MachineInstr *I,
147d8e91e46SDimitry Andric                     const SIInstrInfo *TII, Status InstrMode);
148d8e91e46SDimitry Andric };
149d8e91e46SDimitry Andric } // End anonymous namespace.
150d8e91e46SDimitry Andric 
151d8e91e46SDimitry Andric INITIALIZE_PASS(SIModeRegister, DEBUG_TYPE,
152d8e91e46SDimitry Andric                 "Insert required mode register values", false, false)
153d8e91e46SDimitry Andric 
154d8e91e46SDimitry Andric char SIModeRegister::ID = 0;
155d8e91e46SDimitry Andric 
156d8e91e46SDimitry Andric char &llvm::SIModeRegisterID = SIModeRegister::ID;
157d8e91e46SDimitry Andric 
createSIModeRegisterPass()158d8e91e46SDimitry Andric FunctionPass *llvm::createSIModeRegisterPass() { return new SIModeRegister(); }
159d8e91e46SDimitry Andric 
160d8e91e46SDimitry Andric // Determine the Mode register setting required for this instruction.
161d8e91e46SDimitry Andric // Instructions which don't use the Mode register return a null Status.
162d8e91e46SDimitry Andric // Note this currently only deals with instructions that use the floating point
163d8e91e46SDimitry Andric // double precision setting.
getInstructionMode(MachineInstr & MI,const SIInstrInfo * TII)164d8e91e46SDimitry Andric Status SIModeRegister::getInstructionMode(MachineInstr &MI,
165d8e91e46SDimitry Andric                                           const SIInstrInfo *TII) {
166145449b1SDimitry Andric   if (TII->usesFPDPRounding(MI) ||
167145449b1SDimitry Andric       MI.getOpcode() == AMDGPU::FPTRUNC_UPWARD_PSEUDO ||
168145449b1SDimitry Andric       MI.getOpcode() == AMDGPU::FPTRUNC_DOWNWARD_PSEUDO) {
169d8e91e46SDimitry Andric     switch (MI.getOpcode()) {
170d8e91e46SDimitry Andric     case AMDGPU::V_INTERP_P1LL_F16:
171d8e91e46SDimitry Andric     case AMDGPU::V_INTERP_P1LV_F16:
172d8e91e46SDimitry Andric     case AMDGPU::V_INTERP_P2_F16:
173d8e91e46SDimitry Andric       // f16 interpolation instructions need double precision round to zero
174d8e91e46SDimitry Andric       return Status(FP_ROUND_MODE_DP(3),
175d8e91e46SDimitry Andric                     FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_ZERO));
176145449b1SDimitry Andric     case AMDGPU::FPTRUNC_UPWARD_PSEUDO: {
177e3b55780SDimitry Andric       // Replacing the pseudo by a real instruction in place
178e3b55780SDimitry Andric       if (TII->getSubtarget().hasTrue16BitInsts()) {
179e3b55780SDimitry Andric         MachineBasicBlock &MBB = *MI.getParent();
180e3b55780SDimitry Andric         MachineInstrBuilder B(*MBB.getParent(), MI);
181e3b55780SDimitry Andric         MI.setDesc(TII->get(AMDGPU::V_CVT_F16_F32_t16_e64));
182e3b55780SDimitry Andric         MachineOperand Src0 = MI.getOperand(1);
183e3b55780SDimitry Andric         MI.removeOperand(1);
184e3b55780SDimitry Andric         B.addImm(0); // src0_modifiers
185e3b55780SDimitry Andric         B.add(Src0); // re-add src0 operand
186e3b55780SDimitry Andric         B.addImm(0); // clamp
187e3b55780SDimitry Andric         B.addImm(0); // omod
188e3b55780SDimitry Andric       } else
189145449b1SDimitry Andric         MI.setDesc(TII->get(AMDGPU::V_CVT_F16_F32_e32));
190145449b1SDimitry Andric       return Status(FP_ROUND_MODE_DP(3),
191145449b1SDimitry Andric                     FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_INF));
192145449b1SDimitry Andric     }
193145449b1SDimitry Andric     case AMDGPU::FPTRUNC_DOWNWARD_PSEUDO: {
194e3b55780SDimitry Andric       // Replacing the pseudo by a real instruction in place
195e3b55780SDimitry Andric       if (TII->getSubtarget().hasTrue16BitInsts()) {
196e3b55780SDimitry Andric         MachineBasicBlock &MBB = *MI.getParent();
197e3b55780SDimitry Andric         MachineInstrBuilder B(*MBB.getParent(), MI);
198e3b55780SDimitry Andric         MI.setDesc(TII->get(AMDGPU::V_CVT_F16_F32_t16_e64));
199e3b55780SDimitry Andric         MachineOperand Src0 = MI.getOperand(1);
200e3b55780SDimitry Andric         MI.removeOperand(1);
201e3b55780SDimitry Andric         B.addImm(0); // src0_modifiers
202e3b55780SDimitry Andric         B.add(Src0); // re-add src0 operand
203e3b55780SDimitry Andric         B.addImm(0); // clamp
204e3b55780SDimitry Andric         B.addImm(0); // omod
205e3b55780SDimitry Andric       } else
206145449b1SDimitry Andric         MI.setDesc(TII->get(AMDGPU::V_CVT_F16_F32_e32));
207145449b1SDimitry Andric       return Status(FP_ROUND_MODE_DP(3),
208145449b1SDimitry Andric                     FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEGINF));
209145449b1SDimitry Andric     }
210d8e91e46SDimitry Andric     default:
211d8e91e46SDimitry Andric       return DefaultStatus;
212d8e91e46SDimitry Andric     }
213d8e91e46SDimitry Andric   }
214d8e91e46SDimitry Andric   return Status();
215d8e91e46SDimitry Andric }
216d8e91e46SDimitry Andric 
217d8e91e46SDimitry Andric // Insert a setreg instruction to update the Mode register.
218d8e91e46SDimitry Andric // It is possible (though unlikely) for an instruction to require a change to
219d8e91e46SDimitry Andric // the value of disjoint parts of the Mode register when we don't know the
220d8e91e46SDimitry Andric // value of the intervening bits. In that case we need to use more than one
221d8e91e46SDimitry Andric // setreg instruction.
insertSetreg(MachineBasicBlock & MBB,MachineInstr * MI,const SIInstrInfo * TII,Status InstrMode)222d8e91e46SDimitry Andric void SIModeRegister::insertSetreg(MachineBasicBlock &MBB, MachineInstr *MI,
223d8e91e46SDimitry Andric                                   const SIInstrInfo *TII, Status InstrMode) {
224d8e91e46SDimitry Andric   while (InstrMode.Mask) {
2257fa27ce4SDimitry Andric     unsigned Offset = llvm::countr_zero<unsigned>(InstrMode.Mask);
2267fa27ce4SDimitry Andric     unsigned Width = llvm::countr_one<unsigned>(InstrMode.Mask >> Offset);
227d8e91e46SDimitry Andric     unsigned Value = (InstrMode.Mode >> Offset) & ((1 << Width) - 1);
228ac9a064cSDimitry Andric     using namespace AMDGPU::Hwreg;
2296f8fc217SDimitry Andric     BuildMI(MBB, MI, nullptr, TII->get(AMDGPU::S_SETREG_IMM32_B32))
230d8e91e46SDimitry Andric         .addImm(Value)
231ac9a064cSDimitry Andric         .addImm(HwregEncoding::encode(ID_MODE, Offset, Width));
232d8e91e46SDimitry Andric     ++NumSetregInserted;
233cfca06d7SDimitry Andric     Changed = true;
234d8e91e46SDimitry Andric     InstrMode.Mask &= ~(((1 << Width) - 1) << Offset);
235d8e91e46SDimitry Andric   }
236d8e91e46SDimitry Andric }
237d8e91e46SDimitry Andric 
238d8e91e46SDimitry Andric // In Phase 1 we iterate through the instructions of the block and for each
239d8e91e46SDimitry Andric // instruction we get its mode usage. If the instruction uses the Mode register
240d8e91e46SDimitry Andric // we:
241d8e91e46SDimitry Andric // - update the Change status, which tracks the changes to the Mode register
242d8e91e46SDimitry Andric //   made by this block
243d8e91e46SDimitry Andric // - if this instruction's requirements are compatible with the current setting
244d8e91e46SDimitry Andric //   of the Mode register we merge the modes
245d8e91e46SDimitry Andric // - if it isn't compatible and an InsertionPoint isn't set, then we set the
246d8e91e46SDimitry Andric //   InsertionPoint to the current instruction, and we remember the current
247d8e91e46SDimitry Andric //   mode
248d8e91e46SDimitry Andric // - if it isn't compatible and InsertionPoint is set we insert a seteg before
249d8e91e46SDimitry Andric //   that instruction (unless this instruction forms part of the block's
250d8e91e46SDimitry Andric //   entry requirements in which case the insertion is deferred until Phase 3
251d8e91e46SDimitry Andric //   when predecessor exit values are known), and move the insertion point to
252d8e91e46SDimitry Andric //   this instruction
253d8e91e46SDimitry Andric // - if this is a setreg instruction we treat it as an incompatible instruction.
254d8e91e46SDimitry Andric //   This is sub-optimal but avoids some nasty corner cases, and is expected to
255d8e91e46SDimitry Andric //   occur very rarely.
256d8e91e46SDimitry Andric // - on exit we have set the Require, Change, and initial Exit modes.
processBlockPhase1(MachineBasicBlock & MBB,const SIInstrInfo * TII)257d8e91e46SDimitry Andric void SIModeRegister::processBlockPhase1(MachineBasicBlock &MBB,
258d8e91e46SDimitry Andric                                         const SIInstrInfo *TII) {
2591d5ae102SDimitry Andric   auto NewInfo = std::make_unique<BlockData>();
260d8e91e46SDimitry Andric   MachineInstr *InsertionPoint = nullptr;
261d8e91e46SDimitry Andric   // RequirePending is used to indicate whether we are collecting the initial
262d8e91e46SDimitry Andric   // requirements for the block, and need to defer the first InsertionPoint to
263d8e91e46SDimitry Andric   // Phase 3. It is set to false once we have set FirstInsertionPoint, or when
264c0981da4SDimitry Andric   // we discover an explicit setreg that means this block doesn't have any
265d8e91e46SDimitry Andric   // initial requirements.
266d8e91e46SDimitry Andric   bool RequirePending = true;
267d8e91e46SDimitry Andric   Status IPChange;
268d8e91e46SDimitry Andric   for (MachineInstr &MI : MBB) {
269d8e91e46SDimitry Andric     Status InstrMode = getInstructionMode(MI, TII);
270b60736ecSDimitry Andric     if (MI.getOpcode() == AMDGPU::S_SETREG_B32 ||
271b60736ecSDimitry Andric         MI.getOpcode() == AMDGPU::S_SETREG_B32_mode ||
272b60736ecSDimitry Andric         MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
273b60736ecSDimitry Andric         MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32_mode) {
274d8e91e46SDimitry Andric       // We preserve any explicit mode register setreg instruction we encounter,
275d8e91e46SDimitry Andric       // as we assume it has been inserted by a higher authority (this is
276d8e91e46SDimitry Andric       // likely to be a very rare occurrence).
277d8e91e46SDimitry Andric       unsigned Dst = TII->getNamedOperand(MI, AMDGPU::OpName::simm16)->getImm();
278ac9a064cSDimitry Andric       using namespace AMDGPU::Hwreg;
279ac9a064cSDimitry Andric       auto [Id, Offset, Width] = HwregEncoding::decode(Dst);
280ac9a064cSDimitry Andric       if (Id != ID_MODE)
281d8e91e46SDimitry Andric         continue;
282d8e91e46SDimitry Andric 
2834df029ccSDimitry Andric       unsigned Mask = maskTrailingOnes<unsigned>(Width) << Offset;
284d8e91e46SDimitry Andric 
285d8e91e46SDimitry Andric       // If an InsertionPoint is set we will insert a setreg there.
286d8e91e46SDimitry Andric       if (InsertionPoint) {
287d8e91e46SDimitry Andric         insertSetreg(MBB, InsertionPoint, TII, IPChange.delta(NewInfo->Change));
288d8e91e46SDimitry Andric         InsertionPoint = nullptr;
289d8e91e46SDimitry Andric       }
290d8e91e46SDimitry Andric       // If this is an immediate then we know the value being set, but if it is
291d8e91e46SDimitry Andric       // not an immediate then we treat the modified bits of the mode register
292d8e91e46SDimitry Andric       // as unknown.
293b60736ecSDimitry Andric       if (MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
294b60736ecSDimitry Andric           MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32_mode) {
295d8e91e46SDimitry Andric         unsigned Val = TII->getNamedOperand(MI, AMDGPU::OpName::imm)->getImm();
296d8e91e46SDimitry Andric         unsigned Mode = (Val << Offset) & Mask;
297d8e91e46SDimitry Andric         Status Setreg = Status(Mask, Mode);
298d8e91e46SDimitry Andric         // If we haven't already set the initial requirements for the block we
299d8e91e46SDimitry Andric         // don't need to as the requirements start from this explicit setreg.
300d8e91e46SDimitry Andric         RequirePending = false;
301d8e91e46SDimitry Andric         NewInfo->Change = NewInfo->Change.merge(Setreg);
302d8e91e46SDimitry Andric       } else {
303d8e91e46SDimitry Andric         NewInfo->Change = NewInfo->Change.mergeUnknown(Mask);
304d8e91e46SDimitry Andric       }
305d8e91e46SDimitry Andric     } else if (!NewInfo->Change.isCompatible(InstrMode)) {
306d8e91e46SDimitry Andric       // This instruction uses the Mode register and its requirements aren't
307d8e91e46SDimitry Andric       // compatible with the current mode.
308d8e91e46SDimitry Andric       if (InsertionPoint) {
309d8e91e46SDimitry Andric         // If the required mode change cannot be included in the current
310d8e91e46SDimitry Andric         // InsertionPoint changes, we need a setreg and start a new
311d8e91e46SDimitry Andric         // InsertionPoint.
312d8e91e46SDimitry Andric         if (!IPChange.delta(NewInfo->Change).isCombinable(InstrMode)) {
313d8e91e46SDimitry Andric           if (RequirePending) {
314d8e91e46SDimitry Andric             // This is the first insertionPoint in the block so we will defer
315d8e91e46SDimitry Andric             // the insertion of the setreg to Phase 3 where we know whether or
316d8e91e46SDimitry Andric             // not it is actually needed.
317d8e91e46SDimitry Andric             NewInfo->FirstInsertionPoint = InsertionPoint;
318d8e91e46SDimitry Andric             NewInfo->Require = NewInfo->Change;
319d8e91e46SDimitry Andric             RequirePending = false;
320d8e91e46SDimitry Andric           } else {
321d8e91e46SDimitry Andric             insertSetreg(MBB, InsertionPoint, TII,
322d8e91e46SDimitry Andric                          IPChange.delta(NewInfo->Change));
323d8e91e46SDimitry Andric             IPChange = NewInfo->Change;
324d8e91e46SDimitry Andric           }
325d8e91e46SDimitry Andric           // Set the new InsertionPoint
326d8e91e46SDimitry Andric           InsertionPoint = &MI;
327d8e91e46SDimitry Andric         }
328d8e91e46SDimitry Andric         NewInfo->Change = NewInfo->Change.merge(InstrMode);
329d8e91e46SDimitry Andric       } else {
330d8e91e46SDimitry Andric         // No InsertionPoint is currently set - this is either the first in
331d8e91e46SDimitry Andric         // the block or we have previously seen an explicit setreg.
332d8e91e46SDimitry Andric         InsertionPoint = &MI;
333d8e91e46SDimitry Andric         IPChange = NewInfo->Change;
334d8e91e46SDimitry Andric         NewInfo->Change = NewInfo->Change.merge(InstrMode);
335d8e91e46SDimitry Andric       }
336d8e91e46SDimitry Andric     }
337d8e91e46SDimitry Andric   }
338d8e91e46SDimitry Andric   if (RequirePending) {
339d8e91e46SDimitry Andric     // If we haven't yet set the initial requirements for the block we set them
340d8e91e46SDimitry Andric     // now.
341d8e91e46SDimitry Andric     NewInfo->FirstInsertionPoint = InsertionPoint;
342d8e91e46SDimitry Andric     NewInfo->Require = NewInfo->Change;
343d8e91e46SDimitry Andric   } else if (InsertionPoint) {
344d8e91e46SDimitry Andric     // We need to insert a setreg at the InsertionPoint
345d8e91e46SDimitry Andric     insertSetreg(MBB, InsertionPoint, TII, IPChange.delta(NewInfo->Change));
346d8e91e46SDimitry Andric   }
347d8e91e46SDimitry Andric   NewInfo->Exit = NewInfo->Change;
348d8e91e46SDimitry Andric   BlockInfo[MBB.getNumber()] = std::move(NewInfo);
349d8e91e46SDimitry Andric }
350d8e91e46SDimitry Andric 
351d8e91e46SDimitry Andric // In Phase 2 we revisit each block and calculate the common Mode register
352d8e91e46SDimitry Andric // value provided by all predecessor blocks. If the Exit value for the block
353d8e91e46SDimitry Andric // is changed, then we add the successor blocks to the worklist so that the
354d8e91e46SDimitry Andric // exit value is propagated.
processBlockPhase2(MachineBasicBlock & MBB,const SIInstrInfo * TII)355d8e91e46SDimitry Andric void SIModeRegister::processBlockPhase2(MachineBasicBlock &MBB,
356d8e91e46SDimitry Andric                                         const SIInstrInfo *TII) {
357cfca06d7SDimitry Andric   bool RevisitRequired = false;
358cfca06d7SDimitry Andric   bool ExitSet = false;
359d8e91e46SDimitry Andric   unsigned ThisBlock = MBB.getNumber();
360d8e91e46SDimitry Andric   if (MBB.pred_empty()) {
361d8e91e46SDimitry Andric     // There are no predecessors, so use the default starting status.
362d8e91e46SDimitry Andric     BlockInfo[ThisBlock]->Pred = DefaultStatus;
363cfca06d7SDimitry Andric     ExitSet = true;
364d8e91e46SDimitry Andric   } else {
365d8e91e46SDimitry Andric     // Build a status that is common to all the predecessors by intersecting
366d8e91e46SDimitry Andric     // all the predecessor exit status values.
367cfca06d7SDimitry Andric     // Mask bits (which represent the Mode bits with a known value) can only be
368cfca06d7SDimitry Andric     // added by explicit SETREG instructions or the initial default value -
369cfca06d7SDimitry Andric     // the intersection process may remove Mask bits.
370cfca06d7SDimitry Andric     // If we find a predecessor that has not yet had an exit value determined
371cfca06d7SDimitry Andric     // (this can happen for example if a block is its own predecessor) we defer
372cfca06d7SDimitry Andric     // use of that value as the Mask will be all zero, and we will revisit this
373cfca06d7SDimitry Andric     // block again later (unless the only predecessor without an exit value is
374cfca06d7SDimitry Andric     // this block).
375d8e91e46SDimitry Andric     MachineBasicBlock::pred_iterator P = MBB.pred_begin(), E = MBB.pred_end();
376d8e91e46SDimitry Andric     MachineBasicBlock &PB = *(*P);
377cfca06d7SDimitry Andric     unsigned PredBlock = PB.getNumber();
378cfca06d7SDimitry Andric     if ((ThisBlock == PredBlock) && (std::next(P) == E)) {
379cfca06d7SDimitry Andric       BlockInfo[ThisBlock]->Pred = DefaultStatus;
380cfca06d7SDimitry Andric       ExitSet = true;
381cfca06d7SDimitry Andric     } else if (BlockInfo[PredBlock]->ExitSet) {
382cfca06d7SDimitry Andric       BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
383cfca06d7SDimitry Andric       ExitSet = true;
384cfca06d7SDimitry Andric     } else if (PredBlock != ThisBlock)
385cfca06d7SDimitry Andric       RevisitRequired = true;
386d8e91e46SDimitry Andric 
387d8e91e46SDimitry Andric     for (P = std::next(P); P != E; P = std::next(P)) {
388d8e91e46SDimitry Andric       MachineBasicBlock *Pred = *P;
389cfca06d7SDimitry Andric       unsigned PredBlock = Pred->getNumber();
390cfca06d7SDimitry Andric       if (BlockInfo[PredBlock]->ExitSet) {
391cfca06d7SDimitry Andric         if (BlockInfo[ThisBlock]->ExitSet) {
392cfca06d7SDimitry Andric           BlockInfo[ThisBlock]->Pred =
393cfca06d7SDimitry Andric               BlockInfo[ThisBlock]->Pred.intersect(BlockInfo[PredBlock]->Exit);
394cfca06d7SDimitry Andric         } else {
395cfca06d7SDimitry Andric           BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
396cfca06d7SDimitry Andric         }
397cfca06d7SDimitry Andric         ExitSet = true;
398cfca06d7SDimitry Andric       } else if (PredBlock != ThisBlock)
399cfca06d7SDimitry Andric         RevisitRequired = true;
400d8e91e46SDimitry Andric     }
401d8e91e46SDimitry Andric   }
402cfca06d7SDimitry Andric   Status TmpStatus =
403cfca06d7SDimitry Andric       BlockInfo[ThisBlock]->Pred.merge(BlockInfo[ThisBlock]->Change);
404d8e91e46SDimitry Andric   if (BlockInfo[ThisBlock]->Exit != TmpStatus) {
405d8e91e46SDimitry Andric     BlockInfo[ThisBlock]->Exit = TmpStatus;
406d8e91e46SDimitry Andric     // Add the successors to the work list so we can propagate the changed exit
407d8e91e46SDimitry Andric     // status.
408c0981da4SDimitry Andric     for (MachineBasicBlock *Succ : MBB.successors())
409c0981da4SDimitry Andric       Phase2List.push(Succ);
410d8e91e46SDimitry Andric   }
411cfca06d7SDimitry Andric   BlockInfo[ThisBlock]->ExitSet = ExitSet;
412cfca06d7SDimitry Andric   if (RevisitRequired)
413cfca06d7SDimitry Andric     Phase2List.push(&MBB);
414d8e91e46SDimitry Andric }
415d8e91e46SDimitry Andric 
416d8e91e46SDimitry Andric // In Phase 3 we revisit each block and if it has an insertion point defined we
417d8e91e46SDimitry Andric // check whether the predecessor mode meets the block's entry requirements. If
418d8e91e46SDimitry Andric // not we insert an appropriate setreg instruction to modify the Mode register.
processBlockPhase3(MachineBasicBlock & MBB,const SIInstrInfo * TII)419d8e91e46SDimitry Andric void SIModeRegister::processBlockPhase3(MachineBasicBlock &MBB,
420d8e91e46SDimitry Andric                                         const SIInstrInfo *TII) {
421d8e91e46SDimitry Andric   unsigned ThisBlock = MBB.getNumber();
422d8e91e46SDimitry Andric   if (!BlockInfo[ThisBlock]->Pred.isCompatible(BlockInfo[ThisBlock]->Require)) {
423cfca06d7SDimitry Andric     Status Delta =
424cfca06d7SDimitry Andric         BlockInfo[ThisBlock]->Pred.delta(BlockInfo[ThisBlock]->Require);
425d8e91e46SDimitry Andric     if (BlockInfo[ThisBlock]->FirstInsertionPoint)
426d8e91e46SDimitry Andric       insertSetreg(MBB, BlockInfo[ThisBlock]->FirstInsertionPoint, TII, Delta);
427d8e91e46SDimitry Andric     else
428d8e91e46SDimitry Andric       insertSetreg(MBB, &MBB.instr_front(), TII, Delta);
429d8e91e46SDimitry Andric   }
430d8e91e46SDimitry Andric }
431d8e91e46SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)432d8e91e46SDimitry Andric bool SIModeRegister::runOnMachineFunction(MachineFunction &MF) {
433ac9a064cSDimitry Andric   // Constrained FP intrinsics are used to support non-default rounding modes.
434ac9a064cSDimitry Andric   // strictfp attribute is required to mark functions with strict FP semantics
435ac9a064cSDimitry Andric   // having constrained FP intrinsics. This pass fixes up operations that uses
436ac9a064cSDimitry Andric   // a non-default rounding mode for non-strictfp functions. But it should not
437ac9a064cSDimitry Andric   // assume or modify any default rounding modes in case of strictfp functions.
438ac9a064cSDimitry Andric   const Function &F = MF.getFunction();
439ac9a064cSDimitry Andric   if (F.hasFnAttribute(llvm::Attribute::StrictFP))
440ac9a064cSDimitry Andric     return Changed;
441d8e91e46SDimitry Andric   BlockInfo.resize(MF.getNumBlockIDs());
442d8e91e46SDimitry Andric   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
443d8e91e46SDimitry Andric   const SIInstrInfo *TII = ST.getInstrInfo();
444d8e91e46SDimitry Andric 
445d8e91e46SDimitry Andric   // Processing is performed in a number of phases
446d8e91e46SDimitry Andric 
447d8e91e46SDimitry Andric   // Phase 1 - determine the initial mode required by each block, and add setreg
448d8e91e46SDimitry Andric   // instructions for intra block requirements.
449d8e91e46SDimitry Andric   for (MachineBasicBlock &BB : MF)
450d8e91e46SDimitry Andric     processBlockPhase1(BB, TII);
451d8e91e46SDimitry Andric 
452d8e91e46SDimitry Andric   // Phase 2 - determine the exit mode from each block. We add all blocks to the
453d8e91e46SDimitry Andric   // list here, but will also add any that need to be revisited during Phase 2
454d8e91e46SDimitry Andric   // processing.
455d8e91e46SDimitry Andric   for (MachineBasicBlock &BB : MF)
456d8e91e46SDimitry Andric     Phase2List.push(&BB);
457d8e91e46SDimitry Andric   while (!Phase2List.empty()) {
458d8e91e46SDimitry Andric     processBlockPhase2(*Phase2List.front(), TII);
459d8e91e46SDimitry Andric     Phase2List.pop();
460d8e91e46SDimitry Andric   }
461d8e91e46SDimitry Andric 
462d8e91e46SDimitry Andric   // Phase 3 - add an initial setreg to each block where the required entry mode
463d8e91e46SDimitry Andric   // is not satisfied by the exit mode of all its predecessors.
464d8e91e46SDimitry Andric   for (MachineBasicBlock &BB : MF)
465d8e91e46SDimitry Andric     processBlockPhase3(BB, TII);
466d8e91e46SDimitry Andric 
467d8e91e46SDimitry Andric   BlockInfo.clear();
468d8e91e46SDimitry Andric 
469cfca06d7SDimitry Andric   return Changed;
470d8e91e46SDimitry Andric }
471