freedreno/ir3/print: print (r) flag
[mesa.git] / src / freedreno / ir3 / ir3_group.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "ir3.h"
28
29 /*
30 * Find/group instruction neighbors:
31 */
32
33 static void
34 insert_mov(struct ir3_instruction *collect, int idx)
35 {
36 struct ir3_instruction *src = ssa(collect->regs[idx+1]);
37 struct ir3_instruction *mov = ir3_MOV(src->block, src,
38 (collect->regs[idx+1]->flags & IR3_REG_HALF) ? TYPE_U16 : TYPE_U32);
39
40 collect->regs[idx+1]->instr = mov;
41
42 /* if collect and src are in the same block, move the inserted mov
43 * to just before the collect to avoid a use-before-def. Otherwise
44 * it should be safe to leave at the end of the block it is in:
45 */
46 if (src->block == collect->block) {
47 list_delinit(&mov->node);
48 list_addtail(&mov->node, &collect->node);
49 }
50 }
51
52 /* verify that cur != instr, but cur is also not in instr's neighbor-list: */
53 static bool
54 in_neighbor_list(struct ir3_instruction *instr, struct ir3_instruction *cur, int pos)
55 {
56 int idx = 0;
57
58 if (!instr)
59 return false;
60
61 if (instr == cur)
62 return true;
63
64 for (instr = ir3_neighbor_first(instr); instr; instr = instr->cp.right)
65 if ((idx++ != pos) && (instr == cur))
66 return true;
67
68 return false;
69 }
70
71 static void
72 group_collect(struct ir3_instruction *collect)
73 {
74 struct ir3_register **regs = &collect->regs[1];
75 unsigned n = collect->regs_count - 1;
76
77 /* first pass, figure out what has conflicts and needs a mov
78 * inserted. Do this up front, before starting to setup
79 * left/right neighbor pointers. Trying to do it in a single
80 * pass could result in a situation where we can't even setup
81 * the mov's right neighbor ptr if the next instr also needs
82 * a mov.
83 */
84 restart:
85 for (unsigned i = 0; i < n; i++) {
86 struct ir3_instruction *instr = ssa(regs[i]);
87 if (instr) {
88 struct ir3_instruction *left = (i > 0) ? ssa(regs[i - 1]) : NULL;
89 struct ir3_instruction *right = (i < (n-1)) ? ssa(regs[i + 1]) : NULL;
90 bool conflict;
91
92 /* check for left/right neighbor conflicts: */
93 conflict = conflicts(instr->cp.left, left) ||
94 conflicts(instr->cp.right, right);
95
96 /* Mixing array elements and higher register classes
97 * (ie. groups) doesn't really work out in RA. See:
98 *
99 * https://trello.com/c/DqeDkeVf/156-bug-with-stk-70frag
100 */
101 if (instr->regs[0]->flags & IR3_REG_ARRAY)
102 conflict = true;
103
104 /* we also can't have an instr twice in the group: */
105 for (unsigned j = i + 1; (j < n) && !conflict; j++)
106 if (in_neighbor_list(ssa(regs[j]), instr, i))
107 conflict = true;
108
109 if (conflict) {
110 insert_mov(collect, i);
111 /* inserting the mov may have caused a conflict
112 * against the previous:
113 */
114 goto restart;
115 }
116 }
117 }
118
119 /* second pass, now that we've inserted mov's, fixup left/right
120 * neighbors. This is guaranteed to succeed, since by definition
121 * the newly inserted mov's cannot conflict with anything.
122 */
123 for (unsigned i = 0; i < n; i++) {
124 struct ir3_instruction *instr = ssa(regs[i]);
125 if (instr) {
126 struct ir3_instruction *left = (i > 0) ? ssa(regs[i - 1]) : NULL;
127 struct ir3_instruction *right = (i < (n-1)) ? ssa(regs[i + 1]) : NULL;
128
129 debug_assert(!conflicts(instr->cp.left, left));
130 if (left) {
131 instr->cp.left_cnt++;
132 instr->cp.left = left;
133 }
134
135 debug_assert(!conflicts(instr->cp.right, right));
136 if (right) {
137 instr->cp.right_cnt++;
138 instr->cp.right = right;
139 }
140 }
141 }
142 }
143
144 static bool
145 instr_find_neighbors(struct ir3_instruction *instr)
146 {
147 bool progress = false;
148
149 if (ir3_instr_check_mark(instr))
150 return false;
151
152 if (instr->opc == OPC_META_COLLECT) {
153 group_collect(instr);
154 progress = true;
155 }
156
157 foreach_ssa_src (src, instr)
158 progress |= instr_find_neighbors(src);
159
160 return progress;
161 }
162
163 static bool
164 find_neighbors(struct ir3 *ir)
165 {
166 bool progress = false;
167 unsigned i;
168
169 foreach_output (out, ir)
170 progress |= instr_find_neighbors(out);
171
172 foreach_block (block, &ir->block_list) {
173 for (i = 0; i < block->keeps_count; i++) {
174 struct ir3_instruction *instr = block->keeps[i];
175 progress |= instr_find_neighbors(instr);
176 }
177
178 /* We also need to account for if-condition: */
179 if (block->condition)
180 progress |= instr_find_neighbors(block->condition);
181 }
182
183 return progress;
184 }
185
186 bool
187 ir3_group(struct ir3 *ir)
188 {
189 ir3_clear_mark(ir);
190 return find_neighbors(ir);
191 }