Is it possible to make contents of constant array be visible across multiple other source files so that compiler can optimize access to the array.
I have an array const int myTable[10];
and in multiple source files I want contents of this table to be visible to other source files so that access can be optimized. For example the following code:
if (myTable[1] == 1)
{
foo();
}
else if (myTable[2] == 5)
{
bar();
}
else
{
baz();
}
could have branch entirely optimized-out if source file is aware of contents of myTable
.
I am aware of couple solutions but none are satisfactory:
- use LTO (on GCC) - on embedded device without support for LTO
- define
myTable
in header file and then include it in source files - I want only single definition of this table because it is large
EDIT:
Additional constraints to make the question clearer:
- Array must be a global variable - must be visible to MULTIPLE files
- Index to table is not an integer but an enumeral and is not always a compile-time constant
#define myTable_1 10
,#define myTable_2 5
etc. And then your code would test these constants.static
in a translation unit that also provides all the functions that access it to make the problem not appear arbitrarily self-imposed.