Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2025-10

constant propagation // kinda

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin Frb

Just came arccos a case for optimisation. I was wondering if its worth a feature request.

Especially since I guess the compiler may already have most of the info needed.

var a: integer;
begin
   A := 42;
   // some code
   x := x + a;

In the entire procedure
- A is set before it is ever read
- A is set in an unconditional path
- A is set only/exactly once, and never changed.

So basically A is a constant.

Now given that the compiler can tell me
- if a variable is not used at all (gives a hint)
- if a variable is assigned but then never used (gives a hint) It would seem the compiler should have (nearly?) all knowledge to see that A is a constant?

Of course at first code like this makes little sense. Isn't expected to exist. Except, if that for example happens in a generic. And the value for A come from a generic param, e.g. a "record" passed in, that could have the value in a field, or alternatively have a declared const for the value.
Then the code written for the generic wouldn't know, until the compiler applies the param.

Reply by email