Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:12

0001 //===- DomTreeUpdater.h - DomTree/Post DomTree Updater ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the DomTreeUpdater class, which provides a uniform way to
0010 // update dominator tree related data structures.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_ANALYSIS_DOMTREEUPDATER_H
0015 #define LLVM_ANALYSIS_DOMTREEUPDATER_H
0016 
0017 #include "llvm/Analysis/GenericDomTreeUpdater.h"
0018 #include "llvm/IR/Dominators.h"
0019 #include "llvm/IR/ValueHandle.h"
0020 #include "llvm/Support/Compiler.h"
0021 #include <functional>
0022 #include <vector>
0023 
0024 namespace llvm {
0025 
0026 class PostDominatorTree;
0027 
0028 class DomTreeUpdater
0029     : public GenericDomTreeUpdater<DomTreeUpdater, DominatorTree,
0030                                    PostDominatorTree> {
0031   friend GenericDomTreeUpdater<DomTreeUpdater, DominatorTree,
0032                                PostDominatorTree>;
0033 
0034 public:
0035   using Base =
0036       GenericDomTreeUpdater<DomTreeUpdater, DominatorTree, PostDominatorTree>;
0037   using Base::Base;
0038 
0039   ~DomTreeUpdater() { flush(); }
0040 
0041   ///@{
0042   /// \name Mutation APIs
0043   ///
0044   /// These methods provide APIs for submitting updates to the DominatorTree and
0045   /// the PostDominatorTree.
0046   ///
0047   /// Note: There are two strategies to update the DominatorTree and the
0048   /// PostDominatorTree:
0049   /// 1. Eager UpdateStrategy: Updates are submitted and then flushed
0050   /// immediately.
0051   /// 2. Lazy UpdateStrategy: Updates are submitted but only flushed when you
0052   /// explicitly call Flush APIs. It is recommended to use this update strategy
0053   /// when you submit a bunch of updates multiple times which can then
0054   /// add up to a large number of updates between two queries on the
0055   /// DominatorTree. The incremental updater can reschedule the updates or
0056   /// decide to recalculate the dominator tree in order to speedup the updating
0057   /// process depending on the number of updates.
0058   ///
0059   /// Although GenericDomTree provides several update primitives,
0060   /// it is not encouraged to use these APIs directly.
0061 
0062   /// Delete DelBB. DelBB will be removed from its Parent and
0063   /// erased from available trees if it exists and finally get deleted.
0064   /// Under Eager UpdateStrategy, DelBB will be processed immediately.
0065   /// Under Lazy UpdateStrategy, DelBB will be queued until a flush event and
0066   /// all available trees are up-to-date. Assert if any instruction of DelBB is
0067   /// modified while awaiting deletion. When both DT and PDT are nullptrs, DelBB
0068   /// will be queued until flush() is called.
0069   void deleteBB(BasicBlock *DelBB);
0070 
0071   /// Delete DelBB. DelBB will be removed from its Parent and
0072   /// erased from available trees if it exists. Then the callback will
0073   /// be called. Finally, DelBB will be deleted.
0074   /// Under Eager UpdateStrategy, DelBB will be processed immediately.
0075   /// Under Lazy UpdateStrategy, DelBB will be queued until a flush event and
0076   /// all available trees are up-to-date. Assert if any instruction of DelBB is
0077   /// modified while awaiting deletion. Multiple callbacks can be queued for one
0078   /// DelBB under Lazy UpdateStrategy.
0079   void callbackDeleteBB(BasicBlock *DelBB,
0080                         std::function<void(BasicBlock *)> Callback);
0081 
0082   ///@}
0083 
0084   /// Debug method to help view the internal state of this class.
0085   LLVM_DUMP_METHOD void dump() const;
0086 
0087 private:
0088   class CallBackOnDeletion final : public CallbackVH {
0089   public:
0090     CallBackOnDeletion(BasicBlock *V,
0091                        std::function<void(BasicBlock *)> Callback)
0092         : CallbackVH(V), DelBB(V), Callback_(Callback) {}
0093 
0094   private:
0095     BasicBlock *DelBB = nullptr;
0096     std::function<void(BasicBlock *)> Callback_;
0097 
0098     void deleted() override {
0099       Callback_(DelBB);
0100       CallbackVH::deleted();
0101     }
0102   };
0103 
0104   std::vector<CallBackOnDeletion> Callbacks;
0105 
0106   /// First remove all the instructions of DelBB and then make sure DelBB has a
0107   /// valid terminator instruction which is necessary to have when DelBB still
0108   /// has to be inside of its parent Function while awaiting deletion under Lazy
0109   /// UpdateStrategy to prevent other routines from asserting the state of the
0110   /// IR is inconsistent. Assert if DelBB is nullptr or has predecessors.
0111   void validateDeleteBB(BasicBlock *DelBB);
0112 
0113   /// Returns true if at least one BasicBlock is deleted.
0114   bool forceFlushDeletedBB();
0115 };
0116 
0117 extern template class GenericDomTreeUpdater<DomTreeUpdater, DominatorTree,
0118                                             PostDominatorTree>;
0119 
0120 extern template void
0121 GenericDomTreeUpdater<DomTreeUpdater, DominatorTree,
0122                       PostDominatorTree>::recalculate(Function &F);
0123 
0124 extern template void
0125 GenericDomTreeUpdater<DomTreeUpdater, DominatorTree, PostDominatorTree>::
0126     applyUpdatesImpl</*IsForward=*/true>();
0127 extern template void
0128 GenericDomTreeUpdater<DomTreeUpdater, DominatorTree, PostDominatorTree>::
0129     applyUpdatesImpl</*IsForward=*/false>();
0130 } // namespace llvm
0131 
0132 #endif // LLVM_ANALYSIS_DOMTREEUPDATER_H