Cheap optimisation for boolean conditions in Switch statements

This commit is contained in:
Brian Carroll 2021-11-12 08:59:19 +00:00
parent 78a7e45b8f
commit 3a751aa3e8

View file

@ -4,7 +4,7 @@ use code_builder::Align;
use roc_collections::all::MutMap;
use roc_module::symbol::Symbol;
use roc_mono::ir::{CallType, Expr, JoinPointId, Literal, Proc, Stmt};
use roc_mono::layout::{Layout, LayoutIds};
use roc_mono::layout::{Builtin, Layout, LayoutIds};
use crate::layout::WasmLayout;
use crate::low_level::{build_call_low_level, LowlevelBuildResult};
@ -326,6 +326,7 @@ impl<'a> WasmBackend<'a> {
self.start_block(BlockType::NoResult)
}
let is_bool = matches!(cond_layout, Layout::Builtin(Builtin::Int1));
let cond_type = WasmLayout::new(cond_layout).value_type();
// then, we jump whenever the value under scrutiny is equal to the value of a branch
@ -334,6 +335,12 @@ impl<'a> WasmBackend<'a> {
self.storage
.load_symbols(&mut self.code_builder, &[*cond_symbol]);
if is_bool {
// We already have a bool, don't need to compare against a const to get one
if *value == 0 {
self.code_builder.i32_eqz();
}
} else {
match cond_type {
ValueType::I32 => {
self.code_builder.i32_const(*value as i32);
@ -352,6 +359,7 @@ impl<'a> WasmBackend<'a> {
self.code_builder.f64_eq();
}
}
}
// "break" out of `i` surrounding blocks
self.code_builder.br_if(i as u32);