1 //===- IteratedDominanceFrontier.h - Calculate IDF --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ANALYSIS_IDF_H
10 #define LLVM_ANALYSIS_IDF_H
12 #include "llvm/IR/CFGDiff.h"
13 #include "llvm/Support/GenericIteratedDominanceFrontier.h"
19 namespace IDFCalculatorDetail
{
21 /// Specialization for BasicBlock for the optional use of GraphDiff.
22 template <bool IsPostDom
> struct ChildrenGetterTy
<BasicBlock
, IsPostDom
> {
23 using NodeRef
= BasicBlock
*;
24 using ChildrenTy
= SmallVector
<BasicBlock
*, 8>;
26 ChildrenGetterTy() = default;
27 ChildrenGetterTy(const GraphDiff
<BasicBlock
*, IsPostDom
> *GD
) : GD(GD
) {
31 ChildrenTy
get(const NodeRef
&N
);
33 const GraphDiff
<BasicBlock
*, IsPostDom
> *GD
= nullptr;
36 } // end of namespace IDFCalculatorDetail
38 template <bool IsPostDom
>
39 class IDFCalculator final
: public IDFCalculatorBase
<BasicBlock
, IsPostDom
> {
41 using IDFCalculatorBase
=
42 typename
llvm::IDFCalculatorBase
<BasicBlock
, IsPostDom
>;
43 using ChildrenGetterTy
= typename
IDFCalculatorBase::ChildrenGetterTy
;
45 IDFCalculator(DominatorTreeBase
<BasicBlock
, IsPostDom
> &DT
)
46 : IDFCalculatorBase(DT
) {}
48 IDFCalculator(DominatorTreeBase
<BasicBlock
, IsPostDom
> &DT
,
49 const GraphDiff
<BasicBlock
*, IsPostDom
> *GD
)
50 : IDFCalculatorBase(DT
, ChildrenGetterTy(GD
)) {
55 using ForwardIDFCalculator
= IDFCalculator
<false>;
56 using ReverseIDFCalculator
= IDFCalculator
<true>;
58 //===----------------------------------------------------------------------===//
60 //===----------------------------------------------------------------------===//
62 namespace IDFCalculatorDetail
{
64 template <bool IsPostDom
>
65 typename ChildrenGetterTy
<BasicBlock
, IsPostDom
>::ChildrenTy
66 ChildrenGetterTy
<BasicBlock
, IsPostDom
>::get(const NodeRef
&N
) {
69 typename IDFCalculatorBase
<BasicBlock
, IsPostDom
>::OrderedNodeTy
;
72 auto Children
= children
<OrderedNodeTy
>(N
);
73 return {Children
.begin(), Children
.end()};
76 using SnapShotBBPairTy
=
77 std::pair
<const GraphDiff
<BasicBlock
*, IsPostDom
> *, OrderedNodeTy
>;
80 for (const auto &SnapShotBBPair
: children
<SnapShotBBPairTy
>({GD
, N
}))
81 Ret
.emplace_back(SnapShotBBPair
.second
);
85 } // end of namespace IDFCalculatorDetail
87 } // end of namespace llvm