optimize branching in debug builds

This commit is contained in:
Folkert 2021-01-26 23:45:41 +01:00
parent aaa103005a
commit e87d8f6449
2 changed files with 88 additions and 56 deletions

View file

@ -1471,11 +1471,8 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
let result = builder.build_alloca(ctx.i64_type(), "result"); let result = builder.build_alloca(ctx.i64_type(), "result");
env.builder.build_switch( env.builder
is_null, .build_conditional_branch(is_null, then_block, else_block);
else_block,
&[(ctx.bool_type().const_int(1, false), then_block)],
);
{ {
env.builder.position_at_end(then_block); env.builder.position_at_end(then_block);
@ -2431,6 +2428,45 @@ fn build_switch_ir<'a, 'ctx, 'env>(
// Build the cases // Build the cases
let mut incoming = Vec::with_capacity_in(branches.len(), arena); let mut incoming = Vec::with_capacity_in(branches.len(), arena);
if let Layout::Builtin(Builtin::Int1) = cond_layout {
match (branches, default_branch) {
([(0, false_branch)], true_branch) | ([(1, true_branch)], false_branch) => {
let then_block = context.append_basic_block(parent, "then_block");
let else_block = context.append_basic_block(parent, "else_block");
builder.build_conditional_branch(cond, then_block, else_block);
{
builder.position_at_end(then_block);
let branch_val = build_exp_stmt(env, layout_ids, scope, parent, true_branch);
if then_block.get_terminator().is_none() {
builder.build_unconditional_branch(cont_block);
incoming.push((branch_val, then_block));
}
}
{
builder.position_at_end(else_block);
let branch_val = build_exp_stmt(env, layout_ids, scope, parent, false_branch);
if else_block.get_terminator().is_none() {
builder.build_unconditional_branch(cont_block);
incoming.push((branch_val, else_block));
}
}
}
_ => {
dbg!(branches);
unreachable!()
}
}
} else {
let default_block = context.append_basic_block(parent, "default");
let mut cases = Vec::with_capacity_in(branches.len(), arena); let mut cases = Vec::with_capacity_in(branches.len(), arena);
for (int, _) in branches.iter() { for (int, _) in branches.iter() {
@ -2461,8 +2497,6 @@ fn build_switch_ir<'a, 'ctx, 'env>(
cases.push((int_val, block)); cases.push((int_val, block));
} }
let default_block = context.append_basic_block(parent, "default");
builder.build_switch(cond, default_block, &cases); builder.build_switch(cond, default_block, &cases);
for ((_, branch_expr), (_, block)) in branches.iter().zip(cases) { for ((_, branch_expr), (_, block)) in branches.iter().zip(cases) {
@ -2485,6 +2519,7 @@ fn build_switch_ir<'a, 'ctx, 'env>(
builder.build_unconditional_branch(cont_block); builder.build_unconditional_branch(cont_block);
incoming.push((default_val, default_block)); incoming.push((default_val, default_block));
} }
}
// emit merge block // emit merge block
if incoming.is_empty() { if incoming.is_empty() {

View file

@ -924,11 +924,8 @@ fn build_rec_union_help<'a, 'ctx, 'env>(
let then_block = ctx.append_basic_block(parent, "then"); let then_block = ctx.append_basic_block(parent, "then");
env.builder.build_switch( env.builder
is_null, .build_conditional_branch(is_null, then_block, cont_block);
cont_block,
&[(ctx.bool_type().const_int(1, false), then_block)],
);
{ {
env.builder.position_at_end(then_block); env.builder.position_at_end(then_block);