1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A pass that promotes borrows of constant rvalues.
13 //! The rvalues considered constant are trees of temps,
14 //! each with exactly one initialization, and holding
15 //! a constant value with no interior mutability.
16 //! They are placed into a new MIR constant body in
17 //! `promoted` and the borrow rvalue is replaced with
18 //! a `Literal::Promoted` using the index into `promoted`
19 //! of that constant MIR.
21 //! This pass assumes that every use is dominated by an
22 //! initialization and can otherwise silence errors, if
23 //! move analysis runs after promotion on broken MIR.
26 use rustc::mir::visit::{PlaceContext, MutatingUseContext, MutVisitor, Visitor};
27 use rustc::mir::traversal::ReversePostorder;
28 use rustc::ty::TyCtxt;
31 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
33 use std::{iter, mem, usize};
35 /// State of a temporary during collection and promotion.
36 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
38 /// No references to this temp.
40 /// One direct assignment and any number of direct uses.
41 /// A borrow of this temp is promotable if the assigned
42 /// value is qualified as constant.
47 /// Any other combination of assignments/uses.
49 /// This temp was part of an rvalue which got extracted
50 /// during promotion and needs cleanup.
55 pub fn is_promotable(&self) -> bool {
56 debug!("is_promotable: self={:?}", self);
57 if let TempState::Defined { uses, .. } = *self {
65 /// A "root candidate" for promotion, which will become the
66 /// returned value in a promoted MIR, unless it's a subset
67 /// of a larger candidate.
70 /// Borrow of a constant temporary.
73 /// Currently applied to function calls where the callee has the unstable
74 /// `#[rustc_args_required_const]` attribute as well as the SIMD shuffle
75 /// intrinsic. The intrinsic requires the arguments are indeed constant and
76 /// the attribute currently provides the semantic requirement that arguments
78 Argument { bb: BasicBlock, index: usize },
81 struct TempCollector<'tcx> {
82 temps: IndexVec<Local, TempState>,
87 impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
88 fn visit_local(&mut self,
90 context: PlaceContext<'tcx>,
92 debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
93 // We're only interested in temporaries
94 if self.mir.local_kind(index) != LocalKind::Temp {
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::AsmOutput) |
115 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
116 *temp = TempState::Defined {
122 _ => { /* mark as unpromotable below */ }
124 } else if let TempState::Defined { ref mut uses, .. } = *temp {
125 // We always allow borrows, even mutable ones, as we need
126 // to promote mutable borrows of some ZSTs e.g., `&mut []`.
127 let allowed_use = context.is_borrow() || context.is_nonmutating_use();
128 debug!("visit_local: allowed_use={:?}", allowed_use);
133 /* mark as unpromotable below */
135 *temp = TempState::Unpromotable;
138 fn visit_source_info(&mut self, source_info: &SourceInfo) {
139 self.span = source_info.span;
143 pub fn collect_temps(mir: &Mir, rpo: &mut ReversePostorder) -> IndexVec<Local, TempState> {
144 let mut collector = TempCollector {
145 temps: IndexVec::from_elem(TempState::Undefined, &mir.local_decls),
149 for (bb, data) in rpo {
150 collector.visit_basic_block_data(bb, data);
155 struct Promoter<'a, 'tcx: 'a> {
156 tcx: TyCtxt<'a, 'tcx, 'tcx>,
157 source: &'a mut Mir<'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(Place::Local(dest), box rvalue)
194 /// Copy the initialization of this temp to the
195 /// promoted MIR, recursing through temps.
196 fn promote_temp(&mut self, temp: Local) -> Local {
197 let old_keep_original = self.keep_original;
198 let loc = match self.temps[temp] {
199 TempState::Defined { location, uses } if uses > 0 => {
201 self.keep_original = true;
206 span_bug!(self.promoted.span, "{:?} not promotable: {:?}",
210 if !self.keep_original {
211 self.temps[temp] = TempState::PromotedOut;
214 let no_stmts = self.source[loc.block].statements.len();
215 let new_temp = self.promoted.local_decls.push(
216 LocalDecl::new_temp(self.source.local_decls[temp].ty,
217 self.source.local_decls[temp].source_info.span));
219 debug!("promote({:?} @ {:?}/{:?}, {:?})",
220 temp, loc, no_stmts, self.keep_original);
222 // First, take the Rvalue or Call out of the source MIR,
223 // or duplicate it, depending on keep_original.
224 if loc.statement_index < no_stmts {
225 let (rvalue, source_info) = {
226 let statement = &mut self.source[loc.block].statements[loc.statement_index];
227 let rhs = match statement.kind {
228 StatementKind::Assign(_, ref mut rhs) => rhs,
230 span_bug!(statement.source_info.span, "{:?} is not an assignment",
235 (if self.keep_original {
238 let unit = box Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
239 mem::replace(rhs, unit)
240 }, statement.source_info)
243 let mut rvalue = *rvalue;
244 self.visit_rvalue(&mut rvalue, loc);
245 self.assign(new_temp, rvalue, source_info.span);
247 let terminator = if self.keep_original {
248 self.source[loc.block].terminator().clone()
250 let terminator = self.source[loc.block].terminator_mut();
251 let target = match terminator.kind {
252 TerminatorKind::Call { destination: Some((_, target)), .. } => target,
254 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
258 source_info: terminator.source_info,
259 kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto {
265 match terminator.kind {
266 TerminatorKind::Call { mut func, mut args, from_hir_call, .. } => {
267 self.visit_operand(&mut func, loc);
268 for arg in &mut args {
269 self.visit_operand(arg, loc);
272 let last = self.promoted.basic_blocks().last().unwrap();
273 let new_target = self.new_block();
275 *self.promoted[last].terminator_mut() = Terminator {
276 kind: TerminatorKind::Call {
280 destination: Some((Place::Local(new_temp), new_target)),
287 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
292 self.keep_original = old_keep_original;
296 fn promote_candidate(mut self, candidate: Candidate) {
298 let promoted = &mut self.promoted;
299 let promoted_id = Promoted::new(self.source.promoted.len());
300 let mut promoted_place = |ty, span| {
301 promoted.span = span;
302 promoted.local_decls[RETURN_PLACE] =
303 LocalDecl::new_return_place(ty, span);
304 Place::Promoted(box (promoted_id, ty))
306 let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
308 Candidate::Ref(loc) => {
309 let ref mut statement = blocks[loc.block].statements[loc.statement_index];
310 match statement.kind {
311 StatementKind::Assign(_, box Rvalue::Ref(_, _, ref mut place)) => {
312 // Find the underlying local for this (necessarily interior) borrow.
313 let mut place = place;
314 while let Place::Projection(ref mut proj) = *place {
315 assert_ne!(proj.elem, ProjectionElem::Deref);
316 place = &mut proj.base;
319 let ty = place.ty(local_decls, self.tcx).to_ty(self.tcx);
320 let span = statement.source_info.span;
322 Operand::Move(mem::replace(place, promoted_place(ty, span)))
327 Candidate::Argument { bb, index } => {
328 let terminator = blocks[bb].terminator_mut();
329 match terminator.kind {
330 TerminatorKind::Call { ref mut args, .. } => {
331 let ty = args[index].ty(local_decls, self.tcx);
332 let span = terminator.source_info.span;
333 let operand = Operand::Copy(promoted_place(ty, span));
334 mem::replace(&mut args[index], operand)
336 // We expected a `TerminatorKind::Call` for which we'd like to promote an
337 // argument. `qualify_consts` saw a `TerminatorKind::Call` here, but
338 // we are seeing a `Goto`. That means that the `promote_temps` method
339 // already promoted this call away entirely. This case occurs when calling
340 // a function requiring a constant argument and as that constant value
341 // providing a value whose computation contains another call to a function
342 // requiring a constant argument.
343 TerminatorKind::Goto { .. } => return,
350 assert_eq!(self.new_block(), START_BLOCK);
351 self.visit_operand(&mut operand, Location {
352 block: BasicBlock::new(0),
353 statement_index: usize::MAX
356 let span = self.promoted.span;
357 self.assign(RETURN_PLACE, Rvalue::Use(operand), span);
358 self.source.promoted.push(self.promoted);
362 /// Replaces all temporaries with their promoted counterparts.
363 impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
364 fn visit_local(&mut self,
366 _: PlaceContext<'tcx>,
368 if self.source.local_kind(*local) == LocalKind::Temp {
369 *local = self.promote_temp(*local);
374 pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
375 tcx: TyCtxt<'a, 'tcx, 'tcx>,
376 mut temps: IndexVec<Local, TempState>,
377 candidates: Vec<Candidate>) {
378 // Visit candidates in reverse, in case they're nested.
379 debug!("promote_candidates({:?})", candidates);
381 for candidate in candidates.into_iter().rev() {
383 Candidate::Ref(Location { block, statement_index }) => {
384 match mir[block].statements[statement_index].kind {
385 StatementKind::Assign(Place::Local(local), _) => {
386 if temps[local] == TempState::PromotedOut {
394 Candidate::Argument { .. } => {}
398 // Declare return place local so that `Mir::new` doesn't complain.
399 let initial_locals = iter::once(
400 LocalDecl::new_return_place(tcx.types.never, mir.span)
403 let promoter = Promoter {
406 // FIXME: maybe try to filter this to avoid blowing up
408 mir.source_scopes.clone(),
409 mir.source_scope_local_data.clone(),
422 promoter.promote_candidate(candidate);
425 // Eliminate assignments to, and drops of promoted temps.
426 let promoted = |index: Local| temps[index] == TempState::PromotedOut;
427 for block in mir.basic_blocks_mut() {
428 block.statements.retain(|statement| {
429 match statement.kind {
430 StatementKind::Assign(Place::Local(index), _) |
431 StatementKind::StorageLive(index) |
432 StatementKind::StorageDead(index) => {
438 let terminator = block.terminator_mut();
439 match terminator.kind {
440 TerminatorKind::Drop { location: Place::Local(index), target, .. } => {
442 terminator.kind = TerminatorKind::Goto {