0

When I use gcc -O2 to optimize my program, gcc changes the value of register RBP. But I want to keep it as FRAME BASE REGISTER, how to do this?

Not the same question as: GCC: Prohibit use of some registers

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
  • 1
    it's impossible to prohibit the use of RSP. And this is not much related to register allocation because RSP and RBP are not general purpose registers – phuclv Nov 27 '14 at 08:21
  • Unless you really need `%rbp` for some other purpose, this doesn't achieve much. There's less register pressure with x86-64. – Brett Hale Nov 27 '14 at 09:44
  • Thank you for your answers, I'm sorry that I hadn't described my question clearly , As @Lưu Vĩnh Phúc say : "trace the stack frame" is the key words . :) – easy good bye Nov 28 '14 at 05:59

1 Answers1

2

-fomit-frame-pointer is enabled by default at optimization levels -O, -O2, -O3, -Os

You need to use -fno-omit-frame-pointer

However there are not much reasons to keep the frame pointer unless you're debugging and need to trace the stack frame. In that case, use -Og instead

phuclv
  • 32,499
  • 12
  • 130
  • 417
  • 1
    Thank you, I solved my problem use -fno-omit-frame-pointer! :) I do want to trace the stack frame in real time and save them into logs,but not debugging ,so I think use -fno-omit-frame-pointer is a good way. :) – easy good bye Nov 28 '14 at 05:50
  • http://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64/ has very good explanation with figures. – wcy Nov 29 '14 at 13:39
  • It can be useful for `perf` to use efficient frame-pointer stack traces instead of needing `.eh_frame` unwind metadata. – Peter Cordes Nov 26 '19 at 03:10