xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/ObjCARCAliasAnalysis.cpp (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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 
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/InitializePasses.h"
30 #include "llvm/Pass.h"
31 
32 #define DEBUG_TYPE "objc-arc-aa"
33 
34 using namespace llvm;
35 using namespace llvm::objcarc;
36 
37 AliasResult ObjCARCAAResult::alias(const MemoryLocation &LocA,
38                                    const MemoryLocation &LocB,
39                                    AAQueryInfo &AAQI, const Instruction *) {
40   if (!EnableARCOpts)
41     return AAResultBase::alias(LocA, LocB, AAQI, nullptr);
42 
43   // First, strip off no-ops, including ObjC-specific no-ops, and try making a
44   // precise alias query.
45   const Value *SA = GetRCIdentityRoot(LocA.Ptr);
46   const Value *SB = GetRCIdentityRoot(LocB.Ptr);
47   AliasResult Result = AAResultBase::alias(
48       MemoryLocation(SA, LocA.Size, LocA.AATags),
49       MemoryLocation(SB, LocB.Size, LocB.AATags), AAQI, nullptr);
50   if (Result != AliasResult::MayAlias)
51     return Result;
52 
53   // If that failed, climb to the underlying object, including climbing through
54   // ObjC-specific no-ops, and try making an imprecise alias query.
55   const Value *UA = GetUnderlyingObjCPtr(SA);
56   const Value *UB = GetUnderlyingObjCPtr(SB);
57   if (UA != SA || UB != SB) {
58     Result = AAResultBase::alias(MemoryLocation::getBeforeOrAfter(UA),
59                                  MemoryLocation::getBeforeOrAfter(UB), AAQI,
60                                  nullptr);
61     // We can't use MustAlias or PartialAlias results here because
62     // GetUnderlyingObjCPtr may return an offsetted pointer value.
63     if (Result == AliasResult::NoAlias)
64       return AliasResult::NoAlias;
65   }
66 
67   // If that failed, fail. We don't need to chain here, since that's covered
68   // by the earlier precise query.
69   return AliasResult::MayAlias;
70 }
71 
72 ModRefInfo ObjCARCAAResult::getModRefInfoMask(const MemoryLocation &Loc,
73                                               AAQueryInfo &AAQI,
74                                               bool IgnoreLocals) {
75   if (!EnableARCOpts)
76     return AAResultBase::getModRefInfoMask(Loc, AAQI, IgnoreLocals);
77 
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 (isNoModRef(AAResultBase::getModRefInfoMask(
82           MemoryLocation(S, Loc.Size, Loc.AATags), AAQI, IgnoreLocals)))
83     return ModRefInfo::NoModRef;
84 
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);
88   if (U != S)
89     return AAResultBase::getModRefInfoMask(MemoryLocation::getBeforeOrAfter(U),
90                                            AAQI, IgnoreLocals);
91 
92   // If that failed, fail. We don't need to chain here, since that's covered
93   // by the earlier precise query.
94   return ModRefInfo::ModRef;
95 }
96 
97 MemoryEffects ObjCARCAAResult::getMemoryEffects(const Function *F) {
98   if (!EnableARCOpts)
99     return AAResultBase::getMemoryEffects(F);
100 
101   switch (GetFunctionClass(F)) {
102   case ARCInstKind::NoopCast:
103     return MemoryEffects::none();
104   default:
105     break;
106   }
107 
108   return AAResultBase::getMemoryEffects(F);
109 }
110 
111 ModRefInfo ObjCARCAAResult::getModRefInfo(const CallBase *Call,
112                                           const MemoryLocation &Loc,
113                                           AAQueryInfo &AAQI) {
114   if (!EnableARCOpts)
115     return AAResultBase::getModRefInfo(Call, Loc, AAQI);
116 
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;
130   default:
131     break;
132   }
133 
134   return AAResultBase::getModRefInfo(Call, Loc, AAQI);
135 }
136 
137 AnalysisKey ObjCARCAA::Key;
138 
139 ObjCARCAAResult ObjCARCAA::run(Function &F, FunctionAnalysisManager &AM) {
140   return ObjCARCAAResult(F.getParent()->getDataLayout());
141 }
142