GlobalStatus.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //===-- GlobalStatus.cpp - Compute status info 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. #include "llvm/Transforms/Utils/GlobalStatus.h"
  9. #include "llvm/ADT/SmallPtrSet.h"
  10. #include "llvm/IR/BasicBlock.h"
  11. #include "llvm/IR/Constant.h"
  12. #include "llvm/IR/Constants.h"
  13. #include "llvm/IR/GlobalValue.h"
  14. #include "llvm/IR/GlobalVariable.h"
  15. #include "llvm/IR/InstrTypes.h"
  16. #include "llvm/IR/Instruction.h"
  17. #include "llvm/IR/Instructions.h"
  18. #include "llvm/IR/IntrinsicInst.h"
  19. #include "llvm/IR/Use.h"
  20. #include "llvm/IR/User.h"
  21. #include "llvm/IR/Value.h"
  22. #include "llvm/Support/AtomicOrdering.h"
  23. #include "llvm/Support/Casting.h"
  24. #include <algorithm>
  25. #include <cassert>
  26. using namespace llvm;
  27. /// Return the stronger of the two ordering. If the two orderings are acquire
  28. /// and release, then return AcquireRelease.
  29. ///
  30. static AtomicOrdering strongerOrdering(AtomicOrdering X, AtomicOrdering Y) {
  31. if ((X == AtomicOrdering::Acquire && Y == AtomicOrdering::Release) ||
  32. (Y == AtomicOrdering::Acquire && X == AtomicOrdering::Release))
  33. return AtomicOrdering::AcquireRelease;
  34. return (AtomicOrdering)std::max((unsigned)X, (unsigned)Y);
  35. }
  36. /// It is safe to destroy a constant iff it is only used by constants itself.
  37. /// Note that while constants cannot be cyclic, they can be tree-like, so we
  38. /// should keep a visited set to avoid exponential runtime.
  39. bool llvm::isSafeToDestroyConstant(const Constant *C) {
  40. SmallVector<const Constant *, 8> Worklist;
  41. SmallPtrSet<const Constant *, 8> Visited;
  42. Worklist.push_back(C);
  43. while (!Worklist.empty()) {
  44. const Constant *C = Worklist.pop_back_val();
  45. if (!Visited.insert(C).second)
  46. continue;
  47. if (isa<GlobalValue>(C) || isa<ConstantData>(C))
  48. return false;
  49. for (const User *U : C->users()) {
  50. if (const Constant *CU = dyn_cast<Constant>(U))
  51. Worklist.push_back(CU);
  52. else
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
  59. SmallPtrSetImpl<const Value *> &VisitedUsers) {
  60. if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
  61. if (GV->isExternallyInitialized())
  62. GS.StoredType = GlobalStatus::StoredOnce;
  63. for (const Use &U : V->uses()) {
  64. const User *UR = U.getUser();
  65. if (const Constant *C = dyn_cast<Constant>(UR)) {
  66. const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
  67. if (CE && isa<PointerType>(CE->getType())) {
  68. // Recursively analyze pointer-typed constant expressions.
  69. // FIXME: Do we need to add constexpr selects to VisitedUsers?
  70. if (analyzeGlobalAux(CE, GS, VisitedUsers))
  71. return true;
  72. } else {
  73. // Ignore dead constant users.
  74. if (!isSafeToDestroyConstant(C))
  75. return true;
  76. }
  77. } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
  78. if (!GS.HasMultipleAccessingFunctions) {
  79. const Function *F = I->getParent()->getParent();
  80. if (!GS.AccessingFunction)
  81. GS.AccessingFunction = F;
  82. else if (GS.AccessingFunction != F)
  83. GS.HasMultipleAccessingFunctions = true;
  84. }
  85. if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
  86. GS.IsLoaded = true;
  87. // Don't hack on volatile loads.
  88. if (LI->isVolatile())
  89. return true;
  90. GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
  91. } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
  92. // Don't allow a store OF the address, only stores TO the address.
  93. if (SI->getOperand(0) == V)
  94. return true;
  95. // Don't hack on volatile stores.
  96. if (SI->isVolatile())
  97. return true;
  98. ++GS.NumStores;
  99. GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
  100. // If this is a direct store to the global (i.e., the global is a scalar
  101. // value, not an aggregate), keep more specific information about
  102. // stores.
  103. if (GS.StoredType != GlobalStatus::Stored) {
  104. const Value *Ptr = SI->getPointerOperand()->stripPointerCasts();
  105. if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
  106. Value *StoredVal = SI->getOperand(0);
  107. if (Constant *C = dyn_cast<Constant>(StoredVal)) {
  108. if (C->isThreadDependent()) {
  109. // The stored value changes between threads; don't track it.
  110. return true;
  111. }
  112. }
  113. if (GV->hasInitializer() && StoredVal == GV->getInitializer()) {
  114. if (GS.StoredType < GlobalStatus::InitializerStored)
  115. GS.StoredType = GlobalStatus::InitializerStored;
  116. } else if (isa<LoadInst>(StoredVal) &&
  117. cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
  118. if (GS.StoredType < GlobalStatus::InitializerStored)
  119. GS.StoredType = GlobalStatus::InitializerStored;
  120. } else if (GS.StoredType < GlobalStatus::StoredOnce) {
  121. GS.StoredType = GlobalStatus::StoredOnce;
  122. GS.StoredOnceStore = SI;
  123. } else if (GS.StoredType == GlobalStatus::StoredOnce &&
  124. GS.getStoredOnceValue() == StoredVal) {
  125. // noop.
  126. } else {
  127. GS.StoredType = GlobalStatus::Stored;
  128. }
  129. } else {
  130. GS.StoredType = GlobalStatus::Stored;
  131. }
  132. }
  133. } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I) ||
  134. isa<AddrSpaceCastInst>(I)) {
  135. // Skip over bitcasts and GEPs; we don't care about the type or offset
  136. // of the pointer.
  137. if (analyzeGlobalAux(I, GS, VisitedUsers))
  138. return true;
  139. } else if (isa<SelectInst>(I) || isa<PHINode>(I)) {
  140. // Look through selects and PHIs to find if the pointer is
  141. // conditionally accessed. Make sure we only visit an instruction
  142. // once; otherwise, we can get infinite recursion or exponential
  143. // compile time.
  144. if (VisitedUsers.insert(I).second)
  145. if (analyzeGlobalAux(I, GS, VisitedUsers))
  146. return true;
  147. } else if (isa<CmpInst>(I)) {
  148. GS.IsCompared = true;
  149. } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
  150. if (MTI->isVolatile())
  151. return true;
  152. if (MTI->getArgOperand(0) == V)
  153. GS.StoredType = GlobalStatus::Stored;
  154. if (MTI->getArgOperand(1) == V)
  155. GS.IsLoaded = true;
  156. } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
  157. assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
  158. if (MSI->isVolatile())
  159. return true;
  160. GS.StoredType = GlobalStatus::Stored;
  161. } else if (const auto *CB = dyn_cast<CallBase>(I)) {
  162. if (!CB->isCallee(&U))
  163. return true;
  164. GS.IsLoaded = true;
  165. } else {
  166. return true; // Any other non-load instruction might take address!
  167. }
  168. } else {
  169. // Otherwise must be some other user.
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. GlobalStatus::GlobalStatus() = default;
  176. bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
  177. SmallPtrSet<const Value *, 16> VisitedUsers;
  178. return analyzeGlobalAux(V, GS, VisitedUsers);
  179. }