Is the compiler allowed to recycle freed pointer variables?

Upon an object reaching the end of its lifetime, all pointers to it become indeterminate. This applies to block-scope variables and to malloced memory just the same. The applicable clause is, in C11, 6.2.4:2. The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. … Read more

GCC(/Clang): Merging functions with identical instructions (COMDAT folding)

Neither GCC nor Clang is a linker, and ICF needs to be done by the linker, or at least with cooperation with the linker. Edit: They don’t do ICF, so yes, distinct instantiations produce distinct code. The GNU gold linker supports ICF with the –icf option, which needs the GCC option -ffunction-sections to be used. … Read more

Change default value of CMAKE_CXX_FLAGS_DEBUG and friends in CMake

I just wanted to add the four possibilities I see: Having your own toolchain files containing the presets for each compiler you support like: GNUToolchain.cmake set(CMAKE_CXX_FLAGS_DEBUG “-ggdb3 -O0” CACHE STRING “”) And then use it with cmake -DCMAKE_TOOLCHAIN_FILE:string=GNUToolchain.cmake … You can try to determine the compiler by checking CMAKE_GENERATOR (which is valid before the project() … Read more

Can compiler optimization introduce bugs?

Compiler optimizations can introduce bugs or undesirable behaviour. That’s why you can turn them off. One example: a compiler can optimize the read/write access to a memory location, doing things like eliminating duplicate reads or duplicate writes, or re-ordering certain operations. If the memory location in question is only used by a single thread and … Read more

How does memory reordering help processors and compilers?

TL;DR: It gives the compiler and hardware more room to take advantage of the as-if rule by not requiring it to preserve all behaviour of the original source, only the result of the single thread itself. Taking the externally-observable (from other threads) ordering of loads/stores out of the picture as something that optimizations must preserve … Read more

Avoid stalling pipeline by calculating conditional early

Yes, it can be beneficial to allow the the branch condition to calculated as early as possible, so that any misprediction can be resolved early and the front-end part of the pipeline can start re-filling early. In the best case, the mis-prediction can be free if there is enough work already in-flight to totally hide … Read more