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