dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
use std::sync::Arc;

use super::*;
use crate::{
    analysis::{CallGraph, SsaFunctionBuilder, SsaType},
    metadata::typesystem::PointerSize,
    test::helpers::test_assembly_arc,
};

/// Creates a test compiler context.
fn test_context() -> CompilerContext {
    let call_graph = Arc::new(CallGraph::new());
    CompilerContext::new(call_graph)
}

#[test]
fn test_pass_creation() {
    let pass = ConstantPropagationPass::new();
    assert_eq!(pass.name(), "constant-propagation");
    assert!(!pass.description().is_empty());
}

#[test]
fn test_pass_default() {
    let pass = ConstantPropagationPass;
    assert_eq!(pass.name(), "constant-propagation");
}

#[test]
fn test_conv_i32_to_i8() {
    let operand = ConstValue::I32(42);
    assert_eq!(
        operand.convert_to(&SsaType::I8, false, PointerSize::Bit64),
        Some(ConstValue::I8(42))
    );
}

#[test]
fn test_conv_i32_to_i8_truncate() {
    let operand = ConstValue::I32(1000);
    // 1000 truncated to i8 is -24 (1000 & 0xFF = 232, as signed = -24)
    assert_eq!(
        operand.convert_to(&SsaType::I8, false, PointerSize::Bit64),
        Some(ConstValue::I8(-24))
    );
}

#[test]
fn test_conv_i32_to_i64() {
    let operand = ConstValue::I32(-42);
    assert_eq!(
        operand.convert_to(&SsaType::I64, false, PointerSize::Bit64),
        Some(ConstValue::I64(-42))
    );
}

#[test]
fn test_conv_to_bool_nonzero() {
    let operand = ConstValue::I32(42);
    assert_eq!(
        operand.convert_to(&SsaType::Bool, false, PointerSize::Bit64),
        Some(ConstValue::True)
    );
}

#[test]
fn test_conv_to_bool_zero() {
    let operand = ConstValue::I32(0);
    assert_eq!(
        operand.convert_to(&SsaType::Bool, false, PointerSize::Bit64),
        Some(ConstValue::False)
    );
}

#[test]
fn test_conv_to_f32() {
    let operand = ConstValue::I32(42);
    assert_eq!(
        operand.convert_to(&SsaType::F32, false, PointerSize::Bit64),
        Some(ConstValue::F32(42.0))
    );
}

#[test]
fn test_conv_ovf_in_range() {
    let operand = ConstValue::I32(100);
    assert_eq!(
        operand.convert_to_checked(&SsaType::I8, false, PointerSize::Bit64),
        Some(ConstValue::I8(100))
    );
}

#[test]
fn test_conv_ovf_out_of_range() {
    let operand = ConstValue::I32(1000);
    assert_eq!(
        operand.convert_to_checked(&SsaType::I8, false, PointerSize::Bit64),
        None
    ); // Would overflow
}

#[test]
fn test_conv_u8() {
    let operand = ConstValue::I32(200);
    assert_eq!(
        operand.convert_to(&SsaType::U8, false, PointerSize::Bit64),
        Some(ConstValue::U8(200))
    );
}

#[test]
fn test_conv_u16() {
    let operand = ConstValue::I32(50000);
    assert_eq!(
        operand.convert_to(&SsaType::U16, false, PointerSize::Bit64),
        Some(ConstValue::U16(50000))
    );
}

#[test]
fn test_conv_u32() {
    let operand = ConstValue::I64(3_000_000_000);
    assert_eq!(
        operand.convert_to(&SsaType::U32, false, PointerSize::Bit64),
        Some(ConstValue::U32(3_000_000_000))
    );
}

#[test]
fn test_conv_u64() {
    let operand = ConstValue::I32(42);
    assert_eq!(
        operand.convert_to(&SsaType::U64, false, PointerSize::Bit64),
        Some(ConstValue::U64(42))
    );
}

#[test]
fn test_conv_f64() {
    let operand = ConstValue::I32(42);
    assert_eq!(
        operand.convert_to(&SsaType::F64, false, PointerSize::Bit64),
        Some(ConstValue::F64(42.0))
    );
}

#[test]
fn test_conv_native_int() {
    let operand = ConstValue::I32(42);
    assert_eq!(
        operand.convert_to(&SsaType::NativeInt, false, PointerSize::Bit64),
        Some(ConstValue::NativeInt(42))
    );
}

#[test]
fn test_conv_native_uint() {
    let operand = ConstValue::I32(42);
    assert_eq!(
        operand.convert_to(&SsaType::NativeUInt, false, PointerSize::Bit64),
        Some(ConstValue::NativeUInt(42))
    );
}

#[test]
fn test_conv_char() {
    let operand = ConstValue::I32(65); // 'A'
    assert_eq!(
        operand.convert_to(&SsaType::Char, false, PointerSize::Bit64),
        Some(ConstValue::U16(65))
    );
}

#[test]
fn test_identity_add_zero() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(0));

    let op = SsaOp::Add {
        dest: v2,
        left: v0,
        right: v1,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    // Result should be a Copy to v0 (since v0 has value 42, not a zero)
    match result {
        Some(AlgebraicResult::Copy { dest, src }) => {
            assert_eq!(dest, v2);
            assert_eq!(src, v0);
        }
        _ => panic!("Expected Copy result"),
    }
}

#[test]
fn test_identity_mul_one() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(1));

    let op = SsaOp::Mul {
        dest: v2,
        left: v0,
        right: v1,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    match result {
        Some(AlgebraicResult::Copy { dest, src }) => {
            assert_eq!(dest, v2);
            assert_eq!(src, v0);
        }
        _ => panic!("Expected Copy result"),
    }
}

#[test]
fn test_identity_and_minus_one() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(-1));

    let op = SsaOp::And {
        dest: v2,
        left: v0,
        right: v1,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    match result {
        Some(AlgebraicResult::Copy { dest, src }) => {
            assert_eq!(dest, v2);
            assert_eq!(src, v0);
        }
        _ => panic!("Expected Copy result"),
    }
}

#[test]
fn test_absorbing_mul_zero() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(0));

    let op = SsaOp::Mul {
        dest: v2,
        left: v0,
        right: v1,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    match result {
        Some(AlgebraicResult::Constant { dest, value }) => {
            assert_eq!(dest, v2);
            assert_eq!(value, ConstValue::I32(0));
        }
        _ => panic!("Expected Constant result"),
    }
}

#[test]
fn test_absorbing_and_zero() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(0));

    let op = SsaOp::And {
        dest: v2,
        left: v0,
        right: v1,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    match result {
        Some(AlgebraicResult::Constant { dest, value }) => {
            assert_eq!(dest, v2);
            assert_eq!(value, ConstValue::I32(0));
        }
        _ => panic!("Expected Constant result"),
    }
}

#[test]
fn test_absorbing_or_minus_one() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(-1));

    let op = SsaOp::Or {
        dest: v2,
        left: v0,
        right: v1,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    match result {
        Some(AlgebraicResult::Constant { dest, value }) => {
            assert_eq!(dest, v2);
            assert_eq!(value, ConstValue::I32(-1));
        }
        _ => panic!("Expected Constant result"),
    }
}

#[test]
fn test_add_ovf_no_overflow() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(100));
    constants.insert(v1, ConstValue::I32(50));

    let op = SsaOp::AddOvf {
        dest: v2,
        left: v0,
        right: v1,
        unsigned: false,
    };

    let result = ConstantPropagationPass::check_overflow_op(&op, &constants, PointerSize::Bit64);
    assert!(result.is_some());
}

#[test]
fn test_mul_ovf_with_zero() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(100));
    constants.insert(v1, ConstValue::I32(0));

    let op = SsaOp::MulOvf {
        dest: v2,
        left: v0,
        right: v1,
        unsigned: false,
    };

    // x * 0 = 0, even with overflow check
    let result = ConstantPropagationPass::check_overflow_op(&op, &constants, PointerSize::Bit64);
    assert_eq!(result, Some((v2, ConstValue::I32(0))));
}

#[test]
fn test_pass_empty_function() {
    let pass = ConstantPropagationPass::new();
    let mut ssa = SsaFunctionBuilder::new(0, 0).build_with(|_f| {});
    let method_token = Token::new(0x0600_0001);
    let ctx = test_context();

    let result = pass.run_on_method(&mut ssa, method_token, &ctx, &test_assembly_arc());
    assert!(result.is_ok());
    assert!(!result.unwrap());
}

#[test]
fn test_pass_simple_folding() {
    let pass = ConstantPropagationPass::new();
    // Create a block with: v0 = 5, v1 = 3, v2 = add v0, v1
    let mut ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
        f.block(0, |b| {
            let v0 = b.const_i32(5);
            let v1 = b.const_i32(3);
            let _v2 = b.add(v0, v1);
            b.ret();
        });
    });

    let method_token = Token::new(0x0600_0001);
    let ctx = test_context();

    let result = pass.run_on_method(&mut ssa, method_token, &ctx, &test_assembly_arc());
    assert!(result.is_ok());

    // Check that v2 was folded to constant 8
    let changed = result.unwrap();
    assert!(changed);

    // Verify the instruction was replaced
    let block = ssa.block(0).unwrap();
    let instr = &block.instructions()[2];
    if let SsaOp::Const { value, .. } = instr.op() {
        assert_eq!(*value, ConstValue::I32(8));
    } else {
        panic!("Expected Const instruction");
    }
}

#[test]
fn test_pass_branch_simplification() {
    let pass = ConstantPropagationPass::new();
    // Block 0: v0 = true, branch v0, B1, B2
    let mut ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
        f.block(0, |b| {
            let cond = b.const_true();
            b.branch(cond, 1, 2);
        });
        f.block(1, |b| b.ret());
        f.block(2, |b| b.ret());
    });

    let method_token = Token::new(0x0600_0001);
    let ctx = test_context();

    let result = pass.run_on_method(&mut ssa, method_token, &ctx, &test_assembly_arc());
    assert!(result.is_ok());

    // Check that branch was simplified to jump
    let block = ssa.block(0).unwrap();
    if let Some(SsaOp::Jump { target }) = block.terminator_op() {
        assert_eq!(*target, 1);
    } else {
        panic!("Expected Jump instruction");
    }
}

#[test]
fn test_pass_switch_simplification() {
    let pass = ConstantPropagationPass::new();
    // Block 0: v0 = 1, switch v0, [B1, B2, B3], default=B4
    let mut ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
        f.block(0, |b| {
            let v0 = b.const_i32(1);
            b.switch(v0, vec![1, 2, 3], 4);
        });
        // Blocks 1-4: return
        f.block(1, |b| b.ret());
        f.block(2, |b| b.ret());
        f.block(3, |b| b.ret());
        f.block(4, |b| b.ret());
    });

    let method_token = Token::new(0x0600_0001);
    let ctx = test_context();

    let result = pass.run_on_method(&mut ssa, method_token, &ctx, &test_assembly_arc());
    assert!(result.is_ok());

    // Check that switch was simplified to jump to target 2 (index 1)
    let block = ssa.block(0).unwrap();
    if let Some(SsaOp::Jump { target }) = block.terminator_op() {
        assert_eq!(*target, 2);
    } else {
        panic!("Expected Jump instruction");
    }
}

#[test]
fn test_constants_cached_in_context() {
    let pass = ConstantPropagationPass::new();
    let (mut ssa, v0) = {
        let mut v0_out = SsaVarId::new();
        let ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| {
                let v0 = b.const_i32(42);
                v0_out = v0;
                b.ret_val(v0);
            });
        });
        (ssa, v0_out)
    };

    let method_token = Token::new(0x0600_0001);
    let ctx = test_context();

    let result = pass.run_on_method(&mut ssa, method_token, &ctx, &test_assembly_arc());
    assert!(result.is_ok());

    // Check that constant was cached
    assert!(ctx.known_value_is(method_token, v0, |v| *v == ConstValue::I32(42)));
}

#[test]
fn test_identity_shl_zero() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(0));

    let op = SsaOp::Shl {
        dest: v2,
        value: v0,
        amount: v1,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    match result {
        Some(AlgebraicResult::Copy { dest, src }) => {
            assert_eq!(dest, v2);
            assert_eq!(src, v0);
        }
        _ => panic!("Expected Copy result"),
    }
}

#[test]
fn test_identity_shr_zero() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(0));

    let op = SsaOp::Shr {
        dest: v2,
        value: v0,
        amount: v1,
        unsigned: false,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    match result {
        Some(AlgebraicResult::Copy { dest, src }) => {
            assert_eq!(dest, v2);
            assert_eq!(src, v0);
        }
        _ => panic!("Expected Copy result"),
    }
}

#[test]
fn test_identity_xor_zero() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(0));

    let op = SsaOp::Xor {
        dest: v2,
        left: v0,
        right: v1,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    match result {
        Some(AlgebraicResult::Copy { dest, src }) => {
            assert_eq!(dest, v2);
            assert_eq!(src, v0);
        }
        _ => panic!("Expected Copy result"),
    }
}

#[test]
fn test_identity_div_one() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(1));

    let op = SsaOp::Div {
        dest: v2,
        left: v0,
        right: v1,
        unsigned: false,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    match result {
        Some(AlgebraicResult::Copy { dest, src }) => {
            assert_eq!(dest, v2);
            assert_eq!(src, v0);
        }
        _ => panic!("Expected Copy result"),
    }
}

#[test]
fn test_identity_sub_zero() {
    let mut constants = HashMap::new();
    let v0 = SsaVarId::new();
    let v1 = SsaVarId::new();
    let v2 = SsaVarId::new();
    constants.insert(v0, ConstValue::I32(42));
    constants.insert(v1, ConstValue::I32(0));

    let op = SsaOp::Sub {
        dest: v2,
        left: v0,
        right: v1,
    };

    let result = ConstantPropagationPass::check_algebraic_identity(&op, &constants);
    match result {
        Some(AlgebraicResult::Copy { dest, src }) => {
            assert_eq!(dest, v2);
            assert_eq!(src, v0);
        }
        _ => panic!("Expected Copy result"),
    }
}

#[test]
fn test_chained_constant_folding() {
    let pass = ConstantPropagationPass::new();
    // v0 = 2, v1 = 3, v2 = v0 + v1, v3 = 2, v4 = v2 * v3, v5 = 0, v6 = v4 + v5
    let (mut ssa, v4) = {
        let mut v4_out = SsaVarId::new();
        let ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| {
                let v0 = b.const_i32(2);
                let v1 = b.const_i32(3);
                let v2 = b.add(v0, v1); // v2 = 5
                let v3 = b.const_i32(2);
                let v4 = b.mul(v2, v3); // v4 = 10
                v4_out = v4;
                let v5 = b.const_i32(0);
                // Now add v4 + 0 (identity) - should fold to 10
                let v6 = b.add(v4, v5);
                b.ret_val(v6);
            });
        });
        (ssa, v4_out)
    };

    let method_token = Token::new(0x0600_0001);
    let ctx = test_context();

    let result = pass.run_on_method(&mut ssa, method_token, &ctx, &test_assembly_arc());
    assert!(result.is_ok());

    // Check that v4 was folded to 10
    assert!(ctx.known_value_is(method_token, v4, |v| *v == ConstValue::I32(10)));
}

#[test]
fn test_branch_false_condition() {
    let pass = ConstantPropagationPass::new();
    // Block 0: v0 = false, branch v0, B1, B2
    let mut ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
        f.block(0, |b| {
            let cond = b.const_false();
            b.branch(cond, 1, 2);
        });
        f.block(1, |b| b.ret());
        f.block(2, |b| b.ret());
    });

    let method_token = Token::new(0x0600_0001);
    let ctx = test_context();

    let result = pass.run_on_method(&mut ssa, method_token, &ctx, &test_assembly_arc());
    assert!(result.is_ok());

    // Check that branch was simplified to jump to false branch (target 2)
    let block = ssa.block(0).unwrap();
    if let Some(SsaOp::Jump { target }) = block.terminator_op() {
        assert_eq!(*target, 2);
    } else {
        panic!("Expected Jump instruction");
    }
}

#[test]
fn test_switch_out_of_range_uses_default() {
    let pass = ConstantPropagationPass::new();
    // Block 0: v0 = 100 (out of range), switch v0, [B1, B2, B3], default=B4
    let mut ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
        f.block(0, |b| {
            let v0 = b.const_i32(100); // Out of range
            b.switch(v0, vec![1, 2, 3], 4);
        });
        // Blocks 1-4: return
        f.block(1, |b| b.ret());
        f.block(2, |b| b.ret());
        f.block(3, |b| b.ret());
        f.block(4, |b| b.ret());
    });

    let method_token = Token::new(0x0600_0001);
    let ctx = test_context();

    let result = pass.run_on_method(&mut ssa, method_token, &ctx, &test_assembly_arc());
    assert!(result.is_ok());

    // Check that switch was simplified to jump to default (target 4)
    let block = ssa.block(0).unwrap();
    if let Some(SsaOp::Jump { target }) = block.terminator_op() {
        assert_eq!(*target, 4);
    } else {
        panic!("Expected Jump instruction");
    }
}

#[test]
fn test_types_match_identical() {
    assert!(ConstantPropagationPass::types_match(
        &SsaType::I32,
        &SsaType::I32
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::I64,
        &SsaType::I64
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::F32,
        &SsaType::F32
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::F64,
        &SsaType::F64
    ));
}

#[test]
fn test_types_match_stack_equivalence() {
    // Small integers promote to I32 on the stack
    assert!(ConstantPropagationPass::types_match(
        &SsaType::I8,
        &SsaType::I32
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::U8,
        &SsaType::I32
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::I16,
        &SsaType::I32
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::U16,
        &SsaType::I32
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::Bool,
        &SsaType::I32
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::Char,
        &SsaType::I32
    ));

    // U32 and I32 are interchangeable
    assert!(ConstantPropagationPass::types_match(
        &SsaType::U32,
        &SsaType::I32
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::I32,
        &SsaType::U32
    ));

    // I64 and U64 are interchangeable
    assert!(ConstantPropagationPass::types_match(
        &SsaType::I64,
        &SsaType::U64
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::U64,
        &SsaType::I64
    ));

    // Native int types
    assert!(ConstantPropagationPass::types_match(
        &SsaType::NativeInt,
        &SsaType::NativeUInt
    ));
    assert!(ConstantPropagationPass::types_match(
        &SsaType::NativeUInt,
        &SsaType::NativeInt
    ));
}

#[test]
fn test_types_no_match() {
    // Floats don't match integers
    assert!(!ConstantPropagationPass::types_match(
        &SsaType::F32,
        &SsaType::I32
    ));
    assert!(!ConstantPropagationPass::types_match(
        &SsaType::I32,
        &SsaType::F32
    ));

    // Different sizes don't match
    assert!(!ConstantPropagationPass::types_match(
        &SsaType::I32,
        &SsaType::I64
    ));
    assert!(!ConstantPropagationPass::types_match(
        &SsaType::I64,
        &SsaType::I32
    ));
}

#[test]
fn test_is_safe_widening_chain_same_sign() {
    // Unsigned widening: source <= inner < outer
    // conv.u32(conv.u8(x)) where x is u8 - both conversions widen
    assert!(ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::U8,  // source
        &SsaType::U8,  // inner target (same as source - no-op)
        &SsaType::U32, // outer target
        true,
        true
    ));
    assert!(ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::U8,  // source
        &SsaType::U16, // inner target
        &SsaType::U64, // outer target
        true,
        true
    ));
    assert!(ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::U16, // source
        &SsaType::U32, // inner target
        &SsaType::U64, // outer target
        true,
        true
    ));

    // Signed widening: source <= inner < outer
    assert!(ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::I8,  // source
        &SsaType::I8,  // inner target
        &SsaType::I32, // outer target
        false,
        false
    ));
    assert!(ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::I8,  // source
        &SsaType::I16, // inner target
        &SsaType::I64, // outer target
        false,
        false
    ));
    assert!(ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::I16, // source
        &SsaType::I32, // inner target
        &SsaType::I64, // outer target
        false,
        false
    ));
}

#[test]
fn test_is_safe_widening_chain_unsigned_to_signed() {
    // unsigned to signed widening is safe (zero extend then reinterpret)
    // conv.i32(conv.u8(x)) where x is u8
    assert!(ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::U8,  // source
        &SsaType::U8,  // inner target
        &SsaType::I32, // outer target
        true,
        false
    ));
    assert!(ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::U8,  // source
        &SsaType::U16, // inner target
        &SsaType::I64, // outer target
        true,
        false
    ));
    assert!(ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::U16, // source
        &SsaType::U32, // inner target
        &SsaType::I64, // outer target
        true,
        false
    ));
}

#[test]
fn test_is_safe_widening_chain_signed_to_unsigned() {
    // signed to unsigned widening is NOT safe (sign extend vs zero extend)
    // conv.u32(conv.i8(x)) where x is i8 - inner is signed, outer is unsigned
    assert!(!ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::I8,  // source
        &SsaType::I8,  // inner target
        &SsaType::U32, // outer target
        false,
        true
    ));
    assert!(!ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::I8,  // source
        &SsaType::I16, // inner target
        &SsaType::U64, // outer target
        false,
        true
    ));
}

#[test]
fn test_is_safe_widening_chain_narrowing_rejected() {
    // Outer narrowing is NOT safe - inner >= outer means data loss
    // conv.i32(conv.i64(x)) where x is i32 - outer narrows from i64 to i32
    assert!(!ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::I32, // source
        &SsaType::I64, // inner target
        &SsaType::I32, // outer target (narrowing!)
        false,
        false
    ));
    // conv.u8(conv.u32(x)) where x is u8 - outer narrows from u32 to u8
    assert!(!ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::U8,  // source
        &SsaType::U32, // inner target
        &SsaType::U8,  // outer target (narrowing!)
        true,
        true
    ));

    // Inner narrowing is also NOT safe - source > inner means truncation
    // conv.i32(conv.u1(x)) where x is i32 - inner truncates i32 to u8
    assert!(!ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::I32, // source (larger than inner!)
        &SsaType::U8,  // inner target (truncates source)
        &SsaType::I32, // outer target
        true,
        false
    ));
}

#[test]
fn test_is_safe_widening_chain_float_rejected() {
    // Float conversions are NOT safe - precision loss
    // conv.f64(conv.f32(x)) - float widening can lose precision
    assert!(!ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::F32, // source
        &SsaType::F32, // inner target
        &SsaType::F64, // outer target
        false,
        false
    ));
    // conv.f64(conv.i32(x)) - int to float conversion
    assert!(!ConstantPropagationPass::is_safe_widening_chain(
        &SsaType::I32, // source
        &SsaType::I32, // inner target
        &SsaType::F64, // outer target
        false,
        false
    ));
}

#[test]
fn test_duplicate_conversion_elimination() {
    // conv.i4(conv.i4(v0)) should become conv.i4(v0)
    let (mut ssa, v0) = {
        let mut v0_out = SsaVarId::new();
        let ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| {
                // v0 = some value (simulated by const for simplicity)
                let v0 = b.const_i64(42);
                v0_out = v0;
                // v1 = conv.i4 v0
                let v1 = b.conv(v0, SsaType::I32);
                // v2 = conv.i4 v1 (duplicate!)
                let v2 = b.conv(v1, SsaType::I32);
                b.ret_val(v2);
            });
        });
        (ssa, v0_out)
    };

    let method_token = Token::new(0x0600_0001);
    let mut changes = EventLog::new();

    ConstantPropagationPass::eliminate_redundant_conversions(&mut ssa, method_token, &mut changes);

    // v2 should now be conv.i4(v0), not conv.i4(v1)
    let block = ssa.block(0).unwrap();
    let instr = &block.instructions()[2];
    if let SsaOp::Conv { operand, .. } = instr.op() {
        assert_eq!(
            *operand, v0,
            "Duplicate conversion should use original operand"
        );
    } else {
        panic!("Expected Conv instruction");
    }
}

#[test]
fn test_widening_chain_elimination() {
    // conv.i8(conv.i4(v0)) should become conv.i8(v0) when unsigned
    let (mut ssa, v0) = {
        let mut v0_out = SsaVarId::new();
        let ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| {
                // v0 = some value
                let v0 = b.const_val(ConstValue::U8(42));
                v0_out = v0;
                // v1 = conv.u4 v0 (u8 -> u32)
                let v1 = b.conv_un(v0, SsaType::U32);
                // v2 = conv.u8 v1 (u32 -> u64 widening chain)
                let v2 = b.conv_un(v1, SsaType::U64);
                b.ret_val(v2);
            });
        });
        (ssa, v0_out)
    };

    let method_token = Token::new(0x0600_0001);
    let mut changes = EventLog::new();

    ConstantPropagationPass::eliminate_redundant_conversions(&mut ssa, method_token, &mut changes);

    // v2 should now be conv.u8(v0), not conv.u8(v1)
    let block = ssa.block(0).unwrap();
    let instr = &block.instructions()[2];
    if let SsaOp::Conv {
        operand, target, ..
    } = instr.op()
    {
        assert_eq!(*operand, v0, "Widening chain should use original operand");
        assert_eq!(*target, SsaType::U64);
    } else {
        panic!("Expected Conv instruction");
    }
}

#[test]
fn test_overflow_checked_conversion_not_eliminated() {
    // conv.ovf.i4(conv.i4(v0)) should NOT eliminate the outer conversion
    let (mut ssa, v1) = {
        let mut v1_out = SsaVarId::new();
        let ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| {
                let v0 = b.const_i64(42);
                // v1 = conv.i4 v0
                let v1 = b.conv(v0, SsaType::I32);
                v1_out = v1;
                // v2 = conv.ovf.i4 v1 (overflow check!)
                let v2 = b.conv_ovf(v1, SsaType::I32);
                b.ret_val(v2);
            });
        });
        (ssa, v1_out)
    };

    let method_token = Token::new(0x0600_0001);
    let mut changes = EventLog::new();

    ConstantPropagationPass::eliminate_redundant_conversions(&mut ssa, method_token, &mut changes);

    // v2 should still be conv.ovf.i4(v1), not modified
    let block = ssa.block(0).unwrap();
    let instr = &block.instructions()[2];
    if let SsaOp::Conv {
        operand,
        overflow_check,
        ..
    } = instr.op()
    {
        assert_eq!(
            *operand, v1,
            "Overflow-checked conversion should not be modified"
        );
        assert!(*overflow_check);
    } else {
        panic!("Expected Conv instruction");
    }
}

#[test]
fn test_float_widening_not_eliminated() {
    // conv.r8(conv.r4(v0)) should NOT be simplified (precision loss)
    let (mut ssa, v1) = {
        let mut v1_out = SsaVarId::new();
        let ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| {
                let v0 = b.const_i32(42);
                // v1 = conv.r4 v0 (int -> float32)
                let v1 = b.conv(v0, SsaType::F32);
                v1_out = v1;
                // v2 = conv.r8 v1 (float32 -> float64, can lose precision info!)
                let v2 = b.conv(v1, SsaType::F64);
                b.ret_val(v2);
            });
        });
        (ssa, v1_out)
    };

    let method_token = Token::new(0x0600_0001);
    let mut changes = EventLog::new();

    ConstantPropagationPass::eliminate_redundant_conversions(&mut ssa, method_token, &mut changes);

    // v2 should still use v1, not v0
    let block = ssa.block(0).unwrap();
    let instr = &block.instructions()[2];
    if let SsaOp::Conv { operand, .. } = instr.op() {
        assert_eq!(
            *operand, v1,
            "Float conversion chain should not be simplified"
        );
    } else {
        panic!("Expected Conv instruction");
    }
}