Bug 96009 - missed optimization with floating point operations and integer literals
Summary: missed optimization with floating point operations and integer literals
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 9.1.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2020-06-30 17:35 UTC by sshannin
Modified: 2020-07-01 10:11 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-07-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description sshannin 2020-06-30 17:35:54 UTC
Consider the two variants below:

double foo(char i) {
    double f = i * 100;
    return f / 100;
}

double bar(char i) {
    return i;
}

Compiled under -Ofast with gcc 9.1.0, we get the following:

foo:
movsbl  %dil, %edi
pxor  %xmm0, %xmm0
imull  $100, %edi, %edi
cvtsi2sdl %edi, %xmm0
mulsd  .LC0(%rip), %xmm0
ret

bar:
movsbl  %dil, %edi
pxor  %xmm0, %xmm0
cvtsi2sdl %edi, %xmm0
ret

.LC0:
.long  1202590843
.long  1065646817

But I think foo should be to be simplified to the same as bar, right?

This seems somewhat similar to PR91739 and PR84997, although not quite the same as far as I can discern.

(Also, separately, aren't the first two instructions of bar unnecessary? Can't we just cvtsi2sdl and ret?)

seth@dev4:$ /toolchain14/bin/g++ -v
Using built-in specs.
COLLECT_GCC=/toolchain14/bin/g++
COLLECT_LTO_WRAPPER=/toolchain14/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc_9_1_0/configure --prefix=/toolchain14 --enable-languages=c,c++,fortran --enable-lto --disable-plugin --program-suffix=-9.1.0 --disable-multi-lib
Thread model: posix
gcc version 9.1.0 (GCC)
Comment 1 Richard Biener 2020-07-01 06:50:51 UTC
Confirmed.
Comment 2 Marc Glisse 2020-07-01 10:11:33 UTC
Note that we don't do the optimization if you replace double with long either.