From dce39a851ebb889695b7e2b5b7ca9d271a531b74 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sun, 3 Jan 2016 15:06:55 -0600 Subject: [PATCH] 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. --- DAG.pas | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/DAG.pas b/DAG.pas index a25005f..e3eeac5 100644 --- a/DAG.pas +++ b/DAG.pas @@ -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}