[PATCH] D19674: [SimplifyCFG] propagate branch metadata when creating select

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 29 17:22:38 PDT 2016


spatel updated this revision to Diff 55690.
spatel added a comment.

Patch updated:
Minimize test case to show that existing metadata on a select isn't changed and the missing weights fixed by this patch are correct.


http://reviews.llvm.org/D19674

Files:
  lib/Transforms/Utils/SimplifyCFG.cpp
  test/Transforms/SimplifyCFG/preserve-branchweights.ll

Index: test/Transforms/SimplifyCFG/preserve-branchweights.ll
===================================================================
--- test/Transforms/SimplifyCFG/preserve-branchweights.ll
+++ test/Transforms/SimplifyCFG/preserve-branchweights.ll
@@ -419,7 +419,7 @@
 ; CHECK-LABEL: @SimplifyCondBranchToCondBranch(
 ; CHECK-NEXT:  block1:
 ; CHECK-NEXT:    [[BRMERGE:%.*]] = or i1 %cmpb, %cmpa
-; CHECK-NEXT:    [[DOTMUX:%.*]] = select i1 %cmpb, i32 0, i32 2
+; CHECK-NEXT:    [[DOTMUX:%.*]] = select i1 %cmpb, i32 0, i32 2, !prof !11
 ; CHECK-NEXT:    [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !12
 ; CHECK-NEXT:    ret i32 [[OUTVAL]]
 ;
Index: lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyCFG.cpp
+++ lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2835,7 +2835,31 @@
   PBI->setSuccessor(0, CommonDest);
   PBI->setSuccessor(1, OtherDest);
 
-  // Update branch weight for PBI.
+  // OtherDest may have phi nodes.  If so, add an entry from PBI's
+  // block that are identical to the entries for BI's block.
+  AddPredecessorToBlock(OtherDest, PBI->getParent(), BB);
+
+  // We know that the CommonDest already had an edge from PBI to
+  // it.  If it has PHIs though, the PHIs may have different
+  // entries for BB and PBI's BB.  If so, insert a select to make
+  // them agree.
+  PHINode *PN;
+  for (BasicBlock::iterator II = CommonDest->begin();
+       (PN = dyn_cast<PHINode>(II)); ++II) {
+    Value *BIV = PN->getIncomingValueForBlock(BB);
+    unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
+    Value *PBIV = PN->getIncomingValue(PBBIdx);
+    if (BIV != PBIV) {
+      // Insert a select in PBI to pick the right value.
+      // Transfer any original profile metadata from the branch to the select.
+      Value *NV = Builder.CreateSelect(PBICond, PBIV, BIV,
+                                       PBIV->getName() + ".mux", PBI);
+      PN->setIncomingValue(PBBIdx, NV);
+    }
+  }
+
+  // Update branch weight for PBI. We do this after the select creation above
+  // because we want to preserve the original branch weights for the select.
   uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
   bool PredHasWeights =
       PBI->extractProfMetadata(PredTrueWeight, PredFalseWeight);
@@ -2860,28 +2884,6 @@
                          .createBranchWeights(NewWeights[0], NewWeights[1]));
   }
 
-  // OtherDest may have phi nodes.  If so, add an entry from PBI's
-  // block that are identical to the entries for BI's block.
-  AddPredecessorToBlock(OtherDest, PBI->getParent(), BB);
-
-  // We know that the CommonDest already had an edge from PBI to
-  // it.  If it has PHIs though, the PHIs may have different
-  // entries for BB and PBI's BB.  If so, insert a select to make
-  // them agree.
-  PHINode *PN;
-  for (BasicBlock::iterator II = CommonDest->begin();
-       (PN = dyn_cast<PHINode>(II)); ++II) {
-    Value *BIV = PN->getIncomingValueForBlock(BB);
-    unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
-    Value *PBIV = PN->getIncomingValue(PBBIdx);
-    if (BIV != PBIV) {
-      // Insert a select in PBI to pick the right value.
-      Value *NV = cast<SelectInst>
-        (Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName() + ".mux"));
-      PN->setIncomingValue(PBBIdx, NV);
-    }
-  }
-
   DEBUG(dbgs() << "INTO: " << *PBI->getParent());
   DEBUG(dbgs() << *PBI->getParent()->getParent());
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19674.55690.patch
Type: text/x-patch
Size: 3508 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160430/22a42990/attachment-0001.bin>


More information about the llvm-commits mailing list