blob: f60d3d5f613373b195200ac228bfdcb2e97647c8 [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 Lattner5e9f3522005-03-29 20:35:10 +0000144MSchedGraph::MSchedGraph(const MachineBasicBlock *bb, const TargetMachine &targ, AliasAnalysis &AA,
145 TargetData &TD, std::map<const MachineInstr*, unsigned> &ignoreInstrs,
146 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 Lattner5e9f3522005-03-29 20:35:10 +0000156 buildNodesAndEdges(AA, TD, 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 Lattner5e9f3522005-03-29 20:35:10 +0000276void MSchedGraph::buildNodesAndEdges(AliasAnalysis &AA, TargetData &TD,
277 std::map<const MachineInstr*, unsigned> &ignoreInstrs,
278 DependenceAnalyzer &DA,
279 std::map<MachineInstr*, Instruction*> &machineTollvm) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000280
281 //Get Machine target information for calculating latency
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000282 const TargetInstrInfo *MTI = Target.getInstrInfo();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000283
284 std::vector<MSchedGraphNode*> memInstructions;
285 std::map<int, std::vector<OpIndexNodePair> > regNumtoNodeMap;
286 std::map<const Value*, std::vector<OpIndexNodePair> > valuetoNodeMap;
287
288 //Save PHI instructions to deal with later
289 std::vector<const MachineInstr*> phiInstrs;
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000290 unsigned index = 0;
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000291
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000292 //Loop over instructions in MBB and add nodes and edges
293 for (MachineBasicBlock::const_iterator MI = BB->begin(), e = BB->end(); MI != e; ++MI) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000294
295 //Ignore indvar instructions
296 if(ignoreInstrs.count(MI)) {
297 ++index;
298 continue;
299 }
300
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000301 //Get each instruction of machine basic block, get the delay
302 //using the op code, create a new node for it, and add to the
303 //graph.
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000304
Tanya Lattner4cffb582004-05-26 06:27:18 +0000305 MachineOpCode opCode = MI->getOpcode();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000306 int delay;
307
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000308#if 0 // FIXME: LOOK INTO THIS
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000309 //Check if subsequent instructions can be issued before
310 //the result is ready, if so use min delay.
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000311 if(MTI->hasResultInterlock(MIopCode))
312 delay = MTI->minLatency(MIopCode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000313 else
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000314#endif
Tanya Lattner4cffb582004-05-26 06:27:18 +0000315 //Get delay
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000316 delay = MTI->maxLatency(opCode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000317
318 //Create new node for this machine instruction and add to the graph.
319 //Create only if not a nop
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000320 if(MTI->isNop(opCode))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000321 continue;
322
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000323 //Sparc BE does not use PHI opcode, so assert on this case
324 assert(opCode != TargetInstrInfo::PHI && "Did not expect PHI opcode");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000325
Tanya Lattner4cffb582004-05-26 06:27:18 +0000326 bool isBranch = false;
327
328 //We want to flag the branch node to treat it special
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000329 if(MTI->isBranch(opCode))
Tanya Lattner4cffb582004-05-26 06:27:18 +0000330 isBranch = true;
331
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000332 //Node is created and added to the graph automatically
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000333 MSchedGraphNode *node = new MSchedGraphNode(MI, this, index, delay, isBranch);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000334
335 DEBUG(std::cerr << "Created Node: " << *node << "\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000336
Tanya Lattner4cffb582004-05-26 06:27:18 +0000337 //Check OpCode to keep track of memory operations to add memory dependencies later.
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000338 if(MTI->isLoad(opCode) || MTI->isStore(opCode))
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000339 memInstructions.push_back(node);
340
341 //Loop over all operands, and put them into the register number to
342 //graph node map for determining dependencies
343 //If an operands is a use/def, we have an anti dependence to itself
344 for(unsigned i=0; i < MI->getNumOperands(); ++i) {
345 //Get Operand
346 const MachineOperand &mOp = MI->getOperand(i);
347
Tanya Lattner4cffb582004-05-26 06:27:18 +0000348 //Check if it has an allocated register
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000349 if(mOp.hasAllocatedReg()) {
350 int regNum = mOp.getReg();
Tanya Lattner4cffb582004-05-26 06:27:18 +0000351
352 if(regNum != SparcV9::g0) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000353 //Put into our map
354 regNumtoNodeMap[regNum].push_back(std::make_pair(i, node));
Tanya Lattner4cffb582004-05-26 06:27:18 +0000355 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000356 continue;
357 }
358
359
360 //Add virtual registers dependencies
361 //Check if any exist in the value map already and create dependencies
362 //between them.
363 if(mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) {
364
365 //Make sure virtual register value is not null
366 assert((mOp.getVRegValue() != NULL) && "Null value is defined");
367
368 //Check if this is a read operation in a phi node, if so DO NOT PROCESS
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000369 if(mOp.isUse() && (opCode == TargetInstrInfo::PHI)) {
370 DEBUG(std::cerr << "Read Operation in a PHI node\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000371 continue;
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000372 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000373
374 if (const Value* srcI = mOp.getVRegValue()) {
375
376 //Find value in the map
377 std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
378 = valuetoNodeMap.find(srcI);
379
380 //If there is something in the map already, add edges from
381 //those instructions
382 //to this one we are processing
383 if(V != valuetoNodeMap.end()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000384 addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000385
386 //Add to value map
387 V->second.push_back(std::make_pair(i,node));
388 }
389 //Otherwise put it in the map
390 else
391 //Put into value map
392 valuetoNodeMap[mOp.getVRegValue()].push_back(std::make_pair(i, node));
393 }
394 }
395 }
Tanya Lattner28e5eab2004-11-28 23:36:15 +0000396 ++index;
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000397 }
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000398
399 //Loop over LLVM BB, examine phi instructions, and add them to our phiInstr list to process
400 const BasicBlock *llvm_bb = BB->getBasicBlock();
401 for(BasicBlock::const_iterator I = llvm_bb->begin(), E = llvm_bb->end(); I != E; ++I) {
402 if(const PHINode *PN = dyn_cast<PHINode>(I)) {
403 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(PN);
404 for (unsigned j = 0; j < tempMvec.size(); j++) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000405 if(!ignoreInstrs.count(tempMvec[j])) {
406 DEBUG(std::cerr << "Inserting phi instr into map: " << *tempMvec[j] << "\n");
407 phiInstrs.push_back((MachineInstr*) tempMvec[j]);
408 }
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000409 }
410 }
411
412 }
413
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000414 addMemEdges(memInstructions, AA, TD, DA, machineTollvm);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000415 addMachRegEdges(regNumtoNodeMap);
416
417 //Finally deal with PHI Nodes and Value*
418 for(std::vector<const MachineInstr*>::iterator I = phiInstrs.begin(), E = phiInstrs.end(); I != E; ++I) {
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000419
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000420 //Get Node for this instruction
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000421 std::map<const MachineInstr*, MSchedGraphNode*>::iterator X;
422 X = find(*I);
423
424 if(X == GraphMap.end())
425 continue;
426
427 MSchedGraphNode *node = X->second;
428
429 DEBUG(std::cerr << "Adding ite diff edges for node: " << *node << "\n");
430
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000431 //Loop over operands for this instruction and add value edges
432 for(unsigned i=0; i < (*I)->getNumOperands(); ++i) {
433 //Get Operand
434 const MachineOperand &mOp = (*I)->getOperand(i);
435 if((mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) && mOp.isUse()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000436
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000437 //find the value in the map
438 if (const Value* srcI = mOp.getVRegValue()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000439
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000440 //Find value in the map
441 std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
Tanya Lattner9532ab92005-03-23 01:47:20 +0000442 = valuetoNodeMap.find(srcI);
443
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000444 //If there is something in the map already, add edges from
445 //those instructions
446 //to this one we are processing
447 if(V != valuetoNodeMap.end()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000448 addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000449 }
450 }
451 }
452 }
Tanya Lattner9532ab92005-03-23 01:47:20 +0000453 }
454}
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000455
Tanya Lattner9532ab92005-03-23 01:47:20 +0000456//Add dependencies for Value*s
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000457void MSchedGraph::addValueEdges(std::vector<OpIndexNodePair> &NodesInMap,
458 MSchedGraphNode *destNode, bool nodeIsUse,
Tanya Lattner9532ab92005-03-23 01:47:20 +0000459 bool nodeIsDef, std::vector<const MachineInstr*> &phiInstrs, int diff) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000460
461 for(std::vector<OpIndexNodePair>::iterator I = NodesInMap.begin(),
462 E = NodesInMap.end(); I != E; ++I) {
463
464 //Get node in vectors machine operand that is the same value as node
465 MSchedGraphNode *srcNode = I->second;
466 MachineOperand mOp = srcNode->getInst()->getOperand(I->first);
467
Tanya Lattner9532ab92005-03-23 01:47:20 +0000468 if(diff > 0)
469 if(std::find(phiInstrs.begin(), phiInstrs.end(), srcNode->getInst()) == phiInstrs.end())
470 continue;
471
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000472 //Node is a Def, so add output dep.
473 if(nodeIsDef) {
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000474 if(mOp.isUse()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000475 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=anti)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000476 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
477 MSchedGraphEdge::AntiDep, diff);
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000478 }
479 if(mOp.isDef()) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000480 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=output)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000481 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
482 MSchedGraphEdge::OutputDep, diff);
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000483 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000484 }
485 if(nodeIsUse) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000486 if(mOp.isDef()) {
487 DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=true)\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000488 srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
489 MSchedGraphEdge::TrueDep, diff);
Tanya Lattner9532ab92005-03-23 01:47:20 +0000490 }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000491 }
492 }
493}
494
Tanya Lattner9532ab92005-03-23 01:47:20 +0000495//Add dependencies for machine registers across iterations
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000496void MSchedGraph::addMachRegEdges(std::map<int, std::vector<OpIndexNodePair> >& regNumtoNodeMap) {
497 //Loop over all machine registers in the map, and add dependencies
498 //between the instructions that use it
499 typedef std::map<int, std::vector<OpIndexNodePair> > regNodeMap;
500 for(regNodeMap::iterator I = regNumtoNodeMap.begin(); I != regNumtoNodeMap.end(); ++I) {
501 //Get the register number
502 int regNum = (*I).first;
503
504 //Get Vector of nodes that use this register
505 std::vector<OpIndexNodePair> Nodes = (*I).second;
506
507 //Loop over nodes and determine the dependence between the other
508 //nodes in the vector
509 for(unsigned i =0; i < Nodes.size(); ++i) {
510
511 //Get src node operator index that uses this machine register
512 int srcOpIndex = Nodes[i].first;
513
514 //Get the actual src Node
515 MSchedGraphNode *srcNode = Nodes[i].second;
516
517 //Get Operand
518 const MachineOperand &srcMOp = srcNode->getInst()->getOperand(srcOpIndex);
519
520 bool srcIsUseandDef = srcMOp.isDef() && srcMOp.isUse();
521 bool srcIsUse = srcMOp.isUse() && !srcMOp.isDef();
522
523
524 //Look at all instructions after this in execution order
525 for(unsigned j=i+1; j < Nodes.size(); ++j) {
526
527 //Sink node is a write
528 if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
529 //Src only uses the register (read)
530 if(srcIsUse)
531 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
532 MSchedGraphEdge::AntiDep);
533
534 else if(srcIsUseandDef) {
535 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
536 MSchedGraphEdge::AntiDep);
537
538 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
539 MSchedGraphEdge::OutputDep);
540 }
541 else
542 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
543 MSchedGraphEdge::OutputDep);
544 }
545 //Dest node is a read
546 else {
547 if(!srcIsUse || srcIsUseandDef)
548 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
549 MSchedGraphEdge::TrueDep);
550 }
551
552 }
553
554 //Look at all the instructions before this one since machine registers
555 //could live across iterations.
556 for(unsigned j = 0; j < i; ++j) {
557 //Sink node is a write
558 if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
559 //Src only uses the register (read)
560 if(srcIsUse)
561 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000562 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000563
564 else if(srcIsUseandDef) {
565 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000566 MSchedGraphEdge::AntiDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000567
568 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000569 MSchedGraphEdge::OutputDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000570 }
571 else
572 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000573 MSchedGraphEdge::OutputDep, 1);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000574 }
575 //Dest node is a read
576 else {
577 if(!srcIsUse || srcIsUseandDef)
578 srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister,
Tanya Lattner4cffb582004-05-26 06:27:18 +0000579 MSchedGraphEdge::TrueDep,1 );
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000580 }
581
582
583 }
584
585 }
586
587 }
588
589}
590
Tanya Lattner9532ab92005-03-23 01:47:20 +0000591//Add edges between all loads and stores
592//Can be less strict with alias analysis and data dependence analysis.
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000593void MSchedGraph::addMemEdges(const std::vector<MSchedGraphNode*>& memInst, AliasAnalysis &AA,
594 TargetData &TD, DependenceAnalyzer &DA,
595 std::map<MachineInstr*, Instruction*> &machineTollvm) {
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000596
597 //Get Target machine instruction info
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000598 const TargetInstrInfo *TMI = Target.getInstrInfo();
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000599
600 //Loop over all memory instructions in the vector
601 //Knowing that they are in execution, add true, anti, and output dependencies
602 for (unsigned srcIndex = 0; srcIndex < memInst.size(); ++srcIndex) {
603
Tanya Lattner9532ab92005-03-23 01:47:20 +0000604 MachineInstr *srcInst = (MachineInstr*) memInst[srcIndex]->getInst();
605
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000606 //Get the machine opCode to determine type of memory instruction
Tanya Lattner9532ab92005-03-23 01:47:20 +0000607 MachineOpCode srcNodeOpCode = srcInst->getOpcode();
608
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000609 //All instructions after this one in execution order have an iteration delay of 0
610 for(unsigned destIndex = srcIndex + 1; destIndex < memInst.size(); ++destIndex) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000611
612 MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst();
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000613 bool malias = false;
614
615 DEBUG(std::cerr << "MInst1: " << *srcInst << "\n");
616 DEBUG(std::cerr << "Inst1: " << *machineTollvm[srcInst] << "\n");
617 DEBUG(std::cerr << "MInst2: " << *destInst << "\n");
618 DEBUG(std::cerr << "Inst2: " << *machineTollvm[destInst] << "\n");
Tanya Lattner9532ab92005-03-23 01:47:20 +0000619
620 //Add Anti dependencies (store after load)
621 //Source is a Load
622 if(TMI->isLoad(srcNodeOpCode)) {
623
624 //Destination is a store
625 if(TMI->isStore(destInst->getOpcode())) {
626
627 //Get the Value* that we are reading from the load, always the first op
628 const MachineOperand &mOp = srcInst->getOperand(0);
Tanya Lattner9532ab92005-03-23 01:47:20 +0000629 const MachineOperand &mOp2 = destInst->getOperand(0);
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000630
631 if(mOp.hasAllocatedReg())
632 if(mOp.getReg() == SparcV9::g0)
633 continue;
634 else
635 malias = true;
636 if(mOp2.hasAllocatedReg())
637 if(mOp2.getReg() == SparcV9::g0)
638 continue;
639 else
640 malias = true;
Tanya Lattner9532ab92005-03-23 01:47:20 +0000641
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000642 //compare to DA
643 DependenceResult dr = DA.getDependenceInfo(machineTollvm[srcInst], machineTollvm[destInst]);
644
Tanya Lattner9532ab92005-03-23 01:47:20 +0000645 //Only add the edge if we can't verify that they do not alias
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000646 if(malias || AA.alias(mOp2.getVRegValue(),
Tanya Lattner9532ab92005-03-23 01:47:20 +0000647 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
648 mOp.getVRegValue(),
649 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
650 != AliasAnalysis::NoAlias) {
651
652 //Add edge from load to store
653 memInst[srcIndex]->addOutEdge(memInst[destIndex],
654 MSchedGraphEdge::MemoryDep,
655 MSchedGraphEdge::AntiDep);
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000656
657 assert(dr.dependences.size() == 1 && "Expected at least one dependence\n");
658
Tanya Lattner9532ab92005-03-23 01:47:20 +0000659 }
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000660 else
661 assert(dr.dependences.size() == 0 && "Expected no dependence\n");
Tanya Lattner9532ab92005-03-23 01:47:20 +0000662 }
663 }
664
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000665 //If source is a store, add output and true dependencies
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000666 if(TMI->isStore(srcNodeOpCode)) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000667
668 //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);
671
672 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
683 //compare to DA
684 DependenceResult dr = DA.getDependenceInfo(machineTollvm[srcInst], machineTollvm[destInst]);
Tanya Lattner9532ab92005-03-23 01:47:20 +0000685
686 //Only add the edge if we can't verify that they do not alias
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000687 if(malias || AA.alias(mOp2.getVRegValue(),
Tanya Lattner9532ab92005-03-23 01:47:20 +0000688 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
689 mOp.getVRegValue(),
690 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
691 != AliasAnalysis::NoAlias) {
692
693 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
694 memInst[srcIndex]->addOutEdge(memInst[destIndex],
695 MSchedGraphEdge::MemoryDep,
696 MSchedGraphEdge::OutputDep);
697 else
698 memInst[srcIndex]->addOutEdge(memInst[destIndex],
699 MSchedGraphEdge::MemoryDep,
700 MSchedGraphEdge::TrueDep);
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000701 assert(dr.dependences.size() == 1 && "Expected at least one dependence\n");
702
Tanya Lattner9532ab92005-03-23 01:47:20 +0000703 }
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000704
705 else
706 assert(dr.dependences.size() == 0 && "Expected no dependence\n");
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000707 }
708 }
709
710 //All instructions before the src in execution order have an iteration delay of 1
711 for(unsigned destIndex = 0; destIndex < srcIndex; ++destIndex) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000712
713 MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst();
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000714 bool malias = false;
Tanya Lattner9532ab92005-03-23 01:47:20 +0000715
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000716 //source is a Load, so add anti-dependencies (store after load)
Tanya Lattner9532ab92005-03-23 01:47:20 +0000717 if(TMI->isLoad(srcNodeOpCode)) {
Tanya Lattner9532ab92005-03-23 01:47:20 +0000718
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000719 //Get the Value* that we are reading from the load, always the first op
720 const MachineOperand &mOp = srcInst->getOperand(0);
721 const MachineOperand &mOp2 = destInst->getOperand(0);
722
723 if(mOp.hasAllocatedReg())
724 if(mOp.getReg() == SparcV9::g0)
725 continue;
726 else
727 malias = true;
728 if(mOp2.hasAllocatedReg())
729 if(mOp2.getReg() == SparcV9::g0)
730 continue;
731 else
732 malias = true;
733
734 //Only add the edge if we can't verify that they do not alias
735 if(AA.alias(mOp2.getVRegValue(),
736 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
737 mOp.getVRegValue(),
738 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
739 != AliasAnalysis::NoAlias) {
740 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
741 memInst[srcIndex]->addOutEdge(memInst[destIndex],
742 MSchedGraphEdge::MemoryDep,
743 MSchedGraphEdge::AntiDep, 1);
744 }
Tanya Lattner9532ab92005-03-23 01:47:20 +0000745 }
746 if(TMI->isStore(srcNodeOpCode)) {
747
Tanya Lattner9532ab92005-03-23 01:47:20 +0000748 //Get the Value* that we are reading from the load, always the first op
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000749 const MachineOperand &mOp = srcInst->getOperand(0);
750 const MachineOperand &mOp2 = destInst->getOperand(0);
Tanya Lattner9532ab92005-03-23 01:47:20 +0000751
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000752 if(mOp.hasAllocatedReg())
753 if(mOp.getReg() == SparcV9::g0)
754 continue;
755 else
756 malias = true;
757 if(mOp2.hasAllocatedReg())
758 if(mOp2.getReg() == SparcV9::g0)
759 continue;
760 else
761 malias = true;
762
Tanya Lattner9532ab92005-03-23 01:47:20 +0000763 //Only add the edge if we can't verify that they do not alias
764 if(AA.alias(mOp2.getVRegValue(),
765 (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()),
766 mOp.getVRegValue(),
767 (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType()))
768 != AliasAnalysis::NoAlias) {
769
770 if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode()))
771 memInst[srcIndex]->addOutEdge(memInst[destIndex],
772 MSchedGraphEdge::MemoryDep,
773 MSchedGraphEdge::OutputDep, 1);
774 else
775 memInst[srcIndex]->addOutEdge(memInst[destIndex],
776 MSchedGraphEdge::MemoryDep,
777 MSchedGraphEdge::TrueDep, 1);
778 }
779 }
780
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000781 }
782
783 }
784}