https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104800

            Bug ID: 104800
           Summary: reodering of potentially trapping operations and
                    volatile stores
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: muecker at gwdg dot de
  Target Milestone: ---

In the following example, a potentially trapping operation is moved before the
store to the volatile variable.  This can change observable behavior.  Because
division by zero is UB this is a correct optimization in C++ where UB is
allowed to affect previous observable behavior.  For C, I believe that this is
not allowed by the standard. In any case, it annoying (e.g. when debugging of
embedded devices) and dangerous (e.g. in device drivers or when controling
machines) and best avoided.


volatile int x;

int foo(int a, int b, _Bool store_to_x)
{
  if (!store_to_x)
    return a / b;
  x = b;
  return a / b;
}

Reply via email to