Compare commits
1 Commits
backend-re
...
deploy-202
| Author | SHA1 | Date | |
|---|---|---|---|
| c2153b6fab |
4
.gitignore
vendored
4
.gitignore
vendored
@ -23,6 +23,7 @@
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
@ -52,5 +53,4 @@ __init__.py
|
||||
|
||||
.DS_*
|
||||
|
||||
antlr/
|
||||
.clang-format
|
||||
antlr/
|
||||
@ -1,6 +0,0 @@
|
||||
# 记录中端遍的开发进度
|
||||
|
||||
| 名称 | 优化级别 | 开发进度 |
|
||||
| ------------ | ------------ | ---------- |
|
||||
| CFG优化 | 函数级 | 已完成 |
|
||||
| DCE | 函数级 | 待测试 |
|
||||
36
README.md
36
README.md
@ -37,38 +37,4 @@ mysysy/ $ bash setup.sh
|
||||
```
|
||||
|
||||
### 配套脚本
|
||||
(TODO: 需要完善)
|
||||
|
||||
|
||||
### TODO_list:
|
||||
|
||||
除开注释中的TODO后续时间充足可以考虑的TODO:
|
||||
|
||||
- store load指令由于gep指令的引入, 维度信息的记录是非必须的, 考虑删除
|
||||
|
||||
- use def关系经过mem2reg和phi函数明确转换为ssa形式, 以及函数参数通过value数组明确定义, 使得基本块的args参数信息记录非必须, 考虑删除
|
||||
|
||||
---
|
||||
|
||||
## 编译器后端 TODO 列表
|
||||
|
||||
### 1. `CALL` 指令处理不完善 (高优先级)
|
||||
|
||||
* **问题描述**:当前 `RISCv64RegAlloc::getInstrUseDef()` 方法中,对 `CALL` 指令的 `use`/`def` 分析不完整。它正确识别了返回值为 `def` 和参数为 `use`,但**没有将所有调用者保存 (Caller-saved) 的物理寄存器(`T0-T6`, `A0-A7`)标记为隐式 `def` (即 `CALL` 会破坏它们)**。
|
||||
* **潜在后果**:
|
||||
* **活跃性分析错误**:寄存器分配器可能会错误地认为某个跨函数调用活跃的虚拟寄存器是安全的,并将其分配给 `T` 或 `A` 寄存器。
|
||||
* **值被破坏**:在 `CALL` 指令执行后,这些 `T` 或 `A` 寄存器中本应保留的值会被被调用的函数破坏,导致程序行为异常。
|
||||
* **参考文件**:`RISCv64RegAlloc.cpp` (在 `getInstrUseDef` 函数中对 `RVOpcodes::CALL` 的处理)。
|
||||
|
||||
### 2. `T6` 寄存器作为溢出寄存器的问题 (中等优先级)
|
||||
|
||||
* **问题描述**:`RISCv64RegAlloc::rewriteFunction()` 方法中,所有未能成功着色并被溢出 (spilled) 的虚拟寄存器,都被统一替换为物理寄存器 `T6`。
|
||||
* **问题 2.1:`T6` 是调用者保存寄存器,但未被调用者保存**:
|
||||
* `T6` 属于调用者保存寄存器 (`T0-T6` 范围)。
|
||||
* 标准 ABI 要求,如果一个调用者保存寄存器在函数调用前后都活跃(例如,它存储了一个被溢出的变量,而这个变量在 `CALL` 指令之后还需要用到),那么**调用者**有责任在 `CALL` 前保存该寄存器,并在 `CALL` 后恢复它。
|
||||
* 目前的 `rewriteFunction` 没有为 `T6` 插入这种保存/恢复逻辑。
|
||||
* **潜在后果**:如果一个溢出变量被分配到 `T6`,并且它跨函数调用活跃,那么 `putint` 或其他任何被调用的函数可能会随意使用 `T6`,从而破坏该溢出变量的值。
|
||||
* **问题 2.2:所有溢出变量共用一个 `T6`**:
|
||||
* 将所有溢出变量映射到同一个物理寄存器 `T6` 是一种简化的溢出策略。
|
||||
* **潜在后果**:这意味着,每当需要使用一个溢出变量时,其值必须从栈中加载到 `T6`;每当一个溢出变量被定义时,其值必须从 `T6` 存储回栈。这会引入大量的 `load`/`store` 指令,并导致 `T6` 本身成为一个高度冲突的寄存器,严重降低代码效率。
|
||||
* **参考文件**:`RISCv64RegAlloc.cpp` (在 `rewriteFunction` 函数中处理 `spilled_vregs` 的部分)。
|
||||
(TODO: 需要完善)
|
||||
Binary file not shown.
Binary file not shown.
@ -57,22 +57,11 @@ bool AddressCalculationExpansion::run() {
|
||||
}
|
||||
}
|
||||
} else if (GlobalValue* globalValue = dynamic_cast<GlobalValue*>(basePointer)) {
|
||||
// 遍历 GlobalValue 的所有维度操作数
|
||||
for (const auto& use_ptr : globalValue->getDims()) {
|
||||
Value* dimValue = use_ptr->getValue();
|
||||
// 将维度值转换为常量整数
|
||||
if (ConstantInteger* constVal = dynamic_cast<ConstantInteger*>(dimValue)) {
|
||||
dims.push_back(constVal->getInt());
|
||||
} else {
|
||||
// 如果维度不是常量整数,则无法处理。
|
||||
// 根据 IR.h 中 GlobalValue 的构造函数,这种情况不应发生,但作为安全检查是好的。
|
||||
std::cerr << "Warning: GlobalValue dimension is not a constant integer. Skipping GEP expansion for: ";
|
||||
SysYPrinter::printValue(globalValue);
|
||||
std::cerr << "\n";
|
||||
dims.clear(); // 清空已收集的部分维度信息
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::cerr << "Warning: GlobalValue dimension handling needs explicit implementation for GEP expansion. Skipping GEP for: ";
|
||||
SysYPrinter::printValue(globalValue);
|
||||
std::cerr << "\n";
|
||||
++it;
|
||||
continue;
|
||||
} else {
|
||||
std::cerr << "Warning: Base pointer is not AllocaInst/GlobalValue or its array dimensions cannot be determined for GEP expansion. Skipping GEP for: ";
|
||||
SysYPrinter::printValue(basePointer);
|
||||
|
||||
@ -21,11 +21,9 @@ add_executable(sysyc
|
||||
IR.cpp
|
||||
SysYIRGenerator.cpp
|
||||
SysYIRPrinter.cpp
|
||||
SysYIRCFGOpt.cpp
|
||||
Pass.cpp
|
||||
Dom.cpp
|
||||
Liveness.cpp
|
||||
DCE.cpp
|
||||
SysYIROptPre.cpp
|
||||
SysYIRAnalyser.cpp
|
||||
# DeadCodeElimination.cpp
|
||||
AddressCalculationExpansion.cpp
|
||||
# Mem2Reg.cpp
|
||||
# Reg2Mem.cpp
|
||||
@ -33,8 +31,6 @@ add_executable(sysyc
|
||||
RISCv64ISel.cpp
|
||||
RISCv64RegAlloc.cpp
|
||||
RISCv64AsmPrinter.cpp
|
||||
RISCv64Passes.cpp
|
||||
RISCv64LLIR.cpp
|
||||
)
|
||||
|
||||
# 设置 include 路径,包含 ANTLR 运行时库和项目头文件
|
||||
|
||||
140
src/DCE.cpp
140
src/DCE.cpp
@ -1,140 +0,0 @@
|
||||
#include "DCE.h" // 包含DCE遍的头文件
|
||||
#include "IR.h" // 包含IR相关的定义
|
||||
#include "SysYIROptUtils.h" // 包含SysY IR优化工具类的定义
|
||||
#include <cassert> // 用于断言
|
||||
#include <iostream> // 用于调试输出
|
||||
#include <set> // 包含set,虽然DCEContext内部用unordered_set,但这里保留
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// DCE 遍的静态 ID
|
||||
void *DCE::ID = (void *)&DCE::ID;
|
||||
|
||||
// ======================================================================
|
||||
// DCEContext 类的实现
|
||||
// 封装了 DCE 遍的核心逻辑和状态,确保每次函数优化运行时状态独立
|
||||
// ======================================================================
|
||||
|
||||
// DCEContext 的 run 方法实现
|
||||
void DCEContext::run(Function *func, AnalysisManager *AM, bool &changed) {
|
||||
// 清空活跃指令集合,确保每次运行都是新的状态
|
||||
alive_insts.clear();
|
||||
|
||||
// 第一次遍历:扫描所有指令,识别“天然活跃”的指令并将其及其依赖标记为活跃
|
||||
// 使用 func->getBasicBlocks() 获取基本块列表,保留用户风格
|
||||
auto basicBlocks = func->getBasicBlocks();
|
||||
for (auto &basicBlock : basicBlocks) {
|
||||
// 确保基本块有效
|
||||
if (!basicBlock)
|
||||
continue;
|
||||
// 使用 basicBlock->getInstructions() 获取指令列表,保留用户风格
|
||||
for (auto &inst : basicBlock->getInstructions()) {
|
||||
// 确保指令有效
|
||||
if (!inst)
|
||||
continue;
|
||||
// 调用 DCEContext 自身的 isAlive 和 addAlive 方法
|
||||
if (isAlive(inst.get())) {
|
||||
addAlive(inst.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 第二次遍历:删除所有未被标记为活跃的指令。
|
||||
for (auto &basicBlock : basicBlocks) {
|
||||
if (!basicBlock)
|
||||
continue;
|
||||
// 使用传统的迭代器循环,并手动管理迭代器,
|
||||
// 以便在删除元素后正确前进。保留用户风格
|
||||
for (auto instIter = basicBlock->getInstructions().begin(); instIter != basicBlock->getInstructions().end();) {
|
||||
auto &inst = *instIter;
|
||||
Instruction *currentInst = inst.get();
|
||||
// 如果指令不在活跃集合中,则删除它。
|
||||
// 分支和返回指令由 isAlive 处理,并会被保留。
|
||||
if (alive_insts.count(currentInst) == 0) {
|
||||
// 删除指令,保留用户风格的 SysYIROptUtils::usedelete 和 erase
|
||||
changed = true; // 标记 IR 已被修改
|
||||
SysYIROptUtils::usedelete(currentInst);
|
||||
instIter = basicBlock->getInstructions().erase(instIter); // 删除后返回下一个迭代器
|
||||
} else {
|
||||
++instIter; // 指令活跃,移动到下一个
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 判断指令是否是“天然活跃”的实现
|
||||
// 只有具有副作用的指令(如存储、函数调用、原子操作)
|
||||
// 和控制流指令(如分支、返回)是天然活跃的。
|
||||
bool DCEContext::isAlive(Instruction *inst) {
|
||||
// TODO: 后续程序并发考虑原子操作
|
||||
// 其结果不被其他指令使用的指令(例如 StoreInst, BranchInst, ReturnInst)。
|
||||
// dynamic_cast<ir::CallInst>(inst) 检查是否是函数调用指令,
|
||||
// 函数调用通常有副作用。
|
||||
// 终止指令 (BranchInst, ReturnInst) 必须是活跃的,因为它控制了程序的执行流程。
|
||||
// 保留用户提供的 isAlive 逻辑
|
||||
bool isBranchOrReturn = inst->isBranch() || inst->isReturn();
|
||||
bool isCall = inst->isCall();
|
||||
bool isStoreOrMemset = inst->isStore() || inst->isMemset();
|
||||
return isBranchOrReturn || isCall || isStoreOrMemset;
|
||||
}
|
||||
|
||||
// 递归地将活跃指令及其依赖加入到 alive_insts 集合中
|
||||
void DCEContext::addAlive(Instruction *inst) {
|
||||
// 如果指令已经存在于活跃集合中,则无需重复处理
|
||||
if (alive_insts.count(inst) > 0) {
|
||||
return;
|
||||
}
|
||||
// 将当前指令标记为活跃
|
||||
alive_insts.insert(inst);
|
||||
// 遍历当前指令的所有操作数
|
||||
// 保留用户提供的 getOperands() 和 getValue()
|
||||
for (auto operand : inst->getOperands()) {
|
||||
// 如果操作数是一个指令(即它是一个值的定义),
|
||||
// 并且它还没有被标记为活跃
|
||||
if (auto opInst = dynamic_cast<Instruction *>(operand->getValue())) {
|
||||
addAlive(opInst); // 递归地将操作数指令标记为活跃
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ======================================================================
|
||||
// DCE Pass 类的实现
|
||||
// 主要负责与 PassManager 交互,创建 DCEContext 实例并运行优化
|
||||
// ======================================================================
|
||||
|
||||
// DCE 遍的 runOnFunction 方法实现
|
||||
bool DCE::runOnFunction(Function *func, AnalysisManager &AM) {
|
||||
|
||||
DCEContext ctx;
|
||||
bool changed = false;
|
||||
ctx.run(func, &AM, changed); // 运行 DCE 优化
|
||||
|
||||
// 如果 IR 被修改,则使相关的分析结果失效
|
||||
if (changed) {
|
||||
// DCE 会删除指令,这会影响数据流分析,尤其是活跃性分析。
|
||||
// 如果删除导致基本块变空,也可能间接影响 CFG 和支配树。
|
||||
// AM.invalidateAnalysis(&LivenessAnalysisPass::ID, func); // 活跃性分析失效
|
||||
// AM.invalidateAnalysis(&DominatorTreeAnalysisPass::ID, func); // 支配树分析可能失效
|
||||
// 其他所有依赖于数据流或 IR 结构的分析都可能失效。
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
// 声明DCE遍的分析依赖和失效信息
|
||||
void DCE::getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const {
|
||||
// DCE不依赖特定的分析结果,它通过遍历和副作用判断来工作。
|
||||
|
||||
// DCE会删除指令,这会影响许多分析结果。
|
||||
// 至少,它会影响活跃性分析、支配树、控制流图(如果删除导致基本块为空并被合并)。
|
||||
// 假设存在LivenessAnalysisPass和DominatorTreeAnalysisPass
|
||||
// analysisInvalidations.insert(&LivenessAnalysisPass::ID);
|
||||
// analysisInvalidations.insert(&DominatorTreeAnalysisPass::ID);
|
||||
// 任何改变IR结构的优化,都可能导致通用分析(如活跃性、支配树、循环信息)失效。
|
||||
// 最保守的做法是使所有函数粒度的分析失效,或者只声明你明确知道会受影响的分析。
|
||||
// 考虑到这个DCE仅删除指令,如果它不删除基本块,CFG可能不变,但数据流分析会失效。
|
||||
// 对于更激进的DCE(如ADCE),CFG也会改变。
|
||||
// 这里我们假设它主要影响数据流分析,并且可能间接影响CFG相关分析。
|
||||
// 如果有SideEffectInfo,它也可能被修改,但通常SideEffectInfo是静态的,不因DCE而变。
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
180
src/Dom.cpp
180
src/Dom.cpp
@ -1,180 +0,0 @@
|
||||
#include "Dom.h"
|
||||
#include <limits> // for std::numeric_limits
|
||||
#include <queue>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 初始化 支配树静态 ID
|
||||
void *DominatorTreeAnalysisPass::ID = (void *)&DominatorTreeAnalysisPass::ID;
|
||||
// ==============================================================
|
||||
// DominatorTree 结果类的实现
|
||||
// ==============================================================
|
||||
|
||||
DominatorTree::DominatorTree(Function *F) : AssociatedFunction(F) {
|
||||
// 构造时可以不计算,在分析遍运行里计算并填充
|
||||
}
|
||||
|
||||
const std::set<BasicBlock *> *DominatorTree::getDominators(BasicBlock *BB) const {
|
||||
auto it = Dominators.find(BB);
|
||||
if (it != Dominators.end()) {
|
||||
return &(it->second);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
BasicBlock *DominatorTree::getImmediateDominator(BasicBlock *BB) const {
|
||||
auto it = IDoms.find(BB);
|
||||
if (it != IDoms.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::set<BasicBlock *> *DominatorTree::getDominanceFrontier(BasicBlock *BB) const {
|
||||
auto it = DominanceFrontiers.find(BB);
|
||||
if (it != DominanceFrontiers.end()) {
|
||||
return &(it->second);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void DominatorTree::computeDominators(Function *F) {
|
||||
// 经典的迭代算法计算支配者集合
|
||||
// TODO: 可以替换为更高效的算法,如 Lengauer-Tarjan 算法
|
||||
BasicBlock *entryBlock = F->getEntryBlock();
|
||||
|
||||
for (const auto &bb_ptr : F->getBasicBlocks()) {
|
||||
BasicBlock *bb = bb_ptr.get();
|
||||
if (bb == entryBlock) {
|
||||
Dominators[bb].insert(bb);
|
||||
} else {
|
||||
for (const auto &all_bb_ptr : F->getBasicBlocks()) {
|
||||
Dominators[bb].insert(all_bb_ptr.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool changed = true;
|
||||
while (changed) {
|
||||
changed = false;
|
||||
for (const auto &bb_ptr : F->getBasicBlocks()) {
|
||||
BasicBlock *bb = bb_ptr.get();
|
||||
if (bb == entryBlock)
|
||||
continue;
|
||||
|
||||
std::set<BasicBlock *> newDom;
|
||||
bool firstPred = true;
|
||||
for (BasicBlock *pred : bb->getPredecessors()) {
|
||||
if (Dominators.count(pred)) {
|
||||
if (firstPred) {
|
||||
newDom = Dominators[pred];
|
||||
firstPred = false;
|
||||
} else {
|
||||
std::set<BasicBlock *> intersection;
|
||||
std::set_intersection(newDom.begin(), newDom.end(), Dominators[pred].begin(), Dominators[pred].end(),
|
||||
std::inserter(intersection, intersection.begin()));
|
||||
newDom = intersection;
|
||||
}
|
||||
}
|
||||
}
|
||||
newDom.insert(bb);
|
||||
|
||||
if (newDom != Dominators[bb]) {
|
||||
Dominators[bb] = newDom;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DominatorTree::computeIDoms(Function *F) {
|
||||
// 采用与之前类似的简化实现。TODO:Lengauer-Tarjan等算法。
|
||||
BasicBlock *entryBlock = F->getEntryBlock();
|
||||
IDoms[entryBlock] = nullptr;
|
||||
|
||||
for (const auto &bb_ptr : F->getBasicBlocks()) {
|
||||
BasicBlock *bb = bb_ptr.get();
|
||||
if (bb == entryBlock)
|
||||
continue;
|
||||
|
||||
BasicBlock *currentIDom = nullptr;
|
||||
const std::set<BasicBlock *> *domsOfBB = getDominators(bb);
|
||||
if (!domsOfBB)
|
||||
continue;
|
||||
|
||||
for (BasicBlock *D : *domsOfBB) {
|
||||
if (D == bb)
|
||||
continue;
|
||||
|
||||
bool isCandidateIDom = true;
|
||||
for (BasicBlock *candidate : *domsOfBB) {
|
||||
if (candidate == bb || candidate == D)
|
||||
continue;
|
||||
const std::set<BasicBlock *> *domsOfCandidate = getDominators(candidate);
|
||||
if (domsOfCandidate && domsOfCandidate->count(D) == 0 && domsOfBB->count(candidate)) {
|
||||
isCandidateIDom = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isCandidateIDom) {
|
||||
currentIDom = D;
|
||||
break;
|
||||
}
|
||||
}
|
||||
IDoms[bb] = currentIDom;
|
||||
}
|
||||
}
|
||||
|
||||
void DominatorTree::computeDominanceFrontiers(Function *F) {
|
||||
// 经典的支配边界计算算法
|
||||
for (const auto &bb_ptr_X : F->getBasicBlocks()) {
|
||||
BasicBlock *X = bb_ptr_X.get();
|
||||
DominanceFrontiers[X].clear();
|
||||
|
||||
for (BasicBlock *Y : X->getSuccessors()) {
|
||||
const std::set<BasicBlock *> *domsOfY = getDominators(Y);
|
||||
if (domsOfY && domsOfY->find(X) == domsOfY->end()) {
|
||||
DominanceFrontiers[X].insert(Y);
|
||||
}
|
||||
}
|
||||
|
||||
const std::set<BasicBlock *> *domsOfX = getDominators(X);
|
||||
if (!domsOfX)
|
||||
continue;
|
||||
for (const auto &bb_ptr_Z : F->getBasicBlocks()) {
|
||||
BasicBlock *Z = bb_ptr_Z.get();
|
||||
if (Z == X)
|
||||
continue;
|
||||
const std::set<BasicBlock *> *domsOfZ = getDominators(Z);
|
||||
if (domsOfZ && domsOfZ->count(X) && Z != X) {
|
||||
|
||||
for (BasicBlock *Y : Z->getSuccessors()) {
|
||||
const std::set<BasicBlock *> *domsOfY = getDominators(Y);
|
||||
if (domsOfY && domsOfY->find(X) == domsOfY->end()) {
|
||||
DominanceFrontiers[X].insert(Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==============================================================
|
||||
// DominatorTreeAnalysisPass 的实现
|
||||
// ==============================================================
|
||||
|
||||
|
||||
bool DominatorTreeAnalysisPass::runOnFunction(Function* F, AnalysisManager &AM) {
|
||||
CurrentDominatorTree = std::make_unique<DominatorTree>(F);
|
||||
CurrentDominatorTree->computeDominators(F);
|
||||
CurrentDominatorTree->computeIDoms(F);
|
||||
CurrentDominatorTree->computeDominanceFrontiers(F);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<AnalysisResultBase> DominatorTreeAnalysisPass::getResult() {
|
||||
// 返回计算好的 DominatorTree 实例,所有权转移给 AnalysisManager
|
||||
return std::move(CurrentDominatorTree);
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
78
src/IR.cpp
78
src/IR.cpp
@ -49,11 +49,6 @@ auto Type::getFunctionType(Type *returnType, const std::vector<Type *> ¶mTyp
|
||||
return FunctionType::get(returnType, paramTypes);
|
||||
}
|
||||
|
||||
auto Type::getArrayType(Type *elementType, unsigned numElements) -> Type * {
|
||||
// forward to ArrayType
|
||||
return ArrayType::get(elementType, numElements);
|
||||
}
|
||||
|
||||
auto Type::getSize() const -> unsigned {
|
||||
switch (kind) {
|
||||
case kInt:
|
||||
@ -63,10 +58,6 @@ auto Type::getSize() const -> unsigned {
|
||||
case kPointer:
|
||||
case kFunction:
|
||||
return 8;
|
||||
case Kind::kArray: {
|
||||
const ArrayType* arrType = static_cast<const ArrayType*>(this);
|
||||
return arrType->getElementType()->getSize() * arrType->getNumElements();
|
||||
}
|
||||
case kVoid:
|
||||
return 0;
|
||||
}
|
||||
@ -104,20 +95,6 @@ FunctionType*FunctionType::get(Type *returnType, const std::vector<Type *> ¶
|
||||
return result.first->get();
|
||||
}
|
||||
|
||||
ArrayType *ArrayType::get(Type *elementType, unsigned numElements) {
|
||||
static std::set<std::unique_ptr<ArrayType>> arrayTypes;
|
||||
auto iter = std::find_if(arrayTypes.begin(), arrayTypes.end(), [&](const std::unique_ptr<ArrayType> &type) -> bool {
|
||||
return elementType == type->getElementType() && numElements == type->getNumElements();
|
||||
});
|
||||
if (iter != arrayTypes.end()) {
|
||||
return iter->get();
|
||||
}
|
||||
auto type = new ArrayType(elementType, numElements);
|
||||
assert(type);
|
||||
auto result = arrayTypes.emplace(type);
|
||||
return result.first->get();
|
||||
}
|
||||
|
||||
void Value::replaceAllUsesWith(Value *value) {
|
||||
for (auto &use : uses) {
|
||||
use->getUser()->setOperand(use->getIndex(), value);
|
||||
@ -488,7 +465,44 @@ Function * Function::clone(const std::string &suffix) const {
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO:复制GEP指令
|
||||
case Instruction::kLa: {
|
||||
auto oldLaInst = dynamic_cast<LaInst *>(inst);
|
||||
auto oldPointer = oldLaInst->getPointer();
|
||||
Value *newPointer;
|
||||
std::vector<Value *> newIndices;
|
||||
newPointer = oldNewValueMap.at(oldPointer);
|
||||
|
||||
for (const auto &index : oldLaInst->getIndices()) {
|
||||
newIndices.emplace_back(oldNewValueMap.at(index->getValue()));
|
||||
}
|
||||
ss << oldLaInst->getName() << suffix;
|
||||
auto newLaInst = new LaInst(newPointer, newIndices, oldNewBlockMap.at(oldLaInst->getParent()), ss.str());
|
||||
ss.str("");
|
||||
oldNewValueMap.emplace(oldLaInst, newLaInst);
|
||||
break;
|
||||
}
|
||||
|
||||
case Instruction::kGetSubArray: {
|
||||
auto oldGetSubArrayInst = dynamic_cast<GetSubArrayInst *>(inst);
|
||||
auto oldFather = oldGetSubArrayInst->getFatherArray();
|
||||
auto oldChild = oldGetSubArrayInst->getChildArray();
|
||||
Value *newFather;
|
||||
Value *newChild;
|
||||
std::vector<Value *> newIndices;
|
||||
newFather = oldNewValueMap.at(oldFather);
|
||||
newChild = oldNewValueMap.at(oldChild);
|
||||
|
||||
for (const auto &index : oldGetSubArrayInst->getIndices()) {
|
||||
newIndices.emplace_back(oldNewValueMap.at(index->getValue()));
|
||||
}
|
||||
ss << oldGetSubArrayInst->getName() << suffix;
|
||||
auto newGetSubArrayInst =
|
||||
new GetSubArrayInst(dynamic_cast<LVal *>(newFather), dynamic_cast<LVal *>(newChild), newIndices,
|
||||
oldNewBlockMap.at(oldGetSubArrayInst->getParent()), ss.str());
|
||||
ss.str("");
|
||||
oldNewValueMap.emplace(oldGetSubArrayInst, newGetSubArrayInst);
|
||||
break;
|
||||
}
|
||||
|
||||
case Instruction::kMemset: {
|
||||
auto oldMemsetInst = dynamic_cast<MemsetInst *>(inst);
|
||||
@ -530,14 +544,8 @@ Function * Function::clone(const std::string &suffix) const {
|
||||
}
|
||||
}
|
||||
|
||||
// for (const auto ¶m : blocks.front()->getArguments()) {
|
||||
// newFunction->getEntryBlock()->insertArgument(dynamic_cast<AllocaInst *>(oldNewValueMap.at(param)));
|
||||
// }
|
||||
for (const auto &arg : arguments) {
|
||||
auto newArg = dynamic_cast<Argument *>(oldNewValueMap.at(arg));
|
||||
if (newArg != nullptr) {
|
||||
newFunction->insertArgument(newArg);
|
||||
}
|
||||
for (const auto ¶m : blocks.front()->getArguments()) {
|
||||
newFunction->getEntryBlock()->insertArgument(dynamic_cast<AllocaInst *>(oldNewValueMap.at(param)));
|
||||
}
|
||||
|
||||
return newFunction;
|
||||
@ -653,7 +661,7 @@ Function * CallInst::getCallee() const { return dynamic_cast<Function *>(getOper
|
||||
/**
|
||||
* 获取变量指针
|
||||
*/
|
||||
auto SymbolTable::getVariable(const std::string &name) const -> Value * {
|
||||
auto SymbolTable::getVariable(const std::string &name) const -> User * {
|
||||
auto node = curNode;
|
||||
while (node != nullptr) {
|
||||
auto iter = node->varList.find(name);
|
||||
@ -668,8 +676,8 @@ auto SymbolTable::getVariable(const std::string &name) const -> Value * {
|
||||
/**
|
||||
* 添加变量到符号表
|
||||
*/
|
||||
auto SymbolTable::addVariable(const std::string &name, Value *variable) -> Value * {
|
||||
Value *result = nullptr;
|
||||
auto SymbolTable::addVariable(const std::string &name, User *variable) -> User * {
|
||||
User *result = nullptr;
|
||||
if (curNode != nullptr) {
|
||||
std::stringstream ss;
|
||||
auto iter = variableIndex.find(name);
|
||||
|
||||
160
src/Liveness.cpp
160
src/Liveness.cpp
@ -1,160 +0,0 @@
|
||||
#include "Liveness.h"
|
||||
#include <algorithm> // For std::set_union, std::set_difference
|
||||
#include <iostream>
|
||||
#include <queue> // Potentially for worklist, though not strictly needed for the iterative approach below
|
||||
#include <set> // For std::set
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 初始化静态 ID
|
||||
void *LivenessAnalysisPass::ID = (void *)&LivenessAnalysisPass::ID;
|
||||
// ==============================================================
|
||||
// LivenessAnalysisResult 结果类的实现
|
||||
// ==============================================================
|
||||
|
||||
const std::set<Value *> *LivenessAnalysisResult::getLiveIn(BasicBlock *BB) const {
|
||||
auto it = liveInSets.find(BB);
|
||||
if (it != liveInSets.end()) {
|
||||
return &(it->second);
|
||||
}
|
||||
// 返回一个空集合,表示未找到或不存在
|
||||
static const std::set<Value *> emptySet;
|
||||
return &emptySet;
|
||||
}
|
||||
|
||||
const std::set<Value *> *LivenessAnalysisResult::getLiveOut(BasicBlock *BB) const {
|
||||
auto it = liveOutSets.find(BB);
|
||||
if (it != liveOutSets.end()) {
|
||||
return &(it->second);
|
||||
}
|
||||
static const std::set<Value *> emptySet;
|
||||
return &emptySet;
|
||||
}
|
||||
|
||||
void LivenessAnalysisResult::computeDefUse(BasicBlock *BB, std::set<Value *> &def, std::set<Value *> &use) {
|
||||
def.clear(); // 将持有在 BB 中定义的值
|
||||
use.clear(); // 将持有在 BB 中使用但在其定义之前的值
|
||||
|
||||
// 临时集合,用于跟踪当前基本块中已经定义过的变量
|
||||
std::set<Value *> defined_in_block_so_far;
|
||||
|
||||
// 按照指令在块中的顺序遍历
|
||||
for (const auto &inst_ptr : BB->getInstructions()) {
|
||||
Instruction *inst = inst_ptr.get();
|
||||
|
||||
// 1. 处理指令的操作数 (Use) - 在定义之前的使用
|
||||
for (const auto &use_ptr : inst->getOperands()) { // 修正迭代器类型
|
||||
Value *operand = use_ptr->getValue(); // 从 shared_ptr<Use> 获取 Value*
|
||||
|
||||
// 过滤掉常量和全局变量,因为它们通常不被视为活跃变量
|
||||
ConstantValue *constValue = dynamic_cast<ConstantValue *>(operand);
|
||||
GlobalValue *globalValue = dynamic_cast<GlobalValue *>(operand);
|
||||
if (constValue || globalValue) {
|
||||
continue; // 跳过常量和全局变量
|
||||
}
|
||||
|
||||
// 如果操作数是一个变量(Instruction 或 Argument),并且它在此基本块的当前点之前尚未被定义
|
||||
if (defined_in_block_so_far.find(operand) == defined_in_block_so_far.end()) {
|
||||
use.insert(operand);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 处理指令自身产生的定义 (Def)
|
||||
if (inst->isDefine()) { // 使用 isDefine() 方法
|
||||
// 指令自身定义了一个值。将其添加到块的 def 集合,
|
||||
// 并添加到当前块中已定义的值的临时集合。
|
||||
def.insert(inst); // inst 本身就是被定义的值(例如,虚拟寄存器)
|
||||
defined_in_block_so_far.insert(inst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LivenessAnalysisResult::computeLiveness(Function *F) {
|
||||
// 每次计算前清空旧结果
|
||||
liveInSets.clear(); // 直接清空 map,不再使用 F 作为键
|
||||
liveOutSets.clear(); // 直接清空 map
|
||||
|
||||
// 初始化所有基本块的 LiveIn 和 LiveOut 集合为空
|
||||
for (const auto &bb_ptr : F->getBasicBlocks()) {
|
||||
BasicBlock *bb = bb_ptr.get();
|
||||
liveInSets[bb] = {}; // 直接以 bb 为键
|
||||
liveOutSets[bb] = {}; // 直接以 bb 为键
|
||||
}
|
||||
|
||||
bool changed = true;
|
||||
while (changed) {
|
||||
changed = false;
|
||||
|
||||
// TODO : 目前为逆序遍历基本块,考虑反向拓扑序遍历基本块
|
||||
|
||||
// 逆序遍历基本块
|
||||
// std::list<std::unique_ptr<BasicBlock>> basicBlocks(F->getBasicBlocks().begin(), F->getBasicBlocks().end());
|
||||
// std::reverse(basicBlocks.begin(), basicBlocks.end());
|
||||
// 然后遍历 basicBlocks
|
||||
// 创建一个 BasicBlock* 的列表来存储指针,避免拷贝 unique_ptr
|
||||
// Option 1: Using std::vector<BasicBlock*> (preferred for performance with reverse)
|
||||
std::vector<BasicBlock*> basicBlocksPointers;
|
||||
for (const auto& bb_ptr : F->getBasicBlocks()) {
|
||||
basicBlocksPointers.push_back(bb_ptr.get());
|
||||
}
|
||||
std::reverse(basicBlocksPointers.begin(), basicBlocksPointers.end());
|
||||
|
||||
for (auto bb_iter = basicBlocksPointers.begin(); bb_iter != basicBlocksPointers.end(); ++bb_iter) {
|
||||
BasicBlock *bb = *bb_iter; // 获取 BasicBlock 指针
|
||||
if (!bb)
|
||||
continue; // 避免空指针
|
||||
|
||||
std::set<Value *> oldLiveIn = liveInSets[bb];
|
||||
std::set<Value *> oldLiveOut = liveOutSets[bb];
|
||||
|
||||
// 1. 计算 LiveOut(BB) = Union(LiveIn(Succ) for Succ in Successors(BB))
|
||||
std::set<Value *> newLiveOut;
|
||||
for (BasicBlock *succ : bb->getSuccessors()) {
|
||||
const std::set<Value *> *succLiveIn = getLiveIn(succ); // 获取后继的 LiveIn
|
||||
if (succLiveIn) {
|
||||
newLiveOut.insert(succLiveIn->begin(), succLiveIn->end());
|
||||
}
|
||||
}
|
||||
liveOutSets[bb] = newLiveOut;
|
||||
|
||||
// 2. 计算 LiveIn(BB) = Use(BB) Union (LiveOut(BB) - Def(BB))
|
||||
std::set<Value *> defSet, useSet;
|
||||
computeDefUse(bb, defSet, useSet); // 计算当前块的 Def 和 Use
|
||||
|
||||
std::set<Value *> liveOutMinusDef;
|
||||
std::set_difference(newLiveOut.begin(), newLiveOut.end(), defSet.begin(), defSet.end(),
|
||||
std::inserter(liveOutMinusDef, liveOutMinusDef.begin()));
|
||||
|
||||
std::set<Value *> newLiveIn = useSet;
|
||||
newLiveIn.insert(liveOutMinusDef.begin(), liveOutMinusDef.end());
|
||||
liveInSets[bb] = newLiveIn;
|
||||
|
||||
// 检查是否发生变化
|
||||
if (oldLiveIn != newLiveIn || oldLiveOut != newLiveOut) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==============================================================
|
||||
// LivenessAnalysisPass 的实现
|
||||
// ==============================================================
|
||||
|
||||
bool LivenessAnalysisPass::runOnFunction(Function *F, AnalysisManager &AM) {
|
||||
// 每次运行创建一个新的 LivenessAnalysisResult 对象来存储结果
|
||||
CurrentLivenessResult = std::make_unique<LivenessAnalysisResult>(F);
|
||||
|
||||
// 调用 LivenessAnalysisResult 内部的方法来计算分析结果
|
||||
CurrentLivenessResult->computeLiveness(F);
|
||||
|
||||
// 分析遍通常不修改 IR,所以返回 false
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<AnalysisResultBase> LivenessAnalysisPass::getResult() {
|
||||
// 返回计算好的 LivenessAnalysisResult 实例,所有权转移给 AnalysisManager
|
||||
return std::move(CurrentLivenessResult);
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
175
src/Pass.cpp
175
src/Pass.cpp
@ -1,175 +0,0 @@
|
||||
#include "Dom.h"
|
||||
#include "Liveness.h"
|
||||
#include "SysYIRCFGOpt.h"
|
||||
#include "SysYIRPrinter.h"
|
||||
#include "DCE.h"
|
||||
#include "Pass.h"
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
extern int DEBUG; // 全局调试标志
|
||||
namespace sysy {
|
||||
|
||||
// ======================================================================
|
||||
// 封装优化流程的函数:包含Pass注册和迭代运行逻辑
|
||||
// ======================================================================
|
||||
|
||||
void PassManager::runOptimizationPipeline(Module* moduleIR, IRBuilder* builderIR, int optLevel) {
|
||||
if (DEBUG) std::cout << "--- Starting Middle-End Optimizations (Level -O" << optLevel << ") ---\n";
|
||||
|
||||
/*
|
||||
中端开发框架基本流程:
|
||||
1) 分析pass
|
||||
1. 实现分析pass并引入Pass.cpp
|
||||
2. 注册分析pass
|
||||
2) 优化pass
|
||||
1. 实现优化pass并引入Pass.cpp
|
||||
2. 注册优化pass
|
||||
3. 添加优化passid
|
||||
*/
|
||||
// 注册分析遍
|
||||
registerAnalysisPass<sysy::DominatorTreeAnalysisPass>();
|
||||
registerAnalysisPass<sysy::LivenessAnalysisPass>();
|
||||
|
||||
// 注册优化遍
|
||||
registerOptimizationPass<SysYDelInstAfterBrPass>();
|
||||
registerOptimizationPass<SysYDelNoPreBLockPass>();
|
||||
registerOptimizationPass<SysYBlockMergePass>();
|
||||
|
||||
registerOptimizationPass<SysYDelEmptyBlockPass>(builderIR);
|
||||
registerOptimizationPass<SysYCondBr2BrPass>(builderIR);
|
||||
registerOptimizationPass<SysYAddReturnPass>(builderIR);
|
||||
|
||||
if (optLevel >= 1) {
|
||||
//经过设计安排优化遍的执行顺序以及执行逻辑
|
||||
if (DEBUG) std::cout << "Applying -O1 optimizations.\n";
|
||||
if (DEBUG) std::cout << "--- Running custom optimization sequence ---\n";
|
||||
|
||||
this->clearPasses();
|
||||
this->addPass(&SysYDelInstAfterBrPass::ID);
|
||||
this->addPass(&SysYDelNoPreBLockPass::ID);
|
||||
this->addPass(&SysYBlockMergePass::ID);
|
||||
this->addPass(&SysYDelEmptyBlockPass::ID);
|
||||
this->addPass(&SysYCondBr2BrPass::ID);
|
||||
this->addPass(&SysYAddReturnPass::ID);
|
||||
this->run();
|
||||
|
||||
this->clearPasses();
|
||||
this->addPass(&DCE::ID);
|
||||
this->run();
|
||||
|
||||
if (DEBUG) std::cout << "--- Custom optimization sequence finished ---\n";
|
||||
}
|
||||
|
||||
// 2. 创建遍管理器
|
||||
// 3. 根据优化级别添加不同的优化遍
|
||||
// TODO : 根据 optLevel 添加不同的优化遍
|
||||
// 讨论 是不动点迭代进行优化遍还是手动客制化优化遍的顺序?
|
||||
|
||||
|
||||
if (DEBUG) {
|
||||
std::cout << "=== Final IR After Middle-End Optimizations (Level -O" << optLevel << ") ===\n";
|
||||
SysYPrinter printer(moduleIR);
|
||||
printer.printIR();
|
||||
}
|
||||
}
|
||||
|
||||
void PassManager::clearPasses() {
|
||||
passes.clear();
|
||||
}
|
||||
|
||||
void PassManager::addPass(void *passID) {
|
||||
|
||||
PassRegistry ®istry = PassRegistry::getPassRegistry();
|
||||
std::unique_ptr<Pass> P = registry.createPass(passID);
|
||||
if (!P) {
|
||||
// Error: Pass not found or failed to create
|
||||
return;
|
||||
}
|
||||
|
||||
passes.push_back(std::move(P));
|
||||
}
|
||||
|
||||
// 运行所有注册的遍
|
||||
bool PassManager::run() {
|
||||
bool changed = false;
|
||||
for (const auto &p : passes) {
|
||||
bool passChanged = false; // 记录当前遍是否修改了 IR
|
||||
|
||||
// 处理优化遍的分析依赖和失效
|
||||
if (p->getPassKind() == Pass::PassKind::Optimization) {
|
||||
OptimizationPass *optPass = static_cast<OptimizationPass *>(p.get());
|
||||
std::set<void *> analysisDependencies;
|
||||
std::set<void *> analysisInvalidations;
|
||||
optPass->getAnalysisUsage(analysisDependencies, analysisInvalidations);
|
||||
|
||||
// PassManager 不显式运行分析依赖。
|
||||
// 而是优化遍在 runOnFunction 内部通过 AnalysisManager.getAnalysisResult 按需请求。
|
||||
}
|
||||
|
||||
if (p->getGranularity() == Pass::Granularity::Module) {
|
||||
passChanged = p->runOnModule(pmodule, analysisManager);
|
||||
} else if (p->getGranularity() == Pass::Granularity::Function) {
|
||||
for (auto &funcPair : pmodule->getFunctions()) {
|
||||
Function *F = funcPair.second.get();
|
||||
passChanged = p->runOnFunction(F, analysisManager) || passChanged;
|
||||
|
||||
if (passChanged && p->getPassKind() == Pass::PassKind::Optimization) {
|
||||
OptimizationPass *optPass = static_cast<OptimizationPass *>(p.get());
|
||||
std::set<void *> analysisDependencies;
|
||||
std::set<void *> analysisInvalidations;
|
||||
optPass->getAnalysisUsage(analysisDependencies, analysisInvalidations);
|
||||
for (void *invalidationID : analysisInvalidations) {
|
||||
analysisManager.invalidateAnalysis(invalidationID, F);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (p->getGranularity() == Pass::Granularity::BasicBlock) {
|
||||
for (auto &funcPair : pmodule->getFunctions()) {
|
||||
Function *F = funcPair.second.get();
|
||||
for (auto &bbPtr : funcPair.second->getBasicBlocks()) {
|
||||
passChanged = p->runOnBasicBlock(bbPtr.get(), analysisManager) || passChanged;
|
||||
|
||||
if (passChanged && p->getPassKind() == Pass::PassKind::Optimization) {
|
||||
OptimizationPass *optPass = static_cast<OptimizationPass *>(p.get());
|
||||
std::set<void *> analysisDependencies;
|
||||
std::set<void *> analysisInvalidations;
|
||||
optPass->getAnalysisUsage(analysisDependencies, analysisInvalidations);
|
||||
for (void *invalidationID : analysisInvalidations) {
|
||||
analysisManager.invalidateAnalysis(invalidationID, F);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
changed = changed || passChanged;
|
||||
}
|
||||
return changed;
|
||||
|
||||
}
|
||||
|
||||
|
||||
template <typename AnalysisPassType> void registerAnalysisPass() {
|
||||
PassRegistry::getPassRegistry().registerPass(&AnalysisPassType::ID,
|
||||
[]() { return std::make_unique<AnalysisPassType>(); });
|
||||
}
|
||||
|
||||
template <typename OptimizationPassType, typename std::enable_if<
|
||||
std::is_constructible<OptimizationPassType, IRBuilder*>::value, int>::type>
|
||||
void registerOptimizationPass(IRBuilder* builder) {
|
||||
PassRegistry::getPassRegistry().registerPass(&OptimizationPassType::ID,
|
||||
[builder]() { return std::make_unique<OptimizationPassType>(builder); });
|
||||
}
|
||||
|
||||
template <typename OptimizationPassType, typename std::enable_if<
|
||||
!std::is_constructible<OptimizationPassType, IRBuilder*>::value, int>::type>
|
||||
void registerOptimizationPass() {
|
||||
PassRegistry::getPassRegistry().registerPass(&OptimizationPassType::ID,
|
||||
[]() { return std::make_unique<OptimizationPassType>(); });
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
@ -18,7 +18,7 @@ bool isMemoryOp(RVOpcodes opcode) {
|
||||
|
||||
RISCv64AsmPrinter::RISCv64AsmPrinter(MachineFunction* mfunc) : MFunc(mfunc) {}
|
||||
|
||||
void RISCv64AsmPrinter::run(std::ostream& os, bool debug) {
|
||||
void RISCv64AsmPrinter::run(std::ostream& os) {
|
||||
OS = &os;
|
||||
|
||||
*OS << ".globl " << MFunc->getName() << "\n";
|
||||
@ -27,12 +27,10 @@ void RISCv64AsmPrinter::run(std::ostream& os, bool debug) {
|
||||
printPrologue();
|
||||
|
||||
for (auto& mbb : MFunc->getBlocks()) {
|
||||
printBasicBlock(mbb.get(), debug);
|
||||
printBasicBlock(mbb.get());
|
||||
}
|
||||
}
|
||||
|
||||
// 在 RISCv64AsmPrinter.cpp 文件中
|
||||
|
||||
void RISCv64AsmPrinter::printPrologue() {
|
||||
StackFrameInfo& frame_info = MFunc->getFrameInfo();
|
||||
// 序言需要为保存ra和s0预留16字节
|
||||
@ -44,7 +42,25 @@ void RISCv64AsmPrinter::printPrologue() {
|
||||
*OS << " addi sp, sp, -" << aligned_stack_size << "\n";
|
||||
*OS << " sd ra, " << (aligned_stack_size - 8) << "(sp)\n";
|
||||
*OS << " sd s0, " << (aligned_stack_size - 16) << "(sp)\n";
|
||||
*OS << " addi s0, sp, " << aligned_stack_size << "\n";
|
||||
*OS << " mv s0, sp\n";
|
||||
}
|
||||
|
||||
// 忠实还原保存函数入口参数的逻辑
|
||||
Function* F = MFunc->getFunc();
|
||||
if (F && F->getEntryBlock()) {
|
||||
int arg_idx = 0;
|
||||
RISCv64ISel* isel = MFunc->getISel();
|
||||
for (AllocaInst* alloca_for_param : F->getEntryBlock()->getArguments()) {
|
||||
if (arg_idx >= 8) break;
|
||||
|
||||
unsigned vreg = isel->getVReg(alloca_for_param);
|
||||
if (frame_info.alloca_offsets.count(vreg)) {
|
||||
int offset = frame_info.alloca_offsets.at(vreg);
|
||||
auto arg_reg = static_cast<PhysicalReg>(static_cast<int>(PhysicalReg::A0) + arg_idx);
|
||||
*OS << " sw " << regToString(arg_reg) << ", " << offset << "(s0)\n";
|
||||
}
|
||||
arg_idx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,31 +73,24 @@ void RISCv64AsmPrinter::printEpilogue() {
|
||||
}
|
||||
}
|
||||
|
||||
void RISCv64AsmPrinter::printBasicBlock(MachineBasicBlock* mbb, bool debug) {
|
||||
void RISCv64AsmPrinter::printBasicBlock(MachineBasicBlock* mbb) {
|
||||
if (!mbb->getName().empty()) {
|
||||
*OS << mbb->getName() << ":\n";
|
||||
}
|
||||
for (auto& instr : mbb->getInstructions()) {
|
||||
printInstruction(instr.get(), debug);
|
||||
printInstruction(instr.get());
|
||||
}
|
||||
}
|
||||
|
||||
void RISCv64AsmPrinter::printInstruction(MachineInstr* instr, bool debug) {
|
||||
void RISCv64AsmPrinter::printInstruction(MachineInstr* instr) {
|
||||
auto opcode = instr->getOpcode();
|
||||
if (opcode == RVOpcodes::RET) {
|
||||
printEpilogue();
|
||||
}
|
||||
|
||||
if (opcode == RVOpcodes::LABEL) {
|
||||
// 标签直接打印,不加缩进
|
||||
printOperand(instr->getOperands()[0].get());
|
||||
*OS << ":\n";
|
||||
return; // 处理完毕,直接返回
|
||||
if (opcode != RVOpcodes::LABEL) {
|
||||
*OS << " ";
|
||||
}
|
||||
|
||||
// 对于所有非标签指令,先打印缩进
|
||||
*OS << " ";
|
||||
|
||||
switch (opcode) {
|
||||
case RVOpcodes::ADD: *OS << "add "; break; case RVOpcodes::ADDI: *OS << "addi "; break;
|
||||
case RVOpcodes::ADDW: *OS << "addw "; break; case RVOpcodes::ADDIW: *OS << "addiw "; break;
|
||||
@ -117,27 +126,13 @@ void RISCv64AsmPrinter::printInstruction(MachineInstr* instr, bool debug) {
|
||||
case RVOpcodes::SNEZ: *OS << "snez "; break;
|
||||
case RVOpcodes::CALL: *OS << "call "; break;
|
||||
case RVOpcodes::LABEL:
|
||||
printOperand(instr->getOperands()[0].get());
|
||||
*OS << ":";
|
||||
break;
|
||||
case RVOpcodes::FRAME_LOAD_W:
|
||||
// It should have been eliminated by RegAlloc
|
||||
if (!debug) throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");
|
||||
*OS << "frame_load_w "; break;
|
||||
case RVOpcodes::FRAME_LOAD_D:
|
||||
// It should have been eliminated by RegAlloc
|
||||
if (!debug) throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");
|
||||
*OS << "frame_load_d "; break;
|
||||
case RVOpcodes::FRAME_STORE_W:
|
||||
// It should have been eliminated by RegAlloc
|
||||
if (!debug) throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");
|
||||
*OS << "frame_store_w "; break;
|
||||
case RVOpcodes::FRAME_STORE_D:
|
||||
// It should have been eliminated by RegAlloc
|
||||
if (!debug) throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");
|
||||
*OS << "frame_store_d "; break;
|
||||
case RVOpcodes::FRAME_ADDR:
|
||||
// It should have been eliminated by RegAlloc
|
||||
if (!debug) throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");
|
||||
*OS << "frame_addr "; break;
|
||||
case RVOpcodes::FRAME_LOAD:
|
||||
case RVOpcodes::FRAME_STORE:
|
||||
// These should have been eliminated by RegAlloc
|
||||
throw std::runtime_error("FRAME pseudo-instruction not eliminated before AsmPrinter");
|
||||
default:
|
||||
throw std::runtime_error("Unknown opcode in AsmPrinter");
|
||||
}
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
#include "RISCv64ISel.h"
|
||||
#include "RISCv64RegAlloc.h"
|
||||
#include "RISCv64AsmPrinter.h"
|
||||
#include "RISCv64Passes.h" // 包含优化Pass的头文件
|
||||
#include <sstream>
|
||||
|
||||
namespace sysy {
|
||||
@ -12,63 +11,17 @@ std::string RISCv64CodeGen::code_gen() {
|
||||
return module_gen();
|
||||
}
|
||||
|
||||
// 模块级代码生成
|
||||
// 模块级代码生成 (移植自原文件,处理.data段和驱动函数生成)
|
||||
std::string RISCv64CodeGen::module_gen() {
|
||||
std::stringstream ss;
|
||||
|
||||
// --- [新逻辑] 步骤1:将全局变量分为.data和.bss两组 ---
|
||||
std::vector<GlobalValue*> data_globals;
|
||||
std::vector<GlobalValue*> bss_globals;
|
||||
|
||||
for (const auto& global_ptr : module->getGlobals()) {
|
||||
GlobalValue* global = global_ptr.get();
|
||||
const auto& init_values = global->getInitValues();
|
||||
|
||||
// 判断是否为大型零初始化数组,以便放入.bss段
|
||||
bool is_large_zero_array = false;
|
||||
// 规则:初始化列表只有一项,且该项是值为0的整数,且数量大于一个阈值(例如16)
|
||||
if (init_values.getValues().size() == 1) {
|
||||
if (auto const_val = dynamic_cast<ConstantValue*>(init_values.getValues()[0])) {
|
||||
if (const_val->isInt() && const_val->getInt() == 0 && init_values.getNumbers()[0] > 16) {
|
||||
is_large_zero_array = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_large_zero_array) {
|
||||
bss_globals.push_back(global);
|
||||
} else {
|
||||
data_globals.push_back(global);
|
||||
}
|
||||
}
|
||||
|
||||
// --- [新逻辑] 步骤2:生成 .bss 段的代码 ---
|
||||
if (!bss_globals.empty()) {
|
||||
ss << ".bss\n"; // 切换到 .bss 段
|
||||
for (GlobalValue* global : bss_globals) {
|
||||
// 获取数组总大小(元素个数 * 元素大小)
|
||||
// 在SysY中,我们假设元素都是4字节(int或float)
|
||||
unsigned count = global->getInitValues().getNumbers()[0];
|
||||
unsigned total_size = count * 4;
|
||||
|
||||
ss << " .align 3\n"; // 8字节对齐 (2^3)
|
||||
ss << ".globl " << global->getName() << "\n";
|
||||
ss << ".type " << global->getName() << ", @object\n";
|
||||
ss << ".size " << global->getName() << ", " << total_size << "\n";
|
||||
ss << global->getName() << ":\n";
|
||||
// 使用 .space 指令来预留指定大小的零填充空间
|
||||
ss << " .space " << total_size << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// --- [旧逻辑保留] 步骤3:生成 .data 段的代码 ---
|
||||
if (!data_globals.empty()) {
|
||||
ss << ".data\n"; // 切换到 .data 段
|
||||
for (GlobalValue* global : data_globals) {
|
||||
// 1. 处理全局变量 (.data段)
|
||||
if (!module->getGlobals().empty()) {
|
||||
ss << ".data\n";
|
||||
for (const auto& global : module->getGlobals()) {
|
||||
ss << ".globl " << global->getName() << "\n";
|
||||
ss << global->getName() << ":\n";
|
||||
const auto& init_values = global->getInitValues();
|
||||
// 使用您原有的逻辑来处理显式初始化的值
|
||||
for (size_t i = 0; i < init_values.getValues().size(); ++i) {
|
||||
auto val = init_values.getValues()[i];
|
||||
auto count = init_values.getNumbers()[i];
|
||||
@ -87,7 +40,7 @@ std::string RISCv64CodeGen::module_gen() {
|
||||
}
|
||||
}
|
||||
|
||||
// --- 处理函数 (.text段) 的逻辑保持不变 ---
|
||||
// 2. 处理函数 (.text段)
|
||||
if (!module->getFunctions().empty()) {
|
||||
ss << ".text\n";
|
||||
for (const auto& func_pair : module->getFunctions()) {
|
||||
@ -99,43 +52,21 @@ std::string RISCv64CodeGen::module_gen() {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// function_gen 现在是包含具体优化名称的、完整的处理流水线
|
||||
// function_gen 现在是新的、模块化的处理流水线
|
||||
std::string RISCv64CodeGen::function_gen(Function* func) {
|
||||
// === 完整的后端处理流水线 ===
|
||||
|
||||
// 阶段 1: 指令选择 (sysy::IR -> LLIR with virtual registers)
|
||||
RISCv64ISel isel;
|
||||
std::unique_ptr<MachineFunction> mfunc = isel.runOnFunction(func);
|
||||
|
||||
std::stringstream ss1;
|
||||
RISCv64AsmPrinter printer1(mfunc.get());
|
||||
printer1.run(ss1, true);
|
||||
|
||||
// 阶段 2: 指令调度 (Instruction Scheduling)
|
||||
PreRA_Scheduler scheduler;
|
||||
scheduler.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// 阶段 3: 物理寄存器分配 (Register Allocation)
|
||||
// 阶段 2: 寄存器分配 (包含栈帧布局, 活跃性分析, 图着色, spill/rewrite)
|
||||
RISCv64RegAlloc reg_alloc(mfunc.get());
|
||||
reg_alloc.run();
|
||||
|
||||
// 阶段 3.5: 处理被调用者保存寄存器
|
||||
CalleeSavedHandler callee_handler;
|
||||
callee_handler.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// 阶段 4: 窥孔优化 (Peephole Optimization)
|
||||
PeepholeOptimizer peephole;
|
||||
peephole.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// 阶段 5: 局部指令调度 (Local Scheduling)
|
||||
PostRA_Scheduler local_scheduler;
|
||||
local_scheduler.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// 阶段 6: 代码发射 (Code Emission)
|
||||
// 阶段 3: 代码发射 (LLIR with physical regs -> Assembly Text)
|
||||
std::stringstream ss;
|
||||
RISCv64AsmPrinter printer(mfunc.get());
|
||||
printer.run(ss);
|
||||
if (DEBUG) ss << "\n" << ss1.str(); // 将指令选择阶段的结果也包含在最终输出中
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
@ -4,13 +4,12 @@
|
||||
#include <functional>
|
||||
#include <cmath> // For std::fabs
|
||||
#include <limits> // For std::numeric_limits
|
||||
#include <iostream>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// DAG节点定义 (内部实现)
|
||||
struct RISCv64ISel::DAGNode {
|
||||
enum NodeKind {ARGUMENT, CONSTANT, LOAD, STORE, BINARY, CALL, RETURN, BRANCH, ALLOCA_ADDR, UNARY, MEMSET, GET_ELEMENT_PTR};
|
||||
enum NodeKind { CONSTANT, LOAD, STORE, BINARY, CALL, RETURN, BRANCH, ALLOCA_ADDR, UNARY, MEMSET };
|
||||
NodeKind kind;
|
||||
Value* value = nullptr;
|
||||
std::vector<DAGNode*> operands;
|
||||
@ -51,28 +50,22 @@ std::unique_ptr<MachineFunction> RISCv64ISel::runOnFunction(Function* func) {
|
||||
|
||||
// 指令选择主流程
|
||||
void RISCv64ISel::select() {
|
||||
// 遍历基本块,为它们创建对应的MachineBasicBlock
|
||||
for (const auto& bb_ptr : F->getBasicBlocks()) {
|
||||
auto mbb = std::make_unique<MachineBasicBlock>(bb_ptr->getName(), MFunc.get());
|
||||
bb_map[bb_ptr.get()] = mbb.get();
|
||||
MFunc->addBlock(std::move(mbb));
|
||||
}
|
||||
|
||||
// 遍历Argument对象,为它们分配虚拟寄存器。
|
||||
if (F) {
|
||||
for (Argument* arg : F->getArguments()) {
|
||||
// getVReg会为每个代表参数的Argument对象在vreg_map中创建一个条目。
|
||||
// 这样,当后续的store指令使用这个参数时,就能找到对应的vreg。
|
||||
getVReg(arg);
|
||||
if (F->getEntryBlock()) {
|
||||
for (auto* arg_alloca : F->getEntryBlock()->getArguments()) {
|
||||
getVReg(arg_alloca);
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历基本块,进行指令选择
|
||||
for (const auto& bb_ptr : F->getBasicBlocks()) {
|
||||
selectBasicBlock(bb_ptr.get());
|
||||
}
|
||||
|
||||
// 链接MachineBasicBlock的前驱和后继
|
||||
for (const auto& bb_ptr : F->getBasicBlocks()) {
|
||||
CurMBB = bb_map.at(bb_ptr.get());
|
||||
for (auto succ : bb_ptr->getSuccessors()) {
|
||||
@ -89,10 +82,6 @@ void RISCv64ISel::selectBasicBlock(BasicBlock* bb) {
|
||||
CurMBB = bb_map.at(bb);
|
||||
auto dag = build_dag(bb);
|
||||
|
||||
if (DEBUG) { // 使用 DEBUG 宏或变量来控制是否打印
|
||||
print_dag(dag, bb->getName());
|
||||
}
|
||||
|
||||
std::map<Value*, DAGNode*> value_to_node;
|
||||
for(const auto& node : dag) {
|
||||
if (node->value) {
|
||||
@ -103,10 +92,6 @@ void RISCv64ISel::selectBasicBlock(BasicBlock* bb) {
|
||||
std::set<DAGNode*> selected_nodes;
|
||||
std::function<void(DAGNode*)> select_recursive =
|
||||
[&](DAGNode* node) {
|
||||
if (DEEPDEBUG) {
|
||||
std::cout << "[DEEPDEBUG] select_recursive: Visiting node with kind: " << node->kind
|
||||
<< " (Value: " << (node->value ? node->value->getName() : "null") << ")" << std::endl;
|
||||
}
|
||||
if (!node || selected_nodes.count(node)) return;
|
||||
for (auto operand : node->operands) {
|
||||
select_recursive(operand);
|
||||
@ -133,73 +118,46 @@ void RISCv64ISel::selectBasicBlock(BasicBlock* bb) {
|
||||
}
|
||||
}
|
||||
|
||||
// 核心函数:为DAG节点选择并生成MachineInstr (已修复和增强的完整版本)
|
||||
// 核心函数:为DAG节点选择并生成MachineInstr (忠实移植版)
|
||||
void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
// 调用者(select_recursive)已经保证了操作数节点会先于当前节点被选择。
|
||||
// 因此,这里我们只处理当前节点。
|
||||
|
||||
switch (node->kind) {
|
||||
// “延迟物化”(Late Materialization)思想。
|
||||
// 这三种节点仅作为标记,不直接生成指令。它们的目的是在DAG中保留类型信息。
|
||||
// 加载其值的责任,被转移给了使用它们的父节点(如STORE, BINARY等)。
|
||||
case DAGNode::ARGUMENT:
|
||||
case DAGNode::CONSTANT:
|
||||
case DAGNode::ALLOCA_ADDR:
|
||||
if (node->value) {
|
||||
// 确保它有一个关联的虚拟寄存器即可,不生成代码。
|
||||
getVReg(node->value);
|
||||
}
|
||||
if (node->value) getVReg(node->value);
|
||||
break;
|
||||
|
||||
case DAGNode::LOAD: {
|
||||
auto dest_vreg = getVReg(node->value);
|
||||
Value* ptr_val = node->operands[0]->value;
|
||||
|
||||
// --- 修改点 ---
|
||||
// 1. 获取加载结果的类型 (即这个LOAD指令自身的类型)
|
||||
Type* loaded_type = node->value->getType();
|
||||
|
||||
// 2. 根据类型选择正确的伪指令或真实指令操作码
|
||||
RVOpcodes frame_opcode = loaded_type->isPointer() ? RVOpcodes::FRAME_LOAD_D : RVOpcodes::FRAME_LOAD_W;
|
||||
RVOpcodes real_opcode = loaded_type->isPointer() ? RVOpcodes::LD : RVOpcodes::LW;
|
||||
|
||||
|
||||
if (auto alloca = dynamic_cast<AllocaInst*>(ptr_val)) {
|
||||
// 3. 创建使用新的、区分宽度的伪指令
|
||||
auto instr = std::make_unique<MachineInstr>(frame_opcode);
|
||||
auto instr = std::make_unique<MachineInstr>(RVOpcodes::FRAME_LOAD);
|
||||
instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
instr->addOperand(std::make_unique<RegOperand>(getVReg(alloca)));
|
||||
CurMBB->addInstruction(std::move(instr));
|
||||
|
||||
} else if (auto global = dynamic_cast<GlobalValue*>(ptr_val)) {
|
||||
// 对于全局变量,先用 la 加载其地址
|
||||
auto addr_vreg = getNewVReg();
|
||||
auto la = std::make_unique<MachineInstr>(RVOpcodes::LA);
|
||||
la->addOperand(std::make_unique<RegOperand>(addr_vreg));
|
||||
la->addOperand(std::make_unique<LabelOperand>(global->getName()));
|
||||
CurMBB->addInstruction(std::move(la));
|
||||
|
||||
// 然后根据类型使用 ld 或 lw 加载其值
|
||||
auto load_instr = std::make_unique<MachineInstr>(real_opcode);
|
||||
load_instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
load_instr->addOperand(std::make_unique<MemOperand>(
|
||||
auto lw = std::make_unique<MachineInstr>(RVOpcodes::LW);
|
||||
lw->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
lw->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(addr_vreg),
|
||||
std::make_unique<ImmOperand>(0)
|
||||
));
|
||||
CurMBB->addInstruction(std::move(load_instr));
|
||||
|
||||
CurMBB->addInstruction(std::move(lw));
|
||||
} else {
|
||||
// 对于已经在虚拟寄存器中的指针地址,直接通过该地址加载
|
||||
auto ptr_vreg = getVReg(ptr_val);
|
||||
|
||||
// 根据类型使用 ld 或 lw
|
||||
auto load_instr = std::make_unique<MachineInstr>(real_opcode);
|
||||
load_instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
load_instr->addOperand(std::make_unique<MemOperand>(
|
||||
auto lw = std::make_unique<MachineInstr>(RVOpcodes::LW);
|
||||
lw->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
lw->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(ptr_vreg),
|
||||
std::make_unique<ImmOperand>(0)
|
||||
));
|
||||
CurMBB->addInstruction(std::move(load_instr));
|
||||
CurMBB->addInstruction(std::move(lw));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -208,7 +166,6 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
Value* val_to_store = node->operands[0]->value;
|
||||
Value* ptr_val = node->operands[1]->value;
|
||||
|
||||
// 如果要存储的值是一个常量,就在这里生成 `li` 指令加载它
|
||||
if (auto val_const = dynamic_cast<ConstantValue*>(val_to_store)) {
|
||||
auto li = std::make_unique<MachineInstr>(RVOpcodes::LI);
|
||||
li->addOperand(std::make_unique<RegOperand>(getVReg(val_const)));
|
||||
@ -217,50 +174,34 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
}
|
||||
auto val_vreg = getVReg(val_to_store);
|
||||
|
||||
// --- 修改点 ---
|
||||
// 1. 获取被存储的值的类型
|
||||
Type* stored_type = val_to_store->getType();
|
||||
|
||||
// 2. 根据类型选择正确的伪指令或真实指令操作码
|
||||
RVOpcodes frame_opcode = stored_type->isPointer() ? RVOpcodes::FRAME_STORE_D : RVOpcodes::FRAME_STORE_W;
|
||||
RVOpcodes real_opcode = stored_type->isPointer() ? RVOpcodes::SD : RVOpcodes::SW;
|
||||
|
||||
if (auto alloca = dynamic_cast<AllocaInst*>(ptr_val)) {
|
||||
// 3. 创建使用新的、区分宽度的伪指令
|
||||
auto instr = std::make_unique<MachineInstr>(frame_opcode);
|
||||
auto instr = std::make_unique<MachineInstr>(RVOpcodes::FRAME_STORE);
|
||||
instr->addOperand(std::make_unique<RegOperand>(val_vreg));
|
||||
instr->addOperand(std::make_unique<RegOperand>(getVReg(alloca)));
|
||||
CurMBB->addInstruction(std::move(instr));
|
||||
|
||||
} else if (auto global = dynamic_cast<GlobalValue*>(ptr_val)) {
|
||||
// 向全局变量存储
|
||||
auto addr_vreg = getNewVReg();
|
||||
auto addr_vreg = getNewVReg();
|
||||
auto la = std::make_unique<MachineInstr>(RVOpcodes::LA);
|
||||
la->addOperand(std::make_unique<RegOperand>(addr_vreg));
|
||||
la->addOperand(std::make_unique<LabelOperand>(global->getName()));
|
||||
CurMBB->addInstruction(std::move(la));
|
||||
|
||||
// 根据类型使用 sd 或 sw
|
||||
auto store_instr = std::make_unique<MachineInstr>(real_opcode);
|
||||
store_instr->addOperand(std::make_unique<RegOperand>(val_vreg));
|
||||
store_instr->addOperand(std::make_unique<MemOperand>(
|
||||
auto sw = std::make_unique<MachineInstr>(RVOpcodes::SW);
|
||||
sw->addOperand(std::make_unique<RegOperand>(val_vreg));
|
||||
sw->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(addr_vreg),
|
||||
std::make_unique<ImmOperand>(0)
|
||||
));
|
||||
CurMBB->addInstruction(std::move(store_instr));
|
||||
|
||||
CurMBB->addInstruction(std::move(sw));
|
||||
} else {
|
||||
// 向一个指针(存储在虚拟寄存器中)指向的地址存储
|
||||
auto ptr_vreg = getVReg(ptr_val);
|
||||
|
||||
// 根据类型使用 sd 或 sw
|
||||
auto store_instr = std::make_unique<MachineInstr>(real_opcode);
|
||||
store_instr->addOperand(std::make_unique<RegOperand>(val_vreg));
|
||||
store_instr->addOperand(std::make_unique<MemOperand>(
|
||||
auto sw = std::make_unique<MachineInstr>(RVOpcodes::SW);
|
||||
sw->addOperand(std::make_unique<RegOperand>(val_vreg));
|
||||
sw->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(ptr_vreg),
|
||||
std::make_unique<ImmOperand>(0)
|
||||
));
|
||||
CurMBB->addInstruction(std::move(store_instr));
|
||||
CurMBB->addInstruction(std::move(sw));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -269,108 +210,37 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
auto bin = dynamic_cast<BinaryInst*>(node->value);
|
||||
Value* lhs = bin->getLhs();
|
||||
Value* rhs = bin->getRhs();
|
||||
|
||||
if (bin->getKind() == BinaryInst::kAdd) {
|
||||
Value* base = nullptr;
|
||||
Value* offset = nullptr;
|
||||
|
||||
// [修改] 扩展基地址的判断,使其可以识别 AllocaInst 或 GlobalValue
|
||||
if (dynamic_cast<AllocaInst*>(lhs) || dynamic_cast<GlobalValue*>(lhs)) {
|
||||
base = lhs;
|
||||
offset = rhs;
|
||||
} else if (dynamic_cast<AllocaInst*>(rhs) || dynamic_cast<GlobalValue*>(rhs)) {
|
||||
base = rhs;
|
||||
offset = lhs;
|
||||
}
|
||||
|
||||
// 如果成功匹配到地址计算模式
|
||||
if (base) {
|
||||
// 1. 先为偏移量加载常量(如果它是常量的话)
|
||||
if (auto const_offset = dynamic_cast<ConstantValue*>(offset)) {
|
||||
auto li = std::make_unique<MachineInstr>(RVOpcodes::LI);
|
||||
li->addOperand(std::make_unique<RegOperand>(getVReg(const_offset)));
|
||||
li->addOperand(std::make_unique<ImmOperand>(const_offset->getInt()));
|
||||
CurMBB->addInstruction(std::move(li));
|
||||
}
|
||||
|
||||
// 2. [修改] 根据基地址的类型,生成不同的指令来获取基地址
|
||||
auto base_addr_vreg = getNewVReg(); // 创建一个新的临时vreg来存放基地址
|
||||
|
||||
// 情况一:基地址是局部栈变量
|
||||
if (auto alloca_base = dynamic_cast<AllocaInst*>(base)) {
|
||||
auto frame_addr_instr = std::make_unique<MachineInstr>(RVOpcodes::FRAME_ADDR);
|
||||
frame_addr_instr->addOperand(std::make_unique<RegOperand>(base_addr_vreg));
|
||||
frame_addr_instr->addOperand(std::make_unique<RegOperand>(getVReg(alloca_base)));
|
||||
CurMBB->addInstruction(std::move(frame_addr_instr));
|
||||
}
|
||||
// 情况二:基地址是全局变量
|
||||
else if (auto global_base = dynamic_cast<GlobalValue*>(base)) {
|
||||
auto la_instr = std::make_unique<MachineInstr>(RVOpcodes::LA);
|
||||
la_instr->addOperand(std::make_unique<RegOperand>(base_addr_vreg));
|
||||
la_instr->addOperand(std::make_unique<LabelOperand>(global_base->getName()));
|
||||
CurMBB->addInstruction(std::move(la_instr));
|
||||
}
|
||||
|
||||
// 3. 生成真正的add指令,计算最终地址(这部分逻辑保持不变)
|
||||
auto final_addr_vreg = getVReg(bin); // 这是整个二元运算的结果vreg
|
||||
auto offset_vreg = getVReg(offset);
|
||||
auto add_instr = std::make_unique<MachineInstr>(RVOpcodes::ADD); // 指针运算是64位
|
||||
add_instr->addOperand(std::make_unique<RegOperand>(final_addr_vreg));
|
||||
add_instr->addOperand(std::make_unique<RegOperand>(base_addr_vreg));
|
||||
add_instr->addOperand(std::make_unique<RegOperand>(offset_vreg));
|
||||
CurMBB->addInstruction(std::move(add_instr));
|
||||
|
||||
return; // 地址计算处理完毕,直接返回
|
||||
}
|
||||
}
|
||||
|
||||
// [V2优点] 在BINARY节点内部按需加载常量操作数。
|
||||
auto load_val_if_const = [&](Value* val) {
|
||||
if (auto c = dynamic_cast<ConstantValue*>(val)) {
|
||||
if (DEBUG) {
|
||||
std::cout << "[DEBUG] selectNode-BINARY: Found constant operand with value " << c->getInt()
|
||||
<< ". Generating LI instruction." << std::endl;
|
||||
}
|
||||
auto li = std::make_unique<MachineInstr>(RVOpcodes::LI);
|
||||
li->addOperand(std::make_unique<RegOperand>(getVReg(c)));
|
||||
li->addOperand(std::make_unique<ImmOperand>(c->getInt()));
|
||||
CurMBB->addInstruction(std::move(li));
|
||||
}
|
||||
};
|
||||
|
||||
// 检查是否能应用立即数优化。
|
||||
bool rhs_is_imm_opt = false;
|
||||
if (auto rhs_const = dynamic_cast<ConstantValue*>(rhs)) {
|
||||
if (bin->getKind() == BinaryInst::kAdd && rhs_const->getInt() >= -2048 && rhs_const->getInt() < 2048) {
|
||||
rhs_is_imm_opt = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 仅在不能作为立即数操作数时才需要提前加载。
|
||||
load_val_if_const(lhs);
|
||||
if (!rhs_is_imm_opt) {
|
||||
load_val_if_const(rhs);
|
||||
}
|
||||
load_val_if_const(rhs);
|
||||
|
||||
auto dest_vreg = getVReg(bin);
|
||||
auto lhs_vreg = getVReg(lhs);
|
||||
|
||||
// [V2优点] 融合 ADDIW 优化。
|
||||
if (rhs_is_imm_opt) {
|
||||
auto rhs_const = dynamic_cast<ConstantValue*>(rhs);
|
||||
auto instr = std::make_unique<MachineInstr>(RVOpcodes::ADDIW);
|
||||
instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
instr->addOperand(std::make_unique<RegOperand>(lhs_vreg));
|
||||
instr->addOperand(std::make_unique<ImmOperand>(rhs_const->getInt()));
|
||||
CurMBB->addInstruction(std::move(instr));
|
||||
return; // 指令已生成,直接返回。
|
||||
}
|
||||
|
||||
auto rhs_vreg = getVReg(rhs);
|
||||
|
||||
if (bin->getKind() == BinaryInst::kAdd) {
|
||||
if (auto rhs_const = dynamic_cast<ConstantValue*>(rhs)) {
|
||||
if (rhs_const->getInt() >= -2048 && rhs_const->getInt() < 2048) {
|
||||
auto instr = std::make_unique<MachineInstr>(RVOpcodes::ADDIW);
|
||||
instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
instr->addOperand(std::make_unique<RegOperand>(lhs_vreg));
|
||||
instr->addOperand(std::make_unique<ImmOperand>(rhs_const->getInt()));
|
||||
CurMBB->addInstruction(std::move(instr));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch (bin->getKind()) {
|
||||
case BinaryInst::kAdd: {
|
||||
// 区分指针运算(64位)和整数运算(32位)。
|
||||
RVOpcodes opcode = (lhs->getType()->isPointer() || rhs->getType()->isPointer()) ? RVOpcodes::ADD : RVOpcodes::ADDW;
|
||||
auto instr = std::make_unique<MachineInstr>(opcode);
|
||||
instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
@ -411,7 +281,7 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(instr));
|
||||
break;
|
||||
}
|
||||
case BinaryInst::kICmpEQ: { // 等于 (a == b) -> (subw; seqz)
|
||||
case BinaryInst::kICmpEQ: {
|
||||
auto sub = std::make_unique<MachineInstr>(RVOpcodes::SUBW);
|
||||
sub->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
sub->addOperand(std::make_unique<RegOperand>(lhs_vreg));
|
||||
@ -424,7 +294,7 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(seqz));
|
||||
break;
|
||||
}
|
||||
case BinaryInst::kICmpNE: { // 不等于 (a != b) -> (subw; snez)
|
||||
case BinaryInst::kICmpNE: {
|
||||
auto sub = std::make_unique<MachineInstr>(RVOpcodes::SUBW);
|
||||
sub->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
sub->addOperand(std::make_unique<RegOperand>(lhs_vreg));
|
||||
@ -437,7 +307,7 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(snez));
|
||||
break;
|
||||
}
|
||||
case BinaryInst::kICmpLT: { // 小于 (a < b) -> slt
|
||||
case BinaryInst::kICmpLT: {
|
||||
auto instr = std::make_unique<MachineInstr>(RVOpcodes::SLT);
|
||||
instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
instr->addOperand(std::make_unique<RegOperand>(lhs_vreg));
|
||||
@ -445,7 +315,7 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(instr));
|
||||
break;
|
||||
}
|
||||
case BinaryInst::kICmpGT: { // 大于 (a > b) -> (b < a) -> slt
|
||||
case BinaryInst::kICmpGT: {
|
||||
auto instr = std::make_unique<MachineInstr>(RVOpcodes::SLT);
|
||||
instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
instr->addOperand(std::make_unique<RegOperand>(rhs_vreg));
|
||||
@ -453,7 +323,7 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(instr));
|
||||
break;
|
||||
}
|
||||
case BinaryInst::kICmpLE: { // 小于等于 (a <= b) -> !(b < a) -> (slt; xori)
|
||||
case BinaryInst::kICmpLE: {
|
||||
auto slt = std::make_unique<MachineInstr>(RVOpcodes::SLT);
|
||||
slt->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
slt->addOperand(std::make_unique<RegOperand>(rhs_vreg));
|
||||
@ -467,7 +337,7 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(xori));
|
||||
break;
|
||||
}
|
||||
case BinaryInst::kICmpGE: { // 大于等于 (a >= b) -> !(a < b) -> (slt; xori)
|
||||
case BinaryInst::kICmpGE: {
|
||||
auto slt = std::make_unique<MachineInstr>(RVOpcodes::SLT);
|
||||
slt->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
slt->addOperand(std::make_unique<RegOperand>(lhs_vreg));
|
||||
@ -493,7 +363,7 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
auto src_vreg = getVReg(unary->getOperand());
|
||||
|
||||
switch (unary->getKind()) {
|
||||
case UnaryInst::kNeg: { // 取负: 0 - src
|
||||
case UnaryInst::kNeg: {
|
||||
auto instr = std::make_unique<MachineInstr>(RVOpcodes::SUBW);
|
||||
instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
instr->addOperand(std::make_unique<RegOperand>(PhysicalReg::ZERO));
|
||||
@ -501,7 +371,7 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(instr));
|
||||
break;
|
||||
}
|
||||
case UnaryInst::kNot: { // 逻辑非: src == 0 ? 1 : 0
|
||||
case UnaryInst::kNot: {
|
||||
auto instr = std::make_unique<MachineInstr>(RVOpcodes::SEQZ);
|
||||
instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
instr->addOperand(std::make_unique<RegOperand>(src_vreg));
|
||||
@ -516,10 +386,7 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
|
||||
case DAGNode::CALL: {
|
||||
auto call = dynamic_cast<CallInst*>(node->value);
|
||||
// 处理函数参数,放入a0-a7物理寄存器
|
||||
size_t num_operands = node->operands.size();
|
||||
size_t reg_arg_count = std::min(num_operands, (size_t)8);
|
||||
for (size_t i = 0; i < reg_arg_count; ++i) {
|
||||
for (size_t i = 0; i < node->operands.size() && i < 8; ++i) {
|
||||
DAGNode* arg_node = node->operands[i];
|
||||
auto arg_preg = static_cast<PhysicalReg>(static_cast<int>(PhysicalReg::A0) + i);
|
||||
|
||||
@ -538,64 +405,11 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(mv));
|
||||
}
|
||||
}
|
||||
if (num_operands > 8) {
|
||||
size_t stack_arg_count = num_operands - 8;
|
||||
int stack_space = stack_arg_count * 8; // RV64中每个参数槽位8字节
|
||||
|
||||
// 2a. 在栈上分配空间
|
||||
auto alloc_instr = std::make_unique<MachineInstr>(RVOpcodes::ADDI);
|
||||
alloc_instr->addOperand(std::make_unique<RegOperand>(PhysicalReg::SP));
|
||||
alloc_instr->addOperand(std::make_unique<RegOperand>(PhysicalReg::SP));
|
||||
alloc_instr->addOperand(std::make_unique<ImmOperand>(-stack_space));
|
||||
CurMBB->addInstruction(std::move(alloc_instr));
|
||||
|
||||
// 2b. 存储每个栈参数
|
||||
for (size_t i = 8; i < num_operands; ++i) {
|
||||
DAGNode* arg_node = node->operands[i];
|
||||
unsigned src_vreg;
|
||||
|
||||
// 准备源寄存器
|
||||
if (arg_node->kind == DAGNode::CONSTANT) {
|
||||
// 如果是常量,先加载到临时寄存器
|
||||
src_vreg = getNewVReg();
|
||||
auto const_val = dynamic_cast<ConstantValue*>(arg_node->value);
|
||||
auto li = std::make_unique<MachineInstr>(RVOpcodes::LI);
|
||||
li->addOperand(std::make_unique<RegOperand>(src_vreg));
|
||||
li->addOperand(std::make_unique<ImmOperand>(const_val->getInt()));
|
||||
CurMBB->addInstruction(std::move(li));
|
||||
} else {
|
||||
src_vreg = getVReg(arg_node->value);
|
||||
}
|
||||
|
||||
// 计算在栈上的偏移量
|
||||
int offset = (i - 8) * 8;
|
||||
|
||||
// 生成 sd 指令
|
||||
auto sd_instr = std::make_unique<MachineInstr>(RVOpcodes::SD);
|
||||
sd_instr->addOperand(std::make_unique<RegOperand>(src_vreg));
|
||||
sd_instr->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(PhysicalReg::SP),
|
||||
std::make_unique<ImmOperand>(offset)
|
||||
));
|
||||
CurMBB->addInstruction(std::move(sd_instr));
|
||||
}
|
||||
}
|
||||
|
||||
auto call_instr = std::make_unique<MachineInstr>(RVOpcodes::CALL);
|
||||
call_instr->addOperand(std::make_unique<LabelOperand>(call->getCallee()->getName()));
|
||||
CurMBB->addInstruction(std::move(call_instr));
|
||||
|
||||
if (num_operands > 8) {
|
||||
size_t stack_arg_count = num_operands - 8;
|
||||
int stack_space = stack_arg_count * 8;
|
||||
|
||||
auto dealloc_instr = std::make_unique<MachineInstr>(RVOpcodes::ADDI);
|
||||
dealloc_instr->addOperand(std::make_unique<RegOperand>(PhysicalReg::SP));
|
||||
dealloc_instr->addOperand(std::make_unique<RegOperand>(PhysicalReg::SP));
|
||||
dealloc_instr->addOperand(std::make_unique<ImmOperand>(stack_space));
|
||||
CurMBB->addInstruction(std::move(dealloc_instr));
|
||||
}
|
||||
// 处理返回值,从a0移动到目标虚拟寄存器
|
||||
if (!call->getType()->isVoid()) {
|
||||
auto mv_instr = std::make_unique<MachineInstr>(RVOpcodes::MV);
|
||||
mv_instr->addOperand(std::make_unique<RegOperand>(getVReg(call)));
|
||||
@ -609,7 +423,6 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
auto ret_inst_ir = dynamic_cast<ReturnInst*>(node->value);
|
||||
if (ret_inst_ir && ret_inst_ir->hasReturnValue()) {
|
||||
Value* ret_val = ret_inst_ir->getReturnValue();
|
||||
// [V2优点] 在RETURN节点内加载常量返回值
|
||||
if (auto const_val = dynamic_cast<ConstantValue*>(ret_val)) {
|
||||
auto li_instr = std::make_unique<MachineInstr>(RVOpcodes::LI);
|
||||
li_instr->addOperand(std::make_unique<RegOperand>(PhysicalReg::A0));
|
||||
@ -622,121 +435,36 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(mv_instr));
|
||||
}
|
||||
}
|
||||
// [V1设计保留] 函数尾声(epilogue)不由RETURN节点生成,
|
||||
// 而是由后续的AsmPrinter或其它Pass统一处理,这是一种常见且有效的模块化设计。
|
||||
auto ret_mi = std::make_unique<MachineInstr>(RVOpcodes::RET);
|
||||
CurMBB->addInstruction(std::move(ret_mi));
|
||||
break;
|
||||
}
|
||||
|
||||
case DAGNode::BRANCH: {
|
||||
// 处理条件分支
|
||||
if (auto cond_br = dynamic_cast<CondBrInst*>(node->value)) {
|
||||
Value* condition = cond_br->getCondition();
|
||||
auto then_bb_name = cond_br->getThenBlock()->getName();
|
||||
auto else_bb_name = cond_br->getElseBlock()->getName();
|
||||
|
||||
// [优化] 检查分支条件是否为编译期常量
|
||||
if (auto const_cond = dynamic_cast<ConstantValue*>(condition)) {
|
||||
// 如果条件是常量,直接生成一个无条件跳转J,而不是BNE
|
||||
if (const_cond->getInt() != 0) { // 条件为 true
|
||||
auto j_instr = std::make_unique<MachineInstr>(RVOpcodes::J);
|
||||
j_instr->addOperand(std::make_unique<LabelOperand>(then_bb_name));
|
||||
CurMBB->addInstruction(std::move(j_instr));
|
||||
} else { // 条件为 false
|
||||
auto j_instr = std::make_unique<MachineInstr>(RVOpcodes::J);
|
||||
j_instr->addOperand(std::make_unique<LabelOperand>(else_bb_name));
|
||||
CurMBB->addInstruction(std::move(j_instr));
|
||||
}
|
||||
}
|
||||
// 如果条件不是常量,则执行标准流程
|
||||
else {
|
||||
// [修复] 为条件变量生成加载指令(如果它是常量的话,尽管上面已经处理了)
|
||||
// 这一步是为了逻辑完整,以防有其他类型的常量没有被捕获
|
||||
if (auto const_val = dynamic_cast<ConstantValue*>(condition)) {
|
||||
auto li = std::make_unique<MachineInstr>(RVOpcodes::LI);
|
||||
li->addOperand(std::make_unique<RegOperand>(getVReg(const_val)));
|
||||
li->addOperand(std::make_unique<ImmOperand>(const_val->getInt()));
|
||||
CurMBB->addInstruction(std::move(li));
|
||||
}
|
||||
|
||||
auto cond_vreg = getVReg(condition);
|
||||
|
||||
// 生成 bne cond, zero, then_label (如果cond不为0,则跳转到then)
|
||||
if (auto cond_br = dynamic_cast<CondBrInst*>(node->value)) {
|
||||
auto br_instr = std::make_unique<MachineInstr>(RVOpcodes::BNE);
|
||||
br_instr->addOperand(std::make_unique<RegOperand>(cond_vreg));
|
||||
br_instr->addOperand(std::make_unique<RegOperand>(getVReg(cond_br->getCondition())));
|
||||
br_instr->addOperand(std::make_unique<RegOperand>(PhysicalReg::ZERO));
|
||||
br_instr->addOperand(std::make_unique<LabelOperand>(then_bb_name));
|
||||
br_instr->addOperand(std::make_unique<LabelOperand>(cond_br->getThenBlock()->getName()));
|
||||
CurMBB->addInstruction(std::move(br_instr));
|
||||
|
||||
// 为else分支生成无条件跳转 (后续Pass可以优化掉不必要的跳转)
|
||||
} else if (auto uncond_br = dynamic_cast<UncondBrInst*>(node->value)) {
|
||||
auto j_instr = std::make_unique<MachineInstr>(RVOpcodes::J);
|
||||
j_instr->addOperand(std::make_unique<LabelOperand>(else_bb_name));
|
||||
j_instr->addOperand(std::make_unique<LabelOperand>(uncond_br->getBlock()->getName()));
|
||||
CurMBB->addInstruction(std::move(j_instr));
|
||||
}
|
||||
}
|
||||
// 处理无条件分支
|
||||
else if (auto uncond_br = dynamic_cast<UncondBrInst*>(node->value)) {
|
||||
auto j_instr = std::make_unique<MachineInstr>(RVOpcodes::J);
|
||||
j_instr->addOperand(std::make_unique<LabelOperand>(uncond_br->getBlock()->getName()));
|
||||
CurMBB->addInstruction(std::move(j_instr));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case DAGNode::MEMSET: {
|
||||
// [V1设计保留] Memset的核心展开逻辑在虚拟寄存器层面是正确的,无需修改。
|
||||
// 之前的bug是由于其输入(地址、值、大小)的虚拟寄存器未被正确初始化。
|
||||
// 在修复了CONSTANT/ALLOCA_ADDR的加载问题后,此处的逻辑现在可以正常工作。
|
||||
|
||||
if (DEBUG) {
|
||||
std::cout << "[DEBUG] selectNode-MEMSET: Processing MEMSET node." << std::endl;
|
||||
}
|
||||
auto memset = dynamic_cast<MemsetInst*>(node->value);
|
||||
Value* val_to_set = memset->getValue();
|
||||
Value* size_to_set = memset->getSize();
|
||||
Value* ptr_val = memset->getPointer();
|
||||
auto dest_addr_vreg = getVReg(ptr_val);
|
||||
|
||||
if (auto const_val = dynamic_cast<ConstantValue*>(val_to_set)) {
|
||||
if (DEBUG) {
|
||||
std::cout << "[DEBUG] selectNode-MEMSET: Found constant 'value' operand (" << const_val->getInt() << "). Generating LI." << std::endl;
|
||||
}
|
||||
auto li = std::make_unique<MachineInstr>(RVOpcodes::LI);
|
||||
li->addOperand(std::make_unique<RegOperand>(getVReg(const_val)));
|
||||
li->addOperand(std::make_unique<ImmOperand>(const_val->getInt()));
|
||||
CurMBB->addInstruction(std::move(li));
|
||||
}
|
||||
if (auto const_size = dynamic_cast<ConstantValue*>(size_to_set)) {
|
||||
if (DEBUG) {
|
||||
std::cout << "[DEBUG] selectNode-MEMSET: Found constant 'size' operand (" << const_size->getInt() << "). Generating LI." << std::endl;
|
||||
}
|
||||
auto li = std::make_unique<MachineInstr>(RVOpcodes::LI);
|
||||
li->addOperand(std::make_unique<RegOperand>(getVReg(const_size)));
|
||||
li->addOperand(std::make_unique<ImmOperand>(const_size->getInt()));
|
||||
CurMBB->addInstruction(std::move(li));
|
||||
}
|
||||
if (auto alloca = dynamic_cast<AllocaInst*>(ptr_val)) {
|
||||
if (DEBUG) {
|
||||
std::cout << "[DEBUG] selectNode-MEMSET: Found 'pointer' operand is an AllocaInst. Generating FRAME_ADDR." << std::endl;
|
||||
}
|
||||
// 生成新的伪指令来获取栈地址
|
||||
auto instr = std::make_unique<MachineInstr>(RVOpcodes::FRAME_ADDR);
|
||||
instr->addOperand(std::make_unique<RegOperand>(dest_addr_vreg)); // 目标虚拟寄存器
|
||||
instr->addOperand(std::make_unique<RegOperand>(getVReg(alloca))); // 源AllocaInst
|
||||
CurMBB->addInstruction(std::move(instr));
|
||||
}
|
||||
auto r_dest_addr = getVReg(memset->getPointer());
|
||||
auto r_num_bytes = getVReg(memset->getSize());
|
||||
auto r_value_byte = getVReg(memset->getValue());
|
||||
|
||||
// 为memset内部逻辑创建新的临时虚拟寄存器
|
||||
auto r_counter = getNewVReg();
|
||||
auto r_end_addr = getNewVReg();
|
||||
auto r_current_addr = getNewVReg();
|
||||
auto r_temp_val = getNewVReg();
|
||||
|
||||
// 定义一系列lambda表达式来简化指令创建
|
||||
auto add_instr = [&](RVOpcodes op, unsigned rd, unsigned rs1, unsigned rs2) {
|
||||
auto i = std::make_unique<MachineInstr>(op);
|
||||
i->addOperand(std::make_unique<RegOperand>(rd));
|
||||
@ -775,14 +503,12 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(i));
|
||||
};
|
||||
|
||||
// 生成唯一的循环标签
|
||||
int unique_id = this->local_label_counter++;
|
||||
std::string loop_start_label = MFunc->getName() + "_memset_loop_start_" + std::to_string(unique_id);
|
||||
std::string loop_end_label = MFunc->getName() + "_memset_loop_end_" + std::to_string(unique_id);
|
||||
std::string remainder_label = MFunc->getName() + "_memset_remainder_" + std::to_string(unique_id);
|
||||
std::string done_label = MFunc->getName() + "_memset_done_" + std::to_string(unique_id);
|
||||
|
||||
// 构造64位的填充值
|
||||
addi_instr(RVOpcodes::ANDI, r_temp_val, r_value_byte, 255);
|
||||
addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 8);
|
||||
add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte);
|
||||
@ -790,8 +516,6 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte);
|
||||
addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 32);
|
||||
add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte);
|
||||
|
||||
// 计算循环边界
|
||||
add_instr(RVOpcodes::ADD, r_end_addr, r_dest_addr, r_num_bytes);
|
||||
auto mv = std::make_unique<MachineInstr>(RVOpcodes::MV);
|
||||
mv->addOperand(std::make_unique<RegOperand>(r_current_addr));
|
||||
@ -799,128 +523,21 @@ void RISCv64ISel::selectNode(DAGNode* node) {
|
||||
CurMBB->addInstruction(std::move(mv));
|
||||
addi_instr(RVOpcodes::ANDI, r_counter, r_num_bytes, -8);
|
||||
add_instr(RVOpcodes::ADD, r_counter, r_dest_addr, r_counter);
|
||||
|
||||
// 8字节主循环
|
||||
label_instr(loop_start_label);
|
||||
branch_instr(RVOpcodes::BGEU, r_current_addr, r_counter, loop_end_label);
|
||||
store_instr(RVOpcodes::SD, r_temp_val, r_current_addr, 0);
|
||||
addi_instr(RVOpcodes::ADDI, r_current_addr, r_current_addr, 8);
|
||||
jump_instr(loop_start_label);
|
||||
|
||||
// 1字节收尾循环
|
||||
label_instr(loop_end_label);
|
||||
label_instr(remainder_label);
|
||||
branch_instr(RVOpcodes::BGEU, r_current_addr, r_end_addr, done_label);
|
||||
store_instr(RVOpcodes::SB, r_temp_val, r_current_addr, 0);
|
||||
addi_instr(RVOpcodes::ADDI, r_current_addr, r_current_addr, 1);
|
||||
jump_instr(remainder_label);
|
||||
|
||||
label_instr(done_label);
|
||||
break;
|
||||
}
|
||||
|
||||
case DAGNode::GET_ELEMENT_PTR: {
|
||||
auto gep = dynamic_cast<GetElementPtrInst*>(node->value);
|
||||
auto result_vreg = getVReg(gep);
|
||||
|
||||
// --- Step 1: 获取基地址 (此部分逻辑正确,保持不变) ---
|
||||
auto base_ptr_node = node->operands[0];
|
||||
auto current_addr_vreg = getNewVReg();
|
||||
|
||||
if (auto alloca_base = dynamic_cast<AllocaInst*>(base_ptr_node->value)) {
|
||||
auto frame_addr_instr = std::make_unique<MachineInstr>(RVOpcodes::FRAME_ADDR);
|
||||
frame_addr_instr->addOperand(std::make_unique<RegOperand>(current_addr_vreg));
|
||||
frame_addr_instr->addOperand(std::make_unique<RegOperand>(getVReg(alloca_base)));
|
||||
CurMBB->addInstruction(std::move(frame_addr_instr));
|
||||
} else if (auto global_base = dynamic_cast<GlobalValue*>(base_ptr_node->value)) {
|
||||
auto la_instr = std::make_unique<MachineInstr>(RVOpcodes::LA);
|
||||
la_instr->addOperand(std::make_unique<RegOperand>(current_addr_vreg));
|
||||
la_instr->addOperand(std::make_unique<LabelOperand>(global_base->getName()));
|
||||
CurMBB->addInstruction(std::move(la_instr));
|
||||
} else {
|
||||
auto base_vreg = getVReg(base_ptr_node->value);
|
||||
auto mv = std::make_unique<MachineInstr>(RVOpcodes::MV);
|
||||
mv->addOperand(std::make_unique<RegOperand>(current_addr_vreg));
|
||||
mv->addOperand(std::make_unique<RegOperand>(base_vreg));
|
||||
CurMBB->addInstruction(std::move(mv));
|
||||
}
|
||||
|
||||
// --- Step 2: [最终权威版] 遵循LLVM GEP语义迭代计算地址 ---
|
||||
|
||||
// 初始被索引的类型,是基指针指向的那个类型 (例如, [2 x i32])
|
||||
Type* current_type = gep->getBasePointer()->getType()->as<PointerType>()->getBaseType();
|
||||
|
||||
// 迭代处理 GEP 的每一个索引
|
||||
for (size_t i = 0; i < gep->getNumIndices(); ++i) {
|
||||
Value* indexValue = gep->getIndex(i);
|
||||
|
||||
// GEP的第一个索引以整个 `current_type` 的大小为步长。
|
||||
// 后续的索引则以 `current_type` 的元素大小为步长。
|
||||
// 这一步是计算地址偏移的关键。
|
||||
unsigned stride = getTypeSizeInBytes(current_type);
|
||||
|
||||
// 如果步长为0(例如对一个void类型或空结构体索引),则不产生任何偏移
|
||||
if (stride != 0) {
|
||||
// --- 为当前索引和步长生成偏移计算指令 ---
|
||||
auto offset_vreg = getNewVReg();
|
||||
auto index_vreg = getVReg(indexValue);
|
||||
|
||||
// 如果索引是常量,先用 LI 指令加载到虚拟寄存器
|
||||
if (auto const_index = dynamic_cast<ConstantValue*>(indexValue)) {
|
||||
auto li = std::make_unique<MachineInstr>(RVOpcodes::LI);
|
||||
li->addOperand(std::make_unique<RegOperand>(index_vreg));
|
||||
li->addOperand(std::make_unique<ImmOperand>(const_index->getInt()));
|
||||
CurMBB->addInstruction(std::move(li));
|
||||
}
|
||||
|
||||
// 优化:如果步长是1,可以直接移动(MV)作为偏移量,无需乘法
|
||||
if (stride == 1) {
|
||||
auto mv = std::make_unique<MachineInstr>(RVOpcodes::MV);
|
||||
mv->addOperand(std::make_unique<RegOperand>(offset_vreg));
|
||||
mv->addOperand(std::make_unique<RegOperand>(index_vreg));
|
||||
CurMBB->addInstruction(std::move(mv));
|
||||
} else {
|
||||
// 步长不为1,需要生成乘法指令
|
||||
auto size_vreg = getNewVReg();
|
||||
auto li_size = std::make_unique<MachineInstr>(RVOpcodes::LI);
|
||||
li_size->addOperand(std::make_unique<RegOperand>(size_vreg));
|
||||
li_size->addOperand(std::make_unique<ImmOperand>(stride));
|
||||
CurMBB->addInstruction(std::move(li_size));
|
||||
|
||||
auto mul = std::make_unique<MachineInstr>(RVOpcodes::MULW);
|
||||
mul->addOperand(std::make_unique<RegOperand>(offset_vreg));
|
||||
mul->addOperand(std::make_unique<RegOperand>(index_vreg));
|
||||
mul->addOperand(std::make_unique<RegOperand>(size_vreg));
|
||||
CurMBB->addInstruction(std::move(mul));
|
||||
}
|
||||
|
||||
// 将计算出的偏移量累加到当前地址上
|
||||
auto add = std::make_unique<MachineInstr>(RVOpcodes::ADD);
|
||||
add->addOperand(std::make_unique<RegOperand>(current_addr_vreg));
|
||||
add->addOperand(std::make_unique<RegOperand>(current_addr_vreg));
|
||||
add->addOperand(std::make_unique<RegOperand>(offset_vreg));
|
||||
CurMBB->addInstruction(std::move(add));
|
||||
}
|
||||
|
||||
// --- 为下一次迭代更新类型:深入一层 ---
|
||||
if (auto array_type = current_type->as<ArrayType>()) {
|
||||
current_type = array_type->getElementType();
|
||||
} else if (auto ptr_type = current_type->as<PointerType>()) {
|
||||
// 这种情况不应该在第二次迭代后发生,但为了逻辑健壮性保留
|
||||
current_type = ptr_type->getBaseType();
|
||||
}
|
||||
// 如果`current_type`已经是i32等基本类型,它会保持不变,
|
||||
// 但下一次循环如果还有索引,`getTypeSizeInBytes(i32)`仍然能正确计算步长。
|
||||
}
|
||||
|
||||
// --- Step 3: 将最终计算出的地址存入GEP的目标虚拟寄存器 (保持不变) ---
|
||||
auto final_mv = std::make_unique<MachineInstr>(RVOpcodes::MV);
|
||||
final_mv->addOperand(std::make_unique<RegOperand>(result_vreg));
|
||||
final_mv->addOperand(std::make_unique<RegOperand>(current_addr_vreg));
|
||||
CurMBB->addInstruction(std::move(final_mv));
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw std::runtime_error("Unsupported DAGNode kind in ISel");
|
||||
}
|
||||
@ -943,7 +560,6 @@ RISCv64ISel::DAGNode* RISCv64ISel::create_node(int kind_int, Value* val, std::ma
|
||||
}
|
||||
|
||||
RISCv64ISel::DAGNode* RISCv64ISel::get_operand_node(Value* val_ir, std::map<Value*, DAGNode*>& value_to_node, std::vector<std::unique_ptr<DAGNode>>& nodes_storage) {
|
||||
// 规则1:如果这个Value已经有对应的节点,直接返回
|
||||
if (value_to_node.count(val_ir)) {
|
||||
return value_to_node[val_ir];
|
||||
} else if (dynamic_cast<ConstantValue*>(val_ir)) {
|
||||
@ -952,14 +568,7 @@ RISCv64ISel::DAGNode* RISCv64ISel::get_operand_node(Value* val_ir, std::map<Valu
|
||||
return create_node(DAGNode::CONSTANT, val_ir, value_to_node, nodes_storage);
|
||||
} else if (dynamic_cast<AllocaInst*>(val_ir)) {
|
||||
return create_node(DAGNode::ALLOCA_ADDR, val_ir, value_to_node, nodes_storage);
|
||||
} else if (dynamic_cast<Argument*>(val_ir)) {
|
||||
// Argument 是一个叶子节点,它代表一个在函数入口就可用的值。
|
||||
return create_node(DAGNode::ARGUMENT, val_ir, value_to_node, nodes_storage);
|
||||
}
|
||||
// 默认行为:如果一个操作数不是上面任何一种叶子节点,
|
||||
// 并且没有在value_to_node中找到(意味着它不是由另一条指令定义的),
|
||||
// 这是一个逻辑问题。但为了保持向前兼容,我们暂时保留旧的LOAD行为,
|
||||
// 尽管在修复Argument后,它不应该再被错误触发。
|
||||
return create_node(DAGNode::LOAD, val_ir, value_to_node, nodes_storage);
|
||||
}
|
||||
|
||||
@ -981,27 +590,6 @@ std::vector<std::unique_ptr<RISCv64ISel::DAGNode>> RISCv64ISel::build_dag(BasicB
|
||||
memset_node->operands.push_back(get_operand_node(memset->getBegin(), value_to_node, nodes_storage));
|
||||
memset_node->operands.push_back(get_operand_node(memset->getSize(), value_to_node, nodes_storage));
|
||||
memset_node->operands.push_back(get_operand_node(memset->getValue(), value_to_node, nodes_storage));
|
||||
if (DEBUG) {
|
||||
std::cout << "[DEBUG] build_dag: Created MEMSET node for: " << memset->getName() << std::endl;
|
||||
for (size_t i = 0; i < memset_node->operands.size(); ++i) {
|
||||
std::cout << " -> Operand " << i << " has kind: " << memset_node->operands[i]->kind << std::endl;
|
||||
}
|
||||
}
|
||||
} else if (auto gep = dynamic_cast<GetElementPtrInst*>(inst)) {
|
||||
// 如果这个GEP指令已经创建过节点,则跳过
|
||||
if(value_to_node.count(gep)) continue;
|
||||
|
||||
// 创建一个新的 GET_ELEMENT_PTR 类型的节点
|
||||
auto gep_node = create_node(DAGNode::GET_ELEMENT_PTR, gep, value_to_node, nodes_storage);
|
||||
|
||||
// 第一个操作数是基指针(即数组本身)
|
||||
gep_node->operands.push_back(get_operand_node(gep->getBasePointer(), value_to_node, nodes_storage));
|
||||
|
||||
// 依次添加所有索引作为后续的操作数
|
||||
for (auto index : gep->getIndices()) {
|
||||
// [修复] 从 Use 对象中获取真正的 Value*
|
||||
gep_node->operands.push_back(get_operand_node(index->getValue(), value_to_node, nodes_storage));
|
||||
}
|
||||
} else if (auto load = dynamic_cast<LoadInst*>(inst)) {
|
||||
auto load_node = create_node(DAGNode::LOAD, load, value_to_node, nodes_storage);
|
||||
load_node->operands.push_back(get_operand_node(load->getPointer(), value_to_node, nodes_storage));
|
||||
@ -1044,136 +632,4 @@ std::vector<std::unique_ptr<RISCv64ISel::DAGNode>> RISCv64ISel::build_dag(BasicB
|
||||
return nodes_storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 计算一个类型在内存中占用的字节数。
|
||||
* @param type 需要计算大小的IR类型。
|
||||
* @return 该类型占用的字节数。
|
||||
*/
|
||||
unsigned RISCv64ISel::getTypeSizeInBytes(Type* type) {
|
||||
if (!type) {
|
||||
assert(false && "Cannot get size of a null type.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (type->getKind()) {
|
||||
// 对于SysY语言,基本类型int和float都占用4字节
|
||||
case Type::kInt:
|
||||
case Type::kFloat:
|
||||
return 4;
|
||||
|
||||
// 指针类型在RISC-V 64位架构下占用8字节
|
||||
// 虽然SysY没有'int*'语法,但数组变量在IR层面本身就是指针类型
|
||||
case Type::kPointer:
|
||||
return 8;
|
||||
|
||||
// 数组类型的总大小 = 元素数量 * 单个元素的大小
|
||||
case Type::kArray: {
|
||||
auto arrayType = type->as<ArrayType>();
|
||||
// 递归调用以计算元素大小
|
||||
return arrayType->getNumElements() * getTypeSizeInBytes(arrayType->getElementType());
|
||||
}
|
||||
|
||||
// 其他类型,如Void, Label等不占用栈空间,或者不应该出现在这里
|
||||
default:
|
||||
// 如果遇到未处理的类型,触发断言,方便调试
|
||||
assert(false && "Unsupported type for size calculation.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// [新] 打印DAG图以供调试的辅助函数
|
||||
void RISCv64ISel::print_dag(const std::vector<std::unique_ptr<DAGNode>>& dag, const std::string& bb_name) {
|
||||
// 检查是否有DEBUG宏或者全局变量,避免在非调试模式下打印
|
||||
// if (!DEBUG) return;
|
||||
|
||||
std::cerr << "=== DAG for Basic Block: " << bb_name << " ===\n";
|
||||
std::set<DAGNode*> visited;
|
||||
|
||||
// 为节点分配临时ID,方便阅读
|
||||
std::map<DAGNode*, int> node_to_id;
|
||||
int current_id = 0;
|
||||
for (const auto& node_ptr : dag) {
|
||||
node_to_id[node_ptr.get()] = current_id++;
|
||||
}
|
||||
|
||||
// 将NodeKind枚举转换为字符串的辅助函数
|
||||
auto get_kind_string = [](DAGNode::NodeKind kind) {
|
||||
switch (kind) {
|
||||
case DAGNode::ARGUMENT: return "ARGUMENT";
|
||||
case DAGNode::CONSTANT: return "CONSTANT";
|
||||
case DAGNode::LOAD: return "LOAD";
|
||||
case DAGNode::STORE: return "STORE";
|
||||
case DAGNode::BINARY: return "BINARY";
|
||||
case DAGNode::CALL: return "CALL";
|
||||
case DAGNode::RETURN: return "RETURN";
|
||||
case DAGNode::BRANCH: return "BRANCH";
|
||||
case DAGNode::ALLOCA_ADDR: return "ALLOCA_ADDR";
|
||||
case DAGNode::UNARY: return "UNARY";
|
||||
case DAGNode::MEMSET: return "MEMSET";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
};
|
||||
|
||||
// 递归打印节点的lambda表达式
|
||||
std::function<void(DAGNode*, int)> print_node =
|
||||
[&](DAGNode* node, int indent) {
|
||||
if (!node) return;
|
||||
|
||||
std::string current_indent(indent, ' ');
|
||||
int node_id = node_to_id.count(node) ? node_to_id[node] : -1;
|
||||
|
||||
std::cerr << current_indent << "Node#" << node_id << ": " << get_kind_string(node->kind);
|
||||
|
||||
// 尝试打印关联的虚拟寄存器
|
||||
if (node->value && vreg_map.count(node->value)) {
|
||||
std::cerr << " (vreg: %vreg" << vreg_map.at(node->value) << ")";
|
||||
}
|
||||
|
||||
// 打印关联的IR Value信息
|
||||
if (node->value) {
|
||||
std::cerr << " [";
|
||||
if (auto inst = dynamic_cast<Instruction*>(node->value)) {
|
||||
std::cerr << inst->getKindString();
|
||||
if (!inst->getName().empty()) {
|
||||
std::cerr << "(" << inst->getName() << ")";
|
||||
}
|
||||
} else if (auto constant = dynamic_cast<ConstantValue*>(node->value)) {
|
||||
std::cerr << "Const(" << constant->getInt() << ")";
|
||||
} else if (auto global = dynamic_cast<GlobalValue*>(node->value)) {
|
||||
std::cerr << "Global(" << global->getName() << ")";
|
||||
} else if (auto alloca = dynamic_cast<AllocaInst*>(node->value)) {
|
||||
std::cerr << "Alloca(" << alloca->getName() << ")";
|
||||
}
|
||||
std::cerr << "]";
|
||||
}
|
||||
std::cerr << "\n";
|
||||
|
||||
if (visited.count(node)) {
|
||||
std::cerr << current_indent << " (已打印过子节点)\n";
|
||||
return;
|
||||
}
|
||||
visited.insert(node);
|
||||
|
||||
if (!node->operands.empty()) {
|
||||
std::cerr << current_indent << " Operands:\n";
|
||||
for (auto operand : node->operands) {
|
||||
print_node(operand, indent + 4);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 从根节点(没有用户的节点,或有副作用的节点)开始打印
|
||||
for (const auto& node_ptr : dag) {
|
||||
if (node_ptr->users.empty() ||
|
||||
node_ptr->kind == DAGNode::STORE ||
|
||||
node_ptr->kind == DAGNode::RETURN ||
|
||||
node_ptr->kind == DAGNode::BRANCH ||
|
||||
node_ptr->kind == DAGNode::MEMSET)
|
||||
{
|
||||
print_node(node_ptr.get(), 0);
|
||||
}
|
||||
}
|
||||
std::cerr << "======================================\n\n";
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
@ -1,6 +0,0 @@
|
||||
#include "RISCv64LLIR.h"
|
||||
#include <vector>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
}
|
||||
@ -1,153 +1,8 @@
|
||||
// RISCv64Passes.cpp
|
||||
#include "RISCv64Passes.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// --- 寄存器分配前优化 ---
|
||||
|
||||
void PreRA_Scheduler::runOnMachineFunction(MachineFunction* mfunc) {
|
||||
// TODO: 在此实现寄存器分配前的指令调度。
|
||||
// 遍历mfunc中的每一个MachineBasicBlock。
|
||||
// 对每个基本块内的MachineInstr列表进行重排。
|
||||
//
|
||||
// 实现思路:
|
||||
// 1. 分析每个基本块内指令的数据依赖关系,构建依赖图(DAG)。
|
||||
// 2. 根据目标处理器的流水线特性(指令延迟等),使用列表调度等算法对指令进行重排。
|
||||
// 3. 此时操作的是虚拟寄存器,只存在真依赖,调度自由度最大。
|
||||
//
|
||||
// std::cout << "Running Pre-RA Instruction Scheduler..." << std::endl;
|
||||
}
|
||||
|
||||
|
||||
// --- 寄存器分配后优化 ---
|
||||
|
||||
void PeepholeOptimizer::runOnMachineFunction(MachineFunction* mfunc) {
|
||||
// TODO: 在此实现窥孔优化。
|
||||
// 遍历mfunc中的每一个MachineBasicBlock。
|
||||
// 对每个基本块内的MachineInstr列表进行扫描和替换。
|
||||
//
|
||||
// 实现思路:
|
||||
// 1. 维护一个大小固定(例如3-5条指令)的滑动窗口。
|
||||
// 2. 识别特定的冗余模式,例如:
|
||||
// - `mv a0, a1` 后紧跟 `mv a1, a0` (可消除的交换)
|
||||
// - `sw t0, 12(s0)` 后紧跟 `lw t1, 12(s0)` (冗余加载)
|
||||
// - 强度削减: `mul x, x, 2` -> `slli x, x, 1`
|
||||
// 3. 识别后,直接修改MachineInstr列表(删除、替换或插入指令)。
|
||||
//
|
||||
// std::cout << "Running Post-RA Peephole Optimizer..." << std::endl;
|
||||
}
|
||||
|
||||
void PostRA_Scheduler::runOnMachineFunction(MachineFunction* mfunc) {
|
||||
// TODO: 在此实现寄存器分配后的局部指令调度。
|
||||
// 遍历mfunc中的每一个MachineBasicBlock。
|
||||
// 重点关注由寄存器分配器插入的spill/fill代码。
|
||||
//
|
||||
// 实现思路:
|
||||
// 1. 识别出用于spill/fill的lw/sw指令。
|
||||
// 2. 在不违反数据依赖(包括物理寄存器引入的伪依赖)的前提下,
|
||||
// 尝试将lw指令向上移动,使其与使用它的指令之间有足够的距离,以隐藏访存延迟。
|
||||
// 3. 同样,可以尝试将sw指令向下移动。
|
||||
//
|
||||
// std::cout << "Running Post-RA Local Scheduler..." << std::endl;
|
||||
}
|
||||
|
||||
void CalleeSavedHandler::runOnMachineFunction(MachineFunction* mfunc) {
|
||||
// 【最终方案】: 此 Pass 负责分析、分配栈空间并插入 callee-saved 寄存器的保存/恢复指令。
|
||||
// 它通过与 FrameInfo 协作,确保为 callee-saved 寄存器分配的空间与局部变量/溢出槽的空间不冲突。
|
||||
// 这样做可以使生成的 sd/ld 指令能被后续的优化 Pass (如 PostRA-Scheduler) 处理。
|
||||
|
||||
StackFrameInfo& frame_info = mfunc->getFrameInfo();
|
||||
std::set<PhysicalReg> used_callee_saved;
|
||||
|
||||
// 1. 扫描所有指令,找出被使用的s寄存器 (s1-s11)
|
||||
for (auto& mbb : mfunc->getBlocks()) {
|
||||
for (auto& instr : mbb->getInstructions()) {
|
||||
for (auto& op : instr->getOperands()) {
|
||||
auto check_and_insert_reg = [&](RegOperand* reg_op) {
|
||||
if (!reg_op->isVirtual()) {
|
||||
PhysicalReg preg = reg_op->getPReg();
|
||||
if (preg >= PhysicalReg::S1 && preg <= PhysicalReg::S11) {
|
||||
used_callee_saved.insert(preg);
|
||||
}
|
||||
}
|
||||
};
|
||||
if (op->getKind() == MachineOperand::KIND_REG) {
|
||||
check_and_insert_reg(static_cast<RegOperand*>(op.get()));
|
||||
} else if (op->getKind() == MachineOperand::KIND_MEM) {
|
||||
check_and_insert_reg(static_cast<MemOperand*>(op.get())->getBase());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (used_callee_saved.empty()) {
|
||||
frame_info.callee_saved_size = 0; // 确保大小被初始化
|
||||
return; // 无需操作
|
||||
}
|
||||
|
||||
// 2. 计算为 callee-saved 寄存器分配的栈空间
|
||||
// 这里的关键是,偏移的基准点要在局部变量和溢出槽之下。
|
||||
int callee_saved_size = used_callee_saved.size() * 8;
|
||||
frame_info.callee_saved_size = callee_saved_size; // 将大小存入 FrameInfo
|
||||
|
||||
// 3. 计算无冲突的栈偏移
|
||||
// 栈向下增长,所以偏移是负数。
|
||||
// ra/s0 占用 -8 和 -16。局部变量和溢出区在它们之下。callee-saved 区在更下方。
|
||||
// 我们使用相对于 s0 的偏移。s0 将指向栈顶 (sp + total_size)。
|
||||
int base_offset = -16 - frame_info.locals_size - frame_info.spill_size;
|
||||
|
||||
// 为了栈帧布局确定性,对寄存器进行排序
|
||||
std::vector<PhysicalReg> sorted_regs(used_callee_saved.begin(), used_callee_saved.end());
|
||||
std::sort(sorted_regs.begin(), sorted_regs.end());
|
||||
|
||||
// 4. 在函数序言插入保存指令
|
||||
MachineBasicBlock* entry_block = mfunc->getBlocks().front().get();
|
||||
auto& entry_instrs = entry_block->getInstructions();
|
||||
auto prologue_end = entry_instrs.begin();
|
||||
|
||||
// 找到序言结束的位置(通常是addi s0, sp, size之后,但为了让优化器看到,我们插在更前面)
|
||||
// 合理的位置是在 IR 指令开始之前,即在任何非序言指令(如第一个标签)之前。
|
||||
// 为简单起见,我们直接插入到块的开头,后续重排 pass 会处理。
|
||||
// (更优的实现会寻找一个特定的插入点)
|
||||
|
||||
int current_offset = base_offset;
|
||||
for (PhysicalReg reg : sorted_regs) {
|
||||
auto sd = std::make_unique<MachineInstr>(RVOpcodes::SD);
|
||||
sd->addOperand(std::make_unique<RegOperand>(reg));
|
||||
sd->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(PhysicalReg::S0), // 基址为帧指针 s0
|
||||
std::make_unique<ImmOperand>(current_offset)
|
||||
));
|
||||
// 从头部插入,但要放在函数标签之后
|
||||
entry_instrs.insert(entry_instrs.begin() + 1, std::move(sd));
|
||||
current_offset -= 8;
|
||||
}
|
||||
|
||||
// 5. 【已修复】在函数结尾(ret之前)插入恢复指令,使用反向遍历来避免迭代器失效
|
||||
for (auto& mbb : mfunc->getBlocks()) {
|
||||
// 使用手动控制的反向循环
|
||||
for (auto it = mbb->getInstructions().end(); it != mbb->getInstructions().begin(); ) {
|
||||
// 在循环开始时就递减迭代器
|
||||
--it;
|
||||
|
||||
if ((*it)->getOpcode() == RVOpcodes::RET) {
|
||||
int current_offset_load = base_offset;
|
||||
// 以相同的顺序恢复(从 s1 开始)
|
||||
for (PhysicalReg reg : sorted_regs) {
|
||||
auto ld = std::make_unique<MachineInstr>(RVOpcodes::LD);
|
||||
ld->addOperand(std::make_unique<RegOperand>(reg));
|
||||
ld->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(PhysicalReg::S0),
|
||||
std::make_unique<ImmOperand>(current_offset_load)
|
||||
));
|
||||
// 在 'it' (即 RET 指令) 之前插入。
|
||||
// 因为我们是反向遍历,所以这不会影响下一次循环的 'it'。
|
||||
mbb->getInstructions().insert(it, std::move(ld));
|
||||
current_offset_load -= 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 此处为未来优化Pass的实现
|
||||
|
||||
} // namespace sysy
|
||||
@ -2,8 +2,6 @@
|
||||
#include "RISCv64ISel.h"
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <iostream> // For DEBUG output
|
||||
#include <cassert> // For assert
|
||||
|
||||
namespace sysy {
|
||||
|
||||
@ -13,337 +11,133 @@ RISCv64RegAlloc::RISCv64RegAlloc(MachineFunction* mfunc) : MFunc(mfunc) {
|
||||
PhysicalReg::T4, PhysicalReg::T5, PhysicalReg::T6,
|
||||
PhysicalReg::A0, PhysicalReg::A1, PhysicalReg::A2, PhysicalReg::A3,
|
||||
PhysicalReg::A4, PhysicalReg::A5, PhysicalReg::A6, PhysicalReg::A7,
|
||||
// PhysicalReg::S0,
|
||||
PhysicalReg::S1, PhysicalReg::S2, PhysicalReg::S3,
|
||||
PhysicalReg::S0, PhysicalReg::S1, PhysicalReg::S2, PhysicalReg::S3,
|
||||
PhysicalReg::S4, PhysicalReg::S5, PhysicalReg::S6, PhysicalReg::S7,
|
||||
PhysicalReg::S8, PhysicalReg::S9, PhysicalReg::S10, PhysicalReg::S11,
|
||||
};
|
||||
|
||||
// 映射物理寄存器到特殊的虚拟寄存器ID,用于干扰图中的物理寄存器节点
|
||||
// 确保这些特殊ID不会与vreg_counter生成的常规虚拟寄存器ID冲突
|
||||
for (PhysicalReg preg : allocable_int_regs) {
|
||||
preg_to_vreg_id_map[preg] = static_cast<unsigned>(PhysicalReg::PHYS_REG_START_ID) + static_cast<unsigned>(preg);
|
||||
}
|
||||
}
|
||||
|
||||
// 寄存器分配的主入口点
|
||||
void RISCv64RegAlloc::run() {
|
||||
// 阶段 1: 处理函数调用约定(参数寄存器预着色)
|
||||
handleCallingConvention();
|
||||
// 阶段 2: 消除帧索引(为局部变量和栈参数分配栈偏移)
|
||||
eliminateFrameIndices();
|
||||
// 阶段 3: 活跃性分析
|
||||
analyzeLiveness();
|
||||
// 阶段 4: 构建干扰图(包含CALL指令对调用者保存寄存器的影响)
|
||||
buildInterferenceGraph();
|
||||
// 阶段 5: 图着色算法分配物理寄存器
|
||||
colorGraph();
|
||||
// 阶段 6: 重写函数(插入溢出/填充代码,替换虚拟寄存器为物理寄存器)
|
||||
rewriteFunction();
|
||||
eliminateFrameIndices();
|
||||
analyzeLiveness();
|
||||
buildInterferenceGraph();
|
||||
colorGraph();
|
||||
rewriteFunction();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 处理调用约定,预先为函数参数分配物理寄存器。
|
||||
*/
|
||||
void RISCv64RegAlloc::handleCallingConvention() {
|
||||
Function* F = MFunc->getFunc();
|
||||
RISCv64ISel* isel = MFunc->getISel();
|
||||
|
||||
// 获取函数的Argument对象列表
|
||||
if (F) {
|
||||
auto& args = F->getArguments();
|
||||
// RISC-V RV64G调用约定:前8个整型/指针参数通过 a0-a7 传递
|
||||
int arg_idx = 0;
|
||||
// 遍历 AllocaInst* 列表
|
||||
for (Argument* arg : args) {
|
||||
if (arg_idx >= 8) {
|
||||
break;
|
||||
}
|
||||
// 获取该 Argument 对象对应的虚拟寄存器ID
|
||||
// 通过 MachineFunction -> RISCv64ISel -> vreg_map 来获取
|
||||
const auto& vreg_map_from_isel = MFunc->getISel()->getVRegMap();
|
||||
assert(vreg_map_from_isel.count(arg) && "Argument not found in ISel's vreg_map!");
|
||||
// 1. 获取该 Argument 对象对应的虚拟寄存器
|
||||
unsigned vreg = isel->getVReg(arg);
|
||||
|
||||
// 2. 根据参数索引,确定对应的物理寄存器 (a0, a1, ...)
|
||||
auto preg = static_cast<PhysicalReg>(static_cast<int>(PhysicalReg::A0) + arg_idx);
|
||||
|
||||
// 3. 在 color_map 中,将 vreg "预着色" 为对应的物理寄存器
|
||||
color_map[vreg] = preg;
|
||||
|
||||
arg_idx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 消除帧索引,为局部变量和栈参数分配栈偏移量,并展开伪指令。
|
||||
*/
|
||||
void RISCv64RegAlloc::eliminateFrameIndices() {
|
||||
StackFrameInfo& frame_info = MFunc->getFrameInfo();
|
||||
// 初始偏移量,为保存ra和s0留出空间。
|
||||
// 假设序言是 addi sp, sp, -stack_size; sd ra, stack_size-8(sp); sd s0, stack_size-16(sp);
|
||||
int current_offset = 16;
|
||||
|
||||
int current_offset = 0;
|
||||
Function* F = MFunc->getFunc();
|
||||
RISCv64ISel* isel = MFunc->getISel();
|
||||
|
||||
// 在处理局部变量前,首先为栈参数计算偏移量。
|
||||
if (F) {
|
||||
int arg_idx = 0;
|
||||
for (Argument* arg : F->getArguments()) {
|
||||
// 我们只关心第8个索引及之后的参数(即第9个参数开始)
|
||||
if (arg_idx >= 8) {
|
||||
// 计算偏移量:第一个栈参数(idx=8)在0(s0),第二个(idx=9)在8(s0),以此类推。
|
||||
int offset = (arg_idx - 8) * 8;
|
||||
unsigned vreg = isel->getVReg(arg);
|
||||
|
||||
// 将这个vreg和它的栈偏移存入map。
|
||||
// 我们可以复用alloca_offsets,因为它们都代表“vreg到栈偏移”的映射。
|
||||
frame_info.alloca_offsets[vreg] = offset;
|
||||
}
|
||||
arg_idx++;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理局部变量
|
||||
// 遍历AllocaInst来计算局部变量所需的总空间
|
||||
for (auto& bb : F->getBasicBlocks()) {
|
||||
for (auto& inst : bb->getInstructions()) {
|
||||
if (auto alloca = dynamic_cast<AllocaInst*>(inst.get())) {
|
||||
// 获取Alloca指令指向的类型 (例如 alloca i32* 中,获取 i32)
|
||||
Type* allocated_type = alloca->getType()->as<PointerType>()->getBaseType();
|
||||
int size = getTypeSizeInBytes(allocated_type);
|
||||
|
||||
// RISC-V要求栈地址8字节对齐
|
||||
size = (size + 7) & ~7;
|
||||
if (size == 0) size = 8; // 至少分配8字节
|
||||
|
||||
int size = 4;
|
||||
if (!alloca->getDims().empty()) {
|
||||
int num_elements = 1;
|
||||
for (const auto& dim_use : alloca->getDims()) {
|
||||
if (auto const_dim = dynamic_cast<ConstantValue*>(dim_use->getValue())) {
|
||||
num_elements *= const_dim->getInt();
|
||||
}
|
||||
}
|
||||
size *= num_elements;
|
||||
}
|
||||
current_offset += size;
|
||||
unsigned alloca_vreg = isel->getVReg(alloca);
|
||||
// 局部变量使用相对于s0的负向偏移
|
||||
frame_info.alloca_offsets[alloca_vreg] = -current_offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
frame_info.locals_size = current_offset;
|
||||
|
||||
// 遍历所有机器指令,将伪指令展开为真实指令
|
||||
for (auto& mbb : MFunc->getBlocks()) {
|
||||
std::vector<std::unique_ptr<MachineInstr>> new_instructions;
|
||||
for (auto& instr_ptr : mbb->getInstructions()) {
|
||||
RVOpcodes opcode = instr_ptr->getOpcode();
|
||||
|
||||
// --- MODIFICATION START: 处理区分宽度的伪指令 ---
|
||||
if (opcode == RVOpcodes::FRAME_LOAD_W || opcode == RVOpcodes::FRAME_LOAD_D) {
|
||||
// 确定要生成的真实加载指令是 lw 还是 ld
|
||||
RVOpcodes real_load_op = (opcode == RVOpcodes::FRAME_LOAD_W) ? RVOpcodes::LW : RVOpcodes::LD;
|
||||
|
||||
if (instr_ptr->getOpcode() == RVOpcodes::FRAME_LOAD) {
|
||||
auto& operands = instr_ptr->getOperands();
|
||||
unsigned dest_vreg = static_cast<RegOperand*>(operands[0].get())->getVRegNum();
|
||||
unsigned alloca_vreg = static_cast<RegOperand*>(operands[1].get())->getVRegNum();
|
||||
int offset = frame_info.alloca_offsets.at(alloca_vreg);
|
||||
auto addr_vreg = isel->getNewVReg();
|
||||
|
||||
// 展开为: addi addr_vreg, s0, offset
|
||||
auto addi = std::make_unique<MachineInstr>(RVOpcodes::ADDI);
|
||||
addi->addOperand(std::make_unique<RegOperand>(addr_vreg));
|
||||
addi->addOperand(std::make_unique<RegOperand>(PhysicalReg::S0));
|
||||
addi->addOperand(std::make_unique<ImmOperand>(offset));
|
||||
new_instructions.push_back(std::move(addi));
|
||||
|
||||
// 展开为: lw/ld dest_vreg, 0(addr_vreg)
|
||||
auto load_instr = std::make_unique<MachineInstr>(real_load_op);
|
||||
load_instr->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
load_instr->addOperand(std::make_unique<MemOperand>(
|
||||
auto lw = std::make_unique<MachineInstr>(RVOpcodes::LW);
|
||||
lw->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
lw->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(addr_vreg),
|
||||
std::make_unique<ImmOperand>(0)));
|
||||
new_instructions.push_back(std::move(load_instr));
|
||||
|
||||
} else if (opcode == RVOpcodes::FRAME_STORE_W || opcode == RVOpcodes::FRAME_STORE_D) {
|
||||
// 确定要生成的真实存储指令是 sw 还是 sd
|
||||
RVOpcodes real_store_op = (opcode == RVOpcodes::FRAME_STORE_W) ? RVOpcodes::SW : RVOpcodes::SD;
|
||||
new_instructions.push_back(std::move(lw));
|
||||
|
||||
} else if (instr_ptr->getOpcode() == RVOpcodes::FRAME_STORE) {
|
||||
auto& operands = instr_ptr->getOperands();
|
||||
unsigned src_vreg = static_cast<RegOperand*>(operands[0].get())->getVRegNum();
|
||||
unsigned alloca_vreg = static_cast<RegOperand*>(operands[1].get())->getVRegNum();
|
||||
int offset = frame_info.alloca_offsets.at(alloca_vreg);
|
||||
auto addr_vreg = isel->getNewVReg();
|
||||
|
||||
// 展开为: addi addr_vreg, s0, offset
|
||||
auto addi = std::make_unique<MachineInstr>(RVOpcodes::ADDI);
|
||||
addi->addOperand(std::make_unique<RegOperand>(addr_vreg));
|
||||
addi->addOperand(std::make_unique<RegOperand>(PhysicalReg::S0));
|
||||
addi->addOperand(std::make_unique<ImmOperand>(offset));
|
||||
new_instructions.push_back(std::move(addi));
|
||||
|
||||
// 展开为: sw/sd src_vreg, 0(addr_vreg)
|
||||
auto store_instr = std::make_unique<MachineInstr>(real_store_op);
|
||||
store_instr->addOperand(std::make_unique<RegOperand>(src_vreg));
|
||||
store_instr->addOperand(std::make_unique<MemOperand>(
|
||||
auto sw = std::make_unique<MachineInstr>(RVOpcodes::SW);
|
||||
sw->addOperand(std::make_unique<RegOperand>(src_vreg));
|
||||
sw->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(addr_vreg),
|
||||
std::make_unique<ImmOperand>(0)));
|
||||
new_instructions.push_back(std::move(store_instr));
|
||||
|
||||
} else if (instr_ptr->getOpcode() == RVOpcodes::FRAME_ADDR) {
|
||||
auto& operands = instr_ptr->getOperands();
|
||||
unsigned dest_vreg = static_cast<RegOperand*>(operands[0].get())->getVRegNum();
|
||||
unsigned alloca_vreg = static_cast<RegOperand*>(operands[1].get())->getVRegNum();
|
||||
int offset = frame_info.alloca_offsets.at(alloca_vreg);
|
||||
|
||||
// 将 `frame_addr rd, rs` 展开为 `addi rd, s0, offset`
|
||||
auto addi = std::make_unique<MachineInstr>(RVOpcodes::ADDI);
|
||||
addi->addOperand(std::make_unique<RegOperand>(dest_vreg));
|
||||
addi->addOperand(std::make_unique<RegOperand>(PhysicalReg::S0));
|
||||
addi->addOperand(std::make_unique<ImmOperand>(offset));
|
||||
new_instructions.push_back(std::move(addi));
|
||||
new_instructions.push_back(std::move(sw));
|
||||
} else {
|
||||
new_instructions.push_back(std::move(instr_ptr));
|
||||
}
|
||||
// --- MODIFICATION END ---
|
||||
}
|
||||
mbb->getInstructions() = std::move(new_instructions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 计算给定 MachineInstr 的 Use (读取) 和 Def (写入) 寄存器集合。
|
||||
* 这是活跃性分析的基础。
|
||||
* @param instr 要分析的机器指令。
|
||||
* @param use 存储 Use 寄存器(虚拟寄存器 ID)的集合。
|
||||
* @param def 存储 Def 寄存器(虚拟寄存器 ID)的集合。
|
||||
*/
|
||||
void RISCv64RegAlloc::getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet& def) {
|
||||
bool first_reg_is_def = true; // 默认情况下,指令的第一个寄存器操作数是定义 (def)
|
||||
bool is_def = true;
|
||||
auto opcode = instr->getOpcode();
|
||||
|
||||
// 1. 特殊指令的 `is_def` 标志调整
|
||||
// 这些指令的第一个寄存器操作数是源操作数 (use),而不是目标操作数 (def)。
|
||||
// 预定义def和use规则
|
||||
if (opcode == RVOpcodes::SW || opcode == RVOpcodes::SD ||
|
||||
opcode == RVOpcodes::BEQ || opcode == RVOpcodes::BNE ||
|
||||
opcode == RVOpcodes::BLT || opcode == RVOpcodes::BGE ||
|
||||
opcode == RVOpcodes::BLTU || opcode == RVOpcodes::BGEU ||
|
||||
opcode == RVOpcodes::RET || opcode == RVOpcodes::J) {
|
||||
first_reg_is_def = false;
|
||||
is_def = false;
|
||||
}
|
||||
|
||||
// JAL 和 JALR 指令定义 ra (x1)
|
||||
if (opcode == RVOpcodes::JAL || opcode == RVOpcodes::JALR) {
|
||||
// 使用 ra 对应的特殊虚拟寄存器ID
|
||||
def.insert(static_cast<unsigned>(PhysicalReg::RA));
|
||||
first_reg_is_def = false; // JAL/JALR 的第一个操作数是 ra,已经处理为 def
|
||||
}
|
||||
|
||||
// 2. CALL 指令的特殊处理
|
||||
if (opcode == RVOpcodes::CALL) {
|
||||
// 1.1 处理返回值 (def)
|
||||
// 约定:如果CALL指令有返回值,IR阶段会将返回值vreg作为指令的第一个操作数。
|
||||
if (!instr->getOperands().empty() && instr->getOperands().front()->getKind() == MachineOperand::KIND_REG) {
|
||||
auto reg_op = static_cast<RegOperand*>(instr->getOperands().front().get());
|
||||
if (reg_op->isVirtual()) {
|
||||
def.insert(reg_op->getVRegNum());
|
||||
}
|
||||
}
|
||||
|
||||
// 1.2 处理参数 (use)
|
||||
// 参数通常是指令的后续操作数
|
||||
bool first_operand_processed = false; // 用于跳过已作为def处理的返回值
|
||||
for (const auto& op : instr->getOperands()) {
|
||||
if (op->getKind() == MachineOperand::KIND_REG) {
|
||||
if (!first_operand_processed) { // 如果是第一个操作数
|
||||
first_operand_processed = true;
|
||||
// 如果第一个操作数是返回值(已被加入def),则跳过
|
||||
if (def.count(static_cast<RegOperand*>(op.get())->getVRegNum())) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// 否则,该寄存器是 use
|
||||
auto reg_op = static_cast<RegOperand*>(op.get());
|
||||
if (reg_op->isVirtual()) {
|
||||
use.insert(reg_op->getVRegNum());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// **重要**: CALL指令隐式定义(杀死)了所有调用者保存的寄存器。
|
||||
// **这部分逻辑不在getInstrUseDef中直接处理**。
|
||||
// 而是通过`buildInterferenceGraph`中添加物理寄存器节点与活跃虚拟寄存器之间的干扰边来完成。
|
||||
// 这样 Liveness Analysis 可以在虚拟寄存器层面进行,而物理寄存器干扰的复杂性则留给干扰图。
|
||||
|
||||
return; // CALL 指令处理完毕,直接返回
|
||||
// CALL会杀死所有调用者保存寄存器,这是一个简化处理
|
||||
// 同时也使用了传入a0-a7的参数
|
||||
}
|
||||
|
||||
// 3. 对其他所有指令的通用处理逻辑
|
||||
for (const auto& op : instr->getOperands()) {
|
||||
if (op->getKind() == MachineOperand::KIND_REG) {
|
||||
auto reg_op = static_cast<RegOperand*>(op.get());
|
||||
if (reg_op->isVirtual()) { // 只有虚拟寄存器才需要处理 Use/Def
|
||||
// 如果是第一个寄存器操作数,且指令类型表明它是定义 (def),则加入 def 集合
|
||||
// 否则,它是 use (读取)
|
||||
if (first_reg_is_def) {
|
||||
if (reg_op->isVirtual()) {
|
||||
if (is_def) {
|
||||
def.insert(reg_op->getVRegNum());
|
||||
first_reg_is_def = false; // 确保每条指令只定义一个目标寄存器
|
||||
is_def = false;
|
||||
} else {
|
||||
use.insert(reg_op->getVRegNum());
|
||||
}
|
||||
}
|
||||
} else if (op->getKind() == MachineOperand::KIND_MEM) {
|
||||
// 内存操作数 `offset(base)` 中的 `base` 寄存器是 `use`
|
||||
auto mem_op = static_cast<MemOperand*>(op.get());
|
||||
if (mem_op->getBase()->isVirtual()) {
|
||||
use.insert(mem_op->getBase()->getVRegNum());
|
||||
}
|
||||
// 对于存储内存指令 (SW, SD),要存储的值(第一个操作数)也是 `use`
|
||||
if ((opcode == RVOpcodes::SW || opcode == RVOpcodes::SD) &&
|
||||
!instr->getOperands().empty() && // 确保有操作数
|
||||
instr->getOperands().front()->getKind() == MachineOperand::KIND_REG) { // 且第一个操作数是寄存器
|
||||
auto src_reg_op = static_cast<RegOperand*>(instr->getOperands().front().get());
|
||||
if (src_reg_op->isVirtual()) {
|
||||
use.insert(src_reg_op->getVRegNum());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 计算一个类型在内存中占用的字节数。
|
||||
* @param type 需要计算大小的IR类型。
|
||||
* @return 该类型占用的字节数。
|
||||
*/
|
||||
unsigned RISCv64RegAlloc::getTypeSizeInBytes(Type* type) {
|
||||
if (!type) {
|
||||
assert(false && "Cannot get size of a null type.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (type->getKind()) {
|
||||
// 对于SysY语言,基本类型int和float都占用4字节
|
||||
case Type::kInt:
|
||||
case Type::kFloat:
|
||||
return 4;
|
||||
|
||||
// 指针类型在RISC-V 64位架构下占用8字节
|
||||
// 虽然SysY没有'int*'语法,但数组变量在IR层面本身就是指针类型
|
||||
case Type::kPointer:
|
||||
return 8;
|
||||
|
||||
// 数组类型的总大小 = 元素数量 * 单个元素的大小
|
||||
case Type::kArray: {
|
||||
auto arrayType = type->as<ArrayType>();
|
||||
// 递归调用以计算元素大小
|
||||
return arrayType->getNumElements() * getTypeSizeInBytes(arrayType->getElementType());
|
||||
}
|
||||
|
||||
// 其他类型,如Void, Label等不占用栈空间,或者不应该出现在这里
|
||||
default:
|
||||
// 如果遇到未处理的类型,触发断言,方便调试
|
||||
assert(false && "Unsupported type for size calculation.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void RISCv64RegAlloc::analyzeLiveness() {
|
||||
bool changed = true;
|
||||
while (changed) {
|
||||
@ -388,7 +182,6 @@ void RISCv64RegAlloc::analyzeLiveness() {
|
||||
|
||||
void RISCv64RegAlloc::buildInterferenceGraph() {
|
||||
std::set<unsigned> all_vregs;
|
||||
// 收集所有虚拟寄存器和物理寄存器在干扰图中的节点ID
|
||||
for (auto& mbb : MFunc->getBlocks()) {
|
||||
for(auto& instr : mbb->getInstructions()) {
|
||||
LiveSet use, def;
|
||||
@ -397,11 +190,6 @@ void RISCv64RegAlloc::buildInterferenceGraph() {
|
||||
for(auto d : def) all_vregs.insert(d);
|
||||
}
|
||||
}
|
||||
// 添加所有物理寄存器对应的特殊虚拟寄存器ID到all_vregs,作为干扰图节点
|
||||
for (auto preg : allocable_int_regs) {
|
||||
all_vregs.insert(preg_to_vreg_id_map.at(preg));
|
||||
}
|
||||
|
||||
|
||||
for (auto vreg : all_vregs) { interference_graph[vreg] = {}; }
|
||||
|
||||
@ -411,7 +199,6 @@ void RISCv64RegAlloc::buildInterferenceGraph() {
|
||||
getInstrUseDef(instr.get(), use, def);
|
||||
const LiveSet& live_out = live_out_map.at(instr.get());
|
||||
|
||||
// 标准干扰图构建:def 与 live_out 中的其他变量干扰
|
||||
for (unsigned d : def) {
|
||||
for (unsigned l : live_out) {
|
||||
if (d != l) {
|
||||
@ -420,24 +207,6 @@ void RISCv64RegAlloc::buildInterferenceGraph() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// *** 核心修改点:处理 CALL 指令的隐式 def ***
|
||||
if (instr->getOpcode() == RVOpcodes::CALL) {
|
||||
// CALL 指令会定义(杀死)所有调用者保存的寄存器。
|
||||
// 因此,所有调用者保存的物理寄存器都与 CALL 指令的 live_out 中的所有变量冲突。
|
||||
const std::vector<PhysicalReg>& caller_saved_regs = getCallerSavedIntRegs();
|
||||
for (PhysicalReg cs_reg : caller_saved_regs) {
|
||||
unsigned cs_vreg_id = preg_to_vreg_id_map.at(cs_reg); // 获取物理寄存器对应的特殊vreg ID
|
||||
|
||||
// 将这个物理寄存器节点与 CALL 指令的 live_out 中的所有虚拟寄存器添加干扰边。
|
||||
for (unsigned live_vreg_out : live_out) {
|
||||
if (cs_vreg_id != live_vreg_out) { // 避免自己和自己干扰
|
||||
interference_graph[cs_vreg_id].insert(live_vreg_out);
|
||||
interference_graph[live_vreg_out].insert(cs_vreg_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -445,40 +214,19 @@ void RISCv64RegAlloc::buildInterferenceGraph() {
|
||||
void RISCv64RegAlloc::colorGraph() {
|
||||
std::vector<unsigned> sorted_vregs;
|
||||
for (auto const& [vreg, neighbors] : interference_graph) {
|
||||
if (color_map.find(vreg) == color_map.end()) {
|
||||
sorted_vregs.push_back(vreg);
|
||||
}
|
||||
sorted_vregs.push_back(vreg);
|
||||
}
|
||||
|
||||
// 排序
|
||||
std::sort(sorted_vregs.begin(), sorted_vregs.end(), [&](unsigned a, unsigned b) {
|
||||
return interference_graph[a].size() > interference_graph[b].size();
|
||||
});
|
||||
|
||||
// 着色
|
||||
for (unsigned vreg : sorted_vregs) {
|
||||
std::set<PhysicalReg> used_colors;
|
||||
for (unsigned neighbor_id : interference_graph.at(vreg)) {
|
||||
// --- 修改开始 ---
|
||||
|
||||
// 情况 1: 邻居是一个已经被着色的虚拟寄存器
|
||||
if (color_map.count(neighbor_id)) {
|
||||
used_colors.insert(color_map.at(neighbor_id));
|
||||
}
|
||||
// 情况 2: 邻居本身就是一个代表物理寄存器的节点
|
||||
else if (neighbor_id >= static_cast<unsigned>(PhysicalReg::PHYS_REG_START_ID)) {
|
||||
// 需要一个反向映射来从特殊ID找回PhysicalReg
|
||||
// 假设你有这样一个映射 inv_preg_to_vreg_id_map
|
||||
// 或者,你可以重新计算
|
||||
for (auto const& [preg, id] : preg_to_vreg_id_map) {
|
||||
if (id == neighbor_id) {
|
||||
used_colors.insert(preg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (unsigned neighbor : interference_graph.at(vreg)) {
|
||||
if (color_map.count(neighbor)) {
|
||||
used_colors.insert(color_map.at(neighbor));
|
||||
}
|
||||
|
||||
// --- 修改结束 ---
|
||||
}
|
||||
|
||||
bool colored = false;
|
||||
@ -498,21 +246,8 @@ void RISCv64RegAlloc::colorGraph() {
|
||||
void RISCv64RegAlloc::rewriteFunction() {
|
||||
StackFrameInfo& frame_info = MFunc->getFrameInfo();
|
||||
int current_offset = frame_info.locals_size;
|
||||
|
||||
// --- FIX 1: 动态计算溢出槽大小 ---
|
||||
// 根据溢出虚拟寄存器的真实类型,为其在栈上分配正确大小的空间。
|
||||
for (unsigned vreg : spilled_vregs) {
|
||||
// 从反向映射中查找 vreg 对应的 IR Value
|
||||
assert(vreg_to_value_map.count(vreg) && "Spilled vreg not found in map!");
|
||||
Value* val = vreg_to_value_map.at(vreg);
|
||||
|
||||
// 使用辅助函数获取类型大小
|
||||
int size = getTypeSizeInBytes(val->getType());
|
||||
|
||||
// 保持栈8字节对齐
|
||||
current_offset += size;
|
||||
current_offset = (current_offset + 7) & ~7;
|
||||
|
||||
current_offset += 4;
|
||||
frame_info.spill_offsets[vreg] = -current_offset;
|
||||
}
|
||||
frame_info.spill_size = current_offset - frame_info.locals_size;
|
||||
@ -523,16 +258,10 @@ void RISCv64RegAlloc::rewriteFunction() {
|
||||
LiveSet use, def;
|
||||
getInstrUseDef(instr_ptr.get(), use, def);
|
||||
|
||||
// --- FIX 2: 为溢出的 'use' 操作数插入正确的加载指令 ---
|
||||
for (unsigned vreg : use) {
|
||||
if (spilled_vregs.count(vreg)) {
|
||||
// 同样地,根据 vreg 的类型决定使用 lw 还是 ld
|
||||
assert(vreg_to_value_map.count(vreg));
|
||||
Value* val = vreg_to_value_map.at(vreg);
|
||||
RVOpcodes load_op = val->getType()->isPointer() ? RVOpcodes::LD : RVOpcodes::LW;
|
||||
|
||||
int offset = frame_info.spill_offsets.at(vreg);
|
||||
auto load = std::make_unique<MachineInstr>(load_op);
|
||||
auto load = std::make_unique<MachineInstr>(RVOpcodes::LW);
|
||||
load->addOperand(std::make_unique<RegOperand>(vreg));
|
||||
load->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(PhysicalReg::S0),
|
||||
@ -544,16 +273,10 @@ void RISCv64RegAlloc::rewriteFunction() {
|
||||
|
||||
new_instructions.push_back(std::move(instr_ptr));
|
||||
|
||||
// --- FIX 3: 为溢出的 'def' 操作数插入正确的存储指令 ---
|
||||
for (unsigned vreg : def) {
|
||||
if (spilled_vregs.count(vreg)) {
|
||||
// 根据 vreg 的类型决定使用 sw 还是 sd
|
||||
assert(vreg_to_value_map.count(vreg));
|
||||
Value* val = vreg_to_value_map.at(vreg);
|
||||
RVOpcodes store_op = val->getType()->isPointer() ? RVOpcodes::SD : RVOpcodes::SW;
|
||||
|
||||
int offset = frame_info.spill_offsets.at(vreg);
|
||||
auto store = std::make_unique<MachineInstr>(store_op);
|
||||
auto store = std::make_unique<MachineInstr>(RVOpcodes::SW);
|
||||
store->addOperand(std::make_unique<RegOperand>(vreg));
|
||||
store->addOperand(std::make_unique<MemOperand>(
|
||||
std::make_unique<RegOperand>(PhysicalReg::S0),
|
||||
@ -566,43 +289,29 @@ void RISCv64RegAlloc::rewriteFunction() {
|
||||
mbb->getInstructions() = std::move(new_instructions);
|
||||
}
|
||||
|
||||
// 最后的虚拟寄存器到物理寄存器的替换过程保持不变
|
||||
for (auto& mbb : MFunc->getBlocks()) {
|
||||
for (auto& instr_ptr : mbb->getInstructions()) {
|
||||
for (auto& op_ptr : instr_ptr->getOperands()) {
|
||||
|
||||
// 情况一:操作数本身就是一个寄存器 (例如 add rd, rs1, rs2 中的所有操作数)
|
||||
if(op_ptr->getKind() == MachineOperand::KIND_REG) {
|
||||
auto reg_op = static_cast<RegOperand*>(op_ptr.get());
|
||||
if (reg_op->isVirtual()) {
|
||||
unsigned vreg = reg_op->getVRegNum();
|
||||
if (color_map.count(vreg)) {
|
||||
PhysicalReg preg = color_map.at(vreg);
|
||||
reg_op->setPReg(preg);
|
||||
reg_op->setPReg(color_map.at(vreg));
|
||||
} else if (spilled_vregs.count(vreg)) {
|
||||
// 如果vreg被溢出,替换为专用的溢出物理寄存器t6
|
||||
reg_op->setPReg(PhysicalReg::T6);
|
||||
reg_op->setPReg(PhysicalReg::T6); // 溢出统一用t6
|
||||
}
|
||||
}
|
||||
}
|
||||
// 情况二:操作数是一个内存地址 (例如 lw rd, offset(rs1) 中的 offset(rs1))
|
||||
else if (op_ptr->getKind() == MachineOperand::KIND_MEM) {
|
||||
} else if (op_ptr->getKind() == MachineOperand::KIND_MEM) {
|
||||
auto mem_op = static_cast<MemOperand*>(op_ptr.get());
|
||||
// 获取内存操作数内部的“基址寄存器”
|
||||
auto base_reg_op = mem_op->getBase();
|
||||
|
||||
// 对这个基址寄存器,执行与情况一完全相同的替换逻辑
|
||||
if(base_reg_op->isVirtual()){
|
||||
unsigned vreg = base_reg_op->getVRegNum();
|
||||
if(color_map.count(vreg)) {
|
||||
// 如果基址vreg被成功着色,替换
|
||||
PhysicalReg preg = color_map.at(vreg);
|
||||
base_reg_op->setPReg(preg);
|
||||
|
||||
} else if (spilled_vregs.count(vreg)) {
|
||||
// 如果基址vreg被溢出,替换为t6
|
||||
base_reg_op->setPReg(PhysicalReg::T6);
|
||||
}
|
||||
unsigned vreg = base_reg_op->getVRegNum();
|
||||
if(color_map.count(vreg)) {
|
||||
base_reg_op->setPReg(color_map.at(vreg));
|
||||
} else if (spilled_vregs.count(vreg)) {
|
||||
base_reg_op->setPReg(PhysicalReg::T6);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -523,6 +523,9 @@ bool ActiveVarAnalysis::analyze(Module *pModule, BasicBlock *block) {
|
||||
}
|
||||
|
||||
|
||||
auto ActiveVarAnalysis::getActiveTable() const -> const std::map<BasicBlock *, std::vector<std::set<User *>>> & {
|
||||
return activeTable;
|
||||
}
|
||||
|
||||
|
||||
} // namespace sysy
|
||||
|
||||
@ -1,600 +0,0 @@
|
||||
#include "SysYIRCFGOpt.h"
|
||||
#include "SysYIROptUtils.h"
|
||||
#include <cassert>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <queue> // 引入队列,SysYDelNoPreBLock需要
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 定义静态ID
|
||||
void *SysYDelInstAfterBrPass::ID = (void *)&SysYDelInstAfterBrPass::ID;
|
||||
void *SysYDelEmptyBlockPass::ID = (void *)&SysYDelEmptyBlockPass::ID;
|
||||
void *SysYDelNoPreBLockPass::ID = (void *)&SysYDelNoPreBLockPass::ID;
|
||||
void *SysYBlockMergePass::ID = (void *)&SysYBlockMergePass::ID;
|
||||
void *SysYAddReturnPass::ID = (void *)&SysYAddReturnPass::ID;
|
||||
void *SysYCondBr2BrPass::ID = (void *)&SysYCondBr2BrPass::ID;
|
||||
|
||||
|
||||
// ======================================================================
|
||||
// SysYCFGOptUtils: 辅助工具类,包含实际的CFG优化逻辑
|
||||
// ======================================================================
|
||||
|
||||
// 删除br后的无用指令
|
||||
bool SysYCFGOptUtils::SysYDelInstAfterBr(Function *func) {
|
||||
bool changed = false;
|
||||
|
||||
auto basicBlocks = func->getBasicBlocks();
|
||||
for (auto &basicBlock : basicBlocks) {
|
||||
bool Branch = false;
|
||||
auto &instructions = basicBlock->getInstructions();
|
||||
auto Branchiter = instructions.end();
|
||||
for (auto iter = instructions.begin(); iter != instructions.end(); ++iter) {
|
||||
if ((*iter)->isTerminator()){
|
||||
Branch = true;
|
||||
Branchiter = iter;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (Branchiter != instructions.end()) ++Branchiter;
|
||||
while (Branchiter != instructions.end()) {
|
||||
changed = true;
|
||||
Branchiter = instructions.erase(Branchiter);
|
||||
}
|
||||
|
||||
if (Branch) { // 更新前驱后继关系
|
||||
auto thelastinstinst = basicBlock->getInstructions().end();
|
||||
--thelastinstinst;
|
||||
auto &Successors = basicBlock->getSuccessors();
|
||||
for (auto iterSucc = Successors.begin(); iterSucc != Successors.end();) {
|
||||
(*iterSucc)->removePredecessor(basicBlock.get());
|
||||
basicBlock->removeSuccessor(*iterSucc);
|
||||
}
|
||||
if (thelastinstinst->get()->isUnconditional()) {
|
||||
BasicBlock* branchBlock = dynamic_cast<BasicBlock *>(thelastinstinst->get()->getOperand(0));
|
||||
basicBlock->addSuccessor(branchBlock);
|
||||
branchBlock->addPredecessor(basicBlock.get());
|
||||
} else if (thelastinstinst->get()->isConditional()) {
|
||||
BasicBlock* thenBlock = dynamic_cast<BasicBlock *>(thelastinstinst->get()->getOperand(1));
|
||||
BasicBlock* elseBlock = dynamic_cast<BasicBlock *>(thelastinstinst->get()->getOperand(2));
|
||||
basicBlock->addSuccessor(thenBlock);
|
||||
basicBlock->addSuccessor(elseBlock);
|
||||
thenBlock->addPredecessor(basicBlock.get());
|
||||
elseBlock->addPredecessor(basicBlock.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// 合并基本块
|
||||
bool SysYCFGOptUtils::SysYBlockMerge(Function *func) {
|
||||
bool changed = false;
|
||||
|
||||
for (auto blockiter = func->getBasicBlocks().begin();
|
||||
blockiter != func->getBasicBlocks().end();) {
|
||||
if (blockiter->get()->getNumSuccessors() == 1) {
|
||||
// 如果当前块只有一个后继块
|
||||
// 且后继块只有一个前驱块
|
||||
// 则将当前块和后继块合并
|
||||
if (((blockiter->get())->getSuccessors()[0])->getNumPredecessors() == 1) {
|
||||
// std::cout << "merge block: " << blockiter->get()->getName() << std::endl;
|
||||
BasicBlock* block = blockiter->get();
|
||||
BasicBlock* nextBlock = blockiter->get()->getSuccessors()[0];
|
||||
// auto nextarguments = nextBlock->getArguments();
|
||||
// 删除br指令
|
||||
if (block->getNumInstructions() != 0) {
|
||||
auto thelastinstinst = block->end();
|
||||
(--thelastinstinst);
|
||||
if (thelastinstinst->get()->isUnconditional()) {
|
||||
SysYIROptUtils::usedelete(thelastinstinst->get());
|
||||
thelastinstinst = block->getInstructions().erase(thelastinstinst);
|
||||
} else if (thelastinstinst->get()->isConditional()) {
|
||||
// 如果是条件分支,判断条件是否相同,主要优化相同布尔表达式
|
||||
if (thelastinstinst->get()->getOperand(1)->getName() == thelastinstinst->get()->getOperand(1)->getName()) {
|
||||
SysYIROptUtils::usedelete(thelastinstinst->get());
|
||||
thelastinstinst = block->getInstructions().erase(thelastinstinst);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 将后继块的指令移动到当前块
|
||||
// 并将后继块的父指针改为当前块
|
||||
for (auto institer = nextBlock->begin(); institer != nextBlock->end();) {
|
||||
institer->get()->setParent(block);
|
||||
block->getInstructions().emplace_back(institer->release());
|
||||
institer = nextBlock->getInstructions().erase(institer);
|
||||
}
|
||||
// 更新前驱后继关系,类似树节点操作
|
||||
block->removeSuccessor(nextBlock);
|
||||
nextBlock->removePredecessor(block);
|
||||
std::list<BasicBlock *> succshoulddel;
|
||||
for (auto &succ : nextBlock->getSuccessors()) {
|
||||
block->addSuccessor(succ);
|
||||
succ->replacePredecessor(nextBlock, block);
|
||||
succshoulddel.push_back(succ);
|
||||
}
|
||||
for (auto del : succshoulddel) {
|
||||
nextBlock->removeSuccessor(del);
|
||||
}
|
||||
|
||||
func->removeBasicBlock(nextBlock);
|
||||
changed = true;
|
||||
|
||||
} else {
|
||||
blockiter++;
|
||||
}
|
||||
} else {
|
||||
blockiter++;
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// 删除无前驱块,兼容SSA后的处理
|
||||
bool SysYCFGOptUtils::SysYDelNoPreBLock(Function *func) {
|
||||
|
||||
bool changed = false;
|
||||
|
||||
for (auto &block : func->getBasicBlocks()) {
|
||||
block->setreachableFalse();
|
||||
}
|
||||
// 对函数基本块做一个拓扑排序,排查不可达基本块
|
||||
auto entryBlock = func->getEntryBlock();
|
||||
entryBlock->setreachableTrue();
|
||||
std::queue<BasicBlock *> blockqueue;
|
||||
blockqueue.push(entryBlock);
|
||||
while (!blockqueue.empty()) {
|
||||
auto block = blockqueue.front();
|
||||
blockqueue.pop();
|
||||
for (auto &succ : block->getSuccessors()) {
|
||||
if (!succ->getreachable()) {
|
||||
succ->setreachableTrue();
|
||||
blockqueue.push(succ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除不可达基本块指令
|
||||
for (auto blockIter = func->getBasicBlocks().begin(); blockIter != func->getBasicBlocks().end(); blockIter++) {
|
||||
if (!blockIter->get()->getreachable()) {
|
||||
for (auto instIter = blockIter->get()->getInstructions().begin();
|
||||
instIter != blockIter->get()->getInstructions().end();) {
|
||||
SysYIROptUtils::usedelete(instIter->get());
|
||||
instIter = blockIter->get()->getInstructions().erase(instIter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (auto blockIter = func->getBasicBlocks().begin(); blockIter != func->getBasicBlocks().end();) {
|
||||
if (!blockIter->get()->getreachable()) {
|
||||
for (auto succblock : blockIter->get()->getSuccessors()) {
|
||||
for (auto &phiinst : succblock->getInstructions()) {
|
||||
if (phiinst->getKind() != Instruction::kPhi) {
|
||||
break;
|
||||
}
|
||||
// 使用 delBlk 方法正确地删除对应于被删除基本块的传入值
|
||||
dynamic_cast<PhiInst *>(phiinst.get())->delBlk(blockIter->get());
|
||||
}
|
||||
}
|
||||
// 删除不可达基本块,注意迭代器不可达问题
|
||||
func->removeBasicBlock((blockIter++)->get());
|
||||
changed = true;
|
||||
} else {
|
||||
blockIter++;
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// 删除空块
|
||||
bool SysYCFGOptUtils::SysYDelEmptyBlock(Function *func, IRBuilder* pBuilder) {
|
||||
bool changed = false;
|
||||
|
||||
// 收集不可达基本块
|
||||
// 这里的不可达基本块是指没有实际指令的基本块
|
||||
// 当一个基本块没有实际指令例如只有phi指令和一个uncondbr指令时,也会被视作不可达
|
||||
auto basicBlocks = func->getBasicBlocks();
|
||||
std::map<sysy::BasicBlock *, BasicBlock *> EmptyBlocks;
|
||||
// 空块儿和后继的基本块的映射
|
||||
for (auto &basicBlock : basicBlocks) {
|
||||
if (basicBlock->getNumInstructions() == 0) {
|
||||
if (basicBlock->getNumSuccessors() == 1) {
|
||||
EmptyBlocks[basicBlock.get()] = basicBlock->getSuccessors().front();
|
||||
}
|
||||
}
|
||||
else{
|
||||
// 如果只有phi指令和一个uncondbr。(phi)*(uncondbr)?
|
||||
// 判断除了最后一个指令之外是不是只有phi指令
|
||||
bool onlyPhi = true;
|
||||
for (auto &inst : basicBlock->getInstructions()) {
|
||||
if (!inst->isPhi() && !inst->isUnconditional()) {
|
||||
onlyPhi = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(onlyPhi && basicBlock->getNumSuccessors() == 1) // 确保有后继且只有一个
|
||||
EmptyBlocks[basicBlock.get()] = basicBlock->getSuccessors().front();
|
||||
}
|
||||
}
|
||||
// 更新基本块信息,增加必要指令
|
||||
for (auto &basicBlock : basicBlocks) {
|
||||
// 把空块转换成只有跳转指令的不可达块 (这段逻辑在优化遍中可能需要调整,这里是原样保留)
|
||||
// 通常,DelEmptyBlock 应该在BlockMerge之后运行,如果存在完全空块,它会尝试填充一个Br指令。
|
||||
// 但是,它主要目的是重定向跳转。
|
||||
if (distance(basicBlock->begin(), basicBlock->end()) == 0) {
|
||||
if (basicBlock->getNumSuccessors() == 0) {
|
||||
continue;
|
||||
}
|
||||
if (basicBlock->getNumSuccessors() > 1) {
|
||||
// 如果一个空块有多个后继,说明CFG结构有问题或者需要特殊处理,这里简单assert
|
||||
assert(false && "Empty block with multiple successors found during SysYDelEmptyBlock");
|
||||
}
|
||||
// 这里的逻辑有点问题,如果一个块是空的,且只有一个后继,应该直接跳转到后继。
|
||||
// 如果这个块最终被删除了,那么其前驱也需要重定向。
|
||||
// 这个循环的目的是重定向现有的跳转指令,而不是创建新的。
|
||||
// 所以下面的逻辑才是核心。
|
||||
// pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||
// pBuilder->createUncondBrInst(basicBlock->getSuccessors()[0], {});
|
||||
continue;
|
||||
}
|
||||
|
||||
auto thelastinst = basicBlock->getInstructions().end();
|
||||
--thelastinst;
|
||||
|
||||
// 根据br指令传递的后继块信息,跳过空块链
|
||||
if (thelastinst->get()->isUnconditional()) {
|
||||
BasicBlock* OldBrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||
BasicBlock *thelastBlockOld = nullptr;
|
||||
// 如果空块链表为多个块
|
||||
while (EmptyBlocks.count(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0)))) {
|
||||
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||
thelastinst->get()->replaceOperand(0, EmptyBlocks[thelastBlockOld]);
|
||||
}
|
||||
|
||||
// 如果有重定向发生
|
||||
if (thelastBlockOld != nullptr) {
|
||||
basicBlock->removeSuccessor(OldBrBlock);
|
||||
OldBrBlock->removePredecessor(basicBlock.get());
|
||||
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0)));
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->addPredecessor(basicBlock.get());
|
||||
changed = true; // 标记IR被修改
|
||||
}
|
||||
|
||||
|
||||
if (thelastBlockOld != nullptr) {
|
||||
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getInstructions()) {
|
||||
if (InstInNew->isPhi()) {
|
||||
// 使用 delBlk 方法删除 oldBlock 对应的传入值
|
||||
dynamic_cast<PhiInst *>(InstInNew.get())->delBlk(thelastBlockOld);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else if (thelastinst->get()->getKind() == Instruction::kCondBr) {
|
||||
auto OldThenBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||
auto OldElseBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2));
|
||||
bool thenChanged = false;
|
||||
bool elseChanged = false;
|
||||
|
||||
|
||||
BasicBlock *thelastBlockOld = nullptr;
|
||||
while (EmptyBlocks.count(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1)))) {
|
||||
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||
thelastinst->get()->replaceOperand(
|
||||
1, EmptyBlocks[dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))]);
|
||||
thenChanged = true;
|
||||
}
|
||||
|
||||
if (thenChanged) {
|
||||
basicBlock->removeSuccessor(OldThenBlock);
|
||||
OldThenBlock->removePredecessor(basicBlock.get());
|
||||
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1)));
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))->addPredecessor(basicBlock.get());
|
||||
changed = true; // 标记IR被修改
|
||||
}
|
||||
|
||||
// 处理 then 和 else 分支合并的情况
|
||||
if (dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1)) ==
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))) {
|
||||
auto thebrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||
SysYIROptUtils::usedelete(thelastinst->get());
|
||||
thelastinst = basicBlock->getInstructions().erase(thelastinst);
|
||||
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||
pBuilder->createUncondBrInst(thebrBlock, {});
|
||||
changed = true; // 标记IR被修改
|
||||
continue;
|
||||
}
|
||||
|
||||
if (thelastBlockOld != nullptr) {
|
||||
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))->getInstructions()) {
|
||||
if (InstInNew->isPhi()) {
|
||||
// 使用 delBlk 方法删除 oldBlock 对应的传入值
|
||||
dynamic_cast<PhiInst *>(InstInNew.get())->delBlk(thelastBlockOld);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
thelastBlockOld = nullptr;
|
||||
while (EmptyBlocks.count(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2)))) {
|
||||
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2));
|
||||
thelastinst->get()->replaceOperand(
|
||||
2, EmptyBlocks[dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))]);
|
||||
elseChanged = true;
|
||||
}
|
||||
|
||||
if (elseChanged) {
|
||||
basicBlock->removeSuccessor(OldElseBlock);
|
||||
OldElseBlock->removePredecessor(basicBlock.get());
|
||||
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2)));
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))->addPredecessor(basicBlock.get());
|
||||
changed = true; // 标记IR被修改
|
||||
}
|
||||
|
||||
// 处理 then 和 else 分支合并的情况
|
||||
if (dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1)) ==
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))) {
|
||||
auto thebrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||
SysYIROptUtils::usedelete(thelastinst->get());
|
||||
thelastinst = basicBlock->getInstructions().erase(thelastinst);
|
||||
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||
pBuilder->createUncondBrInst(thebrBlock, {});
|
||||
changed = true; // 标记IR被修改
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// 如果有重定向发生
|
||||
// 需要更新后继块的前驱关系
|
||||
if (thelastBlockOld != nullptr) {
|
||||
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))->getInstructions()) {
|
||||
if (InstInNew->isPhi()) {
|
||||
// 使用 delBlk 方法删除 oldBlock 对应的传入值
|
||||
dynamic_cast<PhiInst *>(InstInNew.get())->delBlk(thelastBlockOld);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// 如果不是终止指令,但有后继 (例如,末尾没有显式终止指令的块)
|
||||
// 这段逻辑可能需要更严谨的CFG检查来确保正确性
|
||||
if (basicBlock->getNumSuccessors() == 1) {
|
||||
// 这里的逻辑似乎是想为没有terminator的块添加一个,但通常这应该在CFG构建阶段完成。
|
||||
// 如果这里仍然执行,确保它符合预期。
|
||||
// pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||
// pBuilder->createUncondBrInst(basicBlock->getSuccessors()[0], {});
|
||||
// auto thelastinst = basicBlock->getInstructions().end();
|
||||
// (--thelastinst);
|
||||
// auto OldBrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||
// sysy::BasicBlock *thelastBlockOld = nullptr;
|
||||
// while (EmptyBlocks.find(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))) !=
|
||||
// EmptyBlocks.end()) {
|
||||
// thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||
|
||||
// thelastinst->get()->replaceOperand(
|
||||
// 0, EmptyBlocks[dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))]);
|
||||
// }
|
||||
|
||||
// basicBlock->removeSuccessor(OldBrBlock);
|
||||
// OldBrBlock->removePredecessor(basicBlock.get());
|
||||
// basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0)));
|
||||
// dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->addPredecessor(basicBlock.get());
|
||||
// changed = true; // 标记IR被修改
|
||||
// if (thelastBlockOld != nullptr) {
|
||||
// int indexphi = 0;
|
||||
// for (auto &pred : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getPredecessors()) {
|
||||
// if (pred == thelastBlockOld) {
|
||||
// break;
|
||||
// }
|
||||
// indexphi++;
|
||||
// }
|
||||
|
||||
// for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getInstructions()) {
|
||||
// if (InstInNew->isPhi()) {
|
||||
// dynamic_cast<PhiInst *>(InstInNew.get())->removeOperand(indexphi + 1);
|
||||
// } else {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 真正的删除空块
|
||||
for (auto iter = func->getBasicBlocks().begin(); iter != func->getBasicBlocks().end();) {
|
||||
|
||||
if (EmptyBlocks.count(iter->get())) {
|
||||
// EntryBlock跳过
|
||||
if (iter->get() == func->getEntryBlock()) {
|
||||
++iter;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (auto instIter = iter->get()->getInstructions().begin();
|
||||
instIter != iter->get()->getInstructions().end();) {
|
||||
SysYIROptUtils::usedelete(instIter->get()); // 仅删除 use 关系
|
||||
// 显式地从基本块中删除指令并更新迭代器
|
||||
instIter = iter->get()->getInstructions().erase(instIter);
|
||||
}
|
||||
// 删除不可达基本块的phi指令的操作数
|
||||
for (auto &succ : iter->get()->getSuccessors()) {
|
||||
for (auto &instinsucc : succ->getInstructions()) {
|
||||
if (instinsucc->isPhi()) {
|
||||
// iter->get() 就是当前被删除的空基本块,它作为前驱连接到这里的Phi指令
|
||||
dynamic_cast<PhiInst *>(instinsucc.get())->delBlk(iter->get());
|
||||
} else {
|
||||
// Phi 指令通常在基本块的开头,如果不是 Phi 指令就停止检查
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func->removeBasicBlock((iter++)->get());
|
||||
changed = true;
|
||||
} else {
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// 如果函数没有返回指令,则添加一个默认返回指令(主要解决void函数没有返回指令的问题)
|
||||
bool SysYCFGOptUtils::SysYAddReturn(Function *func, IRBuilder* pBuilder) {
|
||||
bool changed = false;
|
||||
auto basicBlocks = func->getBasicBlocks();
|
||||
for (auto &block : basicBlocks) {
|
||||
if (block->getNumSuccessors() == 0) {
|
||||
// 如果基本块没有后继块,则添加一个返回指令
|
||||
if (block->getNumInstructions() == 0) {
|
||||
pBuilder->setPosition(block.get(), block->end());
|
||||
pBuilder->createReturnInst();
|
||||
changed = true; // 标记IR被修改
|
||||
} else {
|
||||
auto thelastinst = block->getInstructions().end();
|
||||
--thelastinst;
|
||||
if (thelastinst->get()->getKind() != Instruction::kReturn) {
|
||||
// std::cout << "Warning: Function " << func->getName() << " has no return instruction, adding default return." << std::endl;
|
||||
|
||||
pBuilder->setPosition(block.get(), block->end());
|
||||
// TODO: 如果int float函数缺少返回值是否需要报错
|
||||
if (func->getReturnType()->isInt()) {
|
||||
pBuilder->createReturnInst(ConstantInteger::get(0));
|
||||
} else if (func->getReturnType()->isFloat()) {
|
||||
pBuilder->createReturnInst(ConstantFloating::get(0.0F));
|
||||
} else {
|
||||
pBuilder->createReturnInst();
|
||||
}
|
||||
changed = true; // 标记IR被修改
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// 条件分支转换为无条件分支
|
||||
// 主要针对已知条件值的分支转换为无条件分支
|
||||
// 例如 if (cond) { ... } else { ... } 中的 cond 已经
|
||||
// 确定为 true 或 false 的情况
|
||||
bool SysYCFGOptUtils::SysYCondBr2Br(Function *func, IRBuilder* pBuilder) {
|
||||
bool changed = false;
|
||||
|
||||
for (auto &basicblock : func->getBasicBlocks()) {
|
||||
if (basicblock->getNumInstructions() == 0)
|
||||
continue;
|
||||
|
||||
auto thelast = basicblock->getInstructions().end();
|
||||
--thelast;
|
||||
|
||||
if (thelast->get()->isConditional()){
|
||||
ConstantValue *constOperand = dynamic_cast<ConstantValue *>(thelast->get()->getOperand(0));
|
||||
std::string opname;
|
||||
int constint = 0;
|
||||
float constfloat = 0.0F;
|
||||
bool constint_Use = false;
|
||||
bool constfloat_Use = false;
|
||||
if (constOperand != nullptr) {
|
||||
if (constOperand->isFloat()) {
|
||||
constfloat = constOperand->getFloat();
|
||||
constfloat_Use = true;
|
||||
} else {
|
||||
constint = constOperand->getInt();
|
||||
constint_Use = true;
|
||||
}
|
||||
}
|
||||
// 如果可以计算
|
||||
if (constfloat_Use || constint_Use) {
|
||||
changed = true;
|
||||
|
||||
auto thenBlock = dynamic_cast<BasicBlock *>(thelast->get()->getOperand(1));
|
||||
auto elseBlock = dynamic_cast<BasicBlock *>(thelast->get()->getOperand(2));
|
||||
SysYIROptUtils::usedelete(thelast->get());
|
||||
thelast = basicblock->getInstructions().erase(thelast);
|
||||
if ((constfloat_Use && constfloat == 1.0F) || (constint_Use && constint == 1)) {
|
||||
// cond为true或非0
|
||||
pBuilder->setPosition(basicblock.get(), basicblock->end());
|
||||
pBuilder->createUncondBrInst(thenBlock, {});
|
||||
|
||||
// 更新CFG关系
|
||||
basicblock->removeSuccessor(elseBlock);
|
||||
elseBlock->removePredecessor(basicblock.get());
|
||||
|
||||
// 删除elseBlock的phi指令中对应的basicblock.get()的传入值
|
||||
for (auto &phiinst : elseBlock->getInstructions()) {
|
||||
if (phiinst->getKind() != Instruction::kPhi) {
|
||||
break;
|
||||
}
|
||||
// 使用 delBlk 方法删除 basicblock.get() 对应的传入值
|
||||
dynamic_cast<PhiInst *>(phiinst.get())->delBlk(basicblock.get());
|
||||
}
|
||||
|
||||
} else { // cond为false或0
|
||||
|
||||
pBuilder->setPosition(basicblock.get(), basicblock->end());
|
||||
pBuilder->createUncondBrInst(elseBlock, {});
|
||||
|
||||
// 更新CFG关系
|
||||
basicblock->removeSuccessor(thenBlock);
|
||||
thenBlock->removePredecessor(basicblock.get());
|
||||
|
||||
// 删除thenBlock的phi指令中对应的basicblock.get()的传入值
|
||||
for (auto &phiinst : thenBlock->getInstructions()) {
|
||||
if (phiinst->getKind() != Instruction::kPhi) {
|
||||
break;
|
||||
}
|
||||
// 使用 delBlk 方法删除 basicblock.get() 对应的传入值
|
||||
dynamic_cast<PhiInst *>(phiinst.get())->delBlk(basicblock.get());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// ======================================================================
|
||||
// 独立的CFG优化遍的实现
|
||||
// ======================================================================
|
||||
|
||||
bool SysYDelInstAfterBrPass::runOnFunction(Function *F, AnalysisManager& AM) {
|
||||
return SysYCFGOptUtils::SysYDelInstAfterBr(F);
|
||||
}
|
||||
|
||||
bool SysYDelEmptyBlockPass::runOnFunction(Function *F, AnalysisManager& AM) {
|
||||
return SysYCFGOptUtils::SysYDelEmptyBlock(F, pBuilder);
|
||||
}
|
||||
|
||||
bool SysYDelNoPreBLockPass::runOnFunction(Function *F, AnalysisManager& AM) {
|
||||
return SysYCFGOptUtils::SysYDelNoPreBLock(F);
|
||||
}
|
||||
|
||||
bool SysYBlockMergePass::runOnFunction(Function *F, AnalysisManager& AM) {
|
||||
return SysYCFGOptUtils::SysYBlockMerge(F);
|
||||
}
|
||||
|
||||
bool SysYAddReturnPass::runOnFunction(Function *F, AnalysisManager& AM) {
|
||||
return SysYCFGOptUtils::SysYAddReturn(F, pBuilder);
|
||||
}
|
||||
|
||||
bool SysYCondBr2BrPass::runOnFunction(Function *F, AnalysisManager& AM) {
|
||||
return SysYCFGOptUtils::SysYCondBr2Br(F, pBuilder);
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
@ -15,46 +15,6 @@
|
||||
using namespace std;
|
||||
namespace sysy {
|
||||
|
||||
|
||||
Type* SysYIRGenerator::buildArrayType(Type* baseType, const std::vector<Value*>& dims){
|
||||
Type* currentType = baseType;
|
||||
// 从最内层维度开始构建 ArrayType
|
||||
// 例如对于 int arr[2][3],先处理 [3],再处理 [2]
|
||||
// 注意:SysY 的 dims 是从最外层到最内层,所以我们需要反向迭代
|
||||
// 或者调整逻辑,使得从内到外构建 ArrayType
|
||||
// 假设 dims 列表是 [dim1, dim2, dim3...] (例如 [2, 3] for int[2][3])
|
||||
// 我们需要从最内层维度开始向外构建 ArrayType
|
||||
for (int i = dims.size() - 1; i >= 0; --i) {
|
||||
// 维度大小必须是常量,否则无法构建 ArrayType
|
||||
ConstantInteger* constDim = dynamic_cast<ConstantInteger*>(dims[i]);
|
||||
if (constDim == nullptr) {
|
||||
// 如果维度不是常量,可能需要特殊处理,例如将其视为指针
|
||||
// 对于函数参数 int arr[] 这种,第一个维度可以为未知
|
||||
// 在这里,我们假设所有声明的数组维度都是常量
|
||||
assert(false && "Array dimension must be a constant integer!");
|
||||
return nullptr;
|
||||
}
|
||||
unsigned dimSize = constDim->getInt();
|
||||
currentType = Type::getArrayType(currentType, dimSize);
|
||||
}
|
||||
return currentType;
|
||||
}
|
||||
|
||||
// @brief: 获取 GEP 指令的地址
|
||||
// @param basePointer: GEP 的基指针,已经过适当的加载/处理,类型为 LLVM IR 中的指针类型。
|
||||
// 例如,对于局部数组,它是 AllocaInst;对于参数数组,它是 LoadInst 的结果。
|
||||
// @param indices: 已经包含了所有必要的偏移索引 (包括可能的初始 0 索引,由 visitLValue 准备)。
|
||||
// @return: 计算得到的地址值 (也是一个指针类型)
|
||||
Value* SysYIRGenerator::getGEPAddressInst(Value* basePointer, const std::vector<Value*>& indices) {
|
||||
// 检查 basePointer 是否为指针类型
|
||||
assert(basePointer->getType()->isPointer() && "Base pointer must be a pointer type!");
|
||||
|
||||
// `indices` 向量现在由调用方(如 visitLValue, visitVarDecl, visitAssignStmt)负责完整准备,
|
||||
// 包括是否需要添加初始的 `0` 索引。
|
||||
// 所以这里直接将其传递给 `builder.createGetElementPtrInst`。
|
||||
return builder.createGetElementPtrInst(basePointer, indices);
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief: visit compUnit
|
||||
* @details:
|
||||
@ -119,11 +79,7 @@ std::any SysYIRGenerator::visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *c
|
||||
delete root;
|
||||
}
|
||||
// 创建全局变量,并更新符号表
|
||||
Type* variableType = type;
|
||||
if (!dims.empty()) { // 如果有维度,说明是数组
|
||||
variableType = buildArrayType(type, dims); // 构建完整的 ArrayType
|
||||
}
|
||||
module->createGlobalValue(name, Type::getPointerType(variableType), dims, values);
|
||||
module->createGlobalValue(name, Type::getPointerType(type), dims, values);
|
||||
}
|
||||
return std::any();
|
||||
}
|
||||
@ -162,28 +118,24 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
Type* variableType = type;
|
||||
if (!dims.empty()) { // 如果有维度,说明是数组
|
||||
variableType = buildArrayType(type, dims); // 构建完整的 ArrayType
|
||||
}
|
||||
|
||||
// 对于数组,alloca 的类型将是指针指向数组类型,例如 `int[2][3]*`
|
||||
// 对于标量,alloca 的类型将是指针指向标量类型,例如 `int*`
|
||||
AllocaInst* alloca =
|
||||
builder.createAllocaInst(Type::getPointerType(variableType), {}, name);
|
||||
builder.createAllocaInst(Type::getPointerType(type), dims, name);
|
||||
|
||||
if (varDef->initVal() != nullptr) {
|
||||
ValueCounter values;
|
||||
// 这里的varDef->initVal()可能是ScalarInitValue或ArrayInitValue
|
||||
ArrayValueTree* root = std::any_cast<ArrayValueTree *>(varDef->initVal()->accept(this));
|
||||
Utils::tree2Array(type, root, dims, dims.size(), values, &builder);
|
||||
delete root;
|
||||
|
||||
if (dims.empty()) { // 标量变量初始化
|
||||
if (dims.empty()) {
|
||||
builder.createStoreInst(values.getValue(0), alloca);
|
||||
} else { // 数组变量初始化
|
||||
} else{
|
||||
// **数组变量初始化**
|
||||
const std::vector<sysy::Value *> &counterValues = values.getValues();
|
||||
const std::vector<unsigned> &counterNumbers = values.getNumbers();
|
||||
|
||||
// 计算数组的**总元素数量**和**总字节大小**
|
||||
int numElements = 1;
|
||||
// 存储每个维度的实际整数大小,用于索引计算
|
||||
std::vector<int> dimSizes;
|
||||
for (Value *dimVal : dims) {
|
||||
if (ConstantInteger *constInt = dynamic_cast<ConstantInteger *>(dimVal)) {
|
||||
@ -193,11 +145,12 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
|
||||
}
|
||||
// TODO else 错误处理:数组维度必须是常量(对于静态分配)
|
||||
}
|
||||
unsigned int elementSizeInBytes = type->getSize();
|
||||
unsigned int elementSizeInBytes = type->getSize(); // 获取单个元素的大小(字节)
|
||||
unsigned int totalSizeInBytes = numElements * elementSizeInBytes;
|
||||
|
||||
// **判断是否可以进行全零初始化优化**
|
||||
bool allValuesAreZero = false;
|
||||
if (counterValues.empty()) {
|
||||
if (counterValues.empty()) { // 例如 int arr[3] = {}; 或 int arr[3][4] = {};
|
||||
allValuesAreZero = true;
|
||||
}
|
||||
else {
|
||||
@ -210,6 +163,7 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
|
||||
}
|
||||
}
|
||||
else{
|
||||
// 如果值不是常量,我们通常不能确定它是否为零,所以不进行 memset 优化
|
||||
allValuesAreZero = false;
|
||||
break;
|
||||
}
|
||||
@ -217,67 +171,64 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
|
||||
}
|
||||
|
||||
if (allValuesAreZero) {
|
||||
// 如果所有初始化值都是零(或没有明确初始化但语法允许),使用 memset 优化
|
||||
builder.createMemsetInst(
|
||||
alloca,
|
||||
ConstantInteger::get(0),
|
||||
alloca, // 目标数组的起始地址
|
||||
ConstantInteger::get(0), // 偏移量(通常为0),后续删除
|
||||
ConstantInteger::get(totalSizeInBytes),
|
||||
ConstantInteger::get(0));
|
||||
ConstantInteger::get(0)); // 填充的总字节数
|
||||
}
|
||||
else {
|
||||
|
||||
int linearIndexOffset = 0; // 用于追踪当前处理的线性索引的偏移量
|
||||
for (int k = 0; k < counterValues.size(); ++k) {
|
||||
// 当前 Value 的值和重复次数
|
||||
Value* currentValue = counterValues[k];
|
||||
unsigned currentRepeatNum = counterNumbers[k];
|
||||
// **逐元素存储:遍历所有初始值,并为每个值生成一个 store 指令**
|
||||
for (size_t k = 0; k < counterValues.size(); ++k) {
|
||||
// 用于存储当前元素的索引列表
|
||||
std::vector<Value *> currentIndices;
|
||||
int tempLinearIndex = k; // 临时线性索引,用于计算多维索引
|
||||
|
||||
for (unsigned i = 0; i < currentRepeatNum; ++i) {
|
||||
std::vector<Value *> currentIndices;
|
||||
int tempLinearIndex = linearIndexOffset + i; // 使用偏移量和当前重复次数内的索引
|
||||
// **将线性索引转换为多维索引**
|
||||
// 这个循环从最内层维度开始倒推,计算每个维度的索引
|
||||
// 假设是行主序(row-major order),这是 C/C++ 数组的标准存储方式
|
||||
for (int dimIdx = dimSizes.size() - 1; dimIdx >= 0; --dimIdx)
|
||||
{
|
||||
// 计算当前维度的索引,并插入到列表的最前面
|
||||
currentIndices.insert(currentIndices.begin(),
|
||||
ConstantInteger::get(static_cast<int>(tempLinearIndex % dimSizes[dimIdx])));
|
||||
// 更新线性索引,用于计算下一个更高维度的索引
|
||||
tempLinearIndex /= dimSizes[dimIdx];
|
||||
}
|
||||
|
||||
// 将线性索引转换为多维索引
|
||||
for (int dimIdx = dimSizes.size() - 1; dimIdx >= 0; --dimIdx) {
|
||||
currentIndices.insert(currentIndices.begin(),
|
||||
ConstantInteger::get(static_cast<int>(tempLinearIndex % dimSizes[dimIdx])));
|
||||
tempLinearIndex /= dimSizes[dimIdx];
|
||||
}
|
||||
|
||||
// 对于局部数组,alloca 本身就是 GEP 的基指针。
|
||||
// GEP 的第一个索引必须是 0,用于“步过”整个数组。
|
||||
std::vector<Value*> gepIndicesForInit;
|
||||
gepIndicesForInit.push_back(ConstantInteger::get(0));
|
||||
gepIndicesForInit.insert(gepIndicesForInit.end(), currentIndices.begin(), currentIndices.end());
|
||||
|
||||
// 计算元素的地址
|
||||
Value* elementAddress = getGEPAddressInst(alloca, gepIndicesForInit);
|
||||
// 生成 store 指令
|
||||
builder.createStoreInst(currentValue, elementAddress);
|
||||
}
|
||||
// 更新线性索引偏移量,以便下一次迭代从正确的位置开始
|
||||
linearIndexOffset += currentRepeatNum;
|
||||
// **生成 store 指令,传入值、基指针和计算出的索引列表**
|
||||
// 你的 builder.createStoreInst 签名需要能够接受这些参数
|
||||
// 假设你的 builder.createStoreInst(Value *val, Value *ptr, const std::vector<Value *> &indices, ...)
|
||||
builder.createStoreInst(counterValues[k], alloca, currentIndices);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else { // 如果没有显式初始化值,默认对数组进行零初始化
|
||||
if (!dims.empty()) { // 只有数组才需要默认的零初始化
|
||||
else
|
||||
{ // **如果没有显式初始化值,默认对数组进行零初始化**
|
||||
if (!dims.empty())
|
||||
{ // 只有数组才需要默认的零初始化
|
||||
int numElements = 1;
|
||||
for (Value *dimVal : dims) {
|
||||
if (ConstantInteger *constInt = dynamic_cast<ConstantInteger *>(dimVal)) {
|
||||
for (Value *dimVal : dims)
|
||||
{
|
||||
if (ConstantInteger *constInt = dynamic_cast<ConstantInteger *>(dimVal))
|
||||
{
|
||||
numElements *= constInt->getInt();
|
||||
}
|
||||
}
|
||||
unsigned int elementSizeInBytes = type->getSize();
|
||||
unsigned int totalSizeInBytes = numElements * elementSizeInBytes;
|
||||
|
||||
// 使用 memset 将整个数组清零
|
||||
builder.createMemsetInst(
|
||||
alloca,
|
||||
ConstantInteger::get(0),
|
||||
ConstantInteger::get(totalSizeInBytes),
|
||||
ConstantInteger::get(0)
|
||||
);
|
||||
); // 填充的总字节数
|
||||
}
|
||||
// 标量变量如果没有初始化值,通常不生成额外的初始化指令,因为其内存已分配但未赋值。
|
||||
}
|
||||
|
||||
module->addVariable(name, alloca);
|
||||
@ -333,119 +284,58 @@ std::any SysYIRGenerator::visitFuncType(SysYParser::FuncTypeContext *ctx) {
|
||||
std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
||||
// 更新作用域
|
||||
module->enterNewScope();
|
||||
HasReturnInst = false;
|
||||
|
||||
auto name = ctx->Ident()->getText();
|
||||
std::vector<Type *> paramActualTypes;
|
||||
std::vector<Type *> paramTypes;
|
||||
std::vector<std::string> paramNames;
|
||||
std::vector<std::vector<Value *>> paramDims;
|
||||
|
||||
if (ctx->funcFParams() != nullptr) {
|
||||
auto params = ctx->funcFParams()->funcFParam();
|
||||
for (const auto ¶m : params) {
|
||||
Type* baseBType = std::any_cast<Type *>(visitBType(param->bType()));
|
||||
std::string paramName = param->Ident()->getText();
|
||||
|
||||
// 用于收集当前参数的维度信息(如果它是数组)
|
||||
std::vector<Value *> currentParamDims;
|
||||
if (!param->LBRACK().empty()) { // 如果参数声明中有方括号,说明是数组
|
||||
// SysY 数组参数的第一个维度可以是未知的(例如 int arr[] 或 int arr[][10])
|
||||
// 这里的 ConstantInteger::get(-1) 表示未知维度,但对于 LLVM 类型构建,我们主要关注已知维度
|
||||
currentParamDims.push_back(ConstantInteger::get(-1)); // 标记第一个维度为未知
|
||||
paramTypes.push_back(std::any_cast<Type *>(visitBType(param->bType())));
|
||||
paramNames.push_back(param->Ident()->getText());
|
||||
std::vector<Value *> dims = {};
|
||||
if (!param->LBRACK().empty()) {
|
||||
dims.push_back(ConstantInteger::get(-1)); // 第一个维度不确定
|
||||
for (const auto &exp : param->exp()) {
|
||||
// 访问表达式以获取维度大小,这些维度必须是常量
|
||||
Value* dimVal = std::any_cast<Value *>(visitExp(exp));
|
||||
// 确保维度是常量整数,否则 buildArrayType 会断言失败
|
||||
assert(dynamic_cast<ConstantInteger*>(dimVal) && "Array dimension in parameter must be a constant integer!");
|
||||
currentParamDims.push_back(dimVal);
|
||||
dims.push_back(std::any_cast<Value *>(visitExp(exp)));
|
||||
}
|
||||
}
|
||||
|
||||
// 根据解析出的信息,确定参数在 LLVM IR 中的实际类型
|
||||
Type* actualParamType;
|
||||
if (currentParamDims.empty()) { // 情况1:标量参数 (e.g., int x)
|
||||
actualParamType = baseBType; // 实际类型就是基本类型
|
||||
} else { // 情况2&3:数组参数 (e.g., int arr[] 或 int arr[][10])
|
||||
// 数组参数在函数传递时会退化为指针。
|
||||
// 这个指针指向的类型是除第一维外,由后续维度构成的数组类型。
|
||||
|
||||
// 从 currentParamDims 中移除第一个标记未知维度的 -1
|
||||
std::vector<Value*> fixedDimsForTypeBuilding;
|
||||
if (currentParamDims.size() > 1) { // 如果有固定维度 (e.g., int arr[][10])
|
||||
// 复制除第一个 -1 之外的所有维度
|
||||
fixedDimsForTypeBuilding.assign(currentParamDims.begin() + 1, currentParamDims.end());
|
||||
}
|
||||
|
||||
Type* pointedToArrayType = baseBType; // 从基本类型开始构建
|
||||
// 从最内层维度向外层构建数组类型
|
||||
// buildArrayType 期望 dims 是从最外层到最内层,但它内部反向迭代,所以这里直接传入
|
||||
// 例如,对于 int arr[][10],fixedDimsForTypeBuilding 包含 [10],构建出 [10 x i32]
|
||||
if (!fixedDimsForTypeBuilding.empty()) {
|
||||
pointedToArrayType = buildArrayType(baseBType, fixedDimsForTypeBuilding);
|
||||
}
|
||||
|
||||
// 实际参数类型是指向这个构建好的数组类型的指针
|
||||
actualParamType = Type::getPointerType(pointedToArrayType); // e.g., i32* 或 [10 x i32]*
|
||||
}
|
||||
|
||||
paramActualTypes.push_back(actualParamType); // 存储参数的实际 LLVM IR 类型
|
||||
paramNames.push_back(paramName); // 存储参数名称
|
||||
|
||||
paramDims.emplace_back(dims);
|
||||
}
|
||||
}
|
||||
|
||||
Type* returnType = std::any_cast<Type *>(visitFuncType(ctx->funcType()));
|
||||
Type* funcType = Type::getFunctionType(returnType, paramActualTypes);
|
||||
Type* funcType = Type::getFunctionType(returnType, paramTypes);
|
||||
Function* function = module->createFunction(name, funcType);
|
||||
BasicBlock* entry = function->getEntryBlock();
|
||||
builder.setPosition(entry, entry->end());
|
||||
|
||||
for(int i = 0; i < paramActualTypes.size(); ++i) {
|
||||
Argument* arg = new Argument(paramActualTypes[i], function, i, paramNames[i]);
|
||||
function->insertArgument(arg);
|
||||
|
||||
}
|
||||
|
||||
auto funcArgs = function->getArguments();
|
||||
std::vector<AllocaInst *> allocas;
|
||||
for (int i = 0; i < paramActualTypes.size(); ++i) {
|
||||
AllocaInst *alloca = builder.createAllocaInst(Type::getPointerType(paramActualTypes[i]), {}, paramNames[i]);
|
||||
allocas.push_back(alloca);
|
||||
for (size_t i = 0; i < paramTypes.size(); ++i) {
|
||||
AllocaInst* alloca = builder.createAllocaInst(Type::getPointerType(paramTypes[i]),
|
||||
paramDims[i], paramNames[i]);
|
||||
entry->insertArgument(alloca);
|
||||
module->addVariable(paramNames[i], alloca);
|
||||
}
|
||||
|
||||
for(int i = 0; i < paramActualTypes.size(); ++i) {
|
||||
Value *argValue = funcArgs[i];
|
||||
builder.createStoreInst(argValue, allocas[i]);
|
||||
}
|
||||
|
||||
// 在处理函数体之前,创建一个新的基本块作为函数体的实际入口
|
||||
// 这样 entryBB 就可以在完成初始化后跳转到这里
|
||||
BasicBlock* funcBodyEntry = function->addBasicBlock("funcBodyEntry_" + name);
|
||||
|
||||
// 从 entryBB 无条件跳转到 funcBodyEntry
|
||||
builder.createUncondBrInst(funcBodyEntry, {});
|
||||
builder.setPosition(funcBodyEntry,funcBodyEntry->end()); // 将插入点设置到 funcBodyEntry
|
||||
|
||||
for (auto item : ctx->blockStmt()->blockItem()) {
|
||||
visitBlockItem(item);
|
||||
}
|
||||
|
||||
// 如果函数没有显式的返回语句,且返回类型不是 void,则需要添加一个默认的返回值
|
||||
ReturnInst* retinst = nullptr;
|
||||
retinst = dynamic_cast<ReturnInst*>(builder.getBasicBlock()->terminator()->get());
|
||||
|
||||
if (!retinst) {
|
||||
if (returnType->isVoid()) {
|
||||
builder.createReturnInst();
|
||||
} else if (returnType->isInt()) {
|
||||
builder.createReturnInst(ConstantInteger::get(0)); // 默认返回 0
|
||||
} else if (returnType->isFloat()) {
|
||||
builder.createReturnInst(ConstantFloating::get(0.0f)); // 默认返回 0.0f
|
||||
if(HasReturnInst == false) {
|
||||
// 如果没有return语句,则默认返回0
|
||||
if (returnType != Type::getVoidType()) {
|
||||
Value* returnValue = ConstantInteger::get(0);
|
||||
if (returnType == Type::getFloatType()) {
|
||||
returnValue = ConstantFloating::get(0.0f);
|
||||
}
|
||||
builder.createReturnInst(returnValue);
|
||||
} else {
|
||||
assert(false && "Function with no explicit return and non-void type should return a value.");
|
||||
builder.createReturnInst();
|
||||
}
|
||||
}
|
||||
|
||||
module->leaveScope();
|
||||
|
||||
return std::any();
|
||||
@ -462,79 +352,33 @@ std::any SysYIRGenerator::visitBlockStmt(SysYParser::BlockStmtContext *ctx) {
|
||||
std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx) {
|
||||
auto lVal = ctx->lValue();
|
||||
std::string name = lVal->Ident()->getText();
|
||||
Value* LValue = nullptr;
|
||||
Value* variable = module->getVariable(name); // 左值
|
||||
|
||||
vector<Value *> indices;
|
||||
if (lVal->exp().size() > 0) {
|
||||
// 如果有下标,访问表达式获取下标值
|
||||
for (const auto &exp : lVal->exp()) {
|
||||
Value* indexValue = std::any_cast<Value *>(visitExp(exp));
|
||||
indices.push_back(indexValue);
|
||||
}
|
||||
}
|
||||
if (indices.empty()) {
|
||||
// variable 本身就是指向标量的指针 (e.g., int* %a)
|
||||
if (dynamic_cast<AllocaInst*>(variable) || dynamic_cast<GlobalValue*>(variable)) {
|
||||
LValue = variable;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 对于数组或多维数组的左值处理
|
||||
// 需要获取 GEP 地址
|
||||
Value* gepBasePointer = nullptr;
|
||||
std::vector<Value*> gepIndices;
|
||||
if (AllocaInst *alloc = dynamic_cast<AllocaInst *>(variable)) {
|
||||
Type* allocatedType = alloc->getType()->as<PointerType>()->getBaseType();
|
||||
if (allocatedType->isPointer()) {
|
||||
gepBasePointer = builder.createLoadInst(alloc);
|
||||
gepIndices = indices;
|
||||
} else {
|
||||
gepBasePointer = alloc;
|
||||
gepIndices.push_back(ConstantInteger::get(0));
|
||||
gepIndices.insert(gepIndices.end(), indices.begin(), indices.end());
|
||||
}
|
||||
} else if (GlobalValue *glob = dynamic_cast<GlobalValue *>(variable)) {
|
||||
// 情况 B: 全局变量 (GlobalValue)
|
||||
gepBasePointer = glob;
|
||||
gepIndices.push_back(ConstantInteger::get(0));
|
||||
gepIndices.insert(gepIndices.end(), indices.begin(), indices.end());
|
||||
} else if (ConstantVariable *constV = dynamic_cast<ConstantVariable *>(variable)) {
|
||||
gepBasePointer = constV;
|
||||
gepIndices.push_back(ConstantInteger::get(0));
|
||||
gepIndices.insert(gepIndices.end(), indices.begin(), indices.end());
|
||||
}
|
||||
// 左值为地址
|
||||
LValue = getGEPAddressInst(gepBasePointer, gepIndices);
|
||||
std::vector<Value *> dims;
|
||||
for (const auto &exp : lVal->exp()) {
|
||||
dims.push_back(std::any_cast<Value *>(visitExp(exp)));
|
||||
}
|
||||
|
||||
Value* RValue = std::any_cast<Value *>(visitExp(ctx->exp())); // 右值
|
||||
auto variable = module->getVariable(name);
|
||||
Value* value = std::any_cast<Value *>(visitExp(ctx->exp()));
|
||||
Type* variableType = dynamic_cast<PointerType *>(variable->getType())->getBaseType();
|
||||
|
||||
// 先推断 LValue 的类型
|
||||
// 如果 LValue 是指向数组的指针,则需要根据 indices 获取正确的类型
|
||||
// 如果 LValue 是标量,则直接使用其类型
|
||||
// 注意:LValue 的类型可能是指向数组的指针 (e.g., int(*)[3]) 或者指向标量的指针 (e.g., int*) 也能推断
|
||||
Type* LType = builder.getIndexedType(variable->getType(), indices);
|
||||
Type* RType = RValue->getType();
|
||||
|
||||
if (LType != RType) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(RValue);
|
||||
// 左值右值类型不同处理
|
||||
if (variableType != value->getType()) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(value);
|
||||
if (constValue != nullptr) {
|
||||
if (LType == Type::getFloatType()) {
|
||||
RValue = ConstantFloating::get(static_cast<float>(constValue->getFloat()));
|
||||
} else { // 假设如果不是浮点型,就是整型
|
||||
RValue = ConstantInteger::get(static_cast<int>(constValue->getInt()));
|
||||
if (variableType == Type::getFloatType()) {
|
||||
value = ConstantInteger::get(static_cast<float>(constValue->getInt()));
|
||||
} else {
|
||||
value = ConstantFloating::get(static_cast<int>(constValue->getFloat()));
|
||||
}
|
||||
} else {
|
||||
if (LType == Type::getFloatType()) {
|
||||
RValue = builder.createIToFInst(RValue);
|
||||
} else { // 假设如果不是浮点型,就是整型
|
||||
RValue = builder.createFtoIInst(RValue);
|
||||
if (variableType == Type::getFloatType()) {
|
||||
value = builder.createIToFInst(value);
|
||||
} else {
|
||||
value = builder.createFtoIInst(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
builder.createStoreInst(RValue, LValue);
|
||||
builder.createStoreInst(value, variable, dims, variable->getName());
|
||||
|
||||
return std::any();
|
||||
}
|
||||
@ -622,7 +466,6 @@ std::any SysYIRGenerator::visitIfStmt(SysYParser::IfStmtContext *ctx) {
|
||||
ctx->stmt(0)->accept(this);
|
||||
module->leaveScope();
|
||||
}
|
||||
builder.createUncondBrInst(exitBlock, {});
|
||||
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
|
||||
|
||||
labelstring << "if_exit.L" << builder.getLabelIndex();
|
||||
@ -644,7 +487,6 @@ std::any SysYIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext *ctx) {
|
||||
labelstring << "while_head.L" << builder.getLabelIndex();
|
||||
BasicBlock *headBlock = function->addBasicBlock(labelstring.str());
|
||||
labelstring.str("");
|
||||
builder.createUncondBrInst(headBlock, {});
|
||||
BasicBlock::conectBlocks(curBlock, headBlock);
|
||||
builder.setPosition(headBlock, headBlock->end());
|
||||
|
||||
@ -729,141 +571,56 @@ std::any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx) {
|
||||
}
|
||||
}
|
||||
builder.createReturnInst(returnValue);
|
||||
|
||||
HasReturnInst = true;
|
||||
return std::any();
|
||||
}
|
||||
|
||||
|
||||
// 辅助函数:计算给定类型中嵌套的数组维度数量
|
||||
// 例如:
|
||||
// - 对于 i32* 类型,它指向 i32,维度为 0。
|
||||
// - 对于 [10 x i32]* 类型,它指向 [10 x i32],维度为 1。
|
||||
// - 对于 [20 x [10 x i32]]* 类型,它指向 [20 x [10 x i32]],维度为 2。
|
||||
unsigned SysYIRGenerator::countArrayDimensions(Type* type) {
|
||||
unsigned dims = 0;
|
||||
Type* currentType = type;
|
||||
|
||||
// 如果是指针类型,先获取它指向的基础类型
|
||||
if (currentType->isPointer()) {
|
||||
currentType = currentType->as<PointerType>()->getBaseType();
|
||||
}
|
||||
|
||||
// 递归地计算数组的维度层数
|
||||
while (currentType && currentType->isArray()) {
|
||||
dims++;
|
||||
currentType = currentType->as<ArrayType>()->getElementType();
|
||||
}
|
||||
return dims;
|
||||
}
|
||||
|
||||
std::any SysYIRGenerator::visitLValue(SysYParser::LValueContext *ctx) {
|
||||
std::string name = ctx->Ident()->getText();
|
||||
Value* variable = module->getVariable(name);
|
||||
User* variable = module->getVariable(name);
|
||||
|
||||
Value* value = nullptr;
|
||||
|
||||
std::vector<Value *> dims;
|
||||
for (const auto &exp : ctx->exp()) {
|
||||
dims.push_back(std::any_cast<Value *>(visitExp(exp)));
|
||||
}
|
||||
|
||||
// 1. 获取变量的声明维度数量
|
||||
unsigned declaredNumDims = countArrayDimensions(variable->getType());
|
||||
if (variable == nullptr) {
|
||||
throw std::runtime_error("Variable " + name + " not found.");
|
||||
}
|
||||
|
||||
bool indicesConstant = true;
|
||||
for (const auto &dim : dims) {
|
||||
if (dynamic_cast<ConstantValue *>(dim) == nullptr) {
|
||||
indicesConstant = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 处理常量变量 (ConstantVariable) 且所有索引都是常量的情况
|
||||
ConstantVariable* constVar = dynamic_cast<ConstantVariable *>(variable);
|
||||
if (constVar != nullptr) {
|
||||
bool allIndicesConstant = true;
|
||||
for (const auto &dim : dims) {
|
||||
if (dynamic_cast<ConstantValue *>(dim) == nullptr) {
|
||||
allIndicesConstant = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (allIndicesConstant) {
|
||||
// 如果是常量变量且所有索引都是常量,直接通过 getByIndices 获取编译时值
|
||||
// 这个方法会根据索引深度返回最终的标量值或指向子数组的指针 (作为 ConstantValue/Variable)
|
||||
return constVar->getByIndices(dims);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 处理可变变量 (AllocaInst/GlobalValue) 或带非常量索引的常量变量
|
||||
// 这里区分标量访问和数组元素/子数组访问
|
||||
|
||||
// 检查是否是访问标量变量本身(没有索引,且声明维度为0)
|
||||
if (dims.empty() && declaredNumDims == 0) {
|
||||
// 对于标量变量,直接加载其值。
|
||||
// variable 本身就是指向标量的指针 (e.g., int* %a)
|
||||
if (dynamic_cast<AllocaInst*>(variable) || dynamic_cast<GlobalValue*>(variable)) {
|
||||
value = builder.createLoadInst(variable);
|
||||
} else {
|
||||
// 如果走到这里且不是AllocaInst/GlobalValue,但dims为空且declaredNumDims为0,
|
||||
// 且又不是ConstantVariable (前面已处理),则可能是错误情况。
|
||||
assert(false && "Unhandled scalar variable type in LValue access.");
|
||||
return static_cast<Value*>(nullptr);
|
||||
}
|
||||
GlobalValue* globalVar = dynamic_cast<GlobalValue *>(variable);
|
||||
AllocaInst* localVar = dynamic_cast<AllocaInst *>(variable);
|
||||
if (constVar != nullptr && indicesConstant) {
|
||||
// 如果是常量变量,且索引是常量,则直接获取子数组
|
||||
value = constVar->getByIndices(dims);
|
||||
} else if (module->isInGlobalArea() && (globalVar != nullptr)) {
|
||||
assert(indicesConstant);
|
||||
value = globalVar->getByIndices(dims);
|
||||
} else {
|
||||
// 访问数组元素或子数组(有索引,或变量本身是数组/多维指针)
|
||||
Value* gepBasePointer = nullptr;
|
||||
std::vector<Value*> gepIndices; // 准备传递给 getGEPAddressInst 的索引列表
|
||||
// GEP 的基指针就是变量本身(它是一个指向内存的指针)
|
||||
if (AllocaInst *alloc = dynamic_cast<AllocaInst *>(variable)) {
|
||||
// 情况 A: 局部变量 (AllocaInst)
|
||||
// 获取 AllocaInst 分配的内存的实际类型。
|
||||
// 例如:对于 `int b[10][20];`,`allocatedType` 是 `[10 x [20 x i32]]`。
|
||||
// 对于 `int b[][20]` 的函数参数,其 AllocaInst 存储的是一个指针,
|
||||
// 此时 `allocatedType` 是 `[20 x i32]*`。
|
||||
Type* allocatedType = alloc->getType()->as<PointerType>()->getBaseType();
|
||||
|
||||
if (allocatedType->isPointer()) {
|
||||
// 如果 AllocaInst 分配的是一个指针类型 (例如,用于存储函数参数的指针,如 int b[][20] 中的 b)
|
||||
// 即 `allocatedType` 是一个指向数组指针的指针 (e.g., [20 x i32]**)
|
||||
// 那么 GEP 的基指针是加载这个指针变量的值。
|
||||
gepBasePointer = builder.createLoadInst(alloc); // 加载出实际的指针值 (e.g., [20 x i32]*)
|
||||
// 对于这种参数指针,用户提供的索引直接作用于它。不需要额外的 0。
|
||||
gepIndices = dims;
|
||||
} else {
|
||||
// 如果 AllocaInst 分配的是实际的数组数据 (例如,int b[10][20] 中的 b)
|
||||
// 那么 AllocaInst 本身就是 GEP 的基指针。
|
||||
// 这里的 `alloc` 是指向数组的指针 (e.g., [10 x [20 x i32]]*)
|
||||
gepBasePointer = alloc; // 类型是 [10 x [20 x i32]]*
|
||||
// 对于这种完整的数组分配,GEP 的第一个索引必须是 0,用于“步过”整个数组。
|
||||
gepIndices.push_back(ConstantInteger::get(0));
|
||||
gepIndices.insert(gepIndices.end(), dims.begin(), dims.end());
|
||||
}
|
||||
} else if (GlobalValue *glob = dynamic_cast<GlobalValue *>(variable)) {
|
||||
// 情况 B: 全局变量 (GlobalValue)
|
||||
// GlobalValue 总是指向全局数据的指针。
|
||||
gepBasePointer = glob; // 类型是 [61 x [67 x i32]]*
|
||||
// 对于全局数组,GEP 的第一个索引必须是 0,用于“步过”整个数组。
|
||||
gepIndices.push_back(ConstantInteger::get(0));
|
||||
gepIndices.insert(gepIndices.end(), dims.begin(), dims.end());
|
||||
} else if (ConstantVariable *constV = dynamic_cast<ConstantVariable *>(variable)) {
|
||||
// 情况 C: 常量变量 (ConstantVariable),如果它代表全局数组常量
|
||||
// 假设 ConstantVariable 可以直接作为 GEP 的基指针。
|
||||
gepBasePointer = constV;
|
||||
// 对于常量数组,也需要 0 索引来“步过”整个数组。
|
||||
// 这里可以进一步检查 constV->getType()->as<PointerType>()->getBaseType()->isArray()
|
||||
// 但为了简洁,假设所有 ConstantVariable 作为 GEP 基指针时都需要此 0。
|
||||
gepIndices.push_back(ConstantInteger::get(0));
|
||||
gepIndices.insert(gepIndices.end(), dims.begin(), dims.end());
|
||||
if ((globalVar != nullptr && globalVar->getNumDims() > dims.size()) ||
|
||||
(localVar != nullptr && localVar->getNumDims() > dims.size()) ||
|
||||
(constVar != nullptr && constVar->getNumDims() > dims.size())) {
|
||||
// value = builder.createLaInst(variable, indices);
|
||||
// 如果变量是全局变量或局部变量,且索引数量小于维度数量,则创建createGetSubArray获取子数组
|
||||
auto getArrayInst =
|
||||
builder.createGetSubArray(dynamic_cast<LVal *>(variable), dims);
|
||||
value = getArrayInst->getChildArray();
|
||||
} else {
|
||||
assert(false && "LValue variable type not supported for GEP base pointer.");
|
||||
return static_cast<Value *>(nullptr);
|
||||
}
|
||||
|
||||
// 现在调用 getGEPAddressInst,传入正确准备的基指针和索引列表
|
||||
Value *targetAddress = getGEPAddressInst(gepBasePointer, gepIndices);
|
||||
|
||||
// 如果提供的索引数量少于声明的维度数量,则表示访问的是子数组,返回其地址
|
||||
if (dims.size() < declaredNumDims) {
|
||||
value = targetAddress;
|
||||
} else {
|
||||
// 否则,表示访问的是最终的标量元素,加载其值
|
||||
// 假设 createLoadInst 接受 Value* pointer
|
||||
value = builder.createLoadInst(targetAddress);
|
||||
value = builder.createLoadInst(variable, dims);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -905,63 +662,32 @@ std::any SysYIRGenerator::visitCall(SysYParser::CallContext *ctx) {
|
||||
|
||||
std::vector<Value *> args = {};
|
||||
if (funcName == "starttime" || funcName == "stoptime") {
|
||||
args.emplace_back(
|
||||
ConstantInteger::get(static_cast<int>(ctx->getStart()->getLine())));
|
||||
// 如果是starttime或stoptime函数
|
||||
// TODO: 这里需要处理starttime和stoptime函数的参数
|
||||
// args.emplace_back()
|
||||
} else {
|
||||
if (ctx->funcRParams() != nullptr) {
|
||||
args = std::any_cast<std::vector<Value *>>(visitFuncRParams(ctx->funcRParams()));
|
||||
}
|
||||
|
||||
// 获取形参列表。`getArguments()` 返回的是 `Argument*` 的集合,
|
||||
// 每个 `Argument` 代表一个函数形参,其 `getType()` 就是指向形参的类型的指针类型。
|
||||
auto formalParams = function->getArguments();
|
||||
|
||||
// 检查实参和形参数量是否匹配。
|
||||
if (args.size() != formalParams.size()) {
|
||||
std::cerr << "Error: Function call argument count mismatch for function '" << funcName << "'." << std::endl;
|
||||
assert(false && "Function call argument count mismatch!");
|
||||
}
|
||||
|
||||
for (int i = 0; i < args.size(); i++) {
|
||||
// 形参的类型 (e.g., i32, float, i32*, [10 x i32]*)
|
||||
Type* formalParamExpectedValueType = formalParams[i]->getType();
|
||||
// 实参的实际类型 (e.g., i32, float, i32*, [67 x i32]*)
|
||||
Type* actualArgType = args[i]->getType();
|
||||
// 如果实参类型与形参类型不匹配,则尝试进行类型转换
|
||||
if (formalParamExpectedValueType != actualArgType) {
|
||||
ConstantValue *constValue = dynamic_cast<ConstantValue *>(args[i]);
|
||||
auto params = function->getEntryBlock()->getArguments();
|
||||
for (size_t i = 0; i < args.size(); i++) {
|
||||
// 参数类型转换
|
||||
if (params[i]->getType() != args[i]->getType() &&
|
||||
(params[i]->getNumDims() != 0 ||
|
||||
params[i]->getType()->as<PointerType>()->getBaseType() != args[i]->getType())) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(args[i]);
|
||||
if (constValue != nullptr) {
|
||||
if (formalParamExpectedValueType->isInt() && actualArgType->isFloat()) {
|
||||
args[i] = ConstantInteger::get(static_cast<int>(constValue->getFloat()));
|
||||
} else if (formalParamExpectedValueType->isFloat() && actualArgType->isInt()) {
|
||||
args[i] = ConstantFloating::get(static_cast<float>(constValue->getInt()));
|
||||
if (params[i]->getType() == Type::getPointerType(Type::getFloatType())) {
|
||||
args[i] = ConstantInteger::get(static_cast<float>(constValue->getInt()));
|
||||
} else {
|
||||
// 如果是常量但不是简单的 int/float 标量转换,
|
||||
// 或者是指针常量需要 bitcast,则让它进入非常量转换逻辑。
|
||||
// 例如,一个常量数组的地址,需要 bitcast 成另一种指针类型。
|
||||
// 目前不知道样例有没有这种情况,所以这里不做处理。
|
||||
args[i] = ConstantFloating::get(static_cast<int>(constValue->getFloat()));
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 1. 标量值类型转换 (例如:int_reg 到 float_reg,float_reg 到 int_reg)
|
||||
if (formalParamExpectedValueType->isInt() && actualArgType->isFloat()) {
|
||||
args[i] = builder.createFtoIInst(args[i]);
|
||||
} else if (formalParamExpectedValueType->isFloat() && actualArgType->isInt()) {
|
||||
} else {
|
||||
if (params[i]->getType() == Type::getPointerType(Type::getFloatType())) {
|
||||
args[i] = builder.createIToFInst(args[i]);
|
||||
}
|
||||
// 2. 指针类型转换 (例如数组退化:`[N x T]*` 到 `T*`,或兼容指针类型之间) TODO:不清楚有没有这种样例
|
||||
// 这种情况常见于数组参数,实参可能是一个更具体的数组指针类型,
|
||||
// 而形参是其退化后的基础指针类型。LLVM 的 `bitcast` 指令可以用于
|
||||
// 在相同大小的指针类型之间进行转换,这对于数组退化至关重要。
|
||||
// else if (formalParamType->isPointer() && actualArgType->isPointer()) {
|
||||
// 检查指针基类型是否兼容,或者是否是数组退化导致的类型不同。
|
||||
// 使用 bitcast,
|
||||
// args[i] = builder.createBitCastInst(args[i], formalParamType);
|
||||
// }
|
||||
// 3. 其他未预期的类型不匹配
|
||||
// 如果代码执行到这里,说明存在编译器前端未处理的类型不兼容或错误。
|
||||
else {
|
||||
// assert(false && "Unhandled type mismatch for function call argument.");
|
||||
} else {
|
||||
args[i] = builder.createFtoIInst(args[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1031,7 +757,7 @@ std::any SysYIRGenerator::visitFuncRParams(SysYParser::FuncRParamsContext *ctx)
|
||||
std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
|
||||
Value * result = std::any_cast<Value *>(visitUnaryExp(ctx->unaryExp(0)));
|
||||
|
||||
for (int i = 1; i < ctx->unaryExp().size(); i++) {
|
||||
for (size_t i = 1; i < ctx->unaryExp().size(); i++) {
|
||||
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
|
||||
int opType = opNode->getSymbol()->getType();
|
||||
|
||||
@ -1107,7 +833,7 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
|
||||
std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
||||
Value* result = std::any_cast<Value *>(visitMulExp(ctx->mulExp(0)));
|
||||
|
||||
for (int i = 1; i < ctx->mulExp().size(); i++) {
|
||||
for (size_t i = 1; i < ctx->mulExp().size(); i++) {
|
||||
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
|
||||
int opType = opNode->getSymbol()->getType();
|
||||
|
||||
@ -1168,7 +894,7 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
||||
std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
|
||||
Value* result = std::any_cast<Value *>(visitAddExp(ctx->addExp(0)));
|
||||
|
||||
for (int i = 1; i < ctx->addExp().size(); i++) {
|
||||
for (size_t i = 1; i < ctx->addExp().size(); i++) {
|
||||
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
|
||||
int opType = opNode->getSymbol()->getType();
|
||||
|
||||
@ -1240,7 +966,7 @@ std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
|
||||
std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext *ctx) {
|
||||
Value * result = std::any_cast<Value *>(visitRelExp(ctx->relExp(0)));
|
||||
|
||||
for (int i = 1; i < ctx->relExp().size(); i++) {
|
||||
for (size_t i = 1; i < ctx->relExp().size(); i++) {
|
||||
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
|
||||
int opType = opNode->getSymbol()->getType();
|
||||
|
||||
@ -1314,7 +1040,7 @@ std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext *ctx){
|
||||
BasicBlock *falseBlock = builder.getFalseBlock();
|
||||
auto conds = ctx->eqExp();
|
||||
|
||||
for (int i = 0; i < conds.size() - 1; i++) {
|
||||
for (size_t i = 0; i < conds.size() - 1; i++) {
|
||||
|
||||
labelstring << "AND.L" << builder.getLabelIndex();
|
||||
BasicBlock *newtrueBlock = function->addBasicBlock(labelstring.str());
|
||||
@ -1345,7 +1071,7 @@ auto SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext *ctx) -> std::any {
|
||||
Function *function = curBlock->getParent();
|
||||
auto conds = ctx->lAndExp();
|
||||
|
||||
for (int i = 0; i < conds.size() - 1; i++) {
|
||||
for (size_t i = 0; i < conds.size() - 1; i++) {
|
||||
labelstring << "OR.L" << builder.getLabelIndex();
|
||||
BasicBlock *newFalseBlock = function->addBasicBlock(labelstring.str());
|
||||
labelstring.str("");
|
||||
@ -1362,7 +1088,6 @@ auto SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext *ctx) -> std::any {
|
||||
return std::any();
|
||||
}
|
||||
|
||||
// attention : 这里的type是数组元素的type
|
||||
void Utils::tree2Array(Type *type, ArrayValueTree *root,
|
||||
const std::vector<Value *> &dims, unsigned numDims,
|
||||
ValueCounter &result, IRBuilder *builder) {
|
||||
@ -1433,13 +1158,11 @@ void Utils::createExternalFunction(
|
||||
auto entry = function->getEntryBlock();
|
||||
pBuilder->setPosition(entry, entry->end());
|
||||
|
||||
for (int i = 0; i < paramTypes.size(); ++i) {
|
||||
auto arg = new Argument(paramTypes[i], function, i, paramNames[i]);
|
||||
for (size_t i = 0; i < paramTypes.size(); ++i) {
|
||||
auto alloca = pBuilder->createAllocaInst(
|
||||
Type::getPointerType(paramTypes[i]), {}, paramNames[i]);
|
||||
function->insertArgument(arg);
|
||||
auto store = pBuilder->createStoreInst(arg, alloca);
|
||||
pModule->addVariable(paramNames[i], alloca);
|
||||
Type::getPointerType(paramTypes[i]), paramDims[i], paramNames[i]);
|
||||
entry->insertArgument(alloca);
|
||||
// pModule->addVariable(paramNames[i], alloca);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
484
src/SysYIROptPre.cpp
Normal file
484
src/SysYIROptPre.cpp
Normal file
@ -0,0 +1,484 @@
|
||||
#include "SysYIROptPre.h"
|
||||
#include <cassert>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "IR.h"
|
||||
#include "IRBuilder.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
/**
|
||||
* use删除operand,以免扰乱后续分析
|
||||
* instr: 要删除的指令
|
||||
*/
|
||||
void SysYOptPre::usedelete(Instruction *instr) {
|
||||
for (auto &use : instr->getOperands()) {
|
||||
Value* val = use->getValue();
|
||||
// std::cout << delete << val->getName() << std::endl;
|
||||
val->removeUse(use);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 删除br后的无用指令
|
||||
void SysYOptPre::SysYDelInstAfterBr() {
|
||||
auto &functions = pModule->getFunctions();
|
||||
for (auto &function : functions) {
|
||||
auto basicBlocks = function.second->getBasicBlocks();
|
||||
for (auto &basicBlock : basicBlocks) {
|
||||
bool Branch = false;
|
||||
auto &instructions = basicBlock->getInstructions();
|
||||
auto Branchiter = instructions.end();
|
||||
for (auto iter = instructions.begin(); iter != instructions.end(); ++iter) {
|
||||
if (Branch)
|
||||
usedelete(iter->get());
|
||||
else if ((*iter)->isTerminator()){
|
||||
Branch = true;
|
||||
Branchiter = iter;
|
||||
}
|
||||
}
|
||||
if (Branchiter != instructions.end()) ++Branchiter;
|
||||
while (Branchiter != instructions.end())
|
||||
Branchiter = instructions.erase(Branchiter);
|
||||
|
||||
if (Branch) { // 更新前驱后继关系
|
||||
auto thelastinstinst = basicBlock->getInstructions().end();
|
||||
--thelastinstinst;
|
||||
auto &Successors = basicBlock->getSuccessors();
|
||||
for (auto iterSucc = Successors.begin(); iterSucc != Successors.end();) {
|
||||
(*iterSucc)->removePredecessor(basicBlock.get());
|
||||
basicBlock->removeSuccessor(*iterSucc);
|
||||
}
|
||||
if (thelastinstinst->get()->isUnconditional()) {
|
||||
BasicBlock* branchBlock = dynamic_cast<BasicBlock *>(thelastinstinst->get()->getOperand(0));
|
||||
basicBlock->addSuccessor(branchBlock);
|
||||
branchBlock->addPredecessor(basicBlock.get());
|
||||
} else if (thelastinstinst->get()->isConditional()) {
|
||||
BasicBlock* thenBlock = dynamic_cast<BasicBlock *>(thelastinstinst->get()->getOperand(1));
|
||||
BasicBlock* elseBlock = dynamic_cast<BasicBlock *>(thelastinstinst->get()->getOperand(2));
|
||||
basicBlock->addSuccessor(thenBlock);
|
||||
basicBlock->addSuccessor(elseBlock);
|
||||
thenBlock->addPredecessor(basicBlock.get());
|
||||
elseBlock->addPredecessor(basicBlock.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SysYOptPre::SysYBlockMerge() {
|
||||
auto &functions = pModule->getFunctions(); //std::map<std::string, std::unique_ptr<Function>>
|
||||
for (auto &function : functions) {
|
||||
// auto basicBlocks = function.second->getBasicBlocks();
|
||||
auto &func = function.second;
|
||||
for (auto blockiter = func->getBasicBlocks().begin();
|
||||
blockiter != func->getBasicBlocks().end();) {
|
||||
if (blockiter->get()->getNumSuccessors() == 1) {
|
||||
// 如果当前块只有一个后继块
|
||||
// 且后继块只有一个前驱块
|
||||
// 则将当前块和后继块合并
|
||||
if (((blockiter->get())->getSuccessors()[0])->getNumPredecessors() == 1) {
|
||||
// std::cout << "merge block: " << blockiter->get()->getName() << std::endl;
|
||||
BasicBlock* block = blockiter->get();
|
||||
BasicBlock* nextBlock = blockiter->get()->getSuccessors()[0];
|
||||
auto nextarguments = nextBlock->getArguments();
|
||||
// 删除br指令
|
||||
if (block->getNumInstructions() != 0) {
|
||||
auto thelastinstinst = block->end();
|
||||
(--thelastinstinst);
|
||||
if (thelastinstinst->get()->isUnconditional()) {
|
||||
usedelete(thelastinstinst->get());
|
||||
block->getInstructions().erase(thelastinstinst);
|
||||
} else if (thelastinstinst->get()->isConditional()) {
|
||||
// 如果是条件分支,判断条件是否相同,主要优化相同布尔表达式
|
||||
if (thelastinstinst->get()->getOperand(1)->getName() == thelastinstinst->get()->getOperand(1)->getName()) {
|
||||
usedelete(thelastinstinst->get());
|
||||
block->getInstructions().erase(thelastinstinst);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 将后继块的指令移动到当前块
|
||||
// 并将后继块的父指针改为当前块
|
||||
for (auto institer = nextBlock->begin(); institer != nextBlock->end();) {
|
||||
institer->get()->setParent(block);
|
||||
block->getInstructions().emplace_back(institer->release());
|
||||
institer = nextBlock->getInstructions().erase(institer);
|
||||
}
|
||||
// 合并参数
|
||||
// TODO:是否需要去重?
|
||||
for (auto &argm : nextarguments) {
|
||||
argm->setParent(block);
|
||||
block->insertArgument(argm);
|
||||
}
|
||||
// 更新前驱后继关系,类似树节点操作
|
||||
block->removeSuccessor(nextBlock);
|
||||
nextBlock->removePredecessor(block);
|
||||
std::list<BasicBlock *> succshoulddel;
|
||||
for (auto &succ : nextBlock->getSuccessors()) {
|
||||
block->addSuccessor(succ);
|
||||
succ->replacePredecessor(nextBlock, block);
|
||||
succshoulddel.push_back(succ);
|
||||
}
|
||||
for (auto del : succshoulddel) {
|
||||
nextBlock->removeSuccessor(del);
|
||||
}
|
||||
|
||||
func->removeBasicBlock(nextBlock);
|
||||
|
||||
} else {
|
||||
blockiter++;
|
||||
}
|
||||
} else {
|
||||
blockiter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除无前驱块,兼容SSA后的处理
|
||||
void SysYOptPre::SysYDelNoPreBLock() {
|
||||
|
||||
auto &functions = pModule->getFunctions(); // std::map<std::string, std::unique_ptr<sysy::Function>>
|
||||
for (auto &function : functions) {
|
||||
auto &func = function.second;
|
||||
|
||||
for (auto &block : func->getBasicBlocks()) {
|
||||
block->setreachableFalse();
|
||||
}
|
||||
// 对函数基本块做一个拓扑排序,排查不可达基本块
|
||||
auto entryBlock = func->getEntryBlock();
|
||||
entryBlock->setreachableTrue();
|
||||
std::queue<BasicBlock *> blockqueue;
|
||||
blockqueue.push(entryBlock);
|
||||
while (!blockqueue.empty()) {
|
||||
auto block = blockqueue.front();
|
||||
blockqueue.pop();
|
||||
for (auto &succ : block->getSuccessors()) {
|
||||
if (!succ->getreachable()) {
|
||||
succ->setreachableTrue();
|
||||
blockqueue.push(succ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除不可达基本块指令
|
||||
for (auto blockIter = func->getBasicBlocks().begin(); blockIter != func->getBasicBlocks().end();blockIter++) {
|
||||
|
||||
if (!blockIter->get()->getreachable())
|
||||
for (auto &iterInst : blockIter->get()->getInstructions())
|
||||
usedelete(iterInst.get());
|
||||
|
||||
}
|
||||
|
||||
|
||||
for (auto blockIter = func->getBasicBlocks().begin(); blockIter != func->getBasicBlocks().end();) {
|
||||
if (!blockIter->get()->getreachable()) {
|
||||
for (auto succblock : blockIter->get()->getSuccessors()) {
|
||||
int indexphi = 1;
|
||||
for (auto pred : succblock->getPredecessors()) {
|
||||
if (pred == blockIter->get()) {
|
||||
break;
|
||||
}
|
||||
indexphi++;
|
||||
}
|
||||
for (auto &phiinst : succblock->getInstructions()) {
|
||||
if (phiinst->getKind() != Instruction::kPhi) {
|
||||
break;
|
||||
}
|
||||
phiinst->removeOperand(indexphi);
|
||||
}
|
||||
}
|
||||
// 删除不可达基本块,注意迭代器不可达问题
|
||||
func->removeBasicBlock((blockIter++)->get());
|
||||
} else {
|
||||
blockIter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SysYOptPre::SysYDelEmptyBlock() {
|
||||
auto &functions = pModule->getFunctions();
|
||||
for (auto &function : functions) {
|
||||
// 收集不可达基本块
|
||||
// 这里的不可达基本块是指没有实际指令的基本块
|
||||
// 当一个基本块没有实际指令例如只有phi指令和一个uncondbr指令时,也会被视作不可达
|
||||
auto basicBlocks = function.second->getBasicBlocks();
|
||||
std::map<sysy::BasicBlock *, BasicBlock *> EmptyBlocks;
|
||||
// 空块儿和后继的基本块的映射
|
||||
for (auto &basicBlock : basicBlocks) {
|
||||
if (basicBlock->getNumInstructions() == 0) {
|
||||
if (basicBlock->getNumSuccessors() == 1) {
|
||||
EmptyBlocks[basicBlock.get()] = basicBlock->getSuccessors().front();
|
||||
}
|
||||
}
|
||||
else{
|
||||
// 如果只有phi指令和一个uncondbr。(phi)*(uncondbr)?
|
||||
// 判断除了最后一个指令之外是不是只有phi指令
|
||||
bool onlyPhi = true;
|
||||
for (auto &inst : basicBlock->getInstructions()) {
|
||||
if (!inst->isPhi() && !inst->isUnconditional()) {
|
||||
onlyPhi = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(onlyPhi)
|
||||
EmptyBlocks[basicBlock.get()] = basicBlock->getSuccessors().front();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
// 更新基本块信息,增加必要指令
|
||||
for (auto &basicBlock : basicBlocks) {
|
||||
// 把空块转换成只有跳转指令的不可达块
|
||||
if (distance(basicBlock->begin(), basicBlock->end()) == 0) {
|
||||
if (basicBlock->getNumSuccessors() == 0) {
|
||||
continue;
|
||||
}
|
||||
if (basicBlock->getNumSuccessors() > 1) {
|
||||
assert("");
|
||||
}
|
||||
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||
pBuilder->createUncondBrInst(basicBlock->getSuccessors()[0], {});
|
||||
continue;
|
||||
}
|
||||
|
||||
auto thelastinst = basicBlock->getInstructions().end();
|
||||
--thelastinst;
|
||||
|
||||
// 根据br指令传递的后继块信息,跳过空块链
|
||||
if (thelastinst->get()->isUnconditional()) {
|
||||
BasicBlock* OldBrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||
BasicBlock *thelastBlockOld = nullptr;
|
||||
// 如果空块链表为多个块
|
||||
while (EmptyBlocks.find(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))) !=
|
||||
EmptyBlocks.end()) {
|
||||
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||
thelastinst->get()->replaceOperand(0, EmptyBlocks[thelastBlockOld]);
|
||||
}
|
||||
|
||||
basicBlock->removeSuccessor(OldBrBlock);
|
||||
OldBrBlock->removePredecessor(basicBlock.get());
|
||||
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0)));
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->addPredecessor(basicBlock.get());
|
||||
|
||||
if (thelastBlockOld != nullptr) {
|
||||
int indexphi = 0;
|
||||
for (auto &pred : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getPredecessors()) {
|
||||
if (pred == thelastBlockOld) {
|
||||
break;
|
||||
}
|
||||
indexphi++;
|
||||
}
|
||||
|
||||
// 更新phi指令的操作数
|
||||
// 移除thelastBlockOld对应的phi操作数
|
||||
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getInstructions()) {
|
||||
if (InstInNew->isPhi()) {
|
||||
dynamic_cast<PhiInst *>(InstInNew.get())->removeOperand(indexphi + 1);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else if (thelastinst->get()->getKind() == Instruction::kCondBr) {
|
||||
auto OldThenBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||
auto OldElseBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2));
|
||||
|
||||
BasicBlock *thelastBlockOld = nullptr;
|
||||
while (EmptyBlocks.find(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))) !=
|
||||
EmptyBlocks.end()) {
|
||||
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||
thelastinst->get()->replaceOperand(
|
||||
1, EmptyBlocks[dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))]);
|
||||
}
|
||||
basicBlock->removeSuccessor(OldThenBlock);
|
||||
OldThenBlock->removePredecessor(basicBlock.get());
|
||||
// 处理 then 和 else 分支合并的情况
|
||||
if (dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1)) ==
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))) {
|
||||
auto thebrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||
usedelete(thelastinst->get());
|
||||
thelastinst = basicBlock->getInstructions().erase(thelastinst);
|
||||
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||
pBuilder->createUncondBrInst(thebrBlock, {});
|
||||
continue;
|
||||
}
|
||||
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1)));
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))->addPredecessor(basicBlock.get());
|
||||
// auto indexInNew = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getPredecessors().
|
||||
|
||||
if (thelastBlockOld != nullptr) {
|
||||
int indexphi = 0;
|
||||
for (auto &pred : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))->getPredecessors()) {
|
||||
if (pred == thelastBlockOld) {
|
||||
break;
|
||||
}
|
||||
indexphi++;
|
||||
}
|
||||
|
||||
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1))->getInstructions()) {
|
||||
if (InstInNew->isPhi()) {
|
||||
dynamic_cast<PhiInst *>(InstInNew.get())->removeOperand(indexphi + 1);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
thelastBlockOld = nullptr;
|
||||
while (EmptyBlocks.find(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))) !=
|
||||
EmptyBlocks.end()) {
|
||||
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2));
|
||||
thelastinst->get()->replaceOperand(
|
||||
2, EmptyBlocks[dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))]);
|
||||
}
|
||||
basicBlock->removeSuccessor(OldElseBlock);
|
||||
OldElseBlock->removePredecessor(basicBlock.get());
|
||||
// 处理 then 和 else 分支合并的情况
|
||||
if (dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1)) ==
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))) {
|
||||
auto thebrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(1));
|
||||
usedelete(thelastinst->get());
|
||||
thelastinst = basicBlock->getInstructions().erase(thelastinst);
|
||||
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||
pBuilder->createUncondBrInst(thebrBlock, {});
|
||||
continue;
|
||||
}
|
||||
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2)));
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))->addPredecessor(basicBlock.get());
|
||||
|
||||
if (thelastBlockOld != nullptr) {
|
||||
int indexphi = 0;
|
||||
for (auto &pred : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))->getPredecessors()) {
|
||||
if (pred == thelastBlockOld) {
|
||||
break;
|
||||
}
|
||||
indexphi++;
|
||||
}
|
||||
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(2))->getInstructions()) {
|
||||
if (InstInNew->isPhi()) {
|
||||
dynamic_cast<PhiInst *>(InstInNew.get())->removeOperand(indexphi + 1);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (basicBlock->getNumSuccessors() == 1) {
|
||||
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||
pBuilder->createUncondBrInst(basicBlock->getSuccessors()[0], {});
|
||||
auto thelastinst = basicBlock->getInstructions().end();
|
||||
(--thelastinst);
|
||||
auto OldBrBlock = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||
sysy::BasicBlock *thelastBlockOld = nullptr;
|
||||
while (EmptyBlocks.find(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))) !=
|
||||
EmptyBlocks.end()) {
|
||||
thelastBlockOld = dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0));
|
||||
|
||||
thelastinst->get()->replaceOperand(
|
||||
0, EmptyBlocks[dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))]);
|
||||
}
|
||||
|
||||
basicBlock->removeSuccessor(OldBrBlock);
|
||||
OldBrBlock->removePredecessor(basicBlock.get());
|
||||
basicBlock->addSuccessor(dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0)));
|
||||
dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->addPredecessor(basicBlock.get());
|
||||
if (thelastBlockOld != nullptr) {
|
||||
int indexphi = 0;
|
||||
for (auto &pred : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getPredecessors()) {
|
||||
if (pred == thelastBlockOld) {
|
||||
break;
|
||||
}
|
||||
indexphi++;
|
||||
}
|
||||
|
||||
for (auto &InstInNew : dynamic_cast<BasicBlock *>(thelastinst->get()->getOperand(0))->getInstructions()) {
|
||||
if (InstInNew->isPhi()) {
|
||||
dynamic_cast<PhiInst *>(InstInNew.get())->removeOperand(indexphi + 1);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto iter = function.second->getBasicBlocks().begin(); iter != function.second->getBasicBlocks().end();) {
|
||||
|
||||
if (EmptyBlocks.find(iter->get()) != EmptyBlocks.end()) {
|
||||
// EntryBlock跳过
|
||||
if (iter->get() == function.second->getEntryBlock()) {
|
||||
++iter;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (auto &iterInst : iter->get()->getInstructions())
|
||||
usedelete(iterInst.get());
|
||||
// 删除不可达基本块的phi指令的操作数
|
||||
for (auto &succ : iter->get()->getSuccessors()) {
|
||||
int index = 0;
|
||||
for (auto &pred : succ->getPredecessors()) {
|
||||
if (pred == iter->get()) {
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
for (auto &instinsucc : succ->getInstructions()) {
|
||||
if (instinsucc->isPhi()) {
|
||||
dynamic_cast<PhiInst *>(instinsucc.get())->removeOperand(index);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function.second->removeBasicBlock((iter++)->get());
|
||||
} else {
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果函数没有返回指令,则添加一个默认返回指令(主要解决void函数没有返回指令的问题)
|
||||
void SysYOptPre::SysYAddReturn() {
|
||||
auto &functions = pModule->getFunctions();
|
||||
for (auto &function : functions) {
|
||||
auto &func = function.second;
|
||||
auto basicBlocks = func->getBasicBlocks();
|
||||
for (auto &block : basicBlocks) {
|
||||
if (block->getNumSuccessors() == 0) {
|
||||
// 如果基本块没有后继块,则添加一个返回指令
|
||||
if (block->getNumInstructions() == 0) {
|
||||
pBuilder->setPosition(block.get(), block->end());
|
||||
pBuilder->createReturnInst();
|
||||
}
|
||||
auto thelastinst = block->getInstructions().end();
|
||||
--thelastinst;
|
||||
if (thelastinst->get()->getKind() != Instruction::kReturn) {
|
||||
// std::cout << "Warning: Function " << func->getName() << " has no return instruction, adding default return." << std::endl;
|
||||
|
||||
pBuilder->setPosition(block.get(), block->end());
|
||||
// TODO: 如果int float函数缺少返回值是否需要报错
|
||||
if (func->getReturnType()->isInt()) {
|
||||
pBuilder->createReturnInst(ConstantInteger::get(0));
|
||||
} else if (func->getReturnType()->isFloat()) {
|
||||
pBuilder->createReturnInst(ConstantFloating::get(0.0F));
|
||||
} else {
|
||||
pBuilder->createReturnInst();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
@ -1,36 +0,0 @@
|
||||
// PassManager.cpp
|
||||
#include "SysYIRPassManager.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
void PassManager::run(Module& M) {
|
||||
// 首先运行Module级别的Pass
|
||||
for (auto& pass : modulePasses) {
|
||||
std::cout << "Running Module Pass: " << pass->getPassName() << std::endl;
|
||||
pass->runOnModule(M);
|
||||
}
|
||||
|
||||
// 然后对每个函数运行Function级别的Pass
|
||||
auto& functions = M.getFunctions();
|
||||
for (auto& pair : functions) {
|
||||
Function& F = *(pair.second); // 获取Function的引用
|
||||
std::cout << " Processing Function: " << F.getName() << std::endl;
|
||||
|
||||
// 在每个函数上运行FunctionPasses
|
||||
bool changedInFunction;
|
||||
do {
|
||||
changedInFunction = false;
|
||||
for (auto& pass : functionPasses) {
|
||||
// 对于FunctionPasses,可以考虑一个迭代执行的循环,直到稳定
|
||||
std::cout << " Running Function Pass: " << pass->getPassName() << std::endl;
|
||||
changedInFunction |= pass->runOnFunction(F);
|
||||
}
|
||||
} while (changedInFunction); // 循环直到函数稳定,这模拟了您SysYCFGOpt的while(changed)逻辑
|
||||
}
|
||||
|
||||
// 分析Pass的运行可以在其他Pass需要时触发,或者在特定的PassManager阶段触发
|
||||
// 对于依赖于分析结果的Pass,可以在其run方法中通过PassManager::getAnalysis()来获取
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
@ -3,11 +3,12 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "IR.h" // 确保IR.h包含了ArrayType、GetElementPtrInst等的定义
|
||||
#include "IR.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
void SysYPrinter::printIR() {
|
||||
|
||||
const auto &functions = pModule->getFunctions();
|
||||
|
||||
//TODO: Print target datalayout and triple (minimal required by LLVM)
|
||||
@ -35,18 +36,11 @@ std::string SysYPrinter::getTypeString(Type *type) {
|
||||
return "i32";
|
||||
} else if (type->isFloat()) {
|
||||
return "float";
|
||||
|
||||
} else if (auto ptrType = dynamic_cast<PointerType*>(type)) {
|
||||
// 递归打印指针指向的类型,然后加上 '*'
|
||||
return getTypeString(ptrType->getBaseType()) + "*";
|
||||
} else if (auto funcType = dynamic_cast<FunctionType*>(type)) {
|
||||
// 对于函数类型,打印其返回类型
|
||||
// 注意:这里可能需要更完整的函数签名打印,取决于你的IR表示方式
|
||||
// 比如:`retType (paramType1, paramType2, ...)`
|
||||
// 但为了简化和LLVM IR兼容性,通常在定义时完整打印
|
||||
return getTypeString(funcType->getReturnType());
|
||||
} else if (auto arrayType = dynamic_cast<ArrayType*>(type)) { // 新增:处理数组类型
|
||||
// 打印格式为 [num_elements x element_type]
|
||||
return "[" + std::to_string(arrayType->getNumElements()) + " x " + getTypeString(arrayType->getElementType()) + "]";
|
||||
} else if (auto ptrType = dynamic_cast<FunctionType*>(type)) {
|
||||
return getTypeString(ptrType->getReturnType());
|
||||
}
|
||||
assert(false && "Unsupported type");
|
||||
return "";
|
||||
@ -57,25 +51,15 @@ std::string SysYPrinter::getValueName(Value *value) {
|
||||
return "@" + global->getName();
|
||||
} else if (auto inst = dynamic_cast<Instruction*>(value)) {
|
||||
return "%" + inst->getName();
|
||||
} else if (auto constInt = dynamic_cast<ConstantInteger*>(value)) { // 优先匹配具体的常量类型
|
||||
return std::to_string(constInt->getInt());
|
||||
} else if (auto constFloat = dynamic_cast<ConstantFloating*>(value)) { // 优先匹配具体的常量类型
|
||||
return std::to_string(constFloat->getFloat());
|
||||
} else if (auto constUndef = dynamic_cast<UndefinedValue*>(value)) { // 如果有Undef类型
|
||||
return "undef";
|
||||
} else if (auto constVal = dynamic_cast<ConstantValue*>(value)) { // fallback for generic ConstantValue
|
||||
// 这里的逻辑可能需要根据你ConstantValue的实际设计调整
|
||||
// 确保它能处理所有可能的ConstantValue
|
||||
if (constVal->getType()->isFloat()) {
|
||||
return std::to_string(constVal->getFloat());
|
||||
} else if (auto constVal = dynamic_cast<ConstantValue*>(value)) {
|
||||
if (constVal->isFloat()) {
|
||||
return std::to_string(constVal->getFloat());
|
||||
}
|
||||
return std::to_string(constVal->getInt());
|
||||
} else if (auto constVar = dynamic_cast<ConstantVariable*>(value)) {
|
||||
return constVar->getName(); // 假设ConstantVariable有自己的名字或通过getByIndices获取值
|
||||
} else if (auto argVar = dynamic_cast<Argument*>(value)) {
|
||||
return "%" + argVar->getName(); // 假设ArgumentVariable有自己的名字
|
||||
return constVar->getName();
|
||||
}
|
||||
assert(false && "Unknown value type or unable to get value name");
|
||||
assert(false && "Unknown value type");
|
||||
return "";
|
||||
}
|
||||
|
||||
@ -93,35 +77,44 @@ void SysYPrinter::printGlobalVariable() {
|
||||
for (const auto &global : globals) {
|
||||
std::cout << "@" << global->getName() << " = global ";
|
||||
|
||||
// 全局变量的类型是一个指针,指向其基类型 (可能是 ArrayType 或 Integer/FloatType)
|
||||
auto globalVarBaseType = dynamic_cast<PointerType *>(global->getType())->getBaseType();
|
||||
printType(globalVarBaseType); // 打印全局变量的实际类型 (例如 i32 或 [10 x i32])
|
||||
auto baseType = dynamic_cast<PointerType *>(global->getType())->getBaseType();
|
||||
printType(baseType);
|
||||
|
||||
if (global->getNumDims() > 0) {
|
||||
// Array type
|
||||
std::cout << " [";
|
||||
for (unsigned i = 0; i < global->getNumDims(); i++) {
|
||||
if (i > 0) std::cout << " x ";
|
||||
std::cout << getValueName(global->getDim(i));
|
||||
}
|
||||
std::cout << "]";
|
||||
}
|
||||
|
||||
std::cout << " ";
|
||||
|
||||
// 检查是否是数组类型 (通过检查 globalVarBaseType 是否是 ArrayType)
|
||||
if (globalVarBaseType->isArray()) {
|
||||
// 数组初始化器
|
||||
std::cout << "["; // LLVM IR 数组初始化器格式: [type value, type value, ...]
|
||||
auto values = global->getInitValues(); // 假设 getInitValues() 返回一个 ValueCounter
|
||||
const std::vector<sysy::Value *> &counterValues = values.getValues(); // 获取所有值
|
||||
if (global->getNumDims() > 0) {
|
||||
// Array initializer
|
||||
std::cout << "[";
|
||||
auto values = global->getInitValues();
|
||||
auto counterValues = values.getValues();
|
||||
auto counterNumbers = values.getNumbers();
|
||||
|
||||
for (size_t i = 0; i < counterValues.size(); i++) {
|
||||
for (size_t i = 0; i < counterNumbers.size(); i++) {
|
||||
if (i > 0) std::cout << ", ";
|
||||
// 打印元素类型,这个元素类型应该是数组的最终元素类型,例如 i32 或 float
|
||||
// 可以从 globalVarBaseType 逐层剥离得到最终元素类型,但这里简化为直接从值获取
|
||||
printType(counterValues[i]->getType());
|
||||
std::cout << " ";
|
||||
printValue(counterValues[i]);
|
||||
if (baseType->isFloat()) {
|
||||
std::cout << "float " << dynamic_cast<ConstantValue*>(counterValues[i])->getFloat();
|
||||
} else {
|
||||
std::cout << "i32 " << dynamic_cast<ConstantValue*>(counterValues[i])->getInt();
|
||||
}
|
||||
}
|
||||
std::cout << "]";
|
||||
} else {
|
||||
// 标量初始化器
|
||||
// 假设标量全局变量的初始化值通过 getByIndex(0) 获取
|
||||
Value* initVal = global->getByIndex(0);
|
||||
printType(initVal->getType()); // 打印标量值的类型
|
||||
std::cout << " ";
|
||||
printValue(initVal); // 打印标量值
|
||||
// Scalar initializer
|
||||
if (baseType->isFloat()) {
|
||||
std::cout << "float " << dynamic_cast<ConstantValue*>(global->getByIndex(0))->getFloat();
|
||||
} else {
|
||||
std::cout << "i32 " << dynamic_cast<ConstantValue*>(global->getByIndex(0))->getInt();
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << ", align 4" << std::endl;
|
||||
@ -136,7 +129,7 @@ void SysYPrinter::printFunction(Function *function) {
|
||||
|
||||
auto entryBlock = function->getEntryBlock();
|
||||
const auto &args_types = function->getParamTypes();
|
||||
auto &args = function->getArguments();
|
||||
auto &args = entryBlock->getArguments();
|
||||
|
||||
int i = 0;
|
||||
for (const auto &args_type : args_types) {
|
||||
@ -152,7 +145,9 @@ void SysYPrinter::printFunction(Function *function) {
|
||||
for (const auto &blockIter : function->getBasicBlocks()) {
|
||||
// Basic block label
|
||||
BasicBlock* blockPtr = blockIter.get();
|
||||
if (!blockPtr->getName().empty()) {
|
||||
if (blockPtr == function->getEntryBlock()) {
|
||||
std::cout << "entry:" << std::endl;
|
||||
} else if (!blockPtr->getName().empty()) {
|
||||
std::cout << blockPtr->getName() << ":" << std::endl;
|
||||
}
|
||||
|
||||
@ -214,19 +209,19 @@ void SysYPrinter::printInst(Instruction *pInst) {
|
||||
case Kind::kFDiv: std::cout << "fdiv"; break;
|
||||
case Kind::kICmpEQ: std::cout << "icmp eq"; break;
|
||||
case Kind::kICmpNE: std::cout << "icmp ne"; break;
|
||||
case Kind::kICmpLT: std::cout << "icmp slt"; break; // LLVM uses slt/sgt for signed less/greater than
|
||||
case Kind::kICmpLT: std::cout << "icmp slt"; break;
|
||||
case Kind::kICmpGT: std::cout << "icmp sgt"; break;
|
||||
case Kind::kICmpLE: std::cout << "icmp sle"; break;
|
||||
case Kind::kICmpGE: std::cout << "icmp sge"; break;
|
||||
case Kind::kFCmpEQ: std::cout << "fcmp oeq"; break; // oeq for ordered equal
|
||||
case Kind::kFCmpNE: std::cout << "fcmp one"; break; // one for ordered not equal
|
||||
case Kind::kFCmpLT: std::cout << "fcmp olt"; break; // olt for ordered less than
|
||||
case Kind::kFCmpGT: std::cout << "fcmp ogt"; break; // ogt for ordered greater than
|
||||
case Kind::kFCmpLE: std::cout << "fcmp ole"; break; // ole for ordered less than or equal
|
||||
case Kind::kFCmpGE: std::cout << "fcmp oge"; break; // oge for ordered greater than or equal
|
||||
case Kind::kFCmpEQ: std::cout << "fcmp oeq"; break;
|
||||
case Kind::kFCmpNE: std::cout << "fcmp one"; break;
|
||||
case Kind::kFCmpLT: std::cout << "fcmp olt"; break;
|
||||
case Kind::kFCmpGT: std::cout << "fcmp ogt"; break;
|
||||
case Kind::kFCmpLE: std::cout << "fcmp ole"; break;
|
||||
case Kind::kFCmpGE: std::cout << "fcmp oge"; break;
|
||||
case Kind::kAnd: std::cout << "and"; break;
|
||||
case Kind::kOr: std::cout << "or"; break;
|
||||
default: break; // Should not reach here
|
||||
default: break;
|
||||
}
|
||||
|
||||
// Types and operands
|
||||
@ -243,6 +238,7 @@ void SysYPrinter::printInst(Instruction *pInst) {
|
||||
case Kind::kNeg:
|
||||
case Kind::kNot:
|
||||
case Kind::kFNeg:
|
||||
case Kind::kFNot:
|
||||
case Kind::kFtoI:
|
||||
case Kind::kBitFtoI:
|
||||
case Kind::kItoF:
|
||||
@ -254,39 +250,31 @@ void SysYPrinter::printInst(Instruction *pInst) {
|
||||
}
|
||||
|
||||
switch (pInst->getKind()) {
|
||||
case Kind::kNeg: std::cout << "sub "; break; // integer negation is `sub i32 0, operand`
|
||||
case Kind::kNot: std::cout << "xor "; break; // logical/bitwise NOT is `xor i32 -1, operand` or `xor i1 true, operand`
|
||||
case Kind::kFNeg: std::cout << "fneg "; break; // float negation
|
||||
case Kind::kFtoI: std::cout << "fptosi "; break; // float to signed integer
|
||||
case Kind::kBitFtoI: std::cout << "bitcast "; break; // bitcast float to int
|
||||
case Kind::kItoF: std::cout << "sitofp "; break; // signed integer to float
|
||||
case Kind::kBitItoF: std::cout << "bitcast "; break; // bitcast int to float
|
||||
default: break; // Should not reach here
|
||||
case Kind::kNeg: std::cout << "sub "; break;
|
||||
case Kind::kNot: std::cout << "not "; break;
|
||||
case Kind::kFNeg: std::cout << "fneg "; break;
|
||||
case Kind::kFNot: std::cout << "fneg "; break; // FNot not standard, map to fneg
|
||||
case Kind::kFtoI: std::cout << "fptosi "; break;
|
||||
case Kind::kBitFtoI: std::cout << "bitcast "; break;
|
||||
case Kind::kItoF: std::cout << "sitofp "; break;
|
||||
case Kind::kBitItoF: std::cout << "bitcast "; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
printType(unyInst->getOperand()->getType()); // Print operand type
|
||||
printType(unyInst->getType());
|
||||
std::cout << " ";
|
||||
|
||||
// Special handling for integer negation and logical NOT
|
||||
if (pInst->getKind() == Kind::kNeg) {
|
||||
std::cout << "0, "; // for 'sub i32 0, operand'
|
||||
} else if (pInst->getKind() == Kind::kNot) {
|
||||
// For logical NOT (i1 -> i1), use 'xor i1 true, operand'
|
||||
// For bitwise NOT (i32 -> i32), use 'xor i32 -1, operand'
|
||||
if (unyInst->getOperand()->getType()->isInt()) { // Assuming i32 for bitwise NOT
|
||||
std::cout << "NOT, "; // or specific bitmask for NOT
|
||||
} else { // Assuming i1 for logical NOT
|
||||
std::cout << "true, ";
|
||||
}
|
||||
// Special handling for negation
|
||||
if (pInst->getKind() == Kind::kNeg || pInst->getKind() == Kind::kNot) {
|
||||
std::cout << "i32 0, ";
|
||||
}
|
||||
|
||||
printValue(pInst->getOperand(0));
|
||||
|
||||
// For type conversions (fptosi, sitofp, bitcast), need to specify destination type
|
||||
if (pInst->getKind() == Kind::kFtoI || pInst->getKind() == Kind::kItoF ||
|
||||
pInst->getKind() == Kind::kBitFtoI || pInst->getKind() == Kind::kBitItoF) {
|
||||
// For bitcast, need to specify destination type
|
||||
if (pInst->getKind() == Kind::kBitFtoI || pInst->getKind() == Kind::kBitItoF) {
|
||||
std::cout << " to ";
|
||||
printType(unyInst->getType()); // Print result type
|
||||
printType(unyInst->getType());
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
@ -301,7 +289,7 @@ void SysYPrinter::printInst(Instruction *pInst) {
|
||||
}
|
||||
|
||||
std::cout << "call ";
|
||||
printType(callInst->getType()); // Return type of the call
|
||||
printType(callInst->getType());
|
||||
std::cout << " @" << function->getName() << "(";
|
||||
|
||||
auto params = callInst->getArguments();
|
||||
@ -309,9 +297,9 @@ void SysYPrinter::printInst(Instruction *pInst) {
|
||||
for (auto ¶m : params) {
|
||||
if (!first) std::cout << ", ";
|
||||
first = false;
|
||||
printType(param->getValue()->getType()); // Type of argument
|
||||
printType(param->getValue()->getType());
|
||||
std::cout << " ";
|
||||
printValue(param->getValue()); // Value of argument
|
||||
printValue(param->getValue());
|
||||
}
|
||||
|
||||
std::cout << ")" << std::endl;
|
||||
@ -319,7 +307,7 @@ void SysYPrinter::printInst(Instruction *pInst) {
|
||||
|
||||
case Kind::kCondBr: {
|
||||
auto condBrInst = dynamic_cast<CondBrInst *>(pInst);
|
||||
std::cout << "br i1 "; // Condition type should be i1
|
||||
std::cout << "br i1 ";
|
||||
printValue(condBrInst->getCondition());
|
||||
std::cout << ", label %" << condBrInst->getThenBlock()->getName();
|
||||
std::cout << ", label %" << condBrInst->getElseBlock()->getName();
|
||||
@ -349,17 +337,14 @@ void SysYPrinter::printInst(Instruction *pInst) {
|
||||
auto allocaInst = dynamic_cast<AllocaInst *>(pInst);
|
||||
std::cout << "%" << allocaInst->getName() << " = alloca ";
|
||||
|
||||
// AllocaInst 的类型现在应该是一个 PointerType,指向正确的 ArrayType 或 ScalarType
|
||||
// 例如:alloca i32, align 4 或者 alloca [10 x i32], align 4
|
||||
auto allocatedType = dynamic_cast<PointerType *>(allocaInst->getType())->getBaseType();
|
||||
printType(allocatedType);
|
||||
auto baseType = dynamic_cast<PointerType *>(allocaInst->getType())->getBaseType();
|
||||
printType(baseType);
|
||||
|
||||
// 仍然打印维度信息,如果存在的话
|
||||
if (allocaInst->getNumDims() > 0) {
|
||||
if (allocaInst->getNumDims() > 0) {
|
||||
std::cout << ", ";
|
||||
for (size_t i = 0; i < allocaInst->getNumDims(); i++) {
|
||||
if (i > 0) std::cout << ", ";
|
||||
printType(Type::getIntType()); // 维度大小通常是 i32 类型
|
||||
printType(Type::getIntType());
|
||||
std::cout << " ";
|
||||
printValue(allocaInst->getDim(i));
|
||||
}
|
||||
@ -371,74 +356,70 @@ void SysYPrinter::printInst(Instruction *pInst) {
|
||||
case Kind::kLoad: {
|
||||
auto loadInst = dynamic_cast<LoadInst *>(pInst);
|
||||
std::cout << "%" << loadInst->getName() << " = load ";
|
||||
printType(loadInst->getType()); // 加载的结果类型
|
||||
printType(loadInst->getType());
|
||||
std::cout << ", ";
|
||||
printType(loadInst->getPointer()->getType()); // 指针类型
|
||||
printType(loadInst->getPointer()->getType());
|
||||
std::cout << " ";
|
||||
printValue(loadInst->getPointer()); // 要加载的地址
|
||||
printValue(loadInst->getPointer());
|
||||
|
||||
// 仍然打印索引信息,如果存在的话
|
||||
if (loadInst->getNumIndices() > 0) {
|
||||
std::cout << ", indices "; // 或者其他分隔符,取决于你期望的格式
|
||||
std::cout << ", ";
|
||||
for (size_t i = 0; i < loadInst->getNumIndices(); i++) {
|
||||
if (i > 0) std::cout << ", ";
|
||||
printType(loadInst->getIndex(i)->getType());
|
||||
std::cout << " ";
|
||||
printValue(loadInst->getIndex(i));
|
||||
if (i > 0) std::cout << ", ";
|
||||
printType(Type::getIntType());
|
||||
std::cout << " ";
|
||||
printValue(loadInst->getIndex(i));
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << ", align 4" << std::endl;
|
||||
} break;
|
||||
|
||||
case Kind::kLa: {
|
||||
auto laInst = dynamic_cast<LaInst *>(pInst);
|
||||
std::cout << "%" << laInst->getName() << " = getelementptr inbounds ";
|
||||
|
||||
auto ptrType = dynamic_cast<PointerType*>(laInst->getPointer()->getType());
|
||||
printType(ptrType->getBaseType());
|
||||
std::cout << ", ";
|
||||
printType(laInst->getPointer()->getType());
|
||||
std::cout << " ";
|
||||
printValue(laInst->getPointer());
|
||||
std::cout << ", ";
|
||||
|
||||
for (size_t i = 0; i < laInst->getNumIndices(); i++) {
|
||||
if (i > 0) std::cout << ", ";
|
||||
printType(Type::getIntType());
|
||||
std::cout << " ";
|
||||
printValue(laInst->getIndex(i));
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
} break;
|
||||
|
||||
case Kind::kStore: {
|
||||
auto storeInst = dynamic_cast<StoreInst *>(pInst);
|
||||
std::cout << "store ";
|
||||
printType(storeInst->getValue()->getType()); // 要存储的值的类型
|
||||
printType(storeInst->getValue()->getType());
|
||||
std::cout << " ";
|
||||
printValue(storeInst->getValue()); // 要存储的值
|
||||
printValue(storeInst->getValue());
|
||||
std::cout << ", ";
|
||||
printType(storeInst->getPointer()->getType()); // 目标指针的类型
|
||||
printType(storeInst->getPointer()->getType());
|
||||
std::cout << " ";
|
||||
printValue(storeInst->getPointer()); // 目标地址
|
||||
printValue(storeInst->getPointer());
|
||||
|
||||
// 仍然打印索引信息,如果存在的话
|
||||
if (storeInst->getNumIndices() > 0) {
|
||||
std::cout << ", indices "; // 或者其他分隔符
|
||||
std::cout << ", ";
|
||||
for (size_t i = 0; i < storeInst->getNumIndices(); i++) {
|
||||
if (i > 0) std::cout << ", ";
|
||||
printType(storeInst->getIndex(i)->getType());
|
||||
std::cout << " ";
|
||||
printValue(storeInst->getIndex(i));
|
||||
if (i > 0) std::cout << ", ";
|
||||
printType(Type::getIntType());
|
||||
std::cout << " ";
|
||||
printValue(storeInst->getIndex(i));
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << ", align 4" << std::endl;
|
||||
} break;
|
||||
|
||||
case Kind::kGetElementPtr: { // 新增:GetElementPtrInst 打印
|
||||
auto gepInst = dynamic_cast<GetElementPtrInst*>(pInst);
|
||||
std::cout << "%" << gepInst->getName() << " = getelementptr inbounds "; // 假设总是 inbounds
|
||||
|
||||
// GEP 的第一个操作数是基指针,其类型是一个指向聚合类型的指针
|
||||
// 第一个参数是基指针所指向的聚合类型的类型 (e.g., [10 x i32])
|
||||
auto basePtrType = dynamic_cast<PointerType*>(gepInst->getBasePointer()->getType());
|
||||
printType(basePtrType->getBaseType()); // 打印基指针指向的类型
|
||||
|
||||
std::cout << ", ";
|
||||
printType(gepInst->getBasePointer()->getType()); // 打印基指针自身的类型 (e.g., [10 x i32]*)
|
||||
std::cout << " ";
|
||||
printValue(gepInst->getBasePointer()); // 打印基指针
|
||||
|
||||
// 打印所有索引
|
||||
for (auto indexVal : gepInst->getIndices()) { // 使用 getIndices() 迭代器
|
||||
std::cout << ", ";
|
||||
printType(indexVal->getValue()->getType()); // 打印索引的类型 (通常是 i32)
|
||||
std::cout << " ";
|
||||
printValue(indexVal->getValue()); // 打印索引值
|
||||
}
|
||||
std::cout << std::endl;
|
||||
} break;
|
||||
|
||||
case Kind::kMemset: {
|
||||
auto memsetInst = dynamic_cast<MemsetInst *>(pInst);
|
||||
@ -452,40 +433,51 @@ void SysYPrinter::printInst(Instruction *pInst) {
|
||||
printValue(memsetInst->getValue());
|
||||
std::cout << ", i32 ";
|
||||
printValue(memsetInst->getSize());
|
||||
std::cout << ", i1 false)" << std::endl; // alignment for memset is typically i1
|
||||
std::cout << ", i1 false)" << std::endl;
|
||||
} break;
|
||||
|
||||
case Kind::kPhi: {
|
||||
auto phiInst = dynamic_cast<PhiInst *>(pInst);
|
||||
// Phi 指令的名称通常是结果变量
|
||||
std::cout << "%" << phiInst->getName() << " = phi ";
|
||||
printType(phiInst->getType()); // Phi 结果类型
|
||||
printValue(phiInst->getOperand(0));
|
||||
std::cout << " = phi ";
|
||||
printType(phiInst->getType());
|
||||
|
||||
// Phi 指令的操作数是成对的 [value, basic_block]
|
||||
// 这里假设 getOperands() 返回的是 (val1, block1, val2, block2...)
|
||||
// 如果你的 PhiInst 存储方式是 getIncomingValues() 和 getIncomingBlocks(),请相应调整
|
||||
// LLVM IR 格式: phi type [value1, block1], [value2, block2]
|
||||
bool firstPair = true;
|
||||
for (unsigned i = 0; i < phiInst->getNumOperands() / 2; ++i) { // 遍历成对的操作数
|
||||
if (!firstPair) std::cout << ", ";
|
||||
firstPair = false;
|
||||
for (unsigned i = 1; i < phiInst->getNumOperands(); i++) {
|
||||
if (i > 0) std::cout << ", ";
|
||||
std::cout << "[ ";
|
||||
printValue(phiInst->getOperand(i * 2)); // value
|
||||
std::cout << ", %";
|
||||
printValue(phiInst->getOperand(i * 2 + 1)); // block
|
||||
printValue(phiInst->getOperand(i));
|
||||
std::cout << " ]";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
} break;
|
||||
|
||||
// 以下两个 Kind 应该删除或替换为 kGEP
|
||||
// case Kind::kLa: { /* REMOVED */ } break;
|
||||
// case Kind::kGetSubArray: { /* REMOVED */ } break;
|
||||
case Kind::kGetSubArray: {
|
||||
auto getSubArrayInst = dynamic_cast<GetSubArrayInst *>(pInst);
|
||||
std::cout << "%" << getSubArrayInst->getName() << " = getelementptr inbounds ";
|
||||
|
||||
auto ptrType = dynamic_cast<PointerType*>(getSubArrayInst->getFatherArray()->getType());
|
||||
printType(ptrType->getBaseType());
|
||||
std::cout << ", ";
|
||||
printType(getSubArrayInst->getFatherArray()->getType());
|
||||
std::cout << " ";
|
||||
printValue(getSubArrayInst->getFatherArray());
|
||||
std::cout << ", ";
|
||||
bool firstIndex = true;
|
||||
for (auto &index : getSubArrayInst->getIndices()) {
|
||||
if (!firstIndex) std::cout << ", ";
|
||||
firstIndex = false;
|
||||
printType(Type::getIntType());
|
||||
std::cout << " ";
|
||||
printValue(index->getValue());
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
} break;
|
||||
|
||||
default:
|
||||
assert(false && "Unsupported instruction kind in SysYPrinter");
|
||||
assert(false && "Unsupported instruction kind");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
} // namespace sysy
|
||||
|
||||
@ -1,63 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Pass.h"
|
||||
#include "IR.h"
|
||||
#include "SysYIROptUtils.h"
|
||||
#include "Dom.h"
|
||||
#include <unordered_set>
|
||||
#include <queue>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 前向声明分析结果类,确保在需要时可以引用
|
||||
// class DominatorTreeAnalysisResult; // Pass.h 中已包含,这里不再需要
|
||||
class SideEffectInfoAnalysisResult; // 假设有副作用分析结果类
|
||||
|
||||
// DCEContext 类,用于封装DCE的内部逻辑和状态
|
||||
// 这样可以避免静态变量在多线程或多次运行时的冲突,并保持代码的模块化
|
||||
class DCEContext {
|
||||
public:
|
||||
// 运行DCE的主要方法
|
||||
// func: 当前要优化的函数
|
||||
// tp: 分析管理器,用于获取其他分析结果(如果需要)
|
||||
void run(Function* func, AnalysisManager* AM, bool &changed);
|
||||
|
||||
private:
|
||||
// 存储活跃指令的集合
|
||||
std::unordered_set<Instruction*> alive_insts;
|
||||
|
||||
// 判断指令是否是“天然活跃”的(即总是保留的)
|
||||
// inst: 要检查的指令
|
||||
// 返回值: 如果指令是天然活跃的,则为true,否则为false
|
||||
bool isAlive(Instruction* inst);
|
||||
|
||||
// 递归地将活跃指令及其依赖加入到 alive_insts 集合中
|
||||
// inst: 要标记为活跃的指令
|
||||
void addAlive(Instruction* inst);
|
||||
};
|
||||
|
||||
// DCE 优化遍类,继承自 OptimizationPass
|
||||
class DCE : public OptimizationPass {
|
||||
public:
|
||||
// 构造函数
|
||||
DCE() : OptimizationPass("DCE", Granularity::Function) {}
|
||||
|
||||
// 静态成员,作为该遍的唯一ID
|
||||
static void *ID;
|
||||
|
||||
// 运行在函数上的优化逻辑
|
||||
// F: 当前要优化的函数
|
||||
// AM: 分析管理器,用于获取或使分析结果失效
|
||||
// 返回值: 如果IR被修改,则为true,否则为false
|
||||
bool runOnFunction(Function *F, AnalysisManager& AM) override;
|
||||
|
||||
// 声明该遍的分析依赖和失效信息
|
||||
// analysisDependencies: 该遍运行前需要哪些分析结果
|
||||
// analysisInvalidations: 该遍运行后会使哪些分析结果失效
|
||||
void getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const override;
|
||||
|
||||
// Pass 基类中的纯虚函数,必须实现
|
||||
void *getPassID() const override { return &ID; }
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
39
src/include/DeadCodeElimination.h
Normal file
39
src/include/DeadCodeElimination.h
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include "IR.h"
|
||||
#include "SysYIRAnalyser.h"
|
||||
#include "SysYIRPrinter.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
class DeadCodeElimination {
|
||||
private:
|
||||
Module *pModule;
|
||||
ControlFlowAnalysis *pCFA; // 控制流分析指针
|
||||
ActiveVarAnalysis *pAVA; // 活跃变量分析指针
|
||||
DataFlowAnalysisUtils dataFlowAnalysisUtils; // 数据流分析工具类
|
||||
|
||||
public:
|
||||
explicit DeadCodeElimination(Module *pMoudle,
|
||||
ControlFlowAnalysis *pCFA = nullptr,
|
||||
ActiveVarAnalysis *pAVA = nullptr)
|
||||
: pModule(pMoudle), pCFA(pCFA), pAVA(pAVA), dataFlowAnalysisUtils() {} // 构造函数
|
||||
|
||||
// TODO:根据参数传入的passes来运行不同的死代码删除流程
|
||||
// void runDCEPipeline(const std::vector<std::string>& passes = {
|
||||
// "dead-store", "redundant-load-store", "dead-load", "dead-alloca", "dead-global"
|
||||
// });
|
||||
void runDCEPipeline(); // 运行死代码删除
|
||||
|
||||
void eliminateDeadStores(Function* func, bool& changed); // 消除无用存储
|
||||
void eliminateDeadLoads(Function* func, bool& changed); // 消除无用加载
|
||||
void eliminateDeadAllocas(Function* func, bool& changed); // 消除无用内存分配
|
||||
void eliminateDeadGlobals(bool& changed); // 消除无用全局变量
|
||||
void eliminateDeadIndirectiveAllocas(Function* func, bool& changed); // 消除无用间接内存分配(phi节点)
|
||||
void eliminateDeadRedundantLoadStore(Function* func, bool& changed); // 消除冗余加载和存储
|
||||
bool isGlobal(Value *val);
|
||||
bool isArr(Value *val);
|
||||
void usedelete(Instruction *instr);
|
||||
|
||||
};
|
||||
} // namespace sysy
|
||||
@ -1,52 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Pass.h" // 包含 Pass 框架
|
||||
#include "IR.h" // 包含 IR 定义
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 支配树分析结果类 (保持不变)
|
||||
class DominatorTree : public AnalysisResultBase {
|
||||
public:
|
||||
DominatorTree(Function* F);
|
||||
const std::set<BasicBlock*>* getDominators(BasicBlock* BB) const;
|
||||
BasicBlock* getImmediateDominator(BasicBlock* BB) const;
|
||||
const std::set<BasicBlock*>* getDominanceFrontier(BasicBlock* BB) const;
|
||||
const std::map<BasicBlock*, std::set<BasicBlock*>>& getDominatorsMap() const { return Dominators; }
|
||||
const std::map<BasicBlock*, BasicBlock*>& getIDomsMap() const { return IDoms; }
|
||||
const std::map<BasicBlock*, std::set<BasicBlock*>>& getDominanceFrontiersMap() const { return DominanceFrontiers; }
|
||||
void computeDominators(Function* F);
|
||||
void computeIDoms(Function* F);
|
||||
void computeDominanceFrontiers(Function* F);
|
||||
private:
|
||||
Function* AssociatedFunction;
|
||||
std::map<BasicBlock*, std::set<BasicBlock*>> Dominators;
|
||||
std::map<BasicBlock*, BasicBlock*> IDoms;
|
||||
std::map<BasicBlock*, std::set<BasicBlock*>> DominanceFrontiers;
|
||||
};
|
||||
|
||||
|
||||
// 支配树分析遍
|
||||
class DominatorTreeAnalysisPass : public AnalysisPass {
|
||||
public:
|
||||
// 唯一的 Pass ID
|
||||
static void *ID;
|
||||
|
||||
DominatorTreeAnalysisPass() : AnalysisPass("DominatorTreeAnalysis", Pass::Granularity::Function) {}
|
||||
|
||||
// 实现 getPassID
|
||||
void* getPassID() const override { return &ID; }
|
||||
|
||||
bool runOnFunction(Function* F, AnalysisManager &AM) override;
|
||||
|
||||
std::unique_ptr<AnalysisResultBase> getResult() override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<DominatorTree> CurrentDominatorTree;
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
307
src/include/IR.h
307
src/include/IR.h
@ -49,7 +49,6 @@ class Type {
|
||||
kLabel,
|
||||
kPointer,
|
||||
kFunction,
|
||||
kArray,
|
||||
};
|
||||
|
||||
Kind kind; ///< 表示具体类型的变量
|
||||
@ -66,7 +65,6 @@ class Type {
|
||||
static Type* getPointerType(Type *baseType); ///< 返回表示指向baseType类型的Pointer类型的Type指针
|
||||
static Type* getFunctionType(Type *returnType, const std::vector<Type *> ¶mTypes = {});
|
||||
///< 返回表示返回类型为returnType,形参类型列表为paramTypes的函数类型的Type指针
|
||||
static Type* getArrayType(Type *elementType, unsigned numElements);
|
||||
|
||||
public:
|
||||
Kind getKind() const { return kind; } ///< 返回Type对象代表原始标量类型
|
||||
@ -76,7 +74,6 @@ class Type {
|
||||
bool isLabel() const { return kind == kLabel; } ///< 判定是否为Label类型
|
||||
bool isPointer() const { return kind == kPointer; } ///< 判定是否为Pointer类型
|
||||
bool isFunction() const { return kind == kFunction; } ///< 判定是否为Function类型
|
||||
bool isArray() const { return kind == Kind::kArray; }
|
||||
unsigned getSize() const; ///< 返回类型所占的空间大小(字节)
|
||||
/// 尝试将一个变量转换为给定的Type及其派生类类型的变量
|
||||
template <typename T>
|
||||
@ -118,22 +115,6 @@ class FunctionType : public Type {
|
||||
unsigned getNumParams() const { return paramTypes.size(); } ///< 获取形参数量
|
||||
};
|
||||
|
||||
class ArrayType : public Type {
|
||||
public:
|
||||
// elements:数组的元素类型 (例如,int[3] 的 elementType 是 int)
|
||||
// numElements:该维度的大小 (例如,int[3] 的 numElements 是 3)
|
||||
static ArrayType *get(Type *elementType, unsigned numElements);
|
||||
|
||||
Type *getElementType() const { return elementType; }
|
||||
unsigned getNumElements() const { return numElements; }
|
||||
|
||||
protected:
|
||||
ArrayType(Type *elementType, unsigned numElements)
|
||||
: Type(Kind::kArray), elementType(elementType), numElements(numElements) {}
|
||||
Type *elementType;
|
||||
unsigned numElements; // 当前维度的大小
|
||||
};
|
||||
|
||||
/*!
|
||||
* @}
|
||||
*/
|
||||
@ -468,6 +449,7 @@ public:
|
||||
|
||||
// --- End of refactored ConstantValue and related classes ---
|
||||
|
||||
|
||||
class Instruction;
|
||||
class Function;
|
||||
class BasicBlock;
|
||||
@ -486,6 +468,7 @@ public:
|
||||
|
||||
using inst_list = std::list<std::unique_ptr<Instruction>>;
|
||||
using iterator = inst_list::iterator;
|
||||
using arg_list = std::vector<AllocaInst *>;
|
||||
using block_list = std::vector<BasicBlock *>;
|
||||
using block_set = std::unordered_set<BasicBlock *>;
|
||||
|
||||
@ -493,6 +476,7 @@ protected:
|
||||
|
||||
Function *parent; ///< 从属的函数
|
||||
inst_list instructions; ///< 拥有的指令序列
|
||||
arg_list arguments; ///< 分配空间后的形式参数列表
|
||||
block_list successors; ///< 前驱列表
|
||||
block_list predecessors; ///< 后继列表
|
||||
bool reachable = false;
|
||||
@ -512,25 +496,19 @@ public:
|
||||
public:
|
||||
|
||||
unsigned getNumInstructions() const { return instructions.size(); }
|
||||
unsigned getNumArguments() const { return arguments.size(); }
|
||||
unsigned getNumPredecessors() const { return predecessors.size(); }
|
||||
unsigned getNumSuccessors() const { return successors.size(); }
|
||||
Function* getParent() const { return parent; }
|
||||
void setParent(Function *func) { parent = func; }
|
||||
inst_list& getInstructions() { return instructions; }
|
||||
auto getInstructions_Range() const { return make_range(instructions); }
|
||||
block_list& getPredecessors() { return predecessors; }
|
||||
void clearPredecessors() { predecessors.clear(); }
|
||||
arg_list& getArguments() { return arguments; }
|
||||
const block_list& getPredecessors() const { return predecessors; }
|
||||
block_list& getSuccessors() { return successors; }
|
||||
void clearSuccessors() { successors.clear(); }
|
||||
iterator begin() { return instructions.begin(); }
|
||||
iterator end() { return instructions.end(); }
|
||||
iterator terminator() { return std::prev(end()); }
|
||||
bool hasSuccessor(BasicBlock *block) const {
|
||||
return std::find(successors.begin(), successors.end(), block) != successors.end();
|
||||
} ///< 判断是否有后继块
|
||||
bool hasPredecessor(BasicBlock *block) const {
|
||||
return std::find(predecessors.begin(), predecessors.end(), block) != predecessors.end();
|
||||
} ///< 判断是否有前驱块
|
||||
void insertArgument(AllocaInst *inst) { arguments.push_back(inst); }
|
||||
void addPredecessor(BasicBlock *block) {
|
||||
if (std::find(predecessors.begin(), predecessors.end(), block) == predecessors.end()) {
|
||||
predecessors.push_back(block);
|
||||
@ -583,15 +561,6 @@ public:
|
||||
next->addPredecessor(prev);
|
||||
}
|
||||
void removeInst(iterator pos) { instructions.erase(pos); }
|
||||
void removeInst(Instruction *inst) {
|
||||
auto pos = std::find_if(instructions.begin(), instructions.end(),
|
||||
[inst](const std::unique_ptr<Instruction> &i) { return i.get() == inst; });
|
||||
if (pos != instructions.end()) {
|
||||
instructions.erase(pos);
|
||||
} else {
|
||||
assert(false && "Instruction not found in BasicBlock");
|
||||
}
|
||||
} ///< 移除指定位置的指令
|
||||
iterator moveInst(iterator sourcePos, iterator targetPos, BasicBlock *block);
|
||||
};
|
||||
|
||||
@ -633,6 +602,49 @@ class User : public Value {
|
||||
void setOperand(unsigned index, Value *value); ///< 设置操作数
|
||||
};
|
||||
|
||||
class GetSubArrayInst;
|
||||
/**
|
||||
* 左值 具有地址的对象
|
||||
*/
|
||||
class LVal {
|
||||
friend class GetSubArrayInst;
|
||||
|
||||
protected:
|
||||
LVal *fatherLVal{}; ///< 父左值
|
||||
std::list<std::unique_ptr<LVal>> childrenLVals; ///< 子左值
|
||||
GetSubArrayInst *defineInst{}; /// 定义该左值的GetSubArray指令
|
||||
|
||||
protected:
|
||||
LVal() = default;
|
||||
|
||||
public:
|
||||
virtual ~LVal() = default;
|
||||
virtual std::vector<Value *> getLValDims() const = 0; ///< 获取左值的维度
|
||||
virtual unsigned getLValNumDims() const = 0; ///< 获取左值的维度数量
|
||||
|
||||
public:
|
||||
LVal* getFatherLVal() const { return fatherLVal; } ///< 获取父左值
|
||||
const std::list<std::unique_ptr<LVal>>& getChildrenLVals() const {
|
||||
return childrenLVals;
|
||||
} ///< 获取子左值列表
|
||||
LVal* getAncestorLVal() const {
|
||||
auto curLVal = const_cast<LVal *>(this);
|
||||
while (curLVal->getFatherLVal() != nullptr) {
|
||||
curLVal = curLVal->getFatherLVal();
|
||||
}
|
||||
return curLVal;
|
||||
} ///< 获取祖先左值
|
||||
void setFatherLVal(LVal *father) { fatherLVal = father; } ///< 设置父左值
|
||||
void setDefineInst(GetSubArrayInst *inst) { defineInst = inst; } ///< 设置定义指令
|
||||
void addChild(LVal *child) { childrenLVals.emplace_back(child); } ///< 添加子左值
|
||||
void removeChild(LVal *child) {
|
||||
auto iter = std::find_if(childrenLVals.begin(), childrenLVals.end(),
|
||||
[child](const std::unique_ptr<LVal> &ptr) { return ptr.get() == child; });
|
||||
childrenLVals.erase(iter);
|
||||
} ///< 移除子左值
|
||||
GetSubArrayInst* getDefineInst() const { return defineInst; } ///< 获取定义指令
|
||||
};
|
||||
|
||||
/*!
|
||||
* Base of all concrete instruction types.
|
||||
*/
|
||||
@ -682,15 +694,15 @@ class Instruction : public User {
|
||||
kAlloca = 0x1UL << 33,
|
||||
kLoad = 0x1UL << 34,
|
||||
kStore = 0x1UL << 35,
|
||||
kGetElementPtr = 0x1UL << 36,
|
||||
kLa = 0x1UL << 36,
|
||||
kMemset = 0x1UL << 37,
|
||||
// kGetSubArray = 0x1UL << 38,
|
||||
kGetSubArray = 0x1UL << 38,
|
||||
// Constant Kind removed as Constants are now Values, not Instructions.
|
||||
// kConstant = 0x1UL << 37, // Conflicts with kMemset if kept as is
|
||||
// phi
|
||||
kPhi = 0x1UL << 39,
|
||||
kBitItoF = 0x1UL << 40,
|
||||
kBitFtoI = 0x1UL << 41,
|
||||
kBitFtoI = 0x1UL << 41
|
||||
};
|
||||
|
||||
protected:
|
||||
@ -781,12 +793,14 @@ public:
|
||||
return "Load";
|
||||
case kStore:
|
||||
return "Store";
|
||||
case kGetElementPtr:
|
||||
return "GetElementPtr";
|
||||
case kLa:
|
||||
return "La";
|
||||
case kMemset:
|
||||
return "Memset";
|
||||
case kPhi:
|
||||
return "Phi";
|
||||
case kGetSubArray:
|
||||
return "GetSubArray";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
@ -839,8 +853,9 @@ public:
|
||||
bool isAlloca() const { return kind == kAlloca; }
|
||||
bool isLoad() const { return kind == kLoad; }
|
||||
bool isStore() const { return kind == kStore; }
|
||||
bool isGetElementPtr() const { return kind == kGetElementPtr; }
|
||||
bool isLa() const { return kind == kLa; }
|
||||
bool isMemset() const { return kind == kMemset; }
|
||||
bool isGetSubArray() const { return kind == kGetSubArray; }
|
||||
bool isCall() const { return kind == kCall; }
|
||||
bool isReturn() const { return kind == kReturn; }
|
||||
bool isDefine() const {
|
||||
@ -852,6 +867,26 @@ public:
|
||||
class Function;
|
||||
//! Function call.
|
||||
|
||||
class LaInst : public Instruction {
|
||||
friend class Function;
|
||||
friend class IRBuilder;
|
||||
|
||||
protected:
|
||||
explicit LaInst(Value *pointer, const std::vector<Value *> &indices = {}, BasicBlock *parent = nullptr,
|
||||
const std::string &name = "")
|
||||
: Instruction(Kind::kLa, pointer->getType(), parent, name) {
|
||||
assert(pointer);
|
||||
addOperand(pointer);
|
||||
addOperands(indices);
|
||||
}
|
||||
|
||||
public:
|
||||
unsigned getNumIndices() const { return getNumOperands() - 1; } ///< 获取索引长度
|
||||
Value* getPointer() const { return getOperand(0); } ///< 获取目标变量的Value指针
|
||||
auto getIndices() const { return make_range(std::next(operand_begin()), operand_end()); } ///< 获取索引列表
|
||||
Value* getIndex(unsigned index) const { return getOperand(index + 1); } ///< 获取位置为index的索引分量
|
||||
};
|
||||
|
||||
class PhiInst : public Instruction {
|
||||
friend class IRBuilder;
|
||||
friend class Function;
|
||||
@ -1059,8 +1094,6 @@ public:
|
||||
}; // class UncondBrInst
|
||||
|
||||
//! Conditional branch
|
||||
// 这里的args是指向条件分支的两个分支的参数列表但是现在弃用了
|
||||
// 通过mem2reg优化后,数据流分析将不会由arguments来传递
|
||||
class CondBrInst : public Instruction {
|
||||
friend class IRBuilder;
|
||||
friend class Function;
|
||||
@ -1086,22 +1119,22 @@ public:
|
||||
BasicBlock* getElseBlock() const {
|
||||
return dynamic_cast<BasicBlock *>(getOperand(2));
|
||||
}
|
||||
// auto getThenArguments() const {
|
||||
// auto begin = std::next(operand_begin(), 3);
|
||||
// // auto end = std::next(begin, getThenBlock()->getNumArguments());
|
||||
// return make_range(begin, end);
|
||||
// }
|
||||
// auto getElseArguments() const {
|
||||
// auto begin =
|
||||
// std::next(operand_begin(), 3 + getThenBlock()->getNumArguments());
|
||||
// auto end = operand_end();
|
||||
// return make_range(begin, end);
|
||||
// }
|
||||
auto getThenArguments() const {
|
||||
auto begin = std::next(operand_begin(), 3);
|
||||
auto end = std::next(begin, getThenBlock()->getNumArguments());
|
||||
return make_range(begin, end);
|
||||
}
|
||||
auto getElseArguments() const {
|
||||
auto begin =
|
||||
std::next(operand_begin(), 3 + getThenBlock()->getNumArguments());
|
||||
auto end = operand_end();
|
||||
return make_range(begin, end);
|
||||
}
|
||||
|
||||
}; // class CondBrInst
|
||||
|
||||
//! Allocate memory for stack variables, used for non-global variable declartion
|
||||
class AllocaInst : public Instruction {
|
||||
class AllocaInst : public Instruction , public LVal {
|
||||
friend class IRBuilder;
|
||||
friend class Function;
|
||||
protected:
|
||||
@ -1112,6 +1145,14 @@ protected:
|
||||
}
|
||||
|
||||
public:
|
||||
std::vector<Value *> getLValDims() const override {
|
||||
std::vector<Value *> dims;
|
||||
for (const auto &dim : getOperands()) {
|
||||
dims.emplace_back(dim->getValue());
|
||||
}
|
||||
return dims;
|
||||
} ///< 获取作为左值的维度数组
|
||||
unsigned getLValNumDims() const override { return getNumOperands(); }
|
||||
|
||||
int getNumDims() const { return getNumOperands(); }
|
||||
auto getDims() const { return getOperands(); }
|
||||
@ -1120,40 +1161,37 @@ public:
|
||||
}; // class AllocaInst
|
||||
|
||||
|
||||
class GetElementPtrInst : public Instruction {
|
||||
class GetSubArrayInst : public Instruction {
|
||||
friend class IRBuilder;
|
||||
friend class Function;
|
||||
|
||||
protected:
|
||||
// GEP的构造函数:
|
||||
// resultType: GEP计算出的地址的类型 (通常是指向目标元素类型的指针)
|
||||
// basePointer: 基指针 (第一个操作数)
|
||||
// indices: 索引列表 (后续操作数)
|
||||
GetElementPtrInst(Type *resultType,
|
||||
Value *basePointer,
|
||||
const std::vector<Value *> &indices = {},
|
||||
BasicBlock *parent = nullptr, const std::string &name = "")
|
||||
: Instruction(Kind::kGetElementPtr, resultType, parent, name) {
|
||||
assert(basePointer && "GEP base pointer cannot be null!");
|
||||
// TODO : 安全检查
|
||||
assert(basePointer->getType()->isPointer() );
|
||||
addOperand(basePointer); // 第一个操作数是基指针
|
||||
addOperands(indices); // 随后的操作数是索引
|
||||
}
|
||||
public:
|
||||
Value* getBasePointer() const { return getOperand(0); }
|
||||
unsigned getNumIndices() const { return getNumOperands() - 1; }
|
||||
auto getIndices() const { return make_range(std::next(operand_begin()), operand_end());}
|
||||
Value* getIndex(unsigned index) const {
|
||||
assert(index < getNumIndices() && "Index out of bounds for GEP!");
|
||||
return getOperand(index + 1);
|
||||
public:
|
||||
GetSubArrayInst(LVal *fatherArray, LVal *childArray, const std::vector<Value *> &indices,
|
||||
BasicBlock *parent = nullptr, const std::string &name = "")
|
||||
: Instruction(Kind::kGetSubArray, Type::getVoidType(), parent, name) {
|
||||
auto predicate = [childArray](const std::unique_ptr<LVal> &child) -> bool { return child.get() == childArray; };
|
||||
if (std::find_if(fatherArray->childrenLVals.begin(), fatherArray->childrenLVals.end(), predicate) ==
|
||||
fatherArray->childrenLVals.end()) {
|
||||
fatherArray->childrenLVals.emplace_back(childArray);
|
||||
}
|
||||
childArray->fatherLVal = fatherArray;
|
||||
childArray->defineInst = this;
|
||||
auto fatherArrayValue = dynamic_cast<Value *>(fatherArray);
|
||||
auto childArrayValue = dynamic_cast<Value *>(childArray);
|
||||
assert(fatherArrayValue);
|
||||
assert(childArrayValue);
|
||||
addOperand(fatherArrayValue);
|
||||
addOperand(childArrayValue);
|
||||
addOperands(indices);
|
||||
}
|
||||
|
||||
// 静态工厂方法,用于创建GEP指令 (如果需要外部直接创建而非通过IRBuilder)
|
||||
static GetElementPtrInst* create(Type *resultType, Value *basePointer,
|
||||
const std::vector<Value *> &indices = {},
|
||||
BasicBlock *parent = nullptr, const std::string &name = "") {
|
||||
return new GetElementPtrInst(resultType, basePointer, indices, parent, name);
|
||||
}
|
||||
public:
|
||||
Value* getFatherArray() const { return getOperand(0); } ///< 获取父数组
|
||||
Value* getChildArray() const { return getOperand(1); } ///< 获取子数组
|
||||
LVal* getFatherLVal() const { return dynamic_cast<LVal *>(getOperand(0)); } ///< 获取父左值
|
||||
LVal* getChildLVal() const { return dynamic_cast<LVal *>(getOperand(1)); } ///< 获取子左值
|
||||
auto getIndices() const { return make_range(std::next(operand_begin(), 2), operand_end()); } ///< 获取索引
|
||||
unsigned getNumIndices() const { return getNumOperands() - 2; } ///< 获取索引数量
|
||||
};
|
||||
|
||||
//! Load a value from memory address specified by a pointer value
|
||||
@ -1177,7 +1215,22 @@ public:
|
||||
return make_range(std::next(operand_begin()), operand_end());
|
||||
}
|
||||
Value* getIndex(int index) const { return getOperand(index + 1); }
|
||||
|
||||
std::list<Value *> getAncestorIndices() const {
|
||||
std::list<Value *> indices;
|
||||
for (const auto &index : getIndices()) {
|
||||
indices.emplace_back(index->getValue());
|
||||
}
|
||||
auto curPointer = dynamic_cast<LVal *>(getPointer());
|
||||
while (curPointer->getFatherLVal() != nullptr) {
|
||||
auto inserter = std::next(indices.begin());
|
||||
for (const auto &index : curPointer->getDefineInst()->getIndices()) {
|
||||
indices.insert(inserter, index->getValue());
|
||||
}
|
||||
curPointer = curPointer->getFatherLVal();
|
||||
}
|
||||
|
||||
return indices;
|
||||
} ///< 获取相对于祖先数组的索引列表
|
||||
}; // class LoadInst
|
||||
|
||||
//! Store a value to memory address specified by a pointer value
|
||||
@ -1203,6 +1256,22 @@ public:
|
||||
return make_range(std::next(operand_begin(), 2), operand_end());
|
||||
}
|
||||
Value* getIndex(int index) const { return getOperand(index + 2); }
|
||||
std::list<Value *> getAncestorIndices() const {
|
||||
std::list<Value *> indices;
|
||||
for (const auto &index : getIndices()) {
|
||||
indices.emplace_back(index->getValue());
|
||||
}
|
||||
auto curPointer = dynamic_cast<LVal *>(getPointer());
|
||||
while (curPointer->getFatherLVal() != nullptr) {
|
||||
auto inserter = std::next(indices.begin());
|
||||
for (const auto &index : curPointer->getDefineInst()->getIndices()) {
|
||||
indices.insert(inserter, index->getValue());
|
||||
}
|
||||
curPointer = curPointer->getFatherLVal();
|
||||
}
|
||||
|
||||
return indices;
|
||||
} ///< 获取相对于祖先数组的索引列表
|
||||
|
||||
}; // class StoreInst
|
||||
|
||||
@ -1239,33 +1308,17 @@ public:
|
||||
class GlobalValue;
|
||||
|
||||
|
||||
class Argument : public Value {
|
||||
protected:
|
||||
Function *func;
|
||||
int index;
|
||||
|
||||
public:
|
||||
Argument(Type *type, Function *func, int index, const std::string &name = "")
|
||||
: Value(type, name), func(func), index(index) {}
|
||||
|
||||
public:
|
||||
Function* getParent() const { return func; }
|
||||
int getIndex() const { return index; }
|
||||
};
|
||||
|
||||
|
||||
class Module;
|
||||
//! Function definitionclass
|
||||
class Function : public Value {
|
||||
friend class Module;
|
||||
protected:
|
||||
Function(Module *parent, Type *type, const std::string &name) : Value(type, name), parent(parent) {
|
||||
blocks.emplace_back(new BasicBlock(this, "entry_" + name)); ///< 创建一个入口基本块
|
||||
blocks.emplace_back(new BasicBlock(this));
|
||||
}
|
||||
|
||||
public:
|
||||
using block_list = std::list<std::unique_ptr<BasicBlock>>;
|
||||
using arg_list = std::vector<Argument *>;
|
||||
enum FunctionAttribute : uint64_t {
|
||||
PlaceHolder = 0x0UL,
|
||||
Pure = 0x1UL << 0,
|
||||
@ -1277,7 +1330,6 @@ public:
|
||||
protected:
|
||||
Module *parent; ///< 函数的父模块
|
||||
block_list blocks; ///< 函数包含的基本块列表
|
||||
arg_list arguments; ///< 函数参数列表
|
||||
FunctionAttribute attribute = PlaceHolder; ///< 函数属性
|
||||
std::set<Function *> callees; ///< 函数调用的函数集合
|
||||
public:
|
||||
@ -1302,16 +1354,6 @@ protected:
|
||||
auto getBasicBlocks() { return make_range(blocks); }
|
||||
block_list& getBasicBlocks_NoRange() { return blocks; }
|
||||
BasicBlock* getEntryBlock() { return blocks.front().get(); }
|
||||
void insertArgument(Argument *arg) { arguments.push_back(arg); }
|
||||
arg_list& getArguments() { return arguments; }
|
||||
unsigned getNumArguments() const { return arguments.size(); }
|
||||
Argument* getArgument(unsigned index) const {
|
||||
assert(index < arguments.size() && "Argument index out of bounds");
|
||||
return arguments[index];
|
||||
} ///< 获取位置为index的参数
|
||||
auto getArgumentsRange() const {
|
||||
return make_range(arguments.begin(), arguments.end());
|
||||
} ///< 获取参数列表的范围
|
||||
void removeBasicBlock(BasicBlock *blockToRemove) {
|
||||
auto is_same_ptr = [blockToRemove](const std::unique_ptr<BasicBlock> &ptr) { return ptr.get() == blockToRemove; };
|
||||
blocks.remove_if(is_same_ptr);
|
||||
@ -1331,7 +1373,7 @@ protected:
|
||||
};
|
||||
|
||||
//! Global value declared at file scope
|
||||
class GlobalValue : public User {
|
||||
class GlobalValue : public User, public LVal {
|
||||
friend class Module;
|
||||
|
||||
protected:
|
||||
@ -1365,6 +1407,16 @@ protected:
|
||||
}
|
||||
|
||||
public:
|
||||
unsigned getLValNumDims() const override { return numDims; } ///< 获取作为左值的维度数量
|
||||
std::vector<Value *> getLValDims() const override {
|
||||
std::vector<Value *> dims;
|
||||
for (const auto &dim : getOperands()) {
|
||||
dims.emplace_back(dim->getValue());
|
||||
}
|
||||
|
||||
return dims;
|
||||
} ///< 获取作为左值的维度列表
|
||||
|
||||
unsigned getNumDims() const { return numDims; } ///< 获取维度数量
|
||||
Value* getDim(unsigned index) const { return getOperand(index); } ///< 获取位置为index的维度
|
||||
auto getDims() const { return getOperands(); } ///< 获取维度列表
|
||||
@ -1386,7 +1438,7 @@ public:
|
||||
}; // class GlobalValue
|
||||
|
||||
|
||||
class ConstantVariable : public User {
|
||||
class ConstantVariable : public User, public LVal {
|
||||
friend class Module;
|
||||
|
||||
protected:
|
||||
@ -1405,6 +1457,15 @@ class ConstantVariable : public User {
|
||||
}
|
||||
|
||||
public:
|
||||
unsigned getLValNumDims() const override { return numDims; } ///< 获取作为左值的维度数量
|
||||
std::vector<Value *> getLValDims() const override {
|
||||
std::vector<Value *> dims;
|
||||
for (const auto &dim : getOperands()) {
|
||||
dims.emplace_back(dim->getValue());
|
||||
}
|
||||
|
||||
return dims;
|
||||
} ///< 获取作为左值的维度列表
|
||||
Value* getByIndex(unsigned index) const { return initValues.getValue(index); } ///< 通过一维位置index获取值
|
||||
Value* getByIndices(const std::vector<Value *> &indices) const {
|
||||
int index = 0;
|
||||
@ -1428,7 +1489,7 @@ class ConstantVariable : public User {
|
||||
using SymbolTableNode = struct SymbolTableNode {
|
||||
SymbolTableNode *pNode; ///< 父节点
|
||||
std::vector<SymbolTableNode *> children; ///< 子节点列表
|
||||
std::map<std::string, Value *> varList; ///< 变量列表
|
||||
std::map<std::string, User *> varList; ///< 变量列表
|
||||
};
|
||||
|
||||
|
||||
@ -1443,8 +1504,8 @@ class SymbolTable {
|
||||
public:
|
||||
SymbolTable() = default;
|
||||
|
||||
Value* getVariable(const std::string &name) const; ///< 根据名字name以及当前作用域获取变量
|
||||
Value* addVariable(const std::string &name, Value *variable); ///< 添加变量
|
||||
User* getVariable(const std::string &name) const; ///< 根据名字name以及当前作用域获取变量
|
||||
User* addVariable(const std::string &name, User *variable); ///< 添加变量
|
||||
std::vector<std::unique_ptr<GlobalValue>>& getGlobals(); ///< 获取全局变量列表
|
||||
const std::vector<std::unique_ptr<ConstantVariable>>& getConsts() const; ///< 获取常量列表
|
||||
void enterNewScope(); ///< 进入新的作用域
|
||||
@ -1506,7 +1567,7 @@ class Module {
|
||||
void addVariable(const std::string &name, AllocaInst *variable) {
|
||||
variableTable.addVariable(name, variable);
|
||||
} ///< 添加变量
|
||||
Value* getVariable(const std::string &name) {
|
||||
User* getVariable(const std::string &name) {
|
||||
return variableTable.getVariable(name);
|
||||
} ///< 根据名字name和当前作用域获取变量
|
||||
Function* getFunction(const std::string &name) const {
|
||||
|
||||
@ -280,6 +280,46 @@ class IRBuilder {
|
||||
block->getInstructions().emplace(position, inst);
|
||||
return inst;
|
||||
} ///< 创建load指令
|
||||
LaInst * createLaInst(Value *pointer, const std::vector<Value *> &indices = {}, const std::string &name = "") {
|
||||
std::string newName;
|
||||
if (name.empty()) {
|
||||
std::stringstream ss;
|
||||
ss << tmpIndex;
|
||||
newName = ss.str();
|
||||
tmpIndex++;
|
||||
} else {
|
||||
newName = name;
|
||||
}
|
||||
|
||||
auto inst = new LaInst(pointer, indices, block, newName);
|
||||
assert(inst);
|
||||
block->getInstructions().emplace(position, inst);
|
||||
return inst;
|
||||
} ///< 创建la指令
|
||||
GetSubArrayInst * createGetSubArray(LVal *fatherArray, const std::vector<Value *> &indices, const std::string &name = "") {
|
||||
assert(fatherArray->getLValNumDims() > indices.size());
|
||||
std::vector<Value *> subDims;
|
||||
auto dims = fatherArray->getLValDims();
|
||||
auto iter = std::next(dims.begin(), indices.size());
|
||||
while (iter != dims.end()) {
|
||||
subDims.emplace_back(*iter);
|
||||
iter++;
|
||||
}
|
||||
|
||||
std::string childArrayName;
|
||||
std::stringstream ss;
|
||||
ss << "A"
|
||||
<< "%" << tmpIndex;
|
||||
childArrayName = ss.str();
|
||||
tmpIndex++;
|
||||
|
||||
auto fatherArrayValue = dynamic_cast<Value *>(fatherArray);
|
||||
auto childArray = new AllocaInst(fatherArrayValue->getType(), subDims, block, childArrayName);
|
||||
auto inst = new GetSubArrayInst(fatherArray, childArray, indices, block, childArrayName);
|
||||
assert(inst);
|
||||
block->getInstructions().emplace(position, inst);
|
||||
return inst;
|
||||
} ///< 创建获取部分数组指令
|
||||
MemsetInst * createMemsetInst(Value *pointer, Value *begin, Value *size, Value *value, const std::string &name = "") {
|
||||
auto inst = new MemsetInst(pointer, begin, size, value, block, name);
|
||||
assert(inst);
|
||||
@ -294,102 +334,12 @@ class IRBuilder {
|
||||
return inst;
|
||||
} ///< 创建store指令
|
||||
PhiInst * createPhiInst(Type *type, const std::vector<Value*> &vals = {}, const std::vector<BasicBlock*> &blks = {}, const std::string &name = "") {
|
||||
auto predNum = block->getNumPredecessors();
|
||||
auto inst = new PhiInst(type, vals, blks, block, name);
|
||||
assert(inst);
|
||||
block->getInstructions().emplace(block->begin(), inst);
|
||||
return inst;
|
||||
} ///< 创建Phi指令
|
||||
// GetElementPtrInst* createGetElementPtrInst(Value *basePointer,
|
||||
// const std::vector<Value *> &indices = {},
|
||||
// const std::string &name = "") {
|
||||
// std::string newName;
|
||||
// if (name.empty()) {
|
||||
// std::stringstream ss;
|
||||
// ss << tmpIndex;
|
||||
// newName = ss.str();
|
||||
// tmpIndex++;
|
||||
// } else {
|
||||
// newName = name;
|
||||
// }
|
||||
|
||||
// auto inst = new GetElementPtrInst(basePointer, indices, block, newName);
|
||||
// assert(inst);
|
||||
// block->getInstructions().emplace(position, inst);
|
||||
// return inst;
|
||||
// }
|
||||
/**
|
||||
* @brief 根据 LLVM 设计模式创建 GEP 指令。
|
||||
* 它会自动推断返回类型,无需手动指定。
|
||||
*/
|
||||
GetElementPtrInst *createGetElementPtrInst(Value *basePointer, const std::vector<Value *> &indices,
|
||||
const std::string &name = "") {
|
||||
Type *ResultElementType = getIndexedType(basePointer->getType(), indices);
|
||||
if (!ResultElementType) {
|
||||
assert(false && "Invalid GEP indexing!");
|
||||
return nullptr;
|
||||
}
|
||||
Type *ResultType = PointerType::get(ResultElementType);
|
||||
std::string newName;
|
||||
if (name.empty()) {
|
||||
std::stringstream ss;
|
||||
ss << tmpIndex;
|
||||
newName = ss.str();
|
||||
tmpIndex++;
|
||||
} else {
|
||||
newName = name;
|
||||
}
|
||||
|
||||
auto inst = new GetElementPtrInst(ResultType, basePointer, indices, block, newName);
|
||||
assert(inst);
|
||||
block->getInstructions().emplace(position, inst);
|
||||
return inst;
|
||||
}
|
||||
|
||||
static Type *getIndexedType(Type *pointerType, const std::vector<Value *> &indices) {
|
||||
assert(pointerType->isPointer() && "base must be a pointer type!");
|
||||
// GEP 的类型推断从基指针所指向的类型开始。
|
||||
// 例如:
|
||||
// - 如果 pointerType 是 `[20 x [10 x i32]]*`,`currentWalkType` 初始为 `[20 x [10 x i32]]`。
|
||||
// - 如果 pointerType 是 `i32*`,`currentWalkType` 初始为 `i32`。
|
||||
// - 如果 pointerType 是 `i32**`,`currentWalkType` 初始为 `i32*`。
|
||||
Type *currentWalkType = pointerType->as<PointerType>()->getBaseType();
|
||||
|
||||
// 遍历所有索引来深入类型层次结构。
|
||||
// `indices` 向量包含了所有 GEP 索引,包括由 `visitLValue` 等函数添加的初始 `0` 索引。
|
||||
for (int i = 0; i < indices.size(); ++i) {
|
||||
if (currentWalkType->isArray()) {
|
||||
// 情况一:当前遍历类型是 `ArrayType`。
|
||||
// 索引用于选择数组元素,`currentWalkType` 更新为数组的元素类型。
|
||||
currentWalkType = currentWalkType->as<ArrayType>()->getElementType();
|
||||
} else if (currentWalkType->isPointer()) {
|
||||
// 情况二:当前遍历类型是 `PointerType`。
|
||||
// 这意味着我们正在通过一个指针来访问其指向的内存。
|
||||
// 索引用于选择该指针所指向的“数组”的元素。
|
||||
// `currentWalkType` 更新为该指针所指向的基础类型。
|
||||
// 例如:如果 `currentWalkType` 是 `i32*`,它将变为 `i32`。
|
||||
// 如果 `currentWalkType` 是 `[10 x i32]*`,它将变为 `[10 x i32]`。
|
||||
currentWalkType = currentWalkType->as<PointerType>()->getBaseType();
|
||||
} else {
|
||||
// 情况三:当前遍历类型是标量类型 (例如 `i32`, `float` 等非聚合、非指针类型)。
|
||||
//
|
||||
// 如果 `currentWalkType` 是标量,并且当前索引 `i` **不是** `indices` 向量中的最后一个索引,
|
||||
// 这意味着尝试对一个标量类型进行进一步的结构性索引,这是**无效的**。
|
||||
// 例如:`int x; x[0];` 对应的 GEP 链中,`x` 的类型是 `i32`,再加 `[0]` 索引就是错误。
|
||||
//
|
||||
// 如果 `currentWalkType` 是标量,且这是**最后一个索引** (`i == indices.size() - 1`),
|
||||
// 那么 GEP 是合法的,它只是计算一个偏移地址,最终的类型就是这个标量类型。
|
||||
// 此时 `currentWalkType` 保持不变,循环结束。
|
||||
if (i < indices.size() - 1) {
|
||||
assert(false && "Invalid GEP indexing: attempting to index into a non-aggregate/non-pointer type with further indices.");
|
||||
return nullptr; // 返回空指针表示类型推断失败
|
||||
}
|
||||
// 如果是最后一个索引,且当前类型是标量,则类型保持不变,这是合法的。
|
||||
// 循环会自然结束,返回正确的 `currentWalkType`。
|
||||
}
|
||||
}
|
||||
// 所有索引处理完毕后,`currentWalkType` 就是 GEP 指令最终计算出的地址所指向的元素的类型。
|
||||
return currentWalkType;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
|
||||
@ -1,72 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "IR.h" // 包含 IR 定义
|
||||
#include "Pass.h" // 包含 Pass 框架
|
||||
#include <algorithm> // for std::set_union, std::set_difference
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 前向声明
|
||||
class Function;
|
||||
class BasicBlock;
|
||||
class Value;
|
||||
class Instruction;
|
||||
|
||||
// 活跃变量分析结果类
|
||||
// 它将包含 LiveIn 和 LiveOut 集合
|
||||
class LivenessAnalysisResult : public AnalysisResultBase {
|
||||
public:
|
||||
LivenessAnalysisResult(Function *F) : AssociatedFunction(F) {}
|
||||
|
||||
// 获取给定基本块的 LiveIn 集合
|
||||
const std::set<Value *> *getLiveIn(BasicBlock *BB) const;
|
||||
|
||||
// 获取给定基本块的 LiveOut 集合
|
||||
const std::set<Value *> *getLiveOut(BasicBlock *BB) const;
|
||||
|
||||
// 暴露内部数据结构,如果需要更直接的访问
|
||||
const std::map<BasicBlock *, std::set<Value *>> &getLiveInSets() const { return liveInSets; }
|
||||
const std::map<BasicBlock *, std::set<Value *>> &getLiveOutSets() const { return liveOutSets; }
|
||||
|
||||
// 核心计算方法,由 LivenessAnalysisPass 调用
|
||||
void computeLiveness(Function *F);
|
||||
|
||||
private:
|
||||
Function *AssociatedFunction; // 这个活跃变量分析是为哪个函数计算的
|
||||
std::map<BasicBlock *, std::set<Value *>> liveInSets;
|
||||
std::map<BasicBlock *, std::set<Value *>> liveOutSets;
|
||||
|
||||
// 辅助函数:计算基本块的 Def 和 Use 集合
|
||||
// Def: 块内定义,且定义在所有使用之前的值
|
||||
// Use: 块内使用,且使用在所有定义之前的值
|
||||
void computeDefUse(BasicBlock *BB, std::set<Value *> &def, std::set<Value *> &use);
|
||||
};
|
||||
|
||||
// 活跃变量分析遍
|
||||
class LivenessAnalysisPass : public AnalysisPass {
|
||||
public:
|
||||
// 唯一的 Pass ID
|
||||
static void *ID; // LLVM 风格的唯一 ID
|
||||
|
||||
LivenessAnalysisPass() : AnalysisPass("LivenessAnalysis", Pass::Granularity::Function) {}
|
||||
|
||||
// 实现 getPassID
|
||||
void *getPassID() const override { return &ID; }
|
||||
|
||||
// 运行分析并返回结果。现在接受 AnalysisManager& AM 参数
|
||||
bool runOnFunction(Function *F, AnalysisManager &AM) override;
|
||||
|
||||
// 获取分析结果的指针。
|
||||
// 注意:AnalysisManager 将会调用此方法来获取结果并进行缓存。
|
||||
std::unique_ptr<AnalysisResultBase> getResult() override;
|
||||
|
||||
private:
|
||||
// 存储当前分析计算出的 LivenessAnalysisResult 实例
|
||||
// runOnFunction 每次调用都会创建新的 LivenessAnalysisResult 对象
|
||||
std::unique_ptr<LivenessAnalysisResult> CurrentLivenessResult;
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
59
src/include/Mem2Reg.h
Normal file
59
src/include/Mem2Reg.h
Normal file
@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include "IR.h"
|
||||
#include "IRBuilder.h"
|
||||
#include "SysYIRAnalyser.h"
|
||||
|
||||
namespace sysy {
|
||||
/**
|
||||
* 实现静态单变量赋值核心类 mem2reg
|
||||
*/
|
||||
class Mem2Reg {
|
||||
private:
|
||||
Module *pModule;
|
||||
IRBuilder *pBuilder;
|
||||
ControlFlowAnalysis *controlFlowAnalysis; // 控制流分析
|
||||
ActiveVarAnalysis *activeVarAnalysis; // 活跃变量分析
|
||||
DataFlowAnalysisUtils dataFlowAnalysisUtils;
|
||||
|
||||
public:
|
||||
Mem2Reg(Module *pMoudle, IRBuilder *pBuilder,
|
||||
ControlFlowAnalysis *pCFA = nullptr, ActiveVarAnalysis *pAVA = nullptr) :
|
||||
pModule(pMoudle), pBuilder(pBuilder), controlFlowAnalysis(pCFA), activeVarAnalysis(pAVA), dataFlowAnalysisUtils()
|
||||
{} // 初始化函数
|
||||
|
||||
void mem2regPipeline(); ///< mem2reg
|
||||
|
||||
private:
|
||||
|
||||
// phi节点的插入需要计算IDF
|
||||
std::unordered_set<BasicBlock *> computeIterDf(const std::unordered_set<BasicBlock *> &blocks); ///< 计算定义块集合的迭代支配边界
|
||||
|
||||
auto computeValue2Blocks() -> void; ///< 计算value2block的映射(不包括数组和global)
|
||||
|
||||
auto preOptimize1() -> void; ///< llvm memtoreg预优化1: 删除不含load的alloc和store
|
||||
auto preOptimize2() -> void; ///< llvm memtoreg预优化2: 针对某个变量的Defblocks只有一个块的情况
|
||||
auto preOptimize3() -> void; ///< llvm memtoreg预优化3: 针对某个变量的所有读写都在同一个块中的情况
|
||||
|
||||
auto insertPhi() -> void; ///< 为所有变量的迭代支配边界插入phi结点
|
||||
|
||||
auto rename(BasicBlock *block, std::unordered_map<Value *, int> &count,
|
||||
std::unordered_map<Value *, std::stack<Instruction *>> &stacks) -> void; ///< 单个块的重命名
|
||||
auto renameAll() -> void; ///< 重命名所有块
|
||||
|
||||
// private helper function.
|
||||
private:
|
||||
auto getPredIndex(BasicBlock *n, BasicBlock *s) -> int; ///< 获取前驱索引
|
||||
auto cascade(Instruction *instr, bool &changed, Function *func, BasicBlock *block,
|
||||
std::list<std::unique_ptr<Instruction>> &instrs) -> void; ///< 消除级联关系
|
||||
auto isGlobal(Value *val) -> bool; ///< 判断是否是全局变量
|
||||
auto isArr(Value *val) -> bool; ///< 判断是否是数组
|
||||
auto usedelete(Instruction *instr) -> void; ///< 删除指令相关的value-use-user关系
|
||||
|
||||
};
|
||||
} // namespace sysy
|
||||
@ -1,316 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional> // For std::function
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <typeindex> // For std::type_index (although void* ID is more common in LLVM)
|
||||
#include <vector>
|
||||
#include <type_traits>
|
||||
#include "IR.h"
|
||||
#include "IRBuilder.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
//前向声明
|
||||
class PassManager;
|
||||
class AnalysisManager;
|
||||
|
||||
// 抽象基类:分析结果
|
||||
class AnalysisResultBase {
|
||||
public:
|
||||
virtual ~AnalysisResultBase() = default;
|
||||
};
|
||||
|
||||
// 抽象基类:Pass
|
||||
class Pass {
|
||||
public:
|
||||
enum class Granularity { Module, Function, BasicBlock };
|
||||
|
||||
enum class PassKind { Analysis, Optimization };
|
||||
|
||||
Pass(const std::string &name, Granularity g, PassKind k) : Name(name), G(g), K(k) {}
|
||||
virtual ~Pass() = default;
|
||||
|
||||
const std::string &getName() const { return Name; }
|
||||
Granularity getGranularity() const { return G; }
|
||||
PassKind getPassKind() const { return K; }
|
||||
|
||||
virtual bool runOnModule(Module *M, AnalysisManager& AM) { return false; }
|
||||
virtual bool runOnFunction(Function *F, AnalysisManager& AM) { return false; }
|
||||
virtual bool runOnBasicBlock(BasicBlock *BB, AnalysisManager& AM) { return false; }
|
||||
|
||||
// 所有 Pass 都必须提供一个唯一的 ID
|
||||
// 这通常是一个静态成员,并在 Pass 类外部定义
|
||||
virtual void *getPassID() const = 0;
|
||||
|
||||
protected:
|
||||
std::string Name;
|
||||
Granularity G;
|
||||
PassKind K;
|
||||
};
|
||||
|
||||
// 抽象基类:分析遍
|
||||
class AnalysisPass : public Pass {
|
||||
public:
|
||||
AnalysisPass(const std::string &name, Granularity g) : Pass(name, g, PassKind::Analysis) {}
|
||||
|
||||
virtual std::unique_ptr<AnalysisResultBase> getResult() = 0;
|
||||
};
|
||||
|
||||
// 抽象基类:优化遍
|
||||
class OptimizationPass : public Pass {
|
||||
public:
|
||||
OptimizationPass(const std::string &name, Granularity g) : Pass(name, g, PassKind::Optimization) {}
|
||||
|
||||
virtual void getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const {
|
||||
// 默认不依赖也不修改任何分析
|
||||
}
|
||||
};
|
||||
|
||||
// ======================================================================
|
||||
// PassRegistry: 全局 Pass 注册表 (单例)
|
||||
// ======================================================================
|
||||
class PassRegistry {
|
||||
public:
|
||||
// Pass 工厂函数类型:返回 Pass 的唯一指针
|
||||
using PassFactory = std::function<std::unique_ptr<Pass>()>;
|
||||
|
||||
// 获取 PassRegistry 实例 (单例模式)
|
||||
static PassRegistry &getPassRegistry() {
|
||||
static PassRegistry instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
// 注册一个 Pass
|
||||
// passID 是 Pass 类的唯一静态 ID (例如 MyPass::ID 的地址)
|
||||
// factory 是一个 lambda 或函数指针,用于创建该 Pass 的实例
|
||||
void registerPass(void *passID, PassFactory factory) {
|
||||
if (factories.count(passID)) {
|
||||
// Error: Pass with this ID already registered
|
||||
// You might want to throw an exception or log an error
|
||||
return;
|
||||
}
|
||||
factories[passID] = std::move(factory);
|
||||
}
|
||||
|
||||
// 通过 Pass ID 创建一个 Pass 实例
|
||||
std::unique_ptr<Pass> createPass(void *passID) {
|
||||
auto it = factories.find(passID);
|
||||
if (it == factories.end()) {
|
||||
// Error: Pass with this ID not registered
|
||||
return nullptr;
|
||||
}
|
||||
return it->second(); // 调用工厂函数创建实例
|
||||
}
|
||||
|
||||
private:
|
||||
PassRegistry() = default; // 私有构造函数,实现单例
|
||||
~PassRegistry() = default;
|
||||
PassRegistry(const PassRegistry &) = delete; // 禁用拷贝构造
|
||||
PassRegistry &operator=(const PassRegistry &) = delete; // 禁用赋值操作
|
||||
|
||||
std::map<void *, PassFactory> factories;
|
||||
};
|
||||
|
||||
// ======================================================================
|
||||
// AnalysisManager: 负责管理和提供分析结果
|
||||
// ======================================================================
|
||||
class AnalysisManager {
|
||||
private:
|
||||
Module *pModuleRef; // 指向被分析的Module
|
||||
|
||||
// 缓存不同粒度的分析结果
|
||||
std::map<void *, std::unique_ptr<AnalysisResultBase>> moduleCachedResults;
|
||||
std::map<std::pair<Function *, void *>, std::unique_ptr<AnalysisResultBase>> functionCachedResults;
|
||||
std::map<std::pair<BasicBlock *, void *>, std::unique_ptr<AnalysisResultBase>> basicBlockCachedResults;
|
||||
|
||||
|
||||
public:
|
||||
// 构造函数接收 Module 指针
|
||||
AnalysisManager(Module *M) : pModuleRef(M) {}
|
||||
AnalysisManager() = delete; // 禁止无参构造
|
||||
|
||||
~AnalysisManager() = default;
|
||||
|
||||
// 获取分析结果的通用模板函数
|
||||
// T 是 AnalysisResult 的具体类型,E 是 AnalysisPass 的具体类型
|
||||
// F 和 BB 参数用于提供上下文,根据分析遍的粒度来使用
|
||||
template <typename T, typename E> T *getAnalysisResult(Function *F = nullptr, BasicBlock *BB = nullptr) {
|
||||
void *analysisID = E::ID; // 获取分析遍的唯一 ID
|
||||
|
||||
// 尝试从注册表创建分析遍实例
|
||||
std::unique_ptr<Pass> basePass = PassRegistry::getPassRegistry().createPass(analysisID);
|
||||
if (!basePass) {
|
||||
// Error: Analysis pass not registered
|
||||
std::cerr << "Error: Analysis pass with ID " << analysisID << " not registered.\n";
|
||||
return nullptr;
|
||||
}
|
||||
AnalysisPass *analysisPass = static_cast<AnalysisPass *>(basePass.get());
|
||||
|
||||
// 根据分析遍的粒度处理
|
||||
switch (analysisPass->getGranularity()) {
|
||||
case Pass::Granularity::Module: {
|
||||
// 检查是否已存在有效结果
|
||||
auto it = moduleCachedResults.find(analysisID);
|
||||
if (it != moduleCachedResults.end()) {
|
||||
return static_cast<T *>(it->second.get()); // 返回缓存结果
|
||||
}
|
||||
// 运行模块级分析遍
|
||||
if (!pModuleRef) {
|
||||
std::cerr << "Error: Module reference not set for AnalysisManager to run Module Pass.\n";
|
||||
return nullptr;
|
||||
}
|
||||
analysisPass->runOnModule(pModuleRef, *this);
|
||||
// 获取结果并缓存
|
||||
std::unique_ptr<AnalysisResultBase> result = analysisPass->getResult();
|
||||
T *specificResult = static_cast<T *>(result.get());
|
||||
moduleCachedResults[analysisID] = std::move(result); // 缓存结果
|
||||
return specificResult;
|
||||
}
|
||||
case Pass::Granularity::Function: {
|
||||
// 检查请求的上下文是否正确
|
||||
if (!F) {
|
||||
std::cerr << "Error: Function context required for Function-level Analysis Pass.\n";
|
||||
return nullptr;
|
||||
}
|
||||
// 检查是否已存在有效结果
|
||||
auto it = functionCachedResults.find({F, analysisID});
|
||||
if (it != functionCachedResults.end()) {
|
||||
return static_cast<T *>(it->second.get()); // 返回缓存结果
|
||||
}
|
||||
// 运行函数级分析遍
|
||||
analysisPass->runOnFunction(F, *this);
|
||||
// 获取结果并缓存
|
||||
std::unique_ptr<AnalysisResultBase> result = analysisPass->getResult();
|
||||
T *specificResult = static_cast<T *>(result.get());
|
||||
functionCachedResults[{F, analysisID}] = std::move(result); // 缓存结果
|
||||
return specificResult;
|
||||
}
|
||||
case Pass::Granularity::BasicBlock: {
|
||||
// 检查请求的上下文是否正确
|
||||
if (!BB) {
|
||||
std::cerr << "Error: BasicBlock context required for BasicBlock-level Analysis Pass.\n";
|
||||
return nullptr;
|
||||
}
|
||||
// 检查是否已存在有效结果
|
||||
auto it = basicBlockCachedResults.find({BB, analysisID});
|
||||
if (it != basicBlockCachedResults.end()) {
|
||||
return static_cast<T *>(it->second.get()); // 返回缓存结果
|
||||
}
|
||||
// 运行基本块级分析遍
|
||||
analysisPass->runOnBasicBlock(BB, *this);
|
||||
// 获取结果并缓存
|
||||
std::unique_ptr<AnalysisResultBase> result = analysisPass->getResult();
|
||||
T *specificResult = static_cast<T *>(result.get());
|
||||
basicBlockCachedResults[{BB, analysisID}] = std::move(result); // 缓存结果
|
||||
return specificResult;
|
||||
}
|
||||
}
|
||||
return nullptr; // 不会到达这里
|
||||
}
|
||||
|
||||
// 使所有分析结果失效 (当 IR 被修改时调用)
|
||||
void invalidateAllAnalyses() {
|
||||
moduleCachedResults.clear();
|
||||
functionCachedResults.clear();
|
||||
basicBlockCachedResults.clear();
|
||||
}
|
||||
|
||||
// 使特定分析结果失效
|
||||
// void *analysisID: 要失效的分析的ID
|
||||
// Function *F: 如果是函数级分析,指定函数;如果是模块级或基本块级,则为nullptr (取决于调用方式)
|
||||
// BasicBlock *BB: 如果是基本块级分析,指定基本块;否则为nullptr
|
||||
void invalidateAnalysis(void *analysisID, Function *F = nullptr, BasicBlock *BB = nullptr) {
|
||||
if (BB) {
|
||||
// 使特定基本块的特定分析结果失效
|
||||
basicBlockCachedResults.erase({BB, analysisID});
|
||||
} else if (F) {
|
||||
// 使特定函数的特定分析结果失效 (也可能包含聚合的BasicBlock结果)
|
||||
functionCachedResults.erase({F, analysisID});
|
||||
// 遍历所有属于F的基本块,使其BasicBlockCache失效 (如果该分析是BasicBlock粒度的)
|
||||
// 这需要遍历F的所有基本块,效率较低,更推荐在BasicBlockPass的invalidateAnalysisUsage中精确指定
|
||||
// 或者在Function级别的invalidate时,清空该Function的所有BasicBlock分析
|
||||
// 这里的实现简单地清空该Function下所有该ID的BasicBlock缓存
|
||||
for (auto it = basicBlockCachedResults.begin(); it != basicBlockCachedResults.end(); ) {
|
||||
// 假设BasicBlock::getParent()方法存在,可以获取所属Function
|
||||
if (it->first.second == analysisID /* && it->first.first->getParent() == F */) { // 需要BasicBlock能获取其父函数
|
||||
it = basicBlockCachedResults.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// 使所有函数的特定分析结果失效 (Module级和所有Function/BasicBlock级)
|
||||
moduleCachedResults.erase(analysisID);
|
||||
for (auto it = functionCachedResults.begin(); it != functionCachedResults.end(); ) {
|
||||
if (it->first.second == analysisID) {
|
||||
it = functionCachedResults.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
for (auto it = basicBlockCachedResults.begin(); it != basicBlockCachedResults.end(); ) {
|
||||
if (it->first.second == analysisID) {
|
||||
it = basicBlockCachedResults.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// ======================================================================
|
||||
// PassManager:遍管理器
|
||||
// ======================================================================
|
||||
class PassManager {
|
||||
private:
|
||||
std::vector<std::unique_ptr<Pass>> passes;
|
||||
AnalysisManager analysisManager;
|
||||
Module *pmodule;
|
||||
IRBuilder *pBuilder;
|
||||
|
||||
public:
|
||||
PassManager() = default;
|
||||
~PassManager() = default;
|
||||
|
||||
PassManager(Module *module, IRBuilder *builder) : pmodule(module) ,pBuilder(builder), analysisManager(module) {}
|
||||
|
||||
// 运行所有注册的遍
|
||||
bool run();
|
||||
|
||||
// 运行优化管道主要负责注册和运行优化遍
|
||||
// 这里可以根据 optLevel 和 DEBUG 控制不同的优化遍
|
||||
void runOptimizationPipeline(Module* moduleIR, IRBuilder* builder, int optLevel);
|
||||
|
||||
// 添加遍:现在接受 Pass 的 ID,而不是直接的 unique_ptr
|
||||
void addPass(void *passID);
|
||||
|
||||
AnalysisManager &getAnalysisManager() { return analysisManager; }
|
||||
|
||||
void clearPasses();
|
||||
};
|
||||
|
||||
// ======================================================================
|
||||
// 辅助宏或函数,用于简化 Pass 的注册
|
||||
// ======================================================================
|
||||
|
||||
// 用于分析遍的注册
|
||||
template <typename AnalysisPassType> void registerAnalysisPass();
|
||||
|
||||
// (1) 针对需要 IRBuilder 参数的优化遍的重载
|
||||
// 这个模板只在 OptimizationPassType 可以通过 IRBuilder* 构造时才有效
|
||||
template <typename OptimizationPassType, typename std::enable_if<
|
||||
std::is_constructible<OptimizationPassType, IRBuilder*>::value, int>::type = 0>
|
||||
void registerOptimizationPass(IRBuilder* builder);
|
||||
|
||||
// (2) 针对不需要 IRBuilder 参数的所有其他优化遍的重载
|
||||
// 这个模板只在 OptimizationPassType 不能通过 IRBuilder* 构造时才有效
|
||||
template <typename OptimizationPassType, typename std::enable_if<
|
||||
!std::is_constructible<OptimizationPassType, IRBuilder*>::value, int>::type = 0>
|
||||
void registerOptimizationPass();
|
||||
|
||||
} // namespace sysy
|
||||
@ -4,23 +4,20 @@
|
||||
#include "RISCv64LLIR.h"
|
||||
#include <iostream>
|
||||
|
||||
extern int DEBUG;
|
||||
extern int DEEPDEBUG;
|
||||
|
||||
namespace sysy {
|
||||
|
||||
class RISCv64AsmPrinter {
|
||||
public:
|
||||
RISCv64AsmPrinter(MachineFunction* mfunc);
|
||||
// 主入口
|
||||
void run(std::ostream& os, bool debug = false);
|
||||
void run(std::ostream& os);
|
||||
|
||||
private:
|
||||
// 打印各个部分
|
||||
void printPrologue();
|
||||
void printEpilogue();
|
||||
void printBasicBlock(MachineBasicBlock* mbb, bool debug = false);
|
||||
void printInstruction(MachineInstr* instr, bool debug = false);
|
||||
void printBasicBlock(MachineBasicBlock* mbb);
|
||||
void printInstruction(MachineInstr* instr);
|
||||
|
||||
// 辅助函数
|
||||
std::string regToString(PhysicalReg reg);
|
||||
|
||||
@ -4,9 +4,6 @@
|
||||
#include "IR.h"
|
||||
#include <string>
|
||||
|
||||
extern int DEBUG;
|
||||
extern int DEEPDEBUG;
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// RISCv64CodeGen 现在是一个高层驱动器
|
||||
|
||||
@ -3,9 +3,6 @@
|
||||
|
||||
#include "RISCv64LLIR.h"
|
||||
|
||||
extern int DEBUG;
|
||||
extern int DEEPDEBUG;
|
||||
|
||||
namespace sysy {
|
||||
|
||||
class RISCv64ISel {
|
||||
@ -17,8 +14,6 @@ public:
|
||||
// 公开接口,以便后续模块(如RegAlloc)可以查询或创建vreg
|
||||
unsigned getVReg(Value* val);
|
||||
unsigned getNewVReg() { return vreg_counter++; }
|
||||
// 获取 vreg_map 的公共接口
|
||||
const std::map<Value*, unsigned>& getVRegMap() const { return vreg_map; }
|
||||
|
||||
private:
|
||||
// DAG节点定义,作为ISel的内部实现细节
|
||||
@ -35,10 +30,6 @@ private:
|
||||
std::vector<std::unique_ptr<DAGNode>> build_dag(BasicBlock* bb);
|
||||
DAGNode* get_operand_node(Value* val_ir, std::map<Value*, DAGNode*>&, std::vector<std::unique_ptr<DAGNode>>&);
|
||||
DAGNode* create_node(int kind, Value* val, std::map<Value*, DAGNode*>&, std::vector<std::unique_ptr<DAGNode>>&);
|
||||
// 用于计算类型大小的辅助函数
|
||||
unsigned getTypeSizeInBytes(Type* type);
|
||||
|
||||
void print_dag(const std::vector<std::unique_ptr<DAGNode>>& dag, const std::string& bb_name);
|
||||
|
||||
// 状态
|
||||
Function* F; // 当前处理的高层IR函数
|
||||
|
||||
@ -18,29 +18,8 @@ namespace sysy {
|
||||
|
||||
// 物理寄存器定义
|
||||
enum class PhysicalReg {
|
||||
// --- 特殊功能寄存器 ---
|
||||
ZERO, RA, SP, GP, TP,
|
||||
|
||||
// --- 整数寄存器 (按调用约定分组) ---
|
||||
// 临时寄存器 (调用者保存)
|
||||
T0, T1, T2, T3, T4, T5, T6,
|
||||
|
||||
// 保存寄存器 (被调用者保存)
|
||||
S0, S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11,
|
||||
|
||||
// 参数/返回值寄存器 (调用者保存)
|
||||
A0, A1, A2, A3, A4, A5, A6, A7,
|
||||
|
||||
// --- 浮点寄存器 ---
|
||||
// (保持您原有的 F0-F31 命名)
|
||||
F0, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11,
|
||||
F12, F13, F14, F15, F16, F17, F18, F19, F20, F21,
|
||||
F22, F23, F24, F25, F26, F27, F28, F29, F30, F31,
|
||||
|
||||
// 用于内部表示物理寄存器在干扰图中的节点ID(一个简单的特殊ID,确保不与vreg_counter冲突)
|
||||
// 假设 vreg_counter 不会达到这么大的值
|
||||
PHYS_REG_START_ID = 10000,
|
||||
PHYS_REG_END_ID = PHYS_REG_START_ID + 32, // 预留足够的空间
|
||||
ZERO, RA, SP, GP, TP, T0, T1, T2, S0, S1, A0, A1, A2, A3, A4, A5, A6, A7, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, T3, T4, T5, T6,
|
||||
F0, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15,F16, F17, F18, F19, F20, F21, F22, F23, F24, F25, F26, F27, F28, F29, F30, F31
|
||||
};
|
||||
|
||||
// RISC-V 指令操作码枚举
|
||||
@ -65,16 +44,10 @@ enum class RVOpcodes {
|
||||
// 特殊标记,非指令
|
||||
LABEL,
|
||||
// 新增伪指令,用于解耦栈帧处理
|
||||
FRAME_LOAD_W, // 从栈帧加载 32位 Word (对应 lw)
|
||||
FRAME_LOAD_D, // 从栈帧加载 64位 Doubleword (对应 ld)
|
||||
FRAME_STORE_W, // 保存 32位 Word 到栈帧 (对应 sw)
|
||||
FRAME_STORE_D, // 保存 64位 Doubleword 到栈帧 (对应 sd)
|
||||
FRAME_ADDR, // 获取栈帧变量的地址
|
||||
FRAME_LOAD, // 从栈帧加载 (AllocaInst)
|
||||
FRAME_STORE, // 保存到栈帧 (AllocaInst)
|
||||
};
|
||||
|
||||
// 定义一个全局辅助函数或常量,提供调用者保存寄存器列表
|
||||
const std::vector<PhysicalReg>& getCallerSavedIntRegs();
|
||||
|
||||
class MachineOperand;
|
||||
class RegOperand;
|
||||
class ImmOperand;
|
||||
@ -195,10 +168,8 @@ struct StackFrameInfo {
|
||||
int locals_size = 0; // 仅为AllocaInst分配的大小
|
||||
int spill_size = 0; // 仅为溢出分配的大小
|
||||
int total_size = 0; // 总大小
|
||||
int callee_saved_size = 0; // 保存寄存器的大小
|
||||
std::map<unsigned, int> alloca_offsets; // <AllocaInst的vreg, 栈偏移>
|
||||
std::map<unsigned, int> spill_offsets; // <溢出vreg, 栈偏移>
|
||||
std::set<PhysicalReg> used_callee_saved_regs; // 使用的保存寄存器
|
||||
};
|
||||
|
||||
// 机器函数
|
||||
@ -224,15 +195,6 @@ private:
|
||||
StackFrameInfo frame_info;
|
||||
};
|
||||
|
||||
inline const std::vector<PhysicalReg>& getCallerSavedIntRegs() {
|
||||
static const std::vector<PhysicalReg> regs = {
|
||||
PhysicalReg::T0, PhysicalReg::T1, PhysicalReg::T2, PhysicalReg::T3,
|
||||
PhysicalReg::T4, PhysicalReg::T5, PhysicalReg::T6,
|
||||
PhysicalReg::A0, PhysicalReg::A1, PhysicalReg::A2, PhysicalReg::A3,
|
||||
PhysicalReg::A4, PhysicalReg::A5, PhysicalReg::A6, PhysicalReg::A7
|
||||
};
|
||||
return regs;
|
||||
}
|
||||
} // namespace sysy
|
||||
|
||||
#endif // RISCV64_LLIR_H
|
||||
@ -1,3 +1,4 @@
|
||||
// RISCv64Passes.h
|
||||
#ifndef RISCV64_PASSES_H
|
||||
#define RISCV64_PASSES_H
|
||||
|
||||
@ -5,70 +6,12 @@
|
||||
|
||||
namespace sysy {
|
||||
|
||||
/**
|
||||
* @class BackendPass
|
||||
* @brief 所有优化Pass的抽象基类 (可选,但推荐)
|
||||
* * 定义一个通用的接口,所有优化都应该实现它。
|
||||
*/
|
||||
class BackendPass {
|
||||
public:
|
||||
virtual ~BackendPass() = default;
|
||||
virtual void runOnMachineFunction(MachineFunction* mfunc) = 0;
|
||||
};
|
||||
|
||||
|
||||
// --- 寄存器分配前优化 ---
|
||||
|
||||
/**
|
||||
* @class PreRA_Scheduler
|
||||
* @brief 寄存器分配前的指令调度器
|
||||
* * 在虚拟寄存器上进行操作,此时调度自由度最大,
|
||||
* 主要目标是隐藏指令延迟,提高流水线效率。
|
||||
*/
|
||||
class PreRA_Scheduler : public BackendPass {
|
||||
public:
|
||||
void runOnMachineFunction(MachineFunction* mfunc) override;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @class CalleeSavedHandler
|
||||
* @brief 处理被调用者保存寄存器(Callee-Saved Registers)的Pass。
|
||||
* * 这个Pass在寄存器分配之后运行。它的主要职责是:
|
||||
* 1. 扫描整个函数,找出所有被使用的 `s` 系列寄存器。
|
||||
* 2. 在函数序言中插入 `sd` 指令来保存这些寄存器。
|
||||
* 3. 在函数结尾(ret指令前)插入 `ld` 指令来恢复这些寄存器。
|
||||
* 4. 正确计算因保存这些寄存器而需要的额外栈空间,并更新StackFrameInfo。
|
||||
*/
|
||||
class CalleeSavedHandler : public BackendPass {
|
||||
public:
|
||||
void runOnMachineFunction(MachineFunction* mfunc) override;
|
||||
};
|
||||
|
||||
// --- 寄存器分配后优化 ---
|
||||
|
||||
/**
|
||||
* @class PeepholeOptimizer
|
||||
* @brief 窥孔优化器
|
||||
* * 在已分配物理寄存器的指令流上,通过一个小的滑动窗口来查找
|
||||
* 并替换掉一些冗余或低效的指令模式。
|
||||
*/
|
||||
class PeepholeOptimizer : public BackendPass {
|
||||
public:
|
||||
void runOnMachineFunction(MachineFunction* mfunc) override;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class PostRA_Scheduler
|
||||
* @brief 寄存器分配后的局部指令调度器
|
||||
* * 主要目标是优化寄存器分配器插入的spill/fill代码(lw/sw),
|
||||
* 尝试将加载指令提前,以隐藏其访存延迟。
|
||||
*/
|
||||
class PostRA_Scheduler : public BackendPass {
|
||||
public:
|
||||
void runOnMachineFunction(MachineFunction* mfunc) override;
|
||||
};
|
||||
|
||||
// 此处为未来优化Pass的基类或独立类定义
|
||||
// 例如:
|
||||
// class PeepholeOptimizer {
|
||||
// public:
|
||||
// void runOnMachineFunction(MachineFunction* mfunc);
|
||||
// };
|
||||
|
||||
} // namespace sysy
|
||||
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
#define RISCV64_REGALLOC_H
|
||||
|
||||
#include "RISCv64LLIR.h"
|
||||
#include "RISCv64ISel.h" // 包含 RISCv64ISel.h 以访问 ISel 和 Value 类型
|
||||
|
||||
namespace sysy {
|
||||
|
||||
@ -35,9 +34,6 @@ private:
|
||||
// 辅助函数,获取指令的Use/Def集合
|
||||
void getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet& def);
|
||||
|
||||
// 辅助函数,处理调用约定
|
||||
void handleCallingConvention();
|
||||
|
||||
MachineFunction* MFunc;
|
||||
|
||||
// 活跃性分析结果
|
||||
@ -53,15 +49,6 @@ private:
|
||||
|
||||
// 可用的物理寄存器池
|
||||
std::vector<PhysicalReg> allocable_int_regs;
|
||||
|
||||
// 存储vreg到IR Value*的反向映射
|
||||
// 这个map将在run()函数开始时被填充,并在rewriteFunction()中使用。
|
||||
std::map<unsigned, Value*> vreg_to_value_map;
|
||||
std::map<PhysicalReg, unsigned> preg_to_vreg_id_map; // 物理寄存器到特殊vreg ID的映射
|
||||
|
||||
// 用于计算类型大小的辅助函数
|
||||
unsigned getTypeSizeInBytes(Type* type);
|
||||
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
|
||||
23
src/include/Reg2Mem.h
Normal file
23
src/include/Reg2Mem.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "IR.h"
|
||||
#include "IRBuilder.h"
|
||||
|
||||
namespace sysy {
|
||||
/**
|
||||
* Reg2Mem(后端未做phi指令翻译)
|
||||
*/
|
||||
class Reg2Mem {
|
||||
private:
|
||||
Module *pModule;
|
||||
IRBuilder *pBuilder;
|
||||
|
||||
public:
|
||||
Reg2Mem(Module *pMoudle, IRBuilder *pBuilder) : pModule(pMoudle), pBuilder(pBuilder) {}
|
||||
|
||||
void DeletePhiInst();
|
||||
// 删除UD关系, 因为删除了phi指令会修改ud关系
|
||||
void usedelete(Instruction *instr);
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
@ -1,196 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "IR.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 稀疏条件常量传播类
|
||||
// Sparse Conditional Constant Propagation
|
||||
/*
|
||||
伪代码
|
||||
function SCCP_Optimization(Module):
|
||||
for each Function in Module:
|
||||
changed = true
|
||||
while changed:
|
||||
changed = false
|
||||
// 阶段1: 常量传播与折叠
|
||||
changed |= PropagateConstants(Function)
|
||||
// 阶段2: 控制流简化
|
||||
changed |= SimplifyControlFlow(Function)
|
||||
end while
|
||||
end for
|
||||
|
||||
function PropagateConstants(Function):
|
||||
// 初始化
|
||||
executableBlocks = {entryBlock}
|
||||
valueState = map<Value, State> // 值->状态映射
|
||||
instWorkList = Queue()
|
||||
edgeWorkList = Queue()
|
||||
|
||||
// 初始化工作列表
|
||||
for each inst in entryBlock:
|
||||
instWorkList.push(inst)
|
||||
|
||||
// 迭代处理
|
||||
while !instWorkList.empty() || !edgeWorkList.empty():
|
||||
// 处理指令工作列表
|
||||
while !instWorkList.empty():
|
||||
inst = instWorkList.pop()
|
||||
// 如果指令是可执行基本块中的
|
||||
if executableBlocks.contains(inst.parent):
|
||||
ProcessInstruction(inst)
|
||||
|
||||
// 处理边工作列表
|
||||
while !edgeWorkList.empty():
|
||||
edge = edgeWorkList.pop()
|
||||
ProcessEdge(edge)
|
||||
|
||||
// 应用常量替换
|
||||
for each inst in Function:
|
||||
if valueState[inst] == CONSTANT:
|
||||
ReplaceWithConstant(inst, valueState[inst].constant)
|
||||
changed = true
|
||||
|
||||
return changed
|
||||
|
||||
function ProcessInstruction(Instruction inst):
|
||||
switch inst.type:
|
||||
//二元操作
|
||||
case BINARY_OP:
|
||||
lhs = GetValueState(inst.operands[0])
|
||||
rhs = GetValueState(inst.operands[1])
|
||||
if lhs == CONSTANT && rhs == CONSTANT:
|
||||
newState = ComputeConstant(inst.op, lhs.value, rhs.value)
|
||||
UpdateState(inst, newState)
|
||||
else if lhs == BOTTOM || rhs == BOTTOM:
|
||||
UpdateState(inst, BOTTOM)
|
||||
//phi
|
||||
case PHI:
|
||||
mergedState = ⊤
|
||||
for each incoming in inst.incomings:
|
||||
// 检查每个输入的状态
|
||||
if executableBlocks.contains(incoming.block):
|
||||
incomingState = GetValueState(incoming.value)
|
||||
mergedState = Meet(mergedState, incomingState)
|
||||
UpdateState(inst, mergedState)
|
||||
// 条件分支
|
||||
case COND_BRANCH:
|
||||
cond = GetValueState(inst.condition)
|
||||
if cond == CONSTANT:
|
||||
// 判断条件分支
|
||||
if cond.value == true:
|
||||
AddEdgeToWorkList(inst.parent, inst.trueTarget)
|
||||
else:
|
||||
AddEdgeToWorkList(inst.parent, inst.falseTarget)
|
||||
else if cond == BOTTOM:
|
||||
AddEdgeToWorkList(inst.parent, inst.trueTarget)
|
||||
AddEdgeToWorkList(inst.parent, inst.falseTarget)
|
||||
|
||||
case UNCOND_BRANCH:
|
||||
AddEdgeToWorkList(inst.parent, inst.target)
|
||||
|
||||
// 其他指令处理...
|
||||
|
||||
function ProcessEdge(Edge edge):
|
||||
fromBB, toBB = edge
|
||||
if !executableBlocks.contains(toBB):
|
||||
executableBlocks.add(toBB)
|
||||
for each inst in toBB:
|
||||
if inst is PHI:
|
||||
instWorkList.push(inst)
|
||||
else:
|
||||
instWorkList.push(inst) // 非PHI指令
|
||||
|
||||
// 更新PHI节点的输入
|
||||
for each phi in toBB.phis:
|
||||
instWorkList.push(phi)
|
||||
|
||||
function SimplifyControlFlow(Function):
|
||||
changed = false
|
||||
// 标记可达基本块
|
||||
ReachableBBs = FindReachableBlocks(Function.entry)
|
||||
|
||||
// 删除不可达块
|
||||
for each bb in Function.blocks:
|
||||
if !ReachableBBs.contains(bb):
|
||||
RemoveDeadBlock(bb)
|
||||
changed = true
|
||||
|
||||
// 简化条件分支
|
||||
for each bb in Function.blocks:
|
||||
terminator = bb.terminator
|
||||
if terminator is COND_BRANCH:
|
||||
cond = GetValueState(terminator.condition)
|
||||
if cond == CONSTANT:
|
||||
SimplifyBranch(terminator, cond.value)
|
||||
changed = true
|
||||
|
||||
return changed
|
||||
|
||||
function RemoveDeadBlock(BasicBlock bb):
|
||||
// 1. 更新前驱块的分支指令
|
||||
for each pred in bb.predecessors:
|
||||
UpdateTerminator(pred, bb)
|
||||
|
||||
// 2. 更新后继块的PHI节点
|
||||
for each succ in bb.successors:
|
||||
RemovePhiIncoming(succ, bb)
|
||||
|
||||
// 3. 删除块内所有指令
|
||||
for each inst in bb.instructions:
|
||||
inst.remove()
|
||||
|
||||
// 4. 从函数中移除基本块
|
||||
Function.removeBlock(bb)
|
||||
|
||||
function Meet(State a, State b):
|
||||
if a == ⊤: return b
|
||||
if b == ⊤: return a
|
||||
if a == ⊥ || b == ⊥: return ⊥
|
||||
if a.value == b.value: return a
|
||||
return ⊥
|
||||
|
||||
function UpdateState(Value v, State newState):
|
||||
oldState = valueState.get(v, ⊤)
|
||||
if newState != oldState:
|
||||
valueState[v] = newState
|
||||
for each user in v.users:
|
||||
if user is Instruction:
|
||||
instWorkList.push(user)
|
||||
|
||||
*/
|
||||
|
||||
enum class LatticeValue {
|
||||
Top, // ⊤ (Unknown)
|
||||
Constant, // c (Constant)
|
||||
Bottom // ⊥ (Undefined / Varying)
|
||||
};
|
||||
// LatticeValue: 用于表示值的状态,Top表示未知,Constant表示常量,Bottom表示未定义或变化的值。
|
||||
// 这里的LatticeValue用于跟踪每个SSA值(变量、指令结果)的状态,
|
||||
// 以便在SCCP过程中进行常量传播和控制流简化。
|
||||
|
||||
//TODO: 下列数据结构考虑集成到类中,避免重命名问题
|
||||
static std::set<Instruction *> Worklist;
|
||||
static std::unordered_set<BasicBlock*> Executable_Blocks;
|
||||
static std::queue<std::pair<BasicBlock *, BasicBlock *> > Executable_Edges;
|
||||
static std::map<Value*, LatticeValue> valueState;
|
||||
|
||||
class SCCP {
|
||||
private:
|
||||
Module *pModule;
|
||||
|
||||
public:
|
||||
SCCP(Module *pMoudle) : pModule(pMoudle) {}
|
||||
|
||||
void run();
|
||||
bool PropagateConstants(Function *function);
|
||||
bool SimplifyControlFlow(Function *function);
|
||||
void ProcessInstruction(Instruction *inst);
|
||||
void ProcessEdge(const std::pair<BasicBlock *, BasicBlock *> &edge);
|
||||
void RemoveDeadBlock(BasicBlock *bb);
|
||||
void UpdateState(Value *v, LatticeValue newState);
|
||||
LatticeValue Meet(LatticeValue a, LatticeValue b);
|
||||
LatticeValue GetValueState(Value *v);
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
340
src/include/SysYFormatter.h
Normal file
340
src/include/SysYFormatter.h
Normal file
@ -0,0 +1,340 @@
|
||||
#pragma once
|
||||
|
||||
#include "SysYBaseVisitor.h"
|
||||
#include "SysYParser.h"
|
||||
#include <ostream>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
class SysYFormatter : public SysYBaseVisitor {
|
||||
protected:
|
||||
std::ostream &os;
|
||||
int indent = 0;
|
||||
|
||||
public:
|
||||
SysYFormatter(std::ostream &os) : os(os), indent(0) {}
|
||||
|
||||
protected:
|
||||
struct Indentor {
|
||||
static constexpr int TabSize = 2;
|
||||
int &indent;
|
||||
Indentor(int &indent) : indent(indent) { indent += TabSize; }
|
||||
~Indentor() { indent -= TabSize; }
|
||||
};
|
||||
std::ostream &space() { return os << std::string(indent, ' '); }
|
||||
template <typename T>
|
||||
std::ostream &interleave(const T &container, const std::string sep = ", ") {
|
||||
auto b = container.begin(), e = container.end();
|
||||
(*b)->accept(this);
|
||||
for (b = std::next(b); b != e; b = std::next(b)) {
|
||||
os << sep;
|
||||
(*b)->accept(this);
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
public:
|
||||
// virtual std::any visitModule(SysYParser::ModuleContext *ctx) override {
|
||||
// return visitChildren(ctx);
|
||||
// }
|
||||
|
||||
virtual std::any visitBtype(SysYParser::BtypeContext *ctx) override {
|
||||
os << ctx->getText();
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitDecl(SysYParser::DeclContext *ctx) override {
|
||||
space();
|
||||
if (ctx->CONST())
|
||||
os << ctx->CONST()->getText() << ' ';
|
||||
ctx->btype()->accept(this);
|
||||
os << ' ';
|
||||
interleave(ctx->varDef(), ", ") << ';' << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitVarDef(SysYParser::VarDefContext *ctx) override {
|
||||
ctx->lValue()->accept(this);
|
||||
if (ctx->initValue()) {
|
||||
os << ' ' << '=' << ' ';
|
||||
ctx->initValue()->accept(this);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitInitValue(SysYParser::InitValueContext *ctx) override {
|
||||
if (not ctx->exp()) {
|
||||
os << '{';
|
||||
auto values = ctx->initValue();
|
||||
if (values.size())
|
||||
interleave(values, ", ");
|
||||
os << '}';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitFunc(SysYParser::FuncContext *ctx) override {
|
||||
ctx->funcType()->accept(this);
|
||||
os << ' ' << ctx->ID()->getText() << '(';
|
||||
if (ctx->funcFParams())
|
||||
ctx->funcFParams()->accept(this);
|
||||
os << ')' << ' ';
|
||||
ctx->blockStmt()->accept(this);
|
||||
os << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitFuncType(SysYParser::FuncTypeContext *ctx) override {
|
||||
os << ctx->getText();
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any
|
||||
visitFuncFParams(SysYParser::FuncFParamsContext *ctx) override {
|
||||
interleave(ctx->funcFParam(), ", ");
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any
|
||||
visitFuncFParam(SysYParser::FuncFParamContext *ctx) override {
|
||||
ctx->btype()->accept(this);
|
||||
os << ' ' << ctx->ID()->getText();
|
||||
if (not ctx->LBRACKET().empty()) {
|
||||
os << '[';
|
||||
auto exp = ctx->exp();
|
||||
if (not exp.empty()) {
|
||||
os << '[';
|
||||
interleave(exp, "][") << ']';
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitBlockStmt(SysYParser::BlockStmtContext *ctx) override {
|
||||
os << '{' << '\n';
|
||||
{
|
||||
Indentor indentor(indent);
|
||||
auto items = ctx->blockItem();
|
||||
if (not items.empty())
|
||||
interleave(items, "");
|
||||
}
|
||||
space() << ctx->RBRACE()->getText() << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
// virtual std::any visitBlockItem(SysYParser::BlockItemContext *ctx)
|
||||
// override {
|
||||
// return visitChildren(ctx);
|
||||
// }
|
||||
|
||||
// virtual std::any visitStmt(SysYParser::StmtContext *ctx) override {
|
||||
// return visitChildren(ctx);
|
||||
// }
|
||||
|
||||
virtual std::any
|
||||
visitAssignStmt(SysYParser::AssignStmtContext *ctx) override {
|
||||
space();
|
||||
ctx->lValue()->accept(this);
|
||||
os << " = ";
|
||||
ctx->exp()->accept(this);
|
||||
os << ';' << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitExpStmt(SysYParser::ExpStmtContext *ctx) override {
|
||||
space();
|
||||
ctx->exp()->accept(this);
|
||||
os << ';' << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
void wrapBlock(SysYParser::StmtContext *stmt) {
|
||||
bool isBlock = stmt->blockStmt();
|
||||
if (isBlock) {
|
||||
stmt->accept(this);
|
||||
} else {
|
||||
os << "{\n";
|
||||
{
|
||||
Indentor indentor(indent);
|
||||
stmt->accept(this);
|
||||
}
|
||||
space() << "}\n";
|
||||
}
|
||||
};
|
||||
virtual std::any visitIfStmt(SysYParser::IfStmtContext *ctx) override {
|
||||
space();
|
||||
os << ctx->IF()->getText() << " (";
|
||||
ctx->exp()->accept(this);
|
||||
os << ") ";
|
||||
auto stmt = ctx->stmt();
|
||||
auto ifStmt = stmt[0];
|
||||
wrapBlock(ifStmt);
|
||||
if (stmt.size() == 2) {
|
||||
auto elseStmt = stmt[1];
|
||||
wrapBlock(elseStmt);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitWhileStmt(SysYParser::WhileStmtContext *ctx) override {
|
||||
space();
|
||||
os << ctx->WHILE()->getText() << " (";
|
||||
ctx->exp()->accept(this);
|
||||
os << ") ";
|
||||
wrapBlock(ctx->stmt());
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitBreakStmt(SysYParser::BreakStmtContext *ctx) override {
|
||||
space() << ctx->BREAK()->getText() << ';' << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any
|
||||
visitContinueStmt(SysYParser::ContinueStmtContext *ctx) override {
|
||||
space() << ctx->CONTINUE()->getText() << ';' << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any
|
||||
visitReturnStmt(SysYParser::ReturnStmtContext *ctx) override {
|
||||
space() << ctx->RETURN()->getText();
|
||||
if (ctx->exp()) {
|
||||
os << ' ';
|
||||
ctx->exp()->accept(this);
|
||||
}
|
||||
os << ';' << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
// virtual std::any visitEmptyStmt(SysYParser::EmptyStmtContext *ctx)
|
||||
// override {
|
||||
// return visitChildren(ctx);
|
||||
// }
|
||||
|
||||
virtual std::any
|
||||
visitRelationExp(SysYParser::RelationExpContext *ctx) override {
|
||||
auto lhs = ctx->exp(0);
|
||||
auto rhs = ctx->exp(1);
|
||||
std::string op =
|
||||
ctx->LT() ? "<" : (ctx->LE() ? "<=" : (ctx->GT() ? ">" : ">="));
|
||||
lhs->accept(this);
|
||||
os << ' ' << op << ' ';
|
||||
rhs->accept(this);
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any
|
||||
visitMultiplicativeExp(SysYParser::MultiplicativeExpContext *ctx) override {
|
||||
auto lhs = ctx->exp(0);
|
||||
auto rhs = ctx->exp(1);
|
||||
std::string op = ctx->MUL() ? "*" : (ctx->DIV() ? "/" : "%");
|
||||
lhs->accept(this);
|
||||
os << ' ' << op << ' ';
|
||||
rhs->accept(this);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// virtual std::any visitLValueExp(SysYParser::LValueExpContext *ctx)
|
||||
// override {
|
||||
// return visitChildren(ctx);
|
||||
// }
|
||||
|
||||
// virtual std::any visitNumberExp(SysYParser::NumberExpContext *ctx)
|
||||
// override {
|
||||
// return visitChildren(ctx);
|
||||
// }
|
||||
|
||||
virtual std::any visitAndExp(SysYParser::AndExpContext *ctx) override {
|
||||
ctx->exp(0)->accept(this);
|
||||
os << " && ";
|
||||
ctx->exp(1)->accept(this);
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitUnaryExp(SysYParser::UnaryExpContext *ctx) override {
|
||||
std::string op = ctx->ADD() ? "+" : (ctx->SUB() ? "-" : "!");
|
||||
os << op;
|
||||
ctx->exp()->accept(this);
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitParenExp(SysYParser::ParenExpContext *ctx) override {
|
||||
os << '(';
|
||||
ctx->exp()->accept(this);
|
||||
os << ')';
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitStringExp(SysYParser::StringExpContext *ctx) override {
|
||||
return visitChildren(ctx);
|
||||
}
|
||||
|
||||
virtual std::any visitOrExp(SysYParser::OrExpContext *ctx) override {
|
||||
ctx->exp(0)->accept(this);
|
||||
os << " || ";
|
||||
ctx->exp(1)->accept(this);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// virtual std::any visitCallExp(SysYParser::CallExpContext *ctx) override {
|
||||
// return visitChildren(ctx);
|
||||
// }
|
||||
|
||||
virtual std::any
|
||||
visitAdditiveExp(SysYParser::AdditiveExpContext *ctx) override {
|
||||
auto lhs = ctx->exp(0);
|
||||
auto rhs = ctx->exp(1);
|
||||
std::string op = ctx->ADD() ? "+" : "-";
|
||||
lhs->accept(this);
|
||||
os << ' ' << op << ' ';
|
||||
rhs->accept(this);
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitEqualExp(SysYParser::EqualExpContext *ctx) override {
|
||||
auto lhs = ctx->exp(0);
|
||||
auto rhs = ctx->exp(1);
|
||||
std::string op = ctx->EQ() ? "==" : "!=";
|
||||
lhs->accept(this);
|
||||
os << ' ' << op << ' ';
|
||||
rhs->accept(this);
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitCall(SysYParser::CallContext *ctx) override {
|
||||
os << ctx->ID()->getText() << '(';
|
||||
if (ctx->funcRParams())
|
||||
ctx->funcRParams()->accept(this);
|
||||
os << ')';
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitLValue(SysYParser::LValueContext *ctx) override {
|
||||
os << ctx->ID()->getText();
|
||||
auto exp = ctx->exp();
|
||||
if (not exp.empty()) {
|
||||
os << '[';
|
||||
interleave(exp, "][") << ']';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitNumber(SysYParser::NumberContext *ctx) override {
|
||||
os << ctx->getText();
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any visitString(SysYParser::StringContext *ctx) override {
|
||||
os << ctx->getText();
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::any
|
||||
visitFuncRParams(SysYParser::FuncRParamsContext *ctx) override {
|
||||
interleave(ctx->exp(), ", ");
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
@ -1,101 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "IR.h"
|
||||
#include "IRBuilder.h"
|
||||
#include "Pass.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 优化前对SysY IR的预处理,也可以视作部分CFG优化
|
||||
// 主要包括删除无用指令、合并基本块、删除空块等
|
||||
// 这些操作可以在SysY IR生成时就完成,但为了简化IR生成过程,
|
||||
// 这里将其放在SysY IR生成后进行预处理
|
||||
// 同时兼容phi节点的处理,可以再mem2reg后再次调用优化
|
||||
|
||||
//TODO: 可增加的CFG优化和方法
|
||||
// - 检查基本块跳转关系正确性
|
||||
// - 简化条件分支(Branch Simplification),如条件恒真/恒假转为直接跳转
|
||||
// - 合并连续的跳转指令(Jump Threading)在合并不可达块中似乎已经实现了
|
||||
// - 基本块重排序(Block Reordering),提升局部性
|
||||
|
||||
// 辅助工具类,包含实际的CFG优化逻辑
|
||||
// 这些方法可以被独立的Pass调用
|
||||
class SysYCFGOptUtils {
|
||||
public:
|
||||
static bool SysYDelInstAfterBr(Function *func); // 删除br后面的指令
|
||||
static bool SysYDelEmptyBlock(Function *func, IRBuilder* pBuilder); // 空块删除
|
||||
static bool SysYDelNoPreBLock(Function *func); // 删除无前驱块(不可达块)
|
||||
static bool SysYBlockMerge(Function *func); // 合并基本块
|
||||
static bool SysYAddReturn(Function *func, IRBuilder* pBuilder); // 添加return指令
|
||||
static bool SysYCondBr2Br(Function *func, IRBuilder* pBuilder); // 条件分支转换为无条件分支
|
||||
};
|
||||
|
||||
// ======================================================================
|
||||
// 独立的CFG优化遍
|
||||
// ======================================================================
|
||||
|
||||
class SysYDelInstAfterBrPass : public OptimizationPass {
|
||||
public:
|
||||
static void *ID; // 唯一ID
|
||||
SysYDelInstAfterBrPass() : OptimizationPass("SysYDelInstAfterBrPass", Granularity::Function) {}
|
||||
bool runOnFunction(Function *F, AnalysisManager& AM) override;
|
||||
void getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const override {
|
||||
// 这个优化可能改变CFG结构,使一些CFG相关的分析失效
|
||||
// 可以在这里指定哪些分析会失效,例如支配树、活跃变量等
|
||||
// analysisInvalidations.insert(DominatorTreeAnalysisPass::ID); // 示例
|
||||
}
|
||||
void *getPassID() const override { return &ID; }
|
||||
};
|
||||
|
||||
class SysYDelEmptyBlockPass : public OptimizationPass {
|
||||
private:
|
||||
IRBuilder *pBuilder;
|
||||
public:
|
||||
static void *ID;
|
||||
SysYDelEmptyBlockPass(IRBuilder *builder) : OptimizationPass("SysYDelEmptyBlockPass", Granularity::Function), pBuilder(builder) {}
|
||||
bool runOnFunction(Function *F, AnalysisManager& AM) override;
|
||||
void getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const override {};
|
||||
void *getPassID() const override { return &ID; }
|
||||
};
|
||||
|
||||
class SysYDelNoPreBLockPass : public OptimizationPass {
|
||||
public:
|
||||
static void *ID;
|
||||
SysYDelNoPreBLockPass() : OptimizationPass("SysYDelNoPreBLockPass", Granularity::Function) {}
|
||||
bool runOnFunction(Function *F, AnalysisManager& AM) override;
|
||||
void getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const override {};
|
||||
void *getPassID() const override { return &ID; }
|
||||
};
|
||||
|
||||
class SysYBlockMergePass : public OptimizationPass {
|
||||
public:
|
||||
static void *ID;
|
||||
SysYBlockMergePass() : OptimizationPass("SysYBlockMergePass", Granularity::Function) {}
|
||||
bool runOnFunction(Function *F, AnalysisManager& AM) override;
|
||||
void getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const override {};
|
||||
void *getPassID() const override { return &ID; }
|
||||
};
|
||||
|
||||
class SysYAddReturnPass : public OptimizationPass {
|
||||
private:
|
||||
IRBuilder *pBuilder;
|
||||
public:
|
||||
static void *ID;
|
||||
SysYAddReturnPass(IRBuilder *builder) : OptimizationPass("SysYAddReturnPass", Granularity::Function), pBuilder(builder) {}
|
||||
bool runOnFunction(Function *F, AnalysisManager& AM) override;
|
||||
void getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const override {};
|
||||
void *getPassID() const override { return &ID; }
|
||||
};
|
||||
|
||||
class SysYCondBr2BrPass : public OptimizationPass {
|
||||
private:
|
||||
IRBuilder *pBuilder;
|
||||
public:
|
||||
static void *ID;
|
||||
SysYCondBr2BrPass(IRBuilder *builder) : OptimizationPass("SysYCondBr2BrPass", Granularity::Function), pBuilder(builder) {}
|
||||
bool runOnFunction(Function *F, AnalysisManager& AM) override;
|
||||
void getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const override {};
|
||||
void *getPassID() const override { return &ID; }
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
@ -62,11 +62,12 @@ private:
|
||||
public:
|
||||
SysYIRGenerator() = default;
|
||||
|
||||
bool HasReturnInst;
|
||||
|
||||
public:
|
||||
Module *get() const { return module.get(); }
|
||||
IRBuilder *getBuilder(){ return &builder; }
|
||||
public:
|
||||
|
||||
std::any visitCompUnit(SysYParser::CompUnitContext *ctx) override;
|
||||
|
||||
std::any visitGlobalConstDecl(SysYParser::GlobalConstDeclContext *ctx) override;
|
||||
@ -133,13 +134,6 @@ public:
|
||||
|
||||
// std::any visitConstExp(SysYParser::ConstExpContext *ctx) override;
|
||||
|
||||
public:
|
||||
// 获取GEP指令的地址
|
||||
Value* getGEPAddressInst(Value* basePointer, const std::vector<Value*>& indices);
|
||||
// 构建数组类型
|
||||
Type* buildArrayType(Type* baseType, const std::vector<Value*>& dims);
|
||||
|
||||
unsigned countArrayDimensions(Type* type);
|
||||
|
||||
}; // class SysYIRGenerator
|
||||
|
||||
|
||||
37
src/include/SysYIROptPre.h
Normal file
37
src/include/SysYIROptPre.h
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "IR.h"
|
||||
#include "IRBuilder.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 优化前对SysY IR的预处理,也可以视作部分CFG优化
|
||||
// 主要包括删除无用指令、合并基本块、删除空块等
|
||||
// 这些操作可以在SysY IR生成时就完成,但为了简化IR生成过程,
|
||||
// 这里将其放在SysY IR生成后进行预处理
|
||||
// 同时兼容phi节点的处理,可以再mem2reg后再次调用优化
|
||||
class SysYOptPre {
|
||||
private:
|
||||
Module *pModule;
|
||||
IRBuilder *pBuilder;
|
||||
|
||||
public:
|
||||
SysYOptPre(Module *pMoudle, IRBuilder *pBuilder) : pModule(pMoudle), pBuilder(pBuilder) {}
|
||||
|
||||
void SysYOptimizateAfterIR(){
|
||||
SysYDelInstAfterBr();
|
||||
SysYBlockMerge();
|
||||
SysYDelNoPreBLock();
|
||||
SysYDelEmptyBlock();
|
||||
SysYAddReturn();
|
||||
}
|
||||
void SysYDelInstAfterBr(); // 删除br后面的指令
|
||||
void SysYDelEmptyBlock(); // 空块删除
|
||||
void SysYDelNoPreBLock(); // 删除无前驱块
|
||||
void SysYBlockMerge(); // 合并基本块(主要针对嵌套if while的exit块,
|
||||
// 也可以修改IR生成实现回填机制
|
||||
void SysYAddReturn(); // 添加return指令(主要针对Void函数)
|
||||
void usedelete(Instruction *instr); // use删除
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
@ -1,33 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "IR.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 优化工具类,包含一些通用的优化方法
|
||||
// 这些方法可以在不同的优化 pass 中复用
|
||||
// 例如:删除use关系,判断是否是全局变量等
|
||||
class SysYIROptUtils{
|
||||
|
||||
public:
|
||||
// 仅仅删除use关系
|
||||
static void usedelete(Instruction *instr) {
|
||||
for (auto &use : instr->getOperands()) {
|
||||
Value* val = use->getValue();
|
||||
val->removeUse(use);
|
||||
}
|
||||
}
|
||||
|
||||
// 判断是否是全局变量
|
||||
static bool isGlobal(Value *val) {
|
||||
auto gval = dynamic_cast<GlobalValue *>(val);
|
||||
return gval != nullptr;
|
||||
}
|
||||
// 判断是否是数组
|
||||
static bool isArr(Value *val) {
|
||||
auto aval = dynamic_cast<AllocaInst *>(val);
|
||||
return aval != nullptr && aval->getNumDims() != 0;
|
||||
}
|
||||
};
|
||||
|
||||
}// namespace sysy
|
||||
@ -1,58 +0,0 @@
|
||||
// PassManager.h
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <typeindex> // For std::type_index
|
||||
#include <unordered_map>
|
||||
#include "SysYIRPass.h"
|
||||
#include "IR.h" // 假设你的IR.h定义了Module, Function等
|
||||
|
||||
namespace sysy {
|
||||
|
||||
class PassManager {
|
||||
public:
|
||||
PassManager() = default;
|
||||
|
||||
// 添加一个FunctionPass
|
||||
void addPass(std::unique_ptr<FunctionPass> pass) {
|
||||
functionPasses.push_back(std::move(pass));
|
||||
}
|
||||
|
||||
// 添加一个ModulePass
|
||||
void addPass(std::unique_ptr<ModulePass> pass) {
|
||||
modulePasses.push_back(std::move(pass));
|
||||
}
|
||||
|
||||
// 添加一个AnalysisPass
|
||||
template<typename T, typename... Args>
|
||||
T* addAnalysisPass(Args&&... args) {
|
||||
static_assert(std::is_base_of<AnalysisPass, T>::value, "T must derive from AnalysisPass");
|
||||
auto analysis = std::make_unique<T>(std::forward<Args>(args)...);
|
||||
T* rawPtr = analysis.get();
|
||||
analysisPasses[std::type_index(typeid(T))] = std::move(analysis);
|
||||
return rawPtr;
|
||||
}
|
||||
|
||||
// 获取分析结果(用于其他Pass访问)
|
||||
template<typename T>
|
||||
T* getAnalysis() {
|
||||
static_assert(std::is_base_of<AnalysisPass, T>::value, "T must derive from AnalysisPass");
|
||||
auto it = analysisPasses.find(std::type_index(typeid(T)));
|
||||
if (it != analysisPasses.end()) {
|
||||
return static_cast<T*>(it->second.get());
|
||||
}
|
||||
return nullptr; // 或者抛出异常
|
||||
}
|
||||
|
||||
// 运行所有注册的遍
|
||||
void run(Module& M);
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<FunctionPass>> functionPasses;
|
||||
std::vector<std::unique_ptr<ModulePass>> modulePasses;
|
||||
std::unordered_map<std::type_index, std::unique_ptr<AnalysisPass>> analysisPasses;
|
||||
// 未来可以添加AnalysisPass的缓存机制
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
@ -13,10 +13,13 @@ using namespace antlr4;
|
||||
|
||||
#include "SysYIRGenerator.h"
|
||||
#include "SysYIRPrinter.h"
|
||||
#include "SysYIRCFGOpt.h" // 包含 CFG 优化
|
||||
#include "SysYIROptPre.h"
|
||||
#include "RISCv64Backend.h"
|
||||
#include "Pass.h" // 包含新的 Pass 框架
|
||||
#include "SysYIRAnalyser.h"
|
||||
// #include "DeadCodeElimination.h"
|
||||
#include "AddressCalculationExpansion.h"
|
||||
// #include "Mem2Reg.h"
|
||||
// #include "Reg2Mem.h"
|
||||
|
||||
using namespace sysy;
|
||||
|
||||
@ -128,20 +131,19 @@ int main(int argc, char **argv) {
|
||||
if (argStopAfter == "ird") {
|
||||
DEBUG = 1; // 这里可能需要更精细地控制 DEBUG 的开启时机和范围
|
||||
}
|
||||
|
||||
// 默认优化 pass (在所有优化级别都会执行)
|
||||
SysYOptPre optPre(moduleIR, builder);
|
||||
optPre.SysYOptimizateAfterIR();
|
||||
|
||||
ControlFlowAnalysis cfa(moduleIR);
|
||||
cfa.init();
|
||||
ActiveVarAnalysis ava;
|
||||
ava.init(moduleIR);
|
||||
|
||||
if (DEBUG) {
|
||||
cout << "=== Init IR ===\n";
|
||||
cout << "=== After CFA & AVA (Default) ===\n";
|
||||
SysYPrinter(moduleIR).printIR(); // 临时打印器用于调试
|
||||
}
|
||||
|
||||
// 创建 Pass 管理器并运行优化管道
|
||||
PassManager passManager(moduleIR, builder); // 创建 Pass 管理器
|
||||
// 好像都不用传递module和builder了,因为 PassManager 初始化了
|
||||
passManager.runOptimizationPipeline(moduleIR, builder, optLevel);
|
||||
|
||||
|
||||
|
||||
|
||||
AddressCalculationExpansion ace(moduleIR, builder);
|
||||
if (ace.run()) {
|
||||
if (DEBUG) cout << "AddressCalculationExpansion made changes.\n";
|
||||
@ -216,7 +218,7 @@ int main(int argc, char **argv) {
|
||||
// 设置 DEBUG 模式(如果指定了 'asmd')
|
||||
if (argStopAfter == "asmd") {
|
||||
DEBUG = 1;
|
||||
DEEPDEBUG = 1;
|
||||
// DEEPDEBUG = 1;
|
||||
}
|
||||
sysy::RISCv64CodeGen codegen(moduleIR); // 传入优化后的 moduleIR
|
||||
string asmCode = codegen.code_gen();
|
||||
|
||||
@ -1,227 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# runit-riscv64-single.sh - 用于在 RISC-V 虚拟机内部测试单个或少量 .s 文件的脚本
|
||||
# 模仿 runit-riscv64.sh 的功能,但以具体文件路径作为输入。
|
||||
|
||||
# --- 配置区 ---
|
||||
# 假设此脚本位于项目根目录 (例如 /home/ubuntu/debug)
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
|
||||
LIB_DIR="${SCRIPT_DIR}/lib"
|
||||
TMP_DIR="${SCRIPT_DIR}/tmp" # 临时可执行文件将存放在这里
|
||||
TESTDATA_DIR="${SCRIPT_DIR}/testdata" # 用于查找 .in/.out 文件
|
||||
|
||||
# 定义编译器
|
||||
GCC_NATIVE="gcc" # VM 内部的原生 gcc
|
||||
|
||||
# --- 初始化变量 ---
|
||||
GCC_TIMEOUT=10 # gcc 编译超时 (秒)
|
||||
EXEC_TIMEOUT=5 # 程序自动化执行超时 (秒)
|
||||
MAX_OUTPUT_LINES=50 # 对比失败时显示的最大行数
|
||||
S_FILES=() # 存储用户提供的 .s 文件列表
|
||||
PASSED_CASES=0
|
||||
FAILED_CASES_LIST=""
|
||||
|
||||
# --- 函数定义 ---
|
||||
show_help() {
|
||||
echo "用法: $0 [文件1.s] [文件2.s] ... [选项]"
|
||||
echo "在 VM 内部编译并测试指定的 .s 文件。"
|
||||
echo ""
|
||||
echo "如果找到对应的 .in/.out 文件,则进行自动化测试。否则,进入交互模式。"
|
||||
echo ""
|
||||
echo "选项:"
|
||||
echo " -ct N 设置 gcc 编译超时为 N 秒 (默认: 10)。"
|
||||
echo " -t N 设置程序自动化执行超时为 N 秒 (默认: 5)。"
|
||||
echo " -ml N, --max-lines N 当输出对比失败时,最多显示 N 行内容 (默认: 50)。"
|
||||
echo " -h, --help 显示此帮助信息并退出。"
|
||||
}
|
||||
|
||||
# 显示文件内容并根据行数截断的函数
|
||||
display_file_content() {
|
||||
local file_path="$1"
|
||||
local title="$2"
|
||||
local max_lines="$3"
|
||||
|
||||
if [ ! -f "$file_path" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo -e "$title"
|
||||
local line_count
|
||||
line_count=$(wc -l < "$file_path")
|
||||
|
||||
if [ "$line_count" -gt "$max_lines" ]; then
|
||||
head -n "$max_lines" "$file_path"
|
||||
echo -e "\e[33m[... 输出已截断,共 ${line_count} 行 ...]\e[0m"
|
||||
else
|
||||
cat "$file_path"
|
||||
fi
|
||||
}
|
||||
|
||||
# --- 参数解析 ---
|
||||
# 从参数中分离出 .s 文件和选项
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
-ct|-t|-ml|--max-lines)
|
||||
# 选项和其值将在下一个循环中处理
|
||||
;;
|
||||
-h|--help)
|
||||
show_help
|
||||
exit 0
|
||||
;;
|
||||
-*)
|
||||
# 检查是否是带值的选项
|
||||
if ! [[ ${args_processed+x} ]]; then
|
||||
args_processed=true # 标记已处理过参数
|
||||
while [[ "$#" -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-ct) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then GCC_TIMEOUT="$2"; shift; else echo "错误: -ct 需要一个正整数参数。" >&2; exit 1; fi ;;
|
||||
-t) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then EXEC_TIMEOUT="$2"; shift; else echo "错误: -t 需要一个正整数参数。" >&2; exit 1; fi ;;
|
||||
-ml|--max-lines) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then MAX_OUTPUT_LINES="$2"; shift; else echo "错误: --max-lines 需要一个正整数参数。" >&2; exit 1; fi ;;
|
||||
*.s) S_FILES+=("$1") ;;
|
||||
*) if ! [[ "$1" =~ ^[0-9]+$ ]]; then echo "未知选项或无效文件: $1"; show_help; exit 1; fi ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
fi
|
||||
;;
|
||||
*.s)
|
||||
if [[ -f "$arg" ]]; then
|
||||
S_FILES+=("$arg")
|
||||
else
|
||||
echo "警告: 文件不存在,已忽略: $arg"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# --- 主逻辑开始 ---
|
||||
if [ ${#S_FILES[@]} -eq 0 ]; then
|
||||
echo "错误: 未提供任何 .s 文件作为输入。"
|
||||
show_help
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "${TMP_DIR}"
|
||||
TOTAL_CASES=${#S_FILES[@]}
|
||||
|
||||
echo "SysY VM 内单例测试运行器启动..."
|
||||
echo "超时设置: gcc=${GCC_TIMEOUT}s, 运行=${EXEC_TIMEOUT}s"
|
||||
echo "失败输出最大行数: ${MAX_OUTPUT_LINES}"
|
||||
echo ""
|
||||
|
||||
for s_file in "${S_FILES[@]}"; do
|
||||
is_passed=1
|
||||
|
||||
# 从 .s 文件名反向推导原始测试用例路径
|
||||
base_name_from_s_file=$(basename "$s_file" .s)
|
||||
original_test_name_underscored=$(echo "$base_name_from_s_file" | sed 's/_sysyc_riscv64$//')
|
||||
category=$(echo "$original_test_name_underscored" | cut -d'_' -f1)
|
||||
test_file_base=$(echo "$original_test_name_underscored" | cut -d'_' -f2-)
|
||||
original_relative_path="${category}/${test_file_base}"
|
||||
|
||||
executable_file="${TMP_DIR}/${base_name_from_s_file}"
|
||||
input_file="${TESTDATA_DIR}/${original_relative_path}.in"
|
||||
output_reference_file="${TESTDATA_DIR}/${original_relative_path}.out"
|
||||
output_actual_file="${TMP_DIR}/${base_name_from_s_file}.actual_out"
|
||||
|
||||
echo "======================================================================"
|
||||
echo "正在处理: ${s_file}"
|
||||
echo " (关联测试用例: ${original_relative_path}.sy)"
|
||||
|
||||
# 步骤 1: GCC 编译
|
||||
echo " 使用 gcc 编译 (超时 ${GCC_TIMEOUT}s)..."
|
||||
timeout -s KILL ${GCC_TIMEOUT} "${GCC_NATIVE}" "${s_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static -g
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "\e[31m错误: GCC 编译失败或超时。\e[0m"
|
||||
is_passed=0
|
||||
fi
|
||||
|
||||
# 步骤 2: 执行与测试
|
||||
if [ "$is_passed" -eq 1 ]; then
|
||||
# 检查是自动化测试还是交互模式
|
||||
if [ -f "${input_file}" ] || [ -f "${output_reference_file}" ]; then
|
||||
# --- 自动化测试模式 ---
|
||||
echo " 检测到 .in/.out 文件,进入自动化测试模式..."
|
||||
echo " 正在执行 (超时 ${EXEC_TIMEOUT}s)..."
|
||||
|
||||
exec_cmd="\"${executable_file}\""
|
||||
[ -f "${input_file}" ] && exec_cmd+=" < \"${input_file}\""
|
||||
exec_cmd+=" > \"${output_actual_file}\""
|
||||
|
||||
eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}"
|
||||
ACTUAL_RETURN_CODE=$?
|
||||
|
||||
if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then
|
||||
echo -e "\e[31m 执行超时。\e[0m"
|
||||
is_passed=0
|
||||
else
|
||||
if [ -f "${output_reference_file}" ]; then
|
||||
LAST_LINE_TRIMMED=$(tail -n 1 "${output_reference_file}" | tr -d '[:space:]')
|
||||
if [[ "$LAST_LINE_TRIMMED" =~ ^[-+]?[0-9]+$ ]]; then
|
||||
EXPECTED_RETURN_CODE="$LAST_LINE_TRIMMED"
|
||||
EXPECTED_STDOUT_FILE="${TMP_DIR}/${base_name_from_s_file}.expected_stdout"
|
||||
head -n -1 "${output_reference_file}" > "${EXPECTED_STDOUT_FILE}"
|
||||
if [ "$ACTUAL_RETURN_CODE" -ne "$EXPECTED_RETURN_CODE" ]; then echo -e "\e[31m 返回码测试失败: 期望 ${EXPECTED_RETURN_CODE}, 实际 ${ACTUAL_RETURN_CODE}\e[0m"; is_passed=0; fi
|
||||
if ! diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then
|
||||
echo -e "\e[31m 标准输出测试失败。\e[0m"; is_passed=0
|
||||
display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
display_file_content "${output_actual_file}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
echo -e " \e[36m----------------\e[0m"
|
||||
fi
|
||||
else
|
||||
if ! diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${output_reference_file}") >/dev/null 2>&1; then
|
||||
echo -e "\e[32m 标准输出测试成功。\e[0m"
|
||||
else
|
||||
echo -e "\e[31m 标准输出测试失败。\e[0m"; is_passed=0
|
||||
display_file_content "${output_reference_file}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
display_file_content "${output_actual_file}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
echo -e " \e[36m----------------\e[0m"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo " 无参考输出文件。程序返回码: ${ACTUAL_RETURN_CODE}"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
# --- 交互模式 ---
|
||||
echo -e "\e[33m"
|
||||
echo " **********************************************************"
|
||||
echo " ** 未找到 .in 或 .out 文件,进入交互模式。 **"
|
||||
echo " ** 程序即将运行,你可以直接在终端中输入。 **"
|
||||
echo " ** 按下 Ctrl+D (EOF) 或以其他方式结束程序以继续。 **"
|
||||
echo " **********************************************************"
|
||||
echo -e "\e[0m"
|
||||
"${executable_file}"
|
||||
INTERACTIVE_RET_CODE=$?
|
||||
echo -e "\e[33m\n 交互模式执行完毕,程序返回码: ${INTERACTIVE_RET_CODE}\e[0m"
|
||||
echo " 注意: 交互模式的结果未经验证。"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$is_passed" -eq 1 ]; then
|
||||
echo -e "\e[32m状态: 通过\e[0m"
|
||||
((PASSED_CASES++))
|
||||
else
|
||||
echo -e "\e[31m状态: 失败\e[0m"
|
||||
FAILED_CASES_LIST+="${original_relative_path}.sy\n"
|
||||
fi
|
||||
done
|
||||
|
||||
# --- 打印最终总结 ---
|
||||
echo "======================================================================"
|
||||
echo "所有测试完成"
|
||||
echo "测试通过率: [${PASSED_CASES}/${TOTAL_CASES}]"
|
||||
|
||||
if [ -n "$FAILED_CASES_LIST" ]; then
|
||||
echo ""
|
||||
echo -e "\e[31m未通过的测例:\e[0m"
|
||||
echo -e "${FAILED_CASES_LIST}"
|
||||
fi
|
||||
|
||||
echo "======================================================================"
|
||||
|
||||
if [ "$PASSED_CASES" -eq "$TOTAL_CASES" ]; then
|
||||
exit 0
|
||||
else
|
||||
exit 1
|
||||
fi
|
||||
@ -1,8 +1,16 @@
|
||||
#!/bin/bash
|
||||
|
||||
# runit-riscv64.sh - 用于在 RISC-V 虚拟机内部汇编、链接和测试 SysY 程序的脚本
|
||||
# run_vm_tests.sh - 用于在 RISC-V 虚拟机内部汇编、链接和测试 SysY 程序的脚本
|
||||
# 此脚本应位于您的项目根目录 (例如 /home/ubuntu/debug)
|
||||
# 假设当前运行环境已经是 RISC-V 64 位架构,可以直接执行编译后的程序。
|
||||
# 脚本的目录结构应该为:
|
||||
# .
|
||||
# ├── runit.sh
|
||||
# ├── lib
|
||||
# │ └── libsysy_riscv.a
|
||||
# └── testdata
|
||||
# ├── functional
|
||||
# └── performance
|
||||
|
||||
# 定义相对于脚本位置的目录
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
|
||||
@ -14,47 +22,30 @@ TESTDATA_DIR="${SCRIPT_DIR}/testdata"
|
||||
GCC_NATIVE="gcc" # VM 内部的 gcc
|
||||
|
||||
# --- 新增功能: 初始化变量 ---
|
||||
GCC_TIMEOUT=10 # 默认 gcc 编译超时 (秒)
|
||||
EXEC_TIMEOUT=5 # 默认运行时超时 (秒)
|
||||
MAX_OUTPUT_LINES=50 # 对比失败时显示的最大行数
|
||||
TIMEOUT_SECONDS=5 # 默认运行时超时时间为 5 秒
|
||||
COMPILE_TIMEOUT_SECONDS=10 # 默认编译超时时间为 10 秒
|
||||
TOTAL_CASES=0
|
||||
PASSED_CASES=0
|
||||
FAILED_CASES_LIST="" # 用于存储未通过的测例列表
|
||||
|
||||
# 显示帮助信息的函数
|
||||
show_help() {
|
||||
echo "用法: $0 [选项]"
|
||||
echo "此脚本用于在 RISC-V 虚拟机内部,对之前生成的 .s 汇编文件进行汇编、链接和测试。"
|
||||
echo "测试会按文件名升序进行。"
|
||||
echo "假设当前运行环境已经是 RISC-V 64 位架构,可以直接执行编译后的程序。"
|
||||
echo ""
|
||||
echo "选项:"
|
||||
echo " -c, --clean 清理 'tmp' 目录下的所有生成文件。"
|
||||
echo " -ct M 设置 gcc 编译的超时时间为 M 秒 (默认: 10)。"
|
||||
echo " -t N 设置每个测试用例的运行时超时为 N 秒 (默认: 5)。"
|
||||
echo " -ml N, --max-lines N 当输出对比失败时,最多显示 N 行内容 (默认: 50)。"
|
||||
echo " -t, --timeout N 设置每个测试用例的运行时超时为 N 秒 (默认: 5)。"
|
||||
echo " -ct, --compile-timeout M 设置 gcc 编译的超时时间为 M 秒 (默认: 10)。"
|
||||
echo " -h, --help 显示此帮助信息并退出。"
|
||||
}
|
||||
|
||||
# 显示文件内容并根据行数截断的函数
|
||||
display_file_content() {
|
||||
local file_path="$1"
|
||||
local title="$2"
|
||||
local max_lines="$3"
|
||||
|
||||
if [ ! -f "$file_path" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo -e "$title"
|
||||
local line_count
|
||||
line_count=$(wc -l < "$file_path")
|
||||
|
||||
if [ "$line_count" -gt "$max_lines" ]; then
|
||||
head -n "$max_lines" "$file_path"
|
||||
echo -e "\e[33m[... 输出已截断,共 ${line_count} 行 ...]\e[0m"
|
||||
else
|
||||
cat "$file_path"
|
||||
fi
|
||||
echo ""
|
||||
echo "执行步骤:"
|
||||
echo "1. 遍历 'tmp/' 目录下的所有 .s 汇编文件。"
|
||||
echo "2. 在指定的超时时间内使用 VM 内部的 gcc 将 .s 文件汇编并链接为可执行文件。"
|
||||
echo "3. 在指定的超时时间内运行编译后的可执行文件。"
|
||||
echo "4. 根据对应的 .out 文件内容进行返回值和/或标准输出的比较。"
|
||||
echo "5. 输出比较时会忽略行尾多余的换行符。"
|
||||
echo "6. 所有测试结束后,报告总通过率。"
|
||||
}
|
||||
|
||||
# 清理临时文件的函数
|
||||
@ -78,14 +69,23 @@ while [[ "$#" -gt 0 ]]; do
|
||||
clean_tmp
|
||||
exit 0
|
||||
;;
|
||||
-t)
|
||||
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then EXEC_TIMEOUT="$2"; shift; else echo "错误: -t 需要一个正整数参数。" >&2; exit 1; fi
|
||||
-t|--timeout)
|
||||
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
|
||||
TIMEOUT_SECONDS="$2"
|
||||
shift # 移过参数值
|
||||
else
|
||||
echo "错误: --timeout 需要一个正整数参数。" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
-ct)
|
||||
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then GCC_TIMEOUT="$2"; shift; else echo "错误: -ct 需要一个正整数参数。" >&2; exit 1; fi
|
||||
;;
|
||||
-ml|--max-lines)
|
||||
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then MAX_OUTPUT_LINES="$2"; shift; else echo "错误: --max-lines 需要一个正整数参数。" >&2; exit 1; fi
|
||||
-ct|--compile-timeout)
|
||||
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
|
||||
COMPILE_TIMEOUT_SECONDS="$2"
|
||||
shift # 移过参数值
|
||||
else
|
||||
echo "错误: --compile-timeout 需要一个正整数参数。" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
-h|--help)
|
||||
show_help
|
||||
@ -101,27 +101,32 @@ while [[ "$#" -gt 0 ]]; do
|
||||
done
|
||||
|
||||
echo "SysY VM 内部测试运行器启动..."
|
||||
echo "GCC 编译超时设置为: ${GCC_TIMEOUT} 秒"
|
||||
echo "运行时超时设置为: ${EXEC_TIMEOUT} 秒"
|
||||
echo "失败输出最大行数: ${MAX_OUTPUT_LINES}"
|
||||
echo "编译超时设置为: ${COMPILE_TIMEOUT_SECONDS} 秒"
|
||||
echo "运行时超时设置为: ${TIMEOUT_SECONDS} 秒"
|
||||
echo "汇编文件目录: ${TMP_DIR}"
|
||||
echo "库文件目录: ${LIB_DIR}"
|
||||
echo "测试数据目录: ${TESTDATA_DIR}"
|
||||
echo ""
|
||||
|
||||
# 查找 tmp 目录下的所有 .s 汇编文件并排序
|
||||
s_files=$(find "${TMP_DIR}" -maxdepth 1 -name "*.s" | sort -V)
|
||||
# 查找 tmp 目录下的所有 .s 汇编文件
|
||||
s_files=$(find "${TMP_DIR}" -maxdepth 1 -name "*.s")
|
||||
TOTAL_CASES=$(echo "$s_files" | wc -w)
|
||||
|
||||
# 使用 here-string (<<<) 避免子 shell 问题
|
||||
while IFS= read -r s_file; do
|
||||
# 遍历找到的每个 .s 文件
|
||||
echo "$s_files" | while read s_file; do
|
||||
# --- 新增功能: 初始化用例通过状态 ---
|
||||
is_passed=1 # 1 表示通过, 0 表示失败
|
||||
|
||||
# 从 .s 文件名中提取原始的测试用例名称部分
|
||||
base_name_from_s_file=$(basename "$s_file" .s)
|
||||
original_test_name_underscored=$(echo "$base_name_from_s_file" | sed 's/_sysyc_riscv64$//')
|
||||
|
||||
# 将 `original_test_name_underscored` 分割成类别和文件名
|
||||
category=$(echo "$original_test_name_underscored" | cut -d'_' -f1)
|
||||
test_file_base=$(echo "$original_test_name_underscored" | cut -d'_' -f2-)
|
||||
original_relative_path="${category}/${test_file_base}"
|
||||
|
||||
# 定义可执行文件、输入文件、参考输出文件和实际输出文件的路径
|
||||
executable_file="${TMP_DIR}/${base_name_from_s_file}"
|
||||
input_file="${TESTDATA_DIR}/${original_relative_path}.in"
|
||||
output_reference_file="${TESTDATA_DIR}/${original_relative_path}.out"
|
||||
@ -131,43 +136,42 @@ while IFS= read -r s_file; do
|
||||
echo " 对应的测试用例路径: ${original_relative_path}"
|
||||
|
||||
# 步骤 1: 使用 VM 内部的 gcc 编译 .s 到可执行文件
|
||||
echo " 使用 gcc 汇编并链接 (超时 ${GCC_TIMEOUT}s)..."
|
||||
timeout -s KILL ${GCC_TIMEOUT} "${GCC_NATIVE}" "${s_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static -g
|
||||
echo " 使用 gcc 汇编并链接 (超时 ${COMPILE_TIMEOUT_SECONDS}s)..."
|
||||
# --- 修改点: 为 gcc 增加 timeout ---
|
||||
timeout ${COMPILE_TIMEOUT_SECONDS} "${GCC_NATIVE}" "${s_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static -g
|
||||
GCC_STATUS=$?
|
||||
if [ $GCC_STATUS -eq 124 ]; then
|
||||
echo -e "\e[31m错误: GCC 编译/链接 ${s_file} 超时\e[0m"
|
||||
echo -e "\e[31m错误: GCC 编译/链接 ${s_file} 超时 (超过 ${COMPILE_TIMEOUT_SECONDS} 秒)\e[0m"
|
||||
is_passed=0
|
||||
elif [ $GCC_STATUS -ne 0 ]; then
|
||||
echo -e "\e[31m错误: GCC 汇编/链接 ${s_file} 失败,退出码: ${GCC_STATUS}\e[0m"
|
||||
is_passed=0
|
||||
fi
|
||||
|
||||
# 步骤 2: 只有当编译成功时才执行
|
||||
if [ "$is_passed" -eq 1 ]; then
|
||||
else
|
||||
echo " 生成的可执行文件: ${executable_file}"
|
||||
echo " 正在执行 (超时 ${EXEC_TIMEOUT}s)..."
|
||||
echo " 正在执行 (超时 ${TIMEOUT_SECONDS}s): \"${executable_file}\""
|
||||
|
||||
exec_cmd="\"${executable_file}\""
|
||||
if [ -f "${input_file}" ]; then
|
||||
exec_cmd+=" < \"${input_file}\""
|
||||
fi
|
||||
exec_cmd+=" > \"${output_actual_file}\""
|
||||
|
||||
eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}"
|
||||
ACTUAL_RETURN_CODE=$?
|
||||
# 步骤 2: 执行编译后的文件并比较/报告结果
|
||||
if [ -f "${output_reference_file}" ]; then
|
||||
LAST_LINE_TRIMMED=$(tail -n 1 "${output_reference_file}" | tr -d '[:space:]')
|
||||
|
||||
if [[ "$LAST_LINE_TRIMMED" =~ ^[-+]?[0-9]+$ ]]; then
|
||||
EXPECTED_RETURN_CODE="$LAST_LINE_TRIMMED"
|
||||
EXPECTED_STDOUT_FILE="${TMP_DIR}/${base_name_from_s_file}.expected_stdout"
|
||||
head -n -1 "${output_reference_file}" > "${EXPECTED_STDOUT_FILE}"
|
||||
echo " 检测到 .out 文件同时包含标准输出和期望的返回码。"
|
||||
echo " 期望返回码: ${EXPECTED_RETURN_CODE}"
|
||||
|
||||
if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then
|
||||
echo -e "\e[31m 执行超时: ${original_relative_path}.sy 运行超过 ${EXEC_TIMEOUT} 秒\e[0m"
|
||||
is_passed=0
|
||||
else
|
||||
if [ -f "${output_reference_file}" ]; then
|
||||
LAST_LINE_TRIMMED=$(tail -n 1 "${output_reference_file}" | tr -d '[:space:]')
|
||||
|
||||
if [[ "$LAST_LINE_TRIMMED" =~ ^[-+]?[0-9]+$ ]]; then
|
||||
EXPECTED_RETURN_CODE="$LAST_LINE_TRIMMED"
|
||||
EXPECTED_STDOUT_FILE="${TMP_DIR}/${base_name_from_s_file}.expected_stdout"
|
||||
head -n -1 "${output_reference_file}" > "${EXPECTED_STDOUT_FILE}"
|
||||
if [ -f "${input_file}" ]; then
|
||||
timeout ${TIMEOUT_SECONDS} "${executable_file}" < "${input_file}" > "${output_actual_file}"
|
||||
else
|
||||
timeout ${TIMEOUT_SECONDS} "${executable_file}" > "${output_actual_file}"
|
||||
fi
|
||||
ACTUAL_RETURN_CODE=$?
|
||||
|
||||
if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then
|
||||
echo -e "\e[31m 执行超时: ${original_relative_path}.sy 运行超过 ${TIMEOUT_SECONDS} 秒\e[0m"
|
||||
is_passed=0
|
||||
else
|
||||
if [ "$ACTUAL_RETURN_CODE" -eq "$EXPECTED_RETURN_CODE" ]; then
|
||||
echo -e "\e[32m 返回码测试成功: (${ACTUAL_RETURN_CODE}) 与期望值 (${EXPECTED_RETURN_CODE}) 匹配\e[0m"
|
||||
else
|
||||
@ -175,51 +179,65 @@ while IFS= read -r s_file; do
|
||||
is_passed=0
|
||||
fi
|
||||
|
||||
if ! diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then
|
||||
echo -e "\e[31m 标准输出测试失败\e[0m"
|
||||
is_passed=0
|
||||
display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
display_file_content "${output_actual_file}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
echo -e " \e[36m------------------------------\e[0m"
|
||||
fi
|
||||
else
|
||||
if [ $ACTUAL_RETURN_CODE -ne 0 ]; then
|
||||
echo -e "\e[33m警告: 程序以非零状态 ${ACTUAL_RETURN_CODE} 退出 (纯输出比较模式)。\e[0m"
|
||||
fi
|
||||
if ! diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${output_reference_file}") >/dev/null 2>&1; then
|
||||
echo -e "\e[32m 成功: 输出与参考输出匹配\e[0m"
|
||||
if diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then
|
||||
echo -e "\e[32m 标准输出测试成功\e[0m"
|
||||
else
|
||||
echo -e "\e[31m 失败: 输出不匹配\e[0m"
|
||||
echo -e "\e[31m 标准输出测试失败\e[0m"
|
||||
echo " 差异:"
|
||||
diff "${output_actual_file}" "${EXPECTED_STDOUT_FILE}"
|
||||
is_passed=0
|
||||
display_file_content "${output_reference_file}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
display_file_content "${output_actual_file}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
echo -e " \e[36m------------------------------\e[0m"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo " 无参考输出文件。程序返回码: ${ACTUAL_RETURN_CODE}"
|
||||
echo " 检测到 .out 文件为纯标准输出参考。"
|
||||
if [ -f "${input_file}" ]; then
|
||||
timeout ${TIMEOUT_SECONDS} "${executable_file}" < "${input_file}" > "${output_actual_file}"
|
||||
else
|
||||
timeout ${TIMEOUT_SECONDS} "${executable_file}" > "${output_actual_file}"
|
||||
fi
|
||||
EXEC_STATUS=$?
|
||||
|
||||
if [ $EXEC_STATUS -eq 124 ]; then
|
||||
echo -e "\e[31m 执行超时: ${original_relative_path}.sy 运行超过 ${TIMEOUT_SECONDS} 秒\e[0m"
|
||||
is_passed=0
|
||||
else
|
||||
if [ $EXEC_STATUS -ne 0 ]; then
|
||||
echo -e "\e[33m警告: 程序以非零状态 ${EXEC_STATUS} 退出 (纯输出比较模式)。\e[0m"
|
||||
fi
|
||||
if diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${output_reference_file}") >/dev/null 2>&1; then
|
||||
echo -e "\e[32m 成功: 输出与参考输出匹配\e[0m"
|
||||
else
|
||||
echo -e "\e[31m 失败: 输出不匹配\e[0m"
|
||||
echo " 差异:"
|
||||
diff "${output_actual_file}" "${output_reference_file}"
|
||||
is_passed=0
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo " 未找到 .out 文件。正在运行并报告返回码。"
|
||||
timeout ${TIMEOUT_SECONDS} "${executable_file}"
|
||||
EXEC_STATUS=$?
|
||||
if [ $EXEC_STATUS -eq 124 ]; then
|
||||
echo -e "\e[31m 执行超时: ${original_relative_path}.sy 运行超过 ${TIMEOUT_SECONDS} 秒\e[0m"
|
||||
is_passed=0
|
||||
else
|
||||
echo " ${original_relative_path}.sy 的返回码: ${EXEC_STATUS}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# --- 新增功能: 更新通过用例计数 ---
|
||||
if [ "$is_passed" -eq 1 ]; then
|
||||
((PASSED_CASES++))
|
||||
else
|
||||
FAILED_CASES_LIST+="${original_relative_path}.sy\n"
|
||||
fi
|
||||
echo ""
|
||||
done <<< "$s_files"
|
||||
echo "" # 为测试用例之间添加一个空行
|
||||
done
|
||||
|
||||
# --- 新增功能: 打印最终总结 ---
|
||||
echo "========================================"
|
||||
echo "测试完成"
|
||||
echo "测试通过率: [${PASSED_CASES}/${TOTAL_CASES}]"
|
||||
|
||||
if [ -n "$FAILED_CASES_LIST" ]; then
|
||||
echo ""
|
||||
echo -e "\e[31m未通过的测例:\e[0m"
|
||||
echo -e "${FAILED_CASES_LIST}"
|
||||
fi
|
||||
|
||||
echo "========================================"
|
||||
|
||||
if [ "$PASSED_CASES" -eq "$TOTAL_CASES" ]; then
|
||||
|
||||
@ -1,270 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# runit-single.sh - 用于编译和测试单个或少量 SysY 程序的脚本
|
||||
# 模仿 runit.sh 的功能,但以具体文件路径作为输入。
|
||||
|
||||
# --- 配置区 ---
|
||||
# 请根据你的环境修改这些路径
|
||||
# 假设此脚本位于你的项目根目录或一个脚本目录中
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
|
||||
# 默认寻找项目根目录下的 build 和 lib
|
||||
BUILD_BIN_DIR="${SCRIPT_DIR}/../build/bin"
|
||||
LIB_DIR="${SCRIPT_DIR}/../lib"
|
||||
# 临时文件会存储在脚本所在目录的 tmp 子目录中
|
||||
TMP_DIR="${SCRIPT_DIR}/tmp"
|
||||
|
||||
# 定义编译器和模拟器
|
||||
SYSYC="${BUILD_BIN_DIR}/sysyc"
|
||||
GCC_RISCV64="riscv64-linux-gnu-gcc"
|
||||
QEMU_RISCV64="qemu-riscv64"
|
||||
|
||||
# --- 初始化变量 ---
|
||||
EXECUTE_MODE=false
|
||||
SYSYC_TIMEOUT=10 # sysyc 编译超时 (秒)
|
||||
GCC_TIMEOUT=10 # gcc 编译超时 (秒)
|
||||
EXEC_TIMEOUT=5 # qemu 自动化执行超时 (秒)
|
||||
MAX_OUTPUT_LINES=50 # 对比失败时显示的最大行数
|
||||
SY_FILES=() # 存储用户提供的 .sy 文件列表
|
||||
PASSED_CASES=0
|
||||
FAILED_CASES_LIST=""
|
||||
|
||||
# --- 函数定义 ---
|
||||
show_help() {
|
||||
echo "用法: $0 [文件1.sy] [文件2.sy] ... [选项]"
|
||||
echo "编译并测试指定的 .sy 文件。"
|
||||
echo ""
|
||||
echo "如果找到对应的 .in/.out 文件,则进行自动化测试。否则,进入交互模式。"
|
||||
echo ""
|
||||
echo "选项:"
|
||||
echo " -e, --executable 编译为可执行文件并运行测试 (必须)。"
|
||||
echo " -sct N 设置 sysyc 编译超时为 N 秒 (默认: 10)。"
|
||||
echo " -gct N 设置 gcc 交叉编译超时为 N 秒 (默认: 10)。"
|
||||
echo " -et N 设置 qemu 自动化执行超时为 N 秒 (默认: 5)。"
|
||||
echo " -ml N, --max-lines N 当输出对比失败时,最多显示 N 行内容 (默认: 50)。"
|
||||
echo " -h, --help 显示此帮助信息并退出。"
|
||||
}
|
||||
|
||||
# --- 新增功能: 显示文件内容并根据行数截断 ---
|
||||
display_file_content() {
|
||||
local file_path="$1"
|
||||
local title="$2"
|
||||
local max_lines="$3"
|
||||
|
||||
if [ ! -f "$file_path" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo -e "$title"
|
||||
local line_count
|
||||
line_count=$(wc -l < "$file_path")
|
||||
|
||||
if [ "$line_count" -gt "$max_lines" ]; then
|
||||
head -n "$max_lines" "$file_path"
|
||||
echo -e "\e[33m[... 输出已截断,共 ${line_count} 行 ...]\e[0m"
|
||||
else
|
||||
cat "$file_path"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# --- 参数解析 ---
|
||||
# 从参数中分离出 .sy 文件和选项
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
-e|--executable)
|
||||
EXECUTE_MODE=true
|
||||
;;
|
||||
-sct|-gct|-et|-ml|--max-lines)
|
||||
# 选项和其值将在下一个循环中处理
|
||||
;;
|
||||
-h|--help)
|
||||
show_help
|
||||
exit 0
|
||||
;;
|
||||
-*)
|
||||
# 检查是否是带值的选项
|
||||
if ! [[ ${args_processed+x} ]]; then
|
||||
args_processed=true # 标记已处理过参数
|
||||
# 重新处理所有参数
|
||||
while [[ "$#" -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-sct) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then SYSYC_TIMEOUT="$2"; shift; else echo "错误: -sct 需要一个正整数参数。" >&2; exit 1; fi ;;
|
||||
-gct) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then GCC_TIMEOUT="$2"; shift; else echo "错误: -gct 需要一个正整数参数。" >&2; exit 1; fi ;;
|
||||
-et) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then EXEC_TIMEOUT="$2"; shift; else echo "错误: -et 需要一个正整数参数。" >&2; exit 1; fi ;;
|
||||
-ml|--max-lines) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then MAX_OUTPUT_LINES="$2"; shift; else echo "错误: --max-lines 需要一个正整数参数。" >&2; exit 1; fi ;;
|
||||
*.sy) SY_FILES+=("$1") ;;
|
||||
-e|--executable) ;; # 已在外部处理
|
||||
*) if ! [[ "$1" =~ ^[0-9]+$ ]]; then echo "未知选项或无效文件: $1"; show_help; exit 1; fi ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
fi
|
||||
;;
|
||||
*.sy)
|
||||
if [[ -f "$arg" ]]; then
|
||||
SY_FILES+=("$arg")
|
||||
else
|
||||
echo "警告: 文件不存在,已忽略: $arg"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# --- 主逻辑开始 ---
|
||||
if ! ${EXECUTE_MODE}; then
|
||||
echo "错误: 请提供 -e 或 --executable 选项来运行测试。"
|
||||
show_help
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ${#SY_FILES[@]} -eq 0 ]; then
|
||||
echo "错误: 未提供任何 .sy 文件作为输入。"
|
||||
show_help
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "${TMP_DIR}"
|
||||
TOTAL_CASES=${#SY_FILES[@]}
|
||||
|
||||
echo "SysY 单例测试运行器启动..."
|
||||
echo "超时设置: sysyc=${SYSYC_TIMEOUT}s, gcc=${GCC_TIMEOUT}s, qemu=${EXEC_TIMEOUT}s"
|
||||
echo "失败输出最大行数: ${MAX_OUTPUT_LINES}"
|
||||
echo ""
|
||||
|
||||
for sy_file in "${SY_FILES[@]}"; do
|
||||
is_passed=1
|
||||
base_name=$(basename "${sy_file}" .sy)
|
||||
source_dir=$(dirname "${sy_file}")
|
||||
|
||||
ir_file="${TMP_DIR}/${base_name}_sysyc_riscv64.ll"
|
||||
assembly_file="${TMP_DIR}/${base_name}.s"
|
||||
executable_file="${TMP_DIR}/${base_name}"
|
||||
input_file="${source_dir}/${base_name}.in"
|
||||
output_reference_file="${source_dir}/${base_name}.out"
|
||||
output_actual_file="${TMP_DIR}/${base_name}.actual_out"
|
||||
|
||||
echo "======================================================================"
|
||||
echo "正在处理: ${sy_file}"
|
||||
|
||||
# 步骤 1: sysyc 编译
|
||||
echo " 使用 sysyc 编译 (超时 ${SYSYC_TIMEOUT}s)..."
|
||||
timeout -s KILL ${SYSYC_TIMEOUT} "${SYSYC}" -s ir "${sy_file}" > "${ir_file}"
|
||||
SYSYC_STATUS=$?
|
||||
if [ $SYSYC_STATUS -eq 124 ]; then
|
||||
echo -e "\e[31m错误: SysY 编译 ${sy_file} IR超时\e[0m"
|
||||
is_passed=0
|
||||
elif [ $SYSYC_STATUS -ne 0 ]; then
|
||||
echo -e "\e[31m错误: SysY 编译 ${sy_file} IR失败,退出码: ${SYSYC_STATUS}\e[0m"
|
||||
is_passed=0
|
||||
fi
|
||||
timeout -s KILL ${SYSYC_TIMEOUT} "${SYSYC}" -S "${sy_file}" -o "${assembly_file}"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "\e[31m错误: SysY 编译失败或超时。\e[0m"
|
||||
is_passed=0
|
||||
fi
|
||||
|
||||
# 步骤 2: GCC 编译
|
||||
if [ "$is_passed" -eq 1 ]; then
|
||||
echo " 使用 gcc 编译 (超时 ${GCC_TIMEOUT}s)..."
|
||||
timeout -s KILL ${GCC_TIMEOUT} "${GCC_RISCV64}" "${assembly_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "\e[31m错误: GCC 编译失败或超时。\e[0m"
|
||||
is_passed=0
|
||||
fi
|
||||
fi
|
||||
|
||||
# 步骤 3: 执行与测试
|
||||
if [ "$is_passed" -eq 1 ]; then
|
||||
# 检查是自动化测试还是交互模式
|
||||
if [ -f "${input_file}" ] || [ -f "${output_reference_file}" ]; then
|
||||
# --- 自动化测试模式 ---
|
||||
echo " 检测到 .in/.out 文件,进入自动化测试模式..."
|
||||
echo " 正在执行 (超时 ${EXEC_TIMEOUT}s)..."
|
||||
|
||||
exec_cmd="${QEMU_RISCV64} \"${executable_file}\""
|
||||
[ -f "${input_file}" ] && exec_cmd+=" < \"${input_file}\""
|
||||
exec_cmd+=" > \"${output_actual_file}\""
|
||||
|
||||
eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}"
|
||||
ACTUAL_RETURN_CODE=$?
|
||||
|
||||
if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then
|
||||
echo -e "\e[31m 执行超时。\e[0m"
|
||||
is_passed=0
|
||||
else
|
||||
if [ -f "${output_reference_file}" ]; then
|
||||
# 此处逻辑与 runit.sh 相同
|
||||
LAST_LINE_TRIMMED=$(tail -n 1 "${output_reference_file}" | tr -d '[:space:]')
|
||||
if [[ "$LAST_LINE_TRIMMED" =~ ^[-+]?[0-9]+$ ]]; then
|
||||
EXPECTED_RETURN_CODE="$LAST_LINE_TRIMMED"
|
||||
EXPECTED_STDOUT_FILE="${TMP_DIR}/${base_name}.expected_stdout"
|
||||
head -n -1 "${output_reference_file}" > "${EXPECTED_STDOUT_FILE}"
|
||||
if [ "$ACTUAL_RETURN_CODE" -ne "$EXPECTED_RETURN_CODE" ]; then echo -e "\e[31m 返回码测试失败: 期望 ${EXPECTED_RETURN_CODE}, 实际 ${ACTUAL_RETURN_CODE}\e[0m"; is_passed=0; fi
|
||||
if ! diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then
|
||||
echo -e "\e[31m 标准输出测试失败。\e[0m"
|
||||
is_passed=0
|
||||
# --- 本次修改点: 使用新函数显示输出 ---
|
||||
display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
display_file_content "${output_actual_file}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
echo -e " \e[36m----------------\e[0m"
|
||||
fi
|
||||
else
|
||||
if ! diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${output_reference_file}") >/dev/null 2>&1; then
|
||||
echo -e "\e[32m 标准输出测试成功。\e[0m"
|
||||
else
|
||||
echo -e "\e[31m 标准输出测试失败。\e[0m"
|
||||
is_passed=0
|
||||
# --- 本次修改点: 使用新函数显示输出 ---
|
||||
display_file_content "${output_reference_file}" " \e[36m--- 期望输出 ---\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
display_file_content "${output_actual_file}" " \e[36m--- 实际输出 ---\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
echo -e " \e[36m----------------\e[0m"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo " 无参考输出文件。程序返回码: ${ACTUAL_RETURN_CODE}"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
# --- 交互模式 ---
|
||||
echo -e "\e[33m"
|
||||
echo " **********************************************************"
|
||||
echo " ** 未找到 .in 或 .out 文件,进入交互模式。 **"
|
||||
echo " ** 程序即将运行,你可以直接在终端中输入。 **"
|
||||
echo " ** 按下 Ctrl+D (EOF) 或以其他方式结束程序以继续。 **"
|
||||
echo " **********************************************************"
|
||||
echo -e "\e[0m"
|
||||
"${QEMU_RISCV64}" "${executable_file}"
|
||||
INTERACTIVE_RET_CODE=$?
|
||||
echo -e "\e[33m\n 交互模式执行完毕,程序返回码: ${INTERACTIVE_RET_CODE}\e[0m"
|
||||
# 交互模式无法自动判断对错,默认算通过,但会提示
|
||||
echo " 注意: 交互模式的结果未经验证。"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$is_passed" -eq 1 ]; then
|
||||
echo -e "\e[32m状态: 通过\e[0m"
|
||||
((PASSED_CASES++))
|
||||
else
|
||||
echo -e "\e[31m状态: 失败\e[0m"
|
||||
FAILED_CASES_LIST+="${sy_file}\n"
|
||||
fi
|
||||
done
|
||||
|
||||
# --- 打印最终总结 ---
|
||||
echo "======================================================================"
|
||||
echo "所有测试完成"
|
||||
echo "测试通过率: [${PASSED_CASES}/${TOTAL_CASES}]"
|
||||
|
||||
if [ -n "$FAILED_CASES_LIST" ]; then
|
||||
echo ""
|
||||
echo -e "\e[31m未通过的测例:\e[0m"
|
||||
echo -e "${FAILED_CASES_LIST}"
|
||||
fi
|
||||
|
||||
echo "======================================================================"
|
||||
|
||||
if [ "$PASSED_CASES" -eq "$TOTAL_CASES" ]; then
|
||||
exit 0
|
||||
else
|
||||
exit 1
|
||||
fi
|
||||
@ -9,7 +9,7 @@ TESTDATA_DIR="${SCRIPT_DIR}/../testdata"
|
||||
BUILD_BIN_DIR="${SCRIPT_DIR}/../build/bin"
|
||||
LIB_DIR="${SCRIPT_DIR}/../lib"
|
||||
# TMP_DIR="${SCRIPT_DIR}/tmp"
|
||||
TMP_DIR="${SCRIPT_DIR}/tmp"
|
||||
TMP_DIR="/home/ladev987/paraComp/debug/share_folder/tmp"
|
||||
|
||||
# 定义编译器和模拟器
|
||||
SYSYC="${BUILD_BIN_DIR}/sysyc"
|
||||
@ -21,7 +21,6 @@ EXECUTE_MODE=false
|
||||
SYSYC_TIMEOUT=10 # sysyc 编译超时 (秒)
|
||||
GCC_TIMEOUT=10 # gcc 编译超时 (秒)
|
||||
EXEC_TIMEOUT=5 # qemu 执行超时 (秒)
|
||||
MAX_OUTPUT_LINES=50 # 对比失败时显示的最大行数
|
||||
TOTAL_CASES=0
|
||||
PASSED_CASES=0
|
||||
FAILED_CASES_LIST="" # 用于存储未通过的测例列表
|
||||
@ -37,32 +36,9 @@ show_help() {
|
||||
echo " -sct N 设置 sysyc 编译超时为 N 秒 (默认: 10)。"
|
||||
echo " -gct N 设置 gcc 交叉编译超时为 N 秒 (默认: 10)。"
|
||||
echo " -et N 设置 qemu 执行超时为 N 秒 (默认: 5)。"
|
||||
echo " -ml N, --max-lines N 当输出对比失败时,最多显示 N 行内容 (默认: 50)。"
|
||||
echo " -h, --help 显示此帮助信息并退出。"
|
||||
}
|
||||
|
||||
# 显示文件内容并根据行数截断的函数
|
||||
display_file_content() {
|
||||
local file_path="$1"
|
||||
local title="$2"
|
||||
local max_lines="$3"
|
||||
|
||||
if [ ! -f "$file_path" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo -e "$title"
|
||||
local line_count
|
||||
line_count=$(wc -l < "$file_path")
|
||||
|
||||
if [ "$line_count" -gt "$max_lines" ]; then
|
||||
head -n "$max_lines" "$file_path"
|
||||
echo -e "\e[33m[... 输出已截断,共 ${line_count} 行 ...]\e[0m"
|
||||
else
|
||||
cat "$file_path"
|
||||
fi
|
||||
}
|
||||
|
||||
# 清理临时文件的函数
|
||||
clean_tmp() {
|
||||
echo "正在清理临时目录: ${TMP_DIR}"
|
||||
@ -91,9 +67,6 @@ while [[ "$#" -gt 0 ]]; do
|
||||
-et)
|
||||
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then EXEC_TIMEOUT="$2"; shift; else echo "错误: -et 需要一个正整数参数。" >&2; exit 1; fi
|
||||
;;
|
||||
-ml|--max-lines)
|
||||
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then MAX_OUTPUT_LINES="$2"; shift; else echo "错误: --max-lines 需要一个正整数参数。" >&2; exit 1; fi
|
||||
;;
|
||||
-h|--help)
|
||||
show_help
|
||||
exit 0
|
||||
@ -113,7 +86,6 @@ echo "临时目录: ${TMP_DIR}"
|
||||
echo "执行模式: ${EXECUTE_MODE}"
|
||||
if ${EXECUTE_MODE}; then
|
||||
echo "超时设置: sysyc=${SYSYC_TIMEOUT}s, gcc=${GCC_TIMEOUT}s, qemu=${EXEC_TIMEOUT}s"
|
||||
echo "失败输出最大行数: ${MAX_OUTPUT_LINES}"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
@ -121,7 +93,8 @@ echo ""
|
||||
sy_files=$(find "${TESTDATA_DIR}" -name "*.sy" | sort -V)
|
||||
TOTAL_CASES=$(echo "$sy_files" | wc -w)
|
||||
|
||||
# --- 修复: 使用 here-string (<<<) 代替管道 (|) 来避免子 shell 问题 ---
|
||||
# --- 本次修复: 使用 here-string (<<<) 代替管道 (|) 来避免子 shell 问题 ---
|
||||
# 这样可以确保循环内的 PASSED_CASES 变量修改在循环结束后依然有效
|
||||
while IFS= read -r sy_file; do
|
||||
is_passed=1 # 1 表示通过, 0 表示失败
|
||||
|
||||
@ -138,7 +111,7 @@ while IFS= read -r sy_file; do
|
||||
|
||||
# 步骤 1: 使用 sysyc 编译 .sy 到 .s
|
||||
echo " 使用 sysyc 编译 (超时 ${SYSYC_TIMEOUT}s)..."
|
||||
timeout -s KILL ${SYSYC_TIMEOUT} "${SYSYC}" -S "${sy_file}" -o "${assembly_file}"
|
||||
timeout ${SYSYC_TIMEOUT} "${SYSYC}" -S "${sy_file}" -o "${assembly_file}"
|
||||
SYSYC_STATUS=$?
|
||||
if [ $SYSYC_STATUS -eq 124 ]; then
|
||||
echo -e "\e[31m错误: SysY 编译 ${sy_file} 超时\e[0m"
|
||||
@ -152,7 +125,7 @@ while IFS= read -r sy_file; do
|
||||
if ${EXECUTE_MODE} && [ "$is_passed" -eq 1 ]; then
|
||||
# 步骤 2: 使用 riscv64-linux-gnu-gcc 编译 .s 到可执行文件
|
||||
echo " 使用 gcc 编译 (超时 ${GCC_TIMEOUT}s)..."
|
||||
timeout -s KILL ${GCC_TIMEOUT} "${GCC_RISCV64}" "${assembly_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static
|
||||
timeout ${GCC_TIMEOUT} "${GCC_RISCV64}" "${assembly_file}" -o "${executable_file}" -L"${LIB_DIR}" -lsysy_riscv -static
|
||||
GCC_STATUS=$?
|
||||
if [ $GCC_STATUS -eq 124 ]; then
|
||||
echo -e "\e[31m错误: GCC 编译 ${assembly_file} 超时\e[0m"
|
||||
@ -163,9 +136,11 @@ while IFS= read -r sy_file; do
|
||||
fi
|
||||
elif ! ${EXECUTE_MODE}; then
|
||||
echo " 跳过执行模式。仅生成汇编文件。"
|
||||
# 如果只编译不执行,只要编译成功就算通过
|
||||
if [ "$is_passed" -eq 1 ]; then
|
||||
((PASSED_CASES++))
|
||||
else
|
||||
# --- 本次修改点 ---
|
||||
FAILED_CASES_LIST+="${relative_path_no_ext}.sy\n"
|
||||
fi
|
||||
echo ""
|
||||
@ -176,19 +151,22 @@ while IFS= read -r sy_file; do
|
||||
if [ "$is_passed" -eq 1 ]; then
|
||||
echo " 正在执行 (超时 ${EXEC_TIMEOUT}s)..."
|
||||
|
||||
# 准备执行命令
|
||||
exec_cmd="${QEMU_RISCV64} \"${executable_file}\""
|
||||
if [ -f "${input_file}" ]; then
|
||||
exec_cmd+=" < \"${input_file}\""
|
||||
fi
|
||||
exec_cmd+=" > \"${output_actual_file}\""
|
||||
|
||||
eval "timeout -s KILL ${EXEC_TIMEOUT} ${exec_cmd}"
|
||||
# 执行并捕获返回码
|
||||
eval "timeout ${EXEC_TIMEOUT} ${exec_cmd}"
|
||||
ACTUAL_RETURN_CODE=$?
|
||||
|
||||
if [ "$ACTUAL_RETURN_CODE" -eq 124 ]; then
|
||||
echo -e "\e[31m 执行超时: ${sy_file} 运行超过 ${EXEC_TIMEOUT} 秒\e[0m"
|
||||
is_passed=0
|
||||
else
|
||||
# 检查是否存在 .out 文件以进行比较
|
||||
if [ -f "${output_reference_file}" ]; then
|
||||
LAST_LINE_TRIMMED=$(tail -n 1 "${output_reference_file}" | tr -d '[:space:]')
|
||||
|
||||
@ -197,54 +175,70 @@ while IFS= read -r sy_file; do
|
||||
EXPECTED_STDOUT_FILE="${TMP_DIR}/${output_base_name}_sysyc_riscv64.expected_stdout"
|
||||
head -n -1 "${output_reference_file}" > "${EXPECTED_STDOUT_FILE}"
|
||||
|
||||
# 比较返回码
|
||||
if [ "$ACTUAL_RETURN_CODE" -eq "$EXPECTED_RETURN_CODE" ]; then
|
||||
echo -e "\e[32m 返回码测试成功: (${ACTUAL_RETURN_CODE}) 与期望值 (${EXPECTED_RETURN_CODE}) 匹配\e[0m"
|
||||
else
|
||||
echo -e "\e[31m 返回码测试失败: 期望: ${EXPECTED_RETURN_CODE}, 实际: ${ACTUAL_RETURN_CODE}\e[0m"
|
||||
is_passed=0
|
||||
fi
|
||||
if ! diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then
|
||||
# 比较标准输出
|
||||
if diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${EXPECTED_STDOUT_FILE}") >/dev/null 2>&1; then
|
||||
echo -e "\e[32m 标准输出测试成功\e[0m"
|
||||
else
|
||||
echo -e "\e[31m 标准输出测试失败\e[0m"
|
||||
is_passed=0
|
||||
display_file_content "${EXPECTED_STDOUT_FILE}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
display_file_content "${output_actual_file}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
echo -e " \e[36m---------- 期望输出 ----------\e[0m"
|
||||
cat "${EXPECTED_STDOUT_FILE}"
|
||||
echo -e " \e[36m---------- 实际输出 ----------\e[0m"
|
||||
cat "${output_actual_file}"
|
||||
echo -e " \e[36m------------------------------\e[0m"
|
||||
fi
|
||||
else
|
||||
# 纯标准输出比较
|
||||
if [ $ACTUAL_RETURN_CODE -ne 0 ]; then
|
||||
echo -e "\e[33m警告: 程序以非零状态 ${ACTUAL_RETURN_CODE} 退出 (纯输出比较模式)。\e[0m"
|
||||
fi
|
||||
if ! diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${output_reference_file}") >/dev/null 2>&1; then
|
||||
if diff -q <(sed ':a;N;$!ba;s/\n*$//' "${output_actual_file}") <(sed ':a;N;$!ba;s/\n*$//' "${output_reference_file}") >/dev/null 2>&1; then
|
||||
echo -e "\e[32m 成功: 输出与参考输出匹配\e[0m"
|
||||
else
|
||||
echo -e "\e[31m 失败: 输出不匹配\e[0m"
|
||||
is_passed=0
|
||||
display_file_content "${output_reference_file}" " \e[36m---------- 期望输出 ----------\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
display_file_content "${output_actual_file}" " \e[36m---------- 实际输出 ----------\e[0m" "${MAX_OUTPUT_LINES}"
|
||||
echo -e " \e[36m---------- 期望输出 ----------\e[0m"
|
||||
cat "${output_reference_file}"
|
||||
echo -e " \e[36m---------- 实际输出 ----------\e[0m"
|
||||
cat "${output_actual_file}"
|
||||
echo -e " \e[36m------------------------------\e[0m"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
# 没有 .out 文件,只报告返回码
|
||||
echo " 无参考输出文件。程序返回码: ${ACTUAL_RETURN_CODE}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# 更新通过用例计数
|
||||
# --- 本次修改点 ---
|
||||
if [ "$is_passed" -eq 1 ]; then
|
||||
((PASSED_CASES++))
|
||||
else
|
||||
# 将失败的用例名称添加到列表中
|
||||
FAILED_CASES_LIST+="${relative_path_no_ext}.sy\n"
|
||||
fi
|
||||
echo ""
|
||||
echo "" # 添加空行以提高可读性
|
||||
done <<< "$sy_files"
|
||||
|
||||
# --- 新增功能: 打印最终总结 ---
|
||||
echo "========================================"
|
||||
echo "测试完成"
|
||||
echo "测试通过率: [${PASSED_CASES}/${TOTAL_CASES}]"
|
||||
|
||||
# --- 本次修改点: 打印未通过的测例列表 ---
|
||||
if [ -n "$FAILED_CASES_LIST" ]; then
|
||||
echo ""
|
||||
echo -e "\e[31m未通过的测例:\e[0m"
|
||||
# 使用 -e 来解释换行符 \n
|
||||
echo -e "${FAILED_CASES_LIST}"
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user