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