LLVM 20.0.0git
ProvenanceAnalysis.cpp
Go to the documentation of this file.
1//===- ProvenanceAnalysis.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//
9/// \file
10///
11/// This file defines a special form of Alias Analysis called ``Provenance
12/// Analysis''. The word ``provenance'' refers to the history of the ownership
13/// of an object. Thus ``Provenance Analysis'' is an analysis which attempts to
14/// use various techniques to determine if locally
15///
16/// WARNING: This file knows about certain library functions. It recognizes them
17/// by name, and hardwires knowledge of their semantics.
18///
19/// WARNING: This file knows about how certain Objective-C library functions are
20/// used. Naive LLVM IR transformations which would otherwise be
21/// behavior-preserving may break these assumptions.
22//
23//===----------------------------------------------------------------------===//
24
25#include "ProvenanceAnalysis.h"
31#include "llvm/IR/Use.h"
32#include "llvm/IR/User.h"
33#include "llvm/IR/Value.h"
35#include <utility>
36
37using namespace llvm;
38using namespace llvm::objcarc;
39
40bool ProvenanceAnalysis::relatedSelect(const SelectInst *A,
41 const Value *B) {
42 // If the values are Selects with the same condition, we can do a more precise
43 // check: just check for relations between the values on corresponding arms.
44 if (const SelectInst *SB = dyn_cast<SelectInst>(B))
45 if (A->getCondition() == SB->getCondition())
46 return related(A->getTrueValue(), SB->getTrueValue()) ||
47 related(A->getFalseValue(), SB->getFalseValue());
48
49 // Check both arms of the Select node individually.
50 return related(A->getTrueValue(), B) || related(A->getFalseValue(), B);
51}
52
53bool ProvenanceAnalysis::relatedPHI(const PHINode *A,
54 const Value *B) {
55 // If the values are PHIs in the same block, we can do a more precise as well
56 // as efficient check: just check for relations between the values on
57 // corresponding edges.
58 if (const PHINode *PNB = dyn_cast<PHINode>(B))
59 if (PNB->getParent() == A->getParent()) {
60 for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i)
61 if (related(A->getIncomingValue(i),
62 PNB->getIncomingValueForBlock(A->getIncomingBlock(i))))
63 return true;
64 return false;
65 }
66
67 // Check each unique source of the PHI node against B.
69 for (Value *PV1 : A->incoming_values()) {
70 if (UniqueSrc.insert(PV1).second && related(PV1, B))
71 return true;
72 }
73
74 // All of the arms checked out.
75 return false;
76}
77
78/// Test if the value of P, or any value covered by its provenance, is ever
79/// stored within the function (not counting callees).
80static bool IsStoredObjCPointer(const Value *P) {
83 Worklist.push_back(P);
84 Visited.insert(P);
85 do {
86 P = Worklist.pop_back_val();
87 for (const Use &U : P->uses()) {
88 const User *Ur = U.getUser();
89 if (isa<StoreInst>(Ur)) {
90 if (U.getOperandNo() == 0)
91 // The pointer is stored.
92 return true;
93 // The pointed is stored through.
94 continue;
95 }
96 if (isa<CallInst>(Ur))
97 // The pointer is passed as an argument, ignore this.
98 continue;
99 if (isa<PtrToIntInst>(P))
100 // Assume the worst.
101 return true;
102 if (Visited.insert(Ur).second)
103 Worklist.push_back(Ur);
104 }
105 } while (!Worklist.empty());
106
107 // Everything checked out.
108 return false;
109}
110
111bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B) {
112 // Ask regular AliasAnalysis, for a first approximation.
113 switch (AA->alias(A, B)) {
115 return false;
118 return true;
120 break;
121 }
122
123 bool AIsIdentified = IsObjCIdentifiedObject(A);
124 bool BIsIdentified = IsObjCIdentifiedObject(B);
125
126 // An ObjC-Identified object can't alias a load if it is never locally stored.
127 if (AIsIdentified) {
128 // Check for an obvious escape.
129 if (isa<LoadInst>(B))
130 return IsStoredObjCPointer(A);
131 if (BIsIdentified) {
132 // Check for an obvious escape.
133 if (isa<LoadInst>(A))
134 return IsStoredObjCPointer(B);
135 // Both pointers are identified and escapes aren't an evident problem.
136 return false;
137 }
138 } else if (BIsIdentified) {
139 // Check for an obvious escape.
140 if (isa<LoadInst>(A))
141 return IsStoredObjCPointer(B);
142 }
143
144 // Special handling for PHI and Select.
145 if (const PHINode *PN = dyn_cast<PHINode>(A))
146 return relatedPHI(PN, B);
147 if (const PHINode *PN = dyn_cast<PHINode>(B))
148 return relatedPHI(PN, A);
149 if (const SelectInst *S = dyn_cast<SelectInst>(A))
150 return relatedSelect(S, B);
151 if (const SelectInst *S = dyn_cast<SelectInst>(B))
152 return relatedSelect(S, A);
153
154 // Conservative.
155 return true;
156}
157
159 A = GetUnderlyingObjCPtrCached(A, UnderlyingObjCPtrCache);
160 B = GetUnderlyingObjCPtrCached(B, UnderlyingObjCPtrCache);
161
162 // Quick check.
163 if (A == B)
164 return true;
165
166 // Begin by inserting a conservative value into the map. If the insertion
167 // fails, we have the answer already. If it succeeds, leave it there until we
168 // compute the real answer to guard against recursive queries.
169 std::pair<CachedResultsTy::iterator, bool> Pair =
170 CachedResults.insert(std::make_pair(ValuePairTy(A, B), true));
171 if (!Pair.second)
172 return Pair.first->second;
173
174 bool Result = relatedCheck(A, B);
175 CachedResults[ValuePairTy(A, B)] = Result;
176 return Result;
177}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This defines the Use class.
This file defines common analysis utilities used by the ObjC ARC Optimizer.
#define P(N)
static bool IsStoredObjCPointer(const Value *P)
Test if the value of P, or any value covered by its provenance, is ever stored within the function (n...
This file declares a special form of Alias Analysis called Provenance Analysis''.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
@ MayAlias
The two locations may or may not alias.
Definition: AliasAnalysis.h:98
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:95
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:211
This class represents the LLVM 'select' instruction.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
bool empty() const
Definition: SmallVector.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
bool related(const Value *A, const Value *B)
bool IsObjCIdentifiedObject(const Value *V)
Return true if this value refers to a distinct and identifiable object.
const Value * GetUnderlyingObjCPtrCached(const Value *V, DenseMap< const Value *, std::pair< WeakVH, WeakTrackingVH > > &Cache)
A wrapper for GetUnderlyingObjCPtr used for results memoization.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18