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 { .. } = *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>,
74 body: &'tcx Body<'tcx>,
77 impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
78 fn visit_local(&mut self,
80 context: PlaceContext,
82 debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
83 // We're only interested in temporaries and the return place
84 match self.body.local_kind(index) {
86 | LocalKind::ReturnPointer
93 // Ignore drops, if the temp gets promoted,
94 // then it's constant and thus drop is noop.
95 // Non-uses are also irrelevent.
96 if context.is_drop() || !context.is_use() {
98 "visit_local: context.is_drop={:?} context.is_use={:?}",
99 context.is_drop(), context.is_use(),
104 let temp = &mut self.temps[index];
105 debug!("visit_local: temp={:?}", temp);
106 if *temp == TempState::Undefined {
108 PlaceContext::MutatingUse(MutatingUseContext::Store) |
109 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
110 *temp = TempState::Defined {
116 _ => { /* mark as unpromotable below */ }
118 } else if let TempState::Defined { ref mut uses, .. } = *temp {
119 // We always allow borrows, even mutable ones, as we need
120 // to promote mutable borrows of some ZSTs e.g., `&mut []`.
121 let allowed_use = context.is_borrow() || context.is_nonmutating_use();
122 debug!("visit_local: allowed_use={:?}", allowed_use);
127 /* mark as unpromotable below */
129 *temp = TempState::Unpromotable;
132 fn visit_source_info(&mut self, source_info: &SourceInfo) {
133 self.span = source_info.span;
137 pub fn collect_temps(body: &Body<'_>,
138 rpo: &mut ReversePostorder<'_, '_>) -> IndexVec<Local, TempState> {
139 let mut collector = TempCollector {
140 temps: IndexVec::from_elem(TempState::Undefined, &body.local_decls),
144 for (bb, data) in rpo {
145 collector.visit_basic_block_data(bb, data);
150 struct Promoter<'a, 'tcx: 'a> {
151 tcx: TyCtxt<'a, 'tcx, 'tcx>,
152 source: &'a mut Body<'tcx>,
153 promoted: Body<'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: OUTERMOST_SOURCE_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: OUTERMOST_SOURCE_SCOPE
185 kind: StatementKind::Assign(Place::Base(PlaceBase::Local(dest)), box rvalue)
189 /// Copies 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 (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 = box Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
234 mem::replace(rhs, unit)
235 }, statement.source_info)
238 let mut rvalue = *rvalue;
239 self.visit_rvalue(&mut rvalue, loc);
240 self.assign(new_temp, rvalue, source_info.span);
242 let terminator = if self.keep_original {
243 self.source[loc.block].terminator().clone()
245 let terminator = self.source[loc.block].terminator_mut();
246 let target = match terminator.kind {
247 TerminatorKind::Call { destination: Some((_, target)), .. } => target,
249 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
253 source_info: terminator.source_info,
254 kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto {
260 match terminator.kind {
261 TerminatorKind::Call { mut func, mut args, from_hir_call, .. } => {
262 self.visit_operand(&mut func, loc);
263 for arg in &mut args {
264 self.visit_operand(arg, loc);
267 let last = self.promoted.basic_blocks().last().unwrap();
268 let new_target = self.new_block();
270 *self.promoted[last].terminator_mut() = Terminator {
271 kind: TerminatorKind::Call {
276 (Place::Base(PlaceBase::Local(new_temp)), new_target)
284 span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
289 self.keep_original = old_keep_original;
293 fn promote_candidate(mut self, candidate: Candidate) {
295 let promoted = &mut self.promoted;
296 let promoted_id = Promoted::new(self.source.promoted.len());
297 let mut promoted_place = |ty, span| {
298 promoted.span = span;
299 promoted.local_decls[RETURN_PLACE] = LocalDecl::new_return_place(ty, span);
301 PlaceBase::Static(box Static{ kind: StaticKind::Promoted(promoted_id), ty })
304 let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
306 Candidate::Ref(loc) => {
307 let ref mut statement = blocks[loc.block].statements[loc.statement_index];
308 match statement.kind {
309 StatementKind::Assign(_, box Rvalue::Ref(_, _, ref mut place)) => {
310 // Find the underlying local for this (necessarily interior) borrow.
311 let mut place = place;
312 while let Place::Projection(ref mut proj) = *place {
313 assert_ne!(proj.elem, ProjectionElem::Deref);
314 place = &mut proj.base;
317 let ty = place.ty(local_decls, self.tcx).ty;
318 let span = statement.source_info.span;
320 Operand::Move(mem::replace(place, promoted_place(ty, span)))
325 Candidate::Argument { bb, index } => {
326 let terminator = blocks[bb].terminator_mut();
327 match terminator.kind {
328 TerminatorKind::Call { ref mut args, .. } => {
329 let ty = args[index].ty(local_decls, self.tcx);
330 let span = terminator.source_info.span;
331 let operand = Operand::Copy(promoted_place(ty, span));
332 mem::replace(&mut args[index], operand)
334 // We expected a `TerminatorKind::Call` for which we'd like to promote an
335 // argument. `qualify_consts` saw a `TerminatorKind::Call` here, but
336 // we are seeing a `Goto`. That means that the `promote_temps` method
337 // already promoted this call away entirely. This case occurs when calling
338 // a function requiring a constant argument and as that constant value
339 // providing a value whose computation contains another call to a function
340 // requiring a constant argument.
341 TerminatorKind::Goto { .. } => return,
348 assert_eq!(self.new_block(), START_BLOCK);
349 self.visit_operand(&mut operand, Location {
350 block: BasicBlock::new(0),
351 statement_index: usize::MAX
354 let span = self.promoted.span;
355 self.assign(RETURN_PLACE, Rvalue::Use(operand), span);
356 self.source.promoted.push(self.promoted);
360 /// Replaces all temporaries with their promoted counterparts.
361 impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
362 fn visit_local(&mut self,
366 if self.source.local_kind(*local) == LocalKind::Temp {
367 *local = self.promote_temp(*local);
372 pub fn promote_candidates<'a, 'tcx>(body: &mut Body<'tcx>,
373 tcx: TyCtxt<'a, 'tcx, 'tcx>,
374 mut temps: IndexVec<Local, TempState>,
375 candidates: Vec<Candidate>) {
376 // Visit candidates in reverse, in case they're nested.
377 debug!("promote_candidates({:?})", candidates);
379 for candidate in candidates.into_iter().rev() {
381 Candidate::Ref(Location { block, statement_index }) => {
382 match body[block].statements[statement_index].kind {
383 StatementKind::Assign(Place::Base(PlaceBase::Local(local)), _) => {
384 if temps[local] == TempState::PromotedOut {
392 Candidate::Argument { .. } => {}
396 // Declare return place local so that `mir::Body::new` doesn't complain.
397 let initial_locals = iter::once(
398 LocalDecl::new_return_place(tcx.types.never, body.span)
401 let promoter = Promoter {
404 // FIXME: maybe try to filter this to avoid blowing up
406 body.source_scopes.clone(),
407 body.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 body.basic_blocks_mut() {
428 block.statements.retain(|statement| {
429 match statement.kind {
430 StatementKind::Assign(Place::Base(PlaceBase::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::Base(PlaceBase::Local(index)), target, .. } => {
442 terminator.kind = TerminatorKind::Goto {