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 | 17b1418 | 2007-11-13 20:04:45 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/SSARegMap.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 | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 42 | DenseMap<MachineBasicBlock*, |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 43 | std::map<unsigned, unsigned> > Waiting; |
| 44 | |
| 45 | std::map<unsigned, std::vector<unsigned> > Stacks; |
| 46 | std::set<unsigned> UsedByAnother; |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 47 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 48 | bool runOnMachineFunction(MachineFunction &Fn); |
| 49 | |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 50 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 51 | AU.addPreserved<LiveVariables>(); |
| 52 | AU.addPreservedID(PHIEliminationID); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 53 | AU.addRequired<MachineDominatorTree>(); |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 54 | AU.addRequired<LiveVariables>(); |
| 55 | AU.setPreservesAll(); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 56 | MachineFunctionPass::getAnalysisUsage(AU); |
| 57 | } |
| 58 | |
| 59 | virtual void releaseMemory() { |
| 60 | preorder.clear(); |
| 61 | maxpreorder.clear(); |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 62 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 63 | Waiting.clear(); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | private: |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 67 | struct DomForestNode { |
| 68 | private: |
| 69 | std::vector<DomForestNode*> children; |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 70 | unsigned reg; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 71 | |
| 72 | void addChild(DomForestNode* DFN) { children.push_back(DFN); } |
| 73 | |
| 74 | public: |
| 75 | typedef std::vector<DomForestNode*>::iterator iterator; |
| 76 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 77 | DomForestNode(unsigned r, DomForestNode* parent) : reg(r) { |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 78 | if (parent) |
| 79 | parent->addChild(this); |
| 80 | } |
| 81 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 82 | ~DomForestNode() { |
| 83 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 84 | delete *I; |
| 85 | } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 86 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 87 | inline unsigned getReg() { return reg; } |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 88 | |
| 89 | inline DomForestNode::iterator begin() { return children.begin(); } |
| 90 | inline DomForestNode::iterator end() { return children.end(); } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 93 | DenseMap<MachineBasicBlock*, unsigned> preorder; |
| 94 | DenseMap<MachineBasicBlock*, unsigned> maxpreorder; |
| 95 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 96 | |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 97 | void computeDFS(MachineFunction& MF); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 98 | void processBlock(MachineBasicBlock* MBB); |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 99 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 100 | std::vector<DomForestNode*> computeDomForest(std::set<unsigned>& instrs); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 101 | void processPHIUnion(MachineInstr* Inst, |
| 102 | std::set<unsigned>& PHIUnion, |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 103 | std::vector<StrongPHIElimination::DomForestNode*>& DF, |
| 104 | std::vector<std::pair<unsigned, unsigned> >& locals); |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 105 | void ScheduleCopies(MachineBasicBlock* MBB, std::set<unsigned>& pushed); |
| 106 | void InsertCopies(MachineBasicBlock* MBB); |
Owen Anderson | 0bda0e8 | 2007-10-31 03:37:57 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | char StrongPHIElimination::ID = 0; |
| 110 | RegisterPass<StrongPHIElimination> X("strong-phi-node-elimination", |
| 111 | "Eliminate PHI nodes for register allocation, intelligently"); |
| 112 | } |
| 113 | |
| 114 | const PassInfo *llvm::StrongPHIEliminationID = X.getPassInfo(); |
| 115 | |
| 116 | /// computeDFS - Computes the DFS-in and DFS-out numbers of the dominator tree |
| 117 | /// of the given MachineFunction. These numbers are then used in other parts |
| 118 | /// of the PHI elimination process. |
| 119 | void StrongPHIElimination::computeDFS(MachineFunction& MF) { |
| 120 | SmallPtrSet<MachineDomTreeNode*, 8> frontier; |
| 121 | SmallPtrSet<MachineDomTreeNode*, 8> visited; |
| 122 | |
| 123 | unsigned time = 0; |
| 124 | |
| 125 | MachineDominatorTree& DT = getAnalysis<MachineDominatorTree>(); |
| 126 | |
| 127 | MachineDomTreeNode* node = DT.getRootNode(); |
| 128 | |
| 129 | std::vector<MachineDomTreeNode*> worklist; |
| 130 | worklist.push_back(node); |
| 131 | |
| 132 | while (!worklist.empty()) { |
| 133 | MachineDomTreeNode* currNode = worklist.back(); |
| 134 | |
| 135 | if (!frontier.count(currNode)) { |
| 136 | frontier.insert(currNode); |
| 137 | ++time; |
| 138 | preorder.insert(std::make_pair(currNode->getBlock(), time)); |
| 139 | } |
| 140 | |
| 141 | bool inserted = false; |
| 142 | for (MachineDomTreeNode::iterator I = node->begin(), E = node->end(); |
| 143 | I != E; ++I) |
| 144 | if (!frontier.count(*I) && !visited.count(*I)) { |
| 145 | worklist.push_back(*I); |
| 146 | inserted = true; |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | if (!inserted) { |
| 151 | frontier.erase(currNode); |
| 152 | visited.insert(currNode); |
| 153 | maxpreorder.insert(std::make_pair(currNode->getBlock(), time)); |
| 154 | |
| 155 | worklist.pop_back(); |
| 156 | } |
| 157 | } |
Duncan Sands | 1bd3271 | 2007-10-31 08:49:24 +0000 | [diff] [blame] | 158 | } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 159 | |
Owen Anderson | 8b96b9f | 2007-11-06 05:26:02 +0000 | [diff] [blame] | 160 | /// PreorderSorter - a helper class that is used to sort registers |
| 161 | /// according to the preorder number of their defining blocks |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 162 | class PreorderSorter { |
| 163 | private: |
| 164 | DenseMap<MachineBasicBlock*, unsigned>& preorder; |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 165 | LiveVariables& LV; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 166 | |
| 167 | public: |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 168 | PreorderSorter(DenseMap<MachineBasicBlock*, unsigned>& p, |
| 169 | LiveVariables& L) : preorder(p), LV(L) { } |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 170 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 171 | bool operator()(unsigned A, unsigned B) { |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 172 | if (A == B) |
| 173 | return false; |
| 174 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 175 | MachineBasicBlock* ABlock = LV.getVarInfo(A).DefInst->getParent(); |
| 176 | MachineBasicBlock* BBlock = LV.getVarInfo(A).DefInst->getParent(); |
| 177 | |
| 178 | if (preorder[ABlock] < preorder[BBlock]) |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 179 | return true; |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 180 | else if (preorder[ABlock] > preorder[BBlock]) |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 181 | return false; |
| 182 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 183 | assert(0 && "Error sorting by dominance!"); |
| 184 | return false; |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 185 | } |
| 186 | }; |
| 187 | |
Owen Anderson | 8b96b9f | 2007-11-06 05:26:02 +0000 | [diff] [blame] | 188 | /// computeDomForest - compute the subforest of the DomTree corresponding |
| 189 | /// to the defining blocks of the registers in question |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 190 | std::vector<StrongPHIElimination::DomForestNode*> |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 191 | StrongPHIElimination::computeDomForest(std::set<unsigned>& regs) { |
| 192 | LiveVariables& LV = getAnalysis<LiveVariables>(); |
| 193 | |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 194 | DomForestNode* VirtualRoot = new DomForestNode(0, 0); |
| 195 | maxpreorder.insert(std::make_pair((MachineBasicBlock*)0, ~0UL)); |
| 196 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 197 | std::vector<unsigned> worklist; |
| 198 | worklist.reserve(regs.size()); |
| 199 | for (std::set<unsigned>::iterator I = regs.begin(), E = regs.end(); |
| 200 | I != E; ++I) |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 201 | worklist.push_back(*I); |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 202 | |
| 203 | PreorderSorter PS(preorder, LV); |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 204 | std::sort(worklist.begin(), worklist.end(), PS); |
| 205 | |
| 206 | DomForestNode* CurrentParent = VirtualRoot; |
| 207 | std::vector<DomForestNode*> stack; |
| 208 | stack.push_back(VirtualRoot); |
| 209 | |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 210 | for (std::vector<unsigned>::iterator I = worklist.begin(), E = worklist.end(); |
| 211 | I != E; ++I) { |
| 212 | unsigned pre = preorder[LV.getVarInfo(*I).DefInst->getParent()]; |
| 213 | MachineBasicBlock* parentBlock = |
| 214 | LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent(); |
| 215 | |
| 216 | while (pre > maxpreorder[parentBlock]) { |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 217 | stack.pop_back(); |
| 218 | CurrentParent = stack.back(); |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 219 | |
| 220 | parentBlock = LV.getVarInfo(CurrentParent->getReg()).DefInst->getParent(); |
Owen Anderson | 83430bc | 2007-11-04 22:33:26 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | DomForestNode* child = new DomForestNode(*I, CurrentParent); |
| 224 | stack.push_back(child); |
| 225 | CurrentParent = child; |
| 226 | } |
| 227 | |
| 228 | std::vector<DomForestNode*> ret; |
| 229 | ret.insert(ret.end(), VirtualRoot->begin(), VirtualRoot->end()); |
| 230 | return ret; |
| 231 | } |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 232 | |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 233 | /// isLiveIn - helper method that determines, from a VarInfo, if a register |
| 234 | /// is live into a block |
| 235 | bool isLiveIn(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) { |
| 236 | if (V.AliveBlocks.test(MBB->getNumber())) |
| 237 | return true; |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 238 | |
Owen Anderson | 14b3fb7 | 2007-11-08 01:32:45 +0000 | [diff] [blame] | 239 | if (V.DefInst->getParent() != MBB && |
| 240 | V.UsedBlocks.test(MBB->getNumber())) |
| 241 | return true; |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 242 | |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | /// isLiveOut - help method that determines, from a VarInfo, if a register is |
| 247 | /// live out of a block. |
| 248 | bool isLiveOut(LiveVariables::VarInfo& V, MachineBasicBlock* MBB) { |
Owen Anderson | 14b3fb7 | 2007-11-08 01:32:45 +0000 | [diff] [blame] | 249 | if (MBB == V.DefInst->getParent() || |
| 250 | V.UsedBlocks.test(MBB->getNumber())) { |
| 251 | for (std::vector<MachineInstr*>::iterator I = V.Kills.begin(), |
| 252 | E = V.Kills.end(); I != E; ++I) |
| 253 | if ((*I)->getParent() == MBB) |
| 254 | return false; |
| 255 | |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 256 | return true; |
Owen Anderson | 14b3fb7 | 2007-11-08 01:32:45 +0000 | [diff] [blame] | 257 | } |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 258 | |
| 259 | return false; |
| 260 | } |
| 261 | |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 262 | /// isKillInst - helper method that determines, from a VarInfo, if an |
| 263 | /// instruction kills a given register |
| 264 | bool isKillInst(LiveVariables::VarInfo& V, MachineInstr* MI) { |
| 265 | return std::find(V.Kills.begin(), V.Kills.end(), MI) != V.Kills.end(); |
| 266 | } |
| 267 | |
| 268 | /// interferes - checks for local interferences by scanning a block. The only |
| 269 | /// trick parameter is 'mode' which tells it the relationship of the two |
| 270 | /// registers. 0 - defined in the same block, 1 - first properly dominates |
| 271 | /// second, 2 - second properly dominates first |
| 272 | bool interferes(LiveVariables::VarInfo& First, LiveVariables::VarInfo& Second, |
| 273 | MachineBasicBlock* scan, unsigned mode) { |
| 274 | MachineInstr* def = 0; |
| 275 | MachineInstr* kill = 0; |
| 276 | |
| 277 | bool interference = false; |
| 278 | |
| 279 | // Wallk the block, checking for interferences |
| 280 | for (MachineBasicBlock::iterator MBI = scan->begin(), MBE = scan->end(); |
| 281 | MBI != MBE; ++MBI) { |
| 282 | MachineInstr* curr = MBI; |
| 283 | |
| 284 | // Same defining block... |
| 285 | if (mode == 0) { |
| 286 | if (curr == First.DefInst) { |
| 287 | // If we find our first DefInst, save it |
| 288 | if (!def) { |
| 289 | def = curr; |
| 290 | // If there's already an unkilled DefInst, then |
| 291 | // this is an interference |
| 292 | } else if (!kill) { |
| 293 | interference = true; |
| 294 | break; |
| 295 | // If there's a DefInst followed by a KillInst, then |
| 296 | // they can't interfere |
| 297 | } else { |
| 298 | interference = false; |
| 299 | break; |
| 300 | } |
| 301 | // Symmetric with the above |
| 302 | } else if (curr == Second.DefInst ) { |
| 303 | if (!def) { |
| 304 | def = curr; |
| 305 | } else if (!kill) { |
| 306 | interference = true; |
| 307 | break; |
| 308 | } else { |
| 309 | interference = false; |
| 310 | break; |
| 311 | } |
| 312 | // Store KillInsts if they match up with the DefInst |
| 313 | } else if (isKillInst(First, curr)) { |
| 314 | if (def == First.DefInst) { |
| 315 | kill = curr; |
| 316 | } else if (isKillInst(Second, curr)) { |
| 317 | if (def == Second.DefInst) { |
| 318 | kill = curr; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | // First properly dominates second... |
| 323 | } else if (mode == 1) { |
| 324 | if (curr == Second.DefInst) { |
| 325 | // DefInst of second without kill of first is an interference |
| 326 | if (!kill) { |
| 327 | interference = true; |
| 328 | break; |
| 329 | // DefInst after a kill is a non-interference |
| 330 | } else { |
| 331 | interference = false; |
| 332 | break; |
| 333 | } |
| 334 | // Save KillInsts of First |
| 335 | } else if (isKillInst(First, curr)) { |
| 336 | kill = curr; |
| 337 | } |
| 338 | // Symmetric with the above |
| 339 | } else if (mode == 2) { |
| 340 | if (curr == First.DefInst) { |
| 341 | if (!kill) { |
| 342 | interference = true; |
| 343 | break; |
| 344 | } else { |
| 345 | interference = false; |
| 346 | break; |
| 347 | } |
| 348 | } else if (isKillInst(Second, curr)) { |
| 349 | kill = curr; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return interference; |
| 355 | } |
| 356 | |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 357 | /// processBlock - Eliminate PHIs in the given block |
| 358 | void StrongPHIElimination::processBlock(MachineBasicBlock* MBB) { |
| 359 | LiveVariables& LV = getAnalysis<LiveVariables>(); |
| 360 | |
| 361 | // Holds names that have been added to a set in any PHI within this block |
| 362 | // before the current one. |
| 363 | std::set<unsigned> ProcessedNames; |
| 364 | |
| 365 | MachineBasicBlock::iterator P = MBB->begin(); |
| 366 | while (P->getOpcode() == TargetInstrInfo::PHI) { |
| 367 | LiveVariables::VarInfo& PHIInfo = LV.getVarInfo(P->getOperand(0).getReg()); |
| 368 | |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 369 | unsigned DestReg = P->getOperand(0).getReg(); |
| 370 | |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 371 | // Hold the names that are currently in the candidate set. |
| 372 | std::set<unsigned> PHIUnion; |
| 373 | std::set<MachineBasicBlock*> UnionedBlocks; |
| 374 | |
| 375 | for (int i = P->getNumOperands() - 1; i >= 2; i-=2) { |
| 376 | unsigned SrcReg = P->getOperand(i-1).getReg(); |
| 377 | LiveVariables::VarInfo& SrcInfo = LV.getVarInfo(SrcReg); |
| 378 | |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 379 | // Check for trivial interferences |
| 380 | if (isLiveIn(SrcInfo, P->getParent()) || |
| 381 | isLiveOut(PHIInfo, SrcInfo.DefInst->getParent()) || |
| 382 | ( PHIInfo.DefInst->getOpcode() == TargetInstrInfo::PHI && |
| 383 | isLiveIn(PHIInfo, SrcInfo.DefInst->getParent()) ) || |
| 384 | ProcessedNames.count(SrcReg) || |
| 385 | UnionedBlocks.count(SrcInfo.DefInst->getParent())) { |
| 386 | |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 387 | // add a copy from a_i to p in Waiting[From[a_i]] |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 388 | MachineBasicBlock* From = P->getOperand(i).getMachineBasicBlock(); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 389 | Waiting[From].insert(std::make_pair(SrcReg, DestReg)); |
| 390 | UsedByAnother.insert(SrcReg); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 391 | } else { |
| 392 | PHIUnion.insert(SrcReg); |
| 393 | UnionedBlocks.insert(SrcInfo.DefInst->getParent()); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 394 | } |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Owen Anderson | 42f9e96 | 2007-11-13 20:13:24 +0000 | [diff] [blame] | 397 | std::vector<StrongPHIElimination::DomForestNode*> DF = |
| 398 | computeDomForest(PHIUnion); |
| 399 | |
Owen Anderson | afc6de0 | 2007-12-10 08:07:09 +0000 | [diff] [blame] | 400 | // Walk DomForest to resolve interferences |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 401 | std::vector<std::pair<unsigned, unsigned> > localInterferences; |
| 402 | processPHIUnion(P, PHIUnion, DF, localInterferences); |
| 403 | |
Owen Anderson | cae8d8d | 2007-12-22 04:59:10 +0000 | [diff] [blame] | 404 | // Check for local interferences |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 405 | for (std::vector<std::pair<unsigned, unsigned> >::iterator I = |
| 406 | localInterferences.begin(), E = localInterferences.end(); I != E; ++I) { |
| 407 | std::pair<unsigned, unsigned> p = *I; |
| 408 | |
| 409 | LiveVariables::VarInfo& FirstInfo = LV.getVarInfo(p.first); |
| 410 | LiveVariables::VarInfo& SecondInfo = LV.getVarInfo(p.second); |
| 411 | |
| 412 | MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>(); |
| 413 | |
| 414 | // Determine the block we need to scan and the relationship between |
| 415 | // the two registers |
| 416 | MachineBasicBlock* scan = 0; |
| 417 | unsigned mode = 0; |
| 418 | if (FirstInfo.DefInst->getParent() == SecondInfo.DefInst->getParent()) { |
| 419 | scan = FirstInfo.DefInst->getParent(); |
| 420 | mode = 0; // Same block |
| 421 | } else if (MDT.dominates(FirstInfo.DefInst->getParent(), |
| 422 | SecondInfo.DefInst->getParent())) { |
| 423 | scan = SecondInfo.DefInst->getParent(); |
| 424 | mode = 1; // First dominates second |
| 425 | } else { |
| 426 | scan = FirstInfo.DefInst->getParent(); |
| 427 | mode = 2; // Second dominates first |
| 428 | } |
| 429 | |
| 430 | // If there's an interference, we need to insert copies |
| 431 | if (interferes(FirstInfo, SecondInfo, scan, mode)) { |
| 432 | // Insert copies for First |
| 433 | for (int i = P->getNumOperands() - 1; i >= 2; i-=2) { |
| 434 | if (P->getOperand(i-1).getReg() == p.first) { |
| 435 | unsigned SrcReg = p.first; |
| 436 | MachineBasicBlock* From = P->getOperand(i).getMBB(); |
| 437 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 438 | Waiting[From].insert(std::make_pair(SrcReg, |
| 439 | P->getOperand(0).getReg())); |
| 440 | UsedByAnother.insert(SrcReg); |
| 441 | |
Owen Anderson | 87a702b | 2007-12-16 05:44:27 +0000 | [diff] [blame] | 442 | PHIUnion.erase(SrcReg); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
Owen Anderson | 42f9e96 | 2007-11-13 20:13:24 +0000 | [diff] [blame] | 447 | |
Owen Anderson | cae8d8d | 2007-12-22 04:59:10 +0000 | [diff] [blame] | 448 | // FIXME: Cache renaming information |
| 449 | |
Owen Anderson | 42f9e96 | 2007-11-13 20:13:24 +0000 | [diff] [blame] | 450 | ProcessedNames.insert(PHIUnion.begin(), PHIUnion.end()); |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 451 | ++P; |
| 452 | } |
Owen Anderson | ee49b53 | 2007-11-06 05:22:43 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Owen Anderson | 965b467 | 2007-12-16 04:07:23 +0000 | [diff] [blame] | 455 | /// processPHIUnion - Take a set of candidate registers to be coallesced when |
| 456 | /// decomposing the PHI instruction. Use the DominanceForest to remove the ones |
| 457 | /// that are known to interfere, and flag others that need to be checked for |
| 458 | /// local interferences. |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 459 | void StrongPHIElimination::processPHIUnion(MachineInstr* Inst, |
| 460 | std::set<unsigned>& PHIUnion, |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 461 | std::vector<StrongPHIElimination::DomForestNode*>& DF, |
| 462 | std::vector<std::pair<unsigned, unsigned> >& locals) { |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 463 | |
| 464 | std::vector<DomForestNode*> worklist(DF.begin(), DF.end()); |
| 465 | SmallPtrSet<DomForestNode*, 4> visited; |
| 466 | |
| 467 | LiveVariables& LV = getAnalysis<LiveVariables>(); |
| 468 | unsigned DestReg = Inst->getOperand(0).getReg(); |
| 469 | |
Owen Anderson | 965b467 | 2007-12-16 04:07:23 +0000 | [diff] [blame] | 470 | // DF walk on the DomForest |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 471 | while (!worklist.empty()) { |
| 472 | DomForestNode* DFNode = worklist.back(); |
| 473 | |
| 474 | LiveVariables::VarInfo& Info = LV.getVarInfo(DFNode->getReg()); |
| 475 | visited.insert(DFNode); |
| 476 | |
| 477 | bool inserted = false; |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 478 | for (DomForestNode::iterator CI = DFNode->begin(), CE = DFNode->end(); |
| 479 | CI != CE; ++CI) { |
| 480 | DomForestNode* child = *CI; |
| 481 | LiveVariables::VarInfo& CInfo = LV.getVarInfo(child->getReg()); |
| 482 | |
| 483 | if (isLiveOut(Info, CInfo.DefInst->getParent())) { |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 484 | // Insert copies for parent |
| 485 | for (int i = Inst->getNumOperands() - 1; i >= 2; i-=2) { |
| 486 | if (Inst->getOperand(i-1).getReg() == DFNode->getReg()) { |
Owen Anderson | ed2ffa2 | 2007-12-12 01:25:08 +0000 | [diff] [blame] | 487 | unsigned SrcReg = DFNode->getReg(); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 488 | MachineBasicBlock* From = Inst->getOperand(i).getMBB(); |
| 489 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 490 | Waiting[From].insert(std::make_pair(SrcReg, DestReg)); |
| 491 | UsedByAnother.insert(SrcReg); |
| 492 | |
Owen Anderson | ed2ffa2 | 2007-12-12 01:25:08 +0000 | [diff] [blame] | 493 | PHIUnion.erase(SrcReg); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
Owen Anderson | 4ba08ec | 2007-12-13 05:43:37 +0000 | [diff] [blame] | 496 | } else if (isLiveIn(Info, CInfo.DefInst->getParent()) || |
| 497 | Info.DefInst->getParent() == CInfo.DefInst->getParent()) { |
Owen Anderson | 62d67dd | 2007-12-13 05:53:03 +0000 | [diff] [blame] | 498 | // Add (p, c) to possible local interferences |
| 499 | locals.push_back(std::make_pair(DFNode->getReg(), child->getReg())); |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 500 | } |
Owen Anderson | 965b467 | 2007-12-16 04:07:23 +0000 | [diff] [blame] | 501 | |
Owen Anderson | 4ba08ec | 2007-12-13 05:43:37 +0000 | [diff] [blame] | 502 | if (!visited.count(child)) { |
| 503 | worklist.push_back(child); |
| 504 | inserted = true; |
Owen Anderson | d525f66 | 2007-12-11 20:12:11 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | if (!inserted) worklist.pop_back(); |
| 509 | } |
| 510 | } |
| 511 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 512 | /// ScheduleCopies - Insert copies into predecessor blocks, scheduling |
| 513 | /// them properly so as to avoid the 'lost copy' and the 'virtual swap' |
| 514 | /// problems. |
| 515 | /// |
| 516 | /// Based on "Practical Improvements to the Construction and Destruction |
| 517 | /// of Static Single Assignment Form" by Briggs, et al. |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 518 | void StrongPHIElimination::ScheduleCopies(MachineBasicBlock* MBB, |
| 519 | std::set<unsigned>& pushed) { |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 520 | std::map<unsigned, unsigned>& copy_set= Waiting[MBB]; |
| 521 | |
| 522 | std::map<unsigned, unsigned> worklist; |
| 523 | std::map<unsigned, unsigned> map; |
| 524 | |
| 525 | // Setup worklist of initial copies |
| 526 | for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(), |
| 527 | E = copy_set.end(); I != E; ) { |
| 528 | map.insert(std::make_pair(I->first, I->first)); |
| 529 | map.insert(std::make_pair(I->second, I->second)); |
| 530 | |
| 531 | if (!UsedByAnother.count(I->first)) { |
| 532 | worklist.insert(*I); |
| 533 | |
| 534 | // Avoid iterator invalidation |
| 535 | unsigned first = I->first; |
| 536 | ++I; |
| 537 | copy_set.erase(first); |
| 538 | } else { |
| 539 | ++I; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | LiveVariables& LV = getAnalysis<LiveVariables>(); |
| 544 | |
| 545 | // Iterate over the worklist, inserting copies |
| 546 | while (!worklist.empty() || !copy_set.empty()) { |
| 547 | while (!worklist.empty()) { |
| 548 | std::pair<unsigned, unsigned> curr = *worklist.begin(); |
| 549 | worklist.erase(curr.first); |
| 550 | |
| 551 | if (isLiveOut(LV.getVarInfo(curr.second), MBB)) { |
| 552 | // Insert copy from curr.second to a temporary |
| 553 | // Push temporary on Stacks |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 554 | // Insert temporary in pushed |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | // Insert copy from map[curr.first] to curr.second |
| 558 | map[curr.first] = curr.second; |
| 559 | |
| 560 | // If curr.first is a destination in copy_set... |
| 561 | for (std::map<unsigned, unsigned>::iterator I = copy_set.begin(), |
| 562 | E = copy_set.end(); I != E; ) |
| 563 | if (curr.first == I->second) { |
| 564 | std::pair<unsigned, unsigned> temp = *I; |
| 565 | |
| 566 | // Avoid iterator invalidation |
| 567 | ++I; |
| 568 | copy_set.erase(temp.first); |
| 569 | worklist.insert(temp); |
| 570 | |
| 571 | break; |
| 572 | } else { |
| 573 | ++I; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (!copy_set.empty()) { |
| 578 | std::pair<unsigned, unsigned> curr = *copy_set.begin(); |
| 579 | copy_set.erase(curr.first); |
| 580 | |
| 581 | // Insert a copy from dest to a new temporary t at the end of b |
| 582 | // map[curr.second] = t; |
| 583 | |
| 584 | worklist.insert(curr); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 589 | /// InsertCopies - insert copies into MBB and all of its successors |
| 590 | void StrongPHIElimination::InsertCopies(MachineBasicBlock* MBB) { |
| 591 | std::set<unsigned> pushed; |
| 592 | |
| 593 | // Rewrite register uses from Stacks |
| 594 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 595 | I != E; ++I) |
| 596 | for (unsigned i = 0; i < I->getNumOperands(); ++i) |
| 597 | if (I->getOperand(i).isRegister() && |
| 598 | Stacks[I->getOperand(i).getReg()].size()) { |
| 599 | I->getOperand(i).setReg(Stacks[I->getOperand(i).getReg()].back()); |
| 600 | } |
| 601 | |
| 602 | // Schedule the copies for this block |
| 603 | ScheduleCopies(MBB, pushed); |
| 604 | |
| 605 | // Recur to our successors |
| 606 | for (GraphTraits<MachineBasicBlock*>::ChildIteratorType I = |
| 607 | GraphTraits<MachineBasicBlock*>::child_begin(MBB), E = |
| 608 | GraphTraits<MachineBasicBlock*>::child_end(MBB); I != E; ++I) |
| 609 | InsertCopies(*I); |
| 610 | |
| 611 | // As we exit this block, pop the names we pushed while processing it |
| 612 | for (std::set<unsigned>::iterator I = pushed.begin(), |
| 613 | E = pushed.end(); I != E; ++I) |
| 614 | Stacks[*I].pop_back(); |
| 615 | } |
| 616 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 617 | bool StrongPHIElimination::runOnMachineFunction(MachineFunction &Fn) { |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 618 | // Compute DFS numbers of each block |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 619 | computeDFS(Fn); |
| 620 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 621 | // Determine which phi node operands need copies |
Owen Anderson | 60a877d | 2007-11-07 05:17:15 +0000 | [diff] [blame] | 622 | for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 623 | if (!I->empty() && |
| 624 | I->begin()->getOpcode() == TargetInstrInfo::PHI) |
| 625 | processBlock(I); |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 626 | |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 627 | // Insert copies |
Owen Anderson | f1519e8 | 2007-12-24 22:12:23 +0000 | [diff] [blame] | 628 | // FIXME: This process should probably preserve LiveVariables |
| 629 | InsertCopies(Fn.begin()); |
Owen Anderson | efbcebc | 2007-12-23 15:37:26 +0000 | [diff] [blame] | 630 | |
Owen Anderson | cae8d8d | 2007-12-22 04:59:10 +0000 | [diff] [blame] | 631 | // FIXME: Perform renaming |
| 632 | |
Owen Anderson | a4ad2e7 | 2007-11-06 04:49:43 +0000 | [diff] [blame] | 633 | return false; |
| 634 | } |