forked from anthropics/claudes-c-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop_unroll.rs
More file actions
1326 lines (1203 loc) · 47.4 KB
/
loop_unroll.rs
File metadata and controls
1326 lines (1203 loc) · 47.4 KB
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
//! Loop unrolling pass.
//!
//! Unrolls small inner loops using "unroll with intermediate IV steps and
//! early exits". Replicates the loop body K times per unrolled cycle, with
//! an exit-condition check inserted between each copy. This handles
//! non-multiple-K trip counts without a separate cleanup loop: whichever
//! intermediate check fires first terminates the partial cycle.
//!
//! Example — 4× unrolled loop:
//!
//! ```text
//! header: %iv = Phi [init, %iv_next]
//! %cond = Cmp %iv, limit
//! CondBranch %cond, exit, body_entry
//!
//! [original body blocks] → exit_check_1
//!
//! exit_check_1:
//! %iv_1 = Add %iv, step
//! %cond_1 = Cmp %iv_1, limit
//! CondBranch %cond_1, exit, body_copy_2_entry
//!
//! [body_copy_2] → exit_check_2
//! ...
//! exit_check_3 → [body_copy_4] → latch
//!
//! latch: %iv_next = Add %iv_3, step ← was Add %iv, step
//! Branch header
//! ```
use crate::common::fx_hash::{FxHashMap, FxHashSet};
use crate::common::types::IrType;
use crate::ir::analysis::CfgAnalysis;
use crate::ir::reexports::{
BasicBlock, BlockId, Instruction, IrBinOp, IrCmpOp, IrConst, IrFunction, Operand, Terminator,
Value,
};
use super::loop_analysis;
/// Maximum number of body-work blocks (body excluding header and latch) for
/// a loop to be eligible. Prevents excessive code size growth.
const MAX_UNROLL_BODY_BLOCKS: usize = 8;
/// Choose the unroll factor based on total instruction count in body-work blocks.
fn choose_unroll_factor(body_inst_count: usize) -> u32 {
match body_inst_count {
0..=8 => 8,
9..=20 => 4,
21..=60 => 2,
_ => 1, // too large — skip
}
}
/// All information needed to perform the unrolling transformation.
struct UnrollCandidate {
/// Block index of the loop header (has the phi + condition check).
header: usize,
/// Block index of the single latch (has the IV increment + back-branch).
latch: usize,
/// Body blocks, excluding header and latch.
body_work: Vec<usize>,
/// Index into `body_work` whose label equals `body_entry`.
body_entry_work_idx: usize,
/// Index into `body_work` of the block that branches to the latch.
pre_latch_work_idx: usize,
/// Exit block label (outside the loop, target of the header's exit branch).
exit_target: BlockId,
/// First in-loop block label (target of the header's continue branch).
body_entry: BlockId,
/// The IV phi value defined in the header.
iv_phi: Value,
/// Type of the IV.
iv_ty: IrType,
/// Constant step added to IV per iteration.
iv_step: i64,
/// Comparison operator used in the exit condition.
exit_cmp_op: IrCmpOp,
/// Type of the exit comparison instruction.
exit_cmp_ty: IrType,
/// The loop-invariant operand of the exit comparison (the "limit").
exit_limit: Operand,
/// `true` if the IV is the left-hand operand of the exit Cmp.
iv_is_lhs: bool,
/// `true` if cond==true means exit (false means continue).
exit_cond_positive: bool,
/// Index of the `Add %iv, step` instruction inside the latch block.
latch_iv_incr_idx: usize,
/// Number of times to replicate the loop body (K). Always ≥ 2.
unroll_factor: u32,
}
/// Run the loop-unrolling pass on one function. Returns the number of loops
/// that were successfully unrolled.
pub(crate) fn unroll_loops(func: &mut IrFunction) -> usize {
if func.blocks.len() < 2 {
return 0;
}
let cfg = CfgAnalysis::build(func);
let raw = loop_analysis::find_natural_loops(
cfg.num_blocks, &cfg.preds, &cfg.succs, &cfg.idom,
);
if raw.is_empty() {
return 0;
}
let loops = loop_analysis::merge_loops_by_header(raw);
// Set of all loop-header block indices (used for nested-loop detection).
let all_headers: FxHashSet<usize> = loops.iter().map(|l| l.header).collect();
// Collect and sort candidates by body size (smallest first = innermost first).
let mut candidates: Vec<UnrollCandidate> = loops
.iter()
.filter_map(|lp| analyze_loop(func, lp, &cfg, &all_headers))
.collect();
candidates.sort_by_key(|c| c.body_work.len());
let mut count = 0;
for c in candidates {
if do_unroll(func, c) {
count += 1;
}
}
count
}
// ── Eligibility analysis ──────────────────────────────────────────────────────
fn analyze_loop(
func: &IrFunction,
lp: &loop_analysis::NaturalLoop,
cfg: &CfgAnalysis,
all_headers: &FxHashSet<usize>,
) -> Option<UnrollCandidate> {
let header = lp.header;
// 1. Size check: body (header + latch + work blocks) must be small.
if lp.body.len() > MAX_UNROLL_BODY_BLOCKS + 2 {
return None;
}
// 2. Single latch: exactly one block in body has a back-edge to header.
let back_preds: Vec<usize> = cfg
.preds
.row(header)
.iter()
.map(|&p| p as usize)
.filter(|p| lp.body.contains(p))
.collect();
if back_preds.len() != 1 {
return None;
}
let latch = back_preds[0];
// Latch must terminate with an unconditional Branch back to the header.
let header_label = func.blocks[header].label;
match &func.blocks[latch].terminator {
Terminator::Branch(lbl) if *lbl == header_label => {}
_ => return None,
}
// 3. A unique preheader must exist.
loop_analysis::find_preheader(header, &lp.body, &cfg.preds)?;
// 4. body_work = body \ {header, latch}; must be non-empty.
let body_work: Vec<usize> = lp
.body
.iter()
.copied()
.filter(|&b| b != header && b != latch)
.collect();
if body_work.is_empty() {
return None;
}
// 5. No nested loops: body_work blocks must not be headers of other loops.
for &b in &body_work {
if all_headers.contains(&b) {
return None;
}
}
// 6. No disqualifying instructions in body_work.
for &bi in &body_work {
for inst in &func.blocks[bi].instructions {
match inst {
Instruction::Call { .. }
| Instruction::CallIndirect { .. }
| Instruction::InlineAsm { .. }
| Instruction::AtomicRmw { .. }
| Instruction::AtomicCmpxchg { .. }
| Instruction::AtomicLoad { .. }
| Instruction::AtomicStore { .. }
| Instruction::DynAlloca { .. } => return None,
_ => {}
}
}
}
// 7. Find basic IV: a phi in the header whose back-edge value is
// Add(%iv, const_step) in the latch.
let latch_label = func.blocks[latch].label;
let (iv_phi, iv_ty, iv_step, latch_iv_incr_idx) =
find_iv_in_loop(func, header, latch, latch_label)?;
// 8. Detect the exit condition from the header's CondBranch.
let (exit_target, body_entry, exit_cmp_op, exit_cmp_ty, exit_limit, iv_is_lhs, exit_cond_positive) =
find_exit_condition(func, header, &lp.body, iv_phi)?;
// 9. Count body instructions and select the unroll factor.
let body_inst_count: usize = body_work
.iter()
.map(|&bi| func.blocks[bi].instructions.len())
.sum();
let unroll_factor = choose_unroll_factor(body_inst_count);
if unroll_factor <= 1 {
return None;
}
// 10. Find body_entry_work_idx and ensure a unique pre-latch block.
let body_entry_work_idx = body_work
.iter()
.position(|&bi| func.blocks[bi].label == body_entry)?;
let mut pre_latch_work_idx: Option<usize> = None;
for (j, &bi) in body_work.iter().enumerate() {
if block_has_succ(&func.blocks[bi].terminator, latch_label) {
if pre_latch_work_idx.is_some() {
return None; // multiple blocks branch to latch — too complex
}
pre_latch_work_idx = Some(j);
}
}
let pre_latch_work_idx = pre_latch_work_idx?;
// 11. Exit-block phi eligibility: all incoming-from-header values must be
// loop-invariant (not defined in body_work), so each new exit edge can
// carry the same value without creating new definitions.
if let Some(exit_bi) = func.blocks.iter().position(|b| b.label == exit_target) {
for inst in &func.blocks[exit_bi].instructions {
if let Instruction::Phi { incoming, .. } = inst {
for (op, src_label) in incoming {
if *src_label == header_label {
if let Operand::Value(v) = op {
if is_defined_in_body(v.0, &lp.body, func) {
return None;
}
}
}
}
}
}
}
// Skip unrolling for I32/U32 IV types on 64-bit targets when the loop body
// contains Cast(I32→I64) or GEP instructions that widen the IV. The unroller
// creates intermediate IV values at the narrow type, and in complex functions
// (like SQLite's 255K-line amalgamation) the widened values can interact
// incorrectly with subsequent optimization passes.
// Simple loops without IV widening (pure I32 arithmetic) are safe to unroll.
if !crate::common::types::target_is_32bit() && iv_ty.size() < 8 && iv_ty.is_integer() {
let has_iv_widening = body_work.iter().any(|&bi| {
func.blocks[bi].instructions.iter().any(|inst| {
match inst {
Instruction::Cast { src: Operand::Value(v), from_ty, to_ty, .. } => {
v.0 == iv_phi.0
&& matches!(from_ty, IrType::I32 | IrType::U32)
&& matches!(to_ty, IrType::I64 | IrType::U64 | IrType::Ptr)
}
Instruction::GetElementPtr { offset: Operand::Value(v), .. } => {
v.0 == iv_phi.0
}
_ => false,
}
})
});
if has_iv_widening {
return None;
}
}
Some(UnrollCandidate {
header,
latch,
body_work,
body_entry_work_idx,
pre_latch_work_idx,
exit_target,
body_entry,
iv_phi,
iv_ty,
iv_step,
exit_cmp_op,
exit_cmp_ty,
exit_limit,
iv_is_lhs,
exit_cond_positive,
latch_iv_incr_idx,
unroll_factor,
})
}
/// Find a basic induction variable in the loop header and its increment in
/// the latch. Returns `(phi_dest, ty, step, latch_incr_idx)`.
fn find_iv_in_loop(
func: &IrFunction,
header: usize,
latch: usize,
latch_label: BlockId,
) -> Option<(Value, IrType, i64, usize)> {
for inst in &func.blocks[header].instructions {
let (phi_dest, ty, incoming) = match inst {
Instruction::Phi { dest, ty, incoming } if ty.is_integer() => (dest, ty, incoming),
_ => continue,
};
// Value flowing into the header from the latch (the back-edge value).
let back_val = incoming
.iter()
.find(|(_, lbl)| *lbl == latch_label)
.and_then(|(op, _)| {
if let Operand::Value(v) = op { Some(*v) } else { None }
});
let back_val = back_val?;
// Look for `Add(phi_dest, const_step)` or `Add(const_step, phi_dest)`
// in the latch that produces `back_val`.
let phi_id = phi_dest.0;
for (idx, latch_inst) in func.blocks[latch].instructions.iter().enumerate() {
if let Instruction::BinOp { dest, op: IrBinOp::Add, lhs, rhs, .. } = latch_inst {
if *dest != back_val {
continue;
}
let step = match (lhs, rhs) {
(Operand::Value(v), Operand::Const(c)) if v.0 == phi_id => c.to_i64(),
(Operand::Const(c), Operand::Value(v)) if v.0 == phi_id => c.to_i64(),
_ => None,
};
if let Some(step) = step {
return Some((*phi_dest, *ty, step, idx));
}
}
}
}
None
}
/// Detect the exit condition from the header's CondBranch terminator.
///
/// Returns `(exit_target, body_entry, cmp_op, cmp_ty, limit, iv_is_lhs, exit_cond_positive)`.
/// `exit_cond_positive` is `true` when the condition evaluating to `true` means "exit".
fn find_exit_condition(
func: &IrFunction,
header: usize,
loop_body: &FxHashSet<usize>,
iv_phi: Value,
) -> Option<(BlockId, BlockId, IrCmpOp, IrType, Operand, bool, bool)> {
let header_block = &func.blocks[header];
let (cond_op, true_label, false_label) = match &header_block.terminator {
Terminator::CondBranch { cond, true_label, false_label } => {
(*cond, *true_label, *false_label)
}
_ => return None,
};
// Map labels to block indices for in-loop membership check.
let label_to_idx: FxHashMap<BlockId, usize> = func
.blocks
.iter()
.enumerate()
.map(|(i, b)| (b.label, i))
.collect();
let true_in_loop = label_to_idx
.get(&true_label)
.map(|&bi| loop_body.contains(&bi))
.unwrap_or(false);
let false_in_loop = label_to_idx
.get(&false_label)
.map(|&bi| loop_body.contains(&bi))
.unwrap_or(false);
// Exactly one branch must be in-loop, the other is the exit.
if true_in_loop == false_in_loop {
return None;
}
let (exit_target, body_entry, exit_cond_positive) = if !true_in_loop {
(true_label, false_label, true)
} else {
(false_label, true_label, false)
};
// Trace the condition value to a Cmp instruction (through at most one Cast).
let cond_id = match cond_op {
Operand::Value(v) => v.0,
_ => return None,
};
// Build a map of value-id → instruction for the header.
let mut hdr_defs: FxHashMap<u32, &Instruction> = FxHashMap::default();
for inst in &header_block.instructions {
if let Some(dest) = inst.dest() {
hdr_defs.insert(dest.0, inst);
}
}
// Look through one Cast.
let cmp_id = match hdr_defs.get(&cond_id) {
Some(Instruction::Cast { src: Operand::Value(v), .. }) => v.0,
_ => cond_id,
};
let (cmp_op, cmp_lhs, cmp_rhs, cmp_ty) = match hdr_defs.get(&cmp_id) {
Some(Instruction::Cmp { op, lhs, rhs, ty, .. }) => (*op, *lhs, *rhs, *ty),
_ => return None,
};
let iv_id = iv_phi.0;
// One Cmp operand must be exactly the IV phi; the other must be loop-invariant.
let (iv_is_lhs, limit_op) =
if matches!(cmp_lhs, Operand::Value(v) if v.0 == iv_id)
&& is_loop_invariant_op(cmp_rhs, loop_body, func)
{
(true, cmp_rhs)
} else if matches!(cmp_rhs, Operand::Value(v) if v.0 == iv_id)
&& is_loop_invariant_op(cmp_lhs, loop_body, func)
{
(false, cmp_lhs)
} else {
return None;
};
Some((exit_target, body_entry, cmp_op, cmp_ty, limit_op, iv_is_lhs, exit_cond_positive))
}
// ── CFG helpers ───────────────────────────────────────────────────────────────
fn is_loop_invariant_op(op: Operand, loop_body: &FxHashSet<usize>, func: &IrFunction) -> bool {
match op {
Operand::Const(_) => true,
Operand::Value(v) => !is_defined_in_body(v.0, loop_body, func),
}
}
fn is_defined_in_body(val_id: u32, loop_body: &FxHashSet<usize>, func: &IrFunction) -> bool {
for &bi in loop_body {
if bi < func.blocks.len() {
for inst in &func.blocks[bi].instructions {
if let Some(dest) = inst.dest() {
if dest.0 == val_id {
return true;
}
}
}
}
}
false
}
fn block_has_succ(term: &Terminator, target: BlockId) -> bool {
match term {
Terminator::Branch(lbl) => *lbl == target,
Terminator::CondBranch { true_label, false_label, .. } => {
*true_label == target || *false_label == target
}
_ => false,
}
}
/// Replace `old` with `new` in one specific block-label slot of a terminator.
fn redirect_label(term: &mut Terminator, old: BlockId, new: BlockId) {
match term {
Terminator::Branch(lbl) if *lbl == old => *lbl = new,
Terminator::CondBranch { true_label, false_label, .. } => {
if *true_label == old {
*true_label = new;
}
if *false_label == old {
*false_label = new;
}
}
_ => {}
}
}
/// Apply a block-label rename map to all branch targets in a terminator.
fn replace_block_ids(term: &mut Terminator, map: &FxHashMap<BlockId, BlockId>) {
match term {
Terminator::Branch(lbl) => {
if let Some(&new) = map.get(lbl) {
*lbl = new;
}
}
Terminator::CondBranch { true_label, false_label, .. } => {
if let Some(&new) = map.get(true_label) {
*true_label = new;
}
if let Some(&new) = map.get(false_label) {
*false_label = new;
}
}
Terminator::Switch { cases, default, .. } => {
if let Some(&new) = map.get(default) {
*default = new;
}
for (_, lbl) in cases {
if let Some(&new) = map.get(lbl) {
*lbl = new;
}
}
}
_ => {}
}
}
// ── Transformation ────────────────────────────────────────────────────────────
fn do_unroll(func: &mut IrFunction, c: UnrollCandidate) -> bool {
let k = c.unroll_factor as usize; // total copies (1 original + k-1 clones)
let num_new = k - 1; // number of clones = number of exit-check blocks
if num_new == 0 {
return false;
}
let header_label = func.blocks[c.header].label;
let latch_label = func.blocks[c.latch].label;
// ── Pre-allocate all new BlockIds and Values ──────────────────────────────
let max_label = func.blocks.iter().map(|b| b.label.0).max().unwrap_or(0);
let mut next_label = max_label + 1;
let mut next_val = func.next_value_id;
// iv_vals[j] = %iv_{j+1} (used in exit_check_{j+1} and clone[j])
// cond_vals[j] = %cond_{j+1} (used in exit_check_{j+1})
// ec_labels[j] = label of exit_check_{j+1}
// cl_labels[j] = labels of clone[j]'s body_work blocks (parallel to body_work)
let iv_vals: Vec<Value> = (0..num_new)
.map(|_| { let v = Value(next_val); next_val += 1; v })
.collect();
let cond_vals: Vec<Value> = (0..num_new)
.map(|_| { let v = Value(next_val); next_val += 1; v })
.collect();
let ec_labels: Vec<BlockId> = (0..num_new)
.map(|_| { let l = BlockId(next_label); next_label += 1; l })
.collect();
let cl_labels: Vec<Vec<BlockId>> = (0..num_new)
.map(|_| {
(0..c.body_work.len())
.map(|_| { let l = BlockId(next_label); next_label += 1; l })
.collect()
})
.collect();
// Build value-rename maps for each clone.
// clone_vmaps[j]: old_value_id → fresh_value_id, seeded with iv_phi → iv_vals[j].
let mut clone_vmaps: Vec<FxHashMap<u32, u32>> = Vec::with_capacity(num_new);
for j in 0..num_new {
let mut vmap: FxHashMap<u32, u32> = FxHashMap::default();
vmap.insert(c.iv_phi.0, iv_vals[j].0);
for &bi in &c.body_work {
for inst in &func.blocks[bi].instructions {
if let Some(dest) = inst.dest() {
vmap.entry(dest.0).or_insert_with(|| {
let v = next_val;
next_val += 1;
v
});
}
}
}
clone_vmaps.push(vmap);
}
func.next_value_id = next_val;
// ── Build new blocks (read-only access to func.blocks) ───────────────────
let mut new_blocks: Vec<BasicBlock> = Vec::new();
for j in 0..num_new {
// The IV value feeding into this exit check:
// j=0: prev_iv = %iv_phi (the header phi)
// j>0: prev_iv = iv_vals[j-1]
let prev_iv: Operand = if j == 0 {
Operand::Value(c.iv_phi)
} else {
Operand::Value(iv_vals[j - 1])
};
let iv_j = iv_vals[j];
let cond_j = cond_vals[j];
// Entry of clone[j] (the block exit_check_{j+1} jumps into on "continue").
let clone_entry = cl_labels[j][c.body_entry_work_idx];
// ── Build exit_check_{j+1} ────────────────────────────────────────
let cmp_lhs = if c.iv_is_lhs { Operand::Value(iv_j) } else { c.exit_limit };
let cmp_rhs = if c.iv_is_lhs { c.exit_limit } else { Operand::Value(iv_j) };
let (ec_true, ec_false) = if c.exit_cond_positive {
(c.exit_target, clone_entry)
} else {
(clone_entry, c.exit_target)
};
new_blocks.push(BasicBlock {
label: ec_labels[j],
instructions: vec![
Instruction::BinOp {
dest: iv_j,
op: IrBinOp::Add,
lhs: prev_iv,
rhs: Operand::Const(IrConst::from_i64(c.iv_step, c.iv_ty)),
ty: c.iv_ty,
},
Instruction::Cmp {
dest: cond_j,
op: c.exit_cmp_op,
lhs: cmp_lhs,
rhs: cmp_rhs,
ty: c.exit_cmp_ty,
},
],
terminator: Terminator::CondBranch {
cond: Operand::Value(cond_j),
true_label: ec_true,
false_label: ec_false,
},
source_spans: Vec::new(),
});
// ── Build clone[j] (cloned body_work blocks) ──────────────────────
// Block-label rename map for internal branches within this clone.
let mut blk_map: FxHashMap<BlockId, BlockId> = FxHashMap::default();
for (i, &bi) in c.body_work.iter().enumerate() {
blk_map.insert(func.blocks[bi].label, cl_labels[j][i]);
}
// Where does clone[j]'s pre-latch block redirect after "latch"?
// j < num_new-1: → exit_check_{j+2} (= ec_labels[j+1])
// j = num_new-1: → original latch (no redirect)
let post_latch_redirect: Option<BlockId> = if j + 1 < num_new {
Some(ec_labels[j + 1])
} else {
None // last clone keeps going to original latch
};
let vmap = &clone_vmaps[j];
for (i, &bi) in c.body_work.iter().enumerate() {
let orig = &func.blocks[bi];
let new_insts: Vec<Instruction> = orig
.instructions
.iter()
.map(|inst| {
let mut cloned = inst.clone();
replace_values_in_inst(&mut cloned, vmap);
rename_inst_dest(&mut cloned, vmap);
cloned
})
.collect();
let mut new_term = orig.terminator.clone();
replace_values_in_terminator(&mut new_term, vmap);
replace_block_ids(&mut new_term, &blk_map);
// Redirect latch edge from pre-latch block.
if i == c.pre_latch_work_idx {
if let Some(redirect_to) = post_latch_redirect {
redirect_label(&mut new_term, latch_label, redirect_to);
}
// else: last clone's pre-latch block stays pointing at original latch.
}
new_blocks.push(BasicBlock {
label: cl_labels[j][i],
instructions: new_insts,
terminator: new_term,
source_spans: Vec::new(),
});
}
}
// ── Mutate existing blocks ────────────────────────────────────────────────
// Step 3: Redirect original body's pre-latch block from latch → exit_check_1.
redirect_label(
&mut func.blocks[c.body_work[c.pre_latch_work_idx]].terminator,
latch_label,
ec_labels[0],
);
// Step 4: Update latch's IV increment: swap iv_phi → iv_{K-1} (= iv_vals[num_new-1]).
let last_iv = iv_vals[num_new - 1];
if let Instruction::BinOp { op: IrBinOp::Add, lhs, rhs, .. } =
&mut func.blocks[c.latch].instructions[c.latch_iv_incr_idx]
{
if matches!(lhs, Operand::Value(v) if v.0 == c.iv_phi.0) {
*lhs = Operand::Value(last_iv);
} else if matches!(rhs, Operand::Value(v) if v.0 == c.iv_phi.0) {
*rhs = Operand::Value(last_iv);
}
}
// Step 5: For any phi in the exit block that has an incoming from header,
// add the same value as incoming from each new exit-check block.
if let Some(exit_bi) = func.blocks.iter().position(|b| b.label == c.exit_target) {
// Collect (phi_index, value) pairs where value came from the header.
let phi_header_vals: Vec<(usize, Operand)> = func.blocks[exit_bi]
.instructions
.iter()
.enumerate()
.filter_map(|(phi_idx, inst)| {
if let Instruction::Phi { incoming, .. } = inst {
incoming
.iter()
.find(|(_, lbl)| *lbl == header_label)
.map(|(op, _)| (phi_idx, *op))
} else {
None
}
})
.collect();
for (phi_idx, op) in phi_header_vals {
for j in 0..num_new {
if let Instruction::Phi { incoming, .. } =
&mut func.blocks[exit_bi].instructions[phi_idx]
{
incoming.push((op, ec_labels[j]));
}
}
}
}
// Step 6: Append all new blocks.
func.blocks.extend(new_blocks);
true
}
// ── Value-replacement helpers (adapted from tail_call_elim.rs) ────────────────
/// Rename the SSA *definition* site (dest) of an instruction using `map`.
/// Only variants that produce an SSA value are affected; others are a no-op.
fn rename_inst_dest(inst: &mut Instruction, map: &FxHashMap<u32, u32>) {
match inst {
Instruction::Alloca { dest, .. }
| Instruction::DynAlloca { dest, .. }
| Instruction::Load { dest, .. }
| Instruction::BinOp { dest, .. }
| Instruction::UnaryOp { dest, .. }
| Instruction::Cmp { dest, .. }
| Instruction::GetElementPtr { dest, .. }
| Instruction::Cast { dest, .. }
| Instruction::Copy { dest, .. }
| Instruction::GlobalAddr { dest, .. }
| Instruction::VaArg { dest, .. }
| Instruction::AtomicRmw { dest, .. }
| Instruction::AtomicCmpxchg { dest, .. }
| Instruction::AtomicLoad { dest, .. }
| Instruction::Phi { dest, .. }
| Instruction::LabelAddr { dest, .. }
| Instruction::GetReturnF64Second { dest }
| Instruction::GetReturnF32Second { dest }
| Instruction::GetReturnF128Second { dest }
| Instruction::Select { dest, .. }
| Instruction::StackSave { dest }
| Instruction::ParamRef { dest, .. } => replace_val(dest, map),
Instruction::Call { info, .. } | Instruction::CallIndirect { info, .. } => {
if let Some(dest) = &mut info.dest {
replace_val(dest, map);
}
}
Instruction::Intrinsic { dest, .. } => {
if let Some(dest) = dest {
replace_val(dest, map);
}
}
// No SSA destination.
Instruction::Store { .. }
| Instruction::Memcpy { .. }
| Instruction::VaArgStruct { .. }
| Instruction::VaStart { .. }
| Instruction::VaEnd { .. }
| Instruction::VaCopy { .. }
| Instruction::AtomicStore { .. }
| Instruction::Fence { .. }
| Instruction::SetReturnF64Second { .. }
| Instruction::SetReturnF32Second { .. }
| Instruction::SetReturnF128Second { .. }
| Instruction::InlineAsm { .. }
| Instruction::StackRestore { .. } => {}
}
}
#[inline]
fn replace_val(v: &mut Value, map: &FxHashMap<u32, u32>) {
if let Some(&new_id) = map.get(&v.0) {
*v = Value(new_id);
}
}
#[inline]
fn replace_op(op: &mut Operand, map: &FxHashMap<u32, u32>) {
if let Operand::Value(v) = op {
replace_val(v, map);
}
}
fn replace_values_in_inst(inst: &mut Instruction, map: &FxHashMap<u32, u32>) {
match inst {
// Definitions with no operands to replace.
Instruction::ParamRef { .. }
| Instruction::Alloca { .. }
| Instruction::GlobalAddr { .. }
| Instruction::LabelAddr { .. }
| Instruction::Fence { .. }
| Instruction::StackSave { .. }
| Instruction::GetReturnF64Second { .. }
| Instruction::GetReturnF32Second { .. }
| Instruction::GetReturnF128Second { .. } => {}
// Memory.
Instruction::Store { val, ptr, .. } => {
replace_op(val, map);
replace_val(ptr, map);
}
Instruction::Load { ptr, .. } => replace_val(ptr, map),
Instruction::Memcpy { dest, src, .. } => {
replace_val(dest, map);
replace_val(src, map);
}
// Arithmetic / logic.
Instruction::BinOp { lhs, rhs, .. } => {
replace_op(lhs, map);
replace_op(rhs, map);
}
Instruction::UnaryOp { src, .. } => replace_op(src, map),
Instruction::Cmp { lhs, rhs, .. } => {
replace_op(lhs, map);
replace_op(rhs, map);
}
// Pointer / address.
Instruction::GetElementPtr { base, offset, .. } => {
replace_val(base, map);
replace_op(offset, map);
}
Instruction::DynAlloca { size, .. } => replace_op(size, map),
Instruction::StackRestore { ptr } => replace_val(ptr, map),
// Conversions.
Instruction::Cast { src, .. } => replace_op(src, map),
Instruction::Copy { src, .. } => replace_op(src, map),
// Calls.
Instruction::Call { info, .. } => {
for arg in &mut info.args {
replace_op(arg, map);
}
}
Instruction::CallIndirect { func_ptr, info } => {
replace_op(func_ptr, map);
for arg in &mut info.args {
replace_op(arg, map);
}
}
// Phi.
Instruction::Phi { incoming, .. } => {
for (op, _) in incoming {
replace_op(op, map);
}
}
// Select.
Instruction::Select { cond, true_val, false_val, .. } => {
replace_op(cond, map);
replace_op(true_val, map);
replace_op(false_val, map);
}
// Atomics.
Instruction::AtomicRmw { ptr, val, .. } => {
replace_op(ptr, map);
replace_op(val, map);
}
Instruction::AtomicCmpxchg { ptr, expected, desired, .. } => {
replace_op(ptr, map);
replace_op(expected, map);
replace_op(desired, map);
}
Instruction::AtomicLoad { ptr, .. } => replace_op(ptr, map),
Instruction::AtomicStore { ptr, val, .. } => {
replace_op(ptr, map);
replace_op(val, map);
}
// Varargs.
Instruction::VaArg { va_list_ptr, .. } => replace_val(va_list_ptr, map),
Instruction::VaArgStruct { dest_ptr, va_list_ptr, .. } => {
replace_val(dest_ptr, map);
replace_val(va_list_ptr, map);
}
Instruction::VaStart { va_list_ptr } => replace_val(va_list_ptr, map),
Instruction::VaEnd { va_list_ptr } => replace_val(va_list_ptr, map),
Instruction::VaCopy { dest_ptr, src_ptr } => {
replace_val(dest_ptr, map);
replace_val(src_ptr, map);
}
// Inline assembly.
Instruction::InlineAsm { inputs, .. } => {
for (_, op, _) in inputs {
replace_op(op, map);
}
}
// Intrinsics.
Instruction::Intrinsic { args, .. } => {
for arg in args {
replace_op(arg, map);
}
}
// Complex-return helpers.
Instruction::SetReturnF64Second { src } => replace_op(src, map),
Instruction::SetReturnF32Second { src } => replace_op(src, map),
Instruction::SetReturnF128Second { src } => replace_op(src, map),
}
}
fn replace_values_in_terminator(term: &mut Terminator, map: &FxHashMap<u32, u32>) {
match term {
Terminator::Return(Some(op)) => replace_op(op, map),
Terminator::CondBranch { cond, .. } => replace_op(cond, map),
Terminator::IndirectBranch { target, .. } => replace_op(target, map),
Terminator::Switch { val, .. } => replace_op(val, map),
Terminator::Return(None) | Terminator::Branch(_) | Terminator::Unreachable => {}
}
}
// ── Tests ─────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
use crate::common::types::{AddressSpace, IrType};
use crate::ir::reexports::{BasicBlock, BlockId, IrConst, Value};
/// Build a simple counting loop:
/// preheader → header → body → latch → (back to header) / exit
///
/// ```
/// preheader (B0):
/// %0 = Copy 0i32
/// Branch B1
///
/// header (B1):
/// %1 = Phi [(%0, B0), (%5, B3)] // i
/// %3 = Cmp Slt %1, const(n_val) // limit is a compile-time constant
/// CondBranch %3, B2(body), B4(exit)
///
/// body (B2):
/// %4 = GEP(arr, %1)
/// Store(0, %4)
/// Branch B3
///
/// latch (B3):
/// %5 = Add %1, 1
/// Branch B1
///
/// exit (B4):
/// Return void
/// ```
///
/// The limit is a constant so it is loop-invariant (not defined in loop.body).
fn make_counting_loop(n_val: i32) -> IrFunction {
let mut func =
IrFunction::new("loop_test".to_string(), IrType::Void, vec![], false);
// B0: preheader — init i = 0
func.blocks.push(BasicBlock {
label: BlockId(0),
instructions: vec![Instruction::Copy {
dest: Value(0),
src: Operand::Const(IrConst::I32(0)),
}],
terminator: Terminator::Branch(BlockId(1)),
source_spans: Vec::new(),
});
// B1: header — %1 = phi(0, %5); %3 = cmp %1 < const(n_val)
// Limit is a constant → loop-invariant → eligible for unrolling.
func.blocks.push(BasicBlock {
label: BlockId(1),