GlobalsModRef.cpp 41 KB

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