1 //! A test for the logic that updates the state in a `ResultsCursor` during seek.
3 use rustc_index::bit_set::BitSet;
4 use rustc_index::vec::IndexVec;
5 use rustc_middle::mir::{self, BasicBlock, Location};
7 use rustc_span::DUMMY_SP;
10 use crate::dataflow::BottomValue;
12 /// Returns `true` if the given location points to a `Call` terminator that can return
14 fn is_call_terminator_non_diverging(body: &mir::Body<'_>, loc: Location) -> bool {
15 loc == body.terminator_loc(loc.block)
17 body[loc.block].terminator().kind,
18 mir::TerminatorKind::Call { destination: Some(_), .. }
22 /// Creates a `mir::Body` with a few disconnected basic blocks.
24 /// This is the `Body` that will be used by the `MockAnalysis` below. The shape of its CFG is not
26 fn mock_body() -> mir::Body<'static> {
27 let source_info = mir::SourceInfo { scope: mir::OUTERMOST_SOURCE_SCOPE, span: DUMMY_SP };
29 let mut blocks = IndexVec::new();
30 let mut block = |n, kind| {
31 let nop = mir::Statement { source_info, kind: mir::StatementKind::Nop };
33 blocks.push(mir::BasicBlockData {
34 statements: std::iter::repeat(&nop).cloned().take(n).collect(),
35 terminator: Some(mir::Terminator { source_info, kind }),
40 let dummy_place = mir::Place { local: mir::RETURN_PLACE, projection: ty::List::empty() };
42 block(4, mir::TerminatorKind::Return);
43 block(1, mir::TerminatorKind::Return);
46 mir::TerminatorKind::Call {
47 func: mir::Operand::Copy(dummy_place.clone()),
49 destination: Some((dummy_place.clone(), mir::START_BLOCK)),
54 block(3, mir::TerminatorKind::Return);
55 block(0, mir::TerminatorKind::Return);
58 mir::TerminatorKind::Call {
59 func: mir::Operand::Copy(dummy_place.clone()),
61 destination: Some((dummy_place.clone(), mir::START_BLOCK)),
67 mir::Body::new_cfg_only(blocks)
70 /// A dataflow analysis whose state is unique at every possible `SeekTarget`.
72 /// Uniqueness is achieved by having a *locally* unique effect before and after each statement and
73 /// terminator (see `effect_at_target`) while ensuring that the entry set for each block is
74 /// *globally* unique (see `mock_entry_set`).
76 /// For example, a `BasicBlock` with ID `2` and a `Call` terminator has the following state at each
77 /// location ("+x" indicates that "x" is added to the state).
79 /// | Location | Before | After |
80 /// |------------------------|-------------------|--------|
81 /// | (on_entry) | {102} ||
82 /// | Statement 0 | +0 | +1 |
83 /// | statement 1 | +2 | +3 |
84 /// | `Call` terminator | +4 | +5 |
85 /// | (on unwind) | {102,0,1,2,3,4,5} ||
86 /// | (on successful return) | +6 ||
88 /// The `102` in the block's entry set is derived from the basic block index and ensures that the
89 /// expected state is unique across all basic blocks. Remember, it is generated by
90 /// `mock_entry_sets`, not from actually running `MockAnalysis` to fixpoint.
91 struct MockAnalysis<'tcx> {
92 body: &'tcx mir::Body<'tcx>,
95 impl MockAnalysis<'tcx> {
96 const BASIC_BLOCK_OFFSET: usize = 100;
98 /// The entry set for each `BasicBlock` is the ID of that block offset by a fixed amount to
99 /// avoid colliding with the statement/terminator effects.
100 fn mock_entry_set(&self, bb: BasicBlock) -> BitSet<usize> {
101 let mut ret = BitSet::new_empty(self.bits_per_block(self.body));
102 ret.insert(Self::BASIC_BLOCK_OFFSET + bb.index());
106 fn mock_entry_sets(&self) -> IndexVec<BasicBlock, BitSet<usize>> {
107 let empty = BitSet::new_empty(self.bits_per_block(self.body));
108 let mut ret = IndexVec::from_elem(empty, &self.body.basic_blocks());
110 for (bb, _) in self.body.basic_blocks().iter_enumerated() {
111 ret[bb] = self.mock_entry_set(bb);
117 /// Returns the index that should be added to the dataflow state at the given target.
119 /// This index is only unique within a given basic block. `SeekAfter` and
120 /// `SeekAfterAssumeCallReturns` have the same effect unless `target` is a `Call` terminator.
121 fn effect_at_target(&self, target: SeekTarget) -> Option<usize> {
124 let idx = match target {
125 BlockStart(_) => return None,
127 AfterAssumeCallReturns(loc) if is_call_terminator_non_diverging(self.body, loc) => {
128 loc.statement_index * 2 + 2
131 Before(loc) => loc.statement_index * 2,
132 After(loc) | AfterAssumeCallReturns(loc) => loc.statement_index * 2 + 1,
135 assert!(idx < Self::BASIC_BLOCK_OFFSET, "Too many statements in basic block");
139 /// Returns the expected state at the given `SeekTarget`.
141 /// This is the union of index of the target basic block, the index assigned to the
142 /// target statement or terminator, and the indices of all preceding statements in the target
145 /// For example, the expected state when calling
146 /// `seek_before(Location { block: 2, statement_index: 2 })` would be `[102, 0, 1, 2, 3, 4]`.
147 fn expected_state_at_target(&self, target: SeekTarget) -> BitSet<usize> {
148 let mut ret = BitSet::new_empty(self.bits_per_block(self.body));
149 ret.insert(Self::BASIC_BLOCK_OFFSET + target.block().index());
151 if let Some(target_effect) = self.effect_at_target(target) {
152 for i in 0..=target_effect {
161 impl BottomValue for MockAnalysis<'tcx> {
162 const BOTTOM_VALUE: bool = false;
165 impl AnalysisDomain<'tcx> for MockAnalysis<'tcx> {
168 const NAME: &'static str = "mock";
170 fn bits_per_block(&self, body: &mir::Body<'tcx>) -> usize {
171 Self::BASIC_BLOCK_OFFSET + body.basic_blocks().len()
174 fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut BitSet<Self::Idx>) {
175 unimplemented!("This is never called since `MockAnalysis` is never iterated to fixpoint");
179 impl Analysis<'tcx> for MockAnalysis<'tcx> {
180 fn apply_statement_effect(
182 state: &mut BitSet<Self::Idx>,
183 _statement: &mir::Statement<'tcx>,
186 let idx = self.effect_at_target(SeekTarget::After(location)).unwrap();
187 assert!(state.insert(idx));
190 fn apply_before_statement_effect(
192 state: &mut BitSet<Self::Idx>,
193 _statement: &mir::Statement<'tcx>,
196 let idx = self.effect_at_target(SeekTarget::Before(location)).unwrap();
197 assert!(state.insert(idx));
200 fn apply_terminator_effect(
202 state: &mut BitSet<Self::Idx>,
203 _terminator: &mir::Terminator<'tcx>,
206 let idx = self.effect_at_target(SeekTarget::After(location)).unwrap();
207 assert!(state.insert(idx));
210 fn apply_before_terminator_effect(
212 state: &mut BitSet<Self::Idx>,
213 _terminator: &mir::Terminator<'tcx>,
216 let idx = self.effect_at_target(SeekTarget::Before(location)).unwrap();
217 assert!(state.insert(idx));
220 fn apply_call_return_effect(
222 state: &mut BitSet<Self::Idx>,
224 _func: &mir::Operand<'tcx>,
225 _args: &[mir::Operand<'tcx>],
226 _return_place: mir::Place<'tcx>,
228 let location = self.body.terminator_loc(block);
229 let idx = self.effect_at_target(SeekTarget::AfterAssumeCallReturns(location)).unwrap();
230 assert!(state.insert(idx));
234 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
236 BlockStart(BasicBlock),
239 AfterAssumeCallReturns(Location),
243 fn block(&self) -> BasicBlock {
247 BlockStart(block) => block,
248 Before(loc) | After(loc) | AfterAssumeCallReturns(loc) => loc.block,
252 /// An iterator over all possible `SeekTarget`s in a given block in order, starting with
255 /// This includes both `After` and `AfterAssumeCallReturns` for every `Location`.
256 fn iter_in_block(body: &mir::Body<'_>, block: BasicBlock) -> impl Iterator<Item = Self> {
257 let statements_and_terminator = (0..=body[block].statements.len())
258 .flat_map(|i| (0..3).map(move |j| (i, j)))
259 .map(move |(i, kind)| {
260 let loc = Location { block, statement_index: i };
262 0 => SeekTarget::Before(loc),
263 1 => SeekTarget::After(loc),
264 2 => SeekTarget::AfterAssumeCallReturns(loc),
269 std::iter::once(SeekTarget::BlockStart(block)).chain(statements_and_terminator)
275 let body = mock_body();
277 let analysis = MockAnalysis { body };
280 Results { entry_sets: analysis.mock_entry_sets(), analysis }.into_results_cursor(body);
282 // Sanity check: the mock call return effect is unique and actually being applied.
283 let call_terminator_loc = Location { block: BasicBlock::from_usize(2), statement_index: 2 };
284 assert!(is_call_terminator_non_diverging(body, call_terminator_loc));
286 let call_return_effect = cursor
288 .effect_at_target(SeekTarget::AfterAssumeCallReturns(call_terminator_loc))
292 cursor.analysis().effect_at_target(SeekTarget::After(call_terminator_loc)).unwrap()
295 cursor.seek_after(call_terminator_loc);
296 assert!(!cursor.get().contains(call_return_effect));
297 cursor.seek_after_assume_success(call_terminator_loc);
298 assert!(cursor.get().contains(call_return_effect));
300 let every_target = || {
303 .flat_map(|(bb, _)| SeekTarget::iter_in_block(body, bb))
306 let mut seek_to_target = |targ| {
310 BlockStart(block) => cursor.seek_to_block_start(block),
311 Before(loc) => cursor.seek_before(loc),
312 After(loc) => cursor.seek_after(loc),
313 AfterAssumeCallReturns(loc) => cursor.seek_after_assume_success(loc),
316 assert_eq!(cursor.get(), &cursor.analysis().expected_state_at_target(targ));
319 // Seek *to* every possible `SeekTarget` *from* every possible `SeekTarget`.
321 // By resetting the cursor to `from` each time it changes, we end up checking some edges twice.
322 // What we really want is an Eulerian cycle for the complete digraph over all possible
323 // `SeekTarget`s, but it's not worth spending the time to compute it.
324 for from in every_target() {
325 seek_to_target(from);
327 for to in every_target() {
329 seek_to_target(from);