aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/GlobalISel/GISelWorkList.h
blob: faeafabae11ff532faec0759f1742758a0b3409f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_GLOBALISEL_GISELWORKLIST_H
#define RCC_CODEGEN_GLOBALISEL_GISELWORKLIST_H

#include <cstddef>
#include <unordered_map>
#include <vector>

namespace rcc {

class MachineInstr;

template <std::size_t InlineCapacity> class GISelWorkList final {
public:
  GISelWorkList() {
    Worklist.reserve(InlineCapacity);
    WorklistMap.reserve(InlineCapacity);
  }

  [[nodiscard]] bool empty() const noexcept {
    return WorklistMap.empty();
  }

  [[nodiscard]] std::size_t size() const noexcept {
    return WorklistMap.size();
  }

  void deferredInsert(MachineInstr *Instruction) {
    Worklist.push_back(Instruction);
  }

  void finalize() {
    WorklistMap.reserve(Worklist.size());
    for (std::size_t Index = 0; Index < Worklist.size(); ++Index)
      WorklistMap.emplace(Worklist[Index], Index);
  }

  void insert(MachineInstr *Instruction) {
    const auto [Position, Inserted] = WorklistMap.emplace(Instruction, Worklist.size());
    if (Inserted)
      Worklist.push_back(Instruction);
  }

  void remove(const MachineInstr *Instruction) {
    const auto Position = WorklistMap.find(Instruction);
    if (Position == WorklistMap.end())
      return;
    Worklist[Position->second] = nullptr;
    WorklistMap.erase(Position);
  }

  void clear() {
    Worklist.clear();
    WorklistMap.clear();
  }

  [[nodiscard]] MachineInstr *popBack() {
    MachineInstr *Instruction = nullptr;
    do {
      Instruction = Worklist.back();
      Worklist.pop_back();
    } while (Instruction == nullptr);
    WorklistMap.erase(Instruction);
    return Instruction;
  }

private:
  std::vector<MachineInstr *> Worklist;
  std::unordered_map<const MachineInstr *, std::size_t> WorklistMap;
};

} // namespace rcc

#endif // RCC_CODEGEN_GLOBALISEL_GISELWORKLIST_H