From patchwork Fri Sep 3 23:34:47 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Hubicka X-Patchwork-Id: 63734 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 0AE69B7159 for ; Sat, 4 Sep 2010 09:34:57 +1000 (EST) Received: (qmail 7829 invoked by alias); 3 Sep 2010 23:34:55 -0000 Received: (qmail 7821 invoked by uid 22791); 3 Sep 2010 23:34:54 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL, BAYES_00, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from nikam-dmz.ms.mff.cuni.cz (HELO nikam.ms.mff.cuni.cz) (195.113.20.16) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 03 Sep 2010 23:34:49 +0000 Received: by nikam.ms.mff.cuni.cz (Postfix, from userid 16202) id 4375A9AC855; Sat, 4 Sep 2010 01:34:47 +0200 (CEST) Date: Sat, 4 Sep 2010 01:34:47 +0200 From: Jan Hubicka To: gcc-patches@gcc.gnu.org, rguenther@suse.de Subject: Fix constant folding of SWTCH statements Message-ID: <20100903233447.GR1664@kam.mff.cuni.cz> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.18 (2008-05-17) Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi, the following testcase from testsuite fails to get switch value folded to constant before it reach cfgexpand. The problem is twofold. First we require ctor to have TREE_STATIC (for some reason) that is not set by tree-switch-conversion.c. Secondly we screw up for variables introduced late because we do not compute varpool_decide_const_value_known. Fixed thus, Bootstrapped/regtested x86_64-linux, OK? Honza /* { dg-do compile } */ /* { dg-options "-O2 -fdump-tree-optimized" } */ void bar (unsigned int); void foo (void) { char buf[1] = { 3 }; const char *p = buf; const char **q = &p; unsigned int ch; switch (**q) { case 1: ch = 5; break; case 2: ch = 4; break; case 3: ch = 3; break; case 4: ch = 2; break; case 5: ch = 1; break; default: ch = 0; break; } bar (ch); } /* The switch should be switch converted and later constant propagated. */ /* { dg-final { scan-tree-dump-not "CSWTCH.1.2" "optimized"} } */ * tree-switch-conversion.c (build_one_array): Set ctor static flag. * varpool.c (varpool_finalize_decl): Set const_value_known. Index: tree-switch-conversion.c =================================================================== --- tree-switch-conversion.c (revision 163808) +++ tree-switch-conversion.c (working copy) @@ -518,6 +518,7 @@ build_one_array (gimple swtch, int num, array_type = build_array_type (value_type, arr_index_type); ctor = build_constructor (array_type, info.constructors[num]); TREE_CONSTANT (ctor) = true; + TREE_STATIC (ctor) = true; decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type); TREE_STATIC (decl) = 1; Index: varpool.c =================================================================== --- varpool.c (revision 163808) +++ varpool.c (working copy) @@ -423,6 +423,7 @@ varpool_finalize_decl (tree decl) there. */ else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)) varpool_mark_needed_node (node); + node->const_value_known |= varpool_decide_const_value_known (node); if (cgraph_global_info_ready) varpool_assemble_pending_decls (); }