1 //===- ObjCARCAliasAnalysis.cpp - ObjC ARC Optimization -------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 /// WARNING: This file knows about certain library functions. It recognizes them
14 /// by name, and hardwires knowledge of their semantics.
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.
20 /// TODO: Theoretically we could check for dependencies between objc_* calls
21 /// and FMRB_OnlyAccessesArgumentPointees calls or other well-behaved calls.
23 //===----------------------------------------------------------------------===//
25 #include "llvm/Analysis/ObjCARCAliasAnalysis.h"
26 #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
27 #include "llvm/Analysis/Passes.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/Instruction.h"
30 #include "llvm/IR/Value.h"
31 #include "llvm/InitializePasses.h"
32 #include "llvm/Pass.h"
34 #define DEBUG_TYPE "objc-arc-aa"
37 using namespace llvm::objcarc
;
39 AliasResult
ObjCARCAAResult::alias(const MemoryLocation
&LocA
,
40 const MemoryLocation
&LocB
,
43 return AAResultBase::alias(LocA
, LocB
, AAQI
);
45 // First, strip off no-ops, including ObjC-specific no-ops, and try making a
46 // precise alias query.
47 const Value
*SA
= GetRCIdentityRoot(LocA
.Ptr
);
48 const Value
*SB
= GetRCIdentityRoot(LocB
.Ptr
);
50 AAResultBase::alias(MemoryLocation(SA
, LocA
.Size
, LocA
.AATags
),
51 MemoryLocation(SB
, LocB
.Size
, LocB
.AATags
), AAQI
);
52 if (Result
!= AliasResult::MayAlias
)
55 // If that failed, climb to the underlying object, including climbing through
56 // ObjC-specific no-ops, and try making an imprecise alias query.
57 const Value
*UA
= GetUnderlyingObjCPtr(SA
);
58 const Value
*UB
= GetUnderlyingObjCPtr(SB
);
59 if (UA
!= SA
|| UB
!= SB
) {
60 Result
= AAResultBase::alias(MemoryLocation::getBeforeOrAfter(UA
),
61 MemoryLocation::getBeforeOrAfter(UB
), AAQI
);
62 // We can't use MustAlias or PartialAlias results here because
63 // GetUnderlyingObjCPtr may return an offsetted pointer value.
64 if (Result
== AliasResult::NoAlias
)
65 return AliasResult::NoAlias
;
68 // If that failed, fail. We don't need to chain here, since that's covered
69 // by the earlier precise query.
70 return AliasResult::MayAlias
;
73 bool ObjCARCAAResult::pointsToConstantMemory(const MemoryLocation
&Loc
,
74 AAQueryInfo
&AAQI
, bool OrLocal
) {
76 return AAResultBase::pointsToConstantMemory(Loc
, AAQI
, OrLocal
);
78 // First, strip off no-ops, including ObjC-specific no-ops, and try making
79 // a precise alias query.
80 const Value
*S
= GetRCIdentityRoot(Loc
.Ptr
);
81 if (AAResultBase::pointsToConstantMemory(
82 MemoryLocation(S
, Loc
.Size
, Loc
.AATags
), AAQI
, OrLocal
))
85 // If that failed, climb to the underlying object, including climbing through
86 // ObjC-specific no-ops, and try making an imprecise alias query.
87 const Value
*U
= GetUnderlyingObjCPtr(S
);
89 return AAResultBase::pointsToConstantMemory(
90 MemoryLocation::getBeforeOrAfter(U
), AAQI
, OrLocal
);
92 // If that failed, fail. We don't need to chain here, since that's covered
93 // by the earlier precise query.
97 FunctionModRefBehavior
ObjCARCAAResult::getModRefBehavior(const Function
*F
) {
99 return AAResultBase::getModRefBehavior(F
);
101 switch (GetFunctionClass(F
)) {
102 case ARCInstKind::NoopCast
:
103 return FMRB_DoesNotAccessMemory
;
108 return AAResultBase::getModRefBehavior(F
);
111 ModRefInfo
ObjCARCAAResult::getModRefInfo(const CallBase
*Call
,
112 const MemoryLocation
&Loc
,
115 return AAResultBase::getModRefInfo(Call
, Loc
, AAQI
);
117 switch (GetBasicARCInstKind(Call
)) {
118 case ARCInstKind::Retain
:
119 case ARCInstKind::RetainRV
:
120 case ARCInstKind::Autorelease
:
121 case ARCInstKind::AutoreleaseRV
:
122 case ARCInstKind::NoopCast
:
123 case ARCInstKind::AutoreleasepoolPush
:
124 case ARCInstKind::FusedRetainAutorelease
:
125 case ARCInstKind::FusedRetainAutoreleaseRV
:
126 // These functions don't access any memory visible to the compiler.
127 // Note that this doesn't include objc_retainBlock, because it updates
128 // pointers when it copies block data.
129 return ModRefInfo::NoModRef
;
134 return AAResultBase::getModRefInfo(Call
, Loc
, AAQI
);
137 AnalysisKey
ObjCARCAA::Key
;
139 ObjCARCAAResult
ObjCARCAA::run(Function
&F
, FunctionAnalysisManager
&AM
) {
140 return ObjCARCAAResult(F
.getParent()->getDataLayout());
143 char ObjCARCAAWrapperPass::ID
= 0;
144 INITIALIZE_PASS(ObjCARCAAWrapperPass
, "objc-arc-aa",
145 "ObjC-ARC-Based Alias Analysis", false, true)
147 ImmutablePass
*llvm::createObjCARCAAWrapperPass() {
148 return new ObjCARCAAWrapperPass();
151 ObjCARCAAWrapperPass::ObjCARCAAWrapperPass() : ImmutablePass(ID
) {
152 initializeObjCARCAAWrapperPassPass(*PassRegistry::getPassRegistry());
155 bool ObjCARCAAWrapperPass::doInitialization(Module
&M
) {
156 Result
.reset(new ObjCARCAAResult(M
.getDataLayout()));
160 bool ObjCARCAAWrapperPass::doFinalization(Module
&M
) {
165 void ObjCARCAAWrapperPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
166 AU
.setPreservesAll();