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(SelectionDAG *dag, MachineBasicBlock *bb, 00026 const TargetMachine &tm) 00027 : ScheduleDAG(dag, bb, tm) { 00028 } 00029 00030 SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) { 00031 SUnit *SU = NewSUnit(Old->getNode()); 00032 SU->OrigNode = Old->OrigNode; 00033 SU->Latency = Old->Latency; 00034 SU->isTwoAddress = Old->isTwoAddress; 00035 SU->isCommutable = Old->isCommutable; 00036 SU->hasPhysRegDefs = Old->hasPhysRegDefs; 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 SU->addPred(SDep(OpSU, isChain ? SDep::Order : SDep::Data, 00192 OpSU->Latency, PhysReg)); 00193 } 00194 } 00195 } 00196 } 00197 00198 /// BuildSchedGraph - Build the SUnit graph from the selection dag that we 00199 /// are input. This SUnit graph is similar to the SelectionDAG, but 00200 /// excludes nodes that aren't interesting to scheduling, and represents 00201 /// flagged together nodes with a single SUnit. 00202 void ScheduleDAGSDNodes::BuildSchedGraph() { 00203 // Populate the SUnits array. 00204 BuildSchedUnits(); 00205 // Compute all the scheduling dependencies between nodes. 00206 AddSchedEdges(); 00207 } 00208 00209 void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) { 00210 const InstrItineraryData &InstrItins = TM.getInstrItineraryData(); 00211 00212 // Compute the latency for the node. We use the sum of the latencies for 00213 // all nodes flagged together into this SUnit. 00214 SU->Latency = 0; 00215 bool SawMachineOpcode = false; 00216 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) 00217 if (N->isMachineOpcode()) { 00218 SawMachineOpcode = true; 00219 SU->Latency += 00220 InstrItins.getLatency(TII->get(N->getMachineOpcode()).getSchedClass()); 00221 } 00222 } 00223 00224 /// CountResults - The results of target nodes have register or immediate 00225 /// operands first, then an optional chain, and optional flag operands (which do 00226 /// not go into the resulting MachineInstr). 00227 unsigned ScheduleDAGSDNodes::CountResults(SDNode *Node) { 00228 unsigned N = Node->getNumValues(); 00229 while (N && Node->getValueType(N - 1) == MVT::Flag) 00230 --N; 00231 if (N && Node->getValueType(N - 1) == MVT::Other) 00232 --N; // Skip over chain result. 00233 return N; 00234 } 00235 00236 /// CountOperands - The inputs to target nodes have any actual inputs first, 00237 /// followed by special operands that describe memory references, then an 00238 /// optional chain operand, then an optional flag operand. Compute the number 00239 /// of actual operands that will go into the resulting MachineInstr. 00240 unsigned ScheduleDAGSDNodes::CountOperands(SDNode *Node) { 00241 unsigned N = ComputeMemOperandsEnd(Node); 00242 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).getNode())) 00243 --N; // Ignore MEMOPERAND nodes 00244 return N; 00245 } 00246 00247 /// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode 00248 /// operand 00249 unsigned ScheduleDAGSDNodes::ComputeMemOperandsEnd(SDNode *Node) { 00250 unsigned N = Node->getNumOperands(); 00251 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag) 00252 --N; 00253 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other) 00254 --N; // Ignore chain if it exists. 00255 return N; 00256 } 00257 00258 00259 void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const { 00260 if (SU->getNode()) 00261 SU->getNode()->dump(DAG); 00262 else 00263 cerr << "CROSS RC COPY "; 00264 cerr << "\n"; 00265 SmallVector<SDNode *, 4> FlaggedNodes; 00266 for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode()) 00267 FlaggedNodes.push_back(N); 00268 while (!FlaggedNodes.empty()) { 00269 cerr << " "; 00270 FlaggedNodes.back()->dump(DAG); 00271 cerr << "\n"; 00272 FlaggedNodes.pop_back(); 00273 } 00274 }
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.