blob: 9688f3930bb01199c08956a342d3735754771267 [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"
Reid Spencer954da372004-07-04 12:19:56 +000025#include <iostream>
Vikram S. Adve78ef1392001-08-28 23:06:02 +000026
Brian Gaeked0fde302003-11-11 22:41:34 +000027namespace llvm {
28
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000029//*********************** Internal Data Structures *************************/
30
Vikram S. Advec352d2c2001-11-05 04:04:23 +000031// The following two types need to be classes, not typedefs, so we can use
32// opaque declarations in SchedGraph.h
33//
Misha Brukmanc2312df2003-05-22 21:24:35 +000034struct RefVec: public std::vector<std::pair<SchedGraphNode*, int> > {
35 typedef std::vector<std::pair<SchedGraphNode*,int> >::iterator iterator;
36 typedef
37 std::vector<std::pair<SchedGraphNode*,int> >::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000038};
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000039
Chris Lattner80c685f2001-10-13 06:51:01 +000040struct RegToRefVecMap: public hash_map<int, RefVec> {
Vikram S. Advec352d2c2001-11-05 04:04:23 +000041 typedef hash_map<int, RefVec>:: iterator iterator;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000042 typedef hash_map<int, RefVec>::const_iterator const_iterator;
43};
44
Vikram S. Adve74d15d32003-07-02 01:16:01 +000045struct ValueToDefVecMap: public hash_map<const Value*, RefVec> {
46 typedef hash_map<const Value*, RefVec>:: iterator iterator;
47 typedef hash_map<const Value*, RefVec>::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000048};
49
Vikram S. Adve78ef1392001-08-28 23:06:02 +000050
51//
52// class SchedGraphNode
53//
54
Tanya Lattnerc50ee552003-08-27 02:42:58 +000055SchedGraphNode::SchedGraphNode(unsigned NID, MachineBasicBlock *mbb,
56 int indexInBB, const TargetMachine& Target)
Chris Lattnere6d04f12004-02-18 16:43:51 +000057 : SchedGraphNodeCommon(NID,indexInBB), MBB(mbb), MI(0) {
58 if (mbb) {
59 MachineBasicBlock::iterator I = MBB->begin();
60 std::advance(I, indexInBB);
61 MI = I;
62
Brian Gaeke918cdd42004-02-12 01:34:05 +000063 MachineOpCode mopCode = MI->getOpcode();
Chris Lattner98107ff2004-06-02 06:06:20 +000064 latency = Target.getInstrInfo()->hasResultInterlock(mopCode)
65 ? Target.getInstrInfo()->minLatency(mopCode)
66 : Target.getInstrInfo()->maxLatency(mopCode);
Tanya Lattnerc50ee552003-08-27 02:42:58 +000067 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000068}
69
John Criswellc9afb492003-08-28 21:43:17 +000070//
71// Method: SchedGraphNode Destructor
72//
73// Description:
74// Free memory allocated by the SchedGraphNode object.
75//
76// Notes:
77// Do not delete the edges here. The base class will take care of that.
78// Only handle subclass specific stuff here (where currently there is
79// none).
80//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000081SchedGraphNode::~SchedGraphNode() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000082}
83
Vikram S. Adve78ef1392001-08-28 23:06:02 +000084//
85// class SchedGraph
86//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000087SchedGraph::SchedGraph(MachineBasicBlock &mbb, const TargetMachine& target)
88 : MBB(mbb) {
Chris Lattner697954c2002-01-20 22:54:45 +000089 buildGraph(target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000090}
91
John Criswellc9afb492003-08-28 21:43:17 +000092//
93// Method: SchedGraph Destructor
94//
95// Description:
96// This method deletes memory allocated by the SchedGraph object.
97//
98// Notes:
99// Do not delete the graphRoot or graphLeaf here. The base class handles
100// that bit of work.
101//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000102SchedGraph::~SchedGraph() {
Chris Lattner697954c2002-01-20 22:54:45 +0000103 for (const_iterator I = begin(); I != end(); ++I)
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000104 delete I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000105}
106
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000107void SchedGraph::dump() const {
Brian Gaeke0dc57532004-02-09 18:42:05 +0000108 std::cerr << " Sched Graph for Basic Block: "
109 << MBB.getBasicBlock()->getName()
110 << " (" << MBB.getBasicBlock() << ")"
111 << "\n\n Actual Root nodes: ";
112 for (SchedGraphNodeCommon::const_iterator I = graphRoot->beginOutEdges(),
113 E = graphRoot->endOutEdges();
114 I != E; ++I) {
115 std::cerr << (*I)->getSink ()->getNodeId ();
116 if (I + 1 != E) { std::cerr << ", "; }
117 }
Misha Brukmanc2312df2003-05-22 21:24:35 +0000118 std::cerr << "\n Graph Nodes:\n";
Brian Gaeke0dc57532004-02-09 18:42:05 +0000119 for (const_iterator I = begin(), E = end(); I != E; ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000120 std::cerr << "\n" << *I->second;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000121 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000122}
123
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000124void SchedGraph::addDummyEdges() {
Brian Gaeke0dc57532004-02-09 18:42:05 +0000125 assert(graphRoot->getNumOutEdges() == 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000126
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000127 for (const_iterator I=begin(); I != end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000128 SchedGraphNode* node = (*I).second;
129 assert(node != graphRoot && node != graphLeaf);
130 if (node->beginInEdges() == node->endInEdges())
131 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
132 SchedGraphEdge::NonDataDep, 0);
133 if (node->beginOutEdges() == node->endOutEdges())
134 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
135 SchedGraphEdge::NonDataDep, 0);
136 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000137}
138
139
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000140void SchedGraph::addCDEdges(const TerminatorInst* term,
141 const TargetMachine& target) {
Chris Lattner98107ff2004-06-02 06:06:20 +0000142 const TargetInstrInfo& mii = *target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000143 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000144
145 // Find the first branch instr in the sequence of machine instrs for term
146 //
147 unsigned first = 0;
Brian Gaeke918cdd42004-02-12 01:34:05 +0000148 while (! mii.isBranch(termMvec[first]->getOpcode()) &&
149 ! mii.isReturn(termMvec[first]->getOpcode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000150 ++first;
151 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000152 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000153 if (first == termMvec.size())
154 return;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000155
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000156 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000157
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000158 // Add CD edges from each instruction in the sequence to the
159 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000160 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000161 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000162 for (unsigned i = termMvec.size(); i > first+1; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000163 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
164 assert(toNode && "No node for instr generated for branch/ret?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000165
Misha Brukman6b77ec42003-05-22 21:49:18 +0000166 for (unsigned j = i-1; j != 0; --j)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000167 if (mii.isBranch(termMvec[j-1]->getOpcode()) ||
168 mii.isReturn(termMvec[j-1]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000169 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
170 assert(brNode && "No node for instr generated for branch/ret?");
171 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
172 SchedGraphEdge::NonDataDep, 0);
173 break; // only one incoming edge is enough
174 }
175 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000176
177 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000178 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000179 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000180 for (unsigned i = first; i != 0; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000181 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
182 assert(fromNode && "No node for instr generated for branch?");
183 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
184 SchedGraphEdge::NonDataDep, 0);
185 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000186
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000187 // Now add CD edges to the first branch instruction in the sequence from
188 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000189 //
Chris Lattnere6d04f12004-02-18 16:43:51 +0000190 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
191 if (&*I == termMvec[first]) // reached the first branch
Misha Brukman6b77ec42003-05-22 21:49:18 +0000192 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000193
Chris Lattnere6d04f12004-02-18 16:43:51 +0000194 SchedGraphNode* fromNode = getGraphNodeForInstr(I);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000195 if (fromNode == NULL)
196 continue; // dummy instruction, e.g., PHI
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000197
Misha Brukman6b77ec42003-05-22 21:49:18 +0000198 (void) new SchedGraphEdge(fromNode, firstBrNode,
199 SchedGraphEdge::CtrlDep,
200 SchedGraphEdge::NonDataDep, 0);
201
202 // If we find any other machine instructions (other than due to
203 // the terminator) that also have delay slots, add an outgoing edge
204 // from the instruction to the instructions in the delay slots.
205 //
Chris Lattnere6d04f12004-02-18 16:43:51 +0000206 unsigned d = mii.getNumDelaySlots(I->getOpcode());
207
208 MachineBasicBlock::iterator J = I; ++J;
209 for (unsigned j=1; j <= d; j++, ++J) {
210 SchedGraphNode* toNode = this->getGraphNodeForInstr(J);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000211 assert(toNode && "No node for machine instr in delay slot?");
212 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000213 SchedGraphEdge::CtrlDep,
214 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000215 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000216 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000217}
218
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000219static const int SG_LOAD_REF = 0;
220static const int SG_STORE_REF = 1;
221static const int SG_CALL_REF = 2;
222
223static const unsigned int SG_DepOrderArray[][3] = {
224 { SchedGraphEdge::NonDataDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000225 SchedGraphEdge::AntiDep,
226 SchedGraphEdge::AntiDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000227 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000228 SchedGraphEdge::OutputDep,
229 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000230 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000231 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
232 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
233 | SchedGraphEdge::OutputDep }
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000234};
235
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000236
Vikram S. Advee64574c2001-11-08 05:20:23 +0000237// Add a dependence edge between every pair of machine load/store/call
238// instructions, where at least one is a store or a call.
239// Use latency 1 just to ensure that memory operations are ordered;
240// latency does not otherwise matter (true dependences enforce that).
241//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000242void SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
243 const TargetMachine& target) {
Chris Lattner98107ff2004-06-02 06:06:20 +0000244 const TargetInstrInfo& mii = *target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000245
Vikram S. Advee64574c2001-11-08 05:20:23 +0000246 // Instructions in memNodeVec are in execution order within the basic block,
247 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
248 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000249 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000250 MachineOpCode fromOpCode = memNodeVec[im]->getOpcode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000251 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
252 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
253 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000254 for (unsigned jm=im+1; jm < NM; jm++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000255 MachineOpCode toOpCode = memNodeVec[jm]->getOpcode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000256 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
257 : (mii.isLoad(toOpCode)? SG_LOAD_REF
258 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000259
Misha Brukman6b77ec42003-05-22 21:49:18 +0000260 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
261 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
262 SchedGraphEdge::MemoryDep,
263 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000264 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000265 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000266}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000267
Vikram S. Advee64574c2001-11-08 05:20:23 +0000268// Add edges from/to CC reg instrs to/from call instrs.
269// Essentially this prevents anything that sets or uses a CC reg from being
270// reordered w.r.t. a call.
271// Use a latency of 0 because we only need to prevent out-of-order issue,
272// like with control dependences.
273//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000274void SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
275 const TargetMachine& target) {
Chris Lattner98107ff2004-06-02 06:06:20 +0000276 const TargetInstrInfo& mii = *target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000277
Vikram S. Adve7952d602003-05-31 07:37:05 +0000278 // Instructions in memNodeVec are in execution order within the basic block,
279 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
280 //
281 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000282 if (mii.isCall(callDepNodeVec[ic]->getOpcode())) {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000283 // Add SG_CALL_REF edges from all preds to this instruction.
284 for (unsigned jc=0; jc < ic; jc++)
285 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
286 SchedGraphEdge::MachineRegister,
287 MachineIntRegsRID, 0);
288
289 // And do the same from this instruction to all successors.
290 for (unsigned jc=ic+1; jc < NC; jc++)
291 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
292 SchedGraphEdge::MachineRegister,
293 MachineIntRegsRID, 0);
294 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000295
296#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000297 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000298 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000299 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000300 if (mii.isCall(memNodeVec[im]->getOpcode()))
Vikram S. Advee64574c2001-11-08 05:20:23 +0000301 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000302
Vikram S. Advee64574c2001-11-08 05:20:23 +0000303 // Now walk the entire basic block, looking for CC instructions *and*
304 // call instructions, and keep track of the order of the instructions.
305 // Use the call node vec to quickly find earlier and later call nodes
306 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000307 //
308 int lastCallNodeIdx = -1;
309 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000310 if (mii.isCall(bbMvec[i]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000311 ++lastCallNodeIdx;
312 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
313 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
314 break;
315 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000316 }
Brian Gaeke918cdd42004-02-12 01:34:05 +0000317 else if (mii.isCCInstr(bbMvec[i]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000318 // Add incoming/outgoing edges from/to preceding/later calls
319 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
320 int j=0;
321 for ( ; j <= lastCallNodeIdx; j++)
322 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
323 MachineCCRegsRID, 0);
324 for ( ; j < (int) callNodeVec.size(); j++)
325 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
326 MachineCCRegsRID, 0);
327 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000328#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000329}
330
331
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000332void SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
333 const TargetMachine& target) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000334 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000335 // not aliased!
336 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000337 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000338 I != regToRefVecMap.end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000339 int regNum = (*I).first;
340 RefVec& regRefVec = (*I).second;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000341
Misha Brukman6b77ec42003-05-22 21:49:18 +0000342 // regRefVec is ordered by control flow order in the basic block
343 for (unsigned i=0; i < regRefVec.size(); ++i) {
344 SchedGraphNode* node = regRefVec[i].first;
345 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000346 const MachineOperand& mop =
347 node->getMachineInstr()->getExplOrImplOperand(opNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000348 bool isDef = mop.isDef() && !mop.isUse();
349 bool isDefAndUse = mop.isDef() && mop.isUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000350
Misha Brukman6b77ec42003-05-22 21:49:18 +0000351 for (unsigned p=0; p < i; ++p) {
352 SchedGraphNode* prevNode = regRefVec[p].first;
353 if (prevNode != node) {
354 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000355 const MachineOperand& prevMop =
356 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000357 bool prevIsDef = prevMop.isDef() && !prevMop.isUse();
358 bool prevIsDefAndUse = prevMop.isDef() && prevMop.isUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000359 if (isDef) {
360 if (prevIsDef)
361 new SchedGraphEdge(prevNode, node, regNum,
362 SchedGraphEdge::OutputDep);
363 if (!prevIsDef || prevIsDefAndUse)
364 new SchedGraphEdge(prevNode, node, regNum,
365 SchedGraphEdge::AntiDep);
366 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000367
Misha Brukman6b77ec42003-05-22 21:49:18 +0000368 if (prevIsDef)
369 if (!isDef || isDefAndUse)
370 new SchedGraphEdge(prevNode, node, regNum,
371 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000372 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000373 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000374 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000375 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000376}
377
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000378
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000379// Adds dependences to/from refNode from/to all other defs
380// in the basic block. refNode may be a use, a def, or both.
381// We do not consider other uses because we are not building use-use deps.
382//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000383void SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
384 const RefVec& defVec,
385 const Value* defValue,
386 bool refNodeIsDef,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000387 bool refNodeIsUse,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000388 const TargetMachine& target) {
Vikram S. Adve200a4352001-11-12 18:53:43 +0000389 // Add true or output dep edges from all def nodes before refNode in BB.
390 // Add anti or output dep edges to all def nodes after refNode.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000391 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000392 if ((*I).first == refNode)
393 continue; // Dont add any self-loops
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000394
Misha Brukman6b77ec42003-05-22 21:49:18 +0000395 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB()) {
396 // (*).first is before refNode
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000397 if (refNodeIsDef && !refNodeIsUse)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000398 (void) new SchedGraphEdge((*I).first, refNode, defValue,
399 SchedGraphEdge::OutputDep);
400 if (refNodeIsUse)
401 (void) new SchedGraphEdge((*I).first, refNode, defValue,
402 SchedGraphEdge::TrueDep);
403 } else {
404 // (*).first is after refNode
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000405 if (refNodeIsDef && !refNodeIsUse)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000406 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
407 SchedGraphEdge::OutputDep);
408 if (refNodeIsUse)
409 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
410 SchedGraphEdge::AntiDep);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000411 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000412 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000413}
414
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000415
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000416void SchedGraph::addEdgesForInstruction(const MachineInstr& MI,
417 const ValueToDefVecMap& valueToDefVecMap,
418 const TargetMachine& target) {
Chris Lattner133f0792002-10-28 04:45:29 +0000419 SchedGraphNode* node = getGraphNodeForInstr(&MI);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000420 if (node == NULL)
421 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000422
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000423 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000424 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000425 for (unsigned i = 0, numOps = MI.getNumOperands(); i != numOps; ++i) {
426 switch (MI.getOperand(i).getType()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000427 case MachineOperand::MO_VirtualRegister:
428 case MachineOperand::MO_CCRegister:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000429 if (const Value* srcI = MI.getOperand(i).getVRegValue()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000430 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
431 if (I != valueToDefVecMap.end())
432 addEdgesForValue(node, I->second, srcI,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000433 MI.getOperand(i).isDef(), MI.getOperand(i).isUse(),
434 target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000435 }
436 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000437
Misha Brukman6b77ec42003-05-22 21:49:18 +0000438 case MachineOperand::MO_MachineRegister:
439 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000440
Misha Brukman6b77ec42003-05-22 21:49:18 +0000441 case MachineOperand::MO_SignExtendedImmed:
442 case MachineOperand::MO_UnextendedImmed:
443 case MachineOperand::MO_PCRelativeDisp:
Misha Brukmane2bf0a22003-11-06 00:04:11 +0000444 case MachineOperand::MO_ConstantPoolIndex:
Misha Brukman6b77ec42003-05-22 21:49:18 +0000445 break; // nothing to do for immediate fields
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000446
Misha Brukman6b77ec42003-05-22 21:49:18 +0000447 default:
448 assert(0 && "Unknown machine operand type in SchedGraph builder");
449 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000450 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000451 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000452
453 // Add edges for values implicitly used by the machine instruction.
454 // Examples include function arguments to a Call instructions or the return
455 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000456 //
Chris Lattner133f0792002-10-28 04:45:29 +0000457 for (unsigned i=0, N=MI.getNumImplicitRefs(); i < N; ++i)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000458 if (MI.getImplicitOp(i).isUse())
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000459 if (const Value* srcI = MI.getImplicitRef(i)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000460 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
461 if (I != valueToDefVecMap.end())
462 addEdgesForValue(node, I->second, srcI,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000463 MI.getImplicitOp(i).isDef(),
464 MI.getImplicitOp(i).isUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000465 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000466}
467
468
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000469void SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
470 SchedGraphNode* node,
471 std::vector<SchedGraphNode*>& memNodeVec,
472 std::vector<SchedGraphNode*>& callDepNodeVec,
473 RegToRefVecMap& regToRefVecMap,
474 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner98107ff2004-06-02 06:06:20 +0000475 const TargetInstrInfo& mii = *target.getInstrInfo();
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000476
Brian Gaeke918cdd42004-02-12 01:34:05 +0000477 MachineOpCode opCode = node->getOpcode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000478
Vikram S. Adve7952d602003-05-31 07:37:05 +0000479 if (mii.isCall(opCode) || mii.isCCInstr(opCode))
480 callDepNodeVec.push_back(node);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000481
Vikram S. Advee64574c2001-11-08 05:20:23 +0000482 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
483 memNodeVec.push_back(node);
484
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000485 // Collect the register references and value defs. for explicit operands
486 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000487 const MachineInstr& MI = *node->getMachineInstr();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000488 for (int i=0, numOps = (int) MI.getNumOperands(); i < numOps; i++) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000489 const MachineOperand& mop = MI.getOperand(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000490
Misha Brukman6b77ec42003-05-22 21:49:18 +0000491 // if this references a register other than the hardwired
492 // "zero" register, record the reference.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000493 if (mop.hasAllocatedReg()) {
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000494 unsigned regNum = mop.getReg();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000495
Vikram S. Adve7952d602003-05-31 07:37:05 +0000496 // If this is not a dummy zero register, record the reference in order
Chris Lattner98107ff2004-06-02 06:06:20 +0000497 if (regNum != target.getRegInfo()->getZeroRegNum())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000498 regToRefVecMap[mop.getReg()]
Misha Brukman6b77ec42003-05-22 21:49:18 +0000499 .push_back(std::make_pair(node, i));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000500
501 // If this is a volatile register, add the instruction to callDepVec
502 // (only if the node is not already on the callDepVec!)
503 if (callDepNodeVec.size() == 0 || callDepNodeVec.back() != node)
504 {
505 unsigned rcid;
Chris Lattner98107ff2004-06-02 06:06:20 +0000506 int regInClass = target.getRegInfo()->getClassRegNum(regNum, rcid);
507 if (target.getRegInfo()->getMachineRegClass(rcid)
Vikram S. Adve7952d602003-05-31 07:37:05 +0000508 ->isRegVolatile(regInClass))
509 callDepNodeVec.push_back(node);
510 }
511
Misha Brukman6b77ec42003-05-22 21:49:18 +0000512 continue; // nothing more to do
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000513 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000514
Misha Brukman6b77ec42003-05-22 21:49:18 +0000515 // ignore all other non-def operands
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000516 if (!MI.getOperand(i).isDef())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000517 continue;
518
519 // We must be defining a value.
520 assert((mop.getType() == MachineOperand::MO_VirtualRegister ||
521 mop.getType() == MachineOperand::MO_CCRegister)
522 && "Do not expect any other kind of operand to be defined!");
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000523 assert(mop.getVRegValue() != NULL && "Null value being defined?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000524
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000525 valueToDefVecMap[mop.getVRegValue()].push_back(std::make_pair(node, i));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000526 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000527
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000528 //
Vikram S. Adve7952d602003-05-31 07:37:05 +0000529 // Collect value defs. for implicit operands. They may have allocated
530 // physical registers also.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000531 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000532 for (unsigned i=0, N = MI.getNumImplicitRefs(); i != N; ++i) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000533 const MachineOperand& mop = MI.getImplicitOp(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000534 if (mop.hasAllocatedReg()) {
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000535 unsigned regNum = mop.getReg();
Chris Lattner98107ff2004-06-02 06:06:20 +0000536 if (regNum != target.getRegInfo()->getZeroRegNum())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000537 regToRefVecMap[mop.getReg()]
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000538 .push_back(std::make_pair(node, i + MI.getNumOperands()));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000539 continue; // nothing more to do
540 }
541
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000542 if (mop.isDef()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000543 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000544 valueToDefVecMap[MI.getImplicitRef(i)].push_back(
545 std::make_pair(node, -i));
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000546 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000547 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000548}
549
550
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000551void SchedGraph::buildNodesForBB(const TargetMachine& target,
552 MachineBasicBlock& MBB,
553 std::vector<SchedGraphNode*>& memNodeVec,
554 std::vector<SchedGraphNode*>& callDepNodeVec,
555 RegToRefVecMap& regToRefVecMap,
556 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner98107ff2004-06-02 06:06:20 +0000557 const TargetInstrInfo& mii = *target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000558
559 // Build graph nodes for each VM instruction and gather def/use info.
560 // Do both those together in a single pass over all machine instructions.
Chris Lattnere6d04f12004-02-18 16:43:51 +0000561 unsigned i = 0;
562 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;
563 ++I, ++i)
564 if (!mii.isDummyPhiInstr(I->getOpcode())) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000565 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
Chris Lattnere6d04f12004-02-18 16:43:51 +0000566 noteGraphNodeForInstr(I, node);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000567
568 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000569 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
570 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000571 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000572}
573
574
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000575void SchedGraph::buildGraph(const TargetMachine& target) {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000576 // Use this data structure to note all machine operands that compute
577 // ordinary LLVM values. These must be computed defs (i.e., instructions).
578 // Note that there may be multiple machine instructions that define
579 // each Value.
580 ValueToDefVecMap valueToDefVecMap;
581
Vikram S. Advee64574c2001-11-08 05:20:23 +0000582 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000583 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000584 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000585
586 // Use this data structure to note all instructions that access physical
587 // registers that can be modified by a call (including call instructions)
588 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000589
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000590 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000591 // machine registers so we can add edges for those later without
592 // extra passes over the nodes.
593 // The vector holds an ordered list of references to the machine reg,
594 // ordered according to control-flow order. This only works for a
595 // single basic block, hence the assertion. Each reference is identified
596 // by the pair: <node, operand-number>.
597 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000598 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000599
600 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000601 graphRoot = new SchedGraphNode(0, NULL, -1, target);
602 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000603
604 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000605 // First add nodes for all the machine instructions in the basic block
606 // because this greatly simplifies identifying which edges to add.
607 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000608 // Also, remember the load/store instructions to add memory deps later.
609 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000610
Vikram S. Adve7952d602003-05-31 07:37:05 +0000611 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
612 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000613
614 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000615 // Now add edges for the following (all are incoming edges except (4)):
616 // (1) operands of the machine instruction, including hidden operands
617 // (2) machine register dependences
618 // (3) memory load/store dependences
619 // (3) other resource dependences for the machine instruction, if any
620 // (4) output dependences when multiple machine instructions define the
621 // same value; all must have been generated from a single VM instrn
622 // (5) control dependences to branch instructions generated for the
623 // terminator instruction of the BB. Because of delay slots and
624 // 2-way conditional branches, multiple CD edges are needed
625 // (see addCDEdges for details).
626 // Also, note any uses or defs of machine registers.
627 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000628 //----------------------------------------------------------------
629
630 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000631 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000632
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000633 // Then add memory dep edges: store->load, load->store, and store->store.
634 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000635 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000636
637 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000638 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000639
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000640 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnere6d04f12004-02-18 16:43:51 +0000641 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
642 addEdgesForInstruction(*I, valueToDefVecMap, target);
Brian Gaeke0dc57532004-02-09 18:42:05 +0000643
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000644 // Then add edges for dependences on machine registers
645 this->addMachineRegEdges(regToRefVecMap, target);
Brian Gaeke0dc57532004-02-09 18:42:05 +0000646
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000647 // Finally, add edges from the dummy root and to dummy leaf
648 this->addDummyEdges();
649}
650
651
652//
653// class SchedGraphSet
654//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000655SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000656 const TargetMachine& target) :
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000657 function(_function) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000658 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000659}
660
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000661SchedGraphSet::~SchedGraphSet() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000662 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000663 for(iterator I = begin(), E = end(); I != E; ++I)
664 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000665}
666
667
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000668void SchedGraphSet::dump() const {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000669 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000670 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000671
672 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000673 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000674
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000675 std::cerr << "\n====== End graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000676 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000677}
678
679
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000680void SchedGraphSet::buildGraphsForMethod(const Function *F,
681 const TargetMachine& target) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000682 MachineFunction &MF = MachineFunction::get(F);
683 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
684 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000685}
686
687
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000688void SchedGraphEdge::print(std::ostream &os) const {
689 os << "edge [" << src->getNodeId() << "] -> ["
690 << sink->getNodeId() << "] : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000691
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000692 switch(depType) {
693 case SchedGraphEdge::CtrlDep:
694 os<< "Control Dep";
695 break;
696 case SchedGraphEdge::ValueDep:
697 os<< "Reg Value " << val;
698 break;
699 case SchedGraphEdge::MemoryDep:
700 os<< "Memory Dep";
701 break;
702 case SchedGraphEdge::MachineRegister:
703 os<< "Reg " << machineRegNum;
704 break;
705 case SchedGraphEdge::MachineResource:
706 os<<"Resource "<< resourceId;
707 break;
708 default:
709 assert(0);
710 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000711 }
712
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000713 os << " : delay = " << minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000714}
715
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000716void SchedGraphNode::print(std::ostream &os) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000717 os << std::string(8, ' ')
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000718 << "Node " << ID << " : "
719 << "latency = " << latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000720
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000721 if (getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000722 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000723 else {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000724 os << *getMachineInstr() << "\n" << std::string(12, ' ');
725 os << inEdges.size() << " Incoming Edges:\n";
726 for (unsigned i=0, N = inEdges.size(); i < N; i++)
727 os << std::string(16, ' ') << *inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000728
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000729 os << std::string(12, ' ') << outEdges.size()
Misha Brukman6b77ec42003-05-22 21:49:18 +0000730 << " Outgoing Edges:\n";
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000731 for (unsigned i=0, N= outEdges.size(); i < N; i++)
732 os << std::string(16, ' ') << *outEdges[i];
Misha Brukman6b77ec42003-05-22 21:49:18 +0000733 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000734}
Brian Gaeked0fde302003-11-11 22:41:34 +0000735
736} // End llvm namespace