blob: f8c9fe728457023124eed09ca9d8aea9be48e95a [file] [log] [blame]
Lang Hamesfae02a22009-07-21 23:47:33 +00001//===-- 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 Hames287b8b02009-07-23 04:34:03 +000013#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/SmallSet.h"
Lang Hamesfae02a22009-07-21 23:47:33 +000015#include "llvm/ADT/SmallPtrSet.h"
16#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/Target/TargetInstrInfo.h"
18
19#include <map>
20
21namespace llvm {
22
23 /// Lower PHI instructions to copies.
24 class PHIElimination : public MachineFunctionPass {
25 MachineRegisterInfo *MRI; // Machine register information
Lang Hames287b8b02009-07-23 04:34:03 +000026 private:
27
28 typedef SmallSet<MachineBasicBlock*, 4> PHIKillList;
29 typedef DenseMap<unsigned, PHIKillList> PHIKillMap;
30 typedef DenseMap<unsigned, MachineBasicBlock*> PHIDefMap;
Lang Hamesfae02a22009-07-21 23:47:33 +000031
32 public:
Lang Hames287b8b02009-07-23 04:34:03 +000033
34 typedef PHIKillList::iterator phi_kill_iterator;
35 typedef PHIKillList::const_iterator const_phi_kill_iterator;
36
Lang Hamesfae02a22009-07-21 23:47:33 +000037 static char ID; // Pass identification, replacement for typeid
38 PHIElimination() : MachineFunctionPass(&ID) {}
39
40 virtual bool runOnMachineFunction(MachineFunction &Fn);
41
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
43
Lang Hames287b8b02009-07-23 04:34:03 +000044 /// Return true if the given vreg was defined by a PHI intsr prior to
45 /// lowering.
46 bool hasPHIDef(unsigned vreg) const {
47 return PHIDefs.count(vreg);
48 }
49
50 /// Returns the block in which the PHI instruction which defined the
51 /// given vreg used to reside.
52 MachineBasicBlock* getPHIDefBlock(unsigned vreg) {
53 PHIDefMap::iterator phiDefItr = PHIDefs.find(vreg);
54 assert(phiDefItr != PHIDefs.end() && "vreg has no phi-def.");
55 return phiDefItr->second;
56 }
57
58 /// Returns true if the given vreg was killed by a PHI instr.
59 bool hasPHIKills(unsigned vreg) const {
60 return PHIKills.count(vreg);
61 }
62
63 /// Returns an iterator over the BasicBlocks which contained PHI
64 /// kills of this register prior to lowering.
65 phi_kill_iterator phiKillsBegin(unsigned vreg) {
66 PHIKillMap::iterator phiKillItr = PHIKills.find(vreg);
67 assert(phiKillItr != PHIKills.end() && "vreg has no phi-kills.");
68 return phiKillItr->second.begin();
69 }
70 phi_kill_iterator phiKillsEnd(unsigned vreg) {
71 PHIKillMap::iterator phiKillItr = PHIKills.find(vreg);
72 assert(phiKillItr != PHIKills.end() && "vreg has no phi-kills.");
73 return phiKillItr->second.end();
74 }
75
Lang Hamesfae02a22009-07-21 23:47:33 +000076 private:
77 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
78 /// in predecessor basic blocks.
79 ///
80 bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
81 void LowerAtomicPHINode(MachineBasicBlock &MBB,
82 MachineBasicBlock::iterator AfterPHIsIt);
83
84 /// analyzePHINodes - Gather information about the PHI nodes in
85 /// here. In particular, we want to map the number of uses of a virtual
86 /// register which is used in a PHI node. We map that to the BB the
87 /// vreg is coming from. This is used later to determine when the vreg
88 /// is killed in the BB.
89 ///
90 void analyzePHINodes(const MachineFunction& Fn);
91
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000092 /// Split critical edges where necessary for good coalescer performance.
Jakob Stoklund Olesen0257dd32009-11-18 18:01:35 +000093 bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
94 LiveVariables &LV);
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +000095
Jakob Stoklund Olesene35e3c32009-11-10 22:00:56 +000096 /// isLiveOut - Determine if Reg is live out from MBB, when not
97 /// considering PHI nodes. This means that Reg is either killed by
98 /// a successor block or passed through one.
99 bool isLiveOut(unsigned Reg, const MachineBasicBlock &MBB,
100 LiveVariables &LV);
101
Jakob Stoklund Olesen3e204752009-11-11 19:31:31 +0000102 /// isLiveIn - Determine if Reg is live in to MBB, not considering PHI
103 /// source registers. This means that Reg is either killed by MBB or passes
104 /// through it.
105 bool isLiveIn(unsigned Reg, const MachineBasicBlock &MBB,
106 LiveVariables &LV);
107
Jakob Stoklund Olesenf235f132009-11-10 22:01:05 +0000108 /// SplitCriticalEdge - Split a critical edge from A to B by
109 /// inserting a new MBB. Update branches in A and PHI instructions
110 /// in B. Return the new block.
111 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *A,
112 MachineBasicBlock *B);
113
Jakob Stoklund Olesen12222872009-11-13 21:56:15 +0000114 /// FindCopyInsertPoint - Find a safe place in MBB to insert a copy from
115 /// SrcReg when following the CFG edge to SuccMBB. This needs to be after
116 /// any def of SrcReg, but before any subsequent point where control flow
117 /// might jump out of the basic block.
Lang Hamesfae02a22009-07-21 23:47:33 +0000118 MachineBasicBlock::iterator FindCopyInsertPoint(MachineBasicBlock &MBB,
Jakob Stoklund Olesen12222872009-11-13 21:56:15 +0000119 MachineBasicBlock &SuccMBB,
Lang Hamesfae02a22009-07-21 23:47:33 +0000120 unsigned SrcReg);
121
122 // SkipPHIsAndLabels - Copies need to be inserted after phi nodes and
123 // also after any exception handling labels: in landing pads execution
124 // starts at the label, so any copies placed before it won't be executed!
125 MachineBasicBlock::iterator SkipPHIsAndLabels(MachineBasicBlock &MBB,
126 MachineBasicBlock::iterator I) {
127 // Rather than assuming that EH labels come before other kinds of labels,
128 // just skip all labels.
129 while (I != MBB.end() &&
130 (I->getOpcode() == TargetInstrInfo::PHI || I->isLabel()))
131 ++I;
132 return I;
133 }
134
135 typedef std::pair<const MachineBasicBlock*, unsigned> BBVRegPair;
136 typedef std::map<BBVRegPair, unsigned> VRegPHIUse;
137
138 VRegPHIUse VRegPHIUseCount;
Lang Hames287b8b02009-07-23 04:34:03 +0000139 PHIDefMap PHIDefs;
140 PHIKillMap PHIKills;
Lang Hamesfae02a22009-07-21 23:47:33 +0000141
142 // Defs of PHI sources which are implicit_def.
143 SmallPtrSet<MachineInstr*, 4> ImpDefs;
144 };
145
146}
147
Evan Chenge9390912009-09-04 07:46:30 +0000148#endif /* LLVM_CODEGEN_PHIELIMINATION_HPP */