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
) {}
56 RegisterFile() {regs
.fill(0);}
58 std::array
<uint32_t, 512> regs
;
60 const uint32_t& operator [] (unsigned index
) const {
64 uint32_t& operator [] (unsigned index
) {
68 unsigned count_zero(PhysReg start
, unsigned size
) {
70 for (unsigned i
= 0; i
< size
; i
++)
71 res
+= !regs
[start
+ i
];
75 bool test(PhysReg start
, unsigned size
) {
76 for (unsigned i
= 0; i
< size
; i
++) {
83 void fill(PhysReg start
, unsigned size
, uint32_t val
) {
84 for (unsigned i
= 0; i
< size
; i
++)
85 regs
[start
+ i
] = val
;
88 void clear(PhysReg start
, unsigned size
) {
92 void fill(Operand op
) {
93 fill(op
.physReg(), op
.size(), op
.tempId());
96 void clear(Operand op
) {
97 fill(op
.physReg(), op
.size(), 0);
100 void fill(Definition def
) {
101 fill(def
.physReg(), def
.size(), def
.tempId());
104 void clear(Definition def
) {
105 fill(def
.physReg(), def
.size(), 0);
110 /* helper function for debugging */
112 void print_regs(ra_ctx
& ctx
, bool vgprs
, RegisterFile
& reg_file
)
114 unsigned max
= vgprs
? ctx
.program
->max_reg_demand
.vgpr
: ctx
.program
->max_reg_demand
.sgpr
;
115 unsigned lb
= vgprs
? 256 : 0;
116 unsigned ub
= lb
+ max
;
117 char reg_char
= vgprs
? 'v' : 's';
121 for (unsigned i
= lb
; i
< ub
; i
+= 3) {
122 printf("%.2u ", i
- lb
);
127 printf("%cgprs: ", reg_char
);
128 unsigned free_regs
= 0;
130 bool char_select
= false;
131 for (unsigned i
= lb
; i
< ub
; i
++) {
132 if (reg_file
[i
] == 0xFFFF) {
134 } else if (reg_file
[i
]) {
135 if (reg_file
[i
] != prev
) {
137 char_select
= !char_select
;
139 printf(char_select
? "#" : "@");
147 printf("%u/%u used, %u/%u free\n", max
- free_regs
, max
, free_regs
, max
);
149 /* print assignments */
152 for (unsigned i
= lb
; i
< ub
; i
++) {
153 if (reg_file
[i
] != prev
) {
154 if (prev
&& size
> 1)
155 printf("-%d]\n", i
- 1 - lb
);
159 if (prev
&& prev
!= 0xFFFF) {
160 if (ctx
.orig_names
.count(reg_file
[i
]) && ctx
.orig_names
[reg_file
[i
]].id() != reg_file
[i
])
161 printf("%%%u (was %%%d) = %c[%d", reg_file
[i
], ctx
.orig_names
[reg_file
[i
]].id(), reg_char
, i
- lb
);
163 printf("%%%u = %c[%d", reg_file
[i
], reg_char
, i
- lb
);
170 if (prev
&& size
> 1)
171 printf("-%d]\n", ub
- lb
- 1);
178 void adjust_max_used_regs(ra_ctx
& ctx
, RegClass rc
, unsigned reg
)
180 unsigned max_addressible_sgpr
= ctx
.program
->sgpr_limit
;
181 unsigned size
= rc
.size();
182 if (rc
.type() == RegType::vgpr
) {
184 unsigned hi
= reg
- 256 + size
- 1;
185 ctx
.max_used_vgpr
= std::max(ctx
.max_used_vgpr
, hi
);
186 } else if (reg
+ rc
.size() <= max_addressible_sgpr
) {
187 unsigned hi
= reg
+ size
- 1;
188 ctx
.max_used_sgpr
= std::max(ctx
.max_used_sgpr
, std::min(hi
, max_addressible_sgpr
));
193 void update_renames(ra_ctx
& ctx
, RegisterFile
& reg_file
,
194 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
195 aco_ptr
<Instruction
>& instr
)
197 /* allocate id's and rename operands: this is done transparently here */
198 for (std::pair
<Operand
, Definition
>& copy
: parallelcopies
) {
199 /* the definitions with id are not from this function and already handled */
200 if (copy
.second
.isTemp())
203 /* check if we we moved another parallelcopy definition */
204 for (std::pair
<Operand
, Definition
>& other
: parallelcopies
) {
205 if (!other
.second
.isTemp())
207 if (copy
.first
.getTemp() == other
.second
.getTemp()) {
208 copy
.first
.setTemp(other
.first
.getTemp());
209 copy
.first
.setFixed(other
.first
.physReg());
212 // FIXME: if a definition got moved, change the target location and remove the parallelcopy
213 copy
.second
.setTemp(Temp(ctx
.program
->allocateId(), copy
.second
.regClass()));
214 ctx
.assignments
[copy
.second
.tempId()] = {copy
.second
.physReg(), copy
.second
.regClass()};
215 for (unsigned i
= copy
.second
.physReg().reg(); i
< copy
.second
.physReg() + copy
.second
.size(); i
++)
216 reg_file
[i
] = copy
.second
.tempId();
217 /* check if we moved an operand */
218 for (Operand
& op
: instr
->operands
) {
221 if (op
.tempId() == copy
.first
.tempId()) {
222 bool omit_renaming
= instr
->opcode
== aco_opcode::p_create_vector
&& !op
.isKillBeforeDef();
223 for (std::pair
<Operand
, Definition
>& pc
: parallelcopies
) {
224 PhysReg def_reg
= pc
.second
.physReg();
225 omit_renaming
&= def_reg
> copy
.first
.physReg() ?
226 (copy
.first
.physReg() + copy
.first
.size() <= def_reg
.reg()) :
227 (def_reg
+ pc
.second
.size() <= copy
.first
.physReg().reg());
231 op
.setTemp(copy
.second
.getTemp());
232 op
.setFixed(copy
.second
.physReg());
238 std::pair
<PhysReg
, bool> get_reg_simple(ra_ctx
& ctx
,
239 RegisterFile
& reg_file
,
240 uint32_t lb
, uint32_t ub
,
241 uint32_t size
, uint32_t stride
,
244 /* best fit algorithm: find the smallest gap to fit in the variable */
246 unsigned best_pos
= 0xFFFF;
247 unsigned gap_size
= 0xFFFF;
248 unsigned next_pos
= 0xFFFF;
250 for (unsigned current_reg
= lb
; current_reg
< ub
; current_reg
++) {
251 if (reg_file
[current_reg
] != 0 || ctx
.war_hint
[current_reg
]) {
252 if (next_pos
== 0xFFFF)
255 /* check if the variable fits */
256 if (next_pos
+ size
> current_reg
) {
261 /* check if the tested gap is smaller */
262 if (current_reg
- next_pos
< gap_size
) {
264 gap_size
= current_reg
- next_pos
;
270 if (next_pos
== 0xFFFF)
271 next_pos
= current_reg
;
275 if (next_pos
!= 0xFFFF &&
276 next_pos
+ size
<= ub
&&
277 ub
- next_pos
< gap_size
) {
279 gap_size
= ub
- next_pos
;
281 if (best_pos
!= 0xFFFF) {
282 adjust_max_used_regs(ctx
, rc
, best_pos
);
283 return {PhysReg
{best_pos
}, true};
289 unsigned reg_lo
= lb
;
290 unsigned reg_hi
= lb
+ size
- 1;
291 while (!found
&& reg_lo
+ size
<= ub
) {
292 if (reg_file
[reg_lo
] != 0) {
296 reg_hi
= reg_lo
+ size
- 1;
298 for (unsigned reg
= reg_lo
+ 1; found
&& reg
<= reg_hi
; reg
++) {
299 if (reg_file
[reg
] != 0 || ctx
.war_hint
[reg
])
303 adjust_max_used_regs(ctx
, rc
, reg_lo
);
304 return {PhysReg
{reg_lo
}, true};
313 bool get_regs_for_copies(ra_ctx
& ctx
,
314 RegisterFile
& reg_file
,
315 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
316 const std::set
<std::pair
<unsigned, unsigned>> &vars
,
317 uint32_t lb
, uint32_t ub
,
318 aco_ptr
<Instruction
>& instr
,
323 /* variables are sorted from small sized to large */
324 /* NOTE: variables are also sorted by ID. this only affects a very small number of shaders slightly though. */
325 for (std::set
<std::pair
<unsigned, unsigned>>::const_reverse_iterator it
= vars
.rbegin(); it
!= vars
.rend(); ++it
) {
326 unsigned id
= it
->second
;
327 std::pair
<PhysReg
, RegClass
> var
= ctx
.assignments
[id
];
328 uint32_t size
= it
->first
;
330 if (var
.second
.type() == RegType::sgpr
) {
337 /* check if this is a dead operand, then we can re-use the space from the definition */
338 bool is_dead_operand
= false;
339 for (unsigned i
= 0; !is_phi(instr
) && !is_dead_operand
&& (i
< instr
->operands
.size()); i
++) {
340 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isKillBeforeDef() && instr
->operands
[i
].tempId() == id
)
341 is_dead_operand
= true;
344 std::pair
<PhysReg
, bool> res
;
345 if (is_dead_operand
) {
346 if (instr
->opcode
== aco_opcode::p_create_vector
) {
347 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].size(), i
++) {
348 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].tempId() == id
) {
349 for (unsigned j
= 0; j
< size
; j
++)
350 assert(reg_file
[def_reg_lo
+ offset
+ j
] == 0);
351 res
= {PhysReg
{def_reg_lo
+ offset
}, true};
356 res
= get_reg_simple(ctx
, reg_file
, def_reg_lo
, def_reg_hi
+ 1, size
, stride
, var
.second
);
359 res
= get_reg_simple(ctx
, reg_file
, lb
, def_reg_lo
, size
, stride
, var
.second
);
361 unsigned lb
= (def_reg_hi
+ stride
) & ~(stride
- 1);
362 res
= get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, stride
, var
.second
);
367 /* mark the area as blocked */
368 reg_file
.fill(res
.first
, size
, 0xFFFFFFFF);
369 /* create parallelcopy pair (without definition id) */
370 Temp tmp
= Temp(id
, var
.second
);
371 Operand pc_op
= Operand(tmp
);
372 pc_op
.setFixed(var
.first
);
373 Definition pc_def
= Definition(res
.first
, pc_op
.regClass());
374 parallelcopies
.emplace_back(pc_op
, pc_def
);
378 unsigned best_pos
= lb
;
379 unsigned num_moves
= 0xFF;
380 unsigned num_vars
= 0;
382 /* we use a sliding window to find potential positions */
383 unsigned reg_lo
= lb
;
384 unsigned reg_hi
= lb
+ size
- 1;
385 for (reg_lo
= lb
, reg_hi
= lb
+ size
- 1; reg_hi
< ub
; reg_lo
+= stride
, reg_hi
+= stride
) {
386 if (!is_dead_operand
&& ((reg_lo
>= def_reg_lo
&& reg_lo
<= def_reg_hi
) ||
387 (reg_hi
>= def_reg_lo
&& reg_hi
<= def_reg_hi
)))
390 /* second, check that we have at most k=num_moves elements in the window
391 * and no element is larger than the currently processed one */
394 unsigned last_var
= 0;
396 for (unsigned j
= reg_lo
; found
&& j
<= reg_hi
; j
++) {
397 if (reg_file
[j
] == 0 || reg_file
[j
] == last_var
)
400 /* 0xFFFF signals that this area is already blocked! */
401 if (reg_file
[j
] == 0xFFFFFFFF || k
> num_moves
) {
405 /* we cannot split live ranges of linear vgprs */
406 if (ctx
.assignments
[reg_file
[j
]].second
& (1 << 6)) {
410 bool is_kill
= false;
411 for (const Operand
& op
: instr
->operands
) {
412 if (op
.isTemp() && op
.isKillBeforeDef() && op
.tempId() == reg_file
[j
]) {
417 if (!is_kill
&& ctx
.assignments
[reg_file
[j
]].second
.size() >= size
) {
422 k
+= ctx
.assignments
[reg_file
[j
]].second
.size();
423 last_var
= reg_file
[j
];
425 if (k
> num_moves
|| (k
== num_moves
&& n
<= num_vars
)) {
438 /* FIXME: we messed up and couldn't find space for the variables to be copied */
439 if (num_moves
== 0xFF)
443 reg_hi
= best_pos
+ size
- 1;
445 /* collect variables and block reg file */
446 std::set
<std::pair
<unsigned, unsigned>> new_vars
;
447 for (unsigned j
= reg_lo
; j
<= reg_hi
; j
++) {
448 if (reg_file
[j
] != 0) {
449 unsigned size
= ctx
.assignments
[reg_file
[j
]].second
.size();
450 unsigned id
= reg_file
[j
];
451 new_vars
.emplace(size
, id
);
452 reg_file
.clear(ctx
.assignments
[id
].first
, size
);
456 /* mark the area as blocked */
457 reg_file
.fill(PhysReg
{reg_lo
}, size
, 0xFFFFFFFF);
459 if (!get_regs_for_copies(ctx
, reg_file
, parallelcopies
, new_vars
, lb
, ub
, instr
, def_reg_lo
, def_reg_hi
))
462 adjust_max_used_regs(ctx
, var
.second
, reg_lo
);
464 /* create parallelcopy pair (without definition id) */
465 Temp tmp
= Temp(id
, var
.second
);
466 Operand pc_op
= Operand(tmp
);
467 pc_op
.setFixed(var
.first
);
468 Definition pc_def
= Definition(PhysReg
{reg_lo
}, pc_op
.regClass());
469 parallelcopies
.emplace_back(pc_op
, pc_def
);
476 std::pair
<PhysReg
, bool> get_reg_impl(ra_ctx
& ctx
,
477 RegisterFile
& reg_file
,
478 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
479 uint32_t lb
, uint32_t ub
,
480 uint32_t size
, uint32_t stride
,
482 aco_ptr
<Instruction
>& instr
)
484 /* check how many free regs we have */
485 unsigned regs_free
= reg_file
.count_zero(PhysReg
{lb
}, ub
-lb
);
487 /* mark and count killed operands */
488 unsigned killed_ops
= 0;
489 for (unsigned j
= 0; !is_phi(instr
) && j
< instr
->operands
.size(); j
++) {
490 if (instr
->operands
[j
].isTemp() &&
491 instr
->operands
[j
].isFirstKillBeforeDef() &&
492 instr
->operands
[j
].physReg() >= lb
&&
493 instr
->operands
[j
].physReg() < ub
) {
494 assert(instr
->operands
[j
].isFixed());
495 assert(reg_file
[instr
->operands
[j
].physReg()] == 0);
496 reg_file
.fill(instr
->operands
[j
].physReg(), instr
->operands
[j
].size(), 0xFFFFFFFF);
497 killed_ops
+= instr
->operands
[j
].getTemp().size();
501 assert(regs_free
>= size
);
502 /* we might have to move dead operands to dst in order to make space */
503 unsigned op_moves
= 0;
505 if (size
> (regs_free
- killed_ops
))
506 op_moves
= size
- (regs_free
- killed_ops
);
508 /* find the best position to place the definition */
509 unsigned best_pos
= lb
;
510 unsigned num_moves
= 0xFF;
511 unsigned num_vars
= 0;
513 /* we use a sliding window to check potential positions */
514 unsigned reg_lo
= lb
;
515 unsigned reg_hi
= lb
+ size
- 1;
516 for (reg_lo
= lb
, reg_hi
= lb
+ size
- 1; reg_hi
< ub
; reg_lo
+= stride
, reg_hi
+= stride
) {
517 /* first check the edges: this is what we have to fix to allow for num_moves > size */
518 if (reg_lo
> lb
&& reg_file
[reg_lo
] != 0 && reg_file
[reg_lo
] == reg_file
[reg_lo
- 1])
520 if (reg_hi
< ub
- 1 && reg_file
[reg_hi
] != 0 && reg_file
[reg_hi
] == reg_file
[reg_hi
+ 1])
523 /* second, check that we have at most k=num_moves elements in the window
524 * and no element is larger than the currently processed one */
525 unsigned k
= op_moves
;
527 unsigned remaining_op_moves
= op_moves
;
528 unsigned last_var
= 0;
530 bool aligned
= rc
== RegClass::v4
&& reg_lo
% 4 == 0;
531 for (unsigned j
= reg_lo
; found
&& j
<= reg_hi
; j
++) {
532 if (reg_file
[j
] == 0 || reg_file
[j
] == last_var
)
535 /* dead operands effectively reduce the number of estimated moves */
536 if (remaining_op_moves
&& reg_file
[j
] == 0xFFFFFFFF) {
538 remaining_op_moves
--;
542 if (ctx
.assignments
[reg_file
[j
]].second
.size() >= size
) {
548 /* we cannot split live ranges of linear vgprs */
549 if (ctx
.assignments
[reg_file
[j
]].second
& (1 << 6)) {
554 k
+= ctx
.assignments
[reg_file
[j
]].second
.size();
556 last_var
= reg_file
[j
];
559 if (!found
|| k
> num_moves
)
561 if (k
== num_moves
&& n
< num_vars
)
563 if (!aligned
&& k
== num_moves
&& n
== num_vars
)
573 if (num_moves
== 0xFF) {
574 /* remove killed operands from reg_file once again */
575 for (unsigned i
= 0; !is_phi(instr
) && i
< instr
->operands
.size(); i
++) {
576 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isFirstKillBeforeDef())
577 reg_file
.clear(instr
->operands
[i
]);
579 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
580 Definition def
= instr
->definitions
[i
];
581 if (def
.isTemp() && def
.isFixed() && ctx
.defs_done
.test(i
))
587 RegisterFile register_file
= reg_file
;
589 /* now, we figured the placement for our definition */
590 std::set
<std::pair
<unsigned, unsigned>> vars
;
591 for (unsigned j
= best_pos
; j
< best_pos
+ size
; j
++) {
592 if (reg_file
[j
] != 0xFFFFFFFF && reg_file
[j
] != 0)
593 vars
.emplace(ctx
.assignments
[reg_file
[j
]].second
.size(), reg_file
[j
]);
597 if (instr
->opcode
== aco_opcode::p_create_vector
) {
598 /* move killed operands which aren't yet at the correct position */
599 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].size(), i
++) {
600 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isFirstKillBeforeDef() &&
601 instr
->operands
[i
].getTemp().type() == rc
.type()) {
603 if (instr
->operands
[i
].physReg() != best_pos
+ offset
) {
604 vars
.emplace(instr
->operands
[i
].size(), instr
->operands
[i
].tempId());
605 reg_file
.clear(instr
->operands
[i
]);
607 reg_file
.fill(instr
->operands
[i
]);
612 /* re-enable the killed operands */
613 for (unsigned j
= 0; !is_phi(instr
) && j
< instr
->operands
.size(); j
++) {
614 if (instr
->operands
[j
].isTemp() && instr
->operands
[j
].isFirstKill())
615 reg_file
.fill(instr
->operands
[j
]);
619 std::vector
<std::pair
<Operand
, Definition
>> pc
;
620 if (!get_regs_for_copies(ctx
, reg_file
, pc
, vars
, lb
, ub
, instr
, best_pos
, best_pos
+ size
- 1)) {
621 reg_file
= std::move(register_file
);
622 /* remove killed operands from reg_file once again */
623 if (!is_phi(instr
)) {
624 for (const Operand
& op
: instr
->operands
) {
625 if (op
.isTemp() && op
.isFirstKillBeforeDef())
629 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
630 Definition
& def
= instr
->definitions
[i
];
631 if (def
.isTemp() && def
.isFixed() && ctx
.defs_done
.test(i
))
637 parallelcopies
.insert(parallelcopies
.end(), pc
.begin(), pc
.end());
639 /* we set the definition regs == 0. the actual caller is responsible for correct setting */
640 reg_file
.clear(PhysReg
{best_pos
}, size
);
642 update_renames(ctx
, reg_file
, parallelcopies
, instr
);
644 /* remove killed operands from reg_file once again */
645 for (unsigned i
= 0; !is_phi(instr
) && i
< instr
->operands
.size(); i
++) {
646 if (!instr
->operands
[i
].isTemp() || !instr
->operands
[i
].isFixed())
648 assert(!instr
->operands
[i
].isUndefined());
649 if (instr
->operands
[i
].isFirstKillBeforeDef())
650 reg_file
.clear(instr
->operands
[i
]);
652 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
653 Definition def
= instr
->definitions
[i
];
654 if (def
.isTemp() && def
.isFixed() && ctx
.defs_done
.test(i
))
658 adjust_max_used_regs(ctx
, rc
, best_pos
);
659 return {PhysReg
{best_pos
}, true};
662 PhysReg
get_reg(ra_ctx
& ctx
,
663 RegisterFile
& reg_file
,
665 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
666 aco_ptr
<Instruction
>& instr
)
668 uint32_t size
= rc
.size();
671 if (rc
.type() == RegType::vgpr
) {
673 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
676 ub
= ctx
.program
->max_reg_demand
.sgpr
;
683 std::pair
<PhysReg
, bool> res
= {{}, false};
684 /* try to find space without live-range splits */
685 if (rc
.type() == RegType::vgpr
&& (size
== 4 || size
== 8))
686 res
= get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, 4, rc
);
688 res
= get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, stride
, rc
);
692 /* try to find space with live-range splits */
693 res
= get_reg_impl(ctx
, reg_file
, parallelcopies
, lb
, ub
, size
, stride
, rc
, instr
);
698 /* try using more registers */
700 /* We should only fail here because keeping under the limit would require
702 assert(reg_file
.count_zero(PhysReg
{lb
}, ub
-lb
) >= size
);
704 uint16_t max_addressible_sgpr
= ctx
.program
->sgpr_limit
;
705 uint16_t max_addressible_vgpr
= ctx
.program
->vgpr_limit
;
706 if (rc
.type() == RegType::vgpr
&& ctx
.program
->max_reg_demand
.vgpr
< max_addressible_vgpr
) {
707 update_vgpr_sgpr_demand(ctx
.program
, RegisterDemand(ctx
.program
->max_reg_demand
.vgpr
+ 1, ctx
.program
->max_reg_demand
.sgpr
));
708 return get_reg(ctx
, reg_file
, rc
, parallelcopies
, instr
);
709 } else if (rc
.type() == RegType::sgpr
&& ctx
.program
->max_reg_demand
.sgpr
< max_addressible_sgpr
) {
710 update_vgpr_sgpr_demand(ctx
.program
, RegisterDemand(ctx
.program
->max_reg_demand
.vgpr
, ctx
.program
->max_reg_demand
.sgpr
+ 1));
711 return get_reg(ctx
, reg_file
, rc
, parallelcopies
, instr
);
714 //FIXME: if nothing helps, shift-rotate the registers to make space
716 unreachable("did not find a register");
720 std::pair
<PhysReg
, bool> get_reg_vec(ra_ctx
& ctx
,
721 RegisterFile
& reg_file
,
724 uint32_t size
= rc
.size();
727 if (rc
.type() == RegType::vgpr
) {
729 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
732 ub
= ctx
.program
->max_reg_demand
.sgpr
;
738 return get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, stride
, rc
);
742 PhysReg
get_reg_create_vector(ra_ctx
& ctx
,
743 RegisterFile
& reg_file
,
745 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
746 aco_ptr
<Instruction
>& instr
)
748 /* create_vector instructions have different costs w.r.t. register coalescing */
749 uint32_t size
= rc
.size();
752 if (rc
.type() == RegType::vgpr
) {
754 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
757 ub
= ctx
.program
->max_reg_demand
.sgpr
;
764 unsigned best_pos
= -1;
765 unsigned num_moves
= 0xFF;
766 bool best_war_hint
= true;
768 /* test for each operand which definition placement causes the least shuffle instructions */
769 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].size(), i
++) {
770 // TODO: think about, if we can alias live operands on the same register
771 if (!instr
->operands
[i
].isTemp() || !instr
->operands
[i
].isKillBeforeDef() || instr
->operands
[i
].getTemp().type() != rc
.type())
774 if (offset
> instr
->operands
[i
].physReg())
777 unsigned reg_lo
= instr
->operands
[i
].physReg() - offset
;
778 unsigned reg_hi
= reg_lo
+ size
- 1;
781 /* no need to check multiple times */
782 if (reg_lo
== best_pos
)
786 // TODO: this can be improved */
787 if (reg_lo
< lb
|| reg_hi
>= ub
|| reg_lo
% stride
!= 0)
789 if (reg_lo
> lb
&& reg_file
[reg_lo
] != 0 && reg_file
[reg_lo
] == reg_file
[reg_lo
- 1])
791 if (reg_hi
< ub
- 1 && reg_file
[reg_hi
] != 0 && reg_file
[reg_hi
] == reg_file
[reg_hi
+ 1])
794 /* count variables to be moved and check war_hint */
795 bool war_hint
= false;
796 bool linear_vgpr
= false;
797 for (unsigned j
= reg_lo
; j
<= reg_hi
&& !linear_vgpr
; j
++) {
798 if (reg_file
[j
] != 0) {
800 /* we cannot split live ranges of linear vgprs */
801 if (ctx
.assignments
[reg_file
[j
]].second
& (1 << 6))
804 war_hint
|= ctx
.war_hint
[j
];
806 if (linear_vgpr
|| (war_hint
&& !best_war_hint
))
809 /* count operands in wrong positions */
810 for (unsigned j
= 0, offset
= 0; j
< instr
->operands
.size(); offset
+= instr
->operands
[j
].size(), j
++) {
812 !instr
->operands
[j
].isTemp() ||
813 instr
->operands
[j
].getTemp().type() != rc
.type())
815 if (instr
->operands
[j
].physReg() != reg_lo
+ offset
)
816 k
+= instr
->operands
[j
].size();
818 bool aligned
= rc
== RegClass::v4
&& reg_lo
% 4 == 0;
819 if (k
> num_moves
|| (!aligned
&& k
== num_moves
))
824 best_war_hint
= war_hint
;
827 if (num_moves
>= size
)
828 return get_reg(ctx
, reg_file
, rc
, parallelcopies
, instr
);
830 /* collect variables to be moved */
831 std::set
<std::pair
<unsigned, unsigned>> vars
;
832 for (unsigned i
= best_pos
; i
< best_pos
+ size
; i
++) {
833 if (reg_file
[i
] != 0)
834 vars
.emplace(ctx
.assignments
[reg_file
[i
]].second
.size(), reg_file
[i
]);
838 /* move killed operands which aren't yet at the correct position */
839 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].size(), i
++) {
840 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isFirstKillBeforeDef() && instr
->operands
[i
].getTemp().type() == rc
.type()) {
841 if (instr
->operands
[i
].physReg() != best_pos
+ offset
)
842 vars
.emplace(instr
->operands
[i
].size(), instr
->operands
[i
].tempId());
844 reg_file
.fill(instr
->operands
[i
]);
848 ASSERTED
bool success
= false;
849 success
= get_regs_for_copies(ctx
, reg_file
, parallelcopies
, vars
, lb
, ub
, instr
, best_pos
, best_pos
+ size
- 1);
852 update_renames(ctx
, reg_file
, parallelcopies
, instr
);
853 adjust_max_used_regs(ctx
, rc
, best_pos
);
854 return PhysReg
{best_pos
};
857 bool get_reg_specified(ra_ctx
& ctx
,
858 RegisterFile
& reg_file
,
860 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
861 aco_ptr
<Instruction
>& instr
,
864 uint32_t size
= rc
.size();
868 if (rc
.type() == RegType::vgpr
) {
870 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
876 if (reg
% stride
!= 0)
879 ub
= ctx
.program
->max_reg_demand
.sgpr
;
882 uint32_t reg_lo
= reg
.reg();
883 uint32_t reg_hi
= reg
+ (size
- 1);
885 if (reg_lo
< lb
|| reg_hi
>= ub
|| reg_lo
> reg_hi
)
888 if (reg_file
.test(reg
, size
))
890 adjust_max_used_regs(ctx
, rc
, reg_lo
);
894 void handle_pseudo(ra_ctx
& ctx
,
895 const RegisterFile
& reg_file
,
898 if (instr
->format
!= Format::PSEUDO
)
901 /* all instructions which use handle_operands() need this information */
902 switch (instr
->opcode
) {
903 case aco_opcode::p_extract_vector
:
904 case aco_opcode::p_create_vector
:
905 case aco_opcode::p_split_vector
:
906 case aco_opcode::p_parallelcopy
:
907 case aco_opcode::p_wqm
:
913 /* if all definitions are vgpr, no need to care for SCC */
914 bool writes_sgpr
= false;
915 for (Definition
& def
: instr
->definitions
) {
916 if (def
.getTemp().type() == RegType::sgpr
) {
921 /* if all operands are constant, no need to care either */
922 bool reads_sgpr
= false;
923 for (Operand
& op
: instr
->operands
) {
924 if (op
.isTemp() && op
.getTemp().type() == RegType::sgpr
) {
929 if (!(writes_sgpr
&& reads_sgpr
))
932 Pseudo_instruction
*pi
= (Pseudo_instruction
*)instr
;
933 if (reg_file
[scc
.reg()]) {
934 pi
->tmp_in_scc
= true;
936 int reg
= ctx
.max_used_sgpr
;
937 for (; reg
>= 0 && reg_file
[reg
]; reg
--)
940 reg
= ctx
.max_used_sgpr
+ 1;
941 for (; reg
< ctx
.program
->max_reg_demand
.sgpr
&& reg_file
[reg
]; reg
++)
943 assert(reg
< ctx
.program
->max_reg_demand
.sgpr
);
946 adjust_max_used_regs(ctx
, s1
, reg
);
947 pi
->scratch_sgpr
= PhysReg
{(unsigned)reg
};
949 pi
->tmp_in_scc
= false;
953 bool operand_can_use_reg(aco_ptr
<Instruction
>& instr
, unsigned idx
, PhysReg reg
)
955 switch (instr
->format
) {
959 (reg
!= m0
|| idx
== 1 || idx
== 3) && /* offset can be m0 */
960 (reg
!= vcc
|| (instr
->definitions
.empty() && idx
== 2)); /* sdata can be vcc */
962 // TODO: there are more instructions with restrictions on registers
967 } /* end namespace */
970 void register_allocation(Program
*program
, std::vector
<std::set
<Temp
>> live_out_per_block
)
974 std::vector
<std::unordered_map
<unsigned, Temp
>> renames(program
->blocks
.size());
979 std::set
<Instruction
*> uses
;
982 bool filled
[program
->blocks
.size()];
983 bool sealed
[program
->blocks
.size()];
984 memset(filled
, 0, sizeof filled
);
985 memset(sealed
, 0, sizeof sealed
);
986 std::vector
<std::vector
<Instruction
*>> incomplete_phis(program
->blocks
.size());
987 std::map
<unsigned, phi_info
> phi_map
;
988 std::map
<unsigned, unsigned> affinities
;
989 std::function
<Temp(Temp
,unsigned)> read_variable
;
990 std::function
<Temp(Temp
,Block
*)> handle_live_in
;
991 std::function
<Temp(std::map
<unsigned, phi_info
>::iterator
)> try_remove_trivial_phi
;
993 read_variable
= [&](Temp val
, unsigned block_idx
) -> Temp
{
994 std::unordered_map
<unsigned, Temp
>::iterator it
= renames
[block_idx
].find(val
.id());
995 assert(it
!= renames
[block_idx
].end());
999 handle_live_in
= [&](Temp val
, Block
*block
) -> Temp
{
1000 std::vector
<unsigned>& preds
= val
.is_linear() ? block
->linear_preds
: block
->logical_preds
;
1001 if (preds
.size() == 0 || val
.regClass() == val
.regClass().as_linear()) {
1002 renames
[block
->index
][val
.id()] = val
;
1005 assert(preds
.size() > 0);
1008 if (!sealed
[block
->index
]) {
1009 /* consider rename from already processed predecessor */
1010 Temp tmp
= read_variable(val
, preds
[0]);
1012 /* if the block is not sealed yet, we create an incomplete phi (which might later get removed again) */
1013 new_val
= Temp
{program
->allocateId(), val
.regClass()};
1014 aco_opcode opcode
= val
.is_linear() ? aco_opcode::p_linear_phi
: aco_opcode::p_phi
;
1015 aco_ptr
<Instruction
> phi
{create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, preds
.size(), 1)};
1016 phi
->definitions
[0] = Definition(new_val
);
1017 for (unsigned i
= 0; i
< preds
.size(); i
++)
1018 phi
->operands
[i
] = Operand(val
);
1019 if (tmp
.regClass() == new_val
.regClass())
1020 affinities
[new_val
.id()] = tmp
.id();
1022 phi_map
.emplace(new_val
.id(), phi_info
{phi
.get(), block
->index
});
1023 incomplete_phis
[block
->index
].emplace_back(phi
.get());
1024 block
->instructions
.insert(block
->instructions
.begin(), std::move(phi
));
1026 } else if (preds
.size() == 1) {
1027 /* if the block has only one predecessor, just look there for the name */
1028 new_val
= read_variable(val
, preds
[0]);
1030 /* there are multiple predecessors and the block is sealed */
1031 Temp ops
[preds
.size()];
1033 /* we start assuming that the name is the same from all predecessors */
1034 renames
[block
->index
][val
.id()] = val
;
1035 bool needs_phi
= false;
1037 /* get the rename from each predecessor and check if they are the same */
1038 for (unsigned i
= 0; i
< preds
.size(); i
++) {
1039 ops
[i
] = read_variable(val
, preds
[i
]);
1043 needs_phi
|= !(new_val
== ops
[i
]);
1047 /* the variable has been renamed differently in the predecessors: we need to insert a phi */
1048 aco_opcode opcode
= val
.is_linear() ? aco_opcode::p_linear_phi
: aco_opcode::p_phi
;
1049 aco_ptr
<Instruction
> phi
{create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, preds
.size(), 1)};
1050 new_val
= Temp
{program
->allocateId(), val
.regClass()};
1051 phi
->definitions
[0] = Definition(new_val
);
1052 for (unsigned i
= 0; i
< preds
.size(); i
++) {
1053 phi
->operands
[i
] = Operand(ops
[i
]);
1054 phi
->operands
[i
].setFixed(ctx
.assignments
[ops
[i
].id()].first
);
1055 if (ops
[i
].regClass() == new_val
.regClass())
1056 affinities
[new_val
.id()] = ops
[i
].id();
1058 phi_map
.emplace(new_val
.id(), phi_info
{phi
.get(), block
->index
});
1059 block
->instructions
.insert(block
->instructions
.begin(), std::move(phi
));
1063 renames
[block
->index
][val
.id()] = new_val
;
1064 renames
[block
->index
][new_val
.id()] = new_val
;
1065 ctx
.orig_names
[new_val
.id()] = val
;
1069 try_remove_trivial_phi
= [&] (std::map
<unsigned, phi_info
>::iterator info
) -> Temp
{
1070 assert(info
->second
.block_idx
!= 0);
1071 Instruction
* phi
= info
->second
.phi
;
1074 Definition def
= phi
->definitions
[0];
1075 /* a phi node is trivial if all operands are the same as the definition of the phi */
1076 for (const Operand
& op
: phi
->operands
) {
1077 const Temp t
= op
.getTemp();
1078 if (t
== same
|| t
== def
.getTemp())
1080 if (!(same
== Temp()) || !(op
.physReg() == def
.physReg())) {
1081 /* phi is not trivial */
1082 return def
.getTemp();
1086 assert(!(same
== Temp() || same
== def
.getTemp()));
1088 /* reroute all uses to same and remove phi */
1089 std::vector
<std::map
<unsigned, phi_info
>::iterator
> phi_users
;
1090 std::map
<unsigned, phi_info
>::iterator same_phi_info
= phi_map
.find(same
.id());
1091 for (Instruction
* instr
: info
->second
.uses
) {
1092 assert(phi
!= instr
);
1093 /* recursively try to remove trivial phis */
1094 if (is_phi(instr
)) {
1095 /* ignore if the phi was already flagged trivial */
1096 if (instr
->definitions
.empty())
1099 std::map
<unsigned, phi_info
>::iterator it
= phi_map
.find(instr
->definitions
[0].tempId());
1100 if (it
!= phi_map
.end() && it
!= info
)
1101 phi_users
.emplace_back(it
);
1103 for (Operand
& op
: instr
->operands
) {
1104 if (op
.isTemp() && op
.tempId() == def
.tempId()) {
1106 if (same_phi_info
!= phi_map
.end())
1107 same_phi_info
->second
.uses
.emplace(instr
);
1112 auto it
= ctx
.orig_names
.find(same
.id());
1113 unsigned orig_var
= it
!= ctx
.orig_names
.end() ? it
->second
.id() : same
.id();
1114 for (unsigned i
= 0; i
< program
->blocks
.size(); i
++) {
1115 auto it
= renames
[i
].find(orig_var
);
1116 if (it
!= renames
[i
].end() && it
->second
== def
.getTemp())
1117 renames
[i
][orig_var
] = same
;
1120 unsigned block_idx
= info
->second
.block_idx
;
1121 phi
->definitions
.clear(); /* this indicates that the phi can be removed */
1122 phi_map
.erase(info
);
1123 for (auto it
: phi_users
) {
1124 if (sealed
[it
->second
.block_idx
])
1125 try_remove_trivial_phi(it
);
1128 /* due to the removal of other phis, the name might have changed once again! */
1129 return renames
[block_idx
][orig_var
];
1132 std::map
<unsigned, Instruction
*> vectors
;
1133 std::vector
<std::vector
<Temp
>> phi_ressources
;
1134 std::map
<unsigned, unsigned> temp_to_phi_ressources
;
1136 for (std::vector
<Block
>::reverse_iterator it
= program
->blocks
.rbegin(); it
!= program
->blocks
.rend(); it
++) {
1139 /* first, compute the death points of all live vars within the block */
1140 std::set
<Temp
>& live
= live_out_per_block
[block
.index
];
1142 std::vector
<aco_ptr
<Instruction
>>::reverse_iterator rit
;
1143 for (rit
= block
.instructions
.rbegin(); rit
!= block
.instructions
.rend(); ++rit
) {
1144 aco_ptr
<Instruction
>& instr
= *rit
;
1145 if (is_phi(instr
)) {
1146 live
.erase(instr
->definitions
[0].getTemp());
1147 if (instr
->definitions
[0].isKill() || instr
->definitions
[0].isFixed())
1149 /* collect information about affinity-related temporaries */
1150 std::vector
<Temp
> affinity_related
;
1151 /* affinity_related[0] is the last seen affinity-related temp */
1152 affinity_related
.emplace_back(instr
->definitions
[0].getTemp());
1153 affinity_related
.emplace_back(instr
->definitions
[0].getTemp());
1154 for (const Operand
& op
: instr
->operands
) {
1155 if (op
.isTemp() && op
.regClass() == instr
->definitions
[0].regClass()) {
1156 affinity_related
.emplace_back(op
.getTemp());
1157 temp_to_phi_ressources
[op
.tempId()] = phi_ressources
.size();
1160 phi_ressources
.emplace_back(std::move(affinity_related
));
1164 /* add vector affinities */
1165 if (instr
->opcode
== aco_opcode::p_create_vector
) {
1166 for (const Operand
& op
: instr
->operands
) {
1167 if (op
.isTemp() && op
.getTemp().type() == instr
->definitions
[0].getTemp().type())
1168 vectors
[op
.tempId()] = instr
.get();
1172 /* add operands to live variables */
1173 for (const Operand
& op
: instr
->operands
) {
1175 live
.emplace(op
.getTemp());
1178 /* erase definitions from live */
1179 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
1180 const Definition
& def
= instr
->definitions
[i
];
1183 live
.erase(def
.getTemp());
1184 /* mark last-seen phi operand */
1185 std::map
<unsigned, unsigned>::iterator it
= temp_to_phi_ressources
.find(def
.tempId());
1186 if (it
!= temp_to_phi_ressources
.end() && def
.regClass() == phi_ressources
[it
->second
][0].regClass()) {
1187 phi_ressources
[it
->second
][0] = def
.getTemp();
1188 /* try to coalesce phi affinities with parallelcopies */
1189 if (!def
.isFixed() && instr
->opcode
== aco_opcode::p_parallelcopy
) {
1190 Operand op
= instr
->operands
[i
];
1191 if (op
.isTemp() && op
.isFirstKillBeforeDef() && def
.regClass() == op
.regClass()) {
1192 phi_ressources
[it
->second
].emplace_back(op
.getTemp());
1193 temp_to_phi_ressources
[op
.tempId()] = it
->second
;
1200 /* create affinities */
1201 for (std::vector
<Temp
>& vec
: phi_ressources
) {
1202 assert(vec
.size() > 1);
1203 for (unsigned i
= 1; i
< vec
.size(); i
++)
1204 if (vec
[i
].id() != vec
[0].id())
1205 affinities
[vec
[i
].id()] = vec
[0].id();
1208 /* state of register file after phis */
1209 std::vector
<std::bitset
<128>> sgpr_live_in(program
->blocks
.size());
1211 for (Block
& block
: program
->blocks
) {
1212 std::set
<Temp
>& live
= live_out_per_block
[block
.index
];
1213 /* initialize register file */
1214 assert(block
.index
!= 0 || live
.empty());
1215 RegisterFile register_file
;
1216 ctx
.war_hint
.reset();
1218 for (Temp t
: live
) {
1219 Temp renamed
= handle_live_in(t
, &block
);
1220 if (ctx
.assignments
.find(renamed
.id()) != ctx
.assignments
.end())
1221 register_file
.fill(ctx
.assignments
[renamed
.id()].first
, t
.size(), renamed
.id());
1224 std::vector
<aco_ptr
<Instruction
>> instructions
;
1225 std::vector
<aco_ptr
<Instruction
>>::iterator it
;
1227 /* this is a slight adjustment from the paper as we already have phi nodes:
1228 * We consider them incomplete phis and only handle the definition. */
1230 /* handle fixed phi definitions */
1231 for (it
= block
.instructions
.begin(); it
!= block
.instructions
.end(); ++it
) {
1232 aco_ptr
<Instruction
>& phi
= *it
;
1235 Definition
& definition
= phi
->definitions
[0];
1236 if (!definition
.isFixed())
1239 /* check if a dead exec mask phi is needed */
1240 if (definition
.isKill()) {
1241 for (Operand
& op
: phi
->operands
) {
1242 assert(op
.isTemp());
1243 if (ctx
.assignments
.find(op
.tempId()) == ctx
.assignments
.end() ||
1244 ctx
.assignments
[op
.tempId()].first
!= exec
) {
1245 definition
.setKill(false);
1251 if (definition
.isKill())
1254 assert(definition
.physReg() == exec
);
1255 assert(!register_file
.test(definition
.physReg(), definition
.size()));
1256 register_file
.fill(definition
);
1257 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1260 /* look up the affinities */
1261 for (it
= block
.instructions
.begin(); it
!= block
.instructions
.end(); ++it
) {
1262 aco_ptr
<Instruction
>& phi
= *it
;
1265 Definition
& definition
= phi
->definitions
[0];
1266 if (definition
.isKill() || definition
.isFixed())
1269 if (affinities
.find(definition
.tempId()) != affinities
.end() &&
1270 ctx
.assignments
.find(affinities
[definition
.tempId()]) != ctx
.assignments
.end()) {
1271 assert(ctx
.assignments
[affinities
[definition
.tempId()]].second
== definition
.regClass());
1272 PhysReg reg
= ctx
.assignments
[affinities
[definition
.tempId()]].first
;
1273 bool try_use_special_reg
= reg
== scc
|| reg
== exec
;
1274 if (try_use_special_reg
) {
1275 for (const Operand
& op
: phi
->operands
) {
1277 ctx
.assignments
.find(op
.tempId()) == ctx
.assignments
.end() ||
1278 !(ctx
.assignments
[op
.tempId()].first
== reg
)) {
1279 try_use_special_reg
= false;
1283 if (!try_use_special_reg
)
1286 /* only assign if register is still free */
1287 if (!register_file
.test(reg
, definition
.size())) {
1288 definition
.setFixed(reg
);
1289 register_file
.fill(definition
);
1290 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1295 /* find registers for phis without affinity or where the register was blocked */
1296 for (it
= block
.instructions
.begin();it
!= block
.instructions
.end(); ++it
) {
1297 aco_ptr
<Instruction
>& phi
= *it
;
1301 Definition
& definition
= phi
->definitions
[0];
1302 if (definition
.isKill())
1305 renames
[block
.index
][definition
.tempId()] = definition
.getTemp();
1307 if (!definition
.isFixed()) {
1308 std::vector
<std::pair
<Operand
, Definition
>> parallelcopy
;
1309 /* try to find a register that is used by at least one operand */
1310 for (const Operand
& op
: phi
->operands
) {
1312 ctx
.assignments
.find(op
.tempId()) == ctx
.assignments
.end())
1314 PhysReg reg
= ctx
.assignments
[op
.tempId()].first
;
1315 /* we tried this already on the previous loop */
1316 if (reg
== scc
|| reg
== exec
)
1318 if (get_reg_specified(ctx
, register_file
, definition
.regClass(), parallelcopy
, phi
, reg
)) {
1319 definition
.setFixed(reg
);
1323 if (!definition
.isFixed())
1324 definition
.setFixed(get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, phi
));
1326 /* process parallelcopy */
1327 for (std::pair
<Operand
, Definition
> pc
: parallelcopy
) {
1328 /* see if it's a copy from a different phi */
1329 //TODO: prefer moving some previous phis over live-ins
1330 //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)
1331 Instruction
*prev_phi
= NULL
;
1332 std::vector
<aco_ptr
<Instruction
>>::iterator phi_it
;
1333 for (phi_it
= instructions
.begin(); phi_it
!= instructions
.end(); ++phi_it
) {
1334 if ((*phi_it
)->definitions
[0].tempId() == pc
.first
.tempId())
1335 prev_phi
= phi_it
->get();
1338 while (!prev_phi
&& is_phi(*++phi_it
)) {
1339 if ((*phi_it
)->definitions
[0].tempId() == pc
.first
.tempId())
1340 prev_phi
= phi_it
->get();
1343 /* if so, just update that phi's register */
1344 prev_phi
->definitions
[0].setFixed(pc
.second
.physReg());
1345 ctx
.assignments
[prev_phi
->definitions
[0].tempId()] = {pc
.second
.physReg(), pc
.second
.regClass()};
1346 register_file
.fill(pc
.second
.physReg(), pc
.second
.size(), prev_phi
->definitions
[0].tempId());
1351 std::map
<unsigned, Temp
>::iterator orig_it
= ctx
.orig_names
.find(pc
.first
.tempId());
1352 Temp orig
= pc
.first
.getTemp();
1353 if (orig_it
!= ctx
.orig_names
.end())
1354 orig
= orig_it
->second
;
1356 ctx
.orig_names
[pc
.second
.tempId()] = orig
;
1357 renames
[block
.index
][orig
.id()] = pc
.second
.getTemp();
1358 renames
[block
.index
][pc
.second
.tempId()] = pc
.second
.getTemp();
1360 /* otherwise, this is a live-in and we need to create a new phi
1361 * to move it in this block's predecessors */
1362 aco_opcode opcode
= pc
.first
.getTemp().is_linear() ? aco_opcode::p_linear_phi
: aco_opcode::p_phi
;
1363 std::vector
<unsigned>& preds
= pc
.first
.getTemp().is_linear() ? block
.linear_preds
: block
.logical_preds
;
1364 aco_ptr
<Instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, preds
.size(), 1)};
1365 new_phi
->definitions
[0] = pc
.second
;
1366 for (unsigned i
= 0; i
< preds
.size(); i
++)
1367 new_phi
->operands
[i
] = Operand(pc
.first
);
1368 instructions
.emplace_back(std::move(new_phi
));
1371 register_file
.fill(definition
);
1372 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1374 live
.emplace(definition
.getTemp());
1376 /* update phi affinities */
1377 for (const Operand
& op
: phi
->operands
) {
1378 if (op
.isTemp() && op
.regClass() == phi
->definitions
[0].regClass())
1379 affinities
[op
.tempId()] = definition
.tempId();
1382 instructions
.emplace_back(std::move(*it
));
1385 /* fill in sgpr_live_in */
1386 for (unsigned i
= 0; i
<= ctx
.max_used_sgpr
; i
++)
1387 sgpr_live_in
[block
.index
][i
] = register_file
[i
];
1388 sgpr_live_in
[block
.index
][127] = register_file
[scc
.reg()];
1390 /* Handle all other instructions of the block */
1391 for (; it
!= block
.instructions
.end(); ++it
) {
1392 aco_ptr
<Instruction
>& instr
= *it
;
1394 /* parallelcopies from p_phi are inserted here which means
1395 * live ranges of killed operands end here as well */
1396 if (instr
->opcode
== aco_opcode::p_logical_end
) {
1397 /* no need to process this instruction any further */
1398 if (block
.logical_succs
.size() != 1) {
1399 instructions
.emplace_back(std::move(instr
));
1403 Block
& succ
= program
->blocks
[block
.logical_succs
[0]];
1405 for (; idx
< succ
.logical_preds
.size(); idx
++) {
1406 if (succ
.logical_preds
[idx
] == block
.index
)
1409 for (aco_ptr
<Instruction
>& phi
: succ
.instructions
) {
1410 if (phi
->opcode
== aco_opcode::p_phi
) {
1411 if (phi
->operands
[idx
].isTemp() &&
1412 phi
->operands
[idx
].getTemp().type() == RegType::sgpr
&&
1413 phi
->operands
[idx
].isFirstKillBeforeDef()) {
1414 Temp phi_op
= read_variable(phi
->operands
[idx
].getTemp(), block
.index
);
1415 PhysReg reg
= ctx
.assignments
[phi_op
.id()].first
;
1416 assert(register_file
[reg
] == phi_op
.id());
1417 register_file
[reg
] = 0;
1419 } else if (phi
->opcode
!= aco_opcode::p_linear_phi
) {
1423 instructions
.emplace_back(std::move(instr
));
1427 std::vector
<std::pair
<Operand
, Definition
>> parallelcopy
;
1429 assert(!is_phi(instr
));
1431 /* handle operands */
1432 for (unsigned i
= 0; i
< instr
->operands
.size(); ++i
) {
1433 auto& operand
= instr
->operands
[i
];
1434 if (!operand
.isTemp())
1437 /* rename operands */
1438 operand
.setTemp(read_variable(operand
.getTemp(), block
.index
));
1440 /* check if the operand is fixed */
1441 if (operand
.isFixed()) {
1443 if (operand
.physReg() == ctx
.assignments
[operand
.tempId()].first
) {
1444 /* we are fine: the operand is already assigned the correct reg */
1447 /* check if target reg is blocked, and move away the blocking var */
1448 if (register_file
[operand
.physReg().reg()]) {
1449 uint32_t blocking_id
= register_file
[operand
.physReg().reg()];
1450 RegClass rc
= ctx
.assignments
[blocking_id
].second
;
1451 Operand pc_op
= Operand(Temp
{blocking_id
, rc
});
1452 pc_op
.setFixed(operand
.physReg());
1453 Definition pc_def
= Definition(Temp
{program
->allocateId(), pc_op
.regClass()});
1455 PhysReg reg
= get_reg(ctx
, register_file
, pc_op
.regClass(), parallelcopy
, instr
);
1456 pc_def
.setFixed(reg
);
1457 ctx
.assignments
[pc_def
.tempId()] = {reg
, pc_def
.regClass()};
1458 register_file
.clear(pc_op
);
1459 register_file
.fill(pc_def
);
1460 parallelcopy
.emplace_back(pc_op
, pc_def
);
1462 /* handle renames of previous operands */
1463 for (unsigned j
= 0; j
< i
; j
++) {
1464 Operand
& op
= instr
->operands
[j
];
1465 if (op
.isTemp() && op
.tempId() == blocking_id
) {
1466 op
.setTemp(pc_def
.getTemp());
1471 /* move operand to fixed reg and create parallelcopy pair */
1472 Operand pc_op
= operand
;
1473 Temp tmp
= Temp
{program
->allocateId(), operand
.regClass()};
1474 Definition pc_def
= Definition(tmp
);
1475 pc_def
.setFixed(operand
.physReg());
1476 pc_op
.setFixed(ctx
.assignments
[operand
.tempId()].first
);
1477 operand
.setTemp(tmp
);
1478 ctx
.assignments
[tmp
.id()] = {pc_def
.physReg(), pc_def
.regClass()};
1479 operand
.setFixed(pc_def
.physReg());
1480 register_file
.clear(pc_op
);
1481 register_file
.fill(pc_def
);
1482 parallelcopy
.emplace_back(pc_op
, pc_def
);
1485 assert(ctx
.assignments
.find(operand
.tempId()) != ctx
.assignments
.end());
1486 PhysReg reg
= ctx
.assignments
[operand
.tempId()].first
;
1488 if (operand_can_use_reg(instr
, i
, reg
)) {
1489 operand
.setFixed(ctx
.assignments
[operand
.tempId()].first
);
1491 Operand pc_op
= operand
;
1492 pc_op
.setFixed(reg
);
1493 PhysReg new_reg
= get_reg(ctx
, register_file
, operand
.regClass(), parallelcopy
, instr
);
1494 Definition pc_def
= Definition(program
->allocateId(), new_reg
, pc_op
.regClass());
1495 ctx
.assignments
[pc_def
.tempId()] = {new_reg
, pc_def
.regClass()};
1496 register_file
.clear(pc_op
);
1497 register_file
.fill(pc_def
);
1498 parallelcopy
.emplace_back(pc_op
, pc_def
);
1499 operand
.setTemp(pc_def
.getTemp());
1500 operand
.setFixed(new_reg
);
1503 if (instr
->format
== Format::EXP
||
1504 (instr
->isVMEM() && i
== 3 && program
->chip_class
== GFX6
) ||
1505 (instr
->format
== Format::DS
&& static_cast<DS_instruction
*>(instr
.get())->gds
)) {
1506 for (unsigned j
= 0; j
< operand
.size(); j
++)
1507 ctx
.war_hint
.set(operand
.physReg().reg() + j
);
1510 std::map
<unsigned, phi_info
>::iterator phi
= phi_map
.find(operand
.getTemp().id());
1511 if (phi
!= phi_map
.end())
1512 phi
->second
.uses
.emplace(instr
.get());
1515 /* remove dead vars from register file */
1516 for (const Operand
& op
: instr
->operands
) {
1517 if (op
.isTemp() && op
.isFirstKillBeforeDef())
1518 register_file
.clear(op
);
1521 /* try to optimize v_mad_f32 -> v_mac_f32 */
1522 if (instr
->opcode
== aco_opcode::v_mad_f32
&&
1523 instr
->operands
[2].isTemp() &&
1524 instr
->operands
[2].isKillBeforeDef() &&
1525 instr
->operands
[2].getTemp().type() == RegType::vgpr
&&
1526 instr
->operands
[1].isTemp() &&
1527 instr
->operands
[1].getTemp().type() == RegType::vgpr
) { /* TODO: swap src0 and src1 in this case */
1528 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(instr
.get());
1529 bool can_use_mac
= !(vop3
->abs
[0] || vop3
->abs
[1] || vop3
->abs
[2] ||
1530 vop3
->neg
[0] || vop3
->neg
[1] || vop3
->neg
[2] ||
1531 vop3
->clamp
|| vop3
->omod
|| vop3
->opsel
);
1533 instr
->format
= Format::VOP2
;
1534 instr
->opcode
= aco_opcode::v_mac_f32
;
1538 /* handle definitions which must have the same register as an operand */
1539 if (instr
->opcode
== aco_opcode::v_interp_p2_f32
||
1540 instr
->opcode
== aco_opcode::v_mac_f32
||
1541 instr
->opcode
== aco_opcode::v_writelane_b32
||
1542 instr
->opcode
== aco_opcode::v_writelane_b32_e64
) {
1543 instr
->definitions
[0].setFixed(instr
->operands
[2].physReg());
1544 } else if (instr
->opcode
== aco_opcode::s_addk_i32
||
1545 instr
->opcode
== aco_opcode::s_mulk_i32
) {
1546 instr
->definitions
[0].setFixed(instr
->operands
[0].physReg());
1547 } else if (instr
->format
== Format::MUBUF
&&
1548 instr
->definitions
.size() == 1 &&
1549 instr
->operands
.size() == 4) {
1550 instr
->definitions
[0].setFixed(instr
->operands
[3].physReg());
1551 } else if (instr
->format
== Format::MIMG
&&
1552 instr
->definitions
.size() == 1 &&
1553 instr
->operands
[1].regClass().type() == RegType::vgpr
) {
1554 instr
->definitions
[0].setFixed(instr
->operands
[1].physReg());
1557 ctx
.defs_done
.reset();
1559 /* handle fixed definitions first */
1560 for (unsigned i
= 0; i
< instr
->definitions
.size(); ++i
) {
1561 auto& definition
= instr
->definitions
[i
];
1562 if (!definition
.isFixed())
1565 adjust_max_used_regs(ctx
, definition
.regClass(), definition
.physReg());
1566 /* check if the target register is blocked */
1567 if (register_file
[definition
.physReg().reg()] != 0) {
1568 /* create parallelcopy pair to move blocking var */
1569 Temp tmp
= {register_file
[definition
.physReg()], ctx
.assignments
[register_file
[definition
.physReg()]].second
};
1570 Operand pc_op
= Operand(tmp
);
1571 pc_op
.setFixed(ctx
.assignments
[register_file
[definition
.physReg().reg()]].first
);
1572 RegClass rc
= pc_op
.regClass();
1573 tmp
= Temp
{program
->allocateId(), rc
};
1574 Definition pc_def
= Definition(tmp
);
1576 /* re-enable the killed operands, so that we don't move the blocking var there */
1577 for (const Operand
& op
: instr
->operands
) {
1578 if (op
.isTemp() && op
.isFirstKillBeforeDef())
1579 register_file
.fill(op
.physReg(), op
.size(), 0xFFFF);
1582 /* find a new register for the blocking variable */
1583 PhysReg reg
= get_reg(ctx
, register_file
, rc
, parallelcopy
, instr
);
1584 /* once again, disable killed operands */
1585 for (const Operand
& op
: instr
->operands
) {
1586 if (op
.isTemp() && op
.isFirstKillBeforeDef())
1587 register_file
.clear(op
);
1589 for (unsigned k
= 0; k
< i
; k
++) {
1590 if (instr
->definitions
[k
].isTemp() && ctx
.defs_done
.test(k
) && !instr
->definitions
[k
].isKill())
1591 register_file
.fill(instr
->definitions
[k
]);
1593 pc_def
.setFixed(reg
);
1595 /* finish assignment of parallelcopy */
1596 ctx
.assignments
[pc_def
.tempId()] = {reg
, pc_def
.regClass()};
1597 parallelcopy
.emplace_back(pc_op
, pc_def
);
1599 /* add changes to reg_file */
1600 register_file
.clear(pc_op
);
1601 register_file
.fill(pc_def
);
1603 ctx
.defs_done
.set(i
);
1605 if (!definition
.isTemp())
1608 /* set live if it has a kill point */
1609 if (!definition
.isKill())
1610 live
.emplace(definition
.getTemp());
1612 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1613 renames
[block
.index
][definition
.tempId()] = definition
.getTemp();
1614 register_file
.fill(definition
);
1617 /* handle all other definitions */
1618 for (unsigned i
= 0; i
< instr
->definitions
.size(); ++i
) {
1619 auto& definition
= instr
->definitions
[i
];
1621 if (definition
.isFixed() || !definition
.isTemp())
1625 if (definition
.hasHint() && register_file
[definition
.physReg().reg()] == 0)
1626 definition
.setFixed(definition
.physReg());
1627 else if (instr
->opcode
== aco_opcode::p_split_vector
) {
1628 PhysReg reg
= PhysReg
{instr
->operands
[0].physReg() + i
* definition
.size()};
1629 if (!get_reg_specified(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
, reg
))
1630 reg
= get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
);
1631 definition
.setFixed(reg
);
1632 } else if (instr
->opcode
== aco_opcode::p_wqm
) {
1634 if (instr
->operands
[0].isKillBeforeDef() && instr
->operands
[0].getTemp().type() == definition
.getTemp().type()) {
1635 reg
= instr
->operands
[0].physReg();
1636 assert(register_file
[reg
.reg()] == 0);
1638 reg
= get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
);
1640 definition
.setFixed(reg
);
1641 } else if (instr
->opcode
== aco_opcode::p_extract_vector
) {
1643 if (instr
->operands
[0].isKillBeforeDef() &&
1644 instr
->operands
[0].getTemp().type() == definition
.getTemp().type()) {
1645 reg
= instr
->operands
[0].physReg();
1646 reg
= PhysReg(reg
.reg() + definition
.size() * instr
->operands
[1].constantValue());
1647 assert(register_file
[reg
.reg()] == 0);
1649 reg
= get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
);
1651 definition
.setFixed(reg
);
1652 } else if (instr
->opcode
== aco_opcode::p_create_vector
) {
1653 PhysReg reg
= get_reg_create_vector(ctx
, register_file
, definition
.regClass(),
1654 parallelcopy
, instr
);
1655 definition
.setFixed(reg
);
1656 } else if (affinities
.find(definition
.tempId()) != affinities
.end() &&
1657 ctx
.assignments
.find(affinities
[definition
.tempId()]) != ctx
.assignments
.end()) {
1658 PhysReg reg
= ctx
.assignments
[affinities
[definition
.tempId()]].first
;
1659 if (get_reg_specified(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
, reg
))
1660 definition
.setFixed(reg
);
1662 definition
.setFixed(get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
));
1664 } else if (vectors
.find(definition
.tempId()) != vectors
.end()) {
1665 Instruction
* vec
= vectors
[definition
.tempId()];
1666 unsigned offset
= 0;
1667 for (const Operand
& op
: vec
->operands
) {
1668 if (op
.isTemp() && op
.tempId() == definition
.tempId())
1671 offset
+= op
.size();
1674 for (const Operand
& op
: vec
->operands
) {
1676 op
.tempId() != definition
.tempId() &&
1677 op
.getTemp().type() == definition
.getTemp().type() &&
1678 ctx
.assignments
.find(op
.tempId()) != ctx
.assignments
.end()) {
1679 PhysReg reg
= ctx
.assignments
[op
.tempId()].first
;
1680 reg
= PhysReg(reg
.reg() - k
+ offset
);
1681 if (get_reg_specified(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
, reg
)) {
1682 definition
.setFixed(reg
);
1688 if (!definition
.isFixed()) {
1689 std::pair
<PhysReg
, bool> res
= get_reg_vec(ctx
, register_file
, vec
->definitions
[0].regClass());
1690 PhysReg reg
= res
.first
;
1692 reg
= PhysReg(reg
.reg() + offset
);
1694 reg
= get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
);
1696 definition
.setFixed(reg
);
1699 definition
.setFixed(get_reg(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
));
1701 assert(definition
.isFixed() && ((definition
.getTemp().type() == RegType::vgpr
&& definition
.physReg() >= 256) ||
1702 (definition
.getTemp().type() != RegType::vgpr
&& definition
.physReg() < 256)));
1703 ctx
.defs_done
.set(i
);
1705 /* set live if it has a kill point */
1706 if (!definition
.isKill())
1707 live
.emplace(definition
.getTemp());
1709 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1710 renames
[block
.index
][definition
.tempId()] = definition
.getTemp();
1711 register_file
.fill(definition
);
1714 handle_pseudo(ctx
, register_file
, instr
.get());
1716 /* kill definitions and late-kill operands */
1717 for (const Definition
& def
: instr
->definitions
) {
1718 if (def
.isTemp() && def
.isKill())
1719 register_file
.clear(def
);
1721 for (const Operand
& op
: instr
->operands
) {
1722 if (op
.isTemp() && op
.isFirstKill() && op
.isLateKill())
1723 register_file
.clear(op
);
1726 /* emit parallelcopy */
1727 if (!parallelcopy
.empty()) {
1728 aco_ptr
<Pseudo_instruction
> pc
;
1729 pc
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_parallelcopy
, Format::PSEUDO
, parallelcopy
.size(), parallelcopy
.size()));
1730 bool temp_in_scc
= register_file
[scc
.reg()];
1731 bool sgpr_operands_alias_defs
= false;
1732 uint64_t sgpr_operands
[4] = {0, 0, 0, 0};
1733 for (unsigned i
= 0; i
< parallelcopy
.size(); i
++) {
1734 if (temp_in_scc
&& parallelcopy
[i
].first
.isTemp() && parallelcopy
[i
].first
.getTemp().type() == RegType::sgpr
) {
1735 if (!sgpr_operands_alias_defs
) {
1736 unsigned reg
= parallelcopy
[i
].first
.physReg().reg();
1737 unsigned size
= parallelcopy
[i
].first
.getTemp().size();
1738 sgpr_operands
[reg
/ 64u] |= ((1u << size
) - 1) << (reg
% 64u);
1740 reg
= parallelcopy
[i
].second
.physReg().reg();
1741 size
= parallelcopy
[i
].second
.getTemp().size();
1742 if (sgpr_operands
[reg
/ 64u] & ((1u << size
) - 1) << (reg
% 64u))
1743 sgpr_operands_alias_defs
= true;
1747 pc
->operands
[i
] = parallelcopy
[i
].first
;
1748 pc
->definitions
[i
] = parallelcopy
[i
].second
;
1749 assert(pc
->operands
[i
].size() == pc
->definitions
[i
].size());
1751 /* it might happen that the operand is already renamed. we have to restore the original name. */
1752 std::map
<unsigned, Temp
>::iterator it
= ctx
.orig_names
.find(pc
->operands
[i
].tempId());
1753 Temp orig
= it
!= ctx
.orig_names
.end() ? it
->second
: pc
->operands
[i
].getTemp();
1754 ctx
.orig_names
[pc
->definitions
[i
].tempId()] = orig
;
1755 renames
[block
.index
][orig
.id()] = pc
->definitions
[i
].getTemp();
1756 renames
[block
.index
][pc
->definitions
[i
].tempId()] = pc
->definitions
[i
].getTemp();
1758 std::map
<unsigned, phi_info
>::iterator phi
= phi_map
.find(pc
->operands
[i
].tempId());
1759 if (phi
!= phi_map
.end())
1760 phi
->second
.uses
.emplace(pc
.get());
1763 if (temp_in_scc
&& sgpr_operands_alias_defs
) {
1764 /* disable definitions and re-enable operands */
1765 for (const Definition
& def
: instr
->definitions
) {
1766 if (def
.isTemp() && !def
.isKill())
1767 register_file
.clear(def
);
1769 for (const Operand
& op
: instr
->operands
) {
1770 if (op
.isTemp() && op
.isFirstKill())
1771 register_file
.fill(op
.physReg(), op
.size(), 0xFFFF);
1774 handle_pseudo(ctx
, register_file
, pc
.get());
1776 /* re-enable live vars */
1777 for (const Operand
& op
: instr
->operands
) {
1778 if (op
.isTemp() && op
.isFirstKill())
1779 register_file
.clear(op
);
1781 for (const Definition
& def
: instr
->definitions
) {
1782 if (def
.isTemp() && !def
.isKill())
1783 register_file
.fill(def
);
1786 pc
->tmp_in_scc
= false;
1789 instructions
.emplace_back(std::move(pc
));
1792 /* some instructions need VOP3 encoding if operand/definition is not assigned to VCC */
1793 bool instr_needs_vop3
= !instr
->isVOP3() &&
1794 ((instr
->format
== Format::VOPC
&& !(instr
->definitions
[0].physReg() == vcc
)) ||
1795 (instr
->opcode
== aco_opcode::v_cndmask_b32
&& !(instr
->operands
[2].physReg() == vcc
)) ||
1796 ((instr
->opcode
== aco_opcode::v_add_co_u32
||
1797 instr
->opcode
== aco_opcode::v_addc_co_u32
||
1798 instr
->opcode
== aco_opcode::v_sub_co_u32
||
1799 instr
->opcode
== aco_opcode::v_subb_co_u32
||
1800 instr
->opcode
== aco_opcode::v_subrev_co_u32
||
1801 instr
->opcode
== aco_opcode::v_subbrev_co_u32
) &&
1802 !(instr
->definitions
[1].physReg() == vcc
)) ||
1803 ((instr
->opcode
== aco_opcode::v_addc_co_u32
||
1804 instr
->opcode
== aco_opcode::v_subb_co_u32
||
1805 instr
->opcode
== aco_opcode::v_subbrev_co_u32
) &&
1806 !(instr
->operands
[2].physReg() == vcc
)));
1807 if (instr_needs_vop3
) {
1809 /* if the first operand is a literal, we have to move it to a reg */
1810 if (instr
->operands
.size() && instr
->operands
[0].isLiteral() && program
->chip_class
< GFX10
) {
1811 bool can_sgpr
= true;
1812 /* check, if we have to move to vgpr */
1813 for (const Operand
& op
: instr
->operands
) {
1814 if (op
.isTemp() && op
.getTemp().type() == RegType::sgpr
) {
1819 aco_ptr
<Instruction
> mov
;
1821 mov
.reset(create_instruction
<SOP1_instruction
>(aco_opcode::s_mov_b32
, Format::SOP1
, 1, 1));
1823 mov
.reset(create_instruction
<VOP1_instruction
>(aco_opcode::v_mov_b32
, Format::VOP1
, 1, 1));
1824 mov
->operands
[0] = instr
->operands
[0];
1825 Temp tmp
= {program
->allocateId(), can_sgpr
? s1
: v1
};
1826 mov
->definitions
[0] = Definition(tmp
);
1827 /* disable definitions and re-enable operands */
1828 for (const Definition
& def
: instr
->definitions
)
1829 register_file
.clear(def
);
1830 for (const Operand
& op
: instr
->operands
) {
1831 if (op
.isTemp() && op
.isFirstKill())
1832 register_file
.fill(op
.physReg(), op
.size(), 0xFFFF);
1834 mov
->definitions
[0].setFixed(get_reg(ctx
, register_file
, tmp
.regClass(), parallelcopy
, mov
));
1835 instr
->operands
[0] = Operand(tmp
);
1836 instr
->operands
[0].setFixed(mov
->definitions
[0].physReg());
1837 instructions
.emplace_back(std::move(mov
));
1838 /* re-enable live vars */
1839 for (const Operand
& op
: instr
->operands
) {
1840 if (op
.isTemp() && op
.isFirstKill())
1841 register_file
.clear(op
);
1843 for (const Definition
& def
: instr
->definitions
) {
1844 if (def
.isTemp() && !def
.isKill())
1845 register_file
.fill(def
);
1849 /* change the instruction to VOP3 to enable an arbitrary register pair as dst */
1850 aco_ptr
<Instruction
> tmp
= std::move(instr
);
1851 Format format
= asVOP3(tmp
->format
);
1852 instr
.reset(create_instruction
<VOP3A_instruction
>(tmp
->opcode
, format
, tmp
->operands
.size(), tmp
->definitions
.size()));
1853 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
1854 Operand
& operand
= tmp
->operands
[i
];
1855 instr
->operands
[i
] = operand
;
1856 /* keep phi_map up to date */
1857 if (operand
.isTemp()) {
1858 std::map
<unsigned, phi_info
>::iterator phi
= phi_map
.find(operand
.tempId());
1859 if (phi
!= phi_map
.end()) {
1860 phi
->second
.uses
.erase(tmp
.get());
1861 phi
->second
.uses
.emplace(instr
.get());
1865 std::copy(tmp
->definitions
.begin(), tmp
->definitions
.end(), instr
->definitions
.begin());
1867 instructions
.emplace_back(std::move(*it
));
1869 } /* end for Instr */
1871 block
.instructions
= std::move(instructions
);
1873 filled
[block
.index
] = true;
1874 for (unsigned succ_idx
: block
.linear_succs
) {
1875 Block
& succ
= program
->blocks
[succ_idx
];
1876 /* seal block if all predecessors are filled */
1877 bool all_filled
= true;
1878 for (unsigned pred_idx
: succ
.linear_preds
) {
1879 if (!filled
[pred_idx
]) {
1885 /* finish incomplete phis and check if they became trivial */
1886 for (Instruction
* phi
: incomplete_phis
[succ_idx
]) {
1887 std::vector
<unsigned> preds
= phi
->definitions
[0].getTemp().is_linear() ? succ
.linear_preds
: succ
.logical_preds
;
1888 for (unsigned i
= 0; i
< phi
->operands
.size(); i
++) {
1889 phi
->operands
[i
].setTemp(read_variable(phi
->operands
[i
].getTemp(), preds
[i
]));
1890 phi
->operands
[i
].setFixed(ctx
.assignments
[phi
->operands
[i
].tempId()].first
);
1892 try_remove_trivial_phi(phi_map
.find(phi
->definitions
[0].tempId()));
1894 /* complete the original phi nodes, but no need to check triviality */
1895 for (aco_ptr
<Instruction
>& instr
: succ
.instructions
) {
1898 std::vector
<unsigned> preds
= instr
->opcode
== aco_opcode::p_phi
? succ
.logical_preds
: succ
.linear_preds
;
1900 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
1901 auto& operand
= instr
->operands
[i
];
1902 if (!operand
.isTemp())
1904 operand
.setTemp(read_variable(operand
.getTemp(), preds
[i
]));
1905 operand
.setFixed(ctx
.assignments
[operand
.tempId()].first
);
1906 std::map
<unsigned, phi_info
>::iterator phi
= phi_map
.find(operand
.getTemp().id());
1907 if (phi
!= phi_map
.end())
1908 phi
->second
.uses
.emplace(instr
.get());
1911 sealed
[succ_idx
] = true;
1916 /* remove trivial phis */
1917 for (Block
& block
: program
->blocks
) {
1918 auto end
= std::find_if(block
.instructions
.begin(), block
.instructions
.end(),
1919 [](aco_ptr
<Instruction
>& instr
) { return !is_phi(instr
);});
1920 auto middle
= std::remove_if(block
.instructions
.begin(), end
,
1921 [](const aco_ptr
<Instruction
>& instr
) { return instr
->definitions
.empty();});
1922 block
.instructions
.erase(middle
, end
);
1925 /* find scc spill registers which may be needed for parallelcopies created by phis */
1926 for (Block
& block
: program
->blocks
) {
1927 if (block
.linear_preds
.size() <= 1)
1930 std::bitset
<128> regs
= sgpr_live_in
[block
.index
];
1934 /* choose a register */
1936 for (; reg
< ctx
.program
->max_reg_demand
.sgpr
&& regs
[reg
]; reg
++)
1938 assert(reg
< ctx
.program
->max_reg_demand
.sgpr
);
1939 adjust_max_used_regs(ctx
, s1
, reg
);
1941 /* update predecessors */
1942 for (unsigned& pred_index
: block
.linear_preds
) {
1943 Block
& pred
= program
->blocks
[pred_index
];
1944 pred
.scc_live_out
= true;
1945 pred
.scratch_sgpr
= PhysReg
{(uint16_t)reg
};
1949 /* num_gpr = rnd_up(max_used_gpr + 1) */
1950 program
->config
->num_vgprs
= align(ctx
.max_used_vgpr
+ 1, 4);
1951 if (program
->family
== CHIP_TONGA
|| program
->family
== CHIP_ICELAND
) /* workaround hardware bug */
1952 program
->config
->num_sgprs
= get_sgpr_alloc(program
, program
->sgpr_limit
);
1954 program
->config
->num_sgprs
= align(ctx
.max_used_sgpr
+ 1 + get_extra_sgprs(program
), 8);