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
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
//! Small method inlining and proxy devirtualization pass.
//!
//! This pass inlines small, pure methods at their call sites and devirtualizes
//! proxy methods to expose further optimization opportunities. It's particularly
//! effective for:
//!
//! - Proxy methods that just forward to another method (even impure ones)
//! - Simple getters/setters
//! - Constant-returning methods
//! - String decryptor helpers
//!
//! # Inlining Criteria
//!
//! A method is considered for inlining if:
//! 1. It's small enough (instruction count <= threshold)
//! 2. It's pure or read-only (no global side effects)
//! 3. It doesn't recurse (directly or indirectly)
//! 4. The call is non-virtual (single target)
//! 5. The callee SSA is available in the context
//!
//! # Proxy Devirtualization
//!
//! Additionally, proxy methods are detected and devirtualized regardless of purity.
//! A proxy method is one that:
//! 1. Contains a single call instruction (the forwarded call)
//! 2. Forwards parameters directly as arguments (possibly reordered)
//! 3. Returns the call result (if non-void)
//! 4. Has no other side effects besides the forwarded call
//!
//! When a proxy is detected, the call is replaced with a direct call to the
//! forwarding target, eliminating the proxy overhead.
//!
//! # Proxy-Only Mode
//!
//! When `proxy_only` is enabled, the pass only performs proxy devirtualization.
//! Full method body inlining and no-op elimination are skipped. This mode is
//! used when the pass is triggered by obfuscator proxy detection (e.g.,
//! ConfuserEx ReferenceProxy) to remove obfuscation indirection without
//! altering the original program structure.
//!
//! # Safety
//!
//! Inlining preserves semantics by:
//! - Only inlining pure/read-only methods
//! - Properly remapping all variable IDs
//! - Handling parameter binding correctly
//! - Replacing return with assignment to destination
//!
//! Proxy devirtualization preserves semantics because the forwarded call
//! happens exactly once in either case - we're just eliminating the wrapper.

use std::{collections::HashMap, sync::Arc};

use crate::{
    analysis::{
        ConstValue, DefSite, MethodRef, ReturnInfo, SsaFunction, SsaInstruction, SsaOp, SsaVarId,
        SsaVariable, VariableOrigin,
    },
    compiler::{pass::SsaPass, CompilerContext, EventKind, EventLog},
    metadata::{tables::MemberRefSignature, token::Token, typesystem::CilTypeReference},
    CilObject, Result,
};

/// Type of inlining to perform at a call site.
#[derive(Debug, Clone)]
enum InlineAction {
    /// Full inlining: copy callee's body into caller
    FullInline,
    /// Proxy devirtualization: replace call target with forwarding target
    ProxyDevirtualize {
        target_method: MethodRef,
        arg_mapping: Vec<usize>,
        is_virtual: bool,
    },
    /// No-op elimination: remove the call entirely (void method that just returns)
    NoOpEliminate,
    /// Constant fold: replace the call with a constant value
    ConstantFold(ConstValue),
}

/// A candidate call site for inlining or devirtualization.
#[derive(Debug, Clone)]
struct InlineCandidate {
    /// Block index containing the call
    block_idx: usize,
    /// Instruction index within the block
    instr_idx: usize,
    /// Token of the method being called (the callee/proxy)
    callee_token: Token,
    /// What action to take at this call site
    action: InlineAction,
}

/// Method-specific context for inlining operations.
///
/// This struct holds all the common parameters needed during inlining of a single
/// method, reducing parameter passing overhead and making the code cleaner.
struct InliningContext<'a> {
    /// Reference to the pass configuration (for thresholds, etc.)
    pass: &'a InliningPass,
    /// The caller's SSA being modified
    caller_ssa: &'a mut SsaFunction,
    /// Token of the method being processed
    caller_token: Token,
    /// The analysis context with method summaries, call graph, etc.
    analysis_ctx: &'a CompilerContext,
    /// The assembly for resolving tokens
    assembly: &'a Arc<CilObject>,
    /// Event log for recording changes
    changes: EventLog,
}

impl<'a> InliningContext<'a> {
    /// Creates a new inlining context for processing a method.
    ///
    /// The context holds all the state needed for inlining operations on a single
    /// method, reducing parameter passing overhead in helper methods.
    ///
    /// # Arguments
    ///
    /// * `pass` - Reference to the inlining pass configuration.
    /// * `caller_ssa` - Mutable reference to the caller's SSA being modified.
    /// * `caller_token` - Token identifying the method being processed.
    /// * `analysis_ctx` - Analysis context with method summaries and call graph.
    /// * `assembly` - Assembly for resolving tokens.
    fn new(
        pass: &'a InliningPass,
        caller_ssa: &'a mut SsaFunction,
        caller_token: Token,
        analysis_ctx: &'a CompilerContext,
        assembly: &'a Arc<CilObject>,
    ) -> Self {
        Self {
            pass,
            caller_ssa,
            caller_token,
            analysis_ctx,
            assembly,
            changes: EventLog::new(),
        }
    }

    /// Returns whether any changes were made during inlining.
    ///
    /// # Returns
    ///
    /// `true` if at least one inlining operation was performed.
    fn has_changes(&self) -> bool {
        !self.changes.is_empty()
    }

    /// Consumes the context and returns the accumulated event log.
    ///
    /// # Returns
    ///
    /// The event log containing all recorded changes from inlining operations.
    fn into_changes(self) -> EventLog {
        self.changes
    }

    /// Checks if a callee is a valid target for inlining or devirtualization.
    ///
    /// A method is invalid for inlining if it:
    /// - Is the same as the caller (self-recursion)
    /// - Is a registered decryptor (handled by decryption passes)
    /// - Is a registered dispatcher (control flow dispatcher)
    ///
    /// # Arguments
    ///
    /// * `callee_token` - Token of the method being considered for inlining.
    ///
    /// # Returns
    ///
    /// `true` if the callee can potentially be inlined.
    fn is_valid_target(&self, callee_token: Token) -> bool {
        // Don't inline self-recursion
        if callee_token == self.caller_token {
            return false;
        }

        // Check if this method should not be inlined (decryptors, dispatchers, etc.)
        if self.analysis_ctx.no_inline.contains(&callee_token) {
            return false;
        }

        true
    }

    /// Checks if a method should be fully inlined at a call site.
    ///
    /// A method qualifies for full inlining if it:
    /// - Is a valid target (not self-recursion, decryptor, or dispatcher)
    /// - Is smaller than the inlining threshold
    /// - Is pure or read-only (no global side effects)
    /// - Does not contain recursive calls
    ///
    /// # Arguments
    ///
    /// * `callee_token` - Token of the method being considered for inlining.
    ///
    /// # Returns
    ///
    /// `true` if the method should be inlined.
    fn should_inline(&self, callee_token: Token) -> bool {
        if !self.is_valid_target(callee_token) {
            return false;
        }

        // Check if we have the callee's SSA and evaluate criteria
        self.analysis_ctx
            .with_ssa(callee_token, |callee_ssa| {
                // Check size threshold
                let instr_count = callee_ssa.instruction_count();
                if instr_count > self.pass.inline_threshold {
                    return false;
                }

                // Check purity - only inline pure or read-only methods
                if !callee_ssa.purity().can_inline() {
                    return false;
                }

                // Check for recursive calls in the callee
                if self.has_recursive_call(callee_token, callee_ssa) {
                    return false;
                }

                true
            })
            .unwrap_or(false)
    }

    /// Checks if a method has any recursive calls (direct or indirect).
    ///
    /// This checks for both direct self-calls and indirect recursion through
    /// the call graph (e.g., A calls B which calls A).
    ///
    /// # Arguments
    ///
    /// * `method_token` - Token of the method to check for recursion.
    /// * `ssa` - The SSA representation of the method.
    ///
    /// # Returns
    ///
    /// `true` if the method contains any recursive calls.
    fn has_recursive_call(&self, method_token: Token, ssa: &SsaFunction) -> bool {
        for block in ssa.blocks() {
            for instr in block.instructions() {
                if let SsaOp::Call { method, .. } | SsaOp::CallVirt { method, .. } = instr.op() {
                    if method.token() == method_token {
                        return true;
                    }
                    // Check for indirect recursion via call graph
                    let callees = self.analysis_ctx.call_graph.callees(method.token());
                    if callees.contains(&method_token) {
                        return true;
                    }
                }
            }
        }
        false
    }

    /// Checks if a method should be devirtualized as a proxy.
    ///
    /// Unlike full inlining, proxy devirtualization doesn't require purity
    /// because we're just changing the call target, not duplicating code.
    /// This is effective for wrapper methods that just forward calls.
    ///
    /// # Arguments
    ///
    /// * `callee_token` - Token of the method being considered for devirtualization.
    ///
    /// # Returns
    ///
    /// If the method is a proxy, returns `Some((target_method, arg_mapping, is_virtual))`:
    /// - `target_method` - The forwarding target method.
    /// - `arg_mapping` - Maps proxy parameter indices to target argument positions.
    /// - `is_virtual` - Whether the forwarded call is virtual.
    ///
    /// Returns `None` if the method is not a proxy.
    fn should_devirtualize_proxy(
        &self,
        callee_token: Token,
    ) -> Option<(MethodRef, Vec<usize>, bool)> {
        if !self.is_valid_target(callee_token) {
            return None;
        }

        // Check for proxy pattern
        self.analysis_ctx
            .with_ssa(callee_token, |callee_ssa| {
                InliningPass::detect_proxy_pattern(callee_ssa)
            })
            .flatten()
    }

    /// Checks if a method is a no-op that can be eliminated at call sites.
    ///
    /// A no-op method is one that:
    /// - Just returns (void method with only `ret`)
    /// - Always returns a constant value
    /// - Has no side effects (is pure)
    ///
    /// # Arguments
    ///
    /// * `callee_token` - Token of the method being considered.
    ///
    /// # Returns
    ///
    /// - `Some(None)` - Void no-op: call can be replaced with Nop.
    /// - `Some(Some(ConstValue))` - Constant return: call can be replaced with constant.
    /// - `None` - Not a no-op method.
    // Intentional: None=not noop, Some(None)=void noop, Some(Some(v))=constant noop
    #[allow(clippy::option_option)]
    fn detect_noop_method(&self, callee_token: Token) -> Option<Option<ConstValue>> {
        if !self.is_valid_target(callee_token) {
            return None;
        }

        // Check if callee is a no-op using SSA analysis
        self.analysis_ctx
            .with_ssa(callee_token, |callee_ssa| {
                // Must have no side effects to safely eliminate
                if !callee_ssa.purity().can_eliminate_if_unused() {
                    return None;
                }

                // Check what the method returns
                match callee_ssa.return_info() {
                    ReturnInfo::Void => Some(None),
                    ReturnInfo::Constant(val) => Some(Some(val.clone())),
                    _ => None,
                }
            })
            .flatten()
    }

    /// Resolves a token to its MethodDef equivalent.
    ///
    /// This is necessary because:
    /// - SSA forms are stored under MethodDef tokens
    /// - Call instructions may use MemberRef tokens even for internal methods
    /// - We need to match MemberRef -> MethodDef to look up the callee's SSA
    ///
    /// # Arguments
    ///
    /// * `token` - The token to resolve (may be MethodDef, MemberRef, or MethodSpec).
    ///
    /// # Returns
    ///
    /// The resolved MethodDef token, or the original token if resolution fails.
    fn resolve_to_method_def(&self, token: Token) -> Token {
        let table_id = token.table();

        // Already a MethodDef - return as-is
        if table_id == 0x06 {
            return token;
        }

        // MemberRef - try to resolve to MethodDef
        if table_id == 0x0A {
            let refs = self.assembly.refs_members();
            if let Some(member_ref_entry) = refs.get(&token) {
                let member_ref = member_ref_entry.value();

                // Only handle method signatures, not fields
                let MemberRefSignature::Method(ref _method_sig) = member_ref.signature else {
                    return token;
                };

                // Check if declared by a TypeDef (internal type)
                if let CilTypeReference::TypeDef(type_ref) = &member_ref.declaredby {
                    if let Some(type_info) = type_ref.upgrade() {
                        if let Some(method) = type_info
                            .query_methods()
                            .name(&member_ref.name)
                            .find_first()
                        {
                            return method.token;
                        }
                    }
                }
            }
        }

        token
    }

    /// Finds all inlinable and devirtualizable call sites in the method.
    ///
    /// Scans the caller's SSA for call instructions and evaluates each one
    /// against the inlining criteria. Candidates are prioritized as:
    /// 1. Full inlining (for small, pure methods)
    /// 2. No-op elimination (for void/constant-returning pure methods)
    /// 3. Proxy devirtualization (for call forwarding wrappers)
    ///
    /// # Returns
    ///
    /// A vector of candidates that can be inlined or devirtualized.
    fn find_candidates(&self) -> Vec<InlineCandidate> {
        let mut candidates = Vec::new();

        for (block_idx, instr_idx, instr) in self.caller_ssa.iter_instructions() {
            let raw_callee_token = match instr.op() {
                SsaOp::Call { method, .. } => method.token(),
                SsaOp::CallVirt { method, .. } => {
                    let token = method.token();
                    // Check if this is actually polymorphic
                    if self
                        .analysis_ctx
                        .call_graph
                        .resolver()
                        .is_polymorphic(token)
                    {
                        continue;
                    }
                    token
                }
                _ => continue,
            };

            // Resolve MemberRef tokens to MethodDef tokens for SSA lookup
            let callee_token = self.resolve_to_method_def(raw_callee_token);

            if !self.pass.proxy_only {
                // Try full inlining first (for pure methods)
                if self.should_inline(callee_token) {
                    candidates.push(InlineCandidate {
                        block_idx,
                        instr_idx,
                        callee_token,
                        action: InlineAction::FullInline,
                    });
                    continue;
                }
                // Try no-op elimination
                if let Some(noop_result) = self.detect_noop_method(callee_token) {
                    let action = match noop_result {
                        None => InlineAction::NoOpEliminate,
                        Some(val) => InlineAction::ConstantFold(val),
                    };
                    candidates.push(InlineCandidate {
                        block_idx,
                        instr_idx,
                        callee_token,
                        action,
                    });
                    continue;
                }
            }

            // Try proxy devirtualization (always enabled)
            if let Some((target_method, arg_mapping, is_virtual)) =
                self.should_devirtualize_proxy(callee_token)
            {
                candidates.push(InlineCandidate {
                    block_idx,
                    instr_idx,
                    callee_token,
                    action: InlineAction::ProxyDevirtualize {
                        target_method,
                        arg_mapping,
                        is_virtual,
                    },
                });
            }
        }

        candidates
    }

    /// Processes a single inlining candidate.
    ///
    /// Dispatches to the appropriate inlining strategy based on the candidate's
    /// action type. Also marks successfully inlined methods in the analysis context.
    ///
    /// # Arguments
    ///
    /// * `candidate` - The inlining candidate to process.
    ///
    /// # Returns
    ///
    /// `true` if the inlining operation was successful.
    fn process_candidate(&mut self, candidate: &InlineCandidate) -> bool {
        let call_op = match self.caller_ssa.block(candidate.block_idx) {
            Some(block) => match block.instructions().get(candidate.instr_idx) {
                Some(instr) => instr.op().clone(),
                None => return false,
            },
            None => return false,
        };

        let success = match &candidate.action {
            InlineAction::FullInline => {
                // Get the callee SSA
                let Some(callee_ssa) = self
                    .analysis_ctx
                    .with_ssa(candidate.callee_token, Clone::clone)
                else {
                    return false;
                };

                self.inline_call(
                    &callee_ssa,
                    candidate.block_idx,
                    candidate.instr_idx,
                    &call_op,
                    candidate.callee_token,
                )
            }
            InlineAction::ProxyDevirtualize {
                target_method,
                arg_mapping,
                is_virtual,
            } => self.devirtualize_proxy(
                candidate.block_idx,
                candidate.instr_idx,
                &call_op,
                *target_method,
                arg_mapping,
                *is_virtual,
                candidate.callee_token,
            ),
            InlineAction::NoOpEliminate => self.eliminate_noop_call(
                candidate.block_idx,
                candidate.instr_idx,
                &call_op,
                candidate.callee_token,
            ),
            InlineAction::ConstantFold(const_val) => self.fold_constant_call(
                candidate.block_idx,
                candidate.instr_idx,
                &call_op,
                const_val,
                candidate.callee_token,
            ),
        };

        if success {
            self.analysis_ctx.mark_inlined(candidate.callee_token);
        }
        success
    }

    /// Inlines a callee method at a specific call site.
    ///
    /// This method handles different return patterns optimally:
    /// - Constant returns: Replace call with constant assignment
    /// - Pass-through returns: Replace call with copy from argument
    /// - Void returns: Replace call with Nop
    /// - Pure computation: Perform full block inlining
    ///
    /// # Arguments
    ///
    /// * `callee_ssa` - The callee's SSA representation.
    /// * `call_block_idx` - Block index of the call instruction in the caller.
    /// * `call_instr_idx` - Instruction index within the block.
    /// * `call_op` - The call operation being inlined.
    /// * `callee_token` - Token of the callee (for logging).
    ///
    /// # Returns
    ///
    /// `true` if inlining was successful.
    fn inline_call(
        &mut self,
        callee_ssa: &SsaFunction,
        call_block_idx: usize,
        call_instr_idx: usize,
        call_op: &SsaOp,
        callee_token: Token,
    ) -> bool {
        // Extract call information
        let (dest, args) = match call_op {
            SsaOp::Call { dest, args, .. } | SsaOp::CallVirt { dest, args, .. } => {
                (*dest, args.clone())
            }
            _ => return false,
        };

        // For very simple cases, we can do a simpler inline
        match callee_ssa.return_info() {
            ReturnInfo::Constant(value) => {
                if let Some(dest_var) = dest {
                    if let Some(block) = self.caller_ssa.block_mut(call_block_idx) {
                        let instr = &mut block.instructions_mut()[call_instr_idx];
                        instr.set_op(SsaOp::Const {
                            dest: dest_var,
                            value: value.clone(),
                        });
                        self.changes
                            .record(EventKind::MethodInlined)
                            .at(self.caller_token, call_instr_idx)
                            .message(format!("inlined constant {callee_token:?}"));
                        return true;
                    }
                } else {
                    // Void destination but constant return - just remove the call
                    if let Some(block) = self.caller_ssa.block_mut(call_block_idx) {
                        let instr = &mut block.instructions_mut()[call_instr_idx];
                        instr.set_op(SsaOp::Nop);
                        self.changes
                            .record(EventKind::MethodInlined)
                            .at(self.caller_token, call_instr_idx)
                            .message(format!("eliminated pure call {callee_token:?}"));
                        return true;
                    }
                }
            }
            ReturnInfo::PassThrough(param_idx) => {
                if let Some(dest_var) = dest {
                    if let Some(&src_var) = args.get(param_idx) {
                        if let Some(block) = self.caller_ssa.block_mut(call_block_idx) {
                            let instr = &mut block.instructions_mut()[call_instr_idx];
                            instr.set_op(SsaOp::Copy {
                                dest: dest_var,
                                src: src_var,
                            });
                            self.changes
                                .record(EventKind::MethodInlined)
                                .at(self.caller_token, call_instr_idx)
                                .message(format!("inlined passthrough {callee_token:?}"));
                            return true;
                        }
                    }
                }
            }
            ReturnInfo::Void => {
                if let Some(block) = self.caller_ssa.block_mut(call_block_idx) {
                    let instr = &mut block.instructions_mut()[call_instr_idx];
                    instr.set_op(SsaOp::Nop);
                    self.changes
                        .record(EventKind::MethodInlined)
                        .at(self.caller_token, call_instr_idx)
                        .message(format!("eliminated void call {callee_token:?}"));
                    return true;
                }
            }
            ReturnInfo::PureComputation | ReturnInfo::Dynamic | ReturnInfo::Unknown => {
                return self.inline_full(
                    callee_ssa,
                    call_block_idx,
                    call_instr_idx,
                    dest,
                    &args,
                    callee_token,
                );
            }
        }

        false
    }

    /// Performs full SSA inlining by copying blocks from callee into caller.
    ///
    /// This handles the complex case where simple replacement isn't possible.
    /// The algorithm:
    /// 1. Map callee parameters to call arguments
    /// 2. Copy callee instructions with remapped variable IDs
    /// 3. Replace the call with inlined instructions
    /// 4. Handle return value assignment if needed
    ///
    /// Currently only supports single-block callees to keep complexity manageable.
    ///
    /// # Arguments
    ///
    /// * `callee_ssa` - The callee's SSA representation.
    /// * `call_block_idx` - Block index of the call instruction.
    /// * `call_instr_idx` - Instruction index within the block.
    /// * `dest` - Optional destination variable for the call result.
    /// * `args` - Arguments passed to the call.
    /// * `callee_token` - Token of the callee (for logging).
    ///
    /// # Returns
    ///
    /// `true` if full inlining was successful.
    fn inline_full(
        &mut self,
        callee_ssa: &SsaFunction,
        call_block_idx: usize,
        call_instr_idx: usize,
        dest: Option<SsaVarId>,
        args: &[SsaVarId],
        callee_token: Token,
    ) -> bool {
        // Only inline single-block callees for now
        if callee_ssa.blocks().len() != 1 {
            return false;
        }

        let Some(callee_block) = callee_ssa.blocks().first() else {
            return false;
        };

        // Build variable remapping
        let mut var_remap: HashMap<SsaVarId, SsaVarId> = HashMap::new();

        // Map callee parameters to call arguments
        for (param_idx, &arg_var) in args.iter().enumerate() {
            // Safe: .NET methods have at most 65535 parameters
            #[allow(clippy::cast_possible_truncation)]
            if let Some(param_var) = callee_ssa
                .variables_from_argument(param_idx as u16)
                .find(|v| v.version() == 0)
            {
                var_remap.insert(param_var.id(), arg_var);
            }
        }

        // Collect instructions to inline (excluding return)
        let mut inlined_ops: Vec<SsaOp> = Vec::new();
        let mut return_value: Option<SsaVarId> = None;

        for instr in callee_block.instructions() {
            let op = instr.op();
            if let SsaOp::Return { value } = op {
                return_value = *value;
            } else {
                let remapped_op = Self::remap_op(op, &mut var_remap, callee_ssa, self.caller_ssa);
                inlined_ops.push(remapped_op);
            }
        }

        // Modify the caller
        let Some(block) = self.caller_ssa.block_mut(call_block_idx) else {
            return false;
        };

        // Replace call instruction with first inlined op (or Nop if empty)
        if let Some(first_op) = inlined_ops.first().cloned() {
            block.instructions_mut()[call_instr_idx].set_op(first_op);
        } else {
            block.instructions_mut()[call_instr_idx].set_op(SsaOp::Nop);
        }

        // Insert remaining inlined ops
        let instructions = block.instructions_mut();
        for (i, op) in inlined_ops.into_iter().skip(1).enumerate() {
            instructions.insert(call_instr_idx + 1 + i, SsaInstruction::synthetic(op));
        }

        // Handle return value
        if let (Some(dest_var), Some(ret_var)) = (dest, return_value) {
            let remapped_ret = var_remap.get(&ret_var).copied().unwrap_or(ret_var);

            if dest_var != remapped_ret {
                let Some(block) = self.caller_ssa.block_mut(call_block_idx) else {
                    return false;
                };
                let insert_pos = call_instr_idx + 1;
                block.instructions_mut().insert(
                    insert_pos,
                    SsaInstruction::synthetic(SsaOp::Copy {
                        dest: dest_var,
                        src: remapped_ret,
                    }),
                );
            }
        }

        self.changes
            .record(EventKind::MethodInlined)
            .at(self.caller_token, call_instr_idx)
            .message(format!("fully inlined {callee_token:?}"));
        true
    }

    /// Devirtualizes a proxy call by replacing it with a direct call to the target.
    ///
    /// This doesn't inline the proxy body - it just changes the call target and
    /// remaps the arguments according to the proxy's parameter mapping.
    ///
    /// # Arguments
    ///
    /// * `call_block_idx` - Block index of the call instruction.
    /// * `call_instr_idx` - Instruction index within the block.
    /// * `call_op` - The call operation being devirtualized.
    /// * `target_method` - The forwarding target method.
    /// * `arg_mapping` - Maps proxy parameter indices to target argument positions.
    /// * `is_virtual` - Whether the forwarded call should be virtual.
    /// * `proxy_token` - Token of the proxy method (for logging).
    ///
    /// # Returns
    ///
    /// `true` if devirtualization was successful.
    #[allow(clippy::too_many_arguments)]
    fn devirtualize_proxy(
        &mut self,
        call_block_idx: usize,
        call_instr_idx: usize,
        call_op: &SsaOp,
        target_method: MethodRef,
        arg_mapping: &[usize],
        is_virtual: bool,
        proxy_token: Token,
    ) -> bool {
        let (dest, original_args) = match call_op {
            SsaOp::Call { dest, args, .. } | SsaOp::CallVirt { dest, args, .. } => {
                (*dest, args.clone())
            }
            _ => return false,
        };

        // Remap arguments
        let mut remapped_args = Vec::with_capacity(arg_mapping.len());
        for &param_idx in arg_mapping {
            if let Some(&arg) = original_args.get(param_idx) {
                remapped_args.push(arg);
            } else {
                return false;
            }
        }

        // Create the new call operation
        let new_op = if is_virtual {
            SsaOp::CallVirt {
                dest,
                method: target_method,
                args: remapped_args,
            }
        } else {
            SsaOp::Call {
                dest,
                method: target_method,
                args: remapped_args,
            }
        };

        // Replace the instruction
        if let Some(block) = self.caller_ssa.block_mut(call_block_idx) {
            block.instructions_mut()[call_instr_idx].set_op(new_op);
            self.changes
                .record(EventKind::MethodInlined)
                .at(self.caller_token, call_instr_idx)
                .message(format!(
                    "devirtualized proxy {:?} -> {:?}",
                    proxy_token,
                    target_method.token()
                ));
            return true;
        }

        false
    }

    /// Eliminates a call to a void no-op method.
    ///
    /// Replaces the call instruction with a Nop, which will be cleaned up by
    /// the dead code elimination pass.
    ///
    /// # Arguments
    ///
    /// * `call_block_idx` - Block index of the call instruction.
    /// * `call_instr_idx` - Instruction index within the block.
    /// * `call_op` - The call operation being eliminated.
    /// * `callee_token` - Token of the no-op method (for logging).
    ///
    /// # Returns
    ///
    /// `true` if elimination was successful.
    fn eliminate_noop_call(
        &mut self,
        call_block_idx: usize,
        call_instr_idx: usize,
        call_op: &SsaOp,
        callee_token: Token,
    ) -> bool {
        if !matches!(call_op, SsaOp::Call { .. } | SsaOp::CallVirt { .. }) {
            return false;
        }

        if let Some(block) = self.caller_ssa.block_mut(call_block_idx) {
            if let Some(instr) = block.instructions_mut().get_mut(call_instr_idx) {
                instr.set_op(SsaOp::Nop);
                self.changes
                    .record(EventKind::MethodInlined)
                    .at(self.caller_token, call_instr_idx)
                    .message(format!(
                        "eliminated no-op call to 0x{:08x}",
                        callee_token.value()
                    ));
                return true;
            }
        }
        false
    }

    /// Folds a call to a constant-returning method into the constant value.
    ///
    /// Replaces the call instruction with a Const instruction that produces
    /// the same value the method would have returned. If the return value is
    /// unused, replaces with Nop instead.
    ///
    /// # Arguments
    ///
    /// * `call_block_idx` - Block index of the call instruction.
    /// * `call_instr_idx` - Instruction index within the block.
    /// * `call_op` - The call operation being folded.
    /// * `const_val` - The constant value to fold to.
    /// * `callee_token` - Token of the constant-returning method (for logging).
    ///
    /// # Returns
    ///
    /// `true` if folding was successful.
    fn fold_constant_call(
        &mut self,
        call_block_idx: usize,
        call_instr_idx: usize,
        call_op: &SsaOp,
        const_val: &ConstValue,
        callee_token: Token,
    ) -> bool {
        let dest = match call_op {
            SsaOp::Call { dest, .. } | SsaOp::CallVirt { dest, .. } => *dest,
            _ => return false,
        };

        if let Some(dest_var) = dest {
            if let Some(block) = self.caller_ssa.block_mut(call_block_idx) {
                if let Some(instr) = block.instructions_mut().get_mut(call_instr_idx) {
                    instr.set_op(SsaOp::Const {
                        dest: dest_var,
                        value: const_val.clone(),
                    });
                    self.changes
                        .record(EventKind::MethodInlined)
                        .at(self.caller_token, call_instr_idx)
                        .message(format!(
                            "folded constant call to 0x{:08x} -> {:?}",
                            callee_token.value(),
                            const_val
                        ));
                    return true;
                }
            }
        } else {
            // No destination - just replace with Nop
            if let Some(block) = self.caller_ssa.block_mut(call_block_idx) {
                if let Some(instr) = block.instructions_mut().get_mut(call_instr_idx) {
                    instr.set_op(SsaOp::Nop);
                    self.changes
                        .record(EventKind::MethodInlined)
                        .at(self.caller_token, call_instr_idx)
                        .message(format!(
                            "eliminated unused constant call to 0x{:08x}",
                            callee_token.value()
                        ));
                    return true;
                }
            }
        }
        false
    }

    /// Remaps variables in an SSA operation using the given mapping.
    ///
    /// Creates fresh variables in the caller for any callee variables not
    /// already in the mapping (i.e., variables defined within the callee).
    ///
    /// # Arguments
    ///
    /// * `op` - The SSA operation to remap.
    /// * `var_remap` - Mapping from callee variables to caller variables.
    /// * `callee_ssa` - The callee's SSA (for variable metadata).
    /// * `caller_ssa` - The caller's SSA (for creating new variables).
    ///
    /// # Returns
    ///
    /// A cloned operation with all variables remapped to caller variables.
    fn remap_op(
        op: &SsaOp,
        var_remap: &mut HashMap<SsaVarId, SsaVarId>,
        callee_ssa: &SsaFunction,
        caller_ssa: &mut SsaFunction,
    ) -> SsaOp {
        let mut cloned = op.clone();

        // Remap destination
        if let Some(dest) = cloned.dest() {
            let new_dest = Self::get_or_create_var(dest, var_remap, callee_ssa, caller_ssa);
            cloned.set_dest(new_dest);
        }

        // Remap uses
        for used in op.uses() {
            let new_var = var_remap.get(&used).copied().unwrap_or(used);
            cloned.replace_uses(used, new_var);
        }

        cloned
    }

    /// Gets the remapped variable or creates a new one in the caller.
    ///
    /// When creating a new variable, copies the metadata from the callee's
    /// variable to ensure proper type information. The new variable is
    /// registered as a stack temporary in the caller.
    ///
    /// # Arguments
    ///
    /// * `var` - The callee variable to look up or create.
    /// * `var_remap` - Mapping from callee variables to caller variables.
    /// * `callee_ssa` - The callee's SSA (for variable metadata).
    /// * `caller_ssa` - The caller's SSA (for creating new variables).
    ///
    /// # Returns
    ///
    /// The remapped variable ID in the caller's SSA.
    fn get_or_create_var(
        var: SsaVarId,
        var_remap: &mut HashMap<SsaVarId, SsaVarId>,
        callee_ssa: &SsaFunction,
        caller_ssa: &mut SsaFunction,
    ) -> SsaVarId {
        if let Some(&remapped) = var_remap.get(&var) {
            return remapped;
        }

        // Safe: SSA variable count fits in u32
        #[allow(clippy::cast_possible_truncation)]
        let new_id = if let Some(callee_var) = callee_ssa.variable(var) {
            let new_var = SsaVariable::new_typed(
                VariableOrigin::Stack(caller_ssa.variable_count() as u32),
                0,
                DefSite::instruction(0, 0),
                callee_var.var_type().clone(),
            );
            caller_ssa.add_variable(new_var)
        } else {
            let new_var = SsaVariable::new(
                VariableOrigin::Stack(caller_ssa.variable_count() as u32),
                0,
                DefSite::instruction(0, 0),
            );
            caller_ssa.add_variable(new_var)
        };

        var_remap.insert(var, new_id);
        new_id
    }
}

/// Pass that inlines small, pure methods at their call sites.
///
/// This pass operates on a per-method basis but consults the global context
/// to access callee method SSA forms. It identifies call sites where the
/// callee is small and pure, then replaces the call with the inlined body.
///
/// In proxy-only mode, the pass skips full inlining and no-op elimination,
/// performing only proxy devirtualization. This preserves the original
/// program structure while removing obfuscation indirection.
#[derive(Debug)]
pub struct InliningPass {
    /// Maximum instruction count for inlining candidates.
    inline_threshold: usize,
    /// When true, only perform proxy devirtualization.
    /// Full method inlining and no-op elimination are skipped.
    proxy_only: bool,
}

impl InliningPass {
    /// Creates a new inlining pass.
    ///
    /// # Arguments
    ///
    /// * `threshold` - Maximum instruction count for methods to be inlined.
    /// * `proxy_only` - When true, only proxy devirtualization is performed.
    ///   Full method body inlining is disabled, preserving the original
    ///   program structure while removing obfuscation indirection.
    #[must_use]
    pub fn new(threshold: usize, proxy_only: bool) -> Self {
        Self {
            inline_threshold: threshold,
            proxy_only,
        }
    }

    /// Finds the argument index that a variable ultimately comes from.
    ///
    /// This traces through:
    /// 1. Direct argument variables (VariableOrigin::Argument)
    /// 2. Variables defined by LoadArg instructions
    /// 3. Variables defined by Copy from arguments
    fn find_argument_origin(
        ssa: &SsaFunction,
        var: SsaVarId,
        instructions: &[SsaInstruction],
    ) -> Option<usize> {
        // First, check if the variable directly represents an argument
        if let Some(var_info) = ssa.variable(var) {
            if let VariableOrigin::Argument(idx) = var_info.origin() {
                return Some(idx as usize);
            }
        }

        // Check if this variable was defined by a LoadArg or Copy instruction
        for instr in instructions {
            match instr.op() {
                SsaOp::LoadArg { dest, arg_index } if *dest == var => {
                    return Some(*arg_index as usize);
                }
                SsaOp::Copy { dest, src } if *dest == var => {
                    return Self::find_argument_origin(ssa, *src, instructions);
                }
                _ => {}
            }
        }

        None
    }

    /// Detects if a method is a simple proxy that just forwards to another method.
    ///
    /// A proxy method:
    /// 1. Has a single basic block
    /// 2. Contains exactly one Call/CallVirt instruction
    /// 3. All call arguments come directly from method parameters
    /// 4. If non-void, returns the call result directly
    /// 5. Has no other instructions besides parameter loads, the call, and return
    ///
    /// # Returns
    ///
    /// If this is a proxy, returns `Some((target_method, arg_mapping, is_virtual))` where:
    /// - `target_method` is the forwarding target
    /// - `arg_mapping` maps caller argument index to callee parameter index
    /// - `is_virtual` indicates if the forwarded call is virtual
    fn detect_proxy_pattern(ssa: &SsaFunction) -> Option<(MethodRef, Vec<usize>, bool)> {
        // Must be single-block method
        if ssa.blocks().len() != 1 {
            return None;
        }

        let block = ssa.blocks().first()?;
        let instructions = block.instructions();

        // Find the call instruction
        let mut call_info: Option<(&MethodRef, &[SsaVarId], Option<SsaVarId>, bool)> = None;
        let mut call_count = 0;

        for instr in instructions {
            match instr.op() {
                SsaOp::Call { method, args, dest } => {
                    call_count += 1;
                    call_info = Some((method, args, *dest, false));
                }
                SsaOp::CallVirt { method, args, dest } => {
                    call_count += 1;
                    call_info = Some((method, args, *dest, true));
                }
                // These are allowed in proxy methods
                SsaOp::Return { .. }
                | SsaOp::Nop
                | SsaOp::Phi { .. }
                | SsaOp::LoadArg { .. }
                | SsaOp::LoadLocal { .. }
                | SsaOp::Copy { .. } => {}
                // Any other instruction disqualifies as a proxy
                _ => return None,
            }
        }

        // Must have exactly one call
        if call_count != 1 {
            return None;
        }

        let (target_method, call_args, call_dest, is_virtual) = call_info?;

        // Build parameter mapping
        let mut arg_mapping = Vec::with_capacity(call_args.len());
        let num_params = ssa.num_args();

        for &arg_var in call_args {
            let param_idx = Self::find_argument_origin(ssa, arg_var, instructions);

            match param_idx {
                Some(idx) if idx < num_params => {
                    arg_mapping.push(idx);
                }
                _ => {
                    return None;
                }
            }
        }

        // Check that if there's a return, it returns the call result
        for instr in instructions {
            if let SsaOp::Return {
                value: Some(ret_var),
            } = instr.op()
            {
                if Some(*ret_var) != call_dest {
                    return None;
                }
            }
        }

        Some((*target_method, arg_mapping, is_virtual))
    }
}

impl SsaPass for InliningPass {
    fn name(&self) -> &'static str {
        "InliningPass"
    }

    fn description(&self) -> &'static str {
        "Inlines small, pure methods and devirtualizes proxy calls"
    }

    fn run_on_method(
        &self,
        ssa: &mut SsaFunction,
        method_token: Token,
        ctx: &CompilerContext,
        assembly: &Arc<CilObject>,
    ) -> Result<bool> {
        // Create method-specific inlining context
        let mut inline_ctx = InliningContext::new(self, ssa, method_token, ctx, assembly);

        // Find all candidates
        let candidates = inline_ctx.find_candidates();
        if candidates.is_empty() {
            return Ok(false);
        }

        // Process candidates in reverse order to maintain valid indices
        for candidate in candidates.into_iter().rev() {
            inline_ctx.process_candidate(&candidate);
        }

        // Merge changes back to the analysis context
        let changed = inline_ctx.has_changes();
        if changed {
            ctx.events.merge(&inline_ctx.into_changes());
        }
        Ok(changed)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use crate::{
        analysis::{CallGraph, ConstValue, MethodRef, SsaFunctionBuilder, SsaOp, SsaVarId},
        compiler::CompilerContext,
        compiler::{
            passes::inlining::{InliningContext, InliningPass},
            SsaPass,
        },
        metadata::token::Token,
        test::helpers::test_assembly_arc,
        CilObject,
    };

    fn test_context() -> CompilerContext {
        CompilerContext::new(Arc::new(CallGraph::new()))
    }

    /// Returns a cached test assembly for use in tests.
    fn test_assembly() -> Arc<CilObject> {
        test_assembly_arc()
    }

    #[test]
    fn test_pass_creation() {
        let pass = InliningPass::new(20, false);
        assert_eq!(pass.name(), "InliningPass");
        assert_eq!(pass.inline_threshold, 20);
        assert!(!pass.proxy_only);

        let pass_custom = InliningPass::new(50, false);
        assert_eq!(pass_custom.inline_threshold, 50);
        assert!(!pass_custom.proxy_only);

        let pass_proxy = InliningPass::new(0, true);
        assert_eq!(pass_proxy.inline_threshold, 0);
        assert!(pass_proxy.proxy_only);
    }

    #[test]
    fn test_inline_constant_return() {
        // Callee: returns constant 42
        let callee_token = Token::new(0x06000002);
        let (callee_ssa, callee_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)
        };

        // Caller: calls callee
        let caller_token = Token::new(0x06000001);
        let (mut caller_ssa, call_dest) = {
            let mut dest_out = SsaVarId::new();
            let ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
                f.block(0, |b| {
                    let dest = b.call(MethodRef::new(callee_token), &[]);
                    dest_out = dest;
                    b.ret_val(dest);
                });
            });
            (ssa, dest_out)
        };

        // Set up context
        let ctx = test_context();
        ctx.set_ssa(callee_token, callee_ssa.clone());

        // Get the call op before creating context (need to borrow caller_ssa)
        let call_op = caller_ssa.block(0).unwrap().instructions()[0].op().clone();

        // Create inlining context and inline
        let pass = InliningPass::new(20, false);
        let assembly = test_assembly();
        let mut inline_ctx =
            InliningContext::new(&pass, &mut caller_ssa, caller_token, &ctx, &assembly);
        let result = inline_ctx.inline_call(&callee_ssa, 0, 0, &call_op, callee_token);

        assert!(result, "Inlining should succeed");

        // Check that the call was replaced with a constant
        let block = inline_ctx.caller_ssa.block(0).unwrap();
        let first_instr = &block.instructions()[0];
        match first_instr.op() {
            SsaOp::Const { dest, value } => {
                assert_eq!(*dest, call_dest);
                assert_eq!(*value, ConstValue::I32(42));
            }
            other => panic!("Expected Const, got {:?}", other),
        }

        // Verify the callee's const was created correctly
        let _ = callee_v0;
    }

    #[test]
    fn test_inline_void_pure() {
        // Callee: void method that does nothing impure
        let callee_token = Token::new(0x06000002);
        let callee_ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| b.ret());
        });

        // Caller: calls callee
        let caller_token = Token::new(0x06000001);
        let mut caller_ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| {
                b.call_void(MethodRef::new(callee_token), &[]);
                b.ret();
            });
        });

        // Set up context
        let ctx = test_context();
        ctx.set_ssa(callee_token, callee_ssa.clone());

        // Get the call op before creating context
        let call_op = caller_ssa.block(0).unwrap().instructions()[0].op().clone();

        // Create inlining context and inline
        let pass = InliningPass::new(20, false);
        let assembly = test_assembly();
        let mut inline_ctx =
            InliningContext::new(&pass, &mut caller_ssa, caller_token, &ctx, &assembly);
        let result = inline_ctx.inline_call(&callee_ssa, 0, 0, &call_op, callee_token);

        assert!(result, "Inlining should succeed");

        // Check that the call was replaced with Nop
        let block = inline_ctx.caller_ssa.block(0).unwrap();
        let first_instr = &block.instructions()[0];
        assert!(
            matches!(first_instr.op(), SsaOp::Nop),
            "Expected Nop, got {:?}",
            first_instr.op()
        );
    }

    #[test]
    fn test_no_inline_self_recursion() {
        let pass = InliningPass::new(20, false);
        let token = Token::new(0x06000001);
        let ctx = test_context();
        let assembly = test_assembly();

        // Create a dummy SSA for the context
        let mut dummy_ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| b.ret());
        });

        let inline_ctx = InliningContext::new(&pass, &mut dummy_ssa, token, &ctx, &assembly);

        // Self-recursion check - is_valid_target returns false for self
        assert!(!inline_ctx.is_valid_target(token));
    }

    #[test]
    fn test_no_inline_large_method() {
        let callee_token = Token::new(0x06000002);
        let caller_token = Token::new(0x06000001);

        // Create a callee with many instructions (> threshold)
        let callee_ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| {
                for _ in 0..30 {
                    let _ = b.const_i32(0);
                }
                b.ret();
            });
        });

        let ctx = test_context();
        ctx.set_ssa(callee_token, callee_ssa);

        let pass = InliningPass::new(20, false);
        let assembly = test_assembly();

        // Create a dummy caller SSA
        let mut caller_ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| b.ret());
        });

        let inline_ctx =
            InliningContext::new(&pass, &mut caller_ssa, caller_token, &ctx, &assembly);
        assert!(!inline_ctx.should_inline(callee_token));
    }

    #[test]
    fn test_inline_full_computation() {
        // Callee: returns arg0 + 10
        // This is a PureComputation that requires full inlining
        let callee_token = Token::new(0x06000002);
        let callee_ssa = SsaFunctionBuilder::new(1, 0).build_with(|f| {
            let param0 = f.arg(0);
            f.block(0, |b| {
                let v1 = b.const_i32(10);
                let v2 = b.add(param0, v1);
                b.ret_val(v2);
            });
        });

        // Caller: calls callee with v0=5, stores result in v3
        let caller_token = Token::new(0x06000001);
        let mut caller_ssa = SsaFunctionBuilder::new(0, 0).build_with(|f| {
            f.block(0, |b| {
                let v0 = b.const_i32(5);
                let v1 = b.call(MethodRef::new(callee_token), &[v0]);
                b.ret_val(v1);
            });
        });

        // Set up context
        let ctx = test_context();
        ctx.set_ssa(callee_token, callee_ssa.clone());

        // Get the call op before creating context
        let call_op = caller_ssa.block(0).unwrap().instructions()[1].op().clone();

        // Create inlining context and inline
        let pass = InliningPass::new(20, false);
        let assembly = test_assembly();
        let mut inline_ctx =
            InliningContext::new(&pass, &mut caller_ssa, caller_token, &ctx, &assembly);
        let result = inline_ctx.inline_call(&callee_ssa, 0, 1, &call_op, callee_token);

        assert!(result, "Full inlining should succeed");

        // Verify the caller now has more instructions
        let block = inline_ctx.caller_ssa.block(0).unwrap();
        assert!(
            block.instructions().len() > 3,
            "Expected inlined instructions, got {} instructions",
            block.instructions().len()
        );

        // Check that the second instruction is now the inlined const 10
        let second_instr = &block.instructions()[1];
        match second_instr.op() {
            SsaOp::Const { value, .. } => {
                assert_eq!(*value, ConstValue::I32(10), "Expected inlined constant 10");
            }
            other => panic!("Expected Const for inlined instruction, got {:?}", other),
        }

        // Check that there's an Add instruction
        let has_add = block
            .instructions()
            .iter()
            .any(|i| matches!(i.op(), SsaOp::Add { .. }));
        assert!(has_add, "Expected Add instruction after inlining");
    }

    #[test]
    fn test_detect_proxy_void() {
        // Proxy: void method that forwards to Console.WriteLine(string)
        // void proxy(string s) { Console.WriteLine(s); }
        let target_token = Token::new(0x0A000001); // External method

        let proxy_ssa = SsaFunctionBuilder::new(1, 0).build_with(|f| {
            let param0 = f.arg(0);
            f.block(0, |b| {
                b.call_void(MethodRef::new(target_token), &[param0]);
                b.ret();
            });
        });

        // Should detect this as a proxy
        let result = InliningPass::detect_proxy_pattern(&proxy_ssa);
        assert!(result.is_some(), "Should detect void proxy");

        let (target, arg_mapping, is_virtual) = result.unwrap();
        assert_eq!(target.token(), target_token);
        assert_eq!(arg_mapping, vec![0]); // First arg maps to param 0
        assert!(!is_virtual);
    }

    #[test]
    fn test_detect_proxy_with_return() {
        // Proxy: string method that forwards to String.Format
        // string proxy(string fmt, object arg) { return String.Format(fmt, arg); }
        let target_token = Token::new(0x0A000002);

        let proxy_ssa = SsaFunctionBuilder::new(2, 0).build_with(|f| {
            let param0 = f.arg(0);
            let param1 = f.arg(1);
            f.block(0, |b| {
                let result = b.call(MethodRef::new(target_token), &[param0, param1]);
                b.ret_val(result);
            });
        });

        let result = InliningPass::detect_proxy_pattern(&proxy_ssa);
        assert!(result.is_some(), "Should detect proxy with return");

        let (target, arg_mapping, is_virtual) = result.unwrap();
        assert_eq!(target.token(), target_token);
        assert_eq!(arg_mapping, vec![0, 1]);
        assert!(!is_virtual);
    }

    #[test]
    fn test_detect_proxy_reordered_args() {
        // Proxy: reorders arguments
        // int proxy(int a, int b) { return target(b, a); }
        let target_token = Token::new(0x0A000003);

        let proxy_ssa = SsaFunctionBuilder::new(2, 0).build_with(|f| {
            let param0 = f.arg(0);
            let param1 = f.arg(1);
            f.block(0, |b| {
                // Pass args in reverse order
                let result = b.call(MethodRef::new(target_token), &[param1, param0]);
                b.ret_val(result);
            });
        });

        let result = InliningPass::detect_proxy_pattern(&proxy_ssa);
        assert!(result.is_some(), "Should detect proxy with reordered args");

        let (target, arg_mapping, is_virtual) = result.unwrap();
        assert_eq!(target.token(), target_token);
        assert_eq!(arg_mapping, vec![1, 0]); // Reversed
        assert!(!is_virtual);
    }

    #[test]
    fn test_not_proxy_with_computation() {
        // Not a proxy: adds a constant before calling
        // int notProxy(int a) { return target(a + 1); }
        let target_token = Token::new(0x0A000004);

        let not_proxy_ssa = SsaFunctionBuilder::new(1, 0).build_with(|f| {
            let param0 = f.arg(0);
            f.block(0, |b| {
                let one = b.const_i32(1);
                let sum = b.add(param0, one);
                let result = b.call(MethodRef::new(target_token), &[sum]);
                b.ret_val(result);
            });
        });

        let result = InliningPass::detect_proxy_pattern(&not_proxy_ssa);
        assert!(
            result.is_none(),
            "Should NOT detect as proxy - has computation"
        );
    }

    #[test]
    fn test_devirtualize_proxy() {
        // Setup: caller calls proxy, proxy forwards to target
        let proxy_token = Token::new(0x06000002);
        let target_token = Token::new(0x0A000001);

        // Proxy: forwards arg0 to target
        let proxy_ssa = SsaFunctionBuilder::new(1, 0).build_with(|f| {
            let param0 = f.arg(0);
            f.block(0, |b| {
                b.call_void(MethodRef::new(target_token), &[param0]);
                b.ret();
            });
        });

        // Caller: calls proxy
        let caller_token = Token::new(0x06000001);
        let mut caller_ssa = SsaFunctionBuilder::new(1, 0).build_with(|f| {
            let arg0 = f.arg(0);
            f.block(0, |b| {
                b.call_void(MethodRef::new(proxy_token), &[arg0]);
                b.ret();
            });
        });

        let ctx = test_context();
        ctx.set_ssa(proxy_token, proxy_ssa);

        // Get the proxy pattern
        let proxy_pattern = ctx
            .with_ssa(proxy_token, InliningPass::detect_proxy_pattern)
            .flatten();
        assert!(proxy_pattern.is_some(), "Proxy should be detected");

        let (target_method, arg_mapping, is_virtual) = proxy_pattern.unwrap();

        // Get call_op before creating context
        let call_op = caller_ssa.block(0).unwrap().instructions()[0].op().clone();

        // Devirtualize using InliningContext
        let pass = InliningPass::new(20, false);
        let assembly = test_assembly();
        let mut inline_ctx =
            InliningContext::new(&pass, &mut caller_ssa, caller_token, &ctx, &assembly);
        let result = inline_ctx.devirtualize_proxy(
            0,
            0,
            &call_op,
            target_method,
            &arg_mapping,
            is_virtual,
            proxy_token,
        );

        assert!(result, "Devirtualization should succeed");

        // Check that the call target changed
        let block = inline_ctx.caller_ssa.block(0).unwrap();
        let first_instr = &block.instructions()[0];
        match first_instr.op() {
            SsaOp::Call { method, .. } => {
                assert_eq!(
                    method.token(),
                    target_token,
                    "Call should now target {:?}",
                    target_token
                );
            }
            other => panic!("Expected Call, got {:?}", other),
        }
    }
}