Thread (4 messages) flat view 4 messages, 3 authors, 2011-01-28

RE: [PATCH 2/7] PowerPC: add unlikely() to BUG_ON()

From: David Laight <hidden>
Date: 2011-01-28 09:14:57
Also in: lkml

=20
quoted
+#define __BUG_ON(x) do {					\
  	if (__builtin_constant_p(x)) {				\
  		if (x)						\
  			BUG();					\
@@ -85,6 +86,8 @@
  	}							\
  } while (0)

+#define BUG_ON(x) __BUG_ON(unlikely(x))
+
From my experiments, adding an 'unlikely' at that point isn't
enough for non-trivial conditions - so its presence will
give a false sense the the optimisation is present!
In particular 'if (unlikely(x && y))' needs to be
'if (unlikely(x) && unlikely(y))' in order to avoid
mispredicted branches when 'x' is false.

Also, as (I think) in some of the generated code quoted,
use of __builtin_expect() with a boolean expression can
force some versions of gcc to generate the integer
value of the expression - rather than just selecting the
branch instructions that statically predict the
normal code path.

Sometimes I've also also had to add an asm() statement
that generates no code in order to actually force a
forwards branch (so it has something to place at the
target).

(I've been counting clocks ....)

	David
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help