[backend]将内联MEMSET函数的步长改为4字节

This commit is contained in:
Lixuanwang
2025-08-19 17:42:45 +08:00
parent b13d81353a
commit 7ada4c193f

View File

@ -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<RegOperand>(r_current_addr));
mv->addOperand(std::make_unique<RegOperand>(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);