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