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