Skip to main content

cranelift_codegen/machinst/
vcode.rs

1//! This implements the VCode container: a CFG of Insts that have been lowered.
2//!
3//! VCode is virtual-register code. An instruction in VCode is almost a machine
4//! instruction; however, its register slots can refer to virtual registers in
5//! addition to real machine registers.
6//!
7//! VCode is structured with traditional basic blocks, and
8//! each block must be terminated by an unconditional branch (one target), a
9//! conditional branch (two targets), or a return (no targets). Note that this
10//! slightly differs from the machine code of most ISAs: in most ISAs, a
11//! conditional branch has one target (and the not-taken case falls through).
12//! However, we expect that machine backends will elide branches to the following
13//! block (i.e., zero-offset jumps), and will be able to codegen a branch-cond /
14//! branch-uncond pair if *both* targets are not fallthrough. This allows us to
15//! play with layout prior to final binary emission, as well, if we want.
16//!
17//! See the main module comment in `mod.rs` for more details on the VCode-based
18//! backend pipeline.
19
20use crate::CodegenError;
21use crate::FxHashMap;
22use crate::ir::{self, Constant, ConstantData, ValueLabel, types};
23use crate::ranges::Ranges;
24use crate::timing;
25use crate::trace;
26use crate::{LabelValueLoc, ValueLocRange};
27use crate::{machinst::*, trace_log_enabled};
28use regalloc2::{
29    Edit, Function as RegallocFunction, InstOrEdit, InstPosition, InstRange, Operand,
30    OperandConstraint, OperandKind, PRegSet, ProgPoint, RegClass,
31};
32
33use crate::HashMap;
34use crate::hash_map::Entry;
35use core::cmp::Ordering;
36use core::fmt::{self, Write};
37use core::mem::take;
38use core::ops::Range;
39use cranelift_entity::{Keys, entity_impl};
40
41/// Index referring to an instruction in VCode.
42pub type InsnIndex = regalloc2::Inst;
43
44/// Extension trait for `InsnIndex` to allow conversion to a
45/// `BackwardsInsnIndex`.
46trait ToBackwardsInsnIndex {
47    fn to_backwards_insn_index(&self, num_insts: usize) -> BackwardsInsnIndex;
48}
49
50impl ToBackwardsInsnIndex for InsnIndex {
51    fn to_backwards_insn_index(&self, num_insts: usize) -> BackwardsInsnIndex {
52        BackwardsInsnIndex::new(num_insts - self.index() - 1)
53    }
54}
55
56/// An index referring to an instruction in the VCode when it is backwards,
57/// during VCode construction.
58#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
59#[cfg_attr(
60    feature = "enable-serde",
61    derive(::serde::Serialize, ::serde::Deserialize)
62)]
63pub struct BackwardsInsnIndex(InsnIndex);
64
65impl BackwardsInsnIndex {
66    pub fn new(i: usize) -> Self {
67        BackwardsInsnIndex(InsnIndex::new(i))
68    }
69}
70
71/// Index referring to a basic block in VCode.
72pub type BlockIndex = regalloc2::Block;
73
74/// VCodeInst wraps all requirements for a MachInst to be in VCode: it must be
75/// a `MachInst` and it must be able to emit itself at least to a `SizeCodeSink`.
76pub trait VCodeInst: MachInst + MachInstEmit {}
77impl<I: MachInst + MachInstEmit> VCodeInst for I {}
78
79/// A function in "VCode" (virtualized-register code) form, after
80/// lowering.  This is essentially a standard CFG of basic blocks,
81/// where each basic block consists of lowered instructions produced
82/// by the machine-specific backend.
83///
84/// Note that the VCode is immutable once produced, and is not
85/// modified by register allocation in particular. Rather, register
86/// allocation on the `VCode` produces a separate `regalloc2::Output`
87/// struct, and this can be passed to `emit`. `emit` in turn does not
88/// modify the vcode, but produces an `EmitResult`, which contains the
89/// machine code itself, and the associated disassembly and/or
90/// metadata as requested.
91pub struct VCode<I: VCodeInst> {
92    /// VReg IR-level types.
93    vreg_types: Vec<Type>,
94
95    /// Lowered machine instructions in order corresponding to the original IR.
96    insts: Vec<I>,
97
98    /// A map from backwards instruction index to the user stack map for that
99    /// instruction.
100    ///
101    /// This is a sparse side table that only has entries for instructions that
102    /// are safepoints, and only for a subset of those that have an associated
103    /// user stack map.
104    user_stack_maps: FxHashMap<BackwardsInsnIndex, ir::UserStackMap>,
105
106    /// A map from backwards instruction index to the debug tags for
107    /// that instruction. Each entry indexes a range in the
108    /// `debug_tag_pool`.
109    debug_tags: FxHashMap<BackwardsInsnIndex, Range<u32>>,
110
111    /// Pooled storage for sequences of debug tags; indexed by entries
112    /// in `debug_tags`.
113    debug_tag_pool: Vec<ir::DebugTag>,
114
115    /// Operands: pre-regalloc references to virtual registers with
116    /// constraints, in one flattened array. This allows the regalloc
117    /// to efficiently access all operands without requiring expensive
118    /// matches or method invocations on insts.
119    operands: Vec<Operand>,
120
121    /// Operand index ranges: for each instruction in `insts`, there
122    /// is a tuple here providing the range in `operands` for that
123    /// instruction's operands.
124    operand_ranges: Ranges,
125
126    /// Clobbers: a sparse map from instruction indices to clobber masks.
127    clobbers: FxHashMap<InsnIndex, PRegSet>,
128
129    /// Source locations for each instruction. (`SourceLoc` is a `u32`, so it is
130    /// reasonable to keep one of these per instruction.)
131    srclocs: Vec<RelSourceLoc>,
132
133    /// Entry block.
134    entry: BlockIndex,
135
136    /// Block instruction indices.
137    block_ranges: Ranges,
138
139    /// Block successors: index range in the `block_succs` list.
140    block_succ_range: Ranges,
141
142    /// Block successor lists, concatenated into one vec. The
143    /// `block_succ_range` list of tuples above gives (start, end)
144    /// ranges within this list that correspond to each basic block's
145    /// successors.
146    block_succs: Vec<regalloc2::Block>,
147
148    /// Block predecessors: index range in the `block_preds` list.
149    block_pred_range: Ranges,
150
151    /// Block predecessor lists, concatenated into one vec. The
152    /// `block_pred_range` list of tuples above gives (start, end)
153    /// ranges within this list that correspond to each basic block's
154    /// predecessors.
155    block_preds: Vec<regalloc2::Block>,
156
157    /// Block parameters: index range in `block_params` below.
158    block_params_range: Ranges,
159
160    /// Block parameter lists, concatenated into one vec. The
161    /// `block_params_range` list of tuples above gives (start, end)
162    /// ranges within this list that correspond to each basic block's
163    /// blockparam vregs.
164    block_params: Vec<regalloc2::VReg>,
165
166    /// Outgoing block arguments on branch instructions, concatenated
167    /// into one list.
168    ///
169    /// Note that this is conceptually a 3D array: we have a VReg list
170    /// per block, per successor. We flatten those three dimensions
171    /// into this 1D vec, then store index ranges in two levels of
172    /// indirection.
173    ///
174    /// Indexed by the indices in `branch_block_arg_succ_range`.
175    branch_block_args: Vec<regalloc2::VReg>,
176
177    /// Array of sequences of (start, end) tuples in
178    /// `branch_block_args`, one for each successor; these sequences
179    /// for each block are concatenated.
180    ///
181    /// Indexed by the indices in `branch_block_arg_succ_range`.
182    branch_block_arg_range: Ranges,
183
184    /// For a given block, indices in `branch_block_arg_range`
185    /// corresponding to all of its successors.
186    branch_block_arg_succ_range: Ranges,
187
188    /// Block-order information.
189    block_order: BlockLoweringOrder,
190
191    /// ABI object.
192    pub(crate) abi: Callee<I::ABIMachineSpec>,
193
194    /// Constant information used during code emission. This should be
195    /// immutable across function compilations within the same module.
196    emit_info: I::Info,
197
198    /// Constants.
199    pub(crate) constants: VCodeConstants,
200
201    /// Value labels for debuginfo attached to vregs.
202    debug_value_labels: Vec<(VReg, InsnIndex, InsnIndex, u32)>,
203
204    pub(crate) sigs: SigSet,
205
206    log2_min_function_alignment: u8,
207}
208
209/// The result of `VCode::emit`. Contains all information computed
210/// during emission: actual machine code, optionally a disassembly,
211/// and optionally metadata about the code layout.
212pub struct EmitResult {
213    /// The MachBuffer containing the machine code.
214    pub buffer: MachBufferFinalized,
215
216    /// Offset of each basic block, recorded during emission. Computed
217    /// only if `machine_code_cfg_info` is enabled.
218    pub bb_offsets: Vec<CodeOffset>,
219
220    /// Final basic-block edges, in terms of code offsets of
221    /// bb-starts. Computed only if `machine_code_cfg_info` is enabled.
222    pub bb_edges: Vec<(CodeOffset, CodeOffset)>,
223
224    /// The pretty-printed disassembly, if any. This uses the same
225    /// pretty-printing for MachInsts as the pre-regalloc VCode Debug
226    /// implementation, but additionally includes the prologue and
227    /// epilogue(s), and makes use of the regalloc results.
228    pub disasm: Option<String>,
229
230    /// Value-labels information (debug metadata).
231    pub value_labels_ranges: ValueLabelsRanges,
232}
233
234/// A builder for a VCode function body.
235///
236/// This builder has the ability to accept instructions in either
237/// forward or reverse order, depending on the pass direction that
238/// produces the VCode. The lowering from CLIF to VCode<MachInst>
239/// ordinarily occurs in reverse order (in order to allow instructions
240/// to be lowered only if used, and not merged) so a reversal will
241/// occur at the end of lowering to ensure the VCode is in machine
242/// order.
243///
244/// If built in reverse, block and instruction indices used once the
245/// VCode is built are relative to the final (reversed) order, not the
246/// order of construction. Note that this means we do not know the
247/// final block or instruction indices when building, so we do not
248/// hand them out. (The user is assumed to know them when appending
249/// terminator instructions with successor blocks.)
250pub struct VCodeBuilder<I: VCodeInst> {
251    /// In-progress VCode.
252    pub(crate) vcode: VCode<I>,
253
254    /// In what direction is the build occurring?
255    direction: VCodeBuildDirection,
256
257    /// Debug-value label in-progress map, keyed by label. For each
258    /// label, we keep disjoint ranges mapping to vregs. We'll flatten
259    /// this into (vreg, range, label) tuples when done.
260    debug_info: FxHashMap<ValueLabel, Vec<(InsnIndex, InsnIndex, VReg)>>,
261}
262
263/// Direction in which a VCodeBuilder builds VCode.
264#[derive(Clone, Copy, Debug, PartialEq, Eq)]
265pub enum VCodeBuildDirection {
266    // TODO: add `Forward` once we need it and can test it adequately.
267    /// Backward-build pass: we expect the producer to call `emit()`
268    /// with instructions in reverse program order within each block.
269    Backward,
270}
271
272impl<I: VCodeInst> VCodeBuilder<I> {
273    /// Create a new VCodeBuilder.
274    pub fn new(
275        sigs: SigSet,
276        abi: Callee<I::ABIMachineSpec>,
277        emit_info: I::Info,
278        block_order: BlockLoweringOrder,
279        constants: VCodeConstants,
280        direction: VCodeBuildDirection,
281        log2_min_function_alignment: u8,
282    ) -> Self {
283        let vcode = VCode::new(
284            sigs,
285            abi,
286            emit_info,
287            block_order,
288            constants,
289            log2_min_function_alignment,
290        );
291
292        VCodeBuilder {
293            vcode,
294            direction,
295            debug_info: FxHashMap::default(),
296        }
297    }
298
299    pub fn init_retval_area(&mut self, vregs: &mut VRegAllocator<I>) -> CodegenResult<()> {
300        self.vcode.abi.init_retval_area(&self.vcode.sigs, vregs)
301    }
302
303    /// Access the ABI object.
304    pub fn abi(&self) -> &Callee<I::ABIMachineSpec> {
305        &self.vcode.abi
306    }
307
308    /// Access the ABI object.
309    pub fn abi_mut(&mut self) -> &mut Callee<I::ABIMachineSpec> {
310        &mut self.vcode.abi
311    }
312
313    pub fn sigs(&self) -> &SigSet {
314        &self.vcode.sigs
315    }
316
317    pub fn sigs_mut(&mut self) -> &mut SigSet {
318        &mut self.vcode.sigs
319    }
320
321    /// Access to the BlockLoweringOrder object.
322    pub fn block_order(&self) -> &BlockLoweringOrder {
323        &self.vcode.block_order
324    }
325
326    /// Set the current block as the entry block.
327    pub fn set_entry(&mut self, block: BlockIndex) {
328        self.vcode.entry = block;
329    }
330
331    /// End the current basic block. Must be called after emitting vcode insts
332    /// for IR insts and prior to ending the function (building the VCode).
333    pub fn end_bb(&mut self) {
334        let end_idx = self.vcode.insts.len();
335        // Add the instruction index range to the list of blocks.
336        self.vcode.block_ranges.push_end(end_idx);
337        // End the successors list.
338        let succ_end = self.vcode.block_succs.len();
339        self.vcode.block_succ_range.push_end(succ_end);
340        // End the blockparams list.
341        let block_params_end = self.vcode.block_params.len();
342        self.vcode.block_params_range.push_end(block_params_end);
343        // End the branch blockparam args list.
344        let branch_block_arg_succ_end = self.vcode.branch_block_arg_range.len();
345        self.vcode
346            .branch_block_arg_succ_range
347            .push_end(branch_block_arg_succ_end);
348    }
349
350    pub fn add_block_param(&mut self, param: VirtualReg) {
351        self.vcode.block_params.push(param.into());
352    }
353
354    fn add_branch_args_for_succ(&mut self, args: &[Reg]) {
355        self.vcode
356            .branch_block_args
357            .extend(args.iter().map(|&arg| VReg::from(arg)));
358        let end = self.vcode.branch_block_args.len();
359        self.vcode.branch_block_arg_range.push_end(end);
360    }
361
362    /// Push an instruction for the current BB and current IR inst
363    /// within the BB.
364    pub fn push(&mut self, insn: I, loc: RelSourceLoc) {
365        assert!(!insn.is_low_level_branch()); // These are not meant to be in VCode.
366        self.vcode.insts.push(insn);
367        self.vcode.srclocs.push(loc);
368    }
369
370    /// Add a successor block with branch args.
371    pub fn add_succ(&mut self, block: BlockIndex, args: &[Reg]) {
372        self.vcode.block_succs.push(block);
373        self.add_branch_args_for_succ(args);
374    }
375
376    /// Add a debug value label to a register.
377    pub fn add_value_label(&mut self, reg: Reg, label: ValueLabel) {
378        // 1) In the reversed order, we consider the instructions
379        //    that define ranges in the "debug_info" array to refer
380        //    to the IP **after** them (when reversed):
381        //      IP[2]__| Inst 3 |
382        //      IP[1]__| Inst 2 |
383        //      IP[0]__| Inst 1 |
384        //             | Inst 0 |
385        //    This is so that we can represent IP[<function start>],
386        //    done at the cost of not being to represent IP[<function end>],
387        //    which is OK since no values will be live at that point.
388        // 2) The live range for "reg" begins at the current IP
389        //    and continues until the next, in execution order,
390        //    VReg that defines "label". Since the ranges are open
391        //    at the end, the subtraction of 1 cancels out:
392        //      [last..current IP] <=>
393        //      [last..last emitted inst index] <=>
394        //      [last..next_inst_index - 1] <=>
395        //      [last..next_inst_index)
396        //
397        let next_inst_index = self.vcode.insts.len();
398        if next_inst_index == 0 {
399            // This would produce a defective [0..0) range.
400            return;
401        }
402        let next_inst = InsnIndex::new(next_inst_index);
403        let labels = self.debug_info.entry(label).or_insert_with(|| vec![]);
404        let last = labels
405            .last()
406            .map(|(_start, end, _vreg)| *end)
407            .unwrap_or(InsnIndex::new(0));
408        labels.push((last, next_inst, reg.into()));
409    }
410
411    /// Access the constants.
412    pub fn constants(&mut self) -> &mut VCodeConstants {
413        &mut self.vcode.constants
414    }
415
416    fn compute_preds_from_succs(&mut self) {
417        // Do a linear-time counting sort: first determine how many
418        // times each block appears as a successor.
419        let mut starts = vec![0u32; self.vcode.num_blocks()];
420        for succ in &self.vcode.block_succs {
421            starts[succ.index()] += 1;
422        }
423
424        // Determine for each block the starting index where that
425        // block's predecessors should go. This is equivalent to the
426        // ranges we need to store in block_pred_range.
427        self.vcode.block_pred_range.reserve(starts.len());
428        let mut end = 0;
429        for count in starts.iter_mut() {
430            let start = end;
431            end += *count;
432            *count = start;
433            self.vcode.block_pred_range.push_end(end as usize);
434        }
435        let end = end as usize;
436        debug_assert_eq!(end, self.vcode.block_succs.len());
437
438        // Walk over the successors again, this time grouped by
439        // predecessor, and push the predecessor at the current
440        // starting position of each of its successors. We build
441        // each group of predecessors in whatever order Ranges::iter
442        // returns them; regalloc2 doesn't care.
443        self.vcode.block_preds.resize(end, BlockIndex::invalid());
444        for (pred, range) in self.vcode.block_succ_range.iter() {
445            let pred = BlockIndex::new(pred);
446            for succ in &self.vcode.block_succs[range] {
447                let pos = &mut starts[succ.index()];
448                self.vcode.block_preds[*pos as usize] = pred;
449                *pos += 1;
450            }
451        }
452        debug_assert!(self.vcode.block_preds.iter().all(|pred| pred.is_valid()));
453    }
454
455    /// Called once, when a build in Backward order is complete, to
456    /// perform the overall reversal (into final forward order) and
457    /// finalize metadata accordingly.
458    fn reverse_and_finalize(&mut self, vregs: &VRegAllocator<I>) {
459        let n_insts = self.vcode.insts.len();
460        if n_insts == 0 {
461            return;
462        }
463
464        // Reverse the per-block and per-inst sequences.
465        self.vcode.block_ranges.reverse_index();
466        self.vcode.block_ranges.reverse_target(n_insts);
467        // block_params_range is indexed by block (and blocks were
468        // traversed in reverse) so we reverse it; but block-param
469        // sequences in the concatenated vec can remain in reverse
470        // order (it is effectively an arena of arbitrarily-placed
471        // referenced sequences).
472        self.vcode.block_params_range.reverse_index();
473        // Likewise, we reverse block_succ_range, but the block_succ
474        // concatenated array can remain as-is.
475        self.vcode.block_succ_range.reverse_index();
476        self.vcode.insts.reverse();
477        self.vcode.srclocs.reverse();
478        // Likewise, branch_block_arg_succ_range is indexed by block
479        // so must be reversed.
480        self.vcode.branch_block_arg_succ_range.reverse_index();
481
482        // To translate an instruction index *endpoint* in reversed
483        // order to forward order, compute `n_insts - i`.
484        //
485        // Why not `n_insts - 1 - i`? That would be correct to
486        // translate an individual instruction index (for ten insts 0
487        // to 9 inclusive, inst 0 becomes 9, and inst 9 becomes
488        // 0). But for the usual inclusive-start, exclusive-end range
489        // idiom, inclusive starts become exclusive ends and
490        // vice-versa, so e.g. an (inclusive) start of 0 becomes an
491        // (exclusive) end of 10.
492        let translate = |inst: InsnIndex| InsnIndex::new(n_insts - inst.index());
493
494        // Generate debug-value labels based on per-label maps.
495        for (label, tuples) in &self.debug_info {
496            for &(start, end, vreg) in tuples {
497                let vreg = vregs.resolve_vreg_alias(vreg);
498                let fwd_start = translate(end);
499                let fwd_end = translate(start);
500                self.vcode
501                    .debug_value_labels
502                    .push((vreg, fwd_start, fwd_end, label.as_u32()));
503            }
504        }
505
506        // Now sort debug value labels by VReg, as required
507        // by regalloc2.
508        self.vcode
509            .debug_value_labels
510            .sort_unstable_by_key(|(vreg, _, _, _)| *vreg);
511    }
512
513    fn collect_operands(&mut self, vregs: &VRegAllocator<I>) {
514        let allocatable = PRegSet::from(self.vcode.abi.machine_env());
515        for (i, insn) in self.vcode.insts.iter_mut().enumerate() {
516            // Push operands from the instruction onto the operand list.
517            //
518            // We rename through the vreg alias table as we collect
519            // the operands. This is better than a separate post-pass
520            // over operands, because it has more cache locality:
521            // operands only need to pass through L1 once. This is
522            // also better than renaming instructions'
523            // operands/registers while lowering, because here we only
524            // need to do the `match` over the instruction to visit
525            // its register fields (which is slow, branchy code) once.
526
527            let mut op_collector =
528                OperandCollector::new(&mut self.vcode.operands, allocatable, |vreg| {
529                    vregs.resolve_vreg_alias(vreg)
530                });
531            insn.get_operands(&mut op_collector);
532            let (ops, clobbers) = op_collector.finish();
533            self.vcode.operand_ranges.push_end(ops);
534
535            if clobbers != PRegSet::default() {
536                self.vcode.clobbers.insert(InsnIndex::new(i), clobbers);
537            }
538
539            if let Some((dst, src)) = insn.is_move() {
540                // We should never see non-virtual registers present in move
541                // instructions.
542                assert!(
543                    src.is_virtual(),
544                    "the real register {src:?} was used as the source of a move instruction"
545                );
546                assert!(
547                    dst.to_reg().is_virtual(),
548                    "the real register {:?} was used as the destination of a move instruction",
549                    dst.to_reg()
550                );
551            }
552        }
553
554        // Translate blockparam args via the vreg aliases table as well.
555        for arg in &mut self.vcode.branch_block_args {
556            let new_arg = vregs.resolve_vreg_alias(*arg);
557            trace!("operandcollector: block arg {:?} -> {:?}", arg, new_arg);
558            *arg = new_arg;
559        }
560    }
561
562    /// Build the final VCode.
563    pub fn build(mut self, mut vregs: VRegAllocator<I>) -> VCode<I> {
564        self.vcode.vreg_types = take(&mut vregs.vreg_types);
565
566        if self.direction == VCodeBuildDirection::Backward {
567            self.reverse_and_finalize(&vregs);
568        }
569        self.collect_operands(&vregs);
570
571        self.compute_preds_from_succs();
572        self.vcode.debug_value_labels.sort_unstable();
573
574        // At this point, nothing in the vcode should mention any
575        // VReg which has been aliased. All the appropriate rewriting
576        // should have happened above. Just to be sure, let's
577        // double-check each field which has vregs.
578        // Note: can't easily check vcode.insts, resolved in collect_operands.
579        // Operands are resolved in collect_operands.
580        vregs.debug_assert_no_vreg_aliases(self.vcode.operands.iter().map(|op| op.vreg()));
581        // Currently block params are never aliased to another vreg.
582        vregs.debug_assert_no_vreg_aliases(self.vcode.block_params.iter().copied());
583        // Branch block args are resolved in collect_operands.
584        vregs.debug_assert_no_vreg_aliases(self.vcode.branch_block_args.iter().copied());
585        // Debug value labels are resolved in reverse_and_finalize.
586        vregs.debug_assert_no_vreg_aliases(
587            self.vcode.debug_value_labels.iter().map(|&(vreg, ..)| vreg),
588        );
589
590        self.vcode
591    }
592
593    /// Add a user stack map for the associated instruction.
594    pub fn add_user_stack_map(
595        &mut self,
596        inst: BackwardsInsnIndex,
597        entries: &[ir::UserStackMapEntry],
598    ) {
599        let stack_map = ir::UserStackMap::new(entries, self.vcode.abi.sized_stackslot_offsets());
600        let old_entry = self.vcode.user_stack_maps.insert(inst, stack_map);
601        debug_assert!(old_entry.is_none());
602    }
603
604    /// Add debug tags for the associated instruction.
605    pub fn add_debug_tags(&mut self, inst: BackwardsInsnIndex, entries: &[ir::DebugTag]) {
606        let start = u32::try_from(self.vcode.debug_tag_pool.len()).unwrap();
607        self.vcode.debug_tag_pool.extend(entries.iter().cloned());
608        let end = u32::try_from(self.vcode.debug_tag_pool.len()).unwrap();
609        self.vcode.debug_tags.insert(inst, start..end);
610    }
611}
612
613const NO_INST_OFFSET: CodeOffset = u32::MAX;
614
615impl<I: VCodeInst> VCode<I> {
616    /// New empty VCode.
617    fn new(
618        sigs: SigSet,
619        abi: Callee<I::ABIMachineSpec>,
620        emit_info: I::Info,
621        block_order: BlockLoweringOrder,
622        constants: VCodeConstants,
623        log2_min_function_alignment: u8,
624    ) -> Self {
625        let n_blocks = block_order.lowered_order().len();
626        VCode {
627            sigs,
628            vreg_types: vec![],
629            insts: Vec::with_capacity(10 * n_blocks),
630            user_stack_maps: FxHashMap::default(),
631            debug_tags: FxHashMap::default(),
632            debug_tag_pool: vec![],
633            operands: Vec::with_capacity(30 * n_blocks),
634            operand_ranges: Ranges::with_capacity(10 * n_blocks),
635            clobbers: FxHashMap::default(),
636            srclocs: Vec::with_capacity(10 * n_blocks),
637            entry: BlockIndex::new(0),
638            block_ranges: Ranges::with_capacity(n_blocks),
639            block_succ_range: Ranges::with_capacity(n_blocks),
640            block_succs: Vec::with_capacity(n_blocks),
641            block_pred_range: Ranges::default(),
642            block_preds: Vec::new(),
643            block_params_range: Ranges::with_capacity(n_blocks),
644            block_params: Vec::with_capacity(5 * n_blocks),
645            branch_block_args: Vec::with_capacity(10 * n_blocks),
646            branch_block_arg_range: Ranges::with_capacity(2 * n_blocks),
647            branch_block_arg_succ_range: Ranges::with_capacity(n_blocks),
648            block_order,
649            abi,
650            emit_info,
651            constants,
652            debug_value_labels: vec![],
653            log2_min_function_alignment,
654        }
655    }
656
657    /// Get the number of blocks. Block indices will be in the range `0 ..
658    /// (self.num_blocks() - 1)`.
659    pub fn num_blocks(&self) -> usize {
660        self.block_ranges.len()
661    }
662
663    /// The number of lowered instructions.
664    pub fn num_insts(&self) -> usize {
665        self.insts.len()
666    }
667
668    fn compute_clobbers_and_function_calls(
669        &self,
670        regalloc: &regalloc2::Output,
671    ) -> (Vec<Writable<RealReg>>, FunctionCalls) {
672        let mut clobbered = PRegSet::default();
673        let mut function_calls = FunctionCalls::None;
674
675        // All moves are included in clobbers.
676        for (_, Edit::Move { to, .. }) in &regalloc.edits {
677            if let Some(preg) = to.as_reg() {
678                clobbered.add(preg);
679            }
680        }
681
682        for (i, range) in self.operand_ranges.iter() {
683            let operands = &self.operands[range.clone()];
684            let allocs = &regalloc.allocs[range];
685            for (operand, alloc) in operands.iter().zip(allocs.iter()) {
686                if operand.kind() == OperandKind::Def {
687                    if let Some(preg) = alloc.as_reg() {
688                        clobbered.add(preg);
689                    }
690                }
691            }
692
693            function_calls.update(self.insts[i].call_type());
694
695            // Also add explicitly-clobbered registers.
696            //
697            // Skip merging this instruction's clobber list if not
698            // "included in clobbers" as per the MachInst. (Some
699            // backends use this to implement ABI specifics; e.g.,
700            // excluding calls of the same ABI as the current function
701            // from clobbers, because by definition everything
702            // clobbered by the call can be clobbered by this function
703            // without saving as well.
704            //
705            // This is important for a particular optimization: when
706            // some registers are "half-clobbered", e.g. vector/float
707            // registers on aarch64, we want them to be seen as
708            // clobbered by regalloc so it avoids carrying values
709            // across calls in these registers but not seen as
710            // clobbered by prologue generation here (because the
711            // actual half-clobber implied by the clobber list fits
712            // within the clobbers that we allow without
713            // clobber-saves).
714            if self.insts[i].is_included_in_clobbers() {
715                if let Some(&inst_clobbered) = self.clobbers.get(&InsnIndex::new(i)) {
716                    clobbered.union_from(inst_clobbered);
717                }
718            }
719        }
720
721        let clobbered_regs = clobbered
722            .into_iter()
723            .map(|preg| Writable::from_reg(RealReg::from(preg)))
724            .collect();
725
726        (clobbered_regs, function_calls)
727    }
728
729    /// Emit the instructions to a `MachBuffer`, containing fixed-up
730    /// code and external reloc/trap/etc. records ready for use. Takes
731    /// the regalloc results as well.
732    ///
733    /// Returns the machine code itself, and optionally metadata
734    /// and/or a disassembly, as an `EmitResult`. The `VCode` itself
735    /// is consumed by the emission process.
736    pub fn emit(
737        mut self,
738        regalloc: &regalloc2::Output,
739        want_disasm: bool,
740        flags: &settings::Flags,
741        ctrl_plane: &mut ControlPlane,
742    ) -> CodegenResult<EmitResult>
743    where
744        I: VCodeInst,
745    {
746        let _tt = timing::vcode_emit();
747        let mut buffer = MachBuffer::new();
748        buffer.set_log2_min_function_alignment(self.log2_min_function_alignment);
749        let mut bb_starts: Vec<Option<CodeOffset>> = vec![];
750
751        // The first M MachLabels are reserved for block indices.
752        buffer.reserve_labels_for_blocks(self.num_blocks());
753
754        // Register all allocated constants with the `MachBuffer` to ensure that
755        // any references to the constants during instructions can be handled
756        // correctly.
757        buffer.register_constants(&self.constants);
758
759        // Construct the final order we emit code in: cold blocks at the end.
760        let mut final_order: SmallVec<[BlockIndex; 16]> = smallvec![];
761        let mut cold_blocks: SmallVec<[BlockIndex; 16]> = smallvec![];
762        for block in 0..self.num_blocks() {
763            let block = BlockIndex::new(block);
764            if self.block_order.is_cold(block) {
765                cold_blocks.push(block);
766            } else {
767                final_order.push(block);
768            }
769        }
770        final_order.extend(cold_blocks.clone());
771
772        // Compute/save info we need for the prologue: clobbers and
773        // number of spillslots.
774        //
775        // We clone `abi` here because we will mutate it as we
776        // generate the prologue and set other info, but we can't
777        // mutate `VCode`. The info it usually carries prior to
778        // setting clobbers is fairly minimal so this should be
779        // relatively cheap.
780        let (clobbers, function_calls) = self.compute_clobbers_and_function_calls(regalloc);
781        self.abi.compute_frame_layout(
782            &self.sigs,
783            regalloc.num_spillslots,
784            clobbers,
785            function_calls,
786        )?;
787
788        // Emit blocks.
789        let mut cur_srcloc = None;
790        let mut last_offset = None;
791        let mut inst_offsets = vec![];
792        let mut state = I::State::new(&self.abi, core::mem::take(ctrl_plane));
793
794        let mut disasm = String::new();
795
796        if !self.debug_value_labels.is_empty() {
797            inst_offsets.resize(self.insts.len(), NO_INST_OFFSET);
798        }
799
800        // Count edits per block ahead of time; this is needed for
801        // lookahead island emission. (We could derive it per-block
802        // with binary search in the edit list, but it's more
803        // efficient to do it in one pass here.)
804        let mut ra_edits_per_block: SmallVec<[u32; 64]> = smallvec![];
805        let mut edit_idx = 0;
806        for block in 0..self.num_blocks() {
807            let end_inst = InsnIndex::new(self.block_ranges.get(block).end);
808            let start_edit_idx = edit_idx;
809            while edit_idx < regalloc.edits.len() && regalloc.edits[edit_idx].0.inst() < end_inst {
810                edit_idx += 1;
811            }
812            let end_edit_idx = edit_idx;
813            ra_edits_per_block.push((end_edit_idx - start_edit_idx) as u32);
814        }
815
816        let is_forward_edge_cfi_enabled = self.abi.is_forward_edge_cfi_enabled();
817        let mut bb_padding = match flags.bb_padding_log2_minus_one() {
818            0 => Vec::new(),
819            n => vec![0; 1 << (n - 1)],
820        };
821        let mut total_bb_padding = 0;
822
823        for &block in final_order.iter() {
824            trace!("emitting block {:?}", block);
825
826            // Call the new block hook for state
827            state.on_new_block();
828
829            // Emit NOPs to align the block.
830            let new_offset = I::align_basic_block(buffer.cur_offset());
831            while new_offset > buffer.cur_offset() {
832                // Pad with NOPs up to the aligned block offset.
833                let nop = I::gen_nop((new_offset - buffer.cur_offset()) as usize);
834                nop.emit(&mut buffer, &self.emit_info, &mut Default::default());
835            }
836            assert_eq!(buffer.cur_offset(), new_offset);
837
838            // The buffer maintains its deadline invariant per-`MachInst`:
839            // after each instruction, ensure that the worst-case end of any
840            // island the buffer might emit lies before the soonest deadline,
841            // even after the next instruction.
842            let emit_island_if_needed = |buffer: &mut MachBuffer<I>, state: &mut I::State| {
843                let lookahead = I::worst_case_size() + I::worst_case_island_growth();
844                if buffer.island_needed(lookahead) {
845                    let jump_around = buffer.get_label();
846                    I::gen_jump(jump_around).emit(buffer, &self.emit_info, state);
847                    buffer.emit_island(0, state.ctrl_plane_mut());
848                    buffer.bind_label(jump_around, state.ctrl_plane_mut());
849                }
850            };
851            let emit_inst = |inst: &I,
852                             disasm: &mut String,
853                             buffer: &mut MachBuffer<I>,
854                             state: &mut I::State| {
855                if want_disasm && !inst.is_args() {
856                    let mut s = state.clone();
857                    writeln!(disasm, "  {}", inst.pretty_print_inst(&mut s)).unwrap();
858                }
859                inst.emit(buffer, &self.emit_info, state);
860            };
861            let do_emit = |inst: &I,
862                           disasm: &mut String,
863                           buffer: &mut MachBuffer<I>,
864                           state: &mut I::State| {
865                emit_inst(inst, disasm, buffer, state);
866                emit_island_if_needed(buffer, state);
867            };
868
869            // Is this the first block? Emit the prologue directly if so.
870            if block == self.entry {
871                trace!(" -> entry block");
872                buffer.start_srcloc(Default::default());
873                for inst in &self.abi.gen_prologue() {
874                    do_emit(&inst, &mut disasm, &mut buffer, &mut state);
875                }
876                buffer.end_srcloc();
877            }
878
879            // Now emit the regular block body.
880
881            buffer.bind_label(MachLabel::from_block(block), state.ctrl_plane_mut());
882
883            if want_disasm {
884                writeln!(&mut disasm, "block{}:", block.index()).unwrap();
885            }
886
887            if flags.machine_code_cfg_info() {
888                // Track BB starts. If we have backed up due to MachBuffer
889                // branch opts, note that the removed blocks were removed.
890                let cur_offset = buffer.cur_offset();
891                if last_offset.is_some() && cur_offset <= last_offset.unwrap() {
892                    for i in (0..bb_starts.len()).rev() {
893                        if bb_starts[i].is_some() && cur_offset > bb_starts[i].unwrap() {
894                            break;
895                        }
896                        bb_starts[i] = None;
897                    }
898                }
899                bb_starts.push(Some(cur_offset));
900                last_offset = Some(cur_offset);
901            }
902
903            if let Some(block_start) = I::gen_block_start(
904                self.block_order.is_indirect_branch_target(block),
905                is_forward_edge_cfi_enabled,
906            ) {
907                do_emit(&block_start, &mut disasm, &mut buffer, &mut state);
908            }
909
910            for inst_or_edit in regalloc.block_insts_and_edits(&self, block) {
911                match inst_or_edit {
912                    InstOrEdit::Inst(iix) => {
913                        if !self.debug_value_labels.is_empty() {
914                            // If we need to produce debug info,
915                            // record the offset of each instruction
916                            // so that we can translate value-label
917                            // ranges to machine-code offsets.
918
919                            // Cold blocks violate monotonicity
920                            // assumptions elsewhere (that
921                            // instructions in inst-index order are in
922                            // order in machine code), so we omit
923                            // their offsets here. Value-label range
924                            // generation below will skip empty ranges
925                            // and ranges with to-offsets of zero.
926                            if !self.block_order.is_cold(block) {
927                                inst_offsets[iix.index()] = buffer.cur_offset();
928                            }
929                        }
930
931                        // Update the srcloc at this point in the buffer.
932                        let srcloc = self.srclocs[iix.index()];
933                        if cur_srcloc != Some(srcloc) {
934                            if cur_srcloc.is_some() {
935                                buffer.end_srcloc();
936                            }
937                            buffer.start_srcloc(srcloc);
938                            cur_srcloc = Some(srcloc);
939                        }
940
941                        // If this is a safepoint, compute a stack map
942                        // and pass it to the emit state.
943                        let stack_map_disasm = if self.insts[iix.index()].is_safepoint() {
944                            let (user_stack_map, user_stack_map_disasm) = {
945                                // The `user_stack_maps` is keyed by reverse
946                                // instruction index, so we must flip the
947                                // index. We can't put this into a helper method
948                                // due to borrowck issues because parts of
949                                // `self` are borrowed mutably elsewhere in this
950                                // function.
951                                let index = iix.to_backwards_insn_index(self.num_insts());
952                                let user_stack_map = self.user_stack_maps.remove(&index);
953                                let user_stack_map_disasm = if want_disasm {
954                                    user_stack_map.as_ref().map(|m| format!("  ; {m:?}"))
955                                } else {
956                                    None
957                                };
958                                (user_stack_map, user_stack_map_disasm)
959                            };
960
961                            state.pre_safepoint(user_stack_map);
962
963                            user_stack_map_disasm
964                        } else {
965                            None
966                        };
967
968                        // Place debug tags in the emission buffer
969                        // either at the offset prior to the
970                        // instruction or after the instruction,
971                        // depending on whether this is a call. See
972                        // the documentation on [`MachDebugTagPos`]
973                        // for details on why.
974                        let mut debug_tag_disasm = None;
975                        let mut place_debug_tags =
976                            |this: &VCode<I>, pos: MachDebugTagPos, buffer: &mut MachBuffer<I>| {
977                                // As above, translate the forward instruction
978                                // index to a backward index for the lookup.
979                                let debug_tag_range = {
980                                    let index = iix.to_backwards_insn_index(this.num_insts());
981                                    this.debug_tags.get(&index)
982                                };
983                                if let Some(range) = debug_tag_range {
984                                    let start = usize::try_from(range.start).unwrap();
985                                    let end = usize::try_from(range.end).unwrap();
986                                    let tags = &this.debug_tag_pool[start..end];
987
988                                    if want_disasm {
989                                        debug_tag_disasm =
990                                            Some(format!("  ; ^-- debug @ {pos:?}: {tags:?}"));
991                                    }
992                                    buffer.push_debug_tags(pos, tags);
993                                }
994                            };
995                        let debug_tag_pos =
996                            if self.insts[iix.index()].call_type() == CallType::Regular {
997                                MachDebugTagPos::Post
998                            } else {
999                                MachDebugTagPos::Pre
1000                            };
1001
1002                        if debug_tag_pos == MachDebugTagPos::Pre {
1003                            place_debug_tags(&self, debug_tag_pos, &mut buffer);
1004                        }
1005
1006                        // If the instruction we are about to emit is
1007                        // a return, place an epilogue at this point
1008                        // (and don't emit the return; the actual
1009                        // epilogue will contain it).
1010                        if self.insts[iix.index()].is_term() == MachTerminator::Ret {
1011                            log::trace!("emitting epilogue");
1012                            for inst in self.abi.gen_epilogue() {
1013                                do_emit(&inst, &mut disasm, &mut buffer, &mut state);
1014                            }
1015                        } else {
1016                            // Update the operands for this inst using the
1017                            // allocations from the regalloc result.
1018                            let mut allocs = regalloc.inst_allocs(iix).iter();
1019                            self.insts[iix.index()].get_operands(
1020                                &mut |reg: &mut Reg, constraint, _kind, _pos| {
1021                                    let alloc =
1022                                        allocs.next().expect("enough allocations for all operands");
1023
1024                                    if let Some(alloc) = alloc.as_reg() {
1025                                        let alloc: Reg = alloc.into();
1026                                        if let OperandConstraint::FixedReg(rreg) = constraint {
1027                                            debug_assert_eq!(Reg::from(rreg), alloc);
1028                                        }
1029                                        *reg = alloc;
1030                                    } else if let Some(alloc) = alloc.as_stack() {
1031                                        let alloc: Reg = alloc.into();
1032                                        *reg = alloc;
1033                                    }
1034                                },
1035                            );
1036                            debug_assert!(allocs.next().is_none());
1037
1038                            log::trace!("emitting: {:?}", self.insts[iix.index()]);
1039
1040                            // Emit the instruction!
1041                            emit_inst(
1042                                &self.insts[iix.index()],
1043                                &mut disasm,
1044                                &mut buffer,
1045                                &mut state,
1046                            );
1047
1048                            // Place post-position tags at the instruction's
1049                            // end, before any island that follows it.
1050                            if debug_tag_pos == MachDebugTagPos::Post {
1051                                place_debug_tags(&self, debug_tag_pos, &mut buffer);
1052                            }
1053                            emit_island_if_needed(&mut buffer, &mut state);
1054
1055                            if let Some(stack_map_disasm) = stack_map_disasm {
1056                                disasm.push_str(&stack_map_disasm);
1057                                disasm.push('\n');
1058                            }
1059                            if let Some(debug_tag_disasm) = debug_tag_disasm {
1060                                disasm.push_str(&debug_tag_disasm);
1061                                disasm.push('\n');
1062                            }
1063                        }
1064                    }
1065
1066                    InstOrEdit::Edit(Edit::Move { from, to }) => {
1067                        // Create a move/spill/reload instruction and
1068                        // immediately emit it.
1069                        match (from.as_reg(), to.as_reg()) {
1070                            (Some(from), Some(to)) => {
1071                                // Reg-to-reg move.
1072                                let from_rreg = Reg::from(from);
1073                                let to_rreg = Writable::from_reg(Reg::from(to));
1074                                debug_assert_eq!(from.class(), to.class());
1075                                let ty = I::canonical_type_for_rc(from.class());
1076                                let mv = I::gen_move(to_rreg, from_rreg, ty);
1077                                do_emit(&mv, &mut disasm, &mut buffer, &mut state);
1078                            }
1079                            (Some(from), None) => {
1080                                // Spill from register to spillslot.
1081                                let to = to.as_stack().unwrap();
1082                                let from_rreg = RealReg::from(from);
1083                                let spill = self.abi.gen_spill(to, from_rreg);
1084                                do_emit(&spill, &mut disasm, &mut buffer, &mut state);
1085                            }
1086                            (None, Some(to)) => {
1087                                // Load from spillslot to register.
1088                                let from = from.as_stack().unwrap();
1089                                let to_rreg = Writable::from_reg(RealReg::from(to));
1090                                let reload = self.abi.gen_reload(to_rreg, from);
1091                                do_emit(&reload, &mut disasm, &mut buffer, &mut state);
1092                            }
1093                            (None, None) => {
1094                                panic!("regalloc2 should have eliminated stack-to-stack moves!");
1095                            }
1096                        }
1097                    }
1098                }
1099            }
1100
1101            if cur_srcloc.is_some() {
1102                buffer.end_srcloc();
1103                cur_srcloc = None;
1104            }
1105
1106            // Insert padding, if configured, to stress the `MachBuffer`'s
1107            // relocation and island calculations.
1108            //
1109            // Padding can get quite large during fuzzing though so place a
1110            // total cap on it where when a per-function threshold is exceeded
1111            // the padding is turned back down to zero. This avoids a small-ish
1112            // test case generating a GB+ memory footprint in Cranelift for
1113            // example.
1114            if !bb_padding.is_empty() {
1115                // The padding bytes go directly into the buffer without
1116                // passing through `do_emit`, so check the deadline invariant
1117                // *before* writing them: if the padding would push the
1118                // worst-case end of an island past any pending deadline,
1119                // drain pending fixups via an island now. We're between
1120                // blocks, so no jump-around is needed.
1121                let padding_len = bb_padding.len() as u32 + I::LabelUse::ALIGN - 1;
1122                let lookahead = I::worst_case_size() + I::worst_case_island_growth();
1123                if buffer.island_needed(padding_len + lookahead) {
1124                    buffer.emit_island(padding_len + lookahead, ctrl_plane);
1125                }
1126
1127                buffer.put_data(&bb_padding);
1128                buffer.align_to(I::LabelUse::ALIGN);
1129                total_bb_padding += bb_padding.len();
1130                if total_bb_padding > (150 << 20) {
1131                    bb_padding = Vec::new();
1132                }
1133            }
1134        }
1135
1136        debug_assert!(
1137            self.user_stack_maps.is_empty(),
1138            "any stack maps should have been consumed by instruction emission, still have: {:#?}",
1139            self.user_stack_maps,
1140        );
1141
1142        // Do any optimizations on branches at tail of buffer, as if we had
1143        // bound one last label.
1144        buffer.optimize_branches(ctrl_plane);
1145
1146        // emission state is not needed anymore, move control plane back out
1147        *ctrl_plane = state.take_ctrl_plane();
1148
1149        let func_body_len = buffer.cur_offset();
1150
1151        // Create `bb_edges` and final (filtered) `bb_starts`.
1152        let mut bb_edges = vec![];
1153        let mut bb_offsets = vec![];
1154        if flags.machine_code_cfg_info() {
1155            for block in 0..self.num_blocks() {
1156                if bb_starts[block].is_none() {
1157                    // Block was deleted by MachBuffer; skip.
1158                    continue;
1159                }
1160                let from = bb_starts[block].unwrap();
1161
1162                bb_offsets.push(from);
1163                // Resolve each `succ` label and add edges.
1164                let succs = self.block_succs(BlockIndex::new(block));
1165                for &succ in succs.iter() {
1166                    let to = buffer.resolve_label_offset(MachLabel::from_block(succ));
1167                    bb_edges.push((from, to));
1168                }
1169            }
1170        }
1171
1172        self.monotonize_inst_offsets(&mut inst_offsets[..], func_body_len);
1173        let value_labels_ranges =
1174            self.compute_value_labels_ranges(regalloc, &inst_offsets[..], func_body_len);
1175
1176        // Store metadata about frame layout in the MachBuffer.
1177        buffer.set_frame_layout(self.abi.frame_slot_metadata());
1178
1179        Ok(EmitResult {
1180            buffer: buffer.finish(&self.constants, ctrl_plane),
1181            bb_offsets,
1182            bb_edges,
1183            disasm: if want_disasm { Some(disasm) } else { None },
1184            value_labels_ranges,
1185        })
1186    }
1187
1188    fn monotonize_inst_offsets(&self, inst_offsets: &mut [CodeOffset], func_body_len: u32) {
1189        if self.debug_value_labels.is_empty() {
1190            return;
1191        }
1192
1193        // During emission, branch removal can make offsets of instructions incorrect.
1194        // Consider the following sequence: [insi][jmp0][jmp1][jmp2][insj]
1195        // It will be recorded as (say):    [30]  [34]  [38]  [42]  [<would be 46>]
1196        // When the jumps get removed we are left with (in "inst_offsets"):
1197        // [insi][jmp0][jmp1][jmp2][insj][...]
1198        // [30]  [34]  [38]  [42]  [34]
1199        // Which violates the monotonicity invariant. This method sets offsets of these
1200        // removed instructions such as to make them appear zero-sized:
1201        // [insi][jmp0][jmp1][jmp2][insj][...]
1202        // [30]  [34]  [34]  [34]  [34]
1203        //
1204        let mut next_offset = func_body_len;
1205        for inst_index in (0..(inst_offsets.len() - 1)).rev() {
1206            let inst_offset = inst_offsets[inst_index];
1207
1208            // Not all instructions get their offsets recorded.
1209            if inst_offset == NO_INST_OFFSET {
1210                continue;
1211            }
1212
1213            if inst_offset > next_offset {
1214                trace!(
1215                    "Fixing code offset of the removed Inst {}: {} -> {}",
1216                    inst_index, inst_offset, next_offset
1217                );
1218                inst_offsets[inst_index] = next_offset;
1219                continue;
1220            }
1221
1222            next_offset = inst_offset;
1223        }
1224    }
1225
1226    fn compute_value_labels_ranges(
1227        &self,
1228        regalloc: &regalloc2::Output,
1229        inst_offsets: &[CodeOffset],
1230        func_body_len: u32,
1231    ) -> ValueLabelsRanges {
1232        if self.debug_value_labels.is_empty() {
1233            return ValueLabelsRanges::default();
1234        }
1235
1236        if trace_log_enabled!() {
1237            self.log_value_labels_ranges(regalloc, inst_offsets);
1238        }
1239
1240        let mut value_labels_ranges: ValueLabelsRanges = HashMap::new();
1241        for &(label, from, to, alloc) in &regalloc.debug_locations {
1242            let label = ValueLabel::from_u32(label);
1243            let ranges = value_labels_ranges.entry(label).or_insert_with(|| vec![]);
1244            let prog_point_to_inst = |prog_point: ProgPoint| {
1245                let mut inst = prog_point.inst();
1246                if prog_point.pos() == InstPosition::After {
1247                    inst = inst.next();
1248                }
1249                inst.index()
1250            };
1251            let inst_to_offset = |inst_index: usize| {
1252                // Skip over cold blocks.
1253                for offset in &inst_offsets[inst_index..] {
1254                    if *offset != NO_INST_OFFSET {
1255                        return *offset;
1256                    }
1257                }
1258                func_body_len
1259            };
1260            let from_inst_index = prog_point_to_inst(from);
1261            let to_inst_index = prog_point_to_inst(to);
1262            let from_offset = inst_to_offset(from_inst_index);
1263            let to_offset = inst_to_offset(to_inst_index);
1264
1265            // Empty ranges or unavailable offsets can happen
1266            // due to cold blocks and branch removal (see above).
1267            if from_offset == to_offset {
1268                continue;
1269            }
1270
1271            let loc = if let Some(preg) = alloc.as_reg() {
1272                LabelValueLoc::Reg(Reg::from(preg))
1273            } else {
1274                #[cfg(not(feature = "unwind"))]
1275                continue;
1276
1277                #[cfg(feature = "unwind")]
1278                {
1279                    let slot = alloc.as_stack().unwrap();
1280                    let slot_offset = self.abi.get_spillslot_offset(slot);
1281                    let slot_base_to_caller_sp_offset = self.abi.slot_base_to_caller_sp_offset();
1282                    let caller_sp_to_cfa_offset =
1283                        crate::isa::unwind::systemv::caller_sp_to_cfa_offset();
1284                    // NOTE: this is a negative offset because it's relative to the caller's SP
1285                    let cfa_to_sp_offset =
1286                        -((slot_base_to_caller_sp_offset + caller_sp_to_cfa_offset) as i64);
1287                    LabelValueLoc::CFAOffset(cfa_to_sp_offset + slot_offset)
1288                }
1289            };
1290
1291            // Coalesce adjacent ranges that for the same location
1292            // to minimize output size here and for the consumers.
1293            if let Some(last_loc_range) = ranges.last_mut() {
1294                if last_loc_range.loc == loc && last_loc_range.end == from_offset {
1295                    trace!(
1296                        "Extending debug range for {:?} in {:?} to Inst {} ({})",
1297                        label, loc, to_inst_index, to_offset
1298                    );
1299                    last_loc_range.end = to_offset;
1300                    continue;
1301                }
1302            }
1303
1304            trace!(
1305                "Recording debug range for {:?} in {:?}: [Inst {}..Inst {}) [{}..{})",
1306                label, loc, from_inst_index, to_inst_index, from_offset, to_offset
1307            );
1308
1309            ranges.push(ValueLocRange {
1310                loc,
1311                start: from_offset,
1312                end: to_offset,
1313            });
1314        }
1315
1316        value_labels_ranges
1317    }
1318
1319    fn log_value_labels_ranges(&self, regalloc: &regalloc2::Output, inst_offsets: &[CodeOffset]) {
1320        debug_assert!(trace_log_enabled!());
1321
1322        // What debug labels do we have? Note we'll skip those that have not been
1323        // allocated any location at all. They will show up as numeric gaps in the table.
1324        let mut labels = vec![];
1325        for &(label, _, _, _) in &regalloc.debug_locations {
1326            if Some(&label) == labels.last() {
1327                continue;
1328            }
1329            labels.push(label);
1330        }
1331
1332        // Reformat the data on what VRegs were the VLs assigned to by lowering, since
1333        // the array we have is sorted by VReg, and we want it sorted by VL for easy
1334        // access in the loop below.
1335        let mut vregs = vec![];
1336        for &(vreg, start, end, label) in &self.debug_value_labels {
1337            if matches!(labels.binary_search(&label), Ok(_)) {
1338                vregs.push((label, start, end, vreg));
1339            }
1340        }
1341        vregs.sort_unstable_by(
1342            |(l_label, l_start, _, _), (r_label, r_start, _, _)| match l_label.cmp(r_label) {
1343                Ordering::Equal => l_start.cmp(r_start),
1344                cmp => cmp,
1345            },
1346        );
1347
1348        #[derive(PartialEq)]
1349        enum Mode {
1350            Measure,
1351            Emit,
1352        }
1353        #[derive(PartialEq)]
1354        enum Row {
1355            Head,
1356            Line,
1357            Inst(usize, usize),
1358        }
1359
1360        let mut widths = vec![0; 3 + 2 * labels.len()];
1361        let mut row = String::new();
1362        let mut output_row = |row_kind: Row, mode: Mode| {
1363            let mut column_index = 0;
1364            row.clear();
1365
1366            macro_rules! output_cell_impl {
1367                ($fill:literal, $span:literal, $($cell_fmt:tt)*) => {
1368                    let column_start = row.len();
1369                    {
1370                        row.push('|');
1371                        write!(row, $($cell_fmt)*).unwrap();
1372                    }
1373
1374                    let next_column_index = column_index + $span;
1375                    let expected_width: usize = widths[column_index..next_column_index].iter().sum();
1376                    if mode == Mode::Measure {
1377                        let actual_width = row.len() - column_start;
1378                        if actual_width > expected_width {
1379                            widths[next_column_index - 1] += actual_width - expected_width;
1380                        }
1381                    } else {
1382                        let column_end = column_start + expected_width;
1383                        while row.len() != column_end {
1384                            row.push($fill);
1385                        }
1386                    }
1387                    column_index = next_column_index;
1388                };
1389            }
1390            macro_rules! output_cell {
1391                ($($cell_fmt:tt)*) => {
1392                    output_cell_impl!(' ', 1, $($cell_fmt)*);
1393                };
1394            }
1395
1396            match row_kind {
1397                Row::Head => {
1398                    output_cell!("BB");
1399                    output_cell!("Inst");
1400                    output_cell!("IP");
1401                    for label in &labels {
1402                        output_cell_impl!(' ', 2, "{:?}", ValueLabel::from_u32(*label));
1403                    }
1404                }
1405                Row::Line => {
1406                    debug_assert!(mode == Mode::Emit);
1407                    for _ in 0..3 {
1408                        output_cell_impl!('-', 1, "");
1409                    }
1410                    for _ in &labels {
1411                        output_cell_impl!('-', 2, "");
1412                    }
1413                }
1414                Row::Inst(block_index, inst_index) => {
1415                    debug_assert!(inst_index < self.num_insts());
1416                    if self.block_ranges.get(block_index).start == inst_index {
1417                        output_cell!("B{}", block_index);
1418                    } else {
1419                        output_cell!("");
1420                    }
1421                    output_cell!("Inst {inst_index} ");
1422                    output_cell!("{} ", inst_offsets[inst_index]);
1423
1424                    for label in &labels {
1425                        // First, the VReg.
1426                        use regalloc2::Inst;
1427                        let vreg_cmp = |inst: usize,
1428                                        vreg_label: &u32,
1429                                        range_start: &Inst,
1430                                        range_end: &Inst| {
1431                            match vreg_label.cmp(&label) {
1432                                Ordering::Equal => {
1433                                    if range_end.index() <= inst {
1434                                        Ordering::Less
1435                                    } else if range_start.index() > inst {
1436                                        Ordering::Greater
1437                                    } else {
1438                                        Ordering::Equal
1439                                    }
1440                                }
1441                                cmp => cmp,
1442                            }
1443                        };
1444                        let vreg_index =
1445                            vregs.binary_search_by(|(l, s, e, _)| vreg_cmp(inst_index, l, s, e));
1446                        if let Ok(vreg_index) = vreg_index {
1447                            let mut prev_vreg = None;
1448                            if inst_index > 0 {
1449                                let prev_vreg_index = vregs.binary_search_by(|(l, s, e, _)| {
1450                                    vreg_cmp(inst_index - 1, l, s, e)
1451                                });
1452                                if let Ok(prev_vreg_index) = prev_vreg_index {
1453                                    prev_vreg = Some(vregs[prev_vreg_index].3);
1454                                }
1455                            }
1456
1457                            let vreg = vregs[vreg_index].3;
1458                            if Some(vreg) == prev_vreg {
1459                                output_cell!("*");
1460                            } else {
1461                                output_cell!("{}", vreg);
1462                            }
1463                        } else {
1464                            output_cell!("");
1465                        }
1466
1467                        // Second, the allocated location.
1468                        let inst_prog_point = ProgPoint::before(Inst::new(inst_index));
1469                        let range_index = regalloc.debug_locations.binary_search_by(
1470                            |(range_label, range_start, range_end, _)| match range_label.cmp(label)
1471                            {
1472                                Ordering::Equal => {
1473                                    if *range_end <= inst_prog_point {
1474                                        Ordering::Less
1475                                    } else if *range_start > inst_prog_point {
1476                                        Ordering::Greater
1477                                    } else {
1478                                        Ordering::Equal
1479                                    }
1480                                }
1481                                cmp => cmp,
1482                            },
1483                        );
1484                        if let Ok(range_index) = range_index {
1485                            // Live at this instruction, print the location.
1486                            if let Some(reg) = regalloc.debug_locations[range_index].3.as_reg() {
1487                                output_cell!("{:?}", Reg::from(reg));
1488                            } else {
1489                                output_cell!("Stk");
1490                            }
1491                        } else {
1492                            // Not live at this instruction.
1493                            output_cell!("");
1494                        }
1495                    }
1496                }
1497            }
1498            row.push('|');
1499
1500            if mode == Mode::Emit {
1501                trace!("{}", row.as_str());
1502            }
1503        };
1504
1505        for block_index in 0..self.num_blocks() {
1506            for inst_index in self.block_ranges.get(block_index) {
1507                output_row(Row::Inst(block_index, inst_index), Mode::Measure);
1508            }
1509        }
1510        output_row(Row::Head, Mode::Measure);
1511
1512        output_row(Row::Head, Mode::Emit);
1513        output_row(Row::Line, Mode::Emit);
1514        for block_index in 0..self.num_blocks() {
1515            for inst_index in self.block_ranges.get(block_index) {
1516                output_row(Row::Inst(block_index, inst_index), Mode::Emit);
1517            }
1518        }
1519    }
1520
1521    /// Get the IR block for a BlockIndex, if one exists.
1522    pub fn bindex_to_bb(&self, block: BlockIndex) -> Option<ir::Block> {
1523        self.block_order.lowered_order()[block.index()].orig_block()
1524    }
1525
1526    /// Get the user stack map associated with the given forward instruction index.
1527    pub fn get_user_stack_map(&self, inst: InsnIndex) -> Option<&ir::UserStackMap> {
1528        let index = inst.to_backwards_insn_index(self.num_insts());
1529        self.user_stack_maps.get(&index)
1530    }
1531}
1532
1533impl<I: VCodeInst> core::ops::Index<InsnIndex> for VCode<I> {
1534    type Output = I;
1535    fn index(&self, idx: InsnIndex) -> &Self::Output {
1536        &self.insts[idx.index()]
1537    }
1538}
1539
1540impl<I: VCodeInst> RegallocFunction for VCode<I> {
1541    fn num_insts(&self) -> usize {
1542        self.insts.len()
1543    }
1544
1545    fn num_blocks(&self) -> usize {
1546        self.block_ranges.len()
1547    }
1548
1549    fn entry_block(&self) -> BlockIndex {
1550        self.entry
1551    }
1552
1553    fn block_insns(&self, block: BlockIndex) -> InstRange {
1554        let range = self.block_ranges.get(block.index());
1555        InstRange::new(InsnIndex::new(range.start), InsnIndex::new(range.end))
1556    }
1557
1558    fn block_succs(&self, block: BlockIndex) -> &[BlockIndex] {
1559        let range = self.block_succ_range.get(block.index());
1560        &self.block_succs[range]
1561    }
1562
1563    fn block_preds(&self, block: BlockIndex) -> &[BlockIndex] {
1564        let range = self.block_pred_range.get(block.index());
1565        &self.block_preds[range]
1566    }
1567
1568    fn block_params(&self, block: BlockIndex) -> &[VReg] {
1569        // As a special case we don't return block params for the entry block, as all the arguments
1570        // will be defined by the `Inst::Args` instruction.
1571        if block == self.entry {
1572            return &[];
1573        }
1574
1575        let range = self.block_params_range.get(block.index());
1576        &self.block_params[range]
1577    }
1578
1579    fn branch_blockparams(&self, block: BlockIndex, _insn: InsnIndex, succ_idx: usize) -> &[VReg] {
1580        let succ_range = self.branch_block_arg_succ_range.get(block.index());
1581        debug_assert!(succ_idx < succ_range.len());
1582        let branch_block_args = self.branch_block_arg_range.get(succ_range.start + succ_idx);
1583        &self.branch_block_args[branch_block_args]
1584    }
1585
1586    fn is_ret(&self, insn: InsnIndex) -> bool {
1587        match self.insts[insn.index()].is_term() {
1588            // We treat blocks terminated by an unconditional trap like a return for regalloc.
1589            MachTerminator::None => self.insts[insn.index()].is_trap(),
1590            MachTerminator::Ret | MachTerminator::RetCall => true,
1591            MachTerminator::Branch => false,
1592        }
1593    }
1594
1595    fn is_branch(&self, insn: InsnIndex) -> bool {
1596        match self.insts[insn.index()].is_term() {
1597            MachTerminator::Branch => true,
1598            _ => false,
1599        }
1600    }
1601
1602    fn inst_operands(&self, insn: InsnIndex) -> &[Operand] {
1603        let range = self.operand_ranges.get(insn.index());
1604        &self.operands[range]
1605    }
1606
1607    fn inst_clobbers(&self, insn: InsnIndex) -> PRegSet {
1608        self.clobbers.get(&insn).cloned().unwrap_or_default()
1609    }
1610
1611    fn num_vregs(&self) -> usize {
1612        self.vreg_types.len()
1613    }
1614
1615    fn debug_value_labels(&self) -> &[(VReg, InsnIndex, InsnIndex, u32)] {
1616        &self.debug_value_labels
1617    }
1618
1619    fn spillslot_size(&self, regclass: RegClass) -> usize {
1620        self.abi.get_spillslot_size(regclass) as usize
1621    }
1622
1623    fn allow_multiple_vreg_defs(&self) -> bool {
1624        // At least the s390x backend requires this, because the
1625        // `Loop` pseudo-instruction aggregates all Operands so pinned
1626        // vregs (RealRegs) may occur more than once.
1627        true
1628    }
1629}
1630
1631impl<I: VCodeInst> Debug for VRegAllocator<I> {
1632    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1633        writeln!(f, "VRegAllocator {{")?;
1634
1635        let mut alias_keys = self.vreg_aliases.keys().cloned().collect::<Vec<_>>();
1636        alias_keys.sort_unstable();
1637        for key in alias_keys {
1638            let dest = self.vreg_aliases.get(&key).unwrap();
1639            writeln!(f, "  {:?} := {:?}", Reg::from(key), Reg::from(*dest))?;
1640        }
1641
1642        writeln!(f, "}}")
1643    }
1644}
1645
1646impl<I: VCodeInst> fmt::Debug for VCode<I> {
1647    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1648        writeln!(f, "VCode {{")?;
1649        writeln!(f, "  Entry block: {}", self.entry.index())?;
1650
1651        let mut state = Default::default();
1652
1653        for block in 0..self.num_blocks() {
1654            let block = BlockIndex::new(block);
1655            writeln!(
1656                f,
1657                "Block {}({:?}):",
1658                block.index(),
1659                self.block_params(block)
1660            )?;
1661            if let Some(bb) = self.bindex_to_bb(block) {
1662                writeln!(f, "    (original IR block: {bb})")?;
1663            }
1664            for (succ_idx, succ) in self.block_succs(block).iter().enumerate() {
1665                writeln!(
1666                    f,
1667                    "    (successor: Block {}({:?}))",
1668                    succ.index(),
1669                    self.branch_blockparams(block, InsnIndex::new(0) /* dummy */, succ_idx)
1670                )?;
1671            }
1672            for inst in self.block_ranges.get(block.index()) {
1673                writeln!(
1674                    f,
1675                    "  Inst {}: {}",
1676                    inst,
1677                    self.insts[inst].pretty_print_inst(&mut state)
1678                )?;
1679                if let Some(user_stack_map) = self.get_user_stack_map(InsnIndex::new(inst)) {
1680                    writeln!(f, "    {user_stack_map:?}")?;
1681                }
1682            }
1683        }
1684
1685        writeln!(f, "}}")?;
1686        Ok(())
1687    }
1688}
1689
1690/// This structure manages VReg allocation during the lifetime of the VCodeBuilder.
1691pub struct VRegAllocator<I> {
1692    /// VReg IR-level types.
1693    vreg_types: Vec<Type>,
1694
1695    /// VReg aliases. When the final VCode is built we rewrite all
1696    /// uses of the keys in this table to their replacement values.
1697    ///
1698    /// We use these aliases to rename an instruction's expected
1699    /// result vregs to the returned vregs from lowering, which are
1700    /// usually freshly-allocated temps.
1701    vreg_aliases: FxHashMap<regalloc2::VReg, regalloc2::VReg>,
1702
1703    /// A deferred error, to be bubbled up to the top level of the
1704    /// lowering algorithm. We take this approach because we cannot
1705    /// currently propagate a `Result` upward through ISLE code (the
1706    /// lowering rules) or some ABI code.
1707    deferred_error: Option<CodegenError>,
1708
1709    /// The type of instruction that this allocator makes registers for.
1710    _inst: core::marker::PhantomData<I>,
1711}
1712
1713impl<I: VCodeInst> VRegAllocator<I> {
1714    /// Make a new VRegAllocator.
1715    pub fn with_capacity(capacity: usize) -> Self {
1716        let capacity = first_user_vreg_index() + capacity;
1717        let mut vreg_types = Vec::with_capacity(capacity);
1718        vreg_types.resize(first_user_vreg_index(), types::INVALID);
1719        Self {
1720            vreg_types,
1721            vreg_aliases: FxHashMap::with_capacity_and_hasher(capacity, Default::default()),
1722            deferred_error: None,
1723            _inst: core::marker::PhantomData::default(),
1724        }
1725    }
1726
1727    /// Allocate a fresh ValueRegs.
1728    pub fn alloc(&mut self, ty: Type) -> CodegenResult<ValueRegs<Reg>> {
1729        if self.deferred_error.is_some() {
1730            return Err(CodegenError::CodeTooLarge);
1731        }
1732        let v = self.vreg_types.len();
1733        let (regclasses, tys) = I::rc_for_type(&ty)?;
1734
1735        // Check that new indices are in-bounds for regalloc2's
1736        // VReg/Operand representation.
1737        if v + regclasses.len() > VReg::MAX {
1738            return Err(CodegenError::CodeTooLarge);
1739        }
1740
1741        // Check that new indices are in-bounds for our Reg
1742        // bit-packing on top of the RA2 types, which represents
1743        // spillslots as well.
1744        let check = |vreg: regalloc2::VReg| -> CodegenResult<Reg> {
1745            Reg::from_virtual_reg_checked(vreg).ok_or(CodegenError::CodeTooLarge)
1746        };
1747
1748        let regs: ValueRegs<Reg> = match regclasses {
1749            &[rc0] => ValueRegs::one(check(VReg::new(v, rc0))?),
1750            &[rc0, rc1] => ValueRegs::two(check(VReg::new(v, rc0))?, check(VReg::new(v + 1, rc1))?),
1751            // We can extend this if/when we support 32-bit targets; e.g.,
1752            // an i128 on a 32-bit machine will need up to four machine regs
1753            // for a `Value`.
1754            _ => panic!("Value must reside in 1 or 2 registers"),
1755        };
1756        for (&reg_ty, &reg) in tys.iter().zip(regs.regs().iter()) {
1757            let vreg = reg.to_virtual_reg().unwrap();
1758            debug_assert_eq!(self.vreg_types.len(), vreg.index());
1759            self.vreg_types.push(reg_ty);
1760        }
1761
1762        Ok(regs)
1763    }
1764
1765    /// Allocate a fresh ValueRegs, deferring any out-of-vregs
1766    /// errors. This is useful in places where we cannot bubble a
1767    /// `CodegenResult` upward easily, and which are known to be
1768    /// invoked from within the lowering loop that checks the deferred
1769    /// error status below.
1770    pub fn alloc_with_deferred_error(&mut self, ty: Type) -> ValueRegs<Reg> {
1771        match self.alloc(ty) {
1772            Ok(x) => x,
1773            Err(e) => {
1774                self.deferred_error = Some(e);
1775                self.bogus_for_deferred_error(ty)
1776            }
1777        }
1778    }
1779
1780    /// Take any deferred error that was accumulated by `alloc_with_deferred_error`.
1781    pub fn take_deferred_error(&mut self) -> Option<CodegenError> {
1782        self.deferred_error.take()
1783    }
1784
1785    /// Produce an bogus VReg placeholder with the proper number of
1786    /// registers for the given type. This is meant to be used with
1787    /// deferred allocation errors (see `Lower::alloc_tmp()`).
1788    fn bogus_for_deferred_error(&self, ty: Type) -> ValueRegs<Reg> {
1789        let (regclasses, _tys) = I::rc_for_type(&ty).expect("must have valid type");
1790        match regclasses {
1791            &[rc0] => ValueRegs::one(VReg::new(0, rc0).into()),
1792            &[rc0, rc1] => ValueRegs::two(VReg::new(0, rc0).into(), VReg::new(1, rc1).into()),
1793            _ => panic!("Value must reside in 1 or 2 registers"),
1794        }
1795    }
1796
1797    /// Rewrite any mention of `from` into `to`.
1798    pub fn set_vreg_alias(&mut self, from: Reg, to: Reg) {
1799        let from = from.into();
1800        let resolved_to = self.resolve_vreg_alias(to.into());
1801        // Disallow cycles (see below).
1802        assert_ne!(resolved_to, from);
1803
1804        let old_alias = self.vreg_aliases.insert(from, resolved_to);
1805        debug_assert_eq!(old_alias, None);
1806    }
1807
1808    fn resolve_vreg_alias(&self, mut vreg: regalloc2::VReg) -> regalloc2::VReg {
1809        // We prevent cycles from existing by resolving targets of
1810        // aliases eagerly before setting them. If the target resolves
1811        // to the origin of the alias, then a cycle would be created
1812        // and the alias is disallowed. Because of the structure of
1813        // SSA code (one instruction can refer to another's defs but
1814        // not vice-versa, except indirectly through
1815        // phis/blockparams), cycles should not occur as we use
1816        // aliases to redirect vregs to the temps that actually define
1817        // them.
1818        while let Some(to) = self.vreg_aliases.get(&vreg) {
1819            vreg = *to;
1820        }
1821        vreg
1822    }
1823
1824    #[inline]
1825    fn debug_assert_no_vreg_aliases(&self, mut list: impl Iterator<Item = VReg>) {
1826        debug_assert!(list.all(|vreg| !self.vreg_aliases.contains_key(&vreg)));
1827    }
1828}
1829
1830/// This structure tracks the large constants used in VCode that will be emitted separately by the
1831/// [MachBuffer].
1832///
1833/// First, during the lowering phase, constants are inserted using
1834/// [VCodeConstants.insert]; an intermediate handle, `VCodeConstant`, tracks what constants are
1835/// used in this phase. Some deduplication is performed, when possible, as constant
1836/// values are inserted.
1837///
1838/// Secondly, during the emission phase, the [MachBuffer] assigns [MachLabel]s for each of the
1839/// constants so that instructions can refer to the value's memory location. The [MachBuffer]
1840/// then writes the constant values to the buffer.
1841#[derive(Default)]
1842pub struct VCodeConstants {
1843    constants: PrimaryMap<VCodeConstant, VCodeConstantData>,
1844    pool_uses: HashMap<Constant, VCodeConstant>,
1845    well_known_uses: HashMap<*const [u8], VCodeConstant>,
1846    u64s: HashMap<[u8; 8], VCodeConstant>,
1847}
1848impl VCodeConstants {
1849    /// Initialize the structure with the expected number of constants.
1850    pub fn with_capacity(expected_num_constants: usize) -> Self {
1851        Self {
1852            constants: PrimaryMap::with_capacity(expected_num_constants),
1853            pool_uses: HashMap::with_capacity(expected_num_constants),
1854            well_known_uses: HashMap::new(),
1855            u64s: HashMap::new(),
1856        }
1857    }
1858
1859    /// Insert a constant; using this method indicates that a constant value will be used and thus
1860    /// will be emitted to the `MachBuffer`. The current implementation can deduplicate constants
1861    /// that are [VCodeConstantData::Pool] or [VCodeConstantData::WellKnown] but not
1862    /// [VCodeConstantData::Generated].
1863    pub fn insert(&mut self, data: VCodeConstantData) -> VCodeConstant {
1864        match data {
1865            VCodeConstantData::Generated(_) => self.constants.push(data),
1866            VCodeConstantData::Pool(constant, _) => match self.pool_uses.get(&constant) {
1867                None => {
1868                    let vcode_constant = self.constants.push(data);
1869                    self.pool_uses.insert(constant, vcode_constant);
1870                    vcode_constant
1871                }
1872                Some(&vcode_constant) => vcode_constant,
1873            },
1874            VCodeConstantData::WellKnown(data_ref) => {
1875                match self.well_known_uses.entry(data_ref as *const [u8]) {
1876                    Entry::Vacant(v) => {
1877                        let vcode_constant = self.constants.push(data);
1878                        v.insert(vcode_constant);
1879                        vcode_constant
1880                    }
1881                    Entry::Occupied(o) => *o.get(),
1882                }
1883            }
1884            VCodeConstantData::U64(value) => match self.u64s.entry(value) {
1885                Entry::Vacant(v) => {
1886                    let vcode_constant = self.constants.push(data);
1887                    v.insert(vcode_constant);
1888                    vcode_constant
1889                }
1890                Entry::Occupied(o) => *o.get(),
1891            },
1892        }
1893    }
1894
1895    /// Return the number of constants inserted.
1896    pub fn len(&self) -> usize {
1897        self.constants.len()
1898    }
1899
1900    /// Iterate over the `VCodeConstant` keys inserted in this structure.
1901    pub fn keys(&self) -> Keys<VCodeConstant> {
1902        self.constants.keys()
1903    }
1904
1905    /// Iterate over the `VCodeConstant` keys and the data (as a byte slice) inserted in this
1906    /// structure.
1907    pub fn iter(&self) -> impl Iterator<Item = (VCodeConstant, &VCodeConstantData)> {
1908        self.constants.iter()
1909    }
1910
1911    /// Returns the data associated with the specified constant.
1912    pub fn get(&self, c: VCodeConstant) -> &VCodeConstantData {
1913        &self.constants[c]
1914    }
1915
1916    /// Checks if the given [VCodeConstantData] is registered as
1917    /// used by the pool.
1918    pub fn pool_uses(&self, constant: &VCodeConstantData) -> bool {
1919        match constant {
1920            VCodeConstantData::Pool(c, _) => self.pool_uses.contains_key(c),
1921            _ => false,
1922        }
1923    }
1924}
1925
1926/// A use of a constant by one or more VCode instructions; see [VCodeConstants].
1927#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1928pub struct VCodeConstant(u32);
1929entity_impl!(VCodeConstant);
1930
1931/// Identify the different types of constant that can be inserted into [VCodeConstants]. Tracking
1932/// these separately instead of as raw byte buffers allows us to avoid some duplication.
1933pub enum VCodeConstantData {
1934    /// A constant already present in the Cranelift IR
1935    /// [ConstantPool](crate::ir::constant::ConstantPool).
1936    Pool(Constant, ConstantData),
1937    /// A reference to a well-known constant value that is statically encoded within the compiler.
1938    WellKnown(&'static [u8]),
1939    /// A constant value generated during lowering; the value may depend on the instruction context
1940    /// which makes it difficult to de-duplicate--if possible, use other variants.
1941    Generated(ConstantData),
1942    /// A constant of at most 64 bits. These are deduplicated as
1943    /// well. Stored as a fixed-size array of `u8` so that we do not
1944    /// encounter endianness problems when cross-compiling.
1945    U64([u8; 8]),
1946}
1947impl VCodeConstantData {
1948    /// Retrieve the constant data as a byte slice.
1949    pub fn as_slice(&self) -> &[u8] {
1950        match self {
1951            VCodeConstantData::Pool(_, d) | VCodeConstantData::Generated(d) => d.as_slice(),
1952            VCodeConstantData::WellKnown(d) => d,
1953            VCodeConstantData::U64(value) => &value[..],
1954        }
1955    }
1956
1957    /// Calculate the alignment of the constant data.
1958    pub fn alignment(&self) -> u32 {
1959        if self.as_slice().len() <= 8 { 8 } else { 16 }
1960    }
1961}
1962
1963#[cfg(test)]
1964mod test {
1965    use super::*;
1966    use core::mem::size_of;
1967
1968    #[test]
1969    fn size_of_constant_structs() {
1970        assert_eq!(size_of::<Constant>(), 4);
1971        assert_eq!(size_of::<VCodeConstant>(), 4);
1972        assert_eq!(size_of::<ConstantData>(), 3 * size_of::<usize>());
1973        assert_eq!(size_of::<VCodeConstantData>(), 4 * size_of::<usize>());
1974        assert_eq!(
1975            size_of::<PrimaryMap<VCodeConstant, VCodeConstantData>>(),
1976            3 * size_of::<usize>()
1977        );
1978        // TODO The VCodeConstants structure's memory size could be further optimized.
1979        // With certain versions of Rust, each `HashMap` in `VCodeConstants` occupied at
1980        // least 48 bytes, making an empty `VCodeConstants` cost 120 bytes.
1981    }
1982}