Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 1 | //===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +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 | // |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This simple pass provides alias and mod/ref information for global values |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 11 | // that do not have their address taken, and keeps track of whether functions |
| 12 | // read or write memory (are "pure"). For this simple (but very common) case, |
| 13 | // we can provide pretty accurate and useful information. |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SCCIterator.h" |
Chandler Carruth | f3af4af | 2015-07-22 11:47:54 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
Victor Hernandez | f390e04 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 25 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Instructions.h" |
| 27 | #include "llvm/IR/IntrinsicInst.h" |
| 28 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Pass.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "globalsmodref-aa" |
| 34 | |
Chris Lattner | 57ef942 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 35 | STATISTIC(NumNonAddrTakenGlobalVars, |
| 36 | "Number of global vars without address taken"); |
| 37 | STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken"); |
| 38 | STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory"); |
| 39 | STATISTIC(NumReadMemFunctions, "Number of functions that only read memory"); |
| 40 | STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects"); |
| 41 | |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 42 | // An option to enable unsafe alias results from the GlobalsModRef analysis. |
| 43 | // When enabled, GlobalsModRef will provide no-alias results which in extremely |
| 44 | // rare cases may not be conservatively correct. In particular, in the face of |
| 45 | // transforms which cause assymetry between how effective GetUnderlyingObject |
| 46 | // is for two pointers, it may produce incorrect results. |
| 47 | // |
| 48 | // These unsafe results have been returned by GMR for many years without |
| 49 | // causing significant issues in the wild and so we provide a mechanism to |
| 50 | // re-enable them for users of LLVM that have a particular performance |
| 51 | // sensitivity and no known issues. The option also makes it easy to evaluate |
| 52 | // the performance impact of these results. |
| 53 | static cl::opt<bool> EnableUnsafeGlobalsModRefAliasResults( |
| 54 | "enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden); |
| 55 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 56 | /// The mod/ref information collected for a particular function. |
| 57 | /// |
| 58 | /// We collect information about mod/ref behavior of a function here, both in |
| 59 | /// general and as pertains to specific globals. We only have this detailed |
| 60 | /// information when we know *something* useful about the behavior. If we |
| 61 | /// saturate to fully general mod/ref, we remove the info for the function. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 62 | class GlobalsAAResult::FunctionInfo { |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 63 | typedef SmallDenseMap<const GlobalValue *, ModRefInfo, 16> GlobalInfoMapType; |
| 64 | |
| 65 | /// Build a wrapper struct that has 8-byte alignment. All heap allocations |
| 66 | /// should provide this much alignment at least, but this makes it clear we |
| 67 | /// specifically rely on this amount of alignment. |
| 68 | struct LLVM_ALIGNAS(8) AlignedMap { |
| 69 | AlignedMap() {} |
| 70 | AlignedMap(const AlignedMap &Arg) : Map(Arg.Map) {} |
| 71 | GlobalInfoMapType Map; |
| 72 | }; |
| 73 | |
| 74 | /// Pointer traits for our aligned map. |
| 75 | struct AlignedMapPointerTraits { |
| 76 | static inline void *getAsVoidPointer(AlignedMap *P) { return P; } |
| 77 | static inline AlignedMap *getFromVoidPointer(void *P) { |
| 78 | return (AlignedMap *)P; |
| 79 | } |
| 80 | enum { NumLowBitsAvailable = 3 }; |
| 81 | static_assert(AlignOf<AlignedMap>::Alignment >= (1 << NumLowBitsAvailable), |
| 82 | "AlignedMap insufficiently aligned to have enough low bits."); |
| 83 | }; |
| 84 | |
| 85 | /// The bit that flags that this function may read any global. This is |
| 86 | /// chosen to mix together with ModRefInfo bits. |
| 87 | enum { MayReadAnyGlobal = 4 }; |
| 88 | |
| 89 | /// Checks to document the invariants of the bit packing here. |
| 90 | static_assert((MayReadAnyGlobal & MRI_ModRef) == 0, |
| 91 | "ModRef and the MayReadAnyGlobal flag bits overlap."); |
| 92 | static_assert(((MayReadAnyGlobal | MRI_ModRef) >> |
| 93 | AlignedMapPointerTraits::NumLowBitsAvailable) == 0, |
| 94 | "Insufficient low bits to store our flag and ModRef info."); |
| 95 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 96 | public: |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 97 | FunctionInfo() : Info() {} |
| 98 | ~FunctionInfo() { |
| 99 | delete Info.getPointer(); |
| 100 | } |
| 101 | // Spell out the copy ond move constructors and assignment operators to get |
| 102 | // deep copy semantics and correct move semantics in the face of the |
| 103 | // pointer-int pair. |
| 104 | FunctionInfo(const FunctionInfo &Arg) |
| 105 | : Info(nullptr, Arg.Info.getInt()) { |
| 106 | if (const auto *ArgPtr = Arg.Info.getPointer()) |
| 107 | Info.setPointer(new AlignedMap(*ArgPtr)); |
| 108 | } |
| 109 | FunctionInfo(FunctionInfo &&Arg) |
| 110 | : Info(Arg.Info.getPointer(), Arg.Info.getInt()) { |
| 111 | Arg.Info.setPointerAndInt(nullptr, 0); |
| 112 | } |
| 113 | FunctionInfo &operator=(const FunctionInfo &RHS) { |
| 114 | delete Info.getPointer(); |
| 115 | Info.setPointerAndInt(nullptr, RHS.Info.getInt()); |
| 116 | if (const auto *RHSPtr = RHS.Info.getPointer()) |
| 117 | Info.setPointer(new AlignedMap(*RHSPtr)); |
| 118 | return *this; |
| 119 | } |
| 120 | FunctionInfo &operator=(FunctionInfo &&RHS) { |
| 121 | delete Info.getPointer(); |
| 122 | Info.setPointerAndInt(RHS.Info.getPointer(), RHS.Info.getInt()); |
| 123 | RHS.Info.setPointerAndInt(nullptr, 0); |
| 124 | return *this; |
| 125 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 126 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 127 | /// Returns the \c ModRefInfo info for this function. |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 128 | ModRefInfo getModRefInfo() const { |
| 129 | return ModRefInfo(Info.getInt() & MRI_ModRef); |
| 130 | } |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 131 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 132 | /// Adds new \c ModRefInfo for this function to its state. |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 133 | void addModRefInfo(ModRefInfo NewMRI) { |
| 134 | Info.setInt(Info.getInt() | NewMRI); |
| 135 | } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 136 | |
| 137 | /// Returns whether this function may read any global variable, and we don't |
| 138 | /// know which global. |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 139 | bool mayReadAnyGlobal() const { return Info.getInt() & MayReadAnyGlobal; } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 140 | |
| 141 | /// Sets this function as potentially reading from any global. |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 142 | void setMayReadAnyGlobal() { Info.setInt(Info.getInt() | MayReadAnyGlobal); } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 143 | |
| 144 | /// Returns the \c ModRefInfo info for this function w.r.t. a particular |
| 145 | /// global, which may be more precise than the general information above. |
| 146 | ModRefInfo getModRefInfoForGlobal(const GlobalValue &GV) const { |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 147 | ModRefInfo GlobalMRI = mayReadAnyGlobal() ? MRI_Ref : MRI_NoModRef; |
| 148 | if (AlignedMap *P = Info.getPointer()) { |
| 149 | auto I = P->Map.find(&GV); |
| 150 | if (I != P->Map.end()) |
| 151 | GlobalMRI = ModRefInfo(GlobalMRI | I->second); |
| 152 | } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 153 | return GlobalMRI; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 156 | /// Add mod/ref info from another function into ours, saturating towards |
| 157 | /// MRI_ModRef. |
| 158 | void addFunctionInfo(const FunctionInfo &FI) { |
| 159 | addModRefInfo(FI.getModRefInfo()); |
| 160 | |
| 161 | if (FI.mayReadAnyGlobal()) |
| 162 | setMayReadAnyGlobal(); |
| 163 | |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 164 | if (AlignedMap *P = FI.Info.getPointer()) |
| 165 | for (const auto &G : P->Map) |
| 166 | addModRefInfoForGlobal(*G.first, G.second); |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 167 | } |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 168 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 169 | void addModRefInfoForGlobal(const GlobalValue &GV, ModRefInfo NewMRI) { |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 170 | AlignedMap *P = Info.getPointer(); |
| 171 | if (!P) { |
| 172 | P = new AlignedMap(); |
| 173 | Info.setPointer(P); |
| 174 | } |
| 175 | auto &GlobalMRI = P->Map[&GV]; |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 176 | GlobalMRI = ModRefInfo(GlobalMRI | NewMRI); |
| 177 | } |
| 178 | |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 179 | /// Clear a global's ModRef info. Should be used when a global is being |
| 180 | /// deleted. |
| 181 | void eraseModRefInfoForGlobal(const GlobalValue &GV) { |
| 182 | if (AlignedMap *P = Info.getPointer()) |
| 183 | P->Map.erase(&GV); |
| 184 | } |
| 185 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 186 | private: |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 187 | /// All of the information is encoded into a single pointer, with a three bit |
| 188 | /// integer in the low three bits. The high bit provides a flag for when this |
| 189 | /// function may read any global. The low two bits are the ModRefInfo. And |
| 190 | /// the pointer, when non-null, points to a map from GlobalValue to |
| 191 | /// ModRefInfo specific to that GlobalValue. |
| 192 | PointerIntPair<AlignedMap *, 3, unsigned, AlignedMapPointerTraits> Info; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 195 | void GlobalsAAResult::DeletionCallbackHandle::deleted() { |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 196 | Value *V = getValPtr(); |
| 197 | if (auto *F = dyn_cast<Function>(V)) |
NAKAMURA Takumi | 98800d9 | 2015-09-14 06:16:44 +0000 | [diff] [blame] | 198 | GAR->FunctionInfos.erase(F); |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 199 | |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 200 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
NAKAMURA Takumi | 98800d9 | 2015-09-14 06:16:44 +0000 | [diff] [blame] | 201 | if (GAR->NonAddressTakenGlobals.erase(GV)) { |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 202 | // This global might be an indirect global. If so, remove it and |
| 203 | // remove any AllocRelatedValues for it. |
NAKAMURA Takumi | 98800d9 | 2015-09-14 06:16:44 +0000 | [diff] [blame] | 204 | if (GAR->IndirectGlobals.erase(GV)) { |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 205 | // Remove any entries in AllocsForIndirectGlobals for this global. |
NAKAMURA Takumi | 98800d9 | 2015-09-14 06:16:44 +0000 | [diff] [blame] | 206 | for (auto I = GAR->AllocsForIndirectGlobals.begin(), |
| 207 | E = GAR->AllocsForIndirectGlobals.end(); |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 208 | I != E; ++I) |
| 209 | if (I->second == GV) |
NAKAMURA Takumi | 98800d9 | 2015-09-14 06:16:44 +0000 | [diff] [blame] | 210 | GAR->AllocsForIndirectGlobals.erase(I); |
Chandler Carruth | 8f1b63e | 2015-07-22 11:10:41 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 213 | // Scan the function info we have collected and remove this global |
| 214 | // from all of them. |
NAKAMURA Takumi | 98800d9 | 2015-09-14 06:16:44 +0000 | [diff] [blame] | 215 | for (auto &FIPair : GAR->FunctionInfos) |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 216 | FIPair.second.eraseModRefInfoForGlobal(*GV); |
Chandler Carruth | 8f1b63e | 2015-07-22 11:10:41 +0000 | [diff] [blame] | 217 | } |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 220 | // If this is an allocation related to an indirect global, remove it. |
NAKAMURA Takumi | 98800d9 | 2015-09-14 06:16:44 +0000 | [diff] [blame] | 221 | GAR->AllocsForIndirectGlobals.erase(V); |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 222 | |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 223 | // And clear out the handle. |
| 224 | setValPtr(nullptr); |
NAKAMURA Takumi | 98800d9 | 2015-09-14 06:16:44 +0000 | [diff] [blame] | 225 | GAR->Handles.erase(I); |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 226 | // This object is now destroyed! |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 227 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 228 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 229 | FunctionModRefBehavior GlobalsAAResult::getModRefBehavior(const Function *F) { |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 230 | FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior; |
| 231 | |
| 232 | if (FunctionInfo *FI = getFunctionInfo(F)) { |
| 233 | if (FI->getModRefInfo() == MRI_NoModRef) |
| 234 | Min = FMRB_DoesNotAccessMemory; |
| 235 | else if ((FI->getModRefInfo() & MRI_Mod) == 0) |
| 236 | Min = FMRB_OnlyReadsMemory; |
| 237 | } |
| 238 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 239 | return FunctionModRefBehavior(AAResultBase::getModRefBehavior(F) & Min); |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 242 | FunctionModRefBehavior |
| 243 | GlobalsAAResult::getModRefBehavior(ImmutableCallSite CS) { |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 244 | FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior; |
| 245 | |
Sanjoy Das | ca2edc7 | 2016-02-09 02:31:47 +0000 | [diff] [blame] | 246 | if (!CS.hasOperandBundles()) |
| 247 | if (const Function *F = CS.getCalledFunction()) |
| 248 | if (FunctionInfo *FI = getFunctionInfo(F)) { |
| 249 | if (FI->getModRefInfo() == MRI_NoModRef) |
| 250 | Min = FMRB_DoesNotAccessMemory; |
| 251 | else if ((FI->getModRefInfo() & MRI_Mod) == 0) |
| 252 | Min = FMRB_OnlyReadsMemory; |
| 253 | } |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 254 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 255 | return FunctionModRefBehavior(AAResultBase::getModRefBehavior(CS) & Min); |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /// Returns the function info for the function, or null if we don't have |
| 259 | /// anything useful to say about it. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 260 | GlobalsAAResult::FunctionInfo * |
| 261 | GlobalsAAResult::getFunctionInfo(const Function *F) { |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 262 | auto I = FunctionInfos.find(F); |
| 263 | if (I != FunctionInfos.end()) |
| 264 | return &I->second; |
| 265 | return nullptr; |
| 266 | } |
| 267 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 268 | /// AnalyzeGlobals - Scan through the users of all of the internal |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 269 | /// GlobalValue's in the program. If none of them have their "address taken" |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 270 | /// (really, their address passed to something nontrivial), record this fact, |
| 271 | /// and record the functions that they are used directly in. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 272 | void GlobalsAAResult::AnalyzeGlobals(Module &M) { |
Matthias Braun | b30f2f51 | 2016-01-30 01:24:31 +0000 | [diff] [blame] | 273 | SmallPtrSet<Function *, 32> TrackedFunctions; |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 274 | for (Function &F : M) |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 275 | if (F.hasLocalLinkage()) |
| 276 | if (!AnalyzeUsesOfPointer(&F)) { |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 277 | // Remember that we are tracking this global. |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 278 | NonAddressTakenGlobals.insert(&F); |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 279 | TrackedFunctions.insert(&F); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 280 | Handles.emplace_front(*this, &F); |
| 281 | Handles.front().I = Handles.begin(); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 282 | ++NumNonAddrTakenFunctions; |
| 283 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 284 | |
Matthias Braun | b30f2f51 | 2016-01-30 01:24:31 +0000 | [diff] [blame] | 285 | SmallPtrSet<Function *, 16> Readers, Writers; |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 286 | for (GlobalVariable &GV : M.globals()) |
| 287 | if (GV.hasLocalLinkage()) { |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 288 | if (!AnalyzeUsesOfPointer(&GV, &Readers, |
| 289 | GV.isConstant() ? nullptr : &Writers)) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 290 | // Remember that we are tracking this global, and the mod/ref fns |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 291 | NonAddressTakenGlobals.insert(&GV); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 292 | Handles.emplace_front(*this, &GV); |
| 293 | Handles.front().I = Handles.begin(); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 294 | |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 295 | for (Function *Reader : Readers) { |
| 296 | if (TrackedFunctions.insert(Reader).second) { |
| 297 | Handles.emplace_front(*this, Reader); |
| 298 | Handles.front().I = Handles.begin(); |
| 299 | } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 300 | FunctionInfos[Reader].addModRefInfoForGlobal(GV, MRI_Ref); |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 301 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 302 | |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 303 | if (!GV.isConstant()) // No need to keep track of writers to constants |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 304 | for (Function *Writer : Writers) { |
| 305 | if (TrackedFunctions.insert(Writer).second) { |
| 306 | Handles.emplace_front(*this, Writer); |
| 307 | Handles.front().I = Handles.begin(); |
| 308 | } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 309 | FunctionInfos[Writer].addModRefInfoForGlobal(GV, MRI_Mod); |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 310 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 311 | ++NumNonAddrTakenGlobalVars; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 312 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 313 | // If this global holds a pointer type, see if it is an indirect global. |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 314 | if (GV.getValueType()->isPointerTy() && |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 315 | AnalyzeIndirectGlobalMemory(&GV)) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 316 | ++NumIndirectGlobalVars; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 317 | } |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 318 | Readers.clear(); |
| 319 | Writers.clear(); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 323 | /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer. |
| 324 | /// If this is used by anything complex (i.e., the address escapes), return |
| 325 | /// true. Also, while we are at it, keep track of those functions that read and |
| 326 | /// write to the value. |
| 327 | /// |
| 328 | /// If OkayStoreDest is non-null, stores into this global are allowed. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 329 | bool GlobalsAAResult::AnalyzeUsesOfPointer(Value *V, |
| 330 | SmallPtrSetImpl<Function *> *Readers, |
| 331 | SmallPtrSetImpl<Function *> *Writers, |
| 332 | GlobalValue *OkayStoreDest) { |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 333 | if (!V->getType()->isPointerTy()) |
| 334 | return true; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 335 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 336 | for (Use &U : V->uses()) { |
| 337 | User *I = U.getUser(); |
| 338 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 339 | if (Readers) |
| 340 | Readers->insert(LI->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 341 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 342 | if (V == SI->getOperand(1)) { |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 343 | if (Writers) |
| 344 | Writers->insert(SI->getParent()->getParent()); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 345 | } else if (SI->getOperand(1) != OkayStoreDest) { |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 346 | return true; // Storing the pointer |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 347 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 348 | } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) { |
| 349 | if (AnalyzeUsesOfPointer(I, Readers, Writers)) |
Victor Hernandez | 537d8d9 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 350 | return true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 351 | } else if (Operator::getOpcode(I) == Instruction::BitCast) { |
| 352 | if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest)) |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 353 | return true; |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 354 | } else if (auto CS = CallSite(I)) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 355 | // Make sure that this is just the function being called, not that it is |
| 356 | // passing into the function. |
David Majnemer | 2bc2538 | 2015-12-23 09:58:46 +0000 | [diff] [blame] | 357 | if (CS.isDataOperand(&U)) { |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 358 | // Detect calls to free. |
David Majnemer | 2bc2538 | 2015-12-23 09:58:46 +0000 | [diff] [blame] | 359 | if (CS.isArgOperand(&U) && isFreeCall(I, &TLI)) { |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 360 | if (Writers) |
| 361 | Writers->insert(CS->getParent()->getParent()); |
| 362 | } else { |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 363 | return true; // Argument of an unknown call. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 364 | } |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 365 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 366 | } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 367 | if (!isa<ConstantPointerNull>(ICI->getOperand(1))) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 368 | return true; // Allow comparison against null. |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 369 | } else { |
| 370 | return true; |
| 371 | } |
Gabor Greif | 070b9a2 | 2010-07-09 15:53:42 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 374 | return false; |
| 375 | } |
| 376 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 377 | /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable |
| 378 | /// which holds a pointer type. See if the global always points to non-aliased |
| 379 | /// heap memory: that is, all initializers of the globals are allocations, and |
| 380 | /// those allocations have no use other than initialization of the global. |
| 381 | /// Further, all loads out of GV must directly use the memory, not store the |
| 382 | /// pointer somewhere. If this is true, we consider the memory pointed to by |
| 383 | /// GV to be owned by GV and can disambiguate other pointers from it. |
James Molloy | c67dec6 | 2015-10-28 10:41:29 +0000 | [diff] [blame] | 384 | bool GlobalsAAResult::AnalyzeIndirectGlobalMemory(GlobalVariable *GV) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 385 | // Keep track of values related to the allocation of the memory, f.e. the |
| 386 | // value produced by the malloc call and any casts. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 387 | std::vector<Value *> AllocRelatedValues; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 388 | |
James Molloy | c67dec6 | 2015-10-28 10:41:29 +0000 | [diff] [blame] | 389 | // If the initializer is a valid pointer, bail. |
| 390 | if (Constant *C = GV->getInitializer()) |
| 391 | if (!C->isNullValue()) |
| 392 | return false; |
| 393 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 394 | // Walk the user list of the global. If we find anything other than a direct |
| 395 | // load or store, bail out. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 396 | for (User *U : GV->users()) { |
Gabor Greif | aa389f5 | 2010-07-09 16:22:36 +0000 | [diff] [blame] | 397 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 398 | // The pointer loaded from the global can only be used in simple ways: |
| 399 | // we allow addressing of it and loading storing to it. We do *not* allow |
| 400 | // storing the loaded pointer somewhere else or passing to a function. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 401 | if (AnalyzeUsesOfPointer(LI)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 402 | return false; // Loaded pointer escapes. |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 403 | // TODO: Could try some IP mod/ref of the loaded pointer. |
Gabor Greif | aa389f5 | 2010-07-09 16:22:36 +0000 | [diff] [blame] | 404 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 405 | // Storing the global itself. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 406 | if (SI->getOperand(0) == GV) |
| 407 | return false; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 408 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 409 | // If storing the null pointer, ignore it. |
| 410 | if (isa<ConstantPointerNull>(SI->getOperand(0))) |
| 411 | continue; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 412 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 413 | // Check the value being stored. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 414 | Value *Ptr = GetUnderlyingObject(SI->getOperand(0), |
| 415 | GV->getParent()->getDataLayout()); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 416 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 417 | if (!isAllocLikeFn(Ptr, &TLI)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 418 | return false; // Too hard to analyze. |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 419 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 420 | // Analyze all uses of the allocation. If any of them are used in a |
| 421 | // non-simple way (e.g. stored to another global) bail out. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 422 | if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr, |
| 423 | GV)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 424 | return false; // Loaded pointer escapes. |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 425 | |
| 426 | // Remember that this allocation is related to the indirect global. |
| 427 | AllocRelatedValues.push_back(Ptr); |
| 428 | } else { |
| 429 | // Something complex, bail out. |
| 430 | return false; |
| 431 | } |
| 432 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 433 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 434 | // Okay, this is an indirect global. Remember all of the allocations for |
| 435 | // this global in AllocsForIndirectGlobals. |
| 436 | while (!AllocRelatedValues.empty()) { |
| 437 | AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV; |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 438 | Handles.emplace_front(*this, AllocRelatedValues.back()); |
| 439 | Handles.front().I = Handles.begin(); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 440 | AllocRelatedValues.pop_back(); |
| 441 | } |
| 442 | IndirectGlobals.insert(GV); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 443 | Handles.emplace_front(*this, GV); |
| 444 | Handles.front().I = Handles.begin(); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 445 | return true; |
| 446 | } |
| 447 | |
James Molloy | eb46641 | 2015-09-25 15:39:29 +0000 | [diff] [blame] | 448 | void GlobalsAAResult::CollectSCCMembership(CallGraph &CG) { |
| 449 | // We do a bottom-up SCC traversal of the call graph. In other words, we |
| 450 | // visit all callees before callers (leaf-first). |
| 451 | unsigned SCCID = 0; |
| 452 | for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) { |
| 453 | const std::vector<CallGraphNode *> &SCC = *I; |
| 454 | assert(!SCC.empty() && "SCC with no functions?"); |
| 455 | |
| 456 | for (auto *CGN : SCC) |
| 457 | if (Function *F = CGN->getFunction()) |
| 458 | FunctionToSCCMap[F] = SCCID; |
| 459 | ++SCCID; |
| 460 | } |
| 461 | } |
| 462 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 463 | /// AnalyzeCallGraph - At this point, we know the functions where globals are |
| 464 | /// immediately stored to and read from. Propagate this information up the call |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 465 | /// graph to all callers and compute the mod/ref info for all memory for each |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 466 | /// function. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 467 | void GlobalsAAResult::AnalyzeCallGraph(CallGraph &CG, Module &M) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 468 | // We do a bottom-up SCC traversal of the call graph. In other words, we |
| 469 | // visit all callees before callers (leaf-first). |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 470 | for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) { |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 471 | const std::vector<CallGraphNode *> &SCC = *I; |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 472 | assert(!SCC.empty() && "SCC with no functions?"); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 473 | |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 474 | if (!SCC[0]->getFunction() || !SCC[0]->getFunction()->isDefinitionExact()) { |
| 475 | // Calls externally or not exact - can't say anything useful. Remove any |
| 476 | // existing function records (may have been created when scanning |
| 477 | // globals). |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 478 | for (auto *Node : SCC) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 479 | FunctionInfos.erase(Node->getFunction()); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 480 | continue; |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 483 | FunctionInfo &FI = FunctionInfos[SCC[0]->getFunction()]; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 484 | bool KnowNothing = false; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 485 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 486 | // Collect the mod/ref properties due to called functions. We only compute |
| 487 | // one mod-ref set. |
| 488 | for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) { |
| 489 | Function *F = SCC[i]->getFunction(); |
| 490 | if (!F) { |
| 491 | KnowNothing = true; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 492 | break; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 493 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 494 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 495 | if (F->isDeclaration()) { |
| 496 | // Try to get mod/ref behaviour from function attributes. |
Amaury Sechet | 457cc4d | 2016-01-06 13:23:52 +0000 | [diff] [blame] | 497 | if (F->doesNotAccessMemory()) { |
Duncan Sands | 0eca057 | 2008-09-03 15:31:24 +0000 | [diff] [blame] | 498 | // Can't do better than that! |
| 499 | } else if (F->onlyReadsMemory()) { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 500 | FI.addModRefInfo(MRI_Ref); |
Tom Stellard | 1b5cf62 | 2016-07-14 15:50:27 +0000 | [diff] [blame] | 501 | if (!F->isIntrinsic() && !F->onlyAccessesArgMemory()) |
Duncan Sands | e30b36f | 2008-09-11 15:43:12 +0000 | [diff] [blame] | 502 | // This function might call back into the module and read a global - |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 503 | // consider every global as possibly being read by this function. |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 504 | FI.setMayReadAnyGlobal(); |
Duncan Sands | 0eca057 | 2008-09-03 15:31:24 +0000 | [diff] [blame] | 505 | } else { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 506 | FI.addModRefInfo(MRI_ModRef); |
Duncan Sands | d4133ac | 2008-09-11 19:35:55 +0000 | [diff] [blame] | 507 | // Can't say anything useful unless it's an intrinsic - they don't |
| 508 | // read or write global variables of the kind considered here. |
| 509 | KnowNothing = !F->isIntrinsic(); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 510 | } |
| 511 | continue; |
| 512 | } |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 513 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 514 | for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end(); |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 515 | CI != E && !KnowNothing; ++CI) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 516 | if (Function *Callee = CI->second->getFunction()) { |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 517 | if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) { |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 518 | // Propagate function effect up. |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 519 | FI.addFunctionInfo(*CalleeFI); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 520 | } else { |
| 521 | // Can't say anything about it. However, if it is inside our SCC, |
| 522 | // then nothing needs to be done. |
| 523 | CallGraphNode *CalleeNode = CG[Callee]; |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 524 | if (!is_contained(SCC, CalleeNode)) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 525 | KnowNothing = true; |
| 526 | } |
| 527 | } else { |
| 528 | KnowNothing = true; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | // If we can't say anything useful about this SCC, remove all SCC functions |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 533 | // from the FunctionInfos map. |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 534 | if (KnowNothing) { |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 535 | for (auto *Node : SCC) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 536 | FunctionInfos.erase(Node->getFunction()); |
Duncan Sands | e74d750 | 2008-09-03 16:10:55 +0000 | [diff] [blame] | 537 | continue; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // Scan the function bodies for explicit loads or stores. |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 541 | for (auto *Node : SCC) { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 542 | if (FI.getModRefInfo() == MRI_ModRef) |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 543 | break; // The mod/ref lattice saturates here. |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 544 | for (Instruction &I : instructions(Node->getFunction())) { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 545 | if (FI.getModRefInfo() == MRI_ModRef) |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 546 | break; // The mod/ref lattice saturates here. |
| 547 | |
| 548 | // We handle calls specially because the graph-relevant aspects are |
| 549 | // handled above. |
| 550 | if (auto CS = CallSite(&I)) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 551 | if (isAllocationFn(&I, &TLI) || isFreeCall(&I, &TLI)) { |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 552 | // FIXME: It is completely unclear why this is necessary and not |
| 553 | // handled by the above graph code. |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 554 | FI.addModRefInfo(MRI_ModRef); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 555 | } else if (Function *Callee = CS.getCalledFunction()) { |
| 556 | // The callgraph doesn't include intrinsic calls. |
| 557 | if (Callee->isIntrinsic()) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 558 | FunctionModRefBehavior Behaviour = |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 559 | AAResultBase::getModRefBehavior(Callee); |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 560 | FI.addModRefInfo(ModRefInfo(Behaviour & MRI_ModRef)); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | continue; |
Duncan Sands | 9ddb314 | 2008-09-13 12:45:50 +0000 | [diff] [blame] | 564 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 565 | |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 566 | // All non-call instructions we use the primary predicates for whether |
| 567 | // thay read or write memory. |
| 568 | if (I.mayReadFromMemory()) |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 569 | FI.addModRefInfo(MRI_Ref); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 570 | if (I.mayWriteToMemory()) |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 571 | FI.addModRefInfo(MRI_Mod); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 575 | if ((FI.getModRefInfo() & MRI_Mod) == 0) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 576 | ++NumReadMemFunctions; |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 577 | if (FI.getModRefInfo() == MRI_NoModRef) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 578 | ++NumNoMemFunctions; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 579 | |
| 580 | // Finally, now that we know the full effect on this SCC, clone the |
| 581 | // information to each function in the SCC. |
James Molloy | 17379c4 | 2015-10-19 08:54:59 +0000 | [diff] [blame] | 582 | // FI is a reference into FunctionInfos, so copy it now so that it doesn't |
| 583 | // get invalidated if DenseMap decides to re-hash. |
| 584 | FunctionInfo CachedFI = FI; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 585 | for (unsigned i = 1, e = SCC.size(); i != e; ++i) |
James Molloy | 17379c4 | 2015-10-19 08:54:59 +0000 | [diff] [blame] | 586 | FunctionInfos[SCC[i]->getFunction()] = CachedFI; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 587 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 588 | } |
| 589 | |
James Molloy | 5b2a732 | 2015-10-22 13:44:26 +0000 | [diff] [blame] | 590 | // GV is a non-escaping global. V is a pointer address that has been loaded from. |
| 591 | // If we can prove that V must escape, we can conclude that a load from V cannot |
| 592 | // alias GV. |
| 593 | static bool isNonEscapingGlobalNoAliasWithLoad(const GlobalValue *GV, |
| 594 | const Value *V, |
| 595 | int &Depth, |
| 596 | const DataLayout &DL) { |
| 597 | SmallPtrSet<const Value *, 8> Visited; |
| 598 | SmallVector<const Value *, 8> Inputs; |
| 599 | Visited.insert(V); |
| 600 | Inputs.push_back(V); |
| 601 | do { |
| 602 | const Value *Input = Inputs.pop_back_val(); |
| 603 | |
| 604 | if (isa<GlobalValue>(Input) || isa<Argument>(Input) || isa<CallInst>(Input) || |
| 605 | isa<InvokeInst>(Input)) |
| 606 | // Arguments to functions or returns from functions are inherently |
| 607 | // escaping, so we can immediately classify those as not aliasing any |
| 608 | // non-addr-taken globals. |
| 609 | // |
| 610 | // (Transitive) loads from a global are also safe - if this aliased |
| 611 | // another global, its address would escape, so no alias. |
| 612 | continue; |
| 613 | |
| 614 | // Recurse through a limited number of selects, loads and PHIs. This is an |
| 615 | // arbitrary depth of 4, lower numbers could be used to fix compile time |
| 616 | // issues if needed, but this is generally expected to be only be important |
| 617 | // for small depths. |
| 618 | if (++Depth > 4) |
| 619 | return false; |
| 620 | |
| 621 | if (auto *LI = dyn_cast<LoadInst>(Input)) { |
| 622 | Inputs.push_back(GetUnderlyingObject(LI->getPointerOperand(), DL)); |
| 623 | continue; |
| 624 | } |
| 625 | if (auto *SI = dyn_cast<SelectInst>(Input)) { |
| 626 | const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL); |
| 627 | const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL); |
| 628 | if (Visited.insert(LHS).second) |
| 629 | Inputs.push_back(LHS); |
| 630 | if (Visited.insert(RHS).second) |
| 631 | Inputs.push_back(RHS); |
| 632 | continue; |
| 633 | } |
| 634 | if (auto *PN = dyn_cast<PHINode>(Input)) { |
| 635 | for (const Value *Op : PN->incoming_values()) { |
| 636 | Op = GetUnderlyingObject(Op, DL); |
| 637 | if (Visited.insert(Op).second) |
| 638 | Inputs.push_back(Op); |
| 639 | } |
| 640 | continue; |
| 641 | } |
| 642 | |
| 643 | return false; |
| 644 | } while (!Inputs.empty()); |
| 645 | |
| 646 | // All inputs were known to be no-alias. |
| 647 | return true; |
| 648 | } |
| 649 | |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 650 | // There are particular cases where we can conclude no-alias between |
| 651 | // a non-addr-taken global and some other underlying object. Specifically, |
| 652 | // a non-addr-taken global is known to not be escaped from any function. It is |
| 653 | // also incorrect for a transformation to introduce an escape of a global in |
| 654 | // a way that is observable when it was not there previously. One function |
| 655 | // being transformed to introduce an escape which could possibly be observed |
| 656 | // (via loading from a global or the return value for example) within another |
| 657 | // function is never safe. If the observation is made through non-atomic |
| 658 | // operations on different threads, it is a data-race and UB. If the |
| 659 | // observation is well defined, by being observed the transformation would have |
| 660 | // changed program behavior by introducing the observed escape, making it an |
| 661 | // invalid transform. |
| 662 | // |
| 663 | // This property does require that transformations which *temporarily* escape |
| 664 | // a global that was not previously escaped, prior to restoring it, cannot rely |
| 665 | // on the results of GMR::alias. This seems a reasonable restriction, although |
| 666 | // currently there is no way to enforce it. There is also no realistic |
| 667 | // optimization pass that would make this mistake. The closest example is |
| 668 | // a transformation pass which does reg2mem of SSA values but stores them into |
| 669 | // global variables temporarily before restoring the global variable's value. |
| 670 | // This could be useful to expose "benign" races for example. However, it seems |
| 671 | // reasonable to require that a pass which introduces escapes of global |
| 672 | // variables in this way to either not trust AA results while the escape is |
| 673 | // active, or to be forced to operate as a module pass that cannot co-exist |
| 674 | // with an alias analysis such as GMR. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 675 | bool GlobalsAAResult::isNonEscapingGlobalNoAlias(const GlobalValue *GV, |
| 676 | const Value *V) { |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 677 | // In order to know that the underlying object cannot alias the |
| 678 | // non-addr-taken global, we must know that it would have to be an escape. |
| 679 | // Thus if the underlying object is a function argument, a load from |
| 680 | // a global, or the return of a function, it cannot alias. We can also |
| 681 | // recurse through PHI nodes and select nodes provided all of their inputs |
| 682 | // resolve to one of these known-escaping roots. |
| 683 | SmallPtrSet<const Value *, 8> Visited; |
| 684 | SmallVector<const Value *, 8> Inputs; |
| 685 | Visited.insert(V); |
| 686 | Inputs.push_back(V); |
| 687 | int Depth = 0; |
| 688 | do { |
| 689 | const Value *Input = Inputs.pop_back_val(); |
| 690 | |
| 691 | if (auto *InputGV = dyn_cast<GlobalValue>(Input)) { |
| 692 | // If one input is the very global we're querying against, then we can't |
| 693 | // conclude no-alias. |
| 694 | if (InputGV == GV) |
| 695 | return false; |
| 696 | |
Michael Kuperstein | 07f31d9 | 2015-08-11 08:06:44 +0000 | [diff] [blame] | 697 | // Distinct GlobalVariables never alias, unless overriden or zero-sized. |
| 698 | // FIXME: The condition can be refined, but be conservative for now. |
| 699 | auto *GVar = dyn_cast<GlobalVariable>(GV); |
| 700 | auto *InputGVar = dyn_cast<GlobalVariable>(InputGV); |
| 701 | if (GVar && InputGVar && |
| 702 | !GVar->isDeclaration() && !InputGVar->isDeclaration() && |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 703 | !GVar->isInterposable() && !InputGVar->isInterposable()) { |
Michael Kuperstein | 07f31d9 | 2015-08-11 08:06:44 +0000 | [diff] [blame] | 704 | Type *GVType = GVar->getInitializer()->getType(); |
| 705 | Type *InputGVType = InputGVar->getInitializer()->getType(); |
| 706 | if (GVType->isSized() && InputGVType->isSized() && |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 707 | (DL.getTypeAllocSize(GVType) > 0) && |
| 708 | (DL.getTypeAllocSize(InputGVType) > 0)) |
Michael Kuperstein | 07f31d9 | 2015-08-11 08:06:44 +0000 | [diff] [blame] | 709 | continue; |
| 710 | } |
| 711 | |
| 712 | // Conservatively return false, even though we could be smarter |
| 713 | // (e.g. look through GlobalAliases). |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 714 | return false; |
| 715 | } |
| 716 | |
| 717 | if (isa<Argument>(Input) || isa<CallInst>(Input) || |
| 718 | isa<InvokeInst>(Input)) { |
| 719 | // Arguments to functions or returns from functions are inherently |
| 720 | // escaping, so we can immediately classify those as not aliasing any |
| 721 | // non-addr-taken globals. |
| 722 | continue; |
| 723 | } |
James Molloy | 5b2a732 | 2015-10-22 13:44:26 +0000 | [diff] [blame] | 724 | |
| 725 | // Recurse through a limited number of selects, loads and PHIs. This is an |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 726 | // arbitrary depth of 4, lower numbers could be used to fix compile time |
| 727 | // issues if needed, but this is generally expected to be only be important |
| 728 | // for small depths. |
| 729 | if (++Depth > 4) |
| 730 | return false; |
James Molloy | 5b2a732 | 2015-10-22 13:44:26 +0000 | [diff] [blame] | 731 | |
| 732 | if (auto *LI = dyn_cast<LoadInst>(Input)) { |
| 733 | // A pointer loaded from a global would have been captured, and we know |
| 734 | // that the global is non-escaping, so no alias. |
| 735 | const Value *Ptr = GetUnderlyingObject(LI->getPointerOperand(), DL); |
| 736 | if (isNonEscapingGlobalNoAliasWithLoad(GV, Ptr, Depth, DL)) |
| 737 | // The load does not alias with GV. |
| 738 | continue; |
| 739 | // Otherwise, a load could come from anywhere, so bail. |
| 740 | return false; |
| 741 | } |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 742 | if (auto *SI = dyn_cast<SelectInst>(Input)) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 743 | const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL); |
| 744 | const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL); |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 745 | if (Visited.insert(LHS).second) |
| 746 | Inputs.push_back(LHS); |
| 747 | if (Visited.insert(RHS).second) |
| 748 | Inputs.push_back(RHS); |
| 749 | continue; |
| 750 | } |
| 751 | if (auto *PN = dyn_cast<PHINode>(Input)) { |
| 752 | for (const Value *Op : PN->incoming_values()) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 753 | Op = GetUnderlyingObject(Op, DL); |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 754 | if (Visited.insert(Op).second) |
| 755 | Inputs.push_back(Op); |
| 756 | } |
| 757 | continue; |
| 758 | } |
| 759 | |
Michael Kuperstein | 07f31d9 | 2015-08-11 08:06:44 +0000 | [diff] [blame] | 760 | // FIXME: It would be good to handle other obvious no-alias cases here, but |
| 761 | // it isn't clear how to do so reasonbly without building a small version |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 762 | // of BasicAA into this code. We could recurse into AAResultBase::alias |
Michael Kuperstein | 07f31d9 | 2015-08-11 08:06:44 +0000 | [diff] [blame] | 763 | // here but that seems likely to go poorly as we're inside the |
| 764 | // implementation of such a query. Until then, just conservatievly retun |
| 765 | // false. |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 766 | return false; |
| 767 | } while (!Inputs.empty()); |
| 768 | |
| 769 | // If all the inputs to V were definitively no-alias, then V is no-alias. |
| 770 | return true; |
| 771 | } |
| 772 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 773 | /// alias - If one of the pointers is to a global that we are tracking, and the |
| 774 | /// other is some random pointer, we know there cannot be an alias, because the |
| 775 | /// address of the global isn't taken. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 776 | AliasResult GlobalsAAResult::alias(const MemoryLocation &LocA, |
| 777 | const MemoryLocation &LocB) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 778 | // Get the base object these pointers point to. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 779 | const Value *UV1 = GetUnderlyingObject(LocA.Ptr, DL); |
| 780 | const Value *UV2 = GetUnderlyingObject(LocB.Ptr, DL); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 781 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 782 | // If either of the underlying values is a global, they may be non-addr-taken |
| 783 | // globals, which we can answer queries about. |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 784 | const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1); |
| 785 | const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 786 | if (GV1 || GV2) { |
| 787 | // If the global's address is taken, pretend we don't know it's a pointer to |
| 788 | // the global. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 789 | if (GV1 && !NonAddressTakenGlobals.count(GV1)) |
| 790 | GV1 = nullptr; |
| 791 | if (GV2 && !NonAddressTakenGlobals.count(GV2)) |
| 792 | GV2 = nullptr; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 793 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 794 | // If the two pointers are derived from two different non-addr-taken |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 795 | // globals we know these can't alias. |
| 796 | if (GV1 && GV2 && GV1 != GV2) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 797 | return NoAlias; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 798 | |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 799 | // If one is and the other isn't, it isn't strictly safe but we can fake |
| 800 | // this result if necessary for performance. This does not appear to be |
| 801 | // a common problem in practice. |
| 802 | if (EnableUnsafeGlobalsModRefAliasResults) |
| 803 | if ((GV1 || GV2) && GV1 != GV2) |
| 804 | return NoAlias; |
| 805 | |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 806 | // Check for a special case where a non-escaping global can be used to |
| 807 | // conclude no-alias. |
Chandler Carruth | 99ad7bb | 2015-07-28 11:11:11 +0000 | [diff] [blame] | 808 | if ((GV1 || GV2) && GV1 != GV2) { |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 809 | const GlobalValue *GV = GV1 ? GV1 : GV2; |
Chandler Carruth | 99ad7bb | 2015-07-28 11:11:11 +0000 | [diff] [blame] | 810 | const Value *UV = GV1 ? UV2 : UV1; |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 811 | if (isNonEscapingGlobalNoAlias(GV, UV)) |
Chandler Carruth | 99ad7bb | 2015-07-28 11:11:11 +0000 | [diff] [blame] | 812 | return NoAlias; |
Chandler Carruth | 99ad7bb | 2015-07-28 11:11:11 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 815 | // Otherwise if they are both derived from the same addr-taken global, we |
| 816 | // can't know the two accesses don't overlap. |
| 817 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 818 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 819 | // These pointers may be based on the memory owned by an indirect global. If |
| 820 | // so, we may be able to handle this. First check to see if the base pointer |
| 821 | // is a direct load from an indirect global. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 822 | GV1 = GV2 = nullptr; |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 823 | if (const LoadInst *LI = dyn_cast<LoadInst>(UV1)) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 824 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) |
| 825 | if (IndirectGlobals.count(GV)) |
| 826 | GV1 = GV; |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 827 | if (const LoadInst *LI = dyn_cast<LoadInst>(UV2)) |
| 828 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 829 | if (IndirectGlobals.count(GV)) |
| 830 | GV2 = GV; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 831 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 832 | // These pointers may also be from an allocation for the indirect global. If |
| 833 | // so, also handle them. |
Chandler Carruth | 56e2c62 | 2015-07-22 11:43:24 +0000 | [diff] [blame] | 834 | if (!GV1) |
| 835 | GV1 = AllocsForIndirectGlobals.lookup(UV1); |
| 836 | if (!GV2) |
| 837 | GV2 = AllocsForIndirectGlobals.lookup(UV2); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 838 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 839 | // Now that we know whether the two pointers are related to indirect globals, |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 840 | // use this to disambiguate the pointers. If the pointers are based on |
| 841 | // different indirect globals they cannot alias. |
| 842 | if (GV1 && GV2 && GV1 != GV2) |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 843 | return NoAlias; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 844 | |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 845 | // If one is based on an indirect global and the other isn't, it isn't |
| 846 | // strictly safe but we can fake this result if necessary for performance. |
| 847 | // This does not appear to be a common problem in practice. |
| 848 | if (EnableUnsafeGlobalsModRefAliasResults) |
| 849 | if ((GV1 || GV2) && GV1 != GV2) |
| 850 | return NoAlias; |
| 851 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 852 | return AAResultBase::alias(LocA, LocB); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 853 | } |
| 854 | |
James Molloy | eb46641 | 2015-09-25 15:39:29 +0000 | [diff] [blame] | 855 | ModRefInfo GlobalsAAResult::getModRefInfoForArgument(ImmutableCallSite CS, |
| 856 | const GlobalValue *GV) { |
| 857 | if (CS.doesNotAccessMemory()) |
| 858 | return MRI_NoModRef; |
| 859 | ModRefInfo ConservativeResult = CS.onlyReadsMemory() ? MRI_Ref : MRI_ModRef; |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 860 | |
James Molloy | eb46641 | 2015-09-25 15:39:29 +0000 | [diff] [blame] | 861 | // Iterate through all the arguments to the called function. If any argument |
| 862 | // is based on GV, return the conservative result. |
| 863 | for (auto &A : CS.args()) { |
| 864 | SmallVector<Value*, 4> Objects; |
| 865 | GetUnderlyingObjects(A, Objects, DL); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 866 | |
James Molloy | eb46641 | 2015-09-25 15:39:29 +0000 | [diff] [blame] | 867 | // All objects must be identified. |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 868 | if (!all_of(Objects, isIdentifiedObject) && |
Vaivaswatha Nagaraj | 68befd7 | 2016-01-14 08:46:45 +0000 | [diff] [blame] | 869 | // Try ::alias to see if all objects are known not to alias GV. |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 870 | !all_of(Objects, [&](Value *V) { |
Vaivaswatha Nagaraj | 68befd7 | 2016-01-14 08:46:45 +0000 | [diff] [blame] | 871 | return this->alias(MemoryLocation(V), MemoryLocation(GV)) == NoAlias; |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 872 | })) |
James Molloy | eb46641 | 2015-09-25 15:39:29 +0000 | [diff] [blame] | 873 | return ConservativeResult; |
| 874 | |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 875 | if (is_contained(Objects, GV)) |
James Molloy | eb46641 | 2015-09-25 15:39:29 +0000 | [diff] [blame] | 876 | return ConservativeResult; |
| 877 | } |
| 878 | |
| 879 | // We identified all objects in the argument list, and none of them were GV. |
| 880 | return MRI_NoModRef; |
| 881 | } |
| 882 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 883 | ModRefInfo GlobalsAAResult::getModRefInfo(ImmutableCallSite CS, |
| 884 | const MemoryLocation &Loc) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 885 | unsigned Known = MRI_ModRef; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 886 | |
| 887 | // If we are asking for mod/ref info of a direct call with a pointer to a |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 888 | // global we are tracking, return information if we have it. |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 889 | if (const GlobalValue *GV = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 890 | dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL))) |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 891 | if (GV->hasLocalLinkage()) |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 892 | if (const Function *F = CS.getCalledFunction()) |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 893 | if (NonAddressTakenGlobals.count(GV)) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 894 | if (const FunctionInfo *FI = getFunctionInfo(F)) |
James Molloy | eb46641 | 2015-09-25 15:39:29 +0000 | [diff] [blame] | 895 | Known = FI->getModRefInfoForGlobal(*GV) | |
| 896 | getModRefInfoForArgument(CS, GV); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 897 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 898 | if (Known == MRI_NoModRef) |
| 899 | return MRI_NoModRef; // No need to query other mod/ref analyses |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 900 | return ModRefInfo(Known & AAResultBase::getModRefInfo(CS, Loc)); |
| 901 | } |
| 902 | |
| 903 | GlobalsAAResult::GlobalsAAResult(const DataLayout &DL, |
| 904 | const TargetLibraryInfo &TLI) |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 905 | : AAResultBase(), DL(DL), TLI(TLI) {} |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 906 | |
| 907 | GlobalsAAResult::GlobalsAAResult(GlobalsAAResult &&Arg) |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 908 | : AAResultBase(std::move(Arg)), DL(Arg.DL), TLI(Arg.TLI), |
NAKAMURA Takumi | 1a296ec | 2015-09-10 07:16:42 +0000 | [diff] [blame] | 909 | NonAddressTakenGlobals(std::move(Arg.NonAddressTakenGlobals)), |
| 910 | IndirectGlobals(std::move(Arg.IndirectGlobals)), |
| 911 | AllocsForIndirectGlobals(std::move(Arg.AllocsForIndirectGlobals)), |
| 912 | FunctionInfos(std::move(Arg.FunctionInfos)), |
NAKAMURA Takumi | 98800d9 | 2015-09-14 06:16:44 +0000 | [diff] [blame] | 913 | Handles(std::move(Arg.Handles)) { |
| 914 | // Update the parent for each DeletionCallbackHandle. |
| 915 | for (auto &H : Handles) { |
| 916 | assert(H.GAR == &Arg); |
| 917 | H.GAR = this; |
| 918 | } |
| 919 | } |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 920 | |
Chandler Carruth | 45a9c20 | 2016-03-11 09:15:11 +0000 | [diff] [blame] | 921 | GlobalsAAResult::~GlobalsAAResult() {} |
| 922 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 923 | /*static*/ GlobalsAAResult |
| 924 | GlobalsAAResult::analyzeModule(Module &M, const TargetLibraryInfo &TLI, |
| 925 | CallGraph &CG) { |
| 926 | GlobalsAAResult Result(M.getDataLayout(), TLI); |
| 927 | |
James Molloy | eb46641 | 2015-09-25 15:39:29 +0000 | [diff] [blame] | 928 | // Discover which functions aren't recursive, to feed into AnalyzeGlobals. |
| 929 | Result.CollectSCCMembership(CG); |
| 930 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 931 | // Find non-addr taken globals. |
| 932 | Result.AnalyzeGlobals(M); |
| 933 | |
| 934 | // Propagate on CG. |
| 935 | Result.AnalyzeCallGraph(CG, M); |
| 936 | |
| 937 | return Result; |
| 938 | } |
| 939 | |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 940 | char GlobalsAA::PassID; |
| 941 | |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 942 | GlobalsAAResult GlobalsAA::run(Module &M, ModuleAnalysisManager &AM) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 943 | return GlobalsAAResult::analyzeModule(M, |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 944 | AM.getResult<TargetLibraryAnalysis>(M), |
| 945 | AM.getResult<CallGraphAnalysis>(M)); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 948 | char GlobalsAAWrapperPass::ID = 0; |
| 949 | INITIALIZE_PASS_BEGIN(GlobalsAAWrapperPass, "globals-aa", |
| 950 | "Globals Alias Analysis", false, true) |
| 951 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
| 952 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 953 | INITIALIZE_PASS_END(GlobalsAAWrapperPass, "globals-aa", |
| 954 | "Globals Alias Analysis", false, true) |
| 955 | |
| 956 | ModulePass *llvm::createGlobalsAAWrapperPass() { |
| 957 | return new GlobalsAAWrapperPass(); |
| 958 | } |
| 959 | |
| 960 | GlobalsAAWrapperPass::GlobalsAAWrapperPass() : ModulePass(ID) { |
| 961 | initializeGlobalsAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 962 | } |
| 963 | |
| 964 | bool GlobalsAAWrapperPass::runOnModule(Module &M) { |
| 965 | Result.reset(new GlobalsAAResult(GlobalsAAResult::analyzeModule( |
| 966 | M, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(), |
| 967 | getAnalysis<CallGraphWrapperPass>().getCallGraph()))); |
| 968 | return false; |
| 969 | } |
| 970 | |
| 971 | bool GlobalsAAWrapperPass::doFinalization(Module &M) { |
| 972 | Result.reset(); |
| 973 | return false; |
| 974 | } |
| 975 | |
| 976 | void GlobalsAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 977 | AU.setPreservesAll(); |
| 978 | AU.addRequired<CallGraphWrapperPass>(); |
| 979 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 980 | } |