ObjCARCAliasAnalysis.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===- ObjCARCAliasAnalysis.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. /// This file defines a simple ARC-aware AliasAnalysis using special knowledge
  11. /// of Objective C to enhance other optimization passes which rely on the Alias
  12. /// Analysis infrastructure.
  13. ///
  14. /// WARNING: This file knows about certain library functions. It recognizes them
  15. /// by name, and hardwires knowledge of their semantics.
  16. ///
  17. /// WARNING: This file knows about how certain Objective-C library functions are
  18. /// used. Naive LLVM IR transformations which would otherwise be
  19. /// behavior-preserving may break these assumptions.
  20. ///
  21. //===----------------------------------------------------------------------===//
  22. #include "ObjCARC.h"
  23. #include "ObjCARCAliasAnalysis.h"
  24. #include "llvm/IR/Instruction.h"
  25. #include "llvm/InitializePasses.h"
  26. #include "llvm/PassAnalysisSupport.h"
  27. #include "llvm/PassSupport.h"
  28. #define DEBUG_TYPE "objc-arc-aa"
  29. namespace llvm {
  30. class Function;
  31. class Value;
  32. }
  33. using namespace llvm;
  34. using namespace llvm::objcarc;
  35. // Register this pass...
  36. char ObjCARCAliasAnalysis::ID = 0;
  37. INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa",
  38. "ObjC-ARC-Based Alias Analysis", false, true, false)
  39. ImmutablePass *llvm::createObjCARCAliasAnalysisPass() {
  40. return new ObjCARCAliasAnalysis();
  41. }
  42. bool ObjCARCAliasAnalysis::doInitialization(Module &M) {
  43. InitializeAliasAnalysis(this, &M.getDataLayout());
  44. return true;
  45. }
  46. void
  47. ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
  48. AU.setPreservesAll();
  49. AliasAnalysis::getAnalysisUsage(AU);
  50. }
  51. AliasResult ObjCARCAliasAnalysis::alias(const MemoryLocation &LocA,
  52. const MemoryLocation &LocB) {
  53. if (!EnableARCOpts)
  54. return AliasAnalysis::alias(LocA, LocB);
  55. // First, strip off no-ops, including ObjC-specific no-ops, and try making a
  56. // precise alias query.
  57. const Value *SA = GetRCIdentityRoot(LocA.Ptr);
  58. const Value *SB = GetRCIdentityRoot(LocB.Ptr);
  59. AliasResult Result =
  60. AliasAnalysis::alias(MemoryLocation(SA, LocA.Size, LocA.AATags),
  61. MemoryLocation(SB, LocB.Size, LocB.AATags));
  62. if (Result != MayAlias)
  63. return Result;
  64. // If that failed, climb to the underlying object, including climbing through
  65. // ObjC-specific no-ops, and try making an imprecise alias query.
  66. const Value *UA = GetUnderlyingObjCPtr(SA, *DL);
  67. const Value *UB = GetUnderlyingObjCPtr(SB, *DL);
  68. if (UA != SA || UB != SB) {
  69. Result = AliasAnalysis::alias(MemoryLocation(UA), MemoryLocation(UB));
  70. // We can't use MustAlias or PartialAlias results here because
  71. // GetUnderlyingObjCPtr may return an offsetted pointer value.
  72. if (Result == NoAlias)
  73. return NoAlias;
  74. }
  75. // If that failed, fail. We don't need to chain here, since that's covered
  76. // by the earlier precise query.
  77. return MayAlias;
  78. }
  79. bool ObjCARCAliasAnalysis::pointsToConstantMemory(const MemoryLocation &Loc,
  80. bool OrLocal) {
  81. if (!EnableARCOpts)
  82. return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
  83. // First, strip off no-ops, including ObjC-specific no-ops, and try making
  84. // a precise alias query.
  85. const Value *S = GetRCIdentityRoot(Loc.Ptr);
  86. if (AliasAnalysis::pointsToConstantMemory(
  87. MemoryLocation(S, Loc.Size, Loc.AATags), OrLocal))
  88. return true;
  89. // If that failed, climb to the underlying object, including climbing through
  90. // ObjC-specific no-ops, and try making an imprecise alias query.
  91. const Value *U = GetUnderlyingObjCPtr(S, *DL);
  92. if (U != S)
  93. return AliasAnalysis::pointsToConstantMemory(MemoryLocation(U), OrLocal);
  94. // If that failed, fail. We don't need to chain here, since that's covered
  95. // by the earlier precise query.
  96. return false;
  97. }
  98. AliasAnalysis::ModRefBehavior
  99. ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
  100. // We have nothing to do. Just chain to the next AliasAnalysis.
  101. return AliasAnalysis::getModRefBehavior(CS);
  102. }
  103. AliasAnalysis::ModRefBehavior
  104. ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) {
  105. if (!EnableARCOpts)
  106. return AliasAnalysis::getModRefBehavior(F);
  107. switch (GetFunctionClass(F)) {
  108. case ARCInstKind::NoopCast:
  109. return DoesNotAccessMemory;
  110. default:
  111. break;
  112. }
  113. return AliasAnalysis::getModRefBehavior(F);
  114. }
  115. AliasAnalysis::ModRefResult
  116. ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
  117. const MemoryLocation &Loc) {
  118. if (!EnableARCOpts)
  119. return AliasAnalysis::getModRefInfo(CS, Loc);
  120. switch (GetBasicARCInstKind(CS.getInstruction())) {
  121. case ARCInstKind::Retain:
  122. case ARCInstKind::RetainRV:
  123. case ARCInstKind::Autorelease:
  124. case ARCInstKind::AutoreleaseRV:
  125. case ARCInstKind::NoopCast:
  126. case ARCInstKind::AutoreleasepoolPush:
  127. case ARCInstKind::FusedRetainAutorelease:
  128. case ARCInstKind::FusedRetainAutoreleaseRV:
  129. // These functions don't access any memory visible to the compiler.
  130. // Note that this doesn't include objc_retainBlock, because it updates
  131. // pointers when it copies block data.
  132. return NoModRef;
  133. default:
  134. break;
  135. }
  136. return AliasAnalysis::getModRefInfo(CS, Loc);
  137. }
  138. AliasAnalysis::ModRefResult
  139. ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
  140. ImmutableCallSite CS2) {
  141. // TODO: Theoretically we could check for dependencies between objc_* calls
  142. // and OnlyAccessesArgumentPointees calls or other well-behaved calls.
  143. return AliasAnalysis::getModRefInfo(CS1, CS2);
  144. }