blob: 16687b0419416ca12cfe24281d6e6d07b84fe025 [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//
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 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 {
Misha Brukman2b37d7c2005-04-21 21:13:18 +000043
Chris Lattner71c7ec92002-08-30 20:28:10 +000044 /// Pass Implementation stuff. This doesn't do any analysis.
45 ///
46 bool runOnFunction(Function &) { return false; }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000047
Chris Lattner71c7ec92002-08-30 20:28:10 +000048 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
49 /// and Alias Analysis.
50 ///
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Misha Brukman2b37d7c2005-04-21 21:13:18 +000052
Chris Lattner71c7ec92002-08-30 20:28:10 +000053 /// getEqualNumberNodes - Return nodes with the same value number as the
54 /// specified Value. This fills in the argument vector with any equal
55 /// values.
56 ///
57 virtual void getEqualNumberNodes(Value *V1,
58 std::vector<Value*> &RetVals) const;
Chris Lattner5a6e9472004-03-15 05:44:59 +000059
Chris Lattner0f312d62004-05-23 21:13:24 +000060 /// deleteValue - This method should be called whenever an LLVM Value is
61 /// deleted from the program, for example when an instruction is found to be
62 /// redundant and is eliminated.
63 ///
64 virtual void deleteValue(Value *V) {
65 getAnalysis<AliasAnalysis>().deleteValue(V);
66 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000067
Chris Lattner0f312d62004-05-23 21:13:24 +000068 /// copyValue - This method should be used whenever a preexisting value in
69 /// the program is copied or cloned, introducing a new value. Note that
70 /// analysis implementations should tolerate clients that use this method to
71 /// introduce the same value multiple times: if the analysis already knows
72 /// about a value, it should ignore the request.
73 ///
74 virtual void copyValue(Value *From, Value *To) {
75 getAnalysis<AliasAnalysis>().copyValue(From, To);
76 }
77
Chris Lattner5a6e9472004-03-15 05:44:59 +000078 /// getCallEqualNumberNodes - Given a call instruction, find other calls
79 /// that have the same value number.
80 void getCallEqualNumberNodes(CallInst *CI,
81 std::vector<Value*> &RetVals) const;
Chris Lattner71c7ec92002-08-30 20:28:10 +000082 };
83
84 // Register this pass...
Chris Lattner7f8897f2006-08-27 22:42:52 +000085 RegisterPass<LoadVN> X("load-vn", "Load Value Numbering");
Chris Lattner71c7ec92002-08-30 20:28:10 +000086
87 // Declare that we implement the ValueNumbering interface
Chris Lattnera5370172006-08-28 00:42:29 +000088 RegisterAnalysisGroup<ValueNumbering> Y(X);
Chris Lattner71c7ec92002-08-30 20:28:10 +000089}
90
Brian Gaeke96d4bf72004-07-27 17:43:21 +000091FunctionPass *llvm::createLoadValueNumberingPass() { return new LoadVN(); }
Chris Lattner71c7ec92002-08-30 20:28:10 +000092
93
94/// getAnalysisUsage - Does not modify anything. It uses Value Numbering and
95/// Alias Analysis.
96///
97void LoadVN::getAnalysisUsage(AnalysisUsage &AU) const {
98 AU.setPreservesAll();
Chris Lattner10673b62006-01-08 09:10:04 +000099 AU.addRequiredTransitive<AliasAnalysis>();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000100 AU.addRequired<ValueNumbering>();
Owen Anderson46b58f72007-04-07 04:43:07 +0000101 AU.addRequiredTransitive<ETForest>();
Chris Lattner10673b62006-01-08 09:10:04 +0000102 AU.addRequiredTransitive<TargetData>();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000103}
104
Chris Lattner3b303d92004-02-05 17:20:00 +0000105static bool isPathTransparentTo(BasicBlock *CurBlock, BasicBlock *Dom,
106 Value *Ptr, unsigned Size, AliasAnalysis &AA,
107 std::set<BasicBlock*> &Visited,
108 std::map<BasicBlock*, bool> &TransparentBlocks){
109 // If we have already checked out this path, or if we reached our destination,
110 // stop searching, returning success.
111 if (CurBlock == Dom || !Visited.insert(CurBlock).second)
112 return true;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000113
Chris Lattner3b303d92004-02-05 17:20:00 +0000114 // Check whether this block is known transparent or not.
115 std::map<BasicBlock*, bool>::iterator TBI =
116 TransparentBlocks.lower_bound(CurBlock);
117
118 if (TBI == TransparentBlocks.end() || TBI->first != CurBlock) {
119 // If this basic block can modify the memory location, then the path is not
120 // transparent!
121 if (AA.canBasicBlockModify(*CurBlock, Ptr, Size)) {
122 TransparentBlocks.insert(TBI, std::make_pair(CurBlock, false));
123 return false;
124 }
Chris Lattner0f312d62004-05-23 21:13:24 +0000125 TransparentBlocks.insert(TBI, std::make_pair(CurBlock, true));
Chris Lattner3b303d92004-02-05 17:20:00 +0000126 } else if (!TBI->second)
127 // This block is known non-transparent, so that path can't be either.
128 return false;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000129
Chris Lattner3b303d92004-02-05 17:20:00 +0000130 // The current block is known to be transparent. The entire path is
131 // transparent if all of the predecessors paths to the parent is also
132 // transparent to the memory location.
133 for (pred_iterator PI = pred_begin(CurBlock), E = pred_end(CurBlock);
134 PI != E; ++PI)
135 if (!isPathTransparentTo(*PI, Dom, Ptr, Size, AA, Visited,
136 TransparentBlocks))
137 return false;
138 return true;
139}
140
Chris Lattner5a6e9472004-03-15 05:44:59 +0000141/// getCallEqualNumberNodes - Given a call instruction, find other calls that
142/// have the same value number.
143void LoadVN::getCallEqualNumberNodes(CallInst *CI,
144 std::vector<Value*> &RetVals) const {
145 Function *CF = CI->getCalledFunction();
146 if (CF == 0) return; // Indirect call.
147 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Chris Lattnerfd4b3c42004-12-15 18:14:04 +0000148 AliasAnalysis::ModRefBehavior MRB = AA.getModRefBehavior(CF, CI);
149 if (MRB != AliasAnalysis::DoesNotAccessMemory &&
150 MRB != AliasAnalysis::OnlyReadsMemory)
151 return; // Nothing we can do for now.
Chris Lattner5a6e9472004-03-15 05:44:59 +0000152
153 // Scan all of the arguments of the function, looking for one that is not
154 // global. In particular, we would prefer to have an argument or instruction
155 // operand to chase the def-use chains of.
156 Value *Op = CF;
157 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
158 if (isa<Argument>(CI->getOperand(i)) ||
159 isa<Instruction>(CI->getOperand(i))) {
160 Op = CI->getOperand(i);
161 break;
162 }
163
164 // Identify all lexically identical calls in this function.
165 std::vector<CallInst*> IdenticalCalls;
166
167 Function *CIFunc = CI->getParent()->getParent();
168 for (Value::use_iterator UI = Op->use_begin(), E = Op->use_end(); UI != E;
169 ++UI)
170 if (CallInst *C = dyn_cast<CallInst>(*UI))
171 if (C->getNumOperands() == CI->getNumOperands() &&
172 C->getOperand(0) == CI->getOperand(0) &&
173 C->getParent()->getParent() == CIFunc && C != CI) {
174 bool AllOperandsEqual = true;
175 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
176 if (C->getOperand(i) != CI->getOperand(i)) {
177 AllOperandsEqual = false;
178 break;
179 }
180
181 if (AllOperandsEqual)
182 IdenticalCalls.push_back(C);
183 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000184
Chris Lattner5a6e9472004-03-15 05:44:59 +0000185 if (IdenticalCalls.empty()) return;
186
187 // Eliminate duplicates, which could occur if we chose a value that is passed
188 // into a call site multiple times.
189 std::sort(IdenticalCalls.begin(), IdenticalCalls.end());
190 IdenticalCalls.erase(std::unique(IdenticalCalls.begin(),IdenticalCalls.end()),
191 IdenticalCalls.end());
192
193 // If the call reads memory, we must make sure that there are no stores
194 // between the calls in question.
195 //
196 // FIXME: This should use mod/ref information. What we really care about it
197 // whether an intervening instruction could modify memory that is read, not
198 // ANY memory.
199 //
Chris Lattnerfd4b3c42004-12-15 18:14:04 +0000200 if (MRB == AliasAnalysis::OnlyReadsMemory) {
Owen Anderson46b58f72007-04-07 04:43:07 +0000201 ETForest &EF = getAnalysis<ETForest>();
Chris Lattner5a6e9472004-03-15 05:44:59 +0000202 BasicBlock *CIBB = CI->getParent();
203 for (unsigned i = 0; i != IdenticalCalls.size(); ++i) {
204 CallInst *C = IdenticalCalls[i];
205 bool CantEqual = false;
206
Owen Anderson46b58f72007-04-07 04:43:07 +0000207 if (EF.dominates(CIBB, C->getParent())) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000208 // FIXME: we currently only handle the case where both calls are in the
209 // same basic block.
210 if (CIBB != C->getParent()) {
211 CantEqual = true;
212 } else {
213 Instruction *First = CI, *Second = C;
Owen Anderson46b58f72007-04-07 04:43:07 +0000214 if (!EF.dominates(CI, C))
Chris Lattner5a6e9472004-03-15 05:44:59 +0000215 std::swap(First, Second);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000216
Chris Lattner5a6e9472004-03-15 05:44:59 +0000217 // Scan the instructions between the calls, checking for stores or
218 // calls to dangerous functions.
219 BasicBlock::iterator I = First;
220 for (++First; I != BasicBlock::iterator(Second); ++I) {
221 if (isa<StoreInst>(I)) {
222 // FIXME: We could use mod/ref information to make this much
223 // better!
224 CantEqual = true;
225 break;
226 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
227 if (CI->getCalledFunction() == 0 ||
228 !AA.onlyReadsMemory(CI->getCalledFunction())) {
229 CantEqual = true;
230 break;
231 }
232 } else if (I->mayWriteToMemory()) {
233 CantEqual = true;
234 break;
235 }
236 }
237 }
238
Owen Anderson46b58f72007-04-07 04:43:07 +0000239 } else if (EF.dominates(C->getParent(), CIBB)) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000240 // FIXME: We could implement this, but we don't for now.
241 CantEqual = true;
242 } else {
243 // FIXME: if one doesn't dominate the other, we can't tell yet.
244 CantEqual = true;
245 }
246
247
248 if (CantEqual) {
249 // This call does not produce the same value as the one in the query.
250 std::swap(IdenticalCalls[i--], IdenticalCalls.back());
251 IdenticalCalls.pop_back();
252 }
253 }
254 }
255
256 // Any calls that are identical and not destroyed will produce equal values!
257 for (unsigned i = 0, e = IdenticalCalls.size(); i != e; ++i)
258 RetVals.push_back(IdenticalCalls[i]);
259}
Chris Lattner3b303d92004-02-05 17:20:00 +0000260
Chris Lattner71c7ec92002-08-30 20:28:10 +0000261// getEqualNumberNodes - Return nodes with the same value number as the
262// specified Value. This fills in the argument vector with any equal values.
263//
264void LoadVN::getEqualNumberNodes(Value *V,
265 std::vector<Value*> &RetVals) const {
Chris Lattneraed2c6d2003-06-29 00:53:34 +0000266 // If the alias analysis has any must alias information to share with us, we
Misha Brukman7bc439a2003-09-11 15:31:17 +0000267 // can definitely use it.
Chris Lattneraed2c6d2003-06-29 00:53:34 +0000268 if (isa<PointerType>(V->getType()))
269 getAnalysis<AliasAnalysis>().getMustAliases(V, RetVals);
Chris Lattner71c7ec92002-08-30 20:28:10 +0000270
Chris Lattner57ef9a22004-02-05 05:56:23 +0000271 if (!isa<LoadInst>(V)) {
Chris Lattner5a6e9472004-03-15 05:44:59 +0000272 if (CallInst *CI = dyn_cast<CallInst>(V))
Chris Lattner002be762004-03-16 03:41:35 +0000273 getCallEqualNumberNodes(CI, RetVals);
Chris Lattner5a6e9472004-03-15 05:44:59 +0000274
Chris Lattner57ef9a22004-02-05 05:56:23 +0000275 // Not a load instruction? Just chain to the base value numbering
276 // implementation to satisfy the request...
Chris Lattner71c7ec92002-08-30 20:28:10 +0000277 assert(&getAnalysis<ValueNumbering>() != (ValueNumbering*)this &&
278 "getAnalysis() returned this!");
279
Chris Lattner71c7ec92002-08-30 20:28:10 +0000280 return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
281 }
Chris Lattner57ef9a22004-02-05 05:56:23 +0000282
283 // Volatile loads cannot be replaced with the value of other loads.
284 LoadInst *LI = cast<LoadInst>(V);
285 if (LI->isVolatile())
286 return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000287
Chris Lattner15774df2005-01-29 06:31:53 +0000288 Value *LoadPtr = LI->getOperand(0);
Chris Lattner3b303d92004-02-05 17:20:00 +0000289 BasicBlock *LoadBB = LI->getParent();
290 Function *F = LoadBB->getParent();
Chris Lattner15774df2005-01-29 06:31:53 +0000291
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000292 // Find out how many bytes of memory are loaded by the load instruction...
Chris Lattner3b303d92004-02-05 17:20:00 +0000293 unsigned LoadSize = getAnalysis<TargetData>().getTypeSize(LI->getType());
Chris Lattner15774df2005-01-29 06:31:53 +0000294 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000295
Chris Lattner3b303d92004-02-05 17:20:00 +0000296 // Figure out if the load is invalidated from the entry of the block it is in
297 // until the actual instruction. This scans the block backwards from LI. If
298 // we see any candidate load or store instructions, then we know that the
299 // candidates have the same value # as LI.
300 bool LoadInvalidatedInBBBefore = false;
301 for (BasicBlock::iterator I = LI; I != LoadBB->begin(); ) {
302 --I;
Chris Lattneree379a12005-01-29 06:42:34 +0000303 if (I == LoadPtr) {
Chris Lattner5da80972004-04-03 00:45:16 +0000304 // If we run into an allocation of the value being loaded, then the
Chris Lattner2652da62005-01-29 05:57:01 +0000305 // contents are not initialized.
Chris Lattner15774df2005-01-29 06:31:53 +0000306 if (isa<AllocationInst>(I))
Chris Lattner2652da62005-01-29 05:57:01 +0000307 RetVals.push_back(UndefValue::get(LI->getType()));
Chris Lattner15774df2005-01-29 06:31:53 +0000308
309 // Otherwise, since this is the definition of what we are loading, this
310 // loaded value cannot occur before this block.
311 LoadInvalidatedInBBBefore = true;
312 break;
Chris Lattneree379a12005-01-29 06:42:34 +0000313 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
314 // If this instruction is a candidate load before LI, we know there are no
315 // invalidating instructions between it and LI, so they have the same
316 // value number.
317 if (LI->getOperand(0) == LoadPtr && !LI->isVolatile())
318 RetVals.push_back(I);
Chris Lattneradf9b902004-02-05 00:36:43 +0000319 }
Chris Lattner3725c122005-01-29 07:04:10 +0000320
Chris Lattner3b303d92004-02-05 17:20:00 +0000321 if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
322 // If the invalidating instruction is a store, and its in our candidate
323 // set, then we can do store-load forwarding: the load has the same value
324 // # as the stored value.
Chris Lattneree379a12005-01-29 06:42:34 +0000325 if (StoreInst *SI = dyn_cast<StoreInst>(I))
326 if (SI->getOperand(1) == LoadPtr)
327 RetVals.push_back(I->getOperand(0));
Chris Lattner3b303d92004-02-05 17:20:00 +0000328
329 LoadInvalidatedInBBBefore = true;
330 break;
Chris Lattneradf9b902004-02-05 00:36:43 +0000331 }
Chris Lattner71c7ec92002-08-30 20:28:10 +0000332 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000333
Chris Lattner3b303d92004-02-05 17:20:00 +0000334 // Figure out if the load is invalidated between the load and the exit of the
335 // block it is defined in. While we are scanning the current basic block, if
336 // we see any candidate loads, then we know they have the same value # as LI.
Chris Lattner28c6cf22003-06-16 12:06:41 +0000337 //
Chris Lattner3b303d92004-02-05 17:20:00 +0000338 bool LoadInvalidatedInBBAfter = false;
Chris Lattner8e8f8652007-04-17 17:52:45 +0000339 {
340 BasicBlock::iterator I = LI;
341 for (++I; I != LoadBB->end(); ++I) {
342 // If this instruction is a load, then this instruction returns the same
343 // value as LI.
344 if (isa<LoadInst>(I) && cast<LoadInst>(I)->getOperand(0) == LoadPtr)
345 RetVals.push_back(I);
Chris Lattner28c6cf22003-06-16 12:06:41 +0000346
Chris Lattner8e8f8652007-04-17 17:52:45 +0000347 if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
348 LoadInvalidatedInBBAfter = true;
349 break;
350 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000351 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000352 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000353
Chris Lattner15774df2005-01-29 06:31:53 +0000354 // If the pointer is clobbered on entry and on exit to the function, there is
355 // no need to do any global analysis at all.
356 if (LoadInvalidatedInBBBefore && LoadInvalidatedInBBAfter)
357 return;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000358
Chris Lattner3725c122005-01-29 07:04:10 +0000359 // Now that we know the value is not neccesarily killed on entry or exit to
360 // the BB, find out how many load and store instructions (to this location)
361 // live in each BB in the function.
Chris Lattner15774df2005-01-29 06:31:53 +0000362 //
Chris Lattner3725c122005-01-29 07:04:10 +0000363 std::map<BasicBlock*, unsigned> CandidateLoads;
364 std::set<BasicBlock*> CandidateStores;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000365
Chris Lattner15774df2005-01-29 06:31:53 +0000366 for (Value::use_iterator UI = LoadPtr->use_begin(), UE = LoadPtr->use_end();
367 UI != UE; ++UI)
368 if (LoadInst *Cand = dyn_cast<LoadInst>(*UI)) {// Is a load of source?
369 if (Cand->getParent()->getParent() == F && // In the same function?
370 // Not in LI's block?
371 Cand->getParent() != LoadBB && !Cand->isVolatile())
Chris Lattner3725c122005-01-29 07:04:10 +0000372 ++CandidateLoads[Cand->getParent()]; // Got one.
Chris Lattner15774df2005-01-29 06:31:53 +0000373 } else if (StoreInst *Cand = dyn_cast<StoreInst>(*UI)) {
374 if (Cand->getParent()->getParent() == F && !Cand->isVolatile() &&
Chris Lattner15774df2005-01-29 06:31:53 +0000375 Cand->getOperand(1) == LoadPtr) // It's a store THROUGH the ptr.
Chris Lattner3725c122005-01-29 07:04:10 +0000376 CandidateStores.insert(Cand->getParent());
Chris Lattner15774df2005-01-29 06:31:53 +0000377 }
Chris Lattner3725c122005-01-29 07:04:10 +0000378
Chris Lattner15774df2005-01-29 06:31:53 +0000379 // Get dominators.
Owen Anderson46b58f72007-04-07 04:43:07 +0000380 ETForest &EF = getAnalysis<ETForest>();
Chris Lattner15774df2005-01-29 06:31:53 +0000381
Chris Lattner3b303d92004-02-05 17:20:00 +0000382 // TransparentBlocks - For each basic block the load/store is alive across,
383 // figure out if the pointer is invalidated or not. If it is invalidated, the
384 // boolean is set to false, if it's not it is set to true. If we don't know
385 // yet, the entry is not in the map.
386 std::map<BasicBlock*, bool> TransparentBlocks;
387
388 // Loop over all of the basic blocks that also load the value. If the value
389 // is live across the CFG from the source to destination blocks, and if the
390 // value is not invalidated in either the source or destination blocks, add it
391 // to the equivalence sets.
Chris Lattner3725c122005-01-29 07:04:10 +0000392 for (std::map<BasicBlock*, unsigned>::iterator
Chris Lattner3b303d92004-02-05 17:20:00 +0000393 I = CandidateLoads.begin(), E = CandidateLoads.end(); I != E; ++I) {
394 bool CantEqual = false;
395
396 // Right now we only can handle cases where one load dominates the other.
397 // FIXME: generalize this!
398 BasicBlock *BB1 = I->first, *BB2 = LoadBB;
Owen Anderson46b58f72007-04-07 04:43:07 +0000399 if (EF.dominates(BB1, BB2)) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000400 // The other load dominates LI. If the loaded value is killed entering
401 // the LoadBB block, we know the load is not live.
402 if (LoadInvalidatedInBBBefore)
403 CantEqual = true;
Owen Anderson46b58f72007-04-07 04:43:07 +0000404 } else if (EF.dominates(BB2, BB1)) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000405 std::swap(BB1, BB2); // Canonicalize
406 // LI dominates the other load. If the loaded value is killed exiting
407 // the LoadBB block, we know the load is not live.
408 if (LoadInvalidatedInBBAfter)
409 CantEqual = true;
410 } else {
411 // None of these loads can VN the same.
412 CantEqual = true;
413 }
414
415 if (!CantEqual) {
416 // Ok, at this point, we know that BB1 dominates BB2, and that there is
417 // nothing in the LI block that kills the loaded value. Check to see if
418 // the value is live across the CFG.
419 std::set<BasicBlock*> Visited;
420 for (pred_iterator PI = pred_begin(BB2), E = pred_end(BB2); PI!=E; ++PI)
421 if (!isPathTransparentTo(*PI, BB1, LoadPtr, LoadSize, AA,
422 Visited, TransparentBlocks)) {
423 // None of these loads can VN the same.
424 CantEqual = true;
425 break;
426 }
427 }
428
429 // If the loads can equal so far, scan the basic block that contains the
430 // loads under consideration to see if they are invalidated in the block.
431 // For any loads that are not invalidated, add them to the equivalence
432 // set!
433 if (!CantEqual) {
Chris Lattner3725c122005-01-29 07:04:10 +0000434 unsigned NumLoads = I->second;
Chris Lattner3b303d92004-02-05 17:20:00 +0000435 if (BB1 == LoadBB) {
436 // If LI dominates the block in question, check to see if any of the
437 // loads in this block are invalidated before they are reached.
438 for (BasicBlock::iterator BBI = I->first->begin(); ; ++BBI) {
Chris Lattner3725c122005-01-29 07:04:10 +0000439 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
440 if (LI->getOperand(0) == LoadPtr && !LI->isVolatile()) {
441 // The load is in the set!
442 RetVals.push_back(BBI);
443 if (--NumLoads == 0) break; // Found last load to check.
444 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000445 } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize)
Chris Lattner3725c122005-01-29 07:04:10 +0000446 & AliasAnalysis::Mod) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000447 // If there is a modifying instruction, nothing below it will value
448 // # the same.
449 break;
450 }
451 }
452 } else {
453 // If the block dominates LI, make sure that the loads in the block are
454 // not invalidated before the block ends.
455 BasicBlock::iterator BBI = I->first->end();
456 while (1) {
457 --BBI;
Chris Lattner3725c122005-01-29 07:04:10 +0000458 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
459 if (LI->getOperand(0) == LoadPtr && !LI->isVolatile()) {
460 // The load is the same as this load!
461 RetVals.push_back(BBI);
462 if (--NumLoads == 0) break; // Found all of the laods.
463 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000464 } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize)
465 & AliasAnalysis::Mod) {
466 // If there is a modifying instruction, nothing above it will value
467 // # the same.
468 break;
469 }
470 }
471 }
Chris Lattner3b303d92004-02-05 17:20:00 +0000472 }
473 }
474
475 // Handle candidate stores. If the loaded location is clobbered on entrance
476 // to the LoadBB, no store outside of the LoadBB can value number equal, so
477 // quick exit.
478 if (LoadInvalidatedInBBBefore)
479 return;
480
Chris Lattner3725c122005-01-29 07:04:10 +0000481 // Stores in the load-bb are handled above.
482 CandidateStores.erase(LoadBB);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000483
Chris Lattner3725c122005-01-29 07:04:10 +0000484 for (std::set<BasicBlock*>::iterator I = CandidateStores.begin(),
485 E = CandidateStores.end(); I != E; ++I)
Owen Anderson46b58f72007-04-07 04:43:07 +0000486 if (EF.dominates(*I, LoadBB)) {
Chris Lattner3725c122005-01-29 07:04:10 +0000487 BasicBlock *StoreBB = *I;
488
Chris Lattner3b303d92004-02-05 17:20:00 +0000489 // Check to see if the path from the store to the load is transparent
490 // w.r.t. the memory location.
491 bool CantEqual = false;
492 std::set<BasicBlock*> Visited;
493 for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
494 PI != E; ++PI)
Chris Lattner3725c122005-01-29 07:04:10 +0000495 if (!isPathTransparentTo(*PI, StoreBB, LoadPtr, LoadSize, AA,
Chris Lattner3b303d92004-02-05 17:20:00 +0000496 Visited, TransparentBlocks)) {
497 // None of these stores can VN the same.
498 CantEqual = true;
499 break;
500 }
501 Visited.clear();
502 if (!CantEqual) {
503 // Okay, the path from the store block to the load block is clear, and
504 // we know that there are no invalidating instructions from the start
505 // of the load block to the load itself. Now we just scan the store
506 // block.
507
Chris Lattner3725c122005-01-29 07:04:10 +0000508 BasicBlock::iterator BBI = StoreBB->end();
Chris Lattner3b303d92004-02-05 17:20:00 +0000509 while (1) {
Chris Lattner3725c122005-01-29 07:04:10 +0000510 assert(BBI != StoreBB->begin() &&
Chris Lattner0f312d62004-05-23 21:13:24 +0000511 "There is a store in this block of the pointer, but the store"
512 " doesn't mod the address being stored to?? Must be a bug in"
513 " the alias analysis implementation!");
Chris Lattner3b303d92004-02-05 17:20:00 +0000514 --BBI;
Chris Lattner0f312d62004-05-23 21:13:24 +0000515 if (AA.getModRefInfo(BBI, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
Chris Lattner3b303d92004-02-05 17:20:00 +0000516 // If the invalidating instruction is one of the candidates,
517 // then it provides the value the load loads.
518 if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
Chris Lattner3725c122005-01-29 07:04:10 +0000519 if (SI->getOperand(1) == LoadPtr)
Chris Lattner3b303d92004-02-05 17:20:00 +0000520 RetVals.push_back(SI->getOperand(0));
521 break;
522 }
523 }
524 }
525 }
Chris Lattner28c6cf22003-06-16 12:06:41 +0000526}