1//===--- HexagonHazardRecognizer.h - Hexagon Post RA Hazard Recognizer ----===//
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// This file defines the hazard recognizer for scheduling on Hexagon.
9//===----------------------------------------------------------------------===//
10
11#ifndef LLVM_LIB_TARGET_HEXAGON_HEXAGONPROFITRECOGNIZER_H
12#define LLVM_LIB_TARGET_HEXAGON_HEXAGONPROFITRECOGNIZER_H
13
14#include "HexagonInstrInfo.h"
15#include "HexagonSubtarget.h"
16#include "llvm/ADT/SmallSet.h"
17#include "llvm/CodeGen/DFAPacketizer.h"
18#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
19
20namespace llvm {
21
22class HexagonHazardRecognizer : public ScheduleHazardRecognizer {
23 DFAPacketizer *Resources;
24 const HexagonInstrInfo *TII;
25 unsigned PacketNum = 0;
26 // If the packet contains a potential dot cur instruction. This is
27 // used for the scheduling priority function.
28 SUnit *UsesDotCur = nullptr;
29 // The packet number when a dor cur is emitted. If its use is not generated
30 // in the same packet, then try to wait another cycle before emitting.
31 int DotCurPNum = -1;
32 // Does the packet contain a load. Used to restrict another load, if possible.
33 bool UsesLoad = false;
34 // Check if we should prefer a vector store that will become a .new version.
35 // The .new store uses different resources than a normal store, and the
36 // packetizer will not generate the .new if the regular store does not have
37 // resources available (even if the .new version does). To help, the schedule
38 // attempts to schedule the .new as soon as possible in the packet.
39 SUnit *PrefVectorStoreNew = nullptr;
40 // The activation and weight instructions of the HMX pair being scheduled,
41 // used to prefer the second half once the first half is scheduled.
42 SUnit *Weight = nullptr;
43 SUnit *Activate = nullptr;
44 // The expected second half of the activation/weight pair after the first
45 // half has been emitted, or null if it is unknown.
46 SUnit *HMXPartner = nullptr;
47 // The set of registers defined by instructions in the current packet.
48 SmallSet<unsigned, 8> RegDefs;
49
50 // Return true if the instruction is a store that is converted to a new value
51 // store because its value is defined in the same packet.
52 bool isNewStore(MachineInstr &MI);
53
54 // Return the HMX instruction that pairs with the activation/weight
55 // instruction \p SU, or null if there is no such successor.
56 SUnit *findHMXPartner(SUnit *SU) const;
57
58 // Return true if instructions other than the pending HMX partner must be
59 // delayed so that the partner is scheduled next to the first half.
60 bool mustWaitForHMXPartner(SUnit *SU) const;
61
62public:
63 HexagonHazardRecognizer(const InstrItineraryData *II,
64 const HexagonInstrInfo *HII,
65 const HexagonSubtarget &ST)
66 : Resources(ST.createDFAPacketizer(IID: II)), TII(HII) { }
67
68 ~HexagonHazardRecognizer() override { delete Resources; }
69
70 /// This callback is invoked when a new block of instructions is about to be
71 /// scheduled. The hazard state is set to an initialized state.
72 void Reset() override;
73
74 /// Return the hazard type of emitting this node. There are three
75 /// possible results. Either:
76 /// * NoHazard: it is legal to issue this instruction on this cycle.
77 /// * Hazard: issuing this instruction would stall the machine. If some
78 /// other instruction is available, issue it first.
79 HazardType getHazardType(SUnit *SU, int stalls) override;
80
81 /// This callback is invoked when an instruction is emitted to be scheduled,
82 /// to advance the hazard state.
83 void EmitInstruction(SUnit *) override;
84
85 /// This callback may be invoked if getHazardType returns NoHazard. If, even
86 /// though there is no hazard, it would be better to schedule another
87 /// available instruction, this callback should return true.
88 bool ShouldPreferAnother(SUnit *) const override;
89
90 /// This callback is invoked whenever the next top-down instruction to be
91 /// scheduled cannot issue in the current cycle, either because of latency
92 /// or resource conflicts. This should increment the internal state of the
93 /// hazard recognizer so that previously "Hazard" instructions will now not
94 /// be hazards.
95 void AdvanceCycle() override;
96};
97
98} // end namespace llvm
99
100#endif // LLVM_LIB_TARGET_HEXAGON_HEXAGONPROFITRECOGNIZER_H
101