[llvm-bugs] [Bug 39109] New: constexpr fibonacci is not computed at compile time

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Sep 28 02:32:05 PDT 2018


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

            Bug ID: 39109
           Summary: constexpr fibonacci is not computed at compile time
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org

constexpr uint32_t fib(const uint32_t n) {
  if (n <= 1) return 1;
  return fib(n - 1) + fib(n - 2);
}

int main() {
  return fib(7);
}

int main2() {
  const uint32_t res = fib(7);
  return res;
}

Clang is unable to compute fib in main (but it can in main2) at compile time,
GCC can do it in both functions.

main:                                   # @main
        mov     edi, 7
        jmp     fib(unsigned int)    


While main2 is interesting.. Clang computes it as 21 but then it still calls
fib uselessly.

main2():                              # @main2()
        push    rax
        mov     edi, 7
        call    fib(unsigned int)
        mov     eax, 21
        pop     rcx
        ret


Latest GCC:
main:
        mov     eax, 21
        ret
main2():
        mov     eax, 21
        ret

-- 
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/20180928/f23cd5e4/attachment.html>


More information about the llvm-bugs mailing list