Skip to content

Commit 44037d7

Browse files
committed
[DAGCombine] Fold overlapping constant stores
Fold a smaller constant store into larger constant stores immediately preceeding it. Reviewers: rnk, courbet Subscribers: javed.absar, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D58468 llvm-svn: 354676
1 parent a9e2891 commit 44037d7

File tree

6 files changed

+44
-20
lines changed

6 files changed

+44
-20
lines changed

llvm/include/llvm/CodeGen/SelectionDAGAddressAnalysis.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ class BaseIndexOffset {
6363
// Returns true if `Other` (with size `OtherSize`) can be proven to be fully
6464
// contained in `*this` (with size `Size`).
6565
bool contains(int64_t Size, const BaseIndexOffset &Other, int64_t OtherSize,
66-
const SelectionDAG &DAG) const;
66+
const SelectionDAG &DAG) const {
67+
int64_t Offset;
68+
return contains(Size, Other, OtherSize, DAG, Offset);
69+
}
70+
71+
bool contains(int64_t Size, const BaseIndexOffset &Other, int64_t OtherSize,
72+
const SelectionDAG &DAG, int64_t &Offset) const;
6773

6874
// Returns true `BasePtr0` and `BasePtr1` can be proven to alias/not alias, in
6975
// which case `IsAlias` is set to true/false.

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15437,6 +15437,32 @@ SDValue DAGCombiner::visitSTORE(SDNode *N) {
1543715437
CombineTo(ST1, ST1->getChain());
1543815438
return SDValue();
1543915439
}
15440+
15441+
// If ST stores to a subset of preceeding store's write set, we may be
15442+
// able to fold ST's value into the preceeding stored value. As we know
15443+
// the other uses of ST1's chain are unconcerned with ST, this folding
15444+
// will not affect those nodes.
15445+
int64_t Offset;
15446+
if (ChainBase.contains(ChainByteSize, STBase, STByteSize, DAG,
15447+
Offset)) {
15448+
SDValue ChainValue = ST1->getValue();
15449+
if (auto *C1 = dyn_cast<ConstantSDNode>(ChainValue)) {
15450+
if (auto *C = dyn_cast<ConstantSDNode>(Value)) {
15451+
APInt Val = C1->getAPIntValue();
15452+
APInt InsertVal = C->getAPIntValue().zextOrTrunc(STByteSize * 8);
15453+
if (DAG.getDataLayout().isBigEndian())
15454+
Offset = ChainByteSize - 1 - Offset;
15455+
Val.insertBits(InsertVal, Offset * 8);
15456+
SDValue NewSDVal =
15457+
DAG.getConstant(Val, SDLoc(C), ChainValue.getValueType(),
15458+
C1->isTargetOpcode(), C1->isOpaque());
15459+
SDNode *NewST1 = DAG.UpdateNodeOperands(
15460+
ST1, ST1->getChain(), NewSDVal, ST1->getOperand(2),
15461+
ST1->getOperand(3));
15462+
return CombineTo(ST, SDValue(NewST1, 0));
15463+
}
15464+
}
15465+
} // End ST subset of ST1 case.
1544015466
}
1544115467
}
1544215468
}

llvm/lib/CodeGen/SelectionDAG/SelectionDAGAddressAnalysis.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ bool BaseIndexOffset::computeAliasing(const BaseIndexOffset &BasePtr0,
136136
}
137137

138138
bool BaseIndexOffset::contains(int64_t Size, const BaseIndexOffset &Other,
139-
int64_t OtherSize,
140-
const SelectionDAG &DAG) const {
141-
int64_t Offset;
139+
int64_t OtherSize, const SelectionDAG &DAG,
140+
int64_t &Offset) const {
142141
if (!equalBaseIndex(Other, DAG, Offset))
143142
return false;
144143
if (Offset >= 0) {

llvm/test/CodeGen/AArch64/ldst-paired-aliasing.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ define i32 @main() local_unnamed_addr #1 {
1111
; Make sure the stores happen in the correct order (the exact instructions could change).
1212
; CHECK-LABEL: main:
1313

14-
; CHECK: str xzr, [sp, #80]
15-
; CHECK: str w9, [sp, #80]
14+
; CHECK: orr w9, wzr, #0x1
15+
; CHECK: str x9, [sp, #80]
1616
; CHECK: stp q0, q0, [sp, #48]
1717
; CHECK: ldr w8, [sp, #48]
1818

llvm/test/CodeGen/PowerPC/constant-combines.ll

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
define void @fold_constant_stores_loaddr(i8* %i8_ptr) {
66
; BE-LABEL: fold_constant_stores_loaddr:
77
; BE: # %bb.0: # %entry
8-
; BE-NEXT: li 4, 0
8+
; BE-NEXT: li 4, 85
9+
; BE-NEXT: sldi 4, 4, 57
910
; BE-NEXT: std 4, 0(3)
10-
; BE-NEXT: li 4, -86
11-
; BE-NEXT: stb 4, 0(3)
1211
; BE-NEXT: blr
1312
;
1413
; LE-LABEL: fold_constant_stores_loaddr:
1514
; LE: # %bb.0: # %entry
16-
; LE-NEXT: li 4, 0
17-
; LE-NEXT: li 5, -86
15+
; LE-NEXT: li 4, 170
1816
; LE-NEXT: std 4, 0(3)
19-
; LE-NEXT: stb 5, 0(3)
2017
; LE-NEXT: blr
2118
entry:
2219
%i64_ptr = bitcast i8* %i8_ptr to i64*
@@ -29,18 +26,15 @@ entry:
2926
define void @fold_constant_stores_hiaddr(i8* %i8_ptr) {
3027
; BE-LABEL: fold_constant_stores_hiaddr:
3128
; BE: # %bb.0: # %entry
32-
; BE-NEXT: li 4, 0
29+
; BE-NEXT: li 4, 85
30+
; BE-NEXT: sldi 4, 4, 57
3331
; BE-NEXT: std 4, 0(3)
34-
; BE-NEXT: li 4, -86
35-
; BE-NEXT: stb 4, 0(3)
3632
; BE-NEXT: blr
3733
;
3834
; LE-LABEL: fold_constant_stores_hiaddr:
3935
; LE: # %bb.0: # %entry
40-
; LE-NEXT: li 4, 0
41-
; LE-NEXT: li 5, -86
36+
; LE-NEXT: li 4, 170
4237
; LE-NEXT: std 4, 0(3)
43-
; LE-NEXT: stb 5, 0(3)
4438
; LE-NEXT: blr
4539
entry:
4640
%i64_ptr = bitcast i8* %i8_ptr to i64*

llvm/test/CodeGen/X86/stores-merging.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ define void @redundant_stores_merging() {
2626
define void @redundant_stores_merging_reverse() {
2727
; CHECK-LABEL: redundant_stores_merging_reverse:
2828
; CHECK: # %bb.0:
29-
; CHECK-NEXT: movabsq $528280977409, %rax # imm = 0x7B00000001
29+
; CHECK-NEXT: movabsq $1958505086977, %rax # imm = 0x1C800000001
3030
; CHECK-NEXT: movq %rax, e+{{.*}}(%rip)
31-
; CHECK-NEXT: movl $456, e+{{.*}}(%rip) # imm = 0x1C8
3231
; CHECK-NEXT: retq
3332
store i32 123, i32* getelementptr inbounds (%structTy, %structTy* @e, i64 0, i32 2), align 4
3433
store i32 456, i32* getelementptr inbounds (%structTy, %structTy* @e, i64 0, i32 2), align 4

0 commit comments

Comments
 (0)