blob: b28abcadca4abd50f9108bca44ffafbeef8c5436 [file] [log] [blame]
Chris Lattner3b04a8a2004-06-28 06:33:13 +00001//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner3b04a8a2004-06-28 06:33:13 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattner3b04a8a2004-06-28 06:33:13 +00008//===----------------------------------------------------------------------===//
9//
10// This simple pass provides alias and mod/ref information for global values
Chris Lattnerfe98f272004-07-27 06:40:37 +000011// 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 Lattner3b04a8a2004-06-28 06:33:13 +000014//
15//===----------------------------------------------------------------------===//
16
Chandler Carruth0d68ddf2015-08-14 03:48:20 +000017#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/SCCIterator.h"
Chandler Carruth6accb772015-07-22 11:47:54 +000019#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ADT/Statistic.h"
Victor Hernandezf006b182009-10-27 20:05:49 +000021#include "llvm/Analysis/MemoryBuiltins.h"
Chandler Carruth91468332015-09-09 17:55:00 +000022#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohman5034dd32010-12-15 20:02:24 +000023#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/DerivedTypes.h"
Chandler Carruth876ac602014-03-04 10:30:26 +000025#include "llvm/IR/InstIterator.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/Instructions.h"
27#include "llvm/IR/IntrinsicInst.h"
28#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000030#include "llvm/Support/CommandLine.h"
Chris Lattner3b04a8a2004-06-28 06:33:13 +000031using namespace llvm;
32
Chandler Carruth4da25372014-04-22 02:48:03 +000033#define DEBUG_TYPE "globalsmodref-aa"
34
Chris Lattner3b27d682006-12-19 22:30:33 +000035STATISTIC(NumNonAddrTakenGlobalVars,
36 "Number of global vars without address taken");
37STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
38STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
39STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
40STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
41
Chandler Carruth81f4bf72015-07-17 06:58:24 +000042// 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.
53static cl::opt<bool> EnableUnsafeGlobalsModRefAliasResults(
54 "enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden);
55
Chandler Carruthd9a7d832015-07-22 23:56:31 +000056/// 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 Carruth91468332015-09-09 17:55:00 +000062class GlobalsAAResult::FunctionInfo {
Chandler Carruthcaabac72015-07-23 07:50:52 +000063 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 Song01a1fe52018-07-27 05:38:14 +000068 struct alignas(8) AlignedMap {
Chandler Carruthcaabac72015-07-23 07:50:52 +000069 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 Kramercb58e1e2016-10-20 15:02:18 +000081 static_assert(alignof(AlignedMap) >= (1 << NumLowBitsAvailable),
Chandler Carruthcaabac72015-07-23 07:50:52 +000082 "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 Sbirleaa2d30e92017-12-05 20:12:23 +000087 /// FIXME: This assumes ModRefInfo lattice will remain 4 bits!
Alina Sbirlea9680c052017-12-21 21:41:53 +000088 /// 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 Carruthcaabac72015-07-23 07:50:52 +000091 enum { MayReadAnyGlobal = 4 };
92
93 /// Checks to document the invariants of the bit packing here.
Alina Sbirlea9680c052017-12-21 21:41:53 +000094 static_assert((MayReadAnyGlobal & static_cast<int>(ModRefInfo::MustModRef)) ==
95 0,
Chandler Carruthcaabac72015-07-23 07:50:52 +000096 "ModRef and the MayReadAnyGlobal flag bits overlap.");
Alina Sbirlea9680c052017-12-21 21:41:53 +000097 static_assert(((MayReadAnyGlobal |
98 static_cast<int>(ModRefInfo::MustModRef)) >>
Chandler Carruthcaabac72015-07-23 07:50:52 +000099 AlignedMapPointerTraits::NumLowBitsAvailable) == 0,
100 "Insufficient low bits to store our flag and ModRef info.");
101
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000102public:
Chandler Carruthcaabac72015-07-23 07:50:52 +0000103 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 Lattnerfe98f272004-07-27 06:40:37 +0000132
Alina Sbirlea9680c052017-12-21 21:41:53 +0000133 /// 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 Carruthd9a7d832015-07-22 23:56:31 +0000141 /// Returns the \c ModRefInfo info for this function.
Chandler Carruthcaabac72015-07-23 07:50:52 +0000142 ModRefInfo getModRefInfo() const {
Alina Sbirlea9680c052017-12-21 21:41:53 +0000143 return globalClearMayReadAnyGlobal(Info.getInt());
Chandler Carruthcaabac72015-07-23 07:50:52 +0000144 }
Duncan Sands2bb4a4d2008-09-12 07:29:58 +0000145
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000146 /// Adds new \c ModRefInfo for this function to its state.
Chandler Carruthcaabac72015-07-23 07:50:52 +0000147 void addModRefInfo(ModRefInfo NewMRI) {
Alina Sbirlea9680c052017-12-21 21:41:53 +0000148 Info.setInt(Info.getInt() | static_cast<int>(setMust(NewMRI)));
Chandler Carruthcaabac72015-07-23 07:50:52 +0000149 }
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000150
151 /// Returns whether this function may read any global variable, and we don't
152 /// know which global.
Chandler Carruthcaabac72015-07-23 07:50:52 +0000153 bool mayReadAnyGlobal() const { return Info.getInt() & MayReadAnyGlobal; }
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000154
155 /// Sets this function as potentially reading from any global.
Chandler Carruthcaabac72015-07-23 07:50:52 +0000156 void setMayReadAnyGlobal() { Info.setInt(Info.getInt() | MayReadAnyGlobal); }
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000157
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 Sbirleac94e8962017-12-07 22:41:34 +0000161 ModRefInfo GlobalMRI =
162 mayReadAnyGlobal() ? ModRefInfo::Ref : ModRefInfo::NoModRef;
Chandler Carruthcaabac72015-07-23 07:50:52 +0000163 if (AlignedMap *P = Info.getPointer()) {
164 auto I = P->Map.find(&GV);
165 if (I != P->Map.end())
Alina Sbirlea9c879112017-12-07 00:43:19 +0000166 GlobalMRI = unionModRef(GlobalMRI, I->second);
Chandler Carruthcaabac72015-07-23 07:50:52 +0000167 }
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000168 return GlobalMRI;
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000169 }
170
Chandler Carruth603a9ad2015-07-23 00:12:32 +0000171 /// Add mod/ref info from another function into ours, saturating towards
Alina Sbirleac94e8962017-12-07 22:41:34 +0000172 /// ModRef.
Chandler Carruth603a9ad2015-07-23 00:12:32 +0000173 void addFunctionInfo(const FunctionInfo &FI) {
174 addModRefInfo(FI.getModRefInfo());
175
176 if (FI.mayReadAnyGlobal())
177 setMayReadAnyGlobal();
178
Chandler Carruthcaabac72015-07-23 07:50:52 +0000179 if (AlignedMap *P = FI.Info.getPointer())
180 for (const auto &G : P->Map)
181 addModRefInfoForGlobal(*G.first, G.second);
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000182 }
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000183
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000184 void addModRefInfoForGlobal(const GlobalValue &GV, ModRefInfo NewMRI) {
Chandler Carruthcaabac72015-07-23 07:50:52 +0000185 AlignedMap *P = Info.getPointer();
186 if (!P) {
187 P = new AlignedMap();
188 Info.setPointer(P);
189 }
190 auto &GlobalMRI = P->Map[&GV];
Alina Sbirlea9c879112017-12-07 00:43:19 +0000191 GlobalMRI = unionModRef(GlobalMRI, NewMRI);
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000192 }
193
Chandler Carruth89576ce2015-07-28 06:01:57 +0000194 /// 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 Carruthd9a7d832015-07-22 23:56:31 +0000201private:
Chandler Carruthcaabac72015-07-23 07:50:52 +0000202 /// 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 Carruth2364d1f2015-07-14 08:42:39 +0000208};
209
Chandler Carruth91468332015-09-09 17:55:00 +0000210void GlobalsAAResult::DeletionCallbackHandle::deleted() {
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000211 Value *V = getValPtr();
212 if (auto *F = dyn_cast<Function>(V))
NAKAMURA Takumi8f188e02015-09-14 06:16:44 +0000213 GAR->FunctionInfos.erase(F);
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000214
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000215 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
NAKAMURA Takumi8f188e02015-09-14 06:16:44 +0000216 if (GAR->NonAddressTakenGlobals.erase(GV)) {
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000217 // This global might be an indirect global. If so, remove it and
218 // remove any AllocRelatedValues for it.
NAKAMURA Takumi8f188e02015-09-14 06:16:44 +0000219 if (GAR->IndirectGlobals.erase(GV)) {
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000220 // Remove any entries in AllocsForIndirectGlobals for this global.
NAKAMURA Takumi8f188e02015-09-14 06:16:44 +0000221 for (auto I = GAR->AllocsForIndirectGlobals.begin(),
222 E = GAR->AllocsForIndirectGlobals.end();
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000223 I != E; ++I)
224 if (I->second == GV)
NAKAMURA Takumi8f188e02015-09-14 06:16:44 +0000225 GAR->AllocsForIndirectGlobals.erase(I);
Chandler Carruth048e3fa2015-07-22 11:10:41 +0000226 }
227
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000228 // Scan the function info we have collected and remove this global
229 // from all of them.
NAKAMURA Takumi8f188e02015-09-14 06:16:44 +0000230 for (auto &FIPair : GAR->FunctionInfos)
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000231 FIPair.second.eraseModRefInfoForGlobal(*GV);
Chandler Carruth048e3fa2015-07-22 11:10:41 +0000232 }
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000233 }
234
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000235 // If this is an allocation related to an indirect global, remove it.
NAKAMURA Takumi8f188e02015-09-14 06:16:44 +0000236 GAR->AllocsForIndirectGlobals.erase(V);
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000237
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000238 // And clear out the handle.
239 setValPtr(nullptr);
NAKAMURA Takumi8f188e02015-09-14 06:16:44 +0000240 GAR->Handles.erase(I);
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000241 // This object is now destroyed!
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +0000242}
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000243
Chandler Carruth91468332015-09-09 17:55:00 +0000244FunctionModRefBehavior GlobalsAAResult::getModRefBehavior(const Function *F) {
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000245 FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
246
247 if (FunctionInfo *FI = getFunctionInfo(F)) {
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000248 if (!isModOrRefSet(FI->getModRefInfo()))
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000249 Min = FMRB_DoesNotAccessMemory;
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000250 else if (!isModSet(FI->getModRefInfo()))
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000251 Min = FMRB_OnlyReadsMemory;
252 }
253
Chandler Carruth91468332015-09-09 17:55:00 +0000254 return FunctionModRefBehavior(AAResultBase::getModRefBehavior(F) & Min);
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000255}
256
Chandler Carruth91468332015-09-09 17:55:00 +0000257FunctionModRefBehavior
Chandler Carruth81aa7122019-01-07 05:42:51 +0000258GlobalsAAResult::getModRefBehavior(const CallBase *Call) {
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000259 FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
260
Chandler Carruth81aa7122019-01-07 05:42:51 +0000261 if (!Call->hasOperandBundles())
262 if (const Function *F = Call->getCalledFunction())
Sanjoy Das4e074142016-02-09 02:31:47 +0000263 if (FunctionInfo *FI = getFunctionInfo(F)) {
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000264 if (!isModOrRefSet(FI->getModRefInfo()))
Sanjoy Das4e074142016-02-09 02:31:47 +0000265 Min = FMRB_DoesNotAccessMemory;
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000266 else if (!isModSet(FI->getModRefInfo()))
Sanjoy Das4e074142016-02-09 02:31:47 +0000267 Min = FMRB_OnlyReadsMemory;
268 }
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000269
Chandler Carruth81aa7122019-01-07 05:42:51 +0000270 return FunctionModRefBehavior(AAResultBase::getModRefBehavior(Call) & Min);
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000271}
272
273/// Returns the function info for the function, or null if we don't have
274/// anything useful to say about it.
Chandler Carruth91468332015-09-09 17:55:00 +0000275GlobalsAAResult::FunctionInfo *
276GlobalsAAResult::getFunctionInfo(const Function *F) {
Chandler Carruth0d68ddf2015-08-14 03:48:20 +0000277 auto I = FunctionInfos.find(F);
278 if (I != FunctionInfos.end())
279 return &I->second;
280 return nullptr;
281}
282
Chris Lattnerab383582006-10-01 22:36:45 +0000283/// AnalyzeGlobals - Scan through the users of all of the internal
Duncan Sands9a036b92008-09-03 12:55:42 +0000284/// GlobalValue's in the program. If none of them have their "address taken"
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000285/// (really, their address passed to something nontrivial), record this fact,
286/// and record the functions that they are used directly in.
Chandler Carruth91468332015-09-09 17:55:00 +0000287void GlobalsAAResult::AnalyzeGlobals(Module &M) {
Matthias Braun5e08bd32016-01-30 01:24:31 +0000288 SmallPtrSet<Function *, 32> TrackedFunctions;
Chandler Carruthf31b1ec2015-07-15 08:09:23 +0000289 for (Function &F : M)
Chandler Carruth30b2dba2015-07-22 22:10:05 +0000290 if (F.hasLocalLinkage())
291 if (!AnalyzeUsesOfPointer(&F)) {
Chris Lattnerfe98f272004-07-27 06:40:37 +0000292 // Remember that we are tracking this global.
Chandler Carruthf31b1ec2015-07-15 08:09:23 +0000293 NonAddressTakenGlobals.insert(&F);
Chandler Carruth89576ce2015-07-28 06:01:57 +0000294 TrackedFunctions.insert(&F);
Chandler Carruth0ea47602015-07-22 09:27:58 +0000295 Handles.emplace_front(*this, &F);
296 Handles.front().I = Handles.begin();
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000297 ++NumNonAddrTakenFunctions;
298 }
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000299
Matthias Braun5e08bd32016-01-30 01:24:31 +0000300 SmallPtrSet<Function *, 16> Readers, Writers;
Chandler Carruthf31b1ec2015-07-15 08:09:23 +0000301 for (GlobalVariable &GV : M.globals())
302 if (GV.hasLocalLinkage()) {
Chandler Carruth30b2dba2015-07-22 22:10:05 +0000303 if (!AnalyzeUsesOfPointer(&GV, &Readers,
304 GV.isConstant() ? nullptr : &Writers)) {
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000305 // Remember that we are tracking this global, and the mod/ref fns
Chandler Carruthf31b1ec2015-07-15 08:09:23 +0000306 NonAddressTakenGlobals.insert(&GV);
Chandler Carruth0ea47602015-07-22 09:27:58 +0000307 Handles.emplace_front(*this, &GV);
308 Handles.front().I = Handles.begin();
Duncan Sands9a036b92008-09-03 12:55:42 +0000309
Chandler Carruth89576ce2015-07-28 06:01:57 +0000310 for (Function *Reader : Readers) {
311 if (TrackedFunctions.insert(Reader).second) {
312 Handles.emplace_front(*this, Reader);
313 Handles.front().I = Handles.begin();
314 }
Alina Sbirleac94e8962017-12-07 22:41:34 +0000315 FunctionInfos[Reader].addModRefInfoForGlobal(GV, ModRefInfo::Ref);
Chandler Carruth89576ce2015-07-28 06:01:57 +0000316 }
Chris Lattnerfe98f272004-07-27 06:40:37 +0000317
Chandler Carruthf31b1ec2015-07-15 08:09:23 +0000318 if (!GV.isConstant()) // No need to keep track of writers to constants
Chandler Carruth89576ce2015-07-28 06:01:57 +0000319 for (Function *Writer : Writers) {
320 if (TrackedFunctions.insert(Writer).second) {
321 Handles.emplace_front(*this, Writer);
322 Handles.front().I = Handles.begin();
323 }
Alina Sbirleac94e8962017-12-07 22:41:34 +0000324 FunctionInfos[Writer].addModRefInfoForGlobal(GV, ModRefInfo::Mod);
Chandler Carruth89576ce2015-07-28 06:01:57 +0000325 }
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000326 ++NumNonAddrTakenGlobalVars;
Duncan Sands9a036b92008-09-03 12:55:42 +0000327
Chris Lattnerab383582006-10-01 22:36:45 +0000328 // If this global holds a pointer type, see if it is an indirect global.
Manuel Jacob75e1cfb2016-01-16 20:30:46 +0000329 if (GV.getValueType()->isPointerTy() &&
Chandler Carruthf31b1ec2015-07-15 08:09:23 +0000330 AnalyzeIndirectGlobalMemory(&GV))
Chris Lattnerab383582006-10-01 22:36:45 +0000331 ++NumIndirectGlobalVars;
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000332 }
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000333 Readers.clear();
334 Writers.clear();
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000335 }
336}
337
Chris Lattnerab383582006-10-01 22:36:45 +0000338/// 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 Carruth91468332015-09-09 17:55:00 +0000344bool GlobalsAAResult::AnalyzeUsesOfPointer(Value *V,
345 SmallPtrSetImpl<Function *> *Readers,
346 SmallPtrSetImpl<Function *> *Writers,
347 GlobalValue *OkayStoreDest) {
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000348 if (!V->getType()->isPointerTy())
349 return true;
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000350
Chandler Carruth36b699f2014-03-09 03:16:01 +0000351 for (Use &U : V->uses()) {
352 User *I = U.getUser();
353 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Chandler Carruth30b2dba2015-07-22 22:10:05 +0000354 if (Readers)
355 Readers->insert(LI->getParent()->getParent());
Chandler Carruth36b699f2014-03-09 03:16:01 +0000356 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattnerab383582006-10-01 22:36:45 +0000357 if (V == SI->getOperand(1)) {
Chandler Carruth30b2dba2015-07-22 22:10:05 +0000358 if (Writers)
359 Writers->insert(SI->getParent()->getParent());
Chris Lattnerab383582006-10-01 22:36:45 +0000360 } else if (SI->getOperand(1) != OkayStoreDest) {
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000361 return true; // Storing the pointer
Chris Lattnerab383582006-10-01 22:36:45 +0000362 }
Chandler Carruth36b699f2014-03-09 03:16:01 +0000363 } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {
364 if (AnalyzeUsesOfPointer(I, Readers, Writers))
Victor Hernandez46e83122009-09-18 21:34:51 +0000365 return true;
Chandler Carruth36b699f2014-03-09 03:16:01 +0000366 } else if (Operator::getOpcode(I) == Instruction::BitCast) {
367 if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
Benjamin Kramerdf3ae8e2014-02-10 14:17:30 +0000368 return true;
Chandler Carruth81aa7122019-01-07 05:42:51 +0000369 } else if (auto *Call = dyn_cast<CallBase>(I)) {
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000370 // Make sure that this is just the function being called, not that it is
371 // passing into the function.
Chandler Carruth81aa7122019-01-07 05:42:51 +0000372 if (Call->isDataOperand(&U)) {
Benjamin Kramerdf3ae8e2014-02-10 14:17:30 +0000373 // Detect calls to free.
Chandler Carruth81aa7122019-01-07 05:42:51 +0000374 if (Call->isArgOperand(&U) && isFreeCall(I, &TLI)) {
Chandler Carruth30b2dba2015-07-22 22:10:05 +0000375 if (Writers)
Chandler Carruth81aa7122019-01-07 05:42:51 +0000376 Writers->insert(Call->getParent()->getParent());
Chandler Carruth30b2dba2015-07-22 22:10:05 +0000377 } else {
Benjamin Kramerdf3ae8e2014-02-10 14:17:30 +0000378 return true; // Argument of an unknown call.
Chandler Carruth30b2dba2015-07-22 22:10:05 +0000379 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000380 }
Chandler Carruth36b699f2014-03-09 03:16:01 +0000381 } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000382 if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000383 return true; // Allow comparison against null.
Eli Friedmanaba00702016-10-04 00:03:55 +0000384 } else if (Constant *C = dyn_cast<Constant>(I)) {
Eli Friedman10310d22016-10-24 21:47:44 +0000385 // Ignore constants which don't have any live uses.
386 if (isa<GlobalValue>(C) || C->isConstantUsed())
387 return true;
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000388 } else {
389 return true;
390 }
Gabor Greifd7cc5212010-07-09 15:53:42 +0000391 }
392
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000393 return false;
394}
395
Chris Lattnerab383582006-10-01 22:36:45 +0000396/// 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 Molloyd56ad582015-10-28 10:41:29 +0000403bool GlobalsAAResult::AnalyzeIndirectGlobalMemory(GlobalVariable *GV) {
Chris Lattnerab383582006-10-01 22:36:45 +0000404 // 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 Carruth2364d1f2015-07-14 08:42:39 +0000406 std::vector<Value *> AllocRelatedValues;
Duncan Sands9a036b92008-09-03 12:55:42 +0000407
James Molloyd56ad582015-10-28 10:41:29 +0000408 // If the initializer is a valid pointer, bail.
409 if (Constant *C = GV->getInitializer())
410 if (!C->isNullValue())
411 return false;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000412
Chris Lattnerab383582006-10-01 22:36:45 +0000413 // Walk the user list of the global. If we find anything other than a direct
414 // load or store, bail out.
Chandler Carruth36b699f2014-03-09 03:16:01 +0000415 for (User *U : GV->users()) {
Gabor Greif8ba992ad2010-07-09 16:22:36 +0000416 if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
Chris Lattnerab383582006-10-01 22:36:45 +0000417 // 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 Carruth30b2dba2015-07-22 22:10:05 +0000420 if (AnalyzeUsesOfPointer(LI))
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000421 return false; // Loaded pointer escapes.
Chris Lattnerab383582006-10-01 22:36:45 +0000422 // TODO: Could try some IP mod/ref of the loaded pointer.
Gabor Greif8ba992ad2010-07-09 16:22:36 +0000423 } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
Chris Lattnerab383582006-10-01 22:36:45 +0000424 // Storing the global itself.
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000425 if (SI->getOperand(0) == GV)
426 return false;
Duncan Sands9a036b92008-09-03 12:55:42 +0000427
Chris Lattnerab383582006-10-01 22:36:45 +0000428 // If storing the null pointer, ignore it.
429 if (isa<ConstantPointerNull>(SI->getOperand(0)))
430 continue;
Duncan Sands9a036b92008-09-03 12:55:42 +0000431
Chris Lattnerab383582006-10-01 22:36:45 +0000432 // Check the value being stored.
Mehdi Amini529919f2015-03-10 02:37:25 +0000433 Value *Ptr = GetUnderlyingObject(SI->getOperand(0),
434 GV->getParent()->getDataLayout());
Chris Lattnerab383582006-10-01 22:36:45 +0000435
Chandler Carruth91468332015-09-09 17:55:00 +0000436 if (!isAllocLikeFn(Ptr, &TLI))
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000437 return false; // Too hard to analyze.
Duncan Sands9a036b92008-09-03 12:55:42 +0000438
Chris Lattnerab383582006-10-01 22:36:45 +0000439 // 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 Carruth30b2dba2015-07-22 22:10:05 +0000441 if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr,
442 GV))
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000443 return false; // Loaded pointer escapes.
Chris Lattnerab383582006-10-01 22:36:45 +0000444
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 Sands9a036b92008-09-03 12:55:42 +0000452
Chris Lattnerab383582006-10-01 22:36:45 +0000453 // 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 Carruth0ea47602015-07-22 09:27:58 +0000457 Handles.emplace_front(*this, AllocRelatedValues.back());
458 Handles.front().I = Handles.begin();
Chris Lattnerab383582006-10-01 22:36:45 +0000459 AllocRelatedValues.pop_back();
460 }
461 IndirectGlobals.insert(GV);
Chandler Carruth0ea47602015-07-22 09:27:58 +0000462 Handles.emplace_front(*this, GV);
463 Handles.front().I = Handles.begin();
Chris Lattnerab383582006-10-01 22:36:45 +0000464 return true;
465}
466
Fangrui Songaf7b1832018-07-30 19:41:25 +0000467void GlobalsAAResult::CollectSCCMembership(CallGraph &CG) {
James Molloy2b276482015-09-25 15:39:29 +0000468 // 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 Lattner3b04a8a2004-06-28 06:33:13 +0000482/// 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 Lattnerfe98f272004-07-27 06:40:37 +0000484/// graph to all callers and compute the mod/ref info for all memory for each
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000485/// function.
Chandler Carruth91468332015-09-09 17:55:00 +0000486void GlobalsAAResult::AnalyzeCallGraph(CallGraph &CG, Module &M) {
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000487 // We do a bottom-up SCC traversal of the call graph. In other words, we
488 // visit all callees before callers (leaf-first).
Chandler Carruth2364d1f2015-07-14 08:42:39 +0000489 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithdb8c1ae2014-04-25 18:24:50 +0000490 const std::vector<CallGraphNode *> &SCC = *I;
Duncan Sandsf423abc2008-09-04 19:16:20 +0000491 assert(!SCC.empty() && "SCC with no functions?");
Duncan Sands9a036b92008-09-03 12:55:42 +0000492
David Blaikie3aa7f802017-06-06 20:51:15 +0000493 Function *F = SCC[0]->getFunction();
494
David Blaikie96e2a992017-06-07 21:37:39 +0000495 if (!F || !F->isDefinitionExact()) {
Sanjoy Dasc9e3e3c2016-04-08 00:48:30 +0000496 // 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 Carruthf31b1ec2015-07-15 08:09:23 +0000499 for (auto *Node : SCC)
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000500 FunctionInfos.erase(Node->getFunction());
Duncan Sands9a036b92008-09-03 12:55:42 +0000501 continue;
Duncan Sandsf423abc2008-09-04 19:16:20 +0000502 }
503
David Blaikie3aa7f802017-06-06 20:51:15 +0000504 FunctionInfo &FI = FunctionInfos[F];
Chandler Carruth27558192018-03-16 23:51:33 +0000505 Handles.emplace_front(*this, F);
506 Handles.front().I = Handles.begin();
Duncan Sands9a036b92008-09-03 12:55:42 +0000507 bool KnowNothing = false;
Chris Lattnerfe98f272004-07-27 06:40:37 +0000508
Duncan Sands9a036b92008-09-03 12:55:42 +0000509 // 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 Sands9a036b92008-09-03 12:55:42 +0000512 if (!F) {
513 KnowNothing = true;
Chris Lattnerfe98f272004-07-27 06:40:37 +0000514 break;
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000515 }
Chris Lattnerfe98f272004-07-27 06:40:37 +0000516
David Blaikie96e2a992017-06-07 21:37:39 +0000517 if (F->isDeclaration() || F->hasFnAttribute(Attribute::OptimizeNone)) {
Duncan Sands9a036b92008-09-03 12:55:42 +0000518 // Try to get mod/ref behaviour from function attributes.
Amaury Sechetc9f1b312016-01-06 13:23:52 +0000519 if (F->doesNotAccessMemory()) {
Duncan Sandsd0ac3732008-09-03 15:31:24 +0000520 // Can't do better than that!
521 } else if (F->onlyReadsMemory()) {
Alina Sbirleac94e8962017-12-07 22:41:34 +0000522 FI.addModRefInfo(ModRefInfo::Ref);
Tom Stellard6e4b75d2016-07-14 15:50:27 +0000523 if (!F->isIntrinsic() && !F->onlyAccessesArgMemory())
Duncan Sands1abe60b2008-09-11 15:43:12 +0000524 // This function might call back into the module and read a global -
Duncan Sands2bb4a4d2008-09-12 07:29:58 +0000525 // consider every global as possibly being read by this function.
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000526 FI.setMayReadAnyGlobal();
Duncan Sandsd0ac3732008-09-03 15:31:24 +0000527 } else {
Alina Sbirleac94e8962017-12-07 22:41:34 +0000528 FI.addModRefInfo(ModRefInfo::ModRef);
Duncan Sands892b8402008-09-11 19:35:55 +0000529 // 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 Sands9a036b92008-09-03 12:55:42 +0000532 }
533 continue;
534 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000535
Duncan Sands9a036b92008-09-03 12:55:42 +0000536 for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
Duncan Sandsf423abc2008-09-04 19:16:20 +0000537 CI != E && !KnowNothing; ++CI)
Duncan Sands9a036b92008-09-03 12:55:42 +0000538 if (Function *Callee = CI->second->getFunction()) {
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000539 if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) {
Duncan Sands9a036b92008-09-03 12:55:42 +0000540 // Propagate function effect up.
Chandler Carruth603a9ad2015-07-23 00:12:32 +0000541 FI.addFunctionInfo(*CalleeFI);
Duncan Sands9a036b92008-09-03 12:55:42 +0000542 } 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 Majnemer975248e2016-08-11 22:21:41 +0000546 if (!is_contained(SCC, CalleeNode))
Duncan Sands9a036b92008-09-03 12:55:42 +0000547 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 Carruthd9a7d832015-07-22 23:56:31 +0000555 // from the FunctionInfos map.
Duncan Sands9a036b92008-09-03 12:55:42 +0000556 if (KnowNothing) {
Chandler Carruthf31b1ec2015-07-15 08:09:23 +0000557 for (auto *Node : SCC)
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000558 FunctionInfos.erase(Node->getFunction());
Duncan Sandsb070bee2008-09-03 16:10:55 +0000559 continue;
Duncan Sands9a036b92008-09-03 12:55:42 +0000560 }
561
562 // Scan the function bodies for explicit loads or stores.
Chandler Carruth7d519232015-07-15 08:53:29 +0000563 for (auto *Node : SCC) {
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000564 if (isModAndRefSet(FI.getModRefInfo()))
Chandler Carruth7d519232015-07-15 08:53:29 +0000565 break; // The mod/ref lattice saturates here.
David Blaikie3aa7f802017-06-06 20:51:15 +0000566
567 // Don't prove any properties based on the implementation of an optnone
David Blaikie96e2a992017-06-07 21:37:39 +0000568 // function. Function attributes were already used as a best approximation
569 // above.
570 if (Node->getFunction()->hasFnAttribute(Attribute::OptimizeNone))
David Blaikie3aa7f802017-06-06 20:51:15 +0000571 continue;
David Blaikie3aa7f802017-06-06 20:51:15 +0000572
Nico Rieck3dd7bf52015-08-06 19:10:45 +0000573 for (Instruction &I : instructions(Node->getFunction())) {
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000574 if (isModAndRefSet(FI.getModRefInfo()))
Chandler Carruth7d519232015-07-15 08:53:29 +0000575 break; // The mod/ref lattice saturates here.
576
577 // We handle calls specially because the graph-relevant aspects are
578 // handled above.
Chandler Carruth81aa7122019-01-07 05:42:51 +0000579 if (auto *Call = dyn_cast<CallBase>(&I)) {
580 if (isAllocationFn(Call, &TLI) || isFreeCall(Call, &TLI)) {
Chandler Carruth7d519232015-07-15 08:53:29 +0000581 // FIXME: It is completely unclear why this is necessary and not
582 // handled by the above graph code.
Alina Sbirleac94e8962017-12-07 22:41:34 +0000583 FI.addModRefInfo(ModRefInfo::ModRef);
Chandler Carruth81aa7122019-01-07 05:42:51 +0000584 } else if (Function *Callee = Call->getCalledFunction()) {
Chandler Carruth7d519232015-07-15 08:53:29 +0000585 // The callgraph doesn't include intrinsic calls.
586 if (Callee->isIntrinsic()) {
Chandler Carruth81aa7122019-01-07 05:42:51 +0000587 if (isa<DbgInfoIntrinsic>(Call))
Mikael Holmenf70e21b2018-01-15 07:05:51 +0000588 // Don't let dbg intrinsics affect alias info.
589 continue;
590
Chandler Carruth52ab0bc2015-07-22 23:15:57 +0000591 FunctionModRefBehavior Behaviour =
Chandler Carruth91468332015-09-09 17:55:00 +0000592 AAResultBase::getModRefBehavior(Callee);
Alina Sbirlea9c879112017-12-07 00:43:19 +0000593 FI.addModRefInfo(createModRefInfo(Behaviour));
Chandler Carruth7d519232015-07-15 08:53:29 +0000594 }
595 }
596 continue;
Duncan Sandsb8ca4ff2008-09-13 12:45:50 +0000597 }
Duncan Sands9a036b92008-09-03 12:55:42 +0000598
Chandler Carruth7d519232015-07-15 08:53:29 +0000599 // All non-call instructions we use the primary predicates for whether
600 // thay read or write memory.
601 if (I.mayReadFromMemory())
Alina Sbirleac94e8962017-12-07 22:41:34 +0000602 FI.addModRefInfo(ModRefInfo::Ref);
Chandler Carruth7d519232015-07-15 08:53:29 +0000603 if (I.mayWriteToMemory())
Alina Sbirleac94e8962017-12-07 22:41:34 +0000604 FI.addModRefInfo(ModRefInfo::Mod);
Chandler Carruth7d519232015-07-15 08:53:29 +0000605 }
606 }
607
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000608 if (!isModSet(FI.getModRefInfo()))
Duncan Sands9a036b92008-09-03 12:55:42 +0000609 ++NumReadMemFunctions;
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000610 if (!isModOrRefSet(FI.getModRefInfo()))
Duncan Sands9a036b92008-09-03 12:55:42 +0000611 ++NumNoMemFunctions;
Duncan Sands9a036b92008-09-03 12:55:42 +0000612
613 // Finally, now that we know the full effect on this SCC, clone the
614 // information to each function in the SCC.
James Molloyf9cd1be2015-10-19 08:54:59 +0000615 // 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 Sands9a036b92008-09-03 12:55:42 +0000618 for (unsigned i = 1, e = SCC.size(); i != e; ++i)
James Molloyf9cd1be2015-10-19 08:54:59 +0000619 FunctionInfos[SCC[i]->getFunction()] = CachedFI;
Chris Lattnerfe98f272004-07-27 06:40:37 +0000620 }
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000621}
622
James Molloy60851822015-10-22 13:44:26 +0000623// 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.
626static 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 Songaf7b1832018-07-30 19:41:25 +0000636
James Molloy60851822015-10-22 13:44:26 +0000637 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 Songaf7b1832018-07-30 19:41:25 +0000657 }
James Molloy60851822015-10-22 13:44:26 +0000658 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 Songaf7b1832018-07-30 19:41:25 +0000675
James Molloy60851822015-10-22 13:44:26 +0000676 return false;
677 } while (!Inputs.empty());
678
679 // All inputs were known to be no-alias.
680 return true;
681}
682
Chandler Carruthbdf5dc72015-08-05 17:58:30 +0000683// 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 Carruth91468332015-09-09 17:55:00 +0000708bool GlobalsAAResult::isNonEscapingGlobalNoAlias(const GlobalValue *GV,
709 const Value *V) {
Chandler Carruthbdf5dc72015-08-05 17:58:30 +0000710 // 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 Kupersteinea7c9942015-08-11 08:06:44 +0000730 // 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 Dasc9e3e3c2016-04-08 00:48:30 +0000736 !GVar->isInterposable() && !InputGVar->isInterposable()) {
Michael Kupersteinea7c9942015-08-11 08:06:44 +0000737 Type *GVType = GVar->getInitializer()->getType();
738 Type *InputGVType = InputGVar->getInitializer()->getType();
739 if (GVType->isSized() && InputGVType->isSized() &&
Chandler Carruth91468332015-09-09 17:55:00 +0000740 (DL.getTypeAllocSize(GVType) > 0) &&
741 (DL.getTypeAllocSize(InputGVType) > 0))
Michael Kupersteinea7c9942015-08-11 08:06:44 +0000742 continue;
743 }
744
745 // Conservatively return false, even though we could be smarter
746 // (e.g. look through GlobalAliases).
Chandler Carruthbdf5dc72015-08-05 17:58:30 +0000747 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 Songaf7b1832018-07-30 19:41:25 +0000757
James Molloy60851822015-10-22 13:44:26 +0000758 // Recurse through a limited number of selects, loads and PHIs. This is an
Chandler Carruthbdf5dc72015-08-05 17:58:30 +0000759 // 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 Molloy60851822015-10-22 13:44:26 +0000764
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 Carruthbdf5dc72015-08-05 17:58:30 +0000775 if (auto *SI = dyn_cast<SelectInst>(Input)) {
Chandler Carruth91468332015-09-09 17:55:00 +0000776 const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL);
777 const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL);
Chandler Carruthbdf5dc72015-08-05 17:58:30 +0000778 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 Carruth91468332015-09-09 17:55:00 +0000786 Op = GetUnderlyingObject(Op, DL);
Chandler Carruthbdf5dc72015-08-05 17:58:30 +0000787 if (Visited.insert(Op).second)
788 Inputs.push_back(Op);
789 }
790 continue;
791 }
792
Michael Kupersteinea7c9942015-08-11 08:06:44 +0000793 // 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 Carruth91468332015-09-09 17:55:00 +0000795 // of BasicAA into this code. We could recurse into AAResultBase::alias
Michael Kupersteinea7c9942015-08-11 08:06:44 +0000796 // 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 Carruthbdf5dc72015-08-05 17:58:30 +0000799 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 Lattner3b04a8a2004-06-28 06:33:13 +0000806/// 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 Carruth91468332015-09-09 17:55:00 +0000809AliasResult GlobalsAAResult::alias(const MemoryLocation &LocA,
810 const MemoryLocation &LocB) {
Chris Lattnerab383582006-10-01 22:36:45 +0000811 // Get the base object these pointers point to.
Chandler Carruth91468332015-09-09 17:55:00 +0000812 const Value *UV1 = GetUnderlyingObject(LocA.Ptr, DL);
813 const Value *UV2 = GetUnderlyingObject(LocB.Ptr, DL);
Duncan Sands9a036b92008-09-03 12:55:42 +0000814
Chris Lattnerab383582006-10-01 22:36:45 +0000815 // If either of the underlying values is a global, they may be non-addr-taken
816 // globals, which we can answer queries about.
Dan Gohman79fca6f2010-08-03 21:48:53 +0000817 const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
818 const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
Chris Lattnerab383582006-10-01 22:36:45 +0000819 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 Carruth2364d1f2015-07-14 08:42:39 +0000822 if (GV1 && !NonAddressTakenGlobals.count(GV1))
823 GV1 = nullptr;
824 if (GV2 && !NonAddressTakenGlobals.count(GV2))
825 GV2 = nullptr;
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000826
Dan Gohmanf451cb82010-02-10 16:03:48 +0000827 // If the two pointers are derived from two different non-addr-taken
Chandler Carruth81f4bf72015-07-17 06:58:24 +0000828 // globals we know these can't alias.
829 if (GV1 && GV2 && GV1 != GV2)
Chris Lattnerab383582006-10-01 22:36:45 +0000830 return NoAlias;
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000831
Chandler Carruth81f4bf72015-07-17 06:58:24 +0000832 // 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 Carruthbdf5dc72015-08-05 17:58:30 +0000839 // Check for a special case where a non-escaping global can be used to
840 // conclude no-alias.
Chandler Carruth3b948582015-07-28 11:11:11 +0000841 if ((GV1 || GV2) && GV1 != GV2) {
Chandler Carruthbdf5dc72015-08-05 17:58:30 +0000842 const GlobalValue *GV = GV1 ? GV1 : GV2;
Chandler Carruth3b948582015-07-28 11:11:11 +0000843 const Value *UV = GV1 ? UV2 : UV1;
Chandler Carruthbdf5dc72015-08-05 17:58:30 +0000844 if (isNonEscapingGlobalNoAlias(GV, UV))
Chandler Carruth3b948582015-07-28 11:11:11 +0000845 return NoAlias;
Chandler Carruth3b948582015-07-28 11:11:11 +0000846 }
847
Chris Lattnerab383582006-10-01 22:36:45 +0000848 // 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 Sands9a036b92008-09-03 12:55:42 +0000851
Chris Lattnerab383582006-10-01 22:36:45 +0000852 // 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 Toppere703fcb2014-04-24 06:44:33 +0000855 GV1 = GV2 = nullptr;
Dan Gohman79fca6f2010-08-03 21:48:53 +0000856 if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))
Chris Lattnerab383582006-10-01 22:36:45 +0000857 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
858 if (IndirectGlobals.count(GV))
859 GV1 = GV;
Dan Gohman79fca6f2010-08-03 21:48:53 +0000860 if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))
861 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
Chris Lattnerab383582006-10-01 22:36:45 +0000862 if (IndirectGlobals.count(GV))
863 GV2 = GV;
Duncan Sands9a036b92008-09-03 12:55:42 +0000864
Chris Lattnerab383582006-10-01 22:36:45 +0000865 // These pointers may also be from an allocation for the indirect global. If
866 // so, also handle them.
Chandler Carruth5b6ebf52015-07-22 11:43:24 +0000867 if (!GV1)
868 GV1 = AllocsForIndirectGlobals.lookup(UV1);
869 if (!GV2)
870 GV2 = AllocsForIndirectGlobals.lookup(UV2);
Duncan Sands9a036b92008-09-03 12:55:42 +0000871
Chris Lattnerab383582006-10-01 22:36:45 +0000872 // Now that we know whether the two pointers are related to indirect globals,
Chandler Carruth81f4bf72015-07-17 06:58:24 +0000873 // 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 Lattner3b04a8a2004-06-28 06:33:13 +0000876 return NoAlias;
Duncan Sands9a036b92008-09-03 12:55:42 +0000877
Chandler Carruth81f4bf72015-07-17 06:58:24 +0000878 // 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 Carruth91468332015-09-09 17:55:00 +0000885 return AAResultBase::alias(LocA, LocB);
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000886}
887
Chandler Carruth81aa7122019-01-07 05:42:51 +0000888ModRefInfo GlobalsAAResult::getModRefInfoForArgument(const CallBase *Call,
James Molloy2b276482015-09-25 15:39:29 +0000889 const GlobalValue *GV) {
Chandler Carruth81aa7122019-01-07 05:42:51 +0000890 if (Call->doesNotAccessMemory())
Alina Sbirleac94e8962017-12-07 22:41:34 +0000891 return ModRefInfo::NoModRef;
892 ModRefInfo ConservativeResult =
Chandler Carruth81aa7122019-01-07 05:42:51 +0000893 Call->onlyReadsMemory() ? ModRefInfo::Ref : ModRefInfo::ModRef;
David Majnemerdc9c7372016-08-11 21:15:00 +0000894
James Molloy2b276482015-09-25 15:39:29 +0000895 // Iterate through all the arguments to the called function. If any argument
896 // is based on GV, return the conservative result.
Chandler Carruth81aa7122019-01-07 05:42:51 +0000897 for (auto &A : Call->args()) {
James Molloy2b276482015-09-25 15:39:29 +0000898 SmallVector<Value*, 4> Objects;
899 GetUnderlyingObjects(A, Objects, DL);
David Majnemerdc9c7372016-08-11 21:15:00 +0000900
James Molloy2b276482015-09-25 15:39:29 +0000901 // All objects must be identified.
David Majnemerdc9c7372016-08-11 21:15:00 +0000902 if (!all_of(Objects, isIdentifiedObject) &&
Vaivaswatha Nagaraj741199b2016-01-14 08:46:45 +0000903 // Try ::alias to see if all objects are known not to alias GV.
David Majnemerdc9c7372016-08-11 21:15:00 +0000904 !all_of(Objects, [&](Value *V) {
Vaivaswatha Nagaraj741199b2016-01-14 08:46:45 +0000905 return this->alias(MemoryLocation(V), MemoryLocation(GV)) == NoAlias;
David Majnemerdc9c7372016-08-11 21:15:00 +0000906 }))
James Molloy2b276482015-09-25 15:39:29 +0000907 return ConservativeResult;
908
David Majnemerdc9c7372016-08-11 21:15:00 +0000909 if (is_contained(Objects, GV))
James Molloy2b276482015-09-25 15:39:29 +0000910 return ConservativeResult;
911 }
912
913 // We identified all objects in the argument list, and none of them were GV.
Alina Sbirleac94e8962017-12-07 22:41:34 +0000914 return ModRefInfo::NoModRef;
James Molloy2b276482015-09-25 15:39:29 +0000915}
916
Chandler Carruth81aa7122019-01-07 05:42:51 +0000917ModRefInfo GlobalsAAResult::getModRefInfo(const CallBase *Call,
Chandler Carruth91468332015-09-09 17:55:00 +0000918 const MemoryLocation &Loc) {
Alina Sbirleac94e8962017-12-07 22:41:34 +0000919 ModRefInfo Known = ModRefInfo::ModRef;
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000920
921 // If we are asking for mod/ref info of a direct call with a pointer to a
Chris Lattnerfe98f272004-07-27 06:40:37 +0000922 // global we are tracking, return information if we have it.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000923 if (const GlobalValue *GV =
Mehdi Amini529919f2015-03-10 02:37:25 +0000924 dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL)))
Rafael Espindolabb46f522009-01-15 20:18:42 +0000925 if (GV->hasLocalLinkage())
Chandler Carruth81aa7122019-01-07 05:42:51 +0000926 if (const Function *F = Call->getCalledFunction())
Chris Lattnerfe98f272004-07-27 06:40:37 +0000927 if (NonAddressTakenGlobals.count(GV))
Chandler Carruthd9a7d832015-07-22 23:56:31 +0000928 if (const FunctionInfo *FI = getFunctionInfo(F))
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000929 Known = unionModRef(FI->getModRefInfoForGlobal(*GV),
Chandler Carruth81aa7122019-01-07 05:42:51 +0000930 getModRefInfoForArgument(Call, GV));
Chris Lattner3b04a8a2004-06-28 06:33:13 +0000931
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000932 if (!isModOrRefSet(Known))
Alina Sbirleac94e8962017-12-07 22:41:34 +0000933 return ModRefInfo::NoModRef; // No need to query other mod/ref analyses
Chandler Carruth81aa7122019-01-07 05:42:51 +0000934 return intersectModRef(Known, AAResultBase::getModRefInfo(Call, Loc));
Chandler Carruth91468332015-09-09 17:55:00 +0000935}
936
937GlobalsAAResult::GlobalsAAResult(const DataLayout &DL,
938 const TargetLibraryInfo &TLI)
Chandler Carruthcf88e922016-03-02 15:56:53 +0000939 : AAResultBase(), DL(DL), TLI(TLI) {}
Chandler Carruth91468332015-09-09 17:55:00 +0000940
941GlobalsAAResult::GlobalsAAResult(GlobalsAAResult &&Arg)
Chandler Carruthcf88e922016-03-02 15:56:53 +0000942 : AAResultBase(std::move(Arg)), DL(Arg.DL), TLI(Arg.TLI),
NAKAMURA Takumieb2f3522015-09-10 07:16:42 +0000943 NonAddressTakenGlobals(std::move(Arg.NonAddressTakenGlobals)),
944 IndirectGlobals(std::move(Arg.IndirectGlobals)),
945 AllocsForIndirectGlobals(std::move(Arg.AllocsForIndirectGlobals)),
946 FunctionInfos(std::move(Arg.FunctionInfos)),
NAKAMURA Takumi8f188e02015-09-14 06:16:44 +0000947 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 Carruth91468332015-09-09 17:55:00 +0000954
Chandler Carruthf51faf02016-03-11 09:15:11 +0000955GlobalsAAResult::~GlobalsAAResult() {}
956
Chandler Carruth91468332015-09-09 17:55:00 +0000957/*static*/ GlobalsAAResult
958GlobalsAAResult::analyzeModule(Module &M, const TargetLibraryInfo &TLI,
959 CallGraph &CG) {
960 GlobalsAAResult Result(M.getDataLayout(), TLI);
961
James Molloy2b276482015-09-25 15:39:29 +0000962 // Discover which functions aren't recursive, to feed into AnalyzeGlobals.
963 Result.CollectSCCMembership(CG);
964
Chandler Carruth91468332015-09-09 17:55:00 +0000965 // 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 Carruth33d56812016-11-23 17:53:26 +0000974AnalysisKey GlobalsAA::Key;
Chandler Carruthe95015f2016-03-11 10:22:49 +0000975
Sean Silva2fb9a982016-08-09 00:28:38 +0000976GlobalsAAResult GlobalsAA::run(Module &M, ModuleAnalysisManager &AM) {
Chandler Carruth91468332015-09-09 17:55:00 +0000977 return GlobalsAAResult::analyzeModule(M,
Chandler Carruth8e27cb22016-03-11 11:05:24 +0000978 AM.getResult<TargetLibraryAnalysis>(M),
979 AM.getResult<CallGraphAnalysis>(M));
Chandler Carruth91468332015-09-09 17:55:00 +0000980}
981
Chandler Carruth91468332015-09-09 17:55:00 +0000982char GlobalsAAWrapperPass::ID = 0;
983INITIALIZE_PASS_BEGIN(GlobalsAAWrapperPass, "globals-aa",
984 "Globals Alias Analysis", false, true)
985INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
986INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
987INITIALIZE_PASS_END(GlobalsAAWrapperPass, "globals-aa",
988 "Globals Alias Analysis", false, true)
989
990ModulePass *llvm::createGlobalsAAWrapperPass() {
991 return new GlobalsAAWrapperPass();
992}
993
994GlobalsAAWrapperPass::GlobalsAAWrapperPass() : ModulePass(ID) {
995 initializeGlobalsAAWrapperPassPass(*PassRegistry::getPassRegistry());
996}
997
998bool 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
1005bool GlobalsAAWrapperPass::doFinalization(Module &M) {
1006 Result.reset();
1007 return false;
1008}
1009
1010void GlobalsAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1011 AU.setPreservesAll();
1012 AU.addRequired<CallGraphWrapperPass>();
1013 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chris Lattner3b04a8a2004-06-28 06:33:13 +00001014}