blob: bcdd1cfcfce0315dafea63b94b95bf2223f306f4 [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)
Tanya Lattner8dc99822003-08-28 15:30:40 +000047 : SchedGraphNodeCommon(NID,indexInBB), MBB(mbb), MI(mbb ? (*mbb)[indexInBB] : 0) {
Tanya Lattnerc50ee552003-08-27 02:42:58 +000048 if (MI) {
49 MachineOpCode mopCode = MI->getOpCode();
50 latency = Target.getInstrInfo().hasResultInterlock(mopCode)
51 ? Target.getInstrInfo().minLatency(mopCode)
52 : Target.getInstrInfo().maxLatency(mopCode);
53 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000054}
55
John Criswellc9afb492003-08-28 21:43:17 +000056//
57// Method: SchedGraphNode Destructor
58//
59// Description:
60// Free memory allocated by the SchedGraphNode object.
61//
62// Notes:
63// Do not delete the edges here. The base class will take care of that.
64// Only handle subclass specific stuff here (where currently there is
65// none).
66//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000067SchedGraphNode::~SchedGraphNode() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000068}
69
Vikram S. Adve78ef1392001-08-28 23:06:02 +000070//
71// class SchedGraph
72//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000073SchedGraph::SchedGraph(MachineBasicBlock &mbb, const TargetMachine& target)
74 : MBB(mbb) {
Chris Lattner697954c2002-01-20 22:54:45 +000075 buildGraph(target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000076}
77
John Criswellc9afb492003-08-28 21:43:17 +000078//
79// Method: SchedGraph Destructor
80//
81// Description:
82// This method deletes memory allocated by the SchedGraph object.
83//
84// Notes:
85// Do not delete the graphRoot or graphLeaf here. The base class handles
86// that bit of work.
87//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000088SchedGraph::~SchedGraph() {
Chris Lattner697954c2002-01-20 22:54:45 +000089 for (const_iterator I = begin(); I != end(); ++I)
Chris Lattnerf3dd05c2002-04-09 05:15:33 +000090 delete I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000091}
92
Tanya Lattnerc50ee552003-08-27 02:42:58 +000093void SchedGraph::dump() const {
Misha Brukmanc2312df2003-05-22 21:24:35 +000094 std::cerr << " Sched Graph for Basic Block: ";
95 std::cerr << MBB.getBasicBlock()->getName()
96 << " (" << MBB.getBasicBlock() << ")";
Vikram S. Adve78ef1392001-08-28 23:06:02 +000097
Misha Brukmanc2312df2003-05-22 21:24:35 +000098 std::cerr << "\n\n Actual Root nodes : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +000099 for (unsigned i=0, N=graphRoot->outEdges.size(); i < N; i++)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000100 std::cerr << graphRoot->outEdges[i]->getSink()->getNodeId()
101 << ((i == N-1)? "" : ", ");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000102
Misha Brukmanc2312df2003-05-22 21:24:35 +0000103 std::cerr << "\n Graph Nodes:\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000104 for (const_iterator I=begin(); I != end(); ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000105 std::cerr << "\n" << *I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000106
Misha Brukmanc2312df2003-05-22 21:24:35 +0000107 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000108}
109
110
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000111
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000112void SchedGraph::addDummyEdges() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000113 assert(graphRoot->outEdges.size() == 0);
114
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000115 for (const_iterator I=begin(); I != end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000116 SchedGraphNode* node = (*I).second;
117 assert(node != graphRoot && node != graphLeaf);
118 if (node->beginInEdges() == node->endInEdges())
119 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
120 SchedGraphEdge::NonDataDep, 0);
121 if (node->beginOutEdges() == node->endOutEdges())
122 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
123 SchedGraphEdge::NonDataDep, 0);
124 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000125}
126
127
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000128void SchedGraph::addCDEdges(const TerminatorInst* term,
129 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000130 const TargetInstrInfo& mii = target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000131 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000132
133 // Find the first branch instr in the sequence of machine instrs for term
134 //
135 unsigned first = 0;
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000136 while (! mii.isBranch(termMvec[first]->getOpCode()) &&
137 ! mii.isReturn(termMvec[first]->getOpCode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000138 ++first;
139 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000140 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000141 if (first == termMvec.size())
142 return;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000143
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000144 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000145
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000146 // Add CD edges from each instruction in the sequence to the
147 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000148 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000149 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000150 for (unsigned i = termMvec.size(); i > first+1; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000151 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
152 assert(toNode && "No node for instr generated for branch/ret?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000153
Misha Brukman6b77ec42003-05-22 21:49:18 +0000154 for (unsigned j = i-1; j != 0; --j)
155 if (mii.isBranch(termMvec[j-1]->getOpCode()) ||
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000156 mii.isReturn(termMvec[j-1]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000157 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
158 assert(brNode && "No node for instr generated for branch/ret?");
159 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
160 SchedGraphEdge::NonDataDep, 0);
161 break; // only one incoming edge is enough
162 }
163 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000164
165 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000166 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000167 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000168 for (unsigned i = first; i != 0; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000169 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
170 assert(fromNode && "No node for instr generated for branch?");
171 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
172 SchedGraphEdge::NonDataDep, 0);
173 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000174
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000175 // Now add CD edges to the first branch instruction in the sequence from
176 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000177 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000178 for (unsigned i=0, N=MBB.size(); i < N; i++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000179 if (MBB[i] == termMvec[first]) // reached the first branch
180 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000181
Misha Brukman6b77ec42003-05-22 21:49:18 +0000182 SchedGraphNode* fromNode = this->getGraphNodeForInstr(MBB[i]);
183 if (fromNode == NULL)
184 continue; // dummy instruction, e.g., PHI
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000185
Misha Brukman6b77ec42003-05-22 21:49:18 +0000186 (void) new SchedGraphEdge(fromNode, firstBrNode,
187 SchedGraphEdge::CtrlDep,
188 SchedGraphEdge::NonDataDep, 0);
189
190 // If we find any other machine instructions (other than due to
191 // the terminator) that also have delay slots, add an outgoing edge
192 // from the instruction to the instructions in the delay slots.
193 //
194 unsigned d = mii.getNumDelaySlots(MBB[i]->getOpCode());
195 assert(i+d < N && "Insufficient delay slots for instruction?");
196
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000197 for (unsigned j=1; j <= d; j++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000198 SchedGraphNode* toNode = this->getGraphNodeForInstr(MBB[i+j]);
199 assert(toNode && "No node for machine instr in delay slot?");
200 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000201 SchedGraphEdge::CtrlDep,
202 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000203 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000204 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000205}
206
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000207static const int SG_LOAD_REF = 0;
208static const int SG_STORE_REF = 1;
209static const int SG_CALL_REF = 2;
210
211static const unsigned int SG_DepOrderArray[][3] = {
212 { SchedGraphEdge::NonDataDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000213 SchedGraphEdge::AntiDep,
214 SchedGraphEdge::AntiDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000215 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000216 SchedGraphEdge::OutputDep,
217 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000218 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000219 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
220 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
221 | SchedGraphEdge::OutputDep }
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000222};
223
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000224
Vikram S. Advee64574c2001-11-08 05:20:23 +0000225// Add a dependence edge between every pair of machine load/store/call
226// instructions, where at least one is a store or a call.
227// Use latency 1 just to ensure that memory operations are ordered;
228// latency does not otherwise matter (true dependences enforce that).
229//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000230void SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
231 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000232 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000233
Vikram S. Advee64574c2001-11-08 05:20:23 +0000234 // Instructions in memNodeVec are in execution order within the basic block,
235 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
236 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000237 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000238 MachineOpCode fromOpCode = memNodeVec[im]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000239 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
240 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
241 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000242 for (unsigned jm=im+1; jm < NM; jm++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000243 MachineOpCode toOpCode = memNodeVec[jm]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000244 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
245 : (mii.isLoad(toOpCode)? SG_LOAD_REF
246 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000247
Misha Brukman6b77ec42003-05-22 21:49:18 +0000248 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
249 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
250 SchedGraphEdge::MemoryDep,
251 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000252 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000253 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000254}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000255
Vikram S. Advee64574c2001-11-08 05:20:23 +0000256// Add edges from/to CC reg instrs to/from call instrs.
257// Essentially this prevents anything that sets or uses a CC reg from being
258// reordered w.r.t. a call.
259// Use a latency of 0 because we only need to prevent out-of-order issue,
260// like with control dependences.
261//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000262void SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
263 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000264 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000265
Vikram S. Adve7952d602003-05-31 07:37:05 +0000266 // Instructions in memNodeVec are in execution order within the basic block,
267 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
268 //
269 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000270 if (mii.isCall(callDepNodeVec[ic]->getOpCode())) {
271 // Add SG_CALL_REF edges from all preds to this instruction.
272 for (unsigned jc=0; jc < ic; jc++)
273 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
274 SchedGraphEdge::MachineRegister,
275 MachineIntRegsRID, 0);
276
277 // And do the same from this instruction to all successors.
278 for (unsigned jc=ic+1; jc < NC; jc++)
279 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
280 SchedGraphEdge::MachineRegister,
281 MachineIntRegsRID, 0);
282 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000283
284#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000285 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000286 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000287 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
288 if (mii.isCall(memNodeVec[im]->getOpCode()))
289 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000290
Vikram S. Advee64574c2001-11-08 05:20:23 +0000291 // Now walk the entire basic block, looking for CC instructions *and*
292 // call instructions, and keep track of the order of the instructions.
293 // Use the call node vec to quickly find earlier and later call nodes
294 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000295 //
296 int lastCallNodeIdx = -1;
297 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000298 if (mii.isCall(bbMvec[i]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000299 ++lastCallNodeIdx;
300 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
301 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
302 break;
303 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000304 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000305 else if (mii.isCCInstr(bbMvec[i]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000306 // Add incoming/outgoing edges from/to preceding/later calls
307 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
308 int j=0;
309 for ( ; j <= lastCallNodeIdx; j++)
310 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
311 MachineCCRegsRID, 0);
312 for ( ; j < (int) callNodeVec.size(); j++)
313 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
314 MachineCCRegsRID, 0);
315 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000316#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000317}
318
319
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000320void SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
321 const TargetMachine& target) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000322 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000323 // not aliased!
324 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000325 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000326 I != regToRefVecMap.end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000327 int regNum = (*I).first;
328 RefVec& regRefVec = (*I).second;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000329
Misha Brukman6b77ec42003-05-22 21:49:18 +0000330 // regRefVec is ordered by control flow order in the basic block
331 for (unsigned i=0; i < regRefVec.size(); ++i) {
332 SchedGraphNode* node = regRefVec[i].first;
333 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000334 const MachineOperand& mop =
335 node->getMachineInstr()->getExplOrImplOperand(opNum);
336 bool isDef = mop.opIsDefOnly();
337 bool isDefAndUse = mop.opIsDefAndUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000338
Misha Brukman6b77ec42003-05-22 21:49:18 +0000339 for (unsigned p=0; p < i; ++p) {
340 SchedGraphNode* prevNode = regRefVec[p].first;
341 if (prevNode != node) {
342 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000343 const MachineOperand& prevMop =
344 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
345 bool prevIsDef = prevMop.opIsDefOnly();
346 bool prevIsDefAndUse = prevMop.opIsDefAndUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000347 if (isDef) {
348 if (prevIsDef)
349 new SchedGraphEdge(prevNode, node, regNum,
350 SchedGraphEdge::OutputDep);
351 if (!prevIsDef || prevIsDefAndUse)
352 new SchedGraphEdge(prevNode, node, regNum,
353 SchedGraphEdge::AntiDep);
354 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000355
Misha Brukman6b77ec42003-05-22 21:49:18 +0000356 if (prevIsDef)
357 if (!isDef || isDefAndUse)
358 new SchedGraphEdge(prevNode, node, regNum,
359 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000360 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000361 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000362 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000363 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000364}
365
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000366
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000367// Adds dependences to/from refNode from/to all other defs
368// in the basic block. refNode may be a use, a def, or both.
369// We do not consider other uses because we are not building use-use deps.
370//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000371void SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
372 const RefVec& defVec,
373 const Value* defValue,
374 bool refNodeIsDef,
375 bool refNodeIsDefAndUse,
376 const TargetMachine& target) {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000377 bool refNodeIsUse = !refNodeIsDef || refNodeIsDefAndUse;
378
Vikram S. Adve200a4352001-11-12 18:53:43 +0000379 // Add true or output dep edges from all def nodes before refNode in BB.
380 // Add anti or output dep edges to all def nodes after refNode.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000381 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000382 if ((*I).first == refNode)
383 continue; // Dont add any self-loops
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000384
Misha Brukman6b77ec42003-05-22 21:49:18 +0000385 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB()) {
386 // (*).first is before refNode
387 if (refNodeIsDef)
388 (void) new SchedGraphEdge((*I).first, refNode, defValue,
389 SchedGraphEdge::OutputDep);
390 if (refNodeIsUse)
391 (void) new SchedGraphEdge((*I).first, refNode, defValue,
392 SchedGraphEdge::TrueDep);
393 } else {
394 // (*).first is after refNode
395 if (refNodeIsDef)
396 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
397 SchedGraphEdge::OutputDep);
398 if (refNodeIsUse)
399 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
400 SchedGraphEdge::AntiDep);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000401 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000402 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000403}
404
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000405
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000406void SchedGraph::addEdgesForInstruction(const MachineInstr& MI,
407 const ValueToDefVecMap& valueToDefVecMap,
408 const TargetMachine& target) {
Chris Lattner133f0792002-10-28 04:45:29 +0000409 SchedGraphNode* node = getGraphNodeForInstr(&MI);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000410 if (node == NULL)
411 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000412
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000413 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000414 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000415 for (unsigned i = 0, numOps = MI.getNumOperands(); i != numOps; ++i) {
416 switch (MI.getOperand(i).getType()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000417 case MachineOperand::MO_VirtualRegister:
418 case MachineOperand::MO_CCRegister:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000419 if (const Value* srcI = MI.getOperand(i).getVRegValue()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000420 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
421 if (I != valueToDefVecMap.end())
422 addEdgesForValue(node, I->second, srcI,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000423 MI.getOperand(i).opIsDefOnly(),
424 MI.getOperand(i).opIsDefAndUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000425 }
426 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000427
Misha Brukman6b77ec42003-05-22 21:49:18 +0000428 case MachineOperand::MO_MachineRegister:
429 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000430
Misha Brukman6b77ec42003-05-22 21:49:18 +0000431 case MachineOperand::MO_SignExtendedImmed:
432 case MachineOperand::MO_UnextendedImmed:
433 case MachineOperand::MO_PCRelativeDisp:
434 break; // nothing to do for immediate fields
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000435
Misha Brukman6b77ec42003-05-22 21:49:18 +0000436 default:
437 assert(0 && "Unknown machine operand type in SchedGraph builder");
438 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000439 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000440 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000441
442 // Add edges for values implicitly used by the machine instruction.
443 // Examples include function arguments to a Call instructions or the return
444 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000445 //
Chris Lattner133f0792002-10-28 04:45:29 +0000446 for (unsigned i=0, N=MI.getNumImplicitRefs(); i < N; ++i)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000447 if (MI.getImplicitOp(i).opIsUse() || MI.getImplicitOp(i).opIsDefAndUse())
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000448 if (const Value* srcI = MI.getImplicitRef(i)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000449 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
450 if (I != valueToDefVecMap.end())
451 addEdgesForValue(node, I->second, srcI,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000452 MI.getImplicitOp(i).opIsDefOnly(),
453 MI.getImplicitOp(i).opIsDefAndUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000454 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000455}
456
457
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000458void SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
459 SchedGraphNode* node,
460 std::vector<SchedGraphNode*>& memNodeVec,
461 std::vector<SchedGraphNode*>& callDepNodeVec,
462 RegToRefVecMap& regToRefVecMap,
463 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000464 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000465
Vikram S. Advee64574c2001-11-08 05:20:23 +0000466 MachineOpCode opCode = node->getOpCode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000467
Vikram S. Adve7952d602003-05-31 07:37:05 +0000468 if (mii.isCall(opCode) || mii.isCCInstr(opCode))
469 callDepNodeVec.push_back(node);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000470
Vikram S. Advee64574c2001-11-08 05:20:23 +0000471 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
472 memNodeVec.push_back(node);
473
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000474 // Collect the register references and value defs. for explicit operands
475 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000476 const MachineInstr& MI = *node->getMachineInstr();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000477 for (int i=0, numOps = (int) MI.getNumOperands(); i < numOps; i++) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000478 const MachineOperand& mop = MI.getOperand(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000479
Misha Brukman6b77ec42003-05-22 21:49:18 +0000480 // if this references a register other than the hardwired
481 // "zero" register, record the reference.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000482 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000483 int regNum = mop.getAllocatedRegNum();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000484
Vikram S. Adve7952d602003-05-31 07:37:05 +0000485 // If this is not a dummy zero register, record the reference in order
Misha Brukman6b77ec42003-05-22 21:49:18 +0000486 if (regNum != target.getRegInfo().getZeroRegNum())
Vikram S. Adve7952d602003-05-31 07:37:05 +0000487 regToRefVecMap[mop.getAllocatedRegNum()]
Misha Brukman6b77ec42003-05-22 21:49:18 +0000488 .push_back(std::make_pair(node, i));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000489
490 // If this is a volatile register, add the instruction to callDepVec
491 // (only if the node is not already on the callDepVec!)
492 if (callDepNodeVec.size() == 0 || callDepNodeVec.back() != node)
493 {
494 unsigned rcid;
495 int regInClass = target.getRegInfo().getClassRegNum(regNum, rcid);
496 if (target.getRegInfo().getMachineRegClass(rcid)
497 ->isRegVolatile(regInClass))
498 callDepNodeVec.push_back(node);
499 }
500
Misha Brukman6b77ec42003-05-22 21:49:18 +0000501 continue; // nothing more to do
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000502 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000503
Misha Brukman6b77ec42003-05-22 21:49:18 +0000504 // ignore all other non-def operands
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000505 if (!MI.getOperand(i).opIsDefOnly() &&
506 !MI.getOperand(i).opIsDefAndUse())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000507 continue;
508
509 // We must be defining a value.
510 assert((mop.getType() == MachineOperand::MO_VirtualRegister ||
511 mop.getType() == MachineOperand::MO_CCRegister)
512 && "Do not expect any other kind of operand to be defined!");
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000513 assert(mop.getVRegValue() != NULL && "Null value being defined?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000514
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000515 valueToDefVecMap[mop.getVRegValue()].push_back(std::make_pair(node, i));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000516 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000517
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000518 //
Vikram S. Adve7952d602003-05-31 07:37:05 +0000519 // Collect value defs. for implicit operands. They may have allocated
520 // physical registers also.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000521 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000522 for (unsigned i=0, N = MI.getNumImplicitRefs(); i != N; ++i) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000523 const MachineOperand& mop = MI.getImplicitOp(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000524 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000525 int regNum = mop.getAllocatedRegNum();
526 if (regNum != target.getRegInfo().getZeroRegNum())
527 regToRefVecMap[mop.getAllocatedRegNum()]
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000528 .push_back(std::make_pair(node, i + MI.getNumOperands()));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000529 continue; // nothing more to do
530 }
531
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000532 if (mop.opIsDefOnly() || mop.opIsDefAndUse()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000533 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
534 valueToDefVecMap[MI.getImplicitRef(i)].push_back(std::make_pair(node,
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000535 -i));
536 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000537 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000538}
539
540
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000541void SchedGraph::buildNodesForBB(const TargetMachine& target,
542 MachineBasicBlock& MBB,
543 std::vector<SchedGraphNode*>& memNodeVec,
544 std::vector<SchedGraphNode*>& callDepNodeVec,
545 RegToRefVecMap& regToRefVecMap,
546 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000547 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000548
549 // Build graph nodes for each VM instruction and gather def/use info.
550 // Do both those together in a single pass over all machine instructions.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000551 for (unsigned i=0; i < MBB.size(); i++)
552 if (!mii.isDummyPhiInstr(MBB[i]->getOpCode())) {
553 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
554 noteGraphNodeForInstr(MBB[i], node);
555
556 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000557 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
558 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000559 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000560}
561
562
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000563void SchedGraph::buildGraph(const TargetMachine& target) {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000564 // Use this data structure to note all machine operands that compute
565 // ordinary LLVM values. These must be computed defs (i.e., instructions).
566 // Note that there may be multiple machine instructions that define
567 // each Value.
568 ValueToDefVecMap valueToDefVecMap;
569
Vikram S. Advee64574c2001-11-08 05:20:23 +0000570 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000571 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000572 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000573
574 // Use this data structure to note all instructions that access physical
575 // registers that can be modified by a call (including call instructions)
576 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000577
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000578 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000579 // machine registers so we can add edges for those later without
580 // extra passes over the nodes.
581 // The vector holds an ordered list of references to the machine reg,
582 // ordered according to control-flow order. This only works for a
583 // single basic block, hence the assertion. Each reference is identified
584 // by the pair: <node, operand-number>.
585 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000586 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000587
588 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000589 graphRoot = new SchedGraphNode(0, NULL, -1, target);
590 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000591
592 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000593 // First add nodes for all the machine instructions in the basic block
594 // because this greatly simplifies identifying which edges to add.
595 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000596 // Also, remember the load/store instructions to add memory deps later.
597 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000598
Vikram S. Adve7952d602003-05-31 07:37:05 +0000599 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
600 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000601
602 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000603 // Now add edges for the following (all are incoming edges except (4)):
604 // (1) operands of the machine instruction, including hidden operands
605 // (2) machine register dependences
606 // (3) memory load/store dependences
607 // (3) other resource dependences for the machine instruction, if any
608 // (4) output dependences when multiple machine instructions define the
609 // same value; all must have been generated from a single VM instrn
610 // (5) control dependences to branch instructions generated for the
611 // terminator instruction of the BB. Because of delay slots and
612 // 2-way conditional branches, multiple CD edges are needed
613 // (see addCDEdges for details).
614 // Also, note any uses or defs of machine registers.
615 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000616 //----------------------------------------------------------------
617
618 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000619 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000620
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000621 // Then add memory dep edges: store->load, load->store, and store->store.
622 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000623 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000624
625 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000626 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000627
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000628 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000629 for (unsigned i=0, N=MBB.size(); i < N; i++)
630 addEdgesForInstruction(*MBB[i], valueToDefVecMap, target);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000631
Vikram S. Adve200a4352001-11-12 18:53:43 +0000632#ifdef NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000633 // Then add non-SSA edges for all VM instructions in the block.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000634 // We assume that all machine instructions that define a value are
635 // generated from the VM instruction corresponding to that value.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000636 // TODO: This could probably be done much more efficiently.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000637 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000638 this->addNonSSAEdgesForValue(*II, target);
Chris Lattner4ed17ba2001-11-26 18:56:52 +0000639#endif //NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000640
641 // Then add edges for dependences on machine registers
642 this->addMachineRegEdges(regToRefVecMap, target);
643
644 // Finally, add edges from the dummy root and to dummy leaf
645 this->addDummyEdges();
646}
647
648
649//
650// class SchedGraphSet
651//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000652SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000653 const TargetMachine& target) :
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000654 function(_function) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000655 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000656}
657
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000658SchedGraphSet::~SchedGraphSet() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000659 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000660 for(iterator I = begin(), E = end(); I != E; ++I)
661 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000662}
663
664
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000665void SchedGraphSet::dump() const {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000666 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000667 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000668
669 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000670 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000671
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000672 std::cerr << "\n====== End graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000673 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000674}
675
676
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000677void SchedGraphSet::buildGraphsForMethod(const Function *F,
678 const TargetMachine& target) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000679 MachineFunction &MF = MachineFunction::get(F);
680 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
681 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000682}
683
684
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000685void SchedGraphEdge::print(std::ostream &os) const {
686 os << "edge [" << src->getNodeId() << "] -> ["
687 << sink->getNodeId() << "] : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000688
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000689 switch(depType) {
690 case SchedGraphEdge::CtrlDep:
691 os<< "Control Dep";
692 break;
693 case SchedGraphEdge::ValueDep:
694 os<< "Reg Value " << val;
695 break;
696 case SchedGraphEdge::MemoryDep:
697 os<< "Memory Dep";
698 break;
699 case SchedGraphEdge::MachineRegister:
700 os<< "Reg " << machineRegNum;
701 break;
702 case SchedGraphEdge::MachineResource:
703 os<<"Resource "<< resourceId;
704 break;
705 default:
706 assert(0);
707 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000708 }
709
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000710 os << " : delay = " << minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000711}
712
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000713void SchedGraphNode::print(std::ostream &os) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000714 os << std::string(8, ' ')
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000715 << "Node " << ID << " : "
716 << "latency = " << latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000717
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000718 if (getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000719 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000720 else {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000721 os << *getMachineInstr() << "\n" << std::string(12, ' ');
722 os << inEdges.size() << " Incoming Edges:\n";
723 for (unsigned i=0, N = inEdges.size(); i < N; i++)
724 os << std::string(16, ' ') << *inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000725
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000726 os << std::string(12, ' ') << outEdges.size()
Misha Brukman6b77ec42003-05-22 21:49:18 +0000727 << " Outgoing Edges:\n";
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000728 for (unsigned i=0, N= outEdges.size(); i < N; i++)
729 os << std::string(16, ' ') << *outEdges[i];
Misha Brukman6b77ec42003-05-22 21:49:18 +0000730 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000731}