Skip to main content

charon_lib/transform/control_flow/
ullbc_to_llbc.rs

1//! ULLBC to LLBC
2//!
3//! We reconstruct the control-flow in the Unstructured LLBC.
4//!
5//! The reconstruction algorithm is not written to be efficient (its complexity
6//! is probably very bad), but it was not written to be: this is still an early
7//! stage and we want the algorithm to generate the best reconstruction as
8//! possible. We still need to test the algorithm on more interesting examples,
9//! and will consider making it more efficient once it is a bit mature and well
10//! tested.
11//! Also note that we more importantly focus on making the algorithm sound: the
12//! reconstructed program must always be equivalent to the original MIR program,
13//! and the fact that the reconstruction preserves this property must be obvious.
14use itertools::Itertools;
15use petgraph::algo::dijkstra;
16use petgraph::algo::dominators::{Dominators, simple_fast};
17use petgraph::graphmap::DiGraphMap;
18use petgraph::visit::{
19    Dfs, DfsPostOrder, EdgeFiltered, EdgeRef, GraphRef, IntoNeighbors, VisitMap, Visitable, Walker,
20};
21use smallvec::SmallVec;
22use std::cmp::Reverse;
23use std::collections::{HashMap, HashSet};
24use std::mem;
25
26use crate::common::ensure_sufficient_stack;
27use crate::ids::IndexVec;
28use crate::llbc_ast::{self as tgt, StatementId};
29use crate::meta::{Span, combine_span};
30use crate::transform::TransformCtx;
31use crate::transform::ctx::TransformPass;
32use crate::ullbc_ast::{self as src, BlockId};
33use crate::{ast::*, register_error};
34
35pub enum StackAction<N> {
36    PopPath,
37    Explore(N),
38}
39pub struct DfsWithPath<N, VM> {
40    /// The stack of nodes to visit
41    pub stack: Vec<StackAction<N>>,
42    /// The map of discovered nodes
43    pub discovered: VM,
44    /// The path from start node to current node.
45    pub path: Vec<N>,
46}
47impl<N, VM> DfsWithPath<N, VM>
48where
49    N: Copy + PartialEq,
50    VM: VisitMap<N>,
51{
52    /// Create a new **DfsWithPath**, using the graph's visitor map, and put **start** in the stack
53    /// of nodes to visit.
54    pub fn new<G>(graph: G, start: N) -> Self
55    where
56        G: GraphRef + Visitable<NodeId = N, Map = VM>,
57    {
58        Self {
59            stack: vec![StackAction::Explore(start)],
60            discovered: graph.visit_map(),
61            path: vec![],
62        }
63    }
64
65    /// Return the next node in the dfs, or **None** if the traversal is done.
66    pub fn next<G>(&mut self, graph: G) -> Option<N>
67    where
68        G: IntoNeighbors<NodeId = N>,
69    {
70        while let Some(action) = self.stack.pop() {
71            match action {
72                StackAction::Explore(node) => {
73                    if self.discovered.visit(node) {
74                        self.path.push(node);
75                        self.stack.push(StackAction::PopPath);
76                        for succ in graph.neighbors(node) {
77                            if !self.discovered.is_visited(&succ) {
78                                self.stack.push(StackAction::Explore(succ));
79                            }
80                        }
81                        return Some(node);
82                    }
83                }
84                StackAction::PopPath => {
85                    self.path.pop();
86                }
87            }
88        }
89        None
90    }
91}
92
93/// Arbitrary-precision numbers
94type BigUint = fraction::DynaInt<u64, fraction::BigUint>;
95type BigRational = fraction::Ratio<BigUint>;
96
97/// Control-Flow Graph
98type Cfg = DiGraphMap<src::BlockId, ()>;
99
100/// Information precomputed about a function's CFG.
101#[derive(Debug)]
102struct CfgInfo {
103    /// The CFG
104    pub cfg: Cfg,
105    /// The CFG where all the backward edges have been removed. Aka "forward CFG".
106    pub fwd_cfg: Cfg,
107    /// We consider the destination of the backward edges to be loop entries and
108    /// store them here.
109    pub loop_entries: HashSet<src::BlockId>,
110    /// The blocks whose terminators are a switch are stored here.
111    pub switch_blocks: HashSet<src::BlockId>,
112    /// Tree of which nodes dominates which other nodes.
113    #[expect(unused)]
114    pub dominator_tree: Dominators<BlockId>,
115    /// Computed data about each block.
116    pub block_data: IndexVec<BlockId, Box<BlockData>>,
117}
118
119#[derive(Debug)]
120struct BlockData {
121    pub id: BlockId,
122    pub span: Span,
123    /// The (unique) entrypoints of each loop. Unique because we error on irreducible cfgs.
124    pub is_loop_header: bool,
125    /// Whether this block is a switch.
126    pub is_switch: bool,
127    /// Whether this block is only reachable by taking an unwind edge.
128    pub is_unwind: bool,
129    /// Whether this block has multiple incoming control-flow edges in the forward graph.
130    pub is_merge_target: bool,
131    /// Order in a reverse postorder numbering. `None` if the block is unreachable.
132    pub reverse_postorder: Option<u32>,
133    /// Nodes that this block immediately dominates. Sorted by reverse_postorder_id, with largest
134    /// id first.
135    pub immediately_dominates: SmallVec<[BlockId; 2]>,
136    /// The nodes from `immediately_dominates` that are also merge targets. Sorted in the same
137    /// order.
138    pub immediately_dominated_merge_targets: SmallVec<[BlockId; 2]>,
139    /// List of loops inside of which this node is (loops are identified by their header). A node
140    /// is considered inside a loop if it is reachable from the loop header and if it can reach the
141    /// loop header using only the backwards edges into it (i.e. we don't count a path that enters
142    /// the loop header through a forward edge).
143    ///
144    /// Note that we might have to take a backward edge to reach the loop header, e.g.:
145    ///   'a: loop {
146    ///       // ...
147    ///       'b: loop {
148    ///           // ...
149    ///           if true {
150    ///               continue 'a;
151    ///           } else {
152    ///               if true {
153    ///                   break 'a;
154    ///               }
155    ///               // This node has to take two backward edges in order to reach the start of `'a`.
156    ///           }
157    ///       }
158    ///   }
159    ///
160    /// The restriction on backwards edges is for the following case:
161    ///   loop {
162    ///     loop {
163    ///       ..
164    ///     }
165    ///     // Not in inner loop
166    ///   }
167    ///
168    /// This is sorted by path order from the graph root.
169    pub within_loops: SmallVec<[BlockId; 2]>,
170    /// Node from where we can only reach error nodes (panic, etc.)
171    // TODO: track more nicely the set of targets reachable from a node: panic, return, exit loop,
172    // continue loop (this is a partial order).
173    pub only_reach_error: bool,
174    /// List of reachable nodes, with the length of shortest path to them. Includes the current
175    /// node.
176    pub shortest_paths: hashbrown::HashMap<BlockId, usize>,
177    /// Let's say we put a quantity of water equal to 1 on the block, and the water flows downards.
178    /// Whenever there is a branching, the quantity of water gets equally divided between the
179    /// branches. When the control flows join, we put the water back together. The set below
180    /// computes the amount of water received by each descendant of the node.
181    ///
182    /// TODO: there must be a known algorithm which computes this, right?...
183    /// This is exactly this problems:
184    /// <https://stackoverflow.com/questions/78221666/algorithm-for-total-flow-through-weighted-directed-acyclic-graph>
185    /// TODO: the way I compute this is not efficient.
186    pub flow: IndexVec<BlockId, BigRational>,
187    /// Reconstructed information about loops and switches.
188    pub exit_info: ExitInfo,
189}
190
191#[derive(Debug, Default, Clone)]
192struct ExitInfo {
193    /// The loop exit
194    loop_exit: Option<src::BlockId>,
195    /// The switch exit.
196    switch_exit: Option<src::BlockId>,
197}
198
199/// Error indicating that the control-flow graph is not reducible. The contained block id is a
200/// block involved in an irreducible subgraph.
201struct Irreducible(BlockId);
202
203impl CfgInfo {
204    /// Build the CFGs (the "regular" CFG and the CFG without backward edges) and precompute a
205    /// bunch of graph information about the CFG.
206    fn build(ctx: &TransformCtx, body: &src::BodyContents) -> Result<Self, Irreducible> {
207        // The steps in this function follow a precise order, as each step typically requires the
208        // previous one.
209        let start_block = BlockId::ZERO;
210
211        let empty_flow = body.map_ref(|_| BigRational::new(0u64.into(), 1u64.into()));
212        let mut block_data: IndexVec<BlockId, _> = body.map_ref_indexed(|id, contents| {
213            Box::new(BlockData {
214                id,
215                span: contents.terminator.span,
216                is_loop_header: false,
217                is_switch: false,
218                is_unwind: false,
219                is_merge_target: false,
220                reverse_postorder: None,
221                immediately_dominates: Default::default(),
222                immediately_dominated_merge_targets: Default::default(),
223                within_loops: Default::default(),
224                only_reach_error: false,
225                shortest_paths: Default::default(),
226                flow: empty_flow.clone(),
227                exit_info: Default::default(),
228            })
229        });
230
231        // Build the node graph.
232        let mut cfg = Cfg::new();
233        let mut cfg_without_unwind = Cfg::new();
234        for (block_id, block) in body.iter_enumerated() {
235            cfg.add_node(block_id);
236            cfg_without_unwind.add_node(block_id);
237            for tgt in block.targets() {
238                cfg.add_edge(block_id, tgt, ());
239            }
240            for tgt in block.targets_ignoring_unwind() {
241                cfg_without_unwind.add_edge(block_id, tgt, ());
242            }
243        }
244
245        // Compute the dominator tree.
246        let dominator_tree = simple_fast(&cfg, start_block);
247
248        // Compute reverse postorder numbering.
249        for (i, block_id) in DfsPostOrder::new(&cfg, start_block).iter(&cfg).enumerate() {
250            let rev_post_id = body.len() - i;
251            block_data[block_id].reverse_postorder = Some(rev_post_id.try_into().unwrap());
252
253            // Store the dominator tree in `block_data`.
254            if let Some(dominator) = dominator_tree.immediate_dominator(block_id) {
255                block_data[dominator].immediately_dominates.push(block_id);
256            }
257        }
258
259        // Compute the forward graph (without backward edges).
260        let mut fwd_cfg = Cfg::new();
261        let mut loop_entries = HashSet::new();
262        let mut switch_blocks = HashSet::new();
263        for block_id in Dfs::new(&cfg, start_block).iter(&cfg) {
264            fwd_cfg.add_node(block_id);
265
266            if body[block_id].terminator.kind.is_switch() {
267                switch_blocks.insert(block_id);
268                block_data[block_id].is_switch = true;
269            }
270
271            // Iterate over edges into this node (so that we can determine whether this node is a
272            // loop header).
273            let mut incoming_fwd_edges = 0;
274            for from in cfg.neighbors_directed(block_id, petgraph::Direction::Incoming) {
275                // Check if the edge is a backward edge.
276                if block_data[from].reverse_postorder >= block_data[block_id].reverse_postorder {
277                    // This is a backward edge
278                    block_data[block_id].is_loop_header = true;
279                    loop_entries.insert(block_id);
280                    // A cfg is reducible iff the target of every back edge dominates the
281                    // edge's source.
282                    if !dominator_tree.dominators(from).unwrap().contains(&block_id) {
283                        return Err(Irreducible(from));
284                    }
285                } else {
286                    incoming_fwd_edges += 1;
287                    fwd_cfg.add_edge(from, block_id, ());
288                }
289            }
290
291            // Detect merge targets.
292            if incoming_fwd_edges >= 2 {
293                block_data[block_id].is_merge_target = true;
294            }
295        }
296
297        let reachable_without_unwind: HashSet<BlockId> = Dfs::new(&cfg_without_unwind, start_block)
298            .iter(&cfg_without_unwind)
299            .collect();
300
301        // Finish filling in information.
302        for block_id in DfsPostOrder::new(&fwd_cfg, start_block).iter(&fwd_cfg) {
303            let block = &body[block_id];
304            let targets = cfg.neighbors(block_id).collect_vec();
305            let fwd_targets = fwd_cfg.neighbors(block_id).collect_vec();
306
307            // Compute the nodes that are part of unwind paths.
308            block_data[block_id].is_unwind = !reachable_without_unwind.contains(&block_id);
309
310            // Compute the nodes that can only reach error nodes.
311            // The node can only reach error nodes if:
312            // - it is an error node;
313            // - or it has neighbors and they all lead to errors.
314            // Note that if there is a backward edge, `only_reach_error` cannot contain this
315            // node yet. In other words, this does not consider infinite loops as reaching an
316            // error node.
317            if block.terminator.is_error()
318                || (!targets.is_empty()
319                    && targets.iter().all(|&tgt| block_data[tgt].only_reach_error))
320            {
321                block_data[block_id].only_reach_error = true;
322            }
323
324            // Compute the flows between each pair of nodes.
325            let mut flow: IndexVec<src::BlockId, BigRational> =
326                mem::take(&mut block_data[block_id].flow);
327            // The flow to self is 1.
328            flow[block_id] = BigRational::new(1u64.into(), 1u64.into());
329            // If a block has both regular and unwind targets, don't let the unwind path dilute
330            // the normal-control-flow heuristic used to pick switch exits. Unwind-only subgraphs
331            // still flow through their own targets.
332            let mut flow_targets = fwd_targets
333                .iter()
334                .copied()
335                .filter(|&child| !block_data[child].is_unwind)
336                .collect_vec();
337            if flow_targets.is_empty() {
338                flow_targets = fwd_targets;
339            }
340            // Divide the flow from each child to a given target block by the number of children.
341            // This is a sparse matrix multiplication and could be implemented using a linalg
342            // library.
343            let num_children: BigUint = flow_targets.len().into();
344            for child in flow_targets {
345                for grandchild in block_data[child].reachable_including_self() {
346                    // Flow from `child` to `grandchild`
347                    flow[grandchild] += &block_data[child].flow[grandchild] / &num_children;
348                }
349            }
350            block_data[block_id].flow = flow;
351
352            // Compute shortest paths to all reachable nodes in the forward graph.
353            block_data[block_id].shortest_paths = dijkstra(&fwd_cfg, block_id, None, |_| 1usize);
354
355            // Fill in the rest of the domination data.
356            let mut dominatees = mem::take(&mut block_data[block_id].immediately_dominates);
357            dominatees.sort_by_key(|&child| block_data[child].reverse_postorder);
358            dominatees.reverse();
359            block_data[block_id].immediately_dominates = dominatees;
360            block_data[block_id].immediately_dominated_merge_targets = block_data[block_id]
361                .immediately_dominates
362                .iter()
363                .copied()
364                .filter(|&child| block_data[child].is_merge_target)
365                .collect();
366        }
367
368        // Fill in the within_loop information. See the docs of `within_loops` to understand what
369        // we're computing.
370        let mut path_dfs = DfsWithPath::new(&cfg, start_block);
371        while let Some(block_id) = path_dfs.next(&cfg) {
372            // Store all the loops on the path to this
373            // node.
374            let mut within_loops: SmallVec<_> = path_dfs
375                .path
376                .iter()
377                .copied()
378                .filter(|&loop_id| block_data[loop_id].is_loop_header)
379                .collect();
380            // The loops that we can reach by taking a single backward edge.
381            let loops_directly_within = within_loops
382                .iter()
383                .copied()
384                .filter(|&loop_header| {
385                    cfg.neighbors_directed(loop_header, petgraph::Direction::Incoming)
386                        .any(|bid| block_data[block_id].shortest_paths.contains_key(&bid))
387                })
388                .collect_vec();
389            // The loops that we can reach by taking any number of backward edges.
390            let loops_indirectly_within: HashSet<_> = loops_directly_within
391                .iter()
392                .copied()
393                .flat_map(|loop_header| &block_data[loop_header].within_loops)
394                .chain(&loops_directly_within)
395                .copied()
396                .collect();
397            within_loops.retain(|id| loops_indirectly_within.contains(id));
398            block_data[block_id].within_loops = within_loops;
399        }
400
401        let mut cfg = CfgInfo {
402            cfg,
403            fwd_cfg,
404            loop_entries,
405            switch_blocks,
406            dominator_tree,
407            block_data,
408        };
409
410        // Pick an exit block for each loop, if we find one.
411        ExitInfo::compute_loop_exits(ctx, &mut cfg);
412
413        // Pick an exit block for each switch, if we find one.
414        ExitInfo::compute_switch_exits(&mut cfg);
415
416        Ok(cfg)
417    }
418
419    fn block_data(&self, block_id: BlockId) -> &BlockData {
420        &self.block_data[block_id]
421    }
422    // fn can_reach(&self, src: BlockId, tgt: BlockId) -> bool {
423    //     self.block_data[src].shortest_paths.contains_key(&tgt)
424    // }
425    fn topo_rank(&self, block_id: BlockId) -> u32 {
426        self.block_data[block_id].reverse_postorder.unwrap()
427    }
428    #[expect(unused)]
429    fn is_backward_edge(&self, src: BlockId, tgt: BlockId) -> bool {
430        self.block_data[src].reverse_postorder >= self.block_data[tgt].reverse_postorder
431            && self.cfg.contains_edge(src, tgt)
432    }
433
434    /// Check if the node is within the given loop.
435    fn is_within_loop(&self, loop_header: src::BlockId, block_id: src::BlockId) -> bool {
436        self.block_data[block_id]
437            .within_loops
438            .contains(&loop_header)
439    }
440
441    /// Check if all paths from `src` to nodes in `target_set` go through `through_node`. If `src`
442    /// is already in `target_set`, we ignore that empty path.
443    fn all_paths_go_through(
444        &self,
445        src: src::BlockId,
446        through_node: src::BlockId,
447        target_set: &HashSet<src::BlockId>,
448    ) -> bool {
449        let graph = EdgeFiltered::from_fn(&self.fwd_cfg, |edge| edge.source() != through_node);
450        !Dfs::new(&graph, src)
451            .iter(&graph)
452            .skip(1) // skip src
453            .any(|bid| target_set.contains(&bid))
454    }
455}
456
457impl BlockData {
458    fn shortest_paths_including_self(&self) -> impl Iterator<Item = (BlockId, usize)> {
459        self.shortest_paths.iter().map(|(bid, d)| (*bid, *d))
460    }
461    fn shortest_paths_excluding_self(&self) -> impl Iterator<Item = (BlockId, usize)> {
462        self.shortest_paths_including_self()
463            .filter(move |&(bid, _)| bid != self.id)
464    }
465    fn reachable_including_self(&self) -> impl Iterator<Item = BlockId> {
466        self.shortest_paths_including_self().map(|(bid, _)| bid)
467    }
468    fn reachable_excluding_self(&self) -> impl Iterator<Item = BlockId> {
469        self.shortest_paths_excluding_self().map(|(bid, _)| bid)
470    }
471    #[expect(unused)]
472    fn can_reach_excluding_self(&self, other: BlockId) -> bool {
473        self.shortest_paths.contains_key(&other) && self.id != other
474    }
475}
476
477/// See [`ExitInfo::compute_loop_exit_ranks`].
478#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
479struct LoopExitRank {
480    /// Prefer regular blocks over blocks reachable only through unwind edges.
481    is_unwind_block: Reverse<bool>,
482    /// Number of paths we found going to this exit.
483    path_count: usize,
484    /// Distance from the loop header.
485    distance_from_header: Reverse<usize>,
486}
487
488impl ExitInfo {
489    /// Compute the first node on each path that exits the loop.
490    fn compute_loop_exit_starting_points(
491        cfg: &CfgInfo,
492        loop_header: src::BlockId,
493    ) -> Vec<src::BlockId> {
494        let mut loop_exits = Vec::new();
495        // Do a dfs from the loop header while keeping track of the path from the loop header to
496        // the current node.
497        let mut dfs = Dfs::new(&cfg.fwd_cfg, loop_header);
498        while let Some(block_id) = dfs.next(&cfg.fwd_cfg) {
499            // If we've exited all the loops after and including the target one, this node is an
500            // exit node for the target loop.
501            if !cfg.is_within_loop(loop_header, block_id) {
502                loop_exits.push(block_id);
503                // Don't explore any more paths from this node.
504                dfs.discovered.extend(cfg.fwd_cfg.neighbors(block_id));
505            }
506        }
507        loop_exits
508    }
509
510    /// Compute the loop exit candidates along with a rank.
511    ///
512    /// In the simple case, there is one exit node through which all exit paths go. We want to be
513    /// sure to catch that case, and when that's not possible we want to still find a node through
514    /// which a lot of exit paths go.
515    ///
516    /// To do that, we first count for each exit node how many exit paths go through it, and pick
517    /// the node with most occurrences. If there are many such nodes, we pick the one with shortest
518    /// distance from the loop header. Finally if there are still many such nodes, we keep the
519    /// first node found (the order in which we explore the graph is deterministic, and we use an
520    /// insertion-order hash map).
521    ///
522    /// Note that exit candidates will typically be referenced more than once for one loop. This
523    /// comes from the fact that whenever we reach a node outside the current loop, we register
524    /// this node as well as all its children as exit candidates.
525    /// Consider the following example:
526    /// ```text
527    /// while i < max {
528    ///     if cond {
529    ///         break;
530    ///     }
531    ///     s += i;
532    ///     i += 1
533    /// }
534    /// // All the below nodes are exit candidates (each of them is referenced twice)
535    /// s += 1;
536    /// return s;
537    /// ```
538    fn compute_loop_exit_ranks(
539        cfg: &CfgInfo,
540        loop_header: src::BlockId,
541    ) -> SeqHashMap<src::BlockId, LoopExitRank> {
542        let mut loop_exits: SeqHashMap<BlockId, LoopExitRank> = SeqHashMap::new();
543        for block_id in Self::compute_loop_exit_starting_points(cfg, loop_header) {
544            for bid in cfg.block_data(block_id).reachable_including_self() {
545                loop_exits
546                    .entry(bid)
547                    .or_insert_with(|| LoopExitRank {
548                        is_unwind_block: Reverse(cfg.block_data[bid].is_unwind),
549                        path_count: 0,
550                        distance_from_header: Reverse(
551                            cfg.block_data[loop_header].shortest_paths[&bid],
552                        ),
553                    })
554                    .path_count += 1;
555            }
556        }
557        loop_exits
558    }
559
560    /// A loop exit is any block reachable from the loop header that isn't inside the loop.
561    /// This function choses an exit for every loop. See `compute_loop_exit_ranks` for how we
562    /// select them.
563    ///
564    /// For example:
565    /// ```text
566    /// while ... {
567    ///    ...
568    ///    if ... {
569    ///        // We can't reach the loop entry from here: this is an exit
570    ///        // candidate
571    ///        return;
572    ///    }
573    /// }
574    /// // This is another exit candidate - and this is the one we want to use
575    /// // as the "real" exit...
576    /// ...
577    /// ```
578    ///
579    /// Once we listed all the exit candidates, we find the "best" one for every loop. The best
580    /// exit is the following one:
581    /// - it is the one which is used the most times (note that there can be
582    ///   several candidates which are referenced strictly more than once: see the
583    ///   comment below)
584    /// - if several exits have the same number of occurrences, we choose the one
585    ///   for which we goto the "earliest" (earliest meaning that the goto is close to
586    ///   the loop entry node in the AST). The reason is that all the loops should
587    ///   have an outer if ... then ... else ... which executes the loop body or goes
588    ///   to the exit (note that this is not necessarily the first
589    ///   if ... then ... else ... we find: loop conditions can be arbitrary
590    ///   expressions, containing branchings).
591    ///
592    /// # Several candidates for a loop exit:
593    /// =====================================
594    /// There used to be a sanity check to ensure there are no two different
595    /// candidates with exactly the same number of occurrences and distance from
596    /// the entry of the loop, if the number of occurrences is > 1.
597    ///
598    /// We removed it because it does happen, for instance here (the match
599    /// introduces an `unreachable` node, and it has the same number of
600    /// occurrences and the same distance to the loop entry as the `panic`
601    /// node):
602    ///
603    /// ```text
604    /// pub fn list_nth_mut_loop_pair<'a, T>(
605    ///     mut ls: &'a mut List<T>,
606    ///     mut i: u32,
607    /// ) -> &'a mut T {
608    ///     loop {
609    ///         match ls {
610    ///             List::Nil => {
611    ///                 panic!() // <-- best candidate
612    ///             }
613    ///             List::Cons(x, tl) => {
614    ///                 if i == 0 {
615    ///                     return x;
616    ///                 } else {
617    ///                     ls = tl;
618    ///                     i -= 1;
619    ///                 }
620    ///             }
621    ///             _ => {
622    ///               // Note that Rustc always introduces an unreachable branch after
623    ///               // desugaring matches.
624    ///               unreachable!(), // <-- best candidate
625    ///             }
626    ///         }
627    ///     }
628    /// }
629    /// ```
630    ///
631    /// When this happens we choose an exit candidate whose edges don't necessarily
632    /// lead to an error (above there are none, so we don't choose any exits). Note
633    /// that this last condition is important to prevent loops from being unnecessarily
634    /// nested:
635    ///
636    /// ```text
637    /// pub fn nested_loops_enum(step_out: usize, step_in: usize) -> usize {
638    ///     let mut s = 0;
639    ///
640    ///     for _ in 0..128 { // We don't want this loop to be nested with the loops below
641    ///         s += 1;
642    ///     }
643    ///
644    ///     for _ in 0..(step_out) {
645    ///         for _ in 0..(step_in) {
646    ///             s += 1;
647    ///         }
648    ///     }
649    ///
650    ///     s
651    /// }
652    /// ```
653    fn compute_loop_exits(_ctx: &TransformCtx, cfg: &mut CfgInfo) {
654        for &loop_id in &cfg.loop_entries {
655            // Compute the candidates.
656            let loop_exits: SeqHashMap<BlockId, LoopExitRank> =
657                Self::compute_loop_exit_ranks(cfg, loop_id);
658            // We choose the exit with:
659            // - the most occurrences
660            // - the least total distance (if there are several possibilities)
661            // - doesn't necessarily lead to an error (panic, unreachable)
662            let best_exits: Vec<(BlockId, LoopExitRank)> =
663                loop_exits.into_iter().max_set_by_key(|&(_, rank)| rank);
664            // If there is exactly one best candidate, use it. Otherwise we need to split further.
665            let chosen_exit = match best_exits.into_iter().map(|(bid, _)| bid).exactly_one() {
666                Ok(best_exit) => Some(best_exit),
667                Err(best_exits) => {
668                    // Remove the candidates which only lead to errors (panic or unreachable).
669                    // If there is exactly one candidate we select it, otherwise we do not select any
670                    // exit.
671                    // We don't want to select any exit if we are in the below situation
672                    // (all paths lead to errors). We added a sanity check below to
673                    // catch the situations where there are several exits which don't
674                    // lead to errors.
675                    //
676                    // Example:
677                    // ========
678                    // ```
679                    // loop {
680                    //     match ls {
681                    //         List::Nil => {
682                    //             panic!() // <-- best candidate
683                    //         }
684                    //         List::Cons(x, tl) => {
685                    //             if i == 0 {
686                    //                 return x;
687                    //             } else {
688                    //                 ls = tl;
689                    //                 i -= 1;
690                    //             }
691                    //         }
692                    //         _ => {
693                    //           unreachable!(); // <-- best candidate (Rustc introduces an `unreachable` case)
694                    //         }
695                    //     }
696                    // }
697                    // ```
698                    // The `exactly_one` can fail, see `tests/ui/control-flow/ambiguous-loop-exit.rs`
699                    best_exits
700                        .filter(|&bid| !cfg.block_data[bid].only_reach_error)
701                        .exactly_one()
702                        .ok()
703                }
704            };
705            cfg.block_data[loop_id].exit_info.loop_exit = chosen_exit;
706        }
707    }
708
709    /// Let's consider the following piece of code:
710    /// ```text
711    /// if cond1 { ... } else { ... };
712    /// if cond2 { ... } else { ... };
713    /// ```
714    /// Once converted to MIR, the control-flow is destructured, which means we
715    /// have gotos everywhere. When reconstructing the control-flow, we have
716    /// to be careful about the point where we should join the two branches of
717    /// the first if.
718    /// For instance, if we don't notice they should be joined at some point (i.e,
719    /// whatever the branch we take, there is a moment when we go to the exact
720    /// same place, just before the second if), we might generate code like
721    /// this, with some duplicata:
722    /// ```text
723    /// if cond1 { ...; if cond2 { ... } else { ...} }
724    /// else { ...; if cond2 { ... } else { ...} }
725    /// ```
726    ///
727    /// Such a reconstructed program is valid, but it is definitely non-optimal:
728    /// it is very different from the original program (making it less clean and
729    /// clear), more bloated, and might involve duplicating the proof effort.
730    ///
731    /// For this reason, we need to find the "exit" of the first switch, which is
732    /// the point where the two branches join. Note that this can be a bit tricky,
733    /// because there may be more than two branches (if we do `switch(x) { ... }`),
734    /// and some of them might not join (if they contain a `break`, `panic`,
735    /// `return`, etc.).
736    ///
737    /// In order to compute the switch exits, we simply recursively compute a
738    /// topologically ordered set of "filtered successors" as follows (note
739    /// that we work in the CFG *without* back edges):
740    /// - for a block which doesn't branch (only one successor), the filtered
741    ///   successors is the set of reachable nodes.
742    /// - for a block which branches, we compute the nodes reachable from all
743    ///   the children, and find the "best" intersection between those.
744    ///   Note that we find the "best" intersection (a pair of branches which
745    ///   maximize the intersection of filtered successors) because some branches
746    ///   might never join the control-flow of the other branches, if they contain
747    ///   a `break`, `return`, `panic`, etc., like here:
748    ///   ```text
749    ///   if b { x = 3; } { return; }
750    ///   y += x;
751    ///   ...
752    ///   ```
753    /// Note that with nested switches, the branches of the inner switches might
754    /// goto the exits of the outer switches: for this reason, we give precedence
755    /// to the outer switches.
756    fn compute_switch_exits(cfg: &mut CfgInfo) {
757        // We need to give precedence to the outer switches: we thus iterate
758        // over the switch blocks in topological order.
759        let mut exits_set = HashSet::new();
760        for bid in cfg
761            .switch_blocks
762            .iter()
763            .copied()
764            .sorted_unstable_by_key(|&bid| (cfg.topo_rank(bid), bid))
765        {
766            let block_data = &cfg.block_data[bid];
767            // Find the best successor: this is the node with the highest flow, and the lowest
768            // topological rank. If several nodes have the same flow, we want to take the highest
769            // one in the hierarchy: hence the use of the topological rank.
770            //
771            // Ex.:
772            // ```text
773            // A  -- we start here
774            // |
775            // |---------------------------------------
776            // |            |            |            |
777            // B:(0.25,-1)  C:(0.25,-2)  D:(0.25,-3)  E:(0.25,-4)
778            // |            |            |
779            // |--------------------------
780            // |
781            // F:(0.75,-5)
782            // |
783            // |
784            // G:(0.75,-6)
785            // ```
786            // The "best" node (with the highest (flow, rank) in the graph above is F.
787            // If the switch is inside a loop, we also only consider exists that are inside that
788            // same loop. There must be one, otherwise the switch entry would not be inside the
789            // loop.
790            let current_loop = block_data.within_loops.last().copied();
791            let best_exit: Option<BlockId> = block_data
792                .reachable_excluding_self()
793                .filter(|&b| {
794                    current_loop.is_none_or(|current_loop| cfg.is_within_loop(current_loop, b))
795                })
796                .max_by_key(|&id| {
797                    let is_unwind_block = Reverse(cfg.block_data[id].is_unwind);
798                    let flow = &block_data.flow[id];
799                    let rank = Reverse(cfg.topo_rank(id));
800                    ((is_unwind_block, flow, rank), id)
801                });
802            // We have an exit candidate: we first check that it was not already taken by an
803            // external switch.
804            //
805            // We then check that we can't reach the exit of an external switch from one of the
806            // branches, without going through the exit candidate. We do this by simply checking
807            // that we can't reach any of the exits of outer switches.
808            //
809            // The reason is that it can lead to code like the following:
810            // ```
811            // if ... { // if #1
812            //   if ... { // if #2
813            //     ...
814            //     // here, we have a `goto b1`, where b1 is the exit
815            //     // of if #2: we thus stop translating the blocks.
816            //   }
817            //   else {
818            //     ...
819            //     // here, we have a `goto b2`, where b2 is the exit
820            //     // of if #1: we thus stop translating the blocks.
821            //   }
822            //   // We insert code for the block b1 here (which is the exit of
823            //   // the exit of if #2). However, this block should only
824            //   // be executed in the branch "then" of the if #2, not in
825            //   // the branch "else".
826            //   ...
827            // }
828            // else {
829            //   ...
830            // }
831            // ```
832            if let Some(exit_id) = best_exit
833                && !exits_set.contains(&exit_id)
834                && cfg.all_paths_go_through(bid, exit_id, &exits_set)
835            {
836                exits_set.insert(exit_id);
837                cfg.block_data[bid].exit_info.switch_exit = Some(exit_id);
838            }
839        }
840    }
841}
842
843/// Iter over the last non-switch statements that may be executed on any branch of this block.
844/// Skips over `Nop`s.
845fn iter_tail_statements(block: &mut tgt::Block, f: &mut impl FnMut(&mut tgt::Statement)) {
846    let Some(st) = block
847        .statements
848        .iter_mut()
849        .rev()
850        .find(|st| !st.kind.is_nop())
851    else {
852        return;
853    };
854    if let tgt::StatementKind::Switch(switch) = &mut st.kind {
855        for block in switch.iter_targets_mut() {
856            iter_tail_statements(block, f);
857        }
858    } else {
859        f(st)
860    };
861}
862
863type Depth = usize;
864
865#[derive(Debug, Clone, Copy)]
866enum SpecialJumpKind {
867    /// This block can be reached by a `continue` to the given depth.
868    LoopContinue(Depth),
869    /// This block can be reached by a `break` to the given depth. This comes from a loop.
870    LoopBreak(Depth),
871    /// This block can be reached by a `break` to the given depth. This is a `loop` context
872    /// introduced only for forward jumps.
873    ForwardBreak(Depth),
874    /// This block can be reached by doing nothing as this is the next block that will be
875    /// translated. Only applies if this block is at the top of the stack.
876    NextBlock,
877}
878
879#[derive(Clone, Copy)]
880struct SpecialJump {
881    /// The relevant block.
882    target_block: BlockId,
883    /// How to translate a jump to the target block.
884    kind: SpecialJumpKind,
885}
886
887impl SpecialJump {
888    fn new(target_block: BlockId, kind: SpecialJumpKind) -> Self {
889        Self { target_block, kind }
890    }
891}
892
893impl std::fmt::Debug for SpecialJump {
894    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
895        write!(f, "SpecialJump({}, {:?})", self.target_block, self.kind)
896    }
897}
898
899/// How to handle blocks reachable from multiple branches.
900enum ReconstructMode {
901    /// Duplicate blocks reachable from multiple branches.
902    Duplicate,
903    /// Insert loops for the purpose of breaking forward to them to implement DAG-like control-flow
904    /// without duplicating blocks.
905    /// Based on the algorithm from "Beyond Relooper" (https://dl.acm.org/doi/10.1145/3547621).
906    ForwardBreak,
907}
908
909struct ReconstructCtx<'a> {
910    cfg: CfgInfo,
911    body: &'a src::ExprBody,
912    /// The depth of `loop` contexts we may `break`/`continue` to.
913    break_context_depth: Depth,
914    /// Stack of block ids that should be translated to special jumps (`break`/`continue`/do
915    /// nothing) in the current context.
916    /// The block where control-flow continues naturally after this block is kept at the top of the
917    /// stack.
918    special_jump_stack: Vec<SpecialJump>,
919    mode: ReconstructMode,
920}
921
922impl<'a> ReconstructCtx<'a> {
923    fn build(ctx: &TransformCtx, src_body: &'a src::ExprBody) -> Result<Self, Irreducible> {
924        // Compute all sorts of graph-related information about the control-flow graph, including
925        // reachability, the dominator tree, loop entries, and loop/switch exits.
926        let cfg = CfgInfo::build(ctx, &src_body.body)?;
927
928        // Translate the body by reconstructing the loops and the
929        // conditional branchings.
930        let allow_duplication = true;
931        Ok(ReconstructCtx {
932            cfg,
933            body: src_body,
934            break_context_depth: 0,
935            special_jump_stack: Vec::new(),
936            mode: if allow_duplication {
937                ReconstructMode::Duplicate
938            } else {
939                ReconstructMode::ForwardBreak
940            },
941        })
942    }
943
944    fn translate_statement(&self, st: &src::Statement) -> tgt::Statement {
945        let span = st.span;
946        let st = match st.kind.clone() {
947            src::StatementKind::Assign(place, rvalue) => tgt::StatementKind::Assign(place, rvalue),
948            src::StatementKind::SetDiscriminant(place, variant_id) => {
949                tgt::StatementKind::SetDiscriminant(place, variant_id)
950            }
951            src::StatementKind::StorageLive(var_id) => tgt::StatementKind::StorageLive(var_id),
952            src::StatementKind::StorageDead(var_id) => tgt::StatementKind::StorageDead(var_id),
953            src::StatementKind::Assert { assert, on_failure } => tgt::StatementKind::Assert {
954                assert,
955                on_failure: on_failure.clone(),
956                on_unwind: tgt::Statement::new(span, tgt::StatementKind::UnwindResume).into_block(),
957            },
958            src::StatementKind::PlaceMention(place) => tgt::StatementKind::PlaceMention(place),
959            src::StatementKind::Borrowck(statement) => tgt::StatementKind::Borrowck(statement),
960            src::StatementKind::Nop => tgt::StatementKind::Nop,
961        };
962        tgt::Statement::new(span, st)
963    }
964
965    /// Translate a jump to the given block. The span is used to create the jump statement, if any.
966    #[tracing::instrument(skip(self), ret, fields(stack = ?self.special_jump_stack))]
967    fn translate_jump(&mut self, span: Span, target_block: src::BlockId) -> tgt::Block {
968        match self
969            .special_jump_stack
970            .iter()
971            .rev()
972            .enumerate()
973            .find(|(_, j)| j.target_block == target_block)
974        {
975            Some((i, jump_target)) => {
976                let mk_block = |kind| tgt::Statement::new(span, kind).into_block();
977                match jump_target.kind {
978                    // The top of the stack is where control-flow goes naturally, no need to add a
979                    // `break`/`continue`. We only do that in `ForwardBreak` mode to avoid breaking aeneas.
980                    SpecialJumpKind::LoopContinue(_)
981                    | SpecialJumpKind::ForwardBreak(_)
982                    | SpecialJumpKind::NextBlock
983                        if i == 0 && matches!(self.mode, ReconstructMode::ForwardBreak) =>
984                    {
985                        mk_block(tgt::StatementKind::Nop)
986                    }
987                    SpecialJumpKind::LoopContinue(depth) => mk_block(tgt::StatementKind::Continue(
988                        self.break_context_depth - depth,
989                    )),
990                    SpecialJumpKind::ForwardBreak(depth) | SpecialJumpKind::LoopBreak(depth) => {
991                        mk_block(tgt::StatementKind::Break(self.break_context_depth - depth))
992                    }
993                    SpecialJumpKind::NextBlock if i == 0 => mk_block(tgt::StatementKind::Nop),
994                    // Translate the block without a jump.
995                    SpecialJumpKind::NextBlock => self.translate_block(target_block),
996                }
997            }
998            // Translate the block without a jump.
999            None => self.translate_block(target_block),
1000        }
1001    }
1002
1003    fn translate_terminator(&mut self, terminator: &src::Terminator) -> tgt::Block {
1004        let src_span = terminator.span;
1005
1006        match &terminator.kind {
1007            src::TerminatorKind::Abort(kind) => {
1008                tgt::Statement::new(src_span, tgt::StatementKind::Abort(kind.clone())).into_block()
1009            }
1010            src::TerminatorKind::Return => {
1011                tgt::Statement::new(src_span, tgt::StatementKind::Return).into_block()
1012            }
1013            src::TerminatorKind::UnwindResume => {
1014                tgt::Statement::new(src_span, tgt::StatementKind::UnwindResume).into_block()
1015            }
1016            src::TerminatorKind::Call {
1017                call,
1018                target,
1019                on_unwind,
1020            } => {
1021                let on_unwind = self.translate_block(*on_unwind);
1022                let st = tgt::Statement::new(
1023                    src_span,
1024                    tgt::StatementKind::Call {
1025                        call: call.clone(),
1026                        on_unwind,
1027                    },
1028                );
1029                let mut block = self.translate_jump(terminator.span, *target);
1030                block.statements.insert(0, st);
1031                block
1032            }
1033            src::TerminatorKind::Drop {
1034                kind,
1035                place,
1036                fn_ptr,
1037                target,
1038                on_unwind,
1039            } => {
1040                let on_unwind = self.translate_block(*on_unwind);
1041                let st = tgt::Statement::new(
1042                    src_span,
1043                    tgt::StatementKind::Drop {
1044                        place: place.clone(),
1045                        fn_ptr: fn_ptr.clone(),
1046                        kind: *kind,
1047                        on_unwind,
1048                    },
1049                );
1050                let mut block = self.translate_jump(terminator.span, *target);
1051                block.statements.insert(0, st);
1052                block
1053            }
1054            src::TerminatorKind::Assert {
1055                assert,
1056                target,
1057                on_unwind,
1058            } => {
1059                let on_unwind = self.translate_block(*on_unwind);
1060                let st = tgt::StatementKind::Assert {
1061                    assert: assert.clone(),
1062                    on_failure: AbortKind::Panic(None),
1063                    on_unwind,
1064                };
1065                let target = self.translate_jump(terminator.span, *target);
1066                tgt::Statement::new(src_span, st).into_block().merge(target)
1067            }
1068            src::TerminatorKind::InlineAsm {
1069                asm,
1070                targets,
1071                on_unwind,
1072            } => {
1073                let targets = targets
1074                    .iter()
1075                    .map(|target| self.translate_jump(terminator.span, *target))
1076                    .collect();
1077                let on_unwind = self.translate_block(*on_unwind);
1078                let st = tgt::StatementKind::InlineAsm {
1079                    asm: asm.clone(),
1080                    targets,
1081                    on_unwind,
1082                };
1083                tgt::Statement::new(src_span, st).into_block()
1084            }
1085            src::TerminatorKind::Goto { target } => self.translate_jump(terminator.span, *target),
1086            src::TerminatorKind::Switch { discr, targets } => {
1087                // Translate the target expressions
1088                let switch = match &targets {
1089                    src::SwitchTargets::If(then_tgt, else_tgt) => {
1090                        let then_block = self.translate_jump(terminator.span, *then_tgt);
1091                        let else_block = self.translate_jump(terminator.span, *else_tgt);
1092                        tgt::Switch::If(discr.clone(), then_block, else_block)
1093                    }
1094                    src::SwitchTargets::SwitchInt(int_ty, targets, otherwise) => {
1095                        // Note that some branches can be grouped together, like
1096                        // here:
1097                        // ```
1098                        // match e {
1099                        //   E::V1 | E::V2 => ..., // Grouped
1100                        //   E::V3 => ...
1101                        // }
1102                        // ```
1103                        // We detect this by checking if a block has already been
1104                        // translated as one of the branches of the switch.
1105                        //
1106                        // Rk.: note there may be intermediate gotos depending
1107                        // on the MIR we use. Typically, we manage to detect the
1108                        // grouped branches with Optimized MIR, but not with Promoted
1109                        // MIR. See the comment in "tests/src/matches.rs".
1110
1111                        // We link block ids to:
1112                        // - vector of matched integer values
1113                        // - translated blocks
1114                        let mut branches: SeqHashMap<src::BlockId, (Vec<Literal>, tgt::Block)> =
1115                            SeqHashMap::new();
1116
1117                        // Translate the children expressions
1118                        for (v, bid) in targets.iter() {
1119                            // Check if the block has already been translated:
1120                            // if yes, it means we need to group branches
1121                            if branches.contains_key(bid) {
1122                                // Already translated: add the matched value to
1123                                // the list of values
1124                                let branch = branches.get_mut(bid).unwrap();
1125                                branch.0.push(v.clone());
1126                            } else {
1127                                // Not translated: translate it
1128                                let block = self.translate_jump(terminator.span, *bid);
1129                                // We use the terminator span information in case then
1130                                // then statement is `None`
1131                                branches.insert(*bid, (vec![v.clone()], block));
1132                            }
1133                        }
1134                        let targets_blocks: Vec<(Vec<Literal>, tgt::Block)> =
1135                            branches.into_iter().map(|(_, x)| x).collect();
1136
1137                        let otherwise_block = self.translate_jump(terminator.span, *otherwise);
1138
1139                        // Translate
1140                        tgt::Switch::SwitchInt(
1141                            discr.clone(),
1142                            *int_ty,
1143                            targets_blocks,
1144                            otherwise_block,
1145                        )
1146                    }
1147                };
1148
1149                // Return
1150                let span = tgt::combine_switch_targets_span(&switch);
1151                let span = combine_span(&src_span, &span);
1152                let st = tgt::StatementKind::Switch(switch);
1153                tgt::Statement::new(span, st).into_block()
1154            }
1155        }
1156    }
1157
1158    /// Translate just the block statements and terminator.
1159    fn translate_block_itself(&mut self, block_id: BlockId) -> tgt::Block {
1160        let block = &self.body.body[block_id];
1161        // Translate the statements inside the block
1162        let statements = block
1163            .statements
1164            .iter()
1165            .map(|st| self.translate_statement(st))
1166            .collect_vec();
1167        // Translate the terminator.
1168        let terminator = self.translate_terminator(&block.terminator);
1169        // Prepend the statements to the terminator.
1170        if let Some(st) = tgt::Block::from_seq(statements) {
1171            st.merge(terminator)
1172        } else {
1173            terminator
1174        }
1175    }
1176
1177    /// Translate a block including surrounding control-flow like looping.
1178    #[tracing::instrument(skip(self), fields(stack = ?self.special_jump_stack))]
1179    fn translate_block(&mut self, block_id: src::BlockId) -> tgt::Block {
1180        ensure_sufficient_stack(|| self.translate_block_inner(block_id))
1181    }
1182    fn translate_block_inner(&mut self, block_id: src::BlockId) -> tgt::Block {
1183        // Some of the blocks we might jump to inside this tree can't be translated as normal
1184        // blocks: the loop backward edges must become `continue`s and the merge nodes may need
1185        // some care if we're jumping to them from distant locations.
1186        // For this purpose, we push to the `special_jump_stack` the block ids that must be
1187        // translated specially. In `translate_jump` we check the stack. At the end of this
1188        // function we restore the stack to its previous state.
1189        let old_context_depth = self.special_jump_stack.len();
1190        let block_data = &self.cfg.block_data[block_id];
1191        let span = block_data.span;
1192
1193        // Catch jumps to the loop header or loop exit.
1194        if block_data.is_loop_header {
1195            self.break_context_depth += 1;
1196            if let Some(exit_id) = block_data.exit_info.loop_exit {
1197                self.special_jump_stack.push(SpecialJump::new(
1198                    exit_id,
1199                    SpecialJumpKind::LoopBreak(self.break_context_depth),
1200                ));
1201            }
1202            // Put the next block at the top of the stack.
1203            self.special_jump_stack.push(SpecialJump::new(
1204                block_id,
1205                SpecialJumpKind::LoopContinue(self.break_context_depth),
1206            ));
1207        }
1208
1209        // Catch jumps to a merge node.
1210        let merge_children = &block_data.immediately_dominated_merge_targets;
1211        if let ReconstructMode::ForwardBreak = self.mode {
1212            // We support forward-jumps using `break`
1213            // The child with highest postorder numbering is nested outermost in this scheme.
1214            for &child in merge_children {
1215                self.break_context_depth += 1;
1216                self.special_jump_stack.push(SpecialJump::new(
1217                    child,
1218                    SpecialJumpKind::ForwardBreak(self.break_context_depth),
1219                ));
1220            }
1221        }
1222
1223        if let Some(bid) = block_data.exit_info.switch_exit
1224            && !block_data.is_loop_header
1225            && !(matches!(self.mode, ReconstructMode::ForwardBreak)
1226                && merge_children.contains(&bid))
1227        {
1228            // Move some code that would be inside one or several switch branches to be after the
1229            // switch intead.
1230            self.special_jump_stack
1231                .push(SpecialJump::new(bid, SpecialJumpKind::NextBlock));
1232        }
1233
1234        // Translate this block. Any jumps to a loop header or a merge node will be replaced with
1235        // `continue`/`break`.
1236        let mut block = self.translate_block_itself(block_id);
1237
1238        // Reset the state to what it was previously, and translate what remains.
1239        let new_statement = move |kind| tgt::Statement::new(block.span, kind);
1240        while self.special_jump_stack.len() > old_context_depth {
1241            let special_jump = self.special_jump_stack.pop().unwrap();
1242            match &special_jump.kind {
1243                SpecialJumpKind::LoopContinue(_) => {
1244                    self.break_context_depth -= 1;
1245                    if let ReconstructMode::ForwardBreak = self.mode {
1246                        // We add `continue` at the end for users that don't know that the default
1247                        // behavior at the end of a loop block is `continue`. Not needed for
1248                        // `Duplicate` mode because we use explicit `continue`s there. TODO: clean
1249                        // that up.
1250                        block
1251                            .statements
1252                            .push(new_statement(tgt::StatementKind::Continue(0)));
1253                    }
1254                    block = new_statement(tgt::StatementKind::Loop(block)).into_block();
1255                }
1256                SpecialJumpKind::ForwardBreak(_) => {
1257                    self.break_context_depth -= 1;
1258                    // Remove unneeded `break`s in branches leading up to that final one.
1259                    iter_tail_statements(&mut block, &mut |st| {
1260                        if matches!(st.kind, tgt::StatementKind::Break(0)) {
1261                            st.kind = tgt::StatementKind::Nop;
1262                        }
1263                    });
1264                    // We add a `loop { ...; break }` so that we can use `break` to jump forward.
1265                    block
1266                        .statements
1267                        .push(new_statement(tgt::StatementKind::Break(0)));
1268                    block = new_statement(tgt::StatementKind::Loop(block)).into_block();
1269                    // We must translate the merge nodes after the block used for forward jumps to
1270                    // them.
1271                    let next_block = self.translate_jump(span, special_jump.target_block);
1272                    block = block.merge(next_block);
1273                }
1274                SpecialJumpKind::NextBlock | SpecialJumpKind::LoopBreak(..) => {
1275                    let next_block = self.translate_jump(span, special_jump.target_block);
1276                    block = block.merge(next_block);
1277                }
1278            }
1279        }
1280        block
1281    }
1282}
1283
1284fn remove_useless_jump_blocks(body: &mut tgt::ExprBody) {
1285    use tgt::StatementKind;
1286    #[derive(Default)]
1287    struct Count {
1288        continue_count: u32,
1289        break_count: u32,
1290    }
1291    #[derive(Default, Visitor)]
1292    struct CountJumpsVisitor {
1293        counts: HashMap<StatementId, Count>,
1294        loop_stack: Vec<StatementId>,
1295    }
1296    #[derive(Visitor)]
1297    struct RemoveUselessJumpsVisitor {
1298        counts: HashMap<StatementId, Count>,
1299        /// For every loop we encounter, whether we're keeping it or removing it.
1300        loop_stack: Vec<bool>,
1301    }
1302
1303    impl VisitBodyMut for CountJumpsVisitor {
1304        fn visit_llbc_statement(&mut self, st: &mut tgt::Statement) -> ControlFlow<Self::Break> {
1305            if let StatementKind::Loop(_) = &st.kind {
1306                self.loop_stack.push(st.id);
1307            }
1308            match &st.kind {
1309                StatementKind::Break(depth) => {
1310                    let loop_id = self.loop_stack[self.loop_stack.len() - 1 - depth];
1311                    self.counts.entry(loop_id).or_default().break_count += 1;
1312                }
1313                StatementKind::Continue(depth) => {
1314                    let loop_id = self.loop_stack[self.loop_stack.len() - 1 - depth];
1315                    self.counts.entry(loop_id).or_default().continue_count += 1;
1316                }
1317                _ => {}
1318            }
1319            self.visit_inner(st)?;
1320            if let StatementKind::Loop(_) = &st.kind {
1321                self.loop_stack.pop();
1322            }
1323            ControlFlow::Continue(())
1324        }
1325    }
1326
1327    impl VisitBodyMut for RemoveUselessJumpsVisitor {
1328        fn visit_llbc_block(&mut self, block: &mut tgt::Block) -> ControlFlow<Self::Break> {
1329            for mut st in mem::take(&mut block.statements) {
1330                if let tgt::StatementKind::Loop(block) = &mut st.kind {
1331                    let counts = &self.counts[&st.id];
1332                    let remove = counts.continue_count == 0
1333                        && counts.break_count == 1
1334                        && matches!(
1335                            block.statements.last().unwrap().kind,
1336                            StatementKind::Break(0)
1337                        );
1338                    self.loop_stack.push(!remove);
1339                }
1340                self.visit(&mut st)?;
1341                if st.kind.is_loop() && !self.loop_stack.pop().unwrap() {
1342                    // Remove the loop.
1343                    let StatementKind::Loop(mut inner_block) = st.kind else {
1344                        unreachable!()
1345                    };
1346                    inner_block.statements.last_mut().unwrap().kind = StatementKind::Nop;
1347                    block.statements.extend(inner_block.statements);
1348                } else {
1349                    block.statements.push(st);
1350                }
1351            }
1352            ControlFlow::Continue(())
1353        }
1354        fn enter_llbc_statement(&mut self, st: &mut tgt::Statement) {
1355            match &st.kind {
1356                StatementKind::Break(depth) => {
1357                    let new_depth = self.loop_stack[self.loop_stack.len() - depth..]
1358                        .iter()
1359                        .filter(|&&keep| keep)
1360                        .count();
1361                    st.kind = StatementKind::Break(new_depth);
1362                }
1363                StatementKind::Continue(depth) => {
1364                    let new_depth = self.loop_stack[self.loop_stack.len() - depth..]
1365                        .iter()
1366                        .filter(|&&keep| keep)
1367                        .count();
1368                    st.kind = StatementKind::Continue(new_depth);
1369                }
1370                _ => {}
1371            }
1372        }
1373    }
1374
1375    let mut v = CountJumpsVisitor::default();
1376    body.body.drive_body_mut(&mut v);
1377    let mut v = RemoveUselessJumpsVisitor {
1378        counts: v.counts,
1379        loop_stack: Default::default(),
1380    };
1381    body.body.drive_body_mut(&mut v);
1382}
1383
1384fn translate_body(ctx: &mut TransformCtx, body: &mut gast::Body) {
1385    use gast::Body::{Structured, Unstructured};
1386    let Unstructured(src_body) = body else {
1387        panic!("Called `ullbc_to_llbc` on an already restructured body")
1388    };
1389    trace!("About to translate to ullbc: {:?}", src_body.span);
1390
1391    // Calculate info about the graph and heuristically determine loop and switch exit blocks.
1392    let start_block = BlockId::ZERO;
1393    let mut ctx = match ReconstructCtx::build(ctx, src_body) {
1394        Ok(ctx) => ctx,
1395        Err(Irreducible(bid)) => {
1396            let span = src_body.body[bid].terminator.span;
1397            register_error!(
1398                ctx,
1399                span,
1400                "the control-flow graph of this function is not reducible"
1401            );
1402            panic!("can't reconstruct irreducible control-flow")
1403        }
1404    };
1405    // Translate the blocks using the computed data.
1406    let tgt_body = ctx.translate_block(start_block);
1407
1408    let mut tgt_body = tgt::ExprBody {
1409        span: src_body.span,
1410        locals: src_body.locals.clone(),
1411        bound_body_regions: src_body.bound_body_regions,
1412        body: tgt_body,
1413        comments: src_body.comments.clone(),
1414    };
1415    remove_useless_jump_blocks(&mut tgt_body);
1416
1417    *body = Structured(tgt_body);
1418}
1419
1420pub struct Transform;
1421impl TransformPass for Transform {
1422    fn transform_ctx(&self, ctx: &mut TransformCtx) {
1423        // Translate the bodies one at a time.
1424        ctx.for_each_body(|ctx, body| {
1425            translate_body(ctx, body);
1426        });
1427
1428        if ctx.options.print_built_llbc {
1429            eprintln!("# LLBC resulting from control-flow reconstruction:\n\n{ctx}\n",);
1430        } else {
1431            trace!("# LLBC resulting from control-flow reconstruction:\n\n{ctx}\n",);
1432        }
1433    }
1434}