Skip to content

Commit fbf68d0

Browse files
committed
Make polarity an enum.
1 parent b0c90ac commit fbf68d0

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

compiler/rustc_mir_transform/src/jump_threading.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
//! looking for assignments that will turn the `SwitchInt` into a simple `Goto`.
1212
//!
1313
//! The algorithm maintains a set of replacement conditions:
14-
//! - `conditions[place]` contains `Condition { value, polarity: true, target }`
14+
//! - `conditions[place]` contains `Condition { value, polarity: Eq, target }`
1515
//! if assigning `value` to `place` turns the `SwitchInt` into `Goto { target }`.
16-
//! - `conditions[place]` contains `Condition { value, polarity: false, target }`
16+
//! - `conditions[place]` contains `Condition { value, polarity: Ne, target }`
1717
//! if assigning anything different from `value` to `place` turns the `SwitchInt`
1818
//! into `Goto { target }`.
1919
//!
@@ -98,13 +98,13 @@ impl<'tcx> MirPass<'tcx> for JumpThreading {
9898
continue;
9999
};
100100
arena.alloc_from_iter([
101-
Condition { value, polarity: true, target: then },
102-
Condition { value, polarity: false, target: else_ },
101+
Condition { value, polarity: Polarity::Eq, target: then },
102+
Condition { value, polarity: Polarity::Ne, target: else_ },
103103
])
104104
} else {
105105
arena.alloc_from_iter(targets.iter().filter_map(|(value, target)| {
106106
let value = ScalarInt::try_from_uint(value, discr_layout.size)?;
107-
Some(Condition { value, polarity: true, target })
107+
Some(Condition { value, polarity: Polarity::Eq, target })
108108
}))
109109
};
110110
let conds = ConditionSet(conds);
@@ -149,18 +149,26 @@ struct TOFinder<'tcx, 'a> {
149149
#[derive(Copy, Clone, Debug)]
150150
struct Condition {
151151
value: ScalarInt,
152-
/// `true` means `==`, `false` means `!=`
153-
polarity: bool,
152+
polarity: Polarity,
154153
target: BasicBlock,
155154
}
156155

156+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
157+
enum Polarity {
158+
Ne,
159+
Eq,
160+
}
161+
157162
impl Condition {
158163
fn matches(&self, value: ScalarInt) -> bool {
159-
(self.value == value) == self.polarity
164+
(self.value == value) == (self.polarity == Polarity::Eq)
160165
}
161166

162167
fn inv(mut self) -> Self {
163-
self.polarity = !self.polarity;
168+
self.polarity = match self.polarity {
169+
Polarity::Eq => Polarity::Ne,
170+
Polarity::Ne => Polarity::Eq,
171+
};
164172
self
165173
}
166174
}
@@ -455,7 +463,11 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
455463
.try_to_scalar_int()?;
456464
let conds = conditions.map(self.arena, |c| Condition {
457465
value,
458-
polarity: c.matches(equals),
466+
polarity: if c.matches(equals) {
467+
Polarity::Eq
468+
} else {
469+
Polarity::Ne
470+
},
459471
..c
460472
});
461473
state.insert_value_idx(place, conds, self.map);
@@ -560,7 +572,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
560572
// Likewise, we know that `discr != value`. That's a must weaker information,
561573
// so we can only match the exact same condition.
562574
for c in conditions.iter() {
563-
if c.value == value && c.polarity == false {
575+
if c.value == value && c.polarity == Polarity::Ne {
564576
debug!(?target_bb, ?c.target, "register");
565577
self.opportunities
566578
.push(ThreadingOpportunity { chain: vec![], target: c.target });

0 commit comments

Comments
 (0)