analyssa 0.1.0

Target-agnostic SSA IR, analyses, and optimization pipeline
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Control flow simplification pass — reduces CFG complexity by removing
//! redundant branches, threading through trampolines, and cleaning up dead
//! code after terminators.
//!
//! # Algorithm
//!
//! Each iteration performs three steps:
//!
//! 1. **Jump threading** — resolves block references through trampoline
//!    chains using [`SsaFunction::find_trampoline_blocks`] and
//!    [`SsaOp::redirect_target`]. Skips the entry block (handled by
//!    block merge).
//! 2. **Branch-to-same-target simplification** — detects `Branch` and
//!    `Switch` terminators where all targets (after trampoline resolution)
//!    point to the same block, reducing them to a `Jump`. Also handles
//!    CFF-style self-loop switches that degenerate to a single non-self
//!    target.
//! 3. **Dead tail removal** — removes instructions that follow a terminator
//!    in the same block (shared with [`crate::passes::deadcode`]).
//!
//! The outer loop iterates until convergence (no changes) or
//! `max_iterations` is reached.

use std::collections::BTreeMap;

use crate::{
    events::{EventKind, EventListener},
    ir::{function::SsaFunction, ops::SsaOp},
    passes::{deadcode::find_dead_tails, utils::resolve_chain},
    target::Target,
};

/// Run control flow simplification on `ssa`.
///
/// Iterates a three-step simplification until convergence or
/// `max_iterations` is reached.
///
/// # Arguments
///
/// * `ssa` — The SSA function to simplify in place.
/// * `method` — Opaque method reference recorded in emitted events.
/// * `events` — Event sink for [`EventKind::ControlFlowRestructured`],
///   [`EventKind::BranchSimplified`], and [`EventKind::InstructionRemoved`].
/// * `max_iterations` — Cap on the outer fixpoint loop.
///
/// # Returns
///
/// `true` if any transformation fired.
pub fn run<T, L>(
    ssa: &mut SsaFunction<T>,
    method: &T::MethodRef,
    events: &L,
    max_iterations: usize,
) -> bool
where
    T: Target,
    L: EventListener<T> + ?Sized,
{
    let mut changed = false;
    for _ in 0..max_iterations {
        if run_iteration(ssa, method, events) == 0 {
            break;
        }
        changed = true;
    }
    changed
}

/// Single iteration of control flow simplification.
///
/// Returns the number of changes made (branches simplified, instructions
/// removed); zero means the algorithm has converged for this iteration.
///
/// # Arguments
///
/// * `ssa` — The SSA function to simplify.
/// * `method` — Opaque method reference recorded in emitted events.
/// * `events` — Event sink for transformation events.
///
/// # Returns
///
/// Count of changes made this iteration.
pub fn run_iteration<T, L>(ssa: &mut SsaFunction<T>, method: &T::MethodRef, events: &L) -> usize
where
    T: Target,
    L: EventListener<T> + ?Sized,
{
    let mut total_changes: usize = 0;

    // Step 1: jump threading (don't skip entry block).
    let trampolines = ssa.find_trampoline_blocks(false);
    if !trampolines.is_empty() {
        total_changes =
            total_changes.saturating_add(apply_jump_threading(ssa, &trampolines, method, events));
    }

    // Step 2: simplify branches to same target (also resolves through trampolines).
    let same_target_branches = find_same_target_branches(ssa, &trampolines);
    if !same_target_branches.is_empty() {
        total_changes = total_changes.saturating_add(simplify_same_target_branches(
            ssa,
            &same_target_branches,
            method,
            events,
        ));
    }

    // Step 3: remove dead tails.
    let dead_tails = find_dead_tails(ssa);
    if !dead_tails.is_empty() {
        total_changes =
            total_changes.saturating_add(remove_dead_tails(ssa, &dead_tails, method, events));
    }

    total_changes
}

fn find_same_target_branches<T: Target>(
    ssa: &SsaFunction<T>,
    trampolines: &BTreeMap<usize, usize>,
) -> Vec<(usize, usize)> {
    ssa.iter_blocks()
        .filter_map(|(block_idx, block)| {
            block.terminator_op().and_then(|op| match op {
                SsaOp::Branch {
                    true_target,
                    false_target,
                    ..
                }
                | SsaOp::BranchCmp {
                    true_target,
                    false_target,
                    ..
                } => {
                    if true_target == false_target {
                        return Some((block_idx, *true_target));
                    }
                    let true_ultimate = resolve_chain(trampolines, *true_target);
                    let false_ultimate = resolve_chain(trampolines, *false_target);
                    if true_ultimate == false_ultimate {
                        Some((block_idx, true_ultimate))
                    } else {
                        None
                    }
                }
                SsaOp::Switch {
                    targets, default, ..
                } => {
                    if targets.iter().all(|t| *t == *default) {
                        return Some((block_idx, *default));
                    }
                    let default_ultimate = resolve_chain(trampolines, *default);
                    if targets
                        .iter()
                        .all(|t| resolve_chain(trampolines, *t) == default_ultimate)
                    {
                        return Some((block_idx, default_ultimate));
                    }
                    // Self-loop elimination: residual CFF in exception
                    // handlers can leave a switch where all cases except one
                    // are self-loops. Degenerates to a jump to the single
                    // non-self target.
                    let non_self: Vec<usize> = targets
                        .iter()
                        .chain(std::iter::once(default))
                        .copied()
                        .filter(|&t| t != block_idx)
                        .collect();
                    if let Some(&first) = non_self.first() {
                        if non_self.iter().all(|t| *t == first) {
                            Some((block_idx, first))
                        } else {
                            None
                        }
                    } else {
                        None
                    }
                }
                _ => None,
            })
        })
        .collect()
}

fn apply_jump_threading<T, L>(
    ssa: &mut SsaFunction<T>,
    trampolines: &BTreeMap<usize, usize>,
    method: &T::MethodRef,
    events: &L,
) -> usize
where
    T: Target,
    L: EventListener<T> + ?Sized,
{
    let ultimate_targets: BTreeMap<usize, usize> = trampolines
        .keys()
        .map(|&t| (t, resolve_chain(trampolines, t)))
        .collect();

    let mut threaded_count: usize = 0;

    let block_count = ssa.block_count();
    for block_idx in 0..block_count {
        if let Some(block) = ssa.block_mut(block_idx) {
            if let Some(last) = block.instructions_mut().last_mut() {
                let op = last.op_mut();
                let old_targets = op.successors();
                let mut changed = false;
                for (&trampoline, &ultimate) in &ultimate_targets {
                    if op.redirect_target(trampoline, ultimate) {
                        changed = true;
                    }
                }
                if changed {
                    let new_targets = op.successors();
                    let event = crate::events::Event {
                        kind: EventKind::ControlFlowRestructured,
                        method: Some(method.clone()),
                        location: Some(block_idx),
                        message: format!("jump threaded: {old_targets:?} -> {new_targets:?}"),
                        pass: None,
                    };
                    events.push(event);
                    threaded_count = threaded_count.saturating_add(1);
                }
            }
        }
    }

    threaded_count
}

fn simplify_same_target_branches<T, L>(
    ssa: &mut SsaFunction<T>,
    same_target_branches: &[(usize, usize)],
    method: &T::MethodRef,
    events: &L,
) -> usize
where
    T: Target,
    L: EventListener<T> + ?Sized,
{
    let mut simplified_count: usize = 0;

    for &(block_idx, target) in same_target_branches {
        if let Some(block) = ssa.block_mut(block_idx) {
            if let Some(last) = block.instructions_mut().last_mut() {
                last.set_op(SsaOp::Jump { target });
                let event = crate::events::Event {
                    kind: EventKind::BranchSimplified,
                    method: Some(method.clone()),
                    location: Some(block_idx),
                    message: format!(
                        "branch to same target simplified: B{block_idx} branch -> jump B{target}"
                    ),
                    pass: None,
                };
                events.push(event);
                simplified_count = simplified_count.saturating_add(1);
            }
        }
    }

    simplified_count
}

fn remove_dead_tails<T, L>(
    ssa: &mut SsaFunction<T>,
    dead_tails: &[(usize, usize)],
    method: &T::MethodRef,
    events: &L,
) -> usize
where
    T: Target,
    L: EventListener<T> + ?Sized,
{
    let mut removed_count: usize = 0;

    for &(block_idx, start_idx) in dead_tails {
        if let Some(block) = ssa.block_mut(block_idx) {
            let instr_count = block.instruction_count();
            let to_remove = instr_count.saturating_sub(start_idx);
            for _ in 0..to_remove {
                block.instructions_mut().pop();
                removed_count = removed_count.saturating_add(1);
            }
            if to_remove > 0 {
                let event = crate::events::Event {
                    kind: EventKind::InstructionRemoved,
                    method: Some(method.clone()),
                    location: Some(block_idx),
                    message: format!(
                        "removed {to_remove} dead instructions after terminator in B{block_idx}"
                    ),
                    pass: None,
                };
                events.push(event);
            }
        }
    }

    removed_count
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        events::EventLog,
        ir::{
            block::SsaBlock,
            instruction::SsaInstruction,
            value::ConstValue,
            variable::{DefSite, SsaVarId, VariableOrigin},
        },
        testing::{MockTarget, MockType},
    };

    fn instr(op: SsaOp<MockTarget>) -> SsaInstruction<MockTarget> {
        SsaInstruction::synthetic(op)
    }

    fn local_at(
        ssa: &mut SsaFunction<MockTarget>,
        idx: u16,
        block: usize,
        instr: usize,
    ) -> SsaVarId {
        ssa.create_variable(
            VariableOrigin::Local(idx),
            0,
            DefSite::instruction(block, instr),
            MockType::I32,
        )
    }

    #[test]
    fn dead_tail_after_return_removed() {
        let mut ssa: SsaFunction<MockTarget> = SsaFunction::new(0, 1);
        let v0 = local_at(&mut ssa, 0, 0, 0);
        let mut b0 = SsaBlock::new(0);
        b0.add_instruction(instr(SsaOp::Return { value: None }));
        b0.add_instruction(instr(SsaOp::Const {
            dest: v0,
            value: ConstValue::I32(999),
        }));
        ssa.add_block(b0);
        ssa.recompute_uses();

        let log: EventLog<MockTarget> = EventLog::new();
        let method = 0u32;
        let changed = run(&mut ssa, &method, &log, 10);
        assert!(changed, "dead tail after return should be removed");
        assert!(log.has(EventKind::InstructionRemoved));
    }

    #[test]
    fn branch_to_same_target_simplified() {
        let mut ssa: SsaFunction<MockTarget> = SsaFunction::new(0, 1);
        let cond = local_at(&mut ssa, 0, 0, 0);
        let mut b0 = SsaBlock::new(0);
        b0.add_instruction(instr(SsaOp::Const {
            dest: cond,
            value: ConstValue::I32(1),
        }));
        b0.add_instruction(instr(SsaOp::Branch {
            condition: cond,
            true_target: 1,
            false_target: 1,
        }));
        ssa.add_block(b0);
        let mut b1 = SsaBlock::new(1);
        b1.add_instruction(instr(SsaOp::Return { value: None }));
        ssa.add_block(b1);
        ssa.recompute_uses();

        let log: EventLog<MockTarget> = EventLog::new();
        let method = 0u32;
        let changed = run(&mut ssa, &method, &log, 10);
        assert!(changed, "branch to same target should be simplified");
        assert!(log.has(EventKind::BranchSimplified));
        // Should now be a Jump
        assert!(matches!(
            ssa.block(0).unwrap().terminator_op().unwrap(),
            SsaOp::Jump { target: 1 }
        ));
    }

    #[test]
    fn jump_thread_through_trampoline() {
        let mut ssa: SsaFunction<MockTarget> = SsaFunction::new(0, 0);
        let mut b0 = SsaBlock::new(0);
        b0.add_instruction(instr(SsaOp::Jump { target: 1 }));
        ssa.add_block(b0);
        let mut b1 = SsaBlock::new(1);
        b1.add_instruction(instr(SsaOp::Jump { target: 2 }));
        ssa.add_block(b1);
        let mut b2 = SsaBlock::new(2);
        b2.add_instruction(instr(SsaOp::Return { value: None }));
        ssa.add_block(b2);
        ssa.recompute_uses();

        let log: EventLog<MockTarget> = EventLog::new();
        let method = 0u32;
        let changed = run(&mut ssa, &method, &log, 10);
        assert!(changed, "jump through trampoline should be threaded");
    }

    #[test]
    fn no_changes_on_well_formed_cfg() {
        let mut ssa: SsaFunction<MockTarget> = SsaFunction::new(0, 1);
        let v0 = local_at(&mut ssa, 0, 0, 0);
        let mut b0 = SsaBlock::new(0);
        b0.add_instruction(instr(SsaOp::Const {
            dest: v0,
            value: ConstValue::I32(0),
        }));
        b0.add_instruction(instr(SsaOp::Return { value: Some(v0) }));
        ssa.add_block(b0);
        ssa.recompute_uses();

        let log: EventLog<MockTarget> = EventLog::new();
        let method = 0u32;
        let changed = run(&mut ssa, &method, &log, 10);
        assert!(!changed, "well-formed CFG should have no changes");
    }

    #[test]
    fn multiple_changes_in_one_run() {
        let mut ssa: SsaFunction<MockTarget> = SsaFunction::new(0, 1);
        let cond = local_at(&mut ssa, 0, 0, 0);
        let mut b0 = SsaBlock::new(0);
        b0.add_instruction(instr(SsaOp::Const {
            dest: cond,
            value: ConstValue::I32(1),
        }));
        b0.add_instruction(instr(SsaOp::Branch {
            condition: cond,
            true_target: 1,
            false_target: 1,
        }));
        b0.add_instruction(instr(SsaOp::Nop)); // dead tail
        ssa.add_block(b0);
        let mut b1 = SsaBlock::new(1);
        b1.add_instruction(instr(SsaOp::Return { value: None }));
        ssa.add_block(b1);
        ssa.recompute_uses();

        let log: EventLog<MockTarget> = EventLog::new();
        let method = 0u32;
        let changed = run(&mut ssa, &method, &log, 10);
        assert!(changed);
    }

    #[test]
    fn dead_tails_in_multiple_blocks() {
        let mut ssa: SsaFunction<MockTarget> = SsaFunction::new(0, 0);
        let mut b0 = SsaBlock::new(0);
        b0.add_instruction(instr(SsaOp::Jump { target: 1 }));
        b0.add_instruction(instr(SsaOp::Nop));
        ssa.add_block(b0);
        let mut b1 = SsaBlock::new(1);
        b1.add_instruction(instr(SsaOp::Return { value: None }));
        b1.add_instruction(instr(SsaOp::Nop));
        ssa.add_block(b1);
        ssa.recompute_uses();

        let log: EventLog<MockTarget> = EventLog::new();
        let method = 0u32;
        let changed = run(&mut ssa, &method, &log, 10);
        assert!(changed);
        assert!(log.count_kind(EventKind::InstructionRemoved) >= 2);
    }

    #[test]
    fn switch_all_targets_same() {
        let mut ssa: SsaFunction<MockTarget> = SsaFunction::new(0, 1);
        let val = local_at(&mut ssa, 0, 0, 0);
        let mut b0 = SsaBlock::new(0);
        b0.add_instruction(instr(SsaOp::Const {
            dest: val,
            value: ConstValue::I32(0),
        }));
        b0.add_instruction(instr(SsaOp::Switch {
            value: val,
            targets: vec![1, 1, 1],
            default: 1,
        }));
        ssa.add_block(b0);
        let mut b1 = SsaBlock::new(1);
        b1.add_instruction(instr(SsaOp::Return { value: None }));
        ssa.add_block(b1);
        ssa.recompute_uses();

        let log: EventLog<MockTarget> = EventLog::new();
        let method = 0u32;
        let changed = run(&mut ssa, &method, &log, 10);
        assert!(changed, "switch with all same targets should simplify");
        assert!(matches!(
            ssa.block(0).unwrap().terminator_op().unwrap(),
            SsaOp::Jump { target: 1 }
        ));
    }

    #[test]
    fn empty_function() {
        let mut ssa: SsaFunction<MockTarget> = SsaFunction::new(0, 0);
        let log: EventLog<MockTarget> = EventLog::new();
        let method = 0u32;
        let changed = run(&mut ssa, &method, &log, 10);
        assert!(!changed);
    }

    #[test]
    fn trampoline_not_skipping_entry_block() {
        let mut ssa: SsaFunction<MockTarget> = SsaFunction::new(0, 0);
        let mut b0 = SsaBlock::new(0);
        b0.add_instruction(instr(SsaOp::Jump { target: 1 }));
        ssa.add_block(b0);
        let mut b1 = SsaBlock::new(1);
        b1.add_instruction(instr(SsaOp::Return { value: None }));
        ssa.add_block(b1);
        ssa.recompute_uses();

        let log: EventLog<MockTarget> = EventLog::new();
        let method = 0u32;
        let changed = run(&mut ssa, &method, &log, 10);
        // B0 is entry trampoline — it has no predecessors so jump threading can't redirect.
        // The entry trampoline handling is done by block merge pass, not control flow.
        let _ = changed;
    }
}