blob: 3a8088043bf75f0346ccb1ce560d44b22dd9e80f [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
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)
Tanya Lattner8dc99822003-08-28 15:30:40 +000056 : SchedGraphNodeCommon(NID,indexInBB), MBB(mbb), MI(mbb ? (*mbb)[indexInBB] : 0) {
Tanya Lattnerc50ee552003-08-27 02:42:58 +000057 if (MI) {
58 MachineOpCode mopCode = MI->getOpCode();
59 latency = Target.getInstrInfo().hasResultInterlock(mopCode)
60 ? Target.getInstrInfo().minLatency(mopCode)
61 : Target.getInstrInfo().maxLatency(mopCode);
62 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000063}
64
John Criswellc9afb492003-08-28 21:43:17 +000065//
66// Method: SchedGraphNode Destructor
67//
68// Description:
69// Free memory allocated by the SchedGraphNode object.
70//
71// Notes:
72// Do not delete the edges here. The base class will take care of that.
73// Only handle subclass specific stuff here (where currently there is
74// none).
75//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000076SchedGraphNode::~SchedGraphNode() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000077}
78
Vikram S. Adve78ef1392001-08-28 23:06:02 +000079//
80// class SchedGraph
81//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000082SchedGraph::SchedGraph(MachineBasicBlock &mbb, const TargetMachine& target)
83 : MBB(mbb) {
Chris Lattner697954c2002-01-20 22:54:45 +000084 buildGraph(target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000085}
86
John Criswellc9afb492003-08-28 21:43:17 +000087//
88// Method: SchedGraph Destructor
89//
90// Description:
91// This method deletes memory allocated by the SchedGraph object.
92//
93// Notes:
94// Do not delete the graphRoot or graphLeaf here. The base class handles
95// that bit of work.
96//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000097SchedGraph::~SchedGraph() {
Chris Lattner697954c2002-01-20 22:54:45 +000098 for (const_iterator I = begin(); I != end(); ++I)
Chris Lattnerf3dd05c2002-04-09 05:15:33 +000099 delete I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000100}
101
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000102void SchedGraph::dump() const {
Misha Brukmanc2312df2003-05-22 21:24:35 +0000103 std::cerr << " Sched Graph for Basic Block: ";
104 std::cerr << MBB.getBasicBlock()->getName()
105 << " (" << MBB.getBasicBlock() << ")";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000106
Misha Brukmanc2312df2003-05-22 21:24:35 +0000107 std::cerr << "\n\n Actual Root nodes : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000108 for (unsigned i=0, N=graphRoot->outEdges.size(); i < N; i++)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000109 std::cerr << graphRoot->outEdges[i]->getSink()->getNodeId()
110 << ((i == N-1)? "" : ", ");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000111
Misha Brukmanc2312df2003-05-22 21:24:35 +0000112 std::cerr << "\n Graph Nodes:\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000113 for (const_iterator I=begin(); I != end(); ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000114 std::cerr << "\n" << *I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000115
Misha Brukmanc2312df2003-05-22 21:24:35 +0000116 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000117}
118
119
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000120
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000121void SchedGraph::addDummyEdges() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000122 assert(graphRoot->outEdges.size() == 0);
123
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000124 for (const_iterator I=begin(); I != end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000125 SchedGraphNode* node = (*I).second;
126 assert(node != graphRoot && node != graphLeaf);
127 if (node->beginInEdges() == node->endInEdges())
128 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
129 SchedGraphEdge::NonDataDep, 0);
130 if (node->beginOutEdges() == node->endOutEdges())
131 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
132 SchedGraphEdge::NonDataDep, 0);
133 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000134}
135
136
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000137void SchedGraph::addCDEdges(const TerminatorInst* term,
138 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000139 const TargetInstrInfo& mii = target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000140 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000141
142 // Find the first branch instr in the sequence of machine instrs for term
143 //
144 unsigned first = 0;
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000145 while (! mii.isBranch(termMvec[first]->getOpCode()) &&
146 ! mii.isReturn(termMvec[first]->getOpCode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000147 ++first;
148 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000149 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000150 if (first == termMvec.size())
151 return;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000152
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000153 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000154
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000155 // Add CD edges from each instruction in the sequence to the
156 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000157 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000158 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000159 for (unsigned i = termMvec.size(); i > first+1; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000160 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
161 assert(toNode && "No node for instr generated for branch/ret?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000162
Misha Brukman6b77ec42003-05-22 21:49:18 +0000163 for (unsigned j = i-1; j != 0; --j)
164 if (mii.isBranch(termMvec[j-1]->getOpCode()) ||
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000165 mii.isReturn(termMvec[j-1]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000166 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
167 assert(brNode && "No node for instr generated for branch/ret?");
168 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
169 SchedGraphEdge::NonDataDep, 0);
170 break; // only one incoming edge is enough
171 }
172 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000173
174 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000175 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000176 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000177 for (unsigned i = first; i != 0; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000178 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
179 assert(fromNode && "No node for instr generated for branch?");
180 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
181 SchedGraphEdge::NonDataDep, 0);
182 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000183
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000184 // Now add CD edges to the first branch instruction in the sequence from
185 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000186 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000187 for (unsigned i=0, N=MBB.size(); i < N; i++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000188 if (MBB[i] == termMvec[first]) // reached the first branch
189 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000190
Misha Brukman6b77ec42003-05-22 21:49:18 +0000191 SchedGraphNode* fromNode = this->getGraphNodeForInstr(MBB[i]);
192 if (fromNode == NULL)
193 continue; // dummy instruction, e.g., PHI
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000194
Misha Brukman6b77ec42003-05-22 21:49:18 +0000195 (void) new SchedGraphEdge(fromNode, firstBrNode,
196 SchedGraphEdge::CtrlDep,
197 SchedGraphEdge::NonDataDep, 0);
198
199 // If we find any other machine instructions (other than due to
200 // the terminator) that also have delay slots, add an outgoing edge
201 // from the instruction to the instructions in the delay slots.
202 //
203 unsigned d = mii.getNumDelaySlots(MBB[i]->getOpCode());
204 assert(i+d < N && "Insufficient delay slots for instruction?");
205
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000206 for (unsigned j=1; j <= d; j++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000207 SchedGraphNode* toNode = this->getGraphNodeForInstr(MBB[i+j]);
208 assert(toNode && "No node for machine instr in delay slot?");
209 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000210 SchedGraphEdge::CtrlDep,
211 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000212 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000213 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000214}
215
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000216static const int SG_LOAD_REF = 0;
217static const int SG_STORE_REF = 1;
218static const int SG_CALL_REF = 2;
219
220static const unsigned int SG_DepOrderArray[][3] = {
221 { SchedGraphEdge::NonDataDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000222 SchedGraphEdge::AntiDep,
223 SchedGraphEdge::AntiDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000224 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000225 SchedGraphEdge::OutputDep,
226 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000227 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000228 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
229 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
230 | SchedGraphEdge::OutputDep }
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000231};
232
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000233
Vikram S. Advee64574c2001-11-08 05:20:23 +0000234// Add a dependence edge between every pair of machine load/store/call
235// instructions, where at least one is a store or a call.
236// Use latency 1 just to ensure that memory operations are ordered;
237// latency does not otherwise matter (true dependences enforce that).
238//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000239void SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
240 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000241 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000242
Vikram S. Advee64574c2001-11-08 05:20:23 +0000243 // Instructions in memNodeVec are in execution order within the basic block,
244 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
245 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000246 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000247 MachineOpCode fromOpCode = memNodeVec[im]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000248 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
249 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
250 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000251 for (unsigned jm=im+1; jm < NM; jm++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000252 MachineOpCode toOpCode = memNodeVec[jm]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000253 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
254 : (mii.isLoad(toOpCode)? SG_LOAD_REF
255 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000256
Misha Brukman6b77ec42003-05-22 21:49:18 +0000257 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
258 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
259 SchedGraphEdge::MemoryDep,
260 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000261 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000262 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000263}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000264
Vikram S. Advee64574c2001-11-08 05:20:23 +0000265// Add edges from/to CC reg instrs to/from call instrs.
266// Essentially this prevents anything that sets or uses a CC reg from being
267// reordered w.r.t. a call.
268// Use a latency of 0 because we only need to prevent out-of-order issue,
269// like with control dependences.
270//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000271void SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
272 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000273 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000274
Vikram S. Adve7952d602003-05-31 07:37:05 +0000275 // Instructions in memNodeVec are in execution order within the basic block,
276 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
277 //
278 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000279 if (mii.isCall(callDepNodeVec[ic]->getOpCode())) {
280 // Add SG_CALL_REF edges from all preds to this instruction.
281 for (unsigned jc=0; jc < ic; jc++)
282 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
283 SchedGraphEdge::MachineRegister,
284 MachineIntRegsRID, 0);
285
286 // And do the same from this instruction to all successors.
287 for (unsigned jc=ic+1; jc < NC; jc++)
288 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
289 SchedGraphEdge::MachineRegister,
290 MachineIntRegsRID, 0);
291 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000292
293#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000294 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000295 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000296 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
297 if (mii.isCall(memNodeVec[im]->getOpCode()))
298 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000299
Vikram S. Advee64574c2001-11-08 05:20:23 +0000300 // Now walk the entire basic block, looking for CC instructions *and*
301 // call instructions, and keep track of the order of the instructions.
302 // Use the call node vec to quickly find earlier and later call nodes
303 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000304 //
305 int lastCallNodeIdx = -1;
306 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000307 if (mii.isCall(bbMvec[i]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000308 ++lastCallNodeIdx;
309 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
310 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
311 break;
312 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000313 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000314 else if (mii.isCCInstr(bbMvec[i]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000315 // Add incoming/outgoing edges from/to preceding/later calls
316 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
317 int j=0;
318 for ( ; j <= lastCallNodeIdx; j++)
319 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
320 MachineCCRegsRID, 0);
321 for ( ; j < (int) callNodeVec.size(); j++)
322 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
323 MachineCCRegsRID, 0);
324 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000325#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000326}
327
328
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000329void SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
330 const TargetMachine& target) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000331 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000332 // not aliased!
333 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000334 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000335 I != regToRefVecMap.end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000336 int regNum = (*I).first;
337 RefVec& regRefVec = (*I).second;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000338
Misha Brukman6b77ec42003-05-22 21:49:18 +0000339 // regRefVec is ordered by control flow order in the basic block
340 for (unsigned i=0; i < regRefVec.size(); ++i) {
341 SchedGraphNode* node = regRefVec[i].first;
342 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000343 const MachineOperand& mop =
344 node->getMachineInstr()->getExplOrImplOperand(opNum);
345 bool isDef = mop.opIsDefOnly();
346 bool isDefAndUse = mop.opIsDefAndUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000347
Misha Brukman6b77ec42003-05-22 21:49:18 +0000348 for (unsigned p=0; p < i; ++p) {
349 SchedGraphNode* prevNode = regRefVec[p].first;
350 if (prevNode != node) {
351 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000352 const MachineOperand& prevMop =
353 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
354 bool prevIsDef = prevMop.opIsDefOnly();
355 bool prevIsDefAndUse = prevMop.opIsDefAndUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000356 if (isDef) {
357 if (prevIsDef)
358 new SchedGraphEdge(prevNode, node, regNum,
359 SchedGraphEdge::OutputDep);
360 if (!prevIsDef || prevIsDefAndUse)
361 new SchedGraphEdge(prevNode, node, regNum,
362 SchedGraphEdge::AntiDep);
363 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000364
Misha Brukman6b77ec42003-05-22 21:49:18 +0000365 if (prevIsDef)
366 if (!isDef || isDefAndUse)
367 new SchedGraphEdge(prevNode, node, regNum,
368 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000369 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000370 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000371 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000372 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000373}
374
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000375
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000376// Adds dependences to/from refNode from/to all other defs
377// in the basic block. refNode may be a use, a def, or both.
378// We do not consider other uses because we are not building use-use deps.
379//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000380void SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
381 const RefVec& defVec,
382 const Value* defValue,
383 bool refNodeIsDef,
384 bool refNodeIsDefAndUse,
385 const TargetMachine& target) {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000386 bool refNodeIsUse = !refNodeIsDef || refNodeIsDefAndUse;
387
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
396 if (refNodeIsDef)
397 (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
404 if (refNodeIsDef)
405 (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,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000432 MI.getOperand(i).opIsDefOnly(),
433 MI.getOperand(i).opIsDefAndUse(), 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)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000457 if (MI.getImplicitOp(i).opIsUse() || MI.getImplicitOp(i).opIsDefAndUse())
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,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000462 MI.getImplicitOp(i).opIsDefOnly(),
463 MI.getImplicitOp(i).opIsDefAndUse(), 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
Vikram S. Advee64574c2001-11-08 05:20:23 +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()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000493 int regNum = mop.getAllocatedRegNum();
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())
Vikram S. Adve7952d602003-05-31 07:37:05 +0000497 regToRefVecMap[mop.getAllocatedRegNum()]
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
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000515 if (!MI.getOperand(i).opIsDefOnly() &&
516 !MI.getOperand(i).opIsDefAndUse())
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()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000535 int regNum = mop.getAllocatedRegNum();
536 if (regNum != target.getRegInfo().getZeroRegNum())
537 regToRefVecMap[mop.getAllocatedRegNum()]
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
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000542 if (mop.opIsDefOnly() || mop.opIsDefAndUse()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000543 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
544 valueToDefVecMap[MI.getImplicitRef(i)].push_back(std::make_pair(node,
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000545 -i));
546 }
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 Lattner3501fea2003-01-14 22:00:31 +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 Lattnerfb3a0aed2002-10-28 18:50:08 +0000561 for (unsigned i=0; i < MBB.size(); i++)
562 if (!mii.isDummyPhiInstr(MBB[i]->getOpCode())) {
563 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
564 noteGraphNodeForInstr(MBB[i], node);
565
566 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000567 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
568 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000569 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000570}
571
572
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000573void SchedGraph::buildGraph(const TargetMachine& target) {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000574 // Use this data structure to note all machine operands that compute
575 // ordinary LLVM values. These must be computed defs (i.e., instructions).
576 // Note that there may be multiple machine instructions that define
577 // each Value.
578 ValueToDefVecMap valueToDefVecMap;
579
Vikram S. Advee64574c2001-11-08 05:20:23 +0000580 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000581 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000582 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000583
584 // Use this data structure to note all instructions that access physical
585 // registers that can be modified by a call (including call instructions)
586 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000587
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000588 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000589 // machine registers so we can add edges for those later without
590 // extra passes over the nodes.
591 // The vector holds an ordered list of references to the machine reg,
592 // ordered according to control-flow order. This only works for a
593 // single basic block, hence the assertion. Each reference is identified
594 // by the pair: <node, operand-number>.
595 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000596 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000597
598 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000599 graphRoot = new SchedGraphNode(0, NULL, -1, target);
600 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000601
602 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000603 // First add nodes for all the machine instructions in the basic block
604 // because this greatly simplifies identifying which edges to add.
605 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000606 // Also, remember the load/store instructions to add memory deps later.
607 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000608
Vikram S. Adve7952d602003-05-31 07:37:05 +0000609 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
610 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000611
612 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000613 // Now add edges for the following (all are incoming edges except (4)):
614 // (1) operands of the machine instruction, including hidden operands
615 // (2) machine register dependences
616 // (3) memory load/store dependences
617 // (3) other resource dependences for the machine instruction, if any
618 // (4) output dependences when multiple machine instructions define the
619 // same value; all must have been generated from a single VM instrn
620 // (5) control dependences to branch instructions generated for the
621 // terminator instruction of the BB. Because of delay slots and
622 // 2-way conditional branches, multiple CD edges are needed
623 // (see addCDEdges for details).
624 // Also, note any uses or defs of machine registers.
625 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000626 //----------------------------------------------------------------
627
628 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000629 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000630
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000631 // Then add memory dep edges: store->load, load->store, and store->store.
632 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000633 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000634
635 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000636 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000637
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000638 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000639 for (unsigned i=0, N=MBB.size(); i < N; i++)
640 addEdgesForInstruction(*MBB[i], valueToDefVecMap, target);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000641
Vikram S. Adve200a4352001-11-12 18:53:43 +0000642#ifdef NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000643 // Then add non-SSA edges for all VM instructions in the block.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000644 // We assume that all machine instructions that define a value are
645 // generated from the VM instruction corresponding to that value.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000646 // TODO: This could probably be done much more efficiently.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000647 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000648 this->addNonSSAEdgesForValue(*II, target);
Chris Lattner4ed17ba2001-11-26 18:56:52 +0000649#endif //NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000650
651 // Then add edges for dependences on machine registers
652 this->addMachineRegEdges(regToRefVecMap, target);
653
654 // Finally, add edges from the dummy root and to dummy leaf
655 this->addDummyEdges();
656}
657
658
659//
660// class SchedGraphSet
661//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000662SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000663 const TargetMachine& target) :
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000664 function(_function) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000665 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000666}
667
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000668SchedGraphSet::~SchedGraphSet() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000669 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000670 for(iterator I = begin(), E = end(); I != E; ++I)
671 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000672}
673
674
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000675void SchedGraphSet::dump() const {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000676 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000677 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000678
679 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000680 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000681
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000682 std::cerr << "\n====== End graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000683 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000684}
685
686
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000687void SchedGraphSet::buildGraphsForMethod(const Function *F,
688 const TargetMachine& target) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000689 MachineFunction &MF = MachineFunction::get(F);
690 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
691 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000692}
693
694
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000695void SchedGraphEdge::print(std::ostream &os) const {
696 os << "edge [" << src->getNodeId() << "] -> ["
697 << sink->getNodeId() << "] : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000698
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000699 switch(depType) {
700 case SchedGraphEdge::CtrlDep:
701 os<< "Control Dep";
702 break;
703 case SchedGraphEdge::ValueDep:
704 os<< "Reg Value " << val;
705 break;
706 case SchedGraphEdge::MemoryDep:
707 os<< "Memory Dep";
708 break;
709 case SchedGraphEdge::MachineRegister:
710 os<< "Reg " << machineRegNum;
711 break;
712 case SchedGraphEdge::MachineResource:
713 os<<"Resource "<< resourceId;
714 break;
715 default:
716 assert(0);
717 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000718 }
719
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000720 os << " : delay = " << minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000721}
722
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000723void SchedGraphNode::print(std::ostream &os) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000724 os << std::string(8, ' ')
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000725 << "Node " << ID << " : "
726 << "latency = " << latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000727
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000728 if (getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000729 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000730 else {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000731 os << *getMachineInstr() << "\n" << std::string(12, ' ');
732 os << inEdges.size() << " Incoming Edges:\n";
733 for (unsigned i=0, N = inEdges.size(); i < N; i++)
734 os << std::string(16, ' ') << *inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000735
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000736 os << std::string(12, ' ') << outEdges.size()
Misha Brukman6b77ec42003-05-22 21:49:18 +0000737 << " Outgoing Edges:\n";
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000738 for (unsigned i=0, N= outEdges.size(); i < N; i++)
739 os << std::string(16, ' ') << *outEdges[i];
Misha Brukman6b77ec42003-05-22 21:49:18 +0000740 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000741}
Brian Gaeked0fde302003-11-11 22:41:34 +0000742
743} // End llvm namespace