From 54aae023ea2f4ee864c4ef3ce98ad387c166cb52 Mon Sep 17 00:00:00 2001 From: "J. Gareth \"Curious Kit\" Moreton" Date: Mon, 2 May 2022 00:50:10 +0100 Subject: [PATCH] * Bug fix that prevents downsizing of "(x div y) and z" when "x div y" is 64-bit and z is 32-bit or less. Fixes i39646 --- compiler/ncnv.pas | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/compiler/ncnv.pas b/compiler/ncnv.pas index 5f5cb3f4cb..fef219df66 100644 --- a/compiler/ncnv.pas +++ b/compiler/ncnv.pas @@ -2969,8 +2969,15 @@ implementation (not(n.nodetype in [modn,divn]) or (not(gotminus1))) ) or - ((((n.nodetype=andn) and wasoriginallysmallerint(tbinarynode(n).left)) or - ((n.nodetype=andn) and wasoriginallysmallerint(tbinarynode(n).right)))); + ( + (n.nodetype=andn) and + ( + { Right node is more likely to be a constant, so check + this one first } + wasoriginallysmallerint(tbinarynode(n).right) or + wasoriginallysmallerint(tbinarynode(n).left) + ) + ); end; else result:=false; @@ -3001,9 +3008,13 @@ implementation dword1:=dword1+((dword2+dword3) shr 2); - while we can remove an extension on the addition, we cannot remove it from the shr + while we can remove an extension on the outermost addition, we cannot + remove it from the shr } - if (n.nodetype=shrn) and (level<>0) then + { Don't downsize into a division operation either, as the numerator can + be much larger than the result and non-linear properties prevent + accurate truncation; fixes #39646 [Kit] } + if (n.nodetype in [shrn,divn,modn]) and (level<>0) then begin inserttypeconv_internal(n,todef); exit;