LLVM  14.0.0git
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/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 
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 != AliasResult::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) {
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;
66  }
67 
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;
71 }
72 
74  AAQueryInfo &AAQI, bool OrLocal) {
75  if (!EnableARCOpts)
76  return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
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);
82  MemoryLocation(S, Loc.Size, Loc.AATags), AAQI, OrLocal))
83  return true;
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)
90  MemoryLocation::getBeforeOrAfter(U), AAQI, OrLocal);
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 false;
95 }
96 
98  if (!EnableARCOpts)
100 
101  switch (GetFunctionClass(F)) {
104  default:
105  break;
106  }
107 
109 }
110 
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:
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 
140  return ObjCARCAAResult(F.getParent()->getDataLayout());
141 }
142 
143 char ObjCARCAAWrapperPass::ID = 0;
145  "ObjC-ARC-Based Alias Analysis", false, true)
146 
148  return new ObjCARCAAWrapperPass();
149 }
150 
153 }
154 
156  Result.reset(new ObjCARCAAResult(M.getDataLayout()));
157  return false;
158 }
159 
161  Result.reset();
162  return false;
163 }
164 
166  AU.setPreservesAll();
167 }
llvm::objcarc::GetBasicARCInstKind
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
Definition: ObjCARCInstKind.h:104
llvm::ModRefInfo::NoModRef
@ NoModRef
The access neither references nor modifies the value stored in memory.
llvm::AliasResult::MayAlias
@ MayAlias
The two locations may or may not alias.
Definition: AliasAnalysis.h:102
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:269
llvm::MemoryLocation::Ptr
const Value * Ptr
The address of the start of the location.
Definition: MemoryLocation.h:217
llvm::Function
Definition: Function.h:61
Pass.h
llvm::objcarc::GetFunctionClass
ARCInstKind GetFunctionClass(const Function *F)
Determine if F is one of the special known Functions.
Definition: ObjCARCInstKind.cpp:85
llvm::objcarc::ObjCARCAAResult
This is a simple alias analysis implementation that uses knowledge of ARC constructs to answer querie...
Definition: ObjCARCAliasAnalysis.h:37
llvm::objcarc::ObjCARCAAResult::alias
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
Definition: ObjCARCAliasAnalysis.cpp:39
llvm::MemoryLocation::Size
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
Definition: MemoryLocation.h:226
llvm::AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:81
llvm::objcarc::ARCInstKind::Autorelease
@ Autorelease
objc_autorelease
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::objcarc::ObjCARCAAWrapperPass::doInitialization
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: ObjCARCAliasAnalysis.cpp:155
llvm::objcarc::ObjCARCAAResult::getModRefBehavior
FunctionModRefBehavior getModRefBehavior(const Function *F)
Definition: ObjCARCAliasAnalysis.cpp:97
llvm::objcarc::ObjCARCAAWrapperPass
Legacy wrapper pass to provide the ObjCARCAAResult object.
Definition: ObjCARCAliasAnalysis.h:80
llvm::AAQueryInfo
This class stores info we want to provide to or retain within an alias query.
Definition: AliasAnalysis.h:414
Instruction.h
llvm::MemoryLocation::AATags
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
Definition: MemoryLocation.h:230
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::objcarc::ARCInstKind::RetainRV
@ RetainRV
objc_retainAutoreleasedReturnValue
llvm::objcarc::ARCInstKind::NoopCast
@ NoopCast
objc_retainedObject, etc.
llvm::objcarc::ObjCARCAAResult::getModRefInfo
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Definition: ObjCARCAliasAnalysis.cpp:111
INITIALIZE_PASS
INITIALIZE_PASS(ObjCARCAAWrapperPass, "objc-arc-aa", "ObjC-ARC-Based Alias Analysis", false, true) ImmutablePass *llvm
Definition: ObjCARCAliasAnalysis.cpp:144
llvm::objcarc::ObjCARCAAWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: ObjCARCAliasAnalysis.cpp:165
llvm::AAResultBase::alias
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
Definition: AliasAnalysis.h:1164
llvm::objcarc::ARCInstKind::FusedRetainAutorelease
@ FusedRetainAutorelease
objc_retainAutorelease
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
llvm::initializeObjCARCAAWrapperPassPass
void initializeObjCARCAAWrapperPassPass(PassRegistry &)
llvm::createObjCARCAAWrapperPass
ImmutablePass * createObjCARCAAWrapperPass()
llvm::objcarc::ObjCARCAAWrapperPass::doFinalization
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: ObjCARCAliasAnalysis.cpp:160
llvm::ModRefInfo
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: AliasAnalysis.h:148
llvm::objcarc::ObjCARCAAWrapperPass::ObjCARCAAWrapperPass
ObjCARCAAWrapperPass()
Definition: ObjCARCAliasAnalysis.cpp:151
llvm::objcarc::EnableARCOpts
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
Definition: ObjCARCAnalysisUtils.cpp:23
ObjCARCAliasAnalysis.h
llvm::objcarc
Definition: ObjCARCAliasAnalysis.h:29
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::FMRB_DoesNotAccessMemory
@ FMRB_DoesNotAccessMemory
This function does not perform any non-local loads or stores to memory.
Definition: AliasAnalysis.h:268
llvm::AAResultBase::getModRefBehavior
FunctionModRefBehavior getModRefBehavior(const CallBase *Call)
Definition: AliasAnalysis.h:1178
llvm::objcarc::ObjCARCAA::run
ObjCARCAAResult run(Function &F, FunctionAnalysisManager &AM)
Definition: ObjCARCAliasAnalysis.cpp:139
llvm::objcarc::GetUnderlyingObjCPtr
const Value * GetUnderlyingObjCPtr(const Value *V)
This is a wrapper around getUnderlyingObject which also knows how to look through objc_retain and obj...
Definition: ObjCARCAnalysisUtils.h:69
llvm::AliasResult::NoAlias
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:99
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::FunctionModRefBehavior
FunctionModRefBehavior
Summary of how a function affects memory in the program.
Definition: AliasAnalysis.h:262
llvm::objcarc::GetRCIdentityRoot
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...
Definition: ObjCARCAnalysisUtils.h:107
ObjCARCAnalysisUtils.h
llvm::AAResultBase::getModRefInfo
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Definition: AliasAnalysis.h:1186
llvm::objcarc::ARCInstKind::AutoreleasepoolPush
@ AutoreleasepoolPush
objc_autoreleasePoolPush
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::objcarc::ARCInstKind::FusedRetainAutoreleaseRV
@ FusedRetainAutoreleaseRV
objc_retainAutoreleaseReturnValue
Function.h
llvm::AAResultBase::pointsToConstantMemory
bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool OrLocal)
Definition: AliasAnalysis.h:1169
llvm::objcarc::ObjCARCAAResult::pointsToConstantMemory
bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool OrLocal)
Definition: ObjCARCAliasAnalysis.cpp:73
llvm::objcarc::ObjCARCAAWrapperPass::ID
static char ID
Definition: ObjCARCAliasAnalysis.h:84
llvm::MemoryLocation::getBeforeOrAfter
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
Definition: MemoryLocation.h:275
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1161
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
Value.h
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::objcarc::ARCInstKind::Retain
@ Retain
objc_retain
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:209
Passes.h
llvm::objcarc::ARCInstKind::AutoreleaseRV
@ AutoreleaseRV
objc_autoreleaseReturnValue
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37