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::{LvalueContext, MutVisitor, Visitor};
27 use rustc::mir::traversal::ReversePostorder;
28 use rustc::ty::TyCtxt;
31 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
37 /// State of a temporary during collection and promotion.
38 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
40 /// No references to this temp.
42 /// One direct assignment and any number of direct uses.
43 /// A borrow of this temp is promotable if the assigned
44 /// value is qualified as constant.
49 /// Any other combination of assignments/uses.
51 /// This temp was part of an rvalue which got extracted
52 /// during promotion and needs cleanup.
57 pub fn is_promotable(&self) -> bool {
58 if let TempState::Defined { uses, .. } = *self {
66 /// A "root candidate" for promotion, which will become the
67 /// returned value in a promoted MIR, unless it's a subset
68 /// of a larger candidate.
71 /// Borrow of a constant temporary.
74 /// Array of indices found in the third argument of
75 /// a call to one of the simd_shuffleN intrinsics.
76 ShuffleIndices(BasicBlock)
79 struct TempCollector<'tcx> {
80 temps: IndexVec<Local, TempState>,
85 impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
86 fn visit_lvalue(&mut self,
87 lvalue: &Lvalue<'tcx>,
88 context: LvalueContext<'tcx>,
90 self.super_lvalue(lvalue, context, location);
91 if let Lvalue::Local(index) = *lvalue {
92 // We're only interested in temporaries
93 if self.mir.local_kind(index) != LocalKind::Temp {
97 // Ignore drops, if the temp gets promoted,
98 // then it's constant and thus drop is noop.
99 // Storage live ranges are also irrelevant.
100 if context.is_drop() || context.is_storage_marker() {
104 let temp = &mut self.temps[index];
105 if *temp == TempState::Undefined {
107 LvalueContext::Store |
108 LvalueContext::Call => {
109 *temp = TempState::Defined {
115 _ => { /* mark as unpromotable below */ }
117 } else if let TempState::Defined { ref mut uses, .. } = *temp {
118 // We always allow borrows, even mutable ones, as we need
119 // to promote mutable borrows of some ZSTs e.g. `&mut []`.
120 let allowed_use = match context {
121 LvalueContext::Borrow {..} => true,
122 _ => context.is_nonmutating_use()
128 /* mark as unpromotable below */
130 *temp = TempState::Unpromotable;
134 fn visit_source_info(&mut self, source_info: &SourceInfo) {
135 self.span = source_info.span;
139 pub fn collect_temps(mir: &Mir, rpo: &mut ReversePostorder) -> IndexVec<Local, TempState> {
140 let mut collector = TempCollector {
141 temps: IndexVec::from_elem(TempState::Undefined, &mir.local_decls),
145 for (bb, data) in rpo {
146 collector.visit_basic_block_data(bb, data);
151 struct Promoter<'a, 'tcx: 'a> {
152 source: &'a mut Mir<'tcx>,
154 temps: &'a mut IndexVec<Local, TempState>,
156 /// If true, all nested temps are also kept in the
157 /// source MIR, not moved to the promoted MIR.
161 impl<'a, 'tcx> Promoter<'a, 'tcx> {
162 fn new_block(&mut self) -> BasicBlock {
163 let span = self.promoted.span;
164 self.promoted.basic_blocks_mut().push(BasicBlockData {
166 terminator: Some(Terminator {
167 source_info: SourceInfo {
169 scope: ARGUMENT_VISIBILITY_SCOPE
171 kind: TerminatorKind::Return
177 fn assign(&mut self, dest: Local, rvalue: Rvalue<'tcx>, span: Span) {
178 let last = self.promoted.basic_blocks().last().unwrap();
179 let data = &mut self.promoted[last];
180 data.statements.push(Statement {
181 source_info: SourceInfo {
183 scope: ARGUMENT_VISIBILITY_SCOPE
185 kind: StatementKind::Assign(Lvalue::Local(dest), rvalue)
189 /// Copy the initialization of this temp to the
190 /// promoted MIR, recursing through temps.
191 fn promote_temp(&mut self, temp: Local) -> Local {
192 let old_keep_original = self.keep_original;
193 let loc = match self.temps[temp] {
194 TempState::Defined { location, uses } if uses > 0 => {
196 self.keep_original = true;
201 span_bug!(self.promoted.span, "{:?} not promotable: {:?}",
205 if !self.keep_original {
206 self.temps[temp] = TempState::PromotedOut;
209 let no_stmts = self.source[loc.block].statements.len();
210 let new_temp = self.promoted.local_decls.push(
211 LocalDecl::new_temp(self.source.local_decls[temp].ty,
212 self.source.local_decls[temp].source_info.span));
214 debug!("promote({:?} @ {:?}/{:?}, {:?})",
215 temp, loc, no_stmts, self.keep_original);
217 // First, take the Rvalue or Call out of the source MIR,
218 // or duplicate it, depending on keep_original.
219 if loc.statement_index < no_stmts {
220 let (mut rvalue, source_info) = {
221 let statement = &mut self.source[loc.block].statements[loc.statement_index];
222 let rhs = match statement.kind {
223 StatementKind::Assign(_, ref mut rhs) => rhs,
225 span_bug!(statement.source_info.span, "{:?} is not an assignment",
230 (if self.keep_original {
233 let unit = Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
234 mem::replace(rhs, unit)
235 }, statement.source_info)
238 self.visit_rvalue(&mut rvalue, loc);
239 self.assign(new_temp, rvalue, source_info.span);
241 let terminator = if self.keep_original {
242 self.source[loc.block].terminator().clone()
244 let terminator = self.source[loc.block].terminator_mut();
245 let target = match terminator.kind {
246 TerminatorKind::Call { destination: Some((_, target)), .. } => target,
248 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
252 source_info: terminator.source_info,
253 kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto {
259 match terminator.kind {
260 TerminatorKind::Call { mut func, mut args, .. } => {
261 self.visit_operand(&mut func, loc);
262 for arg in &mut args {
263 self.visit_operand(arg, loc);
266 let last = self.promoted.basic_blocks().last().unwrap();
267 let new_target = self.new_block();
269 *self.promoted[last].terminator_mut() = Terminator {
270 kind: TerminatorKind::Call {
274 destination: Some((Lvalue::Local(new_temp), new_target))
280 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
285 self.keep_original = old_keep_original;
289 fn promote_candidate(mut self, candidate: Candidate) {
290 let span = self.promoted.span;
291 let new_operand = Operand::Constant(box Constant {
293 ty: self.promoted.return_ty,
294 literal: Literal::Promoted {
295 index: Promoted::new(self.source.promoted.len())
298 let mut rvalue = match candidate {
299 Candidate::Ref(Location { block: bb, statement_index: stmt_idx }) => {
300 let ref mut statement = self.source[bb].statements[stmt_idx];
301 match statement.kind {
302 StatementKind::Assign(_, ref mut rvalue) => {
303 mem::replace(rvalue, Rvalue::Use(new_operand))
308 Candidate::ShuffleIndices(bb) => {
309 match self.source[bb].terminator_mut().kind {
310 TerminatorKind::Call { ref mut args, .. } => {
311 Rvalue::Use(mem::replace(&mut args[2], new_operand))
317 self.visit_rvalue(&mut rvalue, Location {
318 block: BasicBlock::new(0),
319 statement_index: usize::MAX
322 self.assign(RETURN_POINTER, rvalue, span);
323 self.source.promoted.push(self.promoted);
327 /// Replaces all temporaries with their promoted counterparts.
328 impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
329 fn visit_lvalue(&mut self,
330 lvalue: &mut Lvalue<'tcx>,
331 context: LvalueContext<'tcx>,
332 location: Location) {
333 if let Lvalue::Local(ref mut temp) = *lvalue {
334 if self.source.local_kind(*temp) == LocalKind::Temp {
335 *temp = self.promote_temp(*temp);
338 self.super_lvalue(lvalue, context, location);
342 pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
343 tcx: TyCtxt<'a, 'tcx, 'tcx>,
344 mut temps: IndexVec<Local, TempState>,
345 candidates: Vec<Candidate>) {
346 // Visit candidates in reverse, in case they're nested.
347 debug!("promote_candidates({:?})", candidates);
348 for candidate in candidates.into_iter().rev() {
349 let (span, ty) = match candidate {
350 Candidate::Ref(Location { block: bb, statement_index: stmt_idx }) => {
351 let statement = &mir[bb].statements[stmt_idx];
352 let dest = match statement.kind {
353 StatementKind::Assign(ref dest, _) => dest,
355 span_bug!(statement.source_info.span,
356 "expected assignment to promote");
359 if let Lvalue::Local(index) = *dest {
360 if temps[index] == TempState::PromotedOut {
365 (statement.source_info.span, dest.ty(mir, tcx).to_ty(tcx))
367 Candidate::ShuffleIndices(bb) => {
368 let terminator = mir[bb].terminator();
369 let ty = match terminator.kind {
370 TerminatorKind::Call { ref args, .. } => {
374 span_bug!(terminator.source_info.span,
375 "expected simd_shuffleN call to promote");
378 (terminator.source_info.span, ty)
382 // Declare return pointer local
383 let initial_locals = iter::once(LocalDecl::new_return_pointer(ty, span))
386 let mut promoter = Promoter {
389 Some(VisibilityScopeData {
392 }).into_iter().collect(),
404 assert_eq!(promoter.new_block(), START_BLOCK);
405 promoter.promote_candidate(candidate);
408 // Eliminate assignments to, and drops of promoted temps.
409 let promoted = |index: Local| temps[index] == TempState::PromotedOut;
410 for block in mir.basic_blocks_mut() {
411 block.statements.retain(|statement| {
412 match statement.kind {
413 StatementKind::Assign(Lvalue::Local(index), _) |
414 StatementKind::StorageLive(Lvalue::Local(index)) |
415 StatementKind::StorageDead(Lvalue::Local(index)) => {
421 let terminator = block.terminator_mut();
422 match terminator.kind {
423 TerminatorKind::Drop { location: Lvalue::Local(index), target, .. } => {
425 terminator.kind = TerminatorKind::Goto {