[llvm] [LoopIdiomVectorize] Remove redundant DomTreeUpdates (PR #94681)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 13 15:44:14 PDT 2024


================
@@ -391,6 +392,14 @@ Value *LoopIdiomVectorize::expandFindMismatch(
   BasicBlock *LoopIncBlock = BasicBlock::Create(
       Ctx, "mismatch_loop_inc", EndBlock->getParent(), EndBlock);
 
+  // This is actually one of the only two DTU updates we need. The reason being
+  // that we're splitting `mismatch_end` out of the preheader and put
+  // most of the stuff we create later between the preheader and
+  // `mismatch_end`. Now when DTU removes an edge, it simply recalculates
+  // everything in between. In this case, it will be the prehedaer and
+  // `mismatch_end`, along with the aforementioned content. Therefore we don't
+  // need to insert additional DTU updates for new control flow edges
+  // added in this region.
----------------
mshockwave wrote:

> It sounds to me like optimization sounds like something that should be implemented in the updater itself. Please let me know if I missed something here. (Concrete examples would help.)

I believe DomTreeUpdater already has optimizations to skip redundant updates, which is what originally motivated us to come up with this patch.
Using the first function in `test/Transforms/LoopIdiom/AArch64/byte-compare-index.ll` as an example, here is the trace printed out from `-debug-only=dom-tree-builder`:
```
Inserting edge %entry -> %mismatch_min_it_check
Inserting %entry -> (unreachable) %mismatch_min_it_check
After adding unreachable nodes
Inserted %entry -> (prev unreachable) %mismatch_min_it_check
        Inserting discovered connecting edge %mismatch_loop -> %mismatch_end
        Reachable %mismatch_loop -> %mismatch_end
                NCA == %entry
        Inserting discovered connecting edge %mismatch_loop_inc -> %mismatch_end
        Reachable %mismatch_loop_inc -> %mismatch_end
                NCA == %entry
        Inserting discovered connecting edge %mismatch_vec_loop_found -> %mismatch_end
        Reachable %mismatch_vec_loop_found -> %mismatch_end
                NCA == %entry
        Inserting discovered connecting edge %mismatch_vec_loop_inc -> %mismatch_end
        Reachable %mismatch_vec_loop_inc -> %mismatch_end
                NCA == %entry
Deleting edge %entry -> %mismatch_end
        NCD %entry, ToIDom %entry
IsReachableFromIDom %mismatch_end
        Pred %mismatch_loop_inc
        Support %entry
        %mismatch_end is reachable from support %entry
Deleting reachable %entry -> %mismatch_end
        Rebuilding subtree
The entire tree needs to be rebuilt
DomTree recalculated, skipping future batch updates
```
As shown above, all the updates after the deletion are skipped.

https://github.com/llvm/llvm-project/pull/94681


More information about the llvm-commits mailing list