ObjCARCAliasAnalysis.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/InitializePasses.h"
  29. #include "llvm/Pass.h"
  30. #define DEBUG_TYPE "objc-arc-aa"
  31. using namespace llvm;
  32. using namespace llvm::objcarc;
  33. AliasResult ObjCARCAAResult::alias(const MemoryLocation &LocA,
  34. const MemoryLocation &LocB,
  35. AAQueryInfo &AAQI, const Instruction *) {
  36. if (!EnableARCOpts)
  37. return AAResultBase::alias(LocA, LocB, AAQI, nullptr);
  38. // First, strip off no-ops, including ObjC-specific no-ops, and try making a
  39. // precise alias query.
  40. const Value *SA = GetRCIdentityRoot(LocA.Ptr);
  41. const Value *SB = GetRCIdentityRoot(LocB.Ptr);
  42. AliasResult Result = AAResultBase::alias(
  43. MemoryLocation(SA, LocA.Size, LocA.AATags),
  44. MemoryLocation(SB, LocB.Size, LocB.AATags), AAQI, nullptr);
  45. if (Result != AliasResult::MayAlias)
  46. return Result;
  47. // If that failed, climb to the underlying object, including climbing through
  48. // ObjC-specific no-ops, and try making an imprecise alias query.
  49. const Value *UA = GetUnderlyingObjCPtr(SA);
  50. const Value *UB = GetUnderlyingObjCPtr(SB);
  51. if (UA != SA || UB != SB) {
  52. Result = AAResultBase::alias(MemoryLocation::getBeforeOrAfter(UA),
  53. MemoryLocation::getBeforeOrAfter(UB), AAQI,
  54. nullptr);
  55. // We can't use MustAlias or PartialAlias results here because
  56. // GetUnderlyingObjCPtr may return an offsetted pointer value.
  57. if (Result == AliasResult::NoAlias)
  58. return AliasResult::NoAlias;
  59. }
  60. // If that failed, fail. We don't need to chain here, since that's covered
  61. // by the earlier precise query.
  62. return AliasResult::MayAlias;
  63. }
  64. ModRefInfo ObjCARCAAResult::getModRefInfoMask(const MemoryLocation &Loc,
  65. AAQueryInfo &AAQI,
  66. bool IgnoreLocals) {
  67. if (!EnableARCOpts)
  68. return AAResultBase::getModRefInfoMask(Loc, AAQI, IgnoreLocals);
  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 (isNoModRef(AAResultBase::getModRefInfoMask(
  73. MemoryLocation(S, Loc.Size, Loc.AATags), AAQI, IgnoreLocals)))
  74. return ModRefInfo::NoModRef;
  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::getModRefInfoMask(MemoryLocation::getBeforeOrAfter(U),
  80. AAQI, IgnoreLocals);
  81. // If that failed, fail. We don't need to chain here, since that's covered
  82. // by the earlier precise query.
  83. return ModRefInfo::ModRef;
  84. }
  85. MemoryEffects ObjCARCAAResult::getMemoryEffects(const Function *F) {
  86. if (!EnableARCOpts)
  87. return AAResultBase::getMemoryEffects(F);
  88. switch (GetFunctionClass(F)) {
  89. case ARCInstKind::NoopCast:
  90. return MemoryEffects::none();
  91. default:
  92. break;
  93. }
  94. return AAResultBase::getMemoryEffects(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. }