[ARM] MVE integer min and max
[llvm-complete.git] / include / llvm / CodeGen / DFAPacketizer.h
blobcf58ee0cabea97eb0c0b41f57e2c8b9697bf5807
1 //===- llvm/CodeGen/DFAPacketizer.h - DFA Packetizer for VLIW ---*- 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 // This class implements a deterministic finite automaton (DFA) based
9 // packetizing mechanism for VLIW architectures. It provides APIs to
10 // determine whether there exists a legal mapping of instructions to
11 // functional unit assignments in a packet. The DFA is auto-generated from
12 // the target's Schedule.td file.
14 // A DFA consists of 3 major elements: states, inputs, and transitions. For
15 // the packetizing mechanism, the input is the set of instruction classes for
16 // a target. The state models all possible combinations of functional unit
17 // consumption for a given set of instructions in a packet. A transition
18 // models the addition of an instruction to a packet. In the DFA constructed
19 // by this class, if an instruction can be added to a packet, then a valid
20 // transition exists from the corresponding state. Invalid transitions
21 // indicate that the instruction cannot be added to the current packet.
23 //===----------------------------------------------------------------------===//
25 #ifndef LLVM_CODEGEN_DFAPACKETIZER_H
26 #define LLVM_CODEGEN_DFAPACKETIZER_H
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/CodeGen/MachineBasicBlock.h"
30 #include "llvm/CodeGen/ScheduleDAGMutation.h"
31 #include <cstdint>
32 #include <map>
33 #include <memory>
34 #include <utility>
35 #include <vector>
37 namespace llvm {
39 class DefaultVLIWScheduler;
40 class InstrItineraryData;
41 class MachineFunction;
42 class MachineInstr;
43 class MachineLoopInfo;
44 class MCInstrDesc;
45 class SUnit;
46 class TargetInstrInfo;
48 // --------------------------------------------------------------------
49 // Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
51 // DFA_MAX_RESTERMS * DFA_MAX_RESOURCES must fit within sizeof DFAInput.
52 // This is verified in DFAPacketizer.cpp:DFAPacketizer::DFAPacketizer.
54 // e.g. terms x resource bit combinations that fit in uint32_t:
55 // 4 terms x 8 bits = 32 bits
56 // 3 terms x 10 bits = 30 bits
57 // 2 terms x 16 bits = 32 bits
59 // e.g. terms x resource bit combinations that fit in uint64_t:
60 // 8 terms x 8 bits = 64 bits
61 // 7 terms x 9 bits = 63 bits
62 // 6 terms x 10 bits = 60 bits
63 // 5 terms x 12 bits = 60 bits
64 // 4 terms x 16 bits = 64 bits <--- current
65 // 3 terms x 21 bits = 63 bits
66 // 2 terms x 32 bits = 64 bits
68 #define DFA_MAX_RESTERMS 4 // The max # of AND'ed resource terms.
69 #define DFA_MAX_RESOURCES 16 // The max # of resource bits in one term.
71 using DFAInput = uint64_t;
72 using DFAStateInput = int64_t;
74 #define DFA_TBLTYPE "int64_t" // For generating DFAStateInputTable.
75 // --------------------------------------------------------------------
77 class DFAPacketizer {
78 private:
79 using UnsignPair = std::pair<unsigned, DFAInput>;
81 const InstrItineraryData *InstrItins;
82 int CurrentState = 0;
83 const DFAStateInput (*DFAStateInputTable)[2];
84 const unsigned *DFAStateEntryTable;
86 // CachedTable is a map from <FromState, Input> to ToState.
87 DenseMap<UnsignPair, unsigned> CachedTable;
89 // Read the DFA transition table and update CachedTable.
90 void ReadTable(unsigned state);
92 public:
93 DFAPacketizer(const InstrItineraryData *I, const DFAStateInput (*SIT)[2],
94 const unsigned *SET);
96 // Reset the current state to make all resources available.
97 void clearResources() {
98 CurrentState = 0;
101 // Return the DFAInput for an instruction class.
102 DFAInput getInsnInput(unsigned InsnClass);
104 // Return the DFAInput for an instruction class input vector.
105 static DFAInput getInsnInput(const std::vector<unsigned> &InsnClass);
107 // Check if the resources occupied by a MCInstrDesc are available in
108 // the current state.
109 bool canReserveResources(const MCInstrDesc *MID);
111 // Reserve the resources occupied by a MCInstrDesc and change the current
112 // state to reflect that change.
113 void reserveResources(const MCInstrDesc *MID);
115 // Check if the resources occupied by a machine instruction are available
116 // in the current state.
117 bool canReserveResources(MachineInstr &MI);
119 // Reserve the resources occupied by a machine instruction and change the
120 // current state to reflect that change.
121 void reserveResources(MachineInstr &MI);
123 const InstrItineraryData *getInstrItins() const { return InstrItins; }
126 // VLIWPacketizerList implements a simple VLIW packetizer using DFA. The
127 // packetizer works on machine basic blocks. For each instruction I in BB,
128 // the packetizer consults the DFA to see if machine resources are available
129 // to execute I. If so, the packetizer checks if I depends on any instruction
130 // in the current packet. If no dependency is found, I is added to current
131 // packet and the machine resource is marked as taken. If any dependency is
132 // found, a target API call is made to prune the dependence.
133 class VLIWPacketizerList {
134 protected:
135 MachineFunction &MF;
136 const TargetInstrInfo *TII;
137 AliasAnalysis *AA;
139 // The VLIW Scheduler.
140 DefaultVLIWScheduler *VLIWScheduler;
141 // Vector of instructions assigned to the current packet.
142 std::vector<MachineInstr*> CurrentPacketMIs;
143 // DFA resource tracker.
144 DFAPacketizer *ResourceTracker;
145 // Map: MI -> SU.
146 std::map<MachineInstr*, SUnit*> MIToSUnit;
148 public:
149 // The AliasAnalysis parameter can be nullptr.
150 VLIWPacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
151 AliasAnalysis *AA);
153 virtual ~VLIWPacketizerList();
155 // Implement this API in the backend to bundle instructions.
156 void PacketizeMIs(MachineBasicBlock *MBB,
157 MachineBasicBlock::iterator BeginItr,
158 MachineBasicBlock::iterator EndItr);
160 // Return the ResourceTracker.
161 DFAPacketizer *getResourceTracker() {return ResourceTracker;}
163 // addToPacket - Add MI to the current packet.
164 virtual MachineBasicBlock::iterator addToPacket(MachineInstr &MI) {
165 CurrentPacketMIs.push_back(&MI);
166 ResourceTracker->reserveResources(MI);
167 return MI;
170 // End the current packet and reset the state of the packetizer.
171 // Overriding this function allows the target-specific packetizer
172 // to perform custom finalization.
173 virtual void endPacket(MachineBasicBlock *MBB,
174 MachineBasicBlock::iterator MI);
176 // Perform initialization before packetizing an instruction. This
177 // function is supposed to be overrided by the target dependent packetizer.
178 virtual void initPacketizerState() {}
180 // Check if the given instruction I should be ignored by the packetizer.
181 virtual bool ignorePseudoInstruction(const MachineInstr &I,
182 const MachineBasicBlock *MBB) {
183 return false;
186 // Return true if instruction MI can not be packetized with any other
187 // instruction, which means that MI itself is a packet.
188 virtual bool isSoloInstruction(const MachineInstr &MI) { return true; }
190 // Check if the packetizer should try to add the given instruction to
191 // the current packet. One reasons for which it may not be desirable
192 // to include an instruction in the current packet could be that it
193 // would cause a stall.
194 // If this function returns "false", the current packet will be ended,
195 // and the instruction will be added to the next packet.
196 virtual bool shouldAddToPacket(const MachineInstr &MI) { return true; }
198 // Check if it is legal to packetize SUI and SUJ together.
199 virtual bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
200 return false;
203 // Check if it is legal to prune dependece between SUI and SUJ.
204 virtual bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
205 return false;
208 // Add a DAG mutation to be done before the packetization begins.
209 void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation);
211 bool alias(const MachineInstr &MI1, const MachineInstr &MI2,
212 bool UseTBAA = true) const;
214 private:
215 bool alias(const MachineMemOperand &Op1, const MachineMemOperand &Op2,
216 bool UseTBAA = true) const;
219 } // end namespace llvm
221 #endif // LLVM_CODEGEN_DFAPACKETIZER_H