[LLVMdev] Can we assume const parameter and to-be-modified parameter do not alias?

Smith, Kevin B kevin.b.smith at intel.com
Fri Jan 9 15:09:23 PST 2015


I don't think this statement is correct:

> At the first glance, they can not be alias but  it seems that we can not figure out the alias info between them now which prevent us from doing some optimizations.

As an example, take memcpy, which from the C99 standard is defined as:

void * memmove(void * s1, const void *s2, size_t n);

It is perfectly legal to pass to memmove parameters such that an access through s1 can overlap an access through s2.  That is why the standard goes to the
trouble of specifying the semantics when the two areas can overlap.

It is perfectly legal to do make a call to memmove such as:

t = memmove(&a[0], &a[2], 200);

But, the advantage of the const specifier is that the caller can tell that memmove doesn't modify the object pointed to by the second parameter, provided that the
call cannot modify that through the non-const first parameter.

Kevin Smith


-----Original Message-----
From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of yi_jiang
Sent: Friday, January 09, 2015 2:22 PM
To: llvmdev at cs.uiuc.edu
Subject: [LLVMdev] Can we assume const parameter and to-be-modified parameter do not alias?

Hi, 

This question is raised from the following example:
 
int foo (const int a[], int b[])
{
     ...
     b[i] = ...
     ...
}

Basically a[] is defined as read-only while b[] will be explicitly modified in the function.  At the first glance, they can not be alias but  it seems that we can not figure out the alias info between them now which prevent us from doing some optimizations.   Here is the quote from:

c99 rationale, v5.10, p. 77

const is specified in such a way that an implementation is at liberty to put const objects in read-only storage, and is encouraged to diagnose obvious attempts to modify them, but is not required to track down all the subtle ways that such checking can be subverted.

Our understanding is that it is user's responsibility to guarantee that const array will not be touched in any way and if not the behavior is undefined and in this case it is safe to assume that they are not alias? 
_______________________________________________
LLVM Developers mailing list
LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list