Index: tree-vect-loop.c
===================================================================
--- tree-vect-loop.c	(revision 191852)
+++ tree-vect-loop.c	(working copy)
@@ -1243,6 +1243,8 @@ vect_analyze_loop_operations (loop_vec_i
   unsigned int th;
   bool only_slp_in_loop = true, ok;
   HOST_WIDE_INT max_niter;
+  HOST_WIDE_INT estimated_niter;
+  int min_profitable_estimate;
 
   if (vect_print_dump_info (REPORT_DETAILS))
     fprintf (vect_dump, "=== vect_analyze_loop_operations ===");
@@ -1436,7 +1438,8 @@ vect_analyze_loop_operations (loop_vec_i
      vector stmts depends on VF.  */
   vect_update_slp_costs_according_to_vf (loop_vinfo);
 
-  min_profitable_iters = vect_estimate_min_profitable_iters (loop_vinfo);
+  vect_estimate_min_profitable_iters (loop_vinfo, &min_profitable_iters,
+				      &min_profitable_estimate);
   LOOP_VINFO_COST_MODEL_MIN_ITERS (loop_vinfo) = min_profitable_iters;
 
   if (min_profitable_iters < 0)
@@ -1452,6 +1455,7 @@ vect_analyze_loop_operations (loop_vec_i
   min_scalar_loop_bound = ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND)
                             * vectorization_factor) - 1);
 
+
   /* Use the cost model only if it is more conservative than user specified
      threshold.  */
 
@@ -1474,6 +1478,18 @@ vect_analyze_loop_operations (loop_vec_i
       return false;
     }
 
+  if ((estimated_niter = estimated_stmt_executions_int (loop)) != -1
+      && (unsigned HOST_WIDE_INT) estimated_niter <= MAX (th, min_profitable_estimate))
+    {
+      if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS))
+        fprintf (vect_dump, "not vectorized: estimated iteration count too small.");
+      if (vect_print_dump_info (REPORT_DETAILS))
+        fprintf (vect_dump, "not vectorized: estimated iteration count smaller than "
+                 "user specified loop bound parameter or minimum "
+                 "profitable iterations (whichever is more conservative).");
+      return false;
+    }
+
   if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
       || LOOP_VINFO_INT_NITERS (loop_vinfo) % vectorization_factor != 0
       || LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo))
@@ -2512,15 +2528,15 @@ vect_get_known_peeling_cost (loop_vec_in
 
    Return the number of iterations required for the vector version of the
    loop to be profitable relative to the cost of the scalar version of the
-   loop.
+   loop.  */
 
-   TODO: Take profile info into account before making vectorization
-   decisions, if available.  */
-
-int
-vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo)
+void
+vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo,
+				    int *ret_min_profitable_niters,
+				    int *ret_min_profitable_estimate)
 {
   int min_profitable_iters;
+  int min_profitable_estimate;
   int peel_iters_prologue;
   int peel_iters_epilogue;
   unsigned vec_inside_cost = 0;
@@ -2538,7 +2554,9 @@ vect_estimate_min_profitable_iters (loop
     {
       if (vect_print_dump_info (REPORT_COST))
         fprintf (vect_dump, "cost model disabled.");
-      return 0;
+      *ret_min_profitable_niters = 0;
+      *ret_min_profitable_estimate = 0;
+      return;
     }
 
   /* Requires loop versioning tests to handle misalignment.  */
@@ -2774,7 +2792,9 @@ vect_estimate_min_profitable_iters (loop
 		 "divided by the scalar iteration cost = %d "
 		 "is greater or equal to the vectorization factor = %d.",
                  vec_inside_cost, scalar_single_iter_cost, vf);
-      return -1;
+      *ret_min_profitable_niters = -1;
+      *ret_min_profitable_estimate = -1;
+      return;
     }
 
   if (vect_print_dump_info (REPORT_COST))
@@ -2789,6 +2809,7 @@ vect_estimate_min_profitable_iters (loop
       fprintf (vect_dump, "  Scalar iteration cost: %d\n",
 	       scalar_single_iter_cost);
       fprintf (vect_dump, "  Scalar outside cost: %d\n", scalar_outside_cost);
+      fprintf (vect_dump, "  Vector outside cost: %d\n", vec_outside_cost);
       fprintf (vect_dump, "  prologue iterations: %d\n",
                peel_iters_prologue);
       fprintf (vect_dump, "  epilogue iterations: %d\n",
@@ -2809,7 +2830,34 @@ vect_estimate_min_profitable_iters (loop
     fprintf (vect_dump, "  Profitability threshold = %d\n",
 	     min_profitable_iters);
 
-  return min_profitable_iters;
+  *ret_min_profitable_niters = min_profitable_iters;
+  /* Calculate number of iterations required to make the vector version
+     profitable, relative to the loop bodies only.
+
+     Non-vectorized variant is SIC * niters and it must win over vector
+     variant on the expected loop trip count.  The following condition must hold true:
+     SIC * niters > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC  */
+
+  if ((scalar_single_iter_cost * vf) > (int) vec_inside_cost)
+    {
+      if (vec_outside_cost <= 0)
+        min_profitable_estimate = 1;
+      else
+        {
+          min_profitable_estimate = ((vec_outside_cost + scalar_outside_cost) * vf
+				     - vec_inside_cost * peel_iters_prologue
+                                     - vec_inside_cost * peel_iters_epilogue)
+                                     / ((scalar_single_iter_cost * vf)
+                                       - vec_inside_cost);
+        }
+    }
+  min_profitable_estimate --;
+  min_profitable_estimate = MAX (min_profitable_estimate, min_profitable_iters);
+  if (vect_print_dump_info (REPORT_COST))
+    fprintf (vect_dump, "  Profitability estimated iterations threshold = %d\n",
+	     min_profitable_estimate);
+
+  *ret_min_profitable_estimate = min_profitable_estimate;
 }
 
 
