blob: 64b7f481df41afb9c6e482bc236dd7711b0b7cec [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 Parzyszekb4655722015-11-21 20:00:45 +000034DFAPacketizer::DFAPacketizer(const InstrItineraryData *I,
35 const DFAStateInput (*SIT)[2],
Sebastian Popac35a4d2011-12-06 17:34:16 +000036 const unsigned *SET):
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000037 InstrItins(I), CurrentState(0), DFAStateInputTable(SIT),
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000038 DFAStateEntryTable(SET) {
39 // Make sure DFA types are large enough for the number of terms & resources.
40 assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAInput))
41 && "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAInput");
42 assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAStateInput))
43 && "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAStateInput");
44}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000045
46
47//
Sebastian Pop9aa61372011-12-06 17:34:11 +000048// ReadTable - Read the DFA transition table and update CachedTable.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000049//
50// Format of the transition tables:
51// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
52// transitions
53// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable
54// for the ith state
55//
56void DFAPacketizer::ReadTable(unsigned int state) {
57 unsigned ThisState = DFAStateEntryTable[state];
58 unsigned NextStateInTable = DFAStateEntryTable[state+1];
59 // Early exit in case CachedTable has already contains this
Sebastian Pop9aa61372011-12-06 17:34:11 +000060 // state's transitions.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000061 if (CachedTable.count(UnsignPair(state,
62 DFAStateInputTable[ThisState][0])))
63 return;
64
65 for (unsigned i = ThisState; i < NextStateInTable; i++)
66 CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] =
67 DFAStateInputTable[i][1];
68}
69
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000070//
71// getInsnInput - Return the DFAInput for an instruction class.
72//
73DFAInput DFAPacketizer::getInsnInput(unsigned InsnClass) {
74 // Note: this logic must match that in DFAPacketizerDefs.h for input vectors.
75 DFAInput InsnInput = 0;
76 unsigned i = 0;
77 for (const InstrStage *IS = InstrItins->beginStage(InsnClass),
78 *IE = InstrItins->endStage(InsnClass); IS != IE; ++IS, ++i) {
79 InsnInput = addDFAFuncUnits(InsnInput, IS->getUnits());
80 assert ((i < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs");
81 }
82 return InsnInput;
83}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000084
85// canReserveResources - Check if the resources occupied by a MCInstrDesc
Sebastian Pop9aa61372011-12-06 17:34:11 +000086// are available in the current state.
Sebastian Popac35a4d2011-12-06 17:34:16 +000087bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000088 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000089 DFAInput InsnInput = getInsnInput(InsnClass);
90 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000091 ReadTable(CurrentState);
92 return (CachedTable.count(StateTrans) != 0);
93}
94
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000095// reserveResources - Reserve the resources occupied by a MCInstrDesc and
Sebastian Pop9aa61372011-12-06 17:34:11 +000096// change the current state to reflect that change.
Sebastian Popac35a4d2011-12-06 17:34:16 +000097void DFAPacketizer::reserveResources(const llvm::MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000098 unsigned InsnClass = MID->getSchedClass();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000099 DFAInput InsnInput = getInsnInput(InsnClass);
100 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000101 ReadTable(CurrentState);
102 assert(CachedTable.count(StateTrans) != 0);
103 CurrentState = CachedTable[StateTrans];
104}
105
106
107// canReserveResources - Check if the resources occupied by a machine
Sebastian Pop9aa61372011-12-06 17:34:11 +0000108// instruction are available in the current state.
Sebastian Popac35a4d2011-12-06 17:34:16 +0000109bool DFAPacketizer::canReserveResources(llvm::MachineInstr *MI) {
110 const llvm::MCInstrDesc &MID = MI->getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000111 return canReserveResources(&MID);
112}
113
114// reserveResources - Reserve the resources occupied by a machine
Sebastian Pop9aa61372011-12-06 17:34:11 +0000115// instruction and change the current state to reflect that change.
Sebastian Popac35a4d2011-12-06 17:34:16 +0000116void DFAPacketizer::reserveResources(llvm::MachineInstr *MI) {
117 const llvm::MCInstrDesc &MID = MI->getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000118 reserveResources(&MID);
119}
Andrew Trick7a35fae2012-02-15 18:55:14 +0000120
Sirish Pande94212162012-05-01 21:28:30 +0000121namespace llvm {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000122// DefaultVLIWScheduler - This class extends ScheduleDAGInstrs and overrides
123// Schedule method to build the dependence graph.
124class DefaultVLIWScheduler : public ScheduleDAGInstrs {
125public:
Matthias Braun93563e72015-11-03 01:53:29 +0000126 DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000127 // Schedule - Actual scheduling work.
Craig Topper4584cd52014-03-07 09:26:03 +0000128 void schedule() override;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000129};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000130}
Andrew Trick20349b82012-02-15 23:34:15 +0000131
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000132DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
Matthias Braun93563e72015-11-03 01:53:29 +0000133 MachineLoopInfo &MLI)
134 : ScheduleDAGInstrs(MF, &MLI) {
Sirish Pande94212162012-05-01 21:28:30 +0000135 CanHandleTerminators = true;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000136}
137
Andrew Trick52226d42012-03-07 23:00:49 +0000138void DefaultVLIWScheduler::schedule() {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000139 // Build the scheduling graph.
Craig Topperc0196b12014-04-14 00:51:57 +0000140 buildSchedGraph(nullptr);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000141}
142
143// VLIWPacketizerList Ctor
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000144VLIWPacketizerList::VLIWPacketizerList(MachineFunction &MF,
Matthias Braun93563e72015-11-03 01:53:29 +0000145 MachineLoopInfo &MLI)
Eric Christopher2a321f72014-10-14 01:03:16 +0000146 : MF(MF) {
147 TII = MF.getSubtarget().getInstrInfo();
Eric Christopher143f02c2014-10-09 01:59:35 +0000148 ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
Matthias Braun93563e72015-11-03 01:53:29 +0000149 VLIWScheduler = new DefaultVLIWScheduler(MF, MLI);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000150}
151
152// VLIWPacketizerList Dtor
153VLIWPacketizerList::~VLIWPacketizerList() {
Sirish Pande94212162012-05-01 21:28:30 +0000154 if (VLIWScheduler)
155 delete VLIWScheduler;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000156
Sirish Pande94212162012-05-01 21:28:30 +0000157 if (ResourceTracker)
158 delete ResourceTracker;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000159}
160
161// endPacket - End the current packet, bundle packet instructions and reset
162// DFA state.
163void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
Sirish Pande94212162012-05-01 21:28:30 +0000164 MachineInstr *MI) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000165 if (CurrentPacketMIs.size() > 1) {
166 MachineInstr *MIFirst = CurrentPacketMIs.front();
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000167 finalizeBundle(*MBB, MIFirst->getIterator(), MI->getIterator());
Andrew Trick7a35fae2012-02-15 18:55:14 +0000168 }
169 CurrentPacketMIs.clear();
170 ResourceTracker->clearResources();
171}
172
173// PacketizeMIs - Bundle machine instructions into packets.
174void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
175 MachineBasicBlock::iterator BeginItr,
176 MachineBasicBlock::iterator EndItr) {
Sirish Pande94212162012-05-01 21:28:30 +0000177 assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
178 VLIWScheduler->startBlock(MBB);
Andrew Tricka53e1012013-08-23 17:48:33 +0000179 VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
180 std::distance(BeginItr, EndItr));
Sirish Pande94212162012-05-01 21:28:30 +0000181 VLIWScheduler->schedule();
Andrew Trick69b42042012-03-07 23:01:09 +0000182
Sirish Pande94212162012-05-01 21:28:30 +0000183 // Generate MI -> SU map.
184 MIToSUnit.clear();
185 for (unsigned i = 0, e = VLIWScheduler->SUnits.size(); i != e; ++i) {
186 SUnit *SU = &VLIWScheduler->SUnits[i];
187 MIToSUnit[SU->getInstr()] = SU;
188 }
Andrew Trick7a35fae2012-02-15 18:55:14 +0000189
190 // The main packetizer loop.
191 for (; BeginItr != EndItr; ++BeginItr) {
192 MachineInstr *MI = BeginItr;
193
Sirish Pande94212162012-05-01 21:28:30 +0000194 this->initPacketizerState();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000195
196 // End the current packet if needed.
Sirish Pande94212162012-05-01 21:28:30 +0000197 if (this->isSoloInstruction(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000198 endPacket(MBB, MI);
199 continue;
200 }
201
Sirish Pande94212162012-05-01 21:28:30 +0000202 // Ignore pseudo instructions.
203 if (this->ignorePseudoInstruction(MI, MBB))
204 continue;
205
206 SUnit *SUI = MIToSUnit[MI];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000207 assert(SUI && "Missing SUnit Info!");
208
209 // Ask DFA if machine resource is available for MI.
210 bool ResourceAvail = ResourceTracker->canReserveResources(MI);
211 if (ResourceAvail) {
212 // Dependency check for MI with instructions in CurrentPacketMIs.
213 for (std::vector<MachineInstr*>::iterator VI = CurrentPacketMIs.begin(),
214 VE = CurrentPacketMIs.end(); VI != VE; ++VI) {
215 MachineInstr *MJ = *VI;
Sirish Pande94212162012-05-01 21:28:30 +0000216 SUnit *SUJ = MIToSUnit[MJ];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000217 assert(SUJ && "Missing SUnit Info!");
218
219 // Is it legal to packetize SUI and SUJ together.
Sirish Pande94212162012-05-01 21:28:30 +0000220 if (!this->isLegalToPacketizeTogether(SUI, SUJ)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000221 // Allow packetization if dependency can be pruned.
Sirish Pande94212162012-05-01 21:28:30 +0000222 if (!this->isLegalToPruneDependencies(SUI, SUJ)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000223 // End the packet if dependency cannot be pruned.
224 endPacket(MBB, MI);
225 break;
226 } // !isLegalToPruneDependencies.
227 } // !isLegalToPacketizeTogether.
228 } // For all instructions in CurrentPacketMIs.
229 } else {
230 // End the packet if resource is not available.
231 endPacket(MBB, MI);
232 }
233
234 // Add MI to the current packet.
Sirish Pande94212162012-05-01 21:28:30 +0000235 BeginItr = this->addToPacket(MI);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000236 } // For all instructions in BB.
237
238 // End any packet left behind.
239 endPacket(MBB, EndItr);
Sirish Pande94212162012-05-01 21:28:30 +0000240 VLIWScheduler->exitRegion();
241 VLIWScheduler->finishBlock();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000242}