LLVM  3.7.0
ObjCARCAliasAnalysis.cpp
Go to the documentation of this file.
1 //===- ObjCARCAliasAnalysis.cpp - ObjC ARC Optimization -------------------===//
2 //
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 
23 #include "ObjCARC.h"
24 #include "ObjCARCAliasAnalysis.h"
25 #include "llvm/IR/Instruction.h"
26 #include "llvm/InitializePasses.h"
28 #include "llvm/PassSupport.h"
29 
30 #define DEBUG_TYPE "objc-arc-aa"
31 
32 namespace llvm {
33  class Function;
34  class Value;
35 }
36 
37 using namespace llvm;
38 using namespace llvm::objcarc;
39 
40 // Register this pass...
43  "ObjC-ARC-Based Alias Analysis", false, true, false)
44 
46  return new ObjCARCAliasAnalysis();
47 }
48 
49 bool ObjCARCAliasAnalysis::doInitialization(Module &M) {
51  return true;
52 }
53 
54 void
55 ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
56  AU.setPreservesAll();
58 }
59 
60 AliasResult ObjCARCAliasAnalysis::alias(const MemoryLocation &LocA,
61  const MemoryLocation &LocB) {
62  if (!EnableARCOpts)
63  return AliasAnalysis::alias(LocA, LocB);
64 
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);
69  AliasResult Result =
71  MemoryLocation(SB, LocB.Size, LocB.AATags));
72  if (Result != MayAlias)
73  return Result;
74 
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) {
81  // We can't use MustAlias or PartialAlias results here because
82  // GetUnderlyingObjCPtr may return an offsetted pointer value.
83  if (Result == NoAlias)
84  return NoAlias;
85  }
86 
87  // If that failed, fail. We don't need to chain here, since that's covered
88  // by the earlier precise query.
89  return MayAlias;
90 }
91 
92 bool ObjCARCAliasAnalysis::pointsToConstantMemory(const MemoryLocation &Loc,
93  bool OrLocal) {
94  if (!EnableARCOpts)
95  return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
96 
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);
101  MemoryLocation(S, Loc.Size, Loc.AATags), OrLocal))
102  return true;
103 
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);
107  if (U != S)
109 
110  // If that failed, fail. We don't need to chain here, since that's covered
111  // by the earlier precise query.
112  return false;
113 }
114 
116 ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
117  // We have nothing to do. Just chain to the next AliasAnalysis.
119 }
120 
122 ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) {
123  if (!EnableARCOpts)
125 
126  switch (GetFunctionClass(F)) {
128  return DoesNotAccessMemory;
129  default:
130  break;
131  }
132 
134 }
135 
137 ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
138  const MemoryLocation &Loc) {
139  if (!EnableARCOpts)
140  return AliasAnalysis::getModRefInfo(CS, Loc);
141 
142  switch (GetBasicARCInstKind(CS.getInstruction())) {
143  case ARCInstKind::Retain:
151  // These functions don't access any memory visible to the compiler.
152  // Note that this doesn't include objc_retainBlock, because it updates
153  // pointers when it copies block data.
154  return NoModRef;
155  default:
156  break;
157  }
158 
159  return AliasAnalysis::getModRefInfo(CS, Loc);
160 }
161 
163 ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
164  ImmutableCallSite CS2) {
165  // TODO: Theoretically we could check for dependencies between objc_* calls
166  // and OnlyAccessesArgumentPointees calls or other well-behaved calls.
167  return AliasAnalysis::getModRefInfo(CS1, CS2);
168 }
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
InstrTy * getInstruction() const
Definition: CallSite.h:82
ModRefBehavior
ModRefBehavior - Summary of how a function affects memory in the program.
objc_retainedObject, etc.
static ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
Definition: ARCInstKind.h:101
The two locations do not alias at all.
Definition: AliasAnalysis.h:78
F(f)
The two locations may or may not alias. This is the least precise result.
Definition: AliasAnalysis.h:80
virtual bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
pointsToConstantMemory - If the specified memory location is known to be constant, return true.
objc_autoreleaseReturnValue
This file defines common definitions/declarations used by the ObjC ARC Optimizer. ...
objc_retainAutoreleasedReturnValue
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
Definition: ObjCARC.cpp:30
This is a simple alias analysis implementation that uses knowledge of ARC constructs to answer querie...
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:72
Represent the analysis usage information of a pass.
ARCInstKind GetFunctionClass(const Function *F)
Determine if F is one of the special known Functions.
Definition: ARCInstKind.cpp:83
static const Value * GetUnderlyingObjCPtr(const Value *V, const DataLayout &DL)
This is a wrapper around getUnderlyingObject which also knows how to look through objc_retain and obj...
virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS)
getModRefBehavior - Return the behavior when calling the given call site.
This file declares a simple ARC-aware AliasAnalysis using special knowledge of Objective C to enhance...
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:262
virtual AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
Alias Queries...
ImmutablePass * createObjCARCAliasAnalysisPass()
ModRefResult getModRefInfo(const Instruction *I)
getModRefInfo - Return information about whether or not an instruction may read or write memory (with...
void InitializeAliasAnalysis(Pass *P, const DataLayout *DL)
InitializeAliasAnalysis - Subclasses must call this method to initialize the AliasAnalysis interface ...
void setPreservesAll()
Set by analyses that do not transform their input at all.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:418
DoesNotAccessMemory - This function does not perform any non-local loads or stores to memory...
static const Value * GetRCIdentityRoot(const Value *V)
The RCIdentity root of a value V is a dominating value U for which retaining or releasing U is equiva...
LLVM Value Representation.
Definition: Value.h:69
ModRefResult
Simple mod/ref information...
INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis,"objc-arc-aa","ObjC-ARC-Based Alias Analysis", false, true, false) ImmutablePass *llvm
const DataLayout * DL
Definition: AliasAnalysis.h:89
virtual void getAnalysisUsage(AnalysisUsage &AU) const
getAnalysisUsage - All alias analysis implementations should invoke this directly (using AliasAnalysi...
uint64_t Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known...