From 3e1d141e4d22708788af1a14401f257f4153fbae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Wed, 9 Aug 2017 14:19:18 +0200 Subject: Generalize optimization of "one-armed" cases A 'case' expression will force a stack frame (essentially in the same way as a function call), unless it is at the end of a function. In sys_core_fold there is an optimization that can optimize one-armed cases such as: case Expr of Pat1 -> DoSomething; Pat2 -> erlang:error(bad) end, MoreCode. Because only one arm of the 'case' can succeed, the code after the case can be move into the successful arm: case Expr of Pat1 -> DoSomething, MoreCode; Pat2 -> erlang:error(bad) end. Thus, the 'case' is at the end of the function and it will no longer need a stack frame. However, the optimization in sys_core_fold would not be applied if there were more than one failing clause such as in this code: case Expr of Pat1 -> DoSomething, MoreCode; Pat2 -> erlang:error(bad); _ -> erlang:error(case_clause) end. Generalize the optimization to handle any number of failing clauses at the end of the case. Reported-by: bugs.erlang.org/browse/ERL-452 --- lib/compiler/test/core_fold_SUITE.erl | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'lib/compiler/test/core_fold_SUITE.erl') diff --git a/lib/compiler/test/core_fold_SUITE.erl b/lib/compiler/test/core_fold_SUITE.erl index 0097e28d4d..262967d03d 100644 --- a/lib/compiler/test/core_fold_SUITE.erl +++ b/lib/compiler/test/core_fold_SUITE.erl @@ -26,7 +26,8 @@ unused_multiple_values_error/1,unused_multiple_values/1, multiple_aliases/1,redundant_boolean_clauses/1, mixed_matching_clauses/1,unnecessary_building/1, - no_no_file/1,configuration/1,supplies/1]). + no_no_file/1,configuration/1,supplies/1, + redundant_stack_frame/1]). -export([foo/0,foo/1,foo/2,foo/3]). @@ -45,7 +46,8 @@ groups() -> unused_multiple_values_error,unused_multiple_values, multiple_aliases,redundant_boolean_clauses, mixed_matching_clauses,unnecessary_building, - no_no_file,configuration,supplies]}]. + no_no_file,configuration,supplies, + redundant_stack_frame]}]. init_per_suite(Config) -> @@ -527,4 +529,26 @@ supplies(_Config) -> do_supplies(#{1 := Value}) when byte_size(Value), byte_size(kg) -> working. +redundant_stack_frame(_Config) -> + {1,2} = do_redundant_stack_frame(#{x=>1,y=>2}), + {'EXIT',{{badkey,_,x},_}} = (catch do_redundant_stack_frame(#{y=>2})), + {'EXIT',{{badkey,_,y},_}} = (catch do_redundant_stack_frame(#{x=>1})), + ok. + +do_redundant_stack_frame(Map) -> + %% There should not be a stack frame for this function. + X = case Map of + #{x := X0} -> + X0; + #{} -> + erlang:error({badkey, Map, x}) + end, + Y = case Map of + #{y := Y0} -> + Y0; + #{} -> + erlang:error({badkey, Map, y}) + end, + {X, Y}. + id(I) -> I. -- cgit v1.2.3