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