1 //! Elaboration phase: lowers EGraph back to sequences of operations
2 //! in CFG nodes.
3 
4 use super::cost::Cost;
5 use super::Stats;
6 use crate::ctxhash::NullCtx;
7 use crate::dominator_tree::DominatorTreePreorder;
8 use crate::hash_map::Entry as HashEntry;
9 use crate::inst_predicates::is_pure_for_egraph;
10 use crate::ir::{Block, Function, Inst, Value, ValueDef};
11 use crate::loop_analysis::{Loop, LoopAnalysis};
12 use crate::scoped_hash_map::ScopedHashMap;
13 use crate::trace;
14 use alloc::vec::Vec;
15 use cranelift_control::ControlPlane;
16 use cranelift_entity::{packed_option::ReservedValue, SecondaryMap};
17 use rustc_hash::{FxHashMap, FxHashSet};
18 use smallvec::{smallvec, SmallVec};
19 
20 pub(crate) struct Elaborator<'a> {
21     func: &'a mut Function,
22     domtree: &'a DominatorTreePreorder,
23     loop_analysis: &'a LoopAnalysis,
24     /// Map from Value that is produced by a pure Inst (and was thus
25     /// not in the side-effecting skeleton) to the value produced by
26     /// an elaborated inst (placed in the layout) to whose results we
27     /// refer in the final code.
28     ///
29     /// The first time we use some result of an instruction during
30     /// elaboration, we can place it and insert an identity map (inst
31     /// results to that same inst's results) in this scoped
32     /// map. Within that block and its dom-tree children, that mapping
33     /// is visible and we can continue to use it. This allows us to
34     /// avoid cloning the instruction. However, if we pop that scope
35     /// and use it somewhere else as well, we will need to
36     /// duplicate. We detect this case by checking, when a value that
37     /// we want is not present in this map, whether the producing inst
38     /// is already placed in the Layout. If so, we duplicate, and
39     /// insert non-identity mappings from the original inst's results
40     /// to the cloned inst's results.
41     ///
42     /// Note that as values may refer to unions that represent a subset
43     /// of a larger eclass, it's not valid to walk towards the root of a
44     /// union tree: doing so would potentially equate values that fall
45     /// on different branches of the dominator tree.
46     value_to_elaborated_value: ScopedHashMap<Value, ElaboratedValue>,
47     /// Map from Value to the best (lowest-cost) Value in its eclass
48     /// (tree of union value-nodes).
49     value_to_best_value: SecondaryMap<Value, BestEntry>,
50     /// Stack of blocks and loops in current elaboration path.
51     loop_stack: SmallVec<[LoopStackEntry; 8]>,
52     /// The current block into which we are elaborating.
53     cur_block: Block,
54     /// Values that opt rules have indicated should be rematerialized
55     /// in every block they are used (e.g., immediates or other
56     /// "cheap-to-compute" ops).
57     remat_values: &'a FxHashSet<Value>,
58     /// Explicitly-unrolled value elaboration stack.
59     elab_stack: Vec<ElabStackEntry>,
60     /// Results from the elab stack.
61     elab_result_stack: Vec<ElaboratedValue>,
62     /// Explicitly-unrolled block elaboration stack.
63     block_stack: Vec<BlockStackEntry>,
64     /// Copies of values that have been rematerialized.
65     remat_copies: FxHashMap<(Block, Value), Value>,
66     /// Stats for various events during egraph processing, to help
67     /// with optimization of this infrastructure.
68     stats: &'a mut Stats,
69     /// Chaos-mode control-plane so we can test that we still get
70     /// correct results when our heuristics make bad decisions.
71     ctrl_plane: &'a mut ControlPlane,
72 }
73 
74 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
75 struct BestEntry(Cost, Value);
76 
77 impl PartialOrd for BestEntry {
78     fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
79         Some(self.cmp(other))
80     }
81 }
82 
83 impl Ord for BestEntry {
84     #[inline]
85     fn cmp(&self, other: &Self) -> std::cmp::Ordering {
86         self.0.cmp(&other.0).then_with(|| {
87             // Note that this comparison is reversed. When costs are equal,
88             // prefer the value with the bigger index. This is a heuristic that
89             // prefers results of rewrites to the original value, since we
90             // expect that our rewrites are generally improvements.
91             self.1.cmp(&other.1).reverse()
92         })
93     }
94 }
95 
96 #[derive(Clone, Copy, Debug)]
97 struct ElaboratedValue {
98     in_block: Block,
99     value: Value,
100 }
101 
102 #[derive(Clone, Debug)]
103 struct LoopStackEntry {
104     /// The loop identifier.
105     lp: Loop,
106     /// The hoist point: a block that immediately dominates this
107     /// loop. May not be an immediate predecessor, but will be a valid
108     /// point to place all loop-invariant ops: they must depend only
109     /// on inputs that dominate the loop, so are available at (the end
110     /// of) this block.
111     hoist_block: Block,
112     /// The depth in the scope map.
113     scope_depth: u32,
114 }
115 
116 #[derive(Clone, Debug)]
117 enum ElabStackEntry {
118     /// Next action is to resolve this value into an elaborated inst
119     /// (placed into the layout) that produces the value, and
120     /// recursively elaborate the insts that produce its args.
121     ///
122     /// Any inserted ops should be inserted before `before`, which is
123     /// the instruction demanding this value.
124     Start { value: Value, before: Inst },
125     /// Args have been pushed; waiting for results.
126     PendingInst {
127         inst: Inst,
128         result_idx: usize,
129         num_args: usize,
130         before: Inst,
131     },
132 }
133 
134 #[derive(Clone, Debug)]
135 enum BlockStackEntry {
136     Elaborate { block: Block, idom: Option<Block> },
137     Pop,
138 }
139 
140 impl<'a> Elaborator<'a> {
141     pub(crate) fn new(
142         func: &'a mut Function,
143         domtree: &'a DominatorTreePreorder,
144         loop_analysis: &'a LoopAnalysis,
145         remat_values: &'a FxHashSet<Value>,
146         stats: &'a mut Stats,
147         ctrl_plane: &'a mut ControlPlane,
148     ) -> Self {
149         let num_values = func.dfg.num_values();
150         let mut value_to_best_value =
151             SecondaryMap::with_default(BestEntry(Cost::infinity(), Value::reserved_value()));
152         value_to_best_value.resize(num_values);
153         Self {
154             func,
155             domtree,
156             loop_analysis,
157             value_to_elaborated_value: ScopedHashMap::with_capacity(num_values),
158             value_to_best_value,
159             loop_stack: smallvec![],
160             cur_block: Block::reserved_value(),
161             remat_values,
162             elab_stack: vec![],
163             elab_result_stack: vec![],
164             block_stack: vec![],
165             remat_copies: FxHashMap::default(),
166             stats,
167             ctrl_plane,
168         }
169     }
170 
171     fn start_block(&mut self, idom: Option<Block>, block: Block) {
172         trace!(
173             "start_block: block {:?} with idom {:?} at loop depth {:?} scope depth {}",
174             block,
175             idom,
176             self.loop_stack.len(),
177             self.value_to_elaborated_value.depth()
178         );
179 
180         // Pop any loop levels we're no longer in.
181         while let Some(inner_loop) = self.loop_stack.last() {
182             if self.loop_analysis.is_in_loop(block, inner_loop.lp) {
183                 break;
184             }
185             self.loop_stack.pop();
186         }
187 
188         // Note that if the *entry* block is a loop header, we will
189         // not make note of the loop here because it will not have an
190         // immediate dominator. We must disallow this case because we
191         // will skip adding the `LoopStackEntry` here but our
192         // `LoopAnalysis` will otherwise still make note of this loop
193         // and loop depths will not match.
194         if let Some(idom) = idom {
195             if let Some(lp) = self.loop_analysis.is_loop_header(block) {
196                 self.loop_stack.push(LoopStackEntry {
197                     lp,
198                     // Any code hoisted out of this loop will have code
199                     // placed in `idom`, and will have def mappings
200                     // inserted in to the scoped hashmap at that block's
201                     // level.
202                     hoist_block: idom,
203                     scope_depth: (self.value_to_elaborated_value.depth() - 1) as u32,
204                 });
205                 trace!(
206                     " -> loop header, pushing; depth now {}",
207                     self.loop_stack.len()
208                 );
209             }
210         } else {
211             debug_assert!(
212                 self.loop_analysis.is_loop_header(block).is_none(),
213                 "Entry block (domtree root) cannot be a loop header!"
214             );
215         }
216 
217         trace!("block {}: loop stack is {:?}", block, self.loop_stack);
218 
219         self.cur_block = block;
220     }
221 
222     fn compute_best_values(&mut self) {
223         let best = &mut self.value_to_best_value;
224 
225         // We can't make random decisions inside the fixpoint loop below because
226         // that could cause values to change on every iteration of the loop,
227         // which would make the loop never terminate. So in chaos testing
228         // mode we need a form of making suboptimal decisions that is fully
229         // deterministic. We choose to simply make the worst decision we know
230         // how to do instead of the best.
231         let use_worst = self.ctrl_plane.get_decision();
232 
233         // Do a fixpoint loop to compute the best value for each eclass.
234         //
235         // The maximum number of iterations is the length of the longest chain
236         // of `vNN -> vMM` edges in the dataflow graph where `NN < MM`, so this
237         // is *technically* quadratic, but `cranelift-frontend` won't construct
238         // any such edges. NaN canonicalization will introduce some of these
239         // edges, but they are chains of only two or three edges. So in
240         // practice, we *never* do more than a handful of iterations here unless
241         // (a) we parsed the CLIF from text and the text was funkily numbered,
242         // which we don't really care about, or (b) the CLIF producer did
243         // something weird, in which case it is their responsibility to stop
244         // doing that.
245         trace!(
246             "Entering fixpoint loop to compute the {} values for each eclass",
247             if use_worst {
248                 "worst (chaos mode)"
249             } else {
250                 "best"
251             }
252         );
253         let mut keep_going = true;
254         while keep_going {
255             keep_going = false;
256             trace!(
257                 "fixpoint iteration {}",
258                 self.stats.elaborate_best_cost_fixpoint_iters
259             );
260             self.stats.elaborate_best_cost_fixpoint_iters += 1;
261 
262             for (value, def) in self.func.dfg.values_and_defs() {
263                 trace!("computing best for value {:?} def {:?}", value, def);
264                 let orig_best_value = best[value];
265 
266                 match def {
267                     ValueDef::Union(x, y) => {
268                         // Pick the best of the two options based on
269                         // min-cost. This works because each element of `best`
270                         // is a `(cost, value)` tuple; `cost` comes first so
271                         // the natural comparison works based on cost, and
272                         // breaks ties based on value number.
273                         best[value] = if use_worst {
274                             if best[x].1.is_reserved_value() {
275                                 best[y]
276                             } else if best[y].1.is_reserved_value() {
277                                 best[x]
278                             } else {
279                                 std::cmp::max(best[x], best[y])
280                             }
281                         } else {
282                             std::cmp::min(best[x], best[y])
283                         };
284                         trace!(
285                             " -> best of union({:?}, {:?}) = {:?}",
286                             best[x],
287                             best[y],
288                             best[value]
289                         );
290                     }
291                     ValueDef::Param(_, _) => {
292                         best[value] = BestEntry(Cost::zero(), value);
293                     }
294                     // If the Inst is inserted into the layout (which is,
295                     // at this point, only the side-effecting skeleton),
296                     // then it must be computed and thus we give it zero
297                     // cost.
298                     ValueDef::Result(inst, _) => {
299                         if let Some(_) = self.func.layout.inst_block(inst) {
300                             best[value] = BestEntry(Cost::zero(), value);
301                         } else {
302                             let inst_data = &self.func.dfg.insts[inst];
303                             // N.B.: at this point we know that the opcode is
304                             // pure, so `pure_op_cost`'s precondition is
305                             // satisfied.
306                             let cost = Cost::of_pure_op(
307                                 inst_data.opcode(),
308                                 self.func.dfg.inst_values(inst).map(|value| best[value].0),
309                             );
310                             best[value] = BestEntry(cost, value);
311                             trace!(" -> cost of value {} = {:?}", value, cost);
312                         }
313                     }
314                 };
315 
316                 // Keep on iterating the fixpoint loop while we are finding new
317                 // best values.
318                 keep_going |= orig_best_value != best[value];
319             }
320         }
321 
322         if cfg!(any(feature = "trace-log", debug_assertions)) {
323             trace!("finished fixpoint loop to compute best value for each eclass");
324             for value in self.func.dfg.values() {
325                 trace!("-> best for eclass {:?}: {:?}", value, best[value]);
326                 debug_assert_ne!(best[value].1, Value::reserved_value());
327                 // You might additionally be expecting an assert that the best
328                 // cost is not infinity, however infinite cost *can* happen in
329                 // practice. First, note that our cost function doesn't know
330                 // about any shared structure in the dataflow graph, it only
331                 // sums operand costs. (And trying to avoid that by deduping a
332                 // single operation's operands is a losing game because you can
333                 // always just add one indirection and go from `add(x, x)` to
334                 // `add(foo(x), bar(x))` to hide the shared structure.) Given
335                 // that blindness to sharing, we can make cost grow
336                 // exponentially with a linear sequence of operations:
337                 //
338                 //     v0 = iconst.i32 1    ;; cost = 1
339                 //     v1 = iadd v0, v0     ;; cost = 3 + 1 + 1
340                 //     v2 = iadd v1, v1     ;; cost = 3 + 5 + 5
341                 //     v3 = iadd v2, v2     ;; cost = 3 + 13 + 13
342                 //     v4 = iadd v3, v3     ;; cost = 3 + 29 + 29
343                 //     v5 = iadd v4, v4     ;; cost = 3 + 61 + 61
344                 //     v6 = iadd v5, v5     ;; cost = 3 + 125 + 125
345                 //     ;; etc...
346                 //
347                 // Such a chain can cause cost to saturate to infinity. How do
348                 // we choose which e-node is best when there are multiple that
349                 // have saturated to infinity? It doesn't matter. As long as
350                 // invariant (2) for optimization rules is upheld by our rule
351                 // set (see `cranelift/codegen/src/opts/README.md`) it is safe
352                 // to choose *any* e-node in the e-class. At worst we will
353                 // produce suboptimal code, but never an incorrectness.
354             }
355         }
356     }
357 
358     /// Elaborate use of an eclass, inserting any needed new
359     /// instructions before the given inst `before`. Should only be
360     /// given values corresponding to results of instructions or
361     /// blockparams.
362     fn elaborate_eclass_use(&mut self, value: Value, before: Inst) -> ElaboratedValue {
363         debug_assert_ne!(value, Value::reserved_value());
364 
365         // Kick off the process by requesting this result
366         // value.
367         self.elab_stack
368             .push(ElabStackEntry::Start { value, before });
369 
370         // Now run the explicit-stack recursion until we reach
371         // the root.
372         self.process_elab_stack();
373         debug_assert_eq!(self.elab_result_stack.len(), 1);
374         self.elab_result_stack.pop().unwrap()
375     }
376 
377     /// Possibly rematerialize the instruction producing the value in
378     /// `arg` and rewrite `arg` to refer to it, if needed. Returns
379     /// `true` if a rewrite occurred.
380     fn maybe_remat_arg(
381         remat_values: &FxHashSet<Value>,
382         func: &mut Function,
383         remat_copies: &mut FxHashMap<(Block, Value), Value>,
384         insert_block: Block,
385         before: Inst,
386         arg: &mut ElaboratedValue,
387         stats: &mut Stats,
388     ) -> bool {
389         // TODO (#7313): we may want to consider recursive
390         // rematerialization as well. We could process the arguments of
391         // the rematerialized instruction up to a certain depth. This
392         // would affect, e.g., adds-with-one-constant-arg, which are
393         // currently rematerialized. Right now we don't do this, to
394         // avoid the need for another fixpoint loop here.
395         if arg.in_block != insert_block && remat_values.contains(&arg.value) {
396             let new_value = match remat_copies.entry((insert_block, arg.value)) {
397                 HashEntry::Occupied(o) => *o.get(),
398                 HashEntry::Vacant(v) => {
399                     let inst = func.dfg.value_def(arg.value).inst().unwrap();
400                     debug_assert_eq!(func.dfg.inst_results(inst).len(), 1);
401                     let new_inst = func.dfg.clone_inst(inst);
402                     func.layout.insert_inst(new_inst, before);
403                     let new_result = func.dfg.inst_results(new_inst)[0];
404                     *v.insert(new_result)
405                 }
406             };
407             trace!("rematerialized {} as {}", arg.value, new_value);
408             arg.value = new_value;
409             stats.elaborate_remat += 1;
410             true
411         } else {
412             false
413         }
414     }
415 
416     fn process_elab_stack(&mut self) {
417         while let Some(entry) = self.elab_stack.pop() {
418             match entry {
419                 ElabStackEntry::Start { value, before } => {
420                     debug_assert!(self.func.dfg.value_is_real(value));
421 
422                     self.stats.elaborate_visit_node += 1;
423 
424                     // Get the best option; we use `value` (latest
425                     // value) here so we have a full view of the
426                     // eclass.
427                     trace!("looking up best value for {}", value);
428                     let BestEntry(_, best_value) = self.value_to_best_value[value];
429                     trace!("elaborate: value {} -> best {}", value, best_value);
430                     debug_assert_ne!(best_value, Value::reserved_value());
431 
432                     if let Some(elab_val) =
433                         self.value_to_elaborated_value.get(&NullCtx, &best_value)
434                     {
435                         // Value is available; use it.
436                         trace!("elaborate: value {} -> {:?}", value, elab_val);
437                         self.stats.elaborate_memoize_hit += 1;
438                         self.elab_result_stack.push(*elab_val);
439                         continue;
440                     }
441 
442                     self.stats.elaborate_memoize_miss += 1;
443 
444                     // Now resolve the value to its definition to see
445                     // how we can compute it.
446                     let (inst, result_idx) = match self.func.dfg.value_def(best_value) {
447                         ValueDef::Result(inst, result_idx) => {
448                             trace!(
449                                 " -> value {} is result {} of {}",
450                                 best_value,
451                                 result_idx,
452                                 inst
453                             );
454                             (inst, result_idx)
455                         }
456                         ValueDef::Param(in_block, _) => {
457                             // We don't need to do anything to compute
458                             // this value; just push its result on the
459                             // result stack (blockparams are already
460                             // available).
461                             trace!(" -> value {} is a blockparam", best_value);
462                             self.elab_result_stack.push(ElaboratedValue {
463                                 in_block,
464                                 value: best_value,
465                             });
466                             continue;
467                         }
468                         ValueDef::Union(_, _) => {
469                             panic!("Should never have a Union value as the best value");
470                         }
471                     };
472 
473                     trace!(
474                         " -> result {} of inst {:?}",
475                         result_idx,
476                         self.func.dfg.insts[inst]
477                     );
478 
479                     // We're going to need to use this instruction
480                     // result, placing the instruction into the
481                     // layout. First, enqueue all args to be
482                     // elaborated. Push state to receive the results
483                     // and later elab this inst.
484                     let num_args = self.func.dfg.inst_values(inst).count();
485                     self.elab_stack.push(ElabStackEntry::PendingInst {
486                         inst,
487                         result_idx,
488                         num_args,
489                         before,
490                     });
491 
492                     // Push args in reverse order so we process the
493                     // first arg first.
494                     for arg in self.func.dfg.inst_values(inst).rev() {
495                         debug_assert_ne!(arg, Value::reserved_value());
496                         self.elab_stack
497                             .push(ElabStackEntry::Start { value: arg, before });
498                     }
499                 }
500 
501                 ElabStackEntry::PendingInst {
502                     inst,
503                     result_idx,
504                     num_args,
505                     before,
506                 } => {
507                     trace!(
508                         "PendingInst: {} result {} args {} before {}",
509                         inst,
510                         result_idx,
511                         num_args,
512                         before
513                     );
514 
515                     // We should have all args resolved at this
516                     // point. Grab them and drain them out, removing
517                     // them.
518                     let arg_idx = self.elab_result_stack.len() - num_args;
519                     let arg_values = &mut self.elab_result_stack[arg_idx..];
520 
521                     // Compute max loop depth.
522                     //
523                     // Note that if there are no arguments then this instruction
524                     // is allowed to get hoisted up one loop. This is not
525                     // usually used since no-argument values are things like
526                     // constants which are typically rematerialized, but for the
527                     // `vconst` instruction 128-bit constants aren't as easily
528                     // rematerialized. They're hoisted out of inner loops but
529                     // not to the function entry which may run the risk of
530                     // placing too much register pressure on the entire
531                     // function. This is modeled with the `.saturating_sub(1)`
532                     // as the default if there's otherwise no maximum.
533                     let loop_hoist_level = arg_values
534                         .iter()
535                         .map(|&value| {
536                             // Find the outermost loop level at which
537                             // the value's defining block *is not* a
538                             // member. This is the loop-nest level
539                             // whose hoist-block we hoist to.
540                             let hoist_level = self
541                                 .loop_stack
542                                 .iter()
543                                 .position(|loop_entry| {
544                                     !self.loop_analysis.is_in_loop(value.in_block, loop_entry.lp)
545                                 })
546                                 .unwrap_or(self.loop_stack.len());
547                             trace!(
548                                 " -> arg: elab_value {:?} hoist level {:?}",
549                                 value,
550                                 hoist_level
551                             );
552                             hoist_level
553                         })
554                         .max()
555                         .unwrap_or(self.loop_stack.len().saturating_sub(1));
556                     trace!(
557                         " -> loop hoist level: {:?}; cur loop depth: {:?}, loop_stack: {:?}",
558                         loop_hoist_level,
559                         self.loop_stack.len(),
560                         self.loop_stack,
561                     );
562 
563                     // We know that this is a pure inst, because
564                     // non-pure roots have already been placed in the
565                     // value-to-elab'd-value map, so they will not
566                     // reach this stage of processing.
567                     //
568                     // We now must determine the location at which we
569                     // place the instruction. This is the current
570                     // block *unless* we hoist above a loop when all
571                     // args are loop-invariant (and this op is pure).
572                     let (scope_depth, before, insert_block) =
573                         if loop_hoist_level == self.loop_stack.len() {
574                             // Depends on some value at the current
575                             // loop depth, or remat forces it here:
576                             // place it at the current location.
577                             (
578                                 self.value_to_elaborated_value.depth(),
579                                 before,
580                                 self.func.layout.inst_block(before).unwrap(),
581                             )
582                         } else {
583                             // Does not depend on any args at current
584                             // loop depth: hoist out of loop.
585                             self.stats.elaborate_licm_hoist += 1;
586                             let data = &self.loop_stack[loop_hoist_level];
587                             // `data.hoist_block` should dominate `before`'s block.
588                             let before_block = self.func.layout.inst_block(before).unwrap();
589                             debug_assert!(self.domtree.dominates(data.hoist_block, before_block));
590                             // Determine the instruction at which we
591                             // insert in `data.hoist_block`.
592                             let before = self.func.layout.last_inst(data.hoist_block).unwrap();
593                             (data.scope_depth as usize, before, data.hoist_block)
594                         };
595 
596                     trace!(
597                         " -> decided to place: before {} insert_block {}",
598                         before,
599                         insert_block
600                     );
601 
602                     // Now that we have the location for the
603                     // instruction, check if any of its args are remat
604                     // values. If so, and if we don't have a copy of
605                     // the rematerializing instruction for this block
606                     // yet, create one.
607                     let mut remat_arg = false;
608                     for arg_value in arg_values.iter_mut() {
609                         if Self::maybe_remat_arg(
610                             &self.remat_values,
611                             &mut self.func,
612                             &mut self.remat_copies,
613                             insert_block,
614                             before,
615                             arg_value,
616                             &mut self.stats,
617                         ) {
618                             remat_arg = true;
619                         }
620                     }
621 
622                     // Now we need to place `inst` at the computed
623                     // location (just before `before`). Note that
624                     // `inst` may already have been placed somewhere
625                     // else, because a pure node may be elaborated at
626                     // more than one place. In this case, we need to
627                     // duplicate the instruction (and return the
628                     // `Value`s for that duplicated instance instead).
629                     //
630                     // Also clone if we rematerialized, because we
631                     // don't want to rewrite the args in the original
632                     // copy.
633                     trace!("need inst {} before {}", inst, before);
634                     let inst = if self.func.layout.inst_block(inst).is_some() || remat_arg {
635                         // Clone the inst!
636                         let new_inst = self.func.dfg.clone_inst(inst);
637                         trace!(
638                             " -> inst {} already has a location; cloned to {}",
639                             inst,
640                             new_inst
641                         );
642                         // Create mappings in the
643                         // value-to-elab'd-value map from original
644                         // results to cloned results.
645                         for (&result, &new_result) in self
646                             .func
647                             .dfg
648                             .inst_results(inst)
649                             .iter()
650                             .zip(self.func.dfg.inst_results(new_inst).iter())
651                         {
652                             let elab_value = ElaboratedValue {
653                                 value: new_result,
654                                 in_block: insert_block,
655                             };
656                             let best_result = self.value_to_best_value[result];
657                             self.value_to_elaborated_value.insert_if_absent_with_depth(
658                                 &NullCtx,
659                                 best_result.1,
660                                 elab_value,
661                                 scope_depth,
662                             );
663 
664                             self.value_to_best_value[new_result] = best_result;
665 
666                             trace!(
667                                 " -> cloned inst has new result {} for orig {}",
668                                 new_result,
669                                 result
670                             );
671                         }
672                         new_inst
673                     } else {
674                         trace!(" -> no location; using original inst");
675                         // Create identity mappings from result values
676                         // to themselves in this scope, since we're
677                         // using the original inst.
678                         for &result in self.func.dfg.inst_results(inst) {
679                             let elab_value = ElaboratedValue {
680                                 value: result,
681                                 in_block: insert_block,
682                             };
683                             let best_result = self.value_to_best_value[result];
684                             self.value_to_elaborated_value.insert_if_absent_with_depth(
685                                 &NullCtx,
686                                 best_result.1,
687                                 elab_value,
688                                 scope_depth,
689                             );
690                             trace!(" -> inserting identity mapping for {}", result);
691                         }
692                         inst
693                     };
694 
695                     // Place the inst just before `before`.
696                     assert!(
697                         is_pure_for_egraph(self.func, inst),
698                         "something has gone very wrong if we are elaborating effectful \
699                          instructions, they should have remained in the skeleton"
700                     );
701                     self.func.layout.insert_inst(inst, before);
702 
703                     // Update the inst's arguments.
704                     self.func
705                         .dfg
706                         .overwrite_inst_values(inst, arg_values.into_iter().map(|ev| ev.value));
707 
708                     // Now that we've consumed the arg values, pop
709                     // them off the stack.
710                     self.elab_result_stack.truncate(arg_idx);
711 
712                     // Push the requested result index of the
713                     // instruction onto the elab-results stack.
714                     self.elab_result_stack.push(ElaboratedValue {
715                         in_block: insert_block,
716                         value: self.func.dfg.inst_results(inst)[result_idx],
717                     });
718                 }
719             }
720         }
721     }
722 
723     fn elaborate_block(&mut self, elab_values: &mut Vec<Value>, idom: Option<Block>, block: Block) {
724         trace!("elaborate_block: block {}", block);
725         self.start_block(idom, block);
726 
727         // Iterate over the side-effecting skeleton using the linked
728         // list in Layout. We will insert instructions that are
729         // elaborated *before* `inst`, so we can always use its
730         // next-link to continue the iteration.
731         let mut next_inst = self.func.layout.first_inst(block);
732         let mut first_branch = None;
733         while let Some(inst) = next_inst {
734             trace!(
735                 "elaborating inst {} with results {:?}",
736                 inst,
737                 self.func.dfg.inst_results(inst)
738             );
739             // Record the first branch we see in the block; all
740             // elaboration for args of *any* branch must be inserted
741             // before the *first* branch, because the branch group
742             // must remain contiguous at the end of the block.
743             if self.func.dfg.insts[inst].opcode().is_branch() && first_branch == None {
744                 first_branch = Some(inst);
745             }
746 
747             // Determine where elaboration inserts insts.
748             let before = first_branch.unwrap_or(inst);
749             trace!(" -> inserting before {}", before);
750 
751             elab_values.extend(self.func.dfg.inst_values(inst));
752             for arg in elab_values.iter_mut() {
753                 trace!(" -> arg {}", *arg);
754                 // Elaborate the arg, placing any newly-inserted insts
755                 // before `before`. Get the updated value, which may
756                 // be different than the original.
757                 let mut new_arg = self.elaborate_eclass_use(*arg, before);
758                 Self::maybe_remat_arg(
759                     &self.remat_values,
760                     &mut self.func,
761                     &mut self.remat_copies,
762                     block,
763                     inst,
764                     &mut new_arg,
765                     &mut self.stats,
766                 );
767                 trace!("   -> rewrote arg to {:?}", new_arg);
768                 *arg = new_arg.value;
769             }
770             self.func
771                 .dfg
772                 .overwrite_inst_values(inst, elab_values.drain(..));
773 
774             // We need to put the results of this instruction in the
775             // map now.
776             for &result in self.func.dfg.inst_results(inst) {
777                 trace!(" -> result {}", result);
778                 let best_result = self.value_to_best_value[result];
779                 self.value_to_elaborated_value.insert_if_absent(
780                     &NullCtx,
781                     best_result.1,
782                     ElaboratedValue {
783                         in_block: block,
784                         value: result,
785                     },
786                 );
787             }
788 
789             next_inst = self.func.layout.next_inst(inst);
790         }
791     }
792 
793     fn elaborate_domtree(&mut self, domtree: &DominatorTreePreorder) {
794         self.block_stack.push(BlockStackEntry::Elaborate {
795             block: self.func.layout.entry_block().unwrap(),
796             idom: None,
797         });
798 
799         // A temporary workspace for elaborate_block, allocated here to maximize the use of the
800         // allocation.
801         let mut elab_values = Vec::new();
802 
803         while let Some(top) = self.block_stack.pop() {
804             match top {
805                 BlockStackEntry::Elaborate { block, idom } => {
806                     self.block_stack.push(BlockStackEntry::Pop);
807                     self.value_to_elaborated_value.increment_depth();
808 
809                     self.elaborate_block(&mut elab_values, idom, block);
810 
811                     // Push children. We are doing a preorder
812                     // traversal so we do this after processing this
813                     // block above.
814                     let block_stack_end = self.block_stack.len();
815                     for child in self.ctrl_plane.shuffled(domtree.children(block)) {
816                         self.block_stack.push(BlockStackEntry::Elaborate {
817                             block: child,
818                             idom: Some(block),
819                         });
820                     }
821                     // Reverse what we just pushed so we elaborate in
822                     // original block order. (The domtree iter is a
823                     // single-ended iter over a singly-linked list so
824                     // we can't `.rev()` above.)
825                     self.block_stack[block_stack_end..].reverse();
826                 }
827                 BlockStackEntry::Pop => {
828                     self.value_to_elaborated_value.decrement_depth();
829                 }
830             }
831         }
832     }
833 
834     pub(crate) fn elaborate(&mut self) {
835         self.stats.elaborate_func += 1;
836         self.stats.elaborate_func_pre_insts += self.func.dfg.num_insts() as u64;
837         self.compute_best_values();
838         self.elaborate_domtree(&self.domtree);
839         self.stats.elaborate_func_post_insts += self.func.dfg.num_insts() as u64;
840     }
841 }
842