[c++] conditional expr abort

Richard Henderson rth@redhat.com
Mon Apr 9 16:46:00 GMT 2001


On Sun, Apr 08, 2001 at 11:55:02PM +0100, Jason Merrill wrote:
> I think that doing the same in build_array_ref would be appropriate.

Here's a patch to that effect.  Ok?

> But your scheme also probably produces worse code due to missed
> optimization opportunities and unnecessary address-taking.

Perhaps, perhaps not.  I suspect it mostly depends on the instruction
set of the processor.  For instance, *(p ? a1 : a2) scheme can get:

        lda $2,a2
        lda $1,a1
        cmoveq $16,$2,$1
        ldq $0,8($1)
        ret $31,($26),1

whereas the build_array_ref scheme results in 

        beq $16,$L2
        lda $1,a1
        ldq $0,8($1)
        ret $31,($26),1
$L2:
        lda $1,a2
        ldq $0,8($1)
        ret $31,($26),1

Improved if-conversion code might be able to resolve the differences,
but is kinda hard to do.


r~


	* typeck.c (build_array_ref): Push the array reference inside
	COMPOUND_EXPR and COND_EXPR.

Index: typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.342
diff -c -p -d -r1.342 typeck.c
*** typeck.c	2001/03/06 20:45:05	1.342
--- typeck.c	2001/04/09 23:40:50
*************** build_array_ref (array, idx)
*** 2433,2438 ****
--- 2433,2459 ----
        || TREE_TYPE (idx) == error_mark_node)
      return error_mark_node;
  
+   /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
+      inside it.  */
+   switch (TREE_CODE (array))
+     {
+     case COMPOUND_EXPR:
+       {
+ 	tree value = build_array_ref (TREE_OPERAND (array, 1), idx);
+ 	return build (COMPOUND_EXPR, TREE_TYPE (value),
+ 		      TREE_OPERAND (array, 0), value);
+       }
+ 
+     case COND_EXPR:
+       return build_conditional_expr
+ 	(TREE_OPERAND (array, 0),
+ 	 build_array_ref (TREE_OPERAND (array, 1), idx),
+ 	 build_array_ref (TREE_OPERAND (array, 2), idx));
+ 
+     default:
+       break;
+     }
+ 
    if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
        && TREE_CODE (array) != INDIRECT_REF)
      {



More information about the Gcc-patches mailing list