regalloc2/ion/
dump.rs

1//! Debugging output.
2
3use alloc::string::ToString;
4use alloc::{format, vec};
5use alloc::{string::String, vec::Vec};
6
7use super::Env;
8use crate::{Block, Function, ProgPoint};
9
10impl<'a, F: Function> Env<'a, F> {
11    pub fn dump_state(&self) {
12        trace!("Bundles:");
13        for (i, b) in self.bundles.iter().enumerate() {
14            trace!(
15                "bundle{}: spillset={:?} alloc={:?}",
16                i,
17                b.spillset,
18                b.allocation
19            );
20            for entry in &b.ranges {
21                trace!(
22                    " * range {:?} -- {:?}: range{}",
23                    entry.range.from,
24                    entry.range.to,
25                    entry.index.index()
26                );
27            }
28        }
29        trace!("VRegs:");
30        for (i, v) in self.vregs.iter().enumerate() {
31            trace!("vreg{}:", i);
32            for entry in &v.ranges {
33                trace!(
34                    " * range {:?} -- {:?}: range{}",
35                    entry.range.from,
36                    entry.range.to,
37                    entry.index.index()
38                );
39            }
40        }
41        trace!("Ranges:");
42        for (i, r) in self.ranges.iter().enumerate() {
43            trace!(
44                "range{}: range={:?} vreg={:?} bundle={:?} weight={:?}",
45                i,
46                r.range,
47                r.vreg,
48                r.bundle,
49                r.uses_spill_weight(),
50            );
51            for u in &r.uses {
52                trace!(" * use at {:?} (slot {}): {:?}", u.pos, u.slot, u.operand);
53            }
54        }
55    }
56
57    pub fn annotate(&mut self, progpoint: ProgPoint, s: String) {
58        if self.annotations_enabled {
59            self.debug_annotations
60                .entry(progpoint)
61                .or_insert_with(|| vec![])
62                .push(s);
63        }
64    }
65
66    pub fn dump_results(&self) {
67        log::info!("=== REGALLOC RESULTS ===");
68        for block in 0..self.func.num_blocks() {
69            let block = Block::new(block);
70            log::info!(
71                "block{}: [succs {:?} preds {:?}]",
72                block.index(),
73                self.func
74                    .block_succs(block)
75                    .iter()
76                    .map(|b| b.index())
77                    .collect::<Vec<_>>(),
78                self.func
79                    .block_preds(block)
80                    .iter()
81                    .map(|b| b.index())
82                    .collect::<Vec<_>>()
83            );
84            for inst in self.func.block_insns(block).iter() {
85                for annotation in self
86                    .debug_annotations
87                    .get(&ProgPoint::before(inst))
88                    .map(|v| &v[..])
89                    .unwrap_or(&[])
90                {
91                    log::info!("  inst{}-pre: {}", inst.index(), annotation);
92                }
93                let ops = self
94                    .func
95                    .inst_operands(inst)
96                    .iter()
97                    .map(|op| format!("{}", op))
98                    .collect::<Vec<_>>();
99                let clobbers = self
100                    .func
101                    .inst_clobbers(inst)
102                    .into_iter()
103                    .map(|preg| format!("{}", preg))
104                    .collect::<Vec<_>>();
105                let allocs = (0..ops.len())
106                    .map(|i| format!("{}", self.get_alloc(inst, i)))
107                    .collect::<Vec<_>>();
108                let opname = if self.func.is_branch(inst) {
109                    "br"
110                } else if self.func.is_ret(inst) {
111                    "ret"
112                } else {
113                    "op"
114                };
115                let args = ops
116                    .iter()
117                    .zip(allocs.iter())
118                    .map(|(op, alloc)| format!("{} [{}]", op, alloc))
119                    .collect::<Vec<_>>();
120                let clobbers = if clobbers.is_empty() {
121                    "".to_string()
122                } else {
123                    format!(" [clobber: {}]", clobbers.join(", "))
124                };
125                log::info!(
126                    "  inst{}: {} {}{}",
127                    inst.index(),
128                    opname,
129                    args.join(", "),
130                    clobbers
131                );
132                for annotation in self
133                    .debug_annotations
134                    .get(&ProgPoint::after(inst))
135                    .map(|v| &v[..])
136                    .unwrap_or(&[])
137                {
138                    log::info!("  inst{}-post: {}", inst.index(), annotation);
139                }
140            }
141        }
142    }
143}