[midend-SCCP]头文件构架完毕,cpp文件部分报错暂时不commit

This commit is contained in:
rain2133
2025-07-31 17:00:02 +08:00
parent 520ebd96f0
commit 61768fa180

View File

@@ -1,30 +1,27 @@
#pragma once
#include "IR.h" // 假设这是你的 SysY IR 定义,包含 Value, Instruction, BasicBlock, Function, Module 等
#include "Pass.h" // 包含 Pass 的基类定义,以及 AnalysisManager, IRBuilder
#include "SysYIROptUtils.h" // 假设包含 SysYIROptUtils::usedelete 等辅助函数
#include <cassert>   // For assert
#include <functional> // 引入 std::function 用于辅助函数
#include <iostream> // For DEBUG output
#include "IR.h"
#include "Pass.h"
#include "SysYIROptUtils.h"
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <vector>
#include <variant> // 引入 std::variant 用于 ConstVal
using ConstVal = std::variant<int, float>; // 定义一个变体类型,用于存储整数或浮点数常量
#include <variant>
#include <functional>
namespace sysy {
// 定义三值格 (Three-valued Lattice) 的状态
enum class LatticeVal {
Top, // (未知 / 未初始化)
Constant, // c (常量)
Bottom// ⊥ (不确定 / 变化 / 未定义)
Top, // (未知 / 未初始化)
Constant, // c (常量)
Bottom // ⊥ (不确定 / 变化 / 未定义)
};
// 用于表示 SSA 值的具体状态(包含格值和常量值)
// 新增枚举来区分常量的实际类型
enum class ValueType {
Integer,
@@ -32,10 +29,11 @@ enum class ValueType {
Unknown // 用于 Top 和 Bottom 状态
};
// 用于表示 SSA 值的具体状态(包含格值和常量值)
struct SSAPValue {
LatticeVal state;
ConstVal constantVal; // 使用 std::variant 存储 int 或 float
ValueType constant_type; // 记录常量是整数还是浮点数
std::variant<int, float> constantVal; // 使用 std::variant 存储 int 或 float
ValueType constant_type; // 记录常量是整数还是浮点数
// 默认构造函数,初始化为 Top
SSAPValue() : state(LatticeVal::Top), constantVal(0), constant_type(ValueType::Unknown) {}
@@ -53,9 +51,8 @@ struct SSAPValue {
if (state != other.state)
return false;
if (state == LatticeVal::Constant) {
if (constant_type != other.constant_type)
return false; // 类型必须匹配
return constantVal == other.constantVal; // std::variant 会比较内部值
if (constant_type != other.constant_type) return false; // 类型必须匹配
return constantVal == other.constantVal; // std::variant 会比较内部值
}
return true; // Top == Top, Bottom == Bottom
}
@@ -77,8 +74,8 @@ private:
std::map<Value *, SSAPValue> valueState;
// 可执行基本块集合
std::unordered_set<BasicBlock *> executableBlocks;
// 追踪已访问的CFG边防止重复添加
std::unordered_set<std::pair<BasicBlock *, BasicBlock *>, SysYIROptUtils::PairHash> visitedCFGEdges;
// 追踪已访问的CFG边防止重复添加,使用 SysYIROptUtils::PairHash
std::unordered_set<std::pair<BasicBlock*, BasicBlock*>, SysYIROptUtils::PairHash> visitedCFGEdges;
// 辅助函数:格操作 Meet
SSAPValue Meet(const SSAPValue &a, const SSAPValue &b);
@@ -89,24 +86,20 @@ private:
// 辅助函数:将边加入边工作列表,并更新可执行块
void AddEdgeToWorkList(BasicBlock *fromBB, BasicBlock *toBB);
// 辅助函数:标记一个块为可执行
void MarkBlockExecutable(BasicBlock *block);
void MarkBlockExecutable(BasicBlock* block);
// 辅助函数:对二元操作进行常量折叠
SSAPValue ComputeConstant(BinaryInst *binaryinst, SSAPValue lhsVal, SSAPValue rhsVal);
// 辅助函数:对一元操作进行常量折叠
SSAPValue ComputeConstant(UnaryInst *unaryInst, SSAPValue operandVal);
// 辅助函数:对类型转换进行常量折叠
SSAPValue ComputeConstant(CastInst *castInst, SSAPValue operandVal);
// 主要优化阶段
// 阶段1: 常量传播与折叠
// 返回值表示在此阶段IR是否被修改
bool PropagateConstants(Function *func);
// 阶段2: 控制流简化
// 返回值表示在此阶段IR是否被修改
bool SimplifyControlFlow(Function *func);
// 辅助函数:处理单条指令(这包含了原来 computeLatticeValue 的大部分逻辑)
// 辅助函数:处理单条指令
void ProcessInstruction(Instruction *inst);
// 辅助函数:处理单条控制流边
void ProcessEdge(const std::pair<BasicBlock *, BasicBlock *> &edge);
@@ -117,7 +110,7 @@ private:
// 移除死块
void RemoveDeadBlock(BasicBlock *bb, Function *func);
// 简化分支(将条件分支替换为无条件分支)
void SimplifyBranch(BranchInst *brInst, bool condVal); // 修改为 BranchInst,更通用
void SimplifyBranch(CondBrInst*brInst, bool condVal); // 保持 BranchInst
// 更新前驱块的终结指令(当一个后继块被移除时)
void UpdateTerminator(BasicBlock *predBB, BasicBlock *removedSucc);
// 移除 Phi 节点的入边(当其前驱块被移除时)
@@ -127,9 +120,6 @@ public:
SCCPContext(IRBuilder *builder) : builder(builder) {}
// 运行 SCCP 优化
// func: 当前要优化的函数
// AM: 分析管理器,在 SCCP 中通常不需要获取其他分析结果,但可能需要使分析结果失效。
// 返回值: 如果对函数进行了修改,则为 true
void run(Function *func, AnalysisManager &AM);
};