Question about ASTContext::getPromotedIntegerType() function

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"? I think that the line of "ASTContext::getPromotedIntegerType()" function should be changed.
ex)
return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
--> return UnsignedIntTy;

Thanks,
JinGu Kang

clang.patch (551 Bytes)

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

Hi Eli,

I appreciate your comment. It's really helpful to understand the code.

How do you feel about generating "sdiv" instruction on previous example?

Thanks,
JinGu Kang

How else would you divide two signed integers? Instcombine knows how
to optimize it to a udiv. I mean, we could hack an optimization into
clang, but it would be the same optimization instcombine does, and
there isn't much point to optimizing the performance of -O0 code.

-Eli

2013-07-17 오전 3:00, Eli Friedman 쓴 글: