blob: 2414d33863dd1d303c5cf28fb6bed3f2d64b2bff [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 Patelc7582092008-03-19 21:56:59 +000044 LoadVN() : FunctionPass((intptr_t)&ID) {}
Devang Patel1cee94f2008-03-18 00:39:19 +000045
Chris Lattner71c7ec92002-08-30 20:28:10 +000046 /// Pass Implementation stuff. This doesn't do any analysis.
47 ///
48 bool runOnFunction(Function &) { return false; }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000049
Chris Lattner71c7ec92002-08-30 20:28:10 +000050 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
51 /// and Alias Analysis.
52 ///
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Misha Brukman2b37d7c2005-04-21 21:13:18 +000054
Chris Lattner71c7ec92002-08-30 20:28:10 +000055 /// getEqualNumberNodes - Return nodes with the same value number as the
56 /// specified Value. This fills in the argument vector with any equal
57 /// values.
58 ///
59 virtual void getEqualNumberNodes(Value *V1,
60 std::vector<Value*> &RetVals) const;
Chris Lattner5a6e9472004-03-15 05:44:59 +000061
Chris Lattner0f312d62004-05-23 21:13:24 +000062 /// deleteValue - This method should be called whenever an LLVM Value is
63 /// deleted from the program, for example when an instruction is found to be
64 /// redundant and is eliminated.
65 ///
66 virtual void deleteValue(Value *V) {
67 getAnalysis<AliasAnalysis>().deleteValue(V);
68 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000069
Chris Lattner0f312d62004-05-23 21:13:24 +000070 /// copyValue - This method should be used whenever a preexisting value in
71 /// the program is copied or cloned, introducing a new value. Note that
72 /// analysis implementations should tolerate clients that use this method to
73 /// introduce the same value multiple times: if the analysis already knows
74 /// about a value, it should ignore the request.
75 ///
76 virtual void copyValue(Value *From, Value *To) {
77 getAnalysis<AliasAnalysis>().copyValue(From, To);
78 }
79
Chris Lattner5a6e9472004-03-15 05:44:59 +000080 /// getCallEqualNumberNodes - Given a call instruction, find other calls
81 /// that have the same value number.
82 void getCallEqualNumberNodes(CallInst *CI,
83 std::vector<Value*> &RetVals) const;
Chris Lattner71c7ec92002-08-30 20:28:10 +000084 };
Chris Lattner71c7ec92002-08-30 20:28:10 +000085}
86
Dan Gohman844731a2008-05-13 00:00:25 +000087char LoadVN::ID = 0;
88// Register this pass...
89static RegisterPass<LoadVN>
90X("load-vn", "Load Value Numbering", false, true);
91
92// Declare that we implement the ValueNumbering interface
93static RegisterAnalysisGroup<ValueNumbering> Y(X);
94
Brian Gaeke96d4bf72004-07-27 17:43:21 +000095FunctionPass *llvm::createLoadValueNumberingPass() { return new LoadVN(); }
Chris Lattner71c7ec92002-08-30 20:28:10 +000096
97
98/// getAnalysisUsage - Does not modify anything. It uses Value Numbering and
99/// Alias Analysis.
100///
101void LoadVN::getAnalysisUsage(AnalysisUsage &AU) const {
102 AU.setPreservesAll();
Chris Lattner10673b62006-01-08 09:10:04 +0000103 AU.addRequiredTransitive<AliasAnalysis>();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000104 AU.addRequired<ValueNumbering>();
Devang Patel6ea7ac62007-06-07 23:53:38 +0000105 AU.addRequiredTransitive<DominatorTree>();
Chris Lattner10673b62006-01-08 09:10:04 +0000106 AU.addRequiredTransitive<TargetData>();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000107}
108
Chris Lattner3b303d92004-02-05 17:20:00 +0000109static bool isPathTransparentTo(BasicBlock *CurBlock, BasicBlock *Dom,
110 Value *Ptr, unsigned Size, AliasAnalysis &AA,
111 std::set<BasicBlock*> &Visited,
112 std::map<BasicBlock*, bool> &TransparentBlocks){
113 // If we have already checked out this path, or if we reached our destination,
114 // stop searching, returning success.
115 if (CurBlock == Dom || !Visited.insert(CurBlock).second)
116 return true;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000117
Chris Lattner3b303d92004-02-05 17:20:00 +0000118 // Check whether this block is known transparent or not.
119 std::map<BasicBlock*, bool>::iterator TBI =
120 TransparentBlocks.lower_bound(CurBlock);
121
122 if (TBI == TransparentBlocks.end() || TBI->first != CurBlock) {
123 // If this basic block can modify the memory location, then the path is not
124 // transparent!
125 if (AA.canBasicBlockModify(*CurBlock, Ptr, Size)) {
126 TransparentBlocks.insert(TBI, std::make_pair(CurBlock, false));
127 return false;
128 }
Chris Lattner0f312d62004-05-23 21:13:24 +0000129 TransparentBlocks.insert(TBI, std::make_pair(CurBlock, true));
Chris Lattner3b303d92004-02-05 17:20:00 +0000130 } else if (!TBI->second)
131 // This block is known non-transparent, so that path can't be either.
132 return false;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000133
Chris Lattner3b303d92004-02-05 17:20:00 +0000134 // The current block is known to be transparent. The entire path is
135 // transparent if all of the predecessors paths to the parent is also
136 // transparent to the memory location.
137 for (pred_iterator PI = pred_begin(CurBlock), E = pred_end(CurBlock);
138 PI != E; ++PI)
139 if (!isPathTransparentTo(*PI, Dom, Ptr, Size, AA, Visited,
140 TransparentBlocks))
141 return false;
142 return true;
143}
144
Chris Lattner5a6e9472004-03-15 05:44:59 +0000145/// getCallEqualNumberNodes - Given a call instruction, find other calls that
146/// have the same value number.
147void LoadVN::getCallEqualNumberNodes(CallInst *CI,
148 std::vector<Value*> &RetVals) const {
149 Function *CF = CI->getCalledFunction();
150 if (CF == 0) return; // Indirect call.
151 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Duncan Sandsdff67102007-12-01 07:51:45 +0000152 AliasAnalysis::ModRefBehavior MRB = AA.getModRefBehavior(CI);
Chris Lattnerfd4b3c42004-12-15 18:14:04 +0000153 if (MRB != AliasAnalysis::DoesNotAccessMemory &&
154 MRB != AliasAnalysis::OnlyReadsMemory)
155 return; // Nothing we can do for now.
Chris Lattner5a6e9472004-03-15 05:44:59 +0000156
157 // Scan all of the arguments of the function, looking for one that is not
158 // global. In particular, we would prefer to have an argument or instruction
159 // operand to chase the def-use chains of.
160 Value *Op = CF;
Gabor Greif785c6af2008-05-22 19:24:54 +0000161 for (User::op_iterator i = CI->op_begin() + 1, e = CI->op_end(); i != e; ++i)
162 if (isa<Argument>(*i) ||
163 isa<Instruction>(*i)) {
164 Op = *i;
Chris Lattner5a6e9472004-03-15 05:44:59 +0000165 break;
166 }
167
168 // Identify all lexically identical calls in this function.
169 std::vector<CallInst*> IdenticalCalls;
170
171 Function *CIFunc = CI->getParent()->getParent();
172 for (Value::use_iterator UI = Op->use_begin(), E = Op->use_end(); UI != E;
173 ++UI)
174 if (CallInst *C = dyn_cast<CallInst>(*UI))
175 if (C->getNumOperands() == CI->getNumOperands() &&
176 C->getOperand(0) == CI->getOperand(0) &&
177 C->getParent()->getParent() == CIFunc && C != CI) {
178 bool AllOperandsEqual = true;
Gabor Greif785c6af2008-05-22 19:24:54 +0000179 for (User::op_iterator i = CI->op_begin() + 1, j = C->op_begin() + 1,
180 e = CI->op_end(); i != e; ++i, ++j)
181 if (*j != *i) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000182 AllOperandsEqual = false;
183 break;
184 }
185
186 if (AllOperandsEqual)
187 IdenticalCalls.push_back(C);
188 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000189
Chris Lattner5a6e9472004-03-15 05:44:59 +0000190 if (IdenticalCalls.empty()) return;
191
192 // Eliminate duplicates, which could occur if we chose a value that is passed
193 // into a call site multiple times.
194 std::sort(IdenticalCalls.begin(), IdenticalCalls.end());
195 IdenticalCalls.erase(std::unique(IdenticalCalls.begin(),IdenticalCalls.end()),
196 IdenticalCalls.end());
197
198 // If the call reads memory, we must make sure that there are no stores
199 // between the calls in question.
200 //
201 // FIXME: This should use mod/ref information. What we really care about it
202 // whether an intervening instruction could modify memory that is read, not
203 // ANY memory.
204 //
Chris Lattnerfd4b3c42004-12-15 18:14:04 +0000205 if (MRB == AliasAnalysis::OnlyReadsMemory) {
Devang Patel6ea7ac62007-06-07 23:53:38 +0000206 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattner5a6e9472004-03-15 05:44:59 +0000207 BasicBlock *CIBB = CI->getParent();
208 for (unsigned i = 0; i != IdenticalCalls.size(); ++i) {
209 CallInst *C = IdenticalCalls[i];
210 bool CantEqual = false;
211
Devang Patel6ea7ac62007-06-07 23:53:38 +0000212 if (DT.dominates(CIBB, C->getParent())) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000213 // FIXME: we currently only handle the case where both calls are in the
214 // same basic block.
215 if (CIBB != C->getParent()) {
216 CantEqual = true;
217 } else {
218 Instruction *First = CI, *Second = C;
Devang Patel6ea7ac62007-06-07 23:53:38 +0000219 if (!DT.dominates(CI, C))
Chris Lattner5a6e9472004-03-15 05:44:59 +0000220 std::swap(First, Second);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000221
Chris Lattner5a6e9472004-03-15 05:44:59 +0000222 // Scan the instructions between the calls, checking for stores or
223 // calls to dangerous functions.
224 BasicBlock::iterator I = First;
225 for (++First; I != BasicBlock::iterator(Second); ++I) {
226 if (isa<StoreInst>(I)) {
227 // FIXME: We could use mod/ref information to make this much
228 // better!
229 CantEqual = true;
230 break;
231 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000232 if (!AA.onlyReadsMemory(CI)) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000233 CantEqual = true;
234 break;
235 }
236 } else if (I->mayWriteToMemory()) {
237 CantEqual = true;
238 break;
239 }
240 }
241 }
242
Devang Patel6ea7ac62007-06-07 23:53:38 +0000243 } else if (DT.dominates(C->getParent(), CIBB)) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000244 // FIXME: We could implement this, but we don't for now.
245 CantEqual = true;
246 } else {
247 // FIXME: if one doesn't dominate the other, we can't tell yet.
248 CantEqual = true;
249 }
250
251
252 if (CantEqual) {
253 // This call does not produce the same value as the one in the query.
254 std::swap(IdenticalCalls[i--], IdenticalCalls.back());
255 IdenticalCalls.pop_back();
256 }
257 }
258 }
259
260 // Any calls that are identical and not destroyed will produce equal values!
261 for (unsigned i = 0, e = IdenticalCalls.size(); i != e; ++i)
262 RetVals.push_back(IdenticalCalls[i]);
263}
Chris Lattner3b303d92004-02-05 17:20:00 +0000264
Chris Lattner71c7ec92002-08-30 20:28:10 +0000265// getEqualNumberNodes - Return nodes with the same value number as the
266// specified Value. This fills in the argument vector with any equal values.
267//
268void LoadVN::getEqualNumberNodes(Value *V,
269 std::vector<Value*> &RetVals) const {
Chris Lattneraed2c6d2003-06-29 00:53:34 +0000270 // If the alias analysis has any must alias information to share with us, we
Misha Brukman7bc439a2003-09-11 15:31:17 +0000271 // can definitely use it.
Chris Lattneraed2c6d2003-06-29 00:53:34 +0000272 if (isa<PointerType>(V->getType()))
273 getAnalysis<AliasAnalysis>().getMustAliases(V, RetVals);
Chris Lattner71c7ec92002-08-30 20:28:10 +0000274
Chris Lattner57ef9a22004-02-05 05:56:23 +0000275 if (!isa<LoadInst>(V)) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000276 if (CallInst *CI = dyn_cast<CallInst>(V))
Chris Lattner002be762004-03-16 03:41:35 +0000277 getCallEqualNumberNodes(CI, RetVals);
Chris Lattner5a6e9472004-03-15 05:44:59 +0000278
Chris Lattner57ef9a22004-02-05 05:56:23 +0000279 // Not a load instruction? Just chain to the base value numbering
280 // implementation to satisfy the request...
Chris Lattner71c7ec92002-08-30 20:28:10 +0000281 assert(&getAnalysis<ValueNumbering>() != (ValueNumbering*)this &&
282 "getAnalysis() returned this!");
283
Chris Lattner71c7ec92002-08-30 20:28:10 +0000284 return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
285 }
Chris Lattner57ef9a22004-02-05 05:56:23 +0000286
287 // Volatile loads cannot be replaced with the value of other loads.
288 LoadInst *LI = cast<LoadInst>(V);
289 if (LI->isVolatile())
290 return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000291
Chris Lattner15774df2005-01-29 06:31:53 +0000292 Value *LoadPtr = LI->getOperand(0);
Chris Lattner3b303d92004-02-05 17:20:00 +0000293 BasicBlock *LoadBB = LI->getParent();
294 Function *F = LoadBB->getParent();
Chris Lattner15774df2005-01-29 06:31:53 +0000295
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000296 // Find out how many bytes of memory are loaded by the load instruction...
Duncan Sands514ab342007-11-01 20:53:16 +0000297 unsigned LoadSize = getAnalysis<TargetData>().getTypeStoreSize(LI->getType());
Chris Lattner15774df2005-01-29 06:31:53 +0000298 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000299
Chris Lattner3b303d92004-02-05 17:20:00 +0000300 // Figure out if the load is invalidated from the entry of the block it is in
301 // until the actual instruction. This scans the block backwards from LI. If
302 // we see any candidate load or store instructions, then we know that the
303 // candidates have the same value # as LI.
304 bool LoadInvalidatedInBBBefore = false;
305 for (BasicBlock::iterator I = LI; I != LoadBB->begin(); ) {
306 --I;
Chris Lattneree379a12005-01-29 06:42:34 +0000307 if (I == LoadPtr) {
Chris Lattner5da80972004-04-03 00:45:16 +0000308 // If we run into an allocation of the value being loaded, then the
Chris Lattner2652da62005-01-29 05:57:01 +0000309 // contents are not initialized.
Chris Lattner15774df2005-01-29 06:31:53 +0000310 if (isa<AllocationInst>(I))
Chris Lattner2652da62005-01-29 05:57:01 +0000311 RetVals.push_back(UndefValue::get(LI->getType()));
Chris Lattner15774df2005-01-29 06:31:53 +0000312
313 // Otherwise, since this is the definition of what we are loading, this
314 // loaded value cannot occur before this block.
315 LoadInvalidatedInBBBefore = true;
316 break;
Chris Lattneree379a12005-01-29 06:42:34 +0000317 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
318 // If this instruction is a candidate load before LI, we know there are no
319 // invalidating instructions between it and LI, so they have the same
320 // value number.
321 if (LI->getOperand(0) == LoadPtr && !LI->isVolatile())
322 RetVals.push_back(I);
Chris Lattneradf9b902004-02-05 00:36:43 +0000323 }
Chris Lattner3725c122005-01-29 07:04:10 +0000324
Chris Lattner3b303d92004-02-05 17:20:00 +0000325 if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
326 // If the invalidating instruction is a store, and its in our candidate
327 // set, then we can do store-load forwarding: the load has the same value
328 // # as the stored value.
Chris Lattneree379a12005-01-29 06:42:34 +0000329 if (StoreInst *SI = dyn_cast<StoreInst>(I))
330 if (SI->getOperand(1) == LoadPtr)
331 RetVals.push_back(I->getOperand(0));
Chris Lattner3b303d92004-02-05 17:20:00 +0000332
333 LoadInvalidatedInBBBefore = true;
334 break;
Chris Lattneradf9b902004-02-05 00:36:43 +0000335 }
Chris Lattner71c7ec92002-08-30 20:28:10 +0000336 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000337
Chris Lattner3b303d92004-02-05 17:20:00 +0000338 // Figure out if the load is invalidated between the load and the exit of the
339 // block it is defined in. While we are scanning the current basic block, if
340 // we see any candidate loads, then we know they have the same value # as LI.
Chris Lattner28c6cf22003-06-16 12:06:41 +0000341 //
Chris Lattner3b303d92004-02-05 17:20:00 +0000342 bool LoadInvalidatedInBBAfter = false;
Chris Lattner8e8f8652007-04-17 17:52:45 +0000343 {
344 BasicBlock::iterator I = LI;
345 for (++I; I != LoadBB->end(); ++I) {
346 // If this instruction is a load, then this instruction returns the same
347 // value as LI.
348 if (isa<LoadInst>(I) && cast<LoadInst>(I)->getOperand(0) == LoadPtr)
349 RetVals.push_back(I);
Chris Lattner28c6cf22003-06-16 12:06:41 +0000350
Chris Lattner8e8f8652007-04-17 17:52:45 +0000351 if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
352 LoadInvalidatedInBBAfter = true;
353 break;
354 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000355 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000356 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000357
Chris Lattner15774df2005-01-29 06:31:53 +0000358 // If the pointer is clobbered on entry and on exit to the function, there is
359 // no need to do any global analysis at all.
360 if (LoadInvalidatedInBBBefore && LoadInvalidatedInBBAfter)
361 return;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000362
Chris Lattner3725c122005-01-29 07:04:10 +0000363 // Now that we know the value is not neccesarily killed on entry or exit to
364 // the BB, find out how many load and store instructions (to this location)
365 // live in each BB in the function.
Chris Lattner15774df2005-01-29 06:31:53 +0000366 //
Chris Lattner3725c122005-01-29 07:04:10 +0000367 std::map<BasicBlock*, unsigned> CandidateLoads;
368 std::set<BasicBlock*> CandidateStores;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000369
Chris Lattner15774df2005-01-29 06:31:53 +0000370 for (Value::use_iterator UI = LoadPtr->use_begin(), UE = LoadPtr->use_end();
371 UI != UE; ++UI)
372 if (LoadInst *Cand = dyn_cast<LoadInst>(*UI)) {// Is a load of source?
373 if (Cand->getParent()->getParent() == F && // In the same function?
374 // Not in LI's block?
375 Cand->getParent() != LoadBB && !Cand->isVolatile())
Chris Lattner3725c122005-01-29 07:04:10 +0000376 ++CandidateLoads[Cand->getParent()]; // Got one.
Chris Lattner15774df2005-01-29 06:31:53 +0000377 } else if (StoreInst *Cand = dyn_cast<StoreInst>(*UI)) {
378 if (Cand->getParent()->getParent() == F && !Cand->isVolatile() &&
Chris Lattner15774df2005-01-29 06:31:53 +0000379 Cand->getOperand(1) == LoadPtr) // It's a store THROUGH the ptr.
Chris Lattner3725c122005-01-29 07:04:10 +0000380 CandidateStores.insert(Cand->getParent());
Chris Lattner15774df2005-01-29 06:31:53 +0000381 }
Chris Lattner3725c122005-01-29 07:04:10 +0000382
Chris Lattner15774df2005-01-29 06:31:53 +0000383 // Get dominators.
Devang Patel6ea7ac62007-06-07 23:53:38 +0000384 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattner15774df2005-01-29 06:31:53 +0000385
Chris Lattner3b303d92004-02-05 17:20:00 +0000386 // TransparentBlocks - For each basic block the load/store is alive across,
387 // figure out if the pointer is invalidated or not. If it is invalidated, the
388 // boolean is set to false, if it's not it is set to true. If we don't know
389 // yet, the entry is not in the map.
390 std::map<BasicBlock*, bool> TransparentBlocks;
391
392 // Loop over all of the basic blocks that also load the value. If the value
393 // is live across the CFG from the source to destination blocks, and if the
394 // value is not invalidated in either the source or destination blocks, add it
395 // to the equivalence sets.
Chris Lattner3725c122005-01-29 07:04:10 +0000396 for (std::map<BasicBlock*, unsigned>::iterator
Chris Lattner3b303d92004-02-05 17:20:00 +0000397 I = CandidateLoads.begin(), E = CandidateLoads.end(); I != E; ++I) {
398 bool CantEqual = false;
399
400 // Right now we only can handle cases where one load dominates the other.
401 // FIXME: generalize this!
402 BasicBlock *BB1 = I->first, *BB2 = LoadBB;
Devang Patel6ea7ac62007-06-07 23:53:38 +0000403 if (DT.dominates(BB1, BB2)) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000404 // The other load dominates LI. If the loaded value is killed entering
405 // the LoadBB block, we know the load is not live.
406 if (LoadInvalidatedInBBBefore)
407 CantEqual = true;
Devang Patel6ea7ac62007-06-07 23:53:38 +0000408 } else if (DT.dominates(BB2, BB1)) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000409 std::swap(BB1, BB2); // Canonicalize
410 // LI dominates the other load. If the loaded value is killed exiting
411 // the LoadBB block, we know the load is not live.
412 if (LoadInvalidatedInBBAfter)
413 CantEqual = true;
414 } else {
415 // None of these loads can VN the same.
416 CantEqual = true;
417 }
418
419 if (!CantEqual) {
420 // Ok, at this point, we know that BB1 dominates BB2, and that there is
421 // nothing in the LI block that kills the loaded value. Check to see if
422 // the value is live across the CFG.
423 std::set<BasicBlock*> Visited;
424 for (pred_iterator PI = pred_begin(BB2), E = pred_end(BB2); PI!=E; ++PI)
425 if (!isPathTransparentTo(*PI, BB1, LoadPtr, LoadSize, AA,
426 Visited, TransparentBlocks)) {
427 // None of these loads can VN the same.
428 CantEqual = true;
429 break;
430 }
431 }
432
433 // If the loads can equal so far, scan the basic block that contains the
434 // loads under consideration to see if they are invalidated in the block.
435 // For any loads that are not invalidated, add them to the equivalence
436 // set!
437 if (!CantEqual) {
Chris Lattner3725c122005-01-29 07:04:10 +0000438 unsigned NumLoads = I->second;
Chris Lattner3b303d92004-02-05 17:20:00 +0000439 if (BB1 == LoadBB) {
440 // If LI dominates the block in question, check to see if any of the
441 // loads in this block are invalidated before they are reached.
442 for (BasicBlock::iterator BBI = I->first->begin(); ; ++BBI) {
Chris Lattner3725c122005-01-29 07:04:10 +0000443 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
444 if (LI->getOperand(0) == LoadPtr && !LI->isVolatile()) {
445 // The load is in the set!
446 RetVals.push_back(BBI);
447 if (--NumLoads == 0) break; // Found last load to check.
448 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000449 } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize)
Chris Lattner3725c122005-01-29 07:04:10 +0000450 & AliasAnalysis::Mod) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000451 // If there is a modifying instruction, nothing below it will value
452 // # the same.
453 break;
454 }
455 }
456 } else {
457 // If the block dominates LI, make sure that the loads in the block are
458 // not invalidated before the block ends.
459 BasicBlock::iterator BBI = I->first->end();
460 while (1) {
461 --BBI;
Chris Lattner3725c122005-01-29 07:04:10 +0000462 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
463 if (LI->getOperand(0) == LoadPtr && !LI->isVolatile()) {
464 // The load is the same as this load!
465 RetVals.push_back(BBI);
466 if (--NumLoads == 0) break; // Found all of the laods.
467 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000468 } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize)
469 & AliasAnalysis::Mod) {
470 // If there is a modifying instruction, nothing above it will value
471 // # the same.
472 break;
473 }
474 }
475 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000476 }
477 }
478
479 // Handle candidate stores. If the loaded location is clobbered on entrance
480 // to the LoadBB, no store outside of the LoadBB can value number equal, so
481 // quick exit.
482 if (LoadInvalidatedInBBBefore)
483 return;
484
Chris Lattner3725c122005-01-29 07:04:10 +0000485 // Stores in the load-bb are handled above.
486 CandidateStores.erase(LoadBB);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000487
Chris Lattner3725c122005-01-29 07:04:10 +0000488 for (std::set<BasicBlock*>::iterator I = CandidateStores.begin(),
489 E = CandidateStores.end(); I != E; ++I)
Devang Patel6ea7ac62007-06-07 23:53:38 +0000490 if (DT.dominates(*I, LoadBB)) {
Chris Lattner3725c122005-01-29 07:04:10 +0000491 BasicBlock *StoreBB = *I;
492
Chris Lattner3b303d92004-02-05 17:20:00 +0000493 // Check to see if the path from the store to the load is transparent
494 // w.r.t. the memory location.
495 bool CantEqual = false;
496 std::set<BasicBlock*> Visited;
497 for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
498 PI != E; ++PI)
Chris Lattner3725c122005-01-29 07:04:10 +0000499 if (!isPathTransparentTo(*PI, StoreBB, LoadPtr, LoadSize, AA,
Chris Lattner3b303d92004-02-05 17:20:00 +0000500 Visited, TransparentBlocks)) {
501 // None of these stores can VN the same.
502 CantEqual = true;
503 break;
504 }
505 Visited.clear();
506 if (!CantEqual) {
507 // Okay, the path from the store block to the load block is clear, and
508 // we know that there are no invalidating instructions from the start
509 // of the load block to the load itself. Now we just scan the store
510 // block.
511
Chris Lattner3725c122005-01-29 07:04:10 +0000512 BasicBlock::iterator BBI = StoreBB->end();
Chris Lattner3b303d92004-02-05 17:20:00 +0000513 while (1) {
Chris Lattner3725c122005-01-29 07:04:10 +0000514 assert(BBI != StoreBB->begin() &&
Chris Lattner0f312d62004-05-23 21:13:24 +0000515 "There is a store in this block of the pointer, but the store"
516 " doesn't mod the address being stored to?? Must be a bug in"
517 " the alias analysis implementation!");
Chris Lattner3b303d92004-02-05 17:20:00 +0000518 --BBI;
Chris Lattner0f312d62004-05-23 21:13:24 +0000519 if (AA.getModRefInfo(BBI, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000520 // If the invalidating instruction is one of the candidates,
521 // then it provides the value the load loads.
522 if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
Chris Lattner3725c122005-01-29 07:04:10 +0000523 if (SI->getOperand(1) == LoadPtr)
Chris Lattner3b303d92004-02-05 17:20:00 +0000524 RetVals.push_back(SI->getOperand(0));
525 break;
526 }
527 }
528 }
529 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000530}