From f76a9d436e9f0e153ca705b78afdc7cb4b002ce4 Mon Sep 17 00:00:00 2001 From: Kasey Carrothers Date: Wed, 27 Jan 2021 22:45:35 -0800 Subject: [PATCH] Clean up handling of NOPs in the x64 backend. 1. Restricts max nop size to 15 instead of 16. 2. Fixes an edge case where gen_nop() would return a zero sized intruction on multiples of 16. 3. Clarifies the documentation of the gen_nop interface to state that returning zero is allowed when preferred_size is zero. --- cranelift/codegen/src/isa/x64/inst/mod.rs | 6 +++--- cranelift/codegen/src/machinst/mod.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cranelift/codegen/src/isa/x64/inst/mod.rs b/cranelift/codegen/src/isa/x64/inst/mod.rs index bab28f2aa098..44cedb06ca0f 100644 --- a/cranelift/codegen/src/isa/x64/inst/mod.rs +++ b/cranelift/codegen/src/isa/x64/inst/mod.rs @@ -566,7 +566,7 @@ impl Inst { impl Inst { pub(crate) fn nop(len: u8) -> Self { - debug_assert!(len <= 16); + debug_assert!(len <= 15); Self::Nop { len } } @@ -2594,11 +2594,11 @@ impl MachInst for Inst { } fn gen_zero_len_nop() -> Inst { - Inst::Nop { len: 0 } + Inst::nop(0) } fn gen_nop(preferred_size: usize) -> Inst { - Inst::nop((preferred_size % 16) as u8) + Inst::nop(std::cmp::min(preferred_size, 15) as u8) } fn maybe_direct_reload(&self, _reg: VirtualReg, _slot: SpillSlot) -> Option { diff --git a/cranelift/codegen/src/machinst/mod.rs b/cranelift/codegen/src/machinst/mod.rs index 297d53195594..d65fd9b3cabf 100644 --- a/cranelift/codegen/src/machinst/mod.rs +++ b/cranelift/codegen/src/machinst/mod.rs @@ -178,7 +178,7 @@ pub trait MachInst: Clone + Debug { /// request a NOP of that size, or as close to it as possible. The machine /// backend may return a NOP whose binary encoding is smaller than the /// preferred size, but must not return a NOP that is larger. However, - /// the instruction must have a nonzero size. + /// the instruction must have a nonzero size if preferred_size is nonzero. fn gen_nop(preferred_size: usize) -> Self; /// Get the register universe for this backend.