OSDN Git Service

2008-07-28 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / cp / cp-gimplify.c
index 4746103..8dda74d 100644 (file)
@@ -1,13 +1,14 @@
 /* C++-specific tree lowering bits; see also c-gimplify.c and tree-gimple.c.
 
-   Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
+   Free Software Foundation, Inc.
    Contributed by Jason Merrill <jason@redhat.com>
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 2, or (at your option) any later
+Software Foundation; either version 3, or (at your option) any later
 version.
 
 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
@@ -16,9 +17,8 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to the Free
-Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
-02110-1301, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
 #include "system.h"
@@ -28,7 +28,8 @@ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
 #include "cp-tree.h"
 #include "c-common.h"
 #include "toplev.h"
-#include "tree-gimple.h"
+#include "tree-iterator.h"
+#include "gimple.h"
 #include "hashtab.h"
 #include "pointer-set.h"
 #include "flags.h"
@@ -62,20 +63,14 @@ begin_bc_block (enum bc_t bc)
    If we saw a break (or continue) in the scope, append a LABEL_EXPR to
    body.  Otherwise, just forget the label.  */
 
-static tree
-finish_bc_block (enum bc_t bc, tree label, tree body)
+static gimple_seq
+finish_bc_block (enum bc_t bc, tree label, gimple_seq body)
 {
   gcc_assert (label == bc_label[bc]);
 
   if (TREE_USED (label))
     {
-      tree t, sl = NULL;
-
-      t = build1 (LABEL_EXPR, void_type_node, label);
-
-      append_to_statement_list (body, &sl);
-      append_to_statement_list (t, &sl);
-      body = sl;
+      gimple_seq_add_stmt (&body, gimple_build_label (label));
     }
 
   bc_label[bc] = TREE_CHAIN (label);
@@ -83,11 +78,11 @@ finish_bc_block (enum bc_t bc, tree label, tree body)
   return body;
 }
 
-/* Build a GOTO_EXPR to represent a break or continue statement.  BC
-   indicates which.  */
+/* Get the LABEL_EXPR to represent a break or continue statement
+   in the current block scope.  BC indicates which.  */
 
 static tree
-build_bc_goto (enum bc_t bc)
+get_bc_label (enum bc_t bc)
 {
   tree label = bc_label[bc];
 
@@ -103,7 +98,7 @@ build_bc_goto (enum bc_t bc)
 
   /* Mark the label used for finish_bc_block.  */
   TREE_USED (label) = 1;
-  return build1 (GOTO_EXPR, void_type_node, label);
+  return label;
 }
 
 /* Genericize a TRY_BLOCK.  */
@@ -114,13 +109,6 @@ genericize_try_block (tree *stmt_p)
   tree body = TRY_STMTS (*stmt_p);
   tree cleanup = TRY_HANDLERS (*stmt_p);
 
-  gimplify_stmt (&body);
-
-  if (CLEANUP_P (*stmt_p))
-    /* A cleanup is an expression, so it doesn't need to be genericized.  */;
-  else
-    gimplify_stmt (&cleanup);
-
   *stmt_p = build2 (TRY_CATCH_EXPR, void_type_node, body, cleanup);
 }
 
@@ -132,12 +120,28 @@ genericize_catch_block (tree *stmt_p)
   tree type = HANDLER_TYPE (*stmt_p);
   tree body = HANDLER_BODY (*stmt_p);
 
-  gimplify_stmt (&body);
-
   /* FIXME should the caught type go in TREE_TYPE?  */
   *stmt_p = build2 (CATCH_EXPR, void_type_node, type, body);
 }
 
+/* A terser interface for building a representation of an exception
+   specification.  */
+
+static tree
+build_gimple_eh_filter_tree (tree body, tree allowed, tree failure)
+{
+  tree t;
+
+  /* FIXME should the allowed types go in TREE_TYPE?  */
+  t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
+  append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
+
+  t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
+  append_to_statement_list (body, &TREE_OPERAND (t, 0));
+
+  return t;
+}
+
 /* Genericize an EH_SPEC_BLOCK by converting it to a
    TRY_CATCH_EXPR/EH_FILTER_EXPR pair.  */
 
@@ -146,12 +150,9 @@ genericize_eh_spec_block (tree *stmt_p)
 {
   tree body = EH_SPEC_STMTS (*stmt_p);
   tree allowed = EH_SPEC_RAISES (*stmt_p);
-  tree failure = build_call (call_unexpected_node,
-                            tree_cons (NULL_TREE, build_exc_ptr (),
-                                       NULL_TREE));
-  gimplify_stmt (&body);
+  tree failure = build_call_n (call_unexpected_node, 1, build_exc_ptr ());
 
-  *stmt_p = gimple_build_eh_filter (body, allowed, failure);
+  *stmt_p = build_gimple_eh_filter_tree (body, allowed, failure);
 }
 
 /* Genericize an IF_STMT by turning it into a COND_EXPR.  */
@@ -160,6 +161,7 @@ static void
 gimplify_if_stmt (tree *stmt_p)
 {
   tree stmt, cond, then_, else_;
+  location_t locus = EXPR_LOCATION (*stmt_p);
 
   stmt = *stmt_p;
   cond = IF_COND (stmt);
@@ -177,6 +179,8 @@ gimplify_if_stmt (tree *stmt_p)
     stmt = else_;
   else
     stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
+  if (CAN_HAVE_LOCATION_P (stmt) && !EXPR_HAS_LOCATION (stmt))
+    SET_EXPR_LOCATION (stmt, locus);
   *stmt_p = stmt;
 }
 
@@ -187,15 +191,20 @@ gimplify_if_stmt (tree *stmt_p)
    evaluated before the loop body as in while and for loops, or after the
    loop body as in do-while loops.  */
 
-static tree
+static gimple_seq
 gimplify_cp_loop (tree cond, tree body, tree incr, bool cond_is_first)
 {
-  tree top, entry, exit, cont_block, break_block, stmt_list, t;
+  gimple top, entry, stmt;
+  gimple_seq stmt_list, body_seq, incr_seq, exit_seq;
+  tree cont_block, break_block;
   location_t stmt_locus;
 
   stmt_locus = input_location;
-  stmt_list = NULL_TREE;
-  entry = NULL_TREE;
+  stmt_list = NULL;
+  body_seq = NULL;
+  incr_seq = NULL;
+  exit_seq = NULL;
+  entry = NULL;
 
   break_block = begin_bc_block (bc_break);
   cont_block = begin_bc_block (bc_continue);
@@ -203,12 +212,12 @@ gimplify_cp_loop (tree cond, tree body, tree incr, bool cond_is_first)
   /* If condition is zero don't generate a loop construct.  */
   if (cond && integer_zerop (cond))
     {
-      top = NULL_TREE;
-      exit = NULL_TREE;
+      top = NULL;
       if (cond_is_first)
        {
-         t = build_bc_goto (bc_break);
-         append_to_statement_list (t, &stmt_list);
+         stmt = gimple_build_goto (get_bc_label (bc_break));
+         gimple_set_location (stmt, stmt_locus);
+         gimple_seq_add_stmt (&stmt_list, stmt);
        }
     }
   else
@@ -217,44 +226,55 @@ gimplify_cp_loop (tree cond, tree body, tree incr, bool cond_is_first)
         back through the main gimplifier to lower it.  Given that we
         have to gimplify the loop body NOW so that we can resolve
         break/continue stmts, seems easier to just expand to gotos.  */
-      top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
+      top = gimple_build_label (create_artificial_label ());
 
       /* If we have an exit condition, then we build an IF with gotos either
         out of the loop, or to the top of it.  If there's no exit condition,
         then we just build a jump back to the top.  */
-      exit = build_and_jump (&LABEL_EXPR_LABEL (top));
       if (cond && !integer_nonzerop (cond))
        {
-         t = build_bc_goto (bc_break);
-         exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
-         gimplify_stmt (&exit);
+         if (cond != error_mark_node)
+           { 
+             gimplify_expr (&cond, &exit_seq, NULL, is_gimple_val, fb_rvalue);
+             stmt = gimple_build_cond (NE_EXPR, cond,
+                                       build_int_cst (TREE_TYPE (cond), 0),
+                                       gimple_label_label (top),
+                                       get_bc_label (bc_break));
+             gimple_seq_add_stmt (&exit_seq, stmt);
+           }
 
          if (cond_is_first)
            {
              if (incr)
                {
-                 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
-                 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
+                 entry = gimple_build_label (create_artificial_label ());
+                 stmt = gimple_build_goto (gimple_label_label (entry));
                }
              else
-               t = build_bc_goto (bc_continue);
-             append_to_statement_list (t, &stmt_list);
+               stmt = gimple_build_goto (get_bc_label (bc_continue));
+             gimple_set_location (stmt, stmt_locus);
+             gimple_seq_add_stmt (&stmt_list, stmt);
            }
        }
+      else
+       {
+         stmt = gimple_build_goto (gimple_label_label (top));
+         gimple_seq_add_stmt (&exit_seq, stmt);
+       }
     }
 
-  gimplify_stmt (&body);
-  gimplify_stmt (&incr);
+  gimplify_stmt (&body, &body_seq);
+  gimplify_stmt (&incr, &incr_seq);
 
-  body = finish_bc_block (bc_continue, cont_block, body);
+  body_seq = finish_bc_block (bc_continue, cont_block, body_seq);
 
-  append_to_statement_list (top, &stmt_list);
-  append_to_statement_list (body, &stmt_list);
-  append_to_statement_list (incr, &stmt_list);
-  append_to_statement_list (entry, &stmt_list);
-  append_to_statement_list (exit, &stmt_list);
+  gimple_seq_add_stmt (&stmt_list, top);
+  gimple_seq_add_seq (&stmt_list, body_seq);
+  gimple_seq_add_seq (&stmt_list, incr_seq);
+  gimple_seq_add_stmt (&stmt_list, entry);
+  gimple_seq_add_seq (&stmt_list, exit_seq);
 
-  annotate_all_with_locus (&stmt_list, stmt_locus);
+  annotate_all_with_location (stmt_list, stmt_locus);
 
   return finish_bc_block (bc_break, break_block, stmt_list);
 }
@@ -263,45 +283,52 @@ gimplify_cp_loop (tree cond, tree body, tree incr, bool cond_is_first)
    prequeue and hand off to gimplify_cp_loop.  */
 
 static void
-gimplify_for_stmt (tree *stmt_p, tree *pre_p)
+gimplify_for_stmt (tree *stmt_p, gimple_seq *pre_p)
 {
   tree stmt = *stmt_p;
 
   if (FOR_INIT_STMT (stmt))
     gimplify_and_add (FOR_INIT_STMT (stmt), pre_p);
 
-  *stmt_p = gimplify_cp_loop (FOR_COND (stmt), FOR_BODY (stmt),
-                             FOR_EXPR (stmt), 1);
+  gimple_seq_add_seq (pre_p,
+                     gimplify_cp_loop (FOR_COND (stmt), FOR_BODY (stmt),
+                                       FOR_EXPR (stmt), 1));
+  *stmt_p = NULL_TREE;
 }
 
 /* Gimplify a WHILE_STMT node.  */
 
 static void
-gimplify_while_stmt (tree *stmt_p)
+gimplify_while_stmt (tree *stmt_p, gimple_seq *pre_p)
 {
   tree stmt = *stmt_p;
-  *stmt_p = gimplify_cp_loop (WHILE_COND (stmt), WHILE_BODY (stmt),
-                             NULL_TREE, 1);
+  gimple_seq_add_seq (pre_p,
+                     gimplify_cp_loop (WHILE_COND (stmt), WHILE_BODY (stmt),
+                                       NULL_TREE, 1));
+  *stmt_p = NULL_TREE;
 }
 
 /* Gimplify a DO_STMT node.  */
 
 static void
-gimplify_do_stmt (tree *stmt_p)
+gimplify_do_stmt (tree *stmt_p, gimple_seq *pre_p)
 {
   tree stmt = *stmt_p;
-  *stmt_p = gimplify_cp_loop (DO_COND (stmt), DO_BODY (stmt),
-                             NULL_TREE, 0);
+  gimple_seq_add_seq (pre_p,
+                     gimplify_cp_loop (DO_COND (stmt), DO_BODY (stmt),
+                                       NULL_TREE, 0));
+  *stmt_p = NULL_TREE;
 }
 
 /* Genericize a SWITCH_STMT by turning it into a SWITCH_EXPR.  */
 
 static void
-gimplify_switch_stmt (tree *stmt_p)
+gimplify_switch_stmt (tree *stmt_p, gimple_seq *pre_p)
 {
   tree stmt = *stmt_p;
-  tree break_block, body;
+  tree break_block, body, t;
   location_t stmt_locus = input_location;
+  gimple_seq seq = NULL;
 
   break_block = begin_bc_block (bc_break);
 
@@ -309,24 +336,28 @@ gimplify_switch_stmt (tree *stmt_p)
   if (!body)
     body = build_empty_stmt ();
 
-  *stmt_p = build3 (SWITCH_EXPR, SWITCH_STMT_TYPE (stmt),
-                   SWITCH_STMT_COND (stmt), body, NULL_TREE);
-  SET_EXPR_LOCATION (*stmt_p, stmt_locus);
-  gimplify_stmt (stmt_p);
+  t = build3 (SWITCH_EXPR, SWITCH_STMT_TYPE (stmt),
+             SWITCH_STMT_COND (stmt), body, NULL_TREE);
+  SET_EXPR_LOCATION (t, stmt_locus);
+  gimplify_and_add (t, &seq);
 
-  *stmt_p = finish_bc_block (bc_break, break_block, *stmt_p);
+  seq = finish_bc_block (bc_break, break_block, seq);
+  gimple_seq_add_seq (pre_p, seq);
+  *stmt_p = NULL_TREE;
 }
 
 /* Hook into the middle of gimplifying an OMP_FOR node.  This is required
    in order to properly gimplify CONTINUE statements.  Here we merely
    manage the continue stack; the rest of the job is performed by the
-   regular gimplifier.  */ 
+   regular gimplifier.  */
 
 static enum gimplify_status
-cp_gimplify_omp_for (tree *expr_p)
+cp_gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
 {
   tree for_stmt = *expr_p;
   tree cont_block;
+  gimple stmt;
+  gimple_seq seq = NULL;
 
   /* Protect ourselves from recursion.  */
   if (OMP_FOR_GIMPLIFYING_P (for_stmt))
@@ -338,10 +369,15 @@ cp_gimplify_omp_for (tree *expr_p)
      statement expressions within the INIT, COND, or INCR expressions.  */
   cont_block = begin_bc_block (bc_continue);
 
-  gimplify_stmt (expr_p);
+  gimplify_and_add (for_stmt, &seq);
+  stmt = gimple_seq_last_stmt (seq);
+  if (gimple_code (stmt) == GIMPLE_OMP_FOR)
+    gimple_omp_set_body (stmt, finish_bc_block (bc_continue, cont_block,
+                                               gimple_omp_body (stmt)));
+  else
+    seq = finish_bc_block (bc_continue, cont_block, seq);
+  gimple_seq_add_seq (pre_p, seq);
 
-  OMP_FOR_BODY (for_stmt)
-    = finish_bc_block (bc_continue, cont_block, OMP_FOR_BODY (for_stmt));
   OMP_FOR_GIMPLIFYING_P (for_stmt) = 0;
 
   return GS_ALL_DONE;
@@ -363,16 +399,16 @@ gimplify_expr_stmt (tree *stmt_p)
      In this case we will not want to emit the gimplified statement.
      However, we may still want to emit a warning, so we do that before
      gimplification.  */
-  if (stmt && (extra_warnings || warn_unused_value))
+  if (stmt && warn_unused_value)
     {
       if (!TREE_SIDE_EFFECTS (stmt))
        {
          if (!IS_EMPTY_STMT (stmt)
              && !VOID_TYPE_P (TREE_TYPE (stmt))
              && !TREE_NO_WARNING (stmt))
-           warning (OPT_Wextra, "statement with no effect");
+           warning (OPT_Wunused_value, "statement with no effect");
        }
-      else if (warn_unused_value)
+      else
        warn_if_unused_value (stmt, input_location);
     }
 
@@ -385,71 +421,89 @@ gimplify_expr_stmt (tree *stmt_p)
 /* Gimplify initialization from an AGGR_INIT_EXPR.  */
 
 static void
-cp_gimplify_init_expr (tree *expr_p, tree *pre_p, tree *post_p)
+cp_gimplify_init_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
 {
   tree from = TREE_OPERAND (*expr_p, 1);
   tree to = TREE_OPERAND (*expr_p, 0);
-  tree sub;
+  tree t;
+  tree slot = NULL_TREE;
 
-  /* If we are initializing something from a TARGET_EXPR, strip the
-     TARGET_EXPR and initialize it directly.  */
   /* What about code that pulls out the temp and uses it elsewhere?  I
      think that such code never uses the TARGET_EXPR as an initializer.  If
      I'm wrong, we'll abort because the temp won't have any RTL.  In that
      case, I guess we'll need to replace references somehow.  */
   if (TREE_CODE (from) == TARGET_EXPR)
-    from = TARGET_EXPR_INITIAL (from);
-  if (TREE_CODE (from) == CLEANUP_POINT_EXPR)
-    from = TREE_OPERAND (from, 0);
+    {
+      slot = TARGET_EXPR_SLOT (from);
+      from = TARGET_EXPR_INITIAL (from);
+    }
 
-  /* Look through any COMPOUND_EXPRs.  */
-  sub = expr_last (from);
+  /* Look through any COMPOUND_EXPRs, since build_compound_expr pushes them
+     inside the TARGET_EXPR.  */
+  for (t = from; t; )
+    {
+      tree sub = TREE_CODE (t) == COMPOUND_EXPR ? TREE_OPERAND (t, 0) : t;
 
-  /* If we are initializing from an AGGR_INIT_EXPR, drop the INIT_EXPR and
-     replace the slot operand with our target.
+      /* If we are initializing from an AGGR_INIT_EXPR, drop the INIT_EXPR and
+        replace the slot operand with our target.
 
-     Should we add a target parm to gimplify_expr instead?  No, as in this
-     case we want to replace the INIT_EXPR.  */
-  if (TREE_CODE (sub) == AGGR_INIT_EXPR)
-    {
-      gimplify_expr (&to, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
-      TREE_OPERAND (sub, 2) = to;
-      *expr_p = from;
-
-      /* The initialization is now a side-effect, so the container can
-        become void.  */
-      if (from != sub)
-       TREE_TYPE (from) = void_type_node;
+        Should we add a target parm to gimplify_expr instead?  No, as in this
+        case we want to replace the INIT_EXPR.  */
+      if (TREE_CODE (sub) == AGGR_INIT_EXPR)
+       {
+         gimplify_expr (&to, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
+         AGGR_INIT_EXPR_SLOT (sub) = to;
+         *expr_p = from;
+
+         /* The initialization is now a side-effect, so the container can
+            become void.  */
+         if (from != sub)
+           TREE_TYPE (from) = void_type_node;
+       }
+      else if (TREE_CODE (sub) == INIT_EXPR
+              && TREE_OPERAND (sub, 0) == slot)
+       {
+         /* An INIT_EXPR under TARGET_EXPR created by build_value_init,
+            will be followed by an AGGR_INIT_EXPR.  */
+         gimplify_expr (&to, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
+         TREE_OPERAND (sub, 0) = to;
+       }
+
+      if (t == sub)
+       break;
+      else
+       t = TREE_OPERAND (t, 1);
     }
+
 }
 
 /* Gimplify a MUST_NOT_THROW_EXPR.  */
 
-static void
-gimplify_must_not_throw_expr (tree *expr_p, tree *pre_p)
+static enum gimplify_status
+gimplify_must_not_throw_expr (tree *expr_p, gimple_seq *pre_p)
 {
   tree stmt = *expr_p;
   tree temp = voidify_wrapper_expr (stmt, NULL);
   tree body = TREE_OPERAND (stmt, 0);
 
-  gimplify_stmt (&body);
-
-  stmt = gimple_build_eh_filter (body, NULL_TREE,
-                                build_call (terminate_node, NULL_TREE));
+  stmt = build_gimple_eh_filter_tree (body, NULL_TREE,
+                                     build_call_n (terminate_node, 0));
 
+  gimplify_and_add (stmt, pre_p);
   if (temp)
     {
-      append_to_statement_list (stmt, pre_p);
       *expr_p = temp;
+      return GS_OK;
     }
-  else
-    *expr_p = stmt;
+
+  *expr_p = NULL;
+  return GS_ALL_DONE;
 }
 
 /* Do C++-specific gimplification.  Args are as for gimplify_expr.  */
 
 int
-cp_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p)
+cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
 {
   int saved_stmts_are_full_exprs_p = 0;
   enum tree_code code = TREE_CODE (*expr_p);
@@ -475,15 +529,14 @@ cp_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p)
       break;
 
     case THROW_EXPR:
-      /* FIXME communicate throw type to backend, probably by moving
+      /* FIXME communicate throw type to back end, probably by moving
         THROW_EXPR into ../tree.def.  */
       *expr_p = TREE_OPERAND (*expr_p, 0);
       ret = GS_OK;
       break;
 
     case MUST_NOT_THROW_EXPR:
-      gimplify_must_not_throw_expr (expr_p, pre_p);
-      ret = GS_OK;
+      ret = gimplify_must_not_throw_expr (expr_p, pre_p);
       break;
 
       /* We used to do this for MODIFY_EXPR as well, but that's unsafe; the
@@ -523,7 +576,7 @@ cp_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p)
     case USING_STMT:
       /* Just ignore for now.  Eventually we will want to pass this on to
         the debugger.  */
-      *expr_p = build_empty_stmt ();
+      *expr_p = NULL;
       ret = GS_ALL_DONE;
       break;
 
@@ -534,35 +587,37 @@ cp_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p)
 
     case FOR_STMT:
       gimplify_for_stmt (expr_p, pre_p);
-      ret = GS_ALL_DONE;
+      ret = GS_OK;
       break;
 
     case WHILE_STMT:
-      gimplify_while_stmt (expr_p);
-      ret = GS_ALL_DONE;
+      gimplify_while_stmt (expr_p, pre_p);
+      ret = GS_OK;
       break;
 
     case DO_STMT:
-      gimplify_do_stmt (expr_p);
-      ret = GS_ALL_DONE;
+      gimplify_do_stmt (expr_p, pre_p);
+      ret = GS_OK;
       break;
 
     case SWITCH_STMT:
-      gimplify_switch_stmt (expr_p);
-      ret = GS_ALL_DONE;
+      gimplify_switch_stmt (expr_p, pre_p);
+      ret = GS_OK;
       break;
 
     case OMP_FOR:
-      ret = cp_gimplify_omp_for (expr_p);
+      ret = cp_gimplify_omp_for (expr_p, pre_p);
       break;
 
     case CONTINUE_STMT:
-      *expr_p = build_bc_goto (bc_continue);
+      gimple_seq_add_stmt (pre_p, gimple_build_goto (get_bc_label (bc_continue)));
+      *expr_p = NULL_TREE;
       ret = GS_ALL_DONE;
       break;
 
     case BREAK_STMT:
-      *expr_p = build_bc_goto (bc_break);
+      gimple_seq_add_stmt (pre_p, gimple_build_goto (get_bc_label (bc_break)));
+      *expr_p = NULL_TREE;
       ret = GS_ALL_DONE;
       break;
 
@@ -595,12 +650,30 @@ cp_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p)
 }
 
 static inline bool
-is_invisiref_parm (tree t)
+is_invisiref_parm (const_tree t)
 {
   return ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
          && DECL_BY_REFERENCE (t));
 }
 
+/* Return true if the uid in both int tree maps are equal.  */
+
+int
+cxx_int_tree_map_eq (const void *va, const void *vb)
+{
+  const struct cxx_int_tree_map *a = (const struct cxx_int_tree_map *) va;
+  const struct cxx_int_tree_map *b = (const struct cxx_int_tree_map *) vb;
+  return (a->uid == b->uid);
+}
+
+/* Hash a UID in a cxx_int_tree_map.  */
+
+unsigned int
+cxx_int_tree_map_hash (const void *item)
+{
+  return ((const struct cxx_int_tree_map *)item)->uid;
+}
+
 /* Perform any pre-gimplification lowering of C++ front end trees to
    GENERIC.  */
 
@@ -613,13 +686,32 @@ cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
   if (is_invisiref_parm (stmt)
       /* Don't dereference parms in a thunk, pass the references through. */
       && !(DECL_THUNK_P (current_function_decl)
-           && TREE_CODE (stmt) == PARM_DECL))
+          && TREE_CODE (stmt) == PARM_DECL))
     {
       *stmt_p = convert_from_reference (stmt);
       *walk_subtrees = 0;
       return NULL;
     }
 
+  /* Map block scope extern declarations to visible declarations with the
+     same name and type in outer scopes if any.  */
+  if (cp_function_chain->extern_decl_map
+      && (TREE_CODE (stmt) == FUNCTION_DECL || TREE_CODE (stmt) == VAR_DECL)
+      && DECL_EXTERNAL (stmt))
+    {
+      struct cxx_int_tree_map *h, in;
+      in.uid = DECL_UID (stmt);
+      h = (struct cxx_int_tree_map *)
+         htab_find_with_hash (cp_function_chain->extern_decl_map,
+                              &in, in.uid);
+      if (h)
+       {
+         *stmt_p = h->to;
+         *walk_subtrees = 0;
+         return NULL;
+       }
+    }
+
   /* Other than invisiref parms, don't walk the same tree twice.  */
   if (pointer_set_contains (p_set, stmt))
     {
@@ -638,6 +730,34 @@ cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
           && is_invisiref_parm (TREE_OPERAND (stmt, 0)))
     /* Don't dereference an invisiref RESULT_DECL inside a RETURN_EXPR.  */
     *walk_subtrees = 0;
+  else if (TREE_CODE (stmt) == OMP_CLAUSE)
+    switch (OMP_CLAUSE_CODE (stmt))
+      {
+      case OMP_CLAUSE_LASTPRIVATE:
+       /* Don't dereference an invisiref in OpenMP clauses.  */
+       if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
+         {
+           *walk_subtrees = 0;
+           if (OMP_CLAUSE_LASTPRIVATE_STMT (stmt))
+             cp_walk_tree (&OMP_CLAUSE_LASTPRIVATE_STMT (stmt),
+                           cp_genericize_r, p_set, NULL);
+         }
+       break;
+      case OMP_CLAUSE_PRIVATE:
+      case OMP_CLAUSE_SHARED:
+      case OMP_CLAUSE_FIRSTPRIVATE:
+      case OMP_CLAUSE_COPYIN:
+      case OMP_CLAUSE_COPYPRIVATE:
+       /* Don't dereference an invisiref in OpenMP clauses.  */
+       if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
+         *walk_subtrees = 0;
+       break;
+      case OMP_CLAUSE_REDUCTION:
+       gcc_assert (!is_invisiref_parm (OMP_CLAUSE_DECL (stmt)));
+       break;
+      default:
+       break;
+      }
   else if (IS_TYPE_OR_DECL_P (stmt))
     *walk_subtrees = 0;
 
@@ -695,7 +815,7 @@ cp_genericize (tree fndecl)
   /* We do want to see every occurrence of the parms, so we can't just use
      walk_tree's hash functionality.  */
   p_set = pointer_set_create ();
-  walk_tree (&DECL_SAVED_TREE (fndecl), cp_genericize_r, p_set, NULL);
+  cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_genericize_r, p_set, NULL);
   pointer_set_destroy (p_set);
 
   /* Do everything else.  */
@@ -712,12 +832,17 @@ cp_genericize (tree fndecl)
 static tree
 cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
 {
-  tree defparm, parm;
-  int i;
+  tree defparm, parm, t;
+  int i = 0;
+  int nargs;
+  tree *argarray;
 
   if (fn == NULL)
     return NULL;
 
+  nargs = list_length (DECL_ARGUMENTS (fn));
+  argarray = (tree *) alloca (nargs * sizeof (tree));
+
   defparm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
   if (arg2)
     defparm = TREE_CHAIN (defparm);
@@ -727,7 +852,7 @@ cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
       tree inner_type = TREE_TYPE (arg1);
       tree start1, end1, p1;
       tree start2 = NULL, p2 = NULL;
-      tree ret = NULL, lab, t;
+      tree ret = NULL, lab;
 
       start1 = arg1;
       start2 = arg2;
@@ -746,17 +871,16 @@ cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
        start2 = build_fold_addr_expr (start2);
 
       end1 = TYPE_SIZE_UNIT (TREE_TYPE (arg1));
-      end1 = fold_convert (TREE_TYPE (start1), end1);
-      end1 = build2 (PLUS_EXPR, TREE_TYPE (start1), start1, end1);
+      end1 = build2 (POINTER_PLUS_EXPR, TREE_TYPE (start1), start1, end1);
 
       p1 = create_tmp_var (TREE_TYPE (start1), NULL);
-      t = build2 (MODIFY_EXPR, void_type_node, p1, start1);
+      t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, start1);
       append_to_statement_list (t, &ret);
 
       if (arg2)
        {
          p2 = create_tmp_var (TREE_TYPE (start2), NULL);
-         t = build2 (MODIFY_EXPR, void_type_node, p2, start2);
+         t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, start2);
          append_to_statement_list (t, &ret);
        }
 
@@ -764,28 +888,29 @@ cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
       t = build1 (LABEL_EXPR, void_type_node, lab);
       append_to_statement_list (t, &ret);
 
-      t = tree_cons (NULL, p1, NULL);
+      argarray[i++] = p1;
       if (arg2)
-       t = tree_cons (NULL, p2, t);
+       argarray[i++] = p2;
       /* Handle default arguments.  */
-      i = 1 + (arg2 != NULL);
-      for (parm = defparm; parm != void_list_node; parm = TREE_CHAIN (parm))
-       t = tree_cons (NULL, convert_default_arg (TREE_VALUE (parm),
-                                                 TREE_PURPOSE (parm),
-                                                 fn, i++), t);
-      t = build_call (fn, nreverse (t));
+      for (parm = defparm; parm && parm != void_list_node;
+          parm = TREE_CHAIN (parm), i++)
+       argarray[i] = convert_default_arg (TREE_VALUE (parm),
+                                          TREE_PURPOSE (parm), fn, i);
+      t = build_call_a (fn, i, argarray);
+      t = fold_convert (void_type_node, t);
+      t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
       append_to_statement_list (t, &ret);
 
-      t = fold_convert (TREE_TYPE (p1), TYPE_SIZE_UNIT (inner_type));
-      t = build2 (PLUS_EXPR, TREE_TYPE (p1), p1, t);
-      t = build2 (MODIFY_EXPR, void_type_node, p1, t);
+      t = TYPE_SIZE_UNIT (inner_type);
+      t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (p1), p1, t);
+      t = build2 (MODIFY_EXPR, TREE_TYPE (p1), p1, t);
       append_to_statement_list (t, &ret);
 
       if (arg2)
        {
-         t = fold_convert (TREE_TYPE (p2), TYPE_SIZE_UNIT (inner_type));
-         t = build2 (PLUS_EXPR, TREE_TYPE (p2), p2, t);
-         t = build2 (MODIFY_EXPR, void_type_node, p2, t);
+         t = TYPE_SIZE_UNIT (inner_type);
+         t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (p2), p2, t);
+         t = build2 (MODIFY_EXPR, TREE_TYPE (p2), p2, t);
          append_to_statement_list (t, &ret);
        }
 
@@ -797,16 +922,18 @@ cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
     }
   else
     {
-      tree t = tree_cons (NULL, build_fold_addr_expr (arg1), NULL);
+      argarray[i++] = build_fold_addr_expr (arg1);
       if (arg2)
-       t = tree_cons (NULL, build_fold_addr_expr (arg2), t);
+       argarray[i++] = build_fold_addr_expr (arg2);
       /* Handle default arguments.  */
-      i = 1 + (arg2 != NULL);
-      for (parm = defparm; parm != void_list_node; parm = TREE_CHAIN (parm))
-       t = tree_cons (NULL, convert_default_arg (TREE_VALUE (parm),
-                                                 TREE_PURPOSE (parm),
-                                                 fn, i++), t);
-      return build_call (fn, nreverse (t));
+      for (parm = defparm; parm && parm != void_list_node;
+          parm = TREE_CHAIN (parm), i++)
+       argarray[i] = convert_default_arg (TREE_VALUE (parm),
+                                          TREE_PURPOSE (parm),
+                                          fn, i);
+      t = build_call_a (fn, i, argarray);
+      t = fold_convert (void_type_node, t);
+      return fold_build_cleanup_point_expr (TREE_TYPE (t), t);
     }
 }
 
@@ -814,7 +941,8 @@ cxx_omp_clause_apply_fn (tree fn, tree arg1, tree arg2)
    NULL if there's nothing to do.  */
 
 tree
-cxx_omp_clause_default_ctor (tree clause, tree decl)
+cxx_omp_clause_default_ctor (tree clause, tree decl,
+                            tree outer ATTRIBUTE_UNUSED)
 {
   tree info = CP_OMP_CLAUSE_INFO (clause);
   tree ret = NULL;
@@ -836,7 +964,7 @@ cxx_omp_clause_copy_ctor (tree clause, tree dst, tree src)
   if (info)
     ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 0), dst, src);
   if (ret == NULL)
-    ret = build2 (MODIFY_EXPR, void_type_node, dst, src);
+    ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
 
   return ret;
 }
@@ -852,7 +980,7 @@ cxx_omp_clause_assign_op (tree clause, tree dst, tree src)
   if (info)
     ret = cxx_omp_clause_apply_fn (TREE_VEC_ELT (info, 2), dst, src);
   if (ret == NULL)
-    ret = build2 (MODIFY_EXPR, void_type_node, dst, src);
+    ret = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
 
   return ret;
 }
@@ -875,7 +1003,104 @@ cxx_omp_clause_dtor (tree clause, tree decl)
    than the DECL itself.  */
 
 bool
-cxx_omp_privatize_by_reference (tree decl)
+cxx_omp_privatize_by_reference (const_tree decl)
 {
-  return TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl);
+  return is_invisiref_parm (decl);
+}
+
+/* True if OpenMP sharing attribute of DECL is predetermined.  */
+
+enum omp_clause_default_kind
+cxx_omp_predetermined_sharing (tree decl)
+{
+  tree type;
+
+  /* Static data members are predetermined as shared.  */
+  if (TREE_STATIC (decl))
+    {
+      tree ctx = CP_DECL_CONTEXT (decl);
+      if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
+       return OMP_CLAUSE_DEFAULT_SHARED;
+    }
+
+  type = TREE_TYPE (decl);
+  if (TREE_CODE (type) == REFERENCE_TYPE)
+    {
+      if (!is_invisiref_parm (decl))
+       return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
+      type = TREE_TYPE (type);
+
+      if (TREE_CODE (decl) == RESULT_DECL && DECL_NAME (decl))
+       {
+         /* NVR doesn't preserve const qualification of the
+            variable's type.  */
+         tree outer = outer_curly_brace_block (current_function_decl);
+         tree var;
+
+         if (outer)
+           for (var = BLOCK_VARS (outer); var; var = TREE_CHAIN (var))
+             if (DECL_NAME (decl) == DECL_NAME (var)
+                 && (TYPE_MAIN_VARIANT (type)
+                     == TYPE_MAIN_VARIANT (TREE_TYPE (var))))
+               {
+                 if (TYPE_READONLY (TREE_TYPE (var)))
+                   type = TREE_TYPE (var);
+                 break;
+               }
+       }
+    }
+
+  if (type == error_mark_node)
+    return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
+
+  /* Variables with const-qualified type having no mutable member
+     are predetermined shared.  */
+  if (TYPE_READONLY (type) && !cp_has_mutable_p (type))
+    return OMP_CLAUSE_DEFAULT_SHARED;
+
+  return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
+}
+
+/* Finalize an implicitly determined clause.  */
+
+void
+cxx_omp_finish_clause (tree c)
+{
+  tree decl, inner_type;
+  bool make_shared = false;
+
+  if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
+    return;
+
+  decl = OMP_CLAUSE_DECL (c);
+  decl = require_complete_type (decl);
+  inner_type = TREE_TYPE (decl);
+  if (decl == error_mark_node)
+    make_shared = true;
+  else if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
+    {
+      if (is_invisiref_parm (decl))
+       inner_type = TREE_TYPE (inner_type);
+      else
+       {
+         error ("%qE implicitly determined as %<firstprivate%> has reference type",
+                decl);
+         make_shared = true;
+       }
+    }
+
+  /* We're interested in the base element, not arrays.  */
+  while (TREE_CODE (inner_type) == ARRAY_TYPE)
+    inner_type = TREE_TYPE (inner_type);
+
+  /* Check for special function availability by building a call to one.
+     Save the results, because later we won't be in the right context
+     for making these queries.  */
+  if (!make_shared
+      && CLASS_TYPE_P (inner_type)
+      && cxx_omp_create_clause_info (c, inner_type, false, true, false))
+    make_shared = true;
+
+  if (make_shared)
+    OMP_CLAUSE_CODE (c) = OMP_CLAUSE_SHARED;
 }