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