diff --git a/src/mir/GreedyAlloc.cpp b/src/mir/GreedyAlloc.cpp index 803c9e38..eef77987 100644 --- a/src/mir/GreedyAlloc.cpp +++ b/src/mir/GreedyAlloc.cpp @@ -52,8 +52,10 @@ struct SpillWeightCmp const auto& lb = intervals[b]; if (la.generation != lb.generation) return la.generation > lb.generation; + // 新 vreg 按 FirstUsePos 升序:确保处理顺序与指令顺序一致, + // 这样当 vreg B 被分配时,更早的 vreg A 已在 reg_assignments_ 中 if (la.deferred_count == 0 && lb.deferred_count == 0) - return la.Length() > lb.Length(); + return la.FirstUsePos() > lb.FirstUsePos(); return la.spill_weight < lb.spill_weight; } }; @@ -532,6 +534,7 @@ static void AllocateRegistersForFunction(MachineFunction &function) // ---- 阶段 0:活跃分析 + 预处理 ---- auto raw = ComputeInstLiveness(function); auto intervals = EnhanceIntervals(raw, function); + intervals.reserve(function.GetNumVRegs() * 16); auto &blocks = function.GetBlocks(); std::vector pos_to_block; @@ -549,7 +552,7 @@ static void AllocateRegistersForFunction(MachineFunction &function) auto block_depth = AnalyzeLoopDepth(function); ComputeSpillWeights(intervals, block_depth, pos_to_block); PropagateCopyHints(intervals, function); - intervals.reserve(function.GetNumVRegs() * 4); + intervals.reserve(function.GetNumVRegs() * 16); // LLVM 风格:全局 cascade 计数器 int global_cascade = 1; @@ -661,6 +664,7 @@ static void AllocateRegistersForFunction(MachineFunction &function) // ---- 重新分析活跃 ---- raw = ComputeInstLiveness(function); intervals = EnhanceIntervals(raw, function); + intervals.reserve(function.GetNumVRegs() * 16); // ---- 重建位置映射 ---- pos_to_block.clear();