From nobody Mon Feb 9 15:27:23 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1500440523408633.0947395032158; Tue, 18 Jul 2017 22:02:03 -0700 (PDT) Received: from localhost ([::1]:59908 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dXh7a-0007Ey-2f for importer@patchew.org; Wed, 19 Jul 2017 01:02:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41559) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dXh3U-0003NQ-CJ for qemu-devel@nongnu.org; Wed, 19 Jul 2017 00:57:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dXh3T-00028L-5T for qemu-devel@nongnu.org; Wed, 19 Jul 2017 00:57:48 -0400 Received: from mail-pg0-x243.google.com ([2607:f8b0:400e:c05::243]:35312) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dXh3S-000287-VW for qemu-devel@nongnu.org; Wed, 19 Jul 2017 00:57:47 -0400 Received: by mail-pg0-x243.google.com with SMTP id d193so5425435pgc.2 for ; Tue, 18 Jul 2017 21:57:46 -0700 (PDT) Received: from bigtime.twiddle.net (14-203-207-215.tpgi.com.au. [14.203.207.215]) by smtp.gmail.com with ESMTPSA id m196sm9062855pga.33.2017.07.18.21.57.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Jul 2017 21:57:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VETpF4KbRBHhLUfGgJwCZiI9xUj7wkR5IVZ7KzrZTWQ=; b=j1t6KyoTtzwAxKrhss0/hgldCdOz/kF2MwYyjOO4bYu8E+Vhssh40OLTsQKHGyfICR 1MV9ber2dL9TT9dBH2ATXAPH1VkM+51w4swCVnc0UkeirUEXlw6lCCWLo2+shaxlXhUy SNG6AaEMBnUu4uCBuo61zuywyMYaoSntkib1uAXMQDB8nR/CD4eZEEEGc+1HuxIy9AgP sH3UpgrOnH8TyQDvi2m+Phve27EgPXFGyFwXRRG86KVayYuIm/gs4aVcLl2Q8rE3uGlz XAp4JGw/B8odjnIa9TIQYnFR7mj+5Xya0fKjriAEo/XP2Vc5Qe5m9+lVRX1iBUCxdTPM ockw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=VETpF4KbRBHhLUfGgJwCZiI9xUj7wkR5IVZ7KzrZTWQ=; b=G5vteVII+wvwcT++Qd+VXdtPQ34/KVeOPFkKd7TRgV1XVp2tGSrF8YU9RmwaqZF1Uy fDahE0Ja8/1dglfACQTLH/BIi1maPqx8aRNm8YX5s1qFaffuhu3oWhWu1uS7+zaerqot oaD1PVuN4BzpyK2XC/uQSIcwrQWXsqmpLSTjiI3VgGZkaf7m8/QUZrrrw2CDJ2HEui6W /XM0V3JSt72Ze52O7qtubK7LRptcQYueZTiGMpIOu7ZsfkIZy5xKew07V4AAX3VQikov U4yMbKtXvc6GK8szKbiAw0ZRgCzZQeFFha7XXMnDWVE24wWenvzgVaMyw2ROw2DVEZ/e mn5w== X-Gm-Message-State: AIVw111k15ShJmPAVpl2xDtJigKeKZZTZS/Bx/F6c9r+GxSEikTqoHUF XPeaJsS/IJZsJWDKQho= X-Received: by 10.99.171.1 with SMTP id p1mr1180509pgf.140.1500440265725; Tue, 18 Jul 2017 21:57:45 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 18 Jul 2017 18:57:13 -1000 Message-Id: <20170719045722.25492-6-rth@twiddle.net> X-Mailer: git-send-email 2.9.4 In-Reply-To: <20170719045722.25492-1-rth@twiddle.net> References: <20170719045722.25492-1-rth@twiddle.net> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::243 Subject: [Qemu-devel] [PULL 05/14] coccinelle: add a script to optimize tcg op using tcg_gen_extract() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 From: Philippe Mathieu-Daud=C3=A9 The following thread was helpful while writing this script: https://github.com/coccinelle/coccinelle/issues/86 Signed-off-by: Philippe Mathieu-Daud=C3=A9 Message-Id: <20170718045540.16322-3-f4bug@amsat.org> Signed-off-by: Richard Henderson --- scripts/coccinelle/tcg_gen_extract.cocci | 107 +++++++++++++++++++++++++++= ++++ 1 file changed, 107 insertions(+) create mode 100644 scripts/coccinelle/tcg_gen_extract.cocci diff --git a/scripts/coccinelle/tcg_gen_extract.cocci b/scripts/coccinelle/= tcg_gen_extract.cocci new file mode 100644 index 0000000..81e66a3 --- /dev/null +++ b/scripts/coccinelle/tcg_gen_extract.cocci @@ -0,0 +1,107 @@ +// optimize TCG using extract op +// +// Copyright: (C) 2017 Philippe Mathieu-Daud=C3=A9. GPLv2+. +// Confidence: High +// Options: --macro-file scripts/cocci-macro-file.h +// +// Nikunj A Dadhania optimization: +// http://lists.nongnu.org/archive/html/qemu-devel/2017-02/msg05211.html +// Aurelien Jarno optimization: +// http://lists.nongnu.org/archive/html/qemu-devel/2017-05/msg01466.html +// +// This script can be run either using spatch locally or via a docker imag= e: +// +// $ spatch \ +// --macro-file scripts/cocci-macro-file.h \ +// --sp-file scripts/coccinelle/tcg_gen_extract.cocci \ +// --keep-comments --in-place \ +// --use-gitgrep --dir target +// +// $ docker run --rm -v `pwd`:`pwd` -w `pwd` philmd/coccinelle \ +// --macro-file scripts/cocci-macro-file.h \ +// --sp-file scripts/coccinelle/tcg_gen_extract.cocci \ +// --keep-comments --in-place \ +// --use-gitgrep --dir target + +@initialize:python@ +@@ +import sys +fd =3D sys.stderr +def debug(msg=3D"", trailer=3D"\n"): + fd.write("[DBG] " + msg + trailer) +def low_bits_count(value): + bits_count =3D 0 + while (value & (1 << bits_count)): + bits_count +=3D 1 + return bits_count +def Mn(order): # Mersenne number + return (1 << order) - 1 + +@match@ +identifier ret; +metavariable arg; +constant ofs, msk; +position shr_p, and_p; +@@ +( + tcg_gen_shri_i32@shr_p +| + tcg_gen_shri_i64@shr_p +| + tcg_gen_shri_tl@shr_p +)(ret, arg, ofs); +... WHEN !=3D ret +( + tcg_gen_andi_i32@and_p +| + tcg_gen_andi_i64@and_p +| + tcg_gen_andi_tl@and_p +)(ret, ret, msk); + +@script:python verify_len depends on match@ +ret_s << match.ret; +msk_s << match.msk; +shr_p << match.shr_p; +extract_len; +@@ +is_optimizable =3D False +debug("candidate at %s:%s" % (shr_p[0].file, shr_p[0].line)) +try: # only eval integer, no #define like 'SR_M' (cpp did this, else some = headers are missing). + msk_v =3D long(msk_s.strip("UL"), 0) + msk_b =3D low_bits_count(msk_v) + if msk_b =3D=3D 0: + debug(" value: 0x%x low_bits: %d" % (msk_v, msk_b)) + else: + debug(" value: 0x%x low_bits: %d [Mersenne number: 0x%x]" % (msk_= v, msk_b, Mn(msk_b))) + is_optimizable =3D Mn(msk_b) =3D=3D msk_v # check low_bits + coccinelle.extract_len =3D "%d" % msk_b + debug(" candidate %s optimizable" % ("IS" if is_optimizable else "is = NOT")) +except: + debug(" ERROR (check included headers?)") +cocci.include_match(is_optimizable) +debug() + +@replacement depends on verify_len@ +identifier match.ret; +metavariable match.arg; +constant match.ofs, match.msk; +position match.shr_p, match.and_p; +identifier verify_len.extract_len; +@@ +( +-tcg_gen_shri_i32@shr_p(ret, arg, ofs); ++tcg_gen_extract_i32(ret, arg, ofs, extract_len); +... WHEN !=3D ret +-tcg_gen_andi_i32@and_p(ret, ret, msk); +| +-tcg_gen_shri_i64@shr_p(ret, arg, ofs); ++tcg_gen_extract_i64(ret, arg, ofs, extract_len); +... WHEN !=3D ret +-tcg_gen_andi_i64@and_p(ret, ret, msk); +| +-tcg_gen_shri_tl@shr_p(ret, arg, ofs); ++tcg_gen_extract_tl(ret, arg, ofs, extract_len); +... WHEN !=3D ret +-tcg_gen_andi_tl@and_p(ret, ret, msk); +) --=20 2.9.4