Skip to content

Commit 8f04d81

Browse files
committed
[SelectionDAG][RISCV] Mask constants to narrow size in TargetLowering::expandUnalignedStore.
If the SRL for Hi constant folds, but we don't remoe those bits from the Lo, we can end up with strange constant folding through DAGCombine later. I've only seen this with constants being lowered to constant pools during lowering on RISC-V.
1 parent 17a12a2 commit 8f04d81

File tree

2 files changed

+27
-50
lines changed

2 files changed

+27
-50
lines changed

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9558,6 +9558,14 @@ SDValue TargetLowering::expandUnalignedStore(StoreSDNode *ST,
95589558
SDValue ShiftAmount = DAG.getConstant(
95599559
NumBits, dl, getShiftAmountTy(Val.getValueType(), DAG.getDataLayout()));
95609560
SDValue Lo = Val;
9561+
// If Val is a constant, replace the upper bits with 0. The SRL will constant
9562+
// fold and not use the upper bits. A smaller constant may be easier to
9563+
// materialize.
9564+
if (auto *C = dyn_cast<ConstantSDNode>(Lo); C && !C->isOpaque())
9565+
Lo = DAG.getNode(
9566+
ISD::AND, dl, VT, Lo,
9567+
DAG.getConstant(APInt::getLowBitsSet(VT.getSizeInBits(), NumBits), dl,
9568+
VT));
95619569
SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
95629570

95639571
// Store the two parts

llvm/test/CodeGen/RISCV/unaligned-load-store.ll

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -416,57 +416,26 @@ define void @merge_stores_i32_i64(ptr %p) {
416416
ret void
417417
}
418418

419-
; FIXME: We shouldn't generate multiple constant pools entries with shifted
420-
; values.
421-
;.LCPI0_0:
422-
; .quad 280223976814164 # 0xfedcba987654
423-
;.LCPI0_1:
424-
; .quad 71737338064426034 # 0xfedcba98765432
425-
;.LCPI0_2:
426-
; .quad -81985529216486896 # 0xfedcba9876543210
427419
define void @store_large_constant(ptr %x) {
428-
; RV32I-LABEL: store_large_constant:
429-
; RV32I: # %bb.0:
430-
; RV32I-NEXT: li a1, 254
431-
; RV32I-NEXT: sb a1, 7(a0)
432-
; RV32I-NEXT: li a1, 220
433-
; RV32I-NEXT: sb a1, 6(a0)
434-
; RV32I-NEXT: li a1, 186
435-
; RV32I-NEXT: sb a1, 5(a0)
436-
; RV32I-NEXT: li a1, 152
437-
; RV32I-NEXT: sb a1, 4(a0)
438-
; RV32I-NEXT: li a1, 118
439-
; RV32I-NEXT: sb a1, 3(a0)
440-
; RV32I-NEXT: li a1, 84
441-
; RV32I-NEXT: sb a1, 2(a0)
442-
; RV32I-NEXT: li a1, 50
443-
; RV32I-NEXT: sb a1, 1(a0)
444-
; RV32I-NEXT: li a1, 16
445-
; RV32I-NEXT: sb a1, 0(a0)
446-
; RV32I-NEXT: ret
447-
;
448-
; RV64I-LABEL: store_large_constant:
449-
; RV64I: # %bb.0:
450-
; RV64I-NEXT: li a1, 254
451-
; RV64I-NEXT: sb a1, 7(a0)
452-
; RV64I-NEXT: li a1, 220
453-
; RV64I-NEXT: sb a1, 6(a0)
454-
; RV64I-NEXT: li a1, 186
455-
; RV64I-NEXT: sb a1, 5(a0)
456-
; RV64I-NEXT: li a1, 152
457-
; RV64I-NEXT: sb a1, 4(a0)
458-
; RV64I-NEXT: li a1, 118
459-
; RV64I-NEXT: lui a2, %hi(.LCPI16_0)
460-
; RV64I-NEXT: ld a2, %lo(.LCPI16_0)(a2)
461-
; RV64I-NEXT: lui a3, %hi(.LCPI16_1)
462-
; RV64I-NEXT: ld a3, %lo(.LCPI16_1)(a3)
463-
; RV64I-NEXT: lui a4, %hi(.LCPI16_2)
464-
; RV64I-NEXT: ld a4, %lo(.LCPI16_2)(a4)
465-
; RV64I-NEXT: sb a1, 3(a0)
466-
; RV64I-NEXT: sb a2, 2(a0)
467-
; RV64I-NEXT: sb a3, 1(a0)
468-
; RV64I-NEXT: sb a4, 0(a0)
469-
; RV64I-NEXT: ret
420+
; SLOW-LABEL: store_large_constant:
421+
; SLOW: # %bb.0:
422+
; SLOW-NEXT: li a1, 254
423+
; SLOW-NEXT: sb a1, 7(a0)
424+
; SLOW-NEXT: li a1, 220
425+
; SLOW-NEXT: sb a1, 6(a0)
426+
; SLOW-NEXT: li a1, 186
427+
; SLOW-NEXT: sb a1, 5(a0)
428+
; SLOW-NEXT: li a1, 152
429+
; SLOW-NEXT: sb a1, 4(a0)
430+
; SLOW-NEXT: li a1, 118
431+
; SLOW-NEXT: sb a1, 3(a0)
432+
; SLOW-NEXT: li a1, 84
433+
; SLOW-NEXT: sb a1, 2(a0)
434+
; SLOW-NEXT: li a1, 50
435+
; SLOW-NEXT: sb a1, 1(a0)
436+
; SLOW-NEXT: li a1, 16
437+
; SLOW-NEXT: sb a1, 0(a0)
438+
; SLOW-NEXT: ret
470439
;
471440
; RV32I-FAST-LABEL: store_large_constant:
472441
; RV32I-FAST: # %bb.0:

0 commit comments

Comments
 (0)