[backend]微优化与可读性维护不影响原逻辑和分数
This commit is contained in:
@@ -12,6 +12,24 @@ std::string RISCv64CodeGen::code_gen() {
|
|||||||
return module_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::string RISCv64CodeGen::module_gen() {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
@@ -65,22 +83,7 @@ std::string RISCv64CodeGen::module_gen() {
|
|||||||
for (GlobalValue* global : data_globals) {
|
for (GlobalValue* global : data_globals) {
|
||||||
ss << ".globl " << global->getName() << "\n";
|
ss << ".globl " << global->getName() << "\n";
|
||||||
ss << global->getName() << ":\n";
|
ss << global->getName() << ":\n";
|
||||||
const auto& init_values = global->getInitValues();
|
printInitializer(ss, 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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// b. [新增] 再处理全局常量 (ConstantVariable)
|
// b. [新增] 再处理全局常量 (ConstantVariable)
|
||||||
@@ -88,23 +91,7 @@ std::string RISCv64CodeGen::module_gen() {
|
|||||||
ConstantVariable* cnst = const_ptr.get();
|
ConstantVariable* cnst = const_ptr.get();
|
||||||
ss << ".globl " << cnst->getName() << "\n";
|
ss << ".globl " << cnst->getName() << "\n";
|
||||||
ss << cnst->getName() << ":\n";
|
ss << cnst->getName() << ":\n";
|
||||||
const auto& init_values = cnst->getInitValues();
|
printInitializer(ss, 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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -152,8 +152,9 @@ void RISCv64ISel::selectBasicBlock(BasicBlock* bb) {
|
|||||||
|
|
||||||
for (const auto& inst_ptr : bb->getInstructions()) {
|
for (const auto& inst_ptr : bb->getInstructions()) {
|
||||||
DAGNode* node_to_select = nullptr;
|
DAGNode* node_to_select = nullptr;
|
||||||
if (value_to_node.count(inst_ptr.get())) {
|
auto it = value_to_node.find(inst_ptr.get());
|
||||||
node_to_select = value_to_node.at(inst_ptr.get());
|
if (it != value_to_node.end()) {
|
||||||
|
node_to_select = it->second;
|
||||||
} else {
|
} else {
|
||||||
for(const auto& node : dag) {
|
for(const auto& node : dag) {
|
||||||
if(node->value == inst_ptr.get()) {
|
if(node->value == inst_ptr.get()) {
|
||||||
|
|||||||
@@ -398,13 +398,15 @@ void RISCv64RegAlloc::getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet&
|
|||||||
}
|
}
|
||||||
} else { // [修复] CALL指令也可能定义物理寄存器(如a0)
|
} else { // [修复] CALL指令也可能定义物理寄存器(如a0)
|
||||||
if (first_reg_operand_is_def) {
|
if (first_reg_operand_is_def) {
|
||||||
if (preg_to_vreg_id_map.count(reg_op->getPReg())) {
|
auto it = preg_to_vreg_id_map.find(reg_op->getPReg());
|
||||||
def.insert(preg_to_vreg_id_map.at(reg_op->getPReg()));
|
if (it != preg_to_vreg_id_map.end()) {
|
||||||
|
def.insert(it->second);
|
||||||
}
|
}
|
||||||
first_reg_operand_is_def = false;
|
first_reg_operand_is_def = false;
|
||||||
} else {
|
} else {
|
||||||
if (preg_to_vreg_id_map.count(reg_op->getPReg())) {
|
auto it = preg_to_vreg_id_map.find(reg_op->getPReg());
|
||||||
use.insert(preg_to_vreg_id_map.at(reg_op->getPReg()));
|
if (it != preg_to_vreg_id_map.end()) {
|
||||||
|
use.insert(it->second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -444,8 +446,9 @@ void RISCv64RegAlloc::getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet&
|
|||||||
if (reg_op->isVirtual()) {
|
if (reg_op->isVirtual()) {
|
||||||
def.insert(reg_op->getVRegNum());
|
def.insert(reg_op->getVRegNum());
|
||||||
} else { // 物理寄存器也可以是 Def
|
} else { // 物理寄存器也可以是 Def
|
||||||
if (preg_to_vreg_id_map.count(reg_op->getPReg())) {
|
auto it = preg_to_vreg_id_map.find(reg_op->getPReg());
|
||||||
def.insert(preg_to_vreg_id_map.at(reg_op->getPReg()));
|
if (it != preg_to_vreg_id_map.end()) {
|
||||||
|
def.insert(it->second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
first_reg_is_def = false; // **关键**:处理完第一个寄存器后,立即更新标志
|
first_reg_is_def = false; // **关键**:处理完第一个寄存器后,立即更新标志
|
||||||
@@ -454,8 +457,9 @@ void RISCv64RegAlloc::getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet&
|
|||||||
if (reg_op->isVirtual()) {
|
if (reg_op->isVirtual()) {
|
||||||
use.insert(reg_op->getVRegNum());
|
use.insert(reg_op->getVRegNum());
|
||||||
} else { // 物理寄存器也可以是 Use
|
} else { // 物理寄存器也可以是 Use
|
||||||
if (preg_to_vreg_id_map.count(reg_op->getPReg())) {
|
auto it = preg_to_vreg_id_map.find(reg_op->getPReg());
|
||||||
use.insert(preg_to_vreg_id_map.at(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());
|
use.insert(base_reg->getVRegNum());
|
||||||
} else {
|
} else {
|
||||||
PhysicalReg preg = base_reg->getPReg();
|
PhysicalReg preg = base_reg->getPReg();
|
||||||
if (preg_to_vreg_id_map.count(preg)) {
|
auto it = preg_to_vreg_id_map.find(preg);
|
||||||
use.insert(preg_to_vreg_id_map.at(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()) {
|
if (src_reg_op->isVirtual()) {
|
||||||
use.insert(src_reg_op->getVRegNum());
|
use.insert(src_reg_op->getVRegNum());
|
||||||
} else {
|
} else {
|
||||||
if (preg_to_vreg_id_map.count(src_reg_op->getPReg())) {
|
auto it = preg_to_vreg_id_map.find(src_reg_op->getPReg());
|
||||||
use.insert(preg_to_vreg_id_map.at(src_reg_op->getPReg()));
|
if (it != preg_to_vreg_id_map.end()) {
|
||||||
|
use.insert(it->second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user