Peephole optimization: fix some places where an expression with side effects could be removed during optimization.

Without this fix, an expression of the form "0 * exp" would be reduced to simply "0" unless exp contained a function call; other side effects of exp (such as assignments or increments) would be removed.

A similar issue could occur with additions that use the same expression on both sides of the "+": after optimization, it would only be evaluated once. I think the cases addressed here are all undefined behavior under the C standards, so the old behavior wasn't technically wrong, but the new behavior is still less confusing.
This commit is contained in:
Stephen Heumann 2016-01-03 15:06:55 -06:00
parent bd19465ab2
commit dce39a851e

10
DAG.pas
View File

@ -716,7 +716,7 @@ case op^.opcode of {check for optimizations of this node}
end; {else if}
end {if}
else if CodesMatch(op^.left, op^.right, false) then begin
if NoFunctions(op^.left) then begin
if not SideEffects(op^.left) then begin
ZeroIntermediateCode(op^.right);
with op^.right^ do begin
opcode := pc_ldc;
@ -782,7 +782,7 @@ case op^.opcode of {check for optimizations of this node}
end; {else if}
end {if}
else if CodesMatch(op^.left, op^.right, false) then
if NoFunctions(op^.left) then begin
if not SideEffects(op^.left) then begin
ZeroIntermediateCode(op^.right);
with op^.right^ do begin
opcode := pc_ldc;
@ -1620,7 +1620,7 @@ case op^.opcode of {check for optimizations of this node}
if q = 1 then
opv := op^.left
else if q = 0 then begin
if NoFunctions(op^.left) then
if not SideEffects(op^.left) then
opv := op^.right;
end {else if}
else if (q = -1) and (op^.opcode = pc_mpi) then begin
@ -1652,7 +1652,7 @@ case op^.opcode of {check for optimizations of this node}
if lval = 1 then
opv := op^.left
else if lval = 0 then begin
if NoFunctions(op^.left) then
if not SideEffects(op^.left) then
opv := op^.right;
end {else if}
else if (lval = -1) and (op^.opcode = pc_mpl) then begin
@ -1680,7 +1680,7 @@ case op^.opcode of {check for optimizations of this node}
if rval = 1.0 then
opv := op^.left
else if rval = 0.0 then
if NoFunctions(op^.left) then
if not SideEffects(op^.left) then
opv := op^.right;
end; {if}
end; {else}