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