diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 76b79a45ba9ae235f60ac77cfafd2fc47888c730..db26b3a7db19772fb88f342faa66b33825fc872f 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,11 @@
+2004-12-25  Zdenek Dvorak  <dvorakz@suse.cz>
+
+	PR rtl-optimization/19078
+	* tree-ssa-loop-ivopts.c (determine_use_iv_cost_generic,
+	determine_use_iv_cost_outer): Fix computing of cost for the original
+	bivs.
+	(dump_use): Handle case related_cands == NULL.
+
 2004-12-25  Marek Michalkiewicz  <marekm@amelek.gda.pl>
 
 	PR target/19059
diff --git a/gcc/tree-ssa-loop-ivopts.c b/gcc/tree-ssa-loop-ivopts.c
index d1a1bdd910aae164d663de614dcf3f184af462c0..73034127a7c9d60d86c54418f43c987ccbf0b3d7 100644
--- a/gcc/tree-ssa-loop-ivopts.c
+++ b/gcc/tree-ssa-loop-ivopts.c
@@ -444,8 +444,11 @@ dump_use (FILE *file, struct iv_use *use)
 
   dump_iv (file, use->iv);
 
-  fprintf (file, "  related candidates ");
-  dump_bitmap (file, use->related_cands);
+  if (use->related_cands)
+    {
+      fprintf (file, "  related candidates ");
+      dump_bitmap (file, use->related_cands);
+    }
 }
 
 /* Dumps information about the uses to FILE.  */
@@ -3116,8 +3119,20 @@ determine_use_iv_cost_generic (struct ivopts_data *data,
 			       struct iv_use *use, struct iv_cand *cand)
 {
   bitmap depends_on;
-  unsigned cost = get_computation_cost (data, use, cand, false, &depends_on);
+  unsigned cost;
+
+  /* The simple case first -- if we need to express value of the preserved
+     original biv, the cost is 0.  This also prevents us from counting the
+     cost of increment twice -- once at this use and once in the cost of
+     the candidate.  */
+  if (cand->pos == IP_ORIGINAL
+      && cand->incremented_at == use->stmt)
+    {
+      set_use_iv_cost (data, use, cand, 0, NULL);
+      return true;
+    }
 
+  cost = get_computation_cost (data, use, cand, false, &depends_on);
   set_use_iv_cost (data, use, cand, cost, depends_on);
 
   return cost != INFTY;
@@ -3311,7 +3326,18 @@ determine_use_iv_cost_outer (struct ivopts_data *data,
   edge exit;
   tree value;
   struct loop *loop = data->current_loop;
-	  
+
+  /* The simple case first -- if we need to express value of the preserved
+     original biv, the cost is 0.  This also prevents us from counting the
+     cost of increment twice -- once at this use and once in the cost of
+     the candidate.  */
+  if (cand->pos == IP_ORIGINAL
+      && cand->incremented_at == use->stmt)
+    {
+      set_use_iv_cost (data, use, cand, 0, NULL);
+      return true;
+    }
+
   if (!cand->iv)
     {
       if (!may_replace_final_value (loop, use, &value))