diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index 31e2e4d9fe7..2ec83bf273b 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -70,6 +70,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "context.h"
 #include "tree-nested.h"
 
+//#define NOISY_TOPOSORT
+
 /* Hash set of poisoned variables in a bind expr.  */
 static hash_set<tree> *asan_poisoned_variables = NULL;
 
@@ -8957,6 +8959,10 @@ omp_gather_mapping_groups (tree *list_p)
 {
   vec<omp_mapping_group> *groups = new vec<omp_mapping_group> ();
 
+#ifdef NOISY_TOPOSORT
+  fprintf (stderr, "GATHER MAPPING GROUPS\n");
+#endif
+
   for (tree *cp = list_p; *cp; cp = &OMP_CLAUSE_CHAIN (*cp))
     {
       if (OMP_CLAUSE_CODE (*cp) != OMP_CLAUSE_MAP)
@@ -8965,6 +8971,25 @@ omp_gather_mapping_groups (tree *list_p)
       tree *grp_last_p = omp_group_last (cp);
       omp_mapping_group grp;
 
+#ifdef NOISY_TOPOSORT
+      if (cp == grp_last_p)
+	{
+	  tree tmp = OMP_CLAUSE_CHAIN (*cp);
+	  OMP_CLAUSE_CHAIN (*cp) = NULL_TREE;
+	  fprintf (stderr, "found singleton clause:\n");
+	  debug_generic_expr (*cp);
+	  OMP_CLAUSE_CHAIN (*cp) = tmp;
+	}
+      else
+	{
+	  tree tmp = OMP_CLAUSE_CHAIN (*grp_last_p);
+	  OMP_CLAUSE_CHAIN (*grp_last_p) = NULL_TREE;
+	  fprintf (stderr, "found group:\n");
+	  debug_generic_expr (*cp);
+	  OMP_CLAUSE_CHAIN (*grp_last_p) = tmp;
+	}
+#endif
+
       grp.grp_start = cp;
       grp.grp_end = *grp_last_p;
       grp.mark = UNVISITED;
@@ -9129,14 +9154,44 @@ omp_index_mapping_groups (vec<omp_mapping_group> *groups)
   omp_mapping_group *grp;
   unsigned int i;
 
+#ifdef NOISY_TOPOSORT
+  fprintf (stderr, "INDEX MAPPING GROUPS\n");
+#endif
+
   FOR_EACH_VEC_ELT (*groups, i, grp)
     {
+#ifdef NOISY_TOPOSORT
+      debug_mapping_group (grp);
+#endif
+
       tree fpp;
       unsigned int chained;
       tree node = omp_group_base (grp, &chained, &fpp);
 
       if (node == error_mark_node || (!node && !fpp))
-	continue;
+	{
+#ifdef NOISY_TOPOSORT
+	  fprintf (stderr, " -- NULL base, not indexing.\n");
+#endif
+	  continue;
+	}
+
+#ifdef NOISY_TOPOSORT
+      if (node)
+	{
+	  fprintf (stderr, "base%s: ", chained > 1 ? " list" : "");
+
+	  tree walk = node;
+	  for (unsigned j = 0; j < chained; walk = OMP_CLAUSE_CHAIN (walk), j++)
+	    debug_generic_expr (OMP_CLAUSE_DECL (walk));
+	}
+
+      if (fpp)
+	{
+	  fprintf (stderr, "firstprivate pointer/reference: ");
+	  debug_generic_expr (fpp);
+	}
+#endif
 
       for (unsigned j = 0;
 	   node && j < chained;
@@ -9156,7 +9211,11 @@ omp_index_mapping_groups (vec<omp_mapping_group> *groups)
 	  omp_mapping_group **prev = grpmap->get (decl);
 
 	  if (prev && *prev == grp)
-	    /* Empty.  */;
+	    {
+#ifdef NOISY_TOPOSORT
+	      fprintf (stderr, " -- same node\n");
+#endif
+	    }
 	  else if (prev)
 	    {
 	      /* Mapping the same thing twice is normally diagnosed as an error,
@@ -9171,9 +9230,17 @@ omp_index_mapping_groups (vec<omp_mapping_group> *groups)
 
 	      grp->sibling = (*prev)->sibling;
 	      (*prev)->sibling = grp;
+#ifdef NOISY_TOPOSORT
+	      fprintf (stderr, " -- index as sibling\n");
+#endif
 	    }
 	  else
-	    grpmap->put (decl, grp);
+	    {
+#ifdef NOISY_TOPOSORT
+	      fprintf (stderr, " -- index as new decl\n");
+#endif
+	      grpmap->put (decl, grp);
+	    }
 	}
 
       if (!fpp)
@@ -9184,9 +9251,17 @@ omp_index_mapping_groups (vec<omp_mapping_group> *groups)
 	{
 	  grp->sibling = (*prev)->sibling;
 	  (*prev)->sibling = grp;
+#ifdef NOISY_TOPOSORT
+	  fprintf (stderr, " -- index fpp as sibling\n");
+#endif
 	}
       else
-	grpmap->put (fpp, grp);
+	{
+#ifdef NOISY_TOPOSORT
+	  fprintf (stderr, " -- index fpp as new decl\n");
+#endif
+	  grpmap->put (fpp, grp);
+	}
     }
   return grpmap;
 }
@@ -9233,6 +9308,11 @@ omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
 			      *grpmap,
 			    omp_mapping_group *grp)
 {
+#ifdef NOISY_TOPOSORT
+  fprintf (stderr, "processing node/group:\n");
+  debug_mapping_group (grp);
+#endif
+
   if (grp->mark == PERMANENT)
     return true;
   if (grp->mark == TEMPORARY)
@@ -9253,11 +9333,26 @@ omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
       if (basep)
 	{
 	  gcc_assert (*basep != grp);
+#ifdef NOISY_TOPOSORT
+	  fprintf (stderr, "has attachment to:\n");
+	  debug_mapping_group (*basep);
+#endif
 	  for (omp_mapping_group *w = *basep; w; w = w->sibling)
 	    if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
 	      return false;
 	}
+#ifdef NOISY_TOPOSORT
+      else
+	{
+	  fprintf (stderr, "can't find base for attachment, tried:\n");
+	  debug_generic_expr (attaches_to);
+	}
+#endif
     }
+#ifdef NOISY_TOPOSORT
+  else
+    fprintf (stderr, "doesn't attach to anything\n");
+#endif
 
   tree decl = OMP_CLAUSE_DECL (*grp->grp_start);
 
@@ -9266,7 +9361,13 @@ omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
       tree base = omp_get_base_pointer (decl);
 
       if (!base)
-	break;
+        {
+#ifdef NOISY_TOPOSORT
+	  fprintf (stderr, "no base pointer for decl:\n");
+	  debug_generic_expr (decl);
+#endif
+	  break;
+        }
 
       omp_mapping_group **innerp = grpmap->get (base);
 
@@ -9283,6 +9384,9 @@ omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
 	      && TREE_CODE (base) == MEM_REF
 	      && integer_zerop (TREE_OPERAND (base, 1)))
 	    {
+#ifdef NOISY_TOPOSORT
+	      fprintf (stderr, "we have a mem_ref, retry as indirect_ref\n");
+#endif
 	      tree ind = TREE_OPERAND (base, 0);
 	      ind = build1 (INDIRECT_REF, TREE_TYPE (base), ind);
 	      innerp = grpmap->get (ind);
@@ -9291,12 +9395,22 @@ omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
 
       if (innerp && *innerp != grp)
 	{
+#ifdef NOISY_TOPOSORT
+	  fprintf (stderr, "base pointer of node is mapped too:\n");
+	  debug_generic_expr (base);
+#endif
 	  for (omp_mapping_group *w = *innerp; w; w = w->sibling)
 	    if (!omp_tsort_mapping_groups_1 (outlist, groups, grpmap, w))
 	      return false;
 	  break;
 	}
-
+#ifdef NOISY_TOPOSORT
+      else
+	{
+	  fprintf (stderr, "base pointer of node is not mapped:\n");
+	  debug_generic_expr (base);
+	}
+#endif
       decl = base;
     }
 
@@ -9307,6 +9421,16 @@ omp_tsort_mapping_groups_1 (omp_mapping_group ***outlist,
   **outlist = grp;
   *outlist = &grp->next;
 
+#ifdef NOISY_TOPOSORT
+  fprintf (stderr, "+++ EMIT NODE/GROUP:\n");
+  {
+    tree tmp = OMP_CLAUSE_CHAIN (grp->grp_end);
+    OMP_CLAUSE_CHAIN (grp->grp_end) = NULL_TREE;
+    debug_generic_expr (*grp->grp_start);
+    OMP_CLAUSE_CHAIN (grp->grp_end) = tmp;
+  }
+#endif
+
   return true;
 }
 
@@ -9327,6 +9451,10 @@ omp_tsort_mapping_groups (vec<omp_mapping_group> *groups,
 
   cursor = &outlist;
 
+#ifdef NOISY_TOPOSORT
+  fprintf (stderr, "TOPOLOGICALLY SORT MAPPING GROUPS\n");
+#endif
+
   FOR_EACH_VEC_ELT (*groups, i, grp)
     {
       if (grp->mark != PERMANENT)
@@ -9348,6 +9476,10 @@ omp_segregate_mapping_groups (omp_mapping_group *inlist)
   omp_mapping_group *ard_groups = NULL, *tf_groups = NULL;
   omp_mapping_group **ard_tail = &ard_groups, **tf_tail = &tf_groups;
 
+#ifdef NOISY_TOPOSORT
+  fprintf (stderr, "SEGREGATE MAPPING GROUPS\n");
+#endif
+
   for (omp_mapping_group *w = inlist; w;)
     {
       tree c = *w->grp_start;
@@ -9452,7 +9584,7 @@ omp_reorder_mapping_groups (vec<omp_mapping_group> *groups,
 	     -->
 	     a {l m n} e {o p q} h i j   (chain last group to old successor)
 		      ^new_grp_tail
-	   */
+	  */
 	  *new_grp_tail = old_succs[i - 1];
 	}
       else
@@ -10285,9 +10417,30 @@ gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
 	  grpmap = omp_index_mapping_groups (groups);
 
 	  outlist = omp_tsort_mapping_groups (groups, grpmap);
+#ifdef NOISY_TOPOSORT
+	  if (!outlist)
+	    goto failure;
+	  fprintf (stderr, "TOPO-SORTED ORDER:\n");
+	  for (omp_mapping_group *w = outlist; w; w = w->next)
+	    debug_mapping_group (w);
+#endif
 	  outlist = omp_segregate_mapping_groups (outlist);
+#ifdef NOISY_TOPOSORT
+	  fprintf (stderr, "FINAL ORDER:\n");
+	  for (omp_mapping_group *w = outlist; w; w = w->next)
+	    debug_mapping_group (w);
+#endif
 	  list_p = omp_reorder_mapping_groups (groups, outlist, list_p);
-
+#ifdef NOISY_TOPOSORT
+	  fprintf (stderr, "REORDERED CLAUSES:\n");
+	  for (tree *w = list_p; *w; w = &OMP_CLAUSE_CHAIN (*w))
+	    {
+	      tree tmp = OMP_CLAUSE_CHAIN (*w);
+	      OMP_CLAUSE_CHAIN (*w) = NULL_TREE;
+	      debug_generic_expr (*w);
+	      OMP_CLAUSE_CHAIN (*w) = tmp;
+	    }
+#endif
 	failure:
 	  delete grpmap;
 	  delete groups;
