aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/LiveInterval.h
blob: 28791f19a6acb7cf430240b78bdcbf78cfcb6969 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_LIVEINTERVAL_H
#define RCC_CODEGEN_LIVEINTERVAL_H

#include "rcc/CodeGen/MachineIRTypes.h"
#include "rcc/CodeGen/Register.h"
#include "rcc/CodeGen/SlotIndexes.h"
#include "rcc/CodeGen/StableID.h"
#include "rcc/Support/Error.h"

#include <compare>
#include <cstdint>
#include <limits>
#include <optional>
#include <span>
#include <vector>

namespace rcc {

class LiveValueNumberID final {
public:
  constexpr LiveValueNumberID() noexcept = default;

  [[nodiscard]] static constexpr LiveValueNumberID fromValue(std::uint32_t Value) noexcept {
    return LiveValueNumberID(Value);
  }

  [[nodiscard]] constexpr bool isValid() const noexcept {
    return Value != 0;
  }

  [[nodiscard]] constexpr std::uint32_t value() const noexcept {
    return Value;
  }

  friend constexpr bool operator==(LiveValueNumberID, LiveValueNumberID) noexcept = default;
  friend constexpr auto operator<=>(LiveValueNumberID, LiveValueNumberID) noexcept = default;

private:
  explicit constexpr LiveValueNumberID(std::uint32_t Value) noexcept : Value(Value) {}

  std::uint32_t Value = 0;
};

enum class LiveValueState : std::uint8_t {
  Normal,
  Phi,
  Unused,
  Redefined,
};

struct LiveValueNumber final {
  LiveValueNumberID ID;
  SlotIndex Definition;
  LiveValueState State = LiveValueState::Normal;
  std::optional<MachineInstrStableID> Origin;

  friend bool operator==(const LiveValueNumber &, const LiveValueNumber &) = default;
};

struct LiveSegment final {
  SlotIndex Start;
  SlotIndex End;
  LiveValueNumberID Value;

  [[nodiscard]] constexpr bool empty() const noexcept {
    return !(Start < End);
  }

  [[nodiscard]] constexpr bool contains(SlotIndex Position) const noexcept {
    return !(Position < Start) && Position < End;
  }

  [[nodiscard]] constexpr bool overlaps(const LiveSegment &Other) const noexcept {
    return Start < Other.End && Other.Start < End;
  }

  friend constexpr bool operator==(const LiveSegment &, const LiveSegment &) noexcept = default;
};

class LiveRange final {
public:
  [[nodiscard]] std::span<const LiveSegment> segments() const noexcept {
    return Segments;
  }
  [[nodiscard]] std::span<const LiveValueNumber> values() const noexcept {
    return Values;
  }
  [[nodiscard]] bool empty() const noexcept {
    return Segments.empty();
  }

  [[nodiscard]] const LiveSegment *segmentAt(SlotIndex Position) const noexcept;
  [[nodiscard]] const LiveValueNumber *value(LiveValueNumberID ID) const noexcept;
  [[nodiscard]] const LiveValueNumber *valueAt(SlotIndex Position) const noexcept;
  [[nodiscard]] bool liveAt(SlotIndex Position) const noexcept;
  [[nodiscard]] bool overlaps(const LiveRange &Other) const noexcept;

  [[nodiscard]] Expected<LiveValueNumberID>
  createValue(SlotIndex Definition, LiveValueState State = LiveValueState::Normal,
              std::optional<MachineInstrStableID> Origin = std::nullopt);
  [[nodiscard]] Expected<void> addSegment(LiveSegment Segment);
  [[nodiscard]] Expected<void> extend(LiveValueNumberID Value, SlotIndex Start, SlotIndex End);
  [[nodiscard]] Expected<void> remove(SlotIndex Start, SlotIndex End);
  [[nodiscard]] Expected<void> shrinkToUses(std::span<const SlotIndex> Uses);
  [[nodiscard]] Expected<void> verify() const;

private:
  void mergeAdjacent();

  std::vector<LiveSegment> Segments;
  std::vector<LiveValueNumber> Values;
};

struct LiveSubRange final {
  LaneMask Lanes;
  LiveRange Range;
};

struct LiveInterval final {
  Register Reg;
  LiveRange MainRange;
  std::vector<LiveSubRange> SubRanges;
  std::uint64_t SpillWeight = 0;
  bool Spillable = true;
  std::vector<PhysicalRegister> AllocationHints;
  std::optional<Register> SplitParent;
  std::optional<Register> Origin;

  [[nodiscard]] bool overlaps(const LiveInterval &Other) const noexcept {
    return MainRange.overlaps(Other.MainRange);
  }
};

} // namespace rcc

template <> struct std::hash<rcc::LiveValueNumberID> {
  [[nodiscard]] std::size_t operator()(rcc::LiveValueNumberID ID) const noexcept {
    return std::hash<std::uint32_t>{}(ID.value());
  }
};

#endif // RCC_CODEGEN_LIVEINTERVAL_H