David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1 | //===-- SimpleRegisterCoalescing.cpp - Register Coalescing ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a simple register coalescing pass that attempts to |
| 11 | // aggressively coalesce every register copy that it can. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Evan Cheng | 3b1f55e | 2007-07-31 22:37:44 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "regcoalescing" |
Evan Cheng | a461c4d | 2007-11-05 17:41:38 +0000 | [diff] [blame] | 16 | #include "SimpleRegisterCoalescing.h" |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 17 | #include "VirtRegMap.h" |
Evan Cheng | a461c4d | 2007-11-05 17:41:38 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 19 | #include "llvm/Value.h" |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 21 | #include "llvm/CodeGen/MachineInstr.h" |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/Passes.h" |
David Greene | 2c17c4d | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/RegisterCoalescer.h" |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetInstrInfo.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Support/CommandLine.h" |
| 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/ADT/SmallSet.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
| 32 | #include "llvm/ADT/STLExtras.h" |
| 33 | #include <algorithm> |
| 34 | #include <cmath> |
| 35 | using namespace llvm; |
| 36 | |
| 37 | STATISTIC(numJoins , "Number of interval joins performed"); |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 38 | STATISTIC(numSubJoins , "Number of subclass joins performed"); |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 39 | STATISTIC(numCommutes , "Number of instruction commuting performed"); |
| 40 | STATISTIC(numExtends , "Number of copies extended"); |
Evan Cheng | cd04708 | 2008-08-30 09:09:33 +0000 | [diff] [blame] | 41 | STATISTIC(NumReMats , "Number of instructions re-materialized"); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 42 | STATISTIC(numPeep , "Number of identity moves eliminated after coalescing"); |
| 43 | STATISTIC(numAborts , "Number of times interval joining aborted"); |
| 44 | |
| 45 | char SimpleRegisterCoalescing::ID = 0; |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 46 | static cl::opt<bool> |
| 47 | EnableJoining("join-liveintervals", |
| 48 | cl::desc("Coalesce copies (default=true)"), |
| 49 | cl::init(true)); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 50 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 51 | static cl::opt<bool> |
| 52 | NewHeuristic("new-coalescer-heuristic", |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 53 | cl::desc("Use new coalescer heuristic"), |
| 54 | cl::init(false), cl::Hidden); |
| 55 | |
| 56 | static cl::opt<bool> |
| 57 | CrossClassJoin("join-subclass-copies", |
| 58 | cl::desc("Coalesce copies to sub- register class"), |
| 59 | cl::init(false), cl::Hidden); |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 60 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 61 | static RegisterPass<SimpleRegisterCoalescing> |
| 62 | X("simple-register-coalescing", "Simple Register Coalescing"); |
David Greene | 2c17c4d | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 63 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 64 | // Declare that we implement the RegisterCoalescer interface |
| 65 | static RegisterAnalysisGroup<RegisterCoalescer, true/*The Default*/> V(X); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 66 | |
Dan Gohman | 6ddba2b | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 67 | const PassInfo *const llvm::SimpleRegisterCoalescingID = &X; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 68 | |
| 69 | void SimpleRegisterCoalescing::getAnalysisUsage(AnalysisUsage &AU) const { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 70 | AU.addPreserved<LiveIntervals>(); |
Bill Wendling | 67d65bb | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 71 | AU.addPreserved<MachineLoopInfo>(); |
| 72 | AU.addPreservedID(MachineDominatorsID); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 73 | AU.addPreservedID(PHIEliminationID); |
| 74 | AU.addPreservedID(TwoAddressInstructionPassID); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 75 | AU.addRequired<LiveIntervals>(); |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 76 | AU.addRequired<MachineLoopInfo>(); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 77 | MachineFunctionPass::getAnalysisUsage(AU); |
| 78 | } |
| 79 | |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 80 | /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy with IntA |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 81 | /// being the source and IntB being the dest, thus this defines a value number |
| 82 | /// in IntB. If the source value number (in IntA) is defined by a copy from B, |
| 83 | /// see if we can merge these two pieces of B into a single value number, |
| 84 | /// eliminating a copy. For example: |
| 85 | /// |
| 86 | /// A3 = B0 |
| 87 | /// ... |
| 88 | /// B1 = A3 <- this copy |
| 89 | /// |
| 90 | /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1 |
| 91 | /// value number to be replaced with B0 (which simplifies the B liveinterval). |
| 92 | /// |
| 93 | /// This returns true if an interval was modified. |
| 94 | /// |
Bill Wendling | 2674d71 | 2008-01-04 08:59:18 +0000 | [diff] [blame] | 95 | bool SimpleRegisterCoalescing::AdjustCopiesBackFrom(LiveInterval &IntA, |
| 96 | LiveInterval &IntB, |
| 97 | MachineInstr *CopyMI) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 98 | unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI)); |
| 99 | |
| 100 | // BValNo is a value number in B that is defined by a copy from A. 'B3' in |
| 101 | // the example above. |
| 102 | LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 103 | if (BLR == IntB.end()) // Should never happen! |
| 104 | return false; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 105 | VNInfo *BValNo = BLR->valno; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 106 | |
| 107 | // Get the location that B is defined at. Two options: either this value has |
| 108 | // an unknown definition point or it is defined at CopyIdx. If unknown, we |
| 109 | // can't process it. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 110 | if (!BValNo->copy) return false; |
| 111 | assert(BValNo->def == CopyIdx && "Copy doesn't define the value?"); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 112 | |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 113 | // AValNo is the value number in A that defines the copy, A3 in the example. |
| 114 | LiveInterval::iterator ALR = IntA.FindLiveRangeContaining(CopyIdx-1); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 115 | if (ALR == IntA.end()) // Should never happen! |
| 116 | return false; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 117 | VNInfo *AValNo = ALR->valno; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 118 | |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 119 | // If AValNo is defined as a copy from IntB, we can potentially process this. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 120 | // Get the instruction that defines this value number. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 121 | unsigned SrcReg = li_->getVNInfoSourceReg(AValNo); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 122 | if (!SrcReg) return false; // Not defined by a copy. |
| 123 | |
| 124 | // If the value number is not defined by a copy instruction, ignore it. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 125 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 126 | // If the source register comes from an interval other than IntB, we can't |
| 127 | // handle this. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 128 | if (SrcReg != IntB.reg) return false; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 129 | |
| 130 | // Get the LiveRange in IntB that this value number starts with. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 131 | LiveInterval::iterator ValLR = IntB.FindLiveRangeContaining(AValNo->def-1); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 132 | if (ValLR == IntB.end()) // Should never happen! |
| 133 | return false; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 134 | |
| 135 | // Make sure that the end of the live range is inside the same block as |
| 136 | // CopyMI. |
| 137 | MachineInstr *ValLREndInst = li_->getInstructionFromIndex(ValLR->end-1); |
| 138 | if (!ValLREndInst || |
| 139 | ValLREndInst->getParent() != CopyMI->getParent()) return false; |
| 140 | |
| 141 | // Okay, we now know that ValLR ends in the same block that the CopyMI |
| 142 | // live-range starts. If there are no intervening live ranges between them in |
| 143 | // IntB, we can merge them. |
| 144 | if (ValLR+1 != BLR) return false; |
Evan Cheng | dc5294f | 2007-08-14 23:19:28 +0000 | [diff] [blame] | 145 | |
| 146 | // If a live interval is a physical register, conservatively check if any |
| 147 | // of its sub-registers is overlapping the live interval of the virtual |
| 148 | // register. If so, do not coalesce. |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 149 | if (TargetRegisterInfo::isPhysicalRegister(IntB.reg) && |
| 150 | *tri_->getSubRegisters(IntB.reg)) { |
| 151 | for (const unsigned* SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR) |
Evan Cheng | dc5294f | 2007-08-14 23:19:28 +0000 | [diff] [blame] | 152 | if (li_->hasInterval(*SR) && IntA.overlaps(li_->getInterval(*SR))) { |
| 153 | DOUT << "Interfere with sub-register "; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 154 | DEBUG(li_->getInterval(*SR).print(DOUT, tri_)); |
Evan Cheng | dc5294f | 2007-08-14 23:19:28 +0000 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 158 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 159 | DOUT << "\nExtending: "; IntB.print(DOUT, tri_); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 160 | |
Evan Cheng | a8d94f1 | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 161 | unsigned FillerStart = ValLR->end, FillerEnd = BLR->start; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 162 | // We are about to delete CopyMI, so need to remove it as the 'instruction |
Evan Cheng | a8d94f1 | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 163 | // that defines this value #'. Update the the valnum with the new defining |
| 164 | // instruction #. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 165 | BValNo->def = FillerStart; |
| 166 | BValNo->copy = NULL; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 167 | |
| 168 | // Okay, we can merge them. We need to insert a new liverange: |
| 169 | // [ValLR.end, BLR.begin) of either value number, then we merge the |
| 170 | // two value numbers. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 171 | IntB.addRange(LiveRange(FillerStart, FillerEnd, BValNo)); |
| 172 | |
| 173 | // If the IntB live range is assigned to a physical register, and if that |
| 174 | // physreg has aliases, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 175 | if (TargetRegisterInfo::isPhysicalRegister(IntB.reg)) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 176 | // Update the liveintervals of sub-registers. |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 177 | for (const unsigned *AS = tri_->getSubRegisters(IntB.reg); *AS; ++AS) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 178 | LiveInterval &AliasLI = li_->getInterval(*AS); |
| 179 | AliasLI.addRange(LiveRange(FillerStart, FillerEnd, |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 180 | AliasLI.getNextValue(FillerStart, 0, li_->getVNInfoAllocator()))); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | // Okay, merge "B1" into the same value number as "B0". |
Evan Cheng | 25f34a3 | 2008-09-15 06:28:41 +0000 | [diff] [blame] | 185 | if (BValNo != ValLR->valno) { |
| 186 | IntB.addKills(ValLR->valno, BValNo->kills); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 187 | IntB.MergeValueNumberInto(BValNo, ValLR->valno); |
Evan Cheng | 25f34a3 | 2008-09-15 06:28:41 +0000 | [diff] [blame] | 188 | } |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 189 | DOUT << " result = "; IntB.print(DOUT, tri_); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 190 | DOUT << "\n"; |
| 191 | |
| 192 | // If the source instruction was killing the source register before the |
| 193 | // merge, unset the isKill marker given the live range has been extended. |
| 194 | int UIdx = ValLREndInst->findRegisterUseOperandIdx(IntB.reg, true); |
Evan Cheng | 25f34a3 | 2008-09-15 06:28:41 +0000 | [diff] [blame] | 195 | if (UIdx != -1) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 196 | ValLREndInst->getOperand(UIdx).setIsKill(false); |
Evan Cheng | 25f34a3 | 2008-09-15 06:28:41 +0000 | [diff] [blame] | 197 | IntB.removeKill(ValLR->valno, FillerStart); |
| 198 | } |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 199 | |
| 200 | ++numExtends; |
| 201 | return true; |
| 202 | } |
| 203 | |
Evan Cheng | 559f422 | 2008-02-16 02:32:17 +0000 | [diff] [blame] | 204 | /// HasOtherReachingDefs - Return true if there are definitions of IntB |
| 205 | /// other than BValNo val# that can reach uses of AValno val# of IntA. |
| 206 | bool SimpleRegisterCoalescing::HasOtherReachingDefs(LiveInterval &IntA, |
| 207 | LiveInterval &IntB, |
| 208 | VNInfo *AValNo, |
| 209 | VNInfo *BValNo) { |
| 210 | for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end(); |
| 211 | AI != AE; ++AI) { |
| 212 | if (AI->valno != AValNo) continue; |
| 213 | LiveInterval::Ranges::iterator BI = |
| 214 | std::upper_bound(IntB.ranges.begin(), IntB.ranges.end(), AI->start); |
| 215 | if (BI != IntB.ranges.begin()) |
| 216 | --BI; |
| 217 | for (; BI != IntB.ranges.end() && AI->end >= BI->start; ++BI) { |
| 218 | if (BI->valno == BValNo) |
| 219 | continue; |
| 220 | if (BI->start <= AI->start && BI->end > AI->start) |
| 221 | return true; |
| 222 | if (BI->start > AI->start && BI->start < AI->end) |
| 223 | return true; |
| 224 | } |
| 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 229 | /// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy with IntA |
| 230 | /// being the source and IntB being the dest, thus this defines a value number |
| 231 | /// in IntB. If the source value number (in IntA) is defined by a commutable |
| 232 | /// instruction and its other operand is coalesced to the copy dest register, |
| 233 | /// see if we can transform the copy into a noop by commuting the definition. For |
| 234 | /// example, |
| 235 | /// |
| 236 | /// A3 = op A2 B0<kill> |
| 237 | /// ... |
| 238 | /// B1 = A3 <- this copy |
| 239 | /// ... |
| 240 | /// = op A3 <- more uses |
| 241 | /// |
| 242 | /// ==> |
| 243 | /// |
| 244 | /// B2 = op B0 A2<kill> |
| 245 | /// ... |
| 246 | /// B1 = B2 <- now an identify copy |
| 247 | /// ... |
| 248 | /// = op B2 <- more uses |
| 249 | /// |
| 250 | /// This returns true if an interval was modified. |
| 251 | /// |
| 252 | bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(LiveInterval &IntA, |
| 253 | LiveInterval &IntB, |
| 254 | MachineInstr *CopyMI) { |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 255 | unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI)); |
| 256 | |
Evan Cheng | a9407f5 | 2008-02-18 18:56:31 +0000 | [diff] [blame] | 257 | // FIXME: For now, only eliminate the copy by commuting its def when the |
| 258 | // source register is a virtual register. We want to guard against cases |
| 259 | // where the copy is a back edge copy and commuting the def lengthen the |
| 260 | // live interval of the source register to the entire loop. |
| 261 | if (TargetRegisterInfo::isPhysicalRegister(IntA.reg)) |
Evan Cheng | 96cfff0 | 2008-02-18 08:40:53 +0000 | [diff] [blame] | 262 | return false; |
| 263 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 264 | // BValNo is a value number in B that is defined by a copy from A. 'B3' in |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 265 | // the example above. |
| 266 | LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 267 | if (BLR == IntB.end()) // Should never happen! |
| 268 | return false; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 269 | VNInfo *BValNo = BLR->valno; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 270 | |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 271 | // Get the location that B is defined at. Two options: either this value has |
| 272 | // an unknown definition point or it is defined at CopyIdx. If unknown, we |
| 273 | // can't process it. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 274 | if (!BValNo->copy) return false; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 275 | assert(BValNo->def == CopyIdx && "Copy doesn't define the value?"); |
| 276 | |
| 277 | // AValNo is the value number in A that defines the copy, A3 in the example. |
| 278 | LiveInterval::iterator ALR = IntA.FindLiveRangeContaining(CopyIdx-1); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 279 | if (ALR == IntA.end()) // Should never happen! |
| 280 | return false; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 281 | VNInfo *AValNo = ALR->valno; |
Evan Cheng | e35a6d1 | 2008-02-13 08:41:08 +0000 | [diff] [blame] | 282 | // If other defs can reach uses of this def, then it's not safe to perform |
| 283 | // the optimization. |
| 284 | if (AValNo->def == ~0U || AValNo->def == ~1U || AValNo->hasPHIKill) |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 285 | return false; |
| 286 | MachineInstr *DefMI = li_->getInstructionFromIndex(AValNo->def); |
| 287 | const TargetInstrDesc &TID = DefMI->getDesc(); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 288 | unsigned NewDstIdx; |
| 289 | if (!TID.isCommutable() || |
| 290 | !tii_->CommuteChangesDestination(DefMI, NewDstIdx)) |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 291 | return false; |
| 292 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 293 | MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx); |
| 294 | unsigned NewReg = NewDstMO.getReg(); |
| 295 | if (NewReg != IntB.reg || !NewDstMO.isKill()) |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 296 | return false; |
| 297 | |
| 298 | // Make sure there are no other definitions of IntB that would reach the |
| 299 | // uses which the new definition can reach. |
Evan Cheng | 559f422 | 2008-02-16 02:32:17 +0000 | [diff] [blame] | 300 | if (HasOtherReachingDefs(IntA, IntB, AValNo, BValNo)) |
| 301 | return false; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 302 | |
Evan Cheng | ed70cbb3 | 2008-03-26 19:03:01 +0000 | [diff] [blame] | 303 | // If some of the uses of IntA.reg is already coalesced away, return false. |
| 304 | // It's not possible to determine whether it's safe to perform the coalescing. |
| 305 | for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(IntA.reg), |
| 306 | UE = mri_->use_end(); UI != UE; ++UI) { |
| 307 | MachineInstr *UseMI = &*UI; |
| 308 | unsigned UseIdx = li_->getInstructionIndex(UseMI); |
| 309 | LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 310 | if (ULR == IntA.end()) |
| 311 | continue; |
Evan Cheng | ed70cbb3 | 2008-03-26 19:03:01 +0000 | [diff] [blame] | 312 | if (ULR->valno == AValNo && JoinedCopies.count(UseMI)) |
| 313 | return false; |
| 314 | } |
| 315 | |
Evan Cheng | cdbcfcc | 2008-02-13 09:56:03 +0000 | [diff] [blame] | 316 | // At this point we have decided that it is legal to do this |
| 317 | // transformation. Start by commuting the instruction. |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 318 | MachineBasicBlock *MBB = DefMI->getParent(); |
| 319 | MachineInstr *NewMI = tii_->commuteInstruction(DefMI); |
Evan Cheng | 559f422 | 2008-02-16 02:32:17 +0000 | [diff] [blame] | 320 | if (!NewMI) |
| 321 | return false; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 322 | if (NewMI != DefMI) { |
| 323 | li_->ReplaceMachineInstrInMaps(DefMI, NewMI); |
| 324 | MBB->insert(DefMI, NewMI); |
| 325 | MBB->erase(DefMI); |
| 326 | } |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 327 | unsigned OpIdx = NewMI->findRegisterUseOperandIdx(IntA.reg, false); |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 328 | NewMI->getOperand(OpIdx).setIsKill(); |
| 329 | |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 330 | bool BHasPHIKill = BValNo->hasPHIKill; |
| 331 | SmallVector<VNInfo*, 4> BDeadValNos; |
| 332 | SmallVector<unsigned, 4> BKills; |
| 333 | std::map<unsigned, unsigned> BExtend; |
Evan Cheng | 4ff3f1c | 2008-03-10 08:11:32 +0000 | [diff] [blame] | 334 | |
| 335 | // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g. |
| 336 | // A = or A, B |
| 337 | // ... |
| 338 | // B = A |
| 339 | // ... |
| 340 | // C = A<kill> |
| 341 | // ... |
| 342 | // = B |
| 343 | // |
| 344 | // then do not add kills of A to the newly created B interval. |
| 345 | bool Extended = BLR->end > ALR->end && ALR->end != ALR->start; |
| 346 | if (Extended) |
| 347 | BExtend[ALR->end] = BLR->end; |
| 348 | |
| 349 | // Update uses of IntA of the specific Val# with IntB. |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 350 | for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(IntA.reg), |
| 351 | UE = mri_->use_end(); UI != UE;) { |
| 352 | MachineOperand &UseMO = UI.getOperand(); |
Evan Cheng | cdbcfcc | 2008-02-13 09:56:03 +0000 | [diff] [blame] | 353 | MachineInstr *UseMI = &*UI; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 354 | ++UI; |
Evan Cheng | cdbcfcc | 2008-02-13 09:56:03 +0000 | [diff] [blame] | 355 | if (JoinedCopies.count(UseMI)) |
Evan Cheng | ed70cbb3 | 2008-03-26 19:03:01 +0000 | [diff] [blame] | 356 | continue; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 357 | unsigned UseIdx = li_->getInstructionIndex(UseMI); |
| 358 | LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 359 | if (ULR == IntA.end() || ULR->valno != AValNo) |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 360 | continue; |
| 361 | UseMO.setReg(NewReg); |
Evan Cheng | cdbcfcc | 2008-02-13 09:56:03 +0000 | [diff] [blame] | 362 | if (UseMI == CopyMI) |
| 363 | continue; |
Evan Cheng | 4ff3f1c | 2008-03-10 08:11:32 +0000 | [diff] [blame] | 364 | if (UseMO.isKill()) { |
| 365 | if (Extended) |
| 366 | UseMO.setIsKill(false); |
| 367 | else |
| 368 | BKills.push_back(li_->getUseIndex(UseIdx)+1); |
| 369 | } |
Evan Cheng | cdbcfcc | 2008-02-13 09:56:03 +0000 | [diff] [blame] | 370 | unsigned SrcReg, DstReg; |
| 371 | if (!tii_->isMoveInstr(*UseMI, SrcReg, DstReg)) |
| 372 | continue; |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 373 | if (DstReg == IntB.reg) { |
Evan Cheng | cdbcfcc | 2008-02-13 09:56:03 +0000 | [diff] [blame] | 374 | // This copy will become a noop. If it's defining a new val#, |
| 375 | // remove that val# as well. However this live range is being |
| 376 | // extended to the end of the existing live range defined by the copy. |
| 377 | unsigned DefIdx = li_->getDefIndex(UseIdx); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 378 | const LiveRange *DLR = IntB.getLiveRangeContaining(DefIdx); |
Evan Cheng | cdbcfcc | 2008-02-13 09:56:03 +0000 | [diff] [blame] | 379 | BHasPHIKill |= DLR->valno->hasPHIKill; |
| 380 | assert(DLR->valno->def == DefIdx); |
| 381 | BDeadValNos.push_back(DLR->valno); |
| 382 | BExtend[DLR->start] = DLR->end; |
| 383 | JoinedCopies.insert(UseMI); |
| 384 | // If this is a kill but it's going to be removed, the last use |
| 385 | // of the same val# is the new kill. |
Evan Cheng | 4ff3f1c | 2008-03-10 08:11:32 +0000 | [diff] [blame] | 386 | if (UseMO.isKill()) |
Evan Cheng | cdbcfcc | 2008-02-13 09:56:03 +0000 | [diff] [blame] | 387 | BKills.pop_back(); |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | // We need to insert a new liverange: [ALR.start, LastUse). It may be we can |
| 392 | // simply extend BLR if CopyMI doesn't end the range. |
| 393 | DOUT << "\nExtending: "; IntB.print(DOUT, tri_); |
| 394 | |
Evan Cheng | 739583b | 2008-06-17 20:11:16 +0000 | [diff] [blame] | 395 | // Remove val#'s defined by copies that will be coalesced away. |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 396 | for (unsigned i = 0, e = BDeadValNos.size(); i != e; ++i) |
| 397 | IntB.removeValNo(BDeadValNos[i]); |
Evan Cheng | 739583b | 2008-06-17 20:11:16 +0000 | [diff] [blame] | 398 | |
| 399 | // Extend BValNo by merging in IntA live ranges of AValNo. Val# definition |
| 400 | // is updated. Kills are also updated. |
| 401 | VNInfo *ValNo = BValNo; |
| 402 | ValNo->def = AValNo->def; |
| 403 | ValNo->copy = NULL; |
| 404 | for (unsigned j = 0, ee = ValNo->kills.size(); j != ee; ++j) { |
| 405 | unsigned Kill = ValNo->kills[j]; |
| 406 | if (Kill != BLR->end) |
| 407 | BKills.push_back(Kill); |
| 408 | } |
| 409 | ValNo->kills.clear(); |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 410 | for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end(); |
| 411 | AI != AE; ++AI) { |
| 412 | if (AI->valno != AValNo) continue; |
| 413 | unsigned End = AI->end; |
| 414 | std::map<unsigned, unsigned>::iterator EI = BExtend.find(End); |
| 415 | if (EI != BExtend.end()) |
| 416 | End = EI->second; |
| 417 | IntB.addRange(LiveRange(AI->start, End, ValNo)); |
| 418 | } |
| 419 | IntB.addKills(ValNo, BKills); |
| 420 | ValNo->hasPHIKill = BHasPHIKill; |
| 421 | |
| 422 | DOUT << " result = "; IntB.print(DOUT, tri_); |
| 423 | DOUT << "\n"; |
| 424 | |
| 425 | DOUT << "\nShortening: "; IntA.print(DOUT, tri_); |
| 426 | IntA.removeValNo(AValNo); |
| 427 | DOUT << " result = "; IntA.print(DOUT, tri_); |
| 428 | DOUT << "\n"; |
| 429 | |
| 430 | ++numCommutes; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 431 | return true; |
| 432 | } |
| 433 | |
Evan Cheng | cd04708 | 2008-08-30 09:09:33 +0000 | [diff] [blame] | 434 | /// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial |
| 435 | /// computation, replace the copy by rematerialize the definition. |
| 436 | bool SimpleRegisterCoalescing::ReMaterializeTrivialDef(LiveInterval &SrcInt, |
| 437 | unsigned DstReg, |
| 438 | MachineInstr *CopyMI) { |
| 439 | unsigned CopyIdx = li_->getUseIndex(li_->getInstructionIndex(CopyMI)); |
| 440 | LiveInterval::iterator SrcLR = SrcInt.FindLiveRangeContaining(CopyIdx); |
| 441 | if (SrcLR == SrcInt.end()) // Should never happen! |
| 442 | return false; |
| 443 | VNInfo *ValNo = SrcLR->valno; |
| 444 | // If other defs can reach uses of this def, then it's not safe to perform |
| 445 | // the optimization. |
| 446 | if (ValNo->def == ~0U || ValNo->def == ~1U || ValNo->hasPHIKill) |
| 447 | return false; |
| 448 | MachineInstr *DefMI = li_->getInstructionFromIndex(ValNo->def); |
| 449 | const TargetInstrDesc &TID = DefMI->getDesc(); |
| 450 | if (!TID.isAsCheapAsAMove()) |
| 451 | return false; |
| 452 | bool SawStore = false; |
| 453 | if (!DefMI->isSafeToMove(tii_, SawStore)) |
| 454 | return false; |
| 455 | |
| 456 | unsigned DefIdx = li_->getDefIndex(CopyIdx); |
| 457 | const LiveRange *DLR= li_->getInterval(DstReg).getLiveRangeContaining(DefIdx); |
| 458 | DLR->valno->copy = NULL; |
| 459 | |
| 460 | MachineBasicBlock::iterator MII = CopyMI; |
| 461 | MachineBasicBlock *MBB = CopyMI->getParent(); |
| 462 | tii_->reMaterialize(*MBB, MII, DstReg, DefMI); |
| 463 | MachineInstr *NewMI = prior(MII); |
| 464 | // CopyMI may have implicit instructions, transfer them over to the newly |
| 465 | // rematerialized instruction. And update implicit def interval valnos. |
| 466 | for (unsigned i = CopyMI->getDesc().getNumOperands(), |
| 467 | e = CopyMI->getNumOperands(); i != e; ++i) { |
| 468 | MachineOperand &MO = CopyMI->getOperand(i); |
Dan Gohman | 014278e | 2008-09-13 17:58:21 +0000 | [diff] [blame] | 469 | if (MO.isRegister() && MO.isImplicit()) |
Evan Cheng | cd04708 | 2008-08-30 09:09:33 +0000 | [diff] [blame] | 470 | NewMI->addOperand(MO); |
Owen Anderson | 369e987 | 2008-09-10 20:41:13 +0000 | [diff] [blame] | 471 | if (MO.isDef() && li_->hasInterval(MO.getReg())) { |
Evan Cheng | cd04708 | 2008-08-30 09:09:33 +0000 | [diff] [blame] | 472 | unsigned Reg = MO.getReg(); |
| 473 | DLR = li_->getInterval(Reg).getLiveRangeContaining(DefIdx); |
| 474 | if (DLR && DLR->valno->copy == CopyMI) |
| 475 | DLR->valno->copy = NULL; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | li_->ReplaceMachineInstrInMaps(CopyMI, NewMI); |
| 480 | CopyMI->eraseFromParent(); |
| 481 | ReMatCopies.insert(CopyMI); |
Evan Cheng | 20580a1 | 2008-09-19 17:38:47 +0000 | [diff] [blame^] | 482 | ReMatDefs.insert(DefMI); |
Evan Cheng | cd04708 | 2008-08-30 09:09:33 +0000 | [diff] [blame] | 483 | ++NumReMats; |
| 484 | return true; |
| 485 | } |
| 486 | |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 487 | /// isBackEdgeCopy - Returns true if CopyMI is a back edge copy. |
| 488 | /// |
| 489 | bool SimpleRegisterCoalescing::isBackEdgeCopy(MachineInstr *CopyMI, |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 490 | unsigned DstReg) const { |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 491 | MachineBasicBlock *MBB = CopyMI->getParent(); |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 492 | const MachineLoop *L = loopInfo->getLoopFor(MBB); |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 493 | if (!L) |
| 494 | return false; |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 495 | if (MBB != L->getLoopLatch()) |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 496 | return false; |
| 497 | |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 498 | LiveInterval &LI = li_->getInterval(DstReg); |
| 499 | unsigned DefIdx = li_->getInstructionIndex(CopyMI); |
| 500 | LiveInterval::const_iterator DstLR = |
| 501 | LI.FindLiveRangeContaining(li_->getDefIndex(DefIdx)); |
| 502 | if (DstLR == LI.end()) |
| 503 | return false; |
Owen Anderson | b3db9c9 | 2008-06-23 22:12:23 +0000 | [diff] [blame] | 504 | unsigned KillIdx = li_->getMBBEndIdx(MBB) + 1; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 505 | if (DstLR->valno->kills.size() == 1 && |
| 506 | DstLR->valno->kills[0] == KillIdx && DstLR->valno->hasPHIKill) |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 507 | return true; |
| 508 | return false; |
| 509 | } |
| 510 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 511 | /// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and |
| 512 | /// update the subregister number if it is not zero. If DstReg is a |
| 513 | /// physical register and the existing subregister number of the def / use |
| 514 | /// being updated is not zero, make sure to set it to the correct physical |
| 515 | /// subregister. |
| 516 | void |
| 517 | SimpleRegisterCoalescing::UpdateRegDefsUses(unsigned SrcReg, unsigned DstReg, |
| 518 | unsigned SubIdx) { |
| 519 | bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg); |
| 520 | if (DstIsPhys && SubIdx) { |
| 521 | // Figure out the real physical register we are updating with. |
| 522 | DstReg = tri_->getSubReg(DstReg, SubIdx); |
| 523 | SubIdx = 0; |
| 524 | } |
| 525 | |
| 526 | for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(SrcReg), |
| 527 | E = mri_->reg_end(); I != E; ) { |
| 528 | MachineOperand &O = I.getOperand(); |
Evan Cheng | 70366b9 | 2008-03-21 19:09:30 +0000 | [diff] [blame] | 529 | MachineInstr *UseMI = &*I; |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 530 | ++I; |
Evan Cheng | 621d157 | 2008-04-17 00:06:42 +0000 | [diff] [blame] | 531 | unsigned OldSubIdx = O.getSubReg(); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 532 | if (DstIsPhys) { |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 533 | unsigned UseDstReg = DstReg; |
Evan Cheng | 621d157 | 2008-04-17 00:06:42 +0000 | [diff] [blame] | 534 | if (OldSubIdx) |
| 535 | UseDstReg = tri_->getSubReg(DstReg, OldSubIdx); |
Evan Cheng | cd04708 | 2008-08-30 09:09:33 +0000 | [diff] [blame] | 536 | |
| 537 | unsigned CopySrcReg, CopyDstReg; |
| 538 | if (tii_->isMoveInstr(*UseMI, CopySrcReg, CopyDstReg) && |
| 539 | CopySrcReg != CopyDstReg && |
| 540 | CopySrcReg == SrcReg && CopyDstReg != UseDstReg) { |
| 541 | // If the use is a copy and it won't be coalesced away, and its source |
| 542 | // is defined by a trivial computation, try to rematerialize it instead. |
| 543 | if (ReMaterializeTrivialDef(li_->getInterval(SrcReg), CopyDstReg,UseMI)) |
| 544 | continue; |
| 545 | } |
| 546 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 547 | O.setReg(UseDstReg); |
| 548 | O.setSubReg(0); |
Evan Cheng | ee9e1b0 | 2008-09-12 18:13:14 +0000 | [diff] [blame] | 549 | continue; |
| 550 | } |
| 551 | |
| 552 | // Sub-register indexes goes from small to large. e.g. |
| 553 | // RAX: 1 -> AL, 2 -> AX, 3 -> EAX |
| 554 | // EAX: 1 -> AL, 2 -> AX |
| 555 | // So RAX's sub-register 2 is AX, RAX's sub-regsiter 3 is EAX, whose |
| 556 | // sub-register 2 is also AX. |
| 557 | if (SubIdx && OldSubIdx && SubIdx != OldSubIdx) |
| 558 | assert(OldSubIdx < SubIdx && "Conflicting sub-register index!"); |
| 559 | else if (SubIdx) |
| 560 | O.setSubReg(SubIdx); |
| 561 | // Remove would-be duplicated kill marker. |
| 562 | if (O.isKill() && UseMI->killsRegister(DstReg)) |
| 563 | O.setIsKill(false); |
| 564 | O.setReg(DstReg); |
| 565 | |
| 566 | // After updating the operand, check if the machine instruction has |
| 567 | // become a copy. If so, update its val# information. |
| 568 | const TargetInstrDesc &TID = UseMI->getDesc(); |
| 569 | unsigned CopySrcReg, CopyDstReg; |
| 570 | if (TID.getNumDefs() == 1 && TID.getNumOperands() > 2 && |
| 571 | tii_->isMoveInstr(*UseMI, CopySrcReg, CopyDstReg) && |
Evan Cheng | 870e4be | 2008-09-17 18:36:25 +0000 | [diff] [blame] | 572 | CopySrcReg != CopyDstReg && |
| 573 | (TargetRegisterInfo::isVirtualRegister(CopyDstReg) || |
| 574 | allocatableRegs_[CopyDstReg])) { |
Evan Cheng | ee9e1b0 | 2008-09-12 18:13:14 +0000 | [diff] [blame] | 575 | LiveInterval &LI = li_->getInterval(CopyDstReg); |
| 576 | unsigned DefIdx = li_->getDefIndex(li_->getInstructionIndex(UseMI)); |
| 577 | const LiveRange *DLR = LI.getLiveRangeContaining(DefIdx); |
Evan Cheng | 25f34a3 | 2008-09-15 06:28:41 +0000 | [diff] [blame] | 578 | if (DLR->valno->def == DefIdx) |
| 579 | DLR->valno->copy = UseMI; |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 584 | /// RemoveDeadImpDef - Remove implicit_def instructions which are "re-defining" |
| 585 | /// registers due to insert_subreg coalescing. e.g. |
| 586 | /// r1024 = op |
| 587 | /// r1025 = implicit_def |
| 588 | /// r1025 = insert_subreg r1025, r1024 |
| 589 | /// = op r1025 |
| 590 | /// => |
| 591 | /// r1025 = op |
| 592 | /// r1025 = implicit_def |
| 593 | /// r1025 = insert_subreg r1025, r1025 |
| 594 | /// = op r1025 |
| 595 | void |
| 596 | SimpleRegisterCoalescing::RemoveDeadImpDef(unsigned Reg, LiveInterval &LI) { |
| 597 | for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(Reg), |
| 598 | E = mri_->reg_end(); I != E; ) { |
| 599 | MachineOperand &O = I.getOperand(); |
| 600 | MachineInstr *DefMI = &*I; |
| 601 | ++I; |
| 602 | if (!O.isDef()) |
| 603 | continue; |
| 604 | if (DefMI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) |
| 605 | continue; |
| 606 | if (!LI.liveBeforeAndAt(li_->getInstructionIndex(DefMI))) |
| 607 | continue; |
| 608 | li_->RemoveMachineInstrFromMaps(DefMI); |
| 609 | DefMI->eraseFromParent(); |
| 610 | } |
| 611 | } |
| 612 | |
Evan Cheng | 4ff3f1c | 2008-03-10 08:11:32 +0000 | [diff] [blame] | 613 | /// RemoveUnnecessaryKills - Remove kill markers that are no longer accurate |
| 614 | /// due to live range lengthening as the result of coalescing. |
| 615 | void SimpleRegisterCoalescing::RemoveUnnecessaryKills(unsigned Reg, |
| 616 | LiveInterval &LI) { |
| 617 | for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg), |
| 618 | UE = mri_->use_end(); UI != UE; ++UI) { |
| 619 | MachineOperand &UseMO = UI.getOperand(); |
| 620 | if (UseMO.isKill()) { |
| 621 | MachineInstr *UseMI = UseMO.getParent(); |
Evan Cheng | 4ff3f1c | 2008-03-10 08:11:32 +0000 | [diff] [blame] | 622 | unsigned UseIdx = li_->getUseIndex(li_->getInstructionIndex(UseMI)); |
| 623 | if (JoinedCopies.count(UseMI)) |
| 624 | continue; |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 625 | const LiveRange *UI = LI.getLiveRangeContaining(UseIdx); |
Evan Cheng | 068b4ff | 2008-08-05 07:10:38 +0000 | [diff] [blame] | 626 | if (!UI || !LI.isKill(UI->valno, UseIdx+1)) |
Evan Cheng | 4ff3f1c | 2008-03-10 08:11:32 +0000 | [diff] [blame] | 627 | UseMO.setIsKill(false); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 632 | /// removeRange - Wrapper for LiveInterval::removeRange. This removes a range |
| 633 | /// from a physical register live interval as well as from the live intervals |
| 634 | /// of its sub-registers. |
| 635 | static void removeRange(LiveInterval &li, unsigned Start, unsigned End, |
| 636 | LiveIntervals *li_, const TargetRegisterInfo *tri_) { |
| 637 | li.removeRange(Start, End, true); |
| 638 | if (TargetRegisterInfo::isPhysicalRegister(li.reg)) { |
| 639 | for (const unsigned* SR = tri_->getSubRegisters(li.reg); *SR; ++SR) { |
| 640 | if (!li_->hasInterval(*SR)) |
| 641 | continue; |
| 642 | LiveInterval &sli = li_->getInterval(*SR); |
| 643 | unsigned RemoveEnd = Start; |
| 644 | while (RemoveEnd != End) { |
| 645 | LiveInterval::iterator LR = sli.FindLiveRangeContaining(Start); |
| 646 | if (LR == sli.end()) |
| 647 | break; |
| 648 | RemoveEnd = (LR->end < End) ? LR->end : End; |
| 649 | sli.removeRange(Start, RemoveEnd, true); |
| 650 | Start = RemoveEnd; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /// removeIntervalIfEmpty - Check if the live interval of a physical register |
| 657 | /// is empty, if so remove it and also remove the empty intervals of its |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 658 | /// sub-registers. Return true if live interval is removed. |
| 659 | static bool removeIntervalIfEmpty(LiveInterval &li, LiveIntervals *li_, |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 660 | const TargetRegisterInfo *tri_) { |
| 661 | if (li.empty()) { |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 662 | if (TargetRegisterInfo::isPhysicalRegister(li.reg)) |
| 663 | for (const unsigned* SR = tri_->getSubRegisters(li.reg); *SR; ++SR) { |
| 664 | if (!li_->hasInterval(*SR)) |
| 665 | continue; |
| 666 | LiveInterval &sli = li_->getInterval(*SR); |
| 667 | if (sli.empty()) |
| 668 | li_->removeInterval(*SR); |
| 669 | } |
Evan Cheng | d94950c | 2008-04-16 01:22:28 +0000 | [diff] [blame] | 670 | li_->removeInterval(li.reg); |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 671 | return true; |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 672 | } |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 673 | return false; |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /// ShortenDeadCopyLiveRange - Shorten a live range defined by a dead copy. |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 677 | /// Return true if live interval is removed. |
| 678 | bool SimpleRegisterCoalescing::ShortenDeadCopyLiveRange(LiveInterval &li, |
Evan Cheng | ecb2a8b | 2008-03-05 22:09:42 +0000 | [diff] [blame] | 679 | MachineInstr *CopyMI) { |
| 680 | unsigned CopyIdx = li_->getInstructionIndex(CopyMI); |
| 681 | LiveInterval::iterator MLR = |
| 682 | li.FindLiveRangeContaining(li_->getDefIndex(CopyIdx)); |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 683 | if (MLR == li.end()) |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 684 | return false; // Already removed by ShortenDeadCopySrcLiveRange. |
Evan Cheng | ecb2a8b | 2008-03-05 22:09:42 +0000 | [diff] [blame] | 685 | unsigned RemoveStart = MLR->start; |
| 686 | unsigned RemoveEnd = MLR->end; |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 687 | // Remove the liverange that's defined by this. |
| 688 | if (RemoveEnd == li_->getDefIndex(CopyIdx)+1) { |
| 689 | removeRange(li, RemoveStart, RemoveEnd, li_, tri_); |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 690 | return removeIntervalIfEmpty(li, li_, tri_); |
Evan Cheng | ecb2a8b | 2008-03-05 22:09:42 +0000 | [diff] [blame] | 691 | } |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 692 | return false; |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Evan Cheng | 0c28432 | 2008-03-26 20:15:49 +0000 | [diff] [blame] | 695 | /// PropagateDeadness - Propagate the dead marker to the instruction which |
| 696 | /// defines the val#. |
| 697 | static void PropagateDeadness(LiveInterval &li, MachineInstr *CopyMI, |
| 698 | unsigned &LRStart, LiveIntervals *li_, |
| 699 | const TargetRegisterInfo* tri_) { |
| 700 | MachineInstr *DefMI = |
| 701 | li_->getInstructionFromIndex(li_->getDefIndex(LRStart)); |
| 702 | if (DefMI && DefMI != CopyMI) { |
| 703 | int DeadIdx = DefMI->findRegisterDefOperandIdx(li.reg, false, tri_); |
| 704 | if (DeadIdx != -1) { |
| 705 | DefMI->getOperand(DeadIdx).setIsDead(); |
| 706 | // A dead def should have a single cycle interval. |
| 707 | ++LRStart; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
Evan Cheng | 883d260 | 2008-04-18 19:22:23 +0000 | [diff] [blame] | 712 | /// isSameOrFallThroughBB - Return true if MBB == SuccMBB or MBB simply |
| 713 | /// fallthoughs to SuccMBB. |
| 714 | static bool isSameOrFallThroughBB(MachineBasicBlock *MBB, |
| 715 | MachineBasicBlock *SuccMBB, |
| 716 | const TargetInstrInfo *tii_) { |
| 717 | if (MBB == SuccMBB) |
| 718 | return true; |
| 719 | MachineBasicBlock *TBB = 0, *FBB = 0; |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 720 | SmallVector<MachineOperand, 4> Cond; |
Evan Cheng | 883d260 | 2008-04-18 19:22:23 +0000 | [diff] [blame] | 721 | return !tii_->AnalyzeBranch(*MBB, TBB, FBB, Cond) && !TBB && !FBB && |
| 722 | MBB->isSuccessor(SuccMBB); |
| 723 | } |
| 724 | |
Bill Wendling | f231778 | 2008-04-17 05:20:39 +0000 | [diff] [blame] | 725 | /// ShortenDeadCopySrcLiveRange - Shorten a live range as it's artificially |
| 726 | /// extended by a dead copy. Mark the last use (if any) of the val# as kill as |
| 727 | /// ends the live range there. If there isn't another use, then this live range |
| 728 | /// is dead. Return true if live interval is removed. |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 729 | bool |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 730 | SimpleRegisterCoalescing::ShortenDeadCopySrcLiveRange(LiveInterval &li, |
| 731 | MachineInstr *CopyMI) { |
| 732 | unsigned CopyIdx = li_->getInstructionIndex(CopyMI); |
| 733 | if (CopyIdx == 0) { |
| 734 | // FIXME: special case: function live in. It can be a general case if the |
| 735 | // first instruction index starts at > 0 value. |
| 736 | assert(TargetRegisterInfo::isPhysicalRegister(li.reg)); |
| 737 | // Live-in to the function but dead. Remove it from entry live-in set. |
Evan Cheng | a971dbd | 2008-04-24 09:06:33 +0000 | [diff] [blame] | 738 | if (mf_->begin()->isLiveIn(li.reg)) |
| 739 | mf_->begin()->removeLiveIn(li.reg); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 740 | const LiveRange *LR = li.getLiveRangeContaining(CopyIdx); |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 741 | removeRange(li, LR->start, LR->end, li_, tri_); |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 742 | return removeIntervalIfEmpty(li, li_, tri_); |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | LiveInterval::iterator LR = li.FindLiveRangeContaining(CopyIdx-1); |
| 746 | if (LR == li.end()) |
| 747 | // Livein but defined by a phi. |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 748 | return false; |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 749 | |
| 750 | unsigned RemoveStart = LR->start; |
| 751 | unsigned RemoveEnd = li_->getDefIndex(CopyIdx)+1; |
| 752 | if (LR->end > RemoveEnd) |
| 753 | // More uses past this copy? Nothing to do. |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 754 | return false; |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 755 | |
Evan Cheng | 883d260 | 2008-04-18 19:22:23 +0000 | [diff] [blame] | 756 | MachineBasicBlock *CopyMBB = CopyMI->getParent(); |
| 757 | unsigned MBBStart = li_->getMBBStartIdx(CopyMBB); |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 758 | unsigned LastUseIdx; |
Evan Cheng | d2012d0 | 2008-04-10 23:48:35 +0000 | [diff] [blame] | 759 | MachineOperand *LastUse = lastRegisterUse(LR->start, CopyIdx-1, li.reg, |
| 760 | LastUseIdx); |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 761 | if (LastUse) { |
Evan Cheng | 883d260 | 2008-04-18 19:22:23 +0000 | [diff] [blame] | 762 | MachineInstr *LastUseMI = LastUse->getParent(); |
| 763 | if (!isSameOrFallThroughBB(LastUseMI->getParent(), CopyMBB, tii_)) { |
| 764 | // r1024 = op |
| 765 | // ... |
| 766 | // BB1: |
| 767 | // = r1024 |
| 768 | // |
| 769 | // BB2: |
| 770 | // r1025<dead> = r1024<kill> |
| 771 | if (MBBStart < LR->end) |
| 772 | removeRange(li, MBBStart, LR->end, li_, tri_); |
| 773 | return false; |
| 774 | } |
| 775 | |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 776 | // There are uses before the copy, just shorten the live range to the end |
| 777 | // of last use. |
| 778 | LastUse->setIsKill(); |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 779 | removeRange(li, li_->getDefIndex(LastUseIdx), LR->end, li_, tri_); |
| 780 | unsigned SrcReg, DstReg; |
| 781 | if (tii_->isMoveInstr(*LastUseMI, SrcReg, DstReg) && |
| 782 | DstReg == li.reg) { |
| 783 | // Last use is itself an identity code. |
| 784 | int DeadIdx = LastUseMI->findRegisterDefOperandIdx(li.reg, false, tri_); |
| 785 | LastUseMI->getOperand(DeadIdx).setIsDead(); |
| 786 | } |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 787 | return false; |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | // Is it livein? |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 791 | if (LR->start <= MBBStart && LR->end > MBBStart) { |
| 792 | if (LR->start == 0) { |
| 793 | assert(TargetRegisterInfo::isPhysicalRegister(li.reg)); |
| 794 | // Live-in to the function but dead. Remove it from entry live-in set. |
| 795 | mf_->begin()->removeLiveIn(li.reg); |
| 796 | } |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 797 | // FIXME: Shorten intervals in BBs that reaches this BB. |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Evan Cheng | 0c28432 | 2008-03-26 20:15:49 +0000 | [diff] [blame] | 800 | if (LR->valno->def == RemoveStart) |
| 801 | // If the def MI defines the val#, propagate the dead marker. |
| 802 | PropagateDeadness(li, CopyMI, RemoveStart, li_, tri_); |
| 803 | |
| 804 | removeRange(li, RemoveStart, LR->end, li_, tri_); |
Evan Cheng | 9c1e06e | 2008-04-16 20:24:25 +0000 | [diff] [blame] | 805 | return removeIntervalIfEmpty(li, li_, tri_); |
Evan Cheng | ecb2a8b | 2008-03-05 22:09:42 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 808 | /// CanCoalesceWithImpDef - Returns true if the specified copy instruction |
| 809 | /// from an implicit def to another register can be coalesced away. |
| 810 | bool SimpleRegisterCoalescing::CanCoalesceWithImpDef(MachineInstr *CopyMI, |
| 811 | LiveInterval &li, |
| 812 | LiveInterval &ImpLi) const{ |
| 813 | if (!CopyMI->killsRegister(ImpLi.reg)) |
| 814 | return false; |
| 815 | unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI)); |
| 816 | LiveInterval::iterator LR = li.FindLiveRangeContaining(CopyIdx); |
| 817 | if (LR == li.end()) |
| 818 | return false; |
| 819 | if (LR->valno->hasPHIKill) |
| 820 | return false; |
| 821 | if (LR->valno->def != CopyIdx) |
| 822 | return false; |
| 823 | // Make sure all of val# uses are copies. |
| 824 | for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(li.reg), |
| 825 | UE = mri_->use_end(); UI != UE;) { |
| 826 | MachineInstr *UseMI = &*UI; |
| 827 | ++UI; |
| 828 | if (JoinedCopies.count(UseMI)) |
| 829 | continue; |
| 830 | unsigned UseIdx = li_->getUseIndex(li_->getInstructionIndex(UseMI)); |
| 831 | LiveInterval::iterator ULR = li.FindLiveRangeContaining(UseIdx); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 832 | if (ULR == li.end() || ULR->valno != LR->valno) |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 833 | continue; |
| 834 | // If the use is not a use, then it's not safe to coalesce the move. |
| 835 | unsigned SrcReg, DstReg; |
| 836 | if (!tii_->isMoveInstr(*UseMI, SrcReg, DstReg)) { |
| 837 | if (UseMI->getOpcode() == TargetInstrInfo::INSERT_SUBREG && |
| 838 | UseMI->getOperand(1).getReg() == li.reg) |
| 839 | continue; |
| 840 | return false; |
| 841 | } |
| 842 | } |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | |
| 847 | /// RemoveCopiesFromValNo - The specified value# is defined by an implicit |
| 848 | /// def and it is being removed. Turn all copies from this value# into |
| 849 | /// identity copies so they will be removed. |
| 850 | void SimpleRegisterCoalescing::RemoveCopiesFromValNo(LiveInterval &li, |
| 851 | VNInfo *VNI) { |
Evan Cheng | d77d4f9 | 2008-05-28 17:40:10 +0000 | [diff] [blame] | 852 | SmallVector<MachineInstr*, 4> ImpDefs; |
Evan Cheng | d2012d0 | 2008-04-10 23:48:35 +0000 | [diff] [blame] | 853 | MachineOperand *LastUse = NULL; |
| 854 | unsigned LastUseIdx = li_->getUseIndex(VNI->def); |
| 855 | for (MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg), |
| 856 | RE = mri_->reg_end(); RI != RE;) { |
| 857 | MachineOperand *MO = &RI.getOperand(); |
| 858 | MachineInstr *MI = &*RI; |
| 859 | ++RI; |
| 860 | if (MO->isDef()) { |
| 861 | if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) { |
Evan Cheng | d77d4f9 | 2008-05-28 17:40:10 +0000 | [diff] [blame] | 862 | ImpDefs.push_back(MI); |
Evan Cheng | d2012d0 | 2008-04-10 23:48:35 +0000 | [diff] [blame] | 863 | } |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 864 | continue; |
Evan Cheng | d2012d0 | 2008-04-10 23:48:35 +0000 | [diff] [blame] | 865 | } |
| 866 | if (JoinedCopies.count(MI)) |
| 867 | continue; |
| 868 | unsigned UseIdx = li_->getUseIndex(li_->getInstructionIndex(MI)); |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 869 | LiveInterval::iterator ULR = li.FindLiveRangeContaining(UseIdx); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 870 | if (ULR == li.end() || ULR->valno != VNI) |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 871 | continue; |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 872 | // If the use is a copy, turn it into an identity copy. |
| 873 | unsigned SrcReg, DstReg; |
Evan Cheng | d2012d0 | 2008-04-10 23:48:35 +0000 | [diff] [blame] | 874 | if (tii_->isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == li.reg) { |
| 875 | // Each use MI may have multiple uses of this register. Change them all. |
| 876 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 877 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 014278e | 2008-09-13 17:58:21 +0000 | [diff] [blame] | 878 | if (MO.isRegister() && MO.getReg() == li.reg) |
Evan Cheng | d2012d0 | 2008-04-10 23:48:35 +0000 | [diff] [blame] | 879 | MO.setReg(DstReg); |
| 880 | } |
| 881 | JoinedCopies.insert(MI); |
| 882 | } else if (UseIdx > LastUseIdx) { |
| 883 | LastUseIdx = UseIdx; |
| 884 | LastUse = MO; |
Evan Cheng | 172b70c | 2008-04-10 18:38:47 +0000 | [diff] [blame] | 885 | } |
Evan Cheng | d2012d0 | 2008-04-10 23:48:35 +0000 | [diff] [blame] | 886 | } |
| 887 | if (LastUse) |
| 888 | LastUse->setIsKill(); |
| 889 | else { |
Evan Cheng | d77d4f9 | 2008-05-28 17:40:10 +0000 | [diff] [blame] | 890 | // Remove dead implicit_def's. |
| 891 | while (!ImpDefs.empty()) { |
| 892 | MachineInstr *ImpDef = ImpDefs.back(); |
| 893 | ImpDefs.pop_back(); |
| 894 | li_->RemoveMachineInstrFromMaps(ImpDef); |
| 895 | ImpDef->eraseFromParent(); |
| 896 | } |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
Evan Cheng | 8db8668 | 2008-09-11 20:07:10 +0000 | [diff] [blame] | 900 | /// getMatchingSuperReg - Return a super-register of the specified register |
| 901 | /// Reg so its sub-register of index SubIdx is Reg. |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 902 | static unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx, |
| 903 | const TargetRegisterClass *RC, |
| 904 | const TargetRegisterInfo* TRI) { |
| 905 | for (const unsigned *SRs = TRI->getSuperRegisters(Reg); |
| 906 | unsigned SR = *SRs; ++SRs) |
| 907 | if (Reg == TRI->getSubReg(SR, SubIdx) && RC->contains(SR)) |
| 908 | return SR; |
| 909 | return 0; |
| 910 | } |
| 911 | |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 912 | /// isProfitableToCoalesceToSubRC - Given that register class of DstReg is |
| 913 | /// a subset of the register class of SrcReg, return true if it's profitable |
| 914 | /// to coalesce the two registers. |
| 915 | bool |
| 916 | SimpleRegisterCoalescing::isProfitableToCoalesceToSubRC(unsigned SrcReg, |
| 917 | unsigned DstReg, |
| 918 | MachineBasicBlock *MBB){ |
| 919 | if (!CrossClassJoin) |
| 920 | return false; |
| 921 | |
| 922 | // First let's make sure all uses are in the same MBB. |
| 923 | for (MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(SrcReg), |
| 924 | RE = mri_->reg_end(); RI != RE; ++RI) { |
| 925 | MachineInstr &MI = *RI; |
| 926 | if (MI.getParent() != MBB) |
| 927 | return false; |
| 928 | } |
| 929 | for (MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(DstReg), |
| 930 | RE = mri_->reg_end(); RI != RE; ++RI) { |
| 931 | MachineInstr &MI = *RI; |
| 932 | if (MI.getParent() != MBB) |
| 933 | return false; |
| 934 | } |
| 935 | |
| 936 | // Then make sure the intervals are *short*. |
| 937 | LiveInterval &SrcInt = li_->getInterval(SrcReg); |
| 938 | LiveInterval &DstInt = li_->getInterval(DstReg); |
Owen Anderson | a1566f2 | 2008-07-22 22:46:49 +0000 | [diff] [blame] | 939 | unsigned SrcSize = li_->getApproximateInstructionCount(SrcInt); |
| 940 | unsigned DstSize = li_->getApproximateInstructionCount(DstInt); |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 941 | const TargetRegisterClass *RC = mri_->getRegClass(DstReg); |
| 942 | unsigned Threshold = allocatableRCRegs_[RC].count() * 2; |
| 943 | return (SrcSize + DstSize) <= Threshold; |
| 944 | } |
| 945 | |
Evan Cheng | 8db8668 | 2008-09-11 20:07:10 +0000 | [diff] [blame] | 946 | /// HasIncompatibleSubRegDefUse - If we are trying to coalesce a virtual |
| 947 | /// register with a physical register, check if any of the virtual register |
| 948 | /// operand is a sub-register use or def. If so, make sure it won't result |
| 949 | /// in an illegal extract_subreg or insert_subreg instruction. e.g. |
| 950 | /// vr1024 = extract_subreg vr1025, 1 |
| 951 | /// ... |
| 952 | /// vr1024 = mov8rr AH |
| 953 | /// If vr1024 is coalesced with AH, the extract_subreg is now illegal since |
| 954 | /// AH does not have a super-reg whose sub-register 1 is AH. |
| 955 | bool |
| 956 | SimpleRegisterCoalescing::HasIncompatibleSubRegDefUse(MachineInstr *CopyMI, |
| 957 | unsigned VirtReg, |
| 958 | unsigned PhysReg) { |
| 959 | for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(VirtReg), |
| 960 | E = mri_->reg_end(); I != E; ++I) { |
| 961 | MachineOperand &O = I.getOperand(); |
| 962 | MachineInstr *MI = &*I; |
| 963 | if (MI == CopyMI || JoinedCopies.count(MI)) |
| 964 | continue; |
| 965 | unsigned SubIdx = O.getSubReg(); |
| 966 | if (SubIdx && !tri_->getSubReg(PhysReg, SubIdx)) |
| 967 | return true; |
| 968 | if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) { |
| 969 | SubIdx = MI->getOperand(2).getImm(); |
| 970 | if (O.isUse() && !tri_->getSubReg(PhysReg, SubIdx)) |
| 971 | return true; |
| 972 | if (O.isDef()) { |
| 973 | unsigned SrcReg = MI->getOperand(1).getReg(); |
| 974 | const TargetRegisterClass *RC = |
| 975 | TargetRegisterInfo::isPhysicalRegister(SrcReg) |
| 976 | ? tri_->getPhysicalRegisterRegClass(SrcReg) |
| 977 | : mri_->getRegClass(SrcReg); |
| 978 | if (!getMatchingSuperReg(PhysReg, SubIdx, RC, tri_)) |
| 979 | return true; |
| 980 | } |
| 981 | } |
| 982 | if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) { |
| 983 | SubIdx = MI->getOperand(3).getImm(); |
| 984 | if (VirtReg == MI->getOperand(0).getReg()) { |
| 985 | if (!tri_->getSubReg(PhysReg, SubIdx)) |
| 986 | return true; |
| 987 | } else { |
| 988 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 989 | const TargetRegisterClass *RC = |
| 990 | TargetRegisterInfo::isPhysicalRegister(DstReg) |
| 991 | ? tri_->getPhysicalRegisterRegClass(DstReg) |
| 992 | : mri_->getRegClass(DstReg); |
| 993 | if (!getMatchingSuperReg(PhysReg, SubIdx, RC, tri_)) |
| 994 | return true; |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | return false; |
| 999 | } |
| 1000 | |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 1001 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1002 | /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg, |
| 1003 | /// which are the src/dst of the copy instruction CopyMI. This returns true |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1004 | /// if the copy was successfully coalesced away. If it is not currently |
| 1005 | /// possible to coalesce this interval, but it may be possible if other |
| 1006 | /// things get coalesced, then it returns true by reference in 'Again'. |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 1007 | bool SimpleRegisterCoalescing::JoinCopy(CopyRec &TheCopy, bool &Again) { |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1008 | MachineInstr *CopyMI = TheCopy.MI; |
| 1009 | |
| 1010 | Again = false; |
Evan Cheng | cd04708 | 2008-08-30 09:09:33 +0000 | [diff] [blame] | 1011 | if (JoinedCopies.count(CopyMI) || ReMatCopies.count(CopyMI)) |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1012 | return false; // Already done. |
| 1013 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1014 | DOUT << li_->getInstructionIndex(CopyMI) << '\t' << *CopyMI; |
| 1015 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1016 | unsigned SrcReg; |
| 1017 | unsigned DstReg; |
| 1018 | bool isExtSubReg = CopyMI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG; |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1019 | bool isInsSubReg = CopyMI->getOpcode() == TargetInstrInfo::INSERT_SUBREG; |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1020 | unsigned SubIdx = 0; |
| 1021 | if (isExtSubReg) { |
| 1022 | DstReg = CopyMI->getOperand(0).getReg(); |
| 1023 | SrcReg = CopyMI->getOperand(1).getReg(); |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1024 | } else if (isInsSubReg) { |
| 1025 | if (CopyMI->getOperand(2).getSubReg()) { |
| 1026 | DOUT << "\tSource of insert_subreg is already coalesced " |
| 1027 | << "to another register.\n"; |
| 1028 | return false; // Not coalescable. |
| 1029 | } |
| 1030 | DstReg = CopyMI->getOperand(0).getReg(); |
| 1031 | SrcReg = CopyMI->getOperand(2).getReg(); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1032 | } else if (!tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) { |
| 1033 | assert(0 && "Unrecognized copy instruction!"); |
| 1034 | return false; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1037 | // If they are already joined we continue. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1038 | if (SrcReg == DstReg) { |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1039 | DOUT << "\tCopy already coalesced.\n"; |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1040 | return false; // Not coalescable. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1043 | bool SrcIsPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg); |
| 1044 | bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1045 | |
| 1046 | // If they are both physical registers, we cannot join them. |
| 1047 | if (SrcIsPhys && DstIsPhys) { |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1048 | DOUT << "\tCan not coalesce physregs.\n"; |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1049 | return false; // Not coalescable. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | // We only join virtual registers with allocatable physical registers. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1053 | if (SrcIsPhys && !allocatableRegs_[SrcReg]) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1054 | DOUT << "\tSrc reg is unallocatable physreg.\n"; |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1055 | return false; // Not coalescable. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1056 | } |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1057 | if (DstIsPhys && !allocatableRegs_[DstReg]) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1058 | DOUT << "\tDst reg is unallocatable physreg.\n"; |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1059 | return false; // Not coalescable. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1060 | } |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1061 | |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 1062 | // Should be non-null only when coalescing to a sub-register class. |
| 1063 | const TargetRegisterClass *SubRC = NULL; |
| 1064 | MachineBasicBlock *CopyMBB = CopyMI->getParent(); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1065 | unsigned RealDstReg = 0; |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1066 | unsigned RealSrcReg = 0; |
| 1067 | if (isExtSubReg || isInsSubReg) { |
| 1068 | SubIdx = CopyMI->getOperand(isExtSubReg ? 2 : 3).getImm(); |
| 1069 | if (SrcIsPhys && isExtSubReg) { |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1070 | // r1024 = EXTRACT_SUBREG EAX, 0 then r1024 is really going to be |
| 1071 | // coalesced with AX. |
Evan Cheng | 621d157 | 2008-04-17 00:06:42 +0000 | [diff] [blame] | 1072 | unsigned DstSubIdx = CopyMI->getOperand(0).getSubReg(); |
Evan Cheng | 639f493 | 2008-04-17 07:58:04 +0000 | [diff] [blame] | 1073 | if (DstSubIdx) { |
| 1074 | // r1024<2> = EXTRACT_SUBREG EAX, 2. Then r1024 has already been |
| 1075 | // coalesced to a larger register so the subreg indices cancel out. |
| 1076 | if (DstSubIdx != SubIdx) { |
| 1077 | DOUT << "\t Sub-register indices mismatch.\n"; |
| 1078 | return false; // Not coalescable. |
| 1079 | } |
| 1080 | } else |
Evan Cheng | 621d157 | 2008-04-17 00:06:42 +0000 | [diff] [blame] | 1081 | SrcReg = tri_->getSubReg(SrcReg, SubIdx); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1082 | SubIdx = 0; |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1083 | } else if (DstIsPhys && isInsSubReg) { |
| 1084 | // EAX = INSERT_SUBREG EAX, r1024, 0 |
Evan Cheng | 621d157 | 2008-04-17 00:06:42 +0000 | [diff] [blame] | 1085 | unsigned SrcSubIdx = CopyMI->getOperand(2).getSubReg(); |
Evan Cheng | 639f493 | 2008-04-17 07:58:04 +0000 | [diff] [blame] | 1086 | if (SrcSubIdx) { |
| 1087 | // EAX = INSERT_SUBREG EAX, r1024<2>, 2 Then r1024 has already been |
| 1088 | // coalesced to a larger register so the subreg indices cancel out. |
| 1089 | if (SrcSubIdx != SubIdx) { |
| 1090 | DOUT << "\t Sub-register indices mismatch.\n"; |
| 1091 | return false; // Not coalescable. |
| 1092 | } |
| 1093 | } else |
| 1094 | DstReg = tri_->getSubReg(DstReg, SubIdx); |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1095 | SubIdx = 0; |
| 1096 | } else if ((DstIsPhys && isExtSubReg) || (SrcIsPhys && isInsSubReg)) { |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1097 | // If this is a extract_subreg where dst is a physical register, e.g. |
| 1098 | // cl = EXTRACT_SUBREG reg1024, 1 |
| 1099 | // then create and update the actual physical register allocated to RHS. |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1100 | // Ditto for |
| 1101 | // reg1024 = INSERT_SUBREG r1024, cl, 1 |
Evan Cheng | 639f493 | 2008-04-17 07:58:04 +0000 | [diff] [blame] | 1102 | if (CopyMI->getOperand(1).getSubReg()) { |
| 1103 | DOUT << "\tSrc of extract_ / insert_subreg already coalesced with reg" |
| 1104 | << " of a super-class.\n"; |
| 1105 | return false; // Not coalescable. |
| 1106 | } |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1107 | const TargetRegisterClass *RC = |
| 1108 | mri_->getRegClass(isExtSubReg ? SrcReg : DstReg); |
| 1109 | if (isExtSubReg) { |
| 1110 | RealDstReg = getMatchingSuperReg(DstReg, SubIdx, RC, tri_); |
Evan Cheng | 8ec3389 | 2008-09-10 00:30:50 +0000 | [diff] [blame] | 1111 | assert(RealDstReg && "Invalid extract_subreg instruction!"); |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1112 | } else { |
| 1113 | RealSrcReg = getMatchingSuperReg(SrcReg, SubIdx, RC, tri_); |
Evan Cheng | 8ec3389 | 2008-09-10 00:30:50 +0000 | [diff] [blame] | 1114 | assert(RealSrcReg && "Invalid extract_subreg instruction!"); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1115 | } |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1116 | |
| 1117 | // For this type of EXTRACT_SUBREG, conservatively |
| 1118 | // check if the live interval of the source register interfere with the |
| 1119 | // actual super physical register we are trying to coalesce with. |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1120 | unsigned PhysReg = isExtSubReg ? RealDstReg : RealSrcReg; |
| 1121 | LiveInterval &RHS = li_->getInterval(isExtSubReg ? SrcReg : DstReg); |
| 1122 | if (li_->hasInterval(PhysReg) && |
| 1123 | RHS.overlaps(li_->getInterval(PhysReg))) { |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1124 | DOUT << "Interfere with register "; |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1125 | DEBUG(li_->getInterval(PhysReg).print(DOUT, tri_)); |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1126 | return false; // Not coalescable |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1127 | } |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1128 | for (const unsigned* SR = tri_->getSubRegisters(PhysReg); *SR; ++SR) |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1129 | if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) { |
| 1130 | DOUT << "Interfere with sub-register "; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1131 | DEBUG(li_->getInterval(*SR).print(DOUT, tri_)); |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1132 | return false; // Not coalescable |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1133 | } |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1134 | SubIdx = 0; |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1135 | } else { |
Evan Cheng | 639f493 | 2008-04-17 07:58:04 +0000 | [diff] [blame] | 1136 | unsigned OldSubIdx = isExtSubReg ? CopyMI->getOperand(0).getSubReg() |
| 1137 | : CopyMI->getOperand(2).getSubReg(); |
| 1138 | if (OldSubIdx) { |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 1139 | if (OldSubIdx == SubIdx && |
| 1140 | !differingRegisterClasses(SrcReg, DstReg, SubRC)) |
Evan Cheng | 639f493 | 2008-04-17 07:58:04 +0000 | [diff] [blame] | 1141 | // r1024<2> = EXTRACT_SUBREG r1025, 2. Then r1024 has already been |
| 1142 | // coalesced to a larger register so the subreg indices cancel out. |
Evan Cheng | 8509fcf | 2008-04-29 01:41:44 +0000 | [diff] [blame] | 1143 | // Also check if the other larger register is of the same register |
| 1144 | // class as the would be resulting register. |
Evan Cheng | 639f493 | 2008-04-17 07:58:04 +0000 | [diff] [blame] | 1145 | SubIdx = 0; |
| 1146 | else { |
| 1147 | DOUT << "\t Sub-register indices mismatch.\n"; |
| 1148 | return false; // Not coalescable. |
| 1149 | } |
| 1150 | } |
| 1151 | if (SubIdx) { |
| 1152 | unsigned LargeReg = isExtSubReg ? SrcReg : DstReg; |
| 1153 | unsigned SmallReg = isExtSubReg ? DstReg : SrcReg; |
Owen Anderson | a1566f2 | 2008-07-22 22:46:49 +0000 | [diff] [blame] | 1154 | unsigned LargeRegSize = |
| 1155 | li_->getApproximateInstructionCount(li_->getInterval(LargeReg)); |
| 1156 | unsigned SmallRegSize = |
| 1157 | li_->getApproximateInstructionCount(li_->getInterval(SmallReg)); |
Evan Cheng | 639f493 | 2008-04-17 07:58:04 +0000 | [diff] [blame] | 1158 | const TargetRegisterClass *RC = mri_->getRegClass(SmallReg); |
| 1159 | unsigned Threshold = allocatableRCRegs_[RC].count(); |
| 1160 | // Be conservative. If both sides are virtual registers, do not coalesce |
| 1161 | // if this will cause a high use density interval to target a smaller |
| 1162 | // set of registers. |
| 1163 | if (SmallRegSize > Threshold || LargeRegSize > Threshold) { |
Owen Anderson | dbb8137 | 2008-05-30 22:37:27 +0000 | [diff] [blame] | 1164 | if ((float)std::distance(mri_->use_begin(SmallReg), |
| 1165 | mri_->use_end()) / SmallRegSize < |
| 1166 | (float)std::distance(mri_->use_begin(LargeReg), |
| 1167 | mri_->use_end()) / LargeRegSize) { |
Evan Cheng | 639f493 | 2008-04-17 07:58:04 +0000 | [diff] [blame] | 1168 | Again = true; // May be possible to coalesce later. |
| 1169 | return false; |
| 1170 | } |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1173 | } |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 1174 | } else if (differingRegisterClasses(SrcReg, DstReg, SubRC)) { |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1175 | // FIXME: What if the resul of a EXTRACT_SUBREG is then coalesced |
| 1176 | // with another? If it's the resulting destination register, then |
| 1177 | // the subidx must be propagated to uses (but only those defined |
| 1178 | // by the EXTRACT_SUBREG). If it's being coalesced into another |
| 1179 | // register, it should be safe because register is assumed to have |
| 1180 | // the register class of the super-register. |
| 1181 | |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 1182 | if (!SubRC || !isProfitableToCoalesceToSubRC(SrcReg, DstReg, CopyMBB)) { |
| 1183 | // If they are not of the same register class, we cannot join them. |
| 1184 | DOUT << "\tSrc/Dest are different register classes.\n"; |
| 1185 | // Allow the coalescer to try again in case either side gets coalesced to |
| 1186 | // a physical register that's compatible with the other side. e.g. |
| 1187 | // r1024 = MOV32to32_ r1025 |
| 1188 | // but later r1024 is assigned EAX then r1025 may be coalesced with EAX. |
| 1189 | Again = true; // May be possible to coalesce later. |
| 1190 | return false; |
| 1191 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1192 | } |
Evan Cheng | 8db8668 | 2008-09-11 20:07:10 +0000 | [diff] [blame] | 1193 | |
| 1194 | // Will it create illegal extract_subreg / insert_subreg? |
| 1195 | if (SrcIsPhys && HasIncompatibleSubRegDefUse(CopyMI, DstReg, SrcReg)) |
| 1196 | return false; |
| 1197 | if (DstIsPhys && HasIncompatibleSubRegDefUse(CopyMI, SrcReg, DstReg)) |
| 1198 | return false; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1199 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1200 | LiveInterval &SrcInt = li_->getInterval(SrcReg); |
| 1201 | LiveInterval &DstInt = li_->getInterval(DstReg); |
| 1202 | assert(SrcInt.reg == SrcReg && DstInt.reg == DstReg && |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1203 | "Register mapping is horribly broken!"); |
| 1204 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1205 | DOUT << "\t\tInspecting "; SrcInt.print(DOUT, tri_); |
| 1206 | DOUT << " and "; DstInt.print(DOUT, tri_); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1207 | DOUT << ": "; |
| 1208 | |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 1209 | // Check if it is necessary to propagate "isDead" property. |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1210 | if (!isExtSubReg && !isInsSubReg) { |
| 1211 | MachineOperand *mopd = CopyMI->findRegisterDefOperand(DstReg, false); |
| 1212 | bool isDead = mopd->isDead(); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1213 | |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1214 | // We need to be careful about coalescing a source physical register with a |
| 1215 | // virtual register. Once the coalescing is done, it cannot be broken and |
| 1216 | // these are not spillable! If the destination interval uses are far away, |
| 1217 | // think twice about coalescing them! |
| 1218 | if (!isDead && (SrcIsPhys || DstIsPhys)) { |
| 1219 | LiveInterval &JoinVInt = SrcIsPhys ? DstInt : SrcInt; |
| 1220 | unsigned JoinVReg = SrcIsPhys ? DstReg : SrcReg; |
| 1221 | unsigned JoinPReg = SrcIsPhys ? SrcReg : DstReg; |
| 1222 | const TargetRegisterClass *RC = mri_->getRegClass(JoinVReg); |
| 1223 | unsigned Threshold = allocatableRCRegs_[RC].count() * 2; |
| 1224 | if (TheCopy.isBackEdge) |
| 1225 | Threshold *= 2; // Favors back edge copies. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1226 | |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1227 | // If the virtual register live interval is long but it has low use desity, |
| 1228 | // do not join them, instead mark the physical register as its allocation |
| 1229 | // preference. |
Owen Anderson | a1566f2 | 2008-07-22 22:46:49 +0000 | [diff] [blame] | 1230 | unsigned Length = li_->getApproximateInstructionCount(JoinVInt); |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1231 | if (Length > Threshold && |
Owen Anderson | dbb8137 | 2008-05-30 22:37:27 +0000 | [diff] [blame] | 1232 | (((float)std::distance(mri_->use_begin(JoinVReg), |
| 1233 | mri_->use_end()) / Length) < (1.0 / Threshold))) { |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1234 | JoinVInt.preference = JoinPReg; |
| 1235 | ++numAborts; |
| 1236 | DOUT << "\tMay tie down a physical register, abort!\n"; |
| 1237 | Again = true; // May be possible to coalesce later. |
| 1238 | return false; |
| 1239 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | // Okay, attempt to join these two intervals. On failure, this returns false. |
| 1244 | // Otherwise, if one of the intervals being joined is a physreg, this method |
| 1245 | // always canonicalizes DstInt to be it. The output "SrcInt" will not have |
| 1246 | // been modified, so we can use this information below to update aliases. |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 1247 | bool Swapped = false; |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 1248 | // If SrcInt is implicitly defined, it's safe to coalesce. |
| 1249 | bool isEmpty = SrcInt.empty(); |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1250 | if (isEmpty && !CanCoalesceWithImpDef(CopyMI, DstInt, SrcInt)) { |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 1251 | // Only coalesce an empty interval (defined by implicit_def) with |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1252 | // another interval which has a valno defined by the CopyMI and the CopyMI |
| 1253 | // is a kill of the implicit def. |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 1254 | DOUT << "Not profitable!\n"; |
| 1255 | return false; |
| 1256 | } |
| 1257 | |
| 1258 | if (!isEmpty && !JoinIntervals(DstInt, SrcInt, Swapped)) { |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1259 | // Coalescing failed. |
Evan Cheng | cd04708 | 2008-08-30 09:09:33 +0000 | [diff] [blame] | 1260 | |
| 1261 | // If definition of source is defined by trivial computation, try |
| 1262 | // rematerializing it. |
| 1263 | if (!isExtSubReg && !isInsSubReg && |
| 1264 | ReMaterializeTrivialDef(SrcInt, DstInt.reg, CopyMI)) |
| 1265 | return true; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1266 | |
| 1267 | // If we can eliminate the copy without merging the live ranges, do so now. |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1268 | if (!isExtSubReg && !isInsSubReg && |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 1269 | (AdjustCopiesBackFrom(SrcInt, DstInt, CopyMI) || |
| 1270 | RemoveCopyByCommutingDef(SrcInt, DstInt, CopyMI))) { |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1271 | JoinedCopies.insert(CopyMI); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1272 | return true; |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1273 | } |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 1274 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1275 | // Otherwise, we are unable to join the intervals. |
| 1276 | DOUT << "Interference!\n"; |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1277 | Again = true; // May be possible to coalesce later. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1278 | return false; |
| 1279 | } |
| 1280 | |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 1281 | LiveInterval *ResSrcInt = &SrcInt; |
| 1282 | LiveInterval *ResDstInt = &DstInt; |
| 1283 | if (Swapped) { |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1284 | std::swap(SrcReg, DstReg); |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 1285 | std::swap(ResSrcInt, ResDstInt); |
| 1286 | } |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1287 | assert(TargetRegisterInfo::isVirtualRegister(SrcReg) && |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1288 | "LiveInterval::join didn't work right!"); |
| 1289 | |
| 1290 | // If we're about to merge live ranges into a physical register live range, |
| 1291 | // we have to update any aliased register's live ranges to indicate that they |
| 1292 | // have clobbered values for this range. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1293 | if (TargetRegisterInfo::isPhysicalRegister(DstReg)) { |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1294 | // If this is a extract_subreg where dst is a physical register, e.g. |
| 1295 | // cl = EXTRACT_SUBREG reg1024, 1 |
| 1296 | // then create and update the actual physical register allocated to RHS. |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1297 | if (RealDstReg || RealSrcReg) { |
| 1298 | LiveInterval &RealInt = |
| 1299 | li_->getOrCreateInterval(RealDstReg ? RealDstReg : RealSrcReg); |
Evan Cheng | f5c7359 | 2007-10-15 18:33:50 +0000 | [diff] [blame] | 1300 | SmallSet<const VNInfo*, 4> CopiedValNos; |
| 1301 | for (LiveInterval::Ranges::const_iterator I = ResSrcInt->ranges.begin(), |
| 1302 | E = ResSrcInt->ranges.end(); I != E; ++I) { |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 1303 | const LiveRange *DstLR = ResDstInt->getLiveRangeContaining(I->start); |
| 1304 | assert(DstLR && "Invalid joined interval!"); |
Evan Cheng | f5c7359 | 2007-10-15 18:33:50 +0000 | [diff] [blame] | 1305 | const VNInfo *DstValNo = DstLR->valno; |
| 1306 | if (CopiedValNos.insert(DstValNo)) { |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1307 | VNInfo *ValNo = RealInt.getNextValue(DstValNo->def, DstValNo->copy, |
| 1308 | li_->getVNInfoAllocator()); |
Evan Cheng | c3fc7d9 | 2007-11-29 09:49:23 +0000 | [diff] [blame] | 1309 | ValNo->hasPHIKill = DstValNo->hasPHIKill; |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1310 | RealInt.addKills(ValNo, DstValNo->kills); |
| 1311 | RealInt.MergeValueInAsValue(*ResDstInt, DstValNo, ValNo); |
Evan Cheng | f5c7359 | 2007-10-15 18:33:50 +0000 | [diff] [blame] | 1312 | } |
Evan Cheng | 3472925 | 2007-10-14 10:08:34 +0000 | [diff] [blame] | 1313 | } |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1314 | |
| 1315 | DstReg = RealDstReg ? RealDstReg : RealSrcReg; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1318 | // Update the liveintervals of sub-registers. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1319 | for (const unsigned *AS = tri_->getSubRegisters(DstReg); *AS; ++AS) |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1320 | li_->getOrCreateInterval(*AS).MergeInClobberRanges(*ResSrcInt, |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 1321 | li_->getVNInfoAllocator()); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1324 | // If this is a EXTRACT_SUBREG, make sure the result of coalescing is the |
| 1325 | // larger super-register. |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1326 | if ((isExtSubReg || isInsSubReg) && !SrcIsPhys && !DstIsPhys) { |
| 1327 | if ((isExtSubReg && !Swapped) || (isInsSubReg && Swapped)) { |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1328 | ResSrcInt->Copy(*ResDstInt, li_->getVNInfoAllocator()); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1329 | std::swap(SrcReg, DstReg); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1330 | std::swap(ResSrcInt, ResDstInt); |
| 1331 | } |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 1334 | // Coalescing to a virtual register that is of a sub-register class of the |
| 1335 | // other. Make sure the resulting register is set to the right register class. |
| 1336 | if (SubRC) { |
| 1337 | mri_->setRegClass(DstReg, SubRC); |
| 1338 | ++numSubJoins; |
| 1339 | } |
| 1340 | |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1341 | if (NewHeuristic) { |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1342 | // Add all copies that define val# in the source interval into the queue. |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1343 | for (LiveInterval::const_vni_iterator i = ResSrcInt->vni_begin(), |
| 1344 | e = ResSrcInt->vni_end(); i != e; ++i) { |
| 1345 | const VNInfo *vni = *i; |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1346 | if (!vni->def || vni->def == ~1U || vni->def == ~0U) |
| 1347 | continue; |
| 1348 | MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def); |
| 1349 | unsigned NewSrcReg, NewDstReg; |
| 1350 | if (CopyMI && |
| 1351 | JoinedCopies.count(CopyMI) == 0 && |
| 1352 | tii_->isMoveInstr(*CopyMI, NewSrcReg, NewDstReg)) { |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 1353 | unsigned LoopDepth = loopInfo->getLoopDepth(CopyMBB); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1354 | JoinQueue->push(CopyRec(CopyMI, LoopDepth, |
| 1355 | isBackEdgeCopy(CopyMI, DstReg))); |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1360 | // Remember to delete the copy instruction. |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1361 | JoinedCopies.insert(CopyMI); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1362 | |
Evan Cheng | 4ff3f1c | 2008-03-10 08:11:32 +0000 | [diff] [blame] | 1363 | // Some live range has been lengthened due to colaescing, eliminate the |
| 1364 | // unnecessary kills. |
| 1365 | RemoveUnnecessaryKills(SrcReg, *ResDstInt); |
| 1366 | if (TargetRegisterInfo::isVirtualRegister(DstReg)) |
| 1367 | RemoveUnnecessaryKills(DstReg, *ResDstInt); |
| 1368 | |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1369 | if (isInsSubReg) |
| 1370 | // Avoid: |
| 1371 | // r1024 = op |
| 1372 | // r1024 = implicit_def |
| 1373 | // ... |
| 1374 | // = r1024 |
| 1375 | RemoveDeadImpDef(DstReg, *ResDstInt); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1376 | UpdateRegDefsUses(SrcReg, DstReg, SubIdx); |
| 1377 | |
Evan Cheng | cd04708 | 2008-08-30 09:09:33 +0000 | [diff] [blame] | 1378 | // SrcReg is guarateed to be the register whose live interval that is |
| 1379 | // being merged. |
| 1380 | li_->removeInterval(SrcReg); |
| 1381 | |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 1382 | if (isEmpty) { |
| 1383 | // Now the copy is being coalesced away, the val# previously defined |
| 1384 | // by the copy is being defined by an IMPLICIT_DEF which defines a zero |
| 1385 | // length interval. Remove the val#. |
| 1386 | unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI)); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 1387 | const LiveRange *LR = ResDstInt->getLiveRangeContaining(CopyIdx); |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 1388 | VNInfo *ImpVal = LR->valno; |
| 1389 | assert(ImpVal->def == CopyIdx); |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1390 | unsigned NextDef = LR->end; |
| 1391 | RemoveCopiesFromValNo(*ResDstInt, ImpVal); |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 1392 | ResDstInt->removeValNo(ImpVal); |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1393 | LR = ResDstInt->FindLiveRangeContaining(NextDef); |
| 1394 | if (LR != ResDstInt->end() && LR->valno->def == NextDef) { |
| 1395 | // Special case: vr1024 = implicit_def |
| 1396 | // vr1024 = insert_subreg vr1024, vr1025, c |
| 1397 | // The insert_subreg becomes a "copy" that defines a val# which can itself |
| 1398 | // be coalesced away. |
| 1399 | MachineInstr *DefMI = li_->getInstructionFromIndex(NextDef); |
| 1400 | if (DefMI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) |
| 1401 | LR->valno->copy = DefMI; |
| 1402 | } |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Evan Cheng | 3ef2d60 | 2008-09-09 21:44:23 +0000 | [diff] [blame] | 1405 | // If resulting interval has a preference that no longer fits because of subreg |
| 1406 | // coalescing, just clear the preference. |
Evan Cheng | 4086906 | 2008-09-11 18:40:32 +0000 | [diff] [blame] | 1407 | if (ResDstInt->preference && (isExtSubReg || isInsSubReg) && |
| 1408 | TargetRegisterInfo::isVirtualRegister(ResDstInt->reg)) { |
Evan Cheng | 3ef2d60 | 2008-09-09 21:44:23 +0000 | [diff] [blame] | 1409 | const TargetRegisterClass *RC = mri_->getRegClass(ResDstInt->reg); |
| 1410 | if (!RC->contains(ResDstInt->preference)) |
| 1411 | ResDstInt->preference = 0; |
| 1412 | } |
| 1413 | |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 1414 | DOUT << "\n\t\tJoined. Result = "; ResDstInt->print(DOUT, tri_); |
| 1415 | DOUT << "\n"; |
| 1416 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1417 | ++numJoins; |
| 1418 | return true; |
| 1419 | } |
| 1420 | |
| 1421 | /// ComputeUltimateVN - Assuming we are going to join two live intervals, |
| 1422 | /// compute what the resultant value numbers for each value in the input two |
| 1423 | /// ranges will be. This is complicated by copies between the two which can |
| 1424 | /// and will commonly cause multiple value numbers to be merged into one. |
| 1425 | /// |
| 1426 | /// VN is the value number that we're trying to resolve. InstDefiningValue |
| 1427 | /// keeps track of the new InstDefiningValue assignment for the result |
| 1428 | /// LiveInterval. ThisFromOther/OtherFromThis are sets that keep track of |
| 1429 | /// whether a value in this or other is a copy from the opposite set. |
| 1430 | /// ThisValNoAssignments/OtherValNoAssignments keep track of value #'s that have |
| 1431 | /// already been assigned. |
| 1432 | /// |
| 1433 | /// ThisFromOther[x] - If x is defined as a copy from the other interval, this |
| 1434 | /// contains the value number the copy is from. |
| 1435 | /// |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1436 | static unsigned ComputeUltimateVN(VNInfo *VNI, |
| 1437 | SmallVector<VNInfo*, 16> &NewVNInfo, |
Evan Cheng | fadfb5b | 2007-08-31 21:23:06 +0000 | [diff] [blame] | 1438 | DenseMap<VNInfo*, VNInfo*> &ThisFromOther, |
| 1439 | DenseMap<VNInfo*, VNInfo*> &OtherFromThis, |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1440 | SmallVector<int, 16> &ThisValNoAssignments, |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1441 | SmallVector<int, 16> &OtherValNoAssignments) { |
| 1442 | unsigned VN = VNI->id; |
| 1443 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1444 | // If the VN has already been computed, just return it. |
| 1445 | if (ThisValNoAssignments[VN] >= 0) |
| 1446 | return ThisValNoAssignments[VN]; |
| 1447 | // assert(ThisValNoAssignments[VN] != -2 && "Cyclic case?"); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1448 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1449 | // If this val is not a copy from the other val, then it must be a new value |
| 1450 | // number in the destination. |
Evan Cheng | fadfb5b | 2007-08-31 21:23:06 +0000 | [diff] [blame] | 1451 | DenseMap<VNInfo*, VNInfo*>::iterator I = ThisFromOther.find(VNI); |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 1452 | if (I == ThisFromOther.end()) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1453 | NewVNInfo.push_back(VNI); |
| 1454 | return ThisValNoAssignments[VN] = NewVNInfo.size()-1; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1455 | } |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 1456 | VNInfo *OtherValNo = I->second; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1457 | |
| 1458 | // Otherwise, this *is* a copy from the RHS. If the other side has already |
| 1459 | // been computed, return it. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1460 | if (OtherValNoAssignments[OtherValNo->id] >= 0) |
| 1461 | return ThisValNoAssignments[VN] = OtherValNoAssignments[OtherValNo->id]; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1462 | |
| 1463 | // Mark this value number as currently being computed, then ask what the |
| 1464 | // ultimate value # of the other value is. |
| 1465 | ThisValNoAssignments[VN] = -2; |
| 1466 | unsigned UltimateVN = |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1467 | ComputeUltimateVN(OtherValNo, NewVNInfo, OtherFromThis, ThisFromOther, |
| 1468 | OtherValNoAssignments, ThisValNoAssignments); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1469 | return ThisValNoAssignments[VN] = UltimateVN; |
| 1470 | } |
| 1471 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1472 | static bool InVector(VNInfo *Val, const SmallVector<VNInfo*, 8> &V) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1473 | return std::find(V.begin(), V.end(), Val) != V.end(); |
| 1474 | } |
| 1475 | |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1476 | /// RangeIsDefinedByCopyFromReg - Return true if the specified live range of |
| 1477 | /// the specified live interval is defined by a copy from the specified |
| 1478 | /// register. |
| 1479 | bool SimpleRegisterCoalescing::RangeIsDefinedByCopyFromReg(LiveInterval &li, |
| 1480 | LiveRange *LR, |
| 1481 | unsigned Reg) { |
| 1482 | unsigned SrcReg = li_->getVNInfoSourceReg(LR->valno); |
| 1483 | if (SrcReg == Reg) |
| 1484 | return true; |
| 1485 | if (LR->valno->def == ~0U && |
| 1486 | TargetRegisterInfo::isPhysicalRegister(li.reg) && |
| 1487 | *tri_->getSuperRegisters(li.reg)) { |
| 1488 | // It's a sub-register live interval, we may not have precise information. |
| 1489 | // Re-compute it. |
| 1490 | MachineInstr *DefMI = li_->getInstructionFromIndex(LR->start); |
| 1491 | unsigned SrcReg, DstReg; |
Evan Cheng | 76a4d58 | 2008-07-17 19:48:53 +0000 | [diff] [blame] | 1492 | if (DefMI && tii_->isMoveInstr(*DefMI, SrcReg, DstReg) && |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1493 | DstReg == li.reg && SrcReg == Reg) { |
| 1494 | // Cache computed info. |
| 1495 | LR->valno->def = LR->start; |
| 1496 | LR->valno->copy = DefMI; |
| 1497 | return true; |
| 1498 | } |
| 1499 | } |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1503 | /// SimpleJoin - Attempt to joint the specified interval into this one. The |
| 1504 | /// caller of this method must guarantee that the RHS only contains a single |
| 1505 | /// value number and that the RHS is not defined by a copy from this |
| 1506 | /// interval. This returns false if the intervals are not joinable, or it |
| 1507 | /// joins them and returns true. |
Bill Wendling | 2674d71 | 2008-01-04 08:59:18 +0000 | [diff] [blame] | 1508 | bool SimpleRegisterCoalescing::SimpleJoin(LiveInterval &LHS, LiveInterval &RHS){ |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1509 | assert(RHS.containsOneValue()); |
| 1510 | |
| 1511 | // Some number (potentially more than one) value numbers in the current |
| 1512 | // interval may be defined as copies from the RHS. Scan the overlapping |
| 1513 | // portions of the LHS and RHS, keeping track of this and looking for |
| 1514 | // overlapping live ranges that are NOT defined as copies. If these exist, we |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1515 | // cannot coalesce. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1516 | |
| 1517 | LiveInterval::iterator LHSIt = LHS.begin(), LHSEnd = LHS.end(); |
| 1518 | LiveInterval::iterator RHSIt = RHS.begin(), RHSEnd = RHS.end(); |
| 1519 | |
| 1520 | if (LHSIt->start < RHSIt->start) { |
| 1521 | LHSIt = std::upper_bound(LHSIt, LHSEnd, RHSIt->start); |
| 1522 | if (LHSIt != LHS.begin()) --LHSIt; |
| 1523 | } else if (RHSIt->start < LHSIt->start) { |
| 1524 | RHSIt = std::upper_bound(RHSIt, RHSEnd, LHSIt->start); |
| 1525 | if (RHSIt != RHS.begin()) --RHSIt; |
| 1526 | } |
| 1527 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1528 | SmallVector<VNInfo*, 8> EliminatedLHSVals; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1529 | |
| 1530 | while (1) { |
| 1531 | // Determine if these live intervals overlap. |
| 1532 | bool Overlaps = false; |
| 1533 | if (LHSIt->start <= RHSIt->start) |
| 1534 | Overlaps = LHSIt->end > RHSIt->start; |
| 1535 | else |
| 1536 | Overlaps = RHSIt->end > LHSIt->start; |
| 1537 | |
| 1538 | // If the live intervals overlap, there are two interesting cases: if the |
| 1539 | // LHS interval is defined by a copy from the RHS, it's ok and we record |
| 1540 | // that the LHS value # is the same as the RHS. If it's not, then we cannot |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1541 | // coalesce these live ranges and we bail out. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1542 | if (Overlaps) { |
| 1543 | // If we haven't already recorded that this value # is safe, check it. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1544 | if (!InVector(LHSIt->valno, EliminatedLHSVals)) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1545 | // Copy from the RHS? |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1546 | if (!RangeIsDefinedByCopyFromReg(LHS, LHSIt, RHS.reg)) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1547 | return false; // Nope, bail out. |
Evan Cheng | f4ea510 | 2008-05-21 22:34:12 +0000 | [diff] [blame] | 1548 | |
| 1549 | if (LHSIt->contains(RHSIt->valno->def)) |
| 1550 | // Here is an interesting situation: |
| 1551 | // BB1: |
| 1552 | // vr1025 = copy vr1024 |
| 1553 | // .. |
| 1554 | // BB2: |
| 1555 | // vr1024 = op |
| 1556 | // = vr1025 |
| 1557 | // Even though vr1025 is copied from vr1024, it's not safe to |
| 1558 | // coalesced them since live range of vr1025 intersects the |
| 1559 | // def of vr1024. This happens because vr1025 is assigned the |
| 1560 | // value of the previous iteration of vr1024. |
| 1561 | return false; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1562 | EliminatedLHSVals.push_back(LHSIt->valno); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | // We know this entire LHS live range is okay, so skip it now. |
| 1566 | if (++LHSIt == LHSEnd) break; |
| 1567 | continue; |
| 1568 | } |
| 1569 | |
| 1570 | if (LHSIt->end < RHSIt->end) { |
| 1571 | if (++LHSIt == LHSEnd) break; |
| 1572 | } else { |
| 1573 | // One interesting case to check here. It's possible that we have |
| 1574 | // something like "X3 = Y" which defines a new value number in the LHS, |
| 1575 | // and is the last use of this liverange of the RHS. In this case, we |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1576 | // want to notice this copy (so that it gets coalesced away) even though |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1577 | // the live ranges don't actually overlap. |
| 1578 | if (LHSIt->start == RHSIt->end) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1579 | if (InVector(LHSIt->valno, EliminatedLHSVals)) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1580 | // We already know that this value number is going to be merged in |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1581 | // if coalescing succeeds. Just skip the liverange. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1582 | if (++LHSIt == LHSEnd) break; |
| 1583 | } else { |
| 1584 | // Otherwise, if this is a copy from the RHS, mark it as being merged |
| 1585 | // in. |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1586 | if (RangeIsDefinedByCopyFromReg(LHS, LHSIt, RHS.reg)) { |
Evan Cheng | f4ea510 | 2008-05-21 22:34:12 +0000 | [diff] [blame] | 1587 | if (LHSIt->contains(RHSIt->valno->def)) |
| 1588 | // Here is an interesting situation: |
| 1589 | // BB1: |
| 1590 | // vr1025 = copy vr1024 |
| 1591 | // .. |
| 1592 | // BB2: |
| 1593 | // vr1024 = op |
| 1594 | // = vr1025 |
| 1595 | // Even though vr1025 is copied from vr1024, it's not safe to |
| 1596 | // coalesced them since live range of vr1025 intersects the |
| 1597 | // def of vr1024. This happens because vr1025 is assigned the |
| 1598 | // value of the previous iteration of vr1024. |
| 1599 | return false; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1600 | EliminatedLHSVals.push_back(LHSIt->valno); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1601 | |
| 1602 | // We know this entire LHS live range is okay, so skip it now. |
| 1603 | if (++LHSIt == LHSEnd) break; |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | if (++RHSIt == RHSEnd) break; |
| 1609 | } |
| 1610 | } |
| 1611 | |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1612 | // If we got here, we know that the coalescing will be successful and that |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1613 | // the value numbers in EliminatedLHSVals will all be merged together. Since |
| 1614 | // the most common case is that EliminatedLHSVals has a single number, we |
| 1615 | // optimize for it: if there is more than one value, we merge them all into |
| 1616 | // the lowest numbered one, then handle the interval as if we were merging |
| 1617 | // with one value number. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1618 | VNInfo *LHSValNo; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1619 | if (EliminatedLHSVals.size() > 1) { |
| 1620 | // Loop through all the equal value numbers merging them into the smallest |
| 1621 | // one. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1622 | VNInfo *Smallest = EliminatedLHSVals[0]; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1623 | for (unsigned i = 1, e = EliminatedLHSVals.size(); i != e; ++i) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1624 | if (EliminatedLHSVals[i]->id < Smallest->id) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1625 | // Merge the current notion of the smallest into the smaller one. |
| 1626 | LHS.MergeValueNumberInto(Smallest, EliminatedLHSVals[i]); |
| 1627 | Smallest = EliminatedLHSVals[i]; |
| 1628 | } else { |
| 1629 | // Merge into the smallest. |
| 1630 | LHS.MergeValueNumberInto(EliminatedLHSVals[i], Smallest); |
| 1631 | } |
| 1632 | } |
| 1633 | LHSValNo = Smallest; |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1634 | } else if (EliminatedLHSVals.empty()) { |
| 1635 | if (TargetRegisterInfo::isPhysicalRegister(LHS.reg) && |
| 1636 | *tri_->getSuperRegisters(LHS.reg)) |
| 1637 | // Imprecise sub-register information. Can't handle it. |
| 1638 | return false; |
| 1639 | assert(0 && "No copies from the RHS?"); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1640 | } else { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1641 | LHSValNo = EliminatedLHSVals[0]; |
| 1642 | } |
| 1643 | |
| 1644 | // Okay, now that there is a single LHS value number that we're merging the |
| 1645 | // RHS into, update the value number info for the LHS to indicate that the |
| 1646 | // value number is defined where the RHS value number was. |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 1647 | const VNInfo *VNI = RHS.getValNumInfo(0); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1648 | LHSValNo->def = VNI->def; |
| 1649 | LHSValNo->copy = VNI->copy; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1650 | |
| 1651 | // Okay, the final step is to loop over the RHS live intervals, adding them to |
| 1652 | // the LHS. |
Evan Cheng | c3fc7d9 | 2007-11-29 09:49:23 +0000 | [diff] [blame] | 1653 | LHSValNo->hasPHIKill |= VNI->hasPHIKill; |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 1654 | LHS.addKills(LHSValNo, VNI->kills); |
Evan Cheng | 430a7b0 | 2007-08-14 01:56:58 +0000 | [diff] [blame] | 1655 | LHS.MergeRangesInAsValue(RHS, LHSValNo); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1656 | LHS.weight += RHS.weight; |
| 1657 | if (RHS.preference && !LHS.preference) |
| 1658 | LHS.preference = RHS.preference; |
| 1659 | |
| 1660 | return true; |
| 1661 | } |
| 1662 | |
| 1663 | /// JoinIntervals - Attempt to join these two intervals. On failure, this |
| 1664 | /// returns false. Otherwise, if one of the intervals being joined is a |
| 1665 | /// physreg, this method always canonicalizes LHS to be it. The output |
| 1666 | /// "RHS" will not have been modified, so we can use this information |
| 1667 | /// below to update aliases. |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 1668 | bool SimpleRegisterCoalescing::JoinIntervals(LiveInterval &LHS, |
| 1669 | LiveInterval &RHS, bool &Swapped) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1670 | // Compute the final value assignment, assuming that the live ranges can be |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1671 | // coalesced. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1672 | SmallVector<int, 16> LHSValNoAssignments; |
| 1673 | SmallVector<int, 16> RHSValNoAssignments; |
Evan Cheng | fadfb5b | 2007-08-31 21:23:06 +0000 | [diff] [blame] | 1674 | DenseMap<VNInfo*, VNInfo*> LHSValsDefinedFromRHS; |
| 1675 | DenseMap<VNInfo*, VNInfo*> RHSValsDefinedFromLHS; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1676 | SmallVector<VNInfo*, 16> NewVNInfo; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1677 | |
| 1678 | // If a live interval is a physical register, conservatively check if any |
| 1679 | // of its sub-registers is overlapping the live interval of the virtual |
| 1680 | // register. If so, do not coalesce. |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1681 | if (TargetRegisterInfo::isPhysicalRegister(LHS.reg) && |
| 1682 | *tri_->getSubRegisters(LHS.reg)) { |
| 1683 | for (const unsigned* SR = tri_->getSubRegisters(LHS.reg); *SR; ++SR) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1684 | if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) { |
| 1685 | DOUT << "Interfere with sub-register "; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1686 | DEBUG(li_->getInterval(*SR).print(DOUT, tri_)); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1687 | return false; |
| 1688 | } |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1689 | } else if (TargetRegisterInfo::isPhysicalRegister(RHS.reg) && |
| 1690 | *tri_->getSubRegisters(RHS.reg)) { |
| 1691 | for (const unsigned* SR = tri_->getSubRegisters(RHS.reg); *SR; ++SR) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1692 | if (li_->hasInterval(*SR) && LHS.overlaps(li_->getInterval(*SR))) { |
| 1693 | DOUT << "Interfere with sub-register "; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1694 | DEBUG(li_->getInterval(*SR).print(DOUT, tri_)); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1695 | return false; |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | // Compute ultimate value numbers for the LHS and RHS values. |
| 1700 | if (RHS.containsOneValue()) { |
| 1701 | // Copies from a liveinterval with a single value are simple to handle and |
| 1702 | // very common, handle the special case here. This is important, because |
| 1703 | // often RHS is small and LHS is large (e.g. a physreg). |
| 1704 | |
| 1705 | // Find out if the RHS is defined as a copy from some value in the LHS. |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 1706 | int RHSVal0DefinedFromLHS = -1; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1707 | int RHSValID = -1; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1708 | VNInfo *RHSValNoInfo = NULL; |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 1709 | VNInfo *RHSValNoInfo0 = RHS.getValNumInfo(0); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1710 | unsigned RHSSrcReg = li_->getVNInfoSourceReg(RHSValNoInfo0); |
| 1711 | if ((RHSSrcReg == 0 || RHSSrcReg != LHS.reg)) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1712 | // If RHS is not defined as a copy from the LHS, we can use simpler and |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1713 | // faster checks to see if the live ranges are coalescable. This joiner |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1714 | // can't swap the LHS/RHS intervals though. |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1715 | if (!TargetRegisterInfo::isPhysicalRegister(RHS.reg)) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1716 | return SimpleJoin(LHS, RHS); |
| 1717 | } else { |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 1718 | RHSValNoInfo = RHSValNoInfo0; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1719 | } |
| 1720 | } else { |
| 1721 | // It was defined as a copy from the LHS, find out what value # it is. |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 1722 | RHSValNoInfo = LHS.getLiveRangeContaining(RHSValNoInfo0->def-1)->valno; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1723 | RHSValID = RHSValNoInfo->id; |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 1724 | RHSVal0DefinedFromLHS = RHSValID; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | LHSValNoAssignments.resize(LHS.getNumValNums(), -1); |
| 1728 | RHSValNoAssignments.resize(RHS.getNumValNums(), -1); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1729 | NewVNInfo.resize(LHS.getNumValNums(), NULL); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1730 | |
| 1731 | // Okay, *all* of the values in LHS that are defined as a copy from RHS |
| 1732 | // should now get updated. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1733 | for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end(); |
| 1734 | i != e; ++i) { |
| 1735 | VNInfo *VNI = *i; |
| 1736 | unsigned VN = VNI->id; |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1737 | if (unsigned LHSSrcReg = li_->getVNInfoSourceReg(VNI)) { |
| 1738 | if (LHSSrcReg != RHS.reg) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1739 | // If this is not a copy from the RHS, its value number will be |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1740 | // unmodified by the coalescing. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1741 | NewVNInfo[VN] = VNI; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1742 | LHSValNoAssignments[VN] = VN; |
| 1743 | } else if (RHSValID == -1) { |
| 1744 | // Otherwise, it is a copy from the RHS, and we don't already have a |
| 1745 | // value# for it. Keep the current value number, but remember it. |
| 1746 | LHSValNoAssignments[VN] = RHSValID = VN; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1747 | NewVNInfo[VN] = RHSValNoInfo; |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 1748 | LHSValsDefinedFromRHS[VNI] = RHSValNoInfo0; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1749 | } else { |
| 1750 | // Otherwise, use the specified value #. |
| 1751 | LHSValNoAssignments[VN] = RHSValID; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1752 | if (VN == (unsigned)RHSValID) { // Else this val# is dead. |
| 1753 | NewVNInfo[VN] = RHSValNoInfo; |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 1754 | LHSValsDefinedFromRHS[VNI] = RHSValNoInfo0; |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 1755 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1756 | } |
| 1757 | } else { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1758 | NewVNInfo[VN] = VNI; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1759 | LHSValNoAssignments[VN] = VN; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | assert(RHSValID != -1 && "Didn't find value #?"); |
| 1764 | RHSValNoAssignments[0] = RHSValID; |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 1765 | if (RHSVal0DefinedFromLHS != -1) { |
Evan Cheng | 3430135 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 1766 | // This path doesn't go through ComputeUltimateVN so just set |
| 1767 | // it to anything. |
| 1768 | RHSValsDefinedFromLHS[RHSValNoInfo0] = (VNInfo*)1; |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 1769 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1770 | } else { |
| 1771 | // Loop over the value numbers of the LHS, seeing if any are defined from |
| 1772 | // the RHS. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1773 | for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end(); |
| 1774 | i != e; ++i) { |
| 1775 | VNInfo *VNI = *i; |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1776 | if (VNI->def == ~1U || VNI->copy == 0) // Src not defined by a copy? |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1777 | continue; |
| 1778 | |
| 1779 | // DstReg is known to be a register in the LHS interval. If the src is |
| 1780 | // from the RHS interval, we can use its value #. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1781 | if (li_->getVNInfoSourceReg(VNI) != RHS.reg) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1782 | continue; |
| 1783 | |
| 1784 | // Figure out the value # from the RHS. |
Bill Wendling | 2674d71 | 2008-01-04 08:59:18 +0000 | [diff] [blame] | 1785 | LHSValsDefinedFromRHS[VNI]=RHS.getLiveRangeContaining(VNI->def-1)->valno; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | // Loop over the value numbers of the RHS, seeing if any are defined from |
| 1789 | // the LHS. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1790 | for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end(); |
| 1791 | i != e; ++i) { |
| 1792 | VNInfo *VNI = *i; |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1793 | if (VNI->def == ~1U || VNI->copy == 0) // Src not defined by a copy? |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1794 | continue; |
| 1795 | |
| 1796 | // DstReg is known to be a register in the RHS interval. If the src is |
| 1797 | // from the LHS interval, we can use its value #. |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1798 | if (li_->getVNInfoSourceReg(VNI) != LHS.reg) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1799 | continue; |
| 1800 | |
| 1801 | // Figure out the value # from the LHS. |
Bill Wendling | 2674d71 | 2008-01-04 08:59:18 +0000 | [diff] [blame] | 1802 | RHSValsDefinedFromLHS[VNI]=LHS.getLiveRangeContaining(VNI->def-1)->valno; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | LHSValNoAssignments.resize(LHS.getNumValNums(), -1); |
| 1806 | RHSValNoAssignments.resize(RHS.getNumValNums(), -1); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1807 | NewVNInfo.reserve(LHS.getNumValNums() + RHS.getNumValNums()); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1808 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1809 | for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end(); |
| 1810 | i != e; ++i) { |
| 1811 | VNInfo *VNI = *i; |
| 1812 | unsigned VN = VNI->id; |
| 1813 | if (LHSValNoAssignments[VN] >= 0 || VNI->def == ~1U) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1814 | continue; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1815 | ComputeUltimateVN(VNI, NewVNInfo, |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1816 | LHSValsDefinedFromRHS, RHSValsDefinedFromLHS, |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1817 | LHSValNoAssignments, RHSValNoAssignments); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1818 | } |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1819 | for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end(); |
| 1820 | i != e; ++i) { |
| 1821 | VNInfo *VNI = *i; |
| 1822 | unsigned VN = VNI->id; |
| 1823 | if (RHSValNoAssignments[VN] >= 0 || VNI->def == ~1U) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1824 | continue; |
| 1825 | // If this value number isn't a copy from the LHS, it's a new number. |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 1826 | if (RHSValsDefinedFromLHS.find(VNI) == RHSValsDefinedFromLHS.end()) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1827 | NewVNInfo.push_back(VNI); |
| 1828 | RHSValNoAssignments[VN] = NewVNInfo.size()-1; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1829 | continue; |
| 1830 | } |
| 1831 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1832 | ComputeUltimateVN(VNI, NewVNInfo, |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1833 | RHSValsDefinedFromLHS, LHSValsDefinedFromRHS, |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1834 | RHSValNoAssignments, LHSValNoAssignments); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | // Armed with the mappings of LHS/RHS values to ultimate values, walk the |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1839 | // interval lists to see if these intervals are coalescable. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1840 | LiveInterval::const_iterator I = LHS.begin(); |
| 1841 | LiveInterval::const_iterator IE = LHS.end(); |
| 1842 | LiveInterval::const_iterator J = RHS.begin(); |
| 1843 | LiveInterval::const_iterator JE = RHS.end(); |
| 1844 | |
| 1845 | // Skip ahead until the first place of potential sharing. |
| 1846 | if (I->start < J->start) { |
| 1847 | I = std::upper_bound(I, IE, J->start); |
| 1848 | if (I != LHS.begin()) --I; |
| 1849 | } else if (J->start < I->start) { |
| 1850 | J = std::upper_bound(J, JE, I->start); |
| 1851 | if (J != RHS.begin()) --J; |
| 1852 | } |
| 1853 | |
| 1854 | while (1) { |
| 1855 | // Determine if these two live ranges overlap. |
| 1856 | bool Overlaps; |
| 1857 | if (I->start < J->start) { |
| 1858 | Overlaps = I->end > J->start; |
| 1859 | } else { |
| 1860 | Overlaps = J->end > I->start; |
| 1861 | } |
| 1862 | |
| 1863 | // If so, check value # info to determine if they are really different. |
| 1864 | if (Overlaps) { |
| 1865 | // If the live range overlap will map to the same value number in the |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1866 | // result liverange, we can still coalesce them. If not, we can't. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1867 | if (LHSValNoAssignments[I->valno->id] != |
| 1868 | RHSValNoAssignments[J->valno->id]) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1869 | return false; |
| 1870 | } |
| 1871 | |
| 1872 | if (I->end < J->end) { |
| 1873 | ++I; |
| 1874 | if (I == IE) break; |
| 1875 | } else { |
| 1876 | ++J; |
| 1877 | if (J == JE) break; |
| 1878 | } |
| 1879 | } |
| 1880 | |
Evan Cheng | 3472925 | 2007-10-14 10:08:34 +0000 | [diff] [blame] | 1881 | // Update kill info. Some live ranges are extended due to copy coalescing. |
| 1882 | for (DenseMap<VNInfo*, VNInfo*>::iterator I = LHSValsDefinedFromRHS.begin(), |
| 1883 | E = LHSValsDefinedFromRHS.end(); I != E; ++I) { |
| 1884 | VNInfo *VNI = I->first; |
| 1885 | unsigned LHSValID = LHSValNoAssignments[VNI->id]; |
| 1886 | LiveInterval::removeKill(NewVNInfo[LHSValID], VNI->def); |
Evan Cheng | c3fc7d9 | 2007-11-29 09:49:23 +0000 | [diff] [blame] | 1887 | NewVNInfo[LHSValID]->hasPHIKill |= VNI->hasPHIKill; |
Evan Cheng | 3472925 | 2007-10-14 10:08:34 +0000 | [diff] [blame] | 1888 | RHS.addKills(NewVNInfo[LHSValID], VNI->kills); |
| 1889 | } |
| 1890 | |
| 1891 | // Update kill info. Some live ranges are extended due to copy coalescing. |
| 1892 | for (DenseMap<VNInfo*, VNInfo*>::iterator I = RHSValsDefinedFromLHS.begin(), |
| 1893 | E = RHSValsDefinedFromLHS.end(); I != E; ++I) { |
| 1894 | VNInfo *VNI = I->first; |
| 1895 | unsigned RHSValID = RHSValNoAssignments[VNI->id]; |
| 1896 | LiveInterval::removeKill(NewVNInfo[RHSValID], VNI->def); |
Evan Cheng | c3fc7d9 | 2007-11-29 09:49:23 +0000 | [diff] [blame] | 1897 | NewVNInfo[RHSValID]->hasPHIKill |= VNI->hasPHIKill; |
Evan Cheng | 3472925 | 2007-10-14 10:08:34 +0000 | [diff] [blame] | 1898 | LHS.addKills(NewVNInfo[RHSValID], VNI->kills); |
| 1899 | } |
| 1900 | |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1901 | // If we get here, we know that we can coalesce the live ranges. Ask the |
| 1902 | // intervals to coalesce themselves now. |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 1903 | if ((RHS.ranges.size() > LHS.ranges.size() && |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1904 | TargetRegisterInfo::isVirtualRegister(LHS.reg)) || |
| 1905 | TargetRegisterInfo::isPhysicalRegister(RHS.reg)) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1906 | RHS.join(LHS, &RHSValNoAssignments[0], &LHSValNoAssignments[0], NewVNInfo); |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 1907 | Swapped = true; |
| 1908 | } else { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 1909 | LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0], NewVNInfo); |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 1910 | Swapped = false; |
| 1911 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1912 | return true; |
| 1913 | } |
| 1914 | |
| 1915 | namespace { |
| 1916 | // DepthMBBCompare - Comparison predicate that sort first based on the loop |
| 1917 | // depth of the basic block (the unsigned), and then on the MBB number. |
| 1918 | struct DepthMBBCompare { |
| 1919 | typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair; |
| 1920 | bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const { |
| 1921 | if (LHS.first > RHS.first) return true; // Deeper loops first |
| 1922 | return LHS.first == RHS.first && |
| 1923 | LHS.second->getNumber() < RHS.second->getNumber(); |
| 1924 | } |
| 1925 | }; |
| 1926 | } |
| 1927 | |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1928 | /// getRepIntervalSize - Returns the size of the interval that represents the |
| 1929 | /// specified register. |
| 1930 | template<class SF> |
| 1931 | unsigned JoinPriorityQueue<SF>::getRepIntervalSize(unsigned Reg) { |
| 1932 | return Rc->getRepIntervalSize(Reg); |
| 1933 | } |
| 1934 | |
| 1935 | /// CopyRecSort::operator - Join priority queue sorting function. |
| 1936 | /// |
| 1937 | bool CopyRecSort::operator()(CopyRec left, CopyRec right) const { |
| 1938 | // Inner loops first. |
| 1939 | if (left.LoopDepth > right.LoopDepth) |
| 1940 | return false; |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1941 | else if (left.LoopDepth == right.LoopDepth) |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1942 | if (left.isBackEdge && !right.isBackEdge) |
| 1943 | return false; |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1944 | return true; |
| 1945 | } |
| 1946 | |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1947 | void SimpleRegisterCoalescing::CopyCoalesceInMBB(MachineBasicBlock *MBB, |
Evan Cheng | 8b0b874 | 2007-10-16 08:04:24 +0000 | [diff] [blame] | 1948 | std::vector<CopyRec> &TryAgain) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1949 | DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n"; |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1950 | |
Evan Cheng | 8b0b874 | 2007-10-16 08:04:24 +0000 | [diff] [blame] | 1951 | std::vector<CopyRec> VirtCopies; |
| 1952 | std::vector<CopyRec> PhysCopies; |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1953 | std::vector<CopyRec> ImpDefCopies; |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 1954 | unsigned LoopDepth = loopInfo->getLoopDepth(MBB); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1955 | for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end(); |
| 1956 | MII != E;) { |
| 1957 | MachineInstr *Inst = MII++; |
| 1958 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1959 | // If this isn't a copy nor a extract_subreg, we can't join intervals. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1960 | unsigned SrcReg, DstReg; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1961 | if (Inst->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) { |
| 1962 | DstReg = Inst->getOperand(0).getReg(); |
| 1963 | SrcReg = Inst->getOperand(1).getReg(); |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1964 | } else if (Inst->getOpcode() == TargetInstrInfo::INSERT_SUBREG) { |
| 1965 | DstReg = Inst->getOperand(0).getReg(); |
| 1966 | SrcReg = Inst->getOperand(2).getReg(); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1967 | } else if (!tii_->isMoveInstr(*Inst, SrcReg, DstReg)) |
| 1968 | continue; |
Evan Cheng | 8b0b874 | 2007-10-16 08:04:24 +0000 | [diff] [blame] | 1969 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1970 | bool SrcIsPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg); |
| 1971 | bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg); |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1972 | if (NewHeuristic) { |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1973 | JoinQueue->push(CopyRec(Inst, LoopDepth, isBackEdgeCopy(Inst, DstReg))); |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1974 | } else { |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1975 | if (li_->hasInterval(SrcReg) && li_->getInterval(SrcReg).empty()) |
| 1976 | ImpDefCopies.push_back(CopyRec(Inst, 0, false)); |
| 1977 | else if (SrcIsPhys || DstIsPhys) |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1978 | PhysCopies.push_back(CopyRec(Inst, 0, false)); |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1979 | else |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 1980 | VirtCopies.push_back(CopyRec(Inst, 0, false)); |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1981 | } |
Evan Cheng | 8b0b874 | 2007-10-16 08:04:24 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 1984 | if (NewHeuristic) |
| 1985 | return; |
| 1986 | |
Evan Cheng | 7e073ba | 2008-04-09 20:57:25 +0000 | [diff] [blame] | 1987 | // Try coalescing implicit copies first, followed by copies to / from |
| 1988 | // physical registers, then finally copies from virtual registers to |
| 1989 | // virtual registers. |
| 1990 | for (unsigned i = 0, e = ImpDefCopies.size(); i != e; ++i) { |
| 1991 | CopyRec &TheCopy = ImpDefCopies[i]; |
| 1992 | bool Again = false; |
| 1993 | if (!JoinCopy(TheCopy, Again)) |
| 1994 | if (Again) |
| 1995 | TryAgain.push_back(TheCopy); |
| 1996 | } |
Evan Cheng | 8b0b874 | 2007-10-16 08:04:24 +0000 | [diff] [blame] | 1997 | for (unsigned i = 0, e = PhysCopies.size(); i != e; ++i) { |
| 1998 | CopyRec &TheCopy = PhysCopies[i]; |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 1999 | bool Again = false; |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 2000 | if (!JoinCopy(TheCopy, Again)) |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 2001 | if (Again) |
| 2002 | TryAgain.push_back(TheCopy); |
Evan Cheng | 8b0b874 | 2007-10-16 08:04:24 +0000 | [diff] [blame] | 2003 | } |
| 2004 | for (unsigned i = 0, e = VirtCopies.size(); i != e; ++i) { |
| 2005 | CopyRec &TheCopy = VirtCopies[i]; |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 2006 | bool Again = false; |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 2007 | if (!JoinCopy(TheCopy, Again)) |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 2008 | if (Again) |
| 2009 | TryAgain.push_back(TheCopy); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | void SimpleRegisterCoalescing::joinIntervals() { |
| 2014 | DOUT << "********** JOINING INTERVALS ***********\n"; |
| 2015 | |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 2016 | if (NewHeuristic) |
| 2017 | JoinQueue = new JoinPriorityQueue<CopyRecSort>(this); |
| 2018 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2019 | std::vector<CopyRec> TryAgainList; |
Dan Gohman | a8c763b | 2008-08-14 18:13:49 +0000 | [diff] [blame] | 2020 | if (loopInfo->empty()) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2021 | // If there are no loops in the function, join intervals in function order. |
| 2022 | for (MachineFunction::iterator I = mf_->begin(), E = mf_->end(); |
| 2023 | I != E; ++I) |
Evan Cheng | 8b0b874 | 2007-10-16 08:04:24 +0000 | [diff] [blame] | 2024 | CopyCoalesceInMBB(I, TryAgainList); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2025 | } else { |
| 2026 | // Otherwise, join intervals in inner loops before other intervals. |
| 2027 | // Unfortunately we can't just iterate over loop hierarchy here because |
| 2028 | // there may be more MBB's than BB's. Collect MBB's for sorting. |
| 2029 | |
| 2030 | // Join intervals in the function prolog first. We want to join physical |
| 2031 | // registers with virtual registers before the intervals got too long. |
| 2032 | std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs; |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 2033 | for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();I != E;++I){ |
| 2034 | MachineBasicBlock *MBB = I; |
| 2035 | MBBs.push_back(std::make_pair(loopInfo->getLoopDepth(MBB), I)); |
| 2036 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2037 | |
| 2038 | // Sort by loop depth. |
| 2039 | std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare()); |
| 2040 | |
| 2041 | // Finally, join intervals in loop nest order. |
| 2042 | for (unsigned i = 0, e = MBBs.size(); i != e; ++i) |
Evan Cheng | 8b0b874 | 2007-10-16 08:04:24 +0000 | [diff] [blame] | 2043 | CopyCoalesceInMBB(MBBs[i].second, TryAgainList); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | // Joining intervals can allow other intervals to be joined. Iteratively join |
| 2047 | // until we make no progress. |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 2048 | if (NewHeuristic) { |
| 2049 | SmallVector<CopyRec, 16> TryAgain; |
| 2050 | bool ProgressMade = true; |
| 2051 | while (ProgressMade) { |
| 2052 | ProgressMade = false; |
| 2053 | while (!JoinQueue->empty()) { |
| 2054 | CopyRec R = JoinQueue->pop(); |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 2055 | bool Again = false; |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 2056 | bool Success = JoinCopy(R, Again); |
| 2057 | if (Success) |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 2058 | ProgressMade = true; |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 2059 | else if (Again) |
| 2060 | TryAgain.push_back(R); |
| 2061 | } |
| 2062 | |
| 2063 | if (ProgressMade) { |
| 2064 | while (!TryAgain.empty()) { |
| 2065 | JoinQueue->push(TryAgain.back()); |
| 2066 | TryAgain.pop_back(); |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | } else { |
| 2071 | bool ProgressMade = true; |
| 2072 | while (ProgressMade) { |
| 2073 | ProgressMade = false; |
| 2074 | |
| 2075 | for (unsigned i = 0, e = TryAgainList.size(); i != e; ++i) { |
| 2076 | CopyRec &TheCopy = TryAgainList[i]; |
| 2077 | if (TheCopy.MI) { |
| 2078 | bool Again = false; |
| 2079 | bool Success = JoinCopy(TheCopy, Again); |
| 2080 | if (Success || !Again) { |
| 2081 | TheCopy.MI = 0; // Mark this one as done. |
| 2082 | ProgressMade = true; |
| 2083 | } |
Evan Cheng | 0547bab | 2007-11-01 06:22:48 +0000 | [diff] [blame] | 2084 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 2089 | if (NewHeuristic) |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 2090 | delete JoinQueue; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | /// Return true if the two specified registers belong to different register |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 2094 | /// classes. The registers may be either phys or virt regs. In the |
| 2095 | /// case where both registers are virtual registers, it would also returns |
| 2096 | /// true by reference the RegB register class in SubRC if it is a subset of |
| 2097 | /// RegA's register class. |
| 2098 | bool |
| 2099 | SimpleRegisterCoalescing::differingRegisterClasses(unsigned RegA, unsigned RegB, |
| 2100 | const TargetRegisterClass *&SubRC) const { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2101 | |
| 2102 | // Get the register classes for the first reg. |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 2103 | if (TargetRegisterInfo::isPhysicalRegister(RegA)) { |
| 2104 | assert(TargetRegisterInfo::isVirtualRegister(RegB) && |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2105 | "Shouldn't consider two physregs!"); |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 2106 | return !mri_->getRegClass(RegB)->contains(RegA); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | // Compare against the regclass for the second reg. |
Evan Cheng | e00f5de | 2008-06-19 01:39:21 +0000 | [diff] [blame] | 2110 | const TargetRegisterClass *RegClassA = mri_->getRegClass(RegA); |
| 2111 | if (TargetRegisterInfo::isVirtualRegister(RegB)) { |
| 2112 | const TargetRegisterClass *RegClassB = mri_->getRegClass(RegB); |
| 2113 | if (RegClassA == RegClassB) |
| 2114 | return false; |
| 2115 | SubRC = (RegClassA->hasSubClass(RegClassB)) ? RegClassB : NULL; |
| 2116 | return true; |
| 2117 | } |
| 2118 | return !RegClassA->contains(RegB); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | /// lastRegisterUse - Returns the last use of the specific register between |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 2122 | /// cycles Start and End or NULL if there are no uses. |
| 2123 | MachineOperand * |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 2124 | SimpleRegisterCoalescing::lastRegisterUse(unsigned Start, unsigned End, |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 2125 | unsigned Reg, unsigned &UseIdx) const{ |
| 2126 | UseIdx = 0; |
| 2127 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 2128 | MachineOperand *LastUse = NULL; |
| 2129 | for (MachineRegisterInfo::use_iterator I = mri_->use_begin(Reg), |
| 2130 | E = mri_->use_end(); I != E; ++I) { |
| 2131 | MachineOperand &Use = I.getOperand(); |
| 2132 | MachineInstr *UseMI = Use.getParent(); |
Evan Cheng | a2fb634 | 2008-03-25 02:02:19 +0000 | [diff] [blame] | 2133 | unsigned SrcReg, DstReg; |
| 2134 | if (tii_->isMoveInstr(*UseMI, SrcReg, DstReg) && SrcReg == DstReg) |
| 2135 | // Ignore identity copies. |
| 2136 | continue; |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 2137 | unsigned Idx = li_->getInstructionIndex(UseMI); |
| 2138 | if (Idx >= Start && Idx < End && Idx >= UseIdx) { |
| 2139 | LastUse = &Use; |
| 2140 | UseIdx = Idx; |
| 2141 | } |
| 2142 | } |
| 2143 | return LastUse; |
| 2144 | } |
| 2145 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2146 | int e = (End-1) / InstrSlots::NUM * InstrSlots::NUM; |
| 2147 | int s = Start; |
| 2148 | while (e >= s) { |
| 2149 | // Skip deleted instructions |
| 2150 | MachineInstr *MI = li_->getInstructionFromIndex(e); |
| 2151 | while ((e - InstrSlots::NUM) >= s && !MI) { |
| 2152 | e -= InstrSlots::NUM; |
| 2153 | MI = li_->getInstructionFromIndex(e); |
| 2154 | } |
| 2155 | if (e < s || MI == NULL) |
| 2156 | return NULL; |
| 2157 | |
Evan Cheng | a2fb634 | 2008-03-25 02:02:19 +0000 | [diff] [blame] | 2158 | // Ignore identity copies. |
| 2159 | unsigned SrcReg, DstReg; |
| 2160 | if (!(tii_->isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg)) |
| 2161 | for (unsigned i = 0, NumOps = MI->getNumOperands(); i != NumOps; ++i) { |
| 2162 | MachineOperand &Use = MI->getOperand(i); |
| 2163 | if (Use.isRegister() && Use.isUse() && Use.getReg() && |
| 2164 | tri_->regsOverlap(Use.getReg(), Reg)) { |
| 2165 | UseIdx = e; |
| 2166 | return &Use; |
| 2167 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2168 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2169 | |
| 2170 | e -= InstrSlots::NUM; |
| 2171 | } |
| 2172 | |
| 2173 | return NULL; |
| 2174 | } |
| 2175 | |
| 2176 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2177 | void SimpleRegisterCoalescing::printRegName(unsigned reg) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 2178 | if (TargetRegisterInfo::isPhysicalRegister(reg)) |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 2179 | cerr << tri_->getName(reg); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2180 | else |
| 2181 | cerr << "%reg" << reg; |
| 2182 | } |
| 2183 | |
| 2184 | void SimpleRegisterCoalescing::releaseMemory() { |
Evan Cheng | 8fc9a10 | 2007-11-06 08:52:21 +0000 | [diff] [blame] | 2185 | JoinedCopies.clear(); |
Evan Cheng | cd04708 | 2008-08-30 09:09:33 +0000 | [diff] [blame] | 2186 | ReMatCopies.clear(); |
Evan Cheng | 20580a1 | 2008-09-19 17:38:47 +0000 | [diff] [blame^] | 2187 | ReMatDefs.clear(); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | static bool isZeroLengthInterval(LiveInterval *li) { |
| 2191 | for (LiveInterval::Ranges::const_iterator |
| 2192 | i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i) |
| 2193 | if (i->end - i->start > LiveIntervals::InstrSlots::NUM) |
| 2194 | return false; |
| 2195 | return true; |
| 2196 | } |
| 2197 | |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 2198 | /// TurnCopyIntoImpDef - If source of the specified copy is an implicit def, |
| 2199 | /// turn the copy into an implicit def. |
| 2200 | bool |
| 2201 | SimpleRegisterCoalescing::TurnCopyIntoImpDef(MachineBasicBlock::iterator &I, |
| 2202 | MachineBasicBlock *MBB, |
| 2203 | unsigned DstReg, unsigned SrcReg) { |
| 2204 | MachineInstr *CopyMI = &*I; |
| 2205 | unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI)); |
| 2206 | if (!li_->hasInterval(SrcReg)) |
| 2207 | return false; |
| 2208 | LiveInterval &SrcInt = li_->getInterval(SrcReg); |
| 2209 | if (!SrcInt.empty()) |
| 2210 | return false; |
Evan Cheng | f20d943 | 2008-04-09 01:30:15 +0000 | [diff] [blame] | 2211 | if (!li_->hasInterval(DstReg)) |
| 2212 | return false; |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 2213 | LiveInterval &DstInt = li_->getInterval(DstReg); |
Evan Cheng | ff7a3e5 | 2008-04-16 18:48:43 +0000 | [diff] [blame] | 2214 | const LiveRange *DstLR = DstInt.getLiveRangeContaining(CopyIdx); |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 2215 | DstInt.removeValNo(DstLR->valno); |
| 2216 | CopyMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF)); |
| 2217 | for (int i = CopyMI->getNumOperands() - 1, e = 0; i > e; --i) |
| 2218 | CopyMI->RemoveOperand(i); |
Dan Gohman | a8c763b | 2008-08-14 18:13:49 +0000 | [diff] [blame] | 2219 | bool NoUse = mri_->use_empty(SrcReg); |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 2220 | if (NoUse) { |
| 2221 | for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(SrcReg), |
| 2222 | E = mri_->reg_end(); I != E; ) { |
| 2223 | assert(I.getOperand().isDef()); |
| 2224 | MachineInstr *DefMI = &*I; |
| 2225 | ++I; |
| 2226 | // The implicit_def source has no other uses, delete it. |
| 2227 | assert(DefMI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF); |
| 2228 | li_->RemoveMachineInstrFromMaps(DefMI); |
| 2229 | DefMI->eraseFromParent(); |
Evan Cheng | db9b1c3 | 2008-04-03 16:41:54 +0000 | [diff] [blame] | 2230 | } |
| 2231 | } |
| 2232 | ++I; |
| 2233 | return true; |
| 2234 | } |
| 2235 | |
| 2236 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2237 | bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) { |
| 2238 | mf_ = &fn; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 2239 | mri_ = &fn.getRegInfo(); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2240 | tm_ = &fn.getTarget(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 2241 | tri_ = tm_->getRegisterInfo(); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2242 | tii_ = tm_->getInstrInfo(); |
| 2243 | li_ = &getAnalysis<LiveIntervals>(); |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 2244 | loopInfo = &getAnalysis<MachineLoopInfo>(); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2245 | |
| 2246 | DOUT << "********** SIMPLE REGISTER COALESCING **********\n" |
| 2247 | << "********** Function: " |
| 2248 | << ((Value*)mf_->getFunction())->getName() << '\n'; |
| 2249 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 2250 | allocatableRegs_ = tri_->getAllocatableSet(fn); |
| 2251 | for (TargetRegisterInfo::regclass_iterator I = tri_->regclass_begin(), |
| 2252 | E = tri_->regclass_end(); I != E; ++I) |
Bill Wendling | 2674d71 | 2008-01-04 08:59:18 +0000 | [diff] [blame] | 2253 | allocatableRCRegs_.insert(std::make_pair(*I, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 2254 | tri_->getAllocatableSet(fn, *I))); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2255 | |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 2256 | // Join (coalesce) intervals if requested. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2257 | if (EnableJoining) { |
| 2258 | joinIntervals(); |
| 2259 | DOUT << "********** INTERVALS POST JOINING **********\n"; |
Bill Wendling | 2674d71 | 2008-01-04 08:59:18 +0000 | [diff] [blame] | 2260 | for (LiveIntervals::iterator I = li_->begin(), E = li_->end(); I != E; ++I){ |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 2261 | I->second->print(DOUT, tri_); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2262 | DOUT << "\n"; |
| 2263 | } |
| 2264 | } |
| 2265 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 2266 | // Perform a final pass over the instructions and compute spill weights |
| 2267 | // and remove identity moves. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2268 | for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end(); |
| 2269 | mbbi != mbbe; ++mbbi) { |
| 2270 | MachineBasicBlock* mbb = mbbi; |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 2271 | unsigned loopDepth = loopInfo->getLoopDepth(mbb); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2272 | |
| 2273 | for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end(); |
| 2274 | mii != mie; ) { |
Evan Cheng | a971dbd | 2008-04-24 09:06:33 +0000 | [diff] [blame] | 2275 | MachineInstr *MI = mii; |
| 2276 | unsigned SrcReg, DstReg; |
| 2277 | if (JoinedCopies.count(MI)) { |
| 2278 | // Delete all coalesced copies. |
| 2279 | if (!tii_->isMoveInstr(*MI, SrcReg, DstReg)) { |
| 2280 | assert((MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG || |
| 2281 | MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) && |
| 2282 | "Unrecognized copy instruction"); |
| 2283 | DstReg = MI->getOperand(0).getReg(); |
| 2284 | } |
| 2285 | if (MI->registerDefIsDead(DstReg)) { |
| 2286 | LiveInterval &li = li_->getInterval(DstReg); |
| 2287 | if (!ShortenDeadCopySrcLiveRange(li, MI)) |
| 2288 | ShortenDeadCopyLiveRange(li, MI); |
| 2289 | } |
| 2290 | li_->RemoveMachineInstrFromMaps(MI); |
| 2291 | mii = mbbi->erase(mii); |
| 2292 | ++numPeep; |
| 2293 | continue; |
| 2294 | } |
| 2295 | |
Evan Cheng | 20580a1 | 2008-09-19 17:38:47 +0000 | [diff] [blame^] | 2296 | // Now check if this is a remat'ed def instruction which is now dead. |
| 2297 | if (ReMatDefs.count(MI)) { |
| 2298 | bool isDead = true; |
| 2299 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 2300 | const MachineOperand &MO = MI->getOperand(i); |
| 2301 | if (!MO.isRegister() || MO.isDead()) |
| 2302 | continue; |
| 2303 | unsigned Reg = MO.getReg(); |
| 2304 | if (TargetRegisterInfo::isPhysicalRegister(Reg) || |
| 2305 | !mri_->use_empty(Reg)) { |
| 2306 | isDead = false; |
| 2307 | break; |
| 2308 | } |
| 2309 | } |
| 2310 | if (isDead) { |
| 2311 | li_->RemoveMachineInstrFromMaps(mii); |
| 2312 | mii = mbbi->erase(mii); |
| 2313 | } |
| 2314 | } |
| 2315 | |
Evan Cheng | a971dbd | 2008-04-24 09:06:33 +0000 | [diff] [blame] | 2316 | // If the move will be an identity move delete it |
Evan Cheng | 20580a1 | 2008-09-19 17:38:47 +0000 | [diff] [blame^] | 2317 | bool isMove = tii_->isMoveInstr(*MI, SrcReg, DstReg); |
Evan Cheng | a971dbd | 2008-04-24 09:06:33 +0000 | [diff] [blame] | 2318 | if (isMove && SrcReg == DstReg) { |
| 2319 | if (li_->hasInterval(SrcReg)) { |
| 2320 | LiveInterval &RegInt = li_->getInterval(SrcReg); |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 2321 | // If def of this move instruction is dead, remove its live range |
| 2322 | // from the dstination register's live interval. |
Evan Cheng | 20580a1 | 2008-09-19 17:38:47 +0000 | [diff] [blame^] | 2323 | if (MI->registerDefIsDead(DstReg)) { |
| 2324 | if (!ShortenDeadCopySrcLiveRange(RegInt, MI)) |
| 2325 | ShortenDeadCopyLiveRange(RegInt, MI); |
Evan Cheng | 3c88d74 | 2008-03-18 08:26:47 +0000 | [diff] [blame] | 2326 | } |
| 2327 | } |
Evan Cheng | 20580a1 | 2008-09-19 17:38:47 +0000 | [diff] [blame^] | 2328 | li_->RemoveMachineInstrFromMaps(MI); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2329 | mii = mbbi->erase(mii); |
| 2330 | ++numPeep; |
Evan Cheng | a971dbd | 2008-04-24 09:06:33 +0000 | [diff] [blame] | 2331 | } else if (!isMove || !TurnCopyIntoImpDef(mii, mbb, DstReg, SrcReg)) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2332 | SmallSet<unsigned, 4> UniqueUses; |
Evan Cheng | 20580a1 | 2008-09-19 17:38:47 +0000 | [diff] [blame^] | 2333 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 2334 | const MachineOperand &mop = MI->getOperand(i); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2335 | if (mop.isRegister() && mop.getReg() && |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 2336 | TargetRegisterInfo::isVirtualRegister(mop.getReg())) { |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 2337 | unsigned reg = mop.getReg(); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2338 | // Multiple uses of reg by the same instruction. It should not |
| 2339 | // contribute to spill weight again. |
| 2340 | if (UniqueUses.count(reg) != 0) |
| 2341 | continue; |
| 2342 | LiveInterval &RegInt = li_->getInterval(reg); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 2343 | RegInt.weight += |
Evan Cheng | c341760 | 2008-06-21 06:45:54 +0000 | [diff] [blame] | 2344 | li_->getSpillWeight(mop.isDef(), mop.isUse(), loopDepth); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2345 | UniqueUses.insert(reg); |
| 2346 | } |
| 2347 | } |
| 2348 | ++mii; |
| 2349 | } |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | for (LiveIntervals::iterator I = li_->begin(), E = li_->end(); I != E; ++I) { |
Owen Anderson | 03857b2 | 2008-08-13 21:49:13 +0000 | [diff] [blame] | 2354 | LiveInterval &LI = *I->second; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 2355 | if (TargetRegisterInfo::isVirtualRegister(LI.reg)) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2356 | // If the live interval length is essentially zero, i.e. in every live |
| 2357 | // range the use follows def immediately, it doesn't make sense to spill |
| 2358 | // it and hope it will be easier to allocate for this li. |
| 2359 | if (isZeroLengthInterval(&LI)) |
| 2360 | LI.weight = HUGE_VALF; |
Evan Cheng | 5ef3a04 | 2007-12-06 00:01:56 +0000 | [diff] [blame] | 2361 | else { |
| 2362 | bool isLoad = false; |
Evan Cheng | 63a18c4 | 2008-02-09 08:36:28 +0000 | [diff] [blame] | 2363 | if (li_->isReMaterializable(LI, isLoad)) { |
Evan Cheng | 5ef3a04 | 2007-12-06 00:01:56 +0000 | [diff] [blame] | 2364 | // If all of the definitions of the interval are re-materializable, |
| 2365 | // it is a preferred candidate for spilling. If non of the defs are |
| 2366 | // loads, then it's potentially very cheap to re-materialize. |
| 2367 | // FIXME: this gets much more complicated once we support non-trivial |
| 2368 | // re-materialization. |
| 2369 | if (isLoad) |
| 2370 | LI.weight *= 0.9F; |
| 2371 | else |
| 2372 | LI.weight *= 0.5F; |
| 2373 | } |
| 2374 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2375 | |
| 2376 | // Slightly prefer live interval that has been assigned a preferred reg. |
| 2377 | if (LI.preference) |
| 2378 | LI.weight *= 1.01F; |
| 2379 | |
| 2380 | // Divide the weight of the interval by its size. This encourages |
| 2381 | // spilling of intervals that are large and have few uses, and |
| 2382 | // discourages spilling of small intervals with many uses. |
Owen Anderson | 496bac5 | 2008-07-23 19:47:27 +0000 | [diff] [blame] | 2383 | LI.weight /= li_->getApproximateInstructionCount(LI) * InstrSlots::NUM; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | DEBUG(dump()); |
| 2388 | return true; |
| 2389 | } |
| 2390 | |
| 2391 | /// print - Implement the dump method. |
| 2392 | void SimpleRegisterCoalescing::print(std::ostream &O, const Module* m) const { |
| 2393 | li_->print(O, m); |
| 2394 | } |
David Greene | 2c17c4d | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 2395 | |
| 2396 | RegisterCoalescer* llvm::createSimpleRegisterCoalescer() { |
| 2397 | return new SimpleRegisterCoalescing(); |
| 2398 | } |
| 2399 | |
| 2400 | // Make sure that anything that uses RegisterCoalescer pulls in this file... |
| 2401 | DEFINING_FILE_FOR(SimpleRegisterCoalescing) |