blob: 2386af9e6877c746e94049239684c45f73d7d397 [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
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +000026#define DEBUG_TYPE "packets"
27
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000028#include "llvm/CodeGen/DFAPacketizer.h"
29#include "llvm/CodeGen/MachineInstr.h"
Andrew Trick7a35fae2012-02-15 18:55:14 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000032#include "llvm/MC/MCInstrItineraries.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Target/TargetInstrInfo.h"
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000034
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000035using namespace llvm;
36
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000037// --------------------------------------------------------------------
38// Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
39
40namespace {
41 DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
42 return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
43 }
44
45 /// Return the DFAInput for an instruction class input vector.
46 /// This function is used in both DFAPacketizer.cpp and in
47 /// DFAPacketizerEmitter.cpp.
48 DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
49 DFAInput InsnInput = 0;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000050 assert((InsnClass.size() <= DFA_MAX_RESTERMS) &&
51 "Exceeded maximum number of DFA terms");
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000052 for (auto U : InsnClass)
53 InsnInput = addDFAFuncUnits(InsnInput, U);
54 return InsnInput;
55 }
56}
57// --------------------------------------------------------------------
58
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000059DFAPacketizer::DFAPacketizer(const InstrItineraryData *I,
60 const DFAStateInput (*SIT)[2],
Sebastian Popac35a4d2011-12-06 17:34:16 +000061 const unsigned *SET):
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000062 InstrItins(I), CurrentState(0), DFAStateInputTable(SIT),
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000063 DFAStateEntryTable(SET) {
64 // Make sure DFA types are large enough for the number of terms & resources.
Benjamin Kramer3e9a5d32016-05-27 11:36:04 +000065 static_assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <=
66 (8 * sizeof(DFAInput)),
67 "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAInput");
68 static_assert(
69 (DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAStateInput)),
70 "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAStateInput");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000071}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000072
73
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000074// Read the DFA transition table and update CachedTable.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000075//
76// Format of the transition tables:
77// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
78// transitions
79// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable
80// for the ith state
81//
82void DFAPacketizer::ReadTable(unsigned int state) {
83 unsigned ThisState = DFAStateEntryTable[state];
84 unsigned NextStateInTable = DFAStateEntryTable[state+1];
85 // Early exit in case CachedTable has already contains this
Sebastian Pop9aa61372011-12-06 17:34:11 +000086 // state's transitions.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000087 if (CachedTable.count(UnsignPair(state, DFAStateInputTable[ThisState][0])))
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000088 return;
89
90 for (unsigned i = ThisState; i < NextStateInTable; i++)
91 CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] =
92 DFAStateInputTable[i][1];
93}
94
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +000095
96// Return the DFAInput for an instruction class.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000097DFAInput DFAPacketizer::getInsnInput(unsigned InsnClass) {
98 // Note: this logic must match that in DFAPacketizerDefs.h for input vectors.
99 DFAInput InsnInput = 0;
100 unsigned i = 0;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000101 (void)i;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000102 for (const InstrStage *IS = InstrItins->beginStage(InsnClass),
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000103 *IE = InstrItins->endStage(InsnClass); IS != IE; ++IS) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000104 InsnInput = addDFAFuncUnits(InsnInput, IS->getUnits());
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000105 assert((i++ < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000106 }
107 return InsnInput;
108}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000109
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000110
111// Return the DFAInput for an instruction class input vector.
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +0000112DFAInput DFAPacketizer::getInsnInput(const std::vector<unsigned> &InsnClass) {
113 return getDFAInsnInput(InsnClass);
114}
115
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000116
117// Check if the resources occupied by a MCInstrDesc are available in the
118// current state.
Sebastian Popac35a4d2011-12-06 17:34:16 +0000119bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000120 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000121 DFAInput InsnInput = getInsnInput(InsnClass);
122 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000123 ReadTable(CurrentState);
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000124 return CachedTable.count(StateTrans) != 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000125}
126
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000127
128// Reserve the resources occupied by a MCInstrDesc and change the current
129// state to reflect that change.
Sebastian Popac35a4d2011-12-06 17:34:16 +0000130void DFAPacketizer::reserveResources(const llvm::MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000131 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000132 DFAInput InsnInput = getInsnInput(InsnClass);
133 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000134 ReadTable(CurrentState);
135 assert(CachedTable.count(StateTrans) != 0);
136 CurrentState = CachedTable[StateTrans];
137}
138
139
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000140// Check if the resources occupied by a machine instruction are available
141// in the current state.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000142bool DFAPacketizer::canReserveResources(llvm::MachineInstr &MI) {
143 const llvm::MCInstrDesc &MID = MI.getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000144 return canReserveResources(&MID);
145}
146
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000147
148// Reserve the resources occupied by a machine instruction and change the
149// current state to reflect that change.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000150void DFAPacketizer::reserveResources(llvm::MachineInstr &MI) {
151 const llvm::MCInstrDesc &MID = MI.getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000152 reserveResources(&MID);
153}
Andrew Trick7a35fae2012-02-15 18:55:14 +0000154
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000155
Sirish Pande94212162012-05-01 21:28:30 +0000156namespace llvm {
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000157// This class extends ScheduleDAGInstrs and overrides the schedule method
158// to build the dependence graph.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000159class DefaultVLIWScheduler : public ScheduleDAGInstrs {
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000160private:
161 AliasAnalysis *AA;
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000162 /// Ordered list of DAG postprocessing steps.
163 std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000164public:
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000165 DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI,
166 AliasAnalysis *AA);
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000167 // Actual scheduling work.
Craig Topper4584cd52014-03-07 09:26:03 +0000168 void schedule() override;
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000169
170 /// DefaultVLIWScheduler takes ownership of the Mutation object.
171 void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) {
172 Mutations.push_back(std::move(Mutation));
173 }
174protected:
175 void postprocessDAG();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000176};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000177}
Andrew Trick20349b82012-02-15 23:34:15 +0000178
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000179
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000180DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000181 MachineLoopInfo &MLI,
182 AliasAnalysis *AA)
183 : ScheduleDAGInstrs(MF, &MLI), AA(AA) {
Sirish Pande94212162012-05-01 21:28:30 +0000184 CanHandleTerminators = true;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000185}
186
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000187
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000188/// Apply each ScheduleDAGMutation step in order.
189void DefaultVLIWScheduler::postprocessDAG() {
190 for (auto &M : Mutations)
191 M->apply(this);
192}
193
194
Andrew Trick52226d42012-03-07 23:00:49 +0000195void DefaultVLIWScheduler::schedule() {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000196 // Build the scheduling graph.
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000197 buildSchedGraph(AA);
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000198 postprocessDAG();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000199}
200
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000201
202VLIWPacketizerList::VLIWPacketizerList(MachineFunction &mf,
203 MachineLoopInfo &mli, AliasAnalysis *aa)
204 : MF(mf), TII(mf.getSubtarget().getInstrInfo()), AA(aa) {
Eric Christopher143f02c2014-10-09 01:59:35 +0000205 ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000206 VLIWScheduler = new DefaultVLIWScheduler(MF, mli, AA);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000207}
208
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000209
Andrew Trick7a35fae2012-02-15 18:55:14 +0000210VLIWPacketizerList::~VLIWPacketizerList() {
Sirish Pande94212162012-05-01 21:28:30 +0000211 if (VLIWScheduler)
212 delete VLIWScheduler;
Sirish Pande94212162012-05-01 21:28:30 +0000213 if (ResourceTracker)
214 delete ResourceTracker;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000215}
216
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000217
218// End the current packet, bundle packet instructions and reset DFA state.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000219void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
220 MachineBasicBlock::iterator MI) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000221 if (CurrentPacketMIs.size() > 1) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000222 MachineInstr &MIFirst = *CurrentPacketMIs.front();
223 finalizeBundle(*MBB, MIFirst.getIterator(), MI.getInstrIterator());
Andrew Trick7a35fae2012-02-15 18:55:14 +0000224 }
225 CurrentPacketMIs.clear();
226 ResourceTracker->clearResources();
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000227 DEBUG(dbgs() << "End packet\n");
Andrew Trick7a35fae2012-02-15 18:55:14 +0000228}
229
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000230
231// Bundle machine instructions into packets.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000232void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
233 MachineBasicBlock::iterator BeginItr,
234 MachineBasicBlock::iterator EndItr) {
Sirish Pande94212162012-05-01 21:28:30 +0000235 assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
236 VLIWScheduler->startBlock(MBB);
Andrew Tricka53e1012013-08-23 17:48:33 +0000237 VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
238 std::distance(BeginItr, EndItr));
Sirish Pande94212162012-05-01 21:28:30 +0000239 VLIWScheduler->schedule();
Andrew Trick69b42042012-03-07 23:01:09 +0000240
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000241 DEBUG({
242 dbgs() << "Scheduling DAG of the packetize region\n";
243 for (SUnit &SU : VLIWScheduler->SUnits)
244 SU.dumpAll(VLIWScheduler);
245 });
246
Sirish Pande94212162012-05-01 21:28:30 +0000247 // Generate MI -> SU map.
248 MIToSUnit.clear();
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000249 for (SUnit &SU : VLIWScheduler->SUnits)
250 MIToSUnit[SU.getInstr()] = &SU;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000251
252 // The main packetizer loop.
253 for (; BeginItr != EndItr; ++BeginItr) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000254 MachineInstr &MI = *BeginItr;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000255 initPacketizerState();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000256
257 // End the current packet if needed.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000258 if (isSoloInstruction(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000259 endPacket(MBB, MI);
260 continue;
261 }
262
Sirish Pande94212162012-05-01 21:28:30 +0000263 // Ignore pseudo instructions.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000264 if (ignorePseudoInstruction(MI, MBB))
Sirish Pande94212162012-05-01 21:28:30 +0000265 continue;
266
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000267 SUnit *SUI = MIToSUnit[&MI];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000268 assert(SUI && "Missing SUnit Info!");
269
270 // Ask DFA if machine resource is available for MI.
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000271 DEBUG(dbgs() << "Checking resources for adding MI to packet " << MI);
272
Andrew Trick7a35fae2012-02-15 18:55:14 +0000273 bool ResourceAvail = ResourceTracker->canReserveResources(MI);
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000274 DEBUG({
275 if (ResourceAvail)
276 dbgs() << " Resources are available for adding MI to packet\n";
277 else
278 dbgs() << " Resources NOT available\n";
279 });
Krzysztof Parzyszek2005d7d2015-12-16 16:38:16 +0000280 if (ResourceAvail && shouldAddToPacket(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000281 // Dependency check for MI with instructions in CurrentPacketMIs.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000282 for (auto MJ : CurrentPacketMIs) {
Sirish Pande94212162012-05-01 21:28:30 +0000283 SUnit *SUJ = MIToSUnit[MJ];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000284 assert(SUJ && "Missing SUnit Info!");
285
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000286 DEBUG(dbgs() << " Checking against MJ " << *MJ);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000287 // Is it legal to packetize SUI and SUJ together.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000288 if (!isLegalToPacketizeTogether(SUI, SUJ)) {
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000289 DEBUG(dbgs() << " Not legal to add MI, try to prune\n");
Andrew Trick7a35fae2012-02-15 18:55:14 +0000290 // Allow packetization if dependency can be pruned.
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000291 if (!isLegalToPruneDependencies(SUI, SUJ)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000292 // End the packet if dependency cannot be pruned.
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000293 DEBUG(dbgs() << " Could not prune dependencies for adding MI\n");
Andrew Trick7a35fae2012-02-15 18:55:14 +0000294 endPacket(MBB, MI);
295 break;
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000296 }
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000297 DEBUG(dbgs() << " Pruned dependence for adding MI\n");
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000298 }
299 }
Andrew Trick7a35fae2012-02-15 18:55:14 +0000300 } else {
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000301 DEBUG(if (ResourceAvail)
302 dbgs() << "Resources are available, but instruction should not be "
303 "added to packet\n " << MI);
Krzysztof Parzyszek2005d7d2015-12-16 16:38:16 +0000304 // End the packet if resource is not available, or if the instruction
305 // shoud not be added to the current packet.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000306 endPacket(MBB, MI);
307 }
308
309 // Add MI to the current packet.
Krzysztof Parzyszek31ceba72016-07-14 19:04:26 +0000310 DEBUG(dbgs() << "* Adding MI to packet " << MI << '\n');
Krzysztof Parzyszekc005e202016-01-14 21:17:04 +0000311 BeginItr = addToPacket(MI);
312 } // For all instructions in the packetization range.
Andrew Trick7a35fae2012-02-15 18:55:14 +0000313
314 // End any packet left behind.
315 endPacket(MBB, EndItr);
Sirish Pande94212162012-05-01 21:28:30 +0000316 VLIWScheduler->exitRegion();
317 VLIWScheduler->finishBlock();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000318}
Krzysztof Parzyszek1a1d78b2016-03-08 15:33:51 +0000319
320
321// Add a DAG mutation object to the ordered list.
322void VLIWPacketizerList::addMutation(
323 std::unique_ptr<ScheduleDAGMutation> Mutation) {
324 VLIWScheduler->addMutation(std::move(Mutation));
325}