remove parser support for the obsolete "multiple return values" syntax, which
[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 "RegisterClassInfo.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     RegisterClassInfo RegClassInfo;
51
52     /// JoinedCopies - Keep track of copies eliminated due to coalescing.
53     ///
54     SmallPtrSet<MachineInstr*, 32> JoinedCopies;
55
56     /// ReMatCopies - Keep track of copies eliminated due to remat.
57     ///
58     SmallPtrSet<MachineInstr*, 32> ReMatCopies;
59
60     /// ReMatDefs - Keep track of definition instructions which have
61     /// been remat'ed.
62     SmallPtrSet<MachineInstr*, 8> ReMatDefs;
63
64   public:
65     static char ID; // Pass identifcation, replacement for typeid
66     SimpleRegisterCoalescing() : MachineFunctionPass(ID) {
67       initializeSimpleRegisterCoalescingPass(*PassRegistry::getPassRegistry());
68     }
69
70     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
71     virtual void releaseMemory();
72
73     /// runOnMachineFunction - pass entry point
74     virtual bool runOnMachineFunction(MachineFunction&);
75
76     bool coalesceFunction(MachineFunction &mf, RegallocQuery &) {
77       // This runs as an independent pass, so don't do anything.
78       return false;
79     }
80
81     /// print - Implement the dump method.
82     virtual void print(raw_ostream &O, const Module* = 0) const;
83
84   private:
85     /// joinIntervals - join compatible live intervals
86     void joinIntervals();
87
88     /// CopyCoalesceInMBB - Coalesce copies in the specified MBB, putting
89     /// copies that cannot yet be coalesced into the "TryAgain" list.
90     void CopyCoalesceInMBB(MachineBasicBlock *MBB,
91                            std::vector<CopyRec> &TryAgain);
92
93     /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
94     /// which are the src/dst of the copy instruction CopyMI.  This returns true
95     /// if the copy was successfully coalesced away. If it is not currently
96     /// possible to coalesce this interval, but it may be possible if other
97     /// things get coalesced, then it returns true by reference in 'Again'.
98     bool JoinCopy(CopyRec &TheCopy, bool &Again);
99
100     /// JoinIntervals - Attempt to join these two intervals.  On failure, this
101     /// returns false.  The output "SrcInt" will not have been modified, so we can
102     /// use this information below to update aliases.
103     bool JoinIntervals(CoalescerPair &CP);
104
105     /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy. If
106     /// the source value number is defined by a copy from the destination reg
107     /// see if we can merge these two destination reg valno# into a single
108     /// value number, eliminating a copy.
109     bool AdjustCopiesBackFrom(const CoalescerPair &CP, MachineInstr *CopyMI);
110
111     /// HasOtherReachingDefs - Return true if there are definitions of IntB
112     /// other than BValNo val# that can reach uses of AValno val# of IntA.
113     bool HasOtherReachingDefs(LiveInterval &IntA, LiveInterval &IntB,
114                               VNInfo *AValNo, VNInfo *BValNo);
115
116     /// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy.
117     /// If the source value number is defined by a commutable instruction and
118     /// its other operand is coalesced to the copy dest register, see if we
119     /// can transform the copy into a noop by commuting the definition.
120     bool RemoveCopyByCommutingDef(const CoalescerPair &CP,MachineInstr *CopyMI);
121
122     /// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
123     /// computation, replace the copy by rematerialize the definition.
124     /// If PreserveSrcInt is true, make sure SrcInt is valid after the call.
125     bool ReMaterializeTrivialDef(LiveInterval &SrcInt, bool PreserveSrcInt,
126                                  unsigned DstReg, unsigned DstSubIdx,
127                                  MachineInstr *CopyMI);
128
129     /// shouldJoinPhys - Return true if a physreg copy should be joined.
130     bool shouldJoinPhys(CoalescerPair &CP);
131
132     /// isWinToJoinCrossClass - Return true if it's profitable to coalesce
133     /// two virtual registers from different register classes.
134     bool isWinToJoinCrossClass(unsigned SrcReg,
135                                unsigned DstReg,
136                                const TargetRegisterClass *SrcRC,
137                                const TargetRegisterClass *DstRC,
138                                const TargetRegisterClass *NewRC);
139
140     /// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
141     /// update the subregister number if it is not zero. If DstReg is a
142     /// physical register and the existing subregister number of the def / use
143     /// being updated is not zero, make sure to set it to the correct physical
144     /// subregister.
145     void UpdateRegDefsUses(const CoalescerPair &CP);
146
147     /// RemoveDeadDef - If a def of a live interval is now determined dead,
148     /// remove the val# it defines. If the live interval becomes empty, remove
149     /// it as well.
150     bool RemoveDeadDef(LiveInterval &li, MachineInstr *DefMI);
151
152     /// RemoveCopyFlag - If DstReg is no longer defined by CopyMI, clear the
153     /// VNInfo copy flag for DstReg and all aliases.
154     void RemoveCopyFlag(unsigned DstReg, const MachineInstr *CopyMI);
155
156     /// markAsJoined - Remember that CopyMI has already been joined.
157     void markAsJoined(MachineInstr *CopyMI);
158   };
159
160 } // End llvm namespace
161
162 #endif