7dedf0318a8a67ca296044054a3d12bb437892cf
[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
26   public:
27     static char ID; // Pass identification, replacement for typeid
28     PHIElimination() : MachineFunctionPass(&ID) {}
29
30     virtual bool runOnMachineFunction(MachineFunction &Fn);
31     
32     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
33
34   private:
35     /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
36     /// in predecessor basic blocks.
37     ///
38     bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
39     void LowerAtomicPHINode(MachineBasicBlock &MBB,
40                             MachineBasicBlock::iterator AfterPHIsIt);
41
42     /// analyzePHINodes - Gather information about the PHI nodes in
43     /// here. In particular, we want to map the number of uses of a virtual
44     /// register which is used in a PHI node. We map that to the BB the
45     /// vreg is coming from. This is used later to determine when the vreg
46     /// is killed in the BB.
47     ///
48     void analyzePHINodes(const MachineFunction& Fn);
49
50     /// Split critical edges where necessary for good coalescer performance.
51     bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
52                        LiveVariables &LV);
53
54     /// SplitCriticalEdge - Split a critical edge from A to B by
55     /// inserting a new MBB. Update branches in A and PHI instructions
56     /// in B. Return the new block.
57     MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *A,
58                                          MachineBasicBlock *B);
59
60     /// FindCopyInsertPoint - Find a safe place in MBB to insert a copy from
61     /// SrcReg when following the CFG edge to SuccMBB. This needs to be after
62     /// any def of SrcReg, but before any subsequent point where control flow
63     /// might jump out of the basic block.
64     MachineBasicBlock::iterator FindCopyInsertPoint(MachineBasicBlock &MBB,
65                                                     MachineBasicBlock &SuccMBB,
66                                                     unsigned SrcReg);
67
68     // SkipPHIsAndLabels - Copies need to be inserted after phi nodes and
69     // also after any exception handling labels: in landing pads execution
70     // starts at the label, so any copies placed before it won't be executed!
71     // We also deal with DBG_VALUEs, which are a bit tricky:
72     //  PHI
73     //  DBG_VALUE
74     //  LABEL
75     // Here the DBG_VALUE needs to be skipped, and if it refers to a PHI it
76     // needs to be annulled or, better, moved to follow the label, as well.
77     //  PHI
78     //  DBG_VALUE
79     //  no label
80     // Here it is not a good idea to skip the DBG_VALUE.
81     // FIXME: For now we skip and annul all DBG_VALUEs, maximally simple and
82     // maximally stupid.
83     MachineBasicBlock::iterator SkipPHIsAndLabels(MachineBasicBlock &MBB,
84                                                 MachineBasicBlock::iterator I) {
85       // Rather than assuming that EH labels come before other kinds of labels,
86       // just skip all labels.
87       while (I != MBB.end() && 
88              (I->isPHI() || I->isLabel() || I->isDebugValue())) {
89         if (I->isDebugValue() && I->getNumOperands()==3 && 
90             I->getOperand(0).isReg())
91           I->getOperand(0).setReg(0U);
92         ++I;
93       }
94       return I;
95     }
96
97     typedef std::pair<unsigned, unsigned> BBVRegPair;
98     typedef DenseMap<BBVRegPair, unsigned> VRegPHIUse;
99
100     VRegPHIUse VRegPHIUseCount;
101
102     // Defs of PHI sources which are implicit_def.
103     SmallPtrSet<MachineInstr*, 4> ImpDefs;
104
105     // Map reusable lowered PHI node -> incoming join register.
106     typedef DenseMap<MachineInstr*, unsigned,
107                      MachineInstrExpressionTrait> LoweredPHIMap;
108     LoweredPHIMap LoweredPHIs;
109   };
110
111 }
112
113 #endif /* LLVM_CODEGEN_PHIELIMINATION_HPP */