Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 1 | //=- 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 Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 26 | #define DEBUG_TYPE "packets" |
| 27 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/DFAPacketizer.h" |
| 29 | #include "llvm/CodeGen/MachineInstr.h" |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineInstrBundle.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCInstrItineraries.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetInstrInfo.h" |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 34 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Krzysztof Parzyszek | 6753f33 | 2015-11-22 15:20:19 +0000 | [diff] [blame] | 37 | // -------------------------------------------------------------------- |
| 38 | // Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp |
| 39 | |
| 40 | namespace { |
| 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 Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 50 | assert((InsnClass.size() <= DFA_MAX_RESTERMS) && |
| 51 | "Exceeded maximum number of DFA terms"); |
Krzysztof Parzyszek | 6753f33 | 2015-11-22 15:20:19 +0000 | [diff] [blame] | 52 | for (auto U : InsnClass) |
| 53 | InsnInput = addDFAFuncUnits(InsnInput, U); |
| 54 | return InsnInput; |
| 55 | } |
| 56 | } |
| 57 | // -------------------------------------------------------------------- |
| 58 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 59 | DFAPacketizer::DFAPacketizer(const InstrItineraryData *I, |
| 60 | const DFAStateInput (*SIT)[2], |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 61 | const unsigned *SET): |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 62 | InstrItins(I), CurrentState(0), DFAStateInputTable(SIT), |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 63 | DFAStateEntryTable(SET) { |
| 64 | // Make sure DFA types are large enough for the number of terms & resources. |
Benjamin Kramer | 3e9a5d3 | 2016-05-27 11:36:04 +0000 | [diff] [blame] | 65 | 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 Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 71 | } |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 72 | |
| 73 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 74 | // Read the DFA transition table and update CachedTable. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 75 | // |
| 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 | // |
| 82 | void 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 Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 86 | // state's transitions. |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 87 | if (CachedTable.count(UnsignPair(state, DFAStateInputTable[ThisState][0]))) |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 88 | return; |
| 89 | |
| 90 | for (unsigned i = ThisState; i < NextStateInTable; i++) |
| 91 | CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] = |
| 92 | DFAStateInputTable[i][1]; |
| 93 | } |
| 94 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 95 | |
| 96 | // Return the DFAInput for an instruction class. |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 97 | DFAInput 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 Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 101 | (void)i; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 102 | for (const InstrStage *IS = InstrItins->beginStage(InsnClass), |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 103 | *IE = InstrItins->endStage(InsnClass); IS != IE; ++IS) { |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 104 | InsnInput = addDFAFuncUnits(InsnInput, IS->getUnits()); |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 105 | assert((i++ < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs"); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 106 | } |
| 107 | return InsnInput; |
| 108 | } |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 109 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 110 | |
| 111 | // Return the DFAInput for an instruction class input vector. |
Krzysztof Parzyszek | 6753f33 | 2015-11-22 15:20:19 +0000 | [diff] [blame] | 112 | DFAInput DFAPacketizer::getInsnInput(const std::vector<unsigned> &InsnClass) { |
| 113 | return getDFAInsnInput(InsnClass); |
| 114 | } |
| 115 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 116 | |
| 117 | // Check if the resources occupied by a MCInstrDesc are available in the |
| 118 | // current state. |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 119 | bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc *MID) { |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 120 | unsigned InsnClass = MID->getSchedClass(); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 121 | DFAInput InsnInput = getInsnInput(InsnClass); |
| 122 | UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 123 | ReadTable(CurrentState); |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 124 | return CachedTable.count(StateTrans) != 0; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 127 | |
| 128 | // Reserve the resources occupied by a MCInstrDesc and change the current |
| 129 | // state to reflect that change. |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 130 | void DFAPacketizer::reserveResources(const llvm::MCInstrDesc *MID) { |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 131 | unsigned InsnClass = MID->getSchedClass(); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 132 | DFAInput InsnInput = getInsnInput(InsnClass); |
| 133 | UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 134 | ReadTable(CurrentState); |
| 135 | assert(CachedTable.count(StateTrans) != 0); |
| 136 | CurrentState = CachedTable[StateTrans]; |
| 137 | } |
| 138 | |
| 139 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 140 | // Check if the resources occupied by a machine instruction are available |
| 141 | // in the current state. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 142 | bool DFAPacketizer::canReserveResources(llvm::MachineInstr &MI) { |
| 143 | const llvm::MCInstrDesc &MID = MI.getDesc(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 144 | return canReserveResources(&MID); |
| 145 | } |
| 146 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 147 | |
| 148 | // Reserve the resources occupied by a machine instruction and change the |
| 149 | // current state to reflect that change. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 150 | void DFAPacketizer::reserveResources(llvm::MachineInstr &MI) { |
| 151 | const llvm::MCInstrDesc &MID = MI.getDesc(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 152 | reserveResources(&MID); |
| 153 | } |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 154 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 155 | |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 156 | namespace llvm { |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 157 | // This class extends ScheduleDAGInstrs and overrides the schedule method |
| 158 | // to build the dependence graph. |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 159 | class DefaultVLIWScheduler : public ScheduleDAGInstrs { |
Krzysztof Parzyszek | dac7102 | 2015-12-14 20:35:13 +0000 | [diff] [blame] | 160 | private: |
| 161 | AliasAnalysis *AA; |
Krzysztof Parzyszek | 1a1d78b | 2016-03-08 15:33:51 +0000 | [diff] [blame] | 162 | /// Ordered list of DAG postprocessing steps. |
| 163 | std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations; |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 164 | public: |
Krzysztof Parzyszek | dac7102 | 2015-12-14 20:35:13 +0000 | [diff] [blame] | 165 | DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI, |
| 166 | AliasAnalysis *AA); |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 167 | // Actual scheduling work. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 168 | void schedule() override; |
Krzysztof Parzyszek | 1a1d78b | 2016-03-08 15:33:51 +0000 | [diff] [blame] | 169 | |
| 170 | /// DefaultVLIWScheduler takes ownership of the Mutation object. |
| 171 | void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) { |
| 172 | Mutations.push_back(std::move(Mutation)); |
| 173 | } |
| 174 | protected: |
| 175 | void postprocessDAG(); |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 176 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 177 | } |
Andrew Trick | 20349b8 | 2012-02-15 23:34:15 +0000 | [diff] [blame] | 178 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 179 | |
Alexey Samsonov | ea0aee6 | 2014-08-20 20:57:26 +0000 | [diff] [blame] | 180 | DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF, |
Krzysztof Parzyszek | dac7102 | 2015-12-14 20:35:13 +0000 | [diff] [blame] | 181 | MachineLoopInfo &MLI, |
| 182 | AliasAnalysis *AA) |
| 183 | : ScheduleDAGInstrs(MF, &MLI), AA(AA) { |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 184 | CanHandleTerminators = true; |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 187 | |
Krzysztof Parzyszek | 1a1d78b | 2016-03-08 15:33:51 +0000 | [diff] [blame] | 188 | /// Apply each ScheduleDAGMutation step in order. |
| 189 | void DefaultVLIWScheduler::postprocessDAG() { |
| 190 | for (auto &M : Mutations) |
| 191 | M->apply(this); |
| 192 | } |
| 193 | |
| 194 | |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 195 | void DefaultVLIWScheduler::schedule() { |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 196 | // Build the scheduling graph. |
Krzysztof Parzyszek | dac7102 | 2015-12-14 20:35:13 +0000 | [diff] [blame] | 197 | buildSchedGraph(AA); |
Krzysztof Parzyszek | 1a1d78b | 2016-03-08 15:33:51 +0000 | [diff] [blame] | 198 | postprocessDAG(); |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 201 | |
| 202 | VLIWPacketizerList::VLIWPacketizerList(MachineFunction &mf, |
| 203 | MachineLoopInfo &mli, AliasAnalysis *aa) |
| 204 | : MF(mf), TII(mf.getSubtarget().getInstrInfo()), AA(aa) { |
Eric Christopher | 143f02c | 2014-10-09 01:59:35 +0000 | [diff] [blame] | 205 | ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget()); |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 206 | VLIWScheduler = new DefaultVLIWScheduler(MF, mli, AA); |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 209 | |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 210 | VLIWPacketizerList::~VLIWPacketizerList() { |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 211 | if (VLIWScheduler) |
| 212 | delete VLIWScheduler; |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 213 | if (ResourceTracker) |
| 214 | delete ResourceTracker; |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 217 | |
| 218 | // End the current packet, bundle packet instructions and reset DFA state. |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 219 | void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB, |
| 220 | MachineBasicBlock::iterator MI) { |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 221 | if (CurrentPacketMIs.size() > 1) { |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 222 | MachineInstr &MIFirst = *CurrentPacketMIs.front(); |
| 223 | finalizeBundle(*MBB, MIFirst.getIterator(), MI.getInstrIterator()); |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 224 | } |
| 225 | CurrentPacketMIs.clear(); |
| 226 | ResourceTracker->clearResources(); |
Krzysztof Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 227 | DEBUG(dbgs() << "End packet\n"); |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 230 | |
| 231 | // Bundle machine instructions into packets. |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 232 | void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB, |
| 233 | MachineBasicBlock::iterator BeginItr, |
| 234 | MachineBasicBlock::iterator EndItr) { |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 235 | assert(VLIWScheduler && "VLIW Scheduler is not initialized!"); |
| 236 | VLIWScheduler->startBlock(MBB); |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 237 | VLIWScheduler->enterRegion(MBB, BeginItr, EndItr, |
| 238 | std::distance(BeginItr, EndItr)); |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 239 | VLIWScheduler->schedule(); |
Andrew Trick | 69b4204 | 2012-03-07 23:01:09 +0000 | [diff] [blame] | 240 | |
Krzysztof Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 241 | DEBUG({ |
| 242 | dbgs() << "Scheduling DAG of the packetize region\n"; |
| 243 | for (SUnit &SU : VLIWScheduler->SUnits) |
| 244 | SU.dumpAll(VLIWScheduler); |
| 245 | }); |
| 246 | |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 247 | // Generate MI -> SU map. |
| 248 | MIToSUnit.clear(); |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 249 | for (SUnit &SU : VLIWScheduler->SUnits) |
| 250 | MIToSUnit[SU.getInstr()] = &SU; |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 251 | |
| 252 | // The main packetizer loop. |
| 253 | for (; BeginItr != EndItr; ++BeginItr) { |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 254 | MachineInstr &MI = *BeginItr; |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 255 | initPacketizerState(); |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 256 | |
| 257 | // End the current packet if needed. |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 258 | if (isSoloInstruction(MI)) { |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 259 | endPacket(MBB, MI); |
| 260 | continue; |
| 261 | } |
| 262 | |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 263 | // Ignore pseudo instructions. |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 264 | if (ignorePseudoInstruction(MI, MBB)) |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 265 | continue; |
| 266 | |
Duncan P. N. Exon Smith | 5702287 | 2016-02-27 19:09:00 +0000 | [diff] [blame] | 267 | SUnit *SUI = MIToSUnit[&MI]; |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 268 | assert(SUI && "Missing SUnit Info!"); |
| 269 | |
| 270 | // Ask DFA if machine resource is available for MI. |
Krzysztof Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 271 | DEBUG(dbgs() << "Checking resources for adding MI to packet " << MI); |
| 272 | |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 273 | bool ResourceAvail = ResourceTracker->canReserveResources(MI); |
Krzysztof Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 274 | 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 Parzyszek | 2005d7d | 2015-12-16 16:38:16 +0000 | [diff] [blame] | 280 | if (ResourceAvail && shouldAddToPacket(MI)) { |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 281 | // Dependency check for MI with instructions in CurrentPacketMIs. |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 282 | for (auto MJ : CurrentPacketMIs) { |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 283 | SUnit *SUJ = MIToSUnit[MJ]; |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 284 | assert(SUJ && "Missing SUnit Info!"); |
| 285 | |
Krzysztof Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 286 | DEBUG(dbgs() << " Checking against MJ " << *MJ); |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 287 | // Is it legal to packetize SUI and SUJ together. |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 288 | if (!isLegalToPacketizeTogether(SUI, SUJ)) { |
Krzysztof Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 289 | DEBUG(dbgs() << " Not legal to add MI, try to prune\n"); |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 290 | // Allow packetization if dependency can be pruned. |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 291 | if (!isLegalToPruneDependencies(SUI, SUJ)) { |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 292 | // End the packet if dependency cannot be pruned. |
Krzysztof Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 293 | DEBUG(dbgs() << " Could not prune dependencies for adding MI\n"); |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 294 | endPacket(MBB, MI); |
| 295 | break; |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 296 | } |
Krzysztof Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 297 | DEBUG(dbgs() << " Pruned dependence for adding MI\n"); |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 300 | } else { |
Krzysztof Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 301 | DEBUG(if (ResourceAvail) |
| 302 | dbgs() << "Resources are available, but instruction should not be " |
| 303 | "added to packet\n " << MI); |
Krzysztof Parzyszek | 2005d7d | 2015-12-16 16:38:16 +0000 | [diff] [blame] | 304 | // End the packet if resource is not available, or if the instruction |
| 305 | // shoud not be added to the current packet. |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 306 | endPacket(MBB, MI); |
| 307 | } |
| 308 | |
| 309 | // Add MI to the current packet. |
Krzysztof Parzyszek | 31ceba7 | 2016-07-14 19:04:26 +0000 | [diff] [blame] | 310 | DEBUG(dbgs() << "* Adding MI to packet " << MI << '\n'); |
Krzysztof Parzyszek | c005e20 | 2016-01-14 21:17:04 +0000 | [diff] [blame] | 311 | BeginItr = addToPacket(MI); |
| 312 | } // For all instructions in the packetization range. |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 313 | |
| 314 | // End any packet left behind. |
| 315 | endPacket(MBB, EndItr); |
Sirish Pande | 9421216 | 2012-05-01 21:28:30 +0000 | [diff] [blame] | 316 | VLIWScheduler->exitRegion(); |
| 317 | VLIWScheduler->finishBlock(); |
Andrew Trick | 7a35fae | 2012-02-15 18:55:14 +0000 | [diff] [blame] | 318 | } |
Krzysztof Parzyszek | 1a1d78b | 2016-03-08 15:33:51 +0000 | [diff] [blame] | 319 | |
| 320 | |
| 321 | // Add a DAG mutation object to the ordered list. |
| 322 | void VLIWPacketizerList::addMutation( |
| 323 | std::unique_ptr<ScheduleDAGMutation> Mutation) { |
| 324 | VLIWScheduler->addMutation(std::move(Mutation)); |
| 325 | } |