GCC 16 doesn't optimize away an unused return value from mpz_class post-increment

Marc Glisse marc.glisse at inria.fr
Fri Feb 20 23:14:55 CET 2026


On Thu, 19 Feb 2026, martin_n_fuller wrote:

> GCC 16 doesn't optimize away the unused return value from mpz_class 
> post-increment in code like "mpz_class a = 1; a++;".

That's just normal C++ rules, nothing specific to GMP or gcc-16. There are 
very few cases where optimizations are allowed to change the output of a 
program (like the one you included in your email), and this is not one of 
them. And a++ both increments and returns the old value, so it has to 
create a separate object.

We could try to mark __gmp_allocate_func with 
__attribute__((malloc(__gmp_reallocate_func))), but gcc does not seem to 
use it to allow optimizations like it does for free(malloc(n)), so even 
with LTO it wouldn't help. The only option I can think of for this 
optimization would be to write a gcc plugin with specific knowledge of GMP 
functions, not worth the effort when almost noone would use it.

-- 
Marc Glisse


More information about the gmp-bugs mailing list