ProvenanceAnalysis.cpp 6.1 KB

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