aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
diff options
context:
space:
mode:
authorRoman Lebedev <lebedev.ri@gmail.com>2022-02-04 18:43:29 +0300
committerRoman Lebedev <lebedev.ri@gmail.com>2022-02-04 20:26:44 +0300
commit0d384e9228231c766ce43b8ed27f75a1c15a0cf6 (patch)
tree47f80e67c28b92bbd4061b5e78e0c287bd0a2b72 /llvm/lib/Transforms/Utils/SimplifyCFG.cpp
parent332d70cd45b5bbb144b969acfaa31c2ab91aef96 (diff)
downloadllvm-0d384e9228231c766ce43b8ed27f75a1c15a0cf6.zip
llvm-0d384e9228231c766ce43b8ed27f75a1c15a0cf6.tar.gz
llvm-0d384e9228231c766ce43b8ed27f75a1c15a0cf6.tar.bz2
[NFC][SimplifyCFG] Extract `IncomingValuesAreCompatible()` out of `SafeToMergeTerminators()`
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/SimplifyCFG.cpp39
1 files changed, 28 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index ce528e4..d72718e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -293,6 +293,23 @@ public:
} // end anonymous namespace
+/// Return true if all the PHI nodes in the basic block \p BB
+/// receive compatible (identical) incoming values when coming from
+/// all of the predecessor blocks that are specified in \p IncomingBlocks.
+static bool IncomingValuesAreCompatible(BasicBlock *BB,
+ ArrayRef<BasicBlock *> IncomingBlocks) {
+ assert(IncomingBlocks.size() == 2 &&
+ "Only for a pair of incoming blocks at the time!");
+
+ // FIXME: it is okay if one of the incoming values is an `undef` value,
+ // iff the other incoming value is guaranteed to be a non-poison value.
+ // FIXME: it is okay if one of the incoming values is a `poison` value.
+ return all_of(BB->phis(), [IncomingBlocks](PHINode &PN) {
+ return PN.getIncomingValueForBlock(IncomingBlocks[0]) ==
+ PN.getIncomingValueForBlock(IncomingBlocks[1]);
+ });
+}
+
/// Return true if it is safe to merge these two
/// terminator instructions together.
static bool
@@ -309,17 +326,17 @@ SafeToMergeTerminators(Instruction *SI1, Instruction *SI2,
SmallPtrSet<BasicBlock *, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
bool Fail = false;
- for (BasicBlock *Succ : successors(SI2BB))
- if (SI1Succs.count(Succ))
- for (BasicBlock::iterator BBI = Succ->begin(); isa<PHINode>(BBI); ++BBI) {
- PHINode *PN = cast<PHINode>(BBI);
- if (PN->getIncomingValueForBlock(SI1BB) !=
- PN->getIncomingValueForBlock(SI2BB)) {
- if (FailBlocks)
- FailBlocks->insert(Succ);
- Fail = true;
- }
- }
+ for (BasicBlock *Succ : successors(SI2BB)) {
+ if (!SI1Succs.count(Succ))
+ continue;
+ if (IncomingValuesAreCompatible(Succ, {SI1BB, SI2BB}))
+ continue;
+ Fail = true;
+ if (FailBlocks)
+ FailBlocks->insert(Succ);
+ else
+ break;
+ }
return !Fail;
}