1//===- Loads.cpp - Local load analysis ------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines simple local analyses for load instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/Loads.h"
15#include "llvm/Analysis/AliasAnalysis.h"
16#include "llvm/Target/TargetData.h"
17#include "llvm/GlobalAlias.h"
18#include "llvm/GlobalVariable.h"
19#include "llvm/IntrinsicInst.h"
20#include "llvm/Operator.h"
21using namespace llvm;
22
23/// AreEquivalentAddressValues - Test if A and B will obviously have the same
24/// value. This includes recognizing that %t0 and %t1 will have the same
25/// value in code like this:
26///   %t0 = getelementptr \@a, 0, 3
27///   store i32 0, i32* %t0
28///   %t1 = getelementptr \@a, 0, 3
29///   %t2 = load i32* %t1
30///
31static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
32  // Test if the values are trivially equivalent.
33  if (A == B) return true;
34
35  // Test if the values come from identical arithmetic instructions.
36  // Use isIdenticalToWhenDefined instead of isIdenticalTo because
37  // this function is only used when one address use dominates the
38  // other, which means that they'll always either have the same
39  // value or one of them will have an undefined value.
40  if (isa<BinaryOperator>(A) || isa<CastInst>(A) ||
41      isa<PHINode>(A) || isa<GetElementPtrInst>(A))
42    if (const Instruction *BI = dyn_cast<Instruction>(B))
43      if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
44        return true;
45
46  // Otherwise they may not be equivalent.
47  return false;
48}
49
50/// getUnderlyingObjectWithOffset - Strip off up to MaxLookup GEPs and
51/// bitcasts to get back to the underlying object being addressed, keeping
52/// track of the offset in bytes from the GEPs relative to the result.
53/// This is closely related to GetUnderlyingObject but is located
54/// here to avoid making VMCore depend on TargetData.
55static Value *getUnderlyingObjectWithOffset(Value *V, const TargetData *TD,
56                                            uint64_t &ByteOffset,
57                                            unsigned MaxLookup = 6) {
58  if (!V->getType()->isPointerTy())
59    return V;
60  for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
61    if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
62      if (!GEP->hasAllConstantIndices())
63        return V;
64      SmallVector<Value*, 8> Indices(GEP->op_begin() + 1, GEP->op_end());
65      ByteOffset += TD->getIndexedOffset(GEP->getPointerOperandType(),
66                                         Indices);
67      V = GEP->getPointerOperand();
68    } else if (Operator::getOpcode(V) == Instruction::BitCast) {
69      V = cast<Operator>(V)->getOperand(0);
70    } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
71      if (GA->mayBeOverridden())
72        return V;
73      V = GA->getAliasee();
74    } else {
75      return V;
76    }
77    assert(V->getType()->isPointerTy() && "Unexpected operand type!");
78  }
79  return V;
80}
81
82/// isSafeToLoadUnconditionally - Return true if we know that executing a load
83/// from this value cannot trap.  If it is not obviously safe to load from the
84/// specified pointer, we do a quick local scan of the basic block containing
85/// ScanFrom, to determine if the address is already accessed.
86bool llvm::isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom,
87                                       unsigned Align, const TargetData *TD) {
88  uint64_t ByteOffset = 0;
89  Value *Base = V;
90  if (TD)
91    Base = getUnderlyingObjectWithOffset(V, TD, ByteOffset);
92
93  Type *BaseType = 0;
94  unsigned BaseAlign = 0;
95  if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
96    // An alloca is safe to load from as load as it is suitably aligned.
97    BaseType = AI->getAllocatedType();
98    BaseAlign = AI->getAlignment();
99  } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(Base)) {
100    // Global variables are safe to load from but their size cannot be
101    // guaranteed if they are overridden.
102    if (!isa<GlobalAlias>(GV) && !GV->mayBeOverridden()) {
103      BaseType = GV->getType()->getElementType();
104      BaseAlign = GV->getAlignment();
105    }
106  }
107
108  if (BaseType && BaseType->isSized()) {
109    if (TD && BaseAlign == 0)
110      BaseAlign = TD->getPrefTypeAlignment(BaseType);
111
112    if (Align <= BaseAlign) {
113      if (!TD)
114        return true; // Loading directly from an alloca or global is OK.
115
116      // Check if the load is within the bounds of the underlying object.
117      PointerType *AddrTy = cast<PointerType>(V->getType());
118      uint64_t LoadSize = TD->getTypeStoreSize(AddrTy->getElementType());
119      if (ByteOffset + LoadSize <= TD->getTypeAllocSize(BaseType) &&
120          (Align == 0 || (ByteOffset % Align) == 0))
121        return true;
122    }
123  }
124
125  // Otherwise, be a little bit aggressive by scanning the local block where we
126  // want to check to see if the pointer is already being loaded or stored
127  // from/to.  If so, the previous load or store would have already trapped,
128  // so there is no harm doing an extra load (also, CSE will later eliminate
129  // the load entirely).
130  BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
131
132  while (BBI != E) {
133    --BBI;
134
135    // If we see a free or a call which may write to memory (i.e. which might do
136    // a free) the pointer could be marked invalid.
137    if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
138        !isa<DbgInfoIntrinsic>(BBI))
139      return false;
140
141    if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
142      if (AreEquivalentAddressValues(LI->getOperand(0), V)) return true;
143    } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
144      if (AreEquivalentAddressValues(SI->getOperand(1), V)) return true;
145    }
146  }
147  return false;
148}
149
150/// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the
151/// instruction before ScanFrom) checking to see if we have the value at the
152/// memory address *Ptr locally available within a small number of instructions.
153/// If the value is available, return it.
154///
155/// If not, return the iterator for the last validated instruction that the
156/// value would be live through.  If we scanned the entire block and didn't find
157/// something that invalidates *Ptr or provides it, ScanFrom would be left at
158/// begin() and this returns null.  ScanFrom could also be left
159///
160/// MaxInstsToScan specifies the maximum instructions to scan in the block.  If
161/// it is set to 0, it will scan the whole block. You can also optionally
162/// specify an alias analysis implementation, which makes this more precise.
163Value *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB,
164                                      BasicBlock::iterator &ScanFrom,
165                                      unsigned MaxInstsToScan,
166                                      AliasAnalysis *AA) {
167  if (MaxInstsToScan == 0) MaxInstsToScan = ~0U;
168
169  // If we're using alias analysis to disambiguate get the size of *Ptr.
170  uint64_t AccessSize = 0;
171  if (AA) {
172    Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType();
173    AccessSize = AA->getTypeStoreSize(AccessTy);
174  }
175
176  while (ScanFrom != ScanBB->begin()) {
177    // We must ignore debug info directives when counting (otherwise they
178    // would affect codegen).
179    Instruction *Inst = --ScanFrom;
180    if (isa<DbgInfoIntrinsic>(Inst))
181      continue;
182
183    // Restore ScanFrom to expected value in case next test succeeds
184    ScanFrom++;
185
186    // Don't scan huge blocks.
187    if (MaxInstsToScan-- == 0) return 0;
188
189    --ScanFrom;
190    // If this is a load of Ptr, the loaded value is available.
191    // (This is true even if the load is volatile or atomic, although
192    // those cases are unlikely.)
193    if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
194      if (AreEquivalentAddressValues(LI->getOperand(0), Ptr))
195        return LI;
196
197    if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
198      // If this is a store through Ptr, the value is available!
199      // (This is true even if the store is volatile or atomic, although
200      // those cases are unlikely.)
201      if (AreEquivalentAddressValues(SI->getOperand(1), Ptr))
202        return SI->getOperand(0);
203
204      // If Ptr is an alloca and this is a store to a different alloca, ignore
205      // the store.  This is a trivial form of alias analysis that is important
206      // for reg2mem'd code.
207      if ((isa<AllocaInst>(Ptr) || isa<GlobalVariable>(Ptr)) &&
208          (isa<AllocaInst>(SI->getOperand(1)) ||
209           isa<GlobalVariable>(SI->getOperand(1))))
210        continue;
211
212      // If we have alias analysis and it says the store won't modify the loaded
213      // value, ignore the store.
214      if (AA &&
215          (AA->getModRefInfo(SI, Ptr, AccessSize) & AliasAnalysis::Mod) == 0)
216        continue;
217
218      // Otherwise the store that may or may not alias the pointer, bail out.
219      ++ScanFrom;
220      return 0;
221    }
222
223    // If this is some other instruction that may clobber Ptr, bail out.
224    if (Inst->mayWriteToMemory()) {
225      // If alias analysis claims that it really won't modify the load,
226      // ignore it.
227      if (AA &&
228          (AA->getModRefInfo(Inst, Ptr, AccessSize) & AliasAnalysis::Mod) == 0)
229        continue;
230
231      // May modify the pointer, bail out.
232      ++ScanFrom;
233      return 0;
234    }
235  }
236
237  // Got to the start of the block, we didn't find it, but are done for this
238  // block.
239  return 0;
240}
241