Compare commits

...

5 Commits
Mem2Reg ... DCE

9 changed files with 427 additions and 190 deletions

View File

@@ -19,6 +19,7 @@ add_executable(sysyc
SysYIRPrinter.cpp
SysYIROptPre.cpp
SysYIRAnalyser.cpp
DeadCodeElimination.cpp
Mem2Reg.cpp
Reg2Mem.cpp
RISCv32Backend.cpp

249
src/DeadCodeElimination.cpp Normal file
View File

@@ -0,0 +1,249 @@
#include "DeadCodeElimination.h"
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 (isGlobal(pointer) || (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;
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;
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;
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;
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;
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;
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;
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);
usedelete(loadInst);
iter = instrs.erase(iter);
// 删除 prevStore 这里是不是可以留给删除无用store处理
// if (prevStore->getUses().empty()) {
// usedelete(prevStore);
// instrs.erase(prevIter); // 删除 prevStore
// }
continue; // 跳过 ++iter因为已经移动迭代器
}
}
}
}
}
}
}
}
++iter;
}
}
}
bool DeadCodeElimination::isGlobal(Value *val){
auto gval = dynamic_cast<GlobalValue *>(val);
return gval != nullptr;
}
bool DeadCodeElimination::isArr(Value *val){
auto aval = dynamic_cast<AllocaInst *>(val);
return aval != nullptr && aval->getNumDims() != 0;
}
void DeadCodeElimination::usedelete(Instruction *instr){
for (auto &use1 : instr->getOperands()) {
auto val1 = use1->getValue();
val1->removeUse(use1);
}
}
} // namespace sysy

View File

@@ -135,7 +135,7 @@ auto Function::getCalleesWithNoExternalAndSelf() -> std::set<Function *> {
}
return result;
}
// 函数克隆,后续函数级优化(内联等)需要用到
Function * Function::clone(const std::string &suffix) const {
std::stringstream ss;
std::map<BasicBlock *, BasicBlock *> oldNewBlockMap;
@@ -527,11 +527,7 @@ Function * Function::clone(const std::string &suffix) const {
return newFunction;
}
/**
* @brief 设置操作数
*
* @param [in] index 所要设置的操作数的位置
* @param [in] value 所要设置成的value
* @return 无返回值
* 设置操作数
*/
void User::setOperand(unsigned index, Value *value) {
assert(index < getNumOperands());
@@ -539,11 +535,7 @@ void User::setOperand(unsigned index, Value *value) {
value->addUse(operands[index]);
}
/**
* @brief 替换操作数
*
* @param [in] index 所要替换的操作数的位置
* @param [in] value 所要替换成的value
* @return 无返回值
* 替换操作数
*/
void User::replaceOperand(unsigned index, Value *value) {
assert(index < getNumOperands());
@@ -561,17 +553,12 @@ CallInst::CallInst(Function *callee, const std::vector<Value *> &args, BasicBloc
}
}
/**
* @brief 获取被调用函数的指针
*
* @return 被调用函数的指针
* 获取被调用函数的指针
*/
Function * CallInst::getCallee() const { return dynamic_cast<Function *>(getOperand(0)); }
/**
* @brief 获取变量指针
*
* @param [in] name 变量名字
* @return 变量指针
* 获取变量指针
*/
auto SymbolTable::getVariable(const std::string &name) const -> User * {
auto node = curNode;
@@ -586,11 +573,7 @@ auto SymbolTable::getVariable(const std::string &name) const -> User * {
return nullptr;
}
/**
* @brief 添加变量
*
* @param [in] name 变量名字
* @param [in] variable 变量指针
* @return 变量指针
* 添加变量到符号表
*/
auto SymbolTable::addVariable(const std::string &name, User *variable) -> User * {
User *result = nullptr;
@@ -621,21 +604,15 @@ auto SymbolTable::addVariable(const std::string &name, User *variable) -> User *
return result;
}
/**
* @brief 获取全局变量
*
* @return 全局变量列表
* 获取全局变量
*/
auto SymbolTable::getGlobals() -> std::vector<std::unique_ptr<GlobalValue>> & { return globals; }
/**
* @brief 获取常量
*
* @return 常量列表
* 获取常量
*/
auto SymbolTable::getConsts() const -> const std::vector<std::unique_ptr<ConstantVariable>> & { return consts; }
/**
* @brief 进入新的作用域
*
* @return 无返回值
* 进入新的作用域
*/
void SymbolTable::enterNewScope() {
auto newNode = new SymbolTableNode;
@@ -647,31 +624,20 @@ void SymbolTable::enterNewScope() {
curNode = newNode;
}
/**
* @brief 进入全局作用域
*
* @return 无返回值
* 进入全局作用域
*/
void SymbolTable::enterGlobalScope() { curNode = nodeList.front().get(); }
/**
* @brief 离开作用域
*
* @return 无返回值
* 离开作用域
*/
void SymbolTable::leaveScope() { curNode = curNode->pNode; }
/**
* @brief 是否位于全局作用域
*
* @return 布尔值
* 是否位于全局作用域
*/
auto SymbolTable::isInGlobalScope() const -> bool { return curNode->pNode == nullptr; }
/**
* @brief 移动指令
*
* @param [in] sourcePos 源指令列表位置
* @param [in] targetPos 目的指令列表位置
* @param [in] block 目标基本块
* @return 无返回值
*移动指令
*/
auto BasicBlock::moveInst(iterator sourcePos, iterator targetPos, BasicBlock *block) -> iterator {
auto inst = sourcePos->release();

View File

@@ -47,12 +47,12 @@ std::unordered_set<BasicBlock *> Mem2Reg::computeIterDf(const std::unordered_set
*/
auto Mem2Reg::computeValue2Blocks() -> void {
SysYPrinter printer(pModule); // 初始化打印机
std::cout << "===== Start computeValue2Blocks =====" << std::endl;
// std::cout << "===== Start computeValue2Blocks =====" << std::endl;
auto &functions = pModule->getFunctions();
for (const auto &function : functions) {
auto func = function.second.get();
std::cout << "\nProcessing function: " << func->getName() << std::endl;
// std::cout << "\nProcessing function: " << func->getName() << std::endl;
FunctionAnalysisInfo* funcInfo = controlFlowAnalysis->getFunctionAnalysisInfo(func);
if (!funcInfo) {
@@ -61,98 +61,98 @@ auto Mem2Reg::computeValue2Blocks() -> void {
}
auto basicBlocks = func->getBasicBlocks();
std::cout << "BasicBlocks count: " << basicBlocks.size() << std::endl;
// std::cout << "BasicBlocks count: " << basicBlocks.size() << std::endl;
for (auto &it : basicBlocks) {
auto basicBlock = it.get();
std::cout << "\nProcessing BB: " << basicBlock->getName() << std::endl;
// std::cout << "\nProcessing BB: " << basicBlock->getName() << std::endl;
// printer.printBlock(basicBlock); // 打印基本块内容
auto &instrs = basicBlock->getInstructions();
for (auto &instr : instrs) {
std::cout << " Analyzing instruction: ";
printer.printInst(instr.get());
std::cout << std::endl;
// std::cout << " Analyzing instruction: ";
// printer.printInst(instr.get());
// std::cout << std::endl;
if (instr->isAlloca()) {
if (!(isArr(instr.get()) || isGlobal(instr.get()))) {
std::cout << " Found alloca: ";
printer.printInst(instr.get());
std::cout << " -> Adding to allocBlocks" << std::endl;
// std::cout << " Found alloca: ";
// printer.printInst(instr.get());
// std::cout << " -> Adding to allocBlocks" << std::endl;
funcInfo->addValue2AllocBlocks(instr.get(), basicBlock);
} else {
std::cout << " Skip array/global alloca: ";
printer.printInst(instr.get());
std::cout << std::endl;
// std::cout << " Skip array/global alloca: ";
// printer.printInst(instr.get());
// std::cout << std::endl;
}
}
else if (instr->isStore()) {
auto val = instr->getOperand(1);
std::cout << " Store target: ";
printer.printInst(dynamic_cast<Instruction *>(val));
// std::cout << " Store target: ";
// printer.printInst(dynamic_cast<Instruction *>(val));
if (!(isArr(val) || isGlobal(val))) {
std::cout << " Adding store to defBlocks for value: ";
printer.printInst(dynamic_cast<Instruction *>(instr.get()));
std::cout << std::endl;
// std::cout << " Adding store to defBlocks for value: ";
// printer.printInst(dynamic_cast<Instruction *>(instr.get()));
// std::cout << std::endl;
// 将store的目标值添加到defBlocks中
funcInfo->addValue2DefBlocks(val, basicBlock);
} else {
std::cout << " Skip array/global store" << std::endl;
// std::cout << " Skip array/global store" << std::endl;
}
}
else if (instr->isLoad()) {
auto val = instr->getOperand(0);
std::cout << " Load source: ";
printer.printInst(dynamic_cast<Instruction *>(val));
std::cout << std::endl;
// std::cout << " Load source: ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// std::cout << std::endl;
if (!(isArr(val) || isGlobal(val))) {
std::cout << " Adding load to useBlocks for value: ";
printer.printInst(dynamic_cast<Instruction *>(val));
std::cout << std::endl;
// std::cout << " Adding load to useBlocks for value: ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// std::cout << std::endl;
funcInfo->addValue2UseBlocks(val, basicBlock);
} else {
std::cout << " Skip array/global load" << std::endl;
// std::cout << " Skip array/global load" << std::endl;
}
}
}
}
// 打印分析结果
std::cout << "\nAnalysis results for function " << func->getName() << ":" << std::endl;
// std::cout << "\nAnalysis results for function " << func->getName() << ":" << std::endl;
auto &allocMap = funcInfo->getValue2AllocBlocks();
std::cout << "AllocBlocks (" << allocMap.size() << "):" << std::endl;
for (auto &[val, bb] : allocMap) {
std::cout << " ";
printer.printInst(dynamic_cast<Instruction *>(val));
std::cout << " in BB: " << bb->getName() << std::endl;
}
// auto &allocMap = funcInfo->getValue2AllocBlocks();
// std::cout << "AllocBlocks (" << allocMap.size() << "):" << std::endl;
// for (auto &[val, bb] : allocMap) {
// std::cout << " ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// std::cout << " in BB: " << bb->getName() << std::endl;
// }
auto &defMap = funcInfo->getValue2DefBlocks();
std::cout << "DefBlocks (" << defMap.size() << "):" << std::endl;
for (auto &[val, bbs] : defMap) {
std::cout << " ";
printer.printInst(dynamic_cast<Instruction *>(val));
for (const auto &[bb, count] : bbs) {
std::cout << " in BB: " << bb->getName() << " (count: " << count << ")";
}
}
// auto &defMap = funcInfo->getValue2DefBlocks();
// std::cout << "DefBlocks (" << defMap.size() << "):" << std::endl;
// for (auto &[val, bbs] : defMap) {
// std::cout << " ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// for (const auto &[bb, count] : bbs) {
// std::cout << " in BB: " << bb->getName() << " (count: " << count << ")";
// }
// }
auto &useMap = funcInfo->getValue2UseBlocks();
std::cout << "UseBlocks (" << useMap.size() << "):" << std::endl;
for (auto &[val, bbs] : useMap) {
std::cout << " ";
printer.printInst(dynamic_cast<Instruction *>(val));
for (const auto &[bb, count] : bbs) {
std::cout << " in BB: " << bb->getName() << " (count: " << count << ")";
}
}
// auto &useMap = funcInfo->getValue2UseBlocks();
// std::cout << "UseBlocks (" << useMap.size() << "):" << std::endl;
// for (auto &[val, bbs] : useMap) {
// std::cout << " ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// for (const auto &[bb, count] : bbs) {
// std::cout << " in BB: " << bb->getName() << " (count: " << count << ")";
// }
// }
}
std::cout << "===== End computeValue2Blocks =====" << std::endl;
// std::cout << "===== End computeValue2Blocks =====" << std::endl;
}
@@ -207,33 +207,31 @@ auto Mem2Reg::cascade(Instruction *instr, bool &changed, Function *func, BasicBl
}
/**
* @brief llvm mem2reg预优化1: 删除不含load的alloc和store
* llvm mem2reg预优化1: 删除不含load的alloc和store
*
* 1. 删除不含load的alloc和store
* 2. 删除store指令之前的用于作store指令第0个操作数的那些级联指令就冗余了也要删除
* 3. 删除之后可能有些变量的load使用恰好又没有了因此再次从第一步开始循环这里使用不动点法
*
* @note 额外说明:由于删除了级联关系,所以这里的方法有点儿激进;
* 由于删除了级联关系,所以这里的方法有点儿激进;
* 同时也考虑了级联关系时如果调用了函数可能会有side effect所以没有删除调用函数的级联关系
* 而且关于函数参数的alloca不会在指令中删除也不会在value2Alloca中删除;
* 同样地我们不考虑数组和global不过这里的代码是基于value2blocks的在value2blocks中已经考虑了所以不用显式指明
*
* @param [in] void
* @return 无返回值,但满足条件的情况下会对指令进行删除
*=
*/
auto Mem2Reg::preOptimize1() -> void {
SysYPrinter printer(pModule); // 初始化打印机
auto &functions = pModule->getFunctions();
std::cout << "===== Start preOptimize1 =====" << std::endl;
// std::cout << "===== Start preOptimize1 =====" << std::endl;
for (const auto &function : functions) {
auto func = function.second.get();
std::cout << "\nProcessing function: " << func->getName() << std::endl;
// std::cout << "\nProcessing function: " << func->getName() << std::endl;
FunctionAnalysisInfo* funcInfo = controlFlowAnalysis->getFunctionAnalysisInfo(func);
if (!funcInfo) {
std::cerr << "ERROR: No analysis info for function " << func->getName() << std::endl;
// std::cerr << "ERROR: No analysis info for function " << func->getName() << std::endl;
continue;
}
@@ -242,48 +240,45 @@ auto Mem2Reg::preOptimize1() -> void {
auto &vToAllocB = funcInfo->getValue2AllocBlocks();
// 打印初始状态
std::cout << "Initial allocas: " << vToAllocB.size() << std::endl;
for (auto &[val, bb] : vToAllocB) {
std::cout << " Alloca: ";
printer.printInst(dynamic_cast<Instruction *>(val));
std::cout << " in BB: " << bb->getName() << std::endl;
}
// std::cout << "Initial allocas: " << vToAllocB.size() << std::endl;
// for (auto &[val, bb] : vToAllocB) {
// std::cout << " Alloca: ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// std::cout << " in BB: " << bb->getName() << std::endl;
// }
// 阶段1删除无store的alloca
std::cout << "\nPhase 1: Remove unused allocas" << std::endl;
// std::cout << "\nPhase 1: Remove unused allocas" << std::endl;
for (auto iter = vToAllocB.begin(); iter != vToAllocB.end();) {
auto val = iter->first;
auto bb = iter->second;
std::cout << "Checking alloca: ";
printer.printInst(dynamic_cast<Instruction *>(val));
std::cout << " in BB: " << bb->getName() << std::endl;
// std::cout << "Checking alloca: ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// std::cout << " in BB: " << bb->getName() << std::endl;
// 如果该alloca没有对应的store指令且不在函数参数中
// 这里的vToDefB是value2DefBlocksvToUseB是value2UseBlocks
// 打印vToDefB
std::cout << "DefBlocks (" << vToDefB.size() << "):" << std::endl;
for (auto &[val, bbs] : vToDefB) {
std::cout << " ";
printer.printInst(dynamic_cast<Instruction *>(val));
for (const auto &[bb, count] : bbs) {
std::cout << " in BB: " << bb->getName() << " (count: " << count << ")" << std::endl;
}
}
std::cout << vToDefB.count(val) << std::endl;
bool hasStore = false;
// std::cout << "DefBlocks (" << vToDefB.size() << "):" << std::endl;
// for (auto &[val, bbs] : vToDefB) {
// std::cout << " ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// for (const auto &[bb, count] : bbs) {
// std::cout << " in BB: " << bb->getName() << " (count: " << count << ")" << std::endl;
// }
// }
// std::cout << vToDefB.count(val) << std::endl;
if (vToDefB.count(val) == 0U &&
std::find(func->getEntryBlock()->getArguments().begin(),
func->getEntryBlock()->getArguments().end(),
val) == func->getEntryBlock()->getArguments().end()) {
std::cout << " Removing unused alloca: ";
printer.printInst(dynamic_cast<Instruction *>(val));
std::cout << std::endl;
// std::cout << " Removing unused alloca: ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// std::cout << std::endl;
auto tofind = std::find_if(bb->getInstructions().begin(),
bb->getInstructions().end(),
@@ -291,7 +286,7 @@ auto Mem2Reg::preOptimize1() -> void {
return instr.get() == val;
});
if (tofind == bb->getInstructions().end()) {
std::cerr << "ERROR: Alloca not found in BB!" << std::endl;
// std::cerr << "ERROR: Alloca not found in BB!" << std::endl;
++iter;
continue;
}
@@ -305,37 +300,37 @@ auto Mem2Reg::preOptimize1() -> void {
}
// 阶段2删除无load的store
std::cout << "\nPhase 2: Remove dead stores" << std::endl;
// std::cout << "\nPhase 2: Remove dead stores" << std::endl;
bool changed = true;
int iteration = 0;
while (changed) {
changed = false;
iteration++;
std::cout << "\nIteration " << iteration << std::endl;
// std::cout << "\nIteration " << iteration << std::endl;
for (auto iter = vToDefB.begin(); iter != vToDefB.end();) {
auto val = iter->first;
std::cout << "Checking value: ";
printer.printInst(dynamic_cast<Instruction *>(val));
std::cout << std::endl;
// std::cout << "Checking value: ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// std::cout << std::endl;
if (vToUseB.count(val) == 0U) {
std::cout << " Found dead store for value: ";
printer.printInst(dynamic_cast<Instruction *>(val));
std::cout << std::endl;
// std::cout << " Found dead store for value: ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// std::cout << std::endl;
auto blocks = funcInfo->getDefBlocksByValue(val);
for (auto block : blocks) {
std::cout << " Processing BB: " << block->getName() << std::endl;
// std::cout << " Processing BB: " << block->getName() << std::endl;
// printer.printBlock(block); // 打印基本块内容
auto &instrs = block->getInstructions();
for (auto it = instrs.begin(); it != instrs.end();) {
if ((*it)->isStore() && (*it)->getOperand(1) == val) {
std::cout << " Removing store: ";
printer.printInst(it->get());
// std::cout << " Removing store: ";
// printer.printInst(it->get());
std::cout << std::endl;
auto valUsedByStore = dynamic_cast<Instruction *>((*it)->getOperand(0));
@@ -344,9 +339,9 @@ auto Mem2Reg::preOptimize1() -> void {
if (valUsedByStore != nullptr &&
valUsedByStore->getUses().size() == 1 &&
valUsedByStore->getUses().front()->getUser() == (*it).get()) {
std::cout << " Cascade deleting: ";
printer.printInst(valUsedByStore);
std::cout << std::endl;
// std::cout << " Cascade deleting: ";
// printer.printInst(valUsedByStore);
// std::cout << std::endl;
cascade(valUsedByStore, changed, func, block, instrs);
}
@@ -364,9 +359,9 @@ auto Mem2Reg::preOptimize1() -> void {
val) == func->getEntryBlock()->getArguments().end()) {
auto bb = funcInfo->getAllocBlockByValue(val);
if (bb != nullptr) {
std::cout << " Removing alloca: ";
printer.printInst(dynamic_cast<Instruction *>(val));
std::cout << " in BB: " << bb->getName() << std::endl;
// std::cout << " Removing alloca: ";
// printer.printInst(dynamic_cast<Instruction *>(val));
// std::cout << " in BB: " << bb->getName() << std::endl;
funcInfo->removeValue2AllocBlock(val);
auto tofind = std::find_if(bb->getInstructions().begin(),
@@ -389,11 +384,11 @@ auto Mem2Reg::preOptimize1() -> void {
}
}
}
std::cout << "===== End preOptimize1 =====" << std::endl;
// std::cout << "===== End preOptimize1 =====" << std::endl;
}
/**
* @brief llvm mem2reg预优化2: 针对某个变量的Defblocks只有一个块的情况
* llvm mem2reg预优化2: 针对某个变量的Defblocks只有一个块的情况
*
* 1. 该基本块最后一次对该变量的store指令后的所有对该变量的load指令都可以替换为该基本块最后一次store指令的第0个操作数
* 2. 以该基本块为必经结点的结点集合中的对该变量的load指令都可以替换为该基本块最后一次对该变量的store指令的第0个操作数
@@ -402,12 +397,9 @@ auto Mem2Reg::preOptimize1() -> void {
* 4.
* 如果对该value的所有load都替换掉了对于该变量剩下还有store的话就转换成了preOptimize1的情况再调用preOptimize1进行删除
*
* @note 额外说明:同样有点儿激进;
* 同样不考虑数组和全局变量因为这些变量不会被mem2reg优化在value2blocks中已经考虑了所以不用显式指明
* 替换的操作采用了UD链进行简化和效率的提升
*
* @param [in] void
* @return 无返回值,但满足条件的情况下会对指令的操作数进行替换以及对指令进行删除
*/
auto Mem2Reg::preOptimize2() -> void {
auto &functions = pModule->getFunctions();
@@ -570,14 +562,12 @@ auto Mem2Reg::preOptimize3() -> void {
}
/**
* @brief 为所有变量的定义块集合的迭代支配边界插入phi结点
* 为所有变量的定义块集合的迭代支配边界插入phi结点
*
* insertPhi是mem2reg的核心之一这里是对所有变量的迭代支配边界的phi结点插入无参数也无返回值
* 同样跳过对数组和全局变量的处理因为这些变量不会被mem2reg优化刚好这里在计算value2DefBlocks时已经跳过了所以不需要再显式处理了
* 同时我们进行了剪枝处理只有在基本块入口活跃的变量才插入phi函数
*
* @param [in] void
* @return 无返回值但是会在每个变量的迭代支配边界上插入phi结点
*/
auto Mem2Reg::insertPhi() -> void {
auto &functions = pModule->getFunctions();
@@ -604,15 +594,11 @@ auto Mem2Reg::insertPhi() -> void {
}
/**
* @brief 重命名
* 重命名
*
* 重命名是mem2reg的核心之二这里是对单个块的重命名递归实现
* 同样跳过对数组和全局变量的处理因为这些变量不会被mem2reg优化
*
* @param [in] block 一个基本块
* @param [in] count 计数器,用于给变量重命名,地址传递
* @param [in] stacks 用于存储变量的栈,地址传递
* @return 无返回值
*/
auto Mem2Reg::rename(BasicBlock *block, std::unordered_map<Value *, int> &count,
std::unordered_map<Value *, std::stack<Instruction *>> &stacks) -> void {
@@ -712,12 +698,10 @@ auto Mem2Reg::rename(BasicBlock *block, std::unordered_map<Value *, int> &count,
}
/**
* @brief 重命名所有块
* 重命名所有块
*
* 调用rename自上而下实现所有rename
*
* @param [in] void
* @return 无返回值
*/
auto Mem2Reg::renameAll() -> void {
auto &functions = pModule->getFunctions();
@@ -736,20 +720,17 @@ auto Mem2Reg::renameAll() -> void {
}
/**
* @brief mem2reg对外的接口
* mem2reg对外的接口
*
* 静态单一赋值 + mem2reg等pass的逻辑组合
*
* @param [in] void
* @return 无返回值
*/
auto Mem2Reg::mem2regPipeline() -> void {
// 首先进行mem2reg的前置分析
controlFlowAnalysis = new ControlFlowAnalysis(pModule);
activeVarAnalysis = new ActiveVarAnalysis();
// 控制流分析
controlFlowAnalysis->clear();
controlFlowAnalysis->runControlFlowAnalysis();
// 活跃变量分析
activeVarAnalysis->clear();
dataFlowAnalysisUtils.addBackwardAnalyzer(activeVarAnalysis);
dataFlowAnalysisUtils.backwardAnalyze(pModule);
@@ -775,13 +756,10 @@ auto Mem2Reg::mem2regPipeline() -> void {
}
/**
* @brief 计算块n是块s的第几个前驱
* 计算块n是块s的第几个前驱
*
* helperfunction没有返回值但是会将dom和other的交集赋值给dom
*
* @param [in] n 基本块n是s的前驱之一
* @param [in] s 基本块s是n的后继之一
* @return 返回n是s的第几个前驱
*/
auto Mem2Reg::getPredIndex(BasicBlock *n, BasicBlock *s) -> int {
int index = 0;

View File

@@ -1,11 +1,3 @@
/**
* @file: Sysyoptimization.cpp
* @brief CFG优化
* @Author : Ixeux email:you@domain.com
* @Version : 1.0
* @Creat Date : 2024-08-10
*
*/
#include "SysYIROptPre.h"
#include <cassert>
#include <list>

View File

@@ -0,0 +1,37 @@
#pragma once
#include "IR.h"
#include "SysYIRAnalyser.h"
namespace sysy {
class DeadCodeElimination {
private:
Module *pModule;
ControlFlowAnalysis *pCFA; // 控制流分析指针
ActiveVarAnalysis *pAVA; // 活跃变量分析指针
DataFlowAnalysisUtils dataFlowAnalysisUtils; // 数据流分析工具类
public:
explicit DeadCodeElimination(Module *pMoudle,
ControlFlowAnalysis *pCFA = nullptr,
ActiveVarAnalysis *pAVA = nullptr)
: pModule(pMoudle), pCFA(pCFA), pAVA(pAVA), dataFlowAnalysisUtils() {} // 构造函数
// TODO根据参数传入的passes来运行不同的死代码删除流程
// void runDCEPipeline(const std::vector<std::string>& passes = {
// "dead-store", "redundant-load-store", "dead-load", "dead-alloca", "dead-global"
// });
void runDCEPipeline(); // 运行死代码删除
void eliminateDeadStores(Function* func, bool& changed); // 消除无用存储
void eliminateDeadLoads(Function* func, bool& changed); // 消除无用加载
void eliminateDeadAllocas(Function* func, bool& changed); // 消除无用内存分配
void eliminateDeadGlobals(bool& changed); // 消除无用全局变量
void eliminateDeadIndirectiveAllocas(Function* func, bool& changed); // 消除无用间接内存分配(phi节点)
void eliminateDeadRedundantLoadStore(Function* func, bool& changed); // 消除冗余加载和存储
bool isGlobal(Value *val);
bool isArr(Value *val);
void usedelete(Instruction *instr);
};
} // namespace sysy

View File

@@ -263,7 +263,7 @@ class IRBuilder {
auto inst = new AllocaInst(type, dims, parent, name);
assert(inst);
return inst;
} ///< 创建不插入指令列表的分配指令
} ///< 创建不插入指令列表的分配指令[仅用于phi指令]
LoadInst * createLoadInst(Value *pointer, const std::vector<Value *> &indices = {}, const std::string &name = "") {
std::string newName;
if (name.empty()) {

View File

@@ -17,13 +17,14 @@ class Mem2Reg {
private:
Module *pModule;
IRBuilder *pBuilder;
ActiveVarAnalysis *activeVarAnalysis; // 活跃变量分析
ControlFlowAnalysis *controlFlowAnalysis; // 控制流分析
ActiveVarAnalysis *activeVarAnalysis; // 活跃变量分析
DataFlowAnalysisUtils dataFlowAnalysisUtils;
public:
Mem2Reg(Module *pMoudle, IRBuilder *pBuilder) :
pModule(pMoudle), pBuilder(pBuilder), activeVarAnalysis(nullptr), controlFlowAnalysis(nullptr), dataFlowAnalysisUtils()
Mem2Reg(Module *pMoudle, IRBuilder *pBuilder,
ControlFlowAnalysis *pCFA = nullptr, ActiveVarAnalysis *pAVA = nullptr) :
pModule(pMoudle), pBuilder(pBuilder), controlFlowAnalysis(pCFA), activeVarAnalysis(pAVA), dataFlowAnalysisUtils()
{} // 初始化函数
void mem2regPipeline(); ///< mem2reg

View File

@@ -11,6 +11,7 @@ using namespace antlr4;
#include "SysYIRPrinter.h"
#include "SysYIROptPre.h"
#include "SysYIRAnalyser.h"
#include "DeadCodeElimination.h"
#include "Mem2Reg.h"
#include "Reg2Mem.h"
// #include "LLVMIRGenerator.h"
@@ -86,12 +87,24 @@ int main(int argc, char **argv) {
auto builder = generator.getBuilder();
SysYOptPre optPre(moduleIR, builder);
optPre.SysYOptimizateAfterIR();
Mem2Reg mem2reg(moduleIR, builder);
ControlFlowAnalysis cfa(moduleIR);
cfa.init();
ActiveVarAnalysis ava;
ava.init(moduleIR);
printer.printIR();
DeadCodeElimination dce(moduleIR, &cfa, &ava);
dce.runDCEPipeline();
Mem2Reg mem2reg(moduleIR, builder, &cfa, &ava);
mem2reg.mem2regPipeline();
printer.printIR();
Reg2Mem reg2mem(moduleIR, builder);
reg2mem.DeletePhiInst();
printer.printIR();
dce.runDCEPipeline();
printer.printIR();
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;