aboutsummaryrefslogtreecommitdiffstats
path: root/lib/compiler
AgeCommit message (Collapse)Author
2016-11-18v3_kernel: Generate optimized code for guardsBjörn Gustavsson
The compiler produces poor code for complex guard expressions with andalso/orelse. Here is an example from the filename module: -define(IS_DRIVELETTER(Letter),(((Letter >= $A) andalso (Letter =< $Z)) orelse ((Letter >= $a) andalso (Letter =< $z)))). skip_prefix(Name, false) -> Name; skip_prefix([L, DrvSep|Name], DrvSep) when ?IS_DRIVELETTER(L) -> Name; skip_prefix(Name, _) -> Name. beam_bool fails to simplify the code for the guard, leaving several 'bif' instructions: {function, skip_prefix, 2, 49}. {label,48}. {line,[{location,"filename.erl",187}]}. {func_info,{atom,filename},{atom,skip_prefix},2}. {label,49}. {test,is_ne_exact,{f,52},[{x,1},{atom,false}]}. {test,is_nonempty_list,{f,52},[{x,0}]}. {get_list,{x,0},{x,2},{x,3}}. {test,is_nonempty_list,{f,52},[{x,3}]}. {get_list,{x,3},{x,4},{x,5}}. {bif,'=:=',{f,52},[{x,1},{x,4}],{x,6}}. {test,is_ge,{f,50},[{x,2},{integer,65}]}. {bif,'=<',{f,52},[{x,2},{integer,90}],{x,7}}. {test,is_eq_exact,{f,51},[{x,7},{atom,false}]}. {test,is_ge,{f,50},[{x,2},{integer,97}]}. {bif,'=<',{f,52},[{x,2},{integer,122}],{x,7}}. {jump,{f,51}}. {label,50}. {move,{atom,false},{x,7}}. {label,51}. {bif,'=:=',{f,52},[{x,7},{atom,true}],{x,7}}. {test,is_eq_exact,{f,52},[{x,6},{atom,true}]}. {test,is_eq_exact,{f,52},[{x,7},{atom,true}]}. {move,{x,5},{x,0}}. return. {label,52}. return. We can add optimizations of guard tests to v3_kernel to achive a better result: {function, skip_prefix, 2, 49}. {label,48}. {line,[{location,"filename.erl",187}]}. {func_info,{atom,filename},{atom,skip_prefix},2}. {label,49}. {test,is_ne_exact,{f,51},[{x,1},{atom,false}]}. {test,is_nonempty_list,{f,51},[{x,0}]}. {get_list,{x,0},{x,2},{x,3}}. {test,is_nonempty_list,{f,51},[{x,3}]}. {get_list,{x,3},{x,4},{x,5}}. {test,is_eq_exact,{f,51},[{x,1},{x,4}]}. {test,is_ge,{f,51},[{x,2},{integer,65}]}. {test,is_lt,{f,50},[{integer,90},{x,2}]}. {test,is_ge,{f,51},[{x,2},{integer,97}]}. {test,is_ge,{f,51},[{integer,122},{x,2}]}. {label,50}. {move,{x,5},{x,0}}. return. {label,51}. return. Looking at the STDLIB application, there were 112 lines of BIF calls in guards that beam_bool failed to convert to test instructions. This commit eliminates all those BIF calls. Here is how I counted the instructions: $ PATH=$ERL_TOP/bin:$PATH erlc -I ../include -I ../../kernel/include -S *.erl $ grep "bif,'[=<>]" *.S | grep -v f,0 dets.S: {bif,'=:=',{f,547},[{x,4},{atom,read_write}],{x,4}}. dets.S: {bif,'=:=',{f,547},[{x,5},{atom,saved}],{x,5}}. dets.S: {bif,'=:=',{f,589},[{x,5},{atom,read}],{x,5}}. . . . $ grep "bif,'[=<>]" *.S | grep -v f,0 | wc 112 224 6765 $
2016-11-13sys_core_fold: Remove unnecessary calls to opt_bool_case/1Björn Gustavsson
The fixpoint iteration added in 05130e48 makes those calls superfluous.
2016-11-13record_SUITE: Strengthen test of record access in guardsBjörn Gustavsson
2016-11-11guard_SUITE: Add more test of guardsBjörn Gustavsson
2016-11-11compile_SUITE: Smoke test and cover more of v3_kernel_ppBjörn Gustavsson
2016-11-03Merge branch 'gandrade/fmod/PR-1216/OTP-14000'Björn Gustavsson
* gandrade/fmod/PR-1216/OTP-14000: Add test cases for math:fmod/2 BIF Support math:fmod/2 BIF on compiler Add math:fmod/2 BIF
2016-11-02Add test cases for math:fmod/2 BIFGuilherme Andrade
2016-11-02Support math:fmod/2 BIF on compilerGuilherme Andrade
2016-10-28sys_core_fold: Use less effort optimizing not in letsBjörn Gustavsson
There are two calls opt_not_in_let(). Since 05130e4855 introduced iteration to a fixpoint, only the first call is needed. Removing the redundant call will slightly speed up compilation.
2016-10-28sys_core_fold: Eliminate complaint from core_lintBjörn Gustavsson
2016-10-28Merge branch 'maint'Björn Gustavsson
* maint: Don't copy funs into guards
2016-10-27Don't copy funs into guardsBjörn Gustavsson
Funs must not be created in guards. The instruction for creating a fun clobbers all X registers, which is a bad thing to do in a guard.
2016-10-26Merge branch 'maint'Björn Gustavsson
* maint: Don't let inline_list_funcs degrade optimizations
2016-10-24Don't let inline_list_funcs degrade optimizationsBjörn Gustavsson
83199af0263 refactored sys_core_fold to break out the code for the inline_lists_funcs option to its own module. Unfortunately, it also accidentally turned off compile-time evaluation of calls to BIFs with wholly or partial constant arguments. For example, the code for the following funtion gets much worse when inline_list_funcs is used: b() -> R0 = #r{}, R1 = setelement(1+2, R0, "deux"), R2 = setelement(1+3, R1, "trois"), R3 = setelement(1+5, R2, "cinq"), R4 = setelement(1+2, R3, "DEUX"), R4. ERL-285
2016-10-07Merge branch 'maint'Björn Gustavsson
* maint: beam_bsm: Eliminate unsafe optimization
2016-10-06beam_bsm: Eliminate unsafe optimizationBjörn Gustavsson
The following code causes a compiler failure: first_after(Data, Offset) -> case byte_size(Data) > Offset of false -> {First, Rest} = {ok, ok}, ok; true -> <<_:Offset/binary, Rest/binary>> = Data, %% 'Rest' saved in y(0) before the call. {First, _} = match_first(Data, Rest), %% When beam_bsm sees the code, the following line %% which uses y(0) has been optimized away. {First, Rest} = {First, Rest}, First end. match_first(_, <<First:1/binary, Rest/binary>>) -> {First, Rest}. Here is the error message from beam_validator: t: function first_after/2+15: Internal consistency check failed - please report this bug. Instruction: {call,2,{f,7}} Error: {multiple_match_contexts,[{x,1},0]}: Basically, what happens is that at time of code generation, the variable 'Rest' is needed after the call to match_first/2 and is therefore saved in y(0). When beam_bsm (a late optimization pass) sees the code, the use of y(0) following the call to match_first/2 has been optimized away. beam_bsm therefore assumes that the delayed sub-binary creation is safe. (Actually, it is safe, but beam_validator does not realize it.) The bug was caused by two separate commits: e199e2471a reduced the number of special cases to handle in BEAM optimization passed by breaking apart the tail-recursive call instructions (call_only and call_last) into separate instructions. Unfortunately, the special handling for tail calls was lost, which resulted in worse code (i.e. the delayed sub-binary creation optimization could not be applied). e1aa422290 tried to compensate, but did so in a way that was not always safe. Teaching beam_validator that this kind of code is safe would be expensive. Instead, we will undo the damage caused by the two commits. Re-introduce the special handling of tail-recursive calls in beam_bsm that was lost in the first commit. (Effectively) revert the change in the second commit. ERL-268
2016-10-05Merge branch 'maint'Björn Gustavsson
* maint: Update primary bootstrap beam_block: Avoid unsafe inclusion of get_map_elements in blocks
2016-10-05beam_block: Avoid unsafe inclusion of get_map_elements in blocksBjörn Gustavsson
c2035ebb8b restricted the get_map_elements instruction so that it could only occur at the beginning of a block. It turns out that including it anywhere in a block is unsafe. Therefore, never put get_map_elements instruction in blocks. (Also remove the beam_utils:join_even/2 function since it is no longer used.) ERL-266
2016-09-29Merge branch 'rickard/time-unit/OTP-13831'Rickard Green
* rickard/time-unit/OTP-13831: Replace usage of deprecated time units
2016-09-29Merge branch 'josevalim/compiler/at-var/PR-1081/OTP-13924'Björn Gustavsson
* josevalim/compiler/at-var/PR-1081/OTP-13924: Use @ in variable names generated by core and kernel
2016-09-26sys_core_fold: Run optimizations to a fixpointBjörn Gustavsson
Run the optimizations until a fixpoint is reached, or until the maximum iteration count is reached. The hope is that in the future we can many small optimizations instead of optimizations that try to do everything in one pass. This change allows us to remove the ad-hoc calls to expr/2 to run more optimizations on a piece of code.
2016-09-26sys_core_fold: Improve case optimizationBjörn Gustavsson
The optimization that avoids building a tuple in a case expression would not work if any clause matched a tuple as in the following example: f(A, B) -> case {A,B} of {<<X>>,Y} -> {X,Y} end. The generated Core Erlang code would look like this (note the tuples in the case expression and the pattern): 'f'/2 = fun (_cor1,_cor0) -> case {_cor1,_cor0} of <{#{#<X>(8,1,'integer',['unsigned'|['big']])}#,Y}> when 'true' -> {X,Y} . . . end It is expected that the code should look like this (note that tuples have been replaced with "values"): 'f'/2 = fun (_cor1,_cor0) -> %% Line 5 case <_cor1,_cor0> of <#{#<X>(8,1,'integer',['unsigned'|['big']])}#,Y> -> {X,Y} . . . end While at it, also fix bugs in the handling of pattern with aliases. The bindings were produced in the wrong order (creating 'let's with referring to free variables), but in most cases the incorrect bindings were discarded later without causing any harm.
2016-09-26sys_core_fold: Correct scope verification codeBjörn Gustavsson
703e8f4490bf broke the scope verification code (by calling ordsets:is_subset/2 with an unsorted second argument). While we are it, also optimize the verification function by avoiding converting the map to a sorted list.
2016-09-26core_pp: Correct printing of map updatesBjörn Gustavsson
2016-09-26Use @ in variable names generated by core and kernelJosé Valim
The previous variable names can be generated by projects like LFE and Elixir, leading to possible conflicts. Our first to choice to solve such conflicts was to use $ but that's not a valid variable name in core. Therefore we picked @ which is currently supported and still reduces the chance of conflicts.
2016-09-21Simplify handling of internal BIFsBjörn Gustavsson
Do a simpler translation of internal BIFs. While we are it, also remove the dummy values of Index and Uniq from the make_fun internal operation.
2016-09-21v3_life: Eliminate special handling of guardsBjörn Gustavsson
Remove the special handling #k_try{} in guards in v3_life. If we introduce a new #k_protected{} record in v3_kernel, v3_life no longer needs to know whether it is processing guards or bodies.
2016-09-21Simplify beam_utilsBjörn Gustavsson
When beam_utils was first written, it did not have the functions for testing whether a register was not used. Those were added later, in sort of a hacky way. Also, is_killed*() and is_not_used*() for Y registers would return the same answer. Fix that to make the API more consistent (an Y register can only be killed by a deallocate/1 instruction). We will need to change beam_trim to call beam_utils:is_not_used/3 instead of beam_utils:is_killed/3.
2016-09-21bs_match_SUITE: Add a test case for beam_utilsBjörn Gustavsson
During development, a bug in beam_utils caused a compiler failure in xmerl. If the bug reappears, make sure that we catch it when compiling the compiler test suite.
2016-09-21bif_SUITE: Cover the remaining uncovered linesBjörn Gustavsson
2016-09-21erl_bifs: Remove error_logger:warning_map/0 as a safe BIFBjörn Gustavsson
There is no need to list every obscure safe BIF in erl_bifs:is_safe/3. The purpose of erl_bifs:is_safe/3 is merely to warn when the return value of one of the safe BIFs is ignored.
2016-09-21beam_validator: Correct reporting of y register numberBjörn Gustavsson
The error would be: {multiple_match_contexts,[{x,0},2]} instead of: {multiple_match_contexts,[{x,0},{y,2}]}
2016-09-21beam_jump: Don't try to handle a label at the very endBjörn Gustavsson
Since the beam_a pass has always been run and have removed any unused label, there can never be a label as the very last instruction in a function.
2016-09-21beam_jump: Simplify eliminate_fallthroughs/2Björn Gustavsson
eliminate_fallthroughs/2 has special code to handle two labels next to each other, but that does not seem to ever happen and there was one line uncovered in is_label/1. Since inserting an extra jump between two labels would not cause any real problems, remove the extra handling of two consecutive labels.
2016-09-21beam_listing: Remove support for listing sys_pre_expand formatBjörn Gustavsson
The compiler stopped using sys_pre_expand in ae3e177c514c354831.
2016-09-21Merge tag 'OTP-19.1'Henrik Nord
=== OTP-19.1 === Changed Applications: - asn1-4.0.4 - common_test-1.12.3 - compiler-7.0.2 - crypto-3.7.1 - debugger-4.2.1 - dialyzer-3.0.2 - diameter-1.12.1 - edoc-0.8 - erl_docgen-0.6 - erl_interface-3.9.1 - erts-8.1 - eunit-2.3.1 - gs-1.6.2 - hipe-3.15.2 - ic-4.4.2 - inets-6.3.3 - jinterface-1.7.1 - kernel-5.1 - mnesia-4.14.1 - observer-2.2.2 - odbc-2.11.3 - parsetools-2.1.3 - reltool-0.7.2 - runtime_tools-1.10.1 - sasl-3.0.1 - snmp-5.2.4 - ssh-4.3.2 - ssl-8.0.2 - stdlib-3.1 - syntax_tools-2.1 - tools-2.8.6 - wx-1.7.1 - xmerl-1.3.12 Unchanged Applications: - cosEvent-2.2.1 - cosEventDomain-1.2.1 - cosFileTransfer-1.2.1 - cosNotification-1.2.2 - cosProperty-1.2.1 - cosTime-1.2.2 - cosTransactions-1.3.2 - eldap-1.2.2 - et-1.6 - megaco-3.18.1 - orber-3.8.2 - os_mon-2.4.1 - otp_mibs-1.1.1 - percept-0.9 - public_key-1.2 - typer-0.9.11 Conflicts: OTP_VERSION lib/gs/doc/src/notes.xml lib/gs/vsn.mk
2016-09-20Prepare releaseErlang/OTP
2016-09-14Merge branch 'maint'Björn Gustavsson
* maint: beam_validator: Handle unreachable instructions Turn off parallel make for start scripts Makefile
2016-09-13beam_validator: Handle unreachable instructionsBjörn Gustavsson
ab03678e introduced an optimization in the beam_z pass that could introduce unreachable code in BEAM files (a 'jump' instruction is removed after a 'raise' instruction, but the code following the target of the 'jump' is not removed). Since this situation happens very rarely, there is no point in adding another pass that can remove unreachable code after beam_z. Instead we will make sure that beam_validator can skip the unreachable code. Skipping unreachable code is already done in valfun_1/2 (for historical reasons), but we will also need to do it in val_dsetel/2.
2016-09-07Merge branch 'maint'Björn Gustavsson
* maint: [snmp] Correct bug when path to mib contains UTF-8 characters [ic] Fix but when UTF-8 character in path to idl spec sys_core_fold: Don't move a fun into a guard
2016-09-05sys_core_fold: Don't move a fun into a guardBjörn Gustavsson
Moving a fun into a guard may cause code that is not accepted by beam_validator.
2016-09-05Merge branch 'bjorn/floor-ceiling/OTP-13692'Björn Gustavsson
* bjorn/floor-ceiling/OTP-13692: Add math:floor/1 and math:ceil/1 Implement the new ceil/1 and floor/1 guard BIFs
2016-09-05Add math:floor/1 and math:ceil/1Björn Gustavsson
Add math:floor/1 and math:ceil/1 to avoid unnecessary conversions in floating point expressions. That is, instead of having to write float(floor(X)) as part of a floating point expressions, we can write simply math:floor(X).
2016-09-05Implement the new ceil/1 and floor/1 guard BIFsBjörn Gustavsson
Implement as ceil/1 and floor/1 as new guard BIFs (essentially part of Erlang language). They are guard BIFs because trunc/1 is a guard BIF. It would be strange to have trunc/1 as a part of the language, but not ceil/1 and floor/1.
2016-09-02Fix overridden BIFsBjörn Gustavsson
The filters in a list comprehension can be guard expressions or an ordinary expressions. If a guard expression is used as a filter, an exception will basically mean the same as 'false': t() -> L = [{some_tag,42},an_atom], [X || X <- L, element(1, X) =:= some_tag] %% Returns [{some_tag,42}] On the other hand, if an ordinary expression is used as a filter, there will be an exception: my_element(N, T) -> element(N, T). t() -> L = [{some_tag,42},an_atom], [X || X <- L, my_element(1, X) =:= some_tag] %% Causes a 'badarg' exception when element(1, an_atom) is evaluated It has been allowed for several releases to override a BIF with a local function. Thus, if we define a function called element/2, it will be called instead of the BIF element/2 within the module. We must use the "erlang:" prefix to call the BIF. Therefore, the following code is expected to work the same way as in our second example above: -compile({no_auto_import,[element/2]}). element(N, T) -> erlang:element(N, T). t() -> L = [{some_tag,42},an_atom], [X || X <- L, element(1, X) =:= some_tag]. %% Causes a 'badarg' exception when element(1, an_atom) is evaluated But the compiler refuses to compile the code with the following diagnostic: call to local/imported function element/2 is illegal in guard
2016-09-02Merge branch 'bjorn/eliminate-sys_pre_expand/OTP-13856'Björn Gustavsson
* bjorn/eliminate-sys_pre_expand/OTP-13856: Remove sys_pre_expand xref: Eliminate use of sys_pre_expand debugger: Eliminate use of sys_pre_expand compiler: Eliminate use of sys_pre_expand shell_SUITE: Eliminate references to sys_pre_expand in comments qlc_pt: Simplify code because of updated erl_expand_records Teach erl_expand_records to translate module-less calls erl_internal: Add add_predefined_functions/1 cover: Remove always stale comment
2016-09-02Merge branch 'maint'Hans Bolinder
* maint: doc: Correct errors introduced by Editorial changes
2016-09-01Remove sys_pre_expandBjörn Gustavsson
The previous commits have made sys_pre_expand superfluous. Since sys_pre_expand is undocumented and unsupported it can be removed in a major release without prior deprecation. Also remove code in erl_parse that handles abstract code that has passed through sys_pre_expand. We considered deprecating sys_pre_expand just in case, but decided against it for the following reasons: - Anyone brave and knowledgeable enough to use sys_pre_expand should be able to cope with sys_pre_expand being removed. - If we kept it, but didn't test it anywhere in OTP, it could potentially stop working. So we would probably have to add some test cases.
2016-09-01compiler: Eliminate use of sys_pre_expandBjörn Gustavsson
sys_pre_expand previously did a lot more work, for example, translating records and funs, but now is merely a grab bag of small transformations. Move those transformations to v3_core.
2016-09-01doc: Correct errors introduced by Editorial changesHans Bolinder
Fix some older errors as well.