Browse Source

+ optimizations (at -O2 level) for (where 'a' is an integer expression, without
side effects):
* a - a -> 0
* a xor a -> 0
* a and a -> a
* a or a -> a
* a <> a -> false
* a < a -> false
* a > a -> false
* a = a -> true
* a <= a -> true
* a >= a -> true

git-svn-id: trunk@36027 -

nickysn 8 years ago
parent
commit
5aeb73b940
1 changed files with 33 additions and 1 deletions
  1. 33 1
      compiler/nadd.pas

+ 33 - 1
compiler/nadd.pas

@@ -1012,7 +1012,39 @@ implementation
                           end;
                           end;
                     end;
                     end;
                   end
                   end
-               end;
+              end;
+
+            if is_integer(left.resultdef) and is_integer(right.resultdef) then
+              begin
+                if not might_have_sideeffects(left) and
+                  left.isequal(right) then
+                  begin
+                    case nodetype of
+                      andn,orn:
+                        begin
+                          result:=left;
+                          left:=nil;
+                          exit;
+                        end;
+                      xorn,
+                      subn,
+                      unequaln,
+                      ltn,
+                      gtn:
+                        begin
+                          result:=cordconstnode.create(0,resultdef,true);
+                          exit;
+                        end;
+                      equaln,
+                      lten,
+                      gten:
+                        begin
+                          result:=cordconstnode.create(0,resultdef,true);
+                          exit;
+                        end;
+                    end;
+                  end;
+              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 }