[backend]删除了后端对数组访存的地址展开,因为已经在IR中实现

This commit is contained in:
Lixuanwang
2025-07-18 20:48:59 +08:00
parent 3657c08644
commit e8660120cc

View File

@@ -399,108 +399,16 @@ std::vector<std::unique_ptr<RISCv64CodeGen::DAGNode>> RISCv64CodeGen::build_dag(
DAGNode* ptr_node = get_operand_node(ptr_ir, value_to_node, nodes_storage);
store_node->operands.push_back(val_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);
}
// === 修改结束 ===
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);
// 获取内存位置的指针 (基地址)
Value* ptr_ir = load->getPointer();
DAGNode* ptr_node = get_operand_node(ptr_ir, value_to_node, nodes_storage);
// === 修改开始:处理带索引的 LoadInst ===
if (load->getNumIndices() > 0) {
// 假设只有一个索引
Value* index_ir = load->getIndex(0);
DAGNode* index_node = get_operand_node(index_ir, value_to_node, nodes_storage);
// 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->operands.push_back(ptr_node);
ptr_node->users.push_back(load_node);
} else if (auto bin = dynamic_cast<BinaryInst*>(inst)) {
if (value_to_node.count(bin)) continue; // CSE