GlobalStatus.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. for (const User *U : C->users())
  34. if (const Constant *CU = dyn_cast<Constant>(U)) {
  35. if (!isSafeToDestroyConstant(CU))
  36. return false;
  37. } else
  38. return false;
  39. return true;
  40. }
  41. static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
  42. SmallPtrSetImpl<const PHINode *> &PhiUsers) {
  43. for (const Use &U : V->uses()) {
  44. const User *UR = U.getUser();
  45. if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(UR)) {
  46. GS.HasNonInstructionUser = true;
  47. // If the result of the constantexpr isn't pointer type, then we won't
  48. // know to expect it in various places. Just reject early.
  49. if (!isa<PointerType>(CE->getType()))
  50. return true;
  51. if (analyzeGlobalAux(CE, GS, PhiUsers))
  52. return true;
  53. } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
  54. if (!GS.HasMultipleAccessingFunctions) {
  55. const Function *F = I->getParent()->getParent();
  56. if (!GS.AccessingFunction)
  57. GS.AccessingFunction = F;
  58. else if (GS.AccessingFunction != F)
  59. GS.HasMultipleAccessingFunctions = true;
  60. }
  61. if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
  62. GS.IsLoaded = true;
  63. // Don't hack on volatile loads.
  64. if (LI->isVolatile())
  65. return true;
  66. GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
  67. } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
  68. // Don't allow a store OF the address, only stores TO the address.
  69. if (SI->getOperand(0) == V)
  70. return true;
  71. // Don't hack on volatile stores.
  72. if (SI->isVolatile())
  73. return true;
  74. GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
  75. // If this is a direct store to the global (i.e., the global is a scalar
  76. // value, not an aggregate), keep more specific information about
  77. // stores.
  78. if (GS.StoredType != GlobalStatus::Stored) {
  79. if (const GlobalVariable *GV =
  80. dyn_cast<GlobalVariable>(SI->getOperand(1))) {
  81. Value *StoredVal = SI->getOperand(0);
  82. if (Constant *C = dyn_cast<Constant>(StoredVal)) {
  83. if (C->isThreadDependent()) {
  84. // The stored value changes between threads; don't track it.
  85. return true;
  86. }
  87. }
  88. if (StoredVal == GV->getInitializer()) {
  89. if (GS.StoredType < GlobalStatus::InitializerStored)
  90. GS.StoredType = GlobalStatus::InitializerStored;
  91. } else if (isa<LoadInst>(StoredVal) &&
  92. cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
  93. if (GS.StoredType < GlobalStatus::InitializerStored)
  94. GS.StoredType = GlobalStatus::InitializerStored;
  95. } else if (GS.StoredType < GlobalStatus::StoredOnce) {
  96. GS.StoredType = GlobalStatus::StoredOnce;
  97. GS.StoredOnceValue = StoredVal;
  98. } else if (GS.StoredType == GlobalStatus::StoredOnce &&
  99. GS.StoredOnceValue == StoredVal) {
  100. // noop.
  101. } else {
  102. GS.StoredType = GlobalStatus::Stored;
  103. }
  104. } else {
  105. GS.StoredType = GlobalStatus::Stored;
  106. }
  107. }
  108. } else if (isa<BitCastInst>(I)) {
  109. if (analyzeGlobalAux(I, GS, PhiUsers))
  110. return true;
  111. } else if (isa<GetElementPtrInst>(I)) {
  112. if (analyzeGlobalAux(I, GS, PhiUsers))
  113. return true;
  114. } else if (isa<SelectInst>(I)) {
  115. if (analyzeGlobalAux(I, GS, PhiUsers))
  116. return true;
  117. } else if (const PHINode *PN = dyn_cast<PHINode>(I)) {
  118. // PHI nodes we can check just like select or GEP instructions, but we
  119. // have to be careful about infinite recursion.
  120. if (PhiUsers.insert(PN)) // Not already visited.
  121. if (analyzeGlobalAux(I, GS, PhiUsers))
  122. return true;
  123. } else if (isa<CmpInst>(I)) {
  124. GS.IsCompared = true;
  125. } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
  126. if (MTI->isVolatile())
  127. return true;
  128. if (MTI->getArgOperand(0) == V)
  129. GS.StoredType = GlobalStatus::Stored;
  130. if (MTI->getArgOperand(1) == V)
  131. GS.IsLoaded = true;
  132. } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
  133. assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
  134. if (MSI->isVolatile())
  135. return true;
  136. GS.StoredType = GlobalStatus::Stored;
  137. } else if (ImmutableCallSite C = I) {
  138. if (!C.isCallee(&U))
  139. return true;
  140. GS.IsLoaded = true;
  141. } else {
  142. return true; // Any other non-load instruction might take address!
  143. }
  144. } else if (const Constant *C = dyn_cast<Constant>(UR)) {
  145. GS.HasNonInstructionUser = true;
  146. // We might have a dead and dangling constant hanging off of here.
  147. if (!isSafeToDestroyConstant(C))
  148. return true;
  149. } else {
  150. GS.HasNonInstructionUser = true;
  151. // Otherwise must be some other user.
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
  158. SmallPtrSet<const PHINode *, 16> PhiUsers;
  159. return analyzeGlobalAux(V, GS, PhiUsers);
  160. }
  161. GlobalStatus::GlobalStatus()
  162. : IsCompared(false), IsLoaded(false), StoredType(NotStored),
  163. StoredOnceValue(nullptr), AccessingFunction(nullptr),
  164. HasMultipleAccessingFunctions(false), HasNonInstructionUser(false),
  165. Ordering(NotAtomic) {}