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