From patchwork Mon Dec 18 21:27:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uros Bizjak X-Patchwork-Id: 180654 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1543205dyi; Mon, 18 Dec 2023 13:27:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IHfrf76Vn8ixa+rKdk9+bCJb2qIBi03YHq8xIvbF03fKY49DedWpkv+9S3a7r+4tbKvsUdZ X-Received: by 2002:a25:941:0:b0:dbd:49ce:d308 with SMTP id u1-20020a250941000000b00dbd49ced308mr778584ybm.91.1702934877305; Mon, 18 Dec 2023 13:27:57 -0800 (PST) Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ka18-20020a05622a441200b00425781d482fsi22773860qtb.134.2023.12.18.13.27.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 13:27:57 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20230601 header.b=aU2Q2qO0; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 097FA3857BB0 for ; Mon, 18 Dec 2023 21:27:57 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by sourceware.org (Postfix) with ESMTPS id 874983858C78 for ; Mon, 18 Dec 2023 21:27:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 874983858C78 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 874983858C78 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::534 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702934854; cv=none; b=u8BmjNfvFrjQGW8yViu58VHVbaG8NZ27Y70ip7m0tsz7XvjUy/EgGQlqPsK949J00Sc+MoLFTkkVUserdZBmWjN8j+Yb6kJY/LTSfCcKQjWVbBZf5tMFGAplamDlrZKDP0afr3gCJpkH9cfsYWn0bDz8ySL1LQW4I6J9rcUxI9o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702934854; c=relaxed/simple; bh=8fDrJdE6dXcQWiG88MOr3FfugVqTHNKv2AyvxuosTAU=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=ry5pOMhDanxjaWCMMn85ClEIg+y4xkqcKg+nswlh3OMLTosLt5XeP6gAbBxNScm0Le2IbHJn2xPkJSTQORx9YVeQLGYv+8Vj9zoLADeBhnScLCz775xMB+WgLw8nd94AY3FonofRvfGK0e/jluY/hydy1P2eVSNPM1lwBWvpJoU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-55114c073b8so4573710a12.1 for ; Mon, 18 Dec 2023 13:27:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702934851; x=1703539651; darn=gcc.gnu.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=CWiXPyKhzYgLnTkX/Qr4sNHNF7gUt2wqV/IU7whJIcY=; b=aU2Q2qO0QckFICgaxgJdVeWDskD/UlbDTUygHydpvhOIOmPmGE6hnrpr+JM+Wk4uIg j3vUffPsm3e1Eeny/Q4vqXWV9+4tj+S+3AlCY4Dx8OV4YGKjn2CQiX0QpLdim3A+jaeQ IsfC6Wq688x/+IYO0UulS3rG5TKzYwkfT0DAvHefnUUNj5Hwrr2mAKlq/ZsMTgJ0QRw8 ks0JhzQrCoplhDaXkKAyYe1Wgc4meB89DtK1Qg+gtdojQ1pEouZx9DET8ofcg2DH1z/k 3yOLZjDrLp08e5NPcRAGOQY1e9prj6K/ixLOohy+r/KgsmVoLElmlwPED26WOECCcnKf Iekg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702934851; x=1703539651; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=CWiXPyKhzYgLnTkX/Qr4sNHNF7gUt2wqV/IU7whJIcY=; b=jDH+j7aCGNkySPtVfcy5AeXOg0SRswyvqGhpupuPXCzjZu7oa3hwiJwhaUXiq0tDH0 mfz5J2A+4tb5DAzHhdM7s9OhDKbToD60Mz5ZtbvtQ0YrO6LCDI87BI6zMVYL5vde0e1r 2BNznMPOQwoD+4XCHMd/TRO7XYAoW06sDtqGh9PUx6AOVi/+3xCkP53fHlnUg/L/iLB8 iA6/t1kdb68IOkB7Vjb6SKdCqqP9IfytB5YHB1OEqW7OriHctViQmam4VUjYDZwdlx8b AyVxCYRAJ4gzF0+6iMMZJQoCLNYM5lhYdmLP7AGq49HYCfEHafY2bL8IIFOhJYB5hPs1 2xSA== X-Gm-Message-State: AOJu0Yz5xETNk2yJXbiXoRK1A+OtDQJJ0r3Kr795kqeOnDMc9YD/e/gD 3WGTmsBJBxY8h6JgjMMZ6h1MTR7RozJSlVgfPxNAmw/GEtexiQ== X-Received: by 2002:a50:8e12:0:b0:54c:5419:c16c with SMTP id 18-20020a508e12000000b0054c5419c16cmr6688737edw.70.1702934850704; Mon, 18 Dec 2023 13:27:30 -0800 (PST) MIME-Version: 1.0 From: Uros Bizjak Date: Mon, 18 Dec 2023 22:27:19 +0100 Message-ID: Subject: [committed] i386: Eliminate redundant compare between set{z, nz} and j{z, nz} To: "gcc-patches@gcc.gnu.org" X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785656642145989630 X-GMAIL-MSGID: 1785656642145989630 Eliminate redundant compare between set{z,nz} and j{z,nz}: setz %al; test %al,%al; jz <...> -> setz %al; jnz <...> and setnz %al, test %al,%al; jz <...> -> setnz %al; jz <...>. We can use the original Zero-flag value instead of setting the temporary register and testing it for zero. gcc/ChangeLog: * config/i386/i386.md (redundant compare peephole2): New peephole2 pattern. Bootstrapped and regression tested on x86_64-linux-gnu {,-m32}. Also tested by building and booting the linux kernel (where the peephole2 triggers several times). I was not able to craft the testcase, the original sequence is very elusive and requires reuse of the value, set from the flags elsewhere in the function in a very specific way. Uros. diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index f83064ec335..e862368113c 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -18118,6 +18118,35 @@ (define_split FAIL; }) +;; Eliminate redundant compare between set{z,nz} and j{z,nz}: +;; setz %al; test %al,%al; jz <...> -> setz %al; jnz <...> and +;; setnz %al, test %al,%al; jz <...> -> setnz %al; jz <...>. +(define_peephole2 + [(set (match_operand:QI 0 "nonimmediate_operand") + (match_operator:QI 1 "bt_comparison_operator" + [(reg:CCZ FLAGS_REG) (const_int 0)])) + (set (reg:CCZ FLAGS_REG) + (compare:CCZ (match_dup 0) (const_int 0))) + (set (pc) + (if_then_else (match_operator 2 "bt_comparison_operator" + [(reg:CCZ FLAGS_REG) (const_int 0)]) + (match_operand 3) + (pc)))] + "peep2_regno_dead_p (3, FLAGS_REG)" + [(set (match_dup 0) + (match_op_dup 1 [(reg:CCZ FLAGS_REG) (const_int 0)])) + (set (pc) + (if_then_else (match_dup 2) + (match_dup 3) + (pc)))] +{ + if (GET_CODE (operands[1]) == EQ) + { + operands[2] = shallow_copy_rtx (operands[2]); + PUT_CODE (operands[2], reverse_condition (GET_CODE (operands[2]))); + } +}) + ;; The SSE store flag instructions saves 0 or 0xffffffff to the result. ;; subsequent logical operations are used to imitate conditional moves. ;; 0xffffffff is NaN, but not in normalized form, so we can't represent