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) { |
| 102 | for (const auto &AA : AAs) { |
| 103 | auto Result = AA->alias(LocA, LocB); |
| 104 | if (Result != MayAlias) |
| 105 | return Result; |
| 106 | } |
| 107 | return MayAlias; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 110 | bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc, |
| 111 | bool OrLocal) { |
| 112 | for (const auto &AA : AAs) |
| 113 | if (AA->pointsToConstantMemory(Loc, OrLocal)) |
| 114 | return true; |
| 115 | |
| 116 | return false; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 119 | ModRefInfo AAResults::getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 120 | ModRefInfo Result = ModRefInfo::ModRef; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 121 | |
| 122 | for (const auto &AA : AAs) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 123 | Result = intersectModRef(Result, AA->getArgModRefInfo(Call, ArgIdx)); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 124 | |
| 125 | // Early-exit the moment we reach the bottom of the lattice. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 126 | if (isNoModRef(Result)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 127 | return ModRefInfo::NoModRef; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return Result; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 133 | ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2) { |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 134 | // We may have two calls. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 135 | if (const auto *Call1 = dyn_cast<CallBase>(I)) { |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 136 | // Check if the two calls modify the same memory. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 137 | return getModRefInfo(Call1, Call2); |
David Majnemer | a940f36 | 2016-07-15 17:19:24 +0000 | [diff] [blame] | 138 | } else if (I->isFenceLike()) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 139 | // If this is a fence, just return ModRef. |
| 140 | return ModRefInfo::ModRef; |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 141 | } else { |
| 142 | // Otherwise, check if the call modifies or references the |
| 143 | // location this memory access defines. The best we can say |
| 144 | // is that if the call references what this instruction |
| 145 | // defines, it must be clobbered by this location. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 146 | const MemoryLocation DefLoc = MemoryLocation::get(I); |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 147 | ModRefInfo MR = getModRefInfo(Call2, DefLoc); |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 148 | if (isModOrRefSet(MR)) |
| 149 | return setModAndRef(MR); |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 150 | } |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 151 | return ModRefInfo::NoModRef; |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 152 | } |
Owen Anderson | b6e4ff0 | 2011-01-03 21:38:41 +0000 | [diff] [blame] | 153 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 154 | ModRefInfo AAResults::getModRefInfo(const CallBase *Call, |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 155 | const MemoryLocation &Loc) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 156 | ModRefInfo Result = ModRefInfo::ModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 157 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 158 | for (const auto &AA : AAs) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 159 | Result = intersectModRef(Result, AA->getModRefInfo(Call, Loc)); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 160 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 161 | // Early-exit the moment we reach the bottom of the lattice. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 162 | if (isNoModRef(Result)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 163 | return ModRefInfo::NoModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 166 | // Try to refine the mod-ref info further using other API entry points to the |
| 167 | // aggregate set of AA results. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 168 | auto MRB = getModRefBehavior(Call); |
Andrew Kaylor | 9604f34 | 2016-11-08 21:07:42 +0000 | [diff] [blame] | 169 | if (MRB == FMRB_DoesNotAccessMemory || |
| 170 | MRB == FMRB_OnlyAccessesInaccessibleMem) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 171 | return ModRefInfo::NoModRef; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 172 | |
| 173 | if (onlyReadsMemory(MRB)) |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 174 | Result = clearMod(Result); |
Nicolai Haehnle | 84c9f99 | 2016-07-04 08:01:29 +0000 | [diff] [blame] | 175 | else if (doesNotReadMemory(MRB)) |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 176 | Result = clearRef(Result); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 177 | |
Andrew Kaylor | 9604f34 | 2016-11-08 21:07:42 +0000 | [diff] [blame] | 178 | if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) { |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 179 | bool IsMustAlias = true; |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 180 | ModRefInfo AllArgsMask = ModRefInfo::NoModRef; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 181 | if (doesAccessArgPointees(MRB)) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 182 | 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] | 183 | const Value *Arg = *AI; |
| 184 | if (!Arg->getType()->isPointerTy()) |
| 185 | continue; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 186 | unsigned ArgIdx = std::distance(Call->arg_begin(), AI); |
| 187 | MemoryLocation ArgLoc = |
| 188 | MemoryLocation::getForArgument(Call, ArgIdx, TLI); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 189 | AliasResult ArgAlias = alias(ArgLoc, Loc); |
| 190 | if (ArgAlias != NoAlias) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 191 | ModRefInfo ArgMask = getArgModRefInfo(Call, ArgIdx); |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 192 | AllArgsMask = unionModRef(AllArgsMask, ArgMask); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 193 | } |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 194 | // Conservatively clear IsMustAlias unless only MustAlias is found. |
| 195 | IsMustAlias &= (ArgAlias == MustAlias); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 198 | // Return NoModRef if no alias found with any argument. |
Philip Reames | 8abf448 | 2018-08-22 19:50:45 +0000 | [diff] [blame] | 199 | if (isNoModRef(AllArgsMask)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 200 | return ModRefInfo::NoModRef; |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 201 | // Logical & between other AA analyses and argument analysis. |
| 202 | Result = intersectModRef(Result, AllArgsMask); |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 203 | // If only MustAlias found above, set Must bit. |
| 204 | Result = IsMustAlias ? setMust(Result) : clearMust(Result); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // If Loc is a constant memory location, the call definitely could not |
| 208 | // modify the memory location. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 209 | if (isModSet(Result) && pointsToConstantMemory(Loc, /*OrLocal*/ false)) |
| 210 | Result = clearMod(Result); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 211 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 212 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 215 | ModRefInfo AAResults::getModRefInfo(const CallBase *Call1, |
| 216 | const CallBase *Call2) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 217 | ModRefInfo Result = ModRefInfo::ModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 218 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 219 | for (const auto &AA : AAs) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 220 | Result = intersectModRef(Result, AA->getModRefInfo(Call1, Call2)); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 221 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 222 | // Early-exit the moment we reach the bottom of the lattice. |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 223 | if (isNoModRef(Result)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 224 | return ModRefInfo::NoModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 227 | // Try to refine the mod-ref info further using other API entry points to the |
| 228 | // aggregate set of AA results. |
| 229 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 230 | // If Call1 or Call2 are readnone, they don't interact. |
| 231 | auto Call1B = getModRefBehavior(Call1); |
| 232 | if (Call1B == FMRB_DoesNotAccessMemory) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 233 | return ModRefInfo::NoModRef; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 234 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 235 | auto Call2B = getModRefBehavior(Call2); |
| 236 | if (Call2B == FMRB_DoesNotAccessMemory) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 237 | return ModRefInfo::NoModRef; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 238 | |
| 239 | // If they both only read from memory, there is no dependence. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 240 | if (onlyReadsMemory(Call1B) && onlyReadsMemory(Call2B)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 241 | return ModRefInfo::NoModRef; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 242 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 243 | // If Call1 only reads memory, the only dependence on Call2 can be |
| 244 | // from Call1 reading memory written by Call2. |
| 245 | if (onlyReadsMemory(Call1B)) |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 246 | Result = clearMod(Result); |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 247 | else if (doesNotReadMemory(Call1B)) |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 248 | Result = clearRef(Result); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 249 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 250 | // If Call2 only access memory through arguments, accumulate the mod/ref |
| 251 | // information from Call1's references to the memory referenced by |
| 252 | // Call2's arguments. |
| 253 | if (onlyAccessesArgPointees(Call2B)) { |
| 254 | if (!doesAccessArgPointees(Call2B)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 255 | return ModRefInfo::NoModRef; |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 256 | ModRefInfo R = ModRefInfo::NoModRef; |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 257 | bool IsMustAlias = true; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 258 | 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] | 259 | const Value *Arg = *I; |
| 260 | if (!Arg->getType()->isPointerTy()) |
| 261 | continue; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 262 | unsigned Call2ArgIdx = std::distance(Call2->arg_begin(), I); |
| 263 | auto Call2ArgLoc = |
| 264 | MemoryLocation::getForArgument(Call2, Call2ArgIdx, TLI); |
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 | // ArgModRefC2 indicates what Call2 might do to Call2ArgLoc, and the |
| 267 | // dependence of Call1 on that location is the inverse: |
| 268 | // - If Call2 modifies location, dependence exists if Call1 reads or |
| 269 | // writes. |
| 270 | // - If Call2 only reads location, dependence exists if Call1 writes. |
| 271 | ModRefInfo ArgModRefC2 = getArgModRefInfo(Call2, Call2ArgIdx); |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 272 | ModRefInfo ArgMask = ModRefInfo::NoModRef; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 273 | if (isModSet(ArgModRefC2)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 274 | ArgMask = ModRefInfo::ModRef; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 275 | else if (isRefSet(ArgModRefC2)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 276 | ArgMask = ModRefInfo::Mod; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 277 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 278 | // ModRefC1 indicates what Call1 might do to Call2ArgLoc, and we use |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 279 | // above ArgMask to update dependence info. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 280 | ModRefInfo ModRefC1 = getModRefInfo(Call1, Call2ArgLoc); |
| 281 | ArgMask = intersectModRef(ArgMask, ModRefC1); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 282 | |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 283 | // Conservatively clear IsMustAlias unless only MustAlias is found. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 284 | IsMustAlias &= isMustSet(ModRefC1); |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 285 | |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 286 | R = intersectModRef(unionModRef(R, ArgMask), Result); |
| 287 | if (R == Result) { |
| 288 | // On early exit, not all args were checked, cannot set Must. |
| 289 | if (I + 1 != E) |
| 290 | IsMustAlias = false; |
| 291 | break; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 294 | |
| 295 | if (isNoModRef(R)) |
| 296 | return ModRefInfo::NoModRef; |
| 297 | |
| 298 | // If MustAlias found above, set Must bit. |
| 299 | return IsMustAlias ? setMust(R) : clearMust(R); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 302 | // If Call1 only accesses memory through arguments, check if Call2 references |
| 303 | // any of the memory referenced by Call1's arguments. If not, return NoModRef. |
| 304 | if (onlyAccessesArgPointees(Call1B)) { |
| 305 | if (!doesAccessArgPointees(Call1B)) |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 306 | return ModRefInfo::NoModRef; |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 307 | ModRefInfo R = ModRefInfo::NoModRef; |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 308 | bool IsMustAlias = true; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 309 | 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] | 310 | const Value *Arg = *I; |
| 311 | if (!Arg->getType()->isPointerTy()) |
| 312 | continue; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 313 | unsigned Call1ArgIdx = std::distance(Call1->arg_begin(), I); |
| 314 | auto Call1ArgLoc = |
| 315 | MemoryLocation::getForArgument(Call1, Call1ArgIdx, TLI); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 316 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 317 | // ArgModRefC1 indicates what Call1 might do to Call1ArgLoc; if Call1 |
| 318 | // might Mod Call1ArgLoc, then we care about either a Mod or a Ref by |
| 319 | // Call2. If Call1 might Ref, then we care only about a Mod by Call2. |
| 320 | ModRefInfo ArgModRefC1 = getArgModRefInfo(Call1, Call1ArgIdx); |
| 321 | ModRefInfo ModRefC2 = getModRefInfo(Call2, Call1ArgLoc); |
| 322 | if ((isModSet(ArgModRefC1) && isModOrRefSet(ModRefC2)) || |
| 323 | (isRefSet(ArgModRefC1) && isModSet(ModRefC2))) |
| 324 | R = intersectModRef(unionModRef(R, ArgModRefC1), Result); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 325 | |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 326 | // Conservatively clear IsMustAlias unless only MustAlias is found. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 327 | IsMustAlias &= isMustSet(ModRefC2); |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 328 | |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 329 | if (R == Result) { |
| 330 | // On early exit, not all args were checked, cannot set Must. |
| 331 | if (I + 1 != E) |
| 332 | IsMustAlias = false; |
| 333 | break; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
Alina Sbirlea | df26cf8 | 2018-01-19 10:26:40 +0000 | [diff] [blame] | 336 | |
| 337 | if (isNoModRef(R)) |
| 338 | return ModRefInfo::NoModRef; |
| 339 | |
| 340 | // If MustAlias found above, set Must bit. |
| 341 | return IsMustAlias ? setMust(R) : clearMust(R); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 344 | return Result; |
| 345 | } |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 346 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 347 | FunctionModRefBehavior AAResults::getModRefBehavior(const CallBase *Call) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 348 | FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 349 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 350 | for (const auto &AA : AAs) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 351 | Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(Call)); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 352 | |
| 353 | // Early-exit the moment we reach the bottom of the lattice. |
| 354 | if (Result == FMRB_DoesNotAccessMemory) |
| 355 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 358 | return Result; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 361 | FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) { |
| 362 | FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 363 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 364 | for (const auto &AA : AAs) { |
| 365 | Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F)); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 366 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 367 | // Early-exit the moment we reach the bottom of the lattice. |
| 368 | if (Result == FMRB_DoesNotAccessMemory) |
| 369 | return Result; |
| 370 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 371 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 372 | return Result; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 373 | } |
| 374 | |
George Burgess IV | aa283d8 | 2018-06-14 19:55:53 +0000 | [diff] [blame] | 375 | raw_ostream &llvm::operator<<(raw_ostream &OS, AliasResult AR) { |
| 376 | switch (AR) { |
| 377 | case NoAlias: |
| 378 | OS << "NoAlias"; |
| 379 | break; |
| 380 | case MustAlias: |
| 381 | OS << "MustAlias"; |
| 382 | break; |
| 383 | case MayAlias: |
| 384 | OS << "MayAlias"; |
| 385 | break; |
| 386 | case PartialAlias: |
| 387 | OS << "PartialAlias"; |
| 388 | break; |
| 389 | } |
| 390 | return OS; |
| 391 | } |
| 392 | |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 393 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 394 | // Helper method implementation |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 395 | //===----------------------------------------------------------------------===// |
| 396 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 397 | ModRefInfo AAResults::getModRefInfo(const LoadInst *L, |
| 398 | const MemoryLocation &Loc) { |
Daniel Berlin | d952cea | 2017-04-07 01:28:36 +0000 | [diff] [blame] | 399 | // Be conservative in the face of atomic. |
| 400 | if (isStrongerThan(L->getOrdering(), AtomicOrdering::Unordered)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 401 | return ModRefInfo::ModRef; |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 402 | |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 403 | // If the load address doesn't alias the given address, it doesn't read |
| 404 | // or write the specified memory. |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 405 | if (Loc.Ptr) { |
| 406 | AliasResult AR = alias(MemoryLocation::get(L), Loc); |
| 407 | if (AR == NoAlias) |
| 408 | return ModRefInfo::NoModRef; |
| 409 | if (AR == MustAlias) |
| 410 | return ModRefInfo::MustRef; |
| 411 | } |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 412 | // Otherwise, a load just reads. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 413 | return ModRefInfo::Ref; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 416 | ModRefInfo AAResults::getModRefInfo(const StoreInst *S, |
| 417 | const MemoryLocation &Loc) { |
Daniel Berlin | d952cea | 2017-04-07 01:28:36 +0000 | [diff] [blame] | 418 | // Be conservative in the face of atomic. |
| 419 | if (isStrongerThan(S->getOrdering(), AtomicOrdering::Unordered)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 420 | return ModRefInfo::ModRef; |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 421 | |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 422 | if (Loc.Ptr) { |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 423 | AliasResult AR = alias(MemoryLocation::get(S), Loc); |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 424 | // If the store address cannot alias the pointer in question, then the |
| 425 | // specified memory cannot be modified by the store. |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 426 | if (AR == NoAlias) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 427 | return ModRefInfo::NoModRef; |
Chris Lattner | 9605576 | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 428 | |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 429 | // If the pointer is a pointer to constant memory, then it could not have |
| 430 | // been modified by this store. |
| 431 | if (pointsToConstantMemory(Loc)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 432 | return ModRefInfo::NoModRef; |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 433 | |
| 434 | // If the store address aliases the pointer as must alias, set Must. |
| 435 | if (AR == MustAlias) |
| 436 | return ModRefInfo::MustMod; |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 437 | } |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 438 | |
| 439 | // Otherwise, a store just writes. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 440 | return ModRefInfo::Mod; |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Anna Thomas | 698f0de | 2017-01-20 00:21:33 +0000 | [diff] [blame] | 443 | ModRefInfo AAResults::getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) { |
| 444 | // If we know that the location is a constant memory location, the fence |
| 445 | // cannot modify this location. |
| 446 | if (Loc.Ptr && pointsToConstantMemory(Loc)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 447 | return ModRefInfo::Ref; |
| 448 | return ModRefInfo::ModRef; |
Anna Thomas | 698f0de | 2017-01-20 00:21:33 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 451 | ModRefInfo AAResults::getModRefInfo(const VAArgInst *V, |
| 452 | const MemoryLocation &Loc) { |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 453 | if (Loc.Ptr) { |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 454 | AliasResult AR = alias(MemoryLocation::get(V), Loc); |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 455 | // If the va_arg address cannot alias the pointer in question, then the |
| 456 | // specified memory cannot be accessed by the va_arg. |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 457 | if (AR == NoAlias) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 458 | return ModRefInfo::NoModRef; |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 459 | |
| 460 | // If the pointer is a pointer to constant memory, then it could not have |
| 461 | // been modified by this va_arg. |
| 462 | if (pointsToConstantMemory(Loc)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 463 | return ModRefInfo::NoModRef; |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 464 | |
| 465 | // If the va_arg aliases the pointer as must alias, set Must. |
| 466 | if (AR == MustAlias) |
| 467 | return ModRefInfo::MustModRef; |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 468 | } |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 469 | |
| 470 | // Otherwise, a va_arg reads and writes. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 471 | return ModRefInfo::ModRef; |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 472 | } |
| 473 | |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 474 | ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad, |
| 475 | const MemoryLocation &Loc) { |
| 476 | if (Loc.Ptr) { |
| 477 | // If the pointer is a pointer to constant memory, |
| 478 | // then it could not have been modified by this catchpad. |
| 479 | if (pointsToConstantMemory(Loc)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 480 | return ModRefInfo::NoModRef; |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // Otherwise, a catchpad reads and writes. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 484 | return ModRefInfo::ModRef; |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet, |
| 488 | const MemoryLocation &Loc) { |
| 489 | if (Loc.Ptr) { |
| 490 | // If the pointer is a pointer to constant memory, |
| 491 | // then it could not have been modified by this catchpad. |
| 492 | if (pointsToConstantMemory(Loc)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 493 | return ModRefInfo::NoModRef; |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Otherwise, a catchret reads and writes. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 497 | return ModRefInfo::ModRef; |
David Majnemer | 6727c01 | 2015-11-17 08:15:14 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 500 | ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX, |
| 501 | const MemoryLocation &Loc) { |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 502 | // Acquire/Release cmpxchg has properties that matter for arbitrary addresses. |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 503 | if (isStrongerThanMonotonic(CX->getSuccessOrdering())) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 504 | return ModRefInfo::ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 505 | |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 506 | if (Loc.Ptr) { |
| 507 | AliasResult AR = alias(MemoryLocation::get(CX), Loc); |
| 508 | // If the cmpxchg address does not alias the location, it does not access |
| 509 | // it. |
| 510 | if (AR == NoAlias) |
| 511 | return ModRefInfo::NoModRef; |
| 512 | |
| 513 | // If the cmpxchg address aliases the pointer as must alias, set Must. |
| 514 | if (AR == MustAlias) |
| 515 | return ModRefInfo::MustModRef; |
| 516 | } |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 517 | |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 518 | return ModRefInfo::ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 521 | ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW, |
| 522 | const MemoryLocation &Loc) { |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 523 | // Acquire/Release atomicrmw has properties that matter for arbitrary addresses. |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 524 | if (isStrongerThanMonotonic(RMW->getOrdering())) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 525 | return ModRefInfo::ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 526 | |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 527 | if (Loc.Ptr) { |
| 528 | AliasResult AR = alias(MemoryLocation::get(RMW), Loc); |
| 529 | // If the atomicrmw address does not alias the location, it does not access |
| 530 | // it. |
| 531 | if (AR == NoAlias) |
| 532 | return ModRefInfo::NoModRef; |
| 533 | |
| 534 | // If the atomicrmw address aliases the pointer as must alias, set Must. |
| 535 | if (AR == MustAlias) |
| 536 | return ModRefInfo::MustModRef; |
| 537 | } |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 538 | |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 539 | return ModRefInfo::ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 542 | /// Return information about whether a particular call site modifies |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 543 | /// or reads the specified memory location \p MemLoc before instruction \p I |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 544 | /// 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] | 545 | /// instruction-ordering queries inside the BasicBlock containing \p I. |
| 546 | /// FIXME: this is really just shoring-up a deficiency in alias analysis. |
| 547 | /// BasicAA isn't willing to spend linear time determining whether an alloca |
| 548 | /// was captured before or after this particular call, while we are. However, |
| 549 | /// with a smarter AA in place, this test is just wasting compile time. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 550 | ModRefInfo AAResults::callCapturesBefore(const Instruction *I, |
| 551 | const MemoryLocation &MemLoc, |
| 552 | DominatorTree *DT, |
| 553 | OrderedBasicBlock *OBB) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 554 | if (!DT) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 555 | return ModRefInfo::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 556 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 557 | const Value *Object = |
| 558 | GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout()); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 559 | if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) || |
| 560 | isa<Constant>(Object)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 561 | return ModRefInfo::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 562 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 563 | const auto *Call = dyn_cast<CallBase>(I); |
| 564 | if (!Call || Call == Object) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 565 | return ModRefInfo::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 566 | |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 567 | if (PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true, |
| 568 | /* StoreCaptures */ true, I, DT, |
| 569 | /* include Object */ true, |
| 570 | /* OrderedBasicBlock */ OBB)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 571 | return ModRefInfo::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 572 | |
| 573 | unsigned ArgNo = 0; |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 574 | ModRefInfo R = ModRefInfo::NoModRef; |
Alina Sbirlea | 020363e | 2018-04-30 20:11:13 +0000 | [diff] [blame] | 575 | bool IsMustAlias = true; |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 576 | // Set flag only if no May found and all operands processed. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 577 | for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end(); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 578 | CI != CE; ++CI, ++ArgNo) { |
| 579 | // Only look at the no-capture or byval pointer arguments. If this |
| 580 | // pointer were passed to arguments that were neither of these, then it |
| 581 | // couldn't be no-capture. |
| 582 | if (!(*CI)->getType()->isPointerTy() || |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 583 | (!Call->doesNotCapture(ArgNo) && ArgNo < Call->getNumArgOperands() && |
| 584 | !Call->isByValArgument(ArgNo))) |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 585 | continue; |
| 586 | |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 587 | AliasResult AR = alias(MemoryLocation(*CI), MemoryLocation(Object)); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 588 | // If this is a no-capture pointer argument, see if we can tell that it |
| 589 | // is impossible to alias the pointer we're checking. If not, we have to |
| 590 | // assume that the call could touch the pointer, even though it doesn't |
| 591 | // escape. |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 592 | if (AR != MustAlias) |
Alina Sbirlea | 020363e | 2018-04-30 20:11:13 +0000 | [diff] [blame] | 593 | IsMustAlias = false; |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 594 | if (AR == NoAlias) |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 595 | continue; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 596 | if (Call->doesNotAccessMemory(ArgNo)) |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 597 | continue; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 598 | if (Call->onlyReadsMemory(ArgNo)) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 599 | R = ModRefInfo::Ref; |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 600 | continue; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 601 | } |
Alina Sbirlea | 50db8a2 | 2017-12-21 21:41:53 +0000 | [diff] [blame] | 602 | // Not returning MustModRef since we have not seen all the arguments. |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 603 | return ModRefInfo::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 604 | } |
Alina Sbirlea | 020363e | 2018-04-30 20:11:13 +0000 | [diff] [blame] | 605 | return IsMustAlias ? setMust(R) : clearMust(R); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 606 | } |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 607 | |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 608 | /// canBasicBlockModify - Return true if it is possible for execution of the |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 609 | /// specified basic block to modify the location Loc. |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 610 | /// |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 611 | bool AAResults::canBasicBlockModify(const BasicBlock &BB, |
| 612 | const MemoryLocation &Loc) { |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 613 | return canInstructionRangeModRef(BB.front(), BB.back(), Loc, ModRefInfo::Mod); |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 616 | /// canInstructionRangeModRef - Return true if it is possible for the |
| 617 | /// execution of the specified instructions to mod\ref (according to the |
| 618 | /// mode) the location Loc. The instructions to consider are all |
| 619 | /// of the instructions in the range of [I1,I2] INCLUSIVE. |
Teresa Johnson | bbcf75e | 2015-05-13 15:04:14 +0000 | [diff] [blame] | 620 | /// I1 and I2 must be in the same basic block. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 621 | bool AAResults::canInstructionRangeModRef(const Instruction &I1, |
| 622 | const Instruction &I2, |
| 623 | const MemoryLocation &Loc, |
| 624 | const ModRefInfo Mode) { |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 625 | assert(I1.getParent() == I2.getParent() && |
| 626 | "Instructions not in same basic block!"); |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 627 | BasicBlock::const_iterator I = I1.getIterator(); |
| 628 | BasicBlock::const_iterator E = I2.getIterator(); |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 629 | ++E; // Convert from inclusive to exclusive range. |
| 630 | |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 631 | for (; I != E; ++I) // Check every instruction in range |
Alina Sbirlea | 18fea01 | 2017-12-06 19:56:37 +0000 | [diff] [blame] | 632 | if (isModOrRefSet(intersectModRef(getModRefInfo(&*I, Loc), Mode))) |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 633 | return true; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 634 | return false; |
| 635 | } |
| 636 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 637 | // Provide a definition for the root virtual destructor. |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 638 | AAResults::Concept::~Concept() = default; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 639 | |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 640 | // Provide a definition for the static object used to identify passes. |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 641 | AnalysisKey AAManager::Key; |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 642 | |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 643 | namespace { |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 644 | |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 645 | |
| 646 | } // end anonymous namespace |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 647 | |
| 648 | char ExternalAAWrapperPass::ID = 0; |
Eugene Zelenko | 530851c | 2017-08-11 21:30:02 +0000 | [diff] [blame] | 649 | |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 650 | INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis", |
| 651 | false, true) |
| 652 | |
| 653 | ImmutablePass * |
| 654 | llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) { |
| 655 | return new ExternalAAWrapperPass(std::move(Callback)); |
| 656 | } |
| 657 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 658 | AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) { |
| 659 | initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 660 | } |
| 661 | |
| 662 | char AAResultsWrapperPass::ID = 0; |
| 663 | |
| 664 | INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa", |
| 665 | "Function Alias Analysis Results", false, true) |
| 666 | INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass) |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 667 | INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass) |
| 668 | INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass) |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 669 | INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 670 | INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) |
| 671 | INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass) |
| 672 | INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass) |
| 673 | INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass) |
| 674 | INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass) |
| 675 | INITIALIZE_PASS_END(AAResultsWrapperPass, "aa", |
| 676 | "Function Alias Analysis Results", false, true) |
| 677 | |
| 678 | FunctionPass *llvm::createAAResultsWrapperPass() { |
| 679 | return new AAResultsWrapperPass(); |
| 680 | } |
| 681 | |
| 682 | /// Run the wrapper pass to rebuild an aggregation over known AA passes. |
| 683 | /// |
| 684 | /// This is the legacy pass manager's interface to the new-style AA results |
| 685 | /// aggregation object. Because this is somewhat shoe-horned into the legacy |
| 686 | /// pass manager, we hard code all the specific alias analyses available into |
| 687 | /// it. While the particular set enabled is configured via commandline flags, |
| 688 | /// adding a new alias analysis to LLVM will require adding support for it to |
| 689 | /// this list. |
| 690 | bool AAResultsWrapperPass::runOnFunction(Function &F) { |
| 691 | // NB! This *must* be reset before adding new AA results to the new |
| 692 | // AAResults object because in the legacy pass manager, each instance |
| 693 | // of these will refer to the *same* immutable analyses, registering and |
| 694 | // unregistering themselves with them. We need to carefully tear down the |
| 695 | // previous object first, in this case replacing it with an empty one, before |
| 696 | // registering new results. |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 697 | AAR.reset( |
| 698 | new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI())); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 699 | |
| 700 | // BasicAA is always available for function analyses. Also, we add it first |
| 701 | // so that it can trump TBAA results when it proves MustAlias. |
| 702 | // FIXME: TBAA should have an explicit mode to support this and then we |
| 703 | // should reconsider the ordering here. |
| 704 | if (!DisableBasicAA) |
| 705 | AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult()); |
| 706 | |
| 707 | // Populate the results with the currently available AAs. |
| 708 | if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>()) |
| 709 | AAR->addAAResult(WrapperPass->getResult()); |
| 710 | if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>()) |
| 711 | AAR->addAAResult(WrapperPass->getResult()); |
| 712 | if (auto *WrapperPass = |
| 713 | getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>()) |
| 714 | AAR->addAAResult(WrapperPass->getResult()); |
| 715 | if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>()) |
| 716 | AAR->addAAResult(WrapperPass->getResult()); |
| 717 | if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>()) |
| 718 | AAR->addAAResult(WrapperPass->getResult()); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 719 | if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>()) |
| 720 | AAR->addAAResult(WrapperPass->getResult()); |
| 721 | if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>()) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 722 | AAR->addAAResult(WrapperPass->getResult()); |
| 723 | |
Chandler Carruth | 2be1075 | 2015-10-21 12:15:19 +0000 | [diff] [blame] | 724 | // If available, run an external AA providing callback over the results as |
| 725 | // well. |
| 726 | if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>()) |
| 727 | if (WrapperPass->CB) |
| 728 | WrapperPass->CB(*this, F, *AAR); |
| 729 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 730 | // Analyses don't mutate the IR, so return false. |
| 731 | return false; |
| 732 | } |
| 733 | |
| 734 | void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 735 | AU.setPreservesAll(); |
| 736 | AU.addRequired<BasicAAWrapperPass>(); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 737 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 738 | |
| 739 | // We also need to mark all the alias analysis passes we will potentially |
| 740 | // probe in runOnFunction as used here to ensure the legacy pass manager |
| 741 | // preserves them. This hard coding of lists of alias analyses is specific to |
| 742 | // the legacy pass manager. |
| 743 | AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>(); |
| 744 | AU.addUsedIfAvailable<TypeBasedAAWrapperPass>(); |
| 745 | AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>(); |
| 746 | AU.addUsedIfAvailable<GlobalsAAWrapperPass>(); |
| 747 | AU.addUsedIfAvailable<SCEVAAWrapperPass>(); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 748 | AU.addUsedIfAvailable<CFLAndersAAWrapperPass>(); |
| 749 | AU.addUsedIfAvailable<CFLSteensAAWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F, |
| 753 | BasicAAResult &BAR) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 754 | AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 755 | |
| 756 | // Add in our explicitly constructed BasicAA results. |
| 757 | if (!DisableBasicAA) |
| 758 | AAR.addAAResult(BAR); |
| 759 | |
| 760 | // Populate the results with the other currently available AAs. |
| 761 | if (auto *WrapperPass = |
| 762 | P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>()) |
| 763 | AAR.addAAResult(WrapperPass->getResult()); |
| 764 | if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>()) |
| 765 | AAR.addAAResult(WrapperPass->getResult()); |
| 766 | if (auto *WrapperPass = |
| 767 | P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>()) |
| 768 | AAR.addAAResult(WrapperPass->getResult()); |
| 769 | if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>()) |
| 770 | AAR.addAAResult(WrapperPass->getResult()); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 771 | if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>()) |
| 772 | AAR.addAAResult(WrapperPass->getResult()); |
| 773 | if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>()) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 774 | AAR.addAAResult(WrapperPass->getResult()); |
| 775 | |
| 776 | return AAR; |
| 777 | } |
| 778 | |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 779 | bool llvm::isNoAliasCall(const Value *V) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 780 | if (const auto *Call = dyn_cast<CallBase>(V)) |
| 781 | return Call->hasRetAttr(Attribute::NoAlias); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 782 | return false; |
| 783 | } |
| 784 | |
Sanjay Patel | d7f613d | 2016-01-13 22:17:13 +0000 | [diff] [blame] | 785 | bool llvm::isNoAliasArgument(const Value *V) { |
Michael Kuperstein | f3e663a | 2013-05-28 08:17:48 +0000 | [diff] [blame] | 786 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 787 | return A->hasNoAliasAttr(); |
| 788 | return false; |
| 789 | } |
| 790 | |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 791 | bool llvm::isIdentifiedObject(const Value *V) { |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 792 | if (isa<AllocaInst>(V)) |
Dan Gohman | 1f59f01 | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 793 | return true; |
| 794 | if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 795 | return true; |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 796 | if (isNoAliasCall(V)) |
| 797 | return true; |
| 798 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 799 | return A->hasNoAliasAttr() || A->hasByValAttr(); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 800 | return false; |
| 801 | } |
Hal Finkel | c782aa5 | 2014-07-21 12:27:23 +0000 | [diff] [blame] | 802 | |
Sanjay Patel | d7f613d | 2016-01-13 22:17:13 +0000 | [diff] [blame] | 803 | bool llvm::isIdentifiedFunctionLocal(const Value *V) { |
Hal Finkel | c782aa5 | 2014-07-21 12:27:23 +0000 | [diff] [blame] | 804 | return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V); |
| 805 | } |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 806 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 807 | void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) { |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 808 | // This function needs to be in sync with llvm::createLegacyPMAAResults -- if |
| 809 | // more alias analyses are added to llvm::createLegacyPMAAResults, they need |
| 810 | // to be added here also. |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 811 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 812 | AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>(); |
| 813 | AU.addUsedIfAvailable<TypeBasedAAWrapperPass>(); |
| 814 | AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>(); |
| 815 | AU.addUsedIfAvailable<GlobalsAAWrapperPass>(); |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 816 | AU.addUsedIfAvailable<CFLAndersAAWrapperPass>(); |
| 817 | AU.addUsedIfAvailable<CFLSteensAAWrapperPass>(); |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 818 | } |