aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/MachineFunctionRecords.h
blob: 7050072b168fc77e3cf5d0470e7fd56ee5b48937 (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
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_MACHINEFUNCTIONRECORDS_H
#define RCC_CODEGEN_MACHINEFUNCTIONRECORDS_H

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

#include <cstdint>
#include <optional>
#include <span>
#include <string>
#include <vector>

namespace rcc {

class MachineFunctionCloneContext;

struct MachineCallSiteArgument final {
  PhysicalRegister Reg;
  std::uint16_t ArgumentNumber = 0;

  friend constexpr bool operator==(MachineCallSiteArgument Left,
                                   MachineCallSiteArgument Right) noexcept = default;
};

struct MachineCallSiteInfo final {
  MachineInstrStableID CallInstruction;
  std::vector<MachineCallSiteArgument> ForwardedArguments;
  std::vector<std::uint64_t> CalleeTypeIDs;

  friend bool operator==(const MachineCallSiteInfo &Left,
                         const MachineCallSiteInfo &Right) = default;
};

struct MachineCalledGlobal final {
  MachineInstrStableID CallInstruction;
  std::string Callee;
  std::uint32_t Flags = 0;

  friend bool operator==(const MachineCalledGlobal &Left,
                         const MachineCalledGlobal &Right) = default;
};

struct MachinePrefetchTarget final {
  MachineBasicBlockProfileID Block;
  std::uint32_t CallsiteIndex = 0;

  friend constexpr bool operator==(MachinePrefetchTarget Left,
                                   MachinePrefetchTarget Right) noexcept = default;
};

class MachineFunctionRecords final {
public:
  MachineFunctionRecords() = default;
  MachineFunctionRecords(const MachineFunctionRecords &) = delete;
  MachineFunctionRecords &operator=(const MachineFunctionRecords &) = delete;
  MachineFunctionRecords(MachineFunctionRecords &&) noexcept = default;
  MachineFunctionRecords &operator=(MachineFunctionRecords &&) noexcept = default;
  ~MachineFunctionRecords() = default;

  [[nodiscard]] bool hasCalleeSavedRegisters() const noexcept {
    return CalleeSavedRegisters.has_value();
  }

  [[nodiscard]] std::span<const PhysicalRegister> calleeSavedRegisters() const noexcept {
    if (!CalleeSavedRegisters)
      return {};
    return *CalleeSavedRegisters;
  }

  [[nodiscard]] Expected<void> setCalleeSavedRegisters(std::vector<PhysicalRegister> Registers);

  void clearCalleeSavedRegisters() noexcept {
    CalleeSavedRegisters.reset();
  }

  [[nodiscard]] std::span<const MachineCallSiteInfo> callSites() const noexcept {
    return CallSites;
  }

  [[nodiscard]] Expected<void> addCallSite(MachineCallSiteInfo CallSite);

  [[nodiscard]] std::span<const MachineCalledGlobal> calledGlobals() const noexcept {
    return CalledGlobals;
  }

  [[nodiscard]] Expected<void> addCalledGlobal(MachineCalledGlobal CalledGlobal);

  [[nodiscard]] std::span<const MachinePrefetchTarget> prefetchTargets() const noexcept {
    return PrefetchTargets;
  }

  [[nodiscard]] Expected<void> addPrefetchTarget(MachinePrefetchTarget Target);

  [[nodiscard]] Expected<void> verify() const;
  [[nodiscard]] Expected<MachineFunctionRecords>
  clone(const MachineFunctionCloneContext &CloneContext) const;

  friend bool operator==(const MachineFunctionRecords &Left,
                         const MachineFunctionRecords &Right) = default;

private:
  std::optional<std::vector<PhysicalRegister>> CalleeSavedRegisters;
  std::vector<MachineCallSiteInfo> CallSites;
  std::vector<MachineCalledGlobal> CalledGlobals;
  std::vector<MachinePrefetchTarget> PrefetchTargets;
};

} // namespace rcc

#endif // RCC_CODEGEN_MACHINEFUNCTIONRECORDS_H