From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id E35B23858023 for ; Thu, 3 Dec 2020 13:06:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E35B23858023 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 7CE7711D4; Thu, 3 Dec 2020 05:06:58 -0800 (PST) Received: from [192.168.1.19] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C5F613F718; Thu, 3 Dec 2020 05:06:57 -0800 (PST) Subject: Re: Correct way to express to the compiler "this does not get clobbered"? To: Andrea Corallo Cc: gcc-help@gcc.gnu.org, nd@arm.com References: <15f8e09a-9d21-f8f6-8611-2845bd8bc74e@foss.arm.com> From: Richard Earnshaw Message-ID: <1632b7ed-dd89-a53e-8367-a8e29d34bdf4@foss.arm.com> Date: Thu, 3 Dec 2020 13:06:45 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3492.4 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, NICE_REPLY_A, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=no 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-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Dec 2020 13:07:03 -0000 On 03/12/2020 12:28, Andrea Corallo wrote: > Richard Earnshaw writes: > >> On 03/12/2020 11:47, Andrea Corallo via Gcc-help wrote: >>> Hi all, >>> >>> I've a piece of code that reduced looks like this: >>> >>> #+begin_src C >>> typedef struct { >>> void (*fun_ptr)(void); >>> } x_t; >>> >>> x_t *x; >>> >>> void >>> f (void) >>> { >>> const x_t const *y = x; >>> for (int i = 0; i < 1000; ++i) >>> y->fun_ptr (); >>> } >>> #+end_src >>> >>> What is the correct way (if any) to express to the compiler that the >>> value of y->fun_ptr does not get clobbered by the function call itself >>> so the corresponding load to obtain its value can be moved out of the >>> loop? >>> >>> My understanding is that the const qualifier is more for diagnostic >>> reasons and is not sufficient for GCC to make this assumption. OTOH I >>> cannot give 'fun_ptr' the attribute pure as it's not. >>> >>> Thanks >>> >>> Andrea >>> >> >> Why not just put the function pointer in a local variable? Then the >> compiler will know that the value can't change. > > Hi Richard, > > yeah that would do the job. > > This is generated code and changing the code generator that way might > not be completely trivial (e.g. where is the best position to perform > the assignments to each local variable?). Therefore before going for > this solution I'd like to be convinced there's no way to express this > directly to GCC. > > Andrea > Perhaps you could declare y with the restrict qualifier? But that would need to apply to all of *y, not just fun_ptr. R.