@@ -39,7 +39,7 @@ use std::path::{Path, PathBuf};
3939use crate :: transform:: MirSource ;
4040use crate :: util:: pretty:: { dump_enabled, write_basic_block, write_mir_intro} ;
4141
42- pub type LiveVarSet < V > = BitSet < V > ;
42+ pub type LiveVarSet = BitSet < Local > ;
4343
4444/// This gives the result of the liveness analysis at the boundary of
4545/// basic blocks.
@@ -48,66 +48,27 @@ pub type LiveVarSet<V> = BitSet<V>;
4848/// liveness for. This is often `Local`, in which case we computed
4949/// liveness for all variables -- but it can also be some other type,
5050/// which indicates a subset of the variables within the graph.
51- pub struct LivenessResult < V : Idx > {
51+ pub struct LivenessResult {
5252 /// Live variables on exit to each basic block. This is equal to
5353 /// the union of the `ins` for each successor.
54- pub outs : IndexVec < BasicBlock , LiveVarSet < V > > ,
55- }
56-
57- /// Defines the mapping to/from the MIR local variables (`Local`) to
58- /// the "live variable indices" we are using in a particular
59- /// computation.
60- pub trait LiveVariableMap {
61- type LiveVar ;
62-
63- fn from_local ( & self , local : Local ) -> Option < Self :: LiveVar > ;
64- fn from_live_var ( & self , local : Self :: LiveVar ) -> Local ;
65- fn num_variables ( & self ) -> usize ;
66- }
67-
68- #[ derive( Debug ) ]
69- pub struct IdentityMap < ' a , ' tcx : ' a > {
70- mir : & ' a Mir < ' tcx > ,
71- }
72-
73- impl < ' a , ' tcx > IdentityMap < ' a , ' tcx > {
74- pub fn new ( mir : & ' a Mir < ' tcx > ) -> Self {
75- Self { mir }
76- }
77- }
78-
79- impl < ' a , ' tcx > LiveVariableMap for IdentityMap < ' a , ' tcx > {
80- type LiveVar = Local ;
81-
82- fn from_local ( & self , local : Local ) -> Option < Self :: LiveVar > {
83- Some ( local)
84- }
85-
86- fn from_live_var ( & self , local : Self :: LiveVar ) -> Local {
87- local
88- }
89-
90- fn num_variables ( & self ) -> usize {
91- self . mir . local_decls . len ( )
92- }
54+ pub outs : IndexVec < BasicBlock , LiveVarSet > ,
9355}
9456
9557/// Computes which local variables are live within the given function
9658/// `mir`. The liveness mode `mode` determines what sorts of uses are
9759/// considered to make a variable live (e.g., do drops count?).
98- pub fn liveness_of_locals < ' tcx , V : Idx > (
60+ pub fn liveness_of_locals < ' tcx > (
9961 mir : & Mir < ' tcx > ,
100- map : & impl LiveVariableMap < LiveVar = V > ,
101- ) -> LivenessResult < V > {
102- let num_live_vars = map. num_variables ( ) ;
62+ ) -> LivenessResult {
63+ let num_live_vars = mir. local_decls . len ( ) ;
10364
104- let def_use: IndexVec < _ , DefsUses < V > > = mir
65+ let def_use: IndexVec < _ , DefsUses > = mir
10566 . basic_blocks ( )
10667 . iter ( )
107- . map ( |b| block ( map , b, num_live_vars) )
68+ . map ( |b| block ( b, num_live_vars) )
10869 . collect ( ) ;
10970
110- let mut outs: IndexVec < _ , LiveVarSet < V > > = mir
71+ let mut outs: IndexVec < _ , LiveVarSet > = mir
11172 . basic_blocks ( )
11273 . indices ( )
11374 . map ( |_| LiveVarSet :: new_empty ( num_live_vars) )
@@ -211,27 +172,23 @@ pub fn categorize<'tcx>(context: PlaceContext<'tcx>) -> Option<DefUse> {
211172 }
212173}
213174
214- struct DefsUsesVisitor < ' lv , V , M >
215- where
216- V : Idx ,
217- M : LiveVariableMap < LiveVar = V > + ' lv ,
175+ struct DefsUsesVisitor
218176{
219- map : & ' lv M ,
220- defs_uses : DefsUses < V > ,
177+ defs_uses : DefsUses ,
221178}
222179
223180#[ derive( Eq , PartialEq , Clone ) ]
224- struct DefsUses < V : Idx > {
225- defs : LiveVarSet < V > ,
226- uses : LiveVarSet < V > ,
181+ struct DefsUses {
182+ defs : LiveVarSet ,
183+ uses : LiveVarSet ,
227184}
228185
229- impl < V : Idx > DefsUses < V > {
230- fn apply ( & self , bits : & mut LiveVarSet < V > ) -> bool {
186+ impl DefsUses {
187+ fn apply ( & self , bits : & mut LiveVarSet ) -> bool {
231188 bits. subtract ( & self . defs ) | bits. union ( & self . uses )
232189 }
233190
234- fn add_def ( & mut self , index : V ) {
191+ fn add_def ( & mut self , index : Local ) {
235192 // If it was used already in the block, remove that use
236193 // now that we found a definition.
237194 //
@@ -245,7 +202,7 @@ impl<V: Idx> DefsUses<V> {
245202 self . defs . insert ( index) ;
246203 }
247204
248- fn add_use ( & mut self , index : V ) {
205+ fn add_use ( & mut self , index : Local ) {
249206 // Inverse of above.
250207 //
251208 // Example:
@@ -261,29 +218,22 @@ impl<V: Idx> DefsUses<V> {
261218 }
262219}
263220
264- impl < ' tcx , ' lv , V , M > Visitor < ' tcx > for DefsUsesVisitor < ' lv , V , M >
265- where
266- V : Idx ,
267- M : LiveVariableMap < LiveVar = V > ,
221+ impl < ' tcx > Visitor < ' tcx > for DefsUsesVisitor
268222{
269223 fn visit_local ( & mut self , & local: & Local , context : PlaceContext < ' tcx > , _: Location ) {
270- if let Some ( v_index) = self . map . from_local ( local) {
271- match categorize ( context) {
272- Some ( DefUse :: Def ) => self . defs_uses . add_def ( v_index) ,
273- Some ( DefUse :: Use ) | Some ( DefUse :: Drop ) => self . defs_uses . add_use ( v_index) ,
274- _ => ( ) ,
275- }
224+ match categorize ( context) {
225+ Some ( DefUse :: Def ) => self . defs_uses . add_def ( local) ,
226+ Some ( DefUse :: Use ) | Some ( DefUse :: Drop ) => self . defs_uses . add_use ( local) ,
227+ _ => ( ) ,
276228 }
277229 }
278230}
279231
280- fn block < ' tcx , V : Idx > (
281- map : & impl LiveVariableMap < LiveVar = V > ,
232+ fn block < ' tcx > (
282233 b : & BasicBlockData < ' tcx > ,
283234 locals : usize ,
284- ) -> DefsUses < V > {
235+ ) -> DefsUses {
285236 let mut visitor = DefsUsesVisitor {
286- map,
287237 defs_uses : DefsUses {
288238 defs : LiveVarSet :: new_empty ( locals) ,
289239 uses : LiveVarSet :: new_empty ( locals) ,
@@ -305,13 +255,12 @@ fn block<'tcx, V: Idx>(
305255 visitor. defs_uses
306256}
307257
308- pub fn dump_mir < ' a , ' tcx , V : Idx > (
258+ pub fn dump_mir < ' a , ' tcx > (
309259 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
310260 pass_name : & str ,
311261 source : MirSource < ' tcx > ,
312262 mir : & Mir < ' tcx > ,
313- map : & impl LiveVariableMap < LiveVar = V > ,
314- result : & LivenessResult < V > ,
263+ result : & LivenessResult ,
315264) {
316265 if !dump_enabled ( tcx, pass_name, source) {
317266 return ;
@@ -320,17 +269,16 @@ pub fn dump_mir<'a, 'tcx, V: Idx>(
320269 // see notes on #41697 below
321270 tcx. item_path_str ( source. def_id ( ) )
322271 } ) ;
323- dump_matched_mir_node ( tcx, pass_name, & node_path, source, mir, map , result) ;
272+ dump_matched_mir_node ( tcx, pass_name, & node_path, source, mir, result) ;
324273}
325274
326- fn dump_matched_mir_node < ' a , ' tcx , V : Idx > (
275+ fn dump_matched_mir_node < ' a , ' tcx > (
327276 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
328277 pass_name : & str ,
329278 node_path : & str ,
330279 source : MirSource < ' tcx > ,
331280 mir : & Mir < ' tcx > ,
332- map : & dyn LiveVariableMap < LiveVar = V > ,
333- result : & LivenessResult < V > ,
281+ result : & LivenessResult ,
334282) {
335283 let mut file_path = PathBuf :: new ( ) ;
336284 file_path. push ( Path :: new ( & tcx. sess . opts . debugging_opts . dump_mir_dir ) ) ;
@@ -342,25 +290,23 @@ fn dump_matched_mir_node<'a, 'tcx, V: Idx>(
342290 writeln ! ( file, "// source = {:?}" , source) ?;
343291 writeln ! ( file, "// pass_name = {}" , pass_name) ?;
344292 writeln ! ( file, "" ) ?;
345- write_mir_fn ( tcx, source, mir, map , & mut file, result) ?;
293+ write_mir_fn ( tcx, source, mir, & mut file, result) ?;
346294 Ok ( ( ) )
347295 } ) ;
348296}
349297
350- pub fn write_mir_fn < ' a , ' tcx , V : Idx > (
298+ pub fn write_mir_fn < ' a , ' tcx > (
351299 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
352300 src : MirSource < ' tcx > ,
353301 mir : & Mir < ' tcx > ,
354- map : & dyn LiveVariableMap < LiveVar = V > ,
355302 w : & mut dyn Write ,
356- result : & LivenessResult < V > ,
303+ result : & LivenessResult ,
357304) -> io:: Result < ( ) > {
358305 write_mir_intro ( tcx, src, mir, w) ?;
359306 for block in mir. basic_blocks ( ) . indices ( ) {
360- let print = |w : & mut dyn Write , prefix, result : & IndexVec < BasicBlock , LiveVarSet < V > > | {
307+ let print = |w : & mut dyn Write , prefix, result : & IndexVec < BasicBlock , LiveVarSet > | {
361308 let live: Vec < String > = result[ block]
362309 . iter ( )
363- . map ( |v| map. from_live_var ( v) )
364310 . map ( |local| format ! ( "{:?}" , local) )
365311 . collect ( ) ;
366312 writeln ! ( w, "{} {{{}}}" , prefix, live. join( ", " ) )
0 commit comments