blob: f6f37917342ab3807b8f9fbb50bddb5ef8f5b33d [file] [log] [blame]
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +00001//===- SchedGraph.cpp - Scheduling Graph Implementation -------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +00009//
10// Scheduling graph based on SSA graph plus extra dependence edges capturing
11// dependences due to machine resources (machine registers, CC registers, and
12// any others).
13//
14//===----------------------------------------------------------------------===//
Vikram S. Adve78ef1392001-08-28 23:06:02 +000015
Chris Lattner46cbff62001-09-14 16:56:32 +000016#include "SchedGraph.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000017#include "llvm/Function.h"
Chris Lattnerb00c5822001-10-02 03:41:24 +000018#include "llvm/iOther.h"
Tanya Lattnerc50ee552003-08-27 02:42:58 +000019#include "llvm/CodeGen/MachineCodeForInstruction.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetRegInfo.h"
24#include "Support/STLExtras.h"
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
Tanya Lattnerc50ee552003-08-27 02:42:58 +000052SchedGraphNode::SchedGraphNode(unsigned NID, MachineBasicBlock *mbb,
53 int indexInBB, const TargetMachine& Target)
Tanya Lattner8dc99822003-08-28 15:30:40 +000054 : SchedGraphNodeCommon(NID,indexInBB), MBB(mbb), MI(mbb ? (*mbb)[indexInBB] : 0) {
Tanya Lattnerc50ee552003-08-27 02:42:58 +000055 if (MI) {
56 MachineOpCode mopCode = MI->getOpCode();
57 latency = Target.getInstrInfo().hasResultInterlock(mopCode)
58 ? Target.getInstrInfo().minLatency(mopCode)
59 : Target.getInstrInfo().maxLatency(mopCode);
60 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000061}
62
John Criswellc9afb492003-08-28 21:43:17 +000063//
64// Method: SchedGraphNode Destructor
65//
66// Description:
67// Free memory allocated by the SchedGraphNode object.
68//
69// Notes:
70// Do not delete the edges here. The base class will take care of that.
71// Only handle subclass specific stuff here (where currently there is
72// none).
73//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000074SchedGraphNode::~SchedGraphNode() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000075}
76
Vikram S. Adve78ef1392001-08-28 23:06:02 +000077//
78// class SchedGraph
79//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000080SchedGraph::SchedGraph(MachineBasicBlock &mbb, const TargetMachine& target)
81 : MBB(mbb) {
Chris Lattner697954c2002-01-20 22:54:45 +000082 buildGraph(target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000083}
84
John Criswellc9afb492003-08-28 21:43:17 +000085//
86// Method: SchedGraph Destructor
87//
88// Description:
89// This method deletes memory allocated by the SchedGraph object.
90//
91// Notes:
92// Do not delete the graphRoot or graphLeaf here. The base class handles
93// that bit of work.
94//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000095SchedGraph::~SchedGraph() {
Chris Lattner697954c2002-01-20 22:54:45 +000096 for (const_iterator I = begin(); I != end(); ++I)
Chris Lattnerf3dd05c2002-04-09 05:15:33 +000097 delete I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000098}
99
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000100void SchedGraph::dump() const {
Misha Brukmanc2312df2003-05-22 21:24:35 +0000101 std::cerr << " Sched Graph for Basic Block: ";
102 std::cerr << MBB.getBasicBlock()->getName()
103 << " (" << MBB.getBasicBlock() << ")";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000104
Misha Brukmanc2312df2003-05-22 21:24:35 +0000105 std::cerr << "\n\n Actual Root nodes : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000106 for (unsigned i=0, N=graphRoot->outEdges.size(); i < N; i++)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000107 std::cerr << graphRoot->outEdges[i]->getSink()->getNodeId()
108 << ((i == N-1)? "" : ", ");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000109
Misha Brukmanc2312df2003-05-22 21:24:35 +0000110 std::cerr << "\n Graph Nodes:\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000111 for (const_iterator I=begin(); I != end(); ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000112 std::cerr << "\n" << *I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000113
Misha Brukmanc2312df2003-05-22 21:24:35 +0000114 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000115}
116
117
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000118
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000119void SchedGraph::addDummyEdges() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000120 assert(graphRoot->outEdges.size() == 0);
121
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000122 for (const_iterator I=begin(); I != end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000123 SchedGraphNode* node = (*I).second;
124 assert(node != graphRoot && node != graphLeaf);
125 if (node->beginInEdges() == node->endInEdges())
126 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
127 SchedGraphEdge::NonDataDep, 0);
128 if (node->beginOutEdges() == node->endOutEdges())
129 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
130 SchedGraphEdge::NonDataDep, 0);
131 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000132}
133
134
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000135void SchedGraph::addCDEdges(const TerminatorInst* term,
136 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000137 const TargetInstrInfo& mii = target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000138 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000139
140 // Find the first branch instr in the sequence of machine instrs for term
141 //
142 unsigned first = 0;
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000143 while (! mii.isBranch(termMvec[first]->getOpCode()) &&
144 ! mii.isReturn(termMvec[first]->getOpCode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000145 ++first;
146 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000147 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000148 if (first == termMvec.size())
149 return;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000150
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000151 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000152
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000153 // Add CD edges from each instruction in the sequence to the
154 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000155 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000156 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000157 for (unsigned i = termMvec.size(); i > first+1; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000158 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
159 assert(toNode && "No node for instr generated for branch/ret?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000160
Misha Brukman6b77ec42003-05-22 21:49:18 +0000161 for (unsigned j = i-1; j != 0; --j)
162 if (mii.isBranch(termMvec[j-1]->getOpCode()) ||
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000163 mii.isReturn(termMvec[j-1]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000164 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
165 assert(brNode && "No node for instr generated for branch/ret?");
166 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
167 SchedGraphEdge::NonDataDep, 0);
168 break; // only one incoming edge is enough
169 }
170 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000171
172 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000173 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000174 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000175 for (unsigned i = first; i != 0; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000176 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
177 assert(fromNode && "No node for instr generated for branch?");
178 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
179 SchedGraphEdge::NonDataDep, 0);
180 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000181
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000182 // Now add CD edges to the first branch instruction in the sequence from
183 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000184 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000185 for (unsigned i=0, N=MBB.size(); i < N; i++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000186 if (MBB[i] == termMvec[first]) // reached the first branch
187 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000188
Misha Brukman6b77ec42003-05-22 21:49:18 +0000189 SchedGraphNode* fromNode = this->getGraphNodeForInstr(MBB[i]);
190 if (fromNode == NULL)
191 continue; // dummy instruction, e.g., PHI
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000192
Misha Brukman6b77ec42003-05-22 21:49:18 +0000193 (void) new SchedGraphEdge(fromNode, firstBrNode,
194 SchedGraphEdge::CtrlDep,
195 SchedGraphEdge::NonDataDep, 0);
196
197 // If we find any other machine instructions (other than due to
198 // the terminator) that also have delay slots, add an outgoing edge
199 // from the instruction to the instructions in the delay slots.
200 //
201 unsigned d = mii.getNumDelaySlots(MBB[i]->getOpCode());
202 assert(i+d < N && "Insufficient delay slots for instruction?");
203
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000204 for (unsigned j=1; j <= d; j++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000205 SchedGraphNode* toNode = this->getGraphNodeForInstr(MBB[i+j]);
206 assert(toNode && "No node for machine instr in delay slot?");
207 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000208 SchedGraphEdge::CtrlDep,
209 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000210 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000211 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000212}
213
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000214static const int SG_LOAD_REF = 0;
215static const int SG_STORE_REF = 1;
216static const int SG_CALL_REF = 2;
217
218static const unsigned int SG_DepOrderArray[][3] = {
219 { SchedGraphEdge::NonDataDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000220 SchedGraphEdge::AntiDep,
221 SchedGraphEdge::AntiDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000222 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000223 SchedGraphEdge::OutputDep,
224 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000225 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000226 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
227 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
228 | SchedGraphEdge::OutputDep }
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000229};
230
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000231
Vikram S. Advee64574c2001-11-08 05:20:23 +0000232// Add a dependence edge between every pair of machine load/store/call
233// instructions, where at least one is a store or a call.
234// Use latency 1 just to ensure that memory operations are ordered;
235// latency does not otherwise matter (true dependences enforce that).
236//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000237void SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
238 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000239 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000240
Vikram S. Advee64574c2001-11-08 05:20:23 +0000241 // Instructions in memNodeVec are in execution order within the basic block,
242 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
243 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000244 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000245 MachineOpCode fromOpCode = memNodeVec[im]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000246 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
247 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
248 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000249 for (unsigned jm=im+1; jm < NM; jm++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000250 MachineOpCode toOpCode = memNodeVec[jm]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000251 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
252 : (mii.isLoad(toOpCode)? SG_LOAD_REF
253 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000254
Misha Brukman6b77ec42003-05-22 21:49:18 +0000255 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
256 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
257 SchedGraphEdge::MemoryDep,
258 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000259 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000260 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000261}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000262
Vikram S. Advee64574c2001-11-08 05:20:23 +0000263// Add edges from/to CC reg instrs to/from call instrs.
264// Essentially this prevents anything that sets or uses a CC reg from being
265// reordered w.r.t. a call.
266// Use a latency of 0 because we only need to prevent out-of-order issue,
267// like with control dependences.
268//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000269void SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
270 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000271 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000272
Vikram S. Adve7952d602003-05-31 07:37:05 +0000273 // Instructions in memNodeVec are in execution order within the basic block,
274 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
275 //
276 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000277 if (mii.isCall(callDepNodeVec[ic]->getOpCode())) {
278 // Add SG_CALL_REF edges from all preds to this instruction.
279 for (unsigned jc=0; jc < ic; jc++)
280 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
281 SchedGraphEdge::MachineRegister,
282 MachineIntRegsRID, 0);
283
284 // And do the same from this instruction to all successors.
285 for (unsigned jc=ic+1; jc < NC; jc++)
286 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
287 SchedGraphEdge::MachineRegister,
288 MachineIntRegsRID, 0);
289 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000290
291#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000292 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000293 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000294 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
295 if (mii.isCall(memNodeVec[im]->getOpCode()))
296 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000297
Vikram S. Advee64574c2001-11-08 05:20:23 +0000298 // Now walk the entire basic block, looking for CC instructions *and*
299 // call instructions, and keep track of the order of the instructions.
300 // Use the call node vec to quickly find earlier and later call nodes
301 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000302 //
303 int lastCallNodeIdx = -1;
304 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000305 if (mii.isCall(bbMvec[i]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000306 ++lastCallNodeIdx;
307 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
308 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
309 break;
310 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000311 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000312 else if (mii.isCCInstr(bbMvec[i]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000313 // Add incoming/outgoing edges from/to preceding/later calls
314 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
315 int j=0;
316 for ( ; j <= lastCallNodeIdx; j++)
317 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
318 MachineCCRegsRID, 0);
319 for ( ; j < (int) callNodeVec.size(); j++)
320 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
321 MachineCCRegsRID, 0);
322 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000323#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000324}
325
326
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000327void SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
328 const TargetMachine& target) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000329 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000330 // not aliased!
331 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000332 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000333 I != regToRefVecMap.end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000334 int regNum = (*I).first;
335 RefVec& regRefVec = (*I).second;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000336
Misha Brukman6b77ec42003-05-22 21:49:18 +0000337 // regRefVec is ordered by control flow order in the basic block
338 for (unsigned i=0; i < regRefVec.size(); ++i) {
339 SchedGraphNode* node = regRefVec[i].first;
340 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000341 const MachineOperand& mop =
342 node->getMachineInstr()->getExplOrImplOperand(opNum);
343 bool isDef = mop.opIsDefOnly();
344 bool isDefAndUse = mop.opIsDefAndUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000345
Misha Brukman6b77ec42003-05-22 21:49:18 +0000346 for (unsigned p=0; p < i; ++p) {
347 SchedGraphNode* prevNode = regRefVec[p].first;
348 if (prevNode != node) {
349 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000350 const MachineOperand& prevMop =
351 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
352 bool prevIsDef = prevMop.opIsDefOnly();
353 bool prevIsDefAndUse = prevMop.opIsDefAndUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000354 if (isDef) {
355 if (prevIsDef)
356 new SchedGraphEdge(prevNode, node, regNum,
357 SchedGraphEdge::OutputDep);
358 if (!prevIsDef || prevIsDefAndUse)
359 new SchedGraphEdge(prevNode, node, regNum,
360 SchedGraphEdge::AntiDep);
361 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000362
Misha Brukman6b77ec42003-05-22 21:49:18 +0000363 if (prevIsDef)
364 if (!isDef || isDefAndUse)
365 new SchedGraphEdge(prevNode, node, regNum,
366 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000367 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000368 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000369 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000370 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000371}
372
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000373
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000374// Adds dependences to/from refNode from/to all other defs
375// in the basic block. refNode may be a use, a def, or both.
376// We do not consider other uses because we are not building use-use deps.
377//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000378void SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
379 const RefVec& defVec,
380 const Value* defValue,
381 bool refNodeIsDef,
382 bool refNodeIsDefAndUse,
383 const TargetMachine& target) {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000384 bool refNodeIsUse = !refNodeIsDef || refNodeIsDefAndUse;
385
Vikram S. Adve200a4352001-11-12 18:53:43 +0000386 // Add true or output dep edges from all def nodes before refNode in BB.
387 // Add anti or output dep edges to all def nodes after refNode.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000388 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000389 if ((*I).first == refNode)
390 continue; // Dont add any self-loops
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000391
Misha Brukman6b77ec42003-05-22 21:49:18 +0000392 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB()) {
393 // (*).first is before refNode
394 if (refNodeIsDef)
395 (void) new SchedGraphEdge((*I).first, refNode, defValue,
396 SchedGraphEdge::OutputDep);
397 if (refNodeIsUse)
398 (void) new SchedGraphEdge((*I).first, refNode, defValue,
399 SchedGraphEdge::TrueDep);
400 } else {
401 // (*).first is after refNode
402 if (refNodeIsDef)
403 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
404 SchedGraphEdge::OutputDep);
405 if (refNodeIsUse)
406 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
407 SchedGraphEdge::AntiDep);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000408 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000409 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000410}
411
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000412
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000413void SchedGraph::addEdgesForInstruction(const MachineInstr& MI,
414 const ValueToDefVecMap& valueToDefVecMap,
415 const TargetMachine& target) {
Chris Lattner133f0792002-10-28 04:45:29 +0000416 SchedGraphNode* node = getGraphNodeForInstr(&MI);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000417 if (node == NULL)
418 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000419
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000420 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000421 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000422 for (unsigned i = 0, numOps = MI.getNumOperands(); i != numOps; ++i) {
423 switch (MI.getOperand(i).getType()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000424 case MachineOperand::MO_VirtualRegister:
425 case MachineOperand::MO_CCRegister:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000426 if (const Value* srcI = MI.getOperand(i).getVRegValue()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000427 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
428 if (I != valueToDefVecMap.end())
429 addEdgesForValue(node, I->second, srcI,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000430 MI.getOperand(i).opIsDefOnly(),
431 MI.getOperand(i).opIsDefAndUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000432 }
433 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000434
Misha Brukman6b77ec42003-05-22 21:49:18 +0000435 case MachineOperand::MO_MachineRegister:
436 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000437
Misha Brukman6b77ec42003-05-22 21:49:18 +0000438 case MachineOperand::MO_SignExtendedImmed:
439 case MachineOperand::MO_UnextendedImmed:
440 case MachineOperand::MO_PCRelativeDisp:
441 break; // nothing to do for immediate fields
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000442
Misha Brukman6b77ec42003-05-22 21:49:18 +0000443 default:
444 assert(0 && "Unknown machine operand type in SchedGraph builder");
445 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000446 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000447 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000448
449 // Add edges for values implicitly used by the machine instruction.
450 // Examples include function arguments to a Call instructions or the return
451 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000452 //
Chris Lattner133f0792002-10-28 04:45:29 +0000453 for (unsigned i=0, N=MI.getNumImplicitRefs(); i < N; ++i)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000454 if (MI.getImplicitOp(i).opIsUse() || MI.getImplicitOp(i).opIsDefAndUse())
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000455 if (const Value* srcI = MI.getImplicitRef(i)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000456 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
457 if (I != valueToDefVecMap.end())
458 addEdgesForValue(node, I->second, srcI,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000459 MI.getImplicitOp(i).opIsDefOnly(),
460 MI.getImplicitOp(i).opIsDefAndUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000461 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000462}
463
464
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000465void SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
466 SchedGraphNode* node,
467 std::vector<SchedGraphNode*>& memNodeVec,
468 std::vector<SchedGraphNode*>& callDepNodeVec,
469 RegToRefVecMap& regToRefVecMap,
470 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000471 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000472
Vikram S. Advee64574c2001-11-08 05:20:23 +0000473 MachineOpCode opCode = node->getOpCode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000474
Vikram S. Adve7952d602003-05-31 07:37:05 +0000475 if (mii.isCall(opCode) || mii.isCCInstr(opCode))
476 callDepNodeVec.push_back(node);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000477
Vikram S. Advee64574c2001-11-08 05:20:23 +0000478 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
479 memNodeVec.push_back(node);
480
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000481 // Collect the register references and value defs. for explicit operands
482 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000483 const MachineInstr& MI = *node->getMachineInstr();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000484 for (int i=0, numOps = (int) MI.getNumOperands(); i < numOps; i++) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000485 const MachineOperand& mop = MI.getOperand(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000486
Misha Brukman6b77ec42003-05-22 21:49:18 +0000487 // if this references a register other than the hardwired
488 // "zero" register, record the reference.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000489 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000490 int regNum = mop.getAllocatedRegNum();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000491
Vikram S. Adve7952d602003-05-31 07:37:05 +0000492 // If this is not a dummy zero register, record the reference in order
Misha Brukman6b77ec42003-05-22 21:49:18 +0000493 if (regNum != target.getRegInfo().getZeroRegNum())
Vikram S. Adve7952d602003-05-31 07:37:05 +0000494 regToRefVecMap[mop.getAllocatedRegNum()]
Misha Brukman6b77ec42003-05-22 21:49:18 +0000495 .push_back(std::make_pair(node, i));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000496
497 // If this is a volatile register, add the instruction to callDepVec
498 // (only if the node is not already on the callDepVec!)
499 if (callDepNodeVec.size() == 0 || callDepNodeVec.back() != node)
500 {
501 unsigned rcid;
502 int regInClass = target.getRegInfo().getClassRegNum(regNum, rcid);
503 if (target.getRegInfo().getMachineRegClass(rcid)
504 ->isRegVolatile(regInClass))
505 callDepNodeVec.push_back(node);
506 }
507
Misha Brukman6b77ec42003-05-22 21:49:18 +0000508 continue; // nothing more to do
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000509 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000510
Misha Brukman6b77ec42003-05-22 21:49:18 +0000511 // ignore all other non-def operands
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000512 if (!MI.getOperand(i).opIsDefOnly() &&
513 !MI.getOperand(i).opIsDefAndUse())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000514 continue;
515
516 // We must be defining a value.
517 assert((mop.getType() == MachineOperand::MO_VirtualRegister ||
518 mop.getType() == MachineOperand::MO_CCRegister)
519 && "Do not expect any other kind of operand to be defined!");
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000520 assert(mop.getVRegValue() != NULL && "Null value being defined?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000521
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000522 valueToDefVecMap[mop.getVRegValue()].push_back(std::make_pair(node, i));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000523 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000524
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000525 //
Vikram S. Adve7952d602003-05-31 07:37:05 +0000526 // Collect value defs. for implicit operands. They may have allocated
527 // physical registers also.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000528 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000529 for (unsigned i=0, N = MI.getNumImplicitRefs(); i != N; ++i) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000530 const MachineOperand& mop = MI.getImplicitOp(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000531 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000532 int regNum = mop.getAllocatedRegNum();
533 if (regNum != target.getRegInfo().getZeroRegNum())
534 regToRefVecMap[mop.getAllocatedRegNum()]
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000535 .push_back(std::make_pair(node, i + MI.getNumOperands()));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000536 continue; // nothing more to do
537 }
538
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000539 if (mop.opIsDefOnly() || mop.opIsDefAndUse()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000540 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
541 valueToDefVecMap[MI.getImplicitRef(i)].push_back(std::make_pair(node,
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000542 -i));
543 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000544 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000545}
546
547
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000548void SchedGraph::buildNodesForBB(const TargetMachine& target,
549 MachineBasicBlock& MBB,
550 std::vector<SchedGraphNode*>& memNodeVec,
551 std::vector<SchedGraphNode*>& callDepNodeVec,
552 RegToRefVecMap& regToRefVecMap,
553 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000554 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000555
556 // Build graph nodes for each VM instruction and gather def/use info.
557 // Do both those together in a single pass over all machine instructions.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000558 for (unsigned i=0; i < MBB.size(); i++)
559 if (!mii.isDummyPhiInstr(MBB[i]->getOpCode())) {
560 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
561 noteGraphNodeForInstr(MBB[i], node);
562
563 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000564 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
565 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000566 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000567}
568
569
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000570void SchedGraph::buildGraph(const TargetMachine& target) {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000571 // Use this data structure to note all machine operands that compute
572 // ordinary LLVM values. These must be computed defs (i.e., instructions).
573 // Note that there may be multiple machine instructions that define
574 // each Value.
575 ValueToDefVecMap valueToDefVecMap;
576
Vikram S. Advee64574c2001-11-08 05:20:23 +0000577 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000578 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000579 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000580
581 // Use this data structure to note all instructions that access physical
582 // registers that can be modified by a call (including call instructions)
583 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000584
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000585 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000586 // machine registers so we can add edges for those later without
587 // extra passes over the nodes.
588 // The vector holds an ordered list of references to the machine reg,
589 // ordered according to control-flow order. This only works for a
590 // single basic block, hence the assertion. Each reference is identified
591 // by the pair: <node, operand-number>.
592 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000593 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000594
595 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000596 graphRoot = new SchedGraphNode(0, NULL, -1, target);
597 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000598
599 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000600 // First add nodes for all the machine instructions in the basic block
601 // because this greatly simplifies identifying which edges to add.
602 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000603 // Also, remember the load/store instructions to add memory deps later.
604 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000605
Vikram S. Adve7952d602003-05-31 07:37:05 +0000606 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
607 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000608
609 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000610 // Now add edges for the following (all are incoming edges except (4)):
611 // (1) operands of the machine instruction, including hidden operands
612 // (2) machine register dependences
613 // (3) memory load/store dependences
614 // (3) other resource dependences for the machine instruction, if any
615 // (4) output dependences when multiple machine instructions define the
616 // same value; all must have been generated from a single VM instrn
617 // (5) control dependences to branch instructions generated for the
618 // terminator instruction of the BB. Because of delay slots and
619 // 2-way conditional branches, multiple CD edges are needed
620 // (see addCDEdges for details).
621 // Also, note any uses or defs of machine registers.
622 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000623 //----------------------------------------------------------------
624
625 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000626 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000627
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000628 // Then add memory dep edges: store->load, load->store, and store->store.
629 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000630 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000631
632 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000633 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000634
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000635 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000636 for (unsigned i=0, N=MBB.size(); i < N; i++)
637 addEdgesForInstruction(*MBB[i], valueToDefVecMap, target);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000638
Vikram S. Adve200a4352001-11-12 18:53:43 +0000639#ifdef NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000640 // Then add non-SSA edges for all VM instructions in the block.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000641 // We assume that all machine instructions that define a value are
642 // generated from the VM instruction corresponding to that value.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000643 // TODO: This could probably be done much more efficiently.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000644 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000645 this->addNonSSAEdgesForValue(*II, target);
Chris Lattner4ed17ba2001-11-26 18:56:52 +0000646#endif //NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000647
648 // Then add edges for dependences on machine registers
649 this->addMachineRegEdges(regToRefVecMap, target);
650
651 // Finally, add edges from the dummy root and to dummy leaf
652 this->addDummyEdges();
653}
654
655
656//
657// class SchedGraphSet
658//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000659SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000660 const TargetMachine& target) :
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000661 function(_function) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000662 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000663}
664
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000665SchedGraphSet::~SchedGraphSet() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000666 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000667 for(iterator I = begin(), E = end(); I != E; ++I)
668 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000669}
670
671
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000672void SchedGraphSet::dump() const {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000673 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000674 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000675
676 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000677 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000678
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000679 std::cerr << "\n====== End graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000680 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000681}
682
683
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000684void SchedGraphSet::buildGraphsForMethod(const Function *F,
685 const TargetMachine& target) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000686 MachineFunction &MF = MachineFunction::get(F);
687 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
688 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000689}
690
691
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000692void SchedGraphEdge::print(std::ostream &os) const {
693 os << "edge [" << src->getNodeId() << "] -> ["
694 << sink->getNodeId() << "] : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000695
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000696 switch(depType) {
697 case SchedGraphEdge::CtrlDep:
698 os<< "Control Dep";
699 break;
700 case SchedGraphEdge::ValueDep:
701 os<< "Reg Value " << val;
702 break;
703 case SchedGraphEdge::MemoryDep:
704 os<< "Memory Dep";
705 break;
706 case SchedGraphEdge::MachineRegister:
707 os<< "Reg " << machineRegNum;
708 break;
709 case SchedGraphEdge::MachineResource:
710 os<<"Resource "<< resourceId;
711 break;
712 default:
713 assert(0);
714 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000715 }
716
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000717 os << " : delay = " << minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000718}
719
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000720void SchedGraphNode::print(std::ostream &os) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000721 os << std::string(8, ' ')
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000722 << "Node " << ID << " : "
723 << "latency = " << latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000724
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000725 if (getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000726 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000727 else {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000728 os << *getMachineInstr() << "\n" << std::string(12, ' ');
729 os << inEdges.size() << " Incoming Edges:\n";
730 for (unsigned i=0, N = inEdges.size(); i < N; i++)
731 os << std::string(16, ' ') << *inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000732
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000733 os << std::string(12, ' ') << outEdges.size()
Misha Brukman6b77ec42003-05-22 21:49:18 +0000734 << " Outgoing Edges:\n";
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000735 for (unsigned i=0, N= outEdges.size(); i < N; i++)
736 os << std::string(16, ' ') << *outEdges[i];
Misha Brukman6b77ec42003-05-22 21:49:18 +0000737 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000738}