blob: 00b48d537d3eee8089fb54d58f2dd5fd1ee442b6 [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 Lattner9670eec2004-07-29 17:11:37 +000018#include "llvm/Instructions.h"
Tanya Lattnerc50ee552003-08-27 02:42:58 +000019#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Target/TargetMachine.h"
Chris Lattner85015a02004-08-16 21:55:02 +000022#include "../../Target/SparcV9/MachineCodeForInstruction.h"
Brian Gaeke35450d22004-04-23 18:15:46 +000023#include "../../Target/SparcV9/SparcV9RegInfo.h"
Brian Gaeke418379e2004-08-18 20:04:24 +000024#include "../../Target/SparcV9/SparcV9InstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/ADT/STLExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000026#include <iostream>
Vikram S. Adve78ef1392001-08-28 23:06:02 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028namespace llvm {
29
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000030//*********************** Internal Data Structures *************************/
31
Vikram S. Advec352d2c2001-11-05 04:04:23 +000032// The following two types need to be classes, not typedefs, so we can use
33// opaque declarations in SchedGraph.h
34//
Misha Brukmanc2312df2003-05-22 21:24:35 +000035struct RefVec: public std::vector<std::pair<SchedGraphNode*, int> > {
36 typedef std::vector<std::pair<SchedGraphNode*,int> >::iterator iterator;
37 typedef
38 std::vector<std::pair<SchedGraphNode*,int> >::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000039};
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000040
Chris Lattner80c685f2001-10-13 06:51:01 +000041struct RegToRefVecMap: public hash_map<int, RefVec> {
Vikram S. Advec352d2c2001-11-05 04:04:23 +000042 typedef hash_map<int, RefVec>:: iterator iterator;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000043 typedef hash_map<int, RefVec>::const_iterator const_iterator;
44};
45
Vikram S. Adve74d15d32003-07-02 01:16:01 +000046struct ValueToDefVecMap: public hash_map<const Value*, RefVec> {
47 typedef hash_map<const Value*, RefVec>:: iterator iterator;
48 typedef hash_map<const Value*, RefVec>::const_iterator const_iterator;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000049};
50
Vikram S. Adve78ef1392001-08-28 23:06:02 +000051
52//
53// class SchedGraphNode
54//
55
Tanya Lattnerc50ee552003-08-27 02:42:58 +000056SchedGraphNode::SchedGraphNode(unsigned NID, MachineBasicBlock *mbb,
57 int indexInBB, const TargetMachine& Target)
Chris Lattnere6d04f12004-02-18 16:43:51 +000058 : SchedGraphNodeCommon(NID,indexInBB), MBB(mbb), MI(0) {
59 if (mbb) {
60 MachineBasicBlock::iterator I = MBB->begin();
61 std::advance(I, indexInBB);
62 MI = I;
63
Brian Gaeke918cdd42004-02-12 01:34:05 +000064 MachineOpCode mopCode = MI->getOpcode();
Chris Lattner98107ff2004-06-02 06:06:20 +000065 latency = Target.getInstrInfo()->hasResultInterlock(mopCode)
66 ? Target.getInstrInfo()->minLatency(mopCode)
67 : Target.getInstrInfo()->maxLatency(mopCode);
Tanya Lattnerc50ee552003-08-27 02:42:58 +000068 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000069}
70
John Criswellc9afb492003-08-28 21:43:17 +000071//
72// Method: SchedGraphNode Destructor
73//
74// Description:
75// Free memory allocated by the SchedGraphNode object.
76//
77// Notes:
78// Do not delete the edges here. The base class will take care of that.
79// Only handle subclass specific stuff here (where currently there is
80// none).
81//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000082SchedGraphNode::~SchedGraphNode() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000083}
84
Vikram S. Adve78ef1392001-08-28 23:06:02 +000085//
86// class SchedGraph
87//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000088SchedGraph::SchedGraph(MachineBasicBlock &mbb, const TargetMachine& target)
89 : MBB(mbb) {
Chris Lattner697954c2002-01-20 22:54:45 +000090 buildGraph(target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000091}
92
John Criswellc9afb492003-08-28 21:43:17 +000093//
94// Method: SchedGraph Destructor
95//
96// Description:
97// This method deletes memory allocated by the SchedGraph object.
98//
99// Notes:
100// Do not delete the graphRoot or graphLeaf here. The base class handles
101// that bit of work.
102//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000103SchedGraph::~SchedGraph() {
Chris Lattner697954c2002-01-20 22:54:45 +0000104 for (const_iterator I = begin(); I != end(); ++I)
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000105 delete I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000106}
107
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000108void SchedGraph::dump() const {
Brian Gaeke0dc57532004-02-09 18:42:05 +0000109 std::cerr << " Sched Graph for Basic Block: "
110 << MBB.getBasicBlock()->getName()
Chris Lattnerf51c7f562004-07-15 02:40:04 +0000111 << " (" << *MBB.getBasicBlock() << ")"
Brian Gaeke0dc57532004-02-09 18:42:05 +0000112 << "\n\n Actual Root nodes: ";
113 for (SchedGraphNodeCommon::const_iterator I = graphRoot->beginOutEdges(),
114 E = graphRoot->endOutEdges();
115 I != E; ++I) {
116 std::cerr << (*I)->getSink ()->getNodeId ();
117 if (I + 1 != E) { std::cerr << ", "; }
118 }
Misha Brukmanc2312df2003-05-22 21:24:35 +0000119 std::cerr << "\n Graph Nodes:\n";
Brian Gaeke0dc57532004-02-09 18:42:05 +0000120 for (const_iterator I = begin(), E = end(); I != E; ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000121 std::cerr << "\n" << *I->second;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000122 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000123}
124
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000125void SchedGraph::addDummyEdges() {
Brian Gaeke0dc57532004-02-09 18:42:05 +0000126 assert(graphRoot->getNumOutEdges() == 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000127
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000128 for (const_iterator I=begin(); I != end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000129 SchedGraphNode* node = (*I).second;
130 assert(node != graphRoot && node != graphLeaf);
131 if (node->beginInEdges() == node->endInEdges())
132 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
133 SchedGraphEdge::NonDataDep, 0);
134 if (node->beginOutEdges() == node->endOutEdges())
135 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
136 SchedGraphEdge::NonDataDep, 0);
137 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000138}
139
140
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000141void SchedGraph::addCDEdges(const TerminatorInst* term,
142 const TargetMachine& target) {
Chris Lattner98107ff2004-06-02 06:06:20 +0000143 const TargetInstrInfo& mii = *target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000144 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000145
146 // Find the first branch instr in the sequence of machine instrs for term
147 //
148 unsigned first = 0;
Brian Gaeke918cdd42004-02-12 01:34:05 +0000149 while (! mii.isBranch(termMvec[first]->getOpcode()) &&
150 ! mii.isReturn(termMvec[first]->getOpcode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000151 ++first;
152 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000153 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000154 if (first == termMvec.size())
155 return;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000156
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000157 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000158
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000159 // Add CD edges from each instruction in the sequence to the
160 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000161 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000162 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000163 for (unsigned i = termMvec.size(); i > first+1; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000164 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
165 assert(toNode && "No node for instr generated for branch/ret?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000166
Misha Brukman6b77ec42003-05-22 21:49:18 +0000167 for (unsigned j = i-1; j != 0; --j)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000168 if (mii.isBranch(termMvec[j-1]->getOpcode()) ||
169 mii.isReturn(termMvec[j-1]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000170 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
171 assert(brNode && "No node for instr generated for branch/ret?");
172 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
173 SchedGraphEdge::NonDataDep, 0);
174 break; // only one incoming edge is enough
175 }
176 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000177
178 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000179 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000180 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000181 for (unsigned i = first; i != 0; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000182 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
183 assert(fromNode && "No node for instr generated for branch?");
184 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
185 SchedGraphEdge::NonDataDep, 0);
186 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000187
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000188 // Now add CD edges to the first branch instruction in the sequence from
189 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000190 //
Chris Lattnere6d04f12004-02-18 16:43:51 +0000191 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
192 if (&*I == termMvec[first]) // reached the first branch
Misha Brukman6b77ec42003-05-22 21:49:18 +0000193 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000194
Chris Lattnere6d04f12004-02-18 16:43:51 +0000195 SchedGraphNode* fromNode = getGraphNodeForInstr(I);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000196 if (fromNode == NULL)
197 continue; // dummy instruction, e.g., PHI
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000198
Misha Brukman6b77ec42003-05-22 21:49:18 +0000199 (void) new SchedGraphEdge(fromNode, firstBrNode,
200 SchedGraphEdge::CtrlDep,
201 SchedGraphEdge::NonDataDep, 0);
202
203 // If we find any other machine instructions (other than due to
204 // the terminator) that also have delay slots, add an outgoing edge
205 // from the instruction to the instructions in the delay slots.
206 //
Chris Lattnere6d04f12004-02-18 16:43:51 +0000207 unsigned d = mii.getNumDelaySlots(I->getOpcode());
208
209 MachineBasicBlock::iterator J = I; ++J;
210 for (unsigned j=1; j <= d; j++, ++J) {
211 SchedGraphNode* toNode = this->getGraphNodeForInstr(J);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000212 assert(toNode && "No node for machine instr in delay slot?");
213 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000214 SchedGraphEdge::CtrlDep,
215 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000216 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000217 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000218}
219
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000220static const int SG_LOAD_REF = 0;
221static const int SG_STORE_REF = 1;
222static const int SG_CALL_REF = 2;
223
224static const unsigned int SG_DepOrderArray[][3] = {
225 { SchedGraphEdge::NonDataDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000226 SchedGraphEdge::AntiDep,
227 SchedGraphEdge::AntiDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000228 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000229 SchedGraphEdge::OutputDep,
230 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000231 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000232 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
233 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
234 | SchedGraphEdge::OutputDep }
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000235};
236
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000237
Vikram S. Advee64574c2001-11-08 05:20:23 +0000238// Add a dependence edge between every pair of machine load/store/call
239// instructions, where at least one is a store or a call.
240// Use latency 1 just to ensure that memory operations are ordered;
241// latency does not otherwise matter (true dependences enforce that).
242//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000243void SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
244 const TargetMachine& target) {
Chris Lattner98107ff2004-06-02 06:06:20 +0000245 const TargetInstrInfo& mii = *target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000246
Vikram S. Advee64574c2001-11-08 05:20:23 +0000247 // Instructions in memNodeVec are in execution order within the basic block,
248 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
249 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000250 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000251 MachineOpCode fromOpCode = memNodeVec[im]->getOpcode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000252 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
253 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
254 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000255 for (unsigned jm=im+1; jm < NM; jm++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000256 MachineOpCode toOpCode = memNodeVec[jm]->getOpcode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000257 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
258 : (mii.isLoad(toOpCode)? SG_LOAD_REF
259 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000260
Misha Brukman6b77ec42003-05-22 21:49:18 +0000261 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
262 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
263 SchedGraphEdge::MemoryDep,
264 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000265 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000266 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000267}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000268
Vikram S. Advee64574c2001-11-08 05:20:23 +0000269// Add edges from/to CC reg instrs to/from call instrs.
270// Essentially this prevents anything that sets or uses a CC reg from being
271// reordered w.r.t. a call.
272// Use a latency of 0 because we only need to prevent out-of-order issue,
273// like with control dependences.
274//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000275void SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
276 const TargetMachine& target) {
Chris Lattner98107ff2004-06-02 06:06:20 +0000277 const TargetInstrInfo& mii = *target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000278
Vikram S. Adve7952d602003-05-31 07:37:05 +0000279 // Instructions in memNodeVec are in execution order within the basic block,
280 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
281 //
282 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000283 if (mii.isCall(callDepNodeVec[ic]->getOpcode())) {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000284 // Add SG_CALL_REF edges from all preds to this instruction.
285 for (unsigned jc=0; jc < ic; jc++)
286 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
287 SchedGraphEdge::MachineRegister,
288 MachineIntRegsRID, 0);
289
290 // And do the same from this instruction to all successors.
291 for (unsigned jc=ic+1; jc < NC; jc++)
292 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
293 SchedGraphEdge::MachineRegister,
294 MachineIntRegsRID, 0);
295 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000296
297#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000298 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000299 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000300 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000301 if (mii.isCall(memNodeVec[im]->getOpcode()))
Vikram S. Advee64574c2001-11-08 05:20:23 +0000302 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000303
Vikram S. Advee64574c2001-11-08 05:20:23 +0000304 // Now walk the entire basic block, looking for CC instructions *and*
305 // call instructions, and keep track of the order of the instructions.
306 // Use the call node vec to quickly find earlier and later call nodes
307 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000308 //
309 int lastCallNodeIdx = -1;
310 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000311 if (mii.isCall(bbMvec[i]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000312 ++lastCallNodeIdx;
313 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
314 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
315 break;
316 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000317 }
Brian Gaeke918cdd42004-02-12 01:34:05 +0000318 else if (mii.isCCInstr(bbMvec[i]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000319 // Add incoming/outgoing edges from/to preceding/later calls
320 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
321 int j=0;
322 for ( ; j <= lastCallNodeIdx; j++)
323 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
324 MachineCCRegsRID, 0);
325 for ( ; j < (int) callNodeVec.size(); j++)
326 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
327 MachineCCRegsRID, 0);
328 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000329#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000330}
331
332
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000333void SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
334 const TargetMachine& target) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000335 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000336 // not aliased!
337 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000338 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000339 I != regToRefVecMap.end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000340 int regNum = (*I).first;
341 RefVec& regRefVec = (*I).second;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000342
Misha Brukman6b77ec42003-05-22 21:49:18 +0000343 // regRefVec is ordered by control flow order in the basic block
344 for (unsigned i=0; i < regRefVec.size(); ++i) {
345 SchedGraphNode* node = regRefVec[i].first;
346 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000347 const MachineOperand& mop =
348 node->getMachineInstr()->getExplOrImplOperand(opNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000349 bool isDef = mop.isDef() && !mop.isUse();
350 bool isDefAndUse = mop.isDef() && mop.isUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000351
Misha Brukman6b77ec42003-05-22 21:49:18 +0000352 for (unsigned p=0; p < i; ++p) {
353 SchedGraphNode* prevNode = regRefVec[p].first;
354 if (prevNode != node) {
355 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000356 const MachineOperand& prevMop =
357 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000358 bool prevIsDef = prevMop.isDef() && !prevMop.isUse();
359 bool prevIsDefAndUse = prevMop.isDef() && prevMop.isUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000360 if (isDef) {
361 if (prevIsDef)
362 new SchedGraphEdge(prevNode, node, regNum,
363 SchedGraphEdge::OutputDep);
364 if (!prevIsDef || prevIsDefAndUse)
365 new SchedGraphEdge(prevNode, node, regNum,
366 SchedGraphEdge::AntiDep);
367 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000368
Misha Brukman6b77ec42003-05-22 21:49:18 +0000369 if (prevIsDef)
370 if (!isDef || isDefAndUse)
371 new SchedGraphEdge(prevNode, node, regNum,
372 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000373 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000374 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000375 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000376 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000377}
378
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000379
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000380// Adds dependences to/from refNode from/to all other defs
381// in the basic block. refNode may be a use, a def, or both.
382// We do not consider other uses because we are not building use-use deps.
383//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000384void SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
385 const RefVec& defVec,
386 const Value* defValue,
387 bool refNodeIsDef,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000388 bool refNodeIsUse,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000389 const TargetMachine& target) {
Vikram S. Adve200a4352001-11-12 18:53:43 +0000390 // Add true or output dep edges from all def nodes before refNode in BB.
391 // Add anti or output dep edges to all def nodes after refNode.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000392 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000393 if ((*I).first == refNode)
394 continue; // Dont add any self-loops
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000395
Misha Brukman6b77ec42003-05-22 21:49:18 +0000396 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB()) {
397 // (*).first is before refNode
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000398 if (refNodeIsDef && !refNodeIsUse)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000399 (void) new SchedGraphEdge((*I).first, refNode, defValue,
400 SchedGraphEdge::OutputDep);
401 if (refNodeIsUse)
402 (void) new SchedGraphEdge((*I).first, refNode, defValue,
403 SchedGraphEdge::TrueDep);
404 } else {
405 // (*).first is after refNode
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000406 if (refNodeIsDef && !refNodeIsUse)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000407 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
408 SchedGraphEdge::OutputDep);
409 if (refNodeIsUse)
410 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
411 SchedGraphEdge::AntiDep);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000412 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000413 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000414}
415
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000416
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000417void SchedGraph::addEdgesForInstruction(const MachineInstr& MI,
418 const ValueToDefVecMap& valueToDefVecMap,
419 const TargetMachine& target) {
Chris Lattner133f0792002-10-28 04:45:29 +0000420 SchedGraphNode* node = getGraphNodeForInstr(&MI);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000421 if (node == NULL)
422 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000423
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000424 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000425 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000426 for (unsigned i = 0, numOps = MI.getNumOperands(); i != numOps; ++i) {
427 switch (MI.getOperand(i).getType()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000428 case MachineOperand::MO_VirtualRegister:
429 case MachineOperand::MO_CCRegister:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000430 if (const Value* srcI = MI.getOperand(i).getVRegValue()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000431 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
432 if (I != valueToDefVecMap.end())
433 addEdgesForValue(node, I->second, srcI,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000434 MI.getOperand(i).isDef(), MI.getOperand(i).isUse(),
435 target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000436 }
437 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000438
Misha Brukman6b77ec42003-05-22 21:49:18 +0000439 case MachineOperand::MO_MachineRegister:
440 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000441
Misha Brukman6b77ec42003-05-22 21:49:18 +0000442 case MachineOperand::MO_SignExtendedImmed:
443 case MachineOperand::MO_UnextendedImmed:
444 case MachineOperand::MO_PCRelativeDisp:
Misha Brukmane2bf0a22003-11-06 00:04:11 +0000445 case MachineOperand::MO_ConstantPoolIndex:
Misha Brukman6b77ec42003-05-22 21:49:18 +0000446 break; // nothing to do for immediate fields
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000447
Misha Brukman6b77ec42003-05-22 21:49:18 +0000448 default:
449 assert(0 && "Unknown machine operand type in SchedGraph builder");
450 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000451 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000452 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000453
454 // Add edges for values implicitly used by the machine instruction.
455 // Examples include function arguments to a Call instructions or the return
456 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000457 //
Chris Lattner133f0792002-10-28 04:45:29 +0000458 for (unsigned i=0, N=MI.getNumImplicitRefs(); i < N; ++i)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000459 if (MI.getImplicitOp(i).isUse())
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000460 if (const Value* srcI = MI.getImplicitRef(i)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000461 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
462 if (I != valueToDefVecMap.end())
463 addEdgesForValue(node, I->second, srcI,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000464 MI.getImplicitOp(i).isDef(),
465 MI.getImplicitOp(i).isUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000466 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000467}
468
469
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000470void SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
471 SchedGraphNode* node,
472 std::vector<SchedGraphNode*>& memNodeVec,
473 std::vector<SchedGraphNode*>& callDepNodeVec,
474 RegToRefVecMap& regToRefVecMap,
475 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner98107ff2004-06-02 06:06:20 +0000476 const TargetInstrInfo& mii = *target.getInstrInfo();
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000477
Brian Gaeke918cdd42004-02-12 01:34:05 +0000478 MachineOpCode opCode = node->getOpcode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000479
Vikram S. Adve7952d602003-05-31 07:37:05 +0000480 if (mii.isCall(opCode) || mii.isCCInstr(opCode))
481 callDepNodeVec.push_back(node);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000482
Vikram S. Advee64574c2001-11-08 05:20:23 +0000483 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
484 memNodeVec.push_back(node);
485
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000486 // Collect the register references and value defs. for explicit operands
487 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000488 const MachineInstr& MI = *node->getMachineInstr();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000489 for (int i=0, numOps = (int) MI.getNumOperands(); i < numOps; i++) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000490 const MachineOperand& mop = MI.getOperand(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000491
Misha Brukman6b77ec42003-05-22 21:49:18 +0000492 // if this references a register other than the hardwired
493 // "zero" register, record the reference.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000494 if (mop.hasAllocatedReg()) {
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000495 unsigned regNum = mop.getReg();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000496
Vikram S. Adve7952d602003-05-31 07:37:05 +0000497 // If this is not a dummy zero register, record the reference in order
Chris Lattner98107ff2004-06-02 06:06:20 +0000498 if (regNum != target.getRegInfo()->getZeroRegNum())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000499 regToRefVecMap[mop.getReg()]
Misha Brukman6b77ec42003-05-22 21:49:18 +0000500 .push_back(std::make_pair(node, i));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000501
502 // If this is a volatile register, add the instruction to callDepVec
503 // (only if the node is not already on the callDepVec!)
504 if (callDepNodeVec.size() == 0 || callDepNodeVec.back() != node)
505 {
506 unsigned rcid;
Chris Lattner98107ff2004-06-02 06:06:20 +0000507 int regInClass = target.getRegInfo()->getClassRegNum(regNum, rcid);
508 if (target.getRegInfo()->getMachineRegClass(rcid)
Vikram S. Adve7952d602003-05-31 07:37:05 +0000509 ->isRegVolatile(regInClass))
510 callDepNodeVec.push_back(node);
511 }
512
Misha Brukman6b77ec42003-05-22 21:49:18 +0000513 continue; // nothing more to do
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000514 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000515
Misha Brukman6b77ec42003-05-22 21:49:18 +0000516 // ignore all other non-def operands
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000517 if (!MI.getOperand(i).isDef())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000518 continue;
519
520 // We must be defining a value.
521 assert((mop.getType() == MachineOperand::MO_VirtualRegister ||
522 mop.getType() == MachineOperand::MO_CCRegister)
523 && "Do not expect any other kind of operand to be defined!");
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000524 assert(mop.getVRegValue() != NULL && "Null value being defined?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000525
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000526 valueToDefVecMap[mop.getVRegValue()].push_back(std::make_pair(node, i));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000527 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000528
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000529 //
Vikram S. Adve7952d602003-05-31 07:37:05 +0000530 // Collect value defs. for implicit operands. They may have allocated
531 // physical registers also.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000532 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000533 for (unsigned i=0, N = MI.getNumImplicitRefs(); i != N; ++i) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000534 const MachineOperand& mop = MI.getImplicitOp(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000535 if (mop.hasAllocatedReg()) {
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000536 unsigned regNum = mop.getReg();
Chris Lattner98107ff2004-06-02 06:06:20 +0000537 if (regNum != target.getRegInfo()->getZeroRegNum())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000538 regToRefVecMap[mop.getReg()]
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000539 .push_back(std::make_pair(node, i + MI.getNumOperands()));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000540 continue; // nothing more to do
541 }
542
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000543 if (mop.isDef()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000544 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000545 valueToDefVecMap[MI.getImplicitRef(i)].push_back(
546 std::make_pair(node, -i));
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000547 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000548 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000549}
550
551
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000552void SchedGraph::buildNodesForBB(const TargetMachine& target,
553 MachineBasicBlock& MBB,
554 std::vector<SchedGraphNode*>& memNodeVec,
555 std::vector<SchedGraphNode*>& callDepNodeVec,
556 RegToRefVecMap& regToRefVecMap,
557 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner98107ff2004-06-02 06:06:20 +0000558 const TargetInstrInfo& mii = *target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000559
560 // Build graph nodes for each VM instruction and gather def/use info.
561 // Do both those together in a single pass over all machine instructions.
Chris Lattnere6d04f12004-02-18 16:43:51 +0000562 unsigned i = 0;
563 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;
564 ++I, ++i)
Brian Gaeke418379e2004-08-18 20:04:24 +0000565 if (I->getOpcode() != V9::PHI) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000566 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
Chris Lattnere6d04f12004-02-18 16:43:51 +0000567 noteGraphNodeForInstr(I, node);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000568
569 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000570 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
571 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000572 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000573}
574
575
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000576void SchedGraph::buildGraph(const TargetMachine& target) {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000577 // Use this data structure to note all machine operands that compute
578 // ordinary LLVM values. These must be computed defs (i.e., instructions).
579 // Note that there may be multiple machine instructions that define
580 // each Value.
581 ValueToDefVecMap valueToDefVecMap;
582
Vikram S. Advee64574c2001-11-08 05:20:23 +0000583 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000584 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000585 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000586
587 // Use this data structure to note all instructions that access physical
588 // registers that can be modified by a call (including call instructions)
589 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000590
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000591 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000592 // machine registers so we can add edges for those later without
593 // extra passes over the nodes.
594 // The vector holds an ordered list of references to the machine reg,
595 // ordered according to control-flow order. This only works for a
596 // single basic block, hence the assertion. Each reference is identified
597 // by the pair: <node, operand-number>.
598 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000599 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000600
601 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000602 graphRoot = new SchedGraphNode(0, NULL, -1, target);
603 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000604
605 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000606 // First add nodes for all the machine instructions in the basic block
607 // because this greatly simplifies identifying which edges to add.
608 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000609 // Also, remember the load/store instructions to add memory deps later.
610 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000611
Vikram S. Adve7952d602003-05-31 07:37:05 +0000612 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
613 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000614
615 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000616 // Now add edges for the following (all are incoming edges except (4)):
617 // (1) operands of the machine instruction, including hidden operands
618 // (2) machine register dependences
619 // (3) memory load/store dependences
620 // (3) other resource dependences for the machine instruction, if any
621 // (4) output dependences when multiple machine instructions define the
622 // same value; all must have been generated from a single VM instrn
623 // (5) control dependences to branch instructions generated for the
624 // terminator instruction of the BB. Because of delay slots and
625 // 2-way conditional branches, multiple CD edges are needed
626 // (see addCDEdges for details).
627 // Also, note any uses or defs of machine registers.
628 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000629 //----------------------------------------------------------------
630
631 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000632 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000633
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000634 // Then add memory dep edges: store->load, load->store, and store->store.
635 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000636 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000637
638 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000639 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000640
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000641 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnere6d04f12004-02-18 16:43:51 +0000642 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
643 addEdgesForInstruction(*I, valueToDefVecMap, target);
Brian Gaeke0dc57532004-02-09 18:42:05 +0000644
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000645 // Then add edges for dependences on machine registers
646 this->addMachineRegEdges(regToRefVecMap, target);
Brian Gaeke0dc57532004-02-09 18:42:05 +0000647
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000648 // Finally, add edges from the dummy root and to dummy leaf
649 this->addDummyEdges();
650}
651
652
653//
654// class SchedGraphSet
655//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000656SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000657 const TargetMachine& target) :
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000658 function(_function) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000659 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000660}
661
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000662SchedGraphSet::~SchedGraphSet() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000663 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000664 for(iterator I = begin(), E = end(); I != E; ++I)
665 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000666}
667
668
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000669void SchedGraphSet::dump() const {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000670 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000671 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000672
673 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000674 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000675
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000676 std::cerr << "\n====== End 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
680
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000681void SchedGraphSet::buildGraphsForMethod(const Function *F,
682 const TargetMachine& target) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000683 MachineFunction &MF = MachineFunction::get(F);
684 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
685 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000686}
687
688
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000689void SchedGraphEdge::print(std::ostream &os) const {
690 os << "edge [" << src->getNodeId() << "] -> ["
691 << sink->getNodeId() << "] : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000692
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000693 switch(depType) {
694 case SchedGraphEdge::CtrlDep:
695 os<< "Control Dep";
696 break;
697 case SchedGraphEdge::ValueDep:
Chris Lattnerf51c7f562004-07-15 02:40:04 +0000698 os<< "Reg Value " << *val;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000699 break;
700 case SchedGraphEdge::MemoryDep:
701 os<< "Memory Dep";
702 break;
703 case SchedGraphEdge::MachineRegister:
704 os<< "Reg " << machineRegNum;
705 break;
706 case SchedGraphEdge::MachineResource:
707 os<<"Resource "<< resourceId;
708 break;
709 default:
710 assert(0);
711 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000712 }
713
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000714 os << " : delay = " << minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000715}
716
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000717void SchedGraphNode::print(std::ostream &os) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000718 os << std::string(8, ' ')
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000719 << "Node " << ID << " : "
720 << "latency = " << latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000721
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000722 if (getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000723 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000724 else {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000725 os << *getMachineInstr() << "\n" << std::string(12, ' ');
726 os << inEdges.size() << " Incoming Edges:\n";
727 for (unsigned i=0, N = inEdges.size(); i < N; i++)
728 os << std::string(16, ' ') << *inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000729
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000730 os << std::string(12, ' ') << outEdges.size()
Misha Brukman6b77ec42003-05-22 21:49:18 +0000731 << " Outgoing Edges:\n";
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000732 for (unsigned i=0, N= outEdges.size(); i < N; i++)
733 os << std::string(16, ' ') << *outEdges[i];
Misha Brukman6b77ec42003-05-22 21:49:18 +0000734 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000735}
Brian Gaeked0fde302003-11-11 22:41:34 +0000736
737} // End llvm namespace