Skip to main content

cranelift_codegen/ir/
instructions.rs

1//! Instruction formats and opcodes.
2//!
3//! The `instructions` module contains definitions for instruction formats, opcodes, and the
4//! in-memory representation of IR instructions.
5//!
6//! A large part of this module is auto-generated from the instruction descriptions in the meta
7//! directory.
8
9use crate::constant_hash::Table;
10use alloc::vec::Vec;
11use core::fmt::{self, Display, Formatter};
12use core::ops::{Deref, DerefMut};
13use core::str::FromStr;
14
15#[cfg(feature = "enable-serde")]
16use serde_derive::{Deserialize, Serialize};
17
18use crate::bitset::ScalarBitSet;
19use crate::entity;
20use crate::ir::{
21    self, Block, ExceptionTable, ExceptionTables, FuncRef, MemFlags, SigRef, StackSlot, Type,
22    Value,
23    condcodes::{FloatCC, IntCC},
24    trapcode::TrapCode,
25    types,
26};
27
28/// Some instructions use an external list of argument values because there is not enough space in
29/// the 16-byte `InstructionData` struct. These value lists are stored in a memory pool in
30/// `dfg.value_lists`.
31pub type ValueList = entity::EntityList<Value>;
32
33/// Memory pool for holding value lists. See `ValueList`.
34pub type ValueListPool = entity::ListPool<Value>;
35
36/// A pair of a Block and its arguments, stored in a single EntityList internally.
37///
38/// Block arguments are semantically a `BlockArg`.
39///
40/// NOTE: We don't expose either value_to_block or block_to_value outside of this module because
41/// this operation is not generally safe. However, as the two share the same underlying layout,
42/// they can be stored in the same value pool.
43///
44/// BlockCall makes use of this shared layout by storing all of its contents (a block and its
45/// argument) in a single EntityList. This is a bit better than introducing a new entity type for
46/// the pair of a block name and the arguments entity list, as we don't pay any indirection penalty
47/// to get to the argument values -- they're stored in-line with the block in the same list.
48///
49/// The BlockCall::new function guarantees this layout by requiring a block argument that's written
50/// in as the first element of the EntityList. Any subsequent entries are always assumed to be real
51/// Values.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
53#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
54pub struct BlockCall {
55    /// The underlying storage for the BlockCall. The first element of the values EntityList is
56    /// guaranteed to always be a Block encoded as a Value via BlockCall::block_to_value.
57    /// Consequently, the values entity list is never empty.
58    values: entity::EntityList<Value>,
59}
60
61impl BlockCall {
62    // NOTE: the only uses of this function should be internal to BlockCall. See the block comment
63    // on BlockCall for more context.
64    fn value_to_block(val: Value) -> Block {
65        Block::from_u32(val.as_u32())
66    }
67
68    // NOTE: the only uses of this function should be internal to BlockCall. See the block comment
69    // on BlockCall for more context.
70    fn block_to_value(block: Block) -> Value {
71        Value::from_u32(block.as_u32())
72    }
73
74    /// Construct a BlockCall with the given block and arguments.
75    pub fn new(
76        block: Block,
77        args: impl IntoIterator<Item = BlockArg>,
78        pool: &mut ValueListPool,
79    ) -> Self {
80        let mut values = ValueList::default();
81        values.push(Self::block_to_value(block), pool);
82        values.extend(args.into_iter().map(|arg| arg.encode_as_value()), pool);
83        Self { values }
84    }
85
86    /// Return the block for this BlockCall.
87    pub fn block(&self, pool: &ValueListPool) -> Block {
88        let val = self.values.first(pool).unwrap();
89        Self::value_to_block(val)
90    }
91
92    /// Replace the block for this BlockCall.
93    pub fn set_block(&mut self, block: Block, pool: &mut ValueListPool) {
94        *self.values.get_mut(0, pool).unwrap() = Self::block_to_value(block);
95    }
96
97    /// Append an argument to the block args.
98    pub fn append_argument(&mut self, arg: impl Into<BlockArg>, pool: &mut ValueListPool) {
99        self.values.push(arg.into().encode_as_value(), pool);
100    }
101
102    /// Return the length of the argument list.
103    pub fn len(&self, pool: &ValueListPool) -> usize {
104        self.values.len(pool) - 1
105    }
106
107    /// Return an iterator over the arguments of this block.
108    pub fn args<'a>(
109        &self,
110        pool: &'a ValueListPool,
111    ) -> impl ExactSizeIterator<Item = BlockArg> + DoubleEndedIterator<Item = BlockArg> + use<'a>
112    {
113        self.values.as_slice(pool)[1..]
114            .iter()
115            .map(|value| BlockArg::decode_from_value(*value))
116    }
117
118    /// Traverse the arguments with a closure that can mutate them.
119    pub fn update_args<F: FnMut(BlockArg) -> BlockArg>(
120        &mut self,
121        pool: &mut ValueListPool,
122        mut f: F,
123    ) {
124        for raw in self.values.as_mut_slice(pool)[1..].iter_mut() {
125            let new = f(BlockArg::decode_from_value(*raw));
126            *raw = new.encode_as_value();
127        }
128    }
129
130    /// Remove the argument at ix from the argument list.
131    pub fn remove(&mut self, ix: usize, pool: &mut ValueListPool) {
132        self.values.remove(1 + ix, pool)
133    }
134
135    /// Clear out the arguments list.
136    pub fn clear(&mut self, pool: &mut ValueListPool) {
137        self.values.truncate(1, pool)
138    }
139
140    /// Appends multiple elements to the arguments.
141    pub fn extend<I, T>(&mut self, elements: I, pool: &mut ValueListPool)
142    where
143        I: IntoIterator<Item = T>,
144        T: Into<BlockArg>,
145    {
146        self.values.extend(
147            elements
148                .into_iter()
149                .map(|elem| elem.into().encode_as_value()),
150            pool,
151        )
152    }
153
154    /// Return a value that can display this block call.
155    pub fn display<'a>(&self, pool: &'a ValueListPool) -> DisplayBlockCall<'a> {
156        DisplayBlockCall { block: *self, pool }
157    }
158
159    /// Deep-clone the underlying list in the same pool. The returned
160    /// list will have identical contents but changes to this list
161    /// will not change its contents or vice-versa.
162    pub fn deep_clone(&self, pool: &mut ValueListPool) -> Self {
163        Self {
164            values: self.values.deep_clone(pool),
165        }
166    }
167}
168
169/// Wrapper for the context needed to display a [BlockCall] value.
170pub struct DisplayBlockCall<'a> {
171    block: BlockCall,
172    pool: &'a ValueListPool,
173}
174
175impl<'a> Display for DisplayBlockCall<'a> {
176    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
177        write!(f, "{}", self.block.block(&self.pool))?;
178        if self.block.len(self.pool) > 0 {
179            write!(f, "(")?;
180            for (ix, arg) in self.block.args(self.pool).enumerate() {
181                if ix > 0 {
182                    write!(f, ", ")?;
183                }
184                write!(f, "{arg}")?;
185            }
186            write!(f, ")")?;
187        }
188        Ok(())
189    }
190}
191
192/// A `BlockArg` is a sum type of `Value`, `TryCallRet`, and
193/// `TryCallExn`. The latter two are values that are generated "on the
194/// edge" out of a `try_call` instruction into a successor block. We
195/// use special arguments rather than special values for these because
196/// they are not definable as SSA values at a certain program point --
197/// only when the `BlockCall` is executed.
198#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
199pub enum BlockArg {
200    /// An ordinary value, usable at the branch instruction using this
201    /// `BlockArg`, whose value is passed as an argument.
202    Value(Value),
203
204    /// A return value of a `try_call`'s called function. Signatures
205    /// allow multiple return values, so this carries an index. This
206    /// may be used only on the normal (non-exceptional) `BlockCall`
207    /// out of a `try_call` or `try_call_indirect` instruction.
208    TryCallRet(u32),
209
210    /// An exception payload value of a `try_call`. Some ABIs may
211    /// allow multiple payload values, so this carries an index. Its
212    /// type is defined by the ABI of the called function. This may be
213    /// used only on an exceptional `BlockCall` out of a `try_call` or
214    /// `try_call_indirect` instruction.
215    TryCallExn(u32),
216}
217
218impl BlockArg {
219    /// Encode this block argument as a `Value` for storage in the
220    /// value pool. Internal to `BlockCall`, must not be used
221    /// elsewhere to avoid exposing the raw bit encoding.
222    fn encode_as_value(&self) -> Value {
223        let (tag, payload) = match *self {
224            BlockArg::Value(v) => (0, v.as_bits()),
225            BlockArg::TryCallRet(i) => (1, i),
226            BlockArg::TryCallExn(i) => (2, i),
227        };
228        assert!(payload < (1 << 30));
229        let raw = (tag << 30) | payload;
230        Value::from_bits(raw)
231    }
232
233    /// Decode a raw `Value` encoding of this block argument.
234    fn decode_from_value(v: Value) -> Self {
235        let raw = v.as_u32();
236        let tag = raw >> 30;
237        let payload = raw & ((1 << 30) - 1);
238        match tag {
239            0 => BlockArg::Value(Value::from_bits(payload)),
240            1 => BlockArg::TryCallRet(payload),
241            2 => BlockArg::TryCallExn(payload),
242            _ => unreachable!(),
243        }
244    }
245
246    /// Return this argument as a `Value`, if it is one, or `None`
247    /// otherwise.
248    pub fn as_value(&self) -> Option<Value> {
249        match *self {
250            BlockArg::Value(v) => Some(v),
251            _ => None,
252        }
253    }
254
255    /// Update the contained value, if any.
256    pub fn map_value<F: FnMut(Value) -> Value>(&self, mut f: F) -> Self {
257        match *self {
258            BlockArg::Value(v) => BlockArg::Value(f(v)),
259            other => other,
260        }
261    }
262}
263
264impl Display for BlockArg {
265    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
266        match self {
267            BlockArg::Value(v) => write!(f, "{v}"),
268            BlockArg::TryCallRet(i) => write!(f, "ret{i}"),
269            BlockArg::TryCallExn(i) => write!(f, "exn{i}"),
270        }
271    }
272}
273
274impl From<Value> for BlockArg {
275    fn from(value: Value) -> BlockArg {
276        BlockArg::Value(value)
277    }
278}
279
280// Include code generated by `cranelift-codegen/meta/src/gen_inst.rs`. This file contains:
281//
282// - The `pub enum InstructionFormat` enum with all the instruction formats.
283// - The `pub enum InstructionData` enum with all the instruction data fields.
284// - The `pub enum Opcode` definition with all known opcodes,
285// - The `const OPCODE_FORMAT: [InstructionFormat; N]` table.
286// - The private `fn opcode_name(Opcode) -> &'static str` function, and
287// - The hash table `const OPCODE_HASH_TABLE: [Opcode; N]`.
288//
289// For value type constraints:
290//
291// - The `const OPCODE_CONSTRAINTS : [OpcodeConstraints; N]` table.
292// - The `const TYPE_SETS : [ValueTypeSet; N]` table.
293// - The `const OPERAND_CONSTRAINTS : [OperandConstraint; N]` table.
294//
295include!(concat!(env!("OUT_DIR"), "/opcodes.rs"));
296
297impl Display for Opcode {
298    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
299        write!(f, "{}", opcode_name(*self))
300    }
301}
302
303impl Opcode {
304    /// Get the instruction format for this opcode.
305    pub fn format(self) -> InstructionFormat {
306        OPCODE_FORMAT[self as usize - 1]
307    }
308
309    /// Get the constraint descriptor for this opcode.
310    /// Panic if this is called on `NotAnOpcode`.
311    pub fn constraints(self) -> OpcodeConstraints {
312        OPCODE_CONSTRAINTS[self as usize - 1]
313    }
314
315    /// Is this instruction a GC safepoint?
316    ///
317    /// Safepoints are all kinds of calls, except for tail calls.
318    #[inline]
319    pub fn is_safepoint(self) -> bool {
320        self.is_call() && !self.is_return()
321    }
322}
323
324// This trait really belongs in cranelift-reader where it is used by the `.clif` file parser, but since
325// it critically depends on the `opcode_name()` function which is needed here anyway, it lives in
326// this module. This also saves us from running the build script twice to generate code for the two
327// separate crates.
328impl FromStr for Opcode {
329    type Err = &'static str;
330
331    /// Parse an Opcode name from a string.
332    fn from_str(s: &str) -> Result<Self, &'static str> {
333        use crate::constant_hash::{probe, simple_hash};
334
335        match probe::<&str, [Option<Self>]>(&OPCODE_HASH_TABLE, s, simple_hash(s)) {
336            Err(_) => Err("Unknown opcode"),
337            // We unwrap here because probe() should have ensured that the entry
338            // at this index is not None.
339            Ok(i) => Ok(OPCODE_HASH_TABLE[i].unwrap()),
340        }
341    }
342}
343
344impl<'a> Table<&'a str> for [Option<Opcode>] {
345    fn len(&self) -> usize {
346        self.len()
347    }
348
349    fn key(&self, idx: usize) -> Option<&'a str> {
350        self[idx].map(opcode_name)
351    }
352}
353
354/// A variable list of `Value` operands used for function call arguments and passing arguments to
355/// basic blocks.
356#[derive(Clone, Debug)]
357pub struct VariableArgs(Vec<Value>);
358
359impl VariableArgs {
360    /// Create an empty argument list.
361    pub fn new() -> Self {
362        Self(Vec::new())
363    }
364
365    /// Add an argument to the end.
366    pub fn push(&mut self, v: Value) {
367        self.0.push(v)
368    }
369
370    /// Check if the list is empty.
371    pub fn is_empty(&self) -> bool {
372        self.0.is_empty()
373    }
374
375    /// Convert this to a value list in `pool` with `fixed` prepended.
376    pub fn into_value_list(self, fixed: &[Value], pool: &mut ValueListPool) -> ValueList {
377        let mut vlist = ValueList::default();
378        vlist.extend(fixed.iter().cloned(), pool);
379        vlist.extend(self.0, pool);
380        vlist
381    }
382}
383
384// Coerce `VariableArgs` into a `&[Value]` slice.
385impl Deref for VariableArgs {
386    type Target = [Value];
387
388    fn deref(&self) -> &[Value] {
389        &self.0
390    }
391}
392
393impl DerefMut for VariableArgs {
394    fn deref_mut(&mut self) -> &mut [Value] {
395        &mut self.0
396    }
397}
398
399impl Display for VariableArgs {
400    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
401        for (i, val) in self.0.iter().enumerate() {
402            if i == 0 {
403                write!(fmt, "{val}")?;
404            } else {
405                write!(fmt, ", {val}")?;
406            }
407        }
408        Ok(())
409    }
410}
411
412impl Default for VariableArgs {
413    fn default() -> Self {
414        Self::new()
415    }
416}
417
418/// Analyzing an instruction.
419///
420/// Avoid large matches on instruction formats by using the methods defined here to examine
421/// instructions.
422impl InstructionData {
423    /// Get the destinations of this instruction, if it's a branch.
424    ///
425    /// `br_table` returns the empty slice.
426    pub fn branch_destination<'a>(
427        &'a self,
428        jump_tables: &'a ir::JumpTables,
429        exception_tables: &'a ir::ExceptionTables,
430    ) -> &'a [BlockCall] {
431        match self {
432            Self::Jump { destination, .. } => core::slice::from_ref(destination),
433            Self::Brif { blocks, .. } => blocks.as_slice(),
434            Self::BranchTable { table, .. } => jump_tables.get(*table).unwrap().all_branches(),
435            Self::TryCall { exception, .. } | Self::TryCallIndirect { exception, .. } => {
436                exception_tables.get(*exception).unwrap().all_branches()
437            }
438            _ => {
439                debug_assert!(!self.opcode().is_branch());
440                &[]
441            }
442        }
443    }
444
445    /// Get a mutable slice of the destinations of this instruction, if it's a branch.
446    ///
447    /// `br_table` returns the empty slice.
448    pub fn branch_destination_mut<'a>(
449        &'a mut self,
450        jump_tables: &'a mut ir::JumpTables,
451        exception_tables: &'a mut ir::ExceptionTables,
452    ) -> &'a mut [BlockCall] {
453        match self {
454            Self::Jump { destination, .. } => core::slice::from_mut(destination),
455            Self::Brif { blocks, .. } => blocks.as_mut_slice(),
456            Self::BranchTable { table, .. } => {
457                jump_tables.get_mut(*table).unwrap().all_branches_mut()
458            }
459            Self::TryCall { exception, .. } | Self::TryCallIndirect { exception, .. } => {
460                exception_tables
461                    .get_mut(*exception)
462                    .unwrap()
463                    .all_branches_mut()
464            }
465            _ => {
466                debug_assert!(!self.opcode().is_branch());
467                &mut []
468            }
469        }
470    }
471
472    /// Replace the values used in this instruction according to the given
473    /// function.
474    pub fn map_values(
475        &mut self,
476        pool: &mut ValueListPool,
477        jump_tables: &mut ir::JumpTables,
478        exception_tables: &mut ir::ExceptionTables,
479        mut f: impl FnMut(Value) -> Value,
480    ) {
481        // Map all normal operator args.
482        for arg in self.arguments_mut(pool) {
483            *arg = f(*arg);
484        }
485
486        // Map all BlockCall args.
487        for block in self.branch_destination_mut(jump_tables, exception_tables) {
488            block.update_args(pool, |arg| arg.map_value(|val| f(val)));
489        }
490
491        // Map all context items.
492        if let Some(et) = self.exception_table() {
493            for ctx in exception_tables[et].contexts_mut() {
494                *ctx = f(*ctx);
495            }
496        }
497    }
498
499    /// If this is a trapping instruction, get its trap code. Otherwise, return
500    /// `None`.
501    pub fn trap_code(&self) -> Option<TrapCode> {
502        match *self {
503            Self::CondTrap { code, .. }
504            | Self::IntAddTrap { code, .. }
505            | Self::Trap { code, .. } => Some(code),
506            _ => None,
507        }
508    }
509
510    /// If this is a control-flow instruction depending on an integer condition, gets its
511    /// condition.  Otherwise, return `None`.
512    pub fn cond_code(&self) -> Option<IntCC> {
513        match self {
514            &InstructionData::IntCompare { cond, .. } => Some(cond),
515            _ => None,
516        }
517    }
518
519    /// If this is a control-flow instruction depending on a floating-point condition, gets its
520    /// condition.  Otherwise, return `None`.
521    pub fn fp_cond_code(&self) -> Option<FloatCC> {
522        match self {
523            &InstructionData::FloatCompare { cond, .. } => Some(cond),
524            _ => None,
525        }
526    }
527
528    /// If this is a trapping instruction, get an exclusive reference to its
529    /// trap code. Otherwise, return `None`.
530    pub fn trap_code_mut(&mut self) -> Option<&mut TrapCode> {
531        match self {
532            Self::CondTrap { code, .. }
533            | Self::IntAddTrap { code, .. }
534            | Self::Trap { code, .. } => Some(code),
535            _ => None,
536        }
537    }
538
539    /// If this is an atomic read/modify/write instruction, return its subopcode.
540    pub fn atomic_rmw_op(&self) -> Option<ir::AtomicRmwOp> {
541        match self {
542            &InstructionData::AtomicRmw { op, .. } => Some(op),
543            _ => None,
544        }
545    }
546
547    /// If this is a load/store instruction, returns its immediate offset.
548    pub fn load_store_offset(&self) -> Option<i32> {
549        match self {
550            &InstructionData::Load { offset, .. }
551            | &InstructionData::StackAddr { offset, .. }
552            | &InstructionData::Store { offset, .. } => Some(offset.into()),
553            _ => None,
554        }
555    }
556
557    /// If this is a load/store instruction, return its memory flags.
558    pub fn memflags(&self) -> Option<MemFlags> {
559        match self {
560            &InstructionData::Load { flags, .. }
561            | &InstructionData::LoadNoOffset { flags, .. }
562            | &InstructionData::Store { flags, .. }
563            | &InstructionData::StoreNoOffset { flags, .. }
564            | &InstructionData::AtomicCas { flags, .. }
565            | &InstructionData::AtomicRmw { flags, .. } => Some(flags),
566            _ => None,
567        }
568    }
569
570    /// If this is a load/store instruction, return its memory flags.
571    pub fn memflags_mut(&mut self) -> Option<&mut MemFlags> {
572        match self {
573            InstructionData::Load { flags, .. }
574            | InstructionData::LoadNoOffset { flags, .. }
575            | InstructionData::Store { flags, .. }
576            | InstructionData::StoreNoOffset { flags, .. }
577            | InstructionData::AtomicCas { flags, .. }
578            | InstructionData::AtomicRmw { flags, .. } => Some(flags),
579            _ => None,
580        }
581    }
582
583    /// If this is a load/store instruction, resolve its memory flags to data
584    /// through the DFG.
585    pub fn memflags_data(&self, dfg: &super::dfg::DataFlowGraph) -> Option<super::MemFlagsData> {
586        self.memflags().map(|f| dfg.mem_flags[f])
587    }
588
589    /// Get this load/store instruction's trap code, if any.
590    ///
591    /// Returns `None` when this is not a load/store instruction, or if it is a
592    /// load/store instruction but does not have a trap code.
593    pub fn memflags_trap_code(&self, dfg: &super::dfg::DataFlowGraph) -> Option<TrapCode> {
594        self.memflags_data(dfg)?.trap_code()
595    }
596
597    /// If this is a load/store instruction, get its alias region.
598    pub fn alias_region(&self, dfg: &super::dfg::DataFlowGraph) -> Option<super::AliasRegion> {
599        let flags = self.memflags_data(dfg)?;
600        flags.alias_region()
601    }
602
603    /// If this instruction references a stack slot, return it
604    pub fn stack_slot(&self) -> Option<StackSlot> {
605        match self {
606            &InstructionData::StackAddr { stack_slot, .. } => Some(stack_slot),
607            _ => None,
608        }
609    }
610
611    /// Return information about a call instruction.
612    ///
613    /// Any instruction that can call another function reveals its call signature here.
614    pub fn analyze_call<'a>(
615        &'a self,
616        pool: &'a ValueListPool,
617        exception_tables: &ExceptionTables,
618    ) -> CallInfo<'a> {
619        match *self {
620            Self::Call {
621                func_ref, ref args, ..
622            } => CallInfo::Direct(func_ref, args.as_slice(pool)),
623            Self::CallIndirect {
624                sig_ref, ref args, ..
625            } => CallInfo::Indirect(sig_ref, &args.as_slice(pool)[1..]),
626            Self::TryCall {
627                func_ref,
628                ref args,
629                exception,
630                ..
631            } => {
632                let exdata = &exception_tables[exception];
633                CallInfo::DirectWithSig(func_ref, exdata.signature(), args.as_slice(pool))
634            }
635            Self::TryCallIndirect {
636                exception,
637                ref args,
638                ..
639            } => {
640                let exdata = &exception_tables[exception];
641                CallInfo::Indirect(exdata.signature(), &args.as_slice(pool)[1..])
642            }
643            Self::Ternary {
644                opcode: Opcode::StackSwitch,
645                ..
646            } => {
647                // `StackSwitch` is not actually a call, but has the .call() side
648                // effect as it continues execution elsewhere.
649                CallInfo::NotACall
650            }
651            _ => {
652                debug_assert!(!self.opcode().is_call());
653                CallInfo::NotACall
654            }
655        }
656    }
657
658    #[inline]
659    pub(crate) fn mask_immediates(&mut self, ctrl_typevar: Type) {
660        if ctrl_typevar.is_invalid() {
661            return;
662        }
663
664        let bit_width = ctrl_typevar.bits();
665
666        match self {
667            Self::UnaryImm { opcode: _, imm } => {
668                *imm = imm.mask_to_width(bit_width);
669            }
670            _ => {}
671        }
672    }
673
674    /// Get the exception table, if any, associated with this instruction.
675    pub fn exception_table(&self) -> Option<ExceptionTable> {
676        match self {
677            Self::TryCall { exception, .. } | Self::TryCallIndirect { exception, .. } => {
678                Some(*exception)
679            }
680            _ => None,
681        }
682    }
683}
684
685/// Information about call instructions.
686pub enum CallInfo<'a> {
687    /// This is not a call instruction.
688    NotACall,
689
690    /// This is a direct call to an external function declared in the preamble. See
691    /// `DataFlowGraph.ext_funcs`.
692    Direct(FuncRef, &'a [Value]),
693
694    /// This is an indirect call with the specified signature. See `DataFlowGraph.signatures`.
695    Indirect(SigRef, &'a [Value]),
696
697    /// This is a direct call to an external function declared in the
698    /// preamble, but the signature is also known by other means:
699    /// e.g., from an exception table entry.
700    DirectWithSig(FuncRef, SigRef, &'a [Value]),
701}
702
703/// Value type constraints for a given opcode.
704///
705/// The `InstructionFormat` determines the constraints on most operands, but `Value` operands and
706/// results are not determined by the format. Every `Opcode` has an associated
707/// `OpcodeConstraints` object that provides the missing details.
708#[derive(Clone, Copy)]
709pub struct OpcodeConstraints {
710    /// Flags for this opcode encoded as a bit field:
711    ///
712    /// Bits 0-2:
713    ///     Number of fixed result values. This does not include `variable_args` results as are
714    ///     produced by call instructions.
715    ///
716    /// Bit 3:
717    ///     This opcode is polymorphic and the controlling type variable can be inferred from the
718    ///     designated input operand. This is the `typevar_operand` index given to the
719    ///     `InstructionFormat` meta language object. When this bit is not set, the controlling
720    ///     type variable must be the first output value instead.
721    ///
722    /// Bit 4:
723    ///     This opcode is polymorphic and the controlling type variable does *not* appear as the
724    ///     first result type.
725    ///
726    /// Bits 5-7:
727    ///     Number of fixed value arguments. The minimum required number of value operands.
728    flags: u8,
729
730    /// Permitted set of types for the controlling type variable as an index into `TYPE_SETS`.
731    typeset_offset: u8,
732
733    /// Offset into `OPERAND_CONSTRAINT` table of the descriptors for this opcode. The first
734    /// `num_fixed_results()` entries describe the result constraints, then follows constraints for
735    /// the fixed `Value` input operands. (`num_fixed_value_arguments()` of them).
736    constraint_offset: u16,
737}
738
739impl OpcodeConstraints {
740    /// Can the controlling type variable for this opcode be inferred from the designated value
741    /// input operand?
742    /// This also implies that this opcode is polymorphic.
743    pub fn use_typevar_operand(self) -> bool {
744        (self.flags & 0x8) != 0
745    }
746
747    /// Is it necessary to look at the designated value input operand in order to determine the
748    /// controlling type variable, or is it good enough to use the first return type?
749    ///
750    /// Most polymorphic instructions produce a single result with the type of the controlling type
751    /// variable. A few polymorphic instructions either don't produce any results, or produce
752    /// results with a fixed type. These instructions return `true`.
753    pub fn requires_typevar_operand(self) -> bool {
754        (self.flags & 0x10) != 0
755    }
756
757    /// Get the number of *fixed* result values produced by this opcode.
758    /// This does not include `variable_args` produced by calls.
759    pub fn num_fixed_results(self) -> usize {
760        (self.flags & 0x7) as usize
761    }
762
763    /// Get the number of *fixed* input values required by this opcode.
764    ///
765    /// This does not include `variable_args` arguments on call and branch instructions.
766    ///
767    /// The number of fixed input values is usually implied by the instruction format, but
768    /// instruction formats that use a `ValueList` put both fixed and variable arguments in the
769    /// list. This method returns the *minimum* number of values required in the value list.
770    pub fn num_fixed_value_arguments(self) -> usize {
771        ((self.flags >> 5) & 0x7) as usize
772    }
773
774    /// Get the offset into `TYPE_SETS` for the controlling type variable.
775    /// Returns `None` if the instruction is not polymorphic.
776    fn typeset_offset(self) -> Option<usize> {
777        let offset = usize::from(self.typeset_offset);
778        if offset < TYPE_SETS.len() {
779            Some(offset)
780        } else {
781            None
782        }
783    }
784
785    /// Get the offset into OPERAND_CONSTRAINTS where the descriptors for this opcode begin.
786    fn constraint_offset(self) -> usize {
787        self.constraint_offset as usize
788    }
789
790    /// Get the value type of result number `n`, having resolved the controlling type variable to
791    /// `ctrl_type`.
792    pub fn result_type(self, n: usize, ctrl_type: Type) -> Type {
793        debug_assert!(n < self.num_fixed_results(), "Invalid result index");
794        match OPERAND_CONSTRAINTS[self.constraint_offset() + n].resolve(ctrl_type) {
795            ResolvedConstraint::Bound(t) => t,
796            ResolvedConstraint::Free(ts) => panic!("Result constraints can't be free: {ts:?}"),
797        }
798    }
799
800    /// Get the value type of input value number `n`, having resolved the controlling type variable
801    /// to `ctrl_type`.
802    ///
803    /// Unlike results, it is possible for some input values to vary freely within a specific
804    /// `ValueTypeSet`. This is represented with the `ArgumentConstraint::Free` variant.
805    pub fn value_argument_constraint(self, n: usize, ctrl_type: Type) -> ResolvedConstraint {
806        debug_assert!(
807            n < self.num_fixed_value_arguments(),
808            "Invalid value argument index"
809        );
810        let offset = self.constraint_offset() + self.num_fixed_results();
811        OPERAND_CONSTRAINTS[offset + n].resolve(ctrl_type)
812    }
813
814    /// Get the typeset of allowed types for the controlling type variable in a polymorphic
815    /// instruction.
816    pub fn ctrl_typeset(self) -> Option<ValueTypeSet> {
817        self.typeset_offset().map(|offset| TYPE_SETS[offset])
818    }
819
820    /// Is this instruction polymorphic?
821    pub fn is_polymorphic(self) -> bool {
822        self.ctrl_typeset().is_some()
823    }
824}
825
826type BitSet8 = ScalarBitSet<u8>;
827type BitSet16 = ScalarBitSet<u16>;
828
829/// A value type set describes the permitted set of types for a type variable.
830#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
831pub struct ValueTypeSet {
832    /// Allowed lane sizes
833    pub lanes: BitSet16,
834    /// Allowed int widths
835    pub ints: BitSet8,
836    /// Allowed float widths
837    pub floats: BitSet8,
838    /// Allowed dynamic vectors minimum lane sizes
839    pub dynamic_lanes: BitSet16,
840}
841
842impl ValueTypeSet {
843    /// Is `scalar` part of the base type set?
844    ///
845    /// Note that the base type set does not have to be included in the type set proper.
846    fn is_base_type(self, scalar: Type) -> bool {
847        let l2b = u8::try_from(scalar.log2_lane_bits()).unwrap();
848        if scalar.is_int() {
849            self.ints.contains(l2b)
850        } else if scalar.is_float() {
851            self.floats.contains(l2b)
852        } else {
853            false
854        }
855    }
856
857    /// Does `typ` belong to this set?
858    pub fn contains(self, typ: Type) -> bool {
859        if typ.is_dynamic_vector() {
860            let l2l = u8::try_from(typ.log2_min_lane_count()).unwrap();
861            self.dynamic_lanes.contains(l2l) && self.is_base_type(typ.lane_type())
862        } else {
863            let l2l = u8::try_from(typ.log2_lane_count()).unwrap();
864            self.lanes.contains(l2l) && self.is_base_type(typ.lane_type())
865        }
866    }
867
868    /// Get an example member of this type set.
869    ///
870    /// This is used for error messages to avoid suggesting invalid types.
871    pub fn example(self) -> Type {
872        let t = if self.ints.max().unwrap_or(0) > 5 {
873            types::I32
874        } else if self.floats.max().unwrap_or(0) > 5 {
875            types::F32
876        } else {
877            types::I8
878        };
879        t.by(1 << self.lanes.min().unwrap()).unwrap()
880    }
881}
882
883/// Operand constraints. This describes the value type constraints on a single `Value` operand.
884enum OperandConstraint {
885    /// This operand has a concrete value type.
886    Concrete(Type),
887
888    /// This operand can vary freely within the given type set.
889    /// The type set is identified by its index into the TYPE_SETS constant table.
890    Free(u8),
891
892    /// This operand is the same type as the controlling type variable.
893    Same,
894
895    /// This operand is `ctrlType.lane_of()`.
896    LaneOf,
897
898    /// This operand is `ctrlType.as_truthy()`.
899    AsTruthy,
900
901    /// This operand is `ctrlType.half_width()`.
902    HalfWidth,
903
904    /// This operand is `ctrlType.double_width()`.
905    DoubleWidth,
906
907    /// This operand is `ctrlType.split_lanes()`.
908    SplitLanes,
909
910    /// This operand is `ctrlType.merge_lanes()`.
911    MergeLanes,
912
913    /// This operands is `ctrlType.dynamic_to_vector()`.
914    DynamicToVector,
915
916    /// This operand is `ctrlType.narrower()`.
917    Narrower,
918
919    /// This operand is `ctrlType.wider()`.
920    Wider,
921}
922
923impl OperandConstraint {
924    /// Resolve this operand constraint into a concrete value type, given the value of the
925    /// controlling type variable.
926    pub fn resolve(&self, ctrl_type: Type) -> ResolvedConstraint {
927        use self::OperandConstraint::*;
928        use self::ResolvedConstraint::Bound;
929        match *self {
930            Concrete(t) => Bound(t),
931            Free(vts) => ResolvedConstraint::Free(TYPE_SETS[vts as usize]),
932            Same => Bound(ctrl_type),
933            LaneOf => Bound(ctrl_type.lane_of()),
934            AsTruthy => Bound(ctrl_type.as_truthy()),
935            HalfWidth => Bound(ctrl_type.half_width().expect("invalid type for half_width")),
936            DoubleWidth => Bound(
937                ctrl_type
938                    .double_width()
939                    .expect("invalid type for double_width"),
940            ),
941            SplitLanes => {
942                if ctrl_type.is_dynamic_vector() {
943                    Bound(
944                        ctrl_type
945                            .dynamic_to_vector()
946                            .expect("invalid type for dynamic_to_vector")
947                            .split_lanes()
948                            .expect("invalid type for split_lanes")
949                            .vector_to_dynamic()
950                            .expect("invalid dynamic type"),
951                    )
952                } else {
953                    Bound(
954                        ctrl_type
955                            .split_lanes()
956                            .expect("invalid type for split_lanes"),
957                    )
958                }
959            }
960            MergeLanes => {
961                if ctrl_type.is_dynamic_vector() {
962                    Bound(
963                        ctrl_type
964                            .dynamic_to_vector()
965                            .expect("invalid type for dynamic_to_vector")
966                            .merge_lanes()
967                            .expect("invalid type for merge_lanes")
968                            .vector_to_dynamic()
969                            .expect("invalid dynamic type"),
970                    )
971                } else {
972                    Bound(
973                        ctrl_type
974                            .merge_lanes()
975                            .expect("invalid type for merge_lanes"),
976                    )
977                }
978            }
979            DynamicToVector => Bound(
980                ctrl_type
981                    .dynamic_to_vector()
982                    .expect("invalid type for dynamic_to_vector"),
983            ),
984            Narrower => {
985                let ctrl_type_bits = ctrl_type.log2_lane_bits();
986                let mut tys = ValueTypeSet::default();
987
988                // We're testing scalar values, only.
989                tys.lanes = ScalarBitSet::from_range(0, 1);
990
991                if ctrl_type.is_int() {
992                    // The upper bound in from_range is exclusive, and we want to exclude the
993                    // control type to construct the interval of [I8, ctrl_type).
994                    tys.ints = BitSet8::from_range(3, ctrl_type_bits as u8);
995                } else if ctrl_type.is_float() {
996                    // The upper bound in from_range is exclusive, and we want to exclude the
997                    // control type to construct the interval of [F16, ctrl_type).
998                    tys.floats = BitSet8::from_range(4, ctrl_type_bits as u8);
999                } else {
1000                    panic!(
1001                        "The Narrower constraint only operates on floats or ints, got {ctrl_type:?}"
1002                    );
1003                }
1004                ResolvedConstraint::Free(tys)
1005            }
1006            Wider => {
1007                let ctrl_type_bits = ctrl_type.log2_lane_bits();
1008                let mut tys = ValueTypeSet::default();
1009
1010                // We're testing scalar values, only.
1011                tys.lanes = ScalarBitSet::from_range(0, 1);
1012
1013                if ctrl_type.is_int() {
1014                    let lower_bound = ctrl_type_bits as u8 + 1;
1015                    // The largest integer type we can represent in `BitSet8` is I128, which is
1016                    // represented by bit 7 in the bit set. Adding one to exclude I128 from the
1017                    // lower bound would overflow as 2^8 doesn't fit in a u8, but this would
1018                    // already describe the empty set so instead we leave `ints` in its default
1019                    // empty state.
1020                    if lower_bound < BitSet8::capacity() {
1021                        // The interval should include all types wider than `ctrl_type`, so we use
1022                        // `2^8` as the upper bound, and add one to the bits of `ctrl_type` to define
1023                        // the interval `(ctrl_type, I128]`.
1024                        tys.ints = BitSet8::from_range(lower_bound, 8);
1025                    }
1026                } else if ctrl_type.is_float() {
1027                    // Same as above but for `tys.floats`, as the largest float type is F128.
1028                    let lower_bound = ctrl_type_bits as u8 + 1;
1029                    if lower_bound < BitSet8::capacity() {
1030                        tys.floats = BitSet8::from_range(lower_bound, 8);
1031                    }
1032                } else {
1033                    panic!(
1034                        "The Wider constraint only operates on floats or ints, got {ctrl_type:?}"
1035                    );
1036                }
1037
1038                ResolvedConstraint::Free(tys)
1039            }
1040        }
1041    }
1042}
1043
1044/// The type constraint on a value argument once the controlling type variable is known.
1045#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1046pub enum ResolvedConstraint {
1047    /// The operand is bound to a known type.
1048    Bound(Type),
1049    /// The operand type can vary freely within the given set.
1050    Free(ValueTypeSet),
1051}
1052
1053/// A trait to map some functions over each of the entities within an
1054/// instruction, when paired with `InstructionData::map`.
1055pub trait InstructionMapper {
1056    /// Map a function over a `Value`.
1057    fn map_value(&mut self, value: Value) -> Value;
1058
1059    /// Map a function over a `ValueList`.
1060    fn map_value_list(&mut self, value_list: ValueList) -> ValueList;
1061
1062    /// Map a function over a `GlobalValue`.
1063    fn map_global_value(&mut self, global_value: ir::GlobalValue) -> ir::GlobalValue;
1064
1065    /// Map a function over a `JumpTable`.
1066    fn map_jump_table(&mut self, jump_table: ir::JumpTable) -> ir::JumpTable;
1067
1068    /// Map a function over an `ExceptionTable`.
1069    fn map_exception_table(&mut self, exception_table: ExceptionTable) -> ExceptionTable;
1070
1071    /// Map a function over a `BlockCall`.
1072    fn map_block_call(&mut self, block_call: BlockCall) -> BlockCall;
1073
1074    /// Map a function over a `Block`.
1075    fn map_block(&mut self, block: Block) -> Block;
1076
1077    /// Map a function over a `FuncRef`.
1078    fn map_func_ref(&mut self, func_ref: FuncRef) -> FuncRef;
1079
1080    /// Map a function over a `SigRef`.
1081    fn map_sig_ref(&mut self, sig_ref: SigRef) -> SigRef;
1082
1083    /// Map a function over a `StackSlot`.
1084    fn map_stack_slot(&mut self, stack_slot: StackSlot) -> StackSlot;
1085
1086    /// Map a function over a `DynamicStackSlot`.
1087    fn map_dynamic_stack_slot(
1088        &mut self,
1089        dynamic_stack_slot: ir::DynamicStackSlot,
1090    ) -> ir::DynamicStackSlot;
1091
1092    /// Map a function over a `Constant`.
1093    fn map_constant(&mut self, constant: ir::Constant) -> ir::Constant;
1094
1095    /// Map a function over an `Immediate`.
1096    fn map_immediate(&mut self, immediate: ir::Immediate) -> ir::Immediate;
1097
1098    /// Map a function over a `MemFlags` entity.
1099    ///
1100    /// The default implementation returns the flags unchanged, which is correct
1101    /// for mappers within a single function. Override this when mapping between
1102    /// functions (e.g. inlining) to re-insert the flags data into the target DFG.
1103    fn map_mem_flags(&mut self, flags: ir::MemFlags) -> ir::MemFlags {
1104        flags
1105    }
1106}
1107
1108impl<'a, T> InstructionMapper for &'a mut T
1109where
1110    T: InstructionMapper,
1111{
1112    fn map_value(&mut self, value: Value) -> Value {
1113        (**self).map_value(value)
1114    }
1115
1116    fn map_value_list(&mut self, value_list: ValueList) -> ValueList {
1117        (**self).map_value_list(value_list)
1118    }
1119
1120    fn map_global_value(&mut self, global_value: ir::GlobalValue) -> ir::GlobalValue {
1121        (**self).map_global_value(global_value)
1122    }
1123
1124    fn map_jump_table(&mut self, jump_table: ir::JumpTable) -> ir::JumpTable {
1125        (**self).map_jump_table(jump_table)
1126    }
1127
1128    fn map_exception_table(&mut self, exception_table: ExceptionTable) -> ExceptionTable {
1129        (**self).map_exception_table(exception_table)
1130    }
1131
1132    fn map_block_call(&mut self, block_call: BlockCall) -> BlockCall {
1133        (**self).map_block_call(block_call)
1134    }
1135
1136    fn map_block(&mut self, block: Block) -> Block {
1137        (**self).map_block(block)
1138    }
1139
1140    fn map_func_ref(&mut self, func_ref: FuncRef) -> FuncRef {
1141        (**self).map_func_ref(func_ref)
1142    }
1143
1144    fn map_sig_ref(&mut self, sig_ref: SigRef) -> SigRef {
1145        (**self).map_sig_ref(sig_ref)
1146    }
1147
1148    fn map_stack_slot(&mut self, stack_slot: StackSlot) -> StackSlot {
1149        (**self).map_stack_slot(stack_slot)
1150    }
1151
1152    fn map_dynamic_stack_slot(
1153        &mut self,
1154        dynamic_stack_slot: ir::DynamicStackSlot,
1155    ) -> ir::DynamicStackSlot {
1156        (**self).map_dynamic_stack_slot(dynamic_stack_slot)
1157    }
1158
1159    fn map_constant(&mut self, constant: ir::Constant) -> ir::Constant {
1160        (**self).map_constant(constant)
1161    }
1162
1163    fn map_immediate(&mut self, immediate: ir::Immediate) -> ir::Immediate {
1164        (**self).map_immediate(immediate)
1165    }
1166
1167    fn map_mem_flags(&mut self, flags: ir::MemFlags) -> ir::MemFlags {
1168        (**self).map_mem_flags(flags)
1169    }
1170}
1171
1172#[cfg(test)]
1173mod tests {
1174    use super::*;
1175    use alloc::string::ToString;
1176    use ir::{DynamicStackSlot, GlobalValue, JumpTable};
1177
1178    #[test]
1179    fn inst_data_is_copy() {
1180        fn is_copy<T: Copy>() {}
1181        is_copy::<InstructionData>();
1182    }
1183
1184    #[test]
1185    fn inst_data_size() {
1186        // The size of `InstructionData` is performance sensitive, so make sure
1187        // we don't regress it unintentionally.
1188        assert_eq!(core::mem::size_of::<InstructionData>(), 16);
1189    }
1190
1191    #[test]
1192    fn opcodes() {
1193        use core::mem;
1194
1195        let x = Opcode::Iadd;
1196        let mut y = Opcode::Isub;
1197
1198        assert!(x != y);
1199        y = Opcode::Iadd;
1200        assert_eq!(x, y);
1201        assert_eq!(x.format(), InstructionFormat::Binary);
1202
1203        assert_eq!(format!("{:?}", Opcode::StackAddr), "StackAddr");
1204        assert_eq!(Opcode::StackAddr.to_string(), "stack_addr");
1205
1206        // Check the matcher.
1207        assert_eq!("iadd".parse::<Opcode>(), Ok(Opcode::Iadd));
1208        assert_eq!("stack_addr".parse::<Opcode>(), Ok(Opcode::StackAddr));
1209        assert_eq!("iadd\0".parse::<Opcode>(), Err("Unknown opcode"));
1210        assert_eq!("".parse::<Opcode>(), Err("Unknown opcode"));
1211        assert_eq!("\0".parse::<Opcode>(), Err("Unknown opcode"));
1212
1213        // Opcode is a single byte, and because Option<Opcode> originally came to 2 bytes, early on
1214        // Opcode included a variant NotAnOpcode to avoid the unnecessary bloat. Since then the Rust
1215        // compiler has brought in NonZero optimization, meaning that an enum not using the 0 value
1216        // can be optional for no size cost. We want to ensure Option<Opcode> remains small.
1217        assert_eq!(mem::size_of::<Opcode>(), mem::size_of::<Option<Opcode>>());
1218    }
1219
1220    #[test]
1221    fn instruction_data() {
1222        use core::mem;
1223        // The size of the `InstructionData` enum is important for performance. It should not
1224        // exceed 16 bytes. Use `Box<FooData>` out-of-line payloads for instruction formats that
1225        // require more space than that. It would be fine with a data structure smaller than 16
1226        // bytes, but what are the odds of that?
1227        assert_eq!(mem::size_of::<InstructionData>(), 16);
1228    }
1229
1230    #[test]
1231    fn constraints() {
1232        let a = Opcode::Iadd.constraints();
1233        assert!(a.use_typevar_operand());
1234        assert!(!a.requires_typevar_operand());
1235        assert_eq!(a.num_fixed_results(), 1);
1236        assert_eq!(a.num_fixed_value_arguments(), 2);
1237        assert_eq!(a.result_type(0, types::I32), types::I32);
1238        assert_eq!(a.result_type(0, types::I8), types::I8);
1239        assert_eq!(
1240            a.value_argument_constraint(0, types::I32),
1241            ResolvedConstraint::Bound(types::I32)
1242        );
1243        assert_eq!(
1244            a.value_argument_constraint(1, types::I32),
1245            ResolvedConstraint::Bound(types::I32)
1246        );
1247
1248        let b = Opcode::Bitcast.constraints();
1249        assert!(!b.use_typevar_operand());
1250        assert!(!b.requires_typevar_operand());
1251        assert_eq!(b.num_fixed_results(), 1);
1252        assert_eq!(b.num_fixed_value_arguments(), 1);
1253        assert_eq!(b.result_type(0, types::I32), types::I32);
1254        assert_eq!(b.result_type(0, types::I8), types::I8);
1255        match b.value_argument_constraint(0, types::I32) {
1256            ResolvedConstraint::Free(vts) => assert!(vts.contains(types::F32)),
1257            _ => panic!("Unexpected constraint from value_argument_constraint"),
1258        }
1259
1260        let c = Opcode::Call.constraints();
1261        assert_eq!(c.num_fixed_results(), 0);
1262        assert_eq!(c.num_fixed_value_arguments(), 0);
1263
1264        let i = Opcode::CallIndirect.constraints();
1265        assert_eq!(i.num_fixed_results(), 0);
1266        assert_eq!(i.num_fixed_value_arguments(), 1);
1267
1268        let cmp = Opcode::Icmp.constraints();
1269        assert!(cmp.use_typevar_operand());
1270        assert!(cmp.requires_typevar_operand());
1271        assert_eq!(cmp.num_fixed_results(), 1);
1272        assert_eq!(cmp.num_fixed_value_arguments(), 2);
1273        assert_eq!(cmp.result_type(0, types::I64), types::I8);
1274    }
1275
1276    #[test]
1277    fn value_set() {
1278        use crate::ir::types::*;
1279
1280        let vts = ValueTypeSet {
1281            lanes: BitSet16::from_range(0, 8),
1282            ints: BitSet8::from_range(4, 7),
1283            floats: BitSet8::from_range(0, 0),
1284            dynamic_lanes: BitSet16::from_range(0, 4),
1285        };
1286        assert!(!vts.contains(I8));
1287        assert!(vts.contains(I32));
1288        assert!(vts.contains(I64));
1289        assert!(vts.contains(I32X4));
1290        assert!(vts.contains(I32X4XN));
1291        assert!(!vts.contains(F16));
1292        assert!(!vts.contains(F32));
1293        assert!(!vts.contains(F128));
1294        assert_eq!(vts.example().to_string(), "i32");
1295
1296        let vts = ValueTypeSet {
1297            lanes: BitSet16::from_range(0, 8),
1298            ints: BitSet8::from_range(0, 0),
1299            floats: BitSet8::from_range(5, 7),
1300            dynamic_lanes: BitSet16::from_range(0, 8),
1301        };
1302        assert_eq!(vts.example().to_string(), "f32");
1303
1304        let vts = ValueTypeSet {
1305            lanes: BitSet16::from_range(1, 8),
1306            ints: BitSet8::from_range(0, 0),
1307            floats: BitSet8::from_range(5, 7),
1308            dynamic_lanes: BitSet16::from_range(0, 8),
1309        };
1310        assert_eq!(vts.example().to_string(), "f32x2");
1311
1312        let vts = ValueTypeSet {
1313            lanes: BitSet16::from_range(2, 8),
1314            ints: BitSet8::from_range(3, 7),
1315            floats: BitSet8::from_range(0, 0),
1316            dynamic_lanes: BitSet16::from_range(0, 8),
1317        };
1318        assert_eq!(vts.example().to_string(), "i32x4");
1319
1320        let vts = ValueTypeSet {
1321            // TypeSet(lanes=(1, 256), ints=(8, 64))
1322            lanes: BitSet16::from_range(0, 9),
1323            ints: BitSet8::from_range(3, 7),
1324            floats: BitSet8::from_range(0, 0),
1325            dynamic_lanes: BitSet16::from_range(0, 8),
1326        };
1327        assert!(vts.contains(I32));
1328        assert!(vts.contains(I32X4));
1329    }
1330
1331    #[test]
1332    fn instruction_data_map() {
1333        struct TestMapper;
1334
1335        impl InstructionMapper for TestMapper {
1336            fn map_value(&mut self, value: Value) -> Value {
1337                Value::from_u32(value.as_u32() + 1)
1338            }
1339
1340            fn map_value_list(&mut self, _value_list: ValueList) -> ValueList {
1341                ValueList::new()
1342            }
1343
1344            fn map_global_value(&mut self, global_value: ir::GlobalValue) -> ir::GlobalValue {
1345                GlobalValue::from_u32(global_value.as_u32() + 1)
1346            }
1347
1348            fn map_jump_table(&mut self, jump_table: ir::JumpTable) -> ir::JumpTable {
1349                JumpTable::from_u32(jump_table.as_u32() + 1)
1350            }
1351
1352            fn map_exception_table(&mut self, exception_table: ExceptionTable) -> ExceptionTable {
1353                ExceptionTable::from_u32(exception_table.as_u32() + 1)
1354            }
1355
1356            fn map_block_call(&mut self, _block_call: BlockCall) -> BlockCall {
1357                let block = Block::from_u32(42);
1358                let mut pool = ValueListPool::new();
1359                BlockCall::new(block, [], &mut pool)
1360            }
1361
1362            fn map_block(&mut self, block: Block) -> Block {
1363                Block::from_u32(block.as_u32() + 1)
1364            }
1365
1366            fn map_func_ref(&mut self, func_ref: FuncRef) -> FuncRef {
1367                FuncRef::from_u32(func_ref.as_u32() + 1)
1368            }
1369
1370            fn map_sig_ref(&mut self, sig_ref: SigRef) -> SigRef {
1371                SigRef::from_u32(sig_ref.as_u32() + 1)
1372            }
1373
1374            fn map_stack_slot(&mut self, stack_slot: StackSlot) -> StackSlot {
1375                StackSlot::from_u32(stack_slot.as_u32() + 1)
1376            }
1377
1378            fn map_dynamic_stack_slot(
1379                &mut self,
1380                dynamic_stack_slot: ir::DynamicStackSlot,
1381            ) -> ir::DynamicStackSlot {
1382                DynamicStackSlot::from_u32(dynamic_stack_slot.as_u32() + 1)
1383            }
1384
1385            fn map_constant(&mut self, constant: ir::Constant) -> ir::Constant {
1386                ir::Constant::from_u32(constant.as_u32() + 1)
1387            }
1388
1389            fn map_immediate(&mut self, immediate: ir::Immediate) -> ir::Immediate {
1390                ir::Immediate::from_u32(immediate.as_u32() + 1)
1391            }
1392        }
1393
1394        let mut pool = ValueListPool::new();
1395        let map = |inst: InstructionData| inst.map(TestMapper);
1396
1397        // Mapping `Value`s.
1398        assert_eq!(
1399            map(InstructionData::Binary {
1400                opcode: Opcode::Iadd,
1401                args: [Value::from_u32(10), Value::from_u32(20)]
1402            }),
1403            InstructionData::Binary {
1404                opcode: Opcode::Iadd,
1405                args: [Value::from_u32(11), Value::from_u32(21)]
1406            }
1407        );
1408
1409        // Mapping `ValueList`s and `FuncRef`s.
1410        let mut args = ValueList::new();
1411        args.push(Value::from_u32(42), &mut pool);
1412        let func_ref = FuncRef::from_u32(99);
1413        let inst = map(InstructionData::Call {
1414            opcode: Opcode::Call,
1415            args,
1416            func_ref,
1417        });
1418        let InstructionData::Call {
1419            opcode: Opcode::Call,
1420            args,
1421            func_ref,
1422        } = inst
1423        else {
1424            panic!()
1425        };
1426        assert!(args.is_empty());
1427        assert_eq!(func_ref, FuncRef::from_u32(100));
1428
1429        // Mapping `GlobalValue`s.
1430        assert_eq!(
1431            map(InstructionData::UnaryGlobalValue {
1432                opcode: Opcode::SymbolValue,
1433                global_value: GlobalValue::from_u32(4),
1434            }),
1435            InstructionData::UnaryGlobalValue {
1436                opcode: Opcode::SymbolValue,
1437                global_value: GlobalValue::from_u32(5),
1438            }
1439        );
1440
1441        // Mapping `JumpTable`s.
1442        assert_eq!(
1443            map(InstructionData::BranchTable {
1444                opcode: Opcode::BrTable,
1445                arg: Value::from_u32(0),
1446                table: JumpTable::from_u32(1),
1447            }),
1448            InstructionData::BranchTable {
1449                opcode: Opcode::BrTable,
1450                arg: Value::from_u32(1),
1451                table: JumpTable::from_u32(2),
1452            }
1453        );
1454
1455        // Mapping `ExceptionTable`s.
1456        assert_eq!(
1457            map(InstructionData::TryCall {
1458                opcode: Opcode::TryCall,
1459                args,
1460                func_ref: FuncRef::from_u32(0),
1461                exception: ExceptionTable::from_u32(1),
1462            }),
1463            InstructionData::TryCall {
1464                opcode: Opcode::TryCall,
1465                args,
1466                func_ref: FuncRef::from_u32(1),
1467                exception: ExceptionTable::from_u32(2),
1468            }
1469        );
1470
1471        // Mapping `BlockCall`s.
1472        assert_eq!(
1473            map(InstructionData::Jump {
1474                opcode: Opcode::Jump,
1475                destination: BlockCall::new(Block::from_u32(99), [], &mut pool),
1476            }),
1477            map(InstructionData::Jump {
1478                opcode: Opcode::Jump,
1479                destination: BlockCall::new(Block::from_u32(42), [], &mut pool),
1480            })
1481        );
1482
1483        // Mapping `Block`s.
1484        assert_eq!(
1485            map(InstructionData::ExceptionHandlerAddress {
1486                opcode: Opcode::GetExceptionHandlerAddress,
1487                block: Block::from_u32(1),
1488                imm: 0.into(),
1489            }),
1490            InstructionData::ExceptionHandlerAddress {
1491                opcode: Opcode::GetExceptionHandlerAddress,
1492                block: Block::from_u32(2),
1493                imm: 0.into(),
1494            },
1495        );
1496
1497        // Mapping `SigRef`s.
1498        assert_eq!(
1499            map(InstructionData::CallIndirect {
1500                opcode: Opcode::CallIndirect,
1501                args,
1502                sig_ref: SigRef::from_u32(11)
1503            }),
1504            InstructionData::CallIndirect {
1505                opcode: Opcode::CallIndirect,
1506                args: ValueList::new(),
1507                sig_ref: SigRef::from_u32(12)
1508            }
1509        );
1510
1511        // Mapping `StackSlot`s.
1512        assert_eq!(
1513            map(InstructionData::StackAddr {
1514                opcode: Opcode::StackAddr,
1515                stack_slot: StackSlot::from_u32(0),
1516                offset: 0.into()
1517            }),
1518            InstructionData::StackAddr {
1519                opcode: Opcode::StackAddr,
1520                stack_slot: StackSlot::from_u32(1),
1521                offset: 0.into()
1522            },
1523        );
1524
1525        // Mapping `DynamicStackSlot`s.
1526        assert_eq!(
1527            map(InstructionData::DynamicStackAddr {
1528                opcode: Opcode::DynamicStackAddr,
1529                dynamic_stack_slot: DynamicStackSlot::from_u32(0),
1530            }),
1531            InstructionData::DynamicStackAddr {
1532                opcode: Opcode::DynamicStackAddr,
1533                dynamic_stack_slot: DynamicStackSlot::from_u32(1),
1534            },
1535        );
1536
1537        // Mapping `Constant`s
1538        assert_eq!(
1539            map(InstructionData::UnaryConst {
1540                opcode: ir::Opcode::Vconst,
1541                constant_handle: ir::Constant::from_u32(2)
1542            }),
1543            InstructionData::UnaryConst {
1544                opcode: ir::Opcode::Vconst,
1545                constant_handle: ir::Constant::from_u32(3)
1546            },
1547        );
1548
1549        // Mapping `Immediate`s
1550        assert_eq!(
1551            map(InstructionData::Shuffle {
1552                opcode: ir::Opcode::Shuffle,
1553                args: [Value::from_u32(0), Value::from_u32(1)],
1554                imm: ir::Immediate::from_u32(41),
1555            }),
1556            InstructionData::Shuffle {
1557                opcode: ir::Opcode::Shuffle,
1558                args: [Value::from_u32(1), Value::from_u32(2)],
1559                imm: ir::Immediate::from_u32(42),
1560            },
1561        );
1562    }
1563}