Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 1 | //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// |
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 | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the generic AliasAnalysis interface which is used as the |
| 11 | // common interface used by all clients and implementations of alias analysis. |
| 12 | // |
| 13 | // This file also implements the default version of the AliasAnalysis interface |
| 14 | // that is to be used when no other implementation is specified. This does some |
| 15 | // simple tests that detect obvious cases: two different global pointers cannot |
| 16 | // alias, a global cannot alias a malloc, two different mallocs cannot alias, |
| 17 | // etc. |
| 18 | // |
| 19 | // This alias analysis implementation really isn't very good for anything, but |
| 20 | // it is very fast, and makes a nice clean default implementation. Because it |
| 21 | // handles lots of little corner cases, other, more complex, alias analysis |
| 22 | // implementations may choose to rely on this pass to resolve these simple and |
| 23 | // easy cases. |
| 24 | // |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Chris Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Nick Lewycky | 0b68245 | 2013-07-27 01:24:00 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/CFG.h" |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/CFLAndersAliasAnalysis.h" |
| 31 | #include "llvm/Analysis/CFLSteensAliasAnalysis.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/GlobalsModRef.h" |
| 34 | #include "llvm/Analysis/ObjCARCAliasAnalysis.h" |
| 35 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" |
| 36 | #include "llvm/Analysis/ScopedNoAliasAA.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/TypeBasedAliasAnalysis.h" |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 40 | #include "llvm/IR/BasicBlock.h" |
| 41 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Function.h" |
| 44 | #include "llvm/IR/Instructions.h" |
| 45 | #include "llvm/IR/IntrinsicInst.h" |
| 46 | #include "llvm/IR/LLVMContext.h" |
| 47 | #include "llvm/IR/Type.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 48 | #include "llvm/Pass.h" |
Chris Lattner | a67dbd0 | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 49 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 50 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 51 | /// Allow disabling BasicAA from the AA results. This is particularly useful |
| 52 | /// when testing to isolate a single AA implementation. |
| 53 | static cl::opt<bool> DisableBasicAA("disable-basicaa", cl::Hidden, |
| 54 | cl::init(false)); |
| 55 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 56 | AAResults::AAResults(AAResults &&Arg) : TLI(Arg.TLI), AAs(std::move(Arg.AAs)) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 57 | for (auto &AA : AAs) |
| 58 | AA->setAAResults(this); |
| 59 | } |
| 60 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 61 | AAResults::~AAResults() { |
| 62 | // FIXME; It would be nice to at least clear out the pointers back to this |
| 63 | // aggregation here, but we end up with non-nesting lifetimes in the legacy |
| 64 | // pass manager that prevent this from working. In the legacy pass manager |
| 65 | // we'll end up with dangling references here in some cases. |
| 66 | #if 0 |
| 67 | for (auto &AA : AAs) |
| 68 | AA->setAAResults(nullptr); |
| 69 | #endif |
| 70 | } |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 72 | //===----------------------------------------------------------------------===// |
| 73 | // Default chaining methods |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 76 | AliasResult AAResults::alias(const MemoryLocation &LocA, |
| 77 | const MemoryLocation &LocB) { |
| 78 | for (const auto &AA : AAs) { |
| 79 | auto Result = AA->alias(LocA, LocB); |
| 80 | if (Result != MayAlias) |
| 81 | return Result; |
| 82 | } |
| 83 | return MayAlias; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 86 | bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc, |
| 87 | bool OrLocal) { |
| 88 | for (const auto &AA : AAs) |
| 89 | if (AA->pointsToConstantMemory(Loc, OrLocal)) |
| 90 | return true; |
| 91 | |
| 92 | return false; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 95 | ModRefInfo AAResults::getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) { |
| 96 | ModRefInfo Result = MRI_ModRef; |
| 97 | |
| 98 | for (const auto &AA : AAs) { |
| 99 | Result = ModRefInfo(Result & AA->getArgModRefInfo(CS, ArgIdx)); |
| 100 | |
| 101 | // Early-exit the moment we reach the bottom of the lattice. |
| 102 | if (Result == MRI_NoModRef) |
| 103 | return Result; |
| 104 | } |
| 105 | |
| 106 | return Result; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 109 | ModRefInfo AAResults::getModRefInfo(Instruction *I, ImmutableCallSite Call) { |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 110 | // We may have two calls |
| 111 | if (auto CS = ImmutableCallSite(I)) { |
| 112 | // Check if the two calls modify the same memory |
Nicolai Haehnle | 9343f36 | 2016-07-11 14:11:45 +0000 | [diff] [blame] | 113 | return getModRefInfo(CS, Call); |
David Majnemer | a940f36 | 2016-07-15 17:19:24 +0000 | [diff] [blame] | 114 | } else if (I->isFenceLike()) { |
| 115 | // If this is a fence, just return MRI_ModRef. |
| 116 | return MRI_ModRef; |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 117 | } else { |
| 118 | // Otherwise, check if the call modifies or references the |
| 119 | // location this memory access defines. The best we can say |
| 120 | // is that if the call references what this instruction |
| 121 | // defines, it must be clobbered by this location. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 122 | const MemoryLocation DefLoc = MemoryLocation::get(I); |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 123 | if (getModRefInfo(Call, DefLoc) != MRI_NoModRef) |
| 124 | return MRI_ModRef; |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 125 | } |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 126 | return MRI_NoModRef; |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 127 | } |
Owen Anderson | b6e4ff0 | 2011-01-03 21:38:41 +0000 | [diff] [blame] | 128 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 129 | ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS, |
| 130 | const MemoryLocation &Loc) { |
| 131 | ModRefInfo Result = MRI_ModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 132 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 133 | for (const auto &AA : AAs) { |
| 134 | Result = ModRefInfo(Result & AA->getModRefInfo(CS, Loc)); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 135 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 136 | // Early-exit the moment we reach the bottom of the lattice. |
| 137 | if (Result == MRI_NoModRef) |
| 138 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 141 | // Try to refine the mod-ref info further using other API entry points to the |
| 142 | // aggregate set of AA results. |
| 143 | auto MRB = getModRefBehavior(CS); |
Andrew Kaylor | 9604f34 | 2016-11-08 21:07:42 +0000 | [diff] [blame] | 144 | if (MRB == FMRB_DoesNotAccessMemory || |
| 145 | MRB == FMRB_OnlyAccessesInaccessibleMem) |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 146 | return MRI_NoModRef; |
| 147 | |
| 148 | if (onlyReadsMemory(MRB)) |
| 149 | Result = ModRefInfo(Result & MRI_Ref); |
Nicolai Haehnle | 84c9f99 | 2016-07-04 08:01:29 +0000 | [diff] [blame] | 150 | else if (doesNotReadMemory(MRB)) |
| 151 | Result = ModRefInfo(Result & MRI_Mod); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 152 | |
Andrew Kaylor | 9604f34 | 2016-11-08 21:07:42 +0000 | [diff] [blame] | 153 | if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 154 | bool DoesAlias = false; |
| 155 | ModRefInfo AllArgsMask = MRI_NoModRef; |
| 156 | if (doesAccessArgPointees(MRB)) { |
| 157 | for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) { |
| 158 | const Value *Arg = *AI; |
| 159 | if (!Arg->getType()->isPointerTy()) |
| 160 | continue; |
| 161 | unsigned ArgIdx = std::distance(CS.arg_begin(), AI); |
| 162 | MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, TLI); |
| 163 | AliasResult ArgAlias = alias(ArgLoc, Loc); |
| 164 | if (ArgAlias != NoAlias) { |
| 165 | ModRefInfo ArgMask = getArgModRefInfo(CS, ArgIdx); |
| 166 | DoesAlias = true; |
| 167 | AllArgsMask = ModRefInfo(AllArgsMask | ArgMask); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | if (!DoesAlias) |
| 172 | return MRI_NoModRef; |
| 173 | Result = ModRefInfo(Result & AllArgsMask); |
| 174 | } |
| 175 | |
| 176 | // If Loc is a constant memory location, the call definitely could not |
| 177 | // modify the memory location. |
| 178 | if ((Result & MRI_Mod) && |
| 179 | pointsToConstantMemory(Loc, /*OrLocal*/ false)) |
| 180 | Result = ModRefInfo(Result & ~MRI_Mod); |
| 181 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 182 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 185 | ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS1, |
| 186 | ImmutableCallSite CS2) { |
| 187 | ModRefInfo Result = MRI_ModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 188 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 189 | for (const auto &AA : AAs) { |
| 190 | Result = ModRefInfo(Result & AA->getModRefInfo(CS1, CS2)); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 191 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 192 | // Early-exit the moment we reach the bottom of the lattice. |
| 193 | if (Result == MRI_NoModRef) |
| 194 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 197 | // Try to refine the mod-ref info further using other API entry points to the |
| 198 | // aggregate set of AA results. |
| 199 | |
| 200 | // If CS1 or CS2 are readnone, they don't interact. |
| 201 | auto CS1B = getModRefBehavior(CS1); |
| 202 | if (CS1B == FMRB_DoesNotAccessMemory) |
| 203 | return MRI_NoModRef; |
| 204 | |
| 205 | auto CS2B = getModRefBehavior(CS2); |
| 206 | if (CS2B == FMRB_DoesNotAccessMemory) |
| 207 | return MRI_NoModRef; |
| 208 | |
| 209 | // If they both only read from memory, there is no dependence. |
| 210 | if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B)) |
| 211 | return MRI_NoModRef; |
| 212 | |
| 213 | // If CS1 only reads memory, the only dependence on CS2 can be |
| 214 | // from CS1 reading memory written by CS2. |
| 215 | if (onlyReadsMemory(CS1B)) |
| 216 | Result = ModRefInfo(Result & MRI_Ref); |
Nicolai Haehnle | 84c9f99 | 2016-07-04 08:01:29 +0000 | [diff] [blame] | 217 | else if (doesNotReadMemory(CS1B)) |
| 218 | Result = ModRefInfo(Result & MRI_Mod); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 219 | |
| 220 | // If CS2 only access memory through arguments, accumulate the mod/ref |
| 221 | // information from CS1's references to the memory referenced by |
| 222 | // CS2's arguments. |
| 223 | if (onlyAccessesArgPointees(CS2B)) { |
| 224 | ModRefInfo R = MRI_NoModRef; |
| 225 | if (doesAccessArgPointees(CS2B)) { |
| 226 | for (auto I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) { |
| 227 | const Value *Arg = *I; |
| 228 | if (!Arg->getType()->isPointerTy()) |
| 229 | continue; |
| 230 | unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I); |
| 231 | auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, TLI); |
| 232 | |
| 233 | // ArgMask indicates what CS2 might do to CS2ArgLoc, and the dependence |
| 234 | // of CS1 on that location is the inverse. |
| 235 | ModRefInfo ArgMask = getArgModRefInfo(CS2, CS2ArgIdx); |
| 236 | if (ArgMask == MRI_Mod) |
| 237 | ArgMask = MRI_ModRef; |
| 238 | else if (ArgMask == MRI_Ref) |
| 239 | ArgMask = MRI_Mod; |
| 240 | |
| 241 | ArgMask = ModRefInfo(ArgMask & getModRefInfo(CS1, CS2ArgLoc)); |
| 242 | |
| 243 | R = ModRefInfo((R | ArgMask) & Result); |
| 244 | if (R == Result) |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | return R; |
| 249 | } |
| 250 | |
| 251 | // If CS1 only accesses memory through arguments, check if CS2 references |
| 252 | // any of the memory referenced by CS1's arguments. If not, return NoModRef. |
| 253 | if (onlyAccessesArgPointees(CS1B)) { |
| 254 | ModRefInfo R = MRI_NoModRef; |
| 255 | if (doesAccessArgPointees(CS1B)) { |
| 256 | for (auto I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) { |
| 257 | const Value *Arg = *I; |
| 258 | if (!Arg->getType()->isPointerTy()) |
| 259 | continue; |
| 260 | unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I); |
| 261 | auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, TLI); |
| 262 | |
| 263 | // ArgMask indicates what CS1 might do to CS1ArgLoc; if CS1 might Mod |
| 264 | // CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If CS1 |
| 265 | // might Ref, then we care only about a Mod by CS2. |
| 266 | ModRefInfo ArgMask = getArgModRefInfo(CS1, CS1ArgIdx); |
| 267 | ModRefInfo ArgR = getModRefInfo(CS2, CS1ArgLoc); |
| 268 | if (((ArgMask & MRI_Mod) != MRI_NoModRef && |
| 269 | (ArgR & MRI_ModRef) != MRI_NoModRef) || |
| 270 | ((ArgMask & MRI_Ref) != MRI_NoModRef && |
| 271 | (ArgR & MRI_Mod) != MRI_NoModRef)) |
| 272 | R = ModRefInfo((R | ArgMask) & Result); |
| 273 | |
| 274 | if (R == Result) |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | return R; |
| 279 | } |
| 280 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 281 | return Result; |
| 282 | } |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 283 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 284 | FunctionModRefBehavior AAResults::getModRefBehavior(ImmutableCallSite CS) { |
| 285 | FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 286 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 287 | for (const auto &AA : AAs) { |
| 288 | Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(CS)); |
| 289 | |
| 290 | // Early-exit the moment we reach the bottom of the lattice. |
| 291 | if (Result == FMRB_DoesNotAccessMemory) |
| 292 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 295 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 298 | FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) { |
| 299 | FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 300 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 301 | for (const auto &AA : AAs) { |
| 302 | Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F)); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 303 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 304 | // Early-exit the moment we reach the bottom of the lattice. |
| 305 | if (Result == FMRB_DoesNotAccessMemory) |
| 306 | return Result; |
| 307 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 308 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 309 | return Result; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 312 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 313 | // Helper method implementation |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 314 | //===----------------------------------------------------------------------===// |
| 315 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 316 | ModRefInfo AAResults::getModRefInfo(const LoadInst *L, |
| 317 | const MemoryLocation &Loc) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 318 | // Be conservative in the face of volatile/atomic. |
| 319 | if (!L->isUnordered()) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 320 | return MRI_ModRef; |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 321 | |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 322 | // If the load address doesn't alias the given address, it doesn't read |
| 323 | // or write the specified memory. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 324 | if (Loc.Ptr && !alias(MemoryLocation::get(L), Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 325 | return MRI_NoModRef; |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 326 | |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 327 | // Otherwise, a load just reads. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 328 | return MRI_Ref; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 331 | ModRefInfo AAResults::getModRefInfo(const StoreInst *S, |
| 332 | const MemoryLocation &Loc) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 333 | // Be conservative in the face of volatile/atomic. |
| 334 | if (!S->isUnordered()) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 335 | return MRI_ModRef; |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 336 | |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 337 | if (Loc.Ptr) { |
| 338 | // If the store address cannot alias the pointer in question, then the |
| 339 | // specified memory cannot be modified by the store. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 340 | if (!alias(MemoryLocation::get(S), Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 341 | return MRI_NoModRef; |
Chris Lattner | 9605576 | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 342 | |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 343 | // If the pointer is a pointer to constant memory, then it could not have |
| 344 | // been modified by this store. |
| 345 | if (pointsToConstantMemory(Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 346 | return MRI_NoModRef; |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 347 | } |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 348 | |
| 349 | // Otherwise, a store just writes. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 350 | return MRI_Mod; |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 353 | ModRefInfo AAResults::getModRefInfo(const VAArgInst *V, |
| 354 | const MemoryLocation &Loc) { |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 355 | |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 356 | if (Loc.Ptr) { |
| 357 | // If the va_arg address cannot alias the pointer in question, then the |
| 358 | // specified memory cannot be accessed by the va_arg. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 359 | if (!alias(MemoryLocation::get(V), Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 360 | return MRI_NoModRef; |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 361 | |
| 362 | // If the pointer is a pointer to constant memory, then it could not have |
| 363 | // been modified by this va_arg. |
| 364 | if (pointsToConstantMemory(Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 365 | return MRI_NoModRef; |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 366 | } |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 367 | |
| 368 | // Otherwise, a va_arg reads and writes. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 369 | return MRI_ModRef; |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 370 | } |
| 371 | |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 372 | ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad, |
| 373 | const MemoryLocation &Loc) { |
| 374 | if (Loc.Ptr) { |
| 375 | // If the pointer is a pointer to constant memory, |
| 376 | // then it could not have been modified by this catchpad. |
| 377 | if (pointsToConstantMemory(Loc)) |
| 378 | return MRI_NoModRef; |
| 379 | } |
| 380 | |
| 381 | // Otherwise, a catchpad reads and writes. |
| 382 | return MRI_ModRef; |
| 383 | } |
| 384 | |
| 385 | ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet, |
| 386 | const MemoryLocation &Loc) { |
| 387 | if (Loc.Ptr) { |
| 388 | // If the pointer is a pointer to constant memory, |
| 389 | // then it could not have been modified by this catchpad. |
| 390 | if (pointsToConstantMemory(Loc)) |
| 391 | return MRI_NoModRef; |
| 392 | } |
| 393 | |
| 394 | // Otherwise, a catchret reads and writes. |
| 395 | return MRI_ModRef; |
| 396 | } |
| 397 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 398 | ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX, |
| 399 | const MemoryLocation &Loc) { |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 400 | // Acquire/Release cmpxchg has properties that matter for arbitrary addresses. |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 401 | if (isStrongerThanMonotonic(CX->getSuccessOrdering())) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 402 | return MRI_ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 403 | |
| 404 | // If the cmpxchg address does not alias the location, it does not access it. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 405 | if (Loc.Ptr && !alias(MemoryLocation::get(CX), Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 406 | return MRI_NoModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 407 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 408 | return MRI_ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 411 | ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW, |
| 412 | const MemoryLocation &Loc) { |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 413 | // Acquire/Release atomicrmw has properties that matter for arbitrary addresses. |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 414 | if (isStrongerThanMonotonic(RMW->getOrdering())) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 415 | return MRI_ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 416 | |
| 417 | // If the atomicrmw address does not alias the location, it does not access it. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 418 | if (Loc.Ptr && !alias(MemoryLocation::get(RMW), Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 419 | return MRI_NoModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 420 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 421 | return MRI_ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 424 | /// \brief Return information about whether a particular call site modifies |
| 425 | /// or reads the specified memory location \p MemLoc before instruction \p I |
| 426 | /// in a BasicBlock. A ordered basic block \p OBB can be used to speed up |
| 427 | /// instruction-ordering queries inside the BasicBlock containing \p I. |
| 428 | /// FIXME: this is really just shoring-up a deficiency in alias analysis. |
| 429 | /// BasicAA isn't willing to spend linear time determining whether an alloca |
| 430 | /// was captured before or after this particular call, while we are. However, |
| 431 | /// with a smarter AA in place, this test is just wasting compile time. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 432 | ModRefInfo AAResults::callCapturesBefore(const Instruction *I, |
| 433 | const MemoryLocation &MemLoc, |
| 434 | DominatorTree *DT, |
| 435 | OrderedBasicBlock *OBB) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 436 | if (!DT) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 437 | return MRI_ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 438 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 439 | const Value *Object = |
| 440 | GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout()); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 441 | if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) || |
| 442 | isa<Constant>(Object)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 443 | return MRI_ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 444 | |
| 445 | ImmutableCallSite CS(I); |
| 446 | if (!CS.getInstruction() || CS.getInstruction() == Object) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 447 | return MRI_ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 448 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 449 | if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true, |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 450 | /* StoreCaptures */ true, I, DT, |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 451 | /* include Object */ true, |
| 452 | /* OrderedBasicBlock */ OBB)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 453 | return MRI_ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 454 | |
| 455 | unsigned ArgNo = 0; |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 456 | ModRefInfo R = MRI_NoModRef; |
Sanjoy Das | d0bdf3e | 2016-06-13 19:55:04 +0000 | [diff] [blame] | 457 | for (auto CI = CS.data_operands_begin(), CE = CS.data_operands_end(); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 458 | CI != CE; ++CI, ++ArgNo) { |
| 459 | // Only look at the no-capture or byval pointer arguments. If this |
| 460 | // pointer were passed to arguments that were neither of these, then it |
| 461 | // couldn't be no-capture. |
| 462 | if (!(*CI)->getType()->isPointerTy() || |
| 463 | (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo))) |
| 464 | continue; |
| 465 | |
| 466 | // If this is a no-capture pointer argument, see if we can tell that it |
| 467 | // is impossible to alias the pointer we're checking. If not, we have to |
| 468 | // assume that the call could touch the pointer, even though it doesn't |
| 469 | // escape. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 470 | if (isNoAlias(MemoryLocation(*CI), MemoryLocation(Object))) |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 471 | continue; |
| 472 | if (CS.doesNotAccessMemory(ArgNo)) |
| 473 | continue; |
| 474 | if (CS.onlyReadsMemory(ArgNo)) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 475 | R = MRI_Ref; |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 476 | continue; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 477 | } |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 478 | return MRI_ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 479 | } |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 480 | return R; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 481 | } |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 482 | |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 483 | /// canBasicBlockModify - Return true if it is possible for execution of the |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 484 | /// specified basic block to modify the location Loc. |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 485 | /// |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 486 | bool AAResults::canBasicBlockModify(const BasicBlock &BB, |
| 487 | const MemoryLocation &Loc) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 488 | return canInstructionRangeModRef(BB.front(), BB.back(), Loc, MRI_Mod); |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 491 | /// canInstructionRangeModRef - Return true if it is possible for the |
| 492 | /// execution of the specified instructions to mod\ref (according to the |
| 493 | /// mode) the location Loc. The instructions to consider are all |
| 494 | /// of the instructions in the range of [I1,I2] INCLUSIVE. |
Teresa Johnson | bbcf75e | 2015-05-13 15:04:14 +0000 | [diff] [blame] | 495 | /// I1 and I2 must be in the same basic block. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 496 | bool AAResults::canInstructionRangeModRef(const Instruction &I1, |
| 497 | const Instruction &I2, |
| 498 | const MemoryLocation &Loc, |
| 499 | const ModRefInfo Mode) { |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 500 | assert(I1.getParent() == I2.getParent() && |
| 501 | "Instructions not in same basic block!"); |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 502 | BasicBlock::const_iterator I = I1.getIterator(); |
| 503 | BasicBlock::const_iterator E = I2.getIterator(); |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 504 | ++E; // Convert from inclusive to exclusive range. |
| 505 | |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 506 | for (; I != E; ++I) // Check every instruction in range |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 507 | if (getModRefInfo(&*I, Loc) & Mode) |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 508 | return true; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 509 | return false; |
| 510 | } |
| 511 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 512 | // Provide a definition for the root virtual destructor. |
| 513 | AAResults::Concept::~Concept() {} |
| 514 | |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 515 | // Provide a definition for the static object used to identify passes. |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 516 | char AAManager::PassID; |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 517 | |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 518 | namespace { |
| 519 | /// A wrapper pass for external alias analyses. This just squirrels away the |
| 520 | /// callback used to run any analyses and register their results. |
| 521 | struct ExternalAAWrapperPass : ImmutablePass { |
| 522 | typedef std::function<void(Pass &, Function &, AAResults &)> CallbackT; |
| 523 | |
| 524 | CallbackT CB; |
| 525 | |
| 526 | static char ID; |
| 527 | |
| 528 | ExternalAAWrapperPass() : ImmutablePass(ID) { |
| 529 | initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 530 | } |
| 531 | explicit ExternalAAWrapperPass(CallbackT CB) |
| 532 | : ImmutablePass(ID), CB(std::move(CB)) { |
| 533 | initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 534 | } |
| 535 | |
| 536 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 537 | AU.setPreservesAll(); |
| 538 | } |
| 539 | }; |
| 540 | } |
| 541 | |
| 542 | char ExternalAAWrapperPass::ID = 0; |
| 543 | INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis", |
| 544 | false, true) |
| 545 | |
| 546 | ImmutablePass * |
| 547 | llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) { |
| 548 | return new ExternalAAWrapperPass(std::move(Callback)); |
| 549 | } |
| 550 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 551 | AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) { |
| 552 | initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 553 | } |
| 554 | |
| 555 | char AAResultsWrapperPass::ID = 0; |
| 556 | |
| 557 | INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa", |
| 558 | "Function Alias Analysis Results", false, true) |
| 559 | INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass) |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 560 | INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass) |
| 561 | INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass) |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 562 | INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 563 | INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) |
| 564 | INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass) |
| 565 | INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass) |
| 566 | INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass) |
| 567 | INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass) |
| 568 | INITIALIZE_PASS_END(AAResultsWrapperPass, "aa", |
| 569 | "Function Alias Analysis Results", false, true) |
| 570 | |
| 571 | FunctionPass *llvm::createAAResultsWrapperPass() { |
| 572 | return new AAResultsWrapperPass(); |
| 573 | } |
| 574 | |
| 575 | /// Run the wrapper pass to rebuild an aggregation over known AA passes. |
| 576 | /// |
| 577 | /// This is the legacy pass manager's interface to the new-style AA results |
| 578 | /// aggregation object. Because this is somewhat shoe-horned into the legacy |
| 579 | /// pass manager, we hard code all the specific alias analyses available into |
| 580 | /// it. While the particular set enabled is configured via commandline flags, |
| 581 | /// adding a new alias analysis to LLVM will require adding support for it to |
| 582 | /// this list. |
| 583 | bool AAResultsWrapperPass::runOnFunction(Function &F) { |
| 584 | // NB! This *must* be reset before adding new AA results to the new |
| 585 | // AAResults object because in the legacy pass manager, each instance |
| 586 | // of these will refer to the *same* immutable analyses, registering and |
| 587 | // unregistering themselves with them. We need to carefully tear down the |
| 588 | // previous object first, in this case replacing it with an empty one, before |
| 589 | // registering new results. |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 590 | AAR.reset( |
| 591 | new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI())); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 592 | |
| 593 | // BasicAA is always available for function analyses. Also, we add it first |
| 594 | // so that it can trump TBAA results when it proves MustAlias. |
| 595 | // FIXME: TBAA should have an explicit mode to support this and then we |
| 596 | // should reconsider the ordering here. |
| 597 | if (!DisableBasicAA) |
| 598 | AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult()); |
| 599 | |
| 600 | // Populate the results with the currently available AAs. |
| 601 | if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>()) |
| 602 | AAR->addAAResult(WrapperPass->getResult()); |
| 603 | if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>()) |
| 604 | AAR->addAAResult(WrapperPass->getResult()); |
| 605 | if (auto *WrapperPass = |
| 606 | getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>()) |
| 607 | AAR->addAAResult(WrapperPass->getResult()); |
| 608 | if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>()) |
| 609 | AAR->addAAResult(WrapperPass->getResult()); |
| 610 | if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>()) |
| 611 | AAR->addAAResult(WrapperPass->getResult()); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 612 | if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>()) |
| 613 | AAR->addAAResult(WrapperPass->getResult()); |
| 614 | if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>()) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 615 | AAR->addAAResult(WrapperPass->getResult()); |
| 616 | |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 617 | // If available, run an external AA providing callback over the results as |
| 618 | // well. |
| 619 | if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>()) |
| 620 | if (WrapperPass->CB) |
| 621 | WrapperPass->CB(*this, F, *AAR); |
| 622 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 623 | // Analyses don't mutate the IR, so return false. |
| 624 | return false; |
| 625 | } |
| 626 | |
| 627 | void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 628 | AU.setPreservesAll(); |
| 629 | AU.addRequired<BasicAAWrapperPass>(); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 630 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 631 | |
| 632 | // We also need to mark all the alias analysis passes we will potentially |
| 633 | // probe in runOnFunction as used here to ensure the legacy pass manager |
| 634 | // preserves them. This hard coding of lists of alias analyses is specific to |
| 635 | // the legacy pass manager. |
| 636 | AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>(); |
| 637 | AU.addUsedIfAvailable<TypeBasedAAWrapperPass>(); |
| 638 | AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>(); |
| 639 | AU.addUsedIfAvailable<GlobalsAAWrapperPass>(); |
| 640 | AU.addUsedIfAvailable<SCEVAAWrapperPass>(); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 641 | AU.addUsedIfAvailable<CFLAndersAAWrapperPass>(); |
| 642 | AU.addUsedIfAvailable<CFLSteensAAWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F, |
| 646 | BasicAAResult &BAR) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 647 | AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 648 | |
| 649 | // Add in our explicitly constructed BasicAA results. |
| 650 | if (!DisableBasicAA) |
| 651 | AAR.addAAResult(BAR); |
| 652 | |
| 653 | // Populate the results with the other currently available AAs. |
| 654 | if (auto *WrapperPass = |
| 655 | P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>()) |
| 656 | AAR.addAAResult(WrapperPass->getResult()); |
| 657 | if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>()) |
| 658 | AAR.addAAResult(WrapperPass->getResult()); |
| 659 | if (auto *WrapperPass = |
| 660 | P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>()) |
| 661 | AAR.addAAResult(WrapperPass->getResult()); |
| 662 | if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>()) |
| 663 | AAR.addAAResult(WrapperPass->getResult()); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 664 | if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>()) |
| 665 | AAR.addAAResult(WrapperPass->getResult()); |
| 666 | if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>()) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 667 | AAR.addAAResult(WrapperPass->getResult()); |
| 668 | |
| 669 | return AAR; |
| 670 | } |
| 671 | |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 672 | bool llvm::isNoAliasCall(const Value *V) { |
Benjamin Kramer | 45f3954 | 2015-08-05 14:16:44 +0000 | [diff] [blame] | 673 | if (auto CS = ImmutableCallSite(V)) |
| 674 | return CS.paramHasAttr(0, Attribute::NoAlias); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 675 | return false; |
| 676 | } |
| 677 | |
Sanjay Patel | d7f613d | 2016-01-13 22:17:13 +0000 | [diff] [blame] | 678 | bool llvm::isNoAliasArgument(const Value *V) { |
Michael Kuperstein | f3e663a | 2013-05-28 08:17:48 +0000 | [diff] [blame] | 679 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 680 | return A->hasNoAliasAttr(); |
| 681 | return false; |
| 682 | } |
| 683 | |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 684 | bool llvm::isIdentifiedObject(const Value *V) { |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 685 | if (isa<AllocaInst>(V)) |
Dan Gohman | 1f59f01 | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 686 | return true; |
| 687 | if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 688 | return true; |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 689 | if (isNoAliasCall(V)) |
| 690 | return true; |
| 691 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 692 | return A->hasNoAliasAttr() || A->hasByValAttr(); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 693 | return false; |
| 694 | } |
Hal Finkel | c782aa5 | 2014-07-21 12:27:23 +0000 | [diff] [blame] | 695 | |
Sanjay Patel | d7f613d | 2016-01-13 22:17:13 +0000 | [diff] [blame] | 696 | bool llvm::isIdentifiedFunctionLocal(const Value *V) { |
Hal Finkel | c782aa5 | 2014-07-21 12:27:23 +0000 | [diff] [blame] | 697 | return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V); |
| 698 | } |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 699 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 700 | void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) { |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 701 | // This function needs to be in sync with llvm::createLegacyPMAAResults -- if |
| 702 | // more alias analyses are added to llvm::createLegacyPMAAResults, they need |
| 703 | // to be added here also. |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 704 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 705 | AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>(); |
| 706 | AU.addUsedIfAvailable<TypeBasedAAWrapperPass>(); |
| 707 | AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>(); |
| 708 | AU.addUsedIfAvailable<GlobalsAAWrapperPass>(); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 709 | AU.addUsedIfAvailable<CFLAndersAAWrapperPass>(); |
| 710 | AU.addUsedIfAvailable<CFLSteensAAWrapperPass>(); |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 711 | } |