blob: 2b246ca674edee38d9a536fc341c680432c816e8 [file] [log] [blame]
Misha Brukman6a90f822004-08-02 14:02:21 +00001//===-- MSchedGraph.cpp - Scheduling Graph ----------------------*- C++ -*-===//
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +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//===----------------------------------------------------------------------===//
9//
10// A graph class for dependencies
11//
12//===----------------------------------------------------------------------===//
13#define DEBUG_TYPE "ModuloSched"
14
15#include "MSchedGraph.h"
Tanya Lattner4cffb582004-05-26 06:27:18 +000016#include "../../Target/SparcV9/SparcV9RegisterInfo.h"
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000017#include "llvm/CodeGen/MachineBasicBlock.h"
18#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/Debug.h"
Misha Brukman6a90f822004-08-02 14:02:21 +000020#include <cstdlib>
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +000021#include <algorithm>
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000022using namespace llvm;
23
24MSchedGraphNode::MSchedGraphNode(const MachineInstr* inst,
25 MSchedGraph *graph,
Tanya Lattner4cffb582004-05-26 06:27:18 +000026 unsigned late, bool isBranch)
27 : Inst(inst), Parent(graph), latency(late), isBranchInstr(isBranch) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000028
29 //Add to the graph
30 graph->addNode(inst, this);
31}
32
33void MSchedGraphNode::print(std::ostream &os) const {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000034 os << "MSchedGraphNode: Inst=" << *Inst << ", latency= " << latency << "\n";
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000035}
36
37MSchedGraphEdge MSchedGraphNode::getInEdge(MSchedGraphNode *pred) {
38 //Loop over all the successors of our predecessor
39 //return the edge the corresponds to this in edge
Misha Brukman6a90f822004-08-02 14:02:21 +000040 for (MSchedGraphNode::succ_iterator I = pred->succ_begin(),
41 E = pred->succ_end(); I != E; ++I) {
42 if (*I == this)
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000043 return I.getEdge();
44 }
45 assert(0 && "Should have found edge between this node and its predecessor!");
Misha Brukman6a90f822004-08-02 14:02:21 +000046 abort();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000047}
48
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000049unsigned MSchedGraphNode::getInEdgeNum(MSchedGraphNode *pred) {
50 //Loop over all the successors of our predecessor
51 //return the edge the corresponds to this in edge
52 int count = 0;
53 for(MSchedGraphNode::succ_iterator I = pred->succ_begin(), E = pred->succ_end();
54 I != E; ++I) {
55 if(*I == this)
56 return count;
57 count++;
58 }
59 assert(0 && "Should have found edge between this node and its predecessor!");
60 abort();
61}
62bool MSchedGraphNode::isSuccessor(MSchedGraphNode *succ) {
63 for(succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
64 if(*I == succ)
65 return true;
66 return false;
67}
68
69
70bool MSchedGraphNode::isPredecessor(MSchedGraphNode *pred) {
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +000071 if(std::find( Predecessors.begin(), Predecessors.end(), pred) != Predecessors.end())
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000072 return true;
73 else
74 return false;
75}
76
77
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000078void MSchedGraph::addNode(const MachineInstr *MI,
79 MSchedGraphNode *node) {
80
81 //Make sure node does not already exist
82 assert(GraphMap.find(MI) == GraphMap.end()
83 && "New MSchedGraphNode already exists for this instruction");
84
85 GraphMap[MI] = node;
86}
87
88MSchedGraph::MSchedGraph(const MachineBasicBlock *bb, const TargetMachine &targ)
89 : BB(bb), Target(targ) {
90
91 //Make sure BB is not null,
92 assert(BB != NULL && "Basic Block is null");
93
Tanya Lattner420025b2004-10-10 22:44:35 +000094 //DEBUG(std::cerr << "Constructing graph for " << bb << "\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000095
96 //Create nodes and edges for this BB
97 buildNodesAndEdges();
98}
99
100MSchedGraph::~MSchedGraph () {
101 for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I)
102 delete I->second;
103}
104
105void MSchedGraph::buildNodesAndEdges() {
106
107 //Get Machine target information for calculating latency
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000108 const TargetInstrInfo *MTI = Target.getInstrInfo();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000109
110 std::vector<MSchedGraphNode*> memInstructions;
111 std::map<int, std::vector<OpIndexNodePair> > regNumtoNodeMap;
112 std::map<const Value*, std::vector<OpIndexNodePair> > valuetoNodeMap;
113
114 //Save PHI instructions to deal with later
115 std::vector<const MachineInstr*> phiInstrs;
116
117 //Loop over instructions in MBB and add nodes and edges
118 for (MachineBasicBlock::const_iterator MI = BB->begin(), e = BB->end(); MI != e; ++MI) {
119 //Get each instruction of machine basic block, get the delay
120 //using the op code, create a new node for it, and add to the
121 //graph.
122
Tanya Lattner4cffb582004-05-26 06:27:18 +0000123 MachineOpCode opCode = MI->getOpcode();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000124 int delay;
125
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000126#if 0 // FIXME: LOOK INTO THIS
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000127 //Check if subsequent instructions can be issued before
128 //the result is ready, if so use min delay.
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000129 if(MTI->hasResultInterlock(MIopCode))
130 delay = MTI->minLatency(MIopCode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000131 else
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000132#endif
Tanya Lattner4cffb582004-05-26 06:27:18 +0000133 //Get delay
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000134 delay = MTI->maxLatency(opCode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000135
136 //Create new node for this machine instruction and add to the graph.
137 //Create only if not a nop
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000138 if(MTI->isNop(opCode))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000139 continue;
140
141 //Add PHI to phi instruction list to be processed later
Tanya Lattner4cffb582004-05-26 06:27:18 +0000142 if (opCode == TargetInstrInfo::PHI)
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000143 phiInstrs.push_back(MI);
144
Tanya Lattner4cffb582004-05-26 06:27:18 +0000145 bool isBranch = false;
146
147 //We want to flag the branch node to treat it special
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000148 if(MTI->isBranch(opCode))
Tanya Lattner4cffb582004-05-26 06:27:18 +0000149 isBranch = true;
150
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000151 //Node is created and added to the graph automatically
Tanya Lattner4cffb582004-05-26 06:27:18 +0000152 MSchedGraphNode *node = new MSchedGraphNode(MI, this, delay, isBranch);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000153
154 DEBUG(std::cerr << "Created Node: " << *node << "\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000155
Tanya Lattner4cffb582004-05-26 06:27:18 +0000156 //Check OpCode to keep track of memory operations to add memory dependencies later.
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000157 if(MTI->isLoad(opCode) || MTI->isStore(opCode))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000158 memInstructions.push_back(node);
159
160 //Loop over all operands, and put them into the register number to
161 //graph node map for determining dependencies
162 //If an operands is a use/def, we have an anti dependence to itself
163 for(unsigned i=0; i < MI->getNumOperands(); ++i) {
164 //Get Operand
165 const MachineOperand &mOp = MI->getOperand(i);
166
Tanya Lattner4cffb582004-05-26 06:27:18 +0000167 //Check if it has an allocated register
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000168 if(mOp.hasAllocatedReg()) {
169 int regNum = mOp.getReg();
Tanya Lattner4cffb582004-05-26 06:27:18 +0000170
171 if(regNum != SparcV9::g0) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000172 //Put into our map
173 regNumtoNodeMap[regNum].push_back(std::make_pair(i, node));
Tanya Lattner4cffb582004-05-26 06:27:18 +0000174 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000175 continue;
176 }
177
178
179 //Add virtual registers dependencies
180 //Check if any exist in the value map already and create dependencies
181 //between them.
182 if(mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) {
183
184 //Make sure virtual register value is not null
185 assert((mOp.getVRegValue() != NULL) && "Null value is defined");
186
187 //Check if this is a read operation in a phi node, if so DO NOT PROCESS
Tanya Lattner4cffb582004-05-26 06:27:18 +0000188 if(mOp.isUse() && (opCode == TargetInstrInfo::PHI))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000189 continue;
190
191
192 if (const Value* srcI = mOp.getVRegValue()) {
193
194 //Find value in the map
195 std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
196 = valuetoNodeMap.find(srcI);
197
198 //If there is something in the map already, add edges from
199 //those instructions
200 //to this one we are processing
201 if(V != valuetoNodeMap.end()) {
202 addValueEdges(V->second, node, mOp.isUse(), mOp.isDef());
203
204 //Add to value map
205 V->second.push_back(std::make_pair(i,node));
206 }
207 //Otherwise put it in the map
208 else
209 //Put into value map
210 valuetoNodeMap[mOp.getVRegValue()].push_back(std::make_pair(i, node));
211 }
212 }
213 }
214 }
215 addMemEdges(memInstructions);
216 addMachRegEdges(regNumtoNodeMap);
217
218 //Finally deal with PHI Nodes and Value*
219 for(std::vector<const MachineInstr*>::iterator I = phiInstrs.begin(), E = phiInstrs.end(); I != E; ++I) {
220 //Get Node for this instruction
221 MSchedGraphNode *node = find(*I)->second;
222
223 //Loop over operands for this instruction and add value edges
224 for(unsigned i=0; i < (*I)->getNumOperands(); ++i) {
225 //Get Operand
226 const MachineOperand &mOp = (*I)->getOperand(i);
227 if((mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) && mOp.isUse()) {
228 //find the value in the map
229 if (const Value* srcI = mOp.getVRegValue()) {
230
231 //Find value in the map
232 std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
233 = valuetoNodeMap.find(srcI);
234
235 //If there is something in the map already, add edges from
236 //those instructions
237 //to this one we are processing
238 if(V != valuetoNodeMap.end()) {
239 addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), 1);
240 }
241 }
242 }
243 }
244 }
245}
246
247void MSchedGraph::addValueEdges(std::vector<OpIndexNodePair> &NodesInMap,
248 MSchedGraphNode *destNode, bool nodeIsUse,
249 bool nodeIsDef, int diff) {
250
251 for(std::vector<OpIndexNodePair>::iterator I = NodesInMap.begin(),
252 E = NodesInMap.end(); I != E; ++I) {
253
254 //Get node in vectors machine operand that is the same value as node
255 MSchedGraphNode *srcNode = I->second;
256 MachineOperand mOp = srcNode->getInst()->getOperand(I->first);
257
258 //Node is a Def, so add output dep.
259 if(nodeIsDef) {
260 if(mOp.isUse())
261 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
262 MSchedGraphEdge::AntiDep, diff);
263 if(mOp.isDef())
264 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
265 MSchedGraphEdge::OutputDep, diff);
266
267 }
268 if(nodeIsUse) {
269 if(mOp.isDef())
270 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
271 MSchedGraphEdge::TrueDep, diff);
272 }
273 }
274}
275
276
277void MSchedGraph::addMachRegEdges(std::map<int, std::vector<OpIndexNodePair> >& regNumtoNodeMap) {
278 //Loop over all machine registers in the map, and add dependencies
279 //between the instructions that use it
280 typedef std::map<int, std::vector<OpIndexNodePair> > regNodeMap;
281 for(regNodeMap::iterator I = regNumtoNodeMap.begin(); I != regNumtoNodeMap.end(); ++I) {
282 //Get the register number
283 int regNum = (*I).first;
284
285 //Get Vector of nodes that use this register
286 std::vector<OpIndexNodePair> Nodes = (*I).second;
287
288 //Loop over nodes and determine the dependence between the other
289 //nodes in the vector
290 for(unsigned i =0; i < Nodes.size(); ++i) {
291
292 //Get src node operator index that uses this machine register
293 int srcOpIndex = Nodes[i].first;
294
295 //Get the actual src Node
296 MSchedGraphNode *srcNode = Nodes[i].second;
297
298 //Get Operand
299 const MachineOperand &srcMOp = srcNode->getInst()->getOperand(srcOpIndex);
300
301 bool srcIsUseandDef = srcMOp.isDef() && srcMOp.isUse();
302 bool srcIsUse = srcMOp.isUse() && !srcMOp.isDef();
303
304
305 //Look at all instructions after this in execution order
306 for(unsigned j=i+1; j < Nodes.size(); ++j) {
307
308 //Sink node is a write
309 if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
310 //Src only uses the register (read)
311 if(srcIsUse)
312 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
313 MSchedGraphEdge::AntiDep);
314
315 else if(srcIsUseandDef) {
316 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
317 MSchedGraphEdge::AntiDep);
318
319 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
320 MSchedGraphEdge::OutputDep);
321 }
322 else
323 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
324 MSchedGraphEdge::OutputDep);
325 }
326 //Dest node is a read
327 else {
328 if(!srcIsUse || srcIsUseandDef)
329 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
330 MSchedGraphEdge::TrueDep);
331 }
332
333 }
334
335 //Look at all the instructions before this one since machine registers
336 //could live across iterations.
337 for(unsigned j = 0; j < i; ++j) {
338 //Sink node is a write
339 if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
340 //Src only uses the register (read)
341 if(srcIsUse)
342 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000343 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000344
345 else if(srcIsUseandDef) {
346 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000347 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000348
349 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000350 MSchedGraphEdge::OutputDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000351 }
352 else
353 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000354 MSchedGraphEdge::OutputDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000355 }
356 //Dest node is a read
357 else {
358 if(!srcIsUse || srcIsUseandDef)
359 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000360 MSchedGraphEdge::TrueDep,1 );
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000361 }
362
363
364 }
365
366 }
367
368 }
369
370}
371
372void MSchedGraph::addMemEdges(const std::vector<MSchedGraphNode*>& memInst) {
373
374 //Get Target machine instruction info
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000375 const TargetInstrInfo *TMI = Target.getInstrInfo();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000376
377 //Loop over all memory instructions in the vector
378 //Knowing that they are in execution, add true, anti, and output dependencies
379 for (unsigned srcIndex = 0; srcIndex < memInst.size(); ++srcIndex) {
380
381 //Get the machine opCode to determine type of memory instruction
382 MachineOpCode srcNodeOpCode = memInst[srcIndex]->getInst()->getOpcode();
383
384 //All instructions after this one in execution order have an iteration delay of 0
385 for(unsigned destIndex = srcIndex + 1; destIndex < memInst.size(); ++destIndex) {
386
387 //source is a Load, so add anti-dependencies (store after load)
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000388 if(TMI->isLoad(srcNodeOpCode))
389 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000390 memInst[srcIndex]->addOutEdge(memInst[destIndex],
391 MSchedGraphEdge::MemoryDep,
392 MSchedGraphEdge::AntiDep);
393
394 //If source is a store, add output and true dependencies
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000395 if(TMI->isStore(srcNodeOpCode)) {
396 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000397 memInst[srcIndex]->addOutEdge(memInst[destIndex],
398 MSchedGraphEdge::MemoryDep,
399 MSchedGraphEdge::OutputDep);
400 else
401 memInst[srcIndex]->addOutEdge(memInst[destIndex],
402 MSchedGraphEdge::MemoryDep,
403 MSchedGraphEdge::TrueDep);
404 }
405 }
406
407 //All instructions before the src in execution order have an iteration delay of 1
408 for(unsigned destIndex = 0; destIndex < srcIndex; ++destIndex) {
409 //source is a Load, so add anti-dependencies (store after load)
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000410 if(TMI->isLoad(srcNodeOpCode))
411 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000412 memInst[srcIndex]->addOutEdge(memInst[destIndex],
413 MSchedGraphEdge::MemoryDep,
414 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000415 if(TMI->isStore(srcNodeOpCode)) {
416 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000417 memInst[srcIndex]->addOutEdge(memInst[destIndex],
418 MSchedGraphEdge::MemoryDep,
419 MSchedGraphEdge::OutputDep, 1);
420 else
421 memInst[srcIndex]->addOutEdge(memInst[destIndex],
422 MSchedGraphEdge::MemoryDep,
423 MSchedGraphEdge::TrueDep, 1);
424 }
425
426 }
427
428 }
429}