Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Analysis/ObjCARCAliasAnalysis.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ObjCARCAliasAnalysis.cpp - ObjC ARC Optimization -------------------===//
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 a simple ARC-aware AliasAnalysis using special knowledge
10
/// of Objective C to enhance other optimization passes which rely on the Alias
11
/// Analysis infrastructure.
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
/// TODO: Theoretically we could check for dependencies between objc_* calls
21
/// and FMRB_OnlyAccessesArgumentPointees calls or other well-behaved calls.
22
///
23
//===----------------------------------------------------------------------===//
24
25
#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
26
#include "llvm/Analysis/ObjCARCAnalysisUtils.h"
27
#include "llvm/IR/Function.h"
28
#include "llvm/IR/Instruction.h"
29
#include "llvm/IR/Value.h"
30
#include "llvm/InitializePasses.h"
31
#include "llvm/PassAnalysisSupport.h"
32
#include "llvm/PassSupport.h"
33
34
#define DEBUG_TYPE "objc-arc-aa"
35
36
using namespace llvm;
37
using namespace llvm::objcarc;
38
39
AliasResult ObjCARCAAResult::alias(const MemoryLocation &LocA,
40
                                   const MemoryLocation &LocB,
41
1.46k
                                   AAQueryInfo &AAQI) {
42
1.46k
  if (!EnableARCOpts)
43
0
    return AAResultBase::alias(LocA, LocB, AAQI);
44
1.46k
45
1.46k
  // First, strip off no-ops, including ObjC-specific no-ops, and try making a
46
1.46k
  // precise alias query.
47
1.46k
  const Value *SA = GetRCIdentityRoot(LocA.Ptr);
48
1.46k
  const Value *SB = GetRCIdentityRoot(LocB.Ptr);
49
1.46k
  AliasResult Result =
50
1.46k
      AAResultBase::alias(MemoryLocation(SA, LocA.Size, LocA.AATags),
51
1.46k
                          MemoryLocation(SB, LocB.Size, LocB.AATags), AAQI);
52
1.46k
  if (Result != MayAlias)
53
0
    return Result;
54
1.46k
55
1.46k
  // If that failed, climb to the underlying object, including climbing through
56
1.46k
  // ObjC-specific no-ops, and try making an imprecise alias query.
57
1.46k
  const Value *UA = GetUnderlyingObjCPtr(SA, DL);
58
1.46k
  const Value *UB = GetUnderlyingObjCPtr(SB, DL);
59
1.46k
  if (UA != SA || 
UB != SB1.33k
) {
60
170
    Result = AAResultBase::alias(MemoryLocation(UA), MemoryLocation(UB), AAQI);
61
170
    // We can't use MustAlias or PartialAlias results here because
62
170
    // GetUnderlyingObjCPtr may return an offsetted pointer value.
63
170
    if (Result == NoAlias)
64
0
      return NoAlias;
65
1.46k
  }
66
1.46k
67
1.46k
  // If that failed, fail. We don't need to chain here, since that's covered
68
1.46k
  // by the earlier precise query.
69
1.46k
  return MayAlias;
70
1.46k
}
71
72
bool ObjCARCAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
73
3.26k
                                             AAQueryInfo &AAQI, bool OrLocal) {
74
3.26k
  if (!EnableARCOpts)
75
0
    return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
76
3.26k
77
3.26k
  // First, strip off no-ops, including ObjC-specific no-ops, and try making
78
3.26k
  // a precise alias query.
79
3.26k
  const Value *S = GetRCIdentityRoot(Loc.Ptr);
80
3.26k
  if (AAResultBase::pointsToConstantMemory(
81
3.26k
          MemoryLocation(S, Loc.Size, Loc.AATags), AAQI, OrLocal))
82
0
    return true;
83
3.26k
84
3.26k
  // If that failed, climb to the underlying object, including climbing through
85
3.26k
  // ObjC-specific no-ops, and try making an imprecise alias query.
86
3.26k
  const Value *U = GetUnderlyingObjCPtr(S, DL);
87
3.26k
  if (U != S)
88
1.59k
    return AAResultBase::pointsToConstantMemory(MemoryLocation(U), AAQI,
89
1.59k
                                                OrLocal);
90
1.67k
91
1.67k
  // If that failed, fail. We don't need to chain here, since that's covered
92
1.67k
  // by the earlier precise query.
93
1.67k
  return false;
94
1.67k
}
95
96
3.68k
FunctionModRefBehavior ObjCARCAAResult::getModRefBehavior(const Function *F) {
97
3.68k
  if (!EnableARCOpts)
98
0
    return AAResultBase::getModRefBehavior(F);
99
3.68k
100
3.68k
  switch (GetFunctionClass(F)) {
101
3.68k
  case ARCInstKind::NoopCast:
102
0
    return FMRB_DoesNotAccessMemory;
103
3.68k
  default:
104
3.68k
    break;
105
3.68k
  }
106
3.68k
107
3.68k
  return AAResultBase::getModRefBehavior(F);
108
3.68k
}
109
110
ModRefInfo ObjCARCAAResult::getModRefInfo(const CallBase *Call,
111
                                          const MemoryLocation &Loc,
112
672
                                          AAQueryInfo &AAQI) {
113
672
  if (!EnableARCOpts)
114
0
    return AAResultBase::getModRefInfo(Call, Loc, AAQI);
115
672
116
672
  switch (GetBasicARCInstKind(Call)) {
117
672
  case ARCInstKind::Retain:
118
120
  case ARCInstKind::RetainRV:
119
120
  case ARCInstKind::Autorelease:
120
120
  case ARCInstKind::AutoreleaseRV:
121
120
  case ARCInstKind::NoopCast:
122
120
  case ARCInstKind::AutoreleasepoolPush:
123
120
  case ARCInstKind::FusedRetainAutorelease:
124
120
  case ARCInstKind::FusedRetainAutoreleaseRV:
125
120
    // These functions don't access any memory visible to the compiler.
126
120
    // Note that this doesn't include objc_retainBlock, because it updates
127
120
    // pointers when it copies block data.
128
120
    return ModRefInfo::NoModRef;
129
552
  default:
130
552
    break;
131
552
  }
132
552
133
552
  return AAResultBase::getModRefInfo(Call, Loc, AAQI);
134
552
}
135
136
0
ObjCARCAAResult ObjCARCAA::run(Function &F, FunctionAnalysisManager &AM) {
137
0
  return ObjCARCAAResult(F.getParent()->getDataLayout());
138
0
}
139
140
char ObjCARCAAWrapperPass::ID = 0;
141
INITIALIZE_PASS(ObjCARCAAWrapperPass, "objc-arc-aa",
142
                "ObjC-ARC-Based Alias Analysis", false, true)
143
144
0
ImmutablePass *llvm::createObjCARCAAWrapperPass() {
145
0
  return new ObjCARCAAWrapperPass();
146
0
}
147
148
37
ObjCARCAAWrapperPass::ObjCARCAAWrapperPass() : ImmutablePass(ID) {
149
37
  initializeObjCARCAAWrapperPassPass(*PassRegistry::getPassRegistry());
150
37
}
151
152
37
bool ObjCARCAAWrapperPass::doInitialization(Module &M) {
153
37
  Result.reset(new ObjCARCAAResult(M.getDataLayout()));
154
37
  return false;
155
37
}
156
157
37
bool ObjCARCAAWrapperPass::doFinalization(Module &M) {
158
37
  Result.reset();
159
37
  return false;
160
37
}
161
162
37
void ObjCARCAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
163
37
  AU.setPreservesAll();
164
37
}