glsl: Annotate as_foo functions that the this pointer cannot be NULL

We use the idiom

   ir_foo *x = y->as_foo();
   if (x == NULL)
      return;

all over the place.  GCC generates some quite lovely code for this.
One such example:

  340a5b:       83 7d 18 04             cmpl   $0x4,0x18(%rbp)
  340a5f:       0f 85 06 04 00 00       jne    340e6b
  340a65:       48 85 ed                test   %rbp,%rbp
  340a68:       0f 84 fd 03 00 00       je     340e6b

This case used as_expression() (ir_type_expression is 4).  Note that it
checks the ir_type, then checks that the pointer isn't NULL.  There is
some disconnect in GCC around the condition in the as_foo functions.

      return ir_type == ir_type_##TYPE ? (ir_##TYPE *) this : NULL; \

It believes "this" could be NULL, so it emits check outside the function
just for fun.

This patch uses assume() to tell GCC that it need not bother with extra
NULL checking of the pointer returned by the as_foo functions.

   text	   data	    bss	    dec	    hex	filename
4836430	 158688	  26248	5021366	 4c9eb6	i965_dri-before.so
4836173	 158688	  26248	5021109	 4c9db5	i965_dri-after.so

v2: Replace 'if (this == NULL) unreachable("this cannot be NULL")' with
assume(this != NULL).  Suggested by Ilia Mirkin.

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
This commit is contained in:
Ian Romanick 2015-03-16 12:14:10 -07:00
parent bf9d921936
commit a44b95cd57
1 changed files with 4 additions and 0 deletions

View File

@ -119,6 +119,7 @@ public:
/*@{*/
class ir_rvalue *as_rvalue()
{
assume(this != NULL);
if (ir_type == ir_type_dereference_array ||
ir_type == ir_type_dereference_record ||
ir_type == ir_type_dereference_variable ||
@ -132,6 +133,7 @@ public:
class ir_dereference *as_dereference()
{
assume(this != NULL);
if (ir_type == ir_type_dereference_array ||
ir_type == ir_type_dereference_record ||
ir_type == ir_type_dereference_variable)
@ -141,6 +143,7 @@ public:
class ir_jump *as_jump()
{
assume(this != NULL);
if (ir_type == ir_type_loop_jump ||
ir_type == ir_type_return ||
ir_type == ir_type_discard)
@ -151,6 +154,7 @@ public:
#define AS_CHILD(TYPE) \
class ir_##TYPE * as_##TYPE() \
{ \
assume(this != NULL); \
return ir_type == ir_type_##TYPE ? (ir_##TYPE *) this : NULL; \
}
AS_CHILD(variable)