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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef RCC_CODEGEN_VIRTREGMAP_H
#define RCC_CODEGEN_VIRTREGMAP_H
#include "rcc/CodeGen/MachineFrameInfo.h"
#include "rcc/CodeGen/MachinePassManager.h"
#include "rcc/CodeGen/TargetRegisterInfo.h"
#include <cstdint>
#include <optional>
#include <unordered_map>
#include <vector>
namespace rcc {
class MachineFunction;
class TargetDescription;
enum class VirtRegAllocationState : std::uint8_t {
Unassigned,
Assigned,
Spilled,
Split,
};
struct VirtRegSpillMetadata final {
std::optional<FrameIndex> StackSlot;
LaneMask Lanes = LaneMask::all();
std::uint64_t SpillCount = 0;
std::uint64_t ReloadCount = 0;
std::uint64_t RematerializationCount = 0;
};
struct VirtRegMapEntry final {
Register VirtualReg;
VirtRegAllocationState State = VirtRegAllocationState::Unassigned;
std::optional<PhysicalRegister> PhysicalReg;
std::optional<Register> SplitParent;
std::optional<Register> Origin;
bool Spillable = true;
VirtRegSpillMetadata Spill;
};
class VirtRegMap final {
public:
explicit VirtRegMap(MachineFunction &Function) noexcept : Function(&Function) {}
[[nodiscard]] MachineFunction &function() const noexcept {
return *Function;
}
[[nodiscard]] Expected<void> add(Register VirtualReg, bool Spillable = true,
std::optional<Register> SplitParent = std::nullopt,
std::optional<Register> Origin = std::nullopt);
[[nodiscard]] Expected<void> assign(Register VirtualReg, PhysicalRegister PhysicalReg);
[[nodiscard]] Expected<void> assignStackSlot(Register VirtualReg, FrameIndex StackSlot,
LaneMask Lanes = LaneMask::all());
[[nodiscard]] Expected<void> markSplit(Register VirtualReg, Register Parent, Register Origin);
[[nodiscard]] Expected<void> setSpillable(Register VirtualReg, bool Spillable);
[[nodiscard]] VirtRegMapEntry *entry(Register VirtualReg) noexcept;
[[nodiscard]] const VirtRegMapEntry *entry(Register VirtualReg) const noexcept;
[[nodiscard]] std::optional<PhysicalRegister>
physicalRegister(Register VirtualReg) const noexcept;
[[nodiscard]] std::optional<FrameIndex> stackSlot(Register VirtualReg) const noexcept;
[[nodiscard]] bool isSpillable(Register VirtualReg) const noexcept;
[[nodiscard]] std::vector<const VirtRegMapEntry *> orderedEntries() const;
[[nodiscard]] Expected<void> verify() const;
private:
MachineFunction *Function;
std::unordered_map<Register, VirtRegMapEntry> Entries;
};
[[nodiscard]] Expected<bool> rewriteVirtualRegisters(MachineFunction &Function,
const TargetDescription &Target,
const VirtRegMap &Mapping);
class VirtualRegisterRewritePass final : public MachineFunctionPass {
public:
explicit VirtualRegisterRewritePass(const VirtRegMap &Mapping) noexcept : Mapping(&Mapping) {}
[[nodiscard]] const MachinePassContract &contract() const noexcept override;
[[nodiscard]] Expected<MachinePassResult> run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &Analyses) override;
private:
const VirtRegMap *Mapping;
};
} // namespace rcc
#endif // RCC_CODEGEN_VIRTREGMAP_H
|