LLVM  16.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/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 
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 =
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) {
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 
72  AAQueryInfo &AAQI,
73  bool IgnoreLocals) {
74  if (!EnableARCOpts)
75  return AAResultBase::getModRefInfoMask(Loc, AAQI, IgnoreLocals);
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, IgnoreLocals)))
82  return ModRefInfo::NoModRef;
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);
87  if (U != S)
89  AAQI, IgnoreLocals);
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 ModRefInfo::ModRef;
94 }
95 
97  if (!EnableARCOpts)
99 
100  switch (GetFunctionClass(F)) {
102  return MemoryEffects::none();
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 
136 AnalysisKey ObjCARCAA::Key;
137 
139  return ObjCARCAAResult(F.getParent()->getDataLayout());
140 }
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::objcarc::ObjCARCAAResult::getModRefInfoMask
ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool IgnoreLocals)
Definition: ObjCARCAliasAnalysis.cpp:71
llvm::AliasResult::MayAlias
@ MayAlias
The two locations may or may not alias.
Definition: AliasAnalysis.h:104
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::objcarc::ObjCARCAAResult::getMemoryEffects
MemoryEffects getMemoryEffects(const Function *F)
Definition: ObjCARCAliasAnalysis.cpp:96
llvm::MemoryLocation::Ptr
const Value * Ptr
The address of the start of the location.
Definition: MemoryLocation.h:219
llvm::Function
Definition: Function.h:60
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:37
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:228
llvm::AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:83
llvm::objcarc::ARCInstKind::Autorelease
@ Autorelease
objc_autorelease
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::MemoryEffects
Summary of how a function affects memory in the program.
Definition: ModRef.h:63
llvm::AAQueryInfo
This class stores info we want to provide to or retain within an alias query.
Definition: AliasAnalysis.h:233
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:232
llvm::AAResultBase::getModRefInfoMask
ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool IgnoreLocals)
Definition: AliasAnalysis.h:805
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:110
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:69
llvm::ModRefInfo
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: ModRef.h:27
llvm::MemoryEffects::none
static MemoryEffects none()
Create MemoryEffects that cannot read or write any memory.
Definition: ModRef.h:118
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::objcarc::ObjCARCAA::run
ObjCARCAAResult run(Function &F, FunctionAnalysisManager &AM)
Definition: ObjCARCAliasAnalysis.cpp:138
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:70
llvm::AliasResult::NoAlias
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:101
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::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:111
ObjCARCAnalysisUtils.h
llvm::objcarc::ARCInstKind::AutoreleasepoolPush
@ AutoreleasepoolPush
objc_autoreleasePoolPush
llvm::objcarc::ARCInstKind::FusedRetainAutoreleaseRV
@ FusedRetainAutoreleaseRV
objc_retainAutoreleaseReturnValue
Function.h
llvm::AAResultBase::getModRefInfo
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Definition: AliasAnalysis.h:822
llvm::AAResultBase::getMemoryEffects
MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI)
Definition: AliasAnalysis.h:814
llvm::isNoModRef
bool isNoModRef(const ModRefInfo MRI)
Definition: ModRef.h:39
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:279
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::objcarc::ARCInstKind::Retain
@ Retain
objc_retain
llvm::ModRefInfo::ModRef
@ ModRef
The access may reference and may modify the value stored in memory.
llvm::AAResultBase::alias
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
Definition: AliasAnalysis.h:800
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:211
Passes.h
llvm::objcarc::ARCInstKind::AutoreleaseRV
@ AutoreleaseRV
objc_autoreleaseReturnValue