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<Stencil>,
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    ) -> 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            let do_emit = |inst: &I,
839                           disasm: &mut String,
840                           buffer: &mut MachBuffer<I>,
841                           state: &mut I::State| {
842                if want_disasm && !inst.is_args() {
843                    let mut s = state.clone();
844                    writeln!(disasm, "  {}", inst.pretty_print_inst(&mut s)).unwrap();
845                }
846                inst.emit(buffer, &self.emit_info, state);
847                // The buffer maintains its deadline invariant per-`MachInst`:
848                // after each instruction, ensure that the worst-case end of
849                // any island the buffer might emit lies before the soonest
850                // deadline, even after the next instruction.
851                let lookahead = I::worst_case_size() + I::worst_case_island_growth();
852                if buffer.island_needed(lookahead) {
853                    let jump_around = buffer.get_label();
854                    I::gen_jump(jump_around).emit(buffer, &self.emit_info, state);
855                    buffer.emit_island(0, state.ctrl_plane_mut());
856                    buffer.bind_label(jump_around, state.ctrl_plane_mut());
857                }
858            };
859
860            // Is this the first block? Emit the prologue directly if so.
861            if block == self.entry {
862                trace!(" -> entry block");
863                buffer.start_srcloc(Default::default());
864                for inst in &self.abi.gen_prologue() {
865                    do_emit(&inst, &mut disasm, &mut buffer, &mut state);
866                }
867                buffer.end_srcloc();
868            }
869
870            // Now emit the regular block body.
871
872            buffer.bind_label(MachLabel::from_block(block), state.ctrl_plane_mut());
873
874            if want_disasm {
875                writeln!(&mut disasm, "block{}:", block.index()).unwrap();
876            }
877
878            if flags.machine_code_cfg_info() {
879                // Track BB starts. If we have backed up due to MachBuffer
880                // branch opts, note that the removed blocks were removed.
881                let cur_offset = buffer.cur_offset();
882                if last_offset.is_some() && cur_offset <= last_offset.unwrap() {
883                    for i in (0..bb_starts.len()).rev() {
884                        if bb_starts[i].is_some() && cur_offset > bb_starts[i].unwrap() {
885                            break;
886                        }
887                        bb_starts[i] = None;
888                    }
889                }
890                bb_starts.push(Some(cur_offset));
891                last_offset = Some(cur_offset);
892            }
893
894            if let Some(block_start) = I::gen_block_start(
895                self.block_order.is_indirect_branch_target(block),
896                is_forward_edge_cfi_enabled,
897            ) {
898                do_emit(&block_start, &mut disasm, &mut buffer, &mut state);
899            }
900
901            for inst_or_edit in regalloc.block_insts_and_edits(&self, block) {
902                match inst_or_edit {
903                    InstOrEdit::Inst(iix) => {
904                        if !self.debug_value_labels.is_empty() {
905                            // If we need to produce debug info,
906                            // record the offset of each instruction
907                            // so that we can translate value-label
908                            // ranges to machine-code offsets.
909
910                            // Cold blocks violate monotonicity
911                            // assumptions elsewhere (that
912                            // instructions in inst-index order are in
913                            // order in machine code), so we omit
914                            // their offsets here. Value-label range
915                            // generation below will skip empty ranges
916                            // and ranges with to-offsets of zero.
917                            if !self.block_order.is_cold(block) {
918                                inst_offsets[iix.index()] = buffer.cur_offset();
919                            }
920                        }
921
922                        // Update the srcloc at this point in the buffer.
923                        let srcloc = self.srclocs[iix.index()];
924                        if cur_srcloc != Some(srcloc) {
925                            if cur_srcloc.is_some() {
926                                buffer.end_srcloc();
927                            }
928                            buffer.start_srcloc(srcloc);
929                            cur_srcloc = Some(srcloc);
930                        }
931
932                        // If this is a safepoint, compute a stack map
933                        // and pass it to the emit state.
934                        let stack_map_disasm = if self.insts[iix.index()].is_safepoint() {
935                            let (user_stack_map, user_stack_map_disasm) = {
936                                // The `user_stack_maps` is keyed by reverse
937                                // instruction index, so we must flip the
938                                // index. We can't put this into a helper method
939                                // due to borrowck issues because parts of
940                                // `self` are borrowed mutably elsewhere in this
941                                // function.
942                                let index = iix.to_backwards_insn_index(self.num_insts());
943                                let user_stack_map = self.user_stack_maps.remove(&index);
944                                let user_stack_map_disasm = if want_disasm {
945                                    user_stack_map.as_ref().map(|m| format!("  ; {m:?}"))
946                                } else {
947                                    None
948                                };
949                                (user_stack_map, user_stack_map_disasm)
950                            };
951
952                            state.pre_safepoint(user_stack_map);
953
954                            user_stack_map_disasm
955                        } else {
956                            None
957                        };
958
959                        // Place debug tags in the emission buffer
960                        // either at the offset prior to the
961                        // instruction or after the instruction,
962                        // depending on whether this is a call. See
963                        // the documentation on [`MachDebugTagPos`]
964                        // for details on why.
965                        let mut debug_tag_disasm = None;
966                        let mut place_debug_tags =
967                            |this: &VCode<I>, pos: MachDebugTagPos, buffer: &mut MachBuffer<I>| {
968                                // As above, translate the forward instruction
969                                // index to a backward index for the lookup.
970                                let debug_tag_range = {
971                                    let index = iix.to_backwards_insn_index(this.num_insts());
972                                    this.debug_tags.get(&index)
973                                };
974                                if let Some(range) = debug_tag_range {
975                                    let start = usize::try_from(range.start).unwrap();
976                                    let end = usize::try_from(range.end).unwrap();
977                                    let tags = &this.debug_tag_pool[start..end];
978
979                                    if want_disasm {
980                                        debug_tag_disasm =
981                                            Some(format!("  ; ^-- debug @ {pos:?}: {tags:?}"));
982                                    }
983                                    buffer.push_debug_tags(pos, tags);
984                                }
985                            };
986                        let debug_tag_pos =
987                            if self.insts[iix.index()].call_type() == CallType::Regular {
988                                MachDebugTagPos::Post
989                            } else {
990                                MachDebugTagPos::Pre
991                            };
992
993                        if debug_tag_pos == MachDebugTagPos::Pre {
994                            place_debug_tags(&self, debug_tag_pos, &mut buffer);
995                        }
996
997                        // If the instruction we are about to emit is
998                        // a return, place an epilogue at this point
999                        // (and don't emit the return; the actual
1000                        // epilogue will contain it).
1001                        if self.insts[iix.index()].is_term() == MachTerminator::Ret {
1002                            log::trace!("emitting epilogue");
1003                            for inst in self.abi.gen_epilogue() {
1004                                do_emit(&inst, &mut disasm, &mut buffer, &mut state);
1005                            }
1006                        } else {
1007                            // Update the operands for this inst using the
1008                            // allocations from the regalloc result.
1009                            let mut allocs = regalloc.inst_allocs(iix).iter();
1010                            self.insts[iix.index()].get_operands(
1011                                &mut |reg: &mut Reg, constraint, _kind, _pos| {
1012                                    let alloc =
1013                                        allocs.next().expect("enough allocations for all operands");
1014
1015                                    if let Some(alloc) = alloc.as_reg() {
1016                                        let alloc: Reg = alloc.into();
1017                                        if let OperandConstraint::FixedReg(rreg) = constraint {
1018                                            debug_assert_eq!(Reg::from(rreg), alloc);
1019                                        }
1020                                        *reg = alloc;
1021                                    } else if let Some(alloc) = alloc.as_stack() {
1022                                        let alloc: Reg = alloc.into();
1023                                        *reg = alloc;
1024                                    }
1025                                },
1026                            );
1027                            debug_assert!(allocs.next().is_none());
1028
1029                            log::trace!("emitting: {:?}", self.insts[iix.index()]);
1030
1031                            // Emit the instruction!
1032                            do_emit(
1033                                &self.insts[iix.index()],
1034                                &mut disasm,
1035                                &mut buffer,
1036                                &mut state,
1037                            );
1038
1039                            if debug_tag_pos == MachDebugTagPos::Post {
1040                                place_debug_tags(&self, debug_tag_pos, &mut buffer);
1041                            }
1042
1043                            if let Some(stack_map_disasm) = stack_map_disasm {
1044                                disasm.push_str(&stack_map_disasm);
1045                                disasm.push('\n');
1046                            }
1047                            if let Some(debug_tag_disasm) = debug_tag_disasm {
1048                                disasm.push_str(&debug_tag_disasm);
1049                                disasm.push('\n');
1050                            }
1051                        }
1052                    }
1053
1054                    InstOrEdit::Edit(Edit::Move { from, to }) => {
1055                        // Create a move/spill/reload instruction and
1056                        // immediately emit it.
1057                        match (from.as_reg(), to.as_reg()) {
1058                            (Some(from), Some(to)) => {
1059                                // Reg-to-reg move.
1060                                let from_rreg = Reg::from(from);
1061                                let to_rreg = Writable::from_reg(Reg::from(to));
1062                                debug_assert_eq!(from.class(), to.class());
1063                                let ty = I::canonical_type_for_rc(from.class());
1064                                let mv = I::gen_move(to_rreg, from_rreg, ty);
1065                                do_emit(&mv, &mut disasm, &mut buffer, &mut state);
1066                            }
1067                            (Some(from), None) => {
1068                                // Spill from register to spillslot.
1069                                let to = to.as_stack().unwrap();
1070                                let from_rreg = RealReg::from(from);
1071                                let spill = self.abi.gen_spill(to, from_rreg);
1072                                do_emit(&spill, &mut disasm, &mut buffer, &mut state);
1073                            }
1074                            (None, Some(to)) => {
1075                                // Load from spillslot to register.
1076                                let from = from.as_stack().unwrap();
1077                                let to_rreg = Writable::from_reg(RealReg::from(to));
1078                                let reload = self.abi.gen_reload(to_rreg, from);
1079                                do_emit(&reload, &mut disasm, &mut buffer, &mut state);
1080                            }
1081                            (None, None) => {
1082                                panic!("regalloc2 should have eliminated stack-to-stack moves!");
1083                            }
1084                        }
1085                    }
1086                }
1087            }
1088
1089            if cur_srcloc.is_some() {
1090                buffer.end_srcloc();
1091                cur_srcloc = None;
1092            }
1093
1094            // Insert padding, if configured, to stress the `MachBuffer`'s
1095            // relocation and island calculations.
1096            //
1097            // Padding can get quite large during fuzzing though so place a
1098            // total cap on it where when a per-function threshold is exceeded
1099            // the padding is turned back down to zero. This avoids a small-ish
1100            // test case generating a GB+ memory footprint in Cranelift for
1101            // example.
1102            if !bb_padding.is_empty() {
1103                // The padding bytes go directly into the buffer without
1104                // passing through `do_emit`, so check the deadline invariant
1105                // *before* writing them: if the padding would push the
1106                // worst-case end of an island past any pending deadline,
1107                // drain pending fixups via an island now. We're between
1108                // blocks, so no jump-around is needed.
1109                let padding_len = bb_padding.len() as u32 + I::LabelUse::ALIGN - 1;
1110                let lookahead = I::worst_case_size() + I::worst_case_island_growth();
1111                if buffer.island_needed(padding_len + lookahead) {
1112                    buffer.emit_island(padding_len + lookahead, ctrl_plane);
1113                }
1114
1115                buffer.put_data(&bb_padding);
1116                buffer.align_to(I::LabelUse::ALIGN);
1117                total_bb_padding += bb_padding.len();
1118                if total_bb_padding > (150 << 20) {
1119                    bb_padding = Vec::new();
1120                }
1121            }
1122        }
1123
1124        debug_assert!(
1125            self.user_stack_maps.is_empty(),
1126            "any stack maps should have been consumed by instruction emission, still have: {:#?}",
1127            self.user_stack_maps,
1128        );
1129
1130        // Do any optimizations on branches at tail of buffer, as if we had
1131        // bound one last label.
1132        buffer.optimize_branches(ctrl_plane);
1133
1134        // emission state is not needed anymore, move control plane back out
1135        *ctrl_plane = state.take_ctrl_plane();
1136
1137        let func_body_len = buffer.cur_offset();
1138
1139        // Create `bb_edges` and final (filtered) `bb_starts`.
1140        let mut bb_edges = vec![];
1141        let mut bb_offsets = vec![];
1142        if flags.machine_code_cfg_info() {
1143            for block in 0..self.num_blocks() {
1144                if bb_starts[block].is_none() {
1145                    // Block was deleted by MachBuffer; skip.
1146                    continue;
1147                }
1148                let from = bb_starts[block].unwrap();
1149
1150                bb_offsets.push(from);
1151                // Resolve each `succ` label and add edges.
1152                let succs = self.block_succs(BlockIndex::new(block));
1153                for &succ in succs.iter() {
1154                    let to = buffer.resolve_label_offset(MachLabel::from_block(succ));
1155                    bb_edges.push((from, to));
1156                }
1157            }
1158        }
1159
1160        self.monotonize_inst_offsets(&mut inst_offsets[..], func_body_len);
1161        let value_labels_ranges =
1162            self.compute_value_labels_ranges(regalloc, &inst_offsets[..], func_body_len);
1163
1164        // Store metadata about frame layout in the MachBuffer.
1165        buffer.set_frame_layout(self.abi.frame_slot_metadata());
1166
1167        EmitResult {
1168            buffer: buffer.finish(&self.constants, ctrl_plane),
1169            bb_offsets,
1170            bb_edges,
1171            disasm: if want_disasm { Some(disasm) } else { None },
1172            value_labels_ranges,
1173        }
1174    }
1175
1176    fn monotonize_inst_offsets(&self, inst_offsets: &mut [CodeOffset], func_body_len: u32) {
1177        if self.debug_value_labels.is_empty() {
1178            return;
1179        }
1180
1181        // During emission, branch removal can make offsets of instructions incorrect.
1182        // Consider the following sequence: [insi][jmp0][jmp1][jmp2][insj]
1183        // It will be recorded as (say):    [30]  [34]  [38]  [42]  [<would be 46>]
1184        // When the jumps get removed we are left with (in "inst_offsets"):
1185        // [insi][jmp0][jmp1][jmp2][insj][...]
1186        // [30]  [34]  [38]  [42]  [34]
1187        // Which violates the monotonicity invariant. This method sets offsets of these
1188        // removed instructions such as to make them appear zero-sized:
1189        // [insi][jmp0][jmp1][jmp2][insj][...]
1190        // [30]  [34]  [34]  [34]  [34]
1191        //
1192        let mut next_offset = func_body_len;
1193        for inst_index in (0..(inst_offsets.len() - 1)).rev() {
1194            let inst_offset = inst_offsets[inst_index];
1195
1196            // Not all instructions get their offsets recorded.
1197            if inst_offset == NO_INST_OFFSET {
1198                continue;
1199            }
1200
1201            if inst_offset > next_offset {
1202                trace!(
1203                    "Fixing code offset of the removed Inst {}: {} -> {}",
1204                    inst_index, inst_offset, next_offset
1205                );
1206                inst_offsets[inst_index] = next_offset;
1207                continue;
1208            }
1209
1210            next_offset = inst_offset;
1211        }
1212    }
1213
1214    fn compute_value_labels_ranges(
1215        &self,
1216        regalloc: &regalloc2::Output,
1217        inst_offsets: &[CodeOffset],
1218        func_body_len: u32,
1219    ) -> ValueLabelsRanges {
1220        if self.debug_value_labels.is_empty() {
1221            return ValueLabelsRanges::default();
1222        }
1223
1224        if trace_log_enabled!() {
1225            self.log_value_labels_ranges(regalloc, inst_offsets);
1226        }
1227
1228        let mut value_labels_ranges: ValueLabelsRanges = HashMap::new();
1229        for &(label, from, to, alloc) in &regalloc.debug_locations {
1230            let label = ValueLabel::from_u32(label);
1231            let ranges = value_labels_ranges.entry(label).or_insert_with(|| vec![]);
1232            let prog_point_to_inst = |prog_point: ProgPoint| {
1233                let mut inst = prog_point.inst();
1234                if prog_point.pos() == InstPosition::After {
1235                    inst = inst.next();
1236                }
1237                inst.index()
1238            };
1239            let inst_to_offset = |inst_index: usize| {
1240                // Skip over cold blocks.
1241                for offset in &inst_offsets[inst_index..] {
1242                    if *offset != NO_INST_OFFSET {
1243                        return *offset;
1244                    }
1245                }
1246                func_body_len
1247            };
1248            let from_inst_index = prog_point_to_inst(from);
1249            let to_inst_index = prog_point_to_inst(to);
1250            let from_offset = inst_to_offset(from_inst_index);
1251            let to_offset = inst_to_offset(to_inst_index);
1252
1253            // Empty ranges or unavailable offsets can happen
1254            // due to cold blocks and branch removal (see above).
1255            if from_offset == to_offset {
1256                continue;
1257            }
1258
1259            let loc = if let Some(preg) = alloc.as_reg() {
1260                LabelValueLoc::Reg(Reg::from(preg))
1261            } else {
1262                #[cfg(not(feature = "unwind"))]
1263                continue;
1264
1265                #[cfg(feature = "unwind")]
1266                {
1267                    let slot = alloc.as_stack().unwrap();
1268                    let slot_offset = self.abi.get_spillslot_offset(slot);
1269                    let slot_base_to_caller_sp_offset = self.abi.slot_base_to_caller_sp_offset();
1270                    let caller_sp_to_cfa_offset =
1271                        crate::isa::unwind::systemv::caller_sp_to_cfa_offset();
1272                    // NOTE: this is a negative offset because it's relative to the caller's SP
1273                    let cfa_to_sp_offset =
1274                        -((slot_base_to_caller_sp_offset + caller_sp_to_cfa_offset) as i64);
1275                    LabelValueLoc::CFAOffset(cfa_to_sp_offset + slot_offset)
1276                }
1277            };
1278
1279            // Coalesce adjacent ranges that for the same location
1280            // to minimize output size here and for the consumers.
1281            if let Some(last_loc_range) = ranges.last_mut() {
1282                if last_loc_range.loc == loc && last_loc_range.end == from_offset {
1283                    trace!(
1284                        "Extending debug range for {:?} in {:?} to Inst {} ({})",
1285                        label, loc, to_inst_index, to_offset
1286                    );
1287                    last_loc_range.end = to_offset;
1288                    continue;
1289                }
1290            }
1291
1292            trace!(
1293                "Recording debug range for {:?} in {:?}: [Inst {}..Inst {}) [{}..{})",
1294                label, loc, from_inst_index, to_inst_index, from_offset, to_offset
1295            );
1296
1297            ranges.push(ValueLocRange {
1298                loc,
1299                start: from_offset,
1300                end: to_offset,
1301            });
1302        }
1303
1304        value_labels_ranges
1305    }
1306
1307    fn log_value_labels_ranges(&self, regalloc: &regalloc2::Output, inst_offsets: &[CodeOffset]) {
1308        debug_assert!(trace_log_enabled!());
1309
1310        // What debug labels do we have? Note we'll skip those that have not been
1311        // allocated any location at all. They will show up as numeric gaps in the table.
1312        let mut labels = vec![];
1313        for &(label, _, _, _) in &regalloc.debug_locations {
1314            if Some(&label) == labels.last() {
1315                continue;
1316            }
1317            labels.push(label);
1318        }
1319
1320        // Reformat the data on what VRegs were the VLs assigned to by lowering, since
1321        // the array we have is sorted by VReg, and we want it sorted by VL for easy
1322        // access in the loop below.
1323        let mut vregs = vec![];
1324        for &(vreg, start, end, label) in &self.debug_value_labels {
1325            if matches!(labels.binary_search(&label), Ok(_)) {
1326                vregs.push((label, start, end, vreg));
1327            }
1328        }
1329        vregs.sort_unstable_by(
1330            |(l_label, l_start, _, _), (r_label, r_start, _, _)| match l_label.cmp(r_label) {
1331                Ordering::Equal => l_start.cmp(r_start),
1332                cmp => cmp,
1333            },
1334        );
1335
1336        #[derive(PartialEq)]
1337        enum Mode {
1338            Measure,
1339            Emit,
1340        }
1341        #[derive(PartialEq)]
1342        enum Row {
1343            Head,
1344            Line,
1345            Inst(usize, usize),
1346        }
1347
1348        let mut widths = vec![0; 3 + 2 * labels.len()];
1349        let mut row = String::new();
1350        let mut output_row = |row_kind: Row, mode: Mode| {
1351            let mut column_index = 0;
1352            row.clear();
1353
1354            macro_rules! output_cell_impl {
1355                ($fill:literal, $span:literal, $($cell_fmt:tt)*) => {
1356                    let column_start = row.len();
1357                    {
1358                        row.push('|');
1359                        write!(row, $($cell_fmt)*).unwrap();
1360                    }
1361
1362                    let next_column_index = column_index + $span;
1363                    let expected_width: usize = widths[column_index..next_column_index].iter().sum();
1364                    if mode == Mode::Measure {
1365                        let actual_width = row.len() - column_start;
1366                        if actual_width > expected_width {
1367                            widths[next_column_index - 1] += actual_width - expected_width;
1368                        }
1369                    } else {
1370                        let column_end = column_start + expected_width;
1371                        while row.len() != column_end {
1372                            row.push($fill);
1373                        }
1374                    }
1375                    column_index = next_column_index;
1376                };
1377            }
1378            macro_rules! output_cell {
1379                ($($cell_fmt:tt)*) => {
1380                    output_cell_impl!(' ', 1, $($cell_fmt)*);
1381                };
1382            }
1383
1384            match row_kind {
1385                Row::Head => {
1386                    output_cell!("BB");
1387                    output_cell!("Inst");
1388                    output_cell!("IP");
1389                    for label in &labels {
1390                        output_cell_impl!(' ', 2, "{:?}", ValueLabel::from_u32(*label));
1391                    }
1392                }
1393                Row::Line => {
1394                    debug_assert!(mode == Mode::Emit);
1395                    for _ in 0..3 {
1396                        output_cell_impl!('-', 1, "");
1397                    }
1398                    for _ in &labels {
1399                        output_cell_impl!('-', 2, "");
1400                    }
1401                }
1402                Row::Inst(block_index, inst_index) => {
1403                    debug_assert!(inst_index < self.num_insts());
1404                    if self.block_ranges.get(block_index).start == inst_index {
1405                        output_cell!("B{}", block_index);
1406                    } else {
1407                        output_cell!("");
1408                    }
1409                    output_cell!("Inst {inst_index} ");
1410                    output_cell!("{} ", inst_offsets[inst_index]);
1411
1412                    for label in &labels {
1413                        // First, the VReg.
1414                        use regalloc2::Inst;
1415                        let vreg_cmp = |inst: usize,
1416                                        vreg_label: &u32,
1417                                        range_start: &Inst,
1418                                        range_end: &Inst| {
1419                            match vreg_label.cmp(&label) {
1420                                Ordering::Equal => {
1421                                    if range_end.index() <= inst {
1422                                        Ordering::Less
1423                                    } else if range_start.index() > inst {
1424                                        Ordering::Greater
1425                                    } else {
1426                                        Ordering::Equal
1427                                    }
1428                                }
1429                                cmp => cmp,
1430                            }
1431                        };
1432                        let vreg_index =
1433                            vregs.binary_search_by(|(l, s, e, _)| vreg_cmp(inst_index, l, s, e));
1434                        if let Ok(vreg_index) = vreg_index {
1435                            let mut prev_vreg = None;
1436                            if inst_index > 0 {
1437                                let prev_vreg_index = vregs.binary_search_by(|(l, s, e, _)| {
1438                                    vreg_cmp(inst_index - 1, l, s, e)
1439                                });
1440                                if let Ok(prev_vreg_index) = prev_vreg_index {
1441                                    prev_vreg = Some(vregs[prev_vreg_index].3);
1442                                }
1443                            }
1444
1445                            let vreg = vregs[vreg_index].3;
1446                            if Some(vreg) == prev_vreg {
1447                                output_cell!("*");
1448                            } else {
1449                                output_cell!("{}", vreg);
1450                            }
1451                        } else {
1452                            output_cell!("");
1453                        }
1454
1455                        // Second, the allocated location.
1456                        let inst_prog_point = ProgPoint::before(Inst::new(inst_index));
1457                        let range_index = regalloc.debug_locations.binary_search_by(
1458                            |(range_label, range_start, range_end, _)| match range_label.cmp(label)
1459                            {
1460                                Ordering::Equal => {
1461                                    if *range_end <= inst_prog_point {
1462                                        Ordering::Less
1463                                    } else if *range_start > inst_prog_point {
1464                                        Ordering::Greater
1465                                    } else {
1466                                        Ordering::Equal
1467                                    }
1468                                }
1469                                cmp => cmp,
1470                            },
1471                        );
1472                        if let Ok(range_index) = range_index {
1473                            // Live at this instruction, print the location.
1474                            if let Some(reg) = regalloc.debug_locations[range_index].3.as_reg() {
1475                                output_cell!("{:?}", Reg::from(reg));
1476                            } else {
1477                                output_cell!("Stk");
1478                            }
1479                        } else {
1480                            // Not live at this instruction.
1481                            output_cell!("");
1482                        }
1483                    }
1484                }
1485            }
1486            row.push('|');
1487
1488            if mode == Mode::Emit {
1489                trace!("{}", row.as_str());
1490            }
1491        };
1492
1493        for block_index in 0..self.num_blocks() {
1494            for inst_index in self.block_ranges.get(block_index) {
1495                output_row(Row::Inst(block_index, inst_index), Mode::Measure);
1496            }
1497        }
1498        output_row(Row::Head, Mode::Measure);
1499
1500        output_row(Row::Head, Mode::Emit);
1501        output_row(Row::Line, Mode::Emit);
1502        for block_index in 0..self.num_blocks() {
1503            for inst_index in self.block_ranges.get(block_index) {
1504                output_row(Row::Inst(block_index, inst_index), Mode::Emit);
1505            }
1506        }
1507    }
1508
1509    /// Get the IR block for a BlockIndex, if one exists.
1510    pub fn bindex_to_bb(&self, block: BlockIndex) -> Option<ir::Block> {
1511        self.block_order.lowered_order()[block.index()].orig_block()
1512    }
1513
1514    /// Get the user stack map associated with the given forward instruction index.
1515    pub fn get_user_stack_map(&self, inst: InsnIndex) -> Option<&ir::UserStackMap> {
1516        let index = inst.to_backwards_insn_index(self.num_insts());
1517        self.user_stack_maps.get(&index)
1518    }
1519}
1520
1521impl<I: VCodeInst> core::ops::Index<InsnIndex> for VCode<I> {
1522    type Output = I;
1523    fn index(&self, idx: InsnIndex) -> &Self::Output {
1524        &self.insts[idx.index()]
1525    }
1526}
1527
1528impl<I: VCodeInst> RegallocFunction for VCode<I> {
1529    fn num_insts(&self) -> usize {
1530        self.insts.len()
1531    }
1532
1533    fn num_blocks(&self) -> usize {
1534        self.block_ranges.len()
1535    }
1536
1537    fn entry_block(&self) -> BlockIndex {
1538        self.entry
1539    }
1540
1541    fn block_insns(&self, block: BlockIndex) -> InstRange {
1542        let range = self.block_ranges.get(block.index());
1543        InstRange::new(InsnIndex::new(range.start), InsnIndex::new(range.end))
1544    }
1545
1546    fn block_succs(&self, block: BlockIndex) -> &[BlockIndex] {
1547        let range = self.block_succ_range.get(block.index());
1548        &self.block_succs[range]
1549    }
1550
1551    fn block_preds(&self, block: BlockIndex) -> &[BlockIndex] {
1552        let range = self.block_pred_range.get(block.index());
1553        &self.block_preds[range]
1554    }
1555
1556    fn block_params(&self, block: BlockIndex) -> &[VReg] {
1557        // As a special case we don't return block params for the entry block, as all the arguments
1558        // will be defined by the `Inst::Args` instruction.
1559        if block == self.entry {
1560            return &[];
1561        }
1562
1563        let range = self.block_params_range.get(block.index());
1564        &self.block_params[range]
1565    }
1566
1567    fn branch_blockparams(&self, block: BlockIndex, _insn: InsnIndex, succ_idx: usize) -> &[VReg] {
1568        let succ_range = self.branch_block_arg_succ_range.get(block.index());
1569        debug_assert!(succ_idx < succ_range.len());
1570        let branch_block_args = self.branch_block_arg_range.get(succ_range.start + succ_idx);
1571        &self.branch_block_args[branch_block_args]
1572    }
1573
1574    fn is_ret(&self, insn: InsnIndex) -> bool {
1575        match self.insts[insn.index()].is_term() {
1576            // We treat blocks terminated by an unconditional trap like a return for regalloc.
1577            MachTerminator::None => self.insts[insn.index()].is_trap(),
1578            MachTerminator::Ret | MachTerminator::RetCall => true,
1579            MachTerminator::Branch => false,
1580        }
1581    }
1582
1583    fn is_branch(&self, insn: InsnIndex) -> bool {
1584        match self.insts[insn.index()].is_term() {
1585            MachTerminator::Branch => true,
1586            _ => false,
1587        }
1588    }
1589
1590    fn inst_operands(&self, insn: InsnIndex) -> &[Operand] {
1591        let range = self.operand_ranges.get(insn.index());
1592        &self.operands[range]
1593    }
1594
1595    fn inst_clobbers(&self, insn: InsnIndex) -> PRegSet {
1596        self.clobbers.get(&insn).cloned().unwrap_or_default()
1597    }
1598
1599    fn num_vregs(&self) -> usize {
1600        self.vreg_types.len()
1601    }
1602
1603    fn debug_value_labels(&self) -> &[(VReg, InsnIndex, InsnIndex, u32)] {
1604        &self.debug_value_labels
1605    }
1606
1607    fn spillslot_size(&self, regclass: RegClass) -> usize {
1608        self.abi.get_spillslot_size(regclass) as usize
1609    }
1610
1611    fn allow_multiple_vreg_defs(&self) -> bool {
1612        // At least the s390x backend requires this, because the
1613        // `Loop` pseudo-instruction aggregates all Operands so pinned
1614        // vregs (RealRegs) may occur more than once.
1615        true
1616    }
1617}
1618
1619impl<I: VCodeInst> Debug for VRegAllocator<I> {
1620    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1621        writeln!(f, "VRegAllocator {{")?;
1622
1623        let mut alias_keys = self.vreg_aliases.keys().cloned().collect::<Vec<_>>();
1624        alias_keys.sort_unstable();
1625        for key in alias_keys {
1626            let dest = self.vreg_aliases.get(&key).unwrap();
1627            writeln!(f, "  {:?} := {:?}", Reg::from(key), Reg::from(*dest))?;
1628        }
1629
1630        writeln!(f, "}}")
1631    }
1632}
1633
1634impl<I: VCodeInst> fmt::Debug for VCode<I> {
1635    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1636        writeln!(f, "VCode {{")?;
1637        writeln!(f, "  Entry block: {}", self.entry.index())?;
1638
1639        let mut state = Default::default();
1640
1641        for block in 0..self.num_blocks() {
1642            let block = BlockIndex::new(block);
1643            writeln!(
1644                f,
1645                "Block {}({:?}):",
1646                block.index(),
1647                self.block_params(block)
1648            )?;
1649            if let Some(bb) = self.bindex_to_bb(block) {
1650                writeln!(f, "    (original IR block: {bb})")?;
1651            }
1652            for (succ_idx, succ) in self.block_succs(block).iter().enumerate() {
1653                writeln!(
1654                    f,
1655                    "    (successor: Block {}({:?}))",
1656                    succ.index(),
1657                    self.branch_blockparams(block, InsnIndex::new(0) /* dummy */, succ_idx)
1658                )?;
1659            }
1660            for inst in self.block_ranges.get(block.index()) {
1661                writeln!(
1662                    f,
1663                    "  Inst {}: {}",
1664                    inst,
1665                    self.insts[inst].pretty_print_inst(&mut state)
1666                )?;
1667                if let Some(user_stack_map) = self.get_user_stack_map(InsnIndex::new(inst)) {
1668                    writeln!(f, "    {user_stack_map:?}")?;
1669                }
1670            }
1671        }
1672
1673        writeln!(f, "}}")?;
1674        Ok(())
1675    }
1676}
1677
1678/// This structure manages VReg allocation during the lifetime of the VCodeBuilder.
1679pub struct VRegAllocator<I> {
1680    /// VReg IR-level types.
1681    vreg_types: Vec<Type>,
1682
1683    /// VReg aliases. When the final VCode is built we rewrite all
1684    /// uses of the keys in this table to their replacement values.
1685    ///
1686    /// We use these aliases to rename an instruction's expected
1687    /// result vregs to the returned vregs from lowering, which are
1688    /// usually freshly-allocated temps.
1689    vreg_aliases: FxHashMap<regalloc2::VReg, regalloc2::VReg>,
1690
1691    /// A deferred error, to be bubbled up to the top level of the
1692    /// lowering algorithm. We take this approach because we cannot
1693    /// currently propagate a `Result` upward through ISLE code (the
1694    /// lowering rules) or some ABI code.
1695    deferred_error: Option<CodegenError>,
1696
1697    /// The type of instruction that this allocator makes registers for.
1698    _inst: core::marker::PhantomData<I>,
1699}
1700
1701impl<I: VCodeInst> VRegAllocator<I> {
1702    /// Make a new VRegAllocator.
1703    pub fn with_capacity(capacity: usize) -> Self {
1704        let capacity = first_user_vreg_index() + capacity;
1705        let mut vreg_types = Vec::with_capacity(capacity);
1706        vreg_types.resize(first_user_vreg_index(), types::INVALID);
1707        Self {
1708            vreg_types,
1709            vreg_aliases: FxHashMap::with_capacity_and_hasher(capacity, Default::default()),
1710            deferred_error: None,
1711            _inst: core::marker::PhantomData::default(),
1712        }
1713    }
1714
1715    /// Allocate a fresh ValueRegs.
1716    pub fn alloc(&mut self, ty: Type) -> CodegenResult<ValueRegs<Reg>> {
1717        if self.deferred_error.is_some() {
1718            return Err(CodegenError::CodeTooLarge);
1719        }
1720        let v = self.vreg_types.len();
1721        let (regclasses, tys) = I::rc_for_type(ty)?;
1722
1723        // Check that new indices are in-bounds for regalloc2's
1724        // VReg/Operand representation.
1725        if v + regclasses.len() > VReg::MAX {
1726            return Err(CodegenError::CodeTooLarge);
1727        }
1728
1729        // Check that new indices are in-bounds for our Reg
1730        // bit-packing on top of the RA2 types, which represents
1731        // spillslots as well.
1732        let check = |vreg: regalloc2::VReg| -> CodegenResult<Reg> {
1733            Reg::from_virtual_reg_checked(vreg).ok_or(CodegenError::CodeTooLarge)
1734        };
1735
1736        let regs: ValueRegs<Reg> = match regclasses {
1737            &[rc0] => ValueRegs::one(check(VReg::new(v, rc0))?),
1738            &[rc0, rc1] => ValueRegs::two(check(VReg::new(v, rc0))?, check(VReg::new(v + 1, rc1))?),
1739            // We can extend this if/when we support 32-bit targets; e.g.,
1740            // an i128 on a 32-bit machine will need up to four machine regs
1741            // for a `Value`.
1742            _ => panic!("Value must reside in 1 or 2 registers"),
1743        };
1744        for (&reg_ty, &reg) in tys.iter().zip(regs.regs().iter()) {
1745            let vreg = reg.to_virtual_reg().unwrap();
1746            debug_assert_eq!(self.vreg_types.len(), vreg.index());
1747            self.vreg_types.push(reg_ty);
1748        }
1749
1750        Ok(regs)
1751    }
1752
1753    /// Allocate a fresh ValueRegs, deferring any out-of-vregs
1754    /// errors. This is useful in places where we cannot bubble a
1755    /// `CodegenResult` upward easily, and which are known to be
1756    /// invoked from within the lowering loop that checks the deferred
1757    /// error status below.
1758    pub fn alloc_with_deferred_error(&mut self, ty: Type) -> ValueRegs<Reg> {
1759        match self.alloc(ty) {
1760            Ok(x) => x,
1761            Err(e) => {
1762                self.deferred_error = Some(e);
1763                self.bogus_for_deferred_error(ty)
1764            }
1765        }
1766    }
1767
1768    /// Take any deferred error that was accumulated by `alloc_with_deferred_error`.
1769    pub fn take_deferred_error(&mut self) -> Option<CodegenError> {
1770        self.deferred_error.take()
1771    }
1772
1773    /// Produce an bogus VReg placeholder with the proper number of
1774    /// registers for the given type. This is meant to be used with
1775    /// deferred allocation errors (see `Lower::alloc_tmp()`).
1776    fn bogus_for_deferred_error(&self, ty: Type) -> ValueRegs<Reg> {
1777        let (regclasses, _tys) = I::rc_for_type(ty).expect("must have valid type");
1778        match regclasses {
1779            &[rc0] => ValueRegs::one(VReg::new(0, rc0).into()),
1780            &[rc0, rc1] => ValueRegs::two(VReg::new(0, rc0).into(), VReg::new(1, rc1).into()),
1781            _ => panic!("Value must reside in 1 or 2 registers"),
1782        }
1783    }
1784
1785    /// Rewrite any mention of `from` into `to`.
1786    pub fn set_vreg_alias(&mut self, from: Reg, to: Reg) {
1787        let from = from.into();
1788        let resolved_to = self.resolve_vreg_alias(to.into());
1789        // Disallow cycles (see below).
1790        assert_ne!(resolved_to, from);
1791
1792        let old_alias = self.vreg_aliases.insert(from, resolved_to);
1793        debug_assert_eq!(old_alias, None);
1794    }
1795
1796    fn resolve_vreg_alias(&self, mut vreg: regalloc2::VReg) -> regalloc2::VReg {
1797        // We prevent cycles from existing by resolving targets of
1798        // aliases eagerly before setting them. If the target resolves
1799        // to the origin of the alias, then a cycle would be created
1800        // and the alias is disallowed. Because of the structure of
1801        // SSA code (one instruction can refer to another's defs but
1802        // not vice-versa, except indirectly through
1803        // phis/blockparams), cycles should not occur as we use
1804        // aliases to redirect vregs to the temps that actually define
1805        // them.
1806        while let Some(to) = self.vreg_aliases.get(&vreg) {
1807            vreg = *to;
1808        }
1809        vreg
1810    }
1811
1812    #[inline]
1813    fn debug_assert_no_vreg_aliases(&self, mut list: impl Iterator<Item = VReg>) {
1814        debug_assert!(list.all(|vreg| !self.vreg_aliases.contains_key(&vreg)));
1815    }
1816}
1817
1818/// This structure tracks the large constants used in VCode that will be emitted separately by the
1819/// [MachBuffer].
1820///
1821/// First, during the lowering phase, constants are inserted using
1822/// [VCodeConstants.insert]; an intermediate handle, `VCodeConstant`, tracks what constants are
1823/// used in this phase. Some deduplication is performed, when possible, as constant
1824/// values are inserted.
1825///
1826/// Secondly, during the emission phase, the [MachBuffer] assigns [MachLabel]s for each of the
1827/// constants so that instructions can refer to the value's memory location. The [MachBuffer]
1828/// then writes the constant values to the buffer.
1829#[derive(Default)]
1830pub struct VCodeConstants {
1831    constants: PrimaryMap<VCodeConstant, VCodeConstantData>,
1832    pool_uses: HashMap<Constant, VCodeConstant>,
1833    well_known_uses: HashMap<*const [u8], VCodeConstant>,
1834    u64s: HashMap<[u8; 8], VCodeConstant>,
1835}
1836impl VCodeConstants {
1837    /// Initialize the structure with the expected number of constants.
1838    pub fn with_capacity(expected_num_constants: usize) -> Self {
1839        Self {
1840            constants: PrimaryMap::with_capacity(expected_num_constants),
1841            pool_uses: HashMap::with_capacity(expected_num_constants),
1842            well_known_uses: HashMap::new(),
1843            u64s: HashMap::new(),
1844        }
1845    }
1846
1847    /// Insert a constant; using this method indicates that a constant value will be used and thus
1848    /// will be emitted to the `MachBuffer`. The current implementation can deduplicate constants
1849    /// that are [VCodeConstantData::Pool] or [VCodeConstantData::WellKnown] but not
1850    /// [VCodeConstantData::Generated].
1851    pub fn insert(&mut self, data: VCodeConstantData) -> VCodeConstant {
1852        match data {
1853            VCodeConstantData::Generated(_) => self.constants.push(data),
1854            VCodeConstantData::Pool(constant, _) => match self.pool_uses.get(&constant) {
1855                None => {
1856                    let vcode_constant = self.constants.push(data);
1857                    self.pool_uses.insert(constant, vcode_constant);
1858                    vcode_constant
1859                }
1860                Some(&vcode_constant) => vcode_constant,
1861            },
1862            VCodeConstantData::WellKnown(data_ref) => {
1863                match self.well_known_uses.entry(data_ref as *const [u8]) {
1864                    Entry::Vacant(v) => {
1865                        let vcode_constant = self.constants.push(data);
1866                        v.insert(vcode_constant);
1867                        vcode_constant
1868                    }
1869                    Entry::Occupied(o) => *o.get(),
1870                }
1871            }
1872            VCodeConstantData::U64(value) => match self.u64s.entry(value) {
1873                Entry::Vacant(v) => {
1874                    let vcode_constant = self.constants.push(data);
1875                    v.insert(vcode_constant);
1876                    vcode_constant
1877                }
1878                Entry::Occupied(o) => *o.get(),
1879            },
1880        }
1881    }
1882
1883    /// Return the number of constants inserted.
1884    pub fn len(&self) -> usize {
1885        self.constants.len()
1886    }
1887
1888    /// Iterate over the `VCodeConstant` keys inserted in this structure.
1889    pub fn keys(&self) -> Keys<VCodeConstant> {
1890        self.constants.keys()
1891    }
1892
1893    /// Iterate over the `VCodeConstant` keys and the data (as a byte slice) inserted in this
1894    /// structure.
1895    pub fn iter(&self) -> impl Iterator<Item = (VCodeConstant, &VCodeConstantData)> {
1896        self.constants.iter()
1897    }
1898
1899    /// Returns the data associated with the specified constant.
1900    pub fn get(&self, c: VCodeConstant) -> &VCodeConstantData {
1901        &self.constants[c]
1902    }
1903
1904    /// Checks if the given [VCodeConstantData] is registered as
1905    /// used by the pool.
1906    pub fn pool_uses(&self, constant: &VCodeConstantData) -> bool {
1907        match constant {
1908            VCodeConstantData::Pool(c, _) => self.pool_uses.contains_key(c),
1909            _ => false,
1910        }
1911    }
1912}
1913
1914/// A use of a constant by one or more VCode instructions; see [VCodeConstants].
1915#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1916pub struct VCodeConstant(u32);
1917entity_impl!(VCodeConstant);
1918
1919/// Identify the different types of constant that can be inserted into [VCodeConstants]. Tracking
1920/// these separately instead of as raw byte buffers allows us to avoid some duplication.
1921pub enum VCodeConstantData {
1922    /// A constant already present in the Cranelift IR
1923    /// [ConstantPool](crate::ir::constant::ConstantPool).
1924    Pool(Constant, ConstantData),
1925    /// A reference to a well-known constant value that is statically encoded within the compiler.
1926    WellKnown(&'static [u8]),
1927    /// A constant value generated during lowering; the value may depend on the instruction context
1928    /// which makes it difficult to de-duplicate--if possible, use other variants.
1929    Generated(ConstantData),
1930    /// A constant of at most 64 bits. These are deduplicated as
1931    /// well. Stored as a fixed-size array of `u8` so that we do not
1932    /// encounter endianness problems when cross-compiling.
1933    U64([u8; 8]),
1934}
1935impl VCodeConstantData {
1936    /// Retrieve the constant data as a byte slice.
1937    pub fn as_slice(&self) -> &[u8] {
1938        match self {
1939            VCodeConstantData::Pool(_, d) | VCodeConstantData::Generated(d) => d.as_slice(),
1940            VCodeConstantData::WellKnown(d) => d,
1941            VCodeConstantData::U64(value) => &value[..],
1942        }
1943    }
1944
1945    /// Calculate the alignment of the constant data.
1946    pub fn alignment(&self) -> u32 {
1947        if self.as_slice().len() <= 8 { 8 } else { 16 }
1948    }
1949}
1950
1951#[cfg(test)]
1952mod test {
1953    use super::*;
1954    use core::mem::size_of;
1955
1956    #[test]
1957    fn size_of_constant_structs() {
1958        assert_eq!(size_of::<Constant>(), 4);
1959        assert_eq!(size_of::<VCodeConstant>(), 4);
1960        assert_eq!(size_of::<ConstantData>(), 3 * size_of::<usize>());
1961        assert_eq!(size_of::<VCodeConstantData>(), 4 * size_of::<usize>());
1962        assert_eq!(
1963            size_of::<PrimaryMap<VCodeConstant, VCodeConstantData>>(),
1964            3 * size_of::<usize>()
1965        );
1966        // TODO The VCodeConstants structure's memory size could be further optimized.
1967        // With certain versions of Rust, each `HashMap` in `VCodeConstants` occupied at
1968        // least 48 bytes, making an empty `VCodeConstants` cost 120 bytes.
1969    }
1970}