Skip to main content

cranelift_codegen/ir/
dfg.rs

1//! Data flow graph tracking Instructions, Values, and blocks.
2
3use crate::entity::{self, PrimaryMap, SecondaryMap};
4use crate::ir;
5use crate::ir::builder::ReplaceBuilder;
6use crate::ir::dynamic_type::{DynamicTypeData, DynamicTypes};
7use crate::ir::instructions::{CallInfo, InstructionData};
8use crate::ir::user_stack_maps::{UserStackMapEntry, UserStackMapEntryVec};
9use crate::ir::{
10    Block, BlockArg, BlockCall, ConstantData, ConstantPool, DynamicType, ExceptionTables,
11    ExtFuncData, FuncRef, Immediate, Inst, JumpTables, RelSourceLoc, SigRef, Signature, Type,
12    Value, ValueLabelAssignments, ValueList, ValueListPool, types,
13};
14use crate::packed_option::ReservedValue;
15use crate::write::write_operands;
16use core::fmt;
17use core::iter;
18use core::mem;
19use core::ops::{Index, IndexMut};
20use core::u16;
21
22use alloc::collections::BTreeMap;
23#[cfg(feature = "enable-serde")]
24use serde_derive::{Deserialize, Serialize};
25use smallvec::SmallVec;
26
27/// Storage for instructions within the DFG.
28#[derive(Clone, PartialEq, Hash)]
29#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
30pub struct Insts(PrimaryMap<Inst, InstructionData>);
31
32/// Allow immutable access to instructions via indexing.
33impl Index<Inst> for Insts {
34    type Output = InstructionData;
35
36    fn index(&self, inst: Inst) -> &InstructionData {
37        self.0.index(inst)
38    }
39}
40
41/// Allow mutable access to instructions via indexing.
42impl IndexMut<Inst> for Insts {
43    fn index_mut(&mut self, inst: Inst) -> &mut InstructionData {
44        self.0.index_mut(inst)
45    }
46}
47
48/// Storage for basic blocks within the DFG.
49#[derive(Clone, PartialEq, Hash)]
50#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
51pub struct Blocks(PrimaryMap<Block, BlockData>);
52
53impl Blocks {
54    /// Create a new basic block.
55    pub fn add(&mut self) -> Block {
56        self.0.push(BlockData::new())
57    }
58
59    /// Get the total number of basic blocks created in this function, whether they are
60    /// currently inserted in the layout or not.
61    ///
62    /// This is intended for use with `SecondaryMap::with_capacity`.
63    pub fn len(&self) -> usize {
64        self.0.len()
65    }
66
67    /// Reserves capacity for at least `additional` more elements to be
68    /// inserted.
69    pub fn reserve(&mut self, additional: usize) {
70        self.0.reserve(additional);
71    }
72
73    /// Returns `true` if the given block reference is valid.
74    pub fn is_valid(&self, block: Block) -> bool {
75        self.0.is_valid(block)
76    }
77
78    /// Iterate over all blocks, regardless whether a block is actually inserted
79    /// in the layout or not.
80    ///
81    /// Iterates in creation order, not layout order.
82    pub fn iter(&self) -> impl Iterator<Item = Block> {
83        self.0.keys()
84    }
85}
86
87impl Index<Block> for Blocks {
88    type Output = BlockData;
89
90    fn index(&self, block: Block) -> &BlockData {
91        &self.0[block]
92    }
93}
94
95impl IndexMut<Block> for Blocks {
96    fn index_mut(&mut self, block: Block) -> &mut BlockData {
97        &mut self.0[block]
98    }
99}
100
101/// A data flow graph defines all instructions and basic blocks in a function as well as
102/// the data flow dependencies between them. The DFG also tracks values which can be either
103/// instruction results or block parameters.
104///
105/// The layout of blocks in the function and of instructions in each block is recorded by the
106/// `Layout` data structure which forms the other half of the function representation.
107///
108#[derive(Clone, PartialEq, Hash)]
109#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
110pub struct DataFlowGraph {
111    /// Data about all of the instructions in the function, including opcodes and operands.
112    /// The instructions in this map are not in program order. That is tracked by `Layout`, along
113    /// with the block containing each instruction.
114    pub insts: Insts,
115
116    /// List of result values for each instruction.
117    ///
118    /// This map gets resized automatically by `make_inst()` so it is always in sync with the
119    /// primary `insts` map.
120    results: SecondaryMap<Inst, ValueList>,
121
122    /// User-defined stack maps.
123    user_stack_maps: alloc::collections::BTreeMap<Inst, UserStackMapEntryVec>,
124
125    /// basic blocks in the function and their parameters.
126    ///
127    /// This map is not in program order. That is handled by `Layout`, and so is the sequence of
128    /// instructions contained in each block.
129    pub blocks: Blocks,
130
131    /// Dynamic types created.
132    pub dynamic_types: DynamicTypes,
133
134    /// Memory pool of value lists.
135    ///
136    /// The `ValueList` references into this pool appear in many places:
137    ///
138    /// - Instructions in `insts` that don't have room for their entire argument list inline.
139    /// - Instruction result values in `results`.
140    /// - block parameters in `blocks`.
141    pub value_lists: ValueListPool,
142
143    /// Primary value table with entries for all values.
144    values: PrimaryMap<Value, ValueDataPacked>,
145
146    /// Function signature table. These signatures are referenced by indirect call instructions as
147    /// well as the external function references.
148    pub signatures: PrimaryMap<SigRef, Signature>,
149
150    /// External function references. These are functions that can be called directly.
151    pub ext_funcs: PrimaryMap<FuncRef, ExtFuncData>,
152
153    /// Saves Value labels.
154    pub values_labels: Option<BTreeMap<Value, ValueLabelAssignments>>,
155
156    /// Constants used within the function.
157    pub constants: ConstantPool,
158
159    /// Stores large immediates that otherwise will not fit on InstructionData.
160    pub immediates: PrimaryMap<Immediate, ConstantData>,
161
162    /// Jump tables used in this function.
163    pub jump_tables: JumpTables,
164
165    /// Exception tables used in this function.
166    pub exception_tables: ExceptionTables,
167}
168
169impl DataFlowGraph {
170    /// Create a new empty `DataFlowGraph`.
171    pub fn new() -> Self {
172        Self {
173            insts: Insts(PrimaryMap::new()),
174            results: SecondaryMap::new(),
175            user_stack_maps: alloc::collections::BTreeMap::new(),
176            blocks: Blocks(PrimaryMap::new()),
177            dynamic_types: DynamicTypes::new(),
178            value_lists: ValueListPool::new(),
179            values: PrimaryMap::new(),
180            signatures: PrimaryMap::new(),
181            ext_funcs: PrimaryMap::new(),
182            values_labels: None,
183            constants: ConstantPool::new(),
184            immediates: PrimaryMap::new(),
185            jump_tables: JumpTables::new(),
186            exception_tables: ExceptionTables::new(),
187        }
188    }
189
190    /// Clear everything.
191    pub fn clear(&mut self) {
192        self.insts.0.clear();
193        self.results.clear();
194        self.user_stack_maps.clear();
195        self.blocks.0.clear();
196        self.dynamic_types.clear();
197        self.value_lists.clear();
198        self.values.clear();
199        self.signatures.clear();
200        self.ext_funcs.clear();
201        self.values_labels = None;
202        self.constants.clear();
203        self.immediates.clear();
204        self.jump_tables.clear();
205    }
206
207    /// Get the total number of instructions created in this function, whether they are currently
208    /// inserted in the layout or not.
209    ///
210    /// This is intended for use with `SecondaryMap::with_capacity`.
211    pub fn num_insts(&self) -> usize {
212        self.insts.0.len()
213    }
214
215    /// Returns `true` if the given instruction reference is valid.
216    pub fn inst_is_valid(&self, inst: Inst) -> bool {
217        self.insts.0.is_valid(inst)
218    }
219
220    /// Get the total number of basic blocks created in this function, whether they are
221    /// currently inserted in the layout or not.
222    ///
223    /// This is intended for use with `SecondaryMap::with_capacity`.
224    pub fn num_blocks(&self) -> usize {
225        self.blocks.len()
226    }
227
228    /// Returns `true` if the given block reference is valid.
229    pub fn block_is_valid(&self, block: Block) -> bool {
230        self.blocks.is_valid(block)
231    }
232
233    /// Make a BlockCall, bundling together the block and its arguments.
234    pub fn block_call<'a>(
235        &mut self,
236        block: Block,
237        args: impl IntoIterator<Item = &'a BlockArg>,
238    ) -> BlockCall {
239        BlockCall::new(block, args.into_iter().copied(), &mut self.value_lists)
240    }
241
242    /// Get the total number of values.
243    pub fn num_values(&self) -> usize {
244        self.values.len()
245    }
246
247    /// Get an iterator over all values and their definitions.
248    pub fn values_and_defs(&self) -> impl Iterator<Item = (Value, ValueDef)> + '_ {
249        self.values().map(|value| (value, self.value_def(value)))
250    }
251
252    /// Starts collection of debug information.
253    pub fn collect_debug_info(&mut self) {
254        if self.values_labels.is_none() {
255            self.values_labels = Some(Default::default());
256        }
257    }
258
259    /// Inserts a `ValueLabelAssignments::Alias` for `to_alias` if debug info
260    /// collection is enabled.
261    pub fn add_value_label_alias(&mut self, to_alias: Value, from: RelSourceLoc, value: Value) {
262        if let Some(values_labels) = self.values_labels.as_mut() {
263            values_labels.insert(to_alias, ir::ValueLabelAssignments::Alias { from, value });
264        }
265    }
266}
267
268/// Resolve value aliases.
269///
270/// Find the original SSA value that `value` aliases, or None if an
271/// alias cycle is detected.
272fn maybe_resolve_aliases(
273    values: &PrimaryMap<Value, ValueDataPacked>,
274    value: Value,
275) -> Option<Value> {
276    let mut v = value;
277
278    // Note that values may be empty here.
279    for _ in 0..=values.len() {
280        if let ValueData::Alias { original, .. } = ValueData::from(values[v]) {
281            v = original;
282        } else {
283            return Some(v);
284        }
285    }
286
287    None
288}
289
290/// Resolve value aliases.
291///
292/// Find the original SSA value that `value` aliases.
293fn resolve_aliases(values: &PrimaryMap<Value, ValueDataPacked>, value: Value) -> Value {
294    if let Some(v) = maybe_resolve_aliases(values, value) {
295        v
296    } else {
297        panic!("Value alias loop detected for {value}");
298    }
299}
300
301/// Iterator over all Values in a DFG.
302pub struct Values<'a> {
303    inner: entity::Iter<'a, Value, ValueDataPacked>,
304}
305
306/// Check for non-values.
307fn valid_valuedata(data: ValueDataPacked) -> bool {
308    let data = ValueData::from(data);
309    if let ValueData::Alias {
310        ty: types::INVALID,
311        original,
312    } = data
313    {
314        if original == Value::reserved_value() {
315            return false;
316        }
317    }
318    true
319}
320
321impl<'a> Iterator for Values<'a> {
322    type Item = Value;
323
324    fn next(&mut self) -> Option<Self::Item> {
325        self.inner
326            .by_ref()
327            .find(|kv| valid_valuedata(*kv.1))
328            .map(|kv| kv.0)
329    }
330
331    fn size_hint(&self) -> (usize, Option<usize>) {
332        self.inner.size_hint()
333    }
334}
335
336impl ExactSizeIterator for Values<'_> {
337    fn len(&self) -> usize {
338        self.inner.len()
339    }
340}
341
342/// Handling values.
343///
344/// Values are either block parameters or instruction results.
345impl DataFlowGraph {
346    /// Allocate an extended value entry.
347    fn make_value(&mut self, data: ValueData) -> Value {
348        self.values.push(data.into())
349    }
350
351    /// The number of values defined in this DFG.
352    pub fn len_values(&self) -> usize {
353        self.values.len()
354    }
355
356    /// Get an iterator over all values.
357    pub fn values<'a>(&'a self) -> Values<'a> {
358        Values {
359            inner: self.values.iter(),
360        }
361    }
362
363    /// Check if a value reference is valid.
364    pub fn value_is_valid(&self, v: Value) -> bool {
365        self.values.is_valid(v)
366    }
367
368    /// Check whether a value is valid and not an alias.
369    pub fn value_is_real(&self, value: Value) -> bool {
370        // Deleted or unused values are also stored as aliases so this excludes
371        // those as well.
372        self.value_is_valid(value) && !matches!(self.values[value].into(), ValueData::Alias { .. })
373    }
374
375    /// Is the given value an alias?
376    pub fn value_is_alias(&self, v: Value) -> bool {
377        match ValueData::from(self.values[v]) {
378            ValueData::Alias { .. } => true,
379            ValueData::Inst { .. } | ValueData::Param { .. } | ValueData::Union { .. } => false,
380        }
381    }
382
383    /// Get the type of a value.
384    pub fn value_type(&self, v: Value) -> Type {
385        self.values[v].ty()
386    }
387
388    /// Get the definition of a value.
389    ///
390    /// This is either the instruction that defined it or the Block that has the value as an
391    /// parameter.
392    pub fn value_def(&self, v: Value) -> ValueDef {
393        match ValueData::from(self.values[v]) {
394            ValueData::Inst { inst, num, .. } => ValueDef::Result(inst, num as usize),
395            ValueData::Param { block, num, .. } => ValueDef::Param(block, num as usize),
396            ValueData::Alias { original, .. } => {
397                // Make sure we only recurse one level. `resolve_aliases` has safeguards to
398                // detect alias loops without overrunning the stack.
399                self.value_def(self.resolve_aliases(original))
400            }
401            ValueData::Union { x, y, .. } => ValueDef::Union(x, y),
402        }
403    }
404
405    /// Determine if `v` is an attached instruction result / block parameter.
406    ///
407    /// An attached value can't be attached to something else without first being detached.
408    ///
409    /// Value aliases are not considered to be attached to anything. Use `resolve_aliases()` to
410    /// determine if the original aliased value is attached.
411    pub fn value_is_attached(&self, v: Value) -> bool {
412        use self::ValueData::*;
413        match ValueData::from(self.values[v]) {
414            Inst { inst, num, .. } => Some(&v) == self.inst_results(inst).get(num as usize),
415            Param { block, num, .. } => Some(&v) == self.block_params(block).get(num as usize),
416            Alias { .. } => false,
417            Union { .. } => false,
418        }
419    }
420
421    /// Resolve value aliases.
422    ///
423    /// Find the original SSA value that `value` aliases.
424    pub fn resolve_aliases(&self, value: Value) -> Value {
425        resolve_aliases(&self.values, value)
426    }
427
428    /// Replace all uses of value aliases with their resolved values, and delete
429    /// the aliases.
430    pub fn resolve_all_aliases(&mut self) {
431        let invalid_value = ValueDataPacked::from(ValueData::Alias {
432            ty: types::INVALID,
433            original: Value::reserved_value(),
434        });
435
436        // Rewrite each chain of aliases. Update every alias along the chain
437        // into an alias directly to the final value. Due to updating every
438        // alias that it looks at, this loop runs in time linear in the number
439        // of values.
440        for mut src in self.values.keys() {
441            let value_data = self.values[src];
442            if value_data == invalid_value {
443                continue;
444            }
445            if let ValueData::Alias { mut original, .. } = value_data.into() {
446                // We don't use the type after this, we just need some place to
447                // store the resolved aliases temporarily.
448                let resolved = ValueDataPacked::from(ValueData::Alias {
449                    ty: types::INVALID,
450                    original: resolve_aliases(&self.values, original),
451                });
452                // Walk the chain again, splatting the new alias everywhere.
453                // resolve_aliases panics if there's an alias cycle, so we don't
454                // need to guard against cycles here.
455                loop {
456                    self.values[src] = resolved;
457                    src = original;
458                    if let ValueData::Alias { original: next, .. } = self.values[src].into() {
459                        original = next;
460                    } else {
461                        break;
462                    }
463                }
464            }
465        }
466
467        // Now aliases don't point to other aliases, so we can replace any use
468        // of an alias with the final value in constant time.
469
470        // Rewrite InstructionData in `self.insts`.
471        for inst in self.insts.0.values_mut() {
472            inst.map_values(
473                &mut self.value_lists,
474                &mut self.jump_tables,
475                &mut self.exception_tables,
476                |arg| {
477                    if let ValueData::Alias { original, .. } = self.values[arg].into() {
478                        original
479                    } else {
480                        arg
481                    }
482                },
483            );
484        }
485
486        // - `results` and block-params in `blocks` are not aliases, by
487        //   definition.
488        // - `dynamic_types` has no values.
489        // - `value_lists` can only be accessed via references from elsewhere.
490        // - `values` only has value references in aliases (which we've
491        //   removed), and unions (but the egraph pass ensures there are no
492        //   aliases before creating unions).
493
494        // - `signatures` and `ext_funcs` have no values.
495
496        if let Some(values_labels) = &mut self.values_labels {
497            // Debug info is best-effort. If any is attached to value aliases,
498            // just discard it.
499            values_labels.retain(|&k, _| !matches!(self.values[k].into(), ValueData::Alias { .. }));
500
501            // If debug-info says a value should have the same labels as another
502            // value, then make sure that target is not a value alias.
503            for value_label in values_labels.values_mut() {
504                if let ValueLabelAssignments::Alias { value, .. } = value_label {
505                    if let ValueData::Alias { original, .. } = self.values[*value].into() {
506                        *value = original;
507                    }
508                }
509            }
510        }
511
512        // - `constants` and `immediates` have no values.
513        // - `jump_tables` is updated together with instruction-data above.
514
515        // Delete all aliases now that there are no uses left.
516        for value in self.values.values_mut() {
517            if let ValueData::Alias { .. } = ValueData::from(*value) {
518                *value = invalid_value;
519            }
520        }
521    }
522
523    /// Turn a value into an alias of another.
524    ///
525    /// Change the `dest` value to behave as an alias of `src`. This means that all uses of `dest`
526    /// will behave as if they used that value `src`.
527    ///
528    /// The `dest` value can't be attached to an instruction or block.
529    pub fn change_to_alias(&mut self, dest: Value, src: Value) {
530        debug_assert!(!self.value_is_attached(dest));
531        // Try to create short alias chains by finding the original source value.
532        // This also avoids the creation of loops.
533        let original = self.resolve_aliases(src);
534        debug_assert_ne!(
535            dest, original,
536            "Aliasing {dest} to {src} would create a loop"
537        );
538        let ty = self.value_type(original);
539        debug_assert_eq!(
540            self.value_type(dest),
541            ty,
542            "Aliasing {} to {} would change its type {} to {}",
543            dest,
544            src,
545            self.value_type(dest),
546            ty
547        );
548        debug_assert_ne!(ty, types::INVALID);
549
550        self.values[dest] = ValueData::Alias { ty, original }.into();
551    }
552
553    /// Replace the results of one instruction with aliases to the results of another.
554    ///
555    /// Change all the results of `dest_inst` to behave as aliases of
556    /// corresponding results of `src_inst`, as if calling change_to_alias for
557    /// each.
558    ///
559    /// After calling this instruction, `dest_inst` will have had its results
560    /// cleared, so it likely needs to be removed from the graph.
561    ///
562    pub fn replace_with_aliases(&mut self, dest_inst: Inst, original_inst: Inst) {
563        debug_assert_ne!(
564            dest_inst, original_inst,
565            "Replacing {dest_inst} with itself would create a loop"
566        );
567
568        let dest_results = self.results[dest_inst].as_slice(&self.value_lists);
569        let original_results = self.results[original_inst].as_slice(&self.value_lists);
570
571        debug_assert_eq!(
572            dest_results.len(),
573            original_results.len(),
574            "Replacing {dest_inst} with {original_inst} would produce a different number of results."
575        );
576
577        for (&dest, &original) in dest_results.iter().zip(original_results) {
578            let ty = self.value_type(original);
579            debug_assert_eq!(
580                self.value_type(dest),
581                ty,
582                "Aliasing {} to {} would change its type {} to {}",
583                dest,
584                original,
585                self.value_type(dest),
586                ty
587            );
588            debug_assert_ne!(ty, types::INVALID);
589
590            self.values[dest] = ValueData::Alias { ty, original }.into();
591        }
592
593        self.clear_results(dest_inst);
594    }
595
596    /// Get the stack map entries associated with the given instruction.
597    pub fn user_stack_map_entries(&self, inst: Inst) -> Option<&[UserStackMapEntry]> {
598        self.user_stack_maps.get(&inst).map(|es| &**es)
599    }
600
601    /// Append a new stack map entry for the given call instruction.
602    ///
603    /// # Panics
604    ///
605    /// Panics if the given instruction is not a (non-tail) call instruction.
606    pub fn append_user_stack_map_entry(&mut self, inst: Inst, entry: UserStackMapEntry) {
607        let opcode = self.insts[inst].opcode();
608        assert!(opcode.is_safepoint());
609        self.user_stack_maps.entry(inst).or_default().push(entry);
610    }
611
612    /// Append multiple stack map entries for the given call instruction.
613    ///
614    /// # Panics
615    ///
616    /// Panics if the given instruction is not a (non-tail) call instruction.
617    pub fn append_user_stack_map_entries(
618        &mut self,
619        inst: Inst,
620        entries: impl IntoIterator<Item = UserStackMapEntry>,
621    ) {
622        for entry in entries {
623            self.append_user_stack_map_entry(inst, entry);
624        }
625    }
626
627    /// Take the stack map entries for a given instruction, leaving the
628    /// instruction without stack maps.
629    pub(crate) fn take_user_stack_map_entries(
630        &mut self,
631        inst: Inst,
632    ) -> Option<UserStackMapEntryVec> {
633        self.user_stack_maps.remove(&inst)
634    }
635}
636
637/// Where did a value come from?
638#[derive(Clone, Copy, Debug, PartialEq, Eq)]
639pub enum ValueDef {
640    /// Value is the n'th result of an instruction.
641    Result(Inst, usize),
642    /// Value is the n'th parameter to a block.
643    Param(Block, usize),
644    /// Value is a union of two other values.
645    Union(Value, Value),
646}
647
648impl ValueDef {
649    /// Unwrap the instruction where the value was defined, or panic.
650    pub fn unwrap_inst(&self) -> Inst {
651        self.inst().expect("Value is not an instruction result")
652    }
653
654    /// Get the instruction where the value was defined, if any.
655    pub fn inst(&self) -> Option<Inst> {
656        match *self {
657            Self::Result(inst, _) => Some(inst),
658            _ => None,
659        }
660    }
661
662    /// Unwrap the block there the parameter is defined, or panic.
663    pub fn unwrap_block(&self) -> Block {
664        match *self {
665            Self::Param(block, _) => block,
666            _ => panic!("Value is not a block parameter"),
667        }
668    }
669
670    /// Get the number component of this definition.
671    ///
672    /// When multiple values are defined at the same program point, this indicates the index of
673    /// this value.
674    pub fn num(self) -> usize {
675        match self {
676            Self::Result(_, n) | Self::Param(_, n) => n,
677            Self::Union(_, _) => 0,
678        }
679    }
680}
681
682/// Internal table storage for extended values.
683#[derive(Clone, Debug, PartialEq, Hash)]
684#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
685enum ValueData {
686    /// Value is defined by an instruction.
687    Inst { ty: Type, num: u16, inst: Inst },
688
689    /// Value is a block parameter.
690    Param { ty: Type, num: u16, block: Block },
691
692    /// Value is an alias of another value.
693    /// An alias value can't be linked as an instruction result or block parameter. It is used as a
694    /// placeholder when the original instruction or block has been rewritten or modified.
695    Alias { ty: Type, original: Value },
696
697    /// Union is a "fork" in representation: the value can be
698    /// represented as either of the values named here. This is used
699    /// for aegraph (acyclic egraph) representation in the DFG.
700    Union { ty: Type, x: Value, y: Value },
701}
702
703/// Bit-packed version of ValueData, for efficiency.
704///
705/// Layout:
706///
707/// ```plain
708///        | tag:2 |  type:14        |    x:32       | y:32          |
709///
710/// Inst       00     ty               inst output     inst index
711/// Param      01     ty               blockparam num  block index
712/// Alias      10     ty               0               value index
713/// Union      11     ty               first value     second value
714/// ```
715#[derive(Clone, Copy, Debug, PartialEq, Hash)]
716#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
717#[repr(Rust, packed)]
718struct ValueDataPacked {
719    x: u32,
720    y: u32,
721    flags_and_type: u16,
722}
723
724impl ValueDataPacked {
725    const TYPE_SHIFT: u8 = 0;
726    const TYPE_BITS: u8 = 14;
727    const TAG_SHIFT: u8 = Self::TYPE_SHIFT + Self::TYPE_BITS;
728    const TAG_BITS: u8 = 2;
729
730    const TAG_INST: u16 = 0;
731    const TAG_PARAM: u16 = 1;
732    const TAG_ALIAS: u16 = 2;
733    const TAG_UNION: u16 = 3;
734
735    fn make(tag: u16, ty: Type, x: u32, y: u32) -> ValueDataPacked {
736        debug_assert!(tag < (1 << Self::TAG_BITS));
737        debug_assert!(ty.repr() < (1 << Self::TYPE_BITS));
738
739        ValueDataPacked {
740            x,
741            y,
742            flags_and_type: (tag << Self::TAG_SHIFT) | (ty.repr() << Self::TYPE_SHIFT),
743        }
744    }
745
746    #[inline(always)]
747    fn field(self, shift: u8, bits: u8) -> u16 {
748        (self.flags_and_type >> shift) & ((1 << bits) - 1)
749    }
750
751    #[inline(always)]
752    fn ty(self) -> Type {
753        let ty = self.field(ValueDataPacked::TYPE_SHIFT, ValueDataPacked::TYPE_BITS);
754        Type::from_repr(ty)
755    }
756
757    #[inline(always)]
758    fn set_type(&mut self, ty: Type) {
759        self.flags_and_type &= !(((1 << Self::TYPE_BITS) - 1) << Self::TYPE_SHIFT);
760        self.flags_and_type |= ty.repr() << Self::TYPE_SHIFT;
761    }
762}
763
764impl From<ValueData> for ValueDataPacked {
765    fn from(data: ValueData) -> Self {
766        match data {
767            ValueData::Inst { ty, num, inst } => {
768                Self::make(Self::TAG_INST, ty, num.into(), inst.as_bits())
769            }
770            ValueData::Param { ty, num, block } => {
771                Self::make(Self::TAG_PARAM, ty, num.into(), block.as_bits())
772            }
773            ValueData::Alias { ty, original } => {
774                Self::make(Self::TAG_ALIAS, ty, 0, original.as_bits())
775            }
776            ValueData::Union { ty, x, y } => {
777                Self::make(Self::TAG_UNION, ty, x.as_bits(), y.as_bits())
778            }
779        }
780    }
781}
782
783impl From<ValueDataPacked> for ValueData {
784    fn from(data: ValueDataPacked) -> Self {
785        let tag = data.field(ValueDataPacked::TAG_SHIFT, ValueDataPacked::TAG_BITS);
786        let ty = data.field(ValueDataPacked::TYPE_SHIFT, ValueDataPacked::TYPE_BITS);
787
788        let ty = Type::from_repr(ty);
789        match tag {
790            ValueDataPacked::TAG_INST => ValueData::Inst {
791                ty,
792                num: u16::try_from(data.x).expect("Inst result num should fit in u16"),
793                inst: Inst::from_bits(data.y),
794            },
795            ValueDataPacked::TAG_PARAM => ValueData::Param {
796                ty,
797                num: u16::try_from(data.x).expect("Blockparam index should fit in u16"),
798                block: Block::from_bits(data.y),
799            },
800            ValueDataPacked::TAG_ALIAS => ValueData::Alias {
801                ty,
802                original: Value::from_bits(data.y),
803            },
804            ValueDataPacked::TAG_UNION => ValueData::Union {
805                ty,
806                x: Value::from_bits(data.x),
807                y: Value::from_bits(data.y),
808            },
809            _ => panic!("Invalid tag {tag} in ValueDataPacked"),
810        }
811    }
812}
813
814/// Instructions.
815///
816impl DataFlowGraph {
817    /// Create a new instruction.
818    ///
819    /// The type of the first result is indicated by `data.ty`. If the
820    /// instruction produces multiple results, also call
821    /// `make_inst_results` to allocate value table entries. (It is
822    /// always safe to call `make_inst_results`, regardless of how
823    /// many results the instruction has.)
824    pub fn make_inst(&mut self, data: InstructionData) -> Inst {
825        let n = self.num_insts() + 1;
826        self.results.resize(n);
827        self.insts.0.push(data)
828    }
829
830    /// Declares a dynamic vector type
831    pub fn make_dynamic_ty(&mut self, data: DynamicTypeData) -> DynamicType {
832        self.dynamic_types.push(data)
833    }
834
835    /// Returns an object that displays `inst`.
836    pub fn display_inst<'a>(&'a self, inst: Inst) -> DisplayInst<'a> {
837        DisplayInst(self, inst)
838    }
839
840    /// Returns an object that displays the given `value`'s defining instruction.
841    ///
842    /// Panics if the value is not defined by an instruction (i.e. it is a basic
843    /// block argument).
844    pub fn display_value_inst(&self, value: Value) -> DisplayInst<'_> {
845        match self.value_def(value) {
846            ir::ValueDef::Result(inst, _) => self.display_inst(inst),
847            ir::ValueDef::Param(_, _) => panic!("value is not defined by an instruction"),
848            ir::ValueDef::Union(_, _) => panic!("value is a union of two other values"),
849        }
850    }
851
852    /// Construct a read-only visitor context for the values of this instruction.
853    pub fn inst_values<'dfg>(
854        &'dfg self,
855        inst: Inst,
856    ) -> impl DoubleEndedIterator<Item = Value> + 'dfg {
857        self.inst_args(inst)
858            .iter()
859            .copied()
860            .chain(
861                self.insts[inst]
862                    .branch_destination(&self.jump_tables, &self.exception_tables)
863                    .into_iter()
864                    .flat_map(|branch| {
865                        branch
866                            .args(&self.value_lists)
867                            .filter_map(|arg| arg.as_value())
868                    }),
869            )
870            .chain(
871                self.insts[inst]
872                    .exception_table()
873                    .into_iter()
874                    .flat_map(|et| self.exception_tables[et].contexts()),
875            )
876    }
877
878    /// Map a function over the values of the instruction.
879    pub fn map_inst_values<F>(&mut self, inst: Inst, body: F)
880    where
881        F: FnMut(Value) -> Value,
882    {
883        self.insts[inst].map_values(
884            &mut self.value_lists,
885            &mut self.jump_tables,
886            &mut self.exception_tables,
887            body,
888        );
889    }
890
891    /// Overwrite the instruction's value references with values from the iterator.
892    /// NOTE: the iterator provided is expected to yield at least as many values as the instruction
893    /// currently has.
894    pub fn overwrite_inst_values<I>(&mut self, inst: Inst, mut values: I)
895    where
896        I: Iterator<Item = Value>,
897    {
898        self.insts[inst].map_values(
899            &mut self.value_lists,
900            &mut self.jump_tables,
901            &mut self.exception_tables,
902            |_| values.next().unwrap(),
903        );
904    }
905
906    /// Get all value arguments on `inst` as a slice.
907    pub fn inst_args(&self, inst: Inst) -> &[Value] {
908        self.insts[inst].arguments(&self.value_lists)
909    }
910
911    /// Get all value arguments on `inst` as a mutable slice.
912    pub fn inst_args_mut(&mut self, inst: Inst) -> &mut [Value] {
913        self.insts[inst].arguments_mut(&mut self.value_lists)
914    }
915
916    /// Get the fixed value arguments on `inst` as a slice.
917    pub fn inst_fixed_args(&self, inst: Inst) -> &[Value] {
918        let num_fixed_args = self.insts[inst]
919            .opcode()
920            .constraints()
921            .num_fixed_value_arguments();
922        &self.inst_args(inst)[..num_fixed_args]
923    }
924
925    /// Get the fixed value arguments on `inst` as a mutable slice.
926    pub fn inst_fixed_args_mut(&mut self, inst: Inst) -> &mut [Value] {
927        let num_fixed_args = self.insts[inst]
928            .opcode()
929            .constraints()
930            .num_fixed_value_arguments();
931        &mut self.inst_args_mut(inst)[..num_fixed_args]
932    }
933
934    /// Get the variable value arguments on `inst` as a slice.
935    pub fn inst_variable_args(&self, inst: Inst) -> &[Value] {
936        let num_fixed_args = self.insts[inst]
937            .opcode()
938            .constraints()
939            .num_fixed_value_arguments();
940        &self.inst_args(inst)[num_fixed_args..]
941    }
942
943    /// Get the variable value arguments on `inst` as a mutable slice.
944    pub fn inst_variable_args_mut(&mut self, inst: Inst) -> &mut [Value] {
945        let num_fixed_args = self.insts[inst]
946            .opcode()
947            .constraints()
948            .num_fixed_value_arguments();
949        &mut self.inst_args_mut(inst)[num_fixed_args..]
950    }
951
952    /// Create result values for an instruction that produces multiple results.
953    ///
954    /// Instructions that produce no result values only need to be created with `make_inst`,
955    /// otherwise call `make_inst_results` to allocate value table entries for the results.
956    ///
957    /// The result value types are determined from the instruction's value type constraints and the
958    /// provided `ctrl_typevar` type for polymorphic instructions. For non-polymorphic
959    /// instructions, `ctrl_typevar` is ignored, and `INVALID` can be used.
960    ///
961    /// The type of the first result value is also set, even if it was already set in the
962    /// `InstructionData` passed to `make_inst`. If this function is called with a single-result
963    /// instruction, that is the only effect.
964    pub fn make_inst_results(&mut self, inst: Inst, ctrl_typevar: Type) -> usize {
965        self.make_inst_results_reusing(inst, ctrl_typevar, iter::empty())
966    }
967
968    /// Create result values for `inst`, reusing the provided detached values.
969    ///
970    /// Create a new set of result values for `inst` using `ctrl_typevar` to determine the result
971    /// types. Any values provided by `reuse` will be reused. When `reuse` is exhausted or when it
972    /// produces `None`, a new value is created.
973    pub fn make_inst_results_reusing<I>(
974        &mut self,
975        inst: Inst,
976        ctrl_typevar: Type,
977        reuse: I,
978    ) -> usize
979    where
980        I: Iterator<Item = Option<Value>>,
981    {
982        self.clear_results(inst);
983
984        let mut reuse = reuse.fuse();
985        let result_tys: SmallVec<[_; 16]> = self.inst_result_types(inst, ctrl_typevar).collect();
986
987        for (expected, &ty) in result_tys.iter().enumerate() {
988            let num = u16::try_from(expected).expect("Result value index should fit in u16");
989            let value_data = ValueData::Inst { ty, num, inst };
990            let v = if let Some(Some(v)) = reuse.next() {
991                debug_assert_eq!(self.value_type(v), ty, "Reused {ty} is wrong type");
992                debug_assert!(!self.value_is_attached(v));
993                self.values[v] = value_data.into();
994                v
995            } else {
996                self.make_value(value_data)
997            };
998            let actual = self.results[inst].push(v, &mut self.value_lists);
999            debug_assert_eq!(expected, actual);
1000        }
1001
1002        result_tys.len()
1003    }
1004
1005    /// Create a `ReplaceBuilder` that will replace `inst` with a new instruction in place.
1006    pub fn replace(&mut self, inst: Inst) -> ReplaceBuilder<'_> {
1007        ReplaceBuilder::new(self, inst)
1008    }
1009
1010    /// Clear the list of result values from `inst`.
1011    ///
1012    /// This leaves `inst` without any result values. New result values can be created by calling
1013    /// `make_inst_results` or by using a `replace(inst)` builder.
1014    pub fn clear_results(&mut self, inst: Inst) {
1015        self.results[inst].clear(&mut self.value_lists)
1016    }
1017
1018    /// Replace an instruction result with a new value of type `new_type`.
1019    ///
1020    /// The `old_value` must be an attached instruction result.
1021    ///
1022    /// The old value is left detached, so it should probably be changed into something else.
1023    ///
1024    /// Returns the new value.
1025    pub fn replace_result(&mut self, old_value: Value, new_type: Type) -> Value {
1026        let (num, inst) = match ValueData::from(self.values[old_value]) {
1027            ValueData::Inst { num, inst, .. } => (num, inst),
1028            _ => panic!("{old_value} is not an instruction result value"),
1029        };
1030        let new_value = self.make_value(ValueData::Inst {
1031            ty: new_type,
1032            num,
1033            inst,
1034        });
1035        let num = num as usize;
1036        let attached = mem::replace(
1037            self.results[inst]
1038                .get_mut(num, &mut self.value_lists)
1039                .expect("Replacing detached result"),
1040            new_value,
1041        );
1042        debug_assert_eq!(
1043            attached,
1044            old_value,
1045            "{} wasn't detached from {}",
1046            old_value,
1047            self.display_inst(inst)
1048        );
1049        new_value
1050    }
1051
1052    /// Clone an instruction, attaching new result `Value`s and
1053    /// returning them.
1054    pub fn clone_inst(&mut self, inst: Inst) -> Inst {
1055        // First, add a clone of the InstructionData.
1056        let inst_data = self.insts[inst];
1057        // If the `inst_data` has a reference to a ValueList, clone it
1058        // as well, because we can't share these (otherwise mutating
1059        // one would affect the other).
1060        let inst_data = inst_data.deep_clone(&mut self.value_lists);
1061        let new_inst = self.make_inst(inst_data);
1062        // Get the controlling type variable.
1063        let ctrl_typevar = self.ctrl_typevar(inst);
1064        // Create new result values.
1065        self.make_inst_results(new_inst, ctrl_typevar);
1066        new_inst
1067    }
1068
1069    /// Get the first result of an instruction.
1070    ///
1071    /// This function panics if the instruction doesn't have any result.
1072    pub fn first_result(&self, inst: Inst) -> Value {
1073        self.results[inst]
1074            .first(&self.value_lists)
1075            .unwrap_or_else(|| panic!("{inst} has no results"))
1076    }
1077
1078    /// Test if `inst` has any result values currently.
1079    pub fn has_results(&self, inst: Inst) -> bool {
1080        !self.results[inst].is_empty()
1081    }
1082
1083    /// Return all the results of an instruction.
1084    pub fn inst_results(&self, inst: Inst) -> &[Value] {
1085        self.results[inst].as_slice(&self.value_lists)
1086    }
1087
1088    /// Return all the results of an instruction as ValueList.
1089    pub fn inst_results_list(&self, inst: Inst) -> ValueList {
1090        self.results[inst]
1091    }
1092
1093    /// Create a union of two values.
1094    pub fn union(&mut self, x: Value, y: Value) -> Value {
1095        // Get the type.
1096        let ty = self.value_type(x);
1097        debug_assert_eq!(ty, self.value_type(y));
1098        self.make_value(ValueData::Union { ty, x, y })
1099    }
1100
1101    /// Get the call signature of a direct or indirect call instruction.
1102    /// Returns `None` if `inst` is not a call instruction.
1103    pub fn call_signature(&self, inst: Inst) -> Option<SigRef> {
1104        match self.insts[inst].analyze_call(&self.value_lists, &self.exception_tables) {
1105            CallInfo::NotACall => None,
1106            CallInfo::Direct(f, _) => Some(self.ext_funcs[f].signature),
1107            CallInfo::DirectWithSig(_, s, _) => Some(s),
1108            CallInfo::Indirect(s, _) => Some(s),
1109        }
1110    }
1111
1112    /// Like `call_signature` but returns none for tail call
1113    /// instructions and try-call (exception-handling invoke)
1114    /// instructions.
1115    fn non_tail_call_or_try_call_signature(&self, inst: Inst) -> Option<SigRef> {
1116        let sig = self.call_signature(inst)?;
1117        match self.insts[inst].opcode() {
1118            ir::Opcode::ReturnCall | ir::Opcode::ReturnCallIndirect => None,
1119            ir::Opcode::TryCall | ir::Opcode::TryCallIndirect => None,
1120            _ => Some(sig),
1121        }
1122    }
1123
1124    // Only for use by the verifier. Everyone else should just use
1125    // `dfg.inst_results(inst).len()`.
1126    pub(crate) fn num_expected_results_for_verifier(&self, inst: Inst) -> usize {
1127        match self.non_tail_call_or_try_call_signature(inst) {
1128            Some(sig) => self.signatures[sig].returns.len(),
1129            None => {
1130                let constraints = self.insts[inst].opcode().constraints();
1131                constraints.num_fixed_results()
1132            }
1133        }
1134    }
1135
1136    /// Get the result types of the given instruction.
1137    pub fn inst_result_types<'a>(
1138        &'a self,
1139        inst: Inst,
1140        ctrl_typevar: Type,
1141    ) -> impl iter::ExactSizeIterator<Item = Type> + 'a {
1142        return match self.non_tail_call_or_try_call_signature(inst) {
1143            Some(sig) => InstResultTypes::Signature(self, sig, 0),
1144            None => {
1145                let constraints = self.insts[inst].opcode().constraints();
1146                InstResultTypes::Constraints(constraints, ctrl_typevar, 0)
1147            }
1148        };
1149
1150        enum InstResultTypes<'a> {
1151            Signature(&'a DataFlowGraph, SigRef, usize),
1152            Constraints(ir::instructions::OpcodeConstraints, Type, usize),
1153        }
1154
1155        impl Iterator for InstResultTypes<'_> {
1156            type Item = Type;
1157
1158            fn next(&mut self) -> Option<Type> {
1159                match self {
1160                    InstResultTypes::Signature(dfg, sig, i) => {
1161                        let param = dfg.signatures[*sig].returns.get(*i)?;
1162                        *i += 1;
1163                        Some(param.value_type)
1164                    }
1165                    InstResultTypes::Constraints(constraints, ctrl_ty, i) => {
1166                        if *i < constraints.num_fixed_results() {
1167                            let ty = constraints.result_type(*i, *ctrl_ty);
1168                            *i += 1;
1169                            Some(ty)
1170                        } else {
1171                            None
1172                        }
1173                    }
1174                }
1175            }
1176
1177            fn size_hint(&self) -> (usize, Option<usize>) {
1178                let len = match self {
1179                    InstResultTypes::Signature(dfg, sig, i) => {
1180                        dfg.signatures[*sig].returns.len() - *i
1181                    }
1182                    InstResultTypes::Constraints(constraints, _, i) => {
1183                        constraints.num_fixed_results() - *i
1184                    }
1185                };
1186                (len, Some(len))
1187            }
1188        }
1189
1190        impl ExactSizeIterator for InstResultTypes<'_> {}
1191    }
1192
1193    /// Compute the type of an instruction result from opcode constraints and call signatures.
1194    ///
1195    /// This computes the same sequence of result types that `make_inst_results()` above would
1196    /// assign to the created result values, but it does not depend on `make_inst_results()` being
1197    /// called first.
1198    ///
1199    /// Returns `None` if asked about a result index that is too large.
1200    pub fn compute_result_type(
1201        &self,
1202        inst: Inst,
1203        result_idx: usize,
1204        ctrl_typevar: Type,
1205    ) -> Option<Type> {
1206        self.inst_result_types(inst, ctrl_typevar).nth(result_idx)
1207    }
1208
1209    /// Get the controlling type variable, or `INVALID` if `inst` isn't polymorphic.
1210    pub fn ctrl_typevar(&self, inst: Inst) -> Type {
1211        let constraints = self.insts[inst].opcode().constraints();
1212
1213        if !constraints.is_polymorphic() {
1214            types::INVALID
1215        } else if constraints.requires_typevar_operand() {
1216            // Not all instruction formats have a designated operand, but in that case
1217            // `requires_typevar_operand()` should never be true.
1218            self.value_type(
1219                self.insts[inst]
1220                    .typevar_operand(&self.value_lists)
1221                    .unwrap_or_else(|| {
1222                        panic!(
1223                            "Instruction format for {:?} doesn't have a designated operand",
1224                            self.insts[inst]
1225                        )
1226                    }),
1227            )
1228        } else {
1229            self.value_type(self.first_result(inst))
1230        }
1231    }
1232}
1233
1234/// basic blocks.
1235impl DataFlowGraph {
1236    /// Create a new basic block.
1237    pub fn make_block(&mut self) -> Block {
1238        self.blocks.add()
1239    }
1240
1241    /// Get the number of parameters on `block`.
1242    pub fn num_block_params(&self, block: Block) -> usize {
1243        self.blocks[block].params(&self.value_lists).len()
1244    }
1245
1246    /// Get the parameters on `block`.
1247    pub fn block_params(&self, block: Block) -> &[Value] {
1248        self.blocks[block].params(&self.value_lists)
1249    }
1250
1251    /// Get the types of the parameters on `block`.
1252    pub fn block_param_types(&self, block: Block) -> impl Iterator<Item = Type> + '_ {
1253        self.block_params(block).iter().map(|&v| self.value_type(v))
1254    }
1255
1256    /// Append a parameter with type `ty` to `block`.
1257    pub fn append_block_param(&mut self, block: Block, ty: Type) -> Value {
1258        let param = self.values.next_key();
1259        let num = self.blocks[block].params.push(param, &mut self.value_lists);
1260        debug_assert!(num <= u16::MAX as usize, "Too many parameters on block");
1261        self.make_value(ValueData::Param {
1262            ty,
1263            num: num as u16,
1264            block,
1265        })
1266    }
1267
1268    /// Removes `val` from `block`'s parameters by swapping it with the last parameter on `block`.
1269    /// Returns the position of `val` before removal.
1270    ///
1271    /// *Important*: to ensure O(1) deletion, this method swaps the removed parameter with the
1272    /// last `block` parameter. This can disrupt all the branch instructions jumping to this
1273    /// `block` for which you have to change the branch argument order if necessary.
1274    ///
1275    /// Panics if `val` is not a block parameter.
1276    pub fn swap_remove_block_param(&mut self, val: Value) -> usize {
1277        let (block, num) =
1278            if let ValueData::Param { num, block, .. } = ValueData::from(self.values[val]) {
1279                (block, num)
1280            } else {
1281                panic!("{val} must be a block parameter");
1282            };
1283        self.blocks[block]
1284            .params
1285            .swap_remove(num as usize, &mut self.value_lists);
1286        if let Some(last_arg_val) = self.blocks[block]
1287            .params
1288            .get(num as usize, &self.value_lists)
1289        {
1290            // We update the position of the old last arg.
1291            let mut last_arg_data = ValueData::from(self.values[last_arg_val]);
1292            if let ValueData::Param { num: old_num, .. } = &mut last_arg_data {
1293                *old_num = num;
1294                self.values[last_arg_val] = last_arg_data.into();
1295            } else {
1296                panic!("{last_arg_val} should be a Block parameter");
1297            }
1298        }
1299        num as usize
1300    }
1301
1302    /// Removes `val` from `block`'s parameters by a standard linear time list removal which
1303    /// preserves ordering. Also updates the values' data.
1304    pub fn remove_block_param(&mut self, val: Value) {
1305        let (block, num) =
1306            if let ValueData::Param { num, block, .. } = ValueData::from(self.values[val]) {
1307                (block, num)
1308            } else {
1309                panic!("{val} must be a block parameter");
1310            };
1311        self.blocks[block]
1312            .params
1313            .remove(num as usize, &mut self.value_lists);
1314        for index in num..(self.num_block_params(block) as u16) {
1315            let packed = &mut self.values[self.blocks[block]
1316                .params
1317                .get(index as usize, &self.value_lists)
1318                .unwrap()];
1319            let mut data = ValueData::from(*packed);
1320            match &mut data {
1321                ValueData::Param { num, .. } => {
1322                    *num -= 1;
1323                    *packed = data.into();
1324                }
1325                _ => panic!(
1326                    "{} must be a block parameter",
1327                    self.blocks[block]
1328                        .params
1329                        .get(index as usize, &self.value_lists)
1330                        .unwrap()
1331                ),
1332            }
1333        }
1334    }
1335
1336    /// Append an existing value to `block`'s parameters.
1337    ///
1338    /// The appended value can't already be attached to something else.
1339    ///
1340    /// In almost all cases, you should be using `append_block_param()` instead of this method.
1341    pub fn attach_block_param(&mut self, block: Block, param: Value) {
1342        debug_assert!(!self.value_is_attached(param));
1343        let num = self.blocks[block].params.push(param, &mut self.value_lists);
1344        debug_assert!(num <= u16::MAX as usize, "Too many parameters on block");
1345        let ty = self.value_type(param);
1346        self.values[param] = ValueData::Param {
1347            ty,
1348            num: num as u16,
1349            block,
1350        }
1351        .into();
1352    }
1353
1354    /// Replace a block parameter with a new value of type `ty`.
1355    ///
1356    /// The `old_value` must be an attached block parameter. It is removed from its place in the list
1357    /// of parameters and replaced by a new value of type `new_type`. The new value gets the same
1358    /// position in the list, and other parameters are not disturbed.
1359    ///
1360    /// The old value is left detached, so it should probably be changed into something else.
1361    ///
1362    /// Returns the new value.
1363    pub fn replace_block_param(&mut self, old_value: Value, new_type: Type) -> Value {
1364        // Create new value identical to the old one except for the type.
1365        let (block, num) =
1366            if let ValueData::Param { num, block, .. } = ValueData::from(self.values[old_value]) {
1367                (block, num)
1368            } else {
1369                panic!("{old_value} must be a block parameter");
1370            };
1371        let new_arg = self.make_value(ValueData::Param {
1372            ty: new_type,
1373            num,
1374            block,
1375        });
1376
1377        self.blocks[block]
1378            .params
1379            .as_mut_slice(&mut self.value_lists)[num as usize] = new_arg;
1380        new_arg
1381    }
1382
1383    /// Detach all the parameters from `block` and return them as a `ValueList`.
1384    ///
1385    /// This is a quite low-level operation. Sensible things to do with the detached block parameters
1386    /// is to put them back on the same block with `attach_block_param()` or change them into aliases
1387    /// with `change_to_alias()`.
1388    pub fn detach_block_params(&mut self, block: Block) -> ValueList {
1389        self.blocks[block].params.take()
1390    }
1391
1392    /// Detach all of an instruction's result values.
1393    ///
1394    /// This is a quite low-level operation. A sensible thing to do with the
1395    /// detached results is to change them into aliases with
1396    /// `change_to_alias()`.
1397    pub fn detach_inst_results(&mut self, inst: Inst) {
1398        self.results[inst].clear(&mut self.value_lists);
1399    }
1400}
1401
1402/// Contents of a basic block.
1403///
1404/// Parameters on a basic block are values that dominate everything in the block. All
1405/// branches to this block must provide matching arguments, and the arguments to the entry block must
1406/// match the function arguments.
1407#[derive(Clone, PartialEq, Hash)]
1408#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
1409pub struct BlockData {
1410    /// List of parameters to this block.
1411    params: ValueList,
1412}
1413
1414impl BlockData {
1415    fn new() -> Self {
1416        Self {
1417            params: ValueList::new(),
1418        }
1419    }
1420
1421    /// Get the parameters on `block`.
1422    pub fn params<'a>(&self, pool: &'a ValueListPool) -> &'a [Value] {
1423        self.params.as_slice(pool)
1424    }
1425}
1426
1427/// Object that can display an instruction.
1428pub struct DisplayInst<'a>(&'a DataFlowGraph, Inst);
1429
1430impl<'a> fmt::Display for DisplayInst<'a> {
1431    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1432        let dfg = self.0;
1433        let inst = self.1;
1434
1435        if let Some((first, rest)) = dfg.inst_results(inst).split_first() {
1436            write!(f, "{first}")?;
1437            for v in rest {
1438                write!(f, ", {v}")?;
1439            }
1440            write!(f, " = ")?;
1441        }
1442
1443        let typevar = dfg.ctrl_typevar(inst);
1444        if typevar.is_invalid() {
1445            write!(f, "{}", dfg.insts[inst].opcode())?;
1446        } else {
1447            write!(f, "{}.{}", dfg.insts[inst].opcode(), typevar)?;
1448        }
1449        write_operands(f, dfg, inst)
1450    }
1451}
1452
1453/// Parser routines. These routines should not be used outside the parser.
1454impl DataFlowGraph {
1455    /// Set the type of a value. This is only for use in the parser, which needs
1456    /// to create invalid values for index padding which may be reassigned later.
1457    #[cold]
1458    fn set_value_type_for_parser(&mut self, v: Value, t: Type) {
1459        assert_eq!(
1460            self.value_type(v),
1461            types::INVALID,
1462            "this function is only for assigning types to previously invalid values"
1463        );
1464        self.values[v].set_type(t);
1465    }
1466
1467    /// Check that the given concrete `Type` has been defined in the function.
1468    pub fn check_dynamic_type(&mut self, ty: Type) -> Option<Type> {
1469        debug_assert!(ty.is_dynamic_vector());
1470        if self
1471            .dynamic_types
1472            .values()
1473            .any(|dyn_ty_data| dyn_ty_data.concrete().unwrap() == ty)
1474        {
1475            Some(ty)
1476        } else {
1477            None
1478        }
1479    }
1480
1481    /// Create result values for `inst`, reusing the provided detached values.
1482    /// This is similar to `make_inst_results_reusing` except it's only for use
1483    /// in the parser, which needs to reuse previously invalid values.
1484    #[cold]
1485    pub fn make_inst_results_for_parser(
1486        &mut self,
1487        inst: Inst,
1488        ctrl_typevar: Type,
1489        reuse: &[Value],
1490    ) -> usize {
1491        let mut reuse_iter = reuse.iter().copied();
1492        let result_tys: SmallVec<[_; 16]> = self.inst_result_types(inst, ctrl_typevar).collect();
1493        for ty in result_tys {
1494            if ty.is_dynamic_vector() {
1495                self.check_dynamic_type(ty)
1496                    .unwrap_or_else(|| panic!("Use of undeclared dynamic type: {ty}"));
1497            }
1498            if let Some(v) = reuse_iter.next() {
1499                self.set_value_type_for_parser(v, ty);
1500            }
1501        }
1502
1503        self.make_inst_results_reusing(inst, ctrl_typevar, reuse.iter().map(|x| Some(*x)))
1504    }
1505
1506    /// Similar to `append_block_param`, append a parameter with type `ty` to
1507    /// `block`, but using value `val`. This is only for use by the parser to
1508    /// create parameters with specific values.
1509    #[cold]
1510    pub fn append_block_param_for_parser(&mut self, block: Block, ty: Type, val: Value) {
1511        let num = self.blocks[block].params.push(val, &mut self.value_lists);
1512        assert!(num <= u16::MAX as usize, "Too many parameters on block");
1513        self.values[val] = ValueData::Param {
1514            ty,
1515            num: num as u16,
1516            block,
1517        }
1518        .into();
1519    }
1520
1521    /// Create a new value alias. This is only for use by the parser to create
1522    /// aliases with specific values, and the printer for testing.
1523    #[cold]
1524    pub fn make_value_alias_for_serialization(&mut self, src: Value, dest: Value) {
1525        assert_ne!(src, Value::reserved_value());
1526        assert_ne!(dest, Value::reserved_value());
1527
1528        let ty = if self.values.is_valid(src) {
1529            self.value_type(src)
1530        } else {
1531            // As a special case, if we can't resolve the aliasee yet, use INVALID
1532            // temporarily. It will be resolved later in parsing.
1533            types::INVALID
1534        };
1535        let data = ValueData::Alias { ty, original: src };
1536        self.values[dest] = data.into();
1537    }
1538
1539    /// If `v` is already defined as an alias, return its destination value.
1540    /// Otherwise return None. This allows the parser to coalesce identical
1541    /// alias definitions, and the printer to identify an alias's immediate target.
1542    #[cold]
1543    pub fn value_alias_dest_for_serialization(&self, v: Value) -> Option<Value> {
1544        if let ValueData::Alias { original, .. } = ValueData::from(self.values[v]) {
1545            Some(original)
1546        } else {
1547            None
1548        }
1549    }
1550
1551    /// Compute the type of an alias. This is only for use in the parser.
1552    /// Returns false if an alias cycle was encountered.
1553    #[cold]
1554    pub fn set_alias_type_for_parser(&mut self, v: Value) -> bool {
1555        if let Some(resolved) = maybe_resolve_aliases(&self.values, v) {
1556            let old_ty = self.value_type(v);
1557            let new_ty = self.value_type(resolved);
1558            if old_ty == types::INVALID {
1559                self.set_value_type_for_parser(v, new_ty);
1560            } else {
1561                assert_eq!(old_ty, new_ty);
1562            }
1563            true
1564        } else {
1565            false
1566        }
1567    }
1568
1569    /// Create an invalid value, to pad the index space. This is only for use by
1570    /// the parser to pad out the value index space.
1571    #[cold]
1572    pub fn make_invalid_value_for_parser(&mut self) {
1573        let data = ValueData::Alias {
1574            ty: types::INVALID,
1575            original: Value::reserved_value(),
1576        };
1577        self.make_value(data);
1578    }
1579
1580    /// Check if a value reference is valid, while being aware of aliases which
1581    /// may be unresolved while parsing.
1582    #[cold]
1583    pub fn value_is_valid_for_parser(&self, v: Value) -> bool {
1584        if !self.value_is_valid(v) {
1585            return false;
1586        }
1587        if let ValueData::Alias { ty, .. } = ValueData::from(self.values[v]) {
1588            ty != types::INVALID
1589        } else {
1590            true
1591        }
1592    }
1593}
1594
1595#[cfg(test)]
1596mod tests {
1597    use super::*;
1598    use crate::cursor::{Cursor, FuncCursor};
1599    use crate::ir::{Function, Opcode, TrapCode};
1600    use alloc::string::ToString;
1601
1602    #[test]
1603    fn make_inst() {
1604        let mut dfg = DataFlowGraph::new();
1605
1606        let idata = InstructionData::UnaryImm {
1607            opcode: Opcode::Iconst,
1608            imm: 0.into(),
1609        };
1610        let inst = dfg.make_inst(idata);
1611
1612        dfg.make_inst_results(inst, types::I32);
1613        assert_eq!(inst.to_string(), "inst0");
1614        assert_eq!(dfg.display_inst(inst).to_string(), "v0 = iconst.i32 0");
1615
1616        // Immutable reference resolution.
1617        {
1618            let immdfg = &dfg;
1619            let ins = &immdfg.insts[inst];
1620            assert_eq!(ins.opcode(), Opcode::Iconst);
1621        }
1622
1623        // Results.
1624        let val = dfg.first_result(inst);
1625        assert_eq!(dfg.inst_results(inst), &[val]);
1626
1627        assert_eq!(dfg.value_def(val), ValueDef::Result(inst, 0));
1628        assert_eq!(dfg.value_type(val), types::I32);
1629
1630        // Replacing results.
1631        assert!(dfg.value_is_attached(val));
1632        let v2 = dfg.replace_result(val, types::F64);
1633        assert!(!dfg.value_is_attached(val));
1634        assert!(dfg.value_is_attached(v2));
1635        assert_eq!(dfg.inst_results(inst), &[v2]);
1636        assert_eq!(dfg.value_def(v2), ValueDef::Result(inst, 0));
1637        assert_eq!(dfg.value_type(v2), types::F64);
1638    }
1639
1640    #[test]
1641    fn no_results() {
1642        let mut dfg = DataFlowGraph::new();
1643
1644        let idata = InstructionData::Trap {
1645            opcode: Opcode::Trap,
1646            code: TrapCode::unwrap_user(1),
1647        };
1648        let inst = dfg.make_inst(idata);
1649        assert_eq!(dfg.display_inst(inst).to_string(), "trap user1");
1650
1651        // Result slice should be empty.
1652        assert_eq!(dfg.inst_results(inst), &[]);
1653    }
1654
1655    #[test]
1656    fn block() {
1657        let mut dfg = DataFlowGraph::new();
1658
1659        let block = dfg.make_block();
1660        assert_eq!(block.to_string(), "block0");
1661        assert_eq!(dfg.num_block_params(block), 0);
1662        assert_eq!(dfg.block_params(block), &[]);
1663        assert!(dfg.detach_block_params(block).is_empty());
1664        assert_eq!(dfg.num_block_params(block), 0);
1665        assert_eq!(dfg.block_params(block), &[]);
1666
1667        let arg1 = dfg.append_block_param(block, types::F32);
1668        assert_eq!(arg1.to_string(), "v0");
1669        assert_eq!(dfg.num_block_params(block), 1);
1670        assert_eq!(dfg.block_params(block), &[arg1]);
1671
1672        let arg2 = dfg.append_block_param(block, types::I16);
1673        assert_eq!(arg2.to_string(), "v1");
1674        assert_eq!(dfg.num_block_params(block), 2);
1675        assert_eq!(dfg.block_params(block), &[arg1, arg2]);
1676
1677        assert_eq!(dfg.value_def(arg1), ValueDef::Param(block, 0));
1678        assert_eq!(dfg.value_def(arg2), ValueDef::Param(block, 1));
1679        assert_eq!(dfg.value_type(arg1), types::F32);
1680        assert_eq!(dfg.value_type(arg2), types::I16);
1681
1682        // Swap the two block parameters.
1683        let vlist = dfg.detach_block_params(block);
1684        assert_eq!(dfg.num_block_params(block), 0);
1685        assert_eq!(dfg.block_params(block), &[]);
1686        assert_eq!(vlist.as_slice(&dfg.value_lists), &[arg1, arg2]);
1687        dfg.attach_block_param(block, arg2);
1688        let arg3 = dfg.append_block_param(block, types::I32);
1689        dfg.attach_block_param(block, arg1);
1690        assert_eq!(dfg.block_params(block), &[arg2, arg3, arg1]);
1691    }
1692
1693    #[test]
1694    fn replace_block_params() {
1695        let mut dfg = DataFlowGraph::new();
1696
1697        let block = dfg.make_block();
1698        let arg1 = dfg.append_block_param(block, types::F32);
1699
1700        let new1 = dfg.replace_block_param(arg1, types::I64);
1701        assert_eq!(dfg.value_type(arg1), types::F32);
1702        assert_eq!(dfg.value_type(new1), types::I64);
1703        assert_eq!(dfg.block_params(block), &[new1]);
1704
1705        dfg.attach_block_param(block, arg1);
1706        assert_eq!(dfg.block_params(block), &[new1, arg1]);
1707
1708        let new2 = dfg.replace_block_param(arg1, types::I8);
1709        assert_eq!(dfg.value_type(arg1), types::F32);
1710        assert_eq!(dfg.value_type(new2), types::I8);
1711        assert_eq!(dfg.block_params(block), &[new1, new2]);
1712
1713        dfg.attach_block_param(block, arg1);
1714        assert_eq!(dfg.block_params(block), &[new1, new2, arg1]);
1715
1716        let new3 = dfg.replace_block_param(new2, types::I16);
1717        assert_eq!(dfg.value_type(new1), types::I64);
1718        assert_eq!(dfg.value_type(new2), types::I8);
1719        assert_eq!(dfg.value_type(new3), types::I16);
1720        assert_eq!(dfg.block_params(block), &[new1, new3, arg1]);
1721    }
1722
1723    #[test]
1724    fn swap_remove_block_params() {
1725        let mut dfg = DataFlowGraph::new();
1726
1727        let block = dfg.make_block();
1728        let arg1 = dfg.append_block_param(block, types::F32);
1729        let arg2 = dfg.append_block_param(block, types::F32);
1730        let arg3 = dfg.append_block_param(block, types::F32);
1731        assert_eq!(dfg.block_params(block), &[arg1, arg2, arg3]);
1732
1733        dfg.swap_remove_block_param(arg1);
1734        assert_eq!(dfg.value_is_attached(arg1), false);
1735        assert_eq!(dfg.value_is_attached(arg2), true);
1736        assert_eq!(dfg.value_is_attached(arg3), true);
1737        assert_eq!(dfg.block_params(block), &[arg3, arg2]);
1738        dfg.swap_remove_block_param(arg2);
1739        assert_eq!(dfg.value_is_attached(arg2), false);
1740        assert_eq!(dfg.value_is_attached(arg3), true);
1741        assert_eq!(dfg.block_params(block), &[arg3]);
1742        dfg.swap_remove_block_param(arg3);
1743        assert_eq!(dfg.value_is_attached(arg3), false);
1744        assert_eq!(dfg.block_params(block), &[]);
1745    }
1746
1747    #[test]
1748    fn aliases() {
1749        use crate::ir::InstBuilder;
1750        use crate::ir::condcodes::IntCC;
1751
1752        let mut func = Function::new();
1753        let block0 = func.dfg.make_block();
1754        let mut pos = FuncCursor::new(&mut func);
1755        pos.insert_block(block0);
1756
1757        // Build a little test program.
1758        let v1 = pos.ins().iconst(types::I32, 42);
1759
1760        // Make sure we can resolve value aliases even when values is empty.
1761        assert_eq!(pos.func.dfg.resolve_aliases(v1), v1);
1762
1763        let arg0 = pos.func.dfg.append_block_param(block0, types::I32);
1764        let (s, c) = pos.ins().uadd_overflow(v1, arg0);
1765        let iadd = match pos.func.dfg.value_def(s) {
1766            ValueDef::Result(i, 0) => i,
1767            _ => panic!(),
1768        };
1769
1770        // Remove `c` from the result list.
1771        pos.func.stencil.dfg.results[iadd].remove(1, &mut pos.func.stencil.dfg.value_lists);
1772
1773        // Replace `uadd_overflow` with a normal `iadd` and an `icmp`.
1774        pos.func.dfg.replace(iadd).iadd(v1, arg0);
1775        let c2 = pos.ins().icmp(IntCC::Equal, s, v1);
1776        pos.func.dfg.change_to_alias(c, c2);
1777
1778        assert_eq!(pos.func.dfg.resolve_aliases(c2), c2);
1779        assert_eq!(pos.func.dfg.resolve_aliases(c), c2);
1780    }
1781
1782    #[test]
1783    fn cloning() {
1784        use crate::ir::InstBuilder;
1785
1786        let mut func = Function::new();
1787        let mut sig = Signature::new(crate::isa::CallConv::SystemV);
1788        sig.params.push(ir::AbiParam::new(types::I32));
1789        let sig = func.import_signature(sig);
1790        let block0 = func.dfg.make_block();
1791        let mut pos = FuncCursor::new(&mut func);
1792        pos.insert_block(block0);
1793        let v1 = pos.ins().iconst(types::I32, 0);
1794        let v2 = pos.ins().iconst(types::I32, 1);
1795        let call_inst = pos.ins().call_indirect(sig, v1, &[v1]);
1796        let func = pos.func;
1797
1798        let call_inst_dup = func.dfg.clone_inst(call_inst);
1799        func.dfg.inst_args_mut(call_inst)[0] = v2;
1800        assert_eq!(v1, func.dfg.inst_args(call_inst_dup)[0]);
1801    }
1802}