Skip to content

[DFAJumpThreading] Only unfold select coming from directly where it is defined #70966

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
Nov 2, 2023
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
14 changes: 8 additions & 6 deletions llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ static cl::opt<unsigned> MaxPathLength(
cl::desc("Max number of blocks searched to find a threading path"),
cl::Hidden, cl::init(20));

static cl::opt<unsigned> MaxNumPaths(
"dfa-max-num-paths",
cl::desc("Max number of paths enumerated around a switch"),
cl::Hidden, cl::init(200));
static cl::opt<unsigned>
MaxNumPaths("dfa-max-num-paths",
cl::desc("Max number of paths enumerated around a switch"),
cl::Hidden, cl::init(200));

static cl::opt<unsigned>
CostThreshold("dfa-cost-threshold",
Expand Down Expand Up @@ -297,6 +297,7 @@ void unfold(DomTreeUpdater *DTU, SelectInstToUnfold SIToUnfold,
{DominatorTree::Insert, StartBlock, FT}});

// The select is now dead.
assert(SI->use_empty() && "Select must be dead now");
SI->eraseFromParent();
}

Expand Down Expand Up @@ -466,8 +467,9 @@ struct MainSwitch {
if (!SITerm || !SITerm->isUnconditional())
return false;

if (isa<PHINode>(SIUse) &&
SIBB->getSingleSuccessor() != cast<Instruction>(SIUse)->getParent())
// Only fold the select coming from directly where it is defined.
PHINode *PHIUser = dyn_cast<PHINode>(SIUse);
if (PHIUser && PHIUser->getIncomingBlock(*SI->use_begin()) != SIBB)
return false;

// If select will not be sunk during unfolding, and it is in the same basic
Expand Down
25 changes: 25 additions & 0 deletions llvm/test/Transforms/DFAJumpThreading/dfa-unfold-select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,28 @@ for.inc:
for.end:
ret i32 0
}

define void @select_coming_elsewhere(i1 %cond, i16 %a, i16 %b) {
; CHECK-LABEL: @select_coming_elsewhere(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[DIV:%.*]] = select i1 [[COND:%.*]], i16 [[A:%.*]], i16 [[B:%.*]]
; CHECK-NEXT: br label [[FOR_COND:%.*]]
; CHECK: for.cond:
; CHECK-NEXT: [[E_ADDR_0:%.*]] = phi i16 [ 0, [[ENTRY:%.*]] ], [ [[DIV]], [[LOR_END:%.*]] ]
; CHECK-NEXT: switch i16 [[E_ADDR_0]], label [[LOR_END]] [
; CHECK-NEXT: ]
; CHECK: lor.end:
; CHECK-NEXT: br label [[FOR_COND]]
;
entry:
%div = select i1 %cond, i16 %a, i16 %b
br label %for.cond

for.cond: ; preds = %lor.end, %entry
%e.addr.0 = phi i16 [ 0, %entry ], [ %div, %lor.end ]
switch i16 %e.addr.0, label %lor.end [
]

lor.end: ; preds = %for.cond
br label %for.cond
}