From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id 642123992015 for ; Thu, 22 Jul 2021 12:12:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 642123992015 Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16MC4USR043349; Thu, 22 Jul 2021 08:12:50 -0400 Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0b-001b2d01.pphosted.com with ESMTP id 39y5vk4uce-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 22 Jul 2021 08:12:50 -0400 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 16MC6wtg027780; Thu, 22 Jul 2021 12:12:48 GMT Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by ppma04ams.nl.ibm.com with ESMTP id 39xhx48mdh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 22 Jul 2021 12:12:48 +0000 Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 16MCCkWt18940208 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 22 Jul 2021 12:12:46 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 54AB8AE04D; Thu, 22 Jul 2021 12:12:46 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 15C2DAE053; Thu, 22 Jul 2021 12:12:46 +0000 (GMT) Received: from li-926bd7cc-2dd1-11b2-a85c-f6adc0f5efec.ibm.com (unknown [9.171.28.15]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Thu, 22 Jul 2021 12:12:45 +0000 (GMT) Subject: Re: [PATCH 5/7] ifcvt: Try re-using CC for conditional moves. To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com References: <20210625160905.23786-1-rdapp@linux.ibm.com> <20210625160905.23786-6-rdapp@linux.ibm.com> From: Robin Dapp Message-ID: Date: Thu, 22 Jul 2021 14:12:45 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <20210625160905.23786-6-rdapp@linux.ibm.com> Content-Type: multipart/mixed; boundary="------------06F4EEE988138A7DC39C0A02" Content-Language: en-US X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: 39BIhuYHmSlrIVOYyknLO8aFDMJNTLgg X-Proofpoint-GUID: 39BIhuYHmSlrIVOYyknLO8aFDMJNTLgg X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391, 18.0.790 definitions=2021-07-22_04:2021-07-22, 2021-07-22 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 suspectscore=0 bulkscore=0 clxscore=1015 spamscore=0 phishscore=0 mlxlogscore=999 mlxscore=0 adultscore=0 priorityscore=1501 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2107220081 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 22 Jul 2021 12:12:54 -0000 This is a multi-part message in MIME format. --------------06F4EEE988138A7DC39C0A02 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi, v2 now properly gets the reversed CC comparison. It also handles a cost2 == 0 situation that would prefer an empty seq2 before. Regards Robin --------------06F4EEE988138A7DC39C0A02 Content-Type: text/x-patch; charset=UTF-8; name="v2-0005-ifcvt-Try-re-using-CC-for-conditional-moves.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="v2-0005-ifcvt-Try-re-using-CC-for-conditional-moves.patch" >From 12b796c4e081ba8a2e136958f4bf919c63516de6 Mon Sep 17 00:00:00 2001 From: Robin Dapp Date: Thu, 24 Jun 2021 15:22:42 +0200 Subject: [PATCH v2 5/7] ifcvt: Try re-using CC for conditional moves. Following up on the previous patch, this patch makes noce_convert_multiple emit two cmov sequences: The same one as before and a second one that tries to re-use the existing CC. Then their costs are compared and the cheaper one is selected. --- gcc/ifcvt.c | 108 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 87 insertions(+), 21 deletions(-) diff --git a/gcc/ifcvt.c b/gcc/ifcvt.c index 7ce60d0af8d..a82b41d9e4a 100644 --- a/gcc/ifcvt.c +++ b/gcc/ifcvt.c @@ -83,7 +83,7 @@ static rtx_insn *last_active_insn (basic_block, int); static rtx_insn *find_active_insn_before (basic_block, rtx_insn *); static rtx_insn *find_active_insn_after (basic_block, rtx_insn *); static basic_block block_fallthru (basic_block); -static rtx cond_exec_get_condition (rtx_insn *); +static rtx cond_exec_get_condition (rtx_insn *, bool); static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool); static int noce_operand_ok (const_rtx); static void merge_if_block (ce_if_block *); @@ -426,7 +426,7 @@ cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED, /* Return the condition for a jump. Do not do any special processing. */ static rtx -cond_exec_get_condition (rtx_insn *jump) +cond_exec_get_condition (rtx_insn *jump, bool get_reversed = false) { rtx test_if, cond; @@ -438,8 +438,9 @@ cond_exec_get_condition (rtx_insn *jump) /* If this branches to JUMP_LABEL when the condition is false, reverse the condition. */ - if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF - && label_ref_label (XEXP (test_if, 2)) == JUMP_LABEL (jump)) + if (get_reversed || (GET_CODE (XEXP (test_if, 2)) == LABEL_REF + && label_ref_label (XEXP (test_if, 2)) + == JUMP_LABEL (jump))) { enum rtx_code rev = reversed_comparison_code (cond, jump); if (rev == UNKNOWN) @@ -3144,6 +3145,46 @@ bb_valid_for_noce_process_p (basic_block test_bb, rtx cond, return false; } +/* Helper function to emit a cmov sequence. */ + +static rtx_insn* +try_emit_cmove_seq (struct noce_if_info *if_info, rtx temp, + rtx cond, rtx new_val, rtx old_val, bool need_cmov, + unsigned *cost, rtx *temp_dest, + rtx cc_cmp = NULL, rtx rev_cc_cmp = NULL) +{ + rtx_insn *seq = NULL; + *cost = 0; + + rtx x = XEXP (cond, 0); + rtx y = XEXP (cond, 1); + rtx_code cond_code = GET_CODE (cond); + + start_sequence (); + + if (need_cmov) + *temp_dest = noce_emit_cmove (if_info, temp, cond_code, + x, y, new_val, old_val, cc_cmp, rev_cc_cmp); + else + { + *temp_dest = temp; + if (if_info->then_else_reversed) + noce_emit_move_insn (temp, old_val); + else + noce_emit_move_insn (temp, new_val); + } + + if (*temp_dest != NULL_RTX) + { + seq = get_insns (); + *cost = seq_cost (seq, if_info->speed_p); + } + + end_sequence (); + + return seq; +} + /* We have something like: if (x > y) @@ -3201,7 +3242,9 @@ noce_convert_multiple_sets (struct noce_if_info *if_info) rtx cond = noce_get_condition (jump, &cond_earliest, false); rtx x = XEXP (cond, 0); rtx y = XEXP (cond, 1); - rtx_code cond_code = GET_CODE (cond); + + rtx cc_cmp = cond_exec_get_condition (jump); + rtx rev_cc_cmp = cond_exec_get_condition (jump, true); /* The true targets for a conditional move. */ auto_vec targets; @@ -3312,31 +3355,53 @@ noce_convert_multiple_sets (struct noce_if_info *if_info) old_val = lowpart_subreg (dst_mode, old_val, src_mode); } - rtx temp_dest = NULL_RTX; + /* Try emitting a conditional move passing the backend the + canonicalized comparison. The backend is then able to + recognize expressions like - if (need_cmov) + if (x > y) + y = x; + + as min/max and emit an insn, accordingly. + We will still emit a superfluous CC comparison before the + min/max, though, which complicates costing. */ + unsigned cost1 = 0, cost2 = 0; + rtx_insn *seq, *seq1, *seq2; + rtx temp_dest = NULL_RTX, temp_dest1 = NULL_RTX, temp_dest2 = NULL_RTX; + + seq1 = try_emit_cmove_seq (if_info, temp, cond, + new_val, old_val, need_cmov, + &cost1, &temp_dest1); + + /* Here, we try to pass the backend a non-canonicalized cc comparison + as well. This allows the backend to emit a cmov directly without + creating an additional compare for each. If successful, costing + is easier and this sequence is usually preferred. */ + seq2 = try_emit_cmove_seq (if_info, target, cond, + new_val, old_val, need_cmov, + &cost2, &temp_dest2, cc_cmp, rev_cc_cmp); + + /* Check which version is less expensive. */ + if (seq1 != NULL_RTX && (cost1 <= cost2 || seq2 == NULL_RTX)) { - /* Actually emit the conditional move. */ - temp_dest = noce_emit_cmove (if_info, temp, cond_code, - x, y, new_val, old_val); + seq = seq1; + temp_dest = temp_dest1; } - else + else if (seq2 != NULL_RTX) { - if (if_info->then_else_reverse) - noce_emit_move_insn (temp, old_val); - else - noce_emit_move_insn (temp, new_val); - temp_dest = temp; + seq = seq2; + temp_dest = temp_dest2; } - - /* If we failed to expand the conditional move, drop out and don't - try to continue. */ - if (temp_dest == NULL_RTX) + else { + /* Nothing worked, bail out. */ end_sequence (); return FALSE; } + /* End the sub sequence and emit to the main sequence. */ + emit_insn (seq); + /* Bookkeeping. */ count++; targets.safe_push (target); @@ -3350,7 +3415,8 @@ noce_convert_multiple_sets (struct noce_if_info *if_info) /* Now fixup the assignments. */ for (int i = 0; i < count; i++) - noce_emit_move_insn (targets[i], temporaries[i]); + if (targets[i] != temporaries[i]) + noce_emit_move_insn (targets[i], temporaries[i]); /* Actually emit the sequence if it isn't too expensive. */ rtx_insn *seq = get_insns (); -- 2.31.1 --------------06F4EEE988138A7DC39C0A02--