Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 1 | //===- LoadValueNumbering.cpp - Load Value #'ing Implementation -*- C++ -*-===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 10 | // This file implements a value numbering pass that value numbers load and call |
| 11 | // instructions. To do this, it finds lexically identical load instructions, |
| 12 | // and uses alias analysis to determine which loads are guaranteed to produce |
| 13 | // the same value. To value number call instructions, it looks for calls to |
| 14 | // functions that do not write to memory which do not have intervening |
| 15 | // instructions that clobber the memory that is read from. |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 16 | // |
| 17 | // This pass builds off of another value numbering pass to implement value |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 18 | // numbering for non-load and non-call instructions. It uses Alias Analysis so |
| 19 | // that it can disambiguate the load instructions. The more powerful these base |
| 20 | // analyses are, the more powerful the resultant value numbering will be. |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | #include "llvm/Analysis/LoadValueNumbering.h" |
Chris Lattner | 2652da6 | 2005-01-29 05:57:01 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 26 | #include "llvm/Function.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 27 | #include "llvm/Instructions.h" |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 28 | #include "llvm/Pass.h" |
| 29 | #include "llvm/Type.h" |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/ValueNumbering.h" |
| 31 | #include "llvm/Analysis/AliasAnalysis.h" |
| 32 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CFG.h" |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 35 | #include <set> |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Chris Lattner | 270db36 | 2004-02-05 05:51:40 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 39 | namespace { |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 40 | // FIXME: This should not be a FunctionPass. |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 41 | struct LoadVN : public FunctionPass, public ValueNumbering { |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 43 | /// Pass Implementation stuff. This doesn't do any analysis. |
| 44 | /// |
| 45 | bool runOnFunction(Function &) { return false; } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 47 | /// getAnalysisUsage - Does not modify anything. It uses Value Numbering |
| 48 | /// and Alias Analysis. |
| 49 | /// |
| 50 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 52 | /// getEqualNumberNodes - Return nodes with the same value number as the |
| 53 | /// specified Value. This fills in the argument vector with any equal |
| 54 | /// values. |
| 55 | /// |
| 56 | virtual void getEqualNumberNodes(Value *V1, |
| 57 | std::vector<Value*> &RetVals) const; |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 0f312d6 | 2004-05-23 21:13:24 +0000 | [diff] [blame] | 59 | /// deleteValue - This method should be called whenever an LLVM Value is |
| 60 | /// deleted from the program, for example when an instruction is found to be |
| 61 | /// redundant and is eliminated. |
| 62 | /// |
| 63 | virtual void deleteValue(Value *V) { |
| 64 | getAnalysis<AliasAnalysis>().deleteValue(V); |
| 65 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 0f312d6 | 2004-05-23 21:13:24 +0000 | [diff] [blame] | 67 | /// copyValue - This method should be used whenever a preexisting value in |
| 68 | /// the program is copied or cloned, introducing a new value. Note that |
| 69 | /// analysis implementations should tolerate clients that use this method to |
| 70 | /// introduce the same value multiple times: if the analysis already knows |
| 71 | /// about a value, it should ignore the request. |
| 72 | /// |
| 73 | virtual void copyValue(Value *From, Value *To) { |
| 74 | getAnalysis<AliasAnalysis>().copyValue(From, To); |
| 75 | } |
| 76 | |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 77 | /// getCallEqualNumberNodes - Given a call instruction, find other calls |
| 78 | /// that have the same value number. |
| 79 | void getCallEqualNumberNodes(CallInst *CI, |
| 80 | std::vector<Value*> &RetVals) const; |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | // Register this pass... |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame^] | 84 | RegisterPass<LoadVN> X("load-vn", "Load Value Numbering"); |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 85 | |
| 86 | // Declare that we implement the ValueNumbering interface |
| 87 | RegisterAnalysisGroup<ValueNumbering, LoadVN> Y; |
| 88 | } |
| 89 | |
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 90 | FunctionPass *llvm::createLoadValueNumberingPass() { return new LoadVN(); } |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | /// getAnalysisUsage - Does not modify anything. It uses Value Numbering and |
| 94 | /// Alias Analysis. |
| 95 | /// |
| 96 | void LoadVN::getAnalysisUsage(AnalysisUsage &AU) const { |
| 97 | AU.setPreservesAll(); |
Chris Lattner | 10673b6 | 2006-01-08 09:10:04 +0000 | [diff] [blame] | 98 | AU.addRequiredTransitive<AliasAnalysis>(); |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 99 | AU.addRequired<ValueNumbering>(); |
Chris Lattner | 10673b6 | 2006-01-08 09:10:04 +0000 | [diff] [blame] | 100 | AU.addRequiredTransitive<DominatorSet>(); |
| 101 | AU.addRequiredTransitive<TargetData>(); |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 104 | static bool isPathTransparentTo(BasicBlock *CurBlock, BasicBlock *Dom, |
| 105 | Value *Ptr, unsigned Size, AliasAnalysis &AA, |
| 106 | std::set<BasicBlock*> &Visited, |
| 107 | std::map<BasicBlock*, bool> &TransparentBlocks){ |
| 108 | // If we have already checked out this path, or if we reached our destination, |
| 109 | // stop searching, returning success. |
| 110 | if (CurBlock == Dom || !Visited.insert(CurBlock).second) |
| 111 | return true; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 113 | // Check whether this block is known transparent or not. |
| 114 | std::map<BasicBlock*, bool>::iterator TBI = |
| 115 | TransparentBlocks.lower_bound(CurBlock); |
| 116 | |
| 117 | if (TBI == TransparentBlocks.end() || TBI->first != CurBlock) { |
| 118 | // If this basic block can modify the memory location, then the path is not |
| 119 | // transparent! |
| 120 | if (AA.canBasicBlockModify(*CurBlock, Ptr, Size)) { |
| 121 | TransparentBlocks.insert(TBI, std::make_pair(CurBlock, false)); |
| 122 | return false; |
| 123 | } |
Chris Lattner | 0f312d6 | 2004-05-23 21:13:24 +0000 | [diff] [blame] | 124 | TransparentBlocks.insert(TBI, std::make_pair(CurBlock, true)); |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 125 | } else if (!TBI->second) |
| 126 | // This block is known non-transparent, so that path can't be either. |
| 127 | return false; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 129 | // The current block is known to be transparent. The entire path is |
| 130 | // transparent if all of the predecessors paths to the parent is also |
| 131 | // transparent to the memory location. |
| 132 | for (pred_iterator PI = pred_begin(CurBlock), E = pred_end(CurBlock); |
| 133 | PI != E; ++PI) |
| 134 | if (!isPathTransparentTo(*PI, Dom, Ptr, Size, AA, Visited, |
| 135 | TransparentBlocks)) |
| 136 | return false; |
| 137 | return true; |
| 138 | } |
| 139 | |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 140 | /// getCallEqualNumberNodes - Given a call instruction, find other calls that |
| 141 | /// have the same value number. |
| 142 | void LoadVN::getCallEqualNumberNodes(CallInst *CI, |
| 143 | std::vector<Value*> &RetVals) const { |
| 144 | Function *CF = CI->getCalledFunction(); |
| 145 | if (CF == 0) return; // Indirect call. |
| 146 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Chris Lattner | fd4b3c4 | 2004-12-15 18:14:04 +0000 | [diff] [blame] | 147 | AliasAnalysis::ModRefBehavior MRB = AA.getModRefBehavior(CF, CI); |
| 148 | if (MRB != AliasAnalysis::DoesNotAccessMemory && |
| 149 | MRB != AliasAnalysis::OnlyReadsMemory) |
| 150 | return; // Nothing we can do for now. |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 151 | |
| 152 | // Scan all of the arguments of the function, looking for one that is not |
| 153 | // global. In particular, we would prefer to have an argument or instruction |
| 154 | // operand to chase the def-use chains of. |
| 155 | Value *Op = CF; |
| 156 | for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) |
| 157 | if (isa<Argument>(CI->getOperand(i)) || |
| 158 | isa<Instruction>(CI->getOperand(i))) { |
| 159 | Op = CI->getOperand(i); |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | // Identify all lexically identical calls in this function. |
| 164 | std::vector<CallInst*> IdenticalCalls; |
| 165 | |
| 166 | Function *CIFunc = CI->getParent()->getParent(); |
| 167 | for (Value::use_iterator UI = Op->use_begin(), E = Op->use_end(); UI != E; |
| 168 | ++UI) |
| 169 | if (CallInst *C = dyn_cast<CallInst>(*UI)) |
| 170 | if (C->getNumOperands() == CI->getNumOperands() && |
| 171 | C->getOperand(0) == CI->getOperand(0) && |
| 172 | C->getParent()->getParent() == CIFunc && C != CI) { |
| 173 | bool AllOperandsEqual = true; |
| 174 | for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) |
| 175 | if (C->getOperand(i) != CI->getOperand(i)) { |
| 176 | AllOperandsEqual = false; |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | if (AllOperandsEqual) |
| 181 | IdenticalCalls.push_back(C); |
| 182 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 184 | if (IdenticalCalls.empty()) return; |
| 185 | |
| 186 | // Eliminate duplicates, which could occur if we chose a value that is passed |
| 187 | // into a call site multiple times. |
| 188 | std::sort(IdenticalCalls.begin(), IdenticalCalls.end()); |
| 189 | IdenticalCalls.erase(std::unique(IdenticalCalls.begin(),IdenticalCalls.end()), |
| 190 | IdenticalCalls.end()); |
| 191 | |
| 192 | // If the call reads memory, we must make sure that there are no stores |
| 193 | // between the calls in question. |
| 194 | // |
| 195 | // FIXME: This should use mod/ref information. What we really care about it |
| 196 | // whether an intervening instruction could modify memory that is read, not |
| 197 | // ANY memory. |
| 198 | // |
Chris Lattner | fd4b3c4 | 2004-12-15 18:14:04 +0000 | [diff] [blame] | 199 | if (MRB == AliasAnalysis::OnlyReadsMemory) { |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 200 | DominatorSet &DomSetInfo = getAnalysis<DominatorSet>(); |
| 201 | BasicBlock *CIBB = CI->getParent(); |
| 202 | for (unsigned i = 0; i != IdenticalCalls.size(); ++i) { |
| 203 | CallInst *C = IdenticalCalls[i]; |
| 204 | bool CantEqual = false; |
| 205 | |
| 206 | if (DomSetInfo.dominates(CIBB, C->getParent())) { |
| 207 | // FIXME: we currently only handle the case where both calls are in the |
| 208 | // same basic block. |
| 209 | if (CIBB != C->getParent()) { |
| 210 | CantEqual = true; |
| 211 | } else { |
| 212 | Instruction *First = CI, *Second = C; |
| 213 | if (!DomSetInfo.dominates(CI, C)) |
| 214 | std::swap(First, Second); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 216 | // Scan the instructions between the calls, checking for stores or |
| 217 | // calls to dangerous functions. |
| 218 | BasicBlock::iterator I = First; |
| 219 | for (++First; I != BasicBlock::iterator(Second); ++I) { |
| 220 | if (isa<StoreInst>(I)) { |
| 221 | // FIXME: We could use mod/ref information to make this much |
| 222 | // better! |
| 223 | CantEqual = true; |
| 224 | break; |
| 225 | } else if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 226 | if (CI->getCalledFunction() == 0 || |
| 227 | !AA.onlyReadsMemory(CI->getCalledFunction())) { |
| 228 | CantEqual = true; |
| 229 | break; |
| 230 | } |
| 231 | } else if (I->mayWriteToMemory()) { |
| 232 | CantEqual = true; |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | } else if (DomSetInfo.dominates(C->getParent(), CIBB)) { |
| 239 | // FIXME: We could implement this, but we don't for now. |
| 240 | CantEqual = true; |
| 241 | } else { |
| 242 | // FIXME: if one doesn't dominate the other, we can't tell yet. |
| 243 | CantEqual = true; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | if (CantEqual) { |
| 248 | // This call does not produce the same value as the one in the query. |
| 249 | std::swap(IdenticalCalls[i--], IdenticalCalls.back()); |
| 250 | IdenticalCalls.pop_back(); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // Any calls that are identical and not destroyed will produce equal values! |
| 256 | for (unsigned i = 0, e = IdenticalCalls.size(); i != e; ++i) |
| 257 | RetVals.push_back(IdenticalCalls[i]); |
| 258 | } |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 260 | // getEqualNumberNodes - Return nodes with the same value number as the |
| 261 | // specified Value. This fills in the argument vector with any equal values. |
| 262 | // |
| 263 | void LoadVN::getEqualNumberNodes(Value *V, |
| 264 | std::vector<Value*> &RetVals) const { |
Chris Lattner | aed2c6d | 2003-06-29 00:53:34 +0000 | [diff] [blame] | 265 | // If the alias analysis has any must alias information to share with us, we |
Misha Brukman | 7bc439a | 2003-09-11 15:31:17 +0000 | [diff] [blame] | 266 | // can definitely use it. |
Chris Lattner | aed2c6d | 2003-06-29 00:53:34 +0000 | [diff] [blame] | 267 | if (isa<PointerType>(V->getType())) |
| 268 | getAnalysis<AliasAnalysis>().getMustAliases(V, RetVals); |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 57ef9a2 | 2004-02-05 05:56:23 +0000 | [diff] [blame] | 270 | if (!isa<LoadInst>(V)) { |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 271 | if (CallInst *CI = dyn_cast<CallInst>(V)) |
Chris Lattner | 002be76 | 2004-03-16 03:41:35 +0000 | [diff] [blame] | 272 | getCallEqualNumberNodes(CI, RetVals); |
Chris Lattner | 5a6e947 | 2004-03-15 05:44:59 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 57ef9a2 | 2004-02-05 05:56:23 +0000 | [diff] [blame] | 274 | // Not a load instruction? Just chain to the base value numbering |
| 275 | // implementation to satisfy the request... |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 276 | assert(&getAnalysis<ValueNumbering>() != (ValueNumbering*)this && |
| 277 | "getAnalysis() returned this!"); |
| 278 | |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 279 | return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals); |
| 280 | } |
Chris Lattner | 57ef9a2 | 2004-02-05 05:56:23 +0000 | [diff] [blame] | 281 | |
| 282 | // Volatile loads cannot be replaced with the value of other loads. |
| 283 | LoadInst *LI = cast<LoadInst>(V); |
| 284 | if (LI->isVolatile()) |
| 285 | return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 287 | Value *LoadPtr = LI->getOperand(0); |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 288 | BasicBlock *LoadBB = LI->getParent(); |
| 289 | Function *F = LoadBB->getParent(); |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 290 | |
Chris Lattner | f98d8d8 | 2003-02-26 19:27:35 +0000 | [diff] [blame] | 291 | // Find out how many bytes of memory are loaded by the load instruction... |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 292 | unsigned LoadSize = getAnalysis<TargetData>().getTypeSize(LI->getType()); |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 293 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 295 | // Figure out if the load is invalidated from the entry of the block it is in |
| 296 | // until the actual instruction. This scans the block backwards from LI. If |
| 297 | // we see any candidate load or store instructions, then we know that the |
| 298 | // candidates have the same value # as LI. |
| 299 | bool LoadInvalidatedInBBBefore = false; |
| 300 | for (BasicBlock::iterator I = LI; I != LoadBB->begin(); ) { |
| 301 | --I; |
Chris Lattner | ee379a1 | 2005-01-29 06:42:34 +0000 | [diff] [blame] | 302 | if (I == LoadPtr) { |
Chris Lattner | 5da8097 | 2004-04-03 00:45:16 +0000 | [diff] [blame] | 303 | // If we run into an allocation of the value being loaded, then the |
Chris Lattner | 2652da6 | 2005-01-29 05:57:01 +0000 | [diff] [blame] | 304 | // contents are not initialized. |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 305 | if (isa<AllocationInst>(I)) |
Chris Lattner | 2652da6 | 2005-01-29 05:57:01 +0000 | [diff] [blame] | 306 | RetVals.push_back(UndefValue::get(LI->getType())); |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 307 | |
| 308 | // Otherwise, since this is the definition of what we are loading, this |
| 309 | // loaded value cannot occur before this block. |
| 310 | LoadInvalidatedInBBBefore = true; |
| 311 | break; |
Chris Lattner | ee379a1 | 2005-01-29 06:42:34 +0000 | [diff] [blame] | 312 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 313 | // If this instruction is a candidate load before LI, we know there are no |
| 314 | // invalidating instructions between it and LI, so they have the same |
| 315 | // value number. |
| 316 | if (LI->getOperand(0) == LoadPtr && !LI->isVolatile()) |
| 317 | RetVals.push_back(I); |
Chris Lattner | adf9b90 | 2004-02-05 00:36:43 +0000 | [diff] [blame] | 318 | } |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 320 | if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) { |
| 321 | // If the invalidating instruction is a store, and its in our candidate |
| 322 | // set, then we can do store-load forwarding: the load has the same value |
| 323 | // # as the stored value. |
Chris Lattner | ee379a1 | 2005-01-29 06:42:34 +0000 | [diff] [blame] | 324 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 325 | if (SI->getOperand(1) == LoadPtr) |
| 326 | RetVals.push_back(I->getOperand(0)); |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 327 | |
| 328 | LoadInvalidatedInBBBefore = true; |
| 329 | break; |
Chris Lattner | adf9b90 | 2004-02-05 00:36:43 +0000 | [diff] [blame] | 330 | } |
Chris Lattner | 71c7ec9 | 2002-08-30 20:28:10 +0000 | [diff] [blame] | 331 | } |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 333 | // Figure out if the load is invalidated between the load and the exit of the |
| 334 | // block it is defined in. While we are scanning the current basic block, if |
| 335 | // we see any candidate loads, then we know they have the same value # as LI. |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 336 | // |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 337 | bool LoadInvalidatedInBBAfter = false; |
| 338 | for (BasicBlock::iterator I = LI->getNext(); I != LoadBB->end(); ++I) { |
| 339 | // If this instruction is a load, then this instruction returns the same |
| 340 | // value as LI. |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 341 | if (isa<LoadInst>(I) && cast<LoadInst>(I)->getOperand(0) == LoadPtr) |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 342 | RetVals.push_back(I); |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 344 | if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) { |
| 345 | LoadInvalidatedInBBAfter = true; |
| 346 | break; |
| 347 | } |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 348 | } |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 350 | // If the pointer is clobbered on entry and on exit to the function, there is |
| 351 | // no need to do any global analysis at all. |
| 352 | if (LoadInvalidatedInBBBefore && LoadInvalidatedInBBAfter) |
| 353 | return; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 355 | // Now that we know the value is not neccesarily killed on entry or exit to |
| 356 | // the BB, find out how many load and store instructions (to this location) |
| 357 | // live in each BB in the function. |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 358 | // |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 359 | std::map<BasicBlock*, unsigned> CandidateLoads; |
| 360 | std::set<BasicBlock*> CandidateStores; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 362 | for (Value::use_iterator UI = LoadPtr->use_begin(), UE = LoadPtr->use_end(); |
| 363 | UI != UE; ++UI) |
| 364 | if (LoadInst *Cand = dyn_cast<LoadInst>(*UI)) {// Is a load of source? |
| 365 | if (Cand->getParent()->getParent() == F && // In the same function? |
| 366 | // Not in LI's block? |
| 367 | Cand->getParent() != LoadBB && !Cand->isVolatile()) |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 368 | ++CandidateLoads[Cand->getParent()]; // Got one. |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 369 | } else if (StoreInst *Cand = dyn_cast<StoreInst>(*UI)) { |
| 370 | if (Cand->getParent()->getParent() == F && !Cand->isVolatile() && |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 371 | Cand->getOperand(1) == LoadPtr) // It's a store THROUGH the ptr. |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 372 | CandidateStores.insert(Cand->getParent()); |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 373 | } |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 15774df | 2005-01-29 06:31:53 +0000 | [diff] [blame] | 375 | // Get dominators. |
| 376 | DominatorSet &DomSetInfo = getAnalysis<DominatorSet>(); |
| 377 | |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 378 | // TransparentBlocks - For each basic block the load/store is alive across, |
| 379 | // figure out if the pointer is invalidated or not. If it is invalidated, the |
| 380 | // boolean is set to false, if it's not it is set to true. If we don't know |
| 381 | // yet, the entry is not in the map. |
| 382 | std::map<BasicBlock*, bool> TransparentBlocks; |
| 383 | |
| 384 | // Loop over all of the basic blocks that also load the value. If the value |
| 385 | // is live across the CFG from the source to destination blocks, and if the |
| 386 | // value is not invalidated in either the source or destination blocks, add it |
| 387 | // to the equivalence sets. |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 388 | for (std::map<BasicBlock*, unsigned>::iterator |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 389 | I = CandidateLoads.begin(), E = CandidateLoads.end(); I != E; ++I) { |
| 390 | bool CantEqual = false; |
| 391 | |
| 392 | // Right now we only can handle cases where one load dominates the other. |
| 393 | // FIXME: generalize this! |
| 394 | BasicBlock *BB1 = I->first, *BB2 = LoadBB; |
| 395 | if (DomSetInfo.dominates(BB1, BB2)) { |
| 396 | // The other load dominates LI. If the loaded value is killed entering |
| 397 | // the LoadBB block, we know the load is not live. |
| 398 | if (LoadInvalidatedInBBBefore) |
| 399 | CantEqual = true; |
| 400 | } else if (DomSetInfo.dominates(BB2, BB1)) { |
| 401 | std::swap(BB1, BB2); // Canonicalize |
| 402 | // LI dominates the other load. If the loaded value is killed exiting |
| 403 | // the LoadBB block, we know the load is not live. |
| 404 | if (LoadInvalidatedInBBAfter) |
| 405 | CantEqual = true; |
| 406 | } else { |
| 407 | // None of these loads can VN the same. |
| 408 | CantEqual = true; |
| 409 | } |
| 410 | |
| 411 | if (!CantEqual) { |
| 412 | // Ok, at this point, we know that BB1 dominates BB2, and that there is |
| 413 | // nothing in the LI block that kills the loaded value. Check to see if |
| 414 | // the value is live across the CFG. |
| 415 | std::set<BasicBlock*> Visited; |
| 416 | for (pred_iterator PI = pred_begin(BB2), E = pred_end(BB2); PI!=E; ++PI) |
| 417 | if (!isPathTransparentTo(*PI, BB1, LoadPtr, LoadSize, AA, |
| 418 | Visited, TransparentBlocks)) { |
| 419 | // None of these loads can VN the same. |
| 420 | CantEqual = true; |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // If the loads can equal so far, scan the basic block that contains the |
| 426 | // loads under consideration to see if they are invalidated in the block. |
| 427 | // For any loads that are not invalidated, add them to the equivalence |
| 428 | // set! |
| 429 | if (!CantEqual) { |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 430 | unsigned NumLoads = I->second; |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 431 | if (BB1 == LoadBB) { |
| 432 | // If LI dominates the block in question, check to see if any of the |
| 433 | // loads in this block are invalidated before they are reached. |
| 434 | for (BasicBlock::iterator BBI = I->first->begin(); ; ++BBI) { |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 435 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 436 | if (LI->getOperand(0) == LoadPtr && !LI->isVolatile()) { |
| 437 | // The load is in the set! |
| 438 | RetVals.push_back(BBI); |
| 439 | if (--NumLoads == 0) break; // Found last load to check. |
| 440 | } |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 441 | } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize) |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 442 | & AliasAnalysis::Mod) { |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 443 | // If there is a modifying instruction, nothing below it will value |
| 444 | // # the same. |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | } else { |
| 449 | // If the block dominates LI, make sure that the loads in the block are |
| 450 | // not invalidated before the block ends. |
| 451 | BasicBlock::iterator BBI = I->first->end(); |
| 452 | while (1) { |
| 453 | --BBI; |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 454 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| 455 | if (LI->getOperand(0) == LoadPtr && !LI->isVolatile()) { |
| 456 | // The load is the same as this load! |
| 457 | RetVals.push_back(BBI); |
| 458 | if (--NumLoads == 0) break; // Found all of the laods. |
| 459 | } |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 460 | } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize) |
| 461 | & AliasAnalysis::Mod) { |
| 462 | // If there is a modifying instruction, nothing above it will value |
| 463 | // # the same. |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | } |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| 471 | // Handle candidate stores. If the loaded location is clobbered on entrance |
| 472 | // to the LoadBB, no store outside of the LoadBB can value number equal, so |
| 473 | // quick exit. |
| 474 | if (LoadInvalidatedInBBBefore) |
| 475 | return; |
| 476 | |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 477 | // Stores in the load-bb are handled above. |
| 478 | CandidateStores.erase(LoadBB); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 480 | for (std::set<BasicBlock*>::iterator I = CandidateStores.begin(), |
| 481 | E = CandidateStores.end(); I != E; ++I) |
| 482 | if (DomSetInfo.dominates(*I, LoadBB)) { |
| 483 | BasicBlock *StoreBB = *I; |
| 484 | |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 485 | // Check to see if the path from the store to the load is transparent |
| 486 | // w.r.t. the memory location. |
| 487 | bool CantEqual = false; |
| 488 | std::set<BasicBlock*> Visited; |
| 489 | for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB); |
| 490 | PI != E; ++PI) |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 491 | if (!isPathTransparentTo(*PI, StoreBB, LoadPtr, LoadSize, AA, |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 492 | Visited, TransparentBlocks)) { |
| 493 | // None of these stores can VN the same. |
| 494 | CantEqual = true; |
| 495 | break; |
| 496 | } |
| 497 | Visited.clear(); |
| 498 | if (!CantEqual) { |
| 499 | // Okay, the path from the store block to the load block is clear, and |
| 500 | // we know that there are no invalidating instructions from the start |
| 501 | // of the load block to the load itself. Now we just scan the store |
| 502 | // block. |
| 503 | |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 504 | BasicBlock::iterator BBI = StoreBB->end(); |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 505 | while (1) { |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 506 | assert(BBI != StoreBB->begin() && |
Chris Lattner | 0f312d6 | 2004-05-23 21:13:24 +0000 | [diff] [blame] | 507 | "There is a store in this block of the pointer, but the store" |
| 508 | " doesn't mod the address being stored to?? Must be a bug in" |
| 509 | " the alias analysis implementation!"); |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 510 | --BBI; |
Chris Lattner | 0f312d6 | 2004-05-23 21:13:24 +0000 | [diff] [blame] | 511 | if (AA.getModRefInfo(BBI, LoadPtr, LoadSize) & AliasAnalysis::Mod) { |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 512 | // If the invalidating instruction is one of the candidates, |
| 513 | // then it provides the value the load loads. |
| 514 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
Chris Lattner | 3725c12 | 2005-01-29 07:04:10 +0000 | [diff] [blame] | 515 | if (SI->getOperand(1) == LoadPtr) |
Chris Lattner | 3b303d9 | 2004-02-05 17:20:00 +0000 | [diff] [blame] | 516 | RetVals.push_back(SI->getOperand(0)); |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | } |
Chris Lattner | 28c6cf2 | 2003-06-16 12:06:41 +0000 | [diff] [blame] | 522 | } |