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.
16 use rustc::mir::visit::{PlaceContext, MutatingUseContext, MutVisitor, Visitor};
17 use rustc::mir::traversal::ReversePostorder;
18 use rustc::ty::TyCtxt;
21 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
23 use std::{iter, mem, usize};
25 /// State of a temporary during collection and promotion.
26 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
28 /// No references to this temp.
30 /// One direct assignment and any number of direct uses.
31 /// A borrow of this temp is promotable if the assigned
32 /// value is qualified as constant.
37 /// Any other combination of assignments/uses.
39 /// This temp was part of an rvalue which got extracted
40 /// during promotion and needs cleanup.
45 pub fn is_promotable(&self) -> bool {
46 debug!("is_promotable: self={:?}", self);
47 if let TempState::Defined { uses, .. } = *self {
55 /// A "root candidate" for promotion, which will become the
56 /// returned value in a promoted MIR, unless it's a subset
57 /// of a larger candidate.
60 /// Borrow of a constant temporary.
63 /// Currently applied to function calls where the callee has the unstable
64 /// `#[rustc_args_required_const]` attribute as well as the SIMD shuffle
65 /// intrinsic. The intrinsic requires the arguments are indeed constant and
66 /// the attribute currently provides the semantic requirement that arguments
68 Argument { bb: BasicBlock, index: usize },
71 struct TempCollector<'tcx> {
72 temps: IndexVec<Local, TempState>,
77 impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
78 fn visit_local(&mut self,
80 context: PlaceContext<'tcx>,
82 debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
83 // We're only interested in temporaries
84 if self.mir.local_kind(index) != LocalKind::Temp {
88 // Ignore drops, if the temp gets promoted,
89 // then it's constant and thus drop is noop.
90 // Non-uses are also irrelevent.
91 if context.is_drop() || !context.is_use() {
93 "visit_local: context.is_drop={:?} context.is_use={:?}",
94 context.is_drop(), context.is_use(),
99 let temp = &mut self.temps[index];
100 debug!("visit_local: temp={:?}", temp);
101 if *temp == TempState::Undefined {
103 PlaceContext::MutatingUse(MutatingUseContext::Store) |
104 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput) |
105 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
106 *temp = TempState::Defined {
112 _ => { /* mark as unpromotable below */ }
114 } else if let TempState::Defined { ref mut uses, .. } = *temp {
115 // We always allow borrows, even mutable ones, as we need
116 // to promote mutable borrows of some ZSTs e.g., `&mut []`.
117 let allowed_use = context.is_borrow() || context.is_nonmutating_use();
118 debug!("visit_local: allowed_use={:?}", allowed_use);
123 /* mark as unpromotable below */
125 *temp = TempState::Unpromotable;
128 fn visit_source_info(&mut self, source_info: &SourceInfo) {
129 self.span = source_info.span;
133 pub fn collect_temps(mir: &Mir<'_>,
134 rpo: &mut ReversePostorder<'_, '_>) -> IndexVec<Local, TempState> {
135 let mut collector = TempCollector {
136 temps: IndexVec::from_elem(TempState::Undefined, &mir.local_decls),
140 for (bb, data) in rpo {
141 collector.visit_basic_block_data(bb, data);
146 struct Promoter<'a, 'tcx: 'a> {
147 tcx: TyCtxt<'a, 'tcx, 'tcx>,
148 source: &'a mut Mir<'tcx>,
150 temps: &'a mut IndexVec<Local, TempState>,
152 /// If true, all nested temps are also kept in the
153 /// source MIR, not moved to the promoted MIR.
157 impl<'a, 'tcx> Promoter<'a, 'tcx> {
158 fn new_block(&mut self) -> BasicBlock {
159 let span = self.promoted.span;
160 self.promoted.basic_blocks_mut().push(BasicBlockData {
162 terminator: Some(Terminator {
163 source_info: SourceInfo {
165 scope: OUTERMOST_SOURCE_SCOPE
167 kind: TerminatorKind::Return
173 fn assign(&mut self, dest: Local, rvalue: Rvalue<'tcx>, span: Span) {
174 let last = self.promoted.basic_blocks().last().unwrap();
175 let data = &mut self.promoted[last];
176 data.statements.push(Statement {
177 source_info: SourceInfo {
179 scope: OUTERMOST_SOURCE_SCOPE
181 kind: StatementKind::Assign(Place::Local(dest), box rvalue)
185 /// Copy the initialization of this temp to the
186 /// promoted MIR, recursing through temps.
187 fn promote_temp(&mut self, temp: Local) -> Local {
188 let old_keep_original = self.keep_original;
189 let loc = match self.temps[temp] {
190 TempState::Defined { location, uses } if uses > 0 => {
192 self.keep_original = true;
197 span_bug!(self.promoted.span, "{:?} not promotable: {:?}",
201 if !self.keep_original {
202 self.temps[temp] = TempState::PromotedOut;
205 let no_stmts = self.source[loc.block].statements.len();
206 let new_temp = self.promoted.local_decls.push(
207 LocalDecl::new_temp(self.source.local_decls[temp].ty,
208 self.source.local_decls[temp].source_info.span));
210 debug!("promote({:?} @ {:?}/{:?}, {:?})",
211 temp, loc, no_stmts, self.keep_original);
213 // First, take the Rvalue or Call out of the source MIR,
214 // or duplicate it, depending on keep_original.
215 if loc.statement_index < no_stmts {
216 let (rvalue, source_info) = {
217 let statement = &mut self.source[loc.block].statements[loc.statement_index];
218 let rhs = match statement.kind {
219 StatementKind::Assign(_, ref mut rhs) => rhs,
221 span_bug!(statement.source_info.span, "{:?} is not an assignment",
226 (if self.keep_original {
229 let unit = box Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
230 mem::replace(rhs, unit)
231 }, statement.source_info)
234 let mut rvalue = *rvalue;
235 self.visit_rvalue(&mut rvalue, loc);
236 self.assign(new_temp, rvalue, source_info.span);
238 let terminator = if self.keep_original {
239 self.source[loc.block].terminator().clone()
241 let terminator = self.source[loc.block].terminator_mut();
242 let target = match terminator.kind {
243 TerminatorKind::Call { destination: Some((_, target)), .. } => target,
245 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
249 source_info: terminator.source_info,
250 kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto {
256 match terminator.kind {
257 TerminatorKind::Call { mut func, mut args, from_hir_call, .. } => {
258 self.visit_operand(&mut func, loc);
259 for arg in &mut args {
260 self.visit_operand(arg, loc);
263 let last = self.promoted.basic_blocks().last().unwrap();
264 let new_target = self.new_block();
266 *self.promoted[last].terminator_mut() = Terminator {
267 kind: TerminatorKind::Call {
271 destination: Some((Place::Local(new_temp), new_target)),
278 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
283 self.keep_original = old_keep_original;
287 fn promote_candidate(mut self, candidate: Candidate) {
289 let promoted = &mut self.promoted;
290 let promoted_id = Promoted::new(self.source.promoted.len());
291 let mut promoted_place = |ty, span| {
292 promoted.span = span;
293 promoted.local_decls[RETURN_PLACE] =
294 LocalDecl::new_return_place(ty, span);
295 Place::Promoted(box (promoted_id, ty))
297 let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
299 Candidate::Ref(loc) => {
300 let ref mut statement = blocks[loc.block].statements[loc.statement_index];
301 match statement.kind {
302 StatementKind::Assign(_, box Rvalue::Ref(_, _, ref mut place)) => {
303 // Find the underlying local for this (necessarily interior) borrow.
304 let mut place = place;
305 while let Place::Projection(ref mut proj) = *place {
306 assert_ne!(proj.elem, ProjectionElem::Deref);
307 place = &mut proj.base;
310 let ty = place.ty(local_decls, self.tcx).to_ty(self.tcx);
311 let span = statement.source_info.span;
313 Operand::Move(mem::replace(place, promoted_place(ty, span)))
318 Candidate::Argument { bb, index } => {
319 let terminator = blocks[bb].terminator_mut();
320 match terminator.kind {
321 TerminatorKind::Call { ref mut args, .. } => {
322 let ty = args[index].ty(local_decls, self.tcx);
323 let span = terminator.source_info.span;
324 let operand = Operand::Copy(promoted_place(ty, span));
325 mem::replace(&mut args[index], operand)
327 // We expected a `TerminatorKind::Call` for which we'd like to promote an
328 // argument. `qualify_consts` saw a `TerminatorKind::Call` here, but
329 // we are seeing a `Goto`. That means that the `promote_temps` method
330 // already promoted this call away entirely. This case occurs when calling
331 // a function requiring a constant argument and as that constant value
332 // providing a value whose computation contains another call to a function
333 // requiring a constant argument.
334 TerminatorKind::Goto { .. } => return,
341 assert_eq!(self.new_block(), START_BLOCK);
342 self.visit_operand(&mut operand, Location {
343 block: BasicBlock::new(0),
344 statement_index: usize::MAX
347 let span = self.promoted.span;
348 self.assign(RETURN_PLACE, Rvalue::Use(operand), span);
349 self.source.promoted.push(self.promoted);
353 /// Replaces all temporaries with their promoted counterparts.
354 impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
355 fn visit_local(&mut self,
357 _: PlaceContext<'tcx>,
359 if self.source.local_kind(*local) == LocalKind::Temp {
360 *local = self.promote_temp(*local);
365 pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
366 tcx: TyCtxt<'a, 'tcx, 'tcx>,
367 mut temps: IndexVec<Local, TempState>,
368 candidates: Vec<Candidate>) {
369 // Visit candidates in reverse, in case they're nested.
370 debug!("promote_candidates({:?})", candidates);
372 for candidate in candidates.into_iter().rev() {
374 Candidate::Ref(Location { block, statement_index }) => {
375 match mir[block].statements[statement_index].kind {
376 StatementKind::Assign(Place::Local(local), _) => {
377 if temps[local] == TempState::PromotedOut {
385 Candidate::Argument { .. } => {}
389 // Declare return place local so that `Mir::new` doesn't complain.
390 let initial_locals = iter::once(
391 LocalDecl::new_return_place(tcx.types.never, mir.span)
394 let promoter = Promoter {
397 // FIXME: maybe try to filter this to avoid blowing up
399 mir.source_scopes.clone(),
400 mir.source_scope_local_data.clone(),
415 promoter.promote_candidate(candidate);
418 // Eliminate assignments to, and drops of promoted temps.
419 let promoted = |index: Local| temps[index] == TempState::PromotedOut;
420 for block in mir.basic_blocks_mut() {
421 block.statements.retain(|statement| {
422 match statement.kind {
423 StatementKind::Assign(Place::Local(index), _) |
424 StatementKind::StorageLive(index) |
425 StatementKind::StorageDead(index) => {
431 let terminator = block.terminator_mut();
432 match terminator.kind {
433 TerminatorKind::Drop { location: Place::Local(index), target, .. } => {
435 terminator.kind = TerminatorKind::Goto {