xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/GCNHazardRecognizer.h (revision 963f5dc7a30624e95d72fb7f87b8892651164e46)
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 ScheduleDAG;
29 class SIInstrInfo;
30 class SIRegisterInfo;
31 class GCNSubtarget;
32 
33 class GCNHazardRecognizer final : public ScheduleHazardRecognizer {
34 public:
35   typedef function_ref<bool(const MachineInstr &)> IsHazardFn;
36 
37 private:
38   // Distinguish if we are called from scheduler or hazard recognizer
39   bool IsHazardRecognizerMode;
40 
41   // This variable stores the instruction that has been emitted this cycle. It
42   // will be added to EmittedInstrs, when AdvanceCycle() or RecedeCycle() is
43   // called.
44   MachineInstr *CurrCycleInstr;
45   std::list<MachineInstr*> EmittedInstrs;
46   const MachineFunction &MF;
47   const GCNSubtarget &ST;
48   const SIInstrInfo &TII;
49   const SIRegisterInfo &TRI;
50   TargetSchedModel TSchedModel;
51   bool RunLdsBranchVmemWARHazardFixup;
52 
53   /// RegUnits of uses in the current soft memory clause.
54   BitVector ClauseUses;
55 
56   /// RegUnits of defs in the current soft memory clause.
57   BitVector ClauseDefs;
58 
59   void resetClause() {
60     ClauseUses.reset();
61     ClauseDefs.reset();
62   }
63 
64   void addClauseInst(const MachineInstr &MI);
65 
66   // Advance over a MachineInstr bundle. Look for hazards in the bundled
67   // instructions.
68   void processBundle();
69 
70   int getWaitStatesSince(IsHazardFn IsHazard, int Limit);
71   int getWaitStatesSinceDef(unsigned Reg, IsHazardFn IsHazardDef, int Limit);
72   int getWaitStatesSinceSetReg(IsHazardFn IsHazard, int Limit);
73 
74   int checkSoftClauseHazards(MachineInstr *SMEM);
75   int checkSMRDHazards(MachineInstr *SMRD);
76   int checkVMEMHazards(MachineInstr* VMEM);
77   int checkDPPHazards(MachineInstr *DPP);
78   int checkDivFMasHazards(MachineInstr *DivFMas);
79   int checkGetRegHazards(MachineInstr *GetRegInstr);
80   int checkSetRegHazards(MachineInstr *SetRegInstr);
81   int createsVALUHazard(const MachineInstr &MI);
82   int checkVALUHazards(MachineInstr *VALU);
83   int checkVALUHazardsHelper(const MachineOperand &Def, const MachineRegisterInfo &MRI);
84   int checkRWLaneHazards(MachineInstr *RWLane);
85   int checkRFEHazards(MachineInstr *RFE);
86   int checkInlineAsmHazards(MachineInstr *IA);
87   int checkReadM0Hazards(MachineInstr *SMovRel);
88   int checkNSAtoVMEMHazard(MachineInstr *MI);
89   int checkFPAtomicToDenormModeHazard(MachineInstr *MI);
90   void fixHazards(MachineInstr *MI);
91   bool fixVcmpxPermlaneHazards(MachineInstr *MI);
92   bool fixVMEMtoScalarWriteHazards(MachineInstr *MI);
93   bool fixSMEMtoVectorWriteHazards(MachineInstr *MI);
94   bool fixVcmpxExecWARHazard(MachineInstr *MI);
95   bool fixLdsBranchVmemWARHazard(MachineInstr *MI);
96 
97   int checkMAIHazards(MachineInstr *MI);
98   int checkMAIHazards908(MachineInstr *MI);
99   int checkMAIHazards90A(MachineInstr *MI);
100   int checkMAIVALUHazards(MachineInstr *MI);
101   int checkMAILdStHazards(MachineInstr *MI);
102 
103 public:
104   GCNHazardRecognizer(const MachineFunction &MF);
105   // We can only issue one instruction per cycle.
106   bool atIssueLimit() const override { return true; }
107   void EmitInstruction(SUnit *SU) override;
108   void EmitInstruction(MachineInstr *MI) override;
109   HazardType getHazardType(SUnit *SU, int Stalls) override;
110   void EmitNoop() override;
111   unsigned PreEmitNoops(MachineInstr *) override;
112   unsigned PreEmitNoopsCommon(MachineInstr *);
113   void AdvanceCycle() override;
114   void RecedeCycle() override;
115   bool ShouldPreferAnother(SUnit *SU) override;
116   void Reset() override;
117 };
118 
119 } // end namespace llvm
120 
121 #endif //LLVM_LIB_TARGET_AMDGPUHAZARDRECOGNIZERS_H
122