From ad5f35c1a0816a142caaab8a242836ca23a3ec20 Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Tue, 19 Aug 2025 08:56:51 +0800 Subject: [PATCH] =?UTF-8?q?[midend]=E6=9A=82=E6=97=B6=E4=BB=85=E7=94=A8?= =?UTF-8?q?=E4=BA=86=E9=AD=94=E6=95=B0=E4=BC=98=E5=8C=96=E9=99=A4=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E6=89=8B=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Pass/Optimize/GlobalStrengthReduction.cpp | 14 +++++----- .../Pass/Optimize/LoopStrengthReduction.cpp | 26 ++++++++++--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp b/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp index e8254a2..404cf7b 100644 --- a/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp +++ b/src/midend/Pass/Optimize/GlobalStrengthReduction.cpp @@ -671,13 +671,13 @@ bool GlobalStrengthReductionContext::reduceDivision(BinaryInst *inst) { } // x / c = x * magic_number (魔数乘法优化 - 使用libdivide算法) - if (isConstantInt(rhs, constVal) && constVal > 1 && constVal != (uint32_t)(-1)) { - // auto magicPair = computeMulhMagicNumbers(static_cast(constVal)); - Value* magicResult = createMagicDivisionLibdivide(inst, static_cast(constVal)); - replaceWithOptimized(inst, magicResult); - divisionOptCount++; - return true; - } + // if (isConstantInt(rhs, constVal) && constVal > 1 && constVal != (uint32_t)(-1)) { + // // auto magicPair = computeMulhMagicNumbers(static_cast(constVal)); + // Value* magicResult = createMagicDivisionLibdivide(inst, static_cast(constVal)); + // replaceWithOptimized(inst, magicResult); + // divisionOptCount++; + // return true; + // } return false; } diff --git a/src/midend/Pass/Optimize/LoopStrengthReduction.cpp b/src/midend/Pass/Optimize/LoopStrengthReduction.cpp index 0edbed4..33751df 100644 --- a/src/midend/Pass/Optimize/LoopStrengthReduction.cpp +++ b/src/midend/Pass/Optimize/LoopStrengthReduction.cpp @@ -661,9 +661,9 @@ bool StrengthReductionContext::replaceOriginalInstruction(StrengthReductionCandi case StrengthReductionCandidate::DIVIDE_CONST: { // 任意常数除法 - builder->setPosition(candidate->containingBlock, - candidate->containingBlock->findInstIterator(candidate->originalInst)); - replacementValue = generateConstantDivisionReplacement(candidate, builder); + // builder->setPosition(candidate->containingBlock, + // candidate->containingBlock->findInstIterator(candidate->originalInst)); + // replacementValue = generateConstantDivisionReplacement(candidate, builder); break; } @@ -683,17 +683,19 @@ bool StrengthReductionContext::replaceOriginalInstruction(StrengthReductionCandi ); // 检查原值是否为负数 - Value* zero = ConstantInteger::get(0); - Value* isNegative = builder->createICmpLTInst(candidate->inductionVar, zero); + Value* shift31condidata = builder->createBinaryInst( + Instruction::Kind::kSra, candidate->inductionVar->getType(), + candidate->inductionVar, ConstantInteger::get(31) + ); // 如果为负数,需要调整结果 - Value* adjustment = ConstantInteger::get(candidate->multiplier); - Value* adjustedTemp = builder->createAddInst(temp, adjustment); - - // 使用条件分支来模拟select操作 - // 为简化起见,这里先用一个更复杂但可工作的方式 - // 实际应该创建条件分支,但这里先简化处理 - replacementValue = temp; // 简化版本,假设大多数情况下不是负数 + Value* adjustment = builder->createAndInst(shift31condidata, maskConstant); + Value* adjustedTemp = builder->createAddInst(candidate->inductionVar, adjustment); + Value* adjustedResult = builder->createBinaryInst( + Instruction::Kind::kAnd, candidate->inductionVar->getType(), + adjustedTemp, maskConstant + ); + replacementValue = adjustedResult; } else { // 非负数的取模,直接使用位与 replacementValue = builder->createBinaryInst(