Merge branch 'backend' into SCCP
This commit is contained in:
@@ -16,9 +16,10 @@ using namespace antlr4;
|
||||
#include "SysYIRCFGOpt.h"
|
||||
#include "RISCv64Backend.h"
|
||||
#include "SysYIRAnalyser.h"
|
||||
#include "DeadCodeElimination.h"
|
||||
#include "Mem2Reg.h"
|
||||
#include "Reg2Mem.h"
|
||||
// #include "DeadCodeElimination.h"
|
||||
#include "AddressCalculationExpansion.h"
|
||||
// #include "Mem2Reg.h"
|
||||
// #include "Reg2Mem.h"
|
||||
|
||||
using namespace sysy;
|
||||
|
||||
@@ -124,8 +125,12 @@ int main(int argc, char **argv) {
|
||||
// 无论最终输出是 IR 还是 ASM,只要不是停止在 AST 阶段,都会进入此优化流程。
|
||||
// optLevel = 0 时,执行默认优化。
|
||||
// optLevel >= 1 时,执行默认优化 + 额外的 -O1 优化。
|
||||
cout << "Applying middle-end optimizations (level -O" << optLevel << ")...\n";
|
||||
if (DEBUG) cout << "Applying middle-end optimizations (level -O" << optLevel << ")...\n";
|
||||
|
||||
// 设置 DEBUG 模式(如果指定了 'ird')
|
||||
if (argStopAfter == "ird") {
|
||||
DEBUG = 1; // 这里可能需要更精细地控制 DEBUG 的开启时机和范围
|
||||
}
|
||||
// 默认优化 pass (在所有优化级别都会执行)
|
||||
SysYCFGOpt cfgopt(moduleIR, builder);
|
||||
cfgopt.SysYOptimizateAfterIR();
|
||||
@@ -139,37 +144,22 @@ int main(int argc, char **argv) {
|
||||
cout << "=== After CFA & AVA (Default) ===\n";
|
||||
SysYPrinter(moduleIR).printIR(); // 临时打印器用于调试
|
||||
}
|
||||
|
||||
DeadCodeElimination dce(moduleIR, &cfa, &ava);
|
||||
dce.runDCEPipeline();
|
||||
if (DEBUG) {
|
||||
cout << "=== After 1st DCE (Default) ===\n";
|
||||
SysYPrinter(moduleIR).printIR();
|
||||
AddressCalculationExpansion ace(moduleIR, builder);
|
||||
if (ace.run()) {
|
||||
if (DEBUG) cout << "AddressCalculationExpansion made changes.\n";
|
||||
// 如果 ACE 改变了IR,并且 DEBUG 模式开启,可以考虑打印IR
|
||||
if (DEBUG) {
|
||||
cout << "=== After AddressCalculationExpansion ===\n";
|
||||
SysYPrinter(moduleIR).printIR();
|
||||
}
|
||||
} else {
|
||||
if (DEBUG) cout << "AddressCalculationExpansion made no changes.\n";
|
||||
}
|
||||
|
||||
Mem2Reg mem2reg(moduleIR, builder, &cfa, &ava);
|
||||
mem2reg.mem2regPipeline();
|
||||
if (DEBUG) {
|
||||
cout << "=== After Mem2Reg (Default) ===\n";
|
||||
SysYPrinter(moduleIR).printIR();
|
||||
}
|
||||
|
||||
Reg2Mem reg2mem(moduleIR, builder);
|
||||
reg2mem.DeletePhiInst();
|
||||
if (DEBUG) {
|
||||
cout << "=== After Reg2Mem (Default) ===\n";
|
||||
SysYPrinter(moduleIR).printIR();
|
||||
}
|
||||
|
||||
dce.runDCEPipeline(); // 第二次 DCE (默认)
|
||||
if (DEBUG) {
|
||||
cout << "=== After 2nd DCE (Default) ===\n";
|
||||
SysYPrinter(moduleIR).printIR();
|
||||
}
|
||||
|
||||
// 根据优化级别,执行额外的优化 pass
|
||||
if (optLevel >= 1) {
|
||||
cout << "Applying additional -O" << optLevel << " optimizations...\n";
|
||||
if (DEBUG) cout << "Applying additional -O" << optLevel << " optimizations...\n";
|
||||
// 放置 -O1 及其以上级别要启用的额外优化 pass
|
||||
// 例如:
|
||||
// MyNewOptimizationPass newOpt(moduleIR, builder);
|
||||
@@ -184,17 +174,39 @@ int main(int argc, char **argv) {
|
||||
// MyCustomOpt2 opt2_pass(moduleIR, builder, &cfa); // 假设需要CFA
|
||||
// opt2_pass.run();
|
||||
// ... 更多 -O1 特有的优化
|
||||
// DeadCodeElimination dce(moduleIR, &cfa, &ava);
|
||||
// dce.runDCEPipeline();
|
||||
// if (DEBUG) {
|
||||
// cout << "=== After 1st DCE (Default) ===\n";
|
||||
// SysYPrinter(moduleIR).printIR();
|
||||
// }
|
||||
|
||||
// Mem2Reg mem2reg(moduleIR, builder, &cfa, &ava);
|
||||
// mem2reg.mem2regPipeline();
|
||||
// if (DEBUG) {
|
||||
// cout << "=== After Mem2Reg (Default) ===\n";
|
||||
// SysYPrinter(moduleIR).printIR();
|
||||
// }
|
||||
|
||||
// Reg2Mem reg2mem(moduleIR, builder);
|
||||
// reg2mem.DeletePhiInst();
|
||||
// if (DEBUG) {
|
||||
// cout << "=== After Reg2Mem (Default) ===\n";
|
||||
// SysYPrinter(moduleIR).printIR();
|
||||
// }
|
||||
|
||||
// dce.runDCEPipeline(); // 第二次 DCE (默认)
|
||||
// if (DEBUG) {
|
||||
// cout << "=== After 2nd DCE (Default) ===\n";
|
||||
// SysYPrinter(moduleIR).printIR();
|
||||
// }
|
||||
} else {
|
||||
cout << "No additional middle-end optimizations applied for -O" << optLevel << ".\n";
|
||||
if (DEBUG) cout << "No additional middle-end optimizations applied for -O" << optLevel << ".\n";
|
||||
}
|
||||
|
||||
// 5. 根据 argStopAfter 决定后续操作
|
||||
// a) 如果指定停止在 IR 阶段,则打印最终 IR 并退出
|
||||
if (argStopAfter == "ir" || argStopAfter == "ird") {
|
||||
// 设置 DEBUG 模式(如果指定了 'ird')
|
||||
if (argStopAfter == "ird") {
|
||||
DEBUG = 1; // 这里可能需要更精细地控制 DEBUG 的开启时机和范围
|
||||
}
|
||||
// 打印最终 IR
|
||||
cout << "=== Final IR ===\n";
|
||||
SysYPrinter printer(moduleIR); // 在这里创建打印器,因为可能之前调试时用过临时打印器
|
||||
@@ -203,17 +215,16 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
// b) 如果未停止在 IR 阶段,则继续生成汇编 (后端)
|
||||
// 设置 DEBUG 模式(如果指定了 'asmd')
|
||||
if (argStopAfter == "asmd") {
|
||||
DEBUG = 1;
|
||||
// DEEPDEBUG = 1;
|
||||
}
|
||||
sysy::RISCv64CodeGen codegen(moduleIR); // 传入优化后的 moduleIR
|
||||
string asmCode = codegen.code_gen();
|
||||
|
||||
// 如果指定停止在 ASM 阶段,则打印/保存汇编并退出
|
||||
if (argStopAfter == "asm" || argStopAfter == "asmd") {
|
||||
// 设置 DEBUG 模式(如果指定了 'asmd')
|
||||
if (argStopAfter == "asmd") {
|
||||
DEBUG = 1;
|
||||
DEEPDEBUG = 1;
|
||||
}
|
||||
|
||||
if (!argOutputFilename.empty()) {
|
||||
ofstream fout(argOutputFilename);
|
||||
if (not fout.is_open()) {
|
||||
|
||||
Reference in New Issue
Block a user