[LLVMdev] signed/unsigned integers ?

Julien Henry Julien.Henry at imag.fr
Fri Apr 1 02:47:25 PDT 2011

> there is no such information.  You can still consider every type to have values
> in, say, T = [-2^31; 2^31-1].  Probably you are trying to deduce an interval of
> possible values for each register.  You will need to allow intervals to wrap
> around the end of T since (eg) the basic "add" operator in LLVM uses modulo
> arithmetic, i.e. if you add 1 to 2^31-1 you get -2^31, which forces you to use
> wrapping intervals.  Having wrapping intervals makes interval arithmetic more
> tricky but it can still be done.  Whether an operation is signed (eg: sdiv, i.e.
> signed division) or unsigned (eg: udiv, i.e. unsigned division) you need to
> calculate the smallest interval that contains all possible result values given
> the intervals the arguments are known to belong to, or a conservative
> approximation to it.  This is perfectly do-able, it's just that your interval
> for the possible values for the result probably won't always be as precise as
> you would like [*].

Thanks for your suggestions.

Actually, for now, I consider integers as mathematical integers (in 
]-oo; +oo[), and that operators such as add don't overflow.
(indeed, that's incorrect, but I'll work on this later ;) )

Suppose I approximate the set of possible values for my variable "x" 
with intervals.
I think there are some cases for which I'll be unable to find precise 
enough invariants, for example, I'd like this:

unsigned x;
// x >= 0
if (x != 0) {
	// x >= 1

but since I consider x in ]-oo; +oo[, I have this:

unsigned x;
// x in ]-oo; +oo[
if (x != 0) {
	// x in ]-oo; +oo[  (because of the abstraction by intervals)

Unfortunately, I think this loss of precision is unavoidable. Maybe if I 
consider programs that never do comparisons between signed and unsigned, 
I could retrieve types from the signedness of the comparison, as me22 

> Some operations may be annotated with the "nsw" (no signed
> wrap) or "nuw" (no unsigned wrap) flags which allows you to do a better job
> if you are willing to assume that the program does not perform undefined
> behaviour.

I tried some examples to understand when these flags are set, because 
maybe it could help me retrieve types as well, but I confess I didn't 
understand well in which case they are set or not.


More information about the llvm-dev mailing list