Chris Lattner | c5f0afa | 2003-11-12 00:40:34 +0000 | [diff] [blame] | 1 | //===- MemoryDepAnalysis.cpp - Compute dep graph for memory ops -----------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +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 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements a pass (MemoryDepAnalysis) that computes memory-based |
| 11 | // data dependences between instructions for each function in a module. |
| 12 | // Memory-based dependences occur due to load and store operations, but |
| 13 | // also the side-effects of call instructions. |
| 14 | // |
| 15 | // The result of this pass is a DependenceGraph for each function |
| 16 | // representing the memory-based data dependences between instructions. |
Chris Lattner | c5f0afa | 2003-11-12 00:40:34 +0000 | [diff] [blame] | 17 | // |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Chris Lattner | 0ecdcbe | 2004-06-28 00:27:16 +0000 | [diff] [blame] | 20 | #include "MemoryDepAnalysis.h" |
Chris Lattner | e6afb74 | 2004-06-28 00:41:23 +0000 | [diff] [blame] | 21 | #include "IPModRef.h" |
Misha Brukman | d8e1eea | 2004-07-29 17:05:13 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
| 23 | #include "llvm/Module.h" |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
| 25 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 26 | #include "llvm/Support/InstVisitor.h" |
| 27 | #include "llvm/Support/CFG.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SCCIterator.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include "llvm/ADT/STLExtras.h" |
| 31 | #include "llvm/ADT/hash_map" |
| 32 | #include "llvm/ADT/hash_set" |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 33 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | namespace llvm { |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 35 | |
| 36 | ///-------------------------------------------------------------------------- |
| 37 | /// struct ModRefTable: |
| 38 | /// |
| 39 | /// A data structure that tracks ModRefInfo for instructions: |
| 40 | /// -- modRefMap is a map of Instruction* -> ModRefInfo for the instr. |
| 41 | /// -- definers is a vector of instructions that define any node |
| 42 | /// -- users is a vector of instructions that reference any node |
| 43 | /// -- numUsersBeforeDef is a vector indicating that the number of users |
| 44 | /// seen before definers[i] is numUsersBeforeDef[i]. |
| 45 | /// |
| 46 | /// numUsersBeforeDef[] effectively tells us the exact interleaving of |
| 47 | /// definers and users within the ModRefTable. |
| 48 | /// This is only maintained when constructing the table for one SCC, and |
| 49 | /// not copied over from one table to another since it is no longer useful. |
| 50 | ///-------------------------------------------------------------------------- |
| 51 | |
Jeff Cohen | 2611dd4 | 2005-01-09 04:18:28 +0000 | [diff] [blame] | 52 | class ModRefTable { |
| 53 | public: |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 54 | typedef hash_map<Instruction*, ModRefInfo> ModRefMap; |
| 55 | typedef ModRefMap::const_iterator const_map_iterator; |
Misha Brukman | 56c0fa6 | 2004-03-12 00:58:41 +0000 | [diff] [blame] | 56 | typedef ModRefMap:: iterator map_iterator; |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 57 | typedef std::vector<Instruction*>::const_iterator const_ref_iterator; |
| 58 | typedef std::vector<Instruction*>:: iterator ref_iterator; |
| 59 | |
| 60 | ModRefMap modRefMap; |
| 61 | std::vector<Instruction*> definers; |
| 62 | std::vector<Instruction*> users; |
| 63 | std::vector<unsigned> numUsersBeforeDef; |
| 64 | |
| 65 | // Iterators to enumerate all the defining instructions |
| 66 | const_ref_iterator defsBegin() const { return definers.begin(); } |
| 67 | ref_iterator defsBegin() { return definers.begin(); } |
| 68 | const_ref_iterator defsEnd() const { return definers.end(); } |
| 69 | ref_iterator defsEnd() { return definers.end(); } |
| 70 | |
| 71 | // Iterators to enumerate all the user instructions |
| 72 | const_ref_iterator usersBegin() const { return users.begin(); } |
| 73 | ref_iterator usersBegin() { return users.begin(); } |
| 74 | const_ref_iterator usersEnd() const { return users.end(); } |
| 75 | ref_iterator usersEnd() { return users.end(); } |
| 76 | |
| 77 | // Iterator identifying the last user that was seen *before* a |
| 78 | // specified def. In particular, all users in the half-closed range |
| 79 | // [ usersBegin(), usersBeforeDef_End(defPtr) ) |
| 80 | // were seen *before* the specified def. All users in the half-closed range |
| 81 | // [ usersBeforeDef_End(defPtr), usersEnd() ) |
| 82 | // were seen *after* the specified def. |
| 83 | // |
| 84 | ref_iterator usersBeforeDef_End(const_ref_iterator defPtr) { |
| 85 | unsigned defIndex = (unsigned) (defPtr - defsBegin()); |
| 86 | assert(defIndex < numUsersBeforeDef.size()); |
| 87 | assert(usersBegin() + numUsersBeforeDef[defIndex] <= usersEnd()); |
| 88 | return usersBegin() + numUsersBeforeDef[defIndex]; |
| 89 | } |
| 90 | const_ref_iterator usersBeforeDef_End(const_ref_iterator defPtr) const { |
| 91 | return const_cast<ModRefTable*>(this)->usersBeforeDef_End(defPtr); |
| 92 | } |
| 93 | |
| 94 | // |
| 95 | // Modifier methods |
| 96 | // |
| 97 | void AddDef(Instruction* D) { |
| 98 | definers.push_back(D); |
| 99 | numUsersBeforeDef.push_back(users.size()); |
| 100 | } |
| 101 | void AddUse(Instruction* U) { |
| 102 | users.push_back(U); |
| 103 | } |
| 104 | void Insert(const ModRefTable& fromTable) { |
| 105 | modRefMap.insert(fromTable.modRefMap.begin(), fromTable.modRefMap.end()); |
| 106 | definers.insert(definers.end(), |
| 107 | fromTable.definers.begin(), fromTable.definers.end()); |
| 108 | users.insert(users.end(), |
| 109 | fromTable.users.begin(), fromTable.users.end()); |
| 110 | numUsersBeforeDef.clear(); /* fromTable.numUsersBeforeDef is ignored */ |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | ///-------------------------------------------------------------------------- |
| 116 | /// class ModRefInfoBuilder: |
| 117 | /// |
| 118 | /// A simple InstVisitor<> class that retrieves the Mod/Ref info for |
| 119 | /// Load/Store/Call instructions and inserts this information in |
| 120 | /// a ModRefTable. It also records all instructions that Mod any node |
| 121 | /// and all that use any node. |
| 122 | ///-------------------------------------------------------------------------- |
| 123 | |
Chris Lattner | 8043127 | 2003-08-06 17:16:24 +0000 | [diff] [blame] | 124 | class ModRefInfoBuilder : public InstVisitor<ModRefInfoBuilder> { |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 125 | const DSGraph& funcGraph; |
| 126 | const FunctionModRefInfo& funcModRef; |
Chris Lattner | 507bdf9 | 2005-01-12 04:51:37 +0000 | [diff] [blame] | 127 | class ModRefTable& modRefTable; |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 8043127 | 2003-08-06 17:16:24 +0000 | [diff] [blame] | 129 | ModRefInfoBuilder(); // DO NOT IMPLEMENT |
| 130 | ModRefInfoBuilder(const ModRefInfoBuilder&); // DO NOT IMPLEMENT |
| 131 | void operator=(const ModRefInfoBuilder&); // DO NOT IMPLEMENT |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 132 | |
| 133 | public: |
Misha Brukman | 56c0fa6 | 2004-03-12 00:58:41 +0000 | [diff] [blame] | 134 | ModRefInfoBuilder(const DSGraph& _funcGraph, |
| 135 | const FunctionModRefInfo& _funcModRef, |
| 136 | ModRefTable& _modRefTable) |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 137 | : funcGraph(_funcGraph), funcModRef(_funcModRef), modRefTable(_modRefTable) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | // At a call instruction, retrieve the ModRefInfo using IPModRef results. |
| 142 | // Add the call to the defs list if it modifies any nodes and to the uses |
| 143 | // list if it refs any nodes. |
| 144 | // |
Misha Brukman | 56c0fa6 | 2004-03-12 00:58:41 +0000 | [diff] [blame] | 145 | void visitCallInst(CallInst& callInst) { |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 146 | ModRefInfo safeModRef(funcGraph.getGraphSize()); |
| 147 | const ModRefInfo* callModRef = funcModRef.getModRefInfo(callInst); |
Misha Brukman | 56c0fa6 | 2004-03-12 00:58:41 +0000 | [diff] [blame] | 148 | if (callModRef == NULL) { |
| 149 | // call to external/unknown function: mark all nodes as Mod and Ref |
| 150 | safeModRef.getModSet().set(); |
| 151 | safeModRef.getRefSet().set(); |
| 152 | callModRef = &safeModRef; |
| 153 | } |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 154 | |
| 155 | modRefTable.modRefMap.insert(std::make_pair(&callInst, |
| 156 | ModRefInfo(*callModRef))); |
| 157 | if (callModRef->getModSet().any()) |
| 158 | modRefTable.AddDef(&callInst); |
| 159 | if (callModRef->getRefSet().any()) |
| 160 | modRefTable.AddUse(&callInst); |
| 161 | } |
| 162 | |
| 163 | // At a store instruction, add to the mod set the single node pointed to |
| 164 | // by the pointer argument of the store. Interestingly, if there is no |
| 165 | // such node, that would be a null pointer reference! |
Misha Brukman | 56c0fa6 | 2004-03-12 00:58:41 +0000 | [diff] [blame] | 166 | void visitStoreInst(StoreInst& storeInst) { |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 167 | const DSNodeHandle& ptrNode = |
| 168 | funcGraph.getNodeForValue(storeInst.getPointerOperand()); |
Misha Brukman | 56c0fa6 | 2004-03-12 00:58:41 +0000 | [diff] [blame] | 169 | if (const DSNode* target = ptrNode.getNode()) { |
| 170 | unsigned nodeId = funcModRef.getNodeId(target); |
| 171 | ModRefInfo& minfo = |
| 172 | modRefTable.modRefMap.insert( |
| 173 | std::make_pair(&storeInst, |
| 174 | ModRefInfo(funcGraph.getGraphSize()))).first->second; |
| 175 | minfo.setNodeIsMod(nodeId); |
| 176 | modRefTable.AddDef(&storeInst); |
| 177 | } else |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 178 | std::cerr << "Warning: Uninitialized pointer reference!\n"; |
| 179 | } |
| 180 | |
| 181 | // At a load instruction, add to the ref set the single node pointed to |
| 182 | // by the pointer argument of the load. Interestingly, if there is no |
| 183 | // such node, that would be a null pointer reference! |
Misha Brukman | 56c0fa6 | 2004-03-12 00:58:41 +0000 | [diff] [blame] | 184 | void visitLoadInst(LoadInst& loadInst) { |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 185 | const DSNodeHandle& ptrNode = |
| 186 | funcGraph.getNodeForValue(loadInst.getPointerOperand()); |
Misha Brukman | 56c0fa6 | 2004-03-12 00:58:41 +0000 | [diff] [blame] | 187 | if (const DSNode* target = ptrNode.getNode()) { |
| 188 | unsigned nodeId = funcModRef.getNodeId(target); |
| 189 | ModRefInfo& minfo = |
| 190 | modRefTable.modRefMap.insert( |
| 191 | std::make_pair(&loadInst, |
| 192 | ModRefInfo(funcGraph.getGraphSize()))).first->second; |
| 193 | minfo.setNodeIsRef(nodeId); |
| 194 | modRefTable.AddUse(&loadInst); |
| 195 | } else |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 196 | std::cerr << "Warning: Uninitialized pointer reference!\n"; |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | |
| 201 | //---------------------------------------------------------------------------- |
| 202 | // class MemoryDepAnalysis: A dep. graph for load/store/call instructions |
| 203 | //---------------------------------------------------------------------------- |
| 204 | |
Chris Lattner | 95008bc | 2003-08-31 19:40:57 +0000 | [diff] [blame] | 205 | |
| 206 | /// getAnalysisUsage - This does not modify anything. It uses the Top-Down DS |
| 207 | /// Graph and IPModRef. |
| 208 | /// |
| 209 | void MemoryDepAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 210 | AU.setPreservesAll(); |
| 211 | AU.addRequired<TDDataStructures>(); |
| 212 | AU.addRequired<IPModRef>(); |
| 213 | } |
| 214 | |
| 215 | |
Chris Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 216 | /// Basic dependence gathering algorithm, using scc_iterator on CFG: |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 217 | /// |
| 218 | /// for every SCC S in the CFG in PostOrder on the SCC DAG |
| 219 | /// { |
| 220 | /// for every basic block BB in S in *postorder* |
| 221 | /// for every instruction I in BB in reverse |
| 222 | /// Add (I, ModRef[I]) to ModRefCurrent |
| 223 | /// if (Mod[I] != NULL) |
| 224 | /// Add I to DefSetCurrent: { I \in S : Mod[I] != NULL } |
| 225 | /// if (Ref[I] != NULL) |
| 226 | /// Add I to UseSetCurrent: { I : Ref[I] != NULL } |
| 227 | /// |
| 228 | /// for every def D in DefSetCurrent |
| 229 | /// |
| 230 | /// // NOTE: D comes after itself iff S contains a loop |
| 231 | /// if (HasLoop(S) && D & D) |
| 232 | /// Add output-dep: D -> D2 |
| 233 | /// |
| 234 | /// for every def D2 *after* D in DefSetCurrent |
| 235 | /// // NOTE: D2 comes before D in execution order |
| 236 | /// if (D & D2) |
| 237 | /// Add output-dep: D2 -> D |
| 238 | /// if (HasLoop(S)) |
| 239 | /// Add output-dep: D -> D2 |
| 240 | /// |
| 241 | /// for every use U in UseSetCurrent that was seen *before* D |
| 242 | /// // NOTE: U comes after D in execution order |
| 243 | /// if (U & D) |
| 244 | /// if (U != D || HasLoop(S)) |
| 245 | /// Add true-dep: D -> U |
| 246 | /// if (HasLoop(S)) |
| 247 | /// Add anti-dep: U -> D |
| 248 | /// |
| 249 | /// for every use U in UseSetCurrent that was seen *after* D |
| 250 | /// // NOTE: U comes before D in execution order |
| 251 | /// if (U & D) |
| 252 | /// if (U != D || HasLoop(S)) |
| 253 | /// Add anti-dep: U -> D |
| 254 | /// if (HasLoop(S)) |
| 255 | /// Add true-dep: D -> U |
| 256 | /// |
| 257 | /// for every def Dnext in DefSetAfter |
| 258 | /// // NOTE: Dnext comes after D in execution order |
| 259 | /// if (Dnext & D) |
| 260 | /// Add output-dep: D -> Dnext |
| 261 | /// |
| 262 | /// for every use Unext in UseSetAfter |
| 263 | /// // NOTE: Unext comes after D in execution order |
| 264 | /// if (Unext & D) |
| 265 | /// Add true-dep: D -> Unext |
| 266 | /// |
| 267 | /// for every use U in UseSetCurrent |
| 268 | /// for every def Dnext in DefSetAfter |
| 269 | /// // NOTE: Dnext comes after U in execution order |
| 270 | /// if (Dnext & D) |
| 271 | /// Add anti-dep: U -> Dnext |
| 272 | /// |
| 273 | /// Add ModRefCurrent to ModRefAfter: { (I, ModRef[I] ) } |
| 274 | /// Add DefSetCurrent to DefSetAfter: { I : Mod[I] != NULL } |
| 275 | /// Add UseSetCurrent to UseSetAfter: { I : Ref[I] != NULL } |
| 276 | /// } |
| 277 | /// |
| 278 | /// |
Chris Lattner | fe8d880 | 2003-08-31 19:46:48 +0000 | [diff] [blame] | 279 | void MemoryDepAnalysis::ProcessSCC(std::vector<BasicBlock*> &S, |
| 280 | ModRefTable& ModRefAfter, bool hasLoop) { |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 281 | ModRefTable ModRefCurrent; |
| 282 | ModRefTable::ModRefMap& mapCurrent = ModRefCurrent.modRefMap; |
| 283 | ModRefTable::ModRefMap& mapAfter = ModRefAfter.modRefMap; |
| 284 | |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 285 | // Builder class fills out a ModRefTable one instruction at a time. |
| 286 | // To use it, we just invoke it's visit function for each basic block: |
| 287 | // |
| 288 | // for each basic block BB in the SCC in *postorder* |
| 289 | // for each instruction I in BB in *reverse* |
| 290 | // ModRefInfoBuilder::visit(I) |
| 291 | // : Add (I, ModRef[I]) to ModRefCurrent.modRefMap |
| 292 | // : Add I to ModRefCurrent.definers if it defines any node |
| 293 | // : Add I to ModRefCurrent.users if it uses any node |
| 294 | // |
| 295 | ModRefInfoBuilder builder(*funcGraph, *funcModRef, ModRefCurrent); |
Chris Lattner | fe8d880 | 2003-08-31 19:46:48 +0000 | [diff] [blame] | 296 | for (std::vector<BasicBlock*>::iterator BI = S.begin(), BE = S.end(); |
| 297 | BI != BE; ++BI) |
Chris Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 298 | // Note: BBs in the SCC<> created by scc_iterator are in postorder. |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 299 | for (BasicBlock::reverse_iterator II=(*BI)->rbegin(), IE=(*BI)->rend(); |
| 300 | II != IE; ++II) |
| 301 | builder.visit(*II); |
| 302 | |
| 303 | /// for every def D in DefSetCurrent |
| 304 | /// |
| 305 | for (ModRefTable::ref_iterator II=ModRefCurrent.defsBegin(), |
| 306 | IE=ModRefCurrent.defsEnd(); II != IE; ++II) |
| 307 | { |
| 308 | /// // NOTE: D comes after itself iff S contains a loop |
| 309 | /// if (HasLoop(S)) |
| 310 | /// Add output-dep: D -> D2 |
| 311 | if (hasLoop) |
| 312 | funcDepGraph->AddSimpleDependence(**II, **II, OutputDependence); |
| 313 | |
| 314 | /// for every def D2 *after* D in DefSetCurrent |
| 315 | /// // NOTE: D2 comes before D in execution order |
| 316 | /// if (D2 & D) |
| 317 | /// Add output-dep: D2 -> D |
| 318 | /// if (HasLoop(S)) |
| 319 | /// Add output-dep: D -> D2 |
| 320 | for (ModRefTable::ref_iterator JI=II+1; JI != IE; ++JI) |
| 321 | if (!Disjoint(mapCurrent.find(*II)->second.getModSet(), |
| 322 | mapCurrent.find(*JI)->second.getModSet())) |
| 323 | { |
| 324 | funcDepGraph->AddSimpleDependence(**JI, **II, OutputDependence); |
| 325 | if (hasLoop) |
| 326 | funcDepGraph->AddSimpleDependence(**II, **JI, OutputDependence); |
| 327 | } |
| 328 | |
| 329 | /// for every use U in UseSetCurrent that was seen *before* D |
| 330 | /// // NOTE: U comes after D in execution order |
| 331 | /// if (U & D) |
| 332 | /// if (U != D || HasLoop(S)) |
| 333 | /// Add true-dep: U -> D |
| 334 | /// if (HasLoop(S)) |
| 335 | /// Add anti-dep: D -> U |
Chris Lattner | 507bdf9 | 2005-01-12 04:51:37 +0000 | [diff] [blame] | 336 | { |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 337 | ModRefTable::ref_iterator JI=ModRefCurrent.usersBegin(); |
| 338 | ModRefTable::ref_iterator JE = ModRefCurrent.usersBeforeDef_End(II); |
| 339 | for ( ; JI != JE; ++JI) |
| 340 | if (!Disjoint(mapCurrent.find(*II)->second.getModSet(), |
| 341 | mapCurrent.find(*JI)->second.getRefSet())) |
| 342 | { |
| 343 | if (*II != *JI || hasLoop) |
| 344 | funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence); |
| 345 | if (hasLoop) |
| 346 | funcDepGraph->AddSimpleDependence(**JI, **II, AntiDependence); |
| 347 | } |
Chris Lattner | 507bdf9 | 2005-01-12 04:51:37 +0000 | [diff] [blame] | 348 | |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 349 | /// for every use U in UseSetCurrent that was seen *after* D |
| 350 | /// // NOTE: U comes before D in execution order |
| 351 | /// if (U & D) |
| 352 | /// if (U != D || HasLoop(S)) |
| 353 | /// Add anti-dep: U -> D |
| 354 | /// if (HasLoop(S)) |
| 355 | /// Add true-dep: D -> U |
| 356 | for (/*continue JI*/ JE = ModRefCurrent.usersEnd(); JI != JE; ++JI) |
| 357 | if (!Disjoint(mapCurrent.find(*II)->second.getModSet(), |
| 358 | mapCurrent.find(*JI)->second.getRefSet())) |
| 359 | { |
| 360 | if (*II != *JI || hasLoop) |
| 361 | funcDepGraph->AddSimpleDependence(**JI, **II, AntiDependence); |
| 362 | if (hasLoop) |
| 363 | funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence); |
| 364 | } |
Chris Lattner | 507bdf9 | 2005-01-12 04:51:37 +0000 | [diff] [blame] | 365 | } |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 366 | |
| 367 | /// for every def Dnext in DefSetPrev |
| 368 | /// // NOTE: Dnext comes after D in execution order |
| 369 | /// if (Dnext & D) |
| 370 | /// Add output-dep: D -> Dnext |
| 371 | for (ModRefTable::ref_iterator JI=ModRefAfter.defsBegin(), |
| 372 | JE=ModRefAfter.defsEnd(); JI != JE; ++JI) |
| 373 | if (!Disjoint(mapCurrent.find(*II)->second.getModSet(), |
| 374 | mapAfter.find(*JI)->second.getModSet())) |
| 375 | funcDepGraph->AddSimpleDependence(**II, **JI, OutputDependence); |
| 376 | |
| 377 | /// for every use Unext in UseSetAfter |
| 378 | /// // NOTE: Unext comes after D in execution order |
| 379 | /// if (Unext & D) |
| 380 | /// Add true-dep: D -> Unext |
| 381 | for (ModRefTable::ref_iterator JI=ModRefAfter.usersBegin(), |
| 382 | JE=ModRefAfter.usersEnd(); JI != JE; ++JI) |
| 383 | if (!Disjoint(mapCurrent.find(*II)->second.getModSet(), |
| 384 | mapAfter.find(*JI)->second.getRefSet())) |
| 385 | funcDepGraph->AddSimpleDependence(**II, **JI, TrueDependence); |
| 386 | } |
| 387 | |
| 388 | /// |
| 389 | /// for every use U in UseSetCurrent |
| 390 | /// for every def Dnext in DefSetAfter |
| 391 | /// // NOTE: Dnext comes after U in execution order |
| 392 | /// if (Dnext & D) |
| 393 | /// Add anti-dep: U -> Dnext |
| 394 | for (ModRefTable::ref_iterator II=ModRefCurrent.usersBegin(), |
| 395 | IE=ModRefCurrent.usersEnd(); II != IE; ++II) |
| 396 | for (ModRefTable::ref_iterator JI=ModRefAfter.defsBegin(), |
| 397 | JE=ModRefAfter.defsEnd(); JI != JE; ++JI) |
| 398 | if (!Disjoint(mapCurrent.find(*II)->second.getRefSet(), |
| 399 | mapAfter.find(*JI)->second.getModSet())) |
| 400 | funcDepGraph->AddSimpleDependence(**II, **JI, AntiDependence); |
| 401 | |
| 402 | /// Add ModRefCurrent to ModRefAfter: { (I, ModRef[I] ) } |
| 403 | /// Add DefSetCurrent to DefSetAfter: { I : Mod[I] != NULL } |
| 404 | /// Add UseSetCurrent to UseSetAfter: { I : Ref[I] != NULL } |
| 405 | ModRefAfter.Insert(ModRefCurrent); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | /// Debugging support methods |
| 410 | /// |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 411 | void MemoryDepAnalysis::print(std::ostream &O, const Module*) const |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 412 | { |
| 413 | // TEMPORARY LOOP |
| 414 | for (hash_map<Function*, DependenceGraph*>::const_iterator |
| 415 | I = funcMap.begin(), E = funcMap.end(); I != E; ++I) |
| 416 | { |
| 417 | Function* func = I->first; |
| 418 | DependenceGraph* depGraph = I->second; |
| 419 | |
| 420 | O << "\n================================================================\n"; |
| 421 | O << "DEPENDENCE GRAPH FOR MEMORY OPERATIONS IN FUNCTION " << func->getName(); |
| 422 | O << "\n================================================================\n\n"; |
| 423 | depGraph->print(*func, O); |
| 424 | |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | |
| 429 | /// |
| 430 | /// Run the pass on a function |
| 431 | /// |
Chris Lattner | 0c0023b | 2003-08-31 19:29:52 +0000 | [diff] [blame] | 432 | bool MemoryDepAnalysis::runOnFunction(Function &F) { |
| 433 | assert(!F.isExternal()); |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 434 | |
| 435 | // Get the FunctionModRefInfo holding IPModRef results for this function. |
| 436 | // Use the TD graph recorded within the FunctionModRefInfo object, which |
| 437 | // may not be the same as the original TD graph computed by DS analysis. |
| 438 | // |
Chris Lattner | 0c0023b | 2003-08-31 19:29:52 +0000 | [diff] [blame] | 439 | funcModRef = &getAnalysis<IPModRef>().getFunctionModRefInfo(F); |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 440 | funcGraph = &funcModRef->getFuncGraph(); |
| 441 | |
| 442 | // TEMPORARY: ptr to depGraph (later just becomes "this"). |
Chris Lattner | 0c0023b | 2003-08-31 19:29:52 +0000 | [diff] [blame] | 443 | assert(!funcMap.count(&F) && "Analyzing function twice?"); |
| 444 | funcDepGraph = funcMap[&F] = new DependenceGraph(); |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 445 | |
| 446 | ModRefTable ModRefAfter; |
| 447 | |
Chris Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 448 | for (scc_iterator<Function*> I = scc_begin(&F), E = scc_end(&F); I != E; ++I) |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 449 | ProcessSCC(*I, ModRefAfter, I.hasLoop()); |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 450 | |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | //------------------------------------------------------------------------- |
| 456 | // TEMPORARY FUNCTIONS TO MAKE THIS A MODULE PASS --- |
| 457 | // These functions will go away once this class becomes a FunctionPass. |
| 458 | // |
| 459 | |
| 460 | // Driver function to compute dependence graphs for every function. |
| 461 | // This is temporary and will go away once this is a FunctionPass. |
| 462 | // |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 463 | bool MemoryDepAnalysis::runOnModule(Module& M) |
Vikram S. Adve | 96b21c1 | 2002-12-08 13:26:29 +0000 | [diff] [blame] | 464 | { |
| 465 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) |
| 466 | if (! FI->isExternal()) |
| 467 | runOnFunction(*FI); // automatically inserts each depGraph into funcMap |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | // Release all the dependence graphs in the map. |
| 472 | void MemoryDepAnalysis::releaseMemory() |
| 473 | { |
| 474 | for (hash_map<Function*, DependenceGraph*>::const_iterator |
| 475 | I = funcMap.begin(), E = funcMap.end(); I != E; ++I) |
| 476 | delete I->second; |
| 477 | funcMap.clear(); |
| 478 | |
| 479 | // Clear pointers because the pass constructor will not be invoked again. |
| 480 | funcDepGraph = NULL; |
| 481 | funcGraph = NULL; |
| 482 | funcModRef = NULL; |
| 483 | } |
| 484 | |
| 485 | MemoryDepAnalysis::~MemoryDepAnalysis() |
| 486 | { |
| 487 | releaseMemory(); |
| 488 | } |
| 489 | |
| 490 | //----END TEMPORARY FUNCTIONS---------------------------------------------- |
| 491 | |
| 492 | |
| 493 | void MemoryDepAnalysis::dump() const |
| 494 | { |
| 495 | this->print(std::cerr); |
| 496 | } |
| 497 | |
| 498 | static RegisterAnalysis<MemoryDepAnalysis> |
| 499 | Z("memdep", "Memory Dependence Analysis"); |
| 500 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 501 | |
| 502 | } // End llvm namespace |