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
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
//! Constant evaluation for SSA operations.
//!
//! This module provides unified constant folding capabilities for SSA analysis.
//! The [`ConstEvaluator`] can be used by multiple passes (unflattening, decryption,
//! SCCP, etc.) to evaluate SSA operations to constant values.
//!
//! # Algorithm
//!
//! Evaluation is demand-driven and depth-limited:
//!
//! 1. **Cache check**: Results are memoized per variable for O(1) repeated queries.
//! 2. **Cycle detection**: A `BitSet` tracks variables currently being evaluated.
//!    If a variable is encountered while already on the recursion stack, evaluation
//!    returns `None` (avoiding infinite recursion through phi cycles).
//! 3. **Depth limiting**: A configurable maximum recursion depth (default 20) prevents
//!    stack overflow on long dependency chains.
//! 4. **Operation dispatch**: `evaluate_const_op` handles all pure arithmetic, bitwise,
//!    comparison, overflow-checked, and conversion operations by resolving operands
//!    through a caller-provided closure.
//!
//! ## Operations Handled
//!
//! - Arithmetic: Add, Sub, Mul, Div, Rem, Neg
//! - Bitwise: And, Or, Xor, Not, Shl, Shr
//! - Comparisons: Ceq, Clt, Cgt (signed + unsigned)
//! - Overflow-checked: AddOvf, SubOvf, MulOvf
//! - Type conversions: Conv (checked and unchecked)
//! - Copy propagation: trace-through to source
//! - Constants: direct return of Const value
//!
//! ## Operations NOT Handled
//!
//! - Calls, loads, stores, and other side-effecting operations always return `None`.
//! - `Copy` is handled by the evaluator wrapper but not by the shared `evaluate_const_op`
//!   function (callers must handle it first).
//!
//! # Complexity
//!
//! O(d) per variable where d is the depth of the definition chain (bounded by
//! `max_depth`). Caching ensures each variable is evaluated at most once.
//!
//! # Example
//!
//! ```rust
//! use analyssa::{analysis::ConstEvaluator, ir::{ConstValue, SsaVarId}, testing, PointerSize};
//!
//! let ssa = testing::const_i32_return(42);
//! let mut evaluator = ConstEvaluator::new(&ssa, PointerSize::Bit64);
//! let state_var = SsaVarId::from_index(0);
//!
//! // Inject known values from external analysis
//! evaluator.set_known(state_var, ConstValue::I32(42));
//!
//! // Evaluate a variable
//! if let Some(value) = evaluator.evaluate_var(state_var) {
//!     println!("Variable evaluates to: {:?}", value);
//! }
//!
//! // Get all computed constants
//! let constants = evaluator.into_results();
//! ```

use std::collections::HashMap;

use crate::{
    ir::{function::SsaFunction, ops::SsaOp, value::ConstValue, variable::SsaVarId},
    target::Target,
    BitSet, PointerSize,
};

/// Evaluates SSA operations to constant values.
///
/// This provides a unified implementation of constant folding that can be
/// used by multiple passes (unflattening, decryption, SCCP, etc.).
///
/// # Features
///
/// - Caches results for efficiency
/// - Detects cycles to prevent infinite recursion
/// - Supports injecting known values from external analysis
/// - Configurable depth limit
pub struct ConstEvaluator<'a, T: Target> {
    /// Reference to the SSA function being analyzed.
    ssa: &'a SsaFunction<T>,

    /// Cache of evaluated constants.
    /// `Some(value)` means the variable evaluates to that constant.
    /// `None` means the variable was evaluated but is not constant.
    cache: HashMap<SsaVarId, Option<ConstValue<T>>>,

    /// Variables currently being evaluated (for cycle detection).
    visiting: BitSet,

    /// Maximum recursion depth.
    max_depth: usize,

    /// Target pointer size for native int/uint masking.
    pointer_size: PointerSize,
}

impl<'a, T: Target> ConstEvaluator<'a, T> {
    /// Default maximum recursion depth.
    const DEFAULT_MAX_DEPTH: usize = 20;

    /// Creates a new evaluator with default settings.
    ///
    /// # Arguments
    ///
    /// * `ssa` - The SSA function to evaluate.
    /// * `ptr_size` - Target pointer size for native int/uint masking.
    #[must_use]
    pub fn new(ssa: &'a SsaFunction<T>, ptr_size: PointerSize) -> Self {
        Self::with_max_depth(ssa, Self::DEFAULT_MAX_DEPTH, ptr_size)
    }

    /// Creates an evaluator with a custom depth limit.
    ///
    /// # Arguments
    ///
    /// * `ssa` - The SSA function to evaluate.
    /// * `max_depth` - Maximum recursion depth for evaluation.
    /// * `ptr_size` - Target pointer size for native int/uint masking.
    #[must_use]
    pub fn with_max_depth(
        ssa: &'a SsaFunction<T>,
        max_depth: usize,
        ptr_size: PointerSize,
    ) -> Self {
        Self {
            ssa,
            cache: HashMap::new(),
            visiting: BitSet::new(ssa.variable_count().max(1)),
            max_depth,
            pointer_size: ptr_size,
        }
    }

    /// Injects a known value from external analysis.
    ///
    /// This allows passes to provide values discovered through other means
    /// (e.g., from `ctx.known_values` in decryption). Injected values take
    /// precedence over computed values.
    ///
    /// # Arguments
    ///
    /// * `var` - The variable to set.
    /// * `value` - The known constant value.
    pub fn set_known(&mut self, var: SsaVarId, value: ConstValue<T>) {
        self.cache.insert(var, Some(value));
    }

    /// Evaluates a variable to a constant if possible.
    ///
    /// Results are cached, so repeated calls with the same variable are O(1).
    ///
    /// # Arguments
    ///
    /// * `var` - The SSA variable to evaluate.
    ///
    /// # Returns
    ///
    /// The constant value if the variable can be evaluated, `None` otherwise.
    pub fn evaluate_var(&mut self, var: SsaVarId) -> Option<ConstValue<T>> {
        self.evaluate_var_depth(var, 0)
    }

    /// Internal evaluation with depth tracking.
    fn evaluate_var_depth(&mut self, var: SsaVarId, depth: usize) -> Option<ConstValue<T>> {
        // Check depth limit
        if depth > self.max_depth {
            return None;
        }

        // Check cache first
        if let Some(cached) = self.cache.get(&var) {
            return cached.clone();
        }

        // Cycle detection
        if var.index() < self.visiting.len() && self.visiting.contains(var.index()) {
            return None;
        }

        // Mark as visiting
        if var.index() < self.visiting.len() {
            self.visiting.insert(var.index());
        }

        // Get definition and evaluate
        let result = self
            .ssa
            .get_definition(var)
            .and_then(|op| self.evaluate_op_depth(op, depth));

        // Remove from visiting set
        if var.index() < self.visiting.len() {
            self.visiting.remove(var.index());
        }

        // Cache the result
        self.cache.insert(var, result.clone());

        result
    }

    /// Evaluates an SSA operation to a constant if possible.
    ///
    /// # Arguments
    ///
    /// * `op` - The SSA operation to evaluate.
    ///
    /// # Returns
    ///
    /// The constant value if the operation can be evaluated, `None` otherwise.
    pub fn evaluate_op(&mut self, op: &SsaOp<T>) -> Option<ConstValue<T>> {
        self.evaluate_op_depth(op, 0)
    }

    /// Internal operation evaluation with depth tracking.
    fn evaluate_op_depth(&mut self, op: &SsaOp<T>, depth: usize) -> Option<ConstValue<T>> {
        // Check depth limit
        if depth > self.max_depth {
            return None;
        }

        // Copy needs recursive evaluation that the shared helper cannot provide,
        // because it resolves a variable rather than performing arithmetic.
        if let SsaOp::Copy { src, .. } = op {
            return self.evaluate_var_depth(*src, depth.saturating_add(1));
        }

        let ptr_size = self.pointer_size;
        evaluate_const_op(
            op,
            |var| self.evaluate_var_depth(var, depth.saturating_add(1)),
            ptr_size,
        )
    }

    /// Returns all computed constants.
    ///
    /// This consumes the evaluator and returns a map of all variables
    /// that were successfully evaluated to constants.
    #[must_use]
    pub fn into_results(self) -> HashMap<SsaVarId, ConstValue<T>> {
        self.cache
            .into_iter()
            .filter_map(|(var, opt)| opt.map(|val| (var, val)))
            .collect()
    }

    /// Returns a reference to the SSA function being evaluated.
    #[must_use]
    pub fn ssa(&self) -> &SsaFunction<T> {
        self.ssa
    }

    /// Clears the evaluation cache.
    ///
    /// This is useful if the SSA function has been modified and
    /// cached results are no longer valid.
    pub fn clear_cache(&mut self) {
        self.cache.clear();
    }
}

/// Evaluates an SSA operation to a constant value using the provided operand resolver.
///
/// This is the shared arithmetic dispatch for constant evaluation. It handles all
/// pure arithmetic, bitwise, comparison, overflow-checked, and conversion operations.
/// Callers provide a `get_const` closure that resolves an [`SsaVarId`] to its constant
/// value (if known).
///
/// # Operations not handled
///
/// - `Copy` — requires variable-level resolution (trace-through), not arithmetic.
///   Callers should handle `Copy` before calling this function.
/// - Calls, loads, stores, and other side-effecting operations — always returns `None`.
///
/// # Arguments
///
/// * `op` - The SSA operation to evaluate.
/// * `get_const` - Closure that resolves a variable to its constant value.
/// * `ptr_size` - Target pointer size for native int/uint masking.
///
/// # Returns
///
/// The constant result if all operands resolve and the operation succeeds, `None` otherwise.
pub fn evaluate_const_op<T: Target>(
    op: &SsaOp<T>,
    mut get_const: impl FnMut(SsaVarId) -> Option<ConstValue<T>>,
    ptr_size: PointerSize,
) -> Option<ConstValue<T>> {
    match op {
        SsaOp::Const { value, .. } => Some(value.clone()),

        // Binary arithmetic
        SsaOp::Add { left, right, .. } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.add(&r, ptr_size)
        }
        SsaOp::Sub { left, right, .. } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.sub(&r, ptr_size)
        }
        SsaOp::Mul { left, right, .. } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.mul(&r, ptr_size)
        }
        SsaOp::Div { left, right, .. } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.div(&r, ptr_size)
        }
        SsaOp::Rem { left, right, .. } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.rem(&r, ptr_size)
        }

        // Bitwise
        SsaOp::Xor { left, right, .. } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.bitwise_xor(&r, ptr_size)
        }
        SsaOp::And { left, right, .. } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.bitwise_and(&r, ptr_size)
        }
        SsaOp::Or { left, right, .. } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.bitwise_or(&r, ptr_size)
        }

        // Shifts
        SsaOp::Shl { value, amount, .. } => {
            let v = get_const(*value)?;
            let a = get_const(*amount)?;
            v.shl(&a, ptr_size)
        }
        SsaOp::Shr {
            value,
            amount,
            unsigned,
            ..
        } => {
            let v = get_const(*value)?;
            let a = get_const(*amount)?;
            v.shr(&a, *unsigned, ptr_size)
        }

        // Unary
        SsaOp::Neg { operand, .. } => {
            let v = get_const(*operand)?;
            v.negate(ptr_size)
        }
        SsaOp::Not { operand, .. } => {
            let v = get_const(*operand)?;
            v.bitwise_not(ptr_size)
        }

        // Comparisons
        SsaOp::Ceq { left, right, .. } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.ceq(&r)
        }
        SsaOp::Clt {
            left,
            right,
            unsigned,
            ..
        } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            if *unsigned {
                l.clt_un(&r)
            } else {
                l.clt(&r)
            }
        }
        SsaOp::Cgt {
            left,
            right,
            unsigned,
            ..
        } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            if *unsigned {
                l.cgt_un(&r)
            } else {
                l.cgt(&r)
            }
        }

        // Overflow-checked arithmetic
        SsaOp::AddOvf {
            left,
            right,
            unsigned,
            ..
        } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.add_checked(&r, *unsigned, ptr_size)
        }
        SsaOp::SubOvf {
            left,
            right,
            unsigned,
            ..
        } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.sub_checked(&r, *unsigned, ptr_size)
        }
        SsaOp::MulOvf {
            left,
            right,
            unsigned,
            ..
        } => {
            let l = get_const(*left)?;
            let r = get_const(*right)?;
            l.mul_checked(&r, *unsigned, ptr_size)
        }

        // Type conversion
        SsaOp::Conv {
            operand,
            target,
            overflow_check,
            unsigned,
            ..
        } => {
            let v = get_const(*operand)?;
            let ptr_bytes = ptr_size.bytes() as u32;
            if *overflow_check {
                v.convert_to_checked(target, *unsigned, ptr_bytes)
            } else {
                v.convert_to(target, *unsigned, ptr_bytes)
            }
        }

        // Rotates
        SsaOp::Rol { value, amount, .. } => {
            let v = get_const(*value)?;
            let a = get_const(*amount)?;
            let shift = a.as_i32()? as u32;
            match v {
                ConstValue::I8(v) => Some(ConstValue::I8(v.rotate_left(shift))),
                ConstValue::I16(v) => Some(ConstValue::I16(v.rotate_left(shift))),
                ConstValue::I32(v) => Some(ConstValue::I32(v.rotate_left(shift))),
                ConstValue::I64(v) => Some(ConstValue::I64(v.rotate_left(shift))),
                ConstValue::U8(v) => Some(ConstValue::U8(v.rotate_left(shift))),
                ConstValue::U16(v) => Some(ConstValue::U16(v.rotate_left(shift))),
                ConstValue::U32(v) => Some(ConstValue::U32(v.rotate_left(shift))),
                ConstValue::U64(v) => Some(ConstValue::U64(v.rotate_left(shift))),
                ConstValue::NativeInt(v) => Some(ConstValue::NativeInt(v.rotate_left(shift))),
                ConstValue::NativeUInt(v) => Some(ConstValue::NativeUInt(v.rotate_left(shift))),
                _ => None,
            }
        }
        SsaOp::Ror { value, amount, .. } => {
            let v = get_const(*value)?;
            let a = get_const(*amount)?;
            let shift = a.as_i32()? as u32;
            match v {
                ConstValue::I8(v) => Some(ConstValue::I8(v.rotate_right(shift))),
                ConstValue::I16(v) => Some(ConstValue::I16(v.rotate_right(shift))),
                ConstValue::I32(v) => Some(ConstValue::I32(v.rotate_right(shift))),
                ConstValue::I64(v) => Some(ConstValue::I64(v.rotate_right(shift))),
                ConstValue::U8(v) => Some(ConstValue::U8(v.rotate_right(shift))),
                ConstValue::U16(v) => Some(ConstValue::U16(v.rotate_right(shift))),
                ConstValue::U32(v) => Some(ConstValue::U32(v.rotate_right(shift))),
                ConstValue::U64(v) => Some(ConstValue::U64(v.rotate_right(shift))),
                ConstValue::NativeInt(v) => Some(ConstValue::NativeInt(v.rotate_right(shift))),
                ConstValue::NativeUInt(v) => Some(ConstValue::NativeUInt(v.rotate_right(shift))),
                _ => None,
            }
        }
        SsaOp::Rcl { value, amount, .. } => {
            let v = get_const(*value)?;
            let a = get_const(*amount)?;
            let shift = a.as_i32()? as u32;
            match v {
                ConstValue::I8(v) => Some(ConstValue::I8(v.rotate_left(shift))),
                ConstValue::I16(v) => Some(ConstValue::I16(v.rotate_left(shift))),
                ConstValue::I32(v) => Some(ConstValue::I32(v.rotate_left(shift))),
                ConstValue::I64(v) => Some(ConstValue::I64(v.rotate_left(shift))),
                ConstValue::U8(v) => Some(ConstValue::U8(v.rotate_left(shift))),
                ConstValue::U16(v) => Some(ConstValue::U16(v.rotate_left(shift))),
                ConstValue::U32(v) => Some(ConstValue::U32(v.rotate_left(shift))),
                ConstValue::U64(v) => Some(ConstValue::U64(v.rotate_left(shift))),
                ConstValue::NativeInt(v) => Some(ConstValue::NativeInt(v.rotate_left(shift))),
                ConstValue::NativeUInt(v) => Some(ConstValue::NativeUInt(v.rotate_left(shift))),
                _ => None,
            }
        }
        SsaOp::Rcr { value, amount, .. } => {
            let v = get_const(*value)?;
            let a = get_const(*amount)?;
            let shift = a.as_i32()? as u32;
            match v {
                ConstValue::I8(v) => Some(ConstValue::I8(v.rotate_right(shift))),
                ConstValue::I16(v) => Some(ConstValue::I16(v.rotate_right(shift))),
                ConstValue::I32(v) => Some(ConstValue::I32(v.rotate_right(shift))),
                ConstValue::I64(v) => Some(ConstValue::I64(v.rotate_right(shift))),
                ConstValue::U8(v) => Some(ConstValue::U8(v.rotate_right(shift))),
                ConstValue::U16(v) => Some(ConstValue::U16(v.rotate_right(shift))),
                ConstValue::U32(v) => Some(ConstValue::U32(v.rotate_right(shift))),
                ConstValue::U64(v) => Some(ConstValue::U64(v.rotate_right(shift))),
                ConstValue::NativeInt(v) => Some(ConstValue::NativeInt(v.rotate_right(shift))),
                ConstValue::NativeUInt(v) => Some(ConstValue::NativeUInt(v.rotate_right(shift))),
                _ => None,
            }
        }

        // Byte/bit manipulation
        SsaOp::BSwap { src, .. } => {
            let v = get_const(*src)?;
            match v {
                ConstValue::I16(v) => Some(ConstValue::I16(v.swap_bytes())),
                ConstValue::U16(v) => Some(ConstValue::U16(v.swap_bytes())),
                ConstValue::I32(v) => Some(ConstValue::I32(v.swap_bytes())),
                ConstValue::U32(v) => Some(ConstValue::U32(v.swap_bytes())),
                ConstValue::I64(v) => Some(ConstValue::I64(v.swap_bytes())),
                ConstValue::U64(v) => Some(ConstValue::U64(v.swap_bytes())),
                _ => None,
            }
        }
        SsaOp::BRev { src, .. } => {
            let v = get_const(*src)?;
            match v {
                ConstValue::I8(v) => Some(ConstValue::I8(v.reverse_bits())),
                ConstValue::U8(v) => Some(ConstValue::U8(v.reverse_bits())),
                ConstValue::I16(v) => Some(ConstValue::I16(v.reverse_bits())),
                ConstValue::U16(v) => Some(ConstValue::U16(v.reverse_bits())),
                ConstValue::I32(v) => Some(ConstValue::I32(v.reverse_bits())),
                ConstValue::U32(v) => Some(ConstValue::U32(v.reverse_bits())),
                ConstValue::I64(v) => Some(ConstValue::I64(v.reverse_bits())),
                ConstValue::U64(v) => Some(ConstValue::U64(v.reverse_bits())),
                _ => None,
            }
        }

        // Bit scan
        SsaOp::BitScanForward { src, .. } => {
            let v = get_const(*src)?;
            let bits = match v {
                ConstValue::I8(v) => v.trailing_zeros(),
                ConstValue::U8(v) => v.trailing_zeros(),
                ConstValue::I16(v) => v.trailing_zeros(),
                ConstValue::U16(v) => v.trailing_zeros(),
                ConstValue::I32(v) => v.trailing_zeros(),
                ConstValue::U32(v) => v.trailing_zeros(),
                ConstValue::I64(v) => v.trailing_zeros(),
                ConstValue::U64(v) => v.trailing_zeros(),
                _ => return None,
            };
            Some(ConstValue::I32(bits as i32))
        }
        SsaOp::BitScanReverse { src, .. } => {
            let v = get_const(*src)?;
            let bits = match v {
                ConstValue::I8(v) => 7u32.checked_sub(v.leading_zeros())?,
                ConstValue::U8(v) => 7u32.checked_sub(v.leading_zeros())?,
                ConstValue::I16(v) => 15u32.checked_sub(v.leading_zeros())?,
                ConstValue::U16(v) => 15u32.checked_sub(v.leading_zeros())?,
                ConstValue::I32(v) => 31u32.checked_sub(v.leading_zeros())?,
                ConstValue::U32(v) => 31u32.checked_sub(v.leading_zeros())?,
                ConstValue::I64(v) => 63u32.checked_sub(v.leading_zeros())?,
                ConstValue::U64(v) => 63u32.checked_sub(v.leading_zeros())?,
                _ => return None,
            };
            Some(ConstValue::U32(bits))
        }

        // Population count and parity
        SsaOp::Popcount { src, .. } => {
            let v = get_const(*src)?;
            let count = match v {
                ConstValue::I8(v) => v.count_ones(),
                ConstValue::U8(v) => v.count_ones(),
                ConstValue::I16(v) => v.count_ones(),
                ConstValue::U16(v) => v.count_ones(),
                ConstValue::I32(v) => v.count_ones(),
                ConstValue::U32(v) => v.count_ones(),
                ConstValue::I64(v) => v.count_ones(),
                ConstValue::U64(v) => v.count_ones(),
                _ => return None,
            };
            Some(ConstValue::I32(count as i32))
        }
        SsaOp::Parity { src, .. } => {
            let v = get_const(*src)?;
            let parity = match v {
                ConstValue::I8(v) => v.count_ones() % 2,
                ConstValue::U8(v) => v.count_ones() % 2,
                ConstValue::I16(v) => v.count_ones() % 2,
                ConstValue::U16(v) => v.count_ones() % 2,
                ConstValue::I32(v) => v.count_ones() % 2,
                ConstValue::U32(v) => v.count_ones() % 2,
                ConstValue::I64(v) => v.count_ones() % 2,
                ConstValue::U64(v) => v.count_ones() % 2,
                _ => return None,
            };
            Some(ConstValue::I32(parity as i32))
        }

        // Conditional select
        SsaOp::Select {
            condition,
            true_val,
            false_val,
            ..
        } => {
            let cond = get_const(*condition)?;
            match cond.as_i64() {
                Some(0) => get_const(*false_val),
                Some(_) => get_const(*true_val),
                None => None,
            }
        }

        // All other operations cannot be evaluated to constants
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::{
        ir::{block::SsaBlock, instruction::SsaInstruction, variable::DefSite, VariableOrigin},
        testing,
        testing::{MockTarget, MockType},
    };

    type Cv = ConstValue<MockTarget>;

    fn var(index: usize) -> SsaVarId {
        SsaVarId::from_index(index)
    }

    fn resolve(id: SsaVarId) -> Option<Cv> {
        match id.index() {
            0 => Some(Cv::I32(10)),
            1 => Some(Cv::I32(3)),
            2 => Some(Cv::I32(-1)),
            3 => Some(Cv::I32(i32::MAX)),
            _ => None,
        }
    }

    #[test]
    fn const_evaluator_resolves_consts_and_injected_values() {
        let ssa = testing::const_i32_return(42);
        let mut evaluator = ConstEvaluator::new(&ssa, PointerSize::Bit64);

        assert_eq!(evaluator.ssa().block_count(), 1);
        assert_eq!(evaluator.evaluate_var(var(0)), Some(Cv::I32(42)));

        evaluator.set_known(var(0), Cv::I32(7));
        assert_eq!(evaluator.evaluate_var(var(0)), Some(Cv::I32(7)));

        evaluator.clear_cache();
        assert_eq!(evaluator.evaluate_var(var(0)), Some(Cv::I32(42)));
        assert_eq!(evaluator.into_results().get(&var(0)), Some(&Cv::I32(42)));
    }

    #[test]
    fn const_evaluator_traces_copy_chains_and_honors_depth_limit() {
        let mut ssa = crate::ir::SsaFunction::<MockTarget>::new(0, 0);
        for i in 0..3 {
            ssa.create_variable(
                VariableOrigin::Local(i),
                0,
                DefSite::instruction(0, usize::from(i)),
                MockType::I32,
            );
        }
        let mut block = SsaBlock::new(0);
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: var(0),
            value: Cv::I32(5),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Copy {
            dest: var(1),
            src: var(0),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Copy {
            dest: var(2),
            src: var(1),
        }));
        ssa.add_block(block);

        let mut evaluator = ConstEvaluator::new(&ssa, PointerSize::Bit64);
        assert_eq!(evaluator.evaluate_var(var(2)), Some(Cv::I32(5)));

        let mut shallow = ConstEvaluator::with_max_depth(&ssa, 0, PointerSize::Bit64);
        assert_eq!(shallow.evaluate_var(var(2)), None);
    }

    #[test]
    fn evaluate_const_op_folds_arithmetic_bitwise_and_shifts() {
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Add {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(13))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Sub {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(7))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Mul {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(30))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Div {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    unsigned: false,
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(3))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Rem {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    unsigned: false,
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(1))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::And {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(2))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Or {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(11))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Xor {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(9))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Shl {
                    dest: var(9),
                    value: var(1),
                    amount: var(1),
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(24))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Shr {
                    dest: var(9),
                    value: var(2),
                    amount: var(1),
                    unsigned: true,
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(536_870_911))
        );
    }

    #[test]
    fn evaluate_const_op_folds_unary_comparison_and_checked_arithmetic() {
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Neg {
                    dest: var(9),
                    operand: var(1),
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(-3))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Not {
                    dest: var(9),
                    operand: var(1),
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(!3))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Ceq {
                    dest: var(9),
                    left: var(0),
                    right: var(1)
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::False)
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Clt {
                    dest: var(9),
                    left: var(1),
                    right: var(0),
                    unsigned: false
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::True)
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Cgt {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    unsigned: true
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::True)
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::AddOvf {
                    dest: var(9),
                    left: var(3),
                    right: var(1),
                    unsigned: false,
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            None
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::SubOvf {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    unsigned: false,
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(7))
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::MulOvf {
                    dest: var(9),
                    left: var(0),
                    right: var(1),
                    unsigned: false,
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            Some(Cv::I32(30))
        );
    }

    #[test]
    fn evaluate_const_op_returns_none_for_unknown_inputs_and_unsupported_ops() {
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Add {
                    dest: var(9),
                    left: var(0),
                    right: var(99),
                    flags: None,
                },
                resolve,
                PointerSize::Bit64
            ),
            None
        );
        assert_eq!(
            evaluate_const_op(
                &SsaOp::Conv {
                    dest: var(9),
                    operand: var(0),
                    target: MockType::I64,
                    overflow_check: false,
                    unsigned: false
                },
                resolve,
                PointerSize::Bit64
            ),
            None
        );
        assert_eq!(
            evaluate_const_op::<MockTarget>(
                &SsaOp::Return {
                    value: Some(var(0))
                },
                resolve,
                PointerSize::Bit64
            ),
            None
        );
    }
}