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/Module.h"
23 #include "llvm/IR/Operator.h"
24 using namespace llvm;
25 
26 /// \brief Test if A and B will obviously have the same value.
27 ///
28 /// This includes recognizing that %t0 and %t1 will have the same
29 /// value in code like this:
30 /// \code
31 ///   %t0 = getelementptr \@a, 0, 3
32 ///   store i32 0, i32* %t0
33 ///   %t1 = getelementptr \@a, 0, 3
34 ///   %t2 = load i32* %t1
35 /// \endcode
36 ///
37 static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
38   // Test if the values are trivially equivalent.
39   if (A == B)
40     return true;
41 
42   // Test if the values come from identical arithmetic instructions.
43   // Use isIdenticalToWhenDefined instead of isIdenticalTo because
44   // this function is only used when one address use dominates the
45   // other, which means that they'll always either have the same
46   // value or one of them will have an undefined value.
47   if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
48       isa<GetElementPtrInst>(A))
49     if (const Instruction *BI = dyn_cast<Instruction>(B))
50       if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
51         return true;
52 
53   // Otherwise they may not be equivalent.
54   return false;
55 }
56 
57 /// \brief Check if executing a load of this pointer value cannot trap.
58 ///
59 /// If it is not obviously safe to load from the specified pointer, we do
60 /// a quick local scan of the basic block containing \c ScanFrom, to determine
61 /// if the address is already accessed.
62 ///
63 /// This uses the pointee type to determine how many bytes need to be safe to
64 /// load from the pointer.
65 bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align,
66                                        Instruction *ScanFrom) {
67   const DataLayout &DL = ScanFrom->getModule()->getDataLayout();
68 
69   // Zero alignment means that the load has the ABI alignment for the target
70   if (Align == 0)
71     Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
72   assert(isPowerOf2_32(Align));
73 
74   if (isDereferenceableAndAlignedPointer(V, Align, DL))
75     return true;
76 
77   int64_t ByteOffset = 0;
78   Value *Base = V;
79   Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL);
80 
81   if (ByteOffset < 0) // out of bounds
82     return false;
83 
84   Type *BaseType = nullptr;
85   unsigned BaseAlign = 0;
86   if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
87     // An alloca is safe to load from as load as it is suitably aligned.
88     BaseType = AI->getAllocatedType();
89     BaseAlign = AI->getAlignment();
90   } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
91     // Global variables are not necessarily safe to load from if they are
92     // overridden. Their size may change or they may be weak and require a test
93     // to determine if they were in fact provided.
94     if (!GV->mayBeOverridden()) {
95       BaseType = GV->getType()->getElementType();
96       BaseAlign = GV->getAlignment();
97     }
98   }
99 
100   PointerType *AddrTy = cast<PointerType>(V->getType());
101   uint64_t LoadSize = DL.getTypeStoreSize(AddrTy->getElementType());
102 
103   // If we found a base allocated type from either an alloca or global variable,
104   // try to see if we are definitively within the allocated region. We need to
105   // know the size of the base type and the loaded type to do anything in this
106   // case.
107   if (BaseType && BaseType->isSized()) {
108     if (BaseAlign == 0)
109       BaseAlign = DL.getPrefTypeAlignment(BaseType);
110 
111     if (Align <= BaseAlign) {
112       // Check if the load is within the bounds of the underlying object.
113       if (ByteOffset + LoadSize <= DL.getTypeAllocSize(BaseType) &&
114           ((ByteOffset % Align) == 0))
115         return true;
116     }
117   }
118 
119   // Otherwise, be a little bit aggressive by scanning the local block where we
120   // want to check to see if the pointer is already being loaded or stored
121   // from/to.  If so, the previous load or store would have already trapped,
122   // so there is no harm doing an extra load (also, CSE will later eliminate
123   // the load entirely).
124   BasicBlock::iterator BBI = ScanFrom->getIterator(),
125                        E = ScanFrom->getParent()->begin();
126 
127   // We can at least always strip pointer casts even though we can't use the
128   // base here.
129   V = V->stripPointerCasts();
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     Value *AccessedPtr;
141     unsigned AccessedAlign;
142     if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
143       AccessedPtr = LI->getPointerOperand();
144       AccessedAlign = LI->getAlignment();
145     } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
146       AccessedPtr = SI->getPointerOperand();
147       AccessedAlign = SI->getAlignment();
148     } else
149       continue;
150 
151     Type *AccessedTy = AccessedPtr->getType()->getPointerElementType();
152     if (AccessedAlign == 0)
153       AccessedAlign = DL.getABITypeAlignment(AccessedTy);
154     if (AccessedAlign < Align)
155       continue;
156 
157     // Handle trivial cases.
158     if (AccessedPtr == V)
159       return true;
160 
161     if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
162         LoadSize <= DL.getTypeStoreSize(AccessedTy))
163       return true;
164   }
165   return false;
166 }
167 
168 /// DefMaxInstsToScan - the default number of maximum instructions
169 /// to scan in the block, used by FindAvailableLoadedValue().
170 /// FindAvailableLoadedValue() was introduced in r60148, to improve jump
171 /// threading in part by eliminating partially redundant loads.
172 /// At that point, the value of MaxInstsToScan was already set to '6'
173 /// without documented explanation.
174 cl::opt<unsigned>
175 llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
176   cl::desc("Use this to specify the default maximum number of instructions "
177            "to scan backward from a given instruction, when searching for "
178            "available loaded value"));
179 
180 /// \brief Scan the ScanBB block backwards to see if we have the value at the
181 /// memory address *Ptr locally available within a small number of instructions.
182 ///
183 /// The scan starts from \c ScanFrom. \c MaxInstsToScan specifies the maximum
184 /// instructions to scan in the block. If it is set to \c 0, it will scan the whole
185 /// block.
186 ///
187 /// If the value is available, this function returns it. If not, it returns the
188 /// iterator for the last validated instruction that the value would be live
189 /// through. If we scanned the entire block and didn't find something that
190 /// invalidates \c *Ptr or provides it, \c ScanFrom is left at the last
191 /// instruction processed and this returns null.
192 ///
193 /// You can also optionally specify an alias analysis implementation, which
194 /// makes this more precise.
195 ///
196 /// If \c AATags is non-null and a load or store is found, the AA tags from the
197 /// load or store are recorded there. If there are no AA tags or if no access is
198 /// found, it is left unmodified.
199 Value *llvm::FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB,
200                                       BasicBlock::iterator &ScanFrom,
201                                       unsigned MaxInstsToScan,
202                                       AliasAnalysis *AA, AAMDNodes *AATags) {
203   if (MaxInstsToScan == 0)
204     MaxInstsToScan = ~0U;
205 
206   Value *Ptr = Load->getPointerOperand();
207   Type *AccessTy = Load->getType();
208 
209   const DataLayout &DL = ScanBB->getModule()->getDataLayout();
210 
211   // Try to get the store size for the type.
212   uint64_t AccessSize = DL.getTypeStoreSize(AccessTy);
213 
214   Value *StrippedPtr = Ptr->stripPointerCasts();
215 
216   while (ScanFrom != ScanBB->begin()) {
217     // We must ignore debug info directives when counting (otherwise they
218     // would affect codegen).
219     Instruction *Inst = &*--ScanFrom;
220     if (isa<DbgInfoIntrinsic>(Inst))
221       continue;
222 
223     // Restore ScanFrom to expected value in case next test succeeds
224     ScanFrom++;
225 
226     // Don't scan huge blocks.
227     if (MaxInstsToScan-- == 0)
228       return nullptr;
229 
230     --ScanFrom;
231     // If this is a load of Ptr, the loaded value is available.
232     // (This is true even if the load is volatile or atomic, although
233     // those cases are unlikely.)
234     if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
235       if (AreEquivalentAddressValues(
236               LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) &&
237           CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
238         if (AATags)
239           LI->getAAMetadata(*AATags);
240         return LI;
241       }
242 
243     if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
244       Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
245       // If this is a store through Ptr, the value is available!
246       // (This is true even if the store is volatile or atomic, although
247       // those cases are unlikely.)
248       if (AreEquivalentAddressValues(StorePtr, StrippedPtr) &&
249           CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(),
250                                                AccessTy, DL)) {
251         if (AATags)
252           SI->getAAMetadata(*AATags);
253         return SI->getOperand(0);
254       }
255 
256       // If both StrippedPtr and StorePtr reach all the way to an alloca or
257       // global and they are different, ignore the store. This is a trivial form
258       // of alias analysis that is important for reg2mem'd code.
259       if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
260           (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
261           StrippedPtr != StorePtr)
262         continue;
263 
264       // If we have alias analysis and it says the store won't modify the loaded
265       // value, ignore the store.
266       if (AA && (AA->getModRefInfo(SI, StrippedPtr, AccessSize) & MRI_Mod) == 0)
267         continue;
268 
269       // Otherwise the store that may or may not alias the pointer, bail out.
270       ++ScanFrom;
271       return nullptr;
272     }
273 
274     // If this is some other instruction that may clobber Ptr, bail out.
275     if (Inst->mayWriteToMemory()) {
276       // If alias analysis claims that it really won't modify the load,
277       // ignore it.
278       if (AA &&
279           (AA->getModRefInfo(Inst, StrippedPtr, AccessSize) & MRI_Mod) == 0)
280         continue;
281 
282       // May modify the pointer, bail out.
283       ++ScanFrom;
284       return nullptr;
285     }
286   }
287 
288   // Got to the start of the block, we didn't find it, but are done for this
289   // block.
290   return nullptr;
291 }
292