[PATCH] D148708: [BasicBlockUtils] Extract branch inverting to a separate method

Aleksandr Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 21 05:03:45 PDT 2023


aleksandr.popov updated this revision to Diff 515685.
aleksandr.popov added a comment.

rebased


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D148708/new/

https://reviews.llvm.org/D148708

Files:
  llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
  llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
  llvm/lib/Transforms/Utils/FlattenCFG.cpp
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp


Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3580,17 +3580,7 @@
 
   // If we need to invert the condition in the pred block to match, do so now.
   if (InvertPredCond) {
-    Value *NewCond = PBI->getCondition();
-    if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
-      CmpInst *CI = cast<CmpInst>(NewCond);
-      CI->setPredicate(CI->getInversePredicate());
-    } else {
-      NewCond =
-          Builder.CreateNot(NewCond, PBI->getCondition()->getName() + ".not");
-    }
-
-    PBI->setCondition(NewCond);
-    PBI->swapSuccessors();
+    InvertBranch(PBI, Builder);
   }
 
   BasicBlock *UniqueSucc =
Index: llvm/lib/Transforms/Utils/FlattenCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/FlattenCFG.cpp
+++ llvm/lib/Transforms/Utils/FlattenCFG.cpp
@@ -487,16 +487,9 @@
   BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
   Builder.SetInsertPoint(PBI);
   if (InvertCond2) {
-    // If this is a "cmp" instruction, only used for branching (and nowhere
-    // else), then we can simply invert the predicate.
-    auto Cmp2 = dyn_cast<CmpInst>(CInst2);
-    if (Cmp2 && Cmp2->hasOneUse())
-      Cmp2->setPredicate(Cmp2->getInversePredicate());
-    else
-      CInst2 = cast<Instruction>(Builder.CreateNot(CInst2));
-    PBI->swapSuccessors();
+    InvertBranch(PBI, Builder);
   }
-  Value *NC = Builder.CreateBinOp(CombineOp, CInst1, CInst2);
+  Value *NC = Builder.CreateBinOp(CombineOp, CInst1, PBI->getCondition());
   PBI->replaceUsesOfWith(PBI->getCondition(), NC);
   Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
 
Index: llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
===================================================================
--- llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -2049,3 +2049,17 @@
 
   return FirstGuardBlock;
 }
+
+void llvm::InvertBranch(BranchInst *PBI, IRBuilderBase &Builder) {
+  Value *NewCond = PBI->getCondition();
+  // If this is a "cmp" instruction, only used for branching (and nowhere
+  // else), then we can simply invert the predicate.
+  if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
+    CmpInst *CI = cast<CmpInst>(NewCond);
+    CI->setPredicate(CI->getInversePredicate());
+  } else
+    NewCond = Builder.CreateNot(NewCond, NewCond->getName() + ".not");
+
+  PBI->setCondition(NewCond);
+  PBI->swapSuccessors();
+}
Index: llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
===================================================================
--- llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
+++ llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
@@ -605,6 +605,10 @@
     const SetVector<BasicBlock *> &Successors, const StringRef Prefix,
     std::optional<unsigned> MaxControlFlowBooleans = std::nullopt);
 
+// Utility function for inverting branch condition and for swapping its
+// successors
+void InvertBranch(BranchInst *PBI, IRBuilderBase &Builder);
+
 } // end namespace llvm
 
 #endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148708.515685.patch
Type: text/x-patch
Size: 3231 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230421/571eff60/attachment.bin>


More information about the llvm-commits mailing list