blob: dc5c3b0570cb080085056df17c1873e902d50ac5 [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//
Tanya Lattner9532ab92005-03-23 01:47:20 +000010// A graph class for dependencies. This graph only contains true, anti, and
11// output data dependencies for a given MachineBasicBlock. Dependencies
12// across iterations are also computed. Unless data dependence analysis
13// is provided, a conservative approach of adding dependencies between all
14// loads and stores is taken.
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000015//===----------------------------------------------------------------------===//
16#define DEBUG_TYPE "ModuloSched"
17
18#include "MSchedGraph.h"
Misha Brukman7da1e6e2004-10-10 23:34:50 +000019#include "../SparcV9RegisterInfo.h"
Tanya Lattnerdb40cf12005-02-10 17:02:58 +000020#include "../MachineCodeForInstruction.h"
21#include "llvm/BasicBlock.h"
22#include "llvm/Instructions.h"
Chris Lattner26bc78e2005-03-24 05:13:53 +000023#include "llvm/Type.h"
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/Debug.h"
Misha Brukman6a90f822004-08-02 14:02:21 +000027#include <cstdlib>
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +000028#include <algorithm>
Tanya Lattner9532ab92005-03-23 01:47:20 +000029#include <set>
30
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000031using namespace llvm;
32
Tanya Lattner9532ab92005-03-23 01:47:20 +000033//MSchedGraphNode constructor
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000034MSchedGraphNode::MSchedGraphNode(const MachineInstr* inst,
Tanya Lattner28e5eab2004-11-28 23:36:15 +000035 MSchedGraph *graph, unsigned idx,
Tanya Lattner4cffb582004-05-26 06:27:18 +000036 unsigned late, bool isBranch)
Tanya Lattner28e5eab2004-11-28 23:36:15 +000037 : Inst(inst), Parent(graph), index(idx), latency(late), isBranchInstr(isBranch) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000038
39 //Add to the graph
40 graph->addNode(inst, this);
41}
42
Tanya Lattner9532ab92005-03-23 01:47:20 +000043//MSchedGraphNode copy constructor
Tanya Lattnerdb40cf12005-02-10 17:02:58 +000044MSchedGraphNode::MSchedGraphNode(const MSchedGraphNode &N)
45 : Predecessors(N.Predecessors), Successors(N.Successors) {
46
47 Inst = N.Inst;
48 Parent = N.Parent;
49 index = N.index;
50 latency = N.latency;
51 isBranchInstr = N.isBranchInstr;
52
53}
54
Tanya Lattner9532ab92005-03-23 01:47:20 +000055//Print the node (instruction and latency)
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000056void MSchedGraphNode::print(std::ostream &os) const {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000057 os << "MSchedGraphNode: Inst=" << *Inst << ", latency= " << latency << "\n";
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000058}
59
Tanya Lattner9532ab92005-03-23 01:47:20 +000060
61//Get the edge from a predecessor to this node
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000062MSchedGraphEdge MSchedGraphNode::getInEdge(MSchedGraphNode *pred) {
63 //Loop over all the successors of our predecessor
64 //return the edge the corresponds to this in edge
Misha Brukman6a90f822004-08-02 14:02:21 +000065 for (MSchedGraphNode::succ_iterator I = pred->succ_begin(),
66 E = pred->succ_end(); I != E; ++I) {
67 if (*I == this)
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000068 return I.getEdge();
69 }
70 assert(0 && "Should have found edge between this node and its predecessor!");
Misha Brukman6a90f822004-08-02 14:02:21 +000071 abort();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000072}
73
Tanya Lattner9532ab92005-03-23 01:47:20 +000074//Get the iteration difference for the edge from this node to its successor
Tanya Lattnerdb40cf12005-02-10 17:02:58 +000075unsigned MSchedGraphNode::getIteDiff(MSchedGraphNode *succ) {
76 for(std::vector<MSchedGraphEdge>::iterator I = Successors.begin(), E = Successors.end();
77 I != E; ++I) {
78 if(I->getDest() == succ)
79 return I->getIteDiff();
80 }
81 return 0;
82}
83
Tanya Lattner9532ab92005-03-23 01:47:20 +000084//Get the index into the vector of edges for the edge from pred to this node
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000085unsigned MSchedGraphNode::getInEdgeNum(MSchedGraphNode *pred) {
86 //Loop over all the successors of our predecessor
87 //return the edge the corresponds to this in edge
88 int count = 0;
89 for(MSchedGraphNode::succ_iterator I = pred->succ_begin(), E = pred->succ_end();
90 I != E; ++I) {
91 if(*I == this)
92 return count;
93 count++;
94 }
95 assert(0 && "Should have found edge between this node and its predecessor!");
96 abort();
97}
Tanya Lattner9532ab92005-03-23 01:47:20 +000098
99//Determine if succ is a successor of this node
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000100bool MSchedGraphNode::isSuccessor(MSchedGraphNode *succ) {
101 for(succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
102 if(*I == succ)
103 return true;
104 return false;
105}
106
Tanya Lattner9532ab92005-03-23 01:47:20 +0000107//Dtermine if pred is a predecessor of this node
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000108bool MSchedGraphNode::isPredecessor(MSchedGraphNode *pred) {
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000109 if(std::find( Predecessors.begin(), Predecessors.end(), pred) != Predecessors.end())
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000110 return true;
111 else
112 return false;
113}
114
Tanya Lattner9532ab92005-03-23 01:47:20 +0000115//Add a node to the graph
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000116void MSchedGraph::addNode(const MachineInstr *MI,
117 MSchedGraphNode *node) {
118
119 //Make sure node does not already exist
120 assert(GraphMap.find(MI) == GraphMap.end()
121 && "New MSchedGraphNode already exists for this instruction");
122
123 GraphMap[MI] = node;
124}
125
Tanya Lattner9532ab92005-03-23 01:47:20 +0000126//Delete a node to the graph
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000127void MSchedGraph::deleteNode(MSchedGraphNode *node) {
128
129 //Delete the edge to this node from all predecessors
Tanya Lattnerdb1680b2005-02-16 04:00:59 +0000130 while(node->pred_size() > 0) {
131 //DEBUG(std::cerr << "Delete edge from: " << **P << " to " << *node << "\n");
132 MSchedGraphNode *pred = *(node->pred_begin());
133 pred->deleteSuccessor(node);
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000134 }
Tanya Lattnerdb1680b2005-02-16 04:00:59 +0000135
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000136 //Remove this node from the graph
137 GraphMap.erase(node->getInst());
138
139}
140
Tanya Lattner9532ab92005-03-23 01:47:20 +0000141//Create a graph for a machine block. The ignoreInstrs map is so that we ignore instructions
142//associated to the index variable since this is a special case in Modulo Scheduling.
143//We only want to deal with the body of the loop.
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000144MSchedGraph::MSchedGraph(const MachineBasicBlock *bb, const TargetMachine &targ,
145 std::map<const MachineInstr*, unsigned> &ignoreInstrs,
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000146 DependenceAnalyzer &DA, std::map<MachineInstr*, Instruction*> &machineTollvm
147 )
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000148 : BB(bb), Target(targ) {
149
150 //Make sure BB is not null,
151 assert(BB != NULL && "Basic Block is null");
152
Tanya Lattner420025b2004-10-10 22:44:35 +0000153 //DEBUG(std::cerr << "Constructing graph for " << bb << "\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000154
155 //Create nodes and edges for this BB
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000156 buildNodesAndEdges(ignoreInstrs, DA, machineTollvm);
Tanya Lattner9532ab92005-03-23 01:47:20 +0000157
158 //Experimental!
159 //addBranchEdges();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000160}
161
Tanya Lattner9532ab92005-03-23 01:47:20 +0000162//Copies the graph and keeps a map from old to new nodes
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000163MSchedGraph::MSchedGraph(const MSchedGraph &G, std::map<MSchedGraphNode*, MSchedGraphNode*> &newNodes)
164 : BB(G.BB), Target(G.Target) {
165
166 std::map<MSchedGraphNode*, MSchedGraphNode*> oldToNew;
167 //Copy all nodes
168 for(MSchedGraph::const_iterator N = G.GraphMap.begin(), NE = G.GraphMap.end();
169 N != NE; ++N) {
170 MSchedGraphNode *newNode = new MSchedGraphNode(*(N->second));
171 oldToNew[&*(N->second)] = newNode;
172 newNodes[newNode] = &*(N->second);
173 GraphMap[&*(N->first)] = newNode;
174 }
175
176 //Loop over nodes and update edges to point to new nodes
177 for(MSchedGraph::iterator N = GraphMap.begin(), NE = GraphMap.end(); N != NE; ++N) {
178
179 //Get the node we are dealing with
180 MSchedGraphNode *node = &*(N->second);
181
182 node->setParent(this);
183
184 //Loop over nodes successors and predecessors and update to the new nodes
185 for(unsigned i = 0; i < node->pred_size(); ++i) {
186 node->setPredecessor(i, oldToNew[node->getPredecessor(i)]);
187 }
188
189 for(unsigned i = 0; i < node->succ_size(); ++i) {
190 MSchedGraphEdge *edge = node->getSuccessor(i);
191 MSchedGraphNode *oldDest = edge->getDest();
192 edge->setDest(oldToNew[oldDest]);
193 }
194 }
195}
196
Tanya Lattner9532ab92005-03-23 01:47:20 +0000197//Deconstructor, deletes all nodes in the graph
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000198MSchedGraph::~MSchedGraph () {
199 for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I)
200 delete I->second;
201}
202
Tanya Lattner9532ab92005-03-23 01:47:20 +0000203
204//Experimental code to add edges from the branch to all nodes dependent upon it.
205void hasPath(MSchedGraphNode *node, std::set<MSchedGraphNode*> &visited,
206 std::set<MSchedGraphNode*> &branches, MSchedGraphNode *startNode,
207 std::set<std::pair<MSchedGraphNode*,MSchedGraphNode*> > &newEdges ) {
208
209 visited.insert(node);
210 DEBUG(std::cerr << "Visiting: " << *node << "\n");
211 //Loop over successors
212 for(unsigned i = 0; i < node->succ_size(); ++i) {
213 MSchedGraphEdge *edge = node->getSuccessor(i);
214 MSchedGraphNode *dest = edge->getDest();
215 if(branches.count(dest))
216 newEdges.insert(std::make_pair(dest, startNode));
217
218 //only visit if we have not already
219 else if(!visited.count(dest)) {
220 if(edge->getIteDiff() == 0)
221 hasPath(dest, visited, branches, startNode, newEdges);}
222
223 }
224
225}
226
227//Experimental code to add edges from the branch to all nodes dependent upon it.
228void MSchedGraph::addBranchEdges() {
229 std::set<MSchedGraphNode*> branches;
230 std::set<MSchedGraphNode*> nodes;
231
232 for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) {
233 if(I->second->isBranch())
234 if(I->second->hasPredecessors())
235 branches.insert(I->second);
236 }
237
238 //See if there is a path first instruction to the branches, if so, add an
239 //iteration dependence between that node and the branch
240 std::set<std::pair<MSchedGraphNode*, MSchedGraphNode*> > newEdges;
241 for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) {
242 std::set<MSchedGraphNode*> visited;
243 hasPath((I->second), visited, branches, (I->second), newEdges);
244 }
245
246 //Spit out all edges we are going to add
247 unsigned min = GraphMap.size();
248 if(newEdges.size() == 1) {
249 ((newEdges.begin())->first)->addOutEdge(((newEdges.begin())->second),
250 MSchedGraphEdge::BranchDep,
251 MSchedGraphEdge::NonDataDep, 1);
252 }
253 else {
254
255 unsigned count = 0;
256 MSchedGraphNode *start;
257 MSchedGraphNode *end;
258 for(std::set<std::pair<MSchedGraphNode*, MSchedGraphNode*> >::iterator I = newEdges.begin(), E = newEdges.end(); I != E; ++I) {
259
260 DEBUG(std::cerr << "Branch Edge from: " << *(I->first) << " to " << *(I->second) << "\n");
261
262 // if(I->second->getIndex() <= min) {
263 start = I->first;
264 end = I->second;
265 //min = I->second->getIndex();
266 //}
267 start->addOutEdge(end,
268 MSchedGraphEdge::BranchDep,
269 MSchedGraphEdge::NonDataDep, 1);
270 }
271 }
272}
273
274
275//Add edges between the nodes
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000276void MSchedGraph::buildNodesAndEdges(std::map<const MachineInstr*, unsigned> &ignoreInstrs,
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000277 DependenceAnalyzer &DA,
278 std::map<MachineInstr*, Instruction*> &machineTollvm) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000279
280 //Get Machine target information for calculating latency
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000281 const TargetInstrInfo *MTI = Target.getInstrInfo();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000282
283 std::vector<MSchedGraphNode*> memInstructions;
284 std::map<int, std::vector<OpIndexNodePair> > regNumtoNodeMap;
285 std::map<const Value*, std::vector<OpIndexNodePair> > valuetoNodeMap;
286
287 //Save PHI instructions to deal with later
288 std::vector<const MachineInstr*> phiInstrs;
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000289 unsigned index = 0;
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000290
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000291 //Loop over instructions in MBB and add nodes and edges
292 for (MachineBasicBlock::const_iterator MI = BB->begin(), e = BB->end(); MI != e; ++MI) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000293
294 //Ignore indvar instructions
295 if(ignoreInstrs.count(MI)) {
296 ++index;
297 continue;
298 }
299
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000300 //Get each instruction of machine basic block, get the delay
301 //using the op code, create a new node for it, and add to the
302 //graph.
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000303
Tanya Lattner4cffb582004-05-26 06:27:18 +0000304 MachineOpCode opCode = MI->getOpcode();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000305 int delay;
306
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000307#if 0 // FIXME: LOOK INTO THIS
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000308 //Check if subsequent instructions can be issued before
309 //the result is ready, if so use min delay.
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000310 if(MTI->hasResultInterlock(MIopCode))
311 delay = MTI->minLatency(MIopCode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000312 else
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000313#endif
Tanya Lattner4cffb582004-05-26 06:27:18 +0000314 //Get delay
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000315 delay = MTI->maxLatency(opCode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000316
317 //Create new node for this machine instruction and add to the graph.
318 //Create only if not a nop
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000319 if(MTI->isNop(opCode))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000320 continue;
321
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000322 //Sparc BE does not use PHI opcode, so assert on this case
323 assert(opCode != TargetInstrInfo::PHI && "Did not expect PHI opcode");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000324
Tanya Lattner4cffb582004-05-26 06:27:18 +0000325 bool isBranch = false;
326
327 //We want to flag the branch node to treat it special
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000328 if(MTI->isBranch(opCode))
Tanya Lattner4cffb582004-05-26 06:27:18 +0000329 isBranch = true;
330
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000331 //Node is created and added to the graph automatically
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000332 MSchedGraphNode *node = new MSchedGraphNode(MI, this, index, delay, isBranch);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000333
334 DEBUG(std::cerr << "Created Node: " << *node << "\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000335
Tanya Lattner4cffb582004-05-26 06:27:18 +0000336 //Check OpCode to keep track of memory operations to add memory dependencies later.
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000337 if(MTI->isLoad(opCode) || MTI->isStore(opCode))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000338 memInstructions.push_back(node);
339
340 //Loop over all operands, and put them into the register number to
341 //graph node map for determining dependencies
342 //If an operands is a use/def, we have an anti dependence to itself
343 for(unsigned i=0; i < MI->getNumOperands(); ++i) {
344 //Get Operand
345 const MachineOperand &mOp = MI->getOperand(i);
346
Tanya Lattner4cffb582004-05-26 06:27:18 +0000347 //Check if it has an allocated register
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000348 if(mOp.hasAllocatedReg()) {
349 int regNum = mOp.getReg();
Tanya Lattner4cffb582004-05-26 06:27:18 +0000350
351 if(regNum != SparcV9::g0) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000352 //Put into our map
353 regNumtoNodeMap[regNum].push_back(std::make_pair(i, node));
Tanya Lattner4cffb582004-05-26 06:27:18 +0000354 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000355 continue;
356 }
357
358
359 //Add virtual registers dependencies
360 //Check if any exist in the value map already and create dependencies
361 //between them.
362 if(mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) {
363
364 //Make sure virtual register value is not null
365 assert((mOp.getVRegValue() != NULL) && "Null value is defined");
366
367 //Check if this is a read operation in a phi node, if so DO NOT PROCESS
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000368 if(mOp.isUse() && (opCode == TargetInstrInfo::PHI)) {
369 DEBUG(std::cerr << "Read Operation in a PHI node\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000370 continue;
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000371 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000372
373 if (const Value* srcI = mOp.getVRegValue()) {
374
375 //Find value in the map
376 std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
377 = valuetoNodeMap.find(srcI);
378
379 //If there is something in the map already, add edges from
380 //those instructions
381 //to this one we are processing
382 if(V != valuetoNodeMap.end()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000383 addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000384
385 //Add to value map
386 V->second.push_back(std::make_pair(i,node));
387 }
388 //Otherwise put it in the map
389 else
390 //Put into value map
391 valuetoNodeMap[mOp.getVRegValue()].push_back(std::make_pair(i, node));
392 }
393 }
394 }
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000395 ++index;
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000396 }
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000397
398 //Loop over LLVM BB, examine phi instructions, and add them to our phiInstr list to process
399 const BasicBlock *llvm_bb = BB->getBasicBlock();
400 for(BasicBlock::const_iterator I = llvm_bb->begin(), E = llvm_bb->end(); I != E; ++I) {
401 if(const PHINode *PN = dyn_cast<PHINode>(I)) {
402 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(PN);
403 for (unsigned j = 0; j < tempMvec.size(); j++) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000404 if(!ignoreInstrs.count(tempMvec[j])) {
405 DEBUG(std::cerr << "Inserting phi instr into map: " << *tempMvec[j] << "\n");
406 phiInstrs.push_back((MachineInstr*) tempMvec[j]);
407 }
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000408 }
409 }
410
411 }
412
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000413 addMemEdges(memInstructions, DA, machineTollvm);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000414 addMachRegEdges(regNumtoNodeMap);
415
416 //Finally deal with PHI Nodes and Value*
417 for(std::vector<const MachineInstr*>::iterator I = phiInstrs.begin(), E = phiInstrs.end(); I != E; ++I) {
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000418
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000419 //Get Node for this instruction
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000420 std::map<const MachineInstr*, MSchedGraphNode*>::iterator X;
421 X = find(*I);
422
423 if(X == GraphMap.end())
424 continue;
425
426 MSchedGraphNode *node = X->second;
427
428 DEBUG(std::cerr << "Adding ite diff edges for node: " << *node << "\n");
429
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000430 //Loop over operands for this instruction and add value edges
431 for(unsigned i=0; i < (*I)->getNumOperands(); ++i) {
432 //Get Operand
433 const MachineOperand &mOp = (*I)->getOperand(i);
434 if((mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) && mOp.isUse()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000435
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000436 //find the value in the map
437 if (const Value* srcI = mOp.getVRegValue()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000438
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000439 //Find value in the map
440 std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
Tanya Lattner9532ab92005-03-23 01:47:20 +0000441 = valuetoNodeMap.find(srcI);
442
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000443 //If there is something in the map already, add edges from
444 //those instructions
445 //to this one we are processing
446 if(V != valuetoNodeMap.end()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000447 addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000448 }
449 }
450 }
451 }
Tanya Lattner9532ab92005-03-23 01:47:20 +0000452 }
453}
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000454
Tanya Lattner9532ab92005-03-23 01:47:20 +0000455//Add dependencies for Value*s
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000456void MSchedGraph::addValueEdges(std::vector<OpIndexNodePair> &NodesInMap,
457 MSchedGraphNode *destNode, bool nodeIsUse,
Tanya Lattner9532ab92005-03-23 01:47:20 +0000458 bool nodeIsDef, std::vector<const MachineInstr*> &phiInstrs, int diff) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000459
460 for(std::vector<OpIndexNodePair>::iterator I = NodesInMap.begin(),
461 E = NodesInMap.end(); I != E; ++I) {
462
463 //Get node in vectors machine operand that is the same value as node
464 MSchedGraphNode *srcNode = I->second;
465 MachineOperand mOp = srcNode->getInst()->getOperand(I->first);
466
Tanya Lattner9532ab92005-03-23 01:47:20 +0000467 if(diff > 0)
468 if(std::find(phiInstrs.begin(), phiInstrs.end(), srcNode->getInst()) == phiInstrs.end())
469 continue;
470
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000471 //Node is a Def, so add output dep.
472 if(nodeIsDef) {
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000473 if(mOp.isUse()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000474 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=anti)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000475 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
476 MSchedGraphEdge::AntiDep, diff);
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000477 }
478 if(mOp.isDef()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000479 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=output)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000480 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
481 MSchedGraphEdge::OutputDep, diff);
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000482 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000483 }
484 if(nodeIsUse) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000485 if(mOp.isDef()) {
486 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=true)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000487 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
488 MSchedGraphEdge::TrueDep, diff);
Tanya Lattner9532ab92005-03-23 01:47:20 +0000489 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000490 }
491 }
492}
493
Tanya Lattner9532ab92005-03-23 01:47:20 +0000494//Add dependencies for machine registers across iterations
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000495void MSchedGraph::addMachRegEdges(std::map<int, std::vector<OpIndexNodePair> >& regNumtoNodeMap) {
496 //Loop over all machine registers in the map, and add dependencies
497 //between the instructions that use it
498 typedef std::map<int, std::vector<OpIndexNodePair> > regNodeMap;
499 for(regNodeMap::iterator I = regNumtoNodeMap.begin(); I != regNumtoNodeMap.end(); ++I) {
500 //Get the register number
501 int regNum = (*I).first;
502
503 //Get Vector of nodes that use this register
504 std::vector<OpIndexNodePair> Nodes = (*I).second;
505
506 //Loop over nodes and determine the dependence between the other
507 //nodes in the vector
508 for(unsigned i =0; i < Nodes.size(); ++i) {
509
510 //Get src node operator index that uses this machine register
511 int srcOpIndex = Nodes[i].first;
512
513 //Get the actual src Node
514 MSchedGraphNode *srcNode = Nodes[i].second;
515
516 //Get Operand
517 const MachineOperand &srcMOp = srcNode->getInst()->getOperand(srcOpIndex);
518
519 bool srcIsUseandDef = srcMOp.isDef() && srcMOp.isUse();
520 bool srcIsUse = srcMOp.isUse() && !srcMOp.isDef();
521
522
523 //Look at all instructions after this in execution order
524 for(unsigned j=i+1; j < Nodes.size(); ++j) {
525
526 //Sink node is a write
527 if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
528 //Src only uses the register (read)
529 if(srcIsUse)
530 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
531 MSchedGraphEdge::AntiDep);
532
533 else if(srcIsUseandDef) {
534 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
535 MSchedGraphEdge::AntiDep);
536
537 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
538 MSchedGraphEdge::OutputDep);
539 }
540 else
541 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
542 MSchedGraphEdge::OutputDep);
543 }
544 //Dest node is a read
545 else {
546 if(!srcIsUse || srcIsUseandDef)
547 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
548 MSchedGraphEdge::TrueDep);
549 }
550
551 }
552
553 //Look at all the instructions before this one since machine registers
554 //could live across iterations.
555 for(unsigned j = 0; j < i; ++j) {
556 //Sink node is a write
557 if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
558 //Src only uses the register (read)
559 if(srcIsUse)
560 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000561 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000562
563 else if(srcIsUseandDef) {
564 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000565 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000566
567 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000568 MSchedGraphEdge::OutputDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000569 }
570 else
571 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000572 MSchedGraphEdge::OutputDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000573 }
574 //Dest node is a read
575 else {
576 if(!srcIsUse || srcIsUseandDef)
577 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000578 MSchedGraphEdge::TrueDep,1 );
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000579 }
580
581
582 }
583
584 }
585
586 }
587
588}
589
Tanya Lattner9532ab92005-03-23 01:47:20 +0000590//Add edges between all loads and stores
591//Can be less strict with alias analysis and data dependence analysis.
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000592void MSchedGraph::addMemEdges(const std::vector<MSchedGraphNode*>& memInst, DependenceAnalyzer &DA,
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000593 std::map<MachineInstr*, Instruction*> &machineTollvm) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000594
595 //Get Target machine instruction info
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000596 const TargetInstrInfo *TMI = Target.getInstrInfo();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000597
598 //Loop over all memory instructions in the vector
599 //Knowing that they are in execution, add true, anti, and output dependencies
600 for (unsigned srcIndex = 0; srcIndex < memInst.size(); ++srcIndex) {
601
Tanya Lattner9532ab92005-03-23 01:47:20 +0000602 MachineInstr *srcInst = (MachineInstr*) memInst[srcIndex]->getInst();
603
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000604 //Get the machine opCode to determine type of memory instruction
Tanya Lattner9532ab92005-03-23 01:47:20 +0000605 MachineOpCode srcNodeOpCode = srcInst->getOpcode();
606
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000607 //All instructions after this one in execution order have an iteration delay of 0
608 for(unsigned destIndex = srcIndex + 1; destIndex < memInst.size(); ++destIndex) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000609
610 MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst();
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000611
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000612 DEBUG(std::cerr << "MInst1: " << *srcInst << "\n");
613 DEBUG(std::cerr << "Inst1: " << *machineTollvm[srcInst] << "\n");
614 DEBUG(std::cerr << "MInst2: " << *destInst << "\n");
615 DEBUG(std::cerr << "Inst2: " << *machineTollvm[destInst] << "\n");
Tanya Lattner9532ab92005-03-23 01:47:20 +0000616
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000617 DependenceResult dr = DA.getDependenceInfo(machineTollvm[srcInst], machineTollvm[destInst]);
Tanya Lattner9532ab92005-03-23 01:47:20 +0000618
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000619 for(std::vector<Dependence>::iterator d = dr.dependences.begin(), de = dr.dependences.end();
620 d != de; ++d) {
621 //Add edge from load to store
622 memInst[srcIndex]->addOutEdge(memInst[destIndex],
623 MSchedGraphEdge::MemoryDep,
624 d->getDepType(), d->getIteDiff());
625
Tanya Lattner9532ab92005-03-23 01:47:20 +0000626 }
Tanya Lattner9532ab92005-03-23 01:47:20 +0000627
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000628 }
629
630 //All instructions before the src in execution order have an iteration delay of 1
631 for(unsigned destIndex = 0; destIndex < srcIndex; ++destIndex) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000632
633 MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst();
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000634 bool malias = false;
Tanya Lattner9532ab92005-03-23 01:47:20 +0000635
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000636 //source is a Load, so add anti-dependencies (store after load)
Tanya Lattner9532ab92005-03-23 01:47:20 +0000637 if(TMI->isLoad(srcNodeOpCode)) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000638
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000639 //Get the Value* that we are reading from the load, always the first op
640 const MachineOperand &mOp = srcInst->getOperand(0);
641 const MachineOperand &mOp2 = destInst->getOperand(0);
642
643 if(mOp.hasAllocatedReg())
644 if(mOp.getReg() == SparcV9::g0)
645 continue;
646 else
647 malias = true;
648 if(mOp2.hasAllocatedReg())
649 if(mOp2.getReg() == SparcV9::g0)
650 continue;
651 else
652 malias = true;
653
654 //Only add the edge if we can't verify that they do not alias
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000655 /*if(AA.alias(mOp2.getVRegValue(),
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000656 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
657 mOp.getVRegValue(),
658 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000659 != AliasAnalysis::NoAlias) {*/
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000660 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
661 memInst[srcIndex]->addOutEdge(memInst[destIndex],
662 MSchedGraphEdge::MemoryDep,
663 MSchedGraphEdge::AntiDep, 1);
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000664 //}
Tanya Lattner9532ab92005-03-23 01:47:20 +0000665 }
666 if(TMI->isStore(srcNodeOpCode)) {
667
Tanya Lattner9532ab92005-03-23 01:47:20 +0000668 //Get the Value* that we are reading from the load, always the first op
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000669 const MachineOperand &mOp = srcInst->getOperand(0);
670 const MachineOperand &mOp2 = destInst->getOperand(0);
Tanya Lattner9532ab92005-03-23 01:47:20 +0000671
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000672 if(mOp.hasAllocatedReg())
673 if(mOp.getReg() == SparcV9::g0)
674 continue;
675 else
676 malias = true;
677 if(mOp2.hasAllocatedReg())
678 if(mOp2.getReg() == SparcV9::g0)
679 continue;
680 else
681 malias = true;
682
Tanya Lattner9532ab92005-03-23 01:47:20 +0000683 //Only add the edge if we can't verify that they do not alias
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000684 /*if(AA.alias(mOp2.getVRegValue(),
Tanya Lattner9532ab92005-03-23 01:47:20 +0000685 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
686 mOp.getVRegValue(),
687 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000688 != AliasAnalysis::NoAlias) {*/
Tanya Lattner9532ab92005-03-23 01:47:20 +0000689
690 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
691 memInst[srcIndex]->addOutEdge(memInst[destIndex],
692 MSchedGraphEdge::MemoryDep,
693 MSchedGraphEdge::OutputDep, 1);
694 else
695 memInst[srcIndex]->addOutEdge(memInst[destIndex],
696 MSchedGraphEdge::MemoryDep,
697 MSchedGraphEdge::TrueDep, 1);
Tanya Lattnerac6e2db2005-04-05 16:36:44 +0000698 //}
Tanya Lattner9532ab92005-03-23 01:47:20 +0000699 }
700
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000701 }
702
703 }
704}