#ifndef RISCV64_LINEARSCAN_H #define RISCV64_LINEARSCAN_H #include "RISCv64LLIR.h" #include "RISCv64ISel.h" #include #include #include #include namespace sysy { // 前向声明 class MachineBasicBlock; class MachineFunction; class RISCv64ISel; /** * @brief 表示一个虚拟寄存器的活跃区间。 * 包含起始和结束指令编号。为了简化,我们不处理有“洞”的区间。 */ struct LiveInterval { unsigned vreg = 0; int start = -1; int end = -1; LiveInterval(unsigned vreg) : vreg(vreg) {} // 用于排序,按起始点从小到大 bool operator<(const LiveInterval& other) const { return start < other.start; } }; class RISCv64LinearScan { public: RISCv64LinearScan(MachineFunction* mfunc); void run(); private: // --- 核心算法流程 --- void linearizeBlocks(); void computeLiveIntervals(); bool linearScan(); void rewriteProgram(); void applyAllocation(); void chooseRegForInterval(LiveInterval* current); void spillAtInterval(LiveInterval* current); // --- 辅助函数 --- void getInstrUseDef(const MachineInstr* instr, std::set& use, std::set& def); bool isFPVReg(unsigned vreg) const; void collectUsedCalleeSavedRegs(); MachineFunction* MFunc; RISCv64ISel* ISel; // --- 线性扫描数据结构 --- std::vector linear_order_blocks; std::map instr_numbering; std::map live_intervals; std::vector unhandled; std::vector active; // 活跃且已分配物理寄存器的区间 std::set spilled_vregs; // 记录在本轮被决定溢出的vreg // --- 寄存器池和分配结果 --- std::vector allocable_int_regs; std::vector allocable_fp_regs; std::set free_int_regs; std::set free_fp_regs; std::map vreg_to_preg_map; std::map abi_vreg_map; const std::map& vreg_type_map; }; } // namespace sysy #endif // RISCV64_LINEARSCAN_H