Why does adding inline assembly comments cause such radical change in GCC's generated code?

前端 未结 4 2019
无人共我
无人共我 2020-12-12 20:08

So, I had this code:

constexpr unsigned N = 1000;
void f1(char* sum, char* a, char* b) {
    for(int i = 0; i < N; ++i) {
        sum[i] = a[i] + b[i];
           


        
4条回答
  •  一生所求
    2020-12-12 20:44

    This answer is now modified: it was originally written with a mindset considering inline Basic Asm as a pretty strongly specified tool, but it's nothing like that in GCC. Basic Asm is weak and so the answer was edited.

    Each assembly comment acts as a breakpoint.

    EDIT: But a broken one, as you use Basic Asm. Inline asm (an asm statement inside a function body) without explicit clobber list is a weakly specified feature in GCC and its behavior is hard to define. It doesn't seem (I don't fully grasp its guarantees) attached to anything in particular, so while the assembly code must be run at some point if the function is run, it isn't clear when it is run for any non trivial optimization level. A breakpoint that can be reordered with neighboring instruction isn't a very useful "breakpoint". END EDIT

    You could run your program in an interpreter that breaks at each comment and prints out the state of every variable (using debug information). These points must exist so that you observe the environment (state of registers and memory).

    Without the comment, no observation point exists, and the loop is compiled as a single mathematical function taking an environment and producing a modified environment.

    You want to know the answer of a meaningless question: you want to know how each instruction (or maybe block, or maybe range of instruction) is compiled, but no single isolated instruction (or block) is compiled; the whole stuff is compiled as a whole.

    A better question would be:

    Hello GCC. Why do you believe this asm output is implementing the source code? Please explain step by step, with every assumption.

    But then you wouldn't want to read a proof longer than the asm output, written in term of GCC internal representation.

提交回复
热议问题