10b57cec5SDimitry Andric //===- ProvenanceAnalysis.h - ObjC ARC Optimization -------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric /// \file 100b57cec5SDimitry Andric /// 110b57cec5SDimitry Andric /// This file declares a special form of Alias Analysis called ``Provenance 120b57cec5SDimitry Andric /// Analysis''. The word ``provenance'' refers to the history of the ownership 130b57cec5SDimitry Andric /// of an object. Thus ``Provenance Analysis'' is an analysis which attempts to 140b57cec5SDimitry Andric /// use various techniques to determine if locally 150b57cec5SDimitry Andric /// 160b57cec5SDimitry Andric /// WARNING: This file knows about certain library functions. It recognizes them 170b57cec5SDimitry Andric /// by name, and hardwires knowledge of their semantics. 180b57cec5SDimitry Andric /// 190b57cec5SDimitry Andric /// WARNING: This file knows about how certain Objective-C library functions are 200b57cec5SDimitry Andric /// used. Naive LLVM IR transformations which would otherwise be 210b57cec5SDimitry Andric /// behavior-preserving may break these assumptions. 220b57cec5SDimitry Andric // 230b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_PROVENANCEANALYSIS_H 260b57cec5SDimitry Andric #define LLVM_LIB_TRANSFORMS_OBJCARC_PROVENANCEANALYSIS_H 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 29*bdd1243dSDimitry Andric #include "llvm/IR/PassManager.h" 300b57cec5SDimitry Andric #include "llvm/IR/ValueHandle.h" 310b57cec5SDimitry Andric #include <utility> 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric namespace llvm { 340b57cec5SDimitry Andric 35e8d8bef9SDimitry Andric class AAResults; 360b57cec5SDimitry Andric class PHINode; 370b57cec5SDimitry Andric class SelectInst; 380b57cec5SDimitry Andric class Value; 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric namespace objcarc { 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric /// This is similar to BasicAliasAnalysis, and it uses many of the same 430b57cec5SDimitry Andric /// techniques, except it uses special ObjC-specific reasoning about pointer 440b57cec5SDimitry Andric /// relationships. 450b57cec5SDimitry Andric /// 460b57cec5SDimitry Andric /// In this context ``Provenance'' is defined as the history of an object's 470b57cec5SDimitry Andric /// ownership. Thus ``Provenance Analysis'' is defined by using the notion of 480b57cec5SDimitry Andric /// an ``independent provenance source'' of a pointer to determine whether or 490b57cec5SDimitry Andric /// not two pointers have the same provenance source and thus could 500b57cec5SDimitry Andric /// potentially be related. 510b57cec5SDimitry Andric class ProvenanceAnalysis { 52e8d8bef9SDimitry Andric AAResults *AA; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric using ValuePairTy = std::pair<const Value *, const Value *>; 550b57cec5SDimitry Andric using CachedResultsTy = DenseMap<ValuePairTy, bool>; 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric CachedResultsTy CachedResults; 580b57cec5SDimitry Andric 59349cc55cSDimitry Andric DenseMap<const Value *, std::pair<WeakVH, WeakTrackingVH>> 60349cc55cSDimitry Andric UnderlyingObjCPtrCache; 610b57cec5SDimitry Andric 62e8d8bef9SDimitry Andric bool relatedCheck(const Value *A, const Value *B); 630b57cec5SDimitry Andric bool relatedSelect(const SelectInst *A, const Value *B); 640b57cec5SDimitry Andric bool relatedPHI(const PHINode *A, const Value *B); 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric public: 670b57cec5SDimitry Andric ProvenanceAnalysis() = default; 680b57cec5SDimitry Andric ProvenanceAnalysis(const ProvenanceAnalysis &) = delete; 690b57cec5SDimitry Andric ProvenanceAnalysis &operator=(const ProvenanceAnalysis &) = delete; 700b57cec5SDimitry Andric setAA(AAResults * aa)71e8d8bef9SDimitry Andric void setAA(AAResults *aa) { AA = aa; } 720b57cec5SDimitry Andric getAA()73e8d8bef9SDimitry Andric AAResults *getAA() const { return AA; } 740b57cec5SDimitry Andric 75e8d8bef9SDimitry Andric bool related(const Value *A, const Value *B); 760b57cec5SDimitry Andric clear()770b57cec5SDimitry Andric void clear() { 780b57cec5SDimitry Andric CachedResults.clear(); 790b57cec5SDimitry Andric UnderlyingObjCPtrCache.clear(); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric }; 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric } // end namespace objcarc 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric } // end namespace llvm 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric #endif // LLVM_LIB_TRANSFORMS_OBJCARC_PROVENANCEANALYSIS_H 88