GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
middle-end

Restore MEM_NOTRAP for memory-only loads

Restores non-trapping information for memory-only loads used in inline assembly and partial loads.

This commit addresses situations where memory-only loads lose their MEM_NOTRAP attribute. This occurs when EXPAND_MEMORY is used, which, due to conservative store semantics, discards the non-trapping information. The fix involves restoring the MEM_NOTRAP flag for inline assembly inputs and partial loads by explicitly setting it based on tree_could_trap_p after using EXPAND_MEMORY or EXPAND_WRITE.

In Details

The expand_asm_stmt and expand_partial_load_optab_fn in cfgexpand.cc and internal-fn.cc respectively, were using EXPAND_MEMORY or EXPAND_WRITE which strip MEM_NOTRAP. This commit restores the MEM_NOTRAP attribute for memory-only inline assembly inputs and for the source MEM in partial loads by calling tree_could_trap_p after the expansion.

For Context
MEM_NOTRAP
A flag indicating that a memory access is guaranteed not to trap (e.g., cause a segmentation fault or other exception). This allows the compiler to perform more aggressive optimizations, such as reordering or eliminating the access.
EXPAND_MEMORY
A GCC internal construct used in RTL (Register Transfer Language) expansion. It represents a memory operand and has conservative store semantics, meaning it may conservatively assume a memory write operation even if it's only a read.
Partial Load
A memory load operation that reads a subset of a larger memory unit, such as reading only the first few bytes of a 64-bit integer. This can involve specific instructions or masking.
Filed Under: optimizationmiddle-end