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