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 &Successors, const StringRef Prefix, std::optional 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 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(NewCond)) { + CmpInst *CI = cast(NewCond); + CI->setPredicate(CI->getInversePredicate()); + } else { + NewCond = Builder.CreateNot(NewCond, NewCond->getName() + ".not"); + } + PBI->setCondition(NewCond); + PBI->swapSuccessors(); +} 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(CInst2); - if (Cmp2 && Cmp2->hasOneUse()) - Cmp2->setPredicate(Cmp2->getInversePredicate()); - else - CInst2 = cast(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/SimplifyCFG.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3584,17 +3584,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(NewCond)) { - CmpInst *CI = cast(NewCond); - CI->setPredicate(CI->getInversePredicate()); - } else { - NewCond = - Builder.CreateNot(NewCond, PBI->getCondition()->getName() + ".not"); - } - - PBI->setCondition(NewCond); - PBI->swapSuccessors(); + InvertBranch(PBI, Builder); } BasicBlock *UniqueSucc =