cranelift_codegen/machinst/
helpers.rs

1//! Miscellaneous helpers for machine backends.
2
3use crate::ir::Type;
4use std::ops::{Add, BitAnd, Not, Sub};
5
6/// Returns the size (in bits) of a given type.
7pub fn ty_bits(ty: Type) -> usize {
8    ty.bits() as usize
9}
10
11/// Is the type represented by an integer (not float) at the machine level?
12pub(crate) fn ty_has_int_representation(ty: Type) -> bool {
13    ty.is_int() || ty.is_ref()
14}
15
16/// Is the type represented by a float or vector value at the machine level?
17pub(crate) fn ty_has_float_or_vec_representation(ty: Type) -> bool {
18    ty.is_vector() || ty.is_float()
19}
20
21/// Align a size up to a power-of-two alignment.
22pub(crate) fn align_to<N>(x: N, alignment: N) -> N
23where
24    N: Not<Output = N>
25        + BitAnd<N, Output = N>
26        + Add<N, Output = N>
27        + Sub<N, Output = N>
28        + From<u8>
29        + Copy,
30{
31    let alignment_mask = alignment - 1.into();
32    (x + alignment_mask) & !alignment_mask
33}