Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 1 | //===- StrongPhiElimination.cpp - Eliminate PHI nodes by inserting copies -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass eliminates machine instruction PHI nodes by inserting copy |
| 11 | // instructions, using an intelligent copy-folding technique based on |
| 12 | // dominator information. This is technique is derived from: |
| 13 | // |
| 14 | // Budimlic, et al. Fast copy coalescing and live-range identification. |
| 15 | // In Proceedings of the ACM SIGPLAN 2002 Conference on Programming Language |
| 16 | // Design and Implementation (Berlin, Germany, June 17 - 19, 2002). |
| 17 | // PLDI '02. ACM, New York, NY, 25-32. |
| 18 | // DOI= http://doi.acm.org/10.1145/512529.512534 |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #define DEBUG_TYPE "strongphielim" |
| 23 | #include "llvm/CodeGen/Passes.h" |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineDominators.h" |
| 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | #include "llvm/CodeGen/MachineInstr.h" |
Owen Anderson | 755ebab | 2008-03-17 06:08:26 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Owen Anderson | 3947e4d | 2008-05-30 18:38:26 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/RegisterCoalescer.h" |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetInstrInfo.h" |
| 32 | #include "llvm/Target/TargetMachine.h" |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DepthFirstIterator.h" |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Statistic.h" |
| 35 | #include "llvm/Support/Compiler.h" |
| 36 | using namespace llvm; |
| 37 | |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | struct VISIBILITY_HIDDEN StrongPHIElimination : public MachineFunctionPass { |
| 40 | static char ID; // Pass identification, replacement for typeid |
| 41 | StrongPHIElimination() : MachineFunctionPass((intptr_t)&ID) {} |
| 42 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 43 | // Waiting stores, for each MBB, the set of copies that need to |
| 44 | // be inserted into that MBB |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 45 | DenseMap<MachineBasicBlock*, |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 46 | std::map<unsigned, unsigned> > Waiting; |
| 47 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 48 | // Stacks holds the renaming stack for each register |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 49 | std::map<unsigned, std::vector<unsigned> > Stacks; |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 50 | |
| 51 | // Registers in UsedByAnother are PHI nodes that are themselves |
| 52 | // used as operands to another another PHI node |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 53 | std::set<unsigned> UsedByAnother; |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 54 | |
Owen Anderson | a9efb26 | 2008-06-05 17:22:53 +0000 | [diff] [blame] | 55 | // RenameSets are the is a map from a PHI-defined register |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 56 | // to the input registers to be coalesced along with the |
| 57 | // predecessor block for those input registers. |
| 58 | std::map<unsigned, std::map<unsigned, MachineBasicBlock*> > RenameSets; |
Owen Anderson | dfd07ea | 2008-03-12 04:22:57 +0000 | [diff] [blame] | 59 | |
| 60 | // PhiValueNumber holds the ID numbers of the VNs for each phi that we're |
| 61 | // eliminating, indexed by the register defined by that phi. |
| 62 | std::map<unsigned, unsigned> PhiValueNumber; |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 63 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 64 | // Store the DFS-in number of each block |
| 65 | DenseMap<MachineBasicBlock*, unsigned> preorder; |
| 66 | |
| 67 | // Store the DFS-out number of each block |
| 68 | DenseMap<MachineBasicBlock*, unsigned> maxpreorder; |
| 69 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 70 | bool runOnMachineFunction(MachineFunction &Fn); |
| 71 | |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 72 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 73 | AU.addRequired<MachineDominatorTree>(); |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 74 | AU.addRequired<LiveIntervals>(); |
| 75 | |
| 76 | // TODO: Actually make this true. |
| 77 | AU.addPreserved<LiveIntervals>(); |
Owen Anderson | 3947e4d | 2008-05-30 18:38:26 +0000 | [diff] [blame] | 78 | AU.addPreserved<RegisterCoalescer>(); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 79 | MachineFunctionPass::getAnalysisUsage(AU); |
| 80 | } |
| 81 | |
| 82 | virtual void releaseMemory() { |
| 83 | preorder.clear(); |
| 84 | maxpreorder.clear(); |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 85 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 86 | Waiting.clear(); |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 87 | Stacks.clear(); |
| 88 | UsedByAnother.clear(); |
| 89 | RenameSets.clear(); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private: |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 93 | |
| 94 | /// DomForestNode - Represents a node in the "dominator forest". This is |
| 95 | /// a forest in which the nodes represent registers and the edges |
| 96 | /// represent a dominance relation in the block defining those registers. |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 97 | struct DomForestNode { |
| 98 | private: |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 99 | // Store references to our children |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 100 | std::vector<DomForestNode*> children; |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 101 | // The register we represent |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 102 | unsigned reg; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 103 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 104 | // Add another node as our child |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 105 | void addChild(DomForestNode* DFN) { children.push_back(DFN); } |
| 106 | |
| 107 | public: |
| 108 | typedef std::vector<DomForestNode*>::iterator iterator; |
| 109 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 110 | // Create a DomForestNode by providing the register it represents, and |
| 111 | // the node to be its parent. The virtual root node has register 0 |
| 112 | // and a null parent. |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 113 | DomForestNode(unsigned r, DomForestNode* parent) : reg(r) { |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 114 | if (parent) |
| 115 | parent->addChild(this); |
| 116 | } |
| 117 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 118 | ~DomForestNode() { |
| 119 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 120 | delete *I; |
| 121 | } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 122 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 123 | /// getReg - Return the regiser that this node represents |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 124 | inline unsigned getReg() { return reg; } |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 125 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 126 | // Provide iterator access to our children |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 127 | inline DomForestNode::iterator begin() { return children.begin(); } |
| 128 | inline DomForestNode::iterator end() { return children.end(); } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 131 | void computeDFS(MachineFunction& MF); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 132 | void processBlock(MachineBasicBlock* MBB); |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 133 | |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 134 | std::vector<DomForestNode*> computeDomForest( |
| 135 | std::map<unsigned, MachineBasicBlock*>& instrs, |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 136 | MachineRegisterInfo& MRI); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 137 | void processPHIUnion(MachineInstr* Inst, |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 138 | std::map<unsigned, MachineBasicBlock*>& PHIUnion, |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 139 | std::vector<StrongPHIElimination::DomForestNode*>& DF, |
| 140 | std::vector<std::pair<unsigned, unsigned> >& locals); |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 141 | void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed); |
Owen Anderson | e46611e | 2008-03-24 04:11:27 +0000 | [diff] [blame] | 142 | void InsertCopies(MachineBasicBlock* MBB, |
| 143 | SmallPtrSet<MachineBasicBlock*, 16>& v); |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 144 | void mergeLiveIntervals(unsigned primary, unsigned secondary, |
| 145 | MachineBasicBlock* pred); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 146 | }; |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 149 | char StrongPHIElimination::ID = 0; |
| 150 | static RegisterPass<StrongPHIElimination> |
| 151 | X("strong-phi-node-elimination", |
| 152 | "Eliminate PHI nodes for register allocation, intelligently"); |
| 153 | |
Dan Gohman | 6ddba2b | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 154 | const PassInfo *const llvm::StrongPHIEliminationID = &X; |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 155 | |
| 156 | /// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree |
| 157 | /// of the given MachineFunction. These numbers are then used in other parts |
| 158 | /// of the PHI elimination process. |
| 159 | void StrongPHIElimination::computeDFS(MachineFunction& MF) { |
| 160 | SmallPtrSet<MachineDomTreeNode*, 8> frontier; |
| 161 | SmallPtrSet<MachineDomTreeNode*, 8> visited; |
| 162 | |
| 163 | unsigned time = 0; |
| 164 | |
| 165 | MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>(); |
| 166 | |
| 167 | MachineDomTreeNode* node = DT.getRootNode(); |
| 168 | |
| 169 | std::vector<MachineDomTreeNode*> worklist; |
| 170 | worklist.push_back(node); |
| 171 | |
| 172 | while (!worklist.empty()) { |
| 173 | MachineDomTreeNode* currNode = worklist.back(); |
| 174 | |
| 175 | if (!frontier.count(currNode)) { |
| 176 | frontier.insert(currNode); |
| 177 | ++time; |
| 178 | preorder.insert(std::make_pair(currNode->getBlock(), time)); |
| 179 | } |
| 180 | |
| 181 | bool inserted = false; |
Owen Anderson | bc91bd3 | 2008-03-31 01:39:20 +0000 | [diff] [blame] | 182 | for (MachineDomTreeNode::iterator I = currNode->begin(), E = currNode->end(); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 183 | I != E; ++I) |
| 184 | if (!frontier.count(*I) && !visited.count(*I)) { |
| 185 | worklist.push_back(*I); |
| 186 | inserted = true; |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | if (!inserted) { |
| 191 | frontier.erase(currNode); |
| 192 | visited.insert(currNode); |
| 193 | maxpreorder.insert(std::make_pair(currNode->getBlock(), time)); |
| 194 | |
| 195 | worklist.pop_back(); |
| 196 | } |
| 197 | } |
Duncan Sands | 1bd3271 | 2007-10-31 08:49:24 +0000 | [diff] [blame] | 198 | } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 199 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 200 | namespace { |
| 201 | |
Owen Anderson | 8b96b9f | 2007-11-06 05:26:02 +0000 | [diff] [blame] | 202 | /// PreorderSorter - a helper class that is used to sort registers |
| 203 | /// according to the preorder number of their defining blocks |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 204 | class PreorderSorter { |
| 205 | private: |
| 206 | DenseMap<MachineBasicBlock*, unsigned>& preorder; |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 207 | MachineRegisterInfo& MRI; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 208 | |
| 209 | public: |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 210 | PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p, |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 211 | MachineRegisterInfo& M) : preorder(p), MRI(M) { } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 212 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 213 | bool operator()(unsigned A, unsigned B) { |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 214 | if (A == B) |
| 215 | return false; |
| 216 | |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 217 | MachineBasicBlock* ABlock = MRI.getVRegDef(A)->getParent(); |
| 218 | MachineBasicBlock* BBlock = MRI.getVRegDef(B)->getParent(); |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 219 | |
| 220 | if (preorder[ABlock] < preorder[BBlock]) |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 221 | return true; |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 222 | else if (preorder[ABlock] > preorder[BBlock]) |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 223 | return false; |
| 224 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 225 | return false; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 226 | } |
| 227 | }; |
| 228 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Owen Anderson | 8b96b9f | 2007-11-06 05:26:02 +0000 | [diff] [blame] | 231 | /// computeDomForest - compute the subforest of the DomTree corresponding |
| 232 | /// to the defining blocks of the registers in question |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 233 | std::vector<StrongPHIElimination::DomForestNode*> |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 234 | StrongPHIElimination::computeDomForest( |
| 235 | std::map<unsigned, MachineBasicBlock*>& regs, |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 236 | MachineRegisterInfo& MRI) { |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 237 | // Begin by creating a virtual root node, since the actual results |
| 238 | // may well be a forest. Assume this node has maximum DFS-out number. |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 239 | DomForestNode* VirtualRoot = new DomForestNode(0, 0); |
| 240 | maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL)); |
| 241 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 242 | // Populate a worklist with the registers |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 243 | std::vector<unsigned> worklist; |
| 244 | worklist.reserve(regs.size()); |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 245 | for (std::map<unsigned, MachineBasicBlock*>::iterator I = regs.begin(), |
| 246 | E = regs.end(); I != E; ++I) |
Owen Anderson | 0031671 | 2008-03-12 03:13:29 +0000 | [diff] [blame] | 247 | worklist.push_back(I->first); |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 248 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 249 | // Sort the registers by the DFS-in number of their defining block |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 250 | PreorderSorter PS(preorder, MRI); |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 251 | std::sort(worklist.begin(), worklist.end(), PS); |
| 252 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 253 | // Create a "current parent" stack, and put the virtual root on top of it |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 254 | DomForestNode* CurrentParent = VirtualRoot; |
| 255 | std::vector<DomForestNode*> stack; |
| 256 | stack.push_back(VirtualRoot); |
| 257 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 258 | // Iterate over all the registers in the previously computed order |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 259 | for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end(); |
| 260 | I != E; ++I) { |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 261 | unsigned pre = preorder[MRI.getVRegDef(*I)->getParent()]; |
Owen Anderson | cb7d949 | 2008-01-09 06:19:05 +0000 | [diff] [blame] | 262 | MachineBasicBlock* parentBlock = CurrentParent->getReg() ? |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 263 | MRI.getVRegDef(CurrentParent->getReg())->getParent() : |
Owen Anderson | cb7d949 | 2008-01-09 06:19:05 +0000 | [diff] [blame] | 264 | 0; |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 265 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 266 | // If the DFS-in number of the register is greater than the DFS-out number |
| 267 | // of the current parent, repeatedly pop the parent stack until it isn't. |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 268 | while (pre > maxpreorder[parentBlock]) { |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 269 | stack.pop_back(); |
| 270 | CurrentParent = stack.back(); |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 271 | |
Owen Anderson | 864e3a3 | 2008-01-09 10:41:39 +0000 | [diff] [blame] | 272 | parentBlock = CurrentParent->getReg() ? |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 273 | MRI.getVRegDef(CurrentParent->getReg())->getParent() : |
Owen Anderson | 864e3a3 | 2008-01-09 10:41:39 +0000 | [diff] [blame] | 274 | 0; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 277 | // Now that we've found the appropriate parent, create a DomForestNode for |
| 278 | // this register and attach it to the forest |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 279 | DomForestNode* child = new DomForestNode(*I, CurrentParent); |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 280 | |
| 281 | // Push this new node on the "current parent" stack |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 282 | stack.push_back(child); |
| 283 | CurrentParent = child; |
| 284 | } |
| 285 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 286 | // Return a vector containing the children of the virtual root node |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 287 | std::vector<DomForestNode*> ret; |
| 288 | ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end()); |
| 289 | return ret; |
| 290 | } |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 291 | |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 292 | /// isLiveIn - helper method that determines, from a regno, if a register |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 293 | /// is live into a block |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 294 | static bool isLiveIn(unsigned r, MachineBasicBlock* MBB, |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 295 | LiveIntervals& LI) { |
| 296 | LiveInterval& I = LI.getOrCreateInterval(r); |
| 297 | unsigned idx = LI.getMBBStartIdx(MBB); |
| 298 | return I.liveBeforeAndAt(idx); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 301 | /// isLiveOut - help method that determines, from a regno, if a register is |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 302 | /// live out of a block. |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 303 | static bool isLiveOut(unsigned r, MachineBasicBlock* MBB, |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 304 | LiveIntervals& LI) { |
| 305 | for (MachineBasicBlock::succ_iterator PI = MBB->succ_begin(), |
| 306 | E = MBB->succ_end(); PI != E; ++PI) { |
| 307 | if (isLiveIn(r, *PI, LI)) |
| 308 | return true; |
Owen Anderson | 14b3fb7 | 2007-11-08 01:32:45 +0000 | [diff] [blame] | 309 | } |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 310 | |
| 311 | return false; |
| 312 | } |
| 313 | |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 314 | /// interferes - checks for local interferences by scanning a block. The only |
| 315 | /// trick parameter is 'mode' which tells it the relationship of the two |
| 316 | /// registers. 0 - defined in the same block, 1 - first properly dominates |
| 317 | /// second, 2 - second properly dominates first |
Owen Anderson | b199cbe | 2008-01-10 00:33:11 +0000 | [diff] [blame] | 318 | static bool interferes(unsigned a, unsigned b, MachineBasicBlock* scan, |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 319 | LiveIntervals& LV, unsigned mode) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 320 | MachineInstr* def = 0; |
| 321 | MachineInstr* kill = 0; |
| 322 | |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 323 | // The code is still in SSA form at this point, so there is only one |
| 324 | // definition per VReg. Thus we can safely use MRI->getVRegDef(). |
| 325 | const MachineRegisterInfo* MRI = &scan->getParent()->getRegInfo(); |
Owen Anderson | b199cbe | 2008-01-10 00:33:11 +0000 | [diff] [blame] | 326 | |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 327 | bool interference = false; |
| 328 | |
| 329 | // Wallk the block, checking for interferences |
| 330 | for (MachineBasicBlock::iterator MBI = scan->begin(), MBE = scan->end(); |
| 331 | MBI != MBE; ++MBI) { |
| 332 | MachineInstr* curr = MBI; |
| 333 | |
| 334 | // Same defining block... |
| 335 | if (mode == 0) { |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 336 | if (curr == MRI->getVRegDef(a)) { |
| 337 | // If we find our first definition, save it |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 338 | if (!def) { |
| 339 | def = curr; |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 340 | // If there's already an unkilled definition, then |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 341 | // this is an interference |
| 342 | } else if (!kill) { |
| 343 | interference = true; |
| 344 | break; |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 345 | // If there's a definition followed by a KillInst, then |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 346 | // they can't interfere |
| 347 | } else { |
| 348 | interference = false; |
| 349 | break; |
| 350 | } |
| 351 | // Symmetric with the above |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 352 | } else if (curr == MRI->getVRegDef(b)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 353 | if (!def) { |
| 354 | def = curr; |
| 355 | } else if (!kill) { |
| 356 | interference = true; |
| 357 | break; |
| 358 | } else { |
| 359 | interference = false; |
| 360 | break; |
| 361 | } |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 362 | // Store KillInsts if they match up with the definition |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 363 | } else if (curr->killsRegister(a)) { |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 364 | if (def == MRI->getVRegDef(a)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 365 | kill = curr; |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 366 | } else if (curr->killsRegister(b)) { |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 367 | if (def == MRI->getVRegDef(b)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 368 | kill = curr; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | // First properly dominates second... |
| 373 | } else if (mode == 1) { |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 374 | if (curr == MRI->getVRegDef(b)) { |
| 375 | // Definition of second without kill of first is an interference |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 376 | if (!kill) { |
| 377 | interference = true; |
| 378 | break; |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 379 | // Definition after a kill is a non-interference |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 380 | } else { |
| 381 | interference = false; |
| 382 | break; |
| 383 | } |
| 384 | // Save KillInsts of First |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 385 | } else if (curr->killsRegister(a)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 386 | kill = curr; |
| 387 | } |
| 388 | // Symmetric with the above |
| 389 | } else if (mode == 2) { |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 390 | if (curr == MRI->getVRegDef(a)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 391 | if (!kill) { |
| 392 | interference = true; |
| 393 | break; |
| 394 | } else { |
| 395 | interference = false; |
| 396 | break; |
| 397 | } |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 398 | } else if (curr->killsRegister(b)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 399 | kill = curr; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return interference; |
| 405 | } |
| 406 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 407 | /// processBlock - Determine how to break up PHIs in the current block. Each |
| 408 | /// PHI is broken up by some combination of renaming its operands and inserting |
| 409 | /// copies. This method is responsible for determining which operands receive |
| 410 | /// which treatment. |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 411 | void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) { |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 412 | LiveIntervals& LI = getAnalysis<LiveIntervals>(); |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 413 | MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo(); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 414 | |
| 415 | // Holds names that have been added to a set in any PHI within this block |
| 416 | // before the current one. |
| 417 | std::set<unsigned> ProcessedNames; |
| 418 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 419 | // Iterate over all the PHI nodes in this block |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 420 | MachineBasicBlock::iterator P = MBB->begin(); |
Owen Anderson | 78216bc | 2008-04-01 18:05:08 +0000 | [diff] [blame] | 421 | while (P != MBB->end() && P->getOpcode() == TargetInstrInfo::PHI) { |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 422 | unsigned DestReg = P->getOperand(0).getReg(); |
| 423 | |
Owen Anderson | d382f8a | 2008-03-26 03:03:23 +0000 | [diff] [blame] | 424 | // Don't both doing PHI elimination for dead PHI's. |
| 425 | if (P->registerDefIsDead(DestReg)) { |
| 426 | ++P; |
| 427 | continue; |
| 428 | } |
| 429 | |
Owen Anderson | dfd07ea | 2008-03-12 04:22:57 +0000 | [diff] [blame] | 430 | LiveInterval& PI = LI.getOrCreateInterval(DestReg); |
Owen Anderson | c7c0036 | 2008-03-29 01:58:47 +0000 | [diff] [blame] | 431 | unsigned pIdx = LI.getDefIndex(LI.getInstructionIndex(P)); |
Owen Anderson | dfd07ea | 2008-03-12 04:22:57 +0000 | [diff] [blame] | 432 | VNInfo* PVN = PI.getLiveRangeContaining(pIdx)->valno; |
| 433 | PhiValueNumber.insert(std::make_pair(DestReg, PVN->id)); |
| 434 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 435 | // PHIUnion is the set of incoming registers to the PHI node that |
| 436 | // are going to be renames rather than having copies inserted. This set |
| 437 | // is refinded over the course of this function. UnionedBlocks is the set |
| 438 | // of corresponding MBBs. |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 439 | std::map<unsigned, MachineBasicBlock*> PHIUnion; |
Owen Anderson | e46611e | 2008-03-24 04:11:27 +0000 | [diff] [blame] | 440 | SmallPtrSet<MachineBasicBlock*, 8> UnionedBlocks; |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 441 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 442 | // Iterate over the operands of the PHI node |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 443 | for (int i = P->getNumOperands() - 1; i >= 2; i-=2) { |
| 444 | unsigned SrcReg = P->getOperand(i-1).getReg(); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 445 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 446 | // Check for trivial interferences via liveness information, allowing us |
| 447 | // to avoid extra work later. Any registers that interfere cannot both |
| 448 | // be in the renaming set, so choose one and add copies for it instead. |
| 449 | // The conditions are: |
| 450 | // 1) if the operand is live into the PHI node's block OR |
| 451 | // 2) if the PHI node is live out of the operand's defining block OR |
| 452 | // 3) if the operand is itself a PHI node and the original PHI is |
| 453 | // live into the operand's defining block OR |
| 454 | // 4) if the operand is already being renamed for another PHI node |
| 455 | // in this block OR |
| 456 | // 5) if any two operands are defined in the same block, insert copies |
| 457 | // for one of them |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 458 | if (isLiveIn(SrcReg, P->getParent(), LI) || |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 459 | isLiveOut(P->getOperand(0).getReg(), |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 460 | MRI.getVRegDef(SrcReg)->getParent(), LI) || |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 461 | ( MRI.getVRegDef(SrcReg)->getOpcode() == TargetInstrInfo::PHI && |
| 462 | isLiveIn(P->getOperand(0).getReg(), |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 463 | MRI.getVRegDef(SrcReg)->getParent(), LI) ) || |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 464 | ProcessedNames.count(SrcReg) || |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 465 | UnionedBlocks.count(MRI.getVRegDef(SrcReg)->getParent())) { |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 466 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 467 | // Add a copy for the selected register |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 468 | MachineBasicBlock* From = P->getOperand(i).getMBB(); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 469 | Waiting[From].insert(std::make_pair(SrcReg, DestReg)); |
| 470 | UsedByAnother.insert(SrcReg); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 471 | } else { |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 472 | // Otherwise, add it to the renaming set |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 473 | PHIUnion.insert(std::make_pair(SrcReg,P->getOperand(i).getMBB())); |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 474 | UnionedBlocks.insert(MRI.getVRegDef(SrcReg)->getParent()); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 475 | } |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 478 | // Compute the dominator forest for the renaming set. This is a forest |
| 479 | // where the nodes are the registers and the edges represent dominance |
| 480 | // relations between the defining blocks of the registers |
Owen Anderson | 42f9e96 | 2007-11-13 20:13:24 +0000 | [diff] [blame] | 481 | std::vector<StrongPHIElimination::DomForestNode*> DF = |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 482 | computeDomForest(PHIUnion, MRI); |
Owen Anderson | 42f9e96 | 2007-11-13 20:13:24 +0000 | [diff] [blame] | 483 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 484 | // Walk DomForest to resolve interferences at an inter-block level. This |
| 485 | // will remove registers from the renaming set (and insert copies for them) |
| 486 | // if interferences are found. |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 487 | std::vector<std::pair<unsigned, unsigned> > localInterferences; |
| 488 | processPHIUnion(P, PHIUnion, DF, localInterferences); |
| 489 | |
Owen Anderson | 52b1733 | 2008-04-02 03:00:13 +0000 | [diff] [blame] | 490 | // If one of the inputs is defined in the same block as the current PHI |
| 491 | // then we need to check for a local interference between that input and |
| 492 | // the PHI. |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 493 | for (std::map<unsigned, MachineBasicBlock*>::iterator I = PHIUnion.begin(), |
Owen Anderson | 52b1733 | 2008-04-02 03:00:13 +0000 | [diff] [blame] | 494 | E = PHIUnion.end(); I != E; ++I) |
| 495 | if (MRI.getVRegDef(I->first)->getParent() == P->getParent()) |
| 496 | localInterferences.push_back(std::make_pair(I->first, |
| 497 | P->getOperand(0).getReg())); |
| 498 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 499 | // The dominator forest walk may have returned some register pairs whose |
Owen Anderson | 52b1733 | 2008-04-02 03:00:13 +0000 | [diff] [blame] | 500 | // interference cannot be determined from dominator analysis. We now |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 501 | // examine these pairs for local interferences. |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 502 | for (std::vector<std::pair<unsigned, unsigned> >::iterator I = |
| 503 | localInterferences.begin(), E = localInterferences.end(); I != E; ++I) { |
| 504 | std::pair<unsigned, unsigned> p = *I; |
| 505 | |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 506 | MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>(); |
| 507 | |
| 508 | // Determine the block we need to scan and the relationship between |
| 509 | // the two registers |
| 510 | MachineBasicBlock* scan = 0; |
| 511 | unsigned mode = 0; |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 512 | if (MRI.getVRegDef(p.first)->getParent() == |
| 513 | MRI.getVRegDef(p.second)->getParent()) { |
| 514 | scan = MRI.getVRegDef(p.first)->getParent(); |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 515 | mode = 0; // Same block |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 516 | } else if (MDT.dominates(MRI.getVRegDef(p.first)->getParent(), |
| 517 | MRI.getVRegDef(p.second)->getParent())) { |
| 518 | scan = MRI.getVRegDef(p.second)->getParent(); |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 519 | mode = 1; // First dominates second |
| 520 | } else { |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 521 | scan = MRI.getVRegDef(p.first)->getParent(); |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 522 | mode = 2; // Second dominates first |
| 523 | } |
| 524 | |
| 525 | // If there's an interference, we need to insert copies |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 526 | if (interferes(p.first, p.second, scan, LI, mode)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 527 | // Insert copies for First |
| 528 | for (int i = P->getNumOperands() - 1; i >= 2; i-=2) { |
| 529 | if (P->getOperand(i-1).getReg() == p.first) { |
| 530 | unsigned SrcReg = p.first; |
| 531 | MachineBasicBlock* From = P->getOperand(i).getMBB(); |
| 532 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 533 | Waiting[From].insert(std::make_pair(SrcReg, |
| 534 | P->getOperand(0).getReg())); |
| 535 | UsedByAnother.insert(SrcReg); |
| 536 | |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 537 | PHIUnion.erase(SrcReg); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
Owen Anderson | 42f9e96 | 2007-11-13 20:13:24 +0000 | [diff] [blame] | 542 | |
Owen Anderson | 52b1733 | 2008-04-02 03:00:13 +0000 | [diff] [blame] | 543 | // Add the renaming set for this PHI node to our overall renaming information |
Owen Anderson | 0c5714b | 2008-01-08 21:54:52 +0000 | [diff] [blame] | 544 | RenameSets.insert(std::make_pair(P->getOperand(0).getReg(), PHIUnion)); |
Owen Anderson | cae8d8d | 2007-12-22 04:59:10 +0000 | [diff] [blame] | 545 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 546 | // Remember which registers are already renamed, so that we don't try to |
| 547 | // rename them for another PHI node in this block |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 548 | for (std::map<unsigned, MachineBasicBlock*>::iterator I = PHIUnion.begin(), |
Owen Anderson | 0031671 | 2008-03-12 03:13:29 +0000 | [diff] [blame] | 549 | E = PHIUnion.end(); I != E; ++I) |
| 550 | ProcessedNames.insert(I->first); |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame] | 551 | |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 552 | ++P; |
| 553 | } |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Gabor Greif | 2cf36e0 | 2008-03-06 10:51:21 +0000 | [diff] [blame] | 556 | /// processPHIUnion - Take a set of candidate registers to be coalesced when |
Owen Anderson | 965b467 | 2007-12-16 04:07:23 +0000 | [diff] [blame] | 557 | /// decomposing the PHI instruction. Use the DominanceForest to remove the ones |
| 558 | /// that are known to interfere, and flag others that need to be checked for |
| 559 | /// local interferences. |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 560 | void StrongPHIElimination::processPHIUnion(MachineInstr* Inst, |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 561 | std::map<unsigned, MachineBasicBlock*>& PHIUnion, |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 562 | std::vector<StrongPHIElimination::DomForestNode*>& DF, |
| 563 | std::vector<std::pair<unsigned, unsigned> >& locals) { |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 564 | |
| 565 | std::vector<DomForestNode*> worklist(DF.begin(), DF.end()); |
| 566 | SmallPtrSet<DomForestNode*, 4> visited; |
| 567 | |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 568 | // Code is still in SSA form, so we can use MRI::getVRegDef() |
| 569 | MachineRegisterInfo& MRI = Inst->getParent()->getParent()->getRegInfo(); |
| 570 | |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 571 | LiveIntervals& LI = getAnalysis<LiveIntervals>(); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 572 | unsigned DestReg = Inst->getOperand(0).getReg(); |
| 573 | |
Owen Anderson | 965b467 | 2007-12-16 04:07:23 +0000 | [diff] [blame] | 574 | // DF walk on the DomForest |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 575 | while (!worklist.empty()) { |
| 576 | DomForestNode* DFNode = worklist.back(); |
| 577 | |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 578 | visited.insert(DFNode); |
| 579 | |
| 580 | bool inserted = false; |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 581 | for (DomForestNode::iterator CI = DFNode->begin(), CE = DFNode->end(); |
| 582 | CI != CE; ++CI) { |
| 583 | DomForestNode* child = *CI; |
Owen Anderson | 3b48952 | 2008-01-21 22:01:01 +0000 | [diff] [blame] | 584 | |
| 585 | // If the current node is live-out of the defining block of one of its |
Owen Anderson | a6b1926 | 2008-01-21 22:03:00 +0000 | [diff] [blame] | 586 | // children, insert a copy for it. NOTE: The paper actually calls for |
| 587 | // a more elaborate heuristic for determining whether to insert copies |
| 588 | // for the child or the parent. In the interest of simplicity, we're |
| 589 | // just always choosing the parent. |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 590 | if (isLiveOut(DFNode->getReg(), |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 591 | MRI.getVRegDef(child->getReg())->getParent(), LI)) { |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 592 | // Insert copies for parent |
| 593 | for (int i = Inst->getNumOperands() - 1; i >= 2; i-=2) { |
| 594 | if (Inst->getOperand(i-1).getReg() == DFNode->getReg()) { |
Owen Anderson | ed2ffa2 | 2007-12-12 01:25:08 +0000 | [diff] [blame] | 595 | unsigned SrcReg = DFNode->getReg(); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 596 | MachineBasicBlock* From = Inst->getOperand(i).getMBB(); |
| 597 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 598 | Waiting[From].insert(std::make_pair(SrcReg, DestReg)); |
| 599 | UsedByAnother.insert(SrcReg); |
| 600 | |
Owen Anderson | ed2ffa2 | 2007-12-12 01:25:08 +0000 | [diff] [blame] | 601 | PHIUnion.erase(SrcReg); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 602 | } |
| 603 | } |
Owen Anderson | 3b48952 | 2008-01-21 22:01:01 +0000 | [diff] [blame] | 604 | |
| 605 | // If a node is live-in to the defining block of one of its children, but |
| 606 | // not live-out, then we need to scan that block for local interferences. |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 607 | } else if (isLiveIn(DFNode->getReg(), |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 608 | MRI.getVRegDef(child->getReg())->getParent(), LI) || |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 609 | MRI.getVRegDef(DFNode->getReg())->getParent() == |
| 610 | MRI.getVRegDef(child->getReg())->getParent()) { |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 611 | // Add (p, c) to possible local interferences |
| 612 | locals.push_back(std::make_pair(DFNode->getReg(), child->getReg())); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 613 | } |
Owen Anderson | 965b467 | 2007-12-16 04:07:23 +0000 | [diff] [blame] | 614 | |
Owen Anderson | 4ba08ec | 2007-12-13 05:43:37 +0000 | [diff] [blame] | 615 | if (!visited.count(child)) { |
| 616 | worklist.push_back(child); |
| 617 | inserted = true; |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | if (!inserted) worklist.pop_back(); |
| 622 | } |
| 623 | } |
| 624 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 625 | /// ScheduleCopies - Insert copies into predecessor blocks, scheduling |
| 626 | /// them properly so as to avoid the 'lost copy' and the 'virtual swap' |
| 627 | /// problems. |
| 628 | /// |
| 629 | /// Based on "Practical Improvements to the Construction and Destruction |
| 630 | /// of Static Single Assignment Form" by Briggs, et al. |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 631 | void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB, |
| 632 | std::set<unsigned>& pushed) { |
Owen Anderson | 38b4250 | 2008-06-04 17:55:58 +0000 | [diff] [blame] | 633 | // FIXME: This function needs to update LiveIntervals |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 634 | std::map<unsigned, unsigned>& copy_set= Waiting[MBB]; |
| 635 | |
| 636 | std::map<unsigned, unsigned> worklist; |
| 637 | std::map<unsigned, unsigned> map; |
| 638 | |
| 639 | // Setup worklist of initial copies |
| 640 | for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(), |
| 641 | E = copy_set.end(); I != E; ) { |
| 642 | map.insert(std::make_pair(I->first, I->first)); |
| 643 | map.insert(std::make_pair(I->second, I->second)); |
| 644 | |
Owen Anderson | c7c0036 | 2008-03-29 01:58:47 +0000 | [diff] [blame] | 645 | if (!UsedByAnother.count(I->second)) { |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 646 | worklist.insert(*I); |
| 647 | |
| 648 | // Avoid iterator invalidation |
| 649 | unsigned first = I->first; |
| 650 | ++I; |
| 651 | copy_set.erase(first); |
| 652 | } else { |
| 653 | ++I; |
| 654 | } |
| 655 | } |
| 656 | |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 657 | LiveIntervals& LI = getAnalysis<LiveIntervals>(); |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 658 | MachineFunction* MF = MBB->getParent(); |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 659 | MachineRegisterInfo& MRI = MF->getRegInfo(); |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 660 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 661 | |
Owen Anderson | 38b4250 | 2008-06-04 17:55:58 +0000 | [diff] [blame] | 662 | SmallVector<std::pair<unsigned, MachineInstr*>, 4> InsertedPHIDests; |
| 663 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 664 | // Iterate over the worklist, inserting copies |
| 665 | while (!worklist.empty() || !copy_set.empty()) { |
| 666 | while (!worklist.empty()) { |
| 667 | std::pair<unsigned, unsigned> curr = *worklist.begin(); |
| 668 | worklist.erase(curr.first); |
| 669 | |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 670 | const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first); |
| 671 | |
Owen Anderson | eb37ecc | 2008-03-10 07:22:36 +0000 | [diff] [blame] | 672 | if (isLiveOut(curr.second, MBB, LI)) { |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 673 | // Create a temporary |
| 674 | unsigned t = MF->getRegInfo().createVirtualRegister(RC); |
| 675 | |
| 676 | // Insert copy from curr.second to a temporary at |
| 677 | // the Phi defining curr.second |
Owen Anderson | ddd060f | 2008-01-10 01:36:43 +0000 | [diff] [blame] | 678 | MachineBasicBlock::iterator PI = MRI.getVRegDef(curr.second); |
| 679 | TII->copyRegToReg(*PI->getParent(), PI, t, |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 680 | curr.second, RC, RC); |
| 681 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 682 | // Push temporary on Stacks |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 683 | Stacks[curr.second].push_back(t); |
| 684 | |
| 685 | // Insert curr.second in pushed |
| 686 | pushed.insert(curr.second); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | // Insert copy from map[curr.first] to curr.second |
Owen Anderson | 9c2efa8 | 2008-01-10 00:01:41 +0000 | [diff] [blame] | 690 | TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), curr.second, |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 691 | map[curr.first], RC, RC); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 692 | map[curr.first] = curr.second; |
| 693 | |
Owen Anderson | 38b4250 | 2008-06-04 17:55:58 +0000 | [diff] [blame] | 694 | // Push this copy onto InsertedPHICopies so we can |
| 695 | // update LiveIntervals with it. |
| 696 | MachineBasicBlock::iterator MI = MBB->getFirstTerminator(); |
| 697 | InsertedPHIDests.push_back(std::make_pair(curr.second, --MI)); |
| 698 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 699 | // If curr.first is a destination in copy_set... |
| 700 | for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(), |
| 701 | E = copy_set.end(); I != E; ) |
| 702 | if (curr.first == I->second) { |
| 703 | std::pair<unsigned, unsigned> temp = *I; |
| 704 | |
| 705 | // Avoid iterator invalidation |
| 706 | ++I; |
| 707 | copy_set.erase(temp.first); |
| 708 | worklist.insert(temp); |
| 709 | |
| 710 | break; |
| 711 | } else { |
| 712 | ++I; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | if (!copy_set.empty()) { |
| 717 | std::pair<unsigned, unsigned> curr = *copy_set.begin(); |
| 718 | copy_set.erase(curr.first); |
| 719 | |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 720 | const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first); |
| 721 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 722 | // Insert a copy from dest to a new temporary t at the end of b |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 723 | unsigned t = MF->getRegInfo().createVirtualRegister(RC); |
Owen Anderson | 9c2efa8 | 2008-01-10 00:01:41 +0000 | [diff] [blame] | 724 | TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), t, |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 725 | curr.second, RC, RC); |
| 726 | map[curr.second] = t; |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 727 | |
| 728 | worklist.insert(curr); |
| 729 | } |
| 730 | } |
Owen Anderson | 38b4250 | 2008-06-04 17:55:58 +0000 | [diff] [blame] | 731 | |
| 732 | // Renumber the instructions so that we can perform the index computations |
| 733 | // needed to create new live intervals. |
| 734 | LI.computeNumbering(); |
| 735 | |
| 736 | // For copies that we inserted at the ends of predecessors, we construct |
| 737 | // live intervals. This is pretty easy, since we know that the destination |
| 738 | // register cannot have be in live at that point previously. We just have |
| 739 | // to make sure that, for registers that serve as inputs to more than one |
| 740 | // PHI, we don't create multiple overlapping live intervals. |
| 741 | std::set<unsigned> RegHandled; |
| 742 | for (SmallVector<std::pair<unsigned, MachineInstr*>, 4>::iterator I = |
Owen Anderson | a9efb26 | 2008-06-05 17:22:53 +0000 | [diff] [blame] | 743 | InsertedPHIDests.begin(), E = InsertedPHIDests.end(); I != E; ++I) |
Owen Anderson | a279a89 | 2008-07-25 23:38:08 +0000 | [diff] [blame] | 744 | if (RegHandled.insert(I->first).second) |
Owen Anderson | a9efb26 | 2008-06-05 17:22:53 +0000 | [diff] [blame] | 745 | LI.addLiveRangeToEndOfBlock(I->first, I->second); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 748 | /// InsertCopies - insert copies into MBB and all of its successors |
Owen Anderson | 719fef6 | 2008-01-09 10:32:30 +0000 | [diff] [blame] | 749 | void StrongPHIElimination::InsertCopies(MachineBasicBlock* MBB, |
Owen Anderson | e46611e | 2008-03-24 04:11:27 +0000 | [diff] [blame] | 750 | SmallPtrSet<MachineBasicBlock*, 16>& visited) { |
Owen Anderson | 719fef6 | 2008-01-09 10:32:30 +0000 | [diff] [blame] | 751 | visited.insert(MBB); |
| 752 | |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 753 | std::set<unsigned> pushed; |
| 754 | |
| 755 | // Rewrite register uses from Stacks |
| 756 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 757 | I != E; ++I) |
| 758 | for (unsigned i = 0; i < I->getNumOperands(); ++i) |
| 759 | if (I->getOperand(i).isRegister() && |
| 760 | Stacks[I->getOperand(i).getReg()].size()) { |
| 761 | I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back()); |
| 762 | } |
| 763 | |
| 764 | // Schedule the copies for this block |
| 765 | ScheduleCopies(MBB, pushed); |
| 766 | |
| 767 | // Recur to our successors |
| 768 | for (GraphTraits<MachineBasicBlock*>::ChildIteratorType I = |
| 769 | GraphTraits<MachineBasicBlock*>::child_begin(MBB), E = |
| 770 | GraphTraits<MachineBasicBlock*>::child_end(MBB); I != E; ++I) |
Owen Anderson | 719fef6 | 2008-01-09 10:32:30 +0000 | [diff] [blame] | 771 | if (!visited.count(*I)) |
| 772 | InsertCopies(*I, visited); |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 773 | |
| 774 | // As we exit this block, pop the names we pushed while processing it |
| 775 | for (std::set<unsigned>::iterator I = pushed.begin(), |
| 776 | E = pushed.end(); I != E; ++I) |
| 777 | Stacks[*I].pop_back(); |
| 778 | } |
| 779 | |
Owen Anderson | 0031671 | 2008-03-12 03:13:29 +0000 | [diff] [blame] | 780 | void StrongPHIElimination::mergeLiveIntervals(unsigned primary, |
Owen Anderson | e46611e | 2008-03-24 04:11:27 +0000 | [diff] [blame] | 781 | unsigned secondary, |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 782 | MachineBasicBlock* pred) { |
Owen Anderson | 755ebab | 2008-03-17 06:08:26 +0000 | [diff] [blame] | 783 | |
| 784 | LiveIntervals& LI = getAnalysis<LiveIntervals>(); |
| 785 | LiveInterval& LHS = LI.getOrCreateInterval(primary); |
| 786 | LiveInterval& RHS = LI.getOrCreateInterval(secondary); |
| 787 | |
Owen Anderson | a9efb26 | 2008-06-05 17:22:53 +0000 | [diff] [blame] | 788 | LI.computeNumbering(); |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 789 | const LiveRange* RangeMergingIn = |
| 790 | RHS.getLiveRangeContaining(LI.getMBBEndIdx(pred)); |
Owen Anderson | 83ea1f8 | 2008-07-29 21:17:08 +0000 | [diff] [blame] | 791 | VNInfo* RHSVN = RangeMergingIn->valno; |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 792 | VNInfo* NewVN = LHS.getNextValue(RangeMergingIn->valno->def, |
| 793 | RangeMergingIn->valno->copy, |
| 794 | LI.getVNInfoAllocator()); |
Owen Anderson | b9fb8d1 | 2008-07-30 00:21:16 +0000 | [diff] [blame] | 795 | |
| 796 | // If we discover that a live range was defined by a two-addr |
| 797 | // instruction, we need to merge over the input as well, even if |
| 798 | // it has a different VNInfo. |
| 799 | SmallPtrSet<VNInfo*, 4> MergedVNs; |
| 800 | MergedVNs.insert(RHSVN); |
| 801 | |
| 802 | DenseMap<VNInfo*, VNInfo*> VNMap; |
| 803 | VNMap.insert(std::make_pair(RangeMergingIn->valno, NewVN)); |
| 804 | |
Owen Anderson | d723f72 | 2008-07-30 18:27:35 +0000 | [diff] [blame] | 805 | // Find all VNs that are the inputs to two-address instructiosn |
| 806 | // chaining upwards from the VN we're trying to merge. |
| 807 | bool addedVN = true; |
| 808 | while (addedVN) { |
| 809 | addedVN = false; |
| 810 | unsigned defIndex = RHSVN->def; |
| 811 | |
| 812 | if (defIndex != ~0U) { |
| 813 | MachineInstr* instr = LI.getInstructionFromIndex(defIndex); |
Owen Anderson | 83ea1f8 | 2008-07-29 21:17:08 +0000 | [diff] [blame] | 814 | |
Owen Anderson | d723f72 | 2008-07-30 18:27:35 +0000 | [diff] [blame] | 815 | for (unsigned i = 0; i < instr->getNumOperands(); ++i) { |
| 816 | if (instr->getOperand(i).isReg() && |
| 817 | instr->getOperand(i).getReg() == secondary) |
| 818 | if (instr->isRegReDefinedByTwoAddr(secondary, i)) { |
| 819 | RHSVN = RHS.getLiveRangeContaining(defIndex-1)->valno; |
| 820 | addedVN = true; |
| 821 | |
| 822 | VNInfo* NextVN = LHS.getNextValue(RHSVN->def, |
| 823 | RHSVN->copy, |
| 824 | LI.getVNInfoAllocator()); |
| 825 | VNMap.insert(std::make_pair(RHSVN, NextVN)); |
| 826 | |
| 827 | break; |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // Merge VNs from RHS into LHS using the mapping we computed above. |
| 834 | for (DenseMap<VNInfo*, VNInfo*>::iterator VI = VNMap.begin(), |
| 835 | VE = VNMap.end(); VI != VE; ++VI) { |
| 836 | LHS.MergeValueInAsValue(RHS, VI->first, VI->second); |
| 837 | RHS.removeValNo(VI->first); |
Owen Anderson | b9fb8d1 | 2008-07-30 00:21:16 +0000 | [diff] [blame] | 838 | } |
Owen Anderson | 55c6435 | 2008-07-25 21:08:41 +0000 | [diff] [blame] | 839 | |
Owen Anderson | 83ea1f8 | 2008-07-29 21:17:08 +0000 | [diff] [blame] | 840 | if (RHS.begin() == RHS.end()) |
Owen Anderson | 55c6435 | 2008-07-25 21:08:41 +0000 | [diff] [blame] | 841 | LI.removeInterval(RHS.reg); |
Owen Anderson | 0031671 | 2008-03-12 03:13:29 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 844 | bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) { |
Owen Anderson | c7c0036 | 2008-03-29 01:58:47 +0000 | [diff] [blame] | 845 | LiveIntervals& LI = getAnalysis<LiveIntervals>(); |
| 846 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 847 | // Compute DFS numbers of each block |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 848 | computeDFS(Fn); |
| 849 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 850 | // Determine which phi node operands need copies |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 851 | for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 852 | if (!I->empty() && |
| 853 | I->begin()->getOpcode() == TargetInstrInfo::PHI) |
| 854 | processBlock(I); |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 855 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 856 | // Insert copies |
Owen Anderson | a9efb26 | 2008-06-05 17:22:53 +0000 | [diff] [blame] | 857 | // FIXME: This process should probably preserve LiveIntervals |
Owen Anderson | e46611e | 2008-03-24 04:11:27 +0000 | [diff] [blame] | 858 | SmallPtrSet<MachineBasicBlock*, 16> visited; |
Owen Anderson | 719fef6 | 2008-01-09 10:32:30 +0000 | [diff] [blame] | 859 | InsertCopies(Fn.begin(), visited); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 860 | |
Owen Anderson | 0c5714b | 2008-01-08 21:54:52 +0000 | [diff] [blame] | 861 | // Perform renaming |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 862 | typedef std::map<unsigned, std::map<unsigned, MachineBasicBlock*> > |
| 863 | RenameSetType; |
Owen Anderson | 0c5714b | 2008-01-08 21:54:52 +0000 | [diff] [blame] | 864 | for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end(); |
| 865 | I != E; ++I) |
Owen Anderson | c12417e | 2008-07-24 17:12:16 +0000 | [diff] [blame] | 866 | for (std::map<unsigned, MachineBasicBlock*>::iterator SI = |
| 867 | I->second.begin(), SE = I->second.end(); SI != SE; ++SI) { |
Owen Anderson | 0031671 | 2008-03-12 03:13:29 +0000 | [diff] [blame] | 868 | mergeLiveIntervals(I->first, SI->first, SI->second); |
| 869 | Fn.getRegInfo().replaceRegWith(SI->first, I->first); |
| 870 | } |
Owen Anderson | 0c5714b | 2008-01-08 21:54:52 +0000 | [diff] [blame] | 871 | |
| 872 | // FIXME: Insert last-minute copies |
| 873 | |
| 874 | // Remove PHIs |
Owen Anderson | 97ca75e | 2008-01-22 23:58:54 +0000 | [diff] [blame] | 875 | std::vector<MachineInstr*> phis; |
| 876 | for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { |
Owen Anderson | 0c5714b | 2008-01-08 21:54:52 +0000 | [diff] [blame] | 877 | for (MachineBasicBlock::iterator BI = I->begin(), BE = I->end(); |
| 878 | BI != BE; ++BI) |
| 879 | if (BI->getOpcode() == TargetInstrInfo::PHI) |
Owen Anderson | 97ca75e | 2008-01-22 23:58:54 +0000 | [diff] [blame] | 880 | phis.push_back(BI); |
| 881 | } |
| 882 | |
| 883 | for (std::vector<MachineInstr*>::iterator I = phis.begin(), E = phis.end(); |
Owen Anderson | c7c0036 | 2008-03-29 01:58:47 +0000 | [diff] [blame] | 884 | I != E; ) { |
| 885 | MachineInstr* PInstr = *(I++); |
| 886 | |
Owen Anderson | e7b8205 | 2008-08-05 20:51:26 +0000 | [diff] [blame^] | 887 | // Don't do live interval updating for dead PHIs. |
| 888 | if (!PInstr->registerDefIsDead(PInstr->getOperand(0).getReg())) { |
| 889 | // Trim live intervals of input registers. They are no longer live into |
| 890 | // this block. |
| 891 | for (unsigned i = 1; i < PInstr->getNumOperands(); i += 2) { |
| 892 | unsigned reg = PInstr->getOperand(i).getReg(); |
| 893 | MachineBasicBlock* MBB = PInstr->getOperand(i+1).getMBB(); |
| 894 | LiveInterval& InputI = LI.getInterval(reg); |
| 895 | if (MBB != PInstr->getParent() && |
| 896 | InputI.liveAt(LI.getMBBStartIdx(PInstr->getParent()))) |
| 897 | InputI.removeRange(LI.getMBBStartIdx(PInstr->getParent()), |
| 898 | LI.getInstructionIndex(PInstr), |
| 899 | true); |
Owen Anderson | d382f8a | 2008-03-26 03:03:23 +0000 | [diff] [blame] | 900 | } |
Owen Anderson | e7b8205 | 2008-08-05 20:51:26 +0000 | [diff] [blame^] | 901 | |
| 902 | // If this is a dead PHI node, then remove it from LiveIntervals. |
| 903 | unsigned DestReg = PInstr->getOperand(0).getReg(); |
| 904 | LiveInterval& PI = LI.getInterval(DestReg); |
| 905 | if (PInstr->registerDefIsDead(DestReg)) { |
| 906 | if (PI.containsOneValue()) { |
| 907 | LI.removeInterval(DestReg); |
| 908 | } else { |
| 909 | unsigned idx = LI.getDefIndex(LI.getInstructionIndex(PInstr)); |
| 910 | PI.removeRange(*PI.getLiveRangeContaining(idx), true); |
| 911 | } |
| 912 | } else { |
| 913 | // If the PHI is not dead, then the valno defined by the PHI |
| 914 | // now has an unknown def. |
| 915 | unsigned idx = LI.getDefIndex(LI.getInstructionIndex(PInstr)); |
| 916 | const LiveRange* PLR = PI.getLiveRangeContaining(idx); |
| 917 | PLR->valno->def = ~0U; |
| 918 | LiveRange R (LI.getMBBStartIdx(PInstr->getParent()), |
| 919 | PLR->start, PLR->valno); |
| 920 | PI.addRange(R); |
| 921 | } |
Owen Anderson | d382f8a | 2008-03-26 03:03:23 +0000 | [diff] [blame] | 922 | } |
Owen Anderson | 59df878 | 2008-04-02 02:12:45 +0000 | [diff] [blame] | 923 | |
Owen Anderson | c7c0036 | 2008-03-29 01:58:47 +0000 | [diff] [blame] | 924 | LI.RemoveMachineInstrFromMaps(PInstr); |
| 925 | PInstr->eraseFromParent(); |
Owen Anderson | e46611e | 2008-03-24 04:11:27 +0000 | [diff] [blame] | 926 | } |
Owen Anderson | cae8d8d | 2007-12-22 04:59:10 +0000 | [diff] [blame] | 927 | |
Owen Anderson | 3947e4d | 2008-05-30 18:38:26 +0000 | [diff] [blame] | 928 | LI.computeNumbering(); |
| 929 | |
Owen Anderson | c7c0036 | 2008-03-29 01:58:47 +0000 | [diff] [blame] | 930 | return true; |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 931 | } |