This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: why are we not using const?


On 6/29/06, Kaveh R. Ghazi <ghazi@caipclassic.rutgers.edu> wrote:
 > Notice that the value of the parameter "b" is never changed in the
 > function body.  Consequently, if the current optimizers cannot figure
 > that simple cases out (where "b" is not annotated const), then the
 > optimizers in deficient in that respect.  That is the point.
 > -- Gaby

I agree that the compiler should/could be better at optimizing.

However my feeling is that 'const' is more important as documentation
and enforcement of APIs rather than an optimization hint.  From this
perspective, what "b" points to not getting changed is more important
to the caller than whether the function body changes the value of "b"
itself, since the caller doesn't see the latter.

I'd like to do for tree and rtx what I did for const char *, namely
constify those tree/rtx functions that aren't supposed to modify their
arguments.  This would require introducing the const_tree and
const_rtx typedefs Tristan suggested.

But with C language constructs you cannot assume that an object passed to a function via a const pointer is not modified. So, there is no real "const" regarding to objects pointed to. Consider

void foo(const int *i)
{
 int *k = (int *)i;
 *k = 0;
}
int bar(void)
{
 int i = 1;
 foo(&i);
 return i;
}

should return 0, not 1.

Richard.


Something for stage1, obviously.


                --Kaveh
--
Kaveh R. Ghazi                  ghazi@caip.rutgers.edu



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]