[LLVMdev] c const

Gordon Henriksen gordonhenriksen at mac.com
Thu Aug 16 06:47:13 PDT 2007


On Aug 16, 2007, at 06:05, Holger Schurig wrote:

>> if the programmer is going to tell you that the memory pointed to  
>> by a pointer argument is never written.
>
> There are ways to cast away a const.

Even if there weren't, a const value can change, just not through a  
const reference. Consider this code:

   int counter = 0;
   bool f(const int *ci) {
     int i = *ci;
     counter++;
     return *ci == i;
   }

Naively, it seems trivial to observe *ci is const and i == *ci,  
therefore i == *ci is always true:

   int counter = 0;
   bool f(const int *ci) {
     counter++;
     return true;
   }

However, that is a miscompilation since ci and &counter may alias:

   f(&counter); // must return false

So even in the absence of const_cast, most uses of const are not very  
useful for optimizers. As has been pointed out, alias analysis  
provides similar data that can be used for optimization.

— Gordon





More information about the llvm-dev mailing list