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