Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 1 | //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7d58f8d | 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 | d6a2a99 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/AliasAnalysis.h" |
Nick Lewycky | 0b68245 | 2013-07-27 01:24:00 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CFG.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/BasicBlock.h" |
| 33 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Function.h" |
| 36 | #include "llvm/IR/Instructions.h" |
| 37 | #include "llvm/IR/IntrinsicInst.h" |
| 38 | #include "llvm/IR/LLVMContext.h" |
| 39 | #include "llvm/IR/Type.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 40 | #include "llvm/Pass.h" |
Chris Lattner | a67dbd0 | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 43 | // Register the AliasAnalysis interface, providing a nice name to refer to. |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 44 | INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA) |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 45 | char AliasAnalysis::ID = 0; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // Default chaining methods |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 51 | AliasResult AliasAnalysis::alias(const MemoryLocation &LocA, |
| 52 | const MemoryLocation &LocB) { |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 53 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 54 | return AA->alias(LocA, LocB); |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 57 | bool AliasAnalysis::pointsToConstantMemory(const MemoryLocation &Loc, |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 58 | bool OrLocal) { |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 59 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 9130bad | 2010-11-08 16:45:26 +0000 | [diff] [blame] | 60 | return AA->pointsToConstantMemory(Loc, OrLocal); |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 63 | ModRefInfo AliasAnalysis::getArgModRefInfo(ImmutableCallSite CS, |
| 64 | unsigned ArgIdx) { |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 65 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 66 | return AA->getArgModRefInfo(CS, ArgIdx); |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 69 | ModRefInfo AliasAnalysis::getModRefInfo(Instruction *I, |
| 70 | ImmutableCallSite Call) { |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 71 | // We may have two calls |
| 72 | if (auto CS = ImmutableCallSite(I)) { |
| 73 | // Check if the two calls modify the same memory |
| 74 | return getModRefInfo(Call, CS); |
| 75 | } else { |
| 76 | // Otherwise, check if the call modifies or references the |
| 77 | // location this memory access defines. The best we can say |
| 78 | // is that if the call references what this instruction |
| 79 | // defines, it must be clobbered by this location. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 80 | const MemoryLocation DefLoc = MemoryLocation::get(I); |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 81 | if (getModRefInfo(Call, DefLoc) != MRI_NoModRef) |
| 82 | return MRI_ModRef; |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 83 | } |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 84 | return MRI_NoModRef; |
Daniel Berlin | 8de312d | 2015-04-13 23:25:41 +0000 | [diff] [blame] | 85 | } |
Owen Anderson | b6e4ff0 | 2011-01-03 21:38:41 +0000 | [diff] [blame] | 86 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 87 | ModRefInfo AliasAnalysis::getModRefInfo(ImmutableCallSite CS, |
| 88 | const MemoryLocation &Loc) { |
Dan Gohman | 1033ce6 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 89 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 90 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 91 | auto MRB = getModRefBehavior(CS); |
| 92 | if (MRB == FMRB_DoesNotAccessMemory) |
| 93 | return MRI_NoModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 94 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 95 | ModRefInfo Mask = MRI_ModRef; |
Dan Gohman | 5d06f89 | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 96 | if (onlyReadsMemory(MRB)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 97 | Mask = MRI_Ref; |
Dan Gohman | 5d06f89 | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 98 | |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 99 | if (onlyAccessesArgPointees(MRB)) { |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 100 | bool doesAlias = false; |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 101 | ModRefInfo AllArgsMask = MRI_NoModRef; |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 102 | if (doesAccessArgPointees(MRB)) { |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 103 | for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 104 | AI != AE; ++AI) { |
| 105 | const Value *Arg = *AI; |
| 106 | if (!Arg->getType()->isPointerTy()) |
| 107 | continue; |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 108 | unsigned ArgIdx = std::distance(CS.arg_begin(), AI); |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 109 | MemoryLocation ArgLoc = |
| 110 | MemoryLocation::getForArgument(CS, ArgIdx, *TLI); |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 111 | if (!isNoAlias(ArgLoc, Loc)) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 112 | ModRefInfo ArgMask = getArgModRefInfo(CS, ArgIdx); |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 113 | doesAlias = true; |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 114 | AllArgsMask = ModRefInfo(AllArgsMask | ArgMask); |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 115 | } |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 118 | if (!doesAlias) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 119 | return MRI_NoModRef; |
| 120 | Mask = ModRefInfo(Mask & AllArgsMask); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 123 | // If Loc is a constant memory location, the call definitely could not |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 124 | // modify the memory location. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 125 | if ((Mask & MRI_Mod) && pointsToConstantMemory(Loc)) |
| 126 | Mask = ModRefInfo(Mask & ~MRI_Mod); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 127 | |
Dan Gohman | abaf2d8 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 128 | // If this is the end of the chain, don't forward. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 129 | if (!AA) return Mask; |
| 130 | |
| 131 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 132 | // in any mask we've managed to compute. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 133 | return ModRefInfo(AA->getModRefInfo(CS, Loc) & Mask); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 136 | ModRefInfo AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, |
| 137 | ImmutableCallSite CS2) { |
Dan Gohman | 1033ce6 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 138 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 139 | |
| 140 | // If CS1 or CS2 are readnone, they don't interact. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 141 | auto CS1B = getModRefBehavior(CS1); |
| 142 | if (CS1B == FMRB_DoesNotAccessMemory) |
| 143 | return MRI_NoModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 144 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 145 | auto CS2B = getModRefBehavior(CS2); |
| 146 | if (CS2B == FMRB_DoesNotAccessMemory) |
| 147 | return MRI_NoModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 148 | |
| 149 | // If they both only read from memory, there is no dependence. |
Dan Gohman | 5d06f89 | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 150 | if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 151 | return MRI_NoModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 152 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 153 | ModRefInfo Mask = MRI_ModRef; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 154 | |
| 155 | // If CS1 only reads memory, the only dependence on CS2 can be |
| 156 | // from CS1 reading memory written by CS2. |
Dan Gohman | 5d06f89 | 2010-11-09 20:06:55 +0000 | [diff] [blame] | 157 | if (onlyReadsMemory(CS1B)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 158 | Mask = ModRefInfo(Mask & MRI_Ref); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 159 | |
| 160 | // If CS2 only access memory through arguments, accumulate the mod/ref |
| 161 | // information from CS1's references to the memory referenced by |
| 162 | // CS2's arguments. |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 163 | if (onlyAccessesArgPointees(CS2B)) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 164 | ModRefInfo R = MRI_NoModRef; |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 165 | if (doesAccessArgPointees(CS2B)) { |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 166 | for (ImmutableCallSite::arg_iterator |
| 167 | I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) { |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 168 | const Value *Arg = *I; |
| 169 | if (!Arg->getType()->isPointerTy()) |
| 170 | continue; |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 171 | unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I); |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 172 | auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, *TLI); |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 173 | |
| 174 | // ArgMask indicates what CS2 might do to CS2ArgLoc, and the dependence of |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 175 | // CS1 on that location is the inverse. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 176 | ModRefInfo ArgMask = getArgModRefInfo(CS2, CS2ArgIdx); |
| 177 | if (ArgMask == MRI_Mod) |
| 178 | ArgMask = MRI_ModRef; |
| 179 | else if (ArgMask == MRI_Ref) |
| 180 | ArgMask = MRI_Mod; |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 181 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 182 | R = ModRefInfo((R | (getModRefInfo(CS1, CS2ArgLoc) & ArgMask)) & Mask); |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 183 | if (R == Mask) |
| 184 | break; |
| 185 | } |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 186 | } |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 187 | return R; |
| 188 | } |
| 189 | |
| 190 | // If CS1 only accesses memory through arguments, check if CS2 references |
| 191 | // any of the memory referenced by CS1's arguments. If not, return NoModRef. |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 192 | if (onlyAccessesArgPointees(CS1B)) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 193 | ModRefInfo R = MRI_NoModRef; |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 194 | if (doesAccessArgPointees(CS1B)) { |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 195 | for (ImmutableCallSite::arg_iterator |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 196 | I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) { |
| 197 | const Value *Arg = *I; |
| 198 | if (!Arg->getType()->isPointerTy()) |
| 199 | continue; |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 200 | unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I); |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 201 | auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, *TLI); |
Chandler Carruth | c41404a | 2015-06-17 07:12:40 +0000 | [diff] [blame] | 202 | |
| 203 | // ArgMask indicates what CS1 might do to CS1ArgLoc; if CS1 might Mod |
| 204 | // CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If CS1 |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 205 | // might Ref, then we care only about a Mod by CS2. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 206 | ModRefInfo ArgMask = getArgModRefInfo(CS1, CS1ArgIdx); |
| 207 | ModRefInfo ArgR = getModRefInfo(CS2, CS1ArgLoc); |
| 208 | if (((ArgMask & MRI_Mod) != MRI_NoModRef && |
| 209 | (ArgR & MRI_ModRef) != MRI_NoModRef) || |
| 210 | ((ArgMask & MRI_Ref) != MRI_NoModRef && |
| 211 | (ArgR & MRI_Mod) != MRI_NoModRef)) |
| 212 | R = ModRefInfo((R | ArgMask) & Mask); |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 213 | |
| 214 | if (R == Mask) |
Dan Gohman | 066c1bb | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 215 | break; |
Dan Gohman | 39b3a1e | 2011-04-27 18:39:03 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
Hal Finkel | 354e23b | 2014-07-17 01:28:25 +0000 | [diff] [blame] | 218 | return R; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Dan Gohman | abaf2d8 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 221 | // If this is the end of the chain, don't forward. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 222 | if (!AA) return Mask; |
| 223 | |
| 224 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 225 | // in any mask we've managed to compute. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 226 | return ModRefInfo(AA->getModRefInfo(CS1, CS2) & Mask); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 229 | FunctionModRefBehavior AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { |
Dan Gohman | 1033ce6 | 2010-10-25 16:28:57 +0000 | [diff] [blame] | 230 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 231 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 232 | auto Min = FMRB_UnknownModRefBehavior; |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 233 | |
| 234 | // Call back into the alias analysis with the other form of getModRefBehavior |
| 235 | // to see if it can give a better response. |
| 236 | if (const Function *F = CS.getCalledFunction()) |
| 237 | Min = getModRefBehavior(F); |
| 238 | |
Dan Gohman | abaf2d8 | 2010-10-25 16:29:52 +0000 | [diff] [blame] | 239 | // If this is the end of the chain, don't forward. |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 240 | if (!AA) return Min; |
| 241 | |
| 242 | // Otherwise, fall back to the next AA in the chain. But we can merge |
| 243 | // in any result we've managed to compute. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 244 | return FunctionModRefBehavior(AA->getModRefBehavior(CS) & Min); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 247 | FunctionModRefBehavior AliasAnalysis::getModRefBehavior(const Function *F) { |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 248 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Dan Gohman | 5f1702e | 2010-08-06 01:25:49 +0000 | [diff] [blame] | 249 | return AA->getModRefBehavior(F); |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 252 | //===----------------------------------------------------------------------===// |
| 253 | // AliasAnalysis non-virtual helper method implementation |
| 254 | //===----------------------------------------------------------------------===// |
| 255 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 256 | ModRefInfo AliasAnalysis::getModRefInfo(const LoadInst *L, |
| 257 | const MemoryLocation &Loc) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 258 | // Be conservative in the face of volatile/atomic. |
| 259 | if (!L->isUnordered()) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 260 | return MRI_ModRef; |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 261 | |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 262 | // If the load address doesn't alias the given address, it doesn't read |
| 263 | // or write the specified memory. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 264 | if (Loc.Ptr && !alias(MemoryLocation::get(L), Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 265 | return MRI_NoModRef; |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 266 | |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 267 | // Otherwise, a load just reads. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 268 | return MRI_Ref; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 271 | ModRefInfo AliasAnalysis::getModRefInfo(const StoreInst *S, |
| 272 | const MemoryLocation &Loc) { |
Eli Friedman | 5494ada | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 273 | // Be conservative in the face of volatile/atomic. |
| 274 | if (!S->isUnordered()) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 275 | return MRI_ModRef; |
Dan Gohman | 6b4671b | 2010-08-06 18:11:28 +0000 | [diff] [blame] | 276 | |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 277 | if (Loc.Ptr) { |
| 278 | // If the store address cannot alias the pointer in question, then the |
| 279 | // specified memory cannot be modified by the store. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 280 | if (!alias(MemoryLocation::get(S), Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 281 | return MRI_NoModRef; |
Chris Lattner | 9605576 | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 282 | |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 283 | // If the pointer is a pointer to constant memory, then it could not have |
| 284 | // been modified by this store. |
| 285 | if (pointsToConstantMemory(Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 286 | return MRI_NoModRef; |
Daniel Berlin | b2d2276 | 2015-04-13 23:05:45 +0000 | [diff] [blame] | 287 | } |
Dan Gohman | 52f9d7d | 2010-08-03 17:27:43 +0000 | [diff] [blame] | 288 | |
| 289 | // Otherwise, a store just writes. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 290 | return MRI_Mod; |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 293 | ModRefInfo AliasAnalysis::getModRefInfo(const VAArgInst *V, |
| 294 | const MemoryLocation &Loc) { |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 295 | |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 296 | if (Loc.Ptr) { |
| 297 | // If the va_arg address cannot alias the pointer in question, then the |
| 298 | // specified memory cannot be accessed by the va_arg. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 299 | if (!alias(MemoryLocation::get(V), Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 300 | return MRI_NoModRef; |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 301 | |
| 302 | // If the pointer is a pointer to constant memory, then it could not have |
| 303 | // been modified by this va_arg. |
| 304 | if (pointsToConstantMemory(Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 305 | return MRI_NoModRef; |
Daniel Berlin | ec1de3f | 2015-04-28 19:19:14 +0000 | [diff] [blame] | 306 | } |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 307 | |
| 308 | // Otherwise, a va_arg reads and writes. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 309 | return MRI_ModRef; |
Dan Gohman | e68958f | 2010-08-06 18:24:38 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 312 | ModRefInfo AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, |
| 313 | const MemoryLocation &Loc) { |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 314 | // Acquire/Release cmpxchg has properties that matter for arbitrary addresses. |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 315 | if (CX->getSuccessOrdering() > Monotonic) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 316 | return MRI_ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 317 | |
| 318 | // If the cmpxchg address does not alias the location, it does not access it. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 319 | if (Loc.Ptr && !alias(MemoryLocation::get(CX), Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 320 | return MRI_NoModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 321 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 322 | return MRI_ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 325 | ModRefInfo AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, |
| 326 | const MemoryLocation &Loc) { |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 327 | // Acquire/Release atomicrmw has properties that matter for arbitrary addresses. |
| 328 | if (RMW->getOrdering() > Monotonic) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 329 | return MRI_ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 330 | |
| 331 | // If the atomicrmw address does not alias the location, it does not access it. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 332 | if (Loc.Ptr && !alias(MemoryLocation::get(RMW), Loc)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 333 | return MRI_NoModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 334 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 335 | return MRI_ModRef; |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame^] | 338 | /// \brief Return information about whether a particular call site modifies |
| 339 | /// or reads the specified memory location \p MemLoc before instruction \p I |
| 340 | /// in a BasicBlock. A ordered basic block \p OBB can be used to speed up |
| 341 | /// instruction-ordering queries inside the BasicBlock containing \p I. |
| 342 | /// FIXME: this is really just shoring-up a deficiency in alias analysis. |
| 343 | /// BasicAA isn't willing to spend linear time determining whether an alloca |
| 344 | /// was captured before or after this particular call, while we are. However, |
| 345 | /// with a smarter AA in place, this test is just wasting compile time. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 346 | ModRefInfo AliasAnalysis::callCapturesBefore(const Instruction *I, |
| 347 | const MemoryLocation &MemLoc, |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame^] | 348 | DominatorTree *DT, |
| 349 | OrderedBasicBlock *OBB) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 350 | if (!DT) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 351 | return MRI_ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 352 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 353 | const Value *Object = GetUnderlyingObject(MemLoc.Ptr, *DL); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 354 | if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) || |
| 355 | isa<Constant>(Object)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 356 | return MRI_ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 357 | |
| 358 | ImmutableCallSite CS(I); |
| 359 | if (!CS.getInstruction() || CS.getInstruction() == Object) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 360 | return MRI_ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 361 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 362 | if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true, |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 363 | /* StoreCaptures */ true, I, DT, |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame^] | 364 | /* include Object */ true, |
| 365 | /* OrderedBasicBlock */ OBB)) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 366 | return MRI_ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 367 | |
| 368 | unsigned ArgNo = 0; |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 369 | ModRefInfo R = MRI_NoModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 370 | for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
| 371 | CI != CE; ++CI, ++ArgNo) { |
| 372 | // Only look at the no-capture or byval pointer arguments. If this |
| 373 | // pointer were passed to arguments that were neither of these, then it |
| 374 | // couldn't be no-capture. |
| 375 | if (!(*CI)->getType()->isPointerTy() || |
| 376 | (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo))) |
| 377 | continue; |
| 378 | |
| 379 | // If this is a no-capture pointer argument, see if we can tell that it |
| 380 | // is impossible to alias the pointer we're checking. If not, we have to |
| 381 | // assume that the call could touch the pointer, even though it doesn't |
| 382 | // escape. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 383 | if (isNoAlias(MemoryLocation(*CI), MemoryLocation(Object))) |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 384 | continue; |
| 385 | if (CS.doesNotAccessMemory(ArgNo)) |
| 386 | continue; |
| 387 | if (CS.onlyReadsMemory(ArgNo)) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 388 | R = MRI_Ref; |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 389 | continue; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 390 | } |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 391 | return MRI_ModRef; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 392 | } |
Nick Lewycky | c051462 | 2013-07-07 10:15:16 +0000 | [diff] [blame] | 393 | return R; |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 394 | } |
Eli Friedman | 5c91891 | 2011-09-26 20:15:28 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 396 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 397 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 398 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 399 | // support to not be included in the tool correctly! |
| 400 | // |
| 401 | AliasAnalysis::~AliasAnalysis() {} |
| 402 | |
Dan Gohman | 7a68b66 | 2008-05-30 00:02:02 +0000 | [diff] [blame] | 403 | /// InitializeAliasAnalysis - Subclasses must call this method to initialize the |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 404 | /// AliasAnalysis interface before any other methods are called. |
| 405 | /// |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 406 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P, const DataLayout *NewDL) { |
| 407 | DL = NewDL; |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 408 | auto *TLIP = P->getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>(); |
| 409 | TLI = TLIP ? &TLIP->getTLI() : nullptr; |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 410 | AA = &P->getAnalysis<AliasAnalysis>(); |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // getAnalysisUsage - All alias analysis implementations should invoke this |
Dan Gohman | 43d19d6 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 414 | // directly (using AliasAnalysis::getAnalysisUsage(AU)). |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 415 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 62c3700 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 416 | AU.addRequired<AliasAnalysis>(); // All AA's chain |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 419 | /// getTypeStoreSize - Return the DataLayout store size for the given type, |
Dan Gohman | 43d19d6 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 420 | /// if known, or a conservative value otherwise. |
| 421 | /// |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 422 | uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) { |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 423 | return DL ? DL->getTypeStoreSize(Ty) : MemoryLocation::UnknownSize; |
Dan Gohman | 43d19d6 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 426 | /// canBasicBlockModify - Return true if it is possible for execution of the |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 427 | /// specified basic block to modify the location Loc. |
Chris Lattner | d922a84 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 428 | /// |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 429 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 430 | const MemoryLocation &Loc) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 431 | return canInstructionRangeModRef(BB.front(), BB.back(), Loc, MRI_Mod); |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 434 | /// canInstructionRangeModRef - Return true if it is possible for the |
| 435 | /// execution of the specified instructions to mod\ref (according to the |
| 436 | /// mode) the location Loc. The instructions to consider are all |
| 437 | /// of the instructions in the range of [I1,I2] INCLUSIVE. |
Teresa Johnson | bbcf75e | 2015-05-13 15:04:14 +0000 | [diff] [blame] | 438 | /// I1 and I2 must be in the same basic block. |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 439 | bool AliasAnalysis::canInstructionRangeModRef(const Instruction &I1, |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 440 | const Instruction &I2, |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 441 | const MemoryLocation &Loc, |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 442 | const ModRefInfo Mode) { |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 443 | assert(I1.getParent() == I2.getParent() && |
| 444 | "Instructions not in same basic block!"); |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 445 | BasicBlock::const_iterator I = &I1; |
| 446 | BasicBlock::const_iterator E = &I2; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 447 | ++E; // Convert from inclusive to exclusive range. |
| 448 | |
Chris Lattner | 3a31183 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 449 | for (; I != E; ++I) // Check every instruction in range |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 450 | if (getModRefInfo(I, Loc) & Mode) |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 451 | return true; |
Chris Lattner | 7d58f8d | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 452 | return false; |
| 453 | } |
| 454 | |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 455 | /// isNoAliasCall - Return true if this pointer is returned by a noalias |
| 456 | /// function. |
| 457 | bool llvm::isNoAliasCall(const Value *V) { |
| 458 | if (isa<CallInst>(V) || isa<InvokeInst>(V)) |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 459 | return ImmutableCallSite(cast<Instruction>(V)) |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 460 | .paramHasAttr(0, Attribute::NoAlias); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | |
Michael Kuperstein | f3e663a | 2013-05-28 08:17:48 +0000 | [diff] [blame] | 464 | /// isNoAliasArgument - Return true if this is an argument with the noalias |
| 465 | /// attribute. |
| 466 | bool llvm::isNoAliasArgument(const Value *V) |
| 467 | { |
| 468 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 469 | return A->hasNoAliasAttr(); |
| 470 | return false; |
| 471 | } |
| 472 | |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 473 | /// isIdentifiedObject - Return true if this pointer refers to a distinct and |
| 474 | /// identifiable object. This returns true for: |
Dan Gohman | 1f59f01 | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 475 | /// Global Variables and Functions (but not Global Aliases) |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 476 | /// Allocas and Mallocs |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 477 | /// ByVal and NoAlias Arguments |
| 478 | /// NoAlias returns |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 479 | /// |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 480 | bool llvm::isIdentifiedObject(const Value *V) { |
Dan Gohman | 0824aff | 2010-06-29 00:50:39 +0000 | [diff] [blame] | 481 | if (isa<AllocaInst>(V)) |
Dan Gohman | 1f59f01 | 2009-08-27 17:52:56 +0000 | [diff] [blame] | 482 | return true; |
| 483 | if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 484 | return true; |
Dan Gohman | 00ef932 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 485 | if (isNoAliasCall(V)) |
| 486 | return true; |
| 487 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 488 | return A->hasNoAliasAttr() || A->hasByValAttr(); |
Dan Gohman | fb306c0 | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 489 | return false; |
| 490 | } |
Hal Finkel | c782aa5 | 2014-07-21 12:27:23 +0000 | [diff] [blame] | 491 | |
| 492 | /// isIdentifiedFunctionLocal - Return true if V is umabigously identified |
| 493 | /// at the function-level. Different IdentifiedFunctionLocals can't alias. |
| 494 | /// Further, an IdentifiedFunctionLocal can not alias with any function |
| 495 | /// arguments other than itself, which is not necessarily true for |
| 496 | /// IdentifiedObjects. |
| 497 | bool llvm::isIdentifiedFunctionLocal(const Value *V) |
| 498 | { |
| 499 | return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V); |
| 500 | } |