[PATCH] D148708: [Refactor] Extract branch inverting to a separate method
Aleksandr Popov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 19 05:12:44 PDT 2023
aleksandr.popov created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
aleksandr.popov requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
The motivation is to make possible branch inverting code reuse.
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
@@ -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<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.514910.patch
Type: text/x-patch
Size: 3236 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230419/9ac1e0ab/attachment.bin>
More information about the llvm-commits
mailing list