Mesa (master): glsl: Annotate as_foo functions that the this pointer cannot be NULL

Ian Romanick idr at kemper.freedesktop.org
Thu Mar 19 22:37:14 UTC 2015


Module: Mesa
Branch: master
Commit: a44b95cd574ecab4a4c41c6380c82db6029ad114
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=a44b95cd574ecab4a4c41c6380c82db6029ad114

Author: Ian Romanick <ian.d.romanick at intel.com>
Date:   Mon Mar 16 12:14:10 2015 -0700

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 at intel.com>
Reviewed-by: Matt Turner <mattst88 at gmail.com>

---

 src/glsl/ir.h |    4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/glsl/ir.h b/src/glsl/ir.h
index 25f2eca..fdc22ed 100644
--- a/src/glsl/ir.h
+++ b/src/glsl/ir.h
@@ -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)




More information about the mesa-commit mailing list