Thread: Turn Off Optimization?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47

    Turn Off Optimization?

    I'm compiling some 'C' code. I initialize some variables, then use these initialized variables in a decision block with branches that depend on the value. Since the variables have been initialized to something, they will always take the same branch.

    However these decision blocks are inside a 'while loop', and interrupts or other code can change the value, causing another branch to be taken.

    My compiler seeing the unreachable branch at compile time optimizes out the code for this branch.

    The normal way to fix this undesired optimization is to declare the variable 'volatile'. If that's done then the compiler will not consider the branch unreachable.

    I have quite a few of these decision blocks in my code, with a large number of variables that should be declared volatile. Volatile variables cause more code to be generated, since the compiler will reload the variable each time that it's used. Since I have a limited amount of code space, I don't want to declare a lot of variables volatile.

    I want to turn off compiler optimization that eliminates unreachable branches, but keeps all other optimization.

    My present optimization is -Os.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So leave off -O altogether.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    Without Optimization the code exceeds the allocated space. I only want to turn off the optimization that eliminates unreachable branches.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by danlee58 View Post
    Without Optimization the code exceeds the allocated space. I only want to turn off the optimization that eliminates unreachable branches.
    I don't see why you won't just use "volatile." Without it, various other optimizations might be made that are inappropriate. You don't want the compiler to "waste" code by reloading variables, yet that's exactly what needs to happen when more than one execution thread can change the variable.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > The normal way to fix this undesired optimization is to declare the variable 'volatile'. If that's done then the compiler will not consider the branch unreachable.
    So what's wrong with this way?

    I think whatever you do, you're going to have to edit the code.

    > I have quite a few of these decision blocks in my code, with a large number of variables that should be declared volatile.
    Is each decision variable (which should be volatile) inspected only once per decision?

    Like
    Code:
    switch ( var ) {
    }
    And not like
    Code:
    switch ( var ) {
       ///
          if ( var ) 
    }

    You could try something like
    Code:
    extern int magic;
    switch ( var | magic ) {
    }
    Where magic is initialised elsewhere to 0.
    It won't change the result, but it will force the compiler to do that little bit of extra work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by brewbuck View Post
    I don't see why you won't just use "volatile." Without it, various other optimizations might be made that are inappropriate. You don't want the compiler to "waste" code by reloading variables, yet that's exactly what needs to happen when more than one execution thread can change the variable.
    Seconded.

    You're saying that you don't want the compiler to do exactly what you actually need it to do. Just declare it volatile.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I agree with iMalc and brewbuck - if you have a variable that changes due to interrupts or other "compiler don't follow the change" circumstances, volatile is exactly the right solution.

    Then you should be able to have full optimization and the resulting space saving.

    Is there any particular reason(s) NOT to do this?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to approach this c program
    By jaykay in forum C Programming
    Replies: 13
    Last Post: 08-17-2008, 05:43 AM
  2. need reading material for c++ database optimization
    By elninio in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2008, 11:32 PM
  3. I cant turn my Computer off
    By h3ro in forum Tech Board
    Replies: 9
    Last Post: 03-25-2008, 06:51 PM
  4. Optimization settings
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-23-2005, 02:53 AM
  5. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 10:07 PM