Rework the global split cost calculation.
[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/LiveIntervalAnalysis.h"
19 #include "llvm/CodeGen/RegisterCoalescer.h"
20 #include "llvm/ADT/BitVector.h"
21
22 namespace llvm {
23   class SimpleRegisterCoalescing;
24   class LiveDebugVariables;
25   class TargetRegisterInfo;
26   class TargetInstrInfo;
27   class VirtRegMap;
28   class MachineLoopInfo;
29
30   /// CopyRec - Representation for copy instructions in coalescer queue.
31   ///
32   struct CopyRec {
33     MachineInstr *MI;
34     unsigned LoopDepth;
35     CopyRec(MachineInstr *mi, unsigned depth)
36       : MI(mi), LoopDepth(depth) {}
37   };
38
39   class SimpleRegisterCoalescing : public MachineFunctionPass,
40                                    public RegisterCoalescer {
41     MachineFunction* mf_;
42     MachineRegisterInfo* mri_;
43     const TargetMachine* tm_;
44     const TargetRegisterInfo* tri_;
45     const TargetInstrInfo* tii_;
46     LiveIntervals *li_;
47     LiveDebugVariables *ldv_;
48     const MachineLoopInfo* loopInfo;
49     AliasAnalysis *AA;
50     
51     DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs_;
52
53     /// JoinedCopies - Keep track of copies eliminated due to coalescing.
54     ///
55     SmallPtrSet<MachineInstr*, 32> JoinedCopies;
56
57     /// ReMatCopies - Keep track of copies eliminated due to remat.
58     ///
59     SmallPtrSet<MachineInstr*, 32> ReMatCopies;
60
61     /// ReMatDefs - Keep track of definition instructions which have
62     /// been remat'ed.
63     SmallPtrSet<MachineInstr*, 8> ReMatDefs;
64
65   public:
66     static char ID; // Pass identifcation, replacement for typeid
67     SimpleRegisterCoalescing() : MachineFunctionPass(ID) {
68       initializeSimpleRegisterCoalescingPass(*PassRegistry::getPassRegistry());
69     }
70
71     struct InstrSlots {
72       enum {
73         LOAD  = 0,
74         USE   = 1,
75         DEF   = 2,
76         STORE = 3,
77         NUM   = 4
78       };
79     };
80     
81     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
82     virtual void releaseMemory();
83
84     /// runOnMachineFunction - pass entry point
85     virtual bool runOnMachineFunction(MachineFunction&);
86
87     bool coalesceFunction(MachineFunction &mf, RegallocQuery &) {
88       // This runs as an independent pass, so don't do anything.
89       return false;
90     }
91
92     /// print - Implement the dump method.
93     virtual void print(raw_ostream &O, const Module* = 0) const;
94
95   private:
96     /// joinIntervals - join compatible live intervals
97     void joinIntervals();
98
99     /// CopyCoalesceInMBB - Coalesce copies in the specified MBB, putting
100     /// copies that cannot yet be coalesced into the "TryAgain" list.
101     void CopyCoalesceInMBB(MachineBasicBlock *MBB,
102                            std::vector<CopyRec> &TryAgain);
103
104     /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
105     /// which are the src/dst of the copy instruction CopyMI.  This returns true
106     /// if the copy was successfully coalesced away. If it is not currently
107     /// possible to coalesce this interval, but it may be possible if other
108     /// things get coalesced, then it returns true by reference in 'Again'.
109     bool JoinCopy(CopyRec &TheCopy, bool &Again);
110
111     /// JoinIntervals - Attempt to join these two intervals.  On failure, this
112     /// returns false.  The output "SrcInt" will not have been modified, so we can
113     /// use this information below to update aliases.
114     bool JoinIntervals(CoalescerPair &CP);
115
116     /// Return true if the two specified registers belong to different register
117     /// classes.  The registers may be either phys or virt regs.
118     bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
119
120     /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy. If
121     /// the source value number is defined by a copy from the destination reg
122     /// see if we can merge these two destination reg valno# into a single
123     /// value number, eliminating a copy.
124     bool AdjustCopiesBackFrom(const CoalescerPair &CP, MachineInstr *CopyMI);
125
126     /// HasOtherReachingDefs - Return true if there are definitions of IntB
127     /// other than BValNo val# that can reach uses of AValno val# of IntA.
128     bool HasOtherReachingDefs(LiveInterval &IntA, LiveInterval &IntB,
129                               VNInfo *AValNo, VNInfo *BValNo);
130
131     /// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy.
132     /// If the source value number is defined by a commutable instruction and
133     /// its other operand is coalesced to the copy dest register, see if we
134     /// can transform the copy into a noop by commuting the definition.
135     bool RemoveCopyByCommutingDef(const CoalescerPair &CP,MachineInstr *CopyMI);
136
137     /// TrimLiveIntervalToLastUse - If there is a last use in the same basic
138     /// block as the copy instruction, trim the ive interval to the last use
139     /// and return true.
140     bool TrimLiveIntervalToLastUse(SlotIndex CopyIdx,
141                                    MachineBasicBlock *CopyMBB,
142                                    LiveInterval &li, const LiveRange *LR);
143
144     /// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
145     /// computation, replace the copy by rematerialize the definition.
146     /// If PreserveSrcInt is true, make sure SrcInt is valid after the call.
147     bool ReMaterializeTrivialDef(LiveInterval &SrcInt, bool PreserveSrcInt,
148                                  unsigned DstReg, unsigned DstSubIdx,
149                                  MachineInstr *CopyMI);
150
151     /// isWinToJoinCrossClass - Return true if it's profitable to coalesce
152     /// two virtual registers from different register classes.
153     bool isWinToJoinCrossClass(unsigned SrcReg,
154                                unsigned DstReg,
155                                const TargetRegisterClass *SrcRC,
156                                const TargetRegisterClass *DstRC,
157                                const TargetRegisterClass *NewRC);
158
159     /// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
160     /// update the subregister number if it is not zero. If DstReg is a
161     /// physical register and the existing subregister number of the def / use
162     /// being updated is not zero, make sure to set it to the correct physical
163     /// subregister.
164     void UpdateRegDefsUses(const CoalescerPair &CP);
165
166     /// ShortenDeadCopyLiveRange - Shorten a live range defined by a dead copy.
167     /// Return true if live interval is removed.
168     bool ShortenDeadCopyLiveRange(LiveInterval &li, MachineInstr *CopyMI);
169
170     /// ShortenDeadCopyLiveRange - Shorten a live range as it's artificially
171     /// extended by a dead copy. Mark the last use (if any) of the val# as kill
172     /// as ends the live range there. If there isn't another use, then this
173     /// live range is dead. Return true if live interval is removed.
174     bool ShortenDeadCopySrcLiveRange(LiveInterval &li, MachineInstr *CopyMI);
175
176     /// RemoveDeadDef - If a def of a live interval is now determined dead,
177     /// remove the val# it defines. If the live interval becomes empty, remove
178     /// it as well.
179     bool RemoveDeadDef(LiveInterval &li, MachineInstr *DefMI);
180
181     /// RemoveCopyFlag - If DstReg is no longer defined by CopyMI, clear the
182     /// VNInfo copy flag for DstReg and all aliases.
183     void RemoveCopyFlag(unsigned DstReg, const MachineInstr *CopyMI);
184
185     /// lastRegisterUse - Returns the last use of the specific register between
186     /// cycles Start and End or NULL if there are no uses.
187     MachineOperand *lastRegisterUse(SlotIndex Start, SlotIndex End,
188                                     unsigned Reg, SlotIndex &LastUseIdx) const;
189   };
190
191 } // End llvm namespace
192
193 #endif