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