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