• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! A framework that can express both [gen-kill] and generic dataflow problems.
2 //!
3 //! To use this framework, implement either the [`Analysis`] or the
4 //! [`GenKillAnalysis`] trait. If your transfer function can be expressed with only gen/kill
5 //! operations, prefer `GenKillAnalysis` since it will run faster while iterating to fixpoint. The
6 //! `impls` module contains several examples of gen/kill dataflow analyses.
7 //!
8 //! Create an `Engine` for your analysis using the `into_engine` method on the `Analysis` trait,
9 //! then call `iterate_to_fixpoint`. From there, you can use a `ResultsCursor` to inspect the
10 //! fixpoint solution to your dataflow problem, or implement the `ResultsVisitor` interface and use
11 //! `visit_results`. The following example uses the `ResultsCursor` approach.
12 //!
13 //! ```ignore (cross-crate-imports)
14 //! use rustc_const_eval::dataflow::Analysis; // Makes `into_engine` available.
15 //!
16 //! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
17 //!     let analysis = MyAnalysis::new()
18 //!         .into_engine(tcx, body)
19 //!         .iterate_to_fixpoint()
20 //!         .into_results_cursor(body);
21 //!
22 //!     // Print the dataflow state *after* each statement in the start block.
23 //!     for (_, statement_index) in body.block_data[START_BLOCK].statements.iter_enumerated() {
24 //!         cursor.seek_after(Location { block: START_BLOCK, statement_index });
25 //!         let state = cursor.get();
26 //!         println!("{:?}", state);
27 //!     }
28 //! }
29 //! ```
30 //!
31 //! [gen-kill]: https://en.wikipedia.org/wiki/Data-flow_analysis#Bit_vector_problems
32 
33 use std::cmp::Ordering;
34 
35 use rustc_index::bit_set::{BitSet, ChunkedBitSet, HybridBitSet};
36 use rustc_index::Idx;
37 use rustc_middle::mir::{self, BasicBlock, Location};
38 use rustc_middle::ty::TyCtxt;
39 
40 mod cursor;
41 mod direction;
42 mod engine;
43 pub mod fmt;
44 pub mod graphviz;
45 pub mod lattice;
46 mod visitor;
47 
48 pub use self::cursor::{AnalysisResults, ResultsClonedCursor, ResultsCursor, ResultsRefCursor};
49 pub use self::direction::{Backward, Direction, Forward};
50 pub use self::engine::{Engine, EntrySets, Results, ResultsCloned};
51 pub use self::lattice::{JoinSemiLattice, MeetSemiLattice};
52 pub use self::visitor::{visit_results, ResultsVisitable, ResultsVisitor};
53 
54 /// Analysis domains are all bitsets of various kinds. This trait holds
55 /// operations needed by all of them.
56 pub trait BitSetExt<T> {
domain_size(&self) -> usize57     fn domain_size(&self) -> usize;
contains(&self, elem: T) -> bool58     fn contains(&self, elem: T) -> bool;
union(&mut self, other: &HybridBitSet<T>)59     fn union(&mut self, other: &HybridBitSet<T>);
subtract(&mut self, other: &HybridBitSet<T>)60     fn subtract(&mut self, other: &HybridBitSet<T>);
61 }
62 
63 impl<T: Idx> BitSetExt<T> for BitSet<T> {
domain_size(&self) -> usize64     fn domain_size(&self) -> usize {
65         self.domain_size()
66     }
67 
contains(&self, elem: T) -> bool68     fn contains(&self, elem: T) -> bool {
69         self.contains(elem)
70     }
71 
union(&mut self, other: &HybridBitSet<T>)72     fn union(&mut self, other: &HybridBitSet<T>) {
73         self.union(other);
74     }
75 
subtract(&mut self, other: &HybridBitSet<T>)76     fn subtract(&mut self, other: &HybridBitSet<T>) {
77         self.subtract(other);
78     }
79 }
80 
81 impl<T: Idx> BitSetExt<T> for ChunkedBitSet<T> {
domain_size(&self) -> usize82     fn domain_size(&self) -> usize {
83         self.domain_size()
84     }
85 
contains(&self, elem: T) -> bool86     fn contains(&self, elem: T) -> bool {
87         self.contains(elem)
88     }
89 
union(&mut self, other: &HybridBitSet<T>)90     fn union(&mut self, other: &HybridBitSet<T>) {
91         self.union(other);
92     }
93 
subtract(&mut self, other: &HybridBitSet<T>)94     fn subtract(&mut self, other: &HybridBitSet<T>) {
95         self.subtract(other);
96     }
97 }
98 
99 /// Defines the domain of a dataflow problem.
100 ///
101 /// This trait specifies the lattice on which this analysis operates (the domain) as well as its
102 /// initial value at the entry point of each basic block.
103 pub trait AnalysisDomain<'tcx> {
104     /// The type that holds the dataflow state at any given point in the program.
105     type Domain: Clone + JoinSemiLattice;
106 
107     /// The direction of this analysis. Either `Forward` or `Backward`.
108     type Direction: Direction = Forward;
109 
110     /// A descriptive name for this analysis. Used only for debugging.
111     ///
112     /// This name should be brief and contain no spaces, periods or other characters that are not
113     /// suitable as part of a filename.
114     const NAME: &'static str;
115 
116     /// Returns the initial value of the dataflow state upon entry to each basic block.
bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain117     fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain;
118 
119     /// Mutates the initial value of the dataflow state upon entry to the `START_BLOCK`.
120     ///
121     /// For backward analyses, initial state (besides the bottom value) is not yet supported. Trying
122     /// to mutate the initial state will result in a panic.
123     //
124     // FIXME: For backward dataflow analyses, the initial state should be applied to every basic
125     // block where control flow could exit the MIR body (e.g., those terminated with `return` or
126     // `resume`). It's not obvious how to handle `yield` points in generators, however.
initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut Self::Domain)127     fn initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut Self::Domain);
128 }
129 
130 /// A dataflow problem with an arbitrarily complex transfer function.
131 ///
132 /// # Convergence
133 ///
134 /// When implementing this trait directly (not via [`GenKillAnalysis`]), it's possible to choose a
135 /// transfer function such that the analysis does not reach fixpoint. To guarantee convergence,
136 /// your transfer functions must maintain the following invariant:
137 ///
138 /// > If the dataflow state **before** some point in the program changes to be greater
139 /// than the prior state **before** that point, the dataflow state **after** that point must
140 /// also change to be greater than the prior state **after** that point.
141 ///
142 /// This invariant guarantees that the dataflow state at a given point in the program increases
143 /// monotonically until fixpoint is reached. Note that this monotonicity requirement only applies
144 /// to the same point in the program at different points in time. The dataflow state at a given
145 /// point in the program may or may not be greater than the state at any preceding point.
146 pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
147     /// Updates the current dataflow state with the effect of evaluating a statement.
apply_statement_effect( &mut self, state: &mut Self::Domain, statement: &mir::Statement<'tcx>, location: Location, )148     fn apply_statement_effect(
149         &mut self,
150         state: &mut Self::Domain,
151         statement: &mir::Statement<'tcx>,
152         location: Location,
153     );
154 
155     /// Updates the current dataflow state with an effect that occurs immediately *before* the
156     /// given statement.
157     ///
158     /// This method is useful if the consumer of the results of this analysis only needs to observe
159     /// *part* of the effect of a statement (e.g. for two-phase borrows). As a general rule,
160     /// analyses should not implement this without also implementing `apply_statement_effect`.
apply_before_statement_effect( &mut self, _state: &mut Self::Domain, _statement: &mir::Statement<'tcx>, _location: Location, )161     fn apply_before_statement_effect(
162         &mut self,
163         _state: &mut Self::Domain,
164         _statement: &mir::Statement<'tcx>,
165         _location: Location,
166     ) {
167     }
168 
169     /// Updates the current dataflow state with the effect of evaluating a terminator.
170     ///
171     /// The effect of a successful return from a `Call` terminator should **not** be accounted for
172     /// in this function. That should go in `apply_call_return_effect`. For example, in the
173     /// `InitializedPlaces` analyses, the return place for a function call is not marked as
174     /// initialized here.
apply_terminator_effect( &mut self, state: &mut Self::Domain, terminator: &mir::Terminator<'tcx>, location: Location, )175     fn apply_terminator_effect(
176         &mut self,
177         state: &mut Self::Domain,
178         terminator: &mir::Terminator<'tcx>,
179         location: Location,
180     );
181 
182     /// Updates the current dataflow state with an effect that occurs immediately *before* the
183     /// given terminator.
184     ///
185     /// This method is useful if the consumer of the results of this analysis needs only to observe
186     /// *part* of the effect of a terminator (e.g. for two-phase borrows). As a general rule,
187     /// analyses should not implement this without also implementing `apply_terminator_effect`.
apply_before_terminator_effect( &mut self, _state: &mut Self::Domain, _terminator: &mir::Terminator<'tcx>, _location: Location, )188     fn apply_before_terminator_effect(
189         &mut self,
190         _state: &mut Self::Domain,
191         _terminator: &mir::Terminator<'tcx>,
192         _location: Location,
193     ) {
194     }
195 
196     /* Edge-specific effects */
197 
198     /// Updates the current dataflow state with the effect of a successful return from a `Call`
199     /// terminator.
200     ///
201     /// This is separate from `apply_terminator_effect` to properly track state across unwind
202     /// edges.
apply_call_return_effect( &mut self, state: &mut Self::Domain, block: BasicBlock, return_places: CallReturnPlaces<'_, 'tcx>, )203     fn apply_call_return_effect(
204         &mut self,
205         state: &mut Self::Domain,
206         block: BasicBlock,
207         return_places: CallReturnPlaces<'_, 'tcx>,
208     );
209 
210     /// Updates the current dataflow state with the effect of resuming from a `Yield` terminator.
211     ///
212     /// This is similar to `apply_call_return_effect` in that it only takes place after the
213     /// generator is resumed, not when it is dropped.
214     ///
215     /// By default, no effects happen.
apply_yield_resume_effect( &mut self, _state: &mut Self::Domain, _resume_block: BasicBlock, _resume_place: mir::Place<'tcx>, )216     fn apply_yield_resume_effect(
217         &mut self,
218         _state: &mut Self::Domain,
219         _resume_block: BasicBlock,
220         _resume_place: mir::Place<'tcx>,
221     ) {
222     }
223 
224     /// Updates the current dataflow state with the effect of taking a particular branch in a
225     /// `SwitchInt` terminator.
226     ///
227     /// Unlike the other edge-specific effects, which are allowed to mutate `Self::Domain`
228     /// directly, overriders of this method must pass a callback to
229     /// `SwitchIntEdgeEffects::apply`. The callback will be run once for each outgoing edge and
230     /// will have access to the dataflow state that will be propagated along that edge.
231     ///
232     /// This interface is somewhat more complex than the other visitor-like "effect" methods.
233     /// However, it is both more ergonomic—callers don't need to recompute or cache information
234     /// about a given `SwitchInt` terminator for each one of its edges—and more efficient—the
235     /// engine doesn't need to clone the exit state for a block unless
236     /// `SwitchIntEdgeEffects::apply` is actually called.
apply_switch_int_edge_effects( &mut self, _block: BasicBlock, _discr: &mir::Operand<'tcx>, _apply_edge_effects: &mut impl SwitchIntEdgeEffects<Self::Domain>, )237     fn apply_switch_int_edge_effects(
238         &mut self,
239         _block: BasicBlock,
240         _discr: &mir::Operand<'tcx>,
241         _apply_edge_effects: &mut impl SwitchIntEdgeEffects<Self::Domain>,
242     ) {
243     }
244 
245     /* Extension methods */
246 
247     /// Creates an `Engine` to find the fixpoint for this dataflow problem.
248     ///
249     /// You shouldn't need to override this outside this module, since the combination of the
250     /// default impl and the one for all `A: GenKillAnalysis` will do the right thing.
251     /// Its purpose is to enable method chaining like so:
252     ///
253     /// ```ignore (cross-crate-imports)
254     /// let results = MyAnalysis::new(tcx, body)
255     ///     .into_engine(tcx, body, def_id)
256     ///     .iterate_to_fixpoint()
257     ///     .into_results_cursor(body);
258     /// ```
259     #[inline]
into_engine<'mir>( self, tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>, ) -> Engine<'mir, 'tcx, Self> where Self: Sized,260     fn into_engine<'mir>(
261         self,
262         tcx: TyCtxt<'tcx>,
263         body: &'mir mir::Body<'tcx>,
264     ) -> Engine<'mir, 'tcx, Self>
265     where
266         Self: Sized,
267     {
268         Engine::new_generic(tcx, body, self)
269     }
270 }
271 
272 /// Defines an `Analysis` which can be cloned for use in multiple `ResultsCursor`s or
273 /// `ResultsVisitor`s. Note this need not be a full clone, only enough of one to be used with a new
274 /// `ResultsCursor` or `ResultsVisitor`
275 pub trait CloneAnalysis {
clone_analysis(&self) -> Self276     fn clone_analysis(&self) -> Self;
277 }
278 impl<'tcx, A> CloneAnalysis for A
279 where
280     A: Analysis<'tcx> + Copy,
281 {
clone_analysis(&self) -> Self282     fn clone_analysis(&self) -> Self {
283         *self
284     }
285 }
286 
287 /// A gen/kill dataflow problem.
288 ///
289 /// Each method in this trait has a corresponding one in `Analysis`. However, these methods only
290 /// allow modification of the dataflow state via "gen" and "kill" operations. By defining transfer
291 /// functions for each statement in this way, the transfer function for an entire basic block can
292 /// be computed efficiently.
293 ///
294 /// `Analysis` is automatically implemented for all implementers of `GenKillAnalysis`.
295 pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
296     type Idx: Idx;
297 
298     /// See `Analysis::apply_statement_effect`.
statement_effect( &mut self, trans: &mut impl GenKill<Self::Idx>, statement: &mir::Statement<'tcx>, location: Location, )299     fn statement_effect(
300         &mut self,
301         trans: &mut impl GenKill<Self::Idx>,
302         statement: &mir::Statement<'tcx>,
303         location: Location,
304     );
305 
306     /// See `Analysis::apply_before_statement_effect`.
before_statement_effect( &mut self, _trans: &mut impl GenKill<Self::Idx>, _statement: &mir::Statement<'tcx>, _location: Location, )307     fn before_statement_effect(
308         &mut self,
309         _trans: &mut impl GenKill<Self::Idx>,
310         _statement: &mir::Statement<'tcx>,
311         _location: Location,
312     ) {
313     }
314 
315     /// See `Analysis::apply_terminator_effect`.
terminator_effect( &mut self, trans: &mut impl GenKill<Self::Idx>, terminator: &mir::Terminator<'tcx>, location: Location, )316     fn terminator_effect(
317         &mut self,
318         trans: &mut impl GenKill<Self::Idx>,
319         terminator: &mir::Terminator<'tcx>,
320         location: Location,
321     );
322 
323     /// See `Analysis::apply_before_terminator_effect`.
before_terminator_effect( &mut self, _trans: &mut impl GenKill<Self::Idx>, _terminator: &mir::Terminator<'tcx>, _location: Location, )324     fn before_terminator_effect(
325         &mut self,
326         _trans: &mut impl GenKill<Self::Idx>,
327         _terminator: &mir::Terminator<'tcx>,
328         _location: Location,
329     ) {
330     }
331 
332     /* Edge-specific effects */
333 
334     /// See `Analysis::apply_call_return_effect`.
call_return_effect( &mut self, trans: &mut impl GenKill<Self::Idx>, block: BasicBlock, return_places: CallReturnPlaces<'_, 'tcx>, )335     fn call_return_effect(
336         &mut self,
337         trans: &mut impl GenKill<Self::Idx>,
338         block: BasicBlock,
339         return_places: CallReturnPlaces<'_, 'tcx>,
340     );
341 
342     /// See `Analysis::apply_yield_resume_effect`.
yield_resume_effect( &mut self, _trans: &mut impl GenKill<Self::Idx>, _resume_block: BasicBlock, _resume_place: mir::Place<'tcx>, )343     fn yield_resume_effect(
344         &mut self,
345         _trans: &mut impl GenKill<Self::Idx>,
346         _resume_block: BasicBlock,
347         _resume_place: mir::Place<'tcx>,
348     ) {
349     }
350 
351     /// See `Analysis::apply_switch_int_edge_effects`.
switch_int_edge_effects<G: GenKill<Self::Idx>>( &mut self, _block: BasicBlock, _discr: &mir::Operand<'tcx>, _edge_effects: &mut impl SwitchIntEdgeEffects<G>, )352     fn switch_int_edge_effects<G: GenKill<Self::Idx>>(
353         &mut self,
354         _block: BasicBlock,
355         _discr: &mir::Operand<'tcx>,
356         _edge_effects: &mut impl SwitchIntEdgeEffects<G>,
357     ) {
358     }
359 }
360 
361 impl<'tcx, A> Analysis<'tcx> for A
362 where
363     A: GenKillAnalysis<'tcx>,
364     A::Domain: GenKill<A::Idx> + BitSetExt<A::Idx>,
365 {
apply_statement_effect( &mut self, state: &mut A::Domain, statement: &mir::Statement<'tcx>, location: Location, )366     fn apply_statement_effect(
367         &mut self,
368         state: &mut A::Domain,
369         statement: &mir::Statement<'tcx>,
370         location: Location,
371     ) {
372         self.statement_effect(state, statement, location);
373     }
374 
apply_before_statement_effect( &mut self, state: &mut A::Domain, statement: &mir::Statement<'tcx>, location: Location, )375     fn apply_before_statement_effect(
376         &mut self,
377         state: &mut A::Domain,
378         statement: &mir::Statement<'tcx>,
379         location: Location,
380     ) {
381         self.before_statement_effect(state, statement, location);
382     }
383 
apply_terminator_effect( &mut self, state: &mut A::Domain, terminator: &mir::Terminator<'tcx>, location: Location, )384     fn apply_terminator_effect(
385         &mut self,
386         state: &mut A::Domain,
387         terminator: &mir::Terminator<'tcx>,
388         location: Location,
389     ) {
390         self.terminator_effect(state, terminator, location);
391     }
392 
apply_before_terminator_effect( &mut self, state: &mut A::Domain, terminator: &mir::Terminator<'tcx>, location: Location, )393     fn apply_before_terminator_effect(
394         &mut self,
395         state: &mut A::Domain,
396         terminator: &mir::Terminator<'tcx>,
397         location: Location,
398     ) {
399         self.before_terminator_effect(state, terminator, location);
400     }
401 
402     /* Edge-specific effects */
403 
apply_call_return_effect( &mut self, state: &mut A::Domain, block: BasicBlock, return_places: CallReturnPlaces<'_, 'tcx>, )404     fn apply_call_return_effect(
405         &mut self,
406         state: &mut A::Domain,
407         block: BasicBlock,
408         return_places: CallReturnPlaces<'_, 'tcx>,
409     ) {
410         self.call_return_effect(state, block, return_places);
411     }
412 
apply_yield_resume_effect( &mut self, state: &mut A::Domain, resume_block: BasicBlock, resume_place: mir::Place<'tcx>, )413     fn apply_yield_resume_effect(
414         &mut self,
415         state: &mut A::Domain,
416         resume_block: BasicBlock,
417         resume_place: mir::Place<'tcx>,
418     ) {
419         self.yield_resume_effect(state, resume_block, resume_place);
420     }
421 
apply_switch_int_edge_effects( &mut self, block: BasicBlock, discr: &mir::Operand<'tcx>, edge_effects: &mut impl SwitchIntEdgeEffects<A::Domain>, )422     fn apply_switch_int_edge_effects(
423         &mut self,
424         block: BasicBlock,
425         discr: &mir::Operand<'tcx>,
426         edge_effects: &mut impl SwitchIntEdgeEffects<A::Domain>,
427     ) {
428         self.switch_int_edge_effects(block, discr, edge_effects);
429     }
430 
431     /* Extension methods */
432     #[inline]
into_engine<'mir>( self, tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>, ) -> Engine<'mir, 'tcx, Self> where Self: Sized,433     fn into_engine<'mir>(
434         self,
435         tcx: TyCtxt<'tcx>,
436         body: &'mir mir::Body<'tcx>,
437     ) -> Engine<'mir, 'tcx, Self>
438     where
439         Self: Sized,
440     {
441         Engine::new_gen_kill(tcx, body, self)
442     }
443 }
444 
445 /// The legal operations for a transfer function in a gen/kill problem.
446 ///
447 /// This abstraction exists because there are two different contexts in which we call the methods in
448 /// `GenKillAnalysis`. Sometimes we need to store a single transfer function that can be efficiently
449 /// applied multiple times, such as when computing the cumulative transfer function for each block.
450 /// These cases require a `GenKillSet`, which in turn requires two `BitSet`s of storage. Oftentimes,
451 /// however, we only need to apply an effect once. In *these* cases, it is more efficient to pass the
452 /// `BitSet` representing the state vector directly into the `*_effect` methods as opposed to
453 /// building up a `GenKillSet` and then throwing it away.
454 pub trait GenKill<T> {
455     /// Inserts `elem` into the state vector.
gen(&mut self, elem: T)456     fn gen(&mut self, elem: T);
457 
458     /// Removes `elem` from the state vector.
kill(&mut self, elem: T)459     fn kill(&mut self, elem: T);
460 
461     /// Calls `gen` for each element in `elems`.
gen_all(&mut self, elems: impl IntoIterator<Item = T>)462     fn gen_all(&mut self, elems: impl IntoIterator<Item = T>) {
463         for elem in elems {
464             self.gen(elem);
465         }
466     }
467 
468     /// Calls `kill` for each element in `elems`.
kill_all(&mut self, elems: impl IntoIterator<Item = T>)469     fn kill_all(&mut self, elems: impl IntoIterator<Item = T>) {
470         for elem in elems {
471             self.kill(elem);
472         }
473     }
474 }
475 
476 /// Stores a transfer function for a gen/kill problem.
477 ///
478 /// Calling `gen`/`kill` on a `GenKillSet` will "build up" a transfer function so that it can be
479 /// applied multiple times efficiently. When there are multiple calls to `gen` and/or `kill` for
480 /// the same element, the most recent one takes precedence.
481 #[derive(Clone)]
482 pub struct GenKillSet<T> {
483     gen: HybridBitSet<T>,
484     kill: HybridBitSet<T>,
485 }
486 
487 impl<T: Idx> GenKillSet<T> {
488     /// Creates a new transfer function that will leave the dataflow state unchanged.
identity(universe: usize) -> Self489     pub fn identity(universe: usize) -> Self {
490         GenKillSet {
491             gen: HybridBitSet::new_empty(universe),
492             kill: HybridBitSet::new_empty(universe),
493         }
494     }
495 
apply(&self, state: &mut impl BitSetExt<T>)496     pub fn apply(&self, state: &mut impl BitSetExt<T>) {
497         state.union(&self.gen);
498         state.subtract(&self.kill);
499     }
500 }
501 
502 impl<T: Idx> GenKill<T> for GenKillSet<T> {
gen(&mut self, elem: T)503     fn gen(&mut self, elem: T) {
504         self.gen.insert(elem);
505         self.kill.remove(elem);
506     }
507 
kill(&mut self, elem: T)508     fn kill(&mut self, elem: T) {
509         self.kill.insert(elem);
510         self.gen.remove(elem);
511     }
512 }
513 
514 impl<T: Idx> GenKill<T> for BitSet<T> {
gen(&mut self, elem: T)515     fn gen(&mut self, elem: T) {
516         self.insert(elem);
517     }
518 
kill(&mut self, elem: T)519     fn kill(&mut self, elem: T) {
520         self.remove(elem);
521     }
522 }
523 
524 impl<T: Idx> GenKill<T> for ChunkedBitSet<T> {
gen(&mut self, elem: T)525     fn gen(&mut self, elem: T) {
526         self.insert(elem);
527     }
528 
kill(&mut self, elem: T)529     fn kill(&mut self, elem: T) {
530         self.remove(elem);
531     }
532 }
533 
534 impl<T: Idx> GenKill<T> for lattice::Dual<BitSet<T>> {
gen(&mut self, elem: T)535     fn gen(&mut self, elem: T) {
536         self.0.insert(elem);
537     }
538 
kill(&mut self, elem: T)539     fn kill(&mut self, elem: T) {
540         self.0.remove(elem);
541     }
542 }
543 
544 // NOTE: DO NOT CHANGE VARIANT ORDER. The derived `Ord` impls rely on the current order.
545 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
546 pub enum Effect {
547     /// The "before" effect (e.g., `apply_before_statement_effect`) for a statement (or
548     /// terminator).
549     Before,
550 
551     /// The "primary" effect (e.g., `apply_statement_effect`) for a statement (or terminator).
552     Primary,
553 }
554 
555 impl Effect {
at_index(self, statement_index: usize) -> EffectIndex556     pub const fn at_index(self, statement_index: usize) -> EffectIndex {
557         EffectIndex { effect: self, statement_index }
558     }
559 }
560 
561 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
562 pub struct EffectIndex {
563     statement_index: usize,
564     effect: Effect,
565 }
566 
567 impl EffectIndex {
next_in_forward_order(self) -> Self568     fn next_in_forward_order(self) -> Self {
569         match self.effect {
570             Effect::Before => Effect::Primary.at_index(self.statement_index),
571             Effect::Primary => Effect::Before.at_index(self.statement_index + 1),
572         }
573     }
574 
next_in_backward_order(self) -> Self575     fn next_in_backward_order(self) -> Self {
576         match self.effect {
577             Effect::Before => Effect::Primary.at_index(self.statement_index),
578             Effect::Primary => Effect::Before.at_index(self.statement_index - 1),
579         }
580     }
581 
582     /// Returns `true` if the effect at `self` should be applied earlier than the effect at `other`
583     /// in forward order.
precedes_in_forward_order(self, other: Self) -> bool584     fn precedes_in_forward_order(self, other: Self) -> bool {
585         let ord = self
586             .statement_index
587             .cmp(&other.statement_index)
588             .then_with(|| self.effect.cmp(&other.effect));
589         ord == Ordering::Less
590     }
591 
592     /// Returns `true` if the effect at `self` should be applied earlier than the effect at `other`
593     /// in backward order.
precedes_in_backward_order(self, other: Self) -> bool594     fn precedes_in_backward_order(self, other: Self) -> bool {
595         let ord = other
596             .statement_index
597             .cmp(&self.statement_index)
598             .then_with(|| self.effect.cmp(&other.effect));
599         ord == Ordering::Less
600     }
601 }
602 
603 pub struct SwitchIntTarget {
604     pub value: Option<u128>,
605     pub target: BasicBlock,
606 }
607 
608 /// A type that records the edge-specific effects for a `SwitchInt` terminator.
609 pub trait SwitchIntEdgeEffects<D> {
610     /// Calls `apply_edge_effect` for each outgoing edge from a `SwitchInt` terminator and
611     /// records the results.
apply(&mut self, apply_edge_effect: impl FnMut(&mut D, SwitchIntTarget))612     fn apply(&mut self, apply_edge_effect: impl FnMut(&mut D, SwitchIntTarget));
613 }
614 
615 /// List of places that are written to after a successful (non-unwind) return
616 /// from a `Call` or `InlineAsm`.
617 pub enum CallReturnPlaces<'a, 'tcx> {
618     Call(mir::Place<'tcx>),
619     InlineAsm(&'a [mir::InlineAsmOperand<'tcx>]),
620 }
621 
622 impl<'tcx> CallReturnPlaces<'_, 'tcx> {
for_each(&self, mut f: impl FnMut(mir::Place<'tcx>))623     pub fn for_each(&self, mut f: impl FnMut(mir::Place<'tcx>)) {
624         match *self {
625             Self::Call(place) => f(place),
626             Self::InlineAsm(operands) => {
627                 for op in operands {
628                     match *op {
629                         mir::InlineAsmOperand::Out { place: Some(place), .. }
630                         | mir::InlineAsmOperand::InOut { out_place: Some(place), .. } => f(place),
631                         _ => {}
632                     }
633                 }
634             }
635         }
636     }
637 }
638 
639 #[cfg(test)]
640 mod tests;
641