[llvm] [SimplifyCFG] Simplify nested branches (PR #97067)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 28 10:27:43 PDT 2024


================
@@ -7468,6 +7468,91 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
           if (mergeConditionalStores(PBI, BI, DTU, DL, TTI))
             return requestResimplify();
 
+  {
+    // Fold the following pattern:
+    // bb0:
+    //   br i1 %cond1, label %bb1, label %bb2
+    // bb1:
+    //   br i1 %cond2, label %bb3, label %bb4
+    // bb2:
+    //   br i1 %cond2, label %bb4, label %bb3
+    // bb3:
+    //   ...
+    // bb4:
+    //   ...
+    // into
+    // bb0:
+    //   %cond = xor i1 %cond1, %cond2
+    //   br i1 %cond, label %bb4, label %bb3
+    // bb3:
+    //   ...
+    // bb4:
+    //   ...
+    // NOTE: %cond2 always dominates the terminator of bb0.
+
+    BasicBlock *BB1 = BI->getSuccessor(0);
+    BasicBlock *BB2 = BI->getSuccessor(1);
+    auto IsSimpleSuccessor = [BB](BasicBlock *Succ, BranchInst *&SuccBI) {
+      if (Succ == BB)
+        return false;
+      if (Succ->sizeWithoutDebug() > 1)
----------------
dtcxzyw wrote:

Done.

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


More information about the llvm-commits mailing list