diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 2c7195401c84b991a06fe8b8830da862ce997295..1a723feafa562520ec785d256e67670d97e61e60 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,11 @@
+2005-04-11  Daniel Berlin  <dberlin@dberlin.org>
+	
+	Fix PR tree-optimization/20612
+	* lambda-code.c (lambda_loopnest_to_gcc_loopnest): Fix increment
+	handling
+	(perfect_nestify): preheaderbb is *not* part of loop of the
+	old destination.
+
 2005-04-11  Andrew Pinski  <pinskia@physics.uc.edu>
 
 	* tree-ssa-alias.c (may_alias_p): If the variable
diff --git a/gcc/lambda-code.c b/gcc/lambda-code.c
index 80e5478ec0d1417805385a9bb7db04ce456548c4..5b23d6d5fa136995210c9e245ed8ba4c35168270 100644
--- a/gcc/lambda-code.c
+++ b/gcc/lambda-code.c
@@ -1874,6 +1874,7 @@ lambda_loopnest_to_gcc_loopnest (struct loop *old_loopnest,
       lambda_linear_expression offset;
       tree type;
       bool insert_after;
+      tree inc_stmt;
 
       oldiv = VEC_index (tree, old_ivs, i);
       type = TREE_TYPE (oldiv);
@@ -1922,7 +1923,20 @@ lambda_loopnest_to_gcc_loopnest (struct loop *old_loopnest,
       create_iv (newlowerbound,
 		 build_int_cst (type, LL_STEP (newloop)),
 		 ivvar, temp, &bsi, insert_after, &ivvar,
-		 &ivvarinced);
+		 NULL);
+
+      /* Unfortunately, the incremented ivvar that create_iv inserted may not
+	 dominate the block containing the exit condition.
+	 So we simply create our own incremented iv to use in the new exit
+	 test,  and let redundancy elimination sort it out.  */
+      inc_stmt = build (PLUS_EXPR, type, 
+			ivvar, build_int_cst (type, LL_STEP (newloop)));
+      inc_stmt = build (MODIFY_EXPR, void_type_node, SSA_NAME_VAR (ivvar),
+			inc_stmt);
+      ivvarinced = make_ssa_name (SSA_NAME_VAR (ivvar), inc_stmt);
+      TREE_OPERAND (inc_stmt, 0) = ivvarinced;
+      bsi = bsi_for_stmt (exitcond);
+      bsi_insert_before (&bsi, inc_stmt, BSI_SAME_STMT);
 
       /* Replace the exit condition with the new upper bound
          comparison.  */
@@ -2375,7 +2389,6 @@ perfect_nestify (struct loops *loops,
   add_bb_to_loop (latchbb, newloop);
   add_bb_to_loop (bodybb, newloop);
   add_bb_to_loop (headerbb, newloop);
-  add_bb_to_loop (preheaderbb, olddest->loop_father);
   set_immediate_dominator (CDI_DOMINATORS, bodybb, headerbb);
   set_immediate_dominator (CDI_DOMINATORS, headerbb, preheaderbb);
   set_immediate_dominator (CDI_DOMINATORS, preheaderbb,