2016-07-27 17:46:54 -07:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use rustc::ty::TyCtxt;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::*;
|
2016-07-27 17:46:54 -07:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2017-11-10 19:20:35 +02:00
|
|
|
use transform::{MirPass, MirSource};
|
2016-07-27 17:46:54 -07:00
|
|
|
|
|
|
|
pub struct Deaggregator;
|
|
|
|
|
2017-04-25 18:23:33 -04:00
|
|
|
impl MirPass for Deaggregator {
|
|
|
|
fn run_pass<'a, 'tcx>(&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2018-04-27 06:42:45 -07:00
|
|
|
_source: MirSource,
|
2017-04-25 18:23:33 -04:00
|
|
|
mir: &mut Mir<'tcx>) {
|
2018-02-07 15:27:00 +02:00
|
|
|
let (basic_blocks, local_decls) = mir.basic_blocks_and_local_decls_mut();
|
2018-02-16 19:20:18 +02:00
|
|
|
let local_decls = &*local_decls;
|
2018-02-07 15:27:00 +02:00
|
|
|
for bb in basic_blocks {
|
2018-02-16 19:20:18 +02:00
|
|
|
bb.expand_statements(|stmt| {
|
|
|
|
// FIXME(eddyb) don't match twice on `stmt.kind` (post-NLL).
|
|
|
|
if let StatementKind::Assign(_, ref rhs) = stmt.kind {
|
2018-09-24 11:32:31 +10:00
|
|
|
if let Rvalue::Aggregate(ref kind, _) = **rhs {
|
2018-02-16 19:20:18 +02:00
|
|
|
// FIXME(#48193) Deaggregate arrays when it's cheaper to do so.
|
|
|
|
if let AggregateKind::Array(_) = **kind {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
2018-02-07 15:27:00 +02:00
|
|
|
|
2018-02-16 19:20:18 +02:00
|
|
|
let stmt = stmt.replace_nop();
|
|
|
|
let source_info = stmt.source_info;
|
|
|
|
let (mut lhs, kind, operands) = match stmt.kind {
|
2018-09-24 11:32:31 +10:00
|
|
|
StatementKind::Assign(lhs, box rvalue) => {
|
|
|
|
match rvalue {
|
|
|
|
Rvalue::Aggregate(kind, operands) => (lhs, kind, operands),
|
|
|
|
_ => bug!()
|
|
|
|
}
|
|
|
|
}
|
2018-02-07 15:27:00 +02:00
|
|
|
_ => bug!()
|
2016-08-02 10:46:26 -07:00
|
|
|
};
|
2018-02-07 15:27:00 +02:00
|
|
|
|
|
|
|
let mut set_discriminant = None;
|
|
|
|
let active_field_index = match *kind {
|
2018-08-09 11:56:53 -04:00
|
|
|
AggregateKind::Adt(adt_def, variant_index, _, _, active_field_index) => {
|
2018-02-07 15:27:00 +02:00
|
|
|
if adt_def.is_enum() {
|
|
|
|
set_discriminant = Some(Statement {
|
|
|
|
kind: StatementKind::SetDiscriminant {
|
|
|
|
place: lhs.clone(),
|
|
|
|
variant_index,
|
|
|
|
},
|
|
|
|
source_info,
|
|
|
|
});
|
|
|
|
lhs = lhs.downcast(adt_def, variant_index);
|
|
|
|
}
|
|
|
|
active_field_index
|
|
|
|
}
|
|
|
|
_ => None
|
2016-12-09 16:28:54 +01:00
|
|
|
};
|
2016-08-04 16:14:33 -07:00
|
|
|
|
2018-02-16 19:20:18 +02:00
|
|
|
Some(operands.into_iter().enumerate().map(move |(i, op)| {
|
2018-02-07 15:27:00 +02:00
|
|
|
let lhs_field = if let AggregateKind::Array(_) = *kind {
|
|
|
|
// FIXME(eddyb) `offset` should be u64.
|
2018-02-16 19:20:18 +02:00
|
|
|
let offset = i as u32;
|
|
|
|
assert_eq!(offset as usize, i);
|
2018-02-07 15:27:00 +02:00
|
|
|
lhs.clone().elem(ProjectionElem::ConstantIndex {
|
|
|
|
offset,
|
|
|
|
// FIXME(eddyb) `min_length` doesn't appear to be used.
|
|
|
|
min_length: offset + 1,
|
|
|
|
from_end: false
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let ty = op.ty(local_decls, tcx);
|
2018-02-16 19:20:18 +02:00
|
|
|
let field = Field::new(active_field_index.unwrap_or(i));
|
2018-02-07 15:27:00 +02:00
|
|
|
lhs.clone().field(field, ty)
|
2016-12-09 16:28:54 +01:00
|
|
|
};
|
2018-02-16 19:20:18 +02:00
|
|
|
Statement {
|
2018-02-07 15:27:00 +02:00
|
|
|
source_info,
|
2018-09-24 11:32:31 +10:00
|
|
|
kind: StatementKind::Assign(lhs_field, box Rvalue::Use(op)),
|
2018-02-16 19:20:18 +02:00
|
|
|
}
|
|
|
|
}).chain(set_discriminant))
|
|
|
|
});
|
2016-07-27 17:46:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|