LLVM  9.0.0svn
ObjCARCAliasAnalysis.cpp
Go to the documentation of this file.
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 
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Value.h"
30 #include "llvm/InitializePasses.h"
32 #include "llvm/PassSupport.h"
33 
34 #define DEBUG_TYPE "objc-arc-aa"
35 
36 using namespace llvm;
37 using namespace llvm::objcarc;
38 
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 =
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, DL);
58  const Value *UB = GetUnderlyingObjCPtr(SB, DL);
59  if (UA != SA || UB != SB) {
60  Result = AAResultBase::alias(MemoryLocation(UA), MemoryLocation(UB), AAQI);
61  // We can't use MustAlias or PartialAlias results here because
62  // GetUnderlyingObjCPtr may return an offsetted pointer value.
63  if (Result == NoAlias)
64  return 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 MayAlias;
70 }
71 
73  AAQueryInfo &AAQI, bool OrLocal) {
74  if (!EnableARCOpts)
75  return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
76 
77  // First, strip off no-ops, including ObjC-specific no-ops, and try making
78  // a precise alias query.
79  const Value *S = GetRCIdentityRoot(Loc.Ptr);
81  MemoryLocation(S, Loc.Size, Loc.AATags), AAQI, OrLocal))
82  return true;
83 
84  // If that failed, climb to the underlying object, including climbing through
85  // ObjC-specific no-ops, and try making an imprecise alias query.
86  const Value *U = GetUnderlyingObjCPtr(S, DL);
87  if (U != S)
89  OrLocal);
90 
91  // If that failed, fail. We don't need to chain here, since that's covered
92  // by the earlier precise query.
93  return false;
94 }
95 
97  if (!EnableARCOpts)
99 
100  switch (GetFunctionClass(F)) {
103  default:
104  break;
105  }
106 
108 }
109 
111  const MemoryLocation &Loc,
112  AAQueryInfo &AAQI) {
113  if (!EnableARCOpts)
114  return AAResultBase::getModRefInfo(Call, Loc, AAQI);
115 
116  switch (GetBasicARCInstKind(Call)) {
117  case ARCInstKind::Retain:
125  // These functions don't access any memory visible to the compiler.
126  // Note that this doesn't include objc_retainBlock, because it updates
127  // pointers when it copies block data.
128  return ModRefInfo::NoModRef;
129  default:
130  break;
131  }
132 
133  return AAResultBase::getModRefInfo(Call, Loc, AAQI);
134 }
135 
137  return ObjCARCAAResult(F.getParent()->getDataLayout());
138 }
139 
140 char ObjCARCAAWrapperPass::ID = 0;
142  "ObjC-ARC-Based Alias Analysis", false, true)
143 
145  return new ObjCARCAAWrapperPass();
146 }
147 
150 }
151 
153  Result.reset(new ObjCARCAAResult(M.getDataLayout()));
154  return false;
155 }
156 
158  Result.reset();
159  return false;
160 }
161 
163  AU.setPreservesAll();
164 }
bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool OrLocal)
The access neither references nor modifies the value stored in memory.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
ObjCARCAAResult run(Function &F, FunctionAnalysisManager &AM)
This class stores info we want to provide to or retain within an alias query.
objc_retainedObject, etc.
The two locations do not alias at all.
Definition: AliasAnalysis.h:84
FunctionModRefBehavior getModRefBehavior(const CallBase *Call)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1100
F(f)
could call objc_release
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
objc_autoreleaseReturnValue
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:369
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool OrLocal)
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
objc_retainAutoreleasedReturnValue
FunctionModRefBehavior
Summary of how a function affects memory in the program.
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
FunctionModRefBehavior getModRefBehavior(const Function *F)
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:78
Represent the analysis usage information of a pass.
ARCInstKind GetFunctionClass(const Function *F)
Determine if F is one of the special known Functions.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known...
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
This file defines common analysis utilities used by the ObjC ARC Optimizer.
This file declares a simple ARC-aware AliasAnalysis using special knowledge of Objective C to enhance...
This function does not perform any non-local loads or stores to memory.
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
The two locations may or may not alias. This is the least precise result.
Definition: AliasAnalysis.h:86
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:255
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
void initializeObjCARCAAWrapperPassPass(PassRegistry &)
void setPreservesAll()
Set by analyses that do not transform their input at all.
This is a simple alias analysis implementation that uses knowledge of ARC constructs to answer querie...
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
const Value * GetUnderlyingObjCPtr(const Value *V, const DataLayout &DL)
This is a wrapper around getUnderlyingObject which also knows how to look through objc_retain and obj...
ObjCARCAAResult(const DataLayout &DL)
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
LLVM Value Representation.
Definition: Value.h:72
Legacy wrapper pass to provide the ObjCARCAAResult object.
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
const Value * GetRCIdentityRoot(const Value *V)
The RCIdentity root of a value V is a dominating value U for which retaining or releasing U is equiva...
A container for analyses that lazily runs them and caches their results.
INITIALIZE_PASS(ObjCARCAAWrapperPass, "objc-arc-aa", "ObjC-ARC-Based Alias Analysis", false, true) ImmutablePass *llvm
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
ImmutablePass * createObjCARCAAWrapperPass()
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...