Skip to content

Commit 891b09a

Browse files
author
Krzysztof Parzyszek
committed
[Hexagon] Make RDF copy propagation a bit more aggressive
Update the testcase to actually test for RDF's output.
1 parent 11f3b15 commit 891b09a

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

llvm/lib/Target/Hexagon/RDFCopy.cpp

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,46 @@ bool CopyPropagation::interpretAsCopy(const MachineInstr *MI, EqualityMap &EM) {
6363
void CopyPropagation::recordCopy(NodeAddr<StmtNode*> SA, EqualityMap &EM) {
6464
CopyMap.insert(std::make_pair(SA.Id, EM));
6565
Copies.push_back(SA.Id);
66+
67+
for (auto I : EM) {
68+
auto FS = DefM.find(I.second.Reg);
69+
if (FS == DefM.end() || FS->second.empty())
70+
continue; // Undefined source
71+
RDefMap[I.second][SA.Id] = FS->second.top()->Id;
72+
// Insert DstR into the map.
73+
RDefMap[I.first];
74+
}
75+
}
76+
77+
78+
void CopyPropagation::updateMap(NodeAddr<InstrNode*> IA) {
79+
RegisterSet RRs;
80+
for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
81+
RRs.insert(RA.Addr->getRegRef(DFG));
82+
bool Common = false;
83+
for (auto &R : RDefMap) {
84+
if (!RRs.count(R.first))
85+
continue;
86+
Common = true;
87+
break;
88+
}
89+
if (!Common)
90+
return;
91+
92+
for (auto &R : RDefMap) {
93+
if (!RRs.count(R.first))
94+
continue;
95+
auto F = DefM.find(R.first.Reg);
96+
if (F == DefM.end() || F->second.empty())
97+
continue;
98+
R.second[IA.Id] = F->second.top()->Id;
99+
}
66100
}
67101

68102
bool CopyPropagation::scanBlock(MachineBasicBlock *B) {
69103
bool Changed = false;
70104
NodeAddr<BlockNode*> BA = DFG.findBlock(B);
105+
DFG.markBlock(BA.Id, DefM);
71106

72107
for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
73108
if (DFG.IsCode<NodeAttrs::Stmt>(IA)) {
@@ -76,28 +111,19 @@ bool CopyPropagation::scanBlock(MachineBasicBlock *B) {
76111
if (interpretAsCopy(SA.Addr->getCode(), EM))
77112
recordCopy(SA, EM);
78113
}
114+
115+
updateMap(IA);
116+
DFG.pushAllDefs(IA, DefM);
79117
}
80118

81119
MachineDomTreeNode *N = MDT.getNode(B);
82120
for (auto *I : *N)
83121
Changed |= scanBlock(I->getBlock());
84122

123+
DFG.releaseBlock(BA.Id, DefM);
85124
return Changed;
86125
}
87126

88-
NodeId CopyPropagation::getLocalReachingDef(RegisterRef RefRR,
89-
NodeAddr<InstrNode*> IA) {
90-
NodeAddr<RefNode*> RA = L.getNearestAliasedRef(RefRR, IA);
91-
if (RA.Id != 0) {
92-
if (RA.Addr->getKind() == NodeAttrs::Def)
93-
return RA.Id;
94-
assert(RA.Addr->getKind() == NodeAttrs::Use);
95-
if (NodeId RD = RA.Addr->getReachingDef())
96-
return RD;
97-
}
98-
return 0;
99-
}
100-
101127
bool CopyPropagation::run() {
102128
scanBlock(&DFG.getMF().front());
103129

@@ -111,6 +137,14 @@ bool CopyPropagation::run() {
111137
<< Print<RegisterRef>(J.second, DFG);
112138
dbgs() << " }\n";
113139
}
140+
dbgs() << "\nRDef map:\n";
141+
for (auto R : RDefMap) {
142+
dbgs() << Print<RegisterRef>(R.first, DFG) << " -> {";
143+
for (auto &M : R.second)
144+
dbgs() << ' ' << Print<NodeId>(M.first, DFG) << ':'
145+
<< Print<NodeId>(M.second, DFG);
146+
dbgs() << " }\n";
147+
}
114148
}
115149

116150
bool Changed = false;
@@ -150,7 +184,8 @@ bool CopyPropagation::run() {
150184
if (DR == SR)
151185
continue;
152186

153-
NodeId AtCopy = getLocalReachingDef(SR, SA);
187+
auto &RDefSR = RDefMap[SR];
188+
NodeId RDefSR_SA = RDefSR[SA.Id];
154189

155190
for (NodeId N = DA.Addr->getReachedUse(), NextN; N; N = NextN) {
156191
auto UA = DFG.addr<UseNode*>(N);
@@ -163,8 +198,7 @@ bool CopyPropagation::run() {
163198

164199
NodeAddr<InstrNode*> IA = UA.Addr->getOwner(DFG);
165200
assert(DFG.IsCode<NodeAttrs::Stmt>(IA));
166-
NodeId AtUse = getLocalReachingDef(SR, IA);
167-
if (AtCopy != AtUse)
201+
if (RDefSR[IA.Id] != RDefSR_SA)
168202
continue;
169203

170204
MachineOperand &Op = UA.Addr->getOp();
@@ -180,8 +214,8 @@ bool CopyPropagation::run() {
180214
Op.setReg(NewReg);
181215
Op.setSubReg(0);
182216
DFG.unlinkUse(UA, false);
183-
if (AtCopy != 0) {
184-
UA.Addr->linkToDef(UA.Id, DFG.addr<DefNode*>(AtCopy));
217+
if (RDefSR_SA != 0) {
218+
UA.Addr->linkToDef(UA.Id, DFG.addr<DefNode*>(RDefSR_SA));
185219
} else {
186220
UA.Addr->setReachingDef(0);
187221
UA.Addr->setSibling(0);

llvm/lib/Target/Hexagon/RDFCopy.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class MachineInstr;
2525
namespace rdf {
2626

2727
struct CopyPropagation {
28-
CopyPropagation(DataFlowGraph &dfg) : MDT(dfg.getDT()), DFG(dfg),
29-
L(dfg.getMF().getRegInfo(), dfg) {}
28+
CopyPropagation(DataFlowGraph &dfg) : MDT(dfg.getDT()), DFG(dfg) {}
3029

3130
virtual ~CopyPropagation() = default;
3231

@@ -36,22 +35,23 @@ namespace rdf {
3635
DataFlowGraph &getDFG() { return DFG; }
3736

3837
using EqualityMap = std::map<RegisterRef, RegisterRef>;
39-
4038
virtual bool interpretAsCopy(const MachineInstr *MI, EqualityMap &EM);
4139

4240
private:
4341
const MachineDominatorTree &MDT;
4442
DataFlowGraph &DFG;
45-
Liveness L;
43+
DataFlowGraph::DefStackMap DefM;
4644
bool Trace = false;
4745

46+
// map: register -> (map: stmt -> reaching def)
47+
std::map<RegisterRef,std::map<NodeId,NodeId>> RDefMap;
4848
// map: statement -> (map: dst reg -> src reg)
4949
std::map<NodeId, EqualityMap> CopyMap;
5050
std::vector<NodeId> Copies;
5151

5252
void recordCopy(NodeAddr<StmtNode*> SA, EqualityMap &EM);
53+
void updateMap(NodeAddr<InstrNode*> IA);
5354
bool scanBlock(MachineBasicBlock *B);
54-
NodeId getLocalReachingDef(RegisterRef RefRR, NodeAddr<InstrNode*> IA);
5555
};
5656

5757
} // end namespace rdf

llvm/test/CodeGen/Hexagon/rdf-copy.ll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
; RUN: llc -march=hexagon < %s | FileCheck %s
1+
; RUN: llc -march=hexagon -disable-copyprop < %s | FileCheck %s
2+
; Disable MachineCopyPropagation to expose this opportunity to RDF copy.
3+
24
;
35
; Check that
46
; {
@@ -26,7 +28,7 @@ target triple = "hexagon"
2628
%struct.t = type { [12 x i8], ptr, double }
2729
%struct.r = type opaque
2830

29-
define ptr @foo(ptr %chain) nounwind readonly {
31+
define ptr @foo(ptr %chain) nounwind readonly #0 {
3032
entry:
3133
%tobool = icmp eq ptr %chain, null
3234
br i1 %tobool, label %if.end, label %while.cond.preheader
@@ -48,6 +50,8 @@ if.end: ; preds = %if.end.loopexit, %e
4850
ret ptr %chain.addr.1
4951
}
5052

53+
attributes #0 = { nounwind "target-features"="-packets" }
54+
5155
!0 = !{!"any pointer", !1}
5256
!1 = !{!"omnipotent char", !2}
5357
!2 = !{!"Simple C/C++ TBAA"}

0 commit comments

Comments
 (0)