diff --git a/gcc/haifa-sched.c b/gcc/haifa-sched.c
index af0ed27..dab29aa 100644
--- a/gcc/haifa-sched.c
+++ b/gcc/haifa-sched.c
@@ -5707,6 +5707,8 @@ autopref_rank_data (autopref_multipass_data_t data1,
 static int
 autopref_rank_for_schedule (const rtx_insn *insn1, const rtx_insn *insn2)
 {
+  int irr1 = 0, irr2 = 0;
+
   for (int write = 0; write < 2; ++write)
     {
       autopref_multipass_data_t data1
@@ -5717,11 +5719,15 @@ autopref_rank_for_schedule (const rtx_insn *insn1, const rtx_insn *insn2)
       if (data1->status == AUTOPREF_MULTIPASS_DATA_UNINITIALIZED)
 	autopref_multipass_init (insn1, write);
       if (data1->status == AUTOPREF_MULTIPASS_DATA_IRRELEVANT)
-	continue;
+	++irr1;
 
       if (data2->status == AUTOPREF_MULTIPASS_DATA_UNINITIALIZED)
 	autopref_multipass_init (insn2, write);
       if (data2->status == AUTOPREF_MULTIPASS_DATA_IRRELEVANT)
+	++irr2;
+
+      /* If either instruction is irrelevant to auto-prefetcher, then skip.  */
+      if (irr1 || irr2)
 	continue;
 
       if (!rtx_equal_p (data1->base, data2->base))
@@ -5730,7 +5736,11 @@ autopref_rank_for_schedule (const rtx_insn *insn1, const rtx_insn *insn2)
       return autopref_rank_data (data1, data2);
     }
 
-  return 0;
+  /* Prioritize instructions irrelevant to auto-prefetcher to the relevant
+     ones.  This gives a chance for other relevant instructions to get into
+     ready list (by resolving their dependencies from irrelevant instructions),
+     and be sorted appropriately among relevant instructions.  */
+  return irr2 - irr1;
 }
 
 /* True if header of debug dump was printed.  */
