From 50fd9cffe9726302699a0115f986637346ffdbcd Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Wed, 16 Jul 2025 13:04:05 +0800 Subject: [PATCH] =?UTF-8?q?[IRPrinter&DCE]=E4=BF=AE=E6=94=B9=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=96=B9=E4=BE=BF=E8=B0=83=E8=AF=95=E6=89=93=E5=8D=B0?= =?UTF-8?q?=EF=BC=8C=E5=9C=A8DEC=E4=B8=AD=E5=A2=9E=E5=8A=A0=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DeadCodeElimination.cpp | 31 +++++++++++++++++++++++++++++-- src/include/DeadCodeElimination.h | 2 ++ src/include/SysYIRPrinter.h | 13 +++++++------ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/DeadCodeElimination.cpp b/src/DeadCodeElimination.cpp index 9abca1c..ffe6022 100644 --- a/src/DeadCodeElimination.cpp +++ b/src/DeadCodeElimination.cpp @@ -1,8 +1,9 @@ #include "DeadCodeElimination.h" +#include +extern int DEBUG; namespace sysy { - void DeadCodeElimination::runDCEPipeline() { const auto& functions = pModule->getFunctions(); for (const auto& function : functions) { @@ -58,6 +59,10 @@ void DeadCodeElimination::eliminateDeadStores(Function* func, bool& changed) { if (changetag) { changed = true; + if(DEBUG){ + std::cout << "=== Dead Store Found ===\n"; + SysYPrinter::printInst(storeInst); + } usedelete(storeInst); iter = instrs.erase(iter); } else { @@ -76,6 +81,10 @@ void DeadCodeElimination::eliminateDeadLoads(Function* func, bool& changed) { 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); + } usedelete(inst); iter = instrs.erase(iter); continue; @@ -101,6 +110,10 @@ void DeadCodeElimination::eliminateDeadAllocas(Function* func, bool& changed) { func->getEntryBlock()->getArguments().end(), allocaInst) == func->getEntryBlock()->getArguments().end()) { changed = true; + if(DEBUG){ + std::cout << "=== Dead Alloca Found ===\n"; + SysYPrinter::printInst(inst); + } usedelete(inst); iter = instrs.erase(iter); continue; @@ -116,8 +129,12 @@ void DeadCodeElimination::eliminateDeadIndirectiveAllocas(Function* func, bool& FunctionAnalysisInfo* funcInfo = pCFA->getFunctionAnalysisInfo(func); for (auto it = funcInfo->getIndirectAllocas().begin(); it != funcInfo->getIndirectAllocas().end();) { auto &allocaInst = *it; - if (allocaInst->getUses().empty()) { + 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; @@ -132,6 +149,10 @@ void DeadCodeElimination::eliminateDeadGlobals(bool& changed) { 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; @@ -207,6 +228,12 @@ void DeadCodeElimination::eliminateDeadRedundantLoadStore(Function* func, bool& // 可以优化直接把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); + } usedelete(loadInst); iter = instrs.erase(iter); // 删除 prevStore 这里是不是可以留给删除无用store处理? diff --git a/src/include/DeadCodeElimination.h b/src/include/DeadCodeElimination.h index 2d614bd..72b9935 100644 --- a/src/include/DeadCodeElimination.h +++ b/src/include/DeadCodeElimination.h @@ -2,6 +2,8 @@ #include "IR.h" #include "SysYIRAnalyser.h" +#include "SysYIRPrinter.h" + namespace sysy { class DeadCodeElimination { diff --git a/src/include/SysYIRPrinter.h b/src/include/SysYIRPrinter.h index 114fb05..bfd78bd 100644 --- a/src/include/SysYIRPrinter.h +++ b/src/include/SysYIRPrinter.h @@ -15,15 +15,16 @@ public: public: void printIR(); void printGlobalVariable(); - void printFunction(Function *function); - void printInst(Instruction *pInst); - void printType(Type *type); - void printValue(Value *value); + public: + static void printFunction(Function *function); + static void printInst(Instruction *pInst); + static void printType(Type *type); + static void printValue(Value *value); static std::string getOperandName(Value *operand); - std::string getTypeString(Type *type); - std::string getValueName(Value *value); + static std::string getTypeString(Type *type); + static std::string getValueName(Value *value); }; } // namespace sysy