blob: f106722fa8903238df8fa21d07655c6150f5c341 [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.
144MSchedGraph::MSchedGraph(const MachineBasicBlock *bb, const TargetMachine &targ, AliasAnalysis &AA, TargetData &TD, std::map<const MachineInstr*, unsigned> &ignoreInstrs)
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000145 : BB(bb), Target(targ) {
146
147 //Make sure BB is not null,
148 assert(BB != NULL && "Basic Block is null");
149
Tanya Lattner420025b2004-10-10 22:44:35 +0000150 //DEBUG(std::cerr << "Constructing graph for " << bb << "\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000151
152 //Create nodes and edges for this BB
Tanya Lattner9532ab92005-03-23 01:47:20 +0000153 buildNodesAndEdges(AA, TD, ignoreInstrs);
154
155 //Experimental!
156 //addBranchEdges();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000157}
158
Tanya Lattner9532ab92005-03-23 01:47:20 +0000159//Copies the graph and keeps a map from old to new nodes
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000160MSchedGraph::MSchedGraph(const MSchedGraph &G, std::map<MSchedGraphNode*, MSchedGraphNode*> &newNodes)
161 : BB(G.BB), Target(G.Target) {
162
163 std::map<MSchedGraphNode*, MSchedGraphNode*> oldToNew;
164 //Copy all nodes
165 for(MSchedGraph::const_iterator N = G.GraphMap.begin(), NE = G.GraphMap.end();
166 N != NE; ++N) {
167 MSchedGraphNode *newNode = new MSchedGraphNode(*(N->second));
168 oldToNew[&*(N->second)] = newNode;
169 newNodes[newNode] = &*(N->second);
170 GraphMap[&*(N->first)] = newNode;
171 }
172
173 //Loop over nodes and update edges to point to new nodes
174 for(MSchedGraph::iterator N = GraphMap.begin(), NE = GraphMap.end(); N != NE; ++N) {
175
176 //Get the node we are dealing with
177 MSchedGraphNode *node = &*(N->second);
178
179 node->setParent(this);
180
181 //Loop over nodes successors and predecessors and update to the new nodes
182 for(unsigned i = 0; i < node->pred_size(); ++i) {
183 node->setPredecessor(i, oldToNew[node->getPredecessor(i)]);
184 }
185
186 for(unsigned i = 0; i < node->succ_size(); ++i) {
187 MSchedGraphEdge *edge = node->getSuccessor(i);
188 MSchedGraphNode *oldDest = edge->getDest();
189 edge->setDest(oldToNew[oldDest]);
190 }
191 }
192}
193
Tanya Lattner9532ab92005-03-23 01:47:20 +0000194//Deconstructor, deletes all nodes in the graph
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000195MSchedGraph::~MSchedGraph () {
196 for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I)
197 delete I->second;
198}
199
Tanya Lattner9532ab92005-03-23 01:47:20 +0000200
201//Experimental code to add edges from the branch to all nodes dependent upon it.
202void hasPath(MSchedGraphNode *node, std::set<MSchedGraphNode*> &visited,
203 std::set<MSchedGraphNode*> &branches, MSchedGraphNode *startNode,
204 std::set<std::pair<MSchedGraphNode*,MSchedGraphNode*> > &newEdges ) {
205
206 visited.insert(node);
207 DEBUG(std::cerr << "Visiting: " << *node << "\n");
208 //Loop over successors
209 for(unsigned i = 0; i < node->succ_size(); ++i) {
210 MSchedGraphEdge *edge = node->getSuccessor(i);
211 MSchedGraphNode *dest = edge->getDest();
212 if(branches.count(dest))
213 newEdges.insert(std::make_pair(dest, startNode));
214
215 //only visit if we have not already
216 else if(!visited.count(dest)) {
217 if(edge->getIteDiff() == 0)
218 hasPath(dest, visited, branches, startNode, newEdges);}
219
220 }
221
222}
223
224//Experimental code to add edges from the branch to all nodes dependent upon it.
225void MSchedGraph::addBranchEdges() {
226 std::set<MSchedGraphNode*> branches;
227 std::set<MSchedGraphNode*> nodes;
228
229 for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) {
230 if(I->second->isBranch())
231 if(I->second->hasPredecessors())
232 branches.insert(I->second);
233 }
234
235 //See if there is a path first instruction to the branches, if so, add an
236 //iteration dependence between that node and the branch
237 std::set<std::pair<MSchedGraphNode*, MSchedGraphNode*> > newEdges;
238 for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) {
239 std::set<MSchedGraphNode*> visited;
240 hasPath((I->second), visited, branches, (I->second), newEdges);
241 }
242
243 //Spit out all edges we are going to add
244 unsigned min = GraphMap.size();
245 if(newEdges.size() == 1) {
246 ((newEdges.begin())->first)->addOutEdge(((newEdges.begin())->second),
247 MSchedGraphEdge::BranchDep,
248 MSchedGraphEdge::NonDataDep, 1);
249 }
250 else {
251
252 unsigned count = 0;
253 MSchedGraphNode *start;
254 MSchedGraphNode *end;
255 for(std::set<std::pair<MSchedGraphNode*, MSchedGraphNode*> >::iterator I = newEdges.begin(), E = newEdges.end(); I != E; ++I) {
256
257 DEBUG(std::cerr << "Branch Edge from: " << *(I->first) << " to " << *(I->second) << "\n");
258
259 // if(I->second->getIndex() <= min) {
260 start = I->first;
261 end = I->second;
262 //min = I->second->getIndex();
263 //}
264 start->addOutEdge(end,
265 MSchedGraphEdge::BranchDep,
266 MSchedGraphEdge::NonDataDep, 1);
267 }
268 }
269}
270
271
272//Add edges between the nodes
273void MSchedGraph::buildNodesAndEdges(AliasAnalysis &AA, TargetData &TD, std::map<const MachineInstr*, unsigned> &ignoreInstrs) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000274
275 //Get Machine target information for calculating latency
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000276 const TargetInstrInfo *MTI = Target.getInstrInfo();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000277
278 std::vector<MSchedGraphNode*> memInstructions;
279 std::map<int, std::vector<OpIndexNodePair> > regNumtoNodeMap;
280 std::map<const Value*, std::vector<OpIndexNodePair> > valuetoNodeMap;
281
282 //Save PHI instructions to deal with later
283 std::vector<const MachineInstr*> phiInstrs;
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000284 unsigned index = 0;
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000285
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000286 //Loop over instructions in MBB and add nodes and edges
287 for (MachineBasicBlock::const_iterator MI = BB->begin(), e = BB->end(); MI != e; ++MI) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000288
289 //Ignore indvar instructions
290 if(ignoreInstrs.count(MI)) {
291 ++index;
292 continue;
293 }
294
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000295 //Get each instruction of machine basic block, get the delay
296 //using the op code, create a new node for it, and add to the
297 //graph.
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000298
Tanya Lattner4cffb582004-05-26 06:27:18 +0000299 MachineOpCode opCode = MI->getOpcode();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000300 int delay;
301
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000302#if 0 // FIXME: LOOK INTO THIS
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000303 //Check if subsequent instructions can be issued before
304 //the result is ready, if so use min delay.
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000305 if(MTI->hasResultInterlock(MIopCode))
306 delay = MTI->minLatency(MIopCode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000307 else
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000308#endif
Tanya Lattner4cffb582004-05-26 06:27:18 +0000309 //Get delay
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000310 delay = MTI->maxLatency(opCode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000311
312 //Create new node for this machine instruction and add to the graph.
313 //Create only if not a nop
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000314 if(MTI->isNop(opCode))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000315 continue;
316
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000317 //Sparc BE does not use PHI opcode, so assert on this case
318 assert(opCode != TargetInstrInfo::PHI && "Did not expect PHI opcode");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000319
Tanya Lattner4cffb582004-05-26 06:27:18 +0000320 bool isBranch = false;
321
322 //We want to flag the branch node to treat it special
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000323 if(MTI->isBranch(opCode))
Tanya Lattner4cffb582004-05-26 06:27:18 +0000324 isBranch = true;
325
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000326 //Node is created and added to the graph automatically
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000327 MSchedGraphNode *node = new MSchedGraphNode(MI, this, index, delay, isBranch);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000328
329 DEBUG(std::cerr << "Created Node: " << *node << "\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000330
Tanya Lattner4cffb582004-05-26 06:27:18 +0000331 //Check OpCode to keep track of memory operations to add memory dependencies later.
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000332 if(MTI->isLoad(opCode) || MTI->isStore(opCode))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000333 memInstructions.push_back(node);
334
335 //Loop over all operands, and put them into the register number to
336 //graph node map for determining dependencies
337 //If an operands is a use/def, we have an anti dependence to itself
338 for(unsigned i=0; i < MI->getNumOperands(); ++i) {
339 //Get Operand
340 const MachineOperand &mOp = MI->getOperand(i);
341
Tanya Lattner4cffb582004-05-26 06:27:18 +0000342 //Check if it has an allocated register
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000343 if(mOp.hasAllocatedReg()) {
344 int regNum = mOp.getReg();
Tanya Lattner4cffb582004-05-26 06:27:18 +0000345
346 if(regNum != SparcV9::g0) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000347 //Put into our map
348 regNumtoNodeMap[regNum].push_back(std::make_pair(i, node));
Tanya Lattner4cffb582004-05-26 06:27:18 +0000349 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000350 continue;
351 }
352
353
354 //Add virtual registers dependencies
355 //Check if any exist in the value map already and create dependencies
356 //between them.
357 if(mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) {
358
359 //Make sure virtual register value is not null
360 assert((mOp.getVRegValue() != NULL) && "Null value is defined");
361
362 //Check if this is a read operation in a phi node, if so DO NOT PROCESS
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000363 if(mOp.isUse() && (opCode == TargetInstrInfo::PHI)) {
364 DEBUG(std::cerr << "Read Operation in a PHI node\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000365 continue;
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000366 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000367
368 if (const Value* srcI = mOp.getVRegValue()) {
369
370 //Find value in the map
371 std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
372 = valuetoNodeMap.find(srcI);
373
374 //If there is something in the map already, add edges from
375 //those instructions
376 //to this one we are processing
377 if(V != valuetoNodeMap.end()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000378 addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000379
380 //Add to value map
381 V->second.push_back(std::make_pair(i,node));
382 }
383 //Otherwise put it in the map
384 else
385 //Put into value map
386 valuetoNodeMap[mOp.getVRegValue()].push_back(std::make_pair(i, node));
387 }
388 }
389 }
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000390 ++index;
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000391 }
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000392
393 //Loop over LLVM BB, examine phi instructions, and add them to our phiInstr list to process
394 const BasicBlock *llvm_bb = BB->getBasicBlock();
395 for(BasicBlock::const_iterator I = llvm_bb->begin(), E = llvm_bb->end(); I != E; ++I) {
396 if(const PHINode *PN = dyn_cast<PHINode>(I)) {
397 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(PN);
398 for (unsigned j = 0; j < tempMvec.size(); j++) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000399 if(!ignoreInstrs.count(tempMvec[j])) {
400 DEBUG(std::cerr << "Inserting phi instr into map: " << *tempMvec[j] << "\n");
401 phiInstrs.push_back((MachineInstr*) tempMvec[j]);
402 }
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000403 }
404 }
405
406 }
407
Tanya Lattner9532ab92005-03-23 01:47:20 +0000408 addMemEdges(memInstructions, AA, TD);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000409 addMachRegEdges(regNumtoNodeMap);
410
411 //Finally deal with PHI Nodes and Value*
412 for(std::vector<const MachineInstr*>::iterator I = phiInstrs.begin(), E = phiInstrs.end(); I != E; ++I) {
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000413
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000414 //Get Node for this instruction
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000415 std::map<const MachineInstr*, MSchedGraphNode*>::iterator X;
416 X = find(*I);
417
418 if(X == GraphMap.end())
419 continue;
420
421 MSchedGraphNode *node = X->second;
422
423 DEBUG(std::cerr << "Adding ite diff edges for node: " << *node << "\n");
424
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000425 //Loop over operands for this instruction and add value edges
426 for(unsigned i=0; i < (*I)->getNumOperands(); ++i) {
427 //Get Operand
428 const MachineOperand &mOp = (*I)->getOperand(i);
429 if((mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) && mOp.isUse()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000430
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000431 //find the value in the map
432 if (const Value* srcI = mOp.getVRegValue()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000433
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000434 //Find value in the map
435 std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
Tanya Lattner9532ab92005-03-23 01:47:20 +0000436 = valuetoNodeMap.find(srcI);
437
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000438 //If there is something in the map already, add edges from
439 //those instructions
440 //to this one we are processing
441 if(V != valuetoNodeMap.end()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000442 addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000443 }
444 }
445 }
446 }
Tanya Lattner9532ab92005-03-23 01:47:20 +0000447 }
448}
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000449
Tanya Lattner9532ab92005-03-23 01:47:20 +0000450//Add dependencies for Value*s
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000451void MSchedGraph::addValueEdges(std::vector<OpIndexNodePair> &NodesInMap,
452 MSchedGraphNode *destNode, bool nodeIsUse,
Tanya Lattner9532ab92005-03-23 01:47:20 +0000453 bool nodeIsDef, std::vector<const MachineInstr*> &phiInstrs, int diff) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000454
455 for(std::vector<OpIndexNodePair>::iterator I = NodesInMap.begin(),
456 E = NodesInMap.end(); I != E; ++I) {
457
458 //Get node in vectors machine operand that is the same value as node
459 MSchedGraphNode *srcNode = I->second;
460 MachineOperand mOp = srcNode->getInst()->getOperand(I->first);
461
Tanya Lattner9532ab92005-03-23 01:47:20 +0000462 if(diff > 0)
463 if(std::find(phiInstrs.begin(), phiInstrs.end(), srcNode->getInst()) == phiInstrs.end())
464 continue;
465
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000466 //Node is a Def, so add output dep.
467 if(nodeIsDef) {
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000468 if(mOp.isUse()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000469 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=anti)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000470 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
471 MSchedGraphEdge::AntiDep, diff);
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000472 }
473 if(mOp.isDef()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000474 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=output)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000475 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
476 MSchedGraphEdge::OutputDep, diff);
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000477 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000478 }
479 if(nodeIsUse) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000480 if(mOp.isDef()) {
481 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=true)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000482 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
483 MSchedGraphEdge::TrueDep, diff);
Tanya Lattner9532ab92005-03-23 01:47:20 +0000484 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000485 }
486 }
487}
488
Tanya Lattner9532ab92005-03-23 01:47:20 +0000489//Add dependencies for machine registers across iterations
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000490void MSchedGraph::addMachRegEdges(std::map<int, std::vector<OpIndexNodePair> >& regNumtoNodeMap) {
491 //Loop over all machine registers in the map, and add dependencies
492 //between the instructions that use it
493 typedef std::map<int, std::vector<OpIndexNodePair> > regNodeMap;
494 for(regNodeMap::iterator I = regNumtoNodeMap.begin(); I != regNumtoNodeMap.end(); ++I) {
495 //Get the register number
496 int regNum = (*I).first;
497
498 //Get Vector of nodes that use this register
499 std::vector<OpIndexNodePair> Nodes = (*I).second;
500
501 //Loop over nodes and determine the dependence between the other
502 //nodes in the vector
503 for(unsigned i =0; i < Nodes.size(); ++i) {
504
505 //Get src node operator index that uses this machine register
506 int srcOpIndex = Nodes[i].first;
507
508 //Get the actual src Node
509 MSchedGraphNode *srcNode = Nodes[i].second;
510
511 //Get Operand
512 const MachineOperand &srcMOp = srcNode->getInst()->getOperand(srcOpIndex);
513
514 bool srcIsUseandDef = srcMOp.isDef() && srcMOp.isUse();
515 bool srcIsUse = srcMOp.isUse() && !srcMOp.isDef();
516
517
518 //Look at all instructions after this in execution order
519 for(unsigned j=i+1; j < Nodes.size(); ++j) {
520
521 //Sink node is a write
522 if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
523 //Src only uses the register (read)
524 if(srcIsUse)
525 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
526 MSchedGraphEdge::AntiDep);
527
528 else if(srcIsUseandDef) {
529 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
530 MSchedGraphEdge::AntiDep);
531
532 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
533 MSchedGraphEdge::OutputDep);
534 }
535 else
536 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
537 MSchedGraphEdge::OutputDep);
538 }
539 //Dest node is a read
540 else {
541 if(!srcIsUse || srcIsUseandDef)
542 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
543 MSchedGraphEdge::TrueDep);
544 }
545
546 }
547
548 //Look at all the instructions before this one since machine registers
549 //could live across iterations.
550 for(unsigned j = 0; j < i; ++j) {
551 //Sink node is a write
552 if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
553 //Src only uses the register (read)
554 if(srcIsUse)
555 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000556 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000557
558 else if(srcIsUseandDef) {
559 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000560 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000561
562 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000563 MSchedGraphEdge::OutputDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000564 }
565 else
566 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000567 MSchedGraphEdge::OutputDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000568 }
569 //Dest node is a read
570 else {
571 if(!srcIsUse || srcIsUseandDef)
572 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000573 MSchedGraphEdge::TrueDep,1 );
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000574 }
575
576
577 }
578
579 }
580
581 }
582
583}
584
Tanya Lattner9532ab92005-03-23 01:47:20 +0000585//Add edges between all loads and stores
586//Can be less strict with alias analysis and data dependence analysis.
587void MSchedGraph::addMemEdges(const std::vector<MSchedGraphNode*>& memInst, AliasAnalysis &AA, TargetData &TD) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000588
589 //Get Target machine instruction info
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000590 const TargetInstrInfo *TMI = Target.getInstrInfo();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000591
592 //Loop over all memory instructions in the vector
593 //Knowing that they are in execution, add true, anti, and output dependencies
594 for (unsigned srcIndex = 0; srcIndex < memInst.size(); ++srcIndex) {
595
Tanya Lattner9532ab92005-03-23 01:47:20 +0000596 MachineInstr *srcInst = (MachineInstr*) memInst[srcIndex]->getInst();
597
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000598 //Get the machine opCode to determine type of memory instruction
Tanya Lattner9532ab92005-03-23 01:47:20 +0000599 MachineOpCode srcNodeOpCode = srcInst->getOpcode();
600
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000601
602 //All instructions after this one in execution order have an iteration delay of 0
603 for(unsigned destIndex = srcIndex + 1; destIndex < memInst.size(); ++destIndex) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000604
605 MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst();
606
607 //Add Anti dependencies (store after load)
608 //Source is a Load
609 if(TMI->isLoad(srcNodeOpCode)) {
610
611 //Destination is a store
612 if(TMI->isStore(destInst->getOpcode())) {
613
614 //Get the Value* that we are reading from the load, always the first op
615 const MachineOperand &mOp = srcInst->getOperand(0);
616 assert((mOp.isUse() && (mOp.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n");
617
618 //Get the value* for the store
619 const MachineOperand &mOp2 = destInst->getOperand(0);
620 assert(mOp2.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a value*\n");
621
622 //Only add the edge if we can't verify that they do not alias
623 if(AA.alias(mOp2.getVRegValue(),
624 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
625 mOp.getVRegValue(),
626 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
627 != AliasAnalysis::NoAlias) {
628
629 //Add edge from load to store
630 memInst[srcIndex]->addOutEdge(memInst[destIndex],
631 MSchedGraphEdge::MemoryDep,
632 MSchedGraphEdge::AntiDep);
633 }
634 }
635 }
636
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000637 //If source is a store, add output and true dependencies
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000638 if(TMI->isStore(srcNodeOpCode)) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000639
640 //Get the Value* that we are reading from the store (src), always the first op
641 const MachineOperand &mOp = srcInst->getOperand(0);
642 assert(mOp.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a use and a value*\n");
643
644 //Get the Value* that we are reading from the load, always the first op
645 const MachineOperand &mOp2 = srcInst->getOperand(0);
646 assert((mOp2.isUse() && (mOp2.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n");
647
648 //Only add the edge if we can't verify that they do not alias
649 if(AA.alias(mOp2.getVRegValue(),
650 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
651 mOp.getVRegValue(),
652 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
653 != AliasAnalysis::NoAlias) {
654
655 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
656 memInst[srcIndex]->addOutEdge(memInst[destIndex],
657 MSchedGraphEdge::MemoryDep,
658 MSchedGraphEdge::OutputDep);
659 else
660 memInst[srcIndex]->addOutEdge(memInst[destIndex],
661 MSchedGraphEdge::MemoryDep,
662 MSchedGraphEdge::TrueDep);
663 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000664 }
665 }
666
667 //All instructions before the src in execution order have an iteration delay of 1
668 for(unsigned destIndex = 0; destIndex < srcIndex; ++destIndex) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000669
670 MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst();
671
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000672 //source is a Load, so add anti-dependencies (store after load)
Tanya Lattner9532ab92005-03-23 01:47:20 +0000673 if(TMI->isLoad(srcNodeOpCode)) {
674 //Get the Value* that we are reading from the load, always the first op
675 const MachineOperand &mOp = srcInst->getOperand(0);
676 assert((mOp.isUse() && (mOp.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000677
Tanya Lattner9532ab92005-03-23 01:47:20 +0000678 //Get the value* for the store
679 const MachineOperand &mOp2 = destInst->getOperand(0);
680 assert(mOp2.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a value*\n");
681
682 //Only add the edge if we can't verify that they do not alias
683 if(AA.alias(mOp2.getVRegValue(),
684 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
685 mOp.getVRegValue(),
686 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
687 != AliasAnalysis::NoAlias) {
688 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
689 memInst[srcIndex]->addOutEdge(memInst[destIndex],
690 MSchedGraphEdge::MemoryDep,
691 MSchedGraphEdge::AntiDep, 1);
692 }
693 }
694 if(TMI->isStore(srcNodeOpCode)) {
695
696 //Get the Value* that we are reading from the store (src), always the first op
697 const MachineOperand &mOp = srcInst->getOperand(0);
698 assert(mOp.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a use and a value*\n");
699
700 //Get the Value* that we are reading from the load, always the first op
701 const MachineOperand &mOp2 = srcInst->getOperand(0);
702 assert((mOp2.isUse() && (mOp2.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n");
703
704 //Only add the edge if we can't verify that they do not alias
705 if(AA.alias(mOp2.getVRegValue(),
706 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
707 mOp.getVRegValue(),
708 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
709 != AliasAnalysis::NoAlias) {
710
711 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
712 memInst[srcIndex]->addOutEdge(memInst[destIndex],
713 MSchedGraphEdge::MemoryDep,
714 MSchedGraphEdge::OutputDep, 1);
715 else
716 memInst[srcIndex]->addOutEdge(memInst[destIndex],
717 MSchedGraphEdge::MemoryDep,
718 MSchedGraphEdge::TrueDep, 1);
719 }
720 }
721
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000722 }
723
724 }
725}