OSDN Git Service

2008-02-21 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-sra.c
index fed7fbd..26f1af4 100644 (file)
@@ -1,7 +1,7 @@
 /* Scalar Replacement of Aggregates (SRA) converts some structure
    references into scalar references, exposing them to the scalar
    optimizers.
-   Copyright (C) 2003, 2004, 2005, 2006, 2007
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
      Free Software Foundation, Inc.
    Contributed by Diego Novillo <dnovillo@redhat.com>
 
@@ -1275,7 +1275,7 @@ instantiate_element (struct sra_elt *elt)
       elt->in_bitfld_block = 1;
       elt->replacement = build3 (BIT_FIELD_REF, elt->type, var,
                                 DECL_SIZE (var),
-                                BITS_BIG_ENDIAN
+                                BYTES_BIG_ENDIAN
                                 ? size_binop (MINUS_EXPR,
                                               TYPE_SIZE (elt->type),
                                               DECL_SIZE (var))
@@ -2127,30 +2127,37 @@ static tree
 sra_build_assignment (tree dst, tree src)
 {
   /* Turning BIT_FIELD_REFs into bit operations enables other passes
-     to do a much better job at optimizing the code.  */
+     to do a much better job at optimizing the code.
+     From dst = BIT_FIELD_REF <var, sz, off> we produce
+
+       SR.1 = (scalar type) var;
+       SR.2 = SR.1 >> off;
+       SR.3 = SR.2 & ((1 << sz) - 1);
+       ... possible sign extension of SR.3 ...
+       dst = (destination type) SR.3;
+   */
   if (scalar_bitfield_p (src))
     {
-      tree cst, cst2, mask, minshift, maxshift;
-      tree tmp, var, utype, stype;
+      tree var, shift, width;
+      tree utype, stype, stmp, utmp;
       tree list, stmt;
       bool unsignedp = BIT_FIELD_REF_UNSIGNED (src);
 
       var = TREE_OPERAND (src, 0);
-      cst = TREE_OPERAND (src, 2);
-      cst2 = size_binop (PLUS_EXPR, TREE_OPERAND (src, 1),
-                        TREE_OPERAND (src, 2));
-
-      if (BITS_BIG_ENDIAN)
+      width = TREE_OPERAND (src, 1);
+      /* The offset needs to be adjusted to a right shift quantity
+        depending on the endianess.  */
+      if (BYTES_BIG_ENDIAN)
        {
-         maxshift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), cst);
-         minshift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), cst2);
+         tree tmp = size_binop (PLUS_EXPR, width, TREE_OPERAND (src, 2));
+         shift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), tmp);
        }
       else
-       {
-         maxshift = cst2;
-         minshift = cst;
-       }
+       shift = TREE_OPERAND (src, 2);
 
+      /* In weird cases we have non-integral types for the source or
+        destination object.
+        ???  For unknown reasons we also want an unsigned scalar type.  */
       stype = TREE_TYPE (var);
       if (!INTEGRAL_TYPE_P (stype))
        stype = lang_hooks.types.type_for_size (TREE_INT_CST_LOW
@@ -2166,117 +2173,92 @@ sra_build_assignment (tree dst, tree src)
        utype = unsigned_type_for (utype);
 
       list = NULL;
+      stmp = make_rename_temp (stype, "SR");
 
-      cst2 = size_binop (MINUS_EXPR, maxshift, minshift);
-      if (tree_int_cst_equal (cst2, TYPE_SIZE (utype)))
-       {
-         unsignedp = true;
-         mask = NULL_TREE;
-       }
-      else
-       {
-         mask = build_int_cst_wide (utype, 1, 0);
-         cst = int_const_binop (LSHIFT_EXPR, mask, cst2, true);
-         mask = int_const_binop (MINUS_EXPR, cst, mask, true);
-       }
-
-      tmp = make_rename_temp (stype, "SR");
-      if (TYPE_MAIN_VARIANT (TREE_TYPE (var)) != TYPE_MAIN_VARIANT (stype))
+      /* Convert the base var of the BIT_FIELD_REF to the scalar type
+        we use for computation if we cannot use it directly.  */
+      if (!useless_type_conversion_p (stype, TREE_TYPE (var)))
        {
          if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
-           stmt = build_gimple_modify_stmt (tmp,
+           stmt = build_gimple_modify_stmt (stmp,
                                             fold_convert (stype, var));
          else
-           stmt = build_gimple_modify_stmt (tmp,
+           stmt = build_gimple_modify_stmt (stmp,
                                             fold_build1 (VIEW_CONVERT_EXPR,
                                                          stype, var));
          append_to_statement_list (stmt, &list);
-
-         var = tmp;
+         var = stmp;
        }
 
-      if (!integer_zerop (minshift))
+      if (!integer_zerop (shift))
        {
-         tmp = make_rename_temp (stype, "SR");
-         stmt = build_gimple_modify_stmt (tmp,
+         stmt = build_gimple_modify_stmt (stmp,
                                           fold_build2 (RSHIFT_EXPR, stype,
-                                                       var, minshift));
+                                                       var, shift));
          append_to_statement_list (stmt, &list);
-
-         var = tmp;
+         var = stmp;
        }
 
-      if (TYPE_MAIN_VARIANT (utype) != TYPE_MAIN_VARIANT (stype))
+      /* If we need a masking operation, produce one.  */
+      if (TREE_INT_CST_LOW (width) == TYPE_PRECISION (stype))
+       unsignedp = true;
+      else
        {
-         if (!mask && unsignedp
-             && (TYPE_MAIN_VARIANT (utype)
-                 == TYPE_MAIN_VARIANT (TREE_TYPE (dst))))
-           tmp = dst;
-         else
-           tmp = make_rename_temp (utype, "SR");
+         tree one = build_int_cst_wide (stype, 1, 0);
+         tree mask = int_const_binop (LSHIFT_EXPR, one, width, 0);
+         mask = int_const_binop (MINUS_EXPR, mask, one, 0);
 
-         stmt = build_gimple_modify_stmt (tmp, fold_convert (utype, var));
+         stmt = build_gimple_modify_stmt (stmp,
+                                          fold_build2 (BIT_AND_EXPR, stype,
+                                                       var, mask));
          append_to_statement_list (stmt, &list);
-
-         var = tmp;
+         var = stmp;
        }
 
-      if (mask)
+      /* After shifting and masking, convert to the target type.  */
+      utmp = stmp;
+      if (!useless_type_conversion_p (utype, stype))
        {
-         if (!unsignedp
-             || (TYPE_MAIN_VARIANT (TREE_TYPE (dst))
-                 != TYPE_MAIN_VARIANT (utype)))
-           tmp = make_rename_temp (utype, "SR");
-         else
-           tmp = dst;
+         utmp = make_rename_temp (utype, "SR");
 
-         stmt = build_gimple_modify_stmt (tmp,
-                                          fold_build2 (BIT_AND_EXPR, utype,
-                                                       var, mask));
+         stmt = build_gimple_modify_stmt (utmp, fold_convert (utype, var));
          append_to_statement_list (stmt, &list);
 
-         var = tmp;
+         var = utmp;
        }
 
+      /* Perform sign extension, if required.
+        ???  This should never be necessary.  */
       if (!unsignedp)
        {
          tree signbit = int_const_binop (LSHIFT_EXPR,
                                          build_int_cst_wide (utype, 1, 0),
-                                         size_binop (MINUS_EXPR, cst2,
-                                                     bitsize_int (1)),
-                                         true);
+                                         size_binop (MINUS_EXPR, width,
+                                                     bitsize_int (1)), 0);
 
-         tmp = make_rename_temp (utype, "SR");
-         stmt = build_gimple_modify_stmt (tmp,
+         stmt = build_gimple_modify_stmt (utmp,
                                           fold_build2 (BIT_XOR_EXPR, utype,
                                                        var, signbit));
          append_to_statement_list (stmt, &list);
 
-         var = tmp;
-
-         if (TYPE_MAIN_VARIANT (TREE_TYPE (dst)) != TYPE_MAIN_VARIANT (utype))
-           tmp = make_rename_temp (utype, "SR");
-         else
-           tmp = dst;
-
-         stmt = build_gimple_modify_stmt (tmp,
+         stmt = build_gimple_modify_stmt (utmp,
                                           fold_build2 (MINUS_EXPR, utype,
-                                                       var, signbit));
+                                                       utmp, signbit));
          append_to_statement_list (stmt, &list);
 
-         var = tmp;
+         var = utmp;
        }
 
-      if (var != dst)
+      /* Finally, move and convert to the destination.  */
+      if (!useless_type_conversion_p (TREE_TYPE (dst), TREE_TYPE (var)))
        {
          if (INTEGRAL_TYPE_P (TREE_TYPE (dst)))
            var = fold_convert (TREE_TYPE (dst), var);
          else
            var = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (dst), var);
-
-         stmt = build_gimple_modify_stmt (dst, var);
-         append_to_statement_list (stmt, &list);
        }
+      stmt = build_gimple_modify_stmt (dst, var);
+      append_to_statement_list (stmt, &list);
 
       return list;
     }
@@ -2288,7 +2270,13 @@ sra_build_assignment (tree dst, tree src)
      Since such accesses under different types require compatibility
      anyway, there's little point in making tests and/or adding
      conversions to ensure the types of src and dst are the same.
-     So we just assume type differences at this point are ok.  */
+     So we just assume type differences at this point are ok.
+     The only exception we make here are pointer types, which can be different
+     in e.g. structurally equal, but non-identical RECORD_TYPEs.  */
+  if (POINTER_TYPE_P (TREE_TYPE (dst))
+      && !useless_type_conversion_p (TREE_TYPE (dst), TREE_TYPE (src)))
+    src = fold_convert (TREE_TYPE (dst), src);
+
   return build_gimple_modify_stmt (dst, src);
 }
 
@@ -2322,7 +2310,7 @@ sra_build_bf_assignment (tree dst, tree src)
                     fold_convert (bitsizetype, TREE_OPERAND (dst, 1)),
                     cst);
 
-  if (BITS_BIG_ENDIAN)
+  if (BYTES_BIG_ENDIAN)
     {
       maxshift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), cst);
       minshift = size_binop (MINUS_EXPR, TYPE_SIZE (TREE_TYPE (var)), cst2);
@@ -2343,8 +2331,14 @@ sra_build_bf_assignment (tree dst, tree src)
     utype = unsigned_type_for (type);
 
   mask = build_int_cst_wide (utype, 1, 0);
-  cst = int_const_binop (LSHIFT_EXPR, mask, maxshift, true);
-  cst2 = int_const_binop (LSHIFT_EXPR, mask, minshift, true);
+  if (TREE_INT_CST_LOW (maxshift) == TYPE_PRECISION (utype))
+    cst = build_int_cst_wide (utype, 0, 0);
+  else
+    cst = int_const_binop (LSHIFT_EXPR, mask, maxshift, true);
+  if (integer_zerop (minshift))
+    cst2 = mask;
+  else
+    cst2 = int_const_binop (LSHIFT_EXPR, mask, minshift, true);
   mask = int_const_binop (MINUS_EXPR, cst, cst2, true);
   mask = fold_build1 (BIT_NOT_EXPR, utype, mask);
 
@@ -2508,13 +2502,13 @@ sra_build_elt_assignment (struct sra_elt *elt, tree src)
            {
              list = NULL;
 
-             if (!INTEGRAL_TYPE_P (TREE_TYPE (src))
-                 || !TYPE_UNSIGNED (TREE_TYPE (src)))
+             if (!INTEGRAL_TYPE_P (TREE_TYPE (src)))
                src = fold_build1 (VIEW_CONVERT_EXPR,
                                   lang_hooks.types.type_for_size
                                   (TREE_INT_CST_LOW
                                    (TYPE_SIZE (TREE_TYPE (src))),
                                    1), src);
+             gcc_assert (TYPE_UNSIGNED (TREE_TYPE (src)));
 
              tmp = make_rename_temp (TREE_TYPE (src), "SR");
              stmt = build_gimple_modify_stmt (tmp, src);
@@ -2612,7 +2606,33 @@ generate_element_copy (struct sra_elt *dst, struct sra_elt *src, tree *list_p)
 
          continue;
        }
-      gcc_assert (sc);
+
+      /* If DST and SRC are structs with the same elements, but do not have
+        the same TYPE_MAIN_VARIANT, then lookup of DST FIELD_DECL in SRC
+        will fail.  Try harder by finding the corresponding FIELD_DECL
+        in SRC.  */
+      if (!sc)
+       {
+         tree f;
+
+         gcc_assert (useless_type_conversion_p (dst->type, src->type));
+         gcc_assert (TREE_CODE (dc->element) == FIELD_DECL);
+         for (f = TYPE_FIELDS (src->type); f ; f = TREE_CHAIN (f))
+           if (simple_cst_equal (DECL_FIELD_OFFSET (f),
+                                 DECL_FIELD_OFFSET (dc->element)) > 0
+               && simple_cst_equal (DECL_FIELD_BIT_OFFSET (f),
+                                    DECL_FIELD_BIT_OFFSET (dc->element)) > 0
+               && simple_cst_equal (DECL_SIZE (f),
+                                    DECL_SIZE (dc->element)) > 0
+               && (useless_type_conversion_p (TREE_TYPE (dc->element),
+                                              TREE_TYPE (f))
+                   || (POINTER_TYPE_P (TREE_TYPE (dc->element))
+                       && POINTER_TYPE_P (TREE_TYPE (f)))))
+             break;
+         gcc_assert (f != NULL_TREE);
+         sc = lookup_element (src, f, NULL, NO_INSERT);
+       }
+
       generate_element_copy (dc, sc, list_p);
     }
 
@@ -2870,7 +2890,7 @@ struct bitfield_overlap_info
 };
 
 /* Return true if a BIT_FIELD_REF<(FLD->parent), BLEN, BPOS>
-   expression (refereced as BF below) accesses any of the bits in FLD,
+   expression (referenced as BF below) accesses any of the bits in FLD,
    false if it doesn't.  If DATA is non-null, its field_len and
    field_pos are filled in such that BIT_FIELD_REF<(FLD->parent),
    field_len, field_pos> (referenced as BFLD below) represents the
@@ -2900,7 +2920,8 @@ bitfield_overlaps_p (tree blen, tree bpos, struct sra_elt *fld,
   else if (TREE_CODE (fld->element) == INTEGER_CST)
     {
       flen = fold_convert (bitsizetype, TYPE_SIZE (fld->type));
-      fpos = size_binop (MULT_EXPR, flen, fld->element);
+      fpos = fold_convert (bitsizetype, fld->element);
+      fpos = size_binop (MULT_EXPR, flen, fpos);
     }
   else
     gcc_unreachable ();
@@ -2976,16 +2997,20 @@ sra_explode_bitfield_assignment (tree var, tree vpos, bool to_var,
 
       if (fld->replacement)
        {
-         tree infld, invar, st;
+         tree infld, invar, st, type;
 
          infld = fld->replacement;
 
+         type = TREE_TYPE (infld);
+         if (TYPE_PRECISION (type) != TREE_INT_CST_LOW (flen))
+           type = lang_hooks.types.type_for_size (TREE_INT_CST_LOW (flen), 1);
+
          if (TREE_CODE (infld) == BIT_FIELD_REF)
            {
              fpos = size_binop (PLUS_EXPR, fpos, TREE_OPERAND (infld, 2));
              infld = TREE_OPERAND (infld, 0);
            }
-         else if (BITS_BIG_ENDIAN && DECL_P (fld->element)
+         else if (BYTES_BIG_ENDIAN && DECL_P (fld->element)
                   && !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (infld)),
                                           DECL_SIZE (fld->element)))
            {
@@ -2995,10 +3020,7 @@ sra_explode_bitfield_assignment (tree var, tree vpos, bool to_var,
                                 DECL_SIZE (fld->element));
            }
 
-         infld = fold_build3 (BIT_FIELD_REF,
-                              lang_hooks.types.type_for_size
-                              (TREE_INT_CST_LOW (flen), 1),
-                              infld, flen, fpos);
+         infld = fold_build3 (BIT_FIELD_REF, type, infld, flen, fpos);
          BIT_FIELD_REF_UNSIGNED (infld) = 1;
 
          invar = size_binop (MINUS_EXPR, flp.field_pos, bpos);
@@ -3006,8 +3028,7 @@ sra_explode_bitfield_assignment (tree var, tree vpos, bool to_var,
            invar = size_binop (PLUS_EXPR, invar, flp.overlap_pos);
          invar = size_binop (PLUS_EXPR, invar, vpos);
 
-         invar = fold_build3 (BIT_FIELD_REF, TREE_TYPE (infld),
-                              var, flen, invar);
+         invar = fold_build3 (BIT_FIELD_REF, type, var, flen, invar);
          BIT_FIELD_REF_UNSIGNED (invar) = 1;
 
          if (to_var)
@@ -3140,7 +3161,7 @@ scalarize_use (struct sra_elt *elt, tree *expr_p, block_stmt_iterator *bsi,
       if (!elt->use_block_copy)
        {
          tree type = TREE_TYPE (bfexpr);
-         tree var = make_rename_temp (type, "SR"), tmp, st;
+         tree var = make_rename_temp (type, "SR"), tmp, st, vpos;
 
          GIMPLE_STMT_OPERAND (stmt, 0) = var;
          update = true;
@@ -3155,8 +3176,16 @@ scalarize_use (struct sra_elt *elt, tree *expr_p, block_stmt_iterator *bsi,
              var = tmp;
            }
 
+         /* If VAR is wider than BLEN bits, it is padded at the
+            most-significant end.  We want to set VPOS such that
+            <BIT_FIELD_REF VAR BLEN VPOS> would refer to the
+            least-significant BLEN bits of VAR.  */
+         if (BYTES_BIG_ENDIAN)
+           vpos = size_binop (MINUS_EXPR, TYPE_SIZE (type), blen);
+         else
+           vpos = bitsize_int (0);
          sra_explode_bitfield_assignment
-           (var, bitsize_int (0), false, &listafter, blen, bpos, elt);
+           (var, vpos, false, &listafter, blen, bpos, elt);
        }
       else
        sra_sync_for_bitfield_assignment
@@ -3192,7 +3221,7 @@ scalarize_use (struct sra_elt *elt, tree *expr_p, block_stmt_iterator *bsi,
       if (!elt->use_block_copy)
        {
          tree type = TREE_TYPE (bfexpr);
-         tree var;
+         tree var, vpos;
 
          if (!TYPE_UNSIGNED (type))
            type = unsigned_type_for (type);
@@ -3203,8 +3232,16 @@ scalarize_use (struct sra_elt *elt, tree *expr_p, block_stmt_iterator *bsi,
                                    (var, build_int_cst_wide (type, 0, 0)),
                                    &list);
 
+         /* If VAR is wider than BLEN bits, it is padded at the
+            most-significant end.  We want to set VPOS such that
+            <BIT_FIELD_REF VAR BLEN VPOS> would refer to the
+            least-significant BLEN bits of VAR.  */
+         if (BYTES_BIG_ENDIAN)
+           vpos = size_binop (MINUS_EXPR, TYPE_SIZE (type), blen);
+         else
+           vpos = bitsize_int (0);
          sra_explode_bitfield_assignment
-           (var, bitsize_int (0), true, &list, blen, bpos, elt);
+           (var, vpos, true, &list, blen, bpos, elt);
 
          GIMPLE_STMT_OPERAND (stmt, 1) = var;
          update = true;
@@ -3331,19 +3368,20 @@ static void
 scalarize_init (struct sra_elt *lhs_elt, tree rhs, block_stmt_iterator *bsi)
 {
   bool result = true;
-  tree list = NULL;
+  tree list = NULL, init_list = NULL;
 
   /* Generate initialization statements for all members extant in the RHS.  */
   if (rhs)
     {
       /* Unshare the expression just in case this is from a decl's initial.  */
       rhs = unshare_expr (rhs);
-      result = generate_element_init (lhs_elt, rhs, &list);
+      result = generate_element_init (lhs_elt, rhs, &init_list);
     }
 
   /* CONSTRUCTOR is defined such that any member not mentioned is assigned
      a zero value.  Initialize the rest of the instantiated elements.  */
   generate_element_zero (lhs_elt, &list);
+  append_to_statement_list (init_list, &list);
 
   if (!result)
     {