From nobody Tue Nov 26 14:52:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1707082905; cv=none; d=zohomail.com; s=zohoarc; b=CmEFyaCvsOyqhcNGHMh6G61hDGY/KZY9JYinw9kgPjyH7fMW7j5D0k+mU/SZ0GVAgn1+e3jfjUDu1rcdAH8hBblwrrbRg81L+Rj+QZCRHPpaACpm9giLCkd9RU6o1hQQl2tlv60gu+sHEGuuXnf/SHMIs3raSuuSXyh1sAYDs2Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707082905; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=4WPFWKge9Cibe1+P1uYI+mpRsrAd20eTdoBjuJnsUMU=; b=VxiON2jU4vw1iq+eXTnjGl6Dbucmhaa4IQ4uqRs+r+9hyslq9NqSBLStRiGVgPocvTp7LqFxbW6T/GXuN/qdptZr6bC19Q+tzZUCZIvkggNuf4oVNrNVZZyl5cLRW7qLb9A/tOMY2NNNT+IM+2EnWNbMGp3/4Kz/ooUJDLMxOEs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1707082905131945.6214380433781; Sun, 4 Feb 2024 13:41:45 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rWkEm-000234-Mr; Sun, 04 Feb 2024 16:41:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rWkEi-000227-Jc for qemu-devel@nongnu.org; Sun, 04 Feb 2024 16:41:12 -0500 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rWkEg-0003VR-JW for qemu-devel@nongnu.org; Sun, 04 Feb 2024 16:41:12 -0500 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1d9bd8fa49eso665545ad.1 for ; Sun, 04 Feb 2024 13:41:10 -0800 (PST) Received: from stoup.. ([43.252.112.156]) by smtp.gmail.com with ESMTPSA id mf11-20020a170902fc8b00b001d8f5560503sm4953670plb.222.2024.02.04.13.41.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 13:41:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1707082869; x=1707687669; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4WPFWKge9Cibe1+P1uYI+mpRsrAd20eTdoBjuJnsUMU=; b=ld3cDBD+SiiC7SY5A0XbuTHhk+NDvAEe6U/+M/05h0NlJNEBmSHzfYPpmv6uVM2LK4 ENNyd3M/Jqg840B/ksiVTYBqgCsL4T1EqGeAiDtSv+CAj7O3tbFPke8LkT/PWU84uIGq p99mqrBMQfbHRsZDObi777lYXvVORZbiNNJGuul8ZltjdpNKKQN/M19P6bd26LZ9fSCq S/kqu8T03zFn1LsWDA4gVhUegOK25HYN7cdGY99BXHlf+w446+0cQep5lZULTodHrvCU PQ6V8mnP4p30H8tR19IHe5bqwZDkG4pXwdYaUYvAmSrP/0CiDvFhOb7evlfioesnZq9W 5dTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707082869; x=1707687669; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4WPFWKge9Cibe1+P1uYI+mpRsrAd20eTdoBjuJnsUMU=; b=af3gkRNags7+X0d3mAo6lbgub/MXHolTgKUCiHGLbFM41Bx6tDNqgw7KbTUuyFU1K6 7Sacu0qJoZnZ7dtQ09rfEakdN/02cK2Eom70emKmjZwugR9Yljvf+mWCNGa8lAqTI7d+ ZYLQtbQKLymH3m1oOBNGTN4Ru3iizDWqx8qpRoEl+Ovo68Xsk+z5F2FXAhtFQPLXvQwu jdcG1i9Wkcxv+VQnXpIPzeDLcSfsVHAxYCkI9BKukfLSJwGoiyJctA4yLZcBMIPeCFas UrAz6zpyAPRfWiukZMLglfRsYbUKfCzxpxSytvyyudeEg85b7jeRPljOiYL241pqIxhp jcgg== X-Gm-Message-State: AOJu0YzByKgWEaG39Omroz4uQlIqEzX84uXSaFtpHFbYHOefC3MpaCbO +lau7F4ffL40iP4QHpO2Zwu6HV1liYpRCFsQYXA8mNyI3GRJhFFaAPzO1jvExdR7J6ANolrAX/7 w0LQ= X-Google-Smtp-Source: AGHT+IEyNH/ru1/unQiO1J4UTucbgrgqgQhkTdKfXTVvbSBq03N2YEj7tPfxY2WNFTb85N12mkXk7g== X-Received: by 2002:a17:903:11d2:b0:1d9:5763:d89b with SMTP id q18-20020a17090311d200b001d95763d89bmr8617156plh.47.1707082869369; Sun, 04 Feb 2024 13:41:09 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 06/39] tcg/optimize: Handle TCG_COND_TST{EQ,NE} Date: Mon, 5 Feb 2024 07:40:19 +1000 Message-Id: <20240204214052.5639-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240204214052.5639-1-richard.henderson@linaro.org> References: <20240204214052.5639-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x635.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1707082905916100002 Fold constant comparisons. Canonicalize "tst x,x" to equality vs zero. Canonicalize "tst x,sign" to sign test vs zero. Fold double-word comparisons with zero parts. Fold setcond of "tst x,pow2" to a bit extract. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- tcg/optimize.c | 240 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 218 insertions(+), 22 deletions(-) diff --git a/tcg/optimize.c b/tcg/optimize.c index 08a9280432..2ed6322f97 100644 --- a/tcg/optimize.c +++ b/tcg/optimize.c @@ -625,9 +625,15 @@ static bool do_constant_folding_cond_32(uint32_t x, ui= nt32_t y, TCGCond c) return x <=3D y; case TCG_COND_GTU: return x > y; - default: - g_assert_not_reached(); + case TCG_COND_TSTEQ: + return (x & y) =3D=3D 0; + case TCG_COND_TSTNE: + return (x & y) !=3D 0; + case TCG_COND_ALWAYS: + case TCG_COND_NEVER: + break; } + g_assert_not_reached(); } =20 static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c) @@ -653,12 +659,18 @@ static bool do_constant_folding_cond_64(uint64_t x, u= int64_t y, TCGCond c) return x <=3D y; case TCG_COND_GTU: return x > y; - default: - g_assert_not_reached(); + case TCG_COND_TSTEQ: + return (x & y) =3D=3D 0; + case TCG_COND_TSTNE: + return (x & y) !=3D 0; + case TCG_COND_ALWAYS: + case TCG_COND_NEVER: + break; } + g_assert_not_reached(); } =20 -static bool do_constant_folding_cond_eq(TCGCond c) +static int do_constant_folding_cond_eq(TCGCond c) { switch (c) { case TCG_COND_GT: @@ -673,9 +685,14 @@ static bool do_constant_folding_cond_eq(TCGCond c) case TCG_COND_LEU: case TCG_COND_EQ: return 1; - default: - g_assert_not_reached(); + case TCG_COND_TSTEQ: + case TCG_COND_TSTNE: + return -1; + case TCG_COND_ALWAYS: + case TCG_COND_NEVER: + break; } + g_assert_not_reached(); } =20 /* @@ -703,8 +720,10 @@ static int do_constant_folding_cond(TCGType type, TCGA= rg x, } else if (arg_is_const_val(y, 0)) { switch (c) { case TCG_COND_LTU: + case TCG_COND_TSTNE: return 0; case TCG_COND_GEU: + case TCG_COND_TSTEQ: return 1; default: return -1; @@ -777,7 +796,30 @@ static int do_constant_folding_cond1(OptContext *ctx, = TCGArg dest, } =20 r =3D do_constant_folding_cond(ctx->type, *p1, *p2, cond); - return r; + if (r >=3D 0) { + return r; + } + if (!is_tst_cond(cond)) { + return -1; + } + + /* + * TSTNE x,x -> NE x,0 + * TSTNE x,-1 -> NE x,0 + */ + if (args_are_copies(*p1, *p2) || arg_is_const_val(*p2, -1)) { + *p2 =3D arg_new_constant(ctx, 0); + *pcond =3D tcg_tst_eqne_cond(cond); + return -1; + } + + /* TSTNE x,sign -> LT x,0 */ + if (arg_is_const_val(*p2, (ctx->type =3D=3D TCG_TYPE_I32 + ? INT32_MIN : INT64_MIN))) { + *p2 =3D arg_new_constant(ctx, 0); + *pcond =3D tcg_tst_ltge_cond(cond); + } + return -1; } =20 static int do_constant_folding_cond2(OptContext *ctx, TCGArg *args) @@ -785,6 +827,7 @@ static int do_constant_folding_cond2(OptContext *ctx, T= CGArg *args) TCGArg al, ah, bl, bh; TCGCond c; bool swap; + int r; =20 swap =3D swap_commutative2(args, args + 2); c =3D args[4]; @@ -806,21 +849,54 @@ static int do_constant_folding_cond2(OptContext *ctx,= TCGArg *args) tcg_target_ulong alv =3D arg_info(al)->val; tcg_target_ulong ahv =3D arg_info(ah)->val; uint64_t a =3D deposit64(alv, 32, 32, ahv); - return do_constant_folding_cond_64(a, b, c); + + r =3D do_constant_folding_cond_64(a, b, c); + if (r >=3D 0) { + return r; + } } + if (b =3D=3D 0) { switch (c) { case TCG_COND_LTU: + case TCG_COND_TSTNE: return 0; case TCG_COND_GEU: + case TCG_COND_TSTEQ: return 1; default: break; } } + + /* TSTNE x,-1 -> NE x,0 */ + if (b =3D=3D -1 && is_tst_cond(c)) { + args[3] =3D args[2] =3D arg_new_constant(ctx, 0); + args[4] =3D tcg_tst_eqne_cond(c); + return -1; + } + + /* TSTNE x,sign -> LT x,0 */ + if (b =3D=3D INT64_MIN && is_tst_cond(c)) { + /* bl must be 0, so copy that to bh */ + args[3] =3D bl; + args[4] =3D tcg_tst_ltge_cond(c); + return -1; + } } + if (args_are_copies(al, bl) && args_are_copies(ah, bh)) { - return do_constant_folding_cond_eq(c); + r =3D do_constant_folding_cond_eq(c); + if (r >=3D 0) { + return r; + } + + /* TSTNE x,x -> NE x,0 */ + if (is_tst_cond(c)) { + args[3] =3D args[2] =3D arg_new_constant(ctx, 0); + args[4] =3D tcg_tst_eqne_cond(c); + return -1; + } } return -1; } @@ -1284,24 +1360,37 @@ static bool fold_brcond2(OptContext *ctx, TCGOp *op) case 0: goto do_brcond_const; case 1: - op->opc =3D INDEX_op_brcond_i32; - op->args[1] =3D op->args[2]; - op->args[2] =3D cond; - op->args[3] =3D label; - break; + goto do_brcond_low; + } + break; + + case TCG_COND_TSTEQ: + case TCG_COND_TSTNE: + if (arg_is_const_val(op->args[2], 0)) { + goto do_brcond_high; + } + if (arg_is_const_val(op->args[3], 0)) { + goto do_brcond_low; } break; =20 default: break; =20 + do_brcond_low: + op->opc =3D INDEX_op_brcond_i32; + op->args[1] =3D op->args[2]; + op->args[2] =3D cond; + op->args[3] =3D label; + return fold_brcond(ctx, op); + do_brcond_high: op->opc =3D INDEX_op_brcond_i32; op->args[0] =3D op->args[1]; op->args[1] =3D op->args[3]; op->args[2] =3D cond; op->args[3] =3D label; - break; + return fold_brcond(ctx, op); =20 do_brcond_const: if (i =3D=3D 0) { @@ -1967,6 +2056,99 @@ static bool fold_remainder(OptContext *ctx, TCGOp *o= p) return false; } =20 +static void fold_setcond_tst_pow2(OptContext *ctx, TCGOp *op, bool neg) +{ + TCGOpcode and_opc, sub_opc, xor_opc, neg_opc, shr_opc, uext_opc, sext_= opc; + TCGCond cond =3D op->args[3]; + TCGArg ret, src1, src2; + TCGOp *op2; + uint64_t val; + int sh; + bool inv; + + if (!is_tst_cond(cond) || !arg_is_const(op->args[2])) { + return; + } + + src2 =3D op->args[2]; + val =3D arg_info(src2)->val; + if (!is_power_of_2(val)) { + return; + } + sh =3D ctz64(val); + + switch (ctx->type) { + case TCG_TYPE_I32: + and_opc =3D INDEX_op_and_i32; + sub_opc =3D INDEX_op_sub_i32; + xor_opc =3D INDEX_op_xor_i32; + shr_opc =3D INDEX_op_shr_i32; + neg_opc =3D INDEX_op_neg_i32; + if (TCG_TARGET_extract_i32_valid(sh, 1)) { + uext_opc =3D TCG_TARGET_HAS_extract_i32 ? INDEX_op_extract_i32= : 0; + sext_opc =3D TCG_TARGET_HAS_sextract_i32 ? INDEX_op_sextract_i= 32 : 0; + } + break; + case TCG_TYPE_I64: + and_opc =3D INDEX_op_and_i64; + sub_opc =3D INDEX_op_sub_i64; + xor_opc =3D INDEX_op_xor_i64; + shr_opc =3D INDEX_op_shr_i64; + neg_opc =3D INDEX_op_neg_i64; + if (TCG_TARGET_extract_i64_valid(sh, 1)) { + uext_opc =3D TCG_TARGET_HAS_extract_i64 ? INDEX_op_extract_i64= : 0; + sext_opc =3D TCG_TARGET_HAS_sextract_i64 ? INDEX_op_sextract_i= 64 : 0; + } + break; + default: + g_assert_not_reached(); + } + + ret =3D op->args[0]; + src1 =3D op->args[1]; + inv =3D cond =3D=3D TCG_COND_TSTEQ; + + if (sh && sext_opc && neg && !inv) { + op->opc =3D sext_opc; + op->args[1] =3D src1; + op->args[2] =3D sh; + op->args[3] =3D 1; + return; + } else if (sh && uext_opc) { + op->opc =3D uext_opc; + op->args[1] =3D src1; + op->args[2] =3D sh; + op->args[3] =3D 1; + } else { + if (sh) { + op2 =3D tcg_op_insert_before(ctx->tcg, op, shr_opc, 3); + op2->args[0] =3D ret; + op2->args[1] =3D src1; + op2->args[2] =3D arg_new_constant(ctx, sh); + src1 =3D ret; + } + op->opc =3D and_opc; + op->args[1] =3D src1; + op->args[2] =3D arg_new_constant(ctx, 1); + } + + if (neg && inv) { + op2 =3D tcg_op_insert_after(ctx->tcg, op, sub_opc, 3); + op2->args[0] =3D ret; + op2->args[1] =3D ret; + op2->args[2] =3D arg_new_constant(ctx, 1); + } else if (inv) { + op2 =3D tcg_op_insert_after(ctx->tcg, op, xor_opc, 3); + op2->args[0] =3D ret; + op2->args[1] =3D ret; + op2->args[2] =3D arg_new_constant(ctx, 1); + } else if (neg) { + op2 =3D tcg_op_insert_after(ctx->tcg, op, neg_opc, 2); + op2->args[0] =3D ret; + op2->args[1] =3D ret; + } +} + static bool fold_setcond(OptContext *ctx, TCGOp *op) { int i =3D do_constant_folding_cond1(ctx, op->args[0], &op->args[1], @@ -1974,6 +2156,7 @@ static bool fold_setcond(OptContext *ctx, TCGOp *op) if (i >=3D 0) { return tcg_opt_gen_movi(ctx, op, op->args[0], i); } + fold_setcond_tst_pow2(ctx, op, false); =20 ctx->z_mask =3D 1; ctx->s_mask =3D smask_from_zmask(1); @@ -1987,13 +2170,13 @@ static bool fold_negsetcond(OptContext *ctx, TCGOp = *op) if (i >=3D 0) { return tcg_opt_gen_movi(ctx, op, op->args[0], -i); } + fold_setcond_tst_pow2(ctx, op, true); =20 /* Value is {0,-1} so all bits are repetitions of the sign. */ ctx->s_mask =3D -1; return false; } =20 - static bool fold_setcond2(OptContext *ctx, TCGOp *op) { TCGCond cond; @@ -2041,22 +2224,35 @@ static bool fold_setcond2(OptContext *ctx, TCGOp *o= p) case 0: goto do_setcond_const; case 1: - op->args[2] =3D op->args[3]; - op->args[3] =3D cond; - op->opc =3D INDEX_op_setcond_i32; - break; + goto do_setcond_low; + } + break; + + case TCG_COND_TSTEQ: + case TCG_COND_TSTNE: + if (arg_is_const_val(op->args[2], 0)) { + goto do_setcond_high; + } + if (arg_is_const_val(op->args[4], 0)) { + goto do_setcond_low; } break; =20 default: break; =20 + do_setcond_low: + op->args[2] =3D op->args[3]; + op->args[3] =3D cond; + op->opc =3D INDEX_op_setcond_i32; + return fold_setcond(ctx, op); + do_setcond_high: op->args[1] =3D op->args[2]; op->args[2] =3D op->args[4]; op->args[3] =3D cond; op->opc =3D INDEX_op_setcond_i32; - break; + return fold_setcond(ctx, op); } =20 ctx->z_mask =3D 1; --=20 2.34.1