Skip to content

Commit

Permalink
Don't use scary block reorderings
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Mar 8, 2018
1 parent 5b247b9 commit edb2af5
Showing 1 changed file with 4 additions and 56 deletions.
60 changes: 4 additions & 56 deletions src/librustc_mir/transform/instcombine.rs
Expand Up @@ -12,19 +12,17 @@

use rustc::mir::{Constant, Literal, Location, Place, Mir, Operand, ProjectionElem, Rvalue, Local};
use rustc::mir::{NullOp, StatementKind, Statement, BasicBlock, LocalKind};
use rustc::mir::{SourceInfo, ARGUMENT_VISIBILITY_SCOPE, TerminatorKind};
use rustc::mir::visit::{MutVisitor, Visitor, TyContext};
use rustc::mir::TerminatorKind;
use rustc::mir::visit::{MutVisitor, Visitor};
use rustc::middle::const_val::ConstVal;
use rustc::ty::{TyCtxt, TypeVariants, self, Instance};
use rustc::mir::interpret::{Value, PrimVal, GlobalId};
use interpret::{eval_body_with_mir, eval_body, mk_borrowck_eval_cx, unary_op, ValTy};
use rustc::util::nodemap::{FxHashMap, FxHashSet};
use rustc_data_structures::indexed_vec::Idx;
use std::mem;
use std::collections::VecDeque;
use transform::{MirPass, MirSource};
use syntax::codemap::Span;
use rustc_data_structures::control_flow_graph::ControlFlowGraph;
use rustc::ty::subst::Substs;

pub struct InstCombine;
Expand Down Expand Up @@ -73,7 +71,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for InstCombineVisitor<'a, 'tcx> {
}

if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
debug!("Replacing `Len([_; N])`: {:?}", rvalue);
debug!("Replacing `Len([_; N])`: {:?} with {:?}", rvalue, constant);
*rvalue = Rvalue::Use(Operand::Constant(box constant));
}

Expand Down Expand Up @@ -101,6 +99,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for InstCombineVisitor<'a, 'tcx> {
) {
self.super_constant(constant, location);
if let Some(&(val, ty, _)) = self.optimizations.constants.get(constant) {
debug!("Replacing `{:?}` with {:?}:{:?}", constant.literal, val, ty);
constant.literal = Literal::Value {
value: self.tcx.mk_const(ty::Const {
val: ConstVal::Value(val),
Expand Down Expand Up @@ -464,57 +463,6 @@ impl<'tcx> Visitor<'tcx> for ConstPropVisitor {
}

impl<'b, 'a, 'tcx> Visitor<'tcx> for OptimizationFinder<'b, 'a, 'tcx> {
// override to visit basic blocks in execution order
fn super_mir(&mut self, mir: &Mir<'tcx>) {
let mut seen = FxHashSet::default();
seen.insert(mir.start_node());
let mut sorted = Vec::new();
let mut next = VecDeque::new();
sorted.push(mir.start_node());
next.push_back(mir.start_node());
while let Some(current) = next.pop_front() {
for successor in mir.successors(current) {
trace!("checking successor of {:?}: {:?}", current, successor);
trace!("{:?}, {:?}", sorted, next);
if seen.contains(&successor) {
for &pending in &next {
// not a back-edge, just a branch merging back into a single execution
if pending == successor {
// move to the back of the queue
let i = sorted.iter().position(|&b| b == successor).unwrap();
sorted.remove(i);
sorted.push(successor);
break;
}
}
} else {
seen.insert(successor);
sorted.push(successor);
next.push_back(successor);
}
}
}
trace!("checking basic blocks: {:?}", sorted);
for bb in sorted {
self.visit_basic_block_data(bb, &mir[bb]);
}

for scope in &mir.visibility_scopes {
self.visit_visibility_scope_data(scope);
}

self.visit_ty(&mir.return_ty(), TyContext::ReturnTy(SourceInfo {
span: mir.span,
scope: ARGUMENT_VISIBILITY_SCOPE,
}));

for local in mir.local_decls.indices() {
self.visit_local_decl(local, &mir.local_decls[local]);
}

self.visit_span(&mir.span);
}

fn visit_constant(
&mut self,
constant: &Constant<'tcx>,
Expand Down

0 comments on commit edb2af5

Please sign in to comment.