From nobody Fri Dec 19 02:23:39 2025 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C1A020E6; Sun, 17 Dec 2023 07:13:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dGp+NfaC" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6d47bb467a9so318623b3a.1; Sat, 16 Dec 2023 23:13:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702797200; x=1703402000; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=feSWUInibmppFuGNXnF0v3E6pdqKZ3fBbhHfQ61np4c=; b=dGp+NfaCZFRFtbtjtoIYcTHPqnW66HPvgXqU9bLFPxfxM98f4dPXBBGP1pKZYpshZk l/eE3XlMjGo3i8bOQBd8AmN1xY3B/VrtQ9Fw2NrMZ2+H2hOzaREKkEpoarFLF6Y9NB3K ir00N8ZwnSlz6+1mss4hUFYaNfppLP9Et/lRt7xCpLFdJxzNiX3SnnaLDuADexN7GoE5 o8nZcAc6vXfyxCZYk2OUX//9ixMVnTaURIVEvL7McfPVTIfDEPVGSz5WCDXuEb26rNud ibd1Q9NPnZ7ZGWWs71GvdiAwkcWTzFMyD8MWbg6Gp89fwYhxVBClbkRSt9eH5yuCVljj J80g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702797200; x=1703402000; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=feSWUInibmppFuGNXnF0v3E6pdqKZ3fBbhHfQ61np4c=; b=eumC9GnRxtN/IGeNihJ8Us6kL3puQy/TmuyACMznwRvua8Fmc8EFGVS3qkzL67MSpC TDESJrR0P0Q2gz43t+QjmuzQujPtf4J+1jZPs5h5OHsFjqu69iKJh95I7goiPACoWgLv 5684JXZqBcTVYcioa/B5tiNpN5W/2j+GfMGikpLfXyBavfD6BUuOlc8RNBP731rPxN3H OV1ifHi5yBTA17VhQQ9buoxLD4DEE5ClpKVdEVt6Czhb6tC+KCz56uwCV36NSxzfhJ+f n2b27ec7Y5yGVq5Dqgk5aCWuGN0C/9UvlNoYAt09t8A6q+EJ9apZg5lpx1r1rVrtNas2 GCYg== X-Gm-Message-State: AOJu0YyAo0V1hKZ8IH8a52ApRm31WFIFAsw+BREkJCZz3gSsakvLiFfj GpBRYk1Rrle0iOPMOvLRo7w= X-Google-Smtp-Source: AGHT+IGqSESWQXTN78W86orpyfFDqNV9phDRlGiR1X9RzBIbvxCFThDNzhlc7ScbiAJmJsiX039f1w== X-Received: by 2002:a05:6a20:8e14:b0:190:23b2:cf1a with SMTP id y20-20020a056a208e1400b0019023b2cf1amr17972903pzj.22.1702797199658; Sat, 16 Dec 2023 23:13:19 -0800 (PST) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id b17-20020a056a000cd100b0068ffb8da107sm15740231pfv.212.2023.12.16.23.13.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 23:13:19 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org, Yury Norov Cc: Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-hexagon@vger.kernel.org, linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v3 1/5] m68k/bitops: force inlining of all bitops functions Date: Sun, 17 Dec 2023 16:12:46 +0900 Message-Id: <20231217071250.892867-2-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The inline keyword actually does not guarantee that the compiler will inline a functions. Whenever the goal is to actually inline a function, __always_inline should always be preferred instead. On an allyesconfig, with GCC 13.2.1, it saves roughly 5 KB. $ size --format=3DGNU vmlinux.before vmlinux.after text data bss total filename 60449738 70975612 2288988 133714338 vmlinux.before 60446534 70972412 2289596 133708542 vmlinux.after Reference: commit 8dd5032d9c54 ("x86/asm/bitops: Force inlining of test_and_set_bit and friends") Link: https://git.kernel.org/torvalds/c/8dd5032d9c54 Signed-off-by: Vincent Mailhol Reviewed-by: Geert Uytterhoeven --- arch/m68k/include/asm/bitops.h | 87 +++++++++++++++++----------------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/arch/m68k/include/asm/bitops.h b/arch/m68k/include/asm/bitops.h index 14c64a6f1217..ae0457d582b8 100644 --- a/arch/m68k/include/asm/bitops.h +++ b/arch/m68k/include/asm/bitops.h @@ -28,7 +28,7 @@ * So we use the best form possible on a given platform. */ =20 -static inline void bset_reg_set_bit(int nr, volatile unsigned long *vaddr) +static __always_inline void bset_reg_set_bit(int nr, volatile unsigned lon= g *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; =20 @@ -38,7 +38,7 @@ static inline void bset_reg_set_bit(int nr, volatile unsi= gned long *vaddr) : "memory"); } =20 -static inline void bset_mem_set_bit(int nr, volatile unsigned long *vaddr) +static __always_inline void bset_mem_set_bit(int nr, volatile unsigned lon= g *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; =20 @@ -47,7 +47,7 @@ static inline void bset_mem_set_bit(int nr, volatile unsi= gned long *vaddr) : "di" (nr & 7)); } =20 -static inline void bfset_mem_set_bit(int nr, volatile unsigned long *vaddr) +static __always_inline void bfset_mem_set_bit(int nr, volatile unsigned lo= ng *vaddr) { __asm__ __volatile__ ("bfset %1{%0:#1}" : @@ -71,7 +71,7 @@ arch___set_bit(unsigned long nr, volatile unsigned long *= addr) set_bit(nr, addr); } =20 -static inline void bclr_reg_clear_bit(int nr, volatile unsigned long *vadd= r) +static __always_inline void bclr_reg_clear_bit(int nr, volatile unsigned l= ong *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; =20 @@ -81,7 +81,7 @@ static inline void bclr_reg_clear_bit(int nr, volatile un= signed long *vaddr) : "memory"); } =20 -static inline void bclr_mem_clear_bit(int nr, volatile unsigned long *vadd= r) +static __always_inline void bclr_mem_clear_bit(int nr, volatile unsigned l= ong *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; =20 @@ -90,7 +90,7 @@ static inline void bclr_mem_clear_bit(int nr, volatile un= signed long *vaddr) : "di" (nr & 7)); } =20 -static inline void bfclr_mem_clear_bit(int nr, volatile unsigned long *vad= dr) +static __always_inline void bfclr_mem_clear_bit(int nr, volatile unsigned = long *vaddr) { __asm__ __volatile__ ("bfclr %1{%0:#1}" : @@ -114,7 +114,7 @@ arch___clear_bit(unsigned long nr, volatile unsigned lo= ng *addr) clear_bit(nr, addr); } =20 -static inline void bchg_reg_change_bit(int nr, volatile unsigned long *vad= dr) +static __always_inline void bchg_reg_change_bit(int nr, volatile unsigned = long *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; =20 @@ -124,7 +124,7 @@ static inline void bchg_reg_change_bit(int nr, volatile= unsigned long *vaddr) : "memory"); } =20 -static inline void bchg_mem_change_bit(int nr, volatile unsigned long *vad= dr) +static __always_inline void bchg_mem_change_bit(int nr, volatile unsigned = long *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; =20 @@ -133,7 +133,7 @@ static inline void bchg_mem_change_bit(int nr, volatile= unsigned long *vaddr) : "di" (nr & 7)); } =20 -static inline void bfchg_mem_change_bit(int nr, volatile unsigned long *va= ddr) +static __always_inline void bfchg_mem_change_bit(int nr, volatile unsigned= long *vaddr) { __asm__ __volatile__ ("bfchg %1{%0:#1}" : @@ -160,8 +160,8 @@ arch___change_bit(unsigned long nr, volatile unsigned l= ong *addr) #define arch_test_bit generic_test_bit #define arch_test_bit_acquire generic_test_bit_acquire =20 -static inline int bset_reg_test_and_set_bit(int nr, - volatile unsigned long *vaddr) +static __always_inline int +bset_reg_test_and_set_bit(int nr, volatile unsigned long *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; char retval; @@ -173,8 +173,8 @@ static inline int bset_reg_test_and_set_bit(int nr, return retval; } =20 -static inline int bset_mem_test_and_set_bit(int nr, - volatile unsigned long *vaddr) +static __always_inline int +bset_mem_test_and_set_bit(int nr, volatile unsigned long *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; char retval; @@ -185,8 +185,8 @@ static inline int bset_mem_test_and_set_bit(int nr, return retval; } =20 -static inline int bfset_mem_test_and_set_bit(int nr, - volatile unsigned long *vaddr) +static __always_inline int +bfset_mem_test_and_set_bit(int nr, volatile unsigned long *vaddr) { char retval; =20 @@ -213,8 +213,8 @@ arch___test_and_set_bit(unsigned long nr, volatile unsi= gned long *addr) return test_and_set_bit(nr, addr); } =20 -static inline int bclr_reg_test_and_clear_bit(int nr, - volatile unsigned long *vaddr) +static __always_inline int +bclr_reg_test_and_clear_bit(int nr, volatile unsigned long *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; char retval; @@ -226,8 +226,8 @@ static inline int bclr_reg_test_and_clear_bit(int nr, return retval; } =20 -static inline int bclr_mem_test_and_clear_bit(int nr, - volatile unsigned long *vaddr) +static __always_inline int +bclr_mem_test_and_clear_bit(int nr, volatile unsigned long *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; char retval; @@ -238,8 +238,8 @@ static inline int bclr_mem_test_and_clear_bit(int nr, return retval; } =20 -static inline int bfclr_mem_test_and_clear_bit(int nr, - volatile unsigned long *vaddr) +static __always_inline int +bfclr_mem_test_and_clear_bit(int nr, volatile unsigned long *vaddr) { char retval; =20 @@ -266,8 +266,8 @@ arch___test_and_clear_bit(unsigned long nr, volatile un= signed long *addr) return test_and_clear_bit(nr, addr); } =20 -static inline int bchg_reg_test_and_change_bit(int nr, - volatile unsigned long *vaddr) +static __always_inline int +bchg_reg_test_and_change_bit(int nr, volatile unsigned long *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; char retval; @@ -279,8 +279,8 @@ static inline int bchg_reg_test_and_change_bit(int nr, return retval; } =20 -static inline int bchg_mem_test_and_change_bit(int nr, - volatile unsigned long *vaddr) +static __always_inline int +bchg_mem_test_and_change_bit(int nr, volatile unsigned long *vaddr) { char *p =3D (char *)vaddr + (nr ^ 31) / 8; char retval; @@ -291,8 +291,8 @@ static inline int bchg_mem_test_and_change_bit(int nr, return retval; } =20 -static inline int bfchg_mem_test_and_change_bit(int nr, - volatile unsigned long *vaddr) +static __always_inline int +bfchg_mem_test_and_change_bit(int nr, volatile unsigned long *vaddr) { char retval; =20 @@ -319,8 +319,8 @@ arch___test_and_change_bit(unsigned long nr, volatile u= nsigned long *addr) return test_and_change_bit(nr, addr); } =20 -static inline bool xor_unlock_is_negative_byte(unsigned long mask, - volatile unsigned long *p) +static __always_inline bool +xor_unlock_is_negative_byte(unsigned long mask, volatile unsigned long *p) { #ifdef CONFIG_COLDFIRE __asm__ __volatile__ ("eorl %1, %0" @@ -350,8 +350,8 @@ static inline bool xor_unlock_is_negative_byte(unsigned= long mask, #include #else =20 -static inline int find_first_zero_bit(const unsigned long *vaddr, - unsigned size) +static __always_inline int +find_first_zero_bit(const unsigned long *vaddr, unsigned size) { const unsigned long *p =3D vaddr; int res =3D 32; @@ -376,8 +376,8 @@ static inline int find_first_zero_bit(const unsigned lo= ng *vaddr, } #define find_first_zero_bit find_first_zero_bit =20 -static inline int find_next_zero_bit(const unsigned long *vaddr, int size, - int offset) +static __always_inline int +find_next_zero_bit(const unsigned long *vaddr, int size, int offset) { const unsigned long *p =3D vaddr + (offset >> 5); int bit =3D offset & 31UL, res; @@ -406,7 +406,8 @@ static inline int find_next_zero_bit(const unsigned lon= g *vaddr, int size, } #define find_next_zero_bit find_next_zero_bit =20 -static inline int find_first_bit(const unsigned long *vaddr, unsigned size) +static __always_inline int +find_first_bit(const unsigned long *vaddr, unsigned size) { const unsigned long *p =3D vaddr; int res =3D 32; @@ -431,8 +432,8 @@ static inline int find_first_bit(const unsigned long *v= addr, unsigned size) } #define find_first_bit find_first_bit =20 -static inline int find_next_bit(const unsigned long *vaddr, int size, - int offset) +static __always_inline int +find_next_bit(const unsigned long *vaddr, int size, int offset) { const unsigned long *p =3D vaddr + (offset >> 5); int bit =3D offset & 31UL, res; @@ -465,7 +466,7 @@ static inline int find_next_bit(const unsigned long *va= ddr, int size, * ffz =3D Find First Zero in word. Undefined if no zero exists, * so code should check against ~0UL first.. */ -static inline unsigned long ffz(unsigned long word) +static __always_inline unsigned long ffz(unsigned long word) { int res; =20 @@ -488,7 +489,7 @@ static inline unsigned long ffz(unsigned long word) */ #if (defined(__mcfisaaplus__) || defined(__mcfisac__)) && \ !defined(CONFIG_M68000) -static inline unsigned long __ffs(unsigned long x) +static __always_inline unsigned long __ffs(unsigned long x) { __asm__ __volatile__ ("bitrev %0; ff1 %0" : "=3Dd" (x) @@ -496,7 +497,7 @@ static inline unsigned long __ffs(unsigned long x) return x; } =20 -static inline int ffs(int x) +static __always_inline int ffs(int x) { if (!x) return 0; @@ -518,7 +519,7 @@ static inline int ffs(int x) * the libc and compiler builtin ffs routines, therefore * differs in spirit from the above ffz (man ffs). */ -static inline int ffs(int x) +static __always_inline int ffs(int x) { int cnt; =20 @@ -528,7 +529,7 @@ static inline int ffs(int x) return 32 - cnt; } =20 -static inline unsigned long __ffs(unsigned long x) +static __always_inline unsigned long __ffs(unsigned long x) { return ffs(x) - 1; } @@ -536,7 +537,7 @@ static inline unsigned long __ffs(unsigned long x) /* * fls: find last bit set. */ -static inline int fls(unsigned int x) +static __always_inline int fls(unsigned int x) { int cnt; =20 @@ -546,7 +547,7 @@ static inline int fls(unsigned int x) return 32 - cnt; } =20 -static inline unsigned long __fls(unsigned long x) +static __always_inline unsigned long __fls(unsigned long x) { return fls(x) - 1; } --=20 2.25.1 From nobody Fri Dec 19 02:23:39 2025 Received: from mail-oo1-f48.google.com (mail-oo1-f48.google.com [209.85.161.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0FB4D3D65; Sun, 17 Dec 2023 07:13:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="R6WnJIqa" Received: by mail-oo1-f48.google.com with SMTP id 006d021491bc7-59148c1ad35so1622928eaf.2; Sat, 16 Dec 2023 23:13:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702797205; x=1703402005; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=5Lcla7+xNiv5DjqNFwhIbwZ/t+uq3BdL5z3Q7Ys3rqQ=; b=R6WnJIqa8CQ0I1PD+Xk6ekB6jyAG+U8SEaA5BA9LtSGvz15HjlxMlLiNIEnkiQRJyO HbOl1TR2DvBma01pGOuD30J1vtt99XXTZv9dGujaPwg/RADqCF+HfDJPe8BNYoBbg61J pQ1TmBWLxPSR+5Ye8NUAmYpp2HHKS0d9dZLyZFncfCdSwGMFTsq9tkR24Mt/KmShHF4u eX4gWvhpZ6th7zTxYizyOXtygsSgZFz6tmqyoeK93U9t4CqIbY3YffOQSSTxdrs3A2a1 WH8Acl6hHrOHATjhPNkojxmjCotFyb/Xje8nBzBySmbHAyfD6JRdKGc05k/+1U06Sg0M scRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702797205; x=1703402005; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5Lcla7+xNiv5DjqNFwhIbwZ/t+uq3BdL5z3Q7Ys3rqQ=; b=rz0+zsjegyUzm05MnBUpGhI3cJAAHA8ctquq3jmAB+qZY9j3wYAMzpWqJdxyuc7n0u C1wgJ2DYKbdvwGDq092Rei5Hn0siAPYWb5/QR2BU3rqu+/m7kokFvctEQW/zRn7oV31v QOBwJ+9MMFdj9+kDSQkl8JWqIE6Tcr+DNZ6RycWcDt+Sz3MBcYoYws276jWdVlKmCYhm XQhltWBcPYWe76/Tqfai4pZ+TA8sRGN1OBgO6QpEld1gMoOmib0+Zw4GAHSV6pDwMEW+ wWp1QN4OdturivLAMPS9yCD2rke/9E1r805LBQ3IPpLAI/39KAVggWa635LG2zoRo+Gb svZg== X-Gm-Message-State: AOJu0Yw6Tc8lGyNrzA8D50rhb4M0n5Av201xcX83nSi4Q6V1Wc7IdStG igCqIy44LVY2IjOUa65QdgU= X-Google-Smtp-Source: AGHT+IEPBGJ4ARPNrxvSgt2qn9lQ4hHfgW1RzzZiJmfZnfo2kj1iNdiBdBHyw68Pbe4qdlzSXmnUVw== X-Received: by 2002:a05:6808:3c95:b0:3b9:e779:8a04 with SMTP id gs21-20020a0568083c9500b003b9e7798a04mr23826650oib.17.1702797204976; Sat, 16 Dec 2023 23:13:24 -0800 (PST) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id b17-20020a056a000cd100b0068ffb8da107sm15740231pfv.212.2023.12.16.23.13.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 23:13:24 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org, Yury Norov Cc: Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-hexagon@vger.kernel.org, linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v3 2/5] m68k/bitops: use __builtin_{clz,ctzl,ffs} to evaluate constant expressions Date: Sun, 17 Dec 2023 16:12:47 +0900 Message-Id: <20231217071250.892867-3-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The compiler is not able to do constant folding on "asm volatile" code. Evaluate whether or not the function argument is a constant expression and if this is the case, return an equivalent builtin expression. On an allyesconfig, with GCC 13.2.1, it saves roughly 8 KB. $ size --format=3DGNU vmlinux.before vmlinux.after text data bss total filename 60446534 70972412 2289596 133708542 vmlinux.before 60429746 70978876 2291676 133700298 vmlinux.after Reference: commit fdb6649ab7c1 ("x86/asm/bitops: Use __builtin_ctzl() to evaluate constant expressions") Link: https://git.kernel.org/torvalds/c/fdb6649ab7c1 Signed-off-by: Vincent Mailhol Reviewed-by: Geert Uytterhoeven --- arch/m68k/include/asm/bitops.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/m68k/include/asm/bitops.h b/arch/m68k/include/asm/bitops.h index ae0457d582b8..3f89b9dccc33 100644 --- a/arch/m68k/include/asm/bitops.h +++ b/arch/m68k/include/asm/bitops.h @@ -470,6 +470,9 @@ static __always_inline unsigned long ffz(unsigned long = word) { int res; =20 + if (__builtin_constant_p(word)) + return __builtin_ctzl(~word); + __asm__ __volatile__ ("bfffo %1{#0,#0},%0" : "=3Dd" (res) : "d" (~word & -~word)); return res ^ 31; @@ -491,6 +494,9 @@ static __always_inline unsigned long ffz(unsigned long = word) !defined(CONFIG_M68000) static __always_inline unsigned long __ffs(unsigned long x) { + if (__builtin_constant_p(x)) + return __builtin_ctzl(x); + __asm__ __volatile__ ("bitrev %0; ff1 %0" : "=3Dd" (x) : "0" (x)); @@ -523,6 +529,9 @@ static __always_inline int ffs(int x) { int cnt; =20 + if (__builtin_constant_p(x)) + return __builtin_ffs(x); + __asm__ ("bfffo %1{#0:#0},%0" : "=3Dd" (cnt) : "dm" (x & -x)); @@ -541,6 +550,9 @@ static __always_inline int fls(unsigned int x) { int cnt; =20 + if (__builtin_constant_p(x)) + return x ? BITS_PER_TYPE(x) - __builtin_clz(x) : 0; + __asm__ ("bfffo %1{#0,#0},%0" : "=3Dd" (cnt) : "dm" (x)); --=20 2.25.1 From nobody Fri Dec 19 02:23:39 2025 Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6695D63AA; Sun, 17 Dec 2023 07:13:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aroyU7BJ" Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3b844357f7cso1696781b6e.1; Sat, 16 Dec 2023 23:13:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702797210; x=1703402010; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=ZV393vzARRuX2pwqmw5hE6OeimRr0obKDvqio21m2gk=; b=aroyU7BJ9ZlRKHGgR74IRmjfYNtOpZUSPvCmssf5fHitT+f9GRoOtub54UyCsuS/bh zR/kVGnGFK+29Pm7Jk9oJlBNvn3R2rCe4Y+gYt+w/c0U/tNHtDi7kkblkVATZN8kZPDb rGivcnP3RWvjOl66y3A4N5EBuVqNQQ9EJixBTm+WKh7BiJEdn++fVF7x18DR3Zsf3dD8 EMmGdBBE1ibhlFbcjdSPujg0cLEWGlkPEpsXlL/gZKZI3UoZ9NW4uA5T0M7jMPkchsQb OwaW6m4SCFODGAuuHfnSZjxIeoE8EZTu584Y4/Pg/mqwjlBlvwsh/StOXyRcPf76xQaJ n4eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702797210; x=1703402010; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ZV393vzARRuX2pwqmw5hE6OeimRr0obKDvqio21m2gk=; b=EeoBEIA9XdeZqJkn5K5nwyB0m64T9iYHnWpfE2jKZRBNViC4laeJdD/vzP4m77/EjT ImEimEqRCkAZ4GYxwZvOOUrb4gmDp+E0n63uVLce93lmyXEngrKT3n9G2oVEuPl3qT2n uuC1vaOuik8jOl3LqJDkx70IwxtM+gXfVqLv4pnNsTJaoMxpgoMjBf2LnzWNHji9r5Za UEND++N28AYZ4l+v4QzuxEG126ewRKxKjgIebTn4N8v6n6sNWRAWhTLxuyhmBRpReJUY xMVWmCB8zwpoXdvK44q3mBsGQsjqQHPbgCa4IdnuZdWCeCxW/rHV1Hduos/FxzYuQ5Mc LxNA== X-Gm-Message-State: AOJu0YzIqnZq2SVZY93zEXG0uhPVJ1UWLL0XawxhxWfljmG9ZZ0se4QF BgHz5ZHAf92VBTTkA7U8Cmg= X-Google-Smtp-Source: AGHT+IF8q2ujwJLtWhsj+hSoyYr3CajraUBLSb5x/4VREsRp3/VoULFMci56eN2ewnOAYG0hVRU8NQ== X-Received: by 2002:a05:6808:2125:b0:3b8:b063:6b96 with SMTP id r37-20020a056808212500b003b8b0636b96mr19083767oiw.69.1702797210271; Sat, 16 Dec 2023 23:13:30 -0800 (PST) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id b17-20020a056a000cd100b0068ffb8da107sm15740231pfv.212.2023.12.16.23.13.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 23:13:29 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org, Yury Norov Cc: Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-hexagon@vger.kernel.org, linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v3 3/5] hexagon/bitops: force inlining of all bitops functions Date: Sun, 17 Dec 2023 16:12:48 +0900 Message-Id: <20231217071250.892867-4-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The inline keyword actually does not guarantee that the compiler will inline a functions. Whenever the goal is to actually inline a function, __always_inline should always be preferred instead. Reference: commit 8dd5032d9c54 ("x86/asm/bitops: Force inlining of test_and_set_bit and friends") Link: https://git.kernel.org/torvalds/c/8dd5032d9c54 Signed-off-by: Vincent Mailhol --- arch/hexagon/include/asm/bitops.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/arch/hexagon/include/asm/bitops.h b/arch/hexagon/include/asm/b= itops.h index 160d8f37fa1a..950d4acc2edc 100644 --- a/arch/hexagon/include/asm/bitops.h +++ b/arch/hexagon/include/asm/bitops.h @@ -28,7 +28,7 @@ * @nr: bit number to clear * @addr: pointer to memory */ -static inline int test_and_clear_bit(int nr, volatile void *addr) +static __always_inline int test_and_clear_bit(int nr, volatile void *addr) { int oldval; =20 @@ -52,7 +52,7 @@ static inline int test_and_clear_bit(int nr, volatile voi= d *addr) * @nr: bit number to set * @addr: pointer to memory */ -static inline int test_and_set_bit(int nr, volatile void *addr) +static __always_inline int test_and_set_bit(int nr, volatile void *addr) { int oldval; =20 @@ -78,7 +78,7 @@ static inline int test_and_set_bit(int nr, volatile void = *addr) * @nr: bit number to set * @addr: pointer to memory */ -static inline int test_and_change_bit(int nr, volatile void *addr) +static __always_inline int test_and_change_bit(int nr, volatile void *addr) { int oldval; =20 @@ -103,17 +103,17 @@ static inline int test_and_change_bit(int nr, volatil= e void *addr) * Rewrite later to save a cycle or two. */ =20 -static inline void clear_bit(int nr, volatile void *addr) +static __always_inline void clear_bit(int nr, volatile void *addr) { test_and_clear_bit(nr, addr); } =20 -static inline void set_bit(int nr, volatile void *addr) +static __always_inline void set_bit(int nr, volatile void *addr) { test_and_set_bit(nr, addr); } =20 -static inline void change_bit(int nr, volatile void *addr) +static __always_inline void change_bit(int nr, volatile void *addr) { test_and_change_bit(nr, addr); } @@ -200,7 +200,7 @@ arch_test_bit_acquire(unsigned long nr, const volatile = unsigned long *addr) * * Undefined if no zero exists, so code should check against ~0UL first. */ -static inline long ffz(int x) +static __always_inline long ffz(int x) { int r; =20 @@ -217,7 +217,7 @@ static inline long ffz(int x) * This is defined the same way as ffs. * Note fls(0) =3D 0, fls(1) =3D 1, fls(0x80000000) =3D 32. */ -static inline int fls(unsigned int x) +static __always_inline int fls(unsigned int x) { int r; =20 @@ -238,7 +238,7 @@ static inline int fls(unsigned int x) * the libc and compiler builtin ffs routines, therefore * differs in spirit from the above ffz (man ffs). */ -static inline int ffs(int x) +static __always_inline int ffs(int x) { int r; =20 @@ -260,7 +260,7 @@ static inline int ffs(int x) * bits_per_long assumed to be 32 * numbering starts at 0 I think (instead of 1 like ffs) */ -static inline unsigned long __ffs(unsigned long word) +static __always_inline unsigned long __ffs(unsigned long word) { int num; =20 @@ -278,7 +278,7 @@ static inline unsigned long __ffs(unsigned long word) * Undefined if no set bit exists, so code should check against 0 first. * bits_per_long assumed to be 32 */ -static inline unsigned long __fls(unsigned long word) +static __always_inline unsigned long __fls(unsigned long word) { int num; =20 --=20 2.25.1 From nobody Fri Dec 19 02:23:39 2025 Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60FB76FD5; Sun, 17 Dec 2023 07:13:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="k30N9yvj" Received: by mail-oi1-f171.google.com with SMTP id 5614622812f47-3ba00fe4e94so1705082b6e.1; Sat, 16 Dec 2023 23:13:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702797215; x=1703402015; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=8vyhSqduyGw//DWggAhtjkJM+OOGpuFMjvvJjdOe8jo=; b=k30N9yvjoCL8Oux3twFbUdaV8+ZgXrVdikZwowb1Zfr4q/fYxUWwp+nLraGTSODz6t CQx5j9s541072br1zWdfPHmZQGn1ULbM8RdB7+ojxyxSzHhH4qgr5vYBKs9aVcqeR8sj KL4yXC9ZoHhkqDcFKL/ImiLYR8bQP6QHAF+Y3u/+IhSJquqSypaSegDjyuAv8vtUNC0I QYeeE8xGOk9md87TW9mQvxvfhcikXKhBsE9OHsmTxEojnK4CXND8zfTqS5VeGsJHlO60 /i+qgiF+1GqB6tVZIxri45D9ManLLVhLYcDFrgqSC+gKyYuL+4sGki6P5Orv0RncCVMx kEow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702797215; x=1703402015; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=8vyhSqduyGw//DWggAhtjkJM+OOGpuFMjvvJjdOe8jo=; b=gr6Sc90dJmMoK/02sZ56qXPp8SnZMMLesO2RCWsyVGpFNA7nXoa4MHRR6mw5JHEbrr 3cVN9PI3zmK2yKu2gQWpdnvSrNK2MYiIlHCZ5LhTzrSQV4C9BqqE7m5QhRSOD+Sqj11D SY/t1Dc6R9NCMYUO7NyGeKLYXXcv8IMv2iV2UnPgi8Ym/ReErHmpYy/+8T5XxT364AZq ZAoQAs+ACwmSVUI+I6R8S8t4X9SaHIIiocHLLfC32kwu23305n7UWHaiTgYQGVyZa1Ya 9OcQhayRoylrWvrKGukAzq7jJdhTPLI9n4Hia00H0gepkV099RcjHvYnoBnvlEmOrjpW iJ8w== X-Gm-Message-State: AOJu0YyaYYF5xBllLhVeVpfYqSpsx16kSULZv72lLGnKMjqA/RzcTwkY OmJwfWZXg+bKuiP8v1q7B2I= X-Google-Smtp-Source: AGHT+IHGOx0BR5HAYftUoQp8Nb+5soW9pU5BJgFekD35zFbH7EwlDc0Na3Oxjb+GsxeC4LLdkZLVbQ== X-Received: by 2002:a05:6808:3309:b0:3ba:231:cd44 with SMTP id ca9-20020a056808330900b003ba0231cd44mr19137878oib.43.1702797215473; Sat, 16 Dec 2023 23:13:35 -0800 (PST) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id b17-20020a056a000cd100b0068ffb8da107sm15740231pfv.212.2023.12.16.23.13.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 23:13:35 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org, Yury Norov Cc: Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-hexagon@vger.kernel.org, linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v3 4/5] hexagon/bitops: use __builtin_{clz,ctzl,ffs} to evaluate constant expressions Date: Sun, 17 Dec 2023 16:12:49 +0900 Message-Id: <20231217071250.892867-5-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The compiler is not able to do constant folding on "asm volatile" code. Evaluate whether or not the function argument is a constant expression and if this is the case, return an equivalent builtin expression. Reference: commit fdb6649ab7c1 ("x86/asm/bitops: Use __builtin_ctzl() to evaluate constant expressions") Link: https://git.kernel.org/torvalds/c/fdb6649ab7c1 Signed-off-by: Vincent Mailhol --- arch/hexagon/include/asm/bitops.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arch/hexagon/include/asm/bitops.h b/arch/hexagon/include/asm/b= itops.h index 950d4acc2edc..12c6ad1ea2ed 100644 --- a/arch/hexagon/include/asm/bitops.h +++ b/arch/hexagon/include/asm/bitops.h @@ -204,6 +204,9 @@ static __always_inline long ffz(int x) { int r; =20 + if (__builtin_constant_p(x)) + return __builtin_ctzl(~x); + asm("%0 =3D ct1(%1);\n" : "=3D&r" (r) : "r" (x)); @@ -221,6 +224,9 @@ static __always_inline int fls(unsigned int x) { int r; =20 + if (__builtin_constant_p(x)) + return x ? BITS_PER_TYPE(x) - __builtin_clz(x) : 0; + asm("{ %0 =3D cl0(%1);}\n" "%0 =3D sub(#32,%0);\n" : "=3D&r" (r) @@ -242,6 +248,9 @@ static __always_inline int ffs(int x) { int r; =20 + if (__builtin_constant_p(x)) + return __builtin_ffs(x); + asm("{ P0 =3D cmp.eq(%1,#0); %0 =3D ct0(%1);}\n" "{ if (P0) %0 =3D #0; if (!P0) %0 =3D add(%0,#1);}\n" : "=3D&r" (r) @@ -264,6 +273,9 @@ static __always_inline unsigned long __ffs(unsigned lon= g word) { int num; =20 + if (__builtin_constant_p(word)) + return __builtin_ctzl(word); + asm("%0 =3D ct0(%1);\n" : "=3D&r" (num) : "r" (word)); @@ -282,6 +294,9 @@ static __always_inline unsigned long __fls(unsigned lon= g word) { int num; =20 + if (__builtin_constant_p(word)) + return BITS_PER_LONG - 1 - __builtin_clzl(word); + asm("%0 =3D cl0(%1);\n" "%0 =3D sub(#31,%0);\n" : "=3D&r" (num) --=20 2.25.1 From nobody Fri Dec 19 02:23:39 2025 Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2886EDDB2; Sun, 17 Dec 2023 07:13:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TZMjzs+Q" Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3ba2dd905f9so1376544b6e.2; Sat, 16 Dec 2023 23:13:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702797220; x=1703402020; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=JsNa9K53QEIWDiiqOBuAFIxQwF1kCxA5oMZgz893y4w=; b=TZMjzs+QukM7cdi3bcOcVsGeJQzJtJtG1caB1xo7KqHzvB87Q/fm93wYXls+4wPHah fYPMe9AnnMnwumjHXT6v0s23V2I5r8dlfQ6KmACKnzKFBOQa89oHhGiSesu0d0fQLRK8 E+/PdAdpbptsaLXbtae3Ibqx+0icHll2HwRkxxs3KQSgBlZ8WiSMZ6br1lqEe9G+K2JA NWYCe92BVaLRg5lzFBiJdN0PV7TwEuidHtG8Zn4P2nBRlCcqIyhziZ6tPtTegp7rnPQ7 tszJZjr30G++D1U6Ksf1eJ75FrxpVd50lEBFflmlRTrBBKgAZYG2+ExzFO3zRnrE/9VL H6uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702797220; x=1703402020; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=JsNa9K53QEIWDiiqOBuAFIxQwF1kCxA5oMZgz893y4w=; b=ZVS7MYyrvdYqCuajwLnS3dizNN7gFXrKEV2bgXfTs+YsN/G++WqMl4uAsAmESP6jz1 EI1NiJuZJfmDgBLxKV19f2l2iPbJEcFQKvlksUnA9erWOc/XNstEm9kLzRCvSOFlHeOJ 5xMStgGYbk1O3ZJ8n3DrBoj1NcFGGse4MOB3CHa/pDM65oj3xBQxe3yR1zGwK/0875aW i4/3Pa1vSID3ZEDpx1QulOs5yII3PFPVax2x9KDZkmksB5iJJRymIvUyixFzCEyq4OeF i3kCLnxrO0uWGIom9P2qlD180Xev12hqjGlUYdv2rRyJJAQG/8jUupLIEn2kdnmwV4Ub ygPQ== X-Gm-Message-State: AOJu0Yy0q4NjnSFL6kSNXTQDgGVqvoBqIG6EFGaNiTXWpn4QlAHV4CVE zxCATM+57u0IY1YZ5RhznF4= X-Google-Smtp-Source: AGHT+IGWwFqDW7XfFkRgyj/Gr80/LunbsYTvIMkNiVdagI90/MvOj3NipYqj2bpZF0ACp4FlPYnzLQ== X-Received: by 2002:a05:6808:1924:b0:3b9:f017:28f with SMTP id bf36-20020a056808192400b003b9f017028fmr18156464oib.67.1702797220239; Sat, 16 Dec 2023 23:13:40 -0800 (PST) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id b17-20020a056a000cd100b0068ffb8da107sm15740231pfv.212.2023.12.16.23.13.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 23:13:39 -0800 (PST) Sender: Vincent Mailhol From: Vincent Mailhol To: Andrew Morton , linux-kernel@vger.kernel.org, Yury Norov Cc: Nick Desaulniers , Douglas Anderson , Kees Cook , Petr Mladek , Randy Dunlap , Zhaoyang Huang , Geert Uytterhoeven , Marco Elver , Brian Cain , Geert Uytterhoeven , Matthew Wilcox , "Paul E . McKenney" , linux-hexagon@vger.kernel.org, linux-m68k@lists.linux-m68k.org, Vincent Mailhol Subject: [PATCH v3 5/5] lib: test_bitops: add compile-time optimization/evaluations assertions Date: Sun, 17 Dec 2023 16:12:50 +0900 Message-Id: <20231217071250.892867-6-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> References: <20221111081316.30373-1-mailhol.vincent@wanadoo.fr> <20231217071250.892867-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a function in the bitops test suite to assert that the bitops helper correctly fold constant expressions (or trigger a build bug otherwise). This should work on all the optimization levels supported by Kbuild. The added function doesn't perform any runtime tests and gets optimized out to nothing after passing the build assertions. Suggested-by: Yury Norov Signed-off-by: Vincent Mailhol --- lib/Kconfig.debug | 4 ++++ lib/test_bitops.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index cc7d53d9dc01..c97d818dbc30 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2454,6 +2454,10 @@ config TEST_BITOPS compilations. It has no dependencies and doesn't run or load unless explicitly requested by name. for example: modprobe test_bitops. =20 + In addition, check that the compiler is able to fold the bitops + function into a compile-time constant (given that the argument is also + a compile-time constant) and trigger a build bug otherwise. + If unsure, say N. =20 config TEST_VMALLOC diff --git a/lib/test_bitops.c b/lib/test_bitops.c index 3b7bcbee84db..99b612515eb6 100644 --- a/lib/test_bitops.c +++ b/lib/test_bitops.c @@ -50,6 +50,34 @@ static unsigned long order_comb_long[][2] =3D { }; #endif =20 +/* Assert that a boolean expression can be folded in a constant and is tru= e. */ +#define test_const_eval(test_expr) \ +({ \ + /* Evaluate once so that compiler can fold it. */ \ + bool __test_expr =3D test_expr; \ + \ + BUILD_BUG_ON(!__builtin_constant_p(__test_expr)); \ + BUILD_BUG_ON(!__test_expr); \ +}) + +/* + * On any supported optimization level (-O2, -Os) and if invoked with + * a compile-time constant argument, the compiler must be able to fold + * into a constant expression all the bit find functions. Namely: + * __ffs(), ffs(), ffz(), __fls(), fls() and fls64(). Otherwise, + * trigger a build bug. + */ +static __always_inline void test_bitops_const_eval(unsigned int n) +{ + test_const_eval(__ffs(BIT(n)) =3D=3D n); + test_const_eval(ffs(BIT(n)) =3D=3D n + 1); + test_const_eval(ffz(~BIT(n)) =3D=3D n); + test_const_eval(__fls(BIT(n)) =3D=3D n); + test_const_eval(fls(BIT(n)) =3D=3D n + 1); + test_const_eval(fls64(BIT_ULL(n)) =3D=3D n + 1); + test_const_eval(fls64(BIT_ULL(n + 32)) =3D=3D n + 33); +} + static int __init test_bitops_startup(void) { int i, bit_set; @@ -94,6 +122,10 @@ static int __init test_bitops_startup(void) if (bit_set !=3D BITOPS_LAST) pr_err("ERROR: FOUND SET BIT %d\n", bit_set); =20 + test_bitops_const_eval(0); + test_bitops_const_eval(10); + test_bitops_const_eval(31); + pr_info("Completed bitops test\n"); =20 return 0; --=20 2.25.1