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>
36 #include "util/u_math.h"
45 assignment() = default;
46 assignment(PhysReg reg
, RegClass rc
) : reg(reg
), rc(rc
), assigned(-1) {}
52 std::set
<Instruction
*> uses
;
56 std::bitset
<512> war_hint
;
58 std::vector
<assignment
> assignments
;
59 std::vector
<std::unordered_map
<unsigned, Temp
>> renames
;
60 std::vector
<std::vector
<Instruction
*>> incomplete_phis
;
61 std::vector
<bool> filled
;
62 std::vector
<bool> sealed
;
63 std::unordered_map
<unsigned, Temp
> orig_names
;
64 std::unordered_map
<unsigned, phi_info
> phi_map
;
65 std::unordered_map
<unsigned, unsigned> affinities
;
66 std::unordered_map
<unsigned, Instruction
*> vectors
;
67 unsigned max_used_sgpr
= 0;
68 unsigned max_used_vgpr
= 0;
69 std::bitset
<64> defs_done
; /* see MAX_ARGS in aco_instruction_selection_setup.cpp */
71 ra_ctx(Program
* program
) : program(program
),
72 assignments(program
->peekAllocationId()),
73 renames(program
->blocks
.size()),
74 incomplete_phis(program
->blocks
.size()),
75 filled(program
->blocks
.size()),
76 sealed(program
->blocks
.size()) {}
81 RegisterFile() {regs
.fill(0);}
83 std::array
<uint32_t, 512> regs
;
84 std::map
<uint32_t, std::array
<uint32_t, 4>> subdword_regs
;
86 const uint32_t& operator [] (unsigned index
) const {
90 uint32_t& operator [] (unsigned index
) {
94 unsigned count_zero(PhysReg start
, unsigned size
) {
96 for (unsigned i
= 0; i
< size
; i
++)
97 res
+= !regs
[start
+ i
];
101 bool test(PhysReg start
, unsigned num_bytes
) {
102 for (PhysReg i
= start
; i
.reg_b
< start
.reg_b
+ num_bytes
; i
= PhysReg(i
+ 1)) {
103 if (regs
[i
] & 0x0FFFFFFF)
105 if (regs
[i
] == 0xF0000000) {
106 assert(subdword_regs
.find(i
) != subdword_regs
.end());
107 for (unsigned j
= i
.byte(); i
* 4 + j
< start
.reg_b
+ num_bytes
&& j
< 4; j
++) {
108 if (subdword_regs
[i
][j
])
116 void block(PhysReg start
, unsigned num_bytes
) {
117 if (start
.byte() || num_bytes
% 4)
118 fill_subdword(start
, num_bytes
, 0xFFFFFFFF);
120 fill(start
, num_bytes
/ 4, 0xFFFFFFFF);
123 bool is_blocked(PhysReg start
) {
124 if (regs
[start
] == 0xFFFFFFFF)
126 if (regs
[start
] == 0xF0000000) {
127 for (unsigned i
= start
.byte(); i
< 4; i
++)
128 if (subdword_regs
[start
][i
] == 0xFFFFFFFF)
134 void clear(PhysReg start
, RegClass rc
) {
135 if (rc
.is_subdword())
136 fill_subdword(start
, rc
.bytes(), 0);
138 fill(start
, rc
.size(), 0);
141 void fill(Operand op
) {
142 if (op
.regClass().is_subdword())
143 fill_subdword(op
.physReg(), op
.bytes(), op
.tempId());
145 fill(op
.physReg(), op
.size(), op
.tempId());
148 void clear(Operand op
) {
149 clear(op
.physReg(), op
.regClass());
152 void fill(Definition def
) {
153 if (def
.regClass().is_subdword())
154 fill_subdword(def
.physReg(), def
.bytes(), def
.tempId());
156 fill(def
.physReg(), def
.size(), def
.tempId());
159 void clear(Definition def
) {
160 clear(def
.physReg(), def
.regClass());
164 void fill(PhysReg start
, unsigned size
, uint32_t val
) {
165 for (unsigned i
= 0; i
< size
; i
++)
166 regs
[start
+ i
] = val
;
169 void fill_subdword(PhysReg start
, unsigned num_bytes
, uint32_t val
) {
170 fill(start
, DIV_ROUND_UP(num_bytes
, 4), 0xF0000000);
171 for (PhysReg i
= start
; i
.reg_b
< start
.reg_b
+ num_bytes
; i
= PhysReg(i
+ 1)) {
173 std::array
<uint32_t, 4>& sub
= subdword_regs
.emplace(i
, std::array
<uint32_t, 4>{0, 0, 0, 0}).first
->second
;
174 for (unsigned j
= i
.byte(); i
* 4 + j
< start
.reg_b
+ num_bytes
&& j
< 4; j
++)
177 if (sub
== std::array
<uint32_t, 4>{0, 0, 0, 0}) {
178 subdword_regs
.erase(i
);
186 /* helper function for debugging */
188 void print_regs(ra_ctx
& ctx
, bool vgprs
, RegisterFile
& reg_file
)
190 unsigned max
= vgprs
? ctx
.program
->max_reg_demand
.vgpr
: ctx
.program
->max_reg_demand
.sgpr
;
191 unsigned lb
= vgprs
? 256 : 0;
192 unsigned ub
= lb
+ max
;
193 char reg_char
= vgprs
? 'v' : 's';
197 for (unsigned i
= lb
; i
< ub
; i
+= 3) {
198 printf("%.2u ", i
- lb
);
203 printf("%cgprs: ", reg_char
);
204 unsigned free_regs
= 0;
206 bool char_select
= false;
207 for (unsigned i
= lb
; i
< ub
; i
++) {
208 if (reg_file
[i
] == 0xFFFF) {
210 } else if (reg_file
[i
]) {
211 if (reg_file
[i
] != prev
) {
213 char_select
= !char_select
;
215 printf(char_select
? "#" : "@");
223 printf("%u/%u used, %u/%u free\n", max
- free_regs
, max
, free_regs
, max
);
225 /* print assignments */
228 for (unsigned i
= lb
; i
< ub
; i
++) {
229 if (reg_file
[i
] != prev
) {
230 if (prev
&& size
> 1)
231 printf("-%d]\n", i
- 1 - lb
);
235 if (prev
&& prev
!= 0xFFFF) {
236 if (ctx
.orig_names
.count(reg_file
[i
]) && ctx
.orig_names
[reg_file
[i
]].id() != reg_file
[i
])
237 printf("%%%u (was %%%d) = %c[%d", reg_file
[i
], ctx
.orig_names
[reg_file
[i
]].id(), reg_char
, i
- lb
);
239 printf("%%%u = %c[%d", reg_file
[i
], reg_char
, i
- lb
);
246 if (prev
&& size
> 1)
247 printf("-%d]\n", ub
- lb
- 1);
254 void adjust_max_used_regs(ra_ctx
& ctx
, RegClass rc
, unsigned reg
)
256 unsigned max_addressible_sgpr
= ctx
.program
->sgpr_limit
;
257 unsigned size
= rc
.size();
258 if (rc
.type() == RegType::vgpr
) {
260 unsigned hi
= reg
- 256 + size
- 1;
261 ctx
.max_used_vgpr
= std::max(ctx
.max_used_vgpr
, hi
);
262 } else if (reg
+ rc
.size() <= max_addressible_sgpr
) {
263 unsigned hi
= reg
+ size
- 1;
264 ctx
.max_used_sgpr
= std::max(ctx
.max_used_sgpr
, std::min(hi
, max_addressible_sgpr
));
269 void update_renames(ra_ctx
& ctx
, RegisterFile
& reg_file
,
270 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
271 aco_ptr
<Instruction
>& instr
)
273 /* allocate id's and rename operands: this is done transparently here */
274 for (std::pair
<Operand
, Definition
>& copy
: parallelcopies
) {
275 /* the definitions with id are not from this function and already handled */
276 if (copy
.second
.isTemp())
279 /* check if we we moved another parallelcopy definition */
280 for (std::pair
<Operand
, Definition
>& other
: parallelcopies
) {
281 if (!other
.second
.isTemp())
283 if (copy
.first
.getTemp() == other
.second
.getTemp()) {
284 copy
.first
.setTemp(other
.first
.getTemp());
285 copy
.first
.setFixed(other
.first
.physReg());
288 // FIXME: if a definition got moved, change the target location and remove the parallelcopy
289 copy
.second
.setTemp(Temp(ctx
.program
->allocateId(), copy
.second
.regClass()));
290 ctx
.assignments
.emplace_back(copy
.second
.physReg(), copy
.second
.regClass());
291 assert(ctx
.assignments
.size() == ctx
.program
->peekAllocationId());
292 reg_file
.fill(copy
.second
);
294 /* check if we moved an operand */
295 for (Operand
& op
: instr
->operands
) {
298 if (op
.tempId() == copy
.first
.tempId()) {
299 bool omit_renaming
= instr
->opcode
== aco_opcode::p_create_vector
&& !op
.isKillBeforeDef();
300 for (std::pair
<Operand
, Definition
>& pc
: parallelcopies
) {
301 PhysReg def_reg
= pc
.second
.physReg();
302 omit_renaming
&= def_reg
> copy
.first
.physReg() ?
303 (copy
.first
.physReg() + copy
.first
.size() <= def_reg
.reg()) :
304 (def_reg
+ pc
.second
.size() <= copy
.first
.physReg().reg());
308 op
.setTemp(copy
.second
.getTemp());
309 op
.setFixed(copy
.second
.physReg());
315 bool instr_can_access_subdword(aco_ptr
<Instruction
>& instr
)
317 return instr
->isSDWA() || instr
->format
== Format::PSEUDO
;
320 std::pair
<PhysReg
, bool> get_reg_simple(ra_ctx
& ctx
,
321 RegisterFile
& reg_file
,
322 uint32_t lb
, uint32_t ub
,
323 uint32_t size
, uint32_t stride
,
326 if (rc
.is_subdword()) {
327 for (std::pair
<uint32_t, std::array
<uint32_t, 4>> entry
: reg_file
.subdword_regs
) {
328 assert(reg_file
[entry
.first
] == 0xF0000000);
329 if (lb
> entry
.first
|| entry
.first
>= ub
)
332 for (unsigned i
= 0; i
< 4; i
+= stride
) {
333 if (entry
.second
[i
] != 0)
336 bool reg_found
= true;
337 for (unsigned j
= 1; reg_found
&& i
+ j
< 4 && j
< rc
.bytes(); j
++)
338 reg_found
&= entry
.second
[i
+ j
] == 0;
340 /* check neighboring reg if needed */
341 reg_found
&= (i
<= 4 - rc
.bytes() || reg_file
[entry
.first
+ 1] == 0);
343 PhysReg res
{entry
.first
};
350 stride
= 1; /* stride in full registers */
353 /* best fit algorithm: find the smallest gap to fit in the variable */
355 unsigned best_pos
= 0xFFFF;
356 unsigned gap_size
= 0xFFFF;
357 unsigned next_pos
= 0xFFFF;
359 for (unsigned current_reg
= lb
; current_reg
< ub
; current_reg
++) {
360 if (reg_file
[current_reg
] != 0 || ctx
.war_hint
[current_reg
]) {
361 if (next_pos
== 0xFFFF)
364 /* check if the variable fits */
365 if (next_pos
+ size
> current_reg
) {
370 /* check if the tested gap is smaller */
371 if (current_reg
- next_pos
< gap_size
) {
373 gap_size
= current_reg
- next_pos
;
379 if (next_pos
== 0xFFFF)
380 next_pos
= current_reg
;
384 if (next_pos
!= 0xFFFF &&
385 next_pos
+ size
<= ub
&&
386 ub
- next_pos
< gap_size
) {
388 gap_size
= ub
- next_pos
;
390 if (best_pos
!= 0xFFFF) {
391 adjust_max_used_regs(ctx
, rc
, best_pos
);
392 return {PhysReg
{best_pos
}, true};
398 unsigned reg_lo
= lb
;
399 unsigned reg_hi
= lb
+ size
- 1;
400 while (!found
&& reg_lo
+ size
<= ub
) {
401 if (reg_file
[reg_lo
] != 0) {
405 reg_hi
= reg_lo
+ size
- 1;
407 for (unsigned reg
= reg_lo
+ 1; found
&& reg
<= reg_hi
; reg
++) {
408 if (reg_file
[reg
] != 0 || ctx
.war_hint
[reg
])
412 adjust_max_used_regs(ctx
, rc
, reg_lo
);
413 return {PhysReg
{reg_lo
}, true};
422 /* collect variables from a register area and clear reg_file */
423 std::set
<std::pair
<unsigned, unsigned>> collect_vars(ra_ctx
& ctx
, RegisterFile
& reg_file
,
424 PhysReg reg
, unsigned size
)
426 std::set
<std::pair
<unsigned, unsigned>> vars
;
427 for (unsigned j
= reg
; j
< reg
+ size
; j
++) {
428 if (reg_file
.is_blocked(PhysReg
{j
}))
430 if (reg_file
[j
] == 0xF0000000) {
431 for (unsigned k
= 0; k
< 4; k
++) {
432 unsigned id
= reg_file
.subdword_regs
[j
][k
];
434 assignment
& var
= ctx
.assignments
[id
];
435 vars
.emplace(var
.rc
.bytes(), id
);
436 reg_file
.clear(var
.reg
, var
.rc
);
441 } else if (reg_file
[j
] != 0) {
442 unsigned id
= reg_file
[j
];
443 assignment
& var
= ctx
.assignments
[id
];
444 vars
.emplace(var
.rc
.bytes(), id
);
445 reg_file
.clear(var
.reg
, var
.rc
);
451 bool get_regs_for_copies(ra_ctx
& ctx
,
452 RegisterFile
& reg_file
,
453 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
454 const std::set
<std::pair
<unsigned, unsigned>> &vars
,
455 uint32_t lb
, uint32_t ub
,
456 aco_ptr
<Instruction
>& instr
,
461 /* variables are sorted from small sized to large */
462 /* NOTE: variables are also sorted by ID. this only affects a very small number of shaders slightly though. */
463 for (std::set
<std::pair
<unsigned, unsigned>>::const_reverse_iterator it
= vars
.rbegin(); it
!= vars
.rend(); ++it
) {
464 unsigned id
= it
->second
;
465 assignment
& var
= ctx
.assignments
[id
];
466 uint32_t size
= var
.rc
.size();
468 if (var
.rc
.type() == RegType::sgpr
) {
475 /* check if this is a dead operand, then we can re-use the space from the definition */
476 bool is_dead_operand
= false;
477 for (unsigned i
= 0; !is_phi(instr
) && !is_dead_operand
&& (i
< instr
->operands
.size()); i
++) {
478 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isKillBeforeDef() && instr
->operands
[i
].tempId() == id
)
479 is_dead_operand
= true;
482 std::pair
<PhysReg
, bool> res
;
483 if (is_dead_operand
) {
484 if (instr
->opcode
== aco_opcode::p_create_vector
) {
485 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].bytes(), i
++) {
486 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].tempId() == id
) {
487 PhysReg
reg(def_reg_lo
);
489 assert(!reg_file
.test(reg
, var
.rc
.bytes()));
495 res
= get_reg_simple(ctx
, reg_file
, def_reg_lo
, def_reg_hi
+ 1, size
, stride
, var
.rc
);
498 res
= get_reg_simple(ctx
, reg_file
, lb
, def_reg_lo
, size
, stride
, var
.rc
);
500 unsigned lb
= (def_reg_hi
+ stride
) & ~(stride
- 1);
501 res
= get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, stride
, var
.rc
);
506 /* mark the area as blocked */
507 reg_file
.block(res
.first
, var
.rc
.bytes());
509 /* create parallelcopy pair (without definition id) */
510 Temp tmp
= Temp(id
, var
.rc
);
511 Operand pc_op
= Operand(tmp
);
512 pc_op
.setFixed(var
.reg
);
513 Definition pc_def
= Definition(res
.first
, pc_op
.regClass());
514 parallelcopies
.emplace_back(pc_op
, pc_def
);
518 unsigned best_pos
= lb
;
519 unsigned num_moves
= 0xFF;
520 unsigned num_vars
= 0;
522 /* we use a sliding window to find potential positions */
523 unsigned reg_lo
= lb
;
524 unsigned reg_hi
= lb
+ size
- 1;
525 for (reg_lo
= lb
, reg_hi
= lb
+ size
- 1; reg_hi
< ub
; reg_lo
+= stride
, reg_hi
+= stride
) {
526 if (!is_dead_operand
&& ((reg_lo
>= def_reg_lo
&& reg_lo
<= def_reg_hi
) ||
527 (reg_hi
>= def_reg_lo
&& reg_hi
<= def_reg_hi
)))
530 /* second, check that we have at most k=num_moves elements in the window
531 * and no element is larger than the currently processed one */
534 unsigned last_var
= 0;
536 for (unsigned j
= reg_lo
; found
&& j
<= reg_hi
; j
++) {
537 if (reg_file
[j
] == 0 || reg_file
[j
] == last_var
)
540 if (reg_file
.is_blocked(PhysReg
{j
}) || k
> num_moves
) {
544 if (reg_file
[j
] == 0xF0000000) {
549 /* we cannot split live ranges of linear vgprs */
550 if (ctx
.assignments
[reg_file
[j
]].rc
& (1 << 6)) {
554 bool is_kill
= false;
555 for (const Operand
& op
: instr
->operands
) {
556 if (op
.isTemp() && op
.isKillBeforeDef() && op
.tempId() == reg_file
[j
]) {
561 if (!is_kill
&& ctx
.assignments
[reg_file
[j
]].rc
.size() >= size
) {
566 k
+= ctx
.assignments
[reg_file
[j
]].rc
.size();
567 last_var
= reg_file
[j
];
569 if (k
> num_moves
|| (k
== num_moves
&& n
<= num_vars
)) {
582 /* FIXME: we messed up and couldn't find space for the variables to be copied */
583 if (num_moves
== 0xFF)
587 reg_hi
= best_pos
+ size
- 1;
589 /* collect variables and block reg file */
590 std::set
<std::pair
<unsigned, unsigned>> new_vars
= collect_vars(ctx
, reg_file
, PhysReg
{reg_lo
}, size
);
592 /* mark the area as blocked */
593 reg_file
.block(PhysReg
{reg_lo
}, size
* 4);
595 if (!get_regs_for_copies(ctx
, reg_file
, parallelcopies
, new_vars
, lb
, ub
, instr
, def_reg_lo
, def_reg_hi
))
598 adjust_max_used_regs(ctx
, var
.rc
, reg_lo
);
600 /* create parallelcopy pair (without definition id) */
601 Temp tmp
= Temp(id
, var
.rc
);
602 Operand pc_op
= Operand(tmp
);
603 pc_op
.setFixed(var
.reg
);
604 Definition pc_def
= Definition(PhysReg
{reg_lo
}, pc_op
.regClass());
605 parallelcopies
.emplace_back(pc_op
, pc_def
);
612 std::pair
<PhysReg
, bool> get_reg_impl(ra_ctx
& ctx
,
613 RegisterFile
& reg_file
,
614 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
615 uint32_t lb
, uint32_t ub
,
616 uint32_t size
, uint32_t stride
,
618 aco_ptr
<Instruction
>& instr
)
620 /* check how many free regs we have */
621 unsigned regs_free
= reg_file
.count_zero(PhysReg
{lb
}, ub
-lb
);
623 /* mark and count killed operands */
624 unsigned killed_ops
= 0;
625 for (unsigned j
= 0; !is_phi(instr
) && j
< instr
->operands
.size(); j
++) {
626 if (instr
->operands
[j
].isTemp() &&
627 instr
->operands
[j
].isFirstKillBeforeDef() &&
628 instr
->operands
[j
].physReg() >= lb
&&
629 instr
->operands
[j
].physReg() < ub
) {
630 assert(instr
->operands
[j
].isFixed());
631 assert(!reg_file
.test(instr
->operands
[j
].physReg(), instr
->operands
[j
].bytes()));
632 reg_file
.block(instr
->operands
[j
].physReg(), instr
->operands
[j
].bytes());
633 killed_ops
+= instr
->operands
[j
].getTemp().size();
637 assert(regs_free
>= size
);
638 /* we might have to move dead operands to dst in order to make space */
639 unsigned op_moves
= 0;
641 if (size
> (regs_free
- killed_ops
))
642 op_moves
= size
- (regs_free
- killed_ops
);
644 /* find the best position to place the definition */
645 unsigned best_pos
= lb
;
646 unsigned num_moves
= 0xFF;
647 unsigned num_vars
= 0;
649 /* we use a sliding window to check potential positions */
650 unsigned reg_lo
= lb
;
651 unsigned reg_hi
= lb
+ size
- 1;
652 for (reg_lo
= lb
, reg_hi
= lb
+ size
- 1; reg_hi
< ub
; reg_lo
+= stride
, reg_hi
+= stride
) {
653 /* first check the edges: this is what we have to fix to allow for num_moves > size */
654 if (reg_lo
> lb
&& reg_file
[reg_lo
] != 0 && reg_file
[reg_lo
] == reg_file
[reg_lo
- 1])
656 if (reg_hi
< ub
- 1 && reg_file
[reg_hi
] != 0 && reg_file
[reg_hi
] == reg_file
[reg_hi
+ 1])
659 /* second, check that we have at most k=num_moves elements in the window
660 * and no element is larger than the currently processed one */
661 unsigned k
= op_moves
;
663 unsigned remaining_op_moves
= op_moves
;
664 unsigned last_var
= 0;
666 bool aligned
= rc
== RegClass::v4
&& reg_lo
% 4 == 0;
667 for (unsigned j
= reg_lo
; found
&& j
<= reg_hi
; j
++) {
668 if (reg_file
[j
] == 0 || reg_file
[j
] == last_var
)
671 /* dead operands effectively reduce the number of estimated moves */
672 if (reg_file
.is_blocked(PhysReg
{j
})) {
673 if (remaining_op_moves
) {
675 remaining_op_moves
--;
680 if (reg_file
[j
] == 0xF0000000) {
686 if (ctx
.assignments
[reg_file
[j
]].rc
.size() >= size
) {
691 /* we cannot split live ranges of linear vgprs */
692 if (ctx
.assignments
[reg_file
[j
]].rc
& (1 << 6)) {
697 k
+= ctx
.assignments
[reg_file
[j
]].rc
.size();
699 last_var
= reg_file
[j
];
702 if (!found
|| k
> num_moves
)
704 if (k
== num_moves
&& n
< num_vars
)
706 if (!aligned
&& k
== num_moves
&& n
== num_vars
)
716 if (num_moves
== 0xFF) {
717 /* remove killed operands from reg_file once again */
718 for (unsigned i
= 0; !is_phi(instr
) && i
< instr
->operands
.size(); i
++) {
719 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isFirstKillBeforeDef())
720 reg_file
.clear(instr
->operands
[i
]);
722 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
723 Definition def
= instr
->definitions
[i
];
724 if (def
.isTemp() && def
.isFixed() && ctx
.defs_done
.test(i
))
730 RegisterFile register_file
= reg_file
;
732 /* now, we figured the placement for our definition */
733 std::set
<std::pair
<unsigned, unsigned>> vars
= collect_vars(ctx
, reg_file
, PhysReg
{best_pos
}, size
);
735 if (instr
->opcode
== aco_opcode::p_create_vector
) {
736 /* move killed operands which aren't yet at the correct position */
737 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].size(), i
++) {
738 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].isFirstKillBeforeDef() &&
739 instr
->operands
[i
].getTemp().type() == rc
.type()) {
741 if (instr
->operands
[i
].physReg() != best_pos
+ offset
) {
742 vars
.emplace(instr
->operands
[i
].bytes(), instr
->operands
[i
].tempId());
743 reg_file
.clear(instr
->operands
[i
]);
745 reg_file
.fill(instr
->operands
[i
]);
750 /* re-enable the killed operands */
751 for (unsigned j
= 0; !is_phi(instr
) && j
< instr
->operands
.size(); j
++) {
752 if (instr
->operands
[j
].isTemp() && instr
->operands
[j
].isFirstKill())
753 reg_file
.fill(instr
->operands
[j
]);
757 std::vector
<std::pair
<Operand
, Definition
>> pc
;
758 if (!get_regs_for_copies(ctx
, reg_file
, pc
, vars
, lb
, ub
, instr
, best_pos
, best_pos
+ size
- 1)) {
759 reg_file
= std::move(register_file
);
760 /* remove killed operands from reg_file once again */
761 if (!is_phi(instr
)) {
762 for (const Operand
& op
: instr
->operands
) {
763 if (op
.isTemp() && op
.isFirstKillBeforeDef())
767 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
768 Definition
& def
= instr
->definitions
[i
];
769 if (def
.isTemp() && def
.isFixed() && ctx
.defs_done
.test(i
))
775 parallelcopies
.insert(parallelcopies
.end(), pc
.begin(), pc
.end());
777 /* we set the definition regs == 0. the actual caller is responsible for correct setting */
778 reg_file
.clear(PhysReg
{best_pos
}, rc
);
780 update_renames(ctx
, reg_file
, parallelcopies
, instr
);
782 /* remove killed operands from reg_file once again */
783 for (unsigned i
= 0; !is_phi(instr
) && i
< instr
->operands
.size(); i
++) {
784 if (!instr
->operands
[i
].isTemp() || !instr
->operands
[i
].isFixed())
786 assert(!instr
->operands
[i
].isUndefined());
787 if (instr
->operands
[i
].isFirstKillBeforeDef())
788 reg_file
.clear(instr
->operands
[i
]);
790 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
791 Definition def
= instr
->definitions
[i
];
792 if (def
.isTemp() && def
.isFixed() && ctx
.defs_done
.test(i
))
796 adjust_max_used_regs(ctx
, rc
, best_pos
);
797 return {PhysReg
{best_pos
}, true};
800 bool get_reg_specified(ra_ctx
& ctx
,
801 RegisterFile
& reg_file
,
803 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
804 aco_ptr
<Instruction
>& instr
,
807 if (rc
.is_subdword() && reg
.byte() && !instr_can_access_subdword(instr
))
810 uint32_t size
= rc
.size();
814 if (rc
.type() == RegType::vgpr
) {
816 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
822 if (reg
% stride
!= 0)
825 ub
= ctx
.program
->max_reg_demand
.sgpr
;
828 uint32_t reg_lo
= reg
.reg();
829 uint32_t reg_hi
= reg
+ (size
- 1);
831 if (reg_lo
< lb
|| reg_hi
>= ub
|| reg_lo
> reg_hi
)
834 if (reg_file
.test(reg
, rc
.bytes()))
837 adjust_max_used_regs(ctx
, rc
, reg_lo
);
841 std::pair
<PhysReg
, bool> get_reg_vec(ra_ctx
& ctx
,
842 RegisterFile
& reg_file
,
845 uint32_t size
= rc
.size();
848 if (rc
.type() == RegType::vgpr
) {
850 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
853 ub
= ctx
.program
->max_reg_demand
.sgpr
;
859 return get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, stride
, rc
);
862 PhysReg
get_reg(ra_ctx
& ctx
,
863 RegisterFile
& reg_file
,
865 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
866 aco_ptr
<Instruction
>& instr
)
868 if (ctx
.affinities
.find(temp
.id()) != ctx
.affinities
.end() &&
869 ctx
.assignments
[ctx
.affinities
[temp
.id()]].assigned
) {
870 PhysReg reg
= ctx
.assignments
[ctx
.affinities
[temp
.id()]].reg
;
871 if (get_reg_specified(ctx
, reg_file
, temp
.regClass(), parallelcopies
, instr
, reg
))
875 if (ctx
.vectors
.find(temp
.id()) != ctx
.vectors
.end()) {
876 Instruction
* vec
= ctx
.vectors
[temp
.id()];
877 unsigned byte_offset
= 0;
878 for (const Operand
& op
: vec
->operands
) {
879 if (op
.isTemp() && op
.tempId() == temp
.id())
882 byte_offset
+= op
.bytes();
885 for (const Operand
& op
: vec
->operands
) {
887 op
.tempId() != temp
.id() &&
888 op
.getTemp().type() == temp
.type() &&
889 ctx
.assignments
[op
.tempId()].assigned
) {
890 PhysReg reg
= ctx
.assignments
[op
.tempId()].reg
;
891 reg
.reg_b
+= (byte_offset
- k
);
892 if (get_reg_specified(ctx
, reg_file
, temp
.regClass(), parallelcopies
, instr
, reg
))
898 std::pair
<PhysReg
, bool> res
= get_reg_vec(ctx
, reg_file
, vec
->definitions
[0].regClass());
899 PhysReg reg
= res
.first
;
901 reg
.reg_b
+= byte_offset
;
902 /* make sure to only use byte offset if the instruction supports it */
903 if (get_reg_specified(ctx
, reg_file
, temp
.regClass(), parallelcopies
, instr
, reg
))
908 RegClass rc
= temp
.regClass();
909 uint32_t size
= rc
.size();
912 if (rc
.type() == RegType::vgpr
) {
914 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
917 ub
= ctx
.program
->max_reg_demand
.sgpr
;
924 if (rc
.is_subdword()) {
925 /* stride in bytes */
926 if(!instr_can_access_subdword(instr
))
928 else if (rc
.bytes() % 4 == 0)
930 else if (rc
.bytes() % 2 == 0)
934 std::pair
<PhysReg
, bool> res
= {{}, false};
935 /* try to find space without live-range splits */
936 if (rc
.type() == RegType::vgpr
&& (size
== 4 || size
== 8))
937 res
= get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, 4, rc
);
939 res
= get_reg_simple(ctx
, reg_file
, lb
, ub
, size
, stride
, rc
);
943 /* try to find space with live-range splits */
944 res
= get_reg_impl(ctx
, reg_file
, parallelcopies
, lb
, ub
, size
, stride
, rc
, instr
);
949 /* try using more registers */
951 /* We should only fail here because keeping under the limit would require
953 assert(reg_file
.count_zero(PhysReg
{lb
}, ub
-lb
) >= size
);
955 uint16_t max_addressible_sgpr
= ctx
.program
->sgpr_limit
;
956 uint16_t max_addressible_vgpr
= ctx
.program
->vgpr_limit
;
957 if (rc
.type() == RegType::vgpr
&& ctx
.program
->max_reg_demand
.vgpr
< max_addressible_vgpr
) {
958 update_vgpr_sgpr_demand(ctx
.program
, RegisterDemand(ctx
.program
->max_reg_demand
.vgpr
+ 1, ctx
.program
->max_reg_demand
.sgpr
));
959 return get_reg(ctx
, reg_file
, temp
, parallelcopies
, instr
);
960 } else if (rc
.type() == RegType::sgpr
&& ctx
.program
->max_reg_demand
.sgpr
< max_addressible_sgpr
) {
961 update_vgpr_sgpr_demand(ctx
.program
, RegisterDemand(ctx
.program
->max_reg_demand
.vgpr
, ctx
.program
->max_reg_demand
.sgpr
+ 1));
962 return get_reg(ctx
, reg_file
, temp
, parallelcopies
, instr
);
965 //FIXME: if nothing helps, shift-rotate the registers to make space
967 unreachable("did not find a register");
970 PhysReg
get_reg_create_vector(ra_ctx
& ctx
,
971 RegisterFile
& reg_file
,
973 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopies
,
974 aco_ptr
<Instruction
>& instr
)
976 RegClass rc
= temp
.regClass();
977 /* create_vector instructions have different costs w.r.t. register coalescing */
978 uint32_t size
= rc
.size();
979 uint32_t bytes
= rc
.bytes();
982 if (rc
.type() == RegType::vgpr
) {
984 ub
= 256 + ctx
.program
->max_reg_demand
.vgpr
;
987 ub
= ctx
.program
->max_reg_demand
.sgpr
;
994 //TODO: improve p_create_vector for sub-dword vectors
996 unsigned best_pos
= -1;
997 unsigned num_moves
= 0xFF;
998 bool best_war_hint
= true;
1000 /* test for each operand which definition placement causes the least shuffle instructions */
1001 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].bytes(), i
++) {
1002 // TODO: think about, if we can alias live operands on the same register
1003 if (!instr
->operands
[i
].isTemp() || !instr
->operands
[i
].isKillBeforeDef() || instr
->operands
[i
].getTemp().type() != rc
.type())
1006 if (offset
> instr
->operands
[i
].physReg().reg_b
)
1009 unsigned reg_lo
= instr
->operands
[i
].physReg().reg_b
- offset
;
1013 unsigned reg_hi
= reg_lo
+ size
- 1;
1016 /* no need to check multiple times */
1017 if (reg_lo
== best_pos
)
1021 // TODO: this can be improved */
1022 if (reg_lo
< lb
|| reg_hi
>= ub
|| reg_lo
% stride
!= 0)
1024 if (reg_lo
> lb
&& reg_file
[reg_lo
] != 0 && reg_file
[reg_lo
] == reg_file
[reg_lo
- 1])
1026 if (reg_hi
< ub
- 1 && reg_file
[reg_hi
] != 0 && reg_file
[reg_hi
] == reg_file
[reg_hi
+ 1])
1029 /* count variables to be moved and check war_hint */
1030 bool war_hint
= false;
1031 bool linear_vgpr
= false;
1032 for (unsigned j
= reg_lo
; j
<= reg_hi
&& !linear_vgpr
; j
++) {
1033 if (reg_file
[j
] != 0) {
1034 if (reg_file
[j
] == 0xF0000000) {
1037 unsigned bytes_left
= bytes
- (j
- reg_lo
) * 4;
1038 for (unsigned k
= 0; k
< MIN2(bytes_left
, 4); k
++, reg
.reg_b
++)
1039 k
+= reg_file
.test(reg
, 1);
1042 /* we cannot split live ranges of linear vgprs */
1043 if (ctx
.assignments
[reg_file
[j
]].rc
& (1 << 6))
1047 war_hint
|= ctx
.war_hint
[j
];
1049 if (linear_vgpr
|| (war_hint
&& !best_war_hint
))
1052 /* count operands in wrong positions */
1053 for (unsigned j
= 0, offset
= 0; j
< instr
->operands
.size(); offset
+= instr
->operands
[j
].bytes(), j
++) {
1055 !instr
->operands
[j
].isTemp() ||
1056 instr
->operands
[j
].getTemp().type() != rc
.type())
1058 if (instr
->operands
[j
].physReg().reg_b
!= reg_lo
* 4 + offset
)
1059 k
+= instr
->operands
[j
].bytes();
1061 bool aligned
= rc
== RegClass::v4
&& reg_lo
% 4 == 0;
1062 if (k
> num_moves
|| (!aligned
&& k
== num_moves
))
1067 best_war_hint
= war_hint
;
1070 if (num_moves
>= bytes
)
1071 return get_reg(ctx
, reg_file
, temp
, parallelcopies
, instr
);
1073 /* collect variables to be moved */
1074 std::set
<std::pair
<unsigned, unsigned>> vars
= collect_vars(ctx
, reg_file
, PhysReg
{best_pos
}, size
);
1076 /* move killed operands which aren't yet at the correct position */
1077 uint64_t moved_operand_mask
= 0;
1078 for (unsigned i
= 0, offset
= 0; i
< instr
->operands
.size(); offset
+= instr
->operands
[i
].bytes(), i
++) {
1079 if (instr
->operands
[i
].isTemp() &&
1080 instr
->operands
[i
].isFirstKillBeforeDef() &&
1081 instr
->operands
[i
].getTemp().type() == rc
.type() &&
1082 instr
->operands
[i
].physReg().reg_b
!= best_pos
* 4 + offset
) {
1083 vars
.emplace(instr
->operands
[i
].bytes(), instr
->operands
[i
].tempId());
1084 moved_operand_mask
|= (uint64_t)1 << i
;
1088 ASSERTED
bool success
= false;
1089 success
= get_regs_for_copies(ctx
, reg_file
, parallelcopies
, vars
, lb
, ub
, instr
, best_pos
, best_pos
+ size
- 1);
1092 update_renames(ctx
, reg_file
, parallelcopies
, instr
);
1093 adjust_max_used_regs(ctx
, rc
, best_pos
);
1095 while (moved_operand_mask
) {
1096 unsigned i
= u_bit_scan64(&moved_operand_mask
);
1097 assert(instr
->operands
[i
].isFirstKillBeforeDef());
1098 reg_file
.clear(instr
->operands
[i
]);
1101 return PhysReg
{best_pos
};
1104 void handle_pseudo(ra_ctx
& ctx
,
1105 const RegisterFile
& reg_file
,
1108 if (instr
->format
!= Format::PSEUDO
)
1111 /* all instructions which use handle_operands() need this information */
1112 switch (instr
->opcode
) {
1113 case aco_opcode::p_extract_vector
:
1114 case aco_opcode::p_create_vector
:
1115 case aco_opcode::p_split_vector
:
1116 case aco_opcode::p_parallelcopy
:
1117 case aco_opcode::p_wqm
:
1123 /* if all definitions are vgpr, no need to care for SCC */
1124 bool writes_sgpr
= false;
1125 for (Definition
& def
: instr
->definitions
) {
1126 if (def
.getTemp().type() == RegType::sgpr
) {
1131 /* if all operands are constant, no need to care either */
1132 bool reads_sgpr
= false;
1133 for (Operand
& op
: instr
->operands
) {
1134 if (op
.isTemp() && op
.getTemp().type() == RegType::sgpr
) {
1139 if (!(writes_sgpr
&& reads_sgpr
))
1142 Pseudo_instruction
*pi
= (Pseudo_instruction
*)instr
;
1143 if (reg_file
[scc
.reg()]) {
1144 pi
->tmp_in_scc
= true;
1146 int reg
= ctx
.max_used_sgpr
;
1147 for (; reg
>= 0 && reg_file
[reg
]; reg
--)
1150 reg
= ctx
.max_used_sgpr
+ 1;
1151 for (; reg
< ctx
.program
->max_reg_demand
.sgpr
&& reg_file
[reg
]; reg
++)
1153 assert(reg
< ctx
.program
->max_reg_demand
.sgpr
);
1156 adjust_max_used_regs(ctx
, s1
, reg
);
1157 pi
->scratch_sgpr
= PhysReg
{(unsigned)reg
};
1159 pi
->tmp_in_scc
= false;
1163 bool operand_can_use_reg(aco_ptr
<Instruction
>& instr
, unsigned idx
, PhysReg reg
)
1165 if (instr
->operands
[idx
].isFixed())
1166 return instr
->operands
[idx
].physReg() == reg
;
1168 if (!instr_can_access_subdword(instr
) && reg
.byte())
1171 switch (instr
->format
) {
1173 return reg
!= scc
&&
1175 (reg
!= m0
|| idx
== 1 || idx
== 3) && /* offset can be m0 */
1176 (reg
!= vcc
|| (instr
->definitions
.empty() && idx
== 2)); /* sdata can be vcc */
1178 // TODO: there are more instructions with restrictions on registers
1183 void get_reg_for_operand(ra_ctx
& ctx
, RegisterFile
& register_file
,
1184 std::vector
<std::pair
<Operand
, Definition
>>& parallelcopy
,
1185 aco_ptr
<Instruction
>& instr
, Operand
& operand
)
1187 /* check if the operand is fixed */
1189 if (operand
.isFixed()) {
1190 assert(operand
.physReg() != ctx
.assignments
[operand
.tempId()].reg
);
1192 /* check if target reg is blocked, and move away the blocking var */
1193 if (register_file
[operand
.physReg().reg()]) {
1194 assert(register_file
[operand
.physReg()] != 0xF0000000);
1195 uint32_t blocking_id
= register_file
[operand
.physReg().reg()];
1196 RegClass rc
= ctx
.assignments
[blocking_id
].rc
;
1197 Operand pc_op
= Operand(Temp
{blocking_id
, rc
});
1198 pc_op
.setFixed(operand
.physReg());
1201 PhysReg reg
= get_reg(ctx
, register_file
, pc_op
.getTemp(), parallelcopy
, instr
);
1202 Definition pc_def
= Definition(PhysReg
{reg
}, pc_op
.regClass());
1203 register_file
.clear(pc_op
);
1204 parallelcopy
.emplace_back(pc_op
, pc_def
);
1206 dst
= operand
.physReg();
1209 dst
= get_reg(ctx
, register_file
, operand
.getTemp(), parallelcopy
, instr
);
1212 Operand pc_op
= operand
;
1213 pc_op
.setFixed(ctx
.assignments
[operand
.tempId()].reg
);
1214 Definition pc_def
= Definition(dst
, pc_op
.regClass());
1215 register_file
.clear(pc_op
);
1216 parallelcopy
.emplace_back(pc_op
, pc_def
);
1217 update_renames(ctx
, register_file
, parallelcopy
, instr
);
1220 Temp
read_variable(ra_ctx
& ctx
, Temp val
, unsigned block_idx
)
1222 std::unordered_map
<unsigned, Temp
>::iterator it
= ctx
.renames
[block_idx
].find(val
.id());
1223 if (it
== ctx
.renames
[block_idx
].end())
1229 Temp
handle_live_in(ra_ctx
& ctx
, Temp val
, Block
* block
)
1231 std::vector
<unsigned>& preds
= val
.is_linear() ? block
->linear_preds
: block
->logical_preds
;
1232 if (preds
.size() == 0 || val
.regClass() == val
.regClass().as_linear())
1235 assert(preds
.size() > 0);
1238 if (!ctx
.sealed
[block
->index
]) {
1239 /* consider rename from already processed predecessor */
1240 Temp tmp
= read_variable(ctx
, val
, preds
[0]);
1242 /* if the block is not sealed yet, we create an incomplete phi (which might later get removed again) */
1243 new_val
= Temp
{ctx
.program
->allocateId(), val
.regClass()};
1244 ctx
.assignments
.emplace_back();
1245 aco_opcode opcode
= val
.is_linear() ? aco_opcode::p_linear_phi
: aco_opcode::p_phi
;
1246 aco_ptr
<Instruction
> phi
{create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, preds
.size(), 1)};
1247 phi
->definitions
[0] = Definition(new_val
);
1248 for (unsigned i
= 0; i
< preds
.size(); i
++)
1249 phi
->operands
[i
] = Operand(val
);
1250 if (tmp
.regClass() == new_val
.regClass())
1251 ctx
.affinities
[new_val
.id()] = tmp
.id();
1253 ctx
.phi_map
.emplace(new_val
.id(), phi_info
{phi
.get(), block
->index
});
1254 ctx
.incomplete_phis
[block
->index
].emplace_back(phi
.get());
1255 block
->instructions
.insert(block
->instructions
.begin(), std::move(phi
));
1257 } else if (preds
.size() == 1) {
1258 /* if the block has only one predecessor, just look there for the name */
1259 new_val
= read_variable(ctx
, val
, preds
[0]);
1261 /* there are multiple predecessors and the block is sealed */
1262 Temp ops
[preds
.size()];
1264 /* get the rename from each predecessor and check if they are the same */
1265 bool needs_phi
= false;
1266 for (unsigned i
= 0; i
< preds
.size(); i
++) {
1267 ops
[i
] = read_variable(ctx
, val
, preds
[i
]);
1271 needs_phi
|= !(new_val
== ops
[i
]);
1275 /* the variable has been renamed differently in the predecessors: we need to insert a phi */
1276 aco_opcode opcode
= val
.is_linear() ? aco_opcode::p_linear_phi
: aco_opcode::p_phi
;
1277 aco_ptr
<Instruction
> phi
{create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, preds
.size(), 1)};
1278 new_val
= Temp
{ctx
.program
->allocateId(), val
.regClass()};
1279 phi
->definitions
[0] = Definition(new_val
);
1280 for (unsigned i
= 0; i
< preds
.size(); i
++) {
1281 phi
->operands
[i
] = Operand(ops
[i
]);
1282 phi
->operands
[i
].setFixed(ctx
.assignments
[ops
[i
].id()].reg
);
1283 if (ops
[i
].regClass() == new_val
.regClass())
1284 ctx
.affinities
[new_val
.id()] = ops
[i
].id();
1286 ctx
.assignments
.emplace_back();
1287 assert(ctx
.assignments
.size() == ctx
.program
->peekAllocationId());
1288 ctx
.phi_map
.emplace(new_val
.id(), phi_info
{phi
.get(), block
->index
});
1289 block
->instructions
.insert(block
->instructions
.begin(), std::move(phi
));
1293 if (new_val
!= val
) {
1294 ctx
.renames
[block
->index
][val
.id()] = new_val
;
1295 ctx
.orig_names
[new_val
.id()] = val
;
1300 void try_remove_trivial_phi(ra_ctx
& ctx
, Temp temp
)
1302 std::unordered_map
<unsigned, phi_info
>::iterator info
= ctx
.phi_map
.find(temp
.id());
1304 if (info
== ctx
.phi_map
.end() || !ctx
.sealed
[info
->second
.block_idx
])
1307 assert(info
->second
.block_idx
!= 0);
1308 Instruction
* phi
= info
->second
.phi
;
1310 Definition def
= phi
->definitions
[0];
1312 /* a phi node is trivial if all operands are the same as the definition of the phi */
1313 for (const Operand
& op
: phi
->operands
) {
1314 const Temp t
= op
.getTemp();
1315 if (t
== same
|| t
== def
.getTemp()) {
1316 assert(t
== same
|| op
.physReg() == def
.physReg());
1324 assert(same
!= Temp() || same
== def
.getTemp());
1326 /* reroute all uses to same and remove phi */
1327 std::vector
<Temp
> phi_users
;
1328 std::unordered_map
<unsigned, phi_info
>::iterator same_phi_info
= ctx
.phi_map
.find(same
.id());
1329 for (Instruction
* instr
: info
->second
.uses
) {
1330 assert(phi
!= instr
);
1331 /* recursively try to remove trivial phis */
1332 if (is_phi(instr
)) {
1333 /* ignore if the phi was already flagged trivial */
1334 if (instr
->definitions
.empty())
1337 if (instr
->definitions
[0].getTemp() != temp
)
1338 phi_users
.emplace_back(instr
->definitions
[0].getTemp());
1340 for (Operand
& op
: instr
->operands
) {
1341 if (op
.isTemp() && op
.tempId() == def
.tempId()) {
1343 if (same_phi_info
!= ctx
.phi_map
.end())
1344 same_phi_info
->second
.uses
.emplace(instr
);
1349 auto it
= ctx
.orig_names
.find(same
.id());
1350 unsigned orig_var
= it
!= ctx
.orig_names
.end() ? it
->second
.id() : same
.id();
1351 for (unsigned i
= 0; i
< ctx
.program
->blocks
.size(); i
++) {
1352 auto it
= ctx
.renames
[i
].find(orig_var
);
1353 if (it
!= ctx
.renames
[i
].end() && it
->second
== def
.getTemp())
1354 ctx
.renames
[i
][orig_var
] = same
;
1357 phi
->definitions
.clear(); /* this indicates that the phi can be removed */
1358 ctx
.phi_map
.erase(info
);
1359 for (Temp t
: phi_users
)
1360 try_remove_trivial_phi(ctx
, t
);
1365 } /* end namespace */
1368 void register_allocation(Program
*program
, std::vector
<TempSet
>& live_out_per_block
)
1370 ra_ctx
ctx(program
);
1371 std::vector
<std::vector
<Temp
>> phi_ressources
;
1372 std::unordered_map
<unsigned, unsigned> temp_to_phi_ressources
;
1374 for (std::vector
<Block
>::reverse_iterator it
= program
->blocks
.rbegin(); it
!= program
->blocks
.rend(); it
++) {
1377 /* first, compute the death points of all live vars within the block */
1378 TempSet
& live
= live_out_per_block
[block
.index
];
1380 std::vector
<aco_ptr
<Instruction
>>::reverse_iterator rit
;
1381 for (rit
= block
.instructions
.rbegin(); rit
!= block
.instructions
.rend(); ++rit
) {
1382 aco_ptr
<Instruction
>& instr
= *rit
;
1383 if (is_phi(instr
)) {
1384 live
.erase(instr
->definitions
[0].getTemp());
1385 if (instr
->definitions
[0].isKill() || instr
->definitions
[0].isFixed())
1387 /* collect information about affinity-related temporaries */
1388 std::vector
<Temp
> affinity_related
;
1389 /* affinity_related[0] is the last seen affinity-related temp */
1390 affinity_related
.emplace_back(instr
->definitions
[0].getTemp());
1391 affinity_related
.emplace_back(instr
->definitions
[0].getTemp());
1392 for (const Operand
& op
: instr
->operands
) {
1393 if (op
.isTemp() && op
.regClass() == instr
->definitions
[0].regClass()) {
1394 affinity_related
.emplace_back(op
.getTemp());
1395 temp_to_phi_ressources
[op
.tempId()] = phi_ressources
.size();
1398 phi_ressources
.emplace_back(std::move(affinity_related
));
1402 /* add vector affinities */
1403 if (instr
->opcode
== aco_opcode::p_create_vector
) {
1404 for (const Operand
& op
: instr
->operands
) {
1405 if (op
.isTemp() && op
.getTemp().type() == instr
->definitions
[0].getTemp().type())
1406 ctx
.vectors
[op
.tempId()] = instr
.get();
1410 /* add operands to live variables */
1411 for (const Operand
& op
: instr
->operands
) {
1413 live
.emplace(op
.getTemp());
1416 /* erase definitions from live */
1417 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
1418 const Definition
& def
= instr
->definitions
[i
];
1421 live
.erase(def
.getTemp());
1422 /* mark last-seen phi operand */
1423 std::unordered_map
<unsigned, unsigned>::iterator it
= temp_to_phi_ressources
.find(def
.tempId());
1424 if (it
!= temp_to_phi_ressources
.end() && def
.regClass() == phi_ressources
[it
->second
][0].regClass()) {
1425 phi_ressources
[it
->second
][0] = def
.getTemp();
1426 /* try to coalesce phi affinities with parallelcopies */
1427 if (!def
.isFixed() && instr
->opcode
== aco_opcode::p_parallelcopy
) {
1428 Operand op
= instr
->operands
[i
];
1429 if (op
.isTemp() && op
.isFirstKillBeforeDef() && def
.regClass() == op
.regClass()) {
1430 phi_ressources
[it
->second
].emplace_back(op
.getTemp());
1431 temp_to_phi_ressources
[op
.tempId()] = it
->second
;
1438 /* create affinities */
1439 for (std::vector
<Temp
>& vec
: phi_ressources
) {
1440 assert(vec
.size() > 1);
1441 for (unsigned i
= 1; i
< vec
.size(); i
++)
1442 if (vec
[i
].id() != vec
[0].id())
1443 ctx
.affinities
[vec
[i
].id()] = vec
[0].id();
1446 /* state of register file after phis */
1447 std::vector
<std::bitset
<128>> sgpr_live_in(program
->blocks
.size());
1449 for (Block
& block
: program
->blocks
) {
1450 TempSet
& live
= live_out_per_block
[block
.index
];
1451 /* initialize register file */
1452 assert(block
.index
!= 0 || live
.empty());
1453 RegisterFile register_file
;
1454 ctx
.war_hint
.reset();
1456 for (Temp t
: live
) {
1457 Temp renamed
= handle_live_in(ctx
, t
, &block
);
1458 assignment
& var
= ctx
.assignments
[renamed
.id()];
1459 /* due to live-range splits, the live-in might be a phi, now */
1461 register_file
.fill(Definition(renamed
.id(), var
.reg
, var
.rc
));
1464 std::vector
<aco_ptr
<Instruction
>> instructions
;
1465 std::vector
<aco_ptr
<Instruction
>>::iterator it
;
1467 /* this is a slight adjustment from the paper as we already have phi nodes:
1468 * We consider them incomplete phis and only handle the definition. */
1470 /* handle fixed phi definitions */
1471 for (it
= block
.instructions
.begin(); it
!= block
.instructions
.end(); ++it
) {
1472 aco_ptr
<Instruction
>& phi
= *it
;
1475 Definition
& definition
= phi
->definitions
[0];
1476 if (!definition
.isFixed())
1479 /* check if a dead exec mask phi is needed */
1480 if (definition
.isKill()) {
1481 for (Operand
& op
: phi
->operands
) {
1482 assert(op
.isTemp());
1483 if (!ctx
.assignments
[op
.tempId()].assigned
||
1484 ctx
.assignments
[op
.tempId()].reg
!= exec
) {
1485 definition
.setKill(false);
1491 if (definition
.isKill())
1494 assert(definition
.physReg() == exec
);
1495 assert(!register_file
.test(definition
.physReg(), definition
.bytes()));
1496 register_file
.fill(definition
);
1497 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1500 /* look up the affinities */
1501 for (it
= block
.instructions
.begin(); it
!= block
.instructions
.end(); ++it
) {
1502 aco_ptr
<Instruction
>& phi
= *it
;
1505 Definition
& definition
= phi
->definitions
[0];
1506 if (definition
.isKill() || definition
.isFixed())
1509 if (ctx
.affinities
.find(definition
.tempId()) != ctx
.affinities
.end() &&
1510 ctx
.assignments
[ctx
.affinities
[definition
.tempId()]].assigned
) {
1511 assert(ctx
.assignments
[ctx
.affinities
[definition
.tempId()]].rc
== definition
.regClass());
1512 PhysReg reg
= ctx
.assignments
[ctx
.affinities
[definition
.tempId()]].reg
;
1513 bool try_use_special_reg
= reg
== scc
|| reg
== exec
;
1514 if (try_use_special_reg
) {
1515 for (const Operand
& op
: phi
->operands
) {
1516 if (!(op
.isTemp() && ctx
.assignments
[op
.tempId()].assigned
&&
1517 ctx
.assignments
[op
.tempId()].reg
== reg
)) {
1518 try_use_special_reg
= false;
1522 if (!try_use_special_reg
)
1525 /* only assign if register is still free */
1526 if (!register_file
.test(reg
, definition
.bytes())) {
1527 definition
.setFixed(reg
);
1528 register_file
.fill(definition
);
1529 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1534 /* find registers for phis without affinity or where the register was blocked */
1535 for (it
= block
.instructions
.begin();it
!= block
.instructions
.end(); ++it
) {
1536 aco_ptr
<Instruction
>& phi
= *it
;
1540 Definition
& definition
= phi
->definitions
[0];
1541 if (definition
.isKill())
1544 if (!definition
.isFixed()) {
1545 std::vector
<std::pair
<Operand
, Definition
>> parallelcopy
;
1546 /* try to find a register that is used by at least one operand */
1547 for (const Operand
& op
: phi
->operands
) {
1548 if (!(op
.isTemp() && ctx
.assignments
[op
.tempId()].assigned
))
1550 PhysReg reg
= ctx
.assignments
[op
.tempId()].reg
;
1551 /* we tried this already on the previous loop */
1552 if (reg
== scc
|| reg
== exec
)
1554 if (get_reg_specified(ctx
, register_file
, definition
.regClass(), parallelcopy
, phi
, reg
)) {
1555 definition
.setFixed(reg
);
1559 if (!definition
.isFixed())
1560 definition
.setFixed(get_reg(ctx
, register_file
, definition
.getTemp(), parallelcopy
, phi
));
1562 /* process parallelcopy */
1563 for (std::pair
<Operand
, Definition
> pc
: parallelcopy
) {
1564 /* see if it's a copy from a different phi */
1565 //TODO: prefer moving some previous phis over live-ins
1566 //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)
1567 Instruction
*prev_phi
= NULL
;
1568 std::vector
<aco_ptr
<Instruction
>>::iterator phi_it
;
1569 for (phi_it
= instructions
.begin(); phi_it
!= instructions
.end(); ++phi_it
) {
1570 if ((*phi_it
)->definitions
[0].tempId() == pc
.first
.tempId())
1571 prev_phi
= phi_it
->get();
1574 while (!prev_phi
&& is_phi(*++phi_it
)) {
1575 if ((*phi_it
)->definitions
[0].tempId() == pc
.first
.tempId())
1576 prev_phi
= phi_it
->get();
1579 /* if so, just update that phi's register */
1580 register_file
.clear(prev_phi
->definitions
[0]);
1581 prev_phi
->definitions
[0].setFixed(pc
.second
.physReg());
1582 ctx
.assignments
[prev_phi
->definitions
[0].tempId()] = {pc
.second
.physReg(), pc
.second
.regClass()};
1583 register_file
.fill(prev_phi
->definitions
[0]);
1588 std::unordered_map
<unsigned, Temp
>::iterator orig_it
= ctx
.orig_names
.find(pc
.first
.tempId());
1589 Temp orig
= pc
.first
.getTemp();
1590 if (orig_it
!= ctx
.orig_names
.end())
1591 orig
= orig_it
->second
;
1593 ctx
.orig_names
[pc
.second
.tempId()] = orig
;
1594 ctx
.renames
[block
.index
][orig
.id()] = pc
.second
.getTemp();
1596 /* otherwise, this is a live-in and we need to create a new phi
1597 * to move it in this block's predecessors */
1598 aco_opcode opcode
= pc
.first
.getTemp().is_linear() ? aco_opcode::p_linear_phi
: aco_opcode::p_phi
;
1599 std::vector
<unsigned>& preds
= pc
.first
.getTemp().is_linear() ? block
.linear_preds
: block
.logical_preds
;
1600 aco_ptr
<Instruction
> new_phi
{create_instruction
<Pseudo_instruction
>(opcode
, Format::PSEUDO
, preds
.size(), 1)};
1601 new_phi
->definitions
[0] = pc
.second
;
1602 for (unsigned i
= 0; i
< preds
.size(); i
++)
1603 new_phi
->operands
[i
] = Operand(pc
.first
);
1604 instructions
.emplace_back(std::move(new_phi
));
1607 register_file
.fill(definition
);
1608 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1610 live
.emplace(definition
.getTemp());
1612 /* update phi affinities */
1613 for (const Operand
& op
: phi
->operands
) {
1614 if (op
.isTemp() && op
.regClass() == phi
->definitions
[0].regClass())
1615 ctx
.affinities
[op
.tempId()] = definition
.tempId();
1618 instructions
.emplace_back(std::move(*it
));
1621 /* fill in sgpr_live_in */
1622 for (unsigned i
= 0; i
<= ctx
.max_used_sgpr
; i
++)
1623 sgpr_live_in
[block
.index
][i
] = register_file
[i
];
1624 sgpr_live_in
[block
.index
][127] = register_file
[scc
.reg()];
1626 /* Handle all other instructions of the block */
1627 for (; it
!= block
.instructions
.end(); ++it
) {
1628 aco_ptr
<Instruction
>& instr
= *it
;
1630 /* parallelcopies from p_phi are inserted here which means
1631 * live ranges of killed operands end here as well */
1632 if (instr
->opcode
== aco_opcode::p_logical_end
) {
1633 /* no need to process this instruction any further */
1634 if (block
.logical_succs
.size() != 1) {
1635 instructions
.emplace_back(std::move(instr
));
1639 Block
& succ
= program
->blocks
[block
.logical_succs
[0]];
1641 for (; idx
< succ
.logical_preds
.size(); idx
++) {
1642 if (succ
.logical_preds
[idx
] == block
.index
)
1645 for (aco_ptr
<Instruction
>& phi
: succ
.instructions
) {
1646 if (phi
->opcode
== aco_opcode::p_phi
) {
1647 if (phi
->operands
[idx
].isTemp() &&
1648 phi
->operands
[idx
].getTemp().type() == RegType::sgpr
&&
1649 phi
->operands
[idx
].isFirstKillBeforeDef()) {
1650 Temp phi_op
= read_variable(ctx
, phi
->operands
[idx
].getTemp(), block
.index
);
1651 PhysReg reg
= ctx
.assignments
[phi_op
.id()].reg
;
1652 assert(register_file
[reg
] == phi_op
.id());
1653 register_file
[reg
] = 0;
1655 } else if (phi
->opcode
!= aco_opcode::p_linear_phi
) {
1659 instructions
.emplace_back(std::move(instr
));
1663 std::vector
<std::pair
<Operand
, Definition
>> parallelcopy
;
1665 assert(!is_phi(instr
));
1667 /* handle operands */
1668 for (unsigned i
= 0; i
< instr
->operands
.size(); ++i
) {
1669 auto& operand
= instr
->operands
[i
];
1670 if (!operand
.isTemp())
1673 /* rename operands */
1674 operand
.setTemp(read_variable(ctx
, operand
.getTemp(), block
.index
));
1675 assert(ctx
.assignments
[operand
.tempId()].assigned
);
1677 PhysReg reg
= ctx
.assignments
[operand
.tempId()].reg
;
1678 if (operand_can_use_reg(instr
, i
, reg
))
1679 operand
.setFixed(reg
);
1681 get_reg_for_operand(ctx
, register_file
, parallelcopy
, instr
, operand
);
1683 if (instr
->format
== Format::EXP
||
1684 (instr
->isVMEM() && i
== 3 && ctx
.program
->chip_class
== GFX6
) ||
1685 (instr
->format
== Format::DS
&& static_cast<DS_instruction
*>(instr
.get())->gds
)) {
1686 for (unsigned j
= 0; j
< operand
.size(); j
++)
1687 ctx
.war_hint
.set(operand
.physReg().reg() + j
);
1690 std::unordered_map
<unsigned, phi_info
>::iterator phi
= ctx
.phi_map
.find(operand
.getTemp().id());
1691 if (phi
!= ctx
.phi_map
.end())
1692 phi
->second
.uses
.emplace(instr
.get());
1695 /* remove dead vars from register file */
1696 for (const Operand
& op
: instr
->operands
) {
1697 if (op
.isTemp() && op
.isFirstKillBeforeDef())
1698 register_file
.clear(op
);
1701 /* try to optimize v_mad_f32 -> v_mac_f32 */
1702 if (instr
->opcode
== aco_opcode::v_mad_f32
&&
1703 instr
->operands
[2].isTemp() &&
1704 instr
->operands
[2].isKillBeforeDef() &&
1705 instr
->operands
[2].getTemp().type() == RegType::vgpr
&&
1706 instr
->operands
[1].isTemp() &&
1707 instr
->operands
[1].getTemp().type() == RegType::vgpr
) { /* TODO: swap src0 and src1 in this case */
1708 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(instr
.get());
1709 bool can_use_mac
= !(vop3
->abs
[0] || vop3
->abs
[1] || vop3
->abs
[2] ||
1710 vop3
->neg
[0] || vop3
->neg
[1] || vop3
->neg
[2] ||
1711 vop3
->clamp
|| vop3
->omod
|| vop3
->opsel
);
1713 instr
->format
= Format::VOP2
;
1714 instr
->opcode
= aco_opcode::v_mac_f32
;
1718 /* handle definitions which must have the same register as an operand */
1719 if (instr
->opcode
== aco_opcode::v_interp_p2_f32
||
1720 instr
->opcode
== aco_opcode::v_mac_f32
||
1721 instr
->opcode
== aco_opcode::v_writelane_b32
||
1722 instr
->opcode
== aco_opcode::v_writelane_b32_e64
) {
1723 instr
->definitions
[0].setFixed(instr
->operands
[2].physReg());
1724 } else if (instr
->opcode
== aco_opcode::s_addk_i32
||
1725 instr
->opcode
== aco_opcode::s_mulk_i32
) {
1726 instr
->definitions
[0].setFixed(instr
->operands
[0].physReg());
1727 } else if (instr
->format
== Format::MUBUF
&&
1728 instr
->definitions
.size() == 1 &&
1729 instr
->operands
.size() == 4) {
1730 instr
->definitions
[0].setFixed(instr
->operands
[3].physReg());
1731 } else if (instr
->format
== Format::MIMG
&&
1732 instr
->definitions
.size() == 1 &&
1733 instr
->operands
[1].regClass().type() == RegType::vgpr
) {
1734 instr
->definitions
[0].setFixed(instr
->operands
[1].physReg());
1737 ctx
.defs_done
.reset();
1739 /* handle fixed definitions first */
1740 for (unsigned i
= 0; i
< instr
->definitions
.size(); ++i
) {
1741 auto& definition
= instr
->definitions
[i
];
1742 if (!definition
.isFixed())
1745 adjust_max_used_regs(ctx
, definition
.regClass(), definition
.physReg());
1746 /* check if the target register is blocked */
1747 if (register_file
[definition
.physReg().reg()] != 0) {
1748 /* create parallelcopy pair to move blocking var */
1749 Temp tmp
= {register_file
[definition
.physReg()], ctx
.assignments
[register_file
[definition
.physReg()]].rc
};
1750 Operand pc_op
= Operand(tmp
);
1751 pc_op
.setFixed(ctx
.assignments
[register_file
[definition
.physReg().reg()]].reg
);
1752 RegClass rc
= pc_op
.regClass();
1753 tmp
= Temp
{program
->allocateId(), rc
};
1754 Definition pc_def
= Definition(tmp
);
1756 /* re-enable the killed operands, so that we don't move the blocking var there */
1757 for (const Operand
& op
: instr
->operands
) {
1758 if (op
.isTemp() && op
.isFirstKillBeforeDef())
1759 register_file
.fill(op
);
1762 /* find a new register for the blocking variable */
1763 PhysReg reg
= get_reg(ctx
, register_file
, pc_op
.getTemp(), parallelcopy
, instr
);
1764 /* once again, disable killed operands */
1765 for (const Operand
& op
: instr
->operands
) {
1766 if (op
.isTemp() && op
.isFirstKillBeforeDef())
1767 register_file
.clear(op
);
1769 for (unsigned k
= 0; k
< i
; k
++) {
1770 if (instr
->definitions
[k
].isTemp() && ctx
.defs_done
.test(k
) && !instr
->definitions
[k
].isKill())
1771 register_file
.fill(instr
->definitions
[k
]);
1773 pc_def
.setFixed(reg
);
1775 /* finish assignment of parallelcopy */
1776 ctx
.assignments
.emplace_back(reg
, pc_def
.regClass());
1777 assert(ctx
.assignments
.size() == ctx
.program
->peekAllocationId());
1778 parallelcopy
.emplace_back(pc_op
, pc_def
);
1780 /* add changes to reg_file */
1781 register_file
.clear(pc_op
);
1782 register_file
.fill(pc_def
);
1784 ctx
.defs_done
.set(i
);
1786 if (!definition
.isTemp())
1789 /* set live if it has a kill point */
1790 if (!definition
.isKill())
1791 live
.emplace(definition
.getTemp());
1793 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1794 register_file
.fill(definition
);
1797 /* handle all other definitions */
1798 for (unsigned i
= 0; i
< instr
->definitions
.size(); ++i
) {
1799 auto& definition
= instr
->definitions
[i
];
1801 if (definition
.isFixed() || !definition
.isTemp())
1805 if (definition
.hasHint() && register_file
[definition
.physReg().reg()] == 0)
1806 definition
.setFixed(definition
.physReg());
1807 else if (instr
->opcode
== aco_opcode::p_split_vector
) {
1808 PhysReg reg
= instr
->operands
[0].physReg();
1809 reg
.reg_b
+= i
* definition
.bytes();
1810 if (get_reg_specified(ctx
, register_file
, definition
.regClass(), parallelcopy
, instr
, reg
))
1811 definition
.setFixed(reg
);
1812 } else if (instr
->opcode
== aco_opcode::p_wqm
) {
1814 if (instr
->operands
[0].isKillBeforeDef() && instr
->operands
[0].getTemp().type() == definition
.getTemp().type()) {
1815 reg
= instr
->operands
[0].physReg();
1816 definition
.setFixed(reg
);
1817 assert(register_file
[reg
.reg()] == 0);
1819 } else if (instr
->opcode
== aco_opcode::p_extract_vector
) {
1821 if (instr
->operands
[0].isKillBeforeDef() &&
1822 instr
->operands
[0].getTemp().type() == definition
.getTemp().type()) {
1823 reg
= instr
->operands
[0].physReg();
1824 reg
.reg_b
+= definition
.bytes() * instr
->operands
[1].constantValue();
1825 assert(!register_file
.test(reg
, definition
.bytes()));
1826 definition
.setFixed(reg
);
1828 } else if (instr
->opcode
== aco_opcode::p_create_vector
) {
1829 PhysReg reg
= get_reg_create_vector(ctx
, register_file
, definition
.getTemp(),
1830 parallelcopy
, instr
);
1831 definition
.setFixed(reg
);
1834 if (!definition
.isFixed())
1835 definition
.setFixed(get_reg(ctx
, register_file
, definition
.getTemp(), parallelcopy
, instr
));
1837 assert(definition
.isFixed() && ((definition
.getTemp().type() == RegType::vgpr
&& definition
.physReg() >= 256) ||
1838 (definition
.getTemp().type() != RegType::vgpr
&& definition
.physReg() < 256)));
1839 ctx
.defs_done
.set(i
);
1841 /* set live if it has a kill point */
1842 if (!definition
.isKill())
1843 live
.emplace(definition
.getTemp());
1845 ctx
.assignments
[definition
.tempId()] = {definition
.physReg(), definition
.regClass()};
1846 register_file
.fill(definition
);
1849 handle_pseudo(ctx
, register_file
, instr
.get());
1851 /* kill definitions and late-kill operands */
1852 for (const Definition
& def
: instr
->definitions
) {
1853 if (def
.isTemp() && def
.isKill())
1854 register_file
.clear(def
);
1856 for (const Operand
& op
: instr
->operands
) {
1857 if (op
.isTemp() && op
.isFirstKill() && op
.isLateKill())
1858 register_file
.clear(op
);
1861 /* emit parallelcopy */
1862 if (!parallelcopy
.empty()) {
1863 aco_ptr
<Pseudo_instruction
> pc
;
1864 pc
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_parallelcopy
, Format::PSEUDO
, parallelcopy
.size(), parallelcopy
.size()));
1865 bool temp_in_scc
= register_file
[scc
.reg()];
1866 bool sgpr_operands_alias_defs
= false;
1867 uint64_t sgpr_operands
[4] = {0, 0, 0, 0};
1868 for (unsigned i
= 0; i
< parallelcopy
.size(); i
++) {
1869 if (temp_in_scc
&& parallelcopy
[i
].first
.isTemp() && parallelcopy
[i
].first
.getTemp().type() == RegType::sgpr
) {
1870 if (!sgpr_operands_alias_defs
) {
1871 unsigned reg
= parallelcopy
[i
].first
.physReg().reg();
1872 unsigned size
= parallelcopy
[i
].first
.getTemp().size();
1873 sgpr_operands
[reg
/ 64u] |= ((1u << size
) - 1) << (reg
% 64u);
1875 reg
= parallelcopy
[i
].second
.physReg().reg();
1876 size
= parallelcopy
[i
].second
.getTemp().size();
1877 if (sgpr_operands
[reg
/ 64u] & ((1u << size
) - 1) << (reg
% 64u))
1878 sgpr_operands_alias_defs
= true;
1882 pc
->operands
[i
] = parallelcopy
[i
].first
;
1883 pc
->definitions
[i
] = parallelcopy
[i
].second
;
1884 assert(pc
->operands
[i
].size() == pc
->definitions
[i
].size());
1886 /* it might happen that the operand is already renamed. we have to restore the original name. */
1887 std::unordered_map
<unsigned, Temp
>::iterator it
= ctx
.orig_names
.find(pc
->operands
[i
].tempId());
1888 Temp orig
= it
!= ctx
.orig_names
.end() ? it
->second
: pc
->operands
[i
].getTemp();
1889 ctx
.orig_names
[pc
->definitions
[i
].tempId()] = orig
;
1890 ctx
.renames
[block
.index
][orig
.id()] = pc
->definitions
[i
].getTemp();
1892 std::unordered_map
<unsigned, phi_info
>::iterator phi
= ctx
.phi_map
.find(pc
->operands
[i
].tempId());
1893 if (phi
!= ctx
.phi_map
.end())
1894 phi
->second
.uses
.emplace(pc
.get());
1897 if (temp_in_scc
&& sgpr_operands_alias_defs
) {
1898 /* disable definitions and re-enable operands */
1899 for (const Definition
& def
: instr
->definitions
) {
1900 if (def
.isTemp() && !def
.isKill())
1901 register_file
.clear(def
);
1903 for (const Operand
& op
: instr
->operands
) {
1904 if (op
.isTemp() && op
.isFirstKill())
1905 register_file
.block(op
.physReg(), op
.bytes());
1908 handle_pseudo(ctx
, register_file
, pc
.get());
1910 /* re-enable live vars */
1911 for (const Operand
& op
: instr
->operands
) {
1912 if (op
.isTemp() && op
.isFirstKill())
1913 register_file
.clear(op
);
1915 for (const Definition
& def
: instr
->definitions
) {
1916 if (def
.isTemp() && !def
.isKill())
1917 register_file
.fill(def
);
1920 pc
->tmp_in_scc
= false;
1923 instructions
.emplace_back(std::move(pc
));
1926 /* some instructions need VOP3 encoding if operand/definition is not assigned to VCC */
1927 bool instr_needs_vop3
= !instr
->isVOP3() &&
1928 ((instr
->format
== Format::VOPC
&& !(instr
->definitions
[0].physReg() == vcc
)) ||
1929 (instr
->opcode
== aco_opcode::v_cndmask_b32
&& !(instr
->operands
[2].physReg() == vcc
)) ||
1930 ((instr
->opcode
== aco_opcode::v_add_co_u32
||
1931 instr
->opcode
== aco_opcode::v_addc_co_u32
||
1932 instr
->opcode
== aco_opcode::v_sub_co_u32
||
1933 instr
->opcode
== aco_opcode::v_subb_co_u32
||
1934 instr
->opcode
== aco_opcode::v_subrev_co_u32
||
1935 instr
->opcode
== aco_opcode::v_subbrev_co_u32
) &&
1936 !(instr
->definitions
[1].physReg() == vcc
)) ||
1937 ((instr
->opcode
== aco_opcode::v_addc_co_u32
||
1938 instr
->opcode
== aco_opcode::v_subb_co_u32
||
1939 instr
->opcode
== aco_opcode::v_subbrev_co_u32
) &&
1940 !(instr
->operands
[2].physReg() == vcc
)));
1941 if (instr_needs_vop3
) {
1943 /* if the first operand is a literal, we have to move it to a reg */
1944 if (instr
->operands
.size() && instr
->operands
[0].isLiteral() && program
->chip_class
< GFX10
) {
1945 bool can_sgpr
= true;
1946 /* check, if we have to move to vgpr */
1947 for (const Operand
& op
: instr
->operands
) {
1948 if (op
.isTemp() && op
.getTemp().type() == RegType::sgpr
) {
1953 /* disable definitions and re-enable operands */
1954 for (const Definition
& def
: instr
->definitions
)
1955 register_file
.clear(def
);
1956 for (const Operand
& op
: instr
->operands
) {
1957 if (op
.isTemp() && op
.isFirstKill())
1958 register_file
.block(op
.physReg(), op
.bytes());
1960 Temp tmp
= {program
->allocateId(), can_sgpr
? s1
: v1
};
1961 ctx
.assignments
.emplace_back();
1962 PhysReg reg
= get_reg(ctx
, register_file
, tmp
, parallelcopy
, instr
);
1964 aco_ptr
<Instruction
> mov
;
1966 mov
.reset(create_instruction
<SOP1_instruction
>(aco_opcode::s_mov_b32
, Format::SOP1
, 1, 1));
1968 mov
.reset(create_instruction
<VOP1_instruction
>(aco_opcode::v_mov_b32
, Format::VOP1
, 1, 1));
1969 mov
->operands
[0] = instr
->operands
[0];
1970 mov
->definitions
[0] = Definition(tmp
);
1971 mov
->definitions
[0].setFixed(reg
);
1973 instr
->operands
[0] = Operand(tmp
);
1974 instr
->operands
[0].setFixed(reg
);
1975 instructions
.emplace_back(std::move(mov
));
1976 /* re-enable live vars */
1977 for (const Operand
& op
: instr
->operands
) {
1978 if (op
.isTemp() && op
.isFirstKill())
1979 register_file
.clear(op
);
1981 for (const Definition
& def
: instr
->definitions
) {
1982 if (def
.isTemp() && !def
.isKill())
1983 register_file
.fill(def
);
1987 /* change the instruction to VOP3 to enable an arbitrary register pair as dst */
1988 aco_ptr
<Instruction
> tmp
= std::move(instr
);
1989 Format format
= asVOP3(tmp
->format
);
1990 instr
.reset(create_instruction
<VOP3A_instruction
>(tmp
->opcode
, format
, tmp
->operands
.size(), tmp
->definitions
.size()));
1991 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
1992 Operand
& operand
= tmp
->operands
[i
];
1993 instr
->operands
[i
] = operand
;
1994 /* keep phi_map up to date */
1995 if (operand
.isTemp()) {
1996 std::unordered_map
<unsigned, phi_info
>::iterator phi
= ctx
.phi_map
.find(operand
.tempId());
1997 if (phi
!= ctx
.phi_map
.end()) {
1998 phi
->second
.uses
.erase(tmp
.get());
1999 phi
->second
.uses
.emplace(instr
.get());
2003 std::copy(tmp
->definitions
.begin(), tmp
->definitions
.end(), instr
->definitions
.begin());
2005 instructions
.emplace_back(std::move(*it
));
2007 } /* end for Instr */
2009 block
.instructions
= std::move(instructions
);
2011 ctx
.filled
[block
.index
] = true;
2012 for (unsigned succ_idx
: block
.linear_succs
) {
2013 Block
& succ
= program
->blocks
[succ_idx
];
2014 /* seal block if all predecessors are filled */
2015 bool all_filled
= true;
2016 for (unsigned pred_idx
: succ
.linear_preds
) {
2017 if (!ctx
.filled
[pred_idx
]) {
2023 ctx
.sealed
[succ_idx
] = true;
2025 /* finish incomplete phis and check if they became trivial */
2026 for (Instruction
* phi
: ctx
.incomplete_phis
[succ_idx
]) {
2027 std::vector
<unsigned> preds
= phi
->definitions
[0].getTemp().is_linear() ? succ
.linear_preds
: succ
.logical_preds
;
2028 for (unsigned i
= 0; i
< phi
->operands
.size(); i
++) {
2029 phi
->operands
[i
].setTemp(read_variable(ctx
, phi
->operands
[i
].getTemp(), preds
[i
]));
2030 phi
->operands
[i
].setFixed(ctx
.assignments
[phi
->operands
[i
].tempId()].reg
);
2032 try_remove_trivial_phi(ctx
, phi
->definitions
[0].getTemp());
2034 /* complete the original phi nodes, but no need to check triviality */
2035 for (aco_ptr
<Instruction
>& instr
: succ
.instructions
) {
2038 std::vector
<unsigned> preds
= instr
->opcode
== aco_opcode::p_phi
? succ
.logical_preds
: succ
.linear_preds
;
2040 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
2041 auto& operand
= instr
->operands
[i
];
2042 if (!operand
.isTemp())
2044 operand
.setTemp(read_variable(ctx
, operand
.getTemp(), preds
[i
]));
2045 operand
.setFixed(ctx
.assignments
[operand
.tempId()].reg
);
2046 std::unordered_map
<unsigned, phi_info
>::iterator phi
= ctx
.phi_map
.find(operand
.getTemp().id());
2047 if (phi
!= ctx
.phi_map
.end())
2048 phi
->second
.uses
.emplace(instr
.get());
2055 /* remove trivial phis */
2056 for (Block
& block
: program
->blocks
) {
2057 auto end
= std::find_if(block
.instructions
.begin(), block
.instructions
.end(),
2058 [](aco_ptr
<Instruction
>& instr
) { return !is_phi(instr
);});
2059 auto middle
= std::remove_if(block
.instructions
.begin(), end
,
2060 [](const aco_ptr
<Instruction
>& instr
) { return instr
->definitions
.empty();});
2061 block
.instructions
.erase(middle
, end
);
2064 /* find scc spill registers which may be needed for parallelcopies created by phis */
2065 for (Block
& block
: program
->blocks
) {
2066 if (block
.linear_preds
.size() <= 1)
2069 std::bitset
<128> regs
= sgpr_live_in
[block
.index
];
2073 /* choose a register */
2075 for (; reg
< ctx
.program
->max_reg_demand
.sgpr
&& regs
[reg
]; reg
++)
2077 assert(reg
< ctx
.program
->max_reg_demand
.sgpr
);
2078 adjust_max_used_regs(ctx
, s1
, reg
);
2080 /* update predecessors */
2081 for (unsigned& pred_index
: block
.linear_preds
) {
2082 Block
& pred
= program
->blocks
[pred_index
];
2083 pred
.scc_live_out
= true;
2084 pred
.scratch_sgpr
= PhysReg
{(uint16_t)reg
};
2088 /* num_gpr = rnd_up(max_used_gpr + 1) */
2089 program
->config
->num_vgprs
= align(ctx
.max_used_vgpr
+ 1, 4);
2090 if (program
->family
== CHIP_TONGA
|| program
->family
== CHIP_ICELAND
) /* workaround hardware bug */
2091 program
->config
->num_sgprs
= get_sgpr_alloc(program
, program
->sgpr_limit
);
2093 program
->config
->num_sgprs
= align(ctx
.max_used_sgpr
+ 1 + get_extra_sgprs(program
), 8);