GlobalStatus.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 constants cannot be cyclic, so this test is pretty easy to
  38. /// implement recursively.
  39. ///
  40. bool llvm::isSafeToDestroyConstant(const Constant *C) {
  41. if (isa<GlobalValue>(C))
  42. return false;
  43. if (isa<ConstantData>(C))
  44. return false;
  45. for (const User *U : C->users())
  46. if (const Constant *CU = dyn_cast<Constant>(U)) {
  47. if (!isSafeToDestroyConstant(CU))
  48. return false;
  49. } else
  50. return false;
  51. return true;
  52. }
  53. static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
  54. SmallPtrSetImpl<const Value *> &VisitedUsers) {
  55. if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
  56. if (GV->isExternallyInitialized())
  57. GS.StoredType = GlobalStatus::StoredOnce;
  58. for (const Use &U : V->uses()) {
  59. const User *UR = U.getUser();
  60. if (const Constant *C = dyn_cast<Constant>(UR)) {
  61. const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
  62. if (CE && isa<PointerType>(CE->getType())) {
  63. // Recursively analyze pointer-typed constant expressions.
  64. // FIXME: Do we need to add constexpr selects to VisitedUsers?
  65. if (analyzeGlobalAux(CE, GS, VisitedUsers))
  66. return true;
  67. } else {
  68. // Ignore dead constant users.
  69. if (!isSafeToDestroyConstant(C))
  70. return true;
  71. }
  72. } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
  73. if (!GS.HasMultipleAccessingFunctions) {
  74. const Function *F = I->getParent()->getParent();
  75. if (!GS.AccessingFunction)
  76. GS.AccessingFunction = F;
  77. else if (GS.AccessingFunction != F)
  78. GS.HasMultipleAccessingFunctions = true;
  79. }
  80. if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
  81. GS.IsLoaded = true;
  82. // Don't hack on volatile loads.
  83. if (LI->isVolatile())
  84. return true;
  85. GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
  86. } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
  87. // Don't allow a store OF the address, only stores TO the address.
  88. if (SI->getOperand(0) == V)
  89. return true;
  90. // Don't hack on volatile stores.
  91. if (SI->isVolatile())
  92. return true;
  93. GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
  94. // If this is a direct store to the global (i.e., the global is a scalar
  95. // value, not an aggregate), keep more specific information about
  96. // stores.
  97. if (GS.StoredType != GlobalStatus::Stored) {
  98. const Value *Ptr = SI->getPointerOperand()->stripPointerCasts();
  99. if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
  100. Value *StoredVal = SI->getOperand(0);
  101. if (Constant *C = dyn_cast<Constant>(StoredVal)) {
  102. if (C->isThreadDependent()) {
  103. // The stored value changes between threads; don't track it.
  104. return true;
  105. }
  106. }
  107. if (GV->hasInitializer() && StoredVal == GV->getInitializer()) {
  108. if (GS.StoredType < GlobalStatus::InitializerStored)
  109. GS.StoredType = GlobalStatus::InitializerStored;
  110. } else if (isa<LoadInst>(StoredVal) &&
  111. cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
  112. if (GS.StoredType < GlobalStatus::InitializerStored)
  113. GS.StoredType = GlobalStatus::InitializerStored;
  114. } else if (GS.StoredType < GlobalStatus::StoredOnce) {
  115. GS.StoredType = GlobalStatus::StoredOnce;
  116. GS.StoredOnceStore = SI;
  117. } else if (GS.StoredType == GlobalStatus::StoredOnce &&
  118. GS.getStoredOnceValue() == StoredVal) {
  119. // noop.
  120. } else {
  121. GS.StoredType = GlobalStatus::Stored;
  122. }
  123. } else {
  124. GS.StoredType = GlobalStatus::Stored;
  125. }
  126. }
  127. } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I) ||
  128. isa<AddrSpaceCastInst>(I)) {
  129. // Skip over bitcasts and GEPs; we don't care about the type or offset
  130. // of the pointer.
  131. if (analyzeGlobalAux(I, GS, VisitedUsers))
  132. return true;
  133. } else if (isa<SelectInst>(I) || isa<PHINode>(I)) {
  134. // Look through selects and PHIs to find if the pointer is
  135. // conditionally accessed. Make sure we only visit an instruction
  136. // once; otherwise, we can get infinite recursion or exponential
  137. // compile time.
  138. if (VisitedUsers.insert(I).second)
  139. if (analyzeGlobalAux(I, GS, VisitedUsers))
  140. return true;
  141. } else if (isa<CmpInst>(I)) {
  142. GS.IsCompared = true;
  143. } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
  144. if (MTI->isVolatile())
  145. return true;
  146. if (MTI->getArgOperand(0) == V)
  147. GS.StoredType = GlobalStatus::Stored;
  148. if (MTI->getArgOperand(1) == V)
  149. GS.IsLoaded = true;
  150. } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
  151. assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
  152. if (MSI->isVolatile())
  153. return true;
  154. GS.StoredType = GlobalStatus::Stored;
  155. } else if (const auto *CB = dyn_cast<CallBase>(I)) {
  156. if (!CB->isCallee(&U))
  157. return true;
  158. GS.IsLoaded = true;
  159. } else {
  160. return true; // Any other non-load instruction might take address!
  161. }
  162. } else {
  163. // Otherwise must be some other user.
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. GlobalStatus::GlobalStatus() = default;
  170. bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
  171. SmallPtrSet<const Value *, 16> VisitedUsers;
  172. return analyzeGlobalAux(V, GS, VisitedUsers);
  173. }