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