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