From 95fdc89f1be47c52add4c2947af6e88c77d04603 Mon Sep 17 00:00:00 2001
From: Chia-I Wu <olv@lunarg.com>
Date: Thu, 17 Oct 2013 16:10:35 +0800
Subject: [PATCH] i965:

---
 .../drivers/dri/i965/brw_schedule_instructions.cpp | 57 ++++++++++++++--------
 1 file changed, 36 insertions(+), 21 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_schedule_instructions.cpp b/src/mesa/drivers/dri/i965/brw_schedule_instructions.cpp
index b24c38c..842ab1b 100644
--- a/src/mesa/drivers/dri/i965/brw_schedule_instructions.cpp
+++ b/src/mesa/drivers/dri/i965/brw_schedule_instructions.cpp
@@ -946,31 +946,18 @@ fs_instruction_scheduler::choose_instruction_to_schedule()
          }
       }
    } else {
-      /* Before register allocation, we don't care about the latencies of
-       * instructions.  All we care about is reducing live intervals of
-       * variables so that we can avoid register spilling, or get 16-wide
-       * shaders which naturally do a better job of hiding instruction
-       * latency.
-       *
-       * To do so, schedule our instructions in a roughly LIFO/depth-first
-       * order: when new instructions become available as a result of
-       * scheduling something, choose those first so that our result
-       * hopefully is consumed quickly.
-       *
-       * The exception is messages that generate more than one result
-       * register (AKA texturing).  In those cases, the LIFO search would
-       * normally tend to choose them quickly (because scheduling the
-       * previous message not only unblocked the children using its result,
-       * but also the MRF setup for the next sampler message, which in turn
-       * unblocks the next sampler message).
-       */
-      for (schedule_node *node = (schedule_node *)instructions.get_tail();
-           node != instructions.get_head()->prev;
-           node = (schedule_node *)node->prev) {
+      foreach_list(node, &instructions) {
          schedule_node *n = (schedule_node *)node;
          fs_inst *inst = (fs_inst *)n->inst;
 
          chosen = n;
+
+         /* Choose messages that generate more than one result register
+          * (AKA texturing) last, because scheduling the previous message not
+          * only unblocked the children using its result, but also the MRF
+          * setup for the next sampler message, which in turn unblocks the
+          * next sampler message.
+          */
          if (inst->regs_written <= 1)
             break;
       }
@@ -1054,6 +1041,34 @@ instruction_scheduler::schedule_instructions(backend_instruction *next_block_hea
          bv->dump_instruction(chosen->inst);
       }
 
+      /* Before register allocation, we don't care about the latencies of
+       * instructions.  All we care about is reducing live intervals of
+       * variables so that we can avoid register spilling, or get 16-wide
+       * shaders which naturally do a better job of hiding instruction
+       * latency.
+       *
+       * To do so, schedule our instructions in a roughly LIFO/depth-first
+       * order: when new instructions become available as a result of
+       * scheduling something, choose those first so that our result
+       * hopefully is consumed quickly.
+       */
+      if (!post_reg_alloc) {
+         for (int i = chosen->child_count - 1; i >= 0; i--) {
+            schedule_node *child = chosen->children[i];
+
+            child->parent_count--;
+            if (child->parent_count == 0) {
+               if (debug) {
+                  printf("now available: ");
+                  bv->dump_instruction(child->inst);
+               }
+               instructions.push_head(child);
+            }
+         }
+
+         continue;
+      }
+
       /* Now that we've scheduled a new instruction, some of its
        * children can be promoted to the list of instructions ready to
        * be scheduled.  Update the children's unblocked time for this
-- 
1.8.3.1

