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     types, Block, BlockCall, ConstantData, ConstantPool, DynamicType, ExtFuncData, FuncRef,
12     Immediate, Inst, JumpTables, RelSourceLoc, SigRef, Signature, Type, Value,
13     ValueLabelAssignments, ValueList, ValueListPool,
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     /// Returns `true` if the given block reference is valid.
69     pub fn is_valid(&self, block: Block) -> bool {
70         self.0.is_valid(block)
71     }
72 }
73 
74 impl Index<Block> for Blocks {
75     type Output = BlockData;
76 
77     fn index(&self, block: Block) -> &BlockData {
78         &self.0[block]
79     }
80 }
81 
82 impl IndexMut<Block> for Blocks {
83     fn index_mut(&mut self, block: Block) -> &mut BlockData {
84         &mut self.0[block]
85     }
86 }
87 
88 /// A data flow graph defines all instructions and basic blocks in a function as well as
89 /// the data flow dependencies between them. The DFG also tracks values which can be either
90 /// instruction results or block parameters.
91 ///
92 /// The layout of blocks in the function and of instructions in each block is recorded by the
93 /// `Layout` data structure which forms the other half of the function representation.
94 ///
95 #[derive(Clone, PartialEq, Hash)]
96 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
97 pub struct DataFlowGraph {
98     /// Data about all of the instructions in the function, including opcodes and operands.
99     /// The instructions in this map are not in program order. That is tracked by `Layout`, along
100     /// with the block containing each instruction.
101     pub insts: Insts,
102 
103     /// List of result values for each instruction.
104     ///
105     /// This map gets resized automatically by `make_inst()` so it is always in sync with the
106     /// primary `insts` map.
107     results: SecondaryMap<Inst, ValueList>,
108 
109     /// User-defined stack maps.
110     ///
111     /// Not to be confused with the stack maps that `regalloc2` produces. These
112     /// are defined by the user in `cranelift-frontend`. These will eventually
113     /// replace the stack maps support in `regalloc2`, but in the name of
114     /// incrementalism and avoiding gigantic PRs that completely overhaul
115     /// Cranelift and Wasmtime at the same time, we are allowing them to live in
116     /// parallel for the time being.
117     user_stack_maps: alloc::collections::BTreeMap<Inst, UserStackMapEntryVec>,
118 
119     /// basic blocks in the function and their parameters.
120     ///
121     /// This map is not in program order. That is handled by `Layout`, and so is the sequence of
122     /// instructions contained in each block.
123     pub blocks: Blocks,
124 
125     /// Dynamic types created.
126     pub dynamic_types: DynamicTypes,
127 
128     /// Memory pool of value lists.
129     ///
130     /// The `ValueList` references into this pool appear in many places:
131     ///
132     /// - Instructions in `insts` that don't have room for their entire argument list inline.
133     /// - Instruction result values in `results`.
134     /// - block parameters in `blocks`.
135     pub value_lists: ValueListPool,
136 
137     /// Primary value table with entries for all values.
138     values: PrimaryMap<Value, ValueDataPacked>,
139 
140     /// Facts: proof-carrying-code assertions about values.
141     pub facts: SecondaryMap<Value, Option<Fact>>,
142 
143     /// Function signature table. These signatures are referenced by indirect call instructions as
144     /// well as the external function references.
145     pub signatures: PrimaryMap<SigRef, Signature>,
146 
147     /// External function references. These are functions that can be called directly.
148     pub ext_funcs: PrimaryMap<FuncRef, ExtFuncData>,
149 
150     /// Saves Value labels.
151     pub values_labels: Option<BTreeMap<Value, ValueLabelAssignments>>,
152 
153     /// Constants used within the function.
154     pub constants: ConstantPool,
155 
156     /// Stores large immediates that otherwise will not fit on InstructionData.
157     pub immediates: PrimaryMap<Immediate, ConstantData>,
158 
159     /// Jump tables used in this function.
160     pub jump_tables: JumpTables,
161 }
162 
163 impl DataFlowGraph {
164     /// Create a new empty `DataFlowGraph`.
165     pub fn new() -> Self {
166         Self {
167             insts: Insts(PrimaryMap::new()),
168             results: SecondaryMap::new(),
169             user_stack_maps: alloc::collections::BTreeMap::new(),
170             blocks: Blocks(PrimaryMap::new()),
171             dynamic_types: DynamicTypes::new(),
172             value_lists: ValueListPool::new(),
173             values: PrimaryMap::new(),
174             facts: SecondaryMap::new(),
175             signatures: PrimaryMap::new(),
176             ext_funcs: PrimaryMap::new(),
177             values_labels: None,
178             constants: ConstantPool::new(),
179             immediates: PrimaryMap::new(),
180             jump_tables: JumpTables::new(),
181         }
182     }
183 
184     /// Clear everything.
185     pub fn clear(&mut self) {
186         self.insts.0.clear();
187         self.results.clear();
188         self.user_stack_maps.clear();
189         self.blocks.0.clear();
190         self.dynamic_types.clear();
191         self.value_lists.clear();
192         self.values.clear();
193         self.signatures.clear();
194         self.ext_funcs.clear();
195         self.values_labels = None;
196         self.constants.clear();
197         self.immediates.clear();
198         self.jump_tables.clear();
199         self.facts.clear();
200     }
201 
202     /// Get the total number of instructions created in this function, whether they are currently
203     /// inserted in the layout or not.
204     ///
205     /// This is intended for use with `SecondaryMap::with_capacity`.
206     pub fn num_insts(&self) -> usize {
207         self.insts.0.len()
208     }
209 
210     /// Returns `true` if the given instruction reference is valid.
211     pub fn inst_is_valid(&self, inst: Inst) -> bool {
212         self.insts.0.is_valid(inst)
213     }
214 
215     /// Get the total number of basic blocks created in this function, whether they are
216     /// currently inserted in the layout or not.
217     ///
218     /// This is intended for use with `SecondaryMap::with_capacity`.
219     pub fn num_blocks(&self) -> usize {
220         self.blocks.len()
221     }
222 
223     /// Returns `true` if the given block reference is valid.
224     pub fn block_is_valid(&self, block: Block) -> bool {
225         self.blocks.is_valid(block)
226     }
227 
228     /// Make a BlockCall, bundling together the block and its arguments.
229     pub fn block_call(&mut self, block: Block, args: &[Value]) -> BlockCall {
230         BlockCall::new(block, args, &mut self.value_lists)
231     }
232 
233     /// Get the total number of values.
234     pub fn num_values(&self) -> usize {
235         self.values.len()
236     }
237 
238     /// Get an iterator over all values and their definitions.
239     pub fn values_and_defs(&self) -> impl Iterator<Item = (Value, ValueDef)> + '_ {
240         self.values().map(|value| (value, self.value_def(value)))
241     }
242 
243     /// Starts collection of debug information.
244     pub fn collect_debug_info(&mut self) {
245         if self.values_labels.is_none() {
246             self.values_labels = Some(Default::default());
247         }
248     }
249 
250     /// Inserts a `ValueLabelAssignments::Alias` for `to_alias` if debug info
251     /// collection is enabled.
252     pub fn add_value_label_alias(&mut self, to_alias: Value, from: RelSourceLoc, value: Value) {
253         if let Some(values_labels) = self.values_labels.as_mut() {
254             values_labels.insert(to_alias, ir::ValueLabelAssignments::Alias { from, value });
255         }
256     }
257 }
258 
259 /// Resolve value aliases.
260 ///
261 /// Find the original SSA value that `value` aliases, or None if an
262 /// alias cycle is detected.
263 fn maybe_resolve_aliases(
264     values: &PrimaryMap<Value, ValueDataPacked>,
265     value: Value,
266 ) -> Option<Value> {
267     let mut v = value;
268 
269     // Note that values may be empty here.
270     for _ in 0..=values.len() {
271         if let ValueData::Alias { original, .. } = ValueData::from(values[v]) {
272             v = original;
273         } else {
274             return Some(v);
275         }
276     }
277 
278     None
279 }
280 
281 /// Resolve value aliases.
282 ///
283 /// Find the original SSA value that `value` aliases.
284 fn resolve_aliases(values: &PrimaryMap<Value, ValueDataPacked>, value: Value) -> Value {
285     if let Some(v) = maybe_resolve_aliases(values, value) {
286         v
287     } else {
288         panic!("Value alias loop detected for {}", value);
289     }
290 }
291 
292 /// Iterator over all Values in a DFG.
293 pub struct Values<'a> {
294     inner: entity::Iter<'a, Value, ValueDataPacked>,
295 }
296 
297 /// Check for non-values.
298 fn valid_valuedata(data: ValueDataPacked) -> bool {
299     let data = ValueData::from(data);
300     if let ValueData::Alias {
301         ty: types::INVALID,
302         original,
303     } = ValueData::from(data)
304     {
305         if original == Value::reserved_value() {
306             return false;
307         }
308     }
309     true
310 }
311 
312 impl<'a> Iterator for Values<'a> {
313     type Item = Value;
314 
315     fn next(&mut self) -> Option<Self::Item> {
316         self.inner
317             .by_ref()
318             .find(|kv| valid_valuedata(*kv.1))
319             .map(|kv| kv.0)
320     }
321 }
322 
323 /// Handling values.
324 ///
325 /// Values are either block parameters or instruction results.
326 impl DataFlowGraph {
327     /// Allocate an extended value entry.
328     fn make_value(&mut self, data: ValueData) -> Value {
329         self.values.push(data.into())
330     }
331 
332     /// Get an iterator over all values.
333     pub fn values<'a>(&'a self) -> Values {
334         Values {
335             inner: self.values.iter(),
336         }
337     }
338 
339     /// Check if a value reference is valid.
340     pub fn value_is_valid(&self, v: Value) -> bool {
341         self.values.is_valid(v)
342     }
343 
344     /// Check whether a value is valid and not an alias.
345     pub fn value_is_real(&self, value: Value) -> bool {
346         // Deleted or unused values are also stored as aliases so this excludes
347         // those as well.
348         self.value_is_valid(value) && !matches!(self.values[value].into(), ValueData::Alias { .. })
349     }
350 
351     /// Get the type of a value.
352     pub fn value_type(&self, v: Value) -> Type {
353         self.values[v].ty()
354     }
355 
356     /// Get the definition of a value.
357     ///
358     /// This is either the instruction that defined it or the Block that has the value as an
359     /// parameter.
360     pub fn value_def(&self, v: Value) -> ValueDef {
361         match ValueData::from(self.values[v]) {
362             ValueData::Inst { inst, num, .. } => ValueDef::Result(inst, num as usize),
363             ValueData::Param { block, num, .. } => ValueDef::Param(block, num as usize),
364             ValueData::Alias { original, .. } => {
365                 // Make sure we only recurse one level. `resolve_aliases` has safeguards to
366                 // detect alias loops without overrunning the stack.
367                 self.value_def(self.resolve_aliases(original))
368             }
369             ValueData::Union { x, y, .. } => ValueDef::Union(x, y),
370         }
371     }
372 
373     /// Determine if `v` is an attached instruction result / block parameter.
374     ///
375     /// An attached value can't be attached to something else without first being detached.
376     ///
377     /// Value aliases are not considered to be attached to anything. Use `resolve_aliases()` to
378     /// determine if the original aliased value is attached.
379     pub fn value_is_attached(&self, v: Value) -> bool {
380         use self::ValueData::*;
381         match ValueData::from(self.values[v]) {
382             Inst { inst, num, .. } => Some(&v) == self.inst_results(inst).get(num as usize),
383             Param { block, num, .. } => Some(&v) == self.block_params(block).get(num as usize),
384             Alias { .. } => false,
385             Union { .. } => false,
386         }
387     }
388 
389     /// Resolve value aliases.
390     ///
391     /// Find the original SSA value that `value` aliases.
392     pub fn resolve_aliases(&self, value: Value) -> Value {
393         resolve_aliases(&self.values, value)
394     }
395 
396     /// Replace all uses of value aliases with their resolved values, and delete
397     /// the aliases.
398     pub fn resolve_all_aliases(&mut self) {
399         let invalid_value = ValueDataPacked::from(ValueData::Alias {
400             ty: types::INVALID,
401             original: Value::reserved_value(),
402         });
403 
404         // Rewrite each chain of aliases. Update every alias along the chain
405         // into an alias directly to the final value. Due to updating every
406         // alias that it looks at, this loop runs in time linear in the number
407         // of values.
408         for mut src in self.values.keys() {
409             let value_data = self.values[src];
410             if value_data == invalid_value {
411                 continue;
412             }
413             if let ValueData::Alias { mut original, .. } = value_data.into() {
414                 // We don't use the type after this, we just need some place to
415                 // store the resolved aliases temporarily.
416                 let resolved = ValueDataPacked::from(ValueData::Alias {
417                     ty: types::INVALID,
418                     original: resolve_aliases(&self.values, original),
419                 });
420                 // Walk the chain again, splatting the new alias everywhere.
421                 // resolve_aliases panics if there's an alias cycle, so we don't
422                 // need to guard against cycles here.
423                 loop {
424                     self.values[src] = resolved;
425                     src = original;
426                     if let ValueData::Alias { original: next, .. } = self.values[src].into() {
427                         original = next;
428                     } else {
429                         break;
430                     }
431                 }
432             }
433         }
434 
435         // Now aliases don't point to other aliases, so we can replace any use
436         // of an alias with the final value in constant time.
437 
438         // Rewrite InstructionData in `self.insts`.
439         for inst in self.insts.0.values_mut() {
440             inst.map_values(&mut self.value_lists, &mut self.jump_tables, |arg| {
441                 if let ValueData::Alias { original, .. } = self.values[arg].into() {
442                     original
443                 } else {
444                     arg
445                 }
446             });
447         }
448 
449         // - `results` and block-params in `blocks` are not aliases, by
450         //   definition.
451         // - `dynamic_types` has no values.
452         // - `value_lists` can only be accessed via references from elsewhere.
453         // - `values` only has value references in aliases (which we've
454         //   removed), and unions (but the egraph pass ensures there are no
455         //   aliases before creating unions).
456 
457         // Merge `facts` from any alias onto the aliased value. Note that if
458         // there was a chain of aliases, at this point every alias that was in
459         // the chain points to the same final value, so their facts will all be
460         // merged together.
461         for value in self.facts.keys() {
462             if let ValueData::Alias { original, .. } = self.values[value].into() {
463                 if let Some(new_fact) = self.facts[value].take() {
464                     match &mut self.facts[original] {
465                         Some(old_fact) => *old_fact = Fact::intersect(old_fact, &new_fact),
466                         old_fact => *old_fact = Some(new_fact),
467                     }
468                 }
469             }
470         }
471 
472         // - `signatures` and `ext_funcs` have no values.
473 
474         if let Some(values_labels) = &mut self.values_labels {
475             // Debug info is best-effort. If any is attached to value aliases,
476             // just discard it.
477             values_labels.retain(|&k, _| !matches!(self.values[k].into(), ValueData::Alias { .. }));
478 
479             // If debug-info says a value should have the same labels as another
480             // value, then make sure that target is not a value alias.
481             for value_label in values_labels.values_mut() {
482                 if let ValueLabelAssignments::Alias { value, .. } = value_label {
483                     if let ValueData::Alias { original, .. } = self.values[*value].into() {
484                         *value = original;
485                     }
486                 }
487             }
488         }
489 
490         // - `constants` and `immediates` have no values.
491         // - `jump_tables` is updated together with instruction-data above.
492 
493         // Delete all aliases now that there are no uses left.
494         for value in self.values.values_mut() {
495             if let ValueData::Alias { .. } = ValueData::from(*value) {
496                 *value = invalid_value;
497             }
498         }
499     }
500 
501     /// Turn a value into an alias of another.
502     ///
503     /// Change the `dest` value to behave as an alias of `src`. This means that all uses of `dest`
504     /// will behave as if they used that value `src`.
505     ///
506     /// The `dest` value can't be attached to an instruction or block.
507     pub fn change_to_alias(&mut self, dest: Value, src: Value) {
508         debug_assert!(!self.value_is_attached(dest));
509         // Try to create short alias chains by finding the original source value.
510         // This also avoids the creation of loops.
511         let original = self.resolve_aliases(src);
512         debug_assert_ne!(
513             dest, original,
514             "Aliasing {} to {} would create a loop",
515             dest, src
516         );
517         let ty = self.value_type(original);
518         debug_assert_eq!(
519             self.value_type(dest),
520             ty,
521             "Aliasing {} to {} would change its type {} to {}",
522             dest,
523             src,
524             self.value_type(dest),
525             ty
526         );
527         debug_assert_ne!(ty, types::INVALID);
528 
529         self.values[dest] = ValueData::Alias { ty, original }.into();
530     }
531 
532     /// Replace the results of one instruction with aliases to the results of another.
533     ///
534     /// Change all the results of `dest_inst` to behave as aliases of
535     /// corresponding results of `src_inst`, as if calling change_to_alias for
536     /// each.
537     ///
538     /// After calling this instruction, `dest_inst` will have had its results
539     /// cleared, so it likely needs to be removed from the graph.
540     ///
541     pub fn replace_with_aliases(&mut self, dest_inst: Inst, original_inst: Inst) {
542         debug_assert_ne!(
543             dest_inst, original_inst,
544             "Replacing {} with itself would create a loop",
545             dest_inst
546         );
547 
548         let dest_results = self.results[dest_inst].as_slice(&self.value_lists);
549         let original_results = self.results[original_inst].as_slice(&self.value_lists);
550 
551         debug_assert_eq!(
552             dest_results.len(),
553             original_results.len(),
554             "Replacing {} with {} would produce a different number of results.",
555             dest_inst,
556             original_inst
557         );
558 
559         for (&dest, &original) in dest_results.iter().zip(original_results) {
560             let ty = self.value_type(original);
561             debug_assert_eq!(
562                 self.value_type(dest),
563                 ty,
564                 "Aliasing {} to {} would change its type {} to {}",
565                 dest,
566                 original,
567                 self.value_type(dest),
568                 ty
569             );
570             debug_assert_ne!(ty, types::INVALID);
571 
572             self.values[dest] = ValueData::Alias { ty, original }.into();
573         }
574 
575         self.clear_results(dest_inst);
576     }
577 
578     /// Get the stack map entries associated with the given instruction.
579     pub fn user_stack_map_entries(&self, inst: Inst) -> Option<&[UserStackMapEntry]> {
580         self.user_stack_maps.get(&inst).map(|es| &**es)
581     }
582 
583     /// Append a new stack map entry for the given call instruction.
584     ///
585     /// # Panics
586     ///
587     /// Panics if the given instruction is not a (non-tail) call instruction.
588     pub fn append_user_stack_map_entry(&mut self, inst: Inst, entry: UserStackMapEntry) {
589         let opcode = self.insts[inst].opcode();
590         assert!(opcode.is_call() && !opcode.is_return());
591         self.user_stack_maps.entry(inst).or_default().push(entry);
592     }
593 }
594 
595 /// Where did a value come from?
596 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
597 pub enum ValueDef {
598     /// Value is the n'th result of an instruction.
599     Result(Inst, usize),
600     /// Value is the n'th parameter to a block.
601     Param(Block, usize),
602     /// Value is a union of two other values.
603     Union(Value, Value),
604 }
605 
606 impl ValueDef {
607     /// Unwrap the instruction where the value was defined, or panic.
608     pub fn unwrap_inst(&self) -> Inst {
609         self.inst().expect("Value is not an instruction result")
610     }
611 
612     /// Get the instruction where the value was defined, if any.
613     pub fn inst(&self) -> Option<Inst> {
614         match *self {
615             Self::Result(inst, _) => Some(inst),
616             _ => None,
617         }
618     }
619 
620     /// Unwrap the block there the parameter is defined, or panic.
621     pub fn unwrap_block(&self) -> Block {
622         match *self {
623             Self::Param(block, _) => block,
624             _ => panic!("Value is not a block parameter"),
625         }
626     }
627 
628     /// Get the number component of this definition.
629     ///
630     /// When multiple values are defined at the same program point, this indicates the index of
631     /// this value.
632     pub fn num(self) -> usize {
633         match self {
634             Self::Result(_, n) | Self::Param(_, n) => n,
635             Self::Union(_, _) => 0,
636         }
637     }
638 }
639 
640 /// Internal table storage for extended values.
641 #[derive(Clone, Debug, PartialEq, Hash)]
642 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
643 enum ValueData {
644     /// Value is defined by an instruction.
645     Inst { ty: Type, num: u16, inst: Inst },
646 
647     /// Value is a block parameter.
648     Param { ty: Type, num: u16, block: Block },
649 
650     /// Value is an alias of another value.
651     /// An alias value can't be linked as an instruction result or block parameter. It is used as a
652     /// placeholder when the original instruction or block has been rewritten or modified.
653     Alias { ty: Type, original: Value },
654 
655     /// Union is a "fork" in representation: the value can be
656     /// represented as either of the values named here. This is used
657     /// for aegraph (acyclic egraph) representation in the DFG.
658     Union { ty: Type, x: Value, y: Value },
659 }
660 
661 /// Bit-packed version of ValueData, for efficiency.
662 ///
663 /// Layout:
664 ///
665 /// ```plain
666 ///        | tag:2 |  type:14        |    x:24       | y:24          |
667 ///
668 /// Inst       00     ty               inst output     inst index
669 /// Param      01     ty               blockparam num  block index
670 /// Alias      10     ty               0               value index
671 /// Union      11     ty               first value     second value
672 /// ```
673 #[derive(Clone, Copy, Debug, PartialEq, Hash)]
674 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
675 struct ValueDataPacked(u64);
676 
677 /// Encodes a value in 0..2^32 into 0..2^n, where n is less than 32
678 /// (and is implied by `mask`), by translating 2^32-1 (0xffffffff)
679 /// into 2^n-1 and panic'ing on 2^n..2^32-1.
680 fn encode_narrow_field(x: u32, bits: u8) -> u32 {
681     let max = (1 << bits) - 1;
682     if x == 0xffff_ffff {
683         max
684     } else {
685         debug_assert!(
686             x < max,
687             "{x} does not fit into {bits} bits (must be less than {max} to \
688              allow for a 0xffffffff sentinel)"
689         );
690         x
691     }
692 }
693 
694 /// The inverse of the above `encode_narrow_field`: unpacks 2^n-1 into
695 /// 2^32-1.
696 fn decode_narrow_field(x: u32, bits: u8) -> u32 {
697     if x == (1 << bits) - 1 {
698         0xffff_ffff
699     } else {
700         x
701     }
702 }
703 
704 impl ValueDataPacked {
705     const Y_SHIFT: u8 = 0;
706     const Y_BITS: u8 = 24;
707     const X_SHIFT: u8 = Self::Y_SHIFT + Self::Y_BITS;
708     const X_BITS: u8 = 24;
709     const TYPE_SHIFT: u8 = Self::X_SHIFT + Self::X_BITS;
710     const TYPE_BITS: u8 = 14;
711     const TAG_SHIFT: u8 = Self::TYPE_SHIFT + Self::TYPE_BITS;
712     const TAG_BITS: u8 = 2;
713 
714     const TAG_INST: u64 = 0;
715     const TAG_PARAM: u64 = 1;
716     const TAG_ALIAS: u64 = 2;
717     const TAG_UNION: u64 = 3;
718 
719     fn make(tag: u64, ty: Type, x: u32, y: u32) -> ValueDataPacked {
720         debug_assert!(tag < (1 << Self::TAG_BITS));
721         debug_assert!(ty.repr() < (1 << Self::TYPE_BITS));
722 
723         let x = encode_narrow_field(x, Self::X_BITS);
724         let y = encode_narrow_field(y, Self::Y_BITS);
725 
726         ValueDataPacked(
727             (tag << Self::TAG_SHIFT)
728                 | ((ty.repr() as u64) << Self::TYPE_SHIFT)
729                 | ((x as u64) << Self::X_SHIFT)
730                 | ((y as u64) << Self::Y_SHIFT),
731         )
732     }
733 
734     #[inline(always)]
735     fn field(self, shift: u8, bits: u8) -> u64 {
736         (self.0 >> shift) & ((1 << bits) - 1)
737     }
738 
739     #[inline(always)]
740     fn ty(self) -> Type {
741         let ty = self.field(ValueDataPacked::TYPE_SHIFT, ValueDataPacked::TYPE_BITS) as u16;
742         Type::from_repr(ty)
743     }
744 
745     #[inline(always)]
746     fn set_type(&mut self, ty: Type) {
747         self.0 &= !(((1 << Self::TYPE_BITS) - 1) << Self::TYPE_SHIFT);
748         self.0 |= (ty.repr() as u64) << Self::TYPE_SHIFT;
749     }
750 }
751 
752 impl From<ValueData> for ValueDataPacked {
753     fn from(data: ValueData) -> Self {
754         match data {
755             ValueData::Inst { ty, num, inst } => {
756                 Self::make(Self::TAG_INST, ty, num.into(), inst.as_bits())
757             }
758             ValueData::Param { ty, num, block } => {
759                 Self::make(Self::TAG_PARAM, ty, num.into(), block.as_bits())
760             }
761             ValueData::Alias { ty, original } => {
762                 Self::make(Self::TAG_ALIAS, ty, 0, original.as_bits())
763             }
764             ValueData::Union { ty, x, y } => {
765                 Self::make(Self::TAG_UNION, ty, x.as_bits(), y.as_bits())
766             }
767         }
768     }
769 }
770 
771 impl From<ValueDataPacked> for ValueData {
772     fn from(data: ValueDataPacked) -> Self {
773         let tag = data.field(ValueDataPacked::TAG_SHIFT, ValueDataPacked::TAG_BITS);
774         let ty = u16::try_from(data.field(ValueDataPacked::TYPE_SHIFT, ValueDataPacked::TYPE_BITS))
775             .expect("Mask should ensure result fits in a u16");
776         let x = u32::try_from(data.field(ValueDataPacked::X_SHIFT, ValueDataPacked::X_BITS))
777             .expect("Mask should ensure result fits in a u32");
778         let y = u32::try_from(data.field(ValueDataPacked::Y_SHIFT, ValueDataPacked::Y_BITS))
779             .expect("Mask should ensure result fits in a u32");
780 
781         let ty = Type::from_repr(ty);
782         match tag {
783             ValueDataPacked::TAG_INST => ValueData::Inst {
784                 ty,
785                 num: u16::try_from(x).expect("Inst result num should fit in u16"),
786                 inst: Inst::from_bits(decode_narrow_field(y, ValueDataPacked::Y_BITS)),
787             },
788             ValueDataPacked::TAG_PARAM => ValueData::Param {
789                 ty,
790                 num: u16::try_from(x).expect("Blockparam index should fit in u16"),
791                 block: Block::from_bits(decode_narrow_field(y, ValueDataPacked::Y_BITS)),
792             },
793             ValueDataPacked::TAG_ALIAS => ValueData::Alias {
794                 ty,
795                 original: Value::from_bits(decode_narrow_field(y, ValueDataPacked::Y_BITS)),
796             },
797             ValueDataPacked::TAG_UNION => ValueData::Union {
798                 ty,
799                 x: Value::from_bits(decode_narrow_field(x, ValueDataPacked::X_BITS)),
800                 y: Value::from_bits(decode_narrow_field(y, ValueDataPacked::Y_BITS)),
801             },
802             _ => panic!("Invalid tag {} in ValueDataPacked 0x{:x}", tag, data.0),
803         }
804     }
805 }
806 
807 /// Instructions.
808 ///
809 impl DataFlowGraph {
810     /// Create a new instruction.
811     ///
812     /// The type of the first result is indicated by `data.ty`. If the
813     /// instruction produces multiple results, also call
814     /// `make_inst_results` to allocate value table entries. (It is
815     /// always safe to call `make_inst_results`, regardless of how
816     /// many results the instruction has.)
817     pub fn make_inst(&mut self, data: InstructionData) -> Inst {
818         let n = self.num_insts() + 1;
819         self.results.resize(n);
820         self.insts.0.push(data)
821     }
822 
823     /// Declares a dynamic vector type
824     pub fn make_dynamic_ty(&mut self, data: DynamicTypeData) -> DynamicType {
825         self.dynamic_types.push(data)
826     }
827 
828     /// Returns an object that displays `inst`.
829     pub fn display_inst<'a>(&'a self, inst: Inst) -> DisplayInst<'a> {
830         DisplayInst(self, inst)
831     }
832 
833     /// Returns an object that displays the given `value`'s defining instruction.
834     ///
835     /// Panics if the value is not defined by an instruction (i.e. it is a basic
836     /// block argument).
837     pub fn display_value_inst(&self, value: Value) -> DisplayInst<'_> {
838         match self.value_def(value) {
839             ir::ValueDef::Result(inst, _) => self.display_inst(inst),
840             ir::ValueDef::Param(_, _) => panic!("value is not defined by an instruction"),
841             ir::ValueDef::Union(_, _) => panic!("value is a union of two other values"),
842         }
843     }
844 
845     /// Construct a read-only visitor context for the values of this instruction.
846     pub fn inst_values<'dfg>(
847         &'dfg self,
848         inst: Inst,
849     ) -> impl DoubleEndedIterator<Item = Value> + 'dfg {
850         self.inst_args(inst)
851             .iter()
852             .chain(
853                 self.insts[inst]
854                     .branch_destination(&self.jump_tables)
855                     .into_iter()
856                     .flat_map(|branch| branch.args_slice(&self.value_lists).iter()),
857             )
858             .copied()
859     }
860 
861     /// Map a function over the values of the instruction.
862     pub fn map_inst_values<F>(&mut self, inst: Inst, body: F)
863     where
864         F: FnMut(Value) -> Value,
865     {
866         self.insts[inst].map_values(&mut self.value_lists, &mut self.jump_tables, body);
867     }
868 
869     /// Overwrite the instruction's value references with values from the iterator.
870     /// NOTE: the iterator provided is expected to yield at least as many values as the instruction
871     /// currently has.
872     pub fn overwrite_inst_values<I>(&mut self, inst: Inst, mut values: I)
873     where
874         I: Iterator<Item = Value>,
875     {
876         self.insts[inst].map_values(&mut self.value_lists, &mut self.jump_tables, |_| {
877             values.next().unwrap()
878         });
879     }
880 
881     /// Get all value arguments on `inst` as a slice.
882     pub fn inst_args(&self, inst: Inst) -> &[Value] {
883         self.insts[inst].arguments(&self.value_lists)
884     }
885 
886     /// Get all value arguments on `inst` as a mutable slice.
887     pub fn inst_args_mut(&mut self, inst: Inst) -> &mut [Value] {
888         self.insts[inst].arguments_mut(&mut self.value_lists)
889     }
890 
891     /// Get the fixed value arguments on `inst` as a slice.
892     pub fn inst_fixed_args(&self, inst: Inst) -> &[Value] {
893         let num_fixed_args = self.insts[inst]
894             .opcode()
895             .constraints()
896             .num_fixed_value_arguments();
897         &self.inst_args(inst)[..num_fixed_args]
898     }
899 
900     /// Get the fixed value arguments on `inst` as a mutable slice.
901     pub fn inst_fixed_args_mut(&mut self, inst: Inst) -> &mut [Value] {
902         let num_fixed_args = self.insts[inst]
903             .opcode()
904             .constraints()
905             .num_fixed_value_arguments();
906         &mut self.inst_args_mut(inst)[..num_fixed_args]
907     }
908 
909     /// Get the variable value arguments on `inst` as a slice.
910     pub fn inst_variable_args(&self, inst: Inst) -> &[Value] {
911         let num_fixed_args = self.insts[inst]
912             .opcode()
913             .constraints()
914             .num_fixed_value_arguments();
915         &self.inst_args(inst)[num_fixed_args..]
916     }
917 
918     /// Get the variable value arguments on `inst` as a mutable slice.
919     pub fn inst_variable_args_mut(&mut self, inst: Inst) -> &mut [Value] {
920         let num_fixed_args = self.insts[inst]
921             .opcode()
922             .constraints()
923             .num_fixed_value_arguments();
924         &mut self.inst_args_mut(inst)[num_fixed_args..]
925     }
926 
927     /// Create result values for an instruction that produces multiple results.
928     ///
929     /// Instructions that produce no result values only need to be created with `make_inst`,
930     /// otherwise call `make_inst_results` to allocate value table entries for the results.
931     ///
932     /// The result value types are determined from the instruction's value type constraints and the
933     /// provided `ctrl_typevar` type for polymorphic instructions. For non-polymorphic
934     /// instructions, `ctrl_typevar` is ignored, and `INVALID` can be used.
935     ///
936     /// The type of the first result value is also set, even if it was already set in the
937     /// `InstructionData` passed to `make_inst`. If this function is called with a single-result
938     /// instruction, that is the only effect.
939     pub fn make_inst_results(&mut self, inst: Inst, ctrl_typevar: Type) -> usize {
940         self.make_inst_results_reusing(inst, ctrl_typevar, iter::empty())
941     }
942 
943     /// Create result values for `inst`, reusing the provided detached values.
944     ///
945     /// Create a new set of result values for `inst` using `ctrl_typevar` to determine the result
946     /// types. Any values provided by `reuse` will be reused. When `reuse` is exhausted or when it
947     /// produces `None`, a new value is created.
948     pub fn make_inst_results_reusing<I>(
949         &mut self,
950         inst: Inst,
951         ctrl_typevar: Type,
952         reuse: I,
953     ) -> usize
954     where
955         I: Iterator<Item = Option<Value>>,
956     {
957         self.clear_results(inst);
958 
959         let mut reuse = reuse.fuse();
960         let result_tys: SmallVec<[_; 16]> = self.inst_result_types(inst, ctrl_typevar).collect();
961 
962         for (expected, &ty) in result_tys.iter().enumerate() {
963             let num = u16::try_from(expected).expect("Result value index should fit in u16");
964             let value_data = ValueData::Inst { ty, num, inst };
965             let v = if let Some(Some(v)) = reuse.next() {
966                 debug_assert_eq!(self.value_type(v), ty, "Reused {} is wrong type", ty);
967                 debug_assert!(!self.value_is_attached(v));
968                 self.values[v] = value_data.into();
969                 v
970             } else {
971                 self.make_value(value_data)
972             };
973             let actual = self.results[inst].push(v, &mut self.value_lists);
974             debug_assert_eq!(expected, actual);
975         }
976 
977         result_tys.len()
978     }
979 
980     /// Create a `ReplaceBuilder` that will replace `inst` with a new instruction in place.
981     pub fn replace(&mut self, inst: Inst) -> ReplaceBuilder {
982         ReplaceBuilder::new(self, inst)
983     }
984 
985     /// Clear the list of result values from `inst`.
986     ///
987     /// This leaves `inst` without any result values. New result values can be created by calling
988     /// `make_inst_results` or by using a `replace(inst)` builder.
989     pub fn clear_results(&mut self, inst: Inst) {
990         self.results[inst].clear(&mut self.value_lists)
991     }
992 
993     /// Replace an instruction result with a new value of type `new_type`.
994     ///
995     /// The `old_value` must be an attached instruction result.
996     ///
997     /// The old value is left detached, so it should probably be changed into something else.
998     ///
999     /// Returns the new value.
1000     pub fn replace_result(&mut self, old_value: Value, new_type: Type) -> Value {
1001         let (num, inst) = match ValueData::from(self.values[old_value]) {
1002             ValueData::Inst { num, inst, .. } => (num, inst),
1003             _ => panic!("{} is not an instruction result value", old_value),
1004         };
1005         let new_value = self.make_value(ValueData::Inst {
1006             ty: new_type,
1007             num,
1008             inst,
1009         });
1010         let num = num as usize;
1011         let attached = mem::replace(
1012             self.results[inst]
1013                 .get_mut(num, &mut self.value_lists)
1014                 .expect("Replacing detached result"),
1015             new_value,
1016         );
1017         debug_assert_eq!(
1018             attached,
1019             old_value,
1020             "{} wasn't detached from {}",
1021             old_value,
1022             self.display_inst(inst)
1023         );
1024         new_value
1025     }
1026 
1027     /// Clone an instruction, attaching new result `Value`s and
1028     /// returning them.
1029     pub fn clone_inst(&mut self, inst: Inst) -> Inst {
1030         // First, add a clone of the InstructionData.
1031         let inst_data = self.insts[inst].clone();
1032         // If the `inst_data` has a reference to a ValueList, clone it
1033         // as well, because we can't share these (otherwise mutating
1034         // one would affect the other).
1035         let inst_data = inst_data.deep_clone(&mut self.value_lists);
1036         let new_inst = self.make_inst(inst_data);
1037         // Get the controlling type variable.
1038         let ctrl_typevar = self.ctrl_typevar(inst);
1039         // Create new result values.
1040         let num_results = self.make_inst_results(new_inst, ctrl_typevar);
1041         // Copy over PCC facts, if any.
1042         for i in 0..num_results {
1043             let old_result = self.inst_results(inst)[i];
1044             let new_result = self.inst_results(new_inst)[i];
1045             self.facts[new_result] = self.facts[old_result].clone();
1046         }
1047         new_inst
1048     }
1049 
1050     /// Get the first result of an instruction.
1051     ///
1052     /// This function panics if the instruction doesn't have any result.
1053     pub fn first_result(&self, inst: Inst) -> Value {
1054         self.results[inst]
1055             .first(&self.value_lists)
1056             .expect("Instruction has no results")
1057     }
1058 
1059     /// Test if `inst` has any result values currently.
1060     pub fn has_results(&self, inst: Inst) -> bool {
1061         !self.results[inst].is_empty()
1062     }
1063 
1064     /// Return all the results of an instruction.
1065     pub fn inst_results(&self, inst: Inst) -> &[Value] {
1066         self.results[inst].as_slice(&self.value_lists)
1067     }
1068 
1069     /// Return all the results of an instruction as ValueList.
1070     pub fn inst_results_list(&self, inst: Inst) -> ValueList {
1071         self.results[inst]
1072     }
1073 
1074     /// Create a union of two values.
1075     pub fn union(&mut self, x: Value, y: Value) -> Value {
1076         // Get the type.
1077         let ty = self.value_type(x);
1078         debug_assert_eq!(ty, self.value_type(y));
1079         self.make_value(ValueData::Union { ty, x, y })
1080     }
1081 
1082     /// Get the call signature of a direct or indirect call instruction.
1083     /// Returns `None` if `inst` is not a call instruction.
1084     pub fn call_signature(&self, inst: Inst) -> Option<SigRef> {
1085         match self.insts[inst].analyze_call(&self.value_lists) {
1086             CallInfo::NotACall => None,
1087             CallInfo::Direct(f, _) => Some(self.ext_funcs[f].signature),
1088             CallInfo::Indirect(s, _) => Some(s),
1089         }
1090     }
1091 
1092     /// Like `call_signature` but returns none for tail call instructions.
1093     fn non_tail_call_signature(&self, inst: Inst) -> Option<SigRef> {
1094         let sig = self.call_signature(inst)?;
1095         match self.insts[inst].opcode() {
1096             ir::Opcode::ReturnCall | ir::Opcode::ReturnCallIndirect => None,
1097             _ => Some(sig),
1098         }
1099     }
1100 
1101     // Only for use by the verifier. Everyone else should just use
1102     // `dfg.inst_results(inst).len()`.
1103     pub(crate) fn num_expected_results_for_verifier(&self, inst: Inst) -> usize {
1104         match self.non_tail_call_signature(inst) {
1105             Some(sig) => self.signatures[sig].returns.len(),
1106             None => {
1107                 let constraints = self.insts[inst].opcode().constraints();
1108                 constraints.num_fixed_results()
1109             }
1110         }
1111     }
1112 
1113     /// Get the result types of the given instruction.
1114     pub fn inst_result_types<'a>(
1115         &'a self,
1116         inst: Inst,
1117         ctrl_typevar: Type,
1118     ) -> impl iter::ExactSizeIterator<Item = Type> + 'a {
1119         return match self.non_tail_call_signature(inst) {
1120             Some(sig) => InstResultTypes::Signature(self, sig, 0),
1121             None => {
1122                 let constraints = self.insts[inst].opcode().constraints();
1123                 InstResultTypes::Constraints(constraints, ctrl_typevar, 0)
1124             }
1125         };
1126 
1127         enum InstResultTypes<'a> {
1128             Signature(&'a DataFlowGraph, SigRef, usize),
1129             Constraints(ir::instructions::OpcodeConstraints, Type, usize),
1130         }
1131 
1132         impl Iterator for InstResultTypes<'_> {
1133             type Item = Type;
1134 
1135             fn next(&mut self) -> Option<Type> {
1136                 match self {
1137                     InstResultTypes::Signature(dfg, sig, i) => {
1138                         let param = dfg.signatures[*sig].returns.get(*i)?;
1139                         *i += 1;
1140                         Some(param.value_type)
1141                     }
1142                     InstResultTypes::Constraints(constraints, ctrl_ty, i) => {
1143                         if *i < constraints.num_fixed_results() {
1144                             let ty = constraints.result_type(*i, *ctrl_ty);
1145                             *i += 1;
1146                             Some(ty)
1147                         } else {
1148                             None
1149                         }
1150                     }
1151                 }
1152             }
1153 
1154             fn size_hint(&self) -> (usize, Option<usize>) {
1155                 let len = match self {
1156                     InstResultTypes::Signature(dfg, sig, i) => {
1157                         dfg.signatures[*sig].returns.len() - *i
1158                     }
1159                     InstResultTypes::Constraints(constraints, _, i) => {
1160                         constraints.num_fixed_results() - *i
1161                     }
1162                 };
1163                 (len, Some(len))
1164             }
1165         }
1166 
1167         impl ExactSizeIterator for InstResultTypes<'_> {}
1168     }
1169 
1170     /// Compute the type of an instruction result from opcode constraints and call signatures.
1171     ///
1172     /// This computes the same sequence of result types that `make_inst_results()` above would
1173     /// assign to the created result values, but it does not depend on `make_inst_results()` being
1174     /// called first.
1175     ///
1176     /// Returns `None` if asked about a result index that is too large.
1177     pub fn compute_result_type(
1178         &self,
1179         inst: Inst,
1180         result_idx: usize,
1181         ctrl_typevar: Type,
1182     ) -> Option<Type> {
1183         self.inst_result_types(inst, ctrl_typevar).nth(result_idx)
1184     }
1185 
1186     /// Get the controlling type variable, or `INVALID` if `inst` isn't polymorphic.
1187     pub fn ctrl_typevar(&self, inst: Inst) -> Type {
1188         let constraints = self.insts[inst].opcode().constraints();
1189 
1190         if !constraints.is_polymorphic() {
1191             types::INVALID
1192         } else if constraints.requires_typevar_operand() {
1193             // Not all instruction formats have a designated operand, but in that case
1194             // `requires_typevar_operand()` should never be true.
1195             self.value_type(
1196                 self.insts[inst]
1197                     .typevar_operand(&self.value_lists)
1198                     .unwrap_or_else(|| {
1199                         panic!(
1200                             "Instruction format for {:?} doesn't have a designated operand",
1201                             self.insts[inst]
1202                         )
1203                     }),
1204             )
1205         } else {
1206             self.value_type(self.first_result(inst))
1207         }
1208     }
1209 }
1210 
1211 /// basic blocks.
1212 impl DataFlowGraph {
1213     /// Create a new basic block.
1214     pub fn make_block(&mut self) -> Block {
1215         self.blocks.add()
1216     }
1217 
1218     /// Get the number of parameters on `block`.
1219     pub fn num_block_params(&self, block: Block) -> usize {
1220         self.blocks[block].params(&self.value_lists).len()
1221     }
1222 
1223     /// Get the parameters on `block`.
1224     pub fn block_params(&self, block: Block) -> &[Value] {
1225         self.blocks[block].params(&self.value_lists)
1226     }
1227 
1228     /// Get the types of the parameters on `block`.
1229     pub fn block_param_types(&self, block: Block) -> impl Iterator<Item = Type> + '_ {
1230         self.block_params(block).iter().map(|&v| self.value_type(v))
1231     }
1232 
1233     /// Append a parameter with type `ty` to `block`.
1234     pub fn append_block_param(&mut self, block: Block, ty: Type) -> Value {
1235         let param = self.values.next_key();
1236         let num = self.blocks[block].params.push(param, &mut self.value_lists);
1237         debug_assert!(num <= u16::MAX as usize, "Too many parameters on block");
1238         self.make_value(ValueData::Param {
1239             ty,
1240             num: num as u16,
1241             block,
1242         })
1243     }
1244 
1245     /// Removes `val` from `block`'s parameters by swapping it with the last parameter on `block`.
1246     /// Returns the position of `val` before removal.
1247     ///
1248     /// *Important*: to ensure O(1) deletion, this method swaps the removed parameter with the
1249     /// last `block` parameter. This can disrupt all the branch instructions jumping to this
1250     /// `block` for which you have to change the branch argument order if necessary.
1251     ///
1252     /// Panics if `val` is not a block parameter.
1253     pub fn swap_remove_block_param(&mut self, val: Value) -> usize {
1254         let (block, num) =
1255             if let ValueData::Param { num, block, .. } = ValueData::from(self.values[val]) {
1256                 (block, num)
1257             } else {
1258                 panic!("{} must be a block parameter", val);
1259             };
1260         self.blocks[block]
1261             .params
1262             .swap_remove(num as usize, &mut self.value_lists);
1263         if let Some(last_arg_val) = self.blocks[block]
1264             .params
1265             .get(num as usize, &self.value_lists)
1266         {
1267             // We update the position of the old last arg.
1268             let mut last_arg_data = ValueData::from(self.values[last_arg_val]);
1269             if let ValueData::Param {
1270                 num: ref mut old_num,
1271                 ..
1272             } = &mut last_arg_data
1273             {
1274                 *old_num = num;
1275                 self.values[last_arg_val] = last_arg_data.into();
1276             } else {
1277                 panic!("{} should be a Block parameter", last_arg_val);
1278             }
1279         }
1280         num as usize
1281     }
1282 
1283     /// Removes `val` from `block`'s parameters by a standard linear time list removal which
1284     /// preserves ordering. Also updates the values' data.
1285     pub fn remove_block_param(&mut self, val: Value) {
1286         let (block, num) =
1287             if let ValueData::Param { num, block, .. } = ValueData::from(self.values[val]) {
1288                 (block, num)
1289             } else {
1290                 panic!("{} must be a block parameter", val);
1291             };
1292         self.blocks[block]
1293             .params
1294             .remove(num as usize, &mut self.value_lists);
1295         for index in num..(self.num_block_params(block) as u16) {
1296             let packed = &mut self.values[self.blocks[block]
1297                 .params
1298                 .get(index as usize, &self.value_lists)
1299                 .unwrap()];
1300             let mut data = ValueData::from(*packed);
1301             match &mut data {
1302                 ValueData::Param { ref mut num, .. } => {
1303                     *num -= 1;
1304                     *packed = data.into();
1305                 }
1306                 _ => panic!(
1307                     "{} must be a block parameter",
1308                     self.blocks[block]
1309                         .params
1310                         .get(index as usize, &self.value_lists)
1311                         .unwrap()
1312                 ),
1313             }
1314         }
1315     }
1316 
1317     /// Append an existing value to `block`'s parameters.
1318     ///
1319     /// The appended value can't already be attached to something else.
1320     ///
1321     /// In almost all cases, you should be using `append_block_param()` instead of this method.
1322     pub fn attach_block_param(&mut self, block: Block, param: Value) {
1323         debug_assert!(!self.value_is_attached(param));
1324         let num = self.blocks[block].params.push(param, &mut self.value_lists);
1325         debug_assert!(num <= u16::MAX as usize, "Too many parameters on block");
1326         let ty = self.value_type(param);
1327         self.values[param] = ValueData::Param {
1328             ty,
1329             num: num as u16,
1330             block,
1331         }
1332         .into();
1333     }
1334 
1335     /// Replace a block parameter with a new value of type `ty`.
1336     ///
1337     /// The `old_value` must be an attached block parameter. It is removed from its place in the list
1338     /// of parameters and replaced by a new value of type `new_type`. The new value gets the same
1339     /// position in the list, and other parameters are not disturbed.
1340     ///
1341     /// The old value is left detached, so it should probably be changed into something else.
1342     ///
1343     /// Returns the new value.
1344     pub fn replace_block_param(&mut self, old_value: Value, new_type: Type) -> Value {
1345         // Create new value identical to the old one except for the type.
1346         let (block, num) =
1347             if let ValueData::Param { num, block, .. } = ValueData::from(self.values[old_value]) {
1348                 (block, num)
1349             } else {
1350                 panic!("{} must be a block parameter", old_value);
1351             };
1352         let new_arg = self.make_value(ValueData::Param {
1353             ty: new_type,
1354             num,
1355             block,
1356         });
1357 
1358         self.blocks[block]
1359             .params
1360             .as_mut_slice(&mut self.value_lists)[num as usize] = new_arg;
1361         new_arg
1362     }
1363 
1364     /// Detach all the parameters from `block` and return them as a `ValueList`.
1365     ///
1366     /// This is a quite low-level operation. Sensible things to do with the detached block parameters
1367     /// is to put them back on the same block with `attach_block_param()` or change them into aliases
1368     /// with `change_to_alias()`.
1369     pub fn detach_block_params(&mut self, block: Block) -> ValueList {
1370         self.blocks[block].params.take()
1371     }
1372 
1373     /// Merge the facts for two values. If both values have facts and
1374     /// they differ, both values get a special "conflict" fact that is
1375     /// never satisfied.
1376     pub fn merge_facts(&mut self, a: Value, b: Value) {
1377         let a = self.resolve_aliases(a);
1378         let b = self.resolve_aliases(b);
1379         match (&self.facts[a], &self.facts[b]) {
1380             (Some(a), Some(b)) if a == b => { /* nothing */ }
1381             (None, None) => { /* nothing */ }
1382             (Some(a), None) => {
1383                 self.facts[b] = Some(a.clone());
1384             }
1385             (None, Some(b)) => {
1386                 self.facts[a] = Some(b.clone());
1387             }
1388             (Some(a_fact), Some(b_fact)) => {
1389                 assert_eq!(self.value_type(a), self.value_type(b));
1390                 let merged = Fact::intersect(a_fact, b_fact);
1391                 crate::trace!(
1392                     "facts merge on {} and {}: {:?}, {:?} -> {:?}",
1393                     a,
1394                     b,
1395                     a_fact,
1396                     b_fact,
1397                     merged,
1398                 );
1399                 self.facts[a] = Some(merged.clone());
1400                 self.facts[b] = Some(merged);
1401             }
1402         }
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))]
1413 pub struct BlockData {
1414     /// List of parameters to this block.
1415     params: ValueList,
1416 }
1417 
1418 impl 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.
1432 pub struct DisplayInst<'a>(&'a DataFlowGraph, Inst);
1433 
1434 impl<'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.
1458 impl 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)]
1600 mod 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::User(0),
1651         };
1652         let inst = dfg.make_inst(idata);
1653         assert_eq!(dfg.display_inst(inst).to_string(), "trap user0");
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::condcodes::IntCC;
1754         use crate::ir::InstBuilder;
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.dfg.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 }
1807