trans-decl.c (gfc_build_label_decl): Set DECL_ARTIFICAL and TREE_USED for all labels.
[platform/upstream/gcc.git] / gcc / fortran / trans-stmt.c
index bbaa19d..7a55cbc 100644 (file)
@@ -1,5 +1,5 @@
 /* Statement translation -- generate GCC trees from gfc_code.
-   Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
    Contributed by Paul Brook <paul@nowt.org>
    and Steven Bosscher <s.bosscher@student.tudelft.nl>
 
@@ -26,12 +26,9 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "coretypes.h"
 #include "tree.h"
 #include "tree-gimple.h"
-#include <stdio.h>
 #include "ggc.h"
 #include "toplev.h"
 #include "real.h"
-#include <assert.h>
-#include <gmp.h>
 #include "gfortran.h"
 #include "trans.h"
 #include "trans-stmt.h"
@@ -113,7 +110,7 @@ gfc_trans_label_assign (gfc_code * code)
     {
       label_str = code->label->format->value.character.string;
       label_len = code->label->format->value.character.length;
-      len_tree = build_int_2 (label_len, 0);
+      len_tree = build_int_cst (NULL_TREE, label_len);
       label_tree = gfc_build_string_const (label_len + 1, label_str);
       label_tree = gfc_build_addr_expr (pchar_type_node, label_tree);
     }
@@ -145,9 +142,9 @@ gfc_trans_goto (gfc_code * code)
   gfc_start_block (&se.pre);
   gfc_conv_expr (&se, code->expr);
   assign_error =
-    gfc_build_string_const (37, "Assigned label is not a target label");
+    gfc_build_cstring_const ("Assigned label is not a target label");
   tmp = GFC_DECL_STRING_LEN (se.expr);
-  tmp = build (NE_EXPR, boolean_type_node, tmp, integer_minus_one_node);
+  tmp = build2 (NE_EXPR, boolean_type_node, tmp, integer_minus_one_node);
   gfc_trans_runtime_check (tmp, assign_error, &se.pre);
 
   assigned_goto = GFC_DECL_ASSIGN_ADDR (se.expr);
@@ -161,15 +158,14 @@ gfc_trans_goto (gfc_code * code)
     }
 
   /* Check the label list.  */
-  range_error =
-    gfc_build_string_const (34, "Assigned label is not in the list");
+  range_error = gfc_build_cstring_const ("Assigned label is not in the list");
 
   do
     {
       tmp = gfc_get_label_decl (code->label);
       tmp = gfc_build_addr_expr (pvoid_type_node, tmp);
-      tmp = build (EQ_EXPR, boolean_type_node, tmp, assigned_goto);
-      tmp = build_v (COND_EXPR, tmp, target, build_empty_stmt ());
+      tmp = build2 (EQ_EXPR, boolean_type_node, tmp, assigned_goto);
+      tmp = build3_v (COND_EXPR, tmp, target, build_empty_stmt ());
       gfc_add_expr_to_block (&se.pre, tmp);
       code = code->block;
     }
@@ -179,6 +175,14 @@ gfc_trans_goto (gfc_code * code)
 }
 
 
+/* Translate an ENTRY statement.  Just adds a label for this entry point.  */
+tree
+gfc_trans_entry (gfc_code * code)
+{
+  return build1_v (LABEL_EXPR, code->ext.entry->label);
+}
+
+
 /* Translate the CALL statement.  Builds a call to an F95 subroutine.  */
 
 tree
@@ -191,7 +195,7 @@ gfc_trans_call (gfc_code * code)
   gfc_init_se (&se, NULL);
   gfc_start_block (&se.pre);
 
-  assert (code->resolved_sym);
+  gcc_assert (code->resolved_sym);
   has_alternate_specifier = 0;
 
   /* Translate the call.  */
@@ -206,7 +210,7 @@ gfc_trans_call (gfc_code * code)
       gfc_code *select_code;
       gfc_symbol *sym;
       select_code = code->next;
-      assert(select_code->op == EXEC_SELECT);
+      gcc_assert(select_code->op == EXEC_SELECT);
       sym = select_code->expr->symtree->n.sym;
       se.expr = convert (gfc_typenode_for_spec (&sym->ts), se.expr);
       gfc_add_modify_expr (&se.pre, sym->backend_decl, se.expr);
@@ -248,7 +252,7 @@ gfc_trans_return (gfc_code * code ATTRIBUTE_UNUSED)
 
       gfc_conv_expr (&se, code->expr);
 
-      tmp = build (MODIFY_EXPR, TREE_TYPE (result), result, se.expr);
+      tmp = build2 (MODIFY_EXPR, TREE_TYPE (result), result, se.expr);
       gfc_add_expr_to_block (&se.pre, tmp);
 
       tmp = build1_v (GOTO_EXPR, gfc_get_return_label ());
@@ -267,6 +271,7 @@ gfc_trans_return (gfc_code * code ATTRIBUTE_UNUSED)
 tree
 gfc_trans_pause (gfc_code * code)
 {
+  tree gfc_int4_type_node = gfc_get_int_type (4);
   gfc_se se;
   tree args;
   tree tmp;
@@ -279,8 +284,7 @@ gfc_trans_pause (gfc_code * code)
 
   if (code->expr == NULL)
     {
-      tmp = build_int_2 (code->ext.stop_code, 0);
-      TREE_TYPE (tmp) = gfc_int4_type_node;
+      tmp = build_int_cst (gfc_int4_type_node, code->ext.stop_code);
       args = gfc_chainon_list (NULL_TREE, tmp);
       fndecl = gfor_fndecl_pause_numeric;
     }
@@ -307,6 +311,7 @@ gfc_trans_pause (gfc_code * code)
 tree
 gfc_trans_stop (gfc_code * code)
 {
+  tree gfc_int4_type_node = gfc_get_int_type (4);
   gfc_se se;
   tree args;
   tree tmp;
@@ -319,8 +324,7 @@ gfc_trans_stop (gfc_code * code)
 
   if (code->expr == NULL)
     {
-      tmp = build_int_2 (code->ext.stop_code, 0);
-      TREE_TYPE (tmp) = gfc_int4_type_node;
+      tmp = build_int_cst (gfc_int4_type_node, code->ext.stop_code);
       args = gfc_chainon_list (NULL_TREE, tmp);
       fndecl = gfor_fndecl_stop_numeric;
     }
@@ -408,7 +412,7 @@ gfc_trans_if_1 (gfc_code * code)
     elsestmt = build_empty_stmt ();
 
   /* Build the condition expression and add it to the condition block.  */
-  stmt = build_v (COND_EXPR, if_se.expr, stmt, elsestmt);
+  stmt = build3_v (COND_EXPR, if_se.expr, stmt, elsestmt);
   
   gfc_add_expr_to_block (&if_se.pre, stmt);
 
@@ -465,13 +469,13 @@ gfc_trans_arithmetic_if (gfc_code * code)
   branch1 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label));
   branch2 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label2));
 
-  tmp = build (LT_EXPR, boolean_type_node, se.expr, zero);
-  branch1 = build_v (COND_EXPR, tmp, branch1, branch2);
+  tmp = build2 (LT_EXPR, boolean_type_node, se.expr, zero);
+  branch1 = build3_v (COND_EXPR, tmp, branch1, branch2);
 
   /* if (cond <= 0) take branch1 else take branch2.  */
   branch2 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label3));
-  tmp = build (LE_EXPR, boolean_type_node, se.expr, zero);
-  branch1 = build_v (COND_EXPR, tmp, branch1, branch2);
+  tmp = build2 (LE_EXPR, boolean_type_node, se.expr, zero);
+  branch1 = build3_v (COND_EXPR, tmp, branch1, branch2);
 
   /* Append the COND_EXPR to the evaluation of COND, and return.  */
   gfc_add_expr_to_block (&se.pre, branch1);
@@ -479,13 +483,113 @@ gfc_trans_arithmetic_if (gfc_code * code)
 }
 
 
+/* Translate the simple DO construct.  This is where the loop variable has
+   integer type and step +-1.  We can't use this in the general case
+   because integer overflow and floating point errors could give incorrect
+   results.
+   We translate a do loop from:
+
+   DO dovar = from, to, step
+      body
+   END DO
+
+   to:
+
+   [Evaluate loop bounds and step]
+   dovar = from;
+   if ((step > 0) ? (dovar <= to) : (dovar => to))
+    {
+      for (;;)
+        {
+         body;
+   cycle_label:
+         cond = (dovar == to);
+         dovar += step;
+         if (cond) goto end_label;
+       }
+      }
+   end_label:
+
+   This helps the optimizers by avoiding the extra induction variable
+   used in the general case.  */
+
+static tree
+gfc_trans_simple_do (gfc_code * code, stmtblock_t *pblock, tree dovar,
+                    tree from, tree to, tree step)
+{
+  stmtblock_t body;
+  tree type;
+  tree cond;
+  tree tmp;
+  tree cycle_label;
+  tree exit_label;
+  
+  type = TREE_TYPE (dovar);
+
+  /* Initialize the DO variable: dovar = from.  */
+  gfc_add_modify_expr (pblock, dovar, from);
+
+  /* Cycle and exit statements are implemented with gotos.  */
+  cycle_label = gfc_build_label_decl (NULL_TREE);
+  exit_label = gfc_build_label_decl (NULL_TREE);
+
+  /* Put the labels where they can be found later. See gfc_trans_do().  */
+  code->block->backend_decl = tree_cons (cycle_label, exit_label, NULL);
+
+  /* Loop body.  */
+  gfc_start_block (&body);
+
+  /* Main loop body.  */
+  tmp = gfc_trans_code (code->block->next);
+  gfc_add_expr_to_block (&body, tmp);
+
+  /* Label for cycle statements (if needed).  */
+  if (TREE_USED (cycle_label))
+    {
+      tmp = build1_v (LABEL_EXPR, cycle_label);
+      gfc_add_expr_to_block (&body, tmp);
+    }
+
+  /* Evaluate the loop condition.  */
+  cond = build2 (EQ_EXPR, boolean_type_node, dovar, to);
+  cond = gfc_evaluate_now (cond, &body);
+
+  /* Increment the loop variable.  */
+  tmp = build2 (PLUS_EXPR, type, dovar, step);
+  gfc_add_modify_expr (&body, dovar, tmp);
+
+  /* The loop exit.  */
+  tmp = build1_v (GOTO_EXPR, exit_label);
+  TREE_USED (exit_label) = 1;
+  tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt ());
+  gfc_add_expr_to_block (&body, tmp);
+
+  /* Finish the loop body.  */
+  tmp = gfc_finish_block (&body);
+  tmp = build1_v (LOOP_EXPR, tmp);
+
+  /* Only execute the loop if the number of iterations is positive.  */
+  if (tree_int_cst_sgn (step) > 0)
+    cond = fold (build2 (LE_EXPR, boolean_type_node, dovar, to));
+  else
+    cond = fold (build2 (GE_EXPR, boolean_type_node, dovar, to));
+  tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt ());
+  gfc_add_expr_to_block (pblock, tmp);
+
+  /* Add the exit label.  */
+  tmp = build1_v (LABEL_EXPR, exit_label);
+  gfc_add_expr_to_block (pblock, tmp);
+
+  return gfc_finish_block (pblock);
+}
+
 /* Translate the DO construct.  This obviously is one of the most
    important ones to get right with any compiler, but especially
    so for Fortran.
 
-   Currently we calculate the loop count before entering the loop, but
-   it may be possible to optimize if step is a constant. The main
-   advantage is that the loop test is a single GENERIC node
+   We special case some loop forms as described in gfc_trans_simple_do.
+   For other cases we implement them with a separate loop count,
+   as described in the standard.
 
    We translate a do loop from:
 
@@ -495,29 +599,22 @@ gfc_trans_arithmetic_if (gfc_code * code)
 
    to:
 
-   pre_dovar;
-   pre_from;
-   pre_to;
-   pre_step;
-   temp1=to_expr-from_expr;
-   step_temp=step_expr;
-   range_temp=step_tmp/range_temp;
-   for ( ; range_temp > 0 ; range_temp = range_temp - 1)
+   [evaluate loop bounds and step]
+   count = to + step - from;
+   dovar = from;
+   for (;;)
      {
        body;
 cycle_label:
-       dovar_temp = dovar
-       dovar=dovar_temp + step_temp;
+       dovar += step
+       count--;
+       if (count <=0) goto exit_label;
      }
 exit_label:
 
-   Some optimization is done for empty do loops. We can't just let
-   dovar=to because it's possible for from+range*loopcount!=to.  Anyone
-   who writes empty DO deserves sub-optimal (but correct) code anyway.
-
    TODO: Large loop counts
-   Does not work loop counts which do not fit into a signed integer kind,
-   ie. Does not work for loop counts > 2^31 for integer(kind=4) variables
+   The code above assumes the loop count fits into a signed integer kind,
+   i.e. Does not work for loop counts > 2^31 for integer(kind=4) variables
    We must support the full range.  */
 
 tree
@@ -529,6 +626,7 @@ gfc_trans_do (gfc_code * code)
   tree to;
   tree step;
   tree count;
+  tree count_one;
   tree type;
   tree cond;
   tree cycle_label;
@@ -539,8 +637,7 @@ gfc_trans_do (gfc_code * code)
 
   gfc_start_block (&block);
 
-  /* Create GIMPLE versions of all expressions in the iterator.  */
-
+  /* Evaluate all the expressions in the iterator.  */
   gfc_init_se (&se, NULL);
   gfc_conv_expr_lhs (&se, code->ext.iterator->var);
   gfc_add_block_to_block (&block, &se.pre);
@@ -548,34 +645,50 @@ gfc_trans_do (gfc_code * code)
   type = TREE_TYPE (dovar);
 
   gfc_init_se (&se, NULL);
-  gfc_conv_expr_type (&se, code->ext.iterator->start, type);
+  gfc_conv_expr_val (&se, code->ext.iterator->start);
   gfc_add_block_to_block (&block, &se.pre);
-  from = se.expr;
+  from = gfc_evaluate_now (se.expr, &block);
 
   gfc_init_se (&se, NULL);
-  gfc_conv_expr_type (&se, code->ext.iterator->end, type);
+  gfc_conv_expr_val (&se, code->ext.iterator->end);
   gfc_add_block_to_block (&block, &se.pre);
-  to = se.expr;
+  to = gfc_evaluate_now (se.expr, &block);
 
   gfc_init_se (&se, NULL);
-  gfc_conv_expr_type (&se, code->ext.iterator->step, type);
-
-  /* We don't want this changing part way through.  */
-  gfc_make_safe_expr (&se);
+  gfc_conv_expr_val (&se, code->ext.iterator->step);
   gfc_add_block_to_block (&block, &se.pre);
-  step = se.expr;
-
-  /* Initialise loop count. This code is executed before we enter the
+  step = gfc_evaluate_now (se.expr, &block);
+
+  /* Special case simple loops.  */
+  if (TREE_CODE (type) == INTEGER_TYPE
+      && (integer_onep (step)
+       || tree_int_cst_equal (step, integer_minus_one_node)))
+    return gfc_trans_simple_do (code, &block, dovar, from, to, step);
+      
+  /* Initialize loop count. This code is executed before we enter the
      loop body. We generate: count = (to + step - from) / step.  */
 
-  tmp = fold (build (MINUS_EXPR, type, step, from));
-  tmp = fold (build (PLUS_EXPR, type, to, tmp));
-  tmp = fold (build (TRUNC_DIV_EXPR, type, tmp, step));
-
-  count = gfc_create_var (type, "count");
+  tmp = fold (build2 (MINUS_EXPR, type, step, from));
+  tmp = fold (build2 (PLUS_EXPR, type, to, tmp));
+  if (TREE_CODE (type) == INTEGER_TYPE)
+    {
+      tmp = fold (build2 (TRUNC_DIV_EXPR, type, tmp, step));
+      count = gfc_create_var (type, "count");
+    }
+  else
+    {
+      /* TODO: We could use the same width as the real type.
+        This would probably cause more problems that it solves
+        when we implement "long double" types.  */
+      tmp = fold (build2 (RDIV_EXPR, type, tmp, step));
+      tmp = fold (build1 (FIX_TRUNC_EXPR, gfc_array_index_type, tmp));
+      count = gfc_create_var (gfc_array_index_type, "count");
+    }
   gfc_add_modify_expr (&block, count, tmp);
 
-  /* Initialise the DO variable: dovar = from.  */
+  count_one = convert (TREE_TYPE (count), integer_one_node);
+
+  /* Initialize the DO variable: dovar = from.  */
   gfc_add_modify_expr (&block, dovar, from);
 
   /* Loop body.  */
@@ -586,10 +699,11 @@ gfc_trans_do (gfc_code * code)
   exit_label = gfc_build_label_decl (NULL_TREE);
 
   /* Start with the loop condition.  Loop until count <= 0.  */
-  cond = build (LE_EXPR, boolean_type_node, count, integer_zero_node);
+  cond = build2 (LE_EXPR, boolean_type_node, count,
+               convert (TREE_TYPE (count), integer_zero_node));
   tmp = build1_v (GOTO_EXPR, exit_label);
   TREE_USED (exit_label) = 1;
-  tmp = build_v (COND_EXPR, cond, tmp, build_empty_stmt ());
+  tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt ());
   gfc_add_expr_to_block (&body, tmp);
 
   /* Put these labels where they can be found later. We put the
@@ -611,18 +725,18 @@ gfc_trans_do (gfc_code * code)
     }
 
   /* Increment the loop variable.  */
-  tmp = build (PLUS_EXPR, type, dovar, step);
+  tmp = build2 (PLUS_EXPR, type, dovar, step);
   gfc_add_modify_expr (&body, dovar, tmp);
 
   /* Decrement the loop count.  */
-  tmp = build (MINUS_EXPR, type, count, integer_one_node);
+  tmp = build2 (MINUS_EXPR, TREE_TYPE (count), count, count_one);
   gfc_add_modify_expr (&body, count, tmp);
 
   /* End of loop body.  */
   tmp = gfc_finish_block (&body);
 
   /* The for loop itself.  */
-  tmp = build_v (LOOP_EXPR, tmp);
+  tmp = build1_v (LOOP_EXPR, tmp);
   gfc_add_expr_to_block (&block, tmp);
 
   /* Add the exit label.  */
@@ -684,7 +798,7 @@ gfc_trans_do_while (gfc_code * code)
   /* Build "IF (! cond) GOTO exit_label".  */
   tmp = build1_v (GOTO_EXPR, exit_label);
   TREE_USED (exit_label) = 1;
-  tmp = build_v (COND_EXPR, cond.expr, tmp, build_empty_stmt ());
+  tmp = build3_v (COND_EXPR, cond.expr, tmp, build_empty_stmt ());
   gfc_add_expr_to_block (&block, tmp);
 
   /* The main body of the loop.  */
@@ -703,7 +817,7 @@ gfc_trans_do_while (gfc_code * code)
 
   gfc_init_block (&block);
   /* Build the loop.  */
-  tmp = build_v (LOOP_EXPR, tmp);
+  tmp = build1_v (LOOP_EXPR, tmp);
   gfc_add_expr_to_block (&block, tmp);
 
   /* Add the exit label.  */
@@ -831,12 +945,11 @@ gfc_trans_integer_select (gfc_code * code)
            }
 
           /* Build a label.  */
-          label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
-          DECL_CONTEXT (label) = current_function_decl;
+          label = gfc_build_label_decl (NULL_TREE);
 
          /* Add this case label.
              Add parameter 'label', make it match GCC backend.  */
-         tmp = build (CASE_LABEL_EXPR, void_type_node, low, high, label);
+         tmp = build3 (CASE_LABEL_EXPR, void_type_node, low, high, label);
          gfc_add_expr_to_block (&body, tmp);
        }
 
@@ -850,7 +963,7 @@ gfc_trans_integer_select (gfc_code * code)
     }
 
   tmp = gfc_finish_block (&body);
-  tmp = build_v (SWITCH_EXPR, se.expr, tmp, NULL_TREE);
+  tmp = build3_v (SWITCH_EXPR, se.expr, tmp, NULL_TREE);
   gfc_add_expr_to_block (&block, tmp);
 
   tmp = build1_v (LABEL_EXPR, end_label);
@@ -951,8 +1064,8 @@ gfc_trans_logical_select (gfc_code * code)
       if (f != NULL)
        false_tree = gfc_trans_code (f->next);
 
-      gfc_add_expr_to_block (&block, build_v (COND_EXPR, se.expr,
-                                             true_tree, false_tree));
+      gfc_add_expr_to_block (&block, build3_v (COND_EXPR, se.expr,
+                                              true_tree, false_tree));
     }
 
   return gfc_finish_block (&block);
@@ -985,6 +1098,8 @@ gfc_trans_character_select (gfc_code *code)
 
   if (select_struct == NULL)
     {
+      tree gfc_int4_type_node = gfc_get_int_type (4);
+
       select_struct = make_node (RECORD_TYPE);
       TYPE_NAME (select_struct) = get_identifier ("_jump_struct");
 
@@ -1038,14 +1153,14 @@ gfc_trans_character_select (gfc_code *code)
     {
       for (d = c->ext.case_list; d; d = d->next)
         {
-          tmp = build_v (LABEL_EXPR, labels[d->n]);
+          tmp = build1_v (LABEL_EXPR, labels[d->n]);
           gfc_add_expr_to_block (&body, tmp);
         }
 
       tmp = gfc_trans_code (c->next);
       gfc_add_expr_to_block (&body, tmp);
 
-      tmp = build_v (GOTO_EXPR, end_label);
+      tmp = build1_v (GOTO_EXPR, end_label);
       gfc_add_expr_to_block (&body, tmp);
     }
 
@@ -1093,8 +1208,8 @@ gfc_trans_character_select (gfc_code *code)
       init = tree_cons (NULL_TREE, tmp, init);
     }
 
-  type = build_array_type (select_struct,
-                           build_index_type (build_int_2(n - 1, 0)));
+  type = build_array_type (select_struct, build_index_type
+                          (build_int_cst (NULL_TREE, n - 1)));
 
   init = build1 (CONSTRUCTOR, type, nreverse(init));
   TREE_CONSTANT (init) = 1;
@@ -1112,7 +1227,7 @@ gfc_trans_character_select (gfc_code *code)
   init = gfc_build_addr_expr (pvoid_type_node, init);
   args = gfc_chainon_list (NULL_TREE, init);
 
-  tmp = build_int_2 (n, 0);
+  tmp = build_int_cst (NULL_TREE, n);
   args = gfc_chainon_list (args, tmp);
 
   tmp = gfc_build_addr_expr (pvoid_type_node, end_label);
@@ -1132,7 +1247,7 @@ gfc_trans_character_select (gfc_code *code)
 
   tmp = gfc_finish_block (&body);
   gfc_add_expr_to_block (&block, tmp);
-  tmp = build_v (LABEL_EXPR, end_label);
+  tmp = build1_v (LABEL_EXPR, end_label);
   gfc_add_expr_to_block (&block, tmp);
 
   if (n != 0)
@@ -1159,7 +1274,7 @@ gfc_trans_character_select (gfc_code *code)
 tree
 gfc_trans_select (gfc_code * code)
 {
-  assert (code && code->expr);
+  gcc_assert (code && code->expr);
 
   /* Empty SELECT constructs are legal.  */
   if (code->block == NULL)
@@ -1221,16 +1336,16 @@ gfc_trans_forall_loop (forall_info *forall_tmp, int nvar, tree body, int mask_fl
       gfc_init_block (&block);
 
       /* The exit condition.  */
-      cond = build (LE_EXPR, boolean_type_node, count, integer_zero_node);
+      cond = build2 (LE_EXPR, boolean_type_node, count, integer_zero_node);
       tmp = build1_v (GOTO_EXPR, exit_label);
-      tmp = build_v (COND_EXPR, cond, tmp, build_empty_stmt ());
+      tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt ());
       gfc_add_expr_to_block (&block, tmp);
 
       /* The main loop body.  */
       gfc_add_expr_to_block (&block, body);
 
       /* Increment the loop variable.  */
-      tmp = build (PLUS_EXPR, TREE_TYPE (var), var, step);
+      tmp = build2 (PLUS_EXPR, TREE_TYPE (var), var, step);
       gfc_add_modify_expr (&block, var, tmp);
 
       /* Advance to the next mask element.  */
@@ -1240,13 +1355,13 @@ gfc_trans_forall_loop (forall_info *forall_tmp, int nvar, tree body, int mask_fl
           maskindex = forall_tmp->maskindex;
           if (mask)
             {
-              tmp = build (PLUS_EXPR, gfc_array_index_type, maskindex,
-                           integer_one_node);
+              tmp = build2 (PLUS_EXPR, gfc_array_index_type,
+                           maskindex, gfc_index_one_node);
               gfc_add_modify_expr (&block, maskindex, tmp);
             }
         }
       /* Decrement the loop counter.  */
-      tmp = build (MINUS_EXPR, TREE_TYPE (var), count, integer_one_node);
+      tmp = build2 (MINUS_EXPR, TREE_TYPE (var), count, gfc_index_one_node);
       gfc_add_modify_expr (&block, count, tmp);
 
       body = gfc_finish_block (&block);
@@ -1256,13 +1371,13 @@ gfc_trans_forall_loop (forall_info *forall_tmp, int nvar, tree body, int mask_fl
       gfc_add_modify_expr (&block, var, start);
 
       /* Initialize the loop counter.  */
-      tmp = fold (build (MINUS_EXPR, TREE_TYPE (var), step, start));
-      tmp = fold (build (PLUS_EXPR, TREE_TYPE (var), end, tmp));
-      tmp = fold (build (TRUNC_DIV_EXPR, TREE_TYPE (var), tmp, step));
+      tmp = fold (build2 (MINUS_EXPR, TREE_TYPE (var), step, start));
+      tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (var), end, tmp));
+      tmp = fold (build2 (TRUNC_DIV_EXPR, TREE_TYPE (var), tmp, step));
       gfc_add_modify_expr (&block, count, tmp);
 
       /* The loop expression.  */
-      tmp = build_v (LOOP_EXPR, body);
+      tmp = build1_v (LOOP_EXPR, body);
       gfc_add_expr_to_block (&block, tmp);
 
       /* The exit label.  */
@@ -1277,9 +1392,9 @@ gfc_trans_forall_loop (forall_info *forall_tmp, int nvar, tree body, int mask_fl
 
 
 /* Generate the body and loops according to MASK_FLAG and NEST_FLAG.
-   if MASK_FLAG is non-zero, the body is controlled by maskes in forall
+   if MASK_FLAG is nonzero, the body is controlled by maskes in forall
    nest, otherwise, the body is not controlled by maskes.
-   if NEST_FLAG is non-zero, generate loops for nested forall, otherwise,
+   if NEST_FLAG is nonzero, generate loops for nested forall, otherwise,
    only generate loops for the current forall level.  */
 
 static tree
@@ -1308,14 +1423,14 @@ gfc_trans_nested_forall_loop (forall_info * nested_forall_info, tree body,
 
               if (mask)
                 {
-                  /* If a mask was specified make the assignment contitional.  */
+                  /* If a mask was specified make the assignment conditional.  */
                   if (pmask)
                    tmp = gfc_build_indirect_ref (mask);
                   else
                     tmp = mask;
                   tmp = gfc_build_array_ref (tmp, maskindex);
 
-                  body = build_v (COND_EXPR, tmp, body, build_empty_stmt ());
+                  body = build3_v (COND_EXPR, tmp, body, build_empty_stmt ());
                 }
             }
           nvar = forall_tmp->nvar;
@@ -1347,17 +1462,17 @@ gfc_do_allocate (tree bytesize, tree size, tree * pdata, stmtblock_t * pblock,
 
   if (INTEGER_CST_P (size))
     {
-      tmp = fold (build (MINUS_EXPR, gfc_array_index_type, size,
-                        integer_one_node));
+      tmp = fold (build2 (MINUS_EXPR, gfc_array_index_type, size,
+                         gfc_index_one_node));
     }
   else
     tmp = NULL_TREE;
 
-  type = build_range_type (gfc_array_index_type, integer_zero_node, tmp);
+  type = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
   type = build_array_type (elem_type, type);
   if (gfc_can_put_var_on_stack (bytesize))
     {
-      assert (INTEGER_CST_P (size));
+      gcc_assert (INTEGER_CST_P (size));
       tmpvar = gfc_create_var (type, "temp");
       *pdata = NULL_TREE;
     }
@@ -1372,7 +1487,7 @@ gfc_do_allocate (tree bytesize, tree size, tree * pdata, stmtblock_t * pblock,
       else if (gfc_index_integer_kind == 8)
        tmp = gfor_fndecl_internal_malloc64;
       else
-       abort ();
+       gcc_unreachable ();
       tmp = gfc_build_function_call (tmp, args);
       tmp = convert (TREE_TYPE (tmpvar), tmp);
       gfc_add_modify_expr (pblock, tmpvar, tmp);
@@ -1416,7 +1531,7 @@ generate_loop_for_temp_to_lhs (gfc_expr *expr, tree tmp1, tree size,
       gfc_add_block_to_block (&block, &lse.post);
 
       /* Increment the count1.  */
-      tmp = fold (build (PLUS_EXPR, TREE_TYPE (count1), count1, size));
+      tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (count1), count1, size));
       gfc_add_modify_expr (&block, count1, tmp);
       tmp = gfc_finish_block (&block);
     }
@@ -1438,7 +1553,7 @@ generate_loop_for_temp_to_lhs (gfc_expr *expr, tree tmp1, tree size,
 
       gfc_mark_ss_chain_used (lss, 1);
       /* Initialize count2.  */
-      gfc_add_modify_expr (&block, count2, integer_zero_node);
+      gfc_add_modify_expr (&block, count2, gfc_index_zero_node);
 
       /* Start the scalarized loop body.  */
       gfc_start_scalarized_body (&loop1, &body);
@@ -1450,8 +1565,8 @@ generate_loop_for_temp_to_lhs (gfc_expr *expr, tree tmp1, tree size,
       /* Form the expression of the temporary.  */
       if (lss != gfc_ss_terminator)
         {
-          index = fold (build (PLUS_EXPR, gfc_array_index_type,
-                              count1, count2));
+          index = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                               count1, count2));
           rse.expr = gfc_build_array_ref (tmp1, index);
         }
       /* Translate expr.  */
@@ -1470,25 +1585,25 @@ generate_loop_for_temp_to_lhs (gfc_expr *expr, tree tmp1, tree size,
          while (tmp2)
            {
              tmp1 = gfc_build_array_ref (tmp2, count3);
-             wheremaskexpr = build (TRUTH_AND_EXPR, TREE_TYPE (tmp1),
-                                    wheremaskexpr, tmp1);
+             wheremaskexpr = build2 (TRUTH_AND_EXPR, TREE_TYPE (tmp1),
+                                    wheremaskexpr, tmp1);
              tmp2 = TREE_CHAIN (tmp2);
            }
-         tmp = build_v (COND_EXPR, wheremaskexpr, tmp, build_empty_stmt ());
+         tmp = build3_v (COND_EXPR, wheremaskexpr, tmp, build_empty_stmt ());
        }
 
       gfc_add_expr_to_block (&body, tmp);
 
       /* Increment count2.  */
-      tmp = fold (build (PLUS_EXPR, TREE_TYPE (count2), count2,
-                         integer_one_node));
+      tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                         count2, gfc_index_one_node));
       gfc_add_modify_expr (&body, count2, tmp);
 
       /* Increment count3.  */
       if (count3)
         {
-          tmp = fold (build (PLUS_EXPR, TREE_TYPE (count3), count3,
-                             integer_one_node));
+          tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                             count3, gfc_index_one_node));
           gfc_add_modify_expr (&body, count3, tmp);
         }
 
@@ -1499,7 +1614,7 @@ generate_loop_for_temp_to_lhs (gfc_expr *expr, tree tmp1, tree size,
       gfc_cleanup_loop (&loop1);
 
       /* Increment count1.  */
-      tmp = fold (build (PLUS_EXPR, TREE_TYPE (count1), count1, size));
+      tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (count1), count1, size));
       gfc_add_modify_expr (&block, count1, tmp);
       tmp = gfc_finish_block (&block);
     }
@@ -1536,10 +1651,10 @@ generate_loop_for_rhs_to_temp (gfc_expr *expr2, tree tmp1, tree size,
     }
   else
     {
-      /* Initilize count2.  */
-      gfc_add_modify_expr (&block, count2, integer_zero_node);
+      /* Initialize count2.  */
+      gfc_add_modify_expr (&block, count2, gfc_index_zero_node);
 
-      /* Initiliaze the loop.  */
+      /* Initialize the loop.  */
       gfc_init_loopinfo (&loop);
 
       /* We may need LSS to determine the shape of the expression.  */
@@ -1559,7 +1674,7 @@ generate_loop_for_rhs_to_temp (gfc_expr *expr2, tree tmp1, tree size,
       gfc_conv_expr (&rse, expr2);
 
       /* Form the expression of the temporary.  */
-      index = fold (build (PLUS_EXPR, gfc_array_index_type, count1, count2));
+      index = fold (build2 (PLUS_EXPR, gfc_array_index_type, count1, count2));
       lse.expr = gfc_build_array_ref (tmp1, index);
     }
 
@@ -1576,11 +1691,11 @@ generate_loop_for_rhs_to_temp (gfc_expr *expr2, tree tmp1, tree size,
       while (tmp2)
         {
           tmp1 = gfc_build_array_ref (tmp2, count3);
-          wheremaskexpr = build (TRUTH_AND_EXPR, TREE_TYPE (tmp1),
-                                 wheremaskexpr, tmp1);
+          wheremaskexpr = build2 (TRUTH_AND_EXPR, TREE_TYPE (tmp1),
+                                 wheremaskexpr, tmp1);
           tmp2 = TREE_CHAIN (tmp2);
         }
-      tmp = build_v (COND_EXPR, wheremaskexpr, tmp, build_empty_stmt ());
+      tmp = build3_v (COND_EXPR, wheremaskexpr, tmp, build_empty_stmt ());
     }
 
   gfc_add_expr_to_block (&body1, tmp);
@@ -1592,15 +1707,15 @@ generate_loop_for_rhs_to_temp (gfc_expr *expr2, tree tmp1, tree size,
   else
     {
       /* Increment count2.  */
-      tmp = fold (build (PLUS_EXPR, gfc_array_index_type, count2,
-                        integer_one_node));
+      tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                         count2, gfc_index_one_node));
       gfc_add_modify_expr (&body1, count2, tmp);
 
       /* Increment count3.  */
       if (count3)
         {
-          tmp = fold (build (PLUS_EXPR, gfc_array_index_type, count3,
-                             integer_one_node));
+          tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                             count3, gfc_index_one_node));
           gfc_add_modify_expr (&body1, count3, tmp);
         }
 
@@ -1615,7 +1730,7 @@ generate_loop_for_rhs_to_temp (gfc_expr *expr2, tree tmp1, tree size,
          as tree nodes in SS may not be valid in different scope.  */
     }
   /* Increment count1.  */
-  tmp = fold (build (PLUS_EXPR, TREE_TYPE (count1), count1, size));
+  tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (count1), count1, size));
   gfc_add_modify_expr (&block, count1, tmp);
 
   tmp = gfc_finish_block (&block);
@@ -1639,7 +1754,7 @@ compute_inner_temp_size (gfc_expr *expr1, gfc_expr *expr2,
   *lss = gfc_walk_expr (expr1);
   *rss = NULL;
 
-  size = integer_one_node;
+  size = gfc_index_one_node;
   if (*lss != gfc_ss_terminator)
     {
       gfc_init_loopinfo (&loop);
@@ -1672,10 +1787,11 @@ compute_inner_temp_size (gfc_expr *expr1, gfc_expr *expr2,
       /* Figure out how many elements we need.  */
       for (i = 0; i < loop.dimen; i++)
         {
-         tmp = fold (build (MINUS_EXPR, TREE_TYPE (loop.from[i]),
-                            integer_one_node, loop.from[i]));
-          tmp = fold (build (PLUS_EXPR, TREE_TYPE (tmp), tmp, loop.to[i]));
-          size = fold (build (MULT_EXPR, TREE_TYPE (size), size, tmp));
+         tmp = fold (build2 (MINUS_EXPR, gfc_array_index_type,
+                             gfc_index_one_node, loop.from[i]));
+          tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                             tmp, loop.to[i]));
+          size = fold (build2 (MULT_EXPR, gfc_array_index_type, size, tmp));
         }
       gfc_add_block_to_block (pblock, &loop.pre);
       size = gfc_evaluate_now (size, pblock);
@@ -1700,12 +1816,12 @@ compute_overall_iter_number (forall_info *nested_forall_info, tree inner_size,
 
   /* TODO: optimizing the computing process.  */
   number = gfc_create_var (gfc_array_index_type, "num");
-  gfc_add_modify_expr (block, number, integer_zero_node);
+  gfc_add_modify_expr (block, number, gfc_index_zero_node);
 
   gfc_start_block (&body);
   if (nested_forall_info)
-    tmp = build (PLUS_EXPR, gfc_array_index_type, number,
-                 inner_size);
+    tmp = build2 (PLUS_EXPR, gfc_array_index_type, number,
+                 inner_size);
   else
     tmp = inner_size;
   gfc_add_modify_expr (&body, number, tmp);
@@ -1739,7 +1855,7 @@ allocate_temp_for_forall_nest (forall_info * nested_forall_info, tree type,
   size = compute_overall_iter_number (nested_forall_info, inner_size, block);
 
   unit = TYPE_SIZE_UNIT (type);
-  bytesize = fold (build (MULT_EXPR, gfc_array_index_type, size, unit));
+  bytesize = fold (build2 (MULT_EXPR, gfc_array_index_type, size, unit));
 
   *ptemp1 = NULL;
   temp1 = gfc_do_allocate (bytesize, size, ptemp1, block, type);
@@ -1778,13 +1894,13 @@ gfc_trans_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2, tree wheremask,
   if (wheremask)
     {
       count = gfc_create_var (gfc_array_index_type, "count");
-      gfc_add_modify_expr (block, count, integer_zero_node);
+      gfc_add_modify_expr (block, count, gfc_index_zero_node);
     }
   else
     count = NULL;
 
   /* Initialize count1.  */
-  gfc_add_modify_expr (block, count1, integer_zero_node);
+  gfc_add_modify_expr (block, count1, gfc_index_zero_node);
 
   /* Calculate the size of temporary needed in the assignment. Return loop, lss
      and rss which are used in function generate_loop_for_rhs_to_temp().  */
@@ -1794,7 +1910,7 @@ gfc_trans_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2, tree wheremask,
   type = gfc_typenode_for_spec (&expr1->ts);
 
   /* Allocate temporary for nested forall construct according to the
-     information in nested_forall_info and inner_size. */
+     information in nested_forall_info and inner_size.  */
   tmp1 = allocate_temp_for_forall_nest (nested_forall_info, type,
                                 inner_size, block, &ptemp1);
 
@@ -1805,7 +1921,7 @@ gfc_trans_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2, tree wheremask,
       mask = forall_tmp->mask;
       maskindex = forall_tmp->maskindex;
       if (mask)
-        gfc_add_modify_expr (block, maskindex, integer_zero_node);
+        gfc_add_modify_expr (block, maskindex, gfc_index_zero_node);
       forall_tmp = forall_tmp->next_nest;
     }
 
@@ -1813,13 +1929,13 @@ gfc_trans_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2, tree wheremask,
   tmp = generate_loop_for_rhs_to_temp (expr2, tmp1, inner_size, count,
                                        count1, count2, lss, rss, wheremask);
 
-  /* Generate body and loops according to the inforamtion in
+  /* Generate body and loops according to the information in
      nested_forall_info.  */
   tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1, 1);
   gfc_add_expr_to_block (block, tmp);
 
   /* Reset count1.  */
-  gfc_add_modify_expr (block, count1, integer_zero_node);
+  gfc_add_modify_expr (block, count1, gfc_index_zero_node);
 
   /* Reset maskindexed.  */
   forall_tmp = nested_forall_info;
@@ -1828,19 +1944,19 @@ gfc_trans_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2, tree wheremask,
       mask = forall_tmp->mask;
       maskindex = forall_tmp->maskindex;
       if (mask)
-        gfc_add_modify_expr (block, maskindex, integer_zero_node);
+        gfc_add_modify_expr (block, maskindex, gfc_index_zero_node);
       forall_tmp = forall_tmp->next_nest;
     }
 
   /* Reset count.  */
   if (wheremask)
-    gfc_add_modify_expr (block, count, integer_zero_node);
+    gfc_add_modify_expr (block, count, gfc_index_zero_node);
 
   /* Generate codes to copy the temporary to lhs.  */
   tmp = generate_loop_for_temp_to_lhs (expr1, tmp1, inner_size, count,
                                        count1, count2, wheremask);
 
-  /* Generate body and loops according to the inforamtion in
+  /* Generate body and loops according to the information in
      nested_forall_info.  */
   tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1, 1);
   gfc_add_expr_to_block (block, tmp);
@@ -1879,7 +1995,7 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
   forall_info *forall_tmp;
 
   count = gfc_create_var (gfc_array_index_type, "count");
-  gfc_add_modify_expr (block, count, integer_zero_node);
+  gfc_add_modify_expr (block, count, gfc_index_zero_node);
 
   inner_size = integer_one_node;
   lss = gfc_walk_expr (expr1);
@@ -1904,8 +2020,8 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
       gfc_add_block_to_block (&body, &rse.post);
 
       /* Increment count.  */
-      tmp = fold (build (PLUS_EXPR, TREE_TYPE (count), count,
-                         integer_one_node));
+      tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                         count, gfc_index_one_node));
       gfc_add_modify_expr (&body, count, tmp);
 
       tmp = gfc_finish_block (&body);
@@ -1917,17 +2033,17 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
           mask = forall_tmp->mask;
           maskindex = forall_tmp->maskindex;
           if (mask)
-            gfc_add_modify_expr (block, maskindex, integer_zero_node);
+            gfc_add_modify_expr (block, maskindex, gfc_index_zero_node);
           forall_tmp = forall_tmp->next_nest;
         }
 
-      /* Generate body and loops according to the inforamtion in
+      /* Generate body and loops according to the information in
          nested_forall_info.  */
       tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1, 1);
       gfc_add_expr_to_block (block, tmp);
 
       /* Reset count.  */
-      gfc_add_modify_expr (block, count, integer_zero_node);
+      gfc_add_modify_expr (block, count, gfc_index_zero_node);
 
       /* Reset maskindexes.  */
       forall_tmp = nested_forall_info;
@@ -1936,7 +2052,7 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
           mask = forall_tmp->mask;
           maskindex = forall_tmp->maskindex;
           if (mask)
-            gfc_add_modify_expr (block, maskindex, integer_zero_node);
+            gfc_add_modify_expr (block, maskindex, gfc_index_zero_node);
           forall_tmp = forall_tmp->next_nest;
         }
       gfc_start_block (&body);
@@ -1949,12 +2065,12 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
       gfc_add_modify_expr (&body, lse.expr, rse.expr);
       gfc_add_block_to_block (&body, &lse.post);
       /* Increment count.  */
-      tmp = fold (build (PLUS_EXPR, TREE_TYPE (count), count,
-                         integer_one_node));
+      tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                         count, gfc_index_one_node));
       gfc_add_modify_expr (&body, count, tmp);
       tmp = gfc_finish_block (&body);
 
-      /* Generate body and loops according to the inforamtion in
+      /* Generate body and loops according to the information in
          nested_forall_info.  */
       tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1, 1);
       gfc_add_expr_to_block (block, tmp);
@@ -1993,8 +2109,8 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
       gfc_add_block_to_block (&body, &lse.post);
 
       /* Increment count.  */
-      tmp = fold (build (PLUS_EXPR, TREE_TYPE (count), count,
-                         integer_one_node));
+      tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                         count, gfc_index_one_node));
       gfc_add_modify_expr (&body, count, tmp);
 
       tmp = gfc_finish_block (&body);
@@ -2006,17 +2122,17 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
           mask = forall_tmp->mask;
           maskindex = forall_tmp->maskindex;
           if (mask)
-            gfc_add_modify_expr (block, maskindex, integer_zero_node);
+            gfc_add_modify_expr (block, maskindex, gfc_index_zero_node);
           forall_tmp = forall_tmp->next_nest;
         }
 
-      /* Generate body and loops according to the inforamtion in
+      /* Generate body and loops according to the information in
          nested_forall_info.  */
       tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1, 1);
       gfc_add_expr_to_block (block, tmp);
 
       /* Reset count.  */
-      gfc_add_modify_expr (block, count, integer_zero_node);
+      gfc_add_modify_expr (block, count, gfc_index_zero_node);
 
       /* Reset maskindexes.  */
       forall_tmp = nested_forall_info;
@@ -2025,7 +2141,7 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
           mask = forall_tmp->mask;
           maskindex = forall_tmp->maskindex;
           if (mask)
-            gfc_add_modify_expr (block, maskindex, integer_zero_node);
+            gfc_add_modify_expr (block, maskindex, gfc_index_zero_node);
           forall_tmp = forall_tmp->next_nest;
         }
       parm = gfc_build_array_ref (tmp1, count);
@@ -2038,8 +2154,8 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
       gfc_add_block_to_block (&body, &lse.post);
 
       /* Increment count.  */
-      tmp = fold (build (PLUS_EXPR, TREE_TYPE (count), count,
-                         integer_one_node));
+      tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                         count, gfc_index_one_node));
       gfc_add_modify_expr (&body, count, tmp);
 
       tmp = gfc_finish_block (&body);
@@ -2070,7 +2186,7 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
       e<i> = f<i>
       g<i> = h<i>
     end forall
-   (where e,f,g,h<i> are arbitary expressions possibly involving i)
+   (where e,f,g,h<i> are arbitrary expressions possibly involving i)
    Translates to:
     count = ((end + 1 - start) / staride)
     masktmp(:) = maskexpr(:)
@@ -2091,7 +2207,7 @@ gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
     Note that this code only works when there are no dependencies.
     Forall loop with array assignments and data dependencies are a real pain,
     because the size of the temporary cannot always be determined before the
-    loop is executed.  This problem is compouded by the presence of nested
+    loop is executed.  This problem is compounded by the presence of nested
     FORALL constructs.
  */
 
@@ -2207,7 +2323,7 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
   /* Work out the number of elements in the mask array.  */
   tmpvar = NULL_TREE;
   lenvar = NULL_TREE;
-  size = integer_one_node;
+  size = gfc_index_one_node;
   sizevar = NULL_TREE;
 
   for (n = 0; n < nvar; n++)
@@ -2216,13 +2332,14 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
        lenvar = NULL_TREE;
 
       /* size = (end + step - start) / step.  */
-      tmp = fold (build (MINUS_EXPR, TREE_TYPE (start[n]), step[n], start[n]));
-      tmp = fold (build (PLUS_EXPR, TREE_TYPE (end[n]), end[n], tmp));
+      tmp = fold (build2 (MINUS_EXPR, TREE_TYPE (start[n]), 
+                         step[n], start[n]));
+      tmp = fold (build2 (PLUS_EXPR, TREE_TYPE (end[n]), end[n], tmp));
 
-      tmp = fold (build (FLOOR_DIV_EXPR, TREE_TYPE (tmp), tmp, step[n]));
+      tmp = fold (build2 (FLOOR_DIV_EXPR, TREE_TYPE (tmp), tmp, step[n]));
       tmp = convert (gfc_array_index_type, tmp);
 
-      size = fold (build (MULT_EXPR, gfc_array_index_type, size, tmp));
+      size = fold (build2 (MULT_EXPR, gfc_array_index_type, size, tmp));
     }
 
   /* Record the nvar and size of current forall level.  */
@@ -2242,12 +2359,12 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
     }
 
   /* Copy the mask into a temporary variable if required.
-     For now we assume a mask temporary is needed. */
+     For now we assume a mask temporary is needed.  */
   if (code->expr)
     {
       /* Allocate the mask temporary.  */
-      bytesize = fold (build (MULT_EXPR, gfc_array_index_type, size,
-                             TYPE_SIZE_UNIT (boolean_type_node)));
+      bytesize = fold (build2 (MULT_EXPR, gfc_array_index_type, size,
+                              TYPE_SIZE_UNIT (boolean_type_node)));
 
       mask = gfc_do_allocate (bytesize, size, &pmask, &block, boolean_type_node);
 
@@ -2257,7 +2374,7 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
       info->mask = mask;
       info->maskindex = maskindex;
 
-      gfc_add_modify_expr (&block, maskindex, integer_zero_node);
+      gfc_add_modify_expr (&block, maskindex, gfc_index_zero_node);
 
       /* Start of mask assignment loop body.  */
       gfc_start_block (&body);
@@ -2278,8 +2395,8 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
       gfc_add_modify_expr (&body, tmp, se.expr);
 
       /* Advance to the next mask element.  */
-      tmp = build (PLUS_EXPR, gfc_array_index_type, maskindex,
-                  integer_one_node);
+      tmp = build2 (PLUS_EXPR, gfc_array_index_type,
+                  maskindex, gfc_index_one_node);
       gfc_add_modify_expr (&body, maskindex, tmp);
 
       /* Generate the loops.  */
@@ -2303,7 +2420,7 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
       switch (c->op)
        {
        case EXEC_ASSIGN:
-          /* A scalar or array assingment.  */
+          /* A scalar or array assignment.  */
          need_temp = gfc_check_dependency (c->expr, c->expr2, varexpr, nvar);
           /* Teporaries due to array assignment data dependencies introduce
              no end of problems.  */
@@ -2317,7 +2434,7 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
 
               /* Reset the mask index.  */
               if (mask)
-                gfc_add_modify_expr (&block, maskindex, integer_zero_node);
+                gfc_add_modify_expr (&block, maskindex, gfc_index_zero_node);
 
               /* Generate body and loops.  */
               tmp = gfc_trans_nested_forall_loop (nested_forall_info, assign, 1, 1);
@@ -2362,7 +2479,7 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
 
               /* Reset the mask index.  */
               if (mask)
-                gfc_add_modify_expr (&block, maskindex, integer_zero_node);
+                gfc_add_modify_expr (&block, maskindex, gfc_index_zero_node);
 
               /* Generate body and loops.  */
               tmp = gfc_trans_nested_forall_loop (nested_forall_info, assign,
@@ -2377,8 +2494,7 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
           break;
 
        default:
-         abort ();
-         break;
+         gcc_unreachable ();
        }
 
       c = c->next;
@@ -2477,8 +2593,8 @@ gfc_evaluate_where_mask (gfc_expr * me, forall_info * nested_forall_info,
 
   /* Variable to index the temporary.  */
   count = gfc_create_var (gfc_array_index_type, "count");
-  /* Initilize count.  */
-  gfc_add_modify_expr (block, count, integer_zero_node);
+  /* Initialize count.  */
+  gfc_add_modify_expr (block, count, gfc_index_zero_node);
 
   gfc_start_block (&body);
 
@@ -2491,7 +2607,7 @@ gfc_evaluate_where_mask (gfc_expr * me, forall_info * nested_forall_info,
     }
   else
     {
-      /* Initiliaze the loop.  */
+      /* Initialize the loop.  */
       gfc_init_loopinfo (&loop);
 
       /* We may need LSS to determine the shape of the expression.  */
@@ -2529,8 +2645,8 @@ gfc_evaluate_where_mask (gfc_expr * me, forall_info * nested_forall_info,
   else
     {
       /* Increment count.  */
-      tmp1 = fold (build (PLUS_EXPR, gfc_array_index_type, count,
-                          integer_one_node));
+      tmp1 = fold (build2 (PLUS_EXPR, gfc_array_index_type, count,
+                          gfc_index_one_node));
       gfc_add_modify_expr (&body1, count, tmp1);
 
       /* Generate the copying loops.  */
@@ -2602,7 +2718,7 @@ gfc_trans_where_assign (gfc_expr *expr1, gfc_expr *expr2, tree mask,
 
   /* In each where-assign-stmt, the mask-expr and the variable being
      defined shall be arrays of the same shape.  */
-  assert (lss != gfc_ss_terminator);
+  gcc_assert (lss != gfc_ss_terminator);
 
   /* The assignment needs scalarization.  */
   lss_section = lss;
@@ -2612,7 +2728,7 @@ gfc_trans_where_assign (gfc_expr *expr1, gfc_expr *expr2, tree mask,
          && lss_section->type != GFC_SS_SECTION)
     lss_section = lss_section->next;
 
-  assert (lss_section != gfc_ss_terminator);
+  gcc_assert (lss_section != gfc_ss_terminator);
 
   /* Initialize the scalarizer.  */
   gfc_init_loopinfo (&loop);
@@ -2684,20 +2800,20 @@ gfc_trans_where_assign (gfc_expr *expr1, gfc_expr *expr2, tree mask,
   while (tmp)
     {
       tmp1 = gfc_build_array_ref (tmp, index);
-      maskexpr = build (TRUTH_AND_EXPR, TREE_TYPE (tmp1), maskexpr, tmp1);
+      maskexpr = build2 (TRUTH_AND_EXPR, TREE_TYPE (tmp1), maskexpr, tmp1);
       tmp = TREE_CHAIN (tmp);
     }
   /* Use the scalar assignment as is.  */
   tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts.type);
-  tmp = build_v (COND_EXPR, maskexpr, tmp, build_empty_stmt ());
+  tmp = build3_v (COND_EXPR, maskexpr, tmp, build_empty_stmt ());
 
   gfc_add_expr_to_block (&body, tmp);
 
   if (lss == gfc_ss_terminator)
     {
       /* Increment count1.  */
-      tmp = fold (build (PLUS_EXPR, TREE_TYPE (count1), count1,
-                         integer_one_node));
+      tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                         count1, gfc_index_one_node));
       gfc_add_modify_expr (&body, count1, tmp);
 
       /* Use the scalar assignment as is.  */
@@ -2705,17 +2821,15 @@ gfc_trans_where_assign (gfc_expr *expr1, gfc_expr *expr2, tree mask,
     }
   else
     {
-      if (lse.ss != gfc_ss_terminator)
-        abort ();
-      if (rse.ss != gfc_ss_terminator)
-        abort ();
+      gcc_assert (lse.ss == gfc_ss_terminator
+                 && rse.ss == gfc_ss_terminator);
 
       if (loop.temp_ss != NULL)
         {
           /* Increment count1 before finish the main body of a scalarized
              expression.  */
-          tmp = fold (build (PLUS_EXPR, TREE_TYPE (count1), count1,
-                             integer_one_node));
+          tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                             count1, gfc_index_one_node));
           gfc_add_modify_expr (&body, count1, tmp);
           gfc_trans_scalarized_loop_boundary (&loop, &body);
 
@@ -2732,11 +2846,8 @@ gfc_trans_where_assign (gfc_expr *expr1, gfc_expr *expr2, tree mask,
           gfc_advance_se_ss_chain (&rse);
           gfc_conv_expr (&lse, expr1);
 
-          if (lse.ss != gfc_ss_terminator)
-            abort ();
-
-          if (rse.ss != gfc_ss_terminator)
-            abort ();
+          gcc_assert (lse.ss == gfc_ss_terminator
+                     && rse.ss == gfc_ss_terminator);
 
           /* Form the mask expression according to the mask tree list.  */
           index = count2;
@@ -2750,24 +2861,25 @@ gfc_trans_where_assign (gfc_expr *expr1, gfc_expr *expr2, tree mask,
           while (tmp)
             {
               tmp1 = gfc_build_array_ref (tmp, index);
-              maskexpr = build (TRUTH_AND_EXPR, TREE_TYPE (tmp1), maskexpr,
-                                tmp1);
+              maskexpr = build2 (TRUTH_AND_EXPR, TREE_TYPE (tmp1),
+                                maskexpr, tmp1);
               tmp = TREE_CHAIN (tmp);
             }
           /* Use the scalar assignment as is.  */
           tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts.type);
-          tmp = build_v (COND_EXPR, maskexpr, tmp, build_empty_stmt ());
+          tmp = build3_v (COND_EXPR, maskexpr, tmp, build_empty_stmt ());
           gfc_add_expr_to_block (&body, tmp);
+
           /* Increment count2.  */
-          tmp = fold (build (PLUS_EXPR, TREE_TYPE (count2), count2,
-                             integer_one_node));
+          tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                             count2, gfc_index_one_node));
           gfc_add_modify_expr (&body, count2, tmp);
         }
       else
         {
           /* Increment count1.  */
-          tmp = fold (build (PLUS_EXPR, TREE_TYPE (count1), count1,
-                             integer_one_node));
+          tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type,
+                             count1, gfc_index_one_node));
           gfc_add_modify_expr (&body, count1, tmp);
         }
 
@@ -2785,7 +2897,7 @@ gfc_trans_where_assign (gfc_expr *expr1, gfc_expr *expr2, tree mask,
 
 
 /* Translate the WHERE construct or statement.
-   This fuction can be called iteratelly to translate the nested WHERE
+   This fuction can be called iteratively to translate the nested WHERE
    construct or statement.
    MASK is the control mask, and PMASK is the pending control mask.
    TEMP records the temporary address which must be freed later.  */
@@ -2876,8 +2988,8 @@ gfc_trans_where_2 (gfc_code * code, tree mask, tree pmask,
                       /* Variables to control maskexpr.  */
                       count1 = gfc_create_var (gfc_array_index_type, "count1");
                       count2 = gfc_create_var (gfc_array_index_type, "count2");
-                      gfc_add_modify_expr (block, count1, integer_zero_node);
-                      gfc_add_modify_expr (block, count2, integer_zero_node);
+                      gfc_add_modify_expr (block, count1, gfc_index_zero_node);
+                      gfc_add_modify_expr (block, count2, gfc_index_zero_node);
 
                       tmp = gfc_trans_where_assign (expr1, expr2, mask, count1,
                                                     count2);
@@ -2891,8 +3003,8 @@ gfc_trans_where_2 (gfc_code * code, tree mask, tree pmask,
                   /* Variables to control maskexpr.  */
                   count1 = gfc_create_var (gfc_array_index_type, "count1");
                   count2 = gfc_create_var (gfc_array_index_type, "count2");
-                  gfc_add_modify_expr (block, count1, integer_zero_node);
-                  gfc_add_modify_expr (block, count2, integer_zero_node);
+                  gfc_add_modify_expr (block, count1, gfc_index_zero_node);
+                  gfc_add_modify_expr (block, count2, gfc_index_zero_node);
 
                   tmp = gfc_trans_where_assign (expr1, expr2, mask, count1,
                                                 count2);
@@ -2910,7 +3022,7 @@ gfc_trans_where_2 (gfc_code * code, tree mask, tree pmask,
               break;
 
             default:
-              abort ();
+              gcc_unreachable ();
             }
 
          /* The next statement within the same where-body-construct.  */
@@ -2924,7 +3036,7 @@ gfc_trans_where_2 (gfc_code * code, tree mask, tree pmask,
 
 /* As the WHERE or WHERE construct statement can be nested, we call
    gfc_trans_where_2 to do the translation, and pass the initial
-   NULL values for both the control mask and the pending control mask. */
+   NULL values for both the control mask and the pending control mask.  */
 
 tree
 gfc_trans_where (gfc_code * code)
@@ -2969,7 +3081,7 @@ gfc_trans_cycle (gfc_code * code)
 }
 
 
-/* EXIT a DO loop. Similair to CYCLE, but now the label is in
+/* EXIT a DO loop. Similar to CYCLE, but now the label is in
    TREE_VALUE (backend_decl) of the gfc_code node at the head of the
    loop.  */
 
@@ -3007,6 +3119,8 @@ gfc_trans_allocate (gfc_code * code)
 
   if (code->expr)
     {
+      tree gfc_int4_type_node = gfc_get_int_type (4);
+
       stat = gfc_create_var (gfc_int4_type_node, "stat");
       pstat = gfc_build_addr_expr (NULL, stat);
 
@@ -3036,7 +3150,7 @@ gfc_trans_allocate (gfc_code * code)
       /* Find the last reference in the chain.  */
       while (ref && ref->next != NULL)
        {
-         assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT);
+         gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT);
          ref = ref->next;
        }
 
@@ -3065,8 +3179,8 @@ gfc_trans_allocate (gfc_code * code)
            {
              tmp = build1_v (GOTO_EXPR, error_label);
              parm =
-               build (NE_EXPR, boolean_type_node, stat, integer_zero_node);
-             tmp = build_v (COND_EXPR, parm, tmp, build_empty_stmt ());
+               build2 (NE_EXPR, boolean_type_node, stat, integer_zero_node);
+             tmp = build3_v (COND_EXPR, parm, tmp, build_empty_stmt ());
              gfc_add_expr_to_block (&se.pre, tmp);
            }
        }
@@ -3107,7 +3221,7 @@ gfc_trans_deallocate (gfc_code * code)
   for (al = code->ext.alloc_list; al != NULL; al = al->next)
     {
       expr = al->expr;
-      assert (expr->expr_type == EXPR_VARIABLE);
+      gcc_assert (expr->expr_type == EXPR_VARIABLE);
 
       gfc_init_se (&se, NULL);
       gfc_start_block (&se.pre);