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