2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
30 * Find/group instruction neighbors:
34 insert_mov(struct ir3_instruction
*collect
, int idx
)
36 struct ir3_instruction
*src
= ssa(collect
->regs
[idx
+1]);
37 struct ir3_instruction
*mov
= ir3_MOV(src
->block
, src
, TYPE_F32
);
38 collect
->regs
[idx
+1]->instr
= mov
;
40 /* if collect and src are in the same block, move the inserted mov
41 * to just before the collect to avoid a use-before-def. Otherwise
42 * it should be safe to leave at the end of the block it is in:
44 if (src
->block
== collect
->block
) {
45 list_delinit(&mov
->node
);
46 list_addtail(&mov
->node
, &collect
->node
);
50 /* verify that cur != instr, but cur is also not in instr's neighbor-list: */
52 in_neighbor_list(struct ir3_instruction
*instr
, struct ir3_instruction
*cur
, int pos
)
62 for (instr
= ir3_neighbor_first(instr
); instr
; instr
= instr
->cp
.right
)
63 if ((idx
++ != pos
) && (instr
== cur
))
70 group_collect(struct ir3_instruction
*collect
)
72 struct ir3_register
**regs
= &collect
->regs
[1];
73 unsigned n
= collect
->regs_count
- 1;
75 /* first pass, figure out what has conflicts and needs a mov
76 * inserted. Do this up front, before starting to setup
77 * left/right neighbor pointers. Trying to do it in a single
78 * pass could result in a situation where we can't even setup
79 * the mov's right neighbor ptr if the next instr also needs
83 for (unsigned i
= 0; i
< n
; i
++) {
84 struct ir3_instruction
*instr
= ssa(regs
[i
]);
86 struct ir3_instruction
*left
= (i
> 0) ? ssa(regs
[i
- 1]) : NULL
;
87 struct ir3_instruction
*right
= (i
< (n
-1)) ? ssa(regs
[i
+ 1]) : NULL
;
90 /* check for left/right neighbor conflicts: */
91 conflict
= conflicts(instr
->cp
.left
, left
) ||
92 conflicts(instr
->cp
.right
, right
);
94 /* Mixing array elements and higher register classes
95 * (ie. groups) doesn't really work out in RA. See:
97 * https://trello.com/c/DqeDkeVf/156-bug-with-stk-70frag
99 if (instr
->regs
[0]->flags
& IR3_REG_ARRAY
)
102 /* we also can't have an instr twice in the group: */
103 for (unsigned j
= i
+ 1; (j
< n
) && !conflict
; j
++)
104 if (in_neighbor_list(ssa(regs
[j
]), instr
, i
))
108 insert_mov(collect
, i
);
109 /* inserting the mov may have caused a conflict
110 * against the previous:
117 /* second pass, now that we've inserted mov's, fixup left/right
118 * neighbors. This is guaranteed to succeed, since by definition
119 * the newly inserted mov's cannot conflict with anything.
121 for (unsigned i
= 0; i
< n
; i
++) {
122 struct ir3_instruction
*instr
= ssa(regs
[i
]);
124 struct ir3_instruction
*left
= (i
> 0) ? ssa(regs
[i
- 1]) : NULL
;
125 struct ir3_instruction
*right
= (i
< (n
-1)) ? ssa(regs
[i
+ 1]) : NULL
;
127 debug_assert(!conflicts(instr
->cp
.left
, left
));
129 instr
->cp
.left_cnt
++;
130 instr
->cp
.left
= left
;
133 debug_assert(!conflicts(instr
->cp
.right
, right
));
135 instr
->cp
.right_cnt
++;
136 instr
->cp
.right
= right
;
143 instr_find_neighbors(struct ir3_instruction
*instr
)
145 bool progress
= false;
147 if (ir3_instr_check_mark(instr
))
150 if (instr
->opc
== OPC_META_COLLECT
) {
151 group_collect(instr
);
155 foreach_ssa_src (src
, instr
)
156 progress
|= instr_find_neighbors(src
);
162 find_neighbors(struct ir3
*ir
)
164 bool progress
= false;
167 foreach_output (out
, ir
)
168 progress
|= instr_find_neighbors(out
);
170 foreach_block (block
, &ir
->block_list
) {
171 for (i
= 0; i
< block
->keeps_count
; i
++) {
172 struct ir3_instruction
*instr
= block
->keeps
[i
];
173 progress
|= instr_find_neighbors(instr
);
176 /* We also need to account for if-condition: */
177 if (block
->condition
)
178 progress
|= instr_find_neighbors(block
->condition
);
185 ir3_group(struct ir3
*ir
)
188 return find_neighbors(ir
);