Lab6: Implement DominatorTree-based natural loop discovery and loop-invariant code motion hoisting pass

This commit is contained in:
2026-06-01 15:45:10 +08:00
committed by CGH0S7
parent 4475e91bd8
commit 6f48016c10
6 changed files with 327 additions and 6 deletions

View File

@@ -4,13 +4,11 @@
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
const int max_iterations = 16;
while (changed && iterations < max_iterations) {
changed = false;
@@ -19,6 +17,7 @@ void RunFunctionOptimizationPasses(Function* func, Context& ctx) {
changed |= RunConstProp(func, ctx);
changed |= RunConstFold(func, ctx);
changed |= RunCSE(func);
changed |= RunLICM(func);
changed |= RunDCE(func);
changed |= RunCFGSimplify(func);
}