File 3972-Introduce-beam_ssa-linearize_only-1.patch of Package erlang

From a2a6484d3a46911419e4e22353178abc26018a01 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Sat, 27 Sep 2025 06:54:37 +0200
Subject: [PATCH 2/4] Introduce beam_ssa:linearize_only/1

Introduce a faster version of beam_ssa:linearize/1 called
beam_ssa:linearize_only/1. It can be used when we know that
there is no need adjust phi nodes.
---
 lib/compiler/src/beam_ssa.erl             | 17 +++++++++++++++++
 lib/compiler/src/beam_ssa_codegen.erl     |  2 +-
 lib/compiler/src/beam_ssa_opt.erl         |  8 ++++----
 lib/compiler/src/beam_ssa_pre_codegen.erl |  2 +-
 4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/lib/compiler/src/beam_ssa.erl b/lib/compiler/src/beam_ssa.erl
index df9e6a74d4..7f00775955 100644
--- a/lib/compiler/src/beam_ssa.erl
+++ b/lib/compiler/src/beam_ssa.erl
@@ -35,6 +35,7 @@
          insert_on_edges/3,
          is_loop_header/1,
          linearize/1,
+         linearize_only/1,
          mapfold_blocks/4,
          mapfold_instrs/4,
          merge_blocks/2,
@@ -653,6 +654,22 @@ linearize(Blocks) when is_map(Blocks) ->
     Linear = fix_phis(Linear0, maps:from_list(Linear0)),
     Linear.
 
+%% linearize_only(Blocks) -> [{BlockLabel,#b_blk{}}].
+%%  Linearize the intermediate representation of the code, discarding
+%%  unreachable blocks. Phi nodes will not be be adjusted, which makes
+%%  this function somewhat faster than linearize/1. However, it must
+%%  only be called when we KNOW that there is no need to adjust phi
+%%  nodes.
+
+-spec linearize_only(Blocks) -> Linear when
+      Blocks :: block_map(),
+      Linear :: [{label(),b_blk()}].
+
+linearize_only(Blocks) when is_map(Blocks) ->
+    Seen = sets:new(),
+    {Linear,_} = linearize_1([0], Blocks, Seen, []),
+    Linear.
+
 -spec rpo(Blocks) -> [Label] when
       Blocks :: block_map(),
       Label :: label().
diff --git a/lib/compiler/src/beam_ssa_codegen.erl b/lib/compiler/src/beam_ssa_codegen.erl
index 5ddf87c0f5..1d27d1d0b5 100644
--- a/lib/compiler/src/beam_ssa_codegen.erl
+++ b/lib/compiler/src/beam_ssa_codegen.erl
@@ -2387,7 +2387,7 @@ successors(#cg_ret{}) -> [].
 %%  used only in this module.
 
 linearize(Blocks) ->
-    Linear = beam_ssa:linearize(Blocks),
+    Linear = beam_ssa:linearize_only(Blocks),
     linearize_1(Linear, Blocks).
 
 linearize_1([{?EXCEPTION_BLOCK,_}|Ls], Blocks) ->
diff --git a/lib/compiler/src/beam_ssa_opt.erl b/lib/compiler/src/beam_ssa_opt.erl
index 5b3b47e656..e2de01639e 100644
--- a/lib/compiler/src/beam_ssa_opt.erl
+++ b/lib/compiler/src/beam_ssa_opt.erl
@@ -857,7 +857,7 @@ are_all_literals(Args) ->
 ssa_opt_element({#opt_st{ssa=Blocks}=St, FuncDb}) ->
     %% Collect the information about element instructions in this
     %% function.
-    GetEls = collect_element_calls(beam_ssa:linearize(Blocks)),
+    GetEls = collect_element_calls(beam_ssa:linearize_only(Blocks)),
 
     %% Collect the element instructions into chains. The
     %% element calls in each chain are ordered in reverse
@@ -1633,7 +1633,7 @@ ssa_opt_live({#opt_st{ssa=Linear0}=St, FuncDb}) ->
     RevLinear = reverse(Linear0),
     Blocks0 = maps:from_list(RevLinear),
     Blocks = live_opt(RevLinear, #{}, Blocks0),
-    Linear = beam_ssa:linearize(Blocks),
+    Linear = beam_ssa:linearize_only(Blocks),
     {St#opt_st{ssa=Linear}, FuncDb}.
 
 live_opt([{L,Blk0}|Bs], LiveMap0, Blocks) ->
@@ -1762,7 +1762,7 @@ ssa_opt_try({#opt_st{ssa=SSA0,cnt=Count0}=St, FuncDb}) ->
     {St#opt_st{ssa=SSA,cnt=Count}, FuncDb}.
 
 opt_try(Blocks, Count0) when is_map(Blocks) ->
-    {Count, Linear} = opt_try(beam_ssa:linearize(Blocks), Count0),
+    {Count, Linear} = opt_try(beam_ssa:linearize_only(Blocks), Count0),
     {Count, maps:from_list(Linear)};
 opt_try(Linear, Count0) when is_list(Linear) ->
     {Count, Shrunk} = shrink_try(Linear, Count0, []),
@@ -2854,7 +2854,7 @@ do_ssa_opt_sink(Defs, #opt_st{ssa=Linear}=St) when is_map(Defs) ->
                            move_defs(V, From, To, A)
                    end, Blocks0, DefLocs),
 
-    St#opt_st{ssa=beam_ssa:linearize(Blocks)}.
+    St#opt_st{ssa=beam_ssa:linearize_only(Blocks)}.
 
 def_blocks([{L,#b_blk{is=Is}}|Bs]) ->
     def_blocks_is(Is, L, def_blocks(Bs));
diff --git a/lib/compiler/src/beam_ssa_pre_codegen.erl b/lib/compiler/src/beam_ssa_pre_codegen.erl
index 6d60ac6c63..89ee13b942 100644
--- a/lib/compiler/src/beam_ssa_pre_codegen.erl
+++ b/lib/compiler/src/beam_ssa_pre_codegen.erl
@@ -1188,7 +1188,7 @@ find_fc_errors([], Acc) ->
 %%%   Expands the update_tuple psuedo-instruction into its actual instructions.
 %%%
 expand_update_tuple(#st{ssa=Blocks0,cnt=Count0}=St) ->
-    Linear0 = beam_ssa:linearize(Blocks0),
+    Linear0 = beam_ssa:linearize_only(Blocks0),
     {Linear, Count} = expand_update_tuple_1(Linear0, Count0, []),
     Blocks = maps:from_list(Linear),
     St#st{ssa=Blocks,cnt=Count}.
-- 
2.51.0

openSUSE Build Service is sponsored by