Skip to content

Commit 0b7d0a3

Browse files
authored
Rollup merge of rust-lang#99027 - tmiasko:basic-blocks, r=oli-obk
Replace `Body::basic_blocks()` with field access Since the refactoring in rust-lang#98930, it is possible to borrow the basic blocks independently from other parts of MIR by accessing the `basic_blocks` field directly. Replace unnecessary `Body::basic_blocks()` method with a direct field access, which has an additional benefit of borrowing the basic blocks only.
2 parents 9c23719 + b48870b commit 0b7d0a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+131
-140
lines changed

compiler/rustc_borrowck/src/constraint_generation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(super) fn generate_constraints<'cx, 'tcx>(
3131
body,
3232
};
3333

34-
for (bb, data) in body.basic_blocks().iter_enumerated() {
34+
for (bb, data) in body.basic_blocks.iter_enumerated() {
3535
cg.visit_basic_block_data(bb, data);
3636
}
3737
}

compiler/rustc_borrowck/src/dataflow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct OutOfScopePrecomputer<'a, 'tcx> {
143143
impl<'a, 'tcx> OutOfScopePrecomputer<'a, 'tcx> {
144144
fn new(body: &'a Body<'tcx>, regioncx: &'a RegionInferenceContext<'tcx>) -> Self {
145145
OutOfScopePrecomputer {
146-
visited: BitSet::new_empty(body.basic_blocks().len()),
146+
visited: BitSet::new_empty(body.basic_blocks.len()),
147147
visit_stack: vec![],
148148
body,
149149
regioncx,

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
459459
return outmost_back_edge;
460460
}
461461

462-
let block = &self.body.basic_blocks()[location.block];
462+
let block = &self.body.basic_blocks[location.block];
463463

464464
if location.statement_index < block.statements.len() {
465465
let successor = location.successor_within_block();
@@ -518,7 +518,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
518518
}
519519

520520
if loop_head.dominates(from, &self.dominators) {
521-
let block = &self.body.basic_blocks()[from.block];
521+
let block = &self.body.basic_blocks[from.block];
522522

523523
if from.statement_index < block.statements.len() {
524524
let successor = from.successor_within_block();
@@ -568,7 +568,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
568568
UseSpans::PatUse(span)
569569
| UseSpans::OtherUse(span)
570570
| UseSpans::FnSelfUse { var_span: span, .. } => {
571-
let block = &self.body.basic_blocks()[location.block];
571+
let block = &self.body.basic_blocks[location.block];
572572

573573
let kind = if let Some(&Statement {
574574
kind: StatementKind::FakeRead(box (FakeReadCause::ForLet(_), _)),

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
8888
if let Some(StatementKind::Assign(box (
8989
place,
9090
Rvalue::Use(Operand::Move(move_from)),
91-
))) = self.body.basic_blocks()[location.block]
91+
))) = self.body.basic_blocks[location.block]
9292
.statements
9393
.get(location.statement_index)
9494
.map(|stmt| &stmt.kind)

compiler/rustc_borrowck/src/location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl LocationTable {
3333
pub(crate) fn new(body: &Body<'_>) -> Self {
3434
let mut num_points = 0;
3535
let statements_before_block = body
36-
.basic_blocks()
36+
.basic_blocks
3737
.iter()
3838
.map(|block_data| {
3939
let v = num_points;

compiler/rustc_borrowck/src/region_infer/values.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl RegionValueElements {
2525
pub(crate) fn new(body: &Body<'_>) -> Self {
2626
let mut num_points = 0;
2727
let statements_before_block: IndexVec<BasicBlock, usize> = body
28-
.basic_blocks()
28+
.basic_blocks
2929
.iter()
3030
.map(|block_data| {
3131
let v = num_points;
@@ -37,7 +37,7 @@ impl RegionValueElements {
3737
debug!("RegionValueElements: num_points={:#?}", num_points);
3838

3939
let mut basic_blocks = IndexVec::with_capacity(num_points);
40-
for (bb, bb_data) in body.basic_blocks().iter_enumerated() {
40+
for (bb, bb_data) in body.basic_blocks.iter_enumerated() {
4141
basic_blocks.extend((0..=bb_data.statements.len()).map(|_| bb));
4242
}
4343

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
26332633
self.check_local(&body, local, local_decl);
26342634
}
26352635

2636-
for (block, block_data) in body.basic_blocks().iter_enumerated() {
2636+
for (block, block_data) in body.basic_blocks.iter_enumerated() {
26372637
let mut location = Location { block, statement_index: 0 };
26382638
for stmt in &block_data.statements {
26392639
if !stmt.source_info.span.is_dummy() {

compiler/rustc_codegen_cranelift/src/analyze.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) fn analyze(fx: &FunctionCx<'_, '_, '_>) -> IndexVec<Local, SsaKind> {
2626
})
2727
.collect::<IndexVec<Local, SsaKind>>();
2828

29-
for bb in fx.mir.basic_blocks().iter() {
29+
for bb in fx.mir.basic_blocks.iter() {
3030
for stmt in bb.statements.iter() {
3131
match &stmt.kind {
3232
Assign(place_and_rval) => match &place_and_rval.1 {

compiler/rustc_codegen_cranelift/src/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub(crate) fn codegen_fn<'tcx>(
7373
// Predefine blocks
7474
let start_block = bcx.create_block();
7575
let block_map: IndexVec<BasicBlock, Block> =
76-
(0..mir.basic_blocks().len()).map(|_| bcx.create_block()).collect();
76+
(0..mir.basic_blocks.len()).map(|_| bcx.create_block()).collect();
7777

7878
// Make FunctionCx
7979
let target_config = module.target_config();
@@ -271,7 +271,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
271271
}
272272
fx.tcx.sess.time("codegen prelude", || crate::abi::codegen_fn_prelude(fx, start_block));
273273

274-
for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
274+
for (bb, bb_data) in fx.mir.basic_blocks.iter_enumerated() {
275275
let block = fx.get_block(bb);
276276
fx.bcx.switch_to_block(block);
277277

compiler/rustc_codegen_cranelift/src/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
505505
return None;
506506
}
507507
let mut computed_const_val = None;
508-
for bb_data in fx.mir.basic_blocks() {
508+
for bb_data in fx.mir.basic_blocks.iter() {
509509
for stmt in &bb_data.statements {
510510
match &stmt.kind {
511511
StatementKind::Assign(local_and_rvalue) if &local_and_rvalue.0 == place => {

compiler/rustc_codegen_ssa/src/mir/analyze.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKi
266266
result: &mut IndexVec<mir::BasicBlock, CleanupKind>,
267267
mir: &mir::Body<'tcx>,
268268
) {
269-
for (bb, data) in mir.basic_blocks().iter_enumerated() {
269+
for (bb, data) in mir.basic_blocks.iter_enumerated() {
270270
match data.terminator().kind {
271271
TerminatorKind::Goto { .. }
272272
| TerminatorKind::Resume
@@ -296,7 +296,7 @@ pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKi
296296
}
297297

298298
fn propagate<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>, mir: &mir::Body<'tcx>) {
299-
let mut funclet_succs = IndexVec::from_elem(None, mir.basic_blocks());
299+
let mut funclet_succs = IndexVec::from_elem(None, &mir.basic_blocks);
300300

301301
let mut set_successor = |funclet: mir::BasicBlock, succ| match funclet_succs[funclet] {
302302
ref mut s @ None => {
@@ -359,7 +359,7 @@ pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKi
359359
}
360360
}
361361

362-
let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks());
362+
let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, &mir.basic_blocks);
363363

364364
discover_masters(&mut result, mir);
365365
propagate(&mut result, mir);

compiler/rustc_codegen_ssa/src/mir/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
150150
let start_llbb = Bx::append_block(cx, llfn, "start");
151151
let mut bx = Bx::build(cx, start_llbb);
152152

153-
if mir.basic_blocks().iter().any(|bb| bb.is_cleanup) {
153+
if mir.basic_blocks.iter().any(|bb| bb.is_cleanup) {
154154
bx.set_personality_fn(cx.eh_personality());
155155
}
156156

157157
let cleanup_kinds = analyze::cleanup_kinds(&mir);
158158
let cached_llbbs: IndexVec<mir::BasicBlock, Option<Bx::BasicBlock>> = mir
159-
.basic_blocks()
159+
.basic_blocks
160160
.indices()
161161
.map(|bb| if bb == mir::START_BLOCK { Some(start_llbb) } else { None })
162162
.collect();
@@ -172,8 +172,8 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
172172
unreachable_block: None,
173173
double_unwind_guard: None,
174174
cleanup_kinds,
175-
landing_pads: IndexVec::from_elem(None, mir.basic_blocks()),
176-
funclets: IndexVec::from_fn_n(|_| None, mir.basic_blocks().len()),
175+
landing_pads: IndexVec::from_elem(None, &mir.basic_blocks),
176+
funclets: IndexVec::from_fn_n(|_| None, mir.basic_blocks.len()),
177177
locals: IndexVec::new(),
178178
debug_context,
179179
per_local_var_debug_info: None,

compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
782782
assert_eq!(
783783
unwinding,
784784
match self.frame().loc {
785-
Ok(loc) => self.body().basic_blocks()[loc.block].is_cleanup,
785+
Ok(loc) => self.body().basic_blocks[loc.block].is_cleanup,
786786
Err(_) => true,
787787
}
788788
);

compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2828
let mut source_info = *frame.body.source_info(loc);
2929

3030
// If this is a `Call` terminator, use the `fn_span` instead.
31-
let block = &frame.body.basic_blocks()[loc.block];
31+
let block = &frame.body.basic_blocks[loc.block];
3232
if loc.statement_index == block.statements.len() {
3333
debug!(
3434
"find_closest_untracked_caller_location: got terminator {:?} ({:?})",

compiler/rustc_const_eval/src/interpret/step.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5353
self.pop_stack_frame(/* unwinding */ true)?;
5454
return Ok(true);
5555
};
56-
let basic_block = &self.body().basic_blocks()[loc.block];
56+
let basic_block = &self.body().basic_blocks[loc.block];
5757

5858
if let Some(stmt) = basic_block.statements.get(loc.statement_index) {
5959
let old_frames = self.frame_idx();

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
135135
// qualifs for the return type.
136136
let return_block = ccx
137137
.body
138-
.basic_blocks()
138+
.basic_blocks
139139
.iter_enumerated()
140140
.find(|(_, block)| matches!(block.terminator().kind, TerminatorKind::Return))
141141
.map(|(bb, _)| bb);

compiler/rustc_const_eval/src/transform/promote_consts.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
710710
}
711711

712712
fn assign(&mut self, dest: Local, rvalue: Rvalue<'tcx>, span: Span) {
713-
let last = self.promoted.basic_blocks().last().unwrap();
713+
let last = self.promoted.basic_blocks.last().unwrap();
714714
let data = &mut self.promoted[last];
715715
data.statements.push(Statement {
716716
source_info: SourceInfo::outermost(span),
@@ -803,7 +803,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
803803
self.visit_operand(arg, loc);
804804
}
805805

806-
let last = self.promoted.basic_blocks().last().unwrap();
806+
let last = self.promoted.basic_blocks.last().unwrap();
807807
let new_target = self.new_block();
808808

809809
*self.promoted[last].terminator_mut() = Terminator {
@@ -1041,7 +1041,7 @@ pub fn is_const_fn_in_array_repeat_expression<'tcx>(
10411041
_ => {}
10421042
}
10431043

1044-
for block in body.basic_blocks() {
1044+
for block in body.basic_blocks.iter() {
10451045
if let Some(Terminator { kind: TerminatorKind::Call { func, destination, .. }, .. }) =
10461046
&block.terminator
10471047
{

compiler/rustc_const_eval/src/transform/validate.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
140140
if bb == START_BLOCK {
141141
self.fail(location, "start block must not have predecessors")
142142
}
143-
if let Some(bb) = self.body.basic_blocks().get(bb) {
144-
let src = self.body.basic_blocks().get(location.block).unwrap();
143+
if let Some(bb) = self.body.basic_blocks.get(bb) {
144+
let src = self.body.basic_blocks.get(location.block).unwrap();
145145
match (src.is_cleanup, bb.is_cleanup, edge_kind) {
146146
// Non-cleanup blocks can jump to non-cleanup blocks along non-unwind edges
147147
(false, false, EdgeKind::Normal)
@@ -881,13 +881,13 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
881881
}
882882
TerminatorKind::Resume | TerminatorKind::Abort => {
883883
let bb = location.block;
884-
if !self.body.basic_blocks()[bb].is_cleanup {
884+
if !self.body.basic_blocks[bb].is_cleanup {
885885
self.fail(location, "Cannot `Resume` or `Abort` from non-cleanup basic block")
886886
}
887887
}
888888
TerminatorKind::Return => {
889889
let bb = location.block;
890-
if self.body.basic_blocks()[bb].is_cleanup {
890+
if self.body.basic_blocks[bb].is_cleanup {
891891
self.fail(location, "Cannot `Return` from cleanup basic block")
892892
}
893893
}

compiler/rustc_middle/src/mir/generic_graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ pub fn mir_fn_to_generic_graph<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'_>) -> Grap
1212

1313
// Nodes
1414
let nodes: Vec<Node> = body
15-
.basic_blocks()
15+
.basic_blocks
1616
.iter_enumerated()
1717
.map(|(block, _)| bb_to_graph_node(block, body, dark_mode))
1818
.collect();
1919

2020
// Edges
2121
let mut edges = Vec::new();
22-
for (source, _) in body.basic_blocks().iter_enumerated() {
22+
for (source, _) in body.basic_blocks.iter_enumerated() {
2323
let def_id = body.source.def_id();
2424
let terminator = body[source].terminator();
2525
let labels = terminator.kind.fmt_successor_labels();

compiler/rustc_middle/src/mir/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,6 @@ impl<'tcx> Body<'tcx> {
331331
body
332332
}
333333

334-
#[inline]
335-
pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
336-
&self.basic_blocks
337-
}
338-
339334
#[inline]
340335
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
341336
self.basic_blocks.as_mut()
@@ -490,7 +485,7 @@ impl<'tcx> Index<BasicBlock> for Body<'tcx> {
490485

491486
#[inline]
492487
fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
493-
&self.basic_blocks()[index]
488+
&self.basic_blocks[index]
494489
}
495490
}
496491

compiler/rustc_middle/src/mir/patch.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct MirPatch<'tcx> {
1919
impl<'tcx> MirPatch<'tcx> {
2020
pub fn new(body: &Body<'tcx>) -> Self {
2121
let mut result = MirPatch {
22-
patch_map: IndexVec::from_elem(None, body.basic_blocks()),
22+
patch_map: IndexVec::from_elem(None, &body.basic_blocks),
2323
new_blocks: vec![],
2424
new_statements: vec![],
2525
new_locals: vec![],
@@ -29,7 +29,7 @@ impl<'tcx> MirPatch<'tcx> {
2929
};
3030

3131
// Check if we already have a resume block
32-
for (bb, block) in body.basic_blocks().iter_enumerated() {
32+
for (bb, block) in body.basic_blocks.iter_enumerated() {
3333
if let TerminatorKind::Resume = block.terminator().kind && block.statements.is_empty() {
3434
result.resume_block = Some(bb);
3535
break;
@@ -61,7 +61,7 @@ impl<'tcx> MirPatch<'tcx> {
6161
}
6262

6363
pub fn terminator_loc(&self, body: &Body<'tcx>, bb: BasicBlock) -> Location {
64-
let offset = match bb.index().checked_sub(body.basic_blocks().len()) {
64+
let offset = match bb.index().checked_sub(body.basic_blocks.len()) {
6565
Some(index) => self.new_blocks[index].statements.len(),
6666
None => body[bb].statements.len(),
6767
};
@@ -129,7 +129,7 @@ impl<'tcx> MirPatch<'tcx> {
129129
debug!(
130130
"MirPatch: {} new blocks, starting from index {}",
131131
self.new_blocks.len(),
132-
body.basic_blocks().len()
132+
body.basic_blocks.len()
133133
);
134134
let bbs = if self.patch_map.is_empty() && self.new_blocks.is_empty() {
135135
body.basic_blocks.as_mut_preserves_cfg()
@@ -173,7 +173,7 @@ impl<'tcx> MirPatch<'tcx> {
173173
}
174174

175175
pub fn source_info_for_location(&self, body: &Body<'tcx>, loc: Location) -> SourceInfo {
176-
let data = match loc.block.index().checked_sub(body.basic_blocks().len()) {
176+
let data = match loc.block.index().checked_sub(body.basic_blocks.len()) {
177177
Some(new) => &self.new_blocks[new],
178178
None => &body[loc.block],
179179
};

compiler/rustc_middle/src/mir/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ where
318318
F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
319319
{
320320
write_mir_intro(tcx, body, w)?;
321-
for block in body.basic_blocks().indices() {
321+
for block in body.basic_blocks.indices() {
322322
extra_data(PassWhere::BeforeBlock(block), w)?;
323323
write_basic_block(tcx, block, body, extra_data, w)?;
324-
if block.index() + 1 != body.basic_blocks().len() {
324+
if block.index() + 1 != body.basic_blocks.len() {
325325
writeln!(w)?;
326326
}
327327
}

compiler/rustc_middle/src/mir/spanview.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ where
105105
}
106106
let body_span = hir_body.unwrap().value.span;
107107
let mut span_viewables = Vec::new();
108-
for (bb, data) in body.basic_blocks().iter_enumerated() {
108+
for (bb, data) in body.basic_blocks.iter_enumerated() {
109109
match spanview {
110110
MirSpanview::Statement => {
111111
for (i, statement) in data.statements.iter().enumerate() {

compiler/rustc_middle/src/mir/traversal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'a, 'tcx> Preorder<'a, 'tcx> {
3737

3838
Preorder {
3939
body,
40-
visited: BitSet::new_empty(body.basic_blocks().len()),
40+
visited: BitSet::new_empty(body.basic_blocks.len()),
4141
worklist,
4242
root_is_start_block: root == START_BLOCK,
4343
}
@@ -71,7 +71,7 @@ impl<'a, 'tcx> Iterator for Preorder<'a, 'tcx> {
7171

7272
fn size_hint(&self) -> (usize, Option<usize>) {
7373
// All the blocks, minus the number of blocks we've visited.
74-
let upper = self.body.basic_blocks().len() - self.visited.count();
74+
let upper = self.body.basic_blocks.len() - self.visited.count();
7575

7676
let lower = if self.root_is_start_block {
7777
// We will visit all remaining blocks exactly once.

0 commit comments

Comments
 (0)