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