From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by sourceware.org (Postfix) with ESMTPS id 6EF08388E806 for ; Mon, 3 May 2021 08:39:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6EF08388E806 Received: by mail-lf1-x132.google.com with SMTP id 124so6959515lff.5 for ; Mon, 03 May 2021 01:39:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=u+hV8NRPsdfDPHD12/cKaX8Z5MEDEAP3sW7cptCxsDI=; b=QEa6VtzAK0LmqFV8DLMuabHvlvAbsDAiuYpfAK/zrEmVSFoei1nkDM+os/l4M20k8X fQpuQ75YgduJSd2RnuqUZjcbaP6uFSB6f/X8UuObxBUIyDU3ebCULy7ZTbJomxM5EmLF fQ/IdvsGMrjvdlYMeeikTs1iekkw5oKU/znKvzh6MJm/H/jj2dKx0qKbC1nllihVp7/i P8tfX6rNz/gAA2BC4PPQiGLGOJq2gevy9en8f7GBuSaVfZ9wtaq5oBX+yxTpemfEJJtS DQam2q4bw9mMHh4CcTXwMl6TsC6bo+Npl8MJrqo4tk2vRIyuuh4aOfgGg+GiCgpfDyO6 uutA== X-Gm-Message-State: AOAM533+itt36qTAqkG3bMoUQFCrOtQ3oFlxqLFOe8JcgAdAceBeX2ta v1+YzCpKxJ7x/nIPbangvA/WsqRo2FzgrA== X-Google-Smtp-Source: ABdhPJyb7ts6Ms9iKLp8m03DMWm2Gswq7Q3rGcFUbkBCGYF/V8N5IeChpjB+bAnW5PrulaYi9FuFOg== X-Received: by 2002:a05:6512:48c:: with SMTP id v12mr12238423lfq.132.1620031195777; Mon, 03 May 2021 01:39:55 -0700 (PDT) Received: from UNIT-1323.Labs.IntelliJ.Net ([178.252.116.27]) by smtp.gmail.com with ESMTPSA id t184sm1070802lff.208.2021.05.03.01.39.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 May 2021 01:39:55 -0700 (PDT) From: Ivan Sorokin To: gcc-patches@gcc.gnu.org Subject: [PATCH] [PR91400] build only one __cpu_model variable Date: Mon, 3 May 2021 11:39:48 +0300 Message-Id: <20210503083948.449-1-vanyacpp@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.0 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 autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 03 May 2021 08:39:59 -0000 Prior to this commit GCC -O2 generated quite bad code for this function: bool f() { return __builtin_cpu_supports("popcnt") && __builtin_cpu_supports("ssse3"); } f: movl __cpu_model+12(%rip), %eax xorl %r8d, %r8d testb $4, %al je .L1 shrl $6, %eax movl %eax, %r8d andl $1, %r8d .L1: movl %r8d, %eax ret The problem was caused by the fact that internally every invocation of __builtin_cpu_supports built a new variable __cpu_model and a new type __processor_model. Because of this GIMPLE level optimizers weren't able to CSE the loads of __cpu_model and optimize bit-operations properly. This commit fixes the problem by caching created __cpu_model variable and __processor_model type. Now the GCC -O2 generates: f: movl __cpu_model+12(%rip), %eax andl $68, %eax cmpl $68, %eax sete %al ret gcc/ChangeLog: PR target/91400 * config/i386/i386-builtins.c (fold_builtin_cpu): Extract building of __cpu_model and __processor_model into new function. * config/i386/i386-builtins.c (init_cpu_model_var): New. Cache creation of __cpu_model and __processor_model. gcc/testsuite/Changelog: PR target/91400 * gcc.target/i386/pr91400.c: New. --- gcc/config/i386/i386-builtins.c | 27 ++++++++++++++++++------- gcc/testsuite/gcc.target/i386/pr91400.c | 11 ++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr91400.c diff --git a/gcc/config/i386/i386-builtins.c b/gcc/config/i386/i386-builtins.c index b66911082ab..b7d9dd18b03 100644 --- a/gcc/config/i386/i386-builtins.c +++ b/gcc/config/i386/i386-builtins.c @@ -2103,6 +2103,25 @@ make_var_decl (tree type, const char *name) return new_decl; } +static GTY(()) tree __cpu_model_var; +static GTY(()) tree __processor_model_type; + +static void +init_cpu_model_var() +{ + if (__cpu_model_var != NULL_TREE) + { + gcc_assert (__processor_model_type != NULL_TREE); + return; + } + + __processor_model_type = build_processor_model_struct (); + __cpu_model_var = make_var_decl (__processor_model_type, + "__cpu_model"); + + varpool_node::add (__cpu_model_var); +} + /* FNDECL is a __builtin_cpu_is or a __builtin_cpu_supports call that is folded into an integer defined in libgcc/config/i386/cpuinfo.c */ @@ -2114,13 +2133,7 @@ fold_builtin_cpu (tree fndecl, tree *args) = (enum ix86_builtins) DECL_MD_FUNCTION_CODE (fndecl); tree param_string_cst = NULL; - tree __processor_model_type = build_processor_model_struct (); - tree __cpu_model_var = make_var_decl (__processor_model_type, - "__cpu_model"); - - - varpool_node::add (__cpu_model_var); - + init_cpu_model_var (); gcc_assert ((args != NULL) && (*args != NULL)); param_string_cst = *args; diff --git a/gcc/testsuite/gcc.target/i386/pr91400.c b/gcc/testsuite/gcc.target/i386/pr91400.c new file mode 100644 index 00000000000..e8b7d9285f9 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr91400.c @@ -0,0 +1,11 @@ +/* PR target/91400 */ +/* { dg-do compile { target { ! ia32 } } } */ +/* { dg-options "-O2" } */ +/* { dg-final { scan-assembler-times "andl" 1 } } */ +/* { dg-final { scan-assembler-times "68" 2 } } */ +/* { dg-final { scan-assembler-not "je" } } */ + +_Bool f() +{ + return __builtin_cpu_supports("popcnt") && __builtin_cpu_supports("ssse3"); +} -- 2.25.1