@@ -778,9 +778,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BorrowDependencies<'a, 'tcx> {
778
778
Rvalue :: Ref ( _, borrow_kind, borrowed_place) => {
779
779
let mutbl = match borrow_kind {
780
780
BorrowKind :: Mut { .. } => Mutability :: Mut ,
781
- BorrowKind :: Shallow | BorrowKind :: Shared | BorrowKind :: Unique => {
782
- Mutability :: Not
783
- }
781
+ BorrowKind :: Shallow | BorrowKind :: Shared => Mutability :: Not ,
784
782
} ;
785
783
786
784
self . handle_rvalue_ref_or_ptr ( borrowed_place, mutbl)
@@ -923,8 +921,8 @@ where
923
921
MaybeBorrowedLocals ,
924
922
& ' a Results < ' tcx , MaybeBorrowedLocals > ,
925
923
> ,
924
+ dep_graph : BorrowDepGraph ,
926
925
) -> Self {
927
- let dep_graph = & borrows_analysis_results. analysis . borrow_deps . dep_graph ;
928
926
let borrowed_local_to_locals_to_keep_alive = Self :: get_locals_to_keep_alive_map ( dep_graph) ;
929
927
Self {
930
928
borrows_analysis_results,
@@ -938,14 +936,14 @@ where
938
936
/// Uses the dependency graph to find all locals that we need to keep live for a given
939
937
/// `Node` (or more specically the `Local` corresponding to that `Node`).
940
938
#[ instrument( skip( dep_graph) , level = "debug" ) ]
941
- fn get_locals_to_keep_alive_map < ' b > (
942
- dep_graph : & ' b BorrowDepGraph ,
939
+ fn get_locals_to_keep_alive_map (
940
+ dep_graph : BorrowDepGraph ,
943
941
) -> FxHashMap < Local , FxHashSet < Local > > {
944
942
let mut borrows_to_locals: FxHashMap < Local , FxHashSet < Local > > = Default :: default ( ) ;
945
943
let mut memoization_map: FxHashMap < NodeIndex , FxHashSet < Local > > = Default :: default ( ) ;
946
944
947
945
// create SCCs for dependency graph and map each local to its SCC.
948
- let sccs: Sccs < NodeIndex , NodeIndex > = Sccs :: new ( dep_graph) ;
946
+ let sccs: Sccs < NodeIndex , NodeIndex > = Sccs :: new ( & dep_graph) ;
949
947
950
948
// Contains the Locals to keep alive for each scc.
951
949
let mut scc_to_locals_to_keep_alive: FxHashMap < NodeIndex , FxHashSet < Local > > =
@@ -1096,11 +1094,28 @@ pub fn get_borrowed_locals_results<'a, 'mir, 'tcx>(
1096
1094
}
1097
1095
}
1098
1096
1099
- let live_borrows = LiveBorrows :: new ( body, tcx, borrow_deps) ;
1100
- let results =
1097
+ let reborrows_map = borrow_deps. reborrows_map ;
1098
+ let local_to_nodekind = borrow_deps
1099
+ . dep_graph
1100
+ . all_nodes ( )
1101
+ . iter ( )
1102
+ . map ( |node| {
1103
+ let nodekind = node. data ;
1104
+ let local = nodekind. get_local ( ) ;
1105
+
1106
+ ( local, nodekind)
1107
+ } )
1108
+ . collect :: < FxHashMap < Local , NodeKind > > ( ) ;
1109
+
1110
+ let live_borrows = LiveBorrows :: new ( body, tcx, local_to_nodekind, reborrows_map) ;
1111
+ let live_borrows_results =
1101
1112
live_borrows. into_engine ( tcx, body) . pass_name ( "borrowed_locals" ) . iterate_to_fixpoint ( ) ;
1102
1113
1103
- BorrowedLocalsResults :: new ( results, maybe_borrowed_locals_cursor)
1114
+ BorrowedLocalsResults :: new (
1115
+ live_borrows_results,
1116
+ maybe_borrowed_locals_cursor,
1117
+ borrow_deps. dep_graph ,
1118
+ )
1104
1119
}
1105
1120
1106
1121
/// The `ResultsCursor` equivalent for the borrowed locals analysis. Since this analysis doesn't
@@ -1178,19 +1193,22 @@ impl<'a, 'mir, 'tcx> BorrowedLocalsResultsCursor<'a, 'mir, 'tcx> {
1178
1193
/// Performs a liveness analysis for borrows and raw pointers. This analysis also tracks `Local`s
1179
1194
/// corresponding to `Node`s of kind `NodeKind::LocalWithRefs`, as these could potentially refer to
1180
1195
/// or include references, pointers or exposed pointers.
1196
+ #[ derive( Clone ) ]
1181
1197
pub struct LiveBorrows < ' mir , ' tcx > {
1182
1198
body : & ' mir Body < ' tcx > ,
1183
1199
tcx : TyCtxt < ' tcx > ,
1184
- borrow_deps : BorrowDependencies < ' mir , ' tcx > ,
1200
+ local_to_nodekind : FxHashMap < Local , NodeKind > ,
1201
+ reborrows_map : FxHashMap < Local , Local > ,
1185
1202
}
1186
1203
1187
1204
impl < ' mir , ' tcx > LiveBorrows < ' mir , ' tcx > {
1188
1205
fn new (
1189
1206
body : & ' mir Body < ' tcx > ,
1190
1207
tcx : TyCtxt < ' tcx > ,
1191
- borrow_deps : BorrowDependencies < ' mir , ' tcx > ,
1208
+ local_to_nodekind : FxHashMap < Local , NodeKind > ,
1209
+ reborrows_map : FxHashMap < Local , Local > ,
1192
1210
) -> Self {
1193
- LiveBorrows { body, tcx, borrow_deps }
1211
+ LiveBorrows { body, tcx, local_to_nodekind , reborrows_map }
1194
1212
}
1195
1213
1196
1214
fn transfer_function < ' b , T > (
@@ -1201,11 +1219,18 @@ impl<'mir, 'tcx> LiveBorrows<'mir, 'tcx> {
1201
1219
body : self . body ,
1202
1220
tcx : self . tcx ,
1203
1221
_trans : trans,
1204
- borrow_deps : & self . borrow_deps ,
1222
+ local_to_nodekind : & self . local_to_nodekind ,
1223
+ reborrows_map : & self . reborrows_map ,
1205
1224
}
1206
1225
}
1207
1226
}
1208
1227
1228
+ impl < ' mir , ' tcx > crate :: CloneAnalysis for LiveBorrows < ' mir , ' tcx > {
1229
+ fn clone_analysis ( & self ) -> Self {
1230
+ self . clone ( )
1231
+ }
1232
+ }
1233
+
1209
1234
impl < ' a , ' tcx > AnalysisDomain < ' tcx > for LiveBorrows < ' a , ' tcx > {
1210
1235
type Domain = BitSet < Local > ;
1211
1236
type Direction = Backward ;
@@ -1258,7 +1283,8 @@ struct TransferFunction<'a, 'b, 'c, 'tcx, T> {
1258
1283
body : & ' a Body < ' tcx > ,
1259
1284
tcx : TyCtxt < ' tcx > ,
1260
1285
_trans : & ' b mut T ,
1261
- borrow_deps : & ' c BorrowDependencies < ' a , ' tcx > ,
1286
+ local_to_nodekind : & ' c FxHashMap < Local , NodeKind > ,
1287
+ reborrows_map : & ' c FxHashMap < Local , Local > ,
1262
1288
}
1263
1289
1264
1290
impl < ' a , ' b , ' c , ' tcx , T > TransferFunction < ' a , ' b , ' c , ' tcx , T >
@@ -1267,21 +1293,21 @@ where
1267
1293
{
1268
1294
fn gen ( & mut self , local : Local ) {
1269
1295
debug ! ( "gen {:?}" , local) ;
1270
- if let Some ( reborrowed_local) = self . borrow_deps . reborrows_map . get ( & local) {
1296
+ if let Some ( reborrowed_local) = self . reborrows_map . get ( & local) {
1271
1297
self . _trans . gen ( * reborrowed_local) ;
1272
1298
} else {
1273
- if self . borrow_deps . dep_graph . locals_to_node_indexes . get ( & local) . is_some ( ) {
1299
+ if self . local_to_nodekind . get ( & local) . is_some ( ) {
1274
1300
self . _trans . gen ( local)
1275
1301
}
1276
1302
}
1277
1303
}
1278
1304
1279
1305
fn kill ( & mut self , local : Local ) {
1280
1306
debug ! ( "killing {:?}" , local) ;
1281
- if let Some ( reborrowed_local) = self . borrow_deps . reborrows_map . get ( & local) {
1307
+ if let Some ( reborrowed_local) = self . reborrows_map . get ( & local) {
1282
1308
self . _trans . kill ( * reborrowed_local) ;
1283
1309
} else {
1284
- if self . borrow_deps . dep_graph . locals_to_node_indexes . get ( & local) . is_some ( ) {
1310
+ if self . local_to_nodekind . get ( & local) . is_some ( ) {
1285
1311
self . _trans . kill ( local)
1286
1312
}
1287
1313
}
@@ -1312,16 +1338,10 @@ where
1312
1338
self . visit_rvalue ( & assign. 1 , location) ;
1313
1339
}
1314
1340
_ => {
1315
- if let Some ( node_idx) = self
1316
- . borrow_deps
1317
- . dep_graph
1318
- . locals_to_node_indexes
1319
- . get ( & lhs_place. local )
1341
+ if let Some ( NodeKind :: LocalWithRefs ( _) ) =
1342
+ self . local_to_nodekind . get ( & lhs_place. local )
1320
1343
{
1321
- let node = self . borrow_deps . dep_graph . node ( * node_idx) ;
1322
- if let NodeKind :: LocalWithRefs ( _) = node. data {
1323
- self . kill ( lhs_place. local ) ;
1324
- }
1344
+ self . kill ( lhs_place. local ) ;
1325
1345
}
1326
1346
self . super_assign ( & assign. 0 , & assign. 1 , location) ;
1327
1347
}
@@ -1357,13 +1377,8 @@ where
1357
1377
self . gen ( local) ;
1358
1378
}
1359
1379
_ => {
1360
- if let Some ( node_idx) =
1361
- self . borrow_deps . dep_graph . locals_to_node_indexes . get ( & local)
1362
- {
1363
- let node = self . borrow_deps . dep_graph . node ( * node_idx) ;
1364
- if matches ! ( node. data, NodeKind :: LocalWithRefs ( _) ) {
1365
- self . gen ( local) ;
1366
- }
1380
+ if let Some ( NodeKind :: LocalWithRefs ( _) ) = self . local_to_nodekind . get ( & local) {
1381
+ self . gen ( local) ;
1367
1382
}
1368
1383
}
1369
1384
}
@@ -1392,20 +1407,13 @@ where
1392
1407
for arg in args {
1393
1408
match arg {
1394
1409
Operand :: Copy ( place) | Operand :: Move ( place) => {
1395
- if let Some ( node_idx) = self
1396
- . borrow_deps
1397
- . dep_graph
1398
- . locals_to_node_indexes
1399
- . get ( & place. local )
1410
+ if let Some ( NodeKind :: LocalWithRefs ( _) ) =
1411
+ self . local_to_nodekind . get ( & place. local )
1400
1412
{
1401
- let node = self . borrow_deps . dep_graph . node ( * node_idx) ;
1402
-
1403
1413
// these are `Local`s that contain references/pointers or are raw pointers
1404
1414
// that were assigned to raw pointers, which were cast to usize. Since the
1405
1415
// function call is free to use these in any form, we need to gen them here.
1406
- if let NodeKind :: LocalWithRefs ( _) = node. data {
1407
- self . gen ( place. local ) ;
1408
- }
1416
+ self . gen ( place. local ) ;
1409
1417
} else {
1410
1418
self . super_operand ( arg, location)
1411
1419
}
@@ -1424,20 +1432,13 @@ where
1424
1432
1425
1433
match value {
1426
1434
Operand :: Copy ( place) | Operand :: Move ( place) => {
1427
- if let Some ( node_idx) = self
1428
- . borrow_deps
1429
- . dep_graph
1430
- . locals_to_node_indexes
1431
- . get ( & place. local )
1435
+ if let Some ( NodeKind :: LocalWithRefs ( _) ) =
1436
+ self . local_to_nodekind . get ( & place. local )
1432
1437
{
1433
- let node = self . borrow_deps . dep_graph . node ( * node_idx) ;
1434
-
1435
1438
// these are `Local`s that contain references/pointers or are raw pointers
1436
1439
// that were assigned to raw pointers, which were cast to usize. Since the
1437
1440
// function call is free to use these in any form, we need to gen them here.
1438
- if let NodeKind :: LocalWithRefs ( _) = node. data {
1439
- self . gen ( place. local ) ;
1440
- }
1441
+ self . gen ( place. local ) ;
1441
1442
} else {
1442
1443
self . super_operand ( value, location)
1443
1444
}
0 commit comments