Move SimpleRegisterCoalescing.h to lib/CodeGen since there is now a common
[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
24 namespace llvm {
25
26   class LiveVariables;
27   class MRegisterInfo;
28   class TargetInstrInfo;
29   class VirtRegMap;
30
31   class SimpleRegisterCoalescing : public MachineFunctionPass,
32                                    public RegisterCoalescer {
33     MachineFunction* mf_;
34     const TargetMachine* tm_;
35     const MRegisterInfo* mri_;
36     const TargetInstrInfo* tii_;
37     LiveIntervals *li_;
38     LiveVariables *lv_;
39     
40     BitVector allocatableRegs_;
41     DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs_;
42
43     /// r2rMap_ - Map from register to its representative register.
44     ///
45     IndexedMap<unsigned> r2rMap_;
46
47     /// r2rRevMap_ - Reverse of r2rRevMap_, i.e. Map from register to all
48     /// the registers it represent.
49     IndexedMap<std::vector<unsigned> > r2rRevMap_;
50
51     /// JoinedLIs - Keep track which register intervals have been coalesced
52     /// with other intervals.
53     BitVector JoinedLIs;
54
55     /// SubRegIdxes - Keep track of sub-register and indexes.
56     ///
57     SmallVector<std::pair<unsigned, unsigned>, 32> SubRegIdxes;
58
59     /// JoinedCopies - Keep track of copies eliminated due to coalescing.
60     ///
61     SmallPtrSet<MachineInstr*, 32> JoinedCopies;
62
63   public:
64     static char ID; // Pass identifcation, replacement for typeid
65     SimpleRegisterCoalescing() : MachineFunctionPass((intptr_t)&ID) {}
66
67     struct CopyRec {
68       MachineInstr *MI;
69       unsigned SrcReg, DstReg;
70     };
71     CopyRec getCopyRec(MachineInstr *MI, unsigned SrcReg, unsigned DstReg) {
72       CopyRec R;
73       R.MI = MI;
74       R.SrcReg = SrcReg;
75       R.DstReg = DstReg;
76       return R;
77     }
78     struct InstrSlots {
79       enum {
80         LOAD  = 0,
81         USE   = 1,
82         DEF   = 2,
83         STORE = 3,
84         NUM   = 4
85       };
86     };
87     
88     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
89     virtual void releaseMemory();
90
91     /// runOnMachineFunction - pass entry point
92     virtual bool runOnMachineFunction(MachineFunction&);
93
94     bool coalesceFunction(MachineFunction &mf, RegallocQuery &) {
95       // This runs as an independent pass, so don't do anything.
96       return(false);
97     };
98
99     /// print - Implement the dump method.
100     virtual void print(std::ostream &O, const Module* = 0) const;
101     void print(std::ostream *O, const Module* M = 0) const {
102       if (O) print(*O, M);
103     }
104
105   private:      
106     /// joinIntervals - join compatible live intervals
107     void joinIntervals();
108
109     /// CopyCoalesceInMBB - Coalesce copies in the specified MBB, putting
110     /// copies that cannot yet be coalesced into the "TryAgain" list.
111     void CopyCoalesceInMBB(MachineBasicBlock *MBB,
112                            std::vector<CopyRec> &TryAgain);
113
114     /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
115     /// which are the src/dst of the copy instruction CopyMI.  This returns true
116     /// if the copy was successfully coalesced away. If it is not currently
117     /// possible to coalesce this interval, but it may be possible if other
118     /// things get coalesced, then it returns true by reference in 'Again'.
119     bool JoinCopy(MachineInstr *CopyMI, unsigned SrcReg, unsigned DstReg,
120                   bool &Again);
121     
122     /// JoinIntervals - Attempt to join these two intervals.  On failure, this
123     /// returns false.  Otherwise, if one of the intervals being joined is a
124     /// physreg, this method always canonicalizes DestInt to be it.  The output
125     /// "SrcInt" will not have been modified, so we can use this information
126     /// below to update aliases.
127     bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS, bool &Swapped);
128     
129     /// SimpleJoin - Attempt to join the specified interval into this one. The
130     /// caller of this method must guarantee that the RHS only contains a single
131     /// value number and that the RHS is not defined by a copy from this
132     /// interval.  This returns false if the intervals are not joinable, or it
133     /// joins them and returns true.
134     bool SimpleJoin(LiveInterval &LHS, LiveInterval &RHS);
135     
136     /// Return true if the two specified registers belong to different
137     /// register classes.  The registers may be either phys or virt regs.
138     bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
139
140
141     bool AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB,
142                               MachineInstr *CopyMI);
143
144     /// AddSubRegIdxPairs - Recursively mark all the registers represented by the
145     /// specified register as sub-registers. The recursion level is expected to be
146     /// shallow.
147     void AddSubRegIdxPairs(unsigned Reg, unsigned SubIdx);
148
149     /// lastRegisterUse - Returns the last use of the specific register between
150     /// cycles Start and End. It also returns the use operand by reference. It
151     /// returns NULL if there are no uses.
152     MachineInstr *lastRegisterUse(unsigned Start, unsigned End, unsigned Reg,
153                                   MachineOperand *&MOU);
154
155     /// findDefOperand - Returns the MachineOperand that is a def of the specific
156     /// register. It returns NULL if the def is not found.
157     MachineOperand *findDefOperand(MachineInstr *MI, unsigned Reg);
158
159     /// unsetRegisterKill - Unset IsKill property of all uses of the specific
160     /// register of the specific instruction.
161     void unsetRegisterKill(MachineInstr *MI, unsigned Reg);
162
163     /// unsetRegisterKills - Unset IsKill property of all uses of specific register
164     /// between cycles Start and End.
165     void unsetRegisterKills(unsigned Start, unsigned End, unsigned Reg);
166
167     /// hasRegisterDef - True if the instruction defines the specific register.
168     ///
169     bool hasRegisterDef(MachineInstr *MI, unsigned Reg);
170
171     /// rep - returns the representative of this register
172     unsigned rep(unsigned Reg) {
173       unsigned Rep = r2rMap_[Reg];
174       if (Rep)
175         return r2rMap_[Reg] = rep(Rep);
176       return Reg;
177     }
178
179     void printRegName(unsigned reg) const;
180   };
181
182 } // End llvm namespace
183
184 #endif