Which internal file in g++ contain the information

2019-06-09 01:03发布

问题:

This is about finding the Fibonacci number using recursive approach which I had asked in my previous question. Using one of the solution(answered), the run time taken by the program was almost 0. I attach the program in GDB and check the assembly instruction and found the following:

#include<iostream>
template<size_t N>
struct fibonacci:std::integral_constant<size_t,fibonacci<N-1>{}+fibonacci<N-2>{}>{};
template<> struct fibonacci<1> : std::integral_constant<size_t,1> {};
template<> struct fibonacci<0> : std::integral_constant<size_t,0> {};
int main() {
    int out = 0;
    constexpr int number = 40;
    out = fibonacci<number>();
    std::cout<<"Fibonacci Series Of "<<number<<" is "<<out<<std::endl;
}

I have compiled my program using following flags and assembly instruction of my program is as:

$g++ -g -gdwarf-2 -Wall -fdump-tree-all -std=c++11 fibonacci.cpp -o fibcpp

(gdb) disassemble main
   Dump of assembler code for function main():
   0x0000000000400890 <+0>: push   %rbp
   0x0000000000400891 <+1>: mov    %rsp,%rbp
   0x0000000000400894 <+4>: sub    $0x10,%rsp
  0x0000000000400898 <+8>:  movl   $0x0,-0x8(%rbp)
   0x000000000040089f <+15>:    movl   $0x28,-0x4(%rbp)
   0x00000000004008a6 <+22>:    lea    -0x9(%rbp),%rax
   0x00000000004008aa <+26>:    mov    %rax,%rdi
=>  0x00000000004008ad <+29>:   callq  0x400952 <std::integral_constant<unsigned long, 102334155ul>::operator unsigned long() const>
   0x00000000004008b2 <+34>:    mov    %eax,-0x8(%rbp)
   0x00000000004008b5 <+37>:    mov    $0x400a15,%esi

we can see that(on the arrowed==>) 102334155 is there which is fibonacci(40). This indicates that indeed all calculation has happened in the compile time.

When we compile our program and put extra flag(-fdump-tree-all), we get many internal files and normally(fibonacci.gimple) files are the one where normally template instantiated code would go. However in this case I did not find anything related to this calculation in fibonacci.gimple file.

My question is in which file g++ does calculate and store these information?. My aim over here is to understand more about compile time calculation/manipulation which happens in C++ program.

回答1:

From you disassembly it seems, that the "method" operator unsigned long() is called and not inlined. When you look at its disassembly, you should see the actual returned value. It is the instantiation of integral_constant<>::operator value_type() with size_t = unsigned long as value_type.

But you might already know all that... You want to actually see it. The message https://gcc.gnu.org/ml/gcc/2011-06/msg00110.html suggests, that others thought about an -ftrace-template-instantiation option, but no one implemented it, yet.

EDIT: There is lots of information about debugging and tracing templates in this question.