]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/promote_consts.rs
Rollup merge of #65518 - estebank:i-want-to-break-free, r=eddyb
[rust.git] / src / librustc_mir / transform / promote_consts.rs
1 //! A pass that promotes borrows of constant rvalues.
2 //!
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.
10 //!
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.
14
15 use rustc::hir::def_id::DefId;
16 use rustc::mir::*;
17 use rustc::mir::visit::{PlaceContext, MutatingUseContext, MutVisitor, Visitor};
18 use rustc::mir::traversal::ReversePostorder;
19 use rustc::ty::subst::InternalSubsts;
20 use rustc::ty::TyCtxt;
21 use syntax_pos::Span;
22
23 use rustc_index::vec::{IndexVec, Idx};
24
25 use std::{iter, mem, usize};
26
27 /// State of a temporary during collection and promotion.
28 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
29 pub enum TempState {
30     /// No references to this temp.
31     Undefined,
32     /// One direct assignment and any number of direct uses.
33     /// A borrow of this temp is promotable if the assigned
34     /// value is qualified as constant.
35     Defined {
36         location: Location,
37         uses: usize
38     },
39     /// Any other combination of assignments/uses.
40     Unpromotable,
41     /// This temp was part of an rvalue which got extracted
42     /// during promotion and needs cleanup.
43     PromotedOut
44 }
45
46 impl TempState {
47     pub fn is_promotable(&self) -> bool {
48         debug!("is_promotable: self={:?}", self);
49         if let TempState::Defined { .. } = *self {
50             true
51         } else {
52             false
53         }
54     }
55 }
56
57 /// A "root candidate" for promotion, which will become the
58 /// returned value in a promoted MIR, unless it's a subset
59 /// of a larger candidate.
60 #[derive(Debug)]
61 pub enum Candidate {
62     /// Borrow of a constant temporary.
63     Ref(Location),
64
65     /// Promotion of the `x` in `[x; 32]`.
66     Repeat(Location),
67
68     /// Currently applied to function calls where the callee has the unstable
69     /// `#[rustc_args_required_const]` attribute as well as the SIMD shuffle
70     /// intrinsic. The intrinsic requires the arguments are indeed constant and
71     /// the attribute currently provides the semantic requirement that arguments
72     /// must be constant.
73     Argument { bb: BasicBlock, index: usize },
74 }
75
76 struct TempCollector<'tcx> {
77     temps: IndexVec<Local, TempState>,
78     span: Span,
79     body: &'tcx Body<'tcx>,
80 }
81
82 impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
83     fn visit_local(&mut self,
84                    &index: &Local,
85                    context: PlaceContext,
86                    location: Location) {
87         debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
88         // We're only interested in temporaries and the return place
89         match self.body.local_kind(index) {
90             | LocalKind::Temp
91             | LocalKind::ReturnPointer
92             => {},
93             | LocalKind::Arg
94             | LocalKind::Var
95             => return,
96         }
97
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() {
102             debug!(
103                 "visit_local: context.is_drop={:?} context.is_use={:?}",
104                 context.is_drop(), context.is_use(),
105             );
106             return;
107         }
108
109         let temp = &mut self.temps[index];
110         debug!("visit_local: temp={:?}", temp);
111         if *temp == TempState::Undefined {
112             match context {
113                 PlaceContext::MutatingUse(MutatingUseContext::Store) |
114                 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
115                     *temp = TempState::Defined {
116                         location,
117                         uses: 0
118                     };
119                     return;
120                 }
121                 _ => { /* mark as unpromotable below */ }
122             }
123         } else if let TempState::Defined { ref mut uses, .. } = *temp {
124             // We always allow borrows, even mutable ones, as we need
125             // to promote mutable borrows of some ZSTs e.g., `&mut []`.
126             let allowed_use = context.is_borrow() || context.is_nonmutating_use();
127             debug!("visit_local: allowed_use={:?}", allowed_use);
128             if allowed_use {
129                 *uses += 1;
130                 return;
131             }
132             /* mark as unpromotable below */
133         }
134         *temp = TempState::Unpromotable;
135     }
136
137     fn visit_source_info(&mut self, source_info: &SourceInfo) {
138         self.span = source_info.span;
139     }
140 }
141
142 pub fn collect_temps(body: &Body<'_>,
143                      rpo: &mut ReversePostorder<'_, '_>) -> IndexVec<Local, TempState> {
144     let mut collector = TempCollector {
145         temps: IndexVec::from_elem(TempState::Undefined, &body.local_decls),
146         span: body.span,
147         body,
148     };
149     for (bb, data) in rpo {
150         collector.visit_basic_block_data(bb, data);
151     }
152     collector.temps
153 }
154
155 struct Promoter<'a, 'tcx> {
156     tcx: TyCtxt<'tcx>,
157     source: &'a mut Body<'tcx>,
158     promoted: Body<'tcx>,
159     temps: &'a mut IndexVec<Local, TempState>,
160
161     /// If true, all nested temps are also kept in the
162     /// source MIR, not moved to the promoted MIR.
163     keep_original: bool,
164 }
165
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 {
170             statements: vec![],
171             terminator: Some(Terminator {
172                 source_info: SourceInfo {
173                     span,
174                     scope: OUTERMOST_SOURCE_SCOPE
175                 },
176                 kind: TerminatorKind::Return
177             }),
178             is_cleanup: false
179         })
180     }
181
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 {
187                 span,
188                 scope: OUTERMOST_SOURCE_SCOPE
189             },
190             kind: StatementKind::Assign(box(Place::from(dest), rvalue))
191         });
192     }
193
194     fn is_temp_kind(&self, local: Local) -> bool {
195         self.source.local_kind(local) == LocalKind::Temp
196     }
197
198     /// Copies the initialization of this temp to the
199     /// promoted MIR, recursing through temps.
200     fn promote_temp(&mut self, temp: Local) -> Local {
201         let old_keep_original = self.keep_original;
202         let loc = match self.temps[temp] {
203             TempState::Defined { location, uses } if uses > 0 => {
204                 if uses > 1 {
205                     self.keep_original = true;
206                 }
207                 location
208             }
209             state =>  {
210                 span_bug!(self.promoted.span, "{:?} not promotable: {:?}",
211                           temp, state);
212             }
213         };
214         if !self.keep_original {
215             self.temps[temp] = TempState::PromotedOut;
216         }
217
218         let no_stmts = self.source[loc.block].statements.len();
219         let new_temp = self.promoted.local_decls.push(
220             LocalDecl::new_temp(self.source.local_decls[temp].ty,
221                                 self.source.local_decls[temp].source_info.span));
222
223         debug!("promote({:?} @ {:?}/{:?}, {:?})",
224                temp, loc, no_stmts, self.keep_original);
225
226         // First, take the Rvalue or Call out of the source MIR,
227         // or duplicate it, depending on keep_original.
228         if loc.statement_index < no_stmts {
229             let (mut rvalue, source_info) = {
230                 let statement = &mut self.source[loc.block].statements[loc.statement_index];
231                 let rhs = match statement.kind {
232                     StatementKind::Assign(box(_, ref mut rhs)) => rhs,
233                     _ => {
234                         span_bug!(statement.source_info.span, "{:?} is not an assignment",
235                                   statement);
236                     }
237                 };
238
239                 (if self.keep_original {
240                     rhs.clone()
241                 } else {
242                     let unit = Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
243                     mem::replace(rhs, unit)
244                 }, statement.source_info)
245             };
246
247             self.visit_rvalue(&mut rvalue, loc);
248             self.assign(new_temp, rvalue, source_info.span);
249         } else {
250             let terminator = if self.keep_original {
251                 self.source[loc.block].terminator().clone()
252             } else {
253                 let terminator = self.source[loc.block].terminator_mut();
254                 let target = match terminator.kind {
255                     TerminatorKind::Call { destination: Some((_, target)), .. } => target,
256                     ref kind => {
257                         span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
258                     }
259                 };
260                 Terminator {
261                     source_info: terminator.source_info,
262                     kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto {
263                         target,
264                     })
265                 }
266             };
267
268             match terminator.kind {
269                 TerminatorKind::Call { mut func, mut args, from_hir_call, .. } => {
270                     self.visit_operand(&mut func, loc);
271                     for arg in &mut args {
272                         self.visit_operand(arg, loc);
273                     }
274
275                     let last = self.promoted.basic_blocks().last().unwrap();
276                     let new_target = self.new_block();
277
278                     *self.promoted[last].terminator_mut() = Terminator {
279                         kind: TerminatorKind::Call {
280                             func,
281                             args,
282                             cleanup: None,
283                             destination: Some(
284                                 (Place::from(new_temp), new_target)
285                             ),
286                             from_hir_call,
287                         },
288                         ..terminator
289                     };
290                 }
291                 ref kind => {
292                     span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
293                 }
294             };
295         };
296
297         self.keep_original = old_keep_original;
298         new_temp
299     }
300
301     fn promote_candidate(
302         mut self,
303         def_id: DefId,
304         candidate: Candidate,
305         next_promoted_id: usize,
306     ) -> Option<Body<'tcx>> {
307         let mut operand = {
308             let promoted = &mut self.promoted;
309             let promoted_id = Promoted::new(next_promoted_id);
310             let tcx = self.tcx;
311             let mut promoted_place = |ty, span| {
312                 promoted.span = span;
313                 promoted.local_decls[RETURN_PLACE] = LocalDecl::new_return_place(ty, span);
314                 Place {
315                     base: PlaceBase::Static(box Static {
316                         kind:
317                             StaticKind::Promoted(
318                                 promoted_id,
319                                 InternalSubsts::identity_for_item(tcx, def_id),
320                             ),
321                         ty,
322                         def_id,
323                     }),
324                     projection: box [],
325                 }
326             };
327             let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
328             match candidate {
329                 Candidate::Ref(loc) => {
330                     let ref mut statement = blocks[loc.block].statements[loc.statement_index];
331                     match statement.kind {
332                         StatementKind::Assign(box(_, Rvalue::Ref(_, _, ref mut place))) => {
333                             // Use the underlying local for this (necessarily interior) borrow.
334                             let ty = place.base.ty(local_decls).ty;
335                             let span = statement.source_info.span;
336
337                             Operand::Move(Place {
338                                 base: mem::replace(
339                                     &mut place.base,
340                                     promoted_place(ty, span).base,
341                                 ),
342                                 projection: box [],
343                             })
344                         }
345                         _ => bug!()
346                     }
347                 }
348                 Candidate::Repeat(loc) => {
349                     let ref mut statement = blocks[loc.block].statements[loc.statement_index];
350                     match statement.kind {
351                         StatementKind::Assign(box(_, Rvalue::Repeat(ref mut operand, _))) => {
352                             let ty = operand.ty(local_decls, self.tcx);
353                             let span = statement.source_info.span;
354                             mem::replace(
355                                 operand,
356                                 Operand::Copy(promoted_place(ty, span))
357                             )
358                         }
359                         _ => bug!()
360                     }
361                 },
362                 Candidate::Argument { bb, index } => {
363                     let terminator = blocks[bb].terminator_mut();
364                     match terminator.kind {
365                         TerminatorKind::Call { ref mut args, .. } => {
366                             let ty = args[index].ty(local_decls, self.tcx);
367                             let span = terminator.source_info.span;
368                             let operand = Operand::Copy(promoted_place(ty, span));
369                             mem::replace(&mut args[index], operand)
370                         }
371                         // We expected a `TerminatorKind::Call` for which we'd like to promote an
372                         // argument. `qualify_consts` saw a `TerminatorKind::Call` here, but
373                         // we are seeing a `Goto`. That means that the `promote_temps` method
374                         // already promoted this call away entirely. This case occurs when calling
375                         // a function requiring a constant argument and as that constant value
376                         // providing a value whose computation contains another call to a function
377                         // requiring a constant argument.
378                         TerminatorKind::Goto { .. } => return None,
379                         _ => bug!()
380                     }
381                 }
382             }
383         };
384
385         assert_eq!(self.new_block(), START_BLOCK);
386         self.visit_operand(&mut operand, Location {
387             block: BasicBlock::new(0),
388             statement_index: usize::MAX
389         });
390
391         let span = self.promoted.span;
392         self.assign(RETURN_PLACE, Rvalue::Use(operand), span);
393         Some(self.promoted)
394     }
395 }
396
397 /// Replaces all temporaries with their promoted counterparts.
398 impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
399     fn visit_local(&mut self,
400                    local: &mut Local,
401                    _: PlaceContext,
402                    _: Location) {
403         if self.is_temp_kind(*local) {
404             *local = self.promote_temp(*local);
405         }
406     }
407
408     fn process_projection_elem(
409         &mut self,
410         elem: &PlaceElem<'tcx>,
411     ) -> Option<PlaceElem<'tcx>> {
412         match elem {
413             PlaceElem::Index(local) if self.is_temp_kind(*local) => {
414                 Some(PlaceElem::Index(self.promote_temp(*local)))
415             }
416             _ => None,
417         }
418     }
419 }
420
421 pub fn promote_candidates<'tcx>(
422     def_id: DefId,
423     body: &mut Body<'tcx>,
424     tcx: TyCtxt<'tcx>,
425     mut temps: IndexVec<Local, TempState>,
426     candidates: Vec<Candidate>,
427 ) -> IndexVec<Promoted, Body<'tcx>> {
428     // Visit candidates in reverse, in case they're nested.
429     debug!("promote_candidates({:?})", candidates);
430
431     let mut promotions = IndexVec::new();
432
433     for candidate in candidates.into_iter().rev() {
434         match candidate {
435             Candidate::Repeat(Location { block, statement_index }) |
436             Candidate::Ref(Location { block, statement_index }) => {
437                 match body[block].statements[statement_index].kind {
438                     StatementKind::Assign(box(Place {
439                         base: PlaceBase::Local(local),
440                         projection: box [],
441                     }, _)) => {
442                         if temps[local] == TempState::PromotedOut {
443                             // Already promoted.
444                             continue;
445                         }
446                     }
447                     _ => {}
448                 }
449             }
450             Candidate::Argument { .. } => {}
451         }
452
453
454         // Declare return place local so that `mir::Body::new` doesn't complain.
455         let initial_locals = iter::once(
456             LocalDecl::new_return_place(tcx.types.never, body.span)
457         ).collect();
458
459         let promoter = Promoter {
460             promoted: Body::new(
461                 IndexVec::new(),
462                 // FIXME: maybe try to filter this to avoid blowing up
463                 // memory usage?
464                 body.source_scopes.clone(),
465                 body.source_scope_local_data.clone(),
466                 None,
467                 initial_locals,
468                 IndexVec::new(),
469                 0,
470                 vec![],
471                 body.span,
472                 vec![],
473             ),
474             tcx,
475             source: body,
476             temps: &mut temps,
477             keep_original: false
478         };
479
480         //FIXME(oli-obk): having a `maybe_push()` method on `IndexVec` might be nice
481         if let Some(promoted) = promoter.promote_candidate(def_id, candidate, promotions.len()) {
482             promotions.push(promoted);
483         }
484     }
485
486     // Eliminate assignments to, and drops of promoted temps.
487     let promoted = |index: Local| temps[index] == TempState::PromotedOut;
488     for block in body.basic_blocks_mut() {
489         block.statements.retain(|statement| {
490             match statement.kind {
491                 StatementKind::Assign(box(Place {
492                     base: PlaceBase::Local(index),
493                     projection: box [],
494                 }, _)) |
495                 StatementKind::StorageLive(index) |
496                 StatementKind::StorageDead(index) => {
497                     !promoted(index)
498                 }
499                 _ => true
500             }
501         });
502         let terminator = block.terminator_mut();
503         match terminator.kind {
504             TerminatorKind::Drop { location: Place {
505                 base: PlaceBase::Local(index),
506                 projection: box [],
507             }, target, .. } => {
508                 if promoted(index) {
509                     terminator.kind = TerminatorKind::Goto {
510                         target,
511                     };
512                 }
513             }
514             _ => {}
515         }
516     }
517
518     promotions
519 }