1 //===- RegisterCoalescer.cpp - Generic Register Coalescing Interface -------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the generic RegisterCoalescer interface which
11 // is used as the common interface used by all clients and
12 // implementations of register coalescing.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/CodeGen/RegisterCoalescer.h"
17 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Target/TargetInstrInfo.h"
21 #include "llvm/Target/TargetRegisterInfo.h"
22 #include "llvm/Pass.h"
26 // Register the RegisterCoalescer interface, providing a nice name to refer to.
27 INITIALIZE_ANALYSIS_GROUP(RegisterCoalescer
, "Register Coalescer",
28 SimpleRegisterCoalescing
)
29 char RegisterCoalescer::ID
= 0;
31 // RegisterCoalescer destructor: DO NOT move this to the header file
32 // for RegisterCoalescer or else clients of the RegisterCoalescer
33 // class may not depend on the RegisterCoalescer.o file in the current
34 // .a file, causing alias analysis support to not be included in the
37 RegisterCoalescer::~RegisterCoalescer() {}
39 unsigned CoalescerPair::compose(unsigned a
, unsigned b
) const {
42 return tri_
.composeSubRegIndices(a
, b
);
45 bool CoalescerPair::isMoveInstr(const MachineInstr
*MI
,
46 unsigned &Src
, unsigned &Dst
,
47 unsigned &SrcSub
, unsigned &DstSub
) const {
49 Dst
= MI
->getOperand(0).getReg();
50 DstSub
= MI
->getOperand(0).getSubReg();
51 Src
= MI
->getOperand(1).getReg();
52 SrcSub
= MI
->getOperand(1).getSubReg();
53 } else if (MI
->isSubregToReg()) {
54 Dst
= MI
->getOperand(0).getReg();
55 DstSub
= compose(MI
->getOperand(0).getSubReg(), MI
->getOperand(3).getImm());
56 Src
= MI
->getOperand(2).getReg();
57 SrcSub
= MI
->getOperand(2).getSubReg();
63 bool CoalescerPair::setRegisters(const MachineInstr
*MI
) {
64 srcReg_
= dstReg_
= subIdx_
= 0;
66 flipped_
= crossClass_
= false;
68 unsigned Src
, Dst
, SrcSub
, DstSub
;
69 if (!isMoveInstr(MI
, Src
, Dst
, SrcSub
, DstSub
))
71 partial_
= SrcSub
|| DstSub
;
73 // If one register is a physreg, it must be Dst.
74 if (TargetRegisterInfo::isPhysicalRegister(Src
)) {
75 if (TargetRegisterInfo::isPhysicalRegister(Dst
))
78 std::swap(SrcSub
, DstSub
);
82 const MachineRegisterInfo
&MRI
= MI
->getParent()->getParent()->getRegInfo();
84 if (TargetRegisterInfo::isPhysicalRegister(Dst
)) {
85 // Eliminate DstSub on a physreg.
87 Dst
= tri_
.getSubReg(Dst
, DstSub
);
88 if (!Dst
) return false;
92 // Eliminate SrcSub by picking a corresponding Dst superregister.
94 Dst
= tri_
.getMatchingSuperReg(Dst
, SrcSub
, MRI
.getRegClass(Src
));
95 if (!Dst
) return false;
97 } else if (!MRI
.getRegClass(Src
)->contains(Dst
)) {
101 // Both registers are virtual.
103 // Both registers have subreg indices.
104 if (SrcSub
&& DstSub
) {
105 // For now we only handle the case of identical indices in commensurate
106 // registers: Dreg:ssub_1 + Dreg:ssub_1 -> Dreg
107 // FIXME: Handle Qreg:ssub_3 + Dreg:ssub_1 as QReg:dsub_1 + Dreg.
108 if (SrcSub
!= DstSub
)
110 const TargetRegisterClass
*SrcRC
= MRI
.getRegClass(Src
);
111 const TargetRegisterClass
*DstRC
= MRI
.getRegClass(Dst
);
112 if (!getCommonSubClass(DstRC
, SrcRC
))
117 // There can be no SrcSub.
122 assert(!flipped_
&& "Unexpected flip");
126 // Find the new register class.
127 const TargetRegisterClass
*SrcRC
= MRI
.getRegClass(Src
);
128 const TargetRegisterClass
*DstRC
= MRI
.getRegClass(Dst
);
130 newRC_
= tri_
.getMatchingSuperRegClass(DstRC
, SrcRC
, DstSub
);
132 newRC_
= getCommonSubClass(DstRC
, SrcRC
);
135 crossClass_
= newRC_
!= DstRC
|| newRC_
!= SrcRC
;
137 // Check our invariants
138 assert(TargetRegisterInfo::isVirtualRegister(Src
) && "Src must be virtual");
139 assert(!(TargetRegisterInfo::isPhysicalRegister(Dst
) && DstSub
) &&
140 "Cannot have a physical SubIdx");
147 bool CoalescerPair::flip() {
148 if (subIdx_
|| TargetRegisterInfo::isPhysicalRegister(dstReg_
))
150 std::swap(srcReg_
, dstReg_
);
151 flipped_
= !flipped_
;
155 bool CoalescerPair::isCoalescable(const MachineInstr
*MI
) const {
158 unsigned Src
, Dst
, SrcSub
, DstSub
;
159 if (!isMoveInstr(MI
, Src
, Dst
, SrcSub
, DstSub
))
162 // Find the virtual register that is srcReg_.
163 if (Dst
== srcReg_
) {
165 std::swap(SrcSub
, DstSub
);
166 } else if (Src
!= srcReg_
) {
170 // Now check that Dst matches dstReg_.
171 if (TargetRegisterInfo::isPhysicalRegister(dstReg_
)) {
172 if (!TargetRegisterInfo::isPhysicalRegister(Dst
))
174 assert(!subIdx_
&& "Inconsistent CoalescerPair state.");
175 // DstSub could be set for a physreg from INSERT_SUBREG.
177 Dst
= tri_
.getSubReg(Dst
, DstSub
);
180 return dstReg_
== Dst
;
181 // This is a partial register copy. Check that the parts match.
182 return tri_
.getSubReg(dstReg_
, SrcSub
) == Dst
;
184 // dstReg_ is virtual.
187 // Registers match, do the subregisters line up?
188 return compose(subIdx_
, SrcSub
) == DstSub
;
192 // Because of the way .a files work, we must force the SimpleRC
193 // implementation to be pulled in if the RegisterCoalescer classes are
194 // pulled in. Otherwise we run the risk of RegisterCoalescer being
195 // used, but the default implementation not being linked into the tool
197 DEFINING_FILE_FOR(RegisterCoalescer
)