Browse Source

* optimize x </>= length(...) also if the operands are swapped

florian 1 year ago
parent
commit
2f9ed0576e
2 changed files with 33 additions and 1 deletions
  1. 25 0
      compiler/nadd.pas
  2. 8 1
      tests/webtbs/tw40292.pp

+ 25 - 0
compiler/nadd.pas

@@ -1764,6 +1764,31 @@ implementation
                 exit;
                 exit;
               end;
               end;
 
 
+            {
+              compile length(arr) > x as high(arr) >= x
+              compile length(arr) <= x as high(arr) < x
+
+              tested by tests/webtbs/tw40292.pp
+            }
+            if (nodetype in [lten,gtn]) and
+              (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_length_x) and
+              ((is_dynamic_array(tinlinenode(left).left.resultdef)) or
+               (is_open_array(tinlinenode(left).left.resultdef))
+              ) then
+              begin
+                case nodetype of
+                  gtn:
+                    result:=caddnode.create(gten,cinlinenode.create(in_high_x,false,tinlinenode(left).left),right);
+                  lten:
+                    result:=caddnode.create(ltn,cinlinenode.create(in_high_x,false,tinlinenode(left).left),right);
+                  else
+                    Internalerror(2024041701);
+                end;
+                right:=nil;
+                tinlinenode(left).left:=nil;
+                exit;
+              end;
+
             { using sqr(x) for reals instead of x*x might reduces register pressure and/or
             { using sqr(x) for reals instead of x*x might reduces register pressure and/or
               memory accesses while sqr(<real>) has no drawback }
               memory accesses while sqr(<real>) has no drawback }
             if
             if

+ 8 - 1
tests/webtbs/tw40292.pp

@@ -6,7 +6,14 @@ begin
   setlength(arr,13+random(0));
   setlength(arr,13+random(0));
   if (x < length(arr)) <> (x <= high(arr)) then
   if (x < length(arr)) <> (x <= high(arr)) then
     halt(1);
     halt(1);
+
+  if (length(arr) > x) <> (x <= high(arr)) then
+    halt(2);
+
   x:=13+random(0);
   x:=13+random(0);
   if (x >= length(arr)) <> (x > high(arr)) then
   if (x >= length(arr)) <> (x > high(arr)) then
-    halt(2);
+    halt(3);
+
+  if (length(arr) <= x) <> (x > high(arr)) then
+    halt(4);
 end.
 end.