From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13310 invoked by alias); 8 Nov 2013 09:28:10 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 13288 invoked by uid 89); 8 Nov 2013 09:28:09 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=2.5 required=5.0 tests=AWL,BAYES_50,FREEMAIL_FROM,RDNS_NONE,SPAM_SUBJECT,SPF_PASS autolearn=no version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-qa0-f45.google.com Received: from Unknown (HELO mail-qa0-f45.google.com) (209.85.216.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Fri, 08 Nov 2013 09:28:08 +0000 Received: by mail-qa0-f45.google.com with SMTP id hu16so1460640qab.4 for ; Fri, 08 Nov 2013 01:28:00 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.224.36.201 with SMTP id u9mr21928753qad.76.1383902880664; Fri, 08 Nov 2013 01:28:00 -0800 (PST) Received: by 10.229.220.201 with HTTP; Fri, 8 Nov 2013 01:28:00 -0800 (PST) Date: Fri, 08 Nov 2013 09:28:00 -0000 Message-ID: Subject: How can I tune gcc to move up simple common subexpression? From: Konstantin Vladimirov To: gcc@gcc.gnu.org, gcc-help@gcc.gnu.org Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2013-11/txt/msg00044.txt.bz2 Hi, Consider simple code: typedef struct { unsigned prev; unsigned next; } foo_t; void foo( unsigned x, unsigned y) { foo_t *ptr = (foo_t *)((void *)x); if (y != 0) { ptr->prev = y; ptr->next = x; } else { ptr->prev = 0; /* or explicitly ptr->prev = y; no difference */ ptr->next = 0; } } GCC 4.7.2 and 4.8.1 both on O2 and Os creates code like: testl %esi, %esi movl %edi, %eax jne .L5 movl $0, (%edi) movl $0, 4(%rax) ret .L5: movl %esi, (%edi) movl %edi, 4(%rax) ret Which can be obviously changed to: testl %esi, %esi movl %edi, %eax movl %esi, (%edi) jne .L5 movl $0, 4(%rax) ret .L5: movl %edi, 4(%rax) ret May be there are some options to make it behave so? This is question for gcc-help group. Question for gcc group is trickier: May be in x86 it is not a big deal, but I am working on my private backend, that have predicated instructions and second form is really much more prefferable. May be I can somehow tune my backend to achieve this effect? I can see that 210r.csa pass (try_optimize_cfg before it, not pass itself) can move code upper in some cases, but only with very simple memory addressing and rather unstable, say changing ptr->prev = y; ptr->next = x; to ptr->prev = x; ptr->next = y; may break everything just because next is second member and addressed like M[%r+4]. Any ideas? --- With best regards, Konstantin