[InstSimplify] SimplifyPHINode(): check that instruction is in basic block first

As pointed out in post-commit review, this can legally be called
on instructions that are not inserted into basic blocks,
so don't blindly assume that there is basic block.
This commit is contained in:
Roman Lebedev 2020-08-27 22:31:40 +03:00
parent 035833ae42
commit b85f91fdce
No known key found for this signature in database
GPG Key ID: 083C3EBB4A1689E0
1 changed files with 12 additions and 10 deletions

View File

@ -4404,16 +4404,18 @@ Value *llvm::SimplifyExtractElementInst(Value *Vec, Value *Idx,
/// See if we can fold the given phi. If not, returns null.
static Value *SimplifyPHINode(PHINode *PN, const SimplifyQuery &Q) {
// Is there an identical PHI node before this one in this basic block?
for (PHINode &Src : PN->getParent()->phis()) {
// Once we've reached the PHI node we've been asked about, stop looking.
if (&Src == PN)
break;
// If the previous PHI is currently trivially dead, ignore it,
// it might have been already recorded as being dead.
if (Src.use_empty())
continue;
if (PN->isIdenticalToWhenDefined(&Src))
return &Src;
if (BasicBlock *BB = PN->getParent()) {
for (PHINode &Src : BB->phis()) {
// Once we've reached the PHI node we've been asked about, stop looking.
if (&Src == PN)
break;
// If the previous PHI is currently trivially dead, ignore it,
// it might have been already recorded as being dead.
if (Src.use_empty())
continue;
if (PN->isIdenticalToWhenDefined(&Src))
return &Src;
}
}
// If all of the PHI's incoming values are the same then replace the PHI node