1

With most optimizations disabled (-O0) GCC compiles this function to the below assembly code:

int dummy_function(int x)
{
    if (x == 1) {
        return 1;
    } else if (x == 2) {
        return 2;
    } else if (x == 3) {
        return 3;
    } else if (x == 4) {
        return 4;
    } else if (x == 5) {
        return 5;
    } else if (x == 6) {
        return 6;
    } else {
        return 10;
    }
}
  0:    55                      push   rbp
  1:    48 89 e5                mov    rbp,rsp
  4:    89 7d fc                mov    DWORD PTR [rbp-0x4],edi
  7:    83 7d fc 01             cmp    DWORD PTR [rbp-0x4],0x1
  b:    75 07                   jne    14 <test+0x14>
  d:    b8 01 00 00 00          mov    eax,0x1
 12:    eb 46                   jmp    5a <test+0x5a>
 14:    83 7d fc 02             cmp    DWORD PTR [rbp-0x4],0x2
 18:    75 07                   jne    21 <test+0x21>
 1a:    b8 02 00 00 00          mov    eax,0x2
 1f:    eb 39                   jmp    5a <test+0x5a>
 21:    83 7d fc 03             cmp    DWORD PTR [rbp-0x4],0x3
 25:    75 07                   jne    2e <test+0x2e>
 27:    b8 03 00 00 00          mov    eax,0x3
 2c:    eb 2c                   jmp    5a <test+0x5a>
 2e:    83 7d fc 04             cmp    DWORD PTR [rbp-0x4],0x4
 32:    75 07                   jne    3b <test+0x3b>
 34:    b8 04 00 00 00          mov    eax,0x4
 39:    eb 1f                   jmp    5a <test+0x5a>
 3b:    83 7d fc 05             cmp    DWORD PTR [rbp-0x4],0x5
 3f:    75 07                   jne    48 <test+0x48>
 41:    b8 05 00 00 00          mov    eax,0x5
 46:    eb 12                   jmp    5a <test+0x5a>
 48:    83 7d fc 06             cmp    DWORD PTR [rbp-0x4],0x6
 4c:    75 07                   jne    55 <test+0x55>
 4e:    b8 06 00 00 00          mov    eax,0x6
 53:    eb 05                   jmp    5a <test+0x5a>
 55:    b8 0a 00 00 00          mov    eax,0xa
 5a:    5d                      pop    rbp
 5b:    c3                      ret    

Return instructions are replaced with a jmp to the last one even when non merge instructions option is used. How to prevent GCC from doing this optimization ?

cafce25
  • 15,907
  • 4
  • 25
  • 31
Yacine Hebbal
  • 394
  • 3
  • 16
  • 1
    This seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What *problem* is GCC optimizing its output causing? And what do you mean by "non merge instructions option"? I can not find any [GCC option](https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html) that seems to fit that description exactly. Do you mean [tail merge](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options)? Are you saying `-fno-tail-merge` doesn't work? – Andrew Henle Dec 20 '22 at 11:37
  • 1
    I wonder what benefit you would get from having an extra `pop` and `ret` instruction per condition? You would need a conditional jump anyway. – Gerhardh Dec 20 '22 at 11:42
  • @Andrew Henle yeah I was referring to -fno-tail-merge , it did not change anything. – Yacine Hebbal Dec 20 '22 at 12:05
  • @Gerhardh I am working on a binary rewriting project in which return instructions must stay where they are in the source code – Yacine Hebbal Dec 20 '22 at 12:05
  • *return instructions must stay where they are in the source code* Well, you [can't use a C compiler then](http://port70.net/%7Ensz/c/c11/n1570.html#5.1.2.3), as there are no requirements whatsoever that any C compiler has to do that. See [**What exactly is the "as-if" rule?**](https://stackoverflow.com/questions/15718262/what-exactly-is-the-as-if-rule). And C does not have "instructions" anyway, so there's really no concept of "where [instructions] are" in the C source code. Logically one could say there's one return "instruction" that is always at the last brace of the function. – Andrew Henle Dec 20 '22 at 12:19
  • 2
    Sounds like you should be writing the code in (inline) assembler. – Lundin Dec 20 '22 at 12:55
  • Some commercial compilers can be controlled to behave exactly as I want. So my question was to know if GCC also can be used to reach the result I am looking for – Yacine Hebbal Dec 21 '22 at 14:12

0 Answers0