Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 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 | |
Lang Hames | 287b8b0 | 2009-07-23 04:34:03 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
| 14 | #include "llvm/ADT/SmallSet.h" |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallPtrSet.h" |
| 16 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 18 | |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 19 | namespace llvm { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 20 | class LiveVariables; |
| 21 | |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 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 | |
Jakob Stoklund Olesen | f235f13 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 50 | /// Split critical edges where necessary for good coalescer performance. |
Jakob Stoklund Olesen | 0257dd3 | 2009-11-18 18:01:35 +0000 | [diff] [blame] | 51 | bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB, |
| 52 | LiveVariables &LV); |
Jakob Stoklund Olesen | f235f13 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 53 | |
Jakob Stoklund Olesen | f235f13 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 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 | |
Jakob Stoklund Olesen | 1222287 | 2009-11-13 21:56:15 +0000 | [diff] [blame] | 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. |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 64 | MachineBasicBlock::iterator FindCopyInsertPoint(MachineBasicBlock &MBB, |
Jakob Stoklund Olesen | 1222287 | 2009-11-13 21:56:15 +0000 | [diff] [blame] | 65 | MachineBasicBlock &SuccMBB, |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 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! |
Dale Johannesen | 6e303cb | 2010-02-16 01:57:28 +0000 | [diff] [blame] | 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. |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 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. |
Dale Johannesen | 6e303cb | 2010-02-16 01:57:28 +0000 | [diff] [blame] | 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); |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 92 | ++I; |
Dale Johannesen | 6e303cb | 2010-02-16 01:57:28 +0000 | [diff] [blame] | 93 | } |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 94 | return I; |
| 95 | } |
| 96 | |
Jakob Stoklund Olesen | 74215fc | 2009-12-16 18:55:53 +0000 | [diff] [blame] | 97 | typedef std::pair<unsigned, unsigned> BBVRegPair; |
| 98 | typedef DenseMap<BBVRegPair, unsigned> VRegPHIUse; |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 99 | |
| 100 | VRegPHIUse VRegPHIUseCount; |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 101 | |
| 102 | // Defs of PHI sources which are implicit_def. |
| 103 | SmallPtrSet<MachineInstr*, 4> ImpDefs; |
Jakob Stoklund Olesen | 74215fc | 2009-12-16 18:55:53 +0000 | [diff] [blame] | 104 | |
Jakob Stoklund Olesen | 74215fc | 2009-12-16 18:55:53 +0000 | [diff] [blame] | 105 | // Map reusable lowered PHI node -> incoming join register. |
Evan Cheng | a92dced | 2010-03-03 23:55:49 +0000 | [diff] [blame^] | 106 | typedef DenseMap<MachineInstr*, unsigned, |
| 107 | MachineInstrExpressionTrait> LoweredPHIMap; |
Jakob Stoklund Olesen | 74215fc | 2009-12-16 18:55:53 +0000 | [diff] [blame] | 108 | LoweredPHIMap LoweredPHIs; |
Lang Hames | fae02a2 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } |
| 112 | |
Evan Cheng | e939091 | 2009-09-04 07:46:30 +0000 | [diff] [blame] | 113 | #endif /* LLVM_CODEGEN_PHIELIMINATION_HPP */ |