[fpc-devel] constant propagation // kinda
Martin Frb
lazarus at mfriebe.de
Wed Oct 1 22:37:26 CEST 2025
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.
More information about the fpc-devel
mailing list