xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/ObjCARCAliasAnalysis.cpp (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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) {
40   if (!EnableARCOpts)
41     return AAResultBase::alias(LocA, LocB, AAQI);
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 =
48       AAResultBase::alias(MemoryLocation(SA, LocA.Size, LocA.AATags),
49                           MemoryLocation(SB, LocB.Size, LocB.AATags), AAQI);
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     // We can't use MustAlias or PartialAlias results here because
61     // GetUnderlyingObjCPtr may return an offsetted pointer value.
62     if (Result == AliasResult::NoAlias)
63       return AliasResult::NoAlias;
64   }
65 
66   // If that failed, fail. We don't need to chain here, since that's covered
67   // by the earlier precise query.
68   return AliasResult::MayAlias;
69 }
70 
71 bool ObjCARCAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
72                                              AAQueryInfo &AAQI, bool OrLocal) {
73   if (!EnableARCOpts)
74     return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
75 
76   // First, strip off no-ops, including ObjC-specific no-ops, and try making
77   // a precise alias query.
78   const Value *S = GetRCIdentityRoot(Loc.Ptr);
79   if (AAResultBase::pointsToConstantMemory(
80           MemoryLocation(S, Loc.Size, Loc.AATags), AAQI, OrLocal))
81     return true;
82 
83   // If that failed, climb to the underlying object, including climbing through
84   // ObjC-specific no-ops, and try making an imprecise alias query.
85   const Value *U = GetUnderlyingObjCPtr(S);
86   if (U != S)
87     return AAResultBase::pointsToConstantMemory(
88         MemoryLocation::getBeforeOrAfter(U), AAQI, OrLocal);
89 
90   // If that failed, fail. We don't need to chain here, since that's covered
91   // by the earlier precise query.
92   return false;
93 }
94 
95 FunctionModRefBehavior ObjCARCAAResult::getModRefBehavior(const Function *F) {
96   if (!EnableARCOpts)
97     return AAResultBase::getModRefBehavior(F);
98 
99   switch (GetFunctionClass(F)) {
100   case ARCInstKind::NoopCast:
101     return FMRB_DoesNotAccessMemory;
102   default:
103     break;
104   }
105 
106   return AAResultBase::getModRefBehavior(F);
107 }
108 
109 ModRefInfo ObjCARCAAResult::getModRefInfo(const CallBase *Call,
110                                           const MemoryLocation &Loc,
111                                           AAQueryInfo &AAQI) {
112   if (!EnableARCOpts)
113     return AAResultBase::getModRefInfo(Call, Loc, AAQI);
114 
115   switch (GetBasicARCInstKind(Call)) {
116   case ARCInstKind::Retain:
117   case ARCInstKind::RetainRV:
118   case ARCInstKind::Autorelease:
119   case ARCInstKind::AutoreleaseRV:
120   case ARCInstKind::NoopCast:
121   case ARCInstKind::AutoreleasepoolPush:
122   case ARCInstKind::FusedRetainAutorelease:
123   case ARCInstKind::FusedRetainAutoreleaseRV:
124     // These functions don't access any memory visible to the compiler.
125     // Note that this doesn't include objc_retainBlock, because it updates
126     // pointers when it copies block data.
127     return ModRefInfo::NoModRef;
128   default:
129     break;
130   }
131 
132   return AAResultBase::getModRefInfo(Call, Loc, AAQI);
133 }
134 
135 AnalysisKey ObjCARCAA::Key;
136 
137 ObjCARCAAResult ObjCARCAA::run(Function &F, FunctionAnalysisManager &AM) {
138   return ObjCARCAAResult(F.getParent()->getDataLayout());
139 }
140 
141 char ObjCARCAAWrapperPass::ID = 0;
142 INITIALIZE_PASS(ObjCARCAAWrapperPass, "objc-arc-aa",
143                 "ObjC-ARC-Based Alias Analysis", false, true)
144 
145 ImmutablePass *llvm::createObjCARCAAWrapperPass() {
146   return new ObjCARCAAWrapperPass();
147 }
148 
149 ObjCARCAAWrapperPass::ObjCARCAAWrapperPass() : ImmutablePass(ID) {
150   initializeObjCARCAAWrapperPassPass(*PassRegistry::getPassRegistry());
151 }
152 
153 bool ObjCARCAAWrapperPass::doInitialization(Module &M) {
154   Result.reset(new ObjCARCAAResult(M.getDataLayout()));
155   return false;
156 }
157 
158 bool ObjCARCAAWrapperPass::doFinalization(Module &M) {
159   Result.reset();
160   return false;
161 }
162 
163 void ObjCARCAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
164   AU.setPreservesAll();
165 }
166