blob: 6dd55a4a35fba1d48ff087991ced8e80283ed3be [file] [log] [blame]
Chris Lattner71c7ec92002-08-30 20:28:10 +00001//===- LoadValueNumbering.cpp - Load Value #'ing Implementation -*- C++ -*-===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner2652da62005-01-29 05:57:01 +000025#include "llvm/Constants.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"
Reid Spencerd7d83db2007-02-05 23:42:17 +000034#include "llvm/Support/Compiler.h"
Chris Lattner5a6e9472004-03-15 05:44:59 +000035#include "llvm/Target/TargetData.h"
Chris Lattner71c7ec92002-08-30 20:28:10 +000036#include <set>
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000037#include <algorithm>
Chris Lattner270db362004-02-05 05:51:40 +000038using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000039
Chris Lattner71c7ec92002-08-30 20:28:10 +000040namespace {
Chris Lattner28c6cf22003-06-16 12:06:41 +000041 // FIXME: This should not be a FunctionPass.
Reid Spencerd7d83db2007-02-05 23:42:17 +000042 struct VISIBILITY_HIDDEN LoadVN : public FunctionPass, public ValueNumbering {
Devang Patel19974732007-05-03 01:11:54 +000043 static char ID; // Class identification, replacement for typeinfo
Devang Patel794fd752007-05-01 21:15:47 +000044 LoadVN() : FunctionPass((intptr_t)&ID) {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +000045
Devang Patel1cee94f2008-03-18 00:39:19 +000046 /// isAnalysis - Return true if this pass is implementing an analysis pass.
47 virtual bool isAnalysis() const { return true; }
48
Chris Lattner71c7ec92002-08-30 20:28:10 +000049 /// Pass Implementation stuff. This doesn't do any analysis.
50 ///
51 bool runOnFunction(Function &) { return false; }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000052
Chris Lattner71c7ec92002-08-30 20:28:10 +000053 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
54 /// and Alias Analysis.
55 ///
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Misha Brukman2b37d7c2005-04-21 21:13:18 +000057
Chris Lattner71c7ec92002-08-30 20:28:10 +000058 /// getEqualNumberNodes - Return nodes with the same value number as the
59 /// specified Value. This fills in the argument vector with any equal
60 /// values.
61 ///
62 virtual void getEqualNumberNodes(Value *V1,
63 std::vector<Value*> &RetVals) const;
Chris Lattner5a6e9472004-03-15 05:44:59 +000064
Chris Lattner0f312d62004-05-23 21:13:24 +000065 /// deleteValue - This method should be called whenever an LLVM Value is
66 /// deleted from the program, for example when an instruction is found to be
67 /// redundant and is eliminated.
68 ///
69 virtual void deleteValue(Value *V) {
70 getAnalysis<AliasAnalysis>().deleteValue(V);
71 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000072
Chris Lattner0f312d62004-05-23 21:13:24 +000073 /// copyValue - This method should be used whenever a preexisting value in
74 /// the program is copied or cloned, introducing a new value. Note that
75 /// analysis implementations should tolerate clients that use this method to
76 /// introduce the same value multiple times: if the analysis already knows
77 /// about a value, it should ignore the request.
78 ///
79 virtual void copyValue(Value *From, Value *To) {
80 getAnalysis<AliasAnalysis>().copyValue(From, To);
81 }
82
Chris Lattner5a6e9472004-03-15 05:44:59 +000083 /// getCallEqualNumberNodes - Given a call instruction, find other calls
84 /// that have the same value number.
85 void getCallEqualNumberNodes(CallInst *CI,
86 std::vector<Value*> &RetVals) const;
Chris Lattner71c7ec92002-08-30 20:28:10 +000087 };
88
Devang Patel19974732007-05-03 01:11:54 +000089 char LoadVN::ID = 0;
Chris Lattner71c7ec92002-08-30 20:28:10 +000090 // Register this pass...
Chris Lattner7f8897f2006-08-27 22:42:52 +000091 RegisterPass<LoadVN> X("load-vn", "Load Value Numbering");
Chris Lattner71c7ec92002-08-30 20:28:10 +000092
93 // Declare that we implement the ValueNumbering interface
Chris Lattnera5370172006-08-28 00:42:29 +000094 RegisterAnalysisGroup<ValueNumbering> Y(X);
Chris Lattner71c7ec92002-08-30 20:28:10 +000095}
96
Brian Gaeke96d4bf72004-07-27 17:43:21 +000097FunctionPass *llvm::createLoadValueNumberingPass() { return new LoadVN(); }
Chris Lattner71c7ec92002-08-30 20:28:10 +000098
99
100/// getAnalysisUsage - Does not modify anything. It uses Value Numbering and
101/// Alias Analysis.
102///
103void LoadVN::getAnalysisUsage(AnalysisUsage &AU) const {
104 AU.setPreservesAll();
Chris Lattner10673b62006-01-08 09:10:04 +0000105 AU.addRequiredTransitive<AliasAnalysis>();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000106 AU.addRequired<ValueNumbering>();
Devang Patel6ea7ac62007-06-07 23:53:38 +0000107 AU.addRequiredTransitive<DominatorTree>();
Chris Lattner10673b62006-01-08 09:10:04 +0000108 AU.addRequiredTransitive<TargetData>();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000109}
110
Chris Lattner3b303d92004-02-05 17:20:00 +0000111static bool isPathTransparentTo(BasicBlock *CurBlock, BasicBlock *Dom,
112 Value *Ptr, unsigned Size, AliasAnalysis &AA,
113 std::set<BasicBlock*> &Visited,
114 std::map<BasicBlock*, bool> &TransparentBlocks){
115 // If we have already checked out this path, or if we reached our destination,
116 // stop searching, returning success.
117 if (CurBlock == Dom || !Visited.insert(CurBlock).second)
118 return true;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000119
Chris Lattner3b303d92004-02-05 17:20:00 +0000120 // Check whether this block is known transparent or not.
121 std::map<BasicBlock*, bool>::iterator TBI =
122 TransparentBlocks.lower_bound(CurBlock);
123
124 if (TBI == TransparentBlocks.end() || TBI->first != CurBlock) {
125 // If this basic block can modify the memory location, then the path is not
126 // transparent!
127 if (AA.canBasicBlockModify(*CurBlock, Ptr, Size)) {
128 TransparentBlocks.insert(TBI, std::make_pair(CurBlock, false));
129 return false;
130 }
Chris Lattner0f312d62004-05-23 21:13:24 +0000131 TransparentBlocks.insert(TBI, std::make_pair(CurBlock, true));
Chris Lattner3b303d92004-02-05 17:20:00 +0000132 } else if (!TBI->second)
133 // This block is known non-transparent, so that path can't be either.
134 return false;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000135
Chris Lattner3b303d92004-02-05 17:20:00 +0000136 // The current block is known to be transparent. The entire path is
137 // transparent if all of the predecessors paths to the parent is also
138 // transparent to the memory location.
139 for (pred_iterator PI = pred_begin(CurBlock), E = pred_end(CurBlock);
140 PI != E; ++PI)
141 if (!isPathTransparentTo(*PI, Dom, Ptr, Size, AA, Visited,
142 TransparentBlocks))
143 return false;
144 return true;
145}
146
Chris Lattner5a6e9472004-03-15 05:44:59 +0000147/// getCallEqualNumberNodes - Given a call instruction, find other calls that
148/// have the same value number.
149void LoadVN::getCallEqualNumberNodes(CallInst *CI,
150 std::vector<Value*> &RetVals) const {
151 Function *CF = CI->getCalledFunction();
152 if (CF == 0) return; // Indirect call.
153 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Duncan Sandsdff67102007-12-01 07:51:45 +0000154 AliasAnalysis::ModRefBehavior MRB = AA.getModRefBehavior(CI);
Chris Lattnerfd4b3c42004-12-15 18:14:04 +0000155 if (MRB != AliasAnalysis::DoesNotAccessMemory &&
156 MRB != AliasAnalysis::OnlyReadsMemory)
157 return; // Nothing we can do for now.
Chris Lattner5a6e9472004-03-15 05:44:59 +0000158
159 // Scan all of the arguments of the function, looking for one that is not
160 // global. In particular, we would prefer to have an argument or instruction
161 // operand to chase the def-use chains of.
162 Value *Op = CF;
163 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
164 if (isa<Argument>(CI->getOperand(i)) ||
165 isa<Instruction>(CI->getOperand(i))) {
166 Op = CI->getOperand(i);
167 break;
168 }
169
170 // Identify all lexically identical calls in this function.
171 std::vector<CallInst*> IdenticalCalls;
172
173 Function *CIFunc = CI->getParent()->getParent();
174 for (Value::use_iterator UI = Op->use_begin(), E = Op->use_end(); UI != E;
175 ++UI)
176 if (CallInst *C = dyn_cast<CallInst>(*UI))
177 if (C->getNumOperands() == CI->getNumOperands() &&
178 C->getOperand(0) == CI->getOperand(0) &&
179 C->getParent()->getParent() == CIFunc && C != CI) {
180 bool AllOperandsEqual = true;
181 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
182 if (C->getOperand(i) != CI->getOperand(i)) {
183 AllOperandsEqual = false;
184 break;
185 }
186
187 if (AllOperandsEqual)
188 IdenticalCalls.push_back(C);
189 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000190
Chris Lattner5a6e9472004-03-15 05:44:59 +0000191 if (IdenticalCalls.empty()) return;
192
193 // Eliminate duplicates, which could occur if we chose a value that is passed
194 // into a call site multiple times.
195 std::sort(IdenticalCalls.begin(), IdenticalCalls.end());
196 IdenticalCalls.erase(std::unique(IdenticalCalls.begin(),IdenticalCalls.end()),
197 IdenticalCalls.end());
198
199 // If the call reads memory, we must make sure that there are no stores
200 // between the calls in question.
201 //
202 // FIXME: This should use mod/ref information. What we really care about it
203 // whether an intervening instruction could modify memory that is read, not
204 // ANY memory.
205 //
Chris Lattnerfd4b3c42004-12-15 18:14:04 +0000206 if (MRB == AliasAnalysis::OnlyReadsMemory) {
Devang Patel6ea7ac62007-06-07 23:53:38 +0000207 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattner5a6e9472004-03-15 05:44:59 +0000208 BasicBlock *CIBB = CI->getParent();
209 for (unsigned i = 0; i != IdenticalCalls.size(); ++i) {
210 CallInst *C = IdenticalCalls[i];
211 bool CantEqual = false;
212
Devang Patel6ea7ac62007-06-07 23:53:38 +0000213 if (DT.dominates(CIBB, C->getParent())) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000214 // FIXME: we currently only handle the case where both calls are in the
215 // same basic block.
216 if (CIBB != C->getParent()) {
217 CantEqual = true;
218 } else {
219 Instruction *First = CI, *Second = C;
Devang Patel6ea7ac62007-06-07 23:53:38 +0000220 if (!DT.dominates(CI, C))
Chris Lattner5a6e9472004-03-15 05:44:59 +0000221 std::swap(First, Second);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000222
Chris Lattner5a6e9472004-03-15 05:44:59 +0000223 // Scan the instructions between the calls, checking for stores or
224 // calls to dangerous functions.
225 BasicBlock::iterator I = First;
226 for (++First; I != BasicBlock::iterator(Second); ++I) {
227 if (isa<StoreInst>(I)) {
228 // FIXME: We could use mod/ref information to make this much
229 // better!
230 CantEqual = true;
231 break;
232 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000233 if (!AA.onlyReadsMemory(CI)) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000234 CantEqual = true;
235 break;
236 }
237 } else if (I->mayWriteToMemory()) {
238 CantEqual = true;
239 break;
240 }
241 }
242 }
243
Devang Patel6ea7ac62007-06-07 23:53:38 +0000244 } else if (DT.dominates(C->getParent(), CIBB)) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000245 // FIXME: We could implement this, but we don't for now.
246 CantEqual = true;
247 } else {
248 // FIXME: if one doesn't dominate the other, we can't tell yet.
249 CantEqual = true;
250 }
251
252
253 if (CantEqual) {
254 // This call does not produce the same value as the one in the query.
255 std::swap(IdenticalCalls[i--], IdenticalCalls.back());
256 IdenticalCalls.pop_back();
257 }
258 }
259 }
260
261 // Any calls that are identical and not destroyed will produce equal values!
262 for (unsigned i = 0, e = IdenticalCalls.size(); i != e; ++i)
263 RetVals.push_back(IdenticalCalls[i]);
264}
Chris Lattner3b303d92004-02-05 17:20:00 +0000265
Chris Lattner71c7ec92002-08-30 20:28:10 +0000266// getEqualNumberNodes - Return nodes with the same value number as the
267// specified Value. This fills in the argument vector with any equal values.
268//
269void LoadVN::getEqualNumberNodes(Value *V,
270 std::vector<Value*> &RetVals) const {
Chris Lattneraed2c6d2003-06-29 00:53:34 +0000271 // If the alias analysis has any must alias information to share with us, we
Misha Brukman7bc439a2003-09-11 15:31:17 +0000272 // can definitely use it.
Chris Lattneraed2c6d2003-06-29 00:53:34 +0000273 if (isa<PointerType>(V->getType()))
274 getAnalysis<AliasAnalysis>().getMustAliases(V, RetVals);
Chris Lattner71c7ec92002-08-30 20:28:10 +0000275
Chris Lattner57ef9a22004-02-05 05:56:23 +0000276 if (!isa<LoadInst>(V)) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000277 if (CallInst *CI = dyn_cast<CallInst>(V))
Chris Lattner002be762004-03-16 03:41:35 +0000278 getCallEqualNumberNodes(CI, RetVals);
Chris Lattner5a6e9472004-03-15 05:44:59 +0000279
Chris Lattner57ef9a22004-02-05 05:56:23 +0000280 // Not a load instruction? Just chain to the base value numbering
281 // implementation to satisfy the request...
Chris Lattner71c7ec92002-08-30 20:28:10 +0000282 assert(&getAnalysis<ValueNumbering>() != (ValueNumbering*)this &&
283 "getAnalysis() returned this!");
284
Chris Lattner71c7ec92002-08-30 20:28:10 +0000285 return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
286 }
Chris Lattner57ef9a22004-02-05 05:56:23 +0000287
288 // Volatile loads cannot be replaced with the value of other loads.
289 LoadInst *LI = cast<LoadInst>(V);
290 if (LI->isVolatile())
291 return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000292
Chris Lattner15774df2005-01-29 06:31:53 +0000293 Value *LoadPtr = LI->getOperand(0);
Chris Lattner3b303d92004-02-05 17:20:00 +0000294 BasicBlock *LoadBB = LI->getParent();
295 Function *F = LoadBB->getParent();
Chris Lattner15774df2005-01-29 06:31:53 +0000296
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000297 // Find out how many bytes of memory are loaded by the load instruction...
Duncan Sands514ab342007-11-01 20:53:16 +0000298 unsigned LoadSize = getAnalysis<TargetData>().getTypeStoreSize(LI->getType());
Chris Lattner15774df2005-01-29 06:31:53 +0000299 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000300
Chris Lattner3b303d92004-02-05 17:20:00 +0000301 // Figure out if the load is invalidated from the entry of the block it is in
302 // until the actual instruction. This scans the block backwards from LI. If
303 // we see any candidate load or store instructions, then we know that the
304 // candidates have the same value # as LI.
305 bool LoadInvalidatedInBBBefore = false;
306 for (BasicBlock::iterator I = LI; I != LoadBB->begin(); ) {
307 --I;
Chris Lattneree379a12005-01-29 06:42:34 +0000308 if (I == LoadPtr) {
Chris Lattner5da80972004-04-03 00:45:16 +0000309 // If we run into an allocation of the value being loaded, then the
Chris Lattner2652da62005-01-29 05:57:01 +0000310 // contents are not initialized.
Chris Lattner15774df2005-01-29 06:31:53 +0000311 if (isa<AllocationInst>(I))
Chris Lattner2652da62005-01-29 05:57:01 +0000312 RetVals.push_back(UndefValue::get(LI->getType()));
Chris Lattner15774df2005-01-29 06:31:53 +0000313
314 // Otherwise, since this is the definition of what we are loading, this
315 // loaded value cannot occur before this block.
316 LoadInvalidatedInBBBefore = true;
317 break;
Chris Lattneree379a12005-01-29 06:42:34 +0000318 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
319 // If this instruction is a candidate load before LI, we know there are no
320 // invalidating instructions between it and LI, so they have the same
321 // value number.
322 if (LI->getOperand(0) == LoadPtr && !LI->isVolatile())
323 RetVals.push_back(I);
Chris Lattneradf9b902004-02-05 00:36:43 +0000324 }
Chris Lattner3725c122005-01-29 07:04:10 +0000325
Chris Lattner3b303d92004-02-05 17:20:00 +0000326 if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
327 // If the invalidating instruction is a store, and its in our candidate
328 // set, then we can do store-load forwarding: the load has the same value
329 // # as the stored value.
Chris Lattneree379a12005-01-29 06:42:34 +0000330 if (StoreInst *SI = dyn_cast<StoreInst>(I))
331 if (SI->getOperand(1) == LoadPtr)
332 RetVals.push_back(I->getOperand(0));
Chris Lattner3b303d92004-02-05 17:20:00 +0000333
334 LoadInvalidatedInBBBefore = true;
335 break;
Chris Lattneradf9b902004-02-05 00:36:43 +0000336 }
Chris Lattner71c7ec92002-08-30 20:28:10 +0000337 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000338
Chris Lattner3b303d92004-02-05 17:20:00 +0000339 // Figure out if the load is invalidated between the load and the exit of the
340 // block it is defined in. While we are scanning the current basic block, if
341 // we see any candidate loads, then we know they have the same value # as LI.
Chris Lattner28c6cf22003-06-16 12:06:41 +0000342 //
Chris Lattner3b303d92004-02-05 17:20:00 +0000343 bool LoadInvalidatedInBBAfter = false;
Chris Lattner8e8f8652007-04-17 17:52:45 +0000344 {
345 BasicBlock::iterator I = LI;
346 for (++I; I != LoadBB->end(); ++I) {
347 // If this instruction is a load, then this instruction returns the same
348 // value as LI.
349 if (isa<LoadInst>(I) && cast<LoadInst>(I)->getOperand(0) == LoadPtr)
350 RetVals.push_back(I);
Chris Lattner28c6cf22003-06-16 12:06:41 +0000351
Chris Lattner8e8f8652007-04-17 17:52:45 +0000352 if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
353 LoadInvalidatedInBBAfter = true;
354 break;
355 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000356 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000357 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000358
Chris Lattner15774df2005-01-29 06:31:53 +0000359 // If the pointer is clobbered on entry and on exit to the function, there is
360 // no need to do any global analysis at all.
361 if (LoadInvalidatedInBBBefore && LoadInvalidatedInBBAfter)
362 return;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000363
Chris Lattner3725c122005-01-29 07:04:10 +0000364 // Now that we know the value is not neccesarily killed on entry or exit to
365 // the BB, find out how many load and store instructions (to this location)
366 // live in each BB in the function.
Chris Lattner15774df2005-01-29 06:31:53 +0000367 //
Chris Lattner3725c122005-01-29 07:04:10 +0000368 std::map<BasicBlock*, unsigned> CandidateLoads;
369 std::set<BasicBlock*> CandidateStores;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000370
Chris Lattner15774df2005-01-29 06:31:53 +0000371 for (Value::use_iterator UI = LoadPtr->use_begin(), UE = LoadPtr->use_end();
372 UI != UE; ++UI)
373 if (LoadInst *Cand = dyn_cast<LoadInst>(*UI)) {// Is a load of source?
374 if (Cand->getParent()->getParent() == F && // In the same function?
375 // Not in LI's block?
376 Cand->getParent() != LoadBB && !Cand->isVolatile())
Chris Lattner3725c122005-01-29 07:04:10 +0000377 ++CandidateLoads[Cand->getParent()]; // Got one.
Chris Lattner15774df2005-01-29 06:31:53 +0000378 } else if (StoreInst *Cand = dyn_cast<StoreInst>(*UI)) {
379 if (Cand->getParent()->getParent() == F && !Cand->isVolatile() &&
Chris Lattner15774df2005-01-29 06:31:53 +0000380 Cand->getOperand(1) == LoadPtr) // It's a store THROUGH the ptr.
Chris Lattner3725c122005-01-29 07:04:10 +0000381 CandidateStores.insert(Cand->getParent());
Chris Lattner15774df2005-01-29 06:31:53 +0000382 }
Chris Lattner3725c122005-01-29 07:04:10 +0000383
Chris Lattner15774df2005-01-29 06:31:53 +0000384 // Get dominators.
Devang Patel6ea7ac62007-06-07 23:53:38 +0000385 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattner15774df2005-01-29 06:31:53 +0000386
Chris Lattner3b303d92004-02-05 17:20:00 +0000387 // TransparentBlocks - For each basic block the load/store is alive across,
388 // figure out if the pointer is invalidated or not. If it is invalidated, the
389 // boolean is set to false, if it's not it is set to true. If we don't know
390 // yet, the entry is not in the map.
391 std::map<BasicBlock*, bool> TransparentBlocks;
392
393 // Loop over all of the basic blocks that also load the value. If the value
394 // is live across the CFG from the source to destination blocks, and if the
395 // value is not invalidated in either the source or destination blocks, add it
396 // to the equivalence sets.
Chris Lattner3725c122005-01-29 07:04:10 +0000397 for (std::map<BasicBlock*, unsigned>::iterator
Chris Lattner3b303d92004-02-05 17:20:00 +0000398 I = CandidateLoads.begin(), E = CandidateLoads.end(); I != E; ++I) {
399 bool CantEqual = false;
400
401 // Right now we only can handle cases where one load dominates the other.
402 // FIXME: generalize this!
403 BasicBlock *BB1 = I->first, *BB2 = LoadBB;
Devang Patel6ea7ac62007-06-07 23:53:38 +0000404 if (DT.dominates(BB1, BB2)) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000405 // The other load dominates LI. If the loaded value is killed entering
406 // the LoadBB block, we know the load is not live.
407 if (LoadInvalidatedInBBBefore)
408 CantEqual = true;
Devang Patel6ea7ac62007-06-07 23:53:38 +0000409 } else if (DT.dominates(BB2, BB1)) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000410 std::swap(BB1, BB2); // Canonicalize
411 // LI dominates the other load. If the loaded value is killed exiting
412 // the LoadBB block, we know the load is not live.
413 if (LoadInvalidatedInBBAfter)
414 CantEqual = true;
415 } else {
416 // None of these loads can VN the same.
417 CantEqual = true;
418 }
419
420 if (!CantEqual) {
421 // Ok, at this point, we know that BB1 dominates BB2, and that there is
422 // nothing in the LI block that kills the loaded value. Check to see if
423 // the value is live across the CFG.
424 std::set<BasicBlock*> Visited;
425 for (pred_iterator PI = pred_begin(BB2), E = pred_end(BB2); PI!=E; ++PI)
426 if (!isPathTransparentTo(*PI, BB1, LoadPtr, LoadSize, AA,
427 Visited, TransparentBlocks)) {
428 // None of these loads can VN the same.
429 CantEqual = true;
430 break;
431 }
432 }
433
434 // If the loads can equal so far, scan the basic block that contains the
435 // loads under consideration to see if they are invalidated in the block.
436 // For any loads that are not invalidated, add them to the equivalence
437 // set!
438 if (!CantEqual) {
Chris Lattner3725c122005-01-29 07:04:10 +0000439 unsigned NumLoads = I->second;
Chris Lattner3b303d92004-02-05 17:20:00 +0000440 if (BB1 == LoadBB) {
441 // If LI dominates the block in question, check to see if any of the
442 // loads in this block are invalidated before they are reached.
443 for (BasicBlock::iterator BBI = I->first->begin(); ; ++BBI) {
Chris Lattner3725c122005-01-29 07:04:10 +0000444 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
445 if (LI->getOperand(0) == LoadPtr && !LI->isVolatile()) {
446 // The load is in the set!
447 RetVals.push_back(BBI);
448 if (--NumLoads == 0) break; // Found last load to check.
449 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000450 } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize)
Chris Lattner3725c122005-01-29 07:04:10 +0000451 & AliasAnalysis::Mod) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000452 // If there is a modifying instruction, nothing below it will value
453 // # the same.
454 break;
455 }
456 }
457 } else {
458 // If the block dominates LI, make sure that the loads in the block are
459 // not invalidated before the block ends.
460 BasicBlock::iterator BBI = I->first->end();
461 while (1) {
462 --BBI;
Chris Lattner3725c122005-01-29 07:04:10 +0000463 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
464 if (LI->getOperand(0) == LoadPtr && !LI->isVolatile()) {
465 // The load is the same as this load!
466 RetVals.push_back(BBI);
467 if (--NumLoads == 0) break; // Found all of the laods.
468 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000469 } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize)
470 & AliasAnalysis::Mod) {
471 // If there is a modifying instruction, nothing above it will value
472 // # the same.
473 break;
474 }
475 }
476 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000477 }
478 }
479
480 // Handle candidate stores. If the loaded location is clobbered on entrance
481 // to the LoadBB, no store outside of the LoadBB can value number equal, so
482 // quick exit.
483 if (LoadInvalidatedInBBBefore)
484 return;
485
Chris Lattner3725c122005-01-29 07:04:10 +0000486 // Stores in the load-bb are handled above.
487 CandidateStores.erase(LoadBB);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000488
Chris Lattner3725c122005-01-29 07:04:10 +0000489 for (std::set<BasicBlock*>::iterator I = CandidateStores.begin(),
490 E = CandidateStores.end(); I != E; ++I)
Devang Patel6ea7ac62007-06-07 23:53:38 +0000491 if (DT.dominates(*I, LoadBB)) {
Chris Lattner3725c122005-01-29 07:04:10 +0000492 BasicBlock *StoreBB = *I;
493
Chris Lattner3b303d92004-02-05 17:20:00 +0000494 // Check to see if the path from the store to the load is transparent
495 // w.r.t. the memory location.
496 bool CantEqual = false;
497 std::set<BasicBlock*> Visited;
498 for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
499 PI != E; ++PI)
Chris Lattner3725c122005-01-29 07:04:10 +0000500 if (!isPathTransparentTo(*PI, StoreBB, LoadPtr, LoadSize, AA,
Chris Lattner3b303d92004-02-05 17:20:00 +0000501 Visited, TransparentBlocks)) {
502 // None of these stores can VN the same.
503 CantEqual = true;
504 break;
505 }
506 Visited.clear();
507 if (!CantEqual) {
508 // Okay, the path from the store block to the load block is clear, and
509 // we know that there are no invalidating instructions from the start
510 // of the load block to the load itself. Now we just scan the store
511 // block.
512
Chris Lattner3725c122005-01-29 07:04:10 +0000513 BasicBlock::iterator BBI = StoreBB->end();
Chris Lattner3b303d92004-02-05 17:20:00 +0000514 while (1) {
Chris Lattner3725c122005-01-29 07:04:10 +0000515 assert(BBI != StoreBB->begin() &&
Chris Lattner0f312d62004-05-23 21:13:24 +0000516 "There is a store in this block of the pointer, but the store"
517 " doesn't mod the address being stored to?? Must be a bug in"
518 " the alias analysis implementation!");
Chris Lattner3b303d92004-02-05 17:20:00 +0000519 --BBI;
Chris Lattner0f312d62004-05-23 21:13:24 +0000520 if (AA.getModRefInfo(BBI, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000521 // If the invalidating instruction is one of the candidates,
522 // then it provides the value the load loads.
523 if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
Chris Lattner3725c122005-01-29 07:04:10 +0000524 if (SI->getOperand(1) == LoadPtr)
Chris Lattner3b303d92004-02-05 17:20:00 +0000525 RetVals.push_back(SI->getOperand(0));
526 break;
527 }
528 }
529 }
530 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000531}