blob: c024e3a2c5b18120be1abf20bebf6a2364ba1cd4 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman01808ca2005-04-21 21:13:18 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner7d58f8d2002-08-22 18:25:32 +00008//
9// This file implements the generic AliasAnalysis interface which is used as the
10// common interface used by all clients and implementations of alias analysis.
11//
12// This file also implements the default version of the AliasAnalysis interface
13// that is to be used when no other implementation is specified. This does some
14// simple tests that detect obvious cases: two different global pointers cannot
15// alias, a global cannot alias a malloc, two different mallocs cannot alias,
16// etc.
17//
18// This alias analysis implementation really isn't very good for anything, but
19// it is very fast, and makes a nice clean default implementation. Because it
20// handles lots of little corner cases, other, more complex, alias analysis
21// implementations may choose to rely on this pass to resolve these simple and
22// easy cases.
23//
24//===----------------------------------------------------------------------===//
25
Chris Lattnerd6a2a992003-02-26 19:41:54 +000026#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000027#include "llvm/Analysis/BasicAliasAnalysis.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000028#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
29#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000031#include "llvm/Analysis/GlobalsModRef.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000032#include "llvm/Analysis/MemoryLocation.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000033#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
34#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
35#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000036#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000037#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chad Rosiera968caf2012-05-14 20:35:04 +000038#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000039#include "llvm/IR/Argument.h"
40#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/BasicBlock.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000042#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Instructions.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000044#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/Type.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000046#include "llvm/IR/Value.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Pass.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000048#include "llvm/Support/AtomicOrdering.h"
49#include "llvm/Support/Casting.h"
50#include "llvm/Support/CommandLine.h"
51#include <algorithm>
52#include <cassert>
53#include <functional>
54#include <iterator>
55
Chris Lattnera67dbd02004-03-15 04:07:29 +000056using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000057
Chandler Carruth7b560d42015-09-09 17:55:00 +000058/// Allow disabling BasicAA from the AA results. This is particularly useful
59/// when testing to isolate a single AA implementation.
60static cl::opt<bool> DisableBasicAA("disable-basicaa", cl::Hidden,
61 cl::init(false));
62
Chandler Carruth17c630a2016-12-27 08:44:39 +000063AAResults::AAResults(AAResults &&Arg)
64 : TLI(Arg.TLI), AAs(std::move(Arg.AAs)), AADeps(std::move(Arg.AADeps)) {
Chandler Carruth7b560d42015-09-09 17:55:00 +000065 for (auto &AA : AAs)
66 AA->setAAResults(this);
67}
68
Chandler Carruth7b560d42015-09-09 17:55:00 +000069AAResults::~AAResults() {
70// FIXME; It would be nice to at least clear out the pointers back to this
71// aggregation here, but we end up with non-nesting lifetimes in the legacy
72// pass manager that prevent this from working. In the legacy pass manager
73// we'll end up with dangling references here in some cases.
74#if 0
75 for (auto &AA : AAs)
76 AA->setAAResults(nullptr);
77#endif
78}
Chris Lattner7d58f8d2002-08-22 18:25:32 +000079
Chandler Carruth17c630a2016-12-27 08:44:39 +000080bool AAResults::invalidate(Function &F, const PreservedAnalyses &PA,
81 FunctionAnalysisManager::Invalidator &Inv) {
Chandler Carruth17c630a2016-12-27 08:44:39 +000082 // Check if the AA manager itself has been invalidated.
83 auto PAC = PA.getChecker<AAManager>();
84 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
85 return true; // The manager needs to be blown away, clear everything.
86
87 // Check all of the dependencies registered.
88 for (AnalysisKey *ID : AADeps)
89 if (Inv.invalidate(ID, F, PA))
90 return true;
91
92 // Everything we depend on is still fine, so are we. Nothing to invalidate.
93 return false;
94}
95
Chris Lattner62c37002004-05-23 21:15:48 +000096//===----------------------------------------------------------------------===//
97// Default chaining methods
98//===----------------------------------------------------------------------===//
99
Chandler Carruth7b560d42015-09-09 17:55:00 +0000100AliasResult AAResults::alias(const MemoryLocation &LocA,
101 const MemoryLocation &LocB) {
102 for (const auto &AA : AAs) {
103 auto Result = AA->alias(LocA, LocB);
104 if (Result != MayAlias)
105 return Result;
106 }
107 return MayAlias;
Chris Lattner62c37002004-05-23 21:15:48 +0000108}
109
Chandler Carruth7b560d42015-09-09 17:55:00 +0000110bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
111 bool OrLocal) {
112 for (const auto &AA : AAs)
113 if (AA->pointsToConstantMemory(Loc, OrLocal))
114 return true;
115
116 return false;
Chris Lattner62c37002004-05-23 21:15:48 +0000117}
118
Chandler Carruth363ac682019-01-07 05:42:51 +0000119ModRefInfo AAResults::getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000120 ModRefInfo Result = ModRefInfo::ModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000121
122 for (const auto &AA : AAs) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000123 Result = intersectModRef(Result, AA->getArgModRefInfo(Call, ArgIdx));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000124
125 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000126 if (isNoModRef(Result))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000127 return ModRefInfo::NoModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000128 }
129
130 return Result;
Hal Finkel354e23b2014-07-17 01:28:25 +0000131}
132
Chandler Carruth363ac682019-01-07 05:42:51 +0000133ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000134 // We may have two calls.
Chandler Carruth363ac682019-01-07 05:42:51 +0000135 if (const auto *Call1 = dyn_cast<CallBase>(I)) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000136 // Check if the two calls modify the same memory.
Chandler Carruth363ac682019-01-07 05:42:51 +0000137 return getModRefInfo(Call1, Call2);
David Majnemera940f362016-07-15 17:19:24 +0000138 } else if (I->isFenceLike()) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000139 // If this is a fence, just return ModRef.
140 return ModRefInfo::ModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000141 } else {
142 // Otherwise, check if the call modifies or references the
143 // location this memory access defines. The best we can say
144 // is that if the call references what this instruction
145 // defines, it must be clobbered by this location.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000146 const MemoryLocation DefLoc = MemoryLocation::get(I);
Chandler Carruth363ac682019-01-07 05:42:51 +0000147 ModRefInfo MR = getModRefInfo(Call2, DefLoc);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000148 if (isModOrRefSet(MR))
149 return setModAndRef(MR);
Daniel Berlin8de312d2015-04-13 23:25:41 +0000150 }
Alina Sbirlea193429f2017-12-07 22:41:34 +0000151 return ModRefInfo::NoModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000152}
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000153
Chandler Carruth363ac682019-01-07 05:42:51 +0000154ModRefInfo AAResults::getModRefInfo(const CallBase *Call,
Chandler Carruth7b560d42015-09-09 17:55:00 +0000155 const MemoryLocation &Loc) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000156 ModRefInfo Result = ModRefInfo::ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000157
Chandler Carruth7b560d42015-09-09 17:55:00 +0000158 for (const auto &AA : AAs) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000159 Result = intersectModRef(Result, AA->getModRefInfo(Call, Loc));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000160
Chandler Carruth7b560d42015-09-09 17:55:00 +0000161 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000162 if (isNoModRef(Result))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000163 return ModRefInfo::NoModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000164 }
165
Chandler Carruth12884f72016-03-02 15:56:53 +0000166 // Try to refine the mod-ref info further using other API entry points to the
167 // aggregate set of AA results.
Chandler Carruth363ac682019-01-07 05:42:51 +0000168 auto MRB = getModRefBehavior(Call);
Andrew Kaylor9604f342016-11-08 21:07:42 +0000169 if (MRB == FMRB_DoesNotAccessMemory ||
170 MRB == FMRB_OnlyAccessesInaccessibleMem)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000171 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000172
173 if (onlyReadsMemory(MRB))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000174 Result = clearMod(Result);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000175 else if (doesNotReadMemory(MRB))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000176 Result = clearRef(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000177
Andrew Kaylor9604f342016-11-08 21:07:42 +0000178 if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000179 bool IsMustAlias = true;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000180 ModRefInfo AllArgsMask = ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000181 if (doesAccessArgPointees(MRB)) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000182 for (auto AI = Call->arg_begin(), AE = Call->arg_end(); AI != AE; ++AI) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000183 const Value *Arg = *AI;
184 if (!Arg->getType()->isPointerTy())
185 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000186 unsigned ArgIdx = std::distance(Call->arg_begin(), AI);
187 MemoryLocation ArgLoc =
188 MemoryLocation::getForArgument(Call, ArgIdx, TLI);
Chandler Carruth12884f72016-03-02 15:56:53 +0000189 AliasResult ArgAlias = alias(ArgLoc, Loc);
190 if (ArgAlias != NoAlias) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000191 ModRefInfo ArgMask = getArgModRefInfo(Call, ArgIdx);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000192 AllArgsMask = unionModRef(AllArgsMask, ArgMask);
Chandler Carruth12884f72016-03-02 15:56:53 +0000193 }
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000194 // Conservatively clear IsMustAlias unless only MustAlias is found.
195 IsMustAlias &= (ArgAlias == MustAlias);
Chandler Carruth12884f72016-03-02 15:56:53 +0000196 }
197 }
Alina Sbirlea193429f2017-12-07 22:41:34 +0000198 // Return NoModRef if no alias found with any argument.
Philip Reames8abf4482018-08-22 19:50:45 +0000199 if (isNoModRef(AllArgsMask))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000200 return ModRefInfo::NoModRef;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000201 // Logical & between other AA analyses and argument analysis.
202 Result = intersectModRef(Result, AllArgsMask);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000203 // If only MustAlias found above, set Must bit.
204 Result = IsMustAlias ? setMust(Result) : clearMust(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000205 }
206
207 // If Loc is a constant memory location, the call definitely could not
208 // modify the memory location.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000209 if (isModSet(Result) && pointsToConstantMemory(Loc, /*OrLocal*/ false))
210 Result = clearMod(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000211
Chandler Carruth7b560d42015-09-09 17:55:00 +0000212 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000213}
214
Chandler Carruth363ac682019-01-07 05:42:51 +0000215ModRefInfo AAResults::getModRefInfo(const CallBase *Call1,
216 const CallBase *Call2) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000217 ModRefInfo Result = ModRefInfo::ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000218
Chandler Carruth7b560d42015-09-09 17:55:00 +0000219 for (const auto &AA : AAs) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000220 Result = intersectModRef(Result, AA->getModRefInfo(Call1, Call2));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000221
Chandler Carruth7b560d42015-09-09 17:55:00 +0000222 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000223 if (isNoModRef(Result))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000224 return ModRefInfo::NoModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000225 }
226
Chandler Carruth12884f72016-03-02 15:56:53 +0000227 // Try to refine the mod-ref info further using other API entry points to the
228 // aggregate set of AA results.
229
Chandler Carruth363ac682019-01-07 05:42:51 +0000230 // If Call1 or Call2 are readnone, they don't interact.
231 auto Call1B = getModRefBehavior(Call1);
232 if (Call1B == FMRB_DoesNotAccessMemory)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000233 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000234
Chandler Carruth363ac682019-01-07 05:42:51 +0000235 auto Call2B = getModRefBehavior(Call2);
236 if (Call2B == FMRB_DoesNotAccessMemory)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000237 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000238
239 // If they both only read from memory, there is no dependence.
Chandler Carruth363ac682019-01-07 05:42:51 +0000240 if (onlyReadsMemory(Call1B) && onlyReadsMemory(Call2B))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000241 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000242
Chandler Carruth363ac682019-01-07 05:42:51 +0000243 // If Call1 only reads memory, the only dependence on Call2 can be
244 // from Call1 reading memory written by Call2.
245 if (onlyReadsMemory(Call1B))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000246 Result = clearMod(Result);
Chandler Carruth363ac682019-01-07 05:42:51 +0000247 else if (doesNotReadMemory(Call1B))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000248 Result = clearRef(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000249
Chandler Carruth363ac682019-01-07 05:42:51 +0000250 // If Call2 only access memory through arguments, accumulate the mod/ref
251 // information from Call1's references to the memory referenced by
252 // Call2's arguments.
253 if (onlyAccessesArgPointees(Call2B)) {
254 if (!doesAccessArgPointees(Call2B))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000255 return ModRefInfo::NoModRef;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000256 ModRefInfo R = ModRefInfo::NoModRef;
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000257 bool IsMustAlias = true;
Chandler Carruth363ac682019-01-07 05:42:51 +0000258 for (auto I = Call2->arg_begin(), E = Call2->arg_end(); I != E; ++I) {
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000259 const Value *Arg = *I;
260 if (!Arg->getType()->isPointerTy())
261 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000262 unsigned Call2ArgIdx = std::distance(Call2->arg_begin(), I);
263 auto Call2ArgLoc =
264 MemoryLocation::getForArgument(Call2, Call2ArgIdx, TLI);
Chandler Carruth12884f72016-03-02 15:56:53 +0000265
Chandler Carruth363ac682019-01-07 05:42:51 +0000266 // ArgModRefC2 indicates what Call2 might do to Call2ArgLoc, and the
267 // dependence of Call1 on that location is the inverse:
268 // - If Call2 modifies location, dependence exists if Call1 reads or
269 // writes.
270 // - If Call2 only reads location, dependence exists if Call1 writes.
271 ModRefInfo ArgModRefC2 = getArgModRefInfo(Call2, Call2ArgIdx);
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000272 ModRefInfo ArgMask = ModRefInfo::NoModRef;
Chandler Carruth363ac682019-01-07 05:42:51 +0000273 if (isModSet(ArgModRefC2))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000274 ArgMask = ModRefInfo::ModRef;
Chandler Carruth363ac682019-01-07 05:42:51 +0000275 else if (isRefSet(ArgModRefC2))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000276 ArgMask = ModRefInfo::Mod;
Chandler Carruth12884f72016-03-02 15:56:53 +0000277
Chandler Carruth363ac682019-01-07 05:42:51 +0000278 // ModRefC1 indicates what Call1 might do to Call2ArgLoc, and we use
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000279 // above ArgMask to update dependence info.
Chandler Carruth363ac682019-01-07 05:42:51 +0000280 ModRefInfo ModRefC1 = getModRefInfo(Call1, Call2ArgLoc);
281 ArgMask = intersectModRef(ArgMask, ModRefC1);
Chandler Carruth12884f72016-03-02 15:56:53 +0000282
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000283 // Conservatively clear IsMustAlias unless only MustAlias is found.
Chandler Carruth363ac682019-01-07 05:42:51 +0000284 IsMustAlias &= isMustSet(ModRefC1);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000285
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000286 R = intersectModRef(unionModRef(R, ArgMask), Result);
287 if (R == Result) {
288 // On early exit, not all args were checked, cannot set Must.
289 if (I + 1 != E)
290 IsMustAlias = false;
291 break;
Chandler Carruth12884f72016-03-02 15:56:53 +0000292 }
293 }
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000294
295 if (isNoModRef(R))
296 return ModRefInfo::NoModRef;
297
298 // If MustAlias found above, set Must bit.
299 return IsMustAlias ? setMust(R) : clearMust(R);
Chandler Carruth12884f72016-03-02 15:56:53 +0000300 }
301
Chandler Carruth363ac682019-01-07 05:42:51 +0000302 // If Call1 only accesses memory through arguments, check if Call2 references
303 // any of the memory referenced by Call1's arguments. If not, return NoModRef.
304 if (onlyAccessesArgPointees(Call1B)) {
305 if (!doesAccessArgPointees(Call1B))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000306 return ModRefInfo::NoModRef;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000307 ModRefInfo R = ModRefInfo::NoModRef;
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000308 bool IsMustAlias = true;
Chandler Carruth363ac682019-01-07 05:42:51 +0000309 for (auto I = Call1->arg_begin(), E = Call1->arg_end(); I != E; ++I) {
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000310 const Value *Arg = *I;
311 if (!Arg->getType()->isPointerTy())
312 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000313 unsigned Call1ArgIdx = std::distance(Call1->arg_begin(), I);
314 auto Call1ArgLoc =
315 MemoryLocation::getForArgument(Call1, Call1ArgIdx, TLI);
Chandler Carruth12884f72016-03-02 15:56:53 +0000316
Chandler Carruth363ac682019-01-07 05:42:51 +0000317 // ArgModRefC1 indicates what Call1 might do to Call1ArgLoc; if Call1
318 // might Mod Call1ArgLoc, then we care about either a Mod or a Ref by
319 // Call2. If Call1 might Ref, then we care only about a Mod by Call2.
320 ModRefInfo ArgModRefC1 = getArgModRefInfo(Call1, Call1ArgIdx);
321 ModRefInfo ModRefC2 = getModRefInfo(Call2, Call1ArgLoc);
322 if ((isModSet(ArgModRefC1) && isModOrRefSet(ModRefC2)) ||
323 (isRefSet(ArgModRefC1) && isModSet(ModRefC2)))
324 R = intersectModRef(unionModRef(R, ArgModRefC1), Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000325
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000326 // Conservatively clear IsMustAlias unless only MustAlias is found.
Chandler Carruth363ac682019-01-07 05:42:51 +0000327 IsMustAlias &= isMustSet(ModRefC2);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000328
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000329 if (R == Result) {
330 // On early exit, not all args were checked, cannot set Must.
331 if (I + 1 != E)
332 IsMustAlias = false;
333 break;
Chandler Carruth12884f72016-03-02 15:56:53 +0000334 }
335 }
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000336
337 if (isNoModRef(R))
338 return ModRefInfo::NoModRef;
339
340 // If MustAlias found above, set Must bit.
341 return IsMustAlias ? setMust(R) : clearMust(R);
Chandler Carruth12884f72016-03-02 15:56:53 +0000342 }
343
Chandler Carruth7b560d42015-09-09 17:55:00 +0000344 return Result;
345}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000346
Chandler Carruth363ac682019-01-07 05:42:51 +0000347FunctionModRefBehavior AAResults::getModRefBehavior(const CallBase *Call) {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000348 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Hal Finkel354e23b2014-07-17 01:28:25 +0000349
Chandler Carruth7b560d42015-09-09 17:55:00 +0000350 for (const auto &AA : AAs) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000351 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(Call));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000352
353 // Early-exit the moment we reach the bottom of the lattice.
354 if (Result == FMRB_DoesNotAccessMemory)
355 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000356 }
357
Chandler Carruth7b560d42015-09-09 17:55:00 +0000358 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000359}
360
Chandler Carruth7b560d42015-09-09 17:55:00 +0000361FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
362 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000363
Chandler Carruth7b560d42015-09-09 17:55:00 +0000364 for (const auto &AA : AAs) {
365 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000366
Chandler Carruth7b560d42015-09-09 17:55:00 +0000367 // Early-exit the moment we reach the bottom of the lattice.
368 if (Result == FMRB_DoesNotAccessMemory)
369 return Result;
370 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000371
Chandler Carruth7b560d42015-09-09 17:55:00 +0000372 return Result;
Chris Lattner62c37002004-05-23 21:15:48 +0000373}
374
George Burgess IVaa283d82018-06-14 19:55:53 +0000375raw_ostream &llvm::operator<<(raw_ostream &OS, AliasResult AR) {
376 switch (AR) {
377 case NoAlias:
378 OS << "NoAlias";
379 break;
380 case MustAlias:
381 OS << "MustAlias";
382 break;
383 case MayAlias:
384 OS << "MayAlias";
385 break;
386 case PartialAlias:
387 OS << "PartialAlias";
388 break;
389 }
390 return OS;
391}
392
Chris Lattner62c37002004-05-23 21:15:48 +0000393//===----------------------------------------------------------------------===//
Chandler Carruth7b560d42015-09-09 17:55:00 +0000394// Helper method implementation
Chris Lattner62c37002004-05-23 21:15:48 +0000395//===----------------------------------------------------------------------===//
396
Chandler Carruth7b560d42015-09-09 17:55:00 +0000397ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
398 const MemoryLocation &Loc) {
Daniel Berlind952cea2017-04-07 01:28:36 +0000399 // Be conservative in the face of atomic.
400 if (isStrongerThan(L->getOrdering(), AtomicOrdering::Unordered))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000401 return ModRefInfo::ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000402
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000403 // If the load address doesn't alias the given address, it doesn't read
404 // or write the specified memory.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000405 if (Loc.Ptr) {
406 AliasResult AR = alias(MemoryLocation::get(L), Loc);
407 if (AR == NoAlias)
408 return ModRefInfo::NoModRef;
409 if (AR == MustAlias)
410 return ModRefInfo::MustRef;
411 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000412 // Otherwise, a load just reads.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000413 return ModRefInfo::Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000414}
415
Chandler Carruth7b560d42015-09-09 17:55:00 +0000416ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
417 const MemoryLocation &Loc) {
Daniel Berlind952cea2017-04-07 01:28:36 +0000418 // Be conservative in the face of atomic.
419 if (isStrongerThan(S->getOrdering(), AtomicOrdering::Unordered))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000420 return ModRefInfo::ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000421
Daniel Berlinb2d22762015-04-13 23:05:45 +0000422 if (Loc.Ptr) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000423 AliasResult AR = alias(MemoryLocation::get(S), Loc);
Daniel Berlinb2d22762015-04-13 23:05:45 +0000424 // If the store address cannot alias the pointer in question, then the
425 // specified memory cannot be modified by the store.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000426 if (AR == NoAlias)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000427 return ModRefInfo::NoModRef;
Chris Lattner96055762004-01-30 22:16:42 +0000428
Daniel Berlinb2d22762015-04-13 23:05:45 +0000429 // If the pointer is a pointer to constant memory, then it could not have
430 // been modified by this store.
431 if (pointsToConstantMemory(Loc))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000432 return ModRefInfo::NoModRef;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000433
434 // If the store address aliases the pointer as must alias, set Must.
435 if (AR == MustAlias)
436 return ModRefInfo::MustMod;
Daniel Berlinb2d22762015-04-13 23:05:45 +0000437 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000438
439 // Otherwise, a store just writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000440 return ModRefInfo::Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000441}
442
Anna Thomas698f0de2017-01-20 00:21:33 +0000443ModRefInfo AAResults::getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) {
444 // If we know that the location is a constant memory location, the fence
445 // cannot modify this location.
446 if (Loc.Ptr && pointsToConstantMemory(Loc))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000447 return ModRefInfo::Ref;
448 return ModRefInfo::ModRef;
Anna Thomas698f0de2017-01-20 00:21:33 +0000449}
450
Chandler Carruth7b560d42015-09-09 17:55:00 +0000451ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
452 const MemoryLocation &Loc) {
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000453 if (Loc.Ptr) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000454 AliasResult AR = alias(MemoryLocation::get(V), Loc);
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000455 // If the va_arg address cannot alias the pointer in question, then the
456 // specified memory cannot be accessed by the va_arg.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000457 if (AR == NoAlias)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000458 return ModRefInfo::NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000459
460 // If the pointer is a pointer to constant memory, then it could not have
461 // been modified by this va_arg.
462 if (pointsToConstantMemory(Loc))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000463 return ModRefInfo::NoModRef;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000464
465 // If the va_arg aliases the pointer as must alias, set Must.
466 if (AR == MustAlias)
467 return ModRefInfo::MustModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000468 }
Dan Gohmane68958f2010-08-06 18:24:38 +0000469
470 // Otherwise, a va_arg reads and writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000471 return ModRefInfo::ModRef;
Dan Gohmane68958f2010-08-06 18:24:38 +0000472}
473
David Majnemer6727c012015-11-17 08:15:14 +0000474ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
475 const MemoryLocation &Loc) {
476 if (Loc.Ptr) {
477 // If the pointer is a pointer to constant memory,
478 // then it could not have been modified by this catchpad.
479 if (pointsToConstantMemory(Loc))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000480 return ModRefInfo::NoModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000481 }
482
483 // Otherwise, a catchpad reads and writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000484 return ModRefInfo::ModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000485}
486
487ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
488 const MemoryLocation &Loc) {
489 if (Loc.Ptr) {
490 // If the pointer is a pointer to constant memory,
491 // then it could not have been modified by this catchpad.
492 if (pointsToConstantMemory(Loc))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000493 return ModRefInfo::NoModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000494 }
495
496 // Otherwise, a catchret reads and writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000497 return ModRefInfo::ModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000498}
499
Chandler Carruth7b560d42015-09-09 17:55:00 +0000500ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
501 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000502 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000503 if (isStrongerThanMonotonic(CX->getSuccessOrdering()))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000504 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000505
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000506 if (Loc.Ptr) {
507 AliasResult AR = alias(MemoryLocation::get(CX), Loc);
508 // If the cmpxchg address does not alias the location, it does not access
509 // it.
510 if (AR == NoAlias)
511 return ModRefInfo::NoModRef;
512
513 // If the cmpxchg address aliases the pointer as must alias, set Must.
514 if (AR == MustAlias)
515 return ModRefInfo::MustModRef;
516 }
Eli Friedman5c918912011-09-26 20:15:28 +0000517
Alina Sbirlea193429f2017-12-07 22:41:34 +0000518 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000519}
520
Chandler Carruth7b560d42015-09-09 17:55:00 +0000521ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
522 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000523 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000524 if (isStrongerThanMonotonic(RMW->getOrdering()))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000525 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000526
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000527 if (Loc.Ptr) {
528 AliasResult AR = alias(MemoryLocation::get(RMW), Loc);
529 // If the atomicrmw address does not alias the location, it does not access
530 // it.
531 if (AR == NoAlias)
532 return ModRefInfo::NoModRef;
533
534 // If the atomicrmw address aliases the pointer as must alias, set Must.
535 if (AR == MustAlias)
536 return ModRefInfo::MustModRef;
537 }
Eli Friedman5c918912011-09-26 20:15:28 +0000538
Alina Sbirlea193429f2017-12-07 22:41:34 +0000539 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000540}
541
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000542/// Return information about whether a particular call site modifies
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000543/// or reads the specified memory location \p MemLoc before instruction \p I
Alina Sbirlea63d22502017-12-05 20:12:23 +0000544/// in a BasicBlock. An ordered basic block \p OBB can be used to speed up
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000545/// instruction-ordering queries inside the BasicBlock containing \p I.
546/// FIXME: this is really just shoring-up a deficiency in alias analysis.
547/// BasicAA isn't willing to spend linear time determining whether an alloca
548/// was captured before or after this particular call, while we are. However,
549/// with a smarter AA in place, this test is just wasting compile time.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000550ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
551 const MemoryLocation &MemLoc,
552 DominatorTree *DT,
553 OrderedBasicBlock *OBB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000554 if (!DT)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000555 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000556
Chandler Carruth7b560d42015-09-09 17:55:00 +0000557 const Value *Object =
558 GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout());
Chad Rosiera968caf2012-05-14 20:35:04 +0000559 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
560 isa<Constant>(Object))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000561 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000562
Chandler Carruth363ac682019-01-07 05:42:51 +0000563 const auto *Call = dyn_cast<CallBase>(I);
564 if (!Call || Call == Object)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000565 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000566
Eugene Zelenko530851c2017-08-11 21:30:02 +0000567 if (PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
568 /* StoreCaptures */ true, I, DT,
569 /* include Object */ true,
570 /* OrderedBasicBlock */ OBB))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000571 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000572
573 unsigned ArgNo = 0;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000574 ModRefInfo R = ModRefInfo::NoModRef;
Alina Sbirlea020363e2018-04-30 20:11:13 +0000575 bool IsMustAlias = true;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000576 // Set flag only if no May found and all operands processed.
Chandler Carruth363ac682019-01-07 05:42:51 +0000577 for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
Chad Rosiera968caf2012-05-14 20:35:04 +0000578 CI != CE; ++CI, ++ArgNo) {
579 // Only look at the no-capture or byval pointer arguments. If this
580 // pointer were passed to arguments that were neither of these, then it
581 // couldn't be no-capture.
582 if (!(*CI)->getType()->isPointerTy() ||
Chandler Carruth363ac682019-01-07 05:42:51 +0000583 (!Call->doesNotCapture(ArgNo) && ArgNo < Call->getNumArgOperands() &&
584 !Call->isByValArgument(ArgNo)))
Chad Rosiera968caf2012-05-14 20:35:04 +0000585 continue;
586
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000587 AliasResult AR = alias(MemoryLocation(*CI), MemoryLocation(Object));
Chad Rosiera968caf2012-05-14 20:35:04 +0000588 // If this is a no-capture pointer argument, see if we can tell that it
589 // is impossible to alias the pointer we're checking. If not, we have to
590 // assume that the call could touch the pointer, even though it doesn't
591 // escape.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000592 if (AR != MustAlias)
Alina Sbirlea020363e2018-04-30 20:11:13 +0000593 IsMustAlias = false;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000594 if (AR == NoAlias)
Nick Lewyckyc0514622013-07-07 10:15:16 +0000595 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000596 if (Call->doesNotAccessMemory(ArgNo))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000597 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000598 if (Call->onlyReadsMemory(ArgNo)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000599 R = ModRefInfo::Ref;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000600 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000601 }
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000602 // Not returning MustModRef since we have not seen all the arguments.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000603 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000604 }
Alina Sbirlea020363e2018-04-30 20:11:13 +0000605 return IsMustAlias ? setMust(R) : clearMust(R);
Chad Rosiera968caf2012-05-14 20:35:04 +0000606}
Eli Friedman5c918912011-09-26 20:15:28 +0000607
Chris Lattnerd922a842002-08-22 22:46:39 +0000608/// canBasicBlockModify - Return true if it is possible for execution of the
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000609/// specified basic block to modify the location Loc.
Chris Lattnerd922a842002-08-22 22:46:39 +0000610///
Chandler Carruth7b560d42015-09-09 17:55:00 +0000611bool AAResults::canBasicBlockModify(const BasicBlock &BB,
612 const MemoryLocation &Loc) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000613 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, ModRefInfo::Mod);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000614}
615
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000616/// canInstructionRangeModRef - Return true if it is possible for the
617/// execution of the specified instructions to mod\ref (according to the
618/// mode) the location Loc. The instructions to consider are all
619/// of the instructions in the range of [I1,I2] INCLUSIVE.
Teresa Johnsonbbcf75e2015-05-13 15:04:14 +0000620/// I1 and I2 must be in the same basic block.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000621bool AAResults::canInstructionRangeModRef(const Instruction &I1,
622 const Instruction &I2,
623 const MemoryLocation &Loc,
624 const ModRefInfo Mode) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000625 assert(I1.getParent() == I2.getParent() &&
626 "Instructions not in same basic block!");
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000627 BasicBlock::const_iterator I = I1.getIterator();
628 BasicBlock::const_iterator E = I2.getIterator();
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000629 ++E; // Convert from inclusive to exclusive range.
630
Chris Lattner3a311832003-02-26 19:26:51 +0000631 for (; I != E; ++I) // Check every instruction in range
Alina Sbirlea18fea012017-12-06 19:56:37 +0000632 if (isModOrRefSet(intersectModRef(getModRefInfo(&*I, Loc), Mode)))
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000633 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000634 return false;
635}
636
Chandler Carruth7b560d42015-09-09 17:55:00 +0000637// Provide a definition for the root virtual destructor.
Eugene Zelenko530851c2017-08-11 21:30:02 +0000638AAResults::Concept::~Concept() = default;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000639
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000640// Provide a definition for the static object used to identify passes.
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000641AnalysisKey AAManager::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000642
Chandler Carruth2be10752015-10-21 12:15:19 +0000643namespace {
Eugene Zelenko530851c2017-08-11 21:30:02 +0000644
Eugene Zelenko530851c2017-08-11 21:30:02 +0000645
646} // end anonymous namespace
Chandler Carruth2be10752015-10-21 12:15:19 +0000647
648char ExternalAAWrapperPass::ID = 0;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000649
Chandler Carruth2be10752015-10-21 12:15:19 +0000650INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
651 false, true)
652
653ImmutablePass *
654llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
655 return new ExternalAAWrapperPass(std::move(Callback));
656}
657
Chandler Carruth7b560d42015-09-09 17:55:00 +0000658AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
659 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
660}
661
662char AAResultsWrapperPass::ID = 0;
663
664INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
665 "Function Alias Analysis Results", false, true)
666INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
George Burgess IVbfa401e2016-07-06 00:26:41 +0000667INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass)
668INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass)
Chandler Carruth2be10752015-10-21 12:15:19 +0000669INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000670INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
671INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
672INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
673INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
674INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
675INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
676 "Function Alias Analysis Results", false, true)
677
678FunctionPass *llvm::createAAResultsWrapperPass() {
679 return new AAResultsWrapperPass();
680}
681
682/// Run the wrapper pass to rebuild an aggregation over known AA passes.
683///
684/// This is the legacy pass manager's interface to the new-style AA results
685/// aggregation object. Because this is somewhat shoe-horned into the legacy
686/// pass manager, we hard code all the specific alias analyses available into
687/// it. While the particular set enabled is configured via commandline flags,
688/// adding a new alias analysis to LLVM will require adding support for it to
689/// this list.
690bool AAResultsWrapperPass::runOnFunction(Function &F) {
691 // NB! This *must* be reset before adding new AA results to the new
692 // AAResults object because in the legacy pass manager, each instance
693 // of these will refer to the *same* immutable analyses, registering and
694 // unregistering themselves with them. We need to carefully tear down the
695 // previous object first, in this case replacing it with an empty one, before
696 // registering new results.
Chandler Carruth12884f72016-03-02 15:56:53 +0000697 AAR.reset(
698 new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000699
700 // BasicAA is always available for function analyses. Also, we add it first
701 // so that it can trump TBAA results when it proves MustAlias.
702 // FIXME: TBAA should have an explicit mode to support this and then we
703 // should reconsider the ordering here.
704 if (!DisableBasicAA)
705 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
706
707 // Populate the results with the currently available AAs.
708 if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
709 AAR->addAAResult(WrapperPass->getResult());
710 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
711 AAR->addAAResult(WrapperPass->getResult());
712 if (auto *WrapperPass =
713 getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
714 AAR->addAAResult(WrapperPass->getResult());
715 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
716 AAR->addAAResult(WrapperPass->getResult());
717 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
718 AAR->addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000719 if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
720 AAR->addAAResult(WrapperPass->getResult());
721 if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000722 AAR->addAAResult(WrapperPass->getResult());
723
Chandler Carruth2be10752015-10-21 12:15:19 +0000724 // If available, run an external AA providing callback over the results as
725 // well.
726 if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
727 if (WrapperPass->CB)
728 WrapperPass->CB(*this, F, *AAR);
729
Chandler Carruth7b560d42015-09-09 17:55:00 +0000730 // Analyses don't mutate the IR, so return false.
731 return false;
732}
733
734void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
735 AU.setPreservesAll();
736 AU.addRequired<BasicAAWrapperPass>();
Chandler Carruth12884f72016-03-02 15:56:53 +0000737 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000738
739 // We also need to mark all the alias analysis passes we will potentially
740 // probe in runOnFunction as used here to ensure the legacy pass manager
741 // preserves them. This hard coding of lists of alias analyses is specific to
742 // the legacy pass manager.
743 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
744 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
745 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
746 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
747 AU.addUsedIfAvailable<SCEVAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000748 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
749 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000750}
751
752AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
753 BasicAAResult &BAR) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000754 AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000755
756 // Add in our explicitly constructed BasicAA results.
757 if (!DisableBasicAA)
758 AAR.addAAResult(BAR);
759
760 // Populate the results with the other currently available AAs.
761 if (auto *WrapperPass =
762 P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
763 AAR.addAAResult(WrapperPass->getResult());
764 if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
765 AAR.addAAResult(WrapperPass->getResult());
766 if (auto *WrapperPass =
767 P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
768 AAR.addAAResult(WrapperPass->getResult());
769 if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
770 AAR.addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000771 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
772 AAR.addAAResult(WrapperPass->getResult());
773 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000774 AAR.addAAResult(WrapperPass->getResult());
775
776 return AAR;
777}
778
Dan Gohmanfb306c02009-02-03 01:28:32 +0000779bool llvm::isNoAliasCall(const Value *V) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000780 if (const auto *Call = dyn_cast<CallBase>(V))
781 return Call->hasRetAttr(Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000782 return false;
783}
784
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000785bool llvm::isNoAliasArgument(const Value *V) {
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000786 if (const Argument *A = dyn_cast<Argument>(V))
787 return A->hasNoAliasAttr();
788 return false;
789}
790
Dan Gohman00ef9322010-07-07 14:27:09 +0000791bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000792 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000793 return true;
794 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000795 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000796 if (isNoAliasCall(V))
797 return true;
798 if (const Argument *A = dyn_cast<Argument>(V))
799 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000800 return false;
801}
Hal Finkelc782aa52014-07-21 12:27:23 +0000802
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000803bool llvm::isIdentifiedFunctionLocal(const Value *V) {
Hal Finkelc782aa52014-07-21 12:27:23 +0000804 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
805}
Sanjoy Das1c481f52016-02-09 01:21:57 +0000806
Chandler Carruth12884f72016-03-02 15:56:53 +0000807void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
Sanjoy Das1c481f52016-02-09 01:21:57 +0000808 // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
809 // more alias analyses are added to llvm::createLegacyPMAAResults, they need
810 // to be added here also.
Chandler Carruth12884f72016-03-02 15:56:53 +0000811 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000812 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
813 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
814 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
815 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000816 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
817 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000818}