diff options
Diffstat (limited to 'Notes')
-rw-r--r-- | Notes/MIR-Optimisations.md | 99 |
1 files changed, 77 insertions, 22 deletions
diff --git a/Notes/MIR-Optimisations.md b/Notes/MIR-Optimisations.md index 759bfa2e..e83f7cd3 100644 --- a/Notes/MIR-Optimisations.md +++ b/Notes/MIR-Optimisations.md @@ -19,8 +19,8 @@ At the end of the block, remove original tuple assignment and propagate the valu -De-Temporary -============ +De-Temporary (version 1) +======================== Purpose: Eliminate useless temporaries ``` @@ -31,20 +31,19 @@ _2 = foo(_1) Algorithm --------- -Where the pattern `Local(N) = Use(...)` is seen, record that statement as the assignment for `Local(N)`. +- Where the pattern `Local(N) = Use(...)` is seen, record that statement as the assignment for `Local(N)`. +- Detect any uses of `Local(N)` in mutation context (e.g. `Field(Local(N), 0) = ...`) or as a borrow and remove from the + list (as these cause the local to diverge from its source). +- Detect any by-value or mutation uses of the origin value that would mean that moving the assignment forwards would + lead to a use-after-free or incorrect value. -Detect any uses of `Local(N)` in mutation context (e.g. `Field(Local(N), 0) = ...`) or as a borrow and remove from the -list (as these cause the local to diverge from its source). - -Detect any by-value or mutation uses of the origin value that would mean that moving the assignment forwards would -lead to a use-after-free or incorrect value. - -If a recorded local is used, replace that usage by the original source. If the value was !Copy, and the use is a move, - delete the original assignment. -- CATCH: Detecting a by-move of !Copy is hard when a Copy value is used from a !Copy struct, needs knowledge of the - types involved. -- CATCH: Since partial moves are valid, the !Copy rule above is invalid if it would have been a partial move. - - Only allow use of a !Copy result when it's just the slot. +- Find any uses of locals recorded locals + - If this is of a Copy value, the replacement + - Copy values can be reused freely + - If the value is used at the top level of the source RValue, do the replacemnet and remove the record + - !Copy value moved, so can't be moved again. + - Otherwise, remove the record + - The value was used, so then assignment can't be replaced with later and deleted. Pseudocode ---------- @@ -67,15 +66,22 @@ for stmt in block.statements fn do_replacements(stmt) { visit_lvalues(stmt, |lv, usage| { - // 1. Check if Copy (if it is, don't delete any referenced values) - delete = (usage == Move && !lv_is_copy(lv)); - // 2. Search for replacements + top_level = true; + // 1. Search for replacements visit_lvalues(lv, |ilv, usage2| { if Local(i) == ilv { - ilv = replacement; - if delete { - remove_replacement; - } + if replacement = find_replacement(i) { + if is_copy(ilv) { + ilv = replacement + } + else if top_level { + ilv = replacement + remove_replacement(i) + } + else { + remove_replacement(i) + } + } } }); // Early-return (don't recuse) @@ -95,6 +101,13 @@ Locate `RETURN = Local(N)` and replace use of Local(N) as assignment destination Note: Only replace assignments that are setting the value that will become the return value. +Algorithm +--------- +TODO: + +Pseudocode +---------- +TODO: Dead Assignment Elimination @@ -104,14 +117,56 @@ Purpose: Remove dead code from previous passes Remove assignments where the assigned value isn't read/borrowed before next assign +Algorithm +--------- + +Pseudocode +--------- + +Dead Drop Flag Elimination +========================== + +Purpose: Remove unused (or unchanged) drop flags + +Algorithm +--------- +- Enumerate all changes to drop flags +- If a flag is never set, replace all uses with its default and delete +- If a flag is never used, delete + + Constant Propagation ==================== Purpose: Allow deletion of useless code (`if false`) and expansion of `size_of` (and relevant optimisations) +Algorithm +--------- +- Track assignments of slots with constants +- Erase record if the slot is invalidated +- Replace usage of the slot where possible with the constant +- Evaluate BinOps with both arguments a known slot + Value Forward Propagation ========================= Purpose: Determine when a value has the same value in all paths to a BB and move the assignment into that BB +Inlining +======== + +Purpose: Inline simple methods + +Algorithm +--------- +- For each function call: +- Check if the called function is simple enough to be inlined + - A single BB - inline + - Three bbs (first being a function call) - inline + - First bb ends with a switch of a constant parameter, and all arms point to return or a return bb + +CFG Simplification +================== +Purpose: Remove gotos to blocks that only have a single use + |