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 26 #include "llvm/CodeGen/DFAPacketizer.h" 27 #include "llvm/CodeGen/MachineInstr.h" 28 #include "llvm/CodeGen/MachineInstrBundle.h" 29 #include "llvm/CodeGen/ScheduleDAGInstrs.h" 30 #include "llvm/MC/MCInstrItineraries.h" 31 #include "llvm/Target/TargetInstrInfo.h" 32 using namespace llvm; 33 34 DFAPacketizer::DFAPacketizer(const InstrItineraryData *I, 35 const DFAStateInput (*SIT)[2], 36 const unsigned *SET): 37 InstrItins(I), CurrentState(0), DFAStateInputTable(SIT), 38 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 } 45 46 47 // 48 // ReadTable - Read the DFA transition table and update CachedTable. 49 // 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 // 56 void 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 60 // state's transitions. 61 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 70 // 71 // getInsnInput - Return the DFAInput for an instruction class. 72 // 73 DFAInput DFAPacketizer::getInsnInput(unsigned InsnClass) { 74 // note: this logic must match that in DFAPacketizer.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 unsigned FuncUnits = IS->getUnits(); 80 InsnInput <<= DFA_MAX_RESOURCES; // Shift over any previous AND'ed terms. 81 InsnInput |= FuncUnits; 82 assert ((i < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs"); 83 } 84 85 return InsnInput; 86 } 87 88 // canReserveResources - Check if the resources occupied by a MCInstrDesc 89 // are available in the current state. 90 bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc *MID) { 91 unsigned InsnClass = MID->getSchedClass(); 92 DFAInput InsnInput = getInsnInput(InsnClass); 93 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput); 94 ReadTable(CurrentState); 95 return (CachedTable.count(StateTrans) != 0); 96 } 97 98 // reserveResources - Reserve the resources occupied by a MCInstrDesc and 99 // change the current state to reflect that change. 100 void DFAPacketizer::reserveResources(const llvm::MCInstrDesc *MID) { 101 unsigned InsnClass = MID->getSchedClass(); 102 DFAInput InsnInput = getInsnInput(InsnClass); 103 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput); 104 ReadTable(CurrentState); 105 assert(CachedTable.count(StateTrans) != 0); 106 CurrentState = CachedTable[StateTrans]; 107 } 108 109 110 // canReserveResources - Check if the resources occupied by a machine 111 // instruction are available in the current state. 112 bool DFAPacketizer::canReserveResources(llvm::MachineInstr *MI) { 113 const llvm::MCInstrDesc &MID = MI->getDesc(); 114 return canReserveResources(&MID); 115 } 116 117 // reserveResources - Reserve the resources occupied by a machine 118 // instruction and change the current state to reflect that change. 119 void DFAPacketizer::reserveResources(llvm::MachineInstr *MI) { 120 const llvm::MCInstrDesc &MID = MI->getDesc(); 121 reserveResources(&MID); 122 } 123 124 namespace llvm { 125 // DefaultVLIWScheduler - This class extends ScheduleDAGInstrs and overrides 126 // Schedule method to build the dependence graph. 127 class DefaultVLIWScheduler : public ScheduleDAGInstrs { 128 public: 129 DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI); 130 // Schedule - Actual scheduling work. 131 void schedule() override; 132 }; 133 } 134 135 DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF, 136 MachineLoopInfo &MLI) 137 : ScheduleDAGInstrs(MF, &MLI) { 138 CanHandleTerminators = true; 139 } 140 141 void DefaultVLIWScheduler::schedule() { 142 // Build the scheduling graph. 143 buildSchedGraph(nullptr); 144 } 145 146 // VLIWPacketizerList Ctor 147 VLIWPacketizerList::VLIWPacketizerList(MachineFunction &MF, 148 MachineLoopInfo &MLI) 149 : MF(MF) { 150 TII = MF.getSubtarget().getInstrInfo(); 151 ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget()); 152 VLIWScheduler = new DefaultVLIWScheduler(MF, MLI); 153 } 154 155 // VLIWPacketizerList Dtor 156 VLIWPacketizerList::~VLIWPacketizerList() { 157 if (VLIWScheduler) 158 delete VLIWScheduler; 159 160 if (ResourceTracker) 161 delete ResourceTracker; 162 } 163 164 // endPacket - End the current packet, bundle packet instructions and reset 165 // DFA state. 166 void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB, 167 MachineInstr *MI) { 168 if (CurrentPacketMIs.size() > 1) { 169 MachineInstr *MIFirst = CurrentPacketMIs.front(); 170 finalizeBundle(*MBB, MIFirst->getIterator(), MI->getIterator()); 171 } 172 CurrentPacketMIs.clear(); 173 ResourceTracker->clearResources(); 174 } 175 176 // PacketizeMIs - Bundle machine instructions into packets. 177 void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB, 178 MachineBasicBlock::iterator BeginItr, 179 MachineBasicBlock::iterator EndItr) { 180 assert(VLIWScheduler && "VLIW Scheduler is not initialized!"); 181 VLIWScheduler->startBlock(MBB); 182 VLIWScheduler->enterRegion(MBB, BeginItr, EndItr, 183 std::distance(BeginItr, EndItr)); 184 VLIWScheduler->schedule(); 185 186 // Generate MI -> SU map. 187 MIToSUnit.clear(); 188 for (unsigned i = 0, e = VLIWScheduler->SUnits.size(); i != e; ++i) { 189 SUnit *SU = &VLIWScheduler->SUnits[i]; 190 MIToSUnit[SU->getInstr()] = SU; 191 } 192 193 // The main packetizer loop. 194 for (; BeginItr != EndItr; ++BeginItr) { 195 MachineInstr *MI = BeginItr; 196 197 this->initPacketizerState(); 198 199 // End the current packet if needed. 200 if (this->isSoloInstruction(MI)) { 201 endPacket(MBB, MI); 202 continue; 203 } 204 205 // Ignore pseudo instructions. 206 if (this->ignorePseudoInstruction(MI, MBB)) 207 continue; 208 209 SUnit *SUI = MIToSUnit[MI]; 210 assert(SUI && "Missing SUnit Info!"); 211 212 // Ask DFA if machine resource is available for MI. 213 bool ResourceAvail = ResourceTracker->canReserveResources(MI); 214 if (ResourceAvail) { 215 // Dependency check for MI with instructions in CurrentPacketMIs. 216 for (std::vector<MachineInstr*>::iterator VI = CurrentPacketMIs.begin(), 217 VE = CurrentPacketMIs.end(); VI != VE; ++VI) { 218 MachineInstr *MJ = *VI; 219 SUnit *SUJ = MIToSUnit[MJ]; 220 assert(SUJ && "Missing SUnit Info!"); 221 222 // Is it legal to packetize SUI and SUJ together. 223 if (!this->isLegalToPacketizeTogether(SUI, SUJ)) { 224 // Allow packetization if dependency can be pruned. 225 if (!this->isLegalToPruneDependencies(SUI, SUJ)) { 226 // End the packet if dependency cannot be pruned. 227 endPacket(MBB, MI); 228 break; 229 } // !isLegalToPruneDependencies. 230 } // !isLegalToPacketizeTogether. 231 } // For all instructions in CurrentPacketMIs. 232 } else { 233 // End the packet if resource is not available. 234 endPacket(MBB, MI); 235 } 236 237 // Add MI to the current packet. 238 BeginItr = this->addToPacket(MI); 239 } // For all instructions in BB. 240 241 // End any packet left behind. 242 endPacket(MBB, EndItr); 243 VLIWScheduler->exitRegion(); 244 VLIWScheduler->finishBlock(); 245 } 246