diff --git a/compiler/gen/src/llvm/build.rs b/compiler/gen/src/llvm/build.rs index bf8b9a7261..3b99235197 100644 --- a/compiler/gen/src/llvm/build.rs +++ b/compiler/gen/src/llvm/build.rs @@ -1102,13 +1102,7 @@ fn call_with_args<'a, 'ctx, 'env>( // Bounds check: only proceed if index < length. // Otherwise, return the list unaltered. - // - // Note: Check for index < length as the "true" condition, - // to avoid misprediction. (In practice this should usually pass, - // and CPUs generally default to predicting that a forward jump - // shouldn't be taken; that is, they predict "else" won't be taken.) - let comparison = - builder.build_int_compare(IntPredicate::ULT, elem_index, list_len, "bounds_check"); + let comparison = bounds_check_comparison(builder, elem_index, list_len); // If the index is in bounds, clone and mutate in place. let then_val: BasicValueEnum = { @@ -1272,3 +1266,16 @@ fn clone_list<'a, 'ctx, 'env>( (struct_val.into_struct_value(), ptr_val) } + +fn bounds_check_comparison<'ctx>( + builder: &Builder<'ctx>, + elem_index: IntValue<'ctx>, + len: IntValue<'ctx>, +) -> IntValue<'ctx> { + // + // Note: Check for index < length as the "true" condition, + // to avoid misprediction. (In practice this should usually pass, + // and CPUs generally default to predicting that a forward jump + // shouldn't be taken; that is, they predict "else" won't be taken.) + builder.build_int_compare(IntPredicate::ULT, elem_index, len, "bounds_check") +}