https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71775
Bug ID: 71775 Summary: Redundant move instruction for sign extension Product: gcc Version: 6.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: wmhkebe at gmail dot com Target Milestone: --- Created attachment 38838 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=38838&action=edit preprocessed file for the test program Compiling the following program with g++ -O3 -S: #include <cstdint> unsigned long long f(std::uint64_t x) { std::uint64_t ans = 0; for (; x != 0; x &= (x - 1)) { ans ^= __builtin_ctzll(x); } return ans; } yields the following assembly code: .text .align 4,0x90 .globl __Z1fy __Z1fy: LFB0: xorl %eax, %eax testq %rdi, %rdi je L4 .align 4,0x90 L3: bsfq %rdi, %rdx movslq %edx, %rdx xorq %rdx, %rax leaq -1(%rdi), %rdx andq %rdx, %rdi jne L3 ret L4: ret Note that the movslq instruction inside the loop is redundant. Clang is able to generate code without this move. Benchmark shows, this represents a 5%-10% difference in the running time of this function. Version info: Using built-in specs. COLLECT_GCC=gcc-6 COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc6/6.1.0/libexec/gcc/x86_64-apple-darwin15.5.0/6.1.0/lto-wrapper Target: x86_64-apple-darwin15.5.0 Configured with: ../configure --build=x86_64-apple-darwin15.5.0 --prefix=/usr/local/Cellar/gcc6/6.1.0 --libdir=/usr/local/Cellar/gcc6/6.1.0/lib/gcc/6 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-6 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl014 --with-system-zlib --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --with-build-config=bootstrap-debug --disable-werror --with-pkgversion='Homebrew gcc6 6.1.0' --with-bugurl=https://github.com/Homebrew/homebrew-versions/issues --enable-plugin --disable-nls --enable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk Thread model: posix gcc version 6.1.0 (Homebrew gcc6 6.1.0)