[midend-GVN] commit头文件
This commit is contained in:
82
src/include/midend/Pass/Optimize/GVN.h
Normal file
82
src/include/midend/Pass/Optimize/GVN.h
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Pass.h"
|
||||||
|
#include "IR.h"
|
||||||
|
#include "Dom.h"
|
||||||
|
#include "SideEffectAnalysis.h"
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace sysy {
|
||||||
|
|
||||||
|
// GVN优化遍的核心逻辑封装类
|
||||||
|
class GVNContext {
|
||||||
|
public:
|
||||||
|
// 运行GVN优化的主要方法
|
||||||
|
void run(Function* func, AnalysisManager* AM, bool& changed);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// 值编号的哈希表:Value -> 代表值
|
||||||
|
std::unordered_map<Value*, Value*> hashtable;
|
||||||
|
|
||||||
|
// 已访问的基本块集合
|
||||||
|
std::unordered_set<BasicBlock*> visited;
|
||||||
|
|
||||||
|
// 逆后序遍历的基本块列表
|
||||||
|
std::vector<BasicBlock*> rpoBlocks;
|
||||||
|
|
||||||
|
// 需要删除的指令集合
|
||||||
|
std::unordered_set<Instruction*> needRemove;
|
||||||
|
|
||||||
|
// 分析结果
|
||||||
|
DominatorTree* domTree = nullptr;
|
||||||
|
SideEffectAnalysisResult* sideEffectAnalysis = nullptr;
|
||||||
|
|
||||||
|
// 计算逆后序遍历
|
||||||
|
void computeRPO(Function* func);
|
||||||
|
void dfs(BasicBlock* bb);
|
||||||
|
|
||||||
|
// 检查哈希表并获取值编号
|
||||||
|
Value* checkHashtable(Value* value);
|
||||||
|
|
||||||
|
// 为不同类型的指令获取值编号
|
||||||
|
Value* getValueNumber(Instruction* inst);
|
||||||
|
Value* getValueNumber(BinaryInst* inst);
|
||||||
|
Value* getValueNumber(UnaryInst* inst);
|
||||||
|
Value* getValueNumber(GetElementPtrInst* inst);
|
||||||
|
Value* getValueNumber(LoadInst* inst);
|
||||||
|
Value* getValueNumber(CallInst* inst);
|
||||||
|
|
||||||
|
// 访问指令并进行GVN优化
|
||||||
|
void visitInstruction(Instruction* inst);
|
||||||
|
|
||||||
|
// 检查是否可以安全地用一个值替换另一个值
|
||||||
|
bool canReplace(Instruction* original, Value* replacement);
|
||||||
|
|
||||||
|
// 生成表达式的标准化字符串
|
||||||
|
std::string getCanonicalExpression(Instruction* inst);
|
||||||
|
};
|
||||||
|
|
||||||
|
// GVN优化遍类
|
||||||
|
class GVN : public OptimizationPass {
|
||||||
|
public:
|
||||||
|
// 静态成员,作为该遍的唯一ID
|
||||||
|
static void* ID;
|
||||||
|
|
||||||
|
GVN() : OptimizationPass("GVN", Granularity::Function) {}
|
||||||
|
|
||||||
|
// 在函数上运行优化
|
||||||
|
bool runOnFunction(Function* func, AnalysisManager& AM) override;
|
||||||
|
|
||||||
|
// 返回该遍的唯一ID
|
||||||
|
void* getPassID() const override { return ID; }
|
||||||
|
|
||||||
|
// 声明分析依赖
|
||||||
|
void getAnalysisUsage(std::set<void*>& analysisDependencies,
|
||||||
|
std::set<void*>& analysisInvalidations) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace sysy
|
||||||
Reference in New Issue
Block a user