blob: 443f4e2c62aae8673e710254f9fe30ee08e6a8e6 [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"
Brian Gaeke35450d22004-04-23 18:15:46 +000023#include "../../Target/SparcV9/SparcV9RegInfo.h"
Tanya Lattnerc50ee552003-08-27 02:42:58 +000024#include "Support/STLExtras.h"
Vikram S. Adve78ef1392001-08-28 23:06:02 +000025
Brian Gaeked0fde302003-11-11 22:41:34 +000026namespace llvm {
27
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000028//*********************** Internal Data Structures *************************/
29
Vikram S. Advec352d2c2001-11-05 04:04:23 +000030// The following two types need to be classes, not typedefs, so we can use
31// opaque declarations in SchedGraph.h
32//
Misha Brukmanc2312df2003-05-22 21:24:35 +000033struct RefVec: public std::vector<std::pair<SchedGraphNode*, int> > {
34 typedef std::vector<std::pair<SchedGraphNode*,int> >::iterator iterator;
35 typedef
36 std::vector<std::pair<SchedGraphNode*,int> >::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000037};
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000038
Chris Lattner80c685f2001-10-13 06:51:01 +000039struct RegToRefVecMap: public hash_map<int, RefVec> {
Vikram S. Advec352d2c2001-11-05 04:04:23 +000040 typedef hash_map<int, RefVec>:: iterator iterator;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000041 typedef hash_map<int, RefVec>::const_iterator const_iterator;
42};
43
Vikram S. Adve74d15d32003-07-02 01:16:01 +000044struct ValueToDefVecMap: public hash_map<const Value*, RefVec> {
45 typedef hash_map<const Value*, RefVec>:: iterator iterator;
46 typedef hash_map<const Value*, RefVec>::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000047};
48
Vikram S. Adve78ef1392001-08-28 23:06:02 +000049
50//
51// class SchedGraphNode
52//
53
Tanya Lattnerc50ee552003-08-27 02:42:58 +000054SchedGraphNode::SchedGraphNode(unsigned NID, MachineBasicBlock *mbb,
55 int indexInBB, const TargetMachine& Target)
Chris Lattnere6d04f12004-02-18 16:43:51 +000056 : SchedGraphNodeCommon(NID,indexInBB), MBB(mbb), MI(0) {
57 if (mbb) {
58 MachineBasicBlock::iterator I = MBB->begin();
59 std::advance(I, indexInBB);
60 MI = I;
61
Brian Gaeke918cdd42004-02-12 01:34:05 +000062 MachineOpCode mopCode = MI->getOpcode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +000063 latency = Target.getInstrInfo().hasResultInterlock(mopCode)
64 ? Target.getInstrInfo().minLatency(mopCode)
65 : Target.getInstrInfo().maxLatency(mopCode);
66 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000067}
68
John Criswellc9afb492003-08-28 21:43:17 +000069//
70// Method: SchedGraphNode Destructor
71//
72// Description:
73// Free memory allocated by the SchedGraphNode object.
74//
75// Notes:
76// Do not delete the edges here. The base class will take care of that.
77// Only handle subclass specific stuff here (where currently there is
78// none).
79//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000080SchedGraphNode::~SchedGraphNode() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000081}
82
Vikram S. Adve78ef1392001-08-28 23:06:02 +000083//
84// class SchedGraph
85//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000086SchedGraph::SchedGraph(MachineBasicBlock &mbb, const TargetMachine& target)
87 : MBB(mbb) {
Chris Lattner697954c2002-01-20 22:54:45 +000088 buildGraph(target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000089}
90
John Criswellc9afb492003-08-28 21:43:17 +000091//
92// Method: SchedGraph Destructor
93//
94// Description:
95// This method deletes memory allocated by the SchedGraph object.
96//
97// Notes:
98// Do not delete the graphRoot or graphLeaf here. The base class handles
99// that bit of work.
100//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000101SchedGraph::~SchedGraph() {
Chris Lattner697954c2002-01-20 22:54:45 +0000102 for (const_iterator I = begin(); I != end(); ++I)
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000103 delete I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000104}
105
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000106void SchedGraph::dump() const {
Brian Gaeke0dc57532004-02-09 18:42:05 +0000107 std::cerr << " Sched Graph for Basic Block: "
108 << MBB.getBasicBlock()->getName()
109 << " (" << MBB.getBasicBlock() << ")"
110 << "\n\n Actual Root nodes: ";
111 for (SchedGraphNodeCommon::const_iterator I = graphRoot->beginOutEdges(),
112 E = graphRoot->endOutEdges();
113 I != E; ++I) {
114 std::cerr << (*I)->getSink ()->getNodeId ();
115 if (I + 1 != E) { std::cerr << ", "; }
116 }
Misha Brukmanc2312df2003-05-22 21:24:35 +0000117 std::cerr << "\n Graph Nodes:\n";
Brian Gaeke0dc57532004-02-09 18:42:05 +0000118 for (const_iterator I = begin(), E = end(); I != E; ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000119 std::cerr << "\n" << *I->second;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000120 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000121}
122
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000123void SchedGraph::addDummyEdges() {
Brian Gaeke0dc57532004-02-09 18:42:05 +0000124 assert(graphRoot->getNumOutEdges() == 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000125
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000126 for (const_iterator I=begin(); I != end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000127 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
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000139void SchedGraph::addCDEdges(const TerminatorInst* term,
140 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000141 const TargetInstrInfo& mii = target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000142 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000143
144 // Find the first branch instr in the sequence of machine instrs for term
145 //
146 unsigned first = 0;
Brian Gaeke918cdd42004-02-12 01:34:05 +0000147 while (! mii.isBranch(termMvec[first]->getOpcode()) &&
148 ! mii.isReturn(termMvec[first]->getOpcode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000149 ++first;
150 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000151 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000152 if (first == termMvec.size())
153 return;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000154
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000155 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000156
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000157 // Add CD edges from each instruction in the sequence to the
158 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000159 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000160 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000161 for (unsigned i = termMvec.size(); i > first+1; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000162 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
163 assert(toNode && "No node for instr generated for branch/ret?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000164
Misha Brukman6b77ec42003-05-22 21:49:18 +0000165 for (unsigned j = i-1; j != 0; --j)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000166 if (mii.isBranch(termMvec[j-1]->getOpcode()) ||
167 mii.isReturn(termMvec[j-1]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000168 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
169 assert(brNode && "No node for instr generated for branch/ret?");
170 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
171 SchedGraphEdge::NonDataDep, 0);
172 break; // only one incoming edge is enough
173 }
174 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000175
176 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000177 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000178 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000179 for (unsigned i = first; i != 0; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000180 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
181 assert(fromNode && "No node for instr generated for branch?");
182 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
183 SchedGraphEdge::NonDataDep, 0);
184 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000185
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000186 // Now add CD edges to the first branch instruction in the sequence from
187 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000188 //
Chris Lattnere6d04f12004-02-18 16:43:51 +0000189 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
190 if (&*I == termMvec[first]) // reached the first branch
Misha Brukman6b77ec42003-05-22 21:49:18 +0000191 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000192
Chris Lattnere6d04f12004-02-18 16:43:51 +0000193 SchedGraphNode* fromNode = getGraphNodeForInstr(I);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000194 if (fromNode == NULL)
195 continue; // dummy instruction, e.g., PHI
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000196
Misha Brukman6b77ec42003-05-22 21:49:18 +0000197 (void) new SchedGraphEdge(fromNode, firstBrNode,
198 SchedGraphEdge::CtrlDep,
199 SchedGraphEdge::NonDataDep, 0);
200
201 // If we find any other machine instructions (other than due to
202 // the terminator) that also have delay slots, add an outgoing edge
203 // from the instruction to the instructions in the delay slots.
204 //
Chris Lattnere6d04f12004-02-18 16:43:51 +0000205 unsigned d = mii.getNumDelaySlots(I->getOpcode());
206
207 MachineBasicBlock::iterator J = I; ++J;
208 for (unsigned j=1; j <= d; j++, ++J) {
209 SchedGraphNode* toNode = this->getGraphNodeForInstr(J);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000210 assert(toNode && "No node for machine instr in delay slot?");
211 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000212 SchedGraphEdge::CtrlDep,
213 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000214 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000215 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000216}
217
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000218static const int SG_LOAD_REF = 0;
219static const int SG_STORE_REF = 1;
220static const int SG_CALL_REF = 2;
221
222static const unsigned int SG_DepOrderArray[][3] = {
223 { SchedGraphEdge::NonDataDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000224 SchedGraphEdge::AntiDep,
225 SchedGraphEdge::AntiDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000226 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000227 SchedGraphEdge::OutputDep,
228 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000229 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000230 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
231 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
232 | SchedGraphEdge::OutputDep }
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000233};
234
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000235
Vikram S. Advee64574c2001-11-08 05:20:23 +0000236// Add a dependence edge between every pair of machine load/store/call
237// instructions, where at least one is a store or a call.
238// Use latency 1 just to ensure that memory operations are ordered;
239// latency does not otherwise matter (true dependences enforce that).
240//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000241void SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
242 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000243 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000244
Vikram S. Advee64574c2001-11-08 05:20:23 +0000245 // Instructions in memNodeVec are in execution order within the basic block,
246 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
247 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000248 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000249 MachineOpCode fromOpCode = memNodeVec[im]->getOpcode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000250 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
251 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
252 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000253 for (unsigned jm=im+1; jm < NM; jm++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000254 MachineOpCode toOpCode = memNodeVec[jm]->getOpcode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000255 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
256 : (mii.isLoad(toOpCode)? SG_LOAD_REF
257 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000258
Misha Brukman6b77ec42003-05-22 21:49:18 +0000259 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
260 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
261 SchedGraphEdge::MemoryDep,
262 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000263 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000264 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000265}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000266
Vikram S. Advee64574c2001-11-08 05:20:23 +0000267// Add edges from/to CC reg instrs to/from call instrs.
268// Essentially this prevents anything that sets or uses a CC reg from being
269// reordered w.r.t. a call.
270// Use a latency of 0 because we only need to prevent out-of-order issue,
271// like with control dependences.
272//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000273void SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
274 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000275 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000276
Vikram S. Adve7952d602003-05-31 07:37:05 +0000277 // Instructions in memNodeVec are in execution order within the basic block,
278 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
279 //
280 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000281 if (mii.isCall(callDepNodeVec[ic]->getOpcode())) {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000282 // Add SG_CALL_REF edges from all preds to this instruction.
283 for (unsigned jc=0; jc < ic; jc++)
284 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
285 SchedGraphEdge::MachineRegister,
286 MachineIntRegsRID, 0);
287
288 // And do the same from this instruction to all successors.
289 for (unsigned jc=ic+1; jc < NC; jc++)
290 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
291 SchedGraphEdge::MachineRegister,
292 MachineIntRegsRID, 0);
293 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000294
295#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000296 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000297 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000298 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000299 if (mii.isCall(memNodeVec[im]->getOpcode()))
Vikram S. Advee64574c2001-11-08 05:20:23 +0000300 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000301
Vikram S. Advee64574c2001-11-08 05:20:23 +0000302 // Now walk the entire basic block, looking for CC instructions *and*
303 // call instructions, and keep track of the order of the instructions.
304 // Use the call node vec to quickly find earlier and later call nodes
305 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000306 //
307 int lastCallNodeIdx = -1;
308 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000309 if (mii.isCall(bbMvec[i]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000310 ++lastCallNodeIdx;
311 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
312 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
313 break;
314 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000315 }
Brian Gaeke918cdd42004-02-12 01:34:05 +0000316 else if (mii.isCCInstr(bbMvec[i]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000317 // Add incoming/outgoing edges from/to preceding/later calls
318 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
319 int j=0;
320 for ( ; j <= lastCallNodeIdx; j++)
321 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
322 MachineCCRegsRID, 0);
323 for ( ; j < (int) callNodeVec.size(); j++)
324 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
325 MachineCCRegsRID, 0);
326 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000327#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000328}
329
330
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000331void SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
332 const TargetMachine& target) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000333 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000334 // not aliased!
335 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000336 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000337 I != regToRefVecMap.end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000338 int regNum = (*I).first;
339 RefVec& regRefVec = (*I).second;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000340
Misha Brukman6b77ec42003-05-22 21:49:18 +0000341 // regRefVec is ordered by control flow order in the basic block
342 for (unsigned i=0; i < regRefVec.size(); ++i) {
343 SchedGraphNode* node = regRefVec[i].first;
344 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000345 const MachineOperand& mop =
346 node->getMachineInstr()->getExplOrImplOperand(opNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000347 bool isDef = mop.isDef() && !mop.isUse();
348 bool isDefAndUse = mop.isDef() && mop.isUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000349
Misha Brukman6b77ec42003-05-22 21:49:18 +0000350 for (unsigned p=0; p < i; ++p) {
351 SchedGraphNode* prevNode = regRefVec[p].first;
352 if (prevNode != node) {
353 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000354 const MachineOperand& prevMop =
355 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000356 bool prevIsDef = prevMop.isDef() && !prevMop.isUse();
357 bool prevIsDefAndUse = prevMop.isDef() && prevMop.isUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000358 if (isDef) {
359 if (prevIsDef)
360 new SchedGraphEdge(prevNode, node, regNum,
361 SchedGraphEdge::OutputDep);
362 if (!prevIsDef || prevIsDefAndUse)
363 new SchedGraphEdge(prevNode, node, regNum,
364 SchedGraphEdge::AntiDep);
365 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000366
Misha Brukman6b77ec42003-05-22 21:49:18 +0000367 if (prevIsDef)
368 if (!isDef || isDefAndUse)
369 new SchedGraphEdge(prevNode, node, regNum,
370 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000371 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000372 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000373 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000374 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000375}
376
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000377
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000378// Adds dependences to/from refNode from/to all other defs
379// in the basic block. refNode may be a use, a def, or both.
380// We do not consider other uses because we are not building use-use deps.
381//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000382void SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
383 const RefVec& defVec,
384 const Value* defValue,
385 bool refNodeIsDef,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000386 bool refNodeIsUse,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000387 const TargetMachine& target) {
Vikram S. Adve200a4352001-11-12 18:53:43 +0000388 // Add true or output dep edges from all def nodes before refNode in BB.
389 // Add anti or output dep edges to all def nodes after refNode.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000390 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000391 if ((*I).first == refNode)
392 continue; // Dont add any self-loops
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000393
Misha Brukman6b77ec42003-05-22 21:49:18 +0000394 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB()) {
395 // (*).first is before refNode
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000396 if (refNodeIsDef && !refNodeIsUse)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000397 (void) new SchedGraphEdge((*I).first, refNode, defValue,
398 SchedGraphEdge::OutputDep);
399 if (refNodeIsUse)
400 (void) new SchedGraphEdge((*I).first, refNode, defValue,
401 SchedGraphEdge::TrueDep);
402 } else {
403 // (*).first is after refNode
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000404 if (refNodeIsDef && !refNodeIsUse)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000405 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
406 SchedGraphEdge::OutputDep);
407 if (refNodeIsUse)
408 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
409 SchedGraphEdge::AntiDep);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000410 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000411 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000412}
413
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000414
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000415void SchedGraph::addEdgesForInstruction(const MachineInstr& MI,
416 const ValueToDefVecMap& valueToDefVecMap,
417 const TargetMachine& target) {
Chris Lattner133f0792002-10-28 04:45:29 +0000418 SchedGraphNode* node = getGraphNodeForInstr(&MI);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000419 if (node == NULL)
420 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000421
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000422 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000423 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000424 for (unsigned i = 0, numOps = MI.getNumOperands(); i != numOps; ++i) {
425 switch (MI.getOperand(i).getType()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000426 case MachineOperand::MO_VirtualRegister:
427 case MachineOperand::MO_CCRegister:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000428 if (const Value* srcI = MI.getOperand(i).getVRegValue()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000429 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
430 if (I != valueToDefVecMap.end())
431 addEdgesForValue(node, I->second, srcI,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000432 MI.getOperand(i).isDef(), MI.getOperand(i).isUse(),
433 target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000434 }
435 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000436
Misha Brukman6b77ec42003-05-22 21:49:18 +0000437 case MachineOperand::MO_MachineRegister:
438 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000439
Misha Brukman6b77ec42003-05-22 21:49:18 +0000440 case MachineOperand::MO_SignExtendedImmed:
441 case MachineOperand::MO_UnextendedImmed:
442 case MachineOperand::MO_PCRelativeDisp:
Misha Brukmane2bf0a22003-11-06 00:04:11 +0000443 case MachineOperand::MO_ConstantPoolIndex:
Misha Brukman6b77ec42003-05-22 21:49:18 +0000444 break; // nothing to do for immediate fields
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000445
Misha Brukman6b77ec42003-05-22 21:49:18 +0000446 default:
447 assert(0 && "Unknown machine operand type in SchedGraph builder");
448 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000449 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000450 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000451
452 // Add edges for values implicitly used by the machine instruction.
453 // Examples include function arguments to a Call instructions or the return
454 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000455 //
Chris Lattner133f0792002-10-28 04:45:29 +0000456 for (unsigned i=0, N=MI.getNumImplicitRefs(); i < N; ++i)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000457 if (MI.getImplicitOp(i).isUse())
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000458 if (const Value* srcI = MI.getImplicitRef(i)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000459 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
460 if (I != valueToDefVecMap.end())
461 addEdgesForValue(node, I->second, srcI,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000462 MI.getImplicitOp(i).isDef(),
463 MI.getImplicitOp(i).isUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000464 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000465}
466
467
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000468void SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
469 SchedGraphNode* node,
470 std::vector<SchedGraphNode*>& memNodeVec,
471 std::vector<SchedGraphNode*>& callDepNodeVec,
472 RegToRefVecMap& regToRefVecMap,
473 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000474 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000475
Brian Gaeke918cdd42004-02-12 01:34:05 +0000476 MachineOpCode opCode = node->getOpcode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000477
Vikram S. Adve7952d602003-05-31 07:37:05 +0000478 if (mii.isCall(opCode) || mii.isCCInstr(opCode))
479 callDepNodeVec.push_back(node);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000480
Vikram S. Advee64574c2001-11-08 05:20:23 +0000481 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
482 memNodeVec.push_back(node);
483
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000484 // Collect the register references and value defs. for explicit operands
485 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000486 const MachineInstr& MI = *node->getMachineInstr();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000487 for (int i=0, numOps = (int) MI.getNumOperands(); i < numOps; i++) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000488 const MachineOperand& mop = MI.getOperand(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000489
Misha Brukman6b77ec42003-05-22 21:49:18 +0000490 // if this references a register other than the hardwired
491 // "zero" register, record the reference.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000492 if (mop.hasAllocatedReg()) {
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000493 unsigned regNum = mop.getReg();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000494
Vikram S. Adve7952d602003-05-31 07:37:05 +0000495 // If this is not a dummy zero register, record the reference in order
Misha Brukman6b77ec42003-05-22 21:49:18 +0000496 if (regNum != target.getRegInfo().getZeroRegNum())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000497 regToRefVecMap[mop.getReg()]
Misha Brukman6b77ec42003-05-22 21:49:18 +0000498 .push_back(std::make_pair(node, i));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000499
500 // If this is a volatile register, add the instruction to callDepVec
501 // (only if the node is not already on the callDepVec!)
502 if (callDepNodeVec.size() == 0 || callDepNodeVec.back() != node)
503 {
504 unsigned rcid;
505 int regInClass = target.getRegInfo().getClassRegNum(regNum, rcid);
506 if (target.getRegInfo().getMachineRegClass(rcid)
507 ->isRegVolatile(regInClass))
508 callDepNodeVec.push_back(node);
509 }
510
Misha Brukman6b77ec42003-05-22 21:49:18 +0000511 continue; // nothing more to do
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000512 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000513
Misha Brukman6b77ec42003-05-22 21:49:18 +0000514 // ignore all other non-def operands
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000515 if (!MI.getOperand(i).isDef())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000516 continue;
517
518 // We must be defining a value.
519 assert((mop.getType() == MachineOperand::MO_VirtualRegister ||
520 mop.getType() == MachineOperand::MO_CCRegister)
521 && "Do not expect any other kind of operand to be defined!");
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000522 assert(mop.getVRegValue() != NULL && "Null value being defined?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000523
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000524 valueToDefVecMap[mop.getVRegValue()].push_back(std::make_pair(node, i));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000525 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000526
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000527 //
Vikram S. Adve7952d602003-05-31 07:37:05 +0000528 // Collect value defs. for implicit operands. They may have allocated
529 // physical registers also.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000530 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000531 for (unsigned i=0, N = MI.getNumImplicitRefs(); i != N; ++i) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000532 const MachineOperand& mop = MI.getImplicitOp(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000533 if (mop.hasAllocatedReg()) {
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000534 unsigned regNum = mop.getReg();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000535 if (regNum != target.getRegInfo().getZeroRegNum())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000536 regToRefVecMap[mop.getReg()]
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000537 .push_back(std::make_pair(node, i + MI.getNumOperands()));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000538 continue; // nothing more to do
539 }
540
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000541 if (mop.isDef()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000542 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000543 valueToDefVecMap[MI.getImplicitRef(i)].push_back(
544 std::make_pair(node, -i));
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000545 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000546 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000547}
548
549
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000550void SchedGraph::buildNodesForBB(const TargetMachine& target,
551 MachineBasicBlock& MBB,
552 std::vector<SchedGraphNode*>& memNodeVec,
553 std::vector<SchedGraphNode*>& callDepNodeVec,
554 RegToRefVecMap& regToRefVecMap,
555 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000556 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000557
558 // Build graph nodes for each VM instruction and gather def/use info.
559 // Do both those together in a single pass over all machine instructions.
Chris Lattnere6d04f12004-02-18 16:43:51 +0000560 unsigned i = 0;
561 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;
562 ++I, ++i)
563 if (!mii.isDummyPhiInstr(I->getOpcode())) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000564 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
Chris Lattnere6d04f12004-02-18 16:43:51 +0000565 noteGraphNodeForInstr(I, node);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000566
567 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000568 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
569 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000570 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000571}
572
573
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000574void SchedGraph::buildGraph(const TargetMachine& target) {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000575 // Use this data structure to note all machine operands that compute
576 // ordinary LLVM values. These must be computed defs (i.e., instructions).
577 // Note that there may be multiple machine instructions that define
578 // each Value.
579 ValueToDefVecMap valueToDefVecMap;
580
Vikram S. Advee64574c2001-11-08 05:20:23 +0000581 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000582 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000583 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000584
585 // Use this data structure to note all instructions that access physical
586 // registers that can be modified by a call (including call instructions)
587 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000588
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000589 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000590 // machine registers so we can add edges for those later without
591 // extra passes over the nodes.
592 // The vector holds an ordered list of references to the machine reg,
593 // ordered according to control-flow order. This only works for a
594 // single basic block, hence the assertion. Each reference is identified
595 // by the pair: <node, operand-number>.
596 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000597 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000598
599 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000600 graphRoot = new SchedGraphNode(0, NULL, -1, target);
601 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000602
603 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000604 // First add nodes for all the machine instructions in the basic block
605 // because this greatly simplifies identifying which edges to add.
606 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000607 // Also, remember the load/store instructions to add memory deps later.
608 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000609
Vikram S. Adve7952d602003-05-31 07:37:05 +0000610 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
611 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000612
613 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000614 // Now add edges for the following (all are incoming edges except (4)):
615 // (1) operands of the machine instruction, including hidden operands
616 // (2) machine register dependences
617 // (3) memory load/store dependences
618 // (3) other resource dependences for the machine instruction, if any
619 // (4) output dependences when multiple machine instructions define the
620 // same value; all must have been generated from a single VM instrn
621 // (5) control dependences to branch instructions generated for the
622 // terminator instruction of the BB. Because of delay slots and
623 // 2-way conditional branches, multiple CD edges are needed
624 // (see addCDEdges for details).
625 // Also, note any uses or defs of machine registers.
626 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000627 //----------------------------------------------------------------
628
629 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000630 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000631
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000632 // Then add memory dep edges: store->load, load->store, and store->store.
633 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000634 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000635
636 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000637 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000638
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000639 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnere6d04f12004-02-18 16:43:51 +0000640 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
641 addEdgesForInstruction(*I, valueToDefVecMap, target);
Brian Gaeke0dc57532004-02-09 18:42:05 +0000642
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000643 // Then add edges for dependences on machine registers
644 this->addMachineRegEdges(regToRefVecMap, target);
Brian Gaeke0dc57532004-02-09 18:42:05 +0000645
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000646 // Finally, add edges from the dummy root and to dummy leaf
647 this->addDummyEdges();
648}
649
650
651//
652// class SchedGraphSet
653//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000654SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000655 const TargetMachine& target) :
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000656 function(_function) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000657 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000658}
659
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000660SchedGraphSet::~SchedGraphSet() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000661 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000662 for(iterator I = begin(), E = end(); I != E; ++I)
663 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000664}
665
666
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000667void SchedGraphSet::dump() const {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000668 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000669 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000670
671 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000672 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000673
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000674 std::cerr << "\n====== End graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000675 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000676}
677
678
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000679void SchedGraphSet::buildGraphsForMethod(const Function *F,
680 const TargetMachine& target) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000681 MachineFunction &MF = MachineFunction::get(F);
682 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
683 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000684}
685
686
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000687void SchedGraphEdge::print(std::ostream &os) const {
688 os << "edge [" << src->getNodeId() << "] -> ["
689 << sink->getNodeId() << "] : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000690
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000691 switch(depType) {
692 case SchedGraphEdge::CtrlDep:
693 os<< "Control Dep";
694 break;
695 case SchedGraphEdge::ValueDep:
696 os<< "Reg Value " << val;
697 break;
698 case SchedGraphEdge::MemoryDep:
699 os<< "Memory Dep";
700 break;
701 case SchedGraphEdge::MachineRegister:
702 os<< "Reg " << machineRegNum;
703 break;
704 case SchedGraphEdge::MachineResource:
705 os<<"Resource "<< resourceId;
706 break;
707 default:
708 assert(0);
709 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000710 }
711
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000712 os << " : delay = " << minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000713}
714
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000715void SchedGraphNode::print(std::ostream &os) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000716 os << std::string(8, ' ')
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000717 << "Node " << ID << " : "
718 << "latency = " << latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000719
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000720 if (getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000721 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000722 else {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000723 os << *getMachineInstr() << "\n" << std::string(12, ' ');
724 os << inEdges.size() << " Incoming Edges:\n";
725 for (unsigned i=0, N = inEdges.size(); i < N; i++)
726 os << std::string(16, ' ') << *inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000727
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000728 os << std::string(12, ' ') << outEdges.size()
Misha Brukman6b77ec42003-05-22 21:49:18 +0000729 << " Outgoing Edges:\n";
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000730 for (unsigned i=0, N= outEdges.size(); i < N; i++)
731 os << std::string(16, ' ') << *outEdges[i];
Misha Brukman6b77ec42003-05-22 21:49:18 +0000732 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000733}
Brian Gaeked0fde302003-11-11 22:41:34 +0000734
735} // End llvm namespace