1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- Loads.cpp - Local load analysis ------------------------------------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file defines simple local analyses for load instructions.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Analysis/Loads.h"
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Analysis/AliasAnalysis.h"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetData.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/GlobalAlias.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/GlobalVariable.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/IntrinsicInst.h"
2019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Operator.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// AreEquivalentAddressValues - Test if A and B will obviously have the same
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// value. This includes recognizing that %t0 and %t1 will have the same
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// value in code like this:
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   %t0 = getelementptr \@a, 0, 3
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   store i32 0, i32* %t0
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   %t1 = getelementptr \@a, 0, 3
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   %t2 = load i32* %t1
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool AreEquivalentAddressValues(const Value *A, const Value *B) {
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Test if the values are trivially equivalent.
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (A == B) return true;
3419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Test if the values come from identical arithmetic instructions.
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Use isIdenticalToWhenDefined instead of isIdenticalTo because
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // this function is only used when one address use dominates the
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // other, which means that they'll always either have the same
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // value or one of them will have an undefined value.
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<BinaryOperator>(A) || isa<CastInst>(A) ||
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      isa<PHINode>(A) || isa<GetElementPtrInst>(A))
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (const Instruction *BI = dyn_cast<Instruction>(B))
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
4519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise they may not be equivalent.
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getUnderlyingObjectWithOffset - Strip off up to MaxLookup GEPs and
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// bitcasts to get back to the underlying object being addressed, keeping
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// track of the offset in bytes from the GEPs relative to the result.
5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// This is closely related to GetUnderlyingObject but is located
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// here to avoid making VMCore depend on TargetData.
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic Value *getUnderlyingObjectWithOffset(Value *V, const TargetData *TD,
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                            uint64_t &ByteOffset,
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                            unsigned MaxLookup = 6) {
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!V->getType()->isPointerTy())
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return V;
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!GEP->hasAllConstantIndices())
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return V;
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SmallVector<Value*, 8> Indices(GEP->op_begin() + 1, GEP->op_end());
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ByteOffset += TD->getIndexedOffset(GEP->getPointerOperandType(),
6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         Indices);
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V = GEP->getPointerOperand();
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (Operator::getOpcode(V) == Instruction::BitCast) {
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V = cast<Operator>(V)->getOperand(0);
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (GA->mayBeOverridden())
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return V;
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V = GA->getAliasee();
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return V;
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(V->getType()->isPointerTy() && "Unexpected operand type!");
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return V;
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isSafeToLoadUnconditionally - Return true if we know that executing a load
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// from this value cannot trap.  If it is not obviously safe to load from the
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specified pointer, we do a quick local scan of the basic block containing
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ScanFrom, to determine if the address is already accessed.
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool llvm::isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom,
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       unsigned Align, const TargetData *TD) {
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint64_t ByteOffset = 0;
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Value *Base = V;
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TD)
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Base = getUnderlyingObjectWithOffset(V, TD, ByteOffset);
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *BaseType = 0;
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned BaseAlign = 0;
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // An alloca is safe to load from as load as it is suitably aligned.
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BaseType = AI->getAllocatedType();
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BaseAlign = AI->getAlignment();
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(Base)) {
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Global variables are safe to load from but their size cannot be
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // guaranteed if they are overridden.
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isa<GlobalAlias>(GV) && !GV->mayBeOverridden()) {
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BaseType = GV->getType()->getElementType();
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BaseAlign = GV->getAlignment();
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (BaseType && BaseType->isSized()) {
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TD && BaseAlign == 0)
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BaseAlign = TD->getPrefTypeAlignment(BaseType);
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Align <= BaseAlign) {
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!TD)
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true; // Loading directly from an alloca or global is OK.
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Check if the load is within the bounds of the underlying object.
11719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      PointerType *AddrTy = cast<PointerType>(V->getType());
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      uint64_t LoadSize = TD->getTypeStoreSize(AddrTy->getElementType());
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ByteOffset + LoadSize <= TD->getTypeAllocSize(BaseType) &&
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (Align == 0 || (ByteOffset % Align) == 0))
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, be a little bit aggressive by scanning the local block where we
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // want to check to see if the pointer is already being loaded or stored
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // from/to.  If so, the previous load or store would have already trapped,
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // so there is no harm doing an extra load (also, CSE will later eliminate
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the load entirely).
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (BBI != E) {
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    --BBI;
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we see a free or a call which may write to memory (i.e. which might do
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // a free) the pointer could be marked invalid.
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
13819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        !isa<DbgInfoIntrinsic>(BBI))
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (AreEquivalentAddressValues(LI->getOperand(0), V)) return true;
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (AreEquivalentAddressValues(SI->getOperand(1), V)) return true;
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// instruction before ScanFrom) checking to see if we have the value at the
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// memory address *Ptr locally available within a small number of instructions.
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// If the value is available, return it.
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// If not, return the iterator for the last validated instruction that the
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// value would be live through.  If we scanned the entire block and didn't find
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// something that invalidates *Ptr or provides it, ScanFrom would be left at
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// begin() and this returns null.  ScanFrom could also be left
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// MaxInstsToScan specifies the maximum instructions to scan in the block.  If
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// it is set to 0, it will scan the whole block. You can also optionally
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specify an alias analysis implementation, which makes this more precise.
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanValue *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB,
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      BasicBlock::iterator &ScanFrom,
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      unsigned MaxInstsToScan,
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      AliasAnalysis *AA) {
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MaxInstsToScan == 0) MaxInstsToScan = ~0U;
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we're using alias analysis to disambiguate get the size of *Ptr.
17019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t AccessSize = 0;
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AA) {
17219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType();
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AccessSize = AA->getTypeStoreSize(AccessTy);
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (ScanFrom != ScanBB->begin()) {
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We must ignore debug info directives when counting (otherwise they
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // would affect codegen).
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Instruction *Inst = --ScanFrom;
18019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (isa<DbgInfoIntrinsic>(Inst))
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Restore ScanFrom to expected value in case next test succeeds
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ScanFrom++;
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Don't scan huge blocks.
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (MaxInstsToScan-- == 0) return 0;
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    --ScanFrom;
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is a load of Ptr, the loaded value is available.
19119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // (This is true even if the load is volatile or atomic, although
19219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // those cases are unlikely.)
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (AreEquivalentAddressValues(LI->getOperand(0), Ptr))
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return LI;
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If this is a store through Ptr, the value is available!
19919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // (This is true even if the store is volatile or atomic, although
20019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // those cases are unlikely.)
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (AreEquivalentAddressValues(SI->getOperand(1), Ptr))
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return SI->getOperand(0);
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If Ptr is an alloca and this is a store to a different alloca, ignore
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // the store.  This is a trivial form of alias analysis that is important
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // for reg2mem'd code.
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((isa<AllocaInst>(Ptr) || isa<GlobalVariable>(Ptr)) &&
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (isa<AllocaInst>(SI->getOperand(1)) ||
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           isa<GlobalVariable>(SI->getOperand(1))))
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If we have alias analysis and it says the store won't modify the loaded
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // value, ignore the store.
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (AA &&
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (AA->getModRefInfo(SI, Ptr, AccessSize) & AliasAnalysis::Mod) == 0)
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Otherwise the store that may or may not alias the pointer, bail out.
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++ScanFrom;
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return 0;
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is some other instruction that may clobber Ptr, bail out.
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Inst->mayWriteToMemory()) {
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If alias analysis claims that it really won't modify the load,
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // ignore it.
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (AA &&
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (AA->getModRefInfo(Inst, Ptr, AccessSize) & AliasAnalysis::Mod) == 0)
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // May modify the pointer, bail out.
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++ScanFrom;
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return 0;
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Got to the start of the block, we didn't find it, but are done for this
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // block.
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
241