codegen: do not generate cont_bb if unreachable

This commit is contained in:
pca006132 2021-08-27 11:46:12 +08:00
parent 52dd792b3e
commit bf4e0009c0

View File

@ -127,10 +127,11 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
let current = self.builder.get_insert_block().unwrap().get_parent().unwrap(); let current = self.builder.get_insert_block().unwrap().get_parent().unwrap();
let test_bb = self.ctx.append_basic_block(current, "test"); let test_bb = self.ctx.append_basic_block(current, "test");
let body_bb = self.ctx.append_basic_block(current, "body"); let body_bb = self.ctx.append_basic_block(current, "body");
let cont_bb = self.ctx.append_basic_block(current, "cont"); let mut cont_bb = None; // self.ctx.append_basic_block(current, "cont");
// if there is no orelse, we just go to cont_bb // if there is no orelse, we just go to cont_bb
let orelse_bb = if orelse.is_empty() { let orelse_bb = if orelse.is_empty() {
cont_bb cont_bb = Some(self.ctx.append_basic_block(current, "cont"));
cont_bb.unwrap()
} else { } else {
self.ctx.append_basic_block(current, "orelse") self.ctx.append_basic_block(current, "orelse")
}; };
@ -151,7 +152,10 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
} }
} }
if !exited { if !exited {
self.builder.build_unconditional_branch(cont_bb); if cont_bb.is_none() {
cont_bb = Some(self.ctx.append_basic_block(current, "cont"));
}
self.builder.build_unconditional_branch(cont_bb.unwrap());
} }
if !orelse.is_empty() { if !orelse.is_empty() {
exited = false; exited = false;
@ -163,10 +167,15 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
} }
} }
if !exited { if !exited {
self.builder.build_unconditional_branch(cont_bb); if cont_bb.is_none() {
cont_bb = Some(self.ctx.append_basic_block(current, "cont"));
}
self.builder.build_unconditional_branch(cont_bb.unwrap());
} }
} }
self.builder.position_at_end(cont_bb); if let Some(cont_bb) = cont_bb {
self.builder.position_at_end(cont_bb);
}
} }
StmtKind::While { test, body, orelse } => { StmtKind::While { test, body, orelse } => {
let current = self.builder.get_insert_block().unwrap().get_parent().unwrap(); let current = self.builder.get_insert_block().unwrap().get_parent().unwrap();