User account creation filtered due to spam.

Bug 37780 - Conditional expression with __builtin_clz() should be optimized out
Summary: Conditional expression with __builtin_clz() should be optimized out
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.0
: P3 enhancement
Target Milestone: ---
Assignee: ktkachov
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2008-10-09 07:12 UTC by Tobias Burnus
Modified: 2016-06-15 10:20 UTC (History)
3 users (show)

See Also:
Host:
Target: CLZ_DEFINED_VALUE_AT_ZERO != 0
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-03-28 16:33:36


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2008-10-09 07:12:07 UTC
Follow up to PR 37635.

From trans-intrinsic.c:
/* LEADZ (i) = (i == 0) ? BIT_SIZE (i)
                        : __builtin_clz(i) - (BIT_SIZE('int') - BIT_SIZE(i))

   The conditional expression is necessary because the result of LEADZ(0)
   is defined, but the result of __builtin_clz(0) is undefined for most
   targets.
[...]
  /* ??? For some combinations of targets and integer kinds, the condition
         can be avoided if CLZ_DEFINED_VALUE_AT_ZERO is used.  Later.  */
Comment 1 Francois-Xavier Coudert 2009-03-28 16:33:36 UTC
This is not a Fortran issue, it should be done in the middle-end. On platforms that have a defined value for __builtin_clz(0), the conditional in the function below should be optimized out:

int foo (int i)
{
  return (i == 0) ? sizeof (int) * 8 : __builtin_clz (i);
}

Targets where this is expected to be an issue are: alpha, cris, rs6000, m68k, arm and s390.
Comment 2 bin.cheng 2012-03-20 07:58:09 UTC
the special case could be easily detected when gimplifying.
but actually I am not sure whether it can be done even in middle end, since the middle end should not depend on any target information, like CLZ_DEFINED_VALUE_AT_ZERO, right?
Comment 3 ktkachov 2016-04-29 16:53:29 UTC
I'll have a look
Comment 4 ktkachov 2016-06-06 16:00:21 UTC
Author: ktkachov
Date: Mon Jun  6 15:59:48 2016
New Revision: 237138

URL: https://gcc.gnu.org/viewcvs?rev=237138&root=gcc&view=rev
Log:
[1/3][ARM] Keep ctz expressions together until after reload

	PR middle-end/37780
	* config/arm/arm.md (ctzsi2): Convert to define_insn_and_split.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/arm/arm.md
Comment 5 ktkachov 2016-06-06 16:02:19 UTC
Author: ktkachov
Date: Mon Jun  6 16:01:47 2016
New Revision: 237139

URL: https://gcc.gnu.org/viewcvs?rev=237139&root=gcc&view=rev
Log:
[2/3][AArch64] Keep CTZ components together until after reload

	PR middle-end/37780
	* config/aarch64/aarch64.md (ctz<mode>2): Convert to
	define_insn_and_split.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/aarch64/aarch64.md
Comment 6 ktkachov 2016-06-06 16:06:37 UTC
Author: ktkachov
Date: Mon Jun  6 16:06:05 2016
New Revision: 237141

URL: https://gcc.gnu.org/viewcvs?rev=237141&root=gcc&view=rev
Log:
[3/3][RTL ifcvt] PR middle-end/37780: Conditional expression with __builtin_clz() should be optimized out

	PR middle-end/37780
	* ifcvt.c (noce_try_ifelse_collapse): New function.
	Declare prototype.
	(noce_process_if_block): Call noce_try_ifelse_collapse.
	* simplify-rtx.c (simplify_cond_clz_ctz): New function.
	(simplify_ternary_operation): Use the above to simplify
	conditional CLZ/CTZ expressions.

	* gcc.c-torture/execute/pr37780.c: New test.
	* gcc.target/aarch64/pr37780_1.c: Likewise.
	* gcc.target/arm/pr37780_1.c: Likewise.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr37780.c
    trunk/gcc/testsuite/gcc.target/aarch64/pr37780_1.c
    trunk/gcc/testsuite/gcc.target/arm/pr37780_1.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/ifcvt.c
    trunk/gcc/simplify-rtx.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 ktkachov 2016-06-15 10:20:47 UTC
Fixed for GCC 7