From 8078675aae7bab023f875310ff92b59a744a5706 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Thu, 11 Feb 2021 14:50:36 -0600 Subject: [PATCH] Do not eliminate expressions with side effects in "exp | -1" or "exp & 0". This was previously happening in intermediate code peephole optimization. The following example program demonstrates the problem: #pragma optimize 1 int main(void) { int i = 0; long j = 0; ++i | -1; ++i & 0; ++j | -1; ++j & 0; return i+j; /* should be 4 */ } --- DAG.pas | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/DAG.pas b/DAG.pas index a6a3be5..51f8a88 100644 --- a/DAG.pas +++ b/DAG.pas @@ -896,8 +896,10 @@ case op^.opcode of {check for optimizations of this node} opv := op^.left; end {if} else if op^.right^.opcode = pc_ldc then begin - if op^.right^.lval = 0 then - opv := op^.right + if op^.right^.lval = 0 then begin + if not SideEffects(op^.left) then + opv := op^.right; + end {if} else if op^.right^.lval = -1 then opv := op^.left; end; {else if} @@ -911,8 +913,10 @@ case op^.opcode of {check for optimizations of this node} opv := op^.left; end {if} else if op^.right^.opcode = pc_ldc then begin - if op^.right^.lval = -1 then - opv := op^.right + if op^.right^.lval = -1 then begin + if not SideEffects(op^.left) then + opv := op^.right; + end {if} else if op^.right^.lval = 0 then opv := op^.left; end; {else if} @@ -943,8 +947,10 @@ case op^.opcode of {check for optimizations of this node} opv := op^.left; end {if} else if op^.right^.opcode = pc_ldc then begin - if op^.right^.q = 0 then - opv := op^.right + if op^.right^.q = 0 then begin + if not SideEffects(op^.left) then + opv := op^.right; + end {if} else if op^.right^.q = -1 then opv := op^.left; end; {else if} @@ -981,8 +987,10 @@ case op^.opcode of {check for optimizations of this node} opv := op^.left; end {if} else if op^.right^.opcode = pc_ldc then begin - if op^.right^.q = -1 then - opv := op^.right + if op^.right^.q = -1 then begin + if not SideEffects(op^.left) then + opv := op^.right; + end {if} else if op^.right^.q = 0 then opv := op^.left; end; {else if}