Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- IteratedDominanceFrontier.h - Calculate IDF --------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 8 | |
| 9 | #ifndef LLVM_ANALYSIS_IDF_H |
| 10 | #define LLVM_ANALYSIS_IDF_H |
| 11 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 12 | #include "llvm/Support/CFGDiff.h" |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 13 | #include "llvm/Support/GenericIteratedDominanceFrontier.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 14 | |
| 15 | namespace llvm { |
| 16 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 17 | class BasicBlock; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 18 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 19 | namespace IDFCalculatorDetail { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 20 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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>; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 25 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 26 | ChildrenGetterTy() = default; |
| 27 | ChildrenGetterTy(const GraphDiff<BasicBlock *, IsPostDom> *GD) : GD(GD) { |
| 28 | assert(GD); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 31 | ChildrenTy get(const NodeRef &N); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 32 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 33 | const GraphDiff<BasicBlock *, IsPostDom> *GD = nullptr; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 34 | }; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 35 | |
| 36 | } // end of namespace IDFCalculatorDetail |
| 37 | |
| 38 | template <bool IsPostDom> |
| 39 | class IDFCalculator final : public IDFCalculatorBase<BasicBlock, IsPostDom> { |
| 40 | public: |
| 41 | using IDFCalculatorBase = |
| 42 | typename llvm::IDFCalculatorBase<BasicBlock, IsPostDom>; |
| 43 | using ChildrenGetterTy = typename IDFCalculatorBase::ChildrenGetterTy; |
| 44 | |
| 45 | IDFCalculator(DominatorTreeBase<BasicBlock, IsPostDom> &DT) |
| 46 | : IDFCalculatorBase(DT) {} |
| 47 | |
| 48 | IDFCalculator(DominatorTreeBase<BasicBlock, IsPostDom> &DT, |
| 49 | const GraphDiff<BasicBlock *, IsPostDom> *GD) |
| 50 | : IDFCalculatorBase(DT, ChildrenGetterTy(GD)) { |
| 51 | assert(GD); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | using ForwardIDFCalculator = IDFCalculator<false>; |
| 56 | using ReverseIDFCalculator = IDFCalculator<true>; |
| 57 | |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | // Implementation. |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
| 62 | namespace IDFCalculatorDetail { |
| 63 | |
| 64 | template <bool IsPostDom> |
| 65 | typename ChildrenGetterTy<BasicBlock, IsPostDom>::ChildrenTy |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 66 | ChildrenGetterTy<BasicBlock, IsPostDom>::get(const NodeRef &N) { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 67 | |
| 68 | using OrderedNodeTy = |
| 69 | typename IDFCalculatorBase<BasicBlock, IsPostDom>::OrderedNodeTy; |
| 70 | |
| 71 | if (!GD) { |
| 72 | auto Children = children<OrderedNodeTy>(N); |
| 73 | return {Children.begin(), Children.end()}; |
| 74 | } |
| 75 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 76 | return GD->template getChildren<IsPostDom>(N); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 77 | } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 78 | |
| 79 | } // end of namespace IDFCalculatorDetail |
| 80 | |
| 81 | } // end of namespace llvm |
| 82 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 83 | #endif |