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