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 //===----------------------------------------------------------------------===//
23 #include "llvm/Analysis/ObjCARCAliasAnalysis.h"
24 #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/Instruction.h"
27 #include "llvm/IR/Value.h"
28 #include "llvm/InitializePasses.h"
29 #include "llvm/PassAnalysisSupport.h"
30 #include "llvm/PassSupport.h"
32 #define DEBUG_TYPE "objc-arc-aa"
35 using namespace llvm::objcarc;
37 // Register this pass...
38 char ObjCARCAliasAnalysis::ID = 0;
39 INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa",
40 "ObjC-ARC-Based Alias Analysis", false, true, false)
42 ImmutablePass *llvm::createObjCARCAliasAnalysisPass() {
43 return new ObjCARCAliasAnalysis();
46 bool ObjCARCAliasAnalysis::doInitialization(Module &M) {
47 InitializeAliasAnalysis(this, &M.getDataLayout());
51 void ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
53 AliasAnalysis::getAnalysisUsage(AU);
56 AliasResult ObjCARCAliasAnalysis::alias(const MemoryLocation &LocA,
57 const MemoryLocation &LocB) {
59 return AliasAnalysis::alias(LocA, LocB);
61 // First, strip off no-ops, including ObjC-specific no-ops, and try making a
62 // precise alias query.
63 const Value *SA = GetRCIdentityRoot(LocA.Ptr);
64 const Value *SB = GetRCIdentityRoot(LocB.Ptr);
66 AliasAnalysis::alias(MemoryLocation(SA, LocA.Size, LocA.AATags),
67 MemoryLocation(SB, LocB.Size, LocB.AATags));
68 if (Result != MayAlias)
71 // If that failed, climb to the underlying object, including climbing through
72 // ObjC-specific no-ops, and try making an imprecise alias query.
73 const Value *UA = GetUnderlyingObjCPtr(SA, *DL);
74 const Value *UB = GetUnderlyingObjCPtr(SB, *DL);
75 if (UA != SA || UB != SB) {
76 Result = AliasAnalysis::alias(MemoryLocation(UA), MemoryLocation(UB));
77 // We can't use MustAlias or PartialAlias results here because
78 // GetUnderlyingObjCPtr may return an offsetted pointer value.
79 if (Result == NoAlias)
83 // If that failed, fail. We don't need to chain here, since that's covered
84 // by the earlier precise query.
88 bool ObjCARCAliasAnalysis::pointsToConstantMemory(const MemoryLocation &Loc,
91 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
93 // First, strip off no-ops, including ObjC-specific no-ops, and try making
94 // a precise alias query.
95 const Value *S = GetRCIdentityRoot(Loc.Ptr);
96 if (AliasAnalysis::pointsToConstantMemory(
97 MemoryLocation(S, Loc.Size, Loc.AATags), OrLocal))
100 // If that failed, climb to the underlying object, including climbing through
101 // ObjC-specific no-ops, and try making an imprecise alias query.
102 const Value *U = GetUnderlyingObjCPtr(S, *DL);
104 return AliasAnalysis::pointsToConstantMemory(MemoryLocation(U), OrLocal);
106 // If that failed, fail. We don't need to chain here, since that's covered
107 // by the earlier precise query.
111 FunctionModRefBehavior
112 ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
113 // We have nothing to do. Just chain to the next AliasAnalysis.
114 return AliasAnalysis::getModRefBehavior(CS);
117 FunctionModRefBehavior
118 ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) {
120 return AliasAnalysis::getModRefBehavior(F);
122 switch (GetFunctionClass(F)) {
123 case ARCInstKind::NoopCast:
124 return FMRB_DoesNotAccessMemory;
129 return AliasAnalysis::getModRefBehavior(F);
132 ModRefInfo ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
133 const MemoryLocation &Loc) {
135 return AliasAnalysis::getModRefInfo(CS, Loc);
137 switch (GetBasicARCInstKind(CS.getInstruction())) {
138 case ARCInstKind::Retain:
139 case ARCInstKind::RetainRV:
140 case ARCInstKind::Autorelease:
141 case ARCInstKind::AutoreleaseRV:
142 case ARCInstKind::NoopCast:
143 case ARCInstKind::AutoreleasepoolPush:
144 case ARCInstKind::FusedRetainAutorelease:
145 case ARCInstKind::FusedRetainAutoreleaseRV:
146 // These functions don't access any memory visible to the compiler.
147 // Note that this doesn't include objc_retainBlock, because it updates
148 // pointers when it copies block data.
154 return AliasAnalysis::getModRefInfo(CS, Loc);
157 ModRefInfo ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
158 ImmutableCallSite CS2) {
159 // TODO: Theoretically we could check for dependencies between objc_* calls
160 // and FMRB_OnlyAccessesArgumentPointees calls or other well-behaved calls.
161 return AliasAnalysis::getModRefInfo(CS1, CS2);