[llvm] [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 (PR #109593)
Volodymyr Vasylkun via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 22 14:56:23 PDT 2024
https://github.com/Poseydon42 created https://github.com/llvm/llvm-project/pull/109593
This extends the optimisation implemented in #107769 by relaxing the condtions to make it happen. Now, the value produced by `ucmp`/`scmp` doesn't need to be one-use, but only one-user, meaning it can be present in a single phi node more than once.
>From 7dcf97607ca7d8af6efce1bc4569e2bbbf9c627c Mon Sep 17 00:00:00 2001
From: Poseydon42 <vvmposeydon at gmail.com>
Date: Sun, 22 Sep 2024 22:40:51 +0100
Subject: [PATCH 1/2] Precommit tests
---
...phi-with-multiple-unsimplifiable-values.ll | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/phi-with-multiple-unsimplifiable-values.ll b/llvm/test/Transforms/InstCombine/phi-with-multiple-unsimplifiable-values.ll
index 2b75d5c5475117..853eefe615de51 100644
--- a/llvm/test/Transforms/InstCombine/phi-with-multiple-unsimplifiable-values.ll
+++ b/llvm/test/Transforms/InstCombine/phi-with-multiple-unsimplifiable-values.ll
@@ -133,3 +133,36 @@ 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: [[CMP:%.*]] = call i8 @llvm.scmp.i8.i16(i16 [[X]], i16 [[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: [[PHI:%.*]] = phi i8 [ [[CMP]], %[[ENTRY]] ], [ [[CMP]], %[[ENTRY]] ], [ 0, %[[BB_2]] ]
+; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[PHI]], 0
+; 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
+}
>From 532881891001dd664f947afb1df1cdcb30e0ebe7 Mon Sep 17 00:00:00 2001
From: Poseydon42 <vvmposeydon at gmail.com>
Date: Sun, 22 Sep 2024 22:42:36 +0100
Subject: [PATCH 2/2] Implement the optimisation
---
.../InstCombine/InstructionCombining.cpp | 23 ++++++++++++-------
...phi-with-multiple-unsimplifiable-values.ll | 5 ++--
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index f6a0f5880cd5c7..0627f76f10891f 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1830,7 +1830,7 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {
// 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);
@@ -1868,18 +1868,25 @@ 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);
+ auto CloneIt = Clones.find(OpBB);
+ if (CloneIt == Clones.end()) {
+ Instruction *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());
+ CloneIt = Clones.insert(std::make_pair(OpBB, Clone)).first;
}
- Clone = InsertNewInstBefore(Clone, OpBB->getTerminator()->getIterator());
+
+ Instruction *Clone = CloneIt->second;
NewPhiValues[OpIndex] = Clone;
}
diff --git a/llvm/test/Transforms/InstCombine/phi-with-multiple-unsimplifiable-values.ll b/llvm/test/Transforms/InstCombine/phi-with-multiple-unsimplifiable-values.ll
index 853eefe615de51..cd40aa92ed4fd1 100644
--- a/llvm/test/Transforms/InstCombine/phi-with-multiple-unsimplifiable-values.ll
+++ b/llvm/test/Transforms/InstCombine/phi-with-multiple-unsimplifiable-values.ll
@@ -139,7 +139,7 @@ define i1 @icmp_of_phi_of_scmp_with_constant_one_user_two_uses(i8 %c, i16 %x, i1
; 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: [[CMP:%.*]] = call i8 @llvm.scmp.i8.i16(i16 [[X]], i16 [[Y]])
+; 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]]
@@ -147,8 +147,7 @@ define i1 @icmp_of_phi_of_scmp_with_constant_one_user_two_uses(i8 %c, i16 %x, i1
; CHECK: [[BB_2]]:
; CHECK-NEXT: br label %[[BB]]
; CHECK: [[BB]]:
-; CHECK-NEXT: [[PHI:%.*]] = phi i8 [ [[CMP]], %[[ENTRY]] ], [ [[CMP]], %[[ENTRY]] ], [ 0, %[[BB_2]] ]
-; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[PHI]], 0
+; CHECK-NEXT: [[R:%.*]] = phi i1 [ [[TMP0]], %[[ENTRY]] ], [ [[TMP0]], %[[ENTRY]] ], [ false, %[[BB_2]] ]
; CHECK-NEXT: ret i1 [[R]]
;
entry:
More information about the llvm-commits
mailing list