blob: 01ca36ff6a18ffc254cfb0485f6a8478fa0a5432 [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)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000056 : SchedGraphNodeCommon(NID,indexInBB), MBB(mbb),
57 MI(mbb ? &(*mbb)[indexInBB] : (MachineInstr*)0) {
Tanya Lattnerc50ee552003-08-27 02:42:58 +000058 if (MI) {
Brian Gaeke918cdd42004-02-12 01:34:05 +000059 MachineOpCode mopCode = MI->getOpcode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +000060 latency = Target.getInstrInfo().hasResultInterlock(mopCode)
61 ? Target.getInstrInfo().minLatency(mopCode)
62 : Target.getInstrInfo().maxLatency(mopCode);
63 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +000064}
65
John Criswellc9afb492003-08-28 21:43:17 +000066//
67// Method: SchedGraphNode Destructor
68//
69// Description:
70// Free memory allocated by the SchedGraphNode object.
71//
72// Notes:
73// Do not delete the edges here. The base class will take care of that.
74// Only handle subclass specific stuff here (where currently there is
75// none).
76//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000077SchedGraphNode::~SchedGraphNode() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +000078}
79
Vikram S. Adve78ef1392001-08-28 23:06:02 +000080//
81// class SchedGraph
82//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000083SchedGraph::SchedGraph(MachineBasicBlock &mbb, const TargetMachine& target)
84 : MBB(mbb) {
Chris Lattner697954c2002-01-20 22:54:45 +000085 buildGraph(target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +000086}
87
John Criswellc9afb492003-08-28 21:43:17 +000088//
89// Method: SchedGraph Destructor
90//
91// Description:
92// This method deletes memory allocated by the SchedGraph object.
93//
94// Notes:
95// Do not delete the graphRoot or graphLeaf here. The base class handles
96// that bit of work.
97//
Tanya Lattnerc50ee552003-08-27 02:42:58 +000098SchedGraph::~SchedGraph() {
Chris Lattner697954c2002-01-20 22:54:45 +000099 for (const_iterator I = begin(); I != end(); ++I)
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000100 delete I->second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000101}
102
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000103void SchedGraph::dump() const {
Brian Gaeke0dc57532004-02-09 18:42:05 +0000104 std::cerr << " Sched Graph for Basic Block: "
105 << MBB.getBasicBlock()->getName()
106 << " (" << MBB.getBasicBlock() << ")"
107 << "\n\n Actual Root nodes: ";
108 for (SchedGraphNodeCommon::const_iterator I = graphRoot->beginOutEdges(),
109 E = graphRoot->endOutEdges();
110 I != E; ++I) {
111 std::cerr << (*I)->getSink ()->getNodeId ();
112 if (I + 1 != E) { std::cerr << ", "; }
113 }
Misha Brukmanc2312df2003-05-22 21:24:35 +0000114 std::cerr << "\n Graph Nodes:\n";
Brian Gaeke0dc57532004-02-09 18:42:05 +0000115 for (const_iterator I = begin(), E = end(); I != E; ++I)
Misha Brukmanc2312df2003-05-22 21:24:35 +0000116 std::cerr << "\n" << *I->second;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000117 std::cerr << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000118}
119
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000120void SchedGraph::addDummyEdges() {
Brian Gaeke0dc57532004-02-09 18:42:05 +0000121 assert(graphRoot->getNumOutEdges() == 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000122
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000123 for (const_iterator I=begin(); I != end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000124 SchedGraphNode* node = (*I).second;
125 assert(node != graphRoot && node != graphLeaf);
126 if (node->beginInEdges() == node->endInEdges())
127 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
128 SchedGraphEdge::NonDataDep, 0);
129 if (node->beginOutEdges() == node->endOutEdges())
130 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
131 SchedGraphEdge::NonDataDep, 0);
132 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000133}
134
135
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000136void SchedGraph::addCDEdges(const TerminatorInst* term,
137 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000138 const TargetInstrInfo& mii = target.getInstrInfo();
Chris Lattner0861b0c2002-02-03 07:29:45 +0000139 MachineCodeForInstruction &termMvec = MachineCodeForInstruction::get(term);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000140
141 // Find the first branch instr in the sequence of machine instrs for term
142 //
143 unsigned first = 0;
Brian Gaeke918cdd42004-02-12 01:34:05 +0000144 while (! mii.isBranch(termMvec[first]->getOpcode()) &&
145 ! mii.isReturn(termMvec[first]->getOpcode()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000146 ++first;
147 assert(first < termMvec.size() &&
Vikram S. Adveacf0f702002-10-13 00:39:22 +0000148 "No branch instructions for terminator? Ok, but weird!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000149 if (first == termMvec.size())
150 return;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000151
Chris Lattnerb0cfa6d2002-08-09 18:55:18 +0000152 SchedGraphNode* firstBrNode = getGraphNodeForInstr(termMvec[first]);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000153
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000154 // Add CD edges from each instruction in the sequence to the
155 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000156 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000157 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000158 for (unsigned i = termMvec.size(); i > first+1; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000159 SchedGraphNode* toNode = getGraphNodeForInstr(termMvec[i-1]);
160 assert(toNode && "No node for instr generated for branch/ret?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000161
Misha Brukman6b77ec42003-05-22 21:49:18 +0000162 for (unsigned j = i-1; j != 0; --j)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000163 if (mii.isBranch(termMvec[j-1]->getOpcode()) ||
164 mii.isReturn(termMvec[j-1]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000165 SchedGraphNode* brNode = getGraphNodeForInstr(termMvec[j-1]);
166 assert(brNode && "No node for instr generated for branch/ret?");
167 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
168 SchedGraphEdge::NonDataDep, 0);
169 break; // only one incoming edge is enough
170 }
171 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000172
173 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000174 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000175 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000176 for (unsigned i = first; i != 0; --i) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000177 SchedGraphNode* fromNode = getGraphNodeForInstr(termMvec[i-1]);
178 assert(fromNode && "No node for instr generated for branch?");
179 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
180 SchedGraphEdge::NonDataDep, 0);
181 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000182
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000183 // Now add CD edges to the first branch instruction in the sequence from
184 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000185 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000186 for (unsigned i=0, N=MBB.size(); i < N; i++) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000187 if (&MBB[i] == termMvec[first]) // reached the first branch
Misha Brukman6b77ec42003-05-22 21:49:18 +0000188 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000189
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000190 SchedGraphNode* fromNode = this->getGraphNodeForInstr(&MBB[i]);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000191 if (fromNode == NULL)
192 continue; // dummy instruction, e.g., PHI
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000193
Misha Brukman6b77ec42003-05-22 21:49:18 +0000194 (void) new SchedGraphEdge(fromNode, firstBrNode,
195 SchedGraphEdge::CtrlDep,
196 SchedGraphEdge::NonDataDep, 0);
197
198 // If we find any other machine instructions (other than due to
199 // the terminator) that also have delay slots, add an outgoing edge
200 // from the instruction to the instructions in the delay slots.
201 //
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000202 unsigned d = mii.getNumDelaySlots(MBB[i].getOpcode());
Misha Brukman6b77ec42003-05-22 21:49:18 +0000203 assert(i+d < N && "Insufficient delay slots for instruction?");
204
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000205 for (unsigned j=1; j <= d; j++) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000206 SchedGraphNode* toNode = this->getGraphNodeForInstr(&MBB[i+j]);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000207 assert(toNode && "No node for machine instr in delay slot?");
208 (void) new SchedGraphEdge(fromNode, toNode,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000209 SchedGraphEdge::CtrlDep,
210 SchedGraphEdge::NonDataDep, 0);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000211 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000212 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000213}
214
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000215static const int SG_LOAD_REF = 0;
216static const int SG_STORE_REF = 1;
217static const int SG_CALL_REF = 2;
218
219static const unsigned int SG_DepOrderArray[][3] = {
220 { SchedGraphEdge::NonDataDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000221 SchedGraphEdge::AntiDep,
222 SchedGraphEdge::AntiDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000223 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000224 SchedGraphEdge::OutputDep,
225 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000226 { SchedGraphEdge::TrueDep,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000227 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
228 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
229 | SchedGraphEdge::OutputDep }
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000230};
231
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000232
Vikram S. Advee64574c2001-11-08 05:20:23 +0000233// Add a dependence edge between every pair of machine load/store/call
234// instructions, where at least one is a store or a call.
235// Use latency 1 just to ensure that memory operations are ordered;
236// latency does not otherwise matter (true dependences enforce that).
237//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000238void SchedGraph::addMemEdges(const std::vector<SchedGraphNode*>& memNodeVec,
239 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000240 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000241
Vikram S. Advee64574c2001-11-08 05:20:23 +0000242 // Instructions in memNodeVec are in execution order within the basic block,
243 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
244 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000245 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000246 MachineOpCode fromOpCode = memNodeVec[im]->getOpcode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000247 int fromType = (mii.isCall(fromOpCode)? SG_CALL_REF
248 : (mii.isLoad(fromOpCode)? SG_LOAD_REF
249 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000250 for (unsigned jm=im+1; jm < NM; jm++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000251 MachineOpCode toOpCode = memNodeVec[jm]->getOpcode();
Vikram S. Adve7952d602003-05-31 07:37:05 +0000252 int toType = (mii.isCall(toOpCode)? SG_CALL_REF
253 : (mii.isLoad(toOpCode)? SG_LOAD_REF
254 : SG_STORE_REF));
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000255
Misha Brukman6b77ec42003-05-22 21:49:18 +0000256 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
257 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
258 SchedGraphEdge::MemoryDep,
259 SG_DepOrderArray[fromType][toType], 1);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000260 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000261 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000262}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000263
Vikram S. Advee64574c2001-11-08 05:20:23 +0000264// Add edges from/to CC reg instrs to/from call instrs.
265// Essentially this prevents anything that sets or uses a CC reg from being
266// reordered w.r.t. a call.
267// Use a latency of 0 because we only need to prevent out-of-order issue,
268// like with control dependences.
269//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000270void SchedGraph::addCallDepEdges(const std::vector<SchedGraphNode*>& callDepNodeVec,
271 const TargetMachine& target) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000272 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000273
Vikram S. Adve7952d602003-05-31 07:37:05 +0000274 // Instructions in memNodeVec are in execution order within the basic block,
275 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
276 //
277 for (unsigned ic=0, NC=callDepNodeVec.size(); ic < NC; ic++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000278 if (mii.isCall(callDepNodeVec[ic]->getOpcode())) {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000279 // Add SG_CALL_REF edges from all preds to this instruction.
280 for (unsigned jc=0; jc < ic; jc++)
281 (void) new SchedGraphEdge(callDepNodeVec[jc], callDepNodeVec[ic],
282 SchedGraphEdge::MachineRegister,
283 MachineIntRegsRID, 0);
284
285 // And do the same from this instruction to all successors.
286 for (unsigned jc=ic+1; jc < NC; jc++)
287 (void) new SchedGraphEdge(callDepNodeVec[ic], callDepNodeVec[jc],
288 SchedGraphEdge::MachineRegister,
289 MachineIntRegsRID, 0);
290 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000291
292#ifdef CALL_DEP_NODE_VEC_CANNOT_WORK
Vikram S. Advee64574c2001-11-08 05:20:23 +0000293 // Find the call instruction nodes and put them in a vector.
Vikram S. Adve7952d602003-05-31 07:37:05 +0000294 std::vector<SchedGraphNode*> callNodeVec;
Vikram S. Advee64574c2001-11-08 05:20:23 +0000295 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000296 if (mii.isCall(memNodeVec[im]->getOpcode()))
Vikram S. Advee64574c2001-11-08 05:20:23 +0000297 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000298
Vikram S. Advee64574c2001-11-08 05:20:23 +0000299 // Now walk the entire basic block, looking for CC instructions *and*
300 // call instructions, and keep track of the order of the instructions.
301 // Use the call node vec to quickly find earlier and later call nodes
302 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000303 //
304 int lastCallNodeIdx = -1;
305 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000306 if (mii.isCall(bbMvec[i]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000307 ++lastCallNodeIdx;
308 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
309 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
310 break;
311 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
Vikram S. Adve7952d602003-05-31 07:37:05 +0000312 }
Brian Gaeke918cdd42004-02-12 01:34:05 +0000313 else if (mii.isCCInstr(bbMvec[i]->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000314 // Add incoming/outgoing edges from/to preceding/later calls
315 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
316 int j=0;
317 for ( ; j <= lastCallNodeIdx; j++)
318 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
319 MachineCCRegsRID, 0);
320 for ( ; j < (int) callNodeVec.size(); j++)
321 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
322 MachineCCRegsRID, 0);
323 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000324#endif
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000325}
326
327
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000328void SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
329 const TargetMachine& target) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000330 // This code assumes that two registers with different numbers are
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000331 // not aliased!
332 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000333 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000334 I != regToRefVecMap.end(); ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000335 int regNum = (*I).first;
336 RefVec& regRefVec = (*I).second;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000337
Misha Brukman6b77ec42003-05-22 21:49:18 +0000338 // regRefVec is ordered by control flow order in the basic block
339 for (unsigned i=0; i < regRefVec.size(); ++i) {
340 SchedGraphNode* node = regRefVec[i].first;
341 unsigned int opNum = regRefVec[i].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000342 const MachineOperand& mop =
343 node->getMachineInstr()->getExplOrImplOperand(opNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000344 bool isDef = mop.isDef() && !mop.isUse();
345 bool isDefAndUse = mop.isDef() && mop.isUse();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000346
Misha Brukman6b77ec42003-05-22 21:49:18 +0000347 for (unsigned p=0; p < i; ++p) {
348 SchedGraphNode* prevNode = regRefVec[p].first;
349 if (prevNode != node) {
350 unsigned int prevOpNum = regRefVec[p].second;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000351 const MachineOperand& prevMop =
352 prevNode->getMachineInstr()->getExplOrImplOperand(prevOpNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000353 bool prevIsDef = prevMop.isDef() && !prevMop.isUse();
354 bool prevIsDefAndUse = prevMop.isDef() && prevMop.isUse();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000355 if (isDef) {
356 if (prevIsDef)
357 new SchedGraphEdge(prevNode, node, regNum,
358 SchedGraphEdge::OutputDep);
359 if (!prevIsDef || prevIsDefAndUse)
360 new SchedGraphEdge(prevNode, node, regNum,
361 SchedGraphEdge::AntiDep);
362 }
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000363
Misha Brukman6b77ec42003-05-22 21:49:18 +0000364 if (prevIsDef)
365 if (!isDef || isDefAndUse)
366 new SchedGraphEdge(prevNode, node, regNum,
367 SchedGraphEdge::TrueDep);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000368 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000369 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000370 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000371 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000372}
373
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000374
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000375// Adds dependences to/from refNode from/to all other defs
376// in the basic block. refNode may be a use, a def, or both.
377// We do not consider other uses because we are not building use-use deps.
378//
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000379void SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
380 const RefVec& defVec,
381 const Value* defValue,
382 bool refNodeIsDef,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000383 bool refNodeIsUse,
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000384 const TargetMachine& target) {
Vikram S. Adve200a4352001-11-12 18:53:43 +0000385 // Add true or output dep edges from all def nodes before refNode in BB.
386 // Add anti or output dep edges to all def nodes after refNode.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000387 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000388 if ((*I).first == refNode)
389 continue; // Dont add any self-loops
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000390
Misha Brukman6b77ec42003-05-22 21:49:18 +0000391 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB()) {
392 // (*).first is before refNode
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000393 if (refNodeIsDef && !refNodeIsUse)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000394 (void) new SchedGraphEdge((*I).first, refNode, defValue,
395 SchedGraphEdge::OutputDep);
396 if (refNodeIsUse)
397 (void) new SchedGraphEdge((*I).first, refNode, defValue,
398 SchedGraphEdge::TrueDep);
399 } else {
400 // (*).first is after refNode
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000401 if (refNodeIsDef && !refNodeIsUse)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000402 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
403 SchedGraphEdge::OutputDep);
404 if (refNodeIsUse)
405 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
406 SchedGraphEdge::AntiDep);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000407 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000408 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000409}
410
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000411
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000412void SchedGraph::addEdgesForInstruction(const MachineInstr& MI,
413 const ValueToDefVecMap& valueToDefVecMap,
414 const TargetMachine& target) {
Chris Lattner133f0792002-10-28 04:45:29 +0000415 SchedGraphNode* node = getGraphNodeForInstr(&MI);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000416 if (node == NULL)
417 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000418
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000419 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000420 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000421 for (unsigned i = 0, numOps = MI.getNumOperands(); i != numOps; ++i) {
422 switch (MI.getOperand(i).getType()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000423 case MachineOperand::MO_VirtualRegister:
424 case MachineOperand::MO_CCRegister:
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000425 if (const Value* srcI = MI.getOperand(i).getVRegValue()) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000426 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
427 if (I != valueToDefVecMap.end())
428 addEdgesForValue(node, I->second, srcI,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000429 MI.getOperand(i).isDef(), MI.getOperand(i).isUse(),
430 target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000431 }
432 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000433
Misha Brukman6b77ec42003-05-22 21:49:18 +0000434 case MachineOperand::MO_MachineRegister:
435 break;
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000436
Misha Brukman6b77ec42003-05-22 21:49:18 +0000437 case MachineOperand::MO_SignExtendedImmed:
438 case MachineOperand::MO_UnextendedImmed:
439 case MachineOperand::MO_PCRelativeDisp:
Misha Brukmane2bf0a22003-11-06 00:04:11 +0000440 case MachineOperand::MO_ConstantPoolIndex:
Misha Brukman6b77ec42003-05-22 21:49:18 +0000441 break; // nothing to do for immediate fields
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000442
Misha Brukman6b77ec42003-05-22 21:49:18 +0000443 default:
444 assert(0 && "Unknown machine operand type in SchedGraph builder");
445 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000446 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000447 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000448
449 // Add edges for values implicitly used by the machine instruction.
450 // Examples include function arguments to a Call instructions or the return
451 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000452 //
Chris Lattner133f0792002-10-28 04:45:29 +0000453 for (unsigned i=0, N=MI.getNumImplicitRefs(); i < N; ++i)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000454 if (MI.getImplicitOp(i).isUse())
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000455 if (const Value* srcI = MI.getImplicitRef(i)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000456 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
457 if (I != valueToDefVecMap.end())
458 addEdgesForValue(node, I->second, srcI,
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000459 MI.getImplicitOp(i).isDef(),
460 MI.getImplicitOp(i).isUse(), target);
Misha Brukman6b77ec42003-05-22 21:49:18 +0000461 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000462}
463
464
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000465void SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
466 SchedGraphNode* node,
467 std::vector<SchedGraphNode*>& memNodeVec,
468 std::vector<SchedGraphNode*>& callDepNodeVec,
469 RegToRefVecMap& regToRefVecMap,
470 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000471 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000472
Brian Gaeke918cdd42004-02-12 01:34:05 +0000473 MachineOpCode opCode = node->getOpcode();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000474
Vikram S. Adve7952d602003-05-31 07:37:05 +0000475 if (mii.isCall(opCode) || mii.isCCInstr(opCode))
476 callDepNodeVec.push_back(node);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000477
Vikram S. Advee64574c2001-11-08 05:20:23 +0000478 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
479 memNodeVec.push_back(node);
480
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000481 // Collect the register references and value defs. for explicit operands
482 //
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000483 const MachineInstr& MI = *node->getMachineInstr();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000484 for (int i=0, numOps = (int) MI.getNumOperands(); i < numOps; i++) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000485 const MachineOperand& mop = MI.getOperand(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000486
Misha Brukman6b77ec42003-05-22 21:49:18 +0000487 // if this references a register other than the hardwired
488 // "zero" register, record the reference.
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000489 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000490 int regNum = mop.getAllocatedRegNum();
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000491
Vikram S. Adve7952d602003-05-31 07:37:05 +0000492 // If this is not a dummy zero register, record the reference in order
Misha Brukman6b77ec42003-05-22 21:49:18 +0000493 if (regNum != target.getRegInfo().getZeroRegNum())
Vikram S. Adve7952d602003-05-31 07:37:05 +0000494 regToRefVecMap[mop.getAllocatedRegNum()]
Misha Brukman6b77ec42003-05-22 21:49:18 +0000495 .push_back(std::make_pair(node, i));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000496
497 // If this is a volatile register, add the instruction to callDepVec
498 // (only if the node is not already on the callDepVec!)
499 if (callDepNodeVec.size() == 0 || callDepNodeVec.back() != node)
500 {
501 unsigned rcid;
502 int regInClass = target.getRegInfo().getClassRegNum(regNum, rcid);
503 if (target.getRegInfo().getMachineRegClass(rcid)
504 ->isRegVolatile(regInClass))
505 callDepNodeVec.push_back(node);
506 }
507
Misha Brukman6b77ec42003-05-22 21:49:18 +0000508 continue; // nothing more to do
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000509 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000510
Misha Brukman6b77ec42003-05-22 21:49:18 +0000511 // ignore all other non-def operands
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000512 if (!MI.getOperand(i).isDef())
Misha Brukman6b77ec42003-05-22 21:49:18 +0000513 continue;
514
515 // We must be defining a value.
516 assert((mop.getType() == MachineOperand::MO_VirtualRegister ||
517 mop.getType() == MachineOperand::MO_CCRegister)
518 && "Do not expect any other kind of operand to be defined!");
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000519 assert(mop.getVRegValue() != NULL && "Null value being defined?");
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000520
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000521 valueToDefVecMap[mop.getVRegValue()].push_back(std::make_pair(node, i));
Misha Brukman6b77ec42003-05-22 21:49:18 +0000522 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000523
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000524 //
Vikram S. Adve7952d602003-05-31 07:37:05 +0000525 // Collect value defs. for implicit operands. They may have allocated
526 // physical registers also.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000527 //
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000528 for (unsigned i=0, N = MI.getNumImplicitRefs(); i != N; ++i) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000529 const MachineOperand& mop = MI.getImplicitOp(i);
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000530 if (mop.hasAllocatedReg()) {
Vikram S. Adve7952d602003-05-31 07:37:05 +0000531 int regNum = mop.getAllocatedRegNum();
532 if (regNum != target.getRegInfo().getZeroRegNum())
533 regToRefVecMap[mop.getAllocatedRegNum()]
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000534 .push_back(std::make_pair(node, i + MI.getNumOperands()));
Vikram S. Adve7952d602003-05-31 07:37:05 +0000535 continue; // nothing more to do
536 }
537
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000538 if (mop.isDef()) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000539 assert(MI.getImplicitRef(i) != NULL && "Null value being defined?");
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000540 valueToDefVecMap[MI.getImplicitRef(i)].push_back(
541 std::make_pair(node, -i));
Vikram S. Adve74d15d32003-07-02 01:16:01 +0000542 }
Vikram S. Adve7952d602003-05-31 07:37:05 +0000543 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000544}
545
546
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000547void SchedGraph::buildNodesForBB(const TargetMachine& target,
548 MachineBasicBlock& MBB,
549 std::vector<SchedGraphNode*>& memNodeVec,
550 std::vector<SchedGraphNode*>& callDepNodeVec,
551 RegToRefVecMap& regToRefVecMap,
552 ValueToDefVecMap& valueToDefVecMap) {
Chris Lattner3501fea2003-01-14 22:00:31 +0000553 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000554
555 // Build graph nodes for each VM instruction and gather def/use info.
556 // Do both those together in a single pass over all machine instructions.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000557 for (unsigned i=0; i < MBB.size(); i++)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000558 if (!mii.isDummyPhiInstr(MBB[i].getOpcode())) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000559 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), &MBB, i, target);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000560 noteGraphNodeForInstr(&MBB[i], node);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000561
562 // Remember all register references and value defs
Vikram S. Adve7952d602003-05-31 07:37:05 +0000563 findDefUseInfoAtInstr(target, node, memNodeVec, callDepNodeVec,
564 regToRefVecMap, valueToDefVecMap);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000565 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000566}
567
568
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000569void SchedGraph::buildGraph(const TargetMachine& target) {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000570 // Use this data structure to note all machine operands that compute
571 // ordinary LLVM values. These must be computed defs (i.e., instructions).
572 // Note that there may be multiple machine instructions that define
573 // each Value.
574 ValueToDefVecMap valueToDefVecMap;
575
Vikram S. Advee64574c2001-11-08 05:20:23 +0000576 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000577 // We use this to add memory dependence edges without a second full walk.
Misha Brukmanc2312df2003-05-22 21:24:35 +0000578 std::vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve7952d602003-05-31 07:37:05 +0000579
580 // Use this data structure to note all instructions that access physical
581 // registers that can be modified by a call (including call instructions)
582 std::vector<SchedGraphNode*> callDepNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000583
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000584 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000585 // machine registers so we can add edges for those later without
586 // extra passes over the nodes.
587 // The vector holds an ordered list of references to the machine reg,
588 // ordered according to control-flow order. This only works for a
589 // single basic block, hence the assertion. Each reference is identified
590 // by the pair: <node, operand-number>.
591 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000592 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000593
594 // Make a dummy root node. We'll add edges to the real roots later.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000595 graphRoot = new SchedGraphNode(0, NULL, -1, target);
596 graphLeaf = new SchedGraphNode(1, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000597
598 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000599 // First add nodes for all the machine instructions in the basic block
600 // because this greatly simplifies identifying which edges to add.
601 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000602 // Also, remember the load/store instructions to add memory deps later.
603 //----------------------------------------------------------------
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000604
Vikram S. Adve7952d602003-05-31 07:37:05 +0000605 buildNodesForBB(target, MBB, memNodeVec, callDepNodeVec,
606 regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000607
608 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000609 // Now add edges for the following (all are incoming edges except (4)):
610 // (1) operands of the machine instruction, including hidden operands
611 // (2) machine register dependences
612 // (3) memory load/store dependences
613 // (3) other resource dependences for the machine instruction, if any
614 // (4) output dependences when multiple machine instructions define the
615 // same value; all must have been generated from a single VM instrn
616 // (5) control dependences to branch instructions generated for the
617 // terminator instruction of the BB. Because of delay slots and
618 // 2-way conditional branches, multiple CD edges are needed
619 // (see addCDEdges for details).
620 // Also, note any uses or defs of machine registers.
621 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000622 //----------------------------------------------------------------
623
624 // First, add edges to the terminator instruction of the basic block.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000625 this->addCDEdges(MBB.getBasicBlock()->getTerminator(), target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000626
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000627 // Then add memory dep edges: store->load, load->store, and store->store.
628 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000629 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000630
631 // Then add edges between call instructions and CC set/use instructions
Vikram S. Adve7952d602003-05-31 07:37:05 +0000632 this->addCallDepEdges(callDepNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000633
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000634 // Then add incoming def-use (SSA) edges for each machine instruction.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000635 for (unsigned i=0, N=MBB.size(); i < N; i++)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000636 addEdgesForInstruction(MBB[i], valueToDefVecMap, target);
Brian Gaeke0dc57532004-02-09 18:42:05 +0000637
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000638 // Then add edges for dependences on machine registers
639 this->addMachineRegEdges(regToRefVecMap, target);
Brian Gaeke0dc57532004-02-09 18:42:05 +0000640
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000641 // Finally, add edges from the dummy root and to dummy leaf
642 this->addDummyEdges();
643}
644
645
646//
647// class SchedGraphSet
648//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000649SchedGraphSet::SchedGraphSet(const Function* _function,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000650 const TargetMachine& target) :
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000651 function(_function) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000652 buildGraphsForMethod(function, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000653}
654
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000655SchedGraphSet::~SchedGraphSet() {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000656 // delete all the graphs
Chris Lattnerf3dd05c2002-04-09 05:15:33 +0000657 for(iterator I = begin(), E = end(); I != E; ++I)
658 delete *I; // destructor is a friend
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000659}
660
661
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000662void SchedGraphSet::dump() const {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000663 std::cerr << "======== Sched graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000664 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000665
666 for (const_iterator I=begin(); I != end(); ++I)
Vikram S. Advecf8a98f2002-03-24 03:40:59 +0000667 (*I)->dump();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000668
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000669 std::cerr << "\n====== End graphs for function `" << function->getName()
Misha Brukmanc2312df2003-05-22 21:24:35 +0000670 << "' ========\n\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000671}
672
673
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000674void SchedGraphSet::buildGraphsForMethod(const Function *F,
675 const TargetMachine& target) {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000676 MachineFunction &MF = MachineFunction::get(F);
677 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
678 addGraph(new SchedGraph(*I, target));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000679}
680
681
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000682void SchedGraphEdge::print(std::ostream &os) const {
683 os << "edge [" << src->getNodeId() << "] -> ["
684 << sink->getNodeId() << "] : ";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000685
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000686 switch(depType) {
687 case SchedGraphEdge::CtrlDep:
688 os<< "Control Dep";
689 break;
690 case SchedGraphEdge::ValueDep:
691 os<< "Reg Value " << val;
692 break;
693 case SchedGraphEdge::MemoryDep:
694 os<< "Memory Dep";
695 break;
696 case SchedGraphEdge::MachineRegister:
697 os<< "Reg " << machineRegNum;
698 break;
699 case SchedGraphEdge::MachineResource:
700 os<<"Resource "<< resourceId;
701 break;
702 default:
703 assert(0);
704 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000705 }
706
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000707 os << " : delay = " << minDelay << "\n";
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000708}
709
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000710void SchedGraphNode::print(std::ostream &os) const {
Chris Lattner697954c2002-01-20 22:54:45 +0000711 os << std::string(8, ' ')
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000712 << "Node " << ID << " : "
713 << "latency = " << latency << "\n" << std::string(12, ' ');
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000714
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000715 if (getMachineInstr() == NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000716 os << "(Dummy node)\n";
Misha Brukman6b77ec42003-05-22 21:49:18 +0000717 else {
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000718 os << *getMachineInstr() << "\n" << std::string(12, ' ');
719 os << inEdges.size() << " Incoming Edges:\n";
720 for (unsigned i=0, N = inEdges.size(); i < N; i++)
721 os << std::string(16, ' ') << *inEdges[i];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000722
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000723 os << std::string(12, ' ') << outEdges.size()
Misha Brukman6b77ec42003-05-22 21:49:18 +0000724 << " Outgoing Edges:\n";
Tanya Lattnerc50ee552003-08-27 02:42:58 +0000725 for (unsigned i=0, N= outEdges.size(); i < N; i++)
726 os << std::string(16, ' ') << *outEdges[i];
Misha Brukman6b77ec42003-05-22 21:49:18 +0000727 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000728}
Brian Gaeked0fde302003-11-11 22:41:34 +0000729
730} // End llvm namespace