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

JinGu Kang jingu at codeplay.com
Tue Jul 16 09:56:03 PDT 2013


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

On 16/07/2013 17:41, Eli Friedman wrote:
> 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

-- 
JinGu Kang
Codeplay Software Ltd
45 York Place, Edinburgh, EH1 3HP
Tel: 0131 466 0503
Fax: 0131 557 6600
Website: http://www.codeplay.com
Twitter: https://twitter.com/codeplaysoft

This email and any attachments may contain confidential and /or privileged information and is for use by the addressee only. If you are not the intended recipient, please notify Codeplay Software Ltd immediately and delete the message from your computer. You may not copy or forward it,or use or disclose its contents to any other person. Any views or other information in this message which do not relate to our business are not authorized by Codeplay software Ltd, nor does this message form part of any contract unless so stated.
As internet communications are capable of data corruption Codeplay Software Ltd does not accept any responsibility for any changes made to this message after it was sent. Please note that Codeplay Software Ltd does not accept any liability or responsibility for viruses and it is your responsibility to scan any attachments.
Company registered in England and Wales, number: 04567874
Registered office: 81 Linkfield Street, Redhill RH1 6BY




More information about the cfe-dev mailing list