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