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