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