> Mark Mitchell writes: >> Kazu Hirata wrote: >> ... >> static const int array[] = { 0, 0, 0, 1, 1, 1, 2, 2, 2 }; >> >> int >> foo (int a) >> { >> return array[7]; >> } >> >> I am trying to fold array[7] into 2. It turns out that if T is an >> ARRAY_REF, >> >> TREE_READONLY (TREE_OPERAND (T, 0)) >> >> is 0. Why? This would be 1 if the program is fed into the C front >> end, which is needed to safely fold a constant array reference. > > That's a bug, or, rather, a place where the C++ front-end is failing to give > full information to the optimizer. It should be TREE_READONLY. There are some > tricky bits, in that if we're doing a dynamic initialization, we presently > cannot mark it TREE_READONLY, but this is a static initialization, so it > should be fine. Isn't TREE_OPERAND (T, 0) the VAR_DECL for array itself? If > so, waht's probably going wrong is that it's not being marked TREE_READONLY > because we're afraid of the dynamic initialization case. We're missing a call > to c_apply_quals_to_decl (sp?) somewhere.
- ??? no such thing, you can't "dynamically" initialize a "static const", as then it's not a "static const", but rather simply a global "const" (as a "static const" object is logically equivalent to a named/addressable literal, yes?)