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