1 //=- CFLAliasAnalysisUtils.h - Utilities for CFL Alias Analysis ----*- 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 // These are the utilities/helpers used by the CFL Alias Analyses available in
10 // tree, i.e. Steensgaard's and Andersens'.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_CFLALIASANALYSISUTILS_H
15 #define LLVM_ANALYSIS_CFLALIASANALYSISUTILS_H
16 
17 #include "llvm/IR/Argument.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/ValueHandle.h"
20 
21 namespace llvm {
22 
23 namespace cflaa {
24 
25 template <typename AAResult> struct FunctionHandle final : public CallbackVH {
FunctionHandlefinal26   FunctionHandle(Function *Fn, AAResult *Result)
27       : CallbackVH(Fn), Result(Result) {
28     assert(Fn != nullptr);
29     assert(Result != nullptr);
30   }
31 
deletedfinal32   void deleted() override { removeSelfFromCache(); }
allUsesReplacedWithfinal33   void allUsesReplacedWith(Value *) override { removeSelfFromCache(); }
34 
35 private:
36   AAResult *Result;
37 
removeSelfFromCachefinal38   void removeSelfFromCache() {
39     assert(Result != nullptr);
40     auto *Val = getValPtr();
41     Result->evict(cast<Function>(Val));
42     setValPtr(nullptr);
43   }
44 };
45 
parentFunctionOfValue(const Value * Val)46 static inline const Function *parentFunctionOfValue(const Value *Val) {
47   if (auto *Inst = dyn_cast<Instruction>(Val)) {
48     auto *Bb = Inst->getParent();
49     return Bb->getParent();
50   }
51 
52   if (auto *Arg = dyn_cast<Argument>(Val))
53     return Arg->getParent();
54   return nullptr;
55 }
56 } // namespace cflaa
57 } // namespace llvm
58 
59 #endif // LLVM_ANALYSIS_CFLALIASANALYSISUTILS_H
60