xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/GCNHazardRecognizer.h (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 //===-- GCNHazardRecognizers.h - GCN Hazard Recognizers ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines hazard recognizers for scheduling on GCN processors.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_TARGET_AMDGPUHAZARDRECOGNIZERS_H
14 #define LLVM_LIB_TARGET_AMDGPUHAZARDRECOGNIZERS_H
15 
16 #include "llvm/ADT/BitVector.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
19 #include "llvm/CodeGen/TargetSchedule.h"
20 #include <list>
21 
22 namespace llvm {
23 
24 class MachineFunction;
25 class MachineInstr;
26 class MachineOperand;
27 class MachineRegisterInfo;
28 class SIInstrInfo;
29 class SIRegisterInfo;
30 class GCNSubtarget;
31 
32 class GCNHazardRecognizer final : public ScheduleHazardRecognizer {
33 public:
34   typedef function_ref<bool(const MachineInstr &)> IsHazardFn;
35 
36 private:
37   // Distinguish if we are called from scheduler or hazard recognizer
38   bool IsHazardRecognizerMode;
39 
40   // This variable stores the instruction that has been emitted this cycle. It
41   // will be added to EmittedInstrs, when AdvanceCycle() or RecedeCycle() is
42   // called.
43   MachineInstr *CurrCycleInstr;
44   std::list<MachineInstr*> EmittedInstrs;
45   const MachineFunction &MF;
46   const GCNSubtarget &ST;
47   const SIInstrInfo &TII;
48   const SIRegisterInfo &TRI;
49   TargetSchedModel TSchedModel;
50   bool RunLdsBranchVmemWARHazardFixup;
51 
52   /// RegUnits of uses in the current soft memory clause.
53   BitVector ClauseUses;
54 
55   /// RegUnits of defs in the current soft memory clause.
56   BitVector ClauseDefs;
57 
58   void resetClause() {
59     ClauseUses.reset();
60     ClauseDefs.reset();
61   }
62 
63   void addClauseInst(const MachineInstr &MI);
64 
65   // Advance over a MachineInstr bundle. Look for hazards in the bundled
66   // instructions.
67   void processBundle();
68 
69   int getWaitStatesSince(IsHazardFn IsHazard, int Limit);
70   int getWaitStatesSinceDef(unsigned Reg, IsHazardFn IsHazardDef, int Limit);
71   int getWaitStatesSinceSetReg(IsHazardFn IsHazard, int Limit);
72 
73   int checkSoftClauseHazards(MachineInstr *SMEM);
74   int checkSMRDHazards(MachineInstr *SMRD);
75   int checkVMEMHazards(MachineInstr* VMEM);
76   int checkDPPHazards(MachineInstr *DPP);
77   int checkDivFMasHazards(MachineInstr *DivFMas);
78   int checkGetRegHazards(MachineInstr *GetRegInstr);
79   int checkSetRegHazards(MachineInstr *SetRegInstr);
80   int createsVALUHazard(const MachineInstr &MI);
81   int checkVALUHazards(MachineInstr *VALU);
82   int checkVALUHazardsHelper(const MachineOperand &Def, const MachineRegisterInfo &MRI);
83   int checkRWLaneHazards(MachineInstr *RWLane);
84   int checkRFEHazards(MachineInstr *RFE);
85   int checkInlineAsmHazards(MachineInstr *IA);
86   int checkReadM0Hazards(MachineInstr *SMovRel);
87   int checkNSAtoVMEMHazard(MachineInstr *MI);
88   int checkFPAtomicToDenormModeHazard(MachineInstr *MI);
89   void fixHazards(MachineInstr *MI);
90   bool fixVcmpxPermlaneHazards(MachineInstr *MI);
91   bool fixVMEMtoScalarWriteHazards(MachineInstr *MI);
92   bool fixSMEMtoVectorWriteHazards(MachineInstr *MI);
93   bool fixVcmpxExecWARHazard(MachineInstr *MI);
94   bool fixLdsBranchVmemWARHazard(MachineInstr *MI);
95 
96   int checkMAIHazards(MachineInstr *MI);
97   int checkMAIHazards908(MachineInstr *MI);
98   int checkMAIHazards90A(MachineInstr *MI);
99   int checkMAIVALUHazards(MachineInstr *MI);
100   int checkMAILdStHazards(MachineInstr *MI);
101 
102 public:
103   GCNHazardRecognizer(const MachineFunction &MF);
104   // We can only issue one instruction per cycle.
105   bool atIssueLimit() const override { return true; }
106   void EmitInstruction(SUnit *SU) override;
107   void EmitInstruction(MachineInstr *MI) override;
108   HazardType getHazardType(SUnit *SU, int Stalls) override;
109   void EmitNoop() override;
110   unsigned PreEmitNoops(MachineInstr *) override;
111   unsigned PreEmitNoopsCommon(MachineInstr *);
112   void AdvanceCycle() override;
113   void RecedeCycle() override;
114   bool ShouldPreferAnother(SUnit *SU) override;
115   void Reset() override;
116 };
117 
118 } // end namespace llvm
119 
120 #endif //LLVM_LIB_TARGET_AMDGPUHAZARDRECOGNIZERS_H
121