You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 22 Next »

Loop Predication

Loop Predication is an optimization in C2. The general idea is to insert a predicate on the entry path to a loop,
and raise a uncommon trap if the check of the condition fails. The condition checks are promoted from inside the
loop body, and thus the checks inside the loop could be eliminated. Currently, loop predication optimization has
been applied to remove array range check and loop invariant checks (such as null checks and array checks).

  Array Range Check Elimination

    Use loop predicate to remove array range checks in a loop.

original loop

for (int i = init,  i < limit;  i += stride) {  // loop with array range check
    ...
    if (scale*i + offset < a.length) {  // array range check
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c5a7b2bd-f686-42d8-97b1-43c96b05cb94"><ac:plain-text-body><![CDATA[        ... a[scale*i+offset] ...
]]></ac:plain-text-body></ac:structured-macro>
    } else raise_uncommon_trap();
    ...
}

after loop predication

if ( scale * imax + offset < a.length ) {  // loop predicate. imax is the maximum value of i where init <= i < limit
    for (int i = init,  i < limit;  i += stride) {  // loop without array range check
         ...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0d613126-0cce-4d14-ad55-7c27a80a62ec"><ac:plain-text-body><![CDATA[         ... a[scale*i+offset] ...
]]></ac:plain-text-body></ac:structured-macro>
         ...
    }
} else raise_uncommon_trap();

   From the above example, the requirements to perform loop predication for array range check elimination are that

   init, limit, offset and array a are loop invariants, and stride and scale are compile time constants.

  Loop Invariant Check Elimination

  Similar to array range check elimination, but the loop invariant check elimination is much simple. The check inside the

  loop is loop invariant, and can be promoted outside the loop directly. Current implementation of loop invariant check

  elimination includes null check elimination and array check elimination, etc.

Loop Predication Advantages

  • No labels