gpt4 book ai didi

c - 为什么 GCC 会根据常量的值生成不同的乘法操作码?

转载 作者:太空狗 更新时间:2023-10-29 16:36:18 25 4
gpt4 key购买 nike

我在 gcc.godbolt.org 上玩 GCC 反汇编程序我注意到从 4.6 版开始的 GCC 以不同的方式编译乘法。我有以下两个功能:

unsigned m126(unsigned i)
{
return i * 126;
}

unsigned m131(unsigned i)
{
return i * 131;
}

m126 编译成:

mov eax, edi
mov edx, 126
imul eax, edx
ret

m131编译成:

imul eax, edi, 131
ret

为什么不同? GCC 4.5 在这两种情况下生成相同的操作码。

A link to the actual example on GCC Explorer .

最佳答案

gcc/config/i386/i386.md 中找到这个(见顶部的评论):

;; imul $8/16bit_imm, regmem, reg is vector decoded.
;; Convert it into imul reg, reg
;; It would be better to force assembler to encode instruction using long
;; immediate, but there is apparently no way to do so.
(define_peephole2
[(parallel [(set (match_operand:SWI248 0 "register_operand")
(mult:SWI248
(match_operand:SWI248 1 "nonimmediate_operand")
(match_operand:SWI248 2 "const_int_operand")))
(clobber (reg:CC FLAGS_REG))])
(match_scratch:SWI248 3 "r")]
"TARGET_SLOW_IMUL_IMM8 && optimize_insn_for_speed_p ()
&& satisfies_constraint_K (operands[2])"
[(set (match_dup 3) (match_dup 2))
(parallel [(set (match_dup 0) (mult:SWI248 (match_dup 0) (match_dup 3)))
(clobber (reg:CC FLAGS_REG))])]
{
if (!rtx_equal_p (operands[0], operands[1]))
emit_move_insn (operands[0], operands[1]);
})

好像跟指令解码有关(不好意思我不是专家)

关于c - 为什么 GCC 会根据常量的值生成不同的乘法操作码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13976904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com