Dan Gohman | dd9344f | 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 | 1ef05b1 | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 0b8c9a8 | 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 | a716c07 | 2015-03-03 22:01:13 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Module.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Operator.h" |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Statepoint.h" |
| 25 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Benjamin Kramer | 36538ff | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 28 | static bool isAligned(const Value *Base, const APInt &Offset, unsigned Align, |
Artur Pilipenko | ba9f09c | 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 | 36538ff | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 55 | const Value *V, unsigned Align, const APInt &Size, const DataLayout &DL, |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 56 | const Instruction *CtxI, const DominatorTree *DT, |
Sean Silva | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 57 | SmallPtrSetImpl<const Value *> &Visited) { |
David Majnemer | 8b6ce01 | 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 | ba9f09c | 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 | 07e4e55 | 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 | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 68 | DL, CtxI, DT, Visited); |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 69 | |
Sanjoy Das | cfd3677 | 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)) |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 75 | if (!CheckForNonNull || isKnownNonZero(V, DL, 0, nullptr, CtxI, DT)) |
Sanjoy Das | cfd3677 | 2016-06-02 00:52:53 +0000 | [diff] [blame] | 76 | return isAligned(V, Align, DL); |
| 77 | } |
Artur Pilipenko | ba9f09c | 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 | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 81 | const Value *Base = GEP->getPointerOperand(); |
| 82 | |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 83 | APInt Offset(DL.getIndexTypeSizeInBits(GEP->getType()), 0); |
Sanjoy Das | 07e4e55 | 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 | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 86 | return false; |
| 87 | |
Sanjoy Das | 07e4e55 | 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 | d0b25b0 | 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 | ba9f09c | 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 | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 104 | RelocateInst->getDerivedPtr(), Align, Size, DL, CtxI, DT, Visited); |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 105 | |
| 106 | if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V)) |
Sanjoy Das | 07e4e55 | 2016-06-01 16:47:45 +0000 | [diff] [blame] | 107 | return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, Size, |
Sean Silva | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 108 | DL, CtxI, DT, Visited); |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 109 | |
Chandler Carruth | 81aa712 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 110 | if (const auto *Call = dyn_cast<CallBase>(V)) |
| 111 | if (auto *RP = getArgumentAliasingToReturnedPointer(Call)) |
Piotr Padlewski | 2e3226a | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 112 | return isDereferenceableAndAlignedPointer(RP, Align, Size, DL, CtxI, DT, |
Hal Finkel | 0017f36 | 2016-07-11 03:08:49 +0000 | [diff] [blame] | 113 | Visited); |
Piotr Padlewski | 2e3226a | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 114 | |
Artur Pilipenko | ba9f09c | 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, |
Vitaly Buka | 2587e3e | 2017-06-24 01:35:13 +0000 | [diff] [blame] | 120 | const APInt &Size, |
| 121 | const DataLayout &DL, |
| 122 | const Instruction *CtxI, |
| 123 | const DominatorTree *DT) { |
| 124 | SmallPtrSet<const Value *, 32> Visited; |
| 125 | return ::isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT, |
| 126 | Visited); |
| 127 | } |
| 128 | |
| 129 | bool llvm::isDereferenceableAndAlignedPointer(const Value *V, unsigned Align, |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 130 | const DataLayout &DL, |
| 131 | const Instruction *CtxI, |
Sean Silva | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 132 | const DominatorTree *DT) { |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 133 | // When dereferenceability information is provided by a dereferenceable |
| 134 | // attribute, we know exactly how many bytes are dereferenceable. If we can |
| 135 | // determine the exact offset to the attributed variable, we can use that |
| 136 | // information here. |
| 137 | Type *VTy = V->getType(); |
| 138 | Type *Ty = VTy->getPointerElementType(); |
| 139 | |
| 140 | // Require ABI alignment for loads without alignment specification |
| 141 | if (Align == 0) |
| 142 | Align = DL.getABITypeAlignment(Ty); |
| 143 | |
Sanjoy Das | 07e4e55 | 2016-06-01 16:47:45 +0000 | [diff] [blame] | 144 | if (!Ty->isSized()) |
| 145 | return false; |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 146 | |
| 147 | SmallPtrSet<const Value *, 32> Visited; |
Sanjoy Das | 07e4e55 | 2016-06-01 16:47:45 +0000 | [diff] [blame] | 148 | return ::isDereferenceableAndAlignedPointer( |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 149 | V, Align, APInt(DL.getIndexTypeSizeInBits(VTy), DL.getTypeStoreSize(Ty)), DL, |
Sean Silva | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 150 | CtxI, DT, Visited); |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL, |
| 154 | const Instruction *CtxI, |
Sean Silva | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 155 | const DominatorTree *DT) { |
| 156 | return isDereferenceableAndAlignedPointer(V, 1, DL, CtxI, DT); |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 159 | /// Test if A and B will obviously have the same value. |
Chandler Carruth | e99ca83 | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 160 | /// |
| 161 | /// This includes recognizing that %t0 and %t1 will have the same |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 162 | /// value in code like this: |
Chandler Carruth | e99ca83 | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 163 | /// \code |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 164 | /// %t0 = getelementptr \@a, 0, 3 |
| 165 | /// store i32 0, i32* %t0 |
| 166 | /// %t1 = getelementptr \@a, 0, 3 |
| 167 | /// %t2 = load i32* %t1 |
Chandler Carruth | e99ca83 | 2014-10-18 23:31:55 +0000 | [diff] [blame] | 168 | /// \endcode |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 169 | /// |
| 170 | static bool AreEquivalentAddressValues(const Value *A, const Value *B) { |
| 171 | // Test if the values are trivially equivalent. |
Chandler Carruth | 28502a8 | 2014-10-18 23:41:25 +0000 | [diff] [blame] | 172 | if (A == B) |
| 173 | return true; |
Hans Wennborg | 68df608 | 2011-06-03 17:15:37 +0000 | [diff] [blame] | 174 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 175 | // Test if the values come from identical arithmetic instructions. |
| 176 | // Use isIdenticalToWhenDefined instead of isIdenticalTo because |
| 177 | // this function is only used when one address use dominates the |
| 178 | // other, which means that they'll always either have the same |
| 179 | // value or one of them will have an undefined value. |
Chandler Carruth | 28502a8 | 2014-10-18 23:41:25 +0000 | [diff] [blame] | 180 | if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) || |
| 181 | isa<GetElementPtrInst>(A)) |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 182 | if (const Instruction *BI = dyn_cast<Instruction>(B)) |
| 183 | if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) |
| 184 | return true; |
Hans Wennborg | 68df608 | 2011-06-03 17:15:37 +0000 | [diff] [blame] | 185 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 186 | // Otherwise they may not be equivalent. |
| 187 | return false; |
| 188 | } |
| 189 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 190 | /// Check if executing a load of this pointer value cannot trap. |
Chandler Carruth | dacb8a6 | 2014-10-18 23:46:17 +0000 | [diff] [blame] | 191 | /// |
Artur Pilipenko | aeb3be1 | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 192 | /// If DT and ScanFrom are specified this method performs context-sensitive |
| 193 | /// analysis and returns true if it is safe to load immediately before ScanFrom. |
Artur Pilipenko | 5880de5 | 2016-02-11 13:42:59 +0000 | [diff] [blame] | 194 | /// |
Chandler Carruth | dacb8a6 | 2014-10-18 23:46:17 +0000 | [diff] [blame] | 195 | /// If it is not obviously safe to load from the specified pointer, we do |
| 196 | /// a quick local scan of the basic block containing \c ScanFrom, to determine |
| 197 | /// if the address is already accessed. |
| 198 | /// |
| 199 | /// This uses the pointee type to determine how many bytes need to be safe to |
| 200 | /// load from the pointer. |
Artur Pilipenko | 69ba216 | 2016-01-15 15:27:46 +0000 | [diff] [blame] | 201 | bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align, |
Artur Pilipenko | aeb3be1 | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 202 | const DataLayout &DL, |
Artur Pilipenko | 5880de5 | 2016-02-11 13:42:59 +0000 | [diff] [blame] | 203 | Instruction *ScanFrom, |
Sean Silva | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 204 | const DominatorTree *DT) { |
Artur Pilipenko | f2e7bb5 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 205 | // Zero alignment means that the load has the ABI alignment for the target |
| 206 | if (Align == 0) |
| 207 | Align = DL.getABITypeAlignment(V->getType()->getPointerElementType()); |
| 208 | assert(isPowerOf2_32(Align)); |
| 209 | |
Artur Pilipenko | 5880de5 | 2016-02-11 13:42:59 +0000 | [diff] [blame] | 210 | // If DT is not specified we can't make context-sensitive query |
| 211 | const Instruction* CtxI = DT ? ScanFrom : nullptr; |
Sean Silva | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 212 | if (isDereferenceableAndAlignedPointer(V, Align, DL, CtxI, DT)) |
Artur Pilipenko | ae316aa | 2016-01-17 12:35:29 +0000 | [diff] [blame] | 213 | return true; |
| 214 | |
Nuno Lopes | 1ef05b1 | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 215 | int64_t ByteOffset = 0; |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 216 | Value *Base = V; |
Chandler Carruth | 652627d | 2014-10-18 23:47:22 +0000 | [diff] [blame] | 217 | Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL); |
Nuno Lopes | 1ef05b1 | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 218 | |
| 219 | if (ByteOffset < 0) // out of bounds |
| 220 | return false; |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 221 | |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 222 | Type *BaseType = nullptr; |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 223 | unsigned BaseAlign = 0; |
| 224 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) { |
| 225 | // An alloca is safe to load from as load as it is suitably aligned. |
| 226 | BaseType = AI->getAllocatedType(); |
| 227 | BaseAlign = AI->getAlignment(); |
Nuno Lopes | 1ef05b1 | 2012-12-31 17:42:11 +0000 | [diff] [blame] | 228 | } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) { |
Chandler Carruth | f84407b | 2014-10-19 00:42:16 +0000 | [diff] [blame] | 229 | // Global variables are not necessarily safe to load from if they are |
Sanjoy Das | c9e3e3c | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 230 | // interposed arbitrarily. Their size may change or they may be weak and |
| 231 | // require a test to determine if they were in fact provided. |
| 232 | if (!GV->isInterposable()) { |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 233 | BaseType = GV->getType()->getElementType(); |
| 234 | BaseAlign = GV->getAlignment(); |
| 235 | } |
| 236 | } |
| 237 | |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 238 | PointerType *AddrTy = cast<PointerType>(V->getType()); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 239 | uint64_t LoadSize = DL.getTypeStoreSize(AddrTy->getElementType()); |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 240 | |
Chandler Carruth | f84407b | 2014-10-19 00:42:16 +0000 | [diff] [blame] | 241 | // If we found a base allocated type from either an alloca or global variable, |
| 242 | // try to see if we are definitively within the allocated region. We need to |
| 243 | // know the size of the base type and the loaded type to do anything in this |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 244 | // case. |
| 245 | if (BaseType && BaseType->isSized()) { |
Chandler Carruth | f84407b | 2014-10-19 00:42:16 +0000 | [diff] [blame] | 246 | if (BaseAlign == 0) |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 247 | BaseAlign = DL.getPrefTypeAlignment(BaseType); |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 248 | |
| 249 | if (Align <= BaseAlign) { |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 250 | // Check if the load is within the bounds of the underlying object. |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 251 | if (ByteOffset + LoadSize <= DL.getTypeAllocSize(BaseType) && |
Artur Pilipenko | f2e7bb5 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 252 | ((ByteOffset % Align) == 0)) |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | } |
| 256 | |
Artur Pilipenko | aeb3be1 | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 257 | if (!ScanFrom) |
| 258 | return false; |
| 259 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 260 | // Otherwise, be a little bit aggressive by scanning the local block where we |
| 261 | // want to check to see if the pointer is already being loaded or stored |
| 262 | // from/to. If so, the previous load or store would have already trapped, |
| 263 | // so there is no harm doing an extra load (also, CSE will later eliminate |
| 264 | // the load entirely). |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 265 | BasicBlock::iterator BBI = ScanFrom->getIterator(), |
| 266 | E = ScanFrom->getParent()->begin(); |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 267 | |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 268 | // We can at least always strip pointer casts even though we can't use the |
| 269 | // base here. |
| 270 | V = V->stripPointerCasts(); |
| 271 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 272 | while (BBI != E) { |
| 273 | --BBI; |
| 274 | |
| 275 | // If we see a free or a call which may write to memory (i.e. which might do |
| 276 | // a free) the pointer could be marked invalid. |
| 277 | if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() && |
| 278 | !isa<DbgInfoIntrinsic>(BBI)) |
| 279 | return false; |
| 280 | |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 281 | Value *AccessedPtr; |
Artur Pilipenko | f2e7bb5 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 282 | unsigned AccessedAlign; |
| 283 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 284 | AccessedPtr = LI->getPointerOperand(); |
Artur Pilipenko | f2e7bb5 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 285 | AccessedAlign = LI->getAlignment(); |
| 286 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 287 | AccessedPtr = SI->getPointerOperand(); |
Artur Pilipenko | f2e7bb5 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 288 | AccessedAlign = SI->getAlignment(); |
| 289 | } else |
| 290 | continue; |
| 291 | |
| 292 | Type *AccessedTy = AccessedPtr->getType()->getPointerElementType(); |
| 293 | if (AccessedAlign == 0) |
| 294 | AccessedAlign = DL.getABITypeAlignment(AccessedTy); |
| 295 | if (AccessedAlign < Align) |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 296 | continue; |
| 297 | |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 298 | // Handle trivial cases. |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 299 | if (AccessedPtr == V) |
| 300 | return true; |
| 301 | |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 302 | if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) && |
Artur Pilipenko | f2e7bb5 | 2015-06-25 12:18:43 +0000 | [diff] [blame] | 303 | LoadSize <= DL.getTypeStoreSize(AccessedTy)) |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 304 | return true; |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
Larisse Voufo | 6932880 | 2015-09-18 19:14:35 +0000 | [diff] [blame] | 309 | /// DefMaxInstsToScan - the default number of maximum instructions |
| 310 | /// to scan in the block, used by FindAvailableLoadedValue(). |
| 311 | /// FindAvailableLoadedValue() was introduced in r60148, to improve jump |
| 312 | /// threading in part by eliminating partially redundant loads. |
| 313 | /// At that point, the value of MaxInstsToScan was already set to '6' |
| 314 | /// without documented explanation. |
| 315 | cl::opt<unsigned> |
| 316 | llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden, |
| 317 | cl::desc("Use this to specify the default maximum number of instructions " |
| 318 | "to scan backward from a given instruction, when searching for " |
| 319 | "available loaded value")); |
| 320 | |
Eli Friedman | 97b394f | 2016-08-08 04:10:22 +0000 | [diff] [blame] | 321 | Value *llvm::FindAvailableLoadedValue(LoadInst *Load, |
| 322 | BasicBlock *ScanBB, |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 323 | BasicBlock::iterator &ScanFrom, |
| 324 | unsigned MaxInstsToScan, |
Xin Tong | 966ca1d | 2017-03-19 15:27:52 +0000 | [diff] [blame] | 325 | AliasAnalysis *AA, bool *IsLoad, |
Jun Bum Lim | 0de282c | 2017-02-02 15:12:34 +0000 | [diff] [blame] | 326 | unsigned *NumScanedInst) { |
Xin Tong | 966ca1d | 2017-03-19 15:27:52 +0000 | [diff] [blame] | 327 | // Don't CSE load that is volatile or anything stronger than unordered. |
Anna Thomas | add1bef | 2016-07-08 22:15:08 +0000 | [diff] [blame] | 328 | if (!Load->isUnordered()) |
| 329 | return nullptr; |
| 330 | |
Xin Tong | 966ca1d | 2017-03-19 15:27:52 +0000 | [diff] [blame] | 331 | return FindAvailablePtrLoadStore( |
| 332 | Load->getPointerOperand(), Load->getType(), Load->isAtomic(), ScanBB, |
| 333 | ScanFrom, MaxInstsToScan, AA, IsLoad, NumScanedInst); |
| 334 | } |
| 335 | |
| 336 | Value *llvm::FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy, |
| 337 | bool AtLeastAtomic, BasicBlock *ScanBB, |
| 338 | BasicBlock::iterator &ScanFrom, |
| 339 | unsigned MaxInstsToScan, |
| 340 | AliasAnalysis *AA, bool *IsLoadCSE, |
| 341 | unsigned *NumScanedInst) { |
| 342 | if (MaxInstsToScan == 0) |
| 343 | MaxInstsToScan = ~0U; |
| 344 | |
Mehdi Amini | c94da20 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 345 | const DataLayout &DL = ScanBB->getModule()->getDataLayout(); |
Anna Thomas | add1bef | 2016-07-08 22:15:08 +0000 | [diff] [blame] | 346 | |
Chandler Carruth | a87c354 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 347 | // Try to get the store size for the type. |
George Burgess IV | 8bf0c71 | 2018-12-23 03:10:56 +0000 | [diff] [blame] | 348 | auto AccessSize = LocationSize::precise(DL.getTypeStoreSize(AccessTy)); |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 349 | |
| 350 | Value *StrippedPtr = Ptr->stripPointerCasts(); |
Chandler Carruth | 01dc911 | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 351 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 352 | while (ScanFrom != ScanBB->begin()) { |
| 353 | // We must ignore debug info directives when counting (otherwise they |
| 354 | // would affect codegen). |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 355 | Instruction *Inst = &*--ScanFrom; |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 356 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 357 | continue; |
| 358 | |
| 359 | // Restore ScanFrom to expected value in case next test succeeds |
| 360 | ScanFrom++; |
Chandler Carruth | 01dc911 | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 361 | |
Jun Bum Lim | 0de282c | 2017-02-02 15:12:34 +0000 | [diff] [blame] | 362 | if (NumScanedInst) |
| 363 | ++(*NumScanedInst); |
| 364 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 365 | // Don't scan huge blocks. |
Chandler Carruth | 01dc911 | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 366 | if (MaxInstsToScan-- == 0) |
| 367 | return nullptr; |
| 368 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 369 | --ScanFrom; |
| 370 | // If this is a load of Ptr, the loaded value is available. |
Eli Friedman | c869b43 | 2011-08-15 21:56:39 +0000 | [diff] [blame] | 371 | // (This is true even if the load is volatile or atomic, although |
| 372 | // those cases are unlikely.) |
Reid Kleckner | c0b6cd2 | 2016-06-24 18:42:58 +0000 | [diff] [blame] | 373 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) |
| 374 | if (AreEquivalentAddressValues( |
| 375 | LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) && |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 376 | CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) { |
Philip Reames | 6159d86 | 2016-04-21 16:51:08 +0000 | [diff] [blame] | 377 | |
| 378 | // We can value forward from an atomic to a non-atomic, but not the |
| 379 | // other way around. |
Xin Tong | 966ca1d | 2017-03-19 15:27:52 +0000 | [diff] [blame] | 380 | if (LI->isAtomic() < AtLeastAtomic) |
Philip Reames | 6159d86 | 2016-04-21 16:51:08 +0000 | [diff] [blame] | 381 | return nullptr; |
| 382 | |
Eli Friedman | 3c80c26 | 2016-06-16 02:33:42 +0000 | [diff] [blame] | 383 | if (IsLoadCSE) |
| 384 | *IsLoadCSE = true; |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 385 | return LI; |
Chris Lattner | 5161de6 | 2012-03-13 18:07:41 +0000 | [diff] [blame] | 386 | } |
Chandler Carruth | 01dc911 | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 387 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 388 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 389 | Value *StorePtr = SI->getPointerOperand()->stripPointerCasts(); |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 390 | // If this is a store through Ptr, the value is available! |
Eli Friedman | c869b43 | 2011-08-15 21:56:39 +0000 | [diff] [blame] | 391 | // (This is true even if the store is volatile or atomic, although |
| 392 | // those cases are unlikely.) |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 393 | if (AreEquivalentAddressValues(StorePtr, StrippedPtr) && |
Chandler Carruth | a87c354 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 394 | CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(), |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 395 | AccessTy, DL)) { |
Philip Reames | 6159d86 | 2016-04-21 16:51:08 +0000 | [diff] [blame] | 396 | |
| 397 | // We can value forward from an atomic to a non-atomic, but not the |
| 398 | // other way around. |
Xin Tong | 966ca1d | 2017-03-19 15:27:52 +0000 | [diff] [blame] | 399 | if (SI->isAtomic() < AtLeastAtomic) |
Philip Reames | 6159d86 | 2016-04-21 16:51:08 +0000 | [diff] [blame] | 400 | return nullptr; |
| 401 | |
Eli Friedman | 97b394f | 2016-08-08 04:10:22 +0000 | [diff] [blame] | 402 | if (IsLoadCSE) |
| 403 | *IsLoadCSE = false; |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 404 | return SI->getOperand(0); |
Chris Lattner | 5161de6 | 2012-03-13 18:07:41 +0000 | [diff] [blame] | 405 | } |
Chandler Carruth | 01dc911 | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 406 | |
Chandler Carruth | 3ac929c | 2014-10-20 10:03:01 +0000 | [diff] [blame] | 407 | // If both StrippedPtr and StorePtr reach all the way to an alloca or |
| 408 | // global and they are different, ignore the store. This is a trivial form |
| 409 | // of alias analysis that is important for reg2mem'd code. |
Chandler Carruth | 35c4e07 | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 410 | if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) && |
Chandler Carruth | 3ac929c | 2014-10-20 10:03:01 +0000 | [diff] [blame] | 411 | (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) && |
| 412 | StrippedPtr != StorePtr) |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 413 | continue; |
Chandler Carruth | 01dc911 | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 414 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 415 | // If we have alias analysis and it says the store won't modify the loaded |
| 416 | // value, ignore the store. |
Alina Sbirlea | a2d30e9 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 417 | if (AA && !isModSet(AA->getModRefInfo(SI, StrippedPtr, AccessSize))) |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 418 | continue; |
Chandler Carruth | 01dc911 | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 419 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 420 | // Otherwise the store that may or may not alias the pointer, bail out. |
| 421 | ++ScanFrom; |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 422 | return nullptr; |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 423 | } |
Chandler Carruth | 01dc911 | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 424 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 425 | // If this is some other instruction that may clobber Ptr, bail out. |
| 426 | if (Inst->mayWriteToMemory()) { |
| 427 | // If alias analysis claims that it really won't modify the load, |
| 428 | // ignore it. |
Alina Sbirlea | a2d30e9 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 429 | if (AA && !isModSet(AA->getModRefInfo(Inst, StrippedPtr, AccessSize))) |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 430 | continue; |
Chandler Carruth | 01dc911 | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 431 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 432 | // May modify the pointer, bail out. |
| 433 | ++ScanFrom; |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 434 | return nullptr; |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
Chandler Carruth | 01dc911 | 2014-10-18 23:19:03 +0000 | [diff] [blame] | 437 | |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 438 | // Got to the start of the block, we didn't find it, but are done for this |
| 439 | // block. |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 440 | return nullptr; |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 441 | } |