Hoisting

Loop-invariant expressions can be hoisted out of loops, thus improving run-time performance by executing the expression only once rather than at each iteration.

Example:

In the code fragment below, the expression (x + y) is loop invariant, and the addition can be hoisted out of the loop.

void f (int x, int y)
{
  int i;

  for (i = 0; i < 100; i++)
    {
      a[i] = x + y;
    }
}
  

Below is the code fragment after the invariant expression has been hoisted out of the loop.

void f (int x, int y)
{
  int i;
  int t;

  t = x + y;
  for (i = 0; i < 100; i++)
    {
      a[i] = t;
    }
}
  

Notes:

Some C compilers can hoist a subset of loop-invariant expressions (e.g. integer addition, subtraction, and multiplication), but few compilers can hoist a wide range of expressions (e.g. left shift, right shift, etc.).

Most compilers reduce expressions with sequence operators (i.e. &&, ||, and ?:) to if-then-else blocks early in the compiler, and most compilers fail to hoist loop-invariant expressions containing sequence operators.