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
  • 328,167
  • 45
  • 605
  • 847
  • 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
  • 37,963
  • 15
  • 156
  • 475
  • 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
  • 1
    https://www.phoronix.com/scan.php?page=article&item=fedora-frame-pointer&num=1 recently tested the performance cost of `-O2 -fno-omit-frame-pointer` with GCC12.1 on a Zen3 laptop CPU for multiple open-source programs, as proposed for Fedora 37. Most of them had performance regressions, a few of them very serious (probably some kind of bad thing happening at a key hotspot, probably didn't have to be that bad but happened to be.) – Peter Cordes Jul 02 '22 at 07:06