[llvm] 8502c54 - [FlattenCFG] Replace branch condition with a created one correctly

Max Kazantsev via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 21 02:50:02 PDT 2023


Author: Max Kazantsev
Date: 2023-04-21T16:49:18+07:00
New Revision: 8502c545771ee6c740b0ffc10863dcb4546f6e75

URL: https://github.com/llvm/llvm-project/commit/8502c545771ee6c740b0ffc10863dcb4546f6e75
DIFF: https://github.com/llvm/llvm-project/commit/8502c545771ee6c740b0ffc10863dcb4546f6e75.diff

LOG: [FlattenCFG] Replace branch condition with a created one correctly

Fix case when FlattenCFGOpt combines the conditions but doesn't update
branch with a new combined condition. It happens when one of the
combinable condition is inverted and has more then 1 user

Patch by Aleksandr Popov!

Differential Revision: https://reviews.llvm.org/D148707

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/FlattenCFG.cpp
    llvm/test/Transforms/Util/flattencfg.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/FlattenCFG.cpp b/llvm/lib/Transforms/Utils/FlattenCFG.cpp
index 2fb2ab82e41af..58ececd16a66e 100644
--- a/llvm/lib/Transforms/Utils/FlattenCFG.cpp
+++ b/llvm/lib/Transforms/Utils/FlattenCFG.cpp
@@ -497,7 +497,7 @@ bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
     PBI->swapSuccessors();
   }
   Value *NC = Builder.CreateBinOp(CombineOp, CInst1, CInst2);
-  PBI->replaceUsesOfWith(CInst2, NC);
+  PBI->replaceUsesOfWith(PBI->getCondition(), NC);
   Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
 
   // Handle PHI node to replace its predecessors to FirstEntryBlock.

diff  --git a/llvm/test/Transforms/Util/flattencfg.ll b/llvm/test/Transforms/Util/flattencfg.ll
index 7bd10df2ff681..4a4d4279f360d 100644
--- a/llvm/test/Transforms/Util/flattencfg.ll
+++ b/llvm/test/Transforms/Util/flattencfg.ll
@@ -269,8 +269,9 @@ bb_4:                                             ; preds = %bb_4, %bb_2
 }
 
 ; cmp.y has 2 users, but should be inverted. So that a new one cmp is created instead.
-; FIXME: Branch condition must be replaced with a new created combined condition
+; Branch condition must be replaced with a new created combined condition
 ; Proof of bug: https://alive2.llvm.org/ce/z/L4ps9v
+; Proof of fix: https://alive2.llvm.org/ce/z/QdrG5U
 define i1 @test_cond_multi_use(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i1 @test_cond_multi_use
 ; CHECK-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
@@ -279,7 +280,7 @@ define i1 @test_cond_multi_use(i32 %x, i32 %y, i32 %z) {
 ; CHECK-NEXT:    [[CMP_Y:%.*]] = icmp eq i32 [[Y]], 0
 ; CHECK-NEXT:    [[TMP0:%.*]] = xor i1 [[CMP_Y]], true
 ; CHECK-NEXT:    [[TMP1:%.*]] = or i1 [[CMP_X]], [[TMP0]]
-; CHECK-NEXT:    br i1 [[CMP_Y]], label [[IF_THEN_Y:%.*]], label [[EXIT:%.*]]
+; CHECK-NEXT:    br i1 [[TMP1]], label [[IF_THEN_Y:%.*]], label [[EXIT:%.*]]
 ; CHECK:       if.then.y:
 ; CHECK-NEXT:    store i32 [[Z]], ptr @g, align 4
 ; CHECK-NEXT:    br label [[EXIT]]


        


More information about the llvm-commits mailing list