	* cfgloop.c (flow_loops_find): Reset bb->loop_depth.
	(verify_loop_structure): Verify loop_father and loop_depth.
	* cfgloopmanip.c (fix_loop_structure): Require an up-to-date
	dominator tree with fast queries.  Reset loop_depth.
	* tree-cfgcleanup.c (repair_loop_structures): Update dominators
	before calling fix_loop_structure.

Index: cfgloop.c
===================================================================
--- cfgloop.c	(revision 190287)
+++ cfgloop.c	(working copy)
@@ -438,7 +438,10 @@ flow_loops_find (struct loops *loops)
   /* Find and record information about all the natural loops
      in the CFG.  */
   FOR_EACH_BB (bb)
-    bb->loop_father = loops->tree_root;
+    {
+      bb->loop_father = loops->tree_root;
+      bb->loop_depth = 0;
+    }
 
   if (num_loops)
     {
@@ -1302,6 +1305,7 @@ cancel_loop_tree (struct loop *loop)
      -- loop header have just single entry edge and single latch edge
      -- loop latches have only single successor that is header of their loop
      -- irreducible loops are correctly marked
+     -- the cached loop depth and loop father of each bb is correct
   */
 DEBUG_FUNCTION void
 verify_loop_structure (void)
@@ -1349,12 +1353,26 @@ verify_loop_structure (void)
       bbs = get_loop_body (loop);
 
       for (j = 0; j < loop->num_nodes; j++)
-	if (!flow_bb_inside_loop_p (loop, bbs[j]))
-	  {
-	    error ("bb %d do not belong to loop %d",
-		    bbs[j]->index, loop->num);
-	    err = 1;
-	  }
+	{
+	  if (!flow_bb_inside_loop_p (loop, bbs[j]))
+	    {
+	      error ("bb %d do not belong to loop %d",
+		     bbs[j]->index, loop->num);
+	      err = 1;
+	    }
+	  if (bbs[j]->loop_father != loop)
+	    {
+	      error ("bb %d has father loop %d, should be loop %d",
+		     bbs[j]->index, bbs[j]->loop_father->num, loop->num);
+	      err = 1;
+	    }
+	  if (bbs[j]->loop_depth != loop_depth (loop))
+	    {
+	      error ("bb %d has loop depth %d, should be %d",
+		     bb->index, bb->loop_depth, loop_depth (loop));
+	      err = 1;
+	    }
+	}
       free (bbs);
     }
 
Index: cfgloopmanip.c
===================================================================
--- cfgloopmanip.c	(revision 190287)
+++ cfgloopmanip.c	(working copy)
@@ -1640,6 +1640,9 @@ fix_loop_structure (bitmap changed_bbs)
   bool record_exits = false;
   struct loop **superloop = XNEWVEC (struct loop *, number_of_loops ());
 
+  /* We need exact and fast dominance info to be available.  */
+  gcc_assert (dom_info_state (CDI_DOMINATORS) == DOM_OK);
+
   /* Remove the old bb -> loop mapping.  Remember the depth of the blocks in
      the loop hierarchy, so that we can recognize blocks whose loop nesting
      relationship has changed.  */
@@ -1648,6 +1651,7 @@ fix_loop_structure (bitmap changed_bbs)
       if (changed_bbs)
 	bb->aux = (void *) (size_t) loop_depth (bb->loop_father);
       bb->loop_father = current_loops->tree_root;
+      bb->loop_depth = 0;
     }
 
   if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
Index: tree-cfgcleanup.c
===================================================================
--- tree-cfgcleanup.c	(revision 190287)
+++ tree-cfgcleanup.c	(working copy)
@@ -737,6 +737,8 @@ repair_loop_structures (void)
 {
   bitmap changed_bbs;
 
+  calculate_dominance_info (CDI_DOMINATORS);
+
   timevar_push (TV_REPAIR_LOOPS);
   changed_bbs = BITMAP_ALLOC (NULL);
   fix_loop_structure (changed_bbs);
