@@ -39,7 +39,7 @@ use std::path::{Path, PathBuf};
39
39
use crate :: transform:: MirSource ;
40
40
use crate :: util:: pretty:: { dump_enabled, write_basic_block, write_mir_intro} ;
41
41
42
- pub type LiveVarSet < V > = BitSet < V > ;
42
+ pub type LiveVarSet = BitSet < Local > ;
43
43
44
44
/// This gives the result of the liveness analysis at the boundary of
45
45
/// basic blocks.
@@ -48,66 +48,27 @@ pub type LiveVarSet<V> = BitSet<V>;
48
48
/// liveness for. This is often `Local`, in which case we computed
49
49
/// liveness for all variables -- but it can also be some other type,
50
50
/// which indicates a subset of the variables within the graph.
51
- pub struct LivenessResult < V : Idx > {
51
+ pub struct LivenessResult {
52
52
/// Live variables on exit to each basic block. This is equal to
53
53
/// 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 > ,
93
55
}
94
56
95
57
/// Computes which local variables are live within the given function
96
58
/// `mir`. The liveness mode `mode` determines what sorts of uses are
97
59
/// 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 > (
99
61
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 ( ) ;
103
64
104
- let def_use: IndexVec < _ , DefsUses < V > > = mir
65
+ let def_use: IndexVec < _ , DefsUses > = mir
105
66
. basic_blocks ( )
106
67
. iter ( )
107
- . map ( |b| block ( map , b, num_live_vars) )
68
+ . map ( |b| block ( b, num_live_vars) )
108
69
. collect ( ) ;
109
70
110
- let mut outs: IndexVec < _ , LiveVarSet < V > > = mir
71
+ let mut outs: IndexVec < _ , LiveVarSet > = mir
111
72
. basic_blocks ( )
112
73
. indices ( )
113
74
. map ( |_| LiveVarSet :: new_empty ( num_live_vars) )
@@ -211,27 +172,23 @@ pub fn categorize<'tcx>(context: PlaceContext<'tcx>) -> Option<DefUse> {
211
172
}
212
173
}
213
174
214
- struct DefsUsesVisitor < ' lv , V , M >
215
- where
216
- V : Idx ,
217
- M : LiveVariableMap < LiveVar = V > + ' lv ,
175
+ struct DefsUsesVisitor
218
176
{
219
- map : & ' lv M ,
220
- defs_uses : DefsUses < V > ,
177
+ defs_uses : DefsUses ,
221
178
}
222
179
223
180
#[ 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 ,
227
184
}
228
185
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 {
231
188
bits. subtract ( & self . defs ) | bits. union ( & self . uses )
232
189
}
233
190
234
- fn add_def ( & mut self , index : V ) {
191
+ fn add_def ( & mut self , index : Local ) {
235
192
// If it was used already in the block, remove that use
236
193
// now that we found a definition.
237
194
//
@@ -245,7 +202,7 @@ impl<V: Idx> DefsUses<V> {
245
202
self . defs . insert ( index) ;
246
203
}
247
204
248
- fn add_use ( & mut self , index : V ) {
205
+ fn add_use ( & mut self , index : Local ) {
249
206
// Inverse of above.
250
207
//
251
208
// Example:
@@ -261,29 +218,22 @@ impl<V: Idx> DefsUses<V> {
261
218
}
262
219
}
263
220
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
268
222
{
269
223
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
+ _ => ( ) ,
276
228
}
277
229
}
278
230
}
279
231
280
- fn block < ' tcx , V : Idx > (
281
- map : & impl LiveVariableMap < LiveVar = V > ,
232
+ fn block < ' tcx > (
282
233
b : & BasicBlockData < ' tcx > ,
283
234
locals : usize ,
284
- ) -> DefsUses < V > {
235
+ ) -> DefsUses {
285
236
let mut visitor = DefsUsesVisitor {
286
- map,
287
237
defs_uses : DefsUses {
288
238
defs : LiveVarSet :: new_empty ( locals) ,
289
239
uses : LiveVarSet :: new_empty ( locals) ,
@@ -305,13 +255,12 @@ fn block<'tcx, V: Idx>(
305
255
visitor. defs_uses
306
256
}
307
257
308
- pub fn dump_mir < ' a , ' tcx , V : Idx > (
258
+ pub fn dump_mir < ' a , ' tcx > (
309
259
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
310
260
pass_name : & str ,
311
261
source : MirSource < ' tcx > ,
312
262
mir : & Mir < ' tcx > ,
313
- map : & impl LiveVariableMap < LiveVar = V > ,
314
- result : & LivenessResult < V > ,
263
+ result : & LivenessResult ,
315
264
) {
316
265
if !dump_enabled ( tcx, pass_name, source) {
317
266
return ;
@@ -320,17 +269,16 @@ pub fn dump_mir<'a, 'tcx, V: Idx>(
320
269
// see notes on #41697 below
321
270
tcx. item_path_str ( source. def_id ( ) )
322
271
} ) ;
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) ;
324
273
}
325
274
326
- fn dump_matched_mir_node < ' a , ' tcx , V : Idx > (
275
+ fn dump_matched_mir_node < ' a , ' tcx > (
327
276
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
328
277
pass_name : & str ,
329
278
node_path : & str ,
330
279
source : MirSource < ' tcx > ,
331
280
mir : & Mir < ' tcx > ,
332
- map : & dyn LiveVariableMap < LiveVar = V > ,
333
- result : & LivenessResult < V > ,
281
+ result : & LivenessResult ,
334
282
) {
335
283
let mut file_path = PathBuf :: new ( ) ;
336
284
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>(
342
290
writeln ! ( file, "// source = {:?}" , source) ?;
343
291
writeln ! ( file, "// pass_name = {}" , pass_name) ?;
344
292
writeln ! ( file, "" ) ?;
345
- write_mir_fn ( tcx, source, mir, map , & mut file, result) ?;
293
+ write_mir_fn ( tcx, source, mir, & mut file, result) ?;
346
294
Ok ( ( ) )
347
295
} ) ;
348
296
}
349
297
350
- pub fn write_mir_fn < ' a , ' tcx , V : Idx > (
298
+ pub fn write_mir_fn < ' a , ' tcx > (
351
299
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
352
300
src : MirSource < ' tcx > ,
353
301
mir : & Mir < ' tcx > ,
354
- map : & dyn LiveVariableMap < LiveVar = V > ,
355
302
w : & mut dyn Write ,
356
- result : & LivenessResult < V > ,
303
+ result : & LivenessResult ,
357
304
) -> io:: Result < ( ) > {
358
305
write_mir_intro ( tcx, src, mir, w) ?;
359
306
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 > | {
361
308
let live: Vec < String > = result[ block]
362
309
. iter ( )
363
- . map ( |v| map. from_live_var ( v) )
364
310
. map ( |local| format ! ( "{:?}" , local) )
365
311
. collect ( ) ;
366
312
writeln ! ( w, "{} {{{}}}" , prefix, live. join( ", " ) )
0 commit comments