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" |
Chad Rosier | 3a884f5 | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CaptureTracking.h" |
| 29 | #include "llvm/Analysis/Dominators.h" |
| 30 | #include "llvm/Analysis/ValueTracking.h" |
Reid Spencer | 6df60a9 | 2006-06-07 20:00:19 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 32 | #include "llvm/BasicBlock.h" |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 33 | #include "llvm/Function.h" |
Owen Anderson | cd89525 | 2009-02-03 06:27:22 +0000 | [diff] [blame] | 34 | #include "llvm/IntrinsicInst.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 35 | #include "llvm/Instructions.h" |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 36 | #include "llvm/LLVMContext.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 37 | #include "llvm/Type.h" |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 41 | // Register the AliasAnalysis interface, providing a nice name to refer to. |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 42 | INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA) |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 43 | char AliasAnalysis::ID = 0; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
| 46 | // Default chaining methods |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
| 49 | AliasAnalysis::AliasResult |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 50 | AliasAnalysis::alias(const Location &LocA, const Location &LocB) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 51 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 52 | return AA->alias(LocA, LocB); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Dan Gohman | a25e5db | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 55 | bool AliasAnalysis::pointsToConstantMemory(const Location &Loc, |
| 56 | bool OrLocal) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 57 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | a25e5db | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 58 | return AA->pointsToConstantMemory(Loc, OrLocal); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 61 | void AliasAnalysis::deleteValue(Value *V) { |
| 62 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 63 | AA->deleteValue(V); |
| 64 | } |
| 65 | |
| 66 | void AliasAnalysis::copyValue(Value *From, Value *To) { |
| 67 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 68 | AA->copyValue(From, To); |
| 69 | } |
| 70 | |
Owen Anderson | ab6acc6 | 2011-01-03 21:38:41 +0000 | [diff] [blame] | 71 | void AliasAnalysis::addEscapingUse(Use &U) { |
| 72 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 73 | AA->addEscapingUse(U); |
| 74 | } |
| 75 | |
| 76 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 77 | AliasAnalysis::ModRefResult |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 78 | AliasAnalysis::getModRefInfo(ImmutableCallSite CS, |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 79 | const Location &Loc) { |
Dan Gohman | 852dda4 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 80 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 81 | |
| 82 | ModRefBehavior MRB = getModRefBehavior(CS); |
| 83 | if (MRB == DoesNotAccessMemory) |
| 84 | return NoModRef; |
| 85 | |
| 86 | ModRefResult Mask = ModRef; |
Dan Gohman | c07661c | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 87 | if (onlyReadsMemory(MRB)) |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 88 | Mask = Ref; |
Dan Gohman | c07661c | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 89 | |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 90 | if (onlyAccessesArgPointees(MRB)) { |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 91 | bool doesAlias = false; |
Dan Gohman | bddc1ca | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 92 | if (doesAccessArgPointees(MRB)) { |
| 93 | MDNode *CSTag = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa); |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 94 | for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
Dan Gohman | bddc1ca | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 95 | AI != AE; ++AI) { |
| 96 | const Value *Arg = *AI; |
| 97 | if (!Arg->getType()->isPointerTy()) |
| 98 | continue; |
| 99 | Location CSLoc(Arg, UnknownSize, CSTag); |
| 100 | if (!isNoAlias(CSLoc, Loc)) { |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 101 | doesAlias = true; |
| 102 | break; |
| 103 | } |
Dan Gohman | bddc1ca | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 106 | if (!doesAlias) |
| 107 | return NoModRef; |
| 108 | } |
| 109 | |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 110 | // If Loc is a constant memory location, the call definitely could not |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 111 | // modify the memory location. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 112 | if ((Mask & Mod) && pointsToConstantMemory(Loc)) |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 113 | Mask = ModRefResult(Mask & ~Mod); |
| 114 | |
Dan Gohman | e46a388 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 115 | // If this is the end of the chain, don't forward. |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 116 | if (!AA) return Mask; |
| 117 | |
| 118 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 119 | // in any mask we've managed to compute. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 120 | return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | AliasAnalysis::ModRefResult |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 124 | AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) { |
Dan Gohman | 852dda4 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 125 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 126 | |
| 127 | // If CS1 or CS2 are readnone, they don't interact. |
| 128 | ModRefBehavior CS1B = getModRefBehavior(CS1); |
| 129 | if (CS1B == DoesNotAccessMemory) return NoModRef; |
| 130 | |
| 131 | ModRefBehavior CS2B = getModRefBehavior(CS2); |
| 132 | if (CS2B == DoesNotAccessMemory) return NoModRef; |
| 133 | |
| 134 | // If they both only read from memory, there is no dependence. |
Dan Gohman | c07661c | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 135 | if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B)) |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 136 | return NoModRef; |
| 137 | |
| 138 | AliasAnalysis::ModRefResult Mask = ModRef; |
| 139 | |
| 140 | // If CS1 only reads memory, the only dependence on CS2 can be |
| 141 | // from CS1 reading memory written by CS2. |
Dan Gohman | c07661c | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 142 | if (onlyReadsMemory(CS1B)) |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 143 | Mask = ModRefResult(Mask & Ref); |
| 144 | |
| 145 | // If CS2 only access memory through arguments, accumulate the mod/ref |
| 146 | // information from CS1's references to the memory referenced by |
| 147 | // CS2's arguments. |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 148 | if (onlyAccessesArgPointees(CS2B)) { |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 149 | AliasAnalysis::ModRefResult R = NoModRef; |
Dan Gohman | bddc1ca | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 150 | if (doesAccessArgPointees(CS2B)) { |
| 151 | MDNode *CS2Tag = CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa); |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 152 | for (ImmutableCallSite::arg_iterator |
| 153 | I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) { |
Dan Gohman | bddc1ca | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 154 | const Value *Arg = *I; |
| 155 | if (!Arg->getType()->isPointerTy()) |
| 156 | continue; |
| 157 | Location CS2Loc(Arg, UnknownSize, CS2Tag); |
| 158 | R = ModRefResult((R | getModRefInfo(CS1, CS2Loc)) & Mask); |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 159 | if (R == Mask) |
| 160 | break; |
| 161 | } |
Dan Gohman | bddc1ca | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 162 | } |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 163 | return R; |
| 164 | } |
| 165 | |
| 166 | // If CS1 only accesses memory through arguments, check if CS2 references |
| 167 | // any of the memory referenced by CS1's arguments. If not, return NoModRef. |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 168 | if (onlyAccessesArgPointees(CS1B)) { |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 169 | AliasAnalysis::ModRefResult R = NoModRef; |
Dan Gohman | bddc1ca | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 170 | if (doesAccessArgPointees(CS1B)) { |
| 171 | MDNode *CS1Tag = CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa); |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 172 | for (ImmutableCallSite::arg_iterator |
Dan Gohman | bddc1ca | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 173 | I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) { |
| 174 | const Value *Arg = *I; |
| 175 | if (!Arg->getType()->isPointerTy()) |
| 176 | continue; |
| 177 | Location CS1Loc(Arg, UnknownSize, CS1Tag); |
| 178 | if (getModRefInfo(CS2, CS1Loc) != NoModRef) { |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 179 | R = Mask; |
| 180 | break; |
| 181 | } |
Dan Gohman | bddc1ca | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 184 | if (R == NoModRef) |
| 185 | return R; |
| 186 | } |
| 187 | |
Dan Gohman | e46a388 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 188 | // If this is the end of the chain, don't forward. |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 189 | if (!AA) return Mask; |
| 190 | |
| 191 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 192 | // in any mask we've managed to compute. |
| 193 | return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask); |
| 194 | } |
| 195 | |
| 196 | AliasAnalysis::ModRefBehavior |
| 197 | AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { |
Dan Gohman | 852dda4 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 198 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 199 | |
| 200 | ModRefBehavior Min = UnknownModRefBehavior; |
| 201 | |
| 202 | // Call back into the alias analysis with the other form of getModRefBehavior |
| 203 | // to see if it can give a better response. |
| 204 | if (const Function *F = CS.getCalledFunction()) |
| 205 | Min = getModRefBehavior(F); |
| 206 | |
Dan Gohman | e46a388 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 207 | // If this is the end of the chain, don't forward. |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 208 | if (!AA) return Min; |
| 209 | |
| 210 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 211 | // in any result we've managed to compute. |
Dan Gohman | 42c31a7 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 212 | return ModRefBehavior(AA->getModRefBehavior(CS) & Min); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | AliasAnalysis::ModRefBehavior |
| 216 | AliasAnalysis::getModRefBehavior(const Function *F) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 217 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 6ce9d8b | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 218 | return AA->getModRefBehavior(F); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 221 | //===----------------------------------------------------------------------===// |
| 222 | // AliasAnalysis non-virtual helper method implementation |
| 223 | //===----------------------------------------------------------------------===// |
| 224 | |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 225 | AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) { |
| 226 | return Location(LI->getPointerOperand(), |
| 227 | getTypeStoreSize(LI->getType()), |
| 228 | LI->getMetadata(LLVMContext::MD_tbaa)); |
| 229 | } |
| 230 | |
| 231 | AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) { |
| 232 | return Location(SI->getPointerOperand(), |
| 233 | getTypeStoreSize(SI->getValueOperand()->getType()), |
| 234 | SI->getMetadata(LLVMContext::MD_tbaa)); |
| 235 | } |
| 236 | |
| 237 | AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) { |
| 238 | return Location(VI->getPointerOperand(), |
| 239 | UnknownSize, |
| 240 | VI->getMetadata(LLVMContext::MD_tbaa)); |
| 241 | } |
| 242 | |
Eli Friedman | 46cb5af | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 243 | AliasAnalysis::Location |
| 244 | AliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) { |
| 245 | return Location(CXI->getPointerOperand(), |
| 246 | getTypeStoreSize(CXI->getCompareOperand()->getType()), |
| 247 | CXI->getMetadata(LLVMContext::MD_tbaa)); |
| 248 | } |
| 249 | |
| 250 | AliasAnalysis::Location |
| 251 | AliasAnalysis::getLocation(const AtomicRMWInst *RMWI) { |
| 252 | return Location(RMWI->getPointerOperand(), |
| 253 | getTypeStoreSize(RMWI->getValOperand()->getType()), |
| 254 | RMWI->getMetadata(LLVMContext::MD_tbaa)); |
| 255 | } |
Chris Lattner | e90c5cb | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 256 | |
| 257 | AliasAnalysis::Location |
| 258 | AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) { |
| 259 | uint64_t Size = UnknownSize; |
| 260 | if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) |
| 261 | Size = C->getValue().getZExtValue(); |
| 262 | |
Dan Gohman | 387f28a | 2010-12-16 02:51:19 +0000 | [diff] [blame] | 263 | // memcpy/memmove can have TBAA tags. For memcpy, they apply |
| 264 | // to both the source and the destination. |
| 265 | MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa); |
| 266 | |
| 267 | return Location(MTI->getRawSource(), Size, TBAATag); |
Chris Lattner | e90c5cb | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | AliasAnalysis::Location |
Chris Lattner | 9dc9e81 | 2010-11-30 01:48:20 +0000 | [diff] [blame] | 271 | AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) { |
Chris Lattner | e90c5cb | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 272 | uint64_t Size = UnknownSize; |
| 273 | if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) |
| 274 | Size = C->getValue().getZExtValue(); |
Dan Gohman | 387f28a | 2010-12-16 02:51:19 +0000 | [diff] [blame] | 275 | |
| 276 | // memcpy/memmove can have TBAA tags. For memcpy, they apply |
| 277 | // to both the source and the destination. |
| 278 | MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa); |
Chris Lattner | e90c5cb | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 279 | |
Dan Gohman | 387f28a | 2010-12-16 02:51:19 +0000 | [diff] [blame] | 280 | return Location(MTI->getRawDest(), Size, TBAATag); |
Chris Lattner | e90c5cb | 2010-11-21 07:51:27 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
| 284 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 285 | AliasAnalysis::ModRefResult |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 286 | AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) { |
Eli Friedman | 667ccf2 | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 287 | // Be conservative in the face of volatile/atomic. |
| 288 | if (!L->isUnordered()) |
Dan Gohman | b9db52d | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 289 | return ModRef; |
| 290 | |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 291 | // If the load address doesn't alias the given address, it doesn't read |
| 292 | // or write the specified memory. |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 293 | if (!alias(getLocation(L), Loc)) |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 294 | return NoModRef; |
| 295 | |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 296 | // Otherwise, a load just reads. |
| 297 | return Ref; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 300 | AliasAnalysis::ModRefResult |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 301 | AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) { |
Eli Friedman | 667ccf2 | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 302 | // Be conservative in the face of volatile/atomic. |
| 303 | if (!S->isUnordered()) |
Dan Gohman | b9db52d | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 304 | return ModRef; |
| 305 | |
Dan Gohman | 9b8639c | 2010-08-06 18:10:45 +0000 | [diff] [blame] | 306 | // If the store address cannot alias the pointer in question, then the |
| 307 | // specified memory cannot be modified by the store. |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 308 | if (!alias(getLocation(S), Loc)) |
Chris Lattner | f4d904d | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 309 | return NoModRef; |
| 310 | |
| 311 | // If the pointer is a pointer to constant memory, then it could not have been |
| 312 | // modified by this store. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 313 | if (pointsToConstantMemory(Loc)) |
Dan Gohman | 14a498a | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 314 | return NoModRef; |
| 315 | |
| 316 | // Otherwise, a store just writes. |
| 317 | return Mod; |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Dan Gohman | e26a7b5 | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 320 | AliasAnalysis::ModRefResult |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 321 | AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) { |
Dan Gohman | e26a7b5 | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 322 | // If the va_arg address cannot alias the pointer in question, then the |
| 323 | // specified memory cannot be accessed by the va_arg. |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 324 | if (!alias(getLocation(V), Loc)) |
Dan Gohman | e26a7b5 | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 325 | return NoModRef; |
| 326 | |
| 327 | // If the pointer is a pointer to constant memory, then it could not have been |
| 328 | // modified by this va_arg. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 329 | if (pointsToConstantMemory(Loc)) |
Dan Gohman | e26a7b5 | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 330 | return NoModRef; |
| 331 | |
| 332 | // Otherwise, a va_arg reads and writes. |
| 333 | return ModRef; |
| 334 | } |
| 335 | |
Eli Friedman | 46cb5af | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 336 | AliasAnalysis::ModRefResult |
| 337 | AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) { |
| 338 | // Acquire/Release cmpxchg has properties that matter for arbitrary addresses. |
| 339 | if (CX->getOrdering() > Monotonic) |
| 340 | return ModRef; |
| 341 | |
| 342 | // If the cmpxchg address does not alias the location, it does not access it. |
| 343 | if (!alias(getLocation(CX), Loc)) |
| 344 | return NoModRef; |
| 345 | |
| 346 | return ModRef; |
| 347 | } |
| 348 | |
| 349 | AliasAnalysis::ModRefResult |
| 350 | AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) { |
| 351 | // Acquire/Release atomicrmw has properties that matter for arbitrary addresses. |
| 352 | if (RMW->getOrdering() > Monotonic) |
| 353 | return ModRef; |
| 354 | |
| 355 | // If the atomicrmw address does not alias the location, it does not access it. |
| 356 | if (!alias(getLocation(RMW), Loc)) |
| 357 | return NoModRef; |
| 358 | |
| 359 | return ModRef; |
| 360 | } |
| 361 | |
Chad Rosier | 3a884f5 | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 362 | namespace { |
| 363 | /// Only find pointer captures which happen before the given instruction. Uses |
| 364 | /// the dominator tree to determine whether one instruction is before another. |
| 365 | struct CapturesBefore : public CaptureTracker { |
| 366 | CapturesBefore(const Instruction *I, DominatorTree *DT) |
| 367 | : BeforeHere(I), DT(DT), Captured(false) {} |
| 368 | |
| 369 | void tooManyUses() { Captured = true; } |
| 370 | |
| 371 | bool shouldExplore(Use *U) { |
| 372 | Instruction *I = cast<Instruction>(U->getUser()); |
| 373 | BasicBlock *BB = I->getParent(); |
| 374 | if (BeforeHere != I && |
| 375 | (!DT->isReachableFromEntry(BB) || DT->dominates(BeforeHere, I))) |
| 376 | return false; |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | bool captured(Use *U) { |
| 381 | Instruction *I = cast<Instruction>(U->getUser()); |
| 382 | BasicBlock *BB = I->getParent(); |
| 383 | if (BeforeHere != I && |
| 384 | (!DT->isReachableFromEntry(BB) || DT->dominates(BeforeHere, I))) |
| 385 | return false; |
| 386 | Captured = true; |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | const Instruction *BeforeHere; |
| 391 | DominatorTree *DT; |
| 392 | |
| 393 | bool Captured; |
| 394 | }; |
| 395 | } |
| 396 | |
| 397 | // FIXME: this is really just shoring-up a deficiency in alias analysis. |
| 398 | // BasicAA isn't willing to spend linear time determining whether an alloca |
| 399 | // was captured before or after this particular call, while we are. However, |
| 400 | // with a smarter AA in place, this test is just wasting compile time. |
| 401 | AliasAnalysis::ModRefResult |
| 402 | AliasAnalysis::callCapturesBefore(const Instruction *I, |
| 403 | const AliasAnalysis::Location &MemLoc, |
| 404 | DominatorTree *DT) { |
| 405 | if (!DT || !TD) return AliasAnalysis::ModRef; |
| 406 | |
| 407 | const Value *Object = GetUnderlyingObject(MemLoc.Ptr, TD); |
| 408 | if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) || |
| 409 | isa<Constant>(Object)) |
| 410 | return AliasAnalysis::ModRef; |
| 411 | |
| 412 | ImmutableCallSite CS(I); |
| 413 | if (!CS.getInstruction() || CS.getInstruction() == Object) |
| 414 | return AliasAnalysis::ModRef; |
| 415 | |
| 416 | CapturesBefore CB(I, DT); |
| 417 | llvm::PointerMayBeCaptured(Object, &CB); |
| 418 | if (CB.Captured) |
| 419 | return AliasAnalysis::ModRef; |
| 420 | |
| 421 | unsigned ArgNo = 0; |
| 422 | for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
| 423 | CI != CE; ++CI, ++ArgNo) { |
| 424 | // Only look at the no-capture or byval pointer arguments. If this |
| 425 | // pointer were passed to arguments that were neither of these, then it |
| 426 | // couldn't be no-capture. |
| 427 | if (!(*CI)->getType()->isPointerTy() || |
| 428 | (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo))) |
| 429 | continue; |
| 430 | |
| 431 | // If this is a no-capture pointer argument, see if we can tell that it |
| 432 | // is impossible to alias the pointer we're checking. If not, we have to |
| 433 | // assume that the call could touch the pointer, even though it doesn't |
| 434 | // escape. |
| 435 | if (!isNoAlias(AliasAnalysis::Location(*CI), |
| 436 | AliasAnalysis::Location(Object))) { |
| 437 | return AliasAnalysis::ModRef; |
| 438 | } |
| 439 | } |
| 440 | return AliasAnalysis::NoModRef; |
| 441 | } |
Eli Friedman | 46cb5af | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 443 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 444 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 445 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 446 | // support to not be included in the tool correctly! |
| 447 | // |
| 448 | AliasAnalysis::~AliasAnalysis() {} |
| 449 | |
Dan Gohman | 5a56bf6 | 2008-05-30 00:02:02 +0000 | [diff] [blame] | 450 | /// InitializeAliasAnalysis - Subclasses must call this method to initialize the |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 451 | /// AliasAnalysis interface before any other methods are called. |
| 452 | /// |
| 453 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 454 | TD = P->getAnalysisIfAvailable<TargetData>(); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 455 | AA = &P->getAnalysis<AliasAnalysis>(); |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // getAnalysisUsage - All alias analysis implementations should invoke this |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 459 | // directly (using AliasAnalysis::getAnalysisUsage(AU)). |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 460 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 461 | AU.addRequired<AliasAnalysis>(); // All AA's chain |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 464 | /// getTypeStoreSize - Return the TargetData store size for the given type, |
| 465 | /// if known, or a conservative value otherwise. |
| 466 | /// |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 467 | uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) { |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 468 | return TD ? TD->getTypeStoreSize(Ty) : UnknownSize; |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 471 | /// canBasicBlockModify - Return true if it is possible for execution of the |
| 472 | /// specified basic block to modify the value pointed to by Ptr. |
| 473 | /// |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 474 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 475 | const Location &Loc) { |
| 476 | return canInstructionRangeModify(BB.front(), BB.back(), Loc); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 479 | /// canInstructionRangeModify - Return true if it is possible for the execution |
| 480 | /// of the specified instructions to modify the value pointed to by Ptr. The |
| 481 | /// instructions to consider are all of the instructions in the range of [I1,I2] |
| 482 | /// INCLUSIVE. I1 and I2 must be in the same basic block. |
| 483 | /// |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 484 | bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, |
| 485 | const Instruction &I2, |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 486 | const Location &Loc) { |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 487 | assert(I1.getParent() == I2.getParent() && |
| 488 | "Instructions not in same basic block!"); |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 489 | BasicBlock::const_iterator I = &I1; |
| 490 | BasicBlock::const_iterator E = &I2; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 491 | ++E; // Convert from inclusive to exclusive range. |
| 492 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 493 | for (; I != E; ++I) // Check every instruction in range |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 494 | if (getModRefInfo(I, Loc) & Mod) |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 495 | return true; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 496 | return false; |
| 497 | } |
| 498 | |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 499 | /// isNoAliasCall - Return true if this pointer is returned by a noalias |
| 500 | /// function. |
| 501 | bool llvm::isNoAliasCall(const Value *V) { |
| 502 | if (isa<CallInst>(V) || isa<InvokeInst>(V)) |
Dan Gohman | 79fca6f | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 503 | return ImmutableCallSite(cast<Instruction>(V)) |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 504 | .paramHasAttr(0, Attribute::NoAlias); |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | /// isIdentifiedObject - Return true if this pointer refers to a distinct and |
| 509 | /// identifiable object. This returns true for: |
Dan Gohman | 5753a4a | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 510 | /// Global Variables and Functions (but not Global Aliases) |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 511 | /// Allocas and Mallocs |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 512 | /// ByVal and NoAlias Arguments |
| 513 | /// NoAlias returns |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 514 | /// |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 515 | bool llvm::isIdentifiedObject(const Value *V) { |
Dan Gohman | 6be2bd5 | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 516 | if (isa<AllocaInst>(V)) |
Dan Gohman | 5753a4a | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 517 | return true; |
| 518 | if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 519 | return true; |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 520 | if (isNoAliasCall(V)) |
| 521 | return true; |
| 522 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 523 | return A->hasNoAliasAttr() || A->hasByValAttr(); |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 524 | return false; |
| 525 | } |
Nick Lewycky | 55f4ab8 | 2012-02-25 10:56:28 +0000 | [diff] [blame] | 526 | |
| 527 | /// isKnownNonNull - Return true if we know that the specified value is never |
| 528 | /// null. |
| 529 | bool llvm::isKnownNonNull(const Value *V) { |
| 530 | // Alloca never returns null, malloc might. |
| 531 | if (isa<AllocaInst>(V)) return true; |
| 532 | |
| 533 | // A byval argument is never null. |
| 534 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 535 | return A->hasByValAttr(); |
| 536 | |
| 537 | // Global values are not null unless extern weak. |
| 538 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 539 | return !GV->hasExternalWeakLinkage(); |
| 540 | return false; |
| 541 | } |