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