GlobalsModRef.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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/MemoryBuiltins.h"
  20. #include "llvm/Analysis/TargetLibraryInfo.h"
  21. #include "llvm/Analysis/ValueTracking.h"
  22. #include "llvm/IR/DerivedTypes.h"
  23. #include "llvm/IR/InstIterator.h"
  24. #include "llvm/IR/Instructions.h"
  25. #include "llvm/IR/IntrinsicInst.h"
  26. #include "llvm/IR/Module.h"
  27. #include "llvm/Pass.h"
  28. #include "llvm/Support/CommandLine.h"
  29. using namespace llvm;
  30. #define DEBUG_TYPE "globalsmodref-aa"
  31. STATISTIC(NumNonAddrTakenGlobalVars,
  32. "Number of global vars without address taken");
  33. STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
  34. STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
  35. STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
  36. STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
  37. // An option to enable unsafe alias results from the GlobalsModRef analysis.
  38. // When enabled, GlobalsModRef will provide no-alias results which in extremely
  39. // rare cases may not be conservatively correct. In particular, in the face of
  40. // transforms which cause assymetry between how effective GetUnderlyingObject
  41. // is for two pointers, it may produce incorrect results.
  42. //
  43. // These unsafe results have been returned by GMR for many years without
  44. // causing significant issues in the wild and so we provide a mechanism to
  45. // re-enable them for users of LLVM that have a particular performance
  46. // sensitivity and no known issues. The option also makes it easy to evaluate
  47. // the performance impact of these results.
  48. static cl::opt<bool> EnableUnsafeGlobalsModRefAliasResults(
  49. "enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden);
  50. /// The mod/ref information collected for a particular function.
  51. ///
  52. /// We collect information about mod/ref behavior of a function here, both in
  53. /// general and as pertains to specific globals. We only have this detailed
  54. /// information when we know *something* useful about the behavior. If we
  55. /// saturate to fully general mod/ref, we remove the info for the function.
  56. class GlobalsAAResult::FunctionInfo {
  57. typedef SmallDenseMap<const GlobalValue *, ModRefInfo, 16> GlobalInfoMapType;
  58. /// Build a wrapper struct that has 8-byte alignment. All heap allocations
  59. /// should provide this much alignment at least, but this makes it clear we
  60. /// specifically rely on this amount of alignment.
  61. struct alignas(8) AlignedMap {
  62. AlignedMap() {}
  63. AlignedMap(const AlignedMap &Arg) : Map(Arg.Map) {}
  64. GlobalInfoMapType Map;
  65. };
  66. /// Pointer traits for our aligned map.
  67. struct AlignedMapPointerTraits {
  68. static inline void *getAsVoidPointer(AlignedMap *P) { return P; }
  69. static inline AlignedMap *getFromVoidPointer(void *P) {
  70. return (AlignedMap *)P;
  71. }
  72. enum { NumLowBitsAvailable = 3 };
  73. static_assert(alignof(AlignedMap) >= (1 << NumLowBitsAvailable),
  74. "AlignedMap insufficiently aligned to have enough low bits.");
  75. };
  76. /// The bit that flags that this function may read any global. This is
  77. /// chosen to mix together with ModRefInfo bits.
  78. /// FIXME: This assumes ModRefInfo lattice will remain 4 bits!
  79. /// It overlaps with ModRefInfo::Must bit!
  80. /// FunctionInfo.getModRefInfo() masks out everything except ModRef so
  81. /// this remains correct, but the Must info is lost.
  82. enum { MayReadAnyGlobal = 4 };
  83. /// Checks to document the invariants of the bit packing here.
  84. static_assert((MayReadAnyGlobal & static_cast<int>(ModRefInfo::MustModRef)) ==
  85. 0,
  86. "ModRef and the MayReadAnyGlobal flag bits overlap.");
  87. static_assert(((MayReadAnyGlobal |
  88. static_cast<int>(ModRefInfo::MustModRef)) >>
  89. AlignedMapPointerTraits::NumLowBitsAvailable) == 0,
  90. "Insufficient low bits to store our flag and ModRef info.");
  91. public:
  92. FunctionInfo() : Info() {}
  93. ~FunctionInfo() {
  94. delete Info.getPointer();
  95. }
  96. // Spell out the copy ond move constructors and assignment operators to get
  97. // deep copy semantics and correct move semantics in the face of the
  98. // pointer-int pair.
  99. FunctionInfo(const FunctionInfo &Arg)
  100. : Info(nullptr, Arg.Info.getInt()) {
  101. if (const auto *ArgPtr = Arg.Info.getPointer())
  102. Info.setPointer(new AlignedMap(*ArgPtr));
  103. }
  104. FunctionInfo(FunctionInfo &&Arg)
  105. : Info(Arg.Info.getPointer(), Arg.Info.getInt()) {
  106. Arg.Info.setPointerAndInt(nullptr, 0);
  107. }
  108. FunctionInfo &operator=(const FunctionInfo &RHS) {
  109. delete Info.getPointer();
  110. Info.setPointerAndInt(nullptr, RHS.Info.getInt());
  111. if (const auto *RHSPtr = RHS.Info.getPointer())
  112. Info.setPointer(new AlignedMap(*RHSPtr));
  113. return *this;
  114. }
  115. FunctionInfo &operator=(FunctionInfo &&RHS) {
  116. delete Info.getPointer();
  117. Info.setPointerAndInt(RHS.Info.getPointer(), RHS.Info.getInt());
  118. RHS.Info.setPointerAndInt(nullptr, 0);
  119. return *this;
  120. }
  121. /// This method clears MayReadAnyGlobal bit added by GlobalsAAResult to return
  122. /// the corresponding ModRefInfo. It must align in functionality with
  123. /// clearMust().
  124. ModRefInfo globalClearMayReadAnyGlobal(int I) const {
  125. return ModRefInfo((I & static_cast<int>(ModRefInfo::ModRef)) |
  126. static_cast<int>(ModRefInfo::NoModRef));
  127. }
  128. /// Returns the \c ModRefInfo info for this function.
  129. ModRefInfo getModRefInfo() const {
  130. return globalClearMayReadAnyGlobal(Info.getInt());
  131. }
  132. /// Adds new \c ModRefInfo for this function to its state.
  133. void addModRefInfo(ModRefInfo NewMRI) {
  134. Info.setInt(Info.getInt() | static_cast<int>(setMust(NewMRI)));
  135. }
  136. /// Returns whether this function may read any global variable, and we don't
  137. /// know which global.
  138. bool mayReadAnyGlobal() const { return Info.getInt() & MayReadAnyGlobal; }
  139. /// Sets this function as potentially reading from any global.
  140. void setMayReadAnyGlobal() { Info.setInt(Info.getInt() | MayReadAnyGlobal); }
  141. /// Returns the \c ModRefInfo info for this function w.r.t. a particular
  142. /// global, which may be more precise than the general information above.
  143. ModRefInfo getModRefInfoForGlobal(const GlobalValue &GV) const {
  144. ModRefInfo GlobalMRI =
  145. mayReadAnyGlobal() ? ModRefInfo::Ref : ModRefInfo::NoModRef;
  146. if (AlignedMap *P = Info.getPointer()) {
  147. auto I = P->Map.find(&GV);
  148. if (I != P->Map.end())
  149. GlobalMRI = unionModRef(GlobalMRI, I->second);
  150. }
  151. return GlobalMRI;
  152. }
  153. /// Add mod/ref info from another function into ours, saturating towards
  154. /// ModRef.
  155. void addFunctionInfo(const FunctionInfo &FI) {
  156. addModRefInfo(FI.getModRefInfo());
  157. if (FI.mayReadAnyGlobal())
  158. setMayReadAnyGlobal();
  159. if (AlignedMap *P = FI.Info.getPointer())
  160. for (const auto &G : P->Map)
  161. addModRefInfoForGlobal(*G.first, G.second);
  162. }
  163. void addModRefInfoForGlobal(const GlobalValue &GV, ModRefInfo NewMRI) {
  164. AlignedMap *P = Info.getPointer();
  165. if (!P) {
  166. P = new AlignedMap();
  167. Info.setPointer(P);
  168. }
  169. auto &GlobalMRI = P->Map[&GV];
  170. GlobalMRI = unionModRef(GlobalMRI, NewMRI);
  171. }
  172. /// Clear a global's ModRef info. Should be used when a global is being
  173. /// deleted.
  174. void eraseModRefInfoForGlobal(const GlobalValue &GV) {
  175. if (AlignedMap *P = Info.getPointer())
  176. P->Map.erase(&GV);
  177. }
  178. private:
  179. /// All of the information is encoded into a single pointer, with a three bit
  180. /// integer in the low three bits. The high bit provides a flag for when this
  181. /// function may read any global. The low two bits are the ModRefInfo. And
  182. /// the pointer, when non-null, points to a map from GlobalValue to
  183. /// ModRefInfo specific to that GlobalValue.
  184. PointerIntPair<AlignedMap *, 3, unsigned, AlignedMapPointerTraits> Info;
  185. };
  186. void GlobalsAAResult::DeletionCallbackHandle::deleted() {
  187. Value *V = getValPtr();
  188. if (auto *F = dyn_cast<Function>(V))
  189. GAR->FunctionInfos.erase(F);
  190. if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
  191. if (GAR->NonAddressTakenGlobals.erase(GV)) {
  192. // This global might be an indirect global. If so, remove it and
  193. // remove any AllocRelatedValues for it.
  194. if (GAR->IndirectGlobals.erase(GV)) {
  195. // Remove any entries in AllocsForIndirectGlobals for this global.
  196. for (auto I = GAR->AllocsForIndirectGlobals.begin(),
  197. E = GAR->AllocsForIndirectGlobals.end();
  198. I != E; ++I)
  199. if (I->second == GV)
  200. GAR->AllocsForIndirectGlobals.erase(I);
  201. }
  202. // Scan the function info we have collected and remove this global
  203. // from all of them.
  204. for (auto &FIPair : GAR->FunctionInfos)
  205. FIPair.second.eraseModRefInfoForGlobal(*GV);
  206. }
  207. }
  208. // If this is an allocation related to an indirect global, remove it.
  209. GAR->AllocsForIndirectGlobals.erase(V);
  210. // And clear out the handle.
  211. setValPtr(nullptr);
  212. GAR->Handles.erase(I);
  213. // This object is now destroyed!
  214. }
  215. FunctionModRefBehavior GlobalsAAResult::getModRefBehavior(const Function *F) {
  216. FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
  217. if (FunctionInfo *FI = getFunctionInfo(F)) {
  218. if (!isModOrRefSet(FI->getModRefInfo()))
  219. Min = FMRB_DoesNotAccessMemory;
  220. else if (!isModSet(FI->getModRefInfo()))
  221. Min = FMRB_OnlyReadsMemory;
  222. }
  223. return FunctionModRefBehavior(AAResultBase::getModRefBehavior(F) & Min);
  224. }
  225. FunctionModRefBehavior
  226. GlobalsAAResult::getModRefBehavior(const CallBase *Call) {
  227. FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
  228. if (!Call->hasOperandBundles())
  229. if (const Function *F = Call->getCalledFunction())
  230. if (FunctionInfo *FI = getFunctionInfo(F)) {
  231. if (!isModOrRefSet(FI->getModRefInfo()))
  232. Min = FMRB_DoesNotAccessMemory;
  233. else if (!isModSet(FI->getModRefInfo()))
  234. Min = FMRB_OnlyReadsMemory;
  235. }
  236. return FunctionModRefBehavior(AAResultBase::getModRefBehavior(Call) & Min);
  237. }
  238. /// Returns the function info for the function, or null if we don't have
  239. /// anything useful to say about it.
  240. GlobalsAAResult::FunctionInfo *
  241. GlobalsAAResult::getFunctionInfo(const Function *F) {
  242. auto I = FunctionInfos.find(F);
  243. if (I != FunctionInfos.end())
  244. return &I->second;
  245. return nullptr;
  246. }
  247. /// AnalyzeGlobals - Scan through the users of all of the internal
  248. /// GlobalValue's in the program. If none of them have their "address taken"
  249. /// (really, their address passed to something nontrivial), record this fact,
  250. /// and record the functions that they are used directly in.
  251. void GlobalsAAResult::AnalyzeGlobals(Module &M) {
  252. SmallPtrSet<Function *, 32> TrackedFunctions;
  253. for (Function &F : M)
  254. if (F.hasLocalLinkage())
  255. if (!AnalyzeUsesOfPointer(&F)) {
  256. // Remember that we are tracking this global.
  257. NonAddressTakenGlobals.insert(&F);
  258. TrackedFunctions.insert(&F);
  259. Handles.emplace_front(*this, &F);
  260. Handles.front().I = Handles.begin();
  261. ++NumNonAddrTakenFunctions;
  262. }
  263. SmallPtrSet<Function *, 16> Readers, Writers;
  264. for (GlobalVariable &GV : M.globals())
  265. if (GV.hasLocalLinkage()) {
  266. if (!AnalyzeUsesOfPointer(&GV, &Readers,
  267. GV.isConstant() ? nullptr : &Writers)) {
  268. // Remember that we are tracking this global, and the mod/ref fns
  269. NonAddressTakenGlobals.insert(&GV);
  270. Handles.emplace_front(*this, &GV);
  271. Handles.front().I = Handles.begin();
  272. for (Function *Reader : Readers) {
  273. if (TrackedFunctions.insert(Reader).second) {
  274. Handles.emplace_front(*this, Reader);
  275. Handles.front().I = Handles.begin();
  276. }
  277. FunctionInfos[Reader].addModRefInfoForGlobal(GV, ModRefInfo::Ref);
  278. }
  279. if (!GV.isConstant()) // No need to keep track of writers to constants
  280. for (Function *Writer : Writers) {
  281. if (TrackedFunctions.insert(Writer).second) {
  282. Handles.emplace_front(*this, Writer);
  283. Handles.front().I = Handles.begin();
  284. }
  285. FunctionInfos[Writer].addModRefInfoForGlobal(GV, ModRefInfo::Mod);
  286. }
  287. ++NumNonAddrTakenGlobalVars;
  288. // If this global holds a pointer type, see if it is an indirect global.
  289. if (GV.getValueType()->isPointerTy() &&
  290. AnalyzeIndirectGlobalMemory(&GV))
  291. ++NumIndirectGlobalVars;
  292. }
  293. Readers.clear();
  294. Writers.clear();
  295. }
  296. }
  297. /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
  298. /// If this is used by anything complex (i.e., the address escapes), return
  299. /// true. Also, while we are at it, keep track of those functions that read and
  300. /// write to the value.
  301. ///
  302. /// If OkayStoreDest is non-null, stores into this global are allowed.
  303. bool GlobalsAAResult::AnalyzeUsesOfPointer(Value *V,
  304. SmallPtrSetImpl<Function *> *Readers,
  305. SmallPtrSetImpl<Function *> *Writers,
  306. GlobalValue *OkayStoreDest) {
  307. if (!V->getType()->isPointerTy())
  308. return true;
  309. for (Use &U : V->uses()) {
  310. User *I = U.getUser();
  311. if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
  312. if (Readers)
  313. Readers->insert(LI->getParent()->getParent());
  314. } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
  315. if (V == SI->getOperand(1)) {
  316. if (Writers)
  317. Writers->insert(SI->getParent()->getParent());
  318. } else if (SI->getOperand(1) != OkayStoreDest) {
  319. return true; // Storing the pointer
  320. }
  321. } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {
  322. if (AnalyzeUsesOfPointer(I, Readers, Writers))
  323. return true;
  324. } else if (Operator::getOpcode(I) == Instruction::BitCast) {
  325. if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
  326. return true;
  327. } else if (auto *Call = dyn_cast<CallBase>(I)) {
  328. // Make sure that this is just the function being called, not that it is
  329. // passing into the function.
  330. if (Call->isDataOperand(&U)) {
  331. // Detect calls to free.
  332. if (Call->isArgOperand(&U) &&
  333. isFreeCall(I, &GetTLI(*Call->getFunction()))) {
  334. if (Writers)
  335. Writers->insert(Call->getParent()->getParent());
  336. } else {
  337. return true; // Argument of an unknown call.
  338. }
  339. }
  340. } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
  341. if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
  342. return true; // Allow comparison against null.
  343. } else if (Constant *C = dyn_cast<Constant>(I)) {
  344. // Ignore constants which don't have any live uses.
  345. if (isa<GlobalValue>(C) || C->isConstantUsed())
  346. return true;
  347. } else {
  348. return true;
  349. }
  350. }
  351. return false;
  352. }
  353. /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
  354. /// which holds a pointer type. See if the global always points to non-aliased
  355. /// heap memory: that is, all initializers of the globals are allocations, and
  356. /// those allocations have no use other than initialization of the global.
  357. /// Further, all loads out of GV must directly use the memory, not store the
  358. /// pointer somewhere. If this is true, we consider the memory pointed to by
  359. /// GV to be owned by GV and can disambiguate other pointers from it.
  360. bool GlobalsAAResult::AnalyzeIndirectGlobalMemory(GlobalVariable *GV) {
  361. // Keep track of values related to the allocation of the memory, f.e. the
  362. // value produced by the malloc call and any casts.
  363. std::vector<Value *> AllocRelatedValues;
  364. // If the initializer is a valid pointer, bail.
  365. if (Constant *C = GV->getInitializer())
  366. if (!C->isNullValue())
  367. return false;
  368. // Walk the user list of the global. If we find anything other than a direct
  369. // load or store, bail out.
  370. for (User *U : GV->users()) {
  371. if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
  372. // The pointer loaded from the global can only be used in simple ways:
  373. // we allow addressing of it and loading storing to it. We do *not* allow
  374. // storing the loaded pointer somewhere else or passing to a function.
  375. if (AnalyzeUsesOfPointer(LI))
  376. return false; // Loaded pointer escapes.
  377. // TODO: Could try some IP mod/ref of the loaded pointer.
  378. } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  379. // Storing the global itself.
  380. if (SI->getOperand(0) == GV)
  381. return false;
  382. // If storing the null pointer, ignore it.
  383. if (isa<ConstantPointerNull>(SI->getOperand(0)))
  384. continue;
  385. // Check the value being stored.
  386. Value *Ptr = GetUnderlyingObject(SI->getOperand(0),
  387. GV->getParent()->getDataLayout());
  388. if (!isAllocLikeFn(Ptr, &GetTLI(*SI->getFunction())))
  389. return false; // Too hard to analyze.
  390. // Analyze all uses of the allocation. If any of them are used in a
  391. // non-simple way (e.g. stored to another global) bail out.
  392. if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr,
  393. GV))
  394. return false; // Loaded pointer escapes.
  395. // Remember that this allocation is related to the indirect global.
  396. AllocRelatedValues.push_back(Ptr);
  397. } else {
  398. // Something complex, bail out.
  399. return false;
  400. }
  401. }
  402. // Okay, this is an indirect global. Remember all of the allocations for
  403. // this global in AllocsForIndirectGlobals.
  404. while (!AllocRelatedValues.empty()) {
  405. AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
  406. Handles.emplace_front(*this, AllocRelatedValues.back());
  407. Handles.front().I = Handles.begin();
  408. AllocRelatedValues.pop_back();
  409. }
  410. IndirectGlobals.insert(GV);
  411. Handles.emplace_front(*this, GV);
  412. Handles.front().I = Handles.begin();
  413. return true;
  414. }
  415. void GlobalsAAResult::CollectSCCMembership(CallGraph &CG) {
  416. // We do a bottom-up SCC traversal of the call graph. In other words, we
  417. // visit all callees before callers (leaf-first).
  418. unsigned SCCID = 0;
  419. for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
  420. const std::vector<CallGraphNode *> &SCC = *I;
  421. assert(!SCC.empty() && "SCC with no functions?");
  422. for (auto *CGN : SCC)
  423. if (Function *F = CGN->getFunction())
  424. FunctionToSCCMap[F] = SCCID;
  425. ++SCCID;
  426. }
  427. }
  428. /// AnalyzeCallGraph - At this point, we know the functions where globals are
  429. /// immediately stored to and read from. Propagate this information up the call
  430. /// graph to all callers and compute the mod/ref info for all memory for each
  431. /// function.
  432. void GlobalsAAResult::AnalyzeCallGraph(CallGraph &CG, Module &M) {
  433. // We do a bottom-up SCC traversal of the call graph. In other words, we
  434. // visit all callees before callers (leaf-first).
  435. for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
  436. const std::vector<CallGraphNode *> &SCC = *I;
  437. assert(!SCC.empty() && "SCC with no functions?");
  438. Function *F = SCC[0]->getFunction();
  439. if (!F || !F->isDefinitionExact()) {
  440. // Calls externally or not exact - can't say anything useful. Remove any
  441. // existing function records (may have been created when scanning
  442. // globals).
  443. for (auto *Node : SCC)
  444. FunctionInfos.erase(Node->getFunction());
  445. continue;
  446. }
  447. FunctionInfo &FI = FunctionInfos[F];
  448. Handles.emplace_front(*this, F);
  449. Handles.front().I = Handles.begin();
  450. bool KnowNothing = false;
  451. // Collect the mod/ref properties due to called functions. We only compute
  452. // one mod-ref set.
  453. for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
  454. if (!F) {
  455. KnowNothing = true;
  456. break;
  457. }
  458. if (F->isDeclaration() || F->hasOptNone()) {
  459. // Try to get mod/ref behaviour from function attributes.
  460. if (F->doesNotAccessMemory()) {
  461. // Can't do better than that!
  462. } else if (F->onlyReadsMemory()) {
  463. FI.addModRefInfo(ModRefInfo::Ref);
  464. if (!F->isIntrinsic() && !F->onlyAccessesArgMemory())
  465. // This function might call back into the module and read a global -
  466. // consider every global as possibly being read by this function.
  467. FI.setMayReadAnyGlobal();
  468. } else {
  469. FI.addModRefInfo(ModRefInfo::ModRef);
  470. // Can't say anything useful unless it's an intrinsic - they don't
  471. // read or write global variables of the kind considered here.
  472. KnowNothing = !F->isIntrinsic();
  473. }
  474. continue;
  475. }
  476. for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
  477. CI != E && !KnowNothing; ++CI)
  478. if (Function *Callee = CI->second->getFunction()) {
  479. if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) {
  480. // Propagate function effect up.
  481. FI.addFunctionInfo(*CalleeFI);
  482. } else {
  483. // Can't say anything about it. However, if it is inside our SCC,
  484. // then nothing needs to be done.
  485. CallGraphNode *CalleeNode = CG[Callee];
  486. if (!is_contained(SCC, CalleeNode))
  487. KnowNothing = true;
  488. }
  489. } else {
  490. KnowNothing = true;
  491. }
  492. }
  493. // If we can't say anything useful about this SCC, remove all SCC functions
  494. // from the FunctionInfos map.
  495. if (KnowNothing) {
  496. for (auto *Node : SCC)
  497. FunctionInfos.erase(Node->getFunction());
  498. continue;
  499. }
  500. // Scan the function bodies for explicit loads or stores.
  501. for (auto *Node : SCC) {
  502. if (isModAndRefSet(FI.getModRefInfo()))
  503. break; // The mod/ref lattice saturates here.
  504. // Don't prove any properties based on the implementation of an optnone
  505. // function. Function attributes were already used as a best approximation
  506. // above.
  507. if (Node->getFunction()->hasOptNone())
  508. continue;
  509. for (Instruction &I : instructions(Node->getFunction())) {
  510. if (isModAndRefSet(FI.getModRefInfo()))
  511. break; // The mod/ref lattice saturates here.
  512. // We handle calls specially because the graph-relevant aspects are
  513. // handled above.
  514. if (auto *Call = dyn_cast<CallBase>(&I)) {
  515. auto &TLI = GetTLI(*Node->getFunction());
  516. if (isAllocationFn(Call, &TLI) || isFreeCall(Call, &TLI)) {
  517. // FIXME: It is completely unclear why this is necessary and not
  518. // handled by the above graph code.
  519. FI.addModRefInfo(ModRefInfo::ModRef);
  520. } else if (Function *Callee = Call->getCalledFunction()) {
  521. // The callgraph doesn't include intrinsic calls.
  522. if (Callee->isIntrinsic()) {
  523. if (isa<DbgInfoIntrinsic>(Call))
  524. // Don't let dbg intrinsics affect alias info.
  525. continue;
  526. FunctionModRefBehavior Behaviour =
  527. AAResultBase::getModRefBehavior(Callee);
  528. FI.addModRefInfo(createModRefInfo(Behaviour));
  529. }
  530. }
  531. continue;
  532. }
  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(), DL));
  584. continue;
  585. }
  586. if (auto *SI = dyn_cast<SelectInst>(Input)) {
  587. const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL);
  588. const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL);
  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, DL);
  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(), DL);
  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(), DL);
  696. const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL);
  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, DL);
  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. /// alias - If one of the pointers is to a global that we are tracking, and the
  723. /// other is some random pointer, we know there cannot be an alias, because the
  724. /// address of the global isn't taken.
  725. AliasResult GlobalsAAResult::alias(const MemoryLocation &LocA,
  726. const MemoryLocation &LocB,
  727. AAQueryInfo &AAQI) {
  728. // Get the base object these pointers point to.
  729. const Value *UV1 = GetUnderlyingObject(LocA.Ptr, DL);
  730. const Value *UV2 = GetUnderlyingObject(LocB.Ptr, DL);
  731. // If either of the underlying values is a global, they may be non-addr-taken
  732. // globals, which we can answer queries about.
  733. const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
  734. const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
  735. if (GV1 || GV2) {
  736. // If the global's address is taken, pretend we don't know it's a pointer to
  737. // the global.
  738. if (GV1 && !NonAddressTakenGlobals.count(GV1))
  739. GV1 = nullptr;
  740. if (GV2 && !NonAddressTakenGlobals.count(GV2))
  741. GV2 = nullptr;
  742. // If the two pointers are derived from two different non-addr-taken
  743. // globals we know these can't alias.
  744. if (GV1 && GV2 && GV1 != GV2)
  745. return NoAlias;
  746. // If one is and the other isn't, it isn't strictly safe but we can fake
  747. // this result if necessary for performance. This does not appear to be
  748. // a common problem in practice.
  749. if (EnableUnsafeGlobalsModRefAliasResults)
  750. if ((GV1 || GV2) && GV1 != GV2)
  751. return NoAlias;
  752. // Check for a special case where a non-escaping global can be used to
  753. // conclude no-alias.
  754. if ((GV1 || GV2) && GV1 != GV2) {
  755. const GlobalValue *GV = GV1 ? GV1 : GV2;
  756. const Value *UV = GV1 ? UV2 : UV1;
  757. if (isNonEscapingGlobalNoAlias(GV, UV))
  758. return NoAlias;
  759. }
  760. // Otherwise if they are both derived from the same addr-taken global, we
  761. // can't know the two accesses don't overlap.
  762. }
  763. // These pointers may be based on the memory owned by an indirect global. If
  764. // so, we may be able to handle this. First check to see if the base pointer
  765. // is a direct load from an indirect global.
  766. GV1 = GV2 = nullptr;
  767. if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))
  768. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
  769. if (IndirectGlobals.count(GV))
  770. GV1 = GV;
  771. if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))
  772. if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
  773. if (IndirectGlobals.count(GV))
  774. GV2 = GV;
  775. // These pointers may also be from an allocation for the indirect global. If
  776. // so, also handle them.
  777. if (!GV1)
  778. GV1 = AllocsForIndirectGlobals.lookup(UV1);
  779. if (!GV2)
  780. GV2 = AllocsForIndirectGlobals.lookup(UV2);
  781. // Now that we know whether the two pointers are related to indirect globals,
  782. // use this to disambiguate the pointers. If the pointers are based on
  783. // different indirect globals they cannot alias.
  784. if (GV1 && GV2 && GV1 != GV2)
  785. return NoAlias;
  786. // If one is based on an indirect global and the other isn't, it isn't
  787. // strictly safe but we can fake this result if necessary for performance.
  788. // This does not appear to be a common problem in practice.
  789. if (EnableUnsafeGlobalsModRefAliasResults)
  790. if ((GV1 || GV2) && GV1 != GV2)
  791. return NoAlias;
  792. return AAResultBase::alias(LocA, LocB, AAQI);
  793. }
  794. ModRefInfo GlobalsAAResult::getModRefInfoForArgument(const CallBase *Call,
  795. const GlobalValue *GV,
  796. AAQueryInfo &AAQI) {
  797. if (Call->doesNotAccessMemory())
  798. return ModRefInfo::NoModRef;
  799. ModRefInfo ConservativeResult =
  800. Call->onlyReadsMemory() ? ModRefInfo::Ref : ModRefInfo::ModRef;
  801. // Iterate through all the arguments to the called function. If any argument
  802. // is based on GV, return the conservative result.
  803. for (auto &A : Call->args()) {
  804. SmallVector<const Value*, 4> Objects;
  805. GetUnderlyingObjects(A, Objects, DL);
  806. // All objects must be identified.
  807. if (!all_of(Objects, isIdentifiedObject) &&
  808. // Try ::alias to see if all objects are known not to alias GV.
  809. !all_of(Objects, [&](const Value *V) {
  810. return this->alias(MemoryLocation(V), MemoryLocation(GV), AAQI) ==
  811. NoAlias;
  812. }))
  813. return ConservativeResult;
  814. if (is_contained(Objects, GV))
  815. return ConservativeResult;
  816. }
  817. // We identified all objects in the argument list, and none of them were GV.
  818. return ModRefInfo::NoModRef;
  819. }
  820. ModRefInfo GlobalsAAResult::getModRefInfo(const CallBase *Call,
  821. const MemoryLocation &Loc,
  822. AAQueryInfo &AAQI) {
  823. ModRefInfo Known = ModRefInfo::ModRef;
  824. // If we are asking for mod/ref info of a direct call with a pointer to a
  825. // global we are tracking, return information if we have it.
  826. if (const GlobalValue *GV =
  827. dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL)))
  828. if (GV->hasLocalLinkage())
  829. if (const Function *F = Call->getCalledFunction())
  830. if (NonAddressTakenGlobals.count(GV))
  831. if (const FunctionInfo *FI = getFunctionInfo(F))
  832. Known = unionModRef(FI->getModRefInfoForGlobal(*GV),
  833. getModRefInfoForArgument(Call, GV, AAQI));
  834. if (!isModOrRefSet(Known))
  835. return ModRefInfo::NoModRef; // No need to query other mod/ref analyses
  836. return intersectModRef(Known, AAResultBase::getModRefInfo(Call, Loc, AAQI));
  837. }
  838. GlobalsAAResult::GlobalsAAResult(
  839. const DataLayout &DL,
  840. std::function<const TargetLibraryInfo &(Function &F)> GetTLI)
  841. : AAResultBase(), DL(DL), GetTLI(std::move(GetTLI)) {}
  842. GlobalsAAResult::GlobalsAAResult(GlobalsAAResult &&Arg)
  843. : AAResultBase(std::move(Arg)), DL(Arg.DL), GetTLI(std::move(Arg.GetTLI)),
  844. NonAddressTakenGlobals(std::move(Arg.NonAddressTakenGlobals)),
  845. IndirectGlobals(std::move(Arg.IndirectGlobals)),
  846. AllocsForIndirectGlobals(std::move(Arg.AllocsForIndirectGlobals)),
  847. FunctionInfos(std::move(Arg.FunctionInfos)),
  848. Handles(std::move(Arg.Handles)) {
  849. // Update the parent for each DeletionCallbackHandle.
  850. for (auto &H : Handles) {
  851. assert(H.GAR == &Arg);
  852. H.GAR = this;
  853. }
  854. }
  855. GlobalsAAResult::~GlobalsAAResult() {}
  856. /*static*/ GlobalsAAResult GlobalsAAResult::analyzeModule(
  857. Module &M, std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
  858. CallGraph &CG) {
  859. GlobalsAAResult Result(M.getDataLayout(), GetTLI);
  860. // Discover which functions aren't recursive, to feed into AnalyzeGlobals.
  861. Result.CollectSCCMembership(CG);
  862. // Find non-addr taken globals.
  863. Result.AnalyzeGlobals(M);
  864. // Propagate on CG.
  865. Result.AnalyzeCallGraph(CG, M);
  866. return Result;
  867. }
  868. AnalysisKey GlobalsAA::Key;
  869. GlobalsAAResult GlobalsAA::run(Module &M, ModuleAnalysisManager &AM) {
  870. FunctionAnalysisManager &FAM =
  871. AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  872. auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
  873. return FAM.getResult<TargetLibraryAnalysis>(F);
  874. };
  875. return GlobalsAAResult::analyzeModule(M, GetTLI,
  876. AM.getResult<CallGraphAnalysis>(M));
  877. }
  878. char GlobalsAAWrapperPass::ID = 0;
  879. INITIALIZE_PASS_BEGIN(GlobalsAAWrapperPass, "globals-aa",
  880. "Globals Alias Analysis", false, true)
  881. INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
  882. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  883. INITIALIZE_PASS_END(GlobalsAAWrapperPass, "globals-aa",
  884. "Globals Alias Analysis", false, true)
  885. ModulePass *llvm::createGlobalsAAWrapperPass() {
  886. return new GlobalsAAWrapperPass();
  887. }
  888. GlobalsAAWrapperPass::GlobalsAAWrapperPass() : ModulePass(ID) {
  889. initializeGlobalsAAWrapperPassPass(*PassRegistry::getPassRegistry());
  890. }
  891. bool GlobalsAAWrapperPass::runOnModule(Module &M) {
  892. auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
  893. return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  894. };
  895. Result.reset(new GlobalsAAResult(GlobalsAAResult::analyzeModule(
  896. M, GetTLI, getAnalysis<CallGraphWrapperPass>().getCallGraph())));
  897. return false;
  898. }
  899. bool GlobalsAAWrapperPass::doFinalization(Module &M) {
  900. Result.reset();
  901. return false;
  902. }
  903. void GlobalsAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  904. AU.setPreservesAll();
  905. AU.addRequired<CallGraphWrapperPass>();
  906. AU.addRequired<TargetLibraryInfoWrapperPass>();
  907. }