blob: 25f660ffe2210ef75ddcb2709e8e2dbef80ee819 [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///
Chandler Carruth7b560d42015-09-09 17:55:00 +000021/// TODO: Theoretically we could check for dependencies between objc_* calls
22/// and FMRB_OnlyAccessesArgumentPointees calls or other well-behaved calls.
23///
Michael Gottesman294e7da2013-01-28 05:51:54 +000024//===----------------------------------------------------------------------===//
25
Chandler Carruth0f792182015-08-20 08:06:03 +000026#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
27#include "llvm/Analysis/ObjCARCAnalysisUtils.h"
Chandler Carruthb4ebdf32015-08-14 03:55:36 +000028#include "llvm/IR/Function.h"
Michael Gottesman294e7da2013-01-28 05:51:54 +000029#include "llvm/IR/Instruction.h"
Chandler Carruthb4ebdf32015-08-14 03:55:36 +000030#include "llvm/IR/Value.h"
Michael Gottesman294e7da2013-01-28 05:51:54 +000031#include "llvm/InitializePasses.h"
32#include "llvm/PassAnalysisSupport.h"
33#include "llvm/PassSupport.h"
34
Chandler Carruth964daaa2014-04-22 02:55:47 +000035#define DEBUG_TYPE "objc-arc-aa"
36
Michael Gottesman294e7da2013-01-28 05:51:54 +000037using namespace llvm;
38using namespace llvm::objcarc;
39
Chandler Carruth7b560d42015-09-09 17:55:00 +000040AliasResult ObjCARCAAResult::alias(const MemoryLocation &LocA,
41 const MemoryLocation &LocB) {
Michael Gottesman294e7da2013-01-28 05:51:54 +000042 if (!EnableARCOpts)
Chandler Carruth7b560d42015-09-09 17:55:00 +000043 return AAResultBase::alias(LocA, LocB);
Michael Gottesman294e7da2013-01-28 05:51:54 +000044
45 // First, strip off no-ops, including ObjC-specific no-ops, and try making a
46 // precise alias query.
Michael Gottesmane5ad66f2015-02-19 00:42:38 +000047 const Value *SA = GetRCIdentityRoot(LocA.Ptr);
48 const Value *SB = GetRCIdentityRoot(LocB.Ptr);
Michael Gottesman294e7da2013-01-28 05:51:54 +000049 AliasResult Result =
Chandler Carruth7b560d42015-09-09 17:55:00 +000050 AAResultBase::alias(MemoryLocation(SA, LocA.Size, LocA.AATags),
51 MemoryLocation(SB, LocB.Size, LocB.AATags));
Michael Gottesman294e7da2013-01-28 05:51:54 +000052 if (Result != 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.
Chandler Carruth7b560d42015-09-09 17:55:00 +000057 const Value *UA = GetUnderlyingObjCPtr(SA, DL);
58 const Value *UB = GetUnderlyingObjCPtr(SB, DL);
Michael Gottesman294e7da2013-01-28 05:51:54 +000059 if (UA != SA || UB != SB) {
Chandler Carruth7b560d42015-09-09 17:55:00 +000060 Result = AAResultBase::alias(MemoryLocation(UA), MemoryLocation(UB));
Michael Gottesman294e7da2013-01-28 05:51:54 +000061 // We can't use MustAlias or PartialAlias results here because
62 // GetUnderlyingObjCPtr may return an offsetted pointer value.
63 if (Result == NoAlias)
64 return NoAlias;
65 }
66
67 // If that failed, fail. We don't need to chain here, since that's covered
68 // by the earlier precise query.
69 return MayAlias;
70}
71
Chandler Carruth7b560d42015-09-09 17:55:00 +000072bool ObjCARCAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
73 bool OrLocal) {
Michael Gottesman294e7da2013-01-28 05:51:54 +000074 if (!EnableARCOpts)
Chandler Carruth7b560d42015-09-09 17:55:00 +000075 return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
Michael Gottesman294e7da2013-01-28 05:51:54 +000076
77 // First, strip off no-ops, including ObjC-specific no-ops, and try making
78 // a precise alias query.
Michael Gottesmane5ad66f2015-02-19 00:42:38 +000079 const Value *S = GetRCIdentityRoot(Loc.Ptr);
Chandler Carruth7b560d42015-09-09 17:55:00 +000080 if (AAResultBase::pointsToConstantMemory(
Chandler Carruthac80dc72015-06-17 07:18:54 +000081 MemoryLocation(S, Loc.Size, Loc.AATags), OrLocal))
Michael Gottesman294e7da2013-01-28 05:51:54 +000082 return true;
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.
Chandler Carruth7b560d42015-09-09 17:55:00 +000086 const Value *U = GetUnderlyingObjCPtr(S, DL);
Michael Gottesman294e7da2013-01-28 05:51:54 +000087 if (U != S)
Chandler Carruth7b560d42015-09-09 17:55:00 +000088 return AAResultBase::pointsToConstantMemory(MemoryLocation(U), OrLocal);
Michael Gottesman294e7da2013-01-28 05:51:54 +000089
90 // If that failed, fail. We don't need to chain here, since that's covered
91 // by the earlier precise query.
92 return false;
93}
94
Chandler Carruth7b560d42015-09-09 17:55:00 +000095FunctionModRefBehavior ObjCARCAAResult::getModRefBehavior(const Function *F) {
Michael Gottesman294e7da2013-01-28 05:51:54 +000096 if (!EnableARCOpts)
Chandler Carruth7b560d42015-09-09 17:55:00 +000097 return AAResultBase::getModRefBehavior(F);
Michael Gottesman294e7da2013-01-28 05:51:54 +000098
99 switch (GetFunctionClass(F)) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000100 case ARCInstKind::NoopCast:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000101 return FMRB_DoesNotAccessMemory;
Michael Gottesman294e7da2013-01-28 05:51:54 +0000102 default:
103 break;
104 }
105
Chandler Carruth7b560d42015-09-09 17:55:00 +0000106 return AAResultBase::getModRefBehavior(F);
Michael Gottesman294e7da2013-01-28 05:51:54 +0000107}
108
Chandler Carruth7b560d42015-09-09 17:55:00 +0000109ModRefInfo ObjCARCAAResult::getModRefInfo(ImmutableCallSite CS,
110 const MemoryLocation &Loc) {
Michael Gottesman294e7da2013-01-28 05:51:54 +0000111 if (!EnableARCOpts)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000112 return AAResultBase::getModRefInfo(CS, Loc);
Michael Gottesman294e7da2013-01-28 05:51:54 +0000113
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000114 switch (GetBasicARCInstKind(CS.getInstruction())) {
115 case ARCInstKind::Retain:
116 case ARCInstKind::RetainRV:
117 case ARCInstKind::Autorelease:
118 case ARCInstKind::AutoreleaseRV:
119 case ARCInstKind::NoopCast:
120 case ARCInstKind::AutoreleasepoolPush:
121 case ARCInstKind::FusedRetainAutorelease:
122 case ARCInstKind::FusedRetainAutoreleaseRV:
Michael Gottesman294e7da2013-01-28 05:51:54 +0000123 // These functions don't access any memory visible to the compiler.
124 // Note that this doesn't include objc_retainBlock, because it updates
125 // pointers when it copies block data.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000126 return MRI_NoModRef;
Michael Gottesman294e7da2013-01-28 05:51:54 +0000127 default:
128 break;
129 }
130
Chandler Carruth7b560d42015-09-09 17:55:00 +0000131 return AAResultBase::getModRefInfo(CS, Loc);
Michael Gottesman294e7da2013-01-28 05:51:54 +0000132}
133
Chandler Carruth7b560d42015-09-09 17:55:00 +0000134ObjCARCAAResult ObjCARCAA::run(Function &F, AnalysisManager<Function> *AM) {
135 return ObjCARCAAResult(F.getParent()->getDataLayout(),
136 AM->getResult<TargetLibraryAnalysis>(F));
137}
138
139char ObjCARCAA::PassID;
140
141char ObjCARCAAWrapperPass::ID = 0;
142INITIALIZE_PASS_BEGIN(ObjCARCAAWrapperPass, "objc-arc-aa",
143 "ObjC-ARC-Based Alias Analysis", false, true)
144INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
145INITIALIZE_PASS_END(ObjCARCAAWrapperPass, "objc-arc-aa",
146 "ObjC-ARC-Based Alias Analysis", false, true)
147
148ImmutablePass *llvm::createObjCARCAAWrapperPass() {
149 return new ObjCARCAAWrapperPass();
150}
151
152ObjCARCAAWrapperPass::ObjCARCAAWrapperPass() : ImmutablePass(ID) {
153 initializeObjCARCAAWrapperPassPass(*PassRegistry::getPassRegistry());
154}
155
156bool ObjCARCAAWrapperPass::doInitialization(Module &M) {
157 Result.reset(new ObjCARCAAResult(
158 M.getDataLayout(), getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
159 return false;
160}
161
162bool ObjCARCAAWrapperPass::doFinalization(Module &M) {
163 Result.reset();
164 return false;
165}
166
167void ObjCARCAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
168 AU.setPreservesAll();
169 AU.addRequired<TargetLibraryInfoWrapperPass>();
Michael Gottesman294e7da2013-01-28 05:51:54 +0000170}