Lang Hames | 6ffaeb4 | 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 | 26a3c1b | 2009-07-23 04:34:03 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
| 14 | #include "llvm/ADT/SmallSet.h" |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallPtrSet.h" |
| 16 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | 4052b29 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 18 | |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 19 | namespace llvm { |
Chris Lattner | 4052b29 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 20 | class LiveVariables; |
| 21 | |
Lang Hames | 6ffaeb4 | 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 |
Lang Hames | 26a3c1b | 2009-07-23 04:34:03 +0000 | [diff] [blame] | 25 | typedef DenseMap<unsigned, MachineBasicBlock*> PHIDefMap; |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 26 | |
| 27 | public: |
Lang Hames | 26a3c1b | 2009-07-23 04:34:03 +0000 | [diff] [blame] | 28 | |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 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 | |
Lang Hames | 26a3c1b | 2009-07-23 04:34:03 +0000 | [diff] [blame] | 36 | /// Return true if the given vreg was defined by a PHI intsr prior to |
| 37 | /// lowering. |
| 38 | bool hasPHIDef(unsigned vreg) const { |
| 39 | return PHIDefs.count(vreg); |
| 40 | } |
| 41 | |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 42 | private: |
| 43 | /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions |
| 44 | /// in predecessor basic blocks. |
| 45 | /// |
| 46 | bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB); |
| 47 | void LowerAtomicPHINode(MachineBasicBlock &MBB, |
| 48 | MachineBasicBlock::iterator AfterPHIsIt); |
| 49 | |
| 50 | /// analyzePHINodes - Gather information about the PHI nodes in |
| 51 | /// here. In particular, we want to map the number of uses of a virtual |
| 52 | /// register which is used in a PHI node. We map that to the BB the |
| 53 | /// vreg is coming from. This is used later to determine when the vreg |
| 54 | /// is killed in the BB. |
| 55 | /// |
| 56 | void analyzePHINodes(const MachineFunction& Fn); |
| 57 | |
Jakob Stoklund Olesen | be9cdbf | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 58 | /// Split critical edges where necessary for good coalescer performance. |
Jakob Stoklund Olesen | f1a48f5 | 2009-11-18 18:01:35 +0000 | [diff] [blame] | 59 | bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB, |
| 60 | LiveVariables &LV); |
Jakob Stoklund Olesen | be9cdbf | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 61 | |
Jakob Stoklund Olesen | be9cdbf | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 62 | /// SplitCriticalEdge - Split a critical edge from A to B by |
| 63 | /// inserting a new MBB. Update branches in A and PHI instructions |
| 64 | /// in B. Return the new block. |
| 65 | MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *A, |
| 66 | MachineBasicBlock *B); |
| 67 | |
Jakob Stoklund Olesen | abb5622 | 2009-11-13 21:56:15 +0000 | [diff] [blame] | 68 | /// FindCopyInsertPoint - Find a safe place in MBB to insert a copy from |
| 69 | /// SrcReg when following the CFG edge to SuccMBB. This needs to be after |
| 70 | /// any def of SrcReg, but before any subsequent point where control flow |
| 71 | /// might jump out of the basic block. |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 72 | MachineBasicBlock::iterator FindCopyInsertPoint(MachineBasicBlock &MBB, |
Jakob Stoklund Olesen | abb5622 | 2009-11-13 21:56:15 +0000 | [diff] [blame] | 73 | MachineBasicBlock &SuccMBB, |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 74 | unsigned SrcReg); |
| 75 | |
| 76 | // SkipPHIsAndLabels - Copies need to be inserted after phi nodes and |
| 77 | // also after any exception handling labels: in landing pads execution |
| 78 | // starts at the label, so any copies placed before it won't be executed! |
Dale Johannesen | 35740e3 | 2010-02-16 01:57:28 +0000 | [diff] [blame] | 79 | // We also deal with DBG_VALUEs, which are a bit tricky: |
| 80 | // PHI |
| 81 | // DBG_VALUE |
| 82 | // LABEL |
| 83 | // Here the DBG_VALUE needs to be skipped, and if it refers to a PHI it |
| 84 | // needs to be annulled or, better, moved to follow the label, as well. |
| 85 | // PHI |
| 86 | // DBG_VALUE |
| 87 | // no label |
| 88 | // Here it is not a good idea to skip the DBG_VALUE. |
| 89 | // FIXME: For now we skip and annul all DBG_VALUEs, maximally simple and |
| 90 | // maximally stupid. |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 91 | MachineBasicBlock::iterator SkipPHIsAndLabels(MachineBasicBlock &MBB, |
| 92 | MachineBasicBlock::iterator I) { |
| 93 | // Rather than assuming that EH labels come before other kinds of labels, |
| 94 | // just skip all labels. |
Dale Johannesen | 35740e3 | 2010-02-16 01:57:28 +0000 | [diff] [blame] | 95 | while (I != MBB.end() && |
| 96 | (I->isPHI() || I->isLabel() || I->isDebugValue())) { |
| 97 | if (I->isDebugValue() && I->getNumOperands()==3 && |
| 98 | I->getOperand(0).isReg()) |
| 99 | I->getOperand(0).setReg(0U); |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 100 | ++I; |
Dale Johannesen | 35740e3 | 2010-02-16 01:57:28 +0000 | [diff] [blame] | 101 | } |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 102 | return I; |
| 103 | } |
| 104 | |
Jakob Stoklund Olesen | 87450c8 | 2009-12-16 18:55:53 +0000 | [diff] [blame] | 105 | typedef std::pair<unsigned, unsigned> BBVRegPair; |
| 106 | typedef DenseMap<BBVRegPair, unsigned> VRegPHIUse; |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 107 | |
| 108 | VRegPHIUse VRegPHIUseCount; |
Lang Hames | 26a3c1b | 2009-07-23 04:34:03 +0000 | [diff] [blame] | 109 | PHIDefMap PHIDefs; |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 110 | |
| 111 | // Defs of PHI sources which are implicit_def. |
| 112 | SmallPtrSet<MachineInstr*, 4> ImpDefs; |
Jakob Stoklund Olesen | 87450c8 | 2009-12-16 18:55:53 +0000 | [diff] [blame] | 113 | |
| 114 | // Lowered PHI nodes may be reused. We provide special DenseMap traits to |
| 115 | // match PHI nodes with identical arguments. |
| 116 | struct PHINodeTraits : public DenseMapInfo<MachineInstr*> { |
| 117 | static unsigned getHashValue(const MachineInstr *PtrVal); |
| 118 | static bool isEqual(const MachineInstr *LHS, const MachineInstr *RHS); |
| 119 | }; |
| 120 | |
| 121 | // Map reusable lowered PHI node -> incoming join register. |
| 122 | typedef DenseMap<MachineInstr*, unsigned, PHINodeTraits> LoweredPHIMap; |
| 123 | LoweredPHIMap LoweredPHIs; |
Lang Hames | 6ffaeb4 | 2009-07-21 23:47:33 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } |
| 127 | |
Evan Cheng | 368ddcb | 2009-09-04 07:46:30 +0000 | [diff] [blame] | 128 | #endif /* LLVM_CODEGEN_PHIELIMINATION_HPP */ |