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 154EC3861032 for ; Thu, 3 Dec 2020 12:04:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 154EC3861032 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 BC5C211D4; Thu, 3 Dec 2020 04:04:22 -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 1A1033F718; Thu, 3 Dec 2020 04:04:21 -0800 (PST) Subject: Re: Correct way to express to the compiler "this does not get clobbered"? To: Andrea Corallo , gcc-help@gcc.gnu.org Cc: nd@arm.com References: From: Richard Earnshaw Message-ID: <15f8e09a-9d21-f8f6-8611-2845bd8bc74e@foss.arm.com> Date: Thu, 3 Dec 2020 12:04:12 +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 12:04:24 -0000 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. R.