Skip to main content

rucc_opt/
simplify_cfg.rs

1//! Control flow simplification: unreachable blocks go, a branch that only ever goes one way
2//! becomes a jump, a block that does nothing but jump somewhere else stops being in the way, a
3//! block parameter that is the same value on every way in stops being a parameter, and a block
4//! with one way in is folded into the block above it.
5//!
6//! Design: `spec/optimizer/21-cfg-simplification.md`, and section 6.5 of
7//! `spec/optimizer/06-cfg-and-dominators.md`, which states the rule for the whole optimizer, that
8//! a block the entry does not reach is invisible to every analysis and is deleted here rather than
9//! by whichever pass happened to notice it.
10//!
11//! # The order
12//!
13//! Section 21.4, and it is an order rather than a loop. Unreachable removal, then the branches,
14//! then the straightening, then merging, each once. Running the four to a fixed point would cost a
15//! walk of the function for every pass over it and buy back a case nobody has: what merging leaves
16//! behind is a bigger block, and a bigger block does not make a branch foldable that was not
17//! foldable before. The pipeline runs this pass more than once anyway, so the second chance is a
18//! pass boundary away rather than a loop away, and that is a chance the pass manager can count and
19//! print.
20//!
21//! Step three is the exception, and it is the spec's exception rather than one taken here. Taking a
22//! forwarder out gives the block below it a way in it did not have, which can be the way in that
23//! makes one of its parameters the same value from everywhere; and taking a parameter away can be
24//! what leaves a block empty enough to be a forwarder. So the two run together on one worklist,
25//! which is a fixed point over a step and not over the pass.
26//!
27//! # The parameter nothing reads
28//!
29//! Section 21.2 is about a parameter that is the same value every way in, and there is a second
30//! kind that goes, which is one nothing reads at all. `crate::dce` is the pass that removes what
31//! nothing reads and it says in its own documentation why this one is not its job: a count driven
32//! to zero does not see a loop counter, because the counter's only reader is the addition that
33//! produces the value handed back to the counter. The count never reaches zero and the whole cycle
34//! is dead anyway.
35//!
36//! So it is answered the other way round, by asking what is live rather than what is dead.
37//! Something is live if an instruction that has to happen reads it, and then live spreads: the
38//! operands of a live instruction are live, and the argument every edge passes in a live
39//! parameter's place is live. Anything the spread does not reach is not read by anything that
40//! happens, and a parameter it does not reach goes along with the argument in its place on every
41//! edge into the block. What that strands is an addition whose result nobody wants any more, which
42//! is exactly the shape `crate::dce` was already good at.
43//!
44//! Starting from nothing live rather than from everything live is what breaks the cycle, and it is
45//! the same optimistic reading section 14.1 takes and section 21.2 takes one paragraph up. The
46//! risk in reading optimistically is claiming something is dead when it is not, so the seeds are
47//! generous: a terminator or an instruction with effects makes its operands live whatever else is
48//! true, and the entry block's parameters are the function's own and stay whether anybody reads
49//! them or not.
50//!
51//! `crate::ivopts` is what makes this worth having. Section 28.4 of
52//! `spec/optimizer/28-induction-variables.md` has a loop stop asking its counter anything, and
53//! before this the counter went on being incremented round a loop that had no other use for it.
54//!
55//! What the addition is left reading matters, and this used to leave it reading the parameter that
56//! had just gone. The argument for that was that the addition is dead and `crate::dce` is the pass
57//! for what is dead, and the argument is true at every level except the one where it counts:
58//! `-O0` runs this pass and nothing else, so nothing came along behind it and the operand reached
59//! the printer as a use with no definition. So the addition goes here, with the parameter that
60//! stranded it, and the `strand` function below carries the argument for why that is always safe.
61//!
62//! Cross jumping is the one transformation of section 21.1 that is not here at all, and that is
63//! section 21.1's last paragraph telling us not to: it costs a branch to save a copy, so it belongs
64//! at the machine level under `-Os`, which is document 37.
65//!
66//! # What a forwarder is allowed to be
67//!
68//! Section 21.1 wants four things of a block before its predecessors are pointed past it: one
69//! successor, the successor is not the exit, the successor is not the block itself, and the edge
70//! out is not abnormal. Then it adds the one the block parameter form needs, which is that the
71//! arguments the block passes on all dominate every predecessor of it, because those predecessors
72//! are the ones that will be passing them.
73//!
74//! Requiring the block to have no parameters of its own discharges that last one without a
75//! dominator tree, and the argument is short. A value defined in a block that dominates the
76//! forwarder dominates every predecessor of it too: take any path to a predecessor, follow the edge
77//! to the forwarder, and the definition is somewhere on the result, which is either before the
78//! predecessor or is the forwarder itself. A block with no parameters and no instructions defines
79//! nothing, so the second case cannot arise and the first is the condition.
80//!
81//! The requirement earns something else as well. A parameter of the forwarder could be read by a
82//! block below it, which is legal exactly when the forwarder dominates that block, and pointing the
83//! predecessors past would leave that read with nothing to read. Insisting on no parameters is one
84//! rule that answers both, and a forwarder that has one gets taken apart by the other half of the
85//! worklist first.
86//!
87//! There is no exit block in this IR, so the second condition is not a condition. Abnormal edges
88//! are the ones into a block whose address is taken, which arrive from an `indirect_br` the graph
89//! reads from the other end, and those blocks are refused here the same way they are refused
90//! everywhere else in this pass.
91//!
92//! One condition is here that the section does not ask for. A forwarder that passes arguments on,
93//! and that is arrived at from a block which branches, is the block the moves for those arguments
94//! go in. Take it out and the edge it was on becomes one that goes out of a block with two ways out
95//! and into a block with two ways in, which has no end of a block to put a move at, so the back end
96//! splits it and puts an empty block back. The block comes back at the end of the layout instead of
97//! where it was, the jump that was free because it fell through is a jump that is taken, and the
98//! value the move was carrying is live across more of the function. On the corpus at -O2 that costs
99//! more than the block is worth, so a forwarder in that position stays. A forwarder that carries
100//! nothing is taken out whatever the edges look like, since there is no move to find a place for.
101//!
102//! # Why this is not only an optimization
103//!
104//! Issue 359 is a program that does not link:
105//!
106//! ```c
107//! extern void link_error(void);
108//! void foo(int x) {
109//!     switch (x) {
110//!     case 0:
111//!         if (0) { link_error(); case 1: bar(); }
112//!     }
113//! }
114//! ```
115//!
116//! Nothing calls `link_error`, so a compiler that emits the call produces an object file that
117//! does not link, and the difference between the two compilers is not how fast the program runs.
118//! The file is in a suite of forty years of compiler bugs for the reason the `case 1:` is where
119//! it is: control does reach `bar` through the switch, and it reaches it from inside the body of
120//! the dead `if`. A compiler that deletes the compound statement gets this as wrong as one that
121//! keeps all of it.
122//!
123//! Doing it in two steps is what makes that come out right without a special case for it. The
124//! branch on the constant becomes a jump, which takes the edge into the dead arm away, and then
125//! reachability from the entry decides what is left. The block holding `bar` has an edge from the
126//! `switch` and stays. The block holding `link_error` has no edges at all and goes.
127//!
128//! # The condition it can read
129//!
130//! A constant, and a comparison of two constants. The second is here rather than in
131//! [`crate::fold`] because folding a comparison would produce an `i1` standing on its own, which
132//! is issue 352 and does not lower, so the pass that folds arithmetic deliberately leaves
133//! comparisons alone. Reading one to decide which way a branch goes produces no `i1` at all: the
134//! comparison is left exactly where it was, used by nothing, and [`crate::dce`] takes it out.
135//!
136//! # Fuel
137//!
138//! Fuel is charged for each branch that folds and for each block that is merged away, and not for
139//! the blocks that go because nothing reaches them. Removal is the second half of the
140//! transformation that was already paid for rather than a transformation of its own, and a fuel
141//! limit that could stop between the two halves would hand the verifier a block nothing reaches.
142//! Section 41.5 of `spec/optimizer/41-correctness.md` asks for fuel that is monotonic, which means
143//! each step being all of one change and not part of one.
144//!
145//! That reasoning covers the blocks a fold stranded. It does not cover the ones that arrived
146//! unreachable, and those are not charged for either, for a different reason: section 6.5 makes
147//! removing them this pass's standing obligation rather than an optimization, everything below
148//! reads the graph as though they are not there, and a bisection that turned the obligation off
149//! would be bisecting over a function the rest of the optimizer does not believe in.
150
151use std::collections::{HashMap, HashSet, VecDeque};
152
153use rucc_base::Idx;
154use rucc_ir::{Block, BlockCall, Def, Extra, Func, Inst, Opcode, Value};
155
156use crate::fold::constant;
157use crate::{Analyses, Fuel, Pass, Preserved, Stats, uses};
158
159/// Recorded once for each branch that turned into a jump.
160const FOLDED: &str = "branch on a condition that is always the same way replaced by a jump";
161
162/// Recorded once for each block that went with it.
163pub(crate) const REMOVED: &str = "block nothing reaches removed";
164
165/// Recorded once for each block folded into the one above it.
166const MERGED: &str = "block with one way into it merged into the block above it";
167
168/// Recorded once for each block that did nothing but jump and is no longer in the way.
169const FORWARDED: &str = "block that only jumped somewhere else removed and its edges pointed past";
170
171/// Recorded once for each block parameter that turned out to be one value.
172const SAME_EVERY_WAY: &str = "block parameter that arrives as the same value every way in removed";
173
174/// Recorded for a branch that would have folded if there had been fuel for it.
175const NO_FUEL: &str = "branch on a known condition left alone, the pass ran out of fuel";
176
177/// Recorded for a block that would have been merged if there had been fuel for it.
178const NO_FUEL_MERGE: &str = "block with one way into it left alone, the pass ran out of fuel";
179
180/// Recorded for a forwarder that would have gone if there had been fuel for it.
181const NO_FUEL_FORWARD: &str =
182    "block that only jumped somewhere else kept, the pass ran out of fuel";
183
184/// Recorded for a block parameter that would have gone if there had been fuel for it.
185const NO_FUEL_PARAM: &str = "block parameter that is one value kept, the pass ran out of fuel";
186
187/// Recorded once for each block parameter that nothing turned out to read.
188const NOTHING_READS_IT: &str = "block parameter nothing reads removed, and the argument on every \
189                                edge that was feeding it";
190
191/// Recorded for a parameter nothing reads that would have gone if there had been fuel for it.
192const NO_FUEL_UNREAD: &str = "block parameter nothing reads kept, the pass ran out of fuel";
193
194/// The pass.
195#[derive(Debug, Clone, Copy, PartialEq, Eq)]
196pub struct SimplifyCfg;
197
198impl Pass for SimplifyCfg {
199    fn name(&self) -> &'static str {
200        "simplify-cfg"
201    }
202
203    fn describe(&self) -> &'static str {
204        "unreachable blocks go, a branch that only goes one way becomes a jump, a block that only \
205         jumps stops being in the way, and a block with one way in is merged into the one above it"
206    }
207
208    fn preserves(&self) -> Preserved {
209        // Nothing at all, and this is the pass the declaration exists for. An edge moves, so the
210        // graph is a different graph, and everything built on the graph was about the old one.
211        Preserved::NONE
212    }
213
214    fn run(&self, func: &mut Func, an: &mut Analyses, fuel: &mut Fuel) -> Stats {
215        let mut stats = Stats::new();
216        // Step one, and it is first for a reason beyond tidiness: a branch in a block nothing
217        // reaches is a branch nothing executes, and folding one would spend fuel on a change
218        // nobody can see and charge the two steps below for walking blocks that are not there.
219        sweep(func, an, &mut stats);
220        let mut folded = false;
221        // Nothing bound, because this step asks where a branch goes whichever way control arrived
222        // at it. Binding a block's parameters to one edge's arguments is the question
223        // [`crate::thread`] asks, and it is a different question with a different answer.
224        let unbound = Bindings::new();
225        for block in func.blocks().collect::<Vec<Block>>() {
226            let Some(term) = func.terminator(block) else { continue };
227            let Some(taken) = taken(func, term, &unbound) else { continue };
228            if !fuel.take() {
229                // Out of fuel stops the transforming and not the looking, the same way the other
230                // passes treat it, so that the walk is the same walk at every fuel setting.
231                stats.missed(NO_FUEL);
232                continue;
233            }
234            jump_to(func, term, taken);
235            stats.optimized(FOLDED);
236            folded = true;
237        }
238        if folded {
239            // The second sweep section 21.4 folds into step two. The cache is holding answers
240            // about the function as it was a moment ago, and the manager clears it after the pass
241            // returns, which is too late for the pass itself.
242            an.clear();
243            sweep(func, an, &mut stats);
244        }
245        let mut forward = HashMap::new();
246        // Step three, and it keeps its own record of the edges rather than asking for the graph,
247        // because it changes the edges as it goes and a cached answer would be about the shape the
248        // function had one forwarder ago. The parameters nothing reads go first, for the reason
249        // this module's documentation gives, which is that a block they leave empty is a forwarder
250        // and the worklist below is what takes forwarders out.
251        let dropped = drop_unread(func, fuel, &mut stats);
252        if straighten(func, fuel, &mut stats, &mut forward) || dropped {
253            an.clear();
254        }
255        // Merging reads which blocks have one predecessor, so it has to run on the graph as it is
256        // after the stranded ones have gone. A block kept alive only by an edge from a block
257        // nothing reaches looks like it has two ways in until that block is out of the function.
258        for chain in chains(func, an) {
259            for (at, &block) in chain.iter().enumerate().skip(1) {
260                if !fuel.take() {
261                    // The rest of the chain goes with it. A block is merged into the one at the
262                    // head of its chain, and it can only get there once everything between them
263                    // has already arrived.
264                    for _ in at..chain.len() {
265                        stats.missed(NO_FUEL_MERGE);
266                    }
267                    break;
268                }
269                merge(func, chain[0], block, &mut forward);
270                stats.optimized(MERGED);
271            }
272        }
273        if !forward.is_empty() {
274            // Once, for every parameter every merge bound, rather than a walk of the function per
275            // block merged.
276            uses::substitute(func, &forward);
277        }
278        stats
279    }
280}
281
282/// What a block's parameters hold along one particular edge into it.
283///
284/// Empty is the honest answer for a question asked about a block rather than about an edge, and it
285/// is what this pass passes, since a branch it folds has to fold whichever way control arrived.
286/// [`crate::thread`] asks the same question one edge at a time and fills this in, which is the
287/// whole difference between folding a branch and threading one.
288pub(crate) type Bindings = HashMap<Value, Value>;
289
290/// The value this one stands for along the edge, which is itself when the edge says nothing.
291fn resolve(subst: &Bindings, value: Value) -> Value {
292    subst.get(&value).copied().unwrap_or(value)
293}
294
295/// Where this terminator always goes, if it always goes to one place.
296///
297/// `None` is every reason not to fold and does not say which, because the answer to all of them
298/// is to leave the branch alone.
299///
300/// Shared with [`crate::thread`], which asks it under a `subst` that binds the block's parameters
301/// to what one edge into the block carries. Two answers about when a branch is decided would be
302/// two compilers, and the threading pass would be the one nobody checked.
303pub(crate) fn taken(func: &Func, term: Inst, subst: &Bindings) -> Option<BlockCall> {
304    let data = &func[term];
305    let arg = *func[data.args].first()?;
306    match data.opcode {
307        Opcode::BrIf => {
308            let Extra::Targets(targets) = data.extra else { return None };
309            if let Some(call) = one_place(func, &func[targets]) {
310                return Some(call);
311            }
312            // The first target is the one taken when the condition is one, which is what
313            // `Builder::br_if` writes and what the printer reads back.
314            let arm = usize::from(!known(func, arg, subst)?);
315            func[targets].get(arm).copied()
316        }
317        Opcode::Switch => {
318            let Extra::Switch(at) = data.extra else { return None };
319            let info = func[at];
320            if let Some(call) = one_place(func, &func[info.targets]) {
321                return Some(call);
322            }
323            let (value, _) = constant(func, resolve(subst, arg))?;
324            // The default is the first target and the cases follow it in the order their values
325            // are in, so the target for a case that matches is one past the value's own place.
326            let case = func[info.cases].iter().position(|it| *it == value);
327            func[info.targets].get(case.map_or(0, |case| case + 1)).copied()
328        }
329        _ => None,
330    }
331}
332
333/// The one edge every arm of a branch is, when they are all the same edge.
334///
335/// Section 21.1's branch simplification, the half of it that is not about a constant. A branch
336/// whose arms all go to the same block with the same arguments goes there whatever the condition
337/// says, so it is a jump, and the condition becomes something nothing reads for
338/// [`crate::dce`] to take out.
339///
340/// The arguments have to match and not only the block. Two edges to one block carrying different
341/// arguments are two different edges, and that is the whole reason this IR passes arguments along
342/// an edge rather than writing a phi in the block: `if (c) goto L(1); else goto L(2);` is a real
343/// program and turning it into a jump would have to pick one of the two numbers.
344fn one_place(func: &Func, calls: &[BlockCall]) -> Option<BlockCall> {
345    let &first = calls.first()?;
346    let same = |call: &BlockCall| call.block == first.block && func[call.args] == func[first.args];
347    calls[1..].iter().all(same).then_some(first)
348}
349
350/// Rewrites the terminator as a jump to that one of its targets.
351///
352/// In place, and the target keeps the arguments it already had, because the arguments belong to
353/// the edge and the edge is the one that survives.
354pub(crate) fn jump_to(func: &mut Func, term: Inst, call: BlockCall) {
355    let targets = func.push_block_calls(&[call]);
356    let args = func.push_values(&[]);
357    let data = &mut func[term];
358    data.opcode = Opcode::Jump;
359    data.args = args;
360    data.extra = Extra::Targets(targets);
361}
362
363/// Takes every block the entry does not reach out of the function.
364///
365/// The cache goes with them, because what it is holding is answers about a function that had them
366/// in it, and the pass is not finished asking.
367///
368/// Shared with [`crate::thread`]. Section 6.5 makes this a standing obligation of whichever pass
369/// stranded the block rather than an optimization of this one, the verifier holds every pass to it,
370/// and a second walk written next door would be a second answer about what reachable means.
371pub(crate) fn sweep(func: &mut Func, an: &mut Analyses, stats: &mut Stats) {
372    let gone = stranded(func, an);
373    if gone.is_empty() {
374        return;
375    }
376    for block in gone {
377        func.remove_block(block);
378        stats.optimized(REMOVED);
379    }
380    an.clear();
381}
382
383/// The blocks the entry cannot reach, in block order.
384///
385/// This is reachability as the verifier counts it, which is over the edges the terminators name
386/// and additionally over the blocks a `block_addr` mentions. A block whose address is taken is
387/// arrived at by an `indirect_br` somewhere, and that instruction lists every block the address
388/// can hold, so the edge is already in the graph from the place control really leaves. What the
389/// graph does not carry is the `block_addr` itself, and deleting the block under one would leave
390/// an instruction naming a block that is not there.
391fn stranded(func: &Func, an: &mut Analyses) -> Vec<Block> {
392    let cfg = an.cfg(func);
393    let Some(entry) = cfg.entry() else { return Vec::new() };
394    let mut seen = vec![false; cfg.capacity()];
395    seen[entry.index()] = true;
396    let mut stack = vec![entry];
397    // And every block an image names, whatever the entry reaches. The `indirect_br` that arrives at
398    // one of those can be in another function, so unlike a `block_addr` below there is nothing in
399    // this function that says the block is still wanted.
400    for (block, _) in func.named_blocks() {
401        if !seen[block.index()] {
402            seen[block.index()] = true;
403            stack.push(block);
404        }
405    }
406    let mut reached = Vec::new();
407    while let Some(block) = stack.pop() {
408        for &succ in cfg.successors(block) {
409            if !seen[succ.index()] {
410                seen[succ.index()] = true;
411                stack.push(succ);
412            }
413        }
414        reached.push(block);
415    }
416    // The addresses in a second walk over the blocks the first one reached, because an address
417    // taken in a block nothing reaches is an address nothing takes.
418    let mut next = reached;
419    while !next.is_empty() {
420        let mut found = Vec::new();
421        for block in next {
422            for inst in func.insts(block) {
423                if func[inst].opcode != Opcode::BlockAddr {
424                    continue;
425                }
426                for call in func.successors(inst) {
427                    if !seen[call.block.index()] {
428                        seen[call.block.index()] = true;
429                        found.push(call.block);
430                    }
431                }
432            }
433        }
434        // Everything the newly kept blocks reach is kept too, which is what makes this a fixed
435        // point rather than one extra step.
436        let mut stack = found.clone();
437        while let Some(block) = stack.pop() {
438            for &succ in cfg.successors(block) {
439                if !seen[succ.index()] {
440                    seen[succ.index()] = true;
441                    stack.push(succ);
442                    found.push(succ);
443                }
444            }
445        }
446        next = found;
447    }
448    func.blocks().filter(|block| !seen[block.index()]).collect()
449}
450
451/// Where every edge that arrives at a block was written down, and which block it left.
452///
453/// A block call rather than a predecessor, because both halves of step three edit the edge and
454/// neither of them can find it again from the block it goes to. Redirecting one wants the slot in
455/// the pool, and taking a block parameter away wants the slot too, so this is what the step keeps
456/// instead of a [`crate::Cfg`].
457pub(crate) type Edges = HashMap<Block, Vec<(Block, Idx<BlockCall>)>>;
458
459/// Every edge in the function, filed under the block it arrives at.
460///
461/// Terminators only. A `block_addr` names a block and is not an edge, which is the same
462/// distinction [`stranded`] draws from the other side.
463///
464/// Shared with [`crate::thread`], which edits edges as well and so wants the slot in the pool for
465/// the same reason this step does.
466pub(crate) fn incoming(func: &Func) -> Edges {
467    let mut edges: Edges = HashMap::new();
468    for block in func.blocks() {
469        let Some(term) = func.terminator(block) else { continue };
470        for at in func.target_list(term).iter() {
471            edges.entry(func[at].block).or_default().push((block, at));
472        }
473    }
474    edges
475}
476
477/// Takes out every block parameter nothing reads, and the argument in its place on every edge.
478///
479/// This module's documentation is the argument. [`live`] is where the reading is done and this is
480/// what acts on it: one walk, in block order, because the answer is about the whole function and a
481/// worklist would only be asking the same question again.
482///
483/// # Fuel
484///
485/// One unit per parameter, and running out stops the removals rather than the looking, which this
486/// step can do because it has already worked out the whole answer. The parameters of a block are
487/// taken together once they have all been paid for, since an argument list that has lost some of
488/// its entries and not others is a function the verifier refuses.
489fn drop_unread(func: &mut Func, fuel: &mut Fuel, stats: &mut Stats) -> bool {
490    let Some(entry) = func.entry() else { return false };
491    let live = live(func, entry, &addressed(func));
492    let edges = incoming(func);
493    let mut changed = false;
494    let mut gone: HashSet<Value> = HashSet::new();
495    for block in func.blocks().collect::<Vec<Block>>() {
496        let mut taking = Vec::new();
497        for (index, &param) in func[block].params.iter().enumerate() {
498            if live.contains(&param) {
499                continue;
500            }
501            if !fuel.take() {
502                stats.missed(NO_FUEL_UNREAD);
503                continue;
504            }
505            taking.push(index);
506        }
507        if taking.is_empty() {
508            continue;
509        }
510        for _ in &taking {
511            stats.optimized(NOTHING_READS_IT);
512        }
513        gone.extend(taking.iter().map(|&index| func[block].params[index]));
514        take_params(func, block, &taking, edges.get(&block));
515        changed = true;
516    }
517    if !gone.is_empty() {
518        strand(func, gone);
519    }
520    changed
521}
522
523/// Takes out the instructions that were reading a parameter the step above removed.
524///
525/// Without this the step above leaves a use of a value nothing defines, which is a function the
526/// verifier is entitled to refuse and a printed IR that does not read back. It was written the
527/// other way round, on the argument that what is stranded is dead and [`crate::dce`] is the pass
528/// that takes what is dead. That holds at every level except the one that matters most, because
529/// `-O0` runs this pass and no other, so at `-O0` nothing came along afterwards and the dangling
530/// operand reached the printer. That was issue 1016.
531///
532/// Every instruction this removes is dead, and the proof is [`live`] read backwards. Live spreads
533/// from a result to the operands that produced it, so an instruction whose result were live would
534/// have made its operands live, and the parameter it reads was removed exactly because nothing
535/// made it live. A terminator's operands and an effectful instruction's operands are live by
536/// seeding, so neither of those can be reading a removed parameter either. The same argument
537/// applies again to the results of what goes, which is why this follows the chain.
538///
539/// # Fuel
540///
541/// None, for the reason this module's documentation gives about the blocks a fold strands: this is
542/// the second half of a transformation that has already been paid for, and a budget that could
543/// stop between the two halves would hand the verifier a use with no definition.
544fn strand(func: &mut Func, mut gone: HashSet<Value>) {
545    loop {
546        let mut spread = false;
547        for block in func.blocks().collect::<Vec<Block>>() {
548            for inst in func.insts(block).collect::<Vec<Inst>>() {
549                if !func[func[inst].args].iter().any(|value| gone.contains(value)) {
550                    continue;
551                }
552                let results: Vec<Value> = func[inst].results().collect();
553                for result in results {
554                    spread |= gone.insert(result);
555                }
556                func.remove_inst(inst);
557            }
558        }
559        // A second walk only when something this removed produced a value of its own, since that
560        // value may be read further down and layout order is not a promise about where.
561        if !spread {
562            return;
563        }
564    }
565}
566
567/// Every value something that happens reads, worked out from nothing live outwards.
568///
569/// The seeds are the operands of the instructions that have to happen, which are the terminators
570/// and the ones with effects. A terminator's own operands are seeds and the arguments it passes to
571/// the blocks it branches to are not, and that split is the whole point: an argument is read only
572/// if the parameter it lands in is read, so it waits for that parameter to be reached.
573///
574/// Then live spreads two ways. From a value an instruction produced, to that instruction's
575/// operands, because producing it meant reading them. From a parameter, to the argument in its
576/// place on every edge into the block, because arriving there meant passing them.
577///
578/// The entry block's parameters are the function's own and are live by declaration rather than by
579/// use, and so are the parameters of a block whose address is taken, because an `indirect_br` is a
580/// way in that this reads from the wrong end.
581fn live(func: &Func, entry: Block, addressed: &HashSet<Block>) -> HashSet<Value> {
582    let mut where_from: HashMap<Value, (Block, usize)> = HashMap::new();
583    let mut live: HashSet<Value> = HashSet::new();
584    let mut work: Vec<Value> = Vec::new();
585    let seed = |value: Value, live: &mut HashSet<Value>, work: &mut Vec<Value>| {
586        if live.insert(value) {
587            work.push(value);
588        }
589    };
590    for block in func.blocks() {
591        let held = block == entry || addressed.contains(&block);
592        for (index, &param) in func[block].params.iter().enumerate() {
593            where_from.insert(param, (block, index));
594            if held {
595                seed(param, &mut live, &mut work);
596            }
597        }
598        for inst in func.insts(block) {
599            if !func.is_terminator(inst) && !func[inst].opcode.has_effects() {
600                continue;
601            }
602            for &value in &func[func[inst].args] {
603                seed(value, &mut live, &mut work);
604            }
605        }
606    }
607
608    let edges = incoming(func);
609    while let Some(value) = work.pop() {
610        match func[value].def {
611            Def::Result { inst, .. } => {
612                for &operand in &func[func[inst].args] {
613                    seed(operand, &mut live, &mut work);
614                }
615            }
616            Def::Param { .. } => {
617                let Some(&(block, index)) = where_from.get(&value) else { continue };
618                for &(_, at) in edges.get(&block).into_iter().flatten() {
619                    let Some(&arg) = func[func[at].args].get(index) else { continue };
620                    seed(arg, &mut live, &mut work);
621                }
622            }
623        }
624    }
625    live
626}
627
628/// Section 21.4's step three, both halves of it, on one worklist. Says whether anything changed.
629///
630/// Forwarder removal and redundant block parameter removal are one step because each is the other's
631/// reason to look again. Pointing a block's predecessors past it hands the block below several ways
632/// in where there was one, and a parameter that was obviously one value may stop being one, or
633/// several arguments that were the same may now arrive together and make one; taking a parameter
634/// away can leave a block with nothing but its jump, which is the whole of what a forwarder is.
635///
636/// A block goes back on the worklist when an edge into it or out of it moved, and the loop stops
637/// when nothing has moved. That is a fixed point, and it is the one section 21.4 asks for, over the
638/// step rather than over the pass.
639///
640/// What this does not do is put the two halves in a particular order within a block. Parameters
641/// first is not a policy, it is the only order that gets a forwarder with a redundant parameter in
642/// one visit rather than two.
643///
644/// # Fuel
645///
646/// One unit for each forwarder and one for each parameter, and the first refusal is where the step
647/// stops rather than where it starts skipping. The other steps go on looking after they run out and
648/// say so once for each thing they did not do, which they can because each of them walks the
649/// function once. This one comes back to a block whenever an edge near it moved, so a refusal
650/// counted per visit would count one opportunity several times and the number would say more about
651/// the shape of the worklist than about the function. A budget that has reached zero is not going
652/// to have anything in it later, so there is one refusal recorded and it is the true one.
653fn straighten(
654    func: &mut Func,
655    fuel: &mut Fuel,
656    stats: &mut Stats,
657    forward: &mut HashMap<Value, Value>,
658) -> bool {
659    let Some(entry) = func.entry() else { return false };
660    let addressed = addressed(func);
661    let mut edges = incoming(func);
662    let mut work: VecDeque<Block> = func.blocks().collect();
663    let mut queued: HashSet<Block> = work.iter().copied().collect();
664    let mut gone: HashSet<Block> = HashSet::new();
665    let mut changed = false;
666    while let Some(block) = work.pop_front() {
667        queued.remove(&block);
668        if gone.contains(&block) {
669            continue;
670        }
671        let mut starved = false;
672        if block != entry {
673            let drop = redundant(func, block, edges.get(&block), forward);
674            let mut taking = Vec::new();
675            for (index, value) in drop {
676                if !fuel.take() {
677                    stats.missed(NO_FUEL_PARAM);
678                    starved = true;
679                    break;
680                }
681                // Through what an earlier one already decided, the same way merging does, because
682                // a parameter can be redundant on an argument that is on its way somewhere else.
683                let value = uses::chase(forward, value);
684                forward.insert(func[block].params[index], value);
685                taking.push(index);
686                stats.optimized(SAME_EVERY_WAY);
687            }
688            if !taking.is_empty() {
689                take_params(func, block, &taking, edges.get(&block));
690                // Itself, because a block that has run out of parameters may be a forwarder now,
691                // and because a parameter can be redundant on one that just went.
692                requeue(block, &mut work, &mut queued);
693                // And the blocks below, because a parameter passed straight on down is the shape
694                // section 21.2 means by one removal making the next one possible.
695                if let Some(term) = func.terminator(block) {
696                    for call in func.successors(term).collect::<Vec<BlockCall>>() {
697                        requeue(call.block, &mut work, &mut queued);
698                    }
699                }
700                changed = true;
701            }
702        }
703        // What was already paid for is applied first, and then the step stops, because a block
704        // whose parameters half went is a block whose edges have to agree with it.
705        if starved {
706            break;
707        }
708        let Some((term, into, args)) = forwards(func, block, entry, &addressed, &edges) else {
709            continue;
710        };
711        if !fuel.take() {
712            stats.missed(NO_FUEL_FORWARD);
713            break;
714        }
715        // The block's own edge stops existing along with the block, and it has to come out of the
716        // record before its predecessors' edges go in, or the block below would be told it has a
717        // way in from a block that is not there.
718        let out = func.target_list(term).iter().next().expect("a jump has a target");
719        if let Some(list) = edges.get_mut(&into) {
720            list.retain(|&(_, at)| at != out);
721        }
722        let ins = edges.remove(&block).unwrap_or_default();
723        for &(_, at) in &ins {
724            // A list of its own for each edge rather than one shared between them, because a
725            // later substitution rewrites a list in place and a shared one would be rewritten
726            // once for every edge that named it.
727            let call = func[at];
728            let args = func.push_values(&args);
729            func.set_block_call(at, BlockCall { block: into, args, ..call });
730        }
731        edges.entry(into).or_default().extend(ins.iter().copied());
732        func.remove_block(block);
733        gone.insert(block);
734        stats.optimized(FORWARDED);
735        changed = true;
736        requeue(into, &mut work, &mut queued);
737        for &(from, _) in &ins {
738            requeue(from, &mut work, &mut queued);
739        }
740    }
741    changed
742}
743
744/// Puts a block back on the worklist, if it is not on it already.
745fn requeue(block: Block, work: &mut VecDeque<Block>, queued: &mut HashSet<Block>) {
746    if queued.insert(block) {
747        work.push_back(block);
748    }
749}
750
751/// Which of a block's parameters arrive as the same value every way in, and what that value is.
752///
753/// Section 21.2. A parameter that is `x` from one edge and `x` from every other is not carrying
754/// anything, it is spelling `x` a second way, and document 12's hash consing cannot see through the
755/// spelling, so two equal values look different for as long as it is there.
756///
757/// The one subtlety is an argument that is the parameter itself, which is what a loop header looks
758/// like: the preheader passes `init` and the latch passes the parameter back. Reading that
759/// literally says two different values and the answer is `init`, because a value that can only ever
760/// be itself or `init` was `init` to begin with. So a self reference is not an argument for this
761/// purpose, which is the same optimistic reading section 14.1 takes.
762///
763/// A block with no way in gets nothing said about it. That is an unreachable block, [`sweep`] has
764/// already run, and answering `init` for a parameter with no arguments at all would be inventing
765/// one.
766fn redundant(
767    func: &Func,
768    block: Block,
769    ins: Option<&Vec<(Block, Idx<BlockCall>)>>,
770    forward: &HashMap<Value, Value>,
771) -> Vec<(usize, Value)> {
772    let Some(ins) = ins.filter(|ins| !ins.is_empty()) else { return Vec::new() };
773    let mut found = Vec::new();
774    for (index, &param) in func[block].params.iter().enumerate() {
775        let mut only = None;
776        let mut agree = true;
777        for &(_, at) in ins {
778            let list = func[at].args;
779            let Some(&arg) = func[list].get(index) else {
780                // Fewer arguments than parameters is a function the verifier will refuse, and
781                // guessing what the missing one was is not this pass's job.
782                agree = false;
783                break;
784            };
785            let arg = uses::chase(forward, arg);
786            if arg == param {
787                continue;
788            }
789            match only {
790                None => only = Some(arg),
791                Some(seen) if seen == arg => {}
792                Some(_) => {
793                    agree = false;
794                    break;
795                }
796            }
797        }
798        if !agree {
799            continue;
800        }
801        if let Some(value) = only {
802            found.push((index, value));
803        }
804    }
805    found
806}
807
808/// Drops those parameters of a block and the arguments in their places on every edge into it.
809///
810/// Both halves together, because a block whose parameters and arguments disagree in number is one
811/// the verifier refuses, and section 21.6 says that is the most common bug in this document.
812fn take_params(
813    func: &mut Func,
814    block: Block,
815    taking: &[usize],
816    ins: Option<&Vec<(Block, Idx<BlockCall>)>>,
817) {
818    for &(_, at) in ins.into_iter().flatten() {
819        let call = func[at];
820        let kept: Vec<Value> = func[call.args]
821            .iter()
822            .enumerate()
823            .filter(|(index, _)| !taking.contains(index))
824            .map(|(_, &value)| value)
825            .collect();
826        let args = func.push_values(&kept);
827        func.set_block_call(at, BlockCall { args, ..call });
828    }
829    let mut index = 0;
830    func.retain_params(block, |_| {
831        let keep = !taking.contains(&index);
832        index += 1;
833        keep
834    });
835}
836
837/// Where a block forwards to and what it passes on, when it is a forwarder.
838///
839/// The conditions are in this module's documentation, and every one of them is a `None` here. What
840/// comes back is the terminator, the block below, and the arguments the jump was carrying, which
841/// are what each of the block's predecessors will be carrying instead.
842fn forwards(
843    func: &Func,
844    block: Block,
845    entry: Block,
846    addressed: &HashSet<Block>,
847    edges: &Edges,
848) -> Option<(Inst, Block, Vec<Value>)> {
849    if block == entry || addressed.contains(&block) || !func[block].params.is_empty() {
850        return None;
851    }
852    let term = func.terminator(block)?;
853    if func[term].opcode != Opcode::Jump {
854        return None;
855    }
856    // Nothing above the jump, which is what "no instructions" means once the jump is counted as
857    // one of them.
858    if func.insts(block).count() != 1 {
859        return None;
860    }
861    let call = func.successors(term).next()?;
862    if call.block == block {
863        return None;
864    }
865    if carrying(func, block, call.block, func[call.args].len(), edges) {
866        return None;
867    }
868    Some((term, call.block, func[call.args].to_vec()))
869}
870
871/// Whether taking this forwarder out would put arguments on an edge that has nowhere to move them.
872///
873/// An edge carries values when the block it arrives at takes parameters, and giving a parameter its
874/// value is a move that has to happen on the edge itself. An edge out of a block that goes two ways
875/// and into a block arrived at two ways has no block to put that move in, so the back end splits it
876/// and puts an empty block back on it, which is `rucc_codegen::split::critical`. A forwarder that
877/// carries arguments and whose predecessor branches is already that block, sitting in the place the
878/// layout wants it rather than at the end where the splitter has to append it. Taking it out and
879/// having it put back costs a jump and a longer live range, and the measurement on the corpus says
880/// it costs enough to see, so it is not taken out.
881///
882/// A forwarder that carries nothing is removed whatever the edges look like, because there is no
883/// move to find a place for and the splitter would leave the edge alone as well.
884fn carrying(func: &Func, block: Block, into: Block, args: usize, edges: &Edges) -> bool {
885    if args == 0 {
886        return false;
887    }
888    let ins = edges.get(&block).map_or(0, Vec::len);
889    let after = edges.get(&into).map_or(0, Vec::len) - 1 + ins;
890    if after < 2 {
891        return false;
892    }
893    edges.get(&block).into_iter().flatten().any(|&(from, _)| {
894        let Some(term) = func.terminator(from) else { return false };
895        func.target_list(term).iter().count() >= 2
896    })
897}
898
899/// The runs of blocks that are one block written as several, head first.
900///
901/// Section 21.1's block merging, and the doc calls it a pure win for a reason worth stating: it
902/// does not delete an instruction or move one earlier, it takes a boundary out. Every analysis
903/// that is cheap inside a block and expensive across one gets more of the cheap kind, which is
904/// most of them, and the branch that stops being a branch is the smallest part of it.
905///
906/// A block goes into the one above it when the one above it ends in a jump and this is the only
907/// way in. Both halves are needed. One way in and a `br_if` above means the other arm would lose
908/// its terminator, and a jump above with two ways in means the second predecessor would arrive in
909/// the middle of a block.
910///
911/// The refusals are the entry block, which has to stay where control arrives even when one block
912/// jumps to it; a block that jumps to itself, whose one predecessor is itself; and a block whose
913/// address is taken, which is arrived at by an `indirect_br` the graph reads from the other end.
914///
915/// The answer is chains rather than pairs because a run of three is ordinary and the middle one
916/// stops existing partway through. Each block is the head of at most one of these and the tail of
917/// at most one, so what comes out is disjoint paths, and starting only from a head is what leaves
918/// a ring of blocks that all point at each other alone rather than walking it forever.
919fn chains(func: &Func, an: &mut Analyses) -> Vec<Vec<Block>> {
920    let cfg = an.cfg(func);
921    let Some(entry) = cfg.entry() else { return Vec::new() };
922    let addressed = addressed(func);
923    let mut below = HashMap::new();
924    let mut is_below = HashSet::new();
925    for block in func.blocks() {
926        let Some(term) = func.terminator(block) else { continue };
927        if func[term].opcode != Opcode::Jump {
928            continue;
929        }
930        let Some(call) = func.successors(term).next() else { continue };
931        let into = call.block;
932        let preds = cfg.predecessors(into);
933        if into == entry || into == block || addressed.contains(&into) {
934            continue;
935        }
936        if preds.len() != 1 || preds[0] != block {
937            continue;
938        }
939        below.insert(block, into);
940        is_below.insert(into);
941    }
942    let heads = func.blocks().filter(|it| below.contains_key(it) && !is_below.contains(it));
943    heads
944        .map(|head| {
945            let mut chain = vec![head];
946            let mut at = head;
947            while let Some(&next) = below.get(&at) {
948                chain.push(next);
949                at = next;
950            }
951            chain
952        })
953        .collect()
954}
955
956/// Every block some `block_addr` names, and every block an image names.
957///
958/// The second kind is the one with nothing in the function to find. An image is in another section
959/// and what it holds is a relocation, so the `goto *p` that arrives at the block can be in a
960/// function this one cannot see, and a block merged into the one above it would be a name pointing
961/// at the wrong instruction.
962fn addressed(func: &Func) -> HashSet<Block> {
963    let mut taken: HashSet<Block> = func.named_blocks().map(|(block, _)| block).collect();
964    for block in func.blocks() {
965        for inst in func.insts(block) {
966            if func[inst].opcode != Opcode::BlockAddr {
967                continue;
968            }
969            for call in func.successors(inst) {
970                taken.insert(call.block);
971            }
972        }
973    }
974    taken
975}
976
977/// Moves everything in a block into the head of its chain and takes the block out of the function.
978///
979/// The jump is what is really being deleted, and the arguments it carried are what the merged
980/// block's parameters were going to be told. Binding each parameter to the argument in its place
981/// and pointing every reader at it is exactly what the jump was doing at run time, so the record
982/// goes in the map and the whole map is spent in one walk when the pass is done.
983fn merge(func: &mut Func, head: Block, block: Block, forward: &mut HashMap<Value, Value>) {
984    let term = func.terminator(head).expect("the head of a chain ends in a jump");
985    let call = func.successors(term).next().expect("a jump goes somewhere");
986    let args = func[call.args].to_vec();
987    let params = func[block].params.clone();
988    for (param, arg) in params.into_iter().zip(args) {
989        // Through whatever the merge above this one already decided, because a chain of three
990        // binds the middle block's parameter to something the head passed and then binds the last
991        // block's parameter to that same parameter.
992        let arg = uses::chase(forward, arg);
993        forward.insert(param, arg);
994    }
995    func.remove_inst(term);
996    for inst in func.insts(block).collect::<Vec<Inst>>() {
997        func.remove_inst(inst);
998        func.append_inst(head, inst);
999    }
1000    func.remove_block(block);
1001}
1002
1003/// Whether this condition is always true or always false, given what the edge binds.
1004fn known(func: &Func, value: Value, subst: &Bindings) -> Option<bool> {
1005    let value = resolve(subst, value);
1006    if let Some((imm, _)) = constant(func, value) {
1007        return Some(imm.unsigned() != 0);
1008    }
1009    compared(func, value, subst)
1010}
1011
1012/// What a comparison of two constants comes out as.
1013///
1014/// The comparison itself is never rewritten here, and it does not have to be. [`crate::fold`]
1015/// evaluates one whose operands are both already constants, so what is left for this is the case
1016/// folding cannot see: an operand that is a constant only along the edge being followed, which is
1017/// what `subst` carries. That is the whole reason this is still a question worth asking after
1018/// folding has run.
1019///
1020/// The arithmetic is [`crate::fold::compare`] rather than a copy of it, so the two places cannot
1021/// come to differ about what `slt` means.
1022fn compared(func: &Func, value: Value, subst: &Bindings) -> Option<bool> {
1023    let Def::Result { inst, .. } = func[value].def else { return None };
1024    let data = &func[inst];
1025    if data.opcode != Opcode::ICmp {
1026        return None;
1027    }
1028    let Extra::IntPred(pred) = data.extra else { return None };
1029    let args = &func[data.args];
1030    let (lhs, ty) = constant(func, resolve(subst, *args.first()?))?;
1031    let (rhs, _) = constant(func, resolve(subst, *args.get(1)?))?;
1032    Some(crate::fold::compare(pred, lhs, rhs, ty))
1033}
1034
1035#[cfg(test)]
1036mod tests {
1037    use rucc_base::Interner;
1038    use rucc_ir::{
1039        Block, Builder, Def, Flags, Func, Inst, IntPred, MemInfo, MemOrder, Module, Opcode,
1040        Restrict, Signature, Type, Value,
1041    };
1042    use rucc_target::{Arch, Env, Os, TargetInfo, Triple};
1043
1044    use super::SimplifyCfg;
1045    use crate::stats::Kind;
1046    use crate::testing::graph;
1047    use crate::{Fuel, Pass, Preserved, Stats};
1048
1049    /// Runs the pass with as much fuel as it wants.
1050    fn simplify(func: &mut Func) -> Stats {
1051        SimplifyCfg.run(func, &mut crate::machine::fixtures::analyses(), &mut Fuel::unlimited())
1052    }
1053
1054    /// The blocks the function still has, by number.
1055    fn blocks(func: &Func) -> Vec<usize> {
1056        func.blocks().map(Block::index).collect()
1057    }
1058
1059    /// The opcode of a block's terminator.
1060    fn terminator(func: &Func, block: usize) -> Opcode {
1061        let block = Block::from_usize(block);
1062        func[func.terminator(block).expect("every block here has one")].opcode
1063    }
1064
1065    /// Where a block's terminator goes, as block numbers.
1066    fn goes_to(func: &Func, block: usize) -> Vec<usize> {
1067        let block = Block::from_usize(block);
1068        let term = func.terminator(block).expect("every block here has one");
1069        func.successors(term).map(|call| call.block.index()).collect()
1070    }
1071
1072    /// The block the instruction that produced a value is in now, if it is in one.
1073    ///
1074    /// Which arm of a branch survived is a question about where its code ended up rather than
1075    /// about the shape of the graph, because the arm that survives is merged into the block above
1076    /// it in the same run and the two blocks stop being two.
1077    fn lives_in(func: &Func, value: Value) -> Option<usize> {
1078        let Def::Result { inst, .. } = func[value].def else { return None };
1079        func.block_of(inst).map(Block::index)
1080    }
1081
1082    /// A function with an entry, a `br_if` on `cond`, two arms and a join.
1083    ///
1084    /// The condition is built by the caller out of the builder it is handed, which is what lets
1085    /// one shape stand for a constant, a comparison and a value nothing knows anything about. Each
1086    /// arm holds one instruction that does nothing, which is there to be told apart from the one
1087    /// in the other arm, and the two of them come back with the function.
1088    fn diamond(cond: impl FnOnce(&mut Builder<'_>) -> Value) -> (Func, [Value; 2]) {
1089        let mut names = Interner::new();
1090        let mut func = Func::new(names.intern("f"), Signature::new());
1091        let entry = func.create_block();
1092        let then_block = func.create_block();
1093        let else_block = func.create_block();
1094        let join = func.create_block();
1095        let mut build = Builder::new(&mut func, entry);
1096        let cond = cond(&mut build);
1097        build.br_if(cond, then_block, &[], else_block, &[]);
1098        let mut marks = Vec::new();
1099        for (arm, mark) in [(then_block, 111), (else_block, 222)] {
1100            let mut build = Builder::new(&mut func, arm);
1101            marks.push(build.iconst(Type::int(32), mark));
1102            build.jump(join, &[]);
1103        }
1104        let mut build = Builder::new(&mut func, join);
1105        build.ret(&[]);
1106        (func, [marks[0], marks[1]])
1107    }
1108
1109    #[test]
1110    fn a_branch_on_a_true_constant_becomes_a_jump_to_the_first_arm() {
1111        let (mut func, [taken, other]) = diamond(|build| build.iconst(Type::int(1), 1));
1112        let stats = simplify(&mut func);
1113        assert!(stats.changed());
1114        assert_eq!(stats.count(Kind::Optimized, super::FOLDED), 1);
1115        // The arm it did not take is gone, because nothing else went there, and the arm it did
1116        // take had one way in and went into the entry along with the join below it.
1117        assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 1);
1118        assert_eq!(stats.count(Kind::Optimized, super::MERGED), 2);
1119        assert_eq!(lives_in(&func, taken), Some(0));
1120        assert_eq!(lives_in(&func, other), None);
1121        assert_eq!(blocks(&func), [0]);
1122    }
1123
1124    #[test]
1125    fn a_branch_on_a_false_constant_becomes_a_jump_to_the_second_arm() {
1126        let (mut func, [other, taken]) = diamond(|build| build.iconst(Type::int(1), 0));
1127        assert!(simplify(&mut func).changed());
1128        assert_eq!(lives_in(&func, taken), Some(0));
1129        assert_eq!(lives_in(&func, other), None);
1130        assert_eq!(blocks(&func), [0]);
1131    }
1132
1133    #[test]
1134    fn folding_a_branch_and_merging_what_it_leaves_are_two_things_fuel_buys_apart() {
1135        // The same function as the test above, with fuel for the fold and nothing after it. The
1136        // jump is there to be seen, which is the shape the merge would otherwise take away.
1137        let (mut func, _) = diamond(|build| build.iconst(Type::int(1), 1));
1138        let stats =
1139            SimplifyCfg.run(&mut func, &mut crate::machine::fixtures::analyses(), &mut Fuel::of(1));
1140        assert_eq!(terminator(&func, 0), Opcode::Jump);
1141        assert_eq!(goes_to(&func, 0), [1]);
1142        assert_eq!(blocks(&func), [0, 1, 3]);
1143        assert_eq!(stats.count(Kind::Optimized, super::MERGED), 0);
1144        // Both blocks of the chain, because a block only reaches the head once the block between
1145        // them has, so running out before the first one means neither.
1146        assert_eq!(stats.count(Kind::Missed, super::NO_FUEL_MERGE), 2);
1147    }
1148
1149    #[test]
1150    fn a_branch_on_a_comparison_of_two_constants_is_read_without_folding_it() {
1151        // Both ways round on every predicate, which is where a sign error or an inverted
1152        // comparison would hide. A comparison the pass reads is left standing, because folding
1153        // it would produce an `i1` on its own and issue 352 says that does not lower.
1154        let cases: &[(IntPred, i128, i128, bool)] = &[
1155            (IntPred::Eq, 7, 7, true),
1156            (IntPred::Eq, 7, 8, false),
1157            (IntPred::Ne, 7, 8, true),
1158            (IntPred::Ne, 7, 7, false),
1159            (IntPred::Slt, -1, 1, true),
1160            (IntPred::Slt, 1, -1, false),
1161            (IntPred::Sle, -1, -1, true),
1162            (IntPred::Sle, 1, -1, false),
1163            (IntPred::Sgt, 1, -1, true),
1164            (IntPred::Sgt, -1, 1, false),
1165            (IntPred::Sge, -1, -1, true),
1166            (IntPred::Sge, -1, 1, false),
1167            (IntPred::Ult, 1, -1, true),
1168            (IntPred::Ult, -1, 1, false),
1169            (IntPred::Ule, -1, -1, true),
1170            (IntPred::Ule, -1, 1, false),
1171            (IntPred::Ugt, -1, 1, true),
1172            (IntPred::Ugt, 1, -1, false),
1173            (IntPred::Uge, -1, -1, true),
1174            (IntPred::Uge, 1, -1, false),
1175        ];
1176        for &(pred, lhs, rhs, taken) in cases {
1177            let (mut func, marks) = diamond(|build| {
1178                let lhs = build.iconst(Type::int(32), lhs);
1179                let rhs = build.iconst(Type::int(32), rhs);
1180                build.icmp(pred, lhs, rhs)
1181            });
1182            assert!(simplify(&mut func).changed(), "{pred:?} {lhs} {rhs}");
1183            let [went, gone] = if taken { [marks[0], marks[1]] } else { [marks[1], marks[0]] };
1184            assert_eq!(lives_in(&func, went), Some(0), "{pred:?} {lhs} {rhs}");
1185            assert_eq!(lives_in(&func, gone), None, "{pred:?} {lhs} {rhs}");
1186            let kept = func.insts(Block::from_usize(0)).any(|it| func[it].opcode == Opcode::ICmp);
1187            assert!(kept, "the comparison was folded away and issue 352 says it must not be");
1188        }
1189    }
1190
1191    #[test]
1192    fn a_branch_on_something_nobody_knows_is_left_alone() {
1193        let mut names = Interner::new();
1194        let mut func = Func::new(names.intern("f"), Signature::new().with_params(&[Type::int(1)]));
1195        let entry = func.create_block();
1196        let then_block = func.create_block();
1197        let else_block = func.create_block();
1198        let cond = func.append_param(entry, Type::int(1));
1199        let mut build = Builder::new(&mut func, entry);
1200        build.br_if(cond, then_block, &[], else_block, &[]);
1201        for arm in [then_block, else_block] {
1202            let mut build = Builder::new(&mut func, arm);
1203            build.ret(&[]);
1204        }
1205        let stats = simplify(&mut func);
1206        assert!(!stats.changed());
1207        assert!(stats.is_empty(), "a pass with nothing to say should say nothing");
1208        assert_eq!(terminator(&func, 0), Opcode::BrIf);
1209        assert_eq!(blocks(&func), [0, 1, 2]);
1210    }
1211
1212    /// A function whose entry switches on a constant, with a marker in the default and in each
1213    /// case, in that order.
1214    fn switched(on: i128, cases: &[i128]) -> (Func, Vec<Value>) {
1215        let mut names = Interner::new();
1216        let mut func = Func::new(names.intern("f"), Signature::new());
1217        let entry = func.create_block();
1218        let arms: Vec<Block> = (0..=cases.len()).map(|_| func.create_block()).collect();
1219        let mut build = Builder::new(&mut func, entry);
1220        let value = build.iconst(Type::int(32), on);
1221        let pairs: Vec<(i128, Block)> =
1222            cases.iter().enumerate().map(|(at, &case)| (case, arms[at + 1])).collect();
1223        build.switch(value, arms[0], &pairs);
1224        let mut marks = Vec::new();
1225        for (at, &arm) in arms.iter().enumerate() {
1226            let mut build = Builder::new(&mut func, arm);
1227            marks.push(build.iconst(Type::int(32), 100 + at as i128));
1228            build.ret(&[]);
1229        }
1230        (func, marks)
1231    }
1232
1233    #[test]
1234    fn a_switch_on_a_constant_takes_the_case_that_matches() {
1235        let (mut func, marks) = switched(5, &[4, 5]);
1236        assert!(simplify(&mut func).changed());
1237        assert_eq!(lives_in(&func, marks[2]), Some(0));
1238        assert_eq!(lives_in(&func, marks[0]), None);
1239        assert_eq!(lives_in(&func, marks[1]), None);
1240        assert_eq!(blocks(&func), [0]);
1241    }
1242
1243    #[test]
1244    fn a_switch_on_a_constant_no_case_names_takes_the_default() {
1245        let (mut func, marks) = switched(9, &[4]);
1246        assert!(simplify(&mut func).changed());
1247        assert_eq!(lives_in(&func, marks[0]), Some(0));
1248        assert_eq!(lives_in(&func, marks[1]), None);
1249        assert_eq!(blocks(&func), [0]);
1250    }
1251
1252    #[test]
1253    fn the_arguments_travel_with_the_edge_that_survives() {
1254        // The whole reason there are no phi nodes: the argument is in the branch beside the
1255        // block it goes to, so the surviving arm brings its own and the other one leaves with
1256        // the edge it was on. Both arms name the same block, so this is also the case branch
1257        // simplification has to leave alone: one block and two edges, because the two edges say
1258        // different things.
1259        let mut names = Interner::new();
1260        let mut func = Func::new(names.intern("f"), Signature::new());
1261        let entry = func.create_block();
1262        let join = func.create_block();
1263        let param = func.append_param(join, Type::int(32));
1264        let mut build = Builder::new(&mut func, entry);
1265        let cond = build.iconst(Type::int(1), 0);
1266        let taken = build.iconst(Type::int(32), 11);
1267        let other = build.iconst(Type::int(32), 22);
1268        build.br_if(cond, join, &[other], join, &[taken]);
1269        let mut build = Builder::new(&mut func, join);
1270        build.ret(&[param]);
1271        assert!(simplify(&mut func).changed());
1272        // The jump took the edge that survived, and then the block below it had one way in and
1273        // came up, which is where the parameter stopped being a parameter: whatever read it reads
1274        // the argument that edge was carrying.
1275        assert_eq!(blocks(&func), [0]);
1276        let term = func.terminator(entry).expect("the entry has one");
1277        assert_eq!(func[func[term].args], [taken]);
1278        assert_ne!(func[func[term].args], [param]);
1279    }
1280
1281    #[test]
1282    fn a_branch_whose_arms_are_the_same_edge_becomes_a_jump() {
1283        // Section 21.1's branch simplification, which is about the targets rather than about the
1284        // condition: nothing here knows what `cond` is and it does not matter, because both ways
1285        // out arrive at the same place carrying the same thing.
1286        let mut names = Interner::new();
1287        let signature = Signature::new().with_params(&[Type::int(1)]);
1288        let mut func = Func::new(names.intern("f"), signature);
1289        let entry = func.create_block();
1290        let join = func.create_block();
1291        let cond = func.append_param(entry, Type::int(1));
1292        let mut build = Builder::new(&mut func, entry);
1293        build.br_if(cond, join, &[], join, &[]);
1294        let mut build = Builder::new(&mut func, join);
1295        build.ret(&[]);
1296        let stats = simplify(&mut func);
1297        assert_eq!(stats.count(Kind::Optimized, super::FOLDED), 1);
1298        assert_eq!(stats.count(Kind::Optimized, super::MERGED), 1);
1299        assert_eq!(blocks(&func), [0]);
1300        assert_eq!(terminator(&func, 0), Opcode::Return);
1301    }
1302
1303    #[test]
1304    fn a_switch_whose_cases_all_go_to_one_place_becomes_a_jump() {
1305        let mut names = Interner::new();
1306        let signature = Signature::new().with_params(&[Type::int(32)]);
1307        let mut func = Func::new(names.intern("f"), signature);
1308        let entry = func.create_block();
1309        let join = func.create_block();
1310        let value = func.append_param(entry, Type::int(32));
1311        let mut build = Builder::new(&mut func, entry);
1312        build.switch(value, join, &[(4, join), (5, join)]);
1313        let mut build = Builder::new(&mut func, join);
1314        build.ret(&[]);
1315        let stats = simplify(&mut func);
1316        assert_eq!(stats.count(Kind::Optimized, super::FOLDED), 1);
1317        assert_eq!(blocks(&func), [0]);
1318    }
1319
1320    #[test]
1321    fn a_branch_to_one_block_by_two_edges_that_differ_is_left_alone() {
1322        // One block and two edges. Folding would have to pick one of the two arguments, and
1323        // whichever it picked would be the wrong one half the time.
1324        let mut names = Interner::new();
1325        let signature = Signature::new().with_params(&[Type::int(1)]);
1326        let mut func = Func::new(names.intern("f"), signature);
1327        let entry = func.create_block();
1328        let join = func.create_block();
1329        let cond = func.append_param(entry, Type::int(1));
1330        let param = func.append_param(join, Type::int(32));
1331        let mut build = Builder::new(&mut func, entry);
1332        let first = build.iconst(Type::int(32), 11);
1333        let second = build.iconst(Type::int(32), 22);
1334        build.br_if(cond, join, &[first], join, &[second]);
1335        let mut build = Builder::new(&mut func, join);
1336        // Returned rather than dropped, because a parameter nobody reads is one the step that
1337        // takes those out would take, and this test is about the branch above it.
1338        build.ret(&[param]);
1339        let stats = simplify(&mut func);
1340        assert!(!stats.changed());
1341        assert_eq!(terminator(&func, 0), Opcode::BrIf);
1342        assert_eq!(blocks(&func), [0, 1]);
1343    }
1344
1345    #[test]
1346    fn a_block_the_dead_arm_shared_with_a_live_one_stays() {
1347        // Issue 359 in the small. The block holding `bar` is inside the body of the dead `if`
1348        // and is a `case` of the switch as well, so the arm goes and the block does not.
1349        let mut names = Interner::new();
1350        let mut func = Func::new(names.intern("f"), Signature::new().with_params(&[Type::int(32)]));
1351        let entry = func.create_block();
1352        let dead = func.create_block();
1353        let shared = func.create_block();
1354        let exit = func.create_block();
1355        let x = func.append_param(entry, Type::int(32));
1356        let mut build = Builder::new(&mut func, entry);
1357        let never = build.iconst(Type::int(1), 0);
1358        build.switch(x, exit, &[(0, dead), (1, shared)]);
1359        // The `if (0)` inside the first case, whose body is where the second case's label sits.
1360        // The constant is the body of the arm that survives, and it is there so that the block is
1361        // a block with something in it rather than a forwarder that step three points past.
1362        let mut build = Builder::new(&mut func, dead);
1363        build.iconst(Type::int(32), 1);
1364        build.br_if(never, shared, &[], exit, &[]);
1365        for arm in [shared, exit] {
1366            let mut build = Builder::new(&mut func, arm);
1367            build.ret(&[]);
1368        }
1369        let stats = simplify(&mut func);
1370        assert!(stats.changed());
1371        // The switch is on a parameter, so it stays. The branch inside the dead arm folds to the
1372        // exit, and nothing is removed at all, because the shared block is still a case.
1373        assert_eq!(terminator(&func, 0), Opcode::Switch);
1374        assert_eq!(goes_to(&func, 1), [3]);
1375        assert_eq!(blocks(&func), [0, 1, 2, 3]);
1376        assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 0);
1377    }
1378
1379    #[test]
1380    fn a_block_whose_address_is_taken_is_not_removed() {
1381        // Reachability here has to be the verifier's reachability. The graph does not carry the
1382        // edge from a `block_addr` to the block it names, and a pass that removed the block
1383        // under one would leave an instruction pointing at nothing.
1384        let mut names = Interner::new();
1385        let mut func = Func::new(names.intern("f"), Signature::new());
1386        let entry = func.create_block();
1387        let labelled = func.create_block();
1388        let arm = func.create_block();
1389        let mut build = Builder::new(&mut func, entry);
1390        let cond = build.iconst(Type::int(1), 1);
1391        let addr = build.block_addr(labelled);
1392        build.br_if(cond, arm, &[], labelled, &[]);
1393        let mut build = Builder::new(&mut func, arm);
1394        build.indirect_br(addr, &[labelled]);
1395        let mut build = Builder::new(&mut func, labelled);
1396        build.ret(&[]);
1397        assert!(simplify(&mut func).changed());
1398        assert!(blocks(&func).contains(&1), "the labelled block went with the arm");
1399        // The arm had one way in and came up into the entry, which is where the `indirect_br`
1400        // that reaches the labelled block is now.
1401        assert_eq!(blocks(&func), [0, 1]);
1402        assert_eq!(goes_to(&func, 0), [1]);
1403    }
1404
1405    #[test]
1406    fn a_block_only_an_unreachable_block_takes_the_address_of_goes_too() {
1407        // The other half of the same rule. Once the block holding the `block_addr` is gone, the
1408        // address is gone with it, and the block it named is reached by nothing.
1409        let mut names = Interner::new();
1410        let mut func = Func::new(names.intern("f"), Signature::new());
1411        let entry = func.create_block();
1412        let dead = func.create_block();
1413        let labelled = func.create_block();
1414        let mut build = Builder::new(&mut func, entry);
1415        let cond = build.iconst(Type::int(1), 1);
1416        build.br_if(cond, entry, &[], dead, &[]);
1417        let mut build = Builder::new(&mut func, dead);
1418        let addr = build.block_addr(labelled);
1419        build.indirect_br(addr, &[labelled]);
1420        let mut build = Builder::new(&mut func, labelled);
1421        build.ret(&[]);
1422        assert!(simplify(&mut func).changed());
1423        assert_eq!(blocks(&func), [0]);
1424    }
1425
1426    #[test]
1427    fn a_block_nothing_reaches_goes_even_when_no_branch_folded() {
1428        // Section 6.5 says this pass is the one that deletes them, and it says so about the
1429        // blocks the front end handed over as well as the ones a fold here stranded. Nothing in
1430        // this function folds, and the block still has to go, because every analysis below reads
1431        // the graph as though it is not there.
1432        let mut func = graph(&[&[], &[]]);
1433        let stats = simplify(&mut func);
1434        assert_eq!(stats.count(Kind::Optimized, super::FOLDED), 0);
1435        assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 1);
1436        assert_eq!(blocks(&func), [0]);
1437    }
1438
1439    #[test]
1440    fn a_block_with_one_way_into_it_goes_into_the_block_above_it() {
1441        // Something in each of the first two blocks, so that this is three blocks for the merge
1442        // rather than two forwarders step three would point past before it got here.
1443        let mut names = Interner::new();
1444        let mut func = Func::new(names.intern("f"), Signature::new());
1445        let entry = func.create_block();
1446        let middle = func.create_block();
1447        let last = func.create_block();
1448        let mut build = Builder::new(&mut func, entry);
1449        build.iconst(Type::int(32), 1);
1450        build.jump(middle, &[]);
1451        let mut build = Builder::new(&mut func, middle);
1452        build.iconst(Type::int(32), 2);
1453        build.jump(last, &[]);
1454        let mut build = Builder::new(&mut func, last);
1455        build.ret(&[]);
1456        let stats = simplify(&mut func);
1457        // A run of three is one chain and not two rounds of one pair, because the block in the
1458        // middle stops being a block partway through.
1459        assert_eq!(stats.count(Kind::Optimized, super::MERGED), 2);
1460        assert_eq!(stats.count(Kind::Optimized, super::FORWARDED), 0);
1461        assert_eq!(blocks(&func), [0]);
1462        assert_eq!(terminator(&func, 0), Opcode::Return);
1463    }
1464
1465    #[test]
1466    fn a_block_with_two_ways_into_it_stays_where_it_is() {
1467        // The join of a diamond nobody can fold. Merging it into either arm would leave the other
1468        // arm branching into the middle of a block.
1469        let mut names = Interner::new();
1470        let signature = Signature::new().with_params(&[Type::int(1)]);
1471        let mut func = Func::new(names.intern("f"), signature);
1472        let entry = func.create_block();
1473        let then_block = func.create_block();
1474        let else_block = func.create_block();
1475        let join = func.create_block();
1476        let cond = func.append_param(entry, Type::int(1));
1477        let mut build = Builder::new(&mut func, entry);
1478        build.br_if(cond, then_block, &[], else_block, &[]);
1479        for (arm, mark) in [(then_block, 111), (else_block, 222)] {
1480            // An arm with something in it, because an empty one is a forwarder and step three
1481            // would take it away before merging ever looked at the join.
1482            let mut build = Builder::new(&mut func, arm);
1483            build.iconst(Type::int(32), mark);
1484            build.jump(join, &[]);
1485        }
1486        let mut build = Builder::new(&mut func, join);
1487        build.ret(&[]);
1488        let stats = simplify(&mut func);
1489        assert!(!stats.changed());
1490        assert_eq!(blocks(&func), [0, 1, 2, 3]);
1491    }
1492
1493    #[test]
1494    fn a_block_above_one_that_does_not_end_in_a_jump_keeps_it() {
1495        // One way into the join, and the block above it is a branch. Merging would take the
1496        // terminator off the other arm.
1497        let mut names = Interner::new();
1498        let signature = Signature::new().with_params(&[Type::int(1)]);
1499        let mut func = Func::new(names.intern("f"), signature);
1500        let entry = func.create_block();
1501        let arm = func.create_block();
1502        let exit = func.create_block();
1503        let cond = func.append_param(entry, Type::int(1));
1504        let mut build = Builder::new(&mut func, entry);
1505        build.br_if(cond, arm, &[], exit, &[]);
1506        for block in [arm, exit] {
1507            let mut build = Builder::new(&mut func, block);
1508            build.ret(&[]);
1509        }
1510        let stats = simplify(&mut func);
1511        assert!(!stats.changed());
1512        assert_eq!(blocks(&func), [0, 1, 2]);
1513    }
1514
1515    #[test]
1516    fn the_entry_block_is_never_the_one_that_moves() {
1517        // A loop back to the entry, so the entry has one way in and the block above it ends in a
1518        // jump, which is every condition but the one that matters. Control arrives at the entry
1519        // and it has to still be there when it does.
1520        let mut names = Interner::new();
1521        let signature = Signature::new().with_params(&[Type::int(1)]);
1522        let mut func = Func::new(names.intern("f"), signature);
1523        let entry = func.create_block();
1524        let latch = func.create_block();
1525        let exit = func.create_block();
1526        let cond = func.append_param(entry, Type::int(1));
1527        let mut build = Builder::new(&mut func, entry);
1528        build.br_if(cond, latch, &[], exit, &[]);
1529        // The body of the loop, which is there so that the latch is a block and not a forwarder.
1530        let mut build = Builder::new(&mut func, latch);
1531        build.iconst(Type::int(32), 1);
1532        build.jump(entry, &[]);
1533        let mut build = Builder::new(&mut func, exit);
1534        build.ret(&[]);
1535        let stats = simplify(&mut func);
1536        assert!(!stats.changed());
1537        assert_eq!(blocks(&func), [0, 1, 2]);
1538    }
1539
1540    #[test]
1541    fn a_block_whose_address_is_taken_is_not_merged_away_either() {
1542        // The same rule as the one about deleting it. Merging it into the block above would take
1543        // the block out of the function, and the `block_addr` would name one that is not there.
1544        let mut names = Interner::new();
1545        let mut func = Func::new(names.intern("f"), Signature::new());
1546        let entry = func.create_block();
1547        let middle = func.create_block();
1548        let labelled = func.create_block();
1549        let mut build = Builder::new(&mut func, entry);
1550        build.block_addr(labelled);
1551        build.jump(middle, &[]);
1552        // Something in the middle block, so that this is a question about merging rather than one
1553        // about the forwarder removal that would otherwise get there first.
1554        let mut build = Builder::new(&mut func, middle);
1555        build.iconst(Type::int(32), 1);
1556        build.jump(labelled, &[]);
1557        let mut build = Builder::new(&mut func, labelled);
1558        build.ret(&[]);
1559        let stats = simplify(&mut func);
1560        // The middle block had one way in and no address, so it came up. The labelled block has
1561        // one way in too, and stayed.
1562        assert_eq!(stats.count(Kind::Optimized, super::MERGED), 1);
1563        assert_eq!(blocks(&func), [0, 2]);
1564    }
1565
1566    #[test]
1567    fn a_block_an_image_names_is_not_merged_away_either() {
1568        // The case with nothing in the function to find. No `block_addr` names this block, only a
1569        // relocation in another section does, so the whole of what keeps it is the name, and a
1570        // merge would leave that name on an instruction in the middle of the block above.
1571        let mut names = Interner::new();
1572        let mut func = Func::new(names.intern("f"), Signature::new());
1573        let entry = func.create_block();
1574        let middle = func.create_block();
1575        let labelled = func.create_block();
1576        Builder::new(&mut func, entry).jump(middle, &[]);
1577        let mut build = Builder::new(&mut func, middle);
1578        build.iconst(Type::int(32), 1);
1579        build.jump(labelled, &[]);
1580        Builder::new(&mut func, labelled).ret(&[]);
1581        func.name_block(labelled, names.intern(".Llbl.0"));
1582        let stats = simplify(&mut func);
1583        assert_eq!(stats.count(Kind::Optimized, super::MERGED), 1);
1584        assert_eq!(blocks(&func), [0, 2]);
1585        assert_eq!(func.block_name(labelled), Some(names.intern(".Llbl.0")));
1586    }
1587
1588    #[test]
1589    fn merging_binds_a_block_parameter_to_the_argument_the_jump_carried() {
1590        let mut names = Interner::new();
1591        let mut func = Func::new(names.intern("f"), Signature::new());
1592        let entry = func.create_block();
1593        let below = func.create_block();
1594        let param = func.append_param(below, Type::int(32));
1595        let mut build = Builder::new(&mut func, entry);
1596        let arg = build.iconst(Type::int(32), 7);
1597        build.jump(below, &[arg]);
1598        let mut build = Builder::new(&mut func, below);
1599        build.ret(&[param]);
1600        assert!(simplify(&mut func).changed());
1601        assert_eq!(blocks(&func), [0]);
1602        let term = func.terminator(entry).expect("the entry has one");
1603        assert_eq!(func[func[term].args], [arg]);
1604    }
1605
1606    #[test]
1607    fn a_chain_of_merges_follows_a_parameter_bound_to_a_parameter() {
1608        // The middle block passes its own parameter down, so the last block's parameter is bound
1609        // to something that is on its way to being the entry's constant. Following the map is
1610        // what makes the second merge worth as much as the first.
1611        let mut names = Interner::new();
1612        let mut func = Func::new(names.intern("f"), Signature::new());
1613        let entry = func.create_block();
1614        let middle = func.create_block();
1615        let last = func.create_block();
1616        let carried = func.append_param(middle, Type::int(32));
1617        let arrived = func.append_param(last, Type::int(32));
1618        let mut build = Builder::new(&mut func, entry);
1619        let arg = build.iconst(Type::int(32), 7);
1620        build.jump(middle, &[arg]);
1621        let mut build = Builder::new(&mut func, middle);
1622        build.jump(last, &[carried]);
1623        let mut build = Builder::new(&mut func, last);
1624        build.ret(&[arrived]);
1625        assert!(simplify(&mut func).changed());
1626        assert_eq!(blocks(&func), [0]);
1627        let term = func.terminator(entry).expect("the entry has one");
1628        assert_eq!(func[func[term].args], [arg]);
1629    }
1630
1631    /// A function whose entry branches on its own parameter into two arms that each hold one
1632    /// instruction and then jump where the caller says, head to tail.
1633    ///
1634    /// Two arms rather than one because almost every question about step three is a question about
1635    /// a block with more than one way in, and something in each arm because an empty arm is itself
1636    /// a forwarder and would answer a different question. The blocks are entry 0, the arms 1 and 2,
1637    /// and whatever the caller builds after that.
1638    fn arms(func: &mut Func) -> (Value, [Block; 2]) {
1639        let entry = func.create_block();
1640        let first = func.create_block();
1641        let second = func.create_block();
1642        let cond = func.append_param(entry, Type::int(1));
1643        let mut build = Builder::new(func, entry);
1644        let carried = build.iconst(Type::int(32), 7);
1645        build.br_if(cond, first, &[], second, &[]);
1646        for (arm, mark) in [(first, 111), (second, 222)] {
1647            let mut build = Builder::new(func, arm);
1648            build.iconst(Type::int(32), mark);
1649        }
1650        (carried, [first, second])
1651    }
1652
1653    /// A function with one `i1` parameter, which is what [`arms`] wants.
1654    fn taking_a_condition() -> Func {
1655        let mut names = Interner::new();
1656        let signature = Signature::new().with_params(&[Type::int(1)]);
1657        Func::new(names.intern("f"), signature)
1658    }
1659
1660    /// The arguments a block's terminator passes on the edge in that place.
1661    fn carries(func: &Func, block: usize, edge: usize) -> Vec<Value> {
1662        let block = Block::from_usize(block);
1663        let term = func.terminator(block).expect("every block here has one");
1664        let call = func.successors(term).nth(edge).expect("the edge is there");
1665        func[call.args].to_vec()
1666    }
1667
1668    #[test]
1669    fn a_block_that_does_nothing_but_jump_stops_being_in_the_way() {
1670        // Section 21.1's edge forwarding. Two arms arrive at a block that only jumps, so the two
1671        // of them go where it was going and it is not there any more.
1672        let mut func = taking_a_condition();
1673        let (_, arms) = arms(&mut func);
1674        let forwarder = func.create_block();
1675        let exit = func.create_block();
1676        for arm in arms {
1677            Builder::new(&mut func, arm).jump(forwarder, &[]);
1678        }
1679        Builder::new(&mut func, forwarder).jump(exit, &[]);
1680        Builder::new(&mut func, exit).ret(&[]);
1681        let stats = simplify(&mut func);
1682        assert_eq!(stats.count(Kind::Optimized, super::FORWARDED), 1);
1683        assert_eq!(blocks(&func), [0, 1, 2, 4]);
1684        assert_eq!(goes_to(&func, 1), [4]);
1685        assert_eq!(goes_to(&func, 2), [4]);
1686    }
1687
1688    #[test]
1689    fn a_forwarder_hands_its_predecessors_the_arguments_it_was_passing() {
1690        // The forwarder was passing something, and taking it out means whoever ends up branching
1691        // to the block below has to pass it instead. Section 21.1's extra condition is about
1692        // exactly this, and a block with no parameters and no instructions cannot be where the
1693        // value came from, so there is nothing further to check. The one way in is through a block
1694        // that goes nowhere else, which keeps the edge off the list of ones that carry a move with
1695        // no block to put it in.
1696        let mut func = taking_a_condition();
1697        let (carried, [arm, above]) = arms(&mut func);
1698        let forwarder = func.create_block();
1699        let exit = func.create_block();
1700        let other = func.append_param(exit, Type::int(32));
1701        let mut build = Builder::new(&mut func, arm);
1702        let mine = build.iconst(Type::int(32), 9);
1703        build.jump(exit, &[mine]);
1704        Builder::new(&mut func, above).jump(forwarder, &[]);
1705        Builder::new(&mut func, forwarder).jump(exit, &[carried]);
1706        Builder::new(&mut func, exit).ret(&[other]);
1707        let stats = simplify(&mut func);
1708        assert_eq!(stats.count(Kind::Optimized, super::FORWARDED), 1);
1709        assert_eq!(blocks(&func), [0, 1, 2, 4]);
1710        // The block above the forwarder is the edge that used to go through it, and it is carrying
1711        // what the forwarder was carrying.
1712        assert_eq!(carries(&func, 2, 0), [carried]);
1713        assert_eq!(carries(&func, 1, 0), [mine]);
1714        // Two edges saying different things, so the parameter is not redundant and stays.
1715        assert_eq!(stats.count(Kind::Optimized, super::SAME_EVERY_WAY), 0);
1716    }
1717
1718    #[test]
1719    fn a_forwarder_carrying_something_on_an_edge_out_of_a_branch_stays() {
1720        // Both ways out of the entry end up at the same block, and that block takes a parameter, so
1721        // the edge through the forwarder is one the back end would have to split again the moment
1722        // the forwarder stopped being there. The block is already the split, in the place the
1723        // layout wants it, so it is left where it is.
1724        let mut func = taking_a_condition();
1725        let (carried, [arm, forwarder]) = arms(&mut func);
1726        let exit = func.create_block();
1727        let other = func.append_param(exit, Type::int(32));
1728        // The second arm is emptied back out, which is what makes it a forwarder at all.
1729        for inst in func.insts(forwarder).collect::<Vec<Inst>>() {
1730            func.remove_inst(inst);
1731        }
1732        let mut build = Builder::new(&mut func, arm);
1733        let mine = build.iconst(Type::int(32), 9);
1734        build.jump(exit, &[mine]);
1735        Builder::new(&mut func, forwarder).jump(exit, &[carried]);
1736        Builder::new(&mut func, exit).ret(&[other]);
1737        let stats = simplify(&mut func);
1738        assert_eq!(stats.count(Kind::Optimized, super::FORWARDED), 0);
1739        assert_eq!(blocks(&func), [0, 1, 2, 3]);
1740    }
1741
1742    #[test]
1743    fn a_forwarder_carrying_nothing_out_of_a_branch_goes_anyway() {
1744        // The same shape with nothing on the edge. There is no move to find a place for, so the
1745        // back end would leave the edge alone and the block is only in the way.
1746        let mut func = taking_a_condition();
1747        let (_, [arm, forwarder]) = arms(&mut func);
1748        let exit = func.create_block();
1749        for inst in func.insts(forwarder).collect::<Vec<Inst>>() {
1750            func.remove_inst(inst);
1751        }
1752        Builder::new(&mut func, arm).jump(exit, &[]);
1753        Builder::new(&mut func, forwarder).jump(exit, &[]);
1754        Builder::new(&mut func, exit).ret(&[]);
1755        let stats = simplify(&mut func);
1756        assert_eq!(stats.count(Kind::Optimized, super::FORWARDED), 1);
1757        assert_eq!(blocks(&func), [0, 1, 3]);
1758    }
1759
1760    #[test]
1761    fn a_block_that_jumps_to_itself_is_not_a_forwarder() {
1762        // Section 21.1 says so in as many words, and the reason is that it does not forward
1763        // anywhere: pointing its predecessors past it would have to point them at it.
1764        let mut names = Interner::new();
1765        let mut func = Func::new(names.intern("f"), Signature::new());
1766        let entry = func.create_block();
1767        let spin = func.create_block();
1768        Builder::new(&mut func, entry).jump(spin, &[]);
1769        Builder::new(&mut func, spin).jump(spin, &[]);
1770        let stats = simplify(&mut func);
1771        assert!(!stats.changed());
1772        assert_eq!(blocks(&func), [0, 1]);
1773    }
1774
1775    #[test]
1776    fn the_entry_block_is_never_the_forwarder_that_goes() {
1777        // The entry doing nothing but jumping is every condition of a forwarder except the one
1778        // that matters. What happens instead is the block below coming up into it, which leaves
1779        // control arriving where it has to arrive.
1780        let mut names = Interner::new();
1781        let mut func = Func::new(names.intern("f"), Signature::new());
1782        let entry = func.create_block();
1783        let below = func.create_block();
1784        Builder::new(&mut func, entry).jump(below, &[]);
1785        let mut build = Builder::new(&mut func, below);
1786        build.iconst(Type::int(32), 1);
1787        build.ret(&[]);
1788        let stats = simplify(&mut func);
1789        assert_eq!(stats.count(Kind::Optimized, super::FORWARDED), 0);
1790        assert_eq!(stats.count(Kind::Optimized, super::MERGED), 1);
1791        assert_eq!(blocks(&func), [0]);
1792    }
1793
1794    #[test]
1795    fn a_block_whose_address_is_taken_is_not_forwarded_past_either() {
1796        // The abnormal edge condition, which in this IR is the edge an `indirect_br` takes. The
1797        // block is arrived at from somewhere the graph reads from the other end, and pointing the
1798        // edges the graph does carry past it would not move that one.
1799        let mut names = Interner::new();
1800        let mut func = Func::new(names.intern("f"), Signature::new());
1801        let entry = func.create_block();
1802        let labelled = func.create_block();
1803        let exit = func.create_block();
1804        let mut build = Builder::new(&mut func, entry);
1805        let addr = build.block_addr(labelled);
1806        build.indirect_br(addr, &[labelled]);
1807        Builder::new(&mut func, labelled).jump(exit, &[]);
1808        let mut build = Builder::new(&mut func, exit);
1809        build.iconst(Type::int(32), 1);
1810        build.ret(&[]);
1811        let stats = simplify(&mut func);
1812        assert_eq!(stats.count(Kind::Optimized, super::FORWARDED), 0);
1813        assert!(blocks(&func).contains(&1), "the labelled block was forwarded past");
1814    }
1815
1816    #[test]
1817    fn a_run_of_forwarders_comes_out_as_one_edge() {
1818        let mut func = taking_a_condition();
1819        let (_, arms) = arms(&mut func);
1820        let first = func.create_block();
1821        let second = func.create_block();
1822        let exit = func.create_block();
1823        for arm in arms {
1824            Builder::new(&mut func, arm).jump(first, &[]);
1825        }
1826        Builder::new(&mut func, first).jump(second, &[]);
1827        Builder::new(&mut func, second).jump(exit, &[]);
1828        Builder::new(&mut func, exit).ret(&[]);
1829        let stats = simplify(&mut func);
1830        assert_eq!(stats.count(Kind::Optimized, super::FORWARDED), 2);
1831        assert_eq!(blocks(&func), [0, 1, 2, 5]);
1832        assert_eq!(goes_to(&func, 1), [5]);
1833        assert_eq!(goes_to(&func, 2), [5]);
1834    }
1835
1836    #[test]
1837    fn a_block_parameter_that_arrives_as_one_value_every_way_in_goes() {
1838        // Section 21.2. The parameter is not carrying anything, it is spelling the constant a
1839        // second way, and document 12 cannot see through the spelling.
1840        let mut func = taking_a_condition();
1841        let (carried, arms) = arms(&mut func);
1842        let join = func.create_block();
1843        let param = func.append_param(join, Type::int(32));
1844        for arm in arms {
1845            Builder::new(&mut func, arm).jump(join, &[carried]);
1846        }
1847        Builder::new(&mut func, join).ret(&[param]);
1848        let stats = simplify(&mut func);
1849        assert_eq!(stats.count(Kind::Optimized, super::SAME_EVERY_WAY), 1);
1850        assert!(func[Block::from_usize(3)].params.is_empty());
1851        // What read the parameter reads the value it was always going to be.
1852        let term = func.terminator(Block::from_usize(3)).expect("the join has one");
1853        assert_eq!(func[func[term].args], [carried]);
1854        // And the argument in its place is off both edges, because a branch that passes more
1855        // arguments than the block takes is one the verifier refuses.
1856        assert!(carries(&func, 1, 0).is_empty());
1857        assert!(carries(&func, 2, 0).is_empty());
1858    }
1859
1860    #[test]
1861    fn a_block_parameter_that_differs_on_one_way_in_stays() {
1862        let mut func = taking_a_condition();
1863        let (carried, arms) = arms(&mut func);
1864        let join = func.create_block();
1865        let param = func.append_param(join, Type::int(32));
1866        let mut build = Builder::new(&mut func, arms[0]);
1867        let mine = build.iconst(Type::int(32), 9);
1868        build.jump(join, &[mine]);
1869        Builder::new(&mut func, arms[1]).jump(join, &[carried]);
1870        Builder::new(&mut func, join).ret(&[param]);
1871        let stats = simplify(&mut func);
1872        assert!(!stats.changed());
1873        assert_eq!(func[Block::from_usize(3)].params, [param]);
1874    }
1875
1876    #[test]
1877    fn a_loop_header_parameter_whose_other_argument_is_itself_is_what_it_started_as() {
1878        // The subtlety section 21.2 spends its second paragraph on. The latch passes the
1879        // parameter back, so reading the arguments literally says two values and says leave it
1880        // alone. A value that can only ever be itself or the initial one was the initial one.
1881        let mut names = Interner::new();
1882        let signature = Signature::new().with_params(&[Type::int(1)]);
1883        let mut func = Func::new(names.intern("f"), signature);
1884        let entry = func.create_block();
1885        let header = func.create_block();
1886        let latch = func.create_block();
1887        let exit = func.create_block();
1888        let cond = func.append_param(entry, Type::int(1));
1889        let param = func.append_param(header, Type::int(32));
1890        let mut build = Builder::new(&mut func, entry);
1891        let init = build.iconst(Type::int(32), 7);
1892        build.jump(header, &[init]);
1893        Builder::new(&mut func, header).br_if(cond, latch, &[], exit, &[]);
1894        let mut build = Builder::new(&mut func, latch);
1895        build.iconst(Type::int(32), 1);
1896        build.jump(header, &[param]);
1897        Builder::new(&mut func, exit).ret(&[param]);
1898        let stats = simplify(&mut func);
1899        assert_eq!(stats.count(Kind::Optimized, super::SAME_EVERY_WAY), 1);
1900        assert!(func[Block::from_usize(1)].params.is_empty());
1901        let term = func.terminator(Block::from_usize(3)).expect("the exit has one");
1902        assert_eq!(func[func[term].args], [init]);
1903    }
1904
1905    #[test]
1906    fn the_entry_blocks_parameters_are_the_functions_and_stay() {
1907        // The entry's parameters arrive from the caller, which is a way in the graph has no edge
1908        // for. A branch back to the entry is one edge out of two, and reading it as though it
1909        // were the only one would replace an argument with whatever the loop happened to pass.
1910        let mut names = Interner::new();
1911        let signature = Signature::new().with_params(&[Type::int(1), Type::int(32)]);
1912        let mut func = Func::new(names.intern("f"), signature);
1913        let entry = func.create_block();
1914        let latch = func.create_block();
1915        let exit = func.create_block();
1916        let cond = func.append_param(entry, Type::int(1));
1917        let x = func.append_param(entry, Type::int(32));
1918        Builder::new(&mut func, entry).br_if(cond, latch, &[], exit, &[]);
1919        let mut build = Builder::new(&mut func, latch);
1920        let one = build.iconst(Type::int(1), 1);
1921        let seven = build.iconst(Type::int(32), 7);
1922        build.jump(entry, &[one, seven]);
1923        Builder::new(&mut func, exit).ret(&[x]);
1924        let stats = simplify(&mut func);
1925        assert!(!stats.changed());
1926        assert_eq!(func[Block::from_usize(0)].params, [cond, x]);
1927    }
1928
1929    #[test]
1930    fn taking_one_parameter_away_is_what_makes_the_next_one_redundant() {
1931        // Section 21.2's reason for a worklist. The last block's parameter arrives as the middle
1932        // block's parameter one way and as the constant the other way, which is two values until
1933        // the middle block's parameter turns out to be that same constant.
1934        let mut func = taking_a_condition();
1935        let (carried, arms) = arms(&mut func);
1936        let join = func.create_block();
1937        let inner = func.append_param(join, Type::int(32));
1938        let left = func.create_block();
1939        let right = func.create_block();
1940        let last = func.create_block();
1941        let outer = func.append_param(last, Type::int(32));
1942        for arm in arms {
1943            Builder::new(&mut func, arm).jump(join, &[carried]);
1944        }
1945        let cond = func[Block::from_usize(0)].params[0];
1946        Builder::new(&mut func, join).br_if(cond, left, &[], right, &[]);
1947        let mut build = Builder::new(&mut func, left);
1948        build.iconst(Type::int(32), 1);
1949        build.jump(last, &[inner]);
1950        let mut build = Builder::new(&mut func, right);
1951        build.iconst(Type::int(32), 2);
1952        build.jump(last, &[carried]);
1953        Builder::new(&mut func, last).ret(&[outer]);
1954        let stats = simplify(&mut func);
1955        assert_eq!(stats.count(Kind::Optimized, super::SAME_EVERY_WAY), 2);
1956        let term = func.terminator(Block::from_usize(6)).expect("the last block has one");
1957        assert_eq!(func[func[term].args], [carried]);
1958    }
1959
1960    #[test]
1961    fn a_forwarder_with_a_parameter_goes_once_the_parameter_does() {
1962        // The two halves of step three being one step. The block passes its own parameter on, so
1963        // it is not a forwarder while it has one, and the parameter is the same value both ways
1964        // in, so it does not have one for long.
1965        let mut func = taking_a_condition();
1966        let (carried, arms) = arms(&mut func);
1967        let forwarder = func.create_block();
1968        let param = func.append_param(forwarder, Type::int(32));
1969        let exit = func.create_block();
1970        let arrived = func.append_param(exit, Type::int(32));
1971        for arm in arms {
1972            Builder::new(&mut func, arm).jump(forwarder, &[carried]);
1973        }
1974        Builder::new(&mut func, forwarder).jump(exit, &[param]);
1975        Builder::new(&mut func, exit).ret(&[arrived]);
1976        let stats = simplify(&mut func);
1977        assert_eq!(stats.count(Kind::Optimized, super::FORWARDED), 1);
1978        // Both of them: the forwarder's, which is what let it go, and the exit's, which arrives
1979        // as the same thing from both arms once the block between them is not there.
1980        assert_eq!(stats.count(Kind::Optimized, super::SAME_EVERY_WAY), 2);
1981        assert_eq!(blocks(&func), [0, 1, 2, 4]);
1982        let term = func.terminator(Block::from_usize(4)).expect("the exit has one");
1983        assert_eq!(func[func[term].args], [carried]);
1984    }
1985
1986    #[test]
1987    fn fuel_stops_step_three_the_same_way_it_stops_the_rest() {
1988        // One unit, and the first thing that asks for it is the parameter, because parameters go
1989        // first within a block. The forwarder then has nothing to spend and stays.
1990        let mut func = taking_a_condition();
1991        let (carried, arms) = arms(&mut func);
1992        let forwarder = func.create_block();
1993        let param = func.append_param(forwarder, Type::int(32));
1994        let exit = func.create_block();
1995        // Arriving at the exit and returned there, so that the forwarder's parameter is one
1996        // something reads and the step that takes the unread ones out leaves it alone.
1997        let arrived = func.append_param(exit, Type::int(32));
1998        for arm in arms {
1999            Builder::new(&mut func, arm).jump(forwarder, &[carried]);
2000        }
2001        Builder::new(&mut func, forwarder).jump(exit, &[param]);
2002        Builder::new(&mut func, exit).ret(&[arrived]);
2003        let stats =
2004            SimplifyCfg.run(&mut func, &mut crate::machine::fixtures::analyses(), &mut Fuel::of(1));
2005        assert_eq!(stats.count(Kind::Optimized, super::SAME_EVERY_WAY), 1);
2006        assert_eq!(stats.count(Kind::Optimized, super::FORWARDED), 0);
2007        assert_eq!(stats.count(Kind::Missed, super::NO_FUEL_FORWARD), 1);
2008        assert_eq!(blocks(&func), [0, 1, 2, 3, 4]);
2009    }
2010
2011    /// A loop that carries a counter and a pointer, and leaves when the pointer reaches `end`.
2012    ///
2013    /// The shape `crate::ivopts` produces once section 28.4 has moved the exit test off the
2014    /// counter: nothing asks the counter anything any more, and the only thing left reading it is
2015    /// the addition that produces what the latch hands back to it.
2016    ///
2017    /// `on_counter` puts the exit test back on the counter, which is the same loop with the
2018    /// counter live, and it is the negative half of every test below.
2019    fn walking_a_pointer(on_counter: bool) -> Func {
2020        let mut names = Interner::new();
2021        let signature = Signature::new().with_params(&[Type::int(64)]);
2022        let mut func = Func::new(names.intern("f"), signature);
2023        let entry = func.create_block();
2024        let head = func.create_block();
2025        let out = func.create_block();
2026        let end = func.append_param(entry, Type::int(64));
2027        let counter = func.append_param(head, Type::int(32));
2028        let pointer = func.append_param(head, Type::int(64));
2029        let mut build = Builder::new(&mut func, entry);
2030        let from_zero = build.iconst(Type::int(32), 0);
2031        let from_start = build.iconst(Type::int(64), 0);
2032        build.jump(head, &[from_zero, from_start]);
2033        let mut build = Builder::new(&mut func, head);
2034        let one = build.iconst(Type::int(32), 1);
2035        let eight = build.iconst(Type::int(64), 8);
2036        let next = build.binary(Opcode::Add, counter, one, Flags::NONE);
2037        let along = build.binary(Opcode::Add, pointer, eight, Flags::NONE);
2038        // The write the loop is there for, so that the pointer is read by something that happens
2039        // whichever way the exit test is written.
2040        let address = build.unary(Opcode::IntToPtr, pointer, Type::PTR);
2041        let info = MemInfo {
2042            size: 8,
2043            align: 8,
2044            order: MemOrder::NotAtomic,
2045            tbaa: None,
2046            owns: 0,
2047            restrict: Restrict::NONE,
2048        };
2049        build.store(eight, address, info, Flags::NONE);
2050        let going = if on_counter {
2051            let limit = build.iconst(Type::int(32), 10);
2052            build.icmp(IntPred::Ne, next, limit)
2053        } else {
2054            build.icmp(IntPred::Ne, along, end)
2055        };
2056        build.br_if(going, head, &[next, along], out, &[]);
2057        Builder::new(&mut func, out).ret(&[]);
2058        func
2059    }
2060
2061    #[test]
2062    fn a_counter_the_loop_stopped_asking_about_stops_going_round() {
2063        let mut func = walking_a_pointer(false);
2064        let stats = simplify(&mut func);
2065        assert_eq!(stats.count(Kind::Optimized, super::NOTHING_READS_IT), 1);
2066        // The pointer stays, because the test that decides whether to go round again reads it.
2067        assert_eq!(func[Block::from_usize(1)].params.len(), 1);
2068        // And the edge that was feeding the counter is carrying one value now instead of two.
2069        assert_eq!(carries(&func, 1, 0).len(), 1);
2070        assert_eq!(carries(&func, 0, 0).len(), 1);
2071    }
2072
2073    #[test]
2074    fn a_counter_the_loop_still_asks_about_goes_round_exactly_as_before() {
2075        let mut func = walking_a_pointer(true);
2076        let stats = simplify(&mut func);
2077        assert_eq!(stats.count(Kind::Optimized, super::NOTHING_READS_IT), 0);
2078        assert_eq!(func[Block::from_usize(1)].params.len(), 2);
2079    }
2080
2081    /// A counter nothing reads, with two instructions behind it rather than one.
2082    ///
2083    /// The addition reads the parameter and the doubling reads the addition, so taking the
2084    /// parameter out strands the first and taking the first out strands the second. The condition
2085    /// is asked about the function's own parameter so that nothing here folds and the loop stays a
2086    /// loop.
2087    fn counting_into_nothing() -> (Func, Value, Value) {
2088        let mut names = Interner::new();
2089        let signature = Signature::new().with_params(&[Type::int(32)]);
2090        let mut func = Func::new(names.intern("f"), signature);
2091        let entry = func.create_block();
2092        let head = func.create_block();
2093        let out = func.create_block();
2094        let limit = func.append_param(entry, Type::int(32));
2095        let counter = func.append_param(head, Type::int(32));
2096        let mut build = Builder::new(&mut func, entry);
2097        let zero = build.iconst(Type::int(32), 0);
2098        build.jump(head, &[zero]);
2099        let mut build = Builder::new(&mut func, head);
2100        let one = build.iconst(Type::int(32), 1);
2101        let next = build.binary(Opcode::Add, counter, one, Flags::NONE);
2102        let twice = build.binary(Opcode::Add, next, next, Flags::NONE);
2103        let going = build.icmp(IntPred::Ne, limit, one);
2104        build.br_if(going, head, &[next], out, &[]);
2105        Builder::new(&mut func, out).ret(&[]);
2106        (func, next, twice)
2107    }
2108
2109    #[test]
2110    fn what_was_reading_a_parameter_nothing_reads_goes_with_it() {
2111        let (mut func, next, twice) = counting_into_nothing();
2112        let stats = simplify(&mut func);
2113        assert_eq!(stats.count(Kind::Optimized, super::NOTHING_READS_IT), 1);
2114        // Both of them, and the second one is the point: it was never reading the parameter, it
2115        // was reading what did, so one walk that only took the direct readers would have left a
2116        // use of a value nothing defines. This is issue 1016.
2117        assert_eq!(lives_in(&func, next), None);
2118        assert_eq!(lives_in(&func, twice), None);
2119    }
2120
2121    #[test]
2122    fn the_functions_own_parameters_stay_whether_or_not_anything_reads_them() {
2123        // The entry's parameters are the signature. Nothing in this function reads the one it
2124        // has, and taking it out would be changing what the function is rather than what it does.
2125        let mut names = Interner::new();
2126        let signature = Signature::new().with_params(&[Type::int(32)]);
2127        let mut func = Func::new(names.intern("f"), signature);
2128        let entry = func.create_block();
2129        func.append_param(entry, Type::int(32));
2130        Builder::new(&mut func, entry).ret(&[]);
2131        let stats = simplify(&mut func);
2132        assert_eq!(stats.count(Kind::Optimized, super::NOTHING_READS_IT), 0);
2133        assert_eq!(func[entry].params.len(), 1);
2134    }
2135
2136    #[test]
2137    fn a_parameter_nothing_reads_costs_one_unit_of_fuel_and_stays_without_it() {
2138        let mut func = walking_a_pointer(false);
2139        let stats =
2140            SimplifyCfg.run(&mut func, &mut crate::machine::fixtures::analyses(), &mut Fuel::of(0));
2141        assert_eq!(stats.count(Kind::Optimized, super::NOTHING_READS_IT), 0);
2142        assert_eq!(stats.count(Kind::Missed, super::NO_FUEL_UNREAD), 1);
2143        assert_eq!(func[Block::from_usize(1)].params.len(), 2);
2144    }
2145
2146    #[test]
2147    fn the_counter_that_went_leaves_the_verifier_nothing_to_complain_about() {
2148        let target = TargetInfo::new(Triple::new(Arch::X86_64, Os::Linux, Env::Gnu));
2149        let mut names = Interner::new();
2150        let mut module = Module::new(names.intern("test.c"), &target);
2151        let mut func = walking_a_pointer(false);
2152        simplify(&mut func);
2153        module.add_func(func);
2154        rucc_ir::verify(&module, &names).expect("taking a parameter out left the function whole");
2155    }
2156
2157    #[test]
2158    fn step_three_leaves_the_verifier_nothing_to_complain_about() {
2159        // Section 21.6 names an argument list that stops matching its block's parameters as the
2160        // most common bug in this document, and both halves of step three change one.
2161        let target = TargetInfo::new(Triple::new(Arch::X86_64, Os::Linux, Env::Gnu));
2162        let mut names = Interner::new();
2163        let mut module = Module::new(names.intern("test.c"), &target);
2164        let mut func = taking_a_condition();
2165        let (carried, arms) = arms(&mut func);
2166        let forwarder = func.create_block();
2167        let param = func.append_param(forwarder, Type::int(32));
2168        let exit = func.create_block();
2169        let arrived = func.append_param(exit, Type::int(32));
2170        let mut build = Builder::new(&mut func, arms[0]);
2171        let mine = build.iconst(Type::int(32), 9);
2172        build.jump(exit, &[mine]);
2173        Builder::new(&mut func, arms[1]).jump(forwarder, &[carried]);
2174        Builder::new(&mut func, forwarder).jump(exit, &[param]);
2175        let mut build = Builder::new(&mut func, exit);
2176        // A reader of the parameter that is not the return, because this function returns nothing
2177        // and the point is that something downstream still has the value it was passed.
2178        build.icmp(IntPred::Eq, arrived, arrived);
2179        build.ret(&[]);
2180        simplify(&mut func);
2181        module.add_func(func);
2182        rucc_ir::verify(&module, &names).expect("step three left the function verifiable");
2183    }
2184
2185    #[test]
2186    fn out_of_fuel_leaves_the_function_exactly_as_it_was() {
2187        let (mut func, _) = diamond(|build| build.iconst(Type::int(1), 1));
2188        let before = blocks(&func);
2189        let stats =
2190            SimplifyCfg.run(&mut func, &mut crate::machine::fixtures::analyses(), &mut Fuel::of(0));
2191        assert!(!stats.changed());
2192        assert_eq!(stats.count(Kind::Missed, super::NO_FUEL), 1);
2193        assert_eq!(terminator(&func, 0), Opcode::BrIf);
2194        assert_eq!(blocks(&func), before);
2195    }
2196
2197    #[test]
2198    fn what_fuel_buys_is_one_whole_change_and_never_half_of_one() {
2199        // Two foldable branches and fuel for one. The half that removes the stranded blocks is
2200        // not charged for, because a limit that could stop between the two halves would leave a
2201        // block nothing reaches and the verifier would refuse the function.
2202        let mut func = graph(&[&[1, 2], &[3, 4], &[5], &[5], &[5], &[]]);
2203        let stats =
2204            SimplifyCfg.run(&mut func, &mut crate::machine::fixtures::analyses(), &mut Fuel::of(1));
2205        assert_eq!(stats.count(Kind::Optimized, super::FOLDED), 1);
2206        assert_eq!(stats.count(Kind::Missed, super::NO_FUEL), 1);
2207        // The entry folded to its first arm, so the second arm is stranded and goes, and the
2208        // block only it reached goes with it.
2209        assert_eq!(blocks(&func), [0, 1, 3, 4, 5]);
2210    }
2211
2212    #[test]
2213    fn the_pass_leaves_the_verifier_nothing_to_complain_about() {
2214        let target = TargetInfo::new(Triple::new(Arch::X86_64, Os::Linux, Env::Gnu));
2215        let mut names = Interner::new();
2216        let mut module = Module::new(names.intern("test.c"), &target);
2217        let mut func = graph(&[&[1, 2], &[3], &[3], &[4, 1], &[]]);
2218        simplify(&mut func);
2219        module.add_func(func);
2220        rucc_ir::verify(&module, &names).expect("the pass left the function verifiable");
2221    }
2222
2223    #[test]
2224    fn the_pass_says_it_preserves_nothing() {
2225        assert_eq!(SimplifyCfg.preserves(), Preserved::NONE);
2226    }
2227}