From b4983526bd5a098b69a84556fb5ccd3ab3b233f8 Mon Sep 17 00:00:00 2001 From: David Mak Date: Tue, 5 Sep 2023 17:07:48 +0800 Subject: [PATCH] core: Remove redundant for.cond BB for iterable loops Simplifies logic for creating basic blocks. --- nac3core/src/codegen/stmt.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index 93bd7eb6..f1edf77d 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -248,7 +248,6 @@ pub fn gen_for<'ctx, 'a, G: CodeGenerator>( let size_t = generator.get_size_type(ctx.ctx); let zero = int32.const_zero(); let current = ctx.builder.get_insert_block().and_then(|bb| bb.get_parent()).unwrap(); - let test_bb = ctx.ctx.append_basic_block(current, "for.cond"); let body_bb = ctx.ctx.append_basic_block(current, "for.body"); let cont_bb = ctx.ctx.append_basic_block(current, "for.end"); // if there is no orelse, we just go to cont_bb @@ -258,13 +257,16 @@ pub fn gen_for<'ctx, 'a, G: CodeGenerator>( // Whether the iterable is a range() expression let is_iterable_range_expr = ctx.unifier.unioned(iter.custom.unwrap(), ctx.primitives.range); - // store loop bb information and restore it later - let loop_bb = if is_iterable_range_expr { - ctx.loop_target.replace((body_bb, cont_bb)) + // The target BB of the loop backedge + let backedge_bb_target = if is_iterable_range_expr { + body_bb } else { - ctx.loop_target.replace((test_bb, cont_bb)) + ctx.ctx.append_basic_block(current, "for.cond") }; + // store loop bb information and restore it later + let loop_bb = ctx.loop_target.replace((backedge_bb_target, cont_bb)); + let iter_val = generator.gen_expr(ctx, iter)?.unwrap().to_basic_value_enum( ctx, generator, @@ -317,6 +319,8 @@ pub fn gen_for<'ctx, 'a, G: CodeGenerator>( ctx.builder.position_at_end(cond_cont_bb); ctx.builder.build_store(i, next_i); } else { + let test_bb = backedge_bb_target; + let counter = generator.gen_var_alloc(ctx, size_t.into(), Some("for.counter.addr"))?; // counter = -1 ctx.builder.build_store(counter, size_t.const_int(u64::max_value(), true)); @@ -353,11 +357,7 @@ pub fn gen_for<'ctx, 'a, G: CodeGenerator>( } if !ctx.is_terminated() { - if is_iterable_range_expr { - ctx.builder.build_unconditional_branch(body_bb); - } else { - ctx.builder.build_unconditional_branch(test_bb); - } + ctx.builder.build_unconditional_branch(backedge_bb_target); } if !orelse.is_empty() { @@ -375,12 +375,6 @@ pub fn gen_for<'ctx, 'a, G: CodeGenerator>( } } - // Clear test_bb if unused - if is_iterable_range_expr { - ctx.builder.position_at_end(test_bb); - ctx.builder.build_unreachable(); - } - ctx.builder.position_at_end(cont_bb); ctx.loop_target = loop_bb; } else {