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