Fix mis-evaluation of certain equality comparisons with intermediate code optimization.

This affected comparisons of the form "logical operation or comparison == constant other than 0 or 1". These should always evaluate to 0 (false), but could mis-evaluate to true due to the bad optimization.

The following program gives an example showing the problem:

#pragma optimize 1
int main(void) {
        int i = 0, j = 42;
        return (i || j) == 123;
}
This commit is contained in:
Stephen Heumann 2018-03-26 18:20:36 -05:00
parent 7266b1d613
commit db98f7842d
1 changed files with 2 additions and 2 deletions

View File

@ -1329,7 +1329,7 @@ case op^.opcode of {check for optimizations of this node}
end; {case}
end {if}
else if op^.right^.optype in [cgByte, cgUByte, cgWord, cgUWord] then begin
if op^.right^.q <> 0 then
if op^.right^.q = 1 then
if op^.left^.opcode in
[pc_and,pc_ior,pc_neq,pc_equ,pc_geq,pc_leq,pc_les,pc_grt]
then begin
@ -1338,7 +1338,7 @@ case op^.opcode of {check for optimizations of this node}
end; {if}
end {else if}
else if op^.right^.optype in [cgLong, cgULong] then begin
if op^.right^.lval <> 0 then
if op^.right^.lval = 1 then
if op^.left^.opcode in
[pc_and,pc_ior,pc_neq,pc_equ,pc_geq,pc_leq,pc_les,pc_grt]
then begin