[midend]通过编译,TODO:验证正确性
This commit is contained in:
@@ -22,7 +22,9 @@ add_executable(sysyc
|
|||||||
SysYIRGenerator.cpp
|
SysYIRGenerator.cpp
|
||||||
SysYIRPrinter.cpp
|
SysYIRPrinter.cpp
|
||||||
SysYIRCFGOpt.cpp
|
SysYIRCFGOpt.cpp
|
||||||
# SysYIRAnalyser.cpp
|
Pass.cpp
|
||||||
|
Dom.cpp
|
||||||
|
Liveness.cpp
|
||||||
# DeadCodeElimination.cpp
|
# DeadCodeElimination.cpp
|
||||||
AddressCalculationExpansion.cpp
|
AddressCalculationExpansion.cpp
|
||||||
# Mem2Reg.cpp
|
# Mem2Reg.cpp
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ void DominatorTree::computeDominanceFrontiers(Function *F) {
|
|||||||
// ==============================================================
|
// ==============================================================
|
||||||
|
|
||||||
|
|
||||||
bool DominatorTreeAnalysisPass::runOnFunction(Function* F) {
|
bool DominatorTreeAnalysisPass::runOnFunction(Function* F, AnalysisManager &AM) {
|
||||||
CurrentDominatorTree = std::make_unique<DominatorTree>(F);
|
CurrentDominatorTree = std::make_unique<DominatorTree>(F);
|
||||||
CurrentDominatorTree->computeDominators(F);
|
CurrentDominatorTree->computeDominators(F);
|
||||||
CurrentDominatorTree->computeIDoms(F);
|
CurrentDominatorTree->computeIDoms(F);
|
||||||
|
|||||||
@@ -89,12 +89,19 @@ void LivenessAnalysisResult::computeLiveness(Function *F) {
|
|||||||
// TODO : 目前为逆序遍历基本块,考虑反向拓扑序遍历基本块
|
// TODO : 目前为逆序遍历基本块,考虑反向拓扑序遍历基本块
|
||||||
|
|
||||||
// 逆序遍历基本块
|
// 逆序遍历基本块
|
||||||
std::list<std::unique_ptr<BasicBlock>> basicBlocks(F->getBasicBlocks().begin(), F->getBasicBlocks().end());
|
// std::list<std::unique_ptr<BasicBlock>> basicBlocks(F->getBasicBlocks().begin(), F->getBasicBlocks().end());
|
||||||
std::reverse(basicBlocks.begin(), basicBlocks.end());
|
// std::reverse(basicBlocks.begin(), basicBlocks.end());
|
||||||
// 然后遍历 basicBlocks
|
// 然后遍历 basicBlocks
|
||||||
|
// 创建一个 BasicBlock* 的列表来存储指针,避免拷贝 unique_ptr
|
||||||
|
// Option 1: Using std::vector<BasicBlock*> (preferred for performance with reverse)
|
||||||
|
std::vector<BasicBlock*> basicBlocksPointers;
|
||||||
|
for (const auto& bb_ptr : F->getBasicBlocks()) {
|
||||||
|
basicBlocksPointers.push_back(bb_ptr.get());
|
||||||
|
}
|
||||||
|
std::reverse(basicBlocksPointers.begin(), basicBlocksPointers.end());
|
||||||
|
|
||||||
for (auto bb_iter = basicBlocks.begin(); bb_iter != basicBlocks.end(); ++bb_iter) {
|
for (auto bb_iter = basicBlocksPointers.begin(); bb_iter != basicBlocksPointers.end(); ++bb_iter) {
|
||||||
BasicBlock *bb = bb_iter->get();
|
BasicBlock *bb = *bb_iter; // 获取 BasicBlock 指针
|
||||||
if (!bb)
|
if (!bb)
|
||||||
continue; // 避免空指针
|
continue; // 避免空指针
|
||||||
|
|
||||||
|
|||||||
@@ -11,9 +11,8 @@
|
|||||||
#include "Dom.h"
|
#include "Dom.h"
|
||||||
#include "Liveness.h"
|
#include "Liveness.h"
|
||||||
|
|
||||||
namespace sysy {
|
|
||||||
|
|
||||||
extern int DEBUG; // 全局调试标志
|
extern int DEBUG; // 全局调试标志
|
||||||
|
namespace sysy {
|
||||||
|
|
||||||
// ======================================================================
|
// ======================================================================
|
||||||
// 封装优化流程的函数:包含Pass注册和迭代运行逻辑
|
// 封装优化流程的函数:包含Pass注册和迭代运行逻辑
|
||||||
@@ -32,6 +31,8 @@ void PassManager::runOptimizationPipeline(Module* moduleIR, int optLevel) {
|
|||||||
sysy::PassManager pm(moduleIR);
|
sysy::PassManager pm(moduleIR);
|
||||||
|
|
||||||
// 3. 根据优化级别添加不同的优化遍
|
// 3. 根据优化级别添加不同的优化遍
|
||||||
|
// TODO : 根据 optLevel 添加不同的优化遍
|
||||||
|
// 讨论 是不动点迭代进行优化遍还是手动客制化优化遍的顺序?
|
||||||
if (optLevel >= 1) {
|
if (optLevel >= 1) {
|
||||||
if (DEBUG) std::cout << "Applying -O1 optimizations.\n";
|
if (DEBUG) std::cout << "Applying -O1 optimizations.\n";
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,7 @@ class Instruction;
|
|||||||
// 它将包含 LiveIn 和 LiveOut 集合
|
// 它将包含 LiveIn 和 LiveOut 集合
|
||||||
class LivenessAnalysisResult : public AnalysisResultBase {
|
class LivenessAnalysisResult : public AnalysisResultBase {
|
||||||
public:
|
public:
|
||||||
LivenessAnalysisResult(Function *F); // 构造函数,需要一个函数来关联结果
|
LivenessAnalysisResult(Function *F) : AssociatedFunction(F) {}
|
||||||
LivenessAnalysisResult::LivenessAnalysisResult(Function *F) : AssociatedFunction(F) {}
|
|
||||||
|
|
||||||
// 获取给定基本块的 LiveIn 集合
|
// 获取给定基本块的 LiveIn 集合
|
||||||
const std::set<Value *> *getLiveIn(BasicBlock *BB) const;
|
const std::set<Value *> *getLiveIn(BasicBlock *BB) const;
|
||||||
|
|||||||
@@ -7,9 +7,14 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <typeindex> // For std::type_index (although void* ID is more common in LLVM)
|
#include <typeindex> // For std::type_index (although void* ID is more common in LLVM)
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "IR.h"
|
||||||
|
|
||||||
namespace sysy {
|
namespace sysy {
|
||||||
|
|
||||||
|
//前向声明
|
||||||
|
class PassManager;
|
||||||
|
class AnalysisManager;
|
||||||
|
|
||||||
// 抽象基类:分析结果
|
// 抽象基类:分析结果
|
||||||
class AnalysisResultBase {
|
class AnalysisResultBase {
|
||||||
public:
|
public:
|
||||||
@@ -144,7 +149,7 @@ public:
|
|||||||
|
|
||||||
// 确保分析遍的粒度与请求的上下文匹配
|
// 确保分析遍的粒度与请求的上下文匹配
|
||||||
if (analysisPass->getGranularity() == Pass::Granularity::Function) {
|
if (analysisPass->getGranularity() == Pass::Granularity::Function) {
|
||||||
analysisPass->runOnFunction(F); // 运行分析遍
|
analysisPass->runOnFunction(F, *this); // 运行分析遍
|
||||||
// 获取结果并缓存
|
// 获取结果并缓存
|
||||||
std::unique_ptr<AnalysisResultBase> result = analysisPass->getResult();
|
std::unique_ptr<AnalysisResultBase> result = analysisPass->getResult();
|
||||||
T *specificResult = static_cast<T *>(result.get());
|
T *specificResult = static_cast<T *>(result.get());
|
||||||
@@ -166,13 +171,14 @@ public:
|
|||||||
cachedResults.erase({F, analysisID});
|
cachedResults.erase({F, analysisID});
|
||||||
} else {
|
} else {
|
||||||
// 使所有函数的特定分析结果失效
|
// 使所有函数的特定分析结果失效
|
||||||
std::map<std::pair<Function *, void *>, std::unique_ptr<AnalysisResultBase>> newCachedResults;
|
// 遍历并删除匹配的元素,避免拷贝 unique_ptr
|
||||||
for (auto &pair : cachedResults) {
|
for (auto it = cachedResults.begin(); it != cachedResults.end(); ) {
|
||||||
if (pair.first.second != analysisID) {
|
if (it->first.second == analysisID) {
|
||||||
newCachedResults.insert(std::move(pair));
|
it = cachedResults.erase(it); // erase 返回下一个元素的迭代器
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cachedResults = std::move(newCachedResults);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -190,9 +196,7 @@ public:
|
|||||||
PassManager() = default;
|
PassManager() = default;
|
||||||
~PassManager() = default;
|
~PassManager() = default;
|
||||||
|
|
||||||
PassManager(Module *module) : pmodule(module) {
|
PassManager(Module *module) : pmodule(module) , analysisManager() {}
|
||||||
analysisManager = AnalysisManager(); // 初始化分析管理器
|
|
||||||
}
|
|
||||||
|
|
||||||
// 运行所有注册的遍
|
// 运行所有注册的遍
|
||||||
bool run();
|
bool run();
|
||||||
|
|||||||
Reference in New Issue
Block a user