mirror of
https://github.com/byteworksinc/ORCA-C.git
synced 2025-01-18 03:29:49 +00:00
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:
parent
bd19465ab2
commit
dce39a851e
10
DAG.pas
10
DAG.pas
@ -716,7 +716,7 @@ case op^.opcode of {check for optimizations of this node}
|
|||||||
end; {else if}
|
end; {else if}
|
||||||
end {if}
|
end {if}
|
||||||
else if CodesMatch(op^.left, op^.right, false) then begin
|
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);
|
ZeroIntermediateCode(op^.right);
|
||||||
with op^.right^ do begin
|
with op^.right^ do begin
|
||||||
opcode := pc_ldc;
|
opcode := pc_ldc;
|
||||||
@ -782,7 +782,7 @@ case op^.opcode of {check for optimizations of this node}
|
|||||||
end; {else if}
|
end; {else if}
|
||||||
end {if}
|
end {if}
|
||||||
else if CodesMatch(op^.left, op^.right, false) then
|
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);
|
ZeroIntermediateCode(op^.right);
|
||||||
with op^.right^ do begin
|
with op^.right^ do begin
|
||||||
opcode := pc_ldc;
|
opcode := pc_ldc;
|
||||||
@ -1620,7 +1620,7 @@ case op^.opcode of {check for optimizations of this node}
|
|||||||
if q = 1 then
|
if q = 1 then
|
||||||
opv := op^.left
|
opv := op^.left
|
||||||
else if q = 0 then begin
|
else if q = 0 then begin
|
||||||
if NoFunctions(op^.left) then
|
if not SideEffects(op^.left) then
|
||||||
opv := op^.right;
|
opv := op^.right;
|
||||||
end {else if}
|
end {else if}
|
||||||
else if (q = -1) and (op^.opcode = pc_mpi) then begin
|
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
|
if lval = 1 then
|
||||||
opv := op^.left
|
opv := op^.left
|
||||||
else if lval = 0 then begin
|
else if lval = 0 then begin
|
||||||
if NoFunctions(op^.left) then
|
if not SideEffects(op^.left) then
|
||||||
opv := op^.right;
|
opv := op^.right;
|
||||||
end {else if}
|
end {else if}
|
||||||
else if (lval = -1) and (op^.opcode = pc_mpl) then begin
|
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
|
if rval = 1.0 then
|
||||||
opv := op^.left
|
opv := op^.left
|
||||||
else if rval = 0.0 then
|
else if rval = 0.0 then
|
||||||
if NoFunctions(op^.left) then
|
if not SideEffects(op^.left) then
|
||||||
opv := op^.right;
|
opv := op^.right;
|
||||||
end; {if}
|
end; {if}
|
||||||
end; {else}
|
end; {else}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user