Lab4: Implement basic scalar optimizations and lower Phi nodes to assembly

This commit is contained in:
2026-05-05 10:20:15 +08:00
committed by CGH0S7
parent 0b0bc04be3
commit 8f7e0ac5b4
17 changed files with 1318 additions and 35 deletions

View File

@@ -1 +1,35 @@
// IR Pass 管理骨架。
#include "ir/PassManager.h"
#include <iostream>
namespace ir {
void RunFunctionOptimizationPasses(Function* func, Context& ctx) {
// 1. Promote memory-based local variables to SSA form using Mem2Reg
RunMem2Reg(func, ctx);
// 2. Run scalar optimizations iteratively until convergence (no changes observed)
bool changed = true;
int iterations = 0;
const int max_iterations = 16; // Safe limit to prevent compile-time infinite loops
while (changed && iterations < max_iterations) {
changed = false;
iterations++;
changed |= RunConstProp(func, ctx);
changed |= RunConstFold(func, ctx);
changed |= RunCSE(func);
changed |= RunDCE(func);
changed |= RunCFGSimplify(func);
}
}
void RunOptimizationPasses(Module& module) {
for (const auto& funcPtr : module.GetFunctions()) {
if (!funcPtr->GetBlocks().empty()) {
RunFunctionOptimizationPasses(funcPtr.get(), module.GetContext());
}
}
}
} // namespace ir