David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1 | //===-- SimpleRegisterCoalescing.cpp - Register Coalescing ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/SimpleRegisterCoalescing.h" |
| 17 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
| 18 | #include "VirtRegMap.h" |
| 19 | #include "llvm/Value.h" |
| 20 | #include "llvm/Analysis/LoopInfo.h" |
| 21 | #include "llvm/CodeGen/LiveVariables.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/MachineInstr.h" |
| 24 | #include "llvm/CodeGen/Passes.h" |
| 25 | #include "llvm/CodeGen/SSARegMap.h" |
David Greene | 2c17c4d | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/RegisterCoalescer.h" |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 27 | #include "llvm/Target/MRegisterInfo.h" |
| 28 | #include "llvm/Target/TargetInstrInfo.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
| 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/ADT/SmallSet.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include "llvm/ADT/STLExtras.h" |
| 35 | #include <algorithm> |
| 36 | #include <cmath> |
| 37 | using namespace llvm; |
| 38 | |
| 39 | STATISTIC(numJoins , "Number of interval joins performed"); |
| 40 | STATISTIC(numPeep , "Number of identity moves eliminated after coalescing"); |
| 41 | STATISTIC(numAborts , "Number of times interval joining aborted"); |
| 42 | |
| 43 | char SimpleRegisterCoalescing::ID = 0; |
| 44 | namespace { |
| 45 | static cl::opt<bool> |
| 46 | EnableJoining("join-liveintervals", |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 47 | cl::desc("Coalesce copies (default=true)"), |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 48 | cl::init(true)); |
| 49 | |
| 50 | RegisterPass<SimpleRegisterCoalescing> |
Chris Lattner | e76fad2 | 2007-08-05 18:45:33 +0000 | [diff] [blame] | 51 | X("simple-register-coalescing", "Simple Register Coalescing"); |
David Greene | 2c17c4d | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 52 | |
| 53 | // Declare that we implement the RegisterCoalescer interface |
| 54 | RegisterAnalysisGroup<RegisterCoalescer, true/*The Default*/> V(X); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | const PassInfo *llvm::SimpleRegisterCoalescingID = X.getPassInfo(); |
| 58 | |
| 59 | void SimpleRegisterCoalescing::getAnalysisUsage(AnalysisUsage &AU) const { |
| 60 | //AU.addPreserved<LiveVariables>(); |
| 61 | AU.addPreserved<LiveIntervals>(); |
| 62 | AU.addPreservedID(PHIEliminationID); |
| 63 | AU.addPreservedID(TwoAddressInstructionPassID); |
| 64 | AU.addRequired<LiveVariables>(); |
| 65 | AU.addRequired<LiveIntervals>(); |
| 66 | AU.addRequired<LoopInfo>(); |
| 67 | MachineFunctionPass::getAnalysisUsage(AU); |
| 68 | } |
| 69 | |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 70 | /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy with IntA |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 71 | /// being the source and IntB being the dest, thus this defines a value number |
| 72 | /// in IntB. If the source value number (in IntA) is defined by a copy from B, |
| 73 | /// see if we can merge these two pieces of B into a single value number, |
| 74 | /// eliminating a copy. For example: |
| 75 | /// |
| 76 | /// A3 = B0 |
| 77 | /// ... |
| 78 | /// B1 = A3 <- this copy |
| 79 | /// |
| 80 | /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1 |
| 81 | /// value number to be replaced with B0 (which simplifies the B liveinterval). |
| 82 | /// |
| 83 | /// This returns true if an interval was modified. |
| 84 | /// |
| 85 | bool SimpleRegisterCoalescing::AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB, |
| 86 | MachineInstr *CopyMI) { |
| 87 | unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI)); |
| 88 | |
| 89 | // BValNo is a value number in B that is defined by a copy from A. 'B3' in |
| 90 | // the example above. |
| 91 | LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 92 | VNInfo *BValNo = BLR->valno; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 93 | |
| 94 | // Get the location that B is defined at. Two options: either this value has |
| 95 | // an unknown definition point or it is defined at CopyIdx. If unknown, we |
| 96 | // can't process it. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 97 | if (!BValNo->reg) return false; |
| 98 | assert(BValNo->def == CopyIdx && |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 99 | "Copy doesn't define the value?"); |
| 100 | |
| 101 | // AValNo is the value number in A that defines the copy, A0 in the example. |
| 102 | LiveInterval::iterator AValLR = IntA.FindLiveRangeContaining(CopyIdx-1); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 103 | VNInfo *AValNo = AValLR->valno; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 104 | |
| 105 | // If AValNo is defined as a copy from IntB, we can potentially process this. |
| 106 | |
| 107 | // Get the instruction that defines this value number. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 108 | unsigned SrcReg = AValNo->reg; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 109 | if (!SrcReg) return false; // Not defined by a copy. |
| 110 | |
| 111 | // If the value number is not defined by a copy instruction, ignore it. |
| 112 | |
| 113 | // If the source register comes from an interval other than IntB, we can't |
| 114 | // handle this. |
| 115 | if (rep(SrcReg) != IntB.reg) return false; |
| 116 | |
| 117 | // Get the LiveRange in IntB that this value number starts with. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 118 | LiveInterval::iterator ValLR = IntB.FindLiveRangeContaining(AValNo->def-1); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 119 | |
| 120 | // Make sure that the end of the live range is inside the same block as |
| 121 | // CopyMI. |
| 122 | MachineInstr *ValLREndInst = li_->getInstructionFromIndex(ValLR->end-1); |
| 123 | if (!ValLREndInst || |
| 124 | ValLREndInst->getParent() != CopyMI->getParent()) return false; |
| 125 | |
| 126 | // Okay, we now know that ValLR ends in the same block that the CopyMI |
| 127 | // live-range starts. If there are no intervening live ranges between them in |
| 128 | // IntB, we can merge them. |
| 129 | if (ValLR+1 != BLR) return false; |
Evan Cheng | dc5294f | 2007-08-14 23:19:28 +0000 | [diff] [blame] | 130 | |
| 131 | // If a live interval is a physical register, conservatively check if any |
| 132 | // of its sub-registers is overlapping the live interval of the virtual |
| 133 | // register. If so, do not coalesce. |
| 134 | if (MRegisterInfo::isPhysicalRegister(IntB.reg) && |
| 135 | *mri_->getSubRegisters(IntB.reg)) { |
| 136 | for (const unsigned* SR = mri_->getSubRegisters(IntB.reg); *SR; ++SR) |
| 137 | if (li_->hasInterval(*SR) && IntA.overlaps(li_->getInterval(*SR))) { |
| 138 | DOUT << "Interfere with sub-register "; |
| 139 | DEBUG(li_->getInterval(*SR).print(DOUT, mri_)); |
| 140 | return false; |
| 141 | } |
| 142 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 143 | |
| 144 | DOUT << "\nExtending: "; IntB.print(DOUT, mri_); |
| 145 | |
Evan Cheng | a8d94f1 | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 146 | unsigned FillerStart = ValLR->end, FillerEnd = BLR->start; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 147 | // 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] | 148 | // that defines this value #'. Update the the valnum with the new defining |
| 149 | // instruction #. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 150 | BValNo->def = FillerStart; |
| 151 | BValNo->reg = 0; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 152 | |
| 153 | // Okay, we can merge them. We need to insert a new liverange: |
| 154 | // [ValLR.end, BLR.begin) of either value number, then we merge the |
| 155 | // two value numbers. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 156 | IntB.addRange(LiveRange(FillerStart, FillerEnd, BValNo)); |
| 157 | |
| 158 | // If the IntB live range is assigned to a physical register, and if that |
| 159 | // physreg has aliases, |
| 160 | if (MRegisterInfo::isPhysicalRegister(IntB.reg)) { |
| 161 | // Update the liveintervals of sub-registers. |
| 162 | for (const unsigned *AS = mri_->getSubRegisters(IntB.reg); *AS; ++AS) { |
| 163 | LiveInterval &AliasLI = li_->getInterval(*AS); |
| 164 | AliasLI.addRange(LiveRange(FillerStart, FillerEnd, |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 165 | AliasLI.getNextValue(FillerStart, 0, li_->getVNInfoAllocator()))); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | // Okay, merge "B1" into the same value number as "B0". |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 170 | if (BValNo != ValLR->valno) |
| 171 | IntB.MergeValueNumberInto(BValNo, ValLR->valno); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 172 | DOUT << " result = "; IntB.print(DOUT, mri_); |
| 173 | DOUT << "\n"; |
| 174 | |
| 175 | // If the source instruction was killing the source register before the |
| 176 | // merge, unset the isKill marker given the live range has been extended. |
| 177 | int UIdx = ValLREndInst->findRegisterUseOperandIdx(IntB.reg, true); |
| 178 | if (UIdx != -1) |
| 179 | ValLREndInst->getOperand(UIdx).unsetIsKill(); |
| 180 | |
| 181 | // Finally, delete the copy instruction. |
| 182 | li_->RemoveMachineInstrFromMaps(CopyMI); |
| 183 | CopyMI->eraseFromParent(); |
| 184 | ++numPeep; |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg, |
| 189 | /// which are the src/dst of the copy instruction CopyMI. This returns true |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 190 | /// if the copy was successfully coalesced away, or if it is never possible |
| 191 | /// to coalesce this copy, due to register constraints. It returns |
| 192 | /// false if it is not currently possible to coalesce this interval, but |
| 193 | /// it may be possible if other things get coalesced. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 194 | bool SimpleRegisterCoalescing::JoinCopy(MachineInstr *CopyMI, |
| 195 | unsigned SrcReg, unsigned DstReg, bool PhysOnly) { |
| 196 | DOUT << li_->getInstructionIndex(CopyMI) << '\t' << *CopyMI; |
| 197 | |
| 198 | // Get representative registers. |
| 199 | unsigned repSrcReg = rep(SrcReg); |
| 200 | unsigned repDstReg = rep(DstReg); |
| 201 | |
| 202 | // If they are already joined we continue. |
| 203 | if (repSrcReg == repDstReg) { |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 204 | DOUT << "\tCopy already coalesced.\n"; |
| 205 | return true; // Not coalescable. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | bool SrcIsPhys = MRegisterInfo::isPhysicalRegister(repSrcReg); |
| 209 | bool DstIsPhys = MRegisterInfo::isPhysicalRegister(repDstReg); |
| 210 | if (PhysOnly && !SrcIsPhys && !DstIsPhys) |
| 211 | // Only joining physical registers with virtual registers in this round. |
| 212 | return true; |
| 213 | |
| 214 | // If they are both physical registers, we cannot join them. |
| 215 | if (SrcIsPhys && DstIsPhys) { |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 216 | DOUT << "\tCan not coalesce physregs.\n"; |
| 217 | return true; // Not coalescable. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // We only join virtual registers with allocatable physical registers. |
| 221 | if (SrcIsPhys && !allocatableRegs_[repSrcReg]) { |
| 222 | DOUT << "\tSrc reg is unallocatable physreg.\n"; |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 223 | return true; // Not coalescable. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 224 | } |
| 225 | if (DstIsPhys && !allocatableRegs_[repDstReg]) { |
| 226 | DOUT << "\tDst reg is unallocatable physreg.\n"; |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 227 | return true; // Not coalescable. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 228 | } |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 229 | |
| 230 | bool isExtSubReg = CopyMI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG; |
| 231 | unsigned RealDstReg = 0; |
| 232 | if (isExtSubReg) { |
| 233 | unsigned SubIdx = CopyMI->getOperand(2).getImm(); |
| 234 | if (SrcIsPhys) |
| 235 | // r1024 = EXTRACT_SUBREG EAX, 0 then r1024 is really going to be |
| 236 | // coalesced with AX. |
| 237 | repSrcReg = mri_->getSubReg(repSrcReg, SubIdx); |
| 238 | else if (DstIsPhys) { |
| 239 | // If this is a extract_subreg where dst is a physical register, e.g. |
| 240 | // cl = EXTRACT_SUBREG reg1024, 1 |
| 241 | // then create and update the actual physical register allocated to RHS. |
| 242 | const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(SrcReg); |
| 243 | for (const unsigned *SRs = mri_->getSuperRegisters(repDstReg); |
| 244 | unsigned SR = *SRs; ++SRs) { |
| 245 | if (repDstReg == mri_->getSubReg(SR, SubIdx) && |
| 246 | RC->contains(SR)) { |
| 247 | RealDstReg = SR; |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | assert(RealDstReg && "Invalid extra_subreg instruction!"); |
| 252 | |
| 253 | // For this type of EXTRACT_SUBREG, conservatively |
| 254 | // check if the live interval of the source register interfere with the |
| 255 | // actual super physical register we are trying to coalesce with. |
| 256 | LiveInterval &RHS = li_->getInterval(repSrcReg); |
| 257 | if (li_->hasInterval(RealDstReg) && |
| 258 | RHS.overlaps(li_->getInterval(RealDstReg))) { |
| 259 | DOUT << "Interfere with register "; |
| 260 | DEBUG(li_->getInterval(RealDstReg).print(DOUT, mri_)); |
| 261 | return true; // Not coalescable |
| 262 | } |
| 263 | for (const unsigned* SR = mri_->getSubRegisters(RealDstReg); *SR; ++SR) |
| 264 | if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) { |
| 265 | DOUT << "Interfere with sub-register "; |
| 266 | DEBUG(li_->getInterval(*SR).print(DOUT, mri_)); |
| 267 | return true; |
| 268 | } |
| 269 | } |
| 270 | } else if (differingRegisterClasses(repSrcReg, repDstReg)) { |
| 271 | // If they are not of the same register class, we cannot join them. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 272 | DOUT << "\tSrc/Dest are different register classes.\n"; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 273 | // Allow the coalescer to try again in case either side gets coalesced to |
| 274 | // a physical register that's compatible with the other side. e.g. |
| 275 | // r1024 = MOV32to32_ r1025 |
| 276 | // but later r1024 is assigned EAX then r1025 may be coalesced with EAX. |
| 277 | return false; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | LiveInterval &SrcInt = li_->getInterval(repSrcReg); |
| 281 | LiveInterval &DstInt = li_->getInterval(repDstReg); |
| 282 | assert(SrcInt.reg == repSrcReg && DstInt.reg == repDstReg && |
| 283 | "Register mapping is horribly broken!"); |
| 284 | |
| 285 | DOUT << "\t\tInspecting "; SrcInt.print(DOUT, mri_); |
| 286 | DOUT << " and "; DstInt.print(DOUT, mri_); |
| 287 | DOUT << ": "; |
| 288 | |
| 289 | // Check if it is necessary to propagate "isDead" property before intervals |
| 290 | // are joined. |
| 291 | MachineOperand *mopd = CopyMI->findRegisterDefOperand(DstReg); |
| 292 | bool isDead = mopd->isDead(); |
| 293 | bool isShorten = false; |
| 294 | unsigned SrcStart = 0, RemoveStart = 0; |
| 295 | unsigned SrcEnd = 0, RemoveEnd = 0; |
| 296 | if (isDead) { |
| 297 | unsigned CopyIdx = li_->getInstructionIndex(CopyMI); |
| 298 | LiveInterval::iterator SrcLR = |
| 299 | SrcInt.FindLiveRangeContaining(li_->getUseIndex(CopyIdx)); |
| 300 | RemoveStart = SrcStart = SrcLR->start; |
| 301 | RemoveEnd = SrcEnd = SrcLR->end; |
| 302 | // The instruction which defines the src is only truly dead if there are |
| 303 | // no intermediate uses and there isn't a use beyond the copy. |
| 304 | // FIXME: find the last use, mark is kill and shorten the live range. |
| 305 | if (SrcEnd > li_->getDefIndex(CopyIdx)) { |
| 306 | isDead = false; |
| 307 | } else { |
| 308 | MachineOperand *MOU; |
| 309 | MachineInstr *LastUse= lastRegisterUse(SrcStart, CopyIdx, repSrcReg, MOU); |
| 310 | if (LastUse) { |
| 311 | // Shorten the liveinterval to the end of last use. |
| 312 | MOU->setIsKill(); |
| 313 | isDead = false; |
| 314 | isShorten = true; |
| 315 | RemoveStart = li_->getDefIndex(li_->getInstructionIndex(LastUse)); |
| 316 | RemoveEnd = SrcEnd; |
| 317 | } else { |
| 318 | MachineInstr *SrcMI = li_->getInstructionFromIndex(SrcStart); |
| 319 | if (SrcMI) { |
| 320 | MachineOperand *mops = findDefOperand(SrcMI, repSrcReg); |
| 321 | if (mops) |
| 322 | // A dead def should have a single cycle interval. |
| 323 | ++RemoveStart; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // We need to be careful about coalescing a source physical register with a |
| 330 | // virtual register. Once the coalescing is done, it cannot be broken and |
| 331 | // these are not spillable! If the destination interval uses are far away, |
| 332 | // think twice about coalescing them! |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 333 | if (!mopd->isDead() && (SrcIsPhys || DstIsPhys) && !isExtSubReg) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 334 | LiveInterval &JoinVInt = SrcIsPhys ? DstInt : SrcInt; |
| 335 | unsigned JoinVReg = SrcIsPhys ? repDstReg : repSrcReg; |
| 336 | unsigned JoinPReg = SrcIsPhys ? repSrcReg : repDstReg; |
| 337 | const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(JoinVReg); |
| 338 | unsigned Threshold = allocatableRCRegs_[RC].count(); |
| 339 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 340 | // If the virtual register live interval is long but it has low use desity, |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 341 | // do not join them, instead mark the physical register as its allocation |
| 342 | // preference. |
| 343 | unsigned Length = JoinVInt.getSize() / InstrSlots::NUM; |
| 344 | LiveVariables::VarInfo &vi = lv_->getVarInfo(JoinVReg); |
| 345 | if (Length > Threshold && |
| 346 | (((float)vi.NumUses / Length) < (1.0 / Threshold))) { |
| 347 | JoinVInt.preference = JoinPReg; |
| 348 | ++numAborts; |
| 349 | DOUT << "\tMay tie down a physical register, abort!\n"; |
| 350 | return false; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // Okay, attempt to join these two intervals. On failure, this returns false. |
| 355 | // Otherwise, if one of the intervals being joined is a physreg, this method |
| 356 | // always canonicalizes DstInt to be it. The output "SrcInt" will not have |
| 357 | // been modified, so we can use this information below to update aliases. |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 358 | bool Swapped = false; |
| 359 | if (JoinIntervals(DstInt, SrcInt, Swapped)) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 360 | if (isDead) { |
| 361 | // Result of the copy is dead. Propagate this property. |
| 362 | if (SrcStart == 0) { |
| 363 | assert(MRegisterInfo::isPhysicalRegister(repSrcReg) && |
| 364 | "Live-in must be a physical register!"); |
| 365 | // Live-in to the function but dead. Remove it from entry live-in set. |
| 366 | // JoinIntervals may end up swapping the two intervals. |
| 367 | mf_->begin()->removeLiveIn(repSrcReg); |
| 368 | } else { |
| 369 | MachineInstr *SrcMI = li_->getInstructionFromIndex(SrcStart); |
| 370 | if (SrcMI) { |
| 371 | MachineOperand *mops = findDefOperand(SrcMI, repSrcReg); |
| 372 | if (mops) |
| 373 | mops->setIsDead(); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if (isShorten || isDead) { |
Evan Cheng | ccb36a4 | 2007-08-12 01:26:19 +0000 | [diff] [blame] | 379 | // Shorten the destination live interval. |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 380 | if (Swapped) |
| 381 | SrcInt.removeRange(RemoveStart, RemoveEnd); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 382 | } |
| 383 | } else { |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 384 | // Coalescing failed. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 385 | |
| 386 | // If we can eliminate the copy without merging the live ranges, do so now. |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 387 | if (!isExtSubReg && AdjustCopiesBackFrom(SrcInt, DstInt, CopyMI)) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 388 | return true; |
| 389 | |
| 390 | // Otherwise, we are unable to join the intervals. |
| 391 | DOUT << "Interference!\n"; |
| 392 | return false; |
| 393 | } |
| 394 | |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 395 | LiveInterval *ResSrcInt = &SrcInt; |
| 396 | LiveInterval *ResDstInt = &DstInt; |
| 397 | if (Swapped) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 398 | std::swap(repSrcReg, repDstReg); |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 399 | std::swap(ResSrcInt, ResDstInt); |
| 400 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 401 | assert(MRegisterInfo::isVirtualRegister(repSrcReg) && |
| 402 | "LiveInterval::join didn't work right!"); |
| 403 | |
| 404 | // If we're about to merge live ranges into a physical register live range, |
| 405 | // we have to update any aliased register's live ranges to indicate that they |
| 406 | // have clobbered values for this range. |
| 407 | if (MRegisterInfo::isPhysicalRegister(repDstReg)) { |
| 408 | // Unset unnecessary kills. |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 409 | if (!ResDstInt->containsOneValue()) { |
| 410 | for (LiveInterval::Ranges::const_iterator I = ResSrcInt->begin(), |
| 411 | E = ResSrcInt->end(); I != E; ++I) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 412 | unsetRegisterKills(I->start, I->end, repDstReg); |
| 413 | } |
| 414 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 415 | // If this is a extract_subreg where dst is a physical register, e.g. |
| 416 | // cl = EXTRACT_SUBREG reg1024, 1 |
| 417 | // then create and update the actual physical register allocated to RHS. |
| 418 | if (RealDstReg) { |
| 419 | unsigned CopyIdx = li_->getInstructionIndex(CopyMI); |
| 420 | VNInfo *DstValNo = |
| 421 | ResDstInt->getLiveRangeContaining(li_->getUseIndex(CopyIdx))->valno; |
| 422 | LiveInterval &RealDstInt = li_->getOrCreateInterval(RealDstReg); |
| 423 | VNInfo *ValNo = RealDstInt.getNextValue(DstValNo->def, DstValNo->reg, |
| 424 | li_->getVNInfoAllocator()); |
| 425 | RealDstInt.addKills(ValNo, DstValNo->kills); |
| 426 | RealDstInt.MergeValueInAsValue(*ResDstInt, DstValNo, ValNo); |
| 427 | repDstReg = RealDstReg; |
| 428 | } |
| 429 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 430 | // Update the liveintervals of sub-registers. |
| 431 | for (const unsigned *AS = mri_->getSubRegisters(repDstReg); *AS; ++AS) |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 432 | li_->getOrCreateInterval(*AS).MergeInClobberRanges(*ResSrcInt, |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 433 | li_->getVNInfoAllocator()); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 434 | } else { |
| 435 | // Merge use info if the destination is a virtual register. |
| 436 | LiveVariables::VarInfo& dVI = lv_->getVarInfo(repDstReg); |
| 437 | LiveVariables::VarInfo& sVI = lv_->getVarInfo(repSrcReg); |
| 438 | dVI.NumUses += sVI.NumUses; |
| 439 | } |
| 440 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 441 | // Remember these liveintervals have been joined. |
| 442 | JoinedLIs.set(repSrcReg - MRegisterInfo::FirstVirtualRegister); |
| 443 | if (MRegisterInfo::isVirtualRegister(repDstReg)) |
| 444 | JoinedLIs.set(repDstReg - MRegisterInfo::FirstVirtualRegister); |
| 445 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 446 | if (isExtSubReg && !SrcIsPhys && !DstIsPhys) { |
| 447 | if (!Swapped) { |
| 448 | // Make sure we allocate the larger super-register. |
| 449 | ResSrcInt->Copy(*ResDstInt, li_->getVNInfoAllocator()); |
| 450 | std::swap(repSrcReg, repDstReg); |
| 451 | std::swap(ResSrcInt, ResDstInt); |
| 452 | } |
| 453 | SubRegIdxes.push_back(std::make_pair(repSrcReg, |
| 454 | CopyMI->getOperand(2).getImm())); |
| 455 | } |
| 456 | |
| 457 | DOUT << "\n\t\tJoined. Result = "; ResDstInt->print(DOUT, mri_); |
| 458 | DOUT << "\n"; |
| 459 | |
Evan Cheng | 273288c | 2007-07-18 23:34:48 +0000 | [diff] [blame] | 460 | // repSrcReg is guarateed to be the register whose live interval that is |
| 461 | // being merged. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 462 | li_->removeInterval(repSrcReg); |
| 463 | r2rMap_[repSrcReg] = repDstReg; |
| 464 | |
| 465 | // Finally, delete the copy instruction. |
| 466 | li_->RemoveMachineInstrFromMaps(CopyMI); |
| 467 | CopyMI->eraseFromParent(); |
| 468 | ++numPeep; |
| 469 | ++numJoins; |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | /// ComputeUltimateVN - Assuming we are going to join two live intervals, |
| 474 | /// compute what the resultant value numbers for each value in the input two |
| 475 | /// ranges will be. This is complicated by copies between the two which can |
| 476 | /// and will commonly cause multiple value numbers to be merged into one. |
| 477 | /// |
| 478 | /// VN is the value number that we're trying to resolve. InstDefiningValue |
| 479 | /// keeps track of the new InstDefiningValue assignment for the result |
| 480 | /// LiveInterval. ThisFromOther/OtherFromThis are sets that keep track of |
| 481 | /// whether a value in this or other is a copy from the opposite set. |
| 482 | /// ThisValNoAssignments/OtherValNoAssignments keep track of value #'s that have |
| 483 | /// already been assigned. |
| 484 | /// |
| 485 | /// ThisFromOther[x] - If x is defined as a copy from the other interval, this |
| 486 | /// contains the value number the copy is from. |
| 487 | /// |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 488 | static unsigned ComputeUltimateVN(VNInfo *VNI, |
| 489 | SmallVector<VNInfo*, 16> &NewVNInfo, |
Evan Cheng | fadfb5b | 2007-08-31 21:23:06 +0000 | [diff] [blame] | 490 | DenseMap<VNInfo*, VNInfo*> &ThisFromOther, |
| 491 | DenseMap<VNInfo*, VNInfo*> &OtherFromThis, |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 492 | SmallVector<int, 16> &ThisValNoAssignments, |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 493 | SmallVector<int, 16> &OtherValNoAssignments) { |
| 494 | unsigned VN = VNI->id; |
| 495 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 496 | // If the VN has already been computed, just return it. |
| 497 | if (ThisValNoAssignments[VN] >= 0) |
| 498 | return ThisValNoAssignments[VN]; |
| 499 | // assert(ThisValNoAssignments[VN] != -2 && "Cyclic case?"); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 500 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 501 | // If this val is not a copy from the other val, then it must be a new value |
| 502 | // number in the destination. |
Evan Cheng | fadfb5b | 2007-08-31 21:23:06 +0000 | [diff] [blame] | 503 | DenseMap<VNInfo*, VNInfo*>::iterator I = ThisFromOther.find(VNI); |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 504 | if (I == ThisFromOther.end()) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 505 | NewVNInfo.push_back(VNI); |
| 506 | return ThisValNoAssignments[VN] = NewVNInfo.size()-1; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 507 | } |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 508 | VNInfo *OtherValNo = I->second; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 509 | |
| 510 | // Otherwise, this *is* a copy from the RHS. If the other side has already |
| 511 | // been computed, return it. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 512 | if (OtherValNoAssignments[OtherValNo->id] >= 0) |
| 513 | return ThisValNoAssignments[VN] = OtherValNoAssignments[OtherValNo->id]; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 514 | |
| 515 | // Mark this value number as currently being computed, then ask what the |
| 516 | // ultimate value # of the other value is. |
| 517 | ThisValNoAssignments[VN] = -2; |
| 518 | unsigned UltimateVN = |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 519 | ComputeUltimateVN(OtherValNo, NewVNInfo, OtherFromThis, ThisFromOther, |
| 520 | OtherValNoAssignments, ThisValNoAssignments); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 521 | return ThisValNoAssignments[VN] = UltimateVN; |
| 522 | } |
| 523 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 524 | static bool InVector(VNInfo *Val, const SmallVector<VNInfo*, 8> &V) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 525 | return std::find(V.begin(), V.end(), Val) != V.end(); |
| 526 | } |
| 527 | |
| 528 | /// SimpleJoin - Attempt to joint the specified interval into this one. The |
| 529 | /// caller of this method must guarantee that the RHS only contains a single |
| 530 | /// value number and that the RHS is not defined by a copy from this |
| 531 | /// interval. This returns false if the intervals are not joinable, or it |
| 532 | /// joins them and returns true. |
| 533 | bool SimpleRegisterCoalescing::SimpleJoin(LiveInterval &LHS, LiveInterval &RHS) { |
| 534 | assert(RHS.containsOneValue()); |
| 535 | |
| 536 | // Some number (potentially more than one) value numbers in the current |
| 537 | // interval may be defined as copies from the RHS. Scan the overlapping |
| 538 | // portions of the LHS and RHS, keeping track of this and looking for |
| 539 | // 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] | 540 | // cannot coalesce. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 541 | |
| 542 | LiveInterval::iterator LHSIt = LHS.begin(), LHSEnd = LHS.end(); |
| 543 | LiveInterval::iterator RHSIt = RHS.begin(), RHSEnd = RHS.end(); |
| 544 | |
| 545 | if (LHSIt->start < RHSIt->start) { |
| 546 | LHSIt = std::upper_bound(LHSIt, LHSEnd, RHSIt->start); |
| 547 | if (LHSIt != LHS.begin()) --LHSIt; |
| 548 | } else if (RHSIt->start < LHSIt->start) { |
| 549 | RHSIt = std::upper_bound(RHSIt, RHSEnd, LHSIt->start); |
| 550 | if (RHSIt != RHS.begin()) --RHSIt; |
| 551 | } |
| 552 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 553 | SmallVector<VNInfo*, 8> EliminatedLHSVals; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 554 | |
| 555 | while (1) { |
| 556 | // Determine if these live intervals overlap. |
| 557 | bool Overlaps = false; |
| 558 | if (LHSIt->start <= RHSIt->start) |
| 559 | Overlaps = LHSIt->end > RHSIt->start; |
| 560 | else |
| 561 | Overlaps = RHSIt->end > LHSIt->start; |
| 562 | |
| 563 | // If the live intervals overlap, there are two interesting cases: if the |
| 564 | // LHS interval is defined by a copy from the RHS, it's ok and we record |
| 565 | // 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] | 566 | // coalesce these live ranges and we bail out. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 567 | if (Overlaps) { |
| 568 | // 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] | 569 | if (!InVector(LHSIt->valno, EliminatedLHSVals)) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 570 | // Copy from the RHS? |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 571 | unsigned SrcReg = LHSIt->valno->reg; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 572 | if (rep(SrcReg) != RHS.reg) |
| 573 | return false; // Nope, bail out. |
| 574 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 575 | EliminatedLHSVals.push_back(LHSIt->valno); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | // We know this entire LHS live range is okay, so skip it now. |
| 579 | if (++LHSIt == LHSEnd) break; |
| 580 | continue; |
| 581 | } |
| 582 | |
| 583 | if (LHSIt->end < RHSIt->end) { |
| 584 | if (++LHSIt == LHSEnd) break; |
| 585 | } else { |
| 586 | // One interesting case to check here. It's possible that we have |
| 587 | // something like "X3 = Y" which defines a new value number in the LHS, |
| 588 | // 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] | 589 | // 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] | 590 | // the live ranges don't actually overlap. |
| 591 | if (LHSIt->start == RHSIt->end) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 592 | if (InVector(LHSIt->valno, EliminatedLHSVals)) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 593 | // 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] | 594 | // if coalescing succeeds. Just skip the liverange. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 595 | if (++LHSIt == LHSEnd) break; |
| 596 | } else { |
| 597 | // Otherwise, if this is a copy from the RHS, mark it as being merged |
| 598 | // in. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 599 | if (rep(LHSIt->valno->reg) == RHS.reg) { |
| 600 | EliminatedLHSVals.push_back(LHSIt->valno); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 601 | |
| 602 | // We know this entire LHS live range is okay, so skip it now. |
| 603 | if (++LHSIt == LHSEnd) break; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (++RHSIt == RHSEnd) break; |
| 609 | } |
| 610 | } |
| 611 | |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 612 | // 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] | 613 | // the value numbers in EliminatedLHSVals will all be merged together. Since |
| 614 | // the most common case is that EliminatedLHSVals has a single number, we |
| 615 | // optimize for it: if there is more than one value, we merge them all into |
| 616 | // the lowest numbered one, then handle the interval as if we were merging |
| 617 | // with one value number. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 618 | VNInfo *LHSValNo; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 619 | if (EliminatedLHSVals.size() > 1) { |
| 620 | // Loop through all the equal value numbers merging them into the smallest |
| 621 | // one. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 622 | VNInfo *Smallest = EliminatedLHSVals[0]; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 623 | for (unsigned i = 1, e = EliminatedLHSVals.size(); i != e; ++i) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 624 | if (EliminatedLHSVals[i]->id < Smallest->id) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 625 | // Merge the current notion of the smallest into the smaller one. |
| 626 | LHS.MergeValueNumberInto(Smallest, EliminatedLHSVals[i]); |
| 627 | Smallest = EliminatedLHSVals[i]; |
| 628 | } else { |
| 629 | // Merge into the smallest. |
| 630 | LHS.MergeValueNumberInto(EliminatedLHSVals[i], Smallest); |
| 631 | } |
| 632 | } |
| 633 | LHSValNo = Smallest; |
| 634 | } else { |
| 635 | assert(!EliminatedLHSVals.empty() && "No copies from the RHS?"); |
| 636 | LHSValNo = EliminatedLHSVals[0]; |
| 637 | } |
| 638 | |
| 639 | // Okay, now that there is a single LHS value number that we're merging the |
| 640 | // RHS into, update the value number info for the LHS to indicate that the |
| 641 | // value number is defined where the RHS value number was. |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 642 | const VNInfo *VNI = RHS.getValNumInfo(0); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 643 | LHSValNo->def = VNI->def; |
| 644 | LHSValNo->reg = VNI->reg; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 645 | |
| 646 | // Okay, the final step is to loop over the RHS live intervals, adding them to |
| 647 | // the LHS. |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 648 | LHS.addKills(LHSValNo, VNI->kills); |
Evan Cheng | 430a7b0 | 2007-08-14 01:56:58 +0000 | [diff] [blame] | 649 | LHS.MergeRangesInAsValue(RHS, LHSValNo); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 650 | LHS.weight += RHS.weight; |
| 651 | if (RHS.preference && !LHS.preference) |
| 652 | LHS.preference = RHS.preference; |
| 653 | |
| 654 | return true; |
| 655 | } |
| 656 | |
| 657 | /// JoinIntervals - Attempt to join these two intervals. On failure, this |
| 658 | /// returns false. Otherwise, if one of the intervals being joined is a |
| 659 | /// physreg, this method always canonicalizes LHS to be it. The output |
| 660 | /// "RHS" will not have been modified, so we can use this information |
| 661 | /// below to update aliases. |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 662 | bool SimpleRegisterCoalescing::JoinIntervals(LiveInterval &LHS, |
| 663 | LiveInterval &RHS, bool &Swapped) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 664 | // Compute the final value assignment, assuming that the live ranges can be |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 665 | // coalesced. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 666 | SmallVector<int, 16> LHSValNoAssignments; |
| 667 | SmallVector<int, 16> RHSValNoAssignments; |
Evan Cheng | fadfb5b | 2007-08-31 21:23:06 +0000 | [diff] [blame] | 668 | DenseMap<VNInfo*, VNInfo*> LHSValsDefinedFromRHS; |
| 669 | DenseMap<VNInfo*, VNInfo*> RHSValsDefinedFromLHS; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 670 | SmallVector<VNInfo*, 16> NewVNInfo; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 671 | |
| 672 | // If a live interval is a physical register, conservatively check if any |
| 673 | // of its sub-registers is overlapping the live interval of the virtual |
| 674 | // register. If so, do not coalesce. |
| 675 | if (MRegisterInfo::isPhysicalRegister(LHS.reg) && |
| 676 | *mri_->getSubRegisters(LHS.reg)) { |
| 677 | for (const unsigned* SR = mri_->getSubRegisters(LHS.reg); *SR; ++SR) |
| 678 | if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) { |
| 679 | DOUT << "Interfere with sub-register "; |
| 680 | DEBUG(li_->getInterval(*SR).print(DOUT, mri_)); |
| 681 | return false; |
| 682 | } |
| 683 | } else if (MRegisterInfo::isPhysicalRegister(RHS.reg) && |
| 684 | *mri_->getSubRegisters(RHS.reg)) { |
| 685 | for (const unsigned* SR = mri_->getSubRegisters(RHS.reg); *SR; ++SR) |
| 686 | if (li_->hasInterval(*SR) && LHS.overlaps(li_->getInterval(*SR))) { |
| 687 | DOUT << "Interfere with sub-register "; |
| 688 | DEBUG(li_->getInterval(*SR).print(DOUT, mri_)); |
| 689 | return false; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | // Compute ultimate value numbers for the LHS and RHS values. |
| 694 | if (RHS.containsOneValue()) { |
| 695 | // Copies from a liveinterval with a single value are simple to handle and |
| 696 | // very common, handle the special case here. This is important, because |
| 697 | // often RHS is small and LHS is large (e.g. a physreg). |
| 698 | |
| 699 | // 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] | 700 | int RHSVal0DefinedFromLHS = -1; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 701 | int RHSValID = -1; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 702 | VNInfo *RHSValNoInfo = NULL; |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 703 | VNInfo *RHSValNoInfo0 = RHS.getValNumInfo(0); |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 704 | unsigned RHSSrcReg = RHSValNoInfo0->reg; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 705 | if ((RHSSrcReg == 0 || rep(RHSSrcReg) != LHS.reg)) { |
| 706 | // 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] | 707 | // faster checks to see if the live ranges are coalescable. This joiner |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 708 | // can't swap the LHS/RHS intervals though. |
| 709 | if (!MRegisterInfo::isPhysicalRegister(RHS.reg)) { |
| 710 | return SimpleJoin(LHS, RHS); |
| 711 | } else { |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 712 | RHSValNoInfo = RHSValNoInfo0; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 713 | } |
| 714 | } else { |
| 715 | // 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] | 716 | RHSValNoInfo = LHS.getLiveRangeContaining(RHSValNoInfo0->def-1)->valno; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 717 | RHSValID = RHSValNoInfo->id; |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 718 | RHSVal0DefinedFromLHS = RHSValID; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | LHSValNoAssignments.resize(LHS.getNumValNums(), -1); |
| 722 | RHSValNoAssignments.resize(RHS.getNumValNums(), -1); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 723 | NewVNInfo.resize(LHS.getNumValNums(), NULL); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 724 | |
| 725 | // Okay, *all* of the values in LHS that are defined as a copy from RHS |
| 726 | // should now get updated. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 727 | for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end(); |
| 728 | i != e; ++i) { |
| 729 | VNInfo *VNI = *i; |
| 730 | unsigned VN = VNI->id; |
| 731 | if (unsigned LHSSrcReg = VNI->reg) { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 732 | if (rep(LHSSrcReg) != RHS.reg) { |
| 733 | // 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] | 734 | // unmodified by the coalescing. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 735 | NewVNInfo[VN] = VNI; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 736 | LHSValNoAssignments[VN] = VN; |
| 737 | } else if (RHSValID == -1) { |
| 738 | // Otherwise, it is a copy from the RHS, and we don't already have a |
| 739 | // value# for it. Keep the current value number, but remember it. |
| 740 | LHSValNoAssignments[VN] = RHSValID = VN; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 741 | NewVNInfo[VN] = RHSValNoInfo; |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 742 | LHSValsDefinedFromRHS[VNI] = RHSValNoInfo0; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 743 | } else { |
| 744 | // Otherwise, use the specified value #. |
| 745 | LHSValNoAssignments[VN] = RHSValID; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 746 | if (VN == (unsigned)RHSValID) { // Else this val# is dead. |
| 747 | NewVNInfo[VN] = RHSValNoInfo; |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 748 | LHSValsDefinedFromRHS[VNI] = RHSValNoInfo0; |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 749 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 750 | } |
| 751 | } else { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 752 | NewVNInfo[VN] = VNI; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 753 | LHSValNoAssignments[VN] = VN; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | assert(RHSValID != -1 && "Didn't find value #?"); |
| 758 | RHSValNoAssignments[0] = RHSValID; |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 759 | if (RHSVal0DefinedFromLHS != -1) { |
Evan Cheng | 3430135 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 760 | // This path doesn't go through ComputeUltimateVN so just set |
| 761 | // it to anything. |
| 762 | RHSValsDefinedFromLHS[RHSValNoInfo0] = (VNInfo*)1; |
Evan Cheng | 4f8ff16 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 763 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 764 | } else { |
| 765 | // Loop over the value numbers of the LHS, seeing if any are defined from |
| 766 | // the RHS. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 767 | for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end(); |
| 768 | i != e; ++i) { |
| 769 | VNInfo *VNI = *i; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 770 | unsigned ValSrcReg = VNI->reg; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 771 | if (ValSrcReg == 0) // Src not defined by a copy? |
| 772 | continue; |
| 773 | |
| 774 | // DstReg is known to be a register in the LHS interval. If the src is |
| 775 | // from the RHS interval, we can use its value #. |
| 776 | if (rep(ValSrcReg) != RHS.reg) |
| 777 | continue; |
| 778 | |
| 779 | // Figure out the value # from the RHS. |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 780 | LHSValsDefinedFromRHS[VNI] = RHS.getLiveRangeContaining(VNI->def-1)->valno; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | // Loop over the value numbers of the RHS, seeing if any are defined from |
| 784 | // the LHS. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 785 | for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end(); |
| 786 | i != e; ++i) { |
| 787 | VNInfo *VNI = *i; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 788 | unsigned ValSrcReg = VNI->reg; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 789 | if (ValSrcReg == 0) // Src not defined by a copy? |
| 790 | continue; |
| 791 | |
| 792 | // DstReg is known to be a register in the RHS interval. If the src is |
| 793 | // from the LHS interval, we can use its value #. |
| 794 | if (rep(ValSrcReg) != LHS.reg) |
| 795 | continue; |
| 796 | |
| 797 | // Figure out the value # from the LHS. |
Evan Cheng | c14b144 | 2007-08-31 08:04:17 +0000 | [diff] [blame] | 798 | RHSValsDefinedFromLHS[VNI]= LHS.getLiveRangeContaining(VNI->def-1)->valno; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | LHSValNoAssignments.resize(LHS.getNumValNums(), -1); |
| 802 | RHSValNoAssignments.resize(RHS.getNumValNums(), -1); |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 803 | NewVNInfo.reserve(LHS.getNumValNums() + RHS.getNumValNums()); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 804 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 805 | for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end(); |
| 806 | i != e; ++i) { |
| 807 | VNInfo *VNI = *i; |
| 808 | unsigned VN = VNI->id; |
| 809 | if (LHSValNoAssignments[VN] >= 0 || VNI->def == ~1U) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 810 | continue; |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 811 | ComputeUltimateVN(VNI, NewVNInfo, |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 812 | LHSValsDefinedFromRHS, RHSValsDefinedFromLHS, |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 813 | LHSValNoAssignments, RHSValNoAssignments); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 814 | } |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 815 | for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end(); |
| 816 | i != e; ++i) { |
| 817 | VNInfo *VNI = *i; |
| 818 | unsigned VN = VNI->id; |
| 819 | if (RHSValNoAssignments[VN] >= 0 || VNI->def == ~1U) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 820 | continue; |
| 821 | // 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] | 822 | if (RHSValsDefinedFromLHS.find(VNI) == RHSValsDefinedFromLHS.end()) { |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 823 | NewVNInfo.push_back(VNI); |
| 824 | RHSValNoAssignments[VN] = NewVNInfo.size()-1; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 825 | continue; |
| 826 | } |
| 827 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 828 | ComputeUltimateVN(VNI, NewVNInfo, |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 829 | RHSValsDefinedFromLHS, LHSValsDefinedFromRHS, |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 830 | RHSValNoAssignments, LHSValNoAssignments); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
| 834 | // 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] | 835 | // interval lists to see if these intervals are coalescable. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 836 | LiveInterval::const_iterator I = LHS.begin(); |
| 837 | LiveInterval::const_iterator IE = LHS.end(); |
| 838 | LiveInterval::const_iterator J = RHS.begin(); |
| 839 | LiveInterval::const_iterator JE = RHS.end(); |
| 840 | |
| 841 | // Skip ahead until the first place of potential sharing. |
| 842 | if (I->start < J->start) { |
| 843 | I = std::upper_bound(I, IE, J->start); |
| 844 | if (I != LHS.begin()) --I; |
| 845 | } else if (J->start < I->start) { |
| 846 | J = std::upper_bound(J, JE, I->start); |
| 847 | if (J != RHS.begin()) --J; |
| 848 | } |
| 849 | |
| 850 | while (1) { |
| 851 | // Determine if these two live ranges overlap. |
| 852 | bool Overlaps; |
| 853 | if (I->start < J->start) { |
| 854 | Overlaps = I->end > J->start; |
| 855 | } else { |
| 856 | Overlaps = J->end > I->start; |
| 857 | } |
| 858 | |
| 859 | // If so, check value # info to determine if they are really different. |
| 860 | if (Overlaps) { |
| 861 | // 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] | 862 | // result liverange, we can still coalesce them. If not, we can't. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 863 | if (LHSValNoAssignments[I->valno->id] != |
| 864 | RHSValNoAssignments[J->valno->id]) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 865 | return false; |
| 866 | } |
| 867 | |
| 868 | if (I->end < J->end) { |
| 869 | ++I; |
| 870 | if (I == IE) break; |
| 871 | } else { |
| 872 | ++J; |
| 873 | if (J == JE) break; |
| 874 | } |
| 875 | } |
| 876 | |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 877 | // If we get here, we know that we can coalesce the live ranges. Ask the |
| 878 | // intervals to coalesce themselves now. |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 879 | if ((RHS.ranges.size() > LHS.ranges.size() && |
| 880 | MRegisterInfo::isVirtualRegister(LHS.reg)) || |
| 881 | MRegisterInfo::isPhysicalRegister(RHS.reg)) { |
Evan Cheng | 3430135 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 882 | // Update kill info. Some live ranges are extended due to copy coalescing. |
| 883 | for (DenseMap<VNInfo*, VNInfo*>::iterator I = LHSValsDefinedFromRHS.begin(), |
| 884 | E = LHSValsDefinedFromRHS.end(); I != E; ++I) { |
| 885 | VNInfo *VNI = I->first; |
| 886 | unsigned LHSValID = LHSValNoAssignments[VNI->id]; |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 887 | LiveInterval::removeKill(NewVNInfo[LHSValID], VNI->def); |
| 888 | RHS.addKills(NewVNInfo[LHSValID], VNI->kills); |
Evan Cheng | 3430135 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 891 | RHS.join(LHS, &RHSValNoAssignments[0], &LHSValNoAssignments[0], NewVNInfo); |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 892 | Swapped = true; |
| 893 | } else { |
Evan Cheng | 3430135 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 894 | // Update kill info. Some live ranges are extended due to copy coalescing. |
| 895 | for (DenseMap<VNInfo*, VNInfo*>::iterator I = RHSValsDefinedFromLHS.begin(), |
| 896 | E = RHSValsDefinedFromLHS.end(); I != E; ++I) { |
| 897 | VNInfo *VNI = I->first; |
| 898 | unsigned RHSValID = RHSValNoAssignments[VNI->id]; |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 899 | LiveInterval::removeKill(NewVNInfo[RHSValID], VNI->def); |
| 900 | LHS.addKills(NewVNInfo[RHSValID], VNI->kills); |
Evan Cheng | 3430135 | 2007-09-01 02:03:17 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 903 | LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0], NewVNInfo); |
Evan Cheng | 1a66f0a | 2007-08-28 08:28:51 +0000 | [diff] [blame] | 904 | Swapped = false; |
| 905 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 906 | return true; |
| 907 | } |
| 908 | |
| 909 | namespace { |
| 910 | // DepthMBBCompare - Comparison predicate that sort first based on the loop |
| 911 | // depth of the basic block (the unsigned), and then on the MBB number. |
| 912 | struct DepthMBBCompare { |
| 913 | typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair; |
| 914 | bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const { |
| 915 | if (LHS.first > RHS.first) return true; // Deeper loops first |
| 916 | return LHS.first == RHS.first && |
| 917 | LHS.second->getNumber() < RHS.second->getNumber(); |
| 918 | } |
| 919 | }; |
| 920 | } |
| 921 | |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 922 | void SimpleRegisterCoalescing::CopyCoalesceInMBB(MachineBasicBlock *MBB, |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 923 | std::vector<CopyRec> *TryAgain, bool PhysOnly) { |
| 924 | DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n"; |
| 925 | |
| 926 | for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end(); |
| 927 | MII != E;) { |
| 928 | MachineInstr *Inst = MII++; |
| 929 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 930 | // 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] | 931 | unsigned SrcReg, DstReg; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 932 | if (Inst->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) { |
| 933 | DstReg = Inst->getOperand(0).getReg(); |
| 934 | SrcReg = Inst->getOperand(1).getReg(); |
| 935 | } else if (!tii_->isMoveInstr(*Inst, SrcReg, DstReg)) |
| 936 | continue; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 937 | |
Evan Cheng | be8db24 | 2007-10-10 00:11:40 +0000 | [diff] [blame] | 938 | bool Done = JoinCopy(Inst, SrcReg, DstReg, PhysOnly); |
| 939 | if (TryAgain && !Done) |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 940 | TryAgain->push_back(getCopyRec(Inst, SrcReg, DstReg)); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | void SimpleRegisterCoalescing::joinIntervals() { |
| 945 | DOUT << "********** JOINING INTERVALS ***********\n"; |
| 946 | |
| 947 | JoinedLIs.resize(li_->getNumIntervals()); |
| 948 | JoinedLIs.reset(); |
| 949 | |
| 950 | std::vector<CopyRec> TryAgainList; |
| 951 | const LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 952 | if (LI.begin() == LI.end()) { |
| 953 | // If there are no loops in the function, join intervals in function order. |
| 954 | for (MachineFunction::iterator I = mf_->begin(), E = mf_->end(); |
| 955 | I != E; ++I) |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 956 | CopyCoalesceInMBB(I, &TryAgainList); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 957 | } else { |
| 958 | // Otherwise, join intervals in inner loops before other intervals. |
| 959 | // Unfortunately we can't just iterate over loop hierarchy here because |
| 960 | // there may be more MBB's than BB's. Collect MBB's for sorting. |
| 961 | |
| 962 | // Join intervals in the function prolog first. We want to join physical |
| 963 | // registers with virtual registers before the intervals got too long. |
| 964 | std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs; |
| 965 | for (MachineFunction::iterator I = mf_->begin(), E = mf_->end(); I != E;++I) |
| 966 | MBBs.push_back(std::make_pair(LI.getLoopDepth(I->getBasicBlock()), I)); |
| 967 | |
| 968 | // Sort by loop depth. |
| 969 | std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare()); |
| 970 | |
| 971 | // Finally, join intervals in loop nest order. |
| 972 | for (unsigned i = 0, e = MBBs.size(); i != e; ++i) |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 973 | CopyCoalesceInMBB(MBBs[i].second, NULL, true); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 974 | for (unsigned i = 0, e = MBBs.size(); i != e; ++i) |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 975 | CopyCoalesceInMBB(MBBs[i].second, &TryAgainList, false); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | // Joining intervals can allow other intervals to be joined. Iteratively join |
| 979 | // until we make no progress. |
| 980 | bool ProgressMade = true; |
| 981 | while (ProgressMade) { |
| 982 | ProgressMade = false; |
| 983 | |
| 984 | for (unsigned i = 0, e = TryAgainList.size(); i != e; ++i) { |
| 985 | CopyRec &TheCopy = TryAgainList[i]; |
| 986 | if (TheCopy.MI && |
| 987 | JoinCopy(TheCopy.MI, TheCopy.SrcReg, TheCopy.DstReg)) { |
| 988 | TheCopy.MI = 0; // Mark this one as done. |
| 989 | ProgressMade = true; |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | // Some live range has been lengthened due to colaescing, eliminate the |
| 995 | // unnecessary kills. |
| 996 | int RegNum = JoinedLIs.find_first(); |
| 997 | while (RegNum != -1) { |
| 998 | unsigned Reg = RegNum + MRegisterInfo::FirstVirtualRegister; |
| 999 | unsigned repReg = rep(Reg); |
| 1000 | LiveInterval &LI = li_->getInterval(repReg); |
| 1001 | LiveVariables::VarInfo& svi = lv_->getVarInfo(Reg); |
| 1002 | for (unsigned i = 0, e = svi.Kills.size(); i != e; ++i) { |
| 1003 | MachineInstr *Kill = svi.Kills[i]; |
| 1004 | // Suppose vr1 = op vr2, x |
| 1005 | // and vr1 and vr2 are coalesced. vr2 should still be marked kill |
| 1006 | // unless it is a two-address operand. |
| 1007 | if (li_->isRemoved(Kill) || hasRegisterDef(Kill, repReg)) |
| 1008 | continue; |
| 1009 | if (LI.liveAt(li_->getInstructionIndex(Kill) + InstrSlots::NUM)) |
| 1010 | unsetRegisterKill(Kill, repReg); |
| 1011 | } |
| 1012 | RegNum = JoinedLIs.find_next(RegNum); |
| 1013 | } |
| 1014 | |
| 1015 | DOUT << "*** Register mapping ***\n"; |
| 1016 | for (int i = 0, e = r2rMap_.size(); i != e; ++i) |
| 1017 | if (r2rMap_[i]) { |
| 1018 | DOUT << " reg " << i << " -> "; |
| 1019 | DEBUG(printRegName(r2rMap_[i])); |
| 1020 | DOUT << "\n"; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /// Return true if the two specified registers belong to different register |
| 1025 | /// classes. The registers may be either phys or virt regs. |
| 1026 | bool SimpleRegisterCoalescing::differingRegisterClasses(unsigned RegA, |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 1027 | unsigned RegB) const { |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1028 | |
| 1029 | // Get the register classes for the first reg. |
| 1030 | if (MRegisterInfo::isPhysicalRegister(RegA)) { |
| 1031 | assert(MRegisterInfo::isVirtualRegister(RegB) && |
| 1032 | "Shouldn't consider two physregs!"); |
| 1033 | return !mf_->getSSARegMap()->getRegClass(RegB)->contains(RegA); |
| 1034 | } |
| 1035 | |
| 1036 | // Compare against the regclass for the second reg. |
| 1037 | const TargetRegisterClass *RegClass = mf_->getSSARegMap()->getRegClass(RegA); |
| 1038 | if (MRegisterInfo::isVirtualRegister(RegB)) |
| 1039 | return RegClass != mf_->getSSARegMap()->getRegClass(RegB); |
| 1040 | else |
| 1041 | return !RegClass->contains(RegB); |
| 1042 | } |
| 1043 | |
| 1044 | /// lastRegisterUse - Returns the last use of the specific register between |
| 1045 | /// cycles Start and End. It also returns the use operand by reference. It |
| 1046 | /// returns NULL if there are no uses. |
| 1047 | MachineInstr * |
| 1048 | SimpleRegisterCoalescing::lastRegisterUse(unsigned Start, unsigned End, unsigned Reg, |
| 1049 | MachineOperand *&MOU) { |
| 1050 | int e = (End-1) / InstrSlots::NUM * InstrSlots::NUM; |
| 1051 | int s = Start; |
| 1052 | while (e >= s) { |
| 1053 | // Skip deleted instructions |
| 1054 | MachineInstr *MI = li_->getInstructionFromIndex(e); |
| 1055 | while ((e - InstrSlots::NUM) >= s && !MI) { |
| 1056 | e -= InstrSlots::NUM; |
| 1057 | MI = li_->getInstructionFromIndex(e); |
| 1058 | } |
| 1059 | if (e < s || MI == NULL) |
| 1060 | return NULL; |
| 1061 | |
| 1062 | for (unsigned i = 0, NumOps = MI->getNumOperands(); i != NumOps; ++i) { |
| 1063 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 1064 | if (MO.isRegister() && MO.isUse() && MO.getReg() && |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1065 | mri_->regsOverlap(rep(MO.getReg()), Reg)) { |
| 1066 | MOU = &MO; |
| 1067 | return MI; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | e -= InstrSlots::NUM; |
| 1072 | } |
| 1073 | |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | /// findDefOperand - Returns the MachineOperand that is a def of the specific |
| 1079 | /// register. It returns NULL if the def is not found. |
| 1080 | MachineOperand *SimpleRegisterCoalescing::findDefOperand(MachineInstr *MI, unsigned Reg) { |
| 1081 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1082 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 1083 | if (MO.isRegister() && MO.isDef() && |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1084 | mri_->regsOverlap(rep(MO.getReg()), Reg)) |
| 1085 | return &MO; |
| 1086 | } |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | |
| 1090 | /// unsetRegisterKill - Unset IsKill property of all uses of specific register |
| 1091 | /// of the specific instruction. |
| 1092 | void SimpleRegisterCoalescing::unsetRegisterKill(MachineInstr *MI, unsigned Reg) { |
| 1093 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1094 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 1095 | if (MO.isRegister() && MO.isKill() && MO.getReg() && |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1096 | mri_->regsOverlap(rep(MO.getReg()), Reg)) |
| 1097 | MO.unsetIsKill(); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | /// unsetRegisterKills - Unset IsKill property of all uses of specific register |
| 1102 | /// between cycles Start and End. |
| 1103 | void SimpleRegisterCoalescing::unsetRegisterKills(unsigned Start, unsigned End, |
| 1104 | unsigned Reg) { |
| 1105 | int e = (End-1) / InstrSlots::NUM * InstrSlots::NUM; |
| 1106 | int s = Start; |
| 1107 | while (e >= s) { |
| 1108 | // Skip deleted instructions |
| 1109 | MachineInstr *MI = li_->getInstructionFromIndex(e); |
| 1110 | while ((e - InstrSlots::NUM) >= s && !MI) { |
| 1111 | e -= InstrSlots::NUM; |
| 1112 | MI = li_->getInstructionFromIndex(e); |
| 1113 | } |
| 1114 | if (e < s || MI == NULL) |
| 1115 | return; |
| 1116 | |
| 1117 | for (unsigned i = 0, NumOps = MI->getNumOperands(); i != NumOps; ++i) { |
| 1118 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 1119 | if (MO.isRegister() && MO.isKill() && MO.getReg() && |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1120 | mri_->regsOverlap(rep(MO.getReg()), Reg)) { |
| 1121 | MO.unsetIsKill(); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | e -= InstrSlots::NUM; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | /// hasRegisterDef - True if the instruction defines the specific register. |
| 1130 | /// |
| 1131 | bool SimpleRegisterCoalescing::hasRegisterDef(MachineInstr *MI, unsigned Reg) { |
| 1132 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1133 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 1134 | if (MO.isRegister() && MO.isDef() && |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1135 | mri_->regsOverlap(rep(MO.getReg()), Reg)) |
| 1136 | return true; |
| 1137 | } |
| 1138 | return false; |
| 1139 | } |
| 1140 | |
| 1141 | void SimpleRegisterCoalescing::printRegName(unsigned reg) const { |
| 1142 | if (MRegisterInfo::isPhysicalRegister(reg)) |
| 1143 | cerr << mri_->getName(reg); |
| 1144 | else |
| 1145 | cerr << "%reg" << reg; |
| 1146 | } |
| 1147 | |
| 1148 | void SimpleRegisterCoalescing::releaseMemory() { |
| 1149 | r2rMap_.clear(); |
| 1150 | JoinedLIs.clear(); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 1151 | SubRegIdxes.clear(); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | static bool isZeroLengthInterval(LiveInterval *li) { |
| 1155 | for (LiveInterval::Ranges::const_iterator |
| 1156 | i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i) |
| 1157 | if (i->end - i->start > LiveIntervals::InstrSlots::NUM) |
| 1158 | return false; |
| 1159 | return true; |
| 1160 | } |
| 1161 | |
| 1162 | bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) { |
| 1163 | mf_ = &fn; |
| 1164 | tm_ = &fn.getTarget(); |
| 1165 | mri_ = tm_->getRegisterInfo(); |
| 1166 | tii_ = tm_->getInstrInfo(); |
| 1167 | li_ = &getAnalysis<LiveIntervals>(); |
| 1168 | lv_ = &getAnalysis<LiveVariables>(); |
| 1169 | |
| 1170 | DOUT << "********** SIMPLE REGISTER COALESCING **********\n" |
| 1171 | << "********** Function: " |
| 1172 | << ((Value*)mf_->getFunction())->getName() << '\n'; |
| 1173 | |
| 1174 | allocatableRegs_ = mri_->getAllocatableSet(fn); |
| 1175 | for (MRegisterInfo::regclass_iterator I = mri_->regclass_begin(), |
| 1176 | E = mri_->regclass_end(); I != E; ++I) |
| 1177 | allocatableRCRegs_.insert(std::make_pair(*I,mri_->getAllocatableSet(fn, *I))); |
| 1178 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 1179 | SSARegMap *RegMap = mf_->getSSARegMap(); |
| 1180 | r2rMap_.grow(RegMap->getLastVirtReg()); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1181 | |
Gabor Greif | e510b3a | 2007-07-09 12:00:59 +0000 | [diff] [blame] | 1182 | // Join (coalesce) intervals if requested. |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1183 | if (EnableJoining) { |
| 1184 | joinIntervals(); |
| 1185 | DOUT << "********** INTERVALS POST JOINING **********\n"; |
| 1186 | for (LiveIntervals::iterator I = li_->begin(), E = li_->end(); I != E; ++I) { |
| 1187 | I->second.print(DOUT, mri_); |
| 1188 | DOUT << "\n"; |
| 1189 | } |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 1190 | |
| 1191 | // Track coalesced sub-registers. |
| 1192 | while (!SubRegIdxes.empty()) { |
| 1193 | std::pair<unsigned, unsigned> RI = SubRegIdxes.back(); |
| 1194 | SubRegIdxes.pop_back(); |
| 1195 | mf_->getSSARegMap()->setIsSubRegister(RI.first, rep(RI.first), RI.second); |
| 1196 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | // perform a final pass over the instructions and compute spill |
| 1200 | // weights, coalesce virtual registers and remove identity moves. |
| 1201 | const LoopInfo &loopInfo = getAnalysis<LoopInfo>(); |
| 1202 | |
| 1203 | for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end(); |
| 1204 | mbbi != mbbe; ++mbbi) { |
| 1205 | MachineBasicBlock* mbb = mbbi; |
| 1206 | unsigned loopDepth = loopInfo.getLoopDepth(mbb->getBasicBlock()); |
| 1207 | |
| 1208 | for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end(); |
| 1209 | mii != mie; ) { |
| 1210 | // if the move will be an identity move delete it |
| 1211 | unsigned srcReg, dstReg, RegRep; |
| 1212 | if (tii_->isMoveInstr(*mii, srcReg, dstReg) && |
| 1213 | (RegRep = rep(srcReg)) == rep(dstReg)) { |
| 1214 | // remove from def list |
| 1215 | LiveInterval &RegInt = li_->getOrCreateInterval(RegRep); |
| 1216 | MachineOperand *MO = mii->findRegisterDefOperand(dstReg); |
| 1217 | // If def of this move instruction is dead, remove its live range from |
| 1218 | // the dstination register's live interval. |
| 1219 | if (MO->isDead()) { |
| 1220 | unsigned MoveIdx = li_->getDefIndex(li_->getInstructionIndex(mii)); |
| 1221 | LiveInterval::iterator MLR = RegInt.FindLiveRangeContaining(MoveIdx); |
| 1222 | RegInt.removeRange(MLR->start, MoveIdx+1); |
| 1223 | if (RegInt.empty()) |
| 1224 | li_->removeInterval(RegRep); |
| 1225 | } |
| 1226 | li_->RemoveMachineInstrFromMaps(mii); |
| 1227 | mii = mbbi->erase(mii); |
| 1228 | ++numPeep; |
| 1229 | } else { |
| 1230 | SmallSet<unsigned, 4> UniqueUses; |
| 1231 | for (unsigned i = 0, e = mii->getNumOperands(); i != e; ++i) { |
| 1232 | const MachineOperand &mop = mii->getOperand(i); |
| 1233 | if (mop.isRegister() && mop.getReg() && |
| 1234 | MRegisterInfo::isVirtualRegister(mop.getReg())) { |
| 1235 | // replace register with representative register |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame^] | 1236 | unsigned OrigReg = mop.getReg(); |
| 1237 | unsigned reg = rep(OrigReg); |
| 1238 | // Don't rewrite if it is a sub-register of a virtual register. |
| 1239 | if (!RegMap->isSubRegister(OrigReg)) |
| 1240 | mii->getOperand(i).setReg(reg); |
| 1241 | else if (MRegisterInfo::isPhysicalRegister(reg)) |
| 1242 | mii->getOperand(i).setReg(mri_->getSubReg(reg, |
| 1243 | RegMap->getSubRegisterIndex(OrigReg))); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1244 | |
| 1245 | // Multiple uses of reg by the same instruction. It should not |
| 1246 | // contribute to spill weight again. |
| 1247 | if (UniqueUses.count(reg) != 0) |
| 1248 | continue; |
| 1249 | LiveInterval &RegInt = li_->getInterval(reg); |
| 1250 | float w = (mop.isUse()+mop.isDef()) * powf(10.0F, (float)loopDepth); |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 1251 | RegInt.weight += w; |
| 1252 | UniqueUses.insert(reg); |
| 1253 | } |
| 1254 | } |
| 1255 | ++mii; |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | for (LiveIntervals::iterator I = li_->begin(), E = li_->end(); I != E; ++I) { |
| 1261 | LiveInterval &LI = I->second; |
| 1262 | if (MRegisterInfo::isVirtualRegister(LI.reg)) { |
| 1263 | // If the live interval length is essentially zero, i.e. in every live |
| 1264 | // range the use follows def immediately, it doesn't make sense to spill |
| 1265 | // it and hope it will be easier to allocate for this li. |
| 1266 | if (isZeroLengthInterval(&LI)) |
| 1267 | LI.weight = HUGE_VALF; |
| 1268 | |
| 1269 | // Slightly prefer live interval that has been assigned a preferred reg. |
| 1270 | if (LI.preference) |
| 1271 | LI.weight *= 1.01F; |
| 1272 | |
| 1273 | // Divide the weight of the interval by its size. This encourages |
| 1274 | // spilling of intervals that are large and have few uses, and |
| 1275 | // discourages spilling of small intervals with many uses. |
| 1276 | LI.weight /= LI.getSize(); |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | DEBUG(dump()); |
| 1281 | return true; |
| 1282 | } |
| 1283 | |
| 1284 | /// print - Implement the dump method. |
| 1285 | void SimpleRegisterCoalescing::print(std::ostream &O, const Module* m) const { |
| 1286 | li_->print(O, m); |
| 1287 | } |
David Greene | 2c17c4d | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 1288 | |
| 1289 | RegisterCoalescer* llvm::createSimpleRegisterCoalescer() { |
| 1290 | return new SimpleRegisterCoalescing(); |
| 1291 | } |
| 1292 | |
| 1293 | // Make sure that anything that uses RegisterCoalescer pulls in this file... |
| 1294 | DEFINING_FILE_FOR(SimpleRegisterCoalescing) |