LLVM API Documentation
00001 //===--- ScheduleDAGSDNodes.cpp - Implement the ScheduleDAGSDNodes class --===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This implements the ScheduleDAG class, which is a base class used by 00011 // scheduling implementation classes. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "pre-RA-sched" 00016 #include "llvm/CodeGen/ScheduleDAGSDNodes.h" 00017 #include "llvm/CodeGen/SelectionDAG.h" 00018 #include "llvm/Target/TargetMachine.h" 00019 #include "llvm/Target/TargetInstrInfo.h" 00020 #include "llvm/Target/TargetRegisterInfo.h" 00021 #include "llvm/Support/Debug.h" 00022 #include "llvm/Support/raw_ostream.h" 00023 using namespace llvm; 00024 00025 ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf) 00026 : ScheduleDAG(mf) { 00027 } 00028 00029 SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) { 00030 SUnit *SU = NewSUnit(Old->getNode()); 00031 SU->OrigNode = Old->OrigNode; 00032 SU->Latency = Old->Latency; 00033 SU->isTwoAddress = Old->isTwoAddress; 00034 SU->isCommutable = Old->isCommutable; 00035 SU->hasPhysRegDefs = Old->hasPhysRegDefs; 00036 Old->isCloned = true; 00037 return SU; 00038 } 00039 00040 /// CheckForPhysRegDependency - Check if the dependency between def and use of 00041 /// a specified operand is a physical register dependency. If so, returns the 00042 /// register and the cost of copying the register. 00043 static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op, 00044 const TargetRegisterInfo *TRI, 00045 const TargetInstrInfo *TII, 00046 unsigned &PhysReg, int &Cost) { 00047 if (Op != 2 || User->getOpcode() != ISD::CopyToReg) 00048 return; 00049 00050 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 00051 if (TargetRegisterInfo::isVirtualRegister(Reg)) 00052 return; 00053 00054 unsigned ResNo = User->getOperand(2).getResNo(); 00055 if (Def->isMachineOpcode()) { 00056 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode()); 00057 if (ResNo >= II.getNumDefs() && 00058 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) { 00059 PhysReg = Reg; 00060 const TargetRegisterClass *RC = 00061 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo)); 00062 Cost = RC->getCopyCost(); 00063 } 00064 } 00065 } 00066 00067 void ScheduleDAGSDNodes::BuildSchedUnits() { 00068 // During scheduling, the NodeId field of SDNode is used to map SDNodes 00069 // to their associated SUnits by holding SUnits table indices. A value 00070 // of -1 means the SDNode does not yet have an associated SUnit. 00071 unsigned NumNodes = 0; 00072 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(), 00073 E = DAG->allnodes_end(); NI != E; ++NI) { 00074 NI->setNodeId(-1); 00075 ++NumNodes; 00076 } 00077 00078 // Reserve entries in the vector for each of the SUnits we are creating. This 00079 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get 00080 // invalidated. 00081 // FIXME: Multiply by 2 because we may clone nodes during scheduling. 00082 // This is a temporary workaround. 00083 SUnits.reserve(NumNodes * 2); 00084 00085 // Check to see if the scheduler cares about latencies. 00086 bool UnitLatencies = ForceUnitLatencies(); 00087 00088 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(), 00089 E = DAG->allnodes_end(); NI != E; ++NI) { 00090 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate. 00091 continue; 00092 00093 // If this node has already been processed, stop now. 00094 if (NI->getNodeId() != -1) continue; 00095 00096 SUnit *NodeSUnit = NewSUnit(NI); 00097 00098 // See if anything is flagged to this node, if so, add them to flagged 00099 // nodes. Nodes can have at most one flag input and one flag output. Flags 00100 // are required the be the last operand and result of a node. 00101 00102 // Scan up to find flagged preds. 00103 SDNode *N = NI; 00104 if (N->getNumOperands() && 00105 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) { 00106 do { 00107 N = N->getOperand(N->getNumOperands()-1).getNode(); 00108 assert(N->getNodeId() == -1 && "Node already inserted!"); 00109 N->setNodeId(NodeSUnit->NodeNum); 00110 } while (N->getNumOperands() && 00111 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag); 00112 } 00113 00114 // Scan down to find any flagged succs. 00115 N = NI; 00116 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) { 00117 SDValue FlagVal(N, N->getNumValues()-1); 00118 00119 // There are either zero or one users of the Flag result. 00120 bool HasFlagUse = false; 00121 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); 00122 UI != E; ++UI) 00123 if (FlagVal.isOperandOf(*UI)) { 00124 HasFlagUse = true; 00125 assert(N->getNodeId() == -1 && "Node already inserted!"); 00126 N->setNodeId(NodeSUnit->NodeNum); 00127 N = *UI; 00128 break; 00129 } 00130 if (!HasFlagUse) break; 00131 } 00132 00133 // If there are flag operands involved, N is now the bottom-most node 00134 // of the sequence of nodes that are flagged together. 00135 // Update the SUnit. 00136 NodeSUnit->setNode(N); 00137 assert(N->getNodeId() == -1 && "Node already inserted!"); 00138 N->setNodeId(NodeSUnit->NodeNum); 00139 00140 // Assign the Latency field of NodeSUnit using target-provided information. 00141 if (UnitLatencies) 00142 NodeSUnit->Latency = 1; 00143 else 00144 ComputeLatency(NodeSUnit); 00145 } 00146 } 00147 00148 void ScheduleDAGSDNodes::AddSchedEdges() { 00149 // Pass 2: add the preds, succs, etc. 00150 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) { 00151 SUnit *SU = &SUnits[su]; 00152 SDNode *MainNode = SU->getNode(); 00153 00154 if (MainNode->isMachineOpcode()) { 00155 unsigned Opc = MainNode->getMachineOpcode(); 00156 const TargetInstrDesc &TID = TII->get(Opc); 00157 for (unsigned i = 0; i != TID.getNumOperands(); ++i) { 00158 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) { 00159 SU->isTwoAddress = true; 00160 break; 00161 } 00162 } 00163 if (TID.isCommutable()) 00164 SU->isCommutable = true; 00165 } 00166 00167 // Find all predecessors and successors of the group. 00168 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) { 00169 if (N->isMachineOpcode() && 00170 TII->get(N->getMachineOpcode()).getImplicitDefs() && 00171 CountResults(N) > TII->get(N->getMachineOpcode()).getNumDefs()) 00172 SU->hasPhysRegDefs = true; 00173 00174 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 00175 SDNode *OpN = N->getOperand(i).getNode(); 00176 if (isPassiveNode(OpN)) continue; // Not scheduled. 00177 SUnit *OpSU = &SUnits[OpN->getNodeId()]; 00178 assert(OpSU && "Node has no SUnit!"); 00179 if (OpSU == SU) continue; // In the same group. 00180 00181 MVT OpVT = N->getOperand(i).getValueType(); 00182 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!"); 00183 bool isChain = OpVT == MVT::Other; 00184 00185 unsigned PhysReg = 0; 00186 int Cost = 1; 00187 // Determine if this is a physical register dependency. 00188 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost); 00189 assert((PhysReg == 0 || !isChain) && 00190 "Chain dependence via physreg data?"); 00191 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler 00192 // emits a copy from the physical register to a virtual register unless 00193 // it requires a cross class copy (cost < 0). That means we are only 00194 // treating "expensive to copy" register dependency as physical register 00195 // dependency. This may change in the future though. 00196 if (Cost >= 0) 00197 PhysReg = 0; 00198 SU->addPred(SDep(OpSU, isChain ? SDep::Order : SDep::Data, 00199 OpSU->Latency, PhysReg)); 00200 } 00201 } 00202 } 00203 } 00204 00205 /// BuildSchedGraph - Build the SUnit graph from the selection dag that we 00206 /// are input. This SUnit graph is similar to the SelectionDAG, but 00207 /// excludes nodes that aren't interesting to scheduling, and represents 00208 /// flagged together nodes with a single SUnit. 00209 void ScheduleDAGSDNodes::BuildSchedGraph() { 00210 // Populate the SUnits array. 00211 BuildSchedUnits(); 00212 // Compute all the scheduling dependencies between nodes. 00213 AddSchedEdges(); 00214 } 00215 00216 void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) { 00217 const InstrItineraryData &InstrItins = TM.getInstrItineraryData(); 00218 00219 // Compute the latency for the node. We use the sum of the latencies for 00220 // all nodes flagged together into this SUnit. 00221 SU->Latency = 0; 00222 bool SawMachineOpcode = false; 00223 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) 00224 if (N->isMachineOpcode()) { 00225 SawMachineOpcode = true; 00226 SU->Latency += 00227 InstrItins.getLatency(TII->get(N->getMachineOpcode()).getSchedClass()); 00228 } 00229 } 00230 00231 /// CountResults - The results of target nodes have register or immediate 00232 /// operands first, then an optional chain, and optional flag operands (which do 00233 /// not go into the resulting MachineInstr). 00234 unsigned ScheduleDAGSDNodes::CountResults(SDNode *Node) { 00235 unsigned N = Node->getNumValues(); 00236 while (N && Node->getValueType(N - 1) == MVT::Flag) 00237 --N; 00238 if (N && Node->getValueType(N - 1) == MVT::Other) 00239 --N; // Skip over chain result. 00240 return N; 00241 } 00242 00243 /// CountOperands - The inputs to target nodes have any actual inputs first, 00244 /// followed by special operands that describe memory references, then an 00245 /// optional chain operand, then an optional flag operand. Compute the number 00246 /// of actual operands that will go into the resulting MachineInstr. 00247 unsigned ScheduleDAGSDNodes::CountOperands(SDNode *Node) { 00248 unsigned N = ComputeMemOperandsEnd(Node); 00249 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).getNode())) 00250 --N; // Ignore MEMOPERAND nodes 00251 return N; 00252 } 00253 00254 /// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode 00255 /// operand 00256 unsigned ScheduleDAGSDNodes::ComputeMemOperandsEnd(SDNode *Node) { 00257 unsigned N = Node->getNumOperands(); 00258 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag) 00259 --N; 00260 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other) 00261 --N; // Ignore chain if it exists. 00262 return N; 00263 } 00264 00265 00266 void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const { 00267 if (!SU->getNode()) { 00268 cerr << "PHYS REG COPY\n"; 00269 return; 00270 } 00271 00272 SU->getNode()->dump(DAG); 00273 cerr << "\n"; 00274 SmallVector<SDNode *, 4> FlaggedNodes; 00275 for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode()) 00276 FlaggedNodes.push_back(N); 00277 while (!FlaggedNodes.empty()) { 00278 cerr << " "; 00279 FlaggedNodes.back()->dump(DAG); 00280 cerr << "\n"; 00281 FlaggedNodes.pop_back(); 00282 } 00283 }