blob: 06a33f659c19301936cd7888b2208227237f5671 [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) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000102 AAQueryInfo AAQIP;
103 return alias(LocA, LocB, AAQIP);
104}
105
106AliasResult AAResults::alias(const MemoryLocation &LocA,
107 const MemoryLocation &LocB, AAQueryInfo &AAQI) {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000108 for (const auto &AA : AAs) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000109 auto Result = AA->alias(LocA, LocB, AAQI);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000110 if (Result != MayAlias)
111 return Result;
112 }
113 return MayAlias;
Chris Lattner62c37002004-05-23 21:15:48 +0000114}
115
Chandler Carruth7b560d42015-09-09 17:55:00 +0000116bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
117 bool OrLocal) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000118 AAQueryInfo AAQIP;
119 return pointsToConstantMemory(Loc, AAQIP, OrLocal);
120}
121
122bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
123 AAQueryInfo &AAQI, bool OrLocal) {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000124 for (const auto &AA : AAs)
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000125 if (AA->pointsToConstantMemory(Loc, AAQI, OrLocal))
Chandler Carruth7b560d42015-09-09 17:55:00 +0000126 return true;
127
128 return false;
Chris Lattner62c37002004-05-23 21:15:48 +0000129}
130
Chandler Carruth363ac682019-01-07 05:42:51 +0000131ModRefInfo AAResults::getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000132 ModRefInfo Result = ModRefInfo::ModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000133
134 for (const auto &AA : AAs) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000135 Result = intersectModRef(Result, AA->getArgModRefInfo(Call, ArgIdx));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000136
137 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000138 if (isNoModRef(Result))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000139 return ModRefInfo::NoModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000140 }
141
142 return Result;
Hal Finkel354e23b2014-07-17 01:28:25 +0000143}
144
Chandler Carruth363ac682019-01-07 05:42:51 +0000145ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000146 AAQueryInfo AAQIP;
147 return getModRefInfo(I, Call2, AAQIP);
148}
149
150ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2,
151 AAQueryInfo &AAQI) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000152 // We may have two calls.
Chandler Carruth363ac682019-01-07 05:42:51 +0000153 if (const auto *Call1 = dyn_cast<CallBase>(I)) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000154 // Check if the two calls modify the same memory.
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000155 return getModRefInfo(Call1, Call2, AAQI);
David Majnemera940f362016-07-15 17:19:24 +0000156 } else if (I->isFenceLike()) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000157 // If this is a fence, just return ModRef.
158 return ModRefInfo::ModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000159 } else {
160 // Otherwise, check if the call modifies or references the
161 // location this memory access defines. The best we can say
162 // is that if the call references what this instruction
163 // defines, it must be clobbered by this location.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000164 const MemoryLocation DefLoc = MemoryLocation::get(I);
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000165 ModRefInfo MR = getModRefInfo(Call2, DefLoc, AAQI);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000166 if (isModOrRefSet(MR))
167 return setModAndRef(MR);
Daniel Berlin8de312d2015-04-13 23:25:41 +0000168 }
Alina Sbirlea193429f2017-12-07 22:41:34 +0000169 return ModRefInfo::NoModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000170}
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000171
Chandler Carruth363ac682019-01-07 05:42:51 +0000172ModRefInfo AAResults::getModRefInfo(const CallBase *Call,
Chandler Carruth7b560d42015-09-09 17:55:00 +0000173 const MemoryLocation &Loc) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000174 AAQueryInfo AAQIP;
175 return getModRefInfo(Call, Loc, AAQIP);
176}
177
178ModRefInfo AAResults::getModRefInfo(const CallBase *Call,
179 const MemoryLocation &Loc,
180 AAQueryInfo &AAQI) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000181 ModRefInfo Result = ModRefInfo::ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000182
Chandler Carruth7b560d42015-09-09 17:55:00 +0000183 for (const auto &AA : AAs) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000184 Result = intersectModRef(Result, AA->getModRefInfo(Call, Loc, AAQI));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000185
Chandler Carruth7b560d42015-09-09 17:55:00 +0000186 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000187 if (isNoModRef(Result))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000188 return ModRefInfo::NoModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000189 }
190
Chandler Carruth12884f72016-03-02 15:56:53 +0000191 // Try to refine the mod-ref info further using other API entry points to the
192 // aggregate set of AA results.
Chandler Carruth363ac682019-01-07 05:42:51 +0000193 auto MRB = getModRefBehavior(Call);
Andrew Kaylor9604f342016-11-08 21:07:42 +0000194 if (MRB == FMRB_DoesNotAccessMemory ||
195 MRB == FMRB_OnlyAccessesInaccessibleMem)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000196 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000197
198 if (onlyReadsMemory(MRB))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000199 Result = clearMod(Result);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000200 else if (doesNotReadMemory(MRB))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000201 Result = clearRef(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000202
Andrew Kaylor9604f342016-11-08 21:07:42 +0000203 if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) {
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000204 bool IsMustAlias = true;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000205 ModRefInfo AllArgsMask = ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000206 if (doesAccessArgPointees(MRB)) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000207 for (auto AI = Call->arg_begin(), AE = Call->arg_end(); AI != AE; ++AI) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000208 const Value *Arg = *AI;
209 if (!Arg->getType()->isPointerTy())
210 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000211 unsigned ArgIdx = std::distance(Call->arg_begin(), AI);
212 MemoryLocation ArgLoc =
213 MemoryLocation::getForArgument(Call, ArgIdx, TLI);
Chandler Carruth12884f72016-03-02 15:56:53 +0000214 AliasResult ArgAlias = alias(ArgLoc, Loc);
215 if (ArgAlias != NoAlias) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000216 ModRefInfo ArgMask = getArgModRefInfo(Call, ArgIdx);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000217 AllArgsMask = unionModRef(AllArgsMask, ArgMask);
Chandler Carruth12884f72016-03-02 15:56:53 +0000218 }
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000219 // Conservatively clear IsMustAlias unless only MustAlias is found.
220 IsMustAlias &= (ArgAlias == MustAlias);
Chandler Carruth12884f72016-03-02 15:56:53 +0000221 }
222 }
Alina Sbirlea193429f2017-12-07 22:41:34 +0000223 // Return NoModRef if no alias found with any argument.
Philip Reames8abf4482018-08-22 19:50:45 +0000224 if (isNoModRef(AllArgsMask))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000225 return ModRefInfo::NoModRef;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000226 // Logical & between other AA analyses and argument analysis.
227 Result = intersectModRef(Result, AllArgsMask);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000228 // If only MustAlias found above, set Must bit.
229 Result = IsMustAlias ? setMust(Result) : clearMust(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000230 }
231
232 // If Loc is a constant memory location, the call definitely could not
233 // modify the memory location.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000234 if (isModSet(Result) && pointsToConstantMemory(Loc, /*OrLocal*/ false))
235 Result = clearMod(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000236
Chandler Carruth7b560d42015-09-09 17:55:00 +0000237 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000238}
239
Chandler Carruth363ac682019-01-07 05:42:51 +0000240ModRefInfo AAResults::getModRefInfo(const CallBase *Call1,
241 const CallBase *Call2) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000242 AAQueryInfo AAQIP;
243 return getModRefInfo(Call1, Call2, AAQIP);
244}
245
246ModRefInfo AAResults::getModRefInfo(const CallBase *Call1,
247 const CallBase *Call2, AAQueryInfo &AAQI) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000248 ModRefInfo Result = ModRefInfo::ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000249
Chandler Carruth7b560d42015-09-09 17:55:00 +0000250 for (const auto &AA : AAs) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000251 Result = intersectModRef(Result, AA->getModRefInfo(Call1, Call2, AAQI));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000252
Chandler Carruth7b560d42015-09-09 17:55:00 +0000253 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000254 if (isNoModRef(Result))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000255 return ModRefInfo::NoModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000256 }
257
Chandler Carruth12884f72016-03-02 15:56:53 +0000258 // Try to refine the mod-ref info further using other API entry points to the
259 // aggregate set of AA results.
260
Chandler Carruth363ac682019-01-07 05:42:51 +0000261 // If Call1 or Call2 are readnone, they don't interact.
262 auto Call1B = getModRefBehavior(Call1);
263 if (Call1B == FMRB_DoesNotAccessMemory)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000264 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000265
Chandler Carruth363ac682019-01-07 05:42:51 +0000266 auto Call2B = getModRefBehavior(Call2);
267 if (Call2B == FMRB_DoesNotAccessMemory)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000268 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000269
270 // If they both only read from memory, there is no dependence.
Chandler Carruth363ac682019-01-07 05:42:51 +0000271 if (onlyReadsMemory(Call1B) && onlyReadsMemory(Call2B))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000272 return ModRefInfo::NoModRef;
Chandler Carruth12884f72016-03-02 15:56:53 +0000273
Chandler Carruth363ac682019-01-07 05:42:51 +0000274 // If Call1 only reads memory, the only dependence on Call2 can be
275 // from Call1 reading memory written by Call2.
276 if (onlyReadsMemory(Call1B))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000277 Result = clearMod(Result);
Chandler Carruth363ac682019-01-07 05:42:51 +0000278 else if (doesNotReadMemory(Call1B))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000279 Result = clearRef(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000280
Chandler Carruth363ac682019-01-07 05:42:51 +0000281 // If Call2 only access memory through arguments, accumulate the mod/ref
282 // information from Call1's references to the memory referenced by
283 // Call2's arguments.
284 if (onlyAccessesArgPointees(Call2B)) {
285 if (!doesAccessArgPointees(Call2B))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000286 return ModRefInfo::NoModRef;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000287 ModRefInfo R = ModRefInfo::NoModRef;
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000288 bool IsMustAlias = true;
Chandler Carruth363ac682019-01-07 05:42:51 +0000289 for (auto I = Call2->arg_begin(), E = Call2->arg_end(); I != E; ++I) {
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000290 const Value *Arg = *I;
291 if (!Arg->getType()->isPointerTy())
292 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000293 unsigned Call2ArgIdx = std::distance(Call2->arg_begin(), I);
294 auto Call2ArgLoc =
295 MemoryLocation::getForArgument(Call2, Call2ArgIdx, TLI);
Chandler Carruth12884f72016-03-02 15:56:53 +0000296
Chandler Carruth363ac682019-01-07 05:42:51 +0000297 // ArgModRefC2 indicates what Call2 might do to Call2ArgLoc, and the
298 // dependence of Call1 on that location is the inverse:
299 // - If Call2 modifies location, dependence exists if Call1 reads or
300 // writes.
301 // - If Call2 only reads location, dependence exists if Call1 writes.
302 ModRefInfo ArgModRefC2 = getArgModRefInfo(Call2, Call2ArgIdx);
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000303 ModRefInfo ArgMask = ModRefInfo::NoModRef;
Chandler Carruth363ac682019-01-07 05:42:51 +0000304 if (isModSet(ArgModRefC2))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000305 ArgMask = ModRefInfo::ModRef;
Chandler Carruth363ac682019-01-07 05:42:51 +0000306 else if (isRefSet(ArgModRefC2))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000307 ArgMask = ModRefInfo::Mod;
Chandler Carruth12884f72016-03-02 15:56:53 +0000308
Chandler Carruth363ac682019-01-07 05:42:51 +0000309 // ModRefC1 indicates what Call1 might do to Call2ArgLoc, and we use
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000310 // above ArgMask to update dependence info.
Chandler Carruth363ac682019-01-07 05:42:51 +0000311 ModRefInfo ModRefC1 = getModRefInfo(Call1, Call2ArgLoc);
312 ArgMask = intersectModRef(ArgMask, ModRefC1);
Chandler Carruth12884f72016-03-02 15:56:53 +0000313
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000314 // Conservatively clear IsMustAlias unless only MustAlias is found.
Chandler Carruth363ac682019-01-07 05:42:51 +0000315 IsMustAlias &= isMustSet(ModRefC1);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000316
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000317 R = intersectModRef(unionModRef(R, ArgMask), Result);
318 if (R == Result) {
319 // On early exit, not all args were checked, cannot set Must.
320 if (I + 1 != E)
321 IsMustAlias = false;
322 break;
Chandler Carruth12884f72016-03-02 15:56:53 +0000323 }
324 }
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000325
326 if (isNoModRef(R))
327 return ModRefInfo::NoModRef;
328
329 // If MustAlias found above, set Must bit.
330 return IsMustAlias ? setMust(R) : clearMust(R);
Chandler Carruth12884f72016-03-02 15:56:53 +0000331 }
332
Chandler Carruth363ac682019-01-07 05:42:51 +0000333 // If Call1 only accesses memory through arguments, check if Call2 references
334 // any of the memory referenced by Call1's arguments. If not, return NoModRef.
335 if (onlyAccessesArgPointees(Call1B)) {
336 if (!doesAccessArgPointees(Call1B))
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000337 return ModRefInfo::NoModRef;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000338 ModRefInfo R = ModRefInfo::NoModRef;
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000339 bool IsMustAlias = true;
Chandler Carruth363ac682019-01-07 05:42:51 +0000340 for (auto I = Call1->arg_begin(), E = Call1->arg_end(); I != E; ++I) {
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000341 const Value *Arg = *I;
342 if (!Arg->getType()->isPointerTy())
343 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000344 unsigned Call1ArgIdx = std::distance(Call1->arg_begin(), I);
345 auto Call1ArgLoc =
346 MemoryLocation::getForArgument(Call1, Call1ArgIdx, TLI);
Chandler Carruth12884f72016-03-02 15:56:53 +0000347
Chandler Carruth363ac682019-01-07 05:42:51 +0000348 // ArgModRefC1 indicates what Call1 might do to Call1ArgLoc; if Call1
349 // might Mod Call1ArgLoc, then we care about either a Mod or a Ref by
350 // Call2. If Call1 might Ref, then we care only about a Mod by Call2.
351 ModRefInfo ArgModRefC1 = getArgModRefInfo(Call1, Call1ArgIdx);
352 ModRefInfo ModRefC2 = getModRefInfo(Call2, Call1ArgLoc);
353 if ((isModSet(ArgModRefC1) && isModOrRefSet(ModRefC2)) ||
354 (isRefSet(ArgModRefC1) && isModSet(ModRefC2)))
355 R = intersectModRef(unionModRef(R, ArgModRefC1), Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000356
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000357 // Conservatively clear IsMustAlias unless only MustAlias is found.
Chandler Carruth363ac682019-01-07 05:42:51 +0000358 IsMustAlias &= isMustSet(ModRefC2);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000359
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000360 if (R == Result) {
361 // On early exit, not all args were checked, cannot set Must.
362 if (I + 1 != E)
363 IsMustAlias = false;
364 break;
Chandler Carruth12884f72016-03-02 15:56:53 +0000365 }
366 }
Alina Sbirleadf26cf82018-01-19 10:26:40 +0000367
368 if (isNoModRef(R))
369 return ModRefInfo::NoModRef;
370
371 // If MustAlias found above, set Must bit.
372 return IsMustAlias ? setMust(R) : clearMust(R);
Chandler Carruth12884f72016-03-02 15:56:53 +0000373 }
374
Chandler Carruth7b560d42015-09-09 17:55:00 +0000375 return Result;
376}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000377
Chandler Carruth363ac682019-01-07 05:42:51 +0000378FunctionModRefBehavior AAResults::getModRefBehavior(const CallBase *Call) {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000379 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Hal Finkel354e23b2014-07-17 01:28:25 +0000380
Chandler Carruth7b560d42015-09-09 17:55:00 +0000381 for (const auto &AA : AAs) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000382 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(Call));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000383
384 // Early-exit the moment we reach the bottom of the lattice.
385 if (Result == FMRB_DoesNotAccessMemory)
386 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000387 }
388
Chandler Carruth7b560d42015-09-09 17:55:00 +0000389 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000390}
391
Chandler Carruth7b560d42015-09-09 17:55:00 +0000392FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
393 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000394
Chandler Carruth7b560d42015-09-09 17:55:00 +0000395 for (const auto &AA : AAs) {
396 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000397
Chandler Carruth7b560d42015-09-09 17:55:00 +0000398 // Early-exit the moment we reach the bottom of the lattice.
399 if (Result == FMRB_DoesNotAccessMemory)
400 return Result;
401 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000402
Chandler Carruth7b560d42015-09-09 17:55:00 +0000403 return Result;
Chris Lattner62c37002004-05-23 21:15:48 +0000404}
405
George Burgess IVaa283d82018-06-14 19:55:53 +0000406raw_ostream &llvm::operator<<(raw_ostream &OS, AliasResult AR) {
407 switch (AR) {
408 case NoAlias:
409 OS << "NoAlias";
410 break;
411 case MustAlias:
412 OS << "MustAlias";
413 break;
414 case MayAlias:
415 OS << "MayAlias";
416 break;
417 case PartialAlias:
418 OS << "PartialAlias";
419 break;
420 }
421 return OS;
422}
423
Chris Lattner62c37002004-05-23 21:15:48 +0000424//===----------------------------------------------------------------------===//
Chandler Carruth7b560d42015-09-09 17:55:00 +0000425// Helper method implementation
Chris Lattner62c37002004-05-23 21:15:48 +0000426//===----------------------------------------------------------------------===//
427
Chandler Carruth7b560d42015-09-09 17:55:00 +0000428ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
429 const MemoryLocation &Loc) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000430 AAQueryInfo AAQIP;
431 return getModRefInfo(L, Loc, AAQIP);
432}
433ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
434 const MemoryLocation &Loc,
435 AAQueryInfo &AAQI) {
Daniel Berlind952cea2017-04-07 01:28:36 +0000436 // Be conservative in the face of atomic.
437 if (isStrongerThan(L->getOrdering(), AtomicOrdering::Unordered))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000438 return ModRefInfo::ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000439
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000440 // If the load address doesn't alias the given address, it doesn't read
441 // or write the specified memory.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000442 if (Loc.Ptr) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000443 AliasResult AR = alias(MemoryLocation::get(L), Loc, AAQI);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000444 if (AR == NoAlias)
445 return ModRefInfo::NoModRef;
446 if (AR == MustAlias)
447 return ModRefInfo::MustRef;
448 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000449 // Otherwise, a load just reads.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000450 return ModRefInfo::Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000451}
452
Chandler Carruth7b560d42015-09-09 17:55:00 +0000453ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
454 const MemoryLocation &Loc) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000455 AAQueryInfo AAQIP;
456 return getModRefInfo(S, Loc, AAQIP);
457}
458ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
459 const MemoryLocation &Loc,
460 AAQueryInfo &AAQI) {
Daniel Berlind952cea2017-04-07 01:28:36 +0000461 // Be conservative in the face of atomic.
462 if (isStrongerThan(S->getOrdering(), AtomicOrdering::Unordered))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000463 return ModRefInfo::ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000464
Daniel Berlinb2d22762015-04-13 23:05:45 +0000465 if (Loc.Ptr) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000466 AliasResult AR = alias(MemoryLocation::get(S), Loc, AAQI);
Daniel Berlinb2d22762015-04-13 23:05:45 +0000467 // If the store address cannot alias the pointer in question, then the
468 // specified memory cannot be modified by the store.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000469 if (AR == NoAlias)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000470 return ModRefInfo::NoModRef;
Chris Lattner96055762004-01-30 22:16:42 +0000471
Daniel Berlinb2d22762015-04-13 23:05:45 +0000472 // If the pointer is a pointer to constant memory, then it could not have
473 // been modified by this store.
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000474 if (pointsToConstantMemory(Loc, AAQI))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000475 return ModRefInfo::NoModRef;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000476
477 // If the store address aliases the pointer as must alias, set Must.
478 if (AR == MustAlias)
479 return ModRefInfo::MustMod;
Daniel Berlinb2d22762015-04-13 23:05:45 +0000480 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000481
482 // Otherwise, a store just writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000483 return ModRefInfo::Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000484}
485
Anna Thomas698f0de2017-01-20 00:21:33 +0000486ModRefInfo AAResults::getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000487 AAQueryInfo AAQIP;
488 return getModRefInfo(S, Loc, AAQIP);
489}
490
491ModRefInfo AAResults::getModRefInfo(const FenceInst *S,
492 const MemoryLocation &Loc,
493 AAQueryInfo &AAQI) {
Anna Thomas698f0de2017-01-20 00:21:33 +0000494 // If we know that the location is a constant memory location, the fence
495 // cannot modify this location.
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000496 if (Loc.Ptr && pointsToConstantMemory(Loc, AAQI))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000497 return ModRefInfo::Ref;
498 return ModRefInfo::ModRef;
Anna Thomas698f0de2017-01-20 00:21:33 +0000499}
500
Chandler Carruth7b560d42015-09-09 17:55:00 +0000501ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
502 const MemoryLocation &Loc) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000503 AAQueryInfo AAQIP;
504 return getModRefInfo(V, Loc, AAQIP);
505}
506
507ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
508 const MemoryLocation &Loc,
509 AAQueryInfo &AAQI) {
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000510 if (Loc.Ptr) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000511 AliasResult AR = alias(MemoryLocation::get(V), Loc, AAQI);
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000512 // If the va_arg address cannot alias the pointer in question, then the
513 // specified memory cannot be accessed by the va_arg.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000514 if (AR == NoAlias)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000515 return ModRefInfo::NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000516
517 // If the pointer is a pointer to constant memory, then it could not have
518 // been modified by this va_arg.
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000519 if (pointsToConstantMemory(Loc, AAQI))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000520 return ModRefInfo::NoModRef;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000521
522 // If the va_arg aliases the pointer as must alias, set Must.
523 if (AR == MustAlias)
524 return ModRefInfo::MustModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000525 }
Dan Gohmane68958f2010-08-06 18:24:38 +0000526
527 // Otherwise, a va_arg reads and writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000528 return ModRefInfo::ModRef;
Dan Gohmane68958f2010-08-06 18:24:38 +0000529}
530
David Majnemer6727c012015-11-17 08:15:14 +0000531ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
532 const MemoryLocation &Loc) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000533 AAQueryInfo AAQIP;
534 return getModRefInfo(CatchPad, Loc, AAQIP);
535}
536
537ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
538 const MemoryLocation &Loc,
539 AAQueryInfo &AAQI) {
David Majnemer6727c012015-11-17 08:15:14 +0000540 if (Loc.Ptr) {
541 // If the pointer is a pointer to constant memory,
542 // then it could not have been modified by this catchpad.
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000543 if (pointsToConstantMemory(Loc, AAQI))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000544 return ModRefInfo::NoModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000545 }
546
547 // Otherwise, a catchpad reads and writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000548 return ModRefInfo::ModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000549}
550
551ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
552 const MemoryLocation &Loc) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000553 AAQueryInfo AAQIP;
554 return getModRefInfo(CatchRet, Loc, AAQIP);
555}
556
557ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
558 const MemoryLocation &Loc,
559 AAQueryInfo &AAQI) {
David Majnemer6727c012015-11-17 08:15:14 +0000560 if (Loc.Ptr) {
561 // If the pointer is a pointer to constant memory,
562 // then it could not have been modified by this catchpad.
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000563 if (pointsToConstantMemory(Loc, AAQI))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000564 return ModRefInfo::NoModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000565 }
566
567 // Otherwise, a catchret reads and writes.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000568 return ModRefInfo::ModRef;
David Majnemer6727c012015-11-17 08:15:14 +0000569}
570
Chandler Carruth7b560d42015-09-09 17:55:00 +0000571ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
572 const MemoryLocation &Loc) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000573 AAQueryInfo AAQIP;
574 return getModRefInfo(CX, Loc, AAQIP);
575}
576
577ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
578 const MemoryLocation &Loc,
579 AAQueryInfo &AAQI) {
Eli Friedman5c918912011-09-26 20:15:28 +0000580 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000581 if (isStrongerThanMonotonic(CX->getSuccessOrdering()))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000582 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000583
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000584 if (Loc.Ptr) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000585 AliasResult AR = alias(MemoryLocation::get(CX), Loc, AAQI);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000586 // If the cmpxchg address does not alias the location, it does not access
587 // it.
588 if (AR == NoAlias)
589 return ModRefInfo::NoModRef;
590
591 // If the cmpxchg address aliases the pointer as must alias, set Must.
592 if (AR == MustAlias)
593 return ModRefInfo::MustModRef;
594 }
Eli Friedman5c918912011-09-26 20:15:28 +0000595
Alina Sbirlea193429f2017-12-07 22:41:34 +0000596 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000597}
598
Chandler Carruth7b560d42015-09-09 17:55:00 +0000599ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
600 const MemoryLocation &Loc) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000601 AAQueryInfo AAQIP;
602 return getModRefInfo(RMW, Loc, AAQIP);
603}
604
605ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
606 const MemoryLocation &Loc,
607 AAQueryInfo &AAQI) {
Eli Friedman5c918912011-09-26 20:15:28 +0000608 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000609 if (isStrongerThanMonotonic(RMW->getOrdering()))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000610 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000611
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000612 if (Loc.Ptr) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000613 AliasResult AR = alias(MemoryLocation::get(RMW), Loc, AAQI);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000614 // If the atomicrmw address does not alias the location, it does not access
615 // it.
616 if (AR == NoAlias)
617 return ModRefInfo::NoModRef;
618
619 // If the atomicrmw address aliases the pointer as must alias, set Must.
620 if (AR == MustAlias)
621 return ModRefInfo::MustModRef;
622 }
Eli Friedman5c918912011-09-26 20:15:28 +0000623
Alina Sbirlea193429f2017-12-07 22:41:34 +0000624 return ModRefInfo::ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000625}
626
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000627/// Return information about whether a particular call site modifies
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000628/// or reads the specified memory location \p MemLoc before instruction \p I
Alina Sbirlea63d22502017-12-05 20:12:23 +0000629/// in a BasicBlock. An ordered basic block \p OBB can be used to speed up
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000630/// instruction-ordering queries inside the BasicBlock containing \p I.
631/// FIXME: this is really just shoring-up a deficiency in alias analysis.
632/// BasicAA isn't willing to spend linear time determining whether an alloca
633/// was captured before or after this particular call, while we are. However,
634/// with a smarter AA in place, this test is just wasting compile time.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000635ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
636 const MemoryLocation &MemLoc,
637 DominatorTree *DT,
638 OrderedBasicBlock *OBB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000639 if (!DT)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000640 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000641
Chandler Carruth7b560d42015-09-09 17:55:00 +0000642 const Value *Object =
643 GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout());
Chad Rosiera968caf2012-05-14 20:35:04 +0000644 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
645 isa<Constant>(Object))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000646 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000647
Chandler Carruth363ac682019-01-07 05:42:51 +0000648 const auto *Call = dyn_cast<CallBase>(I);
649 if (!Call || Call == Object)
Alina Sbirlea193429f2017-12-07 22:41:34 +0000650 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000651
Eugene Zelenko530851c2017-08-11 21:30:02 +0000652 if (PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
653 /* StoreCaptures */ true, I, DT,
654 /* include Object */ true,
655 /* OrderedBasicBlock */ OBB))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000656 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000657
658 unsigned ArgNo = 0;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000659 ModRefInfo R = ModRefInfo::NoModRef;
Alina Sbirlea020363e2018-04-30 20:11:13 +0000660 bool IsMustAlias = true;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000661 // Set flag only if no May found and all operands processed.
Chandler Carruth363ac682019-01-07 05:42:51 +0000662 for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
Chad Rosiera968caf2012-05-14 20:35:04 +0000663 CI != CE; ++CI, ++ArgNo) {
664 // Only look at the no-capture or byval pointer arguments. If this
665 // pointer were passed to arguments that were neither of these, then it
666 // couldn't be no-capture.
667 if (!(*CI)->getType()->isPointerTy() ||
Chandler Carruth363ac682019-01-07 05:42:51 +0000668 (!Call->doesNotCapture(ArgNo) && ArgNo < Call->getNumArgOperands() &&
669 !Call->isByValArgument(ArgNo)))
Chad Rosiera968caf2012-05-14 20:35:04 +0000670 continue;
671
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000672 AliasResult AR = alias(MemoryLocation(*CI), MemoryLocation(Object));
Chad Rosiera968caf2012-05-14 20:35:04 +0000673 // If this is a no-capture pointer argument, see if we can tell that it
674 // is impossible to alias the pointer we're checking. If not, we have to
675 // assume that the call could touch the pointer, even though it doesn't
676 // escape.
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000677 if (AR != MustAlias)
Alina Sbirlea020363e2018-04-30 20:11:13 +0000678 IsMustAlias = false;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000679 if (AR == NoAlias)
Nick Lewyckyc0514622013-07-07 10:15:16 +0000680 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000681 if (Call->doesNotAccessMemory(ArgNo))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000682 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000683 if (Call->onlyReadsMemory(ArgNo)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000684 R = ModRefInfo::Ref;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000685 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000686 }
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000687 // Not returning MustModRef since we have not seen all the arguments.
Alina Sbirlea193429f2017-12-07 22:41:34 +0000688 return ModRefInfo::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000689 }
Alina Sbirlea020363e2018-04-30 20:11:13 +0000690 return IsMustAlias ? setMust(R) : clearMust(R);
Chad Rosiera968caf2012-05-14 20:35:04 +0000691}
Eli Friedman5c918912011-09-26 20:15:28 +0000692
Chris Lattnerd922a842002-08-22 22:46:39 +0000693/// canBasicBlockModify - Return true if it is possible for execution of the
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000694/// specified basic block to modify the location Loc.
Chris Lattnerd922a842002-08-22 22:46:39 +0000695///
Chandler Carruth7b560d42015-09-09 17:55:00 +0000696bool AAResults::canBasicBlockModify(const BasicBlock &BB,
697 const MemoryLocation &Loc) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000698 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, ModRefInfo::Mod);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000699}
700
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000701/// canInstructionRangeModRef - Return true if it is possible for the
702/// execution of the specified instructions to mod\ref (according to the
703/// mode) the location Loc. The instructions to consider are all
704/// of the instructions in the range of [I1,I2] INCLUSIVE.
Teresa Johnsonbbcf75e2015-05-13 15:04:14 +0000705/// I1 and I2 must be in the same basic block.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000706bool AAResults::canInstructionRangeModRef(const Instruction &I1,
707 const Instruction &I2,
708 const MemoryLocation &Loc,
709 const ModRefInfo Mode) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000710 assert(I1.getParent() == I2.getParent() &&
711 "Instructions not in same basic block!");
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000712 BasicBlock::const_iterator I = I1.getIterator();
713 BasicBlock::const_iterator E = I2.getIterator();
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000714 ++E; // Convert from inclusive to exclusive range.
715
Chris Lattner3a311832003-02-26 19:26:51 +0000716 for (; I != E; ++I) // Check every instruction in range
Alina Sbirlea18fea012017-12-06 19:56:37 +0000717 if (isModOrRefSet(intersectModRef(getModRefInfo(&*I, Loc), Mode)))
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000718 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000719 return false;
720}
721
Chandler Carruth7b560d42015-09-09 17:55:00 +0000722// Provide a definition for the root virtual destructor.
Eugene Zelenko530851c2017-08-11 21:30:02 +0000723AAResults::Concept::~Concept() = default;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000724
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000725// Provide a definition for the static object used to identify passes.
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000726AnalysisKey AAManager::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000727
Chandler Carruth2be10752015-10-21 12:15:19 +0000728namespace {
Eugene Zelenko530851c2017-08-11 21:30:02 +0000729
Eugene Zelenko530851c2017-08-11 21:30:02 +0000730
731} // end anonymous namespace
Chandler Carruth2be10752015-10-21 12:15:19 +0000732
733char ExternalAAWrapperPass::ID = 0;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000734
Chandler Carruth2be10752015-10-21 12:15:19 +0000735INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
736 false, true)
737
738ImmutablePass *
739llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
740 return new ExternalAAWrapperPass(std::move(Callback));
741}
742
Chandler Carruth7b560d42015-09-09 17:55:00 +0000743AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
744 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
745}
746
747char AAResultsWrapperPass::ID = 0;
748
749INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
750 "Function Alias Analysis Results", false, true)
751INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
George Burgess IVbfa401e2016-07-06 00:26:41 +0000752INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass)
753INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass)
Chandler Carruth2be10752015-10-21 12:15:19 +0000754INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000755INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
756INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
757INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
758INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
759INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
760INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
761 "Function Alias Analysis Results", false, true)
762
763FunctionPass *llvm::createAAResultsWrapperPass() {
764 return new AAResultsWrapperPass();
765}
766
767/// Run the wrapper pass to rebuild an aggregation over known AA passes.
768///
769/// This is the legacy pass manager's interface to the new-style AA results
770/// aggregation object. Because this is somewhat shoe-horned into the legacy
771/// pass manager, we hard code all the specific alias analyses available into
772/// it. While the particular set enabled is configured via commandline flags,
773/// adding a new alias analysis to LLVM will require adding support for it to
774/// this list.
775bool AAResultsWrapperPass::runOnFunction(Function &F) {
776 // NB! This *must* be reset before adding new AA results to the new
777 // AAResults object because in the legacy pass manager, each instance
778 // of these will refer to the *same* immutable analyses, registering and
779 // unregistering themselves with them. We need to carefully tear down the
780 // previous object first, in this case replacing it with an empty one, before
781 // registering new results.
Chandler Carruth12884f72016-03-02 15:56:53 +0000782 AAR.reset(
783 new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000784
785 // BasicAA is always available for function analyses. Also, we add it first
786 // so that it can trump TBAA results when it proves MustAlias.
787 // FIXME: TBAA should have an explicit mode to support this and then we
788 // should reconsider the ordering here.
789 if (!DisableBasicAA)
790 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
791
792 // Populate the results with the currently available AAs.
793 if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
794 AAR->addAAResult(WrapperPass->getResult());
795 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
796 AAR->addAAResult(WrapperPass->getResult());
797 if (auto *WrapperPass =
798 getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
799 AAR->addAAResult(WrapperPass->getResult());
800 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
801 AAR->addAAResult(WrapperPass->getResult());
802 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
803 AAR->addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000804 if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
805 AAR->addAAResult(WrapperPass->getResult());
806 if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000807 AAR->addAAResult(WrapperPass->getResult());
808
Chandler Carruth2be10752015-10-21 12:15:19 +0000809 // If available, run an external AA providing callback over the results as
810 // well.
811 if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
812 if (WrapperPass->CB)
813 WrapperPass->CB(*this, F, *AAR);
814
Chandler Carruth7b560d42015-09-09 17:55:00 +0000815 // Analyses don't mutate the IR, so return false.
816 return false;
817}
818
819void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
820 AU.setPreservesAll();
821 AU.addRequired<BasicAAWrapperPass>();
Chandler Carruth12884f72016-03-02 15:56:53 +0000822 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000823
824 // We also need to mark all the alias analysis passes we will potentially
825 // probe in runOnFunction as used here to ensure the legacy pass manager
826 // preserves them. This hard coding of lists of alias analyses is specific to
827 // the legacy pass manager.
828 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
829 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
830 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
831 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
832 AU.addUsedIfAvailable<SCEVAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000833 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
834 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000835}
836
837AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
838 BasicAAResult &BAR) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000839 AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000840
841 // Add in our explicitly constructed BasicAA results.
842 if (!DisableBasicAA)
843 AAR.addAAResult(BAR);
844
845 // Populate the results with the other currently available AAs.
846 if (auto *WrapperPass =
847 P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
848 AAR.addAAResult(WrapperPass->getResult());
849 if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
850 AAR.addAAResult(WrapperPass->getResult());
851 if (auto *WrapperPass =
852 P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
853 AAR.addAAResult(WrapperPass->getResult());
854 if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
855 AAR.addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000856 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
857 AAR.addAAResult(WrapperPass->getResult());
858 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000859 AAR.addAAResult(WrapperPass->getResult());
860
861 return AAR;
862}
863
Dan Gohmanfb306c02009-02-03 01:28:32 +0000864bool llvm::isNoAliasCall(const Value *V) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000865 if (const auto *Call = dyn_cast<CallBase>(V))
866 return Call->hasRetAttr(Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000867 return false;
868}
869
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000870bool llvm::isNoAliasArgument(const Value *V) {
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000871 if (const Argument *A = dyn_cast<Argument>(V))
872 return A->hasNoAliasAttr();
873 return false;
874}
875
Dan Gohman00ef9322010-07-07 14:27:09 +0000876bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000877 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000878 return true;
879 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000880 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000881 if (isNoAliasCall(V))
882 return true;
883 if (const Argument *A = dyn_cast<Argument>(V))
884 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000885 return false;
886}
Hal Finkelc782aa52014-07-21 12:27:23 +0000887
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000888bool llvm::isIdentifiedFunctionLocal(const Value *V) {
Hal Finkelc782aa52014-07-21 12:27:23 +0000889 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
890}
Sanjoy Das1c481f52016-02-09 01:21:57 +0000891
Chandler Carruth12884f72016-03-02 15:56:53 +0000892void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
Sanjoy Das1c481f52016-02-09 01:21:57 +0000893 // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
894 // more alias analyses are added to llvm::createLegacyPMAAResults, they need
895 // to be added here also.
Chandler Carruth12884f72016-03-02 15:56:53 +0000896 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000897 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
898 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
899 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
900 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000901 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
902 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000903}