[llvm-dev] trunc nsw/nuw?

Dr.-Ing. Christoph Cullmann via llvm-dev llvm-dev at lists.llvm.org
Wed Jul 5 08:03:49 PDT 2017


> On 07/04/2017 01:41 AM, Dr.-Ing. Christoph Cullmann via llvm-dev wrote:
>> Hi,
>>> Hi Alexandre,
>>> LLVM currently doesn't have trunc nsw/nuw, no.
>>> Which frontend would emit such instructions? Any application in mind?
>>> Just asking because if no frontend could emit those, then the motivation to
>>> add nsw/nuw support to trunc would be very low I guess.
>> I think the clang frontend could use that to allow better static analysis of
>> integer overflows
>> on the LLVM IR.
>> It might be interesting for static analysis tools that want to know if you
>> truncate something
>> that is too large for the target range but you need the sign to determine, e.g.
>> the C/C++ frontend
>> could use that flag for trunc of signed/unsigned integers, then you could e.g.
>> check if
>> (uint8_t)x
>> changes the values if x has stuff out of the 0..255 range.
>> e.g. x as int with -100 => trunc nuw to 8 => bad
>> (int8_t)x
>>    => trunc nsw to 8 => ok
> I'm not sure that a C/C++ frontend could add this flag. In C++, the
> conversion for unsigned types is specified and for signed types, it's
> implementation defined, but in neither case is it UB.
Yes, you are right, that would be a missuse of these flags and one would need other markers
for that.

In principle what would be nice for static analysis on IR is some "we cast from signed to unsigned"
or back, even for equal bit widths to detect overflows in the representable values.


----------------------------- Dr.-Ing. Christoph Cullmann ---------
AbsInt Angewandte Informatik GmbH      Email: cullmann at AbsInt.com
Science Park 1                         Tel:   +49-681-38360-22
66123 Saarbrücken                      Fax:   +49-681-38360-20
GERMANY                                WWW:   http://www.AbsInt.com
Geschäftsführung: Dr.-Ing. Christian Ferdinand
Eingetragen im Handelsregister des Amtsgerichts Saarbrücken, HRB 11234

More information about the llvm-dev mailing list