1 //===-- lib/CodeGen/PHIElimination.h ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CODEGEN_PHIELIMINATION_HPP
11 #define LLVM_CODEGEN_PHIELIMINATION_HPP
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/Target/TargetInstrInfo.h"
21 /// Lower PHI instructions to copies.
22 class PHIElimination : public MachineFunctionPass {
23 MachineRegisterInfo *MRI; // Machine register information
26 typedef SmallSet<MachineBasicBlock*, 4> PHIKillList;
27 typedef DenseMap<unsigned, PHIKillList> PHIKillMap;
28 typedef DenseMap<unsigned, MachineBasicBlock*> PHIDefMap;
32 typedef PHIKillList::iterator phi_kill_iterator;
33 typedef PHIKillList::const_iterator const_phi_kill_iterator;
35 static char ID; // Pass identification, replacement for typeid
36 PHIElimination() : MachineFunctionPass(&ID) {}
38 virtual bool runOnMachineFunction(MachineFunction &Fn);
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
42 /// Return true if the given vreg was defined by a PHI intsr prior to
44 bool hasPHIDef(unsigned vreg) const {
45 return PHIDefs.count(vreg);
48 /// Returns the block in which the PHI instruction which defined the
49 /// given vreg used to reside.
50 MachineBasicBlock* getPHIDefBlock(unsigned vreg) {
51 PHIDefMap::iterator phiDefItr = PHIDefs.find(vreg);
52 assert(phiDefItr != PHIDefs.end() && "vreg has no phi-def.");
53 return phiDefItr->second;
56 /// Returns true if the given vreg was killed by a PHI instr.
57 bool hasPHIKills(unsigned vreg) const {
58 return PHIKills.count(vreg);
61 /// Returns an iterator over the BasicBlocks which contained PHI
62 /// kills of this register prior to lowering.
63 phi_kill_iterator phiKillsBegin(unsigned vreg) {
64 PHIKillMap::iterator phiKillItr = PHIKills.find(vreg);
65 assert(phiKillItr != PHIKills.end() && "vreg has no phi-kills.");
66 return phiKillItr->second.begin();
68 phi_kill_iterator phiKillsEnd(unsigned vreg) {
69 PHIKillMap::iterator phiKillItr = PHIKills.find(vreg);
70 assert(phiKillItr != PHIKills.end() && "vreg has no phi-kills.");
71 return phiKillItr->second.end();
75 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
76 /// in predecessor basic blocks.
78 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
79 void LowerAtomicPHINode(MachineBasicBlock &MBB,
80 MachineBasicBlock::iterator AfterPHIsIt);
82 /// analyzePHINodes - Gather information about the PHI nodes in
83 /// here. In particular, we want to map the number of uses of a virtual
84 /// register which is used in a PHI node. We map that to the BB the
85 /// vreg is coming from. This is used later to determine when the vreg
86 /// is killed in the BB.
88 void analyzePHINodes(const MachineFunction& Fn);
90 /// Split critical edges where necessary for good coalescer performance.
91 bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
94 /// SplitCriticalEdge - Split a critical edge from A to B by
95 /// inserting a new MBB. Update branches in A and PHI instructions
96 /// in B. Return the new block.
97 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *A,
98 MachineBasicBlock *B);
100 /// FindCopyInsertPoint - Find a safe place in MBB to insert a copy from
101 /// SrcReg when following the CFG edge to SuccMBB. This needs to be after
102 /// any def of SrcReg, but before any subsequent point where control flow
103 /// might jump out of the basic block.
104 MachineBasicBlock::iterator FindCopyInsertPoint(MachineBasicBlock &MBB,
105 MachineBasicBlock &SuccMBB,
108 // SkipPHIsAndLabels - Copies need to be inserted after phi nodes and
109 // also after any exception handling labels: in landing pads execution
110 // starts at the label, so any copies placed before it won't be executed!
111 MachineBasicBlock::iterator SkipPHIsAndLabels(MachineBasicBlock &MBB,
112 MachineBasicBlock::iterator I) {
113 // Rather than assuming that EH labels come before other kinds of labels,
114 // just skip all labels.
115 while (I != MBB.end() &&
116 (I->getOpcode() == TargetInstrInfo::PHI || I->isLabel()))
121 typedef std::pair<unsigned, unsigned> BBVRegPair;
122 typedef DenseMap<BBVRegPair, unsigned> VRegPHIUse;
124 VRegPHIUse VRegPHIUseCount;
128 // Defs of PHI sources which are implicit_def.
129 SmallPtrSet<MachineInstr*, 4> ImpDefs;
131 // Lowered PHI nodes may be reused. We provide special DenseMap traits to
132 // match PHI nodes with identical arguments.
133 struct PHINodeTraits : public DenseMapInfo<MachineInstr*> {
134 static unsigned getHashValue(const MachineInstr *PtrVal);
135 static bool isEqual(const MachineInstr *LHS, const MachineInstr *RHS);
138 // Map reusable lowered PHI node -> incoming join register.
139 typedef DenseMap<MachineInstr*, unsigned, PHINodeTraits> LoweredPHIMap;
140 LoweredPHIMap LoweredPHIs;
145 #endif /* LLVM_CODEGEN_PHIELIMINATION_HPP */