Skip to content

[UndefOrPoison] [CompileTime] Avoid IDom walk unless required. NFC #90092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7283,31 +7283,35 @@ static bool isGuaranteedNotToBeUndefOrPoison(
// BB1:
// CtxI ; V cannot be undef or poison here
auto *Dominator = DNode->getIDom();
while (Dominator) {
auto *TI = Dominator->getBlock()->getTerminator();

Value *Cond = nullptr;
if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
if (BI->isConditional())
Cond = BI->getCondition();
} else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
Cond = SI->getCondition();
}
// This check is purely for compile time reasons: we can skip the IDom walk
// if what we are checking for includes undef and the value is not an integer.
if (!includesUndef(Kind) || V->getType()->isIntegerTy())
while (Dominator) {
auto *TI = Dominator->getBlock()->getTerminator();

Value *Cond = nullptr;
if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
if (BI->isConditional())
Cond = BI->getCondition();
} else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
Cond = SI->getCondition();
}

if (Cond) {
if (Cond == V)
return true;
else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
// For poison, we can analyze further
auto *Opr = cast<Operator>(Cond);
if (any_of(Opr->operands(),
[V](const Use &U) { return V == U && propagatesPoison(U); }))
if (Cond) {
if (Cond == V)
return true;
else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
// For poison, we can analyze further
auto *Opr = cast<Operator>(Cond);
if (any_of(Opr->operands(), [V](const Use &U) {
return V == U && propagatesPoison(U);
}))
return true;
}
}
}

Dominator = Dominator->getIDom();
}
Dominator = Dominator->getIDom();
}

if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
return true;
Expand Down