Skip to main content

cranelift_codegen_meta/shared/
instructions.rs

1#![expect(non_snake_case, reason = "DSL style here")]
2
3use crate::cdsl::instructions::{
4    AllInstructions, InstructionBuilder as Inst, InstructionGroupBuilder,
5};
6use crate::cdsl::operands::Operand;
7use crate::cdsl::types::{LaneType, ValueType};
8use crate::cdsl::typevar::{Interval, TypeSetBuilder, TypeVar};
9use crate::shared::formats::Formats;
10use crate::shared::types;
11use crate::shared::{entities::EntityRefs, immediates::Immediates};
12
13#[inline(never)]
14fn define_control_flow(
15    ig: &mut InstructionGroupBuilder,
16    formats: &Formats,
17    imm: &Immediates,
18    entities: &EntityRefs,
19) {
20    ig.push(
21        Inst::new(
22            "jump",
23            r#"
24        Jump.
25
26        Unconditionally jump to a basic block, passing the specified
27        block arguments. The number and types of arguments must match the
28        destination block.
29        "#,
30            &formats.jump,
31        )
32        .operands_in(vec![
33            Operand::new("block_call", &entities.block_call)
34                .with_doc("Destination basic block, with its arguments provided"),
35        ])
36        .branches(),
37    );
38
39    let ScalarTruthy = &TypeVar::new(
40        "ScalarTruthy",
41        "A scalar truthy type",
42        TypeSetBuilder::new().ints(Interval::All).build(),
43    );
44
45    ig.push(
46        Inst::new(
47            "brif",
48            r#"
49        Conditional branch when cond is non-zero.
50
51        Take the ``then`` branch when ``c != 0``, and the ``else`` branch otherwise.
52        "#,
53            &formats.brif,
54        )
55        .operands_in(vec![
56            Operand::new("c", ScalarTruthy).with_doc("Controlling value to test"),
57            Operand::new("block_then", &entities.block_then).with_doc("Then block"),
58            Operand::new("block_else", &entities.block_else).with_doc("Else block"),
59        ])
60        .branches(),
61    );
62
63    {
64        let _i32 = &TypeVar::new(
65            "i32",
66            "A 32 bit scalar integer type",
67            TypeSetBuilder::new().ints(32..32).build(),
68        );
69
70        ig.push(
71            Inst::new(
72                "br_table",
73                r#"
74        Indirect branch via jump table.
75
76        Use ``x`` as an unsigned index into the jump table ``JT``. If a jump
77        table entry is found, branch to the corresponding block. If no entry was
78        found or the index is out-of-bounds, branch to the default block of the
79        table.
80
81        Note that this branch instruction can't pass arguments to the targeted
82        blocks. Split critical edges as needed to work around this.
83
84        Do not confuse this with "tables" in WebAssembly. ``br_table`` is for
85        jump tables with destinations within the current function only -- think
86        of a ``match`` in Rust or a ``switch`` in C.  If you want to call a
87        function in a dynamic library, that will typically use
88        ``call_indirect``.
89        "#,
90                &formats.branch_table,
91            )
92            .operands_in(vec![
93                Operand::new("x", _i32).with_doc("i32 index into jump table"),
94                Operand::new("JT", &entities.jump_table),
95            ])
96            .branches(),
97        );
98    }
99
100    let iAddr = &TypeVar::new(
101        "iAddr",
102        "An integer address type",
103        TypeSetBuilder::new().ints(32..64).build(),
104    );
105
106    ig.push(
107        Inst::new(
108            "debugtrap",
109            r#"
110        Encodes an assembly debug trap.
111        "#,
112            &formats.nullary,
113        )
114        .other_side_effects()
115        .can_load()
116        .can_store(),
117    );
118
119    ig.push(
120        Inst::new(
121            "trap",
122            r#"
123        Terminate execution unconditionally.
124        "#,
125            &formats.trap,
126        )
127        .operands_in(vec![Operand::new("code", &imm.trapcode)])
128        .can_trap()
129        .terminates_block(),
130    );
131
132    ig.push(
133        Inst::new(
134            "trapz",
135            r#"
136        Trap when zero.
137
138        if ``c`` is non-zero, execution continues at the following instruction.
139        "#,
140            &formats.cond_trap,
141        )
142        .operands_in(vec![
143            Operand::new("c", ScalarTruthy).with_doc("Controlling value to test"),
144            Operand::new("code", &imm.trapcode),
145        ])
146        .can_trap()
147        // When one `trapz` dominates another `trapz` and they have identical
148        // conditions and trap codes, it is safe to deduplicate them (like GVN,
149        // although there is not actually any value being numbered). Either the
150        // first `trapz` raised a trap and execution halted, or it didn't and
151        // therefore the dominated `trapz` will not raise a trap either.
152        .side_effects_idempotent(),
153    );
154
155    ig.push(
156        Inst::new(
157            "trapnz",
158            r#"
159        Trap when non-zero.
160
161        If ``c`` is zero, execution continues at the following instruction.
162        "#,
163            &formats.cond_trap,
164        )
165        .operands_in(vec![
166            Operand::new("c", ScalarTruthy).with_doc("Controlling value to test"),
167            Operand::new("code", &imm.trapcode),
168        ])
169        .can_trap()
170        // See the above comment for `trapz` and idempotent side effects.
171        .side_effects_idempotent(),
172    );
173
174    ig.push(
175        Inst::new(
176            "return",
177            r#"
178        Return from the function.
179
180        Unconditionally transfer control to the calling function, passing the
181        provided return values. The list of return values must match the
182        function signature's return types.
183        "#,
184            &formats.multiary,
185        )
186        .operands_in(vec![
187            Operand::new("rvals", &entities.varargs).with_doc("return values"),
188        ])
189        .returns(),
190    );
191
192    ig.push(
193        Inst::new(
194            "call",
195            r#"
196        Direct function call.
197
198        Call a function which has been declared in the preamble. The argument
199        types must match the function's signature.
200        "#,
201            &formats.call,
202        )
203        .operands_in(vec![
204            Operand::new("FN", &entities.func_ref)
205                .with_doc("function to call, declared by `function`"),
206            Operand::new("args", &entities.varargs).with_doc("call arguments"),
207        ])
208        .operands_out(vec![
209            Operand::new("rvals", &entities.varargs).with_doc("return values"),
210        ])
211        .call(),
212    );
213
214    ig.push(
215        Inst::new(
216            "call_indirect",
217            r#"
218        Indirect function call.
219
220        Call the function pointed to by `callee` with the given arguments. The
221        called function must match the specified signature.
222
223        Note that this is different from WebAssembly's ``call_indirect``; the
224        callee is a native address, rather than a table index. For WebAssembly,
225        `table_addr` and `load` are used to obtain a native address
226        from a table.
227        "#,
228            &formats.call_indirect,
229        )
230        .operands_in(vec![
231            Operand::new("SIG", &entities.sig_ref).with_doc("function signature"),
232            Operand::new("callee", iAddr).with_doc("address of function to call"),
233            Operand::new("args", &entities.varargs).with_doc("call arguments"),
234        ])
235        .operands_out(vec![
236            Operand::new("rvals", &entities.varargs).with_doc("return values"),
237        ])
238        .call(),
239    );
240
241    ig.push(
242        Inst::new(
243            "return_call",
244            r#"
245        Direct tail call.
246
247        Tail call a function which has been declared in the preamble. The
248        argument types must match the function's signature, the caller and
249        callee calling conventions must be the same, and must be a calling
250        convention that supports tail calls.
251
252        This instruction is a block terminator.
253        "#,
254            &formats.call,
255        )
256        .operands_in(vec![
257            Operand::new("FN", &entities.func_ref)
258                .with_doc("function to call, declared by `function`"),
259            Operand::new("args", &entities.varargs).with_doc("call arguments"),
260        ])
261        .returns()
262        .call(),
263    );
264
265    ig.push(
266        Inst::new(
267            "return_call_indirect",
268            r#"
269        Indirect tail call.
270
271        Call the function pointed to by `callee` with the given arguments. The
272        argument types must match the function's signature, the caller and
273        callee calling conventions must be the same, and must be a calling
274        convention that supports tail calls.
275
276        This instruction is a block terminator.
277
278        Note that this is different from WebAssembly's ``tail_call_indirect``;
279        the callee is a native address, rather than a table index. For
280        WebAssembly, `table_addr` and `load` are used to obtain a native address
281        from a table.
282        "#,
283            &formats.call_indirect,
284        )
285        .operands_in(vec![
286            Operand::new("SIG", &entities.sig_ref).with_doc("function signature"),
287            Operand::new("callee", iAddr).with_doc("address of function to call"),
288            Operand::new("args", &entities.varargs).with_doc("call arguments"),
289        ])
290        .returns()
291        .call(),
292    );
293
294    ig.push(
295        Inst::new(
296            "func_addr",
297            r#"
298        Get the address of a function.
299
300        Compute the absolute address of a function declared in the preamble.
301        The returned address can be used as a ``callee`` argument to
302        `call_indirect`. This is also a method for calling functions that
303        are too far away to be addressable by a direct `call`
304        instruction.
305        "#,
306            &formats.func_addr,
307        )
308        .operands_in(vec![
309            Operand::new("FN", &entities.func_ref)
310                .with_doc("function to call, declared by `function`"),
311        ])
312        .operands_out(vec![Operand::new("addr", iAddr)]),
313    );
314
315    ig.push(
316        Inst::new(
317            "try_call",
318            r#"
319        Call a function, catching the specified exceptions.
320
321        Call the function pointed to by `callee` with the given arguments. On
322        normal return, branch to the first target, with function returns
323        available as `retN` block arguments. On exceptional return,
324        look up the thrown exception tag in the provided exception table;
325        if the tag matches one of the targets, branch to the matching
326        target with the exception payloads available as `exnN` block arguments.
327        If no tag matches, then propagate the exception up the stack.
328
329        It is the Cranelift embedder's responsibility to define the meaning
330        of tags: they are accepted by this instruction and passed through
331        to unwind metadata tables in Cranelift's output. Actual unwinding is
332        outside the purview of the core Cranelift compiler.
333
334        Payload values on exception are passed in fixed register(s) that are
335        defined by the platform and ABI. See the documentation on `CallConv`
336        for details.
337        "#,
338            &formats.try_call,
339        )
340        .operands_in(vec![
341            Operand::new("callee", &entities.func_ref)
342                .with_doc("function to call, declared by `function`"),
343            Operand::new("args", &entities.varargs).with_doc("call arguments"),
344            Operand::new("ET", &entities.exception_table).with_doc("exception table"),
345        ])
346        .call()
347        .branches(),
348    );
349
350    ig.push(
351        Inst::new(
352            "try_call_indirect",
353            r#"
354        Call a function, catching the specified exceptions.
355
356        Call the function pointed to by `callee` with the given arguments. On
357        normal return, branch to the first target, with function returns
358        available as `retN` block arguments. On exceptional return,
359        look up the thrown exception tag in the provided exception table;
360        if the tag matches one of the targets, branch to the matching
361        target with the exception payloads available as `exnN` block arguments.
362        If no tag matches, then propagate the exception up the stack.
363
364        It is the Cranelift embedder's responsibility to define the meaning
365        of tags: they are accepted by this instruction and passed through
366        to unwind metadata tables in Cranelift's output. Actual unwinding is
367        outside the purview of the core Cranelift compiler.
368
369        Payload values on exception are passed in fixed register(s) that are
370        defined by the platform and ABI. See the documentation on `CallConv`
371        for details.
372        "#,
373            &formats.try_call_indirect,
374        )
375        .operands_in(vec![
376            Operand::new("callee", iAddr).with_doc("address of function to call"),
377            Operand::new("args", &entities.varargs).with_doc("call arguments"),
378            Operand::new("ET", &entities.exception_table).with_doc("exception table"),
379        ])
380        .call()
381        .branches(),
382    );
383}
384
385#[inline(never)]
386fn define_simd_lane_access(
387    ig: &mut InstructionGroupBuilder,
388    formats: &Formats,
389    imm: &Immediates,
390    _: &EntityRefs,
391) {
392    let TxN = &TypeVar::new(
393        "TxN",
394        "A SIMD vector type",
395        TypeSetBuilder::new()
396            .ints(Interval::All)
397            .floats(Interval::All)
398            .simd_lanes(Interval::All)
399            .dynamic_simd_lanes(Interval::All)
400            .includes_scalars(false)
401            .build(),
402    );
403
404    ig.push(
405        Inst::new(
406            "splat",
407            r#"
408        Vector splat.
409
410        Return a vector whose lanes are all ``x``.
411        "#,
412            &formats.unary,
413        )
414        .operands_in(vec![
415            Operand::new("x", &TxN.lane_of()).with_doc("Value to splat to all lanes"),
416        ])
417        .operands_out(vec![Operand::new("a", TxN)]),
418    );
419
420    let I8x16 = &TypeVar::new(
421        "I8x16",
422        "A SIMD vector type consisting of 16 lanes of 8-bit integers",
423        TypeSetBuilder::new()
424            .ints(8..8)
425            .simd_lanes(16..16)
426            .includes_scalars(false)
427            .build(),
428    );
429
430    ig.push(
431        Inst::new(
432            "swizzle",
433            r#"
434        Vector swizzle.
435
436        Returns a new vector with byte-width lanes selected from the lanes of the first input
437        vector ``x`` specified in the second input vector ``s``. The indices ``i`` in range
438        ``[0, 15]`` select the ``i``-th element of ``x``. For indices outside of the range the
439        resulting lane is 0. Note that this operates on byte-width lanes.
440        "#,
441            &formats.binary,
442        )
443        .operands_in(vec![
444            Operand::new("x", I8x16).with_doc("Vector to modify by re-arranging lanes"),
445            Operand::new("y", I8x16).with_doc("Mask for re-arranging lanes"),
446        ])
447        .operands_out(vec![Operand::new("a", I8x16)]),
448    );
449
450    ig.push(
451        Inst::new(
452            "x86_pshufb",
453            r#"
454        A vector swizzle lookalike which has the semantics of `pshufb` on x64.
455
456        This instruction will permute the 8-bit lanes of `x` with the indices
457        specified in `y`. Each lane in the mask, `y`, uses the bottom four
458        bits for selecting the lane from `x` unless the most significant bit
459        is set, in which case the lane is zeroed. The output vector will have
460        the following contents when the element of `y` is in these ranges:
461
462        * `[0, 127]` -> `x[y[i] % 16]`
463        * `[128, 255]` -> 0
464        "#,
465            &formats.binary,
466        )
467        .operands_in(vec![
468            Operand::new("x", I8x16).with_doc("Vector to modify by re-arranging lanes"),
469            Operand::new("y", I8x16).with_doc("Mask for re-arranging lanes"),
470        ])
471        .operands_out(vec![Operand::new("a", I8x16)]),
472    );
473
474    ig.push(
475        Inst::new(
476            "insertlane",
477            r#"
478        Insert ``y`` as lane ``Idx`` in x.
479
480        The lane index, ``Idx``, is an immediate value, not an SSA value. It
481        must indicate a valid lane index for the type of ``x``.
482        "#,
483            &formats.ternary_imm8,
484        )
485        .operands_in(vec![
486            Operand::new("x", TxN).with_doc("The vector to modify"),
487            Operand::new("y", &TxN.lane_of()).with_doc("New lane value"),
488            Operand::new("Idx", &imm.uimm8).with_doc("Lane index"),
489        ])
490        .operands_out(vec![Operand::new("a", TxN)]),
491    );
492
493    ig.push(
494        Inst::new(
495            "extractlane",
496            r#"
497        Extract lane ``Idx`` from ``x``.
498
499        The lane index, ``Idx``, is an immediate value, not an SSA value. It
500        must indicate a valid lane index for the type of ``x``. Note that the upper bits of ``a``
501        may or may not be zeroed depending on the ISA but the type system should prevent using
502        ``a`` as anything other than the extracted value.
503        "#,
504            &formats.binary_imm8,
505        )
506        .operands_in(vec![
507            Operand::new("x", TxN),
508            Operand::new("Idx", &imm.uimm8).with_doc("Lane index"),
509        ])
510        .operands_out(vec![Operand::new("a", &TxN.lane_of())]),
511    );
512}
513
514#[inline(never)]
515fn define_simd_arithmetic(
516    ig: &mut InstructionGroupBuilder,
517    formats: &Formats,
518    _: &Immediates,
519    _: &EntityRefs,
520) {
521    let Int = &TypeVar::new(
522        "Int",
523        "A scalar or vector integer type",
524        TypeSetBuilder::new()
525            .ints(Interval::All)
526            .simd_lanes(Interval::All)
527            .build(),
528    );
529
530    ig.push(
531        Inst::new(
532            "smin",
533            r#"
534        Signed integer minimum.
535        "#,
536            &formats.binary,
537        )
538        .operands_in(vec![Operand::new("x", Int), Operand::new("y", Int)])
539        .operands_out(vec![Operand::new("a", Int)]),
540    );
541
542    ig.push(
543        Inst::new(
544            "umin",
545            r#"
546        Unsigned integer minimum.
547        "#,
548            &formats.binary,
549        )
550        .operands_in(vec![Operand::new("x", Int), Operand::new("y", Int)])
551        .operands_out(vec![Operand::new("a", Int)]),
552    );
553
554    ig.push(
555        Inst::new(
556            "smax",
557            r#"
558        Signed integer maximum.
559        "#,
560            &formats.binary,
561        )
562        .operands_in(vec![Operand::new("x", Int), Operand::new("y", Int)])
563        .operands_out(vec![Operand::new("a", Int)]),
564    );
565
566    ig.push(
567        Inst::new(
568            "umax",
569            r#"
570        Unsigned integer maximum.
571        "#,
572            &formats.binary,
573        )
574        .operands_in(vec![Operand::new("x", Int), Operand::new("y", Int)])
575        .operands_out(vec![Operand::new("a", Int)]),
576    );
577
578    let IxN = &TypeVar::new(
579        "IxN",
580        "A SIMD vector type containing integers",
581        TypeSetBuilder::new()
582            .ints(Interval::All)
583            .simd_lanes(Interval::All)
584            .includes_scalars(false)
585            .build(),
586    );
587
588    ig.push(
589        Inst::new(
590            "avg_round",
591            r#"
592        Unsigned average with rounding: `a := (x + y + 1) // 2`
593
594        The addition does not lose any information (such as from overflow).
595        "#,
596            &formats.binary,
597        )
598        .operands_in(vec![Operand::new("x", IxN), Operand::new("y", IxN)])
599        .operands_out(vec![Operand::new("a", IxN)]),
600    );
601
602    ig.push(
603        Inst::new(
604            "uadd_sat",
605            r#"
606        Add with unsigned saturation.
607
608        This is similar to `iadd` but the operands are interpreted as unsigned integers and their
609        summed result, instead of wrapping, will be saturated to the highest unsigned integer for
610        the controlling type (e.g. `0xFF` for i8).
611        "#,
612            &formats.binary,
613        )
614        .operands_in(vec![Operand::new("x", IxN), Operand::new("y", IxN)])
615        .operands_out(vec![Operand::new("a", IxN)]),
616    );
617
618    ig.push(
619        Inst::new(
620            "sadd_sat",
621            r#"
622        Add with signed saturation.
623
624        This is similar to `iadd` but the operands are interpreted as signed integers and their
625        summed result, instead of wrapping, will be saturated to the lowest or highest
626        signed integer for the controlling type (e.g. `0x80` or `0x7F` for i8). For example,
627        since an `sadd_sat.i8` of `0x70` and `0x70` is greater than `0x7F`, the result will be
628        clamped to `0x7F`.
629        "#,
630            &formats.binary,
631        )
632        .operands_in(vec![Operand::new("x", IxN), Operand::new("y", IxN)])
633        .operands_out(vec![Operand::new("a", IxN)]),
634    );
635
636    ig.push(
637        Inst::new(
638            "usub_sat",
639            r#"
640        Subtract with unsigned saturation.
641
642        This is similar to `isub` but the operands are interpreted as unsigned integers and their
643        difference, instead of wrapping, will be saturated to the lowest unsigned integer for
644        the controlling type (e.g. `0x00` for i8).
645        "#,
646            &formats.binary,
647        )
648        .operands_in(vec![Operand::new("x", IxN), Operand::new("y", IxN)])
649        .operands_out(vec![Operand::new("a", IxN)]),
650    );
651
652    ig.push(
653        Inst::new(
654            "ssub_sat",
655            r#"
656        Subtract with signed saturation.
657
658        This is similar to `isub` but the operands are interpreted as signed integers and their
659        difference, instead of wrapping, will be saturated to the lowest or highest
660        signed integer for the controlling type (e.g. `0x80` or `0x7F` for i8).
661        "#,
662            &formats.binary,
663        )
664        .operands_in(vec![Operand::new("x", IxN), Operand::new("y", IxN)])
665        .operands_out(vec![Operand::new("a", IxN)]),
666    );
667}
668
669pub(crate) fn define(
670    all_instructions: &mut AllInstructions,
671    formats: &Formats,
672    imm: &Immediates,
673    entities: &EntityRefs,
674) {
675    let mut ig = InstructionGroupBuilder::new(all_instructions);
676
677    define_control_flow(&mut ig, formats, imm, entities);
678    define_simd_lane_access(&mut ig, formats, imm, entities);
679    define_simd_arithmetic(&mut ig, formats, imm, entities);
680
681    // Operand kind shorthands.
682    let i8: &TypeVar = &ValueType::from(LaneType::from(types::Int::I8)).into();
683    let f16_: &TypeVar = &ValueType::from(LaneType::from(types::Float::F16)).into();
684    let f32_: &TypeVar = &ValueType::from(LaneType::from(types::Float::F32)).into();
685    let f64_: &TypeVar = &ValueType::from(LaneType::from(types::Float::F64)).into();
686    let f128_: &TypeVar = &ValueType::from(LaneType::from(types::Float::F128)).into();
687
688    // Starting definitions.
689    let Int = &TypeVar::new(
690        "Int",
691        "A scalar or vector integer type",
692        TypeSetBuilder::new()
693            .ints(Interval::All)
694            .simd_lanes(Interval::All)
695            .dynamic_simd_lanes(Interval::All)
696            .build(),
697    );
698
699    let NarrowInt = &TypeVar::new(
700        "NarrowInt",
701        "An integer type of width up to `i64`",
702        TypeSetBuilder::new().ints(8..64).build(),
703    );
704
705    let ScalarTruthy = &TypeVar::new(
706        "ScalarTruthy",
707        "A scalar truthy type",
708        TypeSetBuilder::new().ints(Interval::All).build(),
709    );
710
711    let iB = &TypeVar::new(
712        "iB",
713        "A scalar integer type",
714        TypeSetBuilder::new().ints(Interval::All).build(),
715    );
716
717    let iSwappable = &TypeVar::new(
718        "iSwappable",
719        "A multi byte scalar integer type",
720        TypeSetBuilder::new().ints(16..128).build(),
721    );
722
723    let iAddr = &TypeVar::new(
724        "iAddr",
725        "An integer address type",
726        TypeSetBuilder::new().ints(32..64).build(),
727    );
728
729    let TxN = &TypeVar::new(
730        "TxN",
731        "A SIMD vector type",
732        TypeSetBuilder::new()
733            .ints(Interval::All)
734            .floats(Interval::All)
735            .simd_lanes(Interval::All)
736            .includes_scalars(false)
737            .build(),
738    );
739    let Any = &TypeVar::new(
740        "Any",
741        "Any integer, float, or reference scalar or vector type",
742        TypeSetBuilder::new()
743            .ints(Interval::All)
744            .floats(Interval::All)
745            .simd_lanes(Interval::All)
746            .includes_scalars(true)
747            .build(),
748    );
749
750    let Mem = &TypeVar::new(
751        "Mem",
752        "Any type that can be stored in memory",
753        TypeSetBuilder::new()
754            .ints(Interval::All)
755            .floats(Interval::All)
756            .simd_lanes(Interval::All)
757            .dynamic_simd_lanes(Interval::All)
758            .build(),
759    );
760
761    let MemTo = &TypeVar::copy_from(Mem, "MemTo".to_string());
762
763    ig.push(
764        Inst::new(
765            "load",
766            r#"
767        Load from memory at ``p + Offset``.
768
769        This is a polymorphic instruction that can load any value type which
770        has a memory representation.
771        "#,
772            &formats.load,
773        )
774        .operands_in(vec![
775            Operand::new("MemFlags", &imm.memflags),
776            Operand::new("p", iAddr),
777            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
778        ])
779        .operands_out(vec![Operand::new("a", Mem).with_doc("Value loaded")])
780        .can_load(),
781    );
782
783    ig.push(
784        Inst::new(
785            "store",
786            r#"
787        Store ``x`` to memory at ``p + Offset``.
788
789        This is a polymorphic instruction that can store any value type with a
790        memory representation.
791        "#,
792            &formats.store,
793        )
794        .operands_in(vec![
795            Operand::new("MemFlags", &imm.memflags),
796            Operand::new("x", Mem).with_doc("Value to be stored"),
797            Operand::new("p", iAddr),
798            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
799        ])
800        .can_store(),
801    );
802
803    let iExt8 = &TypeVar::new(
804        "iExt8",
805        "An integer type with more than 8 bits",
806        TypeSetBuilder::new().ints(16..64).build(),
807    );
808
809    ig.push(
810        Inst::new(
811            "uload8",
812            r#"
813        Load 8 bits from memory at ``p + Offset`` and zero-extend.
814
815        This is equivalent to ``load.i8`` followed by ``uextend``.
816        "#,
817            &formats.load,
818        )
819        .operands_in(vec![
820            Operand::new("MemFlags", &imm.memflags),
821            Operand::new("p", iAddr),
822            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
823        ])
824        .operands_out(vec![Operand::new("a", iExt8)])
825        .can_load(),
826    );
827
828    ig.push(
829        Inst::new(
830            "sload8",
831            r#"
832        Load 8 bits from memory at ``p + Offset`` and sign-extend.
833
834        This is equivalent to ``load.i8`` followed by ``sextend``.
835        "#,
836            &formats.load,
837        )
838        .operands_in(vec![
839            Operand::new("MemFlags", &imm.memflags),
840            Operand::new("p", iAddr),
841            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
842        ])
843        .operands_out(vec![Operand::new("a", iExt8)])
844        .can_load(),
845    );
846
847    ig.push(
848        Inst::new(
849            "istore8",
850            r#"
851        Store the low 8 bits of ``x`` to memory at ``p + Offset``.
852
853        This is equivalent to ``ireduce.i8`` followed by ``store.i8``.
854        "#,
855            &formats.store,
856        )
857        .operands_in(vec![
858            Operand::new("MemFlags", &imm.memflags),
859            Operand::new("x", iExt8),
860            Operand::new("p", iAddr),
861            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
862        ])
863        .can_store(),
864    );
865
866    let iExt16 = &TypeVar::new(
867        "iExt16",
868        "An integer type with more than 16 bits",
869        TypeSetBuilder::new().ints(32..64).build(),
870    );
871
872    ig.push(
873        Inst::new(
874            "uload16",
875            r#"
876        Load 16 bits from memory at ``p + Offset`` and zero-extend.
877
878        This is equivalent to ``load.i16`` followed by ``uextend``.
879        "#,
880            &formats.load,
881        )
882        .operands_in(vec![
883            Operand::new("MemFlags", &imm.memflags),
884            Operand::new("p", iAddr),
885            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
886        ])
887        .operands_out(vec![Operand::new("a", iExt16)])
888        .can_load(),
889    );
890
891    ig.push(
892        Inst::new(
893            "sload16",
894            r#"
895        Load 16 bits from memory at ``p + Offset`` and sign-extend.
896
897        This is equivalent to ``load.i16`` followed by ``sextend``.
898        "#,
899            &formats.load,
900        )
901        .operands_in(vec![
902            Operand::new("MemFlags", &imm.memflags),
903            Operand::new("p", iAddr),
904            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
905        ])
906        .operands_out(vec![Operand::new("a", iExt16)])
907        .can_load(),
908    );
909
910    ig.push(
911        Inst::new(
912            "istore16",
913            r#"
914        Store the low 16 bits of ``x`` to memory at ``p + Offset``.
915
916        This is equivalent to ``ireduce.i16`` followed by ``store.i16``.
917        "#,
918            &formats.store,
919        )
920        .operands_in(vec![
921            Operand::new("MemFlags", &imm.memflags),
922            Operand::new("x", iExt16),
923            Operand::new("p", iAddr),
924            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
925        ])
926        .can_store(),
927    );
928
929    let iExt32 = &TypeVar::new(
930        "iExt32",
931        "An integer type with more than 32 bits",
932        TypeSetBuilder::new().ints(64..64).build(),
933    );
934
935    ig.push(
936        Inst::new(
937            "uload32",
938            r#"
939        Load 32 bits from memory at ``p + Offset`` and zero-extend.
940
941        This is equivalent to ``load.i32`` followed by ``uextend``.
942        "#,
943            &formats.load,
944        )
945        .operands_in(vec![
946            Operand::new("MemFlags", &imm.memflags),
947            Operand::new("p", iAddr),
948            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
949        ])
950        .operands_out(vec![Operand::new("a", iExt32)])
951        .can_load(),
952    );
953
954    ig.push(
955        Inst::new(
956            "sload32",
957            r#"
958        Load 32 bits from memory at ``p + Offset`` and sign-extend.
959
960        This is equivalent to ``load.i32`` followed by ``sextend``.
961        "#,
962            &formats.load,
963        )
964        .operands_in(vec![
965            Operand::new("MemFlags", &imm.memflags),
966            Operand::new("p", iAddr),
967            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
968        ])
969        .operands_out(vec![Operand::new("a", iExt32)])
970        .can_load(),
971    );
972
973    ig.push(
974        Inst::new(
975            "istore32",
976            r#"
977        Store the low 32 bits of ``x`` to memory at ``p + Offset``.
978
979        This is equivalent to ``ireduce.i32`` followed by ``store.i32``.
980        "#,
981            &formats.store,
982        )
983        .operands_in(vec![
984            Operand::new("MemFlags", &imm.memflags),
985            Operand::new("x", iExt32),
986            Operand::new("p", iAddr),
987            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
988        ])
989        .can_store(),
990    );
991    ig.push(
992        Inst::new(
993            "stack_switch",
994            r#"
995        Suspends execution of the current stack and resumes execution of another
996        one.
997
998        The target stack to switch to is identified by the data stored at
999        ``load_context_ptr``. Before switching, this instruction stores
1000        analogous information about the
1001        current (i.e., original) stack at ``store_context_ptr``, to
1002        enabled switching back to the original stack at a later point.
1003
1004        The size, alignment and layout of the information stored at
1005        ``load_context_ptr`` and ``store_context_ptr`` is platform-dependent.
1006        The instruction assumes that ``load_context_ptr`` and
1007        ``store_context_ptr`` are valid pointers to memory with said layout and
1008        alignment, and does not perform any checks on these pointers or the data
1009        stored there.
1010
1011        The instruction is experimental and only supported on x64 Linux at the
1012        moment.
1013
1014        When switching from a stack A to a stack B, one of the following cases
1015        must apply:
1016        1. Stack B was previously suspended using a ``stack_switch`` instruction.
1017        2. Stack B is a newly initialized stack. The necessary initialization is
1018        platform-dependent and will generally involve running some kind of
1019        trampoline to start execution of a function on the new stack.
1020
1021        In both cases, the ``in_payload`` argument of the ``stack_switch``
1022        instruction executed on A is passed to stack B. In the first case above,
1023        it will be the result value of the earlier ``stack_switch`` instruction
1024        executed on stack B. In the second case, the value will be accessible to
1025        the trampoline in a platform-dependent register.
1026
1027        The pointers ``load_context_ptr`` and ``store_context_ptr`` are allowed
1028        to be equal; the instruction ensures that all data is loaded from the
1029        former before writing to the latter.
1030
1031        Stack switching is one-shot in the sense that each ``stack_switch``
1032        operation effectively consumes the context identified by
1033        ``load_context_ptr``. In other words, performing two ``stack_switches``
1034        using the same ``load_context_ptr`` causes undefined behavior, unless
1035        the context at ``load_context_ptr`` is overwritten by another
1036        `stack_switch` in between.
1037        "#,
1038            &formats.ternary,
1039        )
1040        .operands_in(vec![
1041            Operand::new("store_context_ptr", iAddr),
1042            Operand::new("load_context_ptr", iAddr),
1043            Operand::new("in_payload0", iAddr),
1044        ])
1045        .operands_out(vec![Operand::new("out_payload0", iAddr)])
1046        .other_side_effects()
1047        .can_load()
1048        .can_store()
1049        .call(),
1050    );
1051
1052    let I16x8 = &TypeVar::new(
1053        "I16x8",
1054        "A SIMD vector with exactly 8 lanes of 16-bit values",
1055        TypeSetBuilder::new()
1056            .ints(16..16)
1057            .simd_lanes(8..8)
1058            .includes_scalars(false)
1059            .build(),
1060    );
1061
1062    ig.push(
1063        Inst::new(
1064            "uload8x8",
1065            r#"
1066        Load an 8x8 vector (64 bits) from memory at ``p + Offset`` and zero-extend into an i16x8
1067        vector.
1068        "#,
1069            &formats.load,
1070        )
1071        .operands_in(vec![
1072            Operand::new("MemFlags", &imm.memflags),
1073            Operand::new("p", iAddr),
1074            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
1075        ])
1076        .operands_out(vec![Operand::new("a", I16x8).with_doc("Value loaded")])
1077        .can_load(),
1078    );
1079
1080    ig.push(
1081        Inst::new(
1082            "sload8x8",
1083            r#"
1084        Load an 8x8 vector (64 bits) from memory at ``p + Offset`` and sign-extend into an i16x8
1085        vector.
1086        "#,
1087            &formats.load,
1088        )
1089        .operands_in(vec![
1090            Operand::new("MemFlags", &imm.memflags),
1091            Operand::new("p", iAddr),
1092            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
1093        ])
1094        .operands_out(vec![Operand::new("a", I16x8).with_doc("Value loaded")])
1095        .can_load(),
1096    );
1097
1098    let I32x4 = &TypeVar::new(
1099        "I32x4",
1100        "A SIMD vector with exactly 4 lanes of 32-bit values",
1101        TypeSetBuilder::new()
1102            .ints(32..32)
1103            .simd_lanes(4..4)
1104            .includes_scalars(false)
1105            .build(),
1106    );
1107
1108    ig.push(
1109        Inst::new(
1110            "uload16x4",
1111            r#"
1112        Load a 16x4 vector (64 bits) from memory at ``p + Offset`` and zero-extend into an i32x4
1113        vector.
1114        "#,
1115            &formats.load,
1116        )
1117        .operands_in(vec![
1118            Operand::new("MemFlags", &imm.memflags),
1119            Operand::new("p", iAddr),
1120            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
1121        ])
1122        .operands_out(vec![Operand::new("a", I32x4).with_doc("Value loaded")])
1123        .can_load(),
1124    );
1125
1126    ig.push(
1127        Inst::new(
1128            "sload16x4",
1129            r#"
1130        Load a 16x4 vector (64 bits) from memory at ``p + Offset`` and sign-extend into an i32x4
1131        vector.
1132        "#,
1133            &formats.load,
1134        )
1135        .operands_in(vec![
1136            Operand::new("MemFlags", &imm.memflags),
1137            Operand::new("p", iAddr),
1138            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
1139        ])
1140        .operands_out(vec![Operand::new("a", I32x4).with_doc("Value loaded")])
1141        .can_load(),
1142    );
1143
1144    let I64x2 = &TypeVar::new(
1145        "I64x2",
1146        "A SIMD vector with exactly 2 lanes of 64-bit values",
1147        TypeSetBuilder::new()
1148            .ints(64..64)
1149            .simd_lanes(2..2)
1150            .includes_scalars(false)
1151            .build(),
1152    );
1153
1154    ig.push(
1155        Inst::new(
1156            "uload32x2",
1157            r#"
1158        Load an 32x2 vector (64 bits) from memory at ``p + Offset`` and zero-extend into an i64x2
1159        vector.
1160        "#,
1161            &formats.load,
1162        )
1163        .operands_in(vec![
1164            Operand::new("MemFlags", &imm.memflags),
1165            Operand::new("p", iAddr),
1166            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
1167        ])
1168        .operands_out(vec![Operand::new("a", I64x2).with_doc("Value loaded")])
1169        .can_load(),
1170    );
1171
1172    ig.push(
1173        Inst::new(
1174            "sload32x2",
1175            r#"
1176        Load a 32x2 vector (64 bits) from memory at ``p + Offset`` and sign-extend into an i64x2
1177        vector.
1178        "#,
1179            &formats.load,
1180        )
1181        .operands_in(vec![
1182            Operand::new("MemFlags", &imm.memflags),
1183            Operand::new("p", iAddr),
1184            Operand::new("Offset", &imm.offset32).with_doc("Byte offset from base address"),
1185        ])
1186        .operands_out(vec![Operand::new("a", I64x2).with_doc("Value loaded")])
1187        .can_load(),
1188    );
1189
1190    ig.push(
1191        Inst::new(
1192            "stack_load",
1193            r#"
1194        Load a value from a stack slot at the constant offset.
1195
1196        This is a polymorphic instruction that can load any value type which
1197        has a memory representation.
1198
1199        The offset is an immediate constant, not an SSA value. The memory
1200        access cannot go out of bounds, i.e.
1201        `sizeof(a) + Offset <= sizeof(SS)`.
1202        "#,
1203            &formats.stack_load,
1204        )
1205        .operands_in(vec![
1206            Operand::new("SS", &entities.stack_slot),
1207            Operand::new("Offset", &imm.offset32).with_doc("In-bounds offset into stack slot"),
1208        ])
1209        .operands_out(vec![Operand::new("a", Mem).with_doc("Value loaded")])
1210        .can_load(),
1211    );
1212
1213    ig.push(
1214        Inst::new(
1215            "stack_store",
1216            r#"
1217        Store a value to a stack slot at a constant offset.
1218
1219        This is a polymorphic instruction that can store any value type with a
1220        memory representation.
1221
1222        The offset is an immediate constant, not an SSA value. The memory
1223        access cannot go out of bounds, i.e.
1224        `sizeof(a) + Offset <= sizeof(SS)`.
1225        "#,
1226            &formats.stack_store,
1227        )
1228        .operands_in(vec![
1229            Operand::new("x", Mem).with_doc("Value to be stored"),
1230            Operand::new("SS", &entities.stack_slot),
1231            Operand::new("Offset", &imm.offset32).with_doc("In-bounds offset into stack slot"),
1232        ])
1233        .can_store(),
1234    );
1235
1236    ig.push(
1237        Inst::new(
1238            "stack_addr",
1239            r#"
1240        Get the address of a stack slot.
1241
1242        Compute the absolute address of a byte in a stack slot. The offset must
1243        refer to a byte inside the stack slot:
1244        `0 <= Offset < sizeof(SS)`.
1245        "#,
1246            &formats.stack_load,
1247        )
1248        .operands_in(vec![
1249            Operand::new("SS", &entities.stack_slot),
1250            Operand::new("Offset", &imm.offset32).with_doc("In-bounds offset into stack slot"),
1251        ])
1252        .operands_out(vec![Operand::new("addr", iAddr)]),
1253    );
1254
1255    ig.push(
1256        Inst::new(
1257            "dynamic_stack_load",
1258            r#"
1259        Load a value from a dynamic stack slot.
1260
1261        This is a polymorphic instruction that can load any value type which
1262        has a memory representation.
1263        "#,
1264            &formats.dynamic_stack_load,
1265        )
1266        .operands_in(vec![Operand::new("DSS", &entities.dynamic_stack_slot)])
1267        .operands_out(vec![Operand::new("a", Mem).with_doc("Value loaded")])
1268        .can_load(),
1269    );
1270
1271    ig.push(
1272        Inst::new(
1273            "dynamic_stack_store",
1274            r#"
1275        Store a value to a dynamic stack slot.
1276
1277        This is a polymorphic instruction that can store any dynamic value type with a
1278        memory representation.
1279        "#,
1280            &formats.dynamic_stack_store,
1281        )
1282        .operands_in(vec![
1283            Operand::new("x", Mem).with_doc("Value to be stored"),
1284            Operand::new("DSS", &entities.dynamic_stack_slot),
1285        ])
1286        .can_store(),
1287    );
1288
1289    ig.push(
1290        Inst::new(
1291            "dynamic_stack_addr",
1292            r#"
1293        Get the address of a dynamic stack slot.
1294
1295        Compute the absolute address of the first byte of a dynamic stack slot.
1296        "#,
1297            &formats.dynamic_stack_load,
1298        )
1299        .operands_in(vec![Operand::new("DSS", &entities.dynamic_stack_slot)])
1300        .operands_out(vec![Operand::new("addr", iAddr)]),
1301    );
1302
1303    ig.push(
1304        Inst::new(
1305            "global_value",
1306            r#"
1307        Compute the value of global GV.
1308        "#,
1309            &formats.unary_global_value,
1310        )
1311        .operands_in(vec![Operand::new("GV", &entities.global_value)])
1312        .operands_out(vec![Operand::new("a", Mem).with_doc("Value loaded")]),
1313    );
1314
1315    ig.push(
1316        Inst::new(
1317            "symbol_value",
1318            r#"
1319        Compute the value of global GV, which is a symbolic value.
1320        "#,
1321            &formats.unary_global_value,
1322        )
1323        .operands_in(vec![Operand::new("GV", &entities.global_value)])
1324        .operands_out(vec![Operand::new("a", Mem).with_doc("Value loaded")]),
1325    );
1326
1327    ig.push(
1328        Inst::new(
1329            "tls_value",
1330            r#"
1331        Compute the value of global GV, which is a TLS (thread local storage) value.
1332        "#,
1333            &formats.unary_global_value,
1334        )
1335        .operands_in(vec![Operand::new("GV", &entities.global_value)])
1336        .operands_out(vec![Operand::new("a", Mem).with_doc("Value loaded")]),
1337    );
1338
1339    // Note this instruction is marked as having other side-effects, so GVN won't try to hoist it,
1340    // which would result in it being subject to spilling. While not hoisting would generally hurt
1341    // performance, since a computed value used many times may need to be regenerated before each
1342    // use, it is not the case here: this instruction doesn't generate any code.  That's because,
1343    // by definition the pinned register is never used by the register allocator, but is written to
1344    // and read explicitly and exclusively by set_pinned_reg and get_pinned_reg.
1345    ig.push(
1346        Inst::new(
1347            "get_pinned_reg",
1348            r#"
1349            Gets the content of the pinned register, when it's enabled.
1350        "#,
1351            &formats.nullary,
1352        )
1353        .operands_out(vec![Operand::new("addr", iAddr)])
1354        .other_side_effects(),
1355    );
1356
1357    ig.push(
1358        Inst::new(
1359            "set_pinned_reg",
1360            r#"
1361        Sets the content of the pinned register, when it's enabled.
1362        "#,
1363            &formats.unary,
1364        )
1365        .operands_in(vec![Operand::new("addr", iAddr)])
1366        .other_side_effects(),
1367    );
1368
1369    ig.push(
1370        Inst::new(
1371            "get_frame_pointer",
1372            r#"
1373        Get the address in the frame pointer register.
1374
1375        Usage of this instruction requires setting `preserve_frame_pointers` to `true`.
1376        "#,
1377            &formats.nullary,
1378        )
1379        .operands_out(vec![Operand::new("addr", iAddr)]),
1380    );
1381
1382    ig.push(
1383        Inst::new(
1384            "get_stack_pointer",
1385            r#"
1386        Get the address in the stack pointer register.
1387        "#,
1388            &formats.nullary,
1389        )
1390        .operands_out(vec![Operand::new("addr", iAddr)]),
1391    );
1392
1393    ig.push(
1394        Inst::new(
1395            "get_return_address",
1396            r#"
1397        Get the PC where this function will transfer control to when it returns.
1398
1399        Usage of this instruction requires setting `preserve_frame_pointers` to `true`.
1400        "#,
1401            &formats.nullary,
1402        )
1403        .operands_out(vec![Operand::new("addr", iAddr)]),
1404    );
1405
1406    ig.push(
1407        Inst::new(
1408            "get_exception_handler_address",
1409            r#"
1410        Get the handler PC for the given exceptional edge for an
1411        exception return from the given `try_call`-terminated block.
1412
1413        This instruction provides the PC for the handler resume point,
1414        as defined by the exception-handling aspect of the given
1415        callee ABI, for a return from the given calling block.  It can
1416        be used when the exception unwind mechanism requires manual
1417        plumbing for this information which must be set up before the call
1418        itself: for example, if the resume address needs to be stored in
1419        some context structure for a runtime to resume to on error.
1420
1421        The given caller block must end in a `try_call` and the given
1422        exception-handling block must be one of its exceptional
1423        successors in the associated exception-handling table. The
1424        returned PC is *only* valid to resume to when the `try_call`
1425        is on the stack having called the callee; in other words, when
1426        a normal exception unwinder might otherwise resume to that
1427        handler.
1428        "#,
1429            &formats.exception_handler_address,
1430        )
1431        .operands_in(vec![
1432            Operand::new("block", &entities.raw_block),
1433            Operand::new("index", &imm.imm64),
1434        ])
1435        .operands_out(vec![Operand::new("addr", iAddr)]),
1436    );
1437
1438    ig.push(
1439        Inst::new(
1440            "iconst",
1441            r#"
1442        Integer constant.
1443
1444        Create a scalar integer SSA value with an immediate constant value, or
1445        an integer vector where all the lanes have the same value.
1446        "#,
1447            &formats.unary_imm,
1448        )
1449        .operands_in(vec![Operand::new("N", &imm.imm64)])
1450        .operands_out(vec![
1451            Operand::new("a", NarrowInt).with_doc("A constant integer scalar or vector value"),
1452        ]),
1453    );
1454
1455    ig.push(
1456        Inst::new(
1457            "f16const",
1458            r#"
1459        Floating point constant.
1460
1461        Create a `f16` SSA value with an immediate constant value.
1462        "#,
1463            &formats.unary_ieee16,
1464        )
1465        .operands_in(vec![Operand::new("N", &imm.ieee16)])
1466        .operands_out(vec![
1467            Operand::new("a", f16_).with_doc("A constant f16 scalar value"),
1468        ]),
1469    );
1470
1471    ig.push(
1472        Inst::new(
1473            "f32const",
1474            r#"
1475        Floating point constant.
1476
1477        Create a `f32` SSA value with an immediate constant value.
1478        "#,
1479            &formats.unary_ieee32,
1480        )
1481        .operands_in(vec![Operand::new("N", &imm.ieee32)])
1482        .operands_out(vec![
1483            Operand::new("a", f32_).with_doc("A constant f32 scalar value"),
1484        ]),
1485    );
1486
1487    ig.push(
1488        Inst::new(
1489            "f64const",
1490            r#"
1491        Floating point constant.
1492
1493        Create a `f64` SSA value with an immediate constant value.
1494        "#,
1495            &formats.unary_ieee64,
1496        )
1497        .operands_in(vec![Operand::new("N", &imm.ieee64)])
1498        .operands_out(vec![
1499            Operand::new("a", f64_).with_doc("A constant f64 scalar value"),
1500        ]),
1501    );
1502
1503    ig.push(
1504        Inst::new(
1505            "f128const",
1506            r#"
1507        Floating point constant.
1508
1509        Create a `f128` SSA value with an immediate constant value.
1510        "#,
1511            &formats.unary_const,
1512        )
1513        .operands_in(vec![Operand::new("N", &entities.pool_constant)])
1514        .operands_out(vec![
1515            Operand::new("a", f128_).with_doc("A constant f128 scalar value"),
1516        ]),
1517    );
1518
1519    ig.push(
1520        Inst::new(
1521            "vconst",
1522            r#"
1523        SIMD vector constant.
1524
1525        Construct a vector with the given immediate bytes.
1526        "#,
1527            &formats.unary_const,
1528        )
1529        .operands_in(vec![
1530            Operand::new("N", &entities.pool_constant)
1531                .with_doc("The 16 immediate bytes of a 128-bit vector"),
1532        ])
1533        .operands_out(vec![
1534            Operand::new("a", TxN).with_doc("A constant vector value"),
1535        ]),
1536    );
1537
1538    let Tx16 = &TypeVar::new(
1539        "Tx16",
1540        "A SIMD vector with exactly 16 lanes of 8-bit values; eventually this may support other \
1541         lane counts and widths",
1542        TypeSetBuilder::new()
1543            .ints(8..8)
1544            .simd_lanes(16..16)
1545            .includes_scalars(false)
1546            .build(),
1547    );
1548
1549    ig.push(
1550        Inst::new(
1551            "shuffle",
1552            r#"
1553        SIMD vector shuffle.
1554
1555        Shuffle two vectors using the given immediate bytes. For each of the 16 bytes of the
1556        immediate, a value i of 0-15 selects the i-th element of the first vector and a value i of
1557        16-31 selects the (i-16)th element of the second vector. Immediate values outside of the
1558        0-31 range are not valid.
1559        "#,
1560            &formats.shuffle,
1561        )
1562        .operands_in(vec![
1563            Operand::new("a", Tx16).with_doc("A vector value"),
1564            Operand::new("b", Tx16).with_doc("A vector value"),
1565            Operand::new("mask", &entities.uimm128)
1566                .with_doc("The 16 immediate bytes used for selecting the elements to shuffle"),
1567        ])
1568        .operands_out(vec![Operand::new("a", Tx16).with_doc("A vector value")]),
1569    );
1570
1571    ig.push(Inst::new(
1572        "nop",
1573        r#"
1574        Just a dummy instruction.
1575
1576        Note: this doesn't compile to a machine code nop.
1577        "#,
1578        &formats.nullary,
1579    ));
1580
1581    ig.push(
1582        Inst::new(
1583            "select",
1584            r#"
1585        Conditional select.
1586
1587        This instruction selects whole values. Use `bitselect` to choose each
1588        bit according to a mask.
1589        "#,
1590            &formats.ternary,
1591        )
1592        .operands_in(vec![
1593            Operand::new("c", ScalarTruthy).with_doc("Controlling value to test"),
1594            Operand::new("x", Any).with_doc("Value to use when `c` is true"),
1595            Operand::new("y", Any).with_doc("Value to use when `c` is false"),
1596        ])
1597        .operands_out(vec![Operand::new("a", Any)]),
1598    );
1599
1600    ig.push(
1601        Inst::new(
1602            "select_spectre_guard",
1603            r#"
1604            Conditional select intended for Spectre guards.
1605
1606            This operation is semantically equivalent to a select instruction.
1607            However, this instruction prohibits all speculation on the
1608            controlling value when determining which input to use as the result.
1609            As such, it is suitable for use in Spectre guards.
1610
1611            For example, on a target which may speculatively execute branches,
1612            the lowering of this instruction is guaranteed to not conditionally
1613            branch. Instead it will typically lower to a conditional move
1614            instruction. (No Spectre-vulnerable processors are known to perform
1615            value speculation on conditional move instructions.)
1616
1617            Ensure that the instruction you're trying to protect from Spectre
1618            attacks has a data dependency on the result of this instruction.
1619            That prevents an out-of-order CPU from evaluating that instruction
1620            until the result of this one is known, which in turn will be blocked
1621            until the controlling value is known.
1622
1623            Typical usage is to use a bounds-check as the controlling value,
1624            and select between either a null pointer if the bounds-check
1625            fails, or an in-bounds address otherwise, so that dereferencing
1626            the resulting address with a load or store instruction will trap if
1627            the bounds-check failed. When this instruction is used in this way,
1628            any microarchitectural side effects of the memory access will only
1629            occur after the bounds-check finishes, which ensures that no Spectre
1630            vulnerability will exist.
1631
1632            Optimization opportunities for this instruction are limited compared
1633            to a normal select instruction, but it is allowed to be replaced
1634            by other values which are functionally equivalent as long as doing
1635            so does not introduce any new opportunities to speculate on the
1636            controlling value.
1637            "#,
1638            &formats.ternary,
1639        )
1640        .operands_in(vec![
1641            Operand::new("c", ScalarTruthy).with_doc("Controlling value to test"),
1642            Operand::new("x", Any).with_doc("Value to use when `c` is true"),
1643            Operand::new("y", Any).with_doc("Value to use when `c` is false"),
1644        ])
1645        .operands_out(vec![Operand::new("a", Any)]),
1646    );
1647
1648    ig.push(
1649        Inst::new(
1650            "bitselect",
1651            r#"
1652        Conditional select of bits.
1653
1654        For each bit in `c`, this instruction selects the corresponding bit from `x` if the bit
1655        in `c` is 1 and the corresponding bit from `y` if the bit in `c` is 0. See also:
1656        `select`.
1657        "#,
1658            &formats.ternary,
1659        )
1660        .operands_in(vec![
1661            Operand::new("c", Any).with_doc("Controlling value to test"),
1662            Operand::new("x", Any).with_doc("Value to use when `c` is true"),
1663            Operand::new("y", Any).with_doc("Value to use when `c` is false"),
1664        ])
1665        .operands_out(vec![Operand::new("a", Any)]),
1666    );
1667
1668    ig.push(
1669        Inst::new(
1670            "blendv",
1671            r#"
1672        A bitselect-lookalike instruction except with the semantics of
1673        `blendv`-related instructions on x86.
1674
1675        This instruction will use the top bit of each lane in `c`, the condition
1676        mask. If the bit is 1 then the corresponding lane from `x` is chosen.
1677        Otherwise the corresponding lane from `y` is chosen.
1678
1679            "#,
1680            &formats.ternary,
1681        )
1682        .operands_in(vec![
1683            Operand::new("c", Any).with_doc("Controlling value to test"),
1684            Operand::new("x", Any).with_doc("Value to use when `c` is true"),
1685            Operand::new("y", Any).with_doc("Value to use when `c` is false"),
1686        ])
1687        .operands_out(vec![Operand::new("a", Any)]),
1688    );
1689
1690    ig.push(
1691        Inst::new(
1692            "vany_true",
1693            r#"
1694        Reduce a vector to a scalar boolean.
1695
1696        Return a scalar boolean true if any lane in ``a`` is non-zero, false otherwise.
1697        "#,
1698            &formats.unary,
1699        )
1700        .operands_in(vec![Operand::new("a", TxN)])
1701        .operands_out(vec![Operand::new("s", i8)]),
1702    );
1703
1704    ig.push(
1705        Inst::new(
1706            "vall_true",
1707            r#"
1708        Reduce a vector to a scalar boolean.
1709
1710        Return a scalar boolean true if all lanes in ``i`` are non-zero, false otherwise.
1711        "#,
1712            &formats.unary,
1713        )
1714        .operands_in(vec![Operand::new("a", TxN)])
1715        .operands_out(vec![Operand::new("s", i8)]),
1716    );
1717
1718    ig.push(
1719        Inst::new(
1720            "vhigh_bits",
1721            r#"
1722        Reduce a vector to a scalar integer.
1723
1724        Return a scalar integer, consisting of the concatenation of the most significant bit
1725        of each lane of ``a``.
1726        "#,
1727            &formats.unary,
1728        )
1729        .operands_in(vec![Operand::new("a", TxN)])
1730        .operands_out(vec![Operand::new("x", NarrowInt)]),
1731    );
1732
1733    ig.push(
1734        Inst::new(
1735            "icmp",
1736            r#"
1737        Integer comparison.
1738
1739        The condition code determines if the operands are interpreted as signed
1740        or unsigned integers.
1741
1742        | Signed | Unsigned | Condition             |
1743        |--------|----------|-----------------------|
1744        | eq     | eq       | Equal                 |
1745        | ne     | ne       | Not equal             |
1746        | slt    | ult      | Less than             |
1747        | sge    | uge      | Greater than or equal |
1748        | sgt    | ugt      | Greater than          |
1749        | sle    | ule      | Less than or equal    |
1750
1751        When this instruction compares integer vectors, it returns a vector of
1752        lane-wise comparisons.
1753
1754        When comparing scalars, the result is:
1755            - `1` if the condition holds.
1756            - `0` if the condition does not hold.
1757
1758        When comparing vectors, the result is:
1759            - `-1` (i.e. all ones) in each lane where the condition holds.
1760            - `0` in each lane where the condition does not hold.
1761        "#,
1762            &formats.int_compare,
1763        )
1764        .operands_in(vec![
1765            Operand::new("Cond", &imm.intcc),
1766            Operand::new("x", Int),
1767            Operand::new("y", Int),
1768        ])
1769        .operands_out(vec![Operand::new("a", &Int.as_truthy())])
1770        .inst_builder_imm_method(true),
1771    );
1772
1773    ig.push(
1774        Inst::new(
1775            "iadd",
1776            r#"
1777        Wrapping integer addition: `a := x + y \pmod{2^B}`.
1778
1779        This instruction does not depend on the signed/unsigned interpretation
1780        of the operands.
1781        "#,
1782            &formats.binary,
1783        )
1784        .operands_in(vec![Operand::new("x", Int), Operand::new("y", Int)])
1785        .operands_out(vec![Operand::new("a", Int)])
1786        .inst_builder_imm_method(true),
1787    );
1788
1789    ig.push(
1790        Inst::new(
1791            "isub",
1792            r#"
1793        Wrapping integer subtraction: `a := x - y \pmod{2^B}`.
1794
1795        This instruction does not depend on the signed/unsigned interpretation
1796        of the operands.
1797        "#,
1798            &formats.binary,
1799        )
1800        .operands_in(vec![Operand::new("x", Int), Operand::new("y", Int)])
1801        .operands_out(vec![Operand::new("a", Int)]),
1802    );
1803
1804    ig.push(
1805        Inst::new(
1806            "ineg",
1807            r#"
1808        Integer negation: `a := -x \pmod{2^B}`.
1809        "#,
1810            &formats.unary,
1811        )
1812        .operands_in(vec![Operand::new("x", Int)])
1813        .operands_out(vec![Operand::new("a", Int)]),
1814    );
1815
1816    ig.push(
1817        Inst::new(
1818            "iabs",
1819            r#"
1820        Integer absolute value with wrapping: `a := |x|`.
1821        "#,
1822            &formats.unary,
1823        )
1824        .operands_in(vec![Operand::new("x", Int)])
1825        .operands_out(vec![Operand::new("a", Int)]),
1826    );
1827
1828    ig.push(
1829        Inst::new(
1830            "imul",
1831            r#"
1832        Wrapping integer multiplication: `a := x y \pmod{2^B}`.
1833
1834        This instruction does not depend on the signed/unsigned interpretation
1835        of the operands.
1836
1837        Polymorphic over all integer types (vector and scalar).
1838        "#,
1839            &formats.binary,
1840        )
1841        .operands_in(vec![Operand::new("x", Int), Operand::new("y", Int)])
1842        .operands_out(vec![Operand::new("a", Int)])
1843        .inst_builder_imm_method(true),
1844    );
1845
1846    ig.push(
1847        Inst::new(
1848            "umulhi",
1849            r#"
1850        Unsigned integer multiplication, producing the high half of a
1851        double-length result.
1852
1853        Polymorphic over all integer types (vector and scalar).
1854        "#,
1855            &formats.binary,
1856        )
1857        .operands_in(vec![Operand::new("x", Int), Operand::new("y", Int)])
1858        .operands_out(vec![Operand::new("a", Int)]),
1859    );
1860
1861    ig.push(
1862        Inst::new(
1863            "smulhi",
1864            r#"
1865        Signed integer multiplication, producing the high half of a
1866        double-length result.
1867
1868        Polymorphic over all integer types (vector and scalar).
1869        "#,
1870            &formats.binary,
1871        )
1872        .operands_in(vec![Operand::new("x", Int), Operand::new("y", Int)])
1873        .operands_out(vec![Operand::new("a", Int)]),
1874    );
1875
1876    let I16or32 = &TypeVar::new(
1877        "I16or32",
1878        "A vector integer type with 16- or 32-bit numbers",
1879        TypeSetBuilder::new().ints(16..32).simd_lanes(4..8).build(),
1880    );
1881
1882    ig.push(
1883        Inst::new(
1884            "sqmul_round_sat",
1885            r#"
1886        Fixed-point multiplication of numbers in the QN format, where N + 1
1887        is the number bitwidth:
1888        `a := signed_saturate((x * y + (1 << (Q - 1))) >> Q)`
1889
1890        Polymorphic over all integer vector types with 16- or 32-bit numbers.
1891        "#,
1892            &formats.binary,
1893        )
1894        .operands_in(vec![Operand::new("x", I16or32), Operand::new("y", I16or32)])
1895        .operands_out(vec![Operand::new("a", I16or32)]),
1896    );
1897
1898    ig.push(
1899        Inst::new(
1900            "x86_pmulhrsw",
1901            r#"
1902        A similar instruction to `sqmul_round_sat` except with the semantics
1903        of x86's `pmulhrsw` instruction.
1904
1905        This is the same as `sqmul_round_sat` except when both input lanes are
1906        `i16::MIN`.
1907        "#,
1908            &formats.binary,
1909        )
1910        .operands_in(vec![Operand::new("x", I16or32), Operand::new("y", I16or32)])
1911        .operands_out(vec![Operand::new("a", I16or32)]),
1912    );
1913
1914    // Integer division and remainder are scalar-only; most
1915    // hardware does not directly support vector integer division.
1916
1917    ig.push(
1918        Inst::new(
1919            "udiv",
1920            r#"
1921        Unsigned integer division: `a := \lfloor {x \over y} \rfloor`.
1922
1923        This operation traps if the divisor is zero.
1924        "#,
1925            &formats.binary,
1926        )
1927        .operands_in(vec![Operand::new("x", iB), Operand::new("y", iB)])
1928        .operands_out(vec![Operand::new("a", iB)])
1929        .can_trap()
1930        .side_effects_idempotent()
1931        .inst_builder_imm_method(true),
1932    );
1933
1934    ig.push(
1935        Inst::new(
1936            "sdiv",
1937            r#"
1938        Signed integer division rounded toward zero: `a := sign(xy)
1939        \lfloor {|x| \over |y|}\rfloor`.
1940
1941        This operation traps if the divisor is zero, or if the result is not
1942        representable in `B` bits two's complement. This only happens
1943        when `x = -2^{B-1}, y = -1`.
1944        "#,
1945            &formats.binary,
1946        )
1947        .operands_in(vec![Operand::new("x", iB), Operand::new("y", iB)])
1948        .operands_out(vec![Operand::new("a", iB)])
1949        .can_trap()
1950        .side_effects_idempotent()
1951        .inst_builder_imm_method(true),
1952    );
1953
1954    ig.push(
1955        Inst::new(
1956            "urem",
1957            r#"
1958        Unsigned integer remainder.
1959
1960        This operation traps if the divisor is zero.
1961        "#,
1962            &formats.binary,
1963        )
1964        .operands_in(vec![Operand::new("x", iB), Operand::new("y", iB)])
1965        .operands_out(vec![Operand::new("a", iB)])
1966        .can_trap()
1967        .side_effects_idempotent()
1968        .inst_builder_imm_method(true),
1969    );
1970
1971    ig.push(
1972        Inst::new(
1973            "srem",
1974            r#"
1975        Signed integer remainder. The result has the sign of the dividend.
1976
1977        This operation traps if the divisor is zero.
1978        "#,
1979            &formats.binary,
1980        )
1981        .operands_in(vec![Operand::new("x", iB), Operand::new("y", iB)])
1982        .operands_out(vec![Operand::new("a", iB)])
1983        .can_trap()
1984        .side_effects_idempotent()
1985        .inst_builder_imm_method(true),
1986    );
1987
1988    ig.push(
1989        Inst::new(
1990            "sadd_overflow_cin",
1991            r#"
1992        Add signed integers with carry in and overflow out.
1993
1994        Same as `sadd_overflow` with an additional carry input. The `c_in` type
1995        is interpreted as 1 if it's nonzero or 0 if it's zero.
1996        "#,
1997            &formats.ternary,
1998        )
1999        .operands_in(vec![
2000            Operand::new("x", iB),
2001            Operand::new("y", iB),
2002            Operand::new("c_in", i8).with_doc("Input carry flag"),
2003        ])
2004        .operands_out(vec![
2005            Operand::new("a", iB),
2006            Operand::new("c_out", i8).with_doc("Output carry flag"),
2007        ]),
2008    );
2009
2010    ig.push(
2011        Inst::new(
2012            "uadd_overflow_cin",
2013            r#"
2014        Add unsigned integers with carry in and overflow out.
2015
2016        Same as `uadd_overflow` with an additional carry input. The `c_in` type
2017        is interpreted as 1 if it's nonzero or 0 if it's zero.
2018        "#,
2019            &formats.ternary,
2020        )
2021        .operands_in(vec![
2022            Operand::new("x", iB),
2023            Operand::new("y", iB),
2024            Operand::new("c_in", i8).with_doc("Input carry flag"),
2025        ])
2026        .operands_out(vec![
2027            Operand::new("a", iB),
2028            Operand::new("c_out", i8).with_doc("Output carry flag"),
2029        ]),
2030    );
2031
2032    {
2033        let of_out = Operand::new("of", i8).with_doc("Overflow flag");
2034        ig.push(
2035            Inst::new(
2036                "uadd_overflow",
2037                r#"
2038            Add integers unsigned with overflow out.
2039            ``of`` is set when the addition overflowed.
2040            ```text
2041                a &= x + y \pmod 2^B \\
2042                of &= x+y >= 2^B
2043            ```
2044            Polymorphic over all scalar integer types, but does not support vector
2045            types.
2046            "#,
2047                &formats.binary,
2048            )
2049            .operands_in(vec![Operand::new("x", iB), Operand::new("y", iB)])
2050            .operands_out(vec![Operand::new("a", iB), of_out.clone()]),
2051        );
2052
2053        ig.push(
2054            Inst::new(
2055                "sadd_overflow",
2056                r#"
2057            Add integers signed with overflow out.
2058            ``of`` is set when the addition over- or underflowed.
2059            Polymorphic over all scalar integer types, but does not support vector
2060            types.
2061            "#,
2062                &formats.binary,
2063            )
2064            .operands_in(vec![Operand::new("x", iB), Operand::new("y", iB)])
2065            .operands_out(vec![Operand::new("a", iB), of_out.clone()]),
2066        );
2067
2068        ig.push(
2069            Inst::new(
2070                "usub_overflow",
2071                r#"
2072            Subtract integers unsigned with overflow out.
2073            ``of`` is set when the subtraction underflowed.
2074            ```text
2075                a &= x - y \pmod 2^B \\
2076                of &= x - y < 0
2077            ```
2078            Polymorphic over all scalar integer types, but does not support vector
2079            types.
2080            "#,
2081                &formats.binary,
2082            )
2083            .operands_in(vec![Operand::new("x", iB), Operand::new("y", iB)])
2084            .operands_out(vec![Operand::new("a", iB), of_out.clone()]),
2085        );
2086
2087        ig.push(
2088            Inst::new(
2089                "ssub_overflow",
2090                r#"
2091            Subtract integers signed with overflow out.
2092            ``of`` is set when the subtraction over- or underflowed.
2093            Polymorphic over all scalar integer types, but does not support vector
2094            types.
2095            "#,
2096                &formats.binary,
2097            )
2098            .operands_in(vec![Operand::new("x", iB), Operand::new("y", iB)])
2099            .operands_out(vec![Operand::new("a", iB), of_out.clone()]),
2100        );
2101
2102        {
2103            let NarrowScalar = &TypeVar::new(
2104                "NarrowScalar",
2105                "A scalar integer type up to 64 bits",
2106                TypeSetBuilder::new().ints(8..64).build(),
2107            );
2108
2109            ig.push(
2110                Inst::new(
2111                    "umul_overflow",
2112                    r#"
2113                Multiply integers unsigned with overflow out.
2114                ``of`` is set when the multiplication overflowed.
2115                ```text
2116                    a &= x * y \pmod 2^B \\
2117                    of &= x * y > 2^B
2118                ```
2119                Polymorphic over all scalar integer types except i128, but does not support vector
2120                types.
2121                "#,
2122                    &formats.binary,
2123                )
2124                .operands_in(vec![
2125                    Operand::new("x", NarrowScalar),
2126                    Operand::new("y", NarrowScalar),
2127                ])
2128                .operands_out(vec![Operand::new("a", NarrowScalar), of_out.clone()]),
2129            );
2130
2131            ig.push(
2132                Inst::new(
2133                    "smul_overflow",
2134                    r#"
2135                Multiply integers signed with overflow out.
2136                ``of`` is set when the multiplication over- or underflowed.
2137                Polymorphic over all scalar integer types except i128, but does not support vector
2138                types.
2139                "#,
2140                    &formats.binary,
2141                )
2142                .operands_in(vec![
2143                    Operand::new("x", NarrowScalar),
2144                    Operand::new("y", NarrowScalar),
2145                ])
2146                .operands_out(vec![Operand::new("a", NarrowScalar), of_out.clone()]),
2147            );
2148        }
2149    }
2150
2151    let i32_64 = &TypeVar::new(
2152        "i32_64",
2153        "A 32 or 64-bit scalar integer type",
2154        TypeSetBuilder::new().ints(32..64).build(),
2155    );
2156
2157    ig.push(
2158        Inst::new(
2159            "uadd_overflow_trap",
2160            r#"
2161        Unsigned addition of x and y, trapping if the result overflows.
2162
2163        Accepts 32 or 64-bit integers, and does not support vector types.
2164        "#,
2165            &formats.int_add_trap,
2166        )
2167        .operands_in(vec![
2168            Operand::new("x", i32_64),
2169            Operand::new("y", i32_64),
2170            Operand::new("code", &imm.trapcode),
2171        ])
2172        .operands_out(vec![Operand::new("a", i32_64)])
2173        .can_trap()
2174        .side_effects_idempotent(),
2175    );
2176
2177    ig.push(
2178        Inst::new(
2179            "ssub_overflow_bin",
2180            r#"
2181        Subtract signed integers with borrow in and overflow out.
2182
2183        Same as `ssub_overflow` with an additional borrow input. The `b_in` type
2184        is interpreted as 1 if it's nonzero or 0 if it's zero. The computation
2185        performed here is `x - (y + (b_in != 0))`.
2186        "#,
2187            &formats.ternary,
2188        )
2189        .operands_in(vec![
2190            Operand::new("x", iB),
2191            Operand::new("y", iB),
2192            Operand::new("b_in", i8).with_doc("Input borrow flag"),
2193        ])
2194        .operands_out(vec![
2195            Operand::new("a", iB),
2196            Operand::new("b_out", i8).with_doc("Output borrow flag"),
2197        ]),
2198    );
2199
2200    ig.push(
2201        Inst::new(
2202            "usub_overflow_bin",
2203            r#"
2204        Subtract unsigned integers with borrow in and overflow out.
2205
2206        Same as `usub_overflow` with an additional borrow input. The `b_in` type
2207        is interpreted as 1 if it's nonzero or 0 if it's zero. The computation
2208        performed here is `x - (y + (b_in != 0))`.
2209        "#,
2210            &formats.ternary,
2211        )
2212        .operands_in(vec![
2213            Operand::new("x", iB),
2214            Operand::new("y", iB),
2215            Operand::new("b_in", i8).with_doc("Input borrow flag"),
2216        ])
2217        .operands_out(vec![
2218            Operand::new("a", iB),
2219            Operand::new("b_out", i8).with_doc("Output borrow flag"),
2220        ]),
2221    );
2222
2223    let bits = &TypeVar::new(
2224        "bits",
2225        "Any integer, float, or vector type",
2226        TypeSetBuilder::new()
2227            .ints(Interval::All)
2228            .floats(Interval::All)
2229            .simd_lanes(Interval::All)
2230            .includes_scalars(true)
2231            .build(),
2232    );
2233
2234    ig.push(
2235        Inst::new(
2236            "band",
2237            r#"
2238        Bitwise and.
2239        "#,
2240            &formats.binary,
2241        )
2242        .operands_in(vec![Operand::new("x", bits), Operand::new("y", bits)])
2243        .operands_out(vec![Operand::new("a", bits)])
2244        .inst_builder_imm_method(true),
2245    );
2246
2247    ig.push(
2248        Inst::new(
2249            "bor",
2250            r#"
2251        Bitwise or.
2252        "#,
2253            &formats.binary,
2254        )
2255        .operands_in(vec![Operand::new("x", bits), Operand::new("y", bits)])
2256        .operands_out(vec![Operand::new("a", bits)])
2257        .inst_builder_imm_method(true),
2258    );
2259
2260    ig.push(
2261        Inst::new(
2262            "bxor",
2263            r#"
2264        Bitwise xor.
2265        "#,
2266            &formats.binary,
2267        )
2268        .operands_in(vec![Operand::new("x", bits), Operand::new("y", bits)])
2269        .operands_out(vec![Operand::new("a", bits)])
2270        .inst_builder_imm_method(true),
2271    );
2272
2273    ig.push(
2274        Inst::new(
2275            "bnot",
2276            r#"
2277        Bitwise not.
2278        "#,
2279            &formats.unary,
2280        )
2281        .operands_in(vec![Operand::new("x", bits)])
2282        .operands_out(vec![Operand::new("a", bits)]),
2283    );
2284
2285    ig.push(
2286        Inst::new(
2287            "band_not",
2288            r#"
2289        Bitwise and not.
2290
2291        Computes `x & ~y`.
2292        "#,
2293            &formats.binary,
2294        )
2295        .operands_in(vec![Operand::new("x", bits), Operand::new("y", bits)])
2296        .operands_out(vec![Operand::new("a", bits)]),
2297    );
2298
2299    ig.push(
2300        Inst::new(
2301            "bor_not",
2302            r#"
2303        Bitwise or not.
2304
2305        Computes `x | ~y`.
2306        "#,
2307            &formats.binary,
2308        )
2309        .operands_in(vec![Operand::new("x", bits), Operand::new("y", bits)])
2310        .operands_out(vec![Operand::new("a", bits)]),
2311    );
2312
2313    ig.push(
2314        Inst::new(
2315            "bxor_not",
2316            r#"
2317        Bitwise xor not.
2318
2319        Computes `x ^ ~y`.
2320        "#,
2321            &formats.binary,
2322        )
2323        .operands_in(vec![Operand::new("x", bits), Operand::new("y", bits)])
2324        .operands_out(vec![Operand::new("a", bits)]),
2325    );
2326
2327    ig.push(
2328        Inst::new(
2329            "rotl",
2330            r#"
2331        Rotate left.
2332
2333        Rotate the bits in ``x`` by ``y`` places.
2334        "#,
2335            &formats.binary,
2336        )
2337        .operands_in(vec![
2338            Operand::new("x", Int).with_doc("Scalar or vector value to shift"),
2339            Operand::new("y", iB).with_doc("Number of bits to shift"),
2340        ])
2341        .operands_out(vec![Operand::new("a", Int)])
2342        .inst_builder_imm_method(true),
2343    );
2344
2345    ig.push(
2346        Inst::new(
2347            "rotr",
2348            r#"
2349        Rotate right.
2350
2351        Rotate the bits in ``x`` by ``y`` places.
2352        "#,
2353            &formats.binary,
2354        )
2355        .operands_in(vec![
2356            Operand::new("x", Int).with_doc("Scalar or vector value to shift"),
2357            Operand::new("y", iB).with_doc("Number of bits to shift"),
2358        ])
2359        .operands_out(vec![Operand::new("a", Int)])
2360        .inst_builder_imm_method(true),
2361    );
2362
2363    ig.push(
2364        Inst::new(
2365            "ishl",
2366            r#"
2367        Integer shift left. Shift the bits in ``x`` towards the MSB by ``y``
2368        places. Shift in zero bits to the LSB.
2369
2370        The shift amount is masked to the size of ``x``.
2371
2372        When shifting a B-bits integer type, this instruction computes:
2373
2374        ```text
2375            s &:= y \pmod B,
2376            a &:= x \cdot 2^s \pmod{2^B}.
2377        ```
2378        "#,
2379            &formats.binary,
2380        )
2381        .operands_in(vec![
2382            Operand::new("x", Int).with_doc("Scalar or vector value to shift"),
2383            Operand::new("y", iB).with_doc("Number of bits to shift"),
2384        ])
2385        .operands_out(vec![Operand::new("a", Int)])
2386        .inst_builder_imm_method(true),
2387    );
2388
2389    ig.push(
2390        Inst::new(
2391            "ushr",
2392            r#"
2393        Unsigned shift right. Shift bits in ``x`` towards the LSB by ``y``
2394        places, shifting in zero bits to the MSB. Also called a *logical
2395        shift*.
2396
2397        The shift amount is masked to the size of ``x``.
2398
2399        When shifting a B-bits integer type, this instruction computes:
2400
2401        ```text
2402            s &:= y \pmod B,
2403            a &:= \lfloor x \cdot 2^{-s} \rfloor.
2404        ```
2405        "#,
2406            &formats.binary,
2407        )
2408        .operands_in(vec![
2409            Operand::new("x", Int).with_doc("Scalar or vector value to shift"),
2410            Operand::new("y", iB).with_doc("Number of bits to shift"),
2411        ])
2412        .operands_out(vec![Operand::new("a", Int)])
2413        .inst_builder_imm_method(true),
2414    );
2415
2416    ig.push(
2417        Inst::new(
2418            "sshr",
2419            r#"
2420        Signed shift right. Shift bits in ``x`` towards the LSB by ``y``
2421        places, shifting in sign bits to the MSB. Also called an *arithmetic
2422        shift*.
2423
2424        The shift amount is masked to the size of ``x``.
2425        "#,
2426            &formats.binary,
2427        )
2428        .operands_in(vec![
2429            Operand::new("x", Int).with_doc("Scalar or vector value to shift"),
2430            Operand::new("y", iB).with_doc("Number of bits to shift"),
2431        ])
2432        .operands_out(vec![Operand::new("a", Int)])
2433        .inst_builder_imm_method(true),
2434    );
2435
2436    ig.push(
2437        Inst::new(
2438            "bitrev",
2439            r#"
2440        Reverse the bits of a integer.
2441
2442        Reverses the bits in ``x``.
2443        "#,
2444            &formats.unary,
2445        )
2446        .operands_in(vec![Operand::new("x", iB)])
2447        .operands_out(vec![Operand::new("a", iB)]),
2448    );
2449
2450    ig.push(
2451        Inst::new(
2452            "clz",
2453            r#"
2454        Count leading zero bits.
2455
2456        Starting from the MSB in ``x``, count the number of zero bits before
2457        reaching the first one bit. When ``x`` is zero, returns the size of x
2458        in bits.
2459        "#,
2460            &formats.unary,
2461        )
2462        .operands_in(vec![Operand::new("x", iB)])
2463        .operands_out(vec![Operand::new("a", iB)]),
2464    );
2465
2466    ig.push(
2467        Inst::new(
2468            "cls",
2469            r#"
2470        Count leading sign bits.
2471
2472        Starting from the MSB after the sign bit in ``x``, count the number of
2473        consecutive bits identical to the sign bit. When ``x`` is 0 or -1,
2474        returns one less than the size of x in bits.
2475        "#,
2476            &formats.unary,
2477        )
2478        .operands_in(vec![Operand::new("x", iB)])
2479        .operands_out(vec![Operand::new("a", iB)]),
2480    );
2481
2482    ig.push(
2483        Inst::new(
2484            "ctz",
2485            r#"
2486        Count trailing zeros.
2487
2488        Starting from the LSB in ``x``, count the number of zero bits before
2489        reaching the first one bit. When ``x`` is zero, returns the size of x
2490        in bits.
2491        "#,
2492            &formats.unary,
2493        )
2494        .operands_in(vec![Operand::new("x", iB)])
2495        .operands_out(vec![Operand::new("a", iB)]),
2496    );
2497
2498    ig.push(
2499        Inst::new(
2500            "bswap",
2501            r#"
2502        Reverse the byte order of an integer.
2503
2504        Reverses the bytes in ``x``.
2505        "#,
2506            &formats.unary,
2507        )
2508        .operands_in(vec![Operand::new("x", iSwappable)])
2509        .operands_out(vec![Operand::new("a", iSwappable)]),
2510    );
2511
2512    ig.push(
2513        Inst::new(
2514            "popcnt",
2515            r#"
2516        Population count
2517
2518        Count the number of one bits in ``x``.
2519        "#,
2520            &formats.unary,
2521        )
2522        .operands_in(vec![Operand::new("x", Int)])
2523        .operands_out(vec![Operand::new("a", Int)]),
2524    );
2525
2526    let Float = &TypeVar::new(
2527        "Float",
2528        "A scalar or vector floating point number",
2529        TypeSetBuilder::new()
2530            .floats(Interval::All)
2531            .simd_lanes(Interval::All)
2532            .dynamic_simd_lanes(Interval::All)
2533            .build(),
2534    );
2535
2536    ig.push(
2537        Inst::new(
2538            "fcmp",
2539            r#"
2540        Floating point comparison.
2541
2542        Two IEEE 754-2008 floating point numbers, `x` and `y`, relate to each
2543        other in exactly one of four ways:
2544
2545        ```text
2546        == ==========================================
2547        UN Unordered when one or both numbers is NaN.
2548        EQ When `x = y`. (And `0.0 = -0.0`).
2549        LT When `x < y`.
2550        GT When `x > y`.
2551        == ==========================================
2552        ```
2553
2554        The 14 `floatcc` condition codes each correspond to a subset of
2555        the four relations, except for the empty set which would always be
2556        false, and the full set which would always be true.
2557
2558        The condition codes are divided into 7 'ordered' conditions which don't
2559        include UN, and 7 unordered conditions which all include UN.
2560
2561        ```text
2562        +-------+------------+---------+------------+-------------------------+
2563        |Ordered             |Unordered             |Condition                |
2564        +=======+============+=========+============+=========================+
2565        |ord    |EQ | LT | GT|uno      |UN          |NaNs absent / present.   |
2566        +-------+------------+---------+------------+-------------------------+
2567        |eq     |EQ          |ueq      |UN | EQ     |Equal                    |
2568        +-------+------------+---------+------------+-------------------------+
2569        |one    |LT | GT     |ne       |UN | LT | GT|Not equal                |
2570        +-------+------------+---------+------------+-------------------------+
2571        |lt     |LT          |ult      |UN | LT     |Less than                |
2572        +-------+------------+---------+------------+-------------------------+
2573        |le     |LT | EQ     |ule      |UN | LT | EQ|Less than or equal       |
2574        +-------+------------+---------+------------+-------------------------+
2575        |gt     |GT          |ugt      |UN | GT     |Greater than             |
2576        +-------+------------+---------+------------+-------------------------+
2577        |ge     |GT | EQ     |uge      |UN | GT | EQ|Greater than or equal    |
2578        +-------+------------+---------+------------+-------------------------+
2579        ```
2580
2581        The standard C comparison operators, `<, <=, >, >=`, are all ordered,
2582        so they are false if either operand is NaN. The C equality operator,
2583        `==`, is ordered, and since inequality is defined as the logical
2584        inverse it is *unordered*. They map to the `floatcc` condition
2585        codes as follows:
2586
2587        ```text
2588        ==== ====== ============
2589        C    `Cond` Subset
2590        ==== ====== ============
2591        `==` eq     EQ
2592        `!=` ne     UN | LT | GT
2593        `<`  lt     LT
2594        `<=` le     LT | EQ
2595        `>`  gt     GT
2596        `>=` ge     GT | EQ
2597        ==== ====== ============
2598        ```
2599
2600        This subset of condition codes also corresponds to the WebAssembly
2601        floating point comparisons of the same name.
2602
2603        When this instruction compares floating point vectors, it returns a
2604        vector with the results of lane-wise comparisons.
2605
2606        When comparing scalars, the result is:
2607            - `1` if the condition holds.
2608            - `0` if the condition does not hold.
2609
2610        When comparing vectors, the result is:
2611            - `-1` (i.e. all ones) in each lane where the condition holds.
2612            - `0` in each lane where the condition does not hold.
2613        "#,
2614            &formats.float_compare,
2615        )
2616        .operands_in(vec![
2617            Operand::new("Cond", &imm.floatcc),
2618            Operand::new("x", Float),
2619            Operand::new("y", Float),
2620        ])
2621        .operands_out(vec![Operand::new("a", &Float.as_truthy())]),
2622    );
2623
2624    ig.push(
2625        Inst::new(
2626            "fadd",
2627            r#"
2628        Floating point addition.
2629        "#,
2630            &formats.binary,
2631        )
2632        .operands_in(vec![Operand::new("x", Float), Operand::new("y", Float)])
2633        .operands_out(vec![
2634            Operand::new("a", Float).with_doc("Result of applying operator to each lane"),
2635        ]),
2636    );
2637
2638    ig.push(
2639        Inst::new(
2640            "fsub",
2641            r#"
2642        Floating point subtraction.
2643        "#,
2644            &formats.binary,
2645        )
2646        .operands_in(vec![Operand::new("x", Float), Operand::new("y", Float)])
2647        .operands_out(vec![
2648            Operand::new("a", Float).with_doc("Result of applying operator to each lane"),
2649        ]),
2650    );
2651
2652    ig.push(
2653        Inst::new(
2654            "fmul",
2655            r#"
2656        Floating point multiplication.
2657        "#,
2658            &formats.binary,
2659        )
2660        .operands_in(vec![Operand::new("x", Float), Operand::new("y", Float)])
2661        .operands_out(vec![
2662            Operand::new("a", Float).with_doc("Result of applying operator to each lane"),
2663        ]),
2664    );
2665
2666    ig.push(
2667        Inst::new(
2668            "fdiv",
2669            r#"
2670        Floating point division.
2671
2672        Unlike the integer division instructions ` and
2673        `udiv`, this can't trap. Division by zero is infinity or
2674        NaN, depending on the dividend.
2675        "#,
2676            &formats.binary,
2677        )
2678        .operands_in(vec![Operand::new("x", Float), Operand::new("y", Float)])
2679        .operands_out(vec![
2680            Operand::new("a", Float).with_doc("Result of applying operator to each lane"),
2681        ]),
2682    );
2683
2684    ig.push(
2685        Inst::new(
2686            "sqrt",
2687            r#"
2688        Floating point square root.
2689        "#,
2690            &formats.unary,
2691        )
2692        .operands_in(vec![Operand::new("x", Float)])
2693        .operands_out(vec![
2694            Operand::new("a", Float).with_doc("Result of applying operator to each lane"),
2695        ]),
2696    );
2697
2698    ig.push(
2699        Inst::new(
2700            "fma",
2701            r#"
2702        Floating point fused multiply-and-add.
2703
2704        Computes `a := xy+z` without any intermediate rounding of the
2705        product.
2706        "#,
2707            &formats.ternary,
2708        )
2709        .operands_in(vec![
2710            Operand::new("x", Float),
2711            Operand::new("y", Float),
2712            Operand::new("z", Float),
2713        ])
2714        .operands_out(vec![
2715            Operand::new("a", Float).with_doc("Result of applying operator to each lane"),
2716        ]),
2717    );
2718
2719    ig.push(
2720        Inst::new(
2721            "fneg",
2722            r#"
2723        Floating point negation.
2724
2725        Note that this is a pure bitwise operation.
2726        "#,
2727            &formats.unary,
2728        )
2729        .operands_in(vec![Operand::new("x", Float)])
2730        .operands_out(vec![
2731            Operand::new("a", Float).with_doc("``x`` with its sign bit inverted"),
2732        ]),
2733    );
2734
2735    ig.push(
2736        Inst::new(
2737            "fabs",
2738            r#"
2739        Floating point absolute value.
2740
2741        Note that this is a pure bitwise operation.
2742        "#,
2743            &formats.unary,
2744        )
2745        .operands_in(vec![Operand::new("x", Float)])
2746        .operands_out(vec![
2747            Operand::new("a", Float).with_doc("``x`` with its sign bit cleared"),
2748        ]),
2749    );
2750
2751    ig.push(
2752        Inst::new(
2753            "fcopysign",
2754            r#"
2755        Floating point copy sign.
2756
2757        Note that this is a pure bitwise operation. The sign bit from ``y`` is
2758        copied to the sign bit of ``x``.
2759        "#,
2760            &formats.binary,
2761        )
2762        .operands_in(vec![Operand::new("x", Float), Operand::new("y", Float)])
2763        .operands_out(vec![
2764            Operand::new("a", Float).with_doc("``x`` with its sign bit changed to that of ``y``"),
2765        ]),
2766    );
2767
2768    ig.push(
2769        Inst::new(
2770            "fmin",
2771            r#"
2772        Floating point minimum, propagating NaNs using the WebAssembly rules.
2773
2774        If either operand is NaN, this returns NaN with an unspecified sign. Furthermore, if
2775        each input NaN consists of a mantissa whose most significant bit is 1 and the rest is
2776        0, then the output has the same form. Otherwise, the output mantissa's most significant
2777        bit is 1 and the rest is unspecified.
2778        "#,
2779            &formats.binary,
2780        )
2781        .operands_in(vec![Operand::new("x", Float), Operand::new("y", Float)])
2782        .operands_out(vec![
2783            Operand::new("a", Float).with_doc("The smaller of ``x`` and ``y``"),
2784        ]),
2785    );
2786
2787    ig.push(
2788        Inst::new(
2789            "fmax",
2790            r#"
2791        Floating point maximum, propagating NaNs using the WebAssembly rules.
2792
2793        If either operand is NaN, this returns NaN with an unspecified sign. Furthermore, if
2794        each input NaN consists of a mantissa whose most significant bit is 1 and the rest is
2795        0, then the output has the same form. Otherwise, the output mantissa's most significant
2796        bit is 1 and the rest is unspecified.
2797        "#,
2798            &formats.binary,
2799        )
2800        .operands_in(vec![Operand::new("x", Float), Operand::new("y", Float)])
2801        .operands_out(vec![
2802            Operand::new("a", Float).with_doc("The larger of ``x`` and ``y``"),
2803        ]),
2804    );
2805
2806    ig.push(
2807        Inst::new(
2808            "ceil",
2809            r#"
2810        Round floating point round to integral, towards positive infinity.
2811        "#,
2812            &formats.unary,
2813        )
2814        .operands_in(vec![Operand::new("x", Float)])
2815        .operands_out(vec![
2816            Operand::new("a", Float).with_doc("``x`` rounded to integral value"),
2817        ]),
2818    );
2819
2820    ig.push(
2821        Inst::new(
2822            "floor",
2823            r#"
2824        Round floating point round to integral, towards negative infinity.
2825        "#,
2826            &formats.unary,
2827        )
2828        .operands_in(vec![Operand::new("x", Float)])
2829        .operands_out(vec![
2830            Operand::new("a", Float).with_doc("``x`` rounded to integral value"),
2831        ]),
2832    );
2833
2834    ig.push(
2835        Inst::new(
2836            "trunc",
2837            r#"
2838        Round floating point round to integral, towards zero.
2839        "#,
2840            &formats.unary,
2841        )
2842        .operands_in(vec![Operand::new("x", Float)])
2843        .operands_out(vec![
2844            Operand::new("a", Float).with_doc("``x`` rounded to integral value"),
2845        ]),
2846    );
2847
2848    ig.push(
2849        Inst::new(
2850            "nearest",
2851            r#"
2852        Round floating point round to integral, towards nearest with ties to
2853        even.
2854        "#,
2855            &formats.unary,
2856        )
2857        .operands_in(vec![Operand::new("x", Float)])
2858        .operands_out(vec![
2859            Operand::new("a", Float).with_doc("``x`` rounded to integral value"),
2860        ]),
2861    );
2862
2863    ig.push(
2864        Inst::new(
2865            "bitcast",
2866            r#"
2867        Reinterpret the bits in `x` as a different type.
2868
2869        The input and output types must be storable to memory and of the same
2870        size. A bitcast is equivalent to storing one type and loading the other
2871        type from the same address, both using the specified MemFlags.
2872
2873        Note that this operation only supports the `big` or `little` MemFlags.
2874        The specified byte order only affects the result in the case where
2875        input and output types differ in lane count/size.  In this case, the
2876        operation is only valid if a byte order specifier is provided.
2877        "#,
2878            &formats.load_no_offset,
2879        )
2880        .operands_in(vec![
2881            Operand::new("MemFlags", &imm.memflags),
2882            Operand::new("x", Mem),
2883        ])
2884        .operands_out(vec![
2885            Operand::new("a", MemTo).with_doc("Bits of `x` reinterpreted"),
2886        ]),
2887    );
2888
2889    ig.push(
2890        Inst::new(
2891            "scalar_to_vector",
2892            r#"
2893            Copies a scalar value to a vector value.  The scalar is copied into the
2894            least significant lane of the vector, and all other lanes will be zero.
2895            "#,
2896            &formats.unary,
2897        )
2898        .operands_in(vec![
2899            Operand::new("s", &TxN.lane_of()).with_doc("A scalar value"),
2900        ])
2901        .operands_out(vec![Operand::new("a", TxN).with_doc("A vector value")]),
2902    );
2903
2904    let Truthy = &TypeVar::new(
2905        "Truthy",
2906        "A scalar whose values are truthy",
2907        TypeSetBuilder::new().ints(Interval::All).build(),
2908    );
2909    let IntTo = &TypeVar::new(
2910        "IntTo",
2911        "An integer type",
2912        TypeSetBuilder::new().ints(Interval::All).build(),
2913    );
2914
2915    ig.push(
2916        Inst::new(
2917            "bmask",
2918            r#"
2919        Convert `x` to an integer mask.
2920
2921        Non-zero maps to all 1s and zero maps to all 0s.
2922        "#,
2923            &formats.unary,
2924        )
2925        .operands_in(vec![Operand::new("x", Truthy)])
2926        .operands_out(vec![Operand::new("a", IntTo)]),
2927    );
2928
2929    let Int = &TypeVar::new(
2930        "Int",
2931        "A scalar integer type",
2932        TypeSetBuilder::new().ints(Interval::All).build(),
2933    );
2934
2935    ig.push(
2936        Inst::new(
2937            "ireduce",
2938            r#"
2939        Convert `x` to a smaller integer type by discarding
2940        the most significant bits.
2941
2942        This is the same as reducing modulo `2^n`.
2943        "#,
2944            &formats.unary,
2945        )
2946        .operands_in(vec![
2947            Operand::new("x", &Int.wider())
2948                .with_doc("A scalar integer type, wider than the controlling type"),
2949        ])
2950        .operands_out(vec![Operand::new("a", Int)]),
2951    );
2952
2953    let I16or32or64xN = &TypeVar::new(
2954        "I16or32or64xN",
2955        "A SIMD vector type containing integer lanes 16, 32, or 64 bits wide",
2956        TypeSetBuilder::new()
2957            .ints(16..64)
2958            .simd_lanes(2..8)
2959            .dynamic_simd_lanes(2..8)
2960            .includes_scalars(false)
2961            .build(),
2962    );
2963
2964    ig.push(
2965        Inst::new(
2966            "snarrow",
2967            r#"
2968        Combine `x` and `y` into a vector with twice the lanes but half the integer width while
2969        saturating overflowing values to the signed maximum and minimum.
2970
2971        The lanes will be concatenated after narrowing. For example, when `x` and `y` are `i32x4`
2972        and `x = [x3, x2, x1, x0]` and `y = [y3, y2, y1, y0]`, then after narrowing the value
2973        returned is an `i16x8`: `a = [y3', y2', y1', y0', x3', x2', x1', x0']`.
2974            "#,
2975            &formats.binary,
2976        )
2977        .operands_in(vec![
2978            Operand::new("x", I16or32or64xN),
2979            Operand::new("y", I16or32or64xN),
2980        ])
2981        .operands_out(vec![Operand::new("a", &I16or32or64xN.split_lanes())]),
2982    );
2983
2984    ig.push(
2985        Inst::new(
2986            "unarrow",
2987            r#"
2988        Combine `x` and `y` into a vector with twice the lanes but half the integer width while
2989        saturating overflowing values to the unsigned maximum and minimum.
2990
2991        Note that all input lanes are considered signed: any negative lanes will overflow and be
2992        replaced with the unsigned minimum, `0x00`.
2993
2994        The lanes will be concatenated after narrowing. For example, when `x` and `y` are `i32x4`
2995        and `x = [x3, x2, x1, x0]` and `y = [y3, y2, y1, y0]`, then after narrowing the value
2996        returned is an `i16x8`: `a = [y3', y2', y1', y0', x3', x2', x1', x0']`.
2997            "#,
2998            &formats.binary,
2999        )
3000        .operands_in(vec![
3001            Operand::new("x", I16or32or64xN),
3002            Operand::new("y", I16or32or64xN),
3003        ])
3004        .operands_out(vec![Operand::new("a", &I16or32or64xN.split_lanes())]),
3005    );
3006
3007    ig.push(
3008        Inst::new(
3009            "uunarrow",
3010            r#"
3011        Combine `x` and `y` into a vector with twice the lanes but half the integer width while
3012        saturating overflowing values to the unsigned maximum and minimum.
3013
3014        Note that all input lanes are considered unsigned: any negative values will be interpreted as unsigned, overflowing and being replaced with the unsigned maximum.
3015
3016        The lanes will be concatenated after narrowing. For example, when `x` and `y` are `i32x4`
3017        and `x = [x3, x2, x1, x0]` and `y = [y3, y2, y1, y0]`, then after narrowing the value
3018        returned is an `i16x8`: `a = [y3', y2', y1', y0', x3', x2', x1', x0']`.
3019            "#,
3020            &formats.binary,
3021        )
3022        .operands_in(vec![Operand::new("x", I16or32or64xN), Operand::new("y", I16or32or64xN)])
3023        .operands_out(vec![Operand::new("a", &I16or32or64xN.split_lanes())]),
3024    );
3025
3026    let I8or16or32xN = &TypeVar::new(
3027        "I8or16or32xN",
3028        "A SIMD vector type containing integer lanes 8, 16, or 32 bits wide.",
3029        TypeSetBuilder::new()
3030            .ints(8..32)
3031            .simd_lanes(2..16)
3032            .dynamic_simd_lanes(2..16)
3033            .includes_scalars(false)
3034            .build(),
3035    );
3036
3037    ig.push(
3038        Inst::new(
3039            "swiden_low",
3040            r#"
3041        Widen the low lanes of `x` using signed extension.
3042
3043        This will double the lane width and halve the number of lanes.
3044            "#,
3045            &formats.unary,
3046        )
3047        .operands_in(vec![Operand::new("x", I8or16or32xN)])
3048        .operands_out(vec![Operand::new("a", &I8or16or32xN.merge_lanes())]),
3049    );
3050
3051    ig.push(
3052        Inst::new(
3053            "swiden_high",
3054            r#"
3055        Widen the high lanes of `x` using signed extension.
3056
3057        This will double the lane width and halve the number of lanes.
3058            "#,
3059            &formats.unary,
3060        )
3061        .operands_in(vec![Operand::new("x", I8or16or32xN)])
3062        .operands_out(vec![Operand::new("a", &I8or16or32xN.merge_lanes())]),
3063    );
3064
3065    ig.push(
3066        Inst::new(
3067            "uwiden_low",
3068            r#"
3069        Widen the low lanes of `x` using unsigned extension.
3070
3071        This will double the lane width and halve the number of lanes.
3072            "#,
3073            &formats.unary,
3074        )
3075        .operands_in(vec![Operand::new("x", I8or16or32xN)])
3076        .operands_out(vec![Operand::new("a", &I8or16or32xN.merge_lanes())]),
3077    );
3078
3079    ig.push(
3080        Inst::new(
3081            "uwiden_high",
3082            r#"
3083            Widen the high lanes of `x` using unsigned extension.
3084
3085            This will double the lane width and halve the number of lanes.
3086            "#,
3087            &formats.unary,
3088        )
3089        .operands_in(vec![Operand::new("x", I8or16or32xN)])
3090        .operands_out(vec![Operand::new("a", &I8or16or32xN.merge_lanes())]),
3091    );
3092
3093    ig.push(
3094        Inst::new(
3095            "iadd_pairwise",
3096            r#"
3097        Does lane-wise integer pairwise addition on two operands, putting the
3098        combined results into a single vector result. Here a pair refers to adjacent
3099        lanes in a vector, i.e. i*2 + (i*2+1) for i == num_lanes/2. The first operand
3100        pairwise add results will make up the low half of the resulting vector while
3101        the second operand pairwise add results will make up the upper half of the
3102        resulting vector.
3103            "#,
3104            &formats.binary,
3105        )
3106        .operands_in(vec![
3107            Operand::new("x", I8or16or32xN),
3108            Operand::new("y", I8or16or32xN),
3109        ])
3110        .operands_out(vec![Operand::new("a", I8or16or32xN)]),
3111    );
3112
3113    let I8x16 = &TypeVar::new(
3114        "I8x16",
3115        "A SIMD vector type consisting of 16 lanes of 8-bit integers",
3116        TypeSetBuilder::new()
3117            .ints(8..8)
3118            .simd_lanes(16..16)
3119            .includes_scalars(false)
3120            .build(),
3121    );
3122
3123    ig.push(
3124        Inst::new(
3125            "x86_pmaddubsw",
3126            r#"
3127        An instruction with equivalent semantics to `pmaddubsw` on x86.
3128
3129        This instruction will take signed bytes from the first argument and
3130        multiply them against unsigned bytes in the second argument. Adjacent
3131        pairs are then added, with saturating, to a 16-bit value and are packed
3132        into the result.
3133            "#,
3134            &formats.binary,
3135        )
3136        .operands_in(vec![Operand::new("x", I8x16), Operand::new("y", I8x16)])
3137        .operands_out(vec![Operand::new("a", I16x8)]),
3138    );
3139
3140    ig.push(
3141        Inst::new(
3142            "uextend",
3143            r#"
3144        Convert `x` to a larger integer type by zero-extending.
3145
3146        Each lane in `x` is converted to a larger integer type by adding
3147        zeroes. The result has the same numerical value as `x` when both are
3148        interpreted as unsigned integers.
3149
3150        The result type must have the same number of vector lanes as the input,
3151        and each lane must not have fewer bits that the input lanes. If the
3152        input and output types are the same, this is a no-op.
3153        "#,
3154            &formats.unary,
3155        )
3156        .operands_in(vec![Operand::new("x", &Int.narrower()).with_doc(
3157            "A scalar integer type, narrower than the controlling type",
3158        )])
3159        .operands_out(vec![Operand::new("a", Int)]),
3160    );
3161
3162    ig.push(
3163        Inst::new(
3164            "sextend",
3165            r#"
3166        Convert `x` to a larger integer type by sign-extending.
3167
3168        Each lane in `x` is converted to a larger integer type by replicating
3169        the sign bit. The result has the same numerical value as `x` when both
3170        are interpreted as signed integers.
3171
3172        The result type must have the same number of vector lanes as the input,
3173        and each lane must not have fewer bits that the input lanes. If the
3174        input and output types are the same, this is a no-op.
3175        "#,
3176            &formats.unary,
3177        )
3178        .operands_in(vec![Operand::new("x", &Int.narrower()).with_doc(
3179            "A scalar integer type, narrower than the controlling type",
3180        )])
3181        .operands_out(vec![Operand::new("a", Int)]),
3182    );
3183
3184    let FloatScalar = &TypeVar::new(
3185        "FloatScalar",
3186        "A scalar only floating point number",
3187        TypeSetBuilder::new().floats(Interval::All).build(),
3188    );
3189
3190    ig.push(
3191        Inst::new(
3192            "fpromote",
3193            r#"
3194        Convert `x` to a larger floating point format.
3195
3196        Each lane in `x` is converted to the destination floating point format.
3197        This is an exact operation.
3198
3199        Cranelift currently only supports two floating point formats
3200        - `f32` and `f64`. This may change in the future.
3201
3202        The result type must have the same number of vector lanes as the input,
3203        and the result lanes must not have fewer bits than the input lanes.
3204        "#,
3205            &formats.unary,
3206        )
3207        .operands_in(vec![Operand::new("x", &FloatScalar.narrower()).with_doc(
3208            "A scalar only floating point number, narrower than the controlling type",
3209        )])
3210        .operands_out(vec![Operand::new("a", FloatScalar)]),
3211    );
3212
3213    ig.push(
3214        Inst::new(
3215            "fdemote",
3216            r#"
3217        Convert `x` to a smaller floating point format.
3218
3219        Each lane in `x` is converted to the destination floating point format
3220        by rounding to nearest, ties to even.
3221
3222        Cranelift currently only supports two floating point formats
3223        - `f32` and `f64`. This may change in the future.
3224
3225        The result type must have the same number of vector lanes as the input,
3226        and the result lanes must not have more bits than the input lanes.
3227        "#,
3228            &formats.unary,
3229        )
3230        .operands_in(vec![Operand::new("x", &FloatScalar.wider()).with_doc(
3231            "A scalar only floating point number, wider than the controlling type",
3232        )])
3233        .operands_out(vec![Operand::new("a", FloatScalar)]),
3234    );
3235
3236    let F64x2 = &TypeVar::new(
3237        "F64x2",
3238        "A SIMD vector type consisting of 2 lanes of 64-bit floats",
3239        TypeSetBuilder::new()
3240            .floats(64..64)
3241            .simd_lanes(2..2)
3242            .includes_scalars(false)
3243            .build(),
3244    );
3245    let F32x4 = &TypeVar::new(
3246        "F32x4",
3247        "A SIMD vector type consisting of 4 lanes of 32-bit floats",
3248        TypeSetBuilder::new()
3249            .floats(32..32)
3250            .simd_lanes(4..4)
3251            .includes_scalars(false)
3252            .build(),
3253    );
3254
3255    ig.push(
3256        Inst::new(
3257            "fvdemote",
3258            r#"
3259                Convert `x` to a smaller floating point format.
3260
3261                Each lane in `x` is converted to the destination floating point format
3262                by rounding to nearest, ties to even.
3263
3264                Cranelift currently only supports two floating point formats
3265                - `f32` and `f64`. This may change in the future.
3266
3267                Fvdemote differs from fdemote in that with fvdemote it targets vectors.
3268                Fvdemote is constrained to having the input type being F64x2 and the result
3269                type being F32x4. The result lane that was the upper half of the input lane
3270                is initialized to zero.
3271                "#,
3272            &formats.unary,
3273        )
3274        .operands_in(vec![Operand::new("x", F64x2)])
3275        .operands_out(vec![Operand::new("a", F32x4)]),
3276    );
3277
3278    ig.push(
3279        Inst::new(
3280            "fvpromote_low",
3281            r#"
3282        Converts packed single precision floating point to packed double precision floating point.
3283
3284        Considering only the lower half of the register, the low lanes in `x` are interpreted as
3285        single precision floats that are then converted to a double precision floats.
3286
3287        The result type will have half the number of vector lanes as the input. Fvpromote_low is
3288        constrained to input F32x4 with a result type of F64x2.
3289        "#,
3290            &formats.unary,
3291        )
3292        .operands_in(vec![Operand::new("a", F32x4)])
3293        .operands_out(vec![Operand::new("x", F64x2)]),
3294    );
3295
3296    let IntTo = &TypeVar::new(
3297        "IntTo",
3298        "An scalar only integer type",
3299        TypeSetBuilder::new().ints(Interval::All).build(),
3300    );
3301
3302    ig.push(
3303        Inst::new(
3304            "fcvt_to_uint",
3305            r#"
3306        Converts floating point scalars to unsigned integer.
3307
3308        Only operates on `x` if it is a scalar. If `x` is NaN or if
3309        the unsigned integral value cannot be represented in the result
3310        type, this instruction traps.
3311
3312        "#,
3313            &formats.unary,
3314        )
3315        .operands_in(vec![Operand::new("x", FloatScalar)])
3316        .operands_out(vec![Operand::new("a", IntTo)])
3317        .can_trap()
3318        .side_effects_idempotent(),
3319    );
3320
3321    ig.push(
3322        Inst::new(
3323            "fcvt_to_sint",
3324            r#"
3325        Converts floating point scalars to signed integer.
3326
3327        Only operates on `x` if it is a scalar. If `x` is NaN or if
3328        the unsigned integral value cannot be represented in the result
3329        type, this instruction traps.
3330
3331        "#,
3332            &formats.unary,
3333        )
3334        .operands_in(vec![Operand::new("x", FloatScalar)])
3335        .operands_out(vec![Operand::new("a", IntTo)])
3336        .can_trap()
3337        .side_effects_idempotent(),
3338    );
3339
3340    let IntTo = &TypeVar::new(
3341        "IntTo",
3342        "A larger integer type with the same number of lanes",
3343        TypeSetBuilder::new()
3344            .ints(Interval::All)
3345            .simd_lanes(Interval::All)
3346            .build(),
3347    );
3348
3349    ig.push(
3350        Inst::new(
3351            "fcvt_to_uint_sat",
3352            r#"
3353        Convert floating point to unsigned integer as fcvt_to_uint does, but
3354        saturates the input instead of trapping. NaN and negative values are
3355        converted to 0.
3356        "#,
3357            &formats.unary,
3358        )
3359        .operands_in(vec![Operand::new("x", Float)])
3360        .operands_out(vec![Operand::new("a", IntTo)]),
3361    );
3362
3363    ig.push(
3364        Inst::new(
3365            "fcvt_to_sint_sat",
3366            r#"
3367        Convert floating point to signed integer as fcvt_to_sint does, but
3368        saturates the input instead of trapping. NaN values are converted to 0.
3369        "#,
3370            &formats.unary,
3371        )
3372        .operands_in(vec![Operand::new("x", Float)])
3373        .operands_out(vec![Operand::new("a", IntTo)]),
3374    );
3375
3376    ig.push(
3377        Inst::new(
3378            "x86_cvtt2dq",
3379            r#"
3380        A float-to-integer conversion instruction for vectors-of-floats which
3381        has the same semantics as `cvttp{s,d}2dq` on x86. This specifically
3382        returns `INT_MIN` for NaN or out-of-bounds lanes.
3383        "#,
3384            &formats.unary,
3385        )
3386        .operands_in(vec![Operand::new("x", Float)])
3387        .operands_out(vec![Operand::new("a", IntTo)]),
3388    );
3389
3390    let Int = &TypeVar::new(
3391        "Int",
3392        "A scalar or vector integer type",
3393        TypeSetBuilder::new()
3394            .ints(Interval::All)
3395            .simd_lanes(Interval::All)
3396            .build(),
3397    );
3398
3399    let FloatTo = &TypeVar::new(
3400        "FloatTo",
3401        "A scalar or vector floating point number",
3402        TypeSetBuilder::new()
3403            .floats(Interval::All)
3404            .simd_lanes(Interval::All)
3405            .build(),
3406    );
3407
3408    ig.push(
3409        Inst::new(
3410            "fcvt_from_uint",
3411            r#"
3412        Convert unsigned integer to floating point.
3413
3414        Each lane in `x` is interpreted as an unsigned integer and converted to
3415        floating point using round to nearest, ties to even.
3416
3417        The result type must have the same number of vector lanes as the input.
3418        "#,
3419            &formats.unary,
3420        )
3421        .operands_in(vec![Operand::new("x", Int)])
3422        .operands_out(vec![Operand::new("a", FloatTo)]),
3423    );
3424
3425    ig.push(
3426        Inst::new(
3427            "fcvt_from_sint",
3428            r#"
3429        Convert signed integer to floating point.
3430
3431        Each lane in `x` is interpreted as a signed integer and converted to
3432        floating point using round to nearest, ties to even.
3433
3434        The result type must have the same number of vector lanes as the input.
3435        "#,
3436            &formats.unary,
3437        )
3438        .operands_in(vec![Operand::new("x", Int)])
3439        .operands_out(vec![Operand::new("a", FloatTo)]),
3440    );
3441
3442    let WideInt = &TypeVar::new(
3443        "WideInt",
3444        "An integer type of width `i16` upwards",
3445        TypeSetBuilder::new().ints(16..128).build(),
3446    );
3447
3448    ig.push(
3449        Inst::new(
3450            "isplit",
3451            r#"
3452        Split an integer into low and high parts.
3453
3454        Vectors of integers are split lane-wise, so the results have the same
3455        number of lanes as the input, but the lanes are half the size.
3456
3457        Returns the low half of `x` and the high half of `x` as two independent
3458        values.
3459        "#,
3460            &formats.unary,
3461        )
3462        .operands_in(vec![Operand::new("x", WideInt)])
3463        .operands_out(vec![
3464            Operand::new("lo", &WideInt.half_width()).with_doc("The low bits of `x`"),
3465            Operand::new("hi", &WideInt.half_width()).with_doc("The high bits of `x`"),
3466        ]),
3467    );
3468
3469    ig.push(
3470        Inst::new(
3471            "iconcat",
3472            r#"
3473        Concatenate low and high bits to form a larger integer type.
3474
3475        Vectors of integers are concatenated lane-wise such that the result has
3476        the same number of lanes as the inputs, but the lanes are twice the
3477        size.
3478        "#,
3479            &formats.binary,
3480        )
3481        .operands_in(vec![
3482            Operand::new("lo", NarrowInt),
3483            Operand::new("hi", NarrowInt),
3484        ])
3485        .operands_out(vec![
3486            Operand::new("a", &NarrowInt.double_width())
3487                .with_doc("The concatenation of `lo` and `hi`"),
3488        ]),
3489    );
3490
3491    // Instructions relating to atomic memory accesses and fences
3492    let AtomicMem = &TypeVar::new(
3493        "AtomicMem",
3494        "Any type that can be stored in memory, which can be used in an atomic operation",
3495        TypeSetBuilder::new().ints(8..128).build(),
3496    );
3497
3498    ig.push(
3499        Inst::new(
3500            "atomic_rmw",
3501            r#"
3502        Atomically read-modify-write memory at `p`, with second operand `x`.  The old value is
3503        returned.  `p` has the type of the target word size, and `x` may be any integer type; note
3504        that some targets require specific target features to be enabled in order to support 128-bit
3505        integer atomics.  The type of the returned value is the same as the type of `x`.  This
3506        operation is sequentially consistent and creates happens-before edges that order normal
3507        (non-atomic) loads and stores.
3508        "#,
3509            &formats.atomic_rmw,
3510        )
3511        .operands_in(vec![
3512            Operand::new("MemFlags", &imm.memflags),
3513            Operand::new("AtomicRmwOp", &imm.atomic_rmw_op),
3514            Operand::new("p", iAddr),
3515            Operand::new("x", AtomicMem).with_doc("Value to be atomically stored"),
3516        ])
3517        .operands_out(vec![
3518            Operand::new("a", AtomicMem).with_doc("Value atomically loaded"),
3519        ])
3520        .can_load()
3521        .can_store()
3522        .other_side_effects(),
3523    );
3524
3525    ig.push(
3526        Inst::new(
3527            "atomic_cas",
3528            r#"
3529        Perform an atomic compare-and-swap operation on memory at `p`, with expected value `e`,
3530        storing `x` if the value at `p` equals `e`.  The old value at `p` is returned,
3531        regardless of whether the operation succeeds or fails.  `p` has the type of the target
3532        word size, and `x` and `e` must have the same type and the same size, which may be any
3533        integer type; note that some targets require specific target features to be enabled in order
3534        to support 128-bit integer atomics.  The type of the returned value is the same as the type
3535        of `x` and `e`.  This operation is sequentially consistent and creates happens-before edges
3536        that order normal (non-atomic) loads and stores.
3537        "#,
3538            &formats.atomic_cas,
3539        )
3540        .operands_in(vec![
3541            Operand::new("MemFlags", &imm.memflags),
3542            Operand::new("p", iAddr),
3543            Operand::new("e", AtomicMem).with_doc("Expected value in CAS"),
3544            Operand::new("x", AtomicMem).with_doc("Value to be atomically stored"),
3545        ])
3546        .operands_out(vec![
3547            Operand::new("a", AtomicMem).with_doc("Value atomically loaded"),
3548        ])
3549        .can_load()
3550        .can_store()
3551        .other_side_effects(),
3552    );
3553
3554    ig.push(
3555        Inst::new(
3556            "atomic_load",
3557            r#"
3558        Atomically load from memory at `p`.
3559
3560        This is a polymorphic instruction that can load any value type which has a memory
3561        representation.  It can only be used for integer types; note that some targets require
3562        specific target features to be enabled in order to support 128-bit integer atomics. This
3563        operation is sequentially consistent and creates happens-before edges that order normal
3564        (non-atomic) loads and stores.
3565        "#,
3566            &formats.load_no_offset,
3567        )
3568        .operands_in(vec![
3569            Operand::new("MemFlags", &imm.memflags),
3570            Operand::new("p", iAddr),
3571        ])
3572        .operands_out(vec![
3573            Operand::new("a", AtomicMem).with_doc("Value atomically loaded"),
3574        ])
3575        .can_load()
3576        .other_side_effects(),
3577    );
3578
3579    ig.push(
3580        Inst::new(
3581            "atomic_store",
3582            r#"
3583        Atomically store `x` to memory at `p`.
3584
3585        This is a polymorphic instruction that can store any value type with a memory
3586        representation.  It can only be used for integer types; note that some targets require
3587        specific target features to be enabled in order to support 128-bit integer atomics This
3588        operation is sequentially consistent and creates happens-before edges that order normal
3589        (non-atomic) loads and stores.
3590        "#,
3591            &formats.store_no_offset,
3592        )
3593        .operands_in(vec![
3594            Operand::new("MemFlags", &imm.memflags),
3595            Operand::new("x", AtomicMem).with_doc("Value to be atomically stored"),
3596            Operand::new("p", iAddr),
3597        ])
3598        .can_store()
3599        .other_side_effects(),
3600    );
3601
3602    ig.push(
3603        Inst::new(
3604            "fence",
3605            r#"
3606        A memory fence.  This must provide ordering to ensure that, at a minimum, neither loads
3607        nor stores of any kind may move forwards or backwards across the fence.  This operation
3608        is sequentially consistent.
3609        "#,
3610            &formats.nullary,
3611        )
3612        .other_side_effects(),
3613    );
3614
3615    let TxN = &TypeVar::new(
3616        "TxN",
3617        "A dynamic vector type",
3618        TypeSetBuilder::new()
3619            .ints(Interval::All)
3620            .floats(Interval::All)
3621            .dynamic_simd_lanes(Interval::All)
3622            .build(),
3623    );
3624
3625    ig.push(
3626        Inst::new(
3627            "extract_vector",
3628            r#"
3629        Return a fixed length sub vector, extracted from a dynamic vector.
3630        "#,
3631            &formats.binary_imm8,
3632        )
3633        .operands_in(vec![
3634            Operand::new("x", TxN).with_doc("The dynamic vector to extract from"),
3635            Operand::new("y", &imm.uimm8).with_doc("128-bit vector index"),
3636        ])
3637        .operands_out(vec![
3638            Operand::new("a", &TxN.dynamic_to_vector()).with_doc("New fixed vector"),
3639        ]),
3640    );
3641
3642    ig.push(
3643        Inst::new(
3644            "sequence_point",
3645            r#"
3646         A compiler barrier that acts as an immovable marker from IR input to machine-code output.
3647
3648         This "sequence point" can have debug tags attached to it, and these tags will be
3649         noted in the output `MachBuffer`.
3650
3651         It prevents motion of any other side-effects across this boundary.
3652         "#,
3653            &formats.nullary,
3654        )
3655        .other_side_effects(),
3656    );
3657}