1 //===- ObjCARCAliasAnalysis.cpp - ObjC ARC Optimization -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 /// WARNING: This file knows about certain library functions. It recognizes them
15 /// by name, and hardwires knowledge of their semantics.
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.
21 //===----------------------------------------------------------------------===//
24 #include "ObjCARCAliasAnalysis.h"
25 #include "llvm/IR/Instruction.h"
26 #include "llvm/InitializePasses.h"
27 #include "llvm/PassAnalysisSupport.h"
28 #include "llvm/PassSupport.h"
30 #define DEBUG_TYPE "objc-arc-aa"
38 using namespace llvm::objcarc;
40 // Register this pass...
41 char ObjCARCAliasAnalysis::ID = 0;
42 INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa",
43 "ObjC-ARC-Based Alias Analysis", false, true, false)
45 ImmutablePass *llvm::createObjCARCAliasAnalysisPass() {
46 return new ObjCARCAliasAnalysis();
49 bool ObjCARCAliasAnalysis::doInitialization(Module &M) {
50 InitializeAliasAnalysis(this, &M.getDataLayout());
55 ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
57 AliasAnalysis::getAnalysisUsage(AU);
60 AliasResult ObjCARCAliasAnalysis::alias(const MemoryLocation &LocA,
61 const MemoryLocation &LocB) {
63 return AliasAnalysis::alias(LocA, LocB);
65 // First, strip off no-ops, including ObjC-specific no-ops, and try making a
66 // precise alias query.
67 const Value *SA = GetRCIdentityRoot(LocA.Ptr);
68 const Value *SB = GetRCIdentityRoot(LocB.Ptr);
70 AliasAnalysis::alias(MemoryLocation(SA, LocA.Size, LocA.AATags),
71 MemoryLocation(SB, LocB.Size, LocB.AATags));
72 if (Result != MayAlias)
75 // If that failed, climb to the underlying object, including climbing through
76 // ObjC-specific no-ops, and try making an imprecise alias query.
77 const Value *UA = GetUnderlyingObjCPtr(SA, *DL);
78 const Value *UB = GetUnderlyingObjCPtr(SB, *DL);
79 if (UA != SA || UB != SB) {
80 Result = AliasAnalysis::alias(MemoryLocation(UA), MemoryLocation(UB));
81 // We can't use MustAlias or PartialAlias results here because
82 // GetUnderlyingObjCPtr may return an offsetted pointer value.
83 if (Result == NoAlias)
87 // If that failed, fail. We don't need to chain here, since that's covered
88 // by the earlier precise query.
92 bool ObjCARCAliasAnalysis::pointsToConstantMemory(const MemoryLocation &Loc,
95 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
97 // First, strip off no-ops, including ObjC-specific no-ops, and try making
98 // a precise alias query.
99 const Value *S = GetRCIdentityRoot(Loc.Ptr);
100 if (AliasAnalysis::pointsToConstantMemory(
101 MemoryLocation(S, Loc.Size, Loc.AATags), OrLocal))
104 // If that failed, climb to the underlying object, including climbing through
105 // ObjC-specific no-ops, and try making an imprecise alias query.
106 const Value *U = GetUnderlyingObjCPtr(S, *DL);
108 return AliasAnalysis::pointsToConstantMemory(MemoryLocation(U), OrLocal);
110 // If that failed, fail. We don't need to chain here, since that's covered
111 // by the earlier precise query.
115 FunctionModRefBehavior
116 ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
117 // We have nothing to do. Just chain to the next AliasAnalysis.
118 return AliasAnalysis::getModRefBehavior(CS);
121 FunctionModRefBehavior
122 ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) {
124 return AliasAnalysis::getModRefBehavior(F);
126 switch (GetFunctionClass(F)) {
127 case ARCInstKind::NoopCast:
128 return FMRB_DoesNotAccessMemory;
133 return AliasAnalysis::getModRefBehavior(F);
136 ModRefInfo ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
137 const MemoryLocation &Loc) {
139 return AliasAnalysis::getModRefInfo(CS, Loc);
141 switch (GetBasicARCInstKind(CS.getInstruction())) {
142 case ARCInstKind::Retain:
143 case ARCInstKind::RetainRV:
144 case ARCInstKind::Autorelease:
145 case ARCInstKind::AutoreleaseRV:
146 case ARCInstKind::NoopCast:
147 case ARCInstKind::AutoreleasepoolPush:
148 case ARCInstKind::FusedRetainAutorelease:
149 case ARCInstKind::FusedRetainAutoreleaseRV:
150 // These functions don't access any memory visible to the compiler.
151 // Note that this doesn't include objc_retainBlock, because it updates
152 // pointers when it copies block data.
158 return AliasAnalysis::getModRefInfo(CS, Loc);
161 ModRefInfo ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
162 ImmutableCallSite CS2) {
163 // TODO: Theoretically we could check for dependencies between objc_* calls
164 // and FMRB_OnlyAccessesArgumentPointees calls or other well-behaved calls.
165 return AliasAnalysis::getModRefInfo(CS1, CS2);