[cfe-dev] [llvm-dev] [RFC] Introducing a byte type to LLVM

Harald van Dijk via cfe-dev cfe-dev at lists.llvm.org
Sun Jun 6 15:19:03 PDT 2021


On 06/06/2021 20:29, James Courtier-Dutton via llvm-dev wrote:
> See inline
> 
> On Sun, 6 Jun 2021 at 20:15, Nuno Lopes via cfe-dev
> <cfe-dev at lists.llvm.org> wrote:
>> Now consider that p/q are pointers. In particular they are the following:
>> char a[n];
>> char q[n];
>> char *p = a + n;
>>
>> inlining the definitions, originally we had:
>> if (a+n == q) {     <-  This is the problem. This being true or false is undefined. One compiler might make them equal, another will not.
>>    *(a+n-1) = 0;
>> }

That is not the problem. It is true that whether a+n and q compare equal 
is unspecified, but that is irrelevant. *If* they compare equal, then 
the if block must execute as written, else it must not. The only valid 
options are: either a[n-1] gets set to zero, or a[n-1] is left 
unmodified. Any transformation to undefined IR is invalid.

You can change the example to something that has fully defined 
behaviour, even, simply by:

   if (a+n == q) {
     *(a+n-1) = 0;
   } else {
     *(a+n-1) = 0;
   }

If LLVM fails to notice that the two branches are equivalent (allowing 
the branch to be eliminated entirely), you still have the exact same 
problem with the first branch potentially getting mis-optimised.

Cheers,
Harald van Dijk


More information about the cfe-dev mailing list