blob: a4ed61b98ce3a6f7cb245319f1ca56b7e75a4b0a [file] [log] [blame]
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001//=- llvm/CodeGen/DFAPacketizer.cpp - DFA Packetizer for VLIW -*- C++ -*-=====//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// This class implements a deterministic finite automaton (DFA) based
10// packetizing mechanism for VLIW architectures. It provides APIs to
11// determine whether there exists a legal mapping of instructions to
12// functional unit assignments in a packet. The DFA is auto-generated from
13// the target's Schedule.td file.
14//
15// A DFA consists of 3 major elements: states, inputs, and transitions. For
16// the packetizing mechanism, the input is the set of instruction classes for
17// a target. The state models all possible combinations of functional unit
18// consumption for a given set of instructions in a packet. A transition
19// models the addition of an instruction to a packet. In the DFA constructed
20// by this class, if an instruction can be added to a packet, then a valid
21// transition exists from the corresponding state. Invalid transitions
22// indicate that the instruction cannot be added to the current packet.
23//
24//===----------------------------------------------------------------------===//
25
26#include "llvm/CodeGen/DFAPacketizer.h"
27#include "llvm/CodeGen/MachineInstr.h"
Andrew Trick7a35fae2012-02-15 18:55:14 +000028#include "llvm/CodeGen/MachineInstrBundle.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000030#include "llvm/MC/MCInstrItineraries.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Target/TargetInstrInfo.h"
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000032
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000033using namespace llvm;
34
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000035// --------------------------------------------------------------------
36// Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
37
38namespace {
39 DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
40 return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
41 }
42
43 /// Return the DFAInput for an instruction class input vector.
44 /// This function is used in both DFAPacketizer.cpp and in
45 /// DFAPacketizerEmitter.cpp.
46 DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
47 DFAInput InsnInput = 0;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000048 assert((InsnClass.size() <= DFA_MAX_RESTERMS) &&
49 "Exceeded maximum number of DFA terms");
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000050 for (auto U : InsnClass)
51 InsnInput = addDFAFuncUnits(InsnInput, U);
52 return InsnInput;
53 }
54}
55// --------------------------------------------------------------------
56
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000057DFAPacketizer::DFAPacketizer(const InstrItineraryData *I,
58 const DFAStateInput (*SIT)[2],
Sebastian Popac35a4d2011-12-06 17:34:16 +000059 const unsigned *SET):
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000060 InstrItins(I), CurrentState(0), DFAStateInputTable(SIT),
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000061 DFAStateEntryTable(SET) {
62 // Make sure DFA types are large enough for the number of terms & resources.
63 assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAInput))
64 && "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAInput");
65 assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAStateInput))
66 && "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAStateInput");
67}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000068
69
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000070// Read the DFA transition table and update CachedTable.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000071//
72// Format of the transition tables:
73// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
74// transitions
75// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable
76// for the ith state
77//
78void DFAPacketizer::ReadTable(unsigned int state) {
79 unsigned ThisState = DFAStateEntryTable[state];
80 unsigned NextStateInTable = DFAStateEntryTable[state+1];
81 // Early exit in case CachedTable has already contains this
Sebastian Pop9aa61372011-12-06 17:34:11 +000082 // state's transitions.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000083 if (CachedTable.count(UnsignPair(state, DFAStateInputTable[ThisState][0])))
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000084 return;
85
86 for (unsigned i = ThisState; i < NextStateInTable; i++)
87 CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] =
88 DFAStateInputTable[i][1];
89}
90
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000091
92// Return the DFAInput for an instruction class.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000093DFAInput DFAPacketizer::getInsnInput(unsigned InsnClass) {
94 // Note: this logic must match that in DFAPacketizerDefs.h for input vectors.
95 DFAInput InsnInput = 0;
96 unsigned i = 0;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000097 (void)i;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000098 for (const InstrStage *IS = InstrItins->beginStage(InsnClass),
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000099 *IE = InstrItins->endStage(InsnClass); IS != IE; ++IS) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000100 InsnInput = addDFAFuncUnits(InsnInput, IS->getUnits());
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000101 assert((i++ < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000102 }
103 return InsnInput;
104}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000105
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000106
107// Return the DFAInput for an instruction class input vector.
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +0000108DFAInput DFAPacketizer::getInsnInput(const std::vector<unsigned> &InsnClass) {
109 return getDFAInsnInput(InsnClass);
110}
111
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000112
113// Check if the resources occupied by a MCInstrDesc are available in the
114// current state.
Sebastian Popac35a4d2011-12-06 17:34:16 +0000115bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000116 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000117 DFAInput InsnInput = getInsnInput(InsnClass);
118 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000119 ReadTable(CurrentState);
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000120 return CachedTable.count(StateTrans) != 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000121}
122
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000123
124// Reserve the resources occupied by a MCInstrDesc and change the current
125// state to reflect that change.
Sebastian Popac35a4d2011-12-06 17:34:16 +0000126void DFAPacketizer::reserveResources(const llvm::MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000127 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000128 DFAInput InsnInput = getInsnInput(InsnClass);
129 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000130 ReadTable(CurrentState);
131 assert(CachedTable.count(StateTrans) != 0);
132 CurrentState = CachedTable[StateTrans];
133}
134
135
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000136// Check if the resources occupied by a machine instruction are available
137// in the current state.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000138bool DFAPacketizer::canReserveResources(llvm::MachineInstr &MI) {
139 const llvm::MCInstrDesc &MID = MI.getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000140 return canReserveResources(&MID);
141}
142
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000143
144// Reserve the resources occupied by a machine instruction and change the
145// current state to reflect that change.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000146void DFAPacketizer::reserveResources(llvm::MachineInstr &MI) {
147 const llvm::MCInstrDesc &MID = MI.getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000148 reserveResources(&MID);
149}
Andrew Trick7a35fae2012-02-15 18:55:14 +0000150
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000151
Sirish Pande94212162012-05-01 21:28:30 +0000152namespace llvm {
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000153// This class extends ScheduleDAGInstrs and overrides the schedule method
154// to build the dependence graph.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000155class DefaultVLIWScheduler : public ScheduleDAGInstrs {
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000156private:
157 AliasAnalysis *AA;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000158public:
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000159 DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI,
160 AliasAnalysis *AA);
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000161 // Actual scheduling work.
Craig Topper4584cd52014-03-07 09:26:03 +0000162 void schedule() override;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000163};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000164}
Andrew Trick20349b82012-02-15 23:34:15 +0000165
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000166
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000167DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000168 MachineLoopInfo &MLI,
169 AliasAnalysis *AA)
170 : ScheduleDAGInstrs(MF, &MLI), AA(AA) {
Sirish Pande94212162012-05-01 21:28:30 +0000171 CanHandleTerminators = true;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000172}
173
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000174
Andrew Trick52226d42012-03-07 23:00:49 +0000175void DefaultVLIWScheduler::schedule() {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000176 // Build the scheduling graph.
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000177 buildSchedGraph(AA);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000178}
179
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000180
181VLIWPacketizerList::VLIWPacketizerList(MachineFunction &mf,
182 MachineLoopInfo &mli, AliasAnalysis *aa)
183 : MF(mf), TII(mf.getSubtarget().getInstrInfo()), AA(aa) {
Eric Christopher143f02c2014-10-09 01:59:35 +0000184 ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000185 VLIWScheduler = new DefaultVLIWScheduler(MF, mli, AA);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000186}
187
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000188
Andrew Trick7a35fae2012-02-15 18:55:14 +0000189VLIWPacketizerList::~VLIWPacketizerList() {
Sirish Pande94212162012-05-01 21:28:30 +0000190 if (VLIWScheduler)
191 delete VLIWScheduler;
Sirish Pande94212162012-05-01 21:28:30 +0000192 if (ResourceTracker)
193 delete ResourceTracker;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000194}
195
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000196
197// End the current packet, bundle packet instructions and reset DFA state.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000198void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
199 MachineBasicBlock::iterator MI) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000200 if (CurrentPacketMIs.size() > 1) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000201 MachineInstr &MIFirst = *CurrentPacketMIs.front();
202 finalizeBundle(*MBB, MIFirst.getIterator(), MI.getInstrIterator());
Andrew Trick7a35fae2012-02-15 18:55:14 +0000203 }
204 CurrentPacketMIs.clear();
205 ResourceTracker->clearResources();
206}
207
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000208
209// Bundle machine instructions into packets.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000210void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
211 MachineBasicBlock::iterator BeginItr,
212 MachineBasicBlock::iterator EndItr) {
Sirish Pande94212162012-05-01 21:28:30 +0000213 assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
214 VLIWScheduler->startBlock(MBB);
Andrew Tricka53e1012013-08-23 17:48:33 +0000215 VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
216 std::distance(BeginItr, EndItr));
Sirish Pande94212162012-05-01 21:28:30 +0000217 VLIWScheduler->schedule();
Andrew Trick69b42042012-03-07 23:01:09 +0000218
Sirish Pande94212162012-05-01 21:28:30 +0000219 // Generate MI -> SU map.
220 MIToSUnit.clear();
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000221 for (SUnit &SU : VLIWScheduler->SUnits)
222 MIToSUnit[SU.getInstr()] = &SU;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000223
224 // The main packetizer loop.
225 for (; BeginItr != EndItr; ++BeginItr) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000226 MachineInstr &MI = *BeginItr;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000227 initPacketizerState();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000228
229 // End the current packet if needed.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000230 if (isSoloInstruction(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000231 endPacket(MBB, MI);
232 continue;
233 }
234
Sirish Pande94212162012-05-01 21:28:30 +0000235 // Ignore pseudo instructions.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000236 if (ignorePseudoInstruction(MI, MBB))
Sirish Pande94212162012-05-01 21:28:30 +0000237 continue;
238
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000239 SUnit *SUI = MIToSUnit[&MI];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000240 assert(SUI && "Missing SUnit Info!");
241
242 // Ask DFA if machine resource is available for MI.
243 bool ResourceAvail = ResourceTracker->canReserveResources(MI);
Krzysztof Parzyszek2005d7d2015-12-16 16:38:16 +0000244 if (ResourceAvail && shouldAddToPacket(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000245 // Dependency check for MI with instructions in CurrentPacketMIs.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000246 for (auto MJ : CurrentPacketMIs) {
Sirish Pande94212162012-05-01 21:28:30 +0000247 SUnit *SUJ = MIToSUnit[MJ];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000248 assert(SUJ && "Missing SUnit Info!");
249
250 // Is it legal to packetize SUI and SUJ together.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000251 if (!isLegalToPacketizeTogether(SUI, SUJ)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000252 // Allow packetization if dependency can be pruned.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000253 if (!isLegalToPruneDependencies(SUI, SUJ)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000254 // End the packet if dependency cannot be pruned.
255 endPacket(MBB, MI);
256 break;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000257 }
258 }
259 }
Andrew Trick7a35fae2012-02-15 18:55:14 +0000260 } else {
Krzysztof Parzyszek2005d7d2015-12-16 16:38:16 +0000261 // End the packet if resource is not available, or if the instruction
262 // shoud not be added to the current packet.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000263 endPacket(MBB, MI);
264 }
265
266 // Add MI to the current packet.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000267 BeginItr = addToPacket(MI);
268 } // For all instructions in the packetization range.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000269
270 // End any packet left behind.
271 endPacket(MBB, EndItr);
Sirish Pande94212162012-05-01 21:28:30 +0000272 VLIWScheduler->exitRegion();
273 VLIWScheduler->finishBlock();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000274}