]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/promote_consts.rs
Rollup merge of #61568 - Mark-Simulacrum:symbol-fmt-macros, r=estebank
[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::mir::*;
16 use rustc::mir::visit::{PlaceContext, MutatingUseContext, MutVisitor, Visitor};
17 use rustc::mir::traversal::ReversePostorder;
18 use rustc::ty::TyCtxt;
19 use syntax_pos::Span;
20
21 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
22
23 use std::{iter, mem, usize};
24
25 /// State of a temporary during collection and promotion.
26 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
27 pub enum TempState {
28     /// No references to this temp.
29     Undefined,
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.
33     Defined {
34         location: Location,
35         uses: usize
36     },
37     /// Any other combination of assignments/uses.
38     Unpromotable,
39     /// This temp was part of an rvalue which got extracted
40     /// during promotion and needs cleanup.
41     PromotedOut
42 }
43
44 impl TempState {
45     pub fn is_promotable(&self) -> bool {
46         debug!("is_promotable: self={:?}", self);
47         if let TempState::Defined { .. } = *self {
48             true
49         } else {
50             false
51         }
52     }
53 }
54
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.
58 #[derive(Debug)]
59 pub enum Candidate {
60     /// Borrow of a constant temporary.
61     Ref(Location),
62
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
67     /// must be constant.
68     Argument { bb: BasicBlock, index: usize },
69 }
70
71 struct TempCollector<'tcx> {
72     temps: IndexVec<Local, TempState>,
73     span: Span,
74     body: &'tcx Body<'tcx>,
75 }
76
77 impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
78     fn visit_local(&mut self,
79                    &index: &Local,
80                    context: PlaceContext,
81                    location: Location) {
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) {
85             | LocalKind::Temp
86             | LocalKind::ReturnPointer
87             => {},
88             | LocalKind::Arg
89             | LocalKind::Var
90             => return,
91         }
92
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() {
97             debug!(
98                 "visit_local: context.is_drop={:?} context.is_use={:?}",
99                 context.is_drop(), context.is_use(),
100             );
101             return;
102         }
103
104         let temp = &mut self.temps[index];
105         debug!("visit_local: temp={:?}", temp);
106         if *temp == TempState::Undefined {
107             match context {
108                 PlaceContext::MutatingUse(MutatingUseContext::Store) |
109                 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
110                     *temp = TempState::Defined {
111                         location,
112                         uses: 0
113                     };
114                     return;
115                 }
116                 _ => { /* mark as unpromotable below */ }
117             }
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);
123             if allowed_use {
124                 *uses += 1;
125                 return;
126             }
127             /* mark as unpromotable below */
128         }
129         *temp = TempState::Unpromotable;
130     }
131
132     fn visit_source_info(&mut self, source_info: &SourceInfo) {
133         self.span = source_info.span;
134     }
135 }
136
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),
141         span: body.span,
142         body,
143     };
144     for (bb, data) in rpo {
145         collector.visit_basic_block_data(bb, data);
146     }
147     collector.temps
148 }
149
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>,
155
156     /// If true, all nested temps are also kept in the
157     /// source MIR, not moved to the promoted MIR.
158     keep_original: bool
159 }
160
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 {
165             statements: vec![],
166             terminator: Some(Terminator {
167                 source_info: SourceInfo {
168                     span,
169                     scope: OUTERMOST_SOURCE_SCOPE
170                 },
171                 kind: TerminatorKind::Return
172             }),
173             is_cleanup: false
174         })
175     }
176
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 {
182                 span,
183                 scope: OUTERMOST_SOURCE_SCOPE
184             },
185             kind: StatementKind::Assign(Place::Base(PlaceBase::Local(dest)), box rvalue)
186         });
187     }
188
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 => {
195                 if uses > 1 {
196                     self.keep_original = true;
197                 }
198                 location
199             }
200             state =>  {
201                 span_bug!(self.promoted.span, "{:?} not promotable: {:?}",
202                           temp, state);
203             }
204         };
205         if !self.keep_original {
206             self.temps[temp] = TempState::PromotedOut;
207         }
208
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));
213
214         debug!("promote({:?} @ {:?}/{:?}, {:?})",
215                temp, loc, no_stmts, self.keep_original);
216
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,
224                     _ => {
225                         span_bug!(statement.source_info.span, "{:?} is not an assignment",
226                                   statement);
227                     }
228                 };
229
230                 (if self.keep_original {
231                     rhs.clone()
232                 } else {
233                     let unit = box Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
234                     mem::replace(rhs, unit)
235                 }, statement.source_info)
236             };
237
238             let mut rvalue = *rvalue;
239             self.visit_rvalue(&mut rvalue, loc);
240             self.assign(new_temp, rvalue, source_info.span);
241         } else {
242             let terminator = if self.keep_original {
243                 self.source[loc.block].terminator().clone()
244             } else {
245                 let terminator = self.source[loc.block].terminator_mut();
246                 let target = match terminator.kind {
247                     TerminatorKind::Call { destination: Some((_, target)), .. } => target,
248                     ref kind => {
249                         span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
250                     }
251                 };
252                 Terminator {
253                     source_info: terminator.source_info,
254                     kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto {
255                         target,
256                     })
257                 }
258             };
259
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);
265                     }
266
267                     let last = self.promoted.basic_blocks().last().unwrap();
268                     let new_target = self.new_block();
269
270                     *self.promoted[last].terminator_mut() = Terminator {
271                         kind: TerminatorKind::Call {
272                             func,
273                             args,
274                             cleanup: None,
275                             destination: Some(
276                                 (Place::Base(PlaceBase::Local(new_temp)), new_target)
277                             ),
278                             from_hir_call,
279                         },
280                         ..terminator
281                     };
282                 }
283                 ref kind => {
284                     span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
285                 }
286             };
287         };
288
289         self.keep_original = old_keep_original;
290         new_temp
291     }
292
293     fn promote_candidate(mut self, candidate: Candidate) {
294         let mut operand = {
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);
300                 Place::Base(
301                     PlaceBase::Static(box Static{ kind: StaticKind::Promoted(promoted_id), ty })
302                 )
303             };
304             let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
305             match candidate {
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;
315                             };
316
317                             let ty = place.ty(local_decls, self.tcx).ty;
318                             let span = statement.source_info.span;
319
320                             Operand::Move(mem::replace(place, promoted_place(ty, span)))
321                         }
322                         _ => bug!()
323                     }
324                 }
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)
333                         }
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,
342                         _ => bug!()
343                     }
344                 }
345             }
346         };
347
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
352         });
353
354         let span = self.promoted.span;
355         self.assign(RETURN_PLACE, Rvalue::Use(operand), span);
356         self.source.promoted.push(self.promoted);
357     }
358 }
359
360 /// Replaces all temporaries with their promoted counterparts.
361 impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
362     fn visit_local(&mut self,
363                    local: &mut Local,
364                    _: PlaceContext,
365                    _: Location) {
366         if self.source.local_kind(*local) == LocalKind::Temp {
367             *local = self.promote_temp(*local);
368         }
369     }
370 }
371
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);
378
379     for candidate in candidates.into_iter().rev() {
380         match candidate {
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 {
385                             // Already promoted.
386                             continue;
387                         }
388                     }
389                     _ => {}
390                 }
391             }
392             Candidate::Argument { .. } => {}
393         }
394
395
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)
399         ).collect();
400
401         let promoter = Promoter {
402             promoted: Body::new(
403                 IndexVec::new(),
404                 // FIXME: maybe try to filter this to avoid blowing up
405                 // memory usage?
406                 body.source_scopes.clone(),
407                 body.source_scope_local_data.clone(),
408                 IndexVec::new(),
409                 None,
410                 initial_locals,
411                 IndexVec::new(),
412                 0,
413                 vec![],
414                 body.span,
415                 vec![],
416             ),
417             tcx,
418             source: body,
419             temps: &mut temps,
420             keep_original: false
421         };
422         promoter.promote_candidate(candidate);
423     }
424
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) => {
433                     !promoted(index)
434                 }
435                 _ => true
436             }
437         });
438         let terminator = block.terminator_mut();
439         match terminator.kind {
440             TerminatorKind::Drop { location: Place::Base(PlaceBase::Local(index)), target, .. } => {
441                 if promoted(index) {
442                     terminator.kind = TerminatorKind::Goto {
443                         target,
444                     };
445                 }
446             }
447             _ => {}
448         }
449     }
450 }