[IRPrinter&DCE]修改定义方便调试打印,在DEC中增加调试信息
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
#include "DeadCodeElimination.h"
|
#include "DeadCodeElimination.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
extern int DEBUG;
|
||||||
namespace sysy {
|
namespace sysy {
|
||||||
|
|
||||||
|
|
||||||
void DeadCodeElimination::runDCEPipeline() {
|
void DeadCodeElimination::runDCEPipeline() {
|
||||||
const auto& functions = pModule->getFunctions();
|
const auto& functions = pModule->getFunctions();
|
||||||
for (const auto& function : functions) {
|
for (const auto& function : functions) {
|
||||||
@@ -58,6 +59,10 @@ void DeadCodeElimination::eliminateDeadStores(Function* func, bool& changed) {
|
|||||||
|
|
||||||
if (changetag) {
|
if (changetag) {
|
||||||
changed = true;
|
changed = true;
|
||||||
|
if(DEBUG){
|
||||||
|
std::cout << "=== Dead Store Found ===\n";
|
||||||
|
SysYPrinter::printInst(storeInst);
|
||||||
|
}
|
||||||
usedelete(storeInst);
|
usedelete(storeInst);
|
||||||
iter = instrs.erase(iter);
|
iter = instrs.erase(iter);
|
||||||
} else {
|
} else {
|
||||||
@@ -76,6 +81,10 @@ void DeadCodeElimination::eliminateDeadLoads(Function* func, bool& changed) {
|
|||||||
if (inst->isBinary() || inst->isUnary() || inst->isLoad()) {
|
if (inst->isBinary() || inst->isUnary() || inst->isLoad()) {
|
||||||
if (inst->getUses().empty()) {
|
if (inst->getUses().empty()) {
|
||||||
changed = true;
|
changed = true;
|
||||||
|
if(DEBUG){
|
||||||
|
std::cout << "=== Dead Load Binary Unary Found ===\n";
|
||||||
|
SysYPrinter::printInst(inst);
|
||||||
|
}
|
||||||
usedelete(inst);
|
usedelete(inst);
|
||||||
iter = instrs.erase(iter);
|
iter = instrs.erase(iter);
|
||||||
continue;
|
continue;
|
||||||
@@ -101,6 +110,10 @@ void DeadCodeElimination::eliminateDeadAllocas(Function* func, bool& changed) {
|
|||||||
func->getEntryBlock()->getArguments().end(),
|
func->getEntryBlock()->getArguments().end(),
|
||||||
allocaInst) == func->getEntryBlock()->getArguments().end()) {
|
allocaInst) == func->getEntryBlock()->getArguments().end()) {
|
||||||
changed = true;
|
changed = true;
|
||||||
|
if(DEBUG){
|
||||||
|
std::cout << "=== Dead Alloca Found ===\n";
|
||||||
|
SysYPrinter::printInst(inst);
|
||||||
|
}
|
||||||
usedelete(inst);
|
usedelete(inst);
|
||||||
iter = instrs.erase(iter);
|
iter = instrs.erase(iter);
|
||||||
continue;
|
continue;
|
||||||
@@ -116,8 +129,12 @@ void DeadCodeElimination::eliminateDeadIndirectiveAllocas(Function* func, bool&
|
|||||||
FunctionAnalysisInfo* funcInfo = pCFA->getFunctionAnalysisInfo(func);
|
FunctionAnalysisInfo* funcInfo = pCFA->getFunctionAnalysisInfo(func);
|
||||||
for (auto it = funcInfo->getIndirectAllocas().begin(); it != funcInfo->getIndirectAllocas().end();) {
|
for (auto it = funcInfo->getIndirectAllocas().begin(); it != funcInfo->getIndirectAllocas().end();) {
|
||||||
auto &allocaInst = *it;
|
auto &allocaInst = *it;
|
||||||
if (allocaInst->getUses().empty()) {
|
if (allocaInst->getUses().empty()) {
|
||||||
changed = true;
|
changed = true;
|
||||||
|
if(DEBUG){
|
||||||
|
std::cout << "=== Dead Indirect Alloca Found ===\n";
|
||||||
|
SysYPrinter::printInst(allocaInst.get());
|
||||||
|
}
|
||||||
it = funcInfo->getIndirectAllocas().erase(it);
|
it = funcInfo->getIndirectAllocas().erase(it);
|
||||||
} else {
|
} else {
|
||||||
++it;
|
++it;
|
||||||
@@ -132,6 +149,10 @@ void DeadCodeElimination::eliminateDeadGlobals(bool& changed) {
|
|||||||
auto& global = *it;
|
auto& global = *it;
|
||||||
if (global->getUses().empty()) {
|
if (global->getUses().empty()) {
|
||||||
changed = true;
|
changed = true;
|
||||||
|
if(DEBUG){
|
||||||
|
std::cout << "=== Dead Global Found ===\n";
|
||||||
|
SysYPrinter::printValue(global.get());
|
||||||
|
}
|
||||||
it = globals.erase(it);
|
it = globals.erase(it);
|
||||||
} else {
|
} else {
|
||||||
++it;
|
++it;
|
||||||
@@ -207,6 +228,12 @@ void DeadCodeElimination::eliminateDeadRedundantLoadStore(Function* func, bool&
|
|||||||
// 可以优化直接把prevStorePointer的值存到nextStorePointer
|
// 可以优化直接把prevStorePointer的值存到nextStorePointer
|
||||||
changed = true;
|
changed = true;
|
||||||
nextStore->setOperand(0, prevStoreValue);
|
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);
|
usedelete(loadInst);
|
||||||
iter = instrs.erase(iter);
|
iter = instrs.erase(iter);
|
||||||
// 删除 prevStore 这里是不是可以留给删除无用store处理?
|
// 删除 prevStore 这里是不是可以留给删除无用store处理?
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include "IR.h"
|
#include "IR.h"
|
||||||
#include "SysYIRAnalyser.h"
|
#include "SysYIRAnalyser.h"
|
||||||
|
#include "SysYIRPrinter.h"
|
||||||
|
|
||||||
namespace sysy {
|
namespace sysy {
|
||||||
|
|
||||||
class DeadCodeElimination {
|
class DeadCodeElimination {
|
||||||
|
|||||||
@@ -15,15 +15,16 @@ public:
|
|||||||
public:
|
public:
|
||||||
void printIR();
|
void printIR();
|
||||||
void printGlobalVariable();
|
void printGlobalVariable();
|
||||||
void printFunction(Function *function);
|
|
||||||
void printInst(Instruction *pInst);
|
|
||||||
void printType(Type *type);
|
|
||||||
void printValue(Value *value);
|
|
||||||
|
|
||||||
public:
|
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);
|
static std::string getOperandName(Value *operand);
|
||||||
std::string getTypeString(Type *type);
|
static std::string getTypeString(Type *type);
|
||||||
std::string getValueName(Value *value);
|
static std::string getValueName(Value *value);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace sysy
|
} // namespace sysy
|
||||||
|
|||||||
Reference in New Issue
Block a user