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 */
}
This commit is contained in:
Stephen Heumann 2021-02-11 14:50:36 -06:00
parent 25697b1fba
commit 8078675aae

24
DAG.pas
View File

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