If Optimization

The conditional expression of an IF statement is known to be true in the then-clause and false in the else-clause, and this information can be used to simplify nested IF and other statements. In addition, two adjacent IF statements with the same conditional expressions can be combined into one IF statement.

Example:

In the code fragment below, the conditional-clause of the nested IF statement can be eliminated.

void f (int *p)
{
  if (p)
    {
      g(1);
      if (p) g(2);
      g(3);
    }
  return;
}
  

Below is the code fragment after the nested conditional-clause has been eliminated.

void f (int *p)
{
  if (p)
    {
      g(1);
      g(2);
      g(3);
    }
  return;
}
  

Example:

In the code fragment below, the two IF statements can be combined into one IF statement.

void f (int *p)
{
  if (p) g(1);
  if (p) g(2);
  return;
}
  

Below is the code fragment after the two IF statements have been combined into one IF statement.

void f (int *p)
{
  if (p)
    {
      g(1);
      g(2);
    }
  return;
}