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
//! Abstract syntax tree type definitions.

pub use id_arena::{Arena, Id};

/// An identifier for a value defined by an assignment.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct ValueId(
    /// Always points to a `Statement::Assignment`, and references the value
    /// defined by the assignment.
    pub(crate) Id<Statement>,
);

impl From<ValueId> for Id<Statement> {
    #[inline]
    fn from(id: ValueId) -> Self {
        id.0
    }
}

/// An identifier for a defined block.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct BlockId(
    /// Always points to an assignment where the RHS is `AssignmentRhs::Block`.
    pub(crate) ValueId,
);

impl From<BlockId> for ValueId {
    #[inline]
    fn from(id: BlockId) -> Self {
        id.0
    }
}

impl From<BlockId> for Id<Statement> {
    #[inline]
    fn from(id: BlockId) -> Self {
        (id.0).0
    }
}

/// A complete optimization that replaces a left-hand side with a right-hand
/// side.
#[derive(Clone, Debug)]
pub enum Replacement {
    /// A replacement in the form of a left-hand side followed by a right-hand
    /// side.
    LhsRhs {
        /// Statements that make up the expression DAGs for the left- and
        /// right-hand sides.
        statements: Arena<Statement>,
        /// The left-hand side that is matched by the optimization.
        lhs: Infer,
        /// The right-hand side that replaces the left-hand side after applying
        /// the optimization.
        rhs: Operand,
    },

    /// A replacement in the form of an expression DAG followed by a `cand x y`
    /// statement that declares that `y` is a candidate for replacing `x`.
    Cand {
        /// Statements that make up the expression DAG for both the
        /// replacement's left- and right-hand sides.
        statements: Arena<Statement>,
        /// The candidate rewrite connecting the left- and right-hand sides of
        /// this replacement within `statements`.
        cand: Cand,
    },
}

impl Replacement {
    /// Get the assignment that defined the given value.
    ///
    /// # Panics
    ///
    /// May panic or produce incorrect results if given a `ValueId` from another
    /// `Replacement`, `LeftHandSide`, or `RightHandSide`'s arena.
    pub fn assignment(&self, id: ValueId) -> &Assignment {
        match self {
            Replacement::LhsRhs { statements, .. } | Replacement::Cand { statements, .. } => {
                match &statements[id.into()] {
                    Statement::Assignment(a) => a,
                    _ => panic!("use of an `id` that is not from this `Replacement`'s arena"),
                }
            }
        }
    }
}

/// A builder for a [`Replacement`][crate::ast::Replacement].
#[derive(Clone, Debug, Default)]
pub struct ReplacementBuilder {
    statements: Arena<Statement>,
}

impl ReplacementBuilder {
    /// Create a new assignment statement.
    ///
    /// Returns the value defined by the assignment.
    ///
    /// # Panics
    ///
    /// Panics if `name` (when given) does not start with '%'.
    pub fn assignment(
        &mut self,
        name: Option<String>,
        r#type: Option<Type>,
        value: impl Into<AssignmentRhs>,
        attributes: Vec<Attribute>,
    ) -> ValueId {
        let name = name.unwrap_or_else(|| format!("%{}", self.statements.len()));
        assert!(name.starts_with('%'));
        ValueId(
            self.statements.alloc(
                Assignment {
                    name,
                    r#type,
                    value: value.into(),
                    attributes,
                }
                .into(),
            ),
        )
    }

    /// Create a new [basic block][crate::ast::Block].
    ///
    /// Declare that the block has `predecessors` number of incoming edges in
    /// the control-flow graph.
    ///
    /// # Panics
    ///
    /// Panics if `name` (when given) does not start with '%'.
    pub fn block(&mut self, name: Option<String>, predecessors: u32) -> BlockId {
        BlockId(self.assignment(name, None, Block { predecessors }, vec![]))
    }

    /// Create a [path condition][crate::ast::Pc].
    ///
    /// Expresses the fact that `x` must equal `y` for the replacement to be
    /// valid.
    pub fn pc(&mut self, x: impl Into<Operand>, y: impl Into<Operand>) {
        let x = x.into();
        let y = y.into();
        self.statements.alloc(Pc { x, y }.into());
    }

    /// Create a [block path condition][crate::ast::BlockPc].
    ///
    /// Expresses that `x` is equal to `y` on an incoming edge to `block` in the
    /// control-flow graph.
    ///
    /// # Panics
    ///
    /// Panics if `predecessor` is greater than or equal to `block`'s number of
    /// predecessors.
    pub fn block_pc(
        &mut self,
        block: BlockId,
        predecessor: u32,
        x: impl Into<Operand>,
        y: impl Into<Operand>,
    ) {
        let x = x.into();
        let y = y.into();
        self.statements.alloc(
            BlockPc {
                block,
                predecessor,
                x,
                y,
            }
            .into(),
        );
    }

    /// Finish building this replacement by providing the left- and right-hand
    /// sides.
    pub fn finish(
        self,
        lhs: ValueId,
        rhs: impl Into<Operand>,
        attributes: impl IntoIterator<Item = RootAttribute>,
    ) -> Replacement {
        Replacement::LhsRhs {
            statements: self.statements,
            lhs: Infer {
                value: lhs,
                attributes: attributes.into_iter().collect(),
            },
            rhs: rhs.into(),
        }
    }
}

/// A candidate rewrite.
#[derive(Clone, Debug)]
pub struct Cand {
    /// The left-hand side expression that can be replaced by the right-hand
    /// side.
    pub lhs: Operand,

    /// The right-hand side expression that can replace the left-hand side.
    pub rhs: Operand,

    /// Attributes for this rewrite.
    pub attributes: Vec<RootAttribute>,
}

/// The left-hand side of a replacement.
#[derive(Clone, Debug)]
pub struct LeftHandSide {
    /// Statements making up this LHS's expression DAG.
    pub statements: Arena<Statement>,

    /// The root of this LHS's expression DAG.
    pub infer: Infer,
}

/// A builder for a [`LeftHandSide`][crate::ast::LeftHandSide].
#[derive(Clone, Debug, Default)]
pub struct LeftHandSideBuilder {
    statements: Arena<Statement>,
}

impl LeftHandSideBuilder {
    /// Create a new assignment statement.
    ///
    /// Returns the value defined by the assignment.
    ///
    /// # Panics
    ///
    /// Panics if `name` (when given) does not start with '%'.
    pub fn assignment(
        &mut self,
        name: Option<String>,
        r#type: Option<Type>,
        value: impl Into<AssignmentRhs>,
        attributes: Vec<Attribute>,
    ) -> ValueId {
        let name = name.unwrap_or_else(|| format!("%{}", self.statements.len()));
        assert!(name.starts_with('%'));
        ValueId(
            self.statements.alloc(
                Assignment {
                    name,
                    r#type,
                    value: value.into(),
                    attributes,
                }
                .into(),
            ),
        )
    }

    /// Create a new [basic block][crate::ast::Block].
    ///
    /// Declare that the block has `predecessors` number of incoming edges in
    /// the control-flow graph.
    ///
    /// # Panics
    ///
    /// Panics if `name` (when given) does not start with '%'.
    pub fn block(&mut self, name: Option<String>, predecessors: u32) -> BlockId {
        BlockId(self.assignment(name, None, Block { predecessors }, vec![]))
    }

    /// Create a [path condition][crate::ast::Pc].
    ///
    /// Expresses the fact that `x` must equal `y` for the replacement to be
    /// valid.
    pub fn pc(&mut self, x: impl Into<Operand>, y: impl Into<Operand>) {
        let x = x.into();
        let y = y.into();
        self.statements.alloc(Pc { x, y }.into());
    }

    /// Create a [block path condition][crate::ast::BlockPc].
    ///
    /// Expresses that `x` is equal to `y` on an incoming edge to `block` in the
    /// control-flow graph.
    ///
    /// # Panics
    ///
    /// Panics if `predecessor` is greater than or equal to `block`'s number of
    /// predecessors.
    pub fn block_pc(
        &mut self,
        block: BlockId,
        predecessor: u32,
        x: impl Into<Operand>,
        y: impl Into<Operand>,
    ) {
        let x = x.into();
        let y = y.into();
        self.statements.alloc(
            BlockPc {
                block,
                predecessor,
                x,
                y,
            }
            .into(),
        );
    }

    /// Finish building this `LeftHandSide`.
    pub fn finish(
        self,
        lhs: ValueId,
        attributes: impl IntoIterator<Item = RootAttribute>,
    ) -> LeftHandSide {
        LeftHandSide {
            statements: self.statements,
            infer: Infer {
                value: lhs,
                attributes: attributes.into_iter().collect(),
            },
        }
    }

    /// Get the assignment that created the given value.
    ///
    /// # Panics
    ///
    /// May panic when given a `ValudId` from a different LHS, RHS, or
    /// replacement.
    pub fn get_value(&self, id: ValueId) -> &Assignment {
        match &self.statements[id.into()] {
            Statement::Assignment(a) => a,
            _ => panic!(),
        }
    }
}

/// The root of a left-hand side.
#[derive(Clone, Debug)]
pub struct Infer {
    /// The value to be replaced by the right-hand side.
    pub value: ValueId,

    /// Attributes for this left-hand side.
    pub attributes: Vec<RootAttribute>,
}

/// The right-hand side of a replacement.
#[derive(Clone, Debug)]
pub struct RightHandSide {
    /// Statements making up this RHS's expression DAG.
    pub statements: Arena<Statement>,

    /// The root of this RHS's expression DAG.
    pub result: Operand,
}

/// A statement in a LHS or RHS.
#[derive(Clone, Debug)]
pub enum Statement {
    /// An assignment statement.
    Assignment(Assignment),

    /// A path condition statement.
    Pc(Pc),

    /// A block path condition statement.
    BlockPc(BlockPc),
}

impl From<Assignment> for Statement {
    fn from(a: Assignment) -> Self {
        Statement::Assignment(a)
    }
}

impl From<Pc> for Statement {
    fn from(pc: Pc) -> Self {
        Statement::Pc(pc)
    }
}

impl From<BlockPc> for Statement {
    fn from(bpc: BlockPc) -> Self {
        Statement::BlockPc(bpc)
    }
}

/// An assignment, defining a value.
#[derive(Clone, Debug)]
pub struct Assignment {
    /// The name of the value being defined by this assignment.
    pub name: String,

    /// The ascribed type, if any.
    pub r#type: Option<Type>,

    /// The value being bound in this assignment.
    pub value: AssignmentRhs,

    /// Attributes describing data-flow facts known about this value.
    pub attributes: Vec<Attribute>,
}

/// Any value that can be assigned to a name.
#[derive(Clone, Debug)]
pub enum AssignmentRhs {
    /// An input variable.
    Var,

    /// A basic block.
    Block(Block),

    /// A phi node.
    Phi(Phi),

    /// A hole reserved for an as-of-yet-unknown instruction.
    ReservedInst,

    /// A hole reserved for an as-of-yet-unknown constant.
    ReservedConst,

    /// An instruction and its operands.
    Instruction(Instruction),
}

impl From<Block> for AssignmentRhs {
    fn from(b: Block) -> Self {
        AssignmentRhs::Block(b)
    }
}

impl From<Phi> for AssignmentRhs {
    fn from(p: Phi) -> Self {
        AssignmentRhs::Phi(p)
    }
}

impl From<Instruction> for AssignmentRhs {
    fn from(i: Instruction) -> Self {
        AssignmentRhs::Instruction(i)
    }
}

/// An input variable.
#[derive(Clone, Debug)]
pub struct Var {
    /// Attributes describing dataflow facts known about this input variable.
    pub attributes: Vec<Attribute>,
}

/// A basic block.
#[derive(Clone, Debug)]
pub struct Block {
    /// The number of incoming predecessors that this basic block has in the
    /// control-flow graph.
    pub predecessors: u32,
}

/// A phi node.
///
/// If a phi's `block` has `n` predecessors, then the length of `values` must be
/// `n`.
///
/// All phi nodes associated with a particular `block` will have their `i`th
/// value selected when control flow comes from `block`'s `i`th predecessor. For
/// example, given:
///
/// ```text
/// %bb = block 3
/// %a = phi %bb, 1, 2, 3
/// %b = phi %bb, 4, 5, 6
/// %c = phi %bb, 7, 8, 9
/// ```
///
/// If the basic block `%bb` has three control-flow predecessors. If it is
/// entered via its first predecessor, then `%a == 1`, `%b == 4`, and `%c ==
/// 7`. If it is entered via its second predecessor, then `%a == 2`, `%b == 5`,
/// and `%c == 8`. Finally, if it is entered via its third predecessor, then `%a
/// == 3`, `%b == 6`, and `%c == 9`.
#[derive(Clone, Debug)]
pub struct Phi {
    /// The basic block that this phi node is contained within.
    pub block: BlockId,

    /// The potential values for this phi node.
    pub values: Vec<Operand>,
}

macro_rules! define_instructions {
    (
        $(
            $( #[$attr:meta] )*
            $token:literal => $inst:ident $( ( $($operand:ident),* ) )? ;
        )*
    ) => {
        /// A Souper instruction.
        #[derive(Copy, Clone, Debug)]
        pub enum Instruction {
            $(
                $( #[$attr] )*
                $inst $( { $(
                    #[allow(missing_docs)]
                    $operand: Operand
                ),* } )? ,
            )*
        }

        #[cfg(feature = "parse")]
        impl crate::parse::Peek for Instruction {
            fn peek<'a>(parser: &mut crate::parse::Parser<'a>) -> crate::parse::Result<bool> {
                match parser.lookahead()? {
                    Some(crate::parse::Token::Ident(ident)) => Ok( false $( || ident == $token )* ),
                    _ => Ok(false),
                }
            }
        }

        #[cfg(feature = "parse")]
        impl crate::parse::Parse for Instruction {
            fn parse<'a>(parser: &mut crate::parse::Parser<'a>) -> crate::parse::Result<Self> {
                let ident = parser.token()?;
                match ident {
                    $(
                        #[allow(unused_assignments)]
                        crate::parse::Token::Ident($token) => {
                            $(
                                let mut first = true;
                                $(
                                    if !first {
                                        parser.comma()?;
                                    }
                                    let $operand = Operand::parse(parser)?;
                                    first = false;
                                )*
                            )?
                            Ok(Instruction::$inst $( { $( $operand ),* } )? )
                        }
                    )*
                    _ => parser.error("expected instruction"),
                }
            }
        }

        #[cfg(feature = "stringify")]
        impl Instruction {
            pub(crate) fn value_ids(&self, mut f: impl FnMut(ValueId)) {
                match self {
                    $(
                        Instruction::$inst $( { $( $operand ),* } )? => {
                            $(
                                $(
                                    if let Operand::Value(v) = $operand {
                                        f(*v);
                                    }
                                )*
                            )?
                        }
                    )*
                }
            }

            pub(crate) fn operands(&self, mut f: impl FnMut(Operand)) {
                match self {
                    $(
                        Instruction::$inst $( { $( $operand ),* } )? => {
                            $(
                                $(
                                    f(*$operand);
                                )*
                            )?
                        }
                    )*
                }
            }

            pub(crate) fn instruction_name(&self) -> &'static str {
                match self {
                    $(
                        Instruction::$inst $( { $( $operand: _),* } )? => $token,
                    )*
                }
            }
        }
    };
}

define_instructions! {
    /// Wrapping integer addition.
    "add" => Add(a, b);

    /// Integer addition where signed overflow is undefined behavior.
    "addnsw" => AddNsw(a, b);

    /// Integer addition where unsigned overflow is undefined behavior.
    "addnuw" => AddNuw(a, b);

    /// Integer addition where any kind of overflow is undefined behavior.
    "addnw" => AddNw(a, b);

    /// Wrapping integer subtraction.
    "sub" => Sub(a, b);

    /// Integer subtraction where signed overflow is undefined behavior.
    "subnsw" => SubNsw(a, b);

    /// Integer subtraction where unsigned overflow is undefined behavior.
    "subnuw" => SubNuw(a, b);

    /// Integer subtraction where any kind of overflow is undefined behavior.
    "subnw" => SubNw(a, b);

    /// Wrapping integer multiplication.
    "mul" => Mul(a, b);

    /// Integer multiplication where signed overflow is undefined behavior.
    "mulnsw" => MulNsw(a, b);

    /// Integer multiplication where unsigned overflow is undefined behavior.
    "mulnuw" => MulNuw(a, b);

    /// Integer multiplication where any kind of overflow is undefined behavior.
    "mulnw" => MulNw(a, b);

    /// Unsigned integer division.
    "udiv" => Udiv(a, b);

    /// Signed integer division.
    "sdiv" => Sdiv(a, b);

    /// Unsigned division where `a` must be exactly divisible by `b`. If `a` is
    /// not exactly divisible by `b`, then the result is undefined behavior.
    "udivexact" => UdivExact(a, b);

    /// Signed division where `a` must be exactly divisible by `b`. If `a` is
    /// not exactly divisible by `b`, then the result is undefined behavior.
    "sdivexact" => SdivExact(a, b);

    /// Unsigned integer remainder.
    "urem" => Urem(a, b);

    /// Signed integer remainder.
    "srem" => Srem(a, b);

    /// Bit-wise and.
    "and" => And(a, b);

    /// Bit-wise or.
    "or" => Or(a, b);

    /// Bit-wise xor.
    "xor" => Xor(a, b);

    /// Bit shift left. Undefined behavior if `b` is greater than or equal to `bitwidth(a)`.
    "shl" => Shl(a, b);

    /// Bit shift left where shifting out any non-sign bits is undefined
    /// behavior.
    "shlnsw" => ShlNsw(a, b);

    /// Bit shift left where shifting out any non-zero bits is undefined
    /// behavior.
    "shlnuw" => ShlNuw(a, b);

    /// Bit shift left where shifting out any non-zero or non-sign bits is
    /// undefined behavior.
    "shlnw" => ShlNw(a, b);

    /// Logical bit shift right (fills left `b` bits with zero).  Undefined
    /// behavior if `b` is greater than or equal to `bitwidth(a)`.
    "lshr" => Lshr(a, b);

    /// Logical bit shift right (fills left `b` bits with zero) where it is
    /// undefined behavior if any bits shifted out are non-zero.
    "lshrexact" => LshrExact(a, b);

    /// Arithmetic bit shift right (sign extends the left `b` bits).
    "ashr" => Ashr(a, b);

    /// Arithmetic bit shift right (fills left `b` bits with zero) where it is
    /// undefined behavior if any bits shifted out are non-zero.
    "ashrexact" => AshrExact(a, b);

    /// If `a` is 1, then evaluates to `b`, otherwise evaluates to `c`.
    "select" => Select(a, b, c);

    /// Zero extend `a`.
    "zext" => Zext(a);

    /// Sign extend `a`.
    "sext" => Sext(a);

    /// Truncate `a`.
    "trunc" => Trunc(a);

    /// `a == b`.
    "eq" => Eq(a, b);

    /// `a != b`
    "ne" => Ne(a, b);

    /// Unsigned less than.
    "ult" => Ult(a, b);

    /// Signed less than.
    "slt" => Slt(a, b);

    /// Unsigned less than or equal.
    "ule" => Ule(a, b);

    /// Signed less than or equal.
    "sle" => Sle(a, b);

    /// Count the number of 1 bits in `a`.
    "ctpop" => Ctpop(a);

    /// Swap bytes in `a`, e.g. `0xaabbccdd` becomes `0xddccbbaa`.
    "bswap" => Bswap(a);

    /// Reverse the bits in `a`.
    "bitreverse" => BitReverse(a);

    /// Count trailing zero bits in `a`.
    "cttz" => Cttz(a);

    /// Count leading one bits in `a`.
    "ctlz" => Ctlz(a);

    /// Left funnel shift.
    "fshl" => Fshl(a, b, c);

    /// Right funnel shift.
    "fshr" => Fshr(a, b, c);

    /// Wrapping signed integer addition of `a` and `b` where the result is
    /// extended by one bit which indicates whether the addition overflowed.
    "sadd.with.overflow" => SaddWithOverflow(a, b);

    /// Wrapping unsigned integer addition of `a` and `b` where the result is
    /// extended by one bit which indicates whether the addition overflowed.
    "uadd.with.overflow" => UaddWithOverflow(a, b);

    /// Wrapping signed integer subtraction of `a` and `b` where the result is
    /// extended by one bit which indicates whether the subtraction overflowed.
    "ssub.with.overflow" => SsubWithOverflow(a, b);

    /// Wrapping unsigned integer subtraction of `a` and `b` where the result is
    /// extended by one bit which indicates whether the subtraction overflowed.
    "usub.with.overflow" => UsubWithOverflow(a, b);

    /// Wrapping signed integer multiplication of `a` and `b` where the result
    /// is extended by one bit which indicates whether the multiplication
    /// overflowed.
    "smul.with.overflow" => SmulWithOverflow(a, b);

    /// Wrapping unsigned integer multiplication of `a` and `b` where the result
    /// is extended by one bit which indicates whether the multiplication
    /// overflowed.
    "umul.with.overflow" => UmulWithOverflow(a, b);

    /// Signed saturating integer addition.
    "sadd.sat" => SaddSat(a, b);

    /// Unsigned saturating integer addition.
    "uadd.sat" => UaddSat(a, b);

    /// Signed saturating integer subtraction.
    "ssub.sat" => SsubSat(a, b);

    /// Unsigned saturating integer subtraction.
    "usub.sat" => UsubSat(a, b);

    /// Extract the `b`th value from `a`.
    "extractvalue" => ExtractValue(a, b);

    /// A hole reserved for an unknown instruction or value.
    "hole" => Hole;

    /// If `a` is a poison or undef value, returns an arbitrary but fixed
    /// value. Otherwise returns `a`.
    "freeze" => Freeze(a);
}

/// An operand for an instruction or some other kind of operation.
#[derive(Copy, Clone, Debug)]
pub enum Operand {
    /// The id of a value defined in an earlier statement.
    Value(ValueId),

    /// A literal constant value.
    Constant(Constant),
}

impl From<Constant> for Operand {
    fn from(c: Constant) -> Self {
        Operand::Constant(c)
    }
}

impl From<ValueId> for Operand {
    fn from(v: ValueId) -> Self {
        Operand::Value(v)
    }
}

/// Attributes describing data-flow facts known about the root of a left- or
/// right-hand side.
#[derive(Clone, Debug)]
pub enum RootAttribute {
    /// Which bits of the resulting value are actually used.
    ///
    /// The vector must have a boolean for each bit in the result type, e.g. if
    /// the result type is `i32`, then the vector's length must be 32.
    ///
    /// If the `n`th entry in the vector is `true`, then the `n`th bit of the
    /// result is used. If it is `false`, then that bit is not used.
    DemandedBits(Vec<bool>),

    /// TODO
    HarvestedFromUse,
}

/// Attributes describing data-flow facts known about a particular value or
/// result of an instruction.
#[derive(Clone, Debug)]
pub enum Attribute {
    /// Bits that are known to be set or unset.
    ///
    /// The vector must have an entry for each bit in the value, e.g. if the
    /// value's type is `i32`, then the vector's length must be 32.
    ///
    /// If the `i`th bit is known to be set, then the `i`th entry should be
    /// `Some(true)`. If the `i`th bit is known to be unset, then the `i`th
    /// entry should be `Some(false)`. If it is unknown whether the `i`th bit is
    /// set or unset, or it can sometimes be either, then the `i`th entry should
    /// be `None`.
    KnownBits(Vec<Option<bool>>),

    /// The value is known to be a power of two.
    PowerOfTwo,

    /// The value is known to be negative.
    Negative,

    /// The value is known to be non-negative.
    NonNegative,

    /// The value is known to be non-zero.
    NonZero,

    /// The value is used by other expressions, not just this replacement's
    /// expression DAG.
    HasExternalUses,

    /// It is known that there are `n` sign bits in this value.
    SignBits(u8),

    /// This value is within the range `.0` (inclusive) to `.1` (exclusive).
    Range(i128, i128),
}

/// A path condition.
///
/// Expresses the fact that `x` must equal `y` for the replacement to be valid.
#[derive(Clone, Debug)]
pub struct Pc {
    /// A value that must be equal to `y` for the replacement to be valid.
    pub x: Operand,

    /// A value that must be equal to `x` for the replacement to be valid.
    pub y: Operand,
}

/// A block path condition.
///
/// Expresses that `x` is equal to `y` on an incoming predecessor to `block` in
/// the control-flow graph.
#[derive(Clone, Debug)]
pub struct BlockPc {
    /// The basic block in question.
    pub block: BlockId,

    /// The `i`th control-flow predecessor of `block`.
    ///
    /// Zero-indexed: must be less than `block`'s total number of predecessors.
    pub predecessor: u32,

    /// Must be equal to `y` if control-flow entered `block` via `predecessor`.
    pub x: Operand,

    /// Must be equal to `x` if control-flow entered `block` via `predecessor`.
    pub y: Operand,
}

/// A constant value.
///
/// Optionally has an ascribed type.
#[derive(Copy, Clone, Debug)]
pub struct Constant {
    /// The constant value.
    pub value: i128,

    /// The ascribed type, if any.
    pub r#type: Option<Type>,
}

/// A type.
///
/// All Souper types are integers, they just have different bit widths.
#[derive(Copy, Clone, Debug)]
pub struct Type {
    /// The bit width of this integer type.
    pub width: u16,
}