diff mbox

Refactoring: use std::swap instead of manual swaps (part 2)

Message ID 557DE88A.2050104@gmail.com
State New
Headers show

Commit Message

Mikhail Maltsev June 14, 2015, 8:48 p.m. UTC
Hi all.
The attached patch replaces manual swaps (i.e. "tmp = a; a = b; b =
tmp;") with std::swap. It also removes a couple of static functions
which were used only for implementing such swaps.

Bootstrapped/regtested on x86_64-linux; full target list build in
progress. OK for trunk?
diff mbox

Patch

diff --git a/gcc/auto-inc-dec.c b/gcc/auto-inc-dec.c
index 0bdf91a..1e93e7e 100644
--- a/gcc/auto-inc-dec.c
+++ b/gcc/auto-inc-dec.c
@@ -768,28 +768,6 @@  get_next_ref (int regno, basic_block bb, rtx_insn **next_array)
 }
 
 
-/* Reverse the operands in a mem insn.  */
-
-static void
-reverse_mem (void)
-{
-  rtx tmp = mem_insn.reg1;
-  mem_insn.reg1 = mem_insn.reg0;
-  mem_insn.reg0 = tmp;
-}
-
-
-/* Reverse the operands in a inc insn.  */
-
-static void
-reverse_inc (void)
-{
-  rtx tmp = inc_insn.reg1;
-  inc_insn.reg1 = inc_insn.reg0;
-  inc_insn.reg0 = tmp;
-}
-
-
 /* Return true if INSN is of a form "a = b op c" where a and b are
    regs.  op is + if c is a reg and +|- if c is a const.  Fill in
    INC_INSN with what is found.
@@ -858,7 +836,7 @@  parse_add_or_inc (rtx_insn *insn, bool before_mem)
 	{
 	  /* Reverse the two operands and turn *_ADD into *_INC since
 	     a = c + a.  */
-	  reverse_inc ();
+	  std::swap (inc_insn.reg0, inc_insn.reg1);
 	  inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
 	  return true;
 	}
@@ -1018,7 +996,7 @@  find_inc (bool first_try)
 	 find this.  Only try it once though.  */
       if (first_try && !mem_insn.reg1_is_const)
 	{
-	  reverse_mem ();
+	  std::swap (mem_insn.reg0, mem_insn.reg1);
 	  return find_inc (false);
 	}
       else
@@ -1119,7 +1097,7 @@  find_inc (bool first_try)
 		    return false;
 
 		  if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
-		    reverse_inc ();
+		    std::swap (inc_insn.reg0, inc_insn.reg1);
 		}
 
 	      other_insn
@@ -1169,7 +1147,7 @@  find_inc (bool first_try)
 		  /* See comment above on find_inc (false) call.  */
 		  if (first_try)
 		    {
-		      reverse_mem ();
+		      std::swap (mem_insn.reg0, mem_insn.reg1);
 		      return find_inc (false);
 		    }
 		  else
@@ -1188,7 +1166,7 @@  find_inc (bool first_try)
 	    {
 	      /* We know that mem_insn.reg0 must equal inc_insn.reg1
 		 or else we would not have found the inc insn.  */
-	      reverse_mem ();
+	      std::swap (mem_insn.reg0, mem_insn.reg1);
 	      if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
 		{
 		  /* See comment above on find_inc (false) call.  */
@@ -1227,7 +1205,7 @@  find_inc (bool first_try)
 	    {
 	      if (first_try)
 		{
-		  reverse_mem ();
+		  std::swap (mem_insn.reg0, mem_insn.reg1);
 		  return find_inc (false);
 		}
 	      else
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index b1af682..aba2919 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -12608,11 +12608,9 @@  scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
       /* What about UNLT_EXPR?  */
 	if (TREE_CODE (type0) == VECTOR_TYPE)
 	  {
-	    tree tmp;
 	    ret = stv_secondarg;
-	    /* Swap TYPE0 with TYPE1 and OP0 with OP1  */
-	    tmp = type0; type0 = type1; type1 = tmp;
-	    tmp = op0; op0 = op1; op1 = tmp;
+	    std::swap (type0, type1);
+	    std::swap (op0, op1);
 	  }
 
 	if (TREE_CODE (type0) == INTEGER_TYPE
diff --git a/gcc/combine.c b/gcc/combine.c
index bce8430..fe71b4d 100644
--- a/gcc/combine.c
+++ b/gcc/combine.c
@@ -5686,11 +5686,7 @@  combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
 	  /* Make sure we pass the constant operand if any as the second
 	     one if this is a commutative operation.  */
 	  if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
-	    {
-	      rtx tem = inner_op0;
-	      inner_op0 = inner_op1;
-	      inner_op1 = tem;
-	    }
+	    std::swap (inner_op0, inner_op1);
 	  inner = simplify_binary_operation (code == MINUS ? PLUS
 					     : code == DIV ? MULT
 					     : code,
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index ea8c8b6..b0de2bf 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -16498,15 +16498,11 @@  maybe_adjust_types_for_deduction (unification_kind_t strict,
       break;
 
     case DEDUCE_CONV:
-      {
-	/* Swap PARM and ARG throughout the remainder of this
-	   function; the handling is precisely symmetric since PARM
-	   will initialize ARG rather than vice versa.  */
-	tree* temp = parm;
-	parm = arg;
-	arg = temp;
-	break;
-      }
+      /* Swap PARM and ARG throughout the remainder of this
+	 function; the handling is precisely symmetric since PARM
+	 will initialize ARG rather than vice versa.  */
+      std::swap (parm, arg);
+      break;
 
     case DEDUCE_EXACT:
       /* Core issue #873: Do the DR606 thing (see below) for these cases,
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 59ec9047..0cdb18d 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -6871,9 +6871,7 @@  finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
       bool swapped = false;
       if (rhs1 && cp_tree_equal (lhs, rhs))
 	{
-	  tree tem = rhs;
-	  rhs = rhs1;
-	  rhs1 = tem;
+	  std::swap (rhs, rhs1);
 	  swapped = !commutative_tree_code (opcode);
 	}
       if (rhs1 && !cp_tree_equal (lhs, rhs1))
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 7716c21..b43419a 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -3179,11 +3179,7 @@  cp_build_array_ref (location_t loc, tree array, tree idx,
 
     /* Put the integer in IND to simplify error checking.  */
     if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
-      {
-	tree temp = ar;
-	ar = ind;
-	ind = temp;
-      }
+      std::swap (ar, ind);
 
     if (ar == error_mark_node || ind == error_mark_node)
       return error_mark_node;
diff --git a/gcc/df-core.c b/gcc/df-core.c
index 702a112..f435fc6 100644
--- a/gcc/df-core.c
+++ b/gcc/df-core.c
@@ -1043,10 +1043,7 @@  df_worklist_dataflow_doublequeue (struct dataflow *dataflow,
       bitmap_iterator bi;
       unsigned int index;
 
-      /* Swap pending and worklist. */
-      bitmap temp = worklist;
-      worklist = pending;
-      pending = temp;
+      std::swap (pending, worklist);
 
       EXECUTE_IF_SET_IN_BITMAP (worklist, 0, index, bi)
 	{
diff --git a/gcc/df-scan.c b/gcc/df-scan.c
index 91f6aca..d79d135 100644
--- a/gcc/df-scan.c
+++ b/gcc/df-scan.c
@@ -2135,14 +2135,6 @@  df_ref_ptr_compare (const void *r1, const void *r2)
   return df_ref_compare (*(const df_ref *) r1, *(const df_ref *) r2);
 }
 
-static void
-df_swap_refs (vec<df_ref, va_heap> *ref_vec, int i, int j)
-{
-  df_ref tmp = (*ref_vec)[i];
-  (*ref_vec)[i] = (*ref_vec)[j];
-  (*ref_vec)[j] = tmp;
-}
-
 /* Sort and compress a set of refs.  */
 
 static void
@@ -2162,7 +2154,7 @@  df_sort_and_compress_refs (vec<df_ref, va_heap> *ref_vec)
       df_ref r0 = (*ref_vec)[0];
       df_ref r1 = (*ref_vec)[1];
       if (df_ref_compare (r0, r1) > 0)
-        df_swap_refs (ref_vec, 0, 1);
+	std::swap ((*ref_vec)[0], (*ref_vec)[1]);
     }
   else
     {
diff --git a/gcc/dominance.c b/gcc/dominance.c
index 23f107c..fd24c2a 100644
--- a/gcc/dominance.c
+++ b/gcc/dominance.c
@@ -485,11 +485,7 @@  link_roots (struct dom_info *di, TBB v, TBB w)
   di->path_min[s] = di->path_min[w];
   di->set_size[v] += di->set_size[w];
   if (di->set_size[v] < 2 * di->set_size[w])
-    {
-      TBB tmp = s;
-      s = di->set_child[v];
-      di->set_child[v] = tmp;
-    }
+    std::swap (di->set_child[v], s);
 
   /* Merge all subtrees.  */
   while (s)
diff --git a/gcc/expr.c b/gcc/expr.c
index 26b54a8..f8e5996 100644
--- a/gcc/expr.c
+++ b/gcc/expr.c
@@ -8428,11 +8428,7 @@  expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
 	 Thus the following special case checks need only
 	 check the second operand.  */
       if (TREE_CODE (treeop0) == INTEGER_CST)
-	{
-	  tree t1 = treeop0;
-	  treeop0 = treeop1;
-	  treeop1 = t1;
-	}
+	std::swap (treeop0, treeop1);
 
       /* First, check if we have a multiplication of one signed and one
 	 unsigned operand.  */
@@ -8557,11 +8553,7 @@  expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
 	    def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
 	    /* Swap operands if the 2nd operand is fed by a negate.  */
 	    if (def0)
-	      {
-		tree tem = treeop0;
-		treeop0 = treeop1;
-		treeop1 = tem;
-	      }
+	      std::swap (treeop0, treeop1);
 	  }
 	def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
 
@@ -8608,11 +8600,7 @@  expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
 	 Thus the following special case checks need only
 	 check the second operand.  */
       if (TREE_CODE (treeop0) == INTEGER_CST)
-	{
-	  tree t1 = treeop0;
-	  treeop0 = treeop1;
-	  treeop1 = t1;
-	}
+	std::swap (treeop0, treeop1);
 
       /* Attempt to return something suitable for generating an
 	 indexed address, for machines that support that.  */
@@ -10912,7 +10900,6 @@  do_store_flag (sepops ops, rtx target, machine_mode mode)
 {
   enum rtx_code code;
   tree arg0, arg1, type;
-  tree tem;
   machine_mode operand_mode;
   int unsignedp;
   rtx op0, op1;
@@ -11035,7 +11022,7 @@  do_store_flag (sepops ops, rtx target, machine_mode mode)
   if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
       || TREE_CODE (arg0) == FIXED_CST)
     {
-      tem = arg0; arg0 = arg1; arg1 = tem;
+      std::swap (arg0, arg1);
       code = swap_condition (code);
     }
 
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index dffd935..06fdff8 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -15666,9 +15666,7 @@  fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
 
   if (code == LE_EXPR || code == GT_EXPR)
     {
-      tree tem = op0;
-      op0 = op1;
-      op1 = tem;
+      std::swap (op0, op1);
       code = swap_tree_comparison (code);
     }
 
diff --git a/gcc/genattrtab.c b/gcc/genattrtab.c
index 2beb7ee..1a4dd38 100644
--- a/gcc/genattrtab.c
+++ b/gcc/genattrtab.c
@@ -2532,11 +2532,7 @@  simplify_test_exp (rtx exp, int insn_code, int insn_index)
 	  && compute_alternative_mask (right, IOR))
 	{
 	  if (GET_CODE (left) == IOR)
-	    {
-	      rtx tem = left;
-	      left = right;
-	      right = tem;
-	    }
+	    std::swap (left, right);
 
 	  newexp = attr_rtx (IOR,
 			     attr_rtx (AND, left, XEXP (right, 0)),
diff --git a/gcc/gimple-match-head.c b/gcc/gimple-match-head.c
index 8fbb489..3fbd6f1 100644
--- a/gcc/gimple-match-head.c
+++ b/gcc/gimple-match-head.c
@@ -192,9 +192,7 @@  gimple_resimplify2 (gimple_seq *seq,
 	  || commutative_tree_code (*res_code))
       && tree_swap_operands_p (res_ops[0], res_ops[1], false))
     {
-      tree tem = res_ops[0];
-      res_ops[0] = res_ops[1];
-      res_ops[1] = tem;
+      std::swap (res_ops[0], res_ops[1]);
       if (TREE_CODE_CLASS ((enum tree_code) *res_code) == tcc_comparison)
 	*res_code = swap_tree_comparison (*res_code);
       canonicalized = true;
@@ -264,9 +262,7 @@  gimple_resimplify3 (gimple_seq *seq,
       && commutative_ternary_tree_code (*res_code)
       && tree_swap_operands_p (res_ops[0], res_ops[1], false))
     {
-      tree tem = res_ops[0];
-      res_ops[0] = res_ops[1];
-      res_ops[1] = tem;
+      std::swap (res_ops[0], res_ops[1]);
       canonicalized = true;
     }
 
@@ -429,9 +425,7 @@  gimple_simplify (enum tree_code code, tree type,
        || TREE_CODE_CLASS (code) == tcc_comparison)
       && tree_swap_operands_p (op0, op1, false))
     {
-      tree tem = op0;
-      op0 = op1;
-      op1 = tem;
+      std::swap (op0, op1);
       if (TREE_CODE_CLASS (code) == tcc_comparison)
 	code = swap_tree_comparison (code);
     }
@@ -464,11 +458,7 @@  gimple_simplify (enum tree_code code, tree type,
      generation.  */
   if (commutative_ternary_tree_code (code)
       && tree_swap_operands_p (op0, op1, false))
-    {
-      tree tem = op0;
-      op0 = op1;
-      op1 = tem;
-    }
+    std::swap (op0, op1);
 
   code_helper rcode;
   tree ops[3] = {};
diff --git a/gcc/ifcvt.c b/gcc/ifcvt.c
index 50ccaa1..adb9cfe 100644
--- a/gcc/ifcvt.c
+++ b/gcc/ifcvt.c
@@ -2066,7 +2066,7 @@  noce_try_abs (struct noce_if_info *if_info)
     negate = 0;
   else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
     {
-      c = a; a = b; b = c;
+      std::swap (a, b);
       negate = 1;
     }
   else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
@@ -2076,7 +2076,7 @@  noce_try_abs (struct noce_if_info *if_info)
     }
   else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
     {
-      c = a; a = b; b = c;
+      std::swap (a, b);
       negate = 1;
       one_cmpl = true;
     }
@@ -3391,11 +3391,7 @@  find_if_header (basic_block test_bb, int pass)
   if (then_edge->flags & EDGE_FALLTHRU)
     ;
   else if (else_edge->flags & EDGE_FALLTHRU)
-    {
-      edge e = else_edge;
-      else_edge = then_edge;
-      then_edge = e;
-    }
+    std::swap (then_edge, else_edge);
   else
     /* Otherwise this must be a multiway branch of some sort.  */
     return NULL;
diff --git a/gcc/internal-fn.c b/gcc/internal-fn.c
index 9ac8827..63e28b6 100644
--- a/gcc/internal-fn.c
+++ b/gcc/internal-fn.c
@@ -528,14 +528,10 @@  expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
       /* PLUS_EXPR is commutative, if operand signedness differs,
 	 canonicalize to the first operand being signed and second
 	 unsigned to simplify following code.  */
-      rtx tem = op1;
-      op1 = op0;
-      op0 = tem;
-      tree t = arg1;
-      arg1 = arg0;
-      arg0 = t;
-      uns0_p = 0;
-      uns1_p = 1;
+      std::swap (op0, op1);
+      std::swap (arg0, arg1);
+      uns0_p = false;
+      uns1_p = true;
     }
 
   /* u1 +- u2 -> ur  */
@@ -676,9 +672,7 @@  expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
 	  int pos_neg0 = get_range_pos_neg (arg0);
 	  if (pos_neg0 != 3 && pos_neg == 3)
 	    {
-	      rtx tem = op1;
-	      op1 = op0;
-	      op0 = tem;
+	      std::swap (op0, op1);
 	      pos_neg = pos_neg0;
 	    }
 	}
@@ -783,22 +777,14 @@  expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
 	 do_compare_rtx_and_jump will be just folded.  Otherwise try
 	 to use range info if available.  */
       if (code == PLUS_EXPR && CONST_INT_P (op0))
-	{
-	  rtx tem = op0;
-	  op0 = op1;
-	  op1 = tem;
-	}
+	std::swap (op0, op1);
       else if (CONST_INT_P (op1))
 	;
       else if (code == PLUS_EXPR && TREE_CODE (arg0) == SSA_NAME)
 	{
 	  pos_neg = get_range_pos_neg (arg0);
 	  if (pos_neg != 3)
-	    {
-	      rtx tem = op0;
-	      op0 = op1;
-	      op1 = tem;
-	    }
+	    std::swap (op0, op1);
 	}
       if (pos_neg == 3 && !CONST_INT_P (op1) && TREE_CODE (arg1) == SSA_NAME)
 	pos_neg = get_range_pos_neg (arg1);
@@ -1025,14 +1011,10 @@  expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
       /* Multiplication is commutative, if operand signedness differs,
 	 canonicalize to the first operand being signed and second
 	 unsigned to simplify following code.  */
-      rtx tem = op1;
-      op1 = op0;
-      op0 = tem;
-      tree t = arg1;
-      arg1 = arg0;
-      arg0 = t;
-      uns0_p = 0;
-      uns1_p = 1;
+      std::swap (op0, op1);
+      std::swap (arg0, arg1);
+      uns0_p = false;
+      uns1_p = true;
     }
 
   int pos_neg0 = get_range_pos_neg (arg0);
diff --git a/gcc/ipa-devirt.c b/gcc/ipa-devirt.c
index d7d97ec..f5ae554 100644
--- a/gcc/ipa-devirt.c
+++ b/gcc/ipa-devirt.c
@@ -1848,12 +1848,7 @@  add_type_duplicate (odr_type val, tree type)
     }
 
   if (prevail)
-    {
-      tree tmp = type;
-
-      type = val->type;
-      val->type = tmp;
-    }
+    std::swap (val->type, type);
 
   val->types_set->add (type);
 
diff --git a/gcc/loop-iv.c b/gcc/loop-iv.c
index fdd3323..7dc7135 100644
--- a/gcc/loop-iv.c
+++ b/gcc/loop-iv.c
@@ -665,7 +665,7 @@  get_biv_step_1 (df_ref def, rtx reg,
 		rtx *outer_step)
 {
   rtx set, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
-  rtx next, nextr, tmp;
+  rtx next, nextr;
   enum rtx_code code;
   rtx_insn *insn = DF_REF_INSN (def);
   df_ref next_def;
@@ -695,9 +695,7 @@  get_biv_step_1 (df_ref def, rtx reg,
       op1 = XEXP (rhs, 1);
 
       if (code == PLUS && CONSTANT_P (op0))
-	{
-	  tmp = op0; op0 = op1; op1 = tmp;
-	}
+	std::swap (op0, op1);
 
       if (!simple_reg_p (op0)
 	  || !CONSTANT_P (op1))
@@ -2348,7 +2346,7 @@  iv_number_of_iterations (struct loop *loop, rtx_insn *insn, rtx condition,
 			 struct niter_desc *desc)
 {
   rtx op0, op1, delta, step, bound, may_xform, tmp, tmp0, tmp1;
-  struct rtx_iv iv0, iv1, tmp_iv;
+  struct rtx_iv iv0, iv1;
   rtx assumption, may_not_xform;
   enum rtx_code cond;
   machine_mode mode, comp_mode;
@@ -2411,7 +2409,7 @@  iv_number_of_iterations (struct loop *loop, rtx_insn *insn, rtx condition,
       case GT:
       case GEU:
       case GTU:
-	tmp_iv = iv0; iv0 = iv1; iv1 = tmp_iv;
+	std::swap (iv0, iv1);
 	cond = swap_condition (cond);
 	break;
       case NE:
diff --git a/gcc/lra-lives.c b/gcc/lra-lives.c
index 66baadd..647dc46 100644
--- a/gcc/lra-lives.c
+++ b/gcc/lra-lives.c
@@ -573,16 +573,10 @@  lra_setup_reload_pseudo_preferenced_hard_reg (int regno,
       && (lra_reg_info[regno].preferred_hard_regno_profit2
 	  > lra_reg_info[regno].preferred_hard_regno_profit1))
     {
-      int temp;
-
-      temp = lra_reg_info[regno].preferred_hard_regno1;
-      lra_reg_info[regno].preferred_hard_regno1
-	= lra_reg_info[regno].preferred_hard_regno2;
-      lra_reg_info[regno].preferred_hard_regno2 = temp;
-      temp = lra_reg_info[regno].preferred_hard_regno_profit1;
-      lra_reg_info[regno].preferred_hard_regno_profit1
-	= lra_reg_info[regno].preferred_hard_regno_profit2;
-      lra_reg_info[regno].preferred_hard_regno_profit2 = temp;
+      std::swap (lra_reg_info[regno].preferred_hard_regno1,
+		 lra_reg_info[regno].preferred_hard_regno2);
+      std::swap (lra_reg_info[regno].preferred_hard_regno_profit1,
+		 lra_reg_info[regno].preferred_hard_regno_profit2);
     }
   if (lra_dump_file != NULL)
     {
diff --git a/gcc/lra.c b/gcc/lra.c
index e44cec0..58fa37e 100644
--- a/gcc/lra.c
+++ b/gcc/lra.c
@@ -1378,11 +1378,8 @@  lra_create_copy (int regno1, int regno2, int freq)
   regno1_dest_p = true;
   if (regno1 > regno2)
     {
-      int temp = regno2;
-
+      std::swap (regno1, regno2);
       regno1_dest_p = false;
-      regno2 = regno1;
-      regno1 = temp;
     }
   cp = new lra_copy ();
   copy_vec.safe_push (cp);
diff --git a/gcc/lto-streamer-out.c b/gcc/lto-streamer-out.c
index 9ee4312..eef974c 100644
--- a/gcc/lto-streamer-out.c
+++ b/gcc/lto-streamer-out.c
@@ -610,11 +610,8 @@  DFS::DFS (struct output_block *ob, tree expr, bool ref_p, bool this_ref_p,
 		    }
 		}
 	      for (unsigned i = 0; i < scc_entry_len; ++i)
-		{
-		  scc_entry tem = sccstack[first + i];
-		  sccstack[first + i] = sccstack[first + entry_start + i];
-		  sccstack[first + entry_start + i] = tem;
-		}
+		std::swap (sccstack[first + i],
+			   sccstack[first + entry_start + i]);
 
 	      if (scc_entry_len == 1)
 		; /* We already sorted SCC deterministically in hash_scc.  */
diff --git a/gcc/modulo-sched.c b/gcc/modulo-sched.c
index 94b79c3..9a69538 100644
--- a/gcc/modulo-sched.c
+++ b/gcc/modulo-sched.c
@@ -2007,9 +2007,7 @@  get_sched_window (partial_schedule_ptr ps, ddg_node_ptr u_node,
      node close to its successors.  */
   if (pss_not_empty && count_succs >= count_preds)
     {
-      int tmp = end;
-      end = start;
-      start = tmp;
+      std::swap (start, end);
       step = -1;
     }
 
diff --git a/gcc/omega.c b/gcc/omega.c
index f940da5..2bd61f4 100644
--- a/gcc/omega.c
+++ b/gcc/omega.c
@@ -548,11 +548,7 @@  omega_pretty_print_problem (FILE *file, omega_pb pb)
 		else
 		  {
 		    if (pb->geqs[e].coef[v1] == 1)
-		      {
-			v3 = v2;
-			v2 = v1;
-			v1 = v3;
-		      }
+		      std::swap (v1, v2);
 
 		    /* Relation is v1 <= v2 or v1 < v2.  */
 		    po[v1][v2] = ((pb->geqs[e].coef[0] == 0) ? le : lt);
diff --git a/gcc/optabs.c b/gcc/optabs.c
index da5dfa4..e0c80ae 100644
--- a/gcc/optabs.c
+++ b/gcc/optabs.c
@@ -4384,8 +4384,7 @@  prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
       if (code_to_optab (swapped)
 	  && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
 	{
-	  rtx tmp;
-	  tmp = x; x = y; y = tmp;
+	  std::swap (x, y);
 	  comparison = swapped;
 	  break;
 	}
@@ -6968,11 +6967,7 @@  expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
       tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
       tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
       if (BYTES_BIG_ENDIAN)
-	{
-	  optab t = tab1;
-	  tab1 = tab2;
-	  tab2 = t;
-	}
+	std::swap (tab1, tab2);
       break;
     default:
       gcc_unreachable ();
diff --git a/gcc/reload1.c b/gcc/reload1.c
index 1e29a49..4b1251f 100644
--- a/gcc/reload1.c
+++ b/gcc/reload1.c
@@ -5613,11 +5613,7 @@  reloads_unique_chain_p (int r1, int r2)
 
   /* The following loop assumes that r1 is the reload that feeds r2.  */
   if (r1 > r2)
-    {
-      int tmp = r2;
-      r2 = r1;
-      r1 = tmp;
-    }
+    std::swap (r1, r2);
 
   for (i = 0; i < n_reloads; i ++)
     /* Look for input reloads that aren't our two */
diff --git a/gcc/sel-sched-ir.c b/gcc/sel-sched-ir.c
index ae6f637..56e0d71 100644
--- a/gcc/sel-sched-ir.c
+++ b/gcc/sel-sched-ir.c
@@ -4415,51 +4415,17 @@  free_data_sets (basic_block bb)
   free_av_set (bb);
 }
 
-/* Exchange lv sets of TO and FROM.  */
-static void
-exchange_lv_sets (basic_block to, basic_block from)
-{
-  {
-    regset to_lv_set = BB_LV_SET (to);
-
-    BB_LV_SET (to) = BB_LV_SET (from);
-    BB_LV_SET (from) = to_lv_set;
-  }
-
-  {
-    bool to_lv_set_valid_p = BB_LV_SET_VALID_P (to);
-
-    BB_LV_SET_VALID_P (to) = BB_LV_SET_VALID_P (from);
-    BB_LV_SET_VALID_P (from) = to_lv_set_valid_p;
-  }
-}
-
-
-/* Exchange av sets of TO and FROM.  */
-static void
-exchange_av_sets (basic_block to, basic_block from)
-{
-  {
-    av_set_t to_av_set = BB_AV_SET (to);
-
-    BB_AV_SET (to) = BB_AV_SET (from);
-    BB_AV_SET (from) = to_av_set;
-  }
-
-  {
-    int to_av_level = BB_AV_LEVEL (to);
-
-    BB_AV_LEVEL (to) = BB_AV_LEVEL (from);
-    BB_AV_LEVEL (from) = to_av_level;
-  }
-}
-
 /* Exchange data sets of TO and FROM.  */
 void
 exchange_data_sets (basic_block to, basic_block from)
 {
-  exchange_lv_sets (to, from);
-  exchange_av_sets (to, from);
+  /* Exchange lv sets of TO and FROM.  */
+  std::swap (BB_LV_SET (from), BB_LV_SET (to));
+  std::swap (BB_LV_SET_VALID_P (from), BB_LV_SET_VALID_P (to));
+
+  /* Exchange av sets of TO and FROM.  */
+  std::swap (BB_AV_SET (from), BB_AV_SET (to));
+  std::swap (BB_AV_LEVEL (from), BB_AV_LEVEL (to));
 }
 
 /* Copy data sets of FROM to TO.  */
diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c
index a572ff0..934eacd 100644
--- a/gcc/simplify-rtx.c
+++ b/gcc/simplify-rtx.c
@@ -945,10 +945,7 @@  simplify_unary_operation_1 (enum rtx_code code, machine_mode mode, rtx op)
 	  in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
 
 	  if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
-	    {
-	      rtx tem = in2;
-	      in2 = in1; in1 = tem;
-	    }
+	    std::swap (in1, in2);
 
 	  return gen_rtx_fmt_ee (GET_CODE (op) == IOR ? AND : IOR,
 				 mode, in1, in2);
diff --git a/gcc/tree-if-conv.c b/gcc/tree-if-conv.c
index 050287e..bba6644 100644
--- a/gcc/tree-if-conv.c
+++ b/gcc/tree-if-conv.c
@@ -1512,11 +1512,7 @@  is_cond_scalar_reduction (gimple phi, gimple *reduc, tree arg_0, tree arg_1,
   /* Make R_OP1 to hold reduction variable.  */
   if (r_op2 == PHI_RESULT (header_phi)
       && reduction_op == PLUS_EXPR)
-    {
-      tree tmp = r_op1;
-      r_op1 = r_op2;
-      r_op2 = tmp;
-    }
+    std::swap (r_op1, r_op2);
   else if (r_op1 != PHI_RESULT (header_phi))
     return false;
 
@@ -1710,11 +1706,7 @@  predicate_scalar_phi (gphi *phi, gimple_stmt_iterator *gsi)
       second_edge = EDGE_PRED (bb, 1);
       cond = bb_predicate (first_edge->src);
       if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
-	{
-	  edge tmp_edge = first_edge;
-	  first_edge = second_edge;
-	  second_edge = tmp_edge;
-	}
+	std::swap (first_edge, second_edge);
       if (EDGE_COUNT (first_edge->src->succs) > 1)
 	{
 	  cond = bb_predicate (second_edge->src);
@@ -1797,11 +1789,7 @@  predicate_scalar_phi (gphi *phi, gimple_stmt_iterator *gsi)
 
   /* Put element with max number of occurences to the end of ARGS.  */
   if (max_ind != -1 && max_ind +1 != (int) args_len)
-    {
-      tree tmp = args[args_len - 1];
-      args[args_len - 1] = args[max_ind];
-      args[max_ind] = tmp;
-    }
+    std::swap (args[args_len - 1], args[max_ind]);
 
   /* Handle one special case when number of arguments with different values
      is equal 2 and one argument has the only occurrence.  Such PHI can be
@@ -2176,11 +2164,7 @@  predicate_mem_writes (loop_p loop)
 	    lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
 	    rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
 	    if (swap)
-	      {
-		tree tem = lhs;
-		lhs = rhs;
-		rhs = tem;
-	      }
+	      std::swap (lhs, rhs);
 	    cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
 					       is_gimple_condexpr, NULL_TREE,
 					       true, GSI_SAME_STMT);
diff --git a/gcc/tree-loop-distribution.c b/gcc/tree-loop-distribution.c
index 5c203db..6892ebd 100644
--- a/gcc/tree-loop-distribution.c
+++ b/gcc/tree-loop-distribution.c
@@ -1363,9 +1363,7 @@  pg_add_dependence_edges (struct graph *rdg, vec<loop_p> loops, int dir,
 	if (rdg_vertex_for_stmt (rdg, DR_STMT (dr1))
 	    > rdg_vertex_for_stmt (rdg, DR_STMT (dr2)))
 	  {
-	    data_reference_p tem = dr1;
-	    dr1 = dr2;
-	    dr2 = tem;
+	    std::swap (dr1, dr2);
 	    this_dir = -this_dir;
 	  }
 	ddr = initialize_data_dependence_relation (dr1, dr2, loops);
@@ -1376,9 +1374,7 @@  pg_add_dependence_edges (struct graph *rdg, vec<loop_p> loops, int dir,
 	  {
 	    if (DDR_REVERSED_P (ddr))
 	      {
-		data_reference_p tem = dr1;
-		dr1 = dr2;
-		dr2 = tem;
+		std::swap (dr1, dr2);
 		this_dir = -this_dir;
 	      }
 	    /* Known dependences can still be unordered througout the
diff --git a/gcc/tree-predcom.c b/gcc/tree-predcom.c
index 4415fe3..6a537c6 100644
--- a/gcc/tree-predcom.c
+++ b/gcc/tree-predcom.c
@@ -2303,11 +2303,7 @@  combine_chains (chain_p ch1, chain_p ch2)
     }
 
   if (swap)
-    {
-      chain_p tmp = ch1;
-      ch1 = ch2;
-      ch2 = tmp;
-    }
+    std::swap (ch1, ch2);
 
   new_chain = XCNEW (struct chain);
   new_chain->type = CT_COMBINATION;
diff --git a/gcc/tree-ssa-alias.c b/gcc/tree-ssa-alias.c
index b2d29ca..2e47d02 100644
--- a/gcc/tree-ssa-alias.c
+++ b/gcc/tree-ssa-alias.c
@@ -955,11 +955,7 @@  nonoverlapping_component_refs_p (const_tree x, const_tree y)
   if (fieldsx.length () == 2)
     {
       if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
-	{
-	  const_tree tem = fieldsx[0];
-	  fieldsx[0] = fieldsx[1];
-	  fieldsx[1] = tem;
-	}
+	std::swap (fieldsx[0], fieldsx[1]);
     }
   else
     fieldsx.qsort (ncr_compar);
@@ -967,11 +963,7 @@  nonoverlapping_component_refs_p (const_tree x, const_tree y)
   if (fieldsy.length () == 2)
     {
       if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
-	{
-	  const_tree tem = fieldsy[0];
-	  fieldsy[0] = fieldsy[1];
-	  fieldsy[1] = tem;
-	}
+	std::swap (fieldsy[0], fieldsy[1]);
     }
   else
     fieldsy.qsort (ncr_compar);
@@ -1428,13 +1420,10 @@  refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
   /* Canonicalize the pointer-vs-decl case.  */
   if (ind1_p && var2_p)
     {
-      HOST_WIDE_INT tmp1;
-      tree tmp2;
-      ao_ref *tmp3;
-      tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
-      tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
-      tmp2 = base1; base1 = base2; base2 = tmp2;
-      tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
+      std::swap (offset1, offset2);
+      std::swap (max_size1, max_size2);
+      std::swap (base1, base2);
+      std::swap (ref1, ref2);
       var1_p = true;
       ind1_p = false;
       var2_p = false;
diff --git a/gcc/tree-ssa-ifcombine.c b/gcc/tree-ssa-ifcombine.c
index a224551..eddca75 100644
--- a/gcc/tree-ssa-ifcombine.c
+++ b/gcc/tree-ssa-ifcombine.c
@@ -101,11 +101,7 @@  recognize_if_then_else (basic_block cond_bb,
   t = EDGE_SUCC (cond_bb, 0);
   e = EDGE_SUCC (cond_bb, 1);
   if (!(t->flags & EDGE_TRUE_VALUE))
-    {
-      edge tmp = t;
-      t = e;
-      e = tmp;
-    }
+    std::swap (t, e);
   if (!(t->flags & EDGE_TRUE_VALUE)
       || !(e->flags & EDGE_FALSE_VALUE))
     return false;
diff --git a/gcc/tree-ssa-loop-ivopts.c b/gcc/tree-ssa-loop-ivopts.c
index e94a0bc..d6bc205 100644
--- a/gcc/tree-ssa-loop-ivopts.c
+++ b/gcc/tree-ssa-loop-ivopts.c
@@ -1487,8 +1487,8 @@  extract_cond_operands (struct ivopts_data *data, gimple stmt,
   /* The objects returned when COND has constant operands.  */
   static struct iv const_iv;
   static tree zero;
-  tree *op0 = &zero, *op1 = &zero, *tmp_op;
-  struct iv *iv0 = &const_iv, *iv1 = &const_iv, *tmp_iv;
+  tree *op0 = &zero, *op1 = &zero;
+  struct iv *iv0 = &const_iv, *iv1 = &const_iv;
   bool ret = false;
 
   if (gimple_code (stmt) == GIMPLE_COND)
@@ -1519,8 +1519,8 @@  extract_cond_operands (struct ivopts_data *data, gimple stmt,
   if (integer_zerop (iv0->step))
     {
       /* Control variable may be on the other side.  */
-      tmp_op = op0; op0 = op1; op1 = tmp_op;
-      tmp_iv = iv0; iv0 = iv1; iv1 = tmp_iv;
+      std::swap (op0, op1);
+      std::swap (iv0, iv1);
     }
   ret = !integer_zerop (iv0->step) && integer_zerop (iv1->step);
 
diff --git a/gcc/tree-ssa-loop-niter.c b/gcc/tree-ssa-loop-niter.c
index 7104e60..a436ea7 100644
--- a/gcc/tree-ssa-loop-niter.c
+++ b/gcc/tree-ssa-loop-niter.c
@@ -75,8 +75,6 @@  along with GCC; see the file COPYING3.  If not see
 #include "wide-int-print.h"
 
 
-#define SWAP(X, Y) do { affine_iv *tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
-
 /* The maximum number of dominator BBs we search for conditions
    of loop header copies we use for simplifying a conditional
    expression.  */
@@ -303,7 +301,7 @@  refine_bounds_using_guard (tree type, tree varx, mpz_t offx,
 			   tree c0, enum tree_code cmp, tree c1,
 			   bounds *bnds)
 {
-  tree varc0, varc1, tmp, ctype;
+  tree varc0, varc1, ctype;
   mpz_t offc0, offc1, loffx, loffy, bnd;
   bool lbound = false;
   bool no_wrap = nowrap_type_p (type);
@@ -373,7 +371,7 @@  refine_bounds_using_guard (tree type, tree varx, mpz_t offx,
 
   if (operand_equal_p (varx, varc1, 0))
     {
-      tmp = varc0; varc0 = varc1; varc1 = tmp;
+      std::swap (varc0, varc1);
       mpz_swap (offc0, offc1);
       cmp = swap_tree_comparison (cmp);
     }
@@ -387,7 +385,7 @@  refine_bounds_using_guard (tree type, tree varx, mpz_t offx,
 
   if (cmp == GT_EXPR || cmp == GE_EXPR)
     {
-      tmp = varx; varx = vary; vary = tmp;
+      std::swap (varx, vary);
       mpz_swap (offc0, offc1);
       mpz_swap (loffx, loffy);
       cmp = swap_tree_comparison (cmp);
@@ -1367,7 +1365,7 @@  number_of_iterations_cond (struct loop *loop,
   if (code == GE_EXPR || code == GT_EXPR
       || (code == NE_EXPR && integer_zerop (iv0->step)))
     {
-      SWAP (iv0, iv1);
+      std::swap (iv0, iv1);
       code = swap_tree_comparison (code);
     }
 
diff --git a/gcc/tree-ssa-phiopt.c b/gcc/tree-ssa-phiopt.c
index e26e686..dff6b2b 100644
--- a/gcc/tree-ssa-phiopt.c
+++ b/gcc/tree-ssa-phiopt.c
@@ -239,12 +239,8 @@  tree_ssa_phiopt_worker (bool do_store_elim, bool do_hoist_loads)
         ;
       else if (EDGE_SUCC (bb2, 0)->dest == bb1)
         {
-	  basic_block bb_tmp = bb1;
-	  edge e_tmp = e1;
-	  bb1 = bb2;
-	  bb2 = bb_tmp;
-	  e1 = e2;
-	  e2 = e_tmp;
+	  std::swap (bb1, bb2);
+	  std::swap (e1, e2);
 	}
       else if (do_store_elim
 	       && EDGE_SUCC (bb1, 0)->dest == EDGE_SUCC (bb2, 0)->dest)
diff --git a/gcc/tree-ssa-sccvn.c b/gcc/tree-ssa-sccvn.c
index 238cf30..f4148a9 100644
--- a/gcc/tree-ssa-sccvn.c
+++ b/gcc/tree-ssa-sccvn.c
@@ -2391,11 +2391,7 @@  vn_nary_op_compute_hash (const vn_nary_op_t vno1)
   if (vno1->length == 2
       && commutative_tree_code (vno1->opcode)
       && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
-    {
-      tree temp = vno1->op[0];
-      vno1->op[0] = vno1->op[1];
-      vno1->op[1] = temp;
-    }
+    std::swap (vno1->op[0], vno1->op[1]);
 
   hstate.add_int (vno1->opcode);
   for (i = 0; i < vno1->length; ++i)
diff --git a/gcc/tree-vect-slp.c b/gcc/tree-vect-slp.c
index 880b245..d2312c8 100644
--- a/gcc/tree-vect-slp.c
+++ b/gcc/tree-vect-slp.c
@@ -1152,9 +1152,8 @@  vect_build_slp_tree (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
 	  for (j = 0; j < group_size; ++j)
 	    if (!matches[j])
 	      {
-		gimple tem = oprnds_info[0]->def_stmts[j];
-		oprnds_info[0]->def_stmts[j] = oprnds_info[1]->def_stmts[j];
-		oprnds_info[1]->def_stmts[j] = tem;
+		std::swap (oprnds_info[0]->def_stmts[j],
+			   oprnds_info[1]->def_stmts[j]);
 		dump_printf (MSG_NOTE, "%d ", j);
 	      }
 	  dump_printf (MSG_NOTE, "\n");
diff --git a/gcc/tree-vect-stmts.c b/gcc/tree-vect-stmts.c
index 2f77e84..84076b6 100644
--- a/gcc/tree-vect-stmts.c
+++ b/gcc/tree-vect-stmts.c
@@ -8196,11 +8196,7 @@  supportable_widening_operation (enum tree_code code, gimple stmt,
     }
 
   if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
-    {
-      enum tree_code ctmp = c1;
-      c1 = c2;
-      c2 = ctmp;
-    }
+    std::swap (c1, c2);
 
   if (code == FIX_TRUNC_EXPR)
     {
diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c
index b517363..9317ae6 100644
--- a/gcc/tree-vrp.c
+++ b/gcc/tree-vrp.c
@@ -2916,33 +2916,17 @@  extract_range_from_binary_expr_1 (value_range_t *vr,
 	     prod3.  */
 	  /* min0min1 > max0max1 */
 	  if (wi::gts_p (prod0, prod3))
-	    {
-	      vrp_int tmp = prod3;
-	      prod3 = prod0;
-	      prod0 = tmp;
-	    }
+	    std::swap (prod0, prod3);
 
 	  /* min0max1 > max0min1 */
 	  if (wi::gts_p (prod1, prod2))
-	    {
-	      vrp_int tmp = prod2;
-	      prod2 = prod1;
-	      prod1 = tmp;
-	    }
+	    std::swap (prod1, prod2);
 
 	  if (wi::gts_p (prod0, prod1))
-	    {
-	      vrp_int tmp = prod1;
-	      prod1 = prod0;
-	      prod0 = tmp;
-	    }
+	    std::swap (prod0, prod1);
 
 	  if (wi::gts_p (prod2, prod3))
-	    {
-	      vrp_int tmp = prod3;
-	      prod3 = prod2;
-	      prod2 = tmp;
-	    }
+	    std::swap (prod2, prod3);
 
 	  /* diff = max - min.  */
 	  prod2 = prod3 - prod0;
@@ -3725,11 +3709,7 @@  extract_range_from_unary_expr_1 (value_range_t *vr,
 	{
           /* If the range was reversed, swap MIN and MAX.  */
 	  if (cmp == 1)
-	    {
-	      tree t = min;
-	      min = max;
-	      max = t;
-	    }
+	    std::swap (min, max);
 	}
 
       cmp = compare_values (min, max);