| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 1 | //===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===// | 
| Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // | 
| John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | f3ebc3f | 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. | 
| Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // | 
| John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
| Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 9 | // | 
| Chris Lattner | 5ab42e5 | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 10 | // This file implements the LiveVariable analysis pass.  For each machine | 
|  | 11 | // instruction in the function, this pass calculates the set of registers that | 
|  | 12 | // are immediately dead after the instruction (i.e., the instruction calculates | 
|  | 13 | // the value, but it is never used) and the set of registers that are used by | 
|  | 14 | // the instruction, but are never used after the instruction (i.e., they are | 
|  | 15 | // killed). | 
|  | 16 | // | 
| Lang Hames | 652f212 | 2012-04-01 19:27:25 +0000 | [diff] [blame] | 17 | // This class computes live variables using a sparse implementation based on | 
| Chris Lattner | 5ab42e5 | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 18 | // the machine code SSA form.  This class computes live variable information for | 
|  | 19 | // each virtual and _register allocatable_ physical register in a function.  It | 
|  | 20 | // uses the dominance properties of SSA form to efficiently compute live | 
|  | 21 | // variables for virtual registers, and assumes that physical registers are only | 
|  | 22 | // live within a single basic block (allowing it to do a single local analysis | 
|  | 23 | // to resolve physical register lifetimes in each basic block).  If a physical | 
|  | 24 | // register is not register allocatable, it is not tracked.  This is useful for | 
|  | 25 | // things like the stack pointer and condition codes. | 
|  | 26 | // | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// | 
|  | 28 |  | 
|  | 29 | #include "llvm/CodeGen/LiveVariables.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DepthFirstIterator.h" | 
|  | 31 | #include "llvm/ADT/STLExtras.h" | 
|  | 32 | #include "llvm/ADT/SmallPtrSet.h" | 
|  | 33 | #include "llvm/ADT/SmallSet.h" | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineInstr.h" | 
| Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
| Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/Passes.h" | 
| Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 37 | #include "llvm/Config/llvm-config.h" | 
| David Greene | d599dcd | 2010-01-04 23:02:10 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ErrorHandling.h" | 
| Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" | 
| Chris Lattner | eeacce5 | 2005-08-24 00:09:33 +0000 | [diff] [blame] | 41 | #include <algorithm> | 
| Chris Lattner | 0770862 | 2004-01-30 22:08:53 +0000 | [diff] [blame] | 42 | using namespace llvm; | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 43 |  | 
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 44 | char LiveVariables::ID = 0; | 
| Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 45 | char &llvm::LiveVariablesID = LiveVariables::ID; | 
| Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 46 | INITIALIZE_PASS_BEGIN(LiveVariables, "livevars", | 
|  | 47 | "Live Variable Analysis", false, false) | 
|  | 48 | INITIALIZE_PASS_DEPENDENCY(UnreachableMachineBlockElim) | 
|  | 49 | INITIALIZE_PASS_END(LiveVariables, "livevars", | 
| Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 50 | "Live Variable Analysis", false, false) | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 51 |  | 
| Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 52 |  | 
|  | 53 | void LiveVariables::getAnalysisUsage(AnalysisUsage &AU) const { | 
|  | 54 | AU.addRequiredID(UnreachableMachineBlockElimID); | 
|  | 55 | AU.setPreservesAll(); | 
| Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 56 | MachineFunctionPass::getAnalysisUsage(AU); | 
| Owen Anderson | a102290 | 2008-08-04 23:54:43 +0000 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Jakob Stoklund Olesen | 4453dc9 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 59 | MachineInstr * | 
|  | 60 | LiveVariables::VarInfo::findKill(const MachineBasicBlock *MBB) const { | 
|  | 61 | for (unsigned i = 0, e = Kills.size(); i != e; ++i) | 
|  | 62 | if (Kills[i]->getParent() == MBB) | 
|  | 63 | return Kills[i]; | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 64 | return nullptr; | 
| Jakob Stoklund Olesen | 4453dc9 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
| Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 67 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | 
| Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 68 | LLVM_DUMP_METHOD void LiveVariables::VarInfo::dump() const { | 
| David Greene | d599dcd | 2010-01-04 23:02:10 +0000 | [diff] [blame] | 69 | dbgs() << "  Alive in blocks: "; | 
| Jeffrey Yasskin | 7d287cb | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 70 | for (SparseBitVector<>::iterator I = AliveBlocks.begin(), | 
|  | 71 | E = AliveBlocks.end(); I != E; ++I) | 
| David Greene | d599dcd | 2010-01-04 23:02:10 +0000 | [diff] [blame] | 72 | dbgs() << *I << ", "; | 
|  | 73 | dbgs() << "\n  Killed by:"; | 
| Chris Lattner | be45b5e | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 74 | if (Kills.empty()) | 
| David Greene | d599dcd | 2010-01-04 23:02:10 +0000 | [diff] [blame] | 75 | dbgs() << " No instructions.\n"; | 
| Chris Lattner | be45b5e | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 76 | else { | 
|  | 77 | for (unsigned i = 0, e = Kills.size(); i != e; ++i) | 
| David Greene | d599dcd | 2010-01-04 23:02:10 +0000 | [diff] [blame] | 78 | dbgs() << "\n    #" << i << ": " << *Kills[i]; | 
|  | 79 | dbgs() << "\n"; | 
| Chris Lattner | be45b5e | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 80 | } | 
|  | 81 | } | 
| Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 82 | #endif | 
| Chris Lattner | be45b5e | 2006-01-04 05:40:30 +0000 | [diff] [blame] | 83 |  | 
| Bill Wendling | 59cc159 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 84 | /// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg. | 
| Chris Lattner | 584bae4 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 85 | LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) { | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 86 | assert(TargetRegisterInfo::isVirtualRegister(RegIdx) && | 
| Chris Lattner | 584bae4 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 87 | "getVarInfo: not a virtual register!"); | 
| Jakob Stoklund Olesen | 28d7669 | 2011-01-08 23:10:57 +0000 | [diff] [blame] | 88 | VirtRegInfo.grow(RegIdx); | 
| Jeffrey Yasskin | 7d287cb | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 89 | return VirtRegInfo[RegIdx]; | 
| Chris Lattner | 584bae4 | 2003-05-12 14:24:00 +0000 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Owen Anderson | 897aed9 | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 92 | void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo, | 
|  | 93 | MachineBasicBlock *DefBlock, | 
| Evan Cheng | 9e17872 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 94 | MachineBasicBlock *MBB, | 
|  | 95 | std::vector<MachineBasicBlock*> &WorkList) { | 
| Chris Lattner | 6c375e4 | 2004-07-01 04:29:47 +0000 | [diff] [blame] | 96 | unsigned BBNum = MBB->getNumber(); | 
| Andrew Trick | 808a7a6 | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 97 |  | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 98 | // Check to see if this basic block is one of the killing blocks.  If so, | 
| Bill Wendling | 59cc159 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 99 | // remove it. | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 100 | for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) | 
| Chris Lattner | aef6c2a | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 101 | if (VRInfo.Kills[i]->getParent() == MBB) { | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 102 | VRInfo.Kills.erase(VRInfo.Kills.begin()+i);  // Erase entry | 
|  | 103 | break; | 
|  | 104 | } | 
| Andrew Trick | 808a7a6 | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 105 |  | 
| Owen Anderson | 897aed9 | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 106 | if (MBB == DefBlock) return;  // Terminate recursion | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 107 |  | 
| Jeffrey Yasskin | 7d287cb | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 108 | if (VRInfo.AliveBlocks.test(BBNum)) | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 109 | return;  // We already know the block is live | 
|  | 110 |  | 
|  | 111 | // Mark the variable known alive in this bb | 
| Jeffrey Yasskin | 7d287cb | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 112 | VRInfo.AliveBlocks.set(BBNum); | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 113 |  | 
| Jakob Stoklund Olesen | 7d544f9 | 2012-03-09 23:41:44 +0000 | [diff] [blame] | 114 | assert(MBB != &MF->front() && "Can't find reaching def for virtreg"); | 
| Benjamin Kramer | b8ca01f | 2011-03-08 17:28:36 +0000 | [diff] [blame] | 115 | WorkList.insert(WorkList.end(), MBB->pred_rbegin(), MBB->pred_rend()); | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
| Bill Wendling | 406fdbd | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 118 | void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo, | 
| Owen Anderson | 897aed9 | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 119 | MachineBasicBlock *DefBlock, | 
| Evan Cheng | 9e17872 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 120 | MachineBasicBlock *MBB) { | 
|  | 121 | std::vector<MachineBasicBlock*> WorkList; | 
| Owen Anderson | 897aed9 | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 122 | MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList); | 
| Bill Wendling | 406fdbd | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 123 |  | 
| Evan Cheng | 9e17872 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 124 | while (!WorkList.empty()) { | 
|  | 125 | MachineBasicBlock *Pred = WorkList.back(); | 
|  | 126 | WorkList.pop_back(); | 
| Owen Anderson | 897aed9 | 2008-01-15 22:58:11 +0000 | [diff] [blame] | 127 | MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList); | 
| Evan Cheng | 9e17872 | 2007-05-08 19:00:00 +0000 | [diff] [blame] | 128 | } | 
|  | 129 | } | 
|  | 130 |  | 
| Owen Anderson | 1ba66e0 | 2008-01-15 22:02:46 +0000 | [diff] [blame] | 131 | void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB, | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 132 | MachineInstr &MI) { | 
| Evan Cheng | d861606 | 2008-04-02 18:04:08 +0000 | [diff] [blame] | 133 | assert(MRI->getVRegDef(reg) && "Register use before def!"); | 
| Alkis Evlogimenos | 6a099d4 | 2004-09-01 22:34:52 +0000 | [diff] [blame] | 134 |  | 
| Owen Anderson | 9d86ef1 | 2007-11-08 01:20:48 +0000 | [diff] [blame] | 135 | unsigned BBNum = MBB->getNumber(); | 
|  | 136 |  | 
| Owen Anderson | 1ba66e0 | 2008-01-15 22:02:46 +0000 | [diff] [blame] | 137 | VarInfo& VRInfo = getVarInfo(reg); | 
| Evan Cheng | f6f0433 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 138 |  | 
| Bill Wendling | 59cc159 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 139 | // Check to see if this basic block is already a kill block. | 
| Chris Lattner | aef6c2a | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 140 | if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) { | 
| Bill Wendling | 59cc159 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 141 | // Yes, this register is killed in this basic block already. Increase the | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 142 | // live range by updating the kill instruction. | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 143 | VRInfo.Kills.back() = &MI; | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 144 | return; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | #ifndef NDEBUG | 
|  | 148 | for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) | 
| Chris Lattner | aef6c2a | 2004-07-19 07:04:55 +0000 | [diff] [blame] | 149 | assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!"); | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 150 | #endif | 
|  | 151 |  | 
| Bill Wendling | c44659b | 2008-06-23 23:41:14 +0000 | [diff] [blame] | 152 | // This situation can occur: | 
|  | 153 | // | 
|  | 154 | //     ,------. | 
|  | 155 | //     |      | | 
|  | 156 | //     |      v | 
|  | 157 | //     |   t2 = phi ... t1 ... | 
|  | 158 | //     |      | | 
|  | 159 | //     |      v | 
|  | 160 | //     |   t1 = ... | 
|  | 161 | //     |  ... = ... t1 ... | 
|  | 162 | //     |      | | 
|  | 163 | //     `------' | 
|  | 164 | // | 
|  | 165 | // where there is a use in a PHI node that's a predecessor to the defining | 
|  | 166 | // block. We don't want to mark all predecessors as having the value "alive" | 
|  | 167 | // in this case. | 
|  | 168 | if (MBB == MRI->getVRegDef(reg)->getParent()) return; | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 169 |  | 
| Bill Wendling | 59cc159 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 170 | // Add a new kill entry for this basic block. If this virtual register is | 
|  | 171 | // already marked as alive in this basic block, that means it is alive in at | 
|  | 172 | // least one of the successor blocks, it's not a kill. | 
| Jeffrey Yasskin | 7d287cb | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 173 | if (!VRInfo.AliveBlocks.test(BBNum)) | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 174 | VRInfo.Kills.push_back(&MI); | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 175 |  | 
| Bill Wendling | 406fdbd | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 176 | // Update all dominating blocks to mark them as "known live". | 
| Chris Lattner | c49a9a5 | 2004-05-01 21:24:24 +0000 | [diff] [blame] | 177 | for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), | 
|  | 178 | E = MBB->pred_end(); PI != E; ++PI) | 
| Evan Cheng | d861606 | 2008-04-02 18:04:08 +0000 | [diff] [blame] | 179 | MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(reg)->getParent(), *PI); | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 182 | void LiveVariables::HandleVirtRegDef(unsigned Reg, MachineInstr &MI) { | 
| Dan Gohman | ae9d9f4 | 2008-09-21 21:11:41 +0000 | [diff] [blame] | 183 | VarInfo &VRInfo = getVarInfo(Reg); | 
|  | 184 |  | 
| Jeffrey Yasskin | 7d287cb | 2009-05-26 18:27:15 +0000 | [diff] [blame] | 185 | if (VRInfo.AliveBlocks.empty()) | 
| Dan Gohman | ae9d9f4 | 2008-09-21 21:11:41 +0000 | [diff] [blame] | 186 | // If vr is not alive in any block, then defaults to dead. | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 187 | VRInfo.Kills.push_back(&MI); | 
| Dan Gohman | ae9d9f4 | 2008-09-21 21:11:41 +0000 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 190 | /// FindLastPartialDef - Return the last partial def of the specified register. | 
| Evan Cheng | 08d1e41 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 191 | /// Also returns the sub-registers that're defined by the instruction. | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 192 | MachineInstr *LiveVariables::FindLastPartialDef(unsigned Reg, | 
| Evan Cheng | 08d1e41 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 193 | SmallSet<unsigned,4> &PartDefRegs) { | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 194 | unsigned LastDefReg = 0; | 
|  | 195 | unsigned LastDefDist = 0; | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 196 | MachineInstr *LastDef = nullptr; | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 197 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { | 
|  | 198 | unsigned SubReg = *SubRegs; | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 199 | MachineInstr *Def = PhysRegDef[SubReg]; | 
|  | 200 | if (!Def) | 
|  | 201 | continue; | 
|  | 202 | unsigned Dist = DistanceMap[Def]; | 
|  | 203 | if (Dist > LastDefDist) { | 
|  | 204 | LastDefReg  = SubReg; | 
|  | 205 | LastDef     = Def; | 
|  | 206 | LastDefDist = Dist; | 
|  | 207 | } | 
|  | 208 | } | 
| Evan Cheng | 08d1e41 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 209 |  | 
|  | 210 | if (!LastDef) | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 211 | return nullptr; | 
| Evan Cheng | 08d1e41 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 212 |  | 
|  | 213 | PartDefRegs.insert(LastDefReg); | 
|  | 214 | for (unsigned i = 0, e = LastDef->getNumOperands(); i != e; ++i) { | 
|  | 215 | MachineOperand &MO = LastDef->getOperand(i); | 
|  | 216 | if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0) | 
|  | 217 | continue; | 
|  | 218 | unsigned DefReg = MO.getReg(); | 
|  | 219 | if (TRI->isSubRegister(Reg, DefReg)) { | 
| Chad Rosier | c7505ef | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 220 | for (MCSubRegIterator SubRegs(DefReg, TRI, /*IncludeSelf=*/true); | 
|  | 221 | SubRegs.isValid(); ++SubRegs) | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 222 | PartDefRegs.insert(*SubRegs); | 
| Evan Cheng | 08d1e41 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 223 | } | 
|  | 224 | } | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 225 | return LastDef; | 
|  | 226 | } | 
|  | 227 |  | 
| Bill Wendling | b912351 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 228 | /// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add | 
|  | 229 | /// implicit defs to a machine instruction if there was an earlier def of its | 
|  | 230 | /// super-register. | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 231 | void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr &MI) { | 
| Evan Cheng | d190b82 | 2009-11-13 20:36:40 +0000 | [diff] [blame] | 232 | MachineInstr *LastDef = PhysRegDef[Reg]; | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 233 | // If there was a previous use or a "full" def all is well. | 
| Evan Cheng | d190b82 | 2009-11-13 20:36:40 +0000 | [diff] [blame] | 234 | if (!LastDef && !PhysRegUse[Reg]) { | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 235 | // Otherwise, the last sub-register def implicitly defines this register. | 
|  | 236 | // e.g. | 
|  | 237 | // AH = | 
| Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 238 | // AL = ... implicit-def EAX, implicit killed AH | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 239 | //    = AH | 
|  | 240 | // ... | 
|  | 241 | //    = EAX | 
|  | 242 | // All of the sub-registers must have been defined before the use of Reg! | 
| Evan Cheng | 08d1e41 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 243 | SmallSet<unsigned, 4> PartDefRegs; | 
|  | 244 | MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefRegs); | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 245 | // If LastPartialDef is NULL, it must be using a livein register. | 
|  | 246 | if (LastPartialDef) { | 
|  | 247 | LastPartialDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/, | 
|  | 248 | true/*IsImp*/)); | 
|  | 249 | PhysRegDef[Reg] = LastPartialDef; | 
| Owen Anderson | 1473812 | 2008-08-14 23:41:38 +0000 | [diff] [blame] | 250 | SmallSet<unsigned, 8> Processed; | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 251 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { | 
|  | 252 | unsigned SubReg = *SubRegs; | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 253 | if (Processed.count(SubReg)) | 
|  | 254 | continue; | 
| Evan Cheng | 08d1e41 | 2009-09-22 08:34:46 +0000 | [diff] [blame] | 255 | if (PartDefRegs.count(SubReg)) | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 256 | continue; | 
|  | 257 | // This part of Reg was defined before the last partial def. It's killed | 
|  | 258 | // here. | 
|  | 259 | LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg, | 
|  | 260 | false/*IsDef*/, | 
|  | 261 | true/*IsImp*/)); | 
|  | 262 | PhysRegDef[SubReg] = LastPartialDef; | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 263 | for (MCSubRegIterator SS(SubReg, TRI); SS.isValid(); ++SS) | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 264 | Processed.insert(*SS); | 
|  | 265 | } | 
|  | 266 | } | 
| Evan Cheng | 6bb9525 | 2012-01-14 01:53:46 +0000 | [diff] [blame] | 267 | } else if (LastDef && !PhysRegUse[Reg] && | 
|  | 268 | !LastDef->findRegisterDefOperand(Reg)) | 
| Evan Cheng | d190b82 | 2009-11-13 20:36:40 +0000 | [diff] [blame] | 269 | // Last def defines the super register, add an implicit def of reg. | 
| Evan Cheng | 6bb9525 | 2012-01-14 01:53:46 +0000 | [diff] [blame] | 270 | LastDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/, | 
|  | 271 | true/*IsImp*/)); | 
| Bill Wendling | 59cc159 | 2008-02-20 06:10:21 +0000 | [diff] [blame] | 272 |  | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 273 | // Remember this use. | 
| Chad Rosier | c7505ef | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 274 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); | 
|  | 275 | SubRegs.isValid(); ++SubRegs) | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 276 | PhysRegUse[*SubRegs] = &MI; | 
| Evan Cheng | d8417d9 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Evan Cheng | 1d31fc9 | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 279 | /// FindLastRefOrPartRef - Return the last reference or partial reference of | 
|  | 280 | /// the specified register. | 
|  | 281 | MachineInstr *LiveVariables::FindLastRefOrPartRef(unsigned Reg) { | 
|  | 282 | MachineInstr *LastDef = PhysRegDef[Reg]; | 
|  | 283 | MachineInstr *LastUse = PhysRegUse[Reg]; | 
|  | 284 | if (!LastDef && !LastUse) | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 285 | return nullptr; | 
| Evan Cheng | 1d31fc9 | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 286 |  | 
|  | 287 | MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef; | 
|  | 288 | unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef]; | 
| Evan Cheng | 1d31fc9 | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 289 | unsigned LastPartDefDist = 0; | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 290 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { | 
|  | 291 | unsigned SubReg = *SubRegs; | 
| Evan Cheng | 1d31fc9 | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 292 | MachineInstr *Def = PhysRegDef[SubReg]; | 
|  | 293 | if (Def && Def != LastDef) { | 
|  | 294 | // There was a def of this sub-register in between. This is a partial | 
|  | 295 | // def, keep track of the last one. | 
|  | 296 | unsigned Dist = DistanceMap[Def]; | 
| Benjamin Kramer | d64b952 | 2010-01-07 17:29:08 +0000 | [diff] [blame] | 297 | if (Dist > LastPartDefDist) | 
| Evan Cheng | 1d31fc9 | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 298 | LastPartDefDist = Dist; | 
| Benjamin Kramer | d64b952 | 2010-01-07 17:29:08 +0000 | [diff] [blame] | 299 | } else if (MachineInstr *Use = PhysRegUse[SubReg]) { | 
| Evan Cheng | 1d31fc9 | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 300 | unsigned Dist = DistanceMap[Use]; | 
|  | 301 | if (Dist > LastRefOrPartRefDist) { | 
|  | 302 | LastRefOrPartRefDist = Dist; | 
|  | 303 | LastRefOrPartRef = Use; | 
|  | 304 | } | 
|  | 305 | } | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | return LastRefOrPartRef; | 
|  | 309 | } | 
|  | 310 |  | 
| Evan Cheng | f1e873a | 2009-01-20 21:25:12 +0000 | [diff] [blame] | 311 | bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *MI) { | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 312 | MachineInstr *LastDef = PhysRegDef[Reg]; | 
|  | 313 | MachineInstr *LastUse = PhysRegUse[Reg]; | 
|  | 314 | if (!LastDef && !LastUse) | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 315 | return false; | 
|  | 316 |  | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 317 | MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef; | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 318 | unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef]; | 
|  | 319 | // The whole register is used. | 
|  | 320 | // AL = | 
|  | 321 | // AH = | 
|  | 322 | // | 
|  | 323 | //    = AX | 
| Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 324 | //    = AL, implicit killed AX | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 325 | // AX = | 
|  | 326 | // | 
|  | 327 | // Or whole register is defined, but not used at all. | 
| Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 328 | // dead AX = | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 329 | // ... | 
|  | 330 | // AX = | 
|  | 331 | // | 
|  | 332 | // Or whole register is defined, but only partly used. | 
| Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 333 | // dead AX = implicit-def AL | 
|  | 334 | //    = killed AL | 
| Andrew Trick | 808a7a6 | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 335 | // AX = | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 336 | MachineInstr *LastPartDef = nullptr; | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 337 | unsigned LastPartDefDist = 0; | 
| Owen Anderson | 1473812 | 2008-08-14 23:41:38 +0000 | [diff] [blame] | 338 | SmallSet<unsigned, 8> PartUses; | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 339 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { | 
|  | 340 | unsigned SubReg = *SubRegs; | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 341 | MachineInstr *Def = PhysRegDef[SubReg]; | 
|  | 342 | if (Def && Def != LastDef) { | 
|  | 343 | // There was a def of this sub-register in between. This is a partial | 
|  | 344 | // def, keep track of the last one. | 
|  | 345 | unsigned Dist = DistanceMap[Def]; | 
|  | 346 | if (Dist > LastPartDefDist) { | 
|  | 347 | LastPartDefDist = Dist; | 
|  | 348 | LastPartDef = Def; | 
|  | 349 | } | 
|  | 350 | continue; | 
|  | 351 | } | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 352 | if (MachineInstr *Use = PhysRegUse[SubReg]) { | 
| Chad Rosier | c7505ef | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 353 | for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true); SS.isValid(); | 
|  | 354 | ++SS) | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 355 | PartUses.insert(*SS); | 
|  | 356 | unsigned Dist = DistanceMap[Use]; | 
|  | 357 | if (Dist > LastRefOrPartRefDist) { | 
|  | 358 | LastRefOrPartRefDist = Dist; | 
|  | 359 | LastRefOrPartRef = Use; | 
| Evan Cheng | d8417d9 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 360 | } | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 361 | } | 
|  | 362 | } | 
| Evan Cheng | f1e873a | 2009-01-20 21:25:12 +0000 | [diff] [blame] | 363 |  | 
| Jakob Stoklund Olesen | 2664d29 | 2010-03-05 21:49:17 +0000 | [diff] [blame] | 364 | if (!PhysRegUse[Reg]) { | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 365 | // Partial uses. Mark register def dead and add implicit def of | 
|  | 366 | // sub-registers which are used. | 
| Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 367 | // dead EAX  = op  implicit-def AL | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 368 | // That is, EAX def is dead but AL def extends pass it. | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 369 | PhysRegDef[Reg]->addRegisterDead(Reg, TRI, true); | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 370 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { | 
|  | 371 | unsigned SubReg = *SubRegs; | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 372 | if (!PartUses.count(SubReg)) | 
|  | 373 | continue; | 
|  | 374 | bool NeedDef = true; | 
|  | 375 | if (PhysRegDef[Reg] == PhysRegDef[SubReg]) { | 
|  | 376 | MachineOperand *MO = PhysRegDef[Reg]->findRegisterDefOperand(SubReg); | 
|  | 377 | if (MO) { | 
|  | 378 | NeedDef = false; | 
|  | 379 | assert(!MO->isDead()); | 
| Evan Cheng | ba2410b | 2009-07-06 21:34:05 +0000 | [diff] [blame] | 380 | } | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 381 | } | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 382 | if (NeedDef) | 
|  | 383 | PhysRegDef[Reg]->addOperand(MachineOperand::CreateReg(SubReg, | 
|  | 384 | true/*IsDef*/, true/*IsImp*/)); | 
| Evan Cheng | 1d31fc9 | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 385 | MachineInstr *LastSubRef = FindLastRefOrPartRef(SubReg); | 
|  | 386 | if (LastSubRef) | 
|  | 387 | LastSubRef->addRegisterKilled(SubReg, TRI, true); | 
|  | 388 | else { | 
|  | 389 | LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true); | 
| Chad Rosier | c7505ef | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 390 | for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true); | 
|  | 391 | SS.isValid(); ++SS) | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 392 | PhysRegUse[*SS] = LastRefOrPartRef; | 
| Evan Cheng | 1d31fc9 | 2009-12-01 00:44:45 +0000 | [diff] [blame] | 393 | } | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 394 | for (MCSubRegIterator SS(SubReg, TRI); SS.isValid(); ++SS) | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 395 | PartUses.erase(*SS); | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 396 | } | 
| Jakob Stoklund Olesen | 2664d29 | 2010-03-05 21:49:17 +0000 | [diff] [blame] | 397 | } else if (LastRefOrPartRef == PhysRegDef[Reg] && LastRefOrPartRef != MI) { | 
|  | 398 | if (LastPartDef) | 
|  | 399 | // The last partial def kills the register. | 
|  | 400 | LastPartDef->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/, | 
|  | 401 | true/*IsImp*/, true/*IsKill*/)); | 
|  | 402 | else { | 
|  | 403 | MachineOperand *MO = | 
|  | 404 | LastRefOrPartRef->findRegisterDefOperand(Reg, false, TRI); | 
|  | 405 | bool NeedEC = MO->isEarlyClobber() && MO->getReg() != Reg; | 
|  | 406 | // If the last reference is the last def, then it's not used at all. | 
|  | 407 | // That is, unless we are currently processing the last reference itself. | 
|  | 408 | LastRefOrPartRef->addRegisterDead(Reg, TRI, true); | 
|  | 409 | if (NeedEC) { | 
|  | 410 | // If we are adding a subreg def and the superreg def is marked early | 
|  | 411 | // clobber, add an early clobber marker to the subreg def. | 
|  | 412 | MO = LastRefOrPartRef->findRegisterDefOperand(Reg); | 
|  | 413 | if (MO) | 
|  | 414 | MO->setIsEarlyClobber(); | 
|  | 415 | } | 
|  | 416 | } | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 417 | } else | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 418 | LastRefOrPartRef->addRegisterKilled(Reg, TRI, true); | 
|  | 419 | return true; | 
|  | 420 | } | 
|  | 421 |  | 
| Jakob Stoklund Olesen | 8e3bb31 | 2012-01-21 00:58:53 +0000 | [diff] [blame] | 422 | void LiveVariables::HandleRegMask(const MachineOperand &MO) { | 
|  | 423 | // Call HandlePhysRegKill() for all live registers clobbered by Mask. | 
|  | 424 | // Clobbered registers are always dead, sp there is no need to use | 
|  | 425 | // HandlePhysRegDef(). | 
|  | 426 | for (unsigned Reg = 1, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg) { | 
|  | 427 | // Skip dead regs. | 
|  | 428 | if (!PhysRegDef[Reg] && !PhysRegUse[Reg]) | 
|  | 429 | continue; | 
|  | 430 | // Skip mask-preserved regs. | 
| Evan Cheng | 64a2bec | 2012-01-21 03:31:03 +0000 | [diff] [blame] | 431 | if (!MO.clobbersPhysReg(Reg)) | 
| Jakob Stoklund Olesen | 8e3bb31 | 2012-01-21 00:58:53 +0000 | [diff] [blame] | 432 | continue; | 
|  | 433 | // Kill the largest clobbered super-register. | 
|  | 434 | // This avoids needless implicit operands. | 
|  | 435 | unsigned Super = Reg; | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 436 | for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) | 
| Jakob Stoklund Olesen | 8e3bb31 | 2012-01-21 00:58:53 +0000 | [diff] [blame] | 437 | if ((PhysRegDef[*SR] || PhysRegUse[*SR]) && MO.clobbersPhysReg(*SR)) | 
|  | 438 | Super = *SR; | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 439 | HandlePhysRegKill(Super, nullptr); | 
| Jakob Stoklund Olesen | 8e3bb31 | 2012-01-21 00:58:53 +0000 | [diff] [blame] | 440 | } | 
|  | 441 | } | 
|  | 442 |  | 
| Evan Cheng | 262f86e | 2009-09-23 06:28:31 +0000 | [diff] [blame] | 443 | void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI, | 
| Craig Topper | 2cd5ff8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 444 | SmallVectorImpl<unsigned> &Defs) { | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 445 | // What parts of the register are previously defined? | 
| Owen Anderson | 413f7d9 | 2008-06-27 07:05:59 +0000 | [diff] [blame] | 446 | SmallSet<unsigned, 32> Live; | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 447 | if (PhysRegDef[Reg] || PhysRegUse[Reg]) { | 
| Chad Rosier | c7505ef | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 448 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); | 
|  | 449 | SubRegs.isValid(); ++SubRegs) | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 450 | Live.insert(*SubRegs); | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 451 | } else { | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 452 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { | 
|  | 453 | unsigned SubReg = *SubRegs; | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 454 | // If a register isn't itself defined, but all parts that make up of it | 
|  | 455 | // are defined, then consider it also defined. | 
|  | 456 | // e.g. | 
|  | 457 | // AL = | 
|  | 458 | // AH = | 
|  | 459 | //    = AX | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 460 | if (Live.count(SubReg)) | 
|  | 461 | continue; | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 462 | if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) { | 
| Chad Rosier | c7505ef | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 463 | for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true); | 
|  | 464 | SS.isValid(); ++SS) | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 465 | Live.insert(*SS); | 
|  | 466 | } | 
| Bill Wendling | 406fdbd | 2008-02-20 07:36:31 +0000 | [diff] [blame] | 467 | } | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 468 | } | 
| Alkis Evlogimenos | ebbd66c | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 469 |  | 
| Evan Cheng | e45b8f8 | 2008-04-16 09:46:40 +0000 | [diff] [blame] | 470 | // Start from the largest piece, find the last time any part of the register | 
|  | 471 | // is referenced. | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 472 | HandlePhysRegKill(Reg, MI); | 
|  | 473 | // Only some of the sub-registers are used. | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 474 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { | 
|  | 475 | unsigned SubReg = *SubRegs; | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 476 | if (!Live.count(SubReg)) | 
|  | 477 | // Skip if this sub-register isn't defined. | 
|  | 478 | continue; | 
|  | 479 | HandlePhysRegKill(SubReg, MI); | 
| Evan Cheng | 7818c03 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 480 | } | 
|  | 481 |  | 
| Evan Cheng | a21aac3 | 2009-09-24 02:15:22 +0000 | [diff] [blame] | 482 | if (MI) | 
|  | 483 | Defs.push_back(Reg);  // Remember this def. | 
| Evan Cheng | 262f86e | 2009-09-23 06:28:31 +0000 | [diff] [blame] | 484 | } | 
|  | 485 |  | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 486 | void LiveVariables::UpdatePhysRegDefs(MachineInstr &MI, | 
| Craig Topper | 2cd5ff8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 487 | SmallVectorImpl<unsigned> &Defs) { | 
| Evan Cheng | 262f86e | 2009-09-23 06:28:31 +0000 | [diff] [blame] | 488 | while (!Defs.empty()) { | 
|  | 489 | unsigned Reg = Defs.back(); | 
|  | 490 | Defs.pop_back(); | 
| Chad Rosier | c7505ef | 2013-05-22 22:26:05 +0000 | [diff] [blame] | 491 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); | 
|  | 492 | SubRegs.isValid(); ++SubRegs) { | 
| Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 493 | unsigned SubReg = *SubRegs; | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 494 | PhysRegDef[SubReg] = &MI; | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 495 | PhysRegUse[SubReg]  = nullptr; | 
| Evan Cheng | d8417d9 | 2007-06-26 21:03:35 +0000 | [diff] [blame] | 496 | } | 
| Alkis Evlogimenos | ebbd66c | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 497 | } | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 498 | } | 
|  | 499 |  | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 500 | void LiveVariables::runOnInstr(MachineInstr &MI, | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 501 | SmallVectorImpl<unsigned> &Defs) { | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 502 | assert(!MI.isDebugValue()); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 503 | // Process all of the operands of the instruction... | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 504 | unsigned NumOperandsToProcess = MI.getNumOperands(); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 505 |  | 
|  | 506 | // Unless it is a PHI node.  In this case, ONLY process the DEF, not any | 
|  | 507 | // of the uses.  They will be handled in other basic blocks. | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 508 | if (MI.isPHI()) | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 509 | NumOperandsToProcess = 1; | 
|  | 510 |  | 
|  | 511 | // Clear kill and dead markers. LV will recompute them. | 
|  | 512 | SmallVector<unsigned, 4> UseRegs; | 
|  | 513 | SmallVector<unsigned, 4> DefRegs; | 
|  | 514 | SmallVector<unsigned, 1> RegMasks; | 
|  | 515 | for (unsigned i = 0; i != NumOperandsToProcess; ++i) { | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 516 | MachineOperand &MO = MI.getOperand(i); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 517 | if (MO.isRegMask()) { | 
|  | 518 | RegMasks.push_back(i); | 
|  | 519 | continue; | 
|  | 520 | } | 
|  | 521 | if (!MO.isReg() || MO.getReg() == 0) | 
|  | 522 | continue; | 
|  | 523 | unsigned MOReg = MO.getReg(); | 
|  | 524 | if (MO.isUse()) { | 
| Matthias Braun | 147110d | 2015-11-24 20:06:56 +0000 | [diff] [blame] | 525 | if (!(TargetRegisterInfo::isPhysicalRegister(MOReg) && | 
|  | 526 | MRI->isReserved(MOReg))) | 
|  | 527 | MO.setIsKill(false); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 528 | if (MO.readsReg()) | 
|  | 529 | UseRegs.push_back(MOReg); | 
| Matthias Braun | 82cff88 | 2016-03-29 03:08:18 +0000 | [diff] [blame] | 530 | } else { | 
|  | 531 | assert(MO.isDef()); | 
| Matthias Braun | 1c20c82 | 2016-03-29 19:07:40 +0000 | [diff] [blame] | 532 | // FIXME: We should not remove any dead flags. However the MIPS RDDSP | 
|  | 533 | // instruction needs it at the moment: http://llvm.org/PR27116. | 
| Matthias Braun | 82cff88 | 2016-03-29 03:08:18 +0000 | [diff] [blame] | 534 | if (TargetRegisterInfo::isPhysicalRegister(MOReg) && | 
|  | 535 | !MRI->isReserved(MOReg)) | 
| Matthias Braun | 147110d | 2015-11-24 20:06:56 +0000 | [diff] [blame] | 536 | MO.setIsDead(false); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 537 | DefRegs.push_back(MOReg); | 
|  | 538 | } | 
|  | 539 | } | 
|  | 540 |  | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 541 | MachineBasicBlock *MBB = MI.getParent(); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 542 | // Process all uses. | 
|  | 543 | for (unsigned i = 0, e = UseRegs.size(); i != e; ++i) { | 
|  | 544 | unsigned MOReg = UseRegs[i]; | 
|  | 545 | if (TargetRegisterInfo::isVirtualRegister(MOReg)) | 
|  | 546 | HandleVirtRegUse(MOReg, MBB, MI); | 
|  | 547 | else if (!MRI->isReserved(MOReg)) | 
|  | 548 | HandlePhysRegUse(MOReg, MI); | 
|  | 549 | } | 
|  | 550 |  | 
|  | 551 | // Process all masked registers. (Call clobbers). | 
|  | 552 | for (unsigned i = 0, e = RegMasks.size(); i != e; ++i) | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 553 | HandleRegMask(MI.getOperand(RegMasks[i])); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 554 |  | 
|  | 555 | // Process all defs. | 
|  | 556 | for (unsigned i = 0, e = DefRegs.size(); i != e; ++i) { | 
|  | 557 | unsigned MOReg = DefRegs[i]; | 
|  | 558 | if (TargetRegisterInfo::isVirtualRegister(MOReg)) | 
|  | 559 | HandleVirtRegDef(MOReg, MI); | 
|  | 560 | else if (!MRI->isReserved(MOReg)) | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 561 | HandlePhysRegDef(MOReg, &MI, Defs); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 562 | } | 
|  | 563 | UpdatePhysRegDefs(MI, Defs); | 
|  | 564 | } | 
|  | 565 |  | 
|  | 566 | void LiveVariables::runOnBlock(MachineBasicBlock *MBB, const unsigned NumRegs) { | 
|  | 567 | // Mark live-in registers as live-in. | 
|  | 568 | SmallVector<unsigned, 4> Defs; | 
| Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 569 | for (const auto &LI : MBB->liveins()) { | 
|  | 570 | assert(TargetRegisterInfo::isPhysicalRegister(LI.PhysReg) && | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 571 | "Cannot have a live-in virtual register!"); | 
| Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 572 | HandlePhysRegDef(LI.PhysReg, nullptr, Defs); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 573 | } | 
|  | 574 |  | 
|  | 575 | // Loop over all of the instructions, processing them. | 
|  | 576 | DistanceMap.clear(); | 
|  | 577 | unsigned Dist = 0; | 
| Duncan P. N. Exon Smith | 07acb3e | 2016-06-30 23:33:35 +0000 | [diff] [blame] | 578 | for (MachineInstr &MI : *MBB) { | 
|  | 579 | if (MI.isDebugValue()) | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 580 | continue; | 
| Duncan P. N. Exon Smith | 07acb3e | 2016-06-30 23:33:35 +0000 | [diff] [blame] | 581 | DistanceMap.insert(std::make_pair(&MI, Dist++)); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 582 |  | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 583 | runOnInstr(MI, Defs); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 584 | } | 
|  | 585 |  | 
|  | 586 | // Handle any virtual assignments from PHI nodes which might be at the | 
|  | 587 | // bottom of this basic block.  We check all of our successor blocks to see | 
|  | 588 | // if they have PHI nodes, and if so, we simulate an assignment at the end | 
|  | 589 | // of the current block. | 
|  | 590 | if (!PHIVarInfo[MBB->getNumber()].empty()) { | 
|  | 591 | SmallVectorImpl<unsigned> &VarInfoVec = PHIVarInfo[MBB->getNumber()]; | 
|  | 592 |  | 
|  | 593 | for (SmallVectorImpl<unsigned>::iterator I = VarInfoVec.begin(), | 
|  | 594 | E = VarInfoVec.end(); I != E; ++I) | 
|  | 595 | // Mark it alive only in the block we are representing. | 
|  | 596 | MarkVirtRegAliveInBlock(getVarInfo(*I),MRI->getVRegDef(*I)->getParent(), | 
|  | 597 | MBB); | 
|  | 598 | } | 
|  | 599 |  | 
|  | 600 | // MachineCSE may CSE instructions which write to non-allocatable physical | 
|  | 601 | // registers across MBBs. Remember if any reserved register is liveout. | 
|  | 602 | SmallSet<unsigned, 4> LiveOuts; | 
|  | 603 | for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(), | 
|  | 604 | SE = MBB->succ_end(); SI != SE; ++SI) { | 
|  | 605 | MachineBasicBlock *SuccMBB = *SI; | 
| Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 606 | if (SuccMBB->isEHPad()) | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 607 | continue; | 
| Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 608 | for (const auto &LI : SuccMBB->liveins()) { | 
|  | 609 | if (!TRI->isInAllocatableClass(LI.PhysReg)) | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 610 | // Ignore other live-ins, e.g. those that are live into landing pads. | 
| Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 611 | LiveOuts.insert(LI.PhysReg); | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 612 | } | 
|  | 613 | } | 
|  | 614 |  | 
|  | 615 | // Loop over PhysRegDef / PhysRegUse, killing any registers that are | 
|  | 616 | // available at the end of the basic block. | 
|  | 617 | for (unsigned i = 0; i != NumRegs; ++i) | 
|  | 618 | if ((PhysRegDef[i] || PhysRegUse[i]) && !LiveOuts.count(i)) | 
|  | 619 | HandlePhysRegDef(i, nullptr, Defs); | 
|  | 620 | } | 
|  | 621 |  | 
| Evan Cheng | f6f0433 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 622 | bool LiveVariables::runOnMachineFunction(MachineFunction &mf) { | 
|  | 623 | MF = &mf; | 
| Evan Cheng | d861606 | 2008-04-02 18:04:08 +0000 | [diff] [blame] | 624 | MRI = &mf.getRegInfo(); | 
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 625 | TRI = MF->getSubtarget().getRegisterInfo(); | 
| Chris Lattner | 2640738 | 2004-02-09 01:35:21 +0000 | [diff] [blame] | 626 |  | 
| Dylan Noblesmith | 46a922c | 2014-08-25 01:59:42 +0000 | [diff] [blame] | 627 | const unsigned NumRegs = TRI->getNumRegs(); | 
| Dylan Noblesmith | 17f05a3 | 2014-08-26 02:03:25 +0000 | [diff] [blame] | 628 | PhysRegDef.assign(NumRegs, nullptr); | 
|  | 629 | PhysRegUse.assign(NumRegs, nullptr); | 
| Dylan Noblesmith | 46a922c | 2014-08-25 01:59:42 +0000 | [diff] [blame] | 630 | PHIVarInfo.resize(MF->getNumBlockIDs()); | 
| Jakob Stoklund Olesen | 38b76e2 | 2010-02-23 22:43:58 +0000 | [diff] [blame] | 631 | PHIJoins.clear(); | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 632 |  | 
| Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 633 | // FIXME: LiveIntervals will be updated to remove its dependence on | 
|  | 634 | // LiveVariables to improve compilation time and eliminate bizarre pass | 
|  | 635 | // dependencies. Until then, we can't change much in -O0. | 
|  | 636 | if (!MRI->isSSA()) | 
|  | 637 | report_fatal_error("regalloc=... not currently supported with -O0"); | 
|  | 638 |  | 
| Evan Cheng | f6f0433 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 639 | analyzePHINodes(mf); | 
| Bill Wendling | 984f0ce | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 640 |  | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 641 | // Calculate live variable information in depth first order on the CFG of the | 
|  | 642 | // function.  This guarantees that we will see the definition of a virtual | 
|  | 643 | // register before its uses due to dominance properties of SSA (except for PHI | 
|  | 644 | // nodes, which are treated as a special case). | 
| Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 645 | MachineBasicBlock *Entry = &MF->front(); | 
| David Callahan | c1051ab | 2016-10-05 21:36:16 +0000 | [diff] [blame] | 646 | df_iterator_default_set<MachineBasicBlock*,16> Visited; | 
| Bill Wendling | b912351 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 647 |  | 
| Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 648 | for (MachineBasicBlock *MBB : depth_first_ext(Entry, Visited)) { | 
| Dylan Noblesmith | 6e69927 | 2014-08-25 01:59:49 +0000 | [diff] [blame] | 649 | runOnBlock(MBB, NumRegs); | 
| Evan Cheng | 7818c03 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 650 |  | 
| Dylan Noblesmith | 17f05a3 | 2014-08-26 02:03:25 +0000 | [diff] [blame] | 651 | PhysRegDef.assign(NumRegs, nullptr); | 
|  | 652 | PhysRegUse.assign(NumRegs, nullptr); | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 653 | } | 
|  | 654 |  | 
| Evan Cheng | 70ec528 | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 655 | // Convert and transfer the dead / killed information we have gathered into | 
|  | 656 | // VirtRegInfo onto MI's. | 
| Jakob Stoklund Olesen | 28d7669 | 2011-01-08 23:10:57 +0000 | [diff] [blame] | 657 | for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) { | 
|  | 658 | const unsigned Reg = TargetRegisterInfo::index2VirtReg(i); | 
|  | 659 | for (unsigned j = 0, e2 = VirtRegInfo[Reg].Kills.size(); j != e2; ++j) | 
|  | 660 | if (VirtRegInfo[Reg].Kills[j] == MRI->getVRegDef(Reg)) | 
|  | 661 | VirtRegInfo[Reg].Kills[j]->addRegisterDead(Reg, TRI); | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 662 | else | 
| Jakob Stoklund Olesen | 28d7669 | 2011-01-08 23:10:57 +0000 | [diff] [blame] | 663 | VirtRegInfo[Reg].Kills[j]->addRegisterKilled(Reg, TRI); | 
|  | 664 | } | 
| Chris Lattner | 7c77fd5 | 2004-07-01 04:24:29 +0000 | [diff] [blame] | 665 |  | 
| Chris Lattner | d47909e | 2004-07-09 16:44:37 +0000 | [diff] [blame] | 666 | // Check to make sure there are no unreachable blocks in the MC CFG for the | 
|  | 667 | // function.  If so, it is due to a bug in the instruction selector or some | 
|  | 668 | // other part of the code generator if this happens. | 
|  | 669 | #ifndef NDEBUG | 
| Evan Cheng | f6f0433 | 2007-03-17 09:29:54 +0000 | [diff] [blame] | 670 | for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i) | 
| Chris Lattner | d47909e | 2004-07-09 16:44:37 +0000 | [diff] [blame] | 671 | assert(Visited.count(&*i) != 0 && "unreachable basic block found"); | 
|  | 672 | #endif | 
|  | 673 |  | 
| Dylan Noblesmith | 46a922c | 2014-08-25 01:59:42 +0000 | [diff] [blame] | 674 | PhysRegDef.clear(); | 
|  | 675 | PhysRegUse.clear(); | 
|  | 676 | PHIVarInfo.clear(); | 
| Evan Cheng | 0fbe14a | 2007-04-25 19:34:00 +0000 | [diff] [blame] | 677 |  | 
| Chris Lattner | cab0b44 | 2003-01-13 20:01:16 +0000 | [diff] [blame] | 678 | return false; | 
|  | 679 | } | 
| Chris Lattner | afa9d7e | 2004-02-19 18:28:02 +0000 | [diff] [blame] | 680 |  | 
| Evan Cheng | 7a265d8 | 2008-07-03 00:07:19 +0000 | [diff] [blame] | 681 | /// replaceKillInstruction - Update register kill info by replacing a kill | 
|  | 682 | /// instruction with a new one. | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 683 | void LiveVariables::replaceKillInstruction(unsigned Reg, MachineInstr &OldMI, | 
|  | 684 | MachineInstr &NewMI) { | 
| Evan Cheng | 7a265d8 | 2008-07-03 00:07:19 +0000 | [diff] [blame] | 685 | VarInfo &VI = getVarInfo(Reg); | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 686 | std::replace(VI.Kills.begin(), VI.Kills.end(), &OldMI, &NewMI); | 
| Evan Cheng | 7a265d8 | 2008-07-03 00:07:19 +0000 | [diff] [blame] | 687 | } | 
|  | 688 |  | 
| Chris Lattner | f8f724a | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 689 | /// removeVirtualRegistersKilled - Remove all killed info for the specified | 
|  | 690 | /// instruction. | 
| Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 691 | void LiveVariables::removeVirtualRegistersKilled(MachineInstr &MI) { | 
|  | 692 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { | 
|  | 693 | MachineOperand &MO = MI.getOperand(i); | 
| Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 694 | if (MO.isReg() && MO.isKill()) { | 
| Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 695 | MO.setIsKill(false); | 
| Evan Cheng | 70ec528 | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 696 | unsigned Reg = MO.getReg(); | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 697 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { | 
| Evan Cheng | 70ec528 | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 698 | bool removed = getVarInfo(Reg).removeKill(MI); | 
|  | 699 | assert(removed && "kill not in register's VarInfo?"); | 
| Duncan Sands | a41634e | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 700 | (void)removed; | 
| Evan Cheng | 70ec528 | 2006-11-15 20:51:59 +0000 | [diff] [blame] | 701 | } | 
| Chris Lattner | f8f724a | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 702 | } | 
|  | 703 | } | 
| Chris Lattner | f8f724a | 2006-09-03 00:05:09 +0000 | [diff] [blame] | 704 | } | 
|  | 705 |  | 
| Bill Wendling | 984f0ce | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 706 | /// analyzePHINodes - Gather information about the PHI nodes in here. In | 
| Bill Wendling | b912351 | 2008-02-20 09:15:16 +0000 | [diff] [blame] | 707 | /// particular, we want to map the variable information of a virtual register | 
|  | 708 | /// which is used in a PHI node. We map that to the BB the vreg is coming from. | 
| Bill Wendling | 984f0ce | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 709 | /// | 
|  | 710 | void LiveVariables::analyzePHINodes(const MachineFunction& Fn) { | 
| Alexey Samsonov | 41b977d | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 711 | for (const auto &MBB : Fn) | 
| Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 712 | for (const auto &BBI : MBB) { | 
|  | 713 | if (!BBI.isPHI()) | 
|  | 714 | break; | 
|  | 715 | for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) | 
|  | 716 | if (BBI.getOperand(i).readsReg()) | 
|  | 717 | PHIVarInfo[BBI.getOperand(i + 1).getMBB()->getNumber()] | 
|  | 718 | .push_back(BBI.getOperand(i).getReg()); | 
|  | 719 | } | 
| Bill Wendling | 984f0ce | 2006-10-03 07:20:20 +0000 | [diff] [blame] | 720 | } | 
| Jakob Stoklund Olesen | 4453dc9 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 721 |  | 
| Jakob Stoklund Olesen | bc630ac | 2009-11-21 02:05:21 +0000 | [diff] [blame] | 722 | bool LiveVariables::VarInfo::isLiveIn(const MachineBasicBlock &MBB, | 
|  | 723 | unsigned Reg, | 
|  | 724 | MachineRegisterInfo &MRI) { | 
|  | 725 | unsigned Num = MBB.getNumber(); | 
|  | 726 |  | 
|  | 727 | // Reg is live-through. | 
|  | 728 | if (AliveBlocks.test(Num)) | 
|  | 729 | return true; | 
|  | 730 |  | 
|  | 731 | // Registers defined in MBB cannot be live in. | 
|  | 732 | const MachineInstr *Def = MRI.getVRegDef(Reg); | 
|  | 733 | if (Def && Def->getParent() == &MBB) | 
|  | 734 | return false; | 
|  | 735 |  | 
|  | 736 | // Reg was not defined in MBB, was it killed here? | 
|  | 737 | return findKill(&MBB); | 
|  | 738 | } | 
|  | 739 |  | 
| Jakob Stoklund Olesen | defc470 | 2009-12-01 17:13:31 +0000 | [diff] [blame] | 740 | bool LiveVariables::isLiveOut(unsigned Reg, const MachineBasicBlock &MBB) { | 
|  | 741 | LiveVariables::VarInfo &VI = getVarInfo(Reg); | 
|  | 742 |  | 
| Arnaud A. de Grandmaison | af37ad1 | 2015-06-11 07:50:21 +0000 | [diff] [blame] | 743 | SmallPtrSet<const MachineBasicBlock *, 8> Kills; | 
|  | 744 | for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i) | 
|  | 745 | Kills.insert(VI.Kills[i]->getParent()); | 
|  | 746 |  | 
| Jakob Stoklund Olesen | defc470 | 2009-12-01 17:13:31 +0000 | [diff] [blame] | 747 | // Loop over all of the successors of the basic block, checking to see if | 
|  | 748 | // the value is either live in the block, or if it is killed in the block. | 
| Arnaud A. de Grandmaison | af37ad1 | 2015-06-11 07:50:21 +0000 | [diff] [blame] | 749 | for (const MachineBasicBlock *SuccMBB : MBB.successors()) { | 
| Jakob Stoklund Olesen | defc470 | 2009-12-01 17:13:31 +0000 | [diff] [blame] | 750 | // Is it alive in this successor? | 
|  | 751 | unsigned SuccIdx = SuccMBB->getNumber(); | 
|  | 752 | if (VI.AliveBlocks.test(SuccIdx)) | 
|  | 753 | return true; | 
| Arnaud A. de Grandmaison | af37ad1 | 2015-06-11 07:50:21 +0000 | [diff] [blame] | 754 | // Or is it live because there is a use in a successor that kills it? | 
|  | 755 | if (Kills.count(SuccMBB)) | 
|  | 756 | return true; | 
| Jakob Stoklund Olesen | defc470 | 2009-12-01 17:13:31 +0000 | [diff] [blame] | 757 | } | 
|  | 758 |  | 
| Jakob Stoklund Olesen | defc470 | 2009-12-01 17:13:31 +0000 | [diff] [blame] | 759 | return false; | 
|  | 760 | } | 
|  | 761 |  | 
| Jakob Stoklund Olesen | 4f7fd3b | 2009-11-11 19:31:31 +0000 | [diff] [blame] | 762 | /// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All | 
|  | 763 | /// variables that are live out of DomBB will be marked as passing live through | 
|  | 764 | /// BB. | 
|  | 765 | void LiveVariables::addNewBlock(MachineBasicBlock *BB, | 
| Jakob Stoklund Olesen | bc630ac | 2009-11-21 02:05:21 +0000 | [diff] [blame] | 766 | MachineBasicBlock *DomBB, | 
|  | 767 | MachineBasicBlock *SuccBB) { | 
| Jakob Stoklund Olesen | 4f7fd3b | 2009-11-11 19:31:31 +0000 | [diff] [blame] | 768 | const unsigned NumNew = BB->getNumber(); | 
| Jakob Stoklund Olesen | bc630ac | 2009-11-21 02:05:21 +0000 | [diff] [blame] | 769 |  | 
| Davide Italiano | 36acbc7 | 2017-05-11 19:37:43 +0000 | [diff] [blame] | 770 | DenseSet<unsigned> Defs, Kills; | 
| Benjamin Kramer | 851c941 | 2012-09-09 11:56:14 +0000 | [diff] [blame] | 771 |  | 
|  | 772 | MachineBasicBlock::iterator BBI = SuccBB->begin(), BBE = SuccBB->end(); | 
|  | 773 | for (; BBI != BBE && BBI->isPHI(); ++BBI) { | 
|  | 774 | // Record the def of the PHI node. | 
|  | 775 | Defs.insert(BBI->getOperand(0).getReg()); | 
|  | 776 |  | 
|  | 777 | // All registers used by PHI nodes in SuccBB must be live through BB. | 
| Jakob Stoklund Olesen | bc630ac | 2009-11-21 02:05:21 +0000 | [diff] [blame] | 778 | for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) | 
|  | 779 | if (BBI->getOperand(i+1).getMBB() == BB) | 
|  | 780 | getVarInfo(BBI->getOperand(i).getReg()).AliveBlocks.set(NumNew); | 
| Benjamin Kramer | 851c941 | 2012-09-09 11:56:14 +0000 | [diff] [blame] | 781 | } | 
|  | 782 |  | 
|  | 783 | // Record all vreg defs and kills of all instructions in SuccBB. | 
|  | 784 | for (; BBI != BBE; ++BBI) { | 
|  | 785 | for (MachineInstr::mop_iterator I = BBI->operands_begin(), | 
|  | 786 | E = BBI->operands_end(); I != E; ++I) { | 
|  | 787 | if (I->isReg() && TargetRegisterInfo::isVirtualRegister(I->getReg())) { | 
|  | 788 | if (I->isDef()) | 
|  | 789 | Defs.insert(I->getReg()); | 
|  | 790 | else if (I->isKill()) | 
|  | 791 | Kills.insert(I->getReg()); | 
|  | 792 | } | 
|  | 793 | } | 
|  | 794 | } | 
| Jakob Stoklund Olesen | 4453dc9 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 795 |  | 
|  | 796 | // Update info for all live variables | 
| Jakob Stoklund Olesen | 28d7669 | 2011-01-08 23:10:57 +0000 | [diff] [blame] | 797 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { | 
|  | 798 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); | 
| Benjamin Kramer | 851c941 | 2012-09-09 11:56:14 +0000 | [diff] [blame] | 799 |  | 
|  | 800 | // If the Defs is defined in the successor it can't be live in BB. | 
|  | 801 | if (Defs.count(Reg)) | 
|  | 802 | continue; | 
|  | 803 |  | 
|  | 804 | // If the register is either killed in or live through SuccBB it's also live | 
|  | 805 | // through BB. | 
| Jakob Stoklund Olesen | 4f7fd3b | 2009-11-11 19:31:31 +0000 | [diff] [blame] | 806 | VarInfo &VI = getVarInfo(Reg); | 
| Benjamin Kramer | 851c941 | 2012-09-09 11:56:14 +0000 | [diff] [blame] | 807 | if (Kills.count(Reg) || VI.AliveBlocks.test(SuccBB->getNumber())) | 
| Jakob Stoklund Olesen | 4f7fd3b | 2009-11-11 19:31:31 +0000 | [diff] [blame] | 808 | VI.AliveBlocks.set(NumNew); | 
| Jakob Stoklund Olesen | 4453dc9 | 2009-11-10 22:01:05 +0000 | [diff] [blame] | 809 | } | 
|  | 810 | } |