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