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