[llvm-commits] [llvm] r160317 - in /llvm/trunk: lib/Support/ConstantRange.cpp unittests/Support/ConstantRangeTest.cpp

Nuno Lopes nunoplopes at sapo.pt
Tue Jul 17 09:01:04 PDT 2012


The multiplication algorithm seems fine to me.
The problem is in truncate, which is far from being optimal. After  
fixing truncate, both this special case and the full set case can go  
away. I'll take a look at truncate.

Nuno

P.S.: I built a little program to test ConstantRange for correctness  
and optimality. So far, no correctness problems have been found, but  
you would be amazed by the huge amount of optimization opportunities  
there are.  There are a few functions that are already optimal, though  
:)


Quoting Duncan Sands <baldrick at free.fr>:

> Hi Nuno,
>
>> teach ConstantRange that zero times X is always zero
>
> while this patch is OK, the multiplication algorithm could clearly  
> be much much
> better in general.  It's astonishing (to me) that it didn't already  
> handle the
> case when one operand has only a single value (and not just when that single
> value is zero).  It would be great if you could enhance it to at least handle
> that case perfectly.
>
> Ciao, Duncan.
>
>> --- llvm/trunk/lib/Support/ConstantRange.cpp (original)
>> +++ llvm/trunk/lib/Support/ConstantRange.cpp Mon Jul 16 15:47:16 2012
>> @@ -537,6 +537,12 @@
>>
>>     if (isEmptySet() || Other.isEmptySet())
>>       return ConstantRange(getBitWidth(), /*isFullSet=*/false);
>> +
>> +  // If any of the operands is zero, then the result is also zero.
>> +  if ((getSingleElement() && *getSingleElement() == 0) ||
>> +      (Other.getSingleElement() && *Other.getSingleElement() == 0))
>> +    return ConstantRange(APInt(getBitWidth(), 0));
>> +
>>     if (isFullSet() || Other.isFullSet())
>>       return ConstantRange(getBitWidth(), /*isFullSet=*/true);
>>
>>
>> Modified: llvm/trunk/unittests/Support/ConstantRangeTest.cpp
>> URL:  
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ConstantRangeTest.cpp?rev=160317&r1=160316&r2=160317&view=diff
>> ==============================================================================
>> --- llvm/trunk/unittests/Support/ConstantRangeTest.cpp (original)
>> +++ llvm/trunk/unittests/Support/ConstantRangeTest.cpp Mon Jul 16  
>> 15:47:16 2012
>> @@ -382,6 +382,14 @@
>>     EXPECT_EQ(Some.multiply(Wrap), Full);
>>     EXPECT_EQ(Wrap.multiply(Wrap), Full);
>>
>> +  ConstantRange Zero(APInt(16, 0));
>> +  EXPECT_EQ(Zero.multiply(Full), Zero);
>> +  EXPECT_EQ(Zero.multiply(Some), Zero);
>> +  EXPECT_EQ(Zero.multiply(Wrap), Zero);
>> +  EXPECT_EQ(Full.multiply(Zero), Zero);
>> +  EXPECT_EQ(Some.multiply(Zero), Zero);
>> +  EXPECT_EQ(Wrap.multiply(Zero), Zero);
>> +
>>     // http://llvm.org/PR4545
>>     EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
>>                   ConstantRange(APInt(4, 6), APInt(4, 2))),
>>



More information about the llvm-commits mailing list