Fix a flawed optimization.

The optimization could turn an unsigned comparison "x <= 0xFFFF" into "x < 0".

Here is an example affected by this:

int main(void) {
        unsigned i = 1;
        return (i <= 0xffff);
}
This commit is contained in:
Stephen Heumann 2022-07-10 22:03:30 -05:00
parent 76e4b1f038
commit 7b0dda5a5e
2 changed files with 5 additions and 2 deletions

View File

@ -1850,8 +1850,9 @@ case op^.opcode of {check for optimizations of this node}
end; {if}
end; {case}
end {if}
else if (op^.opcode = pc_leq) and (op^.optype in [cgWord,cgUWord]) then
if op^.right^.q < maxint then begin
else if (op^.opcode = pc_leq) then
if ((op^.optype = cgWord) and (op^.right^.q <> maxint))
or ((op^.optype = cgUWord) and (op^.right^.q <> -1)) then begin
op^.right^.q := op^.right^.q + 1;
op^.opcode := pc_les;
end; {if}

View File

@ -1865,6 +1865,8 @@ int foo(int[42]);
201. When certain expressions were used for the condition in an if statement, loop, or conditional expression, the conditional branch might be evaluated incorrectly, leading to incorrect control flow. The affected condition expressions included shifts, loads or stores of bit-fields, and ? : expressions.
202. Some comparisons against the constant 0xFFFF would give the wrong result when intermediate code peephole optimization was used.
-- Bugs from C 2.1.0 that have been fixed -----------------------------------
1. In some situations, fread() reread the first 1K or so of the file.