ruchy 4.2.1

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
//! MIR optimization passes
use super::types::{
    BinOp, BlockId, Constant, Function, Local, Operand, Place, Program, Rvalue, Statement,
    Terminator, UnOp,
};
use std::collections::{HashMap, HashSet};
/// Dead Code Elimination pass
pub struct DeadCodeElimination {
    /// Set of live locals
    live_locals: HashSet<Local>,
    /// Set of live blocks
    live_blocks: HashSet<BlockId>,
}
impl Default for DeadCodeElimination {
    fn default() -> Self {
        Self::new()
    }
}
impl DeadCodeElimination {
    /// Create a new DCE pass
    #[must_use]
    /// # Examples
    ///
    /// ```
    /// use ruchy::middleend::mir::optimize::new;
    ///
    /// let result = new(());
    /// assert_eq!(result, Ok(()));
    /// ```
    /// # Examples
    ///
    /// ```
    /// use ruchy::middleend::mir::optimize::new;
    ///
    /// let result = new(());
    /// assert_eq!(result, Ok(()));
    /// ```
    /// # Examples
    ///
    /// ```
    /// use ruchy::middleend::mir::optimize::new;
    ///
    /// let result = new(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn new() -> Self {
        Self {
            live_locals: HashSet::new(),
            live_blocks: HashSet::new(),
        }
    }
    /// Run DCE on a function
    /// Run dead code elimination on a function
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::middleend::mir::optimize::DeadCodeElimination;
    /// let mut dce = DeadCodeElimination::new();
    /// // dce.run(&mut function);
    /// ```
    ///
    /// ```ignore
    /// use ruchy::middleend::mir::optimize::run;
    ///
    /// let result = run(());
    /// assert_eq!(result, Ok(()));
    /// ```
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::middleend::mir::optimize::run;
    ///
    /// let result = run(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn run(&mut self, func: &mut Function) {
        // Mark live locals and blocks
        self.mark_live(func);
        // Remove dead statements
        self.remove_dead_statements(func);
        // Remove dead blocks
        self.remove_dead_blocks(func);
        // Remove dead locals
        self.remove_dead_locals(func);
    }
    /// Mark live locals and blocks
    fn mark_live(&mut self, func: &Function) {
        self.live_locals.clear();
        self.live_blocks.clear();
        // Start from entry block
        let mut worklist = vec![func.entry_block];
        self.live_blocks.insert(func.entry_block);
        while let Some(block_id) = worklist.pop() {
            if let Some(block) = func.blocks.iter().find(|b| b.id == block_id) {
                // Mark locals used in statements
                for stmt in &block.statements {
                    self.mark_statement_live(stmt);
                }
                // Mark locals used in terminator and add successor blocks
                self.mark_terminator_live(&block.terminator, &mut worklist);
            }
        }
        // Mark parameters as live
        for param in &func.params {
            self.live_locals.insert(*param);
        }
    }
    /// Mark locals in a statement as live
    fn mark_statement_live(&mut self, stmt: &Statement) {
        match stmt {
            Statement::Assign(place, rvalue) => {
                self.mark_place_live(place);
                self.mark_rvalue_live(rvalue);
            }
            Statement::StorageLive(local) | Statement::StorageDead(local) => {
                self.live_locals.insert(*local);
            }
            Statement::Nop => {}
        }
    }
    /// Mark locals in a place as live
    fn mark_place_live(&mut self, place: &Place) {
        match place {
            Place::Local(local) => {
                self.live_locals.insert(*local);
            }
            Place::Field(base, _) | Place::Deref(base) => {
                self.mark_place_live(base);
            }
            Place::Index(base, index) => {
                self.mark_place_live(base);
                self.mark_place_live(index);
            }
        }
    }
    /// Mark locals in an rvalue as live
    fn mark_rvalue_live(&mut self, rvalue: &Rvalue) {
        match rvalue {
            Rvalue::Use(operand) | Rvalue::UnaryOp(_, operand) | Rvalue::Cast(_, operand, _) => {
                self.mark_operand_live(operand);
            }
            Rvalue::BinaryOp(_, left, right) => {
                self.mark_operand_live(left);
                self.mark_operand_live(right);
            }
            Rvalue::Ref(_, place) => {
                self.mark_place_live(place);
            }
            Rvalue::Aggregate(_, operands) => {
                for operand in operands {
                    self.mark_operand_live(operand);
                }
            }
            Rvalue::Call(func, args) => {
                self.mark_operand_live(func);
                for arg in args {
                    self.mark_operand_live(arg);
                }
            }
        }
    }
    /// Mark locals in an operand as live
    fn mark_operand_live(&mut self, operand: &Operand) {
        match operand {
            Operand::Copy(place) | Operand::Move(place) => {
                self.mark_place_live(place);
            }
            Operand::Constant(_) => {}
        }
    }
    /// Mark locals in terminator as live and add successor blocks
    fn mark_terminator_live(&mut self, terminator: &Terminator, worklist: &mut Vec<BlockId>) {
        match terminator {
            Terminator::Goto(target) => {
                if self.live_blocks.insert(*target) {
                    worklist.push(*target);
                }
            }
            Terminator::If {
                condition,
                then_block,
                else_block,
            } => {
                self.mark_operand_live(condition);
                if self.live_blocks.insert(*then_block) {
                    worklist.push(*then_block);
                }
                if self.live_blocks.insert(*else_block) {
                    worklist.push(*else_block);
                }
            }
            Terminator::Switch {
                discriminant,
                targets,
                default,
            } => {
                self.mark_operand_live(discriminant);
                for (_, target) in targets {
                    if self.live_blocks.insert(*target) {
                        worklist.push(*target);
                    }
                }
                if let Some(default_block) = default {
                    if self.live_blocks.insert(*default_block) {
                        worklist.push(*default_block);
                    }
                }
            }
            Terminator::Return(operand) => {
                if let Some(op) = operand {
                    self.mark_operand_live(op);
                }
            }
            Terminator::Call {
                func,
                args,
                destination,
            } => {
                self.mark_operand_live(func);
                for arg in args {
                    self.mark_operand_live(arg);
                }
                if let Some((place, target)) = destination {
                    self.mark_place_live(place);
                    if self.live_blocks.insert(*target) {
                        worklist.push(*target);
                    }
                }
            }
            Terminator::Unreachable => {}
        }
    }
    /// Remove dead statements from blocks
    fn remove_dead_statements(&self, func: &mut Function) {
        for block in &mut func.blocks {
            if !self.live_blocks.contains(&block.id) {
                continue;
            }
            block.statements.retain(|stmt| {
                match stmt {
                    Statement::Assign(place, _) => self.is_place_live(place),
                    Statement::StorageLive(local) | Statement::StorageDead(local) => {
                        self.live_locals.contains(local)
                    }
                    Statement::Nop => false, // Always remove nops
                }
            });
        }
    }
    /// Remove dead blocks
    fn remove_dead_blocks(&self, func: &mut Function) {
        func.blocks
            .retain(|block| self.live_blocks.contains(&block.id));
    }
    /// Remove dead locals
    fn remove_dead_locals(&self, func: &mut Function) {
        func.locals
            .retain(|local_decl| self.live_locals.contains(&local_decl.id));
    }
    /// Check if a place is live
    fn is_place_live(&self, place: &Place) -> bool {
        match place {
            Place::Local(local) => self.live_locals.contains(local),
            Place::Field(base, _) | Place::Deref(base) => self.is_place_live(base),
            Place::Index(base, index) => self.is_place_live(base) || self.is_place_live(index),
        }
    }
}
/// Constant Propagation pass
pub struct ConstantPropagation {
    /// Map from locals to their constant values
    constants: HashMap<Local, Constant>,
}
impl Default for ConstantPropagation {
    fn default() -> Self {
        Self::new()
    }
}
impl ConstantPropagation {
    /// Create a new constant propagation pass
    #[must_use]
    pub fn new() -> Self {
        Self {
            constants: HashMap::new(),
        }
    }
    /// Run constant propagation on a function
    pub fn run(&mut self, func: &mut Function) {
        self.constants.clear();
        // Find constant assignments
        for block in &func.blocks {
            for stmt in &block.statements {
                if let Statement::Assign(Place::Local(local), rvalue) = stmt {
                    if let Some(constant) = self.extract_constant(rvalue) {
                        self.constants.insert(*local, constant);
                    }
                }
            }
        }
        // Replace uses of constants
        for block in &mut func.blocks {
            for stmt in &mut block.statements {
                self.propagate_in_statement(stmt);
            }
            self.propagate_in_terminator(&mut block.terminator);
        }
    }
    /// Extract constant from rvalue if possible
    fn extract_constant(&self, rvalue: &Rvalue) -> Option<Constant> {
        match rvalue {
            Rvalue::Use(Operand::Constant(c)) => Some(c.clone()),
            Rvalue::BinaryOp(op, left, right) => self.eval_binary_op(*op, left, right),
            Rvalue::UnaryOp(op, operand) => self.eval_unary_op(*op, operand),
            _ => None,
        }
    }
    /// Evaluate binary operation on constants
    fn eval_binary_op(&self, op: BinOp, left: &Operand, right: &Operand) -> Option<Constant> {
        let left_val = self.get_constant_value(left)?;
        let right_val = self.get_constant_value(right)?;
        match (op, &left_val, &right_val) {
            (BinOp::Add, Constant::Int(a, ty), Constant::Int(b, _)) => {
                Some(Constant::Int(a + b, ty.clone()))
            }
            (BinOp::Sub, Constant::Int(a, ty), Constant::Int(b, _)) => {
                Some(Constant::Int(a - b, ty.clone()))
            }
            (BinOp::Mul, Constant::Int(a, ty), Constant::Int(b, _)) => {
                Some(Constant::Int(a * b, ty.clone()))
            }
            (BinOp::Eq, Constant::Int(a, _), Constant::Int(b, _)) => Some(Constant::Bool(a == b)),
            (BinOp::Lt, Constant::Int(a, _), Constant::Int(b, _)) => Some(Constant::Bool(a < b)),
            (BinOp::And, Constant::Bool(a), Constant::Bool(b)) => Some(Constant::Bool(*a && *b)),
            (BinOp::Or, Constant::Bool(a), Constant::Bool(b)) => Some(Constant::Bool(*a || *b)),
            _ => None,
        }
    }
    /// Evaluate unary operation on constant
    fn eval_unary_op(&self, op: UnOp, operand: &Operand) -> Option<Constant> {
        let val = self.get_constant_value(operand)?;
        match (op, &val) {
            (UnOp::Neg, Constant::Int(i, ty)) => Some(Constant::Int(-i, ty.clone())),
            (UnOp::Not, Constant::Bool(b)) => Some(Constant::Bool(!b)),
            _ => None,
        }
    }
    /// Get constant value for an operand
    fn get_constant_value(&self, operand: &Operand) -> Option<Constant> {
        match operand {
            Operand::Constant(c) => Some(c.clone()),
            Operand::Copy(Place::Local(local)) | Operand::Move(Place::Local(local)) => {
                self.constants.get(local).cloned()
            }
            _ => None,
        }
    }
    /// Propagate constants in a statement
    fn propagate_in_statement(&self, stmt: &mut Statement) {
        if let Statement::Assign(_, rvalue) = stmt {
            self.propagate_in_rvalue(rvalue);
        }
    }
    /// Propagate constants in an rvalue
    fn propagate_in_rvalue(&self, rvalue: &mut Rvalue) {
        match rvalue {
            Rvalue::Use(operand) | Rvalue::UnaryOp(_, operand) => {
                self.propagate_in_operand(operand);
            }
            Rvalue::BinaryOp(_, left, right) => {
                self.propagate_in_operand(left);
                self.propagate_in_operand(right);
            }
            Rvalue::Call(func, args) => {
                self.propagate_in_operand(func);
                for arg in args {
                    self.propagate_in_operand(arg);
                }
            }
            _ => {}
        }
    }
    /// Propagate constants in an operand
    fn propagate_in_operand(&self, operand: &mut Operand) {
        if let Some(constant) = self.get_constant_value(operand) {
            *operand = Operand::Constant(constant);
        }
    }
    /// Propagate constants in a terminator
    fn propagate_in_terminator(&self, terminator: &mut Terminator) {
        match terminator {
            Terminator::If { condition, .. } => {
                self.propagate_in_operand(condition);
            }
            Terminator::Switch { discriminant, .. } => {
                self.propagate_in_operand(discriminant);
            }
            Terminator::Return(Some(operand)) => {
                self.propagate_in_operand(operand);
            }
            Terminator::Call { func, args, .. } => {
                self.propagate_in_operand(func);
                for arg in args {
                    self.propagate_in_operand(arg);
                }
            }
            _ => {}
        }
    }
}
/// Common Subexpression Elimination pass
pub struct CommonSubexpressionElimination {
    /// Map from expressions to locals that compute them
    expressions: HashMap<String, Local>,
}
impl Default for CommonSubexpressionElimination {
    fn default() -> Self {
        Self::new()
    }
}
impl CommonSubexpressionElimination {
    /// Create a new CSE pass
    #[must_use]
    pub fn new() -> Self {
        Self {
            expressions: HashMap::new(),
        }
    }
    /// Run CSE on a function
    pub fn run(&mut self, func: &mut Function) {
        self.expressions.clear();
        for block in &mut func.blocks {
            for stmt in &mut block.statements {
                self.process_statement(stmt);
            }
        }
    }
    /// Process a statement for CSE
    fn process_statement(&mut self, stmt: &mut Statement) {
        if let Statement::Assign(Place::Local(local), rvalue) = stmt {
            let expr_key = self.rvalue_key(rvalue);
            if let Some(existing_local) = self.expressions.get(&expr_key) {
                // Replace with copy from existing local
                *stmt = Statement::Assign(
                    Place::Local(*local),
                    Rvalue::Use(Operand::Copy(Place::Local(*existing_local))),
                );
            } else {
                // Record this expression
                self.expressions.insert(expr_key, *local);
            }
        }
    }
    /// Generate a key for an rvalue
    fn rvalue_key(&self, rvalue: &Rvalue) -> String {
        match rvalue {
            Rvalue::Use(operand) => format!("use({})", self.operand_key(operand)),
            Rvalue::BinaryOp(op, left, right) => {
                format!(
                    "binop({:?}, {}, {})",
                    op,
                    self.operand_key(left),
                    self.operand_key(right)
                )
            }
            Rvalue::UnaryOp(op, operand) => {
                format!("unop({:?}, {})", op, self.operand_key(operand))
            }
            _ => format!("{rvalue:?}"), // Fallback
        }
    }
    /// Generate a key for an operand
    fn operand_key(&self, operand: &Operand) -> String {
        match operand {
            Operand::Copy(place) => format!("copy({})", Self::place_key(place)),
            Operand::Move(place) => format!("move({})", Self::place_key(place)),
            Operand::Constant(c) => format!("const({c:?})"),
        }
    }
    /// Generate a key for a place
    #[allow(clippy::only_used_in_recursion)]
    fn place_key(place: &Place) -> String {
        match place {
            Place::Local(local) => format!("local({})", local.0),
            Place::Field(base, field) => format!("field({}, {})", Self::place_key(base), field.0),
            Place::Index(base, index) => {
                format!(
                    "index({}, {})",
                    Self::place_key(base),
                    Self::place_key(index)
                )
            }
            Place::Deref(base) => format!("deref({})", Self::place_key(base)),
        }
    }
}
/// Run all optimization passes on a function
/// # Examples
///
/// ```ignore
/// use ruchy::middleend::mir::optimize::optimize_function;
///
/// let result = optimize_function(());
/// assert_eq!(result, Ok(()));
/// ```
pub fn optimize_function(func: &mut Function) {
    let mut dce = DeadCodeElimination::new();
    let mut const_prop = ConstantPropagation::new();
    let mut cse = CommonSubexpressionElimination::new();
    // Run multiple rounds for better results
    for _ in 0..3 {
        const_prop.run(func);
        cse.run(func);
        dce.run(func);
    }
}
/// Run all optimization passes on a program
/// # Examples
///
/// ```ignore
/// use ruchy::middleend::mir::optimize::optimize_program;
///
/// let result = optimize_program(());
/// assert_eq!(result, Ok(()));
/// ```
pub fn optimize_program(program: &mut Program) {
    for function in program.functions.values_mut() {
        optimize_function(function);
    }
}

#[cfg(test)]
#[path = "optimize_tests.rs"]
mod tests;
#[cfg(test)]
mod property_tests_optimize {
    use super::*;
    use crate::middleend::mir::{BasicBlock, LocalDecl, Type};
    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(10000))]

        /// Property 1: Entry block always preserved after DCE
        #[test]
        fn prop_dce_preserves_entry_block(num_blocks in 1usize..10) {
            let mut func = Function {
                name: "test".to_string(),
                params: vec![],
                return_ty: Type::Unit,
                locals: vec![],
                blocks: vec![],
                entry_block: BlockId(0),
            };

            // Create blocks
            for i in 0..num_blocks {
                func.blocks.push(BasicBlock {
                    id: BlockId(i),
                    statements: vec![],
                    terminator: if i == num_blocks - 1 {
                        Terminator::Return(None)
                    } else {
                        Terminator::Goto(BlockId(i + 1))
                    },
                });
            }

            let mut dce = DeadCodeElimination::new();
            dce.run(&mut func);

            // Property: entry block must always exist
            prop_assert!(func.blocks.iter().any(|b| b.id == BlockId(0)));
        }

        /// Property 2: Parameters always preserved after DCE
        #[test]
        fn prop_dce_preserves_parameters(num_params in 0usize..5) {
            let params: Vec<Local> = (0..num_params).map(Local).collect();
            let mut func = Function {
                name: "test".to_string(),
                params: params.clone(),
                return_ty: Type::Unit,
                locals: params.iter().map(|&id| LocalDecl {
                    id,
                    ty: Type::I32,
                    mutable: false,
                    name: None,
                }).collect(),
                blocks: vec![BasicBlock {
                    id: BlockId(0),
                    statements: vec![],
                    terminator: Terminator::Return(None),
                }],
                entry_block: BlockId(0),
            };

            let mut dce = DeadCodeElimination::new();
            dce.run(&mut func);

            // Property: all parameters must be in locals after DCE
            for param in &params {
                prop_assert!(func.locals.iter().any(|l| l.id == *param));
            }
        }

        /// Property 3: Optimization never panics
        #[test]
        fn prop_optimization_never_panics(num_locals in 0usize..5, num_blocks in 1usize..5) {
            let mut func = Function {
                name: "test".to_string(),
                params: vec![],
                return_ty: Type::Unit,
                locals: (0..num_locals).map(|i| LocalDecl {
                    id: Local(i),
                    ty: Type::I32,
                    mutable: false,
                    name: None,
                }).collect(),
                blocks: (0..num_blocks).map(|i| BasicBlock {
                    id: BlockId(i),
                    statements: vec![],
                    terminator: if i == num_blocks - 1 {
                        Terminator::Return(None)
                    } else {
                        Terminator::Goto(BlockId(i + 1))
                    },
                }).collect(),
                entry_block: BlockId(0),
            };

            // Property: optimization should never panic
            optimize_function(&mut func);
        }

        /// Property 4: DCE is idempotent
        #[test]
        fn prop_dce_idempotent(num_blocks in 1usize..10) {
            let mut func1 = Function {
                name: "test".to_string(),
                params: vec![],
                return_ty: Type::Unit,
                locals: vec![],
                blocks: (0..num_blocks).map(|i| BasicBlock {
                    id: BlockId(i),
                    statements: vec![],
                    terminator: if i == num_blocks - 1 {
                        Terminator::Return(None)
                    } else {
                        Terminator::Goto(BlockId(i + 1))
                    },
                }).collect(),
                entry_block: BlockId(0),
            };

            let mut func2 = func1.clone();

            let mut dce = DeadCodeElimination::new();
            dce.run(&mut func1);

            let mut dce2 = DeadCodeElimination::new();
            dce2.run(&mut func2);
            dce2.run(&mut func2); // Run twice

            // Property: DCE(DCE(x)) == DCE(x)
            prop_assert_eq!(func1.blocks.len(), func2.blocks.len());
            prop_assert_eq!(func1.locals.len(), func2.locals.len());
        }

        /// Property 5: CSE doesn't change block count
        #[test]
        fn prop_cse_preserves_blocks(num_blocks in 1usize..10) {
            let mut func = Function {
                name: "test".to_string(),
                params: vec![],
                return_ty: Type::Unit,
                locals: vec![],
                blocks: (0..num_blocks).map(|i| BasicBlock {
                    id: BlockId(i),
                    statements: vec![],
                    terminator: if i == num_blocks - 1 {
                        Terminator::Return(None)
                    } else {
                        Terminator::Goto(BlockId(i + 1))
                    },
                }).collect(),
                entry_block: BlockId(0),
            };

            let original_block_count = func.blocks.len();

            let mut cse = CommonSubexpressionElimination::new();
            cse.run(&mut func);

            // Property: CSE only changes statements, not control flow
            prop_assert_eq!(func.blocks.len(), original_block_count);
        }

        /// Property 6: Constant propagation doesn't create new locals
        #[test]
        fn prop_const_prop_no_new_locals(num_locals in 0usize..10) {
            let mut func = Function {
                name: "test".to_string(),
                params: vec![],
                return_ty: Type::Unit,
                locals: (0..num_locals).map(|i| LocalDecl {
                    id: Local(i),
                    ty: Type::I32,
                    mutable: false,
                    name: None,
                }).collect(),
                blocks: vec![BasicBlock {
                    id: BlockId(0),
                    statements: vec![],
                    terminator: Terminator::Return(None),
                }],
                entry_block: BlockId(0),
            };

            let original_local_count = func.locals.len();

            let mut const_prop = ConstantPropagation::new();
            const_prop.run(&mut func);

            // Property: const prop only replaces, never adds locals
            prop_assert_eq!(func.locals.len(), original_local_count);
        }

        /// Property 7: Binary operation folding correctness
        #[test]
        fn prop_const_fold_add_correct(a in -100i128..100, b in -100i128..100) {
            let const_prop = ConstantPropagation::new();
            let result = const_prop.eval_binary_op(
                BinOp::Add,
                &Operand::Constant(Constant::Int(a, Type::I64)),
                &Operand::Constant(Constant::Int(b, Type::I64)),
            );

            if let Some(Constant::Int(val, _)) = result {
                prop_assert_eq!(val, a + b, "Addition should be correct");
            }
        }

        /// Property 8: Binary operation folding correctness - subtraction
        #[test]
        fn prop_const_fold_sub_correct(a in -100i128..100, b in -100i128..100) {
            let const_prop = ConstantPropagation::new();
            let result = const_prop.eval_binary_op(
                BinOp::Sub,
                &Operand::Constant(Constant::Int(a, Type::I64)),
                &Operand::Constant(Constant::Int(b, Type::I64)),
            );

            if let Some(Constant::Int(val, _)) = result {
                prop_assert_eq!(val, a - b, "Subtraction should be correct");
            }
        }
    }
}