[midend-SCCP]头文件构架完毕,cpp文件部分报错暂时不commit
This commit is contained in:
@@ -1,19 +1,17 @@
|
||||
#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 {
|
||||
|
||||
@@ -24,7 +22,6 @@ Constant, // c (常量)
|
||||
Bottom // ⊥ (不确定 / 变化 / 未定义)
|
||||
};
|
||||
|
||||
// 用于表示 SSA 值的具体状态(包含格值和常量值)
|
||||
// 新增枚举来区分常量的实际类型
|
||||
enum class ValueType {
|
||||
Integer,
|
||||
@@ -32,9 +29,10 @@ enum class ValueType {
|
||||
Unknown // 用于 Top 和 Bottom 状态
|
||||
};
|
||||
|
||||
// 用于表示 SSA 值的具体状态(包含格值和常量值)
|
||||
struct SSAPValue {
|
||||
LatticeVal state;
|
||||
ConstVal constantVal; // 使用 std::variant 存储 int 或 float
|
||||
std::variant<int, float> constantVal; // 使用 std::variant 存储 int 或 float
|
||||
ValueType constant_type; // 记录常量是整数还是浮点数
|
||||
|
||||
// 默认构造函数,初始化为 Top
|
||||
@@ -53,8 +51,7 @@ struct SSAPValue {
|
||||
if (state != other.state)
|
||||
return false;
|
||||
if (state == LatticeVal::Constant) {
|
||||
if (constant_type != other.constant_type)
|
||||
return false; // 类型必须匹配
|
||||
if (constant_type != other.constant_type) return false; // 类型必须匹配
|
||||
return constantVal == other.constantVal; // std::variant 会比较内部值
|
||||
}
|
||||
return true; // Top == Top, Bottom == Bottom
|
||||
@@ -77,7 +74,7 @@ private:
|
||||
std::map<Value *, SSAPValue> valueState;
|
||||
// 可执行基本块集合
|
||||
std::unordered_set<BasicBlock *> executableBlocks;
|
||||
// 追踪已访问的CFG边,防止重复添加
|
||||
// 追踪已访问的CFG边,防止重复添加,使用 SysYIROptUtils::PairHash
|
||||
std::unordered_set<std::pair<BasicBlock*, BasicBlock*>, SysYIROptUtils::PairHash> visitedCFGEdges;
|
||||
|
||||
// 辅助函数:格操作 Meet
|
||||
@@ -95,18 +92,14 @@ private:
|
||||
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);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user