|
| 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 | +use rustc::ty::TyCtxt; |
| 12 | +use rustc::mir::repr::*; |
| 13 | +use rustc::mir::transform::{MirPass, MirSource, Pass}; |
| 14 | +use rustc_data_structures::indexed_vec::Idx; |
| 15 | +use rustc::ty::VariantKind; |
| 16 | + |
| 17 | +pub struct Deaggregator; |
| 18 | + |
| 19 | +impl Pass for Deaggregator {} |
| 20 | + |
| 21 | +impl<'tcx> MirPass<'tcx> for Deaggregator { |
| 22 | + fn run_pass<'a>(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 23 | + source: MirSource, mir: &mut Mir<'tcx>) { |
| 24 | + let node_id = source.item_id(); |
| 25 | + let node_path = tcx.item_path_str(tcx.map.local_def_id(node_id)); |
| 26 | + debug!("running on: {:?}", node_path); |
| 27 | + // we only run when mir_opt_level > 1 |
| 28 | + match tcx.sess.opts.debugging_opts.mir_opt_level { |
| 29 | + Some(0) | |
| 30 | + Some(1) | |
| 31 | + None => { return; }, |
| 32 | + _ => {} |
| 33 | + }; |
| 34 | + |
| 35 | + // Do not trigger on constants. Could be revised in future |
| 36 | + if let MirSource::Fn(_) = source {} else { return; } |
| 37 | + // In fact, we might not want to trigger in other cases. |
| 38 | + // Ex: when we could use SROA. See issue #35259 |
| 39 | + |
| 40 | + let mut curr: usize = 0; |
| 41 | + for bb in mir.basic_blocks_mut() { |
| 42 | + let idx = match get_aggregate_statement(curr, &bb.statements) { |
| 43 | + Some(idx) => idx, |
| 44 | + None => continue, |
| 45 | + }; |
| 46 | + // do the replacement |
| 47 | + debug!("removing statement {:?}", idx); |
| 48 | + let src_info = bb.statements[idx].source_info; |
| 49 | + let suffix_stmts = bb.statements.split_off(idx+1); |
| 50 | + let orig_stmt = bb.statements.pop().unwrap(); |
| 51 | + let StatementKind::Assign(ref lhs, ref rhs) = orig_stmt.kind; |
| 52 | + let (agg_kind, operands) = match rhs { |
| 53 | + &Rvalue::Aggregate(ref agg_kind, ref operands) => (agg_kind, operands), |
| 54 | + _ => span_bug!(src_info.span, "expected aggregate, not {:?}", rhs), |
| 55 | + }; |
| 56 | + let (adt_def, variant, substs) = match agg_kind { |
| 57 | + &AggregateKind::Adt(adt_def, variant, substs) => (adt_def, variant, substs), |
| 58 | + _ => span_bug!(src_info.span, "expected struct, not {:?}", rhs), |
| 59 | + }; |
| 60 | + let n = bb.statements.len(); |
| 61 | + bb.statements.reserve(n + operands.len() + suffix_stmts.len()); |
| 62 | + for (i, op) in operands.iter().enumerate() { |
| 63 | + let ref variant_def = adt_def.variants[variant]; |
| 64 | + let ty = variant_def.fields[i].ty(tcx, substs); |
| 65 | + let rhs = Rvalue::Use(op.clone()); |
| 66 | + |
| 67 | + // since we don't handle enums, we don't need a cast |
| 68 | + let lhs_cast = lhs.clone(); |
| 69 | + |
| 70 | + // FIXME we cannot deaggregate enums issue: #35186 |
| 71 | + |
| 72 | + let lhs_proj = Lvalue::Projection(Box::new(LvalueProjection { |
| 73 | + base: lhs_cast, |
| 74 | + elem: ProjectionElem::Field(Field::new(i), ty), |
| 75 | + })); |
| 76 | + let new_statement = Statement { |
| 77 | + source_info: src_info, |
| 78 | + kind: StatementKind::Assign(lhs_proj, rhs), |
| 79 | + }; |
| 80 | + debug!("inserting: {:?} @ {:?}", new_statement, idx + i); |
| 81 | + bb.statements.push(new_statement); |
| 82 | + } |
| 83 | + curr = bb.statements.len(); |
| 84 | + bb.statements.extend(suffix_stmts); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +fn get_aggregate_statement<'a, 'tcx, 'b>(curr: usize, |
| 90 | + statements: &Vec<Statement<'tcx>>) |
| 91 | + -> Option<usize> { |
| 92 | + for i in curr..statements.len() { |
| 93 | + let ref statement = statements[i]; |
| 94 | + let StatementKind::Assign(_, ref rhs) = statement.kind; |
| 95 | + let (kind, operands) = match rhs { |
| 96 | + &Rvalue::Aggregate(ref kind, ref operands) => (kind, operands), |
| 97 | + _ => continue, |
| 98 | + }; |
| 99 | + let (adt_def, variant) = match kind { |
| 100 | + &AggregateKind::Adt(adt_def, variant, _) => (adt_def, variant), |
| 101 | + _ => continue, |
| 102 | + }; |
| 103 | + if operands.len() == 0 || adt_def.variants.len() > 1 { |
| 104 | + // don't deaggregate () |
| 105 | + // don't deaggregate enums ... for now |
| 106 | + continue; |
| 107 | + } |
| 108 | + debug!("getting variant {:?}", variant); |
| 109 | + debug!("for adt_def {:?}", adt_def); |
| 110 | + let variant_def = &adt_def.variants[variant]; |
| 111 | + if variant_def.kind == VariantKind::Struct { |
| 112 | + return Some(i); |
| 113 | + } |
| 114 | + }; |
| 115 | + None |
| 116 | +} |
0 commit comments