]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/promote_consts.rs
Auto merge of #55717 - oli-obk:rustdoc_overflow, r=pnkfelix
[rust.git] / src / librustc_mir / transform / promote_consts.rs
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.
4 //
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.
10
11 //! A pass that promotes borrows of constant rvalues.
12 //!
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.
20 //!
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.
24
25 use rustc::mir::*;
26 use rustc::mir::visit::{PlaceContext, MutatingUseContext, MutVisitor, Visitor};
27 use rustc::mir::traversal::ReversePostorder;
28 use rustc::ty::TyCtxt;
29 use syntax_pos::Span;
30
31 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
32
33 use std::{iter, mem, usize};
34
35 /// State of a temporary during collection and promotion.
36 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
37 pub enum TempState {
38     /// No references to this temp.
39     Undefined,
40     /// One direct assignment and any number of direct uses.
41     /// A borrow of this temp is promotable if the assigned
42     /// value is qualified as constant.
43     Defined {
44         location: Location,
45         uses: usize
46     },
47     /// Any other combination of assignments/uses.
48     Unpromotable,
49     /// This temp was part of an rvalue which got extracted
50     /// during promotion and needs cleanup.
51     PromotedOut
52 }
53
54 impl TempState {
55     pub fn is_promotable(&self) -> bool {
56         debug!("is_promotable: self={:?}", self);
57         if let TempState::Defined { uses, .. } = *self {
58             uses > 0
59         } else {
60             false
61         }
62     }
63 }
64
65 /// A "root candidate" for promotion, which will become the
66 /// returned value in a promoted MIR, unless it's a subset
67 /// of a larger candidate.
68 #[derive(Debug)]
69 pub enum Candidate {
70     /// Borrow of a constant temporary.
71     Ref(Location),
72
73     /// Currently applied to function calls where the callee has the unstable
74     /// `#[rustc_args_required_const]` attribute as well as the SIMD shuffle
75     /// intrinsic. The intrinsic requires the arguments are indeed constant and
76     /// the attribute currently provides the semantic requirement that arguments
77     /// must be constant.
78     Argument { bb: BasicBlock, index: usize },
79 }
80
81 struct TempCollector<'tcx> {
82     temps: IndexVec<Local, TempState>,
83     span: Span,
84     mir: &'tcx Mir<'tcx>,
85 }
86
87 impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
88     fn visit_local(&mut self,
89                    &index: &Local,
90                    context: PlaceContext<'tcx>,
91                    location: Location) {
92         debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
93         // We're only interested in temporaries
94         if self.mir.local_kind(index) != LocalKind::Temp {
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::AsmOutput) |
115                 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
116                     *temp = TempState::Defined {
117                         location,
118                         uses: 0
119                     };
120                     return;
121                 }
122                 _ => { /* mark as unpromotable below */ }
123             }
124         } else if let TempState::Defined { ref mut uses, .. } = *temp {
125             // We always allow borrows, even mutable ones, as we need
126             // to promote mutable borrows of some ZSTs e.g. `&mut []`.
127             let allowed_use = context.is_borrow() || context.is_nonmutating_use();
128             debug!("visit_local: allowed_use={:?}", allowed_use);
129             if allowed_use {
130                 *uses += 1;
131                 return;
132             }
133             /* mark as unpromotable below */
134         }
135         *temp = TempState::Unpromotable;
136     }
137
138     fn visit_source_info(&mut self, source_info: &SourceInfo) {
139         self.span = source_info.span;
140     }
141 }
142
143 pub fn collect_temps(mir: &Mir, rpo: &mut ReversePostorder) -> IndexVec<Local, TempState> {
144     let mut collector = TempCollector {
145         temps: IndexVec::from_elem(TempState::Undefined, &mir.local_decls),
146         span: mir.span,
147         mir,
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: 'a> {
156     tcx: TyCtxt<'a, 'tcx, 'tcx>,
157     source: &'a mut Mir<'tcx>,
158     promoted: Mir<'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(Place::Local(dest), box rvalue)
191         });
192     }
193
194     /// Copy the initialization of this temp to the
195     /// promoted MIR, recursing through temps.
196     fn promote_temp(&mut self, temp: Local) -> Local {
197         let old_keep_original = self.keep_original;
198         let loc = match self.temps[temp] {
199             TempState::Defined { location, uses } if uses > 0 => {
200                 if uses > 1 {
201                     self.keep_original = true;
202                 }
203                 location
204             }
205             state =>  {
206                 span_bug!(self.promoted.span, "{:?} not promotable: {:?}",
207                           temp, state);
208             }
209         };
210         if !self.keep_original {
211             self.temps[temp] = TempState::PromotedOut;
212         }
213
214         let no_stmts = self.source[loc.block].statements.len();
215         let new_temp = self.promoted.local_decls.push(
216             LocalDecl::new_temp(self.source.local_decls[temp].ty,
217                                 self.source.local_decls[temp].source_info.span));
218
219         debug!("promote({:?} @ {:?}/{:?}, {:?})",
220                temp, loc, no_stmts, self.keep_original);
221
222         // First, take the Rvalue or Call out of the source MIR,
223         // or duplicate it, depending on keep_original.
224         if loc.statement_index < no_stmts {
225             let (rvalue, source_info) = {
226                 let statement = &mut self.source[loc.block].statements[loc.statement_index];
227                 let rhs = match statement.kind {
228                     StatementKind::Assign(_, ref mut rhs) => rhs,
229                     _ => {
230                         span_bug!(statement.source_info.span, "{:?} is not an assignment",
231                                   statement);
232                     }
233                 };
234
235                 (if self.keep_original {
236                     rhs.clone()
237                 } else {
238                     let unit = box Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
239                     mem::replace(rhs, unit)
240                 }, statement.source_info)
241             };
242
243             let mut rvalue = *rvalue;
244             self.visit_rvalue(&mut rvalue, loc);
245             self.assign(new_temp, rvalue, source_info.span);
246         } else {
247             let terminator = if self.keep_original {
248                 self.source[loc.block].terminator().clone()
249             } else {
250                 let terminator = self.source[loc.block].terminator_mut();
251                 let target = match terminator.kind {
252                     TerminatorKind::Call { destination: Some((_, target)), .. } => target,
253                     ref kind => {
254                         span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
255                     }
256                 };
257                 Terminator {
258                     source_info: terminator.source_info,
259                     kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto {
260                         target,
261                     })
262                 }
263             };
264
265             match terminator.kind {
266                 TerminatorKind::Call { mut func, mut args, from_hir_call, .. } => {
267                     self.visit_operand(&mut func, loc);
268                     for arg in &mut args {
269                         self.visit_operand(arg, loc);
270                     }
271
272                     let last = self.promoted.basic_blocks().last().unwrap();
273                     let new_target = self.new_block();
274
275                     *self.promoted[last].terminator_mut() = Terminator {
276                         kind: TerminatorKind::Call {
277                             func,
278                             args,
279                             cleanup: None,
280                             destination: Some((Place::Local(new_temp), new_target)),
281                             from_hir_call,
282                         },
283                         ..terminator
284                     };
285                 }
286                 ref kind => {
287                     span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
288                 }
289             };
290         };
291
292         self.keep_original = old_keep_original;
293         new_temp
294     }
295
296     fn promote_candidate(mut self, candidate: Candidate) {
297         let mut operand = {
298             let promoted = &mut self.promoted;
299             let promoted_id = Promoted::new(self.source.promoted.len());
300             let mut promoted_place = |ty, span| {
301                 promoted.span = span;
302                 promoted.local_decls[RETURN_PLACE] =
303                     LocalDecl::new_return_place(ty, span);
304                 Place::Promoted(box (promoted_id, ty))
305             };
306             let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
307             match candidate {
308                 Candidate::Ref(loc) => {
309                     let ref mut statement = blocks[loc.block].statements[loc.statement_index];
310                     match statement.kind {
311                         StatementKind::Assign(_, box Rvalue::Ref(_, _, ref mut place)) => {
312                             // Find the underlying local for this (necessarily interior) borrow.
313                             let mut place = place;
314                             while let Place::Projection(ref mut proj) = *place {
315                                 assert_ne!(proj.elem, ProjectionElem::Deref);
316                                 place = &mut proj.base;
317                             };
318
319                             let ty = place.ty(local_decls, self.tcx).to_ty(self.tcx);
320                             let span = statement.source_info.span;
321
322                             Operand::Move(mem::replace(place, promoted_place(ty, span)))
323                         }
324                         _ => bug!()
325                     }
326                 }
327                 Candidate::Argument { bb, index } => {
328                     let terminator = blocks[bb].terminator_mut();
329                     match terminator.kind {
330                         TerminatorKind::Call { ref mut args, .. } => {
331                             let ty = args[index].ty(local_decls, self.tcx);
332                             let span = terminator.source_info.span;
333                             let operand = Operand::Copy(promoted_place(ty, span));
334                             mem::replace(&mut args[index], operand)
335                         }
336                         // We expected a `TerminatorKind::Call` for which we'd like to promote an
337                         // argument. `qualify_consts` saw a `TerminatorKind::Call` here, but
338                         // we are seeing a `Goto`. That means that the `promote_temps` method
339                         // already promoted this call away entirely. This case occurs when calling
340                         // a function requiring a constant argument and as that constant value
341                         // providing a value whose computation contains another call to a function
342                         // requiring a constant argument.
343                         TerminatorKind::Goto { .. } => return,
344                         _ => bug!()
345                     }
346                 }
347             }
348         };
349
350         assert_eq!(self.new_block(), START_BLOCK);
351         self.visit_operand(&mut operand, Location {
352             block: BasicBlock::new(0),
353             statement_index: usize::MAX
354         });
355
356         let span = self.promoted.span;
357         self.assign(RETURN_PLACE, Rvalue::Use(operand), span);
358         self.source.promoted.push(self.promoted);
359     }
360 }
361
362 /// Replaces all temporaries with their promoted counterparts.
363 impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
364     fn visit_local(&mut self,
365                    local: &mut Local,
366                    _: PlaceContext<'tcx>,
367                    _: Location) {
368         if self.source.local_kind(*local) == LocalKind::Temp {
369             *local = self.promote_temp(*local);
370         }
371     }
372 }
373
374 pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
375                                     tcx: TyCtxt<'a, 'tcx, 'tcx>,
376                                     mut temps: IndexVec<Local, TempState>,
377                                     candidates: Vec<Candidate>) {
378     // Visit candidates in reverse, in case they're nested.
379     debug!("promote_candidates({:?})", candidates);
380
381     for candidate in candidates.into_iter().rev() {
382         match candidate {
383             Candidate::Ref(Location { block, statement_index }) => {
384                 match mir[block].statements[statement_index].kind {
385                     StatementKind::Assign(Place::Local(local), _) => {
386                         if temps[local] == TempState::PromotedOut {
387                             // Already promoted.
388                             continue;
389                         }
390                     }
391                     _ => {}
392                 }
393             }
394             Candidate::Argument { .. } => {}
395         }
396
397
398         // Declare return place local so that `Mir::new` doesn't complain.
399         let initial_locals = iter::once(
400             LocalDecl::new_return_place(tcx.types.never, mir.span)
401         ).collect();
402
403         let promoter = Promoter {
404             promoted: Mir::new(
405                 IndexVec::new(),
406                 // FIXME: maybe try to filter this to avoid blowing up
407                 // memory usage?
408                 mir.source_scopes.clone(),
409                 mir.source_scope_local_data.clone(),
410                 IndexVec::new(),
411                 None,
412                 initial_locals,
413                 0,
414                 vec![],
415                 mir.span
416             ),
417             tcx,
418             source: mir,
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 mir.basic_blocks_mut() {
428         block.statements.retain(|statement| {
429             match statement.kind {
430                 StatementKind::Assign(Place::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::Local(index), target, .. } => {
441                 if promoted(index) {
442                     terminator.kind = TerminatorKind::Goto {
443                         target,
444                     };
445                 }
446             }
447             _ => {}
448         }
449     }
450 }