1327952Sdim//===- ProvenanceAnalysis.h - ObjC ARC Optimization -------------*- C++ -*-===//
2249259Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6249259Sdim//
7249259Sdim//===----------------------------------------------------------------------===//
8327952Sdim//
9249259Sdim/// \file
10249259Sdim///
11249259Sdim/// This file declares a special form of Alias Analysis called ``Provenance
12249259Sdim/// Analysis''. The word ``provenance'' refers to the history of the ownership
13249259Sdim/// of an object. Thus ``Provenance Analysis'' is an analysis which attempts to
14249259Sdim/// use various techniques to determine if locally
15249259Sdim///
16249259Sdim/// WARNING: This file knows about certain library functions. It recognizes them
17249259Sdim/// by name, and hardwires knowledge of their semantics.
18249259Sdim///
19249259Sdim/// WARNING: This file knows about how certain Objective-C library functions are
20249259Sdim/// used. Naive LLVM IR transformations which would otherwise be
21249259Sdim/// behavior-preserving may break these assumptions.
22327952Sdim//
23249259Sdim//===----------------------------------------------------------------------===//
24249259Sdim
25280031Sdim#ifndef LLVM_LIB_TRANSFORMS_OBJCARC_PROVENANCEANALYSIS_H
26280031Sdim#define LLVM_LIB_TRANSFORMS_OBJCARC_PROVENANCEANALYSIS_H
27249259Sdim
28249259Sdim#include "llvm/ADT/DenseMap.h"
29296417Sdim#include "llvm/Analysis/AliasAnalysis.h"
30341825Sdim#include "llvm/IR/ValueHandle.h"
31327952Sdim#include <utility>
32249259Sdim
33249259Sdimnamespace llvm {
34249259Sdim
35327952Sdimclass DataLayout;
36327952Sdimclass PHINode;
37327952Sdimclass SelectInst;
38327952Sdimclass Value;
39327952Sdim
40249259Sdimnamespace objcarc {
41249259Sdim
42341825Sdim/// This is similar to BasicAliasAnalysis, and it uses many of the same
43249259Sdim/// techniques, except it uses special ObjC-specific reasoning about pointer
44249259Sdim/// relationships.
45249259Sdim///
46249259Sdim/// In this context ``Provenance'' is defined as the history of an object's
47249259Sdim/// ownership. Thus ``Provenance Analysis'' is defined by using the notion of
48249259Sdim/// an ``independent provenance source'' of a pointer to determine whether or
49249259Sdim/// not two pointers have the same provenance source and thus could
50249259Sdim/// potentially be related.
51249259Sdimclass ProvenanceAnalysis {
52249259Sdim  AliasAnalysis *AA;
53249259Sdim
54327952Sdim  using ValuePairTy = std::pair<const Value *, const Value *>;
55327952Sdim  using CachedResultsTy = DenseMap<ValuePairTy, bool>;
56327952Sdim
57249259Sdim  CachedResultsTy CachedResults;
58249259Sdim
59341825Sdim  DenseMap<const Value *, WeakTrackingVH> UnderlyingObjCPtrCache;
60341825Sdim
61288943Sdim  bool relatedCheck(const Value *A, const Value *B, const DataLayout &DL);
62249259Sdim  bool relatedSelect(const SelectInst *A, const Value *B);
63249259Sdim  bool relatedPHI(const PHINode *A, const Value *B);
64249259Sdim
65327952Sdimpublic:
66327952Sdim  ProvenanceAnalysis() = default;
67288943Sdim  ProvenanceAnalysis(const ProvenanceAnalysis &) = delete;
68327952Sdim  ProvenanceAnalysis &operator=(const ProvenanceAnalysis &) = delete;
69249259Sdim
70249259Sdim  void setAA(AliasAnalysis *aa) { AA = aa; }
71249259Sdim
72249259Sdim  AliasAnalysis *getAA() const { return AA; }
73249259Sdim
74288943Sdim  bool related(const Value *A, const Value *B, const DataLayout &DL);
75249259Sdim
76249259Sdim  void clear() {
77249259Sdim    CachedResults.clear();
78341825Sdim    UnderlyingObjCPtrCache.clear();
79249259Sdim  }
80249259Sdim};
81249259Sdim
82249259Sdim} // end namespace objcarc
83327952Sdim
84249259Sdim} // end namespace llvm
85249259Sdim
86327952Sdim#endif // LLVM_LIB_TRANSFORMS_OBJCARC_PROVENANCEANALYSIS_H
87