GlobalStatus.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 ConstantExpr *CE = dyn_cast<ConstantExpr>(UR)) {
  61. GS.HasNonInstructionUser = true;
  62. // If the result of the constantexpr isn't pointer type, then we won't
  63. // know to expect it in various places. Just reject early.
  64. if (!isa<PointerType>(CE->getType()))
  65. return true;
  66. // FIXME: Do we need to add constexpr selects to VisitedUsers?
  67. if (analyzeGlobalAux(CE, GS, VisitedUsers))
  68. return true;
  69. } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
  70. if (!GS.HasMultipleAccessingFunctions) {
  71. const Function *F = I->getParent()->getParent();
  72. if (!GS.AccessingFunction)
  73. GS.AccessingFunction = F;
  74. else if (GS.AccessingFunction != F)
  75. GS.HasMultipleAccessingFunctions = true;
  76. }
  77. if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
  78. GS.IsLoaded = true;
  79. // Don't hack on volatile loads.
  80. if (LI->isVolatile())
  81. return true;
  82. GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
  83. } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
  84. // Don't allow a store OF the address, only stores TO the address.
  85. if (SI->getOperand(0) == V)
  86. return true;
  87. // Don't hack on volatile stores.
  88. if (SI->isVolatile())
  89. return true;
  90. GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
  91. // If this is a direct store to the global (i.e., the global is a scalar
  92. // value, not an aggregate), keep more specific information about
  93. // stores.
  94. if (GS.StoredType != GlobalStatus::Stored) {
  95. if (const GlobalVariable *GV =
  96. dyn_cast<GlobalVariable>(SI->getOperand(1))) {
  97. Value *StoredVal = SI->getOperand(0);
  98. if (Constant *C = dyn_cast<Constant>(StoredVal)) {
  99. if (C->isThreadDependent()) {
  100. // The stored value changes between threads; don't track it.
  101. return true;
  102. }
  103. }
  104. if (GV->hasInitializer() && StoredVal == GV->getInitializer()) {
  105. if (GS.StoredType < GlobalStatus::InitializerStored)
  106. GS.StoredType = GlobalStatus::InitializerStored;
  107. } else if (isa<LoadInst>(StoredVal) &&
  108. cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
  109. if (GS.StoredType < GlobalStatus::InitializerStored)
  110. GS.StoredType = GlobalStatus::InitializerStored;
  111. } else if (GS.StoredType < GlobalStatus::StoredOnce) {
  112. GS.StoredType = GlobalStatus::StoredOnce;
  113. GS.StoredOnceValue = StoredVal;
  114. } else if (GS.StoredType == GlobalStatus::StoredOnce &&
  115. GS.StoredOnceValue == StoredVal) {
  116. // noop.
  117. } else {
  118. GS.StoredType = GlobalStatus::Stored;
  119. }
  120. } else {
  121. GS.StoredType = GlobalStatus::Stored;
  122. }
  123. }
  124. } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I) ||
  125. isa<AddrSpaceCastInst>(I)) {
  126. // Skip over bitcasts and GEPs; we don't care about the type or offset
  127. // of the pointer.
  128. if (analyzeGlobalAux(I, GS, VisitedUsers))
  129. return true;
  130. } else if (isa<SelectInst>(I) || isa<PHINode>(I)) {
  131. // Look through selects and PHIs to find if the pointer is
  132. // conditionally accessed. Make sure we only visit an instruction
  133. // once; otherwise, we can get infinite recursion or exponential
  134. // compile time.
  135. if (VisitedUsers.insert(I).second)
  136. if (analyzeGlobalAux(I, GS, VisitedUsers))
  137. return true;
  138. } else if (isa<CmpInst>(I)) {
  139. GS.IsCompared = true;
  140. } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
  141. if (MTI->isVolatile())
  142. return true;
  143. if (MTI->getArgOperand(0) == V)
  144. GS.StoredType = GlobalStatus::Stored;
  145. if (MTI->getArgOperand(1) == V)
  146. GS.IsLoaded = true;
  147. } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
  148. assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
  149. if (MSI->isVolatile())
  150. return true;
  151. GS.StoredType = GlobalStatus::Stored;
  152. } else if (const auto *CB = dyn_cast<CallBase>(I)) {
  153. if (!CB->isCallee(&U))
  154. return true;
  155. GS.IsLoaded = true;
  156. } else {
  157. return true; // Any other non-load instruction might take address!
  158. }
  159. } else if (const Constant *C = dyn_cast<Constant>(UR)) {
  160. GS.HasNonInstructionUser = true;
  161. // We might have a dead and dangling constant hanging off of here.
  162. if (!isSafeToDestroyConstant(C))
  163. return true;
  164. } else {
  165. GS.HasNonInstructionUser = true;
  166. // Otherwise must be some other user.
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. GlobalStatus::GlobalStatus() = default;
  173. bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
  174. SmallPtrSet<const Value *, 16> VisitedUsers;
  175. return analyzeGlobalAux(V, GS, VisitedUsers);
  176. }