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