cranelift x64: use the POPCNT instruction for Popcount when it's available;

This commit is contained in:
Benjamin Bouvier
2021-01-28 12:16:46 +01:00
parent 6bf6612d96
commit 2275519cb1
4 changed files with 102 additions and 12 deletions

View File

@@ -406,6 +406,8 @@ pub enum UnaryRmROpcode {
Lzcnt,
/// Counts trailing zeroes (Trailing Zero CouNT).
Tzcnt,
/// Counts the number of ones (POPulation CouNT).
Popcnt,
}
impl fmt::Debug for UnaryRmROpcode {
@@ -415,6 +417,7 @@ impl fmt::Debug for UnaryRmROpcode {
UnaryRmROpcode::Bsf => write!(fmt, "bsf"),
UnaryRmROpcode::Lzcnt => write!(fmt, "lzcnt"),
UnaryRmROpcode::Tzcnt => write!(fmt, "tzcnt"),
UnaryRmROpcode::Popcnt => write!(fmt, "popcnt"),
}
}
}

View File

@@ -677,23 +677,25 @@ pub(crate) fn emit(
_ => unreachable!(),
};
use UnaryRmROpcode::*;
let prefix = match size {
2 => match op {
UnaryRmROpcode::Bsr | UnaryRmROpcode::Bsf => LegacyPrefixes::_66,
UnaryRmROpcode::Lzcnt | UnaryRmROpcode::Tzcnt => LegacyPrefixes::_66F3,
Bsr | Bsf => LegacyPrefixes::_66,
Lzcnt | Tzcnt | Popcnt => LegacyPrefixes::_66F3,
},
4 | 8 => match op {
UnaryRmROpcode::Bsr | UnaryRmROpcode::Bsf => LegacyPrefixes::None,
UnaryRmROpcode::Lzcnt | UnaryRmROpcode::Tzcnt => LegacyPrefixes::_F3,
Bsr | Bsf => LegacyPrefixes::None,
Lzcnt | Tzcnt | Popcnt => LegacyPrefixes::_F3,
},
_ => unreachable!(),
};
let (opcode, num_opcodes) = match op {
UnaryRmROpcode::Bsr => (0x0fbd, 2),
UnaryRmROpcode::Bsf => (0x0fbc, 2),
UnaryRmROpcode::Lzcnt => (0x0fbd, 2),
UnaryRmROpcode::Tzcnt => (0x0fbc, 2),
Bsr => (0x0fbd, 2),
Bsf => (0x0fbc, 2),
Lzcnt => (0x0fbd, 2),
Tzcnt => (0x0fbc, 2),
Popcnt => (0x0fb8, 2),
};
match src {