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