Jim Grosbach | 905c952 | 2012-02-17 17:35:10 +0000 | [diff] [blame] | 1 | //===-- LiveRangeEdit.cpp - Basic tools for editing a register live range -===// |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The LiveRangeEdit class represents changes done to a virtual register when it |
| 11 | // is spilled or split. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/LiveRangeEdit.h" |
Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
| 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/VirtRegMap.h" |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrInfo.h" |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 26 | #define DEBUG_TYPE "regalloc" |
| 27 | |
Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 28 | STATISTIC(NumDCEDeleted, "Number of instructions deleted by DCE"); |
| 29 | STATISTIC(NumDCEFoldedLoads, "Number of single use loads folded after DCE"); |
| 30 | STATISTIC(NumFracRanges, "Number of live ranges fractured by DCE"); |
| 31 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 32 | void LiveRangeEdit::Delegate::anchor() { } |
| 33 | |
Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 34 | LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(unsigned OldReg) { |
Jakob Stoklund Olesen | 8630840 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 35 | unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg)); |
Pete Cooper | 4f0dbb2 | 2012-04-03 00:28:46 +0000 | [diff] [blame] | 36 | if (VRM) { |
Pete Cooper | 4f0dbb2 | 2012-04-03 00:28:46 +0000 | [diff] [blame] | 37 | VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg)); |
| 38 | } |
Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 39 | LiveInterval &LI = LIS.createEmptyInterval(VReg); |
Quentin Colombet | 5725f56 | 2017-02-02 20:44:36 +0000 | [diff] [blame^] | 40 | if (Parent && !Parent->isSpillable()) |
| 41 | LI.markNotSpillable(); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 42 | // Create empty subranges if the OldReg's interval has them. Do not create |
| 43 | // the main range here---it will be constructed later after the subranges |
| 44 | // have been finalized. |
| 45 | LiveInterval &OldLI = LIS.getInterval(OldReg); |
| 46 | VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator(); |
| 47 | for (LiveInterval::SubRange &S : OldLI.subranges()) |
| 48 | LI.createSubRange(Alloc, S.LaneMask); |
Jakob Stoklund Olesen | 8630840 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 49 | return LI; |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 52 | unsigned LiveRangeEdit::createFrom(unsigned OldReg) { |
| 53 | unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg)); |
| 54 | if (VRM) { |
| 55 | VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg)); |
| 56 | } |
Quentin Colombet | 5725f56 | 2017-02-02 20:44:36 +0000 | [diff] [blame^] | 57 | // FIXME: Getting the interval here actually computes it. |
| 58 | // In theory, this may not be what we want, but in practice |
| 59 | // the createEmptyIntervalFrom API is used when this is not |
| 60 | // the case. Generally speaking we just want to annotate the |
| 61 | // LiveInterval when it gets created but we cannot do that at |
| 62 | // the moment. |
| 63 | if (Parent && !Parent->isSpillable()) |
| 64 | LIS.getInterval(VReg).markNotSpillable(); |
Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 65 | return VReg; |
| 66 | } |
| 67 | |
Jakob Stoklund Olesen | 86e53ce | 2011-04-20 22:14:20 +0000 | [diff] [blame] | 68 | bool LiveRangeEdit::checkRematerializable(VNInfo *VNI, |
Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 69 | const MachineInstr *DefMI, |
Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 70 | AliasAnalysis *aa) { |
| 71 | assert(DefMI && "Missing instruction"); |
Jakob Stoklund Olesen | 3834dae | 2012-05-18 22:10:15 +0000 | [diff] [blame] | 72 | ScannedRemattable = true; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 73 | if (!TII.isTriviallyReMaterializable(*DefMI, aa)) |
Jakob Stoklund Olesen | 86e53ce | 2011-04-20 22:14:20 +0000 | [diff] [blame] | 74 | return false; |
Jakob Stoklund Olesen | 3834dae | 2012-05-18 22:10:15 +0000 | [diff] [blame] | 75 | Remattable.insert(VNI); |
Jakob Stoklund Olesen | 86e53ce | 2011-04-20 22:14:20 +0000 | [diff] [blame] | 76 | return true; |
Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 79 | void LiveRangeEdit::scanRemattable(AliasAnalysis *aa) { |
Matthias Braun | 9676195 | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 80 | for (VNInfo *VNI : getParent().valnos) { |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 81 | if (VNI->isUnused()) |
| 82 | continue; |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 83 | unsigned Original = VRM->getOriginal(getReg()); |
| 84 | LiveInterval &OrigLI = LIS.getInterval(Original); |
| 85 | VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 86 | if (!OrigVNI) |
| 87 | continue; |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 88 | MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def); |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 89 | if (!DefMI) |
| 90 | continue; |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 91 | checkRematerializable(OrigVNI, DefMI, aa); |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 92 | } |
Jakob Stoklund Olesen | 3834dae | 2012-05-18 22:10:15 +0000 | [diff] [blame] | 93 | ScannedRemattable = true; |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 96 | bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) { |
Jakob Stoklund Olesen | 3834dae | 2012-05-18 22:10:15 +0000 | [diff] [blame] | 97 | if (!ScannedRemattable) |
Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 98 | scanRemattable(aa); |
Jakob Stoklund Olesen | 3834dae | 2012-05-18 22:10:15 +0000 | [diff] [blame] | 99 | return !Remattable.empty(); |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 102 | /// allUsesAvailableAt - Return true if all registers used by OrigMI at |
| 103 | /// OrigIdx are also available with the same value at UseIdx. |
| 104 | bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI, |
| 105 | SlotIndex OrigIdx, |
Jakub Staszak | 26ac8a7 | 2013-03-18 23:40:46 +0000 | [diff] [blame] | 106 | SlotIndex UseIdx) const { |
Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 107 | OrigIdx = OrigIdx.getRegSlot(true); |
| 108 | UseIdx = UseIdx.getRegSlot(true); |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 109 | for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) { |
| 110 | const MachineOperand &MO = OrigMI->getOperand(i); |
Jakob Stoklund Olesen | 7809578 | 2012-06-22 17:31:01 +0000 | [diff] [blame] | 111 | if (!MO.isReg() || !MO.getReg() || !MO.readsReg()) |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 112 | continue; |
Jakob Stoklund Olesen | 7809578 | 2012-06-22 17:31:01 +0000 | [diff] [blame] | 113 | |
| 114 | // We can't remat physreg uses, unless it is a constant. |
| 115 | if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) { |
Matthias Braun | de8c1b3 | 2016-10-28 18:05:09 +0000 | [diff] [blame] | 116 | if (MRI.isConstantPhysReg(MO.getReg())) |
Jakob Stoklund Olesen | 7809578 | 2012-06-22 17:31:01 +0000 | [diff] [blame] | 117 | continue; |
| 118 | return false; |
| 119 | } |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 120 | |
Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 121 | LiveInterval &li = LIS.getInterval(MO.getReg()); |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 122 | const VNInfo *OVNI = li.getVNInfoAt(OrigIdx); |
| 123 | if (!OVNI) |
| 124 | continue; |
Jakob Stoklund Olesen | 4df59a9 | 2012-10-16 22:51:58 +0000 | [diff] [blame] | 125 | |
| 126 | // Don't allow rematerialization immediately after the original def. |
| 127 | // It would be incorrect if OrigMI redefines the register. |
| 128 | // See PR14098. |
| 129 | if (SlotIndex::isSameInstr(OrigIdx, UseIdx)) |
| 130 | return false; |
| 131 | |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 132 | if (OVNI != li.getVNInfoAt(UseIdx)) |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 138 | bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI, |
| 139 | SlotIndex UseIdx, bool cheapAsAMove) { |
Jakob Stoklund Olesen | 3834dae | 2012-05-18 22:10:15 +0000 | [diff] [blame] | 140 | assert(ScannedRemattable && "Call anyRematerializable first"); |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 141 | |
| 142 | // Use scanRemattable info. |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 143 | if (!Remattable.count(OrigVNI)) |
Jakob Stoklund Olesen | de5c4dc | 2010-11-10 01:05:12 +0000 | [diff] [blame] | 144 | return false; |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 145 | |
Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 146 | // No defining instruction provided. |
| 147 | SlotIndex DefIdx; |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 148 | assert(RM.OrigMI && "No defining instruction for remattable value"); |
| 149 | DefIdx = LIS.getInstructionIndex(*RM.OrigMI); |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 150 | |
| 151 | // If only cheap remats were requested, bail out early. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 152 | if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI)) |
Jakob Stoklund Olesen | de5c4dc | 2010-11-10 01:05:12 +0000 | [diff] [blame] | 153 | return false; |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 154 | |
| 155 | // Verify that all used registers are available with the same values. |
Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 156 | if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx)) |
Jakob Stoklund Olesen | de5c4dc | 2010-11-10 01:05:12 +0000 | [diff] [blame] | 157 | return false; |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 158 | |
Jakob Stoklund Olesen | de5c4dc | 2010-11-10 01:05:12 +0000 | [diff] [blame] | 159 | return true; |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB, |
| 163 | MachineBasicBlock::iterator MI, |
| 164 | unsigned DestReg, |
| 165 | const Remat &RM, |
Jakob Stoklund Olesen | 7d40679 | 2011-05-02 05:29:58 +0000 | [diff] [blame] | 166 | const TargetRegisterInfo &tri, |
| 167 | bool Late) { |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 168 | assert(RM.OrigMI && "Invalid remat"); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 169 | TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri); |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 170 | // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg |
| 171 | // to false anyway in case the isDead flag of RM.OrigMI's dest register |
| 172 | // is true. |
| 173 | (*--MI).getOperand(0).setIsDead(false); |
Jakob Stoklund Olesen | 3834dae | 2012-05-18 22:10:15 +0000 | [diff] [blame] | 174 | Rematted.insert(RM.ParentVNI); |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 175 | return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot(); |
Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 178 | void LiveRangeEdit::eraseVirtReg(unsigned Reg) { |
Jakob Stoklund Olesen | 3834dae | 2012-05-18 22:10:15 +0000 | [diff] [blame] | 179 | if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg)) |
Jakob Stoklund Olesen | 43a8750 | 2011-03-13 01:23:11 +0000 | [diff] [blame] | 180 | LIS.removeInterval(Reg); |
| 181 | } |
| 182 | |
Jakob Stoklund Olesen | 18fd84c | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 183 | bool LiveRangeEdit::foldAsLoad(LiveInterval *LI, |
Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 184 | SmallVectorImpl<MachineInstr*> &Dead) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 185 | MachineInstr *DefMI = nullptr, *UseMI = nullptr; |
Jakob Stoklund Olesen | 18fd84c | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 186 | |
| 187 | // Check that there is a single def and a single use. |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 188 | for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg)) { |
| 189 | MachineInstr *MI = MO.getParent(); |
Jakob Stoklund Olesen | 18fd84c | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 190 | if (MO.isDef()) { |
| 191 | if (DefMI && DefMI != MI) |
| 192 | return false; |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 193 | if (!MI->canFoldAsLoad()) |
Jakob Stoklund Olesen | 18fd84c | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 194 | return false; |
| 195 | DefMI = MI; |
| 196 | } else if (!MO.isUndef()) { |
| 197 | if (UseMI && UseMI != MI) |
| 198 | return false; |
| 199 | // FIXME: Targets don't know how to fold subreg uses. |
| 200 | if (MO.getSubReg()) |
| 201 | return false; |
| 202 | UseMI = MI; |
| 203 | } |
| 204 | } |
| 205 | if (!DefMI || !UseMI) |
| 206 | return false; |
| 207 | |
Jakob Stoklund Olesen | e2cfd0d | 2012-07-20 21:29:31 +0000 | [diff] [blame] | 208 | // Since we're moving the DefMI load, make sure we're not extending any live |
| 209 | // ranges. |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 210 | if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI), |
| 211 | LIS.getInstructionIndex(*UseMI))) |
Jakob Stoklund Olesen | e2cfd0d | 2012-07-20 21:29:31 +0000 | [diff] [blame] | 212 | return false; |
| 213 | |
| 214 | // We also need to make sure it is safe to move the load. |
| 215 | // Assume there are stores between DefMI and UseMI. |
| 216 | bool SawStore = true; |
Matthias Braun | 07066cc | 2015-05-19 21:22:20 +0000 | [diff] [blame] | 217 | if (!DefMI->isSafeToMove(nullptr, SawStore)) |
Jakob Stoklund Olesen | e2cfd0d | 2012-07-20 21:29:31 +0000 | [diff] [blame] | 218 | return false; |
| 219 | |
Jakob Stoklund Olesen | 18fd84c | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 220 | DEBUG(dbgs() << "Try to fold single def: " << *DefMI |
| 221 | << " into single use: " << *UseMI); |
| 222 | |
| 223 | SmallVector<unsigned, 8> Ops; |
| 224 | if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second) |
| 225 | return false; |
| 226 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 227 | MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS); |
Jakob Stoklund Olesen | 18fd84c | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 228 | if (!FoldMI) |
| 229 | return false; |
| 230 | DEBUG(dbgs() << " folded: " << *FoldMI); |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 231 | LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI); |
Jakob Stoklund Olesen | 18fd84c | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 232 | UseMI->eraseFromParent(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 233 | DefMI->addRegisterDead(LI->reg, nullptr); |
Jakob Stoklund Olesen | 18fd84c | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 234 | Dead.push_back(DefMI); |
Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 235 | ++NumDCEFoldedLoads; |
Jakob Stoklund Olesen | 18fd84c | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 236 | return true; |
| 237 | } |
| 238 | |
Matthias Braun | c1e029e | 2015-06-01 21:26:26 +0000 | [diff] [blame] | 239 | bool LiveRangeEdit::useIsKill(const LiveInterval &LI, |
| 240 | const MachineOperand &MO) const { |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 241 | const MachineInstr &MI = *MO.getParent(); |
Matthias Braun | c1e029e | 2015-06-01 21:26:26 +0000 | [diff] [blame] | 242 | SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot(); |
| 243 | if (LI.Query(Idx).isKill()) |
| 244 | return true; |
| 245 | const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); |
| 246 | unsigned SubReg = MO.getSubReg(); |
Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 247 | LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg); |
Matthias Braun | c1e029e | 2015-06-01 21:26:26 +0000 | [diff] [blame] | 248 | for (const LiveInterval::SubRange &S : LI.subranges()) { |
Krzysztof Parzyszek | ea9f8ce | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 249 | if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill()) |
Matthias Braun | c1e029e | 2015-06-01 21:26:26 +0000 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | return false; |
| 253 | } |
| 254 | |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 255 | /// Find all live intervals that need to shrink, then remove the instruction. |
Wei Mi | c022370 | 2016-07-08 21:08:09 +0000 | [diff] [blame] | 256 | void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink, |
| 257 | AliasAnalysis *AA) { |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 258 | assert(MI->allDefsAreDead() && "Def isn't really dead"); |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 259 | SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot(); |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 260 | |
Andrew Trick | cbd7305 | 2013-06-22 00:33:48 +0000 | [diff] [blame] | 261 | // Never delete a bundled instruction. |
| 262 | if (MI->isBundled()) { |
| 263 | return; |
| 264 | } |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 265 | // Never delete inline asm. |
| 266 | if (MI->isInlineAsm()) { |
| 267 | DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | // Use the same criteria as DeadMachineInstructionElim. |
| 272 | bool SawStore = false; |
Matthias Braun | 07066cc | 2015-05-19 21:22:20 +0000 | [diff] [blame] | 273 | if (!MI->isSafeToMove(nullptr, SawStore)) { |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 274 | DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI); |
| 279 | |
| 280 | // Collect virtual registers to be erased after MI is gone. |
| 281 | SmallVector<unsigned, 8> RegsToErase; |
| 282 | bool ReadsPhysRegs = false; |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 283 | bool isOrigDef = false; |
| 284 | unsigned Dest; |
Geoff Berry | 66d1f0f | 2016-12-15 19:55:19 +0000 | [diff] [blame] | 285 | // Only optimize rematerialize case when the instruction has one def, since |
| 286 | // otherwise we could leave some dead defs in the code. This case is |
| 287 | // extremely rare. |
| 288 | if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() && |
| 289 | MI->getDesc().getNumDefs() == 1) { |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 290 | Dest = MI->getOperand(0).getReg(); |
| 291 | unsigned Original = VRM->getOriginal(Dest); |
| 292 | LiveInterval &OrigLI = LIS.getInterval(Original); |
| 293 | VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx); |
Quentin Colombet | d307909 | 2016-06-09 21:34:31 +0000 | [diff] [blame] | 294 | // The original live-range may have been shrunk to |
| 295 | // an empty live-range. It happens when it is dead, but |
| 296 | // we still keep it around to be able to rematerialize |
| 297 | // other values that depend on it. |
| 298 | if (OrigVNI) |
| 299 | isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx); |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 300 | } |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 301 | |
| 302 | // Check for live intervals that may shrink |
| 303 | for (MachineInstr::mop_iterator MOI = MI->operands_begin(), |
| 304 | MOE = MI->operands_end(); MOI != MOE; ++MOI) { |
| 305 | if (!MOI->isReg()) |
| 306 | continue; |
| 307 | unsigned Reg = MOI->getReg(); |
| 308 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 309 | // Check if MI reads any unreserved physregs. |
| 310 | if (Reg && MOI->readsReg() && !MRI.isReserved(Reg)) |
| 311 | ReadsPhysRegs = true; |
Matthias Braun | cfb8ad2 | 2015-01-21 18:50:21 +0000 | [diff] [blame] | 312 | else if (MOI->isDef()) |
| 313 | LIS.removePhysRegDefAt(Reg, Idx); |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 314 | continue; |
| 315 | } |
| 316 | LiveInterval &LI = LIS.getInterval(Reg); |
| 317 | |
| 318 | // Shrink read registers, unless it is likely to be expensive and |
| 319 | // unlikely to change anything. We typically don't want to shrink the |
| 320 | // PIC base register that has lots of uses everywhere. |
| 321 | // Always shrink COPY uses that probably come from live range splitting. |
Matthias Braun | c1e029e | 2015-06-01 21:26:26 +0000 | [diff] [blame] | 322 | if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) || |
| 323 | (MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI)))) |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 324 | ToShrink.insert(&LI); |
| 325 | |
| 326 | // Remove defined value. |
| 327 | if (MOI->isDef()) { |
Matthias Braun | 311730a | 2015-01-21 19:02:30 +0000 | [diff] [blame] | 328 | if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr) |
| 329 | TheDelegate->LRE_WillShrinkVirtReg(LI.reg); |
| 330 | LIS.removeVRegDefAt(LI, Idx); |
| 331 | if (LI.empty()) |
| 332 | RegsToErase.push_back(Reg); |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | // Currently, we don't support DCE of physreg live ranges. If MI reads |
| 337 | // any unreserved physregs, don't erase the instruction, but turn it into |
| 338 | // a KILL instead. This way, the physreg live ranges don't end up |
| 339 | // dangling. |
| 340 | // FIXME: It would be better to have something like shrinkToUses() for |
| 341 | // physregs. That could potentially enable more DCE and it would free up |
| 342 | // the physreg. It would not happen often, though. |
| 343 | if (ReadsPhysRegs) { |
| 344 | MI->setDesc(TII.get(TargetOpcode::KILL)); |
| 345 | // Remove all operands that aren't physregs. |
| 346 | for (unsigned i = MI->getNumOperands(); i; --i) { |
| 347 | const MachineOperand &MO = MI->getOperand(i-1); |
| 348 | if (MO.isReg() && TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
| 349 | continue; |
| 350 | MI->RemoveOperand(i-1); |
| 351 | } |
| 352 | DEBUG(dbgs() << "Converted physregs to:\t" << *MI); |
| 353 | } else { |
Wei Mi | c022370 | 2016-07-08 21:08:09 +0000 | [diff] [blame] | 354 | // If the dest of MI is an original reg and MI is reMaterializable, |
| 355 | // don't delete the inst. Replace the dest with a new reg, and keep |
| 356 | // the inst for remat of other siblings. The inst is saved in |
| 357 | // LiveRangeEdit::DeadRemats and will be deleted after all the |
| 358 | // allocations of the func are done. |
| 359 | if (isOrigDef && DeadRemats && TII.isTriviallyReMaterializable(*MI, AA)) { |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 360 | LiveInterval &NewLI = createEmptyIntervalFrom(Dest); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 361 | NewLI.removeEmptySubRanges(); |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 362 | VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator()); |
| 363 | NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI)); |
| 364 | pop_back(); |
| 365 | markDeadRemat(MI); |
| 366 | const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); |
| 367 | MI->substituteRegister(Dest, NewLI.reg, 0, TRI); |
| 368 | MI->getOperand(0).setIsDead(true); |
| 369 | } else { |
| 370 | if (TheDelegate) |
| 371 | TheDelegate->LRE_WillEraseInstruction(MI); |
| 372 | LIS.RemoveMachineInstrFromMaps(*MI); |
| 373 | MI->eraseFromParent(); |
| 374 | ++NumDCEDeleted; |
| 375 | } |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // Erase any virtregs that are now empty and unused. There may be <undef> |
| 379 | // uses around. Keep the empty live range in that case. |
| 380 | for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) { |
| 381 | unsigned Reg = RegsToErase[i]; |
| 382 | if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) { |
| 383 | ToShrink.remove(&LIS.getInterval(Reg)); |
| 384 | eraseVirtReg(Reg); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 389 | void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead, |
Wei Mi | c022370 | 2016-07-08 21:08:09 +0000 | [diff] [blame] | 390 | ArrayRef<unsigned> RegsBeingSpilled, |
| 391 | AliasAnalysis *AA) { |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 392 | ToShrinkSet ToShrink; |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 393 | |
| 394 | for (;;) { |
| 395 | // Erase all dead defs. |
Andrew Trick | 530fc1f | 2013-06-21 18:33:17 +0000 | [diff] [blame] | 396 | while (!Dead.empty()) |
Wei Mi | c022370 | 2016-07-08 21:08:09 +0000 | [diff] [blame] | 397 | eliminateDeadDef(Dead.pop_back_val(), ToShrink, AA); |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 398 | |
| 399 | if (ToShrink.empty()) |
| 400 | break; |
| 401 | |
| 402 | // Shrink just one live interval. Then delete new dead defs. |
Jakob Stoklund Olesen | e14b2b2 | 2011-03-16 22:56:16 +0000 | [diff] [blame] | 403 | LiveInterval *LI = ToShrink.back(); |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 404 | ToShrink.pop_back(); |
Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 405 | if (foldAsLoad(LI, Dead)) |
Jakob Stoklund Olesen | 18fd84c | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 406 | continue; |
Matthias Braun | d3dd135 | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 407 | unsigned VReg = LI->reg; |
Jakob Stoklund Olesen | 3834dae | 2012-05-18 22:10:15 +0000 | [diff] [blame] | 408 | if (TheDelegate) |
Matthias Braun | d3dd135 | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 409 | TheDelegate->LRE_WillShrinkVirtReg(VReg); |
Jakob Stoklund Olesen | 8630840 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 410 | if (!LIS.shrinkToUses(LI, &Dead)) |
| 411 | continue; |
Andrew Trick | 7df3f01 | 2013-06-21 18:33:14 +0000 | [diff] [blame] | 412 | |
Pete Cooper | 76e4bc4 | 2011-12-12 22:16:27 +0000 | [diff] [blame] | 413 | // Don't create new intervals for a register being spilled. |
| 414 | // The new intervals would have to be spilled anyway so its not worth it. |
| 415 | // Also they currently aren't spilled so creating them and not spilling |
| 416 | // them results in incorrect code. |
| 417 | bool BeingSpilled = false; |
| 418 | for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) { |
Matthias Braun | d3dd135 | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 419 | if (VReg == RegsBeingSpilled[i]) { |
Pete Cooper | 76e4bc4 | 2011-12-12 22:16:27 +0000 | [diff] [blame] | 420 | BeingSpilled = true; |
| 421 | break; |
| 422 | } |
| 423 | } |
Andrew Trick | 7df3f01 | 2013-06-21 18:33:14 +0000 | [diff] [blame] | 424 | |
Pete Cooper | 76e4bc4 | 2011-12-12 22:16:27 +0000 | [diff] [blame] | 425 | if (BeingSpilled) continue; |
Jakob Stoklund Olesen | 8630840 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 426 | |
| 427 | // LI may have been separated, create new intervals. |
Jakob Stoklund Olesen | 4417c7b | 2013-08-14 17:28:52 +0000 | [diff] [blame] | 428 | LI->RenumberValues(); |
Matthias Braun | d3dd135 | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 429 | SmallVector<LiveInterval*, 8> SplitLIs; |
| 430 | LIS.splitSeparateComponents(*LI, SplitLIs); |
| 431 | if (!SplitLIs.empty()) |
| 432 | ++NumFracRanges; |
| 433 | |
| 434 | unsigned Original = VRM ? VRM->getOriginal(VReg) : 0; |
| 435 | for (const LiveInterval *SplitLI : SplitLIs) { |
Jakob Stoklund Olesen | bbad3bc | 2011-07-05 15:38:41 +0000 | [diff] [blame] | 436 | // If LI is an original interval that hasn't been split yet, make the new |
| 437 | // intervals their own originals instead of referring to LI. The original |
| 438 | // interval must contain all the split products, and LI doesn't. |
Matthias Braun | d3dd135 | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 439 | if (Original != VReg && Original != 0) |
| 440 | VRM->setIsSplitFromReg(SplitLI->reg, Original); |
Jakob Stoklund Olesen | 3834dae | 2012-05-18 22:10:15 +0000 | [diff] [blame] | 441 | if (TheDelegate) |
Matthias Braun | d3dd135 | 2015-09-22 03:44:41 +0000 | [diff] [blame] | 442 | TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg); |
Jakob Stoklund Olesen | dd9a2ec | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 443 | } |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Mark Lacey | f367cd9 | 2013-08-14 23:50:09 +0000 | [diff] [blame] | 447 | // Keep track of new virtual registers created via |
| 448 | // MachineRegisterInfo::createVirtualRegister. |
| 449 | void |
| 450 | LiveRangeEdit::MRI_NoteNewVirtualRegister(unsigned VReg) |
| 451 | { |
| 452 | if (VRM) |
| 453 | VRM->grow(); |
| 454 | |
| 455 | NewRegs.push_back(VReg); |
| 456 | } |
| 457 | |
Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 458 | void |
| 459 | LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF, |
| 460 | const MachineLoopInfo &Loops, |
| 461 | const MachineBlockFrequencyInfo &MBFI) { |
Robert Lougher | 11a44b7 | 2015-08-10 11:59:44 +0000 | [diff] [blame] | 462 | VirtRegAuxInfo VRAI(MF, LIS, VRM, Loops, MBFI); |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 463 | for (unsigned I = 0, Size = size(); I < Size; ++I) { |
| 464 | LiveInterval &LI = LIS.getInterval(get(I)); |
Eric Christopher | 349d588 | 2015-01-27 01:15:16 +0000 | [diff] [blame] | 465 | if (MRI.recomputeRegClass(LI.reg)) |
Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 466 | DEBUG({ |
| 467 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 468 | dbgs() << "Inflated " << PrintReg(LI.reg) << " to " |
Craig Topper | f98c606 | 2014-11-17 05:58:26 +0000 | [diff] [blame] | 469 | << TRI->getRegClassName(MRI.getRegClass(LI.reg)) << '\n'; |
Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 470 | }); |
Arnaud A. de Grandmaison | ea3ac16 | 2013-11-11 19:04:45 +0000 | [diff] [blame] | 471 | VRAI.calculateSpillWeightAndHint(LI); |
Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 472 | } |
| 473 | } |