1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
29 #include "freedreno_util.h"
34 * Find/group instruction neighbors:
37 /* bleh.. we need to do the same group_n() thing for both inputs/outputs
38 * (where we have a simple instr[] array), and fanin nodes (where we have
39 * an extra indirection via reg->instr).
42 struct ir3_instruction
*(*get
)(void *arr
, int idx
);
43 void (*insert_mov
)(void *arr
, int idx
, struct ir3_instruction
*instr
);
46 static struct ir3_instruction
*arr_get(void *arr
, int idx
)
48 return ((struct ir3_instruction
**)arr
)[idx
];
50 static void arr_insert_mov_out(void *arr
, int idx
, struct ir3_instruction
*instr
)
52 ((struct ir3_instruction
**)arr
)[idx
] =
53 ir3_MOV(instr
->block
, instr
, TYPE_F32
);
55 static void arr_insert_mov_in(void *arr
, int idx
, struct ir3_instruction
*instr
)
57 /* so, we can't insert a mov in front of a meta:in.. and the downstream
58 * instruction already has a pointer to 'instr'. So we cheat a bit and
59 * morph the meta:in instruction into a mov and insert a new meta:in
62 struct ir3_instruction
*in
;
64 debug_assert(instr
->regs_count
== 1);
66 in
= ir3_instr_create(instr
->block
, -1, OPC_META_INPUT
);
67 in
->inout
.block
= instr
->block
;
68 ir3_reg_create(in
, instr
->regs
[0]->num
, 0);
70 /* create src reg for meta:in and fixup to now be a mov: */
71 ir3_reg_create(instr
, 0, IR3_REG_SSA
)->instr
= in
;
74 instr
->cat1
.src_type
= TYPE_F32
;
75 instr
->cat1
.dst_type
= TYPE_F32
;
77 ((struct ir3_instruction
**)arr
)[idx
] = in
;
79 static struct group_ops arr_ops_out
= { arr_get
, arr_insert_mov_out
};
80 static struct group_ops arr_ops_in
= { arr_get
, arr_insert_mov_in
};
82 static struct ir3_instruction
*instr_get(void *arr
, int idx
)
84 return ssa(((struct ir3_instruction
*)arr
)->regs
[idx
+1]);
87 instr_insert_mov(void *arr
, int idx
, struct ir3_instruction
*instr
)
89 ((struct ir3_instruction
*)arr
)->regs
[idx
+1]->instr
=
90 ir3_MOV(instr
->block
, instr
, TYPE_F32
);
92 static struct group_ops instr_ops
= { instr_get
, instr_insert_mov
};
96 group_n(struct group_ops
*ops
, void *arr
, unsigned n
)
100 /* first pass, figure out what has conflicts and needs a mov
101 * inserted. Do this up front, before starting to setup
102 * left/right neighbor pointers. Trying to do it in a single
103 * pass could result in a situation where we can't even setup
104 * the mov's right neighbor ptr if the next instr also needs
108 for (i
= 0; i
< n
; i
++) {
109 struct ir3_instruction
*instr
= ops
->get(arr
, i
);
111 struct ir3_instruction
*left
= (i
> 0) ? ops
->get(arr
, i
- 1) : NULL
;
112 struct ir3_instruction
*right
= (i
< (n
-1)) ? ops
->get(arr
, i
+ 1) : NULL
;
115 /* check for left/right neighbor conflicts: */
116 conflict
= conflicts(instr
->cp
.left
, left
) ||
117 conflicts(instr
->cp
.right
, right
);
119 /* RA can't yet deal very well w/ group'd phi's: */
120 if (is_meta(instr
) && (instr
->opc
== OPC_META_PHI
))
123 /* we also can't have an instr twice in the group: */
124 for (j
= i
+ 1; (j
< n
) && !conflict
; j
++)
125 if (ops
->get(arr
, j
) == instr
)
129 ops
->insert_mov(arr
, i
, instr
);
130 /* inserting the mov may have caused a conflict
131 * against the previous:
138 /* second pass, now that we've inserted mov's, fixup left/right
139 * neighbors. This is guaranteed to succeed, since by definition
140 * the newly inserted mov's cannot conflict with anything.
142 for (i
= 0; i
< n
; i
++) {
143 struct ir3_instruction
*instr
= ops
->get(arr
, i
);
145 struct ir3_instruction
*left
= (i
> 0) ? ops
->get(arr
, i
- 1) : NULL
;
146 struct ir3_instruction
*right
= (i
< (n
-1)) ? ops
->get(arr
, i
+ 1) : NULL
;
148 debug_assert(!conflicts(instr
->cp
.left
, left
));
150 instr
->cp
.left_cnt
++;
151 instr
->cp
.left
= left
;
154 debug_assert(!conflicts(instr
->cp
.right
, right
));
156 instr
->cp
.right_cnt
++;
157 instr
->cp
.right
= right
;
164 instr_find_neighbors(struct ir3_instruction
*instr
)
166 struct ir3_instruction
*src
;
168 if (ir3_instr_check_mark(instr
))
171 if (is_meta(instr
) && (instr
->opc
== OPC_META_FI
))
172 group_n(&instr_ops
, instr
, instr
->regs_count
- 1);
174 foreach_ssa_src(src
, instr
)
175 instr_find_neighbors(src
);
178 /* a bit of sadness.. we can't have "holes" in inputs from PoV of
179 * register assignment, they still need to be grouped together. So
180 * we need to insert dummy/padding instruction for grouping, and
181 * then take it back out again before anyone notices.
184 pad_and_group_input(struct ir3_instruction
**input
, unsigned n
)
187 struct ir3_block
*block
= NULL
;
189 for (i
= n
- 1; i
>= 0; i
--) {
190 struct ir3_instruction
*instr
= input
[i
];
192 block
= instr
->block
;
194 instr
= ir3_NOP(block
);
195 ir3_reg_create(instr
, 0, IR3_REG_SSA
); /* dummy dst */
201 group_n(&arr_ops_in
, input
, n
);
203 for (i
= 0; i
< n
; i
++) {
210 find_neighbors(struct ir3
*ir
)
214 /* shader inputs/outputs themselves must be contiguous as well:
216 * NOTE: group inputs first, since we only insert mov's
217 * *before* the conflicted instr (and that would go badly
218 * for inputs). By doing inputs first, we should never
219 * have a conflict on inputs.. pushing any conflict to
220 * resolve to the outputs, for stuff like:
222 * MOV OUT[n], IN[m].wzyx
224 * NOTE: we assume here inputs/outputs are grouped in vec4.
225 * This logic won't quite cut it if we don't align smaller
228 for (i
= 0; i
< ir
->ninputs
; i
+= 4)
229 pad_and_group_input(&ir
->inputs
[i
], 4);
230 for (i
= 0; i
< ir
->noutputs
; i
+= 4)
231 group_n(&arr_ops_out
, &ir
->outputs
[i
], 4);
233 for (i
= 0; i
< ir
->noutputs
; i
++) {
234 if (ir
->outputs
[i
]) {
235 struct ir3_instruction
*instr
= ir
->outputs
[i
];
236 instr_find_neighbors(instr
);
242 ir3_group(struct ir3
*ir
)