Skip to content

Commit 76c5158

Browse files
committed
[DAG] combineShiftToAVG - don't create avgfloor with scalar constant operands unless legal.
Converting to avgfloor and then expanding it back to shift+add later is likely to prevent other folds (re-association and value-tracking in particular) in the meantime. Fixes #95284
1 parent 71e4d70 commit 76c5158

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,12 @@ static SDValue combineShiftToAVG(SDValue Op,
10781078
return SDValue();
10791079
}
10801080

1081+
// Don't create a AVGFLOOR node with a scalar constant unless its legal as
1082+
// this is likely to stop other folds (reassociation, value tracking etc.)
1083+
if (!IsCeil && !TLI.isOperationLegal(AVGOpc, NVT) &&
1084+
(isa<ConstantSDNode>(ExtOpA) || isa<ConstantSDNode>(ExtOpB)))
1085+
return SDValue();
1086+
10811087
SDLoc DL(Op);
10821088
SDValue ResultAVG =
10831089
DAG.getNode(AVGOpc, DL, NVT, DAG.getExtOrTrunc(IsSigned, ExtOpA, DL, NVT),

llvm/test/CodeGen/RISCV/pr95284.ll

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
define signext i64 @PR95284(i32 signext %0) {
77
; RV32I-LABEL: PR95284:
88
; RV32I: # %bb.0: # %entry
9-
; RV32I-NEXT: addi a1, a0, -1
10-
; RV32I-NEXT: srli a1, a1, 1
11-
; RV32I-NEXT: seqz a0, a0
12-
; RV32I-NEXT: slli a2, a0, 31
13-
; RV32I-NEXT: or a1, a2, a1
14-
; RV32I-NEXT: addi a1, a1, 1
15-
; RV32I-NEXT: seqz a2, a1
16-
; RV32I-NEXT: sub a2, a2, a0
17-
; RV32I-NEXT: andi a0, a1, -2
18-
; RV32I-NEXT: slli a1, a2, 1
9+
; RV32I-NEXT: seqz a1, a0
10+
; RV32I-NEXT: neg a2, a1
11+
; RV32I-NEXT: addi a0, a0, -1
12+
; RV32I-NEXT: srli a2, a2, 1
13+
; RV32I-NEXT: srli a0, a0, 1
14+
; RV32I-NEXT: slli a1, a1, 31
15+
; RV32I-NEXT: or a0, a1, a0
16+
; RV32I-NEXT: addi a0, a0, 1
17+
; RV32I-NEXT: seqz a1, a0
18+
; RV32I-NEXT: add a1, a2, a1
19+
; RV32I-NEXT: slli a1, a1, 1
1920
; RV32I-NEXT: srli a1, a1, 1
21+
; RV32I-NEXT: andi a0, a0, -2
2022
; RV32I-NEXT: ret
2123
;
2224
; RV64I-LABEL: PR95284:
2325
; RV64I: # %bb.0: # %entry
2426
; RV64I-NEXT: addi a0, a0, -1
2527
; RV64I-NEXT: srli a0, a0, 1
2628
; RV64I-NEXT: addi a0, a0, 1
27-
; RV64I-NEXT: li a1, -3
28-
; RV64I-NEXT: srli a1, a1, 1
29-
; RV64I-NEXT: and a0, a0, a1
29+
; RV64I-NEXT: andi a0, a0, -2
3030
; RV64I-NEXT: ret
3131
entry:
3232
%1 = zext nneg i32 %0 to i64

llvm/test/CodeGen/X86/avg.ll

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,12 +2201,11 @@ define <8 x i16> @PR52131_not_zext_with_constant(<8 x i32> %a) {
22012201
define i64 @PR95284(i32 %a0) {
22022202
; CHECK-LABEL: PR95284:
22032203
; CHECK: # %bb.0:
2204-
; CHECK-NEXT: movl %edi, %ecx
2205-
; CHECK-NEXT: decq %rcx
2206-
; CHECK-NEXT: shrq %rcx
2207-
; CHECK-NEXT: incq %rcx
2208-
; CHECK-NEXT: movabsq $9223372036854775806, %rax # imm = 0x7FFFFFFFFFFFFFFE
2209-
; CHECK-NEXT: andq %rcx, %rax
2204+
; CHECK-NEXT: movl %edi, %eax
2205+
; CHECK-NEXT: decq %rax
2206+
; CHECK-NEXT: shrq %rax
2207+
; CHECK-NEXT: incq %rax
2208+
; CHECK-NEXT: andq $-2, %rax
22102209
; CHECK-NEXT: retq
22112210
%ext = zext nneg i32 %a0 to i64
22122211
%dec = add i64 %ext, -1

0 commit comments

Comments
 (0)