[llvm-bugs] [Bug 34801] New: Function pointers not inlined and suboptimal assembly is generated

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Oct 2 07:06:20 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=34801

            Bug ID: 34801
           Summary: Function pointers not inlined and suboptimal assembly
                    is generated
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: antoshkka at gmail.com
                CC: llvm-bugs at lists.llvm.org

The following code 

enum class eShape { eSquare, eCircle, eShpere, eTetraeder };

static inline double eval_square(double r) { return 4 * r*r; }
static inline double eval_circle(double r) { return 3.1415 * r * r; }
static inline double eval_sphere(double r) { return 4 * 3.1415 * r * r; }
static inline double eval_tetraeder(double r) { return 24 * 1.73205 * r*r; }

double test_switch_table(eShape shape, double r) {
    using eval_t = double(*)(double);
    constexpr eval_t table[] = { 
        eval_square,
        eval_circle,
        eval_sphere,
        eval_tetraeder,
    };
    return table[static_cast<unsigned>(shape)](r);
}

Produces very long suboptimal assembly, that does not inline the eval_*
functions. 


generated code could be made much better by just rewriting the code in the
following way

double test_switch_native(eShape shape, double r) {
    switch(shape) {
    case eShape::eSquare:    return eval_square(r);
    case eShape::eCircle:    return eval_circle(r);
    case eShape::eShpere:    return eval_sphere(r);
    case eShape::eTetraeder: return eval_tetraeder(r);
    }
}

At -O2 such code produces a nice assembly with merged common `r*r` part:

test_switch_native(eShape, double):         # @test_switch_native(eShape,
double)
        movsxd  rax, edi
        movsd   xmm1, qword ptr [8*rax +
.Lswitch.table._Z18test_switch_native6eShaped] # xmm1 = mem[0],zero
        mulsd   xmm1, xmm0
        mulsd   xmm0, xmm1
        ret
.Lswitch.table._Z18test_switch_native6eShaped:
        .quad   4616189618054758400     # double 4
        .quad   4614256447914709615     # double 3.1415000000000002
        .quad   4623263647169450607     # double 12.566000000000001
        .quad   4631047162110439693     # double 41.569200000000002

-- 
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/20171002/3c51689c/attachment-0001.html>


More information about the llvm-bugs mailing list