[backend]解决了数组访存地址计算问题,加入了参数控制的中端、后端调试选项

This commit is contained in:
Lixuanwang
2025-07-15 11:32:53 +08:00
parent e576f0a21e
commit a509dabbf0
4 changed files with 144 additions and 38 deletions

View File

@@ -6,8 +6,6 @@
#include <iomanip>
#include <functional>
#define DEBUG 0
#define DEEPDEBUG 0
namespace sysy {
// 可用于分配的寄存器
@@ -390,48 +388,118 @@ std::vector<std::unique_ptr<RISCv64CodeGen::DAGNode>> RISCv64CodeGen::build_dag(
// 但它本身不应该有 result_vreg因为不映射到物理寄存器。
create_node(DAGNode::ALLOCA_ADDR, alloca, value_to_node, nodes_storage);
} else if (auto store = dynamic_cast<StoreInst*>(inst)) {
auto store_node = create_node(DAGNode::STORE, store, value_to_node, nodes_storage); // 调用成员函数版 create_node
auto store_node = create_node(DAGNode::STORE, store, value_to_node, nodes_storage);
// 获取要存储的值
DAGNode* val_node = get_operand_node(store->getValue(), value_to_node, nodes_storage); // 传递参数
DAGNode* val_node = get_operand_node(store->getValue(), value_to_node, nodes_storage);
// 获取内存位置的指针 (基地址)
Value* ptr_ir = store->getPointer();
DAGNode* ptr_node = get_operand_node(ptr_ir, value_to_node, nodes_storage); // 传递参数
DAGNode* ptr_node = get_operand_node(ptr_ir, value_to_node, nodes_storage);
store_node->operands.push_back(val_node);
store_node->operands.push_back(ptr_node);
val_node->users.push_back(store_node);
ptr_node->users.push_back(store_node);
if (store->getNumIndices())
if (DEBUG) std::cerr << "处理 StoreInst 的 indices: " << store->getNumIndices() << "\n"; // 调试输出
for (int i = 0; i < store->getNumIndices(); ++i) {
if (DEBUG) std::cerr << "处理 StoreInst 的 indices: " << i << "\n"; // 调试输出
Value* index_ir = store->getIndex(i);
DAGNode* index_node = get_operand_node(index_ir, value_to_node, nodes_storage); // 传递参数
store_node->operands.push_back(index_node);
index_node->users.push_back(store_node);
// === 修改开始:处理带索引的 StoreInst ===
if (store->getNumIndices() > 0) {
if (DEBUG) std::cerr << "处理带索引的 StoreInst: " << store->getNumIndices() << " 个索引\n";
// 假设只有一个索引
Value* index_ir = store->getIndex(0); // 获取索引 IR Value*
DAGNode* index_node = get_operand_node(index_ir, value_to_node, nodes_storage); // 索引 DAG 节点
// 1. 获取元素大小的 ConstantValue * (例如 4 字节)
// ConstantValue::get 返回裸指针,其生命周期由 IR 框架自身管理(假定是单例或池化)。
Value* const_4_value_ir = ConstantValue::get(4);
// 为这个常量创建一个 DAGNode
DAGNode* size_node = create_node(DAGNode::CONSTANT, const_4_value_ir, value_to_node, nodes_storage);
// 2. 创建一个 BINARY (MUL) 节点来计算字节偏移量 (index * element_size)
// BinaryInst 构造函数是 protected 的,需要通过静态工厂方法创建
Instruction* dummy_mul_inst_raw_ptr = BinaryInst::create(BinaryInst::kMul, Type::getIntType(), index_ir, const_4_value_ir, bb);
// 将所有权转移到成员变量 temp_instructions_storage
temp_instructions_storage.push_back(std::unique_ptr<Instruction>(dummy_mul_inst_raw_ptr)); // 存储临时的 Instruction
// 为这个新的 BinaryInst 创建一个 DAGNode它的类型是 DAGNode::BINARY
DAGNode* byte_offset_node = create_node(DAGNode::BINARY, dummy_mul_inst_raw_ptr, value_to_node, nodes_storage);
byte_offset_node->operands.push_back(index_node);
byte_offset_node->operands.push_back(size_node);
index_node->users.push_back(byte_offset_node);
size_node->users.push_back(byte_offset_node);
// 3. 创建一个 BINARY (ADD) 节点来计算最终地址 (base_address + byte_offset)
// 创建另一个临时的 BinaryInst。
Instruction* dummy_add_inst_raw_ptr = BinaryInst::create(BinaryInst::kAdd, Type::getIntType(), ptr_ir, dummy_mul_inst_raw_ptr, bb);
temp_instructions_storage.push_back(std::unique_ptr<Instruction>(dummy_add_inst_raw_ptr)); // 存储临时的 Instruction
// 为这个新的 BinaryInst 创建一个 DAGNode
DAGNode* final_addr_node = create_node(DAGNode::BINARY, dummy_add_inst_raw_ptr, value_to_node, nodes_storage);
final_addr_node->operands.push_back(ptr_node);
final_addr_node->operands.push_back(byte_offset_node);
ptr_node->users.push_back(final_addr_node);
byte_offset_node->users.push_back(final_addr_node);
// 现在STORE 节点的操作数是要存储的值和最终地址
store_node->operands.push_back(final_addr_node);
final_addr_node->users.push_back(store_node);
} else { // 原始的非索引 StoreInst 处理
store_node->operands.push_back(ptr_node);
ptr_node->users.push_back(store_node);
}
// === 修改结束 ===
} else if (auto load = dynamic_cast<LoadInst*>(inst)) {
auto load_node = create_node(DAGNode::LOAD, load, value_to_node, nodes_storage); // 调用成员函数版 create_node
auto load_node = create_node(DAGNode::LOAD, load, value_to_node, nodes_storage);
// 获取内存位置的指针 (基地址)
Value* ptr_ir = load->getPointer();
DAGNode* ptr_node = get_operand_node(ptr_ir, value_to_node, nodes_storage); // 传递参数
DAGNode* ptr_node = get_operand_node(ptr_ir, value_to_node, nodes_storage);
load_node->operands.push_back(ptr_node);
ptr_node->users.push_back(load_node);
// === 修改开始:处理带索引的 LoadInst ===
if (load->getNumIndices() > 0) {
// 假设只有一个索引
Value* index_ir = load->getIndex(0);
DAGNode* index_node = get_operand_node(index_ir, value_to_node, nodes_storage);
for (int i = 0; i < load->getNumIndices(); ++i) {
Value* index_ir = load->getIndex(i);
DAGNode* index_node = get_operand_node(index_ir, value_to_node, nodes_storage); // 传递参数
load_node->operands.push_back(index_node);
index_node->users.push_back(load_node);
// 1. 获取元素大小的 ConstantValue * (例如 4 字节)
Value* const_4_value_ir = ConstantValue::get(4);
DAGNode* size_node = create_node(DAGNode::CONSTANT, const_4_value_ir, value_to_node, nodes_storage);
// 2. 创建一个 BINARY (MUL) 节点来计算字节偏移量 (index * element_size)
Instruction* dummy_mul_inst_raw_ptr = BinaryInst::create(BinaryInst::kMul, Type::getIntType(), index_ir, const_4_value_ir, bb);
temp_instructions_storage.push_back(std::unique_ptr<Instruction>(dummy_mul_inst_raw_ptr)); // 存储临时的 Instruction
DAGNode* byte_offset_node = create_node(DAGNode::BINARY, dummy_mul_inst_raw_ptr, value_to_node, nodes_storage);
byte_offset_node->operands.push_back(index_node);
byte_offset_node->operands.push_back(size_node);
index_node->users.push_back(byte_offset_node);
size_node->users.push_back(byte_offset_node);
// 3. 创建一个 BINARY (ADD) 节点来计算最终地址 (base_address + byte_offset)
Instruction* dummy_add_inst_raw_ptr = BinaryInst::create(BinaryInst::kAdd, Type::getIntType(), ptr_ir, dummy_mul_inst_raw_ptr, bb);
temp_instructions_storage.push_back(std::unique_ptr<Instruction>(dummy_add_inst_raw_ptr)); // 存储临时的 Instruction
DAGNode* final_addr_node = create_node(DAGNode::BINARY, dummy_add_inst_raw_ptr, value_to_node, nodes_storage);
final_addr_node->operands.push_back(ptr_node);
final_addr_node->operands.push_back(byte_offset_node);
ptr_node->users.push_back(final_addr_node);
byte_offset_node->users.push_back(final_addr_node);
// 现在LOAD 节点的操作数是最终地址
load_node->operands.push_back(final_addr_node);
final_addr_node->users.push_back(load_node);
} else { // 原始的非索引 LoadInst 处理
load_node->operands.push_back(ptr_node);
ptr_node->users.push_back(load_node);
}
// 关联 load_node 的结果到 value_to_node
value_to_node[load] = load_node; // 这里的 value_to_node 是局部变量OK
// === 修改结束 ===
} else if (auto bin = dynamic_cast<BinaryInst*>(inst)) {
if (value_to_node.count(bin)) continue; // CSE