File 3132-Enhance-range-analysis-for-integer-division.patch of Package erlang

From ad264fc691f4f9673b095ae40e59e32a99ff3477 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Fri, 4 Aug 2023 06:42:24 +0200
Subject: [PATCH 2/6] Enhance range analysis for integer division

Teach beam_bounds:bound/3 to determine a range for the `div` operator
when nothing is known about the divisor. For example:

    div0(A, B) when is_integer(A), 0 =< A, A < 1204 ->
        %% Range for A is 0..1023
        A div B.                % Range is -1023..1023
---
 lib/compiler/src/beam_bounds.erl        | 4 ++++
 lib/compiler/test/beam_bounds_SUITE.erl | 7 +++++++
 2 files changed, 11 insertions(+)

diff --git a/lib/compiler/src/beam_bounds.erl b/lib/compiler/src/beam_bounds.erl
index e1d1ee98d2..95a222cf9d 100644
--- a/lib/compiler/src/beam_bounds.erl
+++ b/lib/compiler/src/beam_bounds.erl
@@ -299,6 +299,10 @@ div_bounds({'-inf',B}, {C,D}) when is_integer(C), C > 0, is_integer(D) ->
     Min = '-inf',
     Max = max(B div C, B div D),
     normalize({Min,Max});
+div_bounds({A,B}, _) when is_integer(A), is_integer(B) ->
+    Max = max(abs(A), abs(B)),
+    Min = -Max,
+    {Min,Max};
 div_bounds(_, _) ->
     any.
 
diff --git a/lib/compiler/test/beam_bounds_SUITE.erl b/lib/compiler/test/beam_bounds_SUITE.erl
index 8b98e50ca9..33f216548d 100644
--- a/lib/compiler/test/beam_bounds_SUITE.erl
+++ b/lib/compiler/test/beam_bounds_SUITE.erl
@@ -129,6 +129,13 @@ division_bounds(_Config) ->
     any = beam_bounds:bounds('div', {10,'+inf'}, {0,0}),
     {'EXIT', {badarith, _}} = catch division_bounds_1([], ok),
 
+    {-10,10} = beam_bounds:bounds('div', {0,10}, any),
+    {-50,50} = beam_bounds:bounds('div', {-50,-15}, {-10,'+inf'}),
+    {-20,20} = beam_bounds:bounds('div', {-20,10}, any),
+    {-7,7} = beam_bounds:bounds('div', {-5,7}, {'-inf',-1}),
+    any = beam_bounds:bounds('div', {'-inf',10}, any),
+    any = beam_bounds:bounds('div', {0,'+inf'}, any),
+
     ok.
 
 %% GH-6604: Division by zero could cause type analysis to hang forever as
-- 
2.35.3

openSUSE Build Service is sponsored by