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