LLVM 20.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/// TODO: The calls here to AAResultBase member functions are all effectively
24/// no-ops that just return a conservative result. The original intent was to
25/// chain to another analysis for a recursive query, but this was lost in a
26/// refactor. These should instead be rephrased in terms of queries to AAQI.AAR.
27///
28//===----------------------------------------------------------------------===//
29
33#include "llvm/IR/Function.h"
35#include "llvm/Pass.h"
36
37#define DEBUG_TYPE "objc-arc-aa"
38
39using namespace llvm;
40using namespace llvm::objcarc;
41
43 const MemoryLocation &LocB,
44 AAQueryInfo &AAQI, const Instruction *) {
45 if (!EnableARCOpts)
46 return AAResultBase::alias(LocA, LocB, AAQI, nullptr);
47
48 // First, strip off no-ops, including ObjC-specific no-ops, and try making a
49 // precise alias query.
50 const Value *SA = GetRCIdentityRoot(LocA.Ptr);
51 const Value *SB = GetRCIdentityRoot(LocB.Ptr);
53 MemoryLocation(SA, LocA.Size, LocA.AATags),
54 MemoryLocation(SB, LocB.Size, LocB.AATags), AAQI, nullptr);
55 if (Result != AliasResult::MayAlias)
56 return Result;
57
58 // If that failed, climb to the underlying object, including climbing through
59 // ObjC-specific no-ops, and try making an imprecise alias query.
60 const Value *UA = GetUnderlyingObjCPtr(SA);
61 const Value *UB = GetUnderlyingObjCPtr(SB);
62 if (UA != SA || UB != SB) {
65 nullptr);
66 // We can't use MustAlias or PartialAlias results here because
67 // GetUnderlyingObjCPtr may return an offsetted pointer value.
68 if (Result == AliasResult::NoAlias)
70 }
71
72 // If that failed, fail. We don't need to chain here, since that's covered
73 // by the earlier precise query.
75}
76
78 AAQueryInfo &AAQI,
79 bool IgnoreLocals) {
80 if (!EnableARCOpts)
81 return AAResultBase::getModRefInfoMask(Loc, AAQI, IgnoreLocals);
82
83 // First, strip off no-ops, including ObjC-specific no-ops, and try making
84 // a precise alias query.
85 const Value *S = GetRCIdentityRoot(Loc.Ptr);
87 MemoryLocation(S, Loc.Size, Loc.AATags), AAQI, IgnoreLocals)))
89
90 // If that failed, climb to the underlying object, including climbing through
91 // ObjC-specific no-ops, and try making an imprecise alias query.
92 const Value *U = GetUnderlyingObjCPtr(S);
93 if (U != S)
95 AAQI, IgnoreLocals);
96
97 // If that failed, fail. We don't need to chain here, since that's covered
98 // by the earlier precise query.
99 return ModRefInfo::ModRef;
100}
101
103 if (!EnableARCOpts)
105
106 switch (GetFunctionClass(F)) {
108 return MemoryEffects::none();
109 default:
110 break;
111 }
112
114}
115
117 const MemoryLocation &Loc,
118 AAQueryInfo &AAQI) {
119 if (!EnableARCOpts)
120 return AAResultBase::getModRefInfo(Call, Loc, AAQI);
121
122 switch (GetBasicARCInstKind(Call)) {
131 // These functions don't access any memory visible to the compiler.
132 // Note that this doesn't include objc_retainBlock, because it updates
133 // pointers when it copies block data.
135 default:
136 break;
137 }
138
139 return AAResultBase::getModRefInfo(Call, Loc, AAQI);
140}
141
142AnalysisKey ObjCARCAA::Key;
143
145 return ObjCARCAAResult(F.getDataLayout());
146}
#define F(x, y, z)
Definition: MD5.cpp:55
This file declares a simple ARC-aware AliasAnalysis using special knowledge of Objective C to enhance...
This file defines common analysis utilities used by the ObjC ARC Optimizer.
This class stores info we want to provide to or retain within an alias query.
MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI)
ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool IgnoreLocals)
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *I)
The possible results of an alias query.
Definition: AliasAnalysis.h:77
@ MayAlias
The two locations may or may not alias.
Definition: AliasAnalysis.h:98
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:95
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1236
static MemoryEffectsBase none()
Create MemoryEffectsBase that cannot read or write any memory.
Definition: ModRef.h:117
Representation for a specific memory location.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
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...
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
const Value * Ptr
The address of the start of the location.
LLVM Value Representation.
Definition: Value.h:74
This is a simple alias analysis implementation that uses knowledge of ARC constructs to answer querie...
MemoryEffects getMemoryEffects(const Function *F)
ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool IgnoreLocals)
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *CtxI)
ObjCARCAAResult run(Function &F, FunctionAnalysisManager &AM)
@ FusedRetainAutorelease
objc_retainAutorelease
@ AutoreleasepoolPush
objc_autoreleasePoolPush
@ Autorelease
objc_autorelease
@ RetainRV
objc_retainAutoreleasedReturnValue
@ FusedRetainAutoreleaseRV
objc_retainAutoreleaseReturnValue
@ AutoreleaseRV
objc_autoreleaseReturnValue
@ Call
could call objc_release
@ NoopCast
objc_retainedObject, etc.
ARCInstKind GetFunctionClass(const Function *F)
Determine if F is one of the special known Functions.
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
const Value * GetUnderlyingObjCPtr(const Value *V)
This is a wrapper around getUnderlyingObject which also knows how to look through objc_retain and obj...
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...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: ModRef.h:27
@ ModRef
The access may reference and may modify the value stored in memory.
@ NoModRef
The access neither references nor modifies the value stored in memory.
bool isNoModRef(const ModRefInfo MRI)
Definition: ModRef.h:39
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28