Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / Analysis / ObjCARCAliasAnalysis.cpp
blobf768319f0a4dd42b52239f1ffcf80ac6dd641d71
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 //===----------------------------------------------------------------------===//
25 #include "llvm/Analysis/ObjCARCAliasAnalysis.h"
26 #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Value.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/PassAnalysisSupport.h"
32 #include "llvm/PassSupport.h"
34 #define DEBUG_TYPE "objc-arc-aa"
36 using namespace llvm;
37 using namespace llvm::objcarc;
39 AliasResult ObjCARCAAResult::alias(const MemoryLocation &LocA,
40 const MemoryLocation &LocB) {
41 if (!EnableARCOpts)
42 return AAResultBase::alias(LocA, LocB);
44 // First, strip off no-ops, including ObjC-specific no-ops, and try making a
45 // precise alias query.
46 const Value *SA = GetRCIdentityRoot(LocA.Ptr);
47 const Value *SB = GetRCIdentityRoot(LocB.Ptr);
48 AliasResult Result =
49 AAResultBase::alias(MemoryLocation(SA, LocA.Size, LocA.AATags),
50 MemoryLocation(SB, LocB.Size, LocB.AATags));
51 if (Result != MayAlias)
52 return Result;
54 // If that failed, climb to the underlying object, including climbing through
55 // ObjC-specific no-ops, and try making an imprecise alias query.
56 const Value *UA = GetUnderlyingObjCPtr(SA, DL);
57 const Value *UB = GetUnderlyingObjCPtr(SB, DL);
58 if (UA != SA || UB != SB) {
59 Result = AAResultBase::alias(MemoryLocation(UA), MemoryLocation(UB));
60 // We can't use MustAlias or PartialAlias results here because
61 // GetUnderlyingObjCPtr may return an offsetted pointer value.
62 if (Result == NoAlias)
63 return NoAlias;
66 // If that failed, fail. We don't need to chain here, since that's covered
67 // by the earlier precise query.
68 return MayAlias;
71 bool ObjCARCAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
72 bool OrLocal) {
73 if (!EnableARCOpts)
74 return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
76 // First, strip off no-ops, including ObjC-specific no-ops, and try making
77 // a precise alias query.
78 const Value *S = GetRCIdentityRoot(Loc.Ptr);
79 if (AAResultBase::pointsToConstantMemory(
80 MemoryLocation(S, Loc.Size, Loc.AATags), OrLocal))
81 return true;
83 // If that failed, climb to the underlying object, including climbing through
84 // ObjC-specific no-ops, and try making an imprecise alias query.
85 const Value *U = GetUnderlyingObjCPtr(S, DL);
86 if (U != S)
87 return AAResultBase::pointsToConstantMemory(MemoryLocation(U), OrLocal);
89 // If that failed, fail. We don't need to chain here, since that's covered
90 // by the earlier precise query.
91 return false;
94 FunctionModRefBehavior ObjCARCAAResult::getModRefBehavior(const Function *F) {
95 if (!EnableARCOpts)
96 return AAResultBase::getModRefBehavior(F);
98 switch (GetFunctionClass(F)) {
99 case ARCInstKind::NoopCast:
100 return FMRB_DoesNotAccessMemory;
101 default:
102 break;
105 return AAResultBase::getModRefBehavior(F);
108 ModRefInfo ObjCARCAAResult::getModRefInfo(const CallBase *Call,
109 const MemoryLocation &Loc) {
110 if (!EnableARCOpts)
111 return AAResultBase::getModRefInfo(Call, Loc);
113 switch (GetBasicARCInstKind(Call)) {
114 case ARCInstKind::Retain:
115 case ARCInstKind::RetainRV:
116 case ARCInstKind::Autorelease:
117 case ARCInstKind::AutoreleaseRV:
118 case ARCInstKind::NoopCast:
119 case ARCInstKind::AutoreleasepoolPush:
120 case ARCInstKind::FusedRetainAutorelease:
121 case ARCInstKind::FusedRetainAutoreleaseRV:
122 // These functions don't access any memory visible to the compiler.
123 // Note that this doesn't include objc_retainBlock, because it updates
124 // pointers when it copies block data.
125 return ModRefInfo::NoModRef;
126 default:
127 break;
130 return AAResultBase::getModRefInfo(Call, Loc);
133 ObjCARCAAResult ObjCARCAA::run(Function &F, FunctionAnalysisManager &AM) {
134 return ObjCARCAAResult(F.getParent()->getDataLayout());
137 char ObjCARCAAWrapperPass::ID = 0;
138 INITIALIZE_PASS(ObjCARCAAWrapperPass, "objc-arc-aa",
139 "ObjC-ARC-Based Alias Analysis", false, true)
141 ImmutablePass *llvm::createObjCARCAAWrapperPass() {
142 return new ObjCARCAAWrapperPass();
145 ObjCARCAAWrapperPass::ObjCARCAAWrapperPass() : ImmutablePass(ID) {
146 initializeObjCARCAAWrapperPassPass(*PassRegistry::getPassRegistry());
149 bool ObjCARCAAWrapperPass::doInitialization(Module &M) {
150 Result.reset(new ObjCARCAAResult(M.getDataLayout()));
151 return false;
154 bool ObjCARCAAWrapperPass::doFinalization(Module &M) {
155 Result.reset();
156 return false;
159 void ObjCARCAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
160 AU.setPreservesAll();