Traditionally, compilers optimize a program procedure by procedure, or at best compilation unit per compilation unit. Whole program optimization (WPO) means that the compiler considers all compilation units that make up a program or library and optimizes them using the combined knowledge of how they are used together in this particular case.
The way WPO generally works is as follows:
The program is compiled normally, with an option to tell the compiler that it should store various bits of information into a feedback file.
The program is recompiled a second time (and optionally all units that it uses) with WPO enabled, providing the feedback file generated in the first step as extra input to the compiler.
This is the scheme followed by Free Pascal.
The implementation of this scheme is highly compiler dependent. Another implementation could be that the compiler generates some kind of intermediary code (e.g., byte code) and the linker performs all wpo along with the translation to the target machine code