GlobalsModRef.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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) && isFreeCall(I, &TLI)) {
  333. if (Writers)
  334. Writers->insert(Call->getParent()->getParent());
  335. } else {
  336. return true; // Argument of an unknown call.
  337. }
  338. }
  339. } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
  340. if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
  341. return true; // Allow comparison against null.
  342. } else if (Constant *C = dyn_cast<Constant>(I)) {
  343. // Ignore constants which don't have any live uses.
  344. if (isa<GlobalValue>(C) || C->isConstantUsed())
  345. return true;
  346. } else {
  347. return true;
  348. }
  349. }
  350. return false;
  351. }
  352. /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
  353. /// which holds a pointer type. See if the global always points to non-aliased
  354. /// heap memory: that is, all initializers of the globals are allocations, and
  355. /// those allocations have no use other than initialization of the global.
  356. /// Further, all loads out of GV must directly use the memory, not store the
  357. /// pointer somewhere. If this is true, we consider the memory pointed to by
  358. /// GV to be owned by GV and can disambiguate other pointers from it.
  359. bool GlobalsAAResult::AnalyzeIndirectGlobalMemory(GlobalVariable *GV) {
  360. // Keep track of values related to the allocation of the memory, f.e. the
  361. // value produced by the malloc call and any casts.
  362. std::vector<Value *> AllocRelatedValues;
  363. // If the initializer is a valid pointer, bail.
  364. if (Constant *C = GV->getInitializer())
  365. if (!C->isNullValue())
  366. return false;
  367. // Walk the user list of the global. If we find anything other than a direct
  368. // load or store, bail out.
  369. for (User *U : GV->users()) {
  370. if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
  371. // The pointer loaded from the global can only be used in simple ways:
  372. // we allow addressing of it and loading storing to it. We do *not* allow
  373. // storing the loaded pointer somewhere else or passing to a function.
  374. if (AnalyzeUsesOfPointer(LI))
  375. return false; // Loaded pointer escapes.
  376. // TODO: Could try some IP mod/ref of the loaded pointer.
  377. } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  378. // Storing the global itself.
  379. if (SI->getOperand(0) == GV)
  380. return false;
  381. // If storing the null pointer, ignore it.
  382. if (isa<ConstantPointerNull>(SI->getOperand(0)))
  383. continue;
  384. // Check the value being stored.
  385. Value *Ptr = GetUnderlyingObject(SI->getOperand(0),
  386. GV->getParent()->getDataLayout());
  387. if (!isAllocLikeFn(Ptr, &TLI))
  388. return false; // Too hard to analyze.
  389. // Analyze all uses of the allocation. If any of them are used in a
  390. // non-simple way (e.g. stored to another global) bail out.
  391. if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr,
  392. GV))
  393. return false; // Loaded pointer escapes.
  394. // Remember that this allocation is related to the indirect global.
  395. AllocRelatedValues.push_back(Ptr);
  396. } else {
  397. // Something complex, bail out.
  398. return false;
  399. }
  400. }
  401. // Okay, this is an indirect global. Remember all of the allocations for
  402. // this global in AllocsForIndirectGlobals.
  403. while (!AllocRelatedValues.empty()) {
  404. AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
  405. Handles.emplace_front(*this, AllocRelatedValues.back());
  406. Handles.front().I = Handles.begin();
  407. AllocRelatedValues.pop_back();
  408. }
  409. IndirectGlobals.insert(GV);
  410. Handles.emplace_front(*this, GV);
  411. Handles.front().I = Handles.begin();
  412. return true;
  413. }
  414. void GlobalsAAResult::CollectSCCMembership(CallGraph &CG) {
  415. // We do a bottom-up SCC traversal of the call graph. In other words, we
  416. // visit all callees before callers (leaf-first).
  417. unsigned SCCID = 0;
  418. for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
  419. const std::vector<CallGraphNode *> &SCC = *I;
  420. assert(!SCC.empty() && "SCC with no functions?");
  421. for (auto *CGN : SCC)
  422. if (Function *F = CGN->getFunction())
  423. FunctionToSCCMap[F] = SCCID;
  424. ++SCCID;
  425. }
  426. }
  427. /// AnalyzeCallGraph - At this point, we know the functions where globals are
  428. /// immediately stored to and read from. Propagate this information up the call
  429. /// graph to all callers and compute the mod/ref info for all memory for each
  430. /// function.
  431. void GlobalsAAResult::AnalyzeCallGraph(CallGraph &CG, Module &M) {
  432. // We do a bottom-up SCC traversal of the call graph. In other words, we
  433. // visit all callees before callers (leaf-first).
  434. for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
  435. const std::vector<CallGraphNode *> &SCC = *I;
  436. assert(!SCC.empty() && "SCC with no functions?");
  437. Function *F = SCC[0]->getFunction();
  438. if (!F || !F->isDefinitionExact()) {
  439. // Calls externally or not exact - can't say anything useful. Remove any
  440. // existing function records (may have been created when scanning
  441. // globals).
  442. for (auto *Node : SCC)
  443. FunctionInfos.erase(Node->getFunction());
  444. continue;
  445. }
  446. FunctionInfo &FI = FunctionInfos[F];
  447. Handles.emplace_front(*this, F);
  448. Handles.front().I = Handles.begin();
  449. bool KnowNothing = false;
  450. // Collect the mod/ref properties due to called functions. We only compute
  451. // one mod-ref set.
  452. for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
  453. if (!F) {
  454. KnowNothing = true;
  455. break;
  456. }
  457. if (F->isDeclaration() || F->hasOptNone()) {
  458. // Try to get mod/ref behaviour from function attributes.
  459. if (F->doesNotAccessMemory()) {
  460. // Can't do better than that!
  461. } else if (F->onlyReadsMemory()) {
  462. FI.addModRefInfo(ModRefInfo::Ref);
  463. if (!F->isIntrinsic() && !F->onlyAccessesArgMemory())
  464. // This function might call back into the module and read a global -
  465. // consider every global as possibly being read by this function.
  466. FI.setMayReadAnyGlobal();
  467. } else {
  468. FI.addModRefInfo(ModRefInfo::ModRef);
  469. // Can't say anything useful unless it's an intrinsic - they don't
  470. // read or write global variables of the kind considered here.
  471. KnowNothing = !F->isIntrinsic();
  472. }
  473. continue;
  474. }
  475. for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
  476. CI != E && !KnowNothing; ++CI)
  477. if (Function *Callee = CI->second->getFunction()) {
  478. if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) {
  479. // Propagate function effect up.
  480. FI.addFunctionInfo(*CalleeFI);
  481. } else {
  482. // Can't say anything about it. However, if it is inside our SCC,
  483. // then nothing needs to be done.
  484. CallGraphNode *CalleeNode = CG[Callee];
  485. if (!is_contained(SCC, CalleeNode))
  486. KnowNothing = true;
  487. }
  488. } else {
  489. KnowNothing = true;
  490. }
  491. }
  492. // If we can't say anything useful about this SCC, remove all SCC functions
  493. // from the FunctionInfos map.
  494. if (KnowNothing) {
  495. for (auto *Node : SCC)
  496. FunctionInfos.erase(Node->getFunction());
  497. continue;
  498. }
  499. // Scan the function bodies for explicit loads or stores.
  500. for (auto *Node : SCC) {
  501. if (isModAndRefSet(FI.getModRefInfo()))
  502. break; // The mod/ref lattice saturates here.
  503. // Don't prove any properties based on the implementation of an optnone
  504. // function. Function attributes were already used as a best approximation
  505. // above.
  506. if (Node->getFunction()->hasOptNone())
  507. continue;
  508. for (Instruction &I : instructions(Node->getFunction())) {
  509. if (isModAndRefSet(FI.getModRefInfo()))
  510. break; // The mod/ref lattice saturates here.
  511. // We handle calls specially because the graph-relevant aspects are
  512. // handled above.
  513. if (auto *Call = dyn_cast<CallBase>(&I)) {
  514. if (isAllocationFn(Call, &TLI) || isFreeCall(Call, &TLI)) {
  515. // FIXME: It is completely unclear why this is necessary and not
  516. // handled by the above graph code.
  517. FI.addModRefInfo(ModRefInfo::ModRef);
  518. } else if (Function *Callee = Call->getCalledFunction()) {
  519. // The callgraph doesn't include intrinsic calls.
  520. if (Callee->isIntrinsic()) {
  521. if (isa<DbgInfoIntrinsic>(Call))
  522. // Don't let dbg intrinsics affect alias info.
  523. continue;
  524. FunctionModRefBehavior Behaviour =
  525. AAResultBase::getModRefBehavior(Callee);
  526. FI.addModRefInfo(createModRefInfo(Behaviour));
  527. }
  528. }
  529. continue;
  530. }
  531. // All non-call instructions we use the primary predicates for whether
  532. // they read or write memory.
  533. if (I.mayReadFromMemory())
  534. FI.addModRefInfo(ModRefInfo::Ref);
  535. if (I.mayWriteToMemory())
  536. FI.addModRefInfo(ModRefInfo::Mod);
  537. }
  538. }
  539. if (!isModSet(FI.getModRefInfo()))
  540. ++NumReadMemFunctions;
  541. if (!isModOrRefSet(FI.getModRefInfo()))
  542. ++NumNoMemFunctions;
  543. // Finally, now that we know the full effect on this SCC, clone the
  544. // information to each function in the SCC.
  545. // FI is a reference into FunctionInfos, so copy it now so that it doesn't
  546. // get invalidated if DenseMap decides to re-hash.
  547. FunctionInfo CachedFI = FI;
  548. for (unsigned i = 1, e = SCC.size(); i != e; ++i)
  549. FunctionInfos[SCC[i]->getFunction()] = CachedFI;
  550. }
  551. }
  552. // GV is a non-escaping global. V is a pointer address that has been loaded from.
  553. // If we can prove that V must escape, we can conclude that a load from V cannot
  554. // alias GV.
  555. static bool isNonEscapingGlobalNoAliasWithLoad(const GlobalValue *GV,
  556. const Value *V,
  557. int &Depth,
  558. const DataLayout &DL) {
  559. SmallPtrSet<const Value *, 8> Visited;
  560. SmallVector<const Value *, 8> Inputs;
  561. Visited.insert(V);
  562. Inputs.push_back(V);
  563. do {
  564. const Value *Input = Inputs.pop_back_val();
  565. if (isa<GlobalValue>(Input) || isa<Argument>(Input) || isa<CallInst>(Input) ||
  566. isa<InvokeInst>(Input))
  567. // Arguments to functions or returns from functions are inherently
  568. // escaping, so we can immediately classify those as not aliasing any
  569. // non-addr-taken globals.
  570. //
  571. // (Transitive) loads from a global are also safe - if this aliased
  572. // another global, its address would escape, so no alias.
  573. continue;
  574. // Recurse through a limited number of selects, loads and PHIs. This is an
  575. // arbitrary depth of 4, lower numbers could be used to fix compile time
  576. // issues if needed, but this is generally expected to be only be important
  577. // for small depths.
  578. if (++Depth > 4)
  579. return false;
  580. if (auto *LI = dyn_cast<LoadInst>(Input)) {
  581. Inputs.push_back(GetUnderlyingObject(LI->getPointerOperand(), DL));
  582. continue;
  583. }
  584. if (auto *SI = dyn_cast<SelectInst>(Input)) {
  585. const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL);
  586. const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL);
  587. if (Visited.insert(LHS).second)
  588. Inputs.push_back(LHS);
  589. if (Visited.insert(RHS).second)
  590. Inputs.push_back(RHS);
  591. continue;
  592. }
  593. if (auto *PN = dyn_cast<PHINode>(Input)) {
  594. for (const Value *Op : PN->incoming_values()) {
  595. Op = GetUnderlyingObject(Op, DL);
  596. if (Visited.insert(Op).second)
  597. Inputs.push_back(Op);
  598. }
  599. continue;
  600. }
  601. return false;
  602. } while (!Inputs.empty());
  603. // All inputs were known to be no-alias.
  604. return true;
  605. }
  606. // There are particular cases where we can conclude no-alias between
  607. // a non-addr-taken global and some other underlying object. Specifically,
  608. // a non-addr-taken global is known to not be escaped from any function. It is
  609. // also incorrect for a transformation to introduce an escape of a global in
  610. // a way that is observable when it was not there previously. One function
  611. // being transformed to introduce an escape which could possibly be observed
  612. // (via loading from a global or the return value for example) within another
  613. // function is never safe. If the observation is made through non-atomic
  614. // operations on different threads, it is a data-race and UB. If the
  615. // observation is well defined, by being observed the transformation would have
  616. // changed program behavior by introducing the observed escape, making it an
  617. // invalid transform.
  618. //
  619. // This property does require that transformations which *temporarily* escape
  620. // a global that was not previously escaped, prior to restoring it, cannot rely
  621. // on the results of GMR::alias. This seems a reasonable restriction, although
  622. // currently there is no way to enforce it. There is also no realistic
  623. // optimization pass that would make this mistake. The closest example is
  624. // a transformation pass which does reg2mem of SSA values but stores them into
  625. // global variables temporarily before restoring the global variable's value.
  626. // This could be useful to expose "benign" races for example. However, it seems
  627. // reasonable to require that a pass which introduces escapes of global
  628. // variables in this way to either not trust AA results while the escape is
  629. // active, or to be forced to operate as a module pass that cannot co-exist
  630. // with an alias analysis such as GMR.
  631. bool GlobalsAAResult::isNonEscapingGlobalNoAlias(const GlobalValue *GV,
  632. const Value *V) {
  633. // In order to know that the underlying object cannot alias the
  634. // non-addr-taken global, we must know that it would have to be an escape.
  635. // Thus if the underlying object is a function argument, a load from
  636. // a global, or the return of a function, it cannot alias. We can also
  637. // recurse through PHI nodes and select nodes provided all of their inputs
  638. // resolve to one of these known-escaping roots.
  639. SmallPtrSet<const Value *, 8> Visited;
  640. SmallVector<const Value *, 8> Inputs;
  641. Visited.insert(V);
  642. Inputs.push_back(V);
  643. int Depth = 0;
  644. do {
  645. const Value *Input = Inputs.pop_back_val();
  646. if (auto *InputGV = dyn_cast<GlobalValue>(Input)) {
  647. // If one input is the very global we're querying against, then we can't
  648. // conclude no-alias.
  649. if (InputGV == GV)
  650. return false;
  651. // Distinct GlobalVariables never alias, unless overriden or zero-sized.
  652. // FIXME: The condition can be refined, but be conservative for now.
  653. auto *GVar = dyn_cast<GlobalVariable>(GV);
  654. auto *InputGVar = dyn_cast<GlobalVariable>(InputGV);
  655. if (GVar && InputGVar &&
  656. !GVar->isDeclaration() && !InputGVar->isDeclaration() &&
  657. !GVar->isInterposable() && !InputGVar->isInterposable()) {
  658. Type *GVType = GVar->getInitializer()->getType();
  659. Type *InputGVType = InputGVar->getInitializer()->getType();
  660. if (GVType->isSized() && InputGVType->isSized() &&
  661. (DL.getTypeAllocSize(GVType) > 0) &&
  662. (DL.getTypeAllocSize(InputGVType) > 0))
  663. continue;
  664. }
  665. // Conservatively return false, even though we could be smarter
  666. // (e.g. look through GlobalAliases).
  667. return false;
  668. }
  669. if (isa<Argument>(Input) || isa<CallInst>(Input) ||
  670. isa<InvokeInst>(Input)) {
  671. // Arguments to functions or returns from functions are inherently
  672. // escaping, so we can immediately classify those as not aliasing any
  673. // non-addr-taken globals.
  674. continue;
  675. }
  676. // Recurse through a limited number of selects, loads and PHIs. This is an
  677. // arbitrary depth of 4, lower numbers could be used to fix compile time
  678. // issues if needed, but this is generally expected to be only be important
  679. // for small depths.
  680. if (++Depth > 4)
  681. return false;
  682. if (auto *LI = dyn_cast<LoadInst>(Input)) {
  683. // A pointer loaded from a global would have been captured, and we know
  684. // that the global is non-escaping, so no alias.
  685. const Value *Ptr = GetUnderlyingObject(LI->getPointerOperand(), DL);
  686. if (isNonEscapingGlobalNoAliasWithLoad(GV, Ptr, Depth, DL))
  687. // The load does not alias with GV.
  688. continue;
  689. // Otherwise, a load could come from anywhere, so bail.
  690. return false;
  691. }
  692. if (auto *SI = dyn_cast<SelectInst>(Input)) {
  693. const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL);
  694. const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL);
  695. if (Visited.insert(LHS).second)
  696. Inputs.push_back(LHS);
  697. if (Visited.insert(RHS).second)
  698. Inputs.push_back(RHS);
  699. continue;
  700. }
  701. if (auto *PN = dyn_cast<PHINode>(Input)) {
  702. for (const Value *Op : PN->incoming_values()) {
  703. Op = GetUnderlyingObject(Op, DL);
  704. if (Visited.insert(Op).second)
  705. Inputs.push_back(Op);
  706. }
  707. continue;
  708. }
  709. // FIXME: It would be good to handle other obvious no-alias cases here, but
  710. // it isn't clear how to do so reasonably without building a small version
  711. // of BasicAA into this code. We could recurse into AAResultBase::alias
  712. // here but that seems likely to go poorly as we're inside the
  713. // implementation of such a query. Until then, just conservatively return
  714. // false.
  715. return false;
  716. } while (!Inputs.empty());
  717. // If all the inputs to V were definitively no-alias, then V is no-alias.
  718. return true;
  719. }
  720. /// alias - If one of the pointers is to a global that we are tracking, and the
  721. /// other is some random pointer, we know there cannot be an alias, because the
  722. /// address of the global isn't taken.
  723. AliasResult GlobalsAAResult::alias(const MemoryLocation &LocA,
  724. const MemoryLocation &LocB,
  725. AAQueryInfo &AAQI) {
  726. // Get the base object these pointers point to.
  727. const Value *UV1 = GetUnderlyingObject(LocA.Ptr, DL);
  728. const Value *UV2 = GetUnderlyingObject(LocB.Ptr, DL);
  729. // If either of the underlying values is a global, they may be non-addr-taken
  730. // globals, which we can answer queries about.
  731. const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
  732. const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
  733. if (GV1 || GV2) {
  734. // If the global's address is taken, pretend we don't know it's a pointer to
  735. // the global.
  736. if (GV1 && !NonAddressTakenGlobals.count(GV1))
  737. GV1 = nullptr;
  738. if (GV2 && !NonAddressTakenGlobals.count(GV2))
  739. GV2 = nullptr;
  740. // If the two pointers are derived from two different non-addr-taken
  741. // globals we know these can't alias.
  742. if (GV1 && GV2 && GV1 != GV2)
  743. return NoAlias;
  744. // If one is and the other isn't, it isn't strictly safe but we can fake
  745. // this result if necessary for performance. This does not appear to be
  746. // a common problem in practice.
  747. if (EnableUnsafeGlobalsModRefAliasResults)
  748. if ((GV1 || GV2) && GV1 != GV2)
  749. return NoAlias;
  750. // Check for a special case where a non-escaping global can be used to
  751. // conclude no-alias.
  752. if ((GV1 || GV2) && GV1 != GV2) {
  753. const GlobalValue *GV = GV1 ? GV1 : GV2;
  754. const Value *UV = GV1 ? UV2 : UV1;
  755. if (isNonEscapingGlobalNoAlias(GV, UV))
  756. return NoAlias;
  757. }
  758. // Otherwise if they are both derived from the same addr-taken global, we
  759. // can't know the two accesses don't overlap.
  760. }
  761. // These pointers may be based on the memory owned by an indirect global. If
  762. // so, we may be able to handle this. First check to see if the base pointer
  763. // is a direct load from an indirect global.
  764. GV1 = GV2 = nullptr;
  765. if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))
  766. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
  767. if (IndirectGlobals.count(GV))
  768. GV1 = GV;
  769. if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))
  770. if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
  771. if (IndirectGlobals.count(GV))
  772. GV2 = GV;
  773. // These pointers may also be from an allocation for the indirect global. If
  774. // so, also handle them.
  775. if (!GV1)
  776. GV1 = AllocsForIndirectGlobals.lookup(UV1);
  777. if (!GV2)
  778. GV2 = AllocsForIndirectGlobals.lookup(UV2);
  779. // Now that we know whether the two pointers are related to indirect globals,
  780. // use this to disambiguate the pointers. If the pointers are based on
  781. // different indirect globals they cannot alias.
  782. if (GV1 && GV2 && GV1 != GV2)
  783. return NoAlias;
  784. // If one is based on an indirect global and the other isn't, it isn't
  785. // strictly safe but we can fake this result if necessary for performance.
  786. // This does not appear to be a common problem in practice.
  787. if (EnableUnsafeGlobalsModRefAliasResults)
  788. if ((GV1 || GV2) && GV1 != GV2)
  789. return NoAlias;
  790. return AAResultBase::alias(LocA, LocB, AAQI);
  791. }
  792. ModRefInfo GlobalsAAResult::getModRefInfoForArgument(const CallBase *Call,
  793. const GlobalValue *GV,
  794. AAQueryInfo &AAQI) {
  795. if (Call->doesNotAccessMemory())
  796. return ModRefInfo::NoModRef;
  797. ModRefInfo ConservativeResult =
  798. Call->onlyReadsMemory() ? ModRefInfo::Ref : ModRefInfo::ModRef;
  799. // Iterate through all the arguments to the called function. If any argument
  800. // is based on GV, return the conservative result.
  801. for (auto &A : Call->args()) {
  802. SmallVector<const Value*, 4> Objects;
  803. GetUnderlyingObjects(A, Objects, DL);
  804. // All objects must be identified.
  805. if (!all_of(Objects, isIdentifiedObject) &&
  806. // Try ::alias to see if all objects are known not to alias GV.
  807. !all_of(Objects, [&](const Value *V) {
  808. return this->alias(MemoryLocation(V), MemoryLocation(GV), AAQI) ==
  809. NoAlias;
  810. }))
  811. return ConservativeResult;
  812. if (is_contained(Objects, GV))
  813. return ConservativeResult;
  814. }
  815. // We identified all objects in the argument list, and none of them were GV.
  816. return ModRefInfo::NoModRef;
  817. }
  818. ModRefInfo GlobalsAAResult::getModRefInfo(const CallBase *Call,
  819. const MemoryLocation &Loc,
  820. AAQueryInfo &AAQI) {
  821. ModRefInfo Known = ModRefInfo::ModRef;
  822. // If we are asking for mod/ref info of a direct call with a pointer to a
  823. // global we are tracking, return information if we have it.
  824. if (const GlobalValue *GV =
  825. dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL)))
  826. if (GV->hasLocalLinkage())
  827. if (const Function *F = Call->getCalledFunction())
  828. if (NonAddressTakenGlobals.count(GV))
  829. if (const FunctionInfo *FI = getFunctionInfo(F))
  830. Known = unionModRef(FI->getModRefInfoForGlobal(*GV),
  831. getModRefInfoForArgument(Call, GV, AAQI));
  832. if (!isModOrRefSet(Known))
  833. return ModRefInfo::NoModRef; // No need to query other mod/ref analyses
  834. return intersectModRef(Known, AAResultBase::getModRefInfo(Call, Loc, AAQI));
  835. }
  836. GlobalsAAResult::GlobalsAAResult(const DataLayout &DL,
  837. const TargetLibraryInfo &TLI)
  838. : AAResultBase(), DL(DL), TLI(TLI) {}
  839. GlobalsAAResult::GlobalsAAResult(GlobalsAAResult &&Arg)
  840. : AAResultBase(std::move(Arg)), DL(Arg.DL), TLI(Arg.TLI),
  841. NonAddressTakenGlobals(std::move(Arg.NonAddressTakenGlobals)),
  842. IndirectGlobals(std::move(Arg.IndirectGlobals)),
  843. AllocsForIndirectGlobals(std::move(Arg.AllocsForIndirectGlobals)),
  844. FunctionInfos(std::move(Arg.FunctionInfos)),
  845. Handles(std::move(Arg.Handles)) {
  846. // Update the parent for each DeletionCallbackHandle.
  847. for (auto &H : Handles) {
  848. assert(H.GAR == &Arg);
  849. H.GAR = this;
  850. }
  851. }
  852. GlobalsAAResult::~GlobalsAAResult() {}
  853. /*static*/ GlobalsAAResult
  854. GlobalsAAResult::analyzeModule(Module &M, const TargetLibraryInfo &TLI,
  855. CallGraph &CG) {
  856. GlobalsAAResult Result(M.getDataLayout(), TLI);
  857. // Discover which functions aren't recursive, to feed into AnalyzeGlobals.
  858. Result.CollectSCCMembership(CG);
  859. // Find non-addr taken globals.
  860. Result.AnalyzeGlobals(M);
  861. // Propagate on CG.
  862. Result.AnalyzeCallGraph(CG, M);
  863. return Result;
  864. }
  865. AnalysisKey GlobalsAA::Key;
  866. GlobalsAAResult GlobalsAA::run(Module &M, ModuleAnalysisManager &AM) {
  867. return GlobalsAAResult::analyzeModule(M,
  868. AM.getResult<TargetLibraryAnalysis>(M),
  869. AM.getResult<CallGraphAnalysis>(M));
  870. }
  871. char GlobalsAAWrapperPass::ID = 0;
  872. INITIALIZE_PASS_BEGIN(GlobalsAAWrapperPass, "globals-aa",
  873. "Globals Alias Analysis", false, true)
  874. INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
  875. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  876. INITIALIZE_PASS_END(GlobalsAAWrapperPass, "globals-aa",
  877. "Globals Alias Analysis", false, true)
  878. ModulePass *llvm::createGlobalsAAWrapperPass() {
  879. return new GlobalsAAWrapperPass();
  880. }
  881. GlobalsAAWrapperPass::GlobalsAAWrapperPass() : ModulePass(ID) {
  882. initializeGlobalsAAWrapperPassPass(*PassRegistry::getPassRegistry());
  883. }
  884. bool GlobalsAAWrapperPass::runOnModule(Module &M) {
  885. Result.reset(new GlobalsAAResult(GlobalsAAResult::analyzeModule(
  886. M, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
  887. getAnalysis<CallGraphWrapperPass>().getCallGraph())));
  888. return false;
  889. }
  890. bool GlobalsAAWrapperPass::doFinalization(Module &M) {
  891. Result.reset();
  892. return false;
  893. }
  894. void GlobalsAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  895. AU.setPreservesAll();
  896. AU.addRequired<CallGraphWrapperPass>();
  897. AU.addRequired<TargetLibraryInfoWrapperPass>();
  898. }