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. |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 144 | MSchedGraph::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 Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 148 | : BB(bb), Target(targ) { |
| 149 | |
| 150 | //Make sure BB is not null, |
| 151 | assert(BB != NULL && "Basic Block is null"); |
| 152 | |
Tanya Lattner | 420025b | 2004-10-10 22:44:35 +0000 | [diff] [blame] | 153 | //DEBUG(std::cerr << "Constructing graph for " << bb << "\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 154 | |
| 155 | //Create nodes and edges for this BB |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 156 | buildNodesAndEdges(AA, TD, ignoreInstrs, DA, machineTollvm); |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 157 | |
| 158 | //Experimental! |
| 159 | //addBranchEdges(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 162 | //Copies the graph and keeps a map from old to new nodes |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 163 | MSchedGraph::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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 197 | //Deconstructor, deletes all nodes in the graph |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 198 | MSchedGraph::~MSchedGraph () { |
| 199 | for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end(); I != E; ++I) |
| 200 | delete I->second; |
| 201 | } |
| 202 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 203 | |
| 204 | //Experimental code to add edges from the branch to all nodes dependent upon it. |
| 205 | void 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. |
| 228 | void 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 Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 276 | void MSchedGraph::buildNodesAndEdges(AliasAnalysis &AA, TargetData &TD, |
| 277 | std::map<const MachineInstr*, unsigned> &ignoreInstrs, |
| 278 | DependenceAnalyzer &DA, |
| 279 | std::map<MachineInstr*, Instruction*> &machineTollvm) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 280 | |
| 281 | //Get Machine target information for calculating latency |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 282 | const TargetInstrInfo *MTI = Target.getInstrInfo(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 283 | |
| 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 Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 290 | unsigned index = 0; |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 291 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 292 | //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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 294 | |
| 295 | //Ignore indvar instructions |
| 296 | if(ignoreInstrs.count(MI)) { |
| 297 | ++index; |
| 298 | continue; |
| 299 | } |
| 300 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 301 | //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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 304 | |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 305 | MachineOpCode opCode = MI->getOpcode(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 306 | int delay; |
| 307 | |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 308 | #if 0 // FIXME: LOOK INTO THIS |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 309 | //Check if subsequent instructions can be issued before |
| 310 | //the result is ready, if so use min delay. |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 311 | if(MTI->hasResultInterlock(MIopCode)) |
| 312 | delay = MTI->minLatency(MIopCode); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 313 | else |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 314 | #endif |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 315 | //Get delay |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 316 | delay = MTI->maxLatency(opCode); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 317 | |
| 318 | //Create new node for this machine instruction and add to the graph. |
| 319 | //Create only if not a nop |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 320 | if(MTI->isNop(opCode)) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 321 | continue; |
| 322 | |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 323 | //Sparc BE does not use PHI opcode, so assert on this case |
| 324 | assert(opCode != TargetInstrInfo::PHI && "Did not expect PHI opcode"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 325 | |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 326 | bool isBranch = false; |
| 327 | |
| 328 | //We want to flag the branch node to treat it special |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 329 | if(MTI->isBranch(opCode)) |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 330 | isBranch = true; |
| 331 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 332 | //Node is created and added to the graph automatically |
Tanya Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 333 | MSchedGraphNode *node = new MSchedGraphNode(MI, this, index, delay, isBranch); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 334 | |
| 335 | DEBUG(std::cerr << "Created Node: " << *node << "\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 336 | |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 337 | //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] | 338 | if(MTI->isLoad(opCode) || MTI->isStore(opCode)) |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 339 | 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 Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 348 | //Check if it has an allocated register |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 349 | if(mOp.hasAllocatedReg()) { |
| 350 | int regNum = mOp.getReg(); |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 351 | |
| 352 | if(regNum != SparcV9::g0) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 353 | //Put into our map |
| 354 | regNumtoNodeMap[regNum].push_back(std::make_pair(i, node)); |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 355 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 356 | 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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 369 | if(mOp.isUse() && (opCode == TargetInstrInfo::PHI)) { |
| 370 | DEBUG(std::cerr << "Read Operation in a PHI node\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 371 | continue; |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 372 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 373 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 384 | addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 385 | |
| 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 Lattner | 28e5eab | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 396 | ++index; |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 397 | } |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 398 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 405 | 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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | } |
| 413 | |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 414 | addMemEdges(memInstructions, AA, TD, DA, machineTollvm); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 415 | 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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 419 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 420 | //Get Node for this instruction |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 421 | 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 Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 431 | //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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 436 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 437 | //find the value in the map |
| 438 | if (const Value* srcI = mOp.getVRegValue()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 439 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 440 | //Find value in the map |
| 441 | std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 442 | = valuetoNodeMap.find(srcI); |
| 443 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 444 | //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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 448 | addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 455 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 456 | //Add dependencies for Value*s |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 457 | void MSchedGraph::addValueEdges(std::vector<OpIndexNodePair> &NodesInMap, |
| 458 | MSchedGraphNode *destNode, bool nodeIsUse, |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 459 | bool nodeIsDef, std::vector<const MachineInstr*> &phiInstrs, int diff) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 460 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 468 | if(diff > 0) |
| 469 | if(std::find(phiInstrs.begin(), phiInstrs.end(), srcNode->getInst()) == phiInstrs.end()) |
| 470 | continue; |
| 471 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 472 | //Node is a Def, so add output dep. |
| 473 | if(nodeIsDef) { |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 474 | if(mOp.isUse()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 475 | 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] | 476 | srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep, |
| 477 | MSchedGraphEdge::AntiDep, diff); |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 478 | } |
| 479 | if(mOp.isDef()) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 480 | 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] | 481 | srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep, |
| 482 | MSchedGraphEdge::OutputDep, diff); |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 483 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 484 | } |
| 485 | if(nodeIsUse) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 486 | if(mOp.isDef()) { |
| 487 | 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] | 488 | srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep, |
| 489 | MSchedGraphEdge::TrueDep, diff); |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 490 | } |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 495 | //Add dependencies for machine registers across iterations |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 496 | void 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 Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 562 | MSchedGraphEdge::AntiDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 563 | |
| 564 | else if(srcIsUseandDef) { |
| 565 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 566 | MSchedGraphEdge::AntiDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 567 | |
| 568 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 569 | MSchedGraphEdge::OutputDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 570 | } |
| 571 | else |
| 572 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 573 | MSchedGraphEdge::OutputDep, 1); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 574 | } |
| 575 | //Dest node is a read |
| 576 | else { |
| 577 | if(!srcIsUse || srcIsUseandDef) |
| 578 | srcNode->addOutEdge(Nodes[j].second, MSchedGraphEdge::MachineRegister, |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 579 | MSchedGraphEdge::TrueDep,1 ); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
| 583 | } |
| 584 | |
| 585 | } |
| 586 | |
| 587 | } |
| 588 | |
| 589 | } |
| 590 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 591 | //Add edges between all loads and stores |
| 592 | //Can be less strict with alias analysis and data dependence analysis. |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 593 | void MSchedGraph::addMemEdges(const std::vector<MSchedGraphNode*>& memInst, AliasAnalysis &AA, |
| 594 | TargetData &TD, DependenceAnalyzer &DA, |
| 595 | std::map<MachineInstr*, Instruction*> &machineTollvm) { |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 596 | |
| 597 | //Get Target machine instruction info |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 598 | const TargetInstrInfo *TMI = Target.getInstrInfo(); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 599 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 604 | MachineInstr *srcInst = (MachineInstr*) memInst[srcIndex]->getInst(); |
| 605 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 606 | //Get the machine opCode to determine type of memory instruction |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 607 | MachineOpCode srcNodeOpCode = srcInst->getOpcode(); |
| 608 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 609 | //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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 611 | |
| 612 | MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst(); |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 613 | 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 619 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 629 | const MachineOperand &mOp2 = destInst->getOperand(0); |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 630 | |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 641 | |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 642 | //compare to DA |
| 643 | DependenceResult dr = DA.getDependenceInfo(machineTollvm[srcInst], machineTollvm[destInst]); |
| 644 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 645 | //Only add the edge if we can't verify that they do not alias |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 646 | if(malias || AA.alias(mOp2.getVRegValue(), |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 647 | (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 Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 656 | |
| 657 | assert(dr.dependences.size() == 1 && "Expected at least one dependence\n"); |
| 658 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 659 | } |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 660 | else |
| 661 | assert(dr.dependences.size() == 0 && "Expected no dependence\n"); |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 665 | //If source is a store, add output and true dependencies |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 666 | if(TMI->isStore(srcNodeOpCode)) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 667 | |
| 668 | //Get the Value* that we are reading from the load, always the first op |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 669 | 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 685 | |
| 686 | //Only add the edge if we can't verify that they do not alias |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 687 | if(malias || AA.alias(mOp2.getVRegValue(), |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 688 | (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 Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 701 | assert(dr.dependences.size() == 1 && "Expected at least one dependence\n"); |
| 702 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 703 | } |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 704 | |
| 705 | else |
| 706 | assert(dr.dependences.size() == 0 && "Expected no dependence\n"); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 707 | } |
| 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 712 | |
| 713 | MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst(); |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 714 | bool malias = false; |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 715 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 716 | //source is a Load, so add anti-dependencies (store after load) |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 717 | if(TMI->isLoad(srcNodeOpCode)) { |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 718 | |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 719 | //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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 745 | } |
| 746 | if(TMI->isStore(srcNodeOpCode)) { |
| 747 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 748 | //Get the Value* that we are reading from the load, always the first op |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 749 | const MachineOperand &mOp = srcInst->getOperand(0); |
| 750 | const MachineOperand &mOp2 = destInst->getOperand(0); |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 751 | |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame^] | 752 | 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 763 | //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 Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | } |
| 784 | } |