From b95508c51a334e7c5cf0dff0bb9dd4d55fb098a0 Mon Sep 17 00:00:00 2001 From: julian-seward1 Date: Mon, 16 Sep 2019 15:01:07 +0200 Subject: [PATCH] legalizer/split.rs: split_ebb_params: avoid pointless Vec allocations. (#1025) This function is responsible for 2.2% of all heap allocation (calls) in CL. This change avoids all of them in the (presumably) common case where none of the parameters require splitting. It also slightly reduces the compiler's instruction count. --- cranelift/codegen/src/legalizer/split.rs | 28 +++++++++++++++--------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/cranelift/codegen/src/legalizer/split.rs b/cranelift/codegen/src/legalizer/split.rs index 9f689922f1..5e55419fad 100644 --- a/cranelift/codegen/src/legalizer/split.rs +++ b/cranelift/codegen/src/legalizer/split.rs @@ -131,19 +131,27 @@ fn split_any( } pub fn split_ebb_params(func: &mut ir::Function, cfg: &ControlFlowGraph, ebb: Ebb) { - let mut repairs = Vec::new(); let pos = &mut FuncCursor::new(func).at_top(ebb); + let ebb_params = pos.func.dfg.ebb_params(ebb); - for (num, ebb_param) in pos - .func - .dfg - .ebb_params(ebb) - .to_vec() - .into_iter() - .enumerate() + // Add further splittable types here. + fn type_requires_splitting(ty: Type) -> bool { + ty == ir::types::I128 + } + + // A shortcut. If none of the param types require splitting, exit now. This helps because + // the loop below necessarily has to copy the ebb params into a new vector, so it's better to + // avoid doing so when possible. + if !ebb_params + .iter() + .any(|ebb_param| type_requires_splitting(pos.func.dfg.value_type(*ebb_param))) { - let ty = pos.func.dfg.value_type(ebb_param); - if ty != ir::types::I128 { + return; + } + + let mut repairs = Vec::new(); + for (num, ebb_param) in ebb_params.to_vec().into_iter().enumerate() { + if !type_requires_splitting(pos.func.dfg.value_type(ebb_param)) { continue; }