blob: f4a7a978d6604553037dab15b2dcba03184a7597 [file] [log] [blame]
Chris Lattner71c7ec92002-08-30 20:28:10 +00001//===- LoadValueNumbering.cpp - Load Value #'ing Implementation -*- C++ -*-===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner71c7ec92002-08-30 20:28:10 +00009//
Chris Lattner5a6e9472004-03-15 05:44:59 +000010// 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 Lattner71c7ec92002-08-30 20:28:10 +000016//
17// This pass builds off of another value numbering pass to implement value
Chris Lattner5a6e9472004-03-15 05:44:59 +000018// 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 Lattner71c7ec92002-08-30 20:28:10 +000021//
22//===----------------------------------------------------------------------===//
23
24#include "llvm/Analysis/LoadValueNumbering.h"
Chris Lattner5da80972004-04-03 00:45:16 +000025#include "llvm/Constant.h"
Chris Lattner5a6e9472004-03-15 05:44:59 +000026#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000027#include "llvm/Instructions.h"
Chris Lattner5a6e9472004-03-15 05:44:59 +000028#include "llvm/Pass.h"
29#include "llvm/Type.h"
Chris Lattner71c7ec92002-08-30 20:28:10 +000030#include "llvm/Analysis/ValueNumbering.h"
31#include "llvm/Analysis/AliasAnalysis.h"
32#include "llvm/Analysis/Dominators.h"
Chris Lattner71c7ec92002-08-30 20:28:10 +000033#include "llvm/Support/CFG.h"
Chris Lattner5a6e9472004-03-15 05:44:59 +000034#include "llvm/Target/TargetData.h"
Chris Lattner71c7ec92002-08-30 20:28:10 +000035#include <set>
Chris Lattner270db362004-02-05 05:51:40 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner71c7ec92002-08-30 20:28:10 +000038namespace {
Chris Lattner28c6cf22003-06-16 12:06:41 +000039 // FIXME: This should not be a FunctionPass.
Chris Lattner71c7ec92002-08-30 20:28:10 +000040 struct LoadVN : public FunctionPass, public ValueNumbering {
41
42 /// Pass Implementation stuff. This doesn't do any analysis.
43 ///
44 bool runOnFunction(Function &) { return false; }
45
46 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
47 /// and Alias Analysis.
48 ///
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
50
51 /// getEqualNumberNodes - Return nodes with the same value number as the
52 /// specified Value. This fills in the argument vector with any equal
53 /// values.
54 ///
55 virtual void getEqualNumberNodes(Value *V1,
56 std::vector<Value*> &RetVals) const;
Chris Lattner5a6e9472004-03-15 05:44:59 +000057
Chris Lattner0f312d62004-05-23 21:13:24 +000058 /// deleteValue - This method should be called whenever an LLVM Value is
59 /// deleted from the program, for example when an instruction is found to be
60 /// redundant and is eliminated.
61 ///
62 virtual void deleteValue(Value *V) {
63 getAnalysis<AliasAnalysis>().deleteValue(V);
64 }
65
66 /// copyValue - This method should be used whenever a preexisting value in
67 /// the program is copied or cloned, introducing a new value. Note that
68 /// analysis implementations should tolerate clients that use this method to
69 /// introduce the same value multiple times: if the analysis already knows
70 /// about a value, it should ignore the request.
71 ///
72 virtual void copyValue(Value *From, Value *To) {
73 getAnalysis<AliasAnalysis>().copyValue(From, To);
74 }
75
Chris Lattner5a6e9472004-03-15 05:44:59 +000076 /// getCallEqualNumberNodes - Given a call instruction, find other calls
77 /// that have the same value number.
78 void getCallEqualNumberNodes(CallInst *CI,
79 std::vector<Value*> &RetVals) const;
Chris Lattner71c7ec92002-08-30 20:28:10 +000080 };
81
82 // Register this pass...
83 RegisterOpt<LoadVN> X("load-vn", "Load Value Numbering");
84
85 // Declare that we implement the ValueNumbering interface
86 RegisterAnalysisGroup<ValueNumbering, LoadVN> Y;
87}
88
Brian Gaeke96d4bf72004-07-27 17:43:21 +000089FunctionPass *llvm::createLoadValueNumberingPass() { return new LoadVN(); }
Chris Lattner71c7ec92002-08-30 20:28:10 +000090
91
92/// getAnalysisUsage - Does not modify anything. It uses Value Numbering and
93/// Alias Analysis.
94///
95void LoadVN::getAnalysisUsage(AnalysisUsage &AU) const {
96 AU.setPreservesAll();
97 AU.addRequired<AliasAnalysis>();
98 AU.addRequired<ValueNumbering>();
99 AU.addRequired<DominatorSet>();
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000100 AU.addRequired<TargetData>();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000101}
102
Chris Lattner3b303d92004-02-05 17:20:00 +0000103static bool isPathTransparentTo(BasicBlock *CurBlock, BasicBlock *Dom,
104 Value *Ptr, unsigned Size, AliasAnalysis &AA,
105 std::set<BasicBlock*> &Visited,
106 std::map<BasicBlock*, bool> &TransparentBlocks){
107 // If we have already checked out this path, or if we reached our destination,
108 // stop searching, returning success.
109 if (CurBlock == Dom || !Visited.insert(CurBlock).second)
110 return true;
111
112 // Check whether this block is known transparent or not.
113 std::map<BasicBlock*, bool>::iterator TBI =
114 TransparentBlocks.lower_bound(CurBlock);
115
116 if (TBI == TransparentBlocks.end() || TBI->first != CurBlock) {
117 // If this basic block can modify the memory location, then the path is not
118 // transparent!
119 if (AA.canBasicBlockModify(*CurBlock, Ptr, Size)) {
120 TransparentBlocks.insert(TBI, std::make_pair(CurBlock, false));
121 return false;
122 }
Chris Lattner0f312d62004-05-23 21:13:24 +0000123 TransparentBlocks.insert(TBI, std::make_pair(CurBlock, true));
Chris Lattner3b303d92004-02-05 17:20:00 +0000124 } else if (!TBI->second)
125 // This block is known non-transparent, so that path can't be either.
126 return false;
127
128 // The current block is known to be transparent. The entire path is
129 // transparent if all of the predecessors paths to the parent is also
130 // transparent to the memory location.
131 for (pred_iterator PI = pred_begin(CurBlock), E = pred_end(CurBlock);
132 PI != E; ++PI)
133 if (!isPathTransparentTo(*PI, Dom, Ptr, Size, AA, Visited,
134 TransparentBlocks))
135 return false;
136 return true;
137}
138
Chris Lattner5a6e9472004-03-15 05:44:59 +0000139/// getCallEqualNumberNodes - Given a call instruction, find other calls that
140/// have the same value number.
141void LoadVN::getCallEqualNumberNodes(CallInst *CI,
142 std::vector<Value*> &RetVals) const {
143 Function *CF = CI->getCalledFunction();
144 if (CF == 0) return; // Indirect call.
145 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
146 if (!AA.onlyReadsMemory(CF)) return; // Nothing we can do.
147
148 // Scan all of the arguments of the function, looking for one that is not
149 // global. In particular, we would prefer to have an argument or instruction
150 // operand to chase the def-use chains of.
151 Value *Op = CF;
152 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
153 if (isa<Argument>(CI->getOperand(i)) ||
154 isa<Instruction>(CI->getOperand(i))) {
155 Op = CI->getOperand(i);
156 break;
157 }
158
159 // Identify all lexically identical calls in this function.
160 std::vector<CallInst*> IdenticalCalls;
161
162 Function *CIFunc = CI->getParent()->getParent();
163 for (Value::use_iterator UI = Op->use_begin(), E = Op->use_end(); UI != E;
164 ++UI)
165 if (CallInst *C = dyn_cast<CallInst>(*UI))
166 if (C->getNumOperands() == CI->getNumOperands() &&
167 C->getOperand(0) == CI->getOperand(0) &&
168 C->getParent()->getParent() == CIFunc && C != CI) {
169 bool AllOperandsEqual = true;
170 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
171 if (C->getOperand(i) != CI->getOperand(i)) {
172 AllOperandsEqual = false;
173 break;
174 }
175
176 if (AllOperandsEqual)
177 IdenticalCalls.push_back(C);
178 }
179
180 if (IdenticalCalls.empty()) return;
181
182 // Eliminate duplicates, which could occur if we chose a value that is passed
183 // into a call site multiple times.
184 std::sort(IdenticalCalls.begin(), IdenticalCalls.end());
185 IdenticalCalls.erase(std::unique(IdenticalCalls.begin(),IdenticalCalls.end()),
186 IdenticalCalls.end());
187
188 // If the call reads memory, we must make sure that there are no stores
189 // between the calls in question.
190 //
191 // FIXME: This should use mod/ref information. What we really care about it
192 // whether an intervening instruction could modify memory that is read, not
193 // ANY memory.
194 //
195 if (!AA.doesNotAccessMemory(CF)) {
196 DominatorSet &DomSetInfo = getAnalysis<DominatorSet>();
197 BasicBlock *CIBB = CI->getParent();
198 for (unsigned i = 0; i != IdenticalCalls.size(); ++i) {
199 CallInst *C = IdenticalCalls[i];
200 bool CantEqual = false;
201
202 if (DomSetInfo.dominates(CIBB, C->getParent())) {
203 // FIXME: we currently only handle the case where both calls are in the
204 // same basic block.
205 if (CIBB != C->getParent()) {
206 CantEqual = true;
207 } else {
208 Instruction *First = CI, *Second = C;
209 if (!DomSetInfo.dominates(CI, C))
210 std::swap(First, Second);
211
212 // Scan the instructions between the calls, checking for stores or
213 // calls to dangerous functions.
214 BasicBlock::iterator I = First;
215 for (++First; I != BasicBlock::iterator(Second); ++I) {
216 if (isa<StoreInst>(I)) {
217 // FIXME: We could use mod/ref information to make this much
218 // better!
219 CantEqual = true;
220 break;
221 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
222 if (CI->getCalledFunction() == 0 ||
223 !AA.onlyReadsMemory(CI->getCalledFunction())) {
224 CantEqual = true;
225 break;
226 }
227 } else if (I->mayWriteToMemory()) {
228 CantEqual = true;
229 break;
230 }
231 }
232 }
233
234 } else if (DomSetInfo.dominates(C->getParent(), CIBB)) {
235 // FIXME: We could implement this, but we don't for now.
236 CantEqual = true;
237 } else {
238 // FIXME: if one doesn't dominate the other, we can't tell yet.
239 CantEqual = true;
240 }
241
242
243 if (CantEqual) {
244 // This call does not produce the same value as the one in the query.
245 std::swap(IdenticalCalls[i--], IdenticalCalls.back());
246 IdenticalCalls.pop_back();
247 }
248 }
249 }
250
251 // Any calls that are identical and not destroyed will produce equal values!
252 for (unsigned i = 0, e = IdenticalCalls.size(); i != e; ++i)
253 RetVals.push_back(IdenticalCalls[i]);
254}
Chris Lattner3b303d92004-02-05 17:20:00 +0000255
Chris Lattner71c7ec92002-08-30 20:28:10 +0000256// getEqualNumberNodes - Return nodes with the same value number as the
257// specified Value. This fills in the argument vector with any equal values.
258//
259void LoadVN::getEqualNumberNodes(Value *V,
260 std::vector<Value*> &RetVals) const {
Chris Lattneraed2c6d2003-06-29 00:53:34 +0000261 // If the alias analysis has any must alias information to share with us, we
Misha Brukman7bc439a2003-09-11 15:31:17 +0000262 // can definitely use it.
Chris Lattneraed2c6d2003-06-29 00:53:34 +0000263 if (isa<PointerType>(V->getType()))
264 getAnalysis<AliasAnalysis>().getMustAliases(V, RetVals);
Chris Lattner71c7ec92002-08-30 20:28:10 +0000265
Chris Lattner57ef9a22004-02-05 05:56:23 +0000266 if (!isa<LoadInst>(V)) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000267 if (CallInst *CI = dyn_cast<CallInst>(V))
Chris Lattner002be762004-03-16 03:41:35 +0000268 getCallEqualNumberNodes(CI, RetVals);
Chris Lattner5a6e9472004-03-15 05:44:59 +0000269
Chris Lattner57ef9a22004-02-05 05:56:23 +0000270 // Not a load instruction? Just chain to the base value numbering
271 // implementation to satisfy the request...
Chris Lattner71c7ec92002-08-30 20:28:10 +0000272 assert(&getAnalysis<ValueNumbering>() != (ValueNumbering*)this &&
273 "getAnalysis() returned this!");
274
Chris Lattner71c7ec92002-08-30 20:28:10 +0000275 return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
276 }
Chris Lattner57ef9a22004-02-05 05:56:23 +0000277
278 // Volatile loads cannot be replaced with the value of other loads.
279 LoadInst *LI = cast<LoadInst>(V);
280 if (LI->isVolatile())
281 return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
282
283 // If we have a load instruction, find all of the load and store instructions
284 // that use the same source operand. We implement this recursively, because
285 // there could be a load of a load of a load that are all identical. We are
286 // guaranteed that this cannot be an infinite recursion because load
287 // instructions would have to pass through a PHI node in order for there to be
288 // a cycle. The PHI node would be handled by the else case here, breaking the
289 // infinite recursion.
290 //
291 std::vector<Value*> PointerSources;
292 getEqualNumberNodes(LI->getOperand(0), PointerSources);
293 PointerSources.push_back(LI->getOperand(0));
294
Chris Lattner3b303d92004-02-05 17:20:00 +0000295 BasicBlock *LoadBB = LI->getParent();
296 Function *F = LoadBB->getParent();
Chris Lattner57ef9a22004-02-05 05:56:23 +0000297
298 // Now that we know the set of equivalent source pointers for the load
299 // instruction, look to see if there are any load or store candidates that are
300 // identical.
301 //
Chris Lattner3b303d92004-02-05 17:20:00 +0000302 std::map<BasicBlock*, std::vector<LoadInst*> > CandidateLoads;
303 std::map<BasicBlock*, std::vector<StoreInst*> > CandidateStores;
Chris Lattner5da80972004-04-03 00:45:16 +0000304 std::set<AllocationInst*> Allocations;
Chris Lattner57ef9a22004-02-05 05:56:23 +0000305
306 while (!PointerSources.empty()) {
307 Value *Source = PointerSources.back();
308 PointerSources.pop_back(); // Get a source pointer...
Chris Lattner5da80972004-04-03 00:45:16 +0000309
310 if (AllocationInst *AI = dyn_cast<AllocationInst>(Source))
311 Allocations.insert(AI);
Chris Lattner57ef9a22004-02-05 05:56:23 +0000312
313 for (Value::use_iterator UI = Source->use_begin(), UE = Source->use_end();
314 UI != UE; ++UI)
315 if (LoadInst *Cand = dyn_cast<LoadInst>(*UI)) {// Is a load of source?
316 if (Cand->getParent()->getParent() == F && // In the same function?
317 Cand != LI && !Cand->isVolatile()) // Not LI itself?
Chris Lattner3b303d92004-02-05 17:20:00 +0000318 CandidateLoads[Cand->getParent()].push_back(Cand); // Got one...
Chris Lattner57ef9a22004-02-05 05:56:23 +0000319 } else if (StoreInst *Cand = dyn_cast<StoreInst>(*UI)) {
320 if (Cand->getParent()->getParent() == F && !Cand->isVolatile() &&
321 Cand->getOperand(1) == Source) // It's a store THROUGH the ptr...
Chris Lattner3b303d92004-02-05 17:20:00 +0000322 CandidateStores[Cand->getParent()].push_back(Cand);
Chris Lattner57ef9a22004-02-05 05:56:23 +0000323 }
324 }
325
Chris Lattner3b303d92004-02-05 17:20:00 +0000326 // Get alias analysis & dominators.
Chris Lattner57ef9a22004-02-05 05:56:23 +0000327 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
328 DominatorSet &DomSetInfo = getAnalysis<DominatorSet>();
Chris Lattner3b303d92004-02-05 17:20:00 +0000329 Value *LoadPtr = LI->getOperand(0);
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000330 // Find out how many bytes of memory are loaded by the load instruction...
Chris Lattner3b303d92004-02-05 17:20:00 +0000331 unsigned LoadSize = getAnalysis<TargetData>().getTypeSize(LI->getType());
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000332
Chris Lattner3b303d92004-02-05 17:20:00 +0000333 // Find all of the candidate loads and stores that are in the same block as
334 // the defining instruction.
335 std::set<Instruction*> Instrs;
336 Instrs.insert(CandidateLoads[LoadBB].begin(), CandidateLoads[LoadBB].end());
337 CandidateLoads.erase(LoadBB);
338 Instrs.insert(CandidateStores[LoadBB].begin(), CandidateStores[LoadBB].end());
339 CandidateStores.erase(LoadBB);
Chris Lattner71c7ec92002-08-30 20:28:10 +0000340
Chris Lattner3b303d92004-02-05 17:20:00 +0000341 // Figure out if the load is invalidated from the entry of the block it is in
342 // until the actual instruction. This scans the block backwards from LI. If
343 // we see any candidate load or store instructions, then we know that the
344 // candidates have the same value # as LI.
345 bool LoadInvalidatedInBBBefore = false;
346 for (BasicBlock::iterator I = LI; I != LoadBB->begin(); ) {
347 --I;
348 // If this instruction is a candidate load before LI, we know there are no
349 // invalidating instructions between it and LI, so they have the same value
350 // number.
351 if (isa<LoadInst>(I) && Instrs.count(I)) {
352 RetVals.push_back(I);
353 Instrs.erase(I);
Chris Lattner5da80972004-04-03 00:45:16 +0000354 } else if (AllocationInst *AI = dyn_cast<AllocationInst>(I)) {
355 // If we run into an allocation of the value being loaded, then the
356 // contenxt are not initialized. We can return any value, so we will
357 // return a zero.
358 if (Allocations.count(AI)) {
359 LoadInvalidatedInBBBefore = true;
360 RetVals.push_back(Constant::getNullValue(LI->getType()));
361 break;
362 }
Chris Lattneradf9b902004-02-05 00:36:43 +0000363 }
364
Chris Lattner3b303d92004-02-05 17:20:00 +0000365 if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
366 // If the invalidating instruction is a store, and its in our candidate
367 // set, then we can do store-load forwarding: the load has the same value
368 // # as the stored value.
369 if (isa<StoreInst>(I) && Instrs.count(I)) {
370 Instrs.erase(I);
371 RetVals.push_back(I->getOperand(0));
Chris Lattneradf9b902004-02-05 00:36:43 +0000372 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000373
374 LoadInvalidatedInBBBefore = true;
375 break;
Chris Lattneradf9b902004-02-05 00:36:43 +0000376 }
Chris Lattner71c7ec92002-08-30 20:28:10 +0000377 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000378
Chris Lattner3b303d92004-02-05 17:20:00 +0000379 // Figure out if the load is invalidated between the load and the exit of the
380 // block it is defined in. While we are scanning the current basic block, if
381 // we see any candidate loads, then we know they have the same value # as LI.
Chris Lattner28c6cf22003-06-16 12:06:41 +0000382 //
Chris Lattner3b303d92004-02-05 17:20:00 +0000383 bool LoadInvalidatedInBBAfter = false;
384 for (BasicBlock::iterator I = LI->getNext(); I != LoadBB->end(); ++I) {
385 // If this instruction is a load, then this instruction returns the same
386 // value as LI.
387 if (isa<LoadInst>(I) && Instrs.count(I)) {
388 RetVals.push_back(I);
389 Instrs.erase(I);
390 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000391
Chris Lattner3b303d92004-02-05 17:20:00 +0000392 if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
393 LoadInvalidatedInBBAfter = true;
394 break;
395 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000396 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000397
398 // If there is anything left in the Instrs set, it could not possibly equal
399 // LI.
400 Instrs.clear();
401
402 // TransparentBlocks - For each basic block the load/store is alive across,
403 // figure out if the pointer is invalidated or not. If it is invalidated, the
404 // boolean is set to false, if it's not it is set to true. If we don't know
405 // yet, the entry is not in the map.
406 std::map<BasicBlock*, bool> TransparentBlocks;
407
408 // Loop over all of the basic blocks that also load the value. If the value
409 // is live across the CFG from the source to destination blocks, and if the
410 // value is not invalidated in either the source or destination blocks, add it
411 // to the equivalence sets.
412 for (std::map<BasicBlock*, std::vector<LoadInst*> >::iterator
413 I = CandidateLoads.begin(), E = CandidateLoads.end(); I != E; ++I) {
414 bool CantEqual = false;
415
416 // Right now we only can handle cases where one load dominates the other.
417 // FIXME: generalize this!
418 BasicBlock *BB1 = I->first, *BB2 = LoadBB;
419 if (DomSetInfo.dominates(BB1, BB2)) {
420 // The other load dominates LI. If the loaded value is killed entering
421 // the LoadBB block, we know the load is not live.
422 if (LoadInvalidatedInBBBefore)
423 CantEqual = true;
424 } else if (DomSetInfo.dominates(BB2, BB1)) {
425 std::swap(BB1, BB2); // Canonicalize
426 // LI dominates the other load. If the loaded value is killed exiting
427 // the LoadBB block, we know the load is not live.
428 if (LoadInvalidatedInBBAfter)
429 CantEqual = true;
430 } else {
431 // None of these loads can VN the same.
432 CantEqual = true;
433 }
434
435 if (!CantEqual) {
436 // Ok, at this point, we know that BB1 dominates BB2, and that there is
437 // nothing in the LI block that kills the loaded value. Check to see if
438 // the value is live across the CFG.
439 std::set<BasicBlock*> Visited;
440 for (pred_iterator PI = pred_begin(BB2), E = pred_end(BB2); PI!=E; ++PI)
441 if (!isPathTransparentTo(*PI, BB1, LoadPtr, LoadSize, AA,
442 Visited, TransparentBlocks)) {
443 // None of these loads can VN the same.
444 CantEqual = true;
445 break;
446 }
447 }
448
449 // If the loads can equal so far, scan the basic block that contains the
450 // loads under consideration to see if they are invalidated in the block.
451 // For any loads that are not invalidated, add them to the equivalence
452 // set!
453 if (!CantEqual) {
454 Instrs.insert(I->second.begin(), I->second.end());
455 if (BB1 == LoadBB) {
456 // If LI dominates the block in question, check to see if any of the
457 // loads in this block are invalidated before they are reached.
458 for (BasicBlock::iterator BBI = I->first->begin(); ; ++BBI) {
459 if (isa<LoadInst>(BBI) && Instrs.count(BBI)) {
460 // The load is in the set!
461 RetVals.push_back(BBI);
462 Instrs.erase(BBI);
463 if (Instrs.empty()) break;
464 } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize)
465 & AliasAnalysis::Mod) {
466 // If there is a modifying instruction, nothing below it will value
467 // # the same.
468 break;
469 }
470 }
471 } else {
472 // If the block dominates LI, make sure that the loads in the block are
473 // not invalidated before the block ends.
474 BasicBlock::iterator BBI = I->first->end();
475 while (1) {
476 --BBI;
477 if (isa<LoadInst>(BBI) && Instrs.count(BBI)) {
478 // The load is in the set!
479 RetVals.push_back(BBI);
480 Instrs.erase(BBI);
481 if (Instrs.empty()) break;
482 } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize)
483 & AliasAnalysis::Mod) {
484 // If there is a modifying instruction, nothing above it will value
485 // # the same.
486 break;
487 }
488 }
489 }
490
491 Instrs.clear();
492 }
493 }
494
495 // Handle candidate stores. If the loaded location is clobbered on entrance
496 // to the LoadBB, no store outside of the LoadBB can value number equal, so
497 // quick exit.
498 if (LoadInvalidatedInBBBefore)
499 return;
500
501 for (std::map<BasicBlock*, std::vector<StoreInst*> >::iterator
502 I = CandidateStores.begin(), E = CandidateStores.end(); I != E; ++I)
503 if (DomSetInfo.dominates(I->first, LoadBB)) {
504 // Check to see if the path from the store to the load is transparent
505 // w.r.t. the memory location.
506 bool CantEqual = false;
507 std::set<BasicBlock*> Visited;
508 for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
509 PI != E; ++PI)
510 if (!isPathTransparentTo(*PI, I->first, LoadPtr, LoadSize, AA,
511 Visited, TransparentBlocks)) {
512 // None of these stores can VN the same.
513 CantEqual = true;
514 break;
515 }
516 Visited.clear();
517 if (!CantEqual) {
518 // Okay, the path from the store block to the load block is clear, and
519 // we know that there are no invalidating instructions from the start
520 // of the load block to the load itself. Now we just scan the store
521 // block.
522
523 BasicBlock::iterator BBI = I->first->end();
524 while (1) {
Chris Lattner0f312d62004-05-23 21:13:24 +0000525 assert(BBI != I->first->begin() &&
526 "There is a store in this block of the pointer, but the store"
527 " doesn't mod the address being stored to?? Must be a bug in"
528 " the alias analysis implementation!");
Chris Lattner3b303d92004-02-05 17:20:00 +0000529 --BBI;
Chris Lattner0f312d62004-05-23 21:13:24 +0000530 if (AA.getModRefInfo(BBI, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000531 // If the invalidating instruction is one of the candidates,
532 // then it provides the value the load loads.
533 if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
534 if (std::find(I->second.begin(), I->second.end(), SI) !=
535 I->second.end())
536 RetVals.push_back(SI->getOperand(0));
537 break;
538 }
539 }
540 }
541 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000542}