midenc-hir 0.8.0

High-level Intermediate Representation for Miden Assembly
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
use alloc::{rc::Rc, vec::Vec};
use core::{fmt, ptr::NonNull, sync::atomic::AtomicBool};

use smallvec::SmallVec;

use super::{entity::EntityParent, *};
use crate::{Context, interner};

/// A pointer to a [Block]
pub type BlockRef = UnsafeIntrusiveEntityRef<Block>;
/// An intrusive, doubly-linked list of [Block]
pub type BlockList = EntityList<Block>;
/// A cursor into a [BlockList]
pub type BlockCursor<'a> = EntityListCursor<'a, Block>;
/// A mutable cursor into a [BlockList]
pub type BlockCursorMut<'a> = EntityListCursorMut<'a, Block>;
/// An iterator over blocks produced by a depth-first, pre-order visit of the CFG
pub type PreOrderBlockIter = cfg::PreOrderIter<BlockRef>;
/// An iterator over blocks produced by a depth-first, post-order visit of the CFG
pub type PostOrderBlockIter = cfg::PostOrderIter<BlockRef>;

/// The unique identifier for a [Block]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct BlockId(u32);

impl BlockId {
    const USER_DEFINED_TAG: u32 = 1u32 << 31;

    /// Create a [BlockId] from a [Symbol](interner::Symbol) representing a user-defined name.
    ///
    /// This is used when parsing IR, so that we can preserve the user-provided names.
    pub const fn from_symbol(sym: interner::Symbol) -> Self {
        assert!(
            sym.as_u32() & Self::USER_DEFINED_TAG == 0,
            "cannot convert symbol id to block id: out of range"
        );
        Self(sym.as_u32())
    }

    pub const fn is_user_defined(self) -> bool {
        self.0 & Self::USER_DEFINED_TAG == Self::USER_DEFINED_TAG
    }

    pub const fn from_u32(id: u32) -> Self {
        assert!(
            id & Self::USER_DEFINED_TAG == 0,
            "invalid block id: value must be less than 2^31"
        );
        Self(id)
    }

    pub const fn as_u32(&self) -> u32 {
        self.0 & !Self::USER_DEFINED_TAG
    }
}

impl EntityId for BlockId {
    #[inline(always)]
    fn as_usize(&self) -> usize {
        self.0 as usize
    }
}

impl fmt::Debug for BlockId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            f.debug_struct("BlockId")
                .field("is_user_defined", &self.is_user_defined())
                .field("id", &self.as_u32())
                .finish()
        } else {
            fmt::Display::fmt(self, f)
        }
    }
}

impl fmt::Display for BlockId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_user_defined() {
            write!(f, "^{}", unsafe {
                core::mem::transmute::<u32, interner::Symbol>(self.as_u32())
            })
        } else {
            write!(f, "^block{}", self.as_u32())
        }
    }
}

/// Represents a basic block in the IR.
///
/// Basic blocks are used in SSA regions to provide the structure of the control-flow graph.
/// Operations within a basic block appear in the order they will be executed.
///
/// A block must have a [traits::Terminator], an operation which transfers control to another block
/// in the same region, or out of the containing operation (e.g. returning from a function).
///
/// Blocks have _predecessors_ and _successors_, representing the inbound and outbound edges
/// (respectively) formed by operations that transfer control between blocks. A block can have
/// zero or more predecessors and/or successors. A well-formed region will generally only have a
/// single block (the entry block) with no predecessors (i.e. no unreachable blocks), and no blocks
/// with both multiple predecessors _and_ multiple successors (i.e. no critical edges). It is valid
/// to have both unreachable blocks and critical edges in the IR, but they must be removed during
/// the course of compilation.
pub struct Block {
    /// The [Context] in which this [Block] was allocated.
    context: NonNull<Context>,
    /// The unique id of this block
    id: BlockId,
    /// Flag that indicates whether the ops in this block have a valid ordering
    valid_op_ordering: AtomicBool,
    /// The set of uses of this block
    uses: BlockOperandList,
    /// The list of [Operation]s that comprise this block
    body: OpList,
    /// The parameter list for this block
    arguments: Vec<BlockArgumentRef>,
}

impl Eq for Block {}
impl PartialEq for Block {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}
impl core::hash::Hash for Block {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

impl fmt::Debug for Block {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Block")
            .field("id", &self.id)
            .field_with("region", |f| match self.parent() {
                None => f.write_str("None"),
                Some(r) => write!(f, "Some({r:p})"),
            })
            .field_with("arguments", |f| {
                let mut list = f.debug_list();
                for arg in self.arguments.iter() {
                    list.entry_with(|f| f.write_fmt(format_args!("{}", &arg.borrow())));
                }
                list.finish()
            })
            .finish_non_exhaustive()
    }
}

impl fmt::Display for Block {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.id)
    }
}

impl crate::formatter::PrettyPrint for Block {
    fn render(&self) -> crate::formatter::Document {
        let flags = OpPrintingFlags::default();

        let mut printer = crate::print::AsmPrinter::new(self.context_rc(), &flags);
        printer.print_block(self);
        printer.finish()
    }
}

impl Spanned for Block {
    fn span(&self) -> SourceSpan {
        self.body.front().get().map(|op| op.span()).unwrap_or_default()
    }
}

impl Entity for Block {}

impl EntityWithId for Block {
    type Id = BlockId;

    fn id(&self) -> Self::Id {
        self.id
    }
}

impl EntityWithParent for Block {
    type Parent = Region;
}

impl EntityListItem for Block {}

impl EntityParent<Operation> for Block {
    fn offset() -> usize {
        core::mem::offset_of!(Block, body)
    }
}

impl EntityParent<BlockOperand> for Block {
    fn offset() -> usize {
        core::mem::offset_of!(Block, uses)
    }
}

impl Usable for Block {
    type Use = BlockOperand;

    #[inline(always)]
    fn uses(&self) -> &BlockOperandList {
        &self.uses
    }

    #[inline(always)]
    fn uses_mut(&mut self) -> &mut BlockOperandList {
        &mut self.uses
    }
}

impl cfg::Graph for Block {
    type ChildEdgeIter = BlockSuccessorEdgesIter;
    type ChildIter = BlockSuccessorIter;
    type Edge = BlockOperandRef;
    type Node = BlockRef;

    fn size(&self) -> usize {
        if let Some(term) = self.terminator() {
            term.borrow().num_successors()
        } else {
            0
        }
    }

    fn entry_node(&self) -> Self::Node {
        self.as_block_ref()
    }

    fn children(parent: Self::Node) -> Self::ChildIter {
        BlockSuccessorIter::new(parent)
    }

    fn children_edges(parent: Self::Node) -> Self::ChildEdgeIter {
        BlockSuccessorEdgesIter::new(parent)
    }

    fn edge_dest(edge: Self::Edge) -> Self::Node {
        edge.borrow().successor()
    }
}

impl<'a> cfg::InvertibleGraph for &'a Block {
    type Inverse = cfg::Inverse<&'a Block>;
    type InvertibleChildEdgeIter = BlockPredecessorEdgesIter;
    type InvertibleChildIter = BlockPredecessorIter;

    fn inverse(self) -> Self::Inverse {
        cfg::Inverse::new(self)
    }

    fn inverse_children(parent: Self::Node) -> Self::InvertibleChildIter {
        BlockPredecessorIter::new(parent)
    }

    fn inverse_children_edges(parent: Self::Node) -> Self::InvertibleChildEdgeIter {
        BlockPredecessorEdgesIter::new(parent)
    }
}

impl cfg::Graph for BlockRef {
    type ChildEdgeIter = BlockSuccessorEdgesIter;
    type ChildIter = BlockSuccessorIter;
    type Edge = BlockOperandRef;
    type Node = BlockRef;

    fn size(&self) -> usize {
        if let Some(term) = self.borrow().terminator() {
            term.borrow().num_successors()
        } else {
            0
        }
    }

    fn entry_node(&self) -> Self::Node {
        *self
    }

    fn children(parent: Self::Node) -> Self::ChildIter {
        BlockSuccessorIter::new(parent)
    }

    fn children_edges(parent: Self::Node) -> Self::ChildEdgeIter {
        BlockSuccessorEdgesIter::new(parent)
    }

    fn edge_dest(edge: Self::Edge) -> Self::Node {
        edge.borrow().successor()
    }
}

impl cfg::InvertibleGraph for BlockRef {
    type Inverse = cfg::Inverse<Self>;
    type InvertibleChildEdgeIter = BlockPredecessorEdgesIter;
    type InvertibleChildIter = BlockPredecessorIter;

    fn inverse(self) -> Self::Inverse {
        cfg::Inverse::new(self)
    }

    fn inverse_children(parent: Self::Node) -> Self::InvertibleChildIter {
        BlockPredecessorIter::new(parent)
    }

    fn inverse_children_edges(parent: Self::Node) -> Self::InvertibleChildEdgeIter {
        BlockPredecessorEdgesIter::new(parent)
    }
}

#[doc(hidden)]
pub struct BlockSuccessorIter {
    iter: BlockSuccessorEdgesIter,
}
impl BlockSuccessorIter {
    pub fn new(parent: BlockRef) -> Self {
        Self {
            iter: BlockSuccessorEdgesIter::new(parent),
        }
    }
}
impl ExactSizeIterator for BlockSuccessorIter {
    #[inline]
    fn len(&self) -> usize {
        self.iter.len()
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.iter.is_empty()
    }
}
impl Iterator for BlockSuccessorIter {
    type Item = BlockRef;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|bo| bo.borrow().successor())
    }

    #[inline]
    fn collect<B: FromIterator<Self::Item>>(self) -> B
    where
        Self: Sized,
    {
        let Some(terminator) = self.iter.terminator.as_ref() else {
            return B::from_iter([]);
        };
        let terminator = terminator.borrow();
        let successors = terminator.successors();
        B::from_iter(
            successors.all().as_slice()[self.iter.index..self.iter.num_successors]
                .iter()
                .map(|succ| succ.block.borrow().successor()),
        )
    }

    fn collect_into<E: Extend<Self::Item>>(self, collection: &mut E) -> &mut E
    where
        Self: Sized,
    {
        let Some(terminator) = self.iter.terminator.as_ref() else {
            return collection;
        };
        let terminator = terminator.borrow();
        let successors = terminator.successors();
        collection.extend(
            successors.all().as_slice()[self.iter.index..self.iter.num_successors]
                .iter()
                .map(|succ| succ.block.borrow().successor()),
        );
        collection
    }
}

#[doc(hidden)]
pub struct BlockSuccessorEdgesIter {
    terminator: Option<OperationRef>,
    num_successors: usize,
    index: usize,
}
impl BlockSuccessorEdgesIter {
    pub fn new(parent: BlockRef) -> Self {
        let terminator = parent.borrow().terminator();
        let num_successors = terminator.as_ref().map(|t| t.borrow().num_successors()).unwrap_or(0);
        Self {
            terminator,
            num_successors,
            index: 0,
        }
    }
}
impl ExactSizeIterator for BlockSuccessorEdgesIter {
    #[inline]
    fn len(&self) -> usize {
        self.num_successors.saturating_sub(self.index)
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.index >= self.num_successors
    }
}
impl Iterator for BlockSuccessorEdgesIter {
    type Item = BlockOperandRef;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.num_successors {
            return None;
        }

        // SAFETY: We'll never have a none terminator if we have non-zero number of successors
        let terminator = unsafe { self.terminator.as_ref().unwrap_unchecked() };
        let index = self.index;
        self.index += 1;
        Some(terminator.borrow().successor(index).dest)
    }

    fn collect<B: FromIterator<Self::Item>>(self) -> B
    where
        Self: Sized,
    {
        let Some(terminator) = self.terminator.as_ref() else {
            return B::from_iter([]);
        };
        let terminator = terminator.borrow();
        let successors = terminator.successors();
        B::from_iter(
            successors.all().as_slice()[self.index..self.num_successors]
                .iter()
                .map(|succ| succ.block),
        )
    }

    fn collect_into<E: Extend<Self::Item>>(self, collection: &mut E) -> &mut E
    where
        Self: Sized,
    {
        let Some(terminator) = self.terminator.as_ref() else {
            return collection;
        };
        let terminator = terminator.borrow();
        let successors = terminator.successors();
        collection.extend(
            successors.all().as_slice()[self.index..self.num_successors]
                .iter()
                .map(|succ| succ.block),
        );
        collection
    }
}

#[doc(hidden)]
pub struct BlockPredecessorIter {
    preds: SmallVec<[BlockRef; 4]>,
    index: usize,
}
impl BlockPredecessorIter {
    pub fn new(child: BlockRef) -> Self {
        let preds = child.borrow().predecessors().map(|bo| bo.predecessor()).collect();
        Self { preds, index: 0 }
    }

    #[inline(always)]
    pub fn into_inner(self) -> SmallVec<[BlockRef; 4]> {
        self.preds
    }
}
impl ExactSizeIterator for BlockPredecessorIter {
    #[inline]
    fn len(&self) -> usize {
        self.preds.len().saturating_sub(self.index)
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.index >= self.preds.len()
    }
}
impl Iterator for BlockPredecessorIter {
    type Item = BlockRef;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.is_empty() {
            return None;
        }
        let index = self.index;
        self.index += 1;
        Some(self.preds[index])
    }

    fn collect<B: FromIterator<Self::Item>>(self) -> B
    where
        Self: Sized,
    {
        B::from_iter(self.preds)
    }

    fn collect_into<E: Extend<Self::Item>>(self, collection: &mut E) -> &mut E
    where
        Self: Sized,
    {
        collection.extend(self.preds);
        collection
    }
}

#[doc(hidden)]
pub struct BlockPredecessorEdgesIter {
    preds: SmallVec<[BlockOperandRef; 4]>,
    index: usize,
}
impl BlockPredecessorEdgesIter {
    pub fn new(child: BlockRef) -> Self {
        let preds = child
            .borrow()
            .predecessors()
            .map(|bo| unsafe { BlockOperandRef::from_raw(&*bo) })
            .collect();
        Self { preds, index: 0 }
    }
}
impl ExactSizeIterator for BlockPredecessorEdgesIter {
    #[inline]
    fn len(&self) -> usize {
        self.preds.len().saturating_sub(self.index)
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.index >= self.preds.len()
    }
}
impl Iterator for BlockPredecessorEdgesIter {
    type Item = BlockOperandRef;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.is_empty() {
            return None;
        }
        let index = self.index;
        self.index += 1;
        Some(self.preds[index])
    }

    fn collect<B: FromIterator<Self::Item>>(self) -> B
    where
        Self: Sized,
    {
        B::from_iter(self.preds)
    }

    fn collect_into<E: Extend<Self::Item>>(self, collection: &mut E) -> &mut E
    where
        Self: Sized,
    {
        collection.extend(self.preds);
        collection
    }
}

impl Block {
    pub fn new(context: Rc<Context>, id: BlockId) -> Self {
        Self {
            context: unsafe { NonNull::new_unchecked(Rc::as_ptr(&context).cast_mut()) },
            id,
            valid_op_ordering: AtomicBool::new(true),
            uses: Default::default(),
            body: Default::default(),
            arguments: Default::default(),
        }
    }

    /// Get a borrowed reference to the owning [Context] of this block
    #[inline(always)]
    pub fn context(&self) -> &Context {
        // SAFETY: This is safe so long as this block is allocated in a Context, since the
        // Context by definition outlives the allocation.
        unsafe { self.context.as_ref() }
    }

    /// Get an owned reference to the owning [Context] of this block
    pub fn context_rc(&self) -> Rc<Context> {
        // SAFETY: This is safe so long as this block is allocated in a Context, since the
        // Context by definition outlives the allocation.
        //
        // Additionally, constructing the Rc from a raw pointer is safe here, as the pointer was
        // obtained using `Rc::as_ptr`, so the only requirement to call `Rc::from_raw` is to
        // increment the strong count, as `as_ptr` does not preserve the count for the reference
        // held by this operation. Incrementing the count first is required to manufacture new
        // clones of the `Rc` safely.
        unsafe {
            let ptr = self.context.as_ptr().cast_const();
            Rc::increment_strong_count(ptr);
            Rc::from_raw(ptr)
        }
    }

    #[inline]
    pub fn as_block_ref(&self) -> BlockRef {
        unsafe { BlockRef::from_raw(self) }
    }

    /// Get a handle to the containing [Region] of this block, if it is attached to one
    pub fn parent(&self) -> Option<RegionRef> {
        self.as_block_ref().parent()
    }

    /// Get a handle to the containing [Operation] of this block, if it is attached to one
    pub fn parent_op(&self) -> Option<OperationRef> {
        self.parent().and_then(|region| region.parent())
    }

    /// Get a handle to the ancestor [Block] of this block, if one is present
    pub fn parent_block(&self) -> Option<BlockRef> {
        self.parent_op().and_then(|op| op.parent())
    }

    /// Returns true if this block is the entry block for its containing region
    pub fn is_entry_block(&self) -> bool {
        if let Some(parent) = self.parent().map(|r| r.borrow()) {
            parent.entry_block_ref().is_some_and(|entry| entry == self.as_block_ref())
        } else {
            false
        }
    }

    /// Get the first operation in the body of this block
    #[inline]
    pub fn front(&self) -> Option<OperationRef> {
        self.body.front().as_pointer()
    }

    /// Get the last operation in the body of this block
    #[inline]
    pub fn back(&self) -> Option<OperationRef> {
        self.body.back().as_pointer()
    }

    /// Get the list of [Operation] comprising the body of this block
    #[inline(always)]
    pub fn body(&self) -> &OpList {
        &self.body
    }

    /// Get a mutable reference to the list of [Operation] comprising the body of this block
    #[inline(always)]
    pub fn body_mut(&mut self) -> &mut OpList {
        &mut self.body
    }
}

/// Arguments
impl Block {
    #[inline]
    pub fn has_arguments(&self) -> bool {
        !self.arguments.is_empty()
    }

    #[inline]
    pub fn num_arguments(&self) -> usize {
        self.arguments.len()
    }

    #[inline(always)]
    pub fn arguments(&self) -> &[BlockArgumentRef] {
        self.arguments.as_slice()
    }

    #[inline(always)]
    pub fn arguments_mut(&mut self) -> &mut Vec<BlockArgumentRef> {
        &mut self.arguments
    }

    pub fn argument_values(&self) -> impl ExactSizeIterator<Item = ValueRef> + '_ {
        self.arguments.iter().copied().map(|arg| arg as ValueRef)
    }

    pub fn argument_types(&self) -> impl ExactSizeIterator<Item = Type> + '_ {
        self.arguments.iter().copied().map(|arg| arg.borrow().ty().clone())
    }

    #[inline]
    pub fn get_argument(&self, index: usize) -> BlockArgumentRef {
        self.arguments[index]
    }

    /// Erase the block argument at `index`
    ///
    /// Panics if the argument still has uses.
    pub fn erase_argument(&mut self, index: usize) {
        assert!(
            !self.arguments[index].borrow().is_used(),
            "cannot erase block arguments with uses"
        );
        self.arguments.remove(index);
    }

    /// Erase every parameter of this block for which `should_erase` returns true.
    ///
    /// Panics if any argument to be erased still has uses.
    pub fn erase_arguments<F>(&mut self, should_erase: F)
    where
        F: Fn(&BlockArgument) -> bool,
    {
        self.arguments.retain(|arg| {
            let arg = arg.borrow();
            let keep = !should_erase(&arg);
            assert!(keep || !arg.is_used(), "cannot erase block arguments with uses");
            keep
        });
    }

    pub fn erase(&mut self) {
        if let Some(mut region) = self.parent() {
            let mut region = region.borrow_mut();
            let body = region.body_mut();
            let mut cursor = unsafe { body.cursor_mut_from_ptr(self.as_block_ref()) };
            cursor.remove();
        }
    }
}

/// Placement
impl Block {
    /// Insert this block after `after` in its containing region.
    ///
    /// Panics if this block is already attached to a region, or if `after` is not attached.
    #[track_caller]
    pub fn insert_after(&mut self, after: BlockRef) {
        assert!(
            self.parent().is_none(),
            "cannot insert block that is already attached to another region"
        );
        let mut region = after.parent().expect("'after' block is not attached to a region");
        {
            let mut region = region.borrow_mut();
            let region_body = region.body_mut();
            let mut cursor = unsafe { region_body.cursor_mut_from_ptr(after) };
            cursor.insert_after(self.as_block_ref());
        }
    }

    /// Insert this block before `before` in its containing region.
    ///
    /// Panics if this block is already attached to a region, or if `before` is not attached.
    #[track_caller]
    pub fn insert_before(&mut self, before: BlockRef) {
        assert!(
            self.parent().is_none(),
            "cannot insert block that is already attached to another region"
        );
        let mut region = before.parent().expect("'before' block is not attached to a region");
        {
            let mut region = region.borrow_mut();
            let region_body = region.body_mut();
            let mut cursor = unsafe { region_body.cursor_mut_from_ptr(before) };
            cursor.insert_before(self.as_block_ref());
        }
    }

    /// Insert this block at the end of `region`.
    ///
    /// Panics if this block is already attached to a region.
    #[track_caller]
    pub fn insert_at_end(&mut self, mut region: RegionRef) {
        assert!(
            self.parent().is_none(),
            "cannot insert block that is already attached to another region"
        );
        region.borrow_mut().body_mut().push_back(self.as_block_ref());
    }

    /// Unlink this block from its current region and insert it right before `before`
    #[track_caller]
    pub fn move_before(&mut self, before: BlockRef) {
        self.unlink();
        self.insert_before(before);
    }

    /// Remove this block from its containing region
    fn unlink(&mut self) {
        if let Some(mut region) = self.parent() {
            let mut region = region.borrow_mut();
            unsafe {
                let mut cursor = region.body_mut().cursor_mut_from_ptr(self.as_block_ref());
                cursor.remove();
            }
        }
    }

    /// Splice the body of `block` to the end of `self`, updating the parent of all spliced ops.
    ///
    /// It is up to the caller to ensure that this operation produces valid IR.
    pub fn splice_block(&mut self, block: &mut Self) {
        let ops = block.body_mut().take();
        self.body.back_mut().splice_after(ops);
    }

    /// Splice the body of `block` to `self` before `ip`, updating the parent of all spliced ops.
    ///
    /// It is up to the caller to ensure that this operation produces valid IR.
    pub fn splice_block_before(&mut self, block: &mut Self, ip: OperationRef) {
        assert_eq!(ip.parent().unwrap(), block.as_block_ref());

        let ops = block.body_mut().take();
        let mut cursor = unsafe { self.body.cursor_mut_from_ptr(ip) };
        cursor.splice_before(ops);
    }

    /// Splice the body of `block` to `self` after `ip`, updating the parent of all spliced ops.
    ///
    /// It is up to the caller to ensure that this operation produces valid IR.
    pub fn splice_block_after(&mut self, block: &mut Self, ip: OperationRef) {
        assert_eq!(ip.parent().unwrap(), block.as_block_ref());

        let ops = block.body_mut().take();
        let mut cursor = unsafe { self.body.cursor_mut_from_ptr(ip) };
        cursor.splice_after(ops);
    }

    /// Split this block into two blocks before the specified operation
    ///
    /// Note that all operations in the block prior to `before` stay as part of the original block,
    /// and the rest are moved to the new block, including the old terminator. The original block is
    /// thus left without a terminator.
    ///
    /// Returns the newly created block.
    pub fn split_block(&mut self, before: OperationRef) -> BlockRef {
        let this = self.as_block_ref();
        assert!(
            BlockRef::ptr_eq(
                &this,
                &before.parent().expect("'before' op is not attached to a block")
            ),
            "cannot split block using an operation that does not belong to the block being split"
        );

        // We need the parent op so we can get access to the current Context, but this also tells us
        // that this block is attached to a region and operation.
        let mut region = self.parent().expect("block is not attached to a region");
        let parent = region.parent().expect("parent region is not attached to an operation");
        // Create a new empty block
        let mut new_block = parent.borrow().context_rc().create_block();
        // Insert the block in the same region as `self`, immediately after `self`
        {
            let mut region_mut = region.borrow_mut();
            let blocks = region_mut.body_mut();
            let mut cursor = unsafe { blocks.cursor_mut_from_ptr(this) };
            cursor.insert_after(new_block);
        }
        // Split the body of `self` at `before`, and splice everything after `before`, including
        // `before` itself, into the new block we created.
        let ops = {
            let mut cursor = unsafe { self.body.cursor_mut_from_ptr(before) };
            // Move the cursor before 'before' so that the split we get contains it
            cursor.move_prev();
            cursor.split_after()
        };
        new_block.borrow_mut().body_mut().back_mut().splice_after(ops);
        new_block
    }

    pub fn clear(&mut self) {
        // Drop all references from within this block
        self.drop_all_references();

        // Drop all operations within this block
        self.body_mut().clear();
    }
}

/// Ancestors
impl Block {
    pub fn is_legal_to_hoist_into(&self) -> bool {
        use crate::traits::ReturnLike;

        // No terminator means the block is under construction, and thus legal to hoist into
        let Some(terminator) = self.terminator() else {
            return true;
        };

        // If the block has no successors, it can never be legal to hoist into it, there is nothing
        // to hoist!
        if self.num_successors() == 0 {
            return false;
        }

        // Instructions should not be hoisted across effectful or return-like terminators. This is
        // typically only exception handling intrinsics, which HIR doesn't really have, but which
        // we may nevertheless want to represent in the future.
        //
        // NOTE: Most return-like terminators would have no successors, but in LLVM, for example,
        // there are instructions like `catch_ret`, which semantically are return-like, but which
        // have a successor block (the landing pad).
        let terminator = terminator.borrow();
        !terminator.is_memory_effect_free() || terminator.implements::<dyn ReturnLike>()
    }

    pub fn has_ssa_dominance(&self) -> bool {
        self.parent_op()
            .and_then(|op| {
                op.borrow()
                    .as_trait::<dyn RegionKindInterface>()
                    .map(|rki| rki.has_ssa_dominance())
            })
            .unwrap_or(true)
    }

    /// Walk up the ancestor blocks of `block`, until `f` returns `true` for a block.
    ///
    /// NOTE: `block` is visited before any of its ancestors.
    pub fn traverse_ancestors<F>(block: BlockRef, mut f: F) -> Option<BlockRef>
    where
        F: FnMut(BlockRef) -> bool,
    {
        let mut block = Some(block);
        while let Some(current) = block.take() {
            if f(current) {
                return Some(current);
            }
            block = current.borrow().parent_block();
        }

        None
    }

    /// Try to get a pair of blocks, starting with the given pair, which live in the same region,
    /// by exploring the relationships of both blocks with respect to their regions.
    ///
    /// The returned block pair will either be the same input blocks, or some combination of those
    /// blocks or their ancestors.
    pub fn get_blocks_in_same_region(a: BlockRef, b: BlockRef) -> Option<(BlockRef, BlockRef)> {
        // If both blocks do not live in the same region, we will have to check their parent
        // operations.
        let a_region = a.parent().unwrap();
        let b_region = b.parent().unwrap();
        if a_region == b_region {
            return Some((a, b));
        }

        // Iterate over all ancestors of `a`, counting the depth of `a`.
        //
        // If one of `a`'s ancestors are in the same region as `b`, then we stop early because we
        // found our nearest common ancestor.
        let mut a_depth = 0;
        let result = Self::traverse_ancestors(a, |block| {
            a_depth += 1;
            block.parent().is_some_and(|r| r == b_region)
        });
        if let Some(a) = result {
            return Some((a, b));
        }

        // Iterate over all ancestors of `b`, counting the depth of `b`.
        //
        // If one of `b`'s ancestors are in the same region as `a`, then we stop early because we
        // found our nearest common ancestor.
        let mut b_depth = 0;
        let result = Self::traverse_ancestors(b, |block| {
            b_depth += 1;
            block.parent().is_some_and(|r| r == a_region)
        });
        if let Some(b) = result {
            return Some((a, b));
        }

        // Otherwise, we found two blocks that are siblings at some level. Walk the deepest one
        // up until we reach the top or find a nearest common ancestor.
        let mut a = Some(a);
        let mut b = Some(b);
        loop {
            use core::cmp::Ordering;

            match a_depth.cmp(&b_depth) {
                Ordering::Greater => {
                    a = a.and_then(|a| a.grandparent().and_then(|gp| gp.parent()));
                    a_depth -= 1;
                }
                Ordering::Less => {
                    b = b.and_then(|b| b.grandparent().and_then(|gp| gp.parent()));
                    b_depth -= 1;
                }
                Ordering::Equal => break,
            }
        }

        // If we found something with the same level, then we can march both up at the same time
        // from here on out.
        while let Some(next_a) = a.take() {
            // If they are at the same level, and have the same parent region, then we succeeded.
            let next_a_parent = next_a.parent();
            let b_parent = b.as_ref().and_then(|b| b.parent());
            if next_a_parent == b_parent {
                return Some((next_a, b.unwrap()));
            }

            a = next_a_parent.and_then(|r| r.grandparent());
            b = b_parent.and_then(|r| r.grandparent());
        }

        // They don't share a nearest common ancestor, perhaps they are in different modules or
        // something.
        None
    }
}

/// Predecessors and Successors
impl Block {
    /// Returns true if this block has predecessors
    #[inline(always)]
    pub fn has_predecessors(&self) -> bool {
        self.is_used()
    }

    /// Get an iterator over the predecessors of this block
    #[inline(always)]
    pub fn predecessors(&self) -> BlockOperandIter<'_> {
        self.iter_uses()
    }

    /// If this block has exactly one predecessor, return it, otherwise `None`
    ///
    /// NOTE: A predecessor block with multiple edges, e.g. a conditional branch that has this block
    /// as the destination for both true/false branches is _not_ considered a single predecessor by
    /// this function.
    pub fn get_single_predecessor(&self) -> Option<BlockRef> {
        let front = self.uses.front().as_pointer()?;
        let back = self.uses.back().as_pointer().unwrap();
        if BlockOperandRef::ptr_eq(&front, &back) {
            Some(front.borrow().predecessor())
        } else {
            None
        }
    }

    /// If this block has a unique predecessor, i.e. all incoming edges originate from one block,
    /// return it, otherwise `None`
    pub fn get_unique_predecessor(&self) -> Option<BlockRef> {
        let mut front = self.uses.front();
        let block_operand = front.get()?;
        let block = block_operand.predecessor();
        loop {
            front.move_next();
            if let Some(bo) = front.get() {
                if !BlockRef::ptr_eq(&block, &bo.predecessor()) {
                    break None;
                }
            } else {
                break Some(block);
            }
        }
    }

    /// Returns true if this block has any successors
    #[inline]
    pub fn has_successors(&self) -> bool {
        self.num_successors() > 0
    }

    /// Get the number of successors of this block in the CFG
    pub fn num_successors(&self) -> usize {
        self.terminator().map(|op| op.borrow().num_successors()).unwrap_or(0)
    }

    /// Get the `index`th successor of this block's terminator operation
    #[track_caller]
    pub fn get_successor(&self, index: usize) -> BlockRef {
        let op = self.terminator().expect("this block has no terminator");
        op.borrow().successor(index).dest.borrow().successor()
    }

    /// This drops all operand uses from operations within this block, which is an essential step in
    /// breaking cyclic dependences between references when they are to be deleted.
    pub fn drop_all_references(&mut self) {
        let mut cursor = self.body.front_mut();
        while let Some(mut op) = cursor.as_pointer() {
            op.borrow_mut().drop_all_references();
            cursor.move_next();
        }
    }

    /// This drops all uses of values defined in this block or in the blocks of nested regions
    /// wherever the uses are located.
    pub fn drop_all_defined_value_uses(&mut self) {
        let mut cursor = self.body.back_mut();
        while let Some(mut op) = cursor.as_pointer() {
            op.borrow_mut().drop_all_defined_value_uses();
            cursor.move_prev();
        }
        for arg in self.arguments.iter_mut() {
            let mut arg = arg.borrow_mut();
            arg.uses_mut().clear();
        }
        self.drop_all_uses();
    }

    /// Drop all uses of this block via [BlockOperand]
    #[inline]
    pub fn drop_all_uses(&mut self) {
        self.uses_mut().clear();
    }

    pub fn replace_all_uses_with(&mut self, mut replacement: BlockRef) {
        if BlockRef::ptr_eq(&self.as_block_ref(), &replacement) {
            return;
        }

        let mut replacement_block = replacement.borrow_mut();

        let uses = self.uses_mut().take();
        replacement_block.uses_mut().back_mut().splice_after(uses);
    }

    #[inline(always)]
    pub(super) fn is_op_order_valid(&self) -> bool {
        use core::sync::atomic::Ordering;

        self.valid_op_ordering.load(Ordering::Acquire)
    }

    #[inline(always)]
    pub(super) fn mark_op_order_valid(&self) {
        use core::sync::atomic::Ordering;

        self.valid_op_ordering.store(true, Ordering::Release);
    }

    pub(super) fn invalidate_op_order(&mut self) {
        use core::sync::atomic::Ordering;

        // Validate the current ordering
        assert!(self.verify_op_order());
        self.valid_op_ordering.store(false, Ordering::Release);
    }

    /// Returns true if the current operation ordering in this block is valid
    pub(super) fn verify_op_order(&self) -> bool {
        // The order is already known to be invalid
        if !self.is_op_order_valid() {
            return false;
        }

        // The order is valid if there are less than 2 operations
        if self.body.is_empty()
            || OperationRef::ptr_eq(
                &self.body.front().as_pointer().unwrap(),
                &self.body.back().as_pointer().unwrap(),
            )
        {
            return true;
        }

        let mut cursor = self.body.front();
        let mut prev = None;
        while let Some(op) = cursor.as_pointer() {
            cursor.move_next();
            if prev.is_none() {
                prev = Some(op);
                continue;
            }

            // The previous operation must have a smaller order index than the next
            let prev_order = prev.take().unwrap().borrow().order();
            let current_order = op.borrow().order().unwrap_or(u32::MAX);
            if prev_order.is_some_and(|o| o >= current_order) {
                return false;
            }
            prev = Some(op);
        }

        true
    }

    /// Get the terminator operation of this block, or `None` if the block does not have one.
    pub fn terminator(&self) -> Option<OperationRef> {
        if !self.has_terminator() {
            None
        } else {
            self.body.back().as_pointer()
        }
    }

    /// Returns true if this block has a terminator
    pub fn has_terminator(&self) -> bool {
        use crate::traits::Terminator;
        !self.body.is_empty()
            && self.body.back().get().is_some_and(|op| op.implements::<dyn Terminator>())
    }
}

pub type BlockOperandRef = UnsafeIntrusiveEntityRef<BlockOperand>;
/// An intrusive, doubly-linked list of [BlockOperand]
pub type BlockOperandList = EntityList<BlockOperand>;
#[allow(unused)]
pub type BlockOperandCursor<'a> = EntityListCursor<'a, BlockOperand>;
#[allow(unused)]
pub type BlockOperandCursorMut<'a> = EntityListCursorMut<'a, BlockOperand>;
pub type BlockOperandIter<'a> = EntityListIter<'a, BlockOperand>;

/// A [BlockOperand] represents a use of a [Block] by an [Operation]
pub struct BlockOperand {
    /// The owner of this operand, i.e. the operation it is an operand of
    pub owner: OperationRef,
    /// The index of this operand in the set of block operands of the operation
    pub index: u8,
}

impl Entity for BlockOperand {}
impl EntityWithParent for BlockOperand {
    type Parent = Block;
}
impl EntityListItem for BlockOperand {
    #[track_caller]
    fn on_inserted(
        this: UnsafeIntrusiveEntityRef<Self>,
        _cursor: &mut EntityListCursorMut<'_, Self>,
    ) {
        assert!(this.parent().is_some());
    }
}

impl BlockOperand {
    #[inline]
    pub fn new(owner: OperationRef, index: u8) -> Self {
        Self { owner, index }
    }

    pub fn as_block_operand_ref(&self) -> BlockOperandRef {
        unsafe { BlockOperandRef::from_raw(self) }
    }

    pub fn block_id(&self) -> BlockId {
        self.successor().borrow().id
    }

    /// Get the block from which this block operand originates, i.e. the predecessor block
    pub fn predecessor(&self) -> BlockRef {
        self.owner.parent().expect("owning operation is not attached to a block")
    }

    /// Get the block this operand references
    #[inline]
    pub fn successor(&self) -> BlockRef {
        self.as_block_operand_ref().parent().unwrap_or_else(|| {
            panic!(
                "block operand is dead at index {} in {}",
                self.index,
                &self.owner.borrow().name()
            )
        })
    }

    /// Set the successor block to `block`, removing the block operand from the use list of the
    /// previous block, and adding it to the use list of `block`.
    ///
    /// NOTE: This requires a mutable borrow of `block` when mutating its use list.
    pub fn set(&mut self, mut block: BlockRef) {
        self.unlink();
        block.borrow_mut().insert_use(self.as_block_operand_ref());
    }
}

impl fmt::Debug for BlockOperand {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BlockOperand")
            .field("block", &self.successor())
            .field_with("owner", |f| write!(f, "{:p}", &self.owner))
            .field("index", &self.index)
            .finish()
    }
}
impl fmt::Display for BlockOperand {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", &self.block_id())
    }
}
impl StorableEntity for BlockOperand {
    #[inline(always)]
    fn index(&self) -> usize {
        self.index as usize
    }

    unsafe fn set_index(&mut self, index: usize) {
        self.index = index.try_into().expect("too many successors");
    }

    /// Remove this use of `block`
    fn unlink(&mut self) {
        let this = self.as_block_operand_ref();
        if !this.is_linked() {
            return;
        }
        let Some(mut parent) = this.parent() else {
            return;
        };

        let mut block = parent.borrow_mut();
        let uses = block.uses_mut();
        unsafe {
            let mut cursor = uses.cursor_mut_from_ptr(this);
            cursor.remove();
        }
    }
}