1 //! A pass that promotes borrows of constant rvalues.
3 //! The rvalues considered constant are trees of temps,
4 //! each with exactly one initialization, and holding
5 //! a constant value with no interior mutability.
6 //! They are placed into a new MIR constant body in
7 //! `promoted` and the borrow rvalue is replaced with
8 //! a `Literal::Promoted` using the index into `promoted`
9 //! of that constant MIR.
11 //! This pass assumes that every use is dominated by an
12 //! initialization and can otherwise silence errors, if
13 //! move analysis runs after promotion on broken MIR.
15 use rustc::hir::def_id::DefId;
17 use rustc::mir::visit::{PlaceContext, MutatingUseContext, MutVisitor, Visitor};
18 use rustc::mir::traversal::ReversePostorder;
19 use rustc::ty::subst::InternalSubsts;
20 use rustc::ty::TyCtxt;
23 use rustc_index::vec::{IndexVec, Idx};
25 use std::{iter, mem, usize};
27 /// State of a temporary during collection and promotion.
28 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
30 /// No references to this temp.
32 /// One direct assignment and any number of direct uses.
33 /// A borrow of this temp is promotable if the assigned
34 /// value is qualified as constant.
39 /// Any other combination of assignments/uses.
41 /// This temp was part of an rvalue which got extracted
42 /// during promotion and needs cleanup.
47 pub fn is_promotable(&self) -> bool {
48 debug!("is_promotable: self={:?}", self);
49 if let TempState::Defined { .. } = *self {
57 /// A "root candidate" for promotion, which will become the
58 /// returned value in a promoted MIR, unless it's a subset
59 /// of a larger candidate.
62 /// Borrow of a constant temporary.
65 /// Promotion of the `x` in `[x; 32]`.
68 /// Currently applied to function calls where the callee has the unstable
69 /// `#[rustc_args_required_const]` attribute as well as the SIMD shuffle
70 /// intrinsic. The intrinsic requires the arguments are indeed constant and
71 /// the attribute currently provides the semantic requirement that arguments
73 Argument { bb: BasicBlock, index: usize },
76 struct TempCollector<'tcx> {
77 temps: IndexVec<Local, TempState>,
79 body: &'tcx Body<'tcx>,
82 impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
83 fn visit_local(&mut self,
85 context: PlaceContext,
87 debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
88 // We're only interested in temporaries and the return place
89 match self.body.local_kind(index) {
91 | LocalKind::ReturnPointer
98 // Ignore drops, if the temp gets promoted,
99 // then it's constant and thus drop is noop.
100 // Non-uses are also irrelevent.
101 if context.is_drop() || !context.is_use() {
103 "visit_local: context.is_drop={:?} context.is_use={:?}",
104 context.is_drop(), context.is_use(),
109 let temp = &mut self.temps[index];
110 debug!("visit_local: temp={:?}", temp);
111 if *temp == TempState::Undefined {
113 PlaceContext::MutatingUse(MutatingUseContext::Store) |
114 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
115 *temp = TempState::Defined {
121 _ => { /* mark as unpromotable below */ }
123 } else if let TempState::Defined { ref mut uses, .. } = *temp {
124 // We always allow borrows, even mutable ones, as we need
125 // to promote mutable borrows of some ZSTs e.g., `&mut []`.
126 let allowed_use = context.is_borrow() || context.is_nonmutating_use();
127 debug!("visit_local: allowed_use={:?}", allowed_use);
132 /* mark as unpromotable below */
134 *temp = TempState::Unpromotable;
137 fn visit_source_info(&mut self, source_info: &SourceInfo) {
138 self.span = source_info.span;
142 pub fn collect_temps(body: &Body<'_>,
143 rpo: &mut ReversePostorder<'_, '_>) -> IndexVec<Local, TempState> {
144 let mut collector = TempCollector {
145 temps: IndexVec::from_elem(TempState::Undefined, &body.local_decls),
149 for (bb, data) in rpo {
150 collector.visit_basic_block_data(bb, data);
155 struct Promoter<'a, 'tcx> {
157 source: &'a mut Body<'tcx>,
158 promoted: Body<'tcx>,
159 temps: &'a mut IndexVec<Local, TempState>,
161 /// If true, all nested temps are also kept in the
162 /// source MIR, not moved to the promoted MIR.
166 impl<'a, 'tcx> Promoter<'a, 'tcx> {
167 fn new_block(&mut self) -> BasicBlock {
168 let span = self.promoted.span;
169 self.promoted.basic_blocks_mut().push(BasicBlockData {
171 terminator: Some(Terminator {
172 source_info: SourceInfo {
174 scope: OUTERMOST_SOURCE_SCOPE
176 kind: TerminatorKind::Return
182 fn assign(&mut self, dest: Local, rvalue: Rvalue<'tcx>, span: Span) {
183 let last = self.promoted.basic_blocks().last().unwrap();
184 let data = &mut self.promoted[last];
185 data.statements.push(Statement {
186 source_info: SourceInfo {
188 scope: OUTERMOST_SOURCE_SCOPE
190 kind: StatementKind::Assign(box(Place::from(dest), rvalue))
194 fn is_temp_kind(&self, local: Local) -> bool {
195 self.source.local_kind(local) == LocalKind::Temp
198 /// Copies the initialization of this temp to the
199 /// promoted MIR, recursing through temps.
200 fn promote_temp(&mut self, temp: Local) -> Local {
201 let old_keep_original = self.keep_original;
202 let loc = match self.temps[temp] {
203 TempState::Defined { location, uses } if uses > 0 => {
205 self.keep_original = true;
210 span_bug!(self.promoted.span, "{:?} not promotable: {:?}",
214 if !self.keep_original {
215 self.temps[temp] = TempState::PromotedOut;
218 let no_stmts = self.source[loc.block].statements.len();
219 let new_temp = self.promoted.local_decls.push(
220 LocalDecl::new_temp(self.source.local_decls[temp].ty,
221 self.source.local_decls[temp].source_info.span));
223 debug!("promote({:?} @ {:?}/{:?}, {:?})",
224 temp, loc, no_stmts, self.keep_original);
226 // First, take the Rvalue or Call out of the source MIR,
227 // or duplicate it, depending on keep_original.
228 if loc.statement_index < no_stmts {
229 let (mut rvalue, source_info) = {
230 let statement = &mut self.source[loc.block].statements[loc.statement_index];
231 let rhs = match statement.kind {
232 StatementKind::Assign(box(_, ref mut rhs)) => rhs,
234 span_bug!(statement.source_info.span, "{:?} is not an assignment",
239 (if self.keep_original {
242 let unit = Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
243 mem::replace(rhs, unit)
244 }, statement.source_info)
247 self.visit_rvalue(&mut rvalue, loc);
248 self.assign(new_temp, rvalue, source_info.span);
250 let terminator = if self.keep_original {
251 self.source[loc.block].terminator().clone()
253 let terminator = self.source[loc.block].terminator_mut();
254 let target = match terminator.kind {
255 TerminatorKind::Call { destination: Some((_, target)), .. } => target,
257 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
261 source_info: terminator.source_info,
262 kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto {
268 match terminator.kind {
269 TerminatorKind::Call { mut func, mut args, from_hir_call, .. } => {
270 self.visit_operand(&mut func, loc);
271 for arg in &mut args {
272 self.visit_operand(arg, loc);
275 let last = self.promoted.basic_blocks().last().unwrap();
276 let new_target = self.new_block();
278 *self.promoted[last].terminator_mut() = Terminator {
279 kind: TerminatorKind::Call {
284 (Place::from(new_temp), new_target)
292 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
297 self.keep_original = old_keep_original;
301 fn promote_candidate(
304 candidate: Candidate,
305 next_promoted_id: usize,
306 ) -> Option<Body<'tcx>> {
308 let promoted = &mut self.promoted;
309 let promoted_id = Promoted::new(next_promoted_id);
311 let mut promoted_place = |ty, span| {
312 promoted.span = span;
313 promoted.local_decls[RETURN_PLACE] = LocalDecl::new_return_place(ty, span);
315 base: PlaceBase::Static(box Static {
317 StaticKind::Promoted(
319 InternalSubsts::identity_for_item(tcx, def_id),
327 let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
329 Candidate::Ref(loc) => {
330 let ref mut statement = blocks[loc.block].statements[loc.statement_index];
331 match statement.kind {
332 StatementKind::Assign(box(_, Rvalue::Ref(_, _, ref mut place))) => {
333 // Use the underlying local for this (necessarily interior) borrow.
334 let ty = place.base.ty(local_decls).ty;
335 let span = statement.source_info.span;
337 Operand::Move(Place {
340 promoted_place(ty, span).base,
348 Candidate::Repeat(loc) => {
349 let ref mut statement = blocks[loc.block].statements[loc.statement_index];
350 match statement.kind {
351 StatementKind::Assign(box(_, Rvalue::Repeat(ref mut operand, _))) => {
352 let ty = operand.ty(local_decls, self.tcx);
353 let span = statement.source_info.span;
356 Operand::Copy(promoted_place(ty, span))
362 Candidate::Argument { bb, index } => {
363 let terminator = blocks[bb].terminator_mut();
364 match terminator.kind {
365 TerminatorKind::Call { ref mut args, .. } => {
366 let ty = args[index].ty(local_decls, self.tcx);
367 let span = terminator.source_info.span;
368 let operand = Operand::Copy(promoted_place(ty, span));
369 mem::replace(&mut args[index], operand)
371 // We expected a `TerminatorKind::Call` for which we'd like to promote an
372 // argument. `qualify_consts` saw a `TerminatorKind::Call` here, but
373 // we are seeing a `Goto`. That means that the `promote_temps` method
374 // already promoted this call away entirely. This case occurs when calling
375 // a function requiring a constant argument and as that constant value
376 // providing a value whose computation contains another call to a function
377 // requiring a constant argument.
378 TerminatorKind::Goto { .. } => return None,
385 assert_eq!(self.new_block(), START_BLOCK);
386 self.visit_operand(&mut operand, Location {
387 block: BasicBlock::new(0),
388 statement_index: usize::MAX
391 let span = self.promoted.span;
392 self.assign(RETURN_PLACE, Rvalue::Use(operand), span);
397 /// Replaces all temporaries with their promoted counterparts.
398 impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
399 fn visit_local(&mut self,
403 if self.is_temp_kind(*local) {
404 *local = self.promote_temp(*local);
408 fn process_projection_elem(
410 elem: &PlaceElem<'tcx>,
411 ) -> Option<PlaceElem<'tcx>> {
413 PlaceElem::Index(local) if self.is_temp_kind(*local) => {
414 Some(PlaceElem::Index(self.promote_temp(*local)))
421 pub fn promote_candidates<'tcx>(
423 body: &mut Body<'tcx>,
425 mut temps: IndexVec<Local, TempState>,
426 candidates: Vec<Candidate>,
427 ) -> IndexVec<Promoted, Body<'tcx>> {
428 // Visit candidates in reverse, in case they're nested.
429 debug!("promote_candidates({:?})", candidates);
431 let mut promotions = IndexVec::new();
433 for candidate in candidates.into_iter().rev() {
435 Candidate::Repeat(Location { block, statement_index }) |
436 Candidate::Ref(Location { block, statement_index }) => {
437 match body[block].statements[statement_index].kind {
438 StatementKind::Assign(box(Place {
439 base: PlaceBase::Local(local),
442 if temps[local] == TempState::PromotedOut {
450 Candidate::Argument { .. } => {}
454 // Declare return place local so that `mir::Body::new` doesn't complain.
455 let initial_locals = iter::once(
456 LocalDecl::new_return_place(tcx.types.never, body.span)
459 let promoter = Promoter {
462 // FIXME: maybe try to filter this to avoid blowing up
464 body.source_scopes.clone(),
465 body.source_scope_local_data.clone(),
480 //FIXME(oli-obk): having a `maybe_push()` method on `IndexVec` might be nice
481 if let Some(promoted) = promoter.promote_candidate(def_id, candidate, promotions.len()) {
482 promotions.push(promoted);
486 // Eliminate assignments to, and drops of promoted temps.
487 let promoted = |index: Local| temps[index] == TempState::PromotedOut;
488 for block in body.basic_blocks_mut() {
489 block.statements.retain(|statement| {
490 match statement.kind {
491 StatementKind::Assign(box(Place {
492 base: PlaceBase::Local(index),
495 StatementKind::StorageLive(index) |
496 StatementKind::StorageDead(index) => {
502 let terminator = block.terminator_mut();
503 match terminator.kind {
504 TerminatorKind::Drop { location: Place {
505 base: PlaceBase::Local(index),
509 terminator.kind = TerminatorKind::Goto {