Loop-Invariant Code Motion and Safe Hoisting
Move a computation to a loop preheader only after proving its operands stay stable and that earlier evaluation is legal.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
Invariance starts with dependencies
Consider a repeated pure expression \(a+b\) whose operands are defined outside the loop and do not change. Compute it once in a preheader and reuse the result inside. If the operands are loads, a loop store or call may change them through an alias. Invariance then depends on a memory dependence check, not simply on whether a source name is reassigned.
Earlier execution can introduce new behavior
A loop may have zero iterations. Hoisting a potentially trapping operation out of its body can then cause a trap on an input that previously skipped it. A transformation needs either speculation safety or a proof the original operation would run under the relevant conditions. It must also preserve value availability at all replacement uses. Dominator trees describe that placement constraint. Translation validation shows how to check a concrete transformation against its semantic model.[1][2]