https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105160
Bug ID: 105160 Summary: [12 regression] ipa modref marks functions with asm volatile as const or pure Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: ipa Assignee: unassigned at gcc dot gnu.org Reporter: nsz at gcc dot gnu.org CC: marxin at gcc dot gnu.org Target Milestone: --- the following code is miscompiled with gcc -O1 #define sysreg_read(regname) \ ({ \ unsigned long __sr_val; \ asm volatile( \ "mrs %0, " #regname "\n" \ : "=r" (__sr_val)); \ \ __sr_val; \ }) #define sysreg_write(regname, __sw_val) \ do { \ asm volatile( \ "msr " #regname ", %0\n" \ : \ : "r" (__sw_val)); \ } while (0) #define isb() \ do { \ asm volatile( \ "isb" \ : \ : \ : "memory"); \ } while (0) static unsigned long sctlr_read(void) { return sysreg_read(sctlr_el1); } static void sctlr_write(unsigned long val) { sysreg_write(sctlr_el1, val); } static void sctlr_rmw(void) { unsigned long val; val = sctlr_read(); val |= 1UL << 7; sctlr_write(val); } void sctlr_read_multiple(void) { sctlr_read(); sctlr_read(); sctlr_read(); sctlr_read(); } void sctlr_write_multiple(void) { sctlr_write(0); sctlr_write(0); sctlr_write(0); sctlr_write(0); sctlr_write(0); } void sctlr_rmw_multiple(void) { sctlr_rmw(); sctlr_rmw(); sctlr_rmw(); sctlr_rmw(); } void function(void) { sctlr_read_multiple(); sctlr_write_multiple(); sctlr_rmw_multiple(); isb(); } aarch64-linux-gnu-gcc -O1 compiles it to (note 'function' and 'sctlr_rmw_multiple'): sctlr_rmw: mrs x0, sctlr_el1 orr x0, x0, 128 msr sctlr_el1, x0 ret sctlr_read_multiple: mrs x0, sctlr_el1 mrs x0, sctlr_el1 mrs x0, sctlr_el1 mrs x0, sctlr_el1 ret sctlr_write_multiple: mov x0, 0 msr sctlr_el1, x0 msr sctlr_el1, x0 msr sctlr_el1, x0 msr sctlr_el1, x0 msr sctlr_el1, x0 ret sctlr_rmw_multiple: ret function: isb ret a similar issue in linux (but lager source file) got bisected to https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=1b62cddcf091fb8cadf575246a7d3ff778650a6b commit 1b62cddcf091fb8cadf575246a7d3ff778650a6b Author: Jan Hubicka <j...@suse.cz> Date: Fri Nov 12 14:00:47 2021 +0100 Fix ipa-modref pure/const discovery PR ipa/103200 * ipa-modref.c (analyze_function, modref_propagate_in_scc): Do not mark pure/const function if there are side-effects. with -fdump-ipa-all $ grep found t.c.087i.modref Function found to be const: sctlr_rmw/2 Function found to be const: sctlr_read_multiple/3 Function found to be const: sctlr_write_multiple/4 Function found to be const: sctlr_rmw_multiple/5 even though t.c.086i.pure-const correctly identifies asm volatile as not const/pure.