blob: 86c9c4557b2db371308c0d1da3cc0994e032674d [file] [log] [blame]
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +00001//===- SchedGraph.cpp - Scheduling Graph Implementation -------------------===//
2//
3// Scheduling graph based on SSA graph plus extra dependence edges capturing
4// dependences due to machine resources (machine registers, CC registers, and
5// any others).
6//
7//===----------------------------------------------------------------------===//
Vikram S. Adve78ef1392001-08-28 23:06:02 +00008
Chris Lattner46cbff62001-09-14 16:56:32 +00009#include "SchedGraph.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000010#include "llvm/Function.h"
Chris Lattnerb00c5822001-10-02 03:41:24 +000011#include "llvm/iOther.h"
Tanya Lattnerc50ee552003-08-27 02:42:58 +000012#include "llvm/CodeGen/MachineCodeForInstruction.h"
13#include "llvm/CodeGen/MachineFunction.h"
14#include "llvm/Target/TargetInstrInfo.h"
15#include "llvm/Target/TargetMachine.h"
16#include "llvm/Target/TargetRegInfo.h"
17#include "Support/STLExtras.h"
Vikram S. Adve78ef1392001-08-28 23:06:02 +000018
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000019//*********************** Internal Data Structures *************************/
20
Vikram S. Advec352d2c2001-11-05 04:04:23 +000021// The following two types need to be classes, not typedefs, so we can use
22// opaque declarations in SchedGraph.h
23//
Misha Brukmanc2312df2003-05-22 21:24:35 +000024struct RefVec: public std::vector<std::pair<SchedGraphNode*, int> > {
25 typedef std::vector<std::pair<SchedGraphNode*,int> >::iterator iterator;
26 typedef
27 std::vector<std::pair<SchedGraphNode*,int> >::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000028};
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000029
Chris Lattner80c685f2001-10-13 06:51:01 +000030struct RegToRefVecMap: public hash_map<int, RefVec> {
Vikram S. Advec352d2c2001-11-05 04:04:23 +000031 typedef hash_map<int, RefVec>:: iterator iterator;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000032 typedef hash_map<int, RefVec>::const_iterator const_iterator;
33};
34
Vikram S. Adve74d15d32003-07-02 01:16:01 +000035struct ValueToDefVecMap: public hash_map<const Value*, RefVec> {
36 typedef hash_map<const Value*, RefVec>:: iterator iterator;
37 typedef hash_map<const Value*, RefVec>::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000038};
39
Vikram S. Adve78ef1392001-08-28 23:06:02 +000040
41//
42// class SchedGraphNode
43//
44
Tanya Lattnerc50ee552003-08-27 02:42:58 +000045SchedGraphNode::SchedGraphNode(unsigned NID, MachineBasicBlock *mbb,
46 int indexInBB, const TargetMachine& Target)
47 : SchedGraphNodeCommon(NID), origIndexInBB(indexInBB), MBB(mbb),
48 MI(mbb ? (*mbb)[indexInBB] : 0) {
49 if (MI) {
50 MachineOpCode mopCode = MI->getOpCode();
51 latency = Target.getInstrInfo().hasResultInterlock(mopCode)
52 ? Target.getInstrInfo().minLatency(mopCode)
53 : Target.getInstrInfo().maxLatency(mopCode);
54 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000055}
56
Tanya Lattnerc50ee552003-08-27 02:42:58 +000057SchedGraphNode::~SchedGraphNode() {
Chris Lattnerf3dd05c2002-04-09 05:15:33 +000058 // for each node, delete its out-edges
59 std::for_each(beginOutEdges(), endOutEdges(),
60 deleter<SchedGraphEdge>);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000061}
62
Vikram S. Adve78ef1392001-08-28 23:06:02 +000063//
64// class SchedGraph
65//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000066SchedGraph::SchedGraph(MachineBasicBlock &mbb, const TargetMachine& target)
67 : MBB(mbb) {
Chris Lattner697954c2002-01-20 22:54:45 +000068 buildGraph(target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000069}
70
Tanya Lattnerc50ee552003-08-27 02:42:58 +000071SchedGraph::~SchedGraph() {
Chris Lattner697954c2002-01-20 22:54:45 +000072 for (const_iterator I = begin(); I != end(); ++I)
Chris Lattnerf3dd05c2002-04-09 05:15:33 +000073 delete I->second;
74 delete graphRoot;
75 delete graphLeaf;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000076}
77
Tanya Lattnerc50ee552003-08-27 02:42:58 +000078void SchedGraph::dump() const {
Misha Brukmanc2312df2003-05-22 21:24:35 +000079 std::cerr << " Sched Graph for Basic Block: ";
80 std::cerr << MBB.getBasicBlock()->getName()
81 << " (" << MBB.getBasicBlock() << ")";
Vikram S. Adve78ef1392001-08-28 23:06:02 +000082
Misha Brukmanc2312df2003-05-22 21:24:35 +000083 std::cerr << "\n\n Actual Root nodes : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +000084 for (unsigned i=0, N=graphRoot->outEdges.size(); i < N; i++)
Misha Brukmanc2312df2003-05-22 21:24:35 +000085 std::cerr << graphRoot->outEdges[i]->getSink()->getNodeId()
86 << ((i == N-1)? "" : ", ");
Vikram S. Adve78ef1392001-08-28 23:06:02 +000087
Misha Brukmanc2312df2003-05-22 21:24:35 +000088 std::cerr << "\n Graph Nodes:\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +000089 for (const_iterator I=begin(); I != end(); ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +000090 std::cerr << "\n" << *I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000091
Misha Brukmanc2312df2003-05-22 21:24:35 +000092 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +000093}
94
95
Vikram S. Adve8b6d2452001-09-18 12:50:40 +000096
Tanya Lattnerc50ee552003-08-27 02:42:58 +000097void SchedGraph::addDummyEdges() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000098 assert(graphRoot->outEdges.size() == 0);
99
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000100 for (const_iterator I=begin(); I != end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000101 SchedGraphNode* node = (*I).second;
102 assert(node != graphRoot && node != graphLeaf);
103 if (node->beginInEdges() == node->endInEdges())
104 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
105 SchedGraphEdge::NonDataDep, 0);
106 if (node->beginOutEdges() == node->endOutEdges())
107 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
108 SchedGraphEdge::NonDataDep, 0);
109 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000110}
111
112
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000113void SchedGraph::addCDEdges(const TerminatorInst* term,
114 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000115 const TargetInstrInfo& mii = target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000116 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000117
118 // Find the first branch instr in the sequence of machine instrs for term
119 //
120 unsigned first = 0;
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000121 while (! mii.isBranch(termMvec[first]->getOpCode()) &&
122 ! mii.isReturn(termMvec[first]->getOpCode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000123 ++first;
124 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000125 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000126 if (first == termMvec.size())
127 return;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000128
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000129 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000130
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000131 // Add CD edges from each instruction in the sequence to the
132 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000133 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000134 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000135 for (unsigned i = termMvec.size(); i > first+1; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000136 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
137 assert(toNode && "No node for instr generated for branch/ret?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000138
Misha Brukman6b77ec42003-05-22 21:49:18 +0000139 for (unsigned j = i-1; j != 0; --j)
140 if (mii.isBranch(termMvec[j-1]->getOpCode()) ||
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000141 mii.isReturn(termMvec[j-1]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000142 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
143 assert(brNode && "No node for instr generated for branch/ret?");
144 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
145 SchedGraphEdge::NonDataDep, 0);
146 break; // only one incoming edge is enough
147 }
148 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000149
150 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000151 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000152 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000153 for (unsigned i = first; i != 0; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000154 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
155 assert(fromNode && "No node for instr generated for branch?");
156 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
157 SchedGraphEdge::NonDataDep, 0);
158 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000159
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000160 // Now add CD edges to the first branch instruction in the sequence from
161 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000162 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000163 for (unsigned i=0, N=MBB.size(); i < N; i++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000164 if (MBB[i] == termMvec[first]) // reached the first branch
165 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000166
Misha Brukman6b77ec42003-05-22 21:49:18 +0000167 SchedGraphNode* fromNode = this->getGraphNodeForInstr(MBB[i]);
168 if (fromNode == NULL)
169 continue; // dummy instruction, e.g., PHI
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000170
Misha Brukman6b77ec42003-05-22 21:49:18 +0000171 (void) new SchedGraphEdge(fromNode, firstBrNode,
172 SchedGraphEdge::CtrlDep,
173 SchedGraphEdge::NonDataDep, 0);
174
175 // If we find any other machine instructions (other than due to
176 // the terminator) that also have delay slots, add an outgoing edge
177 // from the instruction to the instructions in the delay slots.
178 //
179 unsigned d = mii.getNumDelaySlots(MBB[i]->getOpCode());
180 assert(i+d < N && "Insufficient delay slots for instruction?");
181
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000182 for (unsigned j=1; j <= d; j++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000183 SchedGraphNode* toNode = this->getGraphNodeForInstr(MBB[i+j]);
184 assert(toNode && "No node for machine instr in delay slot?");
185 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000186 SchedGraphEdge::CtrlDep,
187 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000188 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000189 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000190}
191
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000192static const int SG_LOAD_REF = 0;
193static const int SG_STORE_REF = 1;
194static const int SG_CALL_REF = 2;
195
196static const unsigned int SG_DepOrderArray[][3] = {
197 { SchedGraphEdge::NonDataDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000198 SchedGraphEdge::AntiDep,
199 SchedGraphEdge::AntiDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000200 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000201 SchedGraphEdge::OutputDep,
202 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000203 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000204 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
205 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
206 | SchedGraphEdge::OutputDep }
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000207};
208
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000209
Vikram S. Advee64574c2001-11-08 05:20:23 +0000210// Add a dependence edge between every pair of machine load/store/call
211// instructions, where at least one is a store or a call.
212// Use latency 1 just to ensure that memory operations are ordered;
213// latency does not otherwise matter (true dependences enforce that).
214//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000215void SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
216 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000217 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000218
Vikram S. Advee64574c2001-11-08 05:20:23 +0000219 // Instructions in memNodeVec are in execution order within the basic block,
220 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
221 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000222 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000223 MachineOpCode fromOpCode = memNodeVec[im]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000224 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
225 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
226 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000227 for (unsigned jm=im+1; jm < NM; jm++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000228 MachineOpCode toOpCode = memNodeVec[jm]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000229 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
230 : (mii.isLoad(toOpCode)? SG_LOAD_REF
231 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000232
Misha Brukman6b77ec42003-05-22 21:49:18 +0000233 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
234 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
235 SchedGraphEdge::MemoryDep,
236 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000237 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000238 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000239}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000240
Vikram S. Advee64574c2001-11-08 05:20:23 +0000241// Add edges from/to CC reg instrs to/from call instrs.
242// Essentially this prevents anything that sets or uses a CC reg from being
243// reordered w.r.t. a call.
244// Use a latency of 0 because we only need to prevent out-of-order issue,
245// like with control dependences.
246//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000247void SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
248 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000249 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000250
Vikram S. Adve7952d602003-05-31 07:37:05 +0000251 // Instructions in memNodeVec are in execution order within the basic block,
252 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
253 //
254 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000255 if (mii.isCall(callDepNodeVec[ic]->getOpCode())) {
256 // Add SG_CALL_REF edges from all preds to this instruction.
257 for (unsigned jc=0; jc < ic; jc++)
258 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
259 SchedGraphEdge::MachineRegister,
260 MachineIntRegsRID, 0);
261
262 // And do the same from this instruction to all successors.
263 for (unsigned jc=ic+1; jc < NC; jc++)
264 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
265 SchedGraphEdge::MachineRegister,
266 MachineIntRegsRID, 0);
267 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000268
269#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000270 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000271 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000272 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
273 if (mii.isCall(memNodeVec[im]->getOpCode()))
274 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000275
Vikram S. Advee64574c2001-11-08 05:20:23 +0000276 // Now walk the entire basic block, looking for CC instructions *and*
277 // call instructions, and keep track of the order of the instructions.
278 // Use the call node vec to quickly find earlier and later call nodes
279 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000280 //
281 int lastCallNodeIdx = -1;
282 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000283 if (mii.isCall(bbMvec[i]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000284 ++lastCallNodeIdx;
285 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
286 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
287 break;
288 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000289 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000290 else if (mii.isCCInstr(bbMvec[i]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000291 // Add incoming/outgoing edges from/to preceding/later calls
292 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
293 int j=0;
294 for ( ; j <= lastCallNodeIdx; j++)
295 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
296 MachineCCRegsRID, 0);
297 for ( ; j < (int) callNodeVec.size(); j++)
298 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
299 MachineCCRegsRID, 0);
300 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000301#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000302}
303
304
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000305void SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
306 const TargetMachine& target) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000307 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000308 // not aliased!
309 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000310 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000311 I != regToRefVecMap.end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000312 int regNum = (*I).first;
313 RefVec& regRefVec = (*I).second;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000314
Misha Brukman6b77ec42003-05-22 21:49:18 +0000315 // regRefVec is ordered by control flow order in the basic block
316 for (unsigned i=0; i < regRefVec.size(); ++i) {
317 SchedGraphNode* node = regRefVec[i].first;
318 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000319 const MachineOperand& mop =
320 node->getMachineInstr()->getExplOrImplOperand(opNum);
321 bool isDef = mop.opIsDefOnly();
322 bool isDefAndUse = mop.opIsDefAndUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000323
Misha Brukman6b77ec42003-05-22 21:49:18 +0000324 for (unsigned p=0; p < i; ++p) {
325 SchedGraphNode* prevNode = regRefVec[p].first;
326 if (prevNode != node) {
327 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000328 const MachineOperand& prevMop =
329 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
330 bool prevIsDef = prevMop.opIsDefOnly();
331 bool prevIsDefAndUse = prevMop.opIsDefAndUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000332 if (isDef) {
333 if (prevIsDef)
334 new SchedGraphEdge(prevNode, node, regNum,
335 SchedGraphEdge::OutputDep);
336 if (!prevIsDef || prevIsDefAndUse)
337 new SchedGraphEdge(prevNode, node, regNum,
338 SchedGraphEdge::AntiDep);
339 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000340
Misha Brukman6b77ec42003-05-22 21:49:18 +0000341 if (prevIsDef)
342 if (!isDef || isDefAndUse)
343 new SchedGraphEdge(prevNode, node, regNum,
344 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000345 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000346 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000347 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000348 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000349}
350
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000351
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000352// Adds dependences to/from refNode from/to all other defs
353// in the basic block. refNode may be a use, a def, or both.
354// We do not consider other uses because we are not building use-use deps.
355//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000356void SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
357 const RefVec& defVec,
358 const Value* defValue,
359 bool refNodeIsDef,
360 bool refNodeIsDefAndUse,
361 const TargetMachine& target) {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000362 bool refNodeIsUse = !refNodeIsDef || refNodeIsDefAndUse;
363
Vikram S. Adve200a4352001-11-12 18:53:43 +0000364 // Add true or output dep edges from all def nodes before refNode in BB.
365 // Add anti or output dep edges to all def nodes after refNode.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000366 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000367 if ((*I).first == refNode)
368 continue; // Dont add any self-loops
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000369
Misha Brukman6b77ec42003-05-22 21:49:18 +0000370 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB()) {
371 // (*).first is before refNode
372 if (refNodeIsDef)
373 (void) new SchedGraphEdge((*I).first, refNode, defValue,
374 SchedGraphEdge::OutputDep);
375 if (refNodeIsUse)
376 (void) new SchedGraphEdge((*I).first, refNode, defValue,
377 SchedGraphEdge::TrueDep);
378 } else {
379 // (*).first is after refNode
380 if (refNodeIsDef)
381 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
382 SchedGraphEdge::OutputDep);
383 if (refNodeIsUse)
384 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
385 SchedGraphEdge::AntiDep);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000386 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000387 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000388}
389
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000390
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000391void SchedGraph::addEdgesForInstruction(const MachineInstr& MI,
392 const ValueToDefVecMap& valueToDefVecMap,
393 const TargetMachine& target) {
Chris Lattner133f0792002-10-28 04:45:29 +0000394 SchedGraphNode* node = getGraphNodeForInstr(&MI);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000395 if (node == NULL)
396 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000397
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000398 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000399 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000400 for (unsigned i = 0, numOps = MI.getNumOperands(); i != numOps; ++i) {
401 switch (MI.getOperand(i).getType()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000402 case MachineOperand::MO_VirtualRegister:
403 case MachineOperand::MO_CCRegister:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000404 if (const Value* srcI = MI.getOperand(i).getVRegValue()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000405 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
406 if (I != valueToDefVecMap.end())
407 addEdgesForValue(node, I->second, srcI,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000408 MI.getOperand(i).opIsDefOnly(),
409 MI.getOperand(i).opIsDefAndUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000410 }
411 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000412
Misha Brukman6b77ec42003-05-22 21:49:18 +0000413 case MachineOperand::MO_MachineRegister:
414 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000415
Misha Brukman6b77ec42003-05-22 21:49:18 +0000416 case MachineOperand::MO_SignExtendedImmed:
417 case MachineOperand::MO_UnextendedImmed:
418 case MachineOperand::MO_PCRelativeDisp:
419 break; // nothing to do for immediate fields
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000420
Misha Brukman6b77ec42003-05-22 21:49:18 +0000421 default:
422 assert(0 && "Unknown machine operand type in SchedGraph builder");
423 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000424 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000425 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000426
427 // Add edges for values implicitly used by the machine instruction.
428 // Examples include function arguments to a Call instructions or the return
429 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000430 //
Chris Lattner133f0792002-10-28 04:45:29 +0000431 for (unsigned i=0, N=MI.getNumImplicitRefs(); i < N; ++i)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000432 if (MI.getImplicitOp(i).opIsUse() || MI.getImplicitOp(i).opIsDefAndUse())
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000433 if (const Value* srcI = MI.getImplicitRef(i)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000434 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
435 if (I != valueToDefVecMap.end())
436 addEdgesForValue(node, I->second, srcI,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000437 MI.getImplicitOp(i).opIsDefOnly(),
438 MI.getImplicitOp(i).opIsDefAndUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000439 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000440}
441
442
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000443void SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
444 SchedGraphNode* node,
445 std::vector<SchedGraphNode*>& memNodeVec,
446 std::vector<SchedGraphNode*>& callDepNodeVec,
447 RegToRefVecMap& regToRefVecMap,
448 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000449 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000450
Vikram S. Advee64574c2001-11-08 05:20:23 +0000451 MachineOpCode opCode = node->getOpCode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000452
Vikram S. Adve7952d602003-05-31 07:37:05 +0000453 if (mii.isCall(opCode) || mii.isCCInstr(opCode))
454 callDepNodeVec.push_back(node);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000455
Vikram S. Advee64574c2001-11-08 05:20:23 +0000456 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
457 memNodeVec.push_back(node);
458
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000459 // Collect the register references and value defs. for explicit operands
460 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000461 const MachineInstr& MI = *node->getMachineInstr();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000462 for (int i=0, numOps = (int) MI.getNumOperands(); i < numOps; i++) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000463 const MachineOperand& mop = MI.getOperand(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000464
Misha Brukman6b77ec42003-05-22 21:49:18 +0000465 // if this references a register other than the hardwired
466 // "zero" register, record the reference.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000467 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000468 int regNum = mop.getAllocatedRegNum();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000469
Vikram S. Adve7952d602003-05-31 07:37:05 +0000470 // If this is not a dummy zero register, record the reference in order
Misha Brukman6b77ec42003-05-22 21:49:18 +0000471 if (regNum != target.getRegInfo().getZeroRegNum())
Vikram S. Adve7952d602003-05-31 07:37:05 +0000472 regToRefVecMap[mop.getAllocatedRegNum()]
Misha Brukman6b77ec42003-05-22 21:49:18 +0000473 .push_back(std::make_pair(node, i));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000474
475 // If this is a volatile register, add the instruction to callDepVec
476 // (only if the node is not already on the callDepVec!)
477 if (callDepNodeVec.size() == 0 || callDepNodeVec.back() != node)
478 {
479 unsigned rcid;
480 int regInClass = target.getRegInfo().getClassRegNum(regNum, rcid);
481 if (target.getRegInfo().getMachineRegClass(rcid)
482 ->isRegVolatile(regInClass))
483 callDepNodeVec.push_back(node);
484 }
485
Misha Brukman6b77ec42003-05-22 21:49:18 +0000486 continue; // nothing more to do
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000487 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000488
Misha Brukman6b77ec42003-05-22 21:49:18 +0000489 // ignore all other non-def operands
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000490 if (!MI.getOperand(i).opIsDefOnly() &&
491 !MI.getOperand(i).opIsDefAndUse())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000492 continue;
493
494 // We must be defining a value.
495 assert((mop.getType() == MachineOperand::MO_VirtualRegister ||
496 mop.getType() == MachineOperand::MO_CCRegister)
497 && "Do not expect any other kind of operand to be defined!");
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000498 assert(mop.getVRegValue() != NULL && "Null value being defined?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000499
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000500 valueToDefVecMap[mop.getVRegValue()].push_back(std::make_pair(node, i));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000501 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000502
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000503 //
Vikram S. Adve7952d602003-05-31 07:37:05 +0000504 // Collect value defs. for implicit operands. They may have allocated
505 // physical registers also.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000506 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000507 for (unsigned i=0, N = MI.getNumImplicitRefs(); i != N; ++i) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000508 const MachineOperand& mop = MI.getImplicitOp(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000509 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000510 int regNum = mop.getAllocatedRegNum();
511 if (regNum != target.getRegInfo().getZeroRegNum())
512 regToRefVecMap[mop.getAllocatedRegNum()]
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000513 .push_back(std::make_pair(node, i + MI.getNumOperands()));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000514 continue; // nothing more to do
515 }
516
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000517 if (mop.opIsDefOnly() || mop.opIsDefAndUse()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000518 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
519 valueToDefVecMap[MI.getImplicitRef(i)].push_back(std::make_pair(node,
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000520 -i));
521 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000522 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000523}
524
525
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000526void SchedGraph::buildNodesForBB(const TargetMachine& target,
527 MachineBasicBlock& MBB,
528 std::vector<SchedGraphNode*>& memNodeVec,
529 std::vector<SchedGraphNode*>& callDepNodeVec,
530 RegToRefVecMap& regToRefVecMap,
531 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000532 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000533
534 // Build graph nodes for each VM instruction and gather def/use info.
535 // Do both those together in a single pass over all machine instructions.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000536 for (unsigned i=0; i < MBB.size(); i++)
537 if (!mii.isDummyPhiInstr(MBB[i]->getOpCode())) {
538 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
539 noteGraphNodeForInstr(MBB[i], node);
540
541 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000542 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
543 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000544 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000545}
546
547
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000548void SchedGraph::buildGraph(const TargetMachine& target) {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000549 // Use this data structure to note all machine operands that compute
550 // ordinary LLVM values. These must be computed defs (i.e., instructions).
551 // Note that there may be multiple machine instructions that define
552 // each Value.
553 ValueToDefVecMap valueToDefVecMap;
554
Vikram S. Advee64574c2001-11-08 05:20:23 +0000555 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000556 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000557 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000558
559 // Use this data structure to note all instructions that access physical
560 // registers that can be modified by a call (including call instructions)
561 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000562
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000563 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000564 // machine registers so we can add edges for those later without
565 // extra passes over the nodes.
566 // The vector holds an ordered list of references to the machine reg,
567 // ordered according to control-flow order. This only works for a
568 // single basic block, hence the assertion. Each reference is identified
569 // by the pair: <node, operand-number>.
570 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000571 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000572
573 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000574 graphRoot = new SchedGraphNode(0, NULL, -1, target);
575 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000576
577 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000578 // First add nodes for all the machine instructions in the basic block
579 // because this greatly simplifies identifying which edges to add.
580 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000581 // Also, remember the load/store instructions to add memory deps later.
582 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000583
Vikram S. Adve7952d602003-05-31 07:37:05 +0000584 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
585 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000586
587 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000588 // Now add edges for the following (all are incoming edges except (4)):
589 // (1) operands of the machine instruction, including hidden operands
590 // (2) machine register dependences
591 // (3) memory load/store dependences
592 // (3) other resource dependences for the machine instruction, if any
593 // (4) output dependences when multiple machine instructions define the
594 // same value; all must have been generated from a single VM instrn
595 // (5) control dependences to branch instructions generated for the
596 // terminator instruction of the BB. Because of delay slots and
597 // 2-way conditional branches, multiple CD edges are needed
598 // (see addCDEdges for details).
599 // Also, note any uses or defs of machine registers.
600 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000601 //----------------------------------------------------------------
602
603 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000604 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000605
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000606 // Then add memory dep edges: store->load, load->store, and store->store.
607 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000608 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000609
610 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000611 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000612
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000613 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000614 for (unsigned i=0, N=MBB.size(); i < N; i++)
615 addEdgesForInstruction(*MBB[i], valueToDefVecMap, target);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000616
Vikram S. Adve200a4352001-11-12 18:53:43 +0000617#ifdef NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000618 // Then add non-SSA edges for all VM instructions in the block.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000619 // We assume that all machine instructions that define a value are
620 // generated from the VM instruction corresponding to that value.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000621 // TODO: This could probably be done much more efficiently.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000622 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000623 this->addNonSSAEdgesForValue(*II, target);
Chris Lattner4ed17ba2001-11-26 18:56:52 +0000624#endif //NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000625
626 // Then add edges for dependences on machine registers
627 this->addMachineRegEdges(regToRefVecMap, target);
628
629 // Finally, add edges from the dummy root and to dummy leaf
630 this->addDummyEdges();
631}
632
633
634//
635// class SchedGraphSet
636//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000637SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000638 const TargetMachine& target) :
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000639 function(_function) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000640 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000641}
642
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000643SchedGraphSet::~SchedGraphSet() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000644 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000645 for(iterator I = begin(), E = end(); I != E; ++I)
646 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000647}
648
649
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000650void SchedGraphSet::dump() const {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000651 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000652 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000653
654 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000655 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000656
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000657 std::cerr << "\n====== End graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000658 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000659}
660
661
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000662void SchedGraphSet::buildGraphsForMethod(const Function *F,
663 const TargetMachine& target) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000664 MachineFunction &MF = MachineFunction::get(F);
665 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
666 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000667}
668
669
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000670void SchedGraphEdge::print(std::ostream &os) const {
671 os << "edge [" << src->getNodeId() << "] -> ["
672 << sink->getNodeId() << "] : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000673
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000674 switch(depType) {
675 case SchedGraphEdge::CtrlDep:
676 os<< "Control Dep";
677 break;
678 case SchedGraphEdge::ValueDep:
679 os<< "Reg Value " << val;
680 break;
681 case SchedGraphEdge::MemoryDep:
682 os<< "Memory Dep";
683 break;
684 case SchedGraphEdge::MachineRegister:
685 os<< "Reg " << machineRegNum;
686 break;
687 case SchedGraphEdge::MachineResource:
688 os<<"Resource "<< resourceId;
689 break;
690 default:
691 assert(0);
692 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000693 }
694
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000695 os << " : delay = " << minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000696}
697
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000698void SchedGraphNode::print(std::ostream &os) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000699 os << std::string(8, ' ')
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000700 << "Node " << ID << " : "
701 << "latency = " << latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000702
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000703 if (getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000704 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000705 else {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000706 os << *getMachineInstr() << "\n" << std::string(12, ' ');
707 os << inEdges.size() << " Incoming Edges:\n";
708 for (unsigned i=0, N = inEdges.size(); i < N; i++)
709 os << std::string(16, ' ') << *inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000710
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000711 os << std::string(12, ' ') << outEdges.size()
Misha Brukman6b77ec42003-05-22 21:49:18 +0000712 << " Outgoing Edges:\n";
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000713 for (unsigned i=0, N= outEdges.size(); i < N; i++)
714 os << std::string(16, ' ') << *outEdges[i];
Misha Brukman6b77ec42003-05-22 21:49:18 +0000715 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000716}