[Reassociation] Place moved instructions after landing pads
Reassociation's NegateValue moved instructions to the beginning of
blocks (after PHIs) without checking for exception handling pads.
It's possible for reassociation to move something into an exception
handling block so we need to make sure we don't move things too early
in the block. This change advances the insertion point past any
exception handling pads.
If the block we want to move into contains a catchswitch, we cannot
move into it. In that case just create a new neg as if we had not
found an existing neg to move.
Differential Revision: https://reviews.llvm.org/D61089
llvm-svn: 360262
diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index 7cdfce8..5cee224 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -861,6 +861,8 @@
if (TheNeg->getParent()->getParent() != BI->getParent()->getParent())
continue;
+ bool FoundCatchSwitch = false;
+
BasicBlock::iterator InsertPt;
if (Instruction *InstInput = dyn_cast<Instruction>(V)) {
if (InvokeInst *II = dyn_cast<InvokeInst>(InstInput)) {
@@ -868,10 +870,30 @@
} else {
InsertPt = ++InstInput->getIterator();
}
- while (isa<PHINode>(InsertPt)) ++InsertPt;
+
+ const BasicBlock *BB = InsertPt->getParent();
+
+ // Make sure we don't move anything before PHIs or exception
+ // handling pads.
+ while (InsertPt != BB->end() && (isa<PHINode>(InsertPt) ||
+ InsertPt->isEHPad())) {
+ if (isa<CatchSwitchInst>(InsertPt))
+ // A catchswitch cannot have anything in the block except
+ // itself and PHIs. We'll bail out below.
+ FoundCatchSwitch = true;
+ ++InsertPt;
+ }
} else {
InsertPt = TheNeg->getParent()->getParent()->getEntryBlock().begin();
}
+
+ // We found a catchswitch in the block where we want to move the
+ // neg. We cannot move anything into that block. Bail and just
+ // create the neg before BI, as if we hadn't found an existing
+ // neg.
+ if (FoundCatchSwitch)
+ break;
+
TheNeg->moveBefore(&*InsertPt);
if (TheNeg->getOpcode() == Instruction::Sub) {
TheNeg->setHasNoUnsignedWrap(false);