Browse Source

* optimize also -(x+1) and -(1+x) into not(x)

florian 1 year ago
parent
commit
f41025f5dd
2 changed files with 20 additions and 0 deletions
  1. 14 0
      compiler/nmat.pas
  2. 6 0
      tests/tbs/tb0711.pp

+ 14 - 0
compiler/nmat.pas

@@ -1092,6 +1092,20 @@ implementation
                 result:=tunarynode(left).left.getcopy;
                 result:=tunarynode(left).left.getcopy;
                 exit;
                 exit;
               end;
               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;
       end;
       end;
 
 

+ 6 - 0
tests/tbs/tb0711.pp

@@ -6,4 +6,10 @@ begin
 
 
   if -l-1<>-1235 then
   if -l-1<>-1235 then
     halt(1);
     halt(1);
+
+  if -(l+1)<>-1235 then
+    halt(2);
+
+  if -(1+l)<>-1235 then
+    halt(2);
 end.
 end.