blob: 55df6671417887f02723764d61a443c57f31298f [file] [log] [blame]
Eugene Zelenko530851c2017-08-11 21:30:02 +00001//==- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation --==//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner7d58f8d2002-08-22 18:25:32 +00009//
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 Lattnerd6a2a992003-02-26 19:41:54 +000027#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000028#include "llvm/Analysis/BasicAliasAnalysis.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000029#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
30#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000032#include "llvm/Analysis/GlobalsModRef.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000033#include "llvm/Analysis/MemoryLocation.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000034#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
35#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
36#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000037#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000038#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chad Rosiera968caf2012-05-14 20:35:04 +000039#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000040#include "llvm/IR/Argument.h"
41#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/BasicBlock.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000043#include "llvm/IR/CallSite.h"
44#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/Instructions.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000046#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000047#include "llvm/IR/Type.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000048#include "llvm/IR/Value.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000049#include "llvm/Pass.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000050#include "llvm/Support/AtomicOrdering.h"
51#include "llvm/Support/Casting.h"
52#include "llvm/Support/CommandLine.h"
53#include <algorithm>
54#include <cassert>
55#include <functional>
56#include <iterator>
57
Chris Lattnera67dbd02004-03-15 04:07:29 +000058using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000059
Chandler Carruth7b560d42015-09-09 17:55:00 +000060/// Allow disabling BasicAA from the AA results. This is particularly useful
61/// when testing to isolate a single AA implementation.
62static cl::opt<bool> DisableBasicAA("disable-basicaa", cl::Hidden,
63 cl::init(false));
64
Chandler Carruth17c630a2016-12-27 08:44:39 +000065AAResults::AAResults(AAResults &&Arg)
66 : TLI(Arg.TLI), AAs(std::move(Arg.AAs)), AADeps(std::move(Arg.AADeps)) {
Chandler Carruth7b560d42015-09-09 17:55:00 +000067 for (auto &AA : AAs)
68 AA->setAAResults(this);
69}
70
Chandler Carruth7b560d42015-09-09 17:55:00 +000071AAResults::~AAResults() {
72// FIXME; It would be nice to at least clear out the pointers back to this
73// aggregation here, but we end up with non-nesting lifetimes in the legacy
74// pass manager that prevent this from working. In the legacy pass manager
75// we'll end up with dangling references here in some cases.
76#if 0
77 for (auto &AA : AAs)
78 AA->setAAResults(nullptr);
79#endif
80}
Chris Lattner7d58f8d2002-08-22 18:25:32 +000081
Chandler Carruth17c630a2016-12-27 08:44:39 +000082bool AAResults::invalidate(Function &F, const PreservedAnalyses &PA,
83 FunctionAnalysisManager::Invalidator &Inv) {
Chandler Carruth17c630a2016-12-27 08:44:39 +000084 // Check if the AA manager itself has been invalidated.
85 auto PAC = PA.getChecker<AAManager>();
86 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
87 return true; // The manager needs to be blown away, clear everything.
88
89 // Check all of the dependencies registered.
90 for (AnalysisKey *ID : AADeps)
91 if (Inv.invalidate(ID, F, PA))
92 return true;
93
94 // Everything we depend on is still fine, so are we. Nothing to invalidate.
95 return false;
96}
97
Chris Lattner62c37002004-05-23 21:15:48 +000098//===----------------------------------------------------------------------===//
99// Default chaining methods
100//===----------------------------------------------------------------------===//
101
Chandler Carruth7b560d42015-09-09 17:55:00 +0000102AliasResult AAResults::alias(const MemoryLocation &LocA,
103 const MemoryLocation &LocB) {
104 for (const auto &AA : AAs) {
105 auto Result = AA->alias(LocA, LocB);
106 if (Result != MayAlias)
107 return Result;
108 }
109 return MayAlias;
Chris Lattner62c37002004-05-23 21:15:48 +0000110}
111
Chandler Carruth7b560d42015-09-09 17:55:00 +0000112bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
113 bool OrLocal) {
114 for (const auto &AA : AAs)
115 if (AA->pointsToConstantMemory(Loc, OrLocal))
116 return true;
117
118 return false;
Chris Lattner62c37002004-05-23 21:15:48 +0000119}
120
Chandler Carruth7b560d42015-09-09 17:55:00 +0000121ModRefInfo AAResults::getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000122 ModRefInfo Result = ModRefInfo::ModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000123
124 for (const auto &AA : AAs) {
Alina Sbirlea63d22502017-12-05 20:12:23 +0000125 Result = intersectModRef(Result, AA->getArgModRefInfo(CS, ArgIdx));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000126
127 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000128 if (isNoModRef(Result))
Chandler Carruth7b560d42015-09-09 17:55:00 +0000129 return Result;
130 }
131
132 return Result;
Hal Finkel354e23b2014-07-17 01:28:25 +0000133}
134
Chandler Carruth7b560d42015-09-09 17:55:00 +0000135ModRefInfo AAResults::getModRefInfo(Instruction *I, ImmutableCallSite Call) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000136 // We may have two calls.
Daniel Berlin8de312d2015-04-13 23:25:41 +0000137 if (auto CS = ImmutableCallSite(I)) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000138 // Check if the two calls modify the same memory.
Nicolai Haehnle9343f362016-07-11 14:11:45 +0000139 return getModRefInfo(CS, Call);
David Majnemera940f362016-07-15 17:19:24 +0000140 } else if (I->isFenceLike()) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000141 // If this is a fence, just return ModRef.
142 return ModRefInfo::ModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000143 } else {
144 // Otherwise, check if the call modifies or references the
145 // location this memory access defines. The best we can say
146 // is that if the call references what this instruction
147 // defines, it must be clobbered by this location.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000148 const MemoryLocation DefLoc = MemoryLocation::get(I);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000149 ModRefInfo MR = getModRefInfo(Call, DefLoc);
150 if (isModOrRefSet(MR))
151 return setModAndRef(MR);
Daniel Berlin8de312d2015-04-13 23:25:41 +0000152 }
Alina Sbirlea193429f2017-12-07 22:41:34 +0000153 return ModRefInfo::NoModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000154}
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000155
Chandler Carruth7b560d42015-09-09 17:55:00 +0000156ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS,
157 const MemoryLocation &Loc) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000158 ModRefInfo Result = ModRefInfo::ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000159
Chandler Carruth7b560d42015-09-09 17:55:00 +0000160 for (const auto &AA : AAs) {
Alina Sbirlea63d22502017-12-05 20:12:23 +0000161 Result = intersectModRef(Result, AA->getModRefInfo(CS, Loc));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000162
Chandler Carruth7b560d42015-09-09 17:55:00 +0000163 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000164 if (isNoModRef(Result))
Chandler Carruth7b560d42015-09-09 17:55:00 +0000165 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000166 }
167
Chandler Carruth12884f72016-03-02 15:56:53 +0000168 // Try to refine the mod-ref info further using other API entry points to the
169 // aggregate set of AA results.
170 auto MRB = getModRefBehavior(CS);
Andrew Kaylor9604f342016-11-08 21:07:42 +0000171 if (MRB == FMRB_DoesNotAccessMemory ||
172 MRB == FMRB_OnlyAccessesInaccessibleMem)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000173 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000174
175 if (onlyReadsMemory(MRB))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000176 Result = clearMod(Result);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000177 else if (doesNotReadMemory(MRB))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000178 Result = clearRef(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000179
Andrew Kaylor9604f342016-11-08 21:07:42 +0000180 if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000181 bool DoesAlias = false;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000182 bool IsMustAlias = true;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000183 ModRefInfo AllArgsMask = ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000184 if (doesAccessArgPointees(MRB)) {
185 for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) {
186 const Value *Arg = *AI;
187 if (!Arg->getType()->isPointerTy())
188 continue;
189 unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
190 MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, TLI);
191 AliasResult ArgAlias = alias(ArgLoc, Loc);
192 if (ArgAlias != NoAlias) {
193 ModRefInfo ArgMask = getArgModRefInfo(CS, ArgIdx);
194 DoesAlias = true;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000195 AllArgsMask = unionModRef(AllArgsMask, ArgMask);
Chandler Carruth12884f72016-03-02 15:56:53 +0000196 }
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000197 // Conservatively clear IsMustAlias unless only MustAlias is found.
198 IsMustAlias &= (ArgAlias == MustAlias);
Chandler Carruth12884f72016-03-02 15:56:53 +0000199 }
200 }
Alina Sbirlea193429f2017-12-07 22:41:34 +0000201 // Return NoModRef if no alias found with any argument.
Chandler Carruth12884f72016-03-02 15:56:53 +0000202 if (!DoesAlias)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000203 return ModRefInfo::NoModRef;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000204 // Logical & between other AA analyses and argument analysis.
205 Result = intersectModRef(Result, AllArgsMask);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000206 // If only MustAlias found above, set Must bit.
207 Result = IsMustAlias ? setMust(Result) : clearMust(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000208 }
209
210 // If Loc is a constant memory location, the call definitely could not
211 // modify the memory location.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000212 if (isModSet(Result) && pointsToConstantMemory(Loc, /*OrLocal*/ false))
213 Result = clearMod(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000214
Chandler Carruth7b560d42015-09-09 17:55:00 +0000215 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000216}
217
Chandler Carruth7b560d42015-09-09 17:55:00 +0000218ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS1,
219 ImmutableCallSite CS2) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000220 ModRefInfo Result = ModRefInfo::ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000221
Chandler Carruth7b560d42015-09-09 17:55:00 +0000222 for (const auto &AA : AAs) {
Alina Sbirlea63d22502017-12-05 20:12:23 +0000223 Result = intersectModRef(Result, AA->getModRefInfo(CS1, CS2));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000224
Chandler Carruth7b560d42015-09-09 17:55:00 +0000225 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000226 if (isNoModRef(Result))
Chandler Carruth7b560d42015-09-09 17:55:00 +0000227 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000228 }
229
Chandler Carruth12884f72016-03-02 15:56:53 +0000230 // Try to refine the mod-ref info further using other API entry points to the
231 // aggregate set of AA results.
232
233 // If CS1 or CS2 are readnone, they don't interact.
234 auto CS1B = getModRefBehavior(CS1);
235 if (CS1B == FMRB_DoesNotAccessMemory)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000236 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000237
238 auto CS2B = getModRefBehavior(CS2);
239 if (CS2B == FMRB_DoesNotAccessMemory)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000240 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000241
242 // If they both only read from memory, there is no dependence.
243 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000244 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000245
246 // If CS1 only reads memory, the only dependence on CS2 can be
247 // from CS1 reading memory written by CS2.
248 if (onlyReadsMemory(CS1B))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000249 Result = clearMod(Result);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000250 else if (doesNotReadMemory(CS1B))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000251 Result = clearRef(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000252
253 // If CS2 only access memory through arguments, accumulate the mod/ref
254 // information from CS1's references to the memory referenced by
255 // CS2's arguments.
256 if (onlyAccessesArgPointees(CS2B)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000257 ModRefInfo R = ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000258 if (doesAccessArgPointees(CS2B)) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000259 bool IsMustAlias = true;
Chandler Carruth12884f72016-03-02 15:56:53 +0000260 for (auto I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
261 const Value *Arg = *I;
262 if (!Arg->getType()->isPointerTy())
263 continue;
264 unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I);
265 auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, TLI);
266
Alina Sbirlea63d22502017-12-05 20:12:23 +0000267 // ArgModRefCS2 indicates what CS2 might do to CS2ArgLoc, and the
268 // dependence of CS1 on that location is the inverse:
269 // - If CS2 modifies location, dependence exists if CS1 reads or writes.
270 // - If CS2 only reads location, dependence exists if CS1 writes.
271 ModRefInfo ArgModRefCS2 = getArgModRefInfo(CS2, CS2ArgIdx);
Alina Sbirlea193429f2017-12-07 22:41:34 +0000272 ModRefInfo ArgMask = ModRefInfo::NoModRef;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000273 if (isModSet(ArgModRefCS2))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000274 ArgMask = ModRefInfo::ModRef;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000275 else if (isRefSet(ArgModRefCS2))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000276 ArgMask = ModRefInfo::Mod;
Chandler Carruth12884f72016-03-02 15:56:53 +0000277
Alina Sbirlea63d22502017-12-05 20:12:23 +0000278 // ModRefCS1 indicates what CS1 might do to CS2ArgLoc, and we use
279 // above ArgMask to update dependence info.
280 ModRefInfo ModRefCS1 = getModRefInfo(CS1, CS2ArgLoc);
281 ArgMask = intersectModRef(ArgMask, ModRefCS1);
Chandler Carruth12884f72016-03-02 15:56:53 +0000282
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000283 // Conservatively clear IsMustAlias unless only MustAlias is found.
284 IsMustAlias &= isMustSet(ModRefCS1);
285
Alina Sbirlea63d22502017-12-05 20:12:23 +0000286 R = intersectModRef(unionModRef(R, ArgMask), Result);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000287 if (R == Result) {
288 // On early exit, not all args were checked, cannot set Must.
289 if (I + 1 != E)
290 IsMustAlias = false;
Chandler Carruth12884f72016-03-02 15:56:53 +0000291 break;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000292 }
Chandler Carruth12884f72016-03-02 15:56:53 +0000293 }
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000294 // If Alias found and only MustAlias found above, set Must bit.
295 R = IsMustAlias ? setMust(R) : clearMust(R);
Chandler Carruth12884f72016-03-02 15:56:53 +0000296 }
297 return R;
298 }
299
300 // If CS1 only accesses memory through arguments, check if CS2 references
301 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
302 if (onlyAccessesArgPointees(CS1B)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000303 ModRefInfo R = ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000304 if (doesAccessArgPointees(CS1B)) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000305 bool IsMustAlias = true;
Chandler Carruth12884f72016-03-02 15:56:53 +0000306 for (auto I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
307 const Value *Arg = *I;
308 if (!Arg->getType()->isPointerTy())
309 continue;
310 unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I);
311 auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, TLI);
312
Alina Sbirlea63d22502017-12-05 20:12:23 +0000313 // ArgModRefCS1 indicates what CS1 might do to CS1ArgLoc; if CS1 might
314 // Mod CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If
315 // CS1 might Ref, then we care only about a Mod by CS2.
316 ModRefInfo ArgModRefCS1 = getArgModRefInfo(CS1, CS1ArgIdx);
317 ModRefInfo ModRefCS2 = getModRefInfo(CS2, CS1ArgLoc);
318 if ((isModSet(ArgModRefCS1) && isModOrRefSet(ModRefCS2)) ||
319 (isRefSet(ArgModRefCS1) && isModSet(ModRefCS2)))
320 R = intersectModRef(unionModRef(R, ArgModRefCS1), Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000321
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000322 // Conservatively clear IsMustAlias unless only MustAlias is found.
323 IsMustAlias &= isMustSet(ModRefCS2);
324
325 if (R == Result) {
326 // On early exit, not all args were checked, cannot set Must.
327 if (I + 1 != E)
328 IsMustAlias = false;
Chandler Carruth12884f72016-03-02 15:56:53 +0000329 break;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000330 }
Chandler Carruth12884f72016-03-02 15:56:53 +0000331 }
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000332 // If Alias found and only MustAlias found above, set Must bit.
333 R = IsMustAlias ? setMust(R) : clearMust(R);
Chandler Carruth12884f72016-03-02 15:56:53 +0000334 }
335 return R;
336 }
337
Chandler Carruth7b560d42015-09-09 17:55:00 +0000338 return Result;
339}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000340
Chandler Carruth7b560d42015-09-09 17:55:00 +0000341FunctionModRefBehavior AAResults::getModRefBehavior(ImmutableCallSite CS) {
342 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Hal Finkel354e23b2014-07-17 01:28:25 +0000343
Chandler Carruth7b560d42015-09-09 17:55:00 +0000344 for (const auto &AA : AAs) {
345 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(CS));
346
347 // Early-exit the moment we reach the bottom of the lattice.
348 if (Result == FMRB_DoesNotAccessMemory)
349 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000350 }
351
Chandler Carruth7b560d42015-09-09 17:55:00 +0000352 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000353}
354
Chandler Carruth7b560d42015-09-09 17:55:00 +0000355FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
356 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000357
Chandler Carruth7b560d42015-09-09 17:55:00 +0000358 for (const auto &AA : AAs) {
359 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000360
Chandler Carruth7b560d42015-09-09 17:55:00 +0000361 // Early-exit the moment we reach the bottom of the lattice.
362 if (Result == FMRB_DoesNotAccessMemory)
363 return Result;
364 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000365
Chandler Carruth7b560d42015-09-09 17:55:00 +0000366 return Result;
Chris Lattner62c37002004-05-23 21:15:48 +0000367}
368
Chris Lattner62c37002004-05-23 21:15:48 +0000369//===----------------------------------------------------------------------===//
Chandler Carruth7b560d42015-09-09 17:55:00 +0000370// Helper method implementation
Chris Lattner62c37002004-05-23 21:15:48 +0000371//===----------------------------------------------------------------------===//
372
Chandler Carruth7b560d42015-09-09 17:55:00 +0000373ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
374 const MemoryLocation &Loc) {
Daniel Berlind952cea2017-04-07 01:28:36 +0000375 // Be conservative in the face of atomic.
376 if (isStrongerThan(L->getOrdering(), AtomicOrdering::Unordered))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000377 return ModRefInfo::ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000378
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000379 // If the load address doesn't alias the given address, it doesn't read
380 // or write the specified memory.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000381 if (Loc.Ptr) {
382 AliasResult AR = alias(MemoryLocation::get(L), Loc);
383 if (AR == NoAlias)
384 return ModRefInfo::NoModRef;
385 if (AR == MustAlias)
386 return ModRefInfo::MustRef;
387 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000388 // Otherwise, a load just reads.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000389 return ModRefInfo::Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000390}
391
Chandler Carruth7b560d42015-09-09 17:55:00 +0000392ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
393 const MemoryLocation &Loc) {
Daniel Berlind952cea2017-04-07 01:28:36 +0000394 // Be conservative in the face of atomic.
395 if (isStrongerThan(S->getOrdering(), AtomicOrdering::Unordered))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000396 return ModRefInfo::ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000397
Daniel Berlinb2d22762015-04-13 23:05:45 +0000398 if (Loc.Ptr) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000399 AliasResult AR = alias(MemoryLocation::get(S), Loc);
Daniel Berlinb2d22762015-04-13 23:05:45 +0000400 // If the store address cannot alias the pointer in question, then the
401 // specified memory cannot be modified by the store.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000402 if (AR == NoAlias)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000403 return ModRefInfo::NoModRef;
Chris Lattner96055762004-01-30 22:16:42 +0000404
Daniel Berlinb2d22762015-04-13 23:05:45 +0000405 // If the pointer is a pointer to constant memory, then it could not have
406 // been modified by this store.
407 if (pointsToConstantMemory(Loc))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000408 return ModRefInfo::NoModRef;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000409
410 // If the store address aliases the pointer as must alias, set Must.
411 if (AR == MustAlias)
412 return ModRefInfo::MustMod;
Daniel Berlinb2d22762015-04-13 23:05:45 +0000413 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000414
415 // Otherwise, a store just writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000416 return ModRefInfo::Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000417}
418
Anna Thomas698f0de2017-01-20 00:21:33 +0000419ModRefInfo AAResults::getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) {
420 // If we know that the location is a constant memory location, the fence
421 // cannot modify this location.
422 if (Loc.Ptr && pointsToConstantMemory(Loc))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000423 return ModRefInfo::Ref;
424 return ModRefInfo::ModRef;
Anna Thomas698f0de2017-01-20 00:21:33 +0000425}
426
Chandler Carruth7b560d42015-09-09 17:55:00 +0000427ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
428 const MemoryLocation &Loc) {
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000429 if (Loc.Ptr) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000430 AliasResult AR = alias(MemoryLocation::get(V), Loc);
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000431 // If the va_arg address cannot alias the pointer in question, then the
432 // specified memory cannot be accessed by the va_arg.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000433 if (AR == NoAlias)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000434 return ModRefInfo::NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000435
436 // If the pointer is a pointer to constant memory, then it could not have
437 // been modified by this va_arg.
438 if (pointsToConstantMemory(Loc))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000439 return ModRefInfo::NoModRef;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000440
441 // If the va_arg aliases the pointer as must alias, set Must.
442 if (AR == MustAlias)
443 return ModRefInfo::MustModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000444 }
Dan Gohmane68958f2010-08-06 18:24:38 +0000445
446 // Otherwise, a va_arg reads and writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000447 return ModRefInfo::ModRef;
Dan Gohmane68958f2010-08-06 18:24:38 +0000448}
449
David Majnemer6727c012015-11-17 08:15:14 +0000450ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
451 const MemoryLocation &Loc) {
452 if (Loc.Ptr) {
453 // If the pointer is a pointer to constant memory,
454 // then it could not have been modified by this catchpad.
455 if (pointsToConstantMemory(Loc))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000456 return ModRefInfo::NoModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000457 }
458
459 // Otherwise, a catchpad reads and writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000460 return ModRefInfo::ModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000461}
462
463ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
464 const MemoryLocation &Loc) {
465 if (Loc.Ptr) {
466 // If the pointer is a pointer to constant memory,
467 // then it could not have been modified by this catchpad.
468 if (pointsToConstantMemory(Loc))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000469 return ModRefInfo::NoModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000470 }
471
472 // Otherwise, a catchret reads and writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000473 return ModRefInfo::ModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000474}
475
Chandler Carruth7b560d42015-09-09 17:55:00 +0000476ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
477 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000478 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000479 if (isStrongerThanMonotonic(CX->getSuccessOrdering()))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000480 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000481
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000482 if (Loc.Ptr) {
483 AliasResult AR = alias(MemoryLocation::get(CX), Loc);
484 // If the cmpxchg address does not alias the location, it does not access
485 // it.
486 if (AR == NoAlias)
487 return ModRefInfo::NoModRef;
488
489 // If the cmpxchg address aliases the pointer as must alias, set Must.
490 if (AR == MustAlias)
491 return ModRefInfo::MustModRef;
492 }
Eli Friedman5c918912011-09-26 20:15:28 +0000493
Alina Sbirlea193429f2017-12-07 22:41:34 +0000494 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000495}
496
Chandler Carruth7b560d42015-09-09 17:55:00 +0000497ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
498 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000499 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000500 if (isStrongerThanMonotonic(RMW->getOrdering()))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000501 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000502
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000503 if (Loc.Ptr) {
504 AliasResult AR = alias(MemoryLocation::get(RMW), Loc);
505 // If the atomicrmw address does not alias the location, it does not access
506 // it.
507 if (AR == NoAlias)
508 return ModRefInfo::NoModRef;
509
510 // If the atomicrmw address aliases the pointer as must alias, set Must.
511 if (AR == MustAlias)
512 return ModRefInfo::MustModRef;
513 }
Eli Friedman5c918912011-09-26 20:15:28 +0000514
Alina Sbirlea193429f2017-12-07 22:41:34 +0000515 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000516}
517
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000518/// \brief Return information about whether a particular call site modifies
519/// or reads the specified memory location \p MemLoc before instruction \p I
Alina Sbirlea63d22502017-12-05 20:12:23 +0000520/// in a BasicBlock. An ordered basic block \p OBB can be used to speed up
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000521/// instruction-ordering queries inside the BasicBlock containing \p I.
522/// FIXME: this is really just shoring-up a deficiency in alias analysis.
523/// BasicAA isn't willing to spend linear time determining whether an alloca
524/// was captured before or after this particular call, while we are. However,
525/// with a smarter AA in place, this test is just wasting compile time.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000526ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
527 const MemoryLocation &MemLoc,
528 DominatorTree *DT,
529 OrderedBasicBlock *OBB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000530 if (!DT)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000531 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000532
Chandler Carruth7b560d42015-09-09 17:55:00 +0000533 const Value *Object =
534 GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout());
Chad Rosiera968caf2012-05-14 20:35:04 +0000535 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
536 isa<Constant>(Object))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000537 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000538
539 ImmutableCallSite CS(I);
540 if (!CS.getInstruction() || CS.getInstruction() == Object)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000541 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000542
Eugene Zelenko530851c2017-08-11 21:30:02 +0000543 if (PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
544 /* StoreCaptures */ true, I, DT,
545 /* include Object */ true,
546 /* OrderedBasicBlock */ OBB))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000547 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000548
549 unsigned ArgNo = 0;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000550 ModRefInfo R = ModRefInfo::NoModRef;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000551 bool MustAlias = true;
552 // Set flag only if no May found and all operands processed.
Sanjoy Dasd0bdf3e2016-06-13 19:55:04 +0000553 for (auto CI = CS.data_operands_begin(), CE = CS.data_operands_end();
Chad Rosiera968caf2012-05-14 20:35:04 +0000554 CI != CE; ++CI, ++ArgNo) {
555 // Only look at the no-capture or byval pointer arguments. If this
556 // pointer were passed to arguments that were neither of these, then it
557 // couldn't be no-capture.
558 if (!(*CI)->getType()->isPointerTy() ||
Hal Finkel39fed392016-12-15 05:09:15 +0000559 (!CS.doesNotCapture(ArgNo) &&
560 ArgNo < CS.getNumArgOperands() && !CS.isByValArgument(ArgNo)))
Chad Rosiera968caf2012-05-14 20:35:04 +0000561 continue;
562
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000563 AliasResult AR = alias(MemoryLocation(*CI), MemoryLocation(Object));
Chad Rosiera968caf2012-05-14 20:35:04 +0000564 // If this is a no-capture pointer argument, see if we can tell that it
565 // is impossible to alias the pointer we're checking. If not, we have to
566 // assume that the call could touch the pointer, even though it doesn't
567 // escape.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000568 if (AR != MustAlias)
569 MustAlias = false;
570 if (AR == NoAlias)
Nick Lewyckyc0514622013-07-07 10:15:16 +0000571 continue;
Hal Finkelf19e1142016-12-15 05:50:45 +0000572 if (CS.doesNotAccessMemory(ArgNo))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000573 continue;
Hal Finkelf19e1142016-12-15 05:50:45 +0000574 if (CS.onlyReadsMemory(ArgNo)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000575 R = ModRefInfo::Ref;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000576 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000577 }
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000578 // Not returning MustModRef since we have not seen all the arguments.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000579 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000580 }
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000581 return MustAlias ? setMust(R) : clearMust(R);
Chad Rosiera968caf2012-05-14 20:35:04 +0000582}
Eli Friedman5c918912011-09-26 20:15:28 +0000583
Chris Lattnerd922a842002-08-22 22:46:39 +0000584/// canBasicBlockModify - Return true if it is possible for execution of the
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000585/// specified basic block to modify the location Loc.
Chris Lattnerd922a842002-08-22 22:46:39 +0000586///
Chandler Carruth7b560d42015-09-09 17:55:00 +0000587bool AAResults::canBasicBlockModify(const BasicBlock &BB,
588 const MemoryLocation &Loc) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000589 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, ModRefInfo::Mod);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000590}
591
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000592/// canInstructionRangeModRef - Return true if it is possible for the
593/// execution of the specified instructions to mod\ref (according to the
594/// mode) the location Loc. The instructions to consider are all
595/// of the instructions in the range of [I1,I2] INCLUSIVE.
Teresa Johnsonbbcf75e2015-05-13 15:04:14 +0000596/// I1 and I2 must be in the same basic block.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000597bool AAResults::canInstructionRangeModRef(const Instruction &I1,
598 const Instruction &I2,
599 const MemoryLocation &Loc,
600 const ModRefInfo Mode) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000601 assert(I1.getParent() == I2.getParent() &&
602 "Instructions not in same basic block!");
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000603 BasicBlock::const_iterator I = I1.getIterator();
604 BasicBlock::const_iterator E = I2.getIterator();
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000605 ++E; // Convert from inclusive to exclusive range.
606
Chris Lattner3a311832003-02-26 19:26:51 +0000607 for (; I != E; ++I) // Check every instruction in range
Alina Sbirlea18fea012017-12-06 19:56:37 +0000608 if (isModOrRefSet(intersectModRef(getModRefInfo(&*I, Loc), Mode)))
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000609 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000610 return false;
611}
612
Chandler Carruth7b560d42015-09-09 17:55:00 +0000613// Provide a definition for the root virtual destructor.
Eugene Zelenko530851c2017-08-11 21:30:02 +0000614AAResults::Concept::~Concept() = default;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000615
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000616// Provide a definition for the static object used to identify passes.
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000617AnalysisKey AAManager::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000618
Chandler Carruth2be10752015-10-21 12:15:19 +0000619namespace {
Eugene Zelenko530851c2017-08-11 21:30:02 +0000620
Chandler Carruth2be10752015-10-21 12:15:19 +0000621/// A wrapper pass for external alias analyses. This just squirrels away the
622/// callback used to run any analyses and register their results.
623struct ExternalAAWrapperPass : ImmutablePass {
Eugene Zelenko530851c2017-08-11 21:30:02 +0000624 using CallbackT = std::function<void(Pass &, Function &, AAResults &)>;
Chandler Carruth2be10752015-10-21 12:15:19 +0000625
626 CallbackT CB;
627
628 static char ID;
629
630 ExternalAAWrapperPass() : ImmutablePass(ID) {
631 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
632 }
Eugene Zelenko530851c2017-08-11 21:30:02 +0000633
Chandler Carruth2be10752015-10-21 12:15:19 +0000634 explicit ExternalAAWrapperPass(CallbackT CB)
635 : ImmutablePass(ID), CB(std::move(CB)) {
636 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
637 }
638
639 void getAnalysisUsage(AnalysisUsage &AU) const override {
640 AU.setPreservesAll();
641 }
642};
Eugene Zelenko530851c2017-08-11 21:30:02 +0000643
644} // end anonymous namespace
Chandler Carruth2be10752015-10-21 12:15:19 +0000645
646char ExternalAAWrapperPass::ID = 0;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000647
Chandler Carruth2be10752015-10-21 12:15:19 +0000648INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
649 false, true)
650
651ImmutablePass *
652llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
653 return new ExternalAAWrapperPass(std::move(Callback));
654}
655
Chandler Carruth7b560d42015-09-09 17:55:00 +0000656AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
657 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
658}
659
660char AAResultsWrapperPass::ID = 0;
661
662INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
663 "Function Alias Analysis Results", false, true)
664INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
George Burgess IVbfa401e2016-07-06 00:26:41 +0000665INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass)
666INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass)
Chandler Carruth2be10752015-10-21 12:15:19 +0000667INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000668INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
669INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
670INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
671INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
672INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
673INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
674 "Function Alias Analysis Results", false, true)
675
676FunctionPass *llvm::createAAResultsWrapperPass() {
677 return new AAResultsWrapperPass();
678}
679
680/// Run the wrapper pass to rebuild an aggregation over known AA passes.
681///
682/// This is the legacy pass manager's interface to the new-style AA results
683/// aggregation object. Because this is somewhat shoe-horned into the legacy
684/// pass manager, we hard code all the specific alias analyses available into
685/// it. While the particular set enabled is configured via commandline flags,
686/// adding a new alias analysis to LLVM will require adding support for it to
687/// this list.
688bool AAResultsWrapperPass::runOnFunction(Function &F) {
689 // NB! This *must* be reset before adding new AA results to the new
690 // AAResults object because in the legacy pass manager, each instance
691 // of these will refer to the *same* immutable analyses, registering and
692 // unregistering themselves with them. We need to carefully tear down the
693 // previous object first, in this case replacing it with an empty one, before
694 // registering new results.
Chandler Carruth12884f72016-03-02 15:56:53 +0000695 AAR.reset(
696 new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000697
698 // BasicAA is always available for function analyses. Also, we add it first
699 // so that it can trump TBAA results when it proves MustAlias.
700 // FIXME: TBAA should have an explicit mode to support this and then we
701 // should reconsider the ordering here.
702 if (!DisableBasicAA)
703 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
704
705 // Populate the results with the currently available AAs.
706 if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
707 AAR->addAAResult(WrapperPass->getResult());
708 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
709 AAR->addAAResult(WrapperPass->getResult());
710 if (auto *WrapperPass =
711 getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
712 AAR->addAAResult(WrapperPass->getResult());
713 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
714 AAR->addAAResult(WrapperPass->getResult());
715 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
716 AAR->addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000717 if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
718 AAR->addAAResult(WrapperPass->getResult());
719 if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000720 AAR->addAAResult(WrapperPass->getResult());
721
Chandler Carruth2be10752015-10-21 12:15:19 +0000722 // If available, run an external AA providing callback over the results as
723 // well.
724 if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
725 if (WrapperPass->CB)
726 WrapperPass->CB(*this, F, *AAR);
727
Chandler Carruth7b560d42015-09-09 17:55:00 +0000728 // Analyses don't mutate the IR, so return false.
729 return false;
730}
731
732void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
733 AU.setPreservesAll();
734 AU.addRequired<BasicAAWrapperPass>();
Chandler Carruth12884f72016-03-02 15:56:53 +0000735 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000736
737 // We also need to mark all the alias analysis passes we will potentially
738 // probe in runOnFunction as used here to ensure the legacy pass manager
739 // preserves them. This hard coding of lists of alias analyses is specific to
740 // the legacy pass manager.
741 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
742 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
743 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
744 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
745 AU.addUsedIfAvailable<SCEVAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000746 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
747 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000748}
749
750AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
751 BasicAAResult &BAR) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000752 AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000753
754 // Add in our explicitly constructed BasicAA results.
755 if (!DisableBasicAA)
756 AAR.addAAResult(BAR);
757
758 // Populate the results with the other currently available AAs.
759 if (auto *WrapperPass =
760 P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
761 AAR.addAAResult(WrapperPass->getResult());
762 if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
763 AAR.addAAResult(WrapperPass->getResult());
764 if (auto *WrapperPass =
765 P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
766 AAR.addAAResult(WrapperPass->getResult());
767 if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
768 AAR.addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000769 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
770 AAR.addAAResult(WrapperPass->getResult());
771 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000772 AAR.addAAResult(WrapperPass->getResult());
773
774 return AAR;
775}
776
Dan Gohmanfb306c02009-02-03 01:28:32 +0000777bool llvm::isNoAliasCall(const Value *V) {
Benjamin Kramer45f39542015-08-05 14:16:44 +0000778 if (auto CS = ImmutableCallSite(V))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000779 return CS.hasRetAttr(Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000780 return false;
781}
782
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000783bool llvm::isNoAliasArgument(const Value *V) {
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000784 if (const Argument *A = dyn_cast<Argument>(V))
785 return A->hasNoAliasAttr();
786 return false;
787}
788
Dan Gohman00ef9322010-07-07 14:27:09 +0000789bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000790 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000791 return true;
792 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000793 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000794 if (isNoAliasCall(V))
795 return true;
796 if (const Argument *A = dyn_cast<Argument>(V))
797 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000798 return false;
799}
Hal Finkelc782aa52014-07-21 12:27:23 +0000800
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000801bool llvm::isIdentifiedFunctionLocal(const Value *V) {
Hal Finkelc782aa52014-07-21 12:27:23 +0000802 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
803}
Sanjoy Das1c481f52016-02-09 01:21:57 +0000804
Chandler Carruth12884f72016-03-02 15:56:53 +0000805void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
Sanjoy Das1c481f52016-02-09 01:21:57 +0000806 // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
807 // more alias analyses are added to llvm::createLegacyPMAAResults, they need
808 // to be added here also.
Chandler Carruth12884f72016-03-02 15:56:53 +0000809 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000810 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
811 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
812 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
813 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000814 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
815 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000816}