2015-11-10 21:38:36 +01:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2017-04-27 16:48:48 -04:00
|
|
|
use rustc::hir::def_id::DefId;
|
|
|
|
use rustc::mir::Mir;
|
2017-05-01 13:46:35 -04:00
|
|
|
use rustc::mir::transform::{MirPassIndex, MirSuite, MirSource, MIR_VALIDATED, MIR_OPTIMIZED};
|
|
|
|
use rustc::ty::{self, TyCtxt};
|
2017-04-28 06:00:48 -04:00
|
|
|
use rustc::ty::steal::Steal;
|
2017-05-01 13:46:35 -04:00
|
|
|
use rustc::ty::maps::Providers;
|
|
|
|
use syntax_pos::DUMMY_SP;
|
2017-04-27 16:48:48 -04:00
|
|
|
|
2016-06-09 00:10:15 +03:00
|
|
|
pub mod simplify_branches;
|
2016-08-16 04:59:50 +03:00
|
|
|
pub mod simplify;
|
2015-11-16 18:41:16 +01:00
|
|
|
pub mod erase_regions;
|
2016-02-11 10:13:35 +02:00
|
|
|
pub mod no_landing_pads;
|
2016-02-07 21:13:00 +02:00
|
|
|
pub mod type_check;
|
2016-06-05 09:00:17 +03:00
|
|
|
pub mod add_call_guards;
|
2016-05-07 19:14:28 +03:00
|
|
|
pub mod promote_consts;
|
|
|
|
pub mod qualify_consts;
|
2016-06-05 09:00:17 +03:00
|
|
|
pub mod dump_mir;
|
2016-07-27 17:46:54 -07:00
|
|
|
pub mod deaggregator;
|
2016-09-10 14:33:29 -07:00
|
|
|
pub mod instcombine;
|
2016-09-15 18:18:40 -07:00
|
|
|
pub mod copy_prop;
|
2017-02-08 22:24:49 +13:00
|
|
|
pub mod inline;
|
2017-04-28 19:29:16 -04:00
|
|
|
pub mod interprocedural;
|
2017-04-27 16:48:48 -04:00
|
|
|
|
|
|
|
pub fn provide(providers: &mut Providers) {
|
|
|
|
self::qualify_consts::provide(providers);
|
|
|
|
*providers = Providers {
|
|
|
|
optimized_mir,
|
2017-04-28 04:49:09 -04:00
|
|
|
mir_suite,
|
2017-04-27 16:48:48 -04:00
|
|
|
mir_pass,
|
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-28 06:00:48 -04:00
|
|
|
fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Mir<'tcx> {
|
|
|
|
let mir = tcx.mir_suite((MIR_OPTIMIZED, def_id)).steal();
|
|
|
|
tcx.alloc_mir(mir)
|
2017-04-27 16:48:48 -04:00
|
|
|
}
|
|
|
|
|
2017-04-28 04:49:09 -04:00
|
|
|
fn mir_suite<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
(suite, def_id): (MirSuite, DefId))
|
2017-04-28 06:00:48 -04:00
|
|
|
-> &'tcx Steal<Mir<'tcx>>
|
2017-04-27 16:48:48 -04:00
|
|
|
{
|
|
|
|
let passes = &tcx.mir_passes;
|
2017-05-01 13:46:35 -04:00
|
|
|
|
|
|
|
if suite == MIR_VALIDATED {
|
|
|
|
let id = tcx.hir.as_local_node_id(def_id).expect("mir source requires local def-id");
|
|
|
|
let source = MirSource::from_node(tcx, id);
|
|
|
|
if let MirSource::Const(_) = source {
|
|
|
|
// Ensure that we compute the `mir_const_qualif` for
|
|
|
|
// constants at this point, before we do any further
|
|
|
|
// optimization (and before we steal the previous
|
|
|
|
// MIR). We don't directly need the result, so we can
|
|
|
|
// just force it.
|
|
|
|
ty::queries::mir_const_qualif::force(tcx, DUMMY_SP, def_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 04:49:09 -04:00
|
|
|
let len = passes.len_passes(suite);
|
|
|
|
assert!(len > 0, "no passes in {:?}", suite);
|
|
|
|
tcx.mir_pass((suite, MirPassIndex(len - 1), def_id))
|
2017-04-27 16:48:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mir_pass<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-04-28 04:49:09 -04:00
|
|
|
(suite, pass_num, def_id): (MirSuite, MirPassIndex, DefId))
|
2017-05-01 13:46:35 -04:00
|
|
|
-> &'tcx Steal<Mir<'tcx>>
|
2017-04-27 16:48:48 -04:00
|
|
|
{
|
|
|
|
let passes = &tcx.mir_passes;
|
2017-04-28 04:49:09 -04:00
|
|
|
let pass = passes.pass(suite, pass_num);
|
2017-04-27 16:48:48 -04:00
|
|
|
|
2017-05-01 13:46:35 -04:00
|
|
|
let id = tcx.hir.as_local_node_id(def_id).expect("mir source requires local def-id");
|
|
|
|
let source = MirSource::from_node(tcx, id);
|
2017-04-27 16:48:48 -04:00
|
|
|
|
2017-05-01 13:46:35 -04:00
|
|
|
let mut mir = {
|
|
|
|
let MirSuite(suite) = suite;
|
|
|
|
let MirPassIndex(pass_num) = pass_num;
|
|
|
|
if pass_num > 0 {
|
|
|
|
tcx.mir_pass((MirSuite(suite), MirPassIndex(pass_num - 1), def_id)).steal()
|
|
|
|
} else if suite > 0 {
|
|
|
|
tcx.mir_suite((MirSuite(suite - 1), def_id)).steal()
|
|
|
|
} else {
|
|
|
|
tcx.mir_build(def_id).steal()
|
|
|
|
}
|
|
|
|
};
|
2017-04-27 16:48:48 -04:00
|
|
|
|
|
|
|
for hook in passes.hooks() {
|
2017-05-01 13:46:35 -04:00
|
|
|
hook.on_mir_pass(tcx, suite, pass_num, &pass.name(), source, &mir, false);
|
2017-04-27 16:48:48 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 13:46:35 -04:00
|
|
|
pass.run_pass(tcx, source, &mut mir);
|
2017-04-28 19:29:16 -04:00
|
|
|
|
2017-05-01 13:46:35 -04:00
|
|
|
for hook in passes.hooks() {
|
|
|
|
hook.on_mir_pass(tcx, suite, pass_num, &pass.name(), source, &mir, true);
|
2017-04-28 19:29:16 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 13:46:35 -04:00
|
|
|
tcx.alloc_steal_mir(mir)
|
2017-04-28 06:00:48 -04:00
|
|
|
}
|
|
|
|
|