[fpc-devel] tNode.isequal optimization

Dariusz Mazur darekm at emadar.com
Thu Jan 29 16:36:07 CET 2009


Hi
I've read some code of compiler (try to understand how it work)
and I notice, that tNode.isequal is invoked very often but has not 
optimal (by me) implementation


my proposition is (one assigned less):


   function tnode.isequal(p : tnode) : boolean;
     begin
        if assigned(self) then   begin
            result:=(assigned(p) and
                (p.classtype=classtype) and
                (p.nodetype=nodetype) and
                (flags*flagsequal=p.flags*flagsequal) and
                docompare(p));
        end  else
          result:=not assigned(p);
     end;
(*    *original*
        isequal:=   (not assigned(self) and not assigned(p)) or
          (assigned(self) and assigned(p) and
           { optimized subclasses have the same nodetype as their        }
           { superclass (for compatibility), so also check the classtype 
(JM) }
           (p.classtype=classtype) and
           (p.nodetype=nodetype) and
           (flags*flagsequal=p.flags*flagsequal) and
           docompare(p));

*)



and second:

make isequal inlined,  then in function

   function tunarynode.docompare(p : tnode) : boolean;
     begin
        docompare:=(inherited docompare(p) and
          (not assigned(left) or left.isequal(tunarynode(p).left))
        );
     end;

compiler can fold two equal node  :     not assigned(left) or 
((assigned(p) and
           { optimized subclasses have the same nodetype as their        }
           { superclass (for compatibility), so also check the classtype 
(JM) }
           (p.classtype=classtype) and
           (p.nodetype=nodetype) and
           (flags*flagsequal=p.flags*flagsequal) and
           docompare(p));



Will those optimization give better code?
Can compiler remove unnecessary nodes in boolean operation after inline 
unfolding?

-- 

 Darek





More information about the fpc-devel mailing list