| ... | ... | @@ -36,10 +36,10 @@ use std::collections::VecDeque; |
| 36 | 36 | use std::fmt::{Debug, Formatter}; |
| 37 | 37 | use std::ops::Range; |
| 38 | 38 | |
| 39 | | use rustc_data_structures::fx::FxHashMap; |
| 39 | use rustc_data_structures::fx::{FxHashMap, StdEntry}; |
| 40 | 40 | use rustc_data_structures::stack::ensure_sufficient_stack; |
| 41 | 41 | use rustc_index::bit_set::BitSet; |
| 42 | | use rustc_index::{IndexSlice, IndexVec}; |
| 42 | use rustc_index::IndexVec; |
| 43 | 43 | use rustc_middle::bug; |
| 44 | 44 | use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor}; |
| 45 | 45 | use rustc_middle::mir::*; |
| ... | ... | @@ -336,14 +336,13 @@ impl<'tcx, T: ValueAnalysis<'tcx>> AnalysisDomain<'tcx> for ValueAnalysisWrapper |
| 336 | 336 | const NAME: &'static str = T::NAME; |
| 337 | 337 | |
| 338 | 338 | fn bottom_value(&self, _body: &Body<'tcx>) -> Self::Domain { |
| 339 | | State(StateData::Unreachable) |
| 339 | State::Unreachable |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) { |
| 343 | 343 | // The initial state maps all tracked places of argument projections to ⊤ and the rest to ⊥. |
| 344 | | assert!(matches!(state.0, StateData::Unreachable)); |
| 345 | | let values = IndexVec::from_elem_n(T::Value::BOTTOM, self.0.map().value_count); |
| 346 | | *state = State(StateData::Reachable(values)); |
| 344 | assert!(matches!(state, State::Unreachable)); |
| 345 | *state = State::new_reachable(); |
| 347 | 346 | for arg in body.args_iter() { |
| 348 | 347 | state.flood(PlaceRef { local: arg, projection: &[] }, self.0.map()); |
| 349 | 348 | } |
| ... | ... | @@ -415,27 +414,54 @@ rustc_index::newtype_index!( |
| 415 | 414 | |
| 416 | 415 | /// See [`State`]. |
| 417 | 416 | #[derive(PartialEq, Eq, Debug)] |
| 418 | | enum StateData<V> { |
| 419 | | Reachable(IndexVec<ValueIndex, V>), |
| 420 | | Unreachable, |
| 417 | pub struct StateData<V> { |
| 418 | bottom: V, |
| 419 | /// This map only contains values that are not `⊥`. |
| 420 | map: FxHashMap<ValueIndex, V>, |
| 421 | } |
| 422 | |
| 423 | impl<V: HasBottom> StateData<V> { |
| 424 | fn new() -> StateData<V> { |
| 425 | StateData { bottom: V::BOTTOM, map: FxHashMap::default() } |
| 426 | } |
| 427 | |
| 428 | fn get(&self, idx: ValueIndex) -> &V { |
| 429 | self.map.get(&idx).unwrap_or(&self.bottom) |
| 430 | } |
| 431 | |
| 432 | fn insert(&mut self, idx: ValueIndex, elem: V) { |
| 433 | if elem.is_bottom() { |
| 434 | self.map.remove(&idx); |
| 435 | } else { |
| 436 | self.map.insert(idx, elem); |
| 437 | } |
| 438 | } |
| 421 | 439 | } |
| 422 | 440 | |
| 423 | 441 | impl<V: Clone> Clone for StateData<V> { |
| 424 | 442 | fn clone(&self) -> Self { |
| 425 | | match self { |
| 426 | | Self::Reachable(x) => Self::Reachable(x.clone()), |
| 427 | | Self::Unreachable => Self::Unreachable, |
| 428 | | } |
| 443 | StateData { bottom: self.bottom.clone(), map: self.map.clone() } |
| 429 | 444 | } |
| 430 | 445 | |
| 431 | 446 | fn clone_from(&mut self, source: &Self) { |
| 432 | | match (&mut *self, source) { |
| 433 | | (Self::Reachable(x), Self::Reachable(y)) => { |
| 434 | | // We go through `raw` here, because `IndexVec` currently has a naive `clone_from`. |
| 435 | | x.raw.clone_from(&y.raw); |
| 447 | self.map.clone_from(&source.map) |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | impl<V: JoinSemiLattice + Clone + HasBottom> JoinSemiLattice for StateData<V> { |
| 452 | fn join(&mut self, other: &Self) -> bool { |
| 453 | let mut changed = false; |
| 454 | #[allow(rustc::potential_query_instability)] |
| 455 | for (i, v) in other.map.iter() { |
| 456 | match self.map.entry(*i) { |
| 457 | StdEntry::Vacant(e) => { |
| 458 | e.insert(v.clone()); |
| 459 | changed = true |
| 460 | } |
| 461 | StdEntry::Occupied(e) => changed |= e.into_mut().join(v), |
| 436 | 462 | } |
| 437 | | _ => *self = source.clone(), |
| 438 | 463 | } |
| 464 | changed |
| 439 | 465 | } |
| 440 | 466 | } |
| 441 | 467 | |
| ... | ... | @@ -450,33 +476,47 @@ impl<V: Clone> Clone for StateData<V> { |
| 450 | 476 | /// |
| 451 | 477 | /// Flooding means assigning a value (by default `⊤`) to all tracked projections of a given place. |
| 452 | 478 | #[derive(PartialEq, Eq, Debug)] |
| 453 | | pub struct State<V>(StateData<V>); |
| 479 | pub enum State<V> { |
| 480 | Unreachable, |
| 481 | Reachable(StateData<V>), |
| 482 | } |
| 454 | 483 | |
| 455 | 484 | impl<V: Clone> Clone for State<V> { |
| 456 | 485 | fn clone(&self) -> Self { |
| 457 | | Self(self.0.clone()) |
| 486 | match self { |
| 487 | Self::Reachable(x) => Self::Reachable(x.clone()), |
| 488 | Self::Unreachable => Self::Unreachable, |
| 489 | } |
| 458 | 490 | } |
| 459 | 491 | |
| 460 | 492 | fn clone_from(&mut self, source: &Self) { |
| 461 | | self.0.clone_from(&source.0); |
| 493 | match (&mut *self, source) { |
| 494 | (Self::Reachable(x), Self::Reachable(y)) => { |
| 495 | x.clone_from(&y); |
| 496 | } |
| 497 | _ => *self = source.clone(), |
| 498 | } |
| 462 | 499 | } |
| 463 | 500 | } |
| 464 | 501 | |
| 465 | | impl<V: Clone> State<V> { |
| 466 | | pub fn new(init: V, map: &Map) -> State<V> { |
| 467 | | let values = IndexVec::from_elem_n(init, map.value_count); |
| 468 | | State(StateData::Reachable(values)) |
| 502 | impl<V: Clone + HasBottom> State<V> { |
| 503 | pub fn new_reachable() -> State<V> { |
| 504 | State::Reachable(StateData::new()) |
| 469 | 505 | } |
| 470 | 506 | |
| 471 | | pub fn all(&self, f: impl Fn(&V) -> bool) -> bool { |
| 472 | | match self.0 { |
| 473 | | StateData::Unreachable => true, |
| 474 | | StateData::Reachable(ref values) => values.iter().all(f), |
| 507 | pub fn all_bottom(&self) -> bool { |
| 508 | match self { |
| 509 | State::Unreachable => false, |
| 510 | State::Reachable(ref values) => |
| 511 | { |
| 512 | #[allow(rustc::potential_query_instability)] |
| 513 | values.map.values().all(V::is_bottom) |
| 514 | } |
| 475 | 515 | } |
| 476 | 516 | } |
| 477 | 517 | |
| 478 | 518 | fn is_reachable(&self) -> bool { |
| 479 | | matches!(&self.0, StateData::Reachable(_)) |
| 519 | matches!(self, State::Reachable(_)) |
| 480 | 520 | } |
| 481 | 521 | |
| 482 | 522 | /// Assign `value` to all places that are contained in `place` or may alias one. |
| ... | ... | @@ -519,10 +559,8 @@ impl<V: Clone> State<V> { |
| 519 | 559 | map: &Map, |
| 520 | 560 | value: V, |
| 521 | 561 | ) { |
| 522 | | let StateData::Reachable(values) = &mut self.0 else { return }; |
| 523 | | map.for_each_aliasing_place(place, tail_elem, &mut |vi| { |
| 524 | | values[vi] = value.clone(); |
| 525 | | }); |
| 562 | let State::Reachable(values) = self else { return }; |
| 563 | map.for_each_aliasing_place(place, tail_elem, &mut |vi| values.insert(vi, value.clone())); |
| 526 | 564 | } |
| 527 | 565 | |
| 528 | 566 | /// Low-level method that assigns to a place. |
| ... | ... | @@ -541,9 +579,9 @@ impl<V: Clone> State<V> { |
| 541 | 579 | /// |
| 542 | 580 | /// The target place must have been flooded before calling this method. |
| 543 | 581 | pub fn insert_value_idx(&mut self, target: PlaceIndex, value: V, map: &Map) { |
| 544 | | let StateData::Reachable(values) = &mut self.0 else { return }; |
| 582 | let State::Reachable(values) = self else { return }; |
| 545 | 583 | if let Some(value_index) = map.places[target].value_index { |
| 546 | | values[value_index] = value; |
| 584 | values.insert(value_index, value) |
| 547 | 585 | } |
| 548 | 586 | } |
| 549 | 587 | |
| ... | ... | @@ -555,14 +593,14 @@ impl<V: Clone> State<V> { |
| 555 | 593 | /// |
| 556 | 594 | /// The target place must have been flooded before calling this method. |
| 557 | 595 | pub fn insert_place_idx(&mut self, target: PlaceIndex, source: PlaceIndex, map: &Map) { |
| 558 | | let StateData::Reachable(values) = &mut self.0 else { return }; |
| 596 | let State::Reachable(values) = self else { return }; |
| 559 | 597 | |
| 560 | 598 | // If both places are tracked, we copy the value to the target. |
| 561 | 599 | // If the target is tracked, but the source is not, we do nothing, as invalidation has |
| 562 | 600 | // already been performed. |
| 563 | 601 | if let Some(target_value) = map.places[target].value_index { |
| 564 | 602 | if let Some(source_value) = map.places[source].value_index { |
| 565 | | values[target_value] = values[source_value].clone(); |
| 603 | values.insert(target_value, values.get(source_value).clone()); |
| 566 | 604 | } |
| 567 | 605 | } |
| 568 | 606 | for target_child in map.children(target) { |
| ... | ... | @@ -616,11 +654,11 @@ impl<V: Clone> State<V> { |
| 616 | 654 | |
| 617 | 655 | /// Retrieve the value stored for a place index, or `None` if it is not tracked. |
| 618 | 656 | pub fn try_get_idx(&self, place: PlaceIndex, map: &Map) -> Option<V> { |
| 619 | | match &self.0 { |
| 620 | | StateData::Reachable(values) => { |
| 621 | | map.places[place].value_index.map(|v| values[v].clone()) |
| 657 | match self { |
| 658 | State::Reachable(values) => { |
| 659 | map.places[place].value_index.map(|v| values.get(v).clone()) |
| 622 | 660 | } |
| 623 | | StateData::Unreachable => None, |
| 661 | State::Unreachable => None, |
| 624 | 662 | } |
| 625 | 663 | } |
| 626 | 664 | |
| ... | ... | @@ -631,10 +669,10 @@ impl<V: Clone> State<V> { |
| 631 | 669 | where |
| 632 | 670 | V: HasBottom + HasTop, |
| 633 | 671 | { |
| 634 | | match &self.0 { |
| 635 | | StateData::Reachable(_) => self.try_get(place, map).unwrap_or(V::TOP), |
| 672 | match self { |
| 673 | State::Reachable(_) => self.try_get(place, map).unwrap_or(V::TOP), |
| 636 | 674 | // Because this is unreachable, we can return any value we want. |
| 637 | | StateData::Unreachable => V::BOTTOM, |
| 675 | State::Unreachable => V::BOTTOM, |
| 638 | 676 | } |
| 639 | 677 | } |
| 640 | 678 | |
| ... | ... | @@ -645,10 +683,10 @@ impl<V: Clone> State<V> { |
| 645 | 683 | where |
| 646 | 684 | V: HasBottom + HasTop, |
| 647 | 685 | { |
| 648 | | match &self.0 { |
| 649 | | StateData::Reachable(_) => self.try_get_discr(place, map).unwrap_or(V::TOP), |
| 686 | match self { |
| 687 | State::Reachable(_) => self.try_get_discr(place, map).unwrap_or(V::TOP), |
| 650 | 688 | // Because this is unreachable, we can return any value we want. |
| 651 | | StateData::Unreachable => V::BOTTOM, |
| 689 | State::Unreachable => V::BOTTOM, |
| 652 | 690 | } |
| 653 | 691 | } |
| 654 | 692 | |
| ... | ... | @@ -659,10 +697,10 @@ impl<V: Clone> State<V> { |
| 659 | 697 | where |
| 660 | 698 | V: HasBottom + HasTop, |
| 661 | 699 | { |
| 662 | | match &self.0 { |
| 663 | | StateData::Reachable(_) => self.try_get_len(place, map).unwrap_or(V::TOP), |
| 700 | match self { |
| 701 | State::Reachable(_) => self.try_get_len(place, map).unwrap_or(V::TOP), |
| 664 | 702 | // Because this is unreachable, we can return any value we want. |
| 665 | | StateData::Unreachable => V::BOTTOM, |
| 703 | State::Unreachable => V::BOTTOM, |
| 666 | 704 | } |
| 667 | 705 | } |
| 668 | 706 | |
| ... | ... | @@ -673,11 +711,11 @@ impl<V: Clone> State<V> { |
| 673 | 711 | where |
| 674 | 712 | V: HasBottom + HasTop, |
| 675 | 713 | { |
| 676 | | match &self.0 { |
| 677 | | StateData::Reachable(values) => { |
| 678 | | map.places[place].value_index.map(|v| values[v].clone()).unwrap_or(V::TOP) |
| 714 | match self { |
| 715 | State::Reachable(values) => { |
| 716 | map.places[place].value_index.map(|v| values.get(v).clone()).unwrap_or(V::TOP) |
| 679 | 717 | } |
| 680 | | StateData::Unreachable => { |
| 718 | State::Unreachable => { |
| 681 | 719 | // Because this is unreachable, we can return any value we want. |
| 682 | 720 | V::BOTTOM |
| 683 | 721 | } |
| ... | ... | @@ -685,15 +723,15 @@ impl<V: Clone> State<V> { |
| 685 | 723 | } |
| 686 | 724 | } |
| 687 | 725 | |
| 688 | | impl<V: JoinSemiLattice + Clone> JoinSemiLattice for State<V> { |
| 726 | impl<V: JoinSemiLattice + Clone + HasBottom> JoinSemiLattice for State<V> { |
| 689 | 727 | fn join(&mut self, other: &Self) -> bool { |
| 690 | | match (&mut self.0, &other.0) { |
| 691 | | (_, StateData::Unreachable) => false, |
| 692 | | (StateData::Unreachable, _) => { |
| 728 | match (&mut *self, other) { |
| 729 | (_, State::Unreachable) => false, |
| 730 | (State::Unreachable, _) => { |
| 693 | 731 | *self = other.clone(); |
| 694 | 732 | true |
| 695 | 733 | } |
| 696 | | (StateData::Reachable(this), StateData::Reachable(other)) => this.join(other), |
| 734 | (State::Reachable(this), State::Reachable(ref other)) => this.join(other), |
| 697 | 735 | } |
| 698 | 736 | } |
| 699 | 737 | } |
| ... | ... | @@ -1194,9 +1232,9 @@ where |
| 1194 | 1232 | T::Value: Debug, |
| 1195 | 1233 | { |
| 1196 | 1234 | fn fmt_with(&self, ctxt: &ValueAnalysisWrapper<T>, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 1197 | | match &self.0 { |
| 1198 | | StateData::Reachable(values) => debug_with_context(values, None, ctxt.0.map(), f), |
| 1199 | | StateData::Unreachable => write!(f, "unreachable"), |
| 1235 | match self { |
| 1236 | State::Reachable(values) => debug_with_context(values, None, ctxt.0.map(), f), |
| 1237 | State::Unreachable => write!(f, "unreachable"), |
| 1200 | 1238 | } |
| 1201 | 1239 | } |
| 1202 | 1240 | |
| ... | ... | @@ -1206,8 +1244,8 @@ where |
| 1206 | 1244 | ctxt: &ValueAnalysisWrapper<T>, |
| 1207 | 1245 | f: &mut Formatter<'_>, |
| 1208 | 1246 | ) -> std::fmt::Result { |
| 1209 | | match (&self.0, &old.0) { |
| 1210 | | (StateData::Reachable(this), StateData::Reachable(old)) => { |
| 1247 | match (self, old) { |
| 1248 | (State::Reachable(this), State::Reachable(old)) => { |
| 1211 | 1249 | debug_with_context(this, Some(old), ctxt.0.map(), f) |
| 1212 | 1250 | } |
| 1213 | 1251 | _ => Ok(()), // Consider printing something here. |
| ... | ... | @@ -1215,21 +1253,21 @@ where |
| 1215 | 1253 | } |
| 1216 | 1254 | } |
| 1217 | 1255 | |
| 1218 | | fn debug_with_context_rec<V: Debug + Eq>( |
| 1256 | fn debug_with_context_rec<V: Debug + Eq + HasBottom>( |
| 1219 | 1257 | place: PlaceIndex, |
| 1220 | 1258 | place_str: &str, |
| 1221 | | new: &IndexSlice<ValueIndex, V>, |
| 1222 | | old: Option<&IndexSlice<ValueIndex, V>>, |
| 1259 | new: &StateData<V>, |
| 1260 | old: Option<&StateData<V>>, |
| 1223 | 1261 | map: &Map, |
| 1224 | 1262 | f: &mut Formatter<'_>, |
| 1225 | 1263 | ) -> std::fmt::Result { |
| 1226 | 1264 | if let Some(value) = map.places[place].value_index { |
| 1227 | 1265 | match old { |
| 1228 | | None => writeln!(f, "{}: {:?}", place_str, new[value])?, |
| 1266 | None => writeln!(f, "{}: {:?}", place_str, new.get(value))?, |
| 1229 | 1267 | Some(old) => { |
| 1230 | | if new[value] != old[value] { |
| 1231 | | writeln!(f, "\u{001f}-{}: {:?}", place_str, old[value])?; |
| 1232 | | writeln!(f, "\u{001f}+{}: {:?}", place_str, new[value])?; |
| 1268 | if new.get(value) != old.get(value) { |
| 1269 | writeln!(f, "\u{001f}-{}: {:?}", place_str, old.get(value))?; |
| 1270 | writeln!(f, "\u{001f}+{}: {:?}", place_str, new.get(value))?; |
| 1233 | 1271 | } |
| 1234 | 1272 | } |
| 1235 | 1273 | } |
| ... | ... | @@ -1261,9 +1299,9 @@ fn debug_with_context_rec<V: Debug + Eq>( |
| 1261 | 1299 | Ok(()) |
| 1262 | 1300 | } |
| 1263 | 1301 | |
| 1264 | | fn debug_with_context<V: Debug + Eq>( |
| 1265 | | new: &IndexSlice<ValueIndex, V>, |
| 1266 | | old: Option<&IndexSlice<ValueIndex, V>>, |
| 1302 | fn debug_with_context<V: Debug + Eq + HasBottom>( |
| 1303 | new: &StateData<V>, |
| 1304 | old: Option<&StateData<V>>, |
| 1267 | 1305 | map: &Map, |
| 1268 | 1306 | f: &mut Formatter<'_>, |
| 1269 | 1307 | ) -> std::fmt::Result { |