Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +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" |
Nick Lewycky | 0b68245 | 2013-07-27 01:24:00 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CFG.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/BasicBlock.h" |
| 33 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Function.h" |
| 36 | #include "llvm/IR/Instructions.h" |
| 37 | #include "llvm/IR/IntrinsicInst.h" |
| 38 | #include "llvm/IR/LLVMContext.h" |
| 39 | #include "llvm/IR/Type.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 40 | #include "llvm/Pass.h" |
Chris Lattner | a67dbd0 | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 43 | // Register the AliasAnalysis interface, providing a nice name to refer to. |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 44 | INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA) |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 45 | char AliasAnalysis::ID = 0; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // Default chaining methods |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 51 | AliasAnalysis::AliasResult |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 52 | AliasAnalysis::alias(const Location &LocA, const Location &LocB) { |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 53 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 54 | return AA->alias(LocA, LocB); |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 57 | bool AliasAnalysis::pointsToConstantMemory(const Location &Loc, |
| 58 | bool OrLocal) { |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 59 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 60 | return AA->pointsToConstantMemory(Loc, OrLocal); |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 63 | AliasAnalysis::Location |
| 64 | AliasAnalysis::getArgLocation(ImmutableCallSite CS, unsigned ArgIdx, |
| 65 | AliasAnalysis::ModRefResult &Mask) { |
| 66 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 67 | return AA->getArgLocation(CS, ArgIdx, Mask); |
| 68 | } |
| 69 | |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 70 | void AliasAnalysis::deleteValue(Value *V) { |
| 71 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 72 | AA->deleteValue(V); |
| 73 | } |
| 74 | |
| 75 | void AliasAnalysis::copyValue(Value *From, Value *To) { |
| 76 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 77 | AA->copyValue(From, To); |
| 78 | } |
| 79 | |
Owen Anderson | b6e4ff0 | 2011-01-03 21:38:41 +0000 | [diff] [blame] | 80 | void AliasAnalysis::addEscapingUse(Use &U) { |
| 81 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 82 | AA->addEscapingUse(U); |
| 83 | } |
| 84 | |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 85 | AliasAnalysis::ModRefResult |
| 86 | AliasAnalysis::getModRefInfo(Instruction *I, ImmutableCallSite Call) { |
| 87 | // We may have two calls |
| 88 | if (auto CS = ImmutableCallSite(I)) { |
| 89 | // Check if the two calls modify the same memory |
| 90 | return getModRefInfo(Call, CS); |
| 91 | } else { |
| 92 | // Otherwise, check if the call modifies or references the |
| 93 | // location this memory access defines. The best we can say |
| 94 | // is that if the call references what this instruction |
| 95 | // defines, it must be clobbered by this location. |
| 96 | const AliasAnalysis::Location DefLoc = AA->getLocation(I); |
| 97 | if (getModRefInfo(Call, DefLoc) != AliasAnalysis::NoModRef) |
| 98 | return AliasAnalysis::ModRef; |
| 99 | } |
| 100 | return AliasAnalysis::NoModRef; |
| 101 | } |
Owen Anderson | b6e4ff0 | 2011-01-03 21:38:41 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 103 | AliasAnalysis::ModRefResult |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 104 | AliasAnalysis::getModRefInfo(ImmutableCallSite CS, |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 105 | const Location &Loc) { |
Dan Gohman | 1033ce6 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 106 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 107 | |
| 108 | ModRefBehavior MRB = getModRefBehavior(CS); |
| 109 | if (MRB == DoesNotAccessMemory) |
| 110 | return NoModRef; |
| 111 | |
| 112 | ModRefResult Mask = ModRef; |
Dan Gohman | 5d06f89 | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 113 | if (onlyReadsMemory(MRB)) |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 114 | Mask = Ref; |
Dan Gohman | 5d06f89 | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 115 | |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 116 | if (onlyAccessesArgPointees(MRB)) { |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 117 | bool doesAlias = false; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 118 | ModRefResult AllArgsMask = NoModRef; |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 119 | if (doesAccessArgPointees(MRB)) { |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 120 | for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 121 | AI != AE; ++AI) { |
| 122 | const Value *Arg = *AI; |
| 123 | if (!Arg->getType()->isPointerTy()) |
| 124 | continue; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 125 | ModRefResult ArgMask; |
| 126 | Location CSLoc = |
| 127 | getArgLocation(CS, (unsigned) std::distance(CS.arg_begin(), AI), |
| 128 | ArgMask); |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 129 | if (!isNoAlias(CSLoc, Loc)) { |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 130 | doesAlias = true; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 131 | AllArgsMask = ModRefResult(AllArgsMask | ArgMask); |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 132 | } |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 135 | if (!doesAlias) |
| 136 | return NoModRef; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 137 | Mask = ModRefResult(Mask & AllArgsMask); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 140 | // If Loc is a constant memory location, the call definitely could not |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 141 | // modify the memory location. |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 142 | if ((Mask & Mod) && pointsToConstantMemory(Loc)) |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 143 | Mask = ModRefResult(Mask & ~Mod); |
| 144 | |
Dan Gohman | abaf2d8 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 145 | // If this is the end of the chain, don't forward. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 146 | if (!AA) return Mask; |
| 147 | |
| 148 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 149 | // in any mask we've managed to compute. |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 150 | return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | AliasAnalysis::ModRefResult |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 154 | AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) { |
Dan Gohman | 1033ce6 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 155 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 156 | |
| 157 | // If CS1 or CS2 are readnone, they don't interact. |
| 158 | ModRefBehavior CS1B = getModRefBehavior(CS1); |
| 159 | if (CS1B == DoesNotAccessMemory) return NoModRef; |
| 160 | |
| 161 | ModRefBehavior CS2B = getModRefBehavior(CS2); |
| 162 | if (CS2B == DoesNotAccessMemory) return NoModRef; |
| 163 | |
| 164 | // If they both only read from memory, there is no dependence. |
Dan Gohman | 5d06f89 | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 165 | if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B)) |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 166 | return NoModRef; |
| 167 | |
| 168 | AliasAnalysis::ModRefResult Mask = ModRef; |
| 169 | |
| 170 | // If CS1 only reads memory, the only dependence on CS2 can be |
| 171 | // from CS1 reading memory written by CS2. |
Dan Gohman | 5d06f89 | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 172 | if (onlyReadsMemory(CS1B)) |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 173 | Mask = ModRefResult(Mask & Ref); |
| 174 | |
| 175 | // If CS2 only access memory through arguments, accumulate the mod/ref |
| 176 | // information from CS1's references to the memory referenced by |
| 177 | // CS2's arguments. |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 178 | if (onlyAccessesArgPointees(CS2B)) { |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 179 | AliasAnalysis::ModRefResult R = NoModRef; |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 180 | if (doesAccessArgPointees(CS2B)) { |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 181 | for (ImmutableCallSite::arg_iterator |
| 182 | I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) { |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 183 | const Value *Arg = *I; |
| 184 | if (!Arg->getType()->isPointerTy()) |
| 185 | continue; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 186 | ModRefResult ArgMask; |
| 187 | Location CS2Loc = |
| 188 | getArgLocation(CS2, (unsigned) std::distance(CS2.arg_begin(), I), |
| 189 | ArgMask); |
| 190 | // ArgMask indicates what CS2 might do to CS2Loc, and the dependence of |
| 191 | // CS1 on that location is the inverse. |
| 192 | if (ArgMask == Mod) |
| 193 | ArgMask = ModRef; |
| 194 | else if (ArgMask == Ref) |
| 195 | ArgMask = Mod; |
| 196 | |
| 197 | R = ModRefResult((R | (getModRefInfo(CS1, CS2Loc) & ArgMask)) & Mask); |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 198 | if (R == Mask) |
| 199 | break; |
| 200 | } |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 201 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 202 | return R; |
| 203 | } |
| 204 | |
| 205 | // If CS1 only accesses memory through arguments, check if CS2 references |
| 206 | // any of the memory referenced by CS1's arguments. If not, return NoModRef. |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 207 | if (onlyAccessesArgPointees(CS1B)) { |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 208 | AliasAnalysis::ModRefResult R = NoModRef; |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 209 | if (doesAccessArgPointees(CS1B)) { |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 210 | for (ImmutableCallSite::arg_iterator |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 211 | I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) { |
| 212 | const Value *Arg = *I; |
| 213 | if (!Arg->getType()->isPointerTy()) |
| 214 | continue; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 215 | ModRefResult ArgMask; |
NAKAMURA Takumi | d0e13af | 2014-10-28 11:54:52 +0000 | [diff] [blame] | 216 | Location CS1Loc = getArgLocation( |
| 217 | CS1, (unsigned)std::distance(CS1.arg_begin(), I), ArgMask); |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 218 | // ArgMask indicates what CS1 might do to CS1Loc; if CS1 might Mod |
| 219 | // CS1Loc, then we care about either a Mod or a Ref by CS2. If CS1 |
| 220 | // might Ref, then we care only about a Mod by CS2. |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 221 | ModRefResult ArgR = getModRefInfo(CS2, CS1Loc); |
| 222 | if (((ArgMask & Mod) != NoModRef && (ArgR & ModRef) != NoModRef) || |
| 223 | ((ArgMask & Ref) != NoModRef && (ArgR & Mod) != NoModRef)) |
| 224 | R = ModRefResult((R | ArgMask) & Mask); |
| 225 | |
| 226 | if (R == Mask) |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 227 | break; |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 230 | return R; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Dan Gohman | abaf2d8 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 233 | // If this is the end of the chain, don't forward. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 234 | if (!AA) return Mask; |
| 235 | |
| 236 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 237 | // in any mask we've managed to compute. |
| 238 | return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask); |
| 239 | } |
| 240 | |
| 241 | AliasAnalysis::ModRefBehavior |
| 242 | AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { |
Dan Gohman | 1033ce6 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 243 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 244 | |
| 245 | ModRefBehavior Min = UnknownModRefBehavior; |
| 246 | |
| 247 | // Call back into the alias analysis with the other form of getModRefBehavior |
| 248 | // to see if it can give a better response. |
| 249 | if (const Function *F = CS.getCalledFunction()) |
| 250 | Min = getModRefBehavior(F); |
| 251 | |
Dan Gohman | abaf2d8 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 252 | // If this is the end of the chain, don't forward. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 253 | if (!AA) return Min; |
| 254 | |
| 255 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 256 | // in any result we've managed to compute. |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 257 | return ModRefBehavior(AA->getModRefBehavior(CS) & Min); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | AliasAnalysis::ModRefBehavior |
| 261 | AliasAnalysis::getModRefBehavior(const Function *F) { |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 262 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 263 | return AA->getModRefBehavior(F); |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 266 | //===----------------------------------------------------------------------===// |
| 267 | // AliasAnalysis non-virtual helper method implementation |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | |
Dan Gohman | 65316d6 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 270 | AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) { |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 271 | AAMDNodes AATags; |
| 272 | LI->getAAMetadata(AATags); |
| 273 | |
Dan Gohman | 65316d6 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 274 | return Location(LI->getPointerOperand(), |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 275 | getTypeStoreSize(LI->getType()), AATags); |
Dan Gohman | 65316d6 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) { |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 279 | AAMDNodes AATags; |
| 280 | SI->getAAMetadata(AATags); |
| 281 | |
Dan Gohman | 65316d6 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 282 | return Location(SI->getPointerOperand(), |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 283 | getTypeStoreSize(SI->getValueOperand()->getType()), AATags); |
Dan Gohman | 65316d6 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) { |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 287 | AAMDNodes AATags; |
| 288 | VI->getAAMetadata(AATags); |
| 289 | |
| 290 | return Location(VI->getPointerOperand(), UnknownSize, AATags); |
Dan Gohman | 65316d6 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 293 | AliasAnalysis::Location |
| 294 | AliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) { |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 295 | AAMDNodes AATags; |
| 296 | CXI->getAAMetadata(AATags); |
| 297 | |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 298 | return Location(CXI->getPointerOperand(), |
| 299 | getTypeStoreSize(CXI->getCompareOperand()->getType()), |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 300 | AATags); |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | AliasAnalysis::Location |
| 304 | AliasAnalysis::getLocation(const AtomicRMWInst *RMWI) { |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 305 | AAMDNodes AATags; |
| 306 | RMWI->getAAMetadata(AATags); |
| 307 | |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 308 | return Location(RMWI->getPointerOperand(), |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 309 | getTypeStoreSize(RMWI->getValOperand()->getType()), AATags); |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 310 | } |
Chris Lattner | 663ba91 | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 311 | |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 312 | AliasAnalysis::Location |
Chris Lattner | 663ba91 | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 313 | AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) { |
| 314 | uint64_t Size = UnknownSize; |
| 315 | if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) |
| 316 | Size = C->getValue().getZExtValue(); |
| 317 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 318 | // memcpy/memmove can have AA tags. For memcpy, they apply |
Dan Gohman | e1a17a3 | 2010-12-16 02:51:19 +0000 | [diff] [blame] | 319 | // to both the source and the destination. |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 320 | AAMDNodes AATags; |
| 321 | MTI->getAAMetadata(AATags); |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 322 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 323 | return Location(MTI->getRawSource(), Size, AATags); |
Chris Lattner | 663ba91 | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 324 | } |
| 325 | |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 326 | AliasAnalysis::Location |
Chris Lattner | afbc0c2 | 2010-11-30 01:48:20 +0000 | [diff] [blame] | 327 | AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) { |
Chris Lattner | 663ba91 | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 328 | uint64_t Size = UnknownSize; |
| 329 | if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) |
| 330 | Size = C->getValue().getZExtValue(); |
Dan Gohman | e1a17a3 | 2010-12-16 02:51:19 +0000 | [diff] [blame] | 331 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 332 | // memcpy/memmove can have AA tags. For memcpy, they apply |
Dan Gohman | e1a17a3 | 2010-12-16 02:51:19 +0000 | [diff] [blame] | 333 | // to both the source and the destination. |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 334 | AAMDNodes AATags; |
Benjamin Kramer | 2e52f02 | 2014-10-04 22:44:29 +0000 | [diff] [blame] | 335 | MTI->getAAMetadata(AATags); |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 336 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 337 | return Location(MTI->getRawDest(), Size, AATags); |
Chris Lattner | 663ba91 | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | |
| 341 | |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 342 | AliasAnalysis::ModRefResult |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 343 | AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 344 | // Be conservative in the face of volatile/atomic. |
| 345 | if (!L->isUnordered()) |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 346 | return ModRef; |
| 347 | |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 348 | // If the load address doesn't alias the given address, it doesn't read |
| 349 | // or write the specified memory. |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 350 | if (Loc.Ptr && !alias(getLocation(L), Loc)) |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 351 | return NoModRef; |
| 352 | |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 353 | // Otherwise, a load just reads. |
| 354 | return Ref; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 357 | AliasAnalysis::ModRefResult |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 358 | AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 359 | // Be conservative in the face of volatile/atomic. |
| 360 | if (!S->isUnordered()) |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 361 | return ModRef; |
| 362 | |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 363 | if (Loc.Ptr) { |
| 364 | // If the store address cannot alias the pointer in question, then the |
| 365 | // specified memory cannot be modified by the store. |
| 366 | if (!alias(getLocation(S), Loc)) |
| 367 | return NoModRef; |
Chris Lattner | 9605576 | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 368 | |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 369 | // If the pointer is a pointer to constant memory, then it could not have |
| 370 | // been modified by this store. |
| 371 | if (pointsToConstantMemory(Loc)) |
| 372 | return NoModRef; |
| 373 | |
| 374 | } |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 375 | |
| 376 | // Otherwise, a store just writes. |
| 377 | return Mod; |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 380 | AliasAnalysis::ModRefResult |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 381 | AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) { |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 382 | |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 383 | if (Loc.Ptr) { |
| 384 | // If the va_arg address cannot alias the pointer in question, then the |
| 385 | // specified memory cannot be accessed by the va_arg. |
| 386 | if (!alias(getLocation(V), Loc)) |
| 387 | return NoModRef; |
| 388 | |
| 389 | // If the pointer is a pointer to constant memory, then it could not have |
| 390 | // been modified by this va_arg. |
| 391 | if (pointsToConstantMemory(Loc)) |
| 392 | return NoModRef; |
| 393 | } |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 394 | |
| 395 | // Otherwise, a va_arg reads and writes. |
| 396 | return ModRef; |
| 397 | } |
| 398 | |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 399 | AliasAnalysis::ModRefResult |
| 400 | AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) { |
| 401 | // Acquire/Release cmpxchg has properties that matter for arbitrary addresses. |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 402 | if (CX->getSuccessOrdering() > Monotonic) |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 403 | return ModRef; |
| 404 | |
| 405 | // If the cmpxchg address does not alias the location, it does not access it. |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 406 | if (Loc.Ptr && !alias(getLocation(CX), Loc)) |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 407 | return NoModRef; |
| 408 | |
| 409 | return ModRef; |
| 410 | } |
| 411 | |
| 412 | AliasAnalysis::ModRefResult |
| 413 | AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) { |
| 414 | // Acquire/Release atomicrmw has properties that matter for arbitrary addresses. |
| 415 | if (RMW->getOrdering() > Monotonic) |
| 416 | return ModRef; |
| 417 | |
| 418 | // If the atomicrmw address does not alias the location, it does not access it. |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 419 | if (Loc.Ptr && !alias(getLocation(RMW), Loc)) |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 420 | return NoModRef; |
| 421 | |
| 422 | return ModRef; |
| 423 | } |
| 424 | |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 425 | // FIXME: this is really just shoring-up a deficiency in alias analysis. |
| 426 | // BasicAA isn't willing to spend linear time determining whether an alloca |
| 427 | // was captured before or after this particular call, while we are. However, |
| 428 | // with a smarter AA in place, this test is just wasting compile time. |
| 429 | AliasAnalysis::ModRefResult |
| 430 | AliasAnalysis::callCapturesBefore(const Instruction *I, |
| 431 | const AliasAnalysis::Location &MemLoc, |
| 432 | DominatorTree *DT) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 433 | if (!DT) |
| 434 | return AliasAnalysis::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 435 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 436 | const Value *Object = GetUnderlyingObject(MemLoc.Ptr, *DL); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 437 | if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) || |
| 438 | isa<Constant>(Object)) |
| 439 | return AliasAnalysis::ModRef; |
| 440 | |
| 441 | ImmutableCallSite CS(I); |
| 442 | if (!CS.getInstruction() || CS.getInstruction() == Object) |
| 443 | return AliasAnalysis::ModRef; |
| 444 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 445 | if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true, |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 446 | /* StoreCaptures */ true, I, DT, |
| 447 | /* include Object */ true)) |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 448 | return AliasAnalysis::ModRef; |
| 449 | |
| 450 | unsigned ArgNo = 0; |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 451 | AliasAnalysis::ModRefResult R = AliasAnalysis::NoModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 452 | for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
| 453 | CI != CE; ++CI, ++ArgNo) { |
| 454 | // Only look at the no-capture or byval pointer arguments. If this |
| 455 | // pointer were passed to arguments that were neither of these, then it |
| 456 | // couldn't be no-capture. |
| 457 | if (!(*CI)->getType()->isPointerTy() || |
| 458 | (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo))) |
| 459 | continue; |
| 460 | |
| 461 | // If this is a no-capture pointer argument, see if we can tell that it |
| 462 | // is impossible to alias the pointer we're checking. If not, we have to |
| 463 | // assume that the call could touch the pointer, even though it doesn't |
| 464 | // escape. |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 465 | if (isNoAlias(AliasAnalysis::Location(*CI), |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 466 | AliasAnalysis::Location(Object))) |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 467 | continue; |
| 468 | if (CS.doesNotAccessMemory(ArgNo)) |
| 469 | continue; |
| 470 | if (CS.onlyReadsMemory(ArgNo)) { |
| 471 | R = AliasAnalysis::Ref; |
| 472 | continue; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 473 | } |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 474 | return AliasAnalysis::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 475 | } |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 476 | return R; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 477 | } |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 479 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 480 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 481 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 482 | // support to not be included in the tool correctly! |
| 483 | // |
| 484 | AliasAnalysis::~AliasAnalysis() {} |
| 485 | |
Dan Gohman | 7a68b66 | 2008-05-30 00:02:02 +0000 | [diff] [blame] | 486 | /// InitializeAliasAnalysis - Subclasses must call this method to initialize the |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 487 | /// AliasAnalysis interface before any other methods are called. |
| 488 | /// |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 489 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P, const DataLayout *NewDL) { |
| 490 | DL = NewDL; |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 491 | auto *TLIP = P->getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>(); |
| 492 | TLI = TLIP ? &TLIP->getTLI() : nullptr; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 493 | AA = &P->getAnalysis<AliasAnalysis>(); |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // getAnalysisUsage - All alias analysis implementations should invoke this |
Dan Gohman | 43d19d6 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 497 | // directly (using AliasAnalysis::getAnalysisUsage(AU)). |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 498 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 499 | AU.addRequired<AliasAnalysis>(); // All AA's chain |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 502 | /// getTypeStoreSize - Return the DataLayout store size for the given type, |
Dan Gohman | 43d19d6 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 503 | /// if known, or a conservative value otherwise. |
| 504 | /// |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 505 | uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 506 | return DL ? DL->getTypeStoreSize(Ty) : UnknownSize; |
Dan Gohman | 43d19d6 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 509 | /// canBasicBlockModify - Return true if it is possible for execution of the |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 510 | /// specified basic block to modify the location Loc. |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 511 | /// |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 512 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 513 | const Location &Loc) { |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 514 | return canInstructionRangeModRef(BB.front(), BB.back(), Loc, Mod); |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 517 | /// canInstructionRangeModRef - Return true if it is possible for the |
| 518 | /// execution of the specified instructions to mod\ref (according to the |
| 519 | /// mode) the location Loc. The instructions to consider are all |
| 520 | /// of the instructions in the range of [I1,I2] INCLUSIVE. |
Teresa Johnson | bbcf75e | 2015-05-13 15:04:14 +0000 | [diff] [blame^] | 521 | /// I1 and I2 must be in the same basic block. |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 522 | bool AliasAnalysis::canInstructionRangeModRef(const Instruction &I1, |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 523 | const Instruction &I2, |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 524 | const Location &Loc, |
| 525 | const ModRefResult Mode) { |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 526 | assert(I1.getParent() == I2.getParent() && |
| 527 | "Instructions not in same basic block!"); |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 528 | BasicBlock::const_iterator I = &I1; |
| 529 | BasicBlock::const_iterator E = &I2; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 530 | ++E; // Convert from inclusive to exclusive range. |
| 531 | |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 532 | for (; I != E; ++I) // Check every instruction in range |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 533 | if (getModRefInfo(I, Loc) & Mode) |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 534 | return true; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 535 | return false; |
| 536 | } |
| 537 | |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 538 | /// isNoAliasCall - Return true if this pointer is returned by a noalias |
| 539 | /// function. |
| 540 | bool llvm::isNoAliasCall(const Value *V) { |
| 541 | if (isa<CallInst>(V) || isa<InvokeInst>(V)) |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 542 | return ImmutableCallSite(cast<Instruction>(V)) |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 543 | .paramHasAttr(0, Attribute::NoAlias); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 544 | return false; |
| 545 | } |
| 546 | |
Michael Kuperstein | f3e663a | 2013-05-28 08:17:48 +0000 | [diff] [blame] | 547 | /// isNoAliasArgument - Return true if this is an argument with the noalias |
| 548 | /// attribute. |
| 549 | bool llvm::isNoAliasArgument(const Value *V) |
| 550 | { |
| 551 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 552 | return A->hasNoAliasAttr(); |
| 553 | return false; |
| 554 | } |
| 555 | |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 556 | /// isIdentifiedObject - Return true if this pointer refers to a distinct and |
| 557 | /// identifiable object. This returns true for: |
Dan Gohman | 1f59f01 | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 558 | /// Global Variables and Functions (but not Global Aliases) |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 559 | /// Allocas and Mallocs |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 560 | /// ByVal and NoAlias Arguments |
| 561 | /// NoAlias returns |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 562 | /// |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 563 | bool llvm::isIdentifiedObject(const Value *V) { |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 564 | if (isa<AllocaInst>(V)) |
Dan Gohman | 1f59f01 | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 565 | return true; |
| 566 | if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 567 | return true; |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 568 | if (isNoAliasCall(V)) |
| 569 | return true; |
| 570 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 571 | return A->hasNoAliasAttr() || A->hasByValAttr(); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 572 | return false; |
| 573 | } |
Hal Finkel | c782aa5 | 2014-07-21 12:27:23 +0000 | [diff] [blame] | 574 | |
| 575 | /// isIdentifiedFunctionLocal - Return true if V is umabigously identified |
| 576 | /// at the function-level. Different IdentifiedFunctionLocals can't alias. |
| 577 | /// Further, an IdentifiedFunctionLocal can not alias with any function |
| 578 | /// arguments other than itself, which is not necessarily true for |
| 579 | /// IdentifiedObjects. |
| 580 | bool llvm::isIdentifiedFunctionLocal(const Value *V) |
| 581 | { |
| 582 | return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V); |
| 583 | } |