GlobalsModRef.cpp 42 KB

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