[PATCH] D27930: Fix dominator tree update in LoopVersioning

Serge Pavlov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 19 10:18:20 PST 2016


sepavloff created this revision.
sepavloff added reviewers: anemet, silviu.baranga.
sepavloff added subscribers: llvm-commits, davide.

When LoopVersioning clones a loop, the original and cloned loops share the
same exit block. To make dominator tree in sync with CFG the pass sets the
immediate dominator of the exit block to memcheck block. It is however
incorrect if the versioned loop already shares the exit block with another
loop. With this change the dominator is changed only if the original loop
preheader dominates the exit block.


https://reviews.llvm.org/D27930

Files:
  lib/Transforms/Utils/LoopVersioning.cpp
  test/Transforms/LoopVersioning/noalias-version-twice.ll


Index: test/Transforms/LoopVersioning/noalias-version-twice.ll
===================================================================
--- test/Transforms/LoopVersioning/noalias-version-twice.ll
+++ test/Transforms/LoopVersioning/noalias-version-twice.ll
@@ -1,4 +1,4 @@
-; RUN: opt -basicaa -loop-distribute -scoped-noalias -loop-versioning -S < %s | FileCheck %s
+; RUN: opt -basicaa -loop-distribute -scoped-noalias -loop-versioning -verify-dom-info -S < %s | FileCheck %s
 
 ; Test the metadata generated when versioning an already versioned loop.  Here
 ; we invoke loop distribution to perform the first round of versioning.  It
Index: lib/Transforms/Utils/LoopVersioning.cpp
===================================================================
--- lib/Transforms/Utils/LoopVersioning.cpp
+++ lib/Transforms/Utils/LoopVersioning.cpp
@@ -61,6 +61,8 @@
 
   // Add the memcheck in the original preheader (this is empty initially).
   BasicBlock *RuntimeCheckBB = VersionedLoop->getLoopPreheader();
+  bool CheckDominatesExit = DT->dominates(RuntimeCheckBB,
+                                          VersionedLoop->getExitBlock());
   std::tie(FirstCheckInst, MemRuntimeCheck) =
       LAI.addRuntimeChecks(RuntimeCheckBB->getTerminator(), AliasChecks);
 
@@ -112,9 +114,13 @@
                      VersionedLoop->getLoopPreheader(), RuntimeCheck, OrigTerm);
   OrigTerm->eraseFromParent();
 
-  // The loops merge in the original exit block.  This is now dominated by the
-  // memchecking block.
-  DT->changeImmediateDominator(VersionedLoop->getExitBlock(), RuntimeCheckBB);
+  // The loops merge in the original exit block.  This should now be dominated
+  // by the memchecking block, but several loops may share the same exit block,
+  // so it may already be dominated by one of memcheck dominators.  Only if
+  // the exit block is dominated by memcheck block in original loop, update its
+  // idom.
+  if (CheckDominatesExit)
+    DT->changeImmediateDominator(VersionedLoop->getExitBlock(), RuntimeCheckBB);
 
   // Adds the necessary PHI nodes for the versioned loops based on the
   // loop-defined values used outside of the loop.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27930.81973.patch
Type: text/x-patch
Size: 2143 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161219/5d25912f/attachment.bin>


More information about the llvm-commits mailing list