[llvm] 072675f - [DAG] foldSelectOfBinops - correctly handle select of binops where ResNo != 0
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 9 03:09:33 PDT 2023
Author: Simon Pilgrim
Date: 2023-10-09T11:08:55+01:00
New Revision: 072675f14e986c1ecf6275a03c44fca145866d05
URL: https://github.com/llvm/llvm-project/commit/072675f14e986c1ecf6275a03c44fca145866d05
DIFF: https://github.com/llvm/llvm-project/commit/072675f14e986c1ecf6275a03c44fca145866d05.diff
LOG: [DAG] foldSelectOfBinops - correctly handle select of binops where ResNo != 0
Correctly handle cases where the select(cond, binop(x, y), binop(z, y)) --> binop(select(cond, x, z), y) fold is selecting ResNo != 0 results (UADDO flags etc.)
Fixes #68539
Added:
llvm/test/CodeGen/X86/pr68539.ll
Modified:
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 7f3c0f0424abab2..ff0f636bbf77c43 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -26897,7 +26897,8 @@ SDValue DAGCombiner::foldSelectOfBinops(SDNode *N) {
SDLoc DL(N);
unsigned BinOpc = N1.getOpcode();
- if (!TLI.isBinOp(BinOpc) || (N2.getOpcode() != BinOpc))
+ if (!TLI.isBinOp(BinOpc) || (N2.getOpcode() != BinOpc) ||
+ (N1.getResNo() != N2.getResNo()))
return SDValue();
// The use checks are intentionally on SDNode because we may be dealing
@@ -26914,26 +26915,29 @@ SDValue DAGCombiner::foldSelectOfBinops(SDNode *N) {
// Fold select(cond, binop(x, y), binop(z, y))
// --> binop(select(cond, x, z), y)
if (N1.getOperand(1) == N2.getOperand(1)) {
- SDValue NewSel =
- DAG.getSelect(DL, VT, N0, N1.getOperand(0), N2.getOperand(0));
+ SDValue N10 = N1.getOperand(0);
+ SDValue N20 = N2.getOperand(0);
+ SDValue NewSel = DAG.getSelect(DL, N10.getValueType(), N0, N10, N20);
SDValue NewBinOp = DAG.getNode(BinOpc, DL, OpVTs, NewSel, N1.getOperand(1));
NewBinOp->setFlags(N1->getFlags());
NewBinOp->intersectFlagsWith(N2->getFlags());
- return NewBinOp;
+ return SDValue(NewBinOp.getNode(), N1.getResNo());
}
// Fold select(cond, binop(x, y), binop(x, z))
// --> binop(x, select(cond, y, z))
- // Second op VT might be
diff erent (e.g. shift amount type)
- if (N1.getOperand(0) == N2.getOperand(0) &&
- VT == N1.getOperand(1).getValueType() &&
- VT == N2.getOperand(1).getValueType()) {
- SDValue NewSel =
- DAG.getSelect(DL, VT, N0, N1.getOperand(1), N2.getOperand(1));
- SDValue NewBinOp = DAG.getNode(BinOpc, DL, OpVTs, N1.getOperand(0), NewSel);
- NewBinOp->setFlags(N1->getFlags());
- NewBinOp->intersectFlagsWith(N2->getFlags());
- return NewBinOp;
+ if (N1.getOperand(0) == N2.getOperand(0)) {
+ SDValue N11 = N1.getOperand(1);
+ SDValue N21 = N2.getOperand(1);
+ // Second op VT might be
diff erent (e.g. shift amount type)
+ if (N11.getValueType() == N21.getValueType()) {
+ SDValue NewSel = DAG.getSelect(DL, N11.getValueType(), N0, N11, N21);
+ SDValue NewBinOp =
+ DAG.getNode(BinOpc, DL, OpVTs, N1.getOperand(0), NewSel);
+ NewBinOp->setFlags(N1->getFlags());
+ NewBinOp->intersectFlagsWith(N2->getFlags());
+ return SDValue(NewBinOp.getNode(), N1.getResNo());
+ }
}
// TODO: Handle isCommutativeBinOp patterns as well?
diff --git a/llvm/test/CodeGen/X86/pr68539.ll b/llvm/test/CodeGen/X86/pr68539.ll
new file mode 100644
index 000000000000000..8c7e7792dc5f4c6
--- /dev/null
+++ b/llvm/test/CodeGen/X86/pr68539.ll
@@ -0,0 +1,24 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 3
+; RUN: llc < %s -mtriple=x86_64-- | FileCheck %s
+
+define i32 @main(i1 %arg) {
+; CHECK-LABEL: main:
+; CHECK: # %bb.0: # %bb
+; CHECK-NEXT: .p2align 4, 0x90
+; CHECK-NEXT: .LBB0_1: # %bb1
+; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: jmp .LBB0_1
+bb:
+ br label %bb1
+
+bb1:
+ %i = phi i64 [ 0, %bb ], [ %i8, %bb1 ]
+ %i2 = add i32 1, 1
+ %i3 = icmp eq i32 %i2, 0
+ %i4 = add i32 0, 1
+ %i5 = icmp eq i32 %i4, 0
+ %i6 = select i1 %arg, i1 %i5, i1 %i3
+ %i7 = and i64 %i, 0
+ %i8 = select i1 %i6, i64 0, i64 %i
+ br label %bb1
+}
More information about the llvm-commits
mailing list