Lorenzo Laneve via llvm-dev
2016-Apr-14 22:44 UTC
[llvm-dev] Little explanation of this behaviour
I tried to emit IR for a function that returns the sum between an signed char and a double in C++, just to see how Clang handles type implicit casting. Can you explain me why Clang converted the char type into a 32-bit integer type before casting it to a floating point? Can a sitofp i8 %3 to double be done or is it wrong? define i32 @_Z5sumad(i8 signext %x, double %y) #0 { %1 = alloca i8, align 1 %2 = alloca double, align 8 store i8 %x, i8* %1, align 1 store double %y, double* %2, align 8 %3 = load i8, i8* %1, align 1 %4 = sext i8 %3 to i32 %5 = sitofp i32 %4 to double %6 = load double, double* %2, align 8 %7 = fadd double %5, %6 %8 = fptosi double %7 to i32 ret i32 %8 } -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160415/8c96dff2/attachment.html>
Tim Northover via llvm-dev
2016-Apr-14 22:57 UTC
[llvm-dev] Little explanation of this behaviour
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. Cheers. Tim.
Lorenzo Laneve via llvm-dev
2016-Apr-14 23:23 UTC
[llvm-dev] Little explanation of this behaviour
Thanks, so what’s the point of these rules? Do they grant something like safety or faster execution?> 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).> >> 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.Are you saying that instruction will be optimized by LLVM in this case?