i386.c (legitimize_tls_address): Generate tls_initial_exec_64_sun only when !TARGET_X32.
[gcc.git] / gcc / gimple-low.c
index 381068abb269046563f128e20232c4d626cd3f04..04d4275f75b7ec47676d1f85465c727541a10254 100644 (file)
@@ -1,6 +1,6 @@
 /* GIMPLE lowering pass.  Converts High GIMPLE into Low GIMPLE.
 
-   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
 
 This file is part of GCC.
@@ -30,7 +30,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-flow.h"
 #include "flags.h"
 #include "function.h"
-#include "toplev.h"
+#include "diagnostic-core.h"
 #include "tree-pass.h"
 
 /* The differences between High GIMPLE and Low GIMPLE are the
@@ -112,10 +112,6 @@ lower_function_body (void)
   i = gsi_start (lowered_body);
   lower_gimple_bind (&i, &data);
 
-  /* Once the old body has been lowered, replace it with the new
-     lowered sequence.  */
-  gimple_set_body (current_function_decl, lowered_body);
-
   i = gsi_last (lowered_body);
 
   /* If the function falls off the end, we need a null return statement.
@@ -148,11 +144,6 @@ lower_function_body (void)
 
       x = gimple_build_label (t.label);
       gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
-
-      /* Remove the line number from the representative return statement.
-        It now fills in for many such returns.  Failure to remove this
-        will result in incorrect results for coverage analysis.  */
-      gimple_set_location (t.stmt, UNKNOWN_LOCATION);
       gsi_insert_after (&i, t.stmt, GSI_CONTINUE_LINKING);
     }
 
@@ -174,7 +165,7 @@ lower_function_body (void)
         and insert.  */
       disp_var = create_tmp_var (ptr_type_node, "setjmpvar");
       arg = build_addr (disp_label, current_function_decl);
-      t = implicit_built_in_decls[BUILT_IN_SETJMP_DISPATCHER];
+      t = builtin_decl_implicit (BUILT_IN_SETJMP_DISPATCHER);
       x = gimple_build_call (t, 1, arg);
       gimple_call_set_lhs (x, disp_var);
 
@@ -184,6 +175,10 @@ lower_function_body (void)
       gsi_insert_after (&i, x, GSI_CONTINUE_LINKING);
     }
 
+  /* Once the old body has been lowered, replace it with the new
+     lowered sequence.  */
+  gimple_set_body (current_function_decl, lowered_body);
+
   gcc_assert (data.block == DECL_INITIAL (current_function_decl));
   BLOCK_SUBBLOCKS (data.block)
     = blocks_nreverse (BLOCK_SUBBLOCKS (data.block));
@@ -208,30 +203,33 @@ struct gimple_opt_pass pass_lower_cf =
   PROP_gimple_lcf,                     /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */
-  TODO_dump_func                       /* todo_flags_finish */
+  0                                    /* todo_flags_finish */
  }
 };
 
 
+
 /* Verify if the type of the argument matches that of the function
    declaration.  If we cannot verify this or there is a mismatch,
    return false.  */
 
-bool
-gimple_check_call_args (gimple stmt)
+static bool
+gimple_check_call_args (gimple stmt, tree fndecl)
 {
-  tree fndecl, parms, p;
+  tree parms, p;
   unsigned int i, nargs;
 
+  /* Calls to internal functions always match their signature.  */
+  if (gimple_call_internal_p (stmt))
+    return true;
+
   nargs = gimple_call_num_args (stmt);
 
   /* Get argument types for verification.  */
-  fndecl = gimple_call_fndecl (stmt);
-  parms = NULL_TREE;
   if (fndecl)
     parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
-  else if (POINTER_TYPE_P (TREE_TYPE (gimple_call_fn (stmt))))
-    parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (gimple_call_fn (stmt))));
+  else
+    parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
 
   /* Verify if the type of the argument matches that of the function
      declaration.  If we cannot verify this or there is a mismatch,
@@ -240,17 +238,19 @@ gimple_check_call_args (gimple stmt)
     {
       for (i = 0, p = DECL_ARGUMENTS (fndecl);
           i < nargs;
-          i++, p = TREE_CHAIN (p))
+          i++, p = DECL_CHAIN (p))
        {
+         tree arg;
          /* We cannot distinguish a varargs function from the case
             of excess parameters, still deferring the inlining decision
             to the callee is possible.  */
          if (!p)
            break;
+         arg = gimple_call_arg (stmt, i);
          if (p == error_mark_node
-             || gimple_call_arg (stmt, i) == error_mark_node
-             || !fold_convertible_p (DECL_ARG_TYPE (p),
-                                     gimple_call_arg (stmt, i)))
+             || arg == error_mark_node
+             || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
+                 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
             return false;
        }
     }
@@ -258,15 +258,17 @@ gimple_check_call_args (gimple stmt)
     {
       for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
        {
+         tree arg;
          /* If this is a varargs function defer inlining decision
             to callee.  */
          if (!p)
            break;
+         arg = gimple_call_arg (stmt, i);
          if (TREE_VALUE (p) == error_mark_node
-             || gimple_call_arg (stmt, i) == error_mark_node
+             || arg == error_mark_node
              || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
-             || !fold_convertible_p (TREE_VALUE (p),
-                                     gimple_call_arg (stmt, i)))
+             || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
+                 && !fold_convertible_p (TREE_VALUE (p), arg)))
             return false;
        }
     }
@@ -278,17 +280,36 @@ gimple_check_call_args (gimple stmt)
   return true;
 }
 
+/* Verify if the type of the argument and lhs of CALL_STMT matches
+   that of the function declaration CALLEE.
+   If we cannot verify this or there is a mismatch, return false.  */
+
+bool
+gimple_check_call_matching_types (gimple call_stmt, tree callee)
+{
+  tree lhs;
+
+  if ((DECL_RESULT (callee)
+       && !DECL_BY_REFERENCE (DECL_RESULT (callee))
+       && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
+       && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
+                                      TREE_TYPE (lhs))
+       && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
+      || !gimple_check_call_args (call_stmt, callee))
+    return false;
+  return true;
+}
 
 /* Lower sequence SEQ.  Unlike gimplification the statements are not relowered
    when they are changed -- if this has to be done, the lowering routine must
    do it explicitly.  DATA is passed through the recursion.  */
 
 static void
-lower_sequence (gimple_seq seq, struct lower_data *data)
+lower_sequence (gimple_seq *seq, struct lower_data *data)
 {
   gimple_stmt_iterator gsi;
 
-  for (gsi = gsi_start (seq); !gsi_end_p (gsi); )
+  for (gsi = gsi_start (*seq); !gsi_end_p (gsi); )
     lower_stmt (&gsi, data);
 }
 
@@ -303,11 +324,10 @@ lower_omp_directive (gimple_stmt_iterator *gsi, struct lower_data *data)
 
   stmt = gsi_stmt (*gsi);
 
-  lower_sequence (gimple_omp_body (stmt), data);
-  gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
-  gsi_insert_seq_before (gsi, gimple_omp_body (stmt), GSI_SAME_STMT);
+  lower_sequence (gimple_omp_body_ptr (stmt), data);
+  gsi_insert_seq_after (gsi, gimple_omp_body (stmt), GSI_CONTINUE_LINKING);
   gimple_omp_set_body (stmt, NULL);
-  gsi_remove (gsi, false);
+  gsi_next (gsi);
 }
 
 
@@ -355,10 +375,10 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
     case GIMPLE_TRY:
       {
        bool try_cannot_fallthru;
-       lower_sequence (gimple_try_eval (stmt), data);
+       lower_sequence (gimple_try_eval_ptr (stmt), data);
        try_cannot_fallthru = data->cannot_fallthru;
        data->cannot_fallthru = false;
-       lower_sequence (gimple_try_cleanup (stmt), data);
+       lower_sequence (gimple_try_cleanup_ptr (stmt), data);
        /* See gimple_stmt_may_fallthru for the rationale.  */
        if (gimple_try_kind (stmt) == GIMPLE_TRY_FINALLY)
          {
@@ -371,12 +391,17 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
 
     case GIMPLE_CATCH:
       data->cannot_fallthru = false;
-      lower_sequence (gimple_catch_handler (stmt), data);
+      lower_sequence (gimple_catch_handler_ptr (stmt), data);
       break;
 
     case GIMPLE_EH_FILTER:
       data->cannot_fallthru = false;
-      lower_sequence (gimple_eh_filter_failure (stmt), data);
+      lower_sequence (gimple_eh_filter_failure_ptr (stmt), data);
+      break;
+
+    case GIMPLE_EH_ELSE:
+      lower_sequence (gimple_eh_else_n_body_ptr (stmt), data);
+      lower_sequence (gimple_eh_else_e_body_ptr (stmt), data);
       break;
 
     case GIMPLE_NOP:
@@ -429,6 +454,10 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
       data->cannot_fallthru = false;
       return;
 
+    case GIMPLE_TRANSACTION:
+      lower_sequence (gimple_transaction_body_ptr (stmt), data);
+      break;
+
     default:
       gcc_unreachable ();
     }
@@ -475,7 +504,7 @@ lower_gimple_bind (gimple_stmt_iterator *gsi, struct lower_data *data)
     }
 
   record_vars (gimple_bind_vars (stmt));
-  lower_sequence (gimple_bind_body (stmt), data);
+  lower_sequence (gimple_bind_body_ptr (stmt), data);
 
   if (new_block)
     {
@@ -555,7 +584,7 @@ gimple_try_catch_may_fallthru (gimple stmt)
   if (gimple_seq_may_fallthru (gimple_try_eval (stmt)))
     return true;
 
-  i = gsi_start (gimple_try_cleanup (stmt));
+  i = gsi_start (*gimple_try_cleanup_ptr (stmt));
   switch (gimple_code (gsi_stmt (i)))
     {
     case GIMPLE_CATCH:
@@ -710,6 +739,10 @@ gimple_stmt_may_fallthru (gimple stmt)
       return (gimple_seq_may_fallthru (gimple_try_eval (stmt))
              && gimple_seq_may_fallthru (gimple_try_cleanup (stmt)));
 
+    case GIMPLE_EH_ELSE:
+      return (gimple_seq_may_fallthru (gimple_eh_else_n_body (stmt))
+             || gimple_seq_may_fallthru (gimple_eh_else_e_body (stmt)));
+
     case GIMPLE_CALL:
       /* Functions that do not return do not fall through.  */
       return (gimple_call_flags (stmt) & ECF_NORETURN) == 0;
@@ -746,7 +779,14 @@ lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
       tmp_rs = *VEC_index (return_statements_t, data->return_statements, i);
 
       if (gimple_return_retval (stmt) == gimple_return_retval (tmp_rs.stmt))
-       goto found;
+       {
+         /* Remove the line number from the representative return statement.
+            It now fills in for many such returns.  Failure to remove this
+            will result in incorrect results for coverage analysis.  */
+         gimple_set_location (tmp_rs.stmt, UNKNOWN_LOCATION);
+
+         goto found;
+       }
     }
 
   /* Not found.  Create a new label and record the return statement.  */
@@ -756,6 +796,9 @@ lower_gimple_return (gimple_stmt_iterator *gsi, struct lower_data *data)
 
   /* Generate a goto statement and remove the return statement.  */
  found:
+  /* When not optimizing, make sure user returns are preserved.  */
+  if (!optimize && gimple_has_location (stmt))
+    DECL_ARTIFICIAL (tmp_rs.label) = 0;
   t = gimple_build_goto (tmp_rs.label);
   gimple_set_location (t, gimple_location (stmt));
   gimple_set_block (t, gimple_block (stmt));
@@ -834,7 +877,7 @@ lower_builtin_setjmp (gimple_stmt_iterator *gsi)
 
   /* Build '__builtin_setjmp_setup (BUF, NEXT_LABEL)' and insert.  */
   arg = build_addr (next_label, current_function_decl);
-  t = implicit_built_in_decls[BUILT_IN_SETJMP_SETUP];
+  t = builtin_decl_implicit (BUILT_IN_SETJMP_SETUP);
   g = gimple_build_call (t, 2, gimple_call_arg (stmt, 0), arg);
   gimple_set_location (g, loc);
   gimple_set_block (g, gimple_block (stmt));
@@ -843,8 +886,7 @@ lower_builtin_setjmp (gimple_stmt_iterator *gsi)
   /* Build 'DEST = 0' and insert.  */
   if (dest)
     {
-      g = gimple_build_assign (dest, fold_convert_loc (loc, TREE_TYPE (dest),
-                                                      integer_zero_node));
+      g = gimple_build_assign (dest, build_zero_cst (TREE_TYPE (dest)));
       gimple_set_location (g, loc);
       gimple_set_block (g, gimple_block (stmt));
       gsi_insert_before (gsi, g, GSI_SAME_STMT);
@@ -860,7 +902,7 @@ lower_builtin_setjmp (gimple_stmt_iterator *gsi)
 
   /* Build '__builtin_setjmp_receiver (NEXT_LABEL)' and insert.  */
   arg = build_addr (next_label, current_function_decl);
-  t = implicit_built_in_decls[BUILT_IN_SETJMP_RECEIVER];
+  t = builtin_decl_implicit (BUILT_IN_SETJMP_RECEIVER);
   g = gimple_build_call (t, 1, arg);
   gimple_set_location (g, loc);
   gimple_set_block (g, gimple_block (stmt));
@@ -893,7 +935,7 @@ record_vars_into (tree vars, tree fn)
   if (fn != current_function_decl)
     push_cfun (DECL_STRUCT_FUNCTION (fn));
 
-  for (; vars; vars = TREE_CHAIN (vars))
+  for (; vars; vars = DECL_CHAIN (vars))
     {
       tree var = vars;
 
@@ -908,6 +950,8 @@ record_vars_into (tree vars, tree fn)
 
       /* Record the variable.  */
       add_local_decl (cfun, var);
+      if (gimple_referenced_vars (cfun))
+       add_referenced_var (var);
     }
 
   if (fn != current_function_decl)