MSVC and constant expression optimization

The project I'm working on is written in C and uses a macro processor to handle errors.

A macro looks something like this:

#define logevent(level, msg) \
    do {
        int _level = level; \
        somefunction(_level, msg); \
        someotherfunction(_level, msg); \
        if (_level >= ERROR) \
          __assume(0); \
    } while(0)

Let's say that some function executes the exit (1) if the level> = ERROR, and we never go to the if condition when calling logevent (ERROR, "something"); where ERROR is a specific constant. The problem is that the MSVC does not seem to be able to optimize the if condition because the condition is based on a level variable rather than a level constant. The _level variable is required to stop multiple evaluations of a level expression.

Some other compilers seem to be able to optimize if, but I wonder if this is limited to the MSVC compiler, or is there something I can include to get the compiler to optimize these conditions if not present?

+4
source share
1 answer

MSVC 2013 optimizes our expression. Next entry

#define ERROR 1

void somefunction(int level, char const* msg) {
  printf("FUNC1 %d: %s\n", level, msg);
}

void someotherfunction(int level, char const* msg) {
  printf("FUNC2 %d: %s\n", level, msg);

  if (level >= ERROR) {
    exit(1);
  }
}

#define logevent(level, msg) \
  do { \
    int _level = level; \
    somefunction(_level, msg); \
    someotherfunction(_level, msg); \
    if (_level >= ERROR) \
      __assume(0); \
    } while (0)

int _tmain(int argc, _TCHAR* argv[])
{
  logevent(ERROR, "HALLO");
  printf("Hallo\n");
  getchar();
  return 0;
}

will compile in

00CB1000  push        0CB2120h  
00CB1005  push        1  
00CB1007  push        0CB2100h  
00CB100C  call        dword ptr ds:[0CB2090h]  
00CB1012  push        0CB2120h  
00CB1017  push        1  
00CB1019  push        0CB2110h  
00CB101E  call        dword ptr ds:[0CB2090h]  
00CB1024  add         esp,18h  
00CB1027  push        1  
00CB1029  call        dword ptr ds:[0CB208Ch] 
+7
source

All Articles