GlobalStatus.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //===-- GlobalStatus.cpp - Compute status info for globals -----------------==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/ADT/SmallPtrSet.h"
  10. #include "llvm/IR/BasicBlock.h"
  11. #include "llvm/IR/CallSite.h"
  12. #include "llvm/IR/GlobalVariable.h"
  13. #include "llvm/IR/IntrinsicInst.h"
  14. #include "llvm/Transforms/Utils/GlobalStatus.h"
  15. using namespace llvm;
  16. /// Return the stronger of the two ordering. If the two orderings are acquire
  17. /// and release, then return AcquireRelease.
  18. ///
  19. static AtomicOrdering strongerOrdering(AtomicOrdering X, AtomicOrdering Y) {
  20. if (X == Acquire && Y == Release)
  21. return AcquireRelease;
  22. if (Y == Acquire && X == Release)
  23. return AcquireRelease;
  24. return (AtomicOrdering)std::max(X, Y);
  25. }
  26. /// It is safe to destroy a constant iff it is only used by constants itself.
  27. /// Note that constants cannot be cyclic, so this test is pretty easy to
  28. /// implement recursively.
  29. ///
  30. bool llvm::isSafeToDestroyConstant(const Constant *C) {
  31. if (isa<GlobalValue>(C))
  32. return false;
  33. if (isa<ConstantInt>(C) || isa<ConstantFP>(C))
  34. return false;
  35. for (const User *U : C->users())
  36. if (const Constant *CU = dyn_cast<Constant>(U)) {
  37. if (!isSafeToDestroyConstant(CU))
  38. return false;
  39. } else
  40. return false;
  41. return true;
  42. }
  43. static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
  44. SmallPtrSetImpl<const PHINode *> &PhiUsers) {
  45. for (const Use &U : V->uses()) {
  46. const User *UR = U.getUser();
  47. if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(UR)) {
  48. GS.HasNonInstructionUser = true;
  49. // If the result of the constantexpr isn't pointer type, then we won't
  50. // know to expect it in various places. Just reject early.
  51. if (!isa<PointerType>(CE->getType()))
  52. return true;
  53. if (analyzeGlobalAux(CE, GS, PhiUsers))
  54. return true;
  55. } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
  56. if (!GS.HasMultipleAccessingFunctions) {
  57. const Function *F = I->getParent()->getParent();
  58. if (!GS.AccessingFunction)
  59. GS.AccessingFunction = F;
  60. else if (GS.AccessingFunction != F)
  61. GS.HasMultipleAccessingFunctions = true;
  62. }
  63. if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
  64. GS.IsLoaded = true;
  65. // Don't hack on volatile loads.
  66. if (LI->isVolatile())
  67. return true;
  68. GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
  69. } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
  70. // Don't allow a store OF the address, only stores TO the address.
  71. if (SI->getOperand(0) == V)
  72. return true;
  73. // Don't hack on volatile stores.
  74. if (SI->isVolatile())
  75. return true;
  76. GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
  77. // If this is a direct store to the global (i.e., the global is a scalar
  78. // value, not an aggregate), keep more specific information about
  79. // stores.
  80. if (GS.StoredType != GlobalStatus::Stored) {
  81. if (const GlobalVariable *GV =
  82. dyn_cast<GlobalVariable>(SI->getOperand(1))) {
  83. Value *StoredVal = SI->getOperand(0);
  84. if (Constant *C = dyn_cast<Constant>(StoredVal)) {
  85. if (C->isThreadDependent()) {
  86. // The stored value changes between threads; don't track it.
  87. return true;
  88. }
  89. }
  90. if (StoredVal == GV->getInitializer()) {
  91. if (GS.StoredType < GlobalStatus::InitializerStored)
  92. GS.StoredType = GlobalStatus::InitializerStored;
  93. } else if (isa<LoadInst>(StoredVal) &&
  94. cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
  95. if (GS.StoredType < GlobalStatus::InitializerStored)
  96. GS.StoredType = GlobalStatus::InitializerStored;
  97. } else if (GS.StoredType < GlobalStatus::StoredOnce) {
  98. GS.StoredType = GlobalStatus::StoredOnce;
  99. GS.StoredOnceValue = StoredVal;
  100. } else if (GS.StoredType == GlobalStatus::StoredOnce &&
  101. GS.StoredOnceValue == StoredVal) {
  102. // noop.
  103. } else {
  104. GS.StoredType = GlobalStatus::Stored;
  105. }
  106. } else {
  107. GS.StoredType = GlobalStatus::Stored;
  108. }
  109. }
  110. } else if (isa<BitCastInst>(I)) {
  111. if (analyzeGlobalAux(I, GS, PhiUsers))
  112. return true;
  113. } else if (isa<GetElementPtrInst>(I)) {
  114. if (analyzeGlobalAux(I, GS, PhiUsers))
  115. return true;
  116. } else if (isa<SelectInst>(I)) {
  117. if (analyzeGlobalAux(I, GS, PhiUsers))
  118. return true;
  119. } else if (const PHINode *PN = dyn_cast<PHINode>(I)) {
  120. // PHI nodes we can check just like select or GEP instructions, but we
  121. // have to be careful about infinite recursion.
  122. if (PhiUsers.insert(PN).second) // Not already visited.
  123. if (analyzeGlobalAux(I, GS, PhiUsers))
  124. return true;
  125. } else if (isa<CmpInst>(I)) {
  126. GS.IsCompared = true;
  127. } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
  128. if (MTI->isVolatile())
  129. return true;
  130. if (MTI->getArgOperand(0) == V)
  131. GS.StoredType = GlobalStatus::Stored;
  132. if (MTI->getArgOperand(1) == V)
  133. GS.IsLoaded = true;
  134. } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
  135. assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
  136. if (MSI->isVolatile())
  137. return true;
  138. GS.StoredType = GlobalStatus::Stored;
  139. } else if (auto C = ImmutableCallSite(I)) {
  140. if (!C.isCallee(&U))
  141. return true;
  142. GS.IsLoaded = true;
  143. } else {
  144. return true; // Any other non-load instruction might take address!
  145. }
  146. } else if (const Constant *C = dyn_cast<Constant>(UR)) {
  147. GS.HasNonInstructionUser = true;
  148. // We might have a dead and dangling constant hanging off of here.
  149. if (!isSafeToDestroyConstant(C))
  150. return true;
  151. } else {
  152. GS.HasNonInstructionUser = true;
  153. // Otherwise must be some other user.
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
  160. SmallPtrSet<const PHINode *, 16> PhiUsers;
  161. return analyzeGlobalAux(V, GS, PhiUsers);
  162. }
  163. GlobalStatus::GlobalStatus()
  164. : IsCompared(false), IsLoaded(false), StoredType(NotStored),
  165. StoredOnceValue(nullptr), AccessingFunction(nullptr),
  166. HasMultipleAccessingFunctions(false), HasNonInstructionUser(false),
  167. Ordering(NotAtomic) {}