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