diff --git a/src/amd/compiler/aco_opt_value_numbering.cpp b/src/amd/compiler/aco_opt_value_numbering.cpp index 4e3e3548429..4f46f386d69 100644 --- a/src/amd/compiler/aco_opt_value_numbering.cpp +++ b/src/amd/compiler/aco_opt_value_numbering.cpp @@ -313,6 +313,21 @@ can_eliminate(aco_ptr& instr) return true; } +bool +is_trivial_phi(Block& block, Instruction* instr) +{ + if (!is_phi(instr)) + return false; + + /* Logical LCSSA phis must be kept in order to prevent the optimizer + * from doing invalid transformations. */ + if (instr->opcode == aco_opcode::p_phi && (block.kind & block_kind_loop_exit)) + return false; + + return std::all_of(instr->operands.begin(), instr->operands.end(), + [&](Operand& op) { return op == instr->operands[0]; }); +} + void process_block(vn_ctx& ctx, Block& block) { @@ -333,14 +348,9 @@ process_block(vn_ctx& ctx, Block& block) instr->opcode == aco_opcode::p_demote_to_helper || instr->opcode == aco_opcode::p_end_wqm) ctx.exec_id++; - if (!can_eliminate(instr)) { - new_instructions.emplace_back(std::move(instr)); - continue; - } - /* simple copy-propagation through renaming */ bool copy_instr = - instr->opcode == aco_opcode::p_parallelcopy || + is_trivial_phi(block, instr.get()) || instr->opcode == aco_opcode::p_parallelcopy || (instr->opcode == aco_opcode::p_create_vector && instr->operands.size() == 1); if (copy_instr && !instr->definitions[0].isFixed() && instr->operands[0].isTemp() && instr->operands[0].regClass() == instr->definitions[0].regClass()) { @@ -348,6 +358,11 @@ process_block(vn_ctx& ctx, Block& block) continue; } + if (!can_eliminate(instr)) { + new_instructions.emplace_back(std::move(instr)); + continue; + } + instr->pass_flags = ctx.exec_id; std::pair res = ctx.expr_values.emplace(instr.get(), block.index); @@ -388,7 +403,7 @@ void rename_phi_operands(Block& block, aco::unordered_map& renames) { for (aco_ptr& phi : block.instructions) { - if (phi->opcode != aco_opcode::p_phi && phi->opcode != aco_opcode::p_linear_phi) + if (!is_phi(phi)) break; for (Operand& op : phi->operands) {