Lab4: Implement basic scalar optimizations and lower Phi nodes to assembly
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user