Skip to content

Commit e3167ea

Browse files
committed
移除 ModMulIdiom pass 及其专用指令 MulModInst
该惯用法识别优化(递归倍加模乘 → 单宽乘取模)缺乏可描述的合理优化 动机,删除该 pass 并取消其在 PassManager 中的注册。 ModMulIdiom 是 MulModInst 的唯一生产者,pass 删除后该指令成为永不实例 化的死代码,故一并清理:删除 MulModInst.{h,cpp}、Instruction.h 中的 IRINST_OP_MULMOD_I 枚举、后端 InstSelectorRiscV64 的 translate_mulmod 及其处理器注册、CMakeLists 相关条目。
1 parent 2a1ec3d commit e3167ea

9 files changed

Lines changed: 0 additions & 479 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ set(IR_SRCS
182182
ir/Instructions/StoreInst.cpp
183183
ir/Instructions/BinaryInst.h
184184
ir/Instructions/BinaryInst.cpp
185-
ir/Instructions/MulModInst.h
186-
ir/Instructions/MulModInst.cpp
187185
ir/Instructions/FCmpInst.h
188186
ir/Instructions/FCmpInst.cpp
189187
ir/Instructions/ICmpInst.h
@@ -301,8 +299,6 @@ set(IR_SRCS
301299
ir/passes/modulePass/SmallFunctionInline.cpp
302300
ir/passes/functionPass/TailRecursionElim.h
303301
ir/passes/functionPass/TailRecursionElim.cpp
304-
ir/passes/functionPass/ModMulIdiom.h
305-
ir/passes/functionPass/ModMulIdiom.cpp
306302
ir/passes/functionPass/LateLoopCFGCleanup.h
307303
ir/passes/functionPass/LateLoopCFGCleanup.cpp
308304
ir/passes/functionPass/PhiLowering.h

backend/riscv64/InstSelectorRiscV64.cpp

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "AllocaInst.h"
2525
#include "BasicBlock.h"
2626
#include "BinaryInst.h"
27-
#include "MulModInst.h"
2827
#include "BranchInst.h"
2928
#include "CallInst.h"
3029
#include "ConstFloat.h"
@@ -505,7 +504,6 @@ InstSelectorRiscV64::InstSelectorRiscV64(
505504
translatorHandlers[IRInstOperator::IRINST_OP_MUL_I] = &InstSelectorRiscV64::translate_mul;
506505
translatorHandlers[IRInstOperator::IRINST_OP_DIV_I] = &InstSelectorRiscV64::translate_div;
507506
translatorHandlers[IRInstOperator::IRINST_OP_MOD_I] = &InstSelectorRiscV64::translate_mod;
508-
translatorHandlers[IRInstOperator::IRINST_OP_MULMOD_I] = &InstSelectorRiscV64::translate_mulmod;
509507
translatorHandlers[IRInstOperator::IRINST_OP_SHL_I] = &InstSelectorRiscV64::translate_shl;
510508
translatorHandlers[IRInstOperator::IRINST_OP_ASHR_I] = &InstSelectorRiscV64::translate_ashr;
511509
translatorHandlers[IRInstOperator::IRINST_OP_LSHR_I] = &InstSelectorRiscV64::translate_lshr;
@@ -1412,51 +1410,6 @@ void InstSelectorRiscV64::translate_mod(Instruction * inst)
14121410
translate_binary(inst, "remw");
14131411
}
14141412

1415-
/// @brief 翻译宽乘取模指令 (i64)a*b % m
1416-
///
1417-
/// 两个 i32 操作数以有符号扩展形式驻留 64 位寄存器,用 64 位 mul 得到精确的
1418-
/// 64 位积(调用点守卫保证 0<=a<m、b>=0,积非负且 < 2^61 不溢出),再对正常量
1419-
/// 取有符号 64 位余数。余数落在 [0, m) 内,可直接当作已符号扩展的 i32 使用
1420-
void InstSelectorRiscV64::translate_mulmod(Instruction * inst)
1421-
{
1422-
auto * mulmod = dynamic_cast<MulModInst *>(inst);
1423-
if (mulmod == nullptr) {
1424-
return;
1425-
}
1426-
const int32_t modulus = mulmod->getModulus();
1427-
1428-
int dstReg = getResultReg(inst);
1429-
LocalTempManager::Lease dstLease;
1430-
if (dstReg < 0) {
1431-
dstLease = tempMgr.borrow(inst);
1432-
dstReg = dstLease.reg();
1433-
}
1434-
1435-
OperandReg lhs = loadOperand(mulmod->getA(), inst, dstReg);
1436-
const int rhsPreferredReg = lhs.reg != dstReg ? dstReg : -1;
1437-
OperandReg rhs = loadOperand(mulmod->getB(), inst, rhsPreferredReg < 0 ? dstReg : -1, rhsPreferredReg);
1438-
1439-
// 64 位无截断乘法
1440-
iloc.inst("mul",
1441-
PlatformRiscV64::regName[dstReg],
1442-
PlatformRiscV64::regName[lhs.reg],
1443-
PlatformRiscV64::regName[rhs.reg]);
1444-
1445-
releaseOperand(rhs);
1446-
releaseOperand(lhs);
1447-
1448-
// 对常量取 64 位有符号余数
1449-
auto modTmp = tempMgr.borrowExcluding(inst, {dstReg});
1450-
iloc.load_imm(modTmp.reg(), modulus);
1451-
iloc.inst("rem",
1452-
PlatformRiscV64::regName[dstReg],
1453-
PlatformRiscV64::regName[dstReg],
1454-
PlatformRiscV64::regName[modTmp.reg()]);
1455-
modTmp.release();
1456-
1457-
storeResult(inst, dstReg, inst);
1458-
}
1459-
14601413
/// @brief 翻译逻辑左移指令(shl)
14611414
void InstSelectorRiscV64::translate_shl(Instruction * inst)
14621415
{

backend/riscv64/InstSelectorRiscV64.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ class InstSelectorRiscV64 {
130130
void translate_div(Instruction * inst);
131131
/// @brief 翻译mod指令(取模)
132132
void translate_mod(Instruction * inst);
133-
/// @brief 翻译宽乘取模指令((i64)a*b % m,64 位无截断乘后对常量取模)
134-
void translate_mulmod(Instruction * inst);
135133
/// @brief 翻译逻辑左移指令(shl)
136134
void translate_shl(Instruction * inst);
137135
/// @brief 翻译算术右移指令(ashr,保留符号位)

ir/Instruction.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ enum class IRInstOperator : std::int8_t {
1919
IRINST_OP_MUL_I,
2020
IRINST_OP_DIV_I,
2121
IRINST_OP_MOD_I,
22-
IRINST_OP_MULMOD_I, ///< 64 位宽乘后对常量取模:(i64)a * (i64)b % m,结果为 i32
2322
IRINST_OP_SHL_I, ///< 逻辑左移
2423
IRINST_OP_ASHR_I, ///< 算术右移(保留符号位)
2524
IRINST_OP_LSHR_I, ///< 逻辑右移(高位补 0)

ir/Instructions/MulModInst.cpp

Lines changed: 0 additions & 47 deletions
This file was deleted.

ir/Instructions/MulModInst.h

Lines changed: 0 additions & 46 deletions
This file was deleted.

ir/passes/PassManager.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "functionPass/LateLoopCFGCleanup.h"
3737
#include "functionPass/LoopRotate.h"
3838
#include "functionPass/Mem2Reg.h"
39-
#include "functionPass/ModMulIdiom.h"
4039
#include "functionPass/PhiToSelect.h"
4140
#include "functionPass/PhiLowering.h"
4241
#include "functionPass/PureCallCSE.h"
@@ -114,13 +113,6 @@ void PassManager::registerDefaultOptimizationPipeline(int32_t optLevel, bool ena
114113
return false;
115114
});
116115

117-
// 递归倍加模乘惯用法识别:须在 Mem2Reg 后(依赖 SSA 分支形态)、
118-
// GVN/InstCombine 前(避免 srem/sdiv 被变形破坏匹配)
119-
registerFunctionPass("ModMulIdiom", [this](Function * func) {
120-
ModMulIdiom pass(func, module);
121-
return pass.run();
122-
});
123-
124116
registerFunctionPass("GVN", [this](Function * func) {
125117
GVN pass(func, module);
126118
return pass.run();

0 commit comments

Comments
 (0)