LLVM 23.0.0git
ScheduleHazardRecognizer.h
Go to the documentation of this file.
1//=- llvm/CodeGen/ScheduleHazardRecognizer.h - Scheduling Support -*- 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 implements the ScheduleHazardRecognizer class, which implements
10// hazard-avoidance heuristics for scheduling.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_SCHEDULEHAZARDRECOGNIZER_H
15#define LLVM_CODEGEN_SCHEDULEHAZARDRECOGNIZER_H
16
18
19namespace llvm {
20
21class MachineInstr;
22class SUnit;
23
24/// HazardRecognizer - This determines whether or not an instruction can be
25/// issued this cycle, and whether or not a noop needs to be inserted to handle
26/// the hazard.
28protected:
29 /// MaxLookAhead - Indicate the number of cycles in the scoreboard
30 /// state. Important to restore the state after backtracking. Additionally,
31 /// MaxLookAhead=0 identifies a fake recognizer, allowing the client to
32 /// bypass virtual calls. Currently the PostRA scheduler ignores it.
33 unsigned MaxLookAhead = 0;
34
35public:
38
40 NoHazard, // This instruction can be emitted at this cycle.
41 Hazard, // This instruction can't be emitted at this cycle.
42 NoopHazard // This instruction can't be emitted, and needs noops.
43 };
44
45 unsigned getMaxLookAhead() const { return MaxLookAhead; }
46
47 bool isEnabled() const { return MaxLookAhead != 0; }
48
49 /// atIssueLimit - Return true if no more instructions may be issued in this
50 /// cycle.
51 ///
52 /// FIXME: remove this once MachineScheduler is the only client.
53 virtual bool atIssueLimit() const { return false; }
54
55 /// getHazardType - Return the hazard type of emitting this node. There are
56 /// three possible results. Either:
57 /// * NoHazard: it is legal to issue this instruction on this cycle.
58 /// * Hazard: issuing this instruction would stall the machine. If some
59 /// other instruction is available, issue it first.
60 /// * NoopHazard: issuing this instruction would break the program. If
61 /// some other instruction can be issued, do so, otherwise issue a noop.
62 virtual HazardType getHazardType(SUnit *, int Stalls = 0) {
63 return NoHazard;
64 }
65
66 /// Reset - This callback is invoked when a new block of
67 /// instructions is about to be schedule. The hazard state should be
68 /// set to an initialized state.
69 virtual void Reset() {}
70
71 /// EmitInstruction - This callback is invoked when an instruction is
72 /// emitted, to advance the hazard state.
73 virtual void EmitInstruction(SUnit *) {}
74
75 /// This overload will be used when the hazard recognizer is being used
76 /// by a non-scheduling pass, which does not use SUnits.
77 virtual void EmitInstruction(MachineInstr *) {}
78
79 /// PreEmitNoops - This callback is invoked prior to emitting an instruction.
80 /// It should return the number of noops to emit prior to the provided
81 /// instruction.
82 /// Note: This is only used during PostRA scheduling. EmitNoop is not called
83 /// for these noops.
84 virtual unsigned PreEmitNoops(SUnit *) {
85 return 0;
86 }
87
88 /// This overload will be used when the hazard recognizer is being used
89 /// by a non-scheduling pass, which does not use SUnits.
90 virtual unsigned PreEmitNoops(MachineInstr *) {
91 return 0;
92 }
93
94 /// ShouldPreferAnother - This callback may be invoked if getHazardType
95 /// returns NoHazard. If, even though there is no hazard, it would be better to
96 /// schedule another available instruction, this callback should return true.
97 virtual bool ShouldPreferAnother(SUnit *) const { return false; }
98
99 /// AdvanceCycle - This callback is invoked whenever the next top-down
100 /// instruction to be scheduled cannot issue in the current cycle, either
101 /// because of latency or resource conflicts. This should increment the
102 /// internal state of the hazard recognizer so that previously "Hazard"
103 /// instructions will now not be hazards.
104 virtual void AdvanceCycle() {}
105
106 /// RecedeCycle - This callback is invoked whenever the next bottom-up
107 /// instruction to be scheduled cannot issue in the current cycle, either
108 /// because of latency or resource conflicts.
109 virtual void RecedeCycle() {}
110
111 /// EmitNoop - This callback is invoked when a noop was added to the
112 /// instruction stream.
113 virtual void EmitNoop() {
114 // Default implementation: count it as a cycle.
115 AdvanceCycle();
116 }
117
118 /// EmitNoops - This callback is invoked when noops were added to the
119 /// instruction stream.
120 virtual void EmitNoops(unsigned Quantity) {
121 // Default implementation: count it as a cycle.
122 for (unsigned i = 0; i < Quantity; ++i)
123 EmitNoop();
124 }
125};
126
127} // end namespace llvm
128
129#endif // LLVM_CODEGEN_SCHEDULEHAZARDRECOGNIZER_H
#define LLVM_ABI
Definition Compiler.h:213
Representation of each machine instruction.
Scheduling unit. This is a node in the scheduling DAG.
virtual void RecedeCycle()
RecedeCycle - This callback is invoked whenever the next bottom-up instruction to be scheduled cannot...
virtual void Reset()
Reset - This callback is invoked when a new block of instructions is about to be schedule.
virtual void EmitInstruction(MachineInstr *)
This overload will be used when the hazard recognizer is being used by a non-scheduling pass,...
virtual void EmitInstruction(SUnit *)
EmitInstruction - This callback is invoked when an instruction is emitted, to advance the hazard stat...
unsigned MaxLookAhead
MaxLookAhead - Indicate the number of cycles in the scoreboard state.
virtual unsigned PreEmitNoops(MachineInstr *)
This overload will be used when the hazard recognizer is being used by a non-scheduling pass,...
virtual bool atIssueLimit() const
atIssueLimit - Return true if no more instructions may be issued in this cycle.
virtual bool ShouldPreferAnother(SUnit *) const
ShouldPreferAnother - This callback may be invoked if getHazardType returns NoHazard.
virtual void EmitNoop()
EmitNoop - This callback is invoked when a noop was added to the instruction stream.
virtual void AdvanceCycle()
AdvanceCycle - This callback is invoked whenever the next top-down instruction to be scheduled cannot...
virtual HazardType getHazardType(SUnit *, int Stalls=0)
getHazardType - Return the hazard type of emitting this node.
virtual unsigned PreEmitNoops(SUnit *)
PreEmitNoops - This callback is invoked prior to emitting an instruction.
virtual void EmitNoops(unsigned Quantity)
EmitNoops - This callback is invoked when noops were added to the instruction stream.
This is an optimization pass for GlobalISel generic memory operations.