4.6.6.8 Cacheconst Option
The -fcacheconst=[on|off|auto]
option enables
optimizations that can cache objects qualified const
in any unused data
memory, accessing them from this location rather than from program memory. This feature is
only available for licensed compilers.
Code that accesses objects in program memory is often slower and longer than that which accesses objects in data memory; however, lesser amounts of device data memory compared to program memory are present, and data memory is the only memory that can be used for writable objects. This optimization permits programs to use all the data memory required and then automatically utilize any unused portions of this data memory for read-only objects that would otherwise be placed in program memory.
If the option argument is specified as off
, the optimization is completely
disabled and no objects normally allocated to program memory will be cached in data
memory.
If the optimization is enabled (the default state when using optimization level
3
, or when using the on
argument at optimization
levels 3
or s
), the compiler will perform the usual
allocation of objects to data memory. If there are unused data memory locations after this
step, eligible objects that would have otherwise been stored in program memory will be
considered for storage in this free data memory. The compiler will cache objects to data
memory prioritized by a number a factors until no more objects can be allocated there. The
compiler will generate code to access these objects in data memory, which will typically
run faster than code that would have accessed them in program memory.
When the optimization is enabled in auto mode (the default state when using optimization
level s
, or when using the auto
argument at optimization
levels 3
or s
), the preliminary steps performed by the
compiler are the same as those performed when the on
argument to this
option has been specified; however, caching of each eligible object will only take place if
the compiler can determine that there will be no code size increase as a consequence. The
code to access an object in data memory will always be smaller than that which accesses
program memory; however, if an object is located in data memory, its assigned value must be
copied there by the runtime startup code (see 5.10.2.1 Initialization Of Objects). In most instances,
the increase in size of the startup code to initialize the objects in data memory will be
far less than the reduction in code size realized for each access of the object, but where
that is not the case, the compiler will not redirect the object to data memory when this
form of the option is used.
If the -fcacheconst
option is not specified, the optimization will be
disabled when selecting any of optimization levels 0
, 1
,
or 2
(as if the -fcacheconst=off
was specified); the
optimization will be enabled when selecting optimization level 3
(as if
-fcacheconst=on
was specified); and the optimization will be set to its
auto mode when using optimization level s
(as if
-fcacheconst=auto
was specified). When using an optimization level of 2
or lower, the optimization cannot be enabled and use of the -fcacheconst
option will be ignored.
As programs that use either a software stack (for any functions compiled to use a reentrant
stack model) or a heap (used by dynamic memory allocation functions) typically require as
much data memory as possible for these constructs, this optimization is disabled by default
when these constructs are used, regardless of the optimization level. You can force the
optimization to be applied (optimization level permitting) by using the
-fcacheconst
option in these cases. The optimization is disabled if
local optimizations (-flocal
option) have been enabled, but in this case,
the optimization cannot be enabled using the -fcacheconst
option.
Any object that is absolute (defined using __at()
) or any object in a
section (defined using __section()
) or any object marked as
volatile
will never be cached; however, you should not make assumptions
as to which other objects will be cached by this optimization, nor where cached objects
will be located. As a program is developed, the objects cached and where they are placed
can vary from build to build. Ensure that this optimization is disabled if you access
C-defined program memory objects from assembly code.
As this optimization attempts to have programs utilize all of the device's data memory,
information is provided in the map to indicate how much data memory is purely being used
for the caching of const
-qualified objects. It is important to realize
that although it might appear that data memory is full or almost full when this
optimization is enabled, you might still be able to define additional objects in your
program. When you next build, the compiler will attempt to allocate these objects to data
memory in the usual way, and if successful, there will be fewer
const
-qualified objects cached.