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