1210006Srdivacky//===- Loads.cpp - Local load analysis ------------------------------------===//
2210006Srdivacky//
3210006Srdivacky//                     The LLVM Compiler Infrastructure
4210006Srdivacky//
5210006Srdivacky// This file is distributed under the University of Illinois Open Source
6210006Srdivacky// License. See LICENSE.TXT for details.
7210006Srdivacky//
8210006Srdivacky//===----------------------------------------------------------------------===//
9210006Srdivacky//
10210006Srdivacky// This file defines simple local analyses for load instructions.
11210006Srdivacky//
12210006Srdivacky//===----------------------------------------------------------------------===//
13210006Srdivacky
14210006Srdivacky#include "llvm/Analysis/Loads.h"
15210006Srdivacky#include "llvm/Analysis/AliasAnalysis.h"
16249423Sdim#include "llvm/Analysis/ValueTracking.h"
17249423Sdim#include "llvm/IR/DataLayout.h"
18249423Sdim#include "llvm/IR/GlobalAlias.h"
19249423Sdim#include "llvm/IR/GlobalVariable.h"
20249423Sdim#include "llvm/IR/IntrinsicInst.h"
21249423Sdim#include "llvm/IR/LLVMContext.h"
22249423Sdim#include "llvm/IR/Operator.h"
23210006Srdivackyusing namespace llvm;
24210006Srdivacky
25210006Srdivacky/// AreEquivalentAddressValues - Test if A and B will obviously have the same
26210006Srdivacky/// value. This includes recognizing that %t0 and %t1 will have the same
27210006Srdivacky/// value in code like this:
28210006Srdivacky///   %t0 = getelementptr \@a, 0, 3
29210006Srdivacky///   store i32 0, i32* %t0
30210006Srdivacky///   %t1 = getelementptr \@a, 0, 3
31210006Srdivacky///   %t2 = load i32* %t1
32210006Srdivacky///
33210006Srdivackystatic bool AreEquivalentAddressValues(const Value *A, const Value *B) {
34210006Srdivacky  // Test if the values are trivially equivalent.
35210006Srdivacky  if (A == B) return true;
36223017Sdim
37210006Srdivacky  // Test if the values come from identical arithmetic instructions.
38210006Srdivacky  // Use isIdenticalToWhenDefined instead of isIdenticalTo because
39210006Srdivacky  // this function is only used when one address use dominates the
40210006Srdivacky  // other, which means that they'll always either have the same
41210006Srdivacky  // value or one of them will have an undefined value.
42210006Srdivacky  if (isa<BinaryOperator>(A) || isa<CastInst>(A) ||
43210006Srdivacky      isa<PHINode>(A) || isa<GetElementPtrInst>(A))
44210006Srdivacky    if (const Instruction *BI = dyn_cast<Instruction>(B))
45210006Srdivacky      if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
46210006Srdivacky        return true;
47223017Sdim
48210006Srdivacky  // Otherwise they may not be equivalent.
49210006Srdivacky  return false;
50210006Srdivacky}
51210006Srdivacky
52210006Srdivacky/// isSafeToLoadUnconditionally - Return true if we know that executing a load
53210006Srdivacky/// from this value cannot trap.  If it is not obviously safe to load from the
54210006Srdivacky/// specified pointer, we do a quick local scan of the basic block containing
55210006Srdivacky/// ScanFrom, to determine if the address is already accessed.
56210006Srdivackybool llvm::isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom,
57243830Sdim                                       unsigned Align, const DataLayout *TD) {
58249423Sdim  int64_t ByteOffset = 0;
59210006Srdivacky  Value *Base = V;
60249423Sdim  Base = GetPointerBaseWithConstantOffset(V, ByteOffset, TD);
61210006Srdivacky
62249423Sdim  if (ByteOffset < 0) // out of bounds
63249423Sdim    return false;
64249423Sdim
65226633Sdim  Type *BaseType = 0;
66210006Srdivacky  unsigned BaseAlign = 0;
67210006Srdivacky  if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
68210006Srdivacky    // An alloca is safe to load from as load as it is suitably aligned.
69210006Srdivacky    BaseType = AI->getAllocatedType();
70210006Srdivacky    BaseAlign = AI->getAlignment();
71249423Sdim  } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
72210006Srdivacky    // Global variables are safe to load from but their size cannot be
73210006Srdivacky    // guaranteed if they are overridden.
74249423Sdim    if (!GV->mayBeOverridden()) {
75210006Srdivacky      BaseType = GV->getType()->getElementType();
76210006Srdivacky      BaseAlign = GV->getAlignment();
77210006Srdivacky    }
78210006Srdivacky  }
79210006Srdivacky
80210006Srdivacky  if (BaseType && BaseType->isSized()) {
81210006Srdivacky    if (TD && BaseAlign == 0)
82210006Srdivacky      BaseAlign = TD->getPrefTypeAlignment(BaseType);
83210006Srdivacky
84210006Srdivacky    if (Align <= BaseAlign) {
85210006Srdivacky      if (!TD)
86210006Srdivacky        return true; // Loading directly from an alloca or global is OK.
87210006Srdivacky
88210006Srdivacky      // Check if the load is within the bounds of the underlying object.
89226633Sdim      PointerType *AddrTy = cast<PointerType>(V->getType());
90210006Srdivacky      uint64_t LoadSize = TD->getTypeStoreSize(AddrTy->getElementType());
91210006Srdivacky      if (ByteOffset + LoadSize <= TD->getTypeAllocSize(BaseType) &&
92210006Srdivacky          (Align == 0 || (ByteOffset % Align) == 0))
93210006Srdivacky        return true;
94210006Srdivacky    }
95210006Srdivacky  }
96210006Srdivacky
97210006Srdivacky  // Otherwise, be a little bit aggressive by scanning the local block where we
98210006Srdivacky  // want to check to see if the pointer is already being loaded or stored
99210006Srdivacky  // from/to.  If so, the previous load or store would have already trapped,
100210006Srdivacky  // so there is no harm doing an extra load (also, CSE will later eliminate
101210006Srdivacky  // the load entirely).
102210006Srdivacky  BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
103210006Srdivacky
104210006Srdivacky  while (BBI != E) {
105210006Srdivacky    --BBI;
106210006Srdivacky
107210006Srdivacky    // If we see a free or a call which may write to memory (i.e. which might do
108210006Srdivacky    // a free) the pointer could be marked invalid.
109210006Srdivacky    if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
110210006Srdivacky        !isa<DbgInfoIntrinsic>(BBI))
111210006Srdivacky      return false;
112210006Srdivacky
113210006Srdivacky    if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
114210006Srdivacky      if (AreEquivalentAddressValues(LI->getOperand(0), V)) return true;
115210006Srdivacky    } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
116210006Srdivacky      if (AreEquivalentAddressValues(SI->getOperand(1), V)) return true;
117210006Srdivacky    }
118210006Srdivacky  }
119210006Srdivacky  return false;
120210006Srdivacky}
121210006Srdivacky
122210006Srdivacky/// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the
123210006Srdivacky/// instruction before ScanFrom) checking to see if we have the value at the
124210006Srdivacky/// memory address *Ptr locally available within a small number of instructions.
125210006Srdivacky/// If the value is available, return it.
126210006Srdivacky///
127210006Srdivacky/// If not, return the iterator for the last validated instruction that the
128210006Srdivacky/// value would be live through.  If we scanned the entire block and didn't find
129210006Srdivacky/// something that invalidates *Ptr or provides it, ScanFrom would be left at
130210006Srdivacky/// begin() and this returns null.  ScanFrom could also be left
131210006Srdivacky///
132210006Srdivacky/// MaxInstsToScan specifies the maximum instructions to scan in the block.  If
133210006Srdivacky/// it is set to 0, it will scan the whole block. You can also optionally
134210006Srdivacky/// specify an alias analysis implementation, which makes this more precise.
135234353Sdim///
136234353Sdim/// If TBAATag is non-null and a load or store is found, the TBAA tag from the
137234353Sdim/// load or store is recorded there.  If there is no TBAA tag or if no access
138234353Sdim/// is found, it is left unmodified.
139210006SrdivackyValue *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB,
140210006Srdivacky                                      BasicBlock::iterator &ScanFrom,
141210006Srdivacky                                      unsigned MaxInstsToScan,
142234353Sdim                                      AliasAnalysis *AA,
143234353Sdim                                      MDNode **TBAATag) {
144210006Srdivacky  if (MaxInstsToScan == 0) MaxInstsToScan = ~0U;
145210006Srdivacky
146210006Srdivacky  // If we're using alias analysis to disambiguate get the size of *Ptr.
147218893Sdim  uint64_t AccessSize = 0;
148210006Srdivacky  if (AA) {
149226633Sdim    Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType();
150210006Srdivacky    AccessSize = AA->getTypeStoreSize(AccessTy);
151210006Srdivacky  }
152210006Srdivacky
153210006Srdivacky  while (ScanFrom != ScanBB->begin()) {
154210006Srdivacky    // We must ignore debug info directives when counting (otherwise they
155210006Srdivacky    // would affect codegen).
156210006Srdivacky    Instruction *Inst = --ScanFrom;
157210006Srdivacky    if (isa<DbgInfoIntrinsic>(Inst))
158210006Srdivacky      continue;
159210006Srdivacky
160210006Srdivacky    // Restore ScanFrom to expected value in case next test succeeds
161210006Srdivacky    ScanFrom++;
162210006Srdivacky
163210006Srdivacky    // Don't scan huge blocks.
164210006Srdivacky    if (MaxInstsToScan-- == 0) return 0;
165210006Srdivacky
166210006Srdivacky    --ScanFrom;
167210006Srdivacky    // If this is a load of Ptr, the loaded value is available.
168226633Sdim    // (This is true even if the load is volatile or atomic, although
169226633Sdim    // those cases are unlikely.)
170210006Srdivacky    if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
171234353Sdim      if (AreEquivalentAddressValues(LI->getOperand(0), Ptr)) {
172234353Sdim        if (TBAATag) *TBAATag = LI->getMetadata(LLVMContext::MD_tbaa);
173210006Srdivacky        return LI;
174234353Sdim      }
175210006Srdivacky
176210006Srdivacky    if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
177210006Srdivacky      // If this is a store through Ptr, the value is available!
178226633Sdim      // (This is true even if the store is volatile or atomic, although
179226633Sdim      // those cases are unlikely.)
180234353Sdim      if (AreEquivalentAddressValues(SI->getOperand(1), Ptr)) {
181234353Sdim        if (TBAATag) *TBAATag = SI->getMetadata(LLVMContext::MD_tbaa);
182210006Srdivacky        return SI->getOperand(0);
183234353Sdim      }
184210006Srdivacky
185210006Srdivacky      // If Ptr is an alloca and this is a store to a different alloca, ignore
186210006Srdivacky      // the store.  This is a trivial form of alias analysis that is important
187210006Srdivacky      // for reg2mem'd code.
188210006Srdivacky      if ((isa<AllocaInst>(Ptr) || isa<GlobalVariable>(Ptr)) &&
189210006Srdivacky          (isa<AllocaInst>(SI->getOperand(1)) ||
190210006Srdivacky           isa<GlobalVariable>(SI->getOperand(1))))
191210006Srdivacky        continue;
192210006Srdivacky
193210006Srdivacky      // If we have alias analysis and it says the store won't modify the loaded
194210006Srdivacky      // value, ignore the store.
195210006Srdivacky      if (AA &&
196210006Srdivacky          (AA->getModRefInfo(SI, Ptr, AccessSize) & AliasAnalysis::Mod) == 0)
197210006Srdivacky        continue;
198210006Srdivacky
199210006Srdivacky      // Otherwise the store that may or may not alias the pointer, bail out.
200210006Srdivacky      ++ScanFrom;
201210006Srdivacky      return 0;
202210006Srdivacky    }
203210006Srdivacky
204210006Srdivacky    // If this is some other instruction that may clobber Ptr, bail out.
205210006Srdivacky    if (Inst->mayWriteToMemory()) {
206210006Srdivacky      // If alias analysis claims that it really won't modify the load,
207210006Srdivacky      // ignore it.
208210006Srdivacky      if (AA &&
209210006Srdivacky          (AA->getModRefInfo(Inst, Ptr, AccessSize) & AliasAnalysis::Mod) == 0)
210210006Srdivacky        continue;
211210006Srdivacky
212210006Srdivacky      // May modify the pointer, bail out.
213210006Srdivacky      ++ScanFrom;
214210006Srdivacky      return 0;
215210006Srdivacky    }
216210006Srdivacky  }
217210006Srdivacky
218210006Srdivacky  // Got to the start of the block, we didn't find it, but are done for this
219210006Srdivacky  // block.
220210006Srdivacky  return 0;
221210006Srdivacky}
222