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

git-svn-id: trunk@18271 -
This commit is contained in:
florian 2011-08-19 09:26:40 +00:00
parent 386585ab66
commit 4f6a803c29
4 changed files with 43 additions and 1 deletions

1
.gitattributes vendored
View File

@ -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

View File

@ -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;

View File

@ -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
tests/tbs/tb0579.pp Normal file
View File

@ -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.