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" |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Statepoint.h" |
| 25 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 28 | static bool isAligned(const Value *Base, const APInt &Offset, unsigned Align, |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 29 | const DataLayout &DL) { |
| 30 | APInt BaseAlign(Offset.getBitWidth(), Base->getPointerAlignment(DL)); |
| 31 | |
| 32 | if (!BaseAlign) { |
| 33 | Type *Ty = Base->getType()->getPointerElementType(); |
| 34 | if (!Ty->isSized()) |
| 35 | return false; |
| 36 | BaseAlign = DL.getABITypeAlignment(Ty); |
| 37 | } |
| 38 | |
| 39 | APInt Alignment(Offset.getBitWidth(), Align); |
| 40 | |
| 41 | assert(Alignment.isPowerOf2() && "must be a power of 2!"); |
| 42 | return BaseAlign.uge(Alignment) && !(Offset & (Alignment-1)); |
| 43 | } |
| 44 | |
| 45 | static bool isAligned(const Value *Base, unsigned Align, const DataLayout &DL) { |
| 46 | Type *Ty = Base->getType(); |
| 47 | assert(Ty->isSized() && "must be sized"); |
| 48 | APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0); |
| 49 | return isAligned(Base, Offset, Align, DL); |
| 50 | } |
| 51 | |
| 52 | /// Test if V is always a pointer to allocated and suitably aligned memory for |
| 53 | /// a simple load or store. |
| 54 | static bool isDereferenceableAndAlignedPointer( |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 55 | const Value *V, unsigned Align, const APInt &Size, const DataLayout &DL, |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 56 | const Instruction *CtxI, const DominatorTree *DT, |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 57 | SmallPtrSetImpl<const Value *> &Visited) { |
David Majnemer | a90e51e | 2016-08-31 03:22:32 +0000 | [diff] [blame] | 58 | // Already visited? Bail out, we've likely hit unreachable code. |
| 59 | if (!Visited.insert(V).second) |
| 60 | return false; |
| 61 | |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 62 | // Note that it is not safe to speculate into a malloc'd region because |
| 63 | // malloc may return null. |
| 64 | |
Sanjoy Das | 10df497 | 2016-06-01 16:47:45 +0000 | [diff] [blame] | 65 | // bitcast instructions are no-ops as far as dereferenceability is concerned. |
| 66 | if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) |
| 67 | return isDereferenceableAndAlignedPointer(BC->getOperand(0), Align, Size, |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 68 | DL, CtxI, DT, Visited); |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 69 | |
Sanjoy Das | 48cad71 | 2016-06-02 00:52:53 +0000 | [diff] [blame] | 70 | bool CheckForNonNull = false; |
| 71 | APInt KnownDerefBytes(Size.getBitWidth(), |
| 72 | V->getPointerDereferenceableBytes(DL, CheckForNonNull)); |
| 73 | if (KnownDerefBytes.getBoolValue()) { |
| 74 | if (KnownDerefBytes.uge(Size)) |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 75 | if (!CheckForNonNull || isKnownNonNullAt(V, CtxI, DT)) |
Sanjoy Das | 48cad71 | 2016-06-02 00:52:53 +0000 | [diff] [blame] | 76 | return isAligned(V, Align, DL); |
| 77 | } |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 78 | |
| 79 | // For GEPs, determine if the indexing lands within the allocated object. |
| 80 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 81 | const Value *Base = GEP->getPointerOperand(); |
| 82 | |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 83 | APInt Offset(DL.getPointerTypeSizeInBits(GEP->getType()), 0); |
Sanjoy Das | 10df497 | 2016-06-01 16:47:45 +0000 | [diff] [blame] | 84 | if (!GEP->accumulateConstantOffset(DL, Offset) || Offset.isNegative() || |
| 85 | !Offset.urem(APInt(Offset.getBitWidth(), Align)).isMinValue()) |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 86 | return false; |
| 87 | |
Sanjoy Das | 10df497 | 2016-06-01 16:47:45 +0000 | [diff] [blame] | 88 | // If the base pointer is dereferenceable for Offset+Size bytes, then the |
| 89 | // GEP (== Base + Offset) is dereferenceable for Size bytes. If the base |
| 90 | // pointer is aligned to Align bytes, and the Offset is divisible by Align |
| 91 | // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also |
| 92 | // aligned to Align bytes. |
| 93 | |
Tom Stellard | 13068995 | 2016-10-28 15:32:28 +0000 | [diff] [blame] | 94 | // Offset and Size may have different bit widths if we have visited an |
| 95 | // addrspacecast, so we can't do arithmetic directly on the APInt values. |
| 96 | return isDereferenceableAndAlignedPointer( |
| 97 | Base, Align, Offset + Size.sextOrTrunc(Offset.getBitWidth()), |
| 98 | DL, CtxI, DT, Visited); |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // For gc.relocate, look through relocations |
| 102 | if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V)) |
| 103 | return isDereferenceableAndAlignedPointer( |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 104 | RelocateInst->getDerivedPtr(), Align, Size, DL, CtxI, DT, Visited); |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 105 | |
| 106 | if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V)) |
Sanjoy Das | 10df497 | 2016-06-01 16:47:45 +0000 | [diff] [blame] | 107 | return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, Size, |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 108 | DL, CtxI, DT, Visited); |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 109 | |
Hal Finkel | bf3957a | 2016-07-11 03:08:49 +0000 | [diff] [blame] | 110 | if (auto CS = ImmutableCallSite(V)) |
| 111 | if (const Value *RV = CS.getReturnedArgOperand()) |
| 112 | return isDereferenceableAndAlignedPointer(RV, Align, Size, DL, CtxI, DT, |
| 113 | Visited); |
| 114 | |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 115 | // If we don't know, assume the worst. |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | bool llvm::isDereferenceableAndAlignedPointer(const Value *V, unsigned Align, |
| 120 | const DataLayout &DL, |
| 121 | const Instruction *CtxI, |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 122 | const DominatorTree *DT) { |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 123 | // When dereferenceability information is provided by a dereferenceable |
| 124 | // attribute, we know exactly how many bytes are dereferenceable. If we can |
| 125 | // determine the exact offset to the attributed variable, we can use that |
| 126 | // information here. |
| 127 | Type *VTy = V->getType(); |
| 128 | Type *Ty = VTy->getPointerElementType(); |
| 129 | |
| 130 | // Require ABI alignment for loads without alignment specification |
| 131 | if (Align == 0) |
| 132 | Align = DL.getABITypeAlignment(Ty); |
| 133 | |
Sanjoy Das | 10df497 | 2016-06-01 16:47:45 +0000 | [diff] [blame] | 134 | if (!Ty->isSized()) |
| 135 | return false; |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 136 | |
| 137 | SmallPtrSet<const Value *, 32> Visited; |
Sanjoy Das | 10df497 | 2016-06-01 16:47:45 +0000 | [diff] [blame] | 138 | return ::isDereferenceableAndAlignedPointer( |
| 139 | V, Align, APInt(DL.getTypeSizeInBits(VTy), DL.getTypeStoreSize(Ty)), DL, |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 140 | CtxI, DT, Visited); |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL, |
| 144 | const Instruction *CtxI, |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 145 | const DominatorTree *DT) { |
| 146 | return isDereferenceableAndAlignedPointer(V, 1, DL, CtxI, DT); |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chandler Carruth | b56052f | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 149 | /// \brief Test if A and B will obviously have the same value. |
| 150 | /// |
| 151 | /// This includes recognizing that %t0 and %t1 will have the same |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 152 | /// value in code like this: |
Chandler Carruth | b56052f | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 153 | /// \code |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 154 | /// %t0 = getelementptr \@a, 0, 3 |
| 155 | /// store i32 0, i32* %t0 |
| 156 | /// %t1 = getelementptr \@a, 0, 3 |
| 157 | /// %t2 = load i32* %t1 |
Chandler Carruth | b56052f | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 158 | /// \endcode |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 159 | /// |
| 160 | static bool AreEquivalentAddressValues(const Value *A, const Value *B) { |
| 161 | // Test if the values are trivially equivalent. |
Chandler Carruth | be49df3 | 2014-10-18 23:41:25 +0000 | [diff] [blame] | 162 | if (A == B) |
| 163 | return true; |
Hans Wennborg | 060b994 | 2011-06-03 17:15:37 +0000 | [diff] [blame] | 164 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 165 | // Test if the values come from identical arithmetic instructions. |
| 166 | // Use isIdenticalToWhenDefined instead of isIdenticalTo because |
| 167 | // this function is only used when one address use dominates the |
| 168 | // other, which means that they'll always either have the same |
| 169 | // value or one of them will have an undefined value. |
Chandler Carruth | be49df3 | 2014-10-18 23:41:25 +0000 | [diff] [blame] | 170 | if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) || |
| 171 | isa<GetElementPtrInst>(A)) |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 172 | if (const Instruction *BI = dyn_cast<Instruction>(B)) |
| 173 | if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) |
| 174 | return true; |
Hans Wennborg | 060b994 | 2011-06-03 17:15:37 +0000 | [diff] [blame] | 175 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 176 | // Otherwise they may not be equivalent. |
| 177 | return false; |
| 178 | } |
| 179 | |
Chandler Carruth | 1f27f03 | 2014-10-18 23:46:17 +0000 | [diff] [blame] | 180 | /// \brief Check if executing a load of this pointer value cannot trap. |
| 181 | /// |
Artur Pilipenko | 9bb6bea | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 182 | /// If DT and ScanFrom are specified this method performs context-sensitive |
| 183 | /// analysis and returns true if it is safe to load immediately before ScanFrom. |
Artur Pilipenko | 66d6d3e | 2016-02-11 13:42:59 +0000 | [diff] [blame] | 184 | /// |
Chandler Carruth | 1f27f03 | 2014-10-18 23:46:17 +0000 | [diff] [blame] | 185 | /// If it is not obviously safe to load from the specified pointer, we do |
| 186 | /// a quick local scan of the basic block containing \c ScanFrom, to determine |
| 187 | /// if the address is already accessed. |
| 188 | /// |
| 189 | /// This uses the pointee type to determine how many bytes need to be safe to |
| 190 | /// load from the pointer. |
Artur Pilipenko | 6dd6969 | 2016-01-15 15:27:46 +0000 | [diff] [blame] | 191 | bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align, |
Artur Pilipenko | 9bb6bea | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 192 | const DataLayout &DL, |
Artur Pilipenko | 66d6d3e | 2016-02-11 13:42:59 +0000 | [diff] [blame] | 193 | Instruction *ScanFrom, |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 194 | const DominatorTree *DT) { |
Artur Pilipenko | 0e21d54 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 195 | // Zero alignment means that the load has the ABI alignment for the target |
| 196 | if (Align == 0) |
| 197 | Align = DL.getABITypeAlignment(V->getType()->getPointerElementType()); |
| 198 | assert(isPowerOf2_32(Align)); |
| 199 | |
Artur Pilipenko | 66d6d3e | 2016-02-11 13:42:59 +0000 | [diff] [blame] | 200 | // If DT is not specified we can't make context-sensitive query |
| 201 | const Instruction* CtxI = DT ? ScanFrom : nullptr; |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 202 | if (isDereferenceableAndAlignedPointer(V, Align, DL, CtxI, DT)) |
Artur Pilipenko | f84dc06 | 2016-01-17 12:35:29 +0000 | [diff] [blame] | 203 | return true; |
| 204 | |
Nuno Lopes | 69dcc7d | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 205 | int64_t ByteOffset = 0; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 206 | Value *Base = V; |
Chandler Carruth | 38e98d5 | 2014-10-18 23:47:22 +0000 | [diff] [blame] | 207 | Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL); |
Nuno Lopes | 69dcc7d | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 208 | |
| 209 | if (ByteOffset < 0) // out of bounds |
| 210 | return false; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 211 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 212 | Type *BaseType = nullptr; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 213 | unsigned BaseAlign = 0; |
| 214 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) { |
| 215 | // An alloca is safe to load from as load as it is suitably aligned. |
| 216 | BaseType = AI->getAllocatedType(); |
| 217 | BaseAlign = AI->getAlignment(); |
Nuno Lopes | 69dcc7d | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 218 | } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) { |
Chandler Carruth | 8a99373 | 2014-10-19 00:42:16 +0000 | [diff] [blame] | 219 | // Global variables are not necessarily safe to load from if they are |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 220 | // interposed arbitrarily. Their size may change or they may be weak and |
| 221 | // require a test to determine if they were in fact provided. |
| 222 | if (!GV->isInterposable()) { |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 223 | BaseType = GV->getType()->getElementType(); |
| 224 | BaseAlign = GV->getAlignment(); |
| 225 | } |
| 226 | } |
| 227 | |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 228 | PointerType *AddrTy = cast<PointerType>(V->getType()); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 229 | uint64_t LoadSize = DL.getTypeStoreSize(AddrTy->getElementType()); |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 230 | |
Chandler Carruth | 8a99373 | 2014-10-19 00:42:16 +0000 | [diff] [blame] | 231 | // If we found a base allocated type from either an alloca or global variable, |
| 232 | // try to see if we are definitively within the allocated region. We need to |
| 233 | // 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] | 234 | // case. |
| 235 | if (BaseType && BaseType->isSized()) { |
Chandler Carruth | 8a99373 | 2014-10-19 00:42:16 +0000 | [diff] [blame] | 236 | if (BaseAlign == 0) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 237 | BaseAlign = DL.getPrefTypeAlignment(BaseType); |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 238 | |
| 239 | if (Align <= BaseAlign) { |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 240 | // Check if the load is within the bounds of the underlying object. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 241 | if (ByteOffset + LoadSize <= DL.getTypeAllocSize(BaseType) && |
Artur Pilipenko | 0e21d54 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 242 | ((ByteOffset % Align) == 0)) |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 243 | return true; |
| 244 | } |
| 245 | } |
| 246 | |
Artur Pilipenko | 9bb6bea | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 247 | if (!ScanFrom) |
| 248 | return false; |
| 249 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 250 | // Otherwise, be a little bit aggressive by scanning the local block where we |
| 251 | // want to check to see if the pointer is already being loaded or stored |
| 252 | // from/to. If so, the previous load or store would have already trapped, |
| 253 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 254 | // the load entirely). |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 255 | BasicBlock::iterator BBI = ScanFrom->getIterator(), |
| 256 | E = ScanFrom->getParent()->begin(); |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 257 | |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 258 | // We can at least always strip pointer casts even though we can't use the |
| 259 | // base here. |
| 260 | V = V->stripPointerCasts(); |
| 261 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 262 | while (BBI != E) { |
| 263 | --BBI; |
| 264 | |
| 265 | // If we see a free or a call which may write to memory (i.e. which might do |
| 266 | // a free) the pointer could be marked invalid. |
| 267 | if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() && |
| 268 | !isa<DbgInfoIntrinsic>(BBI)) |
| 269 | return false; |
| 270 | |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 271 | Value *AccessedPtr; |
Artur Pilipenko | 0e21d54 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 272 | unsigned AccessedAlign; |
| 273 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 274 | AccessedPtr = LI->getPointerOperand(); |
Artur Pilipenko | 0e21d54 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 275 | AccessedAlign = LI->getAlignment(); |
| 276 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 277 | AccessedPtr = SI->getPointerOperand(); |
Artur Pilipenko | 0e21d54 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 278 | AccessedAlign = SI->getAlignment(); |
| 279 | } else |
| 280 | continue; |
| 281 | |
| 282 | Type *AccessedTy = AccessedPtr->getType()->getPointerElementType(); |
| 283 | if (AccessedAlign == 0) |
| 284 | AccessedAlign = DL.getABITypeAlignment(AccessedTy); |
| 285 | if (AccessedAlign < Align) |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 286 | continue; |
| 287 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 288 | // Handle trivial cases. |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 289 | if (AccessedPtr == V) |
| 290 | return true; |
| 291 | |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 292 | if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) && |
Artur Pilipenko | 0e21d54 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 293 | LoadSize <= DL.getTypeStoreSize(AccessedTy)) |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 294 | return true; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
Larisse Voufo | 532bf71 | 2015-09-18 19:14:35 +0000 | [diff] [blame] | 299 | /// DefMaxInstsToScan - the default number of maximum instructions |
| 300 | /// to scan in the block, used by FindAvailableLoadedValue(). |
| 301 | /// FindAvailableLoadedValue() was introduced in r60148, to improve jump |
| 302 | /// threading in part by eliminating partially redundant loads. |
| 303 | /// At that point, the value of MaxInstsToScan was already set to '6' |
| 304 | /// without documented explanation. |
| 305 | cl::opt<unsigned> |
| 306 | llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden, |
| 307 | cl::desc("Use this to specify the default maximum number of instructions " |
| 308 | "to scan backward from a given instruction, when searching for " |
| 309 | "available loaded value")); |
| 310 | |
Eli Friedman | 02419a9 | 2016-08-08 04:10:22 +0000 | [diff] [blame] | 311 | Value *llvm::FindAvailableLoadedValue(LoadInst *Load, |
| 312 | BasicBlock *ScanBB, |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 313 | BasicBlock::iterator &ScanFrom, |
| 314 | unsigned MaxInstsToScan, |
Eli Friedman | 02419a9 | 2016-08-08 04:10:22 +0000 | [diff] [blame] | 315 | AliasAnalysis *AA, bool *IsLoadCSE) { |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 316 | if (MaxInstsToScan == 0) |
| 317 | MaxInstsToScan = ~0U; |
Anna Thomas | 9ad45ad | 2016-07-08 22:15:08 +0000 | [diff] [blame] | 318 | |
| 319 | Value *Ptr = Load->getPointerOperand(); |
| 320 | Type *AccessTy = Load->getType(); |
| 321 | |
| 322 | // We can never remove a volatile load |
| 323 | if (Load->isVolatile()) |
| 324 | return nullptr; |
| 325 | |
| 326 | // Anything stronger than unordered is currently unimplemented. |
| 327 | if (!Load->isUnordered()) |
| 328 | return nullptr; |
| 329 | |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 330 | const DataLayout &DL = ScanBB->getModule()->getDataLayout(); |
Anna Thomas | 9ad45ad | 2016-07-08 22:15:08 +0000 | [diff] [blame] | 331 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 332 | // Try to get the store size for the type. |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 333 | uint64_t AccessSize = DL.getTypeStoreSize(AccessTy); |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 334 | |
| 335 | Value *StrippedPtr = Ptr->stripPointerCasts(); |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 336 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 337 | while (ScanFrom != ScanBB->begin()) { |
| 338 | // We must ignore debug info directives when counting (otherwise they |
| 339 | // would affect codegen). |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 340 | Instruction *Inst = &*--ScanFrom; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 341 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 342 | continue; |
| 343 | |
| 344 | // Restore ScanFrom to expected value in case next test succeeds |
| 345 | ScanFrom++; |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 346 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 347 | // Don't scan huge blocks. |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 348 | if (MaxInstsToScan-- == 0) |
| 349 | return nullptr; |
| 350 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 351 | --ScanFrom; |
| 352 | // If this is a load of Ptr, the loaded value is available. |
Eli Friedman | 4419cd2 | 2011-08-15 21:56:39 +0000 | [diff] [blame] | 353 | // (This is true even if the load is volatile or atomic, although |
| 354 | // those cases are unlikely.) |
Reid Kleckner | fbd5eef | 2016-06-24 18:42:58 +0000 | [diff] [blame] | 355 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) |
| 356 | if (AreEquivalentAddressValues( |
| 357 | LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 358 | CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) { |
Philip Reames | 92c4369 | 2016-04-21 16:51:08 +0000 | [diff] [blame] | 359 | |
| 360 | // We can value forward from an atomic to a non-atomic, but not the |
| 361 | // other way around. |
Anna Thomas | 9ad45ad | 2016-07-08 22:15:08 +0000 | [diff] [blame] | 362 | if (LI->isAtomic() < Load->isAtomic()) |
Philip Reames | 92c4369 | 2016-04-21 16:51:08 +0000 | [diff] [blame] | 363 | return nullptr; |
| 364 | |
Eli Friedman | bd254a6 | 2016-06-16 02:33:42 +0000 | [diff] [blame] | 365 | if (IsLoadCSE) |
| 366 | *IsLoadCSE = true; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 367 | return LI; |
Chris Lattner | 87fa77b | 2012-03-13 18:07:41 +0000 | [diff] [blame] | 368 | } |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 369 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 370 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 371 | Value *StorePtr = SI->getPointerOperand()->stripPointerCasts(); |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 372 | // If this is a store through Ptr, the value is available! |
Eli Friedman | 4419cd2 | 2011-08-15 21:56:39 +0000 | [diff] [blame] | 373 | // (This is true even if the store is volatile or atomic, although |
| 374 | // those cases are unlikely.) |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 375 | if (AreEquivalentAddressValues(StorePtr, StrippedPtr) && |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 376 | CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 377 | AccessTy, DL)) { |
Philip Reames | 92c4369 | 2016-04-21 16:51:08 +0000 | [diff] [blame] | 378 | |
| 379 | // We can value forward from an atomic to a non-atomic, but not the |
| 380 | // other way around. |
Anna Thomas | 9ad45ad | 2016-07-08 22:15:08 +0000 | [diff] [blame] | 381 | if (SI->isAtomic() < Load->isAtomic()) |
Philip Reames | 92c4369 | 2016-04-21 16:51:08 +0000 | [diff] [blame] | 382 | return nullptr; |
| 383 | |
Eli Friedman | 02419a9 | 2016-08-08 04:10:22 +0000 | [diff] [blame] | 384 | if (IsLoadCSE) |
| 385 | *IsLoadCSE = false; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 386 | return SI->getOperand(0); |
Chris Lattner | 87fa77b | 2012-03-13 18:07:41 +0000 | [diff] [blame] | 387 | } |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 388 | |
Chandler Carruth | a32038b | 2014-10-20 10:03:01 +0000 | [diff] [blame] | 389 | // If both StrippedPtr and StorePtr reach all the way to an alloca or |
| 390 | // global and they are different, ignore the store. This is a trivial form |
| 391 | // of alias analysis that is important for reg2mem'd code. |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 392 | if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) && |
Chandler Carruth | a32038b | 2014-10-20 10:03:01 +0000 | [diff] [blame] | 393 | (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) && |
| 394 | StrippedPtr != StorePtr) |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 395 | continue; |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 396 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 397 | // If we have alias analysis and it says the store won't modify the loaded |
| 398 | // value, ignore the store. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 399 | if (AA && (AA->getModRefInfo(SI, StrippedPtr, AccessSize) & MRI_Mod) == 0) |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 400 | continue; |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 401 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 402 | // Otherwise the store that may or may not alias the pointer, bail out. |
| 403 | ++ScanFrom; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 404 | return nullptr; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 405 | } |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 406 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 407 | // If this is some other instruction that may clobber Ptr, bail out. |
| 408 | if (Inst->mayWriteToMemory()) { |
| 409 | // If alias analysis claims that it really won't modify the load, |
| 410 | // ignore it. |
| 411 | if (AA && |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 412 | (AA->getModRefInfo(Inst, StrippedPtr, AccessSize) & MRI_Mod) == 0) |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 413 | continue; |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 414 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 415 | // May modify the pointer, bail out. |
| 416 | ++ScanFrom; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 417 | return nullptr; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
Chandler Carruth | d67244d | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 420 | |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 421 | // Got to the start of the block, we didn't find it, but are done for this |
| 422 | // block. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 423 | return nullptr; |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 424 | } |