Misha Brukman | 6a90f82 | 2004-08-02 14:02:21 +0000 | [diff] [blame] | 1 | //===-- MSchedGraph.cpp - Scheduling Graph ----------------------*- C++ -*-===// |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 10 | // 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 Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | #define DEBUG_TYPE "ModuloSched" |
| 17 | |
| 18 | #include "MSchedGraph.h" |
Misha Brukman | 7da1e6e | 2004-10-10 23:34:50 +0000 | [diff] [blame] | 19 | #include "../SparcV9RegisterInfo.h" |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 20 | #include "../MachineCodeForInstruction.h" |
| 21 | #include "llvm/BasicBlock.h" |
| 22 | #include "llvm/Instructions.h" |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 24 | #include "llvm/Target/TargetInstrInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Misha Brukman | 6a90f82 | 2004-08-02 14:02:21 +0000 | [diff] [blame] | 26 | #include <cstdlib> |
Alkis Evlogimenos | c72c617 | 2004-09-28 14:42:44 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 28 | #include <set> |
| 29 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 32 | //MSchedGraphNode constructor |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 33 | MSchedGraphNode::MSchedGraphNode(const MachineInstr* inst, |
Tanya Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 34 | MSchedGraph *graph, unsigned idx, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 35 | unsigned late, bool isBranch) |
Tanya Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 36 | : Inst(inst), Parent(graph), index(idx), latency(late), isBranchInstr(isBranch) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 37 | |
| 38 | //Add to the graph |
| 39 | graph->addNode(inst, this); |
| 40 | } |
| 41 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 42 | //MSchedGraphNode copy constructor |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 43 | MSchedGraphNode::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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 54 | //Print the node (instruction and latency) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 55 | void MSchedGraphNode::print(std::ostream &os) const { |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 56 | os << "MSchedGraphNode: Inst=" << *Inst << ", latency= " << latency << "\n"; |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 59 | |
| 60 | //Get the edge from a predecessor to this node |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 61 | MSchedGraphEdge MSchedGraphNode::getInEdge(MSchedGraphNode *pred) { |
| 62 | //Loop over all the successors of our predecessor |
| 63 | //return the edge the corresponds to this in edge |
Misha Brukman | 6a90f82 | 2004-08-02 14:02:21 +0000 | [diff] [blame] | 64 | for (MSchedGraphNode::succ_iterator I = pred->succ_begin(), |
| 65 | E = pred->succ_end(); I != E; ++I) { |
| 66 | if (*I == this) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 67 | return I.getEdge(); |
| 68 | } |
| 69 | assert(0 && "Should have found edge between this node and its predecessor!"); |
Misha Brukman | 6a90f82 | 2004-08-02 14:02:21 +0000 | [diff] [blame] | 70 | abort(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 73 | //Get the iteration difference for the edge from this node to its successor |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 74 | unsigned 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 83 | //Get the index into the vector of edges for the edge from pred to this node |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 84 | unsigned 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 97 | |
| 98 | //Determine if succ is a successor of this node |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 99 | bool 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 106 | //Dtermine if pred is a predecessor of this node |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 107 | bool MSchedGraphNode::isPredecessor(MSchedGraphNode *pred) { |
Alkis Evlogimenos | c72c617 | 2004-09-28 14:42:44 +0000 | [diff] [blame] | 108 | if(std::find( Predecessors.begin(), Predecessors.end(), pred) != Predecessors.end()) |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 109 | return true; |
| 110 | else |
| 111 | return false; |
| 112 | } |
| 113 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 114 | //Add a node to the graph |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 115 | void 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 125 | //Delete a node to the graph |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 126 | void MSchedGraph::deleteNode(MSchedGraphNode *node) { |
| 127 | |
| 128 | //Delete the edge to this node from all predecessors |
Tanya Lattner | db1680b | 2005-02-16 04:00:59 +0000 | [diff] [blame] | 129 | 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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 133 | } |
Tanya Lattner | db1680b | 2005-02-16 04:00:59 +0000 | [diff] [blame] | 134 | |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 135 | //Remove this node from the graph |
| 136 | GraphMap.erase(node->getInst()); |
| 137 | |
| 138 | } |
| 139 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 140 | //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. |
| 143 | MSchedGraph::MSchedGraph(const MachineBasicBlock *bb, const TargetMachine &targ, AliasAnalysis &AA, TargetData &TD, std::map<const MachineInstr*, unsigned> &ignoreInstrs) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 144 | : BB(bb), Target(targ) { |
| 145 | |
| 146 | //Make sure BB is not null, |
| 147 | assert(BB != NULL && "Basic Block is null"); |
| 148 | |
Tanya Lattner | 420025b | 2004-10-10 22:44:35 +0000 | [diff] [blame] | 149 | //DEBUG(std::cerr << "Constructing graph for " << bb << "\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 150 | |
| 151 | //Create nodes and edges for this BB |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 152 | buildNodesAndEdges(AA, TD, ignoreInstrs); |
| 153 | |
| 154 | //Experimental! |
| 155 | //addBranchEdges(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 158 | //Copies the graph and keeps a map from old to new nodes |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 159 | MSchedGraph::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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 193 | //Deconstructor, deletes all nodes in the graph |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 194 | MSchedGraph::~MSchedGraph () { |
| 195 | for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) |
| 196 | delete I->second; |
| 197 | } |
| 198 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 199 | |
| 200 | //Experimental code to add edges from the branch to all nodes dependent upon it. |
| 201 | void 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. |
| 224 | void 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 |
| 272 | void MSchedGraph::buildNodesAndEdges(AliasAnalysis &AA, TargetData &TD, std::map<const MachineInstr*, unsigned> &ignoreInstrs) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 273 | |
| 274 | //Get Machine target information for calculating latency |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 275 | const TargetInstrInfo *MTI = Target.getInstrInfo(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 276 | |
| 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 Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 283 | unsigned index = 0; |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 284 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 285 | //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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 287 | |
| 288 | //Ignore indvar instructions |
| 289 | if(ignoreInstrs.count(MI)) { |
| 290 | ++index; |
| 291 | continue; |
| 292 | } |
| 293 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 294 | //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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 297 | |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 298 | MachineOpCode opCode = MI->getOpcode(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 299 | int delay; |
| 300 | |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 301 | #if 0 // FIXME: LOOK INTO THIS |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 302 | //Check if subsequent instructions can be issued before |
| 303 | //the result is ready, if so use min delay. |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 304 | if(MTI->hasResultInterlock(MIopCode)) |
| 305 | delay = MTI->minLatency(MIopCode); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 306 | else |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 307 | #endif |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 308 | //Get delay |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 309 | delay = MTI->maxLatency(opCode); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 310 | |
| 311 | //Create new node for this machine instruction and add to the graph. |
| 312 | //Create only if not a nop |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 313 | if(MTI->isNop(opCode)) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 314 | continue; |
| 315 | |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 316 | //Sparc BE does not use PHI opcode, so assert on this case |
| 317 | assert(opCode != TargetInstrInfo::PHI && "Did not expect PHI opcode"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 318 | |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 319 | bool isBranch = false; |
| 320 | |
| 321 | //We want to flag the branch node to treat it special |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 322 | if(MTI->isBranch(opCode)) |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 323 | isBranch = true; |
| 324 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 325 | //Node is created and added to the graph automatically |
Tanya Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 326 | MSchedGraphNode *node = new MSchedGraphNode(MI, this, index, delay, isBranch); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 327 | |
| 328 | DEBUG(std::cerr << "Created Node: " << *node << "\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 329 | |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 330 | //Check OpCode to keep track of memory operations to add memory dependencies later. |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 331 | if(MTI->isLoad(opCode) || MTI->isStore(opCode)) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 332 | 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 Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 341 | //Check if it has an allocated register |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 342 | if(mOp.hasAllocatedReg()) { |
| 343 | int regNum = mOp.getReg(); |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 344 | |
| 345 | if(regNum != SparcV9::g0) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 346 | //Put into our map |
| 347 | regNumtoNodeMap[regNum].push_back(std::make_pair(i, node)); |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 348 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 349 | 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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 362 | if(mOp.isUse() && (opCode == TargetInstrInfo::PHI)) { |
| 363 | DEBUG(std::cerr << "Read Operation in a PHI node\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 364 | continue; |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 365 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 366 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 377 | addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 378 | |
| 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 Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 389 | ++index; |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 390 | } |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 391 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 398 | 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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | } |
| 406 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 407 | addMemEdges(memInstructions, AA, TD); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 408 | 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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 412 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 413 | //Get Node for this instruction |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 414 | 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 Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 424 | //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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 429 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 430 | //find the value in the map |
| 431 | if (const Value* srcI = mOp.getVRegValue()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 432 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 433 | //Find value in the map |
| 434 | std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 435 | = valuetoNodeMap.find(srcI); |
| 436 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 437 | //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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 441 | addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 448 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 449 | //Add dependencies for Value*s |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 450 | void MSchedGraph::addValueEdges(std::vector<OpIndexNodePair> &NodesInMap, |
| 451 | MSchedGraphNode *destNode, bool nodeIsUse, |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 452 | bool nodeIsDef, std::vector<const MachineInstr*> &phiInstrs, int diff) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 453 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 461 | if(diff > 0) |
| 462 | if(std::find(phiInstrs.begin(), phiInstrs.end(), srcNode->getInst()) == phiInstrs.end()) |
| 463 | continue; |
| 464 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 465 | //Node is a Def, so add output dep. |
| 466 | if(nodeIsDef) { |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 467 | if(mOp.isUse()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 468 | DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=anti)\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 469 | srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep, |
| 470 | MSchedGraphEdge::AntiDep, diff); |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 471 | } |
| 472 | if(mOp.isDef()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 473 | DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=output)\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 474 | srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep, |
| 475 | MSchedGraphEdge::OutputDep, diff); |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 476 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 477 | } |
| 478 | if(nodeIsUse) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 479 | if(mOp.isDef()) { |
| 480 | DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=true)\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 481 | srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep, |
| 482 | MSchedGraphEdge::TrueDep, diff); |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 483 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 488 | //Add dependencies for machine registers across iterations |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 489 | void 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 Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 555 | MSchedGraphEdge::AntiDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 556 | |
| 557 | else if(srcIsUseandDef) { |
| 558 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 559 | MSchedGraphEdge::AntiDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 560 | |
| 561 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 562 | MSchedGraphEdge::OutputDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 563 | } |
| 564 | else |
| 565 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 566 | MSchedGraphEdge::OutputDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 567 | } |
| 568 | //Dest node is a read |
| 569 | else { |
| 570 | if(!srcIsUse || srcIsUseandDef) |
| 571 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 572 | MSchedGraphEdge::TrueDep,1 ); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | |
| 576 | } |
| 577 | |
| 578 | } |
| 579 | |
| 580 | } |
| 581 | |
| 582 | } |
| 583 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 584 | //Add edges between all loads and stores |
| 585 | //Can be less strict with alias analysis and data dependence analysis. |
| 586 | void MSchedGraph::addMemEdges(const std::vector<MSchedGraphNode*>& memInst, AliasAnalysis &AA, TargetData &TD) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 587 | |
| 588 | //Get Target machine instruction info |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 589 | const TargetInstrInfo *TMI = Target.getInstrInfo(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 590 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 595 | MachineInstr *srcInst = (MachineInstr*) memInst[srcIndex]->getInst(); |
| 596 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 597 | //Get the machine opCode to determine type of memory instruction |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 598 | MachineOpCode srcNodeOpCode = srcInst->getOpcode(); |
| 599 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 600 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 603 | |
| 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 Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 636 | //If source is a store, add output and true dependencies |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 637 | if(TMI->isStore(srcNodeOpCode)) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 638 | |
| 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 Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 663 | } |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 668 | |
| 669 | MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst(); |
| 670 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 671 | //source is a Load, so add anti-dependencies (store after load) |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 672 | 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 Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 676 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 677 | //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 Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | } |
| 724 | } |