blob: af6b6a392b75919a6de2e55b9bfd03772aa4c8eb [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"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000032using namespace llvm;
33
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000034// --------------------------------------------------------------------
35// Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
36
37namespace {
38 DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
39 return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
40 }
41
42 /// Return the DFAInput for an instruction class input vector.
43 /// This function is used in both DFAPacketizer.cpp and in
44 /// DFAPacketizerEmitter.cpp.
45 DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
46 DFAInput InsnInput = 0;
47 assert ((InsnClass.size() <= DFA_MAX_RESTERMS) &&
48 "Exceeded maximum number of DFA terms");
49 for (auto U : InsnClass)
50 InsnInput = addDFAFuncUnits(InsnInput, U);
51 return InsnInput;
52 }
53}
54// --------------------------------------------------------------------
55
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000056DFAPacketizer::DFAPacketizer(const InstrItineraryData *I,
57 const DFAStateInput (*SIT)[2],
Sebastian Popac35a4d2011-12-06 17:34:16 +000058 const unsigned *SET):
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000059 InstrItins(I), CurrentState(0), DFAStateInputTable(SIT),
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000060 DFAStateEntryTable(SET) {
61 // Make sure DFA types are large enough for the number of terms & resources.
62 assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAInput))
63 && "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAInput");
64 assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAStateInput))
65 && "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAStateInput");
66}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000067
68
69//
Sebastian Pop9aa61372011-12-06 17:34:11 +000070// ReadTable - 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.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000083 if (CachedTable.count(UnsignPair(state,
84 DFAStateInputTable[ThisState][0])))
85 return;
86
87 for (unsigned i = ThisState; i < NextStateInTable; i++)
88 CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] =
89 DFAStateInputTable[i][1];
90}
91
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000092//
93// getInsnInput - Return the DFAInput for an instruction class.
94//
95DFAInput DFAPacketizer::getInsnInput(unsigned InsnClass) {
96 // Note: this logic must match that in DFAPacketizerDefs.h for input vectors.
97 DFAInput InsnInput = 0;
98 unsigned i = 0;
99 for (const InstrStage *IS = InstrItins->beginStage(InsnClass),
100 *IE = InstrItins->endStage(InsnClass); IS != IE; ++IS, ++i) {
101 InsnInput = addDFAFuncUnits(InsnInput, IS->getUnits());
102 assert ((i < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs");
103 }
104 return InsnInput;
105}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000106
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +0000107// getInsnInput - Return the DFAInput for an instruction class input vector.
108DFAInput DFAPacketizer::getInsnInput(const std::vector<unsigned> &InsnClass) {
109 return getDFAInsnInput(InsnClass);
110}
111
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000112// canReserveResources - Check if the resources occupied by a MCInstrDesc
Sebastian Pop9aa61372011-12-06 17:34:11 +0000113// are available in the current state.
Sebastian Popac35a4d2011-12-06 17:34:16 +0000114bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000115 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000116 DFAInput InsnInput = getInsnInput(InsnClass);
117 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000118 ReadTable(CurrentState);
119 return (CachedTable.count(StateTrans) != 0);
120}
121
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000122// reserveResources - Reserve the resources occupied by a MCInstrDesc and
Sebastian Pop9aa61372011-12-06 17:34:11 +0000123// change the current state to reflect that change.
Sebastian Popac35a4d2011-12-06 17:34:16 +0000124void DFAPacketizer::reserveResources(const llvm::MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000125 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000126 DFAInput InsnInput = getInsnInput(InsnClass);
127 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000128 ReadTable(CurrentState);
129 assert(CachedTable.count(StateTrans) != 0);
130 CurrentState = CachedTable[StateTrans];
131}
132
133
134// canReserveResources - Check if the resources occupied by a machine
Sebastian Pop9aa61372011-12-06 17:34:11 +0000135// instruction are available in the current state.
Sebastian Popac35a4d2011-12-06 17:34:16 +0000136bool DFAPacketizer::canReserveResources(llvm::MachineInstr *MI) {
137 const llvm::MCInstrDesc &MID = MI->getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000138 return canReserveResources(&MID);
139}
140
141// reserveResources - Reserve the resources occupied by a machine
Sebastian Pop9aa61372011-12-06 17:34:11 +0000142// instruction and change the current state to reflect that change.
Sebastian Popac35a4d2011-12-06 17:34:16 +0000143void DFAPacketizer::reserveResources(llvm::MachineInstr *MI) {
144 const llvm::MCInstrDesc &MID = MI->getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000145 reserveResources(&MID);
146}
Andrew Trick7a35fae2012-02-15 18:55:14 +0000147
Sirish Pande94212162012-05-01 21:28:30 +0000148namespace llvm {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000149// DefaultVLIWScheduler - This class extends ScheduleDAGInstrs and overrides
150// Schedule method to build the dependence graph.
151class DefaultVLIWScheduler : public ScheduleDAGInstrs {
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000152private:
153 AliasAnalysis *AA;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000154public:
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000155 DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI,
156 AliasAnalysis *AA);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000157 // Schedule - Actual scheduling work.
Craig Topper4584cd52014-03-07 09:26:03 +0000158 void schedule() override;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000159};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000160}
Andrew Trick20349b82012-02-15 23:34:15 +0000161
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000162DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000163 MachineLoopInfo &MLI,
164 AliasAnalysis *AA)
165 : ScheduleDAGInstrs(MF, &MLI), AA(AA) {
Sirish Pande94212162012-05-01 21:28:30 +0000166 CanHandleTerminators = true;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000167}
168
Andrew Trick52226d42012-03-07 23:00:49 +0000169void DefaultVLIWScheduler::schedule() {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000170 // Build the scheduling graph.
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000171 buildSchedGraph(AA);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000172}
173
174// VLIWPacketizerList Ctor
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000175VLIWPacketizerList::VLIWPacketizerList(MachineFunction &MF,
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000176 MachineLoopInfo &MLI, AliasAnalysis *AA)
177 : MF(MF), AA(AA) {
Eric Christopher2a321f72014-10-14 01:03:16 +0000178 TII = MF.getSubtarget().getInstrInfo();
Eric Christopher143f02c2014-10-09 01:59:35 +0000179 ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000180 VLIWScheduler = new DefaultVLIWScheduler(MF, MLI, AA);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000181}
182
183// VLIWPacketizerList Dtor
184VLIWPacketizerList::~VLIWPacketizerList() {
Sirish Pande94212162012-05-01 21:28:30 +0000185 if (VLIWScheduler)
186 delete VLIWScheduler;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000187
Sirish Pande94212162012-05-01 21:28:30 +0000188 if (ResourceTracker)
189 delete ResourceTracker;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000190}
191
192// endPacket - End the current packet, bundle packet instructions and reset
193// DFA state.
194void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
Sirish Pande94212162012-05-01 21:28:30 +0000195 MachineInstr *MI) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000196 if (CurrentPacketMIs.size() > 1) {
197 MachineInstr *MIFirst = CurrentPacketMIs.front();
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000198 finalizeBundle(*MBB, MIFirst->getIterator(), MI->getIterator());
Andrew Trick7a35fae2012-02-15 18:55:14 +0000199 }
200 CurrentPacketMIs.clear();
201 ResourceTracker->clearResources();
202}
203
204// PacketizeMIs - Bundle machine instructions into packets.
205void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
206 MachineBasicBlock::iterator BeginItr,
207 MachineBasicBlock::iterator EndItr) {
Sirish Pande94212162012-05-01 21:28:30 +0000208 assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
209 VLIWScheduler->startBlock(MBB);
Andrew Tricka53e1012013-08-23 17:48:33 +0000210 VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
211 std::distance(BeginItr, EndItr));
Sirish Pande94212162012-05-01 21:28:30 +0000212 VLIWScheduler->schedule();
Andrew Trick69b42042012-03-07 23:01:09 +0000213
Sirish Pande94212162012-05-01 21:28:30 +0000214 // Generate MI -> SU map.
215 MIToSUnit.clear();
216 for (unsigned i = 0, e = VLIWScheduler->SUnits.size(); i != e; ++i) {
217 SUnit *SU = &VLIWScheduler->SUnits[i];
218 MIToSUnit[SU->getInstr()] = SU;
219 }
Andrew Trick7a35fae2012-02-15 18:55:14 +0000220
221 // The main packetizer loop.
222 for (; BeginItr != EndItr; ++BeginItr) {
223 MachineInstr *MI = BeginItr;
224
Sirish Pande94212162012-05-01 21:28:30 +0000225 this->initPacketizerState();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000226
227 // End the current packet if needed.
Sirish Pande94212162012-05-01 21:28:30 +0000228 if (this->isSoloInstruction(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000229 endPacket(MBB, MI);
230 continue;
231 }
232
Sirish Pande94212162012-05-01 21:28:30 +0000233 // Ignore pseudo instructions.
234 if (this->ignorePseudoInstruction(MI, MBB))
235 continue;
236
237 SUnit *SUI = MIToSUnit[MI];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000238 assert(SUI && "Missing SUnit Info!");
239
240 // Ask DFA if machine resource is available for MI.
241 bool ResourceAvail = ResourceTracker->canReserveResources(MI);
Krzysztof Parzyszek2005d7d2015-12-16 16:38:16 +0000242 if (ResourceAvail && shouldAddToPacket(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000243 // Dependency check for MI with instructions in CurrentPacketMIs.
244 for (std::vector<MachineInstr*>::iterator VI = CurrentPacketMIs.begin(),
245 VE = CurrentPacketMIs.end(); VI != VE; ++VI) {
246 MachineInstr *MJ = *VI;
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.
Sirish Pande94212162012-05-01 21:28:30 +0000251 if (!this->isLegalToPacketizeTogether(SUI, SUJ)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000252 // Allow packetization if dependency can be pruned.
Sirish Pande94212162012-05-01 21:28:30 +0000253 if (!this->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;
257 } // !isLegalToPruneDependencies.
258 } // !isLegalToPacketizeTogether.
259 } // For all instructions in CurrentPacketMIs.
260 } 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.
Sirish Pande94212162012-05-01 21:28:30 +0000267 BeginItr = this->addToPacket(MI);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000268 } // For all instructions in BB.
269
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}