deploy-20250820-3 #1
@@ -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<int>(constVal));
|
||||
Value* magicResult = createMagicDivisionLibdivide(inst, static_cast<int>(constVal));
|
||||
replaceWithOptimized(inst, magicResult);
|
||||
divisionOptCount++;
|
||||
return true;
|
||||
}
|
||||
// if (isConstantInt(rhs, constVal) && constVal > 1 && constVal != (uint32_t)(-1)) {
|
||||
// // auto magicPair = computeMulhMagicNumbers(static_cast<int>(constVal));
|
||||
// Value* magicResult = createMagicDivisionLibdivide(inst, static_cast<int>(constVal));
|
||||
// replaceWithOptimized(inst, magicResult);
|
||||
// divisionOptCount++;
|
||||
// return true;
|
||||
// }
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user