Skip to main content

rustc_mir_transform/
canonicalization.rs

1use rustc_middle::mir::*;
2use rustc_middle::ty::{Ty, TyCtxt};
3
4/// Canonicalize binary operations:
5/// 1. `op(const, place)` -> `op(place, const)`
6/// 2. `op(place _2, place _1)` -> `op(place _1, place _2)`
7///
8/// For ops `<`/`<=`/`>`/`>=`, we would also modify the op itself.
9pub(super) fn canonicalize_binary_operations<'tcx, 'body>(
10    tcx: TyCtxt<'tcx>,
11    local_decls: &'body LocalDecls<'tcx>,
12    op: &mut BinOp,
13    lhs: &mut Operand<'tcx>,
14    rhs: &mut Operand<'tcx>,
15) -> bool {
16    fn needs_swap<T: std::cmp::Ord>(lhs: &T, rhs: &T) -> Option<bool> {
17        use std::cmp::Ordering;
18
19        match lhs.cmp(rhs) {
20            Ordering::Less => Some(false),
21            Ordering::Equal => None,
22            Ordering::Greater => Some(true),
23        }
24    }
25
26    fn projection_rank<'tcx>(proj: &ProjectionElem<Local, Ty<'tcx>>) -> usize {
27        match proj {
28            ProjectionElem::Deref => 0,
29            ProjectionElem::Field(..) => 1,
30            ProjectionElem::Index(_) => 2,
31            ProjectionElem::ConstantIndex { .. } => 3,
32            ProjectionElem::Subslice { .. } => 4,
33            ProjectionElem::Downcast(..) => 5,
34            ProjectionElem::OpaqueCast(_) => 6,
35            ProjectionElem::UnwrapUnsafeBinder(_) => 7,
36            ProjectionElem::PhantomDeref => 8,
37        }
38    }
39
40    fn needs_swap_operands<'tcx>(lhs: &Operand<'tcx>, rhs: &Operand<'tcx>) -> bool {
41        let (lhs, rhs) = match (lhs, rhs) {
42            (Operand::Constant(_), Operand::Copy(_) | Operand::Move(_)) => return true,
43            (Operand::Copy(lhs) | Operand::Move(lhs), Operand::Copy(rhs) | Operand::Move(rhs)) => {
44                (lhs, rhs)
45            }
46            _ => return false,
47        };
48
49        if let Some(ns) = needs_swap(&lhs.local, &rhs.local) {
50            return ns;
51        }
52
53        let lhs_proj = lhs.projection;
54        let rhs_proj = rhs.projection;
55
56        if let Some(ns) = needs_swap(&lhs_proj.len(), &rhs_proj.len()) {
57            return ns;
58        }
59
60        for (lhs_proj, rhs_proj) in lhs_proj.iter().zip(rhs_proj.iter()) {
61            if let Some(ns) = match (lhs_proj, rhs_proj) {
62                (ProjectionElem::Field(lhs, _), ProjectionElem::Field(rhs, _)) => {
63                    needs_swap(&lhs, &rhs)
64                }
65
66                (ProjectionElem::Index(lhs), ProjectionElem::Index(rhs)) => needs_swap(&lhs, &rhs),
67
68                (
69                    ProjectionElem::ConstantIndex { offset: l0, min_length: l1, from_end: l2 },
70                    ProjectionElem::ConstantIndex { offset: r0, min_length: r1, from_end: r2 },
71                ) => needs_swap(&(l0, l1, l2), &(r0, r1, r2)),
72
73                (
74                    ProjectionElem::Subslice { from: l0, to: l1, from_end: l2 },
75                    ProjectionElem::Subslice { from: r0, to: r1, from_end: r2 },
76                ) => needs_swap(&(l0, l1, l2), &(r0, r1, r2)),
77
78                (ProjectionElem::Downcast(l0, l1), ProjectionElem::Downcast(r0, r1)) => {
79                    needs_swap(&(l0, l1), &(r0, r1))
80                }
81
82                _ => needs_swap(&projection_rank(&lhs_proj), &projection_rank(&rhs_proj)),
83            } {
84                return ns;
85            }
86        }
87
88        false
89    }
90
91    let (needs_integral, new_op) = match *op {
92        BinOp::Eq | BinOp::Ne => (false, *op),
93
94        BinOp::Add
95        | BinOp::AddUnchecked
96        | BinOp::AddWithOverflow
97        | BinOp::Mul
98        | BinOp::MulUnchecked
99        | BinOp::MulWithOverflow
100        | BinOp::BitXor
101        | BinOp::BitAnd
102        | BinOp::BitOr => (true, *op),
103
104        BinOp::Lt => (true, BinOp::Gt),
105        BinOp::Le => (true, BinOp::Ge),
106        BinOp::Ge => (true, BinOp::Le),
107        BinOp::Gt => (true, BinOp::Lt),
108
109        BinOp::Sub
110        | BinOp::SubUnchecked
111        | BinOp::SubWithOverflow
112        | BinOp::Div
113        | BinOp::Rem
114        | BinOp::Shl
115        | BinOp::ShlUnchecked
116        | BinOp::Shr
117        | BinOp::ShrUnchecked
118        | BinOp::Cmp
119        | BinOp::Offset => return false,
120    };
121
122    if needs_integral && !lhs.ty(local_decls, tcx).is_integral() {
123        return false;
124    }
125
126    if needs_swap_operands(lhs, rhs) {
127        *op = new_op;
128        std::mem::swap(lhs, rhs);
129        true
130    } else {
131        false
132    }
133}