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 // -------------------------------------------------------------------- 35 // Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp 36 37 namespace { 38 DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) { 39 return (Inp << DFA_MAX_RESOURCES) | FuncUnits; 40 } 41 42 /// Return the DFAInput for an instruction class input vector. 43 /// This function is used in both DFAPacketizer.cpp and in 44 /// DFAPacketizerEmitter.cpp. 45 DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) { 46 DFAInput InsnInput = 0; 47 assert ((InsnClass.size() <= DFA_MAX_RESTERMS) && 48 "Exceeded maximum number of DFA terms"); 49 for (auto U : InsnClass) 50 InsnInput = addDFAFuncUnits(InsnInput, U); 51 return InsnInput; 52 } 53 } 54 // -------------------------------------------------------------------- 55 56 DFAPacketizer::DFAPacketizer(const InstrItineraryData *I, 57 const DFAStateInput (*SIT)[2], 58 const unsigned *SET): 59 InstrItins(I), CurrentState(0), DFAStateInputTable(SIT), 60 DFAStateEntryTable(SET) { 61 // Make sure DFA types are large enough for the number of terms & resources. 62 assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAInput)) 63 && "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAInput"); 64 assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAStateInput)) 65 && "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAStateInput"); 66 } 67 68 69 // 70 // ReadTable - Read the DFA transition table and update CachedTable. 71 // 72 // Format of the transition tables: 73 // DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid 74 // transitions 75 // DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable 76 // for the ith state 77 // 78 void DFAPacketizer::ReadTable(unsigned int state) { 79 unsigned ThisState = DFAStateEntryTable[state]; 80 unsigned NextStateInTable = DFAStateEntryTable[state+1]; 81 // Early exit in case CachedTable has already contains this 82 // state's transitions. 83 if (CachedTable.count(UnsignPair(state, 84 DFAStateInputTable[ThisState][0]))) 85 return; 86 87 for (unsigned i = ThisState; i < NextStateInTable; i++) 88 CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] = 89 DFAStateInputTable[i][1]; 90 } 91 92 // 93 // getInsnInput - Return the DFAInput for an instruction class. 94 // 95 DFAInput DFAPacketizer::getInsnInput(unsigned InsnClass) { 96 // Note: this logic must match that in DFAPacketizerDefs.h for input vectors. 97 DFAInput InsnInput = 0; 98 unsigned i = 0; 99 for (const InstrStage *IS = InstrItins->beginStage(InsnClass), 100 *IE = InstrItins->endStage(InsnClass); IS != IE; ++IS, ++i) { 101 InsnInput = addDFAFuncUnits(InsnInput, IS->getUnits()); 102 assert ((i < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs"); 103 } 104 return InsnInput; 105 } 106 107 // getInsnInput - Return the DFAInput for an instruction class input vector. 108 DFAInput DFAPacketizer::getInsnInput(const std::vector<unsigned> &InsnClass) { 109 return getDFAInsnInput(InsnClass); 110 } 111 112 // canReserveResources - Check if the resources occupied by a MCInstrDesc 113 // are available in the current state. 114 bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc *MID) { 115 unsigned InsnClass = MID->getSchedClass(); 116 DFAInput InsnInput = getInsnInput(InsnClass); 117 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput); 118 ReadTable(CurrentState); 119 return (CachedTable.count(StateTrans) != 0); 120 } 121 122 // reserveResources - Reserve the resources occupied by a MCInstrDesc and 123 // change the current state to reflect that change. 124 void DFAPacketizer::reserveResources(const llvm::MCInstrDesc *MID) { 125 unsigned InsnClass = MID->getSchedClass(); 126 DFAInput InsnInput = getInsnInput(InsnClass); 127 UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput); 128 ReadTable(CurrentState); 129 assert(CachedTable.count(StateTrans) != 0); 130 CurrentState = CachedTable[StateTrans]; 131 } 132 133 134 // canReserveResources - Check if the resources occupied by a machine 135 // instruction are available in the current state. 136 bool DFAPacketizer::canReserveResources(llvm::MachineInstr *MI) { 137 const llvm::MCInstrDesc &MID = MI->getDesc(); 138 return canReserveResources(&MID); 139 } 140 141 // reserveResources - Reserve the resources occupied by a machine 142 // instruction and change the current state to reflect that change. 143 void DFAPacketizer::reserveResources(llvm::MachineInstr *MI) { 144 const llvm::MCInstrDesc &MID = MI->getDesc(); 145 reserveResources(&MID); 146 } 147 148 namespace llvm { 149 // DefaultVLIWScheduler - This class extends ScheduleDAGInstrs and overrides 150 // Schedule method to build the dependence graph. 151 class DefaultVLIWScheduler : public ScheduleDAGInstrs { 152 public: 153 DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI); 154 // Schedule - Actual scheduling work. 155 void schedule() override; 156 }; 157 } 158 159 DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF, 160 MachineLoopInfo &MLI) 161 : ScheduleDAGInstrs(MF, &MLI) { 162 CanHandleTerminators = true; 163 } 164 165 void DefaultVLIWScheduler::schedule() { 166 // Build the scheduling graph. 167 buildSchedGraph(nullptr); 168 } 169 170 // VLIWPacketizerList Ctor 171 VLIWPacketizerList::VLIWPacketizerList(MachineFunction &MF, 172 MachineLoopInfo &MLI) 173 : MF(MF) { 174 TII = MF.getSubtarget().getInstrInfo(); 175 ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget()); 176 VLIWScheduler = new DefaultVLIWScheduler(MF, MLI); 177 } 178 179 // VLIWPacketizerList Dtor 180 VLIWPacketizerList::~VLIWPacketizerList() { 181 if (VLIWScheduler) 182 delete VLIWScheduler; 183 184 if (ResourceTracker) 185 delete ResourceTracker; 186 } 187 188 // endPacket - End the current packet, bundle packet instructions and reset 189 // DFA state. 190 void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB, 191 MachineInstr *MI) { 192 if (CurrentPacketMIs.size() > 1) { 193 MachineInstr *MIFirst = CurrentPacketMIs.front(); 194 finalizeBundle(*MBB, MIFirst->getIterator(), MI->getIterator()); 195 } 196 CurrentPacketMIs.clear(); 197 ResourceTracker->clearResources(); 198 } 199 200 // PacketizeMIs - Bundle machine instructions into packets. 201 void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB, 202 MachineBasicBlock::iterator BeginItr, 203 MachineBasicBlock::iterator EndItr) { 204 assert(VLIWScheduler && "VLIW Scheduler is not initialized!"); 205 VLIWScheduler->startBlock(MBB); 206 VLIWScheduler->enterRegion(MBB, BeginItr, EndItr, 207 std::distance(BeginItr, EndItr)); 208 VLIWScheduler->schedule(); 209 210 // Generate MI -> SU map. 211 MIToSUnit.clear(); 212 for (unsigned i = 0, e = VLIWScheduler->SUnits.size(); i != e; ++i) { 213 SUnit *SU = &VLIWScheduler->SUnits[i]; 214 MIToSUnit[SU->getInstr()] = SU; 215 } 216 217 // The main packetizer loop. 218 for (; BeginItr != EndItr; ++BeginItr) { 219 MachineInstr *MI = BeginItr; 220 221 this->initPacketizerState(); 222 223 // End the current packet if needed. 224 if (this->isSoloInstruction(MI)) { 225 endPacket(MBB, MI); 226 continue; 227 } 228 229 // Ignore pseudo instructions. 230 if (this->ignorePseudoInstruction(MI, MBB)) 231 continue; 232 233 SUnit *SUI = MIToSUnit[MI]; 234 assert(SUI && "Missing SUnit Info!"); 235 236 // Ask DFA if machine resource is available for MI. 237 bool ResourceAvail = ResourceTracker->canReserveResources(MI); 238 if (ResourceAvail) { 239 // Dependency check for MI with instructions in CurrentPacketMIs. 240 for (std::vector<MachineInstr*>::iterator VI = CurrentPacketMIs.begin(), 241 VE = CurrentPacketMIs.end(); VI != VE; ++VI) { 242 MachineInstr *MJ = *VI; 243 SUnit *SUJ = MIToSUnit[MJ]; 244 assert(SUJ && "Missing SUnit Info!"); 245 246 // Is it legal to packetize SUI and SUJ together. 247 if (!this->isLegalToPacketizeTogether(SUI, SUJ)) { 248 // Allow packetization if dependency can be pruned. 249 if (!this->isLegalToPruneDependencies(SUI, SUJ)) { 250 // End the packet if dependency cannot be pruned. 251 endPacket(MBB, MI); 252 break; 253 } // !isLegalToPruneDependencies. 254 } // !isLegalToPacketizeTogether. 255 } // For all instructions in CurrentPacketMIs. 256 } else { 257 // End the packet if resource is not available. 258 endPacket(MBB, MI); 259 } 260 261 // Add MI to the current packet. 262 BeginItr = this->addToPacket(MI); 263 } // For all instructions in BB. 264 265 // End any packet left behind. 266 endPacket(MBB, EndItr); 267 VLIWScheduler->exitRegion(); 268 VLIWScheduler->finishBlock(); 269 } 270