blob: ee50f972aa7b04e84e0f3a7b55eace53b285cd74 [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
34DFAPacketizer::DFAPacketizer(const InstrItineraryData *I, const int (*SIT)[2],
Sebastian Popac35a4d2011-12-06 17:34:16 +000035 const unsigned *SET):
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000036 InstrItins(I), CurrentState(0), DFAStateInputTable(SIT),
37 DFAStateEntryTable(SET) {}
38
39
40//
Sebastian Pop9aa61372011-12-06 17:34:11 +000041// ReadTable - Read the DFA transition table and update CachedTable.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000042//
43// Format of the transition tables:
44// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
45// transitions
46// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable
47// for the ith state
48//
49void DFAPacketizer::ReadTable(unsigned int state) {
50 unsigned ThisState = DFAStateEntryTable[state];
51 unsigned NextStateInTable = DFAStateEntryTable[state+1];
52 // Early exit in case CachedTable has already contains this
Sebastian Pop9aa61372011-12-06 17:34:11 +000053 // state's transitions.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000054 if (CachedTable.count(UnsignPair(state,
55 DFAStateInputTable[ThisState][0])))
56 return;
57
58 for (unsigned i = ThisState; i < NextStateInTable; i++)
59 CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] =
60 DFAStateInputTable[i][1];
61}
62
63
64// canReserveResources - Check if the resources occupied by a MCInstrDesc
Sebastian Pop9aa61372011-12-06 17:34:11 +000065// are available in the current state.
Sebastian Popac35a4d2011-12-06 17:34:16 +000066bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000067 unsigned InsnClass = MID->getSchedClass();
Sebastian Popac35a4d2011-12-06 17:34:16 +000068 const llvm::InstrStage *IS = InstrItins->beginStage(InsnClass);
Hal Finkel8db55472012-06-22 20:27:13 +000069 unsigned FuncUnits = IS->getUnits();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000070 UnsignPair StateTrans = UnsignPair(CurrentState, FuncUnits);
71 ReadTable(CurrentState);
72 return (CachedTable.count(StateTrans) != 0);
73}
74
75
76// reserveResources - Reserve the resources occupied by a MCInstrDesc and
Sebastian Pop9aa61372011-12-06 17:34:11 +000077// change the current state to reflect that change.
Sebastian Popac35a4d2011-12-06 17:34:16 +000078void DFAPacketizer::reserveResources(const llvm::MCInstrDesc *MID) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000079 unsigned InsnClass = MID->getSchedClass();
Sebastian Popac35a4d2011-12-06 17:34:16 +000080 const llvm::InstrStage *IS = InstrItins->beginStage(InsnClass);
Hal Finkel8db55472012-06-22 20:27:13 +000081 unsigned FuncUnits = IS->getUnits();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000082 UnsignPair StateTrans = UnsignPair(CurrentState, FuncUnits);
83 ReadTable(CurrentState);
84 assert(CachedTable.count(StateTrans) != 0);
85 CurrentState = CachedTable[StateTrans];
86}
87
88
89// canReserveResources - Check if the resources occupied by a machine
Sebastian Pop9aa61372011-12-06 17:34:11 +000090// instruction are available in the current state.
Sebastian Popac35a4d2011-12-06 17:34:16 +000091bool DFAPacketizer::canReserveResources(llvm::MachineInstr *MI) {
92 const llvm::MCInstrDesc &MID = MI->getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000093 return canReserveResources(&MID);
94}
95
96// reserveResources - Reserve the resources occupied by a machine
Sebastian Pop9aa61372011-12-06 17:34:11 +000097// instruction and change the current state to reflect that change.
Sebastian Popac35a4d2011-12-06 17:34:16 +000098void DFAPacketizer::reserveResources(llvm::MachineInstr *MI) {
99 const llvm::MCInstrDesc &MID = MI->getDesc();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000100 reserveResources(&MID);
101}
Andrew Trick7a35fae2012-02-15 18:55:14 +0000102
Sirish Pande94212162012-05-01 21:28:30 +0000103namespace llvm {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000104// DefaultVLIWScheduler - This class extends ScheduleDAGInstrs and overrides
105// Schedule method to build the dependence graph.
106class DefaultVLIWScheduler : public ScheduleDAGInstrs {
107public:
Matthias Braun93563e72015-11-03 01:53:29 +0000108 DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000109 // Schedule - Actual scheduling work.
Craig Topper4584cd52014-03-07 09:26:03 +0000110 void schedule() override;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000111};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000112}
Andrew Trick20349b82012-02-15 23:34:15 +0000113
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000114DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
Matthias Braun93563e72015-11-03 01:53:29 +0000115 MachineLoopInfo &MLI)
116 : ScheduleDAGInstrs(MF, &MLI) {
Sirish Pande94212162012-05-01 21:28:30 +0000117 CanHandleTerminators = true;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000118}
119
Andrew Trick52226d42012-03-07 23:00:49 +0000120void DefaultVLIWScheduler::schedule() {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000121 // Build the scheduling graph.
Craig Topperc0196b12014-04-14 00:51:57 +0000122 buildSchedGraph(nullptr);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000123}
124
125// VLIWPacketizerList Ctor
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000126VLIWPacketizerList::VLIWPacketizerList(MachineFunction &MF,
Matthias Braun93563e72015-11-03 01:53:29 +0000127 MachineLoopInfo &MLI)
Eric Christopher2a321f72014-10-14 01:03:16 +0000128 : MF(MF) {
129 TII = MF.getSubtarget().getInstrInfo();
Eric Christopher143f02c2014-10-09 01:59:35 +0000130 ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
Matthias Braun93563e72015-11-03 01:53:29 +0000131 VLIWScheduler = new DefaultVLIWScheduler(MF, MLI);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000132}
133
134// VLIWPacketizerList Dtor
135VLIWPacketizerList::~VLIWPacketizerList() {
Sirish Pande94212162012-05-01 21:28:30 +0000136 if (VLIWScheduler)
137 delete VLIWScheduler;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000138
Sirish Pande94212162012-05-01 21:28:30 +0000139 if (ResourceTracker)
140 delete ResourceTracker;
Andrew Trick7a35fae2012-02-15 18:55:14 +0000141}
142
143// endPacket - End the current packet, bundle packet instructions and reset
144// DFA state.
145void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
Sirish Pande94212162012-05-01 21:28:30 +0000146 MachineInstr *MI) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000147 if (CurrentPacketMIs.size() > 1) {
148 MachineInstr *MIFirst = CurrentPacketMIs.front();
Duncan P. N. Exon Smithd83547a2015-10-09 18:44:40 +0000149 finalizeBundle(*MBB, MIFirst->getIterator(), MI->getIterator());
Andrew Trick7a35fae2012-02-15 18:55:14 +0000150 }
151 CurrentPacketMIs.clear();
152 ResourceTracker->clearResources();
153}
154
155// PacketizeMIs - Bundle machine instructions into packets.
156void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
157 MachineBasicBlock::iterator BeginItr,
158 MachineBasicBlock::iterator EndItr) {
Sirish Pande94212162012-05-01 21:28:30 +0000159 assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
160 VLIWScheduler->startBlock(MBB);
Andrew Tricka53e1012013-08-23 17:48:33 +0000161 VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
162 std::distance(BeginItr, EndItr));
Sirish Pande94212162012-05-01 21:28:30 +0000163 VLIWScheduler->schedule();
Andrew Trick69b42042012-03-07 23:01:09 +0000164
Sirish Pande94212162012-05-01 21:28:30 +0000165 // Generate MI -> SU map.
166 MIToSUnit.clear();
167 for (unsigned i = 0, e = VLIWScheduler->SUnits.size(); i != e; ++i) {
168 SUnit *SU = &VLIWScheduler->SUnits[i];
169 MIToSUnit[SU->getInstr()] = SU;
170 }
Andrew Trick7a35fae2012-02-15 18:55:14 +0000171
172 // The main packetizer loop.
173 for (; BeginItr != EndItr; ++BeginItr) {
174 MachineInstr *MI = BeginItr;
175
Sirish Pande94212162012-05-01 21:28:30 +0000176 this->initPacketizerState();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000177
178 // End the current packet if needed.
Sirish Pande94212162012-05-01 21:28:30 +0000179 if (this->isSoloInstruction(MI)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000180 endPacket(MBB, MI);
181 continue;
182 }
183
Sirish Pande94212162012-05-01 21:28:30 +0000184 // Ignore pseudo instructions.
185 if (this->ignorePseudoInstruction(MI, MBB))
186 continue;
187
188 SUnit *SUI = MIToSUnit[MI];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000189 assert(SUI && "Missing SUnit Info!");
190
191 // Ask DFA if machine resource is available for MI.
192 bool ResourceAvail = ResourceTracker->canReserveResources(MI);
193 if (ResourceAvail) {
194 // Dependency check for MI with instructions in CurrentPacketMIs.
195 for (std::vector<MachineInstr*>::iterator VI = CurrentPacketMIs.begin(),
196 VE = CurrentPacketMIs.end(); VI != VE; ++VI) {
197 MachineInstr *MJ = *VI;
Sirish Pande94212162012-05-01 21:28:30 +0000198 SUnit *SUJ = MIToSUnit[MJ];
Andrew Trick7a35fae2012-02-15 18:55:14 +0000199 assert(SUJ && "Missing SUnit Info!");
200
201 // Is it legal to packetize SUI and SUJ together.
Sirish Pande94212162012-05-01 21:28:30 +0000202 if (!this->isLegalToPacketizeTogether(SUI, SUJ)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000203 // Allow packetization if dependency can be pruned.
Sirish Pande94212162012-05-01 21:28:30 +0000204 if (!this->isLegalToPruneDependencies(SUI, SUJ)) {
Andrew Trick7a35fae2012-02-15 18:55:14 +0000205 // End the packet if dependency cannot be pruned.
206 endPacket(MBB, MI);
207 break;
208 } // !isLegalToPruneDependencies.
209 } // !isLegalToPacketizeTogether.
210 } // For all instructions in CurrentPacketMIs.
211 } else {
212 // End the packet if resource is not available.
213 endPacket(MBB, MI);
214 }
215
216 // Add MI to the current packet.
Sirish Pande94212162012-05-01 21:28:30 +0000217 BeginItr = this->addToPacket(MI);
Andrew Trick7a35fae2012-02-15 18:55:14 +0000218 } // For all instructions in BB.
219
220 // End any packet left behind.
221 endPacket(MBB, EndItr);
Sirish Pande94212162012-05-01 21:28:30 +0000222 VLIWScheduler->exitRegion();
223 VLIWScheduler->finishBlock();
Andrew Trick7a35fae2012-02-15 18:55:14 +0000224}