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" |
| 22 | #include "llvm/IR/Operator.h" |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chandler Carruth | b56052f | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 25 | /// \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 Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 28 | /// value in code like this: |
Chandler Carruth | b56052f | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 29 | /// \code |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 30 | /// %t0 = getelementptr \@a, 0, 3 |
| 31 | /// store i32 0, i32* %t0 |
| 32 | /// %t1 = getelementptr \@a, 0, 3 |
| 33 | /// %t2 = load i32* %t1 |
Chandler Carruth | b56052f | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 34 | /// \endcode |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 35 | /// |
| 36 | static bool AreEquivalentAddressValues(const Value *A, const Value *B) { |
| 37 | // Test if the values are trivially equivalent. |
Chandler Carruth | be49df3 | 2014-10-18 23:41:25 +0000 | [diff] [blame] | 38 | if (A == B) |
| 39 | return true; |
Hans Wennborg | 060b994 | 2011-06-03 17:15:37 +0000 | [diff] [blame] | 40 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 41 | // 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 Carruth | be49df3 | 2014-10-18 23:41:25 +0000 | [diff] [blame] | 46 | if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) || |
| 47 | isa<GetElementPtrInst>(A)) |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 48 | if (const Instruction *BI = dyn_cast<Instruction>(B)) |
| 49 | if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) |
| 50 | return true; |
Hans Wennborg | 060b994 | 2011-06-03 17:15:37 +0000 | [diff] [blame] | 51 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 52 | // Otherwise they may not be equivalent. |
| 53 | return false; |
| 54 | } |
| 55 | |
Chandler Carruth | 1f27f03 | 2014-10-18 23:46:17 +0000 | [diff] [blame] | 56 | /// \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 Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 64 | bool llvm::isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom, |
Chandler Carruth | 38e98d5 | 2014-10-18 23:47:22 +0000 | [diff] [blame] | 65 | unsigned Align, const DataLayout *DL) { |
Nuno Lopes | 69dcc7d | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 66 | int64_t ByteOffset = 0; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 67 | Value *Base = V; |
Chandler Carruth | 38e98d5 | 2014-10-18 23:47:22 +0000 | [diff] [blame] | 68 | Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL); |
Nuno Lopes | 69dcc7d | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 69 | |
| 70 | if (ByteOffset < 0) // out of bounds |
| 71 | return false; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 72 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 73 | Type *BaseType = nullptr; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 74 | unsigned BaseAlign = 0; |
| 75 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) { |
| 76 | // An alloca is safe to load from as load as it is suitably aligned. |
| 77 | BaseType = AI->getAllocatedType(); |
| 78 | BaseAlign = AI->getAlignment(); |
Nuno Lopes | 69dcc7d | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 79 | } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) { |
Chandler Carruth | 8a99373 | 2014-10-19 00:42:16 +0000 | [diff] [blame] | 80 | // Global variables are not necessarily safe to load from if they are |
| 81 | // overridden. Their size may change or they may be weak and require a test |
| 82 | // to determine if they were in fact provided. |
Nuno Lopes | 69dcc7d | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 83 | if (!GV->mayBeOverridden()) { |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 84 | BaseType = GV->getType()->getElementType(); |
| 85 | BaseAlign = GV->getAlignment(); |
| 86 | } |
| 87 | } |
| 88 | |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 89 | PointerType *AddrTy = cast<PointerType>(V->getType()); |
| 90 | uint64_t LoadSize = DL ? DL->getTypeStoreSize(AddrTy->getElementType()) : 0; |
| 91 | |
Chandler Carruth | 8a99373 | 2014-10-19 00:42:16 +0000 | [diff] [blame] | 92 | // If we found a base allocated type from either an alloca or global variable, |
| 93 | // try to see if we are definitively within the allocated region. We need to |
| 94 | // know the size of the base type and the loaded type to do anything in this |
| 95 | // case, so only try this when we have the DataLayout available. |
| 96 | if (BaseType && BaseType->isSized() && DL) { |
| 97 | if (BaseAlign == 0) |
Chandler Carruth | 38e98d5 | 2014-10-18 23:47:22 +0000 | [diff] [blame] | 98 | BaseAlign = DL->getPrefTypeAlignment(BaseType); |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 99 | |
| 100 | if (Align <= BaseAlign) { |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 101 | // Check if the load is within the bounds of the underlying object. |
Chandler Carruth | 38e98d5 | 2014-10-18 23:47:22 +0000 | [diff] [blame] | 102 | if (ByteOffset + LoadSize <= DL->getTypeAllocSize(BaseType) && |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 103 | (Align == 0 || (ByteOffset % Align) == 0)) |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Otherwise, be a little bit aggressive by scanning the local block where we |
| 109 | // want to check to see if the pointer is already being loaded or stored |
| 110 | // from/to. If so, the previous load or store would have already trapped, |
| 111 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 112 | // the load entirely). |
| 113 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
| 114 | |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 115 | // We can at least always strip pointer casts even though we can't use the |
| 116 | // base here. |
| 117 | V = V->stripPointerCasts(); |
| 118 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 119 | while (BBI != E) { |
| 120 | --BBI; |
| 121 | |
| 122 | // If we see a free or a call which may write to memory (i.e. which might do |
| 123 | // a free) the pointer could be marked invalid. |
| 124 | if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() && |
| 125 | !isa<DbgInfoIntrinsic>(BBI)) |
| 126 | return false; |
| 127 | |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 128 | Value *AccessedPtr; |
| 129 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) |
| 130 | AccessedPtr = LI->getPointerOperand(); |
| 131 | else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
| 132 | AccessedPtr = SI->getPointerOperand(); |
| 133 | else |
| 134 | continue; |
| 135 | |
| 136 | // Handle trivial cases even w/o DataLayout or other work. |
| 137 | if (AccessedPtr == V) |
| 138 | return true; |
| 139 | |
| 140 | if (!DL) |
| 141 | continue; |
| 142 | |
| 143 | auto *AccessedTy = cast<PointerType>(AccessedPtr->getType()); |
| 144 | if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) && |
| 145 | LoadSize <= DL->getTypeStoreSize(AccessedTy->getElementType())) |
| 146 | return true; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
Chandler Carruth | b56052f | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 151 | /// \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] | 152 | /// memory address *Ptr locally available within a small number of instructions. |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 153 | /// |
Chandler Carruth | b56052f | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 154 | /// The scan starts from \c ScanFrom. \c MaxInstsToScan specifies the maximum |
| 155 | /// instructions to scan in the block. If it is set to \c 0, it will scan the whole |
| 156 | /// block. |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 157 | /// |
Chandler Carruth | b56052f | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 158 | /// If the value is available, this function returns it. If not, it returns the |
| 159 | /// iterator for the last validated instruction that the value would be live |
| 160 | /// through. If we scanned the entire block and didn't find something that |
| 161 | /// invalidates \c *Ptr or provides it, \c ScanFrom is left at the last |
| 162 | /// instruction processed and this returns null. |
Chris Lattner | 87fa77b | 2012-03-13 18:07:41 +0000 | [diff] [blame] | 163 | /// |
Chandler Carruth | b56052f | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 164 | /// You can also optionally specify an alias analysis implementation, which |
| 165 | /// makes this more precise. |
| 166 | /// |
| 167 | /// If \c AATags is non-null and a load or store is found, the AA tags from the |
| 168 | /// load or store are recorded there. If there are no AA tags or if no access is |
| 169 | /// found, it is left unmodified. |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 170 | Value *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB, |
| 171 | BasicBlock::iterator &ScanFrom, |
| 172 | unsigned MaxInstsToScan, |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 173 | AliasAnalysis *AA, AAMDNodes *AATags) { |
| 174 | if (MaxInstsToScan == 0) |
| 175 | MaxInstsToScan = ~0U; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 176 | |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 177 | Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType(); |
| 178 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 179 | // If we're using alias analysis to disambiguate get the size of *Ptr. |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 180 | uint64_t AccessSize = AA ? AA->getTypeStoreSize(AccessTy) : 0; |
| 181 | |
| 182 | Value *StrippedPtr = Ptr->stripPointerCasts(); |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 183 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 184 | while (ScanFrom != ScanBB->begin()) { |
| 185 | // We must ignore debug info directives when counting (otherwise they |
| 186 | // would affect codegen). |
| 187 | Instruction *Inst = --ScanFrom; |
| 188 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 189 | continue; |
| 190 | |
| 191 | // Restore ScanFrom to expected value in case next test succeeds |
| 192 | ScanFrom++; |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 193 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 194 | // Don't scan huge blocks. |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 195 | if (MaxInstsToScan-- == 0) |
| 196 | return nullptr; |
| 197 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 198 | --ScanFrom; |
| 199 | // If this is a load of Ptr, the loaded value is available. |
Eli Friedman | 4419cd2 | 2011-08-15 21:56:39 +0000 | [diff] [blame] | 200 | // (This is true even if the load is volatile or atomic, although |
| 201 | // those cases are unlikely.) |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 202 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 203 | if (AreEquivalentAddressValues( |
| 204 | LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) && |
| 205 | CastInst::isBitCastable(LI->getType(), AccessTy)) { |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 206 | if (AATags) |
| 207 | LI->getAAMetadata(*AATags); |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 208 | return LI; |
Chris Lattner | 87fa77b | 2012-03-13 18:07:41 +0000 | [diff] [blame] | 209 | } |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 210 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 211 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 212 | Value *StorePtr = SI->getPointerOperand()->stripPointerCasts(); |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 213 | // If this is a store through Ptr, the value is available! |
Eli Friedman | 4419cd2 | 2011-08-15 21:56:39 +0000 | [diff] [blame] | 214 | // (This is true even if the store is volatile or atomic, although |
| 215 | // those cases are unlikely.) |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 216 | if (AreEquivalentAddressValues(StorePtr, StrippedPtr) && |
| 217 | CastInst::isBitCastable(SI->getValueOperand()->getType(), AccessTy)) { |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 218 | if (AATags) |
| 219 | SI->getAAMetadata(*AATags); |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 220 | return SI->getOperand(0); |
Chris Lattner | 87fa77b | 2012-03-13 18:07:41 +0000 | [diff] [blame] | 221 | } |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 222 | |
Chandler Carruth | a32038b | 2014-10-20 10:03:01 +0000 | [diff] [blame^] | 223 | // If both StrippedPtr and StorePtr reach all the way to an alloca or |
| 224 | // global and they are different, ignore the store. This is a trivial form |
| 225 | // of alias analysis that is important for reg2mem'd code. |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 226 | if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) && |
Chandler Carruth | a32038b | 2014-10-20 10:03:01 +0000 | [diff] [blame^] | 227 | (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) && |
| 228 | StrippedPtr != StorePtr) |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 229 | continue; |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 230 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 231 | // If we have alias analysis and it says the store won't modify the loaded |
| 232 | // value, ignore the store. |
| 233 | if (AA && |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 234 | (AA->getModRefInfo(SI, StrippedPtr, AccessSize) & |
| 235 | AliasAnalysis::Mod) == 0) |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 236 | continue; |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 237 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 238 | // Otherwise the store that may or may not alias the pointer, bail out. |
| 239 | ++ScanFrom; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 240 | return nullptr; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 241 | } |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 242 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 243 | // If this is some other instruction that may clobber Ptr, bail out. |
| 244 | if (Inst->mayWriteToMemory()) { |
| 245 | // If alias analysis claims that it really won't modify the load, |
| 246 | // ignore it. |
| 247 | if (AA && |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 248 | (AA->getModRefInfo(Inst, StrippedPtr, AccessSize) & |
| 249 | AliasAnalysis::Mod) == 0) |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 250 | continue; |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 251 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 252 | // May modify the pointer, bail out. |
| 253 | ++ScanFrom; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 254 | return nullptr; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 257 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 258 | // Got to the start of the block, we didn't find it, but are done for this |
| 259 | // block. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 260 | return nullptr; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 261 | } |