V
V
vlad94862013-07-19 17:34:17
C++ / C#
vlad9486, 2013-07-19 17:34:17

GCC generates bad code?

Keys:

-m64 -ffreestanding -nostdlib -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -pedantic

These are usually recommended for kernel development. Played with the keys, did not help.
Simple preparation:
inline thread_t* schedule(thread_t* current, tss_t* tss) {
  tss->rsp[0] = sizeof(x86_64_state_t)+(uint64_t)current;
  return current;
}

turns into:
0000000000000000 <schedule>:
   0:	55                   	push   %rbp
   1:	48 89 e5             	mov    %rsp,%rbp
   4:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)
   8:	48 89 75 f0          	mov    %rsi,-0x10(%rbp)
   c:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
  10:	48 8d 90 b0 01 00 00 	lea    0x1b0(%rax),%rdx
  17:	48 8b 45 f0          	mov    -0x10(%rbp),%rax
  1b:	48 89 50 04          	mov    %rdx,0x4(%rax)
  1f:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
  23:	5d                   	pop    %rbp
  24:	c3                   	retq

First of all, it doesn't inline, but okay. The same can be written in three lines with one memory access. Maybe I don't know some magic key, or this is how GCC optimizes. You can rewrite such places in assembler, but still you want to achieve something from the compiler.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
CleverMouse, 2013-07-19
@vlad9486

-O2

T
turboNOMAD, 2013-07-19
@turboNOMAD

Check out:
gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
In general, the documentation of GCC itself is a much more reliable source of information than unnamed uncles who "usually recommend" something there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question