[PATCH] [ConstantRange] Add a smultiply method for signed multiply

Sanjoy Das sanjoy at playingwithpointers.com
Sat Feb 21 11:22:25 PST 2015


> [-2,0) * [-4,4)
> Consider the trivial case: i8 [254,0) * i8 [252,4). In signed arithmetic,we
> get:
> a[0] * b[0] = 8
> a[1] * b[1] = 0
> a[0] * b[1] = -8
> a[1] * b[0] = 0

I'm not sure this is correct -- a[0]*b[1] == 254 * 253 == i8 6 no?
Maybe I misunderstood the subscript notation?

> So depending on the signedness of the minimum/maximum operation, we get
> different results. Therefore, we do need a signed version of multiply.

The max and min operations are already signedness-dependent
(ConstantRange has getSignedMax and getUnsignedMax as you know); but
computing the resulting range shouldn't be.

"i8 [254,0) * i8 [252,4)" is "i8 [250, 9)".  The signed max of the
result range is 8 while the unsigned max of that same range is 255.
Similar story for min.

Now it is possible that due to some reason the result for i8 [254,0) *
i8 [252,4) computed by ::multiply is more conservative than [250,9).
In that case, it is ::multiply that should be fixed since we have a
case where it can be taught to be compute a better result.

-- Sanjoy



More information about the llvm-commits mailing list