blob: 7b4680ebe21d9d37c3558f476db338506cea5eb9 [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"
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/Debug.h"
Misha Brukman6a90f822004-08-02 14:02:21 +000026#include <cstdlib>
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +000027#include <algorithm>
Tanya Lattner9532ab92005-03-23 01:47:20 +000028#include <set>
29
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000030using namespace llvm;
31
Tanya Lattner9532ab92005-03-23 01:47:20 +000032//MSchedGraphNode constructor
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000033MSchedGraphNode::MSchedGraphNode(const MachineInstr* inst,
Tanya Lattner28e5eab2004-11-28 23:36:15 +000034 MSchedGraph *graph, unsigned idx,
Tanya Lattner4cffb582004-05-26 06:27:18 +000035 unsigned late, bool isBranch)
Tanya Lattner28e5eab2004-11-28 23:36:15 +000036 : Inst(inst), Parent(graph), index(idx), latency(late), isBranchInstr(isBranch) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000037
38 //Add to the graph
39 graph->addNode(inst, this);
40}
41
Tanya Lattner9532ab92005-03-23 01:47:20 +000042//MSchedGraphNode copy constructor
Tanya Lattnerdb40cf12005-02-10 17:02:58 +000043MSchedGraphNode::MSchedGraphNode(const MSchedGraphNode &N)
44 : Predecessors(N.Predecessors), Successors(N.Successors) {
45
46 Inst = N.Inst;
47 Parent = N.Parent;
48 index = N.index;
49 latency = N.latency;
50 isBranchInstr = N.isBranchInstr;
51
52}
53
Tanya Lattner9532ab92005-03-23 01:47:20 +000054//Print the node (instruction and latency)
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000055void MSchedGraphNode::print(std::ostream &os) const {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000056 os << "MSchedGraphNode: Inst=" << *Inst << ", latency= " << latency << "\n";
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000057}
58
Tanya Lattner9532ab92005-03-23 01:47:20 +000059
60//Get the edge from a predecessor to this node
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000061MSchedGraphEdge MSchedGraphNode::getInEdge(MSchedGraphNode *pred) {
62 //Loop over all the successors of our predecessor
63 //return the edge the corresponds to this in edge
Misha Brukman6a90f822004-08-02 14:02:21 +000064 for (MSchedGraphNode::succ_iterator I = pred->succ_begin(),
65 E = pred->succ_end(); I != E; ++I) {
66 if (*I == this)
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000067 return I.getEdge();
68 }
69 assert(0 && "Should have found edge between this node and its predecessor!");
Misha Brukman6a90f822004-08-02 14:02:21 +000070 abort();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000071}
72
Tanya Lattner9532ab92005-03-23 01:47:20 +000073//Get the iteration difference for the edge from this node to its successor
Tanya Lattnerdb40cf12005-02-10 17:02:58 +000074unsigned MSchedGraphNode::getIteDiff(MSchedGraphNode *succ) {
75 for(std::vector<MSchedGraphEdge>::iterator I = Successors.begin(), E = Successors.end();
76 I != E; ++I) {
77 if(I->getDest() == succ)
78 return I->getIteDiff();
79 }
80 return 0;
81}
82
Tanya Lattner9532ab92005-03-23 01:47:20 +000083//Get the index into the vector of edges for the edge from pred to this node
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000084unsigned MSchedGraphNode::getInEdgeNum(MSchedGraphNode *pred) {
85 //Loop over all the successors of our predecessor
86 //return the edge the corresponds to this in edge
87 int count = 0;
88 for(MSchedGraphNode::succ_iterator I = pred->succ_begin(), E = pred->succ_end();
89 I != E; ++I) {
90 if(*I == this)
91 return count;
92 count++;
93 }
94 assert(0 && "Should have found edge between this node and its predecessor!");
95 abort();
96}
Tanya Lattner9532ab92005-03-23 01:47:20 +000097
98//Determine if succ is a successor of this node
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000099bool MSchedGraphNode::isSuccessor(MSchedGraphNode *succ) {
100 for(succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
101 if(*I == succ)
102 return true;
103 return false;
104}
105
Tanya Lattner9532ab92005-03-23 01:47:20 +0000106//Dtermine if pred is a predecessor of this node
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000107bool MSchedGraphNode::isPredecessor(MSchedGraphNode *pred) {
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000108 if(std::find( Predecessors.begin(), Predecessors.end(), pred) != Predecessors.end())
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000109 return true;
110 else
111 return false;
112}
113
Tanya Lattner9532ab92005-03-23 01:47:20 +0000114//Add a node to the graph
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000115void MSchedGraph::addNode(const MachineInstr *MI,
116 MSchedGraphNode *node) {
117
118 //Make sure node does not already exist
119 assert(GraphMap.find(MI) == GraphMap.end()
120 && "New MSchedGraphNode already exists for this instruction");
121
122 GraphMap[MI] = node;
123}
124
Tanya Lattner9532ab92005-03-23 01:47:20 +0000125//Delete a node to the graph
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000126void MSchedGraph::deleteNode(MSchedGraphNode *node) {
127
128 //Delete the edge to this node from all predecessors
Tanya Lattnerdb1680b2005-02-16 04:00:59 +0000129 while(node->pred_size() > 0) {
130 //DEBUG(std::cerr << "Delete edge from: " << **P << " to " << *node << "\n");
131 MSchedGraphNode *pred = *(node->pred_begin());
132 pred->deleteSuccessor(node);
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000133 }
Tanya Lattnerdb1680b2005-02-16 04:00:59 +0000134
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000135 //Remove this node from the graph
136 GraphMap.erase(node->getInst());
137
138}
139
Tanya Lattner9532ab92005-03-23 01:47:20 +0000140//Create a graph for a machine block. The ignoreInstrs map is so that we ignore instructions
141//associated to the index variable since this is a special case in Modulo Scheduling.
142//We only want to deal with the body of the loop.
143MSchedGraph::MSchedGraph(const MachineBasicBlock *bb, const TargetMachine &targ, AliasAnalysis &AA, TargetData &TD, std::map<const MachineInstr*, unsigned> &ignoreInstrs)
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000144 : BB(bb), Target(targ) {
145
146 //Make sure BB is not null,
147 assert(BB != NULL && "Basic Block is null");
148
Tanya Lattner420025b2004-10-10 22:44:35 +0000149 //DEBUG(std::cerr << "Constructing graph for " << bb << "\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000150
151 //Create nodes and edges for this BB
Tanya Lattner9532ab92005-03-23 01:47:20 +0000152 buildNodesAndEdges(AA, TD, ignoreInstrs);
153
154 //Experimental!
155 //addBranchEdges();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000156}
157
Tanya Lattner9532ab92005-03-23 01:47:20 +0000158//Copies the graph and keeps a map from old to new nodes
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000159MSchedGraph::MSchedGraph(const MSchedGraph &G, std::map<MSchedGraphNode*, MSchedGraphNode*> &newNodes)
160 : BB(G.BB), Target(G.Target) {
161
162 std::map<MSchedGraphNode*, MSchedGraphNode*> oldToNew;
163 //Copy all nodes
164 for(MSchedGraph::const_iterator N = G.GraphMap.begin(), NE = G.GraphMap.end();
165 N != NE; ++N) {
166 MSchedGraphNode *newNode = new MSchedGraphNode(*(N->second));
167 oldToNew[&*(N->second)] = newNode;
168 newNodes[newNode] = &*(N->second);
169 GraphMap[&*(N->first)] = newNode;
170 }
171
172 //Loop over nodes and update edges to point to new nodes
173 for(MSchedGraph::iterator N = GraphMap.begin(), NE = GraphMap.end(); N != NE; ++N) {
174
175 //Get the node we are dealing with
176 MSchedGraphNode *node = &*(N->second);
177
178 node->setParent(this);
179
180 //Loop over nodes successors and predecessors and update to the new nodes
181 for(unsigned i = 0; i < node->pred_size(); ++i) {
182 node->setPredecessor(i, oldToNew[node->getPredecessor(i)]);
183 }
184
185 for(unsigned i = 0; i < node->succ_size(); ++i) {
186 MSchedGraphEdge *edge = node->getSuccessor(i);
187 MSchedGraphNode *oldDest = edge->getDest();
188 edge->setDest(oldToNew[oldDest]);
189 }
190 }
191}
192
Tanya Lattner9532ab92005-03-23 01:47:20 +0000193//Deconstructor, deletes all nodes in the graph
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000194MSchedGraph::~MSchedGraph () {
195 for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I)
196 delete I->second;
197}
198
Tanya Lattner9532ab92005-03-23 01:47:20 +0000199
200//Experimental code to add edges from the branch to all nodes dependent upon it.
201void hasPath(MSchedGraphNode *node, std::set<MSchedGraphNode*> &visited,
202 std::set<MSchedGraphNode*> &branches, MSchedGraphNode *startNode,
203 std::set<std::pair<MSchedGraphNode*,MSchedGraphNode*> > &newEdges ) {
204
205 visited.insert(node);
206 DEBUG(std::cerr << "Visiting: " << *node << "\n");
207 //Loop over successors
208 for(unsigned i = 0; i < node->succ_size(); ++i) {
209 MSchedGraphEdge *edge = node->getSuccessor(i);
210 MSchedGraphNode *dest = edge->getDest();
211 if(branches.count(dest))
212 newEdges.insert(std::make_pair(dest, startNode));
213
214 //only visit if we have not already
215 else if(!visited.count(dest)) {
216 if(edge->getIteDiff() == 0)
217 hasPath(dest, visited, branches, startNode, newEdges);}
218
219 }
220
221}
222
223//Experimental code to add edges from the branch to all nodes dependent upon it.
224void MSchedGraph::addBranchEdges() {
225 std::set<MSchedGraphNode*> branches;
226 std::set<MSchedGraphNode*> nodes;
227
228 for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) {
229 if(I->second->isBranch())
230 if(I->second->hasPredecessors())
231 branches.insert(I->second);
232 }
233
234 //See if there is a path first instruction to the branches, if so, add an
235 //iteration dependence between that node and the branch
236 std::set<std::pair<MSchedGraphNode*, MSchedGraphNode*> > newEdges;
237 for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) {
238 std::set<MSchedGraphNode*> visited;
239 hasPath((I->second), visited, branches, (I->second), newEdges);
240 }
241
242 //Spit out all edges we are going to add
243 unsigned min = GraphMap.size();
244 if(newEdges.size() == 1) {
245 ((newEdges.begin())->first)->addOutEdge(((newEdges.begin())->second),
246 MSchedGraphEdge::BranchDep,
247 MSchedGraphEdge::NonDataDep, 1);
248 }
249 else {
250
251 unsigned count = 0;
252 MSchedGraphNode *start;
253 MSchedGraphNode *end;
254 for(std::set<std::pair<MSchedGraphNode*, MSchedGraphNode*> >::iterator I = newEdges.begin(), E = newEdges.end(); I != E; ++I) {
255
256 DEBUG(std::cerr << "Branch Edge from: " << *(I->first) << " to " << *(I->second) << "\n");
257
258 // if(I->second->getIndex() <= min) {
259 start = I->first;
260 end = I->second;
261 //min = I->second->getIndex();
262 //}
263 start->addOutEdge(end,
264 MSchedGraphEdge::BranchDep,
265 MSchedGraphEdge::NonDataDep, 1);
266 }
267 }
268}
269
270
271//Add edges between the nodes
272void MSchedGraph::buildNodesAndEdges(AliasAnalysis &AA, TargetData &TD, std::map<const MachineInstr*, unsigned> &ignoreInstrs) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000273
274 //Get Machine target information for calculating latency
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000275 const TargetInstrInfo *MTI = Target.getInstrInfo();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000276
277 std::vector<MSchedGraphNode*> memInstructions;
278 std::map<int, std::vector<OpIndexNodePair> > regNumtoNodeMap;
279 std::map<const Value*, std::vector<OpIndexNodePair> > valuetoNodeMap;
280
281 //Save PHI instructions to deal with later
282 std::vector<const MachineInstr*> phiInstrs;
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000283 unsigned index = 0;
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000284
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000285 //Loop over instructions in MBB and add nodes and edges
286 for (MachineBasicBlock::const_iterator MI = BB->begin(), e = BB->end(); MI != e; ++MI) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000287
288 //Ignore indvar instructions
289 if(ignoreInstrs.count(MI)) {
290 ++index;
291 continue;
292 }
293
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000294 //Get each instruction of machine basic block, get the delay
295 //using the op code, create a new node for it, and add to the
296 //graph.
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000297
Tanya Lattner4cffb582004-05-26 06:27:18 +0000298 MachineOpCode opCode = MI->getOpcode();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000299 int delay;
300
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000301#if 0 // FIXME: LOOK INTO THIS
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000302 //Check if subsequent instructions can be issued before
303 //the result is ready, if so use min delay.
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000304 if(MTI->hasResultInterlock(MIopCode))
305 delay = MTI->minLatency(MIopCode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000306 else
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000307#endif
Tanya Lattner4cffb582004-05-26 06:27:18 +0000308 //Get delay
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000309 delay = MTI->maxLatency(opCode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000310
311 //Create new node for this machine instruction and add to the graph.
312 //Create only if not a nop
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000313 if(MTI->isNop(opCode))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000314 continue;
315
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000316 //Sparc BE does not use PHI opcode, so assert on this case
317 assert(opCode != TargetInstrInfo::PHI && "Did not expect PHI opcode");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000318
Tanya Lattner4cffb582004-05-26 06:27:18 +0000319 bool isBranch = false;
320
321 //We want to flag the branch node to treat it special
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000322 if(MTI->isBranch(opCode))
Tanya Lattner4cffb582004-05-26 06:27:18 +0000323 isBranch = true;
324
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000325 //Node is created and added to the graph automatically
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000326 MSchedGraphNode *node = new MSchedGraphNode(MI, this, index, delay, isBranch);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000327
328 DEBUG(std::cerr << "Created Node: " << *node << "\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000329
Tanya Lattner4cffb582004-05-26 06:27:18 +0000330 //Check OpCode to keep track of memory operations to add memory dependencies later.
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000331 if(MTI->isLoad(opCode) || MTI->isStore(opCode))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000332 memInstructions.push_back(node);
333
334 //Loop over all operands, and put them into the register number to
335 //graph node map for determining dependencies
336 //If an operands is a use/def, we have an anti dependence to itself
337 for(unsigned i=0; i < MI->getNumOperands(); ++i) {
338 //Get Operand
339 const MachineOperand &mOp = MI->getOperand(i);
340
Tanya Lattner4cffb582004-05-26 06:27:18 +0000341 //Check if it has an allocated register
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000342 if(mOp.hasAllocatedReg()) {
343 int regNum = mOp.getReg();
Tanya Lattner4cffb582004-05-26 06:27:18 +0000344
345 if(regNum != SparcV9::g0) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000346 //Put into our map
347 regNumtoNodeMap[regNum].push_back(std::make_pair(i, node));
Tanya Lattner4cffb582004-05-26 06:27:18 +0000348 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000349 continue;
350 }
351
352
353 //Add virtual registers dependencies
354 //Check if any exist in the value map already and create dependencies
355 //between them.
356 if(mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) {
357
358 //Make sure virtual register value is not null
359 assert((mOp.getVRegValue() != NULL) && "Null value is defined");
360
361 //Check if this is a read operation in a phi node, if so DO NOT PROCESS
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000362 if(mOp.isUse() && (opCode == TargetInstrInfo::PHI)) {
363 DEBUG(std::cerr << "Read Operation in a PHI node\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000364 continue;
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000365 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000366
367 if (const Value* srcI = mOp.getVRegValue()) {
368
369 //Find value in the map
370 std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
371 = valuetoNodeMap.find(srcI);
372
373 //If there is something in the map already, add edges from
374 //those instructions
375 //to this one we are processing
376 if(V != valuetoNodeMap.end()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000377 addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000378
379 //Add to value map
380 V->second.push_back(std::make_pair(i,node));
381 }
382 //Otherwise put it in the map
383 else
384 //Put into value map
385 valuetoNodeMap[mOp.getVRegValue()].push_back(std::make_pair(i, node));
386 }
387 }
388 }
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000389 ++index;
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000390 }
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000391
392 //Loop over LLVM BB, examine phi instructions, and add them to our phiInstr list to process
393 const BasicBlock *llvm_bb = BB->getBasicBlock();
394 for(BasicBlock::const_iterator I = llvm_bb->begin(), E = llvm_bb->end(); I != E; ++I) {
395 if(const PHINode *PN = dyn_cast<PHINode>(I)) {
396 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(PN);
397 for (unsigned j = 0; j < tempMvec.size(); j++) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000398 if(!ignoreInstrs.count(tempMvec[j])) {
399 DEBUG(std::cerr << "Inserting phi instr into map: " << *tempMvec[j] << "\n");
400 phiInstrs.push_back((MachineInstr*) tempMvec[j]);
401 }
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000402 }
403 }
404
405 }
406
Tanya Lattner9532ab92005-03-23 01:47:20 +0000407 addMemEdges(memInstructions, AA, TD);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000408 addMachRegEdges(regNumtoNodeMap);
409
410 //Finally deal with PHI Nodes and Value*
411 for(std::vector<const MachineInstr*>::iterator I = phiInstrs.begin(), E = phiInstrs.end(); I != E; ++I) {
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000412
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000413 //Get Node for this instruction
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000414 std::map<const MachineInstr*, MSchedGraphNode*>::iterator X;
415 X = find(*I);
416
417 if(X == GraphMap.end())
418 continue;
419
420 MSchedGraphNode *node = X->second;
421
422 DEBUG(std::cerr << "Adding ite diff edges for node: " << *node << "\n");
423
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000424 //Loop over operands for this instruction and add value edges
425 for(unsigned i=0; i < (*I)->getNumOperands(); ++i) {
426 //Get Operand
427 const MachineOperand &mOp = (*I)->getOperand(i);
428 if((mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) && mOp.isUse()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000429
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000430 //find the value in the map
431 if (const Value* srcI = mOp.getVRegValue()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000432
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000433 //Find value in the map
434 std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
Tanya Lattner9532ab92005-03-23 01:47:20 +0000435 = valuetoNodeMap.find(srcI);
436
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000437 //If there is something in the map already, add edges from
438 //those instructions
439 //to this one we are processing
440 if(V != valuetoNodeMap.end()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000441 addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000442 }
443 }
444 }
445 }
Tanya Lattner9532ab92005-03-23 01:47:20 +0000446 }
447}
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000448
Tanya Lattner9532ab92005-03-23 01:47:20 +0000449//Add dependencies for Value*s
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000450void MSchedGraph::addValueEdges(std::vector<OpIndexNodePair> &NodesInMap,
451 MSchedGraphNode *destNode, bool nodeIsUse,
Tanya Lattner9532ab92005-03-23 01:47:20 +0000452 bool nodeIsDef, std::vector<const MachineInstr*> &phiInstrs, int diff) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000453
454 for(std::vector<OpIndexNodePair>::iterator I = NodesInMap.begin(),
455 E = NodesInMap.end(); I != E; ++I) {
456
457 //Get node in vectors machine operand that is the same value as node
458 MSchedGraphNode *srcNode = I->second;
459 MachineOperand mOp = srcNode->getInst()->getOperand(I->first);
460
Tanya Lattner9532ab92005-03-23 01:47:20 +0000461 if(diff > 0)
462 if(std::find(phiInstrs.begin(), phiInstrs.end(), srcNode->getInst()) == phiInstrs.end())
463 continue;
464
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000465 //Node is a Def, so add output dep.
466 if(nodeIsDef) {
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000467 if(mOp.isUse()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000468 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=anti)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000469 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
470 MSchedGraphEdge::AntiDep, diff);
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000471 }
472 if(mOp.isDef()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000473 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=output)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000474 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
475 MSchedGraphEdge::OutputDep, diff);
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000476 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000477 }
478 if(nodeIsUse) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000479 if(mOp.isDef()) {
480 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=true)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000481 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
482 MSchedGraphEdge::TrueDep, diff);
Tanya Lattner9532ab92005-03-23 01:47:20 +0000483 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000484 }
485 }
486}
487
Tanya Lattner9532ab92005-03-23 01:47:20 +0000488//Add dependencies for machine registers across iterations
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000489void MSchedGraph::addMachRegEdges(std::map<int, std::vector<OpIndexNodePair> >& regNumtoNodeMap) {
490 //Loop over all machine registers in the map, and add dependencies
491 //between the instructions that use it
492 typedef std::map<int, std::vector<OpIndexNodePair> > regNodeMap;
493 for(regNodeMap::iterator I = regNumtoNodeMap.begin(); I != regNumtoNodeMap.end(); ++I) {
494 //Get the register number
495 int regNum = (*I).first;
496
497 //Get Vector of nodes that use this register
498 std::vector<OpIndexNodePair> Nodes = (*I).second;
499
500 //Loop over nodes and determine the dependence between the other
501 //nodes in the vector
502 for(unsigned i =0; i < Nodes.size(); ++i) {
503
504 //Get src node operator index that uses this machine register
505 int srcOpIndex = Nodes[i].first;
506
507 //Get the actual src Node
508 MSchedGraphNode *srcNode = Nodes[i].second;
509
510 //Get Operand
511 const MachineOperand &srcMOp = srcNode->getInst()->getOperand(srcOpIndex);
512
513 bool srcIsUseandDef = srcMOp.isDef() && srcMOp.isUse();
514 bool srcIsUse = srcMOp.isUse() && !srcMOp.isDef();
515
516
517 //Look at all instructions after this in execution order
518 for(unsigned j=i+1; j < Nodes.size(); ++j) {
519
520 //Sink node is a write
521 if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
522 //Src only uses the register (read)
523 if(srcIsUse)
524 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
525 MSchedGraphEdge::AntiDep);
526
527 else if(srcIsUseandDef) {
528 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
529 MSchedGraphEdge::AntiDep);
530
531 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
532 MSchedGraphEdge::OutputDep);
533 }
534 else
535 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
536 MSchedGraphEdge::OutputDep);
537 }
538 //Dest node is a read
539 else {
540 if(!srcIsUse || srcIsUseandDef)
541 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
542 MSchedGraphEdge::TrueDep);
543 }
544
545 }
546
547 //Look at all the instructions before this one since machine registers
548 //could live across iterations.
549 for(unsigned j = 0; j < i; ++j) {
550 //Sink node is a write
551 if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
552 //Src only uses the register (read)
553 if(srcIsUse)
554 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000555 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000556
557 else if(srcIsUseandDef) {
558 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000559 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000560
561 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000562 MSchedGraphEdge::OutputDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000563 }
564 else
565 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000566 MSchedGraphEdge::OutputDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000567 }
568 //Dest node is a read
569 else {
570 if(!srcIsUse || srcIsUseandDef)
571 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000572 MSchedGraphEdge::TrueDep,1 );
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000573 }
574
575
576 }
577
578 }
579
580 }
581
582}
583
Tanya Lattner9532ab92005-03-23 01:47:20 +0000584//Add edges between all loads and stores
585//Can be less strict with alias analysis and data dependence analysis.
586void MSchedGraph::addMemEdges(const std::vector<MSchedGraphNode*>& memInst, AliasAnalysis &AA, TargetData &TD) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000587
588 //Get Target machine instruction info
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000589 const TargetInstrInfo *TMI = Target.getInstrInfo();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000590
591 //Loop over all memory instructions in the vector
592 //Knowing that they are in execution, add true, anti, and output dependencies
593 for (unsigned srcIndex = 0; srcIndex < memInst.size(); ++srcIndex) {
594
Tanya Lattner9532ab92005-03-23 01:47:20 +0000595 MachineInstr *srcInst = (MachineInstr*) memInst[srcIndex]->getInst();
596
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000597 //Get the machine opCode to determine type of memory instruction
Tanya Lattner9532ab92005-03-23 01:47:20 +0000598 MachineOpCode srcNodeOpCode = srcInst->getOpcode();
599
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000600
601 //All instructions after this one in execution order have an iteration delay of 0
602 for(unsigned destIndex = srcIndex + 1; destIndex < memInst.size(); ++destIndex) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000603
604 MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst();
605
606 //Add Anti dependencies (store after load)
607 //Source is a Load
608 if(TMI->isLoad(srcNodeOpCode)) {
609
610 //Destination is a store
611 if(TMI->isStore(destInst->getOpcode())) {
612
613 //Get the Value* that we are reading from the load, always the first op
614 const MachineOperand &mOp = srcInst->getOperand(0);
615 assert((mOp.isUse() && (mOp.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n");
616
617 //Get the value* for the store
618 const MachineOperand &mOp2 = destInst->getOperand(0);
619 assert(mOp2.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a value*\n");
620
621 //Only add the edge if we can't verify that they do not alias
622 if(AA.alias(mOp2.getVRegValue(),
623 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
624 mOp.getVRegValue(),
625 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
626 != AliasAnalysis::NoAlias) {
627
628 //Add edge from load to store
629 memInst[srcIndex]->addOutEdge(memInst[destIndex],
630 MSchedGraphEdge::MemoryDep,
631 MSchedGraphEdge::AntiDep);
632 }
633 }
634 }
635
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000636 //If source is a store, add output and true dependencies
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000637 if(TMI->isStore(srcNodeOpCode)) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000638
639 //Get the Value* that we are reading from the store (src), always the first op
640 const MachineOperand &mOp = srcInst->getOperand(0);
641 assert(mOp.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a use and a value*\n");
642
643 //Get the Value* that we are reading from the load, always the first op
644 const MachineOperand &mOp2 = srcInst->getOperand(0);
645 assert((mOp2.isUse() && (mOp2.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n");
646
647 //Only add the edge if we can't verify that they do not alias
648 if(AA.alias(mOp2.getVRegValue(),
649 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
650 mOp.getVRegValue(),
651 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
652 != AliasAnalysis::NoAlias) {
653
654 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
655 memInst[srcIndex]->addOutEdge(memInst[destIndex],
656 MSchedGraphEdge::MemoryDep,
657 MSchedGraphEdge::OutputDep);
658 else
659 memInst[srcIndex]->addOutEdge(memInst[destIndex],
660 MSchedGraphEdge::MemoryDep,
661 MSchedGraphEdge::TrueDep);
662 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000663 }
664 }
665
666 //All instructions before the src in execution order have an iteration delay of 1
667 for(unsigned destIndex = 0; destIndex < srcIndex; ++destIndex) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000668
669 MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst();
670
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000671 //source is a Load, so add anti-dependencies (store after load)
Tanya Lattner9532ab92005-03-23 01:47:20 +0000672 if(TMI->isLoad(srcNodeOpCode)) {
673 //Get the Value* that we are reading from the load, always the first op
674 const MachineOperand &mOp = srcInst->getOperand(0);
675 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 +0000676
Tanya Lattner9532ab92005-03-23 01:47:20 +0000677 //Get the value* for the store
678 const MachineOperand &mOp2 = destInst->getOperand(0);
679 assert(mOp2.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a value*\n");
680
681 //Only add the edge if we can't verify that they do not alias
682 if(AA.alias(mOp2.getVRegValue(),
683 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
684 mOp.getVRegValue(),
685 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
686 != AliasAnalysis::NoAlias) {
687 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
688 memInst[srcIndex]->addOutEdge(memInst[destIndex],
689 MSchedGraphEdge::MemoryDep,
690 MSchedGraphEdge::AntiDep, 1);
691 }
692 }
693 if(TMI->isStore(srcNodeOpCode)) {
694
695 //Get the Value* that we are reading from the store (src), always the first op
696 const MachineOperand &mOp = srcInst->getOperand(0);
697 assert(mOp.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a use and a value*\n");
698
699 //Get the Value* that we are reading from the load, always the first op
700 const MachineOperand &mOp2 = srcInst->getOperand(0);
701 assert((mOp2.isUse() && (mOp2.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n");
702
703 //Only add the edge if we can't verify that they do not alias
704 if(AA.alias(mOp2.getVRegValue(),
705 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
706 mOp.getVRegValue(),
707 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
708 != AliasAnalysis::NoAlias) {
709
710 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
711 memInst[srcIndex]->addOutEdge(memInst[destIndex],
712 MSchedGraphEdge::MemoryDep,
713 MSchedGraphEdge::OutputDep, 1);
714 else
715 memInst[srcIndex]->addOutEdge(memInst[destIndex],
716 MSchedGraphEdge::MemoryDep,
717 MSchedGraphEdge::TrueDep, 1);
718 }
719 }
720
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000721 }
722
723 }
724}