From a1cca3c95a5e3a0f05cc4563a474f5911daaa389 Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Mon, 11 Aug 2025 19:10:11 +0800 Subject: [PATCH] =?UTF-8?q?[midend-llvmirprint]=E4=BF=AE=E6=94=B9i1?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E4=B8=8D=E5=AD=98=E5=9C=A8=E5=BC=95=E5=85=A5?= =?UTF-8?q?=E7=9A=84=E4=B8=B4=E6=97=B6=E5=AF=84=E5=AD=98=E5=99=A8=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E6=AF=94=E8=BE=83=E7=BB=93=E6=9E=9C=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C=E5=87=8F=E5=B0=91?= =?UTF-8?q?=E5=86=B2=E7=AA=81=E7=9A=84=E5=8F=AF=E8=83=BD=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/midend/IR.cpp | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/midend/IR.cpp b/src/midend/IR.cpp index b301d82..7879816 100644 --- a/src/midend/IR.cpp +++ b/src/midend/IR.cpp @@ -752,8 +752,12 @@ void BinaryInst::print(std::ostream &os) const { if (kind == kICmpEQ || kind == kICmpNE || kind == kICmpLT || kind == kICmpGT || kind == kICmpLE || kind == kICmpGE) { // 整数比较指令 - 生成临时i1结果然后转换为i32 - static int tmpCmpCounter = 0; - std::string tmpName = "tmp_icmp_" + std::to_string(++tmpCmpCounter); + // 使用指令地址和操作数地址的组合哈希来确保唯一性 + auto inst_hash = std::hash{}(static_cast(this)); + auto lhs_hash = std::hash{}(static_cast(getLhs())); + auto rhs_hash = std::hash{}(static_cast(getRhs())); + size_t combined_hash = inst_hash ^ (lhs_hash << 1) ^ (rhs_hash << 2); + std::string tmpName = "tmp_icmp_" + std::to_string(combined_hash % 1000000); os << "%" << tmpName << " = " << getKindString() << " " << *getLhs()->getType() << " "; printOperand(os, getLhs()); os << ", "; @@ -763,8 +767,12 @@ void BinaryInst::print(std::ostream &os) const { } else if (kind == kFCmpEQ || kind == kFCmpNE || kind == kFCmpLT || kind == kFCmpGT || kind == kFCmpLE || kind == kFCmpGE) { // 浮点比较指令 - 生成临时i1结果然后转换为i32 - static int tmpCmpCounter = 0; - std::string tmpName = "tmp_fcmp_" + std::to_string(++tmpCmpCounter); + // 使用指令地址和操作数地址的组合哈希来确保唯一性 + auto inst_hash = std::hash{}(static_cast(this)); + auto lhs_hash = std::hash{}(static_cast(getLhs())); + auto rhs_hash = std::hash{}(static_cast(getRhs())); + size_t combined_hash = inst_hash ^ (lhs_hash << 1) ^ (rhs_hash << 2); + std::string tmpName = "tmp_fcmp_" + std::to_string(combined_hash % 1000000); os << "%" << tmpName << " = " << getKindString() << " " << *getLhs()->getType() << " "; printOperand(os, getLhs()); os << ", "; @@ -799,15 +807,25 @@ void UncondBrInst::print(std::ostream &os) const { void CondBrInst::print(std::ostream &os) const { Value* condition = getCondition(); - // 由于所有条件都是i32类型(包括比较结果),统一转换为i1 - static int tmpCondCounter = 0; + // 使用多个因素的组合哈希来确保唯一性 std::string condName = condition->getName(); if (condName.empty()) { - condName = "const" + std::to_string(++tmpCondCounter); + // 使用条件值地址的哈希值作为唯一标识 + auto ptr_hash = std::hash{}(static_cast(condition)); + condName = "const_" + std::to_string(ptr_hash % 100000); } - os << "%tmp_cond_" << condName << " = icmp ne i32 "; + + // 组合指令地址、条件地址和目标块地址的哈希来确保唯一性 + auto inst_hash = std::hash{}(static_cast(this)); + auto cond_hash = std::hash{}(static_cast(condition)); + auto then_hash = std::hash{}(static_cast(getThenBlock())); + auto else_hash = std::hash{}(static_cast(getElseBlock())); + size_t combined_hash = inst_hash ^ (cond_hash << 1) ^ (then_hash << 2) ^ (else_hash << 3); + std::string uniqueSuffix = std::to_string(combined_hash % 1000000); + + os << "%tmp_cond_" << condName << "_" << uniqueSuffix << " = icmp ne i32 "; printOperand(os, condition); - os << ", 0\n br i1 %tmp_cond_" << condName; + os << ", 0\n br i1 %tmp_cond_" << condName << "_" << uniqueSuffix; os << ", label %"; printBlockName(os, getThenBlock());