• AVR Freaks

Hot!Code Generation using constant expressions in an IF statement.

Author
BruceH
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2006/09/25 10:59:31
  • Location: SF Bayarea
  • Status: offline
2019/08/31 11:14:45 (permalink)
0

Code Generation using constant expressions in an IF statement.

Consider the if statement is C.  In general, the if statement has the form: 
if ( <conditional expression>) <statement>
.
During execution, it tests the <conditional expression> and if it evaluates to "true" then <statement> is executed; otherwise <statement> is skipped.
 
The question:
Suppose <conditional expression> is a constant expression that evaluates to "true".  Is this logically equivalent to <statement> so that the compiler generates code as if the 
if ( <conditional expression>) 
were not there?
 
Similarly if <conditional expression> evaluates to "false" then does the compiler generate no code for the entire if compound statement? 
 
Currently, in XC8 2.10 I have the compound statement:

    #define IUL_TMR0 32
    if ( IUL_TMR0 != 0 )
    {    if ( PIE0bits.TMR0IE && PIR0bits.TMR0IF && (!IPR0bits.TMR0IP) )
         {    TickUpdate(); PIR0bits.TMR0IF = 0;   }
    }

 
I get for the line, "if (IUL_TMR0 != 0 )" the following warning:
 
source/BclInterrupt.c:286:: warning: (759) expression generates no code
 
The idea here is to use a constant expressions to include code if the constant expression evaluates to "true" and exclude the code if it evaluates to "false".  The constant expression compares the mask value of an interrupt register to 0.  This value is set if the interrupt is used in the program or remains zero if the interrupt is not used.  Thus only interrupts in use get compiled into the main interrupt service routine.
 
 
#1

7 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3056
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Code Generation using constant expressions in an IF statement. 2019/08/31 12:48:50 (permalink)
    +1 (1)
    Your current if() evaluates to

    if (32 != 0)

    As this is always true, why should the compiler generate code for something that never changes?
    This is, what the warning is indicating. (OK - in the given contect a "warning" doesn't make much sense. But formally it is ok that a warning is issued.)

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    nigelwright7557
    Super Member
    • Total Posts : 296
    • Reward points : 0
    • Joined: 2006/11/06 08:15:51
    • Location: 0
    • Status: offline
    Re: Code Generation using constant expressions in an IF statement. 2019/08/31 15:16:51 (permalink)
    -1 (1)
    I would get rid of the constant expression and use a variable instead.
    int debugmode=1;
     
    if (debugmode!=0)
    writeline("USB opened ok";
     
    #3
    ric
    Super Member
    • Total Posts : 23836
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Code Generation using constant expressions in an IF statement. 2019/08/31 15:21:19 (permalink)
    +1 (1)
    vbhunt
    ...
    The idea here is to use a constant expressions to include code if the constant expression evaluates to "true" and exclude the code if it evaluates to "false".  The constant expression compares the mask value of an interrupt register to 0.  This value is set if the interrupt is used in the program or remains zero if the interrupt is not used.  Thus only interrupts in use get compiled into the main interrupt service routine.

    If it's always a constant expression, use #if() instead of if(), and let the pre-compiler handle it.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11338
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Code Generation using constant expressions in an IF statement. 2019/08/31 15:45:27 (permalink)
    +2 (2)
    The warning means no code is generated for "if ( IUL_TMR0 != 0 )".  It doesn't mean no code is generated for the rest of the code.
     
    You could also use #if to achieve the same effect, and this would not rely on the compiler optimizing anything nor would it generate a warning.
    #5
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11338
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Code Generation using constant expressions in an IF statement. 2019/08/31 18:09:55 (permalink)
    +1 (1)
    I would get rid of the constant expression and use a variable instead.
    int debugmode=1;
     
    if (debugmode!=0)

     
    That accomplishes nothing since the compiler knows the value of debugmode at compile time, and also knows that it's not set anywhere else, so it can (and probably will) make the same optimization.
    #6
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3286
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: online
    Re: Code Generation using constant expressions in an IF statement. 2019/09/01 04:08:35 (permalink)
    0
    "Similarly if <conditional expression> evaluates to "false" then does the compiler generate no code for the entire if compound statement"
     
    At compile time: Yes and it is not a compound statement, it inserts blocks of code or turns the compiler off or on.
     
    Your program constantly chances the program counter when running.
     
    Pre-processor is the builder and it only knows the PC as the code is constructed in an incremental fashion.
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #7
    andersm
    Super Member
    • Total Posts : 2651
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: Code Generation using constant expressions in an IF statement. 2019/09/01 04:46:29 (permalink)
    +1 (1)
    ricIf it's always a constant expression, use #if() instead of if(), and let the pre-compiler handle it.

    The drawback of using preprocessor statements is that the compiler can't check the bits stripped out by the preprocessor. The advantage is that the compiler is guaranteed to not generate code for those parts, while it depends on the optimizer in the other case.
    #if TARGETA
      func(a, b, c); // func was changed to take one more parameter
    #else
      func(a, x); // won't be caught until built for this target
    #endif
     
    if (TARGETA) {
      func(a, b, c);
    }
    else {
      func(a, x); // caught immediately
    }

    #8
    Jump to:
    © 2019 APG vNext Commercial Version 4.5