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 | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/LiveVariables.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 | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetInstrInfo.h" |
| 30 | #include "llvm/Target/TargetMachine.h" |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DepthFirstIterator.h" |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
| 33 | #include "llvm/Support/Compiler.h" |
| 34 | using namespace llvm; |
| 35 | |
| 36 | |
| 37 | namespace { |
| 38 | struct VISIBILITY_HIDDEN StrongPHIElimination : public MachineFunctionPass { |
| 39 | static char ID; // Pass identification, replacement for typeid |
| 40 | StrongPHIElimination() : MachineFunctionPass((intptr_t)&ID) {} |
| 41 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 42 | // Waiting stores, for each MBB, the set of copies that need to |
| 43 | // be inserted into that MBB |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 44 | DenseMap<MachineBasicBlock*, |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 45 | std::map<unsigned, unsigned> > Waiting; |
| 46 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 47 | // Stacks holds the renaming stack for each register |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 48 | std::map<unsigned, std::vector<unsigned> > Stacks; |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 49 | |
| 50 | // Registers in UsedByAnother are PHI nodes that are themselves |
| 51 | // used as operands to another another PHI node |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 52 | std::set<unsigned> UsedByAnother; |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 53 | |
| 54 | // RenameSets are the sets of operands to a PHI (the defining instruction |
| 55 | // of the key) that can be renamed without copies |
Owen Anderson | 0c5714b | 2008-01-08 21:54:52 +0000 | [diff] [blame] | 56 | std::map<unsigned, std::set<unsigned> > RenameSets; |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 57 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 58 | // Store the DFS-in number of each block |
| 59 | DenseMap<MachineBasicBlock*, unsigned> preorder; |
| 60 | |
| 61 | // Store the DFS-out number of each block |
| 62 | DenseMap<MachineBasicBlock*, unsigned> maxpreorder; |
| 63 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 64 | bool runOnMachineFunction(MachineFunction &Fn); |
| 65 | |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 66 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 67 | AU.addRequired<MachineDominatorTree>(); |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 68 | AU.addRequired<LiveVariables>(); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 69 | MachineFunctionPass::getAnalysisUsage(AU); |
| 70 | } |
| 71 | |
| 72 | virtual void releaseMemory() { |
| 73 | preorder.clear(); |
| 74 | maxpreorder.clear(); |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 75 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 76 | Waiting.clear(); |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 77 | Stacks.clear(); |
| 78 | UsedByAnother.clear(); |
| 79 | RenameSets.clear(); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | private: |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 83 | |
| 84 | /// DomForestNode - Represents a node in the "dominator forest". This is |
| 85 | /// a forest in which the nodes represent registers and the edges |
| 86 | /// represent a dominance relation in the block defining those registers. |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 87 | struct DomForestNode { |
| 88 | private: |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 89 | // Store references to our children |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 90 | std::vector<DomForestNode*> children; |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 91 | // The register we represent |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 92 | unsigned reg; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 93 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 94 | // Add another node as our child |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 95 | void addChild(DomForestNode* DFN) { children.push_back(DFN); } |
| 96 | |
| 97 | public: |
| 98 | typedef std::vector<DomForestNode*>::iterator iterator; |
| 99 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 100 | // Create a DomForestNode by providing the register it represents, and |
| 101 | // the node to be its parent. The virtual root node has register 0 |
| 102 | // and a null parent. |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 103 | DomForestNode(unsigned r, DomForestNode* parent) : reg(r) { |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 104 | if (parent) |
| 105 | parent->addChild(this); |
| 106 | } |
| 107 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 108 | ~DomForestNode() { |
| 109 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 110 | delete *I; |
| 111 | } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 112 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 113 | /// getReg - Return the regiser that this node represents |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 114 | inline unsigned getReg() { return reg; } |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 115 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 116 | // Provide iterator access to our children |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 117 | inline DomForestNode::iterator begin() { return children.begin(); } |
| 118 | inline DomForestNode::iterator end() { return children.end(); } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 121 | void computeDFS(MachineFunction& MF); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 122 | void processBlock(MachineBasicBlock* MBB); |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 123 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 124 | std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 125 | void processPHIUnion(MachineInstr* Inst, |
| 126 | std::set<unsigned>& PHIUnion, |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 127 | std::vector<StrongPHIElimination::DomForestNode*>& DF, |
| 128 | std::vector<std::pair<unsigned, unsigned> >& locals); |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 129 | void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed); |
Owen Anderson | 719fef6 | 2008-01-09 10:32:30 +0000 | [diff] [blame] | 130 | void InsertCopies(MachineBasicBlock* MBB, std::set<MachineBasicBlock*>& v); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | char StrongPHIElimination::ID = 0; |
| 134 | RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination", |
| 135 | "Eliminate PHI nodes for register allocation, intelligently"); |
| 136 | } |
| 137 | |
| 138 | const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo(); |
| 139 | |
| 140 | /// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree |
| 141 | /// of the given MachineFunction. These numbers are then used in other parts |
| 142 | /// of the PHI elimination process. |
| 143 | void StrongPHIElimination::computeDFS(MachineFunction& MF) { |
| 144 | SmallPtrSet<MachineDomTreeNode*, 8> frontier; |
| 145 | SmallPtrSet<MachineDomTreeNode*, 8> visited; |
| 146 | |
| 147 | unsigned time = 0; |
| 148 | |
| 149 | MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>(); |
| 150 | |
| 151 | MachineDomTreeNode* node = DT.getRootNode(); |
| 152 | |
| 153 | std::vector<MachineDomTreeNode*> worklist; |
| 154 | worklist.push_back(node); |
| 155 | |
| 156 | while (!worklist.empty()) { |
| 157 | MachineDomTreeNode* currNode = worklist.back(); |
| 158 | |
| 159 | if (!frontier.count(currNode)) { |
| 160 | frontier.insert(currNode); |
| 161 | ++time; |
| 162 | preorder.insert(std::make_pair(currNode->getBlock(), time)); |
| 163 | } |
| 164 | |
| 165 | bool inserted = false; |
| 166 | for (MachineDomTreeNode::iterator I = node->begin(), E = node->end(); |
| 167 | I != E; ++I) |
| 168 | if (!frontier.count(*I) && !visited.count(*I)) { |
| 169 | worklist.push_back(*I); |
| 170 | inserted = true; |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | if (!inserted) { |
| 175 | frontier.erase(currNode); |
| 176 | visited.insert(currNode); |
| 177 | maxpreorder.insert(std::make_pair(currNode->getBlock(), time)); |
| 178 | |
| 179 | worklist.pop_back(); |
| 180 | } |
| 181 | } |
Duncan Sands | 1bd3271 | 2007-10-31 08:49:24 +0000 | [diff] [blame] | 182 | } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 183 | |
Owen Anderson | 8b96b9f | 2007-11-06 05:26:02 +0000 | [diff] [blame] | 184 | /// PreorderSorter - a helper class that is used to sort registers |
| 185 | /// according to the preorder number of their defining blocks |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 186 | class PreorderSorter { |
| 187 | private: |
| 188 | DenseMap<MachineBasicBlock*, unsigned>& preorder; |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 189 | LiveVariables& LV; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 190 | |
| 191 | public: |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 192 | PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p, |
| 193 | LiveVariables& L) : preorder(p), LV(L) { } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 194 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 195 | bool operator()(unsigned A, unsigned B) { |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 196 | if (A == B) |
| 197 | return false; |
| 198 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 199 | MachineBasicBlock* ABlock = LV.getVarInfo(A).DefInst->getParent(); |
| 200 | MachineBasicBlock* BBlock = LV.getVarInfo(A).DefInst->getParent(); |
| 201 | |
| 202 | if (preorder[ABlock] < preorder[BBlock]) |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 203 | return true; |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 204 | else if (preorder[ABlock] > preorder[BBlock]) |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 205 | return false; |
| 206 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 207 | return false; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 208 | } |
| 209 | }; |
| 210 | |
Owen Anderson | 8b96b9f | 2007-11-06 05:26:02 +0000 | [diff] [blame] | 211 | /// computeDomForest - compute the subforest of the DomTree corresponding |
| 212 | /// to the defining blocks of the registers in question |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 213 | std::vector<StrongPHIElimination::DomForestNode*> |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 214 | StrongPHIElimination::computeDomForest(std::set<unsigned>& regs) { |
| 215 | LiveVariables& LV = getAnalysis<LiveVariables>(); |
| 216 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 217 | // Begin by creating a virtual root node, since the actual results |
| 218 | // 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] | 219 | DomForestNode* VirtualRoot = new DomForestNode(0, 0); |
| 220 | maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL)); |
| 221 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 222 | // Populate a worklist with the registers |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 223 | std::vector<unsigned> worklist; |
| 224 | worklist.reserve(regs.size()); |
| 225 | for (std::set<unsigned>::iterator I = regs.begin(), E = regs.end(); |
| 226 | I != E; ++I) |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 227 | worklist.push_back(*I); |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 228 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 229 | // Sort the registers by the DFS-in number of their defining block |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 230 | PreorderSorter PS(preorder, LV); |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 231 | std::sort(worklist.begin(), worklist.end(), PS); |
| 232 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 233 | // 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] | 234 | DomForestNode* CurrentParent = VirtualRoot; |
| 235 | std::vector<DomForestNode*> stack; |
| 236 | stack.push_back(VirtualRoot); |
| 237 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 238 | // Iterate over all the registers in the previously computed order |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 239 | for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end(); |
| 240 | I != E; ++I) { |
| 241 | unsigned pre = preorder[LV.getVarInfo(*I).DefInst->getParent()]; |
Owen Anderson | cb7d949 | 2008-01-09 06:19:05 +0000 | [diff] [blame] | 242 | MachineBasicBlock* parentBlock = CurrentParent->getReg() ? |
| 243 | LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent() : |
| 244 | 0; |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 245 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 246 | // If the DFS-in number of the register is greater than the DFS-out number |
| 247 | // 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] | 248 | while (pre > maxpreorder[parentBlock]) { |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 249 | stack.pop_back(); |
| 250 | CurrentParent = stack.back(); |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 251 | |
Owen Anderson | 864e3a3 | 2008-01-09 10:41:39 +0000 | [diff] [blame] | 252 | parentBlock = CurrentParent->getReg() ? |
| 253 | LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent() : |
| 254 | 0; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 257 | // Now that we've found the appropriate parent, create a DomForestNode for |
| 258 | // this register and attach it to the forest |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 259 | DomForestNode* child = new DomForestNode(*I, CurrentParent); |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 260 | |
| 261 | // Push this new node on the "current parent" stack |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 262 | stack.push_back(child); |
| 263 | CurrentParent = child; |
| 264 | } |
| 265 | |
Owen Anderson | ec1213f | 2008-01-09 22:40:54 +0000 | [diff] [blame] | 266 | // Return a vector containing the children of the virtual root node |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 267 | std::vector<DomForestNode*> ret; |
| 268 | ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end()); |
| 269 | return ret; |
| 270 | } |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 271 | |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 272 | /// isLiveIn - helper method that determines, from a VarInfo, if a register |
| 273 | /// is live into a block |
Owen Anderson | 9e54920 | 2008-01-07 21:30:40 +0000 | [diff] [blame] | 274 | static bool isLiveIn(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) { |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 275 | if (V.AliveBlocks.test(MBB->getNumber())) |
| 276 | return true; |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 277 | |
Owen Anderson | 14b3fb7 | 2007-11-08 01:32:45 +0000 | [diff] [blame] | 278 | if (V.DefInst->getParent() != MBB && |
| 279 | V.UsedBlocks.test(MBB->getNumber())) |
| 280 | return true; |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 281 | |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | /// isLiveOut - help method that determines, from a VarInfo, if a register is |
| 286 | /// live out of a block. |
Owen Anderson | 9e54920 | 2008-01-07 21:30:40 +0000 | [diff] [blame] | 287 | static bool isLiveOut(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) { |
Owen Anderson | 14b3fb7 | 2007-11-08 01:32:45 +0000 | [diff] [blame] | 288 | if (MBB == V.DefInst->getParent() || |
| 289 | V.UsedBlocks.test(MBB->getNumber())) { |
| 290 | for (std::vector<MachineInstr*>::iterator I = V.Kills.begin(), |
| 291 | E = V.Kills.end(); I != E; ++I) |
| 292 | if ((*I)->getParent() == MBB) |
| 293 | return false; |
| 294 | |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 295 | return true; |
Owen Anderson | 14b3fb7 | 2007-11-08 01:32:45 +0000 | [diff] [blame] | 296 | } |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 297 | |
| 298 | return false; |
| 299 | } |
| 300 | |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 301 | /// interferes - checks for local interferences by scanning a block. The only |
| 302 | /// trick parameter is 'mode' which tells it the relationship of the two |
| 303 | /// registers. 0 - defined in the same block, 1 - first properly dominates |
| 304 | /// second, 2 - second properly dominates first |
Owen Anderson | b199cbe | 2008-01-10 00:33:11 +0000 | [diff] [blame] | 305 | static bool interferes(unsigned a, unsigned b, MachineBasicBlock* scan, |
| 306 | LiveVariables& LV, unsigned mode) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 307 | MachineInstr* def = 0; |
| 308 | MachineInstr* kill = 0; |
| 309 | |
Owen Anderson | b199cbe | 2008-01-10 00:33:11 +0000 | [diff] [blame] | 310 | LiveVariables::VarInfo& First = LV.getVarInfo(a); |
| 311 | LiveVariables::VarInfo& Second = LV.getVarInfo(b); |
| 312 | |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 313 | bool interference = false; |
| 314 | |
| 315 | // Wallk the block, checking for interferences |
| 316 | for (MachineBasicBlock::iterator MBI = scan->begin(), MBE = scan->end(); |
| 317 | MBI != MBE; ++MBI) { |
| 318 | MachineInstr* curr = MBI; |
| 319 | |
| 320 | // Same defining block... |
| 321 | if (mode == 0) { |
| 322 | if (curr == First.DefInst) { |
| 323 | // If we find our first DefInst, save it |
| 324 | if (!def) { |
| 325 | def = curr; |
| 326 | // If there's already an unkilled DefInst, then |
| 327 | // this is an interference |
| 328 | } else if (!kill) { |
| 329 | interference = true; |
| 330 | break; |
| 331 | // If there's a DefInst followed by a KillInst, then |
| 332 | // they can't interfere |
| 333 | } else { |
| 334 | interference = false; |
| 335 | break; |
| 336 | } |
| 337 | // Symmetric with the above |
| 338 | } else if (curr == Second.DefInst ) { |
| 339 | if (!def) { |
| 340 | def = curr; |
| 341 | } else if (!kill) { |
| 342 | interference = true; |
| 343 | break; |
| 344 | } else { |
| 345 | interference = false; |
| 346 | break; |
| 347 | } |
| 348 | // Store KillInsts if they match up with the DefInst |
Owen Anderson | b199cbe | 2008-01-10 00:33:11 +0000 | [diff] [blame] | 349 | } else if (LV.KillsRegister(curr, a)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 350 | if (def == First.DefInst) { |
| 351 | kill = curr; |
Owen Anderson | b199cbe | 2008-01-10 00:33:11 +0000 | [diff] [blame] | 352 | } else if (LV.KillsRegister(curr, b)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 353 | if (def == Second.DefInst) { |
| 354 | kill = curr; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | // First properly dominates second... |
| 359 | } else if (mode == 1) { |
| 360 | if (curr == Second.DefInst) { |
| 361 | // DefInst of second without kill of first is an interference |
| 362 | if (!kill) { |
| 363 | interference = true; |
| 364 | break; |
| 365 | // DefInst after a kill is a non-interference |
| 366 | } else { |
| 367 | interference = false; |
| 368 | break; |
| 369 | } |
| 370 | // Save KillInsts of First |
Owen Anderson | b199cbe | 2008-01-10 00:33:11 +0000 | [diff] [blame] | 371 | } else if (LV.KillsRegister(curr, a)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 372 | kill = curr; |
| 373 | } |
| 374 | // Symmetric with the above |
| 375 | } else if (mode == 2) { |
| 376 | if (curr == First.DefInst) { |
| 377 | if (!kill) { |
| 378 | interference = true; |
| 379 | break; |
| 380 | } else { |
| 381 | interference = false; |
| 382 | break; |
| 383 | } |
Owen Anderson | b199cbe | 2008-01-10 00:33:11 +0000 | [diff] [blame] | 384 | } else if (LV.KillsRegister(curr, b)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 385 | kill = curr; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return interference; |
| 391 | } |
| 392 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 393 | /// processBlock - Determine how to break up PHIs in the current block. Each |
| 394 | /// PHI is broken up by some combination of renaming its operands and inserting |
| 395 | /// copies. This method is responsible for determining which operands receive |
| 396 | /// which treatment. |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 397 | void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) { |
| 398 | LiveVariables& LV = getAnalysis<LiveVariables>(); |
| 399 | |
| 400 | // Holds names that have been added to a set in any PHI within this block |
| 401 | // before the current one. |
| 402 | std::set<unsigned> ProcessedNames; |
| 403 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 404 | // Iterate over all the PHI nodes in this block |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 405 | MachineBasicBlock::iterator P = MBB->begin(); |
Owen Anderson | 864e3a3 | 2008-01-09 10:41:39 +0000 | [diff] [blame] | 406 | while (P != MBB->end() && P->getOpcode() == TargetInstrInfo::PHI) { |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 407 | LiveVariables::VarInfo& PHIInfo = LV.getVarInfo(P->getOperand(0).getReg()); |
| 408 | |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 409 | unsigned DestReg = P->getOperand(0).getReg(); |
| 410 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 411 | // PHIUnion is the set of incoming registers to the PHI node that |
| 412 | // are going to be renames rather than having copies inserted. This set |
| 413 | // is refinded over the course of this function. UnionedBlocks is the set |
| 414 | // of corresponding MBBs. |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 415 | std::set<unsigned> PHIUnion; |
| 416 | std::set<MachineBasicBlock*> UnionedBlocks; |
| 417 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 418 | // Iterate over the operands of the PHI node |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 419 | for (int i = P->getNumOperands() - 1; i >= 2; i-=2) { |
| 420 | unsigned SrcReg = P->getOperand(i-1).getReg(); |
| 421 | LiveVariables::VarInfo& SrcInfo = LV.getVarInfo(SrcReg); |
| 422 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 423 | // Check for trivial interferences via liveness information, allowing us |
| 424 | // to avoid extra work later. Any registers that interfere cannot both |
| 425 | // be in the renaming set, so choose one and add copies for it instead. |
| 426 | // The conditions are: |
| 427 | // 1) if the operand is live into the PHI node's block OR |
| 428 | // 2) if the PHI node is live out of the operand's defining block OR |
| 429 | // 3) if the operand is itself a PHI node and the original PHI is |
| 430 | // live into the operand's defining block OR |
| 431 | // 4) if the operand is already being renamed for another PHI node |
| 432 | // in this block OR |
| 433 | // 5) if any two operands are defined in the same block, insert copies |
| 434 | // for one of them |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 435 | if (isLiveIn(SrcInfo, P->getParent()) || |
| 436 | isLiveOut(PHIInfo, SrcInfo.DefInst->getParent()) || |
| 437 | ( PHIInfo.DefInst->getOpcode() == TargetInstrInfo::PHI && |
| 438 | isLiveIn(PHIInfo, SrcInfo.DefInst->getParent()) ) || |
| 439 | ProcessedNames.count(SrcReg) || |
| 440 | UnionedBlocks.count(SrcInfo.DefInst->getParent())) { |
| 441 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 442 | // Add a copy for the selected register |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 443 | MachineBasicBlock* From = P->getOperand(i).getMBB(); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 444 | Waiting[From].insert(std::make_pair(SrcReg, DestReg)); |
| 445 | UsedByAnother.insert(SrcReg); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 446 | } else { |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 447 | // Otherwise, add it to the renaming set |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 448 | PHIUnion.insert(SrcReg); |
| 449 | UnionedBlocks.insert(SrcInfo.DefInst->getParent()); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 450 | } |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 453 | // Compute the dominator forest for the renaming set. This is a forest |
| 454 | // where the nodes are the registers and the edges represent dominance |
| 455 | // relations between the defining blocks of the registers |
Owen Anderson | 42f9e96 | 2007-11-13 20:13:24 +0000 | [diff] [blame] | 456 | std::vector<StrongPHIElimination::DomForestNode*> DF = |
| 457 | computeDomForest(PHIUnion); |
| 458 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 459 | // Walk DomForest to resolve interferences at an inter-block level. This |
| 460 | // will remove registers from the renaming set (and insert copies for them) |
| 461 | // if interferences are found. |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 462 | std::vector<std::pair<unsigned, unsigned> > localInterferences; |
| 463 | processPHIUnion(P, PHIUnion, DF, localInterferences); |
| 464 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 465 | // The dominator forest walk may have returned some register pairs whose |
| 466 | // interference cannot be determines from dominator analysis. We now |
| 467 | // examine these pairs for local interferences. |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 468 | for (std::vector<std::pair<unsigned, unsigned> >::iterator I = |
| 469 | localInterferences.begin(), E = localInterferences.end(); I != E; ++I) { |
| 470 | std::pair<unsigned, unsigned> p = *I; |
| 471 | |
| 472 | LiveVariables::VarInfo& FirstInfo = LV.getVarInfo(p.first); |
| 473 | LiveVariables::VarInfo& SecondInfo = LV.getVarInfo(p.second); |
| 474 | |
| 475 | MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>(); |
| 476 | |
| 477 | // Determine the block we need to scan and the relationship between |
| 478 | // the two registers |
| 479 | MachineBasicBlock* scan = 0; |
| 480 | unsigned mode = 0; |
| 481 | if (FirstInfo.DefInst->getParent() == SecondInfo.DefInst->getParent()) { |
| 482 | scan = FirstInfo.DefInst->getParent(); |
| 483 | mode = 0; // Same block |
| 484 | } else if (MDT.dominates(FirstInfo.DefInst->getParent(), |
| 485 | SecondInfo.DefInst->getParent())) { |
| 486 | scan = SecondInfo.DefInst->getParent(); |
| 487 | mode = 1; // First dominates second |
| 488 | } else { |
| 489 | scan = FirstInfo.DefInst->getParent(); |
| 490 | mode = 2; // Second dominates first |
| 491 | } |
| 492 | |
| 493 | // If there's an interference, we need to insert copies |
Owen Anderson | b199cbe | 2008-01-10 00:33:11 +0000 | [diff] [blame] | 494 | if (interferes(p.first, p.second, scan, LV, mode)) { |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 495 | // Insert copies for First |
| 496 | for (int i = P->getNumOperands() - 1; i >= 2; i-=2) { |
| 497 | if (P->getOperand(i-1).getReg() == p.first) { |
| 498 | unsigned SrcReg = p.first; |
| 499 | MachineBasicBlock* From = P->getOperand(i).getMBB(); |
| 500 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 501 | Waiting[From].insert(std::make_pair(SrcReg, |
| 502 | P->getOperand(0).getReg())); |
| 503 | UsedByAnother.insert(SrcReg); |
| 504 | |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 505 | PHIUnion.erase(SrcReg); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
Owen Anderson | 42f9e96 | 2007-11-13 20:13:24 +0000 | [diff] [blame] | 510 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 511 | // Add the renaming set for this PHI node to our overal renaming information |
Owen Anderson | 0c5714b | 2008-01-08 21:54:52 +0000 | [diff] [blame] | 512 | RenameSets.insert(std::make_pair(P->getOperand(0).getReg(), PHIUnion)); |
Owen Anderson | cae8d8d | 2007-12-22 04:59:10 +0000 | [diff] [blame] | 513 | |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 514 | // Remember which registers are already renamed, so that we don't try to |
| 515 | // rename them for another PHI node in this block |
Owen Anderson | 42f9e96 | 2007-11-13 20:13:24 +0000 | [diff] [blame] | 516 | ProcessedNames.insert(PHIUnion.begin(), PHIUnion.end()); |
Owen Anderson | dc4d655 | 2008-01-10 00:47:01 +0000 | [diff] [blame^] | 517 | |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 518 | ++P; |
| 519 | } |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Owen Anderson | 965b467 | 2007-12-16 04:07:23 +0000 | [diff] [blame] | 522 | /// processPHIUnion - Take a set of candidate registers to be coallesced when |
| 523 | /// decomposing the PHI instruction. Use the DominanceForest to remove the ones |
| 524 | /// that are known to interfere, and flag others that need to be checked for |
| 525 | /// local interferences. |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 526 | void StrongPHIElimination::processPHIUnion(MachineInstr* Inst, |
| 527 | std::set<unsigned>& PHIUnion, |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 528 | std::vector<StrongPHIElimination::DomForestNode*>& DF, |
| 529 | std::vector<std::pair<unsigned, unsigned> >& locals) { |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 530 | |
| 531 | std::vector<DomForestNode*> worklist(DF.begin(), DF.end()); |
| 532 | SmallPtrSet<DomForestNode*, 4> visited; |
| 533 | |
| 534 | LiveVariables& LV = getAnalysis<LiveVariables>(); |
| 535 | unsigned DestReg = Inst->getOperand(0).getReg(); |
| 536 | |
Owen Anderson | 965b467 | 2007-12-16 04:07:23 +0000 | [diff] [blame] | 537 | // DF walk on the DomForest |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 538 | while (!worklist.empty()) { |
| 539 | DomForestNode* DFNode = worklist.back(); |
| 540 | |
| 541 | LiveVariables::VarInfo& Info = LV.getVarInfo(DFNode->getReg()); |
| 542 | visited.insert(DFNode); |
| 543 | |
| 544 | bool inserted = false; |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 545 | for (DomForestNode::iterator CI = DFNode->begin(), CE = DFNode->end(); |
| 546 | CI != CE; ++CI) { |
| 547 | DomForestNode* child = *CI; |
| 548 | LiveVariables::VarInfo& CInfo = LV.getVarInfo(child->getReg()); |
| 549 | |
| 550 | if (isLiveOut(Info, CInfo.DefInst->getParent())) { |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 551 | // Insert copies for parent |
| 552 | for (int i = Inst->getNumOperands() - 1; i >= 2; i-=2) { |
| 553 | if (Inst->getOperand(i-1).getReg() == DFNode->getReg()) { |
Owen Anderson | ed2ffa2 | 2007-12-12 01:25:08 +0000 | [diff] [blame] | 554 | unsigned SrcReg = DFNode->getReg(); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 555 | MachineBasicBlock* From = Inst->getOperand(i).getMBB(); |
| 556 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 557 | Waiting[From].insert(std::make_pair(SrcReg, DestReg)); |
| 558 | UsedByAnother.insert(SrcReg); |
| 559 | |
Owen Anderson | ed2ffa2 | 2007-12-12 01:25:08 +0000 | [diff] [blame] | 560 | PHIUnion.erase(SrcReg); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
Owen Anderson | 4ba08ec | 2007-12-13 05:43:37 +0000 | [diff] [blame] | 563 | } else if (isLiveIn(Info, CInfo.DefInst->getParent()) || |
| 564 | Info.DefInst->getParent() == CInfo.DefInst->getParent()) { |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 565 | // Add (p, c) to possible local interferences |
| 566 | locals.push_back(std::make_pair(DFNode->getReg(), child->getReg())); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 567 | } |
Owen Anderson | 965b467 | 2007-12-16 04:07:23 +0000 | [diff] [blame] | 568 | |
Owen Anderson | 4ba08ec | 2007-12-13 05:43:37 +0000 | [diff] [blame] | 569 | if (!visited.count(child)) { |
| 570 | worklist.push_back(child); |
| 571 | inserted = true; |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | if (!inserted) worklist.pop_back(); |
| 576 | } |
| 577 | } |
| 578 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 579 | /// ScheduleCopies - Insert copies into predecessor blocks, scheduling |
| 580 | /// them properly so as to avoid the 'lost copy' and the 'virtual swap' |
| 581 | /// problems. |
| 582 | /// |
| 583 | /// Based on "Practical Improvements to the Construction and Destruction |
| 584 | /// of Static Single Assignment Form" by Briggs, et al. |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 585 | void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB, |
| 586 | std::set<unsigned>& pushed) { |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 587 | // FIXME: This function needs to update LiveVariables |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 588 | std::map<unsigned, unsigned>& copy_set= Waiting[MBB]; |
| 589 | |
| 590 | std::map<unsigned, unsigned> worklist; |
| 591 | std::map<unsigned, unsigned> map; |
| 592 | |
| 593 | // Setup worklist of initial copies |
| 594 | for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(), |
| 595 | E = copy_set.end(); I != E; ) { |
| 596 | map.insert(std::make_pair(I->first, I->first)); |
| 597 | map.insert(std::make_pair(I->second, I->second)); |
| 598 | |
| 599 | if (!UsedByAnother.count(I->first)) { |
| 600 | worklist.insert(*I); |
| 601 | |
| 602 | // Avoid iterator invalidation |
| 603 | unsigned first = I->first; |
| 604 | ++I; |
| 605 | copy_set.erase(first); |
| 606 | } else { |
| 607 | ++I; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | LiveVariables& LV = getAnalysis<LiveVariables>(); |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 612 | MachineFunction* MF = MBB->getParent(); |
| 613 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 614 | |
| 615 | // Iterate over the worklist, inserting copies |
| 616 | while (!worklist.empty() || !copy_set.empty()) { |
| 617 | while (!worklist.empty()) { |
| 618 | std::pair<unsigned, unsigned> curr = *worklist.begin(); |
| 619 | worklist.erase(curr.first); |
| 620 | |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 621 | const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first); |
| 622 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 623 | if (isLiveOut(LV.getVarInfo(curr.second), MBB)) { |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 624 | // Create a temporary |
| 625 | unsigned t = MF->getRegInfo().createVirtualRegister(RC); |
| 626 | |
| 627 | // Insert copy from curr.second to a temporary at |
| 628 | // the Phi defining curr.second |
| 629 | LiveVariables::VarInfo VI = LV.getVarInfo(curr.second); |
| 630 | MachineBasicBlock::iterator PI = VI.DefInst; |
| 631 | TII->copyRegToReg(*VI.DefInst->getParent(), PI, t, |
| 632 | curr.second, RC, RC); |
| 633 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 634 | // Push temporary on Stacks |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 635 | Stacks[curr.second].push_back(t); |
| 636 | |
| 637 | // Insert curr.second in pushed |
| 638 | pushed.insert(curr.second); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // Insert copy from map[curr.first] to curr.second |
Owen Anderson | 9c2efa8 | 2008-01-10 00:01:41 +0000 | [diff] [blame] | 642 | TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), curr.second, |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 643 | map[curr.first], RC, RC); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 644 | map[curr.first] = curr.second; |
| 645 | |
| 646 | // If curr.first is a destination in copy_set... |
| 647 | for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(), |
| 648 | E = copy_set.end(); I != E; ) |
| 649 | if (curr.first == I->second) { |
| 650 | std::pair<unsigned, unsigned> temp = *I; |
| 651 | |
| 652 | // Avoid iterator invalidation |
| 653 | ++I; |
| 654 | copy_set.erase(temp.first); |
| 655 | worklist.insert(temp); |
| 656 | |
| 657 | break; |
| 658 | } else { |
| 659 | ++I; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | if (!copy_set.empty()) { |
| 664 | std::pair<unsigned, unsigned> curr = *copy_set.begin(); |
| 665 | copy_set.erase(curr.first); |
| 666 | |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 667 | const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(curr.first); |
| 668 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 669 | // 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] | 670 | unsigned t = MF->getRegInfo().createVirtualRegister(RC); |
Owen Anderson | 9c2efa8 | 2008-01-10 00:01:41 +0000 | [diff] [blame] | 671 | TII->copyRegToReg(*MBB, MBB->getFirstTerminator(), t, |
Owen Anderson | 0d893b4 | 2008-01-08 05:16:15 +0000 | [diff] [blame] | 672 | curr.second, RC, RC); |
| 673 | map[curr.second] = t; |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 674 | |
| 675 | worklist.insert(curr); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 680 | /// InsertCopies - insert copies into MBB and all of its successors |
Owen Anderson | 719fef6 | 2008-01-09 10:32:30 +0000 | [diff] [blame] | 681 | void StrongPHIElimination::InsertCopies(MachineBasicBlock* MBB, |
| 682 | std::set<MachineBasicBlock*>& visited) { |
| 683 | visited.insert(MBB); |
| 684 | |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 685 | std::set<unsigned> pushed; |
| 686 | |
| 687 | // Rewrite register uses from Stacks |
| 688 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 689 | I != E; ++I) |
| 690 | for (unsigned i = 0; i < I->getNumOperands(); ++i) |
| 691 | if (I->getOperand(i).isRegister() && |
| 692 | Stacks[I->getOperand(i).getReg()].size()) { |
| 693 | I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back()); |
| 694 | } |
| 695 | |
| 696 | // Schedule the copies for this block |
| 697 | ScheduleCopies(MBB, pushed); |
| 698 | |
| 699 | // Recur to our successors |
| 700 | for (GraphTraits<MachineBasicBlock*>::ChildIteratorType I = |
| 701 | GraphTraits<MachineBasicBlock*>::child_begin(MBB), E = |
| 702 | GraphTraits<MachineBasicBlock*>::child_end(MBB); I != E; ++I) |
Owen Anderson | 719fef6 | 2008-01-09 10:32:30 +0000 | [diff] [blame] | 703 | if (!visited.count(*I)) |
| 704 | InsertCopies(*I, visited); |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 705 | |
| 706 | // As we exit this block, pop the names we pushed while processing it |
| 707 | for (std::set<unsigned>::iterator I = pushed.begin(), |
| 708 | E = pushed.end(); I != E; ++I) |
| 709 | Stacks[*I].pop_back(); |
| 710 | } |
| 711 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 712 | bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) { |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 713 | // Compute DFS numbers of each block |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 714 | computeDFS(Fn); |
| 715 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 716 | // Determine which phi node operands need copies |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 717 | for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 718 | if (!I->empty() && |
| 719 | I->begin()->getOpcode() == TargetInstrInfo::PHI) |
| 720 | processBlock(I); |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 721 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 722 | // Insert copies |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 723 | // FIXME: This process should probably preserve LiveVariables |
Owen Anderson | 719fef6 | 2008-01-09 10:32:30 +0000 | [diff] [blame] | 724 | std::set<MachineBasicBlock*> visited; |
| 725 | InsertCopies(Fn.begin(), visited); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 726 | |
Owen Anderson | 0c5714b | 2008-01-08 21:54:52 +0000 | [diff] [blame] | 727 | // Perform renaming |
| 728 | typedef std::map<unsigned, std::set<unsigned> > RenameSetType; |
| 729 | for (RenameSetType::iterator I = RenameSets.begin(), E = RenameSets.end(); |
| 730 | I != E; ++I) |
| 731 | for (std::set<unsigned>::iterator SI = I->second.begin(), |
| 732 | SE = I->second.end(); SI != SE; ++SI) |
| 733 | Fn.getRegInfo().replaceRegWith(*SI, I->first); |
| 734 | |
| 735 | // FIXME: Insert last-minute copies |
| 736 | |
| 737 | // Remove PHIs |
| 738 | for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 739 | for (MachineBasicBlock::iterator BI = I->begin(), BE = I->end(); |
| 740 | BI != BE; ++BI) |
| 741 | if (BI->getOpcode() == TargetInstrInfo::PHI) |
| 742 | BI->eraseFromParent(); |
Owen Anderson | cae8d8d | 2007-12-22 04:59:10 +0000 | [diff] [blame] | 743 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 744 | return false; |
| 745 | } |