[llvm-bugs] [Bug 37983] New: Clang/LLVM optimizes division and modulo worse than MSVC

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jun 28 17:38:53 PDT 2018


            Bug ID: 37983
           Summary: Clang/LLVM optimizes division and modulo worse than
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: sfinae at hotmail.com
                CC: llvm-bugs at lists.llvm.org

Created attachment 20488
  --> https://bugs.llvm.org/attachment.cgi?id=20488&action=edit
Test case

This affects the Ryu algorithm for printing floating-point numbers
(https://github.com/ulfjack/ryu ) and therefore affects C++17 floating-point
std::to_chars(). This is possibly the same bug as
https://bugs.llvm.org/show_bug.cgi?id=23106 "Division followed by modulo
generates longer machine code than vice versa".

I observe that MSVC's codegen is unaffected by USE_MODULO, while Clang/LLVM
generates more assembly code (which is slower when profiled in the real
algorithm) for USE_MODULO.

Microsoft (R) C/C++ Optimizing Compiler Version 19.15.26504 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

C:\Temp\TESTING_X64>clang-cl -m64 -v
clang version 6.0.0 (tags/RELEASE_600/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: S:\msvc\src\vctools\NonShip\ClangLLVM\bin

C:\Temp\TESTING_X64>type meow.cpp
unsigned long long ryu(unsigned long long vp, unsigned long long vm) {
    bool vmIsTrailingZeros = true;

    while (vp / 10 > vm / 10) {
        vmIsTrailingZeros &= vm % 10 == 0;
        // The compiler does not realize that vm % 10 can be computed from vm /
        // as vm - (vm / 10) * 10.
        vmIsTrailingZeros &= vm - (vm / 10) * 10 == 0; // vm % 10 == 0;
        vp /= 10;
        vm /= 10;

    return vmIsTrailingZeros ? vp : vm;

C:\Temp\TESTING_X64>cl /EHsc /nologo /W4 /MT /O2 /c /FAsc
/Famsvc_workaround.cod meow.cpp

C:\Temp\TESTING_X64>cl /EHsc /nologo /W4 /MT /O2 /c /FAsc /Famsvc_modulo.cod
/DUSE_MODULO meow.cpp

C:\Temp\TESTING_X64>clang-cl -m64 /EHsc /nologo /W4 /MT /O2 /c /FA
/Faclang_workaround.asm meow.cpp

C:\Temp\TESTING_X64>clang-cl -m64 /EHsc /nologo /W4 /MT /O2 /c /FA
/Faclang_modulo.asm /DUSE_MODULO meow.cpp


You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180629/7c9678d5/attachment-0001.html>

More information about the llvm-bugs mailing list