Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 1 | //===- LoadValueNumbering.cpp - Load Value #'ing Implementation -*- C++ -*-===// |
| 2 | // |
| 3 | // This file implements a value numbering pass that value #'s load instructions. |
| 4 | // To do this, it finds lexically identical load instructions, and uses alias |
| 5 | // analysis to determine which loads are guaranteed to produce the same value. |
| 6 | // |
| 7 | // This pass builds off of another value numbering pass to implement value |
| 8 | // numbering for non-load instructions. It uses Alias Analysis so that it can |
| 9 | // disambiguate the load instructions. The more powerful these base analyses |
| 10 | // are, the more powerful the resultant analysis will be. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Analysis/LoadValueNumbering.h" |
| 15 | #include "llvm/Analysis/ValueNumbering.h" |
| 16 | #include "llvm/Analysis/AliasAnalysis.h" |
| 17 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/iMemory.h" |
| 21 | #include "llvm/BasicBlock.h" |
| 22 | #include "llvm/Support/CFG.h" |
| 23 | #include <algorithm> |
| 24 | #include <set> |
| 25 | |
| 26 | namespace { |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 27 | // FIXME: This should not be a FunctionPass. |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 28 | struct LoadVN : public FunctionPass, public ValueNumbering { |
| 29 | |
| 30 | /// Pass Implementation stuff. This doesn't do any analysis. |
| 31 | /// |
| 32 | bool runOnFunction(Function &) { return false; } |
| 33 | |
| 34 | /// getAnalysisUsage - Does not modify anything. It uses Value Numbering |
| 35 | /// and Alias Analysis. |
| 36 | /// |
| 37 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 38 | |
| 39 | /// getEqualNumberNodes - Return nodes with the same value number as the |
| 40 | /// specified Value. This fills in the argument vector with any equal |
| 41 | /// values. |
| 42 | /// |
| 43 | virtual void getEqualNumberNodes(Value *V1, |
| 44 | std::vector<Value*> &RetVals) const; |
| 45 | private: |
| 46 | /// haveEqualValueNumber - Given two load instructions, determine if they |
| 47 | /// both produce the same value on every execution of the program, assuming |
| 48 | /// that their source operands always give the same value. This uses the |
| 49 | /// AliasAnalysis implementation to invalidate loads when stores or function |
| 50 | /// calls occur that could modify the value produced by the load. |
| 51 | /// |
| 52 | bool haveEqualValueNumber(LoadInst *LI, LoadInst *LI2, AliasAnalysis &AA, |
| 53 | DominatorSet &DomSetInfo) const; |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 54 | bool haveEqualValueNumber(LoadInst *LI, StoreInst *SI, AliasAnalysis &AA, |
| 55 | DominatorSet &DomSetInfo) const; |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | // Register this pass... |
| 59 | RegisterOpt<LoadVN> X("load-vn", "Load Value Numbering"); |
| 60 | |
| 61 | // Declare that we implement the ValueNumbering interface |
| 62 | RegisterAnalysisGroup<ValueNumbering, LoadVN> Y; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | Pass *createLoadValueNumberingPass() { return new LoadVN(); } |
| 68 | |
| 69 | |
| 70 | /// getAnalysisUsage - Does not modify anything. It uses Value Numbering and |
| 71 | /// Alias Analysis. |
| 72 | /// |
| 73 | void LoadVN::getAnalysisUsage(AnalysisUsage &AU) const { |
| 74 | AU.setPreservesAll(); |
| 75 | AU.addRequired<AliasAnalysis>(); |
| 76 | AU.addRequired<ValueNumbering>(); |
| 77 | AU.addRequired<DominatorSet>(); |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 78 | AU.addRequired<TargetData>(); |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // getEqualNumberNodes - Return nodes with the same value number as the |
| 82 | // specified Value. This fills in the argument vector with any equal values. |
| 83 | // |
| 84 | void LoadVN::getEqualNumberNodes(Value *V, |
| 85 | std::vector<Value*> &RetVals) const { |
| 86 | |
| 87 | if (LoadInst *LI = dyn_cast<LoadInst>(V)) { |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 88 | // If we have a load instruction, find all of the load and store |
| 89 | // instructions that use the same source operand. We implement this |
| 90 | // recursively, because there could be a load of a load of a load that are |
| 91 | // all identical. We are guaranteed that this cannot be an infinite |
| 92 | // recursion because load instructions would have to pass through a PHI node |
| 93 | // in order for there to be a cycle. The PHI node would be handled by the |
| 94 | // else case here, breaking the infinite recursion. |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 95 | // |
| 96 | std::vector<Value*> PointerSources; |
| 97 | getEqualNumberNodes(LI->getOperand(0), PointerSources); |
| 98 | PointerSources.push_back(LI->getOperand(0)); |
| 99 | |
| 100 | Function *F = LI->getParent()->getParent(); |
| 101 | |
| 102 | // Now that we know the set of equivalent source pointers for the load |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 103 | // instruction, look to see if there are any load or store candiates that |
| 104 | // are identical. |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 105 | // |
| 106 | std::vector<LoadInst*> CandidateLoads; |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 107 | std::vector<StoreInst*> CandidateStores; |
| 108 | |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 109 | while (!PointerSources.empty()) { |
| 110 | Value *Source = PointerSources.back(); |
| 111 | PointerSources.pop_back(); // Get a source pointer... |
| 112 | |
| 113 | for (Value::use_iterator UI = Source->use_begin(), UE = Source->use_end(); |
| 114 | UI != UE; ++UI) |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 115 | if (LoadInst *Cand = dyn_cast<LoadInst>(*UI)) {// Is a load of source? |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 116 | if (Cand->getParent()->getParent() == F && // In the same function? |
| 117 | Cand != LI) // Not LI itself? |
| 118 | CandidateLoads.push_back(Cand); // Got one... |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 119 | } else if (StoreInst *Cand = dyn_cast<StoreInst>(*UI)) { |
| 120 | if (Cand->getParent()->getParent() == F && |
| 121 | Cand->getOperand(1) == Source) // It's a store THROUGH the ptr... |
| 122 | CandidateStores.push_back(Cand); |
| 123 | } |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Remove duplicates from the CandidateLoads list because alias analysis |
| 127 | // processing may be somewhat expensive and we don't want to do more work |
| 128 | // than neccesary. |
| 129 | // |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 130 | unsigned OldSize = CandidateLoads.size(); |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 131 | std::sort(CandidateLoads.begin(), CandidateLoads.end()); |
| 132 | CandidateLoads.erase(std::unique(CandidateLoads.begin(), |
| 133 | CandidateLoads.end()), |
| 134 | CandidateLoads.end()); |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 135 | // FIXME: REMOVE THIS SORTING AND UNIQUING IF IT CAN'T HAPPEN |
| 136 | assert(CandidateLoads.size() == OldSize && "Shrunk the candloads list?"); |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 137 | |
| 138 | // Get Alias Analysis... |
| 139 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 140 | DominatorSet &DomSetInfo = getAnalysis<DominatorSet>(); |
| 141 | |
| 142 | // Loop over all of the candindate loads. If they are not invalidated by |
| 143 | // stores or calls between execution of them and LI, then add them to |
| 144 | // RetVals. |
| 145 | for (unsigned i = 0, e = CandidateLoads.size(); i != e; ++i) |
| 146 | if (haveEqualValueNumber(LI, CandidateLoads[i], AA, DomSetInfo)) |
| 147 | RetVals.push_back(CandidateLoads[i]); |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 148 | for (unsigned i = 0, e = CandidateStores.size(); i != e; ++i) |
| 149 | if (haveEqualValueNumber(LI, CandidateStores[i], AA, DomSetInfo)) |
| 150 | RetVals.push_back(CandidateStores[i]->getOperand(0)); |
| 151 | |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 152 | } else { |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 153 | assert(&getAnalysis<ValueNumbering>() != (ValueNumbering*)this && |
| 154 | "getAnalysis() returned this!"); |
| 155 | |
| 156 | // Not a load instruction? Just chain to the base value numbering |
| 157 | // implementation to satisfy the request... |
| 158 | return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // CheckForInvalidatingInst - Return true if BB or any of the predecessors of BB |
| 163 | // (until DestBB) contain an instruction that might invalidate Ptr. |
| 164 | // |
| 165 | static bool CheckForInvalidatingInst(BasicBlock *BB, BasicBlock *DestBB, |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 166 | Value *Ptr, unsigned Size, |
| 167 | AliasAnalysis &AA, |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 168 | std::set<BasicBlock*> &VisitedSet) { |
| 169 | // Found the termination point! |
| 170 | if (BB == DestBB || VisitedSet.count(BB)) return false; |
| 171 | |
| 172 | // Avoid infinite recursion! |
| 173 | VisitedSet.insert(BB); |
| 174 | |
| 175 | // Can this basic block modify Ptr? |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 176 | if (AA.canBasicBlockModify(*BB, Ptr, Size)) |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 177 | return true; |
| 178 | |
| 179 | // Check all of our predecessor blocks... |
| 180 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 181 | if (CheckForInvalidatingInst(*PI, DestBB, Ptr, Size, AA, VisitedSet)) |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 182 | return true; |
| 183 | |
| 184 | // None of our predecessor blocks contain an invalidating instruction, and we |
| 185 | // don't either! |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /// haveEqualValueNumber - Given two load instructions, determine if they both |
| 191 | /// produce the same value on every execution of the program, assuming that |
| 192 | /// their source operands always give the same value. This uses the |
| 193 | /// AliasAnalysis implementation to invalidate loads when stores or function |
| 194 | /// calls occur that could modify the value produced by the load. |
| 195 | /// |
| 196 | bool LoadVN::haveEqualValueNumber(LoadInst *L1, LoadInst *L2, |
| 197 | AliasAnalysis &AA, |
| 198 | DominatorSet &DomSetInfo) const { |
| 199 | // Figure out which load dominates the other one. If neither dominates the |
| 200 | // other we cannot eliminate them. |
| 201 | // |
| 202 | // FIXME: This could be enhanced to some cases with a shared dominator! |
| 203 | // |
| 204 | if (DomSetInfo.dominates(L2, L1)) |
| 205 | std::swap(L1, L2); // Make L1 dominate L2 |
| 206 | else if (!DomSetInfo.dominates(L1, L2)) |
| 207 | return false; // Neither instruction dominates the other one... |
| 208 | |
| 209 | BasicBlock *BB1 = L1->getParent(), *BB2 = L2->getParent(); |
| 210 | Value *LoadAddress = L1->getOperand(0); |
| 211 | |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 212 | assert(L1->getType() == L2->getType() && |
| 213 | "How could the same source pointer return different types?"); |
| 214 | |
| 215 | // Find out how many bytes of memory are loaded by the load instruction... |
| 216 | unsigned LoadSize = getAnalysis<TargetData>().getTypeSize(L1->getType()); |
| 217 | |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 218 | // L1 now dominates L2. Check to see if the intervening instructions between |
| 219 | // the two loads include a store or call... |
| 220 | // |
| 221 | if (BB1 == BB2) { // In same basic block? |
| 222 | // In this degenerate case, no checking of global basic blocks has to occur |
| 223 | // just check the instructions BETWEEN L1 & L2... |
| 224 | // |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 225 | if (AA.canInstructionRangeModify(*L1, *L2, LoadAddress, LoadSize)) |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 226 | return false; // Cannot eliminate load |
| 227 | |
| 228 | // No instructions invalidate the loads, they produce the same value! |
| 229 | return true; |
| 230 | } else { |
| 231 | // Make sure that there are no store instructions between L1 and the end of |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 232 | // its basic block... |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 233 | // |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 234 | if (AA.canInstructionRangeModify(*L1, *BB1->getTerminator(), LoadAddress, |
| 235 | LoadSize)) |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 236 | return false; // Cannot eliminate load |
| 237 | |
| 238 | // Make sure that there are no store instructions between the start of BB2 |
| 239 | // and the second load instruction... |
| 240 | // |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 241 | if (AA.canInstructionRangeModify(BB2->front(), *L2, LoadAddress, LoadSize)) |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 242 | return false; // Cannot eliminate load |
| 243 | |
| 244 | // Do a depth first traversal of the inverse CFG starting at L2's block, |
| 245 | // looking for L1's block. The inverse CFG is made up of the predecessor |
| 246 | // nodes of a block... so all of the edges in the graph are "backward". |
| 247 | // |
| 248 | std::set<BasicBlock*> VisitedSet; |
| 249 | for (pred_iterator PI = pred_begin(BB2), PE = pred_end(BB2); PI != PE; ++PI) |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 250 | if (CheckForInvalidatingInst(*PI, BB1, LoadAddress, LoadSize, AA, |
| 251 | VisitedSet)) |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 252 | return false; |
| 253 | |
| 254 | // If we passed all of these checks then we are sure that the two loads |
| 255 | // produce the same value. |
| 256 | return true; |
| 257 | } |
| 258 | } |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 259 | |
| 260 | |
| 261 | /// haveEqualValueNumber - Given a load instruction and a store instruction, |
| 262 | /// determine if the stored value reaches the loaded value unambiguously on |
| 263 | /// every execution of the program. This uses the AliasAnalysis implementation |
| 264 | /// to invalidate the stored value when stores or function calls occur that |
| 265 | /// could modify the value produced by the load. |
| 266 | /// |
| 267 | bool LoadVN::haveEqualValueNumber(LoadInst *Load, StoreInst *Store, |
| 268 | AliasAnalysis &AA, |
| 269 | DominatorSet &DomSetInfo) const { |
| 270 | // If the store does not dominate the load, we cannot do anything... |
| 271 | if (!DomSetInfo.dominates(Store, Load)) |
| 272 | return false; |
| 273 | |
| 274 | BasicBlock *BB1 = Store->getParent(), *BB2 = Load->getParent(); |
| 275 | Value *LoadAddress = Load->getOperand(0); |
| 276 | |
| 277 | assert(LoadAddress->getType() == Store->getOperand(1)->getType() && |
| 278 | "How could the same source pointer return different types?"); |
| 279 | |
| 280 | // Find out how many bytes of memory are loaded by the load instruction... |
| 281 | unsigned LoadSize = getAnalysis<TargetData>().getTypeSize(Load->getType()); |
| 282 | |
| 283 | // Compute a basic block iterator pointing to the instruction after the store. |
| 284 | BasicBlock::iterator StoreIt = Store; ++StoreIt; |
| 285 | |
| 286 | // Check to see if the intervening instructions between the two store and load |
| 287 | // include a store or call... |
| 288 | // |
| 289 | if (BB1 == BB2) { // In same basic block? |
| 290 | // In this degenerate case, no checking of global basic blocks has to occur |
| 291 | // just check the instructions BETWEEN Store & Load... |
| 292 | // |
| 293 | if (AA.canInstructionRangeModify(*StoreIt, *Load, LoadAddress, LoadSize)) |
| 294 | return false; // Cannot eliminate load |
| 295 | |
| 296 | // No instructions invalidate the stored value, they produce the same value! |
| 297 | return true; |
| 298 | } else { |
| 299 | // Make sure that there are no store instructions between the Store and the |
| 300 | // end of its basic block... |
| 301 | // |
| 302 | if (AA.canInstructionRangeModify(*StoreIt, *BB1->getTerminator(), |
| 303 | LoadAddress, LoadSize)) |
| 304 | return false; // Cannot eliminate load |
| 305 | |
| 306 | // Make sure that there are no store instructions between the start of BB2 |
| 307 | // and the second load instruction... |
| 308 | // |
| 309 | if (AA.canInstructionRangeModify(BB2->front(), *Load, LoadAddress,LoadSize)) |
| 310 | return false; // Cannot eliminate load |
| 311 | |
| 312 | // Do a depth first traversal of the inverse CFG starting at L2's block, |
| 313 | // looking for L1's block. The inverse CFG is made up of the predecessor |
| 314 | // nodes of a block... so all of the edges in the graph are "backward". |
| 315 | // |
| 316 | std::set<BasicBlock*> VisitedSet; |
| 317 | for (pred_iterator PI = pred_begin(BB2), PE = pred_end(BB2); PI != PE; ++PI) |
| 318 | if (CheckForInvalidatingInst(*PI, BB1, LoadAddress, LoadSize, AA, |
| 319 | VisitedSet)) |
| 320 | return false; |
| 321 | |
| 322 | // If we passed all of these checks then we are sure that the two loads |
| 323 | // produce the same value. |
| 324 | return true; |
| 325 | } |
| 326 | } |