https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85611
Bug ID: 85611 Summary: Suboptimal code generation for (potentially) redundant atomic loads Product: gcc Version: tree-ssa Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: hiraditya at msn dot com Target Milestone: --- $ cat test.cpp #include <atomic> std::atomic<int> atomic_var{100}; int somevar; bool cond; void run1() { auto a = atomic_var.load(std::memory_order_relaxed); auto b = atomic_var.load(std::memory_order_relaxed); // Some code using a and b; } void run2() { if (atomic_var.load(std::memory_order_relaxed) == 2 && cond) { if (atomic_var.load(std::memory_order_relaxed) * somevar > 3) { /*...*/ } } } $ g++-7 -O3 -std=c++17 -S -o - test.cpp -fno-exceptions .text .align 4,0x90 .globl __Z4run1v __Z4run1v: LFB339: movl _atomic_var(%rip), %eax movl _atomic_var(%rip), %eax ret LFE339: .align 4,0x90 .globl __Z4run2v __Z4run2v: LFB340: movl _atomic_var(%rip), %eax cmpl $2, %eax je L5 L3: ret .align 4,0x90 L5: cmpb $0, _cond(%rip) je L3 movl _atomic_var(%rip), %eax ret