2 * Copyright © 2018 Valve Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Daniel Schürmann (daniel.schuermann@campus.tu-berlin.de)
25 * Bas Nieuwenhuizen (bas@basnieuwenhuizen.nl)
32 #include <unordered_map>
37 #include "util/u_math.h"
43 std::bitset
<512> war_hint
;
45 std::unordered_map
<unsigned, std::pair
<PhysReg
, RegClass
>> assignments
;
46 std::map
<unsigned, Temp
> orig_names
;
47 unsigned max_used_sgpr
= 0;
48 unsigned max_used_vgpr
= 0;
49 std::bitset
<64> defs_done
; /* see MAX_ARGS in aco_instruction_selection_setup.cpp */
51 ra_ctx(Program
* program
) : program(program
) {}
55 /* helper function for debugging */
57 void print_regs(ra_ctx
& ctx
, bool vgprs
, std::array
<uint32_t, 512>& reg_file
)
59 unsigned max
= vgprs
? ctx
.program
->max_reg_demand
.vgpr
: ctx
.program
->max_reg_demand
.sgpr
;
60 unsigned lb
= vgprs
? 256 : 0;
61 unsigned ub
= lb
+ max
;
62 char reg_char
= vgprs
? 'v' : 's';
66 for (unsigned i
= lb
; i
< ub
; i
+= 3) {
67 printf("%.2u ", i
- lb
);
72 printf("%cgprs: ", reg_char
);
73 unsigned free_regs
= 0;
75 bool char_select
= false;
76 for (unsigned i
= lb
; i
< ub
; i
++) {
77 if (reg_file
[i
] == 0xFFFF) {
79 } else if (reg_file
[i
]) {
80 if (reg_file
[i
] != prev
) {
82 char_select
= !char_select
;
84 printf(char_select
? "#" : "@");
92 printf("%u/%u used, %u/%u free\n", max
- free_regs
, max
, free_regs
, max
);
94 /* print assignments */
97 for (unsigned i
= lb
; i
< ub
; i
++) {
98 if (reg_file
[i
] != prev
) {
100 printf("-%d]\n", i
- 1 - lb
);
104 if (prev
&& prev
!= 0xFFFF) {
105 if (ctx
.orig_names
.count(reg_file
[i
]) && ctx
.orig_names
[reg_file
[i
]].id() != reg_file
[i
])
106 printf("%%%u (was %%%d) = %c[%d", reg_file
[i
], ctx
.orig_names
[reg_file
[i
]].id(), reg_char
, i
- lb
);
108 printf("%%%u = %c[%d", reg_file
[i
], reg_char
, i
- lb
);
115 if (prev
&& size
> 1)
116 printf("-%d]\n", ub
- lb
- 1);
123 void adjust_max_used_regs(ra_ctx
& ctx
, RegClass rc
, unsigned reg
)
125 unsigned max_addressible_sgpr
= ctx
.program
->sgpr_limit
;
126 unsigned size
= rc
.size();
127 if (rc
.type() == RegType::vgpr
) {
129 unsigned hi
= reg
- 256 + size
- 1;
130 ctx
.max_used_vgpr
= std::max(ctx
.max_used_vgpr
, hi
);
131 } else if (reg
+ rc
.size() <= max_addressible_sgpr
) {
132 unsigned hi
= reg
+ size
- 1;
133 ctx
.max_used_sgpr
= std::max(ctx
.max_used_sgpr
, std::min(hi
, max_addressible_sgpr
));
138 void update_renames(ra_ctx
& ctx
, std::array
<uint32_t, 512>& reg_file
,
139 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
140 aco_ptr
<Instruction
>& instr
)
142 /* allocate id's and rename operands: this is done transparently here */
143 for (std::pair
<Operand
, Definition
>& copy
: parallelcopies
) {
144 /* the definitions with id are not from this function and already handled */
145 if (copy
.second
.isTemp())
148 // FIXME: if a definition got moved, change the target location and remove the parallelcopy
149 copy
.second
.setTemp(Temp(ctx
.program
->allocateId(), copy
.second
.regClass()));
150 ctx
.assignments
[copy
.second
.tempId()] = {copy
.second
.physReg(), copy
.second
.regClass()};
151 for (unsigned i
= copy
.second
.physReg().reg
; i
< copy
.second
.physReg() + copy
.second
.size(); i
++)
152 reg_file
[i
] = copy
.second
.tempId();
153 /* check if we moved an operand */
154 for (Operand
& op
: instr
->operands
) {
157 if (op
.tempId() == copy
.first
.tempId()) {
158 bool omit_renaming
= instr
->opcode
== aco_opcode::p_create_vector
&& !op
.isKill();
159 for (std::pair
<Operand
, Definition
>& pc
: parallelcopies
) {
160 PhysReg def_reg
= pc
.second
.physReg();
161 omit_renaming
&= def_reg
> copy
.first
.physReg() ?
162 (copy
.first
.physReg() + copy
.first
.size() <= def_reg
.reg
) :
163 (def_reg
+ pc
.second
.size() <= copy
.first
.physReg().reg
);
167 op
.setTemp(copy
.second
.getTemp());
168 op
.setFixed(copy
.second
.physReg());
174 std::pair
<PhysReg
, bool> get_reg_simple(ra_ctx
& ctx
,
175 std::array
<uint32_t, 512>& reg_file
,
176 uint32_t lb
, uint32_t ub
,
177 uint32_t size
, uint32_t stride
,
180 /* best fit algorithm: find the smallest gap to fit in the variable */
182 unsigned best_pos
= 0xFFFF;
183 unsigned gap_size
= 0xFFFF;
184 unsigned next_pos
= 0xFFFF;
186 for (unsigned current_reg
= lb
; current_reg
< ub
; current_reg
++) {
187 if (reg_file
[current_reg
] != 0 || ctx
.war_hint
[current_reg
]) {
188 if (next_pos
== 0xFFFF)
191 /* check if the variable fits */
192 if (next_pos
+ size
> current_reg
) {
197 /* check if the tested gap is smaller */
198 if (current_reg
- next_pos
< gap_size
) {
200 gap_size
= current_reg
- next_pos
;
206 if (next_pos
== 0xFFFF)
207 next_pos
= current_reg
;
211 if (next_pos
!= 0xFFFF &&
212 next_pos
+ size
<= ub
&&
213 ub
- next_pos
< gap_size
) {
215 gap_size
= ub
- next_pos
;
217 if (best_pos
!= 0xFFFF) {
218 adjust_max_used_regs(ctx
, rc
, best_pos
);
219 return {PhysReg
{best_pos
}, true};
225 unsigned reg_lo
= lb
;
226 unsigned reg_hi
= lb
+ size
- 1;
227 while (!found
&& reg_lo
+ size
<= ub
) {
228 if (reg_file
[reg_lo
] != 0) {
232 reg_hi
= reg_lo
+ size
- 1;
234 for (unsigned reg
= reg_lo
+ 1; found
&& reg
<= reg_hi
; reg
++) {
235 if (reg_file
[reg
] != 0 || ctx
.war_hint
[reg
])
239 adjust_max_used_regs(ctx
, rc
, reg_lo
);
240 return {PhysReg
{reg_lo
}, true};
249 bool get_regs_for_copies(ra_ctx
& ctx
,
250 std::array
<uint32_t, 512>& reg_file
,
251 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
252 std::set
<std::pair
<unsigned, unsigned>> vars
,
253 uint32_t lb
, uint32_t ub
,
254 aco_ptr
<Instruction
>& instr
,
259 /* variables are sorted from small sized to large */
260 /* NOTE: variables are also sorted by ID. this only affects a very small number of shaders slightly though. */
261 for (std::set
<std::pair
<unsigned, unsigned>>::reverse_iterator it
= vars
.rbegin(); it
!= vars
.rend(); ++it
) {
262 unsigned id
= it
->second
;
263 std::pair
<PhysReg
, RegClass
> var
= ctx
.assignments
[id
];
264 uint32_t size
= it
->first
;
266 if (var
.second
.type() == RegType::sgpr
) {
273 /* check if this is a dead operand, then we can re-use the space from the definition */
274 bool is_dead_operand
= false;
275 for (unsigned i
= 0; !is_phi(instr
) && !is_dead_operand
&& i
< instr
->operands
.size(); i
++) {
276 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isKill() && instr
->operands
[i
].tempId() == id
)
277 is_dead_operand
= true;
280 std::pair
<PhysReg
, bool> res
;
281 if (is_dead_operand
) {
282 if (instr
->opcode
== aco_opcode::p_create_vector
) {
283 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].size(), i
++) {
284 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].tempId() == id
) {
285 for (unsigned j
= 0; j
< size
; j
++)
286 assert(reg_file
[def_reg_lo
+ offset
+ j
] == 0);
287 res
= {PhysReg
{def_reg_lo
+ offset
}, true};
292 res
= get_reg_simple(ctx
, reg_file
, def_reg_lo
, def_reg_hi
+ 1, size
, stride
, var
.second
);
295 res
= get_reg_simple(ctx
, reg_file
, lb
, def_reg_lo
, size
, stride
, var
.second
);
297 unsigned lb
= (def_reg_hi
+ stride
) & ~(stride
- 1);
298 res
= get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, stride
, var
.second
);
303 /* mark the area as blocked */
304 for (unsigned i
= res
.first
.reg
; i
< res
.first
+ size
; i
++)
305 reg_file
[i
] = 0xFFFFFFFF;
306 /* create parallelcopy pair (without definition id) */
307 Temp tmp
= Temp(id
, var
.second
);
308 Operand pc_op
= Operand(tmp
);
309 pc_op
.setFixed(var
.first
);
310 Definition pc_def
= Definition(res
.first
, pc_op
.regClass());
311 parallelcopies
.emplace_back(pc_op
, pc_def
);
315 unsigned best_pos
= lb
;
316 unsigned num_moves
= 0xFF;
317 unsigned num_vars
= 0;
319 /* we use a sliding window to find potential positions */
320 unsigned reg_lo
= lb
;
321 unsigned reg_hi
= lb
+ size
- 1;
322 for (reg_lo
= lb
, reg_hi
= lb
+ size
- 1; reg_hi
< ub
; reg_lo
+= stride
, reg_hi
+= stride
) {
323 if (!is_dead_operand
&& ((reg_lo
>= def_reg_lo
&& reg_lo
<= def_reg_hi
) ||
324 (reg_hi
>= def_reg_lo
&& reg_hi
<= def_reg_hi
)))
327 /* second, check that we have at most k=num_moves elements in the window
328 * and no element is larger than the currently processed one */
331 unsigned last_var
= 0;
333 for (unsigned j
= reg_lo
; found
&& j
<= reg_hi
; j
++) {
334 if (reg_file
[j
] == 0 || reg_file
[j
] == last_var
)
337 /* 0xFFFF signals that this area is already blocked! */
338 if (reg_file
[j
] == 0xFFFFFFFF || k
> num_moves
) {
342 /* we cannot split live ranges of linear vgprs */
343 if (ctx
.assignments
[reg_file
[j
]].second
& (1 << 6)) {
347 bool is_kill
= false;
348 for (const Operand
& op
: instr
->operands
) {
349 if (op
.isTemp() && op
.isKill() && op
.tempId() == reg_file
[j
]) {
354 if (!is_kill
&& ctx
.assignments
[reg_file
[j
]].second
.size() >= size
) {
359 k
+= ctx
.assignments
[reg_file
[j
]].second
.size();
360 last_var
= reg_file
[j
];
362 if (k
> num_moves
|| (k
== num_moves
&& n
<= num_vars
)) {
375 /* FIXME: we messed up and couldn't find space for the variables to be copied */
376 if (num_moves
== 0xFF)
380 reg_hi
= best_pos
+ size
- 1;
382 /* collect variables and block reg file */
383 std::set
<std::pair
<unsigned, unsigned>> new_vars
;
384 for (unsigned j
= reg_lo
; j
<= reg_hi
; j
++) {
385 if (reg_file
[j
] != 0) {
386 unsigned size
= ctx
.assignments
[reg_file
[j
]].second
.size();
387 unsigned id
= reg_file
[j
];
388 new_vars
.emplace(size
, id
);
389 for (unsigned k
= 0; k
< size
; k
++)
390 reg_file
[ctx
.assignments
[id
].first
+ k
] = 0;
394 /* mark the area as blocked */
395 for (unsigned i
= reg_lo
; i
<= reg_hi
; i
++)
396 reg_file
[i
] = 0xFFFFFFFF;
398 if (!get_regs_for_copies(ctx
, reg_file
, parallelcopies
, new_vars
, lb
, ub
, instr
, def_reg_lo
, def_reg_hi
))
401 adjust_max_used_regs(ctx
, var
.second
, reg_lo
);
403 /* create parallelcopy pair (without definition id) */
404 Temp tmp
= Temp(id
, var
.second
);
405 Operand pc_op
= Operand(tmp
);
406 pc_op
.setFixed(var
.first
);
407 Definition pc_def
= Definition(PhysReg
{reg_lo
}, pc_op
.regClass());
408 parallelcopies
.emplace_back(pc_op
, pc_def
);
415 std::pair
<PhysReg
, bool> get_reg_impl(ra_ctx
& ctx
,
416 std::array
<uint32_t, 512>& reg_file
,
417 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
418 uint32_t lb
, uint32_t ub
,
419 uint32_t size
, uint32_t stride
,
421 aco_ptr
<Instruction
>& instr
)
423 unsigned regs_free
= 0;
424 /* check how many free regs we have */
425 for (unsigned j
= lb
; j
< ub
; j
++) {
426 if (reg_file
[j
] == 0)
430 /* mark and count killed operands */
431 unsigned killed_ops
= 0;
432 for (unsigned j
= 0; !is_phi(instr
) && j
< instr
->operands
.size(); j
++) {
433 if (instr
->operands
[j
].isTemp() &&
434 instr
->operands
[j
].isFirstKill() &&
435 instr
->operands
[j
].physReg() >= lb
&&
436 instr
->operands
[j
].physReg() < ub
) {
437 assert(instr
->operands
[j
].isFixed());
438 assert(reg_file
[instr
->operands
[j
].physReg().reg
] == 0);
439 for (unsigned k
= 0; k
< instr
->operands
[j
].size(); k
++)
440 reg_file
[instr
->operands
[j
].physReg() + k
] = 0xFFFFFFFF;
441 killed_ops
+= instr
->operands
[j
].getTemp().size();
445 assert(regs_free
>= size
);
446 /* we might have to move dead operands to dst in order to make space */
447 unsigned op_moves
= 0;
449 if (size
> (regs_free
- killed_ops
))
450 op_moves
= size
- (regs_free
- killed_ops
);
452 /* find the best position to place the definition */
453 unsigned best_pos
= lb
;
454 unsigned num_moves
= 0xFF;
455 unsigned num_vars
= 0;
457 /* we use a sliding window to check potential positions */
458 unsigned reg_lo
= lb
;
459 unsigned reg_hi
= lb
+ size
- 1;
460 for (reg_lo
= lb
, reg_hi
= lb
+ size
- 1; reg_hi
< ub
; reg_lo
+= stride
, reg_hi
+= stride
) {
461 /* first check the edges: this is what we have to fix to allow for num_moves > size */
462 if (reg_lo
> lb
&& reg_file
[reg_lo
] != 0 && reg_file
[reg_lo
] == reg_file
[reg_lo
- 1])
464 if (reg_hi
< ub
- 1 && reg_file
[reg_hi
] != 0 && reg_file
[reg_hi
] == reg_file
[reg_hi
+ 1])
467 /* second, check that we have at most k=num_moves elements in the window
468 * and no element is larger than the currently processed one */
469 unsigned k
= op_moves
;
471 unsigned remaining_op_moves
= op_moves
;
472 unsigned last_var
= 0;
474 bool aligned
= rc
== RegClass::v4
&& reg_lo
% 4 == 0;
475 for (unsigned j
= reg_lo
; found
&& j
<= reg_hi
; j
++) {
476 if (reg_file
[j
] == 0 || reg_file
[j
] == last_var
)
479 /* dead operands effectively reduce the number of estimated moves */
480 if (remaining_op_moves
&& reg_file
[j
] == 0xFFFFFFFF) {
482 remaining_op_moves
--;
486 if (ctx
.assignments
[reg_file
[j
]].second
.size() >= size
) {
492 /* we cannot split live ranges of linear vgprs */
493 if (ctx
.assignments
[reg_file
[j
]].second
& (1 << 6)) {
498 k
+= ctx
.assignments
[reg_file
[j
]].second
.size();
500 last_var
= reg_file
[j
];
503 if (!found
|| k
> num_moves
)
505 if (k
== num_moves
&& n
< num_vars
)
507 if (!aligned
&& k
== num_moves
&& n
== num_vars
)
517 if (num_moves
== 0xFF) {
518 /* remove killed operands from reg_file once again */
519 for (unsigned i
= 0; !is_phi(instr
) && i
< instr
->operands
.size(); i
++) {
520 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isFirstKill()) {
521 for (unsigned k
= 0; k
< instr
->operands
[i
].getTemp().size(); k
++)
522 reg_file
[instr
->operands
[i
].physReg() + k
] = 0;
525 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
526 Definition def
= instr
->definitions
[i
];
527 if (def
.isTemp() && def
.isFixed() && ctx
.defs_done
.test(i
)) {
528 for (unsigned k
= 0; k
< def
.getTemp().size(); k
++)
529 reg_file
[def
.physReg() + k
] = def
.tempId();
535 std::array
<uint32_t, 512> register_file
= reg_file
;
537 /* now, we figured the placement for our definition */
538 std::set
<std::pair
<unsigned, unsigned>> vars
;
539 for (unsigned j
= best_pos
; j
< best_pos
+ size
; j
++) {
540 if (reg_file
[j
] != 0xFFFFFFFF && reg_file
[j
] != 0)
541 vars
.emplace(ctx
.assignments
[reg_file
[j
]].second
.size(), reg_file
[j
]);
545 if (instr
->opcode
== aco_opcode::p_create_vector
) {
546 /* move killed operands which aren't yet at the correct position */
547 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].size(), i
++) {
548 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isFirstKill() &&
549 instr
->operands
[i
].getTemp().type() == rc
.type()) {
551 if (instr
->operands
[i
].physReg() != best_pos
+ offset
) {
552 vars
.emplace(instr
->operands
[i
].size(), instr
->operands
[i
].tempId());
553 for (unsigned j
= 0; j
< instr
->operands
[i
].size(); j
++)
554 reg_file
[instr
->operands
[i
].physReg() + j
] = 0;
556 for (unsigned j
= 0; j
< instr
->operands
[i
].size(); j
++)
557 reg_file
[instr
->operands
[i
].physReg() + j
] = instr
->operands
[i
].tempId();
562 /* re-enable the killed operands */
563 for (unsigned j
= 0; !is_phi(instr
) && j
< instr
->operands
.size(); j
++) {
564 if (instr
->operands
[j
].isTemp() && instr
->operands
[j
].isFirstKill()) {
565 for (unsigned k
= 0; k
< instr
->operands
[j
].getTemp().size(); k
++)
566 reg_file
[instr
->operands
[j
].physReg() + k
] = instr
->operands
[j
].tempId();
571 std::vector
<std::pair
<Operand
, Definition
>> pc
;
572 if (!get_regs_for_copies(ctx
, reg_file
, pc
, vars
, lb
, ub
, instr
, best_pos
, best_pos
+ size
- 1)) {
573 reg_file
= std::move(register_file
);
574 /* remove killed operands from reg_file once again */
575 if (!is_phi(instr
)) {
576 for (const Operand
& op
: instr
->operands
) {
577 if (op
.isTemp() && op
.isFirstKill()) {
578 for (unsigned k
= 0; k
< op
.getTemp().size(); k
++)
579 reg_file
[op
.physReg() + k
] = 0;
583 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
584 Definition
& def
= instr
->definitions
[i
];
585 if (def
.isTemp() && def
.isFixed() && ctx
.defs_done
.test(i
)) {
586 for (unsigned k
= 0; k
< def
.getTemp().size(); k
++)
587 reg_file
[def
.physReg() + k
] = def
.tempId();
593 parallelcopies
.insert(parallelcopies
.end(), pc
.begin(), pc
.end());
595 /* we set the definition regs == 0. the actual caller is responsible for correct setting */
596 for (unsigned i
= 0; i
< size
; i
++)
597 reg_file
[best_pos
+ i
] = 0;
599 update_renames(ctx
, reg_file
, parallelcopies
, instr
);
601 /* remove killed operands from reg_file once again */
602 for (unsigned i
= 0; !is_phi(instr
) && i
< instr
->operands
.size(); i
++) {
603 if (!instr
->operands
[i
].isTemp() || !instr
->operands
[i
].isFixed())
605 assert(!instr
->operands
[i
].isUndefined());
606 if (instr
->operands
[i
].isFirstKill()) {
607 for (unsigned j
= 0; j
< instr
->operands
[i
].getTemp().size(); j
++)
608 reg_file
[instr
->operands
[i
].physReg() + j
] = 0;
611 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
612 Definition def
= instr
->definitions
[i
];
613 if (def
.isTemp() && def
.isFixed() && ctx
.defs_done
.test(i
)) {
614 for (unsigned k
= 0; k
< def
.getTemp().size(); k
++)
615 reg_file
[def
.physReg() + k
] = def
.tempId();
619 adjust_max_used_regs(ctx
, rc
, best_pos
);
620 return {PhysReg
{best_pos
}, true};
623 PhysReg
get_reg(ra_ctx
& ctx
,
624 std::array
<uint32_t, 512>& reg_file
,
626 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
627 aco_ptr
<Instruction
>& instr
)
629 uint32_t size
= rc
.size();
632 if (rc
.type() == RegType::vgpr
) {
634 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
637 ub
= ctx
.program
->max_reg_demand
.sgpr
;
644 std::pair
<PhysReg
, bool> res
= {{}, false};
645 /* try to find space without live-range splits */
646 if (rc
.type() == RegType::vgpr
&& (size
== 4 || size
== 8))
647 res
= get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, 4, rc
);
649 res
= get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, stride
, rc
);
653 /* try to find space with live-range splits */
654 res
= get_reg_impl(ctx
, reg_file
, parallelcopies
, lb
, ub
, size
, stride
, rc
, instr
);
659 unsigned regs_free
= 0;
660 for (unsigned i
= lb
; i
< ub
; i
++) {
665 /* We should only fail here because keeping under the limit would require
667 assert(regs_free
>= size
);
669 /* try using more registers */
670 uint16_t max_addressible_sgpr
= ctx
.program
->sgpr_limit
;
671 uint16_t max_addressible_vgpr
= ctx
.program
->vgpr_limit
;
672 if (rc
.type() == RegType::vgpr
&& ctx
.program
->max_reg_demand
.vgpr
< max_addressible_vgpr
) {
673 update_vgpr_sgpr_demand(ctx
.program
, RegisterDemand(ctx
.program
->max_reg_demand
.vgpr
+ 1, ctx
.program
->max_reg_demand
.sgpr
));
674 return get_reg(ctx
, reg_file
, rc
, parallelcopies
, instr
);
675 } else if (rc
.type() == RegType::sgpr
&& ctx
.program
->max_reg_demand
.sgpr
< max_addressible_sgpr
) {
676 update_vgpr_sgpr_demand(ctx
.program
, RegisterDemand(ctx
.program
->max_reg_demand
.vgpr
, ctx
.program
->max_reg_demand
.sgpr
+ 1));
677 return get_reg(ctx
, reg_file
, rc
, parallelcopies
, instr
);
680 //FIXME: if nothing helps, shift-rotate the registers to make space
682 unreachable("did not find a register");
686 std::pair
<PhysReg
, bool> get_reg_vec(ra_ctx
& ctx
,
687 std::array
<uint32_t, 512>& reg_file
,
690 uint32_t size
= rc
.size();
693 if (rc
.type() == RegType::vgpr
) {
695 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
698 ub
= ctx
.program
->max_reg_demand
.sgpr
;
704 return get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, stride
, rc
);
708 PhysReg
get_reg_create_vector(ra_ctx
& ctx
,
709 std::array
<uint32_t, 512>& reg_file
,
711 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
712 aco_ptr
<Instruction
>& instr
)
714 /* create_vector instructions have different costs w.r.t. register coalescing */
715 uint32_t size
= rc
.size();
718 if (rc
.type() == RegType::vgpr
) {
720 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
723 ub
= ctx
.program
->max_reg_demand
.sgpr
;
730 unsigned best_pos
= -1;
731 unsigned num_moves
= 0xFF;
732 bool best_war_hint
= true;
734 /* test for each operand which definition placement causes the least shuffle instructions */
735 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].size(), i
++) {
736 // TODO: think about, if we can alias live operands on the same register
737 if (!instr
->operands
[i
].isTemp() || !instr
->operands
[i
].isKill() || instr
->operands
[i
].getTemp().type() != rc
.type())
740 if (offset
> instr
->operands
[i
].physReg())
743 unsigned reg_lo
= instr
->operands
[i
].physReg() - offset
;
744 unsigned reg_hi
= reg_lo
+ size
- 1;
747 /* no need to check multiple times */
748 if (reg_lo
== best_pos
)
752 // TODO: this can be improved */
753 if (reg_lo
< lb
|| reg_hi
>= ub
|| reg_lo
% stride
!= 0)
755 if (reg_lo
> lb
&& reg_file
[reg_lo
] != 0 && reg_file
[reg_lo
] == reg_file
[reg_lo
- 1])
757 if (reg_hi
< ub
- 1 && reg_file
[reg_hi
] != 0 && reg_file
[reg_hi
] == reg_file
[reg_hi
+ 1])
760 /* count variables to be moved and check war_hint */
761 bool war_hint
= false;
762 for (unsigned j
= reg_lo
; j
<= reg_hi
; j
++) {
763 if (reg_file
[j
] != 0)
765 war_hint
|= ctx
.war_hint
[j
];
768 /* count operands in wrong positions */
769 for (unsigned j
= 0, offset
= 0; j
< instr
->operands
.size(); offset
+= instr
->operands
[j
].size(), j
++) {
771 !instr
->operands
[j
].isTemp() ||
772 instr
->operands
[j
].getTemp().type() != rc
.type())
774 if (instr
->operands
[j
].physReg() != reg_lo
+ offset
)
775 k
+= instr
->operands
[j
].size();
777 bool aligned
= rc
== RegClass::v4
&& reg_lo
% 4 == 0;
778 if (k
> num_moves
|| (!aligned
&& k
== num_moves
) || (war_hint
&& !best_war_hint
))
783 best_war_hint
= war_hint
;
786 if (num_moves
>= size
)
787 return get_reg(ctx
, reg_file
, rc
, parallelcopies
, instr
);
789 /* collect variables to be moved */
790 std::set
<std::pair
<unsigned, unsigned>> vars
;
791 for (unsigned i
= best_pos
; i
< best_pos
+ size
; i
++) {
792 if (reg_file
[i
] != 0)
793 vars
.emplace(ctx
.assignments
[reg_file
[i
]].second
.size(), reg_file
[i
]);
797 /* move killed operands which aren't yet at the correct position */
798 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].size(), i
++) {
799 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isFirstKill() && instr
->operands
[i
].getTemp().type() == rc
.type()) {
800 if (instr
->operands
[i
].physReg() != best_pos
+ offset
) {
801 vars
.emplace(instr
->operands
[i
].size(), instr
->operands
[i
].tempId());
803 for (unsigned j
= 0; j
< instr
->operands
[i
].size(); j
++)
804 reg_file
[instr
->operands
[i
].physReg() + j
] = instr
->operands
[i
].tempId();
809 ASSERTED
bool success
= false;
810 success
= get_regs_for_copies(ctx
, reg_file
, parallelcopies
, vars
, lb
, ub
, instr
, best_pos
, best_pos
+ size
- 1);
813 update_renames(ctx
, reg_file
, parallelcopies
, instr
);
814 adjust_max_used_regs(ctx
, rc
, best_pos
);
815 return PhysReg
{best_pos
};
818 bool get_reg_specified(ra_ctx
& ctx
,
819 std::array
<uint32_t, 512>& reg_file
,
821 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
822 aco_ptr
<Instruction
>& instr
,
825 uint32_t size
= rc
.size();
829 if (rc
.type() == RegType::vgpr
) {
831 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
837 if (reg
% stride
!= 0)
840 ub
= ctx
.program
->max_reg_demand
.sgpr
;
843 uint32_t reg_lo
= reg
.reg
;
844 uint32_t reg_hi
= reg
+ (size
- 1);
846 if (reg_lo
< lb
|| reg_hi
>= ub
|| reg_lo
> reg_hi
)
849 for (unsigned i
= reg_lo
; i
<= reg_hi
; i
++) {
850 if (reg_file
[i
] != 0)
853 adjust_max_used_regs(ctx
, rc
, reg_lo
);
857 void handle_pseudo(ra_ctx
& ctx
,
858 const std::array
<uint32_t, 512>& reg_file
,
861 if (instr
->format
!= Format::PSEUDO
)
864 /* all instructions which use handle_operands() need this information */
865 switch (instr
->opcode
) {
866 case aco_opcode::p_extract_vector
:
867 case aco_opcode::p_create_vector
:
868 case aco_opcode::p_split_vector
:
869 case aco_opcode::p_parallelcopy
:
870 case aco_opcode::p_wqm
:
876 /* if all definitions are vgpr, no need to care for SCC */
877 bool writes_sgpr
= false;
878 for (Definition
& def
: instr
->definitions
) {
879 if (def
.getTemp().type() == RegType::sgpr
) {
887 Pseudo_instruction
*pi
= (Pseudo_instruction
*)instr
;
888 if (reg_file
[scc
.reg
]) {
889 pi
->tmp_in_scc
= true;
891 int reg
= ctx
.max_used_sgpr
;
892 for (; reg
>= 0 && reg_file
[reg
]; reg
--)
895 reg
= ctx
.max_used_sgpr
+ 1;
896 for (; reg
< ctx
.program
->max_reg_demand
.sgpr
&& reg_file
[reg
]; reg
++)
898 assert(reg
< ctx
.program
->max_reg_demand
.sgpr
);
901 adjust_max_used_regs(ctx
, s1
, reg
);
902 pi
->scratch_sgpr
= PhysReg
{(unsigned)reg
};
904 pi
->tmp_in_scc
= false;
908 bool operand_can_use_reg(aco_ptr
<Instruction
>& instr
, unsigned idx
, PhysReg reg
)
910 switch (instr
->format
) {
914 (reg
!= m0
|| idx
== 1 || idx
== 3) && /* offset can be m0 */
915 (reg
!= vcc
|| (instr
->definitions
.empty() && idx
== 2)); /* sdata can be vcc */
917 // TODO: there are more instructions with restrictions on registers
922 } /* end namespace */
925 void register_allocation(Program
*program
, std::vector
<std::set
<Temp
>> live_out_per_block
)
929 std::vector
<std::unordered_map
<unsigned, Temp
>> renames(program
->blocks
.size());
934 std::set
<Instruction
*> uses
;
937 bool filled
[program
->blocks
.size()];
938 bool sealed
[program
->blocks
.size()];
939 memset(filled
, 0, sizeof filled
);
940 memset(sealed
, 0, sizeof sealed
);
941 std::vector
<std::vector
<Instruction
*>> incomplete_phis(program
->blocks
.size());
942 std::map
<unsigned, phi_info
> phi_map
;
943 std::map
<unsigned, unsigned> affinities
;
944 std::function
<Temp(Temp
,unsigned)> read_variable
;
945 std::function
<Temp(Temp
,Block
*)> handle_live_in
;
946 std::function
<Temp(std::map
<unsigned, phi_info
>::iterator
)> try_remove_trivial_phi
;
948 read_variable
= [&](Temp val
, unsigned block_idx
) -> Temp
{
949 std::unordered_map
<unsigned, Temp
>::iterator it
= renames
[block_idx
].find(val
.id());
950 assert(it
!= renames
[block_idx
].end());
954 handle_live_in
= [&](Temp val
, Block
*block
) -> Temp
{
955 std::vector
<unsigned>& preds
= val
.is_linear() ? block
->linear_preds
: block
->logical_preds
;
956 if (preds
.size() == 0 && block
->index
!= 0) {
957 renames
[block
->index
][val
.id()] = val
;
960 assert(preds
.size() > 0);
963 if (!sealed
[block
->index
]) {
964 /* consider rename from already processed predecessor */
965 Temp tmp
= read_variable(val
, preds
[0]);
967 /* if the block is not sealed yet, we create an incomplete phi (which might later get removed again) */
968 new_val
= Temp
{program
->allocateId(), val
.regClass()};
969 aco_opcode opcode
= val
.is_linear() ? aco_opcode::p_linear_phi
: aco_opcode::p_phi
;
970 aco_ptr
<Instruction
> phi
{create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, preds
.size(), 1)};
971 phi
->definitions
[0] = Definition(new_val
);
972 for (unsigned i
= 0; i
< preds
.size(); i
++)
973 phi
->operands
[i
] = Operand(val
);
974 if (tmp
.regClass() == new_val
.regClass())
975 affinities
[new_val
.id()] = tmp
.id();
977 phi_map
.emplace(new_val
.id(), phi_info
{phi
.get(), block
->index
});
978 incomplete_phis
[block
->index
].emplace_back(phi
.get());
979 block
->instructions
.insert(block
->instructions
.begin(), std::move(phi
));
981 } else if (preds
.size() == 1) {
982 /* if the block has only one predecessor, just look there for the name */
983 new_val
= read_variable(val
, preds
[0]);
985 /* there are multiple predecessors and the block is sealed */
986 Temp ops
[preds
.size()];
988 /* we start assuming that the name is the same from all predecessors */
989 renames
[block
->index
][val
.id()] = val
;
990 bool needs_phi
= false;
992 /* get the rename from each predecessor and check if they are the same */
993 for (unsigned i
= 0; i
< preds
.size(); i
++) {
994 ops
[i
] = read_variable(val
, preds
[i
]);
998 needs_phi
|= !(new_val
== ops
[i
]);
1002 /* the variable has been renamed differently in the predecessors: we need to insert a phi */
1003 aco_opcode opcode
= val
.is_linear() ? aco_opcode::p_linear_phi
: aco_opcode::p_phi
;
1004 aco_ptr
<Instruction
> phi
{create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, preds
.size(), 1)};
1005 new_val
= Temp
{program
->allocateId(), val
.regClass()};
1006 phi
->definitions
[0] = Definition(new_val
);
1007 for (unsigned i
= 0; i
< preds
.size(); i
++) {
1008 phi
->operands
[i
] = Operand(ops
[i
]);
1009 phi
->operands
[i
].setFixed(ctx
.assignments
[ops
[i
].id()].first
);
1010 if (ops
[i
].regClass() == new_val
.regClass())
1011 affinities
[new_val
.id()] = ops
[i
].id();
1013 phi_map
.emplace(new_val
.id(), phi_info
{phi
.get(), block
->index
});
1014 block
->instructions
.insert(block
->instructions
.begin(), std::move(phi
));
1018 renames
[block
->index
][val
.id()] = new_val
;
1019 renames
[block
->index
][new_val
.id()] = new_val
;
1020 ctx
.orig_names
[new_val
.id()] = val
;
1024 try_remove_trivial_phi
= [&] (std::map
<unsigned, phi_info
>::iterator info
) -> Temp
{
1025 assert(info
->second
.block_idx
!= 0);
1026 Instruction
* phi
= info
->second
.phi
;
1029 Definition def
= phi
->definitions
[0];
1030 /* a phi node is trivial if all operands are the same as the definition of the phi */
1031 for (const Operand
& op
: phi
->operands
) {
1032 const Temp t
= op
.getTemp();
1033 if (t
== same
|| t
== def
.getTemp())
1035 if (!(same
== Temp()) || !(op
.physReg() == def
.physReg())) {
1036 /* phi is not trivial */
1037 return def
.getTemp();
1041 assert(!(same
== Temp() || same
== def
.getTemp()));
1043 /* reroute all uses to same and remove phi */
1044 std::vector
<std::map
<unsigned, phi_info
>::iterator
> phi_users
;
1045 std::map
<unsigned, phi_info
>::iterator same_phi_info
= phi_map
.find(same
.id());
1046 for (Instruction
* instr
: info
->second
.uses
) {
1047 assert(phi
!= instr
);
1048 /* recursively try to remove trivial phis */
1049 if (is_phi(instr
)) {
1050 /* ignore if the phi was already flagged trivial */
1051 if (instr
->definitions
.empty())
1054 std::map
<unsigned, phi_info
>::iterator it
= phi_map
.find(instr
->definitions
[0].tempId());
1055 if (it
!= phi_map
.end() && it
!= info
)
1056 phi_users
.emplace_back(it
);
1058 for (Operand
& op
: instr
->operands
) {
1059 if (op
.isTemp() && op
.tempId() == def
.tempId()) {
1061 if (same_phi_info
!= phi_map
.end())
1062 same_phi_info
->second
.uses
.emplace(instr
);
1067 auto it
= ctx
.orig_names
.find(same
.id());
1068 unsigned orig_var
= it
!= ctx
.orig_names
.end() ? it
->second
.id() : same
.id();
1069 for (unsigned i
= 0; i
< program
->blocks
.size(); i
++) {
1070 auto it
= renames
[i
].find(orig_var
);
1071 if (it
!= renames
[i
].end() && it
->second
== def
.getTemp())
1072 renames
[i
][orig_var
] = same
;
1075 unsigned block_idx
= info
->second
.block_idx
;
1076 phi
->definitions
.clear(); /* this indicates that the phi can be removed */
1077 phi_map
.erase(info
);
1078 for (auto it
: phi_users
) {
1079 if (sealed
[it
->second
.block_idx
])
1080 try_remove_trivial_phi(it
);
1083 /* due to the removal of other phis, the name might have changed once again! */
1084 return renames
[block_idx
][orig_var
];
1087 std::map
<unsigned, Instruction
*> vectors
;
1088 std::vector
<std::vector
<Temp
>> phi_ressources
;
1089 std::map
<unsigned, unsigned> temp_to_phi_ressources
;
1091 for (std::vector
<Block
>::reverse_iterator it
= program
->blocks
.rbegin(); it
!= program
->blocks
.rend(); it
++) {
1094 /* first, compute the death points of all live vars within the block */
1095 std::set
<Temp
>& live
= live_out_per_block
[block
.index
];
1097 std::vector
<aco_ptr
<Instruction
>>::reverse_iterator rit
;
1098 for (rit
= block
.instructions
.rbegin(); rit
!= block
.instructions
.rend(); ++rit
) {
1099 aco_ptr
<Instruction
>& instr
= *rit
;
1100 if (!is_phi(instr
)) {
1101 for (const Operand
& op
: instr
->operands
) {
1103 live
.emplace(op
.getTemp());
1105 if (instr
->opcode
== aco_opcode::p_create_vector
) {
1106 for (const Operand
& op
: instr
->operands
) {
1107 if (op
.isTemp() && op
.getTemp().type() == instr
->definitions
[0].getTemp().type())
1108 vectors
[op
.tempId()] = instr
.get();
1111 } else if (!instr
->definitions
[0].isKill() && !instr
->definitions
[0].isFixed()) {
1112 /* collect information about affinity-related temporaries */
1113 std::vector
<Temp
> affinity_related
;
1114 /* affinity_related[0] is the last seen affinity-related temp */
1115 affinity_related
.emplace_back(instr
->definitions
[0].getTemp());
1116 affinity_related
.emplace_back(instr
->definitions
[0].getTemp());
1117 for (const Operand
& op
: instr
->operands
) {
1118 if (op
.isTemp() && op
.regClass() == instr
->definitions
[0].regClass()) {
1119 affinity_related
.emplace_back(op
.getTemp());
1120 temp_to_phi_ressources
[op
.tempId()] = phi_ressources
.size();
1123 phi_ressources
.emplace_back(std::move(affinity_related
));
1126 /* erase from live */
1127 for (const Definition
& def
: instr
->definitions
) {
1129 live
.erase(def
.getTemp());
1130 std::map
<unsigned, unsigned>::iterator it
= temp_to_phi_ressources
.find(def
.tempId());
1131 if (it
!= temp_to_phi_ressources
.end() && def
.regClass() == phi_ressources
[it
->second
][0].regClass())
1132 phi_ressources
[it
->second
][0] = def
.getTemp();
1137 /* create affinities */
1138 for (std::vector
<Temp
>& vec
: phi_ressources
) {
1139 assert(vec
.size() > 1);
1140 for (unsigned i
= 1; i
< vec
.size(); i
++)
1141 if (vec
[i
].id() != vec
[0].id())
1142 affinities
[vec
[i
].id()] = vec
[0].id();
1145 /* state of register file after phis */
1146 std::vector
<std::bitset
<128>> sgpr_live_in(program
->blocks
.size());
1148 for (Block
& block
: program
->blocks
) {
1149 std::set
<Temp
>& live
= live_out_per_block
[block
.index
];
1150 /* initialize register file */
1151 assert(block
.index
!= 0 || live
.empty());
1152 std::array
<uint32_t, 512> register_file
= {0};
1153 ctx
.war_hint
.reset();
1155 for (Temp t
: live
) {
1156 Temp renamed
= handle_live_in(t
, &block
);
1157 if (ctx
.assignments
.find(renamed
.id()) != ctx
.assignments
.end()) {
1158 for (unsigned i
= 0; i
< t
.size(); i
++)
1159 register_file
[ctx
.assignments
[renamed
.id()].first
+ i
] = renamed
.id();
1163 std::vector
<aco_ptr
<Instruction
>> instructions
;
1164 std::vector
<aco_ptr
<Instruction
>>::iterator it
;
1166 /* this is a slight adjustment from the paper as we already have phi nodes:
1167 * We consider them incomplete phis and only handle the definition. */
1169 /* handle fixed phi definitions */
1170 for (it
= block
.instructions
.begin(); it
!= block
.instructions
.end(); ++it
) {
1171 aco_ptr
<Instruction
>& phi
= *it
;
1174 Definition
& definition
= phi
->definitions
[0];
1175 if (!definition
.isFixed())
1178 /* check if a dead exec mask phi is needed */
1179 if (definition
.isKill()) {
1180 for (Operand
& op
: phi
->operands
) {
1181 assert(op
.isTemp());
1182 if (ctx
.assignments
.find(op
.tempId()) == ctx
.assignments
.end() ||
1183 ctx
.assignments
[op
.tempId()].first
!= exec
) {
1184 definition
.setKill(false);
1190 if (definition
.isKill())
1193 assert(definition
.physReg() == exec
);
1194 for (unsigned i
= 0; i
< definition
.size(); i
++) {
1195 assert(register_file
[definition
.physReg() + i
] == 0);
1196 register_file
[definition
.physReg() + i
] = definition
.tempId();
1198 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1201 /* look up the affinities */
1202 for (it
= block
.instructions
.begin(); it
!= block
.instructions
.end(); ++it
) {
1203 aco_ptr
<Instruction
>& phi
= *it
;
1206 Definition
& definition
= phi
->definitions
[0];
1207 if (definition
.isKill() || definition
.isFixed())
1210 if (affinities
.find(definition
.tempId()) != affinities
.end() &&
1211 ctx
.assignments
.find(affinities
[definition
.tempId()]) != ctx
.assignments
.end()) {
1212 assert(ctx
.assignments
[affinities
[definition
.tempId()]].second
== definition
.regClass());
1213 PhysReg reg
= ctx
.assignments
[affinities
[definition
.tempId()]].first
;
1214 bool try_use_special_reg
= reg
== scc
|| reg
== exec
;
1215 if (try_use_special_reg
) {
1216 for (const Operand
& op
: phi
->operands
) {
1218 ctx
.assignments
.find(op
.tempId()) == ctx
.assignments
.end() ||
1219 !(ctx
.assignments
[op
.tempId()].first
== reg
)) {
1220 try_use_special_reg
= false;
1224 if (!try_use_special_reg
)
1227 bool reg_free
= true;
1228 for (unsigned i
= reg
.reg
; reg_free
&& i
< reg
+ definition
.size(); i
++) {
1229 if (register_file
[i
] != 0)
1232 /* only assign if register is still free */
1234 definition
.setFixed(reg
);
1235 for (unsigned i
= 0; i
< definition
.size(); i
++)
1236 register_file
[definition
.physReg() + i
] = definition
.tempId();
1237 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1242 /* find registers for phis without affinity or where the register was blocked */
1243 for (it
= block
.instructions
.begin();it
!= block
.instructions
.end(); ++it
) {
1244 aco_ptr
<Instruction
>& phi
= *it
;
1248 Definition
& definition
= phi
->definitions
[0];
1249 if (definition
.isKill())
1252 renames
[block
.index
][definition
.tempId()] = definition
.getTemp();
1254 if (!definition
.isFixed()) {
1255 std::vector
<std::pair
<Operand
, Definition
>> parallelcopy
;
1256 /* try to find a register that is used by at least one operand */
1257 for (const Operand
& op
: phi
->operands
) {
1259 ctx
.assignments
.find(op
.tempId()) == ctx
.assignments
.end())
1261 PhysReg reg
= ctx
.assignments
[op
.tempId()].first
;
1262 /* we tried this already on the previous loop */
1263 if (reg
== scc
|| reg
== exec
)
1265 if (get_reg_specified(ctx
, register_file
, definition
.regClass(), parallelcopy
, phi
, reg
)) {
1266 definition
.setFixed(reg
);
1270 if (!definition
.isFixed())
1271 definition
.setFixed(get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, phi
));
1273 /* process parallelcopy */
1274 for (std::pair
<Operand
, Definition
> pc
: parallelcopy
) {
1275 /* see if it's a copy from a different phi */
1276 //TODO: prefer moving some previous phis over live-ins
1277 //TODO: somehow prevent phis fixed before the RA from being updated (shouldn't be a problem in practice since they can only be fixed to exec)
1278 Instruction
*prev_phi
= NULL
;
1279 std::vector
<aco_ptr
<Instruction
>>::iterator phi_it
;
1280 for (phi_it
= instructions
.begin(); phi_it
!= instructions
.end(); ++phi_it
) {
1281 if ((*phi_it
)->definitions
[0].tempId() == pc
.first
.tempId())
1282 prev_phi
= phi_it
->get();
1285 while (!prev_phi
&& is_phi(*++phi_it
)) {
1286 if ((*phi_it
)->definitions
[0].tempId() == pc
.first
.tempId())
1287 prev_phi
= phi_it
->get();
1290 /* if so, just update that phi's register */
1291 prev_phi
->definitions
[0].setFixed(pc
.second
.physReg());
1292 ctx
.assignments
[prev_phi
->definitions
[0].tempId()] = {pc
.second
.physReg(), pc
.second
.regClass()};
1293 for (unsigned reg
= pc
.second
.physReg(); reg
< pc
.second
.physReg() + pc
.second
.size(); reg
++)
1294 register_file
[reg
] = prev_phi
->definitions
[0].tempId();
1299 std::map
<unsigned, Temp
>::iterator orig_it
= ctx
.orig_names
.find(pc
.first
.tempId());
1300 Temp orig
= pc
.first
.getTemp();
1301 if (orig_it
!= ctx
.orig_names
.end())
1302 orig
= orig_it
->second
;
1304 ctx
.orig_names
[pc
.second
.tempId()] = orig
;
1305 renames
[block
.index
][orig
.id()] = pc
.second
.getTemp();
1306 renames
[block
.index
][pc
.second
.tempId()] = pc
.second
.getTemp();
1308 /* otherwise, this is a live-in and we need to create a new phi
1309 * to move it in this block's predecessors */
1310 aco_opcode opcode
= pc
.first
.getTemp().is_linear() ? aco_opcode::p_linear_phi
: aco_opcode::p_phi
;
1311 std::vector
<unsigned>& preds
= pc
.first
.getTemp().is_linear() ? block
.linear_preds
: block
.logical_preds
;
1312 aco_ptr
<Instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, preds
.size(), 1)};
1313 new_phi
->definitions
[0] = pc
.second
;
1314 for (unsigned i
= 0; i
< preds
.size(); i
++)
1315 new_phi
->operands
[i
] = Operand(pc
.first
);
1316 instructions
.emplace_back(std::move(new_phi
));
1319 for (unsigned i
= 0; i
< definition
.size(); i
++)
1320 register_file
[definition
.physReg() + i
] = definition
.tempId();
1321 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1323 live
.emplace(definition
.getTemp());
1325 /* update phi affinities */
1326 for (const Operand
& op
: phi
->operands
) {
1327 if (op
.isTemp() && op
.regClass() == phi
->definitions
[0].regClass())
1328 affinities
[op
.tempId()] = definition
.tempId();
1331 instructions
.emplace_back(std::move(*it
));
1334 /* fill in sgpr_live_in */
1335 for (unsigned i
= 0; i
< ctx
.max_used_sgpr
; i
++)
1336 sgpr_live_in
[block
.index
][i
] = register_file
[i
];
1337 sgpr_live_in
[block
.index
][127] = register_file
[scc
.reg
];
1339 /* Handle all other instructions of the block */
1340 for (; it
!= block
.instructions
.end(); ++it
) {
1341 aco_ptr
<Instruction
>& instr
= *it
;
1343 /* parallelcopies from p_phi are inserted here which means
1344 * live ranges of killed operands end here as well */
1345 if (instr
->opcode
== aco_opcode::p_logical_end
) {
1346 /* no need to process this instruction any further */
1347 if (block
.logical_succs
.size() != 1) {
1348 instructions
.emplace_back(std::move(instr
));
1352 Block
& succ
= program
->blocks
[block
.logical_succs
[0]];
1354 for (; idx
< succ
.logical_preds
.size(); idx
++) {
1355 if (succ
.logical_preds
[idx
] == block
.index
)
1358 for (aco_ptr
<Instruction
>& phi
: succ
.instructions
) {
1359 if (phi
->opcode
== aco_opcode::p_phi
) {
1360 if (phi
->operands
[idx
].isTemp() &&
1361 phi
->operands
[idx
].getTemp().type() == RegType::sgpr
&&
1362 phi
->operands
[idx
].isFirstKill()) {
1363 Temp phi_op
= read_variable(phi
->operands
[idx
].getTemp(), block
.index
);
1364 PhysReg reg
= ctx
.assignments
[phi_op
.id()].first
;
1365 assert(register_file
[reg
] == phi_op
.id());
1366 register_file
[reg
] = 0;
1368 } else if (phi
->opcode
!= aco_opcode::p_linear_phi
) {
1372 instructions
.emplace_back(std::move(instr
));
1376 std::vector
<std::pair
<Operand
, Definition
>> parallelcopy
;
1378 assert(!is_phi(instr
));
1380 /* handle operands */
1381 for (unsigned i
= 0; i
< instr
->operands
.size(); ++i
) {
1382 auto& operand
= instr
->operands
[i
];
1383 if (!operand
.isTemp())
1386 /* rename operands */
1387 operand
.setTemp(read_variable(operand
.getTemp(), block
.index
));
1389 /* check if the operand is fixed */
1390 if (operand
.isFixed()) {
1392 if (operand
.physReg() == ctx
.assignments
[operand
.tempId()].first
) {
1393 /* we are fine: the operand is already assigned the correct reg */
1396 /* check if target reg is blocked, and move away the blocking var */
1397 if (register_file
[operand
.physReg().reg
]) {
1398 uint32_t blocking_id
= register_file
[operand
.physReg().reg
];
1399 RegClass rc
= ctx
.assignments
[blocking_id
].second
;
1400 Operand pc_op
= Operand(Temp
{blocking_id
, rc
});
1401 pc_op
.setFixed(operand
.physReg());
1402 Definition pc_def
= Definition(Temp
{program
->allocateId(), pc_op
.regClass()});
1404 PhysReg reg
= get_reg(ctx
, register_file
, pc_op
.regClass(), parallelcopy
, instr
);
1405 pc_def
.setFixed(reg
);
1406 ctx
.assignments
[pc_def
.tempId()] = {reg
, pc_def
.regClass()};
1407 for (unsigned i
= 0; i
< operand
.size(); i
++) {
1408 register_file
[pc_op
.physReg() + i
] = 0;
1409 register_file
[pc_def
.physReg() + i
] = pc_def
.tempId();
1411 parallelcopy
.emplace_back(pc_op
, pc_def
);
1413 /* handle renames of previous operands */
1414 for (unsigned j
= 0; j
< i
; j
++) {
1415 Operand
& op
= instr
->operands
[j
];
1416 if (op
.isTemp() && op
.tempId() == blocking_id
) {
1417 op
= Operand(pc_def
.getTemp());
1422 /* move operand to fixed reg and create parallelcopy pair */
1423 Operand pc_op
= operand
;
1424 Temp tmp
= Temp
{program
->allocateId(), operand
.regClass()};
1425 Definition pc_def
= Definition(tmp
);
1426 pc_def
.setFixed(operand
.physReg());
1427 pc_op
.setFixed(ctx
.assignments
[operand
.tempId()].first
);
1428 operand
.setTemp(tmp
);
1429 ctx
.assignments
[tmp
.id()] = {pc_def
.physReg(), pc_def
.regClass()};
1430 operand
.setFixed(pc_def
.physReg());
1431 for (unsigned i
= 0; i
< operand
.size(); i
++) {
1432 register_file
[pc_op
.physReg() + i
] = 0;
1433 register_file
[pc_def
.physReg() + i
] = tmp
.id();
1435 parallelcopy
.emplace_back(pc_op
, pc_def
);
1438 assert(ctx
.assignments
.find(operand
.tempId()) != ctx
.assignments
.end());
1439 PhysReg reg
= ctx
.assignments
[operand
.tempId()].first
;
1441 if (operand_can_use_reg(instr
, i
, reg
)) {
1442 operand
.setFixed(ctx
.assignments
[operand
.tempId()].first
);
1444 Operand pc_op
= operand
;
1445 pc_op
.setFixed(reg
);
1446 PhysReg new_reg
= get_reg(ctx
, register_file
, operand
.regClass(), parallelcopy
, instr
);
1447 Definition pc_def
= Definition(program
->allocateId(), new_reg
, pc_op
.regClass());
1448 ctx
.assignments
[pc_def
.tempId()] = {reg
, pc_def
.regClass()};
1449 for (unsigned i
= 0; i
< operand
.size(); i
++) {
1450 register_file
[pc_op
.physReg() + i
] = 0;
1451 register_file
[pc_def
.physReg() + i
] = pc_def
.tempId();
1453 parallelcopy
.emplace_back(pc_op
, pc_def
);
1454 operand
.setFixed(new_reg
);
1457 if (instr
->format
== Format::EXP
||
1458 (instr
->isVMEM() && i
== 3 && program
->chip_class
== GFX6
) ||
1459 (instr
->format
== Format::DS
&& static_cast<DS_instruction
*>(instr
.get())->gds
)) {
1460 for (unsigned j
= 0; j
< operand
.size(); j
++)
1461 ctx
.war_hint
.set(operand
.physReg().reg
+ j
);
1464 std::map
<unsigned, phi_info
>::iterator phi
= phi_map
.find(operand
.getTemp().id());
1465 if (phi
!= phi_map
.end())
1466 phi
->second
.uses
.emplace(instr
.get());
1469 /* remove dead vars from register file */
1470 for (const Operand
& op
: instr
->operands
) {
1471 if (op
.isTemp() && op
.isFirstKill())
1472 for (unsigned j
= 0; j
< op
.size(); j
++)
1473 register_file
[op
.physReg() + j
] = 0;
1476 /* try to optimize v_mad_f32 -> v_mac_f32 */
1477 if (instr
->opcode
== aco_opcode::v_mad_f32
&&
1478 instr
->operands
[2].isTemp() &&
1479 instr
->operands
[2].isKill() &&
1480 instr
->operands
[2].getTemp().type() == RegType::vgpr
&&
1481 instr
->operands
[1].isTemp() &&
1482 instr
->operands
[1].getTemp().type() == RegType::vgpr
) { /* TODO: swap src0 and src1 in this case */
1483 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(instr
.get());
1484 bool can_use_mac
= !(vop3
->abs
[0] || vop3
->abs
[1] || vop3
->abs
[2] ||
1485 vop3
->opsel
[0] || vop3
->opsel
[1] || vop3
->opsel
[2] ||
1486 vop3
->neg
[0] || vop3
->neg
[1] || vop3
->neg
[2] ||
1487 vop3
->clamp
|| vop3
->omod
);
1489 instr
->format
= Format::VOP2
;
1490 instr
->opcode
= aco_opcode::v_mac_f32
;
1494 /* handle definitions which must have the same register as an operand */
1495 if (instr
->opcode
== aco_opcode::v_interp_p2_f32
||
1496 instr
->opcode
== aco_opcode::v_mac_f32
||
1497 instr
->opcode
== aco_opcode::v_writelane_b32
) {
1498 instr
->definitions
[0].setFixed(instr
->operands
[2].physReg());
1499 } else if (instr
->opcode
== aco_opcode::s_addk_i32
||
1500 instr
->opcode
== aco_opcode::s_mulk_i32
) {
1501 instr
->definitions
[0].setFixed(instr
->operands
[0].physReg());
1502 } else if ((instr
->format
== Format::MUBUF
||
1503 instr
->format
== Format::MIMG
) &&
1504 instr
->definitions
.size() == 1 &&
1505 instr
->operands
.size() == 4) {
1506 instr
->definitions
[0].setFixed(instr
->operands
[3].physReg());
1509 ctx
.defs_done
.reset();
1511 /* handle fixed definitions first */
1512 for (unsigned i
= 0; i
< instr
->definitions
.size(); ++i
) {
1513 auto& definition
= instr
->definitions
[i
];
1514 if (!definition
.isFixed())
1517 adjust_max_used_regs(ctx
, definition
.regClass(), definition
.physReg());
1518 /* check if the target register is blocked */
1519 if (register_file
[definition
.physReg().reg
] != 0) {
1520 /* create parallelcopy pair to move blocking var */
1521 Temp tmp
= {register_file
[definition
.physReg()], ctx
.assignments
[register_file
[definition
.physReg()]].second
};
1522 Operand pc_op
= Operand(tmp
);
1523 pc_op
.setFixed(ctx
.assignments
[register_file
[definition
.physReg().reg
]].first
);
1524 RegClass rc
= pc_op
.regClass();
1525 tmp
= Temp
{program
->allocateId(), rc
};
1526 Definition pc_def
= Definition(tmp
);
1528 /* re-enable the killed operands, so that we don't move the blocking var there */
1529 for (const Operand
& op
: instr
->operands
) {
1530 if (op
.isTemp() && op
.isFirstKill())
1531 for (unsigned j
= 0; j
< op
.size(); j
++)
1532 register_file
[op
.physReg() + j
] = 0xFFFF;
1535 /* find a new register for the blocking variable */
1536 PhysReg reg
= get_reg(ctx
, register_file
, rc
, parallelcopy
, instr
);
1537 /* once again, disable killed operands */
1538 for (const Operand
& op
: instr
->operands
) {
1539 if (op
.isTemp() && op
.isFirstKill())
1540 for (unsigned j
= 0; j
< op
.size(); j
++)
1541 register_file
[op
.physReg() + j
] = 0;
1543 for (unsigned k
= 0; k
< i
; k
++) {
1544 if (instr
->definitions
[k
].isTemp() && ctx
.defs_done
.test(k
) && !instr
->definitions
[k
].isKill())
1545 for (unsigned j
= 0; j
< instr
->definitions
[k
].size(); j
++)
1546 register_file
[instr
->definitions
[k
].physReg() + j
] = instr
->definitions
[k
].tempId();
1548 pc_def
.setFixed(reg
);
1550 /* finish assignment of parallelcopy */
1551 ctx
.assignments
[pc_def
.tempId()] = {reg
, pc_def
.regClass()};
1552 parallelcopy
.emplace_back(pc_op
, pc_def
);
1554 /* add changes to reg_file */
1555 for (unsigned i
= 0; i
< pc_op
.size(); i
++) {
1556 register_file
[pc_op
.physReg() + i
] = 0x0;
1557 register_file
[pc_def
.physReg() + i
] = pc_def
.tempId();
1560 ctx
.defs_done
.set(i
);
1562 if (!definition
.isTemp())
1565 /* set live if it has a kill point */
1566 if (!definition
.isKill())
1567 live
.emplace(definition
.getTemp());
1569 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1570 renames
[block
.index
][definition
.tempId()] = definition
.getTemp();
1571 for (unsigned j
= 0; j
< definition
.size(); j
++)
1572 register_file
[definition
.physReg() + j
] = definition
.tempId();
1575 /* handle all other definitions */
1576 for (unsigned i
= 0; i
< instr
->definitions
.size(); ++i
) {
1577 auto& definition
= instr
->definitions
[i
];
1579 if (definition
.isFixed() || !definition
.isTemp())
1583 if (definition
.hasHint() && register_file
[definition
.physReg().reg
] == 0)
1584 definition
.setFixed(definition
.physReg());
1585 else if (instr
->opcode
== aco_opcode::p_split_vector
) {
1586 PhysReg reg
= PhysReg
{instr
->operands
[0].physReg() + i
* definition
.size()};
1587 if (!get_reg_specified(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
, reg
))
1588 reg
= get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
);
1589 definition
.setFixed(reg
);
1590 } else if (instr
->opcode
== aco_opcode::p_wqm
) {
1592 if (instr
->operands
[0].isKill() && instr
->operands
[0].getTemp().type() == definition
.getTemp().type()) {
1593 reg
= instr
->operands
[0].physReg();
1594 assert(register_file
[reg
.reg
] == 0);
1596 reg
= get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
);
1598 definition
.setFixed(reg
);
1599 } else if (instr
->opcode
== aco_opcode::p_extract_vector
) {
1601 if (instr
->operands
[0].isKill() &&
1602 instr
->operands
[0].getTemp().type() == definition
.getTemp().type()) {
1603 reg
= instr
->operands
[0].physReg();
1604 reg
.reg
+= definition
.size() * instr
->operands
[1].constantValue();
1605 assert(register_file
[reg
.reg
] == 0);
1607 reg
= get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
);
1609 definition
.setFixed(reg
);
1610 } else if (instr
->opcode
== aco_opcode::p_create_vector
) {
1611 PhysReg reg
= get_reg_create_vector(ctx
, register_file
, definition
.regClass(),
1612 parallelcopy
, instr
);
1613 definition
.setFixed(reg
);
1614 } else if (affinities
.find(definition
.tempId()) != affinities
.end() &&
1615 ctx
.assignments
.find(affinities
[definition
.tempId()]) != ctx
.assignments
.end()) {
1616 PhysReg reg
= ctx
.assignments
[affinities
[definition
.tempId()]].first
;
1617 if (get_reg_specified(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
, reg
))
1618 definition
.setFixed(reg
);
1620 definition
.setFixed(get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
));
1622 } else if (vectors
.find(definition
.tempId()) != vectors
.end()) {
1623 Instruction
* vec
= vectors
[definition
.tempId()];
1624 unsigned offset
= 0;
1625 for (const Operand
& op
: vec
->operands
) {
1626 if (op
.isTemp() && op
.tempId() == definition
.tempId())
1629 offset
+= op
.size();
1632 for (const Operand
& op
: vec
->operands
) {
1634 op
.tempId() != definition
.tempId() &&
1635 op
.getTemp().type() == definition
.getTemp().type() &&
1636 ctx
.assignments
.find(op
.tempId()) != ctx
.assignments
.end()) {
1637 PhysReg reg
= ctx
.assignments
[op
.tempId()].first
;
1638 reg
.reg
= reg
- k
+ offset
;
1639 if (get_reg_specified(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
, reg
)) {
1640 definition
.setFixed(reg
);
1646 if (!definition
.isFixed()) {
1647 std::pair
<PhysReg
, bool> res
= get_reg_vec(ctx
, register_file
, vec
->definitions
[0].regClass());
1648 PhysReg reg
= res
.first
;
1652 reg
= get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
);
1654 definition
.setFixed(reg
);
1657 definition
.setFixed(get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
));
1659 assert(definition
.isFixed() && ((definition
.getTemp().type() == RegType::vgpr
&& definition
.physReg() >= 256) ||
1660 (definition
.getTemp().type() != RegType::vgpr
&& definition
.physReg() < 256)));
1661 ctx
.defs_done
.set(i
);
1663 /* set live if it has a kill point */
1664 if (!definition
.isKill())
1665 live
.emplace(definition
.getTemp());
1667 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1668 renames
[block
.index
][definition
.tempId()] = definition
.getTemp();
1669 for (unsigned j
= 0; j
< definition
.size(); j
++)
1670 register_file
[definition
.physReg() + j
] = definition
.tempId();
1673 handle_pseudo(ctx
, register_file
, instr
.get());
1675 /* kill definitions */
1676 for (const Definition
& def
: instr
->definitions
) {
1677 if (def
.isTemp() && def
.isKill()) {
1678 for (unsigned j
= 0; j
< def
.size(); j
++) {
1679 register_file
[def
.physReg() + j
] = 0;
1684 /* emit parallelcopy */
1685 if (!parallelcopy
.empty()) {
1686 aco_ptr
<Pseudo_instruction
> pc
;
1687 pc
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_parallelcopy
, Format::PSEUDO
, parallelcopy
.size(), parallelcopy
.size()));
1688 bool temp_in_scc
= register_file
[scc
.reg
];
1689 bool sgpr_operands_alias_defs
= false;
1690 uint64_t sgpr_operands
[4] = {0, 0, 0, 0};
1691 for (unsigned i
= 0; i
< parallelcopy
.size(); i
++) {
1692 if (temp_in_scc
&& parallelcopy
[i
].first
.isTemp() && parallelcopy
[i
].first
.getTemp().type() == RegType::sgpr
) {
1693 if (!sgpr_operands_alias_defs
) {
1694 unsigned reg
= parallelcopy
[i
].first
.physReg().reg
;
1695 unsigned size
= parallelcopy
[i
].first
.getTemp().size();
1696 sgpr_operands
[reg
/ 64u] |= ((1u << size
) - 1) << (reg
% 64u);
1698 reg
= parallelcopy
[i
].second
.physReg().reg
;
1699 size
= parallelcopy
[i
].second
.getTemp().size();
1700 if (sgpr_operands
[reg
/ 64u] & ((1u << size
) - 1) << (reg
% 64u))
1701 sgpr_operands_alias_defs
= true;
1705 pc
->operands
[i
] = parallelcopy
[i
].first
;
1706 pc
->definitions
[i
] = parallelcopy
[i
].second
;
1708 /* it might happen that the operand is already renamed. we have to restore the original name. */
1709 std::map
<unsigned, Temp
>::iterator it
= ctx
.orig_names
.find(pc
->operands
[i
].tempId());
1710 if (it
!= ctx
.orig_names
.end())
1711 pc
->operands
[i
].setTemp(it
->second
);
1712 unsigned orig_id
= pc
->operands
[i
].tempId();
1713 ctx
.orig_names
[pc
->definitions
[i
].tempId()] = pc
->operands
[i
].getTemp();
1715 pc
->operands
[i
].setTemp(read_variable(pc
->operands
[i
].getTemp(), block
.index
));
1716 renames
[block
.index
][orig_id
] = pc
->definitions
[i
].getTemp();
1717 renames
[block
.index
][pc
->definitions
[i
].tempId()] = pc
->definitions
[i
].getTemp();
1718 std::map
<unsigned, phi_info
>::iterator phi
= phi_map
.find(pc
->operands
[i
].tempId());
1719 if (phi
!= phi_map
.end())
1720 phi
->second
.uses
.emplace(pc
.get());
1723 if (temp_in_scc
&& sgpr_operands_alias_defs
) {
1724 /* disable definitions and re-enable operands */
1725 for (const Definition
& def
: instr
->definitions
) {
1726 if (def
.isTemp() && !def
.isKill()) {
1727 for (unsigned j
= 0; j
< def
.size(); j
++) {
1728 register_file
[def
.physReg() + j
] = 0x0;
1732 for (const Operand
& op
: instr
->operands
) {
1733 if (op
.isTemp() && op
.isFirstKill()) {
1734 for (unsigned j
= 0; j
< op
.size(); j
++)
1735 register_file
[op
.physReg() + j
] = 0xFFFF;
1739 handle_pseudo(ctx
, register_file
, pc
.get());
1741 /* re-enable live vars */
1742 for (const Operand
& op
: instr
->operands
) {
1743 if (op
.isTemp() && op
.isFirstKill())
1744 for (unsigned j
= 0; j
< op
.size(); j
++)
1745 register_file
[op
.physReg() + j
] = 0x0;
1747 for (const Definition
& def
: instr
->definitions
) {
1748 if (def
.isTemp() && !def
.isKill()) {
1749 for (unsigned j
= 0; j
< def
.size(); j
++) {
1750 register_file
[def
.physReg() + j
] = def
.tempId();
1755 pc
->tmp_in_scc
= false;
1758 instructions
.emplace_back(std::move(pc
));
1761 /* some instructions need VOP3 encoding if operand/definition is not assigned to VCC */
1762 bool instr_needs_vop3
= !instr
->isVOP3() &&
1763 ((instr
->format
== Format::VOPC
&& !(instr
->definitions
[0].physReg() == vcc
)) ||
1764 (instr
->opcode
== aco_opcode::v_cndmask_b32
&& !(instr
->operands
[2].physReg() == vcc
)) ||
1765 ((instr
->opcode
== aco_opcode::v_add_co_u32
||
1766 instr
->opcode
== aco_opcode::v_addc_co_u32
||
1767 instr
->opcode
== aco_opcode::v_sub_co_u32
||
1768 instr
->opcode
== aco_opcode::v_subb_co_u32
||
1769 instr
->opcode
== aco_opcode::v_subrev_co_u32
||
1770 instr
->opcode
== aco_opcode::v_subbrev_co_u32
) &&
1771 !(instr
->definitions
[1].physReg() == vcc
)) ||
1772 ((instr
->opcode
== aco_opcode::v_addc_co_u32
||
1773 instr
->opcode
== aco_opcode::v_subb_co_u32
||
1774 instr
->opcode
== aco_opcode::v_subbrev_co_u32
) &&
1775 !(instr
->operands
[2].physReg() == vcc
)));
1776 if (instr_needs_vop3
) {
1778 /* if the first operand is a literal, we have to move it to a reg */
1779 if (instr
->operands
.size() && instr
->operands
[0].isLiteral()) {
1780 bool can_sgpr
= true;
1781 /* check, if we have to move to vgpr */
1782 for (const Operand
& op
: instr
->operands
) {
1783 if (op
.isTemp() && op
.getTemp().type() == RegType::sgpr
) {
1788 aco_ptr
<Instruction
> mov
;
1790 mov
.reset(create_instruction
<SOP1_instruction
>(aco_opcode::s_mov_b32
, Format::SOP1
, 1, 1));
1792 mov
.reset(create_instruction
<VOP1_instruction
>(aco_opcode::v_mov_b32
, Format::VOP1
, 1, 1));
1793 mov
->operands
[0] = instr
->operands
[0];
1794 Temp tmp
= {program
->allocateId(), can_sgpr
? s1
: v1
};
1795 mov
->definitions
[0] = Definition(tmp
);
1796 /* disable definitions and re-enable operands */
1797 for (const Definition
& def
: instr
->definitions
) {
1798 for (unsigned j
= 0; j
< def
.size(); j
++) {
1799 register_file
[def
.physReg() + j
] = 0x0;
1802 for (const Operand
& op
: instr
->operands
) {
1803 if (op
.isTemp() && op
.isFirstKill()) {
1804 for (unsigned j
= 0; j
< op
.size(); j
++)
1805 register_file
[op
.physReg() + j
] = 0xFFFF;
1808 mov
->definitions
[0].setFixed(get_reg(ctx
, register_file
, tmp
.regClass(), parallelcopy
, mov
));
1809 instr
->operands
[0] = Operand(tmp
);
1810 instr
->operands
[0].setFixed(mov
->definitions
[0].physReg());
1811 instructions
.emplace_back(std::move(mov
));
1812 /* re-enable live vars */
1813 for (const Operand
& op
: instr
->operands
) {
1814 if (op
.isTemp() && op
.isFirstKill())
1815 for (unsigned j
= 0; j
< op
.size(); j
++)
1816 register_file
[op
.physReg() + j
] = 0x0;
1818 for (const Definition
& def
: instr
->definitions
) {
1819 if (def
.isTemp() && !def
.isKill()) {
1820 for (unsigned j
= 0; j
< def
.size(); j
++) {
1821 register_file
[def
.physReg() + j
] = def
.tempId();
1827 /* change the instruction to VOP3 to enable an arbitrary register pair as dst */
1828 aco_ptr
<Instruction
> tmp
= std::move(instr
);
1829 Format format
= asVOP3(tmp
->format
);
1830 instr
.reset(create_instruction
<VOP3A_instruction
>(tmp
->opcode
, format
, tmp
->operands
.size(), tmp
->definitions
.size()));
1831 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
1832 Operand
& operand
= tmp
->operands
[i
];
1833 instr
->operands
[i
] = operand
;
1834 /* keep phi_map up to date */
1835 if (operand
.isTemp()) {
1836 std::map
<unsigned, phi_info
>::iterator phi
= phi_map
.find(operand
.tempId());
1837 if (phi
!= phi_map
.end()) {
1838 phi
->second
.uses
.erase(tmp
.get());
1839 phi
->second
.uses
.emplace(instr
.get());
1843 std::copy(tmp
->definitions
.begin(), tmp
->definitions
.end(), instr
->definitions
.begin());
1845 instructions
.emplace_back(std::move(*it
));
1847 } /* end for Instr */
1849 block
.instructions
= std::move(instructions
);
1851 filled
[block
.index
] = true;
1852 for (unsigned succ_idx
: block
.linear_succs
) {
1853 Block
& succ
= program
->blocks
[succ_idx
];
1854 /* seal block if all predecessors are filled */
1855 bool all_filled
= true;
1856 for (unsigned pred_idx
: succ
.linear_preds
) {
1857 if (!filled
[pred_idx
]) {
1863 /* finish incomplete phis and check if they became trivial */
1864 for (Instruction
* phi
: incomplete_phis
[succ_idx
]) {
1865 std::vector
<unsigned> preds
= phi
->definitions
[0].getTemp().is_linear() ? succ
.linear_preds
: succ
.logical_preds
;
1866 for (unsigned i
= 0; i
< phi
->operands
.size(); i
++) {
1867 phi
->operands
[i
].setTemp(read_variable(phi
->operands
[i
].getTemp(), preds
[i
]));
1868 phi
->operands
[i
].setFixed(ctx
.assignments
[phi
->operands
[i
].tempId()].first
);
1870 try_remove_trivial_phi(phi_map
.find(phi
->definitions
[0].tempId()));
1872 /* complete the original phi nodes, but no need to check triviality */
1873 for (aco_ptr
<Instruction
>& instr
: succ
.instructions
) {
1876 std::vector
<unsigned> preds
= instr
->opcode
== aco_opcode::p_phi
? succ
.logical_preds
: succ
.linear_preds
;
1878 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
1879 auto& operand
= instr
->operands
[i
];
1880 if (!operand
.isTemp())
1882 operand
.setTemp(read_variable(operand
.getTemp(), preds
[i
]));
1883 operand
.setFixed(ctx
.assignments
[operand
.tempId()].first
);
1884 std::map
<unsigned, phi_info
>::iterator phi
= phi_map
.find(operand
.getTemp().id());
1885 if (phi
!= phi_map
.end())
1886 phi
->second
.uses
.emplace(instr
.get());
1889 sealed
[succ_idx
] = true;
1894 /* remove trivial phis */
1895 for (Block
& block
: program
->blocks
) {
1896 auto end
= std::find_if(block
.instructions
.begin(), block
.instructions
.end(),
1897 [](aco_ptr
<Instruction
>& instr
) { return !is_phi(instr
);});
1898 auto middle
= std::remove_if(block
.instructions
.begin(), end
,
1899 [](const aco_ptr
<Instruction
>& instr
) { return instr
->definitions
.empty();});
1900 block
.instructions
.erase(middle
, end
);
1903 /* find scc spill registers which may be needed for parallelcopies created by phis */
1904 for (Block
& block
: program
->blocks
) {
1905 if (block
.linear_preds
.size() <= 1)
1908 std::bitset
<128> regs
= sgpr_live_in
[block
.index
];
1912 /* choose a register */
1914 for (; reg
< ctx
.program
->max_reg_demand
.sgpr
&& regs
[reg
]; reg
++)
1916 assert(reg
< ctx
.program
->max_reg_demand
.sgpr
);
1917 adjust_max_used_regs(ctx
, s1
, reg
);
1919 /* update predecessors */
1920 for (unsigned& pred_index
: block
.linear_preds
) {
1921 Block
& pred
= program
->blocks
[pred_index
];
1922 pred
.scc_live_out
= true;
1923 pred
.scratch_sgpr
= PhysReg
{(uint16_t)reg
};
1927 /* num_gpr = rnd_up(max_used_gpr + 1) */
1928 program
->config
->num_vgprs
= align(ctx
.max_used_vgpr
+ 1, 4);
1929 if (program
->family
== CHIP_TONGA
|| program
->family
== CHIP_ICELAND
) /* workaround hardware bug */
1930 program
->config
->num_sgprs
= get_sgpr_alloc(program
, program
->sgpr_limit
);
1932 program
->config
->num_sgprs
= align(ctx
.max_used_sgpr
+ 1 + get_extra_sgprs(program
), 8);