From f41025f5dd7d1b7fb4367f0aceac021a7a1f8478 Mon Sep 17 00:00:00 2001 From: florian Date: Wed, 6 Mar 2024 22:56:06 +0100 Subject: [PATCH] * optimize also -(x+1) and -(1+x) into not(x) --- compiler/nmat.pas | 14 ++++++++++++++ tests/tbs/tb0711.pp | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/compiler/nmat.pas b/compiler/nmat.pas index 39ba5c1424..69ce0548c0 100644 --- a/compiler/nmat.pas +++ b/compiler/nmat.pas @@ -1092,6 +1092,20 @@ implementation result:=tunarynode(left).left.getcopy; exit; end; + end + { transform -(x+1) or -(1+x) into not(x) } + else if is_integer(left.resultdef) and is_signed(left.resultdef) and (left.nodetype=addn) and ((localswitches*[cs_check_overflow,cs_check_range])=[]) then + begin + if is_constintnode(taddnode(left).right) and (tordconstnode(taddnode(left).right).value=1) then + begin + result:=cnotnode.create(taddnode(left).left.getcopy); + exit; + end + else if is_constintnode(taddnode(left).left) and (tordconstnode(taddnode(left).left).value=1) then + begin + result:=cnotnode.create(taddnode(left).right.getcopy); + exit; + end; end; end; diff --git a/tests/tbs/tb0711.pp b/tests/tbs/tb0711.pp index f9d8173bfa..4233d6480a 100644 --- a/tests/tbs/tb0711.pp +++ b/tests/tbs/tb0711.pp @@ -6,4 +6,10 @@ begin if -l-1<>-1235 then halt(1); + + if -(l+1)<>-1235 then + halt(2); + + if -(1+l)<>-1235 then + halt(2); end. \ No newline at end of file