1 //===-- WebAssemblySortRegion.h - WebAssembly Sort SortRegion ----*- 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 //===----------------------------------------------------------------------===//
10 /// \brief This file implements regions used in CFGSort and CFGStackify.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYSORTREGION_H
15 #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYSORTREGION_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/iterator_range.h"
23 class MachineBasicBlock;
25 class MachineLoopInfo;
26 class WebAssemblyException;
27 class WebAssemblyExceptionInfo;
29 namespace WebAssembly {
31 // Wrapper for loops and exceptions
34 virtual ~SortRegion() = default;
35 virtual MachineBasicBlock *getHeader() const = 0;
36 virtual bool contains(const MachineBasicBlock *MBB) const = 0;
37 virtual unsigned getNumBlocks() const = 0;
38 using block_iterator = typename ArrayRef<MachineBasicBlock *>::const_iterator;
39 virtual iterator_range<block_iterator> blocks() const = 0;
40 virtual bool isLoop() const = 0;
43 template <typename T> class ConcreteSortRegion : public SortRegion {
47 ConcreteSortRegion(const T *Unit) : Unit(Unit) {}
48 MachineBasicBlock *getHeader() const override { return Unit->getHeader(); }
49 bool contains(const MachineBasicBlock *MBB) const override {
50 return Unit->contains(MBB);
52 unsigned getNumBlocks() const override { return Unit->getNumBlocks(); }
53 iterator_range<block_iterator> blocks() const override {
54 return Unit->blocks();
56 bool isLoop() const override { return false; }
59 // This class has information of nested SortRegions; this is analogous to what
60 // LoopInfo is for loops.
61 class SortRegionInfo {
62 friend class ConcreteSortRegion<MachineLoopInfo>;
63 friend class ConcreteSortRegion<WebAssemblyException>;
65 const MachineLoopInfo &MLI;
66 const WebAssemblyExceptionInfo &WEI;
67 DenseMap<const MachineLoop *, std::unique_ptr<SortRegion>> LoopMap;
68 DenseMap<const WebAssemblyException *, std::unique_ptr<SortRegion>>
72 SortRegionInfo(const MachineLoopInfo &MLI,
73 const WebAssemblyExceptionInfo &WEI)
74 : MLI(MLI), WEI(WEI) {}
76 // Returns a smallest loop or exception that contains MBB
77 const SortRegion *getRegionFor(const MachineBasicBlock *MBB);
79 // Return the "bottom" block among all blocks dominated by the region
80 // (MachineLoop or WebAssemblyException) header. This works when the entity is
82 MachineBasicBlock *getBottom(const SortRegion *R);
83 MachineBasicBlock *getBottom(const MachineLoop *ML);
84 MachineBasicBlock *getBottom(const WebAssemblyException *WE);
87 } // end namespace WebAssembly
89 } // end namespace llvm