Handle tGPR register class in a few more places. This fixes some llvm-gcc
[oota-llvm.git] / lib / CodeGen / PHIElimination.h
1 //===-- lib/CodeGen/PHIElimination.h ----------------------------*- 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 #ifndef LLVM_CODEGEN_PHIELIMINATION_HPP
11 #define LLVM_CODEGEN_PHIELIMINATION_HPP
12
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallSet.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18
19 namespace llvm {
20   class LiveVariables;
21   
22   /// Lower PHI instructions to copies.  
23   class PHIElimination : public MachineFunctionPass {
24     MachineRegisterInfo  *MRI; // Machine register information
25   private:
26
27     typedef SmallSet<MachineBasicBlock*, 4> PHIKillList;
28     typedef DenseMap<unsigned, PHIKillList> PHIKillMap;
29     typedef DenseMap<unsigned, MachineBasicBlock*> PHIDefMap;
30
31   public:
32
33     typedef PHIKillList::iterator phi_kill_iterator;
34     typedef PHIKillList::const_iterator const_phi_kill_iterator;
35
36     static char ID; // Pass identification, replacement for typeid
37     PHIElimination() : MachineFunctionPass(&ID) {}
38
39     virtual bool runOnMachineFunction(MachineFunction &Fn);
40     
41     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
42
43     /// Return true if the given vreg was defined by a PHI intsr prior to
44     /// lowering.
45     bool hasPHIDef(unsigned vreg) const {
46       return PHIDefs.count(vreg);
47     }
48
49     /// Returns the block in which the PHI instruction which defined the
50     /// given vreg used to reside. 
51     MachineBasicBlock* getPHIDefBlock(unsigned vreg) {
52       PHIDefMap::iterator phiDefItr = PHIDefs.find(vreg);
53       assert(phiDefItr != PHIDefs.end() && "vreg has no phi-def.");
54       return phiDefItr->second;
55     }
56
57     /// Returns true if the given vreg was killed by a PHI instr.
58     bool hasPHIKills(unsigned vreg) const {
59       return PHIKills.count(vreg);
60     }
61
62     /// Returns an iterator over the BasicBlocks which contained PHI
63     /// kills of this register prior to lowering.
64     phi_kill_iterator phiKillsBegin(unsigned vreg) {
65       PHIKillMap::iterator phiKillItr = PHIKills.find(vreg);
66       assert(phiKillItr != PHIKills.end() && "vreg has no phi-kills.");
67       return phiKillItr->second.begin();
68     } 
69     phi_kill_iterator phiKillsEnd(unsigned vreg) {
70       PHIKillMap::iterator phiKillItr = PHIKills.find(vreg);
71       assert(phiKillItr != PHIKills.end() && "vreg has no phi-kills.");
72       return phiKillItr->second.end();
73     }
74
75   private:
76     /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
77     /// in predecessor basic blocks.
78     ///
79     bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
80     void LowerAtomicPHINode(MachineBasicBlock &MBB,
81                             MachineBasicBlock::iterator AfterPHIsIt);
82
83     /// analyzePHINodes - Gather information about the PHI nodes in
84     /// here. In particular, we want to map the number of uses of a virtual
85     /// register which is used in a PHI node. We map that to the BB the
86     /// vreg is coming from. This is used later to determine when the vreg
87     /// is killed in the BB.
88     ///
89     void analyzePHINodes(const MachineFunction& Fn);
90
91     /// Split critical edges where necessary for good coalescer performance.
92     bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
93                        LiveVariables &LV);
94
95     /// SplitCriticalEdge - Split a critical edge from A to B by
96     /// inserting a new MBB. Update branches in A and PHI instructions
97     /// in B. Return the new block.
98     MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *A,
99                                          MachineBasicBlock *B);
100
101     /// FindCopyInsertPoint - Find a safe place in MBB to insert a copy from
102     /// SrcReg when following the CFG edge to SuccMBB. This needs to be after
103     /// any def of SrcReg, but before any subsequent point where control flow
104     /// might jump out of the basic block.
105     MachineBasicBlock::iterator FindCopyInsertPoint(MachineBasicBlock &MBB,
106                                                     MachineBasicBlock &SuccMBB,
107                                                     unsigned SrcReg);
108
109     // SkipPHIsAndLabels - Copies need to be inserted after phi nodes and
110     // also after any exception handling labels: in landing pads execution
111     // starts at the label, so any copies placed before it won't be executed!
112     // We also deal with DBG_VALUEs, which are a bit tricky:
113     //  PHI
114     //  DBG_VALUE
115     //  LABEL
116     // Here the DBG_VALUE needs to be skipped, and if it refers to a PHI it
117     // needs to be annulled or, better, moved to follow the label, as well.
118     //  PHI
119     //  DBG_VALUE
120     //  no label
121     // Here it is not a good idea to skip the DBG_VALUE.
122     // FIXME: For now we skip and annul all DBG_VALUEs, maximally simple and
123     // maximally stupid.
124     MachineBasicBlock::iterator SkipPHIsAndLabels(MachineBasicBlock &MBB,
125                                                 MachineBasicBlock::iterator I) {
126       // Rather than assuming that EH labels come before other kinds of labels,
127       // just skip all labels.
128       while (I != MBB.end() && 
129              (I->isPHI() || I->isLabel() || I->isDebugValue())) {
130         if (I->isDebugValue() && I->getNumOperands()==3 && 
131             I->getOperand(0).isReg())
132           I->getOperand(0).setReg(0U);
133         ++I;
134       }
135       return I;
136     }
137
138     typedef std::pair<unsigned, unsigned> BBVRegPair;
139     typedef DenseMap<BBVRegPair, unsigned> VRegPHIUse;
140
141     VRegPHIUse VRegPHIUseCount;
142     PHIDefMap PHIDefs;
143     PHIKillMap PHIKills;
144
145     // Defs of PHI sources which are implicit_def.
146     SmallPtrSet<MachineInstr*, 4> ImpDefs;
147
148     // Lowered PHI nodes may be reused. We provide special DenseMap traits to
149     // match PHI nodes with identical arguments.
150     struct PHINodeTraits : public DenseMapInfo<MachineInstr*> {
151       static unsigned getHashValue(const MachineInstr *PtrVal);
152       static bool isEqual(const MachineInstr *LHS, const MachineInstr *RHS);
153     };
154
155     // Map reusable lowered PHI node -> incoming join register.
156     typedef DenseMap<MachineInstr*, unsigned, PHINodeTraits> LoweredPHIMap;
157     LoweredPHIMap LoweredPHIs;
158   };
159
160 }
161
162 #endif /* LLVM_CODEGEN_PHIELIMINATION_HPP */