[backend]微优化与可读性维护不影响原逻辑和分数

This commit is contained in:
2025-07-31 11:23:17 +08:00
parent b3cf3cba29
commit 807fb3f560
3 changed files with 49 additions and 55 deletions

View File

@ -12,6 +12,24 @@ std::string RISCv64CodeGen::code_gen() {
return module_gen();
}
void printInitializer(std::stringstream& ss, const ValueCounter& init_values) {
for (size_t i = 0; i < init_values.getValues().size(); ++i) {
auto val = init_values.getValues()[i];
auto count = init_values.getNumbers()[i];
if (auto constant = dynamic_cast<ConstantValue*>(val)) {
for (unsigned j = 0; j < count; ++j) {
if (constant->isInt()) {
ss << " .word " << constant->getInt() << "\n";
} else {
float f = constant->getFloat();
uint32_t float_bits = *(uint32_t*)&f;
ss << " .word " << float_bits << "\n";
}
}
}
}
}
std::string RISCv64CodeGen::module_gen() {
std::stringstream ss;
@ -65,22 +83,7 @@ std::string RISCv64CodeGen::module_gen() {
for (GlobalValue* global : data_globals) {
ss << ".globl " << global->getName() << "\n";
ss << global->getName() << ":\n";
const auto& init_values = global->getInitValues();
for (size_t i = 0; i < init_values.getValues().size(); ++i) {
auto val = init_values.getValues()[i];
auto count = init_values.getNumbers()[i];
if (auto constant = dynamic_cast<ConstantValue*>(val)) {
for (unsigned j = 0; j < count; ++j) {
if (constant->isInt()) {
ss << " .word " << constant->getInt() << "\n";
} else {
float f = constant->getFloat();
uint32_t float_bits = *(uint32_t*)&f;
ss << " .word " << float_bits << "\n";
}
}
}
}
printInitializer(ss, global->getInitValues());
}
// b. [新增] 再处理全局常量 (ConstantVariable)
@ -88,23 +91,7 @@ std::string RISCv64CodeGen::module_gen() {
ConstantVariable* cnst = const_ptr.get();
ss << ".globl " << cnst->getName() << "\n";
ss << cnst->getName() << ":\n";
const auto& init_values = cnst->getInitValues();
// 这部分逻辑和处理 GlobalValue 完全相同
for (size_t i = 0; i < init_values.getValues().size(); ++i) {
auto val = init_values.getValues()[i];
auto count = init_values.getNumbers()[i];
if (auto constant = dynamic_cast<ConstantValue*>(val)) {
for (unsigned j = 0; j < count; ++j) {
if (constant->isInt()) {
ss << " .word " << constant->getInt() << "\n";
} else {
float f = constant->getFloat();
uint32_t float_bits = *(uint32_t*)&f;
ss << " .word " << float_bits << "\n";
}
}
}
}
printInitializer(ss, cnst->getInitValues());
}
}

View File

@ -152,17 +152,18 @@ void RISCv64ISel::selectBasicBlock(BasicBlock* bb) {
for (const auto& inst_ptr : bb->getInstructions()) {
DAGNode* node_to_select = nullptr;
if (value_to_node.count(inst_ptr.get())) {
node_to_select = value_to_node.at(inst_ptr.get());
auto it = value_to_node.find(inst_ptr.get());
if (it != value_to_node.end()) {
node_to_select = it->second;
} else {
for(const auto& node : dag) {
if(node->value == inst_ptr.get()) {
node_to_select = node.get();
break;
}
for(const auto& node : dag) {
if(node->value == inst_ptr.get()) {
node_to_select = node.get();
break;
}
}
if(node_to_select) {
}
if(node_to_select) {
select_recursive(node_to_select);
}
}

View File

@ -398,13 +398,15 @@ void RISCv64RegAlloc::getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet&
}
} else { // [修复] CALL指令也可能定义物理寄存器如a0
if (first_reg_operand_is_def) {
if (preg_to_vreg_id_map.count(reg_op->getPReg())) {
def.insert(preg_to_vreg_id_map.at(reg_op->getPReg()));
}
auto it = preg_to_vreg_id_map.find(reg_op->getPReg());
if (it != preg_to_vreg_id_map.end()) {
def.insert(it->second);
}
first_reg_operand_is_def = false;
} else {
if (preg_to_vreg_id_map.count(reg_op->getPReg())) {
use.insert(preg_to_vreg_id_map.at(reg_op->getPReg()));
auto it = preg_to_vreg_id_map.find(reg_op->getPReg());
if (it != preg_to_vreg_id_map.end()) {
use.insert(it->second);
}
}
}
@ -444,9 +446,10 @@ void RISCv64RegAlloc::getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet&
if (reg_op->isVirtual()) {
def.insert(reg_op->getVRegNum());
} else { // 物理寄存器也可以是 Def
if (preg_to_vreg_id_map.count(reg_op->getPReg())) {
def.insert(preg_to_vreg_id_map.at(reg_op->getPReg()));
}
auto it = preg_to_vreg_id_map.find(reg_op->getPReg());
if (it != preg_to_vreg_id_map.end()) {
def.insert(it->second);
}
}
first_reg_is_def = false; // **关键**:处理完第一个寄存器后,立即更新标志
} else {
@ -454,8 +457,9 @@ void RISCv64RegAlloc::getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet&
if (reg_op->isVirtual()) {
use.insert(reg_op->getVRegNum());
} else { // 物理寄存器也可以是 Use
if (preg_to_vreg_id_map.count(reg_op->getPReg())) {
use.insert(preg_to_vreg_id_map.at(reg_op->getPReg()));
auto it = preg_to_vreg_id_map.find(reg_op->getPReg());
if (it != preg_to_vreg_id_map.end()) {
use.insert(it->second);
}
}
}
@ -467,8 +471,9 @@ void RISCv64RegAlloc::getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet&
use.insert(base_reg->getVRegNum());
} else {
PhysicalReg preg = base_reg->getPReg();
if (preg_to_vreg_id_map.count(preg)) {
use.insert(preg_to_vreg_id_map.at(preg));
auto it = preg_to_vreg_id_map.find(preg);
if (it != preg_to_vreg_id_map.end()) {
use.insert(it->second);
}
}
@ -480,8 +485,9 @@ void RISCv64RegAlloc::getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet&
if (src_reg_op->isVirtual()) {
use.insert(src_reg_op->getVRegNum());
} else {
if (preg_to_vreg_id_map.count(src_reg_op->getPReg())) {
use.insert(preg_to_vreg_id_map.at(src_reg_op->getPReg()));
auto it = preg_to_vreg_id_map.find(src_reg_op->getPReg());
if (it != preg_to_vreg_id_map.end()) {
use.insert(it->second);
}
}
}