diff options
| author | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-01 17:58:12 +0800 |
|---|---|---|
| committer | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-01 17:58:12 +0800 |
| commit | 6e09614afd2a589690d107b41308f6fe6614c266 (patch) | |
| tree | e7f6bdcbe23eae1d53d610be2683e06a4831997c /backend/include/rcc/CodeGen | |
| parent | 787f3a7a20958bfa6f231abccc617f4d8de25c4d (diff) | |
| download | rcc-project-6e09614afd2a589690d107b41308f6fe6614c266.tar.gz rcc-project-6e09614afd2a589690d107b41308f6fe6614c266.tar.xz | |
[CodeGen] Add machine function records
Represent callee-saved registers, call-site information, called globals, and prefetch targets in the core MachineIR model. Verify target and instruction references and remap StableIDs during explicit function cloning.
Diffstat (limited to 'backend/include/rcc/CodeGen')
| -rw-r--r-- | backend/include/rcc/CodeGen/MachineFunction.h | 10 | ||||
| -rw-r--r-- | backend/include/rcc/CodeGen/MachineFunctionRecords.h | 114 |
2 files changed, 124 insertions, 0 deletions
diff --git a/backend/include/rcc/CodeGen/MachineFunction.h b/backend/include/rcc/CodeGen/MachineFunction.h index 4ed660d..c067ee1 100644 --- a/backend/include/rcc/CodeGen/MachineFunction.h +++ b/backend/include/rcc/CodeGen/MachineFunction.h @@ -8,6 +8,7 @@ #include "rcc/CodeGen/MachineFrameInfo.h" #include "rcc/CodeGen/MachineFunctionAttributes.h" #include "rcc/CodeGen/MachineFunctionProperties.h" +#include "rcc/CodeGen/MachineFunctionRecords.h" #include "rcc/CodeGen/MachineJumpTableInfo.h" #include "rcc/CodeGen/MachineRegisterInfo.h" #include "rcc/CodeGen/StableID.h" @@ -98,6 +99,14 @@ public: return Attributes; } + [[nodiscard]] MachineFunctionRecords &records() noexcept { + return Records; + } + + [[nodiscard]] const MachineFunctionRecords &records() const noexcept { + return Records; + } + [[nodiscard]] const TargetFunctionState &targetState() const noexcept { return TargetState; } @@ -176,6 +185,7 @@ private: MachineJumpTableInfo JumpTables; MachineFunctionProperties Properties; MachineFunctionAttributes Attributes; + MachineFunctionRecords Records; TargetFunctionState TargetState; MachineCompilationStatus CompilationStatus; MachineBasicBlock *FirstBlock = nullptr; diff --git a/backend/include/rcc/CodeGen/MachineFunctionRecords.h b/backend/include/rcc/CodeGen/MachineFunctionRecords.h new file mode 100644 index 0000000..7050072 --- /dev/null +++ b/backend/include/rcc/CodeGen/MachineFunctionRecords.h @@ -0,0 +1,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 |
