[llvm-commits] [llvm] r40737 - in /llvm/trunk: lib/Transforms/Scalar/LoopUnswitch.cpp lib/VMCore/Dominators.cpp test/Transforms/LoopUnswitch/2007-08-01-LCSSA.ll

Devang Patel dpatel at apple.com
Thu Aug 2 08:25:58 PDT 2007


Author: dpatel
Date: Thu Aug  2 10:25:57 2007
New Revision: 40737

URL: http://llvm.org/viewvc/llvm-project?rev=40737&view=rev
Log:
Update dominator info for the middle blocks created while spliting
exit edge to preserve LCSSA.

Fix dominance frontier update during loop unswitch. This fixes PR 1589, again


Added:
    llvm/trunk/test/Transforms/LoopUnswitch/2007-08-01-LCSSA.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp
    llvm/trunk/lib/VMCore/Dominators.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp?rev=40737&r1=40736&r2=40737&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp Thu Aug  2 10:25:57 2007
@@ -543,17 +543,8 @@
     std::swap(TrueDest, FalseDest);
 
   // Insert the new branch.
-  BranchInst *BRI = new BranchInst(TrueDest, FalseDest, BranchVal, InsertPt);
+  new BranchInst(TrueDest, FalseDest, BranchVal, InsertPt);
 
-  // Update dominator info.
-  // BranchVal is a new preheader so it dominates true and false destination
-  // loop headers.
-  if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>()) {
-    DT->changeImmediateDominator(TrueDest, BRI->getParent());
-    DT->changeImmediateDominator(FalseDest, BRI->getParent());
-  }
-  // No need to update DominanceFrontier. BRI->getParent() dominated TrueDest
-  // and FalseDest anyway. Now it immediately dominates them.
 }
 
 
@@ -635,12 +626,14 @@
 
   // Split all of the edges from inside the loop to their exit blocks.  Update
   // the appropriate Phi nodes as we do so.
+  SmallVector<BasicBlock *,8> MiddleBlocks;
   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
     BasicBlock *ExitBlock = ExitBlocks[i];
     std::vector<BasicBlock*> Preds(pred_begin(ExitBlock), pred_end(ExitBlock));
 
     for (unsigned j = 0, e = Preds.size(); j != e; ++j) {
       BasicBlock* MiddleBlock = SplitEdge(Preds[j], ExitBlock, this);
+      MiddleBlocks.push_back(MiddleBlock);
       BasicBlock* StartBlock = Preds[j];
       BasicBlock* EndBlock;
       if (MiddleBlock->getSinglePredecessor() == ExitBlock) {
@@ -685,6 +678,9 @@
   // Add exit blocks to the loop blocks.
   LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end());
 
+  DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>();
+  DominatorTree *DT = getAnalysisToUpdate<DominatorTree>();
+
   // Next step, clone all of the basic blocks that make up the loop (including
   // the loop preheader and exit blocks), keeping track of the mapping between
   // the instructions and blocks.
@@ -698,16 +694,21 @@
     LPM->cloneBasicBlockSimpleAnalysis(LoopBlocks[i], New, L);
   }
 
-  // Update dominator info
-  DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>();
-  if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>())
-    for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) {
-      BasicBlock *LBB = LoopBlocks[i];
-      BasicBlock *NBB = NewBlocks[i];
-      CloneDomInfo(NBB, LBB, NewPreheader, OrigPreheader, 
-                   OrigHeader, DT, DF, ValueMap);
+  // OutSiders are basic block that are dominated by original header and
+  // at the same time they are not part of loop.
+  SmallPtrSet<BasicBlock *, 8> OutSiders;
+  if (DT) {
+    DomTreeNode *OrigHeaderNode = DT->getNode(OrigHeader);
+    for(std::vector<DomTreeNode*>::iterator DI = OrigHeaderNode->begin(), 
+          DE = OrigHeaderNode->end();  DI != DE; ++DI) {
+      BasicBlock *B = (*DI)->getBlock();
+
+      DenseMap<const Value*, Value*>::iterator VI = ValueMap.find(B);
+      if (VI == ValueMap.end()) 
+        OutSiders.insert(B);
     }
-  
+  }
+
   // Splice the newly inserted blocks into the function right before the
   // original preheader.
   F->getBasicBlockList().splice(LoopBlocks[0], F->getBasicBlockList(),
@@ -759,7 +760,62 @@
   EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR);
   OldBR->eraseFromParent();
   LPM->deleteSimpleAnalysisValue(OldBR, L);
-  
+
+  // Update dominator info
+  if (DF && DT) {
+
+    // Clone dominator info for all cloned basic block.
+    for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) {
+      BasicBlock *LBB = LoopBlocks[i];
+      BasicBlock *NBB = NewBlocks[i];
+      CloneDomInfo(NBB, LBB, NewPreheader, OrigPreheader, 
+                   OrigHeader, DT, DF, ValueMap);
+
+      // Remove any OutSiders from LBB and NBB's dominance frontier.
+      DominanceFrontier::iterator LBBI = DF->find(LBB);
+      if (LBBI != DF->end()) {
+        DominanceFrontier::DomSetType &LBSet = LBBI->second;
+        for (DominanceFrontier::DomSetType::iterator LI = LBSet.begin(),
+               LE = LBSet.end(); LI != LE; ++LI) {
+          BasicBlock *B = *LI;
+          if (OutSiders.count(B))
+            DF->removeFromFrontier(LBBI, B);
+        }
+      }
+
+      // Remove any OutSiders from LBB and NBB's dominance frontier.
+      DominanceFrontier::iterator NBBI = DF->find(NBB);
+      if (NBBI != DF->end()) {
+        DominanceFrontier::DomSetType NBSet = NBBI->second;
+        for (DominanceFrontier::DomSetType::iterator NI = NBSet.begin(),
+               NE = NBSet.end(); NI != NE; ++NI) {
+          BasicBlock *B = *NI;
+          if (OutSiders.count(B))
+            DF->removeFromFrontier(NBBI, B);
+        }
+      }
+    }
+
+    // MiddleBlocks are dominated by original pre header. SplitEdge updated
+    // MiddleBlocks' dominance frontier appropriately.
+    for (unsigned i = 0, e = MiddleBlocks.size(); i != e; ++i) {
+      BasicBlock *MBB = MiddleBlocks[i];
+      if (!MBB->getSinglePredecessor())
+        DT->changeImmediateDominator(MBB, OrigPreheader);
+    }
+
+    // All Outsiders are now dominated by original pre header.
+    for (SmallPtrSet<BasicBlock *, 8>::iterator OI = OutSiders.begin(),
+           OE = OutSiders.end(); OI != OE; ++OI) {
+      BasicBlock *OB = *OI;
+      DT->changeImmediateDominator(OB, OrigPreheader);
+    }
+
+    // New loop headers are dominated by original preheader
+    DT->changeImmediateDominator(NewBlocks[0], OrigPreheader);
+    DT->changeImmediateDominator(LoopBlocks[0], OrigPreheader);
+  }
+
   LoopProcessWorklist.push_back(NewLoop);
   redoLoop = true;
 

Modified: llvm/trunk/lib/VMCore/Dominators.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Dominators.cpp?rev=40737&r1=40736&r2=40737&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Dominators.cpp (original)
+++ llvm/trunk/lib/VMCore/Dominators.cpp Thu Aug  2 10:25:57 2007
@@ -627,6 +627,15 @@
   if (!DT.dominates(NewBB, NewBBSucc))
     NewBBDominatesNewBBSucc = false;
 
+  // NewBBSucc inherites original NewBB frontier.
+  DominanceFrontier::iterator NewBBI = find(NewBB);
+  if (NewBBI != end()) {
+    DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
+    DominanceFrontier::DomSetType NewBBSuccSet;
+    NewBBSuccSet.insert(NewBBSet.begin(), NewBBSet.end());
+    addBasicBlock(NewBBSucc, NewBBSuccSet);
+  }
+
   // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the
   // DF(PredBlocks[0]) without the stuff that the new block does not dominate
   // a predecessor of.
@@ -648,7 +657,6 @@
           ++SetI;
       }
 
-      DominanceFrontier::iterator NewBBI = find(NewBB);
       if (NewBBI != end()) {
         DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
         NewBBSet.insert(Set.begin(), Set.end());

Added: llvm/trunk/test/Transforms/LoopUnswitch/2007-08-01-LCSSA.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopUnswitch/2007-08-01-LCSSA.ll?rev=40737&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/LoopUnswitch/2007-08-01-LCSSA.ll (added)
+++ llvm/trunk/test/Transforms/LoopUnswitch/2007-08-01-LCSSA.ll Thu Aug  2 10:25:57 2007
@@ -0,0 +1,55 @@
+; RUN: llvm-as < %s | opt -loop-unswitch -instcombine -disable-output
+	%struct.ClassDef = type { %struct.QByteArray, %struct.QByteArray, %"struct.QList<ArgumentDef>", %"struct.QList<ArgumentDef>", i8, i8, %"struct.QList<ArgumentDef>", %"struct.QList<ArgumentDef>", %"struct.QList<ArgumentDef>", %"struct.QList<ArgumentDef>", %"struct.QList<ArgumentDef>", %"struct.QList<ArgumentDef>", %"struct.QMap<QByteArray,QByteArray>", %"struct.QList<ArgumentDef>", %"struct.QMap<QByteArray,QByteArray>", i32, i32 }
+	%struct.FILE = type { i32, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, %struct._IO_marker*, %struct.FILE*, i32, i32, i32, i16, i8, [1 x i8], i8*, i64, i8*, i8*, i8*, i8*, i32, i32, [40 x i8] }
+	%struct.Generator = type { %struct.FILE*, %struct.ClassDef*, %"struct.QList<ArgumentDef>", %struct.QByteArray, %"struct.QList<ArgumentDef>" }
+	%struct.QBasicAtomic = type { i32 }
+	%struct.QByteArray = type { %"struct.QByteArray::Data"* }
+	%"struct.QByteArray::Data" = type { %struct.QBasicAtomic, i32, i32, i8*, [1 x i8] }
+	%"struct.QList<ArgumentDef>" = type { %"struct.QList<ArgumentDef>::._19" }
+	%"struct.QList<ArgumentDef>::._19" = type { %struct.QListData }
+	%struct.QListData = type { %"struct.QListData::Data"* }
+	%"struct.QListData::Data" = type { %struct.QBasicAtomic, i32, i32, i32, i8, [1 x i8*] }
+	%"struct.QMap<QByteArray,QByteArray>" = type { %"struct.QMap<QByteArray,QByteArray>::._56" }
+	%"struct.QMap<QByteArray,QByteArray>::._56" = type { %struct.QMapData* }
+	%struct.QMapData = type { %struct.QMapData*, [12 x %struct.QMapData*], %struct.QBasicAtomic, i32, i32, i32, i8 }
+	%struct._IO_marker = type { %struct._IO_marker*, %struct.FILE*, i32 }
+ at .str9 = external constant [1 x i8]		; <[1 x i8]*> [#uses=1]
+
+declare i32 @strcmp(i8*, i8*)
+
+define i32 @_ZN9Generator6strregEPKc(%struct.Generator* %this, i8* %s) {
+entry:
+	%s_addr.0 = select i1 false, i8* getelementptr ([1 x i8]* @.str9, i32 0, i32 0), i8* %s		; <i8*> [#uses=2]
+	%tmp122 = icmp eq i8* %s_addr.0, null		; <i1> [#uses=1]
+	br label %bb184
+
+bb55:		; preds = %bb184
+	ret i32 0
+
+bb88:		; preds = %bb184
+	br i1 %tmp122, label %bb154, label %bb128
+
+bb128:		; preds = %bb88
+	%tmp138 = call i32 @strcmp( i8* null, i8* %s_addr.0 )		; <i32> [#uses=1]
+	%iftmp.37.0.in4 = icmp eq i32 %tmp138, 0		; <i1> [#uses=1]
+	br i1 %iftmp.37.0.in4, label %bb250, label %bb166
+
+bb154:		; preds = %bb88
+	br i1 false, label %bb250, label %bb166
+
+bb166:		; preds = %bb154, %bb128
+	%tmp175 = add i32 %idx.0, 1		; <i32> [#uses=1]
+	%tmp177 = add i32 %tmp175, 0		; <i32> [#uses=1]
+	%tmp181 = add i32 %tmp177, 0		; <i32> [#uses=1]
+	%tmp183 = add i32 %i33.0, 1		; <i32> [#uses=1]
+	br label %bb184
+
+bb184:		; preds = %bb166, %entry
+	%i33.0 = phi i32 [ 0, %entry ], [ %tmp183, %bb166 ]		; <i32> [#uses=2]
+	%idx.0 = phi i32 [ 0, %entry ], [ %tmp181, %bb166 ]		; <i32> [#uses=2]
+	%tmp49 = icmp slt i32 %i33.0, 0		; <i1> [#uses=1]
+	br i1 %tmp49, label %bb88, label %bb55
+
+bb250:		; preds = %bb154, %bb128
+	ret i32 %idx.0
+}





More information about the llvm-commits mailing list