blob: b058448566191d48e6f1d0b77f67ed7256acc532 [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"
Tanya Lattnerb6489f32003-08-25 22:42:20 +000010#include "llvm/CodeGen/SchedGraphCommon.h"
11#include <iostream>
12
Chris Lattner0861b0c2002-02-03 07:29:45 +000013#include "llvm/Target/TargetMachine.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000014#include "llvm/Target/TargetInstrInfo.h"
Tanya Lattnerb6489f32003-08-25 22:42:20 +000015#include "Support/STLExtras.h"
16#include "llvm/BasicBlock.h"
17#include "llvm/CodeGen/MachineCodeForInstruction.h"
18#include "llvm/Target/TargetRegInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000019#include "llvm/Function.h"
Tanya Lattnerb6489f32003-08-25 22:42:20 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/InstrSelection.h"
Chris Lattnerb00c5822001-10-02 03:41:24 +000022#include "llvm/iOther.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/StringExtras.h"
Tanya Lattnerb6489f32003-08-25 22:42:20 +000024
Vikram S. Adve78ef1392001-08-28 23:06:02 +000025
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000026//*********************** Internal Data Structures *************************/
27
Vikram S. Advec352d2c2001-11-05 04:04:23 +000028// The following two types need to be classes, not typedefs, so we can use
29// opaque declarations in SchedGraph.h
30//
Misha Brukmanc2312df2003-05-22 21:24:35 +000031struct RefVec: public std::vector<std::pair<SchedGraphNode*, int> > {
32 typedef std::vector<std::pair<SchedGraphNode*,int> >::iterator iterator;
33 typedef
34 std::vector<std::pair<SchedGraphNode*,int> >::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000035};
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000036
Chris Lattner80c685f2001-10-13 06:51:01 +000037struct RegToRefVecMap: public hash_map<int, RefVec> {
Vikram S. Advec352d2c2001-11-05 04:04:23 +000038 typedef hash_map<int, RefVec>:: iterator iterator;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000039 typedef hash_map<int, RefVec>::const_iterator const_iterator;
40};
41
Vikram S. Adve74d15d32003-07-02 01:16:01 +000042struct ValueToDefVecMap: public hash_map<const Value*, RefVec> {
43 typedef hash_map<const Value*, RefVec>:: iterator iterator;
44 typedef hash_map<const Value*, RefVec>::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000045};
46
Vikram S. Adve78ef1392001-08-28 23:06:02 +000047
48//
49// class SchedGraphNode
50//
51
52/*ctor*/
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000053SchedGraphNode::SchedGraphNode(unsigned NID,
54 MachineBasicBlock *mbb,
Vikram S. Adve5b43af92001-11-11 01:23:27 +000055 int indexInBB,
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000056 const TargetMachine& Target)
Tanya Lattnerb6489f32003-08-25 22:42:20 +000057 : SchedGraphNodeCommon(NID), origIndexInBB(indexInBB), MBB(mbb), MI(mbb ? (*mbb)[indexInBB] : 0)
58 {
59 if (MI)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000060 {
Tanya Lattnerb6489f32003-08-25 22:42:20 +000061 MachineOpCode mopCode = MI->getOpCode();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000062 latency = Target.getInstrInfo().hasResultInterlock(mopCode)
63 ? Target.getInstrInfo().minLatency(mopCode)
64 : Target.getInstrInfo().maxLatency(mopCode);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000065 }
66}
67
68
69/*dtor*/
70SchedGraphNode::~SchedGraphNode()
71{
Chris Lattnerf3dd05c2002-04-09 05:15:33 +000072 // for each node, delete its out-edges
73 std::for_each(beginOutEdges(), endOutEdges(),
74 deleter<SchedGraphEdge>);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000075}
76
Vikram S. Adve78ef1392001-08-28 23:06:02 +000077//
78// class SchedGraph
79//
80
81
82/*ctor*/
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000083SchedGraph::SchedGraph(MachineBasicBlock &mbb, const TargetMachine& target)
84 : MBB(mbb) {
Chris Lattner697954c2002-01-20 22:54:45 +000085 buildGraph(target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000086}
87
88
89/*dtor*/
90SchedGraph::~SchedGraph()
91{
Chris Lattner697954c2002-01-20 22:54:45 +000092 for (const_iterator I = begin(); I != end(); ++I)
Chris Lattnerf3dd05c2002-04-09 05:15:33 +000093 delete I->second;
94 delete graphRoot;
95 delete graphLeaf;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000096}
97
98
99void
100SchedGraph::dump() const
101{
Misha Brukmanc2312df2003-05-22 21:24:35 +0000102 std::cerr << " Sched Graph for Basic Block: ";
103 std::cerr << MBB.getBasicBlock()->getName()
104 << " (" << MBB.getBasicBlock() << ")";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000105
Misha Brukmanc2312df2003-05-22 21:24:35 +0000106 std::cerr << "\n\n Actual Root nodes : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000107 for (unsigned i=0, N=graphRoot->outEdges.size(); i < N; i++)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000108 std::cerr << graphRoot->outEdges[i]->getSink()->getNodeId()
109 << ((i == N-1)? "" : ", ");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000110
Misha Brukmanc2312df2003-05-22 21:24:35 +0000111 std::cerr << "\n Graph Nodes:\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000112 for (const_iterator I=begin(); I != end(); ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000113 std::cerr << "\n" << *I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000114
Misha Brukmanc2312df2003-05-22 21:24:35 +0000115 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000116}
117
118
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000119
120void
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000121SchedGraph::addDummyEdges()
122{
123 assert(graphRoot->outEdges.size() == 0);
124
125 for (const_iterator I=begin(); I != end(); ++I)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000126 {
127 SchedGraphNode* node = (*I).second;
128 assert(node != graphRoot && node != graphLeaf);
129 if (node->beginInEdges() == node->endInEdges())
130 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
131 SchedGraphEdge::NonDataDep, 0);
132 if (node->beginOutEdges() == node->endOutEdges())
133 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
134 SchedGraphEdge::NonDataDep, 0);
135 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000136}
137
138
139void
140SchedGraph::addCDEdges(const TerminatorInst* term,
141 const TargetMachine& target)
142{
Chris Lattner3501fea2003-01-14 22:00:31 +0000143 const TargetInstrInfo& mii = target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000144 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000145
146 // Find the first branch instr in the sequence of machine instrs for term
147 //
148 unsigned first = 0;
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000149 while (! mii.isBranch(termMvec[first]->getOpCode()) &&
150 ! mii.isReturn(termMvec[first]->getOpCode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000151 ++first;
152 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000153 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000154 if (first == termMvec.size())
155 return;
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000156
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000157 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000158
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000159 // Add CD edges from each instruction in the sequence to the
160 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000161 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000162 //
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000163 for (unsigned i = termMvec.size(); i > first+1; --i)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000164 {
165 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
166 assert(toNode && "No node for instr generated for branch/ret?");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000167
Misha Brukman6b77ec42003-05-22 21:49:18 +0000168 for (unsigned j = i-1; j != 0; --j)
169 if (mii.isBranch(termMvec[j-1]->getOpCode()) ||
170 mii.isReturn(termMvec[j-1]->getOpCode()))
171 {
172 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
173 assert(brNode && "No node for instr generated for branch/ret?");
174 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
175 SchedGraphEdge::NonDataDep, 0);
176 break; // only one incoming edge is enough
177 }
178 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000179
180 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000181 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000182 //
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000183 for (unsigned i = first; i != 0; --i)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000184 {
185 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
186 assert(fromNode && "No node for instr generated for branch?");
187 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
188 SchedGraphEdge::NonDataDep, 0);
189 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000190
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000191 // Now add CD edges to the first branch instruction in the sequence from
192 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000193 //
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000194 for (unsigned i=0, N=MBB.size(); i < N; i++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000195 {
196 if (MBB[i] == termMvec[first]) // reached the first branch
197 break;
198
199 SchedGraphNode* fromNode = this->getGraphNodeForInstr(MBB[i]);
200 if (fromNode == NULL)
201 continue; // dummy instruction, e.g., PHI
202
203 (void) new SchedGraphEdge(fromNode, firstBrNode,
204 SchedGraphEdge::CtrlDep,
205 SchedGraphEdge::NonDataDep, 0);
206
207 // If we find any other machine instructions (other than due to
208 // the terminator) that also have delay slots, add an outgoing edge
209 // from the instruction to the instructions in the delay slots.
210 //
211 unsigned d = mii.getNumDelaySlots(MBB[i]->getOpCode());
212 assert(i+d < N && "Insufficient delay slots for instruction?");
213
214 for (unsigned j=1; j <= d; j++)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000215 {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000216 SchedGraphNode* toNode = this->getGraphNodeForInstr(MBB[i+j]);
217 assert(toNode && "No node for machine instr in delay slot?");
218 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000219 SchedGraphEdge::CtrlDep,
220 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000221 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000222 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000223}
224
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000225static const int SG_LOAD_REF = 0;
226static const int SG_STORE_REF = 1;
227static const int SG_CALL_REF = 2;
228
229static const unsigned int SG_DepOrderArray[][3] = {
230 { SchedGraphEdge::NonDataDep,
231 SchedGraphEdge::AntiDep,
232 SchedGraphEdge::AntiDep },
233 { SchedGraphEdge::TrueDep,
234 SchedGraphEdge::OutputDep,
235 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
236 { SchedGraphEdge::TrueDep,
237 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
238 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
239 | SchedGraphEdge::OutputDep }
240};
241
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000242
Vikram S. Advee64574c2001-11-08 05:20:23 +0000243// Add a dependence edge between every pair of machine load/store/call
244// instructions, where at least one is a store or a call.
245// Use latency 1 just to ensure that memory operations are ordered;
246// latency does not otherwise matter (true dependences enforce that).
247//
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000248void
Misha Brukmanc2312df2003-05-22 21:24:35 +0000249SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000250 const TargetMachine& target)
251{
Chris Lattner3501fea2003-01-14 22:00:31 +0000252 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000253
Vikram S. Advee64574c2001-11-08 05:20:23 +0000254 // Instructions in memNodeVec are in execution order within the basic block,
255 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
256 //
257 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000258 {
259 MachineOpCode fromOpCode = memNodeVec[im]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000260 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
261 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
262 : SG_STORE_REF));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000263 for (unsigned jm=im+1; jm < NM; jm++)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000264 {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000265 MachineOpCode toOpCode = memNodeVec[jm]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000266 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
267 : (mii.isLoad(toOpCode)? SG_LOAD_REF
268 : SG_STORE_REF));
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000269
Misha Brukman6b77ec42003-05-22 21:49:18 +0000270 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
271 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
272 SchedGraphEdge::MemoryDep,
273 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000274 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000275 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000276}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000277
Vikram S. Advee64574c2001-11-08 05:20:23 +0000278// Add edges from/to CC reg instrs to/from call instrs.
279// Essentially this prevents anything that sets or uses a CC reg from being
280// reordered w.r.t. a call.
281// Use a latency of 0 because we only need to prevent out-of-order issue,
282// like with control dependences.
283//
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000284void
Vikram S. Adve7952d602003-05-31 07:37:05 +0000285SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
286 const TargetMachine& target)
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000287{
Chris Lattner3501fea2003-01-14 22:00:31 +0000288 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000289
Vikram S. Adve7952d602003-05-31 07:37:05 +0000290 // Instructions in memNodeVec are in execution order within the basic block,
291 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
292 //
293 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
294 if (mii.isCall(callDepNodeVec[ic]->getOpCode()))
295 {
296 // Add SG_CALL_REF edges from all preds to this instruction.
297 for (unsigned jc=0; jc < ic; jc++)
298 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
299 SchedGraphEdge::MachineRegister,
300 MachineIntRegsRID, 0);
301
302 // And do the same from this instruction to all successors.
303 for (unsigned jc=ic+1; jc < NC; jc++)
304 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
305 SchedGraphEdge::MachineRegister,
306 MachineIntRegsRID, 0);
307 }
308
309#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000310 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000311 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000312 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
313 if (mii.isCall(memNodeVec[im]->getOpCode()))
314 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000315
Vikram S. Advee64574c2001-11-08 05:20:23 +0000316 // Now walk the entire basic block, looking for CC instructions *and*
317 // call instructions, and keep track of the order of the instructions.
318 // Use the call node vec to quickly find earlier and later call nodes
319 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000320 //
321 int lastCallNodeIdx = -1;
322 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
323 if (mii.isCall(bbMvec[i]->getOpCode()))
Misha Brukman6b77ec42003-05-22 21:49:18 +0000324 {
325 ++lastCallNodeIdx;
326 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
327 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
328 break;
329 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000330 }
331 else if (mii.isCCInstr(bbMvec[i]->getOpCode()))
332 {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000333 // Add incoming/outgoing edges from/to preceding/later calls
334 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
335 int j=0;
336 for ( ; j <= lastCallNodeIdx; j++)
337 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
338 MachineCCRegsRID, 0);
339 for ( ; j < (int) callNodeVec.size(); j++)
340 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
341 MachineCCRegsRID, 0);
342 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000343#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000344}
345
346
347void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000348SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000349 const TargetMachine& target)
350{
Vikram S. Adve7952d602003-05-31 07:37:05 +0000351 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000352 // not aliased!
353 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000354 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000355 I != regToRefVecMap.end(); ++I)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000356 {
357 int regNum = (*I).first;
358 RefVec& regRefVec = (*I).second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000359
Misha Brukman6b77ec42003-05-22 21:49:18 +0000360 // regRefVec is ordered by control flow order in the basic block
361 for (unsigned i=0; i < regRefVec.size(); ++i) {
362 SchedGraphNode* node = regRefVec[i].first;
363 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000364 const MachineOperand& mop =
365 node->getMachineInstr()->getExplOrImplOperand(opNum);
366 bool isDef = mop.opIsDefOnly();
367 bool isDefAndUse = mop.opIsDefAndUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000368
Misha Brukman6b77ec42003-05-22 21:49:18 +0000369 for (unsigned p=0; p < i; ++p) {
370 SchedGraphNode* prevNode = regRefVec[p].first;
371 if (prevNode != node) {
372 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000373 const MachineOperand& prevMop =
374 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
375 bool prevIsDef = prevMop.opIsDefOnly();
376 bool prevIsDefAndUse = prevMop.opIsDefAndUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000377 if (isDef) {
378 if (prevIsDef)
379 new SchedGraphEdge(prevNode, node, regNum,
380 SchedGraphEdge::OutputDep);
381 if (!prevIsDef || prevIsDefAndUse)
382 new SchedGraphEdge(prevNode, node, regNum,
383 SchedGraphEdge::AntiDep);
384 }
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000385
Misha Brukman6b77ec42003-05-22 21:49:18 +0000386 if (prevIsDef)
387 if (!isDef || isDefAndUse)
388 new SchedGraphEdge(prevNode, node, regNum,
389 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000390 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000391 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000392 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000393 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000394}
395
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000396
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000397// Adds dependences to/from refNode from/to all other defs
398// in the basic block. refNode may be a use, a def, or both.
399// We do not consider other uses because we are not building use-use deps.
400//
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000401void
Vikram S. Adve200a4352001-11-12 18:53:43 +0000402SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
403 const RefVec& defVec,
404 const Value* defValue,
405 bool refNodeIsDef,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000406 bool refNodeIsDefAndUse,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000407 const TargetMachine& target)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000408{
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000409 bool refNodeIsUse = !refNodeIsDef || refNodeIsDefAndUse;
410
Vikram S. Adve200a4352001-11-12 18:53:43 +0000411 // Add true or output dep edges from all def nodes before refNode in BB.
412 // Add anti or output dep edges to all def nodes after refNode.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000413 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000414 {
415 if ((*I).first == refNode)
416 continue; // Dont add any self-loops
Vikram S. Adve200a4352001-11-12 18:53:43 +0000417
Misha Brukman6b77ec42003-05-22 21:49:18 +0000418 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB()) {
419 // (*).first is before refNode
420 if (refNodeIsDef)
421 (void) new SchedGraphEdge((*I).first, refNode, defValue,
422 SchedGraphEdge::OutputDep);
423 if (refNodeIsUse)
424 (void) new SchedGraphEdge((*I).first, refNode, defValue,
425 SchedGraphEdge::TrueDep);
426 } else {
427 // (*).first is after refNode
428 if (refNodeIsDef)
429 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
430 SchedGraphEdge::OutputDep);
431 if (refNodeIsUse)
432 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
433 SchedGraphEdge::AntiDep);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000434 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000435 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000436}
437
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000438
439void
Chris Lattner133f0792002-10-28 04:45:29 +0000440SchedGraph::addEdgesForInstruction(const MachineInstr& MI,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000441 const ValueToDefVecMap& valueToDefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000442 const TargetMachine& target)
443{
Chris Lattner133f0792002-10-28 04:45:29 +0000444 SchedGraphNode* node = getGraphNodeForInstr(&MI);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000445 if (node == NULL)
446 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000447
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000448 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000449 //
Chris Lattner133f0792002-10-28 04:45:29 +0000450 for (unsigned i = 0, numOps = MI.getNumOperands(); i != numOps; ++i)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000451 {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000452 switch (MI.getOperand(i).getType())
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000453 {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000454 case MachineOperand::MO_VirtualRegister:
455 case MachineOperand::MO_CCRegister:
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000456 if (const Value* srcI = MI.getOperand(i).getVRegValue())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000457 {
458 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
459 if (I != valueToDefVecMap.end())
460 addEdgesForValue(node, I->second, srcI,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000461 MI.getOperand(i).opIsDefOnly(),
462 MI.getOperand(i).opIsDefAndUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000463 }
464 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000465
Misha Brukman6b77ec42003-05-22 21:49:18 +0000466 case MachineOperand::MO_MachineRegister:
467 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000468
Misha Brukman6b77ec42003-05-22 21:49:18 +0000469 case MachineOperand::MO_SignExtendedImmed:
470 case MachineOperand::MO_UnextendedImmed:
471 case MachineOperand::MO_PCRelativeDisp:
472 break; // nothing to do for immediate fields
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000473
Misha Brukman6b77ec42003-05-22 21:49:18 +0000474 default:
475 assert(0 && "Unknown machine operand type in SchedGraph builder");
476 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000477 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000478 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000479
480 // Add edges for values implicitly used by the machine instruction.
481 // Examples include function arguments to a Call instructions or the return
482 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000483 //
Chris Lattner133f0792002-10-28 04:45:29 +0000484 for (unsigned i=0, N=MI.getNumImplicitRefs(); i < N; ++i)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000485 if (MI.getImplicitOp(i).opIsUse() || MI.getImplicitOp(i).opIsDefAndUse())
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000486 if (const Value* srcI = MI.getImplicitRef(i))
Misha Brukman6b77ec42003-05-22 21:49:18 +0000487 {
488 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
489 if (I != valueToDefVecMap.end())
490 addEdgesForValue(node, I->second, srcI,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000491 MI.getImplicitOp(i).opIsDefOnly(),
492 MI.getImplicitOp(i).opIsDefAndUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000493 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000494}
495
496
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000497void
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000498SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
499 SchedGraphNode* node,
Misha Brukmanc2312df2003-05-22 21:24:35 +0000500 std::vector<SchedGraphNode*>& memNodeVec,
Vikram S. Adve7952d602003-05-31 07:37:05 +0000501 std::vector<SchedGraphNode*>& callDepNodeVec,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000502 RegToRefVecMap& regToRefVecMap,
503 ValueToDefVecMap& valueToDefVecMap)
504{
Chris Lattner3501fea2003-01-14 22:00:31 +0000505 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000506
Vikram S. Advee64574c2001-11-08 05:20:23 +0000507 MachineOpCode opCode = node->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000508
509 if (mii.isCall(opCode) || mii.isCCInstr(opCode))
510 callDepNodeVec.push_back(node);
511
Vikram S. Advee64574c2001-11-08 05:20:23 +0000512 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
513 memNodeVec.push_back(node);
514
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000515 // Collect the register references and value defs. for explicit operands
516 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000517 const MachineInstr& MI = *node->getMachineInstr();
518 for (int i=0, numOps = (int) MI.getNumOperands(); i < numOps; i++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000519 {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000520 const MachineOperand& mop = MI.getOperand(i);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000521
522 // if this references a register other than the hardwired
523 // "zero" register, record the reference.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000524 if (mop.hasAllocatedReg())
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000525 {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000526 int regNum = mop.getAllocatedRegNum();
527
528 // If this is not a dummy zero register, record the reference in order
Misha Brukman6b77ec42003-05-22 21:49:18 +0000529 if (regNum != target.getRegInfo().getZeroRegNum())
Vikram S. Adve7952d602003-05-31 07:37:05 +0000530 regToRefVecMap[mop.getAllocatedRegNum()]
Misha Brukman6b77ec42003-05-22 21:49:18 +0000531 .push_back(std::make_pair(node, i));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000532
533 // If this is a volatile register, add the instruction to callDepVec
534 // (only if the node is not already on the callDepVec!)
535 if (callDepNodeVec.size() == 0 || callDepNodeVec.back() != node)
536 {
537 unsigned rcid;
538 int regInClass = target.getRegInfo().getClassRegNum(regNum, rcid);
539 if (target.getRegInfo().getMachineRegClass(rcid)
540 ->isRegVolatile(regInClass))
541 callDepNodeVec.push_back(node);
542 }
543
Misha Brukman6b77ec42003-05-22 21:49:18 +0000544 continue; // nothing more to do
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000545 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000546
Misha Brukman6b77ec42003-05-22 21:49:18 +0000547 // ignore all other non-def operands
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000548 if (!MI.getOperand(i).opIsDefOnly() &&
549 !MI.getOperand(i).opIsDefAndUse())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000550 continue;
551
552 // We must be defining a value.
553 assert((mop.getType() == MachineOperand::MO_VirtualRegister ||
554 mop.getType() == MachineOperand::MO_CCRegister)
555 && "Do not expect any other kind of operand to be defined!");
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000556 assert(mop.getVRegValue() != NULL && "Null value being defined?");
Misha Brukman6b77ec42003-05-22 21:49:18 +0000557
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000558 valueToDefVecMap[mop.getVRegValue()].push_back(std::make_pair(node, i));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000559 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000560
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000561 //
Vikram S. Adve7952d602003-05-31 07:37:05 +0000562 // Collect value defs. for implicit operands. They may have allocated
563 // physical registers also.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000564 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000565 for (unsigned i=0, N = MI.getNumImplicitRefs(); i != N; ++i)
Vikram S. Adve7952d602003-05-31 07:37:05 +0000566 {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000567 const MachineOperand& mop = MI.getImplicitOp(i);
Vikram S. Adve7952d602003-05-31 07:37:05 +0000568 if (mop.hasAllocatedReg())
569 {
570 int regNum = mop.getAllocatedRegNum();
571 if (regNum != target.getRegInfo().getZeroRegNum())
572 regToRefVecMap[mop.getAllocatedRegNum()]
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000573 .push_back(std::make_pair(node, i + MI.getNumOperands()));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000574 continue; // nothing more to do
575 }
576
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000577 if (mop.opIsDefOnly() || mop.opIsDefAndUse()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000578 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
579 valueToDefVecMap[MI.getImplicitRef(i)].push_back(std::make_pair(node,
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000580 -i));
581 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000582 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000583}
584
585
586void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000587SchedGraph::buildNodesForBB(const TargetMachine& target,
588 MachineBasicBlock& MBB,
Misha Brukmanc2312df2003-05-22 21:24:35 +0000589 std::vector<SchedGraphNode*>& memNodeVec,
Vikram S. Adve7952d602003-05-31 07:37:05 +0000590 std::vector<SchedGraphNode*>& callDepNodeVec,
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000591 RegToRefVecMap& regToRefVecMap,
592 ValueToDefVecMap& valueToDefVecMap)
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000593{
Chris Lattner3501fea2003-01-14 22:00:31 +0000594 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000595
596 // Build graph nodes for each VM instruction and gather def/use info.
597 // Do both those together in a single pass over all machine instructions.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000598 for (unsigned i=0; i < MBB.size(); i++)
599 if (!mii.isDummyPhiInstr(MBB[i]->getOpCode())) {
600 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
601 noteGraphNodeForInstr(MBB[i], node);
602
603 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000604 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
605 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000606 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000607}
608
609
610void
611SchedGraph::buildGraph(const TargetMachine& target)
612{
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000613 // Use this data structure to note all machine operands that compute
614 // ordinary LLVM values. These must be computed defs (i.e., instructions).
615 // Note that there may be multiple machine instructions that define
616 // each Value.
617 ValueToDefVecMap valueToDefVecMap;
618
Vikram S. Advee64574c2001-11-08 05:20:23 +0000619 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000620 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000621 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000622
623 // Use this data structure to note all instructions that access physical
624 // registers that can be modified by a call (including call instructions)
625 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000626
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000627 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000628 // machine registers so we can add edges for those later without
629 // extra passes over the nodes.
630 // The vector holds an ordered list of references to the machine reg,
631 // ordered according to control-flow order. This only works for a
632 // single basic block, hence the assertion. Each reference is identified
633 // by the pair: <node, operand-number>.
634 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000635 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000636
637 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000638 graphRoot = new SchedGraphNode(0, NULL, -1, target);
639 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000640
641 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000642 // First add nodes for all the machine instructions in the basic block
643 // because this greatly simplifies identifying which edges to add.
644 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000645 // Also, remember the load/store instructions to add memory deps later.
646 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000647
Vikram S. Adve7952d602003-05-31 07:37:05 +0000648 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
649 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000650
651 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000652 // Now add edges for the following (all are incoming edges except (4)):
653 // (1) operands of the machine instruction, including hidden operands
654 // (2) machine register dependences
655 // (3) memory load/store dependences
656 // (3) other resource dependences for the machine instruction, if any
657 // (4) output dependences when multiple machine instructions define the
658 // same value; all must have been generated from a single VM instrn
659 // (5) control dependences to branch instructions generated for the
660 // terminator instruction of the BB. Because of delay slots and
661 // 2-way conditional branches, multiple CD edges are needed
662 // (see addCDEdges for details).
663 // Also, note any uses or defs of machine registers.
664 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000665 //----------------------------------------------------------------
666
667 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000668 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000669
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000670 // Then add memory dep edges: store->load, load->store, and store->store.
671 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000672 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000673
674 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000675 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000676
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000677 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000678 for (unsigned i=0, N=MBB.size(); i < N; i++)
679 addEdgesForInstruction(*MBB[i], valueToDefVecMap, target);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000680
Vikram S. Adve200a4352001-11-12 18:53:43 +0000681#ifdef NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000682 // Then add non-SSA edges for all VM instructions in the block.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000683 // We assume that all machine instructions that define a value are
684 // generated from the VM instruction corresponding to that value.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000685 // TODO: This could probably be done much more efficiently.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000686 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000687 this->addNonSSAEdgesForValue(*II, target);
Chris Lattner4ed17ba2001-11-26 18:56:52 +0000688#endif //NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000689
690 // Then add edges for dependences on machine registers
691 this->addMachineRegEdges(regToRefVecMap, target);
692
693 // Finally, add edges from the dummy root and to dummy leaf
694 this->addDummyEdges();
695}
696
697
698//
699// class SchedGraphSet
700//
701
702/*ctor*/
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000703SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000704 const TargetMachine& target) :
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000705 function(_function)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000706{
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000707 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000708}
709
710
711/*dtor*/
712SchedGraphSet::~SchedGraphSet()
713{
714 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000715 for(iterator I = begin(), E = end(); I != E; ++I)
716 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000717}
718
719
720void
721SchedGraphSet::dump() const
722{
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000723 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000724 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000725
726 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000727 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000728
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000729 std::cerr << "\n====== End graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000730 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000731}
732
733
734void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000735SchedGraphSet::buildGraphsForMethod(const Function *F,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000736 const TargetMachine& target)
737{
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000738 MachineFunction &MF = MachineFunction::get(F);
739 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
740 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000741}
742
743
Chris Lattner697954c2002-01-20 22:54:45 +0000744std::ostream &operator<<(std::ostream &os, const SchedGraphEdge& edge)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000745{
746 os << "edge [" << edge.src->getNodeId() << "] -> ["
747 << edge.sink->getNodeId() << "] : ";
748
749 switch(edge.depType) {
750 case SchedGraphEdge::CtrlDep: os<< "Control Dep"; break;
Vikram S. Adve200a4352001-11-12 18:53:43 +0000751 case SchedGraphEdge::ValueDep: os<< "Reg Value " << edge.val; break;
752 case SchedGraphEdge::MemoryDep: os<< "Memory Dep"; break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000753 case SchedGraphEdge::MachineRegister: os<< "Reg " <<edge.machineRegNum;break;
754 case SchedGraphEdge::MachineResource: os<<"Resource "<<edge.resourceId;break;
755 default: assert(0); break;
756 }
757
Chris Lattner697954c2002-01-20 22:54:45 +0000758 os << " : delay = " << edge.minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000759
760 return os;
761}
762
Chris Lattner697954c2002-01-20 22:54:45 +0000763std::ostream &operator<<(std::ostream &os, const SchedGraphNode& node)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000764{
Chris Lattner697954c2002-01-20 22:54:45 +0000765 os << std::string(8, ' ')
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000766 << "Node " << node.ID << " : "
Chris Lattner697954c2002-01-20 22:54:45 +0000767 << "latency = " << node.latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000768
769 if (node.getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000770 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000771 else {
772 os << *node.getMachineInstr() << "\n" << std::string(12, ' ');
773 os << node.inEdges.size() << " Incoming Edges:\n";
774 for (unsigned i=0, N=node.inEdges.size(); i < N; i++)
775 os << std::string(16, ' ') << *node.inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000776
Misha Brukman6b77ec42003-05-22 21:49:18 +0000777 os << std::string(12, ' ') << node.outEdges.size()
778 << " Outgoing Edges:\n";
779 for (unsigned i=0, N=node.outEdges.size(); i < N; i++)
780 os << std::string(16, ' ') << *node.outEdges[i];
781 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000782
783 return os;
784}