|
11 | 11 | //! looking for assignments that will turn the `SwitchInt` into a simple `Goto`.
|
12 | 12 | //!
|
13 | 13 | //! 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 }` |
15 | 15 | //! 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 }` |
17 | 17 | //! if assigning anything different from `value` to `place` turns the `SwitchInt`
|
18 | 18 | //! into `Goto { target }`.
|
19 | 19 | //!
|
@@ -98,13 +98,13 @@ impl<'tcx> MirPass<'tcx> for JumpThreading {
|
98 | 98 | continue;
|
99 | 99 | };
|
100 | 100 | 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_ }, |
103 | 103 | ])
|
104 | 104 | } else {
|
105 | 105 | arena.alloc_from_iter(targets.iter().filter_map(|(value, target)| {
|
106 | 106 | 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 }) |
108 | 108 | }))
|
109 | 109 | };
|
110 | 110 | let conds = ConditionSet(conds);
|
@@ -149,18 +149,26 @@ struct TOFinder<'tcx, 'a> {
|
149 | 149 | #[derive(Copy, Clone, Debug)]
|
150 | 150 | struct Condition {
|
151 | 151 | value: ScalarInt,
|
152 |
| - /// `true` means `==`, `false` means `!=` |
153 |
| - polarity: bool, |
| 152 | + polarity: Polarity, |
154 | 153 | target: BasicBlock,
|
155 | 154 | }
|
156 | 155 |
|
| 156 | +#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 157 | +enum Polarity { |
| 158 | + Ne, |
| 159 | + Eq, |
| 160 | +} |
| 161 | + |
157 | 162 | impl Condition {
|
158 | 163 | fn matches(&self, value: ScalarInt) -> bool {
|
159 |
| - (self.value == value) == self.polarity |
| 164 | + (self.value == value) == (self.polarity == Polarity::Eq) |
160 | 165 | }
|
161 | 166 |
|
162 | 167 | 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 | + }; |
164 | 172 | self
|
165 | 173 | }
|
166 | 174 | }
|
@@ -455,7 +463,11 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
|
455 | 463 | .try_to_scalar_int()?;
|
456 | 464 | let conds = conditions.map(self.arena, |c| Condition {
|
457 | 465 | value,
|
458 |
| - polarity: c.matches(equals), |
| 466 | + polarity: if c.matches(equals) { |
| 467 | + Polarity::Eq |
| 468 | + } else { |
| 469 | + Polarity::Ne |
| 470 | + }, |
459 | 471 | ..c
|
460 | 472 | });
|
461 | 473 | state.insert_value_idx(place, conds, self.map);
|
@@ -560,7 +572,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
|
560 | 572 | // Likewise, we know that `discr != value`. That's a must weaker information,
|
561 | 573 | // so we can only match the exact same condition.
|
562 | 574 | for c in conditions.iter() {
|
563 |
| - if c.value == value && c.polarity == false { |
| 575 | + if c.value == value && c.polarity == Polarity::Ne { |
564 | 576 | debug!(?target_bb, ?c.target, "register");
|
565 | 577 | self.opportunities
|
566 | 578 | .push(ThreadingOpportunity { chain: vec![], target: c.target });
|
|
0 commit comments