[midend]修复entryBB和funcBodyEntry的初始化,Dom计算引进逆后续遍历和LT算法,Pass先默认关掉CFGOpt
This commit is contained in:
@ -6,30 +6,82 @@
|
|||||||
#include <set>
|
#include <set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace sysy {
|
namespace sysy {
|
||||||
|
|
||||||
// 支配树分析结果类 (保持不变)
|
// 支配树分析结果类
|
||||||
class DominatorTree : public AnalysisResultBase {
|
class DominatorTree : public AnalysisResultBase {
|
||||||
public:
|
public:
|
||||||
DominatorTree(Function* F);
|
DominatorTree(Function* F);
|
||||||
|
// 获取指定基本块的所有支配者
|
||||||
const std::set<BasicBlock*>* getDominators(BasicBlock* BB) const;
|
const std::set<BasicBlock*>* getDominators(BasicBlock* BB) const;
|
||||||
|
// 获取指定基本块的即时支配者 (Immediate Dominator)
|
||||||
BasicBlock* getImmediateDominator(BasicBlock* BB) const;
|
BasicBlock* getImmediateDominator(BasicBlock* BB) const;
|
||||||
|
// 获取指定基本块的支配边界 (Dominance Frontier)
|
||||||
const std::set<BasicBlock*>* getDominanceFrontier(BasicBlock* BB) const;
|
const std::set<BasicBlock*>* getDominanceFrontier(BasicBlock* BB) const;
|
||||||
|
// 获取指定基本块在支配树中的子节点
|
||||||
const std::set<BasicBlock*>* getDominatorTreeChildren(BasicBlock* BB) const;
|
const std::set<BasicBlock*>* getDominatorTreeChildren(BasicBlock* BB) const;
|
||||||
|
// 额外的 Getter:获取所有支配者、即时支配者和支配边界的完整映射(可选,主要用于调试或特定场景)
|
||||||
const std::map<BasicBlock*, std::set<BasicBlock*>>& getDominatorsMap() const { return Dominators; }
|
const std::map<BasicBlock*, std::set<BasicBlock*>>& getDominatorsMap() const { return Dominators; }
|
||||||
const std::map<BasicBlock*, BasicBlock*>& getIDomsMap() const { return IDoms; }
|
const std::map<BasicBlock*, BasicBlock*>& getIDomsMap() const { return IDoms; }
|
||||||
const std::map<BasicBlock*, std::set<BasicBlock*>>& getDominanceFrontiersMap() const { return DominanceFrontiers; }
|
const std::map<BasicBlock*, std::set<BasicBlock*>>& getDominanceFrontiersMap() const { return DominanceFrontiers; }
|
||||||
|
|
||||||
|
// 计算所有基本块的支配者集合
|
||||||
void computeDominators(Function* F);
|
void computeDominators(Function* F);
|
||||||
|
// 计算所有基本块的即时支配者(内部使用 Lengauer-Tarjan 算法)
|
||||||
void computeIDoms(Function* F);
|
void computeIDoms(Function* F);
|
||||||
|
// 计算所有基本块的支配边界
|
||||||
void computeDominanceFrontiers(Function* F);
|
void computeDominanceFrontiers(Function* F);
|
||||||
|
// 计算支配树的结构(即每个节点的直接子节点)
|
||||||
void computeDominatorTreeChildren(Function* F);
|
void computeDominatorTreeChildren(Function* F);
|
||||||
private:
|
private:
|
||||||
|
// 与该支配树关联的函数
|
||||||
Function* AssociatedFunction;
|
Function* AssociatedFunction;
|
||||||
std::map<BasicBlock*, std::set<BasicBlock*>> Dominators;
|
std::map<BasicBlock*, std::set<BasicBlock*>> Dominators; // 每个基本块的支配者集合
|
||||||
std::map<BasicBlock*, BasicBlock*> IDoms;
|
std::map<BasicBlock*, BasicBlock*> IDoms; // 每个基本块的即时支配者
|
||||||
std::map<BasicBlock*, std::set<BasicBlock*>> DominanceFrontiers;
|
std::map<BasicBlock*, std::set<BasicBlock*>> DominanceFrontiers; // 每个基本块的支配边界
|
||||||
std::map<BasicBlock*, std::set<BasicBlock*>> DominatorTreeChildren;
|
std::map<BasicBlock*, std::set<BasicBlock*>> DominatorTreeChildren; // 支配树中每个基本块的子节点
|
||||||
|
|
||||||
|
// ==========================================================
|
||||||
|
// Lengauer-Tarjan 算法内部所需的数据结构和辅助函数
|
||||||
|
// 这些成员是私有的,以封装 LT 算法的复杂性并避免命名空间污染
|
||||||
|
// ==========================================================
|
||||||
|
|
||||||
|
// DFS 遍历相关:
|
||||||
|
std::map<BasicBlock*, int> dfnum_map; // 存储每个基本块的 DFS 编号
|
||||||
|
std::vector<BasicBlock*> vertex_vec; // 通过 DFS 编号反向查找对应的基本块指针
|
||||||
|
std::map<BasicBlock*, BasicBlock*> parent_map; // 存储 DFS 树中每个基本块的父节点
|
||||||
|
int df_counter; // DFS 计数器,也代表 DFS 遍历的总节点数 (N)
|
||||||
|
|
||||||
|
// 半支配者 (Semi-dominator) 相关:
|
||||||
|
std::map<BasicBlock*, BasicBlock*> sdom_map; // 存储每个基本块的半支配者
|
||||||
|
std::map<BasicBlock*, BasicBlock*> idom_map; // 存储每个基本块的即时支配者 (IDom)
|
||||||
|
std::map<BasicBlock*, std::vector<BasicBlock*>> bucket_map; // 桶结构,用于存储具有相同半支配者的节点,以延迟 IDom 计算
|
||||||
|
|
||||||
|
// 并查集 (Union-Find) 相关(用于 evalAndCompress 函数):
|
||||||
|
std::map<BasicBlock*, BasicBlock*> ancestor_map; // 并查集中的父节点(用于路径压缩)
|
||||||
|
std::map<BasicBlock*, BasicBlock*> label_map; // 并查集中,每个集合的代表节点(或其路径上 sdom 最小的节点)
|
||||||
|
|
||||||
|
// ==========================================================
|
||||||
|
// 辅助计算函数 (私有)
|
||||||
|
// ==========================================================
|
||||||
|
|
||||||
|
// 计算基本块的逆后序遍历 (Reverse Post Order, RPO) 顺序
|
||||||
|
// RPO 用于优化支配者计算和 LT 算法的效率
|
||||||
|
std::vector<BasicBlock*> computeReversePostOrder(Function* F);
|
||||||
|
|
||||||
|
// Lengauer-Tarjan 算法特定的辅助 DFS 函数
|
||||||
|
// 用于初始化 dfnum_map, vertex_vec, parent_map
|
||||||
|
void dfs_lt_helper(BasicBlock* u);
|
||||||
|
|
||||||
|
// 结合了并查集的 Find 操作和 LT 算法的 Eval 操作
|
||||||
|
// 用于在路径压缩时更新 label,找到路径上 sdom 最小的节点
|
||||||
|
BasicBlock* evalAndCompress_lt_helper(BasicBlock* i);
|
||||||
|
|
||||||
|
// 并查集的 Link 操作
|
||||||
|
// 将 v_child 挂载到 u_parent 的并查集树下
|
||||||
|
void link_lt_helper(BasicBlock* u_parent, BasicBlock* v_child);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,21 +1,30 @@
|
|||||||
#include "Dom.h"
|
#include "Dom.h"
|
||||||
#include <algorithm> // for std::set_intersection, std::set_difference, std::set_union
|
#include <algorithm> // for std::set_intersection, std::reverse
|
||||||
#include <iostream> // for debug output
|
#include <iostream> // for debug output
|
||||||
#include <limits> // for std::numeric_limits
|
#include <limits> // for std::numeric_limits
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <functional> // for std::function
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
namespace sysy {
|
namespace sysy {
|
||||||
|
|
||||||
// 初始化 支配树静态 ID
|
// ==============================================================
|
||||||
|
// DominatorTreeAnalysisPass 的静态ID
|
||||||
|
// ==============================================================
|
||||||
void *DominatorTreeAnalysisPass::ID = (void *)&DominatorTreeAnalysisPass::ID;
|
void *DominatorTreeAnalysisPass::ID = (void *)&DominatorTreeAnalysisPass::ID;
|
||||||
|
|
||||||
// ==============================================================
|
// ==============================================================
|
||||||
// DominatorTree 结果类的实现
|
// DominatorTree 结果类的实现
|
||||||
// ==============================================================
|
// ==============================================================
|
||||||
|
|
||||||
|
// 构造函数:初始化关联函数,但不进行计算
|
||||||
DominatorTree::DominatorTree(Function *F) : AssociatedFunction(F) {
|
DominatorTree::DominatorTree(Function *F) : AssociatedFunction(F) {
|
||||||
// 构造时可以不计算,在分析遍运行里计算并填充
|
// 构造时不需要计算,在分析遍运行里计算并填充
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Getter 方法 (保持不变)
|
||||||
const std::set<BasicBlock *> *DominatorTree::getDominators(BasicBlock *BB) const {
|
const std::set<BasicBlock *> *DominatorTree::getDominators(BasicBlock *BB) const {
|
||||||
auto it = Dominators.find(BB);
|
auto it = Dominators.find(BB);
|
||||||
if (it != Dominators.end()) {
|
if (it != Dominators.end()) {
|
||||||
@ -48,7 +57,7 @@ const std::set<BasicBlock *> *DominatorTree::getDominatorTreeChildren(BasicBlock
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 辅助函数:打印 BasicBlock 集合
|
// 辅助函数:打印 BasicBlock 集合 (保持不变)
|
||||||
void printBBSet(const std::string &prefix, const std::set<BasicBlock *> &s) {
|
void printBBSet(const std::string &prefix, const std::set<BasicBlock *> &s) {
|
||||||
if (!DEBUG)
|
if (!DEBUG)
|
||||||
return;
|
return;
|
||||||
@ -63,24 +72,52 @@ void printBBSet(const std::string &prefix, const std::set<BasicBlock *> &s) {
|
|||||||
std::cout << "}" << std::endl;
|
std::cout << "}" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 辅助函数:计算逆后序遍历 (RPO) - 保持不变
|
||||||
|
std::vector<BasicBlock*> DominatorTree::computeReversePostOrder(Function* F) {
|
||||||
|
std::vector<BasicBlock*> postOrder;
|
||||||
|
std::set<BasicBlock*> visited;
|
||||||
|
|
||||||
|
std::function<void(BasicBlock*)> dfs_rpo =
|
||||||
|
[&](BasicBlock* bb) {
|
||||||
|
visited.insert(bb);
|
||||||
|
for (BasicBlock* succ : bb->getSuccessors()) {
|
||||||
|
if (visited.find(succ) == visited.end()) {
|
||||||
|
dfs_rpo(succ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
postOrder.push_back(bb);
|
||||||
|
};
|
||||||
|
|
||||||
|
dfs_rpo(F->getEntryBlock());
|
||||||
|
std::reverse(postOrder.begin(), postOrder.end());
|
||||||
|
|
||||||
|
if (DEBUG) {
|
||||||
|
std::cout << "--- Computed RPO: ";
|
||||||
|
for (BasicBlock* bb : postOrder) {
|
||||||
|
std::cout << bb->getName() << " ";
|
||||||
|
}
|
||||||
|
std::cout << "---" << std::endl;
|
||||||
|
}
|
||||||
|
return postOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
// computeDominators 方法 (保持不变,因为它它是独立于IDom算法的)
|
||||||
void DominatorTree::computeDominators(Function *F) {
|
void DominatorTree::computeDominators(Function *F) {
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
std::cout << "--- Computing Dominators ---" << std::endl;
|
std::cout << "--- Computing Dominators ---" << std::endl;
|
||||||
|
|
||||||
BasicBlock *entryBlock = F->getEntryBlock();
|
BasicBlock *entryBlock = F->getEntryBlock();
|
||||||
std::vector<BasicBlock *> bbs_in_order; // 用于确定遍历顺序,如果需要的话
|
std::vector<BasicBlock*> bbs_rpo = computeReversePostOrder(F);
|
||||||
|
|
||||||
// 初始化:入口块只被自己支配,其他块被所有块支配
|
for (BasicBlock *bb : bbs_rpo) {
|
||||||
for (const auto &bb_ptr : F->getBasicBlocks()) {
|
|
||||||
BasicBlock *bb = bb_ptr.get();
|
|
||||||
bbs_in_order.push_back(bb); // 收集所有块
|
|
||||||
if (bb == entryBlock) {
|
if (bb == entryBlock) {
|
||||||
|
Dominators[bb].clear();
|
||||||
Dominators[bb].insert(bb);
|
Dominators[bb].insert(bb);
|
||||||
if (DEBUG)
|
if (DEBUG) std::cout << "Init Dominators[" << bb->getName() << "]: {" << bb->getName() << "}" << std::endl;
|
||||||
std::cout << "Init Dominators[" << bb->getName() << "]: {" << bb->getName() << "}" << std::endl;
|
|
||||||
} else {
|
} else {
|
||||||
for (const auto &all_bb_ptr : F->getBasicBlocks()) {
|
Dominators[bb].clear();
|
||||||
Dominators[bb].insert(all_bb_ptr.get());
|
for (BasicBlock *all_bb : bbs_rpo) {
|
||||||
|
Dominators[bb].insert(all_bb);
|
||||||
}
|
}
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
std::cout << "Init Dominators[" << bb->getName() << "]: ";
|
std::cout << "Init Dominators[" << bb->getName() << "]: ";
|
||||||
@ -94,23 +131,18 @@ void DominatorTree::computeDominators(Function *F) {
|
|||||||
while (changed) {
|
while (changed) {
|
||||||
changed = false;
|
changed = false;
|
||||||
iteration++;
|
iteration++;
|
||||||
if (DEBUG)
|
if (DEBUG) std::cout << "Iteration " << iteration << std::endl;
|
||||||
std::cout << "Iteration " << iteration << std::endl;
|
|
||||||
|
|
||||||
// 确保遍历顺序一致性,例如可以按照DFS或BFS顺序,或者简单的迭代器顺序
|
for (BasicBlock *bb : bbs_rpo) {
|
||||||
// 如果Function::getBasicBlocks()返回的迭代器顺序稳定,则无需bbs_in_order
|
if (bb == entryBlock) continue;
|
||||||
for (const auto &bb_ptr : F->getBasicBlocks()) { // 假设这个迭代器顺序稳定
|
|
||||||
BasicBlock *bb = bb_ptr.get();
|
|
||||||
if (bb == entryBlock)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// 计算所有前驱的支配者集合的交集
|
|
||||||
std::set<BasicBlock *> newDom;
|
std::set<BasicBlock *> newDom;
|
||||||
bool firstPredProcessed = false;
|
bool firstPredProcessed = false;
|
||||||
|
|
||||||
for (BasicBlock *pred : bb->getPredecessors()) {
|
for (BasicBlock *pred : bb->getPredecessors()) {
|
||||||
// 确保前驱的支配者集合已经计算过
|
if(DEBUG){
|
||||||
if (Dominators.count(pred)) {
|
std::cout << " Processing predecessor: " << pred->getName() << std::endl;
|
||||||
|
}
|
||||||
if (!firstPredProcessed) {
|
if (!firstPredProcessed) {
|
||||||
newDom = Dominators[pred];
|
newDom = Dominators[pred];
|
||||||
firstPredProcessed = true;
|
firstPredProcessed = true;
|
||||||
@ -121,8 +153,7 @@ void DominatorTree::computeDominators(Function *F) {
|
|||||||
newDom = intersection;
|
newDom = intersection;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
newDom.insert(bb);
|
||||||
newDom.insert(bb); // BB 永远支配自己
|
|
||||||
|
|
||||||
if (newDom != Dominators[bb]) {
|
if (newDom != Dominators[bb]) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
@ -140,78 +171,242 @@ void DominatorTree::computeDominators(Function *F) {
|
|||||||
std::cout << "--- Dominators Computation Finished ---" << std::endl;
|
std::cout << "--- Dominators Computation Finished ---" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==============================================================
|
||||||
|
// Lengauer-Tarjan 算法辅助数据结构和函数 (私有成员)
|
||||||
|
// ==============================================================
|
||||||
|
|
||||||
|
// DFS 遍历,填充 dfnum_map, vertex_vec, parent_map
|
||||||
|
// 对应用户代码的 dfs 函数
|
||||||
|
void DominatorTree::dfs_lt_helper(BasicBlock* u) {
|
||||||
|
dfnum_map[u] = df_counter;
|
||||||
|
if (df_counter >= vertex_vec.size()) { // 动态调整大小
|
||||||
|
vertex_vec.resize(df_counter + 1);
|
||||||
|
}
|
||||||
|
vertex_vec[df_counter] = u;
|
||||||
|
if (DEBUG) std::cout << " DFS: Visiting " << u->getName() << ", dfnum = " << df_counter << std::endl;
|
||||||
|
df_counter++;
|
||||||
|
|
||||||
|
for (BasicBlock* v : u->getSuccessors()) {
|
||||||
|
if (dfnum_map.find(v) == dfnum_map.end()) { // 如果 v 未访问过
|
||||||
|
parent_map[v] = u;
|
||||||
|
if (DEBUG) std::cout << " DFS: Setting parent[" << v->getName() << "] = " << u->getName() << std::endl;
|
||||||
|
dfs_lt_helper(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 并查集:找到集合的代表,并进行路径压缩
|
||||||
|
// 同时更新 label,确保 label[i] 总是指向其祖先链中 sdom_map 最小的节点
|
||||||
|
// 对应用户代码的 find 函数,也包含了 eval 的逻辑
|
||||||
|
BasicBlock* DominatorTree::evalAndCompress_lt_helper(BasicBlock* i) {
|
||||||
|
if (DEBUG) std::cout << " Eval: Processing " << i->getName() << std::endl;
|
||||||
|
// 如果 i 是根 (ancestor_map[i] == nullptr)
|
||||||
|
if (ancestor_map.find(i) == ancestor_map.end() || ancestor_map[i] == nullptr) {
|
||||||
|
if (DEBUG) std::cout << " Eval: " << i->getName() << " is root, returning itself." << std::endl;
|
||||||
|
return i; // 根节点自身就是路径上sdom最小的,因为它没有祖先
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果 i 的祖先不是根,则递归查找并进行路径压缩
|
||||||
|
BasicBlock* root_ancestor = evalAndCompress_lt_helper(ancestor_map[i]);
|
||||||
|
|
||||||
|
// 路径压缩时,根据 sdom_map 比较并更新 label_map
|
||||||
|
// 确保 label_map[i] 存储的是 i 到 root_ancestor 路径上 sdom_map 最小的节点
|
||||||
|
// 注意:这里的 ancestor_map[i] 已经被递归调用压缩过一次了,所以是root_ancestor的旧路径
|
||||||
|
// 应该比较的是 label_map[ancestor_map[i]] 和 label_map[i]
|
||||||
|
if (sdom_map.count(label_map[ancestor_map[i]]) && // 确保 label_map[ancestor_map[i]] 存在 sdom
|
||||||
|
sdom_map.count(label_map[i]) && // 确保 label_map[i] 存在 sdom
|
||||||
|
dfnum_map[sdom_map[label_map[ancestor_map[i]]]] < dfnum_map[sdom_map[label_map[i]]]) {
|
||||||
|
if (DEBUG) std::cout << " Eval: Updating label for " << i->getName() << " from "
|
||||||
|
<< label_map[i]->getName() << " to " << label_map[ancestor_map[i]]->getName() << std::endl;
|
||||||
|
label_map[i] = label_map[ancestor_map[i]];
|
||||||
|
}
|
||||||
|
|
||||||
|
ancestor_map[i] = root_ancestor; // 执行路径压缩:将 i 直接指向其所属集合的根
|
||||||
|
if (DEBUG) std::cout << " Eval: Path compression for " << i->getName() << ", new ancestor = "
|
||||||
|
<< (root_ancestor ? root_ancestor->getName() : "nullptr") << std::endl;
|
||||||
|
|
||||||
|
return label_map[i]; // <-- **将这里改为返回 label_map[i]**
|
||||||
|
}
|
||||||
|
|
||||||
|
// Link 函数:将 v 加入 u 的 DFS 树子树中 (实际上是并查集操作)
|
||||||
|
// 对应用户代码的 fa[u] = fth[u];
|
||||||
|
void DominatorTree::link_lt_helper(BasicBlock* u_parent, BasicBlock* v_child) {
|
||||||
|
ancestor_map[v_child] = u_parent; // 设置并查集父节点
|
||||||
|
label_map[v_child] = v_child; // 初始化 label 为自身
|
||||||
|
if (DEBUG) std::cout << " Link: " << v_child->getName() << " linked to " << u_parent->getName() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==============================================================
|
||||||
|
// Lengauer-Tarjan 算法实现 computeIDoms
|
||||||
|
// ==============================================================
|
||||||
void DominatorTree::computeIDoms(Function *F) {
|
void DominatorTree::computeIDoms(Function *F) {
|
||||||
if (DEBUG)
|
if (DEBUG) std::cout << "--- Computing Immediate Dominators (IDoms) using Lengauer-Tarjan ---" << std::endl;
|
||||||
std::cout << "--- Computing Immediate Dominators (IDoms) ---" << std::endl;
|
|
||||||
|
|
||||||
BasicBlock *entryBlock = F->getEntryBlock();
|
BasicBlock *entryBlock = F->getEntryBlock();
|
||||||
IDoms[entryBlock] = nullptr; // 入口块没有即时支配者
|
|
||||||
|
|
||||||
// 遍历所有非入口块
|
// 1. 初始化所有 LT 相关的数据结构
|
||||||
for (const auto &bb_ptr : F->getBasicBlocks()) {
|
dfnum_map.clear();
|
||||||
|
vertex_vec.clear();
|
||||||
|
parent_map.clear();
|
||||||
|
sdom_map.clear();
|
||||||
|
idom_map.clear();
|
||||||
|
bucket_map.clear();
|
||||||
|
ancestor_map.clear();
|
||||||
|
label_map.clear();
|
||||||
|
df_counter = 0; // DFS 计数器从 0 开始
|
||||||
|
|
||||||
|
// 预分配 vertex_vec 的大小,避免频繁resize
|
||||||
|
vertex_vec.resize(F->getBasicBlocks().size() + 1);
|
||||||
|
// 在 DFS 遍历之前,先为所有基本块初始化 sdom 和 label
|
||||||
|
// 这是 Lengauer-Tarjan 算法的要求,确保所有节点在 Phase 2 开始前都在 map 中
|
||||||
|
for (auto &bb_ptr : F->getBasicBlocks()) {
|
||||||
BasicBlock* bb = bb_ptr.get();
|
BasicBlock* bb = bb_ptr.get();
|
||||||
if (bb == entryBlock)
|
sdom_map[bb] = bb; // sdom(bb) 初始化为 bb 自身
|
||||||
continue;
|
label_map[bb] = bb; // label(bb) 初始化为 bb 自身 (用于 Union-Find 的路径压缩)
|
||||||
|
}
|
||||||
|
// 确保入口块也被正确初始化(如果它不在 F->getBasicBlocks() 的正常迭代中)
|
||||||
|
sdom_map[entryBlock] = entryBlock;
|
||||||
|
label_map[entryBlock] = entryBlock;
|
||||||
|
// Phase 1: DFS 遍历并预处理
|
||||||
|
// 对应用户代码的 dfs(st)
|
||||||
|
dfs_lt_helper(entryBlock);
|
||||||
|
idom_map[entryBlock] = nullptr; // 入口块没有即时支配者
|
||||||
|
if (DEBUG) std::cout << " IDom[" << entryBlock->getName() << "] = nullptr" << std::endl;
|
||||||
|
|
||||||
BasicBlock *currentIDom = nullptr;
|
if (DEBUG) std::cout << " Sdom[" << entryBlock->getName() << "] = " << entryBlock->getName() << std::endl;
|
||||||
const std::set<BasicBlock *> *domsOfBB = getDominators(bb);
|
|
||||||
if (!domsOfBB) {
|
// 初始化并查集的祖先和 label
|
||||||
if (DEBUG)
|
for (auto const& [bb_key, dfn_val] : dfnum_map) {
|
||||||
std::cerr << "Warning: Dominators for " << bb->getName() << " not found!" << std::endl;
|
ancestor_map[bb_key] = nullptr; // 初始为独立集合的根
|
||||||
continue;
|
label_map[bb_key] = bb_key; // 初始 label 为自身
|
||||||
}
|
}
|
||||||
|
|
||||||
// 遍历bb的所有严格支配者 D (即 bb 的支配者中除了 bb 自身)
|
|
||||||
for (BasicBlock *D_candidate : *domsOfBB) {
|
|
||||||
if (D_candidate == bb)
|
|
||||||
continue; // 跳过bb自身
|
|
||||||
|
|
||||||
bool D_candidate_is_IDom = true;
|
|
||||||
// 检查是否存在另一个块 X,使得 D_candidate 严格支配 X 且 X 严格支配 bb
|
|
||||||
// 或者更直接的,检查 D_candidate 是否被 bb 的所有其他严格支配者所支配
|
|
||||||
for (BasicBlock *X_other_dom : *domsOfBB) {
|
|
||||||
if (X_other_dom == bb || X_other_dom == D_candidate)
|
|
||||||
continue; // 跳过bb自身和D_candidate
|
|
||||||
|
|
||||||
// 如果 X_other_dom 严格支配 bb (它在 domsOfBB 中且不是bb自身)
|
|
||||||
// 并且 X_other_dom 不被 D_candidate 支配,那么 D_candidate 就不是 IDom
|
|
||||||
const std::set<BasicBlock *> *domsOfX_other_dom = getDominators(X_other_dom);
|
|
||||||
if (domsOfX_other_dom && domsOfX_other_dom->count(D_candidate)) { // X_other_dom 支配 D_candidate
|
|
||||||
// D_candidate 被另一个支配者 X_other_dom 支配
|
|
||||||
// 这说明 D_candidate 位于 X_other_dom 的“下方”,X_other_dom 更接近 bb
|
|
||||||
// 因此 D_candidate 不是 IDom
|
|
||||||
D_candidate_is_IDom = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (D_candidate_is_IDom) {
|
|
||||||
currentIDom = D_candidate;
|
|
||||||
break; // 找到即时支配者,可以退出循环,因为它是唯一的
|
|
||||||
}
|
|
||||||
}
|
|
||||||
IDoms[bb] = currentIDom;
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
std::cout << " IDom[" << bb->getName() << "] = " << (currentIDom ? currentIDom->getName() : "nullptr")
|
std::cout << " --- DFS Phase Complete ---" << std::endl;
|
||||||
<< std::endl;
|
std::cout << " dfnum_map:" << std::endl;
|
||||||
|
for (auto const& [bb, dfn] : dfnum_map) {
|
||||||
|
std::cout << " " << bb->getName() << " -> " << dfn << std::endl;
|
||||||
}
|
}
|
||||||
|
std::cout << " vertex_vec (by dfnum):" << std::endl;
|
||||||
|
for (size_t k = 0; k < df_counter; ++k) {
|
||||||
|
if (vertex_vec[k]) std::cout << " [" << k << "] -> " << vertex_vec[k]->getName() << std::endl;
|
||||||
}
|
}
|
||||||
if (DEBUG)
|
std::cout << " parent_map:" << std::endl;
|
||||||
std::cout << "--- Immediate Dominators Computation Finished ---" << std::endl;
|
for (auto const& [child, parent] : parent_map) {
|
||||||
|
std::cout << " " << child->getName() << " -> " << (parent ? parent->getName() : "nullptr") << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << " ------------------------" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
for each node n in a postorder traversal of the dominator tree:
|
// Phase 2: 计算半支配者 (sdom)
|
||||||
df[n] = empty set
|
// 对应用户代码的 for (int i = dfc; i >= 2; --i) 循环的上半部分
|
||||||
// compute DF_local(n)
|
// 按照 DFS 编号递减的顺序遍历所有节点 (除了 entryBlock,它的 DFS 编号是 0)
|
||||||
for each child y of n in the CFG:
|
if (DEBUG) std::cout << "--- Phase 2: Computing Semi-Dominators (sdom) ---" << std::endl;
|
||||||
if idom[y] != n:
|
for (int i = df_counter - 1; i >= 1; --i) { // 从 DFS 编号最大的节点开始,到 1
|
||||||
df[n] = df[n] U {y}
|
BasicBlock* w = vertex_vec[i]; // 当前处理的节点
|
||||||
// compute DF_up(n)
|
if (DEBUG) std::cout << " Processing node w: " << w->getName() << " (dfnum=" << i << ")" << std::endl;
|
||||||
for each child c of n in the dominator tree:
|
|
||||||
for each element w in df[c]:
|
|
||||||
if idom[w] != n:
|
// 对于 w 的每个前驱 v
|
||||||
df[n] = df[n] U {w}
|
for (BasicBlock* v : w->getPredecessors()) {
|
||||||
*/
|
if (DEBUG) std::cout << " Considering predecessor v: " << v->getName() << std::endl;
|
||||||
|
// 如果前驱 v 未被 DFS 访问过 (即不在 dfnum_map 中),则跳过
|
||||||
|
if (dfnum_map.find(v) == dfnum_map.end()) {
|
||||||
|
if (DEBUG) std::cout << " Predecessor " << v->getName() << " not in DFS tree, skipping." << std::endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用 evalAndCompress 来找到 v 在其 DFS 树祖先链上具有最小 sdom 的节点
|
||||||
|
BasicBlock* u_with_min_sdom_on_path = evalAndCompress_lt_helper(v);
|
||||||
|
if (DEBUG) std::cout << " Eval(" << v->getName() << ") returned "
|
||||||
|
<< u_with_min_sdom_on_path->getName() << std::endl;
|
||||||
|
if (DEBUG && sdom_map.count(u_with_min_sdom_on_path) && sdom_map.count(w)) {
|
||||||
|
std::cout << " Comparing sdom: dfnum[" << sdom_map[u_with_min_sdom_on_path]->getName() << "] (" << dfnum_map[sdom_map[u_with_min_sdom_on_path]]
|
||||||
|
<< ") vs dfnum[" << sdom_map[w]->getName() << "] (" << dfnum_map[sdom_map[w]] << ")" << std::endl;
|
||||||
|
}
|
||||||
|
// 比较 sdom(u) 和 sdom(w)
|
||||||
|
if (sdom_map.count(u_with_min_sdom_on_path) && sdom_map.count(w) &&
|
||||||
|
dfnum_map[sdom_map[u_with_min_sdom_on_path]] < dfnum_map[sdom_map[w]]) {
|
||||||
|
if (DEBUG) std::cout << " Updating sdom[" << w->getName() << "] from "
|
||||||
|
<< sdom_map[w]->getName() << " to "
|
||||||
|
<< sdom_map[u_with_min_sdom_on_path]->getName() << std::endl;
|
||||||
|
sdom_map[w] = sdom_map[u_with_min_sdom_on_path]; // 更新 sdom(w)
|
||||||
|
if (DEBUG) std::cout << " Sdom update applied. New sdom[" << w->getName() << "] = " << sdom_map[w]->getName() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 w 加入 sdom(w) 对应的桶中
|
||||||
|
bucket_map[sdom_map[w]].push_back(w);
|
||||||
|
if (DEBUG) std::cout << " Adding " << w->getName() << " to bucket of sdom(" << w->getName() << "): "
|
||||||
|
<< sdom_map[w]->getName() << std::endl;
|
||||||
|
|
||||||
|
// 将 w 的父节点加入并查集 (link 操作)
|
||||||
|
if (parent_map.count(w) && parent_map[w] != nullptr) {
|
||||||
|
link_lt_helper(parent_map[w], w);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Phase 3-part 1: 处理 parent[w] 的桶中所有节点,确定部分 idom
|
||||||
|
if (parent_map.count(w) && parent_map[w] != nullptr) {
|
||||||
|
BasicBlock* p = parent_map[w]; // p 是 w 的父节点
|
||||||
|
if (DEBUG) std::cout << " Processing bucket for parent " << p->getName() << std::endl;
|
||||||
|
|
||||||
|
// 注意:这里需要复制桶的内容,因为原始桶在循环中会被clear
|
||||||
|
std::vector<BasicBlock*> nodes_in_p_bucket_copy = bucket_map[p];
|
||||||
|
for (BasicBlock* y : nodes_in_p_bucket_copy) {
|
||||||
|
if (DEBUG) std::cout << " Processing node y from bucket: " << y->getName() << std::endl;
|
||||||
|
// 找到 y 在其 DFS 树祖先链上具有最小 sdom 的节点
|
||||||
|
BasicBlock* u = evalAndCompress_lt_helper(y);
|
||||||
|
if (DEBUG) std::cout << " Eval(" << y->getName() << ") returned " << u->getName() << std::endl;
|
||||||
|
|
||||||
|
// 确定 idom(y)
|
||||||
|
// if sdom(eval(y)) == sdom(parent(w)), then idom(y) = parent(w)
|
||||||
|
// else idom(y) = eval(y)
|
||||||
|
if (sdom_map.count(u) && sdom_map.count(p) &&
|
||||||
|
dfnum_map[sdom_map[u]] < dfnum_map[sdom_map[p]]) {
|
||||||
|
idom_map[y] = u; // 确定的 idom
|
||||||
|
if (DEBUG) std::cout << " IDom[" << y->getName() << "] set to " << u->getName() << std::endl;
|
||||||
|
} else {
|
||||||
|
idom_map[y] = p; // p 是 y 的 idom
|
||||||
|
if (DEBUG) std::cout << " IDom[" << y->getName() << "] set to " << p->getName() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bucket_map[p].clear(); // 清空桶,防止重复处理
|
||||||
|
if (DEBUG) std::cout << " Cleared bucket for parent " << p->getName() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Phase 3-part 2: 最终确定 idom (处理那些 idom != sdom 的节点)
|
||||||
|
if (DEBUG) std::cout << "--- Phase 3: Finalizing Immediate Dominators (idom) ---" << std::endl;
|
||||||
|
for (int i = 1; i < df_counter; ++i) { // 从 DFS 编号最小的节点 (除了 entryBlock) 开始
|
||||||
|
BasicBlock* w = vertex_vec[i];
|
||||||
|
if (DEBUG) std::cout << " Finalizing node w: " << w->getName() << std::endl;
|
||||||
|
if (idom_map.count(w) && sdom_map.count(w) && idom_map[w] != sdom_map[w]) {
|
||||||
|
// idom[w] 的 idom 是其真正的 idom
|
||||||
|
if (DEBUG) std::cout << " idom[" << w->getName() << "] (" << idom_map[w]->getName()
|
||||||
|
<< ") != sdom[" << w->getName() << "] (" << sdom_map[w]->getName() << ")" << std::endl;
|
||||||
|
if (idom_map.count(idom_map[w])) {
|
||||||
|
idom_map[w] = idom_map[idom_map[w]];
|
||||||
|
if (DEBUG) std::cout << " Updating idom[" << w->getName() << "] to idom(idom(w)): "
|
||||||
|
<< idom_map[w]->getName() << std::endl;
|
||||||
|
} else {
|
||||||
|
if (DEBUG) std::cout << " Warning: idom(idom(" << w->getName() << ")) not found, leaving idom[" << w->getName() << "] as is." << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (DEBUG) {
|
||||||
|
std::cout << " Final IDom[" << w->getName() << "] = " << (idom_map[w] ? idom_map[w]->getName() : "nullptr") << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将计算结果从 idom_map 存储到 DominatorTree 的成员变量 IDoms 中
|
||||||
|
IDoms = idom_map;
|
||||||
|
|
||||||
|
if (DEBUG) std::cout << "--- Immediate Dominators Computation Finished ---" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==============================================================
|
||||||
|
// computeDominanceFrontiers 和 computeDominatorTreeChildren (保持不变)
|
||||||
|
// ==============================================================
|
||||||
|
|
||||||
void DominatorTree::computeDominanceFrontiers(Function *F) {
|
void DominatorTree::computeDominanceFrontiers(Function *F) {
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
@ -221,21 +416,17 @@ void DominatorTree::computeDominanceFrontiers(Function *F) {
|
|||||||
BasicBlock *X = bb_ptr_X.get();
|
BasicBlock *X = bb_ptr_X.get();
|
||||||
DominanceFrontiers[X].clear();
|
DominanceFrontiers[X].clear();
|
||||||
|
|
||||||
// 遍历所有可能的 Z (X支配Z,或者Z就是X)
|
|
||||||
for (const auto &bb_ptr_Z : F->getBasicBlocks()) {
|
for (const auto &bb_ptr_Z : F->getBasicBlocks()) {
|
||||||
BasicBlock *Z = bb_ptr_Z.get();
|
BasicBlock *Z = bb_ptr_Z.get();
|
||||||
const std::set<BasicBlock *> *domsOfZ = getDominators(Z);
|
const std::set<BasicBlock *> *domsOfZ = getDominators(Z);
|
||||||
|
|
||||||
// 如果 X 不支配 Z,则 Z 与 DF(X) 无关
|
if (!domsOfZ || domsOfZ->find(X) == domsOfZ->end()) { // Z 不被 X 支配
|
||||||
if (!domsOfZ || domsOfZ->find(X) == domsOfZ->end()) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 遍历 Z 的所有后继 Y
|
|
||||||
for (BasicBlock *Y : Z->getSuccessors()) {
|
for (BasicBlock *Y : Z->getSuccessors()) {
|
||||||
// 如果 Y 不被 X 严格支配,则 Y 在 DF(X) 中
|
|
||||||
// Y 不被 X 严格支配意味着 (Y不被X支配) 或 (Y就是X)
|
|
||||||
const std::set<BasicBlock *> *domsOfY = getDominators(Y);
|
const std::set<BasicBlock *> *domsOfY = getDominators(Y);
|
||||||
|
// 如果 Y == X,或者 Y 不被 X 严格支配 (即 Y 不被 X 支配)
|
||||||
if (Y == X || (domsOfY && domsOfY->find(X) == domsOfY->end())) {
|
if (Y == X || (domsOfY && domsOfY->find(X) == domsOfY->end())) {
|
||||||
DominanceFrontiers[X].insert(Y);
|
DominanceFrontiers[X].insert(Y);
|
||||||
}
|
}
|
||||||
@ -274,23 +465,21 @@ void DominatorTree::computeDominatorTreeChildren(Function *F) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ==============================================================
|
// ==============================================================
|
||||||
// DominatorTreeAnalysisPass 的实现
|
// DominatorTreeAnalysisPass 的实现 (保持不变)
|
||||||
// ==============================================================
|
// ==============================================================
|
||||||
|
|
||||||
bool DominatorTreeAnalysisPass::runOnFunction(Function *F, AnalysisManager &AM) {
|
bool DominatorTreeAnalysisPass::runOnFunction(Function *F, AnalysisManager &AM) {
|
||||||
// 每次运行时清空旧数据,确保重新计算
|
// 每次运行时清空旧数据,确保重新计算
|
||||||
CurrentDominatorTree = std::make_unique<DominatorTree>(F);
|
CurrentDominatorTree = std::make_unique<DominatorTree>(F);
|
||||||
// 不需要手动清空map,unique_ptr会创建新的DominatorTree对象,其map是空的
|
|
||||||
|
|
||||||
CurrentDominatorTree->computeDominators(F);
|
CurrentDominatorTree->computeDominators(F);
|
||||||
CurrentDominatorTree->computeIDoms(F); // 修正后的IDoms算法
|
CurrentDominatorTree->computeIDoms(F); // 修正后的LT算法
|
||||||
CurrentDominatorTree->computeDominanceFrontiers(F);
|
CurrentDominatorTree->computeDominanceFrontiers(F);
|
||||||
CurrentDominatorTree->computeDominatorTreeChildren(F);
|
CurrentDominatorTree->computeDominatorTreeChildren(F);
|
||||||
return false; // 分析遍通常返回 false,表示不修改 IR
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<AnalysisResultBase> DominatorTreeAnalysisPass::getResult() {
|
std::unique_ptr<AnalysisResultBase> DominatorTreeAnalysisPass::getResult() {
|
||||||
// 返回计算好的 DominatorTree 实例,所有权转移给 AnalysisManager
|
|
||||||
return std::move(CurrentDominatorTree);
|
return std::move(CurrentDominatorTree);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -58,14 +58,14 @@ void PassManager::runOptimizationPipeline(Module* moduleIR, IRBuilder* builderIR
|
|||||||
if (DEBUG) std::cout << "Applying -O1 optimizations.\n";
|
if (DEBUG) std::cout << "Applying -O1 optimizations.\n";
|
||||||
if (DEBUG) std::cout << "--- Running custom optimization sequence ---\n";
|
if (DEBUG) std::cout << "--- Running custom optimization sequence ---\n";
|
||||||
|
|
||||||
this->clearPasses();
|
// this->clearPasses();
|
||||||
this->addPass(&SysYDelInstAfterBrPass::ID);
|
// this->addPass(&SysYDelInstAfterBrPass::ID);
|
||||||
this->addPass(&SysYDelNoPreBLockPass::ID);
|
// this->addPass(&SysYDelNoPreBLockPass::ID);
|
||||||
this->addPass(&SysYBlockMergePass::ID);
|
// this->addPass(&SysYBlockMergePass::ID);
|
||||||
this->addPass(&SysYDelEmptyBlockPass::ID);
|
// this->addPass(&SysYDelEmptyBlockPass::ID);
|
||||||
this->addPass(&SysYCondBr2BrPass::ID);
|
// this->addPass(&SysYCondBr2BrPass::ID);
|
||||||
this->addPass(&SysYAddReturnPass::ID);
|
// this->addPass(&SysYAddReturnPass::ID);
|
||||||
this->run();
|
// this->run();
|
||||||
|
|
||||||
if(DEBUG) {
|
if(DEBUG) {
|
||||||
std::cout << "=== IR After CFGOpt Optimizations ===\n";
|
std::cout << "=== IR After CFGOpt Optimizations ===\n";
|
||||||
|
|||||||
@ -1037,6 +1037,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
|||||||
|
|
||||||
// 从 entryBB 无条件跳转到 funcBodyEntry
|
// 从 entryBB 无条件跳转到 funcBodyEntry
|
||||||
builder.createUncondBrInst(funcBodyEntry);
|
builder.createUncondBrInst(funcBodyEntry);
|
||||||
|
BasicBlock::conectBlocks(entry, funcBodyEntry); // 连接 entryBB 和 funcBodyEntry
|
||||||
builder.setPosition(funcBodyEntry,funcBodyEntry->end()); // 将插入点设置到 funcBodyEntry
|
builder.setPosition(funcBodyEntry,funcBodyEntry->end()); // 将插入点设置到 funcBodyEntry
|
||||||
|
|
||||||
for (auto item : ctx->blockStmt()->blockItem()) {
|
for (auto item : ctx->blockStmt()->blockItem()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user