Skip to content

[InstCombine] Relax the conditons of fold of ucmp/scmp into phi by allowing the phi node to use the result of ucmp/scmp more than once #109593

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
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
25 changes: 14 additions & 11 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1826,11 +1826,8 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {
// If the only use of phi is comparing it with a constant then we can
// put this comparison in the incoming BB directly after a ucmp/scmp call
// because we know that it will simplify to a single icmp.
// NOTE: the single-use check here is not only to ensure that the
// optimization is profitable, but also to avoid creating a potentially
// invalid phi node when we have a multi-edge in the CFG.
const APInt *Ignored;
if (isa<CmpIntrinsic>(InVal) && InVal->hasOneUse() &&
if (isa<CmpIntrinsic>(InVal) && InVal->hasOneUser() &&
match(&I, m_ICmp(m_Specific(PN), m_APInt(Ignored)))) {
OpsToMoveUseToIncomingBB.push_back(i);
NewPhiValues.push_back(nullptr);
Expand Down Expand Up @@ -1868,18 +1865,24 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {

// Clone the instruction that uses the phi node and move it into the incoming
// BB because we know that the next iteration of InstCombine will simplify it.
SmallDenseMap<BasicBlock *, Instruction *> Clones;
for (auto OpIndex : OpsToMoveUseToIncomingBB) {
Value *Op = PN->getIncomingValue(OpIndex);
BasicBlock *OpBB = PN->getIncomingBlock(OpIndex);

Instruction *Clone = I.clone();
for (Use &U : Clone->operands()) {
if (U == PN)
U = Op;
else
U = U->DoPHITranslation(PN->getParent(), OpBB);
Instruction *Clone = Clones.lookup(OpBB);
if (!Clone) {
Clone = I.clone();
for (Use &U : Clone->operands()) {
if (U == PN)
U = Op;
else
U = U->DoPHITranslation(PN->getParent(), OpBB);
}
Clone = InsertNewInstBefore(Clone, OpBB->getTerminator()->getIterator());
Clones.insert({OpBB, Clone});
}
Clone = InsertNewInstBefore(Clone, OpBB->getTerminator()->getIterator());

NewPhiValues[OpIndex] = Clone;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,35 @@ exit:
%r = icmp slt i8 %phi, 0
ret i1 %r
}

; Same as the first transformation, but the phi node uses the result of scmp twice. This verifies that we don't clone values more than once per block
define i1 @icmp_of_phi_of_scmp_with_constant_one_user_two_uses(i8 %c, i16 %x, i16 %y, i8 %false_val) {
; CHECK-LABEL: define i1 @icmp_of_phi_of_scmp_with_constant_one_user_two_uses(
; CHECK-SAME: i8 [[C:%.*]], i16 [[X:%.*]], i16 [[Y:%.*]], i8 [[FALSE_VAL:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*]]:
; CHECK-NEXT: [[TMP0:%.*]] = icmp slt i16 [[X]], [[Y]]
; CHECK-NEXT: switch i8 [[C]], label %[[BB_2:.*]] [
; CHECK-NEXT: i8 0, label %[[BB:.*]]
; CHECK-NEXT: i8 1, label %[[BB]]
; CHECK-NEXT: ]
; CHECK: [[BB_2]]:
; CHECK-NEXT: br label %[[BB]]
; CHECK: [[BB]]:
; CHECK-NEXT: [[R:%.*]] = phi i1 [ [[TMP0]], %[[ENTRY]] ], [ [[TMP0]], %[[ENTRY]] ], [ false, %[[BB_2]] ]
; CHECK-NEXT: ret i1 [[R]]
;
entry:
%cmp = call i8 @llvm.scmp(i16 %x, i16 %y)
switch i8 %c, label %bb_2 [
i8 0, label %bb
i8 1, label %bb
]

bb_2:
br label %bb

bb:
%phi = phi i8 [ %cmp, %entry ], [ %cmp, %entry ], [ 0, %bb_2 ]
%r = icmp slt i8 %phi, 0
ret i1 %r
}
Loading