Skip to content

[DAG] Fold (mul (sext (add_nsw x, c1)), c2) -> (add (mul (sext x), c2), c1*c2) #69667

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

Closed
wants to merge 1 commit into from
Closed
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: 24 additions & 1 deletion llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4522,6 +4522,27 @@ SDValue DAGCombiner::visitMUL(SDNode *N) {
DAG.getNode(ISD::MUL, SDLoc(N0), VT, N0.getOperand(0), N1),
DAG.getNode(ISD::MUL, SDLoc(N1), VT, N0.getOperand(1), N1));

// fold (mul (sext (add_nsw x, c1)), c2) -> (add (mul (sext x), c2), c1*c2)
if (N0.getOpcode() == ISD::SIGN_EXTEND &&
N0.getOperand(0).getOpcode() == ISD::ADD &&
N0.getOperand(0)->getFlags().hasNoSignedWrap() &&
DAG.isConstantIntBuildVectorOrConstantInt(N1) &&
DAG.isConstantIntBuildVectorOrConstantInt(
N0.getOperand(0).getOperand(1)) &&
isMulAddWithConstProfitable(N, N0.getOperand(0), N1)) {
SDValue Add = N0.getOperand(0);
SDLoc DL(N0);
if (SDValue ExtC = DAG.FoldConstantArithmetic(N0.getOpcode(), DL, VT,
{Add.getOperand(1)})) {
if (SDValue MulC =
DAG.FoldConstantArithmetic(ISD::MUL, DL, VT, {ExtC, N1})) {
SDValue ExtX = DAG.getNode(N0.getOpcode(), DL, VT, Add.getOperand(0));
SDValue MulX = DAG.getNode(ISD::MUL, DL, VT, ExtX, N1);
return DAG.getNode(ISD::ADD, DL, VT, MulX, MulC);
}
}
}

// Fold (mul (vscale * C0), C1) to (vscale * (C0 * C1)).
ConstantSDNode *NC1 = isConstOrConstSplat(N1);
if (N0.getOpcode() == ISD::VSCALE && NC1) {
Expand Down Expand Up @@ -19702,7 +19723,9 @@ bool DAGCombiner::isMulAddWithConstProfitable(SDNode *MulNode, SDValue AddNode,

if (Use->getOpcode() == ISD::MUL) { // We have another multiply use.
SDNode *OtherOp;
SDNode *MulVar = AddNode.getOperand(0).getNode();
SDNode *MulVar = AddNode.getOperand(0).getOpcode() == ISD::TRUNCATE
? AddNode.getOperand(0).getOperand(0).getNode()
: AddNode.getOperand(0).getNode();

// OtherOp is what we're multiplying against the constant.
if (Use->getOperand(0) == ConstNode)
Expand Down
38 changes: 38 additions & 0 deletions llvm/test/CodeGen/RISCV/mul-and.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 3
; RUN: llc -mtriple=riscv64 -verify-machineinstrs -mattr=+m < %s \
; RUN: | FileCheck -check-prefix=RV64 %s
; ModuleID = '/home/wengliqin/test/shadd.c'

define void @test(ptr nocapture noundef %array2, i32 noundef signext %a, i32 noundef signext %b) #0 {
; RV64-LABEL: test:
; RV64: # %bb.0: # %entry
; RV64-NEXT: slli a2, a1, 2
; RV64-NEXT: li a3, 200
; RV64-NEXT: mul a3, a1, a3
; RV64-NEXT: add a0, a0, a3
; RV64-NEXT: add a2, a0, a2
; RV64-NEXT: lw a3, 1016(a2)
; RV64-NEXT: addiw a1, a1, 5
; RV64-NEXT: addi a3, a3, 1
; RV64-NEXT: sw a3, 1016(a2)
; RV64-NEXT: slli a2, a1, 2
; RV64-NEXT: lui a3, 1
; RV64-NEXT: add a2, a2, a3
; RV64-NEXT: add a0, a0, a2
; RV64-NEXT: sw a1, 904(a0)
; RV64-NEXT: ret
entry:
%add = add nsw i32 %a, 5
%idxprom = sext i32 %add to i64
%sub = add nsw i32 %a, 4
%idxprom1 = sext i32 %sub to i64
%arrayidx2 = getelementptr inbounds [50 x i32], ptr %array2, i64 %idxprom, i64 %idxprom1
%0 = load i32, ptr %arrayidx2, align 4
%add3 = add nsw i32 %0, 1
store i32 %add3, ptr %arrayidx2, align 4
%1 = sext i32 %a to i64
%2 = getelementptr [50 x i32], ptr %array2, i64 %1
%arrayidx8 = getelementptr [50 x i32], ptr %2, i64 25, i64 %idxprom
store i32 %add, ptr %arrayidx8, align 4
ret void
}
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/X86/addr-mode-matcher-2.ll
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ define void @foo_sext_nsw(i1 zeroext, i32) nounwind {
; X64-NEXT: .p2align 4, 0x90
; X64-NEXT: .LBB0_2: # =>This Inner Loop Header: Depth=1
; X64-NEXT: cltq
; X64-NEXT: shlq $2, %rax
; X64-NEXT: leaq 20(%rax,%rax,4), %rdi
; X64-NEXT: leaq (%rax,%rax,4), %rax
; X64-NEXT: leaq 20(,%rax,4), %rdi
; X64-NEXT: callq bar@PLT
; X64-NEXT: jmp .LBB0_2
br i1 %0, label %9, label %3
Expand Down