1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright © 2010 Intel Corporation
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"),
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and/or sell copies of the Software, and to permit persons to whom the
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software is furnished to do so, subject to the following conditions:
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the next
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * paragraph) shall be included in all copies or substantial portions of the
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software.
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IN THE SOFTWARE.
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Authors:
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *    Eric Anholt <eric@anholt.net>
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "brw_fs.h"
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glsl/glsl_types.h"
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glsl/ir_optimization.h"
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glsl/ir_print_visitor.h"
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** @file brw_fs_schedule_instructions.cpp
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * List scheduling of FS instructions.
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The basic model of the list scheduler is to take a basic block,
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * compute a DAG of the dependencies (RAW ordering with latency, WAW
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ordering, WAR ordering), and make a list of the DAG heads.
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Heuristically pick a DAG head, then put all the children that are
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * now DAG heads into the list of things to schedule.
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The heuristic is the important part.  We're trying to be cheap,
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * since actually computing the optimal scheduling is NP complete.
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * What we do is track a "current clock".  When we schedule a node, we
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * update the earliest-unblocked clock time of its children, and
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * increment the clock.  Then, when trying to schedule, we just pick
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the earliest-unblocked instruction to schedule.
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Note that often there will be many things which could execute
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * immediately, and there are a range of heuristic options to choose
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * from in picking among those.
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass schedule_node : public exec_node
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic:
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   schedule_node(fs_inst *inst)
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->inst = inst;
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->child_array_size = 0;
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->children = NULL;
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->child_latency = NULL;
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->child_count = 0;
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->parent_count = 0;
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->unblocked_time = 0;
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      int chans = 8;
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      int math_latency = 22;
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      switch (inst->opcode) {
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case SHADER_OPCODE_RCP:
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 this->latency = 1 * chans * math_latency;
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case SHADER_OPCODE_RSQ:
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 this->latency = 2 * chans * math_latency;
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case SHADER_OPCODE_INT_QUOTIENT:
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case SHADER_OPCODE_SQRT:
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case SHADER_OPCODE_LOG2:
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 /* full precision log.  partial is 2. */
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 this->latency = 3 * chans * math_latency;
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case SHADER_OPCODE_INT_REMAINDER:
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case SHADER_OPCODE_EXP2:
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 /* full precision.  partial is 3, same throughput. */
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 this->latency = 4 * chans * math_latency;
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case SHADER_OPCODE_POW:
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 this->latency = 8 * chans * math_latency;
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case SHADER_OPCODE_SIN:
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case SHADER_OPCODE_COS:
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 /* minimum latency, max is 12 rounds. */
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 this->latency = 5 * chans * math_latency;
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      default:
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 this->latency = 2;
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   fs_inst *inst;
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   schedule_node **children;
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int *child_latency;
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int child_count;
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int parent_count;
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int child_array_size;
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int unblocked_time;
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int latency;
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass instruction_scheduler {
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic:
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   instruction_scheduler(fs_visitor *v, void *mem_ctx, int virtual_grf_count)
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->v = v;
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->mem_ctx = ralloc_context(mem_ctx);
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->virtual_grf_count = virtual_grf_count;
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->instructions.make_empty();
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->instructions_to_schedule = 0;
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ~instruction_scheduler()
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ralloc_free(this->mem_ctx);
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void add_barrier_deps(schedule_node *n);
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void add_dep(schedule_node *before, schedule_node *after, int latency);
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void add_dep(schedule_node *before, schedule_node *after);
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void add_inst(fs_inst *inst);
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void calculate_deps();
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void schedule_instructions(fs_inst *next_block_header);
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bool is_compressed(fs_inst *inst);
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void *mem_ctx;
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int instructions_to_schedule;
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int virtual_grf_count;
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   exec_list instructions;
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   fs_visitor *v;
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginstruction_scheduler::add_inst(fs_inst *inst)
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   schedule_node *n = new(mem_ctx) schedule_node(inst);
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(!inst->is_head_sentinel());
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(!inst->is_tail_sentinel());
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->instructions_to_schedule++;
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   inst->remove();
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   instructions.push_tail(n);
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Add a dependency between two instruction nodes.
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The @after node will be scheduled after @before.  We will try to
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * schedule it @latency cycles after @before, but no guarantees there.
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginstruction_scheduler::add_dep(schedule_node *before, schedule_node *after,
168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			       int latency)
169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!before || !after)
171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return;
172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(before != after);
174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (int i = 0; i < before->child_count; i++) {
176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (before->children[i] == after) {
177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 before->child_latency[i] = MAX2(before->child_latency[i], latency);
178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 return;
179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (before->child_array_size <= before->child_count) {
183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (before->child_array_size < 16)
184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 before->child_array_size = 16;
185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      else
186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 before->child_array_size *= 2;
187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      before->children = reralloc(mem_ctx, before->children,
189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  schedule_node *,
190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				  before->child_array_size);
191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      before->child_latency = reralloc(mem_ctx, before->child_latency,
192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				       int, before->child_array_size);
193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   before->children[before->child_count] = after;
196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   before->child_latency[before->child_count] = latency;
197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   before->child_count++;
198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   after->parent_count++;
199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginstruction_scheduler::add_dep(schedule_node *before, schedule_node *after)
203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!before)
205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return;
206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   add_dep(before, after, before->latency);
208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Sometimes we really want this node to execute after everything that
212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * was before it and before everything that followed it.  This adds
213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the deps to do so.
214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginstruction_scheduler::add_barrier_deps(schedule_node *n)
217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   schedule_node *prev = (schedule_node *)n->prev;
219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   schedule_node *next = (schedule_node *)n->next;
220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (prev) {
222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      while (!prev->is_head_sentinel()) {
223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_dep(prev, n, 0);
224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 prev = (schedule_node *)prev->prev;
225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (next) {
229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      while (!next->is_tail_sentinel()) {
230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_dep(n, next, 0);
231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 next = (schedule_node *)next->next;
232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/* instruction scheduling needs to be aware of when an MRF write
237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * actually writes 2 MRFs.
238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool
240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginstruction_scheduler::is_compressed(fs_inst *inst)
241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return (v->c->dispatch_width == 16 &&
243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	   !inst->force_uncompressed &&
244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	   !inst->force_sechalf);
245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginstruction_scheduler::calculate_deps()
249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   schedule_node *last_grf_write[virtual_grf_count];
251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   schedule_node *last_mrf_write[BRW_MAX_MRF];
252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   schedule_node *last_conditional_mod = NULL;
253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Fixed HW registers are assumed to be separate from the virtual
254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * GRFs, so they can be tracked separately.  We don't really write
255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * to fixed GRFs much, so don't bother tracking them on a more
256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * granular level.
257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   schedule_node *last_fixed_grf_write = NULL;
259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* The last instruction always needs to still be the last
261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * instruction.  Either it's flow control (IF, ELSE, ENDIF, DO,
262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * WHILE) and scheduling other things after it would disturb the
263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * basic block, or it's FB_WRITE and we should do a better job at
264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * dead code elimination anyway.
265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   schedule_node *last = (schedule_node *)instructions.get_tail();
267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   add_barrier_deps(last);
268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   memset(last_grf_write, 0, sizeof(last_grf_write));
270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   memset(last_mrf_write, 0, sizeof(last_mrf_write));
271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* top-to-bottom dependencies: RAW and WAW. */
273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   foreach_list(node, &instructions) {
274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      schedule_node *n = (schedule_node *)node;
275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      fs_inst *inst = n->inst;
276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* read-after-write deps. */
278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for (int i = 0; i < 3; i++) {
279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 if (inst->src[i].file == GRF) {
280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    add_dep(last_grf_write[inst->src[i].reg], n);
281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 } else if (inst->src[i].file == FIXED_HW_REG &&
282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    (inst->src[i].fixed_hw_reg.file ==
283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		     BRW_GENERAL_REGISTER_FILE)) {
284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    add_dep(last_fixed_grf_write, n);
285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 } else if (inst->src[i].file != BAD_FILE &&
286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    inst->src[i].file != IMM &&
287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    inst->src[i].file != UNIFORM) {
288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    assert(inst->src[i].file != MRF);
289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    add_barrier_deps(n);
290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for (int i = 0; i < inst->mlen; i++) {
294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 /* It looks like the MRF regs are released in the send
295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * instruction once it's sent, not when the result comes
296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * back.
297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  */
298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_dep(last_mrf_write[inst->base_mrf + i], n);
299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (inst->predicated) {
302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 assert(last_conditional_mod);
303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_dep(last_conditional_mod, n);
304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* write-after-write deps. */
307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (inst->dst.file == GRF) {
308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_dep(last_grf_write[inst->dst.reg], n);
309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 last_grf_write[inst->dst.reg] = n;
310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      } else if (inst->dst.file == MRF) {
311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 int reg = inst->dst.reg & ~BRW_MRF_COMPR4;
312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
313f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_dep(last_mrf_write[reg], n);
314f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 last_mrf_write[reg] = n;
315f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 if (is_compressed(inst)) {
316f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    if (inst->dst.reg & BRW_MRF_COMPR4)
317f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	       reg += 4;
318f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    else
319f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	       reg++;
320f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    add_dep(last_mrf_write[reg], n);
321f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    last_mrf_write[reg] = n;
322f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
323f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      } else if (inst->dst.file == FIXED_HW_REG &&
324f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		 inst->dst.fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE) {
325f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 last_fixed_grf_write = n;
326f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      } else if (inst->dst.file != BAD_FILE) {
327f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_barrier_deps(n);
328f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
329f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
330f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (inst->mlen > 0) {
331f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 for (int i = 0; i < v->implied_mrf_writes(inst); i++) {
332f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    add_dep(last_mrf_write[inst->base_mrf + i], n);
333f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    last_mrf_write[inst->base_mrf + i] = n;
334f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
335f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
336f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
337f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Treat FS_OPCODE_MOV_DISPATCH_TO_FLAGS as though it had a
338f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * conditional_mod, because it sets the flag register.
339f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
340f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (inst->conditional_mod ||
341f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          inst->opcode == FS_OPCODE_MOV_DISPATCH_TO_FLAGS) {
342f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_dep(last_conditional_mod, n, 0);
343f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 last_conditional_mod = n;
344f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
345f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
346f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
347f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* bottom-to-top dependencies: WAR */
348f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   memset(last_grf_write, 0, sizeof(last_grf_write));
349f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   memset(last_mrf_write, 0, sizeof(last_mrf_write));
350f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   last_conditional_mod = NULL;
351f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   last_fixed_grf_write = NULL;
352f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
353f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   exec_node *node;
354f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   exec_node *prev;
355f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (node = instructions.get_tail(), prev = node->prev;
356f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	!node->is_head_sentinel();
357f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	node = prev, prev = node->prev) {
358f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      schedule_node *n = (schedule_node *)node;
359f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      fs_inst *inst = n->inst;
360f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
361f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* write-after-read deps. */
362f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for (int i = 0; i < 3; i++) {
363f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 if (inst->src[i].file == GRF) {
364f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    add_dep(n, last_grf_write[inst->src[i].reg]);
365f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 } else if (inst->src[i].file == FIXED_HW_REG &&
366f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    (inst->src[i].fixed_hw_reg.file ==
367f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		     BRW_GENERAL_REGISTER_FILE)) {
368f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    add_dep(n, last_fixed_grf_write);
369f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 } else if (inst->src[i].file != BAD_FILE &&
370f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    inst->src[i].file != IMM &&
371f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    inst->src[i].file != UNIFORM) {
372f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    assert(inst->src[i].file != MRF);
373f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    add_barrier_deps(n);
374f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
375f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
376f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
377f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for (int i = 0; i < inst->mlen; i++) {
378f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 /* It looks like the MRF regs are released in the send
379f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * instruction once it's sent, not when the result comes
380f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * back.
381f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  */
382f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_dep(n, last_mrf_write[inst->base_mrf + i], 2);
383f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
384f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
385f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (inst->predicated) {
386f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_dep(n, last_conditional_mod);
387f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
388f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
389f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Update the things this instruction wrote, so earlier reads
390f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * can mark this as WAR dependency.
391f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
392f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (inst->dst.file == GRF) {
393f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 last_grf_write[inst->dst.reg] = n;
394f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      } else if (inst->dst.file == MRF) {
395f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 int reg = inst->dst.reg & ~BRW_MRF_COMPR4;
396f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
397f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 last_mrf_write[reg] = n;
398f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
399f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 if (is_compressed(inst)) {
400f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    if (inst->dst.reg & BRW_MRF_COMPR4)
401f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	       reg += 4;
402f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    else
403f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	       reg++;
404f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
405f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    last_mrf_write[reg] = n;
406f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
407f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      } else if (inst->dst.file == FIXED_HW_REG &&
408f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		 inst->dst.fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE) {
409f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 last_fixed_grf_write = n;
410f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      } else if (inst->dst.file != BAD_FILE) {
411f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 add_barrier_deps(n);
412f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
413f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
414f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (inst->mlen > 0) {
415f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 for (int i = 0; i < v->implied_mrf_writes(inst); i++) {
416f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    last_mrf_write[inst->base_mrf + i] = n;
417f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
418f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
419f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
420f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Treat FS_OPCODE_MOV_DISPATCH_TO_FLAGS as though it had a
421f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * conditional_mod, because it sets the flag register.
422f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
423f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (inst->conditional_mod ||
424f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          inst->opcode == FS_OPCODE_MOV_DISPATCH_TO_FLAGS) {
425f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 last_conditional_mod = n;
426f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
427f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
428f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
429f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
430f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
431f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginstruction_scheduler::schedule_instructions(fs_inst *next_block_header)
432f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
433f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int time = 0;
434f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
435f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Remove non-DAG heads from the list. */
436f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   foreach_list_safe(node, &instructions) {
437f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      schedule_node *n = (schedule_node *)node;
438f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (n->parent_count != 0)
439f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 n->remove();
440f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
441f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
442f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   while (!instructions.is_empty()) {
443f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      schedule_node *chosen = NULL;
444f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      int chosen_time = 0;
445f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
446f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      foreach_list(node, &instructions) {
447f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 schedule_node *n = (schedule_node *)node;
448f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
449f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 if (!chosen || n->unblocked_time < chosen_time) {
450f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    chosen = n;
451f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    chosen_time = n->unblocked_time;
452f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
453f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
454f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
455f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Schedule this instruction. */
456f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(chosen);
457f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      chosen->remove();
458f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      next_block_header->insert_before(chosen->inst);
459f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      instructions_to_schedule--;
460f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
461f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Bump the clock.  If we expected a delay for scheduling, then
462f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * bump the clock to reflect that.
463f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
464f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      time = MAX2(time + 1, chosen_time);
465f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
466f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Now that we've scheduled a new instruction, some of its
467f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * children can be promoted to the list of instructions ready to
468f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * be scheduled.  Update the children's unblocked time for this
469f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * DAG edge as we do so.
470f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
471f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for (int i = 0; i < chosen->child_count; i++) {
472f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 schedule_node *child = chosen->children[i];
473f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
474f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 child->unblocked_time = MAX2(child->unblocked_time,
475f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				      time + chosen->child_latency[i]);
476f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
477f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 child->parent_count--;
478f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 if (child->parent_count == 0) {
479f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    instructions.push_tail(child);
480f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
481f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
482f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
483f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Shared resource: the mathbox.  There's one per EU (on later
484f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * generations, it's even more limited pre-gen6), so if we send
485f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * something off to it then the next math isn't going to make
486f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * progress until the first is done.
487f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
488f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (chosen->inst->is_math()) {
489f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 foreach_list(node, &instructions) {
490f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    schedule_node *n = (schedule_node *)node;
491f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
492f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    if (n->inst->is_math())
493f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	       n->unblocked_time = MAX2(n->unblocked_time,
494f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org					time + chosen->latency);
495f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
496f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
497f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
498f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
499f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(instructions_to_schedule == 0);
500f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
501f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
502f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
503f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgfs_visitor::schedule_instructions()
504f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
505f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   fs_inst *next_block_header = (fs_inst *)instructions.head;
506f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   instruction_scheduler sched(this, mem_ctx, this->virtual_grf_count);
507f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
508f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   while (!next_block_header->is_tail_sentinel()) {
509f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Add things to be scheduled until we get to a new BB. */
510f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      while (!next_block_header->is_tail_sentinel()) {
511f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 fs_inst *inst = next_block_header;
512f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 next_block_header = (fs_inst *)next_block_header->next;
513f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
514f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 sched.add_inst(inst);
515f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 if (inst->opcode == BRW_OPCODE_IF ||
516f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	     inst->opcode == BRW_OPCODE_ELSE ||
517f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	     inst->opcode == BRW_OPCODE_ENDIF ||
518f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	     inst->opcode == BRW_OPCODE_DO ||
519f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	     inst->opcode == BRW_OPCODE_WHILE ||
520f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	     inst->opcode == BRW_OPCODE_BREAK ||
521f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	     inst->opcode == BRW_OPCODE_CONTINUE) {
522f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	    break;
523f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 }
524f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
525f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      sched.calculate_deps();
526f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      sched.schedule_instructions(next_block_header);
527f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
528f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
529f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->live_intervals_valid = false;
530f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
531