From 61768fa180dd89591b10eb285b26829231f4c179 Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Thu, 31 Jul 2025 17:00:02 +0800 Subject: [PATCH] =?UTF-8?q?[midend-SCCP]=E5=A4=B4=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=9E=84=E6=9E=B6=E5=AE=8C=E6=AF=95=EF=BC=8Ccpp=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=83=A8=E5=88=86=E6=8A=A5=E9=94=99=E6=9A=82=E6=97=B6?= =?UTF-8?q?=E4=B8=8Dcommit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/include/midend/Pass/Optimize/SCCP.h | 50 ++++++++++--------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/include/midend/Pass/Optimize/SCCP.h b/src/include/midend/Pass/Optimize/SCCP.h index 8a16eb1..667ee51 100644 --- a/src/include/midend/Pass/Optimize/SCCP.h +++ b/src/include/midend/Pass/Optimize/SCCP.h @@ -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   // For assert -#include // 引入 std::function 用于辅助函数 -#include // For DEBUG output +#include "IR.h" +#include "Pass.h" +#include "SysYIROptUtils.h" +#include +#include #include #include #include #include #include -#include // 引入 std::variant 用于 ConstVal - -using ConstVal = std::variant; // 定义一个变体类型,用于存储整数或浮点数常量 +#include +#include 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 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 valueState; // 可执行基本块集合 std::unordered_set executableBlocks; - // 追踪已访问的CFG边,防止重复添加 - std::unordered_set, SysYIROptUtils::PairHash> visitedCFGEdges; + // 追踪已访问的CFG边,防止重复添加,使用 SysYIROptUtils::PairHash + std::unordered_set, 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 &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); };