beam_bounds: Tighten bounds for band, bor, and bxor#11005
Open
NelsonVides wants to merge 1 commit intoerlang:masterfrom
Open
beam_bounds: Tighten bounds for band, bor, and bxor#11005NelsonVides wants to merge 1 commit intoerlang:masterfrom
NelsonVides wants to merge 1 commit intoerlang:masterfrom
Conversation
Add fallback clauses for bitwise operations when operand ranges exceed
NUM_BITS or involve sign combinations not previously handled. Values
that exceed NUM_BITS are widened to +inf/-inf via a new cap/1 helper to
keep bounds within the representable range.
For band:
- One non-negative operand: result in [0, cap(B)].
- Both strictly negative: result in [-inf, min(B, D)].
- Both straddling zero: result in [-inf, cap(max(B, D))].
- Both non-negative exceeding NUM_BITS: [0, cap(min(B, D))].
For bor:
- Both non-negative exceeding NUM_BITS: [0, +inf].
- Both non-positive exceeding NUM_BITS: [-inf, 0].
For bxor:
- Both non-negative exceeding NUM_BITS: [0, +inf].
- One strictly negative, one non-negative: [-inf, -1].
These tighter bounds enable the compiler to eliminate dead branches in
code that performs comparisons after bitwise operations. For example,
given:
%% bxor with one negative, one non-negative: result is always negative.
bxor_negative(X, Y) when is_integer(X), X >= 0,
is_integer(Y), Y < 0 ->
Z = X bxor Y,
case Z < 0 of
true -> negative;
false -> non_negative
end.
%% band with both negative: result is always negative.
band_neg(X, Y) when is_integer(X), X < 0,
is_integer(Y), Y < 0 ->
Z = X band Y,
case Z < 0 of
true -> negative;
false -> non_negative
end.
The compiler previously emitted:
{function, bxor_negative, 2, 4}.
{label,3}.
{line,[{location,"test_bounds_example.erl",16}]}.
{func_info,{atom,test_bounds_example},{atom,bxor_negative},2}.
{label,4}.
{test,is_integer,{f,3},[{x,0}]}.
{test,is_ge,{f,3},[{tr,{x,0},{t_integer,any}},{integer,0}]}.
{test,is_integer,{f,3},[{x,1}]}.
{test,is_ge,{f,3},[{integer,-1},{tr,{x,1},{t_integer,any}}]}.
{line,[{location,"test_bounds_example.erl",18}]}.
{gc_bif,'bxor',
{f,0},
2,
[{tr,{x,0},{t_integer,{0,'+inf'}}},
{tr,{x,1},{t_integer,{'-inf',-1}}}],
{x,0}}.
{test,is_ge,{f,5},[{integer,-1},{tr,{x,0},{t_integer,any}}]}.
{move,{atom,negative},{x,0}}.
return.
{label,5}.
{move,{atom,non_negative},{x,0}}.
return.
{function, band_neg, 2, 7}.
{label,6}.
{line,[{location,"test_bounds_example.erl",25}]}.
{func_info,{atom,test_bounds_example},{atom,band_neg},2}.
{label,7}.
{test,is_integer,{f,6},[{x,0}]}.
{test,is_ge,{f,6},[{integer,-1},{tr,{x,0},{t_integer,any}}]}.
{test,is_integer,{f,6},[{x,1}]}.
{test,is_ge,{f,6},[{integer,-1},{tr,{x,1},{t_integer,any}}]}.
{line,[{location,"test_bounds_example.erl",27}]}.
{gc_bif,'band',
{f,0},
2,
[{tr,{x,0},{t_integer,{'-inf',-1}}},
{tr,{x,1},{t_integer,{'-inf',-1}}}],
{x,0}}.
{test,is_ge,{f,8},[{integer,-1},{tr,{x,0},{t_integer,any}}]}.
{move,{atom,negative},{x,0}}.
return.
{label,8}.
{move,{atom,non_negative},{x,0}}.
return.
and it now emits:
{function, bxor_negative, 2, 4}.
{label,3}.
{line,[{location,"test_bounds_example.erl",16}]}.
{func_info,{atom,test_bounds_example},{atom,bxor_negative},2}.
{label,4}.
{test,is_integer,{f,3},[{x,0}]}.
{test,is_ge,{f,3},[{tr,{x,0},{t_integer,any}},{integer,0}]}.
{test,is_integer,{f,3},[{x,1}]}.
{test,is_ge,{f,3},[{integer,-1},{tr,{x,1},{t_integer,any}}]}.
{move,{atom,negative},{x,0}}.
return.
{function, band_neg, 2, 6}.
{label,5}.
{line,[{location,"test_bounds_example.erl",25}]}.
{func_info,{atom,test_bounds_example},{atom,band_neg},2}.
{label,6}.
{test,is_integer,{f,5},[{x,0}]}.
{test,is_ge,{f,5},[{integer,-1},{tr,{x,0},{t_integer,any}}]}.
{test,is_integer,{f,5},[{x,1}]}.
{test,is_ge,{f,5},[{integer,-1},{tr,{x,1},{t_integer,any}}]}.
{move,{atom,negative},{x,0}}.
return.
Contributor
CT Test Results 2 files 335 suites 8m 59s ⏱️ Results for commit 5564208. ♻️ This comment has been updated with latest results. To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass. See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally. Artifacts
// Erlang/OTP Github Action Bot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add fallback clauses for bitwise operations when operand ranges exceed NUM_BITS or involve sign combinations not previously handled. Values that exceed NUM_BITS are widened to +inf/-inf via a new cap/1 helper to keep bounds within the representable range.
These tighter bounds enable the compiler to eliminate dead branches in code that performs comparisons after bitwise operations. For example, given:
The compiler previously emitted:
{function, bxor_negative, 2, 4}. {label,3}. {line,[{location,"test_bounds_example.erl",16}]}. {func_info,{atom,test_bounds_example},{atom,bxor_negative},2}. {label,4}. {test,is_integer,{f,3},[{x,0}]}. {test,is_ge,{f,3},[{tr,{x,0},{t_integer,any}},{integer,0}]}. {test,is_integer,{f,3},[{x,1}]}. {test,is_ge,{f,3},[{integer,-1},{tr,{x,1},{t_integer,any}}]}. {line,[{location,"test_bounds_example.erl",18}]}. {gc_bif,'bxor', {f,0}, 2, [{tr,{x,0},{t_integer,{0,'+inf'}}}, {tr,{x,1},{t_integer,{'-inf',-1}}}], {x,0}}. {test,is_ge,{f,5},[{integer,-1},{tr,{x,0},{t_integer,any}}]}. {move,{atom,negative},{x,0}}. return. {label,5}. {move,{atom,non_negative},{x,0}}. return. {function, band_neg, 2, 7}. {label,6}. {line,[{location,"test_bounds_example.erl",25}]}. {func_info,{atom,test_bounds_example},{atom,band_neg},2}. {label,7}. {test,is_integer,{f,6},[{x,0}]}. {test,is_ge,{f,6},[{integer,-1},{tr,{x,0},{t_integer,any}}]}. {test,is_integer,{f,6},[{x,1}]}. {test,is_ge,{f,6},[{integer,-1},{tr,{x,1},{t_integer,any}}]}. {line,[{location,"test_bounds_example.erl",27}]}. {gc_bif,'band', {f,0}, 2, [{tr,{x,0},{t_integer,{'-inf',-1}}}, {tr,{x,1},{t_integer,{'-inf',-1}}}], {x,0}}. {test,is_ge,{f,8},[{integer,-1},{tr,{x,0},{t_integer,any}}]}. {move,{atom,negative},{x,0}}. return. {label,8}. {move,{atom,non_negative},{x,0}}. return.and it now emits:
{function, bxor_negative, 2, 4}. {label,3}. {line,[{location,"test_bounds_example.erl",16}]}. {func_info,{atom,test_bounds_example},{atom,bxor_negative},2}. {label,4}. {test,is_integer,{f,3},[{x,0}]}. {test,is_ge,{f,3},[{tr,{x,0},{t_integer,any}},{integer,0}]}. {test,is_integer,{f,3},[{x,1}]}. {test,is_ge,{f,3},[{integer,-1},{tr,{x,1},{t_integer,any}}]}. {move,{atom,negative},{x,0}}. return. {function, band_neg, 2, 6}. {label,5}. {line,[{location,"test_bounds_example.erl",25}]}. {func_info,{atom,test_bounds_example},{atom,band_neg},2}. {label,6}. {test,is_integer,{f,5},[{x,0}]}. {test,is_ge,{f,5},[{integer,-1},{tr,{x,0},{t_integer,any}}]}. {test,is_integer,{f,5},[{x,1}]}. {test,is_ge,{f,5},[{integer,-1},{tr,{x,1},{t_integer,any}}]}. {move,{atom,negative},{x,0}}. return.Note, I'm assuming two complement's integer representation, for which I'm 99% sure it is the case 🤔