Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIR trans] Optimize trans for biased switches #33566

Merged
merged 1 commit into from
May 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/librustc_trans/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use meth;
use type_of;
use glue;
use type_::Type;
use rustc_data_structures::fnv::FnvHashMap;

use super::{MirContext, TempRef, drop};
use super::constant::Const;
Expand Down Expand Up @@ -95,17 +96,32 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
adt::trans_get_discr(bcx, &repr, discr_lvalue.llval, None, true)
);

// The else branch of the Switch can't be hit, so branch to an unreachable
// instruction so LLVM knows that
let unreachable_blk = self.unreachable_block();
let switch = bcx.switch(discr, unreachable_blk.llbb, targets.len());
let mut bb_hist = FnvHashMap();
Copy link
Member

@nagisa nagisa May 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a FIXME here to check for the validity of such transformation. While currently the otherwise branch is always unreachable (and contains a panic), this might not be the case after a bunch of optimisation passes are added to the MIR thus making this transformation invalid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a note.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TerminatorKind::Switch can't have an otherwise branch in any case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also the assert_eq!(adt_def.variants.len(), targets.len()); which should in theory make sure we've covered all the cases.

for target in targets {
*bb_hist.entry(target).or_insert(0) += 1;
}
let (default_bb, default_blk) = match bb_hist.iter().max_by_key(|&(_, c)| c) {
// If a single target basic blocks is predominant, promote that to be the
// default case for the switch instruction to reduce the size of the generated
// code. This is especially helpful in cases like an if-let on a huge enum.
// Note: This optimization is only valid for exhaustive matches.
Some((&&bb, &c)) if c > targets.len() / 2 => {
(Some(bb), self.blocks[bb.index()])
}
// We're generating an exhaustive switch, so the else branch
// can't be hit. Branching to an unreachable instruction
// lets LLVM know this
_ => (None, self.unreachable_block())
};
let switch = bcx.switch(discr, default_blk.llbb, targets.len());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.llblock(default_bb) would be cleaner I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work for the case where there is no predominant target, because default_bb is None then.

assert_eq!(adt_def.variants.len(), targets.len());
for (adt_variant, target) in adt_def.variants.iter().zip(targets) {
let llval = bcx.with_block(|bcx|
adt::trans_case(bcx, &repr, Disr::from(adt_variant.disr_val))
);
let llbb = self.llblock(*target);
build::AddCase(switch, llval, llbb)
for (adt_variant, &target) in adt_def.variants.iter().zip(targets) {
if default_bb != Some(target) {
let llbb = self.llblock(target);
let llval = bcx.with_block(|bcx| adt::trans_case(
bcx, &repr, Disr::from(adt_variant.disr_val)));
build::AddCase(switch, llval, llbb)
}
}
}

Expand Down