Switch over to MachineLoopInfo.
[oota-llvm.git] / lib / CodeGen / SimpleRegisterCoalescing.h
1 //===-- SimpleRegisterCoalescing.h - Register Coalescing --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a simple register copy coalescing phase.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_SIMPLE_REGISTER_COALESCING_H
15 #define LLVM_CODEGEN_SIMPLE_REGISTER_COALESCING_H
16
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/LiveInterval.h"
19 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
20 #include "llvm/CodeGen/RegisterCoalescer.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/IndexedMap.h"
23 #include <queue>
24
25 namespace llvm {
26   class SimpleRegisterCoalescing;
27   class LiveVariables;
28   class MRegisterInfo;
29   class TargetInstrInfo;
30   class VirtRegMap;
31   class MachineLoopInfo;
32
33   /// CopyRec - Representation for copy instructions in coalescer queue.
34   ///
35   struct CopyRec {
36     MachineInstr *MI;
37     unsigned SrcReg, DstReg;
38     unsigned LoopDepth;
39     bool isBackEdge;
40     CopyRec(MachineInstr *mi, unsigned src, unsigned dst, unsigned depth,
41             bool be)
42       : MI(mi), SrcReg(src), DstReg(dst), LoopDepth(depth), isBackEdge(be) {};
43   };
44
45   template<class SF> class JoinPriorityQueue;
46
47   /// CopyRecSort - Sorting function for coalescer queue.
48   ///
49   struct CopyRecSort : public std::binary_function<CopyRec,CopyRec,bool> {
50     JoinPriorityQueue<CopyRecSort> *JPQ;
51     CopyRecSort(JoinPriorityQueue<CopyRecSort> *jpq) : JPQ(jpq) {}
52     CopyRecSort(const CopyRecSort &RHS) : JPQ(RHS.JPQ) {}
53     bool operator()(CopyRec left, CopyRec right) const;
54   };
55
56   /// JoinQueue - A priority queue of copy instructions the coalescer is
57   /// going to process.
58   template<class SF>
59   class JoinPriorityQueue {
60     SimpleRegisterCoalescing *Rc;
61     std::priority_queue<CopyRec, std::vector<CopyRec>, SF> Queue;
62
63   public:
64     JoinPriorityQueue(SimpleRegisterCoalescing *rc) : Rc(rc), Queue(SF(this)) {}
65
66     bool empty() const { return Queue.empty(); }
67     void push(CopyRec R) { Queue.push(R); }
68     CopyRec pop() {
69       if (empty()) return CopyRec(0, 0, 0, 0, false);
70       CopyRec R = Queue.top();
71       Queue.pop();
72       return R;
73     }
74
75     // Callbacks to SimpleRegisterCoalescing.
76     unsigned getRepIntervalSize(unsigned Reg);
77   };
78
79   class SimpleRegisterCoalescing : public MachineFunctionPass,
80                                    public RegisterCoalescer {
81     MachineFunction* mf_;
82     const TargetMachine* tm_;
83     const MRegisterInfo* mri_;
84     const TargetInstrInfo* tii_;
85     LiveIntervals *li_;
86     LiveVariables *lv_;
87     const MachineLoopInfo* loopInfo;
88     
89     BitVector allocatableRegs_;
90     DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs_;
91
92     /// r2rMap_ - Map from register to its representative register.
93     ///
94     IndexedMap<unsigned> r2rMap_;
95
96     /// r2rRevMap_ - Reverse of r2rRevMap_, i.e. Map from register to all
97     /// the registers it represent.
98     IndexedMap<std::vector<unsigned> > r2rRevMap_;
99
100     /// JoinQueue - A priority queue of copy instructions the coalescer is
101     /// going to process.
102     JoinPriorityQueue<CopyRecSort> *JoinQueue;
103
104     /// JoinedLIs - Keep track which register intervals have been coalesced
105     /// with other intervals.
106     BitVector JoinedLIs;
107
108     /// SubRegIdxes - Keep track of sub-register and indexes.
109     ///
110     SmallVector<std::pair<unsigned, unsigned>, 32> SubRegIdxes;
111
112     /// JoinedCopies - Keep track of copies eliminated due to coalescing.
113     ///
114     SmallPtrSet<MachineInstr*, 32> JoinedCopies;
115
116   public:
117     static char ID; // Pass identifcation, replacement for typeid
118     SimpleRegisterCoalescing() : MachineFunctionPass((intptr_t)&ID) {}
119
120     struct InstrSlots {
121       enum {
122         LOAD  = 0,
123         USE   = 1,
124         DEF   = 2,
125         STORE = 3,
126         NUM   = 4
127       };
128     };
129     
130     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
131     virtual void releaseMemory();
132
133     /// runOnMachineFunction - pass entry point
134     virtual bool runOnMachineFunction(MachineFunction&);
135
136     bool coalesceFunction(MachineFunction &mf, RegallocQuery &) {
137       // This runs as an independent pass, so don't do anything.
138       return false;
139     };
140
141     /// getRepIntervalSize - Called from join priority queue sorting function.
142     /// It returns the size of the interval that represent the given register.
143     unsigned getRepIntervalSize(unsigned Reg) {
144       Reg = rep(Reg);
145       if (!li_->hasInterval(Reg))
146         return 0;
147       return li_->getInterval(Reg).getSize();
148     }
149
150     /// print - Implement the dump method.
151     virtual void print(std::ostream &O, const Module* = 0) const;
152     void print(std::ostream *O, const Module* M = 0) const {
153       if (O) print(*O, M);
154     }
155
156   private:
157     /// joinIntervals - join compatible live intervals
158     void joinIntervals();
159
160     /// CopyCoalesceInMBB - Coalesce copies in the specified MBB, putting
161     /// copies that cannot yet be coalesced into the "TryAgain" list.
162     void CopyCoalesceInMBB(MachineBasicBlock *MBB,
163                            std::vector<CopyRec> &TryAgain);
164
165     /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
166     /// which are the src/dst of the copy instruction CopyMI.  This returns true
167     /// if the copy was successfully coalesced away. If it is not currently
168     /// possible to coalesce this interval, but it may be possible if other
169     /// things get coalesced, then it returns true by reference in 'Again'.
170     bool JoinCopy(CopyRec TheCopy, bool &Again);
171     
172     /// JoinIntervals - Attempt to join these two intervals.  On failure, this
173     /// returns false.  Otherwise, if one of the intervals being joined is a
174     /// physreg, this method always canonicalizes DestInt to be it.  The output
175     /// "SrcInt" will not have been modified, so we can use this information
176     /// below to update aliases.
177     bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS, bool &Swapped);
178     
179     /// SimpleJoin - Attempt to join the specified interval into this one. The
180     /// caller of this method must guarantee that the RHS only contains a single
181     /// value number and that the RHS is not defined by a copy from this
182     /// interval.  This returns false if the intervals are not joinable, or it
183     /// joins them and returns true.
184     bool SimpleJoin(LiveInterval &LHS, LiveInterval &RHS);
185     
186     /// Return true if the two specified registers belong to different
187     /// register classes.  The registers may be either phys or virt regs.
188     bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
189
190
191     bool AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB,
192                               MachineInstr *CopyMI);
193
194     /// AddSubRegIdxPairs - Recursively mark all the registers represented by the
195     /// specified register as sub-registers. The recursion level is expected to be
196     /// shallow.
197     void AddSubRegIdxPairs(unsigned Reg, unsigned SubIdx);
198
199     /// isBackEdgeCopy - Returns true if CopyMI is a back edge copy.
200     ///
201     bool isBackEdgeCopy(MachineInstr *CopyMI, unsigned DstReg);
202
203     /// lastRegisterUse - Returns the last use of the specific register between
204     /// cycles Start and End. It also returns the use operand by reference. It
205     /// returns NULL if there are no uses.
206     MachineInstr *lastRegisterUse(unsigned Start, unsigned End, unsigned Reg,
207                                   MachineOperand *&MOU);
208
209     /// findDefOperand - Returns the MachineOperand that is a def of the specific
210     /// register. It returns NULL if the def is not found.
211     MachineOperand *findDefOperand(MachineInstr *MI, unsigned Reg);
212
213     /// unsetRegisterKill - Unset IsKill property of all uses of the specific
214     /// register of the specific instruction.
215     void unsetRegisterKill(MachineInstr *MI, unsigned Reg);
216
217     /// unsetRegisterKills - Unset IsKill property of all uses of specific register
218     /// between cycles Start and End.
219     void unsetRegisterKills(unsigned Start, unsigned End, unsigned Reg);
220
221     /// hasRegisterDef - True if the instruction defines the specific register.
222     ///
223     bool hasRegisterDef(MachineInstr *MI, unsigned Reg);
224
225     /// rep - returns the representative of this register
226     unsigned rep(unsigned Reg) {
227       unsigned Rep = r2rMap_[Reg];
228       if (Rep)
229         return r2rMap_[Reg] = rep(Rep);
230       return Reg;
231     }
232
233     void printRegName(unsigned reg) const;
234   };
235
236 } // End llvm namespace
237
238 #endif