title: How to find what was optimized by compiler?
body: <p>I have a problem with my code. If I compile it with -O0 or -Og it seems to work ok.</p>
  <p>But if I use any other flag like -Os, -O1 etc it does not work. How to find what was optimized out by compiler?</p>
  <p>Compiler arm-none-eabi-g++ 8.3.1</p>
  <p>Source code: <a href="https://github.com/bielu000/stm32-libopencm3/tree/uart_not_working_version" rel="nofollow noreferrer">https://github.com/bielu000/stm32-libopencm3/tree/uart_not_working_version</a></p>
  <p>I added link to repo, because it would be hard to put the whole code here.
  Main code: src/app/src</p>
  <p>I think that the problem is in server_run function.</p>
  <p>Have a look at screen.</p>
  <p>On the left side (it works)-&gt;</p>
  <blockquote>
  <p>optimization:  -O1,<br />
  <strong>attribute</strong>((optimize(&quot;-O0&quot;))) void server_run();</p>
  </blockquote>
  <p>On the right side (it doesn't work)-&gt;</p>
  <blockquote>
  <p>optimization:  -O1,<br />
  void server_run();</p>
  </blockquote>
  <p>I don't see any calls to get buffer capacity in optimized (right) version. But why?</p>
  <p>Function body</p>
  <pre><code>extern &quot;C&quot; {
    #include &lt;libopencm3/stm32/usart.h&gt; 
    #include &lt;libopencm3/stm32/gpio.h&gt;
    #include &lt;libopencm3/stm32/rcc.h&gt;
    #include &lt;libopencm3/cm3/nvic.h&gt;
  }
  
  #include &lt;server.hpp&gt;
  #include &lt;stdint.h&gt;
  #include &lt;ring_buffer.hpp&gt;
  #include &lt;Os.hpp&gt;
  #include &lt;Timer.hpp&gt;
  #include &lt;target.h&gt;
  
  static uint8_t w_buffer[1024]; // write buffer
  static uint8_t r_buffer[1024]; // read buffer
  
  utils::containers::RingBuffer write_rb{w_buffer, sizeof(w_buffer)};
  utils::containers::RingBuffer read_rb{r_buffer, sizeof(r_buffer)};
  
  static void sendData()
  {
    if (write_rb.capacity() != 0)
    {
      usart_send(USART1, write_rb.read());
      usart_enable_tx_interrupt(USART1);
    }
    else 
    {
      usart_disable_tx_interrupt(USART1);
    }
  }
  
  static void readData()
  {
    auto data = usart_recv(USART1);
    read_rb.write(static_cast&lt;uint8_t&gt;(data));
  }
  
  void server_init()
  {
    //RCC
    rcc_periph_clock_enable(RCC_USART1);
  
    //GPIO
    gpio_set_mode(GPIO_BANK_USART1_TX, GPIO_MODE_OUTPUT_50_MHZ, 
      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
  
    gpio_set_mode(GPIO_BANK_USART1_RX, GPIO_MODE_INPUT, 
      GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO_USART1_RX);
  
    //USART
    usart_set_mode(USART1, USART_MODE_TX_RX);
    usart_set_baudrate(USART1, 9600);
    usart_set_parity(USART1, USART_PARITY_NONE);
    usart_set_databits(USART1, 8);
    usart_set_stopbits(USART1, 1);
    usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
    usart_enable_rx_interrupt(USART1);
  
    //ISR
    nvic_enable_irq(NVIC_USART1_IRQ);
    
    //Enable 
    usart_enable(USART1);
  }
  
  void server_run()
  {
    while(true)
    {
      size_t xsize = read_rb.capacity();
      if (xsize &gt;= 64)
      {
        while (read_rb.capacity() != 0)
        {
          write_rb.write(read_rb.read());
        }
        sendData();
      }
    }
  }
  
  void usart1_isr()
  {
    if (usart_get_flag(USART1, USART_FLAG_TXE) != 0) 
    {
      sendData();
    }
  
    if (usart_get_flag(USART1, USART_FLAG_RXNE) != 0) // when data is ready to read
    {
      readData(); 
    }
  } 
  </code></pre>
  <p>UPDATE:</p>
  <p>I changed <strong>xsize</strong> variable type to</p>
  <blockquote>
  <p>std::atomic&lt;size_t&gt; xsize = read_rb.capacity();</p>
  </blockquote>
  <p>and now it works even with -Os. But why?</p>
  <p><a href="https://i.stack.imgur.com/dIBL3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dIBL3.png" alt="enter image description here" /></a></p>