Skip to content

[ValueTracking] Use isSafeToSpeculativelyExecuteWithVariableReplaced() in more places #109149

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
Sep 19, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion llvm/lib/Analysis/LazyValueInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,8 @@ ValueLatticeElement LazyValueInfoImpl::getValueAtUse(const Use &U) {
// This also disallows looking through phi nodes: If the phi node is part
// of a cycle, we might end up reasoning about values from different cycle
// iterations (PR60629).
if (!CurrI->hasOneUse() || !isSafeToSpeculativelyExecute(CurrI))
if (!CurrI->hasOneUse() ||
!isSafeToSpeculativelyExecuteWithVariableReplaced(CurrI))
break;
CurrU = &*CurrI->use_begin();
}
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ Instruction *InstCombinerImpl::visitExtractElementInst(ExtractElementInst &EI) {
// it may make the operand poison.
BinaryOperator *BO;
if (match(SrcVec, m_BinOp(BO)) && cheapToScalarize(SrcVec, Index) &&
(HasKnownValidIndex || isSafeToSpeculativelyExecute(BO))) {
(HasKnownValidIndex ||
isSafeToSpeculativelyExecuteWithVariableReplaced(BO))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it necessary here? Isn't this transform change BO to handle strictly a subset of what it was previously?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment above this code: We may replace the operand with poison if the extractelement index is out of bounds.

// extelt (binop X, Y), Index --> binop (extelt X, Index), (extelt Y, Index)
Value *X = BO->getOperand(0), *Y = BO->getOperand(1);
Value *E0 = Builder.CreateExtractElement(X, Index);
Expand Down Expand Up @@ -2777,7 +2778,7 @@ Instruction *InstCombinerImpl::simplifyBinOpSplats(ShuffleVectorInst &SVI) {
return nullptr;

auto *BinOp = cast<BinaryOperator>(Op0);
if (!isSafeToSpeculativelyExecute(BinOp))
if (!isSafeToSpeculativelyExecuteWithVariableReplaced(BinOp))
return nullptr;

Value *NewBO = Builder.CreateBinOp(BinOp->getOpcode(), X, Y);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2105,7 +2105,7 @@ Instruction *InstCombinerImpl::foldVectorBinop(BinaryOperator &Inst) {
// It may not be safe to reorder shuffles and things like div, urem, etc.
// because we may trap when executing those ops on unknown vector elements.
// See PR20059.
if (!isSafeToSpeculativelyExecute(&Inst))
if (!isSafeToSpeculativelyExecuteWithVariableReplaced(&Inst))
return nullptr;

auto createBinOpShuffle = [&](Value *X, Value *Y, ArrayRef<int> M) {
Expand Down
Loading