- Removing the infamous r2rMap_ and rep() method. Now the coalescer will update
[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 LoopDepth;
38     bool isBackEdge;
39     CopyRec(MachineInstr *mi, unsigned depth, bool be)
40       : MI(mi), LoopDepth(depth), isBackEdge(be) {};
41   };
42
43   template<class SF> class JoinPriorityQueue;
44
45   /// CopyRecSort - Sorting function for coalescer queue.
46   ///
47   struct CopyRecSort : public std::binary_function<CopyRec,CopyRec,bool> {
48     JoinPriorityQueue<CopyRecSort> *JPQ;
49     explicit CopyRecSort(JoinPriorityQueue<CopyRecSort> *jpq) : JPQ(jpq) {}
50     CopyRecSort(const CopyRecSort &RHS) : JPQ(RHS.JPQ) {}
51     bool operator()(CopyRec left, CopyRec right) const;
52   };
53
54   /// JoinQueue - A priority queue of copy instructions the coalescer is
55   /// going to process.
56   template<class SF>
57   class JoinPriorityQueue {
58     SimpleRegisterCoalescing *Rc;
59     std::priority_queue<CopyRec, std::vector<CopyRec>, SF> Queue;
60
61   public:
62     explicit JoinPriorityQueue(SimpleRegisterCoalescing *rc)
63       : Rc(rc), Queue(SF(this)) {}
64
65     bool empty() const { return Queue.empty(); }
66     void push(CopyRec R) { Queue.push(R); }
67     CopyRec pop() {
68       if (empty()) return CopyRec(0, 0, false);
69       CopyRec R = Queue.top();
70       Queue.pop();
71       return R;
72     }
73
74     // Callbacks to SimpleRegisterCoalescing.
75     unsigned getRepIntervalSize(unsigned Reg);
76   };
77
78   class SimpleRegisterCoalescing : public MachineFunctionPass,
79                                    public RegisterCoalescer {
80     MachineFunction* mf_;
81     MachineRegisterInfo* mri_;
82     const TargetMachine* tm_;
83     const TargetRegisterInfo* tri_;
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     /// JoinQueue - A priority queue of copy instructions the coalescer is
93     /// going to process.
94     JoinPriorityQueue<CopyRecSort> *JoinQueue;
95
96     /// JoinedCopies - Keep track of copies eliminated due to coalescing.
97     ///
98     SmallPtrSet<MachineInstr*, 32> JoinedCopies;
99
100   public:
101     static char ID; // Pass identifcation, replacement for typeid
102     SimpleRegisterCoalescing() : MachineFunctionPass((intptr_t)&ID) {}
103
104     struct InstrSlots {
105       enum {
106         LOAD  = 0,
107         USE   = 1,
108         DEF   = 2,
109         STORE = 3,
110         NUM   = 4
111       };
112     };
113     
114     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
115     virtual void releaseMemory();
116
117     /// runOnMachineFunction - pass entry point
118     virtual bool runOnMachineFunction(MachineFunction&);
119
120     bool coalesceFunction(MachineFunction &mf, RegallocQuery &) {
121       // This runs as an independent pass, so don't do anything.
122       return false;
123     };
124
125     /// getRepIntervalSize - Called from join priority queue sorting function.
126     /// It returns the size of the interval that represent the given register.
127     unsigned getRepIntervalSize(unsigned Reg) {
128       if (!li_->hasInterval(Reg))
129         return 0;
130       return li_->getInterval(Reg).getSize();
131     }
132
133     /// print - Implement the dump method.
134     virtual void print(std::ostream &O, const Module* = 0) const;
135     void print(std::ostream *O, const Module* M = 0) const {
136       if (O) print(*O, M);
137     }
138
139   private:
140     /// joinIntervals - join compatible live intervals
141     void joinIntervals();
142
143     /// CopyCoalesceInMBB - Coalesce copies in the specified MBB, putting
144     /// copies that cannot yet be coalesced into the "TryAgain" list.
145     void CopyCoalesceInMBB(MachineBasicBlock *MBB,
146                            std::vector<CopyRec> &TryAgain);
147
148     /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
149     /// which are the src/dst of the copy instruction CopyMI.  This returns true
150     /// if the copy was successfully coalesced away. If it is not currently
151     /// possible to coalesce this interval, but it may be possible if other
152     /// things get coalesced, then it returns true by reference in 'Again'.
153     bool JoinCopy(CopyRec &TheCopy, bool &Again);
154     
155     /// JoinIntervals - Attempt to join these two intervals.  On failure, this
156     /// returns false.  Otherwise, if one of the intervals being joined is a
157     /// physreg, this method always canonicalizes DestInt to be it.  The output
158     /// "SrcInt" will not have been modified, so we can use this information
159     /// below to update aliases.
160     bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS, bool &Swapped);
161     
162     /// SimpleJoin - Attempt to join the specified interval into this one. The
163     /// caller of this method must guarantee that the RHS only contains a single
164     /// value number and that the RHS is not defined by a copy from this
165     /// interval.  This returns false if the intervals are not joinable, or it
166     /// joins them and returns true.
167     bool SimpleJoin(LiveInterval &LHS, LiveInterval &RHS);
168     
169     /// Return true if the two specified registers belong to different
170     /// register classes.  The registers may be either phys or virt regs.
171     bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
172
173
174     /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy. If
175     /// the source value number is defined by a copy from the destination reg
176     /// see if we can merge these two destination reg valno# into a single
177     /// value number, eliminating a copy.
178     bool AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB,
179                               MachineInstr *CopyMI);
180
181     /// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy.
182     /// If the source value number is defined by a commutable instruction and
183     /// its other operand is coalesced to the copy dest register, see if we
184     /// can transform the copy into a noop by commuting the definition.
185     bool RemoveCopyByCommutingDef(LiveInterval &IntA, LiveInterval &IntB,
186                                   MachineInstr *CopyMI);
187
188     /// RemoveUnnecessaryKills - Remove kill markers that are no longer accurate
189     /// due to live range lengthening as the result of coalescing.
190     void RemoveUnnecessaryKills(unsigned Reg, LiveInterval &LI);
191
192     /// isBackEdgeCopy - Returns true if CopyMI is a back edge copy.
193     ///
194     bool isBackEdgeCopy(MachineInstr *CopyMI, unsigned DstReg);
195
196     /// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
197     /// update the subregister number if it is not zero. If DstReg is a
198     /// physical register and the existing subregister number of the def / use
199     /// being updated is not zero, make sure to set it to the correct physical
200     /// subregister.
201     void UpdateRegDefsUses(unsigned SrcReg, unsigned DstReg, unsigned SubIdx);
202
203     /// lastRegisterUse - Returns the last use of the specific register between
204     /// cycles Start and End or NULL if there are no uses.
205     MachineOperand *lastRegisterUse(unsigned Start, unsigned End, unsigned Reg,
206                                     unsigned &LastUseIdx) const;
207
208     /// findDefOperand - Returns the MachineOperand that is a def of the specific
209     /// register. It returns NULL if the def is not found.
210     MachineOperand *findDefOperand(MachineInstr *MI, unsigned Reg) const;
211
212     /// unsetRegisterKills - Unset IsKill property of all uses of specific register
213     /// between cycles Start and End.
214     void unsetRegisterKills(unsigned Start, unsigned End, unsigned Reg);
215
216     void printRegName(unsigned reg) const;
217   };
218
219 } // End llvm namespace
220
221 #endif