blob: fe150c243ae41dd6ecd058a0150d0457cea25b35 [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) {
Brian Gaeke918cdd42004-02-12 01:34:05 +000058 MachineOpCode mopCode = MI->getOpcode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +000059 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 {
Brian Gaeke0dc57532004-02-09 18:42:05 +0000103 std::cerr << " Sched Graph for Basic Block: "
104 << MBB.getBasicBlock()->getName()
105 << " (" << MBB.getBasicBlock() << ")"
106 << "\n\n Actual Root nodes: ";
107 for (SchedGraphNodeCommon::const_iterator I = graphRoot->beginOutEdges(),
108 E = graphRoot->endOutEdges();
109 I != E; ++I) {
110 std::cerr << (*I)->getSink ()->getNodeId ();
111 if (I + 1 != E) { std::cerr << ", "; }
112 }
Misha Brukmanc2312df2003-05-22 21:24:35 +0000113 std::cerr << "\n Graph Nodes:\n";
Brian Gaeke0dc57532004-02-09 18:42:05 +0000114 for (const_iterator I = begin(), E = end(); I != E; ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000115 std::cerr << "\n" << *I->second;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000116 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000117}
118
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000119void SchedGraph::addDummyEdges() {
Brian Gaeke0dc57532004-02-09 18:42:05 +0000120 assert(graphRoot->getNumOutEdges() == 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000121
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000122 for (const_iterator I=begin(); I != end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000123 SchedGraphNode* node = (*I).second;
124 assert(node != graphRoot && node != graphLeaf);
125 if (node->beginInEdges() == node->endInEdges())
126 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
127 SchedGraphEdge::NonDataDep, 0);
128 if (node->beginOutEdges() == node->endOutEdges())
129 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
130 SchedGraphEdge::NonDataDep, 0);
131 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000132}
133
134
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000135void SchedGraph::addCDEdges(const TerminatorInst* term,
136 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000137 const TargetInstrInfo& mii = target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000138 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000139
140 // Find the first branch instr in the sequence of machine instrs for term
141 //
142 unsigned first = 0;
Brian Gaeke918cdd42004-02-12 01:34:05 +0000143 while (! mii.isBranch(termMvec[first]->getOpcode()) &&
144 ! mii.isReturn(termMvec[first]->getOpcode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000145 ++first;
146 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000147 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000148 if (first == termMvec.size())
149 return;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000150
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000151 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000152
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000153 // Add CD edges from each instruction in the sequence to the
154 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000155 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000156 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000157 for (unsigned i = termMvec.size(); i > first+1; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000158 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
159 assert(toNode && "No node for instr generated for branch/ret?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000160
Misha Brukman6b77ec42003-05-22 21:49:18 +0000161 for (unsigned j = i-1; j != 0; --j)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000162 if (mii.isBranch(termMvec[j-1]->getOpcode()) ||
163 mii.isReturn(termMvec[j-1]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000164 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
165 assert(brNode && "No node for instr generated for branch/ret?");
166 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
167 SchedGraphEdge::NonDataDep, 0);
168 break; // only one incoming edge is enough
169 }
170 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000171
172 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000173 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000174 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000175 for (unsigned i = first; i != 0; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000176 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
177 assert(fromNode && "No node for instr generated for branch?");
178 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
179 SchedGraphEdge::NonDataDep, 0);
180 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000181
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000182 // Now add CD edges to the first branch instruction in the sequence from
183 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000184 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000185 for (unsigned i=0, N=MBB.size(); i < N; i++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000186 if (MBB[i] == termMvec[first]) // reached the first branch
187 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000188
Misha Brukman6b77ec42003-05-22 21:49:18 +0000189 SchedGraphNode* fromNode = this->getGraphNodeForInstr(MBB[i]);
190 if (fromNode == NULL)
191 continue; // dummy instruction, e.g., PHI
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000192
Misha Brukman6b77ec42003-05-22 21:49:18 +0000193 (void) new SchedGraphEdge(fromNode, firstBrNode,
194 SchedGraphEdge::CtrlDep,
195 SchedGraphEdge::NonDataDep, 0);
196
197 // If we find any other machine instructions (other than due to
198 // the terminator) that also have delay slots, add an outgoing edge
199 // from the instruction to the instructions in the delay slots.
200 //
Brian Gaeke918cdd42004-02-12 01:34:05 +0000201 unsigned d = mii.getNumDelaySlots(MBB[i]->getOpcode());
Misha Brukman6b77ec42003-05-22 21:49:18 +0000202 assert(i+d < N && "Insufficient delay slots for instruction?");
203
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000204 for (unsigned j=1; j <= d; j++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000205 SchedGraphNode* toNode = this->getGraphNodeForInstr(MBB[i+j]);
206 assert(toNode && "No node for machine instr in delay slot?");
207 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000208 SchedGraphEdge::CtrlDep,
209 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000210 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000211 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000212}
213
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000214static const int SG_LOAD_REF = 0;
215static const int SG_STORE_REF = 1;
216static const int SG_CALL_REF = 2;
217
218static const unsigned int SG_DepOrderArray[][3] = {
219 { SchedGraphEdge::NonDataDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000220 SchedGraphEdge::AntiDep,
221 SchedGraphEdge::AntiDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000222 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000223 SchedGraphEdge::OutputDep,
224 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000225 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000226 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
227 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
228 | SchedGraphEdge::OutputDep }
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000229};
230
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000231
Vikram S. Advee64574c2001-11-08 05:20:23 +0000232// Add a dependence edge between every pair of machine load/store/call
233// instructions, where at least one is a store or a call.
234// Use latency 1 just to ensure that memory operations are ordered;
235// latency does not otherwise matter (true dependences enforce that).
236//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000237void SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
238 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000239 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000240
Vikram S. Advee64574c2001-11-08 05:20:23 +0000241 // Instructions in memNodeVec are in execution order within the basic block,
242 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
243 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000244 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000245 MachineOpCode fromOpCode = memNodeVec[im]->getOpcode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000246 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
247 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
248 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000249 for (unsigned jm=im+1; jm < NM; jm++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000250 MachineOpCode toOpCode = memNodeVec[jm]->getOpcode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000251 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
252 : (mii.isLoad(toOpCode)? SG_LOAD_REF
253 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000254
Misha Brukman6b77ec42003-05-22 21:49:18 +0000255 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
256 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
257 SchedGraphEdge::MemoryDep,
258 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000259 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000260 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000261}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000262
Vikram S. Advee64574c2001-11-08 05:20:23 +0000263// Add edges from/to CC reg instrs to/from call instrs.
264// Essentially this prevents anything that sets or uses a CC reg from being
265// reordered w.r.t. a call.
266// Use a latency of 0 because we only need to prevent out-of-order issue,
267// like with control dependences.
268//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000269void SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
270 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000271 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000272
Vikram S. Adve7952d602003-05-31 07:37:05 +0000273 // Instructions in memNodeVec are in execution order within the basic block,
274 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
275 //
276 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000277 if (mii.isCall(callDepNodeVec[ic]->getOpcode())) {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000278 // Add SG_CALL_REF edges from all preds to this instruction.
279 for (unsigned jc=0; jc < ic; jc++)
280 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
281 SchedGraphEdge::MachineRegister,
282 MachineIntRegsRID, 0);
283
284 // And do the same from this instruction to all successors.
285 for (unsigned jc=ic+1; jc < NC; jc++)
286 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
287 SchedGraphEdge::MachineRegister,
288 MachineIntRegsRID, 0);
289 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000290
291#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000292 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000293 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000294 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000295 if (mii.isCall(memNodeVec[im]->getOpcode()))
Vikram S. Advee64574c2001-11-08 05:20:23 +0000296 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000297
Vikram S. Advee64574c2001-11-08 05:20:23 +0000298 // Now walk the entire basic block, looking for CC instructions *and*
299 // call instructions, and keep track of the order of the instructions.
300 // Use the call node vec to quickly find earlier and later call nodes
301 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000302 //
303 int lastCallNodeIdx = -1;
304 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000305 if (mii.isCall(bbMvec[i]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000306 ++lastCallNodeIdx;
307 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
308 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
309 break;
310 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000311 }
Brian Gaeke918cdd42004-02-12 01:34:05 +0000312 else if (mii.isCCInstr(bbMvec[i]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000313 // Add incoming/outgoing edges from/to preceding/later calls
314 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
315 int j=0;
316 for ( ; j <= lastCallNodeIdx; j++)
317 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
318 MachineCCRegsRID, 0);
319 for ( ; j < (int) callNodeVec.size(); j++)
320 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
321 MachineCCRegsRID, 0);
322 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000323#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000324}
325
326
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000327void SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
328 const TargetMachine& target) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000329 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000330 // not aliased!
331 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000332 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000333 I != regToRefVecMap.end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000334 int regNum = (*I).first;
335 RefVec& regRefVec = (*I).second;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000336
Misha Brukman6b77ec42003-05-22 21:49:18 +0000337 // regRefVec is ordered by control flow order in the basic block
338 for (unsigned i=0; i < regRefVec.size(); ++i) {
339 SchedGraphNode* node = regRefVec[i].first;
340 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000341 const MachineOperand& mop =
342 node->getMachineInstr()->getExplOrImplOperand(opNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000343 bool isDef = mop.isDef() && !mop.isUse();
344 bool isDefAndUse = mop.isDef() && mop.isUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000345
Misha Brukman6b77ec42003-05-22 21:49:18 +0000346 for (unsigned p=0; p < i; ++p) {
347 SchedGraphNode* prevNode = regRefVec[p].first;
348 if (prevNode != node) {
349 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000350 const MachineOperand& prevMop =
351 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000352 bool prevIsDef = prevMop.isDef() && !prevMop.isUse();
353 bool prevIsDefAndUse = prevMop.isDef() && prevMop.isUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000354 if (isDef) {
355 if (prevIsDef)
356 new SchedGraphEdge(prevNode, node, regNum,
357 SchedGraphEdge::OutputDep);
358 if (!prevIsDef || prevIsDefAndUse)
359 new SchedGraphEdge(prevNode, node, regNum,
360 SchedGraphEdge::AntiDep);
361 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000362
Misha Brukman6b77ec42003-05-22 21:49:18 +0000363 if (prevIsDef)
364 if (!isDef || isDefAndUse)
365 new SchedGraphEdge(prevNode, node, regNum,
366 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000367 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000368 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000369 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000370 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000371}
372
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000373
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000374// Adds dependences to/from refNode from/to all other defs
375// in the basic block. refNode may be a use, a def, or both.
376// We do not consider other uses because we are not building use-use deps.
377//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000378void SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
379 const RefVec& defVec,
380 const Value* defValue,
381 bool refNodeIsDef,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000382 bool refNodeIsUse,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000383 const TargetMachine& target) {
Vikram S. Adve200a4352001-11-12 18:53:43 +0000384 // Add true or output dep edges from all def nodes before refNode in BB.
385 // Add anti or output dep edges to all def nodes after refNode.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000386 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000387 if ((*I).first == refNode)
388 continue; // Dont add any self-loops
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000389
Misha Brukman6b77ec42003-05-22 21:49:18 +0000390 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB()) {
391 // (*).first is before refNode
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000392 if (refNodeIsDef && !refNodeIsUse)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000393 (void) new SchedGraphEdge((*I).first, refNode, defValue,
394 SchedGraphEdge::OutputDep);
395 if (refNodeIsUse)
396 (void) new SchedGraphEdge((*I).first, refNode, defValue,
397 SchedGraphEdge::TrueDep);
398 } else {
399 // (*).first is after refNode
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000400 if (refNodeIsDef && !refNodeIsUse)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000401 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
402 SchedGraphEdge::OutputDep);
403 if (refNodeIsUse)
404 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
405 SchedGraphEdge::AntiDep);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000406 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000407 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000408}
409
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000410
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000411void SchedGraph::addEdgesForInstruction(const MachineInstr& MI,
412 const ValueToDefVecMap& valueToDefVecMap,
413 const TargetMachine& target) {
Chris Lattner133f0792002-10-28 04:45:29 +0000414 SchedGraphNode* node = getGraphNodeForInstr(&MI);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000415 if (node == NULL)
416 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000417
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000418 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000419 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000420 for (unsigned i = 0, numOps = MI.getNumOperands(); i != numOps; ++i) {
421 switch (MI.getOperand(i).getType()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000422 case MachineOperand::MO_VirtualRegister:
423 case MachineOperand::MO_CCRegister:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000424 if (const Value* srcI = MI.getOperand(i).getVRegValue()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000425 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
426 if (I != valueToDefVecMap.end())
427 addEdgesForValue(node, I->second, srcI,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000428 MI.getOperand(i).isDef(), MI.getOperand(i).isUse(),
429 target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000430 }
431 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000432
Misha Brukman6b77ec42003-05-22 21:49:18 +0000433 case MachineOperand::MO_MachineRegister:
434 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000435
Misha Brukman6b77ec42003-05-22 21:49:18 +0000436 case MachineOperand::MO_SignExtendedImmed:
437 case MachineOperand::MO_UnextendedImmed:
438 case MachineOperand::MO_PCRelativeDisp:
Misha Brukmane2bf0a22003-11-06 00:04:11 +0000439 case MachineOperand::MO_ConstantPoolIndex:
Misha Brukman6b77ec42003-05-22 21:49:18 +0000440 break; // nothing to do for immediate fields
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000441
Misha Brukman6b77ec42003-05-22 21:49:18 +0000442 default:
443 assert(0 && "Unknown machine operand type in SchedGraph builder");
444 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000445 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000446 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000447
448 // Add edges for values implicitly used by the machine instruction.
449 // Examples include function arguments to a Call instructions or the return
450 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000451 //
Chris Lattner133f0792002-10-28 04:45:29 +0000452 for (unsigned i=0, N=MI.getNumImplicitRefs(); i < N; ++i)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000453 if (MI.getImplicitOp(i).isUse())
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000454 if (const Value* srcI = MI.getImplicitRef(i)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000455 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
456 if (I != valueToDefVecMap.end())
457 addEdgesForValue(node, I->second, srcI,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000458 MI.getImplicitOp(i).isDef(),
459 MI.getImplicitOp(i).isUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000460 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000461}
462
463
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000464void SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
465 SchedGraphNode* node,
466 std::vector<SchedGraphNode*>& memNodeVec,
467 std::vector<SchedGraphNode*>& callDepNodeVec,
468 RegToRefVecMap& regToRefVecMap,
469 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000470 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000471
Brian Gaeke918cdd42004-02-12 01:34:05 +0000472 MachineOpCode opCode = node->getOpcode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000473
Vikram S. Adve7952d602003-05-31 07:37:05 +0000474 if (mii.isCall(opCode) || mii.isCCInstr(opCode))
475 callDepNodeVec.push_back(node);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000476
Vikram S. Advee64574c2001-11-08 05:20:23 +0000477 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
478 memNodeVec.push_back(node);
479
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000480 // Collect the register references and value defs. for explicit operands
481 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000482 const MachineInstr& MI = *node->getMachineInstr();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000483 for (int i=0, numOps = (int) MI.getNumOperands(); i < numOps; i++) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000484 const MachineOperand& mop = MI.getOperand(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000485
Misha Brukman6b77ec42003-05-22 21:49:18 +0000486 // if this references a register other than the hardwired
487 // "zero" register, record the reference.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000488 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000489 int regNum = mop.getAllocatedRegNum();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000490
Vikram S. Adve7952d602003-05-31 07:37:05 +0000491 // If this is not a dummy zero register, record the reference in order
Misha Brukman6b77ec42003-05-22 21:49:18 +0000492 if (regNum != target.getRegInfo().getZeroRegNum())
Vikram S. Adve7952d602003-05-31 07:37:05 +0000493 regToRefVecMap[mop.getAllocatedRegNum()]
Misha Brukman6b77ec42003-05-22 21:49:18 +0000494 .push_back(std::make_pair(node, i));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000495
496 // If this is a volatile register, add the instruction to callDepVec
497 // (only if the node is not already on the callDepVec!)
498 if (callDepNodeVec.size() == 0 || callDepNodeVec.back() != node)
499 {
500 unsigned rcid;
501 int regInClass = target.getRegInfo().getClassRegNum(regNum, rcid);
502 if (target.getRegInfo().getMachineRegClass(rcid)
503 ->isRegVolatile(regInClass))
504 callDepNodeVec.push_back(node);
505 }
506
Misha Brukman6b77ec42003-05-22 21:49:18 +0000507 continue; // nothing more to do
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000508 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000509
Misha Brukman6b77ec42003-05-22 21:49:18 +0000510 // ignore all other non-def operands
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000511 if (!MI.getOperand(i).isDef())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000512 continue;
513
514 // We must be defining a value.
515 assert((mop.getType() == MachineOperand::MO_VirtualRegister ||
516 mop.getType() == MachineOperand::MO_CCRegister)
517 && "Do not expect any other kind of operand to be defined!");
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000518 assert(mop.getVRegValue() != NULL && "Null value being defined?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000519
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000520 valueToDefVecMap[mop.getVRegValue()].push_back(std::make_pair(node, i));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000521 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000522
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000523 //
Vikram S. Adve7952d602003-05-31 07:37:05 +0000524 // Collect value defs. for implicit operands. They may have allocated
525 // physical registers also.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000526 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000527 for (unsigned i=0, N = MI.getNumImplicitRefs(); i != N; ++i) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000528 const MachineOperand& mop = MI.getImplicitOp(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000529 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000530 int regNum = mop.getAllocatedRegNum();
531 if (regNum != target.getRegInfo().getZeroRegNum())
532 regToRefVecMap[mop.getAllocatedRegNum()]
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000533 .push_back(std::make_pair(node, i + MI.getNumOperands()));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000534 continue; // nothing more to do
535 }
536
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000537 if (mop.isDef()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000538 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000539 valueToDefVecMap[MI.getImplicitRef(i)].push_back(
540 std::make_pair(node, -i));
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000541 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000542 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000543}
544
545
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000546void SchedGraph::buildNodesForBB(const TargetMachine& target,
547 MachineBasicBlock& MBB,
548 std::vector<SchedGraphNode*>& memNodeVec,
549 std::vector<SchedGraphNode*>& callDepNodeVec,
550 RegToRefVecMap& regToRefVecMap,
551 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000552 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000553
554 // Build graph nodes for each VM instruction and gather def/use info.
555 // Do both those together in a single pass over all machine instructions.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000556 for (unsigned i=0; i < MBB.size(); i++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000557 if (!mii.isDummyPhiInstr(MBB[i]->getOpcode())) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000558 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
559 noteGraphNodeForInstr(MBB[i], node);
560
561 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000562 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
563 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000564 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000565}
566
567
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000568void SchedGraph::buildGraph(const TargetMachine& target) {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000569 // Use this data structure to note all machine operands that compute
570 // ordinary LLVM values. These must be computed defs (i.e., instructions).
571 // Note that there may be multiple machine instructions that define
572 // each Value.
573 ValueToDefVecMap valueToDefVecMap;
574
Vikram S. Advee64574c2001-11-08 05:20:23 +0000575 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000576 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000577 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000578
579 // Use this data structure to note all instructions that access physical
580 // registers that can be modified by a call (including call instructions)
581 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000582
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000583 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000584 // machine registers so we can add edges for those later without
585 // extra passes over the nodes.
586 // The vector holds an ordered list of references to the machine reg,
587 // ordered according to control-flow order. This only works for a
588 // single basic block, hence the assertion. Each reference is identified
589 // by the pair: <node, operand-number>.
590 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000591 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000592
593 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000594 graphRoot = new SchedGraphNode(0, NULL, -1, target);
595 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000596
597 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000598 // First add nodes for all the machine instructions in the basic block
599 // because this greatly simplifies identifying which edges to add.
600 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000601 // Also, remember the load/store instructions to add memory deps later.
602 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000603
Vikram S. Adve7952d602003-05-31 07:37:05 +0000604 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
605 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000606
607 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000608 // Now add edges for the following (all are incoming edges except (4)):
609 // (1) operands of the machine instruction, including hidden operands
610 // (2) machine register dependences
611 // (3) memory load/store dependences
612 // (3) other resource dependences for the machine instruction, if any
613 // (4) output dependences when multiple machine instructions define the
614 // same value; all must have been generated from a single VM instrn
615 // (5) control dependences to branch instructions generated for the
616 // terminator instruction of the BB. Because of delay slots and
617 // 2-way conditional branches, multiple CD edges are needed
618 // (see addCDEdges for details).
619 // Also, note any uses or defs of machine registers.
620 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000621 //----------------------------------------------------------------
622
623 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000624 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000625
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000626 // Then add memory dep edges: store->load, load->store, and store->store.
627 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000628 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000629
630 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000631 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000632
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000633 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000634 for (unsigned i=0, N=MBB.size(); i < N; i++)
635 addEdgesForInstruction(*MBB[i], valueToDefVecMap, target);
Brian Gaeke0dc57532004-02-09 18:42:05 +0000636
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000637 // Then add edges for dependences on machine registers
638 this->addMachineRegEdges(regToRefVecMap, target);
Brian Gaeke0dc57532004-02-09 18:42:05 +0000639
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000640 // Finally, add edges from the dummy root and to dummy leaf
641 this->addDummyEdges();
642}
643
644
645//
646// class SchedGraphSet
647//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000648SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000649 const TargetMachine& target) :
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000650 function(_function) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000651 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000652}
653
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000654SchedGraphSet::~SchedGraphSet() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000655 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000656 for(iterator I = begin(), E = end(); I != E; ++I)
657 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000658}
659
660
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000661void SchedGraphSet::dump() const {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000662 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000663 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000664
665 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000666 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000667
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000668 std::cerr << "\n====== End 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
672
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000673void SchedGraphSet::buildGraphsForMethod(const Function *F,
674 const TargetMachine& target) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000675 MachineFunction &MF = MachineFunction::get(F);
676 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
677 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000678}
679
680
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000681void SchedGraphEdge::print(std::ostream &os) const {
682 os << "edge [" << src->getNodeId() << "] -> ["
683 << sink->getNodeId() << "] : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000684
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000685 switch(depType) {
686 case SchedGraphEdge::CtrlDep:
687 os<< "Control Dep";
688 break;
689 case SchedGraphEdge::ValueDep:
690 os<< "Reg Value " << val;
691 break;
692 case SchedGraphEdge::MemoryDep:
693 os<< "Memory Dep";
694 break;
695 case SchedGraphEdge::MachineRegister:
696 os<< "Reg " << machineRegNum;
697 break;
698 case SchedGraphEdge::MachineResource:
699 os<<"Resource "<< resourceId;
700 break;
701 default:
702 assert(0);
703 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000704 }
705
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000706 os << " : delay = " << minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000707}
708
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000709void SchedGraphNode::print(std::ostream &os) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000710 os << std::string(8, ' ')
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000711 << "Node " << ID << " : "
712 << "latency = " << latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000713
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000714 if (getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000715 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000716 else {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000717 os << *getMachineInstr() << "\n" << std::string(12, ' ');
718 os << inEdges.size() << " Incoming Edges:\n";
719 for (unsigned i=0, N = inEdges.size(); i < N; i++)
720 os << std::string(16, ' ') << *inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000721
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000722 os << std::string(12, ' ') << outEdges.size()
Misha Brukman6b77ec42003-05-22 21:49:18 +0000723 << " Outgoing Edges:\n";
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000724 for (unsigned i=0, N= outEdges.size(); i < N; i++)
725 os << std::string(16, ' ') << *outEdges[i];
Misha Brukman6b77ec42003-05-22 21:49:18 +0000726 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000727}
Brian Gaeked0fde302003-11-11 22:41:34 +0000728
729} // End llvm namespace