[fpc-devel] Constant eval (and dead code removal) - comparing 2 methods for being the same.

Florian Klämpfl florian at freepascal.org
Sat Oct 4 12:11:25 CEST 2025



> Am 01.10.2025 um 12:46 schrieb Martin Frb via fpc-devel <fpc-devel at lists.freepascal.org>:
> 
> In the code
> 
>   if true then begin
>     write(1);
>   end
>   else begin
>     write(2);
>   end;
> 
> the "else" block will not generate code (at least O2 and higher).
> 
> But
> 
>   TFoo = class
>     procedure MyFoo;
>   end;
>   X = class(TFoo)
>   end;
> 
> 
>   //if @X.MyFoo = @TFoo.MyFoo then begin
>   if @TFoo.MyFoo = @TFoo.MyFoo then begin
>     write(1);
>   end
>   else begin
>     write(2);
>   end;
> 
> Does not detect this at compile time.
> 
> Note, that this is not on an instance, this is on the actual class => so it is known.
> 
> Is there any way to get that optimize the "else" block away (or the "then" if the compare goes the other way)?
> I can't just compare the classes, as subclasses can have the same method (if it isn't overridden/reintroduced)

I added an optimization for this in dbf7ba4721f8336139e84a99c74b89025edc55c3. Can you please check if it works for your case?



More information about the fpc-devel mailing list