1 //===- RDFCopy.cpp --------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // RDF-based copy propagation.
11 //===----------------------------------------------------------------------===//
15 #include "RDFLiveness.h"
16 #include "RDFRegisters.h"
17 #include "llvm/CodeGen/MachineDominators.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/MachineOperand.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/TargetOpcodes.h"
22 #include "llvm/CodeGen/TargetRegisterInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/raw_ostream.h"
36 static cl::opt
<unsigned> CpLimit("rdf-cp-limit", cl::init(0), cl::Hidden
);
37 static unsigned CpCount
= 0;
40 bool CopyPropagation::interpretAsCopy(const MachineInstr
*MI
, EqualityMap
&EM
) {
41 unsigned Opc
= MI
->getOpcode();
43 case TargetOpcode::COPY
: {
44 const MachineOperand
&Dst
= MI
->getOperand(0);
45 const MachineOperand
&Src
= MI
->getOperand(1);
46 RegisterRef DstR
= DFG
.makeRegRef(Dst
.getReg(), Dst
.getSubReg());
47 RegisterRef SrcR
= DFG
.makeRegRef(Src
.getReg(), Src
.getSubReg());
48 assert(Register::isPhysicalRegister(DstR
.Reg
));
49 assert(Register::isPhysicalRegister(SrcR
.Reg
));
50 const TargetRegisterInfo
&TRI
= DFG
.getTRI();
51 if (TRI
.getMinimalPhysRegClass(DstR
.Reg
) !=
52 TRI
.getMinimalPhysRegClass(SrcR
.Reg
))
54 EM
.insert(std::make_pair(DstR
, SrcR
));
57 case TargetOpcode::REG_SEQUENCE
:
58 llvm_unreachable("Unexpected REG_SEQUENCE");
63 void CopyPropagation::recordCopy(NodeAddr
<StmtNode
*> SA
, EqualityMap
&EM
) {
64 CopyMap
.insert(std::make_pair(SA
.Id
, EM
));
65 Copies
.push_back(SA
.Id
);
68 bool CopyPropagation::scanBlock(MachineBasicBlock
*B
) {
70 NodeAddr
<BlockNode
*> BA
= DFG
.findBlock(B
);
72 for (NodeAddr
<InstrNode
*> IA
: BA
.Addr
->members(DFG
)) {
73 if (DFG
.IsCode
<NodeAttrs::Stmt
>(IA
)) {
74 NodeAddr
<StmtNode
*> SA
= IA
;
76 if (interpretAsCopy(SA
.Addr
->getCode(), EM
))
81 MachineDomTreeNode
*N
= MDT
.getNode(B
);
83 Changed
|= scanBlock(I
->getBlock());
88 NodeId
CopyPropagation::getLocalReachingDef(RegisterRef RefRR
,
89 NodeAddr
<InstrNode
*> IA
) {
90 NodeAddr
<RefNode
*> RA
= L
.getNearestAliasedRef(RefRR
, IA
);
92 if (RA
.Addr
->getKind() == NodeAttrs::Def
)
94 assert(RA
.Addr
->getKind() == NodeAttrs::Use
);
95 if (NodeId RD
= RA
.Addr
->getReachingDef())
101 bool CopyPropagation::run() {
102 scanBlock(&DFG
.getMF().front());
105 dbgs() << "Copies:\n";
106 for (NodeId I
: Copies
) {
107 dbgs() << "Instr: " << *DFG
.addr
<StmtNode
*>(I
).Addr
->getCode();
109 for (auto J
: CopyMap
[I
])
110 dbgs() << ' ' << Print
<RegisterRef
>(J
.first
, DFG
) << '='
111 << Print
<RegisterRef
>(J
.second
, DFG
);
116 bool Changed
= false;
118 bool HasLimit
= CpLimit
.getNumOccurrences() > 0;
121 auto MinPhysReg
= [this] (RegisterRef RR
) -> unsigned {
122 const TargetRegisterInfo
&TRI
= DFG
.getTRI();
123 const TargetRegisterClass
&RC
= *TRI
.getMinimalPhysRegClass(RR
.Reg
);
124 if ((RC
.LaneMask
& RR
.Mask
) == RC
.LaneMask
)
126 for (MCSubRegIndexIterator
S(RR
.Reg
, &TRI
); S
.isValid(); ++S
)
127 if (RR
.Mask
== TRI
.getSubRegIndexLaneMask(S
.getSubRegIndex()))
128 return S
.getSubReg();
129 llvm_unreachable("Should have found a register");
133 for (NodeId C
: Copies
) {
135 if (HasLimit
&& CpCount
>= CpLimit
)
138 auto SA
= DFG
.addr
<InstrNode
*>(C
);
139 auto FS
= CopyMap
.find(SA
.Id
);
140 if (FS
== CopyMap
.end())
143 EqualityMap
&EM
= FS
->second
;
144 for (NodeAddr
<DefNode
*> DA
: SA
.Addr
->members_if(DFG
.IsDef
, DFG
)) {
145 RegisterRef DR
= DA
.Addr
->getRegRef(DFG
);
146 auto FR
= EM
.find(DR
);
149 RegisterRef SR
= FR
->second
;
153 NodeId AtCopy
= getLocalReachingDef(SR
, SA
);
155 for (NodeId N
= DA
.Addr
->getReachedUse(), NextN
; N
; N
= NextN
) {
156 auto UA
= DFG
.addr
<UseNode
*>(N
);
157 NextN
= UA
.Addr
->getSibling();
158 uint16_t F
= UA
.Addr
->getFlags();
159 if ((F
& NodeAttrs::PhiRef
) || (F
& NodeAttrs::Fixed
))
161 if (UA
.Addr
->getRegRef(DFG
) != DR
)
164 NodeAddr
<InstrNode
*> IA
= UA
.Addr
->getOwner(DFG
);
165 assert(DFG
.IsCode
<NodeAttrs::Stmt
>(IA
));
166 NodeId AtUse
= getLocalReachingDef(SR
, IA
);
170 MachineOperand
&Op
= UA
.Addr
->getOp();
174 dbgs() << "Can replace " << Print
<RegisterRef
>(DR
, DFG
)
175 << " with " << Print
<RegisterRef
>(SR
, DFG
) << " in "
176 << *NodeAddr
<StmtNode
*>(IA
).Addr
->getCode();
179 unsigned NewReg
= MinPhysReg(SR
);
182 DFG
.unlinkUse(UA
, false);
184 UA
.Addr
->linkToDef(UA
.Id
, DFG
.addr
<DefNode
*>(AtCopy
));
186 UA
.Addr
->setReachingDef(0);
187 UA
.Addr
->setSibling(0);
192 if (HasLimit
&& CpCount
>= CpLimit
)
197 auto FC
= CopyMap
.find(IA
.Id
);
198 if (FC
!= CopyMap
.end()) {
199 // Update the EM map in the copy's entry.
200 auto &M
= FC
->second
;
208 } // for (N in reached-uses)
209 } // for (DA in defs)
210 } // for (C in Copies)