blob: 9de0434364835a9050f9d2604505ecd50ad65b99 [file] [log] [blame]
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +00001//===- SchedGraph.cpp - Scheduling Graph Implementation -------------------===//
2//
3// Scheduling graph based on SSA graph plus extra dependence edges capturing
4// dependences due to machine resources (machine registers, CC registers, and
5// any others).
6//
7//===----------------------------------------------------------------------===//
Vikram S. Adve78ef1392001-08-28 23:06:02 +00008
Chris Lattner46cbff62001-09-14 16:56:32 +00009#include "SchedGraph.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000010#include "llvm/Function.h"
Chris Lattnerb00c5822001-10-02 03:41:24 +000011#include "llvm/iOther.h"
Tanya Lattnerc50ee552003-08-27 02:42:58 +000012#include "llvm/CodeGen/MachineCodeForInstruction.h"
13#include "llvm/CodeGen/MachineFunction.h"
14#include "llvm/Target/TargetInstrInfo.h"
15#include "llvm/Target/TargetMachine.h"
16#include "llvm/Target/TargetRegInfo.h"
17#include "Support/STLExtras.h"
Vikram S. Adve78ef1392001-08-28 23:06:02 +000018
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000019//*********************** Internal Data Structures *************************/
20
Vikram S. Advec352d2c2001-11-05 04:04:23 +000021// The following two types need to be classes, not typedefs, so we can use
22// opaque declarations in SchedGraph.h
23//
Misha Brukmanc2312df2003-05-22 21:24:35 +000024struct RefVec: public std::vector<std::pair<SchedGraphNode*, int> > {
25 typedef std::vector<std::pair<SchedGraphNode*,int> >::iterator iterator;
26 typedef
27 std::vector<std::pair<SchedGraphNode*,int> >::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000028};
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000029
Chris Lattner80c685f2001-10-13 06:51:01 +000030struct RegToRefVecMap: public hash_map<int, RefVec> {
Vikram S. Advec352d2c2001-11-05 04:04:23 +000031 typedef hash_map<int, RefVec>:: iterator iterator;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000032 typedef hash_map<int, RefVec>::const_iterator const_iterator;
33};
34
Vikram S. Adve74d15d32003-07-02 01:16:01 +000035struct ValueToDefVecMap: public hash_map<const Value*, RefVec> {
36 typedef hash_map<const Value*, RefVec>:: iterator iterator;
37 typedef hash_map<const Value*, RefVec>::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000038};
39
Vikram S. Adve78ef1392001-08-28 23:06:02 +000040
41//
42// class SchedGraphNode
43//
44
Tanya Lattnerc50ee552003-08-27 02:42:58 +000045SchedGraphNode::SchedGraphNode(unsigned NID, MachineBasicBlock *mbb,
46 int indexInBB, const TargetMachine& Target)
Tanya Lattner8dc99822003-08-28 15:30:40 +000047 : SchedGraphNodeCommon(NID,indexInBB), MBB(mbb), MI(mbb ? (*mbb)[indexInBB] : 0) {
Tanya Lattnerc50ee552003-08-27 02:42:58 +000048 if (MI) {
49 MachineOpCode mopCode = MI->getOpCode();
50 latency = Target.getInstrInfo().hasResultInterlock(mopCode)
51 ? Target.getInstrInfo().minLatency(mopCode)
52 : Target.getInstrInfo().maxLatency(mopCode);
53 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000054}
55
Tanya Lattnerc50ee552003-08-27 02:42:58 +000056SchedGraphNode::~SchedGraphNode() {
Chris Lattnerf3dd05c2002-04-09 05:15:33 +000057 // for each node, delete its out-edges
58 std::for_each(beginOutEdges(), endOutEdges(),
59 deleter<SchedGraphEdge>);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000060}
61
Vikram S. Adve78ef1392001-08-28 23:06:02 +000062//
63// class SchedGraph
64//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000065SchedGraph::SchedGraph(MachineBasicBlock &mbb, const TargetMachine& target)
66 : MBB(mbb) {
Chris Lattner697954c2002-01-20 22:54:45 +000067 buildGraph(target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000068}
69
Tanya Lattnerc50ee552003-08-27 02:42:58 +000070SchedGraph::~SchedGraph() {
Chris Lattner697954c2002-01-20 22:54:45 +000071 for (const_iterator I = begin(); I != end(); ++I)
Chris Lattnerf3dd05c2002-04-09 05:15:33 +000072 delete I->second;
73 delete graphRoot;
74 delete graphLeaf;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000075}
76
Tanya Lattnerc50ee552003-08-27 02:42:58 +000077void SchedGraph::dump() const {
Misha Brukmanc2312df2003-05-22 21:24:35 +000078 std::cerr << " Sched Graph for Basic Block: ";
79 std::cerr << MBB.getBasicBlock()->getName()
80 << " (" << MBB.getBasicBlock() << ")";
Vikram S. Adve78ef1392001-08-28 23:06:02 +000081
Misha Brukmanc2312df2003-05-22 21:24:35 +000082 std::cerr << "\n\n Actual Root nodes : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +000083 for (unsigned i=0, N=graphRoot->outEdges.size(); i < N; i++)
Misha Brukmanc2312df2003-05-22 21:24:35 +000084 std::cerr << graphRoot->outEdges[i]->getSink()->getNodeId()
85 << ((i == N-1)? "" : ", ");
Vikram S. Adve78ef1392001-08-28 23:06:02 +000086
Misha Brukmanc2312df2003-05-22 21:24:35 +000087 std::cerr << "\n Graph Nodes:\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +000088 for (const_iterator I=begin(); I != end(); ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +000089 std::cerr << "\n" << *I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000090
Misha Brukmanc2312df2003-05-22 21:24:35 +000091 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +000092}
93
94
Vikram S. Adve8b6d2452001-09-18 12:50:40 +000095
Tanya Lattnerc50ee552003-08-27 02:42:58 +000096void SchedGraph::addDummyEdges() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000097 assert(graphRoot->outEdges.size() == 0);
98
Tanya Lattnerc50ee552003-08-27 02:42:58 +000099 for (const_iterator I=begin(); I != end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000100 SchedGraphNode* node = (*I).second;
101 assert(node != graphRoot && node != graphLeaf);
102 if (node->beginInEdges() == node->endInEdges())
103 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
104 SchedGraphEdge::NonDataDep, 0);
105 if (node->beginOutEdges() == node->endOutEdges())
106 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
107 SchedGraphEdge::NonDataDep, 0);
108 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000109}
110
111
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000112void SchedGraph::addCDEdges(const TerminatorInst* term,
113 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000114 const TargetInstrInfo& mii = target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000115 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000116
117 // Find the first branch instr in the sequence of machine instrs for term
118 //
119 unsigned first = 0;
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000120 while (! mii.isBranch(termMvec[first]->getOpCode()) &&
121 ! mii.isReturn(termMvec[first]->getOpCode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000122 ++first;
123 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000124 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000125 if (first == termMvec.size())
126 return;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000127
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000128 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000129
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000130 // Add CD edges from each instruction in the sequence to the
131 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000132 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000133 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000134 for (unsigned i = termMvec.size(); i > first+1; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000135 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
136 assert(toNode && "No node for instr generated for branch/ret?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000137
Misha Brukman6b77ec42003-05-22 21:49:18 +0000138 for (unsigned j = i-1; j != 0; --j)
139 if (mii.isBranch(termMvec[j-1]->getOpCode()) ||
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000140 mii.isReturn(termMvec[j-1]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000141 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
142 assert(brNode && "No node for instr generated for branch/ret?");
143 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
144 SchedGraphEdge::NonDataDep, 0);
145 break; // only one incoming edge is enough
146 }
147 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000148
149 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000150 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000151 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000152 for (unsigned i = first; i != 0; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000153 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
154 assert(fromNode && "No node for instr generated for branch?");
155 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
156 SchedGraphEdge::NonDataDep, 0);
157 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000158
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000159 // Now add CD edges to the first branch instruction in the sequence from
160 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000161 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000162 for (unsigned i=0, N=MBB.size(); i < N; i++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000163 if (MBB[i] == termMvec[first]) // reached the first branch
164 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000165
Misha Brukman6b77ec42003-05-22 21:49:18 +0000166 SchedGraphNode* fromNode = this->getGraphNodeForInstr(MBB[i]);
167 if (fromNode == NULL)
168 continue; // dummy instruction, e.g., PHI
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000169
Misha Brukman6b77ec42003-05-22 21:49:18 +0000170 (void) new SchedGraphEdge(fromNode, firstBrNode,
171 SchedGraphEdge::CtrlDep,
172 SchedGraphEdge::NonDataDep, 0);
173
174 // If we find any other machine instructions (other than due to
175 // the terminator) that also have delay slots, add an outgoing edge
176 // from the instruction to the instructions in the delay slots.
177 //
178 unsigned d = mii.getNumDelaySlots(MBB[i]->getOpCode());
179 assert(i+d < N && "Insufficient delay slots for instruction?");
180
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000181 for (unsigned j=1; j <= d; j++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000182 SchedGraphNode* toNode = this->getGraphNodeForInstr(MBB[i+j]);
183 assert(toNode && "No node for machine instr in delay slot?");
184 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000185 SchedGraphEdge::CtrlDep,
186 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000187 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000188 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000189}
190
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000191static const int SG_LOAD_REF = 0;
192static const int SG_STORE_REF = 1;
193static const int SG_CALL_REF = 2;
194
195static const unsigned int SG_DepOrderArray[][3] = {
196 { SchedGraphEdge::NonDataDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000197 SchedGraphEdge::AntiDep,
198 SchedGraphEdge::AntiDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000199 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000200 SchedGraphEdge::OutputDep,
201 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000202 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000203 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
204 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
205 | SchedGraphEdge::OutputDep }
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000206};
207
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000208
Vikram S. Advee64574c2001-11-08 05:20:23 +0000209// Add a dependence edge between every pair of machine load/store/call
210// instructions, where at least one is a store or a call.
211// Use latency 1 just to ensure that memory operations are ordered;
212// latency does not otherwise matter (true dependences enforce that).
213//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000214void SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
215 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000216 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000217
Vikram S. Advee64574c2001-11-08 05:20:23 +0000218 // Instructions in memNodeVec are in execution order within the basic block,
219 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
220 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000221 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000222 MachineOpCode fromOpCode = memNodeVec[im]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000223 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
224 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
225 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000226 for (unsigned jm=im+1; jm < NM; jm++) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000227 MachineOpCode toOpCode = memNodeVec[jm]->getOpCode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000228 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
229 : (mii.isLoad(toOpCode)? SG_LOAD_REF
230 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000231
Misha Brukman6b77ec42003-05-22 21:49:18 +0000232 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
233 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
234 SchedGraphEdge::MemoryDep,
235 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000236 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000237 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000238}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000239
Vikram S. Advee64574c2001-11-08 05:20:23 +0000240// Add edges from/to CC reg instrs to/from call instrs.
241// Essentially this prevents anything that sets or uses a CC reg from being
242// reordered w.r.t. a call.
243// Use a latency of 0 because we only need to prevent out-of-order issue,
244// like with control dependences.
245//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000246void SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
247 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000248 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000249
Vikram S. Adve7952d602003-05-31 07:37:05 +0000250 // Instructions in memNodeVec are in execution order within the basic block,
251 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
252 //
253 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000254 if (mii.isCall(callDepNodeVec[ic]->getOpCode())) {
255 // Add SG_CALL_REF edges from all preds to this instruction.
256 for (unsigned jc=0; jc < ic; jc++)
257 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
258 SchedGraphEdge::MachineRegister,
259 MachineIntRegsRID, 0);
260
261 // And do the same from this instruction to all successors.
262 for (unsigned jc=ic+1; jc < NC; jc++)
263 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
264 SchedGraphEdge::MachineRegister,
265 MachineIntRegsRID, 0);
266 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000267
268#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000269 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000270 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000271 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
272 if (mii.isCall(memNodeVec[im]->getOpCode()))
273 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000274
Vikram S. Advee64574c2001-11-08 05:20:23 +0000275 // Now walk the entire basic block, looking for CC instructions *and*
276 // call instructions, and keep track of the order of the instructions.
277 // Use the call node vec to quickly find earlier and later call nodes
278 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000279 //
280 int lastCallNodeIdx = -1;
281 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000282 if (mii.isCall(bbMvec[i]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000283 ++lastCallNodeIdx;
284 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
285 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
286 break;
287 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000288 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000289 else if (mii.isCCInstr(bbMvec[i]->getOpCode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000290 // Add incoming/outgoing edges from/to preceding/later calls
291 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
292 int j=0;
293 for ( ; j <= lastCallNodeIdx; j++)
294 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
295 MachineCCRegsRID, 0);
296 for ( ; j < (int) callNodeVec.size(); j++)
297 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
298 MachineCCRegsRID, 0);
299 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000300#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000301}
302
303
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000304void SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
305 const TargetMachine& target) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000306 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000307 // not aliased!
308 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000309 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000310 I != regToRefVecMap.end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000311 int regNum = (*I).first;
312 RefVec& regRefVec = (*I).second;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000313
Misha Brukman6b77ec42003-05-22 21:49:18 +0000314 // regRefVec is ordered by control flow order in the basic block
315 for (unsigned i=0; i < regRefVec.size(); ++i) {
316 SchedGraphNode* node = regRefVec[i].first;
317 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000318 const MachineOperand& mop =
319 node->getMachineInstr()->getExplOrImplOperand(opNum);
320 bool isDef = mop.opIsDefOnly();
321 bool isDefAndUse = mop.opIsDefAndUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000322
Misha Brukman6b77ec42003-05-22 21:49:18 +0000323 for (unsigned p=0; p < i; ++p) {
324 SchedGraphNode* prevNode = regRefVec[p].first;
325 if (prevNode != node) {
326 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000327 const MachineOperand& prevMop =
328 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
329 bool prevIsDef = prevMop.opIsDefOnly();
330 bool prevIsDefAndUse = prevMop.opIsDefAndUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000331 if (isDef) {
332 if (prevIsDef)
333 new SchedGraphEdge(prevNode, node, regNum,
334 SchedGraphEdge::OutputDep);
335 if (!prevIsDef || prevIsDefAndUse)
336 new SchedGraphEdge(prevNode, node, regNum,
337 SchedGraphEdge::AntiDep);
338 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000339
Misha Brukman6b77ec42003-05-22 21:49:18 +0000340 if (prevIsDef)
341 if (!isDef || isDefAndUse)
342 new SchedGraphEdge(prevNode, node, regNum,
343 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000344 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000345 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000346 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000347 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000348}
349
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000350
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000351// Adds dependences to/from refNode from/to all other defs
352// in the basic block. refNode may be a use, a def, or both.
353// We do not consider other uses because we are not building use-use deps.
354//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000355void SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
356 const RefVec& defVec,
357 const Value* defValue,
358 bool refNodeIsDef,
359 bool refNodeIsDefAndUse,
360 const TargetMachine& target) {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000361 bool refNodeIsUse = !refNodeIsDef || refNodeIsDefAndUse;
362
Vikram S. Adve200a4352001-11-12 18:53:43 +0000363 // Add true or output dep edges from all def nodes before refNode in BB.
364 // Add anti or output dep edges to all def nodes after refNode.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000365 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000366 if ((*I).first == refNode)
367 continue; // Dont add any self-loops
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000368
Misha Brukman6b77ec42003-05-22 21:49:18 +0000369 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB()) {
370 // (*).first is before refNode
371 if (refNodeIsDef)
372 (void) new SchedGraphEdge((*I).first, refNode, defValue,
373 SchedGraphEdge::OutputDep);
374 if (refNodeIsUse)
375 (void) new SchedGraphEdge((*I).first, refNode, defValue,
376 SchedGraphEdge::TrueDep);
377 } else {
378 // (*).first is after refNode
379 if (refNodeIsDef)
380 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
381 SchedGraphEdge::OutputDep);
382 if (refNodeIsUse)
383 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
384 SchedGraphEdge::AntiDep);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000385 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000386 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000387}
388
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000389
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000390void SchedGraph::addEdgesForInstruction(const MachineInstr& MI,
391 const ValueToDefVecMap& valueToDefVecMap,
392 const TargetMachine& target) {
Chris Lattner133f0792002-10-28 04:45:29 +0000393 SchedGraphNode* node = getGraphNodeForInstr(&MI);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000394 if (node == NULL)
395 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000396
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000397 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000398 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000399 for (unsigned i = 0, numOps = MI.getNumOperands(); i != numOps; ++i) {
400 switch (MI.getOperand(i).getType()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000401 case MachineOperand::MO_VirtualRegister:
402 case MachineOperand::MO_CCRegister:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000403 if (const Value* srcI = MI.getOperand(i).getVRegValue()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000404 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
405 if (I != valueToDefVecMap.end())
406 addEdgesForValue(node, I->second, srcI,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000407 MI.getOperand(i).opIsDefOnly(),
408 MI.getOperand(i).opIsDefAndUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000409 }
410 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000411
Misha Brukman6b77ec42003-05-22 21:49:18 +0000412 case MachineOperand::MO_MachineRegister:
413 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000414
Misha Brukman6b77ec42003-05-22 21:49:18 +0000415 case MachineOperand::MO_SignExtendedImmed:
416 case MachineOperand::MO_UnextendedImmed:
417 case MachineOperand::MO_PCRelativeDisp:
418 break; // nothing to do for immediate fields
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000419
Misha Brukman6b77ec42003-05-22 21:49:18 +0000420 default:
421 assert(0 && "Unknown machine operand type in SchedGraph builder");
422 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000423 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000424 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000425
426 // Add edges for values implicitly used by the machine instruction.
427 // Examples include function arguments to a Call instructions or the return
428 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000429 //
Chris Lattner133f0792002-10-28 04:45:29 +0000430 for (unsigned i=0, N=MI.getNumImplicitRefs(); i < N; ++i)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000431 if (MI.getImplicitOp(i).opIsUse() || MI.getImplicitOp(i).opIsDefAndUse())
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000432 if (const Value* srcI = MI.getImplicitRef(i)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000433 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
434 if (I != valueToDefVecMap.end())
435 addEdgesForValue(node, I->second, srcI,
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000436 MI.getImplicitOp(i).opIsDefOnly(),
437 MI.getImplicitOp(i).opIsDefAndUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000438 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000439}
440
441
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000442void SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
443 SchedGraphNode* node,
444 std::vector<SchedGraphNode*>& memNodeVec,
445 std::vector<SchedGraphNode*>& callDepNodeVec,
446 RegToRefVecMap& regToRefVecMap,
447 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000448 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000449
Vikram S. Advee64574c2001-11-08 05:20:23 +0000450 MachineOpCode opCode = node->getOpCode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000451
Vikram S. Adve7952d602003-05-31 07:37:05 +0000452 if (mii.isCall(opCode) || mii.isCCInstr(opCode))
453 callDepNodeVec.push_back(node);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000454
Vikram S. Advee64574c2001-11-08 05:20:23 +0000455 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
456 memNodeVec.push_back(node);
457
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000458 // Collect the register references and value defs. for explicit operands
459 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000460 const MachineInstr& MI = *node->getMachineInstr();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000461 for (int i=0, numOps = (int) MI.getNumOperands(); i < numOps; i++) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000462 const MachineOperand& mop = MI.getOperand(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000463
Misha Brukman6b77ec42003-05-22 21:49:18 +0000464 // if this references a register other than the hardwired
465 // "zero" register, record the reference.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000466 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000467 int regNum = mop.getAllocatedRegNum();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000468
Vikram S. Adve7952d602003-05-31 07:37:05 +0000469 // If this is not a dummy zero register, record the reference in order
Misha Brukman6b77ec42003-05-22 21:49:18 +0000470 if (regNum != target.getRegInfo().getZeroRegNum())
Vikram S. Adve7952d602003-05-31 07:37:05 +0000471 regToRefVecMap[mop.getAllocatedRegNum()]
Misha Brukman6b77ec42003-05-22 21:49:18 +0000472 .push_back(std::make_pair(node, i));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000473
474 // If this is a volatile register, add the instruction to callDepVec
475 // (only if the node is not already on the callDepVec!)
476 if (callDepNodeVec.size() == 0 || callDepNodeVec.back() != node)
477 {
478 unsigned rcid;
479 int regInClass = target.getRegInfo().getClassRegNum(regNum, rcid);
480 if (target.getRegInfo().getMachineRegClass(rcid)
481 ->isRegVolatile(regInClass))
482 callDepNodeVec.push_back(node);
483 }
484
Misha Brukman6b77ec42003-05-22 21:49:18 +0000485 continue; // nothing more to do
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000486 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000487
Misha Brukman6b77ec42003-05-22 21:49:18 +0000488 // ignore all other non-def operands
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000489 if (!MI.getOperand(i).opIsDefOnly() &&
490 !MI.getOperand(i).opIsDefAndUse())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000491 continue;
492
493 // We must be defining a value.
494 assert((mop.getType() == MachineOperand::MO_VirtualRegister ||
495 mop.getType() == MachineOperand::MO_CCRegister)
496 && "Do not expect any other kind of operand to be defined!");
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000497 assert(mop.getVRegValue() != NULL && "Null value being defined?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000498
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000499 valueToDefVecMap[mop.getVRegValue()].push_back(std::make_pair(node, i));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000500 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000501
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000502 //
Vikram S. Adve7952d602003-05-31 07:37:05 +0000503 // Collect value defs. for implicit operands. They may have allocated
504 // physical registers also.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000505 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000506 for (unsigned i=0, N = MI.getNumImplicitRefs(); i != N; ++i) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000507 const MachineOperand& mop = MI.getImplicitOp(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000508 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000509 int regNum = mop.getAllocatedRegNum();
510 if (regNum != target.getRegInfo().getZeroRegNum())
511 regToRefVecMap[mop.getAllocatedRegNum()]
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000512 .push_back(std::make_pair(node, i + MI.getNumOperands()));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000513 continue; // nothing more to do
514 }
515
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000516 if (mop.opIsDefOnly() || mop.opIsDefAndUse()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000517 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
518 valueToDefVecMap[MI.getImplicitRef(i)].push_back(std::make_pair(node,
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000519 -i));
520 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000521 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000522}
523
524
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000525void SchedGraph::buildNodesForBB(const TargetMachine& target,
526 MachineBasicBlock& MBB,
527 std::vector<SchedGraphNode*>& memNodeVec,
528 std::vector<SchedGraphNode*>& callDepNodeVec,
529 RegToRefVecMap& regToRefVecMap,
530 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000531 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000532
533 // Build graph nodes for each VM instruction and gather def/use info.
534 // Do both those together in a single pass over all machine instructions.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000535 for (unsigned i=0; i < MBB.size(); i++)
536 if (!mii.isDummyPhiInstr(MBB[i]->getOpCode())) {
537 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
538 noteGraphNodeForInstr(MBB[i], node);
539
540 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000541 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
542 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000543 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000544}
545
546
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000547void SchedGraph::buildGraph(const TargetMachine& target) {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000548 // Use this data structure to note all machine operands that compute
549 // ordinary LLVM values. These must be computed defs (i.e., instructions).
550 // Note that there may be multiple machine instructions that define
551 // each Value.
552 ValueToDefVecMap valueToDefVecMap;
553
Vikram S. Advee64574c2001-11-08 05:20:23 +0000554 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000555 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000556 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000557
558 // Use this data structure to note all instructions that access physical
559 // registers that can be modified by a call (including call instructions)
560 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000561
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000562 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000563 // machine registers so we can add edges for those later without
564 // extra passes over the nodes.
565 // The vector holds an ordered list of references to the machine reg,
566 // ordered according to control-flow order. This only works for a
567 // single basic block, hence the assertion. Each reference is identified
568 // by the pair: <node, operand-number>.
569 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000570 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000571
572 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000573 graphRoot = new SchedGraphNode(0, NULL, -1, target);
574 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000575
576 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000577 // First add nodes for all the machine instructions in the basic block
578 // because this greatly simplifies identifying which edges to add.
579 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000580 // Also, remember the load/store instructions to add memory deps later.
581 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000582
Vikram S. Adve7952d602003-05-31 07:37:05 +0000583 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
584 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000585
586 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000587 // Now add edges for the following (all are incoming edges except (4)):
588 // (1) operands of the machine instruction, including hidden operands
589 // (2) machine register dependences
590 // (3) memory load/store dependences
591 // (3) other resource dependences for the machine instruction, if any
592 // (4) output dependences when multiple machine instructions define the
593 // same value; all must have been generated from a single VM instrn
594 // (5) control dependences to branch instructions generated for the
595 // terminator instruction of the BB. Because of delay slots and
596 // 2-way conditional branches, multiple CD edges are needed
597 // (see addCDEdges for details).
598 // Also, note any uses or defs of machine registers.
599 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000600 //----------------------------------------------------------------
601
602 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000603 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000604
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000605 // Then add memory dep edges: store->load, load->store, and store->store.
606 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000607 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000608
609 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000610 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000611
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000612 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000613 for (unsigned i=0, N=MBB.size(); i < N; i++)
614 addEdgesForInstruction(*MBB[i], valueToDefVecMap, target);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000615
Vikram S. Adve200a4352001-11-12 18:53:43 +0000616#ifdef NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000617 // Then add non-SSA edges for all VM instructions in the block.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000618 // We assume that all machine instructions that define a value are
619 // generated from the VM instruction corresponding to that value.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000620 // TODO: This could probably be done much more efficiently.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000621 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000622 this->addNonSSAEdgesForValue(*II, target);
Chris Lattner4ed17ba2001-11-26 18:56:52 +0000623#endif //NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000624
625 // Then add edges for dependences on machine registers
626 this->addMachineRegEdges(regToRefVecMap, target);
627
628 // Finally, add edges from the dummy root and to dummy leaf
629 this->addDummyEdges();
630}
631
632
633//
634// class SchedGraphSet
635//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000636SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000637 const TargetMachine& target) :
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000638 function(_function) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000639 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000640}
641
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000642SchedGraphSet::~SchedGraphSet() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000643 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000644 for(iterator I = begin(), E = end(); I != E; ++I)
645 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000646}
647
648
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000649void SchedGraphSet::dump() const {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000650 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000651 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000652
653 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000654 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000655
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000656 std::cerr << "\n====== End graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000657 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000658}
659
660
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000661void SchedGraphSet::buildGraphsForMethod(const Function *F,
662 const TargetMachine& target) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000663 MachineFunction &MF = MachineFunction::get(F);
664 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
665 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000666}
667
668
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000669void SchedGraphEdge::print(std::ostream &os) const {
670 os << "edge [" << src->getNodeId() << "] -> ["
671 << sink->getNodeId() << "] : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000672
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000673 switch(depType) {
674 case SchedGraphEdge::CtrlDep:
675 os<< "Control Dep";
676 break;
677 case SchedGraphEdge::ValueDep:
678 os<< "Reg Value " << val;
679 break;
680 case SchedGraphEdge::MemoryDep:
681 os<< "Memory Dep";
682 break;
683 case SchedGraphEdge::MachineRegister:
684 os<< "Reg " << machineRegNum;
685 break;
686 case SchedGraphEdge::MachineResource:
687 os<<"Resource "<< resourceId;
688 break;
689 default:
690 assert(0);
691 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000692 }
693
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000694 os << " : delay = " << minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000695}
696
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000697void SchedGraphNode::print(std::ostream &os) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000698 os << std::string(8, ' ')
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000699 << "Node " << ID << " : "
700 << "latency = " << latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000701
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000702 if (getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000703 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000704 else {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000705 os << *getMachineInstr() << "\n" << std::string(12, ' ');
706 os << inEdges.size() << " Incoming Edges:\n";
707 for (unsigned i=0, N = inEdges.size(); i < N; i++)
708 os << std::string(16, ' ') << *inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000709
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000710 os << std::string(12, ' ') << outEdges.size()
Misha Brukman6b77ec42003-05-22 21:49:18 +0000711 << " Outgoing Edges:\n";
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000712 for (unsigned i=0, N= outEdges.size(); i < N; i++)
713 os << std::string(16, ' ') << *outEdges[i];
Misha Brukman6b77ec42003-05-22 21:49:18 +0000714 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000715}