Răsfoiți Sursa

+ might_have_sideeffects, make use of it when optimizing x*x into sqr(x)

git-svn-id: trunk@18271 -
florian 14 ani în urmă
părinte
comite
4f6a803c29
4 a modificat fișierele cu 43 adăugiri și 1 ștergeri
  1. 1 0
      .gitattributes
  2. 2 1
      compiler/nadd.pas
  3. 22 0
      compiler/nutils.pas
  4. 18 0
      tests/tbs/tb0579.pp

+ 1 - 0
.gitattributes

@@ -9139,6 +9139,7 @@ tests/tbs/tb0576.pp svneol=native#text/plain
 tests/tbs/tb0577.pp svneol=native#text/plain
 tests/tbs/tb0577a.pp svneol=native#text/plain
 tests/tbs/tb0578.pp svneol=native#text/pascal
+tests/tbs/tb0579.pp svneol=native#text/pascal
 tests/tbs/tb205.pp svneol=native#text/plain
 tests/tbs/ub0060.pp svneol=native#text/plain
 tests/tbs/ub0069.pp svneol=native#text/plain

+ 2 - 1
compiler/nadd.pas

@@ -784,7 +784,8 @@ implementation
               memory accesses while sqr(<real>) has no drawback }
             if (nodetype=muln) and
                is_real(left.resultdef) and is_real(right.resultdef) and
-               left.isequal(right) then
+               left.isequal(right) and
+               not(might_have_sideeffects(left)) then
               begin
                 result:=cinlinenode.create(in_sqr_real,false,left);
                 left:=nil;

+ 22 - 0
compiler/nutils.pas

@@ -109,6 +109,8 @@ interface
       represented by n }
     function genloadfield(n: tnode; const fieldname: string): tnode;
 
+    { returns true, if the tree given might have side effects }
+    function might_have_sideeffects(n : tnode) : boolean;
 
 implementation
 
@@ -1213,4 +1215,24 @@ implementation
         end;
       end;
 
+
+    function check_for_sideeffect(var n: tnode; arg: pointer): foreachnoderesult;
+      begin
+        result:=fen_false;
+        if (n.nodetype in [assignn,calln,asmn]) or
+          ((n.nodetype=inlinen) and
+           (tinlinenode(n).inlinenumber in [in_write_x,in_writeln_x,in_read_x,in_readln_x,in_str_x_string,
+             in_val_x,in_reset_x,in_rewrite_x,in_reset_typedfile,in_rewrite_typedfile,in_settextbuf_file_x,
+             in_inc_x,in_dec_x,in_include_x_y,in_exclude_x_y,in_break,in_continue,in_setlength_x,
+             in_finalize_x,in_new_x,in_dispose_x,in_exit,in_copy_x,in_initialize_x,in_leave,in_cycle])
+          ) then
+          result:=fen_norecurse_true;
+      end;
+
+
+    function might_have_sideeffects(n : tnode) : boolean;
+      begin
+        result:=foreachnodestatic(n,@check_for_sideeffect,nil);
+      end;
+
 end.

+ 18 - 0
tests/tbs/tb0579.pp

@@ -0,0 +1,18 @@
+{ %opt=-O3 }
+var
+  i : longint;
+
+function f : real;
+  begin
+    inc(i);
+    f:=2;
+  end;
+
+begin
+  i:=0;
+  if f*f<>4 then
+    halt(1);
+  if i<>2 then
+    halt(1);
+  writeln('ok');
+end.