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