Make gimple_build main workers more flexible

Message ID 20220512075525.CF75013A97@imap2.suse-dmz.suse.de
State New
Headers
Series Make gimple_build main workers more flexible |

Commit Message

Richard Biener May 12, 2022, 7:55 a.m. UTC
  The following makes the main gimple_build API take a
gimple_stmt_iterator, whether to insert before or after and
an iterator update argument to make it more convenient to use
in certain situations (see the tree-vect-generic.cc hunks for
an example).  It also makes the case we insert into the IL
somewhat distinct from inserting into a standalone sequence in
that it simplifies built expressions the same way as inserting
and calling fold_stmt (..., follow_all_ssa_edges) would.  When
inserting into a standalone sequence we restrict simplification
to defs within the currently building sequence.

The patch only amends the tree_code gimple_build API, I will
followup with converting the rest as well.  The patch got larger
than intended because the template forwarders now use gsi_last
which introduces a dependency on gimple-iterator.h requiring
mass #include re-org across the tree.  There are two frontend
specific files including gimple-fold.h just for some padding
clearing stuff - I've removed the include and instead moved
the declarations to fold-const.h (but not the implementations).
Otherwise I'd have to include half of the middle-end headers in
those files which I didn't much like.

Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.
I've built a cc1 cross to s390x and trust the other target
changes (just re-ordering, not adding of a header).

Comments?

Thanks,
Richard.


2022-05-12  Richard Biener  <rguenther@suse.de>

gcc/cp/
	* constexpr.cc: Remove gimple-fold.h include.

gcc/c-family/
	* c-omp.cc: Remove gimple-fold.h include.

gcc/analyzer/
	* supergraph.cc: Re-order gimple-fold.h include.

gcc/
	* gimple-fold.cc (gimple_build): Adjust for new
	main API.
	* gimple-fold.h (gimple_build): New main APIs with
	iterator, insert direction and iterator update.
	(gimple_build): New forwarder template.
	(clear_padding_type_may_have_padding_p): Remove.
	(clear_type_padding_in_mask): Likewise.
	(arith_overflowed_p): Likewise.
	* fold-const.h (clear_padding_type_may_have_padding_p): Declare.
	(clear_type_padding_in_mask): Likewise.
	(arith_overflowed_p): Likewise.
	* tree-vect-generic.cc (gimplify_build3): Use main gimple_build API.
	(gimplify_build2): Likewise.
	(gimplify_build1): Likewise.
	* ubsan.cc (ubsan_expand_ptr_ifn): Likewise, avoid extra
	compare stmt.
	* gengtype.cc (open_base_files): Re-order includes.
	* builtins.cc: Re-order gimple-fold.h include.
	* calls.cc: Likewise.
	* cgraphbuild.cc: Likewise.
	* cgraphunit.cc: Likewise.
	* config/rs6000/rs6000-builtin.cc: Likewise.
	* config/rs6000/rs6000-call.cc: Likewise.
	* config/rs6000/rs6000.cc: Likewise.
	* config/s390/s390.cc: Likewise.
	* expr.cc: Likewise.
	* fold-const.cc: Likewise.
	* function-tests.cc: Likewise.
	* gimple-match-head.cc: Likewise.
	* gimple-range-fold.cc: Likewise.
	* gimple-ssa-evrp-analyze.cc: Likewise.
	* gimple-ssa-evrp.cc: Likewise.
	* gimple-ssa-sprintf.cc: Likewise.
	* gimple-ssa-warn-access.cc: Likewise.
	* gimplify.cc: Likewise.
	* graphite-isl-ast-to-gimple.cc: Likewise.
	* ipa-cp.cc: Likewise.
	* ipa-devirt.cc: Likewise.
	* ipa-prop.cc: Likewise.
	* omp-low.cc: Likewise.
	* pointer-query.cc: Likewise.
	* range-op.cc: Likewise.
	* tree-cfg.cc: Likewise.
	* tree-if-conv.cc: Likewise.
	* tree-inline.cc: Likewise.
	* tree-object-size.cc: Likewise.
	* tree-ssa-ccp.cc: Likewise.
	* tree-ssa-dom.cc: Likewise.
	* tree-ssa-forwprop.cc: Likewise.
	* tree-ssa-ifcombine.cc: Likewise.
	* tree-ssa-loop-ivcanon.cc: Likewise.
	* tree-ssa-math-opts.cc: Likewise.
	* tree-ssa-pre.cc: Likewise.
	* tree-ssa-propagate.cc: Likewise.
	* tree-ssa-reassoc.cc: Likewise.
	* tree-ssa-sccvn.cc: Likewise.
	* tree-ssa-strlen.cc: Likewise.
	* tree-ssa.cc: Likewise.
	* value-pointer-equiv.cc: Likewise.
	* vr-values.cc: Likewise.

gcc/testsuite/
	* gcc.dg/plugin/diagnostic_group_plugin.c: Reorder or remove
	gimple-fold.h include.
	* gcc.dg/plugin/diagnostic_plugin_show_trees.c:
	Likewise.
	* gcc.dg/plugin/diagnostic_plugin_test_inlining.c:
	Likewise.
	* gcc.dg/plugin/diagnostic_plugin_test_metadata.c:
	Likewise.
	* gcc.dg/plugin/diagnostic_plugin_test_paths.c:
	Likewise.
	* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c:
	Likewise.
	* gcc.dg/plugin/diagnostic_plugin_test_string_literals.c: Likewise.
	* gcc.dg/plugin/diagnostic_plugin_test_tree_expression_range.c:
	Likewise.
	* gcc.dg/plugin/finish_unit_plugin.c: Likewise.
	* gcc.dg/plugin/ggcplug.c: Likewise.
	* gcc.dg/plugin/must_tail_call_plugin.c: Likewise.
	* gcc.dg/plugin/one_time_plugin.c: Likewise.
	* gcc.dg/plugin/selfassign.c: Likewise.
	* gcc.dg/plugin/start_unit_plugin.c: Likewise.
---
 gcc/analyzer/supergraph.cc                    |  4 +-
 gcc/builtins.cc                               |  2 +-
 gcc/c-family/c-omp.cc                         |  1 -
 gcc/calls.cc                                  |  1 +
 gcc/cgraphbuild.cc                            |  2 +-
 gcc/cgraphunit.cc                             |  2 +-
 gcc/config/rs6000/rs6000-builtin.cc           |  2 +-
 gcc/config/rs6000/rs6000-call.cc              |  2 +-
 gcc/config/rs6000/rs6000.cc                   |  2 +-
 gcc/config/s390/s390.cc                       |  1 +
 gcc/cp/constexpr.cc                           |  1 -
 gcc/expr.cc                                   |  1 +
 gcc/fold-const.cc                             |  1 +
 gcc/fold-const.h                              |  5 +
 gcc/function-tests.cc                         |  3 +-
 gcc/gengtype.cc                               |  6 +-
 gcc/gimple-fold.cc                            | 93 +++++++++++++++----
 gcc/gimple-fold.h                             | 34 ++++---
 gcc/gimple-match-head.cc                      |  1 +
 gcc/gimple-range-fold.cc                      |  1 +
 gcc/gimple-ssa-evrp-analyze.cc                |  2 +-
 gcc/gimple-ssa-evrp.cc                        |  2 +-
 gcc/gimple-ssa-sprintf.cc                     |  2 +-
 gcc/gimple-ssa-warn-access.cc                 |  2 +-
 gcc/gimplify.cc                               |  2 +-
 gcc/graphite-isl-ast-to-gimple.cc             |  2 +-
 gcc/ipa-cp.cc                                 |  1 +
 gcc/ipa-devirt.cc                             |  1 +
 gcc/ipa-prop.cc                               |  2 +-
 gcc/omp-low.cc                                |  2 +-
 gcc/pointer-query.cc                          |  1 +
 gcc/range-op.cc                               |  2 +-
 .../gcc.dg/plugin/diagnostic_group_plugin.c   |  4 +-
 .../plugin/diagnostic_plugin_show_trees.c     |  4 +-
 .../plugin/diagnostic_plugin_test_inlining.c  |  4 +-
 .../plugin/diagnostic_plugin_test_metadata.c  |  4 +-
 .../plugin/diagnostic_plugin_test_paths.c     |  4 +-
 .../diagnostic_plugin_test_show_locus.c       |  4 +-
 .../diagnostic_plugin_test_string_literals.c  |  4 +-
 ...nostic_plugin_test_tree_expression_range.c |  4 +-
 .../gcc.dg/plugin/finish_unit_plugin.c        |  1 -
 gcc/testsuite/gcc.dg/plugin/ggcplug.c         |  1 -
 .../gcc.dg/plugin/must_tail_call_plugin.c     |  1 -
 gcc/testsuite/gcc.dg/plugin/one_time_plugin.c |  1 -
 gcc/testsuite/gcc.dg/plugin/selfassign.c      |  4 +-
 .../gcc.dg/plugin/start_unit_plugin.c         |  1 -
 gcc/tree-cfg.cc                               |  2 +-
 gcc/tree-if-conv.cc                           |  2 +-
 gcc/tree-inline.cc                            |  2 +-
 gcc/tree-object-size.cc                       |  2 +-
 gcc/tree-ssa-ccp.cc                           |  2 +-
 gcc/tree-ssa-dom.cc                           |  2 +-
 gcc/tree-ssa-forwprop.cc                      |  2 +-
 gcc/tree-ssa-ifcombine.cc                     |  2 +-
 gcc/tree-ssa-loop-ivcanon.cc                  |  2 +-
 gcc/tree-ssa-math-opts.cc                     |  2 +-
 gcc/tree-ssa-pre.cc                           |  2 +-
 gcc/tree-ssa-propagate.cc                     |  2 +-
 gcc/tree-ssa-reassoc.cc                       |  2 +-
 gcc/tree-ssa-sccvn.cc                         |  2 +-
 gcc/tree-ssa-strlen.cc                        |  2 +-
 gcc/tree-ssa.cc                               |  2 +-
 gcc/tree-vect-generic.cc                      | 15 +--
 gcc/ubsan.cc                                  |  9 +-
 gcc/value-pointer-equiv.cc                    |  2 +-
 gcc/vr-values.cc                              |  2 +-
 66 files changed, 178 insertions(+), 109 deletions(-)
  

Patch

diff --git a/gcc/analyzer/supergraph.cc b/gcc/analyzer/supergraph.cc
index 466f9245181..f023c533a09 100644
--- a/gcc/analyzer/supergraph.cc
+++ b/gcc/analyzer/supergraph.cc
@@ -29,13 +29,13 @@  along with GCC; see the file COPYING3.  If not see
 #include "ggc.h"
 #include "basic-block.h"
 #include "function.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
 #include "timevar.h"
-#include "gimple.h"
-#include "gimple-iterator.h"
 #include "gimple-pretty-print.h"
 #include "tree-pretty-print.h"
 #include "graphviz.h"
diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 5b085e3a14f..5fc89adf01b 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -67,13 +67,13 @@  along with GCC; see the file COPYING3.  If not see
 #include "asan.h"
 #include "internal-fn.h"
 #include "case-cfn-macros.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "intl.h"
 #include "file-prefix-map.h" /* remap_macro_filename()  */
 #include "gomp-constants.h"
 #include "omp-general.h"
 #include "tree-dfa.h"
-#include "gimple-iterator.h"
 #include "gimple-ssa.h"
 #include "tree-ssa-live.h"
 #include "tree-outof-ssa.h"
diff --git a/gcc/c-family/c-omp.cc b/gcc/c-family/c-omp.cc
index 987ba7d724a..01ef4ee4023 100644
--- a/gcc/c-family/c-omp.cc
+++ b/gcc/c-family/c-omp.cc
@@ -36,7 +36,6 @@  along with GCC; see the file COPYING3.  If not see
 #include "gimplify.h"
 #include "langhooks.h"
 #include "bitmap.h"
-#include "gimple-fold.h"
 
 
 /* Complete a #pragma oacc wait construct.  LOC is the location of
diff --git a/gcc/calls.cc b/gcc/calls.cc
index 4d0bc45be28..bbaf69c2d67 100644
--- a/gcc/calls.cc
+++ b/gcc/calls.cc
@@ -55,6 +55,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "hash-traits.h"
 #include "attribs.h"
 #include "builtins.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "attr-fnspec.h"
 #include "value-query.h"
diff --git a/gcc/cgraphbuild.cc b/gcc/cgraphbuild.cc
index 138484c2767..fdd17aa5a2d 100644
--- a/gcc/cgraphbuild.cc
+++ b/gcc/cgraphbuild.cc
@@ -26,8 +26,8 @@  along with GCC; see the file COPYING3.  If not see
 #include "gimple.h"
 #include "tree-pass.h"
 #include "cgraph.h"
-#include "gimple-fold.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "gimple-walk.h"
 #include "ipa-utils.h"
 #include "except.h"
diff --git a/gcc/cgraphunit.cc b/gcc/cgraphunit.cc
index bc3dc754481..e77bf97bea3 100644
--- a/gcc/cgraphunit.cc
+++ b/gcc/cgraphunit.cc
@@ -179,9 +179,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "stor-layout.h"
 #include "output.h"
 #include "cfgcleanup.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "gimplify-me.h"
 #include "tree-cfg.h"
 #include "tree-into-ssa.h"
diff --git a/gcc/config/rs6000/rs6000-builtin.cc b/gcc/config/rs6000/rs6000-builtin.cc
index e925ba9fad9..b60dde9dfbc 100644
--- a/gcc/config/rs6000/rs6000-builtin.cc
+++ b/gcc/config/rs6000/rs6000-builtin.cc
@@ -45,8 +45,8 @@ 
 #include "expr.h"
 #include "langhooks.h"
 #include "gimplify.h"
-#include "gimple-fold.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "ssa.h"
 #include "tree-ssa-propagate.h"
 #include "builtins.h"
diff --git a/gcc/config/rs6000/rs6000-call.cc b/gcc/config/rs6000/rs6000-call.cc
index f06c69252b2..6011fe8d3c5 100644
--- a/gcc/config/rs6000/rs6000-call.cc
+++ b/gcc/config/rs6000/rs6000-call.cc
@@ -55,8 +55,8 @@ 
 #include "common/common-target.h"
 #include "langhooks.h"
 #include "gimplify.h"
-#include "gimple-fold.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "ssa.h"
 #include "tree-ssa-propagate.h"
 #include "builtins.h"
diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index 5cb8a53e9ce..d4defc855d0 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -58,8 +58,8 @@ 
 #include "reload.h"
 #include "sched-int.h"
 #include "gimplify.h"
-#include "gimple-fold.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "gimple-walk.h"
 #include "ssa.h"
 #include "tree-vectorizer.h"
diff --git a/gcc/config/s390/s390.cc b/gcc/config/s390/s390.cc
index 7c3bd6cbe7f..45bbb6c3d70 100644
--- a/gcc/config/s390/s390.cc
+++ b/gcc/config/s390/s390.cc
@@ -70,6 +70,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "debug.h"
 #include "langhooks.h"
 #include "internal-fn.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimplify.h"
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index e560d842e8c..433fa767c03 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -31,7 +31,6 @@  along with GCC; see the file COPYING3.  If not see
 #include "builtins.h"
 #include "tree-inline.h"
 #include "ubsan.h"
-#include "gimple-fold.h"
 #include "timevar.h"
 #include "fold-const-call.h"
 #include "stor-layout.h"
diff --git a/gcc/expr.cc b/gcc/expr.cc
index 5f7142b975a..18060911793 100644
--- a/gcc/expr.cc
+++ b/gcc/expr.cc
@@ -60,6 +60,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "tree-ssa-address.h"
 #include "builtins.h"
 #include "ccmp.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "rtx-vector-builder.h"
 #include "tree-pretty-print.h"
diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index a57ad0739fb..7bf12315293 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -70,6 +70,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "tree-dfa.h"
 #include "builtins.h"
 #include "generic-match.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-into-ssa.h"
 #include "md5.h"
diff --git a/gcc/fold-const.h b/gcc/fold-const.h
index a4ff55418a5..fe78a4d0a58 100644
--- a/gcc/fold-const.h
+++ b/gcc/fold-const.h
@@ -245,6 +245,11 @@  extern tree fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE
 #define fold_build_pointer_plus_hwi(p,o) \
 	fold_build_pointer_plus_hwi_loc (UNKNOWN_LOCATION, p, o)
 
+/* In gimple-fold.cc.  */
+extern void clear_type_padding_in_mask (tree, unsigned char *);
+extern bool clear_padding_type_may_have_padding_p (tree);
+extern bool arith_overflowed_p (enum tree_code, const_tree, const_tree,
+				const_tree);
 
 /* Class used to compare gimple operands.  */
 
diff --git a/gcc/function-tests.cc b/gcc/function-tests.cc
index 7d7761569ad..1f983e87032 100644
--- a/gcc/function-tests.cc
+++ b/gcc/function-tests.cc
@@ -47,6 +47,8 @@  along with GCC; see the file COPYING3.  If not see
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "gimple-expr.h"
 #include "toplev.h"
@@ -67,7 +69,6 @@  along with GCC; see the file COPYING3.  If not see
 #include "internal-fn.h"
 #include "gimple-expr.h"
 #include "is-a.h"
-#include "gimple.h"
 #include "tree-pass.h"
 #include "context.h"
 #include "hash-map.h"
diff --git a/gcc/gengtype.cc b/gcc/gengtype.cc
index e11da9e46d0..19676251fdb 100644
--- a/gcc/gengtype.cc
+++ b/gcc/gengtype.cc
@@ -1703,9 +1703,9 @@  open_base_files (void)
       "alias.h", "insn-config.h", "flags.h", "expmed.h", "dojump.h",
       "explow.h", "calls.h", "memmodel.h", "emit-rtl.h", "varasm.h",
       "stmt.h", "expr.h", "alloc-pool.h", "cselib.h", "insn-addr.h",
-      "optabs.h", "libfuncs.h", "debug.h", "internal-fn.h", "gimple-fold.h",
-      "value-range.h",
-      "tree-eh.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
+      "optabs.h", "libfuncs.h", "debug.h", "internal-fn.h",
+      "gimple-iterator.h", "gimple-fold.h", "value-range.h",
+      "tree-eh.h", "gimple-ssa.h", "tree-cfg.h",
       "tree-vrp.h", "tree-phinodes.h", "ssa-iterators.h", "stringpool.h",
       "tree-ssanames.h", "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h",
       "tree-ssa-loop-manip.h", "tree-ssa-loop-niter.h", "tree-into-ssa.h",
diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
index 7baec119ba3..e086b0310ef 100644
--- a/gcc/gimple-fold.cc
+++ b/gcc/gimple-fold.cc
@@ -37,9 +37,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "expr.h"
 #include "stor-layout.h"
 #include "dumpfile.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "tree-into-ssa.h"
 #include "tree-dfa.h"
 #include "tree-object-size.h"
@@ -8669,14 +8669,23 @@  gimple_build_valueize (tree op)
 
 /* Build the expression CODE OP0 of type TYPE with location LOC,
    simplifying it first if possible.  Returns the built
-   expression value and appends statements possibly defining it
-   to SEQ.  */
+   expression value and inserts statements possibly defining it
+   before GSI if BEFORE is true or after GSI if false and advance
+   the iterator accordingly.
+   If gsi refers to a basic block simplifying is allowed to look
+   at all SSA defs while when it does not it is restricted to
+   SSA defs that are not associated with a basic block yet,
+   indicating they belong to the currently building sequence.  */
 
 tree
-gimple_build (gimple_seq *seq, location_t loc,
-	      enum tree_code code, tree type, tree op0)
+gimple_build (gimple_stmt_iterator *gsi,
+	      bool before, gsi_iterator_update update,
+	      location_t loc, enum tree_code code, tree type, tree op0)
 {
-  tree res = gimple_simplify (code, type, op0, seq, gimple_build_valueize);
+  gimple_seq seq = NULL;
+  tree res
+    = gimple_simplify (code, type, op0, &seq,
+		       gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   if (!res)
     {
       res = create_tmp_reg_or_ssa_name (type);
@@ -8688,7 +8697,21 @@  gimple_build (gimple_seq *seq, location_t loc,
       else
 	stmt = gimple_build_assign (res, code, op0);
       gimple_set_location (stmt, loc);
-      gimple_seq_add_stmt_without_update (seq, stmt);
+      gimple_seq_add_stmt_without_update (&seq, stmt);
+    }
+  if (before)
+    {
+      if (gsi->bb)
+	gsi_insert_seq_before (gsi, seq, update);
+      else
+	gsi_insert_seq_before_without_update (gsi, seq, update);
+    }
+  else
+    {
+      if (gsi->bb)
+	gsi_insert_seq_after (gsi, seq, update);
+      else
+	gsi_insert_seq_after_without_update (gsi, seq, update);
     }
   return res;
 }
@@ -8699,16 +8722,35 @@  gimple_build (gimple_seq *seq, location_t loc,
    to SEQ.  */
 
 tree
-gimple_build (gimple_seq *seq, location_t loc,
-	      enum tree_code code, tree type, tree op0, tree op1)
+gimple_build (gimple_stmt_iterator *gsi,
+	      bool before, gsi_iterator_update update,
+	      location_t loc, enum tree_code code, tree type,
+	      tree op0, tree op1)
 {
-  tree res = gimple_simplify (code, type, op0, op1, seq, gimple_build_valueize);
+  gimple_seq seq = NULL;
+  tree res
+    = gimple_simplify (code, type, op0, op1, &seq,
+		       gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   if (!res)
     {
       res = create_tmp_reg_or_ssa_name (type);
       gimple *stmt = gimple_build_assign (res, code, op0, op1);
       gimple_set_location (stmt, loc);
-      gimple_seq_add_stmt_without_update (seq, stmt);
+      gimple_seq_add_stmt_without_update (&seq, stmt);
+    }
+  if (before)
+    {
+      if (gsi->bb)
+	gsi_insert_seq_before (gsi, seq, update);
+      else
+	gsi_insert_seq_before_without_update (gsi, seq, update);
+    }
+  else
+    {
+      if (gsi->bb)
+	gsi_insert_seq_after (gsi, seq, update);
+      else
+	gsi_insert_seq_after_without_update (gsi, seq, update);
     }
   return res;
 }
@@ -8719,11 +8761,16 @@  gimple_build (gimple_seq *seq, location_t loc,
    to SEQ.  */
 
 tree
-gimple_build (gimple_seq *seq, location_t loc,
-	      enum tree_code code, tree type, tree op0, tree op1, tree op2)
+gimple_build (gimple_stmt_iterator *gsi,
+	      bool before, gsi_iterator_update update,
+	      location_t loc, enum tree_code code, tree type,
+	      tree op0, tree op1, tree op2)
 {
-  tree res = gimple_simplify (code, type, op0, op1, op2,
-			      seq, gimple_build_valueize);
+
+  gimple_seq seq = NULL;
+  tree res
+    = gimple_simplify (code, type, op0, op1, op2, &seq,
+		       gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
   if (!res)
     {
       res = create_tmp_reg_or_ssa_name (type);
@@ -8734,7 +8781,21 @@  gimple_build (gimple_seq *seq, location_t loc,
       else
 	stmt = gimple_build_assign (res, code, op0, op1, op2);
       gimple_set_location (stmt, loc);
-      gimple_seq_add_stmt_without_update (seq, stmt);
+      gimple_seq_add_stmt_without_update (&seq, stmt);
+    }
+  if (before)
+    {
+      if (gsi->bb)
+	gsi_insert_seq_before (gsi, seq, update);
+      else
+	gsi_insert_seq_before_without_update (gsi, seq, update);
+    }
+  else
+    {
+      if (gsi->bb)
+	gsi_insert_seq_after (gsi, seq, update);
+      else
+	gsi_insert_seq_after_without_update (gsi, seq, update);
     }
   return res;
 }
diff --git a/gcc/gimple-fold.h b/gcc/gimple-fold.h
index 850f917bbc1..520fde861e3 100644
--- a/gcc/gimple-fold.h
+++ b/gcc/gimple-fold.h
@@ -38,12 +38,8 @@  extern tree maybe_fold_and_comparisons (tree, enum tree_code, tree, tree,
 extern tree maybe_fold_or_comparisons (tree, enum tree_code, tree, tree,
 				       enum tree_code, tree, tree,
 				       basic_block = nullptr);
-extern bool clear_padding_type_may_have_padding_p (tree);
-extern void clear_type_padding_in_mask (tree, unsigned char *);
 extern bool optimize_atomic_compare_exchange_p (gimple *);
 extern void fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *);
-extern bool arith_overflowed_p (enum tree_code, const_tree, const_tree,
-				const_tree);
 extern tree no_follow_ssa_edges (tree);
 extern tree follow_single_use_edges (tree);
 extern tree follow_all_ssa_edges (tree);
@@ -71,19 +67,35 @@  extern tree tree_vec_extract (gimple_stmt_iterator *, tree, tree, tree, tree);
 /* gimple_build, functionally matching fold_buildN, outputs stmts
    int the provided sequence, matching and simplifying them on-the-fly.
    Supposed to replace force_gimple_operand (fold_buildN (...), ...).  */
-extern tree gimple_build (gimple_seq *, location_t,
-			  enum tree_code, tree, tree);
-extern tree gimple_build (gimple_seq *, location_t,
-			  enum tree_code, tree, tree, tree);
-extern tree gimple_build (gimple_seq *, location_t,
-			  enum tree_code, tree, tree, tree, tree);
+extern tree gimple_build (gimple_stmt_iterator *, bool,
+			  enum gsi_iterator_update,
+			  location_t, enum tree_code, tree, tree);
+extern tree gimple_build (gimple_stmt_iterator *, bool,
+			  enum gsi_iterator_update,
+			  location_t, enum tree_code, tree, tree, tree);
+extern tree gimple_build (gimple_stmt_iterator *, bool,
+			  enum gsi_iterator_update,
+			  location_t, enum tree_code, tree, tree, tree, tree);
+template<class ...Args>
+inline tree
+gimple_build (gimple_seq *seq, location_t loc,
+	      enum tree_code code, tree type, Args ...ops)
+{
+  static_assert (sizeof...(ops) > 0 && sizeof...(ops) <= 3,
+		 "Number of operands must be from one to three");
+  gimple_stmt_iterator gsi = gsi_last (*seq);
+  return gimple_build (&gsi, false, GSI_CONTINUE_LINKING,
+		       loc, code, type, ops...);
+}
 template<class ...Args>
 inline tree
 gimple_build (gimple_seq *seq, enum tree_code code, tree type, Args ...ops)
 {
   static_assert (sizeof...(ops) > 0 && sizeof...(ops) <= 3,
 		 "Number of operands must be from one to three");
-  return gimple_build (seq, UNKNOWN_LOCATION, code, type, ops...);
+  gimple_stmt_iterator gsi = gsi_last (*seq);
+  return gimple_build (&gsi, false, GSI_CONTINUE_LINKING,
+		       UNKNOWN_LOCATION, code, type, ops...);
 }
 
 extern tree gimple_build (gimple_seq *, location_t, combined_fn, tree);
diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc
index 1c74d38088f..4c80d77f8ba 100644
--- a/gcc/gimple-match-head.cc
+++ b/gcc/gimple-match-head.cc
@@ -31,6 +31,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "fold-const.h"
 #include "fold-const-call.h"
 #include "stor-layout.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "calls.h"
 #include "tree-dfa.h"
diff --git a/gcc/gimple-range-fold.cc b/gcc/gimple-range-fold.cc
index 3169e29b5de..08d791a0418 100644
--- a/gcc/gimple-range-fold.cc
+++ b/gcc/gimple-range-fold.cc
@@ -29,6 +29,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "ssa.h"
 #include "gimple-pretty-print.h"
 #include "optabs-tree.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "wide-int.h"
 #include "fold-const.h"
diff --git a/gcc/gimple-ssa-evrp-analyze.cc b/gcc/gimple-ssa-evrp-analyze.cc
index fec6e87ba80..16e5a75b1db 100644
--- a/gcc/gimple-ssa-evrp-analyze.cc
+++ b/gcc/gimple-ssa-evrp-analyze.cc
@@ -27,9 +27,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "ssa.h"
 #include "gimple-pretty-print.h"
 #include "cfganal.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
-#include "gimple-iterator.h"
 #include "tree-cfg.h"
 #include "tree-ssa-loop-manip.h"
 #include "tree-ssa-loop.h"
diff --git a/gcc/gimple-ssa-evrp.cc b/gcc/gimple-ssa-evrp.cc
index 2baaed6d978..92dbdd5df95 100644
--- a/gcc/gimple-ssa-evrp.cc
+++ b/gcc/gimple-ssa-evrp.cc
@@ -27,9 +27,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "ssa.h"
 #include "gimple-pretty-print.h"
 #include "cfganal.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
-#include "gimple-iterator.h"
 #include "tree-cfg.h"
 #include "tree-ssa-loop-manip.h"
 #include "tree-ssa-loop.h"
diff --git a/gcc/gimple-ssa-sprintf.cc b/gcc/gimple-ssa-sprintf.cc
index 9a84fffed1a..961c1b739da 100644
--- a/gcc/gimple-ssa-sprintf.cc
+++ b/gcc/gimple-ssa-sprintf.cc
@@ -53,11 +53,11 @@  along with GCC; see the file COPYING3.  If not see
 #include "gimple.h"
 #include "tree-pass.h"
 #include "ssa.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "gimple-pretty-print.h"
 #include "diagnostic-core.h"
 #include "fold-const.h"
-#include "gimple-iterator.h"
 #include "tree-ssa.h"
 #include "tree-object-size.h"
 #include "tree-cfg.h"
diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc
index 39aa8186de6..c4204241c8c 100644
--- a/gcc/gimple-ssa-warn-access.cc
+++ b/gcc/gimple-ssa-warn-access.cc
@@ -36,8 +36,8 @@ 
 #include "gimple-ssa-warn-restrict.h"
 #include "diagnostic-core.h"
 #include "fold-const.h"
-#include "gimple-fold.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "langhooks.h"
 #include "memmodel.h"
 #include "target.h"
diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index 13413d019c4..2f6d995c385 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -42,10 +42,10 @@  along with GCC; see the file COPYING3.  If not see
 #include "varasm.h"
 #include "stmt.h"
 #include "expr.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "stor-layout.h"
 #include "print-tree.h"
 #include "tree-iterator.h"
diff --git a/gcc/graphite-isl-ast-to-gimple.cc b/gcc/graphite-isl-ast-to-gimple.cc
index ea1129f0da3..45ed7704807 100644
--- a/gcc/graphite-isl-ast-to-gimple.cc
+++ b/gcc/graphite-isl-ast-to-gimple.cc
@@ -32,8 +32,8 @@  along with GCC; see the file COPYING3.  If not see
 #include "gimple.h"
 #include "ssa.h"
 #include "fold-const.h"
-#include "gimple-fold.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "gimplify.h"
 #include "gimplify-me.h"
 #include "tree-eh.h"
diff --git a/gcc/ipa-cp.cc b/gcc/ipa-cp.cc
index 11f4a327b99..152fe7244f7 100644
--- a/gcc/ipa-cp.cc
+++ b/gcc/ipa-cp.cc
@@ -113,6 +113,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "cgraph.h"
 #include "diagnostic.h"
 #include "fold-const.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "symbol-summary.h"
 #include "tree-vrp.h"
diff --git a/gcc/ipa-devirt.cc b/gcc/ipa-devirt.cc
index 6cba2085f1a..9f1442dc3cc 100644
--- a/gcc/ipa-devirt.cc
+++ b/gcc/ipa-devirt.cc
@@ -120,6 +120,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "print-tree.h"
 #include "calls.h"
 #include "ipa-utils.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "symbol-summary.h"
 #include "tree-vrp.h"
diff --git a/gcc/ipa-prop.cc b/gcc/ipa-prop.cc
index 80e67e93e12..c6c745f84a0 100644
--- a/gcc/ipa-prop.cc
+++ b/gcc/ipa-prop.cc
@@ -31,13 +31,13 @@  along with GCC; see the file COPYING3.  If not see
 #include "cgraph.h"
 #include "diagnostic.h"
 #include "fold-const.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "calls.h"
 #include "stor-layout.h"
 #include "print-tree.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "gimplify-me.h"
 #include "gimple-walk.h"
 #include "symbol-summary.h"
diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc
index e7818a9af5f..8aebaeecd42 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -37,9 +37,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "fold-const.h"
 #include "stor-layout.h"
 #include "internal-fn.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "gimplify-me.h"
 #include "gimple-walk.h"
 #include "tree-iterator.h"
diff --git a/gcc/pointer-query.cc b/gcc/pointer-query.cc
index d93657f3206..646606e6344 100644
--- a/gcc/pointer-query.cc
+++ b/gcc/pointer-query.cc
@@ -33,6 +33,7 @@ 
 #include "langhooks.h"
 #include "stringpool.h"
 #include "attribs.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "gimple-ssa.h"
 #include "intl.h"
diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 47c6dff8f3e..eaa02309d70 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -38,9 +38,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "stor-layout.h"
 #include "calls.h"
 #include "cfganal.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
-#include "gimple-iterator.h"
 #include "gimple-walk.h"
 #include "tree-cfg.h"
 #include "wide-int.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.c
index 67ca701adfa..3396b384163 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_group_plugin.c
@@ -15,12 +15,12 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
-#include "gimple.h"
-#include "gimple-iterator.h"
 #include "tree.h"
 #include "tree-pass.h"
 #include "intl.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_show_trees.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_show_trees.c
index ac72503d92e..d81fa571c97 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_show_trees.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_show_trees.c
@@ -17,12 +17,12 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
-#include "gimple.h"
-#include "gimple-iterator.h"
 #include "tree.h"
 #include "tree-pass.h"
 #include "intl.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_inlining.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_inlining.c
index d2bfca092a7..3627f7a2b7f 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_inlining.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_inlining.c
@@ -15,12 +15,12 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
-#include "gimple.h"
-#include "gimple-iterator.h"
 #include "tree.h"
 #include "tree-pass.h"
 #include "intl.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_metadata.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_metadata.c
index a6108919b24..4b13afc093d 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_metadata.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_metadata.c
@@ -15,12 +15,12 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
-#include "gimple.h"
-#include "gimple-iterator.h"
 #include "tree.h"
 #include "tree-pass.h"
 #include "intl.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c
index 5c2da021553..8d97fe8e8d9 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.c
@@ -21,12 +21,12 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
-#include "gimple.h"
-#include "gimple-iterator.h"
 #include "tree.h"
 #include "tree-pass.h"
 #include "intl.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
index 482dbda47f7..baa6b629b83 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
@@ -47,12 +47,12 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
-#include "gimple.h"
-#include "gimple-iterator.h"
 #include "tree.h"
 #include "tree-pass.h"
 #include "intl.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_string_literals.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_string_literals.c
index aa73dcae820..0269f728a93 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_string_literals.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_string_literals.c
@@ -17,12 +17,12 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
-#include "gimple.h"
-#include "gimple-iterator.h"
 #include "tree.h"
 #include "tree-pass.h"
 #include "intl.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_tree_expression_range.c b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_tree_expression_range.c
index 4a89d846dce..f5468636e06 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_tree_expression_range.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_tree_expression_range.c
@@ -17,12 +17,12 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
-#include "gimple.h"
-#include "gimple-iterator.h"
 #include "tree.h"
 #include "tree-pass.h"
 #include "intl.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/finish_unit_plugin.c b/gcc/testsuite/gcc.dg/plugin/finish_unit_plugin.c
index 1b4f7cc26fc..05e188165bf 100644
--- a/gcc/testsuite/gcc.dg/plugin/finish_unit_plugin.c
+++ b/gcc/testsuite/gcc.dg/plugin/finish_unit_plugin.c
@@ -15,7 +15,6 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
-#include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/ggcplug.c b/gcc/testsuite/gcc.dg/plugin/ggcplug.c
index c186d119371..a75eed0e398 100644
--- a/gcc/testsuite/gcc.dg/plugin/ggcplug.c
+++ b/gcc/testsuite/gcc.dg/plugin/ggcplug.c
@@ -14,7 +14,6 @@ 
 #include "ggc.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
-#include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/must_tail_call_plugin.c b/gcc/testsuite/gcc.dg/plugin/must_tail_call_plugin.c
index 5294f28abbc..0c040e54252 100644
--- a/gcc/testsuite/gcc.dg/plugin/must_tail_call_plugin.c
+++ b/gcc/testsuite/gcc.dg/plugin/must_tail_call_plugin.c
@@ -17,7 +17,6 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
-#include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/one_time_plugin.c b/gcc/testsuite/gcc.dg/plugin/one_time_plugin.c
index bd1c0f0e579..84f2d31f0f2 100644
--- a/gcc/testsuite/gcc.dg/plugin/one_time_plugin.c
+++ b/gcc/testsuite/gcc.dg/plugin/one_time_plugin.c
@@ -12,7 +12,6 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
-#include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/selfassign.c b/gcc/testsuite/gcc.dg/plugin/selfassign.c
index 2adb6446b1d..13b3ecaa0f2 100644
--- a/gcc/testsuite/gcc.dg/plugin/selfassign.c
+++ b/gcc/testsuite/gcc.dg/plugin/selfassign.c
@@ -17,12 +17,12 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
-#include "gimple.h"
-#include "gimple-iterator.h"
 #include "tree.h"
 #include "tree-pass.h"
 #include "intl.h"
diff --git a/gcc/testsuite/gcc.dg/plugin/start_unit_plugin.c b/gcc/testsuite/gcc.dg/plugin/start_unit_plugin.c
index 61e9494038c..7b4f40e0e9d 100644
--- a/gcc/testsuite/gcc.dg/plugin/start_unit_plugin.c
+++ b/gcc/testsuite/gcc.dg/plugin/start_unit_plugin.c
@@ -20,7 +20,6 @@ 
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
-#include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
 #include "is-a.h"
diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index e321d929fd0..19ba09facce 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -37,9 +37,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "stor-layout.h"
 #include "print-tree.h"
 #include "cfganal.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
-#include "gimple-iterator.h"
 #include "gimplify-me.h"
 #include "gimple-walk.h"
 #include "tree-cfg.h"
diff --git a/gcc/tree-if-conv.cc b/gcc/tree-if-conv.cc
index 57cc38567d3..4531ca58bdf 100644
--- a/gcc/tree-if-conv.cc
+++ b/gcc/tree-if-conv.cc
@@ -96,9 +96,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "alias.h"
 #include "fold-const.h"
 #include "stor-layout.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "gimplify-me.h"
 #include "tree-cfg.h"
 #include "tree-into-ssa.h"
diff --git a/gcc/tree-inline.cc b/gcc/tree-inline.cc
index 29bb758b7bc..043e1d5987a 100644
--- a/gcc/tree-inline.cc
+++ b/gcc/tree-inline.cc
@@ -41,10 +41,10 @@  along with GCC; see the file COPYING3.  If not see
 #include "cfganal.h"
 #include "tree-iterator.h"
 #include "intl.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "gimplify-me.h"
 #include "gimple-walk.h"
 #include "tree-cfg.h"
diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc
index fc062b94d76..5ca87ae3504 100644
--- a/gcc/tree-object-size.cc
+++ b/gcc/tree-object-size.cc
@@ -29,8 +29,8 @@  along with GCC; see the file COPYING3.  If not see
 #include "gimple-pretty-print.h"
 #include "fold-const.h"
 #include "tree-object-size.h"
-#include "gimple-fold.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "tree-cfg.h"
 #include "tree-dfa.h"
 #include "stringpool.h"
diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc
index 9164efe3037..262a247eac5 100644
--- a/gcc/tree-ssa-ccp.cc
+++ b/gcc/tree-ssa-ccp.cc
@@ -129,10 +129,10 @@  along with GCC; see the file COPYING3.  If not see
 #include "ssa.h"
 #include "gimple-pretty-print.h"
 #include "fold-const.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "tree-cfg.h"
 #include "tree-ssa-propagate.h"
 #include "dbgcnt.h"
diff --git a/gcc/tree-ssa-dom.cc b/gcc/tree-ssa-dom.cc
index 89b05171d57..9a843217553 100644
--- a/gcc/tree-ssa-dom.cc
+++ b/gcc/tree-ssa-dom.cc
@@ -30,10 +30,10 @@  along with GCC; see the file COPYING3.  If not see
 #include "fold-const.h"
 #include "cfganal.h"
 #include "cfgloop.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "tree-inline.h"
-#include "gimple-iterator.h"
 #include "tree-cfg.h"
 #include "tree-into-ssa.h"
 #include "domwalk.h"
diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
index 484491fa1c5..360b0bba61e 100644
--- a/gcc/tree-ssa-forwprop.cc
+++ b/gcc/tree-ssa-forwprop.cc
@@ -32,10 +32,10 @@  along with GCC; see the file COPYING3.  If not see
 #include "gimple-pretty-print.h"
 #include "fold-const.h"
 #include "stor-layout.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "gimplify-me.h"
 #include "tree-cfg.h"
 #include "expr.h"
diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc
index cb86cc1ea5f..88a9f062dcd 100644
--- a/gcc/tree-ssa-ifcombine.cc
+++ b/gcc/tree-ssa-ifcombine.cc
@@ -35,8 +35,8 @@  along with GCC; see the file COPYING3.  If not see
    BRANCH_COST.  */
 #include "fold-const.h"
 #include "cfganal.h"
-#include "gimple-fold.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "gimplify-me.h"
 #include "tree-cfg.h"
 #include "tree-ssa.h"
diff --git a/gcc/tree-ssa-loop-ivcanon.cc b/gcc/tree-ssa-loop-ivcanon.cc
index e2ac2044741..2ee00a3f843 100644
--- a/gcc/tree-ssa-loop-ivcanon.cc
+++ b/gcc/tree-ssa-loop-ivcanon.cc
@@ -48,9 +48,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "gimple-pretty-print.h"
 #include "fold-const.h"
 #include "profile.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
-#include "gimple-iterator.h"
 #include "tree-cfg.h"
 #include "tree-ssa-loop-manip.h"
 #include "tree-ssa-loop-niter.h"
diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc
index 2085597447e..ce1df01f629 100644
--- a/gcc/tree-ssa-math-opts.cc
+++ b/gcc/tree-ssa-math-opts.cc
@@ -100,8 +100,8 @@  along with GCC; see the file COPYING3.  If not see
 #include "gimple-pretty-print.h"
 #include "alias.h"
 #include "fold-const.h"
-#include "gimple-fold.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "gimplify.h"
 #include "gimplify-me.h"
 #include "stor-layout.h"
diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc
index a578ce6dfa4..34d77f1e2cf 100644
--- a/gcc/tree-ssa-pre.cc
+++ b/gcc/tree-ssa-pre.cc
@@ -34,10 +34,10 @@  along with GCC; see the file COPYING3.  If not see
 #include "gimple-pretty-print.h"
 #include "fold-const.h"
 #include "cfganal.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "tree-cfg.h"
 #include "tree-into-ssa.h"
 #include "tree-dfa.h"
diff --git a/gcc/tree-ssa-propagate.cc b/gcc/tree-ssa-propagate.cc
index 7813e511379..c10ffd91766 100644
--- a/gcc/tree-ssa-propagate.cc
+++ b/gcc/tree-ssa-propagate.cc
@@ -27,10 +27,10 @@ 
 #include "ssa.h"
 #include "gimple-pretty-print.h"
 #include "dumpfile.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "tree-cfg.h"
 #include "tree-ssa.h"
 #include "tree-ssa-propagate.h"
diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc
index 43b25371d4b..406523b5d2f 100644
--- a/gcc/tree-ssa-reassoc.cc
+++ b/gcc/tree-ssa-reassoc.cc
@@ -38,9 +38,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "fold-const.h"
 #include "stor-layout.h"
 #include "cfganal.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
-#include "gimple-iterator.h"
 #include "gimplify-me.h"
 #include "tree-cfg.h"
 #include "tree-ssa-loop.h"
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 3732d06b0bb..5cb3dc2ab3a 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -39,6 +39,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "cfganal.h"
 #include "tree-inline.h"
 #include "internal-fn.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimplify.h"
@@ -56,7 +57,6 @@  along with GCC; see the file COPYING3.  If not see
 #include "tree-ssa-propagate.h"
 #include "tree-cfg.h"
 #include "domwalk.h"
-#include "gimple-iterator.h"
 #include "gimple-match.h"
 #include "stringpool.h"
 #include "attribs.h"
diff --git a/gcc/tree-ssa-strlen.cc b/gcc/tree-ssa-strlen.cc
index 1e5f911fedb..bb7fe0853ef 100644
--- a/gcc/tree-ssa-strlen.cc
+++ b/gcc/tree-ssa-strlen.cc
@@ -34,10 +34,10 @@  along with GCC; see the file COPYING3.  If not see
 #include "gimple-ssa-warn-restrict.h"
 #include "fold-const.h"
 #include "stor-layout.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "gimplify-me.h"
 #include "expr.h"
 #include "tree-cfg.h"
diff --git a/gcc/tree-ssa.cc b/gcc/tree-ssa.cc
index a362a0a9ea6..6507348e793 100644
--- a/gcc/tree-ssa.cc
+++ b/gcc/tree-ssa.cc
@@ -30,9 +30,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "diagnostic-core.h"
 #include "fold-const.h"
 #include "stor-layout.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "gimplify.h"
-#include "gimple-iterator.h"
 #include "gimple-walk.h"
 #include "tree-ssa-loop-manip.h"
 #include "tree-into-ssa.h"
diff --git a/gcc/tree-vect-generic.cc b/gcc/tree-vect-generic.cc
index e5bd9dc7c76..d99e3207fbe 100644
--- a/gcc/tree-vect-generic.cc
+++ b/gcc/tree-vect-generic.cc
@@ -54,10 +54,7 @@  gimplify_build3 (gimple_stmt_iterator *gsi, enum tree_code code,
 		 tree type, tree a, tree b, tree c)
 {
   location_t loc = gimple_location (gsi_stmt (*gsi));
-  gimple_seq stmts = NULL;
-  tree ret = gimple_build (&stmts, loc, code, type, a, b, c);
-  gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
-  return ret;
+  return gimple_build (gsi, true, GSI_SAME_STMT, loc, code, type, a, b, c);
 }
 
 /* Build a binary operation and gimplify it.  Emit code before GSI.
@@ -68,10 +65,7 @@  gimplify_build2 (gimple_stmt_iterator *gsi, enum tree_code code,
 		 tree type, tree a, tree b)
 {
   location_t loc = gimple_location (gsi_stmt (*gsi));
-  gimple_seq stmts = NULL;
-  tree ret = gimple_build (&stmts, loc, code, type, a, b);
-  gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
-  return ret;
+  return gimple_build (gsi, true, GSI_SAME_STMT, loc, code, type, a, b);
 }
 
 /* Build a unary operation and gimplify it.  Emit code before GSI.
@@ -82,10 +76,7 @@  gimplify_build1 (gimple_stmt_iterator *gsi, enum tree_code code, tree type,
 		 tree a)
 {
   location_t loc = gimple_location (gsi_stmt (*gsi));
-  gimple_seq stmts = NULL;
-  tree ret = gimple_build (&stmts, loc, code, type, a);
-  gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
-  return ret;
+  return gimple_build (gsi, true, GSI_SAME_STMT, loc, code, type, a);
 }
 
 
diff --git a/gcc/ubsan.cc b/gcc/ubsan.cc
index f74929d0f17..6c058142c37 100644
--- a/gcc/ubsan.cc
+++ b/gcc/ubsan.cc
@@ -1184,12 +1184,9 @@  ubsan_expand_ptr_ifn (gimple_stmt_iterator *gsip)
       gimple_set_location (g, loc);
       gsi_insert_after (&gsi2, g, GSI_NEW_STMT);
 
-      gimple_seq seq = NULL;
-      tree t = gimple_build (&seq, loc, NOP_EXPR, ssizetype, off);
-      t = gimple_build (&seq, loc, GE_EXPR, boolean_type_node,
-			t, ssize_int (0));
-      gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
-      g = gimple_build_cond (NE_EXPR, t, boolean_false_node,
+      tree t = gimple_build (&gsi, true, GSI_SAME_STMT,
+			     loc, NOP_EXPR, ssizetype, off);
+      g = gimple_build_cond (GE_EXPR, t, ssize_int (0),
 			     NULL_TREE, NULL_TREE);
     }
   gimple_set_location (g, loc);
diff --git a/gcc/value-pointer-equiv.cc b/gcc/value-pointer-equiv.cc
index f2d95c4393e..b768ecda11a 100644
--- a/gcc/value-pointer-equiv.cc
+++ b/gcc/value-pointer-equiv.cc
@@ -28,9 +28,9 @@  along with GCC; see the file COPYING3.  If not see
 #include "ssa.h"
 #include "gimple-pretty-print.h"
 #include "cfganal.h"
+#include "gimple-iterator.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
-#include "gimple-iterator.h"
 #include "tree-cfg.h"
 #include "tree-ssa-loop-manip.h"
 #include "tree-ssa-loop.h"
diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc
index 301996d2134..2cc5084c15a 100644
--- a/gcc/vr-values.cc
+++ b/gcc/vr-values.cc
@@ -32,8 +32,8 @@  along with GCC; see the file COPYING3.  If not see
 #include "fold-const.h"
 #include "calls.h"
 #include "cfganal.h"
-#include "gimple-fold.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "tree-cfg.h"
 #include "tree-ssa-loop-niter.h"
 #include "tree-ssa-loop.h"