I am not an expert on compiler optimization. I'm not even sure what “reasonable” to expect from compilers or optimizations. I'm just curious, and all the questions are asked.
Anyway, I encoded some Erlang in a basic form, such as:
% TOY EXAMPLE 1 test(X) -> if X-1 > 0 -> yes; X-1 == 0 -> maybe; true -> no end.
Then I then optimized it so as not to subtract twice:
% TOY EXAMPLE 2 test(X) -> Z = X-1, if Z > 0 -> yes; Z == 0 -> maybe; true -> no end.
Then I thought that a “waste of time”, of course, the compiler optimizes the first example in the second anyway. "Therefore, I decided to check by running the compilation command: a file with the" S "parameter for both. Here is the output:
% TOY EXAMPLE 1 {function, test, 1, 15}. {label,14}. {func_info,{atom,exchange},{atom,test},1}. {label,15}. {gc_bif,'-',{f,16},1,[{x,0},{integer,1}],{x,1}}. {test,is_lt,{f,16},[{integer,0},{x,1}]}. {move,{atom,yes},{x,0}}. return. {label,16}. {gc_bif,'-',{f,17},1,[{x,0},{integer,1}],{x,1}}. {test,is_eq,{f,17},[{x,1},{integer,0}]}. {move,{atom,maybe},{x,0}}. return. {label,17}. {move,{atom,no},{x,0}}. return. % TOY EXAMPLE 2 {function, test, 1, 15}. {label,14}. {func_info,{atom,exchange},{atom,test},1}. {label,15}. {gc_bif,'-',{f,0},1,[{x,0},{integer,1}],{x,0}}. {test,is_lt,{f,16},[{integer,0},{x,0}]}. {move,{atom,yes},{x,0}}. return. {label,16}. {test,is_eq,{f,17},[{x,0},{integer,0}]}. {move,{atom,maybe},{x,0}}. return. {label,17}. {move,{atom,no},{x,0}}. return.
They do not match. If I read this right (maybe not), the optimization fails.
I see several possibilities:
Optimization can be done, I just do not allow optimization, because I use the wrong function to compile or not use the correct flags, etc.
Optimization just doesn't work.
Others.
What is it?
Note. Please do not link conversations about "if you use the expression about deeds, you can do such-and-such" or "you can avoid this by performing blah blah." The point is simply to check which optimizations the erlang compiler does or does not do, and why or why not.
Thanks.