ObjCARCAliasAnalysis.cpp 5.9 KB

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