[cfe-dev] Question about ASTContext::getPromotedIntegerType() function

Eli Friedman eli.friedman at gmail.com
Tue Jul 16 09:41:50 PDT 2013


On Tue, Jul 16, 2013 at 4:01 AM, JinGu Kang <jingu at codeplay.com> wrote:
> Hi all,
>
> I have a question about ASTContext::getPromotedIntegerType() function.
>
> I have tested a code as following:
>
> source code:
> int main()
> {
>   volatile unsigned short x;
>   volatile unsigned short y;
>   volatile unsigned short result;
>
>   x = 8;
>   y = 2;
>
>   result = x / y;
>
>   return 0;
> }
>
> Generated IR from clang:
>   6 define i32 @main() #0 {
>   7 entry:
>   8   %retval = alloca i32, align 4
>   9   %x = alloca i16, align 2
>  10   %y = alloca i16, align 2
>  11   %result = alloca i16, align 2
>  12   store i32 0, i32* %retval
>  13   store volatile i16 8, i16* %x, align 2
>  14   store volatile i16 2, i16* %y, align 2
>  15   %0 = load volatile i16* %x, align 2
>  16   %conv = zext i16 %0 to i32
>  17   %1 = load volatile i16* %y, align 2
>  18   %conv1 = zext i16 %1 to i32
>  19   %div = sdiv i32 %conv, %conv1
>  20   %conv2 = trunc i32 %div to i16
>  21   store volatile i16 %conv2, i16* %result, align 2
>  22   ret i32 0
>  23 }
>
> I expected "udiv" instruction on line 19 but there was "sdiv" instruction
> because of "ASTContext::getPromotedIntegerType()" function promotes
> "unsigned short" to "int".
>
> Could someone explain why this function returns "IntTy" when
> "PromotableSize" is not same with "IntSize"?

Because that's what the standard says.  C11 6.3.1.1: "If an int can
represent all values of the original type (as restricted by the width,
for a bit-field), the value is converted to an int; otherwise, it is
converted to an unsigned int."

-Eli



More information about the cfe-dev mailing list