This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] no lowering of &a.b (and &a->b) in the C/C++ front-ends



On Aug 22, 2004, at 5:28 PM, Zack Weinberg wrote:


That avoids constructing a junk tree node in the majority of cases.

That is much better and I will test the patch with that fixed.

Great, thanks.


Here is the patch which I tested and this also includes the C++ part which
is where most of the problems (both in terms of missed optimizations and
mis-compiling.


OK?

Thanks,
Andrew Pinski


ChangeLog: * c-typeck.c (build_unary_op): Use &a.b if the foldded lowered expression is not constant. (c_finish_return): Do not go through INDIRECT_REF when looking for the inner expression of an ADDR_EXPR for warning about.

cp/ChangeLog:
	* typeck.c (build_unary_op): Use &a.b if the foldded lowered
	expression is not constant.

testsuite/ChangeLog:
	* g++.dg/opt/pr14029.C: New test.
	* gcc.c-torture/execute/pr15262.c: New test.

Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.359
diff -u -p -r1.359 c-typeck.c
--- c-typeck.c	19 Aug 2004 10:35:52 -0000	1.359
+++ c-typeck.c	23 Aug 2004 02:32:05 -0000
@@ -2538,9 +2538,14 @@ build_unary_op (enum tree_code code, tre
 	    addr = fold (build2 (PLUS_EXPR, argtype,
 				 convert (argtype, addr),
 				 convert (argtype, byte_position (field))));
+	
+	    /* If we don't have constant address with the foldded PLUS_EXPR
+	       then we should use the address expression instead.  */
+	    if (!TREE_CONSTANT (addr))
+	      addr = build1 (ADDR_EXPR, argtype, arg);
 	  }
 	else
-	  addr = build1 (code, argtype, arg);
+	  addr = build1 (ADDR_EXPR, argtype, arg);

 	if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
 	  TREE_INVARIANT (addr) = TREE_CONSTANT (addr) = 1;
@@ -6310,7 +6315,8 @@ c_finish_return (tree retval)
 	    case ADDR_EXPR:
 	      inner = TREE_OPERAND (inner, 0);

-	      while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
+	      while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r'
+	             && TREE_CODE (inner) != INDIRECT_REF)
 		inner = TREE_OPERAND (inner, 0);

 	      if (DECL_P (inner)
Index: cp/typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.567
diff -u -p -r1.567 typeck.c
--- cp/typeck.c	16 Aug 2004 02:08:04 -0000	1.567
+++ cp/typeck.c	23 Aug 2004 02:32:06 -0000
@@ -4123,6 +4123,11 @@ build_unary_op (enum tree_code code, tre
 	    addr = fold (build2 (PLUS_EXPR, argtype, rval,
 				 cp_convert (argtype,
 					     byte_position (field))));
+
+	    /* If we don't have constant address with the foldded PLUS_EXPR
+	       then we should use the address expression instead.  */
+	    if (!TREE_CONSTANT (addr))
+	      addr = build_address (arg);
 	  }

if (TREE_CODE (argtype) == POINTER_TYPE



testcases:
C++ one:
g++.dg/opt/pr14029.C:
// { dg-do run }
// { dg-options "-O2" }
// We used to mis-compile this testcase as we did not know that
// &a+offsetof(b,a) was the same as &a.b

struct Iterator {
    int * ptr;

    Iterator(int * i) : ptr(i) { }
    void operator++() { ++ptr; }
    int *const & base() const { return ptr; }
};


Iterator find_7(Iterator first, Iterator last) { int trip_count = (last.base() - first.base()) >> 1;

  for ( ; trip_count > 0 ; --trip_count) {
    if (*first.ptr == 7) return first;
    ++first;

    if (*first.ptr == 7) return first;
    ++first;
  }

  switch(last.base() - first.base()) {
    case 1:
          if (*first.ptr == 7) return first;
          ++first;
    case 0:
    default:
          return last;
  }
}

int main() {
  int as[5] = {4,4,4,4,7};
  return (find_7(Iterator(&as[0]), Iterator(&as[5])).ptr == &as[5]);
};


C one: gcc.c-torture/execute/pr15262.c: // We used to mis-compile this testcase as we did not know that // &a+offsetof(b,a) was the same as &a.b struct A { int t; int i; };

void
bar (float *p)
{
  *p = 5.2;
}

int
foo(struct A *locp, int i, int str)
{
  float f, g, *p;
  int T355;
  int *T356;
  /* Currently, the alias analyzer has limited support for handling
     aliases of structure fields when no other variables are aliased.
     Introduce additional aliases to confuse it.  */
  p =  i ? &g : &f;
  bar (p);
  if (*p > 0.0)
    str = 1;

  T355 = locp->i;
  T356 = &locp->i;
  *T356 = str;
  T355 = locp->i;

  return T355;
}

main ()
{
  struct A loc;
  int str;

  loc.i = 2;
  str = foo (&loc, 10, 3);
  if (str!=1)
    abort ();
  return 0;
}


PS:
I should note that the C++ one we get much better code with the no-lowering patch and we actually do SRA on the struct while we did not before because we would have
&a + offsetof (b,a) which could not be put back when doing an INDIRECT_REF on it.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]