rucc_codegen/split.rs
1//! Splitting critical edges, so that every edge that carries values has somewhere to put them.
2//!
3//! Design: `spec/10-backend.md` section 10.4.
4//!
5//! An edge carries values when the block it goes to takes parameters, and giving a parameter its
6//! value is a move. The move has to happen on the edge and not before it or after it, because
7//! before it is a block that goes somewhere else too and after it is a block that is arrived at
8//! from somewhere else too, and in either case the move would run on a path it was not written
9//! for. An edge out of a block with one successor can put its moves at the end of that block,
10//! since every path through it takes the edge. An edge into a block with one predecessor can put
11//! them at the start of that block, for the same reason the other way round. An edge that is
12//! neither, which is what a critical edge is, has neither place, and the allocator says so:
13//! `rucc_regalloc` asserts that it never sees one.
14//!
15//! So one is turned into two. A block with nothing in it goes on the edge, the arguments move on
16//! to the second half, and both halves are now uncritical: the first goes to a block with one
17//! predecessor and the second leaves a block with one successor. Which of the two the moves end
18//! up in is the allocator's answer and not this one's, and either is correct.
19//!
20//! # What it leaves behind
21//!
22//! An empty block, which is a jump to the next thing unless the layout puts it where it falls
23//! through. That is a cost, and it is why an edge with nothing to carry is left alone: there are
24//! no moves to find a place for, so splitting it would buy a jump and nothing else.
25
26use rucc_mir as mir;
27
28/// Splits every critical edge that carries values, and gives back how many it split.
29///
30/// Run after lowering and before allocation. Running it twice is running it once, because the
31/// blocks it adds have one successor each and are never the source of a critical edge.
32pub fn critical(func: &mut mir::Func) -> usize {
33 let preds = preds(func);
34 let blocks: Vec<mir::Block> = func.blocks().collect();
35 let mut split = 0;
36 for block in blocks {
37 if func[block].succs.len() < 2 {
38 continue;
39 }
40 for index in 0..func[block].succs.len() {
41 let call = func[block].succs[index].clone();
42 if call.args.is_empty() || preds[call.block.index()] < 2 {
43 continue;
44 }
45 // The new block is at the end of the layout, which is where a block that is a jump
46 // and nothing else does the least harm before the layout pass has an opinion.
47 let half = func.create_block();
48 *func.succs_mut(half) = vec![call];
49 func.succs_mut(block)[index] = mir::BlockCall::to(half);
50 split += 1;
51 }
52 }
53 split
54}
55
56/// How many edges arrive at each block, counted by index rather than in layout order so that a
57/// block added while splitting can be looked up in the same table.
58fn preds(func: &mir::Func) -> Vec<usize> {
59 let mut counts = vec![0; func.block_count()];
60 for block in func.blocks() {
61 for call in &func[block].succs {
62 counts[call.block.index()] += 1;
63 }
64 }
65 counts
66}
67
68#[cfg(test)]
69mod tests {
70 use rucc_base::Interner;
71 use rucc_target::x86_64::{GPR, REGS};
72
73 use super::*;
74
75 /// A diamond: one block that goes two ways and one block both ways arrive at, with as many
76 /// parameters on the block they arrive at as the test asks for.
77 fn diamond(params: usize) -> (Interner, mir::Func, [mir::Block; 4]) {
78 let mut names = Interner::new();
79 let mut func = mir::Func::new(names.intern("f"));
80 let head = func.create_block();
81 let left = func.create_block();
82 let right = func.create_block();
83 let join = func.create_block();
84 // The values arrive in the head, so that they have somewhere to be defined and the
85 // printer has a name for them. Nothing here runs an allocator, which is the one thing
86 // that would object to a first block with parameters.
87 let args: Vec<mir::Reg> = (0..params).map(|_| func.append_param(head, GPR)).collect();
88 for _ in 0..params {
89 func.append_param(join, GPR);
90 }
91 *func.succs_mut(head) = vec![mir::BlockCall::to(left), mir::BlockCall::to(right)];
92 *func.succs_mut(left) = vec![mir::BlockCall { block: join, args: args.clone() }];
93 *func.succs_mut(right) = vec![mir::BlockCall { block: join, args }];
94 (names, func, [head, left, right, join])
95 }
96
97 /// Where each block goes, which is the whole of what this changes.
98 fn edges(func: &mir::Func) -> Vec<Vec<usize>> {
99 func.blocks()
100 .map(|block| func[block].succs.iter().map(|call| call.block.index()).collect())
101 .collect()
102 }
103
104 #[test]
105 fn an_edge_that_is_the_only_way_out_is_left_alone() {
106 let (_, mut func, _) = diamond(1);
107 // The two edges into the join carry a value each and neither is critical, because the
108 // block each leaves goes nowhere else.
109 assert_eq!(critical(&mut func), 0);
110 assert_eq!(edges(&func), vec![vec![1, 2], vec![3], vec![3], vec![]]);
111 }
112
113 #[test]
114 fn a_critical_edge_carrying_a_value_is_split_in_two() {
115 let (_, mut func, [head, _, _, join]) = diamond(1);
116 // Now the head goes straight to the join as well, so both of its arms are critical: it
117 // has two ways out and the join has three ways in.
118 let arg = func.append_param(head, GPR);
119 func.succs_mut(head).push(mir::BlockCall { block: join, args: vec![arg] });
120 func.succs_mut(head).swap(1, 2);
121
122 assert_eq!(critical(&mut func), 1);
123 assert_eq!(
124 edges(&func),
125 // The head's second arm is the new block and the new block goes to the join. The
126 // other two arms are untouched, because each goes to a block with one way in.
127 vec![vec![1, 4, 2], vec![3], vec![3], vec![], vec![3]]
128 );
129 }
130
131 #[test]
132 fn a_critical_edge_carrying_nothing_is_left_alone() {
133 let (_, mut func, [head, _, _, join]) = diamond(0);
134 func.succs_mut(head).push(mir::BlockCall::to(join));
135
136 // Critical and not split, because there is no move to find a place for and a block that
137 // is a jump and nothing else is worth more than nothing.
138 assert_eq!(critical(&mut func), 0);
139 }
140
141 #[test]
142 fn the_arguments_move_on_to_the_half_that_arrives() {
143 let (names, mut func, [head, _, _, join]) = diamond(1);
144 let arg = func.append_param(head, GPR);
145 func.succs_mut(head).push(mir::BlockCall { block: join, args: vec![arg] });
146
147 assert_eq!(critical(&mut func), 1);
148 // What the first half carries is nothing, since the block it goes to asks for nothing,
149 // and what the second half carries is what the whole edge used to.
150 let half = func.blocks().last().expect("the block the split added");
151 assert_eq!(func[head].succs[2].args, Vec::new());
152 assert_eq!(func[half].succs[0].args, vec![arg]);
153 assert_eq!(
154 mir::print_func(&func, &names, ®S),
155 "mfunc @f {\nblock0(%0:gpr, %1:gpr):\n block1, block2, block4\n\n\
156 block1:\n block3(%0)\n\nblock2:\n block3(%0)\n\n\
157 block3(%2:gpr):\n\nblock4:\n block3(%1)\n}\n"
158 );
159 }
160
161 #[test]
162 fn splitting_twice_is_splitting_once() {
163 let (_, mut func, [head, _, _, join]) = diamond(1);
164 let arg = func.append_param(head, GPR);
165 func.succs_mut(head).push(mir::BlockCall { block: join, args: vec![arg] });
166
167 assert_eq!(critical(&mut func), 1);
168 assert_eq!(critical(&mut func), 0);
169 }
170}