x64: Only branch once in br_table (#5850)
This uses the `cmov`, which was previously necessary for Spectre mitigation, to clamp the table index instead of zeroing it. By then placing the default target as the last entry in the table, we can use just one branch instruction in all cases. Since there isn't a bounds-check branch any more, this sequence no longer needs Spectre mitigation. And since we don't need to be careful about preserving flags, half the instructions can be removed from this pseudoinstruction and emitted as regular instructions instead. This is a net savings of three bytes in the encoding of x64's br_table pseudoinstruction. The generated code can sometimes be longer overall because the blocks are emitted in a slightly different order. My benchmark results show a very small effect on runtime performance with this change. The spidermonkey benchmark in Sightglass runs "1.01x faster" than main by instructions retired, but with no significant difference in CPU cycles. I think that means it rarely hit the default case in any br_table instructions it executed. The pulldown-cmark benchmark in Sightglass runs "1.01x faster" than main by CPU cycles, but main runs "1.00x faster" by instructions retired. I think that means this benchmark hit the default case a significant amount of the time, so it executes a few more instructions per br_table, but maybe the branches were predicted better.
This commit is contained in:
@@ -4049,16 +4049,10 @@
|
||||
;; wasm-table index) and then 64-bits (address addend). The small
|
||||
;; lie about the I64 type is benign, since the temporary is dead
|
||||
;; after this instruction (and its Cranelift type is thus unused).
|
||||
(tmp2 WritableGpr (temp_writable_gpr))
|
||||
(tmp2 WritableGpr (temp_writable_gpr)))
|
||||
|
||||
(size OperandSize (raw_operand_size_of_type ty))
|
||||
|
||||
(jt_size u32 (jump_table_size jt_targets)))
|
||||
|
||||
(with_flags_side_effect
|
||||
(x64_cmp size (RegMemImm.Imm jt_size) idx)
|
||||
(ConsumesFlags.ConsumesFlagsSideEffect
|
||||
(MInst.JmpTableSeq idx tmp1 tmp2 default_target jt_targets)))))
|
||||
(SideEffectNoResult.Inst
|
||||
(MInst.JmpTableSeq idx tmp1 tmp2 default_target jt_targets))))
|
||||
|
||||
;;;; iadd_pairwise constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user