blob: 1ffbb94636a77955362b1ba203557d67519eb3e9 [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//
10// This file implements a value numbering pass that value #'s load instructions.
11// To do this, it finds lexically identical load instructions, and uses alias
12// analysis to determine which loads are guaranteed to produce the same value.
13//
14// This pass builds off of another value numbering pass to implement value
15// numbering for non-load instructions. It uses Alias Analysis so that it can
16// disambiguate the load instructions. The more powerful these base analyses
17// are, the more powerful the resultant analysis will be.
18//
19//===----------------------------------------------------------------------===//
20
21#include "llvm/Analysis/LoadValueNumbering.h"
22#include "llvm/Analysis/ValueNumbering.h"
23#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/Analysis/Dominators.h"
Chris Lattnerf98d8d82003-02-26 19:27:35 +000025#include "llvm/Target/TargetData.h"
Chris Lattner71c7ec92002-08-30 20:28:10 +000026#include "llvm/Pass.h"
Chris Lattneraed2c6d2003-06-29 00:53:34 +000027#include "llvm/Type.h"
Chris Lattner71c7ec92002-08-30 20:28:10 +000028#include "llvm/iMemory.h"
29#include "llvm/BasicBlock.h"
30#include "llvm/Support/CFG.h"
31#include <algorithm>
32#include <set>
Chris Lattner270db362004-02-05 05:51:40 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner71c7ec92002-08-30 20:28:10 +000035namespace {
Chris Lattner28c6cf22003-06-16 12:06:41 +000036 // FIXME: This should not be a FunctionPass.
Chris Lattner71c7ec92002-08-30 20:28:10 +000037 struct LoadVN : public FunctionPass, public ValueNumbering {
38
39 /// Pass Implementation stuff. This doesn't do any analysis.
40 ///
41 bool runOnFunction(Function &) { return false; }
42
43 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
44 /// and Alias Analysis.
45 ///
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
47
48 /// getEqualNumberNodes - Return nodes with the same value number as the
49 /// specified Value. This fills in the argument vector with any equal
50 /// values.
51 ///
52 virtual void getEqualNumberNodes(Value *V1,
53 std::vector<Value*> &RetVals) const;
54 private:
55 /// haveEqualValueNumber - Given two load instructions, determine if they
56 /// both produce the same value on every execution of the program, assuming
57 /// that their source operands always give the same value. This uses the
58 /// AliasAnalysis implementation to invalidate loads when stores or function
59 /// calls occur that could modify the value produced by the load.
60 ///
61 bool haveEqualValueNumber(LoadInst *LI, LoadInst *LI2, AliasAnalysis &AA,
62 DominatorSet &DomSetInfo) const;
Chris Lattner28c6cf22003-06-16 12:06:41 +000063 bool haveEqualValueNumber(LoadInst *LI, StoreInst *SI, AliasAnalysis &AA,
64 DominatorSet &DomSetInfo) const;
Chris Lattner71c7ec92002-08-30 20:28:10 +000065 };
66
67 // Register this pass...
68 RegisterOpt<LoadVN> X("load-vn", "Load Value Numbering");
69
70 // Declare that we implement the ValueNumbering interface
71 RegisterAnalysisGroup<ValueNumbering, LoadVN> Y;
72}
73
Chris Lattner270db362004-02-05 05:51:40 +000074Pass *llvm::createLoadValueNumberingPass() { return new LoadVN(); }
Chris Lattner71c7ec92002-08-30 20:28:10 +000075
76
77/// getAnalysisUsage - Does not modify anything. It uses Value Numbering and
78/// Alias Analysis.
79///
80void LoadVN::getAnalysisUsage(AnalysisUsage &AU) const {
81 AU.setPreservesAll();
82 AU.addRequired<AliasAnalysis>();
83 AU.addRequired<ValueNumbering>();
84 AU.addRequired<DominatorSet>();
Chris Lattnerf98d8d82003-02-26 19:27:35 +000085 AU.addRequired<TargetData>();
Chris Lattner71c7ec92002-08-30 20:28:10 +000086}
87
88// getEqualNumberNodes - Return nodes with the same value number as the
89// specified Value. This fills in the argument vector with any equal values.
90//
91void LoadVN::getEqualNumberNodes(Value *V,
92 std::vector<Value*> &RetVals) const {
Chris Lattneraed2c6d2003-06-29 00:53:34 +000093 // If the alias analysis has any must alias information to share with us, we
Misha Brukman7bc439a2003-09-11 15:31:17 +000094 // can definitely use it.
Chris Lattneraed2c6d2003-06-29 00:53:34 +000095 if (isa<PointerType>(V->getType()))
96 getAnalysis<AliasAnalysis>().getMustAliases(V, RetVals);
Chris Lattner71c7ec92002-08-30 20:28:10 +000097
98 if (LoadInst *LI = dyn_cast<LoadInst>(V)) {
Chris Lattnerbd70a312003-09-08 18:13:58 +000099 // Volatile loads cannot be replaced with the value of other loads.
100 if (LI->isVolatile())
101 return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
102
Chris Lattner28c6cf22003-06-16 12:06:41 +0000103 // If we have a load instruction, find all of the load and store
104 // instructions that use the same source operand. We implement this
105 // recursively, because there could be a load of a load of a load that are
106 // all identical. We are guaranteed that this cannot be an infinite
107 // recursion because load instructions would have to pass through a PHI node
108 // in order for there to be a cycle. The PHI node would be handled by the
109 // else case here, breaking the infinite recursion.
Chris Lattner71c7ec92002-08-30 20:28:10 +0000110 //
111 std::vector<Value*> PointerSources;
112 getEqualNumberNodes(LI->getOperand(0), PointerSources);
113 PointerSources.push_back(LI->getOperand(0));
114
115 Function *F = LI->getParent()->getParent();
116
117 // Now that we know the set of equivalent source pointers for the load
Misha Brukman2f2d0652003-09-11 18:14:24 +0000118 // instruction, look to see if there are any load or store candidates that
Chris Lattner28c6cf22003-06-16 12:06:41 +0000119 // are identical.
Chris Lattner71c7ec92002-08-30 20:28:10 +0000120 //
121 std::vector<LoadInst*> CandidateLoads;
Chris Lattner28c6cf22003-06-16 12:06:41 +0000122 std::vector<StoreInst*> CandidateStores;
123
Chris Lattner71c7ec92002-08-30 20:28:10 +0000124 while (!PointerSources.empty()) {
125 Value *Source = PointerSources.back();
126 PointerSources.pop_back(); // Get a source pointer...
127
128 for (Value::use_iterator UI = Source->use_begin(), UE = Source->use_end();
129 UI != UE; ++UI)
Chris Lattner28c6cf22003-06-16 12:06:41 +0000130 if (LoadInst *Cand = dyn_cast<LoadInst>(*UI)) {// Is a load of source?
Chris Lattner71c7ec92002-08-30 20:28:10 +0000131 if (Cand->getParent()->getParent() == F && // In the same function?
Chris Lattnerbd70a312003-09-08 18:13:58 +0000132 Cand != LI && !Cand->isVolatile()) // Not LI itself?
Chris Lattner71c7ec92002-08-30 20:28:10 +0000133 CandidateLoads.push_back(Cand); // Got one...
Chris Lattner28c6cf22003-06-16 12:06:41 +0000134 } else if (StoreInst *Cand = dyn_cast<StoreInst>(*UI)) {
Chris Lattnerbd70a312003-09-08 18:13:58 +0000135 if (Cand->getParent()->getParent() == F && !Cand->isVolatile() &&
Chris Lattner28c6cf22003-06-16 12:06:41 +0000136 Cand->getOperand(1) == Source) // It's a store THROUGH the ptr...
137 CandidateStores.push_back(Cand);
138 }
Chris Lattner71c7ec92002-08-30 20:28:10 +0000139 }
140
141 // Remove duplicates from the CandidateLoads list because alias analysis
142 // processing may be somewhat expensive and we don't want to do more work
Misha Brukman5560c9d2003-08-18 14:43:39 +0000143 // than necessary.
Chris Lattner71c7ec92002-08-30 20:28:10 +0000144 //
Chris Lattner28c6cf22003-06-16 12:06:41 +0000145 unsigned OldSize = CandidateLoads.size();
Chris Lattner71c7ec92002-08-30 20:28:10 +0000146 std::sort(CandidateLoads.begin(), CandidateLoads.end());
147 CandidateLoads.erase(std::unique(CandidateLoads.begin(),
148 CandidateLoads.end()),
149 CandidateLoads.end());
Chris Lattner28c6cf22003-06-16 12:06:41 +0000150 // FIXME: REMOVE THIS SORTING AND UNIQUING IF IT CAN'T HAPPEN
151 assert(CandidateLoads.size() == OldSize && "Shrunk the candloads list?");
Chris Lattner71c7ec92002-08-30 20:28:10 +0000152
153 // Get Alias Analysis...
154 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
155 DominatorSet &DomSetInfo = getAnalysis<DominatorSet>();
156
Misha Brukman2f2d0652003-09-11 18:14:24 +0000157 // Loop over all of the candidate loads. If they are not invalidated by
Chris Lattner71c7ec92002-08-30 20:28:10 +0000158 // stores or calls between execution of them and LI, then add them to
159 // RetVals.
160 for (unsigned i = 0, e = CandidateLoads.size(); i != e; ++i)
161 if (haveEqualValueNumber(LI, CandidateLoads[i], AA, DomSetInfo))
162 RetVals.push_back(CandidateLoads[i]);
Chris Lattner28c6cf22003-06-16 12:06:41 +0000163 for (unsigned i = 0, e = CandidateStores.size(); i != e; ++i)
164 if (haveEqualValueNumber(LI, CandidateStores[i], AA, DomSetInfo))
165 RetVals.push_back(CandidateStores[i]->getOperand(0));
166
Chris Lattner71c7ec92002-08-30 20:28:10 +0000167 } else {
Chris Lattner71c7ec92002-08-30 20:28:10 +0000168 assert(&getAnalysis<ValueNumbering>() != (ValueNumbering*)this &&
169 "getAnalysis() returned this!");
170
171 // Not a load instruction? Just chain to the base value numbering
172 // implementation to satisfy the request...
173 return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
174 }
175}
176
177// CheckForInvalidatingInst - Return true if BB or any of the predecessors of BB
178// (until DestBB) contain an instruction that might invalidate Ptr.
179//
180static bool CheckForInvalidatingInst(BasicBlock *BB, BasicBlock *DestBB,
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000181 Value *Ptr, unsigned Size,
182 AliasAnalysis &AA,
Chris Lattner71c7ec92002-08-30 20:28:10 +0000183 std::set<BasicBlock*> &VisitedSet) {
184 // Found the termination point!
185 if (BB == DestBB || VisitedSet.count(BB)) return false;
186
187 // Avoid infinite recursion!
188 VisitedSet.insert(BB);
189
190 // Can this basic block modify Ptr?
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000191 if (AA.canBasicBlockModify(*BB, Ptr, Size))
Chris Lattner71c7ec92002-08-30 20:28:10 +0000192 return true;
193
194 // Check all of our predecessor blocks...
195 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI)
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000196 if (CheckForInvalidatingInst(*PI, DestBB, Ptr, Size, AA, VisitedSet))
Chris Lattner71c7ec92002-08-30 20:28:10 +0000197 return true;
198
199 // None of our predecessor blocks contain an invalidating instruction, and we
200 // don't either!
201 return false;
202}
203
204
205/// haveEqualValueNumber - Given two load instructions, determine if they both
206/// produce the same value on every execution of the program, assuming that
207/// their source operands always give the same value. This uses the
208/// AliasAnalysis implementation to invalidate loads when stores or function
209/// calls occur that could modify the value produced by the load.
210///
211bool LoadVN::haveEqualValueNumber(LoadInst *L1, LoadInst *L2,
212 AliasAnalysis &AA,
213 DominatorSet &DomSetInfo) const {
Chris Lattneradf9b902004-02-05 00:36:43 +0000214 assert(L1 != L2 && "haveEqualValueNumber assumes differing loads!");
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000215 assert(L1->getType() == L2->getType() &&
216 "How could the same source pointer return different types?");
Chris Lattneradf9b902004-02-05 00:36:43 +0000217 Value *LoadAddress = L1->getOperand(0);
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000218
219 // Find out how many bytes of memory are loaded by the load instruction...
220 unsigned LoadSize = getAnalysis<TargetData>().getTypeSize(L1->getType());
221
Chris Lattneradf9b902004-02-05 00:36:43 +0000222 // If the two loads are in the same basic block, just do a local analysis.
223 if (L1->getParent() == L2->getParent()) {
224 // It can be _very_ expensive to determine which instruction occurs first in
225 // the basic block if the block is large (see PR209). For this reason,
226 // instead of figuring out which block is first, then scanning all of the
227 // instructions, we scan the instructions both ways from L1 until we find
228 // L2. Along the way if we find a potentially modifying instruction, we
229 // kill the search. This helps in cases where we have large blocks the have
230 // potentially modifying instructions in them which stop the search.
Chris Lattner71c7ec92002-08-30 20:28:10 +0000231
Chris Lattneradf9b902004-02-05 00:36:43 +0000232 BasicBlock *BB = L1->getParent();
233 BasicBlock::iterator UpIt = L1, DownIt = L1; ++DownIt;
234 bool NoModifiesUp = true, NoModifiesDown = true;
235
236 // Scan up and down looking for L2, a modifying instruction, or the end of a
237 // basic block.
238 while (UpIt != BB->begin() && DownIt != BB->end()) {
239 // Scan up...
240 --UpIt;
241 if (&*UpIt == L2)
242 return NoModifiesUp; // No instructions invalidate the loads!
243 if (NoModifiesUp)
244 NoModifiesUp &=
245 !(AA.getModRefInfo(UpIt, LoadAddress, LoadSize) & AliasAnalysis::Mod);
246
247 if (&*DownIt == L2)
248 return NoModifiesDown;
249 if (NoModifiesDown)
250 NoModifiesDown &=
251 !(AA.getModRefInfo(DownIt, LoadAddress, LoadSize)
252 & AliasAnalysis::Mod);
253 ++DownIt;
254 }
255
256 // If we got here, we ran into one end of the basic block or the other.
257 if (UpIt != BB->begin()) {
258 // If we know that the upward scan found a modifier, return false.
259 if (!NoModifiesUp) return false;
260
261 // Otherwise, continue the scan looking for a modifier or L2.
262 for (--UpIt; &*UpIt != L2; --UpIt)
263 if (AA.getModRefInfo(UpIt, LoadAddress, LoadSize) & AliasAnalysis::Mod)
264 return false;
265 return true;
266 } else {
267 // If we know that the downward scan found a modifier, return false.
Tanya Lattner1ad5bc52004-02-05 04:45:21 +0000268 assert(DownIt != BB->end() && "Didn't find instructions??");
Chris Lattneradf9b902004-02-05 00:36:43 +0000269 if (!NoModifiesDown) return false;
270
271 // Otherwise, continue the scan looking for a modifier or L2.
272 for (; &*DownIt != L2; ++DownIt) {
273 if (AA.getModRefInfo(DownIt, LoadAddress, LoadSize) &AliasAnalysis::Mod)
274 return false;
275 }
276 return true;
277 }
Chris Lattner71c7ec92002-08-30 20:28:10 +0000278 } else {
Chris Lattneradf9b902004-02-05 00:36:43 +0000279 // Figure out which load dominates the other one. If neither dominates the
280 // other we cannot eliminate them.
281 //
282 // FIXME: This could be enhanced greatly!
283 //
284 if (DomSetInfo.dominates(L2, L1))
285 std::swap(L1, L2); // Make L1 dominate L2
286 else if (!DomSetInfo.dominates(L1, L2))
287 return false; // Neither instruction dominates the other one...
288
289 BasicBlock *BB1 = L1->getParent(), *BB2 = L2->getParent();
290
291 // L1 now dominates L2. Check to see if the intervening instructions
292 // between the two loads might modify the loaded location.
293
294 // Make sure that there are no modifying instructions between L1 and the end
295 // of its basic block.
Chris Lattner71c7ec92002-08-30 20:28:10 +0000296 //
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000297 if (AA.canInstructionRangeModify(*L1, *BB1->getTerminator(), LoadAddress,
298 LoadSize))
Chris Lattner71c7ec92002-08-30 20:28:10 +0000299 return false; // Cannot eliminate load
300
Chris Lattneradf9b902004-02-05 00:36:43 +0000301 // Make sure that there are no modifying instructions between the start of
302 // BB2 and the second load instruction.
Chris Lattner71c7ec92002-08-30 20:28:10 +0000303 //
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000304 if (AA.canInstructionRangeModify(BB2->front(), *L2, LoadAddress, LoadSize))
Chris Lattner71c7ec92002-08-30 20:28:10 +0000305 return false; // Cannot eliminate load
306
307 // Do a depth first traversal of the inverse CFG starting at L2's block,
308 // looking for L1's block. The inverse CFG is made up of the predecessor
309 // nodes of a block... so all of the edges in the graph are "backward".
310 //
311 std::set<BasicBlock*> VisitedSet;
312 for (pred_iterator PI = pred_begin(BB2), PE = pred_end(BB2); PI != PE; ++PI)
Chris Lattnerf98d8d82003-02-26 19:27:35 +0000313 if (CheckForInvalidatingInst(*PI, BB1, LoadAddress, LoadSize, AA,
314 VisitedSet))
Chris Lattner71c7ec92002-08-30 20:28:10 +0000315 return false;
Chris Lattneradf9b902004-02-05 00:36:43 +0000316
Chris Lattner71c7ec92002-08-30 20:28:10 +0000317 // If we passed all of these checks then we are sure that the two loads
318 // produce the same value.
319 return true;
320 }
321}
Chris Lattner28c6cf22003-06-16 12:06:41 +0000322
323
324/// haveEqualValueNumber - Given a load instruction and a store instruction,
325/// determine if the stored value reaches the loaded value unambiguously on
326/// every execution of the program. This uses the AliasAnalysis implementation
327/// to invalidate the stored value when stores or function calls occur that
328/// could modify the value produced by the load.
329///
330bool LoadVN::haveEqualValueNumber(LoadInst *Load, StoreInst *Store,
331 AliasAnalysis &AA,
332 DominatorSet &DomSetInfo) const {
333 // If the store does not dominate the load, we cannot do anything...
334 if (!DomSetInfo.dominates(Store, Load))
335 return false;
336
337 BasicBlock *BB1 = Store->getParent(), *BB2 = Load->getParent();
338 Value *LoadAddress = Load->getOperand(0);
339
340 assert(LoadAddress->getType() == Store->getOperand(1)->getType() &&
341 "How could the same source pointer return different types?");
342
343 // Find out how many bytes of memory are loaded by the load instruction...
344 unsigned LoadSize = getAnalysis<TargetData>().getTypeSize(Load->getType());
345
346 // Compute a basic block iterator pointing to the instruction after the store.
347 BasicBlock::iterator StoreIt = Store; ++StoreIt;
348
349 // Check to see if the intervening instructions between the two store and load
350 // include a store or call...
351 //
352 if (BB1 == BB2) { // In same basic block?
353 // In this degenerate case, no checking of global basic blocks has to occur
354 // just check the instructions BETWEEN Store & Load...
355 //
356 if (AA.canInstructionRangeModify(*StoreIt, *Load, LoadAddress, LoadSize))
357 return false; // Cannot eliminate load
358
359 // No instructions invalidate the stored value, they produce the same value!
360 return true;
361 } else {
362 // Make sure that there are no store instructions between the Store and the
363 // end of its basic block...
364 //
365 if (AA.canInstructionRangeModify(*StoreIt, *BB1->getTerminator(),
366 LoadAddress, LoadSize))
367 return false; // Cannot eliminate load
368
369 // Make sure that there are no store instructions between the start of BB2
370 // and the second load instruction...
371 //
372 if (AA.canInstructionRangeModify(BB2->front(), *Load, LoadAddress,LoadSize))
373 return false; // Cannot eliminate load
374
375 // Do a depth first traversal of the inverse CFG starting at L2's block,
376 // looking for L1's block. The inverse CFG is made up of the predecessor
377 // nodes of a block... so all of the edges in the graph are "backward".
378 //
379 std::set<BasicBlock*> VisitedSet;
380 for (pred_iterator PI = pred_begin(BB2), PE = pred_end(BB2); PI != PE; ++PI)
381 if (CheckForInvalidatingInst(*PI, BB1, LoadAddress, LoadSize, AA,
382 VisitedSet))
383 return false;
384
385 // If we passed all of these checks then we are sure that the two loads
386 // produce the same value.
387 return true;
388 }
389}