[llvm-dev] Handling native i16 types in clang and opt

Peter Lawrence via llvm-dev llvm-dev at lists.llvm.org
Wed May 31 13:04:24 PDT 2017


Alex,
        The C language requires “short” arithmetic to be promoted to the size
of “int”, hence the conversions to “int” and later the optimizations back to “short”
but only when the optimizer can prove that the result will be the same.

If your machine has only 16-bit registers and arithmetic then you should
change clang. There won’t be any conversions in the IR (but there are
A variety of problems with LLVM’s optimizations that you will run into ! ).

If your machine has both 16-bit and 32-bit registers and arithmetic, then
you probably must leave clang alone. I am inclined to read your email
as implying this is the case for you.

Do you really need signed div and rem, usually people don’t need the
quirky results of signed div and rem (in fact more often than not they
need results consistent with two’s-complement shifts and masks) ?

If unsigned is OK then CI Should (?) transform unsigned 32-bit div
and rem of unsigned short into 16-bit unsigned div and rem. (Can someone
verify / confirm that I’m thinking correctly here ?)


The only thing I can think of off the top of my head for getting 16-bit sdiv
and srem instructions emitted on a 32-bit machine is with inline-asm ?


BTW, IIRC sdiv and srem also inhibit vectorization to 16-bit SIMD
instructions for the same reason (similarly shifts become undef for different
shift amounts in 16-bit), I wonder what work-arounds folks use in
this context, perhaps someone else on this list can chime in ?


-Peter Lawrence.



> 
> Message: 6
> Date: Sun, 21 May 2017 11:22:20 +0300
> Subject: [llvm-dev] Handling native i16 types in clang and opt
> 
>   Hello.
>     My target architecture supports natively 16 bit integers (i16).
> 
>     Whenever I write in C programs using only short types, clang compiles the program to 
> LLVM and converts the i16 data to i32 to perform arithmetic operations and then truncates 
> the results to i16. Then, the InstructionCombining (INSTCOMBINE or IC) pass removes these 
> conversions back and forth from i16, except for the (s)div LLVM IR operation.
> 
>     Is there a way to avoid these conversion made by clang back and forth from i16 to 
> i32, if my source program uses only short types?
>     Otherwise, how can I make the IC pass handle sdiv the way it does with add (sub), 
> mul? (that is, if the input operands are i16, the add/mul operation will eventually be 
> i16, with any unnecessary conversion back and forth from i32 removed).
> 
>   Thank you,
>     Alex

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170531/a3863f9f/attachment.html>


More information about the llvm-dev mailing list