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

47
include/ir/PassManager.h Normal file
View File

@@ -0,0 +1,47 @@
#pragma once
#include "ir/IR.h"
#include <vector>
#include <unordered_map>
#include <unordered_set>
namespace ir {
// Dominator Tree Analysis
class DominatorTree {
public:
explicit DominatorTree(Function* func);
void Run();
// Query interfaces
BasicBlock* GetIdom(BasicBlock* bb) const;
const std::vector<BasicBlock*>& GetDominatedBlocks(BasicBlock* bb) const;
const std::vector<BasicBlock*>& GetDominanceFrontier(BasicBlock* bb) const;
bool Dominates(BasicBlock* a, BasicBlock* b) const;
private:
Function* func_;
std::vector<BasicBlock*> rpo_;
std::unordered_map<BasicBlock*, BasicBlock*> idom_;
std::unordered_map<BasicBlock*, std::vector<BasicBlock*>> dom_tree_;
std::unordered_map<BasicBlock*, std::vector<BasicBlock*>> df_;
void ComputeRPO();
void ComputeIdom();
void ComputeDomTree();
void ComputeDF();
};
// Individual Pass Declarations
bool RunMem2Reg(Function* func, Context& ctx);
bool RunConstProp(Function* func, Context& ctx);
bool RunConstFold(Function* func, Context& ctx);
bool RunDCE(Function* func);
bool RunCFGSimplify(Function* func);
bool RunCSE(Function* func);
// Run the optimization pipeline on a Function or Module
void RunOptimizationPasses(Module& module);
void RunFunctionOptimizationPasses(Function* func, Context& ctx);
} // namespace ir