llvm.org GIT mirror llvm / a474685
Move the FindCopyInsertPoint method of PHIElimination to a new standalone function so that it can be shared with StrongPHIElimination. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120951 91177308-0d34-0410-b5e6-96231b3b80d8 Cameron Zwarich 9 years ago
4 changed file(s) with 90 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
4949 OcamlGC.cpp
5050 OptimizePHIs.cpp
5151 PHIElimination.cpp
52 PHIEliminationUtils.cpp
5253 Passes.cpp
5354 PeepholeOptimizer.cpp
5455 PostRAHazardRecognizer.cpp
1414
1515 #define DEBUG_TYPE "phielim"
1616 #include "PHIElimination.h"
17 #include "PHIEliminationUtils.h"
1718 #include "llvm/CodeGen/LiveVariables.h"
1819 #include "llvm/CodeGen/Passes.h"
1920 #include "llvm/CodeGen/MachineDominators.h"
120121 return true;
121122 }
122123
123 // FindCopyInsertPoint - Find a safe place in MBB to insert a copy from SrcReg
124 // when following the CFG edge to SuccMBB. This needs to be after any def of
125 // SrcReg, but before any subsequent point where control flow might jump out of
126 // the basic block.
127 MachineBasicBlock::iterator
128 llvm::PHIElimination::FindCopyInsertPoint(MachineBasicBlock &MBB,
129 MachineBasicBlock &SuccMBB,
130 unsigned SrcReg) {
131 // Handle the trivial case trivially.
132 if (MBB.empty())
133 return MBB.begin();
134
135 // Usually, we just want to insert the copy before the first terminator
136 // instruction. However, for the edge going to a landing pad, we must insert
137 // the copy before the call/invoke instruction.
138 if (!SuccMBB.isLandingPad())
139 return MBB.getFirstTerminator();
140
141 // Discover any defs/uses in this basic block.
142 SmallPtrSet DefUsesInMBB;
143 for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(SrcReg),
144 RE = MRI->reg_end(); RI != RE; ++RI) {
145 MachineInstr *DefUseMI = &*RI;
146 if (DefUseMI->getParent() == &MBB)
147 DefUsesInMBB.insert(DefUseMI);
148 }
149
150 MachineBasicBlock::iterator InsertPoint;
151 if (DefUsesInMBB.empty()) {
152 // No defs. Insert the copy at the start of the basic block.
153 InsertPoint = MBB.begin();
154 } else if (DefUsesInMBB.size() == 1) {
155 // Insert the copy immediately after the def/use.
156 InsertPoint = *DefUsesInMBB.begin();
157 ++InsertPoint;
158 } else {
159 // Insert the copy immediately after the last def/use.
160 InsertPoint = MBB.end();
161 while (!DefUsesInMBB.count(&*--InsertPoint)) {}
162 ++InsertPoint;
163 }
164
165 // Make sure the copy goes after any phi nodes however.
166 return MBB.SkipPHIsAndLabels(InsertPoint);
167 }
124
168125
169126 /// LowerAtomicPHINode - Lower the PHI node at the top of the specified block,
170127 /// under the assuption that it needs to be lowered in a way that supports
293250 // Find a safe location to insert the copy, this may be the first terminator
294251 // in the block (or end()).
295252 MachineBasicBlock::iterator InsertPos =
296 FindCopyInsertPoint(opBlock, MBB, SrcReg);
253 findPHICopyInsertPoint(&opBlock, &MBB, SrcReg);
297254
298255 // Insert the copy.
299256 if (!reusedIncoming && IncomingReg)
0 //===-- PHIEliminationUtils.cpp - Helper functions for PHI elimination ----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "PHIEliminationUtils.h"
10 #include "llvm/CodeGen/MachineBasicBlock.h"
11 #include "llvm/CodeGen/MachineFunction.h"
12 #include "llvm/CodeGen/MachineRegisterInfo.h"
13 #include "llvm/ADT/SmallPtrSet.h"
14 using namespace llvm;
15
16 // findCopyInsertPoint - Find a safe place in MBB to insert a copy from SrcReg
17 // when following the CFG edge to SuccMBB. This needs to be after any def of
18 // SrcReg, but before any subsequent point where control flow might jump out of
19 // the basic block.
20 MachineBasicBlock::iterator
21 llvm::findPHICopyInsertPoint(MachineBasicBlock* MBB, MachineBasicBlock* SuccMBB,
22 unsigned SrcReg) {
23 // Handle the trivial case trivially.
24 if (MBB->empty())
25 return MBB->begin();
26
27 // Usually, we just want to insert the copy before the first terminator
28 // instruction. However, for the edge going to a landing pad, we must insert
29 // the copy before the call/invoke instruction.
30 if (!SuccMBB->isLandingPad())
31 return MBB->getFirstTerminator();
32
33 // Discover any defs/uses in this basic block.
34 SmallPtrSet DefUsesInMBB;
35 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
36 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(SrcReg),
37 RE = MRI.reg_end(); RI != RE; ++RI) {
38 MachineInstr* DefUseMI = &*RI;
39 if (DefUseMI->getParent() == MBB)
40 DefUsesInMBB.insert(DefUseMI);
41 }
42
43 MachineBasicBlock::iterator InsertPoint;
44 if (DefUsesInMBB.empty()) {
45 // No defs. Insert the copy at the start of the basic block.
46 InsertPoint = MBB->begin();
47 } else if (DefUsesInMBB.size() == 1) {
48 // Insert the copy immediately after the def/use.
49 InsertPoint = *DefUsesInMBB.begin();
50 ++InsertPoint;
51 } else {
52 // Insert the copy immediately after the last def/use.
53 InsertPoint = MBB->end();
54 while (!DefUsesInMBB.count(&*--InsertPoint)) {}
55 ++InsertPoint;
56 }
57
58 // Make sure the copy goes after any phi nodes however.
59 return MBB->SkipPHIsAndLabels(InsertPoint);
60 }
0 //=- PHIEliminationUtils.h - Helper functions for PHI elimination *- C++ -*--=//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CODEGEN_PHIELIMINATIONUTILS_H
10 #define LLVM_CODEGEN_PHIELIMINATIONUTILS_H
11
12 #include "llvm/CodeGen/MachineBasicBlock.h"
13
14 namespace llvm {
15 /// findPHICopyInsertPoint - Find a safe place in MBB to insert a copy from
16 /// SrcReg when following the CFG edge to SuccMBB. This needs to be after
17 /// any def of SrcReg, but before any subsequent point where control flow
18 /// might jump out of the basic block.
19 MachineBasicBlock::iterator
20 findPHICopyInsertPoint(MachineBasicBlock* MBB, MachineBasicBlock* SuccMBB,
21 unsigned SrcReg);
22 }
23
24 #endif