Forum > FPC development
[SOLVED] Poor optimization of constant folding
lagprogramming:
According to FPC's Programmer's guide:
--- Quote ---11.1.1
Constant folding
In Free Pascal, if the operand(s) of an operator are constants, they will be evaluated at compile time.
Example
x:=1+2+3+6+5;
will generate the same code as
x:=17;
--- End quote ---
So, I thought arithmetic operations of constants are computed at compile time.
The following program should be self explanatory.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program ConstantFoldingOptimization;{$OPTIMIZATION LEVEL3}function Foo(a:integer):integer;begin result:=-a*2; {negl %edi leal (%edi,%edi,1),%eax}end; function Bar(a:integer):integer;begin result:=a*(-2); {movl %edi,%eax imull $-2,%eax}end; function Baz(a:integer):integer;begin result:=a*(-2)*(-1)*(-1)*(-1)*(-1)*(3); {movl %edi,%eax imull $-2,%eax negl %eax negl %eax negl %eax negl %eax}end; function Wow(a:integer):integer;begin result:=a*(-2)*(-1)*(-1)*(-1)*(-1)*(3); {movl %edi,%eax imull $-2,%eax negl %eax negl %eax negl %eax negl %eax leal (%eax,%eax,2),%eax}end; var z:integer;begin z:=paramcount; writeln(Foo(z)); writeln(Bar(z)); writeln(Baz(z)); writeln(Wow(z));end.If constant folding optimization has been disabled temporarily, I have to say that it takes quite some time to enable it again.
Maybe this optimization is not disabled, maybe it's just incomplete. :-\
Fibonacci:
Its computed at compile time only if whole expression is constant
--- Code: ASM [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---# [5] result:=a*(-2)*(-1)*(-1)*(-1)*(-1)*(3); imull $-2,%eax negl %eax negl %eax negl %eax negl %eax leal (%eax,%eax,2),%eax
--- Code: ASM [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---# [5] result := (-1)*(-1)*(-1)*(-1)*(3); movl $3,%edx# [6] result := a*(-2)*result; imull $-2,%eax imull %edx,%eax
runewalsh:
Reported here. Until the fix in mid-2030s, you can force the folding with parentheses:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---result:=a*((-2)*(-1)*(-1)*(-1)*(-1)*(3));
Fibonacci:
--- Quote from: runewalsh on September 27, 2023, 11:42:56 am ---Until the fix in mid-2030s
--- End quote ---
:D
440bx:
--- Quote from: runewalsh on September 27, 2023, 11:42:56 am --- Until the fix in mid-2030s, you can force the folding with parentheses:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---result:=a*((-2)*(-1)*(-1)*(-1)*(-1)*(3));
--- End quote ---
or better yet, calculate the value of the constant along with a comment as to how it was calculated (if necessary for clarity)
Navigation
[0] Message Index
[#] Next page