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