blob: f5eb7055726e59ffed6ca07142eb7fdc433de7fb [file] [log] [blame]
Dan Gohman826bdf82010-05-28 16:19:17 +00001//===- 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"
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000016#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#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"
Dan Gohman826bdf82010-05-28 16:19:17 +000023using namespace llvm;
24
Chandler Carruthb56052f2014-10-18 23:31:55 +000025/// \brief Test if A and B will obviously have the same value.
26///
27/// This includes recognizing that %t0 and %t1 will have the same
Dan Gohman826bdf82010-05-28 16:19:17 +000028/// value in code like this:
Chandler Carruthb56052f2014-10-18 23:31:55 +000029/// \code
Dan Gohman826bdf82010-05-28 16:19:17 +000030/// %t0 = getelementptr \@a, 0, 3
31/// store i32 0, i32* %t0
32/// %t1 = getelementptr \@a, 0, 3
33/// %t2 = load i32* %t1
Chandler Carruthb56052f2014-10-18 23:31:55 +000034/// \endcode
Dan Gohman826bdf82010-05-28 16:19:17 +000035///
36static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
37 // Test if the values are trivially equivalent.
Chandler Carruthbe49df32014-10-18 23:41:25 +000038 if (A == B)
39 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +000040
Dan Gohman826bdf82010-05-28 16:19:17 +000041 // Test if the values come from identical arithmetic instructions.
42 // Use isIdenticalToWhenDefined instead of isIdenticalTo because
43 // this function is only used when one address use dominates the
44 // other, which means that they'll always either have the same
45 // value or one of them will have an undefined value.
Chandler Carruthbe49df32014-10-18 23:41:25 +000046 if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
47 isa<GetElementPtrInst>(A))
Dan Gohman826bdf82010-05-28 16:19:17 +000048 if (const Instruction *BI = dyn_cast<Instruction>(B))
49 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
50 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +000051
Dan Gohman826bdf82010-05-28 16:19:17 +000052 // Otherwise they may not be equivalent.
53 return false;
54}
55
Chandler Carruth1f27f032014-10-18 23:46:17 +000056/// \brief Check if executing a load of this pointer value cannot trap.
57///
58/// If it is not obviously safe to load from the specified pointer, we do
59/// a quick local scan of the basic block containing \c ScanFrom, to determine
60/// if the address is already accessed.
61///
62/// This uses the pointee type to determine how many bytes need to be safe to
63/// load from the pointer.
Dan Gohman826bdf82010-05-28 16:19:17 +000064bool llvm::isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom,
Chandler Carruth38e98d52014-10-18 23:47:22 +000065 unsigned Align, const DataLayout *DL) {
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000066 int64_t ByteOffset = 0;
Dan Gohman826bdf82010-05-28 16:19:17 +000067 Value *Base = V;
Chandler Carruth38e98d52014-10-18 23:47:22 +000068 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL);
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000069
70 if (ByteOffset < 0) // out of bounds
71 return false;
Dan Gohman826bdf82010-05-28 16:19:17 +000072
Craig Topper9f008862014-04-15 04:59:12 +000073 Type *BaseType = nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +000074 unsigned BaseAlign = 0;
75 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
Chandler Carruth8a993732014-10-19 00:42:16 +000076 // Loading directly from an alloca is trivially safe. We can't even look
77 // through pointer casts here though, as that might change the size loaded.
78 if (AI == V)
79 return true;
80
Dan Gohman826bdf82010-05-28 16:19:17 +000081 // An alloca is safe to load from as load as it is suitably aligned.
82 BaseType = AI->getAllocatedType();
83 BaseAlign = AI->getAlignment();
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000084 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
Chandler Carruth8a993732014-10-19 00:42:16 +000085 // Global variables are not necessarily safe to load from if they are
86 // overridden. Their size may change or they may be weak and require a test
87 // to determine if they were in fact provided.
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000088 if (!GV->mayBeOverridden()) {
Chandler Carruth8a993732014-10-19 00:42:16 +000089 // Loading directly from the non-overridden global is trivially safe. We
90 // can't even look through pointer casts here though, as that might change
91 // the size loaded.
92 if (GV == V)
93 return true;
94
Dan Gohman826bdf82010-05-28 16:19:17 +000095 BaseType = GV->getType()->getElementType();
96 BaseAlign = GV->getAlignment();
97 }
98 }
99
Chandler Carruth8a993732014-10-19 00:42:16 +0000100 // If we found a base allocated type from either an alloca or global variable,
101 // try to see if we are definitively within the allocated region. We need to
102 // know the size of the base type and the loaded type to do anything in this
103 // case, so only try this when we have the DataLayout available.
104 if (BaseType && BaseType->isSized() && DL) {
105 if (BaseAlign == 0)
Chandler Carruth38e98d52014-10-18 23:47:22 +0000106 BaseAlign = DL->getPrefTypeAlignment(BaseType);
Dan Gohman826bdf82010-05-28 16:19:17 +0000107
108 if (Align <= BaseAlign) {
Dan Gohman826bdf82010-05-28 16:19:17 +0000109 // Check if the load is within the bounds of the underlying object.
Chris Lattner229907c2011-07-18 04:54:35 +0000110 PointerType *AddrTy = cast<PointerType>(V->getType());
Chandler Carruth38e98d52014-10-18 23:47:22 +0000111 uint64_t LoadSize = DL->getTypeStoreSize(AddrTy->getElementType());
112 if (ByteOffset + LoadSize <= DL->getTypeAllocSize(BaseType) &&
Dan Gohman826bdf82010-05-28 16:19:17 +0000113 (Align == 0 || (ByteOffset % Align) == 0))
114 return true;
115 }
116 }
117
118 // Otherwise, be a little bit aggressive by scanning the local block where we
119 // want to check to see if the pointer is already being loaded or stored
120 // from/to. If so, the previous load or store would have already trapped,
121 // so there is no harm doing an extra load (also, CSE will later eliminate
122 // the load entirely).
123 BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
124
125 while (BBI != E) {
126 --BBI;
127
128 // If we see a free or a call which may write to memory (i.e. which might do
129 // a free) the pointer could be marked invalid.
130 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
131 !isa<DbgInfoIntrinsic>(BBI))
132 return false;
133
134 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Chandler Carruthbe49df32014-10-18 23:41:25 +0000135 if (AreEquivalentAddressValues(LI->getOperand(0), V))
136 return true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000137 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
Chandler Carruthbe49df32014-10-18 23:41:25 +0000138 if (AreEquivalentAddressValues(SI->getOperand(1), V))
139 return true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000140 }
141 }
142 return false;
143}
144
Chandler Carruthb56052f2014-10-18 23:31:55 +0000145/// \brief Scan the ScanBB block backwards to see if we have the value at the
Dan Gohman826bdf82010-05-28 16:19:17 +0000146/// memory address *Ptr locally available within a small number of instructions.
Dan Gohman826bdf82010-05-28 16:19:17 +0000147///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000148/// The scan starts from \c ScanFrom. \c MaxInstsToScan specifies the maximum
149/// instructions to scan in the block. If it is set to \c 0, it will scan the whole
150/// block.
Dan Gohman826bdf82010-05-28 16:19:17 +0000151///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000152/// If the value is available, this function returns it. If not, it returns the
153/// iterator for the last validated instruction that the value would be live
154/// through. If we scanned the entire block and didn't find something that
155/// invalidates \c *Ptr or provides it, \c ScanFrom is left at the last
156/// instruction processed and this returns null.
Chris Lattner87fa77b2012-03-13 18:07:41 +0000157///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000158/// You can also optionally specify an alias analysis implementation, which
159/// makes this more precise.
160///
161/// If \c AATags is non-null and a load or store is found, the AA tags from the
162/// load or store are recorded there. If there are no AA tags or if no access is
163/// found, it is left unmodified.
Dan Gohman826bdf82010-05-28 16:19:17 +0000164Value *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB,
165 BasicBlock::iterator &ScanFrom,
166 unsigned MaxInstsToScan,
Chandler Carruthd67244d2014-10-18 23:19:03 +0000167 AliasAnalysis *AA, AAMDNodes *AATags) {
168 if (MaxInstsToScan == 0)
169 MaxInstsToScan = ~0U;
Dan Gohman826bdf82010-05-28 16:19:17 +0000170
171 // If we're using alias analysis to disambiguate get the size of *Ptr.
Dan Gohmanf372cf82010-10-19 22:54:46 +0000172 uint64_t AccessSize = 0;
Dan Gohman826bdf82010-05-28 16:19:17 +0000173 if (AA) {
Chris Lattner229907c2011-07-18 04:54:35 +0000174 Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType();
Dan Gohman826bdf82010-05-28 16:19:17 +0000175 AccessSize = AA->getTypeStoreSize(AccessTy);
176 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000177
Dan Gohman826bdf82010-05-28 16:19:17 +0000178 while (ScanFrom != ScanBB->begin()) {
179 // We must ignore debug info directives when counting (otherwise they
180 // would affect codegen).
181 Instruction *Inst = --ScanFrom;
182 if (isa<DbgInfoIntrinsic>(Inst))
183 continue;
184
185 // Restore ScanFrom to expected value in case next test succeeds
186 ScanFrom++;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000187
Dan Gohman826bdf82010-05-28 16:19:17 +0000188 // Don't scan huge blocks.
Chandler Carruthd67244d2014-10-18 23:19:03 +0000189 if (MaxInstsToScan-- == 0)
190 return nullptr;
191
Dan Gohman826bdf82010-05-28 16:19:17 +0000192 --ScanFrom;
193 // If this is a load of Ptr, the loaded value is available.
Eli Friedman4419cd22011-08-15 21:56:39 +0000194 // (This is true even if the load is volatile or atomic, although
195 // those cases are unlikely.)
Dan Gohman826bdf82010-05-28 16:19:17 +0000196 if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
Chris Lattner87fa77b2012-03-13 18:07:41 +0000197 if (AreEquivalentAddressValues(LI->getOperand(0), Ptr)) {
Chandler Carruthd67244d2014-10-18 23:19:03 +0000198 if (AATags)
199 LI->getAAMetadata(*AATags);
Dan Gohman826bdf82010-05-28 16:19:17 +0000200 return LI;
Chris Lattner87fa77b2012-03-13 18:07:41 +0000201 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000202
Dan Gohman826bdf82010-05-28 16:19:17 +0000203 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
204 // If this is a store through Ptr, the value is available!
Eli Friedman4419cd22011-08-15 21:56:39 +0000205 // (This is true even if the store is volatile or atomic, although
206 // those cases are unlikely.)
Chris Lattner87fa77b2012-03-13 18:07:41 +0000207 if (AreEquivalentAddressValues(SI->getOperand(1), Ptr)) {
Chandler Carruthd67244d2014-10-18 23:19:03 +0000208 if (AATags)
209 SI->getAAMetadata(*AATags);
Dan Gohman826bdf82010-05-28 16:19:17 +0000210 return SI->getOperand(0);
Chris Lattner87fa77b2012-03-13 18:07:41 +0000211 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000212
Dan Gohman826bdf82010-05-28 16:19:17 +0000213 // If Ptr is an alloca and this is a store to a different alloca, ignore
214 // the store. This is a trivial form of alias analysis that is important
215 // for reg2mem'd code.
216 if ((isa<AllocaInst>(Ptr) || isa<GlobalVariable>(Ptr)) &&
217 (isa<AllocaInst>(SI->getOperand(1)) ||
218 isa<GlobalVariable>(SI->getOperand(1))))
219 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000220
Dan Gohman826bdf82010-05-28 16:19:17 +0000221 // If we have alias analysis and it says the store won't modify the loaded
222 // value, ignore the store.
223 if (AA &&
224 (AA->getModRefInfo(SI, Ptr, AccessSize) & AliasAnalysis::Mod) == 0)
225 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000226
Dan Gohman826bdf82010-05-28 16:19:17 +0000227 // Otherwise the store that may or may not alias the pointer, bail out.
228 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000229 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000230 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000231
Dan Gohman826bdf82010-05-28 16:19:17 +0000232 // If this is some other instruction that may clobber Ptr, bail out.
233 if (Inst->mayWriteToMemory()) {
234 // If alias analysis claims that it really won't modify the load,
235 // ignore it.
236 if (AA &&
237 (AA->getModRefInfo(Inst, Ptr, AccessSize) & AliasAnalysis::Mod) == 0)
238 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000239
Dan Gohman826bdf82010-05-28 16:19:17 +0000240 // May modify the pointer, bail out.
241 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000242 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000243 }
244 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000245
Dan Gohman826bdf82010-05-28 16:19:17 +0000246 // Got to the start of the block, we didn't find it, but are done for this
247 // block.
Craig Topper9f008862014-04-15 04:59:12 +0000248 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000249}