Talk:Loop-invariant code motion

This is an old revision of this page, as edited by 159.28.7.5 (talk) at 17:53, 10 April 2007. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Latest comment: 18 years ago by DavidHopwood

The page originally had the following:

This can then be further optimized, leading to less overall executed code for larger values of maxval and/or smaller values of calcval.

int calcval = (4+array[k])*pi+5;
j = j + integer_part((maximum - 1 - j) / calcval) * calcval;

However, that transformation is not "loop-invariant code motion". In any case it is not obviously correct when maximum is near overflow (well, OK, maybe it's undefined behaviour then, but suppose that j was unsigned). --DavidHopwood 01:16, 29 January 2007 (UTC)Reply


As of 10 Apr 2007, this article has:

while (j < maximum - 1)
{
     j = j + ( 4 + array[ k ] ) * pi + 5; 
}

The calculation of maximum - 1 and (4+array[k])*pi+5 can be moved outside the loop.

This makes an assumption that the program in question is not threaded. Otherwise, what would be the guarantee that no other thread would modify the value of maximum while this thread was executing that loop? (As for the pre-calculation?)

--Kevin