我需要减少代码中的分支数量。存在一个称为中位数的基准,其中包含一些代码,例如:
if ( A < B )
return A = foo[i];
else
return B = foo[i];
我在机器描述文件* .md中写了一个模式以避免分支:
(define_insn "smin<GPR:mode>3"
[
(set
(match_operand:GPR 0 "register_operand" "=r")
(if_then_else:GPR
(lt:GPR
(match_operand:GPR 1 "register_operand" " r")
(match_operand:GPR 2 "register_operand" " r"))
(match_dup 1)
(match_dup 2)))
]
""
"min\t%0,%1,%2"
[(set_attr "type" "move")
(set_attr "mode" "<MODE>")])
在简单比较的情况下有效:
if ( A < B )
return A ;
else
return B;
GCC发射:
min a0,a0,a1 # 9 smindi3 [length = 4]
ret # 21 simple_return [length = 4]
但是如果我尝试相同的操作,但是使用了索引变量(数组):它将无法正常工作:
if ( A < B )
return A = foo[i];
else
return B = foo[i];
GCC发射:
blt a0,a1,.L5 # 11 *branch_orderdi [length = 4]
sd a1,8(a2) # 18 *movdi_64bit/4 [length = 4]
mv a0,a1 # 8 *movdi_64bit/1 [length = 4]
ret # 34 simple_return [length = 4]
.L5:
sd a0,8(a2) # 13 *movdi_64bit/4 [length = 4]
ret # 28 simple_return [length = 4]
我需要GCC发出这样的内容:
min a0,a0,a1 # 9 smindi3 [length = 4]
sd a0,8(a2) # 18 *movdi_64bit/4 [length = 4]
ret # 34 simple_return [length = 4]
感谢您的帮助。
答案 0 :(得分:3)
if ( A < B ) return A = foo[i]; else return B = foo[i]
嗯?假设它在名为fx()
的函数中,为什么不
if (A < B) A = fx(); else B = fx();
并简化fx()
,在此过程中放弃使用全局变量A
和B
,以
return foo[i];