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" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 33 | #include "llvm/Type.h" |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 37 | // Register the AliasAnalysis interface, providing a nice name to refer to. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 38 | static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 39 | char AliasAnalysis::ID = 0; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // Default chaining methods |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | AliasAnalysis::AliasResult |
| 46 | AliasAnalysis::alias(const Value *V1, unsigned V1Size, |
| 47 | const Value *V2, unsigned V2Size) { |
| 48 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 49 | return AA->alias(V1, V1Size, V2, V2Size); |
| 50 | } |
| 51 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 52 | bool AliasAnalysis::pointsToConstantMemory(const Value *P) { |
| 53 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 54 | return AA->pointsToConstantMemory(P); |
| 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, |
| 69 | const Value *P, unsigned Size) { |
| 70 | // Don't assert AA because BasicAA calls us in order to make use of the |
| 71 | // logic here. |
| 72 | |
| 73 | ModRefBehavior MRB = getModRefBehavior(CS); |
| 74 | if (MRB == DoesNotAccessMemory) |
| 75 | return NoModRef; |
| 76 | |
| 77 | ModRefResult Mask = ModRef; |
| 78 | if (MRB == OnlyReadsMemory) |
| 79 | Mask = Ref; |
| 80 | else if (MRB == AliasAnalysis::AccessesArguments) { |
| 81 | bool doesAlias = false; |
| 82 | for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 83 | AI != AE; ++AI) |
| 84 | if (!isNoAlias(*AI, ~0U, P, Size)) { |
| 85 | doesAlias = true; |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | if (!doesAlias) |
| 90 | return NoModRef; |
| 91 | } |
| 92 | |
| 93 | // If P points to a constant memory location, the call definitely could not |
| 94 | // modify the memory location. |
| 95 | if ((Mask & Mod) && pointsToConstantMemory(P)) |
| 96 | Mask = ModRefResult(Mask & ~Mod); |
| 97 | |
| 98 | // If this is BasicAA, don't forward. |
| 99 | if (!AA) return Mask; |
| 100 | |
| 101 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 102 | // in any mask we've managed to compute. |
| 103 | return ModRefResult(AA->getModRefInfo(CS, P, Size) & Mask); |
| 104 | } |
| 105 | |
| 106 | AliasAnalysis::ModRefResult |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 107 | AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) { |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 108 | // Don't assert AA because BasicAA calls us in order to make use of the |
| 109 | // logic here. |
| 110 | |
| 111 | // If CS1 or CS2 are readnone, they don't interact. |
| 112 | ModRefBehavior CS1B = getModRefBehavior(CS1); |
| 113 | if (CS1B == DoesNotAccessMemory) return NoModRef; |
| 114 | |
| 115 | ModRefBehavior CS2B = getModRefBehavior(CS2); |
| 116 | if (CS2B == DoesNotAccessMemory) return NoModRef; |
| 117 | |
| 118 | // If they both only read from memory, there is no dependence. |
| 119 | if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory) |
| 120 | return NoModRef; |
| 121 | |
| 122 | AliasAnalysis::ModRefResult Mask = ModRef; |
| 123 | |
| 124 | // If CS1 only reads memory, the only dependence on CS2 can be |
| 125 | // from CS1 reading memory written by CS2. |
| 126 | if (CS1B == OnlyReadsMemory) |
| 127 | Mask = ModRefResult(Mask & Ref); |
| 128 | |
| 129 | // If CS2 only access memory through arguments, accumulate the mod/ref |
| 130 | // information from CS1's references to the memory referenced by |
| 131 | // CS2's arguments. |
| 132 | if (CS2B == AccessesArguments) { |
| 133 | AliasAnalysis::ModRefResult R = NoModRef; |
| 134 | for (ImmutableCallSite::arg_iterator |
| 135 | I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) { |
| 136 | R = ModRefResult((R | getModRefInfo(CS1, *I, UnknownSize)) & Mask); |
| 137 | if (R == Mask) |
| 138 | break; |
| 139 | } |
| 140 | return R; |
| 141 | } |
| 142 | |
| 143 | // If CS1 only accesses memory through arguments, check if CS2 references |
| 144 | // any of the memory referenced by CS1's arguments. If not, return NoModRef. |
| 145 | if (CS1B == AccessesArguments) { |
| 146 | AliasAnalysis::ModRefResult R = NoModRef; |
| 147 | for (ImmutableCallSite::arg_iterator |
| 148 | I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) |
| 149 | if (getModRefInfo(CS2, *I, UnknownSize) != NoModRef) { |
| 150 | R = Mask; |
| 151 | break; |
| 152 | } |
| 153 | if (R == NoModRef) |
| 154 | return R; |
| 155 | } |
| 156 | |
| 157 | // If this is BasicAA, don't forward. |
| 158 | if (!AA) return Mask; |
| 159 | |
| 160 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 161 | // in any mask we've managed to compute. |
| 162 | return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask); |
| 163 | } |
| 164 | |
| 165 | AliasAnalysis::ModRefBehavior |
| 166 | AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { |
| 167 | // Don't assert AA because BasicAA calls us in order to make use of the |
| 168 | // logic here. |
| 169 | |
| 170 | ModRefBehavior Min = UnknownModRefBehavior; |
| 171 | |
| 172 | // Call back into the alias analysis with the other form of getModRefBehavior |
| 173 | // to see if it can give a better response. |
| 174 | if (const Function *F = CS.getCalledFunction()) |
| 175 | Min = getModRefBehavior(F); |
| 176 | |
| 177 | // If this is BasicAA, don't forward. |
| 178 | if (!AA) return Min; |
| 179 | |
| 180 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 181 | // in any result we've managed to compute. |
| 182 | return std::min(AA->getModRefBehavior(CS), Min); |
| 183 | } |
| 184 | |
| 185 | AliasAnalysis::ModRefBehavior |
| 186 | AliasAnalysis::getModRefBehavior(const Function *F) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 187 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 188 | return AA->getModRefBehavior(F); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 191 | AliasAnalysis::DependenceResult |
| 192 | AliasAnalysis::getDependence(const Instruction *First, |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 193 | const Value *FirstPHITranslatedAddr, |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 194 | DependenceQueryFlags FirstFlags, |
| 195 | const Instruction *Second, |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 196 | const Value *SecondPHITranslatedAddr, |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 197 | DependenceQueryFlags SecondFlags) { |
| 198 | assert(AA && "AA didn't call InitializeAliasAnalyais in its run method!"); |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 199 | return AA->getDependence(First, FirstPHITranslatedAddr, FirstFlags, |
| 200 | Second, SecondPHITranslatedAddr, SecondFlags); |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 201 | } |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 202 | |
| 203 | //===----------------------------------------------------------------------===// |
| 204 | // AliasAnalysis non-virtual helper method implementation |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 207 | AliasAnalysis::ModRefResult |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 208 | AliasAnalysis::getModRefInfo(const LoadInst *L, const Value *P, unsigned Size) { |
Dan Gohman | b9db52d | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 209 | // Be conservative in the face of volatile. |
| 210 | if (L->isVolatile()) |
| 211 | return ModRef; |
| 212 | |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 213 | // If the load address doesn't alias the given address, it doesn't read |
| 214 | // or write the specified memory. |
| 215 | if (!alias(L->getOperand(0), getTypeStoreSize(L->getType()), P, Size)) |
| 216 | return NoModRef; |
| 217 | |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 218 | // Otherwise, a load just reads. |
| 219 | return Ref; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 222 | AliasAnalysis::ModRefResult |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 223 | AliasAnalysis::getModRefInfo(const StoreInst *S, const Value *P, unsigned Size) { |
Dan Gohman | b9db52d | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 224 | // Be conservative in the face of volatile. |
| 225 | if (S->isVolatile()) |
| 226 | return ModRef; |
| 227 | |
Dan Gohman | 9b8639c | 2010-08-06 18:10:45 +0000 | [diff] [blame] | 228 | // If the store address cannot alias the pointer in question, then the |
| 229 | // specified memory cannot be modified by the store. |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 230 | if (!alias(S->getOperand(1), |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 231 | getTypeStoreSize(S->getOperand(0)->getType()), P, Size)) |
Chris Lattner | f4d904d | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 232 | return NoModRef; |
| 233 | |
| 234 | // If the pointer is a pointer to constant memory, then it could not have been |
| 235 | // modified by this store. |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 236 | if (pointsToConstantMemory(P)) |
| 237 | return NoModRef; |
| 238 | |
| 239 | // Otherwise, a store just writes. |
| 240 | return Mod; |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Dan Gohman | e26a7b5 | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 243 | AliasAnalysis::ModRefResult |
| 244 | AliasAnalysis::getModRefInfo(const VAArgInst *V, const Value *P, unsigned Size) { |
| 245 | // If the va_arg address cannot alias the pointer in question, then the |
| 246 | // specified memory cannot be accessed by the va_arg. |
| 247 | if (!alias(V->getOperand(0), UnknownSize, P, Size)) |
| 248 | return NoModRef; |
| 249 | |
| 250 | // If the pointer is a pointer to constant memory, then it could not have been |
| 251 | // modified by this va_arg. |
| 252 | if (pointsToConstantMemory(P)) |
| 253 | return NoModRef; |
| 254 | |
| 255 | // Otherwise, a va_arg reads and writes. |
| 256 | return ModRef; |
| 257 | } |
| 258 | |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 259 | AliasAnalysis::DependenceResult |
| 260 | AliasAnalysis::getDependenceViaModRefInfo(const Instruction *First, |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 261 | const Value *FirstPHITranslatedAddr, |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 262 | DependenceQueryFlags FirstFlags, |
| 263 | const Instruction *Second, |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 264 | const Value *SecondPHITranslatedAddr, |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 265 | DependenceQueryFlags SecondFlags) { |
| 266 | if (const LoadInst *L = dyn_cast<LoadInst>(First)) { |
| 267 | // Be over-conservative with volatile for now. |
| 268 | if (L->isVolatile()) |
| 269 | return Unknown; |
| 270 | |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 271 | // If we don't have a phi-translated address, use the actual one. |
| 272 | if (!FirstPHITranslatedAddr) |
| 273 | FirstPHITranslatedAddr = L->getPointerOperand(); |
| 274 | |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 275 | // Forward this query to getModRefInfo. |
| 276 | switch (getModRefInfo(Second, |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 277 | FirstPHITranslatedAddr, |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 278 | getTypeStoreSize(L->getType()))) { |
| 279 | case NoModRef: |
| 280 | // Second doesn't reference First's memory, so they're independent. |
| 281 | return Independent; |
| 282 | |
| 283 | case Ref: |
| 284 | // Second only reads from the memory read from by First. If it |
| 285 | // also writes to any other memory, be conservative. |
| 286 | if (Second->mayWriteToMemory()) |
| 287 | return Unknown; |
| 288 | |
| 289 | // If it's loading the same size from the same address, we can |
| 290 | // give a more precise result. |
| 291 | if (const LoadInst *SecondL = dyn_cast<LoadInst>(Second)) { |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 292 | // If we don't have a phi-translated address, use the actual one. |
| 293 | if (!SecondPHITranslatedAddr) |
| 294 | SecondPHITranslatedAddr = SecondL->getPointerOperand(); |
| 295 | |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 296 | unsigned LSize = getTypeStoreSize(L->getType()); |
| 297 | unsigned SecondLSize = getTypeStoreSize(SecondL->getType()); |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 298 | if (alias(FirstPHITranslatedAddr, LSize, |
| 299 | SecondPHITranslatedAddr, SecondLSize) == |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 300 | MustAlias) { |
| 301 | // If the loads are the same size, it's ReadThenRead. |
| 302 | if (LSize == SecondLSize) |
| 303 | return ReadThenRead; |
| 304 | |
| 305 | // If the second load is smaller, it's only ReadThenReadSome. |
| 306 | if (LSize > SecondLSize) |
| 307 | return ReadThenReadSome; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Otherwise it's just two loads. |
| 312 | return Independent; |
| 313 | |
| 314 | case Mod: |
| 315 | // Second only writes to the memory read from by First. If it |
| 316 | // also reads from any other memory, be conservative. |
| 317 | if (Second->mayReadFromMemory()) |
| 318 | return Unknown; |
| 319 | |
| 320 | // If it's storing the same size to the same address, we can |
| 321 | // give a more precise result. |
| 322 | if (const StoreInst *SecondS = dyn_cast<StoreInst>(Second)) { |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 323 | // If we don't have a phi-translated address, use the actual one. |
| 324 | if (!SecondPHITranslatedAddr) |
| 325 | SecondPHITranslatedAddr = SecondS->getPointerOperand(); |
| 326 | |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 327 | unsigned LSize = getTypeStoreSize(L->getType()); |
| 328 | unsigned SecondSSize = getTypeStoreSize(SecondS->getType()); |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 329 | if (alias(FirstPHITranslatedAddr, LSize, |
| 330 | SecondPHITranslatedAddr, SecondSSize) == |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 331 | MustAlias) { |
| 332 | // If the load and the store are the same size, it's ReadThenWrite. |
| 333 | if (LSize == SecondSSize) |
| 334 | return ReadThenWrite; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Otherwise we don't know if it could be writing to other memory. |
| 339 | return Unknown; |
| 340 | |
| 341 | case ModRef: |
| 342 | // Second reads and writes to the memory read from by First. |
| 343 | // We don't have a way to express that. |
| 344 | return Unknown; |
| 345 | } |
| 346 | |
| 347 | } else if (const StoreInst *S = dyn_cast<StoreInst>(First)) { |
| 348 | // Be over-conservative with volatile for now. |
| 349 | if (S->isVolatile()) |
| 350 | return Unknown; |
| 351 | |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 352 | // If we don't have a phi-translated address, use the actual one. |
| 353 | if (!FirstPHITranslatedAddr) |
| 354 | FirstPHITranslatedAddr = S->getPointerOperand(); |
| 355 | |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 356 | // Forward this query to getModRefInfo. |
| 357 | switch (getModRefInfo(Second, |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 358 | FirstPHITranslatedAddr, |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 359 | getTypeStoreSize(S->getValueOperand()->getType()))) { |
| 360 | case NoModRef: |
| 361 | // Second doesn't reference First's memory, so they're independent. |
| 362 | return Independent; |
| 363 | |
| 364 | case Ref: |
| 365 | // Second only reads from the memory written to by First. If it |
| 366 | // also writes to any other memory, be conservative. |
| 367 | if (Second->mayWriteToMemory()) |
| 368 | return Unknown; |
| 369 | |
| 370 | // If it's loading the same size from the same address, we can |
| 371 | // give a more precise result. |
| 372 | if (const LoadInst *SecondL = dyn_cast<LoadInst>(Second)) { |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 373 | // If we don't have a phi-translated address, use the actual one. |
| 374 | if (!SecondPHITranslatedAddr) |
| 375 | SecondPHITranslatedAddr = SecondL->getPointerOperand(); |
| 376 | |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 377 | unsigned SSize = getTypeStoreSize(S->getValueOperand()->getType()); |
| 378 | unsigned SecondLSize = getTypeStoreSize(SecondL->getType()); |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 379 | if (alias(FirstPHITranslatedAddr, SSize, |
| 380 | SecondPHITranslatedAddr, SecondLSize) == |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 381 | MustAlias) { |
| 382 | // If the store and the load are the same size, it's WriteThenRead. |
| 383 | if (SSize == SecondLSize) |
| 384 | return WriteThenRead; |
| 385 | |
| 386 | // If the load is smaller, it's only WriteThenReadSome. |
| 387 | if (SSize > SecondLSize) |
| 388 | return WriteThenReadSome; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Otherwise we don't know if it could be reading from other memory. |
| 393 | return Unknown; |
| 394 | |
| 395 | case Mod: |
| 396 | // Second only writes to the memory written to by First. If it |
| 397 | // also reads from any other memory, be conservative. |
| 398 | if (Second->mayReadFromMemory()) |
| 399 | return Unknown; |
| 400 | |
| 401 | // If it's storing the same size to the same address, we can |
| 402 | // give a more precise result. |
| 403 | if (const StoreInst *SecondS = dyn_cast<StoreInst>(Second)) { |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 404 | // If we don't have a phi-translated address, use the actual one. |
| 405 | if (!SecondPHITranslatedAddr) |
| 406 | SecondPHITranslatedAddr = SecondS->getPointerOperand(); |
| 407 | |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 408 | unsigned SSize = getTypeStoreSize(S->getValueOperand()->getType()); |
| 409 | unsigned SecondSSize = getTypeStoreSize(SecondS->getType()); |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 410 | if (alias(FirstPHITranslatedAddr, SSize, |
| 411 | SecondPHITranslatedAddr, SecondSSize) == |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 412 | MustAlias) { |
| 413 | // If the stores are the same size, it's WriteThenWrite. |
| 414 | if (SSize == SecondSSize) |
| 415 | return WriteThenWrite; |
| 416 | |
| 417 | // If the second store is larger, it's only WriteSomeThenWrite. |
| 418 | if (SSize < SecondSSize) |
| 419 | return WriteSomeThenWrite; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // Otherwise we don't know if it could be writing to other memory. |
| 424 | return Unknown; |
| 425 | |
| 426 | case ModRef: |
| 427 | // Second reads and writes to the memory written to by First. |
| 428 | // We don't have a way to express that. |
| 429 | return Unknown; |
| 430 | } |
| 431 | |
| 432 | } else if (const VAArgInst *V = dyn_cast<VAArgInst>(First)) { |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 433 | // If we don't have a phi-translated address, use the actual one. |
| 434 | if (!FirstPHITranslatedAddr) |
| 435 | FirstPHITranslatedAddr = V->getPointerOperand(); |
| 436 | |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 437 | // Forward this query to getModRefInfo. |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 438 | if (getModRefInfo(Second, FirstPHITranslatedAddr, UnknownSize) == NoModRef) |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 439 | // Second doesn't reference First's memory, so they're independent. |
| 440 | return Independent; |
| 441 | |
| 442 | } else if (ImmutableCallSite FirstCS = cast<Value>(First)) { |
Dan Gohman | c8ddbda | 2010-09-09 18:37:31 +0000 | [diff] [blame] | 443 | assert(!FirstPHITranslatedAddr && |
| 444 | !SecondPHITranslatedAddr && |
| 445 | "PHI translation with calls not supported yet!"); |
| 446 | |
Dan Gohman | 6592411 | 2010-09-08 01:32:20 +0000 | [diff] [blame] | 447 | // If both instructions are calls/invokes we can use the two-callsite |
| 448 | // form of getModRefInfo. |
| 449 | if (ImmutableCallSite SecondCS = cast<Value>(Second)) |
| 450 | // getModRefInfo's arguments are backwards from intuition. |
| 451 | switch (getModRefInfo(SecondCS, FirstCS)) { |
| 452 | case NoModRef: |
| 453 | // Second doesn't reference First's memory, so they're independent. |
| 454 | return Independent; |
| 455 | |
| 456 | case Ref: |
| 457 | // If they're both read-only, there's no dependence. |
| 458 | if (FirstCS.onlyReadsMemory() && SecondCS.onlyReadsMemory()) |
| 459 | return Independent; |
| 460 | |
| 461 | // Otherwise it's not obvious what we can do here. |
| 462 | return Unknown; |
| 463 | |
| 464 | case Mod: |
| 465 | // It's not obvious what we can do here. |
| 466 | return Unknown; |
| 467 | |
| 468 | case ModRef: |
| 469 | // I know, right? |
| 470 | return Unknown; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // For anything else, be conservative. |
| 475 | return Unknown; |
| 476 | } |
Dan Gohman | e26a7b5 | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 477 | |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 478 | AliasAnalysis::ModRefBehavior |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 479 | AliasAnalysis::getIntrinsicModRefBehavior(unsigned iid) { |
Duncan Sands | d869b38 | 2009-02-14 10:56:35 +0000 | [diff] [blame] | 480 | #define GET_INTRINSIC_MODREF_BEHAVIOR |
| 481 | #include "llvm/Intrinsics.gen" |
| 482 | #undef GET_INTRINSIC_MODREF_BEHAVIOR |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 485 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 486 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 487 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 488 | // support to not be included in the tool correctly! |
| 489 | // |
| 490 | AliasAnalysis::~AliasAnalysis() {} |
| 491 | |
Dan Gohman | 5a56bf6 | 2008-05-30 00:02:02 +0000 | [diff] [blame] | 492 | /// InitializeAliasAnalysis - Subclasses must call this method to initialize the |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 493 | /// AliasAnalysis interface before any other methods are called. |
| 494 | /// |
| 495 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 496 | TD = P->getAnalysisIfAvailable<TargetData>(); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 497 | AA = &P->getAnalysis<AliasAnalysis>(); |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | // getAnalysisUsage - All alias analysis implementations should invoke this |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 501 | // directly (using AliasAnalysis::getAnalysisUsage(AU)). |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 502 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 503 | AU.addRequired<AliasAnalysis>(); // All AA's chain |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 506 | /// getTypeStoreSize - Return the TargetData store size for the given type, |
| 507 | /// if known, or a conservative value otherwise. |
| 508 | /// |
| 509 | unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) { |
| 510 | return TD ? TD->getTypeStoreSize(Ty) : ~0u; |
| 511 | } |
| 512 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 513 | /// canBasicBlockModify - Return true if it is possible for execution of the |
| 514 | /// specified basic block to modify the value pointed to by Ptr. |
| 515 | /// |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 516 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
| 517 | const Value *Ptr, unsigned Size) { |
| 518 | return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 521 | /// canInstructionRangeModify - Return true if it is possible for the execution |
| 522 | /// of the specified instructions to modify the value pointed to by Ptr. The |
| 523 | /// instructions to consider are all of the instructions in the range of [I1,I2] |
| 524 | /// INCLUSIVE. I1 and I2 must be in the same basic block. |
| 525 | /// |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 526 | bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, |
| 527 | const Instruction &I2, |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 528 | const Value *Ptr, unsigned Size) { |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 529 | assert(I1.getParent() == I2.getParent() && |
| 530 | "Instructions not in same basic block!"); |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 531 | BasicBlock::const_iterator I = &I1; |
| 532 | BasicBlock::const_iterator E = &I2; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 533 | ++E; // Convert from inclusive to exclusive range. |
| 534 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 535 | for (; I != E; ++I) // Check every instruction in range |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 536 | if (getModRefInfo(I, Ptr, Size) & Mod) |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 537 | return true; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 541 | /// isNoAliasCall - Return true if this pointer is returned by a noalias |
| 542 | /// function. |
| 543 | bool llvm::isNoAliasCall(const Value *V) { |
| 544 | if (isa<CallInst>(V) || isa<InvokeInst>(V)) |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 545 | return ImmutableCallSite(cast<Instruction>(V)) |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 546 | .paramHasAttr(0, Attribute::NoAlias); |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | /// isIdentifiedObject - Return true if this pointer refers to a distinct and |
| 551 | /// identifiable object. This returns true for: |
Dan Gohman | 5753a4a | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 552 | /// Global Variables and Functions (but not Global Aliases) |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 553 | /// Allocas and Mallocs |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 554 | /// ByVal and NoAlias Arguments |
| 555 | /// NoAlias returns |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 556 | /// |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 557 | bool llvm::isIdentifiedObject(const Value *V) { |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 558 | if (isa<AllocaInst>(V)) |
Dan Gohman | 5753a4a | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 559 | return true; |
| 560 | if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 561 | return true; |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 562 | if (isNoAliasCall(V)) |
| 563 | return true; |
| 564 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 565 | return A->hasNoAliasAttr() || A->hasByValAttr(); |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 566 | return false; |
| 567 | } |
| 568 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 569 | // Because of the way .a files work, we must force the BasicAA implementation to |
| 570 | // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run |
| 571 | // the risk of AliasAnalysis being used, but the default implementation not |
| 572 | // being linked into the tool that uses it. |
Reid Spencer | 4f1bd9e | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 573 | DEFINING_FILE_FOR(AliasAnalysis) |