Răsfoiți Sursa

* first part of fixing #38267: do not bail out early during constant folding
if the constant is 1 or -1

git-svn-id: trunk@47933 -

florian 4 ani în urmă
părinte
comite
97abf6b495
3 a modificat fișierele cu 53 adăugiri și 20 ștergeri
  1. 1 0
      .gitattributes
  2. 9 20
      compiler/nadd.pas
  3. 43 0
      tests/webtbs/tw38267a.pp

+ 1 - 0
.gitattributes

@@ -18620,6 +18620,7 @@ tests/webtbs/tw38225.pp svneol=native#text/pascal
 tests/webtbs/tw38238.pp svneol=native#text/pascal
 tests/webtbs/tw38249.pp svneol=native#text/pascal
 tests/webtbs/tw38259.pp svneol=native#text/pascal
+tests/webtbs/tw38267a.pp svneol=native#text/pascal
 tests/webtbs/tw3827.pp svneol=native#text/plain
 tests/webtbs/tw3829.pp svneol=native#text/plain
 tests/webtbs/tw3833.pp svneol=native#text/plain

+ 9 - 20
compiler/nadd.pas

@@ -708,9 +708,9 @@ implementation
           end;
 
         { Add,Sub,Mul,Or,Xor,Andn with constant 0, 1 or -1?  }
-        if is_constintnode(right) and (is_integer(left.resultdef) or is_pointer(left.resultdef)) then
+       if is_constintnode(right) and (is_integer(left.resultdef) or is_pointer(left.resultdef)) then
           begin
-            if tordconstnode(right).value = 0 then
+            if (tordconstnode(right).value = 0) and (nodetype in [addn,subn,orn,xorn,andn,muln]) then
               begin
                 case nodetype of
                   addn,subn,orn,xorn:
@@ -725,24 +725,13 @@ implementation
                     ;
                 end;
               end
-            else if tordconstnode(right).value = 1 then
-              begin
-                case nodetype of
-                  muln:
-                   result := left.getcopy;
-                  else
-                    ;
-                end;
-              end
-            else if tordconstnode(right).value = -1 then
-              begin
-                case nodetype of
-                  muln:
-                   result := ctypeconvnode.create_internal(cunaryminusnode.create(left.getcopy),left.resultdef);
-                  else
-                    ;
-                end;
-              end
+
+            else if (tordconstnode(right).value = 1) and (nodetype=muln) then
+              result := left.getcopy
+
+            else if (tordconstnode(right).value = -1) and (nodetype=muln) then
+              result := ctypeconvnode.create_internal(cunaryminusnode.create(left.getcopy),left.resultdef)
+
             { try to fold
                           op                         op
                          /  \                       /  \

+ 43 - 0
tests/webtbs/tw38267a.pp

@@ -0,0 +1,43 @@
+{ %OPT=-O3 }
+{$goto on}
+label start0, end0, start1, end1;
+
+var
+	x: int16;
+
+begin
+	x := random(2);
+	writeln('x := ', x);
+	writeln;
+
+start0:
+	x :=
+		1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+
+		1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+
+		1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+
+		1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+x;
+end0:
+	writeln('x := 1 + 1 + ...100 times ... + x, x = ', x, ': ');
+    writeln(SizeUint(CodePointer(@end0) - CodePointer(@start0)), ' b of code');
+    { hundred is actually arbitrarily chosen but should be sufficient for all targets
+      to show that constant folding works }
+    if SizeUint(CodePointer(@end0) - CodePointer(@start0))>100 then
+       halt(1);
+	writeln;
+
+start1:
+	x := x+
+		1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+
+		1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+
+		1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+
+		1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1;
+end1:
+	writeln('x := x + 1 + 1 + ...100 times ..., x = ', x, ': ');
+    { hundred is actually arbitrarily chosen but should be sufficient for all targets
+      to show that constant folding works }
+    writeln(SizeUint(CodePointer(@end1) - CodePointer(@start1)), ' b of code');
+    if SizeUint(CodePointer(@end1) - CodePointer(@start1))>100 then
+     halt(2);
+   writeln('ok');
+end.
+