Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 1 | //==- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation --==// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 6 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 8 | // |
| 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 Lattner | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CFLAndersAliasAnalysis.h" |
| 29 | #include "llvm/Analysis/CFLSteensAliasAnalysis.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/GlobalsModRef.h" |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/MemoryLocation.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/ObjCARCAliasAnalysis.h" |
| 34 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" |
| 35 | #include "llvm/Analysis/ScopedNoAliasAA.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/TypeBasedAliasAnalysis.h" |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Argument.h" |
| 40 | #include "llvm/IR/Attributes.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 41 | #include "llvm/IR/BasicBlock.h" |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Instructions.h" |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Module.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 45 | #include "llvm/IR/Type.h" |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Value.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 47 | #include "llvm/Pass.h" |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 48 | #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 Lattner | a67dbd0 | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 56 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 57 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 58 | /// Allow disabling BasicAA from the AA results. This is particularly useful |
| 59 | /// when testing to isolate a single AA implementation. |
| 60 | static cl::opt<bool> DisableBasicAA("disable-basicaa", cl::Hidden, |
| 61 | cl::init(false)); |
| 62 | |
Chandler Carruth | 17c630a | 2016-12-27 08:44:39 +0000 | [diff] [blame] | 63 | AAResults::AAResults(AAResults &&Arg) |
| 64 | : TLI(Arg.TLI), AAs(std::move(Arg.AAs)), AADeps(std::move(Arg.AADeps)) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 65 | for (auto &AA : AAs) |
| 66 | AA->setAAResults(this); |
| 67 | } |
| 68 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 69 | AAResults::~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 Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 79 | |
Chandler Carruth | 17c630a | 2016-12-27 08:44:39 +0000 | [diff] [blame] | 80 | bool AAResults::invalidate(Function &F, const PreservedAnalyses &PA, |
| 81 | FunctionAnalysisManager::Invalidator &Inv) { |
Chandler Carruth | 17c630a | 2016-12-27 08:44:39 +0000 | [diff] [blame] | 82 | // 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 Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 96 | //===----------------------------------------------------------------------===// |
| 97 | // Default chaining methods |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 100 | AliasResult AAResults::alias(const MemoryLocation &LocA, |
| 101 | const MemoryLocation &LocB) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 102 | AAQueryInfo AAQIP; |
| 103 | return alias(LocA, LocB, AAQIP); |
| 104 | } |
| 105 | |
| 106 | AliasResult AAResults::alias(const MemoryLocation &LocA, |
| 107 | const MemoryLocation &LocB, AAQueryInfo &AAQI) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 108 | for (const auto &AA : AAs) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 109 | auto Result = AA->alias(LocA, LocB, AAQI); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 110 | if (Result != MayAlias) |
| 111 | return Result; |
| 112 | } |
| 113 | return MayAlias; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 116 | bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc, |
| 117 | bool OrLocal) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 118 | AAQueryInfo AAQIP; |
| 119 | return pointsToConstantMemory(Loc, AAQIP, OrLocal); |
| 120 | } |
| 121 | |
| 122 | bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc, |
| 123 | AAQueryInfo &AAQI, bool OrLocal) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 124 | for (const auto &AA : AAs) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 125 | if (AA->pointsToConstantMemory(Loc, AAQI, OrLocal)) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 126 | return true; |
| 127 | |
| 128 | return false; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 131 | ModRefInfo AAResults::getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 132 | ModRefInfo Result = ModRefInfo::ModRef; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 133 | |
| 134 | for (const auto &AA : AAs) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 135 | Result = intersectModRef(Result, AA->getArgModRefInfo(Call, ArgIdx)); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 136 | |
| 137 | // Early-exit the moment we reach the bottom of the lattice. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 138 | if (isNoModRef(Result)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 139 | return ModRefInfo::NoModRef; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return Result; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 145 | ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 146 | AAQueryInfo AAQIP; |
| 147 | return getModRefInfo(I, Call2, AAQIP); |
| 148 | } |
| 149 | |
| 150 | ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2, |
| 151 | AAQueryInfo &AAQI) { |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 152 | // We may have two calls. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 153 | if (const auto *Call1 = dyn_cast<CallBase>(I)) { |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 154 | // Check if the two calls modify the same memory. |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 155 | return getModRefInfo(Call1, Call2, AAQI); |
David Majnemer | a940f36 | 2016-07-15 17:19:24 +0000 | [diff] [blame] | 156 | } else if (I->isFenceLike()) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 157 | // If this is a fence, just return ModRef. |
| 158 | return ModRefInfo::ModRef; |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 159 | } 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 Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 164 | const MemoryLocation DefLoc = MemoryLocation::get(I); |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 165 | ModRefInfo MR = getModRefInfo(Call2, DefLoc, AAQI); |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 166 | if (isModOrRefSet(MR)) |
| 167 | return setModAndRef(MR); |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 168 | } |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 169 | return ModRefInfo::NoModRef; |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 170 | } |
Owen Anderson | b6e4ff0 | 2011-01-03 21:38:41 +0000 | [diff] [blame] | 171 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 172 | ModRefInfo AAResults::getModRefInfo(const CallBase *Call, |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 173 | const MemoryLocation &Loc) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 174 | AAQueryInfo AAQIP; |
| 175 | return getModRefInfo(Call, Loc, AAQIP); |
| 176 | } |
| 177 | |
| 178 | ModRefInfo AAResults::getModRefInfo(const CallBase *Call, |
| 179 | const MemoryLocation &Loc, |
| 180 | AAQueryInfo &AAQI) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 181 | ModRefInfo Result = ModRefInfo::ModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 182 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 183 | for (const auto &AA : AAs) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 184 | Result = intersectModRef(Result, AA->getModRefInfo(Call, Loc, AAQI)); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 185 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 186 | // Early-exit the moment we reach the bottom of the lattice. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 187 | if (isNoModRef(Result)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 188 | return ModRefInfo::NoModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 191 | // Try to refine the mod-ref info further using other API entry points to the |
| 192 | // aggregate set of AA results. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 193 | auto MRB = getModRefBehavior(Call); |
Andrew Kaylor | 9604f34 | 2016-11-08 21:07:42 +0000 | [diff] [blame] | 194 | if (MRB == FMRB_DoesNotAccessMemory || |
| 195 | MRB == FMRB_OnlyAccessesInaccessibleMem) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 196 | return ModRefInfo::NoModRef; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 197 | |
| 198 | if (onlyReadsMemory(MRB)) |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 199 | Result = clearMod(Result); |
Nicolai Haehnle | 84c9f99 | 2016-07-04 08:01:29 +0000 | [diff] [blame] | 200 | else if (doesNotReadMemory(MRB)) |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 201 | Result = clearRef(Result); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 202 | |
Andrew Kaylor | 9604f34 | 2016-11-08 21:07:42 +0000 | [diff] [blame] | 203 | if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) { |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 204 | bool IsMustAlias = true; |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 205 | ModRefInfo AllArgsMask = ModRefInfo::NoModRef; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 206 | if (doesAccessArgPointees(MRB)) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 207 | for (auto AI = Call->arg_begin(), AE = Call->arg_end(); AI != AE; ++AI) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 208 | const Value *Arg = *AI; |
| 209 | if (!Arg->getType()->isPointerTy()) |
| 210 | continue; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 211 | unsigned ArgIdx = std::distance(Call->arg_begin(), AI); |
| 212 | MemoryLocation ArgLoc = |
| 213 | MemoryLocation::getForArgument(Call, ArgIdx, TLI); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 214 | AliasResult ArgAlias = alias(ArgLoc, Loc); |
| 215 | if (ArgAlias != NoAlias) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 216 | ModRefInfo ArgMask = getArgModRefInfo(Call, ArgIdx); |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 217 | AllArgsMask = unionModRef(AllArgsMask, ArgMask); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 218 | } |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 219 | // Conservatively clear IsMustAlias unless only MustAlias is found. |
| 220 | IsMustAlias &= (ArgAlias == MustAlias); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 223 | // Return NoModRef if no alias found with any argument. |
Philip Reames | 8abf448 | 2018-08-22 19:50:45 +0000 | [diff] [blame] | 224 | if (isNoModRef(AllArgsMask)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 225 | return ModRefInfo::NoModRef; |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 226 | // Logical & between other AA analyses and argument analysis. |
| 227 | Result = intersectModRef(Result, AllArgsMask); |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 228 | // If only MustAlias found above, set Must bit. |
| 229 | Result = IsMustAlias ? setMust(Result) : clearMust(Result); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // If Loc is a constant memory location, the call definitely could not |
| 233 | // modify the memory location. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 234 | if (isModSet(Result) && pointsToConstantMemory(Loc, /*OrLocal*/ false)) |
| 235 | Result = clearMod(Result); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 236 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 237 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 240 | ModRefInfo AAResults::getModRefInfo(const CallBase *Call1, |
| 241 | const CallBase *Call2) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 242 | AAQueryInfo AAQIP; |
| 243 | return getModRefInfo(Call1, Call2, AAQIP); |
| 244 | } |
| 245 | |
| 246 | ModRefInfo AAResults::getModRefInfo(const CallBase *Call1, |
| 247 | const CallBase *Call2, AAQueryInfo &AAQI) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 248 | ModRefInfo Result = ModRefInfo::ModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 249 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 250 | for (const auto &AA : AAs) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 251 | Result = intersectModRef(Result, AA->getModRefInfo(Call1, Call2, AAQI)); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 252 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 253 | // Early-exit the moment we reach the bottom of the lattice. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 254 | if (isNoModRef(Result)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 255 | return ModRefInfo::NoModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 258 | // Try to refine the mod-ref info further using other API entry points to the |
| 259 | // aggregate set of AA results. |
| 260 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 261 | // If Call1 or Call2 are readnone, they don't interact. |
| 262 | auto Call1B = getModRefBehavior(Call1); |
| 263 | if (Call1B == FMRB_DoesNotAccessMemory) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 264 | return ModRefInfo::NoModRef; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 265 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 266 | auto Call2B = getModRefBehavior(Call2); |
| 267 | if (Call2B == FMRB_DoesNotAccessMemory) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 268 | return ModRefInfo::NoModRef; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 269 | |
| 270 | // If they both only read from memory, there is no dependence. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 271 | if (onlyReadsMemory(Call1B) && onlyReadsMemory(Call2B)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 272 | return ModRefInfo::NoModRef; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 273 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 274 | // 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 Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 277 | Result = clearMod(Result); |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 278 | else if (doesNotReadMemory(Call1B)) |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 279 | Result = clearRef(Result); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 280 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 281 | // 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 Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 286 | return ModRefInfo::NoModRef; |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 287 | ModRefInfo R = ModRefInfo::NoModRef; |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 288 | bool IsMustAlias = true; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 289 | for (auto I = Call2->arg_begin(), E = Call2->arg_end(); I != E; ++I) { |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 290 | const Value *Arg = *I; |
| 291 | if (!Arg->getType()->isPointerTy()) |
| 292 | continue; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 293 | unsigned Call2ArgIdx = std::distance(Call2->arg_begin(), I); |
| 294 | auto Call2ArgLoc = |
| 295 | MemoryLocation::getForArgument(Call2, Call2ArgIdx, TLI); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 296 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 297 | // 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 Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 303 | ModRefInfo ArgMask = ModRefInfo::NoModRef; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 304 | if (isModSet(ArgModRefC2)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 305 | ArgMask = ModRefInfo::ModRef; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 306 | else if (isRefSet(ArgModRefC2)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 307 | ArgMask = ModRefInfo::Mod; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 308 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 309 | // ModRefC1 indicates what Call1 might do to Call2ArgLoc, and we use |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 310 | // above ArgMask to update dependence info. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 311 | ModRefInfo ModRefC1 = getModRefInfo(Call1, Call2ArgLoc); |
| 312 | ArgMask = intersectModRef(ArgMask, ModRefC1); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 313 | |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 314 | // Conservatively clear IsMustAlias unless only MustAlias is found. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 315 | IsMustAlias &= isMustSet(ModRefC1); |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 316 | |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 317 | 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 Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 325 | |
| 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 Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 333 | // 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 Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 337 | return ModRefInfo::NoModRef; |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 338 | ModRefInfo R = ModRefInfo::NoModRef; |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 339 | bool IsMustAlias = true; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 340 | for (auto I = Call1->arg_begin(), E = Call1->arg_end(); I != E; ++I) { |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 341 | const Value *Arg = *I; |
| 342 | if (!Arg->getType()->isPointerTy()) |
| 343 | continue; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 344 | unsigned Call1ArgIdx = std::distance(Call1->arg_begin(), I); |
| 345 | auto Call1ArgLoc = |
| 346 | MemoryLocation::getForArgument(Call1, Call1ArgIdx, TLI); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 347 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 348 | // 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 Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 356 | |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 357 | // Conservatively clear IsMustAlias unless only MustAlias is found. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 358 | IsMustAlias &= isMustSet(ModRefC2); |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 359 | |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 360 | 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 Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 367 | |
| 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 Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 375 | return Result; |
| 376 | } |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 377 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 378 | FunctionModRefBehavior AAResults::getModRefBehavior(const CallBase *Call) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 379 | FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 380 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 381 | for (const auto &AA : AAs) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 382 | Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(Call)); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 383 | |
| 384 | // Early-exit the moment we reach the bottom of the lattice. |
| 385 | if (Result == FMRB_DoesNotAccessMemory) |
| 386 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 389 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 392 | FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) { |
| 393 | FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 394 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 395 | for (const auto &AA : AAs) { |
| 396 | Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F)); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 397 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 398 | // Early-exit the moment we reach the bottom of the lattice. |
| 399 | if (Result == FMRB_DoesNotAccessMemory) |
| 400 | return Result; |
| 401 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 402 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 403 | return Result; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 404 | } |
| 405 | |
George Burgess IV | aa283d8 | 2018-06-14 19:55:53 +0000 | [diff] [blame] | 406 | raw_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 Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 424 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 425 | // Helper method implementation |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 426 | //===----------------------------------------------------------------------===// |
| 427 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 428 | ModRefInfo AAResults::getModRefInfo(const LoadInst *L, |
| 429 | const MemoryLocation &Loc) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 430 | AAQueryInfo AAQIP; |
| 431 | return getModRefInfo(L, Loc, AAQIP); |
| 432 | } |
| 433 | ModRefInfo AAResults::getModRefInfo(const LoadInst *L, |
| 434 | const MemoryLocation &Loc, |
| 435 | AAQueryInfo &AAQI) { |
Daniel Berlin | d952cea | 2017-04-07 01:28:36 +0000 | [diff] [blame] | 436 | // Be conservative in the face of atomic. |
| 437 | if (isStrongerThan(L->getOrdering(), AtomicOrdering::Unordered)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 438 | return ModRefInfo::ModRef; |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 439 | |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 440 | // If the load address doesn't alias the given address, it doesn't read |
| 441 | // or write the specified memory. |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 442 | if (Loc.Ptr) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 443 | AliasResult AR = alias(MemoryLocation::get(L), Loc, AAQI); |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 444 | if (AR == NoAlias) |
| 445 | return ModRefInfo::NoModRef; |
| 446 | if (AR == MustAlias) |
| 447 | return ModRefInfo::MustRef; |
| 448 | } |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 449 | // Otherwise, a load just reads. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 450 | return ModRefInfo::Ref; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 453 | ModRefInfo AAResults::getModRefInfo(const StoreInst *S, |
| 454 | const MemoryLocation &Loc) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 455 | AAQueryInfo AAQIP; |
| 456 | return getModRefInfo(S, Loc, AAQIP); |
| 457 | } |
| 458 | ModRefInfo AAResults::getModRefInfo(const StoreInst *S, |
| 459 | const MemoryLocation &Loc, |
| 460 | AAQueryInfo &AAQI) { |
Daniel Berlin | d952cea | 2017-04-07 01:28:36 +0000 | [diff] [blame] | 461 | // Be conservative in the face of atomic. |
| 462 | if (isStrongerThan(S->getOrdering(), AtomicOrdering::Unordered)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 463 | return ModRefInfo::ModRef; |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 464 | |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 465 | if (Loc.Ptr) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 466 | AliasResult AR = alias(MemoryLocation::get(S), Loc, AAQI); |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 467 | // If the store address cannot alias the pointer in question, then the |
| 468 | // specified memory cannot be modified by the store. |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 469 | if (AR == NoAlias) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 470 | return ModRefInfo::NoModRef; |
Chris Lattner | 9605576 | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 471 | |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 472 | // If the pointer is a pointer to constant memory, then it could not have |
| 473 | // been modified by this store. |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 474 | if (pointsToConstantMemory(Loc, AAQI)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 475 | return ModRefInfo::NoModRef; |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 476 | |
| 477 | // If the store address aliases the pointer as must alias, set Must. |
| 478 | if (AR == MustAlias) |
| 479 | return ModRefInfo::MustMod; |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 480 | } |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 481 | |
| 482 | // Otherwise, a store just writes. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 483 | return ModRefInfo::Mod; |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Anna Thomas | 698f0de | 2017-01-20 00:21:33 +0000 | [diff] [blame] | 486 | ModRefInfo AAResults::getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 487 | AAQueryInfo AAQIP; |
| 488 | return getModRefInfo(S, Loc, AAQIP); |
| 489 | } |
| 490 | |
| 491 | ModRefInfo AAResults::getModRefInfo(const FenceInst *S, |
| 492 | const MemoryLocation &Loc, |
| 493 | AAQueryInfo &AAQI) { |
Anna Thomas | 698f0de | 2017-01-20 00:21:33 +0000 | [diff] [blame] | 494 | // If we know that the location is a constant memory location, the fence |
| 495 | // cannot modify this location. |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 496 | if (Loc.Ptr && pointsToConstantMemory(Loc, AAQI)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 497 | return ModRefInfo::Ref; |
| 498 | return ModRefInfo::ModRef; |
Anna Thomas | 698f0de | 2017-01-20 00:21:33 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 501 | ModRefInfo AAResults::getModRefInfo(const VAArgInst *V, |
| 502 | const MemoryLocation &Loc) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 503 | AAQueryInfo AAQIP; |
| 504 | return getModRefInfo(V, Loc, AAQIP); |
| 505 | } |
| 506 | |
| 507 | ModRefInfo AAResults::getModRefInfo(const VAArgInst *V, |
| 508 | const MemoryLocation &Loc, |
| 509 | AAQueryInfo &AAQI) { |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 510 | if (Loc.Ptr) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 511 | AliasResult AR = alias(MemoryLocation::get(V), Loc, AAQI); |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 512 | // If the va_arg address cannot alias the pointer in question, then the |
| 513 | // specified memory cannot be accessed by the va_arg. |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 514 | if (AR == NoAlias) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 515 | return ModRefInfo::NoModRef; |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 516 | |
| 517 | // If the pointer is a pointer to constant memory, then it could not have |
| 518 | // been modified by this va_arg. |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 519 | if (pointsToConstantMemory(Loc, AAQI)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 520 | return ModRefInfo::NoModRef; |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 521 | |
| 522 | // If the va_arg aliases the pointer as must alias, set Must. |
| 523 | if (AR == MustAlias) |
| 524 | return ModRefInfo::MustModRef; |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 525 | } |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 526 | |
| 527 | // Otherwise, a va_arg reads and writes. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 528 | return ModRefInfo::ModRef; |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 529 | } |
| 530 | |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 531 | ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad, |
| 532 | const MemoryLocation &Loc) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 533 | AAQueryInfo AAQIP; |
| 534 | return getModRefInfo(CatchPad, Loc, AAQIP); |
| 535 | } |
| 536 | |
| 537 | ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad, |
| 538 | const MemoryLocation &Loc, |
| 539 | AAQueryInfo &AAQI) { |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 540 | 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 Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 543 | if (pointsToConstantMemory(Loc, AAQI)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 544 | return ModRefInfo::NoModRef; |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // Otherwise, a catchpad reads and writes. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 548 | return ModRefInfo::ModRef; |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet, |
| 552 | const MemoryLocation &Loc) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 553 | AAQueryInfo AAQIP; |
| 554 | return getModRefInfo(CatchRet, Loc, AAQIP); |
| 555 | } |
| 556 | |
| 557 | ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet, |
| 558 | const MemoryLocation &Loc, |
| 559 | AAQueryInfo &AAQI) { |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 560 | 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 Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 563 | if (pointsToConstantMemory(Loc, AAQI)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 564 | return ModRefInfo::NoModRef; |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | // Otherwise, a catchret reads and writes. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 568 | return ModRefInfo::ModRef; |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 571 | ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX, |
| 572 | const MemoryLocation &Loc) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 573 | AAQueryInfo AAQIP; |
| 574 | return getModRefInfo(CX, Loc, AAQIP); |
| 575 | } |
| 576 | |
| 577 | ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX, |
| 578 | const MemoryLocation &Loc, |
| 579 | AAQueryInfo &AAQI) { |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 580 | // Acquire/Release cmpxchg has properties that matter for arbitrary addresses. |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 581 | if (isStrongerThanMonotonic(CX->getSuccessOrdering())) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 582 | return ModRefInfo::ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 583 | |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 584 | if (Loc.Ptr) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 585 | AliasResult AR = alias(MemoryLocation::get(CX), Loc, AAQI); |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 586 | // 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 Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 595 | |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 596 | return ModRefInfo::ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 599 | ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW, |
| 600 | const MemoryLocation &Loc) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 601 | AAQueryInfo AAQIP; |
| 602 | return getModRefInfo(RMW, Loc, AAQIP); |
| 603 | } |
| 604 | |
| 605 | ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW, |
| 606 | const MemoryLocation &Loc, |
| 607 | AAQueryInfo &AAQI) { |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 608 | // Acquire/Release atomicrmw has properties that matter for arbitrary addresses. |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 609 | if (isStrongerThanMonotonic(RMW->getOrdering())) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 610 | return ModRefInfo::ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 611 | |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 612 | if (Loc.Ptr) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 613 | AliasResult AR = alias(MemoryLocation::get(RMW), Loc, AAQI); |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 614 | // 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 Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 623 | |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 624 | return ModRefInfo::ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 627 | /// Return information about whether a particular call site modifies |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 628 | /// or reads the specified memory location \p MemLoc before instruction \p I |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 629 | /// in a BasicBlock. An ordered basic block \p OBB can be used to speed up |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 630 | /// 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 Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 635 | ModRefInfo AAResults::callCapturesBefore(const Instruction *I, |
| 636 | const MemoryLocation &MemLoc, |
| 637 | DominatorTree *DT, |
| 638 | OrderedBasicBlock *OBB) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 639 | if (!DT) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 640 | return ModRefInfo::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 641 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 642 | const Value *Object = |
| 643 | GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout()); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 644 | if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) || |
| 645 | isa<Constant>(Object)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 646 | return ModRefInfo::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 647 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 648 | const auto *Call = dyn_cast<CallBase>(I); |
| 649 | if (!Call || Call == Object) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 650 | return ModRefInfo::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 651 | |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 652 | if (PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true, |
| 653 | /* StoreCaptures */ true, I, DT, |
| 654 | /* include Object */ true, |
| 655 | /* OrderedBasicBlock */ OBB)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 656 | return ModRefInfo::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 657 | |
| 658 | unsigned ArgNo = 0; |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 659 | ModRefInfo R = ModRefInfo::NoModRef; |
Alina Sbirlea | 020363e | 2018-04-30 20:11:13 +0000 | [diff] [blame] | 660 | bool IsMustAlias = true; |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 661 | // Set flag only if no May found and all operands processed. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 662 | for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end(); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 663 | 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 Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 668 | (!Call->doesNotCapture(ArgNo) && ArgNo < Call->getNumArgOperands() && |
| 669 | !Call->isByValArgument(ArgNo))) |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 670 | continue; |
| 671 | |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 672 | AliasResult AR = alias(MemoryLocation(*CI), MemoryLocation(Object)); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 673 | // 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 Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 677 | if (AR != MustAlias) |
Alina Sbirlea | 020363e | 2018-04-30 20:11:13 +0000 | [diff] [blame] | 678 | IsMustAlias = false; |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 679 | if (AR == NoAlias) |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 680 | continue; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 681 | if (Call->doesNotAccessMemory(ArgNo)) |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 682 | continue; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 683 | if (Call->onlyReadsMemory(ArgNo)) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 684 | R = ModRefInfo::Ref; |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 685 | continue; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 686 | } |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 687 | // Not returning MustModRef since we have not seen all the arguments. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 688 | return ModRefInfo::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 689 | } |
Alina Sbirlea | 020363e | 2018-04-30 20:11:13 +0000 | [diff] [blame] | 690 | return IsMustAlias ? setMust(R) : clearMust(R); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 691 | } |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 692 | |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 693 | /// canBasicBlockModify - Return true if it is possible for execution of the |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 694 | /// specified basic block to modify the location Loc. |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 695 | /// |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 696 | bool AAResults::canBasicBlockModify(const BasicBlock &BB, |
| 697 | const MemoryLocation &Loc) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 698 | return canInstructionRangeModRef(BB.front(), BB.back(), Loc, ModRefInfo::Mod); |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 701 | /// 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 Johnson | bbcf75e | 2015-05-13 15:04:14 +0000 | [diff] [blame] | 705 | /// I1 and I2 must be in the same basic block. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 706 | bool AAResults::canInstructionRangeModRef(const Instruction &I1, |
| 707 | const Instruction &I2, |
| 708 | const MemoryLocation &Loc, |
| 709 | const ModRefInfo Mode) { |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 710 | assert(I1.getParent() == I2.getParent() && |
| 711 | "Instructions not in same basic block!"); |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 712 | BasicBlock::const_iterator I = I1.getIterator(); |
| 713 | BasicBlock::const_iterator E = I2.getIterator(); |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 714 | ++E; // Convert from inclusive to exclusive range. |
| 715 | |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 716 | for (; I != E; ++I) // Check every instruction in range |
Alina Sbirlea | 18fea01 | 2017-12-06 19:56:37 +0000 | [diff] [blame] | 717 | if (isModOrRefSet(intersectModRef(getModRefInfo(&*I, Loc), Mode))) |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 718 | return true; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 719 | return false; |
| 720 | } |
| 721 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 722 | // Provide a definition for the root virtual destructor. |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 723 | AAResults::Concept::~Concept() = default; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 724 | |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 725 | // Provide a definition for the static object used to identify passes. |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 726 | AnalysisKey AAManager::Key; |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 727 | |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 728 | namespace { |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 729 | |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 730 | |
| 731 | } // end anonymous namespace |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 732 | |
| 733 | char ExternalAAWrapperPass::ID = 0; |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 734 | |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 735 | INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis", |
| 736 | false, true) |
| 737 | |
| 738 | ImmutablePass * |
| 739 | llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) { |
| 740 | return new ExternalAAWrapperPass(std::move(Callback)); |
| 741 | } |
| 742 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 743 | AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) { |
| 744 | initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 745 | } |
| 746 | |
| 747 | char AAResultsWrapperPass::ID = 0; |
| 748 | |
| 749 | INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa", |
| 750 | "Function Alias Analysis Results", false, true) |
| 751 | INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass) |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 752 | INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass) |
| 753 | INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass) |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 754 | INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 755 | INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) |
| 756 | INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass) |
| 757 | INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass) |
| 758 | INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass) |
| 759 | INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass) |
| 760 | INITIALIZE_PASS_END(AAResultsWrapperPass, "aa", |
| 761 | "Function Alias Analysis Results", false, true) |
| 762 | |
| 763 | FunctionPass *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. |
| 775 | bool 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 Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 782 | AAR.reset( |
| 783 | new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI())); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 784 | |
| 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 IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 804 | if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>()) |
| 805 | AAR->addAAResult(WrapperPass->getResult()); |
| 806 | if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>()) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 807 | AAR->addAAResult(WrapperPass->getResult()); |
| 808 | |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 809 | // 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 Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 815 | // Analyses don't mutate the IR, so return false. |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 820 | AU.setPreservesAll(); |
| 821 | AU.addRequired<BasicAAWrapperPass>(); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 822 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 823 | |
| 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 IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 833 | AU.addUsedIfAvailable<CFLAndersAAWrapperPass>(); |
| 834 | AU.addUsedIfAvailable<CFLSteensAAWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F, |
| 838 | BasicAAResult &BAR) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 839 | AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 840 | |
| 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 IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 856 | if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>()) |
| 857 | AAR.addAAResult(WrapperPass->getResult()); |
| 858 | if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>()) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 859 | AAR.addAAResult(WrapperPass->getResult()); |
| 860 | |
| 861 | return AAR; |
| 862 | } |
| 863 | |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 864 | bool llvm::isNoAliasCall(const Value *V) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 865 | if (const auto *Call = dyn_cast<CallBase>(V)) |
| 866 | return Call->hasRetAttr(Attribute::NoAlias); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 867 | return false; |
| 868 | } |
| 869 | |
Sanjay Patel | d7f613d | 2016-01-13 22:17:13 +0000 | [diff] [blame] | 870 | bool llvm::isNoAliasArgument(const Value *V) { |
Michael Kuperstein | f3e663a | 2013-05-28 08:17:48 +0000 | [diff] [blame] | 871 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 872 | return A->hasNoAliasAttr(); |
| 873 | return false; |
| 874 | } |
| 875 | |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 876 | bool llvm::isIdentifiedObject(const Value *V) { |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 877 | if (isa<AllocaInst>(V)) |
Dan Gohman | 1f59f01 | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 878 | return true; |
| 879 | if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 880 | return true; |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 881 | if (isNoAliasCall(V)) |
| 882 | return true; |
| 883 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 884 | return A->hasNoAliasAttr() || A->hasByValAttr(); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 885 | return false; |
| 886 | } |
Hal Finkel | c782aa5 | 2014-07-21 12:27:23 +0000 | [diff] [blame] | 887 | |
Sanjay Patel | d7f613d | 2016-01-13 22:17:13 +0000 | [diff] [blame] | 888 | bool llvm::isIdentifiedFunctionLocal(const Value *V) { |
Hal Finkel | c782aa5 | 2014-07-21 12:27:23 +0000 | [diff] [blame] | 889 | return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V); |
| 890 | } |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 891 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 892 | void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) { |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 893 | // 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 Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 896 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 897 | AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>(); |
| 898 | AU.addUsedIfAvailable<TypeBasedAAWrapperPass>(); |
| 899 | AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>(); |
| 900 | AU.addUsedIfAvailable<GlobalsAAWrapperPass>(); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 901 | AU.addUsedIfAvailable<CFLAndersAAWrapperPass>(); |
| 902 | AU.addUsedIfAvailable<CFLSteensAAWrapperPass>(); |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 903 | } |