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; |
| 199 | Location CS1Loc = |
| 200 | getArgLocation(CS1, (unsigned) std::distance(CS1.arg_begin(), I), |
| 201 | ArgMask); |
| 202 | // ArgMask indicates what CS1 might do to CS1Loc; if CS1 might Mod |
| 203 | // CS1Loc, then we care about either a Mod or a Ref by CS2. If CS1 |
| 204 | // might Ref, then we care only about a Mod by CS2. |
| 205 | ModRefResult ArgR = getModRefInfo(CS2, CS1Loc); |
| 206 | if (((ArgMask & Mod) != NoModRef && (ArgR & ModRef) != NoModRef) || |
| 207 | ((ArgMask & Ref) != NoModRef && (ArgR & Mod) != NoModRef)) |
| 208 | R = ModRefResult((R | ArgMask) & Mask); |
| 209 | |
| 210 | if (R == Mask) |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 211 | break; |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 214 | return R; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Dan Gohman | abaf2d8 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 217 | // If this is the end of the chain, don't forward. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 218 | if (!AA) return Mask; |
| 219 | |
| 220 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 221 | // in any mask we've managed to compute. |
| 222 | return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask); |
| 223 | } |
| 224 | |
| 225 | AliasAnalysis::ModRefBehavior |
| 226 | AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { |
Dan Gohman | 1033ce6 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 227 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 228 | |
| 229 | ModRefBehavior Min = UnknownModRefBehavior; |
| 230 | |
| 231 | // Call back into the alias analysis with the other form of getModRefBehavior |
| 232 | // to see if it can give a better response. |
| 233 | if (const Function *F = CS.getCalledFunction()) |
| 234 | Min = getModRefBehavior(F); |
| 235 | |
Dan Gohman | abaf2d8 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 236 | // If this is the end of the chain, don't forward. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 237 | if (!AA) return Min; |
| 238 | |
| 239 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 240 | // in any result we've managed to compute. |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 241 | return ModRefBehavior(AA->getModRefBehavior(CS) & Min); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | AliasAnalysis::ModRefBehavior |
| 245 | AliasAnalysis::getModRefBehavior(const Function *F) { |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 246 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 247 | return AA->getModRefBehavior(F); |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 250 | //===----------------------------------------------------------------------===// |
| 251 | // AliasAnalysis non-virtual helper method implementation |
| 252 | //===----------------------------------------------------------------------===// |
| 253 | |
Dan Gohman | 65316d6 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 254 | AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) { |
| 255 | return Location(LI->getPointerOperand(), |
| 256 | getTypeStoreSize(LI->getType()), |
| 257 | LI->getMetadata(LLVMContext::MD_tbaa)); |
| 258 | } |
| 259 | |
| 260 | AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) { |
| 261 | return Location(SI->getPointerOperand(), |
| 262 | getTypeStoreSize(SI->getValueOperand()->getType()), |
| 263 | SI->getMetadata(LLVMContext::MD_tbaa)); |
| 264 | } |
| 265 | |
| 266 | AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) { |
| 267 | return Location(VI->getPointerOperand(), |
| 268 | UnknownSize, |
| 269 | VI->getMetadata(LLVMContext::MD_tbaa)); |
| 270 | } |
| 271 | |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 272 | AliasAnalysis::Location |
| 273 | AliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) { |
| 274 | return Location(CXI->getPointerOperand(), |
| 275 | getTypeStoreSize(CXI->getCompareOperand()->getType()), |
| 276 | CXI->getMetadata(LLVMContext::MD_tbaa)); |
| 277 | } |
| 278 | |
| 279 | AliasAnalysis::Location |
| 280 | AliasAnalysis::getLocation(const AtomicRMWInst *RMWI) { |
| 281 | return Location(RMWI->getPointerOperand(), |
| 282 | getTypeStoreSize(RMWI->getValOperand()->getType()), |
| 283 | RMWI->getMetadata(LLVMContext::MD_tbaa)); |
| 284 | } |
Chris Lattner | 663ba91 | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 285 | |
| 286 | AliasAnalysis::Location |
| 287 | AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) { |
| 288 | uint64_t Size = UnknownSize; |
| 289 | if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) |
| 290 | Size = C->getValue().getZExtValue(); |
| 291 | |
Dan Gohman | e1a17a3 | 2010-12-16 02:51:19 +0000 | [diff] [blame] | 292 | // memcpy/memmove can have TBAA tags. For memcpy, they apply |
| 293 | // to both the source and the destination. |
| 294 | MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa); |
| 295 | |
| 296 | return Location(MTI->getRawSource(), Size, TBAATag); |
Chris Lattner | 663ba91 | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | AliasAnalysis::Location |
Chris Lattner | afbc0c2 | 2010-11-30 01:48:20 +0000 | [diff] [blame] | 300 | AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) { |
Chris Lattner | 663ba91 | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 301 | uint64_t Size = UnknownSize; |
| 302 | if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) |
| 303 | Size = C->getValue().getZExtValue(); |
Dan Gohman | e1a17a3 | 2010-12-16 02:51:19 +0000 | [diff] [blame] | 304 | |
| 305 | // memcpy/memmove can have TBAA tags. For memcpy, they apply |
| 306 | // to both the source and the destination. |
| 307 | MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa); |
Chris Lattner | 663ba91 | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 308 | |
Dan Gohman | e1a17a3 | 2010-12-16 02:51:19 +0000 | [diff] [blame] | 309 | return Location(MTI->getRawDest(), Size, TBAATag); |
Chris Lattner | 663ba91 | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
| 313 | |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 314 | AliasAnalysis::ModRefResult |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 315 | AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 316 | // Be conservative in the face of volatile/atomic. |
| 317 | if (!L->isUnordered()) |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 318 | return ModRef; |
| 319 | |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 320 | // If the load address doesn't alias the given address, it doesn't read |
| 321 | // or write the specified memory. |
Dan Gohman | 65316d6 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 322 | if (!alias(getLocation(L), Loc)) |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 323 | return NoModRef; |
| 324 | |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 325 | // Otherwise, a load just reads. |
| 326 | return Ref; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 329 | AliasAnalysis::ModRefResult |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 330 | AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 331 | // Be conservative in the face of volatile/atomic. |
| 332 | if (!S->isUnordered()) |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 333 | return ModRef; |
| 334 | |
Dan Gohman | 23976df | 2010-08-06 18:10:45 +0000 | [diff] [blame] | 335 | // If the store address cannot alias the pointer in question, then the |
| 336 | // specified memory cannot be modified by the store. |
Dan Gohman | 65316d6 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 337 | if (!alias(getLocation(S), Loc)) |
Chris Lattner | 9605576 | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 338 | return NoModRef; |
| 339 | |
| 340 | // If the pointer is a pointer to constant memory, then it could not have been |
| 341 | // modified by this store. |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 342 | if (pointsToConstantMemory(Loc)) |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 343 | return NoModRef; |
| 344 | |
| 345 | // Otherwise, a store just writes. |
| 346 | return Mod; |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 349 | AliasAnalysis::ModRefResult |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 350 | AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) { |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 351 | // If the va_arg address cannot alias the pointer in question, then the |
| 352 | // specified memory cannot be accessed by the va_arg. |
Dan Gohman | 65316d6 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 353 | if (!alias(getLocation(V), Loc)) |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 354 | return NoModRef; |
| 355 | |
| 356 | // If the pointer is a pointer to constant memory, then it could not have been |
| 357 | // modified by this va_arg. |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 358 | if (pointsToConstantMemory(Loc)) |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 359 | return NoModRef; |
| 360 | |
| 361 | // Otherwise, a va_arg reads and writes. |
| 362 | return ModRef; |
| 363 | } |
| 364 | |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 365 | AliasAnalysis::ModRefResult |
| 366 | AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) { |
| 367 | // Acquire/Release cmpxchg has properties that matter for arbitrary addresses. |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 368 | if (CX->getSuccessOrdering() > Monotonic) |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 369 | return ModRef; |
| 370 | |
| 371 | // If the cmpxchg address does not alias the location, it does not access it. |
| 372 | if (!alias(getLocation(CX), Loc)) |
| 373 | return NoModRef; |
| 374 | |
| 375 | return ModRef; |
| 376 | } |
| 377 | |
| 378 | AliasAnalysis::ModRefResult |
| 379 | AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) { |
| 380 | // Acquire/Release atomicrmw has properties that matter for arbitrary addresses. |
| 381 | if (RMW->getOrdering() > Monotonic) |
| 382 | return ModRef; |
| 383 | |
| 384 | // If the atomicrmw address does not alias the location, it does not access it. |
| 385 | if (!alias(getLocation(RMW), Loc)) |
| 386 | return NoModRef; |
| 387 | |
| 388 | return ModRef; |
| 389 | } |
| 390 | |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 391 | namespace { |
| 392 | /// Only find pointer captures which happen before the given instruction. Uses |
| 393 | /// the dominator tree to determine whether one instruction is before another. |
Manman Ren | fe5a61e | 2013-01-04 19:19:47 +0000 | [diff] [blame] | 394 | /// Only support the case where the Value is defined in the same basic block |
| 395 | /// as the given instruction and the use. |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 396 | struct CapturesBefore : public CaptureTracker { |
| 397 | CapturesBefore(const Instruction *I, DominatorTree *DT) |
| 398 | : BeforeHere(I), DT(DT), Captured(false) {} |
| 399 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 400 | void tooManyUses() override { Captured = true; } |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 401 | |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 402 | bool shouldExplore(const Use *U) override { |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 403 | Instruction *I = cast<Instruction>(U->getUser()); |
| 404 | BasicBlock *BB = I->getParent(); |
Manman Ren | fe5a61e | 2013-01-04 19:19:47 +0000 | [diff] [blame] | 405 | // We explore this usage only if the usage can reach "BeforeHere". |
| 406 | // If use is not reachable from entry, there is no need to explore. |
| 407 | if (BeforeHere != I && !DT->isReachableFromEntry(BB)) |
| 408 | return false; |
| 409 | // If the value is defined in the same basic block as use and BeforeHere, |
| 410 | // there is no need to explore the use if BeforeHere dominates use. |
| 411 | // Check whether there is a path from I to BeforeHere. |
| 412 | if (BeforeHere != I && DT->dominates(BeforeHere, I) && |
Nick Lewycky | 0b68245 | 2013-07-27 01:24:00 +0000 | [diff] [blame] | 413 | !isPotentiallyReachable(I, BeforeHere, DT)) |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 414 | return false; |
| 415 | return true; |
| 416 | } |
| 417 | |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 418 | bool captured(const Use *U) override { |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 419 | Instruction *I = cast<Instruction>(U->getUser()); |
| 420 | BasicBlock *BB = I->getParent(); |
Manman Ren | fe5a61e | 2013-01-04 19:19:47 +0000 | [diff] [blame] | 421 | // Same logic as in shouldExplore. |
| 422 | if (BeforeHere != I && !DT->isReachableFromEntry(BB)) |
| 423 | return false; |
| 424 | if (BeforeHere != I && DT->dominates(BeforeHere, I) && |
Nick Lewycky | 0b68245 | 2013-07-27 01:24:00 +0000 | [diff] [blame] | 425 | !isPotentiallyReachable(I, BeforeHere, DT)) |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 426 | return false; |
| 427 | Captured = true; |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | const Instruction *BeforeHere; |
| 432 | DominatorTree *DT; |
| 433 | |
| 434 | bool Captured; |
| 435 | }; |
| 436 | } |
| 437 | |
| 438 | // FIXME: this is really just shoring-up a deficiency in alias analysis. |
| 439 | // BasicAA isn't willing to spend linear time determining whether an alloca |
| 440 | // was captured before or after this particular call, while we are. However, |
| 441 | // with a smarter AA in place, this test is just wasting compile time. |
| 442 | AliasAnalysis::ModRefResult |
| 443 | AliasAnalysis::callCapturesBefore(const Instruction *I, |
| 444 | const AliasAnalysis::Location &MemLoc, |
| 445 | DominatorTree *DT) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 446 | if (!DT || !DL) return AliasAnalysis::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 447 | |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 448 | const Value *Object = GetUnderlyingObject(MemLoc.Ptr, DL); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 449 | if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) || |
| 450 | isa<Constant>(Object)) |
| 451 | return AliasAnalysis::ModRef; |
| 452 | |
| 453 | ImmutableCallSite CS(I); |
| 454 | if (!CS.getInstruction() || CS.getInstruction() == Object) |
| 455 | return AliasAnalysis::ModRef; |
| 456 | |
| 457 | CapturesBefore CB(I, DT); |
| 458 | llvm::PointerMayBeCaptured(Object, &CB); |
| 459 | if (CB.Captured) |
| 460 | return AliasAnalysis::ModRef; |
| 461 | |
| 462 | unsigned ArgNo = 0; |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 463 | AliasAnalysis::ModRefResult R = AliasAnalysis::NoModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 464 | for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
| 465 | CI != CE; ++CI, ++ArgNo) { |
| 466 | // Only look at the no-capture or byval pointer arguments. If this |
| 467 | // pointer were passed to arguments that were neither of these, then it |
| 468 | // couldn't be no-capture. |
| 469 | if (!(*CI)->getType()->isPointerTy() || |
| 470 | (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo))) |
| 471 | continue; |
| 472 | |
| 473 | // If this is a no-capture pointer argument, see if we can tell that it |
| 474 | // is impossible to alias the pointer we're checking. If not, we have to |
| 475 | // assume that the call could touch the pointer, even though it doesn't |
| 476 | // escape. |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 477 | if (isNoAlias(AliasAnalysis::Location(*CI), |
| 478 | AliasAnalysis::Location(Object))) |
| 479 | continue; |
| 480 | if (CS.doesNotAccessMemory(ArgNo)) |
| 481 | continue; |
| 482 | if (CS.onlyReadsMemory(ArgNo)) { |
| 483 | R = AliasAnalysis::Ref; |
| 484 | continue; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 485 | } |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 486 | return AliasAnalysis::ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 487 | } |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 488 | return R; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 489 | } |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 491 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 492 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 493 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 494 | // support to not be included in the tool correctly! |
| 495 | // |
| 496 | AliasAnalysis::~AliasAnalysis() {} |
| 497 | |
Dan Gohman | 7a68b66 | 2008-05-30 00:02:02 +0000 | [diff] [blame] | 498 | /// InitializeAliasAnalysis - Subclasses must call this method to initialize the |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 499 | /// AliasAnalysis interface before any other methods are called. |
| 500 | /// |
| 501 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 502 | DataLayoutPass *DLP = P->getAnalysisIfAvailable<DataLayoutPass>(); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 503 | DL = DLP ? &DLP->getDataLayout() : nullptr; |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 504 | TLI = P->getAnalysisIfAvailable<TargetLibraryInfo>(); |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 505 | AA = &P->getAnalysis<AliasAnalysis>(); |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // getAnalysisUsage - All alias analysis implementations should invoke this |
Dan Gohman | 43d19d6 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 509 | // directly (using AliasAnalysis::getAnalysisUsage(AU)). |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 510 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 511 | AU.addRequired<AliasAnalysis>(); // All AA's chain |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 514 | /// getTypeStoreSize - Return the DataLayout store size for the given type, |
Dan Gohman | 43d19d6 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 515 | /// if known, or a conservative value otherwise. |
| 516 | /// |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 517 | uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 518 | return DL ? DL->getTypeStoreSize(Ty) : UnknownSize; |
Dan Gohman | 43d19d6 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 521 | /// canBasicBlockModify - Return true if it is possible for execution of the |
| 522 | /// specified basic block to modify the value pointed to by Ptr. |
| 523 | /// |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 524 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 525 | const Location &Loc) { |
| 526 | return canInstructionRangeModify(BB.front(), BB.back(), Loc); |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 529 | /// canInstructionRangeModify - Return true if it is possible for the execution |
| 530 | /// of the specified instructions to modify the value pointed to by Ptr. The |
| 531 | /// instructions to consider are all of the instructions in the range of [I1,I2] |
| 532 | /// INCLUSIVE. I1 and I2 must be in the same basic block. |
| 533 | /// |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 534 | bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, |
| 535 | const Instruction &I2, |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 536 | const Location &Loc) { |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 537 | assert(I1.getParent() == I2.getParent() && |
| 538 | "Instructions not in same basic block!"); |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 539 | BasicBlock::const_iterator I = &I1; |
| 540 | BasicBlock::const_iterator E = &I2; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 541 | ++E; // Convert from inclusive to exclusive range. |
| 542 | |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 543 | for (; I != E; ++I) // Check every instruction in range |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 544 | if (getModRefInfo(I, Loc) & Mod) |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 545 | return true; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 546 | return false; |
| 547 | } |
| 548 | |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 549 | /// isNoAliasCall - Return true if this pointer is returned by a noalias |
| 550 | /// function. |
| 551 | bool llvm::isNoAliasCall(const Value *V) { |
| 552 | if (isa<CallInst>(V) || isa<InvokeInst>(V)) |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 553 | return ImmutableCallSite(cast<Instruction>(V)) |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 554 | .paramHasAttr(0, Attribute::NoAlias); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 555 | return false; |
| 556 | } |
| 557 | |
Michael Kuperstein | f3e663a | 2013-05-28 08:17:48 +0000 | [diff] [blame] | 558 | /// isNoAliasArgument - Return true if this is an argument with the noalias |
| 559 | /// attribute. |
| 560 | bool llvm::isNoAliasArgument(const Value *V) |
| 561 | { |
| 562 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 563 | return A->hasNoAliasAttr(); |
| 564 | return false; |
| 565 | } |
| 566 | |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 567 | /// isIdentifiedObject - Return true if this pointer refers to a distinct and |
| 568 | /// identifiable object. This returns true for: |
Dan Gohman | 1f59f01 | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 569 | /// Global Variables and Functions (but not Global Aliases) |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 570 | /// Allocas and Mallocs |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 571 | /// ByVal and NoAlias Arguments |
| 572 | /// NoAlias returns |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 573 | /// |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 574 | bool llvm::isIdentifiedObject(const Value *V) { |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 575 | if (isa<AllocaInst>(V)) |
Dan Gohman | 1f59f01 | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 576 | return true; |
| 577 | if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 578 | return true; |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 579 | if (isNoAliasCall(V)) |
| 580 | return true; |
| 581 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 582 | return A->hasNoAliasAttr() || A->hasByValAttr(); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 583 | return false; |
| 584 | } |
Hal Finkel | c782aa5 | 2014-07-21 12:27:23 +0000 | [diff] [blame^] | 585 | |
| 586 | /// isIdentifiedFunctionLocal - Return true if V is umabigously identified |
| 587 | /// at the function-level. Different IdentifiedFunctionLocals can't alias. |
| 588 | /// Further, an IdentifiedFunctionLocal can not alias with any function |
| 589 | /// arguments other than itself, which is not necessarily true for |
| 590 | /// IdentifiedObjects. |
| 591 | bool llvm::isIdentifiedFunctionLocal(const Value *V) |
| 592 | { |
| 593 | return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V); |
| 594 | } |
| 595 | |