blob: 94b092cc2aa39e1ed28533b13cd57d29678fb2e4 [file] [log] [blame]
Michael Gottesman6eb95dc2013-07-10 18:49:00 +00001//===- ObjCARCAliasAnalysis.cpp - ObjC ARC Optimization -------------------===//
Michael Gottesman294e7da2013-01-28 05:51:54 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file defines a simple ARC-aware AliasAnalysis using special knowledge
11/// of Objective C to enhance other optimization passes which rely on the Alias
12/// Analysis infrastructure.
13///
14/// WARNING: This file knows about certain library functions. It recognizes them
15/// by name, and hardwires knowledge of their semantics.
16///
17/// WARNING: This file knows about how certain Objective-C library functions are
18/// used. Naive LLVM IR transformations which would otherwise be
19/// behavior-preserving may break these assumptions.
20///
21//===----------------------------------------------------------------------===//
22
Michael Gottesman294e7da2013-01-28 05:51:54 +000023#include "ObjCARC.h"
24#include "ObjCARCAliasAnalysis.h"
Michael Gottesman294e7da2013-01-28 05:51:54 +000025#include "llvm/IR/Instruction.h"
26#include "llvm/InitializePasses.h"
27#include "llvm/PassAnalysisSupport.h"
28#include "llvm/PassSupport.h"
29
Chandler Carruth964daaa2014-04-22 02:55:47 +000030#define DEBUG_TYPE "objc-arc-aa"
31
Michael Gottesman294e7da2013-01-28 05:51:54 +000032namespace llvm {
33 class Function;
34 class Value;
35}
36
37using namespace llvm;
38using namespace llvm::objcarc;
39
40// Register this pass...
41char ObjCARCAliasAnalysis::ID = 0;
42INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa",
43 "ObjC-ARC-Based Alias Analysis", false, true, false)
44
45ImmutablePass *llvm::createObjCARCAliasAnalysisPass() {
46 return new ObjCARCAliasAnalysis();
47}
48
Mehdi Amini46a43552015-03-04 18:43:29 +000049bool ObjCARCAliasAnalysis::doInitialization(Module &M) {
50 InitializeAliasAnalysis(this, &M.getDataLayout());
51 return true;
52}
53
Michael Gottesman294e7da2013-01-28 05:51:54 +000054void
55ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
56 AU.setPreservesAll();
57 AliasAnalysis::getAnalysisUsage(AU);
58}
59
60AliasAnalysis::AliasResult
Chandler Carruthac80dc72015-06-17 07:18:54 +000061ObjCARCAliasAnalysis::alias(const MemoryLocation &LocA,
62 const MemoryLocation &LocB) {
Michael Gottesman294e7da2013-01-28 05:51:54 +000063 if (!EnableARCOpts)
64 return AliasAnalysis::alias(LocA, LocB);
65
66 // First, strip off no-ops, including ObjC-specific no-ops, and try making a
67 // precise alias query.
Michael Gottesmane5ad66f2015-02-19 00:42:38 +000068 const Value *SA = GetRCIdentityRoot(LocA.Ptr);
69 const Value *SB = GetRCIdentityRoot(LocB.Ptr);
Michael Gottesman294e7da2013-01-28 05:51:54 +000070 AliasResult Result =
Chandler Carruthac80dc72015-06-17 07:18:54 +000071 AliasAnalysis::alias(MemoryLocation(SA, LocA.Size, LocA.AATags),
72 MemoryLocation(SB, LocB.Size, LocB.AATags));
Michael Gottesman294e7da2013-01-28 05:51:54 +000073 if (Result != MayAlias)
74 return Result;
75
76 // If that failed, climb to the underlying object, including climbing through
77 // ObjC-specific no-ops, and try making an imprecise alias query.
Mehdi Aminia28d91d2015-03-10 02:37:25 +000078 const Value *UA = GetUnderlyingObjCPtr(SA, *DL);
79 const Value *UB = GetUnderlyingObjCPtr(SB, *DL);
Michael Gottesman294e7da2013-01-28 05:51:54 +000080 if (UA != SA || UB != SB) {
Chandler Carruthac80dc72015-06-17 07:18:54 +000081 Result = AliasAnalysis::alias(MemoryLocation(UA), MemoryLocation(UB));
Michael Gottesman294e7da2013-01-28 05:51:54 +000082 // We can't use MustAlias or PartialAlias results here because
83 // GetUnderlyingObjCPtr may return an offsetted pointer value.
84 if (Result == NoAlias)
85 return NoAlias;
86 }
87
88 // If that failed, fail. We don't need to chain here, since that's covered
89 // by the earlier precise query.
90 return MayAlias;
91}
92
Chandler Carruthac80dc72015-06-17 07:18:54 +000093bool ObjCARCAliasAnalysis::pointsToConstantMemory(const MemoryLocation &Loc,
94 bool OrLocal) {
Michael Gottesman294e7da2013-01-28 05:51:54 +000095 if (!EnableARCOpts)
96 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
97
98 // First, strip off no-ops, including ObjC-specific no-ops, and try making
99 // a precise alias query.
Michael Gottesmane5ad66f2015-02-19 00:42:38 +0000100 const Value *S = GetRCIdentityRoot(Loc.Ptr);
Chandler Carruthac80dc72015-06-17 07:18:54 +0000101 if (AliasAnalysis::pointsToConstantMemory(
102 MemoryLocation(S, Loc.Size, Loc.AATags), OrLocal))
Michael Gottesman294e7da2013-01-28 05:51:54 +0000103 return true;
104
105 // If that failed, climb to the underlying object, including climbing through
106 // ObjC-specific no-ops, and try making an imprecise alias query.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000107 const Value *U = GetUnderlyingObjCPtr(S, *DL);
Michael Gottesman294e7da2013-01-28 05:51:54 +0000108 if (U != S)
Chandler Carruthac80dc72015-06-17 07:18:54 +0000109 return AliasAnalysis::pointsToConstantMemory(MemoryLocation(U), OrLocal);
Michael Gottesman294e7da2013-01-28 05:51:54 +0000110
111 // If that failed, fail. We don't need to chain here, since that's covered
112 // by the earlier precise query.
113 return false;
114}
115
116AliasAnalysis::ModRefBehavior
117ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
118 // We have nothing to do. Just chain to the next AliasAnalysis.
119 return AliasAnalysis::getModRefBehavior(CS);
120}
121
122AliasAnalysis::ModRefBehavior
123ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) {
124 if (!EnableARCOpts)
125 return AliasAnalysis::getModRefBehavior(F);
126
127 switch (GetFunctionClass(F)) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000128 case ARCInstKind::NoopCast:
Michael Gottesman294e7da2013-01-28 05:51:54 +0000129 return DoesNotAccessMemory;
130 default:
131 break;
132 }
133
134 return AliasAnalysis::getModRefBehavior(F);
135}
136
137AliasAnalysis::ModRefResult
Chandler Carruthac80dc72015-06-17 07:18:54 +0000138ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
139 const MemoryLocation &Loc) {
Michael Gottesman294e7da2013-01-28 05:51:54 +0000140 if (!EnableARCOpts)
141 return AliasAnalysis::getModRefInfo(CS, Loc);
142
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000143 switch (GetBasicARCInstKind(CS.getInstruction())) {
144 case ARCInstKind::Retain:
145 case ARCInstKind::RetainRV:
146 case ARCInstKind::Autorelease:
147 case ARCInstKind::AutoreleaseRV:
148 case ARCInstKind::NoopCast:
149 case ARCInstKind::AutoreleasepoolPush:
150 case ARCInstKind::FusedRetainAutorelease:
151 case ARCInstKind::FusedRetainAutoreleaseRV:
Michael Gottesman294e7da2013-01-28 05:51:54 +0000152 // These functions don't access any memory visible to the compiler.
153 // Note that this doesn't include objc_retainBlock, because it updates
154 // pointers when it copies block data.
155 return NoModRef;
156 default:
157 break;
158 }
159
160 return AliasAnalysis::getModRefInfo(CS, Loc);
161}
162
163AliasAnalysis::ModRefResult
164ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
165 ImmutableCallSite CS2) {
166 // TODO: Theoretically we could check for dependencies between objc_* calls
167 // and OnlyAccessesArgumentPointees calls or other well-behaved calls.
168 return AliasAnalysis::getModRefInfo(CS1, CS2);
169}