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