Coverage Report

Created: 2019-02-20 07:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Analysis/ObjCARCAnalysisUtils.h
Line
Count
Source (jump to first uncovered line)
1
//===- ObjCARCAnalysisUtils.h - ObjC ARC Analysis Utilities -----*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
/// \file
9
/// This file defines common analysis utilities used by the ObjC ARC Optimizer.
10
/// ARC stands for Automatic Reference Counting and is a system for managing
11
/// reference counts for objects in Objective C.
12
///
13
/// WARNING: This file knows about certain library functions. It recognizes them
14
/// by name, and hardwires knowledge of their semantics.
15
///
16
/// WARNING: This file knows about how certain Objective-C library functions are
17
/// used. Naive LLVM IR transformations which would otherwise be
18
/// behavior-preserving may break these assumptions.
19
///
20
//===----------------------------------------------------------------------===//
21
22
#ifndef LLVM_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H
23
#define LLVM_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H
24
25
#include "llvm/ADT/Optional.h"
26
#include "llvm/ADT/StringSwitch.h"
27
#include "llvm/Analysis/AliasAnalysis.h"
28
#include "llvm/Analysis/ObjCARCInstKind.h"
29
#include "llvm/Analysis/Passes.h"
30
#include "llvm/Analysis/ValueTracking.h"
31
#include "llvm/IR/CallSite.h"
32
#include "llvm/IR/Constants.h"
33
#include "llvm/IR/InstIterator.h"
34
#include "llvm/IR/LLVMContext.h"
35
#include "llvm/IR/Module.h"
36
#include "llvm/IR/ValueHandle.h"
37
#include "llvm/Pass.h"
38
39
namespace llvm {
40
class raw_ostream;
41
}
42
43
namespace llvm {
44
namespace objcarc {
45
46
/// A handy option to enable/disable all ARC Optimizations.
47
extern bool EnableARCOpts;
48
49
/// Test if the given module looks interesting to run ARC optimization
50
/// on.
51
11.9k
inline bool ModuleHasARC(const Module &M) {
52
11.9k
  return
53
11.9k
    M.getNamedValue("llvm.objc.retain") ||
54
11.9k
    
M.getNamedValue("llvm.objc.release")11.8k
||
55
11.9k
    
M.getNamedValue("llvm.objc.autorelease")11.8k
||
56
11.9k
    
M.getNamedValue("llvm.objc.retainAutoreleasedReturnValue")11.8k
||
57
11.9k
    
M.getNamedValue("llvm.objc.unsafeClaimAutoreleasedReturnValue")11.8k
||
58
11.9k
    
M.getNamedValue("llvm.objc.retainBlock")11.8k
||
59
11.9k
    
M.getNamedValue("llvm.objc.autoreleaseReturnValue")11.8k
||
60
11.9k
    
M.getNamedValue("llvm.objc.autoreleasePoolPush")11.8k
||
61
11.9k
    
M.getNamedValue("llvm.objc.loadWeakRetained")11.8k
||
62
11.9k
    
M.getNamedValue("llvm.objc.loadWeak")11.8k
||
63
11.9k
    
M.getNamedValue("llvm.objc.destroyWeak")11.8k
||
64
11.9k
    
M.getNamedValue("llvm.objc.storeWeak")11.8k
||
65
11.9k
    
M.getNamedValue("llvm.objc.initWeak")11.8k
||
66
11.9k
    
M.getNamedValue("llvm.objc.moveWeak")11.8k
||
67
11.9k
    
M.getNamedValue("llvm.objc.copyWeak")11.8k
||
68
11.9k
    
M.getNamedValue("llvm.objc.retainedObject")11.8k
||
69
11.9k
    
M.getNamedValue("llvm.objc.unretainedObject")11.8k
||
70
11.9k
    
M.getNamedValue("llvm.objc.unretainedPointer")11.8k
||
71
11.9k
    
M.getNamedValue("llvm.objc.clang.arc.use")11.8k
;
72
11.9k
}
73
74
/// This is a wrapper around getUnderlyingObject which also knows how to
75
/// look through objc_retain and objc_autorelease calls, which we know to return
76
/// their argument verbatim.
77
inline const Value *GetUnderlyingObjCPtr(const Value *V,
78
4.82k
                                                const DataLayout &DL) {
79
4.88k
  for (;;) {
80
4.88k
    V = GetUnderlyingObject(V, DL);
81
4.88k
    if (!IsForwarding(GetBasicARCInstKind(V)))
82
4.82k
      break;
83
61
    V = cast<CallInst>(V)->getArgOperand(0);
84
61
  }
85
4.82k
86
4.82k
  return V;
87
4.82k
}
88
89
/// A wrapper for GetUnderlyingObjCPtr used for results memoization.
90
inline const Value *
91
GetUnderlyingObjCPtrCached(const Value *V, const DataLayout &DL,
92
2.80k
                           DenseMap<const Value *, WeakTrackingVH> &Cache) {
93
2.80k
  if (auto InCache = Cache.lookup(V))
94
2.26k
    return InCache;
95
538
96
538
  const Value *Computed = GetUnderlyingObjCPtr(V, DL);
97
538
  Cache[V] = const_cast<Value *>(Computed);
98
538
  return Computed;
99
538
}
100
101
/// The RCIdentity root of a value \p V is a dominating value U for which
102
/// retaining or releasing U is equivalent to retaining or releasing V. In other
103
/// words, ARC operations on \p V are equivalent to ARC operations on \p U.
104
///
105
/// We use this in the ARC optimizer to make it easier to match up ARC
106
/// operations by always mapping ARC operations to RCIdentityRoots instead of
107
/// pointers themselves.
108
///
109
/// The two ways that we see RCIdentical values in ObjC are via:
110
///
111
///   1. PointerCasts
112
///   2. Forwarding Calls that return their argument verbatim.
113
///
114
/// Thus this function strips off pointer casts and forwarding calls. *NOTE*
115
/// This implies that two RCIdentical values must alias.
116
7.23k
inline const Value *GetRCIdentityRoot(const Value *V) {
117
7.72k
  for (;;) {
118
7.72k
    V = V->stripPointerCasts();
119
7.72k
    if (!IsForwarding(GetBasicARCInstKind(V)))
120
7.23k
      break;
121
486
    V = cast<CallInst>(V)->getArgOperand(0);
122
486
  }
123
7.23k
  return V;
124
7.23k
}
125
126
/// Helper which calls const Value *GetRCIdentityRoot(const Value *V) and just
127
/// casts away the const of the result. For documentation about what an
128
/// RCIdentityRoot (and by extension GetRCIdentityRoot is) look at that
129
/// function.
130
2.57k
inline Value *GetRCIdentityRoot(Value *V) {
131
2.57k
  return const_cast<Value *>(GetRCIdentityRoot((const Value *)V));
132
2.57k
}
133
134
/// Assuming the given instruction is one of the special calls such as
135
/// objc_retain or objc_release, return the RCIdentity root of the argument of
136
/// the call.
137
2.45k
inline Value *GetArgRCIdentityRoot(Value *Inst) {
138
2.45k
  return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0));
139
2.45k
}
140
141
889
inline bool IsNullOrUndef(const Value *V) {
142
889
  return isa<ConstantPointerNull>(V) || 
isa<UndefValue>(V)834
;
143
889
}
144
145
74
inline bool IsNoopInstruction(const Instruction *I) {
146
74
  return isa<BitCastInst>(I) ||
147
74
    
(60
isa<GetElementPtrInst>(I)60
&&
148
60
     
cast<GetElementPtrInst>(I)->hasAllZeroIndices()0
);
149
74
}
150
151
/// Test whether the given value is possible a retainable object pointer.
152
4.33k
inline bool IsPotentialRetainableObjPtr(const Value *Op) {
153
4.33k
  // Pointers to static or stack storage are not valid retainable object
154
4.33k
  // pointers.
155
4.33k
  if (isa<Constant>(Op) || 
isa<AllocaInst>(Op)2.55k
)
156
1.89k
    return false;
157
2.43k
  // Special arguments can not be a valid retainable object pointer.
158
2.43k
  if (const Argument *Arg = dyn_cast<Argument>(Op))
159
575
    if (Arg->hasByValAttr() ||
160
575
        Arg->hasInAllocaAttr() ||
161
575
        Arg->hasNestAttr() ||
162
575
        Arg->hasStructRetAttr())
163
0
      return false;
164
2.43k
  // Only consider values with pointer types.
165
2.43k
  //
166
2.43k
  // It seemes intuitive to exclude function pointer types as well, since
167
2.43k
  // functions are never retainable object pointers, however clang occasionally
168
2.43k
  // bitcasts retainable object pointers to function-pointer type temporarily.
169
2.43k
  PointerType *Ty = dyn_cast<PointerType>(Op->getType());
170
2.43k
  if (!Ty)
171
146
    return false;
172
2.28k
  // Conservatively assume anything else is a potential retainable object
173
2.28k
  // pointer.
174
2.28k
  return true;
175
2.28k
}
176
177
inline bool IsPotentialRetainableObjPtr(const Value *Op,
178
1.07k
                                               AliasAnalysis &AA) {
179
1.07k
  // First make the rudimentary check.
180
1.07k
  if (!IsPotentialRetainableObjPtr(Op))
181
127
    return false;
182
949
183
949
  // Objects in constant memory are not reference-counted.
184
949
  if (AA.pointsToConstantMemory(Op))
185
0
    return false;
186
949
187
949
  // Pointers in constant memory are not pointing to reference-counted objects.
188
949
  if (const LoadInst *LI = dyn_cast<LoadInst>(Op))
189
207
    if (AA.pointsToConstantMemory(LI->getPointerOperand()))
190
17
      return false;
191
932
192
932
  // Otherwise assume the worst.
193
932
  return true;
194
932
}
195
196
/// Helper for GetARCInstKind. Determines what kind of construct CS
197
/// is.
198
1.16k
inline ARCInstKind GetCallSiteClass(ImmutableCallSite CS) {
199
1.16k
  for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
200
2.20k
       I != E; 
++I1.03k
)
201
1.58k
    if (IsPotentialRetainableObjPtr(*I))
202
556
      return CS.onlyReadsMemory() ? 
ARCInstKind::User1
:
ARCInstKind::CallOrUser555
;
203
1.16k
204
1.16k
  
return CS.onlyReadsMemory() 611
?
ARCInstKind::None0
:
ARCInstKind::Call611
;
205
1.16k
}
206
207
/// Return true if this value refers to a distinct and identifiable
208
/// object.
209
///
210
/// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses
211
/// special knowledge of ObjC conventions.
212
1.31k
inline bool IsObjCIdentifiedObject(const Value *V) {
213
1.31k
  // Assume that call results and arguments have their own "provenance".
214
1.31k
  // Constants (including GlobalVariables) and Allocas are never
215
1.31k
  // reference-counted.
216
1.31k
  if (isa<CallInst>(V) || 
isa<InvokeInst>(V)997
||
217
1.31k
      
isa<Argument>(V)991
||
isa<Constant>(V)758
||
218
1.31k
      
isa<AllocaInst>(V)632
)
219
714
    return true;
220
596
221
596
  if (const LoadInst *LI = dyn_cast<LoadInst>(V)) {
222
429
    const Value *Pointer =
223
429
      GetRCIdentityRoot(LI->getPointerOperand());
224
429
    if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) {
225
264
      // A constant pointer can't be pointing to an object on the heap. It may
226
264
      // be reference-counted, but it won't be deleted.
227
264
      if (GV->isConstant())
228
4
        return true;
229
260
      StringRef Name = GV->getName();
230
260
      // These special variables are known to hold values which are not
231
260
      // reference-counted pointers.
232
260
      if (Name.startswith("\01l_objc_msgSend_fixup_"))
233
18
        return true;
234
242
235
242
      StringRef Section = GV->getSection();
236
242
      if (Section.find("__message_refs") != StringRef::npos ||
237
242
          
Section.find("__objc_classrefs") != StringRef::npos224
||
238
242
          
Section.find("__objc_superrefs") != StringRef::npos159
||
239
242
          
Section.find("__objc_methname") != StringRef::npos141
||
240
242
          
Section.find("__cstring") != StringRef::npos123
)
241
137
        return true;
242
437
    }
243
429
  }
244
437
245
437
  return false;
246
437
}
247
248
enum class ARCMDKindID {
249
  ImpreciseRelease,
250
  CopyOnEscape,
251
  NoObjCARCExceptions,
252
};
253
254
/// A cache of MDKinds used by various ARC optimizations.
255
class ARCMDKindCache {
256
  Module *M;
257
258
  /// The Metadata Kind for clang.imprecise_release metadata.
259
  llvm::Optional<unsigned> ImpreciseReleaseMDKind;
260
261
  /// The Metadata Kind for clang.arc.copy_on_escape metadata.
262
  llvm::Optional<unsigned> CopyOnEscapeMDKind;
263
264
  /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
265
  llvm::Optional<unsigned> NoObjCARCExceptionsMDKind;
266
267
public:
268
31
  void init(Module *Mod) {
269
31
    M = Mod;
270
31
    ImpreciseReleaseMDKind = NoneType::None;
271
31
    CopyOnEscapeMDKind = NoneType::None;
272
31
    NoObjCARCExceptionsMDKind = NoneType::None;
273
31
  }
274
275
1.19k
  unsigned get(ARCMDKindID ID) {
276
1.19k
    switch (ID) {
277
1.19k
    case ARCMDKindID::ImpreciseRelease:
278
1.00k
      if (!ImpreciseReleaseMDKind)
279
28
        ImpreciseReleaseMDKind =
280
28
            M->getContext().getMDKindID("clang.imprecise_release");
281
1.00k
      return *ImpreciseReleaseMDKind;
282
1.19k
    case ARCMDKindID::CopyOnEscape:
283
0
      if (!CopyOnEscapeMDKind)
284
0
        CopyOnEscapeMDKind =
285
0
            M->getContext().getMDKindID("clang.arc.copy_on_escape");
286
0
      return *CopyOnEscapeMDKind;
287
1.19k
    case ARCMDKindID::NoObjCARCExceptions:
288
197
      if (!NoObjCARCExceptionsMDKind)
289
25
        NoObjCARCExceptionsMDKind =
290
25
            M->getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
291
197
      return *NoObjCARCExceptionsMDKind;
292
0
    }
293
0
    llvm_unreachable("Covered switch isn't covered?!");
294
0
  }
295
};
296
297
} // end namespace objcarc
298
} // end namespace llvm
299
300
#endif