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 C6DF73858D35 for ; Wed, 15 Feb 2023 14:30:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C6DF73858D35 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=foss.arm.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=foss.arm.com 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 C628D1042; Wed, 15 Feb 2023 06:31:38 -0800 (PST) Received: from [10.57.52.9] (unknown [10.57.52.9]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 975383F663; Wed, 15 Feb 2023 06:30:55 -0800 (PST) Message-ID: Date: Wed, 15 Feb 2023 14:30:54 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: Could __builtin_printf parameters be optimized when being compiled Content-Language: en-GB To: Jonny Grant , gcc-help References: <03717f06-9818-0c2a-6625-429887c55a17@jguk.org> From: Richard Earnshaw In-Reply-To: <03717f06-9818-0c2a-6625-429887c55a17@jguk.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3489.6 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 15/02/2023 14:18, Jonny Grant wrote: > Hi > Has GCC considered an improvement to "compile out" from the builtin printf the strings? That being to change it to just be something like puts("file /app/example.cpp:4") > I had a look, but couldn't find it being asked before. > > This is just a short example to demonstrate. > It would be useful to see the exact string in the debugger "file /app/example.cpp:4", also it saves a few lines of asm. > > https://godbolt.org/z/aKz3o6aPd > > > int main() > { > __builtin_printf("file %s:%d", __FILE__, __LINE__); > } > > > .LC0: > .string "/app/example.cpp" > .LC1: > .string "file %s:%d" > main: > subq $8, %rsp > movl $4, %edx > movl $.LC0, %esi > xorl %eax, %eax > movl $.LC1, %edi > call printf > xorl %eax, %eax > addq $8, %rsp > ret We already do when the printf contains simply the format string and no additional arguments. I guess it might be possible to handle cases where all the arguments are constant, but even that has its problems, eg: - can we guarantee identical output to the platform printf? - does it cause string bloat (what if there were 30 or so such statements in your program all identical except for the line number)? - does it even happen often enough to be worth adding (and maintaining) support? Nothing comes for free in a compiler and the optimisations have to be worth-while in the real world. R.