LLVM的整数类型 [英] LLVM's integer types

查看:177
本文介绍了LLVM的整数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LLVM语言将整数类型指定为iN,其中N是整数的位宽,范围为1到2 ^ 23-1(根据:

The LLVM language specifies integer types as iN, where N is the bit-width of the integer, and ranges from 1 to 2^23-1 (According to: http://llvm.org/docs/LangRef.html#integer-type)

我有2个问题:

  1. 将C程序编译到LLVM IR级别时,哪些类型可以降低到i1,i2,i3等?看来i8,i16,i32,i64类型必须足够,所以我想知道其他将近800万个整数类型又是什么.

  1. When compiling a C program down to LLVM IR level, what types may be lowered to i1, i2, i3, etc? It seems like the types i8, i16, i32, i64 must be enough, so I was wondering what all the other nearly 8 million integer types are for.

有符号和无符号整数类型都降低到i32是真的吗?原因是什么?为什么它不适用于32位浮点数(在LLVM中用f32表示)?

Is it true that both signed and unsigned integer types are lowered to i32? What is the reason for that, and why does it not apply to something like 32-bit float (which is represented as f32 in LLVM)?

推荐答案

首先,请注意LLVM 2.0中添加了任意大小的整数,并且带符号和无符号整数之间没有区别.早期版本只有几个整数类型,带有符号/无符号的区别.

First of all, be aware both arbitrary-sized integers and no distinction between signed and unsigned integers are modifications added to LLVM 2.0. Earlier versions had only a few integer types, with a signed/unsigned distinction.

现在,您的问题:

  1. LLVM尽管考虑了C/C ++的设计,但并不特定于这些语言.拥有更多可能的整数类型可为您提供更大的灵活性.当然,您不必使用这些类型-我猜您已经提到,任何LLVM的C/C ++前端(即Clang)可能只会生成i1,i8,i16,i32和i64

  1. LLVM, though designed with C/C++ in mind, is not specific to these languages. Having more possible integer types gives you more flexibility. You don't have to use these types, of course - and I'm guessing that, as you've mentioned, any C/C++ frontend to LLVM (i.e. Clang) would probably only generate i1, i8, i16, i32 and i64.

显然我弄错了,Clang也使用其他一些整数类型,请参阅下面的Jens评论.

apparently I'm mistaken and Clang does use some other integer types as well, see Jens's comment below.

是的,LLVM不会区分有符号和无符号整数类型,因此两者都将降为i32.但是,对无符号整数的运算将根据原始类型进行转换;例如无符号整数之间的除法将为udiv,而有符号整数之间的除法将为sdiv.因为整数表示为二进制补码,但是许多操作(例如add)并不关心带符号/unsigned,因此只有一个版本.

Yes, LLVM does not make a distinction between signed and unsigned integer type, so both will be lowered to i32. The operations on the unsigned integer, though, will be translated according to the original type; e.g. a division between unsigned integers will be udiv while between signed will be sdiv. Because integers are represented as two's complement, though, many operations (e.g. add) don't care about signed/unsigned and so only have a single version.

关于为什么在LLVM中没有签名和未签名之间没有区别,请阅读有关此增强请求的详细信息-简而言之,具有签名和未签名的版本都会导致IR膨胀,并且不利于某些优化,因此将其删除.

As for why no distinction was made in LLVM between signed and unsigned, read the details on this enhancement request - in short, having both signed and unsigned versions led to a large IR bloat and was detrimental to some optimizations, so it was dropped.

最后,您问为什么不使用f32-答案是我不知道,也许它被认为比任意大小的整数有用.但是,请注意,f32并不是真正的描述性-如果要使用任意浮点类型,则至少需要指定基数的大小和指数的大小,例如f23e8而不是floatf52e11而不是double.如果您问我,那有点麻烦,尽管我想floatdouble可能已经成为它们的代名词.

Finally, you ask about why no f32 - the answer is that I don't know, maybe it was deemed to be less useful than arbitrarily-sized integers. However, notice that f32 is not really descriptive - if you want arbitrary floating-point types you need to at least specify the size of the base number and the size of the exponent, something like f23e8 instead of float and f52e11 instead of double. That's a bit cumbersome if you ask me, though I guess float and double could have been made synonymous with those.

这篇关于LLVM的整数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆