[LLVMdev] nsw/nuw for trunc

Duncan Sands baldrick at free.fr
Thu Aug 11 06:56:22 PDT 2011


Hi Florian,

> we'd like to be able to check for loss of information in trunc operations in
> our LLVM-based bounded model checker [1]. For this it is important if the
> trunc was on a signed or unsigned integer, so we need nsw and nuw flags for
> this. Would you accept a patch that adds these flags to LLVM (and possibly
> clang)?

nsw/nuw don't mean signed/unsigned arithmetic.  They mean that signed/unsigned
overflow in the operation results in undefined behaviour.  As far as I know,
truncating a large signed value to a too small signed integer type does not
result in undefined behaviour.  For example, the result of (signed char)999
is perfectly well defined.  So it seems to me that nsw/nuw on truncate (which
is what this cast turns into in LLVM) don't make any sense.  Also, a truncate
operation doesn't need to be signed or unsigned, since the operation performed
is exactly the same (the same set of input bits -> the same set of output bits)
regardless of the sign of the original types.

Ciao, Duncan.



More information about the llvm-dev mailing list