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