Thread: Compiler optimization: volatile

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    26

    Compiler optimization: volatile

    Hello,
    I have some doubts about compiler optimizing C code. This is more related to the field of embedded system.

    Code:
    int a, b;
    
    void experiment() {
        a = 8;
        b = a * 7;
        if(a == 8)
            printf("a equals 8!!!");
        else
            printf("A is NOT 8!!!");
    }
    In above code, inside the function experiment() value of a is not changing. So extreme optimization will change the code as follows

    Code:
    int a, b;
    
    void experiment() {
    a = 8;
    b = a * 7;
    
    printf("a equals 8!!!");
    }
    So in these cases it is necessary to declare variable a as volatile, since a could be modified by an external even ISR or multi threaded application.

    This is clear for me. But what if the code is as follows

    Code:
    int a, b;
    
    void experiment() {
        a = 8;
        b = a * 7;
        if(a == 8)
            printf("a equals 8!!!");
        else {
            printf("A is NOT 8!!!");
            a = 5;
        }
    }
    here a is changed to 5 inside else statement.

    Q1. Will this avoid optimization?


    Another example of using volatile is in loops.

    Code:
    void delay() {
        int x = 1000;
        while(x > 5)
            x--
    }
    I have seen in a tutorial that, if x in not declared volatile, compiler will optimize this.
    Why????
    Q2. Here value of x is modifying inside while loop. So what's the necessary of volatile here??

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    A1) No, it won't. It's the same situation as your previous example.

    A2) The variable changes in a predictable way. Optimization tries to squeeze as much speed as possible, so of course it'll get rid of pointless delays. ( Yes, from the compiler's viewpoint, that delay is pointless ). You shouldn't use busy loops for delay anyway. What you need is to delay based on some clock value, which would be unpredictable for the compiler and thus not optimizable. Your system does have interrupts, right?...
    Last edited by GReaper; 12-18-2018 at 05:44 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    26
    A1) No, it won't. It's the same situation as your previous example.
    How come? here a is changing inside. So how could compiler ignore it.

    You shouldn't use busy loops for delay anyway. What you need is to delay based on some clock value, which would be unpredictable for the compiler and thus not optimizable. Your system does have interrupts, right?
    Like when counter value reaches til a certain value?

    eg:

    if TCINT is a counter
    Code:
    while(TCINT != 1000);

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Athul View Post
    How come? here a is changing inside. So how could compiler ignore it.
    It may be changing inside, but based on its current value the else would never be reached, so the variable would never be changed.

    Quote Originally Posted by Athul View Post
    Like when counter value reaches til a certain value?

    eg:

    if TCINT is a counter
    Code:
    while(TCINT != 1000);
    If TCINT is some volatile variable that is changed automatically (through some CPU register or interrupt), then yes.
    Although you'd need the difference of the current time and the starting time, in order to wait for a specific amount of time.

    EDIT: Oh, you may also want to use some hardware pause instruction/function inside your loop, so as to avoid driving the controller at 100% while it waits for the time to change. Maybe a "wake-on-interrupt" would be appropriate. I don't know though, maybe it makes little difference if the controller is slow or if the interrupts are quite frequent...
    Last edited by GReaper; 12-20-2018 at 03:50 AM.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compiler optimization and num accuracy?
    By serge in forum C++ Programming
    Replies: 11
    Last Post: 06-12-2014, 03:50 AM
  2. To Volatile, or not to Volatile?
    By EVOEx in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2012, 02:07 PM
  3. Testing Compiler Optimization
    By jamesleighe in forum C++ Programming
    Replies: 4
    Last Post: 03-23-2012, 11:32 AM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. Replies: 10
    Last Post: 07-17-2008, 11:21 AM

Tags for this Thread