[LLVMdev] signed/unsigned integers ?

Duncan Sands baldrick at free.fr
Thu Mar 31 09:42:12 PDT 2011


Hi Julien,

> For an function's argument of type T, at the beginning of my analysis, I
> consider its set of possible values is all the set of all elements of
> type T.
> In the case of an int, it is [-2^31; 2^31-1], whereas it is [0, 2^32-1]
> for an unsigned...
> That's the reason why I was searching in the llvm bitcode something
> distinguishing these two types.

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 [*].  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.

Ciao, Duncan.

[*] LLVM has a ConstantRange that implements logic of this kind, so you don't
even need to write it yourself!



More information about the llvm-dev mailing list