Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 1 | //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the generic AliasAnalysis interface which is used as the |
| 11 | // common interface used by all clients and implementations of alias analysis. |
| 12 | // |
| 13 | // This file also implements the default version of the AliasAnalysis interface |
| 14 | // that is to be used when no other implementation is specified. This does some |
| 15 | // simple tests that detect obvious cases: two different global pointers cannot |
| 16 | // alias, a global cannot alias a malloc, two different mallocs cannot alias, |
| 17 | // etc. |
| 18 | // |
| 19 | // This alias analysis implementation really isn't very good for anything, but |
| 20 | // it is very fast, and makes a nice clean default implementation. Because it |
| 21 | // handles lots of little corner cases, other, more complex, alias analysis |
| 22 | // implementations may choose to rely on this pass to resolve these simple and |
| 23 | // easy cases. |
| 24 | // |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/AliasAnalysis.h" |
Reid Spencer | 6df60a9 | 2006-06-07 20:00:19 +0000 | [diff] [blame] | 28 | #include "llvm/Pass.h" |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 29 | #include "llvm/BasicBlock.h" |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 30 | #include "llvm/Function.h" |
Owen Anderson | cd89525 | 2009-02-03 06:27:22 +0000 | [diff] [blame] | 31 | #include "llvm/IntrinsicInst.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 32 | #include "llvm/Instructions.h" |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 33 | #include "llvm/LLVMContext.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 34 | #include "llvm/Type.h" |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 36 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 38 | // Register the AliasAnalysis interface, providing a nice name to refer to. |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 39 | INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA) |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 40 | char AliasAnalysis::ID = 0; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // Default chaining methods |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
| 46 | AliasAnalysis::AliasResult |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 47 | AliasAnalysis::alias(const Location &LocA, const Location &LocB) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 48 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 49 | return AA->alias(LocA, LocB); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 52 | bool AliasAnalysis::pointsToConstantMemory(const Location &Loc) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 53 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 54 | return AA->pointsToConstantMemory(Loc); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 57 | void AliasAnalysis::deleteValue(Value *V) { |
| 58 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 59 | AA->deleteValue(V); |
| 60 | } |
| 61 | |
| 62 | void AliasAnalysis::copyValue(Value *From, Value *To) { |
| 63 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 64 | AA->copyValue(From, To); |
| 65 | } |
| 66 | |
| 67 | AliasAnalysis::ModRefResult |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 68 | AliasAnalysis::getModRefInfo(ImmutableCallSite CS, |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 69 | const Location &Loc) { |
Dan Gohman | 852dda4 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 70 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 71 | |
| 72 | ModRefBehavior MRB = getModRefBehavior(CS); |
| 73 | if (MRB == DoesNotAccessMemory) |
| 74 | return NoModRef; |
| 75 | |
| 76 | ModRefResult Mask = ModRef; |
| 77 | if (MRB == OnlyReadsMemory) |
| 78 | Mask = Ref; |
| 79 | else if (MRB == AliasAnalysis::AccessesArguments) { |
| 80 | bool doesAlias = false; |
| 81 | for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 82 | AI != AE; ++AI) |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 83 | if (!isNoAlias(Location(*AI), Loc)) { |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 84 | doesAlias = true; |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | if (!doesAlias) |
| 89 | return NoModRef; |
| 90 | } |
| 91 | |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 92 | // If Loc is a constant memory location, the call definitely could not |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 93 | // modify the memory location. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 94 | if ((Mask & Mod) && pointsToConstantMemory(Loc)) |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 95 | Mask = ModRefResult(Mask & ~Mod); |
| 96 | |
Dan Gohman | e46a388 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 97 | // If this is the end of the chain, don't forward. |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 98 | if (!AA) return Mask; |
| 99 | |
| 100 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 101 | // in any mask we've managed to compute. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 102 | return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | AliasAnalysis::ModRefResult |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 106 | AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) { |
Dan Gohman | 852dda4 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 107 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 108 | |
| 109 | // If CS1 or CS2 are readnone, they don't interact. |
| 110 | ModRefBehavior CS1B = getModRefBehavior(CS1); |
| 111 | if (CS1B == DoesNotAccessMemory) return NoModRef; |
| 112 | |
| 113 | ModRefBehavior CS2B = getModRefBehavior(CS2); |
| 114 | if (CS2B == DoesNotAccessMemory) return NoModRef; |
| 115 | |
| 116 | // If they both only read from memory, there is no dependence. |
| 117 | if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory) |
| 118 | return NoModRef; |
| 119 | |
| 120 | AliasAnalysis::ModRefResult Mask = ModRef; |
| 121 | |
| 122 | // If CS1 only reads memory, the only dependence on CS2 can be |
| 123 | // from CS1 reading memory written by CS2. |
| 124 | if (CS1B == OnlyReadsMemory) |
| 125 | Mask = ModRefResult(Mask & Ref); |
| 126 | |
| 127 | // If CS2 only access memory through arguments, accumulate the mod/ref |
| 128 | // information from CS1's references to the memory referenced by |
| 129 | // CS2's arguments. |
| 130 | if (CS2B == AccessesArguments) { |
| 131 | AliasAnalysis::ModRefResult R = NoModRef; |
| 132 | for (ImmutableCallSite::arg_iterator |
| 133 | I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) { |
| 134 | R = ModRefResult((R | getModRefInfo(CS1, *I, UnknownSize)) & Mask); |
| 135 | if (R == Mask) |
| 136 | break; |
| 137 | } |
| 138 | return R; |
| 139 | } |
| 140 | |
| 141 | // If CS1 only accesses memory through arguments, check if CS2 references |
| 142 | // any of the memory referenced by CS1's arguments. If not, return NoModRef. |
| 143 | if (CS1B == AccessesArguments) { |
| 144 | AliasAnalysis::ModRefResult R = NoModRef; |
| 145 | for (ImmutableCallSite::arg_iterator |
| 146 | I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) |
| 147 | if (getModRefInfo(CS2, *I, UnknownSize) != NoModRef) { |
| 148 | R = Mask; |
| 149 | break; |
| 150 | } |
| 151 | if (R == NoModRef) |
| 152 | return R; |
| 153 | } |
| 154 | |
Dan Gohman | e46a388 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 155 | // If this is the end of the chain, don't forward. |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 156 | if (!AA) return Mask; |
| 157 | |
| 158 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 159 | // in any mask we've managed to compute. |
| 160 | return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask); |
| 161 | } |
| 162 | |
| 163 | AliasAnalysis::ModRefBehavior |
| 164 | AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { |
Dan Gohman | 852dda4 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 165 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 166 | |
| 167 | ModRefBehavior Min = UnknownModRefBehavior; |
| 168 | |
| 169 | // Call back into the alias analysis with the other form of getModRefBehavior |
| 170 | // to see if it can give a better response. |
| 171 | if (const Function *F = CS.getCalledFunction()) |
| 172 | Min = getModRefBehavior(F); |
| 173 | |
Dan Gohman | e46a388 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 174 | // If this is the end of the chain, don't forward. |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 175 | if (!AA) return Min; |
| 176 | |
| 177 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 178 | // in any result we've managed to compute. |
| 179 | return std::min(AA->getModRefBehavior(CS), Min); |
| 180 | } |
| 181 | |
| 182 | AliasAnalysis::ModRefBehavior |
| 183 | AliasAnalysis::getModRefBehavior(const Function *F) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 184 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 185 | return AA->getModRefBehavior(F); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
| 189 | // AliasAnalysis non-virtual helper method implementation |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 192 | AliasAnalysis::ModRefResult |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 193 | AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) { |
Dan Gohman | b9db52d | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 194 | // Be conservative in the face of volatile. |
| 195 | if (L->isVolatile()) |
| 196 | return ModRef; |
| 197 | |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 198 | // If the load address doesn't alias the given address, it doesn't read |
| 199 | // or write the specified memory. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 200 | if (!alias(Location(L->getOperand(0), |
| 201 | getTypeStoreSize(L->getType()), |
| 202 | L->getMetadata(LLVMContext::MD_tbaa)), |
| 203 | Loc)) |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 204 | return NoModRef; |
| 205 | |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 206 | // Otherwise, a load just reads. |
| 207 | return Ref; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 210 | AliasAnalysis::ModRefResult |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 211 | AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) { |
Dan Gohman | b9db52d | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 212 | // Be conservative in the face of volatile. |
| 213 | if (S->isVolatile()) |
| 214 | return ModRef; |
| 215 | |
Dan Gohman | 9b8639c | 2010-08-06 18:10:45 +0000 | [diff] [blame] | 216 | // If the store address cannot alias the pointer in question, then the |
| 217 | // specified memory cannot be modified by the store. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 218 | if (!alias(Location(S->getOperand(1), |
| 219 | getTypeStoreSize(S->getOperand(0)->getType()), |
| 220 | S->getMetadata(LLVMContext::MD_tbaa)), |
| 221 | Loc)) |
Chris Lattner | f4d904d | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 222 | return NoModRef; |
| 223 | |
| 224 | // If the pointer is a pointer to constant memory, then it could not have been |
| 225 | // modified by this store. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 226 | if (pointsToConstantMemory(Loc)) |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 227 | return NoModRef; |
| 228 | |
| 229 | // Otherwise, a store just writes. |
| 230 | return Mod; |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Dan Gohman | e26a7b5 | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 233 | AliasAnalysis::ModRefResult |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 234 | AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) { |
Dan Gohman | e26a7b5 | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 235 | // If the va_arg address cannot alias the pointer in question, then the |
| 236 | // specified memory cannot be accessed by the va_arg. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 237 | if (!alias(Location(V->getOperand(0), |
| 238 | UnknownSize, |
| 239 | V->getMetadata(LLVMContext::MD_tbaa)), |
| 240 | Loc)) |
Dan Gohman | e26a7b5 | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 241 | return NoModRef; |
| 242 | |
| 243 | // If the pointer is a pointer to constant memory, then it could not have been |
| 244 | // modified by this va_arg. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 245 | if (pointsToConstantMemory(Loc)) |
Dan Gohman | e26a7b5 | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 246 | return NoModRef; |
| 247 | |
| 248 | // Otherwise, a va_arg reads and writes. |
| 249 | return ModRef; |
| 250 | } |
| 251 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 252 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 253 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 254 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 255 | // support to not be included in the tool correctly! |
| 256 | // |
| 257 | AliasAnalysis::~AliasAnalysis() {} |
| 258 | |
Dan Gohman | 5a56bf6 | 2008-05-30 00:02:02 +0000 | [diff] [blame] | 259 | /// InitializeAliasAnalysis - Subclasses must call this method to initialize the |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 260 | /// AliasAnalysis interface before any other methods are called. |
| 261 | /// |
| 262 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 263 | TD = P->getAnalysisIfAvailable<TargetData>(); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 264 | AA = &P->getAnalysis<AliasAnalysis>(); |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // getAnalysisUsage - All alias analysis implementations should invoke this |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 268 | // directly (using AliasAnalysis::getAnalysisUsage(AU)). |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 269 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 270 | AU.addRequired<AliasAnalysis>(); // All AA's chain |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 273 | /// getTypeStoreSize - Return the TargetData store size for the given type, |
| 274 | /// if known, or a conservative value otherwise. |
| 275 | /// |
Dan Gohman | 3da848b | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 276 | uint64_t AliasAnalysis::getTypeStoreSize(const Type *Ty) { |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 277 | return TD ? TD->getTypeStoreSize(Ty) : UnknownSize; |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 280 | /// canBasicBlockModify - Return true if it is possible for execution of the |
| 281 | /// specified basic block to modify the value pointed to by Ptr. |
| 282 | /// |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 283 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 284 | const Location &Loc) { |
| 285 | return canInstructionRangeModify(BB.front(), BB.back(), Loc); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 288 | /// canInstructionRangeModify - Return true if it is possible for the execution |
| 289 | /// of the specified instructions to modify the value pointed to by Ptr. The |
| 290 | /// instructions to consider are all of the instructions in the range of [I1,I2] |
| 291 | /// INCLUSIVE. I1 and I2 must be in the same basic block. |
| 292 | /// |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 293 | bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, |
| 294 | const Instruction &I2, |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 295 | const Location &Loc) { |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 296 | assert(I1.getParent() == I2.getParent() && |
| 297 | "Instructions not in same basic block!"); |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 298 | BasicBlock::const_iterator I = &I1; |
| 299 | BasicBlock::const_iterator E = &I2; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 300 | ++E; // Convert from inclusive to exclusive range. |
| 301 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 302 | for (; I != E; ++I) // Check every instruction in range |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 303 | if (getModRefInfo(I, Loc) & Mod) |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 304 | return true; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 308 | /// isNoAliasCall - Return true if this pointer is returned by a noalias |
| 309 | /// function. |
| 310 | bool llvm::isNoAliasCall(const Value *V) { |
| 311 | if (isa<CallInst>(V) || isa<InvokeInst>(V)) |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 312 | return ImmutableCallSite(cast<Instruction>(V)) |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 313 | .paramHasAttr(0, Attribute::NoAlias); |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | /// isIdentifiedObject - Return true if this pointer refers to a distinct and |
| 318 | /// identifiable object. This returns true for: |
Dan Gohman | 5753a4a | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 319 | /// Global Variables and Functions (but not Global Aliases) |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 320 | /// Allocas and Mallocs |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 321 | /// ByVal and NoAlias Arguments |
| 322 | /// NoAlias returns |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 323 | /// |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 324 | bool llvm::isIdentifiedObject(const Value *V) { |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 325 | if (isa<AllocaInst>(V)) |
Dan Gohman | 5753a4a | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 326 | return true; |
| 327 | if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 328 | return true; |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 329 | if (isNoAliasCall(V)) |
| 330 | return true; |
| 331 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 332 | return A->hasNoAliasAttr() || A->hasByValAttr(); |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 333 | return false; |
| 334 | } |