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.
25 use rustc::mir::repr::*;
26 use rustc::mir::visit::{LvalueContext, MutVisitor, Visitor};
27 use rustc::mir::traversal::ReversePostorder;
28 use rustc::ty::TyCtxt;
33 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.
70 /// Borrow of a constant temporary.
73 /// Array of indices found in the third argument of
74 /// a call to one of the simd_shuffleN intrinsics.
75 ShuffleIndices(BasicBlock)
78 struct TempCollector {
79 temps: IndexVec<Temp, TempState>,
84 impl<'tcx> Visitor<'tcx> for TempCollector {
85 fn visit_lvalue(&mut self, lvalue: &Lvalue<'tcx>, context: LvalueContext) {
86 self.super_lvalue(lvalue, context);
87 if let Lvalue::Temp(index) = *lvalue {
88 // Ignore drops, if the temp gets promoted,
89 // then it's constant and thus drop is noop.
90 // Storage live ranges are also irrelevant.
93 LvalueContext::StorageLive |
94 LvalueContext::StorageDead => return,
98 let temp = &mut self.temps[index];
99 if *temp == TempState::Undefined {
101 LvalueContext::Store |
102 LvalueContext::Call => {
103 *temp = TempState::Defined {
104 location: self.location,
109 _ => { /* mark as unpromotable below */ }
111 } else if let TempState::Defined { ref mut uses, .. } = *temp {
113 LvalueContext::Borrow {..} |
114 LvalueContext::Consume |
115 LvalueContext::Inspect => {
119 _ => { /* mark as unpromotable below */ }
122 *temp = TempState::Unpromotable;
126 fn visit_source_info(&mut self, source_info: &SourceInfo) {
127 self.span = source_info.span;
130 fn visit_statement(&mut self, bb: BasicBlock, statement: &Statement<'tcx>) {
131 assert_eq!(self.location.block, bb);
132 self.super_statement(bb, statement);
133 self.location.statement_index += 1;
136 fn visit_basic_block_data(&mut self, bb: BasicBlock, data: &BasicBlockData<'tcx>) {
137 self.location.statement_index = 0;
138 self.location.block = bb;
139 self.super_basic_block_data(bb, data);
143 pub fn collect_temps(mir: &Mir, rpo: &mut ReversePostorder) -> IndexVec<Temp, TempState> {
144 let mut collector = TempCollector {
145 temps: IndexVec::from_elem(TempState::Undefined, &mir.temp_decls),
152 for (bb, data) in rpo {
153 collector.visit_basic_block_data(bb, data);
158 struct Promoter<'a, 'tcx: 'a> {
159 source: &'a mut Mir<'tcx>,
161 temps: &'a mut IndexVec<Temp, TempState>,
163 /// If true, all nested temps are also kept in the
164 /// source MIR, not moved to the promoted MIR.
168 impl<'a, 'tcx> Promoter<'a, 'tcx> {
169 fn new_block(&mut self) -> BasicBlock {
170 let span = self.promoted.span;
171 self.promoted.basic_blocks_mut().push(BasicBlockData {
173 terminator: Some(Terminator {
174 source_info: SourceInfo {
176 scope: ARGUMENT_VISIBILITY_SCOPE
178 kind: TerminatorKind::Return
184 fn assign(&mut self, dest: Lvalue<'tcx>, rvalue: Rvalue<'tcx>, span: Span) {
185 let last = self.promoted.basic_blocks().last().unwrap();
186 let data = &mut self.promoted[last];
187 data.statements.push(Statement {
188 source_info: SourceInfo {
190 scope: ARGUMENT_VISIBILITY_SCOPE
192 kind: StatementKind::Assign(dest, rvalue)
196 /// Copy the initialization of this temp to the
197 /// promoted MIR, recursing through temps.
198 fn promote_temp(&mut self, temp: Temp) -> Temp {
199 let old_keep_original = self.keep_original;
200 let (bb, stmt_idx) = match self.temps[temp] {
202 location: Location { block, statement_index },
206 self.keep_original = true;
208 (block, statement_index)
211 span_bug!(self.promoted.span, "{:?} not promotable: {:?}",
215 if !self.keep_original {
216 self.temps[temp] = TempState::PromotedOut;
219 let no_stmts = self.source[bb].statements.len();
221 // First, take the Rvalue or Call out of the source MIR,
222 // or duplicate it, depending on keep_original.
223 let (mut rvalue, mut call) = (None, None);
224 let source_info = if stmt_idx < no_stmts {
225 let statement = &mut self.source[bb].statements[stmt_idx];
226 let rhs = match statement.kind {
227 StatementKind::Assign(_, ref mut rhs) => rhs,
229 span_bug!(statement.source_info.span, "{:?} is not an assignment",
233 if self.keep_original {
234 rvalue = Some(rhs.clone());
236 let unit = Rvalue::Aggregate(AggregateKind::Tuple, vec![]);
237 rvalue = Some(mem::replace(rhs, unit));
239 statement.source_info
240 } else if self.keep_original {
241 let terminator = self.source[bb].terminator().clone();
242 call = Some(terminator.kind);
243 terminator.source_info
245 let terminator = self.source[bb].terminator_mut();
246 let target = match terminator.kind {
247 TerminatorKind::Call {
248 destination: ref mut dest @ Some(_),
251 // No cleanup necessary.
254 // We'll put a new destination in later.
255 dest.take().unwrap().1
258 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
261 call = Some(mem::replace(&mut terminator.kind, TerminatorKind::Goto {
264 terminator.source_info
267 // Then, recurse for components in the Rvalue or Call.
268 if stmt_idx < no_stmts {
269 self.visit_rvalue(rvalue.as_mut().unwrap());
271 self.visit_terminator_kind(bb, call.as_mut().unwrap());
274 let new_temp = self.promoted.temp_decls.push(TempDecl {
275 ty: self.source.temp_decls[temp].ty
278 // Inject the Rvalue or Call into the promoted MIR.
279 if stmt_idx < no_stmts {
280 self.assign(Lvalue::Temp(new_temp), rvalue.unwrap(), source_info.span);
282 let last = self.promoted.basic_blocks().last().unwrap();
283 let new_target = self.new_block();
284 let mut call = call.unwrap();
286 TerminatorKind::Call { ref mut destination, ..} => {
287 *destination = Some((Lvalue::Temp(new_temp), new_target));
291 let terminator = self.promoted[last].terminator_mut();
292 terminator.source_info.span = source_info.span;
293 terminator.kind = call;
296 // Restore the old duplication state.
297 self.keep_original = old_keep_original;
302 fn promote_candidate(mut self, candidate: Candidate) {
303 let span = self.promoted.span;
304 let new_operand = Operand::Constant(Constant {
306 ty: self.promoted.return_ty,
307 literal: Literal::Promoted {
308 index: Promoted::new(self.source.promoted.len())
311 let mut rvalue = match candidate {
312 Candidate::Ref(Location { block: bb, statement_index: stmt_idx }) => {
313 let ref mut statement = self.source[bb].statements[stmt_idx];
314 match statement.kind {
315 StatementKind::Assign(_, ref mut rvalue) => {
316 mem::replace(rvalue, Rvalue::Use(new_operand))
321 Candidate::ShuffleIndices(bb) => {
322 match self.source[bb].terminator_mut().kind {
323 TerminatorKind::Call { ref mut args, .. } => {
324 Rvalue::Use(mem::replace(&mut args[2], new_operand))
330 self.visit_rvalue(&mut rvalue);
331 self.assign(Lvalue::ReturnPointer, rvalue, span);
332 self.source.promoted.push(self.promoted);
336 /// Replaces all temporaries with their promoted counterparts.
337 impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
338 fn visit_lvalue(&mut self, lvalue: &mut Lvalue<'tcx>, context: LvalueContext) {
339 if let Lvalue::Temp(ref mut temp) = *lvalue {
340 *temp = self.promote_temp(*temp);
342 self.super_lvalue(lvalue, context);
346 pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
347 tcx: TyCtxt<'a, 'tcx, 'tcx>,
348 mut temps: IndexVec<Temp, TempState>,
349 candidates: Vec<Candidate>) {
350 // Visit candidates in reverse, in case they're nested.
351 for candidate in candidates.into_iter().rev() {
352 let (span, ty) = match candidate {
353 Candidate::Ref(Location { block: bb, statement_index: stmt_idx }) => {
354 let statement = &mir[bb].statements[stmt_idx];
355 let dest = match statement.kind {
356 StatementKind::Assign(ref dest, _) => dest,
358 span_bug!(statement.source_info.span,
359 "expected assignment to promote");
362 if let Lvalue::Temp(index) = *dest {
363 if temps[index] == TempState::PromotedOut {
368 (statement.source_info.span, dest.ty(mir, tcx).to_ty(tcx))
370 Candidate::ShuffleIndices(bb) => {
371 let terminator = mir[bb].terminator();
372 let ty = match terminator.kind {
373 TerminatorKind::Call { ref args, .. } => {
377 span_bug!(terminator.source_info.span,
378 "expected simd_shuffleN call to promote");
381 (terminator.source_info.span, ty)
385 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: Temp| 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::Temp(index), _) |
414 StatementKind::StorageLive(Lvalue::Temp(index)) |
415 StatementKind::StorageDead(Lvalue::Temp(index)) => {
421 let terminator = block.terminator_mut();
422 match terminator.kind {
423 TerminatorKind::Drop { location: Lvalue::Temp(index), target, .. } => {
425 terminator.kind = TerminatorKind::Goto {