Menu

#451 compile-time optimization: recognize variables as constants

unclassified
open
nobody
6
2023-10-17
2023-10-17
No

If a variable in WORKING-STORAGE/ LOCAL-STORAGE has a VALUE clause and is only used as source, then handle this as a constant (= allow the compiler to use that like a level 78 constant).

This is especially (but not only) useful for numeric values (which then can be adjusted for best type i the resulting module) for example in boundary checks, but also for "makers" and for the existing branch false/true optimization.

77  c-type-alph  VALUE 'a'.
77  c-type-num   VALUE 'n'.
01  max-idx      PIC 9(05) VALUE 99.

IF max-idx > LENGTH OF other-item  *> we can optimize that away
   PERFORM STUFF
END-IF

IF current-idx-as-pic99 > max-idx  *> we now can warn as we would if 99 is written
    PERFORM OTHER-STUFF.

To do this the internal xref collecting code would be enabled as this includes "written by parent" entries and works fine.

The main question (not checked) is if that's already too late when we have that information (in which case it would be good to try moving that up-front)... as in this case we'd need to implement a multi-step optimization of the internal AST.

Discussion


Log in to post a comment.