Index: llvm/lib/Transforms/Utils/FlattenCFG.cpp =================================================================== --- llvm/lib/Transforms/Utils/FlattenCFG.cpp +++ llvm/lib/Transforms/Utils/FlattenCFG.cpp @@ -497,7 +497,7 @@ 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. Index: llvm/test/Transforms/Util/flattencfg.ll =================================================================== --- llvm/test/Transforms/Util/flattencfg.ll +++ llvm/test/Transforms/Util/flattencfg.ll @@ -234,7 +234,7 @@ } ; cmp.y has 2 users, but should be inverted. So that a new one cmp is created instead. -; TODO: Branch condition must be replaced with a new created combined condition +; Branch condition must be replaced with a new created combined condition 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:%.*]]) { @@ -243,7 +243,7 @@ ; 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]]