From cc75a9b12be08b3c2ff848af7754230da15cc940 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Mon, 10 Oct 2016 20:02:21 -0500 Subject: [PATCH] Fix a problem where a conversion to a smaller type could be improperly omitted when followed by a conversion to a larger type. This also fixes a logic error that may have permitted other conversions to be improperly omitted in some cases. The following program demonstrates the problem (should print 211): #pragma optimize 1 #include int main(void) { unsigned int i = 1234; long l = (unsigned char)(i+1); printf("%li\n", l); } --- DAG.pas | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/DAG.pas b/DAG.pas index 5039884..3b505d2 100644 --- a/DAG.pas +++ b/DAG.pas @@ -1135,7 +1135,7 @@ case op^.opcode of {check for optimizations of this node} end {if} else if op^.left^.opcode = pc_cnv then begin doit := false; - firsttype.i := (op^.q & $00F0) >> 4; + firsttype.i := (op^.left^.q & $00F0) >> 4; if fromType.optype in [cgReal,cgDouble,cgComp,cgExtended] then begin if toType.optype in [cgReal,cgDouble,cgComp,cgExtended] then doit := true; @@ -1152,6 +1152,9 @@ case op^.opcode of {check for optimizations of this node} if TypeSize(firstType.optype) = TypeSize(fromType.optype) then if TypeSize(firstType.optype) = TypeSize(toType.optype) then doit := true; + if TypeSize(fromType.optype) < TypeSize(firstType.optype) then + if TypeSize(fromType.optype) < TypeSize(toType.optype) then + doit := false; {disable optimization in invalid cases} end; {else} if doit then begin op^.q := (op^.left^.q & $00F0) | (op^.q & $000F);