Implement the "linker_private_weak" linkage type. This will be used for
[oota-llvm.git] / lib / CodeGen / RegisterCoalescer.cpp
1 //===- RegisterCoalescer.cpp - Generic Register Coalescing Interface -------==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
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"
23
24 using namespace llvm;
25
26 // Register the RegisterCoalescer interface, providing a nice name to refer to.
27 static RegisterAnalysisGroup<RegisterCoalescer> Z("Register Coalescer");
28 char RegisterCoalescer::ID = 0;
29
30 // RegisterCoalescer destructor: DO NOT move this to the header file
31 // for RegisterCoalescer or else clients of the RegisterCoalescer
32 // class may not depend on the RegisterCoalescer.o file in the current
33 // .a file, causing alias analysis support to not be included in the
34 // tool correctly!
35 //
36 RegisterCoalescer::~RegisterCoalescer() {}
37
38 unsigned CoalescerPair::compose(unsigned a, unsigned b) const {
39   if (!a) return b;
40   if (!b) return a;
41   return tri_.composeSubRegIndices(a, b);
42 }
43
44 bool CoalescerPair::isMoveInstr(const MachineInstr *MI,
45                                 unsigned &Src, unsigned &Dst,
46                                 unsigned &SrcSub, unsigned &DstSub) const {
47   if (MI->isExtractSubreg()) {
48     Dst = MI->getOperand(0).getReg();
49     DstSub = MI->getOperand(0).getSubReg();
50     Src = MI->getOperand(1).getReg();
51     SrcSub = compose(MI->getOperand(1).getSubReg(), MI->getOperand(2).getImm());
52   } else if (MI->isInsertSubreg() || MI->isSubregToReg()) {
53     Dst = MI->getOperand(0).getReg();
54     DstSub = compose(MI->getOperand(0).getSubReg(), MI->getOperand(3).getImm());
55     Src = MI->getOperand(2).getReg();
56     SrcSub = MI->getOperand(2).getSubReg();
57   } else if (!tii_.isMoveInstr(*MI, Src, Dst, SrcSub, DstSub)) {
58     return false;
59   }
60   return true;
61 }
62
63 bool CoalescerPair::setRegisters(const MachineInstr *MI) {
64   srcReg_ = dstReg_ = subIdx_ = 0;
65   newRC_ = 0;
66   flipped_ = crossClass_ = false;
67
68   unsigned Src, Dst, SrcSub, DstSub;
69   if (!isMoveInstr(MI, Src, Dst, SrcSub, DstSub))
70     return false;
71   partial_ = SrcSub || DstSub;
72
73   // If one register is a physreg, it must be Dst.
74   if (TargetRegisterInfo::isPhysicalRegister(Src)) {
75     if (TargetRegisterInfo::isPhysicalRegister(Dst))
76       return false;
77     std::swap(Src, Dst);
78     std::swap(SrcSub, DstSub);
79     flipped_ = true;
80   }
81   origDstReg_ = Dst;
82
83   const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
84
85   if (TargetRegisterInfo::isPhysicalRegister(Dst)) {
86     // Eliminate DstSub on a physreg.
87     if (DstSub) {
88       Dst = tri_.getSubReg(Dst, DstSub);
89       if (!Dst) return false;
90       DstSub = 0;
91     }
92
93     // Eliminate SrcSub by picking a corresponding Dst superregister.
94     if (SrcSub) {
95       Dst = tri_.getMatchingSuperReg(Dst, SrcSub, MRI.getRegClass(Src));
96       if (!Dst) return false;
97       SrcSub = 0;
98     } else if (!MRI.getRegClass(Src)->contains(Dst)) {
99       return false;
100     }
101   } else {
102     // Both registers are virtual.
103
104     // Both registers have subreg indices.
105     if (SrcSub && DstSub) {
106       // For now we only handle the case of identical indices in commensurate
107       // registers: Dreg:ssub_1 + Dreg:ssub_1 -> Dreg
108       // FIXME: Handle Qreg:ssub_3 + Dreg:ssub_1 as QReg:dsub_1 + Dreg.
109       if (SrcSub != DstSub)
110         return false;
111       const TargetRegisterClass *SrcRC = MRI.getRegClass(Src);
112       const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
113       if (!getCommonSubClass(DstRC, SrcRC))
114         return false;
115       SrcSub = DstSub = 0;
116     }
117
118     // There can be no SrcSub.
119     if (SrcSub) {
120       std::swap(Src, Dst);
121       DstSub = SrcSub;
122       SrcSub = 0;
123       assert(!flipped_ && "Unexpected flip");
124       flipped_ = true;
125     }
126
127     // Find the new register class.
128     const TargetRegisterClass *SrcRC = MRI.getRegClass(Src);
129     const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
130     if (DstSub)
131       newRC_ = tri_.getMatchingSuperRegClass(DstRC, SrcRC, DstSub);
132     else
133       newRC_ = getCommonSubClass(DstRC, SrcRC);
134     if (!newRC_)
135       return false;
136     crossClass_ = newRC_ != DstRC || newRC_ != SrcRC;
137   }
138   // Check our invariants
139   assert(TargetRegisterInfo::isVirtualRegister(Src) && "Src must be virtual");
140   assert(!(TargetRegisterInfo::isPhysicalRegister(Dst) && DstSub) &&
141          "Cannot have a physical SubIdx");
142   srcReg_ = Src;
143   dstReg_ = Dst;
144   subIdx_ = DstSub;
145   return true;
146 }
147
148 bool CoalescerPair::flip() {
149   if (subIdx_ || TargetRegisterInfo::isPhysicalRegister(dstReg_))
150     return false;
151   std::swap(srcReg_, dstReg_);
152   flipped_ = !flipped_;
153   return true;
154 }
155
156 bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
157   if (!MI)
158     return false;
159   unsigned Src, Dst, SrcSub, DstSub;
160   if (!isMoveInstr(MI, Src, Dst, SrcSub, DstSub))
161     return false;
162
163   // Find the virtual register that is srcReg_.
164   if (Dst == srcReg_) {
165     std::swap(Src, Dst);
166     std::swap(SrcSub, DstSub);
167   } else if (Src != srcReg_) {
168     return false;
169   }
170
171   // Now check that Dst matches dstReg_.
172   if (TargetRegisterInfo::isPhysicalRegister(dstReg_)) {
173     if (!TargetRegisterInfo::isPhysicalRegister(Dst))
174       return false;
175     assert(!subIdx_ && "Inconsistent CoalescerPair state.");
176     // DstSub could be set for a physreg from INSERT_SUBREG.
177     if (DstSub)
178       Dst = tri_.getSubReg(Dst, DstSub);
179     // Full copy of Src.
180     if (!SrcSub)
181       return dstReg_ == Dst;
182     // This is a partial register copy. Check that the parts match.
183     return tri_.getSubReg(dstReg_, SrcSub) == Dst;
184   } else {
185     // dstReg_ is virtual.
186     if (dstReg_ != Dst)
187       return false;
188     // Registers match, do the subregisters line up?
189     return compose(subIdx_, SrcSub) == DstSub;
190   }
191 }
192
193 // Because of the way .a files work, we must force the SimpleRC
194 // implementation to be pulled in if the RegisterCoalescer classes are
195 // pulled in.  Otherwise we run the risk of RegisterCoalescer being
196 // used, but the default implementation not being linked into the tool
197 // that uses it.
198 DEFINING_FILE_FOR(RegisterCoalescer)