1//===-- WebAssemblySortRegion.h - WebAssembly Sort SortRegion ----*- 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///
9/// \file
10/// \brief This file implements regions used in CFGSort and CFGStackify.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYSORTREGION_H
15#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYSORTREGION_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/iterator_range.h"
20
21namespace llvm {
22
23class MachineBasicBlock;
24class MachineLoop;
25class MachineLoopInfo;
26class WebAssemblyException;
27class WebAssemblyExceptionInfo;
28
29namespace WebAssembly {
30
31// Wrapper for loops and exceptions
32class SortRegion {
33public:
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;
41};
42
43template <typename T> class ConcreteSortRegion : public SortRegion {
44 const T *Unit;
45
46public:
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);
51 }
52 unsigned getNumBlocks() const override { return Unit->getNumBlocks(); }
53 iterator_range<block_iterator> blocks() const override {
54 return Unit->blocks();
55 }
56 bool isLoop() const override { return false; }
57};
58
59// This class has information of nested SortRegions; this is analogous to what
60// LoopInfo is for loops.
61class SortRegionInfo {
62 friend class ConcreteSortRegion<MachineLoopInfo>;
63 friend class ConcreteSortRegion<WebAssemblyException>;
64
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>>
69 ExceptionMap;
70
71public:
72 SortRegionInfo(const MachineLoopInfo &MLI,
73 const WebAssemblyExceptionInfo &WEI)
74 : MLI(MLI), WEI(WEI) {}
75
76 // Returns a smallest loop or exception that contains MBB
77 const SortRegion *getRegionFor(const MachineBasicBlock *MBB);
78
79 // Return the "bottom" block among all blocks dominated by the region
80 // (MachineLoop or WebAssemblyException) header. This works when the entity is
81 // discontiguous.
82 MachineBasicBlock *getBottom(const SortRegion *R);
83 MachineBasicBlock *getBottom(const MachineLoop *ML);
84 MachineBasicBlock *getBottom(const WebAssemblyException *WE);
85};
86
87} // end namespace WebAssembly
88
89} // end namespace llvm
90
91#endif
92