From 7ada4c193fe1e57178041a50b457c07003339a45 Mon Sep 17 00:00:00 2001 From: Lixuanwang Date: Tue, 19 Aug 2025 17:42:45 +0800 Subject: [PATCH] =?UTF-8?q?[backend]=E5=B0=86=E5=86=85=E8=81=94MEMSET?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=9A=84=E6=AD=A5=E9=95=BF=E6=94=B9=E4=B8=BA?= =?UTF-8?q?4=E5=AD=97=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/RISCv64/RISCv64ISel.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/backend/RISCv64/RISCv64ISel.cpp b/src/backend/RISCv64/RISCv64ISel.cpp index a0ad2f1..a3ba19d 100644 --- a/src/backend/RISCv64/RISCv64ISel.cpp +++ b/src/backend/RISCv64/RISCv64ISel.cpp @@ -1215,15 +1215,13 @@ void RISCv64ISel::selectNode(DAGNode* node) { std::string loop_end_label = MFunc->getName() + "_memset_loop_end_" + std::to_string(unique_id); std::string remainder_label = MFunc->getName() + "_memset_remainder_" + std::to_string(unique_id); std::string done_label = MFunc->getName() + "_memset_done_" + std::to_string(unique_id); - - // 构造64位的填充值 - addi_instr(RVOpcodes::ANDI, r_temp_val, r_value_byte, 255); - addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 8); - add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte); - addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 16); - add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte); - addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 32); - add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte); + + // 构造32位的填充值 (将一个字节复制4次) + addi_instr(RVOpcodes::ANDI, r_temp_val, r_value_byte, 255); // 提取低8位: 000000XX + addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 8); // 左移8位: 0000XX00 + add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte); // 合并得到: 0000XXXX + addi_instr(RVOpcodes::SLLI, r_value_byte, r_temp_val, 16); // 左移16位: XXXX0000 + add_instr(RVOpcodes::OR, r_temp_val, r_temp_val, r_value_byte); // 合并得到完整的32位值: XXXXXXXX // 计算循环边界 add_instr(RVOpcodes::ADD, r_end_addr, r_dest_addr, r_num_bytes); @@ -1231,16 +1229,18 @@ void RISCv64ISel::selectNode(DAGNode* node) { mv->addOperand(std::make_unique(r_current_addr)); mv->addOperand(std::make_unique(r_dest_addr)); CurMBB->addInstruction(std::move(mv)); - addi_instr(RVOpcodes::ANDI, r_counter, r_num_bytes, -8); + // 计算主循环部分的总字节数 (向下舍入到4的倍数) + addi_instr(RVOpcodes::ANDI, r_counter, r_num_bytes, -4); + // 计算主循环的结束地址 add_instr(RVOpcodes::ADD, r_counter, r_dest_addr, r_counter); - // 8字节主循环 + // 4字节主循环 label_instr(loop_start_label); branch_instr(RVOpcodes::BGEU, r_current_addr, r_counter, loop_end_label); - store_instr(RVOpcodes::SD, r_temp_val, r_current_addr, 0); - addi_instr(RVOpcodes::ADDI, r_current_addr, r_current_addr, 8); + store_instr(RVOpcodes::SW, r_temp_val, r_current_addr, 0); // 使用 sw (存储字) + addi_instr(RVOpcodes::ADDI, r_current_addr, r_current_addr, 4); // 步长改为4 jump_instr(loop_start_label); - + // 1字节收尾循环 label_instr(loop_end_label); label_instr(remainder_label);