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