Compare commits
1 Commits
SCCP
...
deploy-202
| Author | SHA1 | Date | |
|---|---|---|---|
| c2153b6fab |
11
README.md
11
README.md
@ -37,13 +37,4 @@ mysysy/ $ bash setup.sh
|
||||
```
|
||||
|
||||
### 配套脚本
|
||||
(TODO: 需要完善)
|
||||
|
||||
|
||||
### TODO_list:
|
||||
|
||||
除开注释中的TODO后续时间充足可以考虑的TODO:
|
||||
|
||||
- store load指令由于gep指令的引入, 维度信息的记录是非必须的, 考虑删除
|
||||
|
||||
- use def关系经过mem2reg和phi函数明确转换为ssa形式, 以及函数参数通过value数组明确定义, 使得基本块的args参数信息记录非必须, 考虑删除
|
||||
(TODO: 需要完善)
|
||||
@ -21,8 +21,8 @@ add_executable(sysyc
|
||||
IR.cpp
|
||||
SysYIRGenerator.cpp
|
||||
SysYIRPrinter.cpp
|
||||
SysYIRCFGOpt.cpp
|
||||
# SysYIRAnalyser.cpp
|
||||
SysYIROptPre.cpp
|
||||
SysYIRAnalyser.cpp
|
||||
# DeadCodeElimination.cpp
|
||||
AddressCalculationExpansion.cpp
|
||||
# Mem2Reg.cpp
|
||||
@ -31,7 +31,6 @@ add_executable(sysyc
|
||||
RISCv64ISel.cpp
|
||||
RISCv64RegAlloc.cpp
|
||||
RISCv64AsmPrinter.cpp
|
||||
RISCv64Passes.cpp
|
||||
)
|
||||
|
||||
# 设置 include 路径,包含 ANTLR 运行时库和项目头文件
|
||||
|
||||
@ -1,259 +0,0 @@
|
||||
#include "DeadCodeElimination.h"
|
||||
#include <iostream>
|
||||
|
||||
extern int DEBUG;
|
||||
namespace sysy {
|
||||
|
||||
void DeadCodeElimination::runDCEPipeline() {
|
||||
const auto& functions = pModule->getFunctions();
|
||||
for (const auto& function : functions) {
|
||||
const auto& func = function.second;
|
||||
bool changed = true;
|
||||
while (changed) {
|
||||
changed = false;
|
||||
eliminateDeadStores(func.get(), changed);
|
||||
eliminateDeadLoads(func.get(), changed);
|
||||
eliminateDeadAllocas(func.get(), changed);
|
||||
eliminateDeadRedundantLoadStore(func.get(), changed);
|
||||
eliminateDeadGlobals(changed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 消除无用存储 消除条件:
|
||||
// 存储的目标指针(pointer)不是全局变量(!isGlobal(pointer))。
|
||||
// 存储的目标指针不是数组参数(!isArr(pointer) 或不在函数参数列表里)。
|
||||
// 该指针的所有使用者(uses)仅限 alloca 或 store(即没有 load 或其他指令使用它)。
|
||||
void DeadCodeElimination::eliminateDeadStores(Function* func, bool& changed) {
|
||||
for (const auto& block : func->getBasicBlocks()) {
|
||||
auto& instrs = block->getInstructions();
|
||||
for (auto iter = instrs.begin(); iter != instrs.end();) {
|
||||
auto inst = iter->get();
|
||||
if (!inst->isStore()) {
|
||||
++iter;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto storeInst = dynamic_cast<StoreInst*>(inst);
|
||||
auto pointer = storeInst->getPointer();
|
||||
// 如果是全局变量或者是函数的数组参数
|
||||
if (SysYIROptUtils::isGlobal(pointer) || (SysYIROptUtils::isArr(pointer) &&
|
||||
std::find(func->getEntryBlock()->getArguments().begin(),
|
||||
func->getEntryBlock()->getArguments().end(),
|
||||
pointer) != func->getEntryBlock()->getArguments().end())) {
|
||||
++iter;
|
||||
continue;
|
||||
}
|
||||
|
||||
bool changetag = true;
|
||||
for (auto& use : pointer->getUses()) {
|
||||
// 依次判断store的指针是否被其他指令使用
|
||||
auto user = use->getUser();
|
||||
auto userInst = dynamic_cast<Instruction*>(user);
|
||||
// 如果使用store的指针的指令不是Alloca或Store,则不删除
|
||||
if (userInst != nullptr && !userInst->isAlloca() && !userInst->isStore()) {
|
||||
changetag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (changetag) {
|
||||
changed = true;
|
||||
if(DEBUG){
|
||||
std::cout << "=== Dead Store Found ===\n";
|
||||
SysYPrinter::printInst(storeInst);
|
||||
}
|
||||
SysYIROptUtils::usedelete(storeInst);
|
||||
iter = instrs.erase(iter);
|
||||
} else {
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 消除无用加载 消除条件:
|
||||
// 该指令的结果未被使用(inst->getUses().empty())。
|
||||
void DeadCodeElimination::eliminateDeadLoads(Function* func, bool& changed) {
|
||||
for (const auto& block : func->getBasicBlocks()) {
|
||||
auto& instrs = block->getInstructions();
|
||||
for (auto iter = instrs.begin(); iter != instrs.end();) {
|
||||
auto inst = iter->get();
|
||||
if (inst->isBinary() || inst->isUnary() || inst->isLoad()) {
|
||||
if (inst->getUses().empty()) {
|
||||
changed = true;
|
||||
if(DEBUG){
|
||||
std::cout << "=== Dead Load Binary Unary Found ===\n";
|
||||
SysYPrinter::printInst(inst);
|
||||
}
|
||||
SysYIROptUtils::usedelete(inst);
|
||||
iter = instrs.erase(iter);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 消除无用加载 消除条件:
|
||||
// 该 alloca 未被任何指令使用(allocaInst->getUses().empty())。
|
||||
// 该 alloca 不是函数的参数(不在 entry 块的参数列表里)。
|
||||
void DeadCodeElimination::eliminateDeadAllocas(Function* func, bool& changed) {
|
||||
for (const auto& block : func->getBasicBlocks()) {
|
||||
auto& instrs = block->getInstructions();
|
||||
for (auto iter = instrs.begin(); iter != instrs.end();) {
|
||||
auto inst = iter->get();
|
||||
if (inst->isAlloca()) {
|
||||
auto allocaInst = dynamic_cast<AllocaInst*>(inst);
|
||||
if (allocaInst->getUses().empty() &&
|
||||
std::find(func->getEntryBlock()->getArguments().begin(),
|
||||
func->getEntryBlock()->getArguments().end(),
|
||||
allocaInst) == func->getEntryBlock()->getArguments().end()) {
|
||||
changed = true;
|
||||
if(DEBUG){
|
||||
std::cout << "=== Dead Alloca Found ===\n";
|
||||
SysYPrinter::printInst(inst);
|
||||
}
|
||||
SysYIROptUtils::usedelete(inst);
|
||||
iter = instrs.erase(iter);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DeadCodeElimination::eliminateDeadIndirectiveAllocas(Function* func, bool& changed) {
|
||||
// 删除mem2reg时引入的且现在已经没有value使用了的隐式alloca
|
||||
FunctionAnalysisInfo* funcInfo = pCFA->getFunctionAnalysisInfo(func);
|
||||
for (auto it = funcInfo->getIndirectAllocas().begin(); it != funcInfo->getIndirectAllocas().end();) {
|
||||
auto &allocaInst = *it;
|
||||
if (allocaInst->getUses().empty()) {
|
||||
changed = true;
|
||||
if(DEBUG){
|
||||
std::cout << "=== Dead Indirect Alloca Found ===\n";
|
||||
SysYPrinter::printInst(allocaInst.get());
|
||||
}
|
||||
it = funcInfo->getIndirectAllocas().erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 该全局变量未被任何指令使用(global->getUses().empty())。
|
||||
void DeadCodeElimination::eliminateDeadGlobals(bool& changed) {
|
||||
auto& globals = pModule->getGlobals();
|
||||
for (auto it = globals.begin(); it != globals.end();) {
|
||||
auto& global = *it;
|
||||
if (global->getUses().empty()) {
|
||||
changed = true;
|
||||
if(DEBUG){
|
||||
std::cout << "=== Dead Global Found ===\n";
|
||||
SysYPrinter::printValue(global.get());
|
||||
}
|
||||
it = globals.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 消除冗余加载和存储 消除条件:
|
||||
// phi 指令的目标指针仅被该 phi 使用(无其他 store/load 使用)。
|
||||
// memset 指令的目标指针未被使用(pointer->getUses().empty())
|
||||
// store -> load -> store 模式
|
||||
void DeadCodeElimination::eliminateDeadRedundantLoadStore(Function* func, bool& changed) {
|
||||
for (const auto& block : func->getBasicBlocks()) {
|
||||
auto& instrs = block->getInstructions();
|
||||
for (auto iter = instrs.begin(); iter != instrs.end();) {
|
||||
auto inst = iter->get();
|
||||
if (inst->isPhi()) {
|
||||
auto phiInst = dynamic_cast<PhiInst*>(inst);
|
||||
auto pointer = phiInst->getPointer();
|
||||
bool tag = true;
|
||||
for (const auto& use : pointer->getUses()) {
|
||||
auto user = use->getUser();
|
||||
if (user != inst) {
|
||||
tag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/// 如果 pointer 仅被该 phi 使用,可以删除 ph
|
||||
if (tag) {
|
||||
changed = true;
|
||||
SysYIROptUtils::usedelete(inst);
|
||||
iter = instrs.erase(iter);
|
||||
continue;
|
||||
}
|
||||
// 数组指令还不完善,不保证memset优化效果
|
||||
} else if (inst->isMemset()) {
|
||||
auto memsetInst = dynamic_cast<MemsetInst*>(inst);
|
||||
auto pointer = memsetInst->getPointer();
|
||||
if (pointer->getUses().empty()) {
|
||||
changed = true;
|
||||
SysYIROptUtils::usedelete(inst);
|
||||
iter = instrs.erase(iter);
|
||||
continue;
|
||||
}
|
||||
}else if(inst->isLoad()) {
|
||||
if (iter != instrs.begin()) {
|
||||
auto loadInst = dynamic_cast<LoadInst*>(inst);
|
||||
auto loadPointer = loadInst->getPointer();
|
||||
// TODO:store -> load -> store 模式
|
||||
auto prevIter = std::prev(iter);
|
||||
auto prevInst = prevIter->get();
|
||||
if (prevInst->isStore()) {
|
||||
auto prevStore = dynamic_cast<StoreInst*>(prevInst);
|
||||
auto prevStorePointer = prevStore->getPointer();
|
||||
auto prevStoreValue = prevStore->getOperand(0);
|
||||
// 确保前一个 store 不是数组操作
|
||||
if (prevStore->getIndices().empty()) {
|
||||
// 检查后一条指令是否是 store 同一个值
|
||||
auto nextIter = std::next(iter);
|
||||
if (nextIter != instrs.end()) {
|
||||
auto nextInst = nextIter->get();
|
||||
if (nextInst->isStore()) {
|
||||
auto nextStore = dynamic_cast<StoreInst*>(nextInst);
|
||||
auto nextStorePointer = nextStore->getPointer();
|
||||
auto nextStoreValue = nextStore->getOperand(0);
|
||||
// 确保后一个 store 不是数组操作
|
||||
if (nextStore->getIndices().empty()) {
|
||||
// 判断优化条件:
|
||||
// 1. prevStore 的指针操作数 == load 的指针操作数
|
||||
// 2. nextStore 的值操作数 == load 指令本身
|
||||
if (prevStorePointer == loadPointer &&
|
||||
nextStoreValue == loadInst) {
|
||||
// 可以优化直接把prevStorePointer的值存到nextStorePointer
|
||||
changed = true;
|
||||
nextStore->setOperand(0, prevStoreValue);
|
||||
if(DEBUG){
|
||||
std::cout << "=== Dead Store Load Store Found(now only del Load) ===\n";
|
||||
SysYPrinter::printInst(prevStore);
|
||||
SysYPrinter::printInst(loadInst);
|
||||
SysYPrinter::printInst(nextStore);
|
||||
}
|
||||
SysYIROptUtils::usedelete(loadInst);
|
||||
iter = instrs.erase(iter);
|
||||
// 删除 prevStore 这里是不是可以留给删除无用store处理?
|
||||
// if (prevStore->getUses().empty()) {
|
||||
// usedelete(prevStore);
|
||||
// instrs.erase(prevIter); // 删除 prevStore
|
||||
// }
|
||||
continue; // 跳过 ++iter,因为已经移动迭代器
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace sysy
|
||||
53
src/IR.cpp
53
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,11 +95,6 @@ FunctionType*FunctionType::get(Type *returnType, const std::vector<Type *> ¶
|
||||
return result.first->get();
|
||||
}
|
||||
|
||||
ArrayType *ArrayType::get(Type *elementType, unsigned numElements) {
|
||||
// TODO:可以考虑在这里添加缓存,避免重复创建相同的数组类型
|
||||
return new ArrayType(elementType, numElements);
|
||||
}
|
||||
|
||||
void Value::replaceAllUsesWith(Value *value) {
|
||||
for (auto &use : uses) {
|
||||
use->getUser()->setOperand(use->getIndex(), value);
|
||||
@ -479,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);
|
||||
|
||||
515
src/Mem2Reg.cpp
515
src/Mem2Reg.cpp
@ -1,515 +0,0 @@
|
||||
#include "Mem2Reg.h"
|
||||
#include "SysYIRPrinter.h"
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// --- 私有成员函数实现 ---
|
||||
|
||||
// 计算给定定义块集合的迭代支配边界
|
||||
std::unordered_set<BasicBlock*> Mem2Reg::computeIteratedDomFrontiers(const std::unordered_set<BasicBlock*>& blocks) {
|
||||
std::unordered_set<BasicBlock*> result;
|
||||
std::queue<BasicBlock*> worklist; // 使用队列进行 BFS-like 遍历
|
||||
|
||||
for (auto* block : blocks)
|
||||
worklist.push(block);
|
||||
|
||||
while (!worklist.empty()) {
|
||||
auto* block = worklist.front();
|
||||
worklist.pop();
|
||||
|
||||
auto* blockInfo = controlFlowAnalysis->getBlockAnalysisInfo(block);
|
||||
if (!blockInfo) continue;
|
||||
|
||||
for (auto* df : blockInfo->getDomFrontiers()) {
|
||||
if (result.find(df) == result.end()) { // If not already in result
|
||||
result.insert(df);
|
||||
worklist.push(df);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 分析一个 alloca 的所有 uses,填充 allocaDefsBlock 和 allocaUsesBlock
|
||||
void Mem2Reg::allocaAnalysis(AllocaInst* alloca) {
|
||||
allocaDefsBlock[alloca].clear();
|
||||
allocaUsesBlock[alloca].clear();
|
||||
|
||||
for (auto use : alloca->getUses()) {
|
||||
Instruction* userInst = dynamic_cast<Instruction*>(use->getUser());
|
||||
if (!userInst) continue;
|
||||
|
||||
if (StoreInst* store = dynamic_cast<StoreInst*>(userInst)) {
|
||||
if (store->getOperand(1) == alloca) { // Store's second operand is the pointer
|
||||
allocaDefsBlock[alloca].insert(store->getParent()); // Store's parent is the defining block
|
||||
}
|
||||
} else if (LoadInst* load = dynamic_cast<LoadInst*>(userInst)) {
|
||||
if (load->getOperand(0) == alloca) { // Load's first operand is the pointer
|
||||
allocaUsesBlock[alloca].insert(load->getParent()); // Load's parent is the using block
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 判断一个 alloca 是否可以被提升为寄存器 (无地址逃逸,标量类型)
|
||||
bool Mem2Reg::is_promoted(AllocaInst* alloca) {
|
||||
// 检查是否是标量类型 (非数组、非全局变量等)
|
||||
if(!(SysYIROptUtils::isArr(alloca) || SysYIROptUtils::isGlobal(alloca))){
|
||||
return false; // 只有标量类型的 alloca 才能被提升
|
||||
}
|
||||
|
||||
// 获取 alloca 指向的基类型
|
||||
PointerType* ptrType = dynamic_cast<PointerType*>(alloca->getType());
|
||||
if (!ptrType) return false; // Should always be a pointer type
|
||||
Type* allocabaseType = ptrType->getBaseType();
|
||||
|
||||
for (const auto& use : alloca->getUses()) {
|
||||
Instruction* userInst = dynamic_cast<Instruction*>(use->getUser());
|
||||
if (!userInst) {
|
||||
// 如果不是指令的 use,比如作为全局变量的初始值等,通常认为逃逸
|
||||
return false;
|
||||
}
|
||||
|
||||
if (LoadInst* load = dynamic_cast<LoadInst*>(userInst)) {
|
||||
// Load 指令结果的类型必须与 alloca 的基类型一致
|
||||
if (load->getType() != allocabaseType) {
|
||||
return false;
|
||||
}
|
||||
} else if (StoreInst* store = dynamic_cast<StoreInst*>(userInst)) {
|
||||
// Store 指令的值操作数类型必须与 alloca 的基类型一致
|
||||
// 且 store 的指针操作数必须是当前 alloca
|
||||
if (store->getOperand(1) != alloca || store->getOperand(0)->getType() != allocabaseType) {
|
||||
return false;
|
||||
}
|
||||
} else if (userInst->isGetSubArray()) {
|
||||
// GSA 指令表示对数组的访问
|
||||
// 这意味着地址逃逸,不能简单提升为单个寄存器
|
||||
return false;
|
||||
} else if (userInst->isCall()) {
|
||||
// 如果 alloca 作为函数参数传递,通常认为地址逃逸
|
||||
return false;
|
||||
}
|
||||
// 如果有其他类型的指令使用 alloca 的地址,也需要判断是否是逃逸
|
||||
// 例如:BitCastInst, PtrToIntInst, 如果这些操作将地址暴露,则不能提升
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 在迭代支配边界处插入 Phi 指令
|
||||
void Mem2Reg::insertPhiNodes(Function* func) {
|
||||
// 清空上次 Phi 插入的结果
|
||||
phiMap.clear();
|
||||
allPhiInstructions.clear();
|
||||
|
||||
std::unordered_set<BasicBlock*> phiPlacementBlocks; // 存放需要插入 Phi 的块
|
||||
std::queue<BasicBlock*> workQueue; // BFS 队列,用于迭代支配边界计算
|
||||
|
||||
// 遍历所有可提升的 alloca
|
||||
for (AllocaInst* alloca : currentFunctionAllocas) {
|
||||
phiPlacementBlocks.clear(); // 为每个 alloca 重新计算 Phi 放置位置
|
||||
|
||||
// 初始化工作队列,放入所有定义该 alloca 的基本块
|
||||
for (BasicBlock* defBB : allocaDefsBlock[alloca]) {
|
||||
workQueue.push(defBB);
|
||||
}
|
||||
|
||||
while (!workQueue.empty()) {
|
||||
BasicBlock* currentBB = workQueue.front();
|
||||
workQueue.pop();
|
||||
|
||||
auto* blockInfo = controlFlowAnalysis->getBlockAnalysisInfo(currentBB);
|
||||
if (!blockInfo) continue;
|
||||
|
||||
// 遍历当前块的支配边界
|
||||
for (BasicBlock* domFrontierBB : blockInfo->getDomFrontiers()) {
|
||||
// 如果这个支配边界块还没有为当前 alloca 插入 Phi 指令
|
||||
if (phiPlacementBlocks.find(domFrontierBB) == phiPlacementBlocks.end()) {
|
||||
// 获取 alloca 的基类型,作为 Phi 指令的结果类型
|
||||
Type* phiType = dynamic_cast<PointerType*>(alloca->getType())->getBaseType();
|
||||
|
||||
// 在支配边界块的开头插入 Phi 指令
|
||||
pBuilder->setPosition(domFrontierBB->begin());
|
||||
PhiInst* newPhi = pBuilder->createPhiInst(phiType, {}, {}); // 初始入边为空
|
||||
|
||||
allPhiInstructions.push_back(newPhi); // 记录所有 Phi
|
||||
phiPlacementBlocks.insert(domFrontierBB); // 标记已插入
|
||||
|
||||
// 将 Phi 指令映射到它所代表的原始 alloca
|
||||
phiMap[domFrontierBB][newPhi] = alloca;
|
||||
|
||||
// 如果支配边界块本身没有定义该 alloca,则其支配边界也可能需要 Phi
|
||||
// 只有当这个块不是当前alloca的定义块时,才将其加入workQueue,以计算其DF。
|
||||
if (allocaDefsBlock[alloca].find(domFrontierBB) == allocaDefsBlock[alloca].end()) {
|
||||
workQueue.push(domFrontierBB);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取前驱块在后继块前驱列表中的索引
|
||||
int Mem2Reg::getPredIndex(BasicBlock* pred, BasicBlock* succ) {
|
||||
int index = 0;
|
||||
for (auto* elem : succ->getPredecessors()) {
|
||||
if (elem == pred) {
|
||||
return index;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
// 断言通常在你的 IR 框架中应该确保前驱是存在的
|
||||
// assert(false && "Predecessor not found in successor's predecessor list");
|
||||
return -1; // 应该不会发生
|
||||
}
|
||||
|
||||
// 递归地重命名基本块中的变量并填充 Phi 指令
|
||||
void Mem2Reg::renameBlock(BasicBlock* block,
|
||||
std::unordered_map<AllocaInst*, Value*>& currentIncomings,
|
||||
std::unordered_set<BasicBlock*>& visitedBlocks) {
|
||||
|
||||
// 记录在此块中发生的定义,以便在退出时将它们从栈中弹出
|
||||
std::unordered_map<AllocaInst*, int> definitionsInBlockCount;
|
||||
|
||||
// 如果已经访问过这个块,直接返回(防止无限循环或重复处理,在DFS中尤其重要)
|
||||
if (visitedBlocks.count(block)) {
|
||||
return;
|
||||
}
|
||||
visitedBlocks.insert(block);
|
||||
|
||||
// --- 1. 处理当前基本块内的指令 ---
|
||||
// 使用迭代器安全地遍历和删除指令
|
||||
for (auto it = block->getInstructions().begin(); it != block->getInstructions().end(); ) {
|
||||
Instruction* currentInst = it->get();
|
||||
|
||||
if (AllocaInst* alloca = dynamic_cast<AllocaInst*>(currentInst)) {
|
||||
// 如果是可提升的 alloca,标记为删除
|
||||
if (std::find(currentFunctionAllocas.begin(), currentFunctionAllocas.end(), alloca) != currentFunctionAllocas.end()) {
|
||||
SysYIROptUtils::usedelete(currentInst); // 标记为删除(或直接删除取决于你的 IR 管理)
|
||||
it = block->getInstructions().erase(it); // 从列表中移除
|
||||
continue; // 继续下一个指令
|
||||
}
|
||||
} else if (LoadInst* load = dynamic_cast<LoadInst*>(currentInst)) {
|
||||
AllocaInst* originalAlloca = dynamic_cast<AllocaInst*>(load->getOperand(0)); // load 的第一个操作数是指针
|
||||
if (originalAlloca && std::find(currentFunctionAllocas.begin(), currentFunctionAllocas.end(), originalAlloca) != currentFunctionAllocas.end()) {
|
||||
// 如果是可提升 alloca 的 load 指令
|
||||
Value* incomingVal = nullptr;
|
||||
if (currentIncomings.count(originalAlloca)) {
|
||||
incomingVal = currentIncomings[originalAlloca];
|
||||
} else {
|
||||
// 如果在当前路径上没有找到定义,则使用 UndefinedValue
|
||||
incomingVal = UndefinedValue::get(originalAlloca->getType()->isPointer() ?
|
||||
dynamic_cast<PointerType*>(originalAlloca->getType())->getBaseType() :
|
||||
originalAlloca->getType());
|
||||
}
|
||||
|
||||
load->replaceAllUsesWith(incomingVal); // 用最新值替换所有 load 的用途
|
||||
SysYIROptUtils::usedelete(currentInst);
|
||||
it = block->getInstructions().erase(it);
|
||||
continue;
|
||||
}
|
||||
} else if (StoreInst* store = dynamic_cast<StoreInst*>(currentInst)) {
|
||||
AllocaInst* originalAlloca = dynamic_cast<AllocaInst*>(store->getOperand(1)); // store 的第二个操作数是指针
|
||||
if (originalAlloca && std::find(currentFunctionAllocas.begin(), currentFunctionAllocas.end(), originalAlloca) != currentFunctionAllocas.end()) {
|
||||
// 如果是可提升 alloca 的 store 指令,更新当前值
|
||||
currentIncomings[originalAlloca] = store->getOperand(0); // store 的第一个操作数是值
|
||||
definitionsInBlockCount[originalAlloca]++; // 记录在该块中进行的定义数量
|
||||
SysYIROptUtils::usedelete(currentInst);
|
||||
it = block->getInstructions().erase(it);
|
||||
continue;
|
||||
}
|
||||
} else if (PhiInst* phi = dynamic_cast<PhiInst*>(currentInst)) {
|
||||
// 如果是 Mem2Reg 插入的 Phi 指令 (通过 phiMap 判断)
|
||||
if (phiMap[block].count(phi)) {
|
||||
AllocaInst* originalAlloca = phiMap[block][phi];
|
||||
currentIncomings[originalAlloca] = phi; // Phi 指令本身成为该变量的新定义
|
||||
definitionsInBlockCount[originalAlloca]++; // 记录该 Phi 的定义
|
||||
}
|
||||
}
|
||||
++it; // 移动到下一个指令
|
||||
}
|
||||
|
||||
// --- 2. 填充后继基本块中 Phi 指令的入边 ---
|
||||
for (BasicBlock* successorBB : block->getSuccessors()) {
|
||||
int predIndex = getPredIndex(block, successorBB);
|
||||
if (predIndex == -1) continue;
|
||||
|
||||
// Phi 指令总是在基本块的开头
|
||||
for (auto& inst_ptr : successorBB->getInstructions()) {
|
||||
if (PhiInst* phi = dynamic_cast<PhiInst*>(inst_ptr.get())) {
|
||||
if (phiMap[successorBB].count(phi)) { // 确保这是我们关心的 Phi 指令
|
||||
AllocaInst* originalAlloca = phiMap[successorBB][phi];
|
||||
Value* incomingValue = nullptr;
|
||||
|
||||
if (currentIncomings.count(originalAlloca)) {
|
||||
incomingValue = currentIncomings[originalAlloca];
|
||||
} else {
|
||||
// 如果在当前块没有找到对应的定义,使用 UndefinedValue
|
||||
incomingValue = UndefinedValue::get(originalAlloca->getType()->isPointer() ?
|
||||
dynamic_cast<PointerType*>(originalAlloca->getType())->getBaseType() :
|
||||
originalAlloca->getType());
|
||||
}
|
||||
|
||||
if (incomingValue) {
|
||||
phi->addIncoming(incomingValue, block); // 添加 (值, 前驱块) 对
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 遇到非 Phi 指令,说明已经处理完所有 Phi,可以跳出
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- 3. 递归调用支配树的子节点 ---
|
||||
auto* blockInfo = controlFlowAnalysis->getBlockAnalysisInfo(block);
|
||||
if (blockInfo) {
|
||||
for (BasicBlock* dominatedChildBB : blockInfo->getSdoms()) { // getSdoms 获取直接支配的子节点
|
||||
// 递归调用,传递当前 Incomings 的副本(或通过值传递以实现回溯)
|
||||
// 注意:这里是传递 `currentIncomings` 的拷贝,以便递归返回后可以恢复。
|
||||
// 但如果 `currentIncomings` 是引用传递,则这里需要回溯逻辑。
|
||||
// 鉴于它是值传递,此处的 `definitionsInBlockCount` 仅用于统计,无需实际操作 `currentIncomings`。
|
||||
renameBlock(dominatedChildBB, currentIncomings, visitedBlocks);
|
||||
}
|
||||
}
|
||||
|
||||
// --- 4. 回溯:从栈中弹出在此块中创建的所有定义 ---
|
||||
for (auto const& [alloca, count] : definitionsInBlockCount) {
|
||||
// 在我们的实现中,`currentIncomings` 是通过值传递的,每次递归都收到一个新的拷贝。
|
||||
// 因此,不需要显式地 "pop" 栈。`currentIncomings` 在函数返回时会自动销毁。
|
||||
// 这种方式模拟了 "SSA 栈" 的行为,每个函数调用帧有自己的局部定义环境。
|
||||
}
|
||||
}
|
||||
|
||||
// 简化冗余的 Phi 指令 (当所有输入都相同时)
|
||||
void Mem2Reg::simplifyphi(PhiInst* phi) {
|
||||
BasicBlock* phifromblock = phi->getParent();
|
||||
if (!phifromblock) return; // 指令可能已经被删除
|
||||
|
||||
Value* commonValue = nullptr;
|
||||
bool allSame = true;
|
||||
|
||||
// Phi 指令的操作数是 Value, BasicBlock 交替出现,所以是 getOperandSize() / 2 个入边
|
||||
if (phi->getNumOperands() == 0) { // 空 Phi,通常是无效的,直接删除
|
||||
phi->replaceAllUsesWith(UndefinedValue::get(phi->getType())); // 用 UndefinedValue 替换所有用途
|
||||
// phi->getParent()->delete_inst(phi);
|
||||
// 删除 Phi 指令后直接返回
|
||||
// phi指令在开头一个比较快
|
||||
// TODO:后续可优化查找
|
||||
auto tofind = std::find_if(phifromblock->getInstructions().begin(), phifromblock->getInstructions().end(),
|
||||
[phi](const auto &instr) { return instr.get() == phi; });
|
||||
SysYIROptUtils::usedelete(phi); // 使用 SysYIROptUtils 删除指令
|
||||
phifromblock->getInstructions().erase(tofind);
|
||||
// 从基本块中删除 Phi 指令
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < phi->getNumIncomingValues(); ++i) {
|
||||
Value* incomingVal = phi->getOperand(2 * i); // 值位于偶数索引
|
||||
|
||||
if (incomingVal == phi) { // 如果 Phi 指令引用自身 (循环变量)
|
||||
// 这种情况下,Phi 暂时不能简化,除非所有入边都是它自己,这通常通过其他优化处理
|
||||
// 为避免复杂性,我们在此处不处理自引用 Phi 的简化,除非它是唯一选择。
|
||||
// 更好的做法是,如果所有入边都指向自身,则该Phi是冗余的,可以替换为undef或其第一个实际值
|
||||
// 但这需要更复杂的分析来确定循环的初始值。目前简单返回。
|
||||
// TODO:留到后续循环优化处理
|
||||
return;
|
||||
}
|
||||
|
||||
if (commonValue == nullptr) {
|
||||
commonValue = incomingVal;
|
||||
} else if (commonValue != incomingVal) {
|
||||
allSame = false;
|
||||
break; // 发现不同的入边值
|
||||
}
|
||||
}
|
||||
|
||||
if (allSame && commonValue != nullptr) {
|
||||
// 所有入边值都相同,用这个值替换 Phi 指令的所有用途
|
||||
phi->replaceAllUsesWith(commonValue);
|
||||
// 从基本块中删除 Phi 指令
|
||||
auto tofind = std::find_if(phifromblock->getInstructions().begin(), phifromblock->getInstructions().end(),
|
||||
[phi](const auto &instr) { return instr.get() == phi; });
|
||||
SysYIROptUtils::usedelete(phi); // 使用 SysYIROptUtils 删除指令
|
||||
phifromblock->getInstructions().erase(tofind);
|
||||
}
|
||||
}
|
||||
|
||||
// 对单个函数执行内存到寄存器的提升
|
||||
bool Mem2Reg::promoteMemoryToRegisters(Function* func) {
|
||||
bool changed = false;
|
||||
|
||||
// 每次开始对一个函数进行 Mem2Reg 时,清空所有上下文信息
|
||||
currentFunctionAllocas.clear();
|
||||
allocaDefsBlock.clear();
|
||||
allocaUsesBlock.clear();
|
||||
phiMap.clear();
|
||||
allPhiInstructions.clear();
|
||||
|
||||
// 1. 收集所有可提升的 AllocaInst,并进行初步分析
|
||||
BasicBlock* entryBB = func->getEntryBlock();
|
||||
if (!entryBB) return false;
|
||||
|
||||
// 逆序遍历入口块的指令,安全地识别 Alloca
|
||||
for (auto it = entryBB->getInstructions().rbegin(); it != entryBB->getInstructions().rend(); ++it) {
|
||||
if (AllocaInst* alloca = dynamic_cast<AllocaInst*>(it->get())) {
|
||||
if (is_promoted(alloca)) {
|
||||
currentFunctionAllocas.push_back(alloca);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 收集后反转,使其按原始顺序排列 (如果需要的话,但对后续分析影响不大)
|
||||
std::reverse(currentFunctionAllocas.begin(), currentFunctionAllocas.end());
|
||||
|
||||
// 对收集到的所有 alloca 进行 DefsBlock 和 UsesBlock 分析
|
||||
for (AllocaInst* alloca : currentFunctionAllocas) {
|
||||
allocaAnalysis(alloca);
|
||||
}
|
||||
|
||||
// 2. 预处理:删除无用的 AllocaInst (没有 Load 和 Store)
|
||||
// 迭代 currentFunctionAllocas,安全删除
|
||||
for (unsigned int i = 0; i < currentFunctionAllocas.size(); ) {
|
||||
AllocaInst* alloca = currentFunctionAllocas[i];
|
||||
|
||||
bool hasRelevantUse = false;
|
||||
// 检查 alloca 的 uses 列表,看是否有 Load 或 Store
|
||||
// 只要有 Load/Store,就认为是"相关用途",不删除
|
||||
for (auto use_ptr : alloca->getUses()) {
|
||||
Instruction* user_inst = dynamic_cast<Instruction*>(use_ptr->getUser());
|
||||
if (user_inst && (dynamic_cast<LoadInst*>(user_inst) || dynamic_cast<StoreInst*>(user_inst))) {
|
||||
hasRelevantUse = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有相关用途(没有 Load 和 Store),则 alloca 是死代码
|
||||
if (!hasRelevantUse && allocaDefsBlock[alloca].empty() && allocaUsesBlock[alloca].empty()) {
|
||||
if (alloca->getParent()) {
|
||||
// alloca->getParent()->delete_inst(alloca); // 从其所在块删除 alloca 指令
|
||||
auto tofind = std::find_if(alloca->getParent()->getInstructions().begin(), alloca->getParent()->getInstructions().end(),
|
||||
[alloca](const auto &instr) { return instr.get() == alloca; });
|
||||
SysYIROptUtils::usedelete(alloca);
|
||||
alloca->getParent()->getInstructions().erase(tofind);
|
||||
}
|
||||
currentFunctionAllocas.erase(currentFunctionAllocas.begin() + i); // 从列表中移除
|
||||
changed = true; // 发生了改变
|
||||
} else {
|
||||
i++; // 否则,移动到下一个 alloca
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有可提升的 alloca 了,直接返回
|
||||
if (currentFunctionAllocas.empty()) {
|
||||
return changed;
|
||||
}
|
||||
|
||||
// 3. 插入 Phi 指令
|
||||
insertPhiNodes(func);
|
||||
if (!allPhiInstructions.empty()) changed = true;
|
||||
|
||||
// 4. 重命名变量,转换为 SSA 形式并填充 Phi 指令
|
||||
std::unordered_map<AllocaInst*, Value*> initialIncomings;
|
||||
std::unordered_set<BasicBlock*> visitedBlocks; // 用于 DFS 遍历,防止循环
|
||||
|
||||
// 初始化 entry block 的 Incomings 状态
|
||||
for (AllocaInst* alloca : currentFunctionAllocas) {
|
||||
initialIncomings[alloca] = UndefinedValue::get(dynamic_cast<PointerType*>(alloca->getType())->getBaseType());
|
||||
}
|
||||
|
||||
// 从入口块开始递归重命名
|
||||
renameBlock(entryBB, initialIncomings, visitedBlocks);
|
||||
|
||||
// 5. 简化 Phi 指令
|
||||
// 由于 renameBlock 可能会删除 Phi,这里复制一份列表以安全迭代
|
||||
std::vector<PhiInst*> phisToSimplify = allPhiInstructions;
|
||||
for (PhiInst* phi : phisToSimplify) {
|
||||
// 检查 phi 是否还在 IR 中 (可能已被其他优化删除)
|
||||
// 一个简单检查是看它是否有父块
|
||||
if (phi->getParent()) {
|
||||
simplifyphi(phi);
|
||||
// simplifyphi 内部会删除 Phi,所以这里不需要再处理 allPhiInstructions
|
||||
// 最终的 allPhiInstructions 清理将在 promoteMemoryToRegisters 结束后进行
|
||||
}
|
||||
}
|
||||
|
||||
// 清理所有 Phi 的列表和映射
|
||||
// 遍历 allPhiInstructions,删除那些在 simplifyphi 后可能仍然存在的、但已经没有 uses 的 Phi
|
||||
std::vector<PhiInst*> remainingPhis;
|
||||
for(PhiInst* phi : allPhiInstructions) {
|
||||
if(phi->getParent() && phi->getUses().empty()){ // 如果还在IR中但没有用处
|
||||
|
||||
// phi->getParent()->delete_inst(phi);
|
||||
// 找到phi节点对应的迭代器
|
||||
auto tofind = std::find_if(phi->getParent()->getInstructions().begin(), phi->getParent()->getInstructions().end(),
|
||||
[phi](const auto &instr) { return instr.get() == phi; });
|
||||
SysYIROptUtils::usedelete(phi); // 使用 SysYIROptUtils 删除指令
|
||||
phi->getParent()->getInstructions().erase(tofind);
|
||||
|
||||
changed = true;
|
||||
} else if (phi->getParent()) { // 仍在IR中且有uses
|
||||
remainingPhis.push_back(phi);
|
||||
}
|
||||
}
|
||||
allPhiInstructions = remainingPhis; // 更新为仅包含未被删除的 Phi
|
||||
|
||||
// 重新清理 phiMap 中已经删除的 Phi 指令项
|
||||
for (auto& pairBBPhiMap : phiMap) {
|
||||
std::vector<PhiInst*> phisToRemoveFromMap;
|
||||
for (auto& pairPhiAlloca : pairBBPhiMap.second) {
|
||||
if (!pairPhiAlloca.first->getParent()) { // 如果 Phi 已经被删除
|
||||
phisToRemoveFromMap.push_back(pairPhiAlloca.first);
|
||||
}
|
||||
}
|
||||
for (PhiInst* phi : phisToRemoveFromMap) {
|
||||
pairBBPhiMap.second.erase(phi);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// --- run函数实现 ---
|
||||
void Mem2Reg::run() {
|
||||
// 每次运行整个 Mem2Reg Pass 时,重新进行分析
|
||||
controlFlowAnalysis->clear();
|
||||
controlFlowAnalysis->runControlFlowAnalysis();
|
||||
activeVarAnalysis->clear();
|
||||
// 假设 dataFlowAnalysisUtils 可以管理和运行各个分析器
|
||||
dataFlowAnalysisUtils.addBackwardAnalyzer(activeVarAnalysis);
|
||||
dataFlowAnalysisUtils.backwardAnalyze(pModule); // 运行活跃变量分析
|
||||
|
||||
bool globalChanged = false;
|
||||
// 循环直到没有更多的 alloca 可以被提升
|
||||
// 每次 promoteMemoryToRegisters 会尝试在一个函数内完成所有 Mem2Reg 优化
|
||||
do {
|
||||
globalChanged = false;
|
||||
for (const auto& [_, func] : pModule->getFunctions()) {
|
||||
// 对每个函数执行 Mem2Reg
|
||||
if (promoteMemoryToRegisters(func.get())) {
|
||||
globalChanged = true;
|
||||
// 如果一个函数发生改变,可能影响其他函数或需要重新分析
|
||||
// 因此需要重新运行控制流和活跃变量分析,以备下一次循环
|
||||
controlFlowAnalysis->clear();
|
||||
controlFlowAnalysis->runControlFlowAnalysis();
|
||||
activeVarAnalysis->clear();
|
||||
dataFlowAnalysisUtils.backwardAnalyze(pModule); // 重新分析活跃变量
|
||||
}
|
||||
}
|
||||
} while (globalChanged); // 如果全局有任何函数发生改变,则继续迭代
|
||||
|
||||
// 最终清理和重新分析
|
||||
controlFlowAnalysis->clear();
|
||||
controlFlowAnalysis->runControlFlowAnalysis();
|
||||
activeVarAnalysis->clear();
|
||||
dataFlowAnalysisUtils.backwardAnalyze(pModule);
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
@ -2,7 +2,6 @@
|
||||
#include "RISCv64ISel.h"
|
||||
#include "RISCv64RegAlloc.h"
|
||||
#include "RISCv64AsmPrinter.h"
|
||||
#include "RISCv64Passes.h" // 包含优化Pass的头文件
|
||||
#include <sstream>
|
||||
|
||||
namespace sysy {
|
||||
@ -12,7 +11,7 @@ std::string RISCv64CodeGen::code_gen() {
|
||||
return module_gen();
|
||||
}
|
||||
|
||||
// 模块级代码生成
|
||||
// 模块级代码生成 (移植自原文件,处理.data段和驱动函数生成)
|
||||
std::string RISCv64CodeGen::module_gen() {
|
||||
std::stringstream ss;
|
||||
|
||||
@ -53,31 +52,17 @@ 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);
|
||||
|
||||
// 阶段 2: 指令调度 (Instruction Scheduling)
|
||||
PreRA_Scheduler scheduler;
|
||||
scheduler.runOnMachineFunction(mfunc.get());
|
||||
|
||||
// 阶段 3: 物理寄存器分配 (Register Allocation)
|
||||
// 阶段 2: 寄存器分配 (包含栈帧布局, 活跃性分析, 图着色, spill/rewrite)
|
||||
RISCv64RegAlloc reg_alloc(mfunc.get());
|
||||
reg_alloc.run();
|
||||
|
||||
// 阶段 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);
|
||||
|
||||
@ -1,54 +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;
|
||||
}
|
||||
// 此处为未来优化Pass的实现
|
||||
|
||||
} // namespace sysy
|
||||
122
src/Reg2Mem.cpp
122
src/Reg2Mem.cpp
@ -1,122 +0,0 @@
|
||||
#include "Reg2Mem.h"
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
/**
|
||||
* 删除phi节点
|
||||
* 删除phi节点后可能会生成冗余存储代码
|
||||
*/
|
||||
void Reg2Mem::DeletePhiInst(){
|
||||
auto &functions = pModule->getFunctions();
|
||||
for (auto &function : functions) {
|
||||
auto basicBlocks = function.second->getBasicBlocks();
|
||||
for (auto &basicBlock : basicBlocks) {
|
||||
|
||||
for (auto iter = basicBlock->begin(); iter != basicBlock->end();) {
|
||||
auto &instruction = *iter;
|
||||
if (instruction->isPhi()) {
|
||||
auto predBlocks = basicBlock->getPredecessors();
|
||||
// 寻找源和目的
|
||||
// 目的就是phi指令的第一个操作数
|
||||
// 源就是phi指令的后续操作数
|
||||
auto destination = instruction->getOperand(0);
|
||||
int predBlockindex = 0;
|
||||
for (auto &predBlock : predBlocks) {
|
||||
++predBlockindex;
|
||||
// 判断前驱块儿只有一个后继还是多个后继
|
||||
// 如果有多个
|
||||
auto source = instruction->getOperand(predBlockindex);
|
||||
if (source == destination) {
|
||||
continue;
|
||||
}
|
||||
// std::cout << predBlock->getNumSuccessors() << std::endl;
|
||||
if (predBlock->getNumSuccessors() > 1) {
|
||||
// 创建一个basicblock
|
||||
auto newbasicBlock = function.second->addBasicBlock();
|
||||
std::stringstream ss;
|
||||
ss << "phidel.L" << pBuilder->getLabelIndex();
|
||||
newbasicBlock->setName(ss.str());
|
||||
ss.str("");
|
||||
// // 修改前驱后继关系
|
||||
basicBlock->replacePredecessor(predBlock, newbasicBlock);
|
||||
// predBlock = newbasicBlock;
|
||||
newbasicBlock->addPredecessor(predBlock);
|
||||
newbasicBlock->addSuccessor(basicBlock.get());
|
||||
predBlock->removeSuccessor(basicBlock.get());
|
||||
predBlock->addSuccessor(newbasicBlock);
|
||||
// std::cout << "the block name is " << basicBlock->getName() << std::endl;
|
||||
// for (auto pb : basicBlock->getPredecessors()) {
|
||||
// // newbasicBlock->addPredecessor(pb);
|
||||
// std::cout << pb->getName() << std::endl;
|
||||
// }
|
||||
// sysy::BasicBlock::conectBlocks(newbasicBlock, static_cast<BasicBlock *>(basicBlock.get()));
|
||||
// 若后为跳转指令,应该修改跳转指令所到达的位置
|
||||
auto thelastinst = predBlock->end();
|
||||
(--thelastinst);
|
||||
|
||||
if (thelastinst->get()->isConditional() || thelastinst->get()->isUnconditional()) { // 如果是跳转指令
|
||||
auto opnum = thelastinst->get()->getNumOperands();
|
||||
for (size_t i = 0; i < opnum; i++) {
|
||||
if (thelastinst->get()->getOperand(i) == basicBlock.get()) {
|
||||
thelastinst->get()->replaceOperand(i, newbasicBlock);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 在新块中插入store指令
|
||||
pBuilder->setPosition(newbasicBlock, newbasicBlock->end());
|
||||
// pBuilder->createStoreInst(source, destination);
|
||||
if (source->isInt() || source->isFloat()) {
|
||||
pBuilder->createStoreInst(source, destination);
|
||||
} else {
|
||||
auto loadInst = pBuilder->createLoadInst(source);
|
||||
pBuilder->createStoreInst(loadInst, destination);
|
||||
}
|
||||
// pBuilder->createMoveInst(Instruction::kMove, destination->getType(), destination, source,
|
||||
// newbasicBlock);
|
||||
pBuilder->setPosition(newbasicBlock, newbasicBlock->end());
|
||||
pBuilder->createUncondBrInst(basicBlock.get(), {});
|
||||
} else {
|
||||
// 如果前驱块只有一个后继
|
||||
auto thelastinst = predBlock->end();
|
||||
(--thelastinst);
|
||||
// std::cout << predBlock->getName() << std::endl;
|
||||
// std::cout << thelastinst->get() << std::endl;
|
||||
// std::cout << "First point 11 " << std::endl;
|
||||
if (thelastinst->get()->isConditional() || thelastinst->get()->isUnconditional()) {
|
||||
// 在跳转语句前insert st指令
|
||||
pBuilder->setPosition(predBlock, thelastinst);
|
||||
} else {
|
||||
pBuilder->setPosition(predBlock, predBlock->end());
|
||||
}
|
||||
|
||||
if (source->isInt() || source->isFloat()) {
|
||||
pBuilder->createStoreInst(source, destination);
|
||||
} else {
|
||||
auto loadInst = pBuilder->createLoadInst(source);
|
||||
pBuilder->createStoreInst(loadInst, destination);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 删除phi指令
|
||||
auto &instructions = basicBlock->getInstructions();
|
||||
SysYIROptUtils::usedelete(iter->get());
|
||||
iter = instructions.erase(iter);
|
||||
if (basicBlock->getNumInstructions() == 0) {
|
||||
if (basicBlock->getNumSuccessors() == 1) {
|
||||
pBuilder->setPosition(basicBlock.get(), basicBlock->end());
|
||||
pBuilder->createUncondBrInst(basicBlock->getSuccessors()[0], {});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
@ -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,565 +0,0 @@
|
||||
#include "SysYIRCFGOpt.h"
|
||||
#include "SysYIROptUtils.h"
|
||||
#include <cassert>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "IR.h"
|
||||
#include "IRBuilder.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
|
||||
// 删除br后的无用指令
|
||||
bool SysYCFGOpt::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 (Branch)
|
||||
SysYIROptUtils::usedelete(iter->get());
|
||||
else if ((*iter)->isTerminator()){
|
||||
Branch = true;
|
||||
Branchiter = iter;
|
||||
}
|
||||
}
|
||||
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 SysYCFGOpt::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());
|
||||
block->getInstructions().erase(thelastinstinst);
|
||||
} else if (thelastinstinst->get()->isConditional()) {
|
||||
// 如果是条件分支,判断条件是否相同,主要优化相同布尔表达式
|
||||
if (thelastinstinst->get()->getOperand(1)->getName() == thelastinstinst->get()->getOperand(1)->getName()) {
|
||||
SysYIROptUtils::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);
|
||||
changed = true;
|
||||
|
||||
} else {
|
||||
blockiter++;
|
||||
}
|
||||
} else {
|
||||
blockiter++;
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// 删除无前驱块,兼容SSA后的处理
|
||||
bool SysYCFGOpt::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 &iterInst : blockIter->get()->getInstructions())
|
||||
SysYIROptUtils::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());
|
||||
changed = true;
|
||||
} else {
|
||||
blockIter++;
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// 删除空块
|
||||
bool SysYCFGOpt::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)
|
||||
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));
|
||||
SysYIROptUtils::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));
|
||||
SysYIROptUtils::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 = func->getBasicBlocks().begin(); iter != func->getBasicBlocks().end();) {
|
||||
|
||||
if (EmptyBlocks.find(iter->get()) != EmptyBlocks.end()) {
|
||||
// EntryBlock跳过
|
||||
if (iter->get() == func->getEntryBlock()) {
|
||||
++iter;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (auto &iterInst : iter->get()->getInstructions())
|
||||
SysYIROptUtils::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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func->removeBasicBlock((iter++)->get());
|
||||
changed = true;
|
||||
} else {
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
|
||||
}
|
||||
|
||||
// 如果函数没有返回指令,则添加一个默认返回指令(主要解决void函数没有返回指令的问题)
|
||||
bool SysYCFGOpt::SysYAddReturn(Function *func, IRBuilder* pBuilder) {
|
||||
bool changed = false;
|
||||
auto basicBlocks = func->getBasicBlocks();
|
||||
for (auto &block : basicBlocks) {
|
||||
if (block->getNumSuccessors() == 0) {
|
||||
changed = true;
|
||||
// 如果基本块没有后继块,则添加一个返回指令
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
// 条件分支转换为无条件分支
|
||||
// 主要针对已知条件值的分支转换为无条件分支
|
||||
// 例如 if (cond) { ... } else { ... } 中的 cond 已经
|
||||
// 确定为 true 或 false 的情况
|
||||
bool SysYCFGOpt::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)) {
|
||||
|
||||
pBuilder->setPosition(basicblock.get(), basicblock->end());
|
||||
pBuilder->createUncondBrInst(thenBlock, {});
|
||||
int phiindex = 0;
|
||||
for (auto pred : elseBlock->getPredecessors()) {
|
||||
phiindex++;
|
||||
if (pred == basicblock.get()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &phiinst : elseBlock->getInstructions()) {
|
||||
if (phiinst->getKind() != Instruction::kPhi) {
|
||||
break;
|
||||
}
|
||||
phiinst->removeOperand(phiindex);
|
||||
}
|
||||
basicblock->removeSuccessor(elseBlock);
|
||||
elseBlock->removePredecessor(basicblock.get());
|
||||
} else {
|
||||
|
||||
pBuilder->setPosition(basicblock.get(), basicblock->end());
|
||||
pBuilder->createUncondBrInst(elseBlock, {});
|
||||
int phiindex = 0;
|
||||
for (auto pred : thenBlock->getPredecessors()) {
|
||||
phiindex++;
|
||||
if (pred == basicblock.get()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &phiinst : thenBlock->getInstructions()) {
|
||||
if (phiinst->getKind() != Instruction::kPhi) {
|
||||
break;
|
||||
}
|
||||
phiinst->removeOperand(phiindex);
|
||||
}
|
||||
basicblock->removeSuccessor(thenBlock);
|
||||
thenBlock->removePredecessor(basicblock.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
@ -15,73 +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;
|
||||
}
|
||||
|
||||
Value* SysYIRGenerator::getGEPAddressInst(Value* basePointer, const std::vector<Value*>& indices) {
|
||||
// 检查 basePointer 是否为指针类型
|
||||
if (!basePointer->getType()->isPointer()) {
|
||||
assert(false && "GEP base pointer must be a pointer type!");
|
||||
}
|
||||
|
||||
// 获取基指针所指向的实际类型 (例如 int* 指向 int, int[2][3]* 指向 int[2][3])
|
||||
Type* currentElementType = basePointer->getType()->as<PointerType>()->getBaseType();
|
||||
|
||||
std::vector<Value*> actualGEPIndices;
|
||||
// GEP 指令的第一个索引通常是0,用于“跳过”基指针指向的聚合类型本身,直接指向其第一个元素。
|
||||
// 例如,对于 AllocaInst 返回的 `int[2][3]*`,第一个 `0` 索引表示从数组的开始而不是指针本身开始索引。
|
||||
actualGEPIndices.push_back(ConstantInteger::get(0));
|
||||
|
||||
// 将用户提供的索引添加到 GEP 操作数中
|
||||
for (Value* index : indices) {
|
||||
actualGEPIndices.push_back(index);
|
||||
}
|
||||
|
||||
// 根据索引链计算最终的元素类型
|
||||
Type* finalTargetType = currentElementType;
|
||||
|
||||
// 遍历用户提供的索引(不包括我们添加的第一个0),逐步确定 GEP 的最终结果类型
|
||||
// 每个索引都“深入”一个维度
|
||||
for (size_t i = 0; i < indices.size(); ++i) { // 这里遍历的是用户提供的索引
|
||||
if (finalTargetType && finalTargetType->isArray()) {
|
||||
finalTargetType = finalTargetType->as<ArrayType>()->getElementType();
|
||||
} else {
|
||||
// 如果索引链还在继续,但当前类型已经不是数组或聚合类型,这通常是一个错误
|
||||
// 或者表示访问的是标量,后续索引无效。此时,finalTargetType 已经是最终的标量类型,不能再深入。
|
||||
// 例如,对 int arr[5]; 访问 arr[i][j] (j 是多余的),这里会停止类型推断。
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// GEP 的结果总是指针类型,指向最终计算出的元素
|
||||
Type* gepResultType = Type::getPointerType(finalTargetType);
|
||||
|
||||
// 创建 GEP 指令。假设 builder.createGetElementPtrInst 的签名为
|
||||
// (Type* resultType, Value* basePointer, const std::vector<Value*>& indices)
|
||||
return builder.createGetElementPtrInst(basePointer, actualGEPIndices);
|
||||
}
|
||||
/*
|
||||
* @brief: visit compUnit
|
||||
* @details:
|
||||
@ -185,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(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();
|
||||
|
||||
// 计算数组的**总元素数量**和**总字节大小**
|
||||
int numElements = 1;
|
||||
// 存储每个维度的实际整数大小,用于索引计算
|
||||
std::vector<int> dimSizes;
|
||||
for (Value *dimVal : dims) {
|
||||
if (ConstantInteger *constInt = dynamic_cast<ConstantInteger *>(dimVal)) {
|
||||
@ -216,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 {
|
||||
@ -233,6 +163,7 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
|
||||
}
|
||||
}
|
||||
else{
|
||||
// 如果值不是常量,我们通常不能确定它是否为零,所以不进行 memset 优化
|
||||
allValuesAreZero = false;
|
||||
break;
|
||||
}
|
||||
@ -240,51 +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 {
|
||||
// **逐元素存储:遍历所有初始值,并为每个值生成一个 store 指令**
|
||||
for (size_t k = 0; k < counterValues.size(); ++k) {
|
||||
// 用于存储当前元素的索引列表
|
||||
std::vector<Value *> currentIndices;
|
||||
int tempLinearIndex = k;
|
||||
int tempLinearIndex = k; // 临时线性索引,用于计算多维索引
|
||||
|
||||
// 将线性索引转换为多维索引
|
||||
// **将线性索引转换为多维索引**
|
||||
// 这个循环从最内层维度开始倒推,计算每个维度的索引
|
||||
// 假设是行主序(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];
|
||||
}
|
||||
|
||||
// 计算元素的地址
|
||||
Value* elementAddress = getGEPAddressInst(alloca, currentIndices);
|
||||
// 生成 store 指令 (假设 createStoreInst 接受 Value* value, Value* pointer)
|
||||
builder.createStoreInst(counterValues[k], elementAddress);
|
||||
// **生成 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);
|
||||
@ -340,6 +284,7 @@ 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 *> paramTypes;
|
||||
@ -375,34 +320,22 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
||||
module->addVariable(paramNames[i], alloca);
|
||||
}
|
||||
|
||||
// 在处理函数体之前,创建一个新的基本块作为函数体的实际入口
|
||||
// 这样 entryBB 就可以在完成初始化后跳转到这里
|
||||
BasicBlock* funcBodyEntry = function->addBasicBlock("funcBodyEntry");
|
||||
|
||||
// 从 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();
|
||||
@ -423,56 +356,29 @@ std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx) {
|
||||
for (const auto &exp : lVal->exp()) {
|
||||
dims.push_back(std::any_cast<Value *>(visitExp(exp)));
|
||||
}
|
||||
|
||||
auto variable = module->getVariable(name); // 获取 AllocaInst 或 GlobalValue
|
||||
Value* value = std::any_cast<Value *>(visitExp(ctx->exp())); // 右值
|
||||
|
||||
if (variable == nullptr) {
|
||||
throw std::runtime_error("Variable " + name + " not found in assignment.");
|
||||
}
|
||||
auto variable = module->getVariable(name);
|
||||
Value* value = std::any_cast<Value *>(visitExp(ctx->exp()));
|
||||
Type* variableType = dynamic_cast<PointerType *>(variable->getType())->getBaseType();
|
||||
|
||||
// 计算最终赋值目标元素的类型
|
||||
// variable 本身应该是一个指针类型 (例如 int* 或 int[2][3]*)
|
||||
if (!variable->getType()->isPointer()) {
|
||||
assert(false && "Variable to be assigned must be a pointer type!");
|
||||
return std::any();
|
||||
}
|
||||
Type* targetElementType = variable->getType()->as<PointerType>()->getBaseType(); // 从基指针指向的类型开始
|
||||
|
||||
// 模拟 GEP 路径,根据 dims 确定最终元素的类型
|
||||
for (size_t i = 0; i < dims.size(); ++i) {
|
||||
if (targetElementType && targetElementType->isArray()) {
|
||||
targetElementType = targetElementType->as<ArrayType>()->getElementType();
|
||||
} else {
|
||||
break; // 如果不是数组类型但还有索引,或者索引超出维度,则停止推断
|
||||
}
|
||||
}
|
||||
|
||||
// 左值右值类型不同处理:根据最终元素类型进行转换
|
||||
if (targetElementType != value->getType()) {
|
||||
// 左值右值类型不同处理
|
||||
if (variableType != value->getType()) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(value);
|
||||
if (constValue != nullptr) {
|
||||
if (targetElementType == Type::getFloatType()) {
|
||||
value = ConstantFloating::get(static_cast<float>(constValue->getInt()));
|
||||
} else { // 假设如果不是浮点型,就是整型
|
||||
value = ConstantInteger::get(static_cast<int>(constValue->getFloat()));
|
||||
if (variableType == Type::getFloatType()) {
|
||||
value = ConstantInteger::get(static_cast<float>(constValue->getInt()));
|
||||
} else {
|
||||
value = ConstantFloating::get(static_cast<int>(constValue->getFloat()));
|
||||
}
|
||||
} else {
|
||||
if (targetElementType == Type::getFloatType()) {
|
||||
if (variableType == Type::getFloatType()) {
|
||||
value = builder.createIToFInst(value);
|
||||
} else { // 假设如果不是浮点型,就是整型
|
||||
} else {
|
||||
value = builder.createFtoIInst(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 计算目标地址:如果 dims 为空,就是变量本身地址;否则通过 GEP 计算
|
||||
Value* targetAddress = variable;
|
||||
if (!dims.empty()) {
|
||||
targetAddress = getGEPAddressInst(variable, dims);
|
||||
}
|
||||
|
||||
builder.createStoreInst(value, targetAddress);
|
||||
builder.createStoreInst(value, variable, dims, variable->getName());
|
||||
|
||||
return std::any();
|
||||
}
|
||||
@ -560,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();
|
||||
@ -582,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());
|
||||
|
||||
@ -667,94 +571,56 @@ std::any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx) {
|
||||
}
|
||||
}
|
||||
builder.createReturnInst(returnValue);
|
||||
|
||||
HasReturnInst = true;
|
||||
return std::any();
|
||||
}
|
||||
|
||||
|
||||
// SysYIRGenerator.cpp (修改部分)
|
||||
|
||||
std::any SysYIRGenerator::visitLValue(SysYParser::LValueContext *ctx) {
|
||||
std::string name = ctx->Ident()->getText();
|
||||
User* variable = module->getVariable(name);
|
||||
|
||||
Value* value = nullptr;
|
||||
if (variable == nullptr) {
|
||||
throw std::runtime_error("Variable " + name + " not found.");
|
||||
}
|
||||
std::vector<Value *> dims;
|
||||
for (const auto &exp : ctx->exp()) {
|
||||
dims.push_back(std::any_cast<Value *>(visitExp(exp)));
|
||||
}
|
||||
|
||||
// 1. 获取变量的声明维度数量
|
||||
unsigned declaredNumDims = 0;
|
||||
if (AllocaInst* alloc = dynamic_cast<AllocaInst*>(variable)) {
|
||||
declaredNumDims = alloc->getNumDims();
|
||||
} else if (GlobalValue* glob = dynamic_cast<GlobalValue*>(variable)) {
|
||||
declaredNumDims = glob->getNumDims();
|
||||
} else if (ConstantVariable* constV = dynamic_cast<ConstantVariable*>(variable)) {
|
||||
declaredNumDims = constV->getNumDims();
|
||||
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* targetAddress = nullptr;
|
||||
|
||||
// GEP 的基指针就是变量本身(它是一个指向内存的指针)
|
||||
if (dynamic_cast<AllocaInst*>(variable) || dynamic_cast<GlobalValue*>(variable) || (constVar != nullptr)) {
|
||||
// 允许对 ConstantVariable (如果它代表全局数组常量) 进行 GEP
|
||||
targetAddress = getGEPAddressInst(variable, dims);
|
||||
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 {
|
||||
// 其他情况(例如尝试对非指针类型或不支持的 LValue 进行 GEP)应报错
|
||||
assert(false && "LValue variable type not supported for GEP or dynamic load.");
|
||||
return static_cast<Value*>(nullptr);
|
||||
}
|
||||
|
||||
// 现在 targetAddress 持有元素或子数组的地址。
|
||||
// 需要判断是加载值,还是返回子数组的地址。
|
||||
|
||||
// 如果提供的索引数量少于声明的维度数量,则表示访问的是子数组,返回其地址
|
||||
if (dims.size() < declaredNumDims) {
|
||||
value = targetAddress;
|
||||
} else {
|
||||
// 否则,表示访问的是最终的标量元素,加载其值
|
||||
// 假设 createLoadInst 接受 Value* pointer
|
||||
value = builder.createLoadInst(targetAddress);
|
||||
value = builder.createLoadInst(variable, dims);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
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,23 +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获取值
|
||||
return constVar->getName();
|
||||
}
|
||||
assert(false && "Unknown value type or unable to get value name");
|
||||
assert(false && "Unknown value type");
|
||||
return "";
|
||||
}
|
||||
|
||||
@ -91,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;
|
||||
@ -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
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
#include "IR.h"
|
||||
#include "SysYIRAnalyser.h"
|
||||
#include "SysYIRPrinter.h"
|
||||
#include "SysYIROptUtils.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
@ -32,5 +31,9 @@ class DeadCodeElimination {
|
||||
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
|
||||
|
||||
222
src/include/IR.h
222
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; // 当前维度的大小
|
||||
};
|
||||
|
||||
/*!
|
||||
* @}
|
||||
*/
|
||||
@ -621,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.
|
||||
*/
|
||||
@ -670,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:
|
||||
@ -769,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";
|
||||
}
|
||||
@ -827,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 {
|
||||
@ -840,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;
|
||||
@ -1087,7 +1134,7 @@ public:
|
||||
}; // 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:
|
||||
@ -1098,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(); }
|
||||
@ -1106,40 +1161,37 @@ public:
|
||||
}; // class AllocaInst
|
||||
|
||||
|
||||
class GetElementPtrInst : public Instruction {
|
||||
friend class IRBuilder; // 如果您有IRBuilder来创建指令,需要friend
|
||||
class GetSubArrayInst : public Instruction {
|
||||
friend class IRBuilder;
|
||||
friend class Function;
|
||||
|
||||
protected:
|
||||
// GEP的构造函数:
|
||||
// resultType: GEP计算出的地址的类型 (通常是指向目标元素类型的指针)
|
||||
// basePointer: 基指针 (第一个操作数)
|
||||
// indices: 索引列表 (后续操作数)
|
||||
GetElementPtrInst(Value *basePointer,
|
||||
const std::vector<Value *> &indices = {},
|
||||
BasicBlock *parent = nullptr, const std::string &name = "")
|
||||
: Instruction(Kind::kGetElementPtr, basePointer->getType(), parent, name) {
|
||||
assert(basePointer && "GEP base pointer cannot be null!");
|
||||
// TODO : 安全检查
|
||||
assert(basePointer->getType()->isPointer() );
|
||||
addOperand(basePointer); // 第一个操作数是基指针
|
||||
addOperands(indices); // 随后的操作数是索引
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 静态工厂方法,用于创建GEP指令 (如果需要外部直接创建而非通过IRBuilder)
|
||||
static GetElementPtrInst* create(Type *resultType, Value *basePointer,
|
||||
const std::vector<Value *> &indices = {},
|
||||
BasicBlock *parent = nullptr, const std::string &name = "") {
|
||||
return new GetElementPtrInst(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
|
||||
@ -1163,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
|
||||
@ -1189,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
|
||||
|
||||
@ -1290,7 +1373,7 @@ protected:
|
||||
};
|
||||
|
||||
//! Global value declared at file scope
|
||||
class GlobalValue : public User {
|
||||
class GlobalValue : public User, public LVal {
|
||||
friend class Module;
|
||||
|
||||
protected:
|
||||
@ -1324,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(); } ///< 获取维度列表
|
||||
@ -1345,7 +1438,7 @@ public:
|
||||
}; // class GlobalValue
|
||||
|
||||
|
||||
class ConstantVariable : public User {
|
||||
class ConstantVariable : public User, public LVal {
|
||||
friend class Module;
|
||||
|
||||
protected:
|
||||
@ -1364,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;
|
||||
|
||||
@ -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);
|
||||
@ -300,24 +340,6 @@ class IRBuilder {
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
|
||||
@ -1,79 +1,59 @@
|
||||
// 假设 Mem2Reg.h 看起来像这样 (你需要根据实际情况调整)
|
||||
#ifndef SYSY_MEM2REG_H
|
||||
#define SYSY_MEM2REG_H
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <stack>
|
||||
#include <queue> // For computeIteratedDomFrontiers
|
||||
|
||||
// Include your IR and analysis headers
|
||||
#include "IR.h"
|
||||
#include "IRBuilder.h"
|
||||
#include "SysYIRAnalyser.h"
|
||||
#include "SysYIROptUtils.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
/**
|
||||
* 实现静态单变量赋值核心类 mem2reg
|
||||
*/
|
||||
class Mem2Reg {
|
||||
private:
|
||||
Module* pModule;
|
||||
IRBuilder* pBuilder;
|
||||
ControlFlowAnalysis* controlFlowAnalysis;
|
||||
ActiveVarAnalysis* activeVarAnalysis;
|
||||
DataFlowAnalysisUtils dataFlowAnalysisUtils; // If this is part of Mem2Reg or an external helper
|
||||
Module *pModule;
|
||||
IRBuilder *pBuilder;
|
||||
ControlFlowAnalysis *controlFlowAnalysis; // 控制流分析
|
||||
ActiveVarAnalysis *activeVarAnalysis; // 活跃变量分析
|
||||
DataFlowAnalysisUtils dataFlowAnalysisUtils;
|
||||
|
||||
public:
|
||||
Mem2Reg(Module* module, IRBuilder* builder, ControlFlowAnalysis* cfa, ActiveVarAnalysis* ava)
|
||||
: pModule(module), pBuilder(builder), controlFlowAnalysis(cfa), activeVarAnalysis(ava) {}
|
||||
// Constructor initializes members
|
||||
void run();
|
||||
Mem2Reg(Module *pMoudle, IRBuilder *pBuilder,
|
||||
ControlFlowAnalysis *pCFA = nullptr, ActiveVarAnalysis *pAVA = nullptr) :
|
||||
pModule(pMoudle), pBuilder(pBuilder), controlFlowAnalysis(pCFA), activeVarAnalysis(pAVA), dataFlowAnalysisUtils()
|
||||
{} // 初始化函数
|
||||
|
||||
// --- 新增的私有成员变量和方法,用于SSA转换上下文 ---
|
||||
// 这是核心,用于存储 SSA 转换过程中的状态
|
||||
std::vector<AllocaInst*> currentFunctionAllocas; // 当前函数中所有可提升的 alloca
|
||||
// alloca -> set of BasicBlocks where it's defined (stored into)
|
||||
std::unordered_map<AllocaInst*, std::unordered_set<BasicBlock*>> allocaDefsBlock;
|
||||
// alloca -> set of BasicBlocks where it's used (loaded from)
|
||||
std::unordered_map<AllocaInst*, std::unordered_set<BasicBlock*>> allocaUsesBlock;
|
||||
void mem2regPipeline(); ///< mem2reg
|
||||
|
||||
// BasicBlock -> Map of (PhiInst, Original AllocaInst)
|
||||
// 用于在 rename 阶段通过 phi 指令找到它代表的原始 alloca
|
||||
std::unordered_map<BasicBlock*, std::unordered_map<PhiInst*, AllocaInst*>> phiMap;
|
||||
std::vector<PhiInst*> allPhiInstructions; // 收集所有创建的 Phi 指令以便后续简化和清理
|
||||
private:
|
||||
|
||||
// --- 核心 SSA 转换辅助函数 ---
|
||||
// 计算给定定义块集合的迭代支配边界
|
||||
std::unordered_set<BasicBlock*> computeIteratedDomFrontiers(const std::unordered_set<BasicBlock*>& blocks);
|
||||
// phi节点的插入需要计算IDF
|
||||
std::unordered_set<BasicBlock *> computeIterDf(const std::unordered_set<BasicBlock *> &blocks); ///< 计算定义块集合的迭代支配边界
|
||||
|
||||
// 分析一个 alloca 的所有 uses,填充 allocaDefsBlock 和 allocaUsesBlock
|
||||
void allocaAnalysis(AllocaInst* alloca);
|
||||
auto computeValue2Blocks() -> void; ///< 计算value2block的映射(不包括数组和global)
|
||||
|
||||
// 判断一个 alloca 是否可以被提升为寄存器 (无地址逃逸,标量类型)
|
||||
bool is_promoted(AllocaInst* alloca);
|
||||
auto preOptimize1() -> void; ///< llvm memtoreg预优化1: 删除不含load的alloc和store
|
||||
auto preOptimize2() -> void; ///< llvm memtoreg预优化2: 针对某个变量的Defblocks只有一个块的情况
|
||||
auto preOptimize3() -> void; ///< llvm memtoreg预优化3: 针对某个变量的所有读写都在同一个块中的情况
|
||||
|
||||
// 在迭代支配边界处插入 Phi 指令
|
||||
void insertPhiNodes(Function* func);
|
||||
auto insertPhi() -> void; ///< 为所有变量的迭代支配边界插入phi结点
|
||||
|
||||
// 递归地重命名基本块中的变量并填充 Phi 指令
|
||||
// 这里的 `count` 和 `stacks` 是临时的,用于 DFS 过程中传递状态
|
||||
void renameBlock(BasicBlock* block,
|
||||
std::unordered_map<AllocaInst*, Value*>& currentIncomings,
|
||||
std::unordered_set<BasicBlock*>& visitedBlocks); // 修改为传递 map 和 set
|
||||
auto rename(BasicBlock *block, std::unordered_map<Value *, int> &count,
|
||||
std::unordered_map<Value *, std::stack<Instruction *>> &stacks) -> void; ///< 单个块的重命名
|
||||
auto renameAll() -> void; ///< 重命名所有块
|
||||
|
||||
// 简化冗余的 Phi 指令 (当所有输入都相同时)
|
||||
void simplifyphi(PhiInst* phi);
|
||||
|
||||
// 获取前驱块在后继块前驱列表中的索引,用于 Phi 指令入边
|
||||
int getPredIndex(BasicBlock* pred, BasicBlock* succ);
|
||||
|
||||
// --- Mem2Reg 的主要工作流函数 ---
|
||||
// 对单个函数执行内存到寄存器的提升
|
||||
bool promoteMemoryToRegisters(Function* func);
|
||||
// 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
|
||||
|
||||
#endif // SYSY_MEM2REG_H
|
||||
} // namespace sysy
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
// RISCv64Passes.h
|
||||
#ifndef RISCV64_PASSES_H
|
||||
#define RISCV64_PASSES_H
|
||||
|
||||
@ -5,56 +6,12 @@
|
||||
|
||||
namespace sysy {
|
||||
|
||||
/**
|
||||
* @class Pass
|
||||
* @brief 所有优化Pass的抽象基类 (可选,但推荐)
|
||||
* * 定义一个通用的接口,所有优化都应该实现它。
|
||||
*/
|
||||
class Pass {
|
||||
public:
|
||||
virtual ~Pass() = default;
|
||||
virtual void runOnMachineFunction(MachineFunction* mfunc) = 0;
|
||||
};
|
||||
|
||||
|
||||
// --- 寄存器分配前优化 ---
|
||||
|
||||
/**
|
||||
* @class PreRA_Scheduler
|
||||
* @brief 寄存器分配前的指令调度器
|
||||
* * 在虚拟寄存器上进行操作,此时调度自由度最大,
|
||||
* 主要目标是隐藏指令延迟,提高流水线效率。
|
||||
*/
|
||||
class PreRA_Scheduler : public Pass {
|
||||
public:
|
||||
void runOnMachineFunction(MachineFunction* mfunc) override;
|
||||
};
|
||||
|
||||
|
||||
// --- 寄存器分配后优化 ---
|
||||
|
||||
/**
|
||||
* @class PeepholeOptimizer
|
||||
* @brief 窥孔优化器
|
||||
* * 在已分配物理寄存器的指令流上,通过一个小的滑动窗口来查找
|
||||
* 并替换掉一些冗余或低效的指令模式。
|
||||
*/
|
||||
class PeepholeOptimizer : public Pass {
|
||||
public:
|
||||
void runOnMachineFunction(MachineFunction* mfunc) override;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class PostRA_Scheduler
|
||||
* @brief 寄存器分配后的局部指令调度器
|
||||
* * 主要目标是优化寄存器分配器插入的spill/fill代码(lw/sw),
|
||||
* 尝试将加载指令提前,以隐藏其访存延迟。
|
||||
*/
|
||||
class PostRA_Scheduler : public Pass {
|
||||
public:
|
||||
void runOnMachineFunction(MachineFunction* mfunc) override;
|
||||
};
|
||||
|
||||
// 此处为未来优化Pass的基类或独立类定义
|
||||
// 例如:
|
||||
// class PeepholeOptimizer {
|
||||
// public:
|
||||
// void runOnMachineFunction(MachineFunction* mfunc);
|
||||
// };
|
||||
|
||||
} // namespace sysy
|
||||
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
|
||||
#include "IR.h"
|
||||
#include "IRBuilder.h"
|
||||
#include "SysYIROptUtils.h"
|
||||
|
||||
namespace sysy {
|
||||
/**
|
||||
@ -17,6 +16,8 @@ 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,60 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "IR.h"
|
||||
#include "IRBuilder.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 优化前对SysY IR的预处理,也可以视作部分CFG优化
|
||||
// 主要包括删除无用指令、合并基本块、删除空块等
|
||||
// 这些操作可以在SysY IR生成时就完成,但为了简化IR生成过程,
|
||||
// 这里将其放在SysY IR生成后进行预处理
|
||||
// 同时兼容phi节点的处理,可以再mem2reg后再次调用优化
|
||||
|
||||
//TODO: 可增加的CFG优化和方法
|
||||
// - 检查基本块跳转关系正确性
|
||||
// - 简化条件分支(Branch Simplification),如条件恒真/恒假转为直接跳转
|
||||
// - 合并连续的跳转指令(Jump Threading)在合并不可达块中似乎已经实现了
|
||||
// - 基本块重排序(Block Reordering),提升局部性
|
||||
|
||||
class SysYCFGOpt {
|
||||
private:
|
||||
Module *pModule;
|
||||
IRBuilder *pBuilder;
|
||||
|
||||
public:
|
||||
SysYCFGOpt(Module *pMoudle, IRBuilder *pBuilder) : pModule(pMoudle), pBuilder(pBuilder) {}
|
||||
|
||||
void SysYOptimizateAfterIR(){
|
||||
|
||||
auto &functions = pModule->getFunctions();
|
||||
for (auto &function : functions) {
|
||||
bool changed = false;
|
||||
while(changed){
|
||||
changed = false;
|
||||
changed |= SysYCondBr2Br(function.second.get(), pBuilder);
|
||||
// 删除br后面的无用指令
|
||||
changed |= SysYDelInstAfterBr(function.second.get());
|
||||
// 合并空基本块
|
||||
changed |= SysYBlockMerge(function.second.get());
|
||||
// 删除无前驱块
|
||||
changed |= SysYDelNoPreBLock(function.second.get());
|
||||
// 删除空块
|
||||
changed |= SysYDelEmptyBlock(function.second.get(), pBuilder);
|
||||
// 添加return指令
|
||||
changed |= SysYAddReturn(function.second.get(), pBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
static bool SysYDelInstAfterBr(Function *func); // 删除br后面的指令
|
||||
static bool SysYDelEmptyBlock(Function *func, IRBuilder* pBuilder); // 空块删除
|
||||
static bool SysYDelNoPreBLock(Function *func); // 删除无前驱块(不可达块)
|
||||
static bool SysYBlockMerge(Function *func); // 合并基本块(主要针对嵌套if while的exit块,
|
||||
// 也可以修改IR生成实现回填机制
|
||||
static bool SysYAddReturn(Function *func, IRBuilder* pBuilder); // 添加return指令(主要针对Void函数)
|
||||
static bool SysYCondBr2Br(Function *func, IRBuilder* pBuilder); // 条件分支(已知cond的值)转换为无条件分支
|
||||
};
|
||||
|
||||
} // 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,11 +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);
|
||||
|
||||
}; // 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,59 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "IR.h"
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 前置声明
|
||||
class FunctionPass;
|
||||
class ModulePass;
|
||||
class AnalysisPass;
|
||||
class PassManager;
|
||||
|
||||
// 抽象基类 Pass
|
||||
class Pass {
|
||||
public:
|
||||
enum PassKind {
|
||||
PK_Function,
|
||||
PK_Module,
|
||||
PK_Analysis
|
||||
};
|
||||
|
||||
Pass(PassKind kind, const std::string& name) : Kind(kind), Name(name) {}
|
||||
virtual ~Pass() = default;
|
||||
|
||||
PassKind getPassKind() const { return Kind; }
|
||||
const std::string& getPassName() const { return Name; }
|
||||
|
||||
// 每个Pass需要实现此方法来执行其逻辑
|
||||
// 具体的run方法将根据Pass类型在FunctionPass和ModulePass中定义
|
||||
protected:
|
||||
PassKind Kind;
|
||||
std::string Name;
|
||||
};
|
||||
|
||||
// 针对函数的优化遍
|
||||
class FunctionPass : public Pass {
|
||||
public:
|
||||
FunctionPass(const std::string& name) : Pass(PK_Function, name) {}
|
||||
// 真正的优化逻辑将在此方法中实现
|
||||
virtual bool runOnFunction(Function& F) = 0;
|
||||
};
|
||||
|
||||
// 针对模块的优化遍
|
||||
class ModulePass : public Pass {
|
||||
public:
|
||||
ModulePass(const std::string& name) : Pass(PK_Module, name) {}
|
||||
// 真正的优化逻辑将在此方法中实现
|
||||
virtual bool runOnModule(Module& M) = 0;
|
||||
};
|
||||
|
||||
// 分析遍
|
||||
class AnalysisPass : public Pass {
|
||||
public:
|
||||
AnalysisPass(const std::string& name) : Pass(PK_Analysis, name) {}
|
||||
// 分析遍通常需要一个模块或函数作为输入,并计算出分析结果
|
||||
// 具体分析结果的存储和访问方式需要设计
|
||||
};
|
||||
|
||||
} // 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,9 +13,9 @@ using namespace antlr4;
|
||||
|
||||
#include "SysYIRGenerator.h"
|
||||
#include "SysYIRPrinter.h"
|
||||
#include "SysYIRCFGOpt.h"
|
||||
#include "SysYIROptPre.h"
|
||||
#include "RISCv64Backend.h"
|
||||
// #include "SysYIRAnalyser.h"
|
||||
#include "SysYIRAnalyser.h"
|
||||
// #include "DeadCodeElimination.h"
|
||||
#include "AddressCalculationExpansion.h"
|
||||
// #include "Mem2Reg.h"
|
||||
@ -132,13 +132,13 @@ int main(int argc, char **argv) {
|
||||
DEBUG = 1; // 这里可能需要更精细地控制 DEBUG 的开启时机和范围
|
||||
}
|
||||
// 默认优化 pass (在所有优化级别都会执行)
|
||||
SysYCFGOpt cfgopt(moduleIR, builder);
|
||||
cfgopt.SysYOptimizateAfterIR();
|
||||
SysYOptPre optPre(moduleIR, builder);
|
||||
optPre.SysYOptimizateAfterIR();
|
||||
|
||||
// ControlFlowAnalysis cfa(moduleIR);
|
||||
// cfa.init();
|
||||
// ActiveVarAnalysis ava;
|
||||
// ava.init(moduleIR);
|
||||
ControlFlowAnalysis cfa(moduleIR);
|
||||
cfa.init();
|
||||
ActiveVarAnalysis ava;
|
||||
ava.init(moduleIR);
|
||||
|
||||
if (DEBUG) {
|
||||
cout << "=== After CFA & AVA (Default) ===\n";
|
||||
|
||||
@ -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"
|
||||
|
||||
Reference in New Issue
Block a user