[llvm-commits] [llvm] r133285 - /llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp

Duncan Sands baldrick at free.fr
Mon Jun 20 00:29:30 PDT 2011


Hi John,

> On Jun 19, 2011, at 5:58 AM, Duncan Sands wrote:
>>> +  // Insist that the amount-to-allocate not overflow.
>>> +  OverflowingBinaryOperator *OBI = dyn_cast<OverflowingBinaryOperator>(Val);
>>> +  if (OBI&&   !OBI->hasNoUnsignedWrap()) return 0;
>>
>> I noticed you dropped the test for NSW.  I thought this was a problem because
>> front-ends generally don't ever produce NUW, though they may produce NSW...
>
> Stuart asked me to look at this, and I gave him a quick answer
> that it's generally correct for nuw arithmetic, but that I wasn't as
> certain for nsw arithmetic.  Now I think it's probably fine for either
> as long as it's homogenous throughout.
>
> The main restriction on the alloca size argument can be expressed as:
>    (N * sizeof(T)) udiv sizeof(T) == N
> However, there's a second restriction, which is that it's undefined
> behavior to allocate something too large for the stack.  Technically
> this is target-specific, but if we just assume by fiat that 2^31 is too large,
> then as long as the arithmetic is being done in some reasonable size
> then we know that N * sizeof(T) isn't permitted to signed overflow, either.

if you are willing to assume that then you don't need to check for nuw either...
I think I saw some GCC testcases fly by in which people were allocating stack
objects larger than 2^32, so I'm not sure that "N * sizeof(T) < 2^31" is a
reasonable assumption...

About nsw: if you assume than N and sizeof(T) are both less than 2^31, then they
are non-negative as signed numbers.  If you have the nsw flag then you know that
(N * sizeof(T)) is non-negative as a signed number.  This implies that there was
no unsigned wrap either, so all is OK.  I'm willing to assume that sizeof(T) is
less than 2^31, but I'm not sure about N.

Ciao, Duncan.



More information about the llvm-commits mailing list