Dan Gohman | da85ed8 | 2010-10-19 23:09:08 +0000 | [diff] [blame] | 1 | //===- BasicAliasAnalysis.cpp - Stateless Alias Analysis Impl -------------===// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 9 | // |
Dan Gohman | da85ed8 | 2010-10-19 23:09:08 +0000 | [diff] [blame] | 10 | // This file defines the primary stateless implementation of the |
| 11 | // Alias Analysis interface that implements identities (two different |
| 12 | // globals cannot alias, etc), but does no stateful analysis. |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 17e0bc3 | 2015-08-06 07:33:15 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Wei Mi | d67daae | 2015-08-05 23:40:30 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CFG.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/InstructionSimplify.h" |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/MemoryBuiltins.h" |
| 25 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Constants.h" |
| 27 | #include "llvm/IR/DataLayout.h" |
| 28 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/GlobalAlias.h" |
| 31 | #include "llvm/IR/GlobalVariable.h" |
| 32 | #include "llvm/IR/Instructions.h" |
| 33 | #include "llvm/IR/IntrinsicInst.h" |
| 34 | #include "llvm/IR/LLVMContext.h" |
| 35 | #include "llvm/IR/Operator.h" |
Chris Lattner | d82256a | 2004-03-15 03:36:49 +0000 | [diff] [blame] | 36 | #include "llvm/Pass.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ErrorHandling.h" |
Alkis Evlogimenos | a5c04ee | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 38 | #include <algorithm> |
Chris Lattner | 3599748 | 2003-11-25 18:33:40 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 40 | |
Tobias Edler von Koch | d8ce16b | 2015-07-15 19:32:22 +0000 | [diff] [blame] | 41 | /// Enable analysis of recursive PHI nodes. |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 42 | static cl::opt<bool> EnableRecPhiAnalysis("basicaa-recphi", cl::Hidden, |
| 43 | cl::init(false)); |
Tobias Edler von Koch | d8ce16b | 2015-07-15 19:32:22 +0000 | [diff] [blame] | 44 | |
Wei Mi | d67daae | 2015-08-05 23:40:30 +0000 | [diff] [blame] | 45 | /// SearchLimitReached / SearchTimes shows how often the limit of |
| 46 | /// to decompose GEPs is reached. It will affect the precision |
| 47 | /// of basic alias analysis. |
| 48 | #define DEBUG_TYPE "basicaa" |
| 49 | STATISTIC(SearchLimitReached, "Number of times the limit to " |
| 50 | "decompose GEPs is reached"); |
| 51 | STATISTIC(SearchTimes, "Number of times a GEP is decomposed"); |
| 52 | |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 53 | /// Cutoff after which to stop analysing a set of phi nodes potentially involved |
| 54 | /// in a cycle. Because we are analysing 'through' phi nodes we need to be |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 55 | /// careful with value equivalence. We use reachability to make sure a value |
| 56 | /// cannot be involved in a cycle. |
| 57 | const unsigned MaxNumPhiBBsValueReachabilityCheck = 20; |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 58 | |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 59 | // The max limit of the search depth in DecomposeGEPExpression() and |
| 60 | // GetUnderlyingObject(), both functions need to use the same search |
| 61 | // depth otherwise the algorithm in aliasGEP will assert. |
| 62 | static const unsigned MaxLookupSearchDepth = 6; |
| 63 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 64 | //===----------------------------------------------------------------------===// |
| 65 | // Useful predicates |
| 66 | //===----------------------------------------------------------------------===// |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 67 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 68 | /// Returns true if the pointer is to a function-local object that never |
| 69 | /// escapes from the function. |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 70 | static bool isNonEscapingLocalObject(const Value *V) { |
Chris Lattner | fa48258 | 2008-06-16 06:28:01 +0000 | [diff] [blame] | 71 | // If this is a local allocation, check to see if it escapes. |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 72 | if (isa<AllocaInst>(V) || isNoAliasCall(V)) |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 73 | // Set StoreCaptures to True so that we can assume in our callers that the |
| 74 | // pointer is not the result of a load instruction. Currently |
| 75 | // PointerMayBeCaptured doesn't have any special analysis for the |
| 76 | // StoreCaptures=false case; if it did, our callers could be refined to be |
| 77 | // more precise. |
| 78 | return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true); |
Duncan Sands | 8d65f36 | 2009-01-05 21:19:53 +0000 | [diff] [blame] | 79 | |
Chris Lattner | fa48258 | 2008-06-16 06:28:01 +0000 | [diff] [blame] | 80 | // If this is an argument that corresponds to a byval or noalias argument, |
Duncan Sands | 8d65f36 | 2009-01-05 21:19:53 +0000 | [diff] [blame] | 81 | // then it has not escaped before entering the function. Check if it escapes |
| 82 | // inside the function. |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 83 | if (const Argument *A = dyn_cast<Argument>(V)) |
Richard Osborne | a1fffcf | 2012-11-05 10:48:24 +0000 | [diff] [blame] | 84 | if (A->hasByValAttr() || A->hasNoAliasAttr()) |
| 85 | // Note even if the argument is marked nocapture we still need to check |
| 86 | // for copies made inside the function. The nocapture attribute only |
| 87 | // specifies that there are no copies made that outlive the function. |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 88 | return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true); |
Richard Osborne | a1fffcf | 2012-11-05 10:48:24 +0000 | [diff] [blame] | 89 | |
Chris Lattner | b35d9b5 | 2008-06-16 06:19:11 +0000 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 93 | /// Returns true if the pointer is one which would have been considered an |
| 94 | /// escape by isNonEscapingLocalObject. |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 95 | static bool isEscapeSource(const Value *V) { |
| 96 | if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V)) |
| 97 | return true; |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 98 | |
| 99 | // The load case works because isNonEscapingLocalObject considers all |
| 100 | // stores to be escapes (it passes true for the StoreCaptures argument |
| 101 | // to PointerMayBeCaptured). |
| 102 | if (isa<LoadInst>(V)) |
| 103 | return true; |
| 104 | |
| 105 | return false; |
| 106 | } |
Chris Lattner | b35d9b5 | 2008-06-16 06:19:11 +0000 | [diff] [blame] | 107 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 108 | /// Returns the size of the object specified by V, or UnknownSize if unknown. |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 109 | static uint64_t getObjectSize(const Value *V, const DataLayout &DL, |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 110 | const TargetLibraryInfo &TLI, |
Eli Friedman | 8bc169c | 2012-02-27 20:46:07 +0000 | [diff] [blame] | 111 | bool RoundToAlign = false) { |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 112 | uint64_t Size; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 113 | if (getObjectSize(V, Size, DL, &TLI, RoundToAlign)) |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 114 | return Size; |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 115 | return MemoryLocation::UnknownSize; |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 118 | /// Returns true if we can prove that the object specified by V is smaller than |
| 119 | /// Size. |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 120 | static bool isObjectSmallerThan(const Value *V, uint64_t Size, |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 121 | const DataLayout &DL, |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 122 | const TargetLibraryInfo &TLI) { |
Nadav Rotem | abcc64f | 2013-04-09 18:16:05 +0000 | [diff] [blame] | 123 | // Note that the meanings of the "object" are slightly different in the |
| 124 | // following contexts: |
| 125 | // c1: llvm::getObjectSize() |
| 126 | // c2: llvm.objectsize() intrinsic |
| 127 | // c3: isObjectSmallerThan() |
| 128 | // c1 and c2 share the same meaning; however, the meaning of "object" in c3 |
| 129 | // refers to the "entire object". |
| 130 | // |
| 131 | // Consider this example: |
| 132 | // char *p = (char*)malloc(100) |
| 133 | // char *q = p+80; |
| 134 | // |
| 135 | // In the context of c1 and c2, the "object" pointed by q refers to the |
| 136 | // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20. |
| 137 | // |
| 138 | // However, in the context of c3, the "object" refers to the chunk of memory |
| 139 | // being allocated. So, the "object" has 100 bytes, and q points to the middle |
| 140 | // the "object". In case q is passed to isObjectSmallerThan() as the 1st |
| 141 | // parameter, before the llvm::getObjectSize() is called to get the size of |
| 142 | // entire object, we should: |
| 143 | // - either rewind the pointer q to the base-address of the object in |
| 144 | // question (in this case rewind to p), or |
| 145 | // - just give up. It is up to caller to make sure the pointer is pointing |
| 146 | // to the base address the object. |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 147 | // |
Nadav Rotem | abcc64f | 2013-04-09 18:16:05 +0000 | [diff] [blame] | 148 | // We go for 2nd option for simplicity. |
| 149 | if (!isIdentifiedObject(V)) |
| 150 | return false; |
| 151 | |
Eli Friedman | 8bc169c | 2012-02-27 20:46:07 +0000 | [diff] [blame] | 152 | // This function needs to use the aligned object size because we allow |
| 153 | // reads a bit past the end given sufficient alignment. |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 154 | uint64_t ObjectSize = getObjectSize(V, DL, TLI, /*RoundToAlign*/ true); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 155 | |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 156 | return ObjectSize != MemoryLocation::UnknownSize && ObjectSize < Size; |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 159 | /// Returns true if we can prove that the object specified by V has size Size. |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 160 | static bool isObjectSize(const Value *V, uint64_t Size, const DataLayout &DL, |
| 161 | const TargetLibraryInfo &TLI) { |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 162 | uint64_t ObjectSize = getObjectSize(V, DL, TLI); |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 163 | return ObjectSize != MemoryLocation::UnknownSize && ObjectSize == Size; |
Chris Lattner | 98ad816 | 2008-06-16 06:10:11 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 166 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 167 | // GetElementPtr Instruction Decomposition and Analysis |
| 168 | //===----------------------------------------------------------------------===// |
| 169 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 170 | /// Analyzes the specified value as a linear expression: "A*V + B", where A and |
| 171 | /// B are constant integers. |
| 172 | /// |
| 173 | /// Returns the scale and offset values as APInts and return V as a Value*, and |
| 174 | /// return whether we looked through any sign or zero extends. The incoming |
| 175 | /// Value is known to have IntegerType and it may already be sign or zero |
| 176 | /// extended. |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 177 | /// |
| 178 | /// Note that this looks through extends, so the high bits may not be |
| 179 | /// represented in the result. |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 180 | /*static*/ const Value *BasicAliasAnalysis::GetLinearExpression( |
| 181 | const Value *V, APInt &Scale, APInt &Offset, unsigned &ZExtBits, |
| 182 | unsigned &SExtBits, const DataLayout &DL, unsigned Depth, |
| 183 | AssumptionCache *AC, DominatorTree *DT, bool &NSW, bool &NUW) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 184 | assert(V->getType()->isIntegerTy() && "Not an integer value"); |
| 185 | |
| 186 | // Limit our recursion depth. |
| 187 | if (Depth == 6) { |
| 188 | Scale = 1; |
| 189 | Offset = 0; |
| 190 | return V; |
| 191 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 192 | |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 193 | if (const ConstantInt *Const = dyn_cast<ConstantInt>(V)) { |
| 194 | // if it's a constant, just convert it to an offset and remove the variable. |
| 195 | // If we've been called recursively the Offset bit width will be greater |
| 196 | // than the constant's (the Offset's always as wide as the outermost call), |
| 197 | // so we'll zext here and process any extension in the isa<SExtInst> & |
| 198 | // isa<ZExtInst> cases below. |
| 199 | Offset += Const->getValue().zextOrSelf(Offset.getBitWidth()); |
| 200 | assert(Scale == 0 && "Constant values don't have a scale"); |
| 201 | return V; |
| 202 | } |
| 203 | |
| 204 | if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(V)) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 205 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) { |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 206 | |
| 207 | // If we've been called recursively then Offset and Scale will be wider |
| 208 | // that the BOp operands. We'll always zext it here as we'll process sign |
| 209 | // extensions below (see the isa<SExtInst> / isa<ZExtInst> cases). |
| 210 | APInt RHS = RHSC->getValue().zextOrSelf(Offset.getBitWidth()); |
| 211 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 212 | switch (BOp->getOpcode()) { |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 213 | default: |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 214 | // We don't understand this instruction, so we can't decompose it any |
| 215 | // further. |
| 216 | Scale = 1; |
| 217 | Offset = 0; |
| 218 | return V; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 219 | case Instruction::Or: |
| 220 | // X|C == X+C if all the bits in C are unset in X. Otherwise we can't |
| 221 | // analyze it. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 222 | if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), DL, 0, AC, |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 223 | BOp, DT)) { |
| 224 | Scale = 1; |
| 225 | Offset = 0; |
| 226 | return V; |
| 227 | } |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 228 | // FALL THROUGH. |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 229 | case Instruction::Add: |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 230 | V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits, |
| 231 | SExtBits, DL, Depth + 1, AC, DT, NSW, NUW); |
| 232 | Offset += RHS; |
| 233 | break; |
| 234 | case Instruction::Sub: |
| 235 | V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits, |
| 236 | SExtBits, DL, Depth + 1, AC, DT, NSW, NUW); |
| 237 | Offset -= RHS; |
| 238 | break; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 239 | case Instruction::Mul: |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 240 | V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits, |
| 241 | SExtBits, DL, Depth + 1, AC, DT, NSW, NUW); |
| 242 | Offset *= RHS; |
| 243 | Scale *= RHS; |
| 244 | break; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 245 | case Instruction::Shl: |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 246 | V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits, |
| 247 | SExtBits, DL, Depth + 1, AC, DT, NSW, NUW); |
| 248 | Offset <<= RHS.getLimitedValue(); |
| 249 | Scale <<= RHS.getLimitedValue(); |
| 250 | // the semantics of nsw and nuw for left shifts don't match those of |
| 251 | // multiplications, so we won't propagate them. |
| 252 | NSW = NUW = false; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 253 | return V; |
| 254 | } |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 255 | |
| 256 | if (isa<OverflowingBinaryOperator>(BOp)) { |
| 257 | NUW &= BOp->hasNoUnsignedWrap(); |
| 258 | NSW &= BOp->hasNoSignedWrap(); |
| 259 | } |
| 260 | return V; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 264 | // Since GEP indices are sign extended anyway, we don't care about the high |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 265 | // bits of a sign or zero extended value - just scales and offsets. The |
| 266 | // extensions have to be consistent though. |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 267 | if (isa<SExtInst>(V) || isa<ZExtInst>(V)) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 268 | Value *CastOp = cast<CastInst>(V)->getOperand(0); |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 269 | unsigned NewWidth = V->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 270 | unsigned SmallWidth = CastOp->getType()->getPrimitiveSizeInBits(); |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 271 | unsigned OldZExtBits = ZExtBits, OldSExtBits = SExtBits; |
| 272 | const Value *Result = |
| 273 | GetLinearExpression(CastOp, Scale, Offset, ZExtBits, SExtBits, DL, |
| 274 | Depth + 1, AC, DT, NSW, NUW); |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 275 | |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 276 | // zext(zext(%x)) == zext(%x), and similiarly for sext; we'll handle this |
| 277 | // by just incrementing the number of bits we've extended by. |
| 278 | unsigned ExtendedBy = NewWidth - SmallWidth; |
| 279 | |
| 280 | if (isa<SExtInst>(V) && ZExtBits == 0) { |
| 281 | // sext(sext(%x, a), b) == sext(%x, a + b) |
| 282 | |
| 283 | if (NSW) { |
| 284 | // We haven't sign-wrapped, so it's valid to decompose sext(%x + c) |
| 285 | // into sext(%x) + sext(c). We'll sext the Offset ourselves: |
| 286 | unsigned OldWidth = Offset.getBitWidth(); |
| 287 | Offset = Offset.trunc(SmallWidth).sext(NewWidth).zextOrSelf(OldWidth); |
| 288 | } else { |
| 289 | // We may have signed-wrapped, so don't decompose sext(%x + c) into |
| 290 | // sext(%x) + sext(c) |
| 291 | Scale = 1; |
| 292 | Offset = 0; |
| 293 | Result = CastOp; |
| 294 | ZExtBits = OldZExtBits; |
| 295 | SExtBits = OldSExtBits; |
| 296 | } |
| 297 | SExtBits += ExtendedBy; |
| 298 | } else { |
| 299 | // sext(zext(%x, a), b) = zext(zext(%x, a), b) = zext(%x, a + b) |
| 300 | |
| 301 | if (!NUW) { |
| 302 | // We may have unsigned-wrapped, so don't decompose zext(%x + c) into |
| 303 | // zext(%x) + zext(c) |
| 304 | Scale = 1; |
| 305 | Offset = 0; |
| 306 | Result = CastOp; |
| 307 | ZExtBits = OldZExtBits; |
| 308 | SExtBits = OldSExtBits; |
| 309 | } |
| 310 | ZExtBits += ExtendedBy; |
| 311 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 313 | return Result; |
| 314 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 316 | Scale = 1; |
| 317 | Offset = 0; |
| 318 | return V; |
| 319 | } |
| 320 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 321 | /// If V is a symbolic pointer expression, decompose it into a base pointer |
| 322 | /// with a constant offset and a number of scaled symbolic offsets. |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 323 | /// |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 324 | /// The scaled symbolic offsets (represented by pairs of a Value* and a scale |
| 325 | /// in the VarIndices vector) are Value*'s that are known to be scaled by the |
| 326 | /// specified amount, but which may have other unrepresented high bits. As |
| 327 | /// such, the gep cannot necessarily be reconstructed from its decomposed form. |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 328 | /// |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 329 | /// When DataLayout is around, this function is capable of analyzing everything |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 330 | /// that GetUnderlyingObject can look through. To be able to do that |
| 331 | /// GetUnderlyingObject and DecomposeGEPExpression must use the same search |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 332 | /// depth (MaxLookupSearchDepth). When DataLayout not is around, it just looks |
| 333 | /// through pointer casts. |
Chandler Carruth | 17e0bc3 | 2015-08-06 07:33:15 +0000 | [diff] [blame] | 334 | /*static*/ const Value *BasicAliasAnalysis::DecomposeGEPExpression( |
| 335 | const Value *V, int64_t &BaseOffs, |
| 336 | SmallVectorImpl<VariableGEPIndex> &VarIndices, bool &MaxLookupReached, |
| 337 | const DataLayout &DL, AssumptionCache *AC, DominatorTree *DT) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 338 | // Limit recursion depth to limit compile time in crazy cases. |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 339 | unsigned MaxLookup = MaxLookupSearchDepth; |
| 340 | MaxLookupReached = false; |
Wei Mi | d67daae | 2015-08-05 23:40:30 +0000 | [diff] [blame] | 341 | SearchTimes++; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 343 | BaseOffs = 0; |
| 344 | do { |
| 345 | // See if this is a bitcast or GEP. |
| 346 | const Operator *Op = dyn_cast<Operator>(V); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 347 | if (!Op) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 348 | // The only non-operator case we can handle are GlobalAliases. |
| 349 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 350 | if (!GA->mayBeOverridden()) { |
| 351 | V = GA->getAliasee(); |
| 352 | continue; |
| 353 | } |
| 354 | } |
| 355 | return V; |
| 356 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 357 | |
Matt Arsenault | 70f4db88 | 2014-07-15 00:56:40 +0000 | [diff] [blame] | 358 | if (Op->getOpcode() == Instruction::BitCast || |
| 359 | Op->getOpcode() == Instruction::AddrSpaceCast) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 360 | V = Op->getOperand(0); |
| 361 | continue; |
| 362 | } |
Dan Gohman | 05b18f1 | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 364 | const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 365 | if (!GEPOp) { |
Dan Gohman | 0573b55 | 2011-05-24 18:24:08 +0000 | [diff] [blame] | 366 | // If it's not a GEP, hand it off to SimplifyInstruction to see if it |
| 367 | // can come up with something. This matches what GetUnderlyingObject does. |
| 368 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 369 | // TODO: Get a DominatorTree and AssumptionCache and use them here |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 370 | // (these are both now available in this function, but this should be |
| 371 | // updated when GetUnderlyingObject is updated). TLI should be |
| 372 | // provided also. |
Dan Gohman | 0573b55 | 2011-05-24 18:24:08 +0000 | [diff] [blame] | 373 | if (const Value *Simplified = |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 374 | SimplifyInstruction(const_cast<Instruction *>(I), DL)) { |
Dan Gohman | 0573b55 | 2011-05-24 18:24:08 +0000 | [diff] [blame] | 375 | V = Simplified; |
| 376 | continue; |
| 377 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 379 | return V; |
Dan Gohman | 0573b55 | 2011-05-24 18:24:08 +0000 | [diff] [blame] | 380 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 382 | // Don't attempt to analyze GEPs over unsized objects. |
Matt Arsenault | fa25272 | 2013-09-27 22:18:51 +0000 | [diff] [blame] | 383 | if (!GEPOp->getOperand(0)->getType()->getPointerElementType()->isSized()) |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 384 | return V; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 385 | |
Matt Arsenault | a8fe22b | 2013-11-16 00:36:43 +0000 | [diff] [blame] | 386 | unsigned AS = GEPOp->getPointerAddressSpace(); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 387 | // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices. |
| 388 | gep_type_iterator GTI = gep_type_begin(GEPOp); |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 389 | for (User::const_op_iterator I = GEPOp->op_begin() + 1, E = GEPOp->op_end(); |
| 390 | I != E; ++I) { |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 391 | const Value *Index = *I; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 392 | // Compute the (potentially symbolic) offset in bytes for this index. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 393 | if (StructType *STy = dyn_cast<StructType>(*GTI++)) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 394 | // For a struct, add the member offset. |
| 395 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 396 | if (FieldNo == 0) |
| 397 | continue; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 398 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 399 | BaseOffs += DL.getStructLayout(STy)->getElementOffset(FieldNo); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 400 | continue; |
| 401 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 403 | // For an array/pointer, add the element offset, explicitly scaled. |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 404 | if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) { |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 405 | if (CIdx->isZero()) |
| 406 | continue; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 407 | BaseOffs += DL.getTypeAllocSize(*GTI) * CIdx->getSExtValue(); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 408 | continue; |
| 409 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 410 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 411 | uint64_t Scale = DL.getTypeAllocSize(*GTI); |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 412 | unsigned ZExtBits = 0, SExtBits = 0; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 414 | // If the integer type is smaller than the pointer size, it is implicitly |
| 415 | // sign extended to pointer size. |
Matt Arsenault | fa25272 | 2013-09-27 22:18:51 +0000 | [diff] [blame] | 416 | unsigned Width = Index->getType()->getIntegerBitWidth(); |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 417 | unsigned PointerSize = DL.getPointerSizeInBits(AS); |
| 418 | if (PointerSize > Width) |
| 419 | SExtBits += PointerSize - Width; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 421 | // Use GetLinearExpression to decompose the index into a C1*V+C2 form. |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 422 | APInt IndexScale(Width, 0), IndexOffset(Width, 0); |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 423 | bool NSW = true, NUW = true; |
| 424 | Index = GetLinearExpression(Index, IndexScale, IndexOffset, ZExtBits, |
| 425 | SExtBits, DL, 0, AC, DT, NSW, NUW); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 427 | // The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale. |
| 428 | // This gives us an aggregate computation of (C1*Scale)*V + C2*Scale. |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 429 | BaseOffs += IndexOffset.getSExtValue() * Scale; |
Eli Friedman | ab3a128 | 2010-09-15 20:08:03 +0000 | [diff] [blame] | 430 | Scale *= IndexScale.getSExtValue(); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 432 | // If we already had an occurrence of this index variable, merge this |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 433 | // scale into it. For example, we want to handle: |
| 434 | // A[x][x] -> x*16 + x*4 -> x*20 |
| 435 | // This also ensures that 'x' only appears in the index list once. |
| 436 | for (unsigned i = 0, e = VarIndices.size(); i != e; ++i) { |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 437 | if (VarIndices[i].V == Index && VarIndices[i].ZExtBits == ZExtBits && |
| 438 | VarIndices[i].SExtBits == SExtBits) { |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 439 | Scale += VarIndices[i].Scale; |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 440 | VarIndices.erase(VarIndices.begin() + i); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 441 | break; |
| 442 | } |
| 443 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 445 | // Make sure that we have a scale that makes sense for this target's |
| 446 | // pointer size. |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 447 | if (unsigned ShiftBits = 64 - PointerSize) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 448 | Scale <<= ShiftBits; |
Eli Friedman | ab3a128 | 2010-09-15 20:08:03 +0000 | [diff] [blame] | 449 | Scale = (int64_t)Scale >> ShiftBits; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 450 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 452 | if (Scale) { |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 453 | VariableGEPIndex Entry = {Index, ZExtBits, SExtBits, |
Jeffrey Yasskin | 6381c01 | 2011-07-27 06:22:51 +0000 | [diff] [blame] | 454 | static_cast<int64_t>(Scale)}; |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 455 | VarIndices.push_back(Entry); |
| 456 | } |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 457 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 459 | // Analyze the base pointer next. |
| 460 | V = GEPOp->getOperand(0); |
| 461 | } while (--MaxLookup); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 463 | // If the chain of expressions is too deep, just return early. |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 464 | MaxLookupReached = true; |
Wei Mi | d67daae | 2015-08-05 23:40:30 +0000 | [diff] [blame] | 465 | SearchLimitReached++; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 466 | return V; |
| 467 | } |
| 468 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 469 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 470 | // BasicAliasAnalysis Pass |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 471 | //===----------------------------------------------------------------------===// |
| 472 | |
Chandler Carruth | 17e0bc3 | 2015-08-06 07:33:15 +0000 | [diff] [blame] | 473 | // Register the pass... |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 474 | char BasicAliasAnalysis::ID = 0; |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 475 | INITIALIZE_AG_PASS_BEGIN(BasicAliasAnalysis, AliasAnalysis, "basicaa", |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 476 | "Basic Alias Analysis (stateless AA impl)", false, |
| 477 | true, false) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 478 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 479 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 480 | INITIALIZE_AG_PASS_END(BasicAliasAnalysis, AliasAnalysis, "basicaa", |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 481 | "Basic Alias Analysis (stateless AA impl)", false, true, |
| 482 | false) |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 484 | ImmutablePass *llvm::createBasicAliasAnalysisPass() { |
| 485 | return new BasicAliasAnalysis(); |
| 486 | } |
| 487 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 488 | /// Returns whether the given pointer value points to memory that is local to |
| 489 | /// the function, with global constants being considered local to all |
| 490 | /// functions. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 491 | bool BasicAliasAnalysis::pointsToConstantMemory(const MemoryLocation &Loc, |
| 492 | bool OrLocal) { |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 493 | assert(Visited.empty() && "Visited must be cleared after use!"); |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 494 | |
Dan Gohman | 142ff82 | 2010-11-08 20:26:19 +0000 | [diff] [blame] | 495 | unsigned MaxLookup = 8; |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 496 | SmallVector<const Value *, 16> Worklist; |
| 497 | Worklist.push_back(Loc.Ptr); |
| 498 | do { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 499 | const Value *V = GetUnderlyingObject(Worklist.pop_back_val(), *DL); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 500 | if (!Visited.insert(V).second) { |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 501 | Visited.clear(); |
| 502 | return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal); |
| 503 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 504 | |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 505 | // An alloca instruction defines local memory. |
| 506 | if (OrLocal && isa<AllocaInst>(V)) |
| 507 | continue; |
| 508 | |
| 509 | // A global constant counts as local memory for our purposes. |
| 510 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 511 | // Note: this doesn't require GV to be "ODR" because it isn't legal for a |
| 512 | // global to be marked constant in some modules and non-constant in |
| 513 | // others. GV may even be a declaration, not a definition. |
| 514 | if (!GV->isConstant()) { |
| 515 | Visited.clear(); |
| 516 | return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal); |
| 517 | } |
| 518 | continue; |
| 519 | } |
| 520 | |
| 521 | // If both select values point to local memory, then so does the select. |
| 522 | if (const SelectInst *SI = dyn_cast<SelectInst>(V)) { |
| 523 | Worklist.push_back(SI->getTrueValue()); |
| 524 | Worklist.push_back(SI->getFalseValue()); |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | // If all values incoming to a phi node point to local memory, then so does |
| 529 | // the phi. |
| 530 | if (const PHINode *PN = dyn_cast<PHINode>(V)) { |
Dan Gohman | 142ff82 | 2010-11-08 20:26:19 +0000 | [diff] [blame] | 531 | // Don't bother inspecting phi nodes with many operands. |
| 532 | if (PN->getNumIncomingValues() > MaxLookup) { |
| 533 | Visited.clear(); |
| 534 | return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal); |
| 535 | } |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 536 | for (Value *IncValue : PN->incoming_values()) |
| 537 | Worklist.push_back(IncValue); |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 538 | continue; |
| 539 | } |
| 540 | |
| 541 | // Otherwise be conservative. |
| 542 | Visited.clear(); |
| 543 | return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal); |
| 544 | |
Dan Gohman | 142ff82 | 2010-11-08 20:26:19 +0000 | [diff] [blame] | 545 | } while (!Worklist.empty() && --MaxLookup); |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 546 | |
| 547 | Visited.clear(); |
Dan Gohman | 142ff82 | 2010-11-08 20:26:19 +0000 | [diff] [blame] | 548 | return Worklist.empty(); |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 551 | // FIXME: This code is duplicated with MemoryLocation and should be hoisted to |
| 552 | // some common utility location. |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 553 | static bool isMemsetPattern16(const Function *MS, |
| 554 | const TargetLibraryInfo &TLI) { |
| 555 | if (TLI.has(LibFunc::memset_pattern16) && |
| 556 | MS->getName() == "memset_pattern16") { |
| 557 | FunctionType *MemsetType = MS->getFunctionType(); |
| 558 | if (!MemsetType->isVarArg() && MemsetType->getNumParams() == 3 && |
| 559 | isa<PointerType>(MemsetType->getParamType(0)) && |
| 560 | isa<PointerType>(MemsetType->getParamType(1)) && |
| 561 | isa<IntegerType>(MemsetType->getParamType(2))) |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | return false; |
| 566 | } |
| 567 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 568 | /// Returns the behavior when calling the given call site. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 569 | FunctionModRefBehavior |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 570 | BasicAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { |
| 571 | if (CS.doesNotAccessMemory()) |
| 572 | // Can't do better than this. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 573 | return FMRB_DoesNotAccessMemory; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 574 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 575 | FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 576 | |
| 577 | // If the callsite knows it only reads memory, don't return worse |
| 578 | // than that. |
| 579 | if (CS.onlyReadsMemory()) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 580 | Min = FMRB_OnlyReadsMemory; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 581 | |
Igor Laevsky | 39d662f | 2015-07-11 10:30:36 +0000 | [diff] [blame] | 582 | if (CS.onlyAccessesArgMemory()) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 583 | Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees); |
Igor Laevsky | 39d662f | 2015-07-11 10:30:36 +0000 | [diff] [blame] | 584 | |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 585 | // The AliasAnalysis base class has some smarts, lets use them. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 586 | return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 589 | /// Returns the behavior when calling the given function. For use when the call |
| 590 | /// site is not known. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 591 | FunctionModRefBehavior |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 592 | BasicAliasAnalysis::getModRefBehavior(const Function *F) { |
Dan Gohman | e461d7d | 2010-11-08 16:08:43 +0000 | [diff] [blame] | 593 | // If the function declares it doesn't access memory, we can't do better. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 594 | if (F->doesNotAccessMemory()) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 595 | return FMRB_DoesNotAccessMemory; |
Dan Gohman | e461d7d | 2010-11-08 16:08:43 +0000 | [diff] [blame] | 596 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 597 | FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior; |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 598 | |
Dan Gohman | e461d7d | 2010-11-08 16:08:43 +0000 | [diff] [blame] | 599 | // If the function declares it only reads memory, go with that. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 600 | if (F->onlyReadsMemory()) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 601 | Min = FMRB_OnlyReadsMemory; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 602 | |
Igor Laevsky | 39d662f | 2015-07-11 10:30:36 +0000 | [diff] [blame] | 603 | if (F->onlyAccessesArgMemory()) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 604 | Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees); |
Igor Laevsky | 39d662f | 2015-07-11 10:30:36 +0000 | [diff] [blame] | 605 | |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 606 | const TargetLibraryInfo &TLI = |
| 607 | getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 608 | if (isMemsetPattern16(F, TLI)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 609 | Min = FMRB_OnlyAccessesArgumentPointees; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 610 | |
Dan Gohman | e461d7d | 2010-11-08 16:08:43 +0000 | [diff] [blame] | 611 | // Otherwise be conservative. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 612 | return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 613 | } |
Owen Anderson | 98a3617 | 2009-02-05 23:36:27 +0000 | [diff] [blame] | 614 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 615 | ModRefInfo BasicAliasAnalysis::getArgModRefInfo(ImmutableCallSite CS, |
| 616 | unsigned ArgIdx) { |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 617 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 618 | switch (II->getIntrinsicID()) { |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 619 | default: |
| 620 | break; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 621 | case Intrinsic::memset: |
| 622 | case Intrinsic::memcpy: |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 623 | case Intrinsic::memmove: |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 624 | assert((ArgIdx == 0 || ArgIdx == 1) && |
| 625 | "Invalid argument index for memory intrinsic"); |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 626 | return ArgIdx ? MRI_Ref : MRI_Mod; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // We can bound the aliasing properties of memset_pattern16 just as we can |
| 630 | // for memcpy/memset. This is particularly important because the |
| 631 | // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16 |
| 632 | // whenever possible. |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 633 | if (CS.getCalledFunction() && |
| 634 | isMemsetPattern16(CS.getCalledFunction(), *TLI)) { |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 635 | assert((ArgIdx == 0 || ArgIdx == 1) && |
| 636 | "Invalid argument index for memset_pattern16"); |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 637 | return ArgIdx ? MRI_Ref : MRI_Mod; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 638 | } |
| 639 | // FIXME: Handle memset_pattern4 and memset_pattern8 also. |
| 640 | |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 641 | return AliasAnalysis::getArgModRefInfo(CS, ArgIdx); |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Hal Finkel | 9304691 | 2014-07-25 21:13:35 +0000 | [diff] [blame] | 644 | static bool isAssumeIntrinsic(ImmutableCallSite CS) { |
| 645 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction()); |
| 646 | if (II && II->getIntrinsicID() == Intrinsic::assume) |
| 647 | return true; |
| 648 | |
| 649 | return false; |
| 650 | } |
| 651 | |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 652 | bool BasicAliasAnalysis::doInitialization(Module &M) { |
| 653 | InitializeAliasAnalysis(this, &M.getDataLayout()); |
| 654 | return true; |
| 655 | } |
| 656 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 657 | /// Checks to see if the specified callsite can clobber the specified memory |
| 658 | /// object. |
| 659 | /// |
| 660 | /// Since we only look at local properties of this function, we really can't |
| 661 | /// say much about this query. We do, however, use simple "address taken" |
| 662 | /// analysis on local objects. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 663 | ModRefInfo BasicAliasAnalysis::getModRefInfo(ImmutableCallSite CS, |
| 664 | const MemoryLocation &Loc) { |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 665 | assert(notDifferentParent(CS.getInstruction(), Loc.Ptr) && |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 666 | "AliasAnalysis query involving multiple functions!"); |
| 667 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 668 | const Value *Object = GetUnderlyingObject(Loc.Ptr, *DL); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 669 | |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 670 | // If this is a tail call and Loc.Ptr points to a stack location, we know that |
Chris Lattner | d6a49ad | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 671 | // the tail call cannot access or modify the local stack. |
| 672 | // We cannot exclude byval arguments here; these belong to the caller of |
| 673 | // the current function not to the current function, and a tail callee |
| 674 | // may reference them. |
| 675 | if (isa<AllocaInst>(Object)) |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 676 | if (const CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) |
Chris Lattner | d6a49ad | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 677 | if (CI->isTailCall()) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 678 | return MRI_NoModRef; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 679 | |
Chris Lattner | d6a49ad | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 680 | // If the pointer is to a locally allocated object that does not escape, |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 681 | // then the call can not mod/ref the pointer unless the call takes the pointer |
| 682 | // as an argument, and itself doesn't capture it. |
Chris Lattner | 1e7b37e | 2009-11-23 16:46:41 +0000 | [diff] [blame] | 683 | if (!isa<Constant>(Object) && CS.getInstruction() != Object && |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 684 | isNonEscapingLocalObject(Object)) { |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 685 | bool PassedAsArg = false; |
| 686 | unsigned ArgNo = 0; |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 687 | for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 688 | CI != CE; ++CI, ++ArgNo) { |
Chris Lattner | 026f5e6 | 2011-05-23 05:15:43 +0000 | [diff] [blame] | 689 | // Only look at the no-capture or byval pointer arguments. If this |
| 690 | // pointer were passed to arguments that were neither of these, then it |
| 691 | // couldn't be no-capture. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 692 | if (!(*CI)->getType()->isPointerTy() || |
Nick Lewycky | 612d70b | 2011-11-20 19:09:04 +0000 | [diff] [blame] | 693 | (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo))) |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 694 | continue; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 695 | |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 696 | // If this is a no-capture pointer argument, see if we can tell that it |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 697 | // is impossible to alias the pointer we're checking. If not, we have to |
| 698 | // assume that the call could touch the pointer, even though it doesn't |
| 699 | // escape. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 700 | if (!isNoAlias(MemoryLocation(*CI), MemoryLocation(Object))) { |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 701 | PassedAsArg = true; |
| 702 | break; |
| 703 | } |
| 704 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 705 | |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 706 | if (!PassedAsArg) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 707 | return MRI_NoModRef; |
Chris Lattner | d6a49ad | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Hal Finkel | 9304691 | 2014-07-25 21:13:35 +0000 | [diff] [blame] | 710 | // While the assume intrinsic is marked as arbitrarily writing so that |
| 711 | // proper control dependencies will be maintained, it never aliases any |
| 712 | // particular memory location. |
| 713 | if (isAssumeIntrinsic(CS)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 714 | return MRI_NoModRef; |
Hal Finkel | 9304691 | 2014-07-25 21:13:35 +0000 | [diff] [blame] | 715 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 716 | // The AliasAnalysis base class has some smarts, lets use them. |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 717 | return AliasAnalysis::getModRefInfo(CS, Loc); |
Dan Gohman | 64d842e | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 718 | } |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 719 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 720 | ModRefInfo BasicAliasAnalysis::getModRefInfo(ImmutableCallSite CS1, |
| 721 | ImmutableCallSite CS2) { |
Hal Finkel | 9304691 | 2014-07-25 21:13:35 +0000 | [diff] [blame] | 722 | // While the assume intrinsic is marked as arbitrarily writing so that |
| 723 | // proper control dependencies will be maintained, it never aliases any |
| 724 | // particular memory location. |
| 725 | if (isAssumeIntrinsic(CS1) || isAssumeIntrinsic(CS2)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 726 | return MRI_NoModRef; |
Hal Finkel | 9304691 | 2014-07-25 21:13:35 +0000 | [diff] [blame] | 727 | |
| 728 | // The AliasAnalysis base class has some smarts, lets use them. |
| 729 | return AliasAnalysis::getModRefInfo(CS1, CS2); |
| 730 | } |
| 731 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 732 | /// Provide ad-hoc rules to disambiguate accesses through two GEP operators, |
| 733 | /// both having the exact same pointer operand. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 734 | static AliasResult aliasSameBasePointerGEPs(const GEPOperator *GEP1, |
| 735 | uint64_t V1Size, |
| 736 | const GEPOperator *GEP2, |
| 737 | uint64_t V2Size, |
| 738 | const DataLayout &DL) { |
Ahmed Bougacha | 29efe3b | 2015-02-07 17:04:29 +0000 | [diff] [blame] | 739 | |
| 740 | assert(GEP1->getPointerOperand() == GEP2->getPointerOperand() && |
| 741 | "Expected GEPs with the same pointer operand"); |
| 742 | |
| 743 | // Try to determine whether GEP1 and GEP2 index through arrays, into structs, |
| 744 | // such that the struct field accesses provably cannot alias. |
| 745 | // We also need at least two indices (the pointer, and the struct field). |
| 746 | if (GEP1->getNumIndices() != GEP2->getNumIndices() || |
| 747 | GEP1->getNumIndices() < 2) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 748 | return MayAlias; |
Ahmed Bougacha | 29efe3b | 2015-02-07 17:04:29 +0000 | [diff] [blame] | 749 | |
| 750 | // If we don't know the size of the accesses through both GEPs, we can't |
| 751 | // determine whether the struct fields accessed can't alias. |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 752 | if (V1Size == MemoryLocation::UnknownSize || |
| 753 | V2Size == MemoryLocation::UnknownSize) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 754 | return MayAlias; |
Ahmed Bougacha | 29efe3b | 2015-02-07 17:04:29 +0000 | [diff] [blame] | 755 | |
| 756 | ConstantInt *C1 = |
| 757 | dyn_cast<ConstantInt>(GEP1->getOperand(GEP1->getNumOperands() - 1)); |
| 758 | ConstantInt *C2 = |
| 759 | dyn_cast<ConstantInt>(GEP2->getOperand(GEP2->getNumOperands() - 1)); |
| 760 | |
| 761 | // If the last (struct) indices aren't constants, we can't say anything. |
| 762 | // If they're identical, the other indices might be also be dynamically |
| 763 | // equal, so the GEPs can alias. |
| 764 | if (!C1 || !C2 || C1 == C2) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 765 | return MayAlias; |
Ahmed Bougacha | 29efe3b | 2015-02-07 17:04:29 +0000 | [diff] [blame] | 766 | |
| 767 | // Find the last-indexed type of the GEP, i.e., the type you'd get if |
| 768 | // you stripped the last index. |
| 769 | // On the way, look at each indexed type. If there's something other |
| 770 | // than an array, different indices can lead to different final types. |
| 771 | SmallVector<Value *, 8> IntermediateIndices; |
| 772 | |
| 773 | // Insert the first index; we don't need to check the type indexed |
| 774 | // through it as it only drops the pointer indirection. |
| 775 | assert(GEP1->getNumIndices() > 1 && "Not enough GEP indices to examine"); |
| 776 | IntermediateIndices.push_back(GEP1->getOperand(1)); |
| 777 | |
| 778 | // Insert all the remaining indices but the last one. |
| 779 | // Also, check that they all index through arrays. |
| 780 | for (unsigned i = 1, e = GEP1->getNumIndices() - 1; i != e; ++i) { |
| 781 | if (!isa<ArrayType>(GetElementPtrInst::getIndexedType( |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 782 | GEP1->getSourceElementType(), IntermediateIndices))) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 783 | return MayAlias; |
Ahmed Bougacha | 29efe3b | 2015-02-07 17:04:29 +0000 | [diff] [blame] | 784 | IntermediateIndices.push_back(GEP1->getOperand(i + 1)); |
| 785 | } |
| 786 | |
| 787 | StructType *LastIndexedStruct = |
| 788 | dyn_cast<StructType>(GetElementPtrInst::getIndexedType( |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 789 | GEP1->getSourceElementType(), IntermediateIndices)); |
Ahmed Bougacha | 29efe3b | 2015-02-07 17:04:29 +0000 | [diff] [blame] | 790 | |
| 791 | if (!LastIndexedStruct) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 792 | return MayAlias; |
Ahmed Bougacha | 29efe3b | 2015-02-07 17:04:29 +0000 | [diff] [blame] | 793 | |
| 794 | // We know that: |
| 795 | // - both GEPs begin indexing from the exact same pointer; |
| 796 | // - the last indices in both GEPs are constants, indexing into a struct; |
| 797 | // - said indices are different, hence, the pointed-to fields are different; |
| 798 | // - both GEPs only index through arrays prior to that. |
| 799 | // |
| 800 | // This lets us determine that the struct that GEP1 indexes into and the |
| 801 | // struct that GEP2 indexes into must either precisely overlap or be |
| 802 | // completely disjoint. Because they cannot partially overlap, indexing into |
| 803 | // different non-overlapping fields of the struct will never alias. |
| 804 | |
| 805 | // Therefore, the only remaining thing needed to show that both GEPs can't |
| 806 | // alias is that the fields are not overlapping. |
| 807 | const StructLayout *SL = DL.getStructLayout(LastIndexedStruct); |
| 808 | const uint64_t StructSize = SL->getSizeInBytes(); |
| 809 | const uint64_t V1Off = SL->getElementOffset(C1->getZExtValue()); |
| 810 | const uint64_t V2Off = SL->getElementOffset(C2->getZExtValue()); |
| 811 | |
| 812 | auto EltsDontOverlap = [StructSize](uint64_t V1Off, uint64_t V1Size, |
| 813 | uint64_t V2Off, uint64_t V2Size) { |
| 814 | return V1Off < V2Off && V1Off + V1Size <= V2Off && |
| 815 | ((V2Off + V2Size <= StructSize) || |
| 816 | (V2Off + V2Size - StructSize <= V1Off)); |
| 817 | }; |
| 818 | |
| 819 | if (EltsDontOverlap(V1Off, V1Size, V2Off, V2Size) || |
| 820 | EltsDontOverlap(V2Off, V2Size, V1Off, V1Size)) |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 821 | return NoAlias; |
Ahmed Bougacha | 29efe3b | 2015-02-07 17:04:29 +0000 | [diff] [blame] | 822 | |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 823 | return MayAlias; |
Ahmed Bougacha | 29efe3b | 2015-02-07 17:04:29 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 826 | /// Provides a bunch of ad-hoc rules to disambiguate a GEP instruction against |
| 827 | /// another pointer. |
Chris Lattner | a99edbe | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 828 | /// |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 829 | /// We know that V1 is a GEP, but we don't know anything about V2. |
| 830 | /// UnderlyingV1 is GetUnderlyingObject(GEP1, DL), UnderlyingV2 is the same for |
| 831 | /// V2. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 832 | AliasResult BasicAliasAnalysis::aliasGEP( |
| 833 | const GEPOperator *GEP1, uint64_t V1Size, const AAMDNodes &V1AAInfo, |
| 834 | const Value *V2, uint64_t V2Size, const AAMDNodes &V2AAInfo, |
| 835 | const Value *UnderlyingV1, const Value *UnderlyingV2) { |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 836 | int64_t GEP1BaseOffset; |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 837 | bool GEP1MaxLookupReached; |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 838 | SmallVector<VariableGEPIndex, 4> GEP1VariableIndices; |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 839 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 840 | // We have to get two AssumptionCaches here because GEP1 and V2 may be from |
| 841 | // different functions. |
| 842 | // FIXME: This really doesn't make any sense. We get a dominator tree below |
| 843 | // that can only refer to a single function. But this function (aliasGEP) is |
| 844 | // a method on an immutable pass that can be called when there *isn't* |
| 845 | // a single function. The old pass management layer makes this "work", but |
| 846 | // this isn't really a clean solution. |
| 847 | AssumptionCacheTracker &ACT = getAnalysis<AssumptionCacheTracker>(); |
| 848 | AssumptionCache *AC1 = nullptr, *AC2 = nullptr; |
| 849 | if (auto *GEP1I = dyn_cast<Instruction>(GEP1)) |
| 850 | AC1 = &ACT.getAssumptionCache( |
| 851 | const_cast<Function &>(*GEP1I->getParent()->getParent())); |
| 852 | if (auto *I2 = dyn_cast<Instruction>(V2)) |
| 853 | AC2 = &ACT.getAssumptionCache( |
| 854 | const_cast<Function &>(*I2->getParent()->getParent())); |
| 855 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 856 | DominatorTreeWrapperPass *DTWP = |
| 857 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 858 | DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr; |
| 859 | |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 860 | // If we have two gep instructions with must-alias or not-alias'ing base |
| 861 | // pointers, figure out if the indexes to the GEP tell us anything about the |
| 862 | // derived pointer. |
Chris Lattner | a99edbe | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 863 | if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) { |
Arnold Schwaighofer | aadf104 | 2013-03-26 18:07:53 +0000 | [diff] [blame] | 864 | // Do the base pointers alias? |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 865 | AliasResult BaseAlias = |
| 866 | aliasCheck(UnderlyingV1, MemoryLocation::UnknownSize, AAMDNodes(), |
| 867 | UnderlyingV2, MemoryLocation::UnknownSize, AAMDNodes()); |
Arnold Schwaighofer | aadf104 | 2013-03-26 18:07:53 +0000 | [diff] [blame] | 868 | |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 869 | // Check for geps of non-aliasing underlying pointers where the offsets are |
| 870 | // identical. |
Arnold Schwaighofer | aadf104 | 2013-03-26 18:07:53 +0000 | [diff] [blame] | 871 | if ((BaseAlias == MayAlias) && V1Size == V2Size) { |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 872 | // Do the base pointers alias assuming type and size. |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 873 | AliasResult PreciseBaseAlias = aliasCheck(UnderlyingV1, V1Size, V1AAInfo, |
| 874 | UnderlyingV2, V2Size, V2AAInfo); |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 875 | if (PreciseBaseAlias == NoAlias) { |
| 876 | // See if the computed offset from the common pointer tells us about the |
| 877 | // relation of the resulting pointer. |
| 878 | int64_t GEP2BaseOffset; |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 879 | bool GEP2MaxLookupReached; |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 880 | SmallVector<VariableGEPIndex, 4> GEP2VariableIndices; |
| 881 | const Value *GEP2BasePtr = |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 882 | DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 883 | GEP2MaxLookupReached, *DL, AC2, DT); |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 884 | const Value *GEP1BasePtr = |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 885 | DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 886 | GEP1MaxLookupReached, *DL, AC1, DT); |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 887 | // DecomposeGEPExpression and GetUnderlyingObject should return the |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 888 | // same result except when DecomposeGEPExpression has no DataLayout. |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 889 | if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 890 | assert(!DL && |
| 891 | "DecomposeGEPExpression and GetUnderlyingObject disagree!"); |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 892 | return MayAlias; |
| 893 | } |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 894 | // If the max search depth is reached the result is undefined |
| 895 | if (GEP2MaxLookupReached || GEP1MaxLookupReached) |
| 896 | return MayAlias; |
| 897 | |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 898 | // Same offsets. |
| 899 | if (GEP1BaseOffset == GEP2BaseOffset && |
Benjamin Kramer | 147644d | 2014-04-18 19:48:03 +0000 | [diff] [blame] | 900 | GEP1VariableIndices == GEP2VariableIndices) |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 901 | return NoAlias; |
| 902 | GEP1VariableIndices.clear(); |
| 903 | } |
| 904 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 905 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 906 | // If we get a No or May, then return it immediately, no amount of analysis |
| 907 | // will improve this situation. |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 908 | if (BaseAlias != MustAlias) |
| 909 | return BaseAlias; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 910 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 911 | // Otherwise, we have a MustAlias. Since the base pointers alias each other |
| 912 | // exactly, see if the computed offset from the common pointer tells us |
| 913 | // about the relation of the resulting pointer. |
| 914 | const Value *GEP1BasePtr = |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 915 | DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 916 | GEP1MaxLookupReached, *DL, AC1, DT); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 917 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 918 | int64_t GEP2BaseOffset; |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 919 | bool GEP2MaxLookupReached; |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 920 | SmallVector<VariableGEPIndex, 4> GEP2VariableIndices; |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 921 | const Value *GEP2BasePtr = |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 922 | DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 923 | GEP2MaxLookupReached, *DL, AC2, DT); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 924 | |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 925 | // DecomposeGEPExpression and GetUnderlyingObject should return the |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 926 | // same result except when DecomposeGEPExpression has no DataLayout. |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 927 | if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) { |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 928 | assert(!DL && "DecomposeGEPExpression and GetUnderlyingObject disagree!"); |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 929 | return MayAlias; |
| 930 | } |
Ahmed Bougacha | 29efe3b | 2015-02-07 17:04:29 +0000 | [diff] [blame] | 931 | |
| 932 | // If we know the two GEPs are based off of the exact same pointer (and not |
| 933 | // just the same underlying object), see if that tells us anything about |
| 934 | // the resulting pointers. |
| 935 | if (DL && GEP1->getPointerOperand() == GEP2->getPointerOperand()) { |
| 936 | AliasResult R = aliasSameBasePointerGEPs(GEP1, V1Size, GEP2, V2Size, *DL); |
| 937 | // If we couldn't find anything interesting, don't abandon just yet. |
| 938 | if (R != MayAlias) |
| 939 | return R; |
| 940 | } |
| 941 | |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 942 | // If the max search depth is reached the result is undefined |
| 943 | if (GEP2MaxLookupReached || GEP1MaxLookupReached) |
| 944 | return MayAlias; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 945 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 946 | // Subtract the GEP2 pointer from the GEP1 pointer to find out their |
| 947 | // symbolic difference. |
| 948 | GEP1BaseOffset -= GEP2BaseOffset; |
Dan Gohman | ad867b0 | 2010-08-03 20:23:52 +0000 | [diff] [blame] | 949 | GetIndexDifference(GEP1VariableIndices, GEP2VariableIndices); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 950 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 951 | } else { |
| 952 | // Check to see if these two pointers are related by the getelementptr |
| 953 | // instruction. If one pointer is a GEP with a non-zero index of the other |
| 954 | // pointer, we know they cannot alias. |
Chris Lattner | 5c1cfc2 | 2009-11-26 16:52:32 +0000 | [diff] [blame] | 955 | |
| 956 | // If both accesses are unknown size, we can't do anything useful here. |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 957 | if (V1Size == MemoryLocation::UnknownSize && |
| 958 | V2Size == MemoryLocation::UnknownSize) |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 959 | return MayAlias; |
Chris Lattner | 6ea17f77f | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 960 | |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 961 | AliasResult R = aliasCheck(UnderlyingV1, MemoryLocation::UnknownSize, |
| 962 | AAMDNodes(), V2, V2Size, V2AAInfo); |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 963 | if (R != MustAlias) |
| 964 | // If V2 may alias GEP base pointer, conservatively returns MayAlias. |
| 965 | // If V2 is known not to alias GEP base pointer, then the two values |
| 966 | // cannot alias per GEP semantics: "A pointer value formed from a |
| 967 | // getelementptr instruction is associated with the addresses associated |
| 968 | // with the first operand of the getelementptr". |
| 969 | return R; |
Chris Lattner | 6ea17f77f | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 971 | const Value *GEP1BasePtr = |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 972 | DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 973 | GEP1MaxLookupReached, *DL, AC1, DT); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 974 | |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 975 | // DecomposeGEPExpression and GetUnderlyingObject should return the |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 976 | // same result except when DecomposeGEPExpression has no DataLayout. |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 977 | if (GEP1BasePtr != UnderlyingV1) { |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 978 | assert(!DL && "DecomposeGEPExpression and GetUnderlyingObject disagree!"); |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 979 | return MayAlias; |
Chris Lattner | 6ea17f77f | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 980 | } |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 981 | // If the max search depth is reached the result is undefined |
| 982 | if (GEP1MaxLookupReached) |
| 983 | return MayAlias; |
Chris Lattner | 6ea17f77f | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 984 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 986 | // In the two GEP Case, if there is no difference in the offsets of the |
| 987 | // computed pointers, the resultant pointers are a must alias. This |
| 988 | // hapens when we have two lexically identical GEP's (for example). |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 989 | // |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 990 | // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2 |
| 991 | // must aliases the GEP, the end result is a must alias also. |
| 992 | if (GEP1BaseOffset == 0 && GEP1VariableIndices.empty()) |
Evan Cheng | c1eed9d | 2009-10-14 06:41:49 +0000 | [diff] [blame] | 993 | return MustAlias; |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 994 | |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 995 | // If there is a constant difference between the pointers, but the difference |
| 996 | // is less than the size of the associated memory object, then we know |
| 997 | // that the objects are partially overlapping. If the difference is |
| 998 | // greater, we know they do not overlap. |
Dan Gohman | c4bf5ca | 2010-12-13 22:50:24 +0000 | [diff] [blame] | 999 | if (GEP1BaseOffset != 0 && GEP1VariableIndices.empty()) { |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1000 | if (GEP1BaseOffset >= 0) { |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 1001 | if (V2Size != MemoryLocation::UnknownSize) { |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1002 | if ((uint64_t)GEP1BaseOffset < V2Size) |
| 1003 | return PartialAlias; |
| 1004 | return NoAlias; |
| 1005 | } |
| 1006 | } else { |
Arnold Schwaighofer | e3ac099 | 2014-01-16 04:53:18 +0000 | [diff] [blame] | 1007 | // We have the situation where: |
| 1008 | // + + |
| 1009 | // | BaseOffset | |
| 1010 | // ---------------->| |
| 1011 | // |-->V1Size |-------> V2Size |
| 1012 | // GEP1 V2 |
| 1013 | // We need to know that V2Size is not unknown, otherwise we might have |
| 1014 | // stripped a gep with negative index ('gep <ptr>, -1, ...). |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 1015 | if (V1Size != MemoryLocation::UnknownSize && |
| 1016 | V2Size != MemoryLocation::UnknownSize) { |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1017 | if (-(uint64_t)GEP1BaseOffset < V1Size) |
| 1018 | return PartialAlias; |
| 1019 | return NoAlias; |
| 1020 | } |
| 1021 | } |
Dan Gohman | c4bf5ca | 2010-12-13 22:50:24 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Eli Friedman | b78ac54 | 2011-09-08 02:37:07 +0000 | [diff] [blame] | 1024 | if (!GEP1VariableIndices.empty()) { |
| 1025 | uint64_t Modulo = 0; |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 1026 | bool AllPositive = true; |
| 1027 | for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i) { |
| 1028 | |
| 1029 | // Try to distinguish something like &A[i][1] against &A[42][0]. |
| 1030 | // Grab the least significant bit set in any of the scales. We |
| 1031 | // don't need std::abs here (even if the scale's negative) as we'll |
| 1032 | // be ^'ing Modulo with itself later. |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1033 | Modulo |= (uint64_t)GEP1VariableIndices[i].Scale; |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 1034 | |
| 1035 | if (AllPositive) { |
| 1036 | // If the Value could change between cycles, then any reasoning about |
| 1037 | // the Value this cycle may not hold in the next cycle. We'll just |
| 1038 | // give up if we can't determine conditions that hold for every cycle: |
| 1039 | const Value *V = GEP1VariableIndices[i].V; |
| 1040 | |
| 1041 | bool SignKnownZero, SignKnownOne; |
| 1042 | ComputeSignBit(const_cast<Value *>(V), SignKnownZero, SignKnownOne, *DL, |
| 1043 | 0, AC1, nullptr, DT); |
| 1044 | |
| 1045 | // Zero-extension widens the variable, and so forces the sign |
| 1046 | // bit to zero. |
| 1047 | bool IsZExt = GEP1VariableIndices[i].ZExtBits > 0 || isa<ZExtInst>(V); |
| 1048 | SignKnownZero |= IsZExt; |
| 1049 | SignKnownOne &= !IsZExt; |
| 1050 | |
| 1051 | // If the variable begins with a zero then we know it's |
| 1052 | // positive, regardless of whether the value is signed or |
| 1053 | // unsigned. |
| 1054 | int64_t Scale = GEP1VariableIndices[i].Scale; |
| 1055 | AllPositive = |
| 1056 | (SignKnownZero && Scale >= 0) || (SignKnownOne && Scale < 0); |
| 1057 | } |
| 1058 | } |
| 1059 | |
Eli Friedman | b78ac54 | 2011-09-08 02:37:07 +0000 | [diff] [blame] | 1060 | Modulo = Modulo ^ (Modulo & (Modulo - 1)); |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1061 | |
Eli Friedman | b78ac54 | 2011-09-08 02:37:07 +0000 | [diff] [blame] | 1062 | // We can compute the difference between the two addresses |
| 1063 | // mod Modulo. Check whether that difference guarantees that the |
| 1064 | // two locations do not alias. |
| 1065 | uint64_t ModOffset = (uint64_t)GEP1BaseOffset & (Modulo - 1); |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 1066 | if (V1Size != MemoryLocation::UnknownSize && |
| 1067 | V2Size != MemoryLocation::UnknownSize && ModOffset >= V2Size && |
| 1068 | V1Size <= Modulo - ModOffset) |
Eli Friedman | b78ac54 | 2011-09-08 02:37:07 +0000 | [diff] [blame] | 1069 | return NoAlias; |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 1070 | |
| 1071 | // If we know all the variables are positive, then GEP1 >= GEP1BasePtr. |
| 1072 | // If GEP1BasePtr > V2 (GEP1BaseOffset > 0) then we know the pointers |
| 1073 | // don't alias if V2Size can fit in the gap between V2 and GEP1BasePtr. |
| 1074 | if (AllPositive && GEP1BaseOffset > 0 && V2Size <= (uint64_t)GEP1BaseOffset) |
| 1075 | return NoAlias; |
| 1076 | |
| 1077 | if (constantOffsetHeuristic(GEP1VariableIndices, V1Size, V2Size, |
| 1078 | GEP1BaseOffset, DL, AC1, DT)) |
| 1079 | return NoAlias; |
Eli Friedman | b78ac54 | 2011-09-08 02:37:07 +0000 | [diff] [blame] | 1080 | } |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1081 | |
Dan Gohman | adf80ae | 2011-06-04 06:50:18 +0000 | [diff] [blame] | 1082 | // Statically, we can see that the base objects are the same, but the |
| 1083 | // pointers have dynamic offsets which we can't resolve. And none of our |
| 1084 | // little tricks above worked. |
| 1085 | // |
| 1086 | // TODO: Returning PartialAlias instead of MayAlias is a mild hack; the |
| 1087 | // practical effect of this is protecting TBAA in the case of dynamic |
Dan Gohman | 9017b84 | 2012-02-17 18:33:38 +0000 | [diff] [blame] | 1088 | // indices into arrays of unions or malloc'd memory. |
Dan Gohman | adf80ae | 2011-06-04 06:50:18 +0000 | [diff] [blame] | 1089 | return PartialAlias; |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1092 | static AliasResult MergeAliasResults(AliasResult A, AliasResult B) { |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1093 | // If the results agree, take it. |
| 1094 | if (A == B) |
| 1095 | return A; |
| 1096 | // A mix of PartialAlias and MustAlias is PartialAlias. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1097 | if ((A == PartialAlias && B == MustAlias) || |
| 1098 | (B == PartialAlias && A == MustAlias)) |
| 1099 | return PartialAlias; |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1100 | // Otherwise, we don't know anything. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1101 | return MayAlias; |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 1104 | /// Provides a bunch of ad-hoc rules to disambiguate a Select instruction |
| 1105 | /// against another. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1106 | AliasResult BasicAliasAnalysis::aliasSelect(const SelectInst *SI, |
| 1107 | uint64_t SISize, |
| 1108 | const AAMDNodes &SIAAInfo, |
| 1109 | const Value *V2, uint64_t V2Size, |
| 1110 | const AAMDNodes &V2AAInfo) { |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1111 | // If the values are Selects with the same condition, we can do a more precise |
| 1112 | // check: just check for aliases between the values on corresponding arms. |
| 1113 | if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) |
| 1114 | if (SI->getCondition() == SI2->getCondition()) { |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1115 | AliasResult Alias = aliasCheck(SI->getTrueValue(), SISize, SIAAInfo, |
| 1116 | SI2->getTrueValue(), V2Size, V2AAInfo); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1117 | if (Alias == MayAlias) |
| 1118 | return MayAlias; |
| 1119 | AliasResult ThisAlias = |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1120 | aliasCheck(SI->getFalseValue(), SISize, SIAAInfo, |
| 1121 | SI2->getFalseValue(), V2Size, V2AAInfo); |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1122 | return MergeAliasResults(ThisAlias, Alias); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | // If both arms of the Select node NoAlias or MustAlias V2, then returns |
| 1126 | // NoAlias / MustAlias. Otherwise, returns MayAlias. |
| 1127 | AliasResult Alias = |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1128 | aliasCheck(V2, V2Size, V2AAInfo, SI->getTrueValue(), SISize, SIAAInfo); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1129 | if (Alias == MayAlias) |
| 1130 | return MayAlias; |
Dan Gohman | 7c34ece | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 1131 | |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1132 | AliasResult ThisAlias = |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1133 | aliasCheck(V2, V2Size, V2AAInfo, SI->getFalseValue(), SISize, SIAAInfo); |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1134 | return MergeAliasResults(ThisAlias, Alias); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 1137 | /// Provide a bunch of ad-hoc rules to disambiguate a PHI instruction against |
| 1138 | /// another. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1139 | AliasResult BasicAliasAnalysis::aliasPHI(const PHINode *PN, uint64_t PNSize, |
| 1140 | const AAMDNodes &PNAAInfo, |
| 1141 | const Value *V2, uint64_t V2Size, |
| 1142 | const AAMDNodes &V2AAInfo) { |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1143 | // Track phi nodes we have visited. We use this information when we determine |
| 1144 | // value equivalence. |
| 1145 | VisitedPhiBBs.insert(PN->getParent()); |
| 1146 | |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1147 | // If the values are PHIs in the same block, we can do a more precise |
| 1148 | // as well as efficient check: just check for aliases between the values |
| 1149 | // on corresponding edges. |
| 1150 | if (const PHINode *PN2 = dyn_cast<PHINode>(V2)) |
| 1151 | if (PN2->getParent() == PN->getParent()) { |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 1152 | LocPair Locs(MemoryLocation(PN, PNSize, PNAAInfo), |
| 1153 | MemoryLocation(V2, V2Size, V2AAInfo)); |
Arnold Schwaighofer | 8dc34cf | 2012-09-06 14:41:53 +0000 | [diff] [blame] | 1154 | if (PN > V2) |
| 1155 | std::swap(Locs.first, Locs.second); |
Arnold Schwaighofer | edd62b1 | 2012-12-10 23:02:41 +0000 | [diff] [blame] | 1156 | // Analyse the PHIs' inputs under the assumption that the PHIs are |
| 1157 | // NoAlias. |
| 1158 | // If the PHIs are May/MustAlias there must be (recursively) an input |
| 1159 | // operand from outside the PHIs' cycle that is MayAlias/MustAlias or |
| 1160 | // there must be an operation on the PHIs within the PHIs' value cycle |
| 1161 | // that causes a MayAlias. |
| 1162 | // Pretend the phis do not alias. |
| 1163 | AliasResult Alias = NoAlias; |
| 1164 | assert(AliasCache.count(Locs) && |
| 1165 | "There must exist an entry for the phi node"); |
| 1166 | AliasResult OrigAliasResult = AliasCache[Locs]; |
| 1167 | AliasCache[Locs] = NoAlias; |
Arnold Schwaighofer | 8dc34cf | 2012-09-06 14:41:53 +0000 | [diff] [blame] | 1168 | |
Hal Finkel | a6f86fc | 2012-11-17 02:33:15 +0000 | [diff] [blame] | 1169 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1170 | AliasResult ThisAlias = |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1171 | aliasCheck(PN->getIncomingValue(i), PNSize, PNAAInfo, |
| 1172 | PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)), |
| 1173 | V2Size, V2AAInfo); |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1174 | Alias = MergeAliasResults(ThisAlias, Alias); |
| 1175 | if (Alias == MayAlias) |
| 1176 | break; |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1177 | } |
Arnold Schwaighofer | 8dc34cf | 2012-09-06 14:41:53 +0000 | [diff] [blame] | 1178 | |
| 1179 | // Reset if speculation failed. |
Arnold Schwaighofer | edd62b1 | 2012-12-10 23:02:41 +0000 | [diff] [blame] | 1180 | if (Alias != NoAlias) |
Arnold Schwaighofer | 8dc34cf | 2012-09-06 14:41:53 +0000 | [diff] [blame] | 1181 | AliasCache[Locs] = OrigAliasResult; |
| 1182 | |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1183 | return Alias; |
| 1184 | } |
| 1185 | |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1186 | SmallPtrSet<Value *, 4> UniqueSrc; |
| 1187 | SmallVector<Value *, 4> V1Srcs; |
Tobias Edler von Koch | d8ce16b | 2015-07-15 19:32:22 +0000 | [diff] [blame] | 1188 | bool isRecursive = false; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1189 | for (Value *PV1 : PN->incoming_values()) { |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1190 | if (isa<PHINode>(PV1)) |
| 1191 | // If any of the source itself is a PHI, return MayAlias conservatively |
Evan Cheng | c1eed9d | 2009-10-14 06:41:49 +0000 | [diff] [blame] | 1192 | // to avoid compile time explosion. The worst possible case is if both |
| 1193 | // sides are PHI nodes. In which case, this is O(m x n) time where 'm' |
| 1194 | // and 'n' are the number of PHI sources. |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1195 | return MayAlias; |
Tobias Edler von Koch | d8ce16b | 2015-07-15 19:32:22 +0000 | [diff] [blame] | 1196 | |
| 1197 | if (EnableRecPhiAnalysis) |
| 1198 | if (GEPOperator *PV1GEP = dyn_cast<GEPOperator>(PV1)) { |
| 1199 | // Check whether the incoming value is a GEP that advances the pointer |
| 1200 | // result of this PHI node (e.g. in a loop). If this is the case, we |
| 1201 | // would recurse and always get a MayAlias. Handle this case specially |
| 1202 | // below. |
| 1203 | if (PV1GEP->getPointerOperand() == PN && PV1GEP->getNumIndices() == 1 && |
| 1204 | isa<ConstantInt>(PV1GEP->idx_begin())) { |
| 1205 | isRecursive = true; |
| 1206 | continue; |
| 1207 | } |
| 1208 | } |
| 1209 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1210 | if (UniqueSrc.insert(PV1).second) |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1211 | V1Srcs.push_back(PV1); |
| 1212 | } |
| 1213 | |
Tobias Edler von Koch | d8ce16b | 2015-07-15 19:32:22 +0000 | [diff] [blame] | 1214 | // If this PHI node is recursive, set the size of the accessed memory to |
| 1215 | // unknown to represent all the possible values the GEP could advance the |
| 1216 | // pointer to. |
| 1217 | if (isRecursive) |
| 1218 | PNSize = MemoryLocation::UnknownSize; |
| 1219 | |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1220 | AliasResult Alias = |
| 1221 | aliasCheck(V2, V2Size, V2AAInfo, V1Srcs[0], PNSize, PNAAInfo); |
Tobias Edler von Koch | d8ce16b | 2015-07-15 19:32:22 +0000 | [diff] [blame] | 1222 | |
Evan Cheng | f92f555 | 2009-10-14 05:22:03 +0000 | [diff] [blame] | 1223 | // Early exit if the check of the first PHI source against V2 is MayAlias. |
| 1224 | // Other results are not possible. |
| 1225 | if (Alias == MayAlias) |
| 1226 | return MayAlias; |
| 1227 | |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1228 | // If all sources of the PHI node NoAlias or MustAlias V2, then returns |
| 1229 | // NoAlias / MustAlias. Otherwise, returns MayAlias. |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1230 | for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) { |
| 1231 | Value *V = V1Srcs[i]; |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1232 | |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1233 | AliasResult ThisAlias = |
| 1234 | aliasCheck(V2, V2Size, V2AAInfo, V, PNSize, PNAAInfo); |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1235 | Alias = MergeAliasResults(ThisAlias, Alias); |
| 1236 | if (Alias == MayAlias) |
| 1237 | break; |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | return Alias; |
| 1241 | } |
| 1242 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 1243 | /// Provideis a bunch of ad-hoc rules to disambiguate in common cases, such as |
| 1244 | /// array references. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1245 | AliasResult BasicAliasAnalysis::aliasCheck(const Value *V1, uint64_t V1Size, |
| 1246 | AAMDNodes V1AAInfo, const Value *V2, |
| 1247 | uint64_t V2Size, |
| 1248 | AAMDNodes V2AAInfo) { |
Dan Gohman | cb45bd9 | 2010-04-08 18:11:50 +0000 | [diff] [blame] | 1249 | // If either of the memory references is empty, it doesn't matter what the |
| 1250 | // pointer values are. |
| 1251 | if (V1Size == 0 || V2Size == 0) |
| 1252 | return NoAlias; |
| 1253 | |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1254 | // Strip off any casts if they exist. |
| 1255 | V1 = V1->stripPointerCasts(); |
| 1256 | V2 = V2->stripPointerCasts(); |
| 1257 | |
Daniel Berlin | 3459d6e | 2015-05-05 18:10:49 +0000 | [diff] [blame] | 1258 | // If V1 or V2 is undef, the result is NoAlias because we can always pick a |
| 1259 | // value for undef that aliases nothing in the program. |
| 1260 | if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) |
| 1261 | return NoAlias; |
| 1262 | |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1263 | // Are we checking for alias of the same value? |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1264 | // Because we look 'through' phi nodes we could look at "Value" pointers from |
| 1265 | // different iterations. We must therefore make sure that this is not the |
| 1266 | // case. The function isValueEqualInPotentialCycles ensures that this cannot |
| 1267 | // happen by looking at the visited phi nodes and making sure they cannot |
| 1268 | // reach the value. |
| 1269 | if (isValueEqualInPotentialCycles(V1, V2)) |
| 1270 | return MustAlias; |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1271 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1272 | if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy()) |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1273 | return NoAlias; // Scalars cannot alias each other |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1274 | |
| 1275 | // Figure out what objects these things are pointing to if we can. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1276 | const Value *O1 = GetUnderlyingObject(V1, *DL, MaxLookupSearchDepth); |
| 1277 | const Value *O2 = GetUnderlyingObject(V2, *DL, MaxLookupSearchDepth); |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1278 | |
Dan Gohman | ccb4584 | 2009-11-09 19:29:11 +0000 | [diff] [blame] | 1279 | // Null values in the default address space don't point to any object, so they |
| 1280 | // don't alias any other pointer. |
| 1281 | if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1)) |
| 1282 | if (CPN->getType()->getAddressSpace() == 0) |
| 1283 | return NoAlias; |
| 1284 | if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2)) |
| 1285 | if (CPN->getType()->getAddressSpace() == 0) |
| 1286 | return NoAlias; |
| 1287 | |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1288 | if (O1 != O2) { |
| 1289 | // If V1/V2 point to two different objects we know that we have no alias. |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 1290 | if (isIdentifiedObject(O1) && isIdentifiedObject(O2)) |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1291 | return NoAlias; |
Nick Lewycky | c53e2ec | 2009-11-14 06:15:14 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Constant pointers can't alias with non-const isIdentifiedObject objects. |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 1294 | if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) || |
| 1295 | (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1))) |
Nick Lewycky | c53e2ec | 2009-11-14 06:15:14 +0000 | [diff] [blame] | 1296 | return NoAlias; |
| 1297 | |
Michael Kuperstein | f3e663a | 2013-05-28 08:17:48 +0000 | [diff] [blame] | 1298 | // Function arguments can't alias with things that are known to be |
| 1299 | // unambigously identified at the function level. |
| 1300 | if ((isa<Argument>(O1) && isIdentifiedFunctionLocal(O2)) || |
| 1301 | (isa<Argument>(O2) && isIdentifiedFunctionLocal(O1))) |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 1302 | return NoAlias; |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1303 | |
| 1304 | // Most objects can't alias null. |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 1305 | if ((isa<ConstantPointerNull>(O2) && isKnownNonNull(O1)) || |
| 1306 | (isa<ConstantPointerNull>(O1) && isKnownNonNull(O2))) |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1307 | return NoAlias; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 1308 | |
Dan Gohman | 5b0a8a8 | 2010-07-07 14:30:04 +0000 | [diff] [blame] | 1309 | // If one pointer is the result of a call/invoke or load and the other is a |
| 1310 | // non-escaping local object within the same function, then we know the |
| 1311 | // object couldn't escape to a point where the call could return it. |
| 1312 | // |
| 1313 | // Note that if the pointers are in different functions, there are a |
| 1314 | // variety of complications. A call with a nocapture argument may still |
| 1315 | // temporary store the nocapture argument's value in a temporary memory |
| 1316 | // location if that memory location doesn't escape. Or it may pass a |
| 1317 | // nocapture value to other functions as long as they don't capture it. |
| 1318 | if (isEscapeSource(O1) && isNonEscapingLocalObject(O2)) |
| 1319 | return NoAlias; |
| 1320 | if (isEscapeSource(O2) && isNonEscapingLocalObject(O1)) |
| 1321 | return NoAlias; |
| 1322 | } |
| 1323 | |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1324 | // If the size of one access is larger than the entire object on the other |
| 1325 | // side, then we know such behavior is undefined and can assume no alias. |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1326 | if (DL) |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 1327 | if ((V1Size != MemoryLocation::UnknownSize && |
| 1328 | isObjectSmallerThan(O2, V1Size, *DL, *TLI)) || |
| 1329 | (V2Size != MemoryLocation::UnknownSize && |
| 1330 | isObjectSmallerThan(O1, V2Size, *DL, *TLI))) |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1331 | return NoAlias; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 1332 | |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1333 | // Check the cache before climbing up use-def chains. This also terminates |
| 1334 | // otherwise infinitely recursive queries. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 1335 | LocPair Locs(MemoryLocation(V1, V1Size, V1AAInfo), |
| 1336 | MemoryLocation(V2, V2Size, V2AAInfo)); |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1337 | if (V1 > V2) |
| 1338 | std::swap(Locs.first, Locs.second); |
| 1339 | std::pair<AliasCacheTy::iterator, bool> Pair = |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1340 | AliasCache.insert(std::make_pair(Locs, MayAlias)); |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1341 | if (!Pair.second) |
| 1342 | return Pair.first->second; |
| 1343 | |
Chris Lattner | 8928899 | 2009-11-26 02:13:03 +0000 | [diff] [blame] | 1344 | // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the |
| 1345 | // GEP can't simplify, we don't even look at the PHI cases. |
Chris Lattner | b2647b9 | 2009-10-17 23:48:54 +0000 | [diff] [blame] | 1346 | if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) { |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 1347 | std::swap(V1, V2); |
| 1348 | std::swap(V1Size, V2Size); |
Chris Lattner | 5341c96 | 2009-11-26 02:14:59 +0000 | [diff] [blame] | 1349 | std::swap(O1, O2); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1350 | std::swap(V1AAInfo, V2AAInfo); |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 1351 | } |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1352 | if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) { |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1353 | AliasResult Result = |
| 1354 | aliasGEP(GV1, V1Size, V1AAInfo, V2, V2Size, V2AAInfo, O1, O2); |
| 1355 | if (Result != MayAlias) |
| 1356 | return AliasCache[Locs] = Result; |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1357 | } |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1358 | |
| 1359 | if (isa<PHINode>(V2) && !isa<PHINode>(V1)) { |
| 1360 | std::swap(V1, V2); |
| 1361 | std::swap(V1Size, V2Size); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1362 | std::swap(V1AAInfo, V2AAInfo); |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1363 | } |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1364 | if (const PHINode *PN = dyn_cast<PHINode>(V1)) { |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1365 | AliasResult Result = aliasPHI(PN, V1Size, V1AAInfo, V2, V2Size, V2AAInfo); |
| 1366 | if (Result != MayAlias) |
| 1367 | return AliasCache[Locs] = Result; |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1368 | } |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1369 | |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1370 | if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) { |
| 1371 | std::swap(V1, V2); |
| 1372 | std::swap(V1Size, V2Size); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1373 | std::swap(V1AAInfo, V2AAInfo); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1374 | } |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1375 | if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) { |
Chandler Carruth | 903c5f9 | 2015-08-06 07:57:58 +0000 | [diff] [blame] | 1376 | AliasResult Result = |
| 1377 | aliasSelect(S1, V1Size, V1AAInfo, V2, V2Size, V2AAInfo); |
| 1378 | if (Result != MayAlias) |
| 1379 | return AliasCache[Locs] = Result; |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1380 | } |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1381 | |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 1382 | // If both pointers are pointing into the same object and one of them |
| 1383 | // accesses is accessing the entire object, then the accesses must |
| 1384 | // overlap in some way. |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1385 | if (DL && O1 == O2) |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 1386 | if ((V1Size != MemoryLocation::UnknownSize && |
| 1387 | isObjectSize(O1, V1Size, *DL, *TLI)) || |
| 1388 | (V2Size != MemoryLocation::UnknownSize && |
| 1389 | isObjectSize(O2, V2Size, *DL, *TLI))) |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1390 | return AliasCache[Locs] = PartialAlias; |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 1391 | |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1392 | AliasResult Result = |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 1393 | AliasAnalysis::alias(MemoryLocation(V1, V1Size, V1AAInfo), |
| 1394 | MemoryLocation(V2, V2Size, V2AAInfo)); |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1395 | return AliasCache[Locs] = Result; |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 1396 | } |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1397 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 1398 | /// Check whether two Values can be considered equivalent. |
| 1399 | /// |
| 1400 | /// In addition to pointer equivalence of \p V1 and \p V2 this checks whether |
| 1401 | /// they can not be part of a cycle in the value graph by looking at all |
| 1402 | /// visited phi nodes an making sure that the phis cannot reach the value. We |
| 1403 | /// have to do this because we are looking through phi nodes (That is we say |
| 1404 | /// noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB). |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1405 | bool BasicAliasAnalysis::isValueEqualInPotentialCycles(const Value *V, |
| 1406 | const Value *V2) { |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1407 | if (V != V2) |
| 1408 | return false; |
| 1409 | |
| 1410 | const Instruction *Inst = dyn_cast<Instruction>(V); |
| 1411 | if (!Inst) |
| 1412 | return true; |
| 1413 | |
Daniel Berlin | 9e77de2 | 2015-03-20 18:05:49 +0000 | [diff] [blame] | 1414 | if (VisitedPhiBBs.empty()) |
| 1415 | return true; |
| 1416 | |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1417 | if (VisitedPhiBBs.size() > MaxNumPhiBBsValueReachabilityCheck) |
| 1418 | return false; |
| 1419 | |
| 1420 | // Use dominance or loop info if available. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 1421 | DominatorTreeWrapperPass *DTWP = |
| 1422 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1423 | DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr; |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 1424 | auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>(); |
| 1425 | LoopInfo *LI = LIWP ? &LIWP->getLoopInfo() : nullptr; |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1426 | |
| 1427 | // Make sure that the visited phis cannot reach the Value. This ensures that |
| 1428 | // the Values cannot come from different iterations of a potential cycle the |
| 1429 | // phi nodes could be involved in. |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1430 | for (auto *P : VisitedPhiBBs) |
| 1431 | if (isPotentiallyReachable(P->begin(), Inst, DT, LI)) |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1432 | return false; |
| 1433 | |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1434 | return true; |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Chandler Carruth | c5d8112 | 2015-08-06 08:17:06 +0000 | [diff] [blame] | 1437 | /// Computes the symbolic difference between two de-composed GEPs. |
| 1438 | /// |
| 1439 | /// Dest and Src are the variable indices from two decomposed GetElementPtr |
| 1440 | /// instructions GEP1 and GEP2 which have common base pointers. |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1441 | void BasicAliasAnalysis::GetIndexDifference( |
| 1442 | SmallVectorImpl<VariableGEPIndex> &Dest, |
| 1443 | const SmallVectorImpl<VariableGEPIndex> &Src) { |
| 1444 | if (Src.empty()) |
| 1445 | return; |
| 1446 | |
| 1447 | for (unsigned i = 0, e = Src.size(); i != e; ++i) { |
| 1448 | const Value *V = Src[i].V; |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 1449 | unsigned ZExtBits = Src[i].ZExtBits, SExtBits = Src[i].SExtBits; |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1450 | int64_t Scale = Src[i].Scale; |
| 1451 | |
| 1452 | // Find V in Dest. This is N^2, but pointer indices almost never have more |
| 1453 | // than a few variable indexes. |
| 1454 | for (unsigned j = 0, e = Dest.size(); j != e; ++j) { |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1455 | if (!isValueEqualInPotentialCycles(Dest[j].V, V) || |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 1456 | Dest[j].ZExtBits != ZExtBits || Dest[j].SExtBits != SExtBits) |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1457 | continue; |
| 1458 | |
| 1459 | // If we found it, subtract off Scale V's from the entry in Dest. If it |
| 1460 | // goes to zero, remove the entry. |
| 1461 | if (Dest[j].Scale != Scale) |
| 1462 | Dest[j].Scale -= Scale; |
| 1463 | else |
| 1464 | Dest.erase(Dest.begin() + j); |
| 1465 | Scale = 0; |
| 1466 | break; |
| 1467 | } |
| 1468 | |
| 1469 | // If we didn't consume this entry, add it to the end of the Dest list. |
| 1470 | if (Scale) { |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 1471 | VariableGEPIndex Entry = {V, ZExtBits, SExtBits, -Scale}; |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1472 | Dest.push_back(Entry); |
| 1473 | } |
| 1474 | } |
| 1475 | } |
Quentin Colombet | 5989bc6 | 2015-08-31 22:32:47 +0000 | [diff] [blame^] | 1476 | |
| 1477 | bool BasicAliasAnalysis::constantOffsetHeuristic( |
| 1478 | const SmallVectorImpl<VariableGEPIndex> &VarIndices, uint64_t V1Size, |
| 1479 | uint64_t V2Size, int64_t BaseOffset, const DataLayout *DL, |
| 1480 | AssumptionCache *AC, DominatorTree *DT) { |
| 1481 | if (VarIndices.size() != 2 || V1Size == MemoryLocation::UnknownSize || |
| 1482 | V2Size == MemoryLocation::UnknownSize || !DL) |
| 1483 | return false; |
| 1484 | |
| 1485 | const VariableGEPIndex &Var0 = VarIndices[0], &Var1 = VarIndices[1]; |
| 1486 | |
| 1487 | if (Var0.ZExtBits != Var1.ZExtBits || Var0.SExtBits != Var1.SExtBits || |
| 1488 | Var0.Scale != -Var1.Scale) |
| 1489 | return false; |
| 1490 | |
| 1491 | unsigned Width = Var1.V->getType()->getIntegerBitWidth(); |
| 1492 | |
| 1493 | // We'll strip off the Extensions of Var0 and Var1 and do another round |
| 1494 | // of GetLinearExpression decomposition. In the example above, if Var0 |
| 1495 | // is zext(%x + 1) we should get V1 == %x and V1Offset == 1. |
| 1496 | |
| 1497 | APInt V0Scale(Width, 0), V0Offset(Width, 0), V1Scale(Width, 0), |
| 1498 | V1Offset(Width, 0); |
| 1499 | bool NSW = true, NUW = true; |
| 1500 | unsigned V0ZExtBits = 0, V0SExtBits = 0, V1ZExtBits = 0, V1SExtBits = 0; |
| 1501 | const Value *V0 = GetLinearExpression(Var0.V, V0Scale, V0Offset, V0ZExtBits, |
| 1502 | V0SExtBits, *DL, 0, AC, DT, NSW, NUW); |
| 1503 | NSW = true, NUW = true; |
| 1504 | const Value *V1 = GetLinearExpression(Var1.V, V1Scale, V1Offset, V1ZExtBits, |
| 1505 | V1SExtBits, *DL, 0, AC, DT, NSW, NUW); |
| 1506 | |
| 1507 | if (V0Scale != V1Scale || V0ZExtBits != V1ZExtBits || |
| 1508 | V0SExtBits != V1SExtBits || !isValueEqualInPotentialCycles(V0, V1)) |
| 1509 | return false; |
| 1510 | |
| 1511 | // We have a hit - Var0 and Var1 only differ by a constant offset! |
| 1512 | |
| 1513 | // If we've been sext'ed then zext'd the maximum difference between Var0 and |
| 1514 | // Var1 is possible to calculate, but we're just interested in the absolute |
| 1515 | // minumum difference between the two. The minimum distance may occur due to |
| 1516 | // wrapping; consider "add i3 %i, 5": if %i == 7 then 7 + 5 mod 8 == 4, and so |
| 1517 | // the minimum distance between %i and %i + 5 is 3. |
| 1518 | APInt MinDiff = V0Offset - V1Offset, |
| 1519 | Wrapped = APInt::getMaxValue(Width) - MinDiff + APInt(Width, 1); |
| 1520 | MinDiff = APIntOps::umin(MinDiff, Wrapped); |
| 1521 | uint64_t MinDiffBytes = MinDiff.getZExtValue() * std::abs(Var0.Scale); |
| 1522 | |
| 1523 | // We can't definitely say whether GEP1 is before or after V2 due to wrapping |
| 1524 | // arithmetic (i.e. for some values of GEP1 and V2 GEP1 < V2, and for other |
| 1525 | // values GEP1 > V2). We'll therefore only declare NoAlias if both V1Size and |
| 1526 | // V2Size can fit in the MinDiffBytes gap. |
| 1527 | return V1Size + std::abs(BaseOffset) <= MinDiffBytes && |
| 1528 | V2Size + std::abs(BaseOffset) <= MinDiffBytes; |
| 1529 | } |