[llvm-dev] Little explanation of this behaviour

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Thu Apr 14 15:57:31 PDT 2016

Hi Lorenzo,

On 14 April 2016 at 15:44, Lorenzo Laneve via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> Can you explain me why Clang converted the char type into a 32-bit integer
> type before casting it to a floating point?

C and C++ have what's called "integer promotion rules", which apply to
most expressions involving types smaller than int and insert an
implicit promotion to int before anything else happens (in this case
another implicit conversion to double). Clang is emitting the natural
IR for this AST:

  `-CompoundStmt 0x10307d140 <col:29, line:3:1>
    `-ReturnStmt 0x10307d128 <line:2:3, col:14>
      `-ImplicitCastExpr 0x10307d110 <col:10, col:14> 'int' <FloatingToIntegral>
        `-BinaryOperator 0x10307d0e8 <col:10, col:14> 'double' '+'
          |-ImplicitCastExpr 0x10307d0d0 <col:10> 'double' <IntegralToFloating>
          | `-ImplicitCastExpr 0x10307d0a0 <col:10> 'int' <IntegralCast>
          |   `-ImplicitCastExpr 0x10307d088 <col:10> 'char' <LValueToRValue>
          |     `-DeclRefExpr 0x10307d038 <col:10> 'char' lvalue
ParmVar 0x10307ce00 'x' 'char'
          `-ImplicitCastExpr 0x10307d0b8 <col:14> 'double' <LValueToRValue>
            `-DeclRefExpr 0x10307d060 <col:14> 'double' lvalue ParmVar
0x10307ce70 'y' 'double'

(found by running "clang++ -Xclang -ast-dump tmp.cpp"). Notice there's
an IntegralCast followed by an IntegralToFloating.

> Can a sitofp i8 %3 to double be done or is it wrong?

That's fine, in fact LLVM optimizes the function to use that itself.



More information about the llvm-dev mailing list