[PATCH] x86: Turn off GCC branch probability heuristics

From: Ingo Molnar
Date: Sat Apr 11 2015 - 05:20:44 EST

* Ingo Molnar <mingo@xxxxxxxxxx> wrote:

> Btw., totally off topic, the following NOP caught my attention:
> > 5a: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
> That's a dead NOP that boats the function a bit, added for the 16 byte
> alignment of one of the jump targets.

Another thing caught my attention (and I'm hijacking the RCU thread
again): GCC's notion of how to place branches seems somewhat random,
and rather bloaty.

So I tried the experiment below on an x86 defconfig, turning off GCC's
branch heuristics, and it's rather surprising:

text data bss dec filename
12566447 1617840 1089536 15273823 vmlinux.fguess-branch-probability
11923593 1617840 1089536 14630969 vmlinux.fno-guess-branch-probability

That's an 5.4% code size improvement!

So maybe we should try this, as it results in much more predictable
(and more compact!) code by default - and allows us to shape loops and
branches in a natural fashion: by their placement, and also via
likely()/unlikely() hints when absolutely necessary.




From: Ingo Molnar <mingo@xxxxxxxxxx>
Date: Sat, 11 Apr 2015 11:16:30 +0200
Subject: [PATCH] x86: Turn off GCC branch probability heuristics

Not-Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx>
arch/x86/Makefile | 3 +++
1 file changed, 3 insertions(+)

diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 5ba2d9ce82dc..7c12b3f56915 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -81,6 +81,9 @@ else
KBUILD_CFLAGS += $(call cc-option,-mno-80387)
KBUILD_CFLAGS += $(call cc-option,-mno-fp-ret-in-387)

+ # Don't guess branch probabilities, follow the code and unlikely()/likely() hints:
+ KBUILD_CFLAGS += -fno-guess-branch-probability
# Use -mpreferred-stack-boundary=3 if supported.
KBUILD_CFLAGS += $(call cc-option,-mpreferred-stack-boundary=3)

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/