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" |
Chris Lattner | 26bc78e | 2005-03-24 05:13:53 +0000 | [diff] [blame] | 23 | #include "llvm/Type.h" |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 25 | #include "llvm/Target/TargetInstrInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Misha Brukman | 6a90f82 | 2004-08-02 14:02:21 +0000 | [diff] [blame] | 27 | #include <cstdlib> |
Alkis Evlogimenos | c72c617 | 2004-09-28 14:42:44 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 29 | #include <set> |
| 30 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 33 | //MSchedGraphNode constructor |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 34 | MSchedGraphNode::MSchedGraphNode(const MachineInstr* inst, |
Tanya Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 35 | MSchedGraph *graph, unsigned idx, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 36 | unsigned late, bool isBranch) |
Tanya Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 37 | : Inst(inst), Parent(graph), index(idx), latency(late), isBranchInstr(isBranch) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 38 | |
| 39 | //Add to the graph |
| 40 | graph->addNode(inst, this); |
| 41 | } |
| 42 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 43 | //MSchedGraphNode copy constructor |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 44 | MSchedGraphNode::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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 55 | //Print the node (instruction and latency) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 56 | void MSchedGraphNode::print(std::ostream &os) const { |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 57 | os << "MSchedGraphNode: Inst=" << *Inst << ", latency= " << latency << "\n"; |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 60 | |
| 61 | //Get the edge from a predecessor to this node |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 62 | MSchedGraphEdge MSchedGraphNode::getInEdge(MSchedGraphNode *pred) { |
| 63 | //Loop over all the successors of our predecessor |
| 64 | //return the edge the corresponds to this in edge |
Misha Brukman | 6a90f82 | 2004-08-02 14:02:21 +0000 | [diff] [blame] | 65 | for (MSchedGraphNode::succ_iterator I = pred->succ_begin(), |
| 66 | E = pred->succ_end(); I != E; ++I) { |
| 67 | if (*I == this) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 68 | return I.getEdge(); |
| 69 | } |
| 70 | assert(0 && "Should have found edge between this node and its predecessor!"); |
Misha Brukman | 6a90f82 | 2004-08-02 14:02:21 +0000 | [diff] [blame] | 71 | abort(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 74 | //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] | 75 | unsigned 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 84 | //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] | 85 | unsigned 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 98 | |
| 99 | //Determine if succ is a successor of this node |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 100 | bool 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 107 | //Dtermine if pred is a predecessor of this node |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 108 | bool MSchedGraphNode::isPredecessor(MSchedGraphNode *pred) { |
Alkis Evlogimenos | c72c617 | 2004-09-28 14:42:44 +0000 | [diff] [blame] | 109 | if(std::find( Predecessors.begin(), Predecessors.end(), pred) != Predecessors.end()) |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 110 | return true; |
| 111 | else |
| 112 | return false; |
| 113 | } |
| 114 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 115 | //Add a node to the graph |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 116 | void 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 126 | //Delete a node to the graph |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 127 | void MSchedGraph::deleteNode(MSchedGraphNode *node) { |
| 128 | |
| 129 | //Delete the edge to this node from all predecessors |
Tanya Lattner | db1680b | 2005-02-16 04:00:59 +0000 | [diff] [blame] | 130 | 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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 134 | } |
Tanya Lattner | db1680b | 2005-02-16 04:00:59 +0000 | [diff] [blame] | 135 | |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 136 | //Remove this node from the graph |
| 137 | GraphMap.erase(node->getInst()); |
| 138 | |
| 139 | } |
| 140 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 141 | //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. |
| 144 | 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] | 145 | : BB(bb), Target(targ) { |
| 146 | |
| 147 | //Make sure BB is not null, |
| 148 | assert(BB != NULL && "Basic Block is null"); |
| 149 | |
Tanya Lattner | 420025b | 2004-10-10 22:44:35 +0000 | [diff] [blame] | 150 | //DEBUG(std::cerr << "Constructing graph for " << bb << "\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 151 | |
| 152 | //Create nodes and edges for this BB |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 153 | buildNodesAndEdges(AA, TD, ignoreInstrs); |
| 154 | |
| 155 | //Experimental! |
| 156 | //addBranchEdges(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 159 | //Copies the graph and keeps a map from old to new nodes |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 160 | MSchedGraph::MSchedGraph(const MSchedGraph &G, std::map<MSchedGraphNode*, MSchedGraphNode*> &newNodes) |
| 161 | : BB(G.BB), Target(G.Target) { |
| 162 | |
| 163 | std::map<MSchedGraphNode*, MSchedGraphNode*> oldToNew; |
| 164 | //Copy all nodes |
| 165 | for(MSchedGraph::const_iterator N = G.GraphMap.begin(), NE = G.GraphMap.end(); |
| 166 | N != NE; ++N) { |
| 167 | MSchedGraphNode *newNode = new MSchedGraphNode(*(N->second)); |
| 168 | oldToNew[&*(N->second)] = newNode; |
| 169 | newNodes[newNode] = &*(N->second); |
| 170 | GraphMap[&*(N->first)] = newNode; |
| 171 | } |
| 172 | |
| 173 | //Loop over nodes and update edges to point to new nodes |
| 174 | for(MSchedGraph::iterator N = GraphMap.begin(), NE = GraphMap.end(); N != NE; ++N) { |
| 175 | |
| 176 | //Get the node we are dealing with |
| 177 | MSchedGraphNode *node = &*(N->second); |
| 178 | |
| 179 | node->setParent(this); |
| 180 | |
| 181 | //Loop over nodes successors and predecessors and update to the new nodes |
| 182 | for(unsigned i = 0; i < node->pred_size(); ++i) { |
| 183 | node->setPredecessor(i, oldToNew[node->getPredecessor(i)]); |
| 184 | } |
| 185 | |
| 186 | for(unsigned i = 0; i < node->succ_size(); ++i) { |
| 187 | MSchedGraphEdge *edge = node->getSuccessor(i); |
| 188 | MSchedGraphNode *oldDest = edge->getDest(); |
| 189 | edge->setDest(oldToNew[oldDest]); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 194 | //Deconstructor, deletes all nodes in the graph |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 195 | MSchedGraph::~MSchedGraph () { |
| 196 | for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) |
| 197 | delete I->second; |
| 198 | } |
| 199 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 200 | |
| 201 | //Experimental code to add edges from the branch to all nodes dependent upon it. |
| 202 | void hasPath(MSchedGraphNode *node, std::set<MSchedGraphNode*> &visited, |
| 203 | std::set<MSchedGraphNode*> &branches, MSchedGraphNode *startNode, |
| 204 | std::set<std::pair<MSchedGraphNode*,MSchedGraphNode*> > &newEdges ) { |
| 205 | |
| 206 | visited.insert(node); |
| 207 | DEBUG(std::cerr << "Visiting: " << *node << "\n"); |
| 208 | //Loop over successors |
| 209 | for(unsigned i = 0; i < node->succ_size(); ++i) { |
| 210 | MSchedGraphEdge *edge = node->getSuccessor(i); |
| 211 | MSchedGraphNode *dest = edge->getDest(); |
| 212 | if(branches.count(dest)) |
| 213 | newEdges.insert(std::make_pair(dest, startNode)); |
| 214 | |
| 215 | //only visit if we have not already |
| 216 | else if(!visited.count(dest)) { |
| 217 | if(edge->getIteDiff() == 0) |
| 218 | hasPath(dest, visited, branches, startNode, newEdges);} |
| 219 | |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | |
| 224 | //Experimental code to add edges from the branch to all nodes dependent upon it. |
| 225 | void MSchedGraph::addBranchEdges() { |
| 226 | std::set<MSchedGraphNode*> branches; |
| 227 | std::set<MSchedGraphNode*> nodes; |
| 228 | |
| 229 | for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) { |
| 230 | if(I->second->isBranch()) |
| 231 | if(I->second->hasPredecessors()) |
| 232 | branches.insert(I->second); |
| 233 | } |
| 234 | |
| 235 | //See if there is a path first instruction to the branches, if so, add an |
| 236 | //iteration dependence between that node and the branch |
| 237 | std::set<std::pair<MSchedGraphNode*, MSchedGraphNode*> > newEdges; |
| 238 | for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) { |
| 239 | std::set<MSchedGraphNode*> visited; |
| 240 | hasPath((I->second), visited, branches, (I->second), newEdges); |
| 241 | } |
| 242 | |
| 243 | //Spit out all edges we are going to add |
| 244 | unsigned min = GraphMap.size(); |
| 245 | if(newEdges.size() == 1) { |
| 246 | ((newEdges.begin())->first)->addOutEdge(((newEdges.begin())->second), |
| 247 | MSchedGraphEdge::BranchDep, |
| 248 | MSchedGraphEdge::NonDataDep, 1); |
| 249 | } |
| 250 | else { |
| 251 | |
| 252 | unsigned count = 0; |
| 253 | MSchedGraphNode *start; |
| 254 | MSchedGraphNode *end; |
| 255 | for(std::set<std::pair<MSchedGraphNode*, MSchedGraphNode*> >::iterator I = newEdges.begin(), E = newEdges.end(); I != E; ++I) { |
| 256 | |
| 257 | DEBUG(std::cerr << "Branch Edge from: " << *(I->first) << " to " << *(I->second) << "\n"); |
| 258 | |
| 259 | // if(I->second->getIndex() <= min) { |
| 260 | start = I->first; |
| 261 | end = I->second; |
| 262 | //min = I->second->getIndex(); |
| 263 | //} |
| 264 | start->addOutEdge(end, |
| 265 | MSchedGraphEdge::BranchDep, |
| 266 | MSchedGraphEdge::NonDataDep, 1); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | |
| 272 | //Add edges between the nodes |
| 273 | 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] | 274 | |
| 275 | //Get Machine target information for calculating latency |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 276 | const TargetInstrInfo *MTI = Target.getInstrInfo(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 277 | |
| 278 | std::vector<MSchedGraphNode*> memInstructions; |
| 279 | std::map<int, std::vector<OpIndexNodePair> > regNumtoNodeMap; |
| 280 | std::map<const Value*, std::vector<OpIndexNodePair> > valuetoNodeMap; |
| 281 | |
| 282 | //Save PHI instructions to deal with later |
| 283 | std::vector<const MachineInstr*> phiInstrs; |
Tanya Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 284 | unsigned index = 0; |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 285 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 286 | //Loop over instructions in MBB and add nodes and edges |
| 287 | for (MachineBasicBlock::const_iterator MI = BB->begin(), e = BB->end(); MI != e; ++MI) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 288 | |
| 289 | //Ignore indvar instructions |
| 290 | if(ignoreInstrs.count(MI)) { |
| 291 | ++index; |
| 292 | continue; |
| 293 | } |
| 294 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 295 | //Get each instruction of machine basic block, get the delay |
| 296 | //using the op code, create a new node for it, and add to the |
| 297 | //graph. |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 298 | |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 299 | MachineOpCode opCode = MI->getOpcode(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 300 | int delay; |
| 301 | |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 302 | #if 0 // FIXME: LOOK INTO THIS |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 303 | //Check if subsequent instructions can be issued before |
| 304 | //the result is ready, if so use min delay. |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 305 | if(MTI->hasResultInterlock(MIopCode)) |
| 306 | delay = MTI->minLatency(MIopCode); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 307 | else |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 308 | #endif |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 309 | //Get delay |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 310 | delay = MTI->maxLatency(opCode); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 311 | |
| 312 | //Create new node for this machine instruction and add to the graph. |
| 313 | //Create only if not a nop |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 314 | if(MTI->isNop(opCode)) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 315 | continue; |
| 316 | |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 317 | //Sparc BE does not use PHI opcode, so assert on this case |
| 318 | assert(opCode != TargetInstrInfo::PHI && "Did not expect PHI opcode"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 319 | |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 320 | bool isBranch = false; |
| 321 | |
| 322 | //We want to flag the branch node to treat it special |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 323 | if(MTI->isBranch(opCode)) |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 324 | isBranch = true; |
| 325 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 326 | //Node is created and added to the graph automatically |
Tanya Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 327 | MSchedGraphNode *node = new MSchedGraphNode(MI, this, index, delay, isBranch); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 328 | |
| 329 | DEBUG(std::cerr << "Created Node: " << *node << "\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 330 | |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 331 | //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] | 332 | if(MTI->isLoad(opCode) || MTI->isStore(opCode)) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 333 | memInstructions.push_back(node); |
| 334 | |
| 335 | //Loop over all operands, and put them into the register number to |
| 336 | //graph node map for determining dependencies |
| 337 | //If an operands is a use/def, we have an anti dependence to itself |
| 338 | for(unsigned i=0; i < MI->getNumOperands(); ++i) { |
| 339 | //Get Operand |
| 340 | const MachineOperand &mOp = MI->getOperand(i); |
| 341 | |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 342 | //Check if it has an allocated register |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 343 | if(mOp.hasAllocatedReg()) { |
| 344 | int regNum = mOp.getReg(); |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 345 | |
| 346 | if(regNum != SparcV9::g0) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 347 | //Put into our map |
| 348 | regNumtoNodeMap[regNum].push_back(std::make_pair(i, node)); |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 349 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 350 | continue; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | //Add virtual registers dependencies |
| 355 | //Check if any exist in the value map already and create dependencies |
| 356 | //between them. |
| 357 | if(mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) { |
| 358 | |
| 359 | //Make sure virtual register value is not null |
| 360 | assert((mOp.getVRegValue() != NULL) && "Null value is defined"); |
| 361 | |
| 362 | //Check if this is a read operation in a phi node, if so DO NOT PROCESS |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 363 | if(mOp.isUse() && (opCode == TargetInstrInfo::PHI)) { |
| 364 | DEBUG(std::cerr << "Read Operation in a PHI node\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 365 | continue; |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 366 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 367 | |
| 368 | if (const Value* srcI = mOp.getVRegValue()) { |
| 369 | |
| 370 | //Find value in the map |
| 371 | std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V |
| 372 | = valuetoNodeMap.find(srcI); |
| 373 | |
| 374 | //If there is something in the map already, add edges from |
| 375 | //those instructions |
| 376 | //to this one we are processing |
| 377 | if(V != valuetoNodeMap.end()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 378 | addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 379 | |
| 380 | //Add to value map |
| 381 | V->second.push_back(std::make_pair(i,node)); |
| 382 | } |
| 383 | //Otherwise put it in the map |
| 384 | else |
| 385 | //Put into value map |
| 386 | valuetoNodeMap[mOp.getVRegValue()].push_back(std::make_pair(i, node)); |
| 387 | } |
| 388 | } |
| 389 | } |
Tanya Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 390 | ++index; |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 391 | } |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 392 | |
| 393 | //Loop over LLVM BB, examine phi instructions, and add them to our phiInstr list to process |
| 394 | const BasicBlock *llvm_bb = BB->getBasicBlock(); |
| 395 | for(BasicBlock::const_iterator I = llvm_bb->begin(), E = llvm_bb->end(); I != E; ++I) { |
| 396 | if(const PHINode *PN = dyn_cast<PHINode>(I)) { |
| 397 | MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(PN); |
| 398 | for (unsigned j = 0; j < tempMvec.size(); j++) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 399 | if(!ignoreInstrs.count(tempMvec[j])) { |
| 400 | DEBUG(std::cerr << "Inserting phi instr into map: " << *tempMvec[j] << "\n"); |
| 401 | phiInstrs.push_back((MachineInstr*) tempMvec[j]); |
| 402 | } |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
| 406 | } |
| 407 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 408 | addMemEdges(memInstructions, AA, TD); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 409 | addMachRegEdges(regNumtoNodeMap); |
| 410 | |
| 411 | //Finally deal with PHI Nodes and Value* |
| 412 | for(std::vector<const MachineInstr*>::iterator I = phiInstrs.begin(), E = phiInstrs.end(); I != E; ++I) { |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 413 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 414 | //Get Node for this instruction |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 415 | std::map<const MachineInstr*, MSchedGraphNode*>::iterator X; |
| 416 | X = find(*I); |
| 417 | |
| 418 | if(X == GraphMap.end()) |
| 419 | continue; |
| 420 | |
| 421 | MSchedGraphNode *node = X->second; |
| 422 | |
| 423 | DEBUG(std::cerr << "Adding ite diff edges for node: " << *node << "\n"); |
| 424 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 425 | //Loop over operands for this instruction and add value edges |
| 426 | for(unsigned i=0; i < (*I)->getNumOperands(); ++i) { |
| 427 | //Get Operand |
| 428 | const MachineOperand &mOp = (*I)->getOperand(i); |
| 429 | if((mOp.getType() == MachineOperand::MO_VirtualRegister || mOp.getType() == MachineOperand::MO_CCRegister) && mOp.isUse()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 430 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 431 | //find the value in the map |
| 432 | if (const Value* srcI = mOp.getVRegValue()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 433 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 434 | //Find value in the map |
| 435 | std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 436 | = valuetoNodeMap.find(srcI); |
| 437 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 438 | //If there is something in the map already, add edges from |
| 439 | //those instructions |
| 440 | //to this one we are processing |
| 441 | if(V != valuetoNodeMap.end()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 442 | addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 449 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 450 | //Add dependencies for Value*s |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 451 | void MSchedGraph::addValueEdges(std::vector<OpIndexNodePair> &NodesInMap, |
| 452 | MSchedGraphNode *destNode, bool nodeIsUse, |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 453 | bool nodeIsDef, std::vector<const MachineInstr*> &phiInstrs, int diff) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 454 | |
| 455 | for(std::vector<OpIndexNodePair>::iterator I = NodesInMap.begin(), |
| 456 | E = NodesInMap.end(); I != E; ++I) { |
| 457 | |
| 458 | //Get node in vectors machine operand that is the same value as node |
| 459 | MSchedGraphNode *srcNode = I->second; |
| 460 | MachineOperand mOp = srcNode->getInst()->getOperand(I->first); |
| 461 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 462 | if(diff > 0) |
| 463 | if(std::find(phiInstrs.begin(), phiInstrs.end(), srcNode->getInst()) == phiInstrs.end()) |
| 464 | continue; |
| 465 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 466 | //Node is a Def, so add output dep. |
| 467 | if(nodeIsDef) { |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 468 | if(mOp.isUse()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 469 | 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] | 470 | srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep, |
| 471 | MSchedGraphEdge::AntiDep, diff); |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 472 | } |
| 473 | if(mOp.isDef()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 474 | 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] | 475 | srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep, |
| 476 | MSchedGraphEdge::OutputDep, diff); |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 477 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 478 | } |
| 479 | if(nodeIsUse) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 480 | if(mOp.isDef()) { |
| 481 | 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] | 482 | srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep, |
| 483 | MSchedGraphEdge::TrueDep, diff); |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 484 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 489 | //Add dependencies for machine registers across iterations |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 490 | void MSchedGraph::addMachRegEdges(std::map<int, std::vector<OpIndexNodePair> >& regNumtoNodeMap) { |
| 491 | //Loop over all machine registers in the map, and add dependencies |
| 492 | //between the instructions that use it |
| 493 | typedef std::map<int, std::vector<OpIndexNodePair> > regNodeMap; |
| 494 | for(regNodeMap::iterator I = regNumtoNodeMap.begin(); I != regNumtoNodeMap.end(); ++I) { |
| 495 | //Get the register number |
| 496 | int regNum = (*I).first; |
| 497 | |
| 498 | //Get Vector of nodes that use this register |
| 499 | std::vector<OpIndexNodePair> Nodes = (*I).second; |
| 500 | |
| 501 | //Loop over nodes and determine the dependence between the other |
| 502 | //nodes in the vector |
| 503 | for(unsigned i =0; i < Nodes.size(); ++i) { |
| 504 | |
| 505 | //Get src node operator index that uses this machine register |
| 506 | int srcOpIndex = Nodes[i].first; |
| 507 | |
| 508 | //Get the actual src Node |
| 509 | MSchedGraphNode *srcNode = Nodes[i].second; |
| 510 | |
| 511 | //Get Operand |
| 512 | const MachineOperand &srcMOp = srcNode->getInst()->getOperand(srcOpIndex); |
| 513 | |
| 514 | bool srcIsUseandDef = srcMOp.isDef() && srcMOp.isUse(); |
| 515 | bool srcIsUse = srcMOp.isUse() && !srcMOp.isDef(); |
| 516 | |
| 517 | |
| 518 | //Look at all instructions after this in execution order |
| 519 | for(unsigned j=i+1; j < Nodes.size(); ++j) { |
| 520 | |
| 521 | //Sink node is a write |
| 522 | if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) { |
| 523 | //Src only uses the register (read) |
| 524 | if(srcIsUse) |
| 525 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
| 526 | MSchedGraphEdge::AntiDep); |
| 527 | |
| 528 | else if(srcIsUseandDef) { |
| 529 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
| 530 | MSchedGraphEdge::AntiDep); |
| 531 | |
| 532 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
| 533 | MSchedGraphEdge::OutputDep); |
| 534 | } |
| 535 | else |
| 536 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
| 537 | MSchedGraphEdge::OutputDep); |
| 538 | } |
| 539 | //Dest node is a read |
| 540 | else { |
| 541 | if(!srcIsUse || srcIsUseandDef) |
| 542 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
| 543 | MSchedGraphEdge::TrueDep); |
| 544 | } |
| 545 | |
| 546 | } |
| 547 | |
| 548 | //Look at all the instructions before this one since machine registers |
| 549 | //could live across iterations. |
| 550 | for(unsigned j = 0; j < i; ++j) { |
| 551 | //Sink node is a write |
| 552 | if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) { |
| 553 | //Src only uses the register (read) |
| 554 | if(srcIsUse) |
| 555 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 556 | MSchedGraphEdge::AntiDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 557 | |
| 558 | else if(srcIsUseandDef) { |
| 559 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 560 | MSchedGraphEdge::AntiDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 561 | |
| 562 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 563 | MSchedGraphEdge::OutputDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 564 | } |
| 565 | else |
| 566 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 567 | MSchedGraphEdge::OutputDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 568 | } |
| 569 | //Dest node is a read |
| 570 | else { |
| 571 | if(!srcIsUse || srcIsUseandDef) |
| 572 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 573 | MSchedGraphEdge::TrueDep,1 ); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | |
| 577 | } |
| 578 | |
| 579 | } |
| 580 | |
| 581 | } |
| 582 | |
| 583 | } |
| 584 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 585 | //Add edges between all loads and stores |
| 586 | //Can be less strict with alias analysis and data dependence analysis. |
| 587 | void MSchedGraph::addMemEdges(const std::vector<MSchedGraphNode*>& memInst, AliasAnalysis &AA, TargetData &TD) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 588 | |
| 589 | //Get Target machine instruction info |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 590 | const TargetInstrInfo *TMI = Target.getInstrInfo(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 591 | |
| 592 | //Loop over all memory instructions in the vector |
| 593 | //Knowing that they are in execution, add true, anti, and output dependencies |
| 594 | for (unsigned srcIndex = 0; srcIndex < memInst.size(); ++srcIndex) { |
| 595 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 596 | MachineInstr *srcInst = (MachineInstr*) memInst[srcIndex]->getInst(); |
| 597 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 598 | //Get the machine opCode to determine type of memory instruction |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 599 | MachineOpCode srcNodeOpCode = srcInst->getOpcode(); |
| 600 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 601 | |
| 602 | //All instructions after this one in execution order have an iteration delay of 0 |
| 603 | for(unsigned destIndex = srcIndex + 1; destIndex < memInst.size(); ++destIndex) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 604 | |
| 605 | MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst(); |
| 606 | |
| 607 | //Add Anti dependencies (store after load) |
| 608 | //Source is a Load |
| 609 | if(TMI->isLoad(srcNodeOpCode)) { |
| 610 | |
| 611 | //Destination is a store |
| 612 | if(TMI->isStore(destInst->getOpcode())) { |
| 613 | |
| 614 | //Get the Value* that we are reading from the load, always the first op |
| 615 | const MachineOperand &mOp = srcInst->getOperand(0); |
| 616 | assert((mOp.isUse() && (mOp.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n"); |
| 617 | |
| 618 | //Get the value* for the store |
| 619 | const MachineOperand &mOp2 = destInst->getOperand(0); |
| 620 | assert(mOp2.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a value*\n"); |
| 621 | |
| 622 | //Only add the edge if we can't verify that they do not alias |
| 623 | if(AA.alias(mOp2.getVRegValue(), |
| 624 | (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()), |
| 625 | mOp.getVRegValue(), |
| 626 | (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType())) |
| 627 | != AliasAnalysis::NoAlias) { |
| 628 | |
| 629 | //Add edge from load to store |
| 630 | memInst[srcIndex]->addOutEdge(memInst[destIndex], |
| 631 | MSchedGraphEdge::MemoryDep, |
| 632 | MSchedGraphEdge::AntiDep); |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 637 | //If source is a store, add output and true dependencies |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 638 | if(TMI->isStore(srcNodeOpCode)) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 639 | |
| 640 | //Get the Value* that we are reading from the store (src), always the first op |
| 641 | const MachineOperand &mOp = srcInst->getOperand(0); |
| 642 | assert(mOp.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a use and a value*\n"); |
| 643 | |
| 644 | //Get the Value* that we are reading from the load, always the first op |
| 645 | const MachineOperand &mOp2 = srcInst->getOperand(0); |
| 646 | assert((mOp2.isUse() && (mOp2.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n"); |
| 647 | |
| 648 | //Only add the edge if we can't verify that they do not alias |
| 649 | if(AA.alias(mOp2.getVRegValue(), |
| 650 | (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()), |
| 651 | mOp.getVRegValue(), |
| 652 | (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType())) |
| 653 | != AliasAnalysis::NoAlias) { |
| 654 | |
| 655 | if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode())) |
| 656 | memInst[srcIndex]->addOutEdge(memInst[destIndex], |
| 657 | MSchedGraphEdge::MemoryDep, |
| 658 | MSchedGraphEdge::OutputDep); |
| 659 | else |
| 660 | memInst[srcIndex]->addOutEdge(memInst[destIndex], |
| 661 | MSchedGraphEdge::MemoryDep, |
| 662 | MSchedGraphEdge::TrueDep); |
| 663 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | //All instructions before the src in execution order have an iteration delay of 1 |
| 668 | for(unsigned destIndex = 0; destIndex < srcIndex; ++destIndex) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 669 | |
| 670 | MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst(); |
| 671 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 672 | //source is a Load, so add anti-dependencies (store after load) |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 673 | if(TMI->isLoad(srcNodeOpCode)) { |
| 674 | //Get the Value* that we are reading from the load, always the first op |
| 675 | const MachineOperand &mOp = srcInst->getOperand(0); |
| 676 | assert((mOp.isUse() && (mOp.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 677 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 678 | //Get the value* for the store |
| 679 | const MachineOperand &mOp2 = destInst->getOperand(0); |
| 680 | assert(mOp2.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a value*\n"); |
| 681 | |
| 682 | //Only add the edge if we can't verify that they do not alias |
| 683 | if(AA.alias(mOp2.getVRegValue(), |
| 684 | (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()), |
| 685 | mOp.getVRegValue(), |
| 686 | (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType())) |
| 687 | != AliasAnalysis::NoAlias) { |
| 688 | if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode())) |
| 689 | memInst[srcIndex]->addOutEdge(memInst[destIndex], |
| 690 | MSchedGraphEdge::MemoryDep, |
| 691 | MSchedGraphEdge::AntiDep, 1); |
| 692 | } |
| 693 | } |
| 694 | if(TMI->isStore(srcNodeOpCode)) { |
| 695 | |
| 696 | //Get the Value* that we are reading from the store (src), always the first op |
| 697 | const MachineOperand &mOp = srcInst->getOperand(0); |
| 698 | assert(mOp.getType() == MachineOperand::MO_VirtualRegister && "Assumed first operand was a use and a value*\n"); |
| 699 | |
| 700 | //Get the Value* that we are reading from the load, always the first op |
| 701 | const MachineOperand &mOp2 = srcInst->getOperand(0); |
| 702 | assert((mOp2.isUse() && (mOp2.getType() == MachineOperand::MO_VirtualRegister)) && "Assumed first operand was a use and a value*\n"); |
| 703 | |
| 704 | //Only add the edge if we can't verify that they do not alias |
| 705 | if(AA.alias(mOp2.getVRegValue(), |
| 706 | (unsigned)TD.getTypeSize(mOp2.getVRegValue()->getType()), |
| 707 | mOp.getVRegValue(), |
| 708 | (unsigned)TD.getTypeSize(mOp.getVRegValue()->getType())) |
| 709 | != AliasAnalysis::NoAlias) { |
| 710 | |
| 711 | if(TMI->isStore(memInst[destIndex]->getInst()->getOpcode())) |
| 712 | memInst[srcIndex]->addOutEdge(memInst[destIndex], |
| 713 | MSchedGraphEdge::MemoryDep, |
| 714 | MSchedGraphEdge::OutputDep, 1); |
| 715 | else |
| 716 | memInst[srcIndex]->addOutEdge(memInst[destIndex], |
| 717 | MSchedGraphEdge::MemoryDep, |
| 718 | MSchedGraphEdge::TrueDep, 1); |
| 719 | } |
| 720 | } |
| 721 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | } |
| 725 | } |