blob: c79c9afc0f72209860c47985b86ba8fa3fc79aea [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
Dan Gohman826bdf82010-05-28 16:19:17 +000056/// isSafeToLoadUnconditionally - Return true if we know that executing a load
57/// from this value cannot trap. If it is not obviously safe to load from the
58/// specified pointer, we do a quick local scan of the basic block containing
59/// ScanFrom, to determine if the address is already accessed.
60bool llvm::isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom,
Micah Villmowcdfe20b2012-10-08 16:38:25 +000061 unsigned Align, const DataLayout *TD) {
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000062 int64_t ByteOffset = 0;
Dan Gohman826bdf82010-05-28 16:19:17 +000063 Value *Base = V;
Dan Gohman20a2ae92013-01-31 02:00:45 +000064 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, TD);
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000065
66 if (ByteOffset < 0) // out of bounds
67 return false;
Dan Gohman826bdf82010-05-28 16:19:17 +000068
Craig Topper9f008862014-04-15 04:59:12 +000069 Type *BaseType = nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +000070 unsigned BaseAlign = 0;
71 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
72 // An alloca is safe to load from as load as it is suitably aligned.
73 BaseType = AI->getAllocatedType();
74 BaseAlign = AI->getAlignment();
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000075 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
Dan Gohman826bdf82010-05-28 16:19:17 +000076 // Global variables are safe to load from but their size cannot be
77 // guaranteed if they are overridden.
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000078 if (!GV->mayBeOverridden()) {
Dan Gohman826bdf82010-05-28 16:19:17 +000079 BaseType = GV->getType()->getElementType();
80 BaseAlign = GV->getAlignment();
81 }
82 }
83
84 if (BaseType && BaseType->isSized()) {
85 if (TD && BaseAlign == 0)
86 BaseAlign = TD->getPrefTypeAlignment(BaseType);
87
88 if (Align <= BaseAlign) {
89 if (!TD)
90 return true; // Loading directly from an alloca or global is OK.
91
92 // Check if the load is within the bounds of the underlying object.
Chris Lattner229907c2011-07-18 04:54:35 +000093 PointerType *AddrTy = cast<PointerType>(V->getType());
Dan Gohman826bdf82010-05-28 16:19:17 +000094 uint64_t LoadSize = TD->getTypeStoreSize(AddrTy->getElementType());
95 if (ByteOffset + LoadSize <= TD->getTypeAllocSize(BaseType) &&
96 (Align == 0 || (ByteOffset % Align) == 0))
97 return true;
98 }
99 }
100
101 // Otherwise, be a little bit aggressive by scanning the local block where we
102 // want to check to see if the pointer is already being loaded or stored
103 // from/to. If so, the previous load or store would have already trapped,
104 // so there is no harm doing an extra load (also, CSE will later eliminate
105 // the load entirely).
106 BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
107
108 while (BBI != E) {
109 --BBI;
110
111 // If we see a free or a call which may write to memory (i.e. which might do
112 // a free) the pointer could be marked invalid.
113 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
114 !isa<DbgInfoIntrinsic>(BBI))
115 return false;
116
117 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Chandler Carruthbe49df32014-10-18 23:41:25 +0000118 if (AreEquivalentAddressValues(LI->getOperand(0), V))
119 return true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000120 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
Chandler Carruthbe49df32014-10-18 23:41:25 +0000121 if (AreEquivalentAddressValues(SI->getOperand(1), V))
122 return true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000123 }
124 }
125 return false;
126}
127
Chandler Carruthb56052f2014-10-18 23:31:55 +0000128/// \brief Scan the ScanBB block backwards to see if we have the value at the
Dan Gohman826bdf82010-05-28 16:19:17 +0000129/// memory address *Ptr locally available within a small number of instructions.
Dan Gohman826bdf82010-05-28 16:19:17 +0000130///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000131/// The scan starts from \c ScanFrom. \c MaxInstsToScan specifies the maximum
132/// instructions to scan in the block. If it is set to \c 0, it will scan the whole
133/// block.
Dan Gohman826bdf82010-05-28 16:19:17 +0000134///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000135/// If the value is available, this function returns it. If not, it returns the
136/// iterator for the last validated instruction that the value would be live
137/// through. If we scanned the entire block and didn't find something that
138/// invalidates \c *Ptr or provides it, \c ScanFrom is left at the last
139/// instruction processed and this returns null.
Chris Lattner87fa77b2012-03-13 18:07:41 +0000140///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000141/// You can also optionally specify an alias analysis implementation, which
142/// makes this more precise.
143///
144/// If \c AATags is non-null and a load or store is found, the AA tags from the
145/// load or store are recorded there. If there are no AA tags or if no access is
146/// found, it is left unmodified.
Dan Gohman826bdf82010-05-28 16:19:17 +0000147Value *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB,
148 BasicBlock::iterator &ScanFrom,
149 unsigned MaxInstsToScan,
Chandler Carruthd67244d2014-10-18 23:19:03 +0000150 AliasAnalysis *AA, AAMDNodes *AATags) {
151 if (MaxInstsToScan == 0)
152 MaxInstsToScan = ~0U;
Dan Gohman826bdf82010-05-28 16:19:17 +0000153
154 // If we're using alias analysis to disambiguate get the size of *Ptr.
Dan Gohmanf372cf82010-10-19 22:54:46 +0000155 uint64_t AccessSize = 0;
Dan Gohman826bdf82010-05-28 16:19:17 +0000156 if (AA) {
Chris Lattner229907c2011-07-18 04:54:35 +0000157 Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType();
Dan Gohman826bdf82010-05-28 16:19:17 +0000158 AccessSize = AA->getTypeStoreSize(AccessTy);
159 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000160
Dan Gohman826bdf82010-05-28 16:19:17 +0000161 while (ScanFrom != ScanBB->begin()) {
162 // We must ignore debug info directives when counting (otherwise they
163 // would affect codegen).
164 Instruction *Inst = --ScanFrom;
165 if (isa<DbgInfoIntrinsic>(Inst))
166 continue;
167
168 // Restore ScanFrom to expected value in case next test succeeds
169 ScanFrom++;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000170
Dan Gohman826bdf82010-05-28 16:19:17 +0000171 // Don't scan huge blocks.
Chandler Carruthd67244d2014-10-18 23:19:03 +0000172 if (MaxInstsToScan-- == 0)
173 return nullptr;
174
Dan Gohman826bdf82010-05-28 16:19:17 +0000175 --ScanFrom;
176 // If this is a load of Ptr, the loaded value is available.
Eli Friedman4419cd22011-08-15 21:56:39 +0000177 // (This is true even if the load is volatile or atomic, although
178 // those cases are unlikely.)
Dan Gohman826bdf82010-05-28 16:19:17 +0000179 if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
Chris Lattner87fa77b2012-03-13 18:07:41 +0000180 if (AreEquivalentAddressValues(LI->getOperand(0), Ptr)) {
Chandler Carruthd67244d2014-10-18 23:19:03 +0000181 if (AATags)
182 LI->getAAMetadata(*AATags);
Dan Gohman826bdf82010-05-28 16:19:17 +0000183 return LI;
Chris Lattner87fa77b2012-03-13 18:07:41 +0000184 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000185
Dan Gohman826bdf82010-05-28 16:19:17 +0000186 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
187 // If this is a store through Ptr, the value is available!
Eli Friedman4419cd22011-08-15 21:56:39 +0000188 // (This is true even if the store is volatile or atomic, although
189 // those cases are unlikely.)
Chris Lattner87fa77b2012-03-13 18:07:41 +0000190 if (AreEquivalentAddressValues(SI->getOperand(1), Ptr)) {
Chandler Carruthd67244d2014-10-18 23:19:03 +0000191 if (AATags)
192 SI->getAAMetadata(*AATags);
Dan Gohman826bdf82010-05-28 16:19:17 +0000193 return SI->getOperand(0);
Chris Lattner87fa77b2012-03-13 18:07:41 +0000194 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000195
Dan Gohman826bdf82010-05-28 16:19:17 +0000196 // If Ptr is an alloca and this is a store to a different alloca, ignore
197 // the store. This is a trivial form of alias analysis that is important
198 // for reg2mem'd code.
199 if ((isa<AllocaInst>(Ptr) || isa<GlobalVariable>(Ptr)) &&
200 (isa<AllocaInst>(SI->getOperand(1)) ||
201 isa<GlobalVariable>(SI->getOperand(1))))
202 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000203
Dan Gohman826bdf82010-05-28 16:19:17 +0000204 // If we have alias analysis and it says the store won't modify the loaded
205 // value, ignore the store.
206 if (AA &&
207 (AA->getModRefInfo(SI, Ptr, AccessSize) & AliasAnalysis::Mod) == 0)
208 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000209
Dan Gohman826bdf82010-05-28 16:19:17 +0000210 // Otherwise the store that may or may not alias the pointer, bail out.
211 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000212 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000213 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000214
Dan Gohman826bdf82010-05-28 16:19:17 +0000215 // If this is some other instruction that may clobber Ptr, bail out.
216 if (Inst->mayWriteToMemory()) {
217 // If alias analysis claims that it really won't modify the load,
218 // ignore it.
219 if (AA &&
220 (AA->getModRefInfo(Inst, Ptr, AccessSize) & AliasAnalysis::Mod) == 0)
221 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000222
Dan Gohman826bdf82010-05-28 16:19:17 +0000223 // May modify the pointer, bail out.
224 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000225 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000226 }
227 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000228
Dan Gohman826bdf82010-05-28 16:19:17 +0000229 // Got to the start of the block, we didn't find it, but are done for this
230 // block.
Craig Topper9f008862014-04-15 04:59:12 +0000231 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000232}