ProvenanceAnalysis.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //===- ProvenanceAnalysis.cpp - ObjC ARC Optimization ---------------------===//
  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. /// \file
  10. ///
  11. /// This file defines a special form of Alias Analysis called ``Provenance
  12. /// Analysis''. The word ``provenance'' refers to the history of the ownership
  13. /// of an object. Thus ``Provenance Analysis'' is an analysis which attempts to
  14. /// use various techniques to determine if locally
  15. ///
  16. /// WARNING: This file knows about certain library functions. It recognizes them
  17. /// by name, and hardwires knowledge of their semantics.
  18. ///
  19. /// WARNING: This file knows about how certain Objective-C library functions are
  20. /// used. Naive LLVM IR transformations which would otherwise be
  21. /// behavior-preserving may break these assumptions.
  22. ///
  23. //===----------------------------------------------------------------------===//
  24. #include "ObjCARC.h"
  25. #include "ProvenanceAnalysis.h"
  26. #include "llvm/ADT/STLExtras.h"
  27. #include "llvm/ADT/SmallPtrSet.h"
  28. using namespace llvm;
  29. using namespace llvm::objcarc;
  30. bool ProvenanceAnalysis::relatedSelect(const SelectInst *A,
  31. const Value *B) {
  32. const DataLayout &DL = A->getModule()->getDataLayout();
  33. // If the values are Selects with the same condition, we can do a more precise
  34. // check: just check for relations between the values on corresponding arms.
  35. if (const SelectInst *SB = dyn_cast<SelectInst>(B))
  36. if (A->getCondition() == SB->getCondition())
  37. return related(A->getTrueValue(), SB->getTrueValue(), DL) ||
  38. related(A->getFalseValue(), SB->getFalseValue(), DL);
  39. // Check both arms of the Select node individually.
  40. return related(A->getTrueValue(), B, DL) ||
  41. related(A->getFalseValue(), B, DL);
  42. }
  43. bool ProvenanceAnalysis::relatedPHI(const PHINode *A,
  44. const Value *B) {
  45. const DataLayout &DL = A->getModule()->getDataLayout();
  46. // If the values are PHIs in the same block, we can do a more precise as well
  47. // as efficient check: just check for relations between the values on
  48. // corresponding edges.
  49. if (const PHINode *PNB = dyn_cast<PHINode>(B))
  50. if (PNB->getParent() == A->getParent()) {
  51. for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i)
  52. if (related(A->getIncomingValue(i),
  53. PNB->getIncomingValueForBlock(A->getIncomingBlock(i)), DL))
  54. return true;
  55. return false;
  56. }
  57. // Check each unique source of the PHI node against B.
  58. SmallPtrSet<const Value *, 4> UniqueSrc;
  59. for (Value *PV1 : A->incoming_values()) {
  60. if (UniqueSrc.insert(PV1).second && related(PV1, B, DL))
  61. return true;
  62. }
  63. // All of the arms checked out.
  64. return false;
  65. }
  66. /// Test if the value of P, or any value covered by its provenance, is ever
  67. /// stored within the function (not counting callees).
  68. static bool IsStoredObjCPointer(const Value *P) {
  69. SmallPtrSet<const Value *, 8> Visited;
  70. SmallVector<const Value *, 8> Worklist;
  71. Worklist.push_back(P);
  72. Visited.insert(P);
  73. do {
  74. P = Worklist.pop_back_val();
  75. for (const Use &U : P->uses()) {
  76. const User *Ur = U.getUser();
  77. if (isa<StoreInst>(Ur)) {
  78. if (U.getOperandNo() == 0)
  79. // The pointer is stored.
  80. return true;
  81. // The pointed is stored through.
  82. continue;
  83. }
  84. if (isa<CallInst>(Ur))
  85. // The pointer is passed as an argument, ignore this.
  86. continue;
  87. if (isa<PtrToIntInst>(P))
  88. // Assume the worst.
  89. return true;
  90. if (Visited.insert(Ur).second)
  91. Worklist.push_back(Ur);
  92. }
  93. } while (!Worklist.empty());
  94. // Everything checked out.
  95. return false;
  96. }
  97. bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B,
  98. const DataLayout &DL) {
  99. // Skip past provenance pass-throughs.
  100. A = GetUnderlyingObjCPtr(A, DL);
  101. B = GetUnderlyingObjCPtr(B, DL);
  102. // Quick check.
  103. if (A == B)
  104. return true;
  105. // Ask regular AliasAnalysis, for a first approximation.
  106. switch (AA->alias(A, B)) {
  107. case NoAlias:
  108. return false;
  109. case MustAlias:
  110. case PartialAlias:
  111. return true;
  112. case MayAlias:
  113. break;
  114. }
  115. bool AIsIdentified = IsObjCIdentifiedObject(A);
  116. bool BIsIdentified = IsObjCIdentifiedObject(B);
  117. // An ObjC-Identified object can't alias a load if it is never locally stored.
  118. if (AIsIdentified) {
  119. // Check for an obvious escape.
  120. if (isa<LoadInst>(B))
  121. return IsStoredObjCPointer(A);
  122. if (BIsIdentified) {
  123. // Check for an obvious escape.
  124. if (isa<LoadInst>(A))
  125. return IsStoredObjCPointer(B);
  126. // Both pointers are identified and escapes aren't an evident problem.
  127. return false;
  128. }
  129. } else if (BIsIdentified) {
  130. // Check for an obvious escape.
  131. if (isa<LoadInst>(A))
  132. return IsStoredObjCPointer(B);
  133. }
  134. // Special handling for PHI and Select.
  135. if (const PHINode *PN = dyn_cast<PHINode>(A))
  136. return relatedPHI(PN, B);
  137. if (const PHINode *PN = dyn_cast<PHINode>(B))
  138. return relatedPHI(PN, A);
  139. if (const SelectInst *S = dyn_cast<SelectInst>(A))
  140. return relatedSelect(S, B);
  141. if (const SelectInst *S = dyn_cast<SelectInst>(B))
  142. return relatedSelect(S, A);
  143. // Conservative.
  144. return true;
  145. }
  146. bool ProvenanceAnalysis::related(const Value *A, const Value *B,
  147. const DataLayout &DL) {
  148. // Begin by inserting a conservative value into the map. If the insertion
  149. // fails, we have the answer already. If it succeeds, leave it there until we
  150. // compute the real answer to guard against recursive queries.
  151. if (A > B) std::swap(A, B);
  152. std::pair<CachedResultsTy::iterator, bool> Pair =
  153. CachedResults.insert(std::make_pair(ValuePairTy(A, B), true));
  154. if (!Pair.second)
  155. return Pair.first->second;
  156. bool Result = relatedCheck(A, B, DL);
  157. CachedResults[ValuePairTy(A, B)] = Result;
  158. return Result;
  159. }