Extract bounds_check_comparison

This commit is contained in:
Richard Feldman 2020-03-21 22:11:06 -04:00
parent ef38095003
commit 3e71b5a38d

View file

@ -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")
}