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 | |
Jeff Cohen | cede1ce | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallPtrSet.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 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/Function.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 31 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/GlobalAlias.h" |
| 33 | #include "llvm/IR/GlobalVariable.h" |
| 34 | #include "llvm/IR/Instructions.h" |
| 35 | #include "llvm/IR/IntrinsicInst.h" |
| 36 | #include "llvm/IR/LLVMContext.h" |
| 37 | #include "llvm/IR/Operator.h" |
Chris Lattner | d82256a | 2004-03-15 03:36:49 +0000 | [diff] [blame] | 38 | #include "llvm/Pass.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetLibraryInfo.h" |
Alkis Evlogimenos | a5c04ee | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 41 | #include <algorithm> |
Chris Lattner | 3599748 | 2003-11-25 18:33:40 +0000 | [diff] [blame] | 42 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 43 | |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 44 | /// Cutoff after which to stop analysing a set of phi nodes potentially involved |
| 45 | /// 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] | 46 | /// careful with value equivalence. We use reachability to make sure a value |
| 47 | /// cannot be involved in a cycle. |
| 48 | const unsigned MaxNumPhiBBsValueReachabilityCheck = 20; |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 49 | |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 50 | // The max limit of the search depth in DecomposeGEPExpression() and |
| 51 | // GetUnderlyingObject(), both functions need to use the same search |
| 52 | // depth otherwise the algorithm in aliasGEP will assert. |
| 53 | static const unsigned MaxLookupSearchDepth = 6; |
| 54 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
| 56 | // Useful predicates |
| 57 | //===----------------------------------------------------------------------===// |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 58 | |
Chris Lattner | b35d9b5 | 2008-06-16 06:19:11 +0000 | [diff] [blame] | 59 | /// isNonEscapingLocalObject - Return true if the pointer is to a function-local |
| 60 | /// object that never escapes from the function. |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 61 | static bool isNonEscapingLocalObject(const Value *V) { |
Chris Lattner | fa48258 | 2008-06-16 06:28:01 +0000 | [diff] [blame] | 62 | // If this is a local allocation, check to see if it escapes. |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 63 | if (isa<AllocaInst>(V) || isNoAliasCall(V)) |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 64 | // Set StoreCaptures to True so that we can assume in our callers that the |
| 65 | // pointer is not the result of a load instruction. Currently |
| 66 | // PointerMayBeCaptured doesn't have any special analysis for the |
| 67 | // StoreCaptures=false case; if it did, our callers could be refined to be |
| 68 | // more precise. |
| 69 | return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true); |
Duncan Sands | 8d65f36 | 2009-01-05 21:19:53 +0000 | [diff] [blame] | 70 | |
Chris Lattner | fa48258 | 2008-06-16 06:28:01 +0000 | [diff] [blame] | 71 | // 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] | 72 | // then it has not escaped before entering the function. Check if it escapes |
| 73 | // inside the function. |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 74 | if (const Argument *A = dyn_cast<Argument>(V)) |
Richard Osborne | a1fffcf | 2012-11-05 10:48:24 +0000 | [diff] [blame] | 75 | if (A->hasByValAttr() || A->hasNoAliasAttr()) |
| 76 | // Note even if the argument is marked nocapture we still need to check |
| 77 | // for copies made inside the function. The nocapture attribute only |
| 78 | // specifies that there are no copies made that outlive the function. |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 79 | return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true); |
Richard Osborne | a1fffcf | 2012-11-05 10:48:24 +0000 | [diff] [blame] | 80 | |
Chris Lattner | b35d9b5 | 2008-06-16 06:19:11 +0000 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 84 | /// isEscapeSource - Return true if the pointer is one which would have |
| 85 | /// been considered an escape by isNonEscapingLocalObject. |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 86 | static bool isEscapeSource(const Value *V) { |
| 87 | if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V)) |
| 88 | return true; |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 89 | |
| 90 | // The load case works because isNonEscapingLocalObject considers all |
| 91 | // stores to be escapes (it passes true for the StoreCaptures argument |
| 92 | // to PointerMayBeCaptured). |
| 93 | if (isa<LoadInst>(V)) |
| 94 | return true; |
| 95 | |
| 96 | return false; |
| 97 | } |
Chris Lattner | b35d9b5 | 2008-06-16 06:19:11 +0000 | [diff] [blame] | 98 | |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 99 | /// getObjectSize - Return the size of the object specified by V, or |
| 100 | /// UnknownSize if unknown. |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 101 | static uint64_t getObjectSize(const Value *V, const DataLayout &DL, |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 102 | const TargetLibraryInfo &TLI, |
Eli Friedman | 8bc169c | 2012-02-27 20:46:07 +0000 | [diff] [blame] | 103 | bool RoundToAlign = false) { |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 104 | uint64_t Size; |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 105 | if (getObjectSize(V, Size, &DL, &TLI, RoundToAlign)) |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 106 | return Size; |
| 107 | return AliasAnalysis::UnknownSize; |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /// isObjectSmallerThan - Return true if we can prove that the object specified |
| 111 | /// by V is smaller than Size. |
| 112 | static bool isObjectSmallerThan(const Value *V, uint64_t Size, |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 113 | const DataLayout &DL, |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 114 | const TargetLibraryInfo &TLI) { |
Nadav Rotem | abcc64f | 2013-04-09 18:16:05 +0000 | [diff] [blame] | 115 | // Note that the meanings of the "object" are slightly different in the |
| 116 | // following contexts: |
| 117 | // c1: llvm::getObjectSize() |
| 118 | // c2: llvm.objectsize() intrinsic |
| 119 | // c3: isObjectSmallerThan() |
| 120 | // c1 and c2 share the same meaning; however, the meaning of "object" in c3 |
| 121 | // refers to the "entire object". |
| 122 | // |
| 123 | // Consider this example: |
| 124 | // char *p = (char*)malloc(100) |
| 125 | // char *q = p+80; |
| 126 | // |
| 127 | // In the context of c1 and c2, the "object" pointed by q refers to the |
| 128 | // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20. |
| 129 | // |
| 130 | // However, in the context of c3, the "object" refers to the chunk of memory |
| 131 | // being allocated. So, the "object" has 100 bytes, and q points to the middle |
| 132 | // the "object". In case q is passed to isObjectSmallerThan() as the 1st |
| 133 | // parameter, before the llvm::getObjectSize() is called to get the size of |
| 134 | // entire object, we should: |
| 135 | // - either rewind the pointer q to the base-address of the object in |
| 136 | // question (in this case rewind to p), or |
| 137 | // - just give up. It is up to caller to make sure the pointer is pointing |
| 138 | // to the base address the object. |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 139 | // |
Nadav Rotem | abcc64f | 2013-04-09 18:16:05 +0000 | [diff] [blame] | 140 | // We go for 2nd option for simplicity. |
| 141 | if (!isIdentifiedObject(V)) |
| 142 | return false; |
| 143 | |
Eli Friedman | 8bc169c | 2012-02-27 20:46:07 +0000 | [diff] [blame] | 144 | // This function needs to use the aligned object size because we allow |
| 145 | // reads a bit past the end given sufficient alignment. |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 146 | uint64_t ObjectSize = getObjectSize(V, DL, TLI, /*RoundToAlign*/true); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 147 | |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 148 | return ObjectSize != AliasAnalysis::UnknownSize && ObjectSize < Size; |
| 149 | } |
| 150 | |
| 151 | /// isObjectSize - Return true if we can prove that the object specified |
| 152 | /// by V has size Size. |
| 153 | static bool isObjectSize(const Value *V, uint64_t Size, |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 154 | const DataLayout &DL, const TargetLibraryInfo &TLI) { |
| 155 | uint64_t ObjectSize = getObjectSize(V, DL, TLI); |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 156 | return ObjectSize != AliasAnalysis::UnknownSize && ObjectSize == Size; |
Chris Lattner | 98ad816 | 2008-06-16 06:10:11 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Michael Kuperstein | f3e663a | 2013-05-28 08:17:48 +0000 | [diff] [blame] | 159 | /// isIdentifiedFunctionLocal - Return true if V is umabigously identified |
| 160 | /// at the function-level. Different IdentifiedFunctionLocals can't alias. |
| 161 | /// Further, an IdentifiedFunctionLocal can not alias with any function |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 162 | /// arguments other than itself, which is not necessarily true for |
Michael Kuperstein | f3e663a | 2013-05-28 08:17:48 +0000 | [diff] [blame] | 163 | /// IdentifiedObjects. |
| 164 | static bool isIdentifiedFunctionLocal(const Value *V) |
| 165 | { |
| 166 | return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V); |
| 167 | } |
| 168 | |
| 169 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 170 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 171 | // GetElementPtr Instruction Decomposition and Analysis |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 174 | namespace { |
| 175 | enum ExtensionKind { |
| 176 | EK_NotExtended, |
| 177 | EK_SignExt, |
| 178 | EK_ZeroExt |
| 179 | }; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 181 | struct VariableGEPIndex { |
| 182 | const Value *V; |
| 183 | ExtensionKind Extension; |
| 184 | int64_t Scale; |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 185 | |
| 186 | bool operator==(const VariableGEPIndex &Other) const { |
| 187 | return V == Other.V && Extension == Other.Extension && |
| 188 | Scale == Other.Scale; |
| 189 | } |
| 190 | |
| 191 | bool operator!=(const VariableGEPIndex &Other) const { |
| 192 | return !operator==(Other); |
| 193 | } |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 194 | }; |
| 195 | } |
| 196 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 197 | |
| 198 | /// GetLinearExpression - Analyze the specified value as a linear expression: |
| 199 | /// "A*V + B", where A and B are constant integers. Return the scale and offset |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 200 | /// values as APInts and return V as a Value*, and return whether we looked |
| 201 | /// through any sign or zero extends. The incoming Value is known to have |
| 202 | /// IntegerType and it may already be sign or zero extended. |
| 203 | /// |
| 204 | /// Note that this looks through extends, so the high bits may not be |
| 205 | /// represented in the result. |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 206 | static Value *GetLinearExpression(Value *V, APInt &Scale, APInt &Offset, |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 207 | ExtensionKind &Extension, |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 208 | const DataLayout &DL, unsigned Depth) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 209 | assert(V->getType()->isIntegerTy() && "Not an integer value"); |
| 210 | |
| 211 | // Limit our recursion depth. |
| 212 | if (Depth == 6) { |
| 213 | Scale = 1; |
| 214 | Offset = 0; |
| 215 | return V; |
| 216 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 218 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(V)) { |
| 219 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) { |
| 220 | switch (BOp->getOpcode()) { |
| 221 | default: break; |
| 222 | case Instruction::Or: |
| 223 | // X|C == X+C if all the bits in C are unset in X. Otherwise we can't |
| 224 | // analyze it. |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 225 | if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), &DL)) |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 226 | break; |
| 227 | // FALL THROUGH. |
| 228 | case Instruction::Add: |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 229 | V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, Extension, |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 230 | DL, Depth+1); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 231 | Offset += RHSC->getValue(); |
| 232 | return V; |
| 233 | case Instruction::Mul: |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 234 | V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, Extension, |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 235 | DL, Depth+1); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 236 | Offset *= RHSC->getValue(); |
| 237 | Scale *= RHSC->getValue(); |
| 238 | return V; |
| 239 | case Instruction::Shl: |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 240 | V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, Extension, |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 241 | DL, Depth+1); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 242 | Offset <<= RHSC->getValue().getLimitedValue(); |
| 243 | Scale <<= RHSC->getValue().getLimitedValue(); |
| 244 | return V; |
| 245 | } |
| 246 | } |
| 247 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 249 | // 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] | 250 | // bits of a sign or zero extended value - just scales and offsets. The |
| 251 | // extensions have to be consistent though. |
| 252 | if ((isa<SExtInst>(V) && Extension != EK_ZeroExt) || |
| 253 | (isa<ZExtInst>(V) && Extension != EK_SignExt)) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 254 | Value *CastOp = cast<CastInst>(V)->getOperand(0); |
| 255 | unsigned OldWidth = Scale.getBitWidth(); |
| 256 | unsigned SmallWidth = CastOp->getType()->getPrimitiveSizeInBits(); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 257 | Scale = Scale.trunc(SmallWidth); |
| 258 | Offset = Offset.trunc(SmallWidth); |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 259 | Extension = isa<SExtInst>(V) ? EK_SignExt : EK_ZeroExt; |
| 260 | |
| 261 | Value *Result = GetLinearExpression(CastOp, Scale, Offset, Extension, |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 262 | DL, Depth+1); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 263 | Scale = Scale.zext(OldWidth); |
| 264 | Offset = Offset.zext(OldWidth); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 266 | return Result; |
| 267 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 269 | Scale = 1; |
| 270 | Offset = 0; |
| 271 | return V; |
| 272 | } |
| 273 | |
| 274 | /// DecomposeGEPExpression - If V is a symbolic pointer expression, decompose it |
| 275 | /// into a base pointer with a constant offset and a number of scaled symbolic |
| 276 | /// offsets. |
| 277 | /// |
| 278 | /// The scaled symbolic offsets (represented by pairs of a Value* and a scale in |
| 279 | /// the VarIndices vector) are Value*'s that are known to be scaled by the |
| 280 | /// specified amount, but which may have other unrepresented high bits. As such, |
| 281 | /// the gep cannot necessarily be reconstructed from its decomposed form. |
| 282 | /// |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 283 | /// When DataLayout is around, this function is capable of analyzing everything |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 284 | /// that GetUnderlyingObject can look through. To be able to do that |
| 285 | /// GetUnderlyingObject and DecomposeGEPExpression must use the same search |
| 286 | /// depth (MaxLookupSearchDepth). |
| 287 | /// When DataLayout not is around, it just looks through pointer casts. |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 288 | /// |
| 289 | static const Value * |
| 290 | DecomposeGEPExpression(const Value *V, int64_t &BaseOffs, |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 291 | SmallVectorImpl<VariableGEPIndex> &VarIndices, |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 292 | bool &MaxLookupReached, const DataLayout *DL) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 293 | // Limit recursion depth to limit compile time in crazy cases. |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 294 | unsigned MaxLookup = MaxLookupSearchDepth; |
| 295 | MaxLookupReached = false; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 297 | BaseOffs = 0; |
| 298 | do { |
| 299 | // See if this is a bitcast or GEP. |
| 300 | const Operator *Op = dyn_cast<Operator>(V); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 301 | if (!Op) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 302 | // The only non-operator case we can handle are GlobalAliases. |
| 303 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 304 | if (!GA->mayBeOverridden()) { |
| 305 | V = GA->getAliasee(); |
| 306 | continue; |
| 307 | } |
| 308 | } |
| 309 | return V; |
| 310 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 312 | if (Op->getOpcode() == Instruction::BitCast) { |
| 313 | V = Op->getOperand(0); |
| 314 | continue; |
| 315 | } |
Dan Gohman | 05b18f1 | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 317 | const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 318 | if (!GEPOp) { |
Dan Gohman | 0573b55 | 2011-05-24 18:24:08 +0000 | [diff] [blame] | 319 | // If it's not a GEP, hand it off to SimplifyInstruction to see if it |
| 320 | // can come up with something. This matches what GetUnderlyingObject does. |
| 321 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 322 | // TODO: Get a DominatorTree and use it here. |
| 323 | if (const Value *Simplified = |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 324 | SimplifyInstruction(const_cast<Instruction *>(I), DL)) { |
Dan Gohman | 0573b55 | 2011-05-24 18:24:08 +0000 | [diff] [blame] | 325 | V = Simplified; |
| 326 | continue; |
| 327 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 329 | return V; |
Dan Gohman | 0573b55 | 2011-05-24 18:24:08 +0000 | [diff] [blame] | 330 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 332 | // Don't attempt to analyze GEPs over unsized objects. |
Matt Arsenault | fa25272 | 2013-09-27 22:18:51 +0000 | [diff] [blame] | 333 | if (!GEPOp->getOperand(0)->getType()->getPointerElementType()->isSized()) |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 334 | return V; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 335 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 336 | // If we are lacking DataLayout information, we can't compute the offets of |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 337 | // elements computed by GEPs. However, we can handle bitcast equivalent |
| 338 | // GEPs. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 339 | if (!DL) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 340 | if (!GEPOp->hasAllZeroIndices()) |
| 341 | return V; |
| 342 | V = GEPOp->getOperand(0); |
| 343 | continue; |
| 344 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 345 | |
Matt Arsenault | a8fe22b | 2013-11-16 00:36:43 +0000 | [diff] [blame] | 346 | unsigned AS = GEPOp->getPointerAddressSpace(); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 347 | // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices. |
| 348 | gep_type_iterator GTI = gep_type_begin(GEPOp); |
| 349 | for (User::const_op_iterator I = GEPOp->op_begin()+1, |
| 350 | E = GEPOp->op_end(); I != E; ++I) { |
| 351 | Value *Index = *I; |
| 352 | // Compute the (potentially symbolic) offset in bytes for this index. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 353 | if (StructType *STy = dyn_cast<StructType>(*GTI++)) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 354 | // For a struct, add the member offset. |
| 355 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
| 356 | if (FieldNo == 0) continue; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 357 | |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 358 | BaseOffs += DL->getStructLayout(STy)->getElementOffset(FieldNo); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 359 | continue; |
| 360 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 362 | // For an array/pointer, add the element offset, explicitly scaled. |
| 363 | if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) { |
| 364 | if (CIdx->isZero()) continue; |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 365 | BaseOffs += DL->getTypeAllocSize(*GTI)*CIdx->getSExtValue(); |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 366 | continue; |
| 367 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 368 | |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 369 | uint64_t Scale = DL->getTypeAllocSize(*GTI); |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 370 | ExtensionKind Extension = EK_NotExtended; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 372 | // If the integer type is smaller than the pointer size, it is implicitly |
| 373 | // sign extended to pointer size. |
Matt Arsenault | fa25272 | 2013-09-27 22:18:51 +0000 | [diff] [blame] | 374 | unsigned Width = Index->getType()->getIntegerBitWidth(); |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 375 | if (DL->getPointerSizeInBits(AS) > Width) |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 376 | Extension = EK_SignExt; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 378 | // Use GetLinearExpression to decompose the index into a C1*V+C2 form. |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 379 | APInt IndexScale(Width, 0), IndexOffset(Width, 0); |
Chris Lattner | 3decde9 | 2010-08-18 23:09:49 +0000 | [diff] [blame] | 380 | Index = GetLinearExpression(Index, IndexScale, IndexOffset, Extension, |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 381 | *DL, 0); |
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 | // The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale. |
| 384 | // This gives us an aggregate computation of (C1*Scale)*V + C2*Scale. |
Eli Friedman | ab3a128 | 2010-09-15 20:08:03 +0000 | [diff] [blame] | 385 | BaseOffs += IndexOffset.getSExtValue()*Scale; |
| 386 | Scale *= IndexScale.getSExtValue(); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 388 | // If we already had an occurrence of this index variable, merge this |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 389 | // scale into it. For example, we want to handle: |
| 390 | // A[x][x] -> x*16 + x*4 -> x*20 |
| 391 | // This also ensures that 'x' only appears in the index list once. |
| 392 | for (unsigned i = 0, e = VarIndices.size(); i != e; ++i) { |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 393 | if (VarIndices[i].V == Index && |
| 394 | VarIndices[i].Extension == Extension) { |
| 395 | Scale += VarIndices[i].Scale; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 396 | VarIndices.erase(VarIndices.begin()+i); |
| 397 | break; |
| 398 | } |
| 399 | } |
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 | // Make sure that we have a scale that makes sense for this target's |
| 402 | // pointer size. |
Rafael Espindola | 5f57f46 | 2014-02-21 18:34:28 +0000 | [diff] [blame] | 403 | if (unsigned ShiftBits = 64 - DL->getPointerSizeInBits(AS)) { |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 404 | Scale <<= ShiftBits; |
Eli Friedman | ab3a128 | 2010-09-15 20:08:03 +0000 | [diff] [blame] | 405 | Scale = (int64_t)Scale >> ShiftBits; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 406 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 408 | if (Scale) { |
Jeffrey Yasskin | 6381c01 | 2011-07-27 06:22:51 +0000 | [diff] [blame] | 409 | VariableGEPIndex Entry = {Index, Extension, |
| 410 | static_cast<int64_t>(Scale)}; |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 411 | VarIndices.push_back(Entry); |
| 412 | } |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 413 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 415 | // Analyze the base pointer next. |
| 416 | V = GEPOp->getOperand(0); |
| 417 | } while (--MaxLookup); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 419 | // If the chain of expressions is too deep, just return early. |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 420 | MaxLookupReached = true; |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 421 | return V; |
| 422 | } |
| 423 | |
Chris Lattner | 9f7500f | 2010-08-18 22:07:29 +0000 | [diff] [blame] | 424 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 425 | // BasicAliasAnalysis Pass |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 426 | //===----------------------------------------------------------------------===// |
| 427 | |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 428 | #ifndef NDEBUG |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 429 | static const Function *getParent(const Value *V) { |
Dan Gohman | 1be9e7c | 2010-06-29 18:12:34 +0000 | [diff] [blame] | 430 | if (const Instruction *inst = dyn_cast<Instruction>(V)) |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 431 | return inst->getParent()->getParent(); |
| 432 | |
Dan Gohman | 1be9e7c | 2010-06-29 18:12:34 +0000 | [diff] [blame] | 433 | if (const Argument *arg = dyn_cast<Argument>(V)) |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 434 | return arg->getParent(); |
| 435 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 436 | return nullptr; |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 439 | static bool notDifferentParent(const Value *O1, const Value *O2) { |
| 440 | |
| 441 | const Function *F1 = getParent(O1); |
| 442 | const Function *F2 = getParent(O2); |
| 443 | |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 444 | return !F1 || !F2 || F1 == F2; |
| 445 | } |
Benjamin Kramer | 80b7bc0 | 2010-06-29 10:03:11 +0000 | [diff] [blame] | 446 | #endif |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 448 | namespace { |
Dan Gohman | da85ed8 | 2010-10-19 23:09:08 +0000 | [diff] [blame] | 449 | /// BasicAliasAnalysis - This is the primary alias analysis implementation. |
| 450 | struct BasicAliasAnalysis : public ImmutablePass, public AliasAnalysis { |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 451 | static char ID; // Class identification, replacement for typeinfo |
Benjamin Kramer | 6c2649c | 2012-09-05 16:49:37 +0000 | [diff] [blame] | 452 | BasicAliasAnalysis() : ImmutablePass(ID) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 453 | initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry()); |
| 454 | } |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 455 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 456 | void initializePass() override { |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 457 | InitializeAliasAnalysis(this); |
| 458 | } |
| 459 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 460 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 461 | AU.addRequired<AliasAnalysis>(); |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 462 | AU.addRequired<TargetLibraryInfo>(); |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 465 | AliasResult alias(const Location &LocA, const Location &LocB) override { |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 466 | assert(AliasCache.empty() && "AliasCache must be cleared after use!"); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 467 | assert(notDifferentParent(LocA.Ptr, LocB.Ptr) && |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 468 | "BasicAliasAnalysis doesn't support interprocedural queries."); |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 469 | AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocA.TBAATag, |
| 470 | LocB.Ptr, LocB.Size, LocB.TBAATag); |
Benjamin Kramer | 6c2649c | 2012-09-05 16:49:37 +0000 | [diff] [blame] | 471 | // AliasCache rarely has more than 1 or 2 elements, always use |
| 472 | // shrink_and_clear so it quickly returns to the inline capacity of the |
| 473 | // SmallDenseMap if it ever grows larger. |
| 474 | // FIXME: This should really be shrink_to_inline_capacity_and_clear(). |
| 475 | AliasCache.shrink_and_clear(); |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 476 | VisitedPhiBBs.clear(); |
Evan Cheng | b3ccb64 | 2009-10-14 06:46:26 +0000 | [diff] [blame] | 477 | return Alias; |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 478 | } |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 479 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 480 | ModRefResult getModRefInfo(ImmutableCallSite CS, |
| 481 | const Location &Loc) override; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 482 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 483 | ModRefResult getModRefInfo(ImmutableCallSite CS1, |
| 484 | ImmutableCallSite CS2) override { |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 485 | // The AliasAnalysis base class has some smarts, lets use them. |
| 486 | return AliasAnalysis::getModRefInfo(CS1, CS2); |
| 487 | } |
Owen Anderson | 98a3617 | 2009-02-05 23:36:27 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 489 | /// pointsToConstantMemory - Chase pointers until we find a (constant |
| 490 | /// global) or not. |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 491 | bool pointsToConstantMemory(const Location &Loc, bool OrLocal) override; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 492 | |
| 493 | /// getModRefBehavior - Return the behavior when calling the given |
| 494 | /// call site. |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 495 | ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 496 | |
| 497 | /// getModRefBehavior - Return the behavior when calling the given function. |
| 498 | /// For use when the call site is not known. |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 499 | ModRefBehavior getModRefBehavior(const Function *F) override; |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 500 | |
Chris Lattner | af362f0 | 2010-01-20 19:26:14 +0000 | [diff] [blame] | 501 | /// getAdjustedAnalysisPointer - This method is used when a pass implements |
Dan Gohman | e0d5c45 | 2010-08-05 23:48:14 +0000 | [diff] [blame] | 502 | /// an analysis interface through multiple inheritance. If needed, it |
| 503 | /// should override this to adjust the this pointer as needed for the |
| 504 | /// specified pass info. |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 505 | void *getAdjustedAnalysisPointer(const void *ID) override { |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 506 | if (ID == &AliasAnalysis::ID) |
Chris Lattner | af362f0 | 2010-01-20 19:26:14 +0000 | [diff] [blame] | 507 | return (AliasAnalysis*)this; |
| 508 | return this; |
| 509 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 511 | private: |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 512 | // AliasCache - Track alias queries to guard against recursion. |
| 513 | typedef std::pair<Location, Location> LocPair; |
Benjamin Kramer | 6c2649c | 2012-09-05 16:49:37 +0000 | [diff] [blame] | 514 | typedef SmallDenseMap<LocPair, AliasResult, 8> AliasCacheTy; |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 515 | AliasCacheTy AliasCache; |
| 516 | |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 517 | /// \brief Track phi nodes we have visited. When interpret "Value" pointer |
| 518 | /// equality as value equality we need to make sure that the "Value" is not |
| 519 | /// part of a cycle. Otherwise, two uses could come from different |
| 520 | /// "iterations" of a cycle and see different values for the same "Value" |
| 521 | /// pointer. |
| 522 | /// The following example shows the problem: |
| 523 | /// %p = phi(%alloca1, %addr2) |
| 524 | /// %l = load %ptr |
| 525 | /// %addr1 = gep, %alloca2, 0, %l |
| 526 | /// %addr2 = gep %alloca2, 0, (%l + 1) |
| 527 | /// alias(%p, %addr1) -> MayAlias ! |
| 528 | /// store %l, ... |
| 529 | SmallPtrSet<const BasicBlock*, 8> VisitedPhiBBs; |
| 530 | |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 531 | // Visited - Track instructions visited by pointsToConstantMemory. |
Dan Gohman | 7c34ece | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 532 | SmallPtrSet<const Value*, 16> Visited; |
Evan Cheng | 31565b3 | 2009-10-14 05:05:02 +0000 | [diff] [blame] | 533 | |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 534 | /// \brief Check whether two Values can be considered equivalent. |
| 535 | /// |
| 536 | /// In addition to pointer equivalence of \p V1 and \p V2 this checks |
| 537 | /// whether they can not be part of a cycle in the value graph by looking at |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 538 | /// all visited phi nodes an making sure that the phis cannot reach the |
| 539 | /// value. We have to do this because we are looking through phi nodes (That |
| 540 | /// is we say noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB). |
| 541 | bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2); |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 542 | |
| 543 | /// \brief Dest and Src are the variable indices from two decomposed |
| 544 | /// GetElementPtr instructions GEP1 and GEP2 which have common base |
| 545 | /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic |
| 546 | /// difference between the two pointers. |
| 547 | void GetIndexDifference(SmallVectorImpl<VariableGEPIndex> &Dest, |
| 548 | const SmallVectorImpl<VariableGEPIndex> &Src); |
| 549 | |
Chris Lattner | 98e25326 | 2009-11-23 16:45:27 +0000 | [diff] [blame] | 550 | // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP |
| 551 | // instruction against another. |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 552 | AliasResult aliasGEP(const GEPOperator *V1, uint64_t V1Size, |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 553 | const MDNode *V1TBAAInfo, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 554 | const Value *V2, uint64_t V2Size, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 555 | const MDNode *V2TBAAInfo, |
Chris Lattner | 5341c96 | 2009-11-26 02:14:59 +0000 | [diff] [blame] | 556 | const Value *UnderlyingV1, const Value *UnderlyingV2); |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 98e25326 | 2009-11-23 16:45:27 +0000 | [diff] [blame] | 558 | // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI |
| 559 | // instruction against another. |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 560 | AliasResult aliasPHI(const PHINode *PN, uint64_t PNSize, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 561 | const MDNode *PNTBAAInfo, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 562 | const Value *V2, uint64_t V2Size, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 563 | const MDNode *V2TBAAInfo); |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 564 | |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 565 | /// aliasSelect - Disambiguate a Select instruction against another value. |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 566 | AliasResult aliasSelect(const SelectInst *SI, uint64_t SISize, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 567 | const MDNode *SITBAAInfo, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 568 | const Value *V2, uint64_t V2Size, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 569 | const MDNode *V2TBAAInfo); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 570 | |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 571 | AliasResult aliasCheck(const Value *V1, uint64_t V1Size, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 572 | const MDNode *V1TBAATag, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 573 | const Value *V2, uint64_t V2Size, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 574 | const MDNode *V2TBAATag); |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 575 | }; |
| 576 | } // End of anonymous namespace |
| 577 | |
| 578 | // Register this pass... |
| 579 | char BasicAliasAnalysis::ID = 0; |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 580 | INITIALIZE_AG_PASS_BEGIN(BasicAliasAnalysis, AliasAnalysis, "basicaa", |
Dan Gohman | da85ed8 | 2010-10-19 23:09:08 +0000 | [diff] [blame] | 581 | "Basic Alias Analysis (stateless AA impl)", |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 582 | false, true, false) |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 583 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
| 584 | INITIALIZE_AG_PASS_END(BasicAliasAnalysis, AliasAnalysis, "basicaa", |
| 585 | "Basic Alias Analysis (stateless AA impl)", |
| 586 | false, true, false) |
| 587 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 588 | |
| 589 | ImmutablePass *llvm::createBasicAliasAnalysisPass() { |
| 590 | return new BasicAliasAnalysis(); |
| 591 | } |
| 592 | |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 593 | /// pointsToConstantMemory - Returns whether the given pointer value |
| 594 | /// points to memory that is local to the function, with global constants being |
| 595 | /// considered local to all functions. |
| 596 | bool |
| 597 | BasicAliasAnalysis::pointsToConstantMemory(const Location &Loc, bool OrLocal) { |
| 598 | assert(Visited.empty() && "Visited must be cleared after use!"); |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 599 | |
Dan Gohman | 142ff82 | 2010-11-08 20:26:19 +0000 | [diff] [blame] | 600 | unsigned MaxLookup = 8; |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 601 | SmallVector<const Value *, 16> Worklist; |
| 602 | Worklist.push_back(Loc.Ptr); |
| 603 | do { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 604 | const Value *V = GetUnderlyingObject(Worklist.pop_back_val(), DL); |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 605 | if (!Visited.insert(V)) { |
| 606 | Visited.clear(); |
| 607 | return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal); |
| 608 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 609 | |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 610 | // An alloca instruction defines local memory. |
| 611 | if (OrLocal && isa<AllocaInst>(V)) |
| 612 | continue; |
| 613 | |
| 614 | // A global constant counts as local memory for our purposes. |
| 615 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 616 | // Note: this doesn't require GV to be "ODR" because it isn't legal for a |
| 617 | // global to be marked constant in some modules and non-constant in |
| 618 | // others. GV may even be a declaration, not a definition. |
| 619 | if (!GV->isConstant()) { |
| 620 | Visited.clear(); |
| 621 | return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal); |
| 622 | } |
| 623 | continue; |
| 624 | } |
| 625 | |
| 626 | // If both select values point to local memory, then so does the select. |
| 627 | if (const SelectInst *SI = dyn_cast<SelectInst>(V)) { |
| 628 | Worklist.push_back(SI->getTrueValue()); |
| 629 | Worklist.push_back(SI->getFalseValue()); |
| 630 | continue; |
| 631 | } |
| 632 | |
| 633 | // If all values incoming to a phi node point to local memory, then so does |
| 634 | // the phi. |
| 635 | if (const PHINode *PN = dyn_cast<PHINode>(V)) { |
Dan Gohman | 142ff82 | 2010-11-08 20:26:19 +0000 | [diff] [blame] | 636 | // Don't bother inspecting phi nodes with many operands. |
| 637 | if (PN->getNumIncomingValues() > MaxLookup) { |
| 638 | Visited.clear(); |
| 639 | return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal); |
| 640 | } |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 641 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 642 | Worklist.push_back(PN->getIncomingValue(i)); |
| 643 | continue; |
| 644 | } |
| 645 | |
| 646 | // Otherwise be conservative. |
| 647 | Visited.clear(); |
| 648 | return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal); |
| 649 | |
Dan Gohman | 142ff82 | 2010-11-08 20:26:19 +0000 | [diff] [blame] | 650 | } while (!Worklist.empty() && --MaxLookup); |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 651 | |
| 652 | Visited.clear(); |
Dan Gohman | 142ff82 | 2010-11-08 20:26:19 +0000 | [diff] [blame] | 653 | return Worklist.empty(); |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 656 | /// getModRefBehavior - Return the behavior when calling the given call site. |
| 657 | AliasAnalysis::ModRefBehavior |
| 658 | BasicAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { |
| 659 | if (CS.doesNotAccessMemory()) |
| 660 | // Can't do better than this. |
| 661 | return DoesNotAccessMemory; |
| 662 | |
| 663 | ModRefBehavior Min = UnknownModRefBehavior; |
| 664 | |
| 665 | // If the callsite knows it only reads memory, don't return worse |
| 666 | // than that. |
| 667 | if (CS.onlyReadsMemory()) |
| 668 | Min = OnlyReadsMemory; |
| 669 | |
| 670 | // The AliasAnalysis base class has some smarts, lets use them. |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 671 | return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /// getModRefBehavior - Return the behavior when calling the given function. |
| 675 | /// For use when the call site is not known. |
| 676 | AliasAnalysis::ModRefBehavior |
| 677 | BasicAliasAnalysis::getModRefBehavior(const Function *F) { |
Dan Gohman | e461d7d | 2010-11-08 16:08:43 +0000 | [diff] [blame] | 678 | // 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] | 679 | if (F->doesNotAccessMemory()) |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 680 | return DoesNotAccessMemory; |
Dan Gohman | e461d7d | 2010-11-08 16:08:43 +0000 | [diff] [blame] | 681 | |
| 682 | // For intrinsics, we can check the table. |
| 683 | if (unsigned iid = F->getIntrinsicID()) { |
| 684 | #define GET_INTRINSIC_MODREF_BEHAVIOR |
Chandler Carruth | db25c6c | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 685 | #include "llvm/IR/Intrinsics.gen" |
Dan Gohman | e461d7d | 2010-11-08 16:08:43 +0000 | [diff] [blame] | 686 | #undef GET_INTRINSIC_MODREF_BEHAVIOR |
| 687 | } |
| 688 | |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 689 | ModRefBehavior Min = UnknownModRefBehavior; |
| 690 | |
Dan Gohman | e461d7d | 2010-11-08 16:08:43 +0000 | [diff] [blame] | 691 | // If the function declares it only reads memory, go with that. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 692 | if (F->onlyReadsMemory()) |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 693 | Min = OnlyReadsMemory; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 694 | |
Dan Gohman | e461d7d | 2010-11-08 16:08:43 +0000 | [diff] [blame] | 695 | // Otherwise be conservative. |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 696 | return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 697 | } |
Owen Anderson | 98a3617 | 2009-02-05 23:36:27 +0000 | [diff] [blame] | 698 | |
Chris Lattner | 98e25326 | 2009-11-23 16:45:27 +0000 | [diff] [blame] | 699 | /// getModRefInfo - Check to see if the specified callsite can clobber the |
| 700 | /// specified memory object. Since we only look at local properties of this |
| 701 | /// function, we really can't say much about this query. We do, however, use |
| 702 | /// simple "address taken" analysis on local objects. |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 703 | AliasAnalysis::ModRefResult |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 704 | BasicAliasAnalysis::getModRefInfo(ImmutableCallSite CS, |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 705 | const Location &Loc) { |
| 706 | assert(notDifferentParent(CS.getInstruction(), Loc.Ptr) && |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 707 | "AliasAnalysis query involving multiple functions!"); |
| 708 | |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 709 | const Value *Object = GetUnderlyingObject(Loc.Ptr, DL); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 710 | |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 711 | // 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] | 712 | // the tail call cannot access or modify the local stack. |
| 713 | // We cannot exclude byval arguments here; these belong to the caller of |
| 714 | // the current function not to the current function, and a tail callee |
| 715 | // may reference them. |
| 716 | if (isa<AllocaInst>(Object)) |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 717 | if (const CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) |
Chris Lattner | d6a49ad | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 718 | if (CI->isTailCall()) |
| 719 | return NoModRef; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 720 | |
Chris Lattner | d6a49ad | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 721 | // 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] | 722 | // then the call can not mod/ref the pointer unless the call takes the pointer |
| 723 | // as an argument, and itself doesn't capture it. |
Chris Lattner | 1e7b37e | 2009-11-23 16:46:41 +0000 | [diff] [blame] | 724 | if (!isa<Constant>(Object) && CS.getInstruction() != Object && |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 725 | isNonEscapingLocalObject(Object)) { |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 726 | bool PassedAsArg = false; |
| 727 | unsigned ArgNo = 0; |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 728 | for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 729 | CI != CE; ++CI, ++ArgNo) { |
Chris Lattner | 026f5e6 | 2011-05-23 05:15:43 +0000 | [diff] [blame] | 730 | // Only look at the no-capture or byval pointer arguments. If this |
| 731 | // pointer were passed to arguments that were neither of these, then it |
| 732 | // couldn't be no-capture. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 733 | if (!(*CI)->getType()->isPointerTy() || |
Nick Lewycky | 612d70b | 2011-11-20 19:09:04 +0000 | [diff] [blame] | 734 | (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo))) |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 735 | continue; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 736 | |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 737 | // 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] | 738 | // is impossible to alias the pointer we're checking. If not, we have to |
| 739 | // assume that the call could touch the pointer, even though it doesn't |
| 740 | // escape. |
Eli Friedman | 5f476dc | 2011-09-28 00:34:27 +0000 | [diff] [blame] | 741 | if (!isNoAlias(Location(*CI), Location(Object))) { |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 742 | PassedAsArg = true; |
| 743 | break; |
| 744 | } |
| 745 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 84ed59ab | 2009-11-23 16:44:43 +0000 | [diff] [blame] | 747 | if (!PassedAsArg) |
Chris Lattner | d6a49ad | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 748 | return NoModRef; |
| 749 | } |
| 750 | |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 751 | const TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfo>(); |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 752 | ModRefResult Min = ModRef; |
| 753 | |
Chris Lattner | d6a49ad | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 754 | // Finally, handle specific knowledge of intrinsics. |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 755 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction()); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 756 | if (II != nullptr) |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 757 | switch (II->getIntrinsicID()) { |
| 758 | default: break; |
| 759 | case Intrinsic::memcpy: |
| 760 | case Intrinsic::memmove: { |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 761 | uint64_t Len = UnknownSize; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 762 | if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) |
| 763 | Len = LenCI->getZExtValue(); |
Gabor Greif | 1abbde3 | 2010-06-23 23:38:07 +0000 | [diff] [blame] | 764 | Value *Dest = II->getArgOperand(0); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 765 | Value *Src = II->getArgOperand(1); |
Chris Lattner | 90c4947 | 2010-11-30 00:43:16 +0000 | [diff] [blame] | 766 | // If it can't overlap the source dest, then it doesn't modref the loc. |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 767 | if (isNoAlias(Location(Dest, Len), Loc)) { |
| 768 | if (isNoAlias(Location(Src, Len), Loc)) |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 769 | return NoModRef; |
Chris Lattner | 90c4947 | 2010-11-30 00:43:16 +0000 | [diff] [blame] | 770 | // If it can't overlap the dest, then worst case it reads the loc. |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 771 | Min = Ref; |
Chris Lattner | 90c4947 | 2010-11-30 00:43:16 +0000 | [diff] [blame] | 772 | } else if (isNoAlias(Location(Src, Len), Loc)) { |
| 773 | // If it can't overlap the source, then worst case it mutates the loc. |
| 774 | Min = Mod; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 775 | } |
| 776 | break; |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 777 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 778 | case Intrinsic::memset: |
| 779 | // Since memset is 'accesses arguments' only, the AliasAnalysis base class |
| 780 | // will handle it for the variable length case. |
| 781 | if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) { |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 782 | uint64_t Len = LenCI->getZExtValue(); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 783 | Value *Dest = II->getArgOperand(0); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 784 | if (isNoAlias(Location(Dest, Len), Loc)) |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 785 | return NoModRef; |
| 786 | } |
Chris Lattner | 9a14637 | 2010-11-30 00:28:45 +0000 | [diff] [blame] | 787 | // We know that memset doesn't load anything. |
| 788 | Min = Mod; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 789 | break; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 790 | case Intrinsic::lifetime_start: |
| 791 | case Intrinsic::lifetime_end: |
| 792 | case Intrinsic::invariant_start: { |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 793 | uint64_t PtrSize = |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 794 | cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 795 | if (isNoAlias(Location(II->getArgOperand(1), |
| 796 | PtrSize, |
| 797 | II->getMetadata(LLVMContext::MD_tbaa)), |
| 798 | Loc)) |
Chris Lattner | d6a49ad | 2009-11-22 16:05:05 +0000 | [diff] [blame] | 799 | return NoModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 800 | break; |
Nick Lewycky | e2782c7 | 2009-10-13 07:48:38 +0000 | [diff] [blame] | 801 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 802 | case Intrinsic::invariant_end: { |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 803 | uint64_t PtrSize = |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 804 | cast<ConstantInt>(II->getArgOperand(1))->getZExtValue(); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 805 | if (isNoAlias(Location(II->getArgOperand(2), |
| 806 | PtrSize, |
| 807 | II->getMetadata(LLVMContext::MD_tbaa)), |
| 808 | Loc)) |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 809 | return NoModRef; |
| 810 | break; |
| 811 | } |
Dan Gohman | 5394c70 | 2011-04-27 20:44:28 +0000 | [diff] [blame] | 812 | case Intrinsic::arm_neon_vld1: { |
| 813 | // LLVM's vld1 and vst1 intrinsics currently only support a single |
| 814 | // vector register. |
| 815 | uint64_t Size = |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 816 | DL ? DL->getTypeStoreSize(II->getType()) : UnknownSize; |
Dan Gohman | 5394c70 | 2011-04-27 20:44:28 +0000 | [diff] [blame] | 817 | if (isNoAlias(Location(II->getArgOperand(0), Size, |
| 818 | II->getMetadata(LLVMContext::MD_tbaa)), |
| 819 | Loc)) |
| 820 | return NoModRef; |
| 821 | break; |
| 822 | } |
| 823 | case Intrinsic::arm_neon_vst1: { |
| 824 | uint64_t Size = |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 825 | DL ? DL->getTypeStoreSize(II->getArgOperand(1)->getType()) : UnknownSize; |
Dan Gohman | 5394c70 | 2011-04-27 20:44:28 +0000 | [diff] [blame] | 826 | if (isNoAlias(Location(II->getArgOperand(0), Size, |
| 827 | II->getMetadata(LLVMContext::MD_tbaa)), |
| 828 | Loc)) |
| 829 | return NoModRef; |
| 830 | break; |
| 831 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 832 | } |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 833 | |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 834 | // We can bound the aliasing properties of memset_pattern16 just as we can |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 835 | // for memcpy/memset. This is particularly important because the |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 836 | // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16 |
| 837 | // whenever possible. |
| 838 | else if (TLI.has(LibFunc::memset_pattern16) && |
| 839 | CS.getCalledFunction() && |
| 840 | CS.getCalledFunction()->getName() == "memset_pattern16") { |
| 841 | const Function *MS = CS.getCalledFunction(); |
| 842 | FunctionType *MemsetType = MS->getFunctionType(); |
| 843 | if (!MemsetType->isVarArg() && MemsetType->getNumParams() == 3 && |
| 844 | isa<PointerType>(MemsetType->getParamType(0)) && |
| 845 | isa<PointerType>(MemsetType->getParamType(1)) && |
| 846 | isa<IntegerType>(MemsetType->getParamType(2))) { |
| 847 | uint64_t Len = UnknownSize; |
| 848 | if (const ConstantInt *LenCI = dyn_cast<ConstantInt>(CS.getArgument(2))) |
| 849 | Len = LenCI->getZExtValue(); |
| 850 | const Value *Dest = CS.getArgument(0); |
| 851 | const Value *Src = CS.getArgument(1); |
| 852 | // If it can't overlap the source dest, then it doesn't modref the loc. |
| 853 | if (isNoAlias(Location(Dest, Len), Loc)) { |
Owen Anderson | f4f09f8 | 2011-09-06 23:43:26 +0000 | [diff] [blame] | 854 | // Always reads 16 bytes of the source. |
| 855 | if (isNoAlias(Location(Src, 16), Loc)) |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 856 | return NoModRef; |
| 857 | // If it can't overlap the dest, then worst case it reads the loc. |
| 858 | Min = Ref; |
Owen Anderson | f4f09f8 | 2011-09-06 23:43:26 +0000 | [diff] [blame] | 859 | // Always reads 16 bytes of the source. |
| 860 | } else if (isNoAlias(Location(Src, 16), Loc)) { |
Owen Anderson | 653cb03 | 2011-09-06 23:33:25 +0000 | [diff] [blame] | 861 | // If it can't overlap the source, then worst case it mutates the loc. |
| 862 | Min = Mod; |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 867 | // The AliasAnalysis base class has some smarts, lets use them. |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 868 | return ModRefResult(AliasAnalysis::getModRefInfo(CS, Loc) & Min); |
Dan Gohman | 64d842e | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 869 | } |
Chris Lattner | 2d33297 | 2008-06-16 06:30:22 +0000 | [diff] [blame] | 870 | |
Chris Lattner | a99edbe | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 871 | /// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction |
| 872 | /// against another pointer. We know that V1 is a GEP, but we don't know |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 873 | /// anything about V2. UnderlyingV1 is GetUnderlyingObject(GEP1, DL), |
Chris Lattner | 5341c96 | 2009-11-26 02:14:59 +0000 | [diff] [blame] | 874 | /// UnderlyingV2 is the same for V2. |
Chris Lattner | a99edbe | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 875 | /// |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 876 | AliasAnalysis::AliasResult |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 877 | BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, uint64_t V1Size, |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 878 | const MDNode *V1TBAAInfo, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 879 | const Value *V2, uint64_t V2Size, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 880 | const MDNode *V2TBAAInfo, |
Chris Lattner | 5341c96 | 2009-11-26 02:14:59 +0000 | [diff] [blame] | 881 | const Value *UnderlyingV1, |
| 882 | const Value *UnderlyingV2) { |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 883 | int64_t GEP1BaseOffset; |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 884 | bool GEP1MaxLookupReached; |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 885 | SmallVector<VariableGEPIndex, 4> GEP1VariableIndices; |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 886 | |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 887 | // If we have two gep instructions with must-alias or not-alias'ing base |
| 888 | // pointers, figure out if the indexes to the GEP tell us anything about the |
| 889 | // derived pointer. |
Chris Lattner | a99edbe | 2009-11-26 02:11:08 +0000 | [diff] [blame] | 890 | if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) { |
Arnold Schwaighofer | aadf104 | 2013-03-26 18:07:53 +0000 | [diff] [blame] | 891 | // Do the base pointers alias? |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 892 | AliasResult BaseAlias = aliasCheck(UnderlyingV1, UnknownSize, nullptr, |
| 893 | UnderlyingV2, UnknownSize, nullptr); |
Arnold Schwaighofer | aadf104 | 2013-03-26 18:07:53 +0000 | [diff] [blame] | 894 | |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 895 | // Check for geps of non-aliasing underlying pointers where the offsets are |
| 896 | // identical. |
Arnold Schwaighofer | aadf104 | 2013-03-26 18:07:53 +0000 | [diff] [blame] | 897 | if ((BaseAlias == MayAlias) && V1Size == V2Size) { |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 898 | // Do the base pointers alias assuming type and size. |
| 899 | AliasResult PreciseBaseAlias = aliasCheck(UnderlyingV1, V1Size, |
| 900 | V1TBAAInfo, UnderlyingV2, |
| 901 | V2Size, V2TBAAInfo); |
| 902 | if (PreciseBaseAlias == NoAlias) { |
| 903 | // See if the computed offset from the common pointer tells us about the |
| 904 | // relation of the resulting pointer. |
| 905 | int64_t GEP2BaseOffset; |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 906 | bool GEP2MaxLookupReached; |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 907 | SmallVector<VariableGEPIndex, 4> GEP2VariableIndices; |
| 908 | const Value *GEP2BasePtr = |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 909 | DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, |
| 910 | GEP2MaxLookupReached, DL); |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 911 | const Value *GEP1BasePtr = |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 912 | DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, |
| 913 | GEP1MaxLookupReached, DL); |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 914 | // DecomposeGEPExpression and GetUnderlyingObject should return the |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 915 | // same result except when DecomposeGEPExpression has no DataLayout. |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 916 | if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 917 | assert(!DL && |
| 918 | "DecomposeGEPExpression and GetUnderlyingObject disagree!"); |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 919 | return MayAlias; |
| 920 | } |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 921 | // If the max search depth is reached the result is undefined |
| 922 | if (GEP2MaxLookupReached || GEP1MaxLookupReached) |
| 923 | return MayAlias; |
| 924 | |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 925 | // Same offsets. |
| 926 | if (GEP1BaseOffset == GEP2BaseOffset && |
Benjamin Kramer | 147644d | 2014-04-18 19:48:03 +0000 | [diff] [blame^] | 927 | GEP1VariableIndices == GEP2VariableIndices) |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 928 | return NoAlias; |
| 929 | GEP1VariableIndices.clear(); |
| 930 | } |
| 931 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 933 | // If we get a No or May, then return it immediately, no amount of analysis |
| 934 | // will improve this situation. |
| 935 | if (BaseAlias != MustAlias) return BaseAlias; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 936 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 937 | // Otherwise, we have a MustAlias. Since the base pointers alias each other |
| 938 | // exactly, see if the computed offset from the common pointer tells us |
| 939 | // about the relation of the resulting pointer. |
| 940 | const Value *GEP1BasePtr = |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 941 | DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, |
| 942 | GEP1MaxLookupReached, DL); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 943 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 944 | int64_t GEP2BaseOffset; |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 945 | bool GEP2MaxLookupReached; |
Chris Lattner | 1b9c387 | 2010-08-18 22:47:56 +0000 | [diff] [blame] | 946 | SmallVector<VariableGEPIndex, 4> GEP2VariableIndices; |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 947 | const Value *GEP2BasePtr = |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 948 | DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, |
| 949 | GEP2MaxLookupReached, DL); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 950 | |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 951 | // DecomposeGEPExpression and GetUnderlyingObject should return the |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 952 | // same result except when DecomposeGEPExpression has no DataLayout. |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 953 | if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 954 | assert(!DL && |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 955 | "DecomposeGEPExpression and GetUnderlyingObject disagree!"); |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 956 | return MayAlias; |
| 957 | } |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 958 | // If the max search depth is reached the result is undefined |
| 959 | if (GEP2MaxLookupReached || GEP1MaxLookupReached) |
| 960 | return MayAlias; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 961 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 962 | // Subtract the GEP2 pointer from the GEP1 pointer to find out their |
| 963 | // symbolic difference. |
| 964 | GEP1BaseOffset -= GEP2BaseOffset; |
Dan Gohman | ad867b0 | 2010-08-03 20:23:52 +0000 | [diff] [blame] | 965 | GetIndexDifference(GEP1VariableIndices, GEP2VariableIndices); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 966 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 967 | } else { |
| 968 | // Check to see if these two pointers are related by the getelementptr |
| 969 | // instruction. If one pointer is a GEP with a non-zero index of the other |
| 970 | // pointer, we know they cannot alias. |
Chris Lattner | 5c1cfc2 | 2009-11-26 16:52:32 +0000 | [diff] [blame] | 971 | |
| 972 | // If both accesses are unknown size, we can't do anything useful here. |
Dan Gohman | 2a19008 | 2010-08-03 01:03:11 +0000 | [diff] [blame] | 973 | if (V1Size == UnknownSize && V2Size == UnknownSize) |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 974 | return MayAlias; |
Chris Lattner | 6ea17f77f | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 975 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 976 | AliasResult R = aliasCheck(UnderlyingV1, UnknownSize, nullptr, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 977 | V2, V2Size, V2TBAAInfo); |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 978 | if (R != MustAlias) |
| 979 | // If V2 may alias GEP base pointer, conservatively returns MayAlias. |
| 980 | // If V2 is known not to alias GEP base pointer, then the two values |
| 981 | // cannot alias per GEP semantics: "A pointer value formed from a |
| 982 | // getelementptr instruction is associated with the addresses associated |
| 983 | // with the first operand of the getelementptr". |
| 984 | return R; |
Chris Lattner | 6ea17f77f | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 986 | const Value *GEP1BasePtr = |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 987 | DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, |
| 988 | GEP1MaxLookupReached, DL); |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 989 | |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 990 | // DecomposeGEPExpression and GetUnderlyingObject should return the |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 991 | // same result except when DecomposeGEPExpression has no DataLayout. |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 992 | if (GEP1BasePtr != UnderlyingV1) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 993 | assert(!DL && |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 994 | "DecomposeGEPExpression and GetUnderlyingObject disagree!"); |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 995 | return MayAlias; |
Chris Lattner | 6ea17f77f | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 996 | } |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 997 | // If the max search depth is reached the result is undefined |
| 998 | if (GEP1MaxLookupReached) |
| 999 | return MayAlias; |
Chris Lattner | 6ea17f77f | 2003-12-11 22:44:13 +0000 | [diff] [blame] | 1000 | } |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 1002 | // In the two GEP Case, if there is no difference in the offsets of the |
| 1003 | // computed pointers, the resultant pointers are a must alias. This |
| 1004 | // hapens when we have two lexically identical GEP's (for example). |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 1005 | // |
Chris Lattner | 7a5b56a | 2009-11-26 02:17:34 +0000 | [diff] [blame] | 1006 | // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2 |
| 1007 | // must aliases the GEP, the end result is a must alias also. |
| 1008 | if (GEP1BaseOffset == 0 && GEP1VariableIndices.empty()) |
Evan Cheng | c1eed9d | 2009-10-14 06:41:49 +0000 | [diff] [blame] | 1009 | return MustAlias; |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1010 | |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1011 | // If there is a constant difference between the pointers, but the difference |
| 1012 | // is less than the size of the associated memory object, then we know |
| 1013 | // that the objects are partially overlapping. If the difference is |
| 1014 | // greater, we know they do not overlap. |
Dan Gohman | c4bf5ca | 2010-12-13 22:50:24 +0000 | [diff] [blame] | 1015 | if (GEP1BaseOffset != 0 && GEP1VariableIndices.empty()) { |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1016 | if (GEP1BaseOffset >= 0) { |
| 1017 | if (V2Size != UnknownSize) { |
| 1018 | if ((uint64_t)GEP1BaseOffset < V2Size) |
| 1019 | return PartialAlias; |
| 1020 | return NoAlias; |
| 1021 | } |
| 1022 | } else { |
Arnold Schwaighofer | e3ac099 | 2014-01-16 04:53:18 +0000 | [diff] [blame] | 1023 | // We have the situation where: |
| 1024 | // + + |
| 1025 | // | BaseOffset | |
| 1026 | // ---------------->| |
| 1027 | // |-->V1Size |-------> V2Size |
| 1028 | // GEP1 V2 |
| 1029 | // We need to know that V2Size is not unknown, otherwise we might have |
| 1030 | // stripped a gep with negative index ('gep <ptr>, -1, ...). |
| 1031 | if (V1Size != UnknownSize && V2Size != UnknownSize) { |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1032 | if (-(uint64_t)GEP1BaseOffset < V1Size) |
| 1033 | return PartialAlias; |
| 1034 | return NoAlias; |
| 1035 | } |
| 1036 | } |
Dan Gohman | c4bf5ca | 2010-12-13 22:50:24 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1039 | // Try to distinguish something like &A[i][1] against &A[42][0]. |
| 1040 | // Grab the least significant bit set in any of the scales. |
Eli Friedman | b78ac54 | 2011-09-08 02:37:07 +0000 | [diff] [blame] | 1041 | if (!GEP1VariableIndices.empty()) { |
| 1042 | uint64_t Modulo = 0; |
| 1043 | for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i) |
| 1044 | Modulo |= (uint64_t)GEP1VariableIndices[i].Scale; |
| 1045 | Modulo = Modulo ^ (Modulo & (Modulo - 1)); |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1046 | |
Eli Friedman | b78ac54 | 2011-09-08 02:37:07 +0000 | [diff] [blame] | 1047 | // We can compute the difference between the two addresses |
| 1048 | // mod Modulo. Check whether that difference guarantees that the |
| 1049 | // two locations do not alias. |
| 1050 | uint64_t ModOffset = (uint64_t)GEP1BaseOffset & (Modulo - 1); |
| 1051 | if (V1Size != UnknownSize && V2Size != UnknownSize && |
| 1052 | ModOffset >= V2Size && V1Size <= Modulo - ModOffset) |
| 1053 | return NoAlias; |
| 1054 | } |
Eli Friedman | 3d1b307 | 2011-09-08 02:23:31 +0000 | [diff] [blame] | 1055 | |
Dan Gohman | adf80ae | 2011-06-04 06:50:18 +0000 | [diff] [blame] | 1056 | // Statically, we can see that the base objects are the same, but the |
| 1057 | // pointers have dynamic offsets which we can't resolve. And none of our |
| 1058 | // little tricks above worked. |
| 1059 | // |
| 1060 | // TODO: Returning PartialAlias instead of MayAlias is a mild hack; the |
| 1061 | // practical effect of this is protecting TBAA in the case of dynamic |
Dan Gohman | 9017b84 | 2012-02-17 18:33:38 +0000 | [diff] [blame] | 1062 | // indices into arrays of unions or malloc'd memory. |
Dan Gohman | adf80ae | 2011-06-04 06:50:18 +0000 | [diff] [blame] | 1063 | return PartialAlias; |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1066 | static AliasAnalysis::AliasResult |
| 1067 | MergeAliasResults(AliasAnalysis::AliasResult A, AliasAnalysis::AliasResult B) { |
| 1068 | // If the results agree, take it. |
| 1069 | if (A == B) |
| 1070 | return A; |
| 1071 | // A mix of PartialAlias and MustAlias is PartialAlias. |
| 1072 | if ((A == AliasAnalysis::PartialAlias && B == AliasAnalysis::MustAlias) || |
| 1073 | (B == AliasAnalysis::PartialAlias && A == AliasAnalysis::MustAlias)) |
| 1074 | return AliasAnalysis::PartialAlias; |
| 1075 | // Otherwise, we don't know anything. |
| 1076 | return AliasAnalysis::MayAlias; |
| 1077 | } |
| 1078 | |
Chris Lattner | 98e25326 | 2009-11-23 16:45:27 +0000 | [diff] [blame] | 1079 | /// aliasSelect - Provide a bunch of ad-hoc rules to disambiguate a Select |
| 1080 | /// instruction against another. |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1081 | AliasAnalysis::AliasResult |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 1082 | BasicAliasAnalysis::aliasSelect(const SelectInst *SI, uint64_t SISize, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1083 | const MDNode *SITBAAInfo, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 1084 | const Value *V2, uint64_t V2Size, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1085 | const MDNode *V2TBAAInfo) { |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1086 | // If the values are Selects with the same condition, we can do a more precise |
| 1087 | // check: just check for aliases between the values on corresponding arms. |
| 1088 | if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) |
| 1089 | if (SI->getCondition() == SI2->getCondition()) { |
| 1090 | AliasResult Alias = |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1091 | aliasCheck(SI->getTrueValue(), SISize, SITBAAInfo, |
| 1092 | SI2->getTrueValue(), V2Size, V2TBAAInfo); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1093 | if (Alias == MayAlias) |
| 1094 | return MayAlias; |
| 1095 | AliasResult ThisAlias = |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1096 | aliasCheck(SI->getFalseValue(), SISize, SITBAAInfo, |
| 1097 | SI2->getFalseValue(), V2Size, V2TBAAInfo); |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1098 | return MergeAliasResults(ThisAlias, Alias); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | // If both arms of the Select node NoAlias or MustAlias V2, then returns |
| 1102 | // NoAlias / MustAlias. Otherwise, returns MayAlias. |
| 1103 | AliasResult Alias = |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1104 | aliasCheck(V2, V2Size, V2TBAAInfo, SI->getTrueValue(), SISize, SITBAAInfo); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1105 | if (Alias == MayAlias) |
| 1106 | return MayAlias; |
Dan Gohman | 7c34ece | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 1107 | |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1108 | AliasResult ThisAlias = |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1109 | aliasCheck(V2, V2Size, V2TBAAInfo, SI->getFalseValue(), SISize, SITBAAInfo); |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1110 | return MergeAliasResults(ThisAlias, Alias); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Evan Cheng | f92f555 | 2009-10-14 05:22:03 +0000 | [diff] [blame] | 1113 | // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI instruction |
Evan Cheng | 31565b3 | 2009-10-14 05:05:02 +0000 | [diff] [blame] | 1114 | // against another. |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1115 | AliasAnalysis::AliasResult |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 1116 | BasicAliasAnalysis::aliasPHI(const PHINode *PN, uint64_t PNSize, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1117 | const MDNode *PNTBAAInfo, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 1118 | const Value *V2, uint64_t V2Size, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1119 | const MDNode *V2TBAAInfo) { |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1120 | // Track phi nodes we have visited. We use this information when we determine |
| 1121 | // value equivalence. |
| 1122 | VisitedPhiBBs.insert(PN->getParent()); |
| 1123 | |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1124 | // If the values are PHIs in the same block, we can do a more precise |
| 1125 | // as well as efficient check: just check for aliases between the values |
| 1126 | // on corresponding edges. |
| 1127 | if (const PHINode *PN2 = dyn_cast<PHINode>(V2)) |
| 1128 | if (PN2->getParent() == PN->getParent()) { |
Arnold Schwaighofer | 8dc34cf | 2012-09-06 14:41:53 +0000 | [diff] [blame] | 1129 | LocPair Locs(Location(PN, PNSize, PNTBAAInfo), |
| 1130 | Location(V2, V2Size, V2TBAAInfo)); |
| 1131 | if (PN > V2) |
| 1132 | std::swap(Locs.first, Locs.second); |
Arnold Schwaighofer | edd62b1 | 2012-12-10 23:02:41 +0000 | [diff] [blame] | 1133 | // Analyse the PHIs' inputs under the assumption that the PHIs are |
| 1134 | // NoAlias. |
| 1135 | // If the PHIs are May/MustAlias there must be (recursively) an input |
| 1136 | // operand from outside the PHIs' cycle that is MayAlias/MustAlias or |
| 1137 | // there must be an operation on the PHIs within the PHIs' value cycle |
| 1138 | // that causes a MayAlias. |
| 1139 | // Pretend the phis do not alias. |
| 1140 | AliasResult Alias = NoAlias; |
| 1141 | assert(AliasCache.count(Locs) && |
| 1142 | "There must exist an entry for the phi node"); |
| 1143 | AliasResult OrigAliasResult = AliasCache[Locs]; |
| 1144 | AliasCache[Locs] = NoAlias; |
Arnold Schwaighofer | 8dc34cf | 2012-09-06 14:41:53 +0000 | [diff] [blame] | 1145 | |
Hal Finkel | a6f86fc | 2012-11-17 02:33:15 +0000 | [diff] [blame] | 1146 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1147 | AliasResult ThisAlias = |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1148 | aliasCheck(PN->getIncomingValue(i), PNSize, PNTBAAInfo, |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1149 | PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)), |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1150 | V2Size, V2TBAAInfo); |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1151 | Alias = MergeAliasResults(ThisAlias, Alias); |
| 1152 | if (Alias == MayAlias) |
| 1153 | break; |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1154 | } |
Arnold Schwaighofer | 8dc34cf | 2012-09-06 14:41:53 +0000 | [diff] [blame] | 1155 | |
| 1156 | // Reset if speculation failed. |
Arnold Schwaighofer | edd62b1 | 2012-12-10 23:02:41 +0000 | [diff] [blame] | 1157 | if (Alias != NoAlias) |
Arnold Schwaighofer | 8dc34cf | 2012-09-06 14:41:53 +0000 | [diff] [blame] | 1158 | AliasCache[Locs] = OrigAliasResult; |
| 1159 | |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1160 | return Alias; |
| 1161 | } |
| 1162 | |
Evan Cheng | 8ec2593 | 2009-10-16 00:33:09 +0000 | [diff] [blame] | 1163 | SmallPtrSet<Value*, 4> UniqueSrc; |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1164 | SmallVector<Value*, 4> V1Srcs; |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1165 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1166 | Value *PV1 = PN->getIncomingValue(i); |
| 1167 | if (isa<PHINode>(PV1)) |
| 1168 | // If any of the source itself is a PHI, return MayAlias conservatively |
Evan Cheng | c1eed9d | 2009-10-14 06:41:49 +0000 | [diff] [blame] | 1169 | // to avoid compile time explosion. The worst possible case is if both |
| 1170 | // sides are PHI nodes. In which case, this is O(m x n) time where 'm' |
| 1171 | // and 'n' are the number of PHI sources. |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1172 | return MayAlias; |
| 1173 | if (UniqueSrc.insert(PV1)) |
| 1174 | V1Srcs.push_back(PV1); |
| 1175 | } |
| 1176 | |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1177 | AliasResult Alias = aliasCheck(V2, V2Size, V2TBAAInfo, |
| 1178 | V1Srcs[0], PNSize, PNTBAAInfo); |
Evan Cheng | f92f555 | 2009-10-14 05:22:03 +0000 | [diff] [blame] | 1179 | // Early exit if the check of the first PHI source against V2 is MayAlias. |
| 1180 | // Other results are not possible. |
| 1181 | if (Alias == MayAlias) |
| 1182 | return MayAlias; |
| 1183 | |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1184 | // If all sources of the PHI node NoAlias or MustAlias V2, then returns |
| 1185 | // NoAlias / MustAlias. Otherwise, returns MayAlias. |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1186 | for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) { |
| 1187 | Value *V = V1Srcs[i]; |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1188 | |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1189 | AliasResult ThisAlias = aliasCheck(V2, V2Size, V2TBAAInfo, |
| 1190 | V, PNSize, PNTBAAInfo); |
Dan Gohman | 4e7e795 | 2011-06-03 20:17:36 +0000 | [diff] [blame] | 1191 | Alias = MergeAliasResults(ThisAlias, Alias); |
| 1192 | if (Alias == MayAlias) |
| 1193 | break; |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | return Alias; |
| 1197 | } |
| 1198 | |
| 1199 | // aliasCheck - Provide a bunch of ad-hoc rules to disambiguate in common cases, |
| 1200 | // such as array references. |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1201 | // |
| 1202 | AliasAnalysis::AliasResult |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 1203 | BasicAliasAnalysis::aliasCheck(const Value *V1, uint64_t V1Size, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1204 | const MDNode *V1TBAAInfo, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 1205 | const Value *V2, uint64_t V2Size, |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1206 | const MDNode *V2TBAAInfo) { |
Dan Gohman | cb45bd9 | 2010-04-08 18:11:50 +0000 | [diff] [blame] | 1207 | // If either of the memory references is empty, it doesn't matter what the |
| 1208 | // pointer values are. |
| 1209 | if (V1Size == 0 || V2Size == 0) |
| 1210 | return NoAlias; |
| 1211 | |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1212 | // Strip off any casts if they exist. |
| 1213 | V1 = V1->stripPointerCasts(); |
| 1214 | V2 = V2->stripPointerCasts(); |
| 1215 | |
| 1216 | // Are we checking for alias of the same value? |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1217 | // Because we look 'through' phi nodes we could look at "Value" pointers from |
| 1218 | // different iterations. We must therefore make sure that this is not the |
| 1219 | // case. The function isValueEqualInPotentialCycles ensures that this cannot |
| 1220 | // happen by looking at the visited phi nodes and making sure they cannot |
| 1221 | // reach the value. |
| 1222 | if (isValueEqualInPotentialCycles(V1, V2)) |
| 1223 | return MustAlias; |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1224 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1225 | if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy()) |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1226 | return NoAlias; // Scalars cannot alias each other |
| 1227 | |
| 1228 | // Figure out what objects these things are pointing to if we can. |
Arnold Schwaighofer | 1a44448 | 2014-03-26 21:30:19 +0000 | [diff] [blame] | 1229 | const Value *O1 = GetUnderlyingObject(V1, DL, MaxLookupSearchDepth); |
| 1230 | const Value *O2 = GetUnderlyingObject(V2, DL, MaxLookupSearchDepth); |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1231 | |
Dan Gohman | ccb4584 | 2009-11-09 19:29:11 +0000 | [diff] [blame] | 1232 | // Null values in the default address space don't point to any object, so they |
| 1233 | // don't alias any other pointer. |
| 1234 | if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1)) |
| 1235 | if (CPN->getType()->getAddressSpace() == 0) |
| 1236 | return NoAlias; |
| 1237 | if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2)) |
| 1238 | if (CPN->getType()->getAddressSpace() == 0) |
| 1239 | return NoAlias; |
| 1240 | |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1241 | if (O1 != O2) { |
| 1242 | // 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] | 1243 | if (isIdentifiedObject(O1) && isIdentifiedObject(O2)) |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1244 | return NoAlias; |
Nick Lewycky | c53e2ec | 2009-11-14 06:15:14 +0000 | [diff] [blame] | 1245 | |
| 1246 | // Constant pointers can't alias with non-const isIdentifiedObject objects. |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 1247 | if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) || |
| 1248 | (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1))) |
Nick Lewycky | c53e2ec | 2009-11-14 06:15:14 +0000 | [diff] [blame] | 1249 | return NoAlias; |
| 1250 | |
Michael Kuperstein | f3e663a | 2013-05-28 08:17:48 +0000 | [diff] [blame] | 1251 | // Function arguments can't alias with things that are known to be |
| 1252 | // unambigously identified at the function level. |
| 1253 | if ((isa<Argument>(O1) && isIdentifiedFunctionLocal(O2)) || |
| 1254 | (isa<Argument>(O2) && isIdentifiedFunctionLocal(O1))) |
Dan Gohman | 84f90a3 | 2010-07-01 20:08:40 +0000 | [diff] [blame] | 1255 | return NoAlias; |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1256 | |
| 1257 | // Most objects can't alias null. |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 1258 | if ((isa<ConstantPointerNull>(O2) && isKnownNonNull(O1)) || |
| 1259 | (isa<ConstantPointerNull>(O1) && isKnownNonNull(O2))) |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1260 | return NoAlias; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 1261 | |
Dan Gohman | 5b0a8a8 | 2010-07-07 14:30:04 +0000 | [diff] [blame] | 1262 | // If one pointer is the result of a call/invoke or load and the other is a |
| 1263 | // non-escaping local object within the same function, then we know the |
| 1264 | // object couldn't escape to a point where the call could return it. |
| 1265 | // |
| 1266 | // Note that if the pointers are in different functions, there are a |
| 1267 | // variety of complications. A call with a nocapture argument may still |
| 1268 | // temporary store the nocapture argument's value in a temporary memory |
| 1269 | // location if that memory location doesn't escape. Or it may pass a |
| 1270 | // nocapture value to other functions as long as they don't capture it. |
| 1271 | if (isEscapeSource(O1) && isNonEscapingLocalObject(O2)) |
| 1272 | return NoAlias; |
| 1273 | if (isEscapeSource(O2) && isNonEscapingLocalObject(O1)) |
| 1274 | return NoAlias; |
| 1275 | } |
| 1276 | |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1277 | // If the size of one access is larger than the entire object on the other |
| 1278 | // 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] | 1279 | if (DL) |
| 1280 | if ((V1Size != UnknownSize && isObjectSmallerThan(O2, V1Size, *DL, *TLI)) || |
| 1281 | (V2Size != UnknownSize && isObjectSmallerThan(O1, V2Size, *DL, *TLI))) |
Evan Cheng | f1f3dd3 | 2009-10-13 18:42:04 +0000 | [diff] [blame] | 1282 | return NoAlias; |
Jakub Staszak | 07f383f | 2013-08-24 14:16:00 +0000 | [diff] [blame] | 1283 | |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1284 | // Check the cache before climbing up use-def chains. This also terminates |
| 1285 | // otherwise infinitely recursive queries. |
| 1286 | LocPair Locs(Location(V1, V1Size, V1TBAAInfo), |
| 1287 | Location(V2, V2Size, V2TBAAInfo)); |
| 1288 | if (V1 > V2) |
| 1289 | std::swap(Locs.first, Locs.second); |
| 1290 | std::pair<AliasCacheTy::iterator, bool> Pair = |
| 1291 | AliasCache.insert(std::make_pair(Locs, MayAlias)); |
| 1292 | if (!Pair.second) |
| 1293 | return Pair.first->second; |
| 1294 | |
Chris Lattner | 8928899 | 2009-11-26 02:13:03 +0000 | [diff] [blame] | 1295 | // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the |
| 1296 | // 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] | 1297 | if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) { |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 1298 | std::swap(V1, V2); |
| 1299 | std::swap(V1Size, V2Size); |
Chris Lattner | 5341c96 | 2009-11-26 02:14:59 +0000 | [diff] [blame] | 1300 | std::swap(O1, O2); |
Duncan Sands | 71c2070 | 2012-11-04 09:02:45 +0000 | [diff] [blame] | 1301 | std::swap(V1TBAAInfo, V2TBAAInfo); |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 1302 | } |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1303 | if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) { |
Arnold Schwaighofer | 76dca58 | 2012-09-06 14:31:51 +0000 | [diff] [blame] | 1304 | AliasResult Result = aliasGEP(GV1, V1Size, V1TBAAInfo, V2, V2Size, V2TBAAInfo, O1, O2); |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1305 | if (Result != MayAlias) return AliasCache[Locs] = Result; |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1306 | } |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1307 | |
| 1308 | if (isa<PHINode>(V2) && !isa<PHINode>(V1)) { |
| 1309 | std::swap(V1, V2); |
| 1310 | std::swap(V1Size, V2Size); |
Duncan Sands | 71c2070 | 2012-11-04 09:02:45 +0000 | [diff] [blame] | 1311 | std::swap(V1TBAAInfo, V2TBAAInfo); |
Evan Cheng | c10e88d | 2009-10-13 22:02:20 +0000 | [diff] [blame] | 1312 | } |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1313 | if (const PHINode *PN = dyn_cast<PHINode>(V1)) { |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1314 | AliasResult Result = aliasPHI(PN, V1Size, V1TBAAInfo, |
| 1315 | V2, V2Size, V2TBAAInfo); |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1316 | if (Result != MayAlias) return AliasCache[Locs] = Result; |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1317 | } |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1318 | |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1319 | if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) { |
| 1320 | std::swap(V1, V2); |
| 1321 | std::swap(V1Size, V2Size); |
Duncan Sands | 71c2070 | 2012-11-04 09:02:45 +0000 | [diff] [blame] | 1322 | std::swap(V1TBAAInfo, V2TBAAInfo); |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1323 | } |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1324 | if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) { |
Dan Gohman | f370245 | 2010-10-18 18:45:11 +0000 | [diff] [blame] | 1325 | AliasResult Result = aliasSelect(S1, V1Size, V1TBAAInfo, |
| 1326 | V2, V2Size, V2TBAAInfo); |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1327 | if (Result != MayAlias) return AliasCache[Locs] = Result; |
Dan Gohman | 02538ac | 2010-10-18 18:04:47 +0000 | [diff] [blame] | 1328 | } |
Dan Gohman | 3b7ba5f | 2009-10-26 21:55:43 +0000 | [diff] [blame] | 1329 | |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 1330 | // If both pointers are pointing into the same object and one of them |
| 1331 | // accesses is accessing the entire object, then the accesses must |
| 1332 | // overlap in some way. |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1333 | if (DL && O1 == O2) |
| 1334 | if ((V1Size != UnknownSize && isObjectSize(O1, V1Size, *DL, *TLI)) || |
| 1335 | (V2Size != UnknownSize && isObjectSize(O2, V2Size, *DL, *TLI))) |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1336 | return AliasCache[Locs] = PartialAlias; |
Dan Gohman | 44da55b | 2011-01-18 21:16:06 +0000 | [diff] [blame] | 1337 | |
Dan Gohman | fb02cec | 2011-06-04 00:31:50 +0000 | [diff] [blame] | 1338 | AliasResult Result = |
| 1339 | AliasAnalysis::alias(Location(V1, V1Size, V1TBAAInfo), |
| 1340 | Location(V2, V2Size, V2TBAAInfo)); |
| 1341 | return AliasCache[Locs] = Result; |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 1342 | } |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1343 | |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1344 | bool BasicAliasAnalysis::isValueEqualInPotentialCycles(const Value *V, |
| 1345 | const Value *V2) { |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1346 | if (V != V2) |
| 1347 | return false; |
| 1348 | |
| 1349 | const Instruction *Inst = dyn_cast<Instruction>(V); |
| 1350 | if (!Inst) |
| 1351 | return true; |
| 1352 | |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1353 | if (VisitedPhiBBs.size() > MaxNumPhiBBsValueReachabilityCheck) |
| 1354 | return false; |
| 1355 | |
| 1356 | // Use dominance or loop info if available. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 1357 | DominatorTreeWrapperPass *DTWP = |
| 1358 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1359 | DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr; |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1360 | LoopInfo *LI = getAnalysisIfAvailable<LoopInfo>(); |
| 1361 | |
| 1362 | // Make sure that the visited phis cannot reach the Value. This ensures that |
| 1363 | // the Values cannot come from different iterations of a potential cycle the |
| 1364 | // phi nodes could be involved in. |
| 1365 | for (SmallPtrSet<const BasicBlock *, 8>::iterator PI = VisitedPhiBBs.begin(), |
| 1366 | PE = VisitedPhiBBs.end(); |
| 1367 | PI != PE; ++PI) |
| 1368 | if (isPotentiallyReachable((*PI)->begin(), Inst, DT, LI)) |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1369 | return false; |
| 1370 | |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1371 | return true; |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /// GetIndexDifference - Dest and Src are the variable indices from two |
| 1375 | /// decomposed GetElementPtr instructions GEP1 and GEP2 which have common base |
| 1376 | /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic |
| 1377 | /// difference between the two pointers. |
| 1378 | void BasicAliasAnalysis::GetIndexDifference( |
| 1379 | SmallVectorImpl<VariableGEPIndex> &Dest, |
| 1380 | const SmallVectorImpl<VariableGEPIndex> &Src) { |
| 1381 | if (Src.empty()) |
| 1382 | return; |
| 1383 | |
| 1384 | for (unsigned i = 0, e = Src.size(); i != e; ++i) { |
| 1385 | const Value *V = Src[i].V; |
| 1386 | ExtensionKind Extension = Src[i].Extension; |
| 1387 | int64_t Scale = Src[i].Scale; |
| 1388 | |
| 1389 | // Find V in Dest. This is N^2, but pointer indices almost never have more |
| 1390 | // than a few variable indexes. |
| 1391 | for (unsigned j = 0, e = Dest.size(); j != e; ++j) { |
Arnold Schwaighofer | 833a82e | 2014-01-03 05:47:03 +0000 | [diff] [blame] | 1392 | if (!isValueEqualInPotentialCycles(Dest[j].V, V) || |
| 1393 | Dest[j].Extension != Extension) |
Arnold Schwaighofer | 0d10a9d | 2014-01-02 03:31:36 +0000 | [diff] [blame] | 1394 | continue; |
| 1395 | |
| 1396 | // If we found it, subtract off Scale V's from the entry in Dest. If it |
| 1397 | // goes to zero, remove the entry. |
| 1398 | if (Dest[j].Scale != Scale) |
| 1399 | Dest[j].Scale -= Scale; |
| 1400 | else |
| 1401 | Dest.erase(Dest.begin() + j); |
| 1402 | Scale = 0; |
| 1403 | break; |
| 1404 | } |
| 1405 | |
| 1406 | // If we didn't consume this entry, add it to the end of the Dest list. |
| 1407 | if (Scale) { |
| 1408 | VariableGEPIndex Entry = { V, Extension, -Scale }; |
| 1409 | Dest.push_back(Entry); |
| 1410 | } |
| 1411 | } |
| 1412 | } |