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-04-28 04:49:09 -04:00
|
|
|
use rustc::mir::transform::{MirCtxt, MirPassIndex, MirSuite, MirSource, MIR_OPTIMIZED};
|
2017-04-28 06:00:48 -04:00
|
|
|
use rustc::ty::steal::Steal;
|
2017-04-27 16:48:48 -04:00
|
|
|
use rustc::ty::TyCtxt;
|
|
|
|
use rustc::ty::maps::Providers;
|
2017-04-28 06:00:48 -04:00
|
|
|
use std::cell::Ref;
|
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-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-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-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-04-28 04:49:09 -04:00
|
|
|
let pass = passes.pass(suite, pass_num);
|
|
|
|
let mir_ctxt = MirCtxtImpl { tcx, pass_num, suite, def_id };
|
2017-04-27 16:48:48 -04:00
|
|
|
|
|
|
|
for hook in passes.hooks() {
|
|
|
|
hook.on_mir_pass(&mir_ctxt, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mir = pass.run_pass(&mir_ctxt);
|
|
|
|
|
|
|
|
for hook in passes.hooks() {
|
2017-04-28 06:00:48 -04:00
|
|
|
hook.on_mir_pass(&mir_ctxt, Some(&mir));
|
2017-04-27 16:48:48 -04:00
|
|
|
}
|
|
|
|
|
2017-04-28 06:00:48 -04:00
|
|
|
tcx.alloc_steal_mir(mir)
|
2017-04-27 16:48:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct MirCtxtImpl<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
pass_num: MirPassIndex,
|
2017-04-28 04:49:09 -04:00
|
|
|
suite: MirSuite,
|
2017-04-27 16:48:48 -04:00
|
|
|
def_id: DefId
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> MirCtxt<'a, 'tcx> for MirCtxtImpl<'a, 'tcx> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2017-04-28 04:49:09 -04:00
|
|
|
fn suite(&self) -> MirSuite {
|
|
|
|
self.suite
|
2017-04-27 16:48:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pass_num(&self) -> MirPassIndex {
|
|
|
|
self.pass_num
|
|
|
|
}
|
|
|
|
|
|
|
|
fn def_id(&self) -> DefId {
|
|
|
|
self.def_id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn source(&self) -> MirSource {
|
|
|
|
let id = self.tcx.hir.as_local_node_id(self.def_id)
|
|
|
|
.expect("mir source requires local def-id");
|
|
|
|
MirSource::from_node(self.tcx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_previous_mir(&self) -> Ref<'tcx, Mir<'tcx>> {
|
2017-04-28 06:00:48 -04:00
|
|
|
self.previous_mir().borrow()
|
2017-04-27 16:48:48 -04:00
|
|
|
}
|
|
|
|
|
2017-04-28 06:00:48 -04:00
|
|
|
fn steal_previous_mir(&self) -> Mir<'tcx> {
|
|
|
|
self.previous_mir().steal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> MirCtxtImpl<'a, 'tcx> {
|
|
|
|
fn previous_mir(&self) -> &'tcx Steal<Mir<'tcx>> {
|
2017-04-28 04:49:09 -04:00
|
|
|
let MirSuite(suite) = self.suite;
|
2017-04-27 16:48:48 -04:00
|
|
|
let MirPassIndex(pass_num) = self.pass_num;
|
|
|
|
if pass_num > 0 {
|
2017-04-28 04:49:09 -04:00
|
|
|
self.tcx.mir_pass((MirSuite(suite), MirPassIndex(pass_num - 1), self.def_id))
|
|
|
|
} else if suite > 0 {
|
|
|
|
self.tcx.mir_suite((MirSuite(suite - 1), self.def_id))
|
2017-04-27 16:48:48 -04:00
|
|
|
} else {
|
|
|
|
self.tcx.mir_build(self.def_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|