1 //===-- SimpleRegisterCoalescing.cpp - Register Coalescing ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a simple register coalescing pass that attempts to
11 // aggressively coalesce every register copy that it can.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "regcoalescing"
16 #include "SimpleRegisterCoalescing.h"
17 #include "VirtRegMap.h"
18 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
19 #include "llvm/Value.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/MachineLoopInfo.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/CodeGen/RegisterCoalescer.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Target/TargetOptions.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"
38 STATISTIC(numJoins
, "Number of interval joins performed");
39 STATISTIC(numCrossRCs
, "Number of cross class joins performed");
40 STATISTIC(numCommutes
, "Number of instruction commuting performed");
41 STATISTIC(numExtends
, "Number of copies extended");
42 STATISTIC(NumReMats
, "Number of instructions re-materialized");
43 STATISTIC(numPeep
, "Number of identity moves eliminated after coalescing");
44 STATISTIC(numAborts
, "Number of times interval joining aborted");
45 STATISTIC(numDeadValNo
, "Number of valno def marked dead");
47 char SimpleRegisterCoalescing::ID
= 0;
49 EnableJoining("join-liveintervals",
50 cl::desc("Coalesce copies (default=true)"),
54 NewHeuristic("new-coalescer-heuristic",
55 cl::desc("Use new coalescer heuristic"),
56 cl::init(false), cl::Hidden
);
59 CrossClassJoin("join-cross-class-copies",
60 cl::desc("Coalesce cross register class copies"),
61 cl::init(false), cl::Hidden
);
63 static RegisterPass
<SimpleRegisterCoalescing
>
64 X("simple-register-coalescing", "Simple Register Coalescing");
66 // Declare that we implement the RegisterCoalescer interface
67 static RegisterAnalysisGroup
<RegisterCoalescer
, true/*The Default*/> V(X
);
69 const PassInfo
*const llvm::SimpleRegisterCoalescingID
= &X
;
71 void SimpleRegisterCoalescing::getAnalysisUsage(AnalysisUsage
&AU
) const {
72 AU
.addRequired
<LiveIntervals
>();
73 AU
.addPreserved
<LiveIntervals
>();
74 AU
.addRequired
<MachineLoopInfo
>();
75 AU
.addPreserved
<MachineLoopInfo
>();
76 AU
.addPreservedID(MachineDominatorsID
);
78 AU
.addPreservedID(StrongPHIEliminationID
);
80 AU
.addPreservedID(PHIEliminationID
);
81 AU
.addPreservedID(TwoAddressInstructionPassID
);
82 MachineFunctionPass::getAnalysisUsage(AU
);
85 /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy with IntA
86 /// being the source and IntB being the dest, thus this defines a value number
87 /// in IntB. If the source value number (in IntA) is defined by a copy from B,
88 /// see if we can merge these two pieces of B into a single value number,
89 /// eliminating a copy. For example:
93 /// B1 = A3 <- this copy
95 /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1
96 /// value number to be replaced with B0 (which simplifies the B liveinterval).
98 /// This returns true if an interval was modified.
100 bool SimpleRegisterCoalescing::AdjustCopiesBackFrom(LiveInterval
&IntA
,
102 MachineInstr
*CopyMI
) {
103 unsigned CopyIdx
= li_
->getDefIndex(li_
->getInstructionIndex(CopyMI
));
105 // BValNo is a value number in B that is defined by a copy from A. 'B3' in
106 // the example above.
107 LiveInterval::iterator BLR
= IntB
.FindLiveRangeContaining(CopyIdx
);
108 assert(BLR
!= IntB
.end() && "Live range not found!");
109 VNInfo
*BValNo
= BLR
->valno
;
111 // Get the location that B is defined at. Two options: either this value has
112 // an unknown definition point or it is defined at CopyIdx. If unknown, we
114 if (!BValNo
->copy
) return false;
115 assert(BValNo
->def
== CopyIdx
&& "Copy doesn't define the value?");
117 // AValNo is the value number in A that defines the copy, A3 in the example.
118 LiveInterval::iterator ALR
= IntA
.FindLiveRangeContaining(CopyIdx
-1);
119 assert(ALR
!= IntA
.end() && "Live range not found!");
120 VNInfo
*AValNo
= ALR
->valno
;
121 // If it's re-defined by an early clobber somewhere in the live range, then
122 // it's not safe to eliminate the copy. FIXME: This is a temporary workaround.
124 // 172 %ECX<def> = MOV32rr %reg1039<kill>
125 // 180 INLINEASM <es:subl $5,$1
126 // sbbl $3,$0>, 10, %EAX<def>, 14, %ECX<earlyclobber,def>, 9, %EAX<kill>,
127 // 36, <fi#0>, 1, %reg0, 0, 9, %ECX<kill>, 36, <fi#1>, 1, %reg0, 0
128 // 188 %EAX<def> = MOV32rr %EAX<kill>
129 // 196 %ECX<def> = MOV32rr %ECX<kill>
130 // 204 %ECX<def> = MOV32rr %ECX<kill>
131 // 212 %EAX<def> = MOV32rr %EAX<kill>
132 // 220 %EAX<def> = MOV32rr %EAX
133 // 228 %reg1039<def> = MOV32rr %ECX<kill>
134 // The early clobber operand ties ECX input to the ECX def.
136 // The live interval of ECX is represented as this:
137 // %reg20,inf = [46,47:1)[174,230:0) 0@174-(230) 1@46-(47)
138 // The coalescer has no idea there was a def in the middle of [174,230].
139 if (AValNo
->redefByEC
)
142 // If AValNo is defined as a copy from IntB, we can potentially process this.
143 // Get the instruction that defines this value number.
144 unsigned SrcReg
= li_
->getVNInfoSourceReg(AValNo
);
145 if (!SrcReg
) return false; // Not defined by a copy.
147 // If the value number is not defined by a copy instruction, ignore it.
149 // If the source register comes from an interval other than IntB, we can't
151 if (SrcReg
!= IntB
.reg
) return false;
153 // Get the LiveRange in IntB that this value number starts with.
154 LiveInterval::iterator ValLR
= IntB
.FindLiveRangeContaining(AValNo
->def
-1);
155 assert(ValLR
!= IntB
.end() && "Live range not found!");
157 // Make sure that the end of the live range is inside the same block as
159 MachineInstr
*ValLREndInst
= li_
->getInstructionFromIndex(ValLR
->end
-1);
161 ValLREndInst
->getParent() != CopyMI
->getParent()) return false;
163 // Okay, we now know that ValLR ends in the same block that the CopyMI
164 // live-range starts. If there are no intervening live ranges between them in
165 // IntB, we can merge them.
166 if (ValLR
+1 != BLR
) return false;
168 // If a live interval is a physical register, conservatively check if any
169 // of its sub-registers is overlapping the live interval of the virtual
170 // register. If so, do not coalesce.
171 if (TargetRegisterInfo::isPhysicalRegister(IntB
.reg
) &&
172 *tri_
->getSubRegisters(IntB
.reg
)) {
173 for (const unsigned* SR
= tri_
->getSubRegisters(IntB
.reg
); *SR
; ++SR
)
174 if (li_
->hasInterval(*SR
) && IntA
.overlaps(li_
->getInterval(*SR
))) {
175 DOUT
<< "Interfere with sub-register ";
176 DEBUG(li_
->getInterval(*SR
).print(DOUT
, tri_
));
181 DOUT
<< "\nExtending: "; IntB
.print(DOUT
, tri_
);
183 unsigned FillerStart
= ValLR
->end
, FillerEnd
= BLR
->start
;
184 // We are about to delete CopyMI, so need to remove it as the 'instruction
185 // that defines this value #'. Update the the valnum with the new defining
187 BValNo
->def
= FillerStart
;
190 // Okay, we can merge them. We need to insert a new liverange:
191 // [ValLR.end, BLR.begin) of either value number, then we merge the
192 // two value numbers.
193 IntB
.addRange(LiveRange(FillerStart
, FillerEnd
, BValNo
));
195 // If the IntB live range is assigned to a physical register, and if that
196 // physreg has sub-registers, update their live intervals as well.
197 if (TargetRegisterInfo::isPhysicalRegister(IntB
.reg
)) {
198 for (const unsigned *SR
= tri_
->getSubRegisters(IntB
.reg
); *SR
; ++SR
) {
199 LiveInterval
&SRLI
= li_
->getInterval(*SR
);
200 SRLI
.addRange(LiveRange(FillerStart
, FillerEnd
,
201 SRLI
.getNextValue(FillerStart
, 0, li_
->getVNInfoAllocator())));
205 // Okay, merge "B1" into the same value number as "B0".
206 if (BValNo
!= ValLR
->valno
) {
207 IntB
.addKills(ValLR
->valno
, BValNo
->kills
);
208 IntB
.MergeValueNumberInto(BValNo
, ValLR
->valno
);
210 DOUT
<< " result = "; IntB
.print(DOUT
, tri_
);
213 // If the source instruction was killing the source register before the
214 // merge, unset the isKill marker given the live range has been extended.
215 int UIdx
= ValLREndInst
->findRegisterUseOperandIdx(IntB
.reg
, true);
217 ValLREndInst
->getOperand(UIdx
).setIsKill(false);
218 IntB
.removeKill(ValLR
->valno
, FillerStart
);
225 /// HasOtherReachingDefs - Return true if there are definitions of IntB
226 /// other than BValNo val# that can reach uses of AValno val# of IntA.
227 bool SimpleRegisterCoalescing::HasOtherReachingDefs(LiveInterval
&IntA
,
231 for (LiveInterval::iterator AI
= IntA
.begin(), AE
= IntA
.end();
233 if (AI
->valno
!= AValNo
) continue;
234 LiveInterval::Ranges::iterator BI
=
235 std::upper_bound(IntB
.ranges
.begin(), IntB
.ranges
.end(), AI
->start
);
236 if (BI
!= IntB
.ranges
.begin())
238 for (; BI
!= IntB
.ranges
.end() && AI
->end
>= BI
->start
; ++BI
) {
239 if (BI
->valno
== BValNo
)
241 if (BI
->start
<= AI
->start
&& BI
->end
> AI
->start
)
243 if (BI
->start
> AI
->start
&& BI
->start
< AI
->end
)
250 /// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy with IntA
251 /// being the source and IntB being the dest, thus this defines a value number
252 /// in IntB. If the source value number (in IntA) is defined by a commutable
253 /// instruction and its other operand is coalesced to the copy dest register,
254 /// see if we can transform the copy into a noop by commuting the definition. For
257 /// A3 = op A2 B0<kill>
259 /// B1 = A3 <- this copy
261 /// = op A3 <- more uses
265 /// B2 = op B0 A2<kill>
267 /// B1 = B2 <- now an identify copy
269 /// = op B2 <- more uses
271 /// This returns true if an interval was modified.
273 bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(LiveInterval
&IntA
,
275 MachineInstr
*CopyMI
) {
276 unsigned CopyIdx
= li_
->getDefIndex(li_
->getInstructionIndex(CopyMI
));
278 // FIXME: For now, only eliminate the copy by commuting its def when the
279 // source register is a virtual register. We want to guard against cases
280 // where the copy is a back edge copy and commuting the def lengthen the
281 // live interval of the source register to the entire loop.
282 if (TargetRegisterInfo::isPhysicalRegister(IntA
.reg
))
285 // BValNo is a value number in B that is defined by a copy from A. 'B3' in
286 // the example above.
287 LiveInterval::iterator BLR
= IntB
.FindLiveRangeContaining(CopyIdx
);
288 assert(BLR
!= IntB
.end() && "Live range not found!");
289 VNInfo
*BValNo
= BLR
->valno
;
291 // Get the location that B is defined at. Two options: either this value has
292 // an unknown definition point or it is defined at CopyIdx. If unknown, we
294 if (!BValNo
->copy
) return false;
295 assert(BValNo
->def
== CopyIdx
&& "Copy doesn't define the value?");
297 // AValNo is the value number in A that defines the copy, A3 in the example.
298 LiveInterval::iterator ALR
= IntA
.FindLiveRangeContaining(CopyIdx
-1);
299 assert(ALR
!= IntA
.end() && "Live range not found!");
300 VNInfo
*AValNo
= ALR
->valno
;
301 // If other defs can reach uses of this def, then it's not safe to perform
303 if (AValNo
->def
== ~0U || AValNo
->def
== ~1U || AValNo
->hasPHIKill
)
305 MachineInstr
*DefMI
= li_
->getInstructionFromIndex(AValNo
->def
);
306 const TargetInstrDesc
&TID
= DefMI
->getDesc();
308 if (!TID
.isCommutable() ||
309 !tii_
->CommuteChangesDestination(DefMI
, NewDstIdx
))
312 MachineOperand
&NewDstMO
= DefMI
->getOperand(NewDstIdx
);
313 unsigned NewReg
= NewDstMO
.getReg();
314 if (NewReg
!= IntB
.reg
|| !NewDstMO
.isKill())
317 // Make sure there are no other definitions of IntB that would reach the
318 // uses which the new definition can reach.
319 if (HasOtherReachingDefs(IntA
, IntB
, AValNo
, BValNo
))
322 // If some of the uses of IntA.reg is already coalesced away, return false.
323 // It's not possible to determine whether it's safe to perform the coalescing.
324 for (MachineRegisterInfo::use_iterator UI
= mri_
->use_begin(IntA
.reg
),
325 UE
= mri_
->use_end(); UI
!= UE
; ++UI
) {
326 MachineInstr
*UseMI
= &*UI
;
327 unsigned UseIdx
= li_
->getInstructionIndex(UseMI
);
328 LiveInterval::iterator ULR
= IntA
.FindLiveRangeContaining(UseIdx
);
329 if (ULR
== IntA
.end())
331 if (ULR
->valno
== AValNo
&& JoinedCopies
.count(UseMI
))
335 // At this point we have decided that it is legal to do this
336 // transformation. Start by commuting the instruction.
337 MachineBasicBlock
*MBB
= DefMI
->getParent();
338 MachineInstr
*NewMI
= tii_
->commuteInstruction(DefMI
);
341 if (NewMI
!= DefMI
) {
342 li_
->ReplaceMachineInstrInMaps(DefMI
, NewMI
);
343 MBB
->insert(DefMI
, NewMI
);
346 unsigned OpIdx
= NewMI
->findRegisterUseOperandIdx(IntA
.reg
, false);
347 NewMI
->getOperand(OpIdx
).setIsKill();
349 bool BHasPHIKill
= BValNo
->hasPHIKill
;
350 SmallVector
<VNInfo
*, 4> BDeadValNos
;
351 SmallVector
<unsigned, 4> BKills
;
352 std::map
<unsigned, unsigned> BExtend
;
354 // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g.
363 // then do not add kills of A to the newly created B interval.
364 bool Extended
= BLR
->end
> ALR
->end
&& ALR
->end
!= ALR
->start
;
366 BExtend
[ALR
->end
] = BLR
->end
;
368 // Update uses of IntA of the specific Val# with IntB.
369 bool BHasSubRegs
= false;
370 if (TargetRegisterInfo::isPhysicalRegister(IntB
.reg
))
371 BHasSubRegs
= *tri_
->getSubRegisters(IntB
.reg
);
372 for (MachineRegisterInfo::use_iterator UI
= mri_
->use_begin(IntA
.reg
),
373 UE
= mri_
->use_end(); UI
!= UE
;) {
374 MachineOperand
&UseMO
= UI
.getOperand();
375 MachineInstr
*UseMI
= &*UI
;
377 if (JoinedCopies
.count(UseMI
))
379 unsigned UseIdx
= li_
->getInstructionIndex(UseMI
);
380 LiveInterval::iterator ULR
= IntA
.FindLiveRangeContaining(UseIdx
);
381 if (ULR
== IntA
.end() || ULR
->valno
!= AValNo
)
383 UseMO
.setReg(NewReg
);
386 if (UseMO
.isKill()) {
388 UseMO
.setIsKill(false);
390 BKills
.push_back(li_
->getUseIndex(UseIdx
)+1);
392 unsigned SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
;
393 if (!tii_
->isMoveInstr(*UseMI
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
))
395 if (DstReg
== IntB
.reg
) {
396 // This copy will become a noop. If it's defining a new val#,
397 // remove that val# as well. However this live range is being
398 // extended to the end of the existing live range defined by the copy.
399 unsigned DefIdx
= li_
->getDefIndex(UseIdx
);
400 const LiveRange
*DLR
= IntB
.getLiveRangeContaining(DefIdx
);
401 BHasPHIKill
|= DLR
->valno
->hasPHIKill
;
402 assert(DLR
->valno
->def
== DefIdx
);
403 BDeadValNos
.push_back(DLR
->valno
);
404 BExtend
[DLR
->start
] = DLR
->end
;
405 JoinedCopies
.insert(UseMI
);
406 // If this is a kill but it's going to be removed, the last use
407 // of the same val# is the new kill.
413 // We need to insert a new liverange: [ALR.start, LastUse). It may be we can
414 // simply extend BLR if CopyMI doesn't end the range.
415 DOUT
<< "\nExtending: "; IntB
.print(DOUT
, tri_
);
417 // Remove val#'s defined by copies that will be coalesced away.
418 for (unsigned i
= 0, e
= BDeadValNos
.size(); i
!= e
; ++i
) {
419 VNInfo
*DeadVNI
= BDeadValNos
[i
];
421 for (const unsigned *SR
= tri_
->getSubRegisters(IntB
.reg
); *SR
; ++SR
) {
422 LiveInterval
&SRLI
= li_
->getInterval(*SR
);
423 const LiveRange
*SRLR
= SRLI
.getLiveRangeContaining(DeadVNI
->def
);
424 SRLI
.removeValNo(SRLR
->valno
);
427 IntB
.removeValNo(BDeadValNos
[i
]);
430 // Extend BValNo by merging in IntA live ranges of AValNo. Val# definition
431 // is updated. Kills are also updated.
432 VNInfo
*ValNo
= BValNo
;
433 ValNo
->def
= AValNo
->def
;
435 for (unsigned j
= 0, ee
= ValNo
->kills
.size(); j
!= ee
; ++j
) {
436 unsigned Kill
= ValNo
->kills
[j
];
437 if (Kill
!= BLR
->end
)
438 BKills
.push_back(Kill
);
440 ValNo
->kills
.clear();
441 for (LiveInterval::iterator AI
= IntA
.begin(), AE
= IntA
.end();
443 if (AI
->valno
!= AValNo
) continue;
444 unsigned End
= AI
->end
;
445 std::map
<unsigned, unsigned>::iterator EI
= BExtend
.find(End
);
446 if (EI
!= BExtend
.end())
448 IntB
.addRange(LiveRange(AI
->start
, End
, ValNo
));
450 // If the IntB live range is assigned to a physical register, and if that
451 // physreg has sub-registers, update their live intervals as well.
453 for (const unsigned *SR
= tri_
->getSubRegisters(IntB
.reg
); *SR
; ++SR
) {
454 LiveInterval
&SRLI
= li_
->getInterval(*SR
);
455 SRLI
.MergeInClobberRange(AI
->start
, End
, li_
->getVNInfoAllocator());
459 IntB
.addKills(ValNo
, BKills
);
460 ValNo
->hasPHIKill
= BHasPHIKill
;
462 DOUT
<< " result = "; IntB
.print(DOUT
, tri_
);
465 DOUT
<< "\nShortening: "; IntA
.print(DOUT
, tri_
);
466 IntA
.removeValNo(AValNo
);
467 DOUT
<< " result = "; IntA
.print(DOUT
, tri_
);
474 /// isSameOrFallThroughBB - Return true if MBB == SuccMBB or MBB simply
475 /// fallthoughs to SuccMBB.
476 static bool isSameOrFallThroughBB(MachineBasicBlock
*MBB
,
477 MachineBasicBlock
*SuccMBB
,
478 const TargetInstrInfo
*tii_
) {
481 MachineBasicBlock
*TBB
= 0, *FBB
= 0;
482 SmallVector
<MachineOperand
, 4> Cond
;
483 return !tii_
->AnalyzeBranch(*MBB
, TBB
, FBB
, Cond
) && !TBB
&& !FBB
&&
484 MBB
->isSuccessor(SuccMBB
);
487 /// removeRange - Wrapper for LiveInterval::removeRange. This removes a range
488 /// from a physical register live interval as well as from the live intervals
489 /// of its sub-registers.
490 static void removeRange(LiveInterval
&li
, unsigned Start
, unsigned End
,
491 LiveIntervals
*li_
, const TargetRegisterInfo
*tri_
) {
492 li
.removeRange(Start
, End
, true);
493 if (TargetRegisterInfo::isPhysicalRegister(li
.reg
)) {
494 for (const unsigned* SR
= tri_
->getSubRegisters(li
.reg
); *SR
; ++SR
) {
495 if (!li_
->hasInterval(*SR
))
497 LiveInterval
&sli
= li_
->getInterval(*SR
);
498 unsigned RemoveEnd
= Start
;
499 while (RemoveEnd
!= End
) {
500 LiveInterval::iterator LR
= sli
.FindLiveRangeContaining(Start
);
503 RemoveEnd
= (LR
->end
< End
) ? LR
->end
: End
;
504 sli
.removeRange(Start
, RemoveEnd
, true);
511 /// TrimLiveIntervalToLastUse - If there is a last use in the same basic block
512 /// as the copy instruction, trim the live interval to the last use and return
515 SimpleRegisterCoalescing::TrimLiveIntervalToLastUse(unsigned CopyIdx
,
516 MachineBasicBlock
*CopyMBB
,
518 const LiveRange
*LR
) {
519 unsigned MBBStart
= li_
->getMBBStartIdx(CopyMBB
);
521 MachineOperand
*LastUse
= lastRegisterUse(LR
->start
, CopyIdx
-1, li
.reg
,
524 MachineInstr
*LastUseMI
= LastUse
->getParent();
525 if (!isSameOrFallThroughBB(LastUseMI
->getParent(), CopyMBB
, tii_
)) {
532 // r1025<dead> = r1024<kill>
533 if (MBBStart
< LR
->end
)
534 removeRange(li
, MBBStart
, LR
->end
, li_
, tri_
);
538 // There are uses before the copy, just shorten the live range to the end
540 LastUse
->setIsKill();
541 removeRange(li
, li_
->getDefIndex(LastUseIdx
), LR
->end
, li_
, tri_
);
542 li
.addKill(LR
->valno
, LastUseIdx
+1);
543 unsigned SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
;
544 if (tii_
->isMoveInstr(*LastUseMI
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
) &&
546 // Last use is itself an identity code.
547 int DeadIdx
= LastUseMI
->findRegisterDefOperandIdx(li
.reg
, false, tri_
);
548 LastUseMI
->getOperand(DeadIdx
).setIsDead();
554 if (LR
->start
<= MBBStart
&& LR
->end
> MBBStart
) {
555 if (LR
->start
== 0) {
556 assert(TargetRegisterInfo::isPhysicalRegister(li
.reg
));
557 // Live-in to the function but dead. Remove it from entry live-in set.
558 mf_
->begin()->removeLiveIn(li
.reg
);
560 // FIXME: Shorten intervals in BBs that reaches this BB.
566 /// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
567 /// computation, replace the copy by rematerialize the definition.
568 bool SimpleRegisterCoalescing::ReMaterializeTrivialDef(LiveInterval
&SrcInt
,
570 MachineInstr
*CopyMI
) {
571 unsigned CopyIdx
= li_
->getUseIndex(li_
->getInstructionIndex(CopyMI
));
572 LiveInterval::iterator SrcLR
= SrcInt
.FindLiveRangeContaining(CopyIdx
);
573 assert(SrcLR
!= SrcInt
.end() && "Live range not found!");
574 VNInfo
*ValNo
= SrcLR
->valno
;
575 // If other defs can reach uses of this def, then it's not safe to perform
577 if (ValNo
->def
== ~0U || ValNo
->def
== ~1U || ValNo
->hasPHIKill
)
579 MachineInstr
*DefMI
= li_
->getInstructionFromIndex(ValNo
->def
);
580 const TargetInstrDesc
&TID
= DefMI
->getDesc();
581 if (!TID
.isAsCheapAsAMove())
583 if (!DefMI
->getDesc().isRematerializable() ||
584 !tii_
->isTriviallyReMaterializable(DefMI
))
586 bool SawStore
= false;
587 if (!DefMI
->isSafeToMove(tii_
, SawStore
))
590 unsigned DefIdx
= li_
->getDefIndex(CopyIdx
);
591 const LiveRange
*DLR
= li_
->getInterval(DstReg
).getLiveRangeContaining(DefIdx
);
592 DLR
->valno
->copy
= NULL
;
593 // Don't forget to update sub-register intervals.
594 if (TargetRegisterInfo::isPhysicalRegister(DstReg
)) {
595 for (const unsigned* SR
= tri_
->getSubRegisters(DstReg
); *SR
; ++SR
) {
596 if (!li_
->hasInterval(*SR
))
598 DLR
= li_
->getInterval(*SR
).getLiveRangeContaining(DefIdx
);
599 if (DLR
&& DLR
->valno
->copy
== CopyMI
)
600 DLR
->valno
->copy
= NULL
;
604 // If copy kills the source register, find the last use and propagate
606 MachineBasicBlock
*MBB
= CopyMI
->getParent();
607 if (CopyMI
->killsRegister(SrcInt
.reg
))
608 TrimLiveIntervalToLastUse(CopyIdx
, MBB
, SrcInt
, SrcLR
);
610 MachineBasicBlock::iterator MII
= next(MachineBasicBlock::iterator(CopyMI
));
611 CopyMI
->removeFromParent();
612 tii_
->reMaterialize(*MBB
, MII
, DstReg
, DefMI
);
613 MachineInstr
*NewMI
= prior(MII
);
614 // CopyMI may have implicit operands, transfer them over to the newly
615 // rematerialized instruction. And update implicit def interval valnos.
616 for (unsigned i
= CopyMI
->getDesc().getNumOperands(),
617 e
= CopyMI
->getNumOperands(); i
!= e
; ++i
) {
618 MachineOperand
&MO
= CopyMI
->getOperand(i
);
619 if (MO
.isReg() && MO
.isImplicit())
620 NewMI
->addOperand(MO
);
621 if (MO
.isDef() && li_
->hasInterval(MO
.getReg())) {
622 unsigned Reg
= MO
.getReg();
623 DLR
= li_
->getInterval(Reg
).getLiveRangeContaining(DefIdx
);
624 if (DLR
&& DLR
->valno
->copy
== CopyMI
)
625 DLR
->valno
->copy
= NULL
;
629 li_
->ReplaceMachineInstrInMaps(CopyMI
, NewMI
);
630 MBB
->getParent()->DeleteMachineInstr(CopyMI
);
631 ReMatCopies
.insert(CopyMI
);
632 ReMatDefs
.insert(DefMI
);
637 /// isBackEdgeCopy - Returns true if CopyMI is a back edge copy.
639 bool SimpleRegisterCoalescing::isBackEdgeCopy(MachineInstr
*CopyMI
,
640 unsigned DstReg
) const {
641 MachineBasicBlock
*MBB
= CopyMI
->getParent();
642 const MachineLoop
*L
= loopInfo
->getLoopFor(MBB
);
645 if (MBB
!= L
->getLoopLatch())
648 LiveInterval
&LI
= li_
->getInterval(DstReg
);
649 unsigned DefIdx
= li_
->getInstructionIndex(CopyMI
);
650 LiveInterval::const_iterator DstLR
=
651 LI
.FindLiveRangeContaining(li_
->getDefIndex(DefIdx
));
652 if (DstLR
== LI
.end())
654 unsigned KillIdx
= li_
->getMBBEndIdx(MBB
) + 1;
655 if (DstLR
->valno
->kills
.size() == 1 &&
656 DstLR
->valno
->kills
[0] == KillIdx
&& DstLR
->valno
->hasPHIKill
)
661 /// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
662 /// update the subregister number if it is not zero. If DstReg is a
663 /// physical register and the existing subregister number of the def / use
664 /// being updated is not zero, make sure to set it to the correct physical
667 SimpleRegisterCoalescing::UpdateRegDefsUses(unsigned SrcReg
, unsigned DstReg
,
669 bool DstIsPhys
= TargetRegisterInfo::isPhysicalRegister(DstReg
);
670 if (DstIsPhys
&& SubIdx
) {
671 // Figure out the real physical register we are updating with.
672 DstReg
= tri_
->getSubReg(DstReg
, SubIdx
);
676 for (MachineRegisterInfo::reg_iterator I
= mri_
->reg_begin(SrcReg
),
677 E
= mri_
->reg_end(); I
!= E
; ) {
678 MachineOperand
&O
= I
.getOperand();
679 MachineInstr
*UseMI
= &*I
;
681 unsigned OldSubIdx
= O
.getSubReg();
683 unsigned UseDstReg
= DstReg
;
685 UseDstReg
= tri_
->getSubReg(DstReg
, OldSubIdx
);
687 unsigned CopySrcReg
, CopyDstReg
, CopySrcSubIdx
, CopyDstSubIdx
;
688 if (tii_
->isMoveInstr(*UseMI
, CopySrcReg
, CopyDstReg
,
689 CopySrcSubIdx
, CopyDstSubIdx
) &&
690 CopySrcReg
!= CopyDstReg
&&
691 CopySrcReg
== SrcReg
&& CopyDstReg
!= UseDstReg
) {
692 // If the use is a copy and it won't be coalesced away, and its source
693 // is defined by a trivial computation, try to rematerialize it instead.
694 if (ReMaterializeTrivialDef(li_
->getInterval(SrcReg
), CopyDstReg
,UseMI
))
703 // Sub-register indexes goes from small to large. e.g.
704 // RAX: 1 -> AL, 2 -> AX, 3 -> EAX
705 // EAX: 1 -> AL, 2 -> AX
706 // So RAX's sub-register 2 is AX, RAX's sub-regsiter 3 is EAX, whose
707 // sub-register 2 is also AX.
708 if (SubIdx
&& OldSubIdx
&& SubIdx
!= OldSubIdx
)
709 assert(OldSubIdx
< SubIdx
&& "Conflicting sub-register index!");
712 // Remove would-be duplicated kill marker.
713 if (O
.isKill() && UseMI
->killsRegister(DstReg
))
717 // After updating the operand, check if the machine instruction has
718 // become a copy. If so, update its val# information.
719 const TargetInstrDesc
&TID
= UseMI
->getDesc();
720 unsigned CopySrcReg
, CopyDstReg
, CopySrcSubIdx
, CopyDstSubIdx
;
721 if (TID
.getNumDefs() == 1 && TID
.getNumOperands() > 2 &&
722 tii_
->isMoveInstr(*UseMI
, CopySrcReg
, CopyDstReg
,
723 CopySrcSubIdx
, CopyDstSubIdx
) &&
724 CopySrcReg
!= CopyDstReg
&&
725 (TargetRegisterInfo::isVirtualRegister(CopyDstReg
) ||
726 allocatableRegs_
[CopyDstReg
])) {
727 LiveInterval
&LI
= li_
->getInterval(CopyDstReg
);
728 unsigned DefIdx
= li_
->getDefIndex(li_
->getInstructionIndex(UseMI
));
729 const LiveRange
*DLR
= LI
.getLiveRangeContaining(DefIdx
);
730 if (DLR
->valno
->def
== DefIdx
)
731 DLR
->valno
->copy
= UseMI
;
736 /// RemoveDeadImpDef - Remove implicit_def instructions which are "re-defining"
737 /// registers due to insert_subreg coalescing. e.g.
739 /// r1025 = implicit_def
740 /// r1025 = insert_subreg r1025, r1024
744 /// r1025 = implicit_def
745 /// r1025 = insert_subreg r1025, r1025
748 SimpleRegisterCoalescing::RemoveDeadImpDef(unsigned Reg
, LiveInterval
&LI
) {
749 for (MachineRegisterInfo::reg_iterator I
= mri_
->reg_begin(Reg
),
750 E
= mri_
->reg_end(); I
!= E
; ) {
751 MachineOperand
&O
= I
.getOperand();
752 MachineInstr
*DefMI
= &*I
;
756 if (DefMI
->getOpcode() != TargetInstrInfo::IMPLICIT_DEF
)
758 if (!LI
.liveBeforeAndAt(li_
->getInstructionIndex(DefMI
)))
760 li_
->RemoveMachineInstrFromMaps(DefMI
);
761 DefMI
->eraseFromParent();
765 /// RemoveUnnecessaryKills - Remove kill markers that are no longer accurate
766 /// due to live range lengthening as the result of coalescing.
767 void SimpleRegisterCoalescing::RemoveUnnecessaryKills(unsigned Reg
,
769 for (MachineRegisterInfo::use_iterator UI
= mri_
->use_begin(Reg
),
770 UE
= mri_
->use_end(); UI
!= UE
; ++UI
) {
771 MachineOperand
&UseMO
= UI
.getOperand();
772 if (UseMO
.isKill()) {
773 MachineInstr
*UseMI
= UseMO
.getParent();
774 unsigned UseIdx
= li_
->getUseIndex(li_
->getInstructionIndex(UseMI
));
775 const LiveRange
*UI
= LI
.getLiveRangeContaining(UseIdx
);
776 if (!UI
|| !LI
.isKill(UI
->valno
, UseIdx
+1))
777 UseMO
.setIsKill(false);
782 /// removeIntervalIfEmpty - Check if the live interval of a physical register
783 /// is empty, if so remove it and also remove the empty intervals of its
784 /// sub-registers. Return true if live interval is removed.
785 static bool removeIntervalIfEmpty(LiveInterval
&li
, LiveIntervals
*li_
,
786 const TargetRegisterInfo
*tri_
) {
788 if (TargetRegisterInfo::isPhysicalRegister(li
.reg
))
789 for (const unsigned* SR
= tri_
->getSubRegisters(li
.reg
); *SR
; ++SR
) {
790 if (!li_
->hasInterval(*SR
))
792 LiveInterval
&sli
= li_
->getInterval(*SR
);
794 li_
->removeInterval(*SR
);
796 li_
->removeInterval(li
.reg
);
802 /// ShortenDeadCopyLiveRange - Shorten a live range defined by a dead copy.
803 /// Return true if live interval is removed.
804 bool SimpleRegisterCoalescing::ShortenDeadCopyLiveRange(LiveInterval
&li
,
805 MachineInstr
*CopyMI
) {
806 unsigned CopyIdx
= li_
->getInstructionIndex(CopyMI
);
807 LiveInterval::iterator MLR
=
808 li
.FindLiveRangeContaining(li_
->getDefIndex(CopyIdx
));
810 return false; // Already removed by ShortenDeadCopySrcLiveRange.
811 unsigned RemoveStart
= MLR
->start
;
812 unsigned RemoveEnd
= MLR
->end
;
813 // Remove the liverange that's defined by this.
814 if (RemoveEnd
== li_
->getDefIndex(CopyIdx
)+1) {
815 removeRange(li
, RemoveStart
, RemoveEnd
, li_
, tri_
);
816 return removeIntervalIfEmpty(li
, li_
, tri_
);
821 /// RemoveDeadDef - If a def of a live interval is now determined dead, remove
822 /// the val# it defines. If the live interval becomes empty, remove it as well.
823 bool SimpleRegisterCoalescing::RemoveDeadDef(LiveInterval
&li
,
824 MachineInstr
*DefMI
) {
825 unsigned DefIdx
= li_
->getDefIndex(li_
->getInstructionIndex(DefMI
));
826 LiveInterval::iterator MLR
= li
.FindLiveRangeContaining(DefIdx
);
827 if (DefIdx
!= MLR
->valno
->def
)
829 li
.removeValNo(MLR
->valno
);
830 return removeIntervalIfEmpty(li
, li_
, tri_
);
833 /// PropagateDeadness - Propagate the dead marker to the instruction which
834 /// defines the val#.
835 static void PropagateDeadness(LiveInterval
&li
, MachineInstr
*CopyMI
,
836 unsigned &LRStart
, LiveIntervals
*li_
,
837 const TargetRegisterInfo
* tri_
) {
838 MachineInstr
*DefMI
=
839 li_
->getInstructionFromIndex(li_
->getDefIndex(LRStart
));
840 if (DefMI
&& DefMI
!= CopyMI
) {
841 int DeadIdx
= DefMI
->findRegisterDefOperandIdx(li
.reg
, false, tri_
);
843 DefMI
->getOperand(DeadIdx
).setIsDead();
844 // A dead def should have a single cycle interval.
850 /// ShortenDeadCopySrcLiveRange - Shorten a live range as it's artificially
851 /// extended by a dead copy. Mark the last use (if any) of the val# as kill as
852 /// ends the live range there. If there isn't another use, then this live range
853 /// is dead. Return true if live interval is removed.
855 SimpleRegisterCoalescing::ShortenDeadCopySrcLiveRange(LiveInterval
&li
,
856 MachineInstr
*CopyMI
) {
857 unsigned CopyIdx
= li_
->getInstructionIndex(CopyMI
);
859 // FIXME: special case: function live in. It can be a general case if the
860 // first instruction index starts at > 0 value.
861 assert(TargetRegisterInfo::isPhysicalRegister(li
.reg
));
862 // Live-in to the function but dead. Remove it from entry live-in set.
863 if (mf_
->begin()->isLiveIn(li
.reg
))
864 mf_
->begin()->removeLiveIn(li
.reg
);
865 const LiveRange
*LR
= li
.getLiveRangeContaining(CopyIdx
);
866 removeRange(li
, LR
->start
, LR
->end
, li_
, tri_
);
867 return removeIntervalIfEmpty(li
, li_
, tri_
);
870 LiveInterval::iterator LR
= li
.FindLiveRangeContaining(CopyIdx
-1);
872 // Livein but defined by a phi.
875 unsigned RemoveStart
= LR
->start
;
876 unsigned RemoveEnd
= li_
->getDefIndex(CopyIdx
)+1;
877 if (LR
->end
> RemoveEnd
)
878 // More uses past this copy? Nothing to do.
881 // If there is a last use in the same bb, we can't remove the live range.
882 // Shorten the live interval and return.
883 MachineBasicBlock
*CopyMBB
= CopyMI
->getParent();
884 if (TrimLiveIntervalToLastUse(CopyIdx
, CopyMBB
, li
, LR
))
887 MachineBasicBlock
*StartMBB
= li_
->getMBBFromIndex(RemoveStart
);
888 if (!isSameOrFallThroughBB(StartMBB
, CopyMBB
, tii_
))
889 // If the live range starts in another mbb and the copy mbb is not a fall
890 // through mbb, then we can only cut the range from the beginning of the
892 RemoveStart
= li_
->getMBBStartIdx(CopyMBB
) + 1;
894 if (LR
->valno
->def
== RemoveStart
) {
895 // If the def MI defines the val# and this copy is the only kill of the
896 // val#, then propagate the dead marker.
897 if (li
.isOnlyLROfValNo(LR
)) {
898 PropagateDeadness(li
, CopyMI
, RemoveStart
, li_
, tri_
);
901 if (li
.isKill(LR
->valno
, RemoveEnd
))
902 li
.removeKill(LR
->valno
, RemoveEnd
);
905 removeRange(li
, RemoveStart
, RemoveEnd
, li_
, tri_
);
906 return removeIntervalIfEmpty(li
, li_
, tri_
);
909 /// CanCoalesceWithImpDef - Returns true if the specified copy instruction
910 /// from an implicit def to another register can be coalesced away.
911 bool SimpleRegisterCoalescing::CanCoalesceWithImpDef(MachineInstr
*CopyMI
,
913 LiveInterval
&ImpLi
) const{
914 if (!CopyMI
->killsRegister(ImpLi
.reg
))
916 unsigned CopyIdx
= li_
->getDefIndex(li_
->getInstructionIndex(CopyMI
));
917 LiveInterval::iterator LR
= li
.FindLiveRangeContaining(CopyIdx
);
920 if (LR
->valno
->hasPHIKill
)
922 if (LR
->valno
->def
!= CopyIdx
)
924 // Make sure all of val# uses are copies.
925 for (MachineRegisterInfo::use_iterator UI
= mri_
->use_begin(li
.reg
),
926 UE
= mri_
->use_end(); UI
!= UE
;) {
927 MachineInstr
*UseMI
= &*UI
;
929 if (JoinedCopies
.count(UseMI
))
931 unsigned UseIdx
= li_
->getUseIndex(li_
->getInstructionIndex(UseMI
));
932 LiveInterval::iterator ULR
= li
.FindLiveRangeContaining(UseIdx
);
933 if (ULR
== li
.end() || ULR
->valno
!= LR
->valno
)
935 // If the use is not a use, then it's not safe to coalesce the move.
936 unsigned SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
;
937 if (!tii_
->isMoveInstr(*UseMI
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
)) {
938 if (UseMI
->getOpcode() == TargetInstrInfo::INSERT_SUBREG
&&
939 UseMI
->getOperand(1).getReg() == li
.reg
)
948 /// RemoveCopiesFromValNo - The specified value# is defined by an implicit
949 /// def and it is being removed. Turn all copies from this value# into
950 /// identity copies so they will be removed.
951 void SimpleRegisterCoalescing::RemoveCopiesFromValNo(LiveInterval
&li
,
953 SmallVector
<MachineInstr
*, 4> ImpDefs
;
954 MachineOperand
*LastUse
= NULL
;
955 unsigned LastUseIdx
= li_
->getUseIndex(VNI
->def
);
956 for (MachineRegisterInfo::reg_iterator RI
= mri_
->reg_begin(li
.reg
),
957 RE
= mri_
->reg_end(); RI
!= RE
;) {
958 MachineOperand
*MO
= &RI
.getOperand();
959 MachineInstr
*MI
= &*RI
;
962 if (MI
->getOpcode() == TargetInstrInfo::IMPLICIT_DEF
) {
963 ImpDefs
.push_back(MI
);
967 if (JoinedCopies
.count(MI
))
969 unsigned UseIdx
= li_
->getUseIndex(li_
->getInstructionIndex(MI
));
970 LiveInterval::iterator ULR
= li
.FindLiveRangeContaining(UseIdx
);
971 if (ULR
== li
.end() || ULR
->valno
!= VNI
)
973 // If the use is a copy, turn it into an identity copy.
974 unsigned SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
;
975 if (tii_
->isMoveInstr(*MI
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
) &&
977 // Each use MI may have multiple uses of this register. Change them all.
978 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
979 MachineOperand
&MO
= MI
->getOperand(i
);
980 if (MO
.isReg() && MO
.getReg() == li
.reg
)
983 JoinedCopies
.insert(MI
);
984 } else if (UseIdx
> LastUseIdx
) {
990 LastUse
->setIsKill();
991 li
.addKill(VNI
, LastUseIdx
+1);
993 // Remove dead implicit_def's.
994 while (!ImpDefs
.empty()) {
995 MachineInstr
*ImpDef
= ImpDefs
.back();
997 li_
->RemoveMachineInstrFromMaps(ImpDef
);
998 ImpDef
->eraseFromParent();
1003 /// getMatchingSuperReg - Return a super-register of the specified register
1004 /// Reg so its sub-register of index SubIdx is Reg.
1005 static unsigned getMatchingSuperReg(unsigned Reg
, unsigned SubIdx
,
1006 const TargetRegisterClass
*RC
,
1007 const TargetRegisterInfo
* TRI
) {
1008 for (const unsigned *SRs
= TRI
->getSuperRegisters(Reg
);
1009 unsigned SR
= *SRs
; ++SRs
)
1010 if (Reg
== TRI
->getSubReg(SR
, SubIdx
) && RC
->contains(SR
))
1015 /// isWinToJoinCrossClass - Return true if it's profitable to coalesce
1016 /// two virtual registers from different register classes.
1018 SimpleRegisterCoalescing::isWinToJoinCrossClass(unsigned LargeReg
,
1020 unsigned Threshold
) {
1021 // Then make sure the intervals are *short*.
1022 LiveInterval
&LargeInt
= li_
->getInterval(LargeReg
);
1023 LiveInterval
&SmallInt
= li_
->getInterval(SmallReg
);
1024 unsigned LargeSize
= li_
->getApproximateInstructionCount(LargeInt
);
1025 unsigned SmallSize
= li_
->getApproximateInstructionCount(SmallInt
);
1026 if (SmallSize
> Threshold
|| LargeSize
> Threshold
)
1027 if ((float)std::distance(mri_
->use_begin(SmallReg
),
1028 mri_
->use_end()) / SmallSize
<
1029 (float)std::distance(mri_
->use_begin(LargeReg
),
1030 mri_
->use_end()) / LargeSize
)
1035 /// HasIncompatibleSubRegDefUse - If we are trying to coalesce a virtual
1036 /// register with a physical register, check if any of the virtual register
1037 /// operand is a sub-register use or def. If so, make sure it won't result
1038 /// in an illegal extract_subreg or insert_subreg instruction. e.g.
1039 /// vr1024 = extract_subreg vr1025, 1
1041 /// vr1024 = mov8rr AH
1042 /// If vr1024 is coalesced with AH, the extract_subreg is now illegal since
1043 /// AH does not have a super-reg whose sub-register 1 is AH.
1045 SimpleRegisterCoalescing::HasIncompatibleSubRegDefUse(MachineInstr
*CopyMI
,
1048 for (MachineRegisterInfo::reg_iterator I
= mri_
->reg_begin(VirtReg
),
1049 E
= mri_
->reg_end(); I
!= E
; ++I
) {
1050 MachineOperand
&O
= I
.getOperand();
1051 MachineInstr
*MI
= &*I
;
1052 if (MI
== CopyMI
|| JoinedCopies
.count(MI
))
1054 unsigned SubIdx
= O
.getSubReg();
1055 if (SubIdx
&& !tri_
->getSubReg(PhysReg
, SubIdx
))
1057 if (MI
->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG
) {
1058 SubIdx
= MI
->getOperand(2).getImm();
1059 if (O
.isUse() && !tri_
->getSubReg(PhysReg
, SubIdx
))
1062 unsigned SrcReg
= MI
->getOperand(1).getReg();
1063 const TargetRegisterClass
*RC
=
1064 TargetRegisterInfo::isPhysicalRegister(SrcReg
)
1065 ? tri_
->getPhysicalRegisterRegClass(SrcReg
)
1066 : mri_
->getRegClass(SrcReg
);
1067 if (!getMatchingSuperReg(PhysReg
, SubIdx
, RC
, tri_
))
1071 if (MI
->getOpcode() == TargetInstrInfo::INSERT_SUBREG
||
1072 MI
->getOpcode() == TargetInstrInfo::SUBREG_TO_REG
) {
1073 SubIdx
= MI
->getOperand(3).getImm();
1074 if (VirtReg
== MI
->getOperand(0).getReg()) {
1075 if (!tri_
->getSubReg(PhysReg
, SubIdx
))
1078 unsigned DstReg
= MI
->getOperand(0).getReg();
1079 const TargetRegisterClass
*RC
=
1080 TargetRegisterInfo::isPhysicalRegister(DstReg
)
1081 ? tri_
->getPhysicalRegisterRegClass(DstReg
)
1082 : mri_
->getRegClass(DstReg
);
1083 if (!getMatchingSuperReg(PhysReg
, SubIdx
, RC
, tri_
))
1092 /// CanJoinExtractSubRegToPhysReg - Return true if it's possible to coalesce
1093 /// an extract_subreg where dst is a physical register, e.g.
1094 /// cl = EXTRACT_SUBREG reg1024, 1
1096 SimpleRegisterCoalescing::CanJoinExtractSubRegToPhysReg(unsigned DstReg
,
1097 unsigned SrcReg
, unsigned SubIdx
,
1098 unsigned &RealDstReg
) {
1099 const TargetRegisterClass
*RC
= mri_
->getRegClass(SrcReg
);
1100 RealDstReg
= getMatchingSuperReg(DstReg
, SubIdx
, RC
, tri_
);
1101 assert(RealDstReg
&& "Invalid extract_subreg instruction!");
1103 // For this type of EXTRACT_SUBREG, conservatively
1104 // check if the live interval of the source register interfere with the
1105 // actual super physical register we are trying to coalesce with.
1106 LiveInterval
&RHS
= li_
->getInterval(SrcReg
);
1107 if (li_
->hasInterval(RealDstReg
) &&
1108 RHS
.overlaps(li_
->getInterval(RealDstReg
))) {
1109 DOUT
<< "Interfere with register ";
1110 DEBUG(li_
->getInterval(RealDstReg
).print(DOUT
, tri_
));
1111 return false; // Not coalescable
1113 for (const unsigned* SR
= tri_
->getSubRegisters(RealDstReg
); *SR
; ++SR
)
1114 if (li_
->hasInterval(*SR
) && RHS
.overlaps(li_
->getInterval(*SR
))) {
1115 DOUT
<< "Interfere with sub-register ";
1116 DEBUG(li_
->getInterval(*SR
).print(DOUT
, tri_
));
1117 return false; // Not coalescable
1122 /// CanJoinInsertSubRegToPhysReg - Return true if it's possible to coalesce
1123 /// an insert_subreg where src is a physical register, e.g.
1124 /// reg1024 = INSERT_SUBREG reg1024, c1, 0
1126 SimpleRegisterCoalescing::CanJoinInsertSubRegToPhysReg(unsigned DstReg
,
1127 unsigned SrcReg
, unsigned SubIdx
,
1128 unsigned &RealSrcReg
) {
1129 const TargetRegisterClass
*RC
= mri_
->getRegClass(DstReg
);
1130 RealSrcReg
= getMatchingSuperReg(SrcReg
, SubIdx
, RC
, tri_
);
1131 assert(RealSrcReg
&& "Invalid extract_subreg instruction!");
1133 LiveInterval
&RHS
= li_
->getInterval(DstReg
);
1134 if (li_
->hasInterval(RealSrcReg
) &&
1135 RHS
.overlaps(li_
->getInterval(RealSrcReg
))) {
1136 DOUT
<< "Interfere with register ";
1137 DEBUG(li_
->getInterval(RealSrcReg
).print(DOUT
, tri_
));
1138 return false; // Not coalescable
1140 for (const unsigned* SR
= tri_
->getSubRegisters(RealSrcReg
); *SR
; ++SR
)
1141 if (li_
->hasInterval(*SR
) && RHS
.overlaps(li_
->getInterval(*SR
))) {
1142 DOUT
<< "Interfere with sub-register ";
1143 DEBUG(li_
->getInterval(*SR
).print(DOUT
, tri_
));
1144 return false; // Not coalescable
1149 /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
1150 /// which are the src/dst of the copy instruction CopyMI. This returns true
1151 /// if the copy was successfully coalesced away. If it is not currently
1152 /// possible to coalesce this interval, but it may be possible if other
1153 /// things get coalesced, then it returns true by reference in 'Again'.
1154 bool SimpleRegisterCoalescing::JoinCopy(CopyRec
&TheCopy
, bool &Again
) {
1155 MachineInstr
*CopyMI
= TheCopy
.MI
;
1158 if (JoinedCopies
.count(CopyMI
) || ReMatCopies
.count(CopyMI
))
1159 return false; // Already done.
1161 DOUT
<< li_
->getInstructionIndex(CopyMI
) << '\t' << *CopyMI
;
1163 unsigned SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
;
1164 bool isExtSubReg
= CopyMI
->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG
;
1165 bool isInsSubReg
= CopyMI
->getOpcode() == TargetInstrInfo::INSERT_SUBREG
;
1166 bool isSubRegToReg
= CopyMI
->getOpcode() == TargetInstrInfo::SUBREG_TO_REG
;
1167 unsigned SubIdx
= 0;
1169 DstReg
= CopyMI
->getOperand(0).getReg();
1170 SrcReg
= CopyMI
->getOperand(1).getReg();
1171 } else if (isInsSubReg
|| isSubRegToReg
) {
1172 if (CopyMI
->getOperand(2).getSubReg()) {
1173 DOUT
<< "\tSource of insert_subreg is already coalesced "
1174 << "to another register.\n";
1175 return false; // Not coalescable.
1177 DstReg
= CopyMI
->getOperand(0).getReg();
1178 SrcReg
= CopyMI
->getOperand(2).getReg();
1179 } else if (!tii_
->isMoveInstr(*CopyMI
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
)){
1180 assert(0 && "Unrecognized copy instruction!");
1184 // If they are already joined we continue.
1185 if (SrcReg
== DstReg
) {
1186 DOUT
<< "\tCopy already coalesced.\n";
1187 return false; // Not coalescable.
1190 bool SrcIsPhys
= TargetRegisterInfo::isPhysicalRegister(SrcReg
);
1191 bool DstIsPhys
= TargetRegisterInfo::isPhysicalRegister(DstReg
);
1193 // If they are both physical registers, we cannot join them.
1194 if (SrcIsPhys
&& DstIsPhys
) {
1195 DOUT
<< "\tCan not coalesce physregs.\n";
1196 return false; // Not coalescable.
1199 // We only join virtual registers with allocatable physical registers.
1200 if (SrcIsPhys
&& !allocatableRegs_
[SrcReg
]) {
1201 DOUT
<< "\tSrc reg is unallocatable physreg.\n";
1202 return false; // Not coalescable.
1204 if (DstIsPhys
&& !allocatableRegs_
[DstReg
]) {
1205 DOUT
<< "\tDst reg is unallocatable physreg.\n";
1206 return false; // Not coalescable.
1209 // Should be non-null only when coalescing to a sub-register class.
1210 bool CrossRC
= false;
1211 const TargetRegisterClass
*NewRC
= NULL
;
1212 MachineBasicBlock
*CopyMBB
= CopyMI
->getParent();
1213 unsigned RealDstReg
= 0;
1214 unsigned RealSrcReg
= 0;
1215 if (isExtSubReg
|| isInsSubReg
|| isSubRegToReg
) {
1216 SubIdx
= CopyMI
->getOperand(isExtSubReg
? 2 : 3).getImm();
1217 if (SrcIsPhys
&& isExtSubReg
) {
1218 // r1024 = EXTRACT_SUBREG EAX, 0 then r1024 is really going to be
1219 // coalesced with AX.
1220 unsigned DstSubIdx
= CopyMI
->getOperand(0).getSubReg();
1222 // r1024<2> = EXTRACT_SUBREG EAX, 2. Then r1024 has already been
1223 // coalesced to a larger register so the subreg indices cancel out.
1224 if (DstSubIdx
!= SubIdx
) {
1225 DOUT
<< "\t Sub-register indices mismatch.\n";
1226 return false; // Not coalescable.
1229 SrcReg
= tri_
->getSubReg(SrcReg
, SubIdx
);
1231 } else if (DstIsPhys
&& (isInsSubReg
|| isSubRegToReg
)) {
1232 // EAX = INSERT_SUBREG EAX, r1024, 0
1233 unsigned SrcSubIdx
= CopyMI
->getOperand(2).getSubReg();
1235 // EAX = INSERT_SUBREG EAX, r1024<2>, 2 Then r1024 has already been
1236 // coalesced to a larger register so the subreg indices cancel out.
1237 if (SrcSubIdx
!= SubIdx
) {
1238 DOUT
<< "\t Sub-register indices mismatch.\n";
1239 return false; // Not coalescable.
1242 DstReg
= tri_
->getSubReg(DstReg
, SubIdx
);
1244 } else if ((DstIsPhys
&& isExtSubReg
) ||
1245 (SrcIsPhys
&& (isInsSubReg
|| isSubRegToReg
))) {
1246 if (!isSubRegToReg
&& CopyMI
->getOperand(1).getSubReg()) {
1247 DOUT
<< "\tSrc of extract_subreg already coalesced with reg"
1248 << " of a super-class.\n";
1249 return false; // Not coalescable.
1253 if (!CanJoinExtractSubRegToPhysReg(DstReg
, SrcReg
, SubIdx
, RealDstReg
))
1254 return false; // Not coalescable
1256 if (!CanJoinInsertSubRegToPhysReg(DstReg
, SrcReg
, SubIdx
, RealSrcReg
))
1257 return false; // Not coalescable
1261 unsigned OldSubIdx
= isExtSubReg
? CopyMI
->getOperand(0).getSubReg()
1262 : CopyMI
->getOperand(2).getSubReg();
1264 if (OldSubIdx
== SubIdx
&& !differingRegisterClasses(SrcReg
, DstReg
))
1265 // r1024<2> = EXTRACT_SUBREG r1025, 2. Then r1024 has already been
1266 // coalesced to a larger register so the subreg indices cancel out.
1267 // Also check if the other larger register is of the same register
1268 // class as the would be resulting register.
1271 DOUT
<< "\t Sub-register indices mismatch.\n";
1272 return false; // Not coalescable.
1276 unsigned LargeReg
= isExtSubReg
? SrcReg
: DstReg
;
1277 unsigned SmallReg
= isExtSubReg
? DstReg
: SrcReg
;
1278 unsigned Limit
= allocatableRCRegs_
[mri_
->getRegClass(SmallReg
)].count();
1279 if (!isWinToJoinCrossClass(LargeReg
, SmallReg
, Limit
)) {
1280 Again
= true; // May be possible to coalesce later.
1285 } else if (differingRegisterClasses(SrcReg
, DstReg
)) {
1286 if (!CrossClassJoin
)
1290 // FIXME: What if the result of a EXTRACT_SUBREG is then coalesced
1291 // with another? If it's the resulting destination register, then
1292 // the subidx must be propagated to uses (but only those defined
1293 // by the EXTRACT_SUBREG). If it's being coalesced into another
1294 // register, it should be safe because register is assumed to have
1295 // the register class of the super-register.
1297 // Process moves where one of the registers have a sub-register index.
1298 MachineOperand
*DstMO
= CopyMI
->findRegisterDefOperand(DstReg
);
1299 MachineOperand
*SrcMO
= CopyMI
->findRegisterUseOperand(SrcReg
);
1300 SubIdx
= DstMO
->getSubReg();
1302 if (SrcMO
->getSubReg())
1303 // FIXME: can we handle this?
1305 // This is not an insert_subreg but it looks like one.
1306 // e.g. %reg1024:4 = MOV32rr %EAX
1309 if (!CanJoinInsertSubRegToPhysReg(DstReg
, SrcReg
, SubIdx
, RealSrcReg
))
1310 return false; // Not coalescable
1314 SubIdx
= SrcMO
->getSubReg();
1316 // This is not a extract_subreg but it looks like one.
1317 // e.g. %cl = MOV16rr %reg1024:1
1320 if (!CanJoinExtractSubRegToPhysReg(DstReg
, SrcReg
, SubIdx
,RealDstReg
))
1321 return false; // Not coalescable
1327 const TargetRegisterClass
*SrcRC
= SrcIsPhys
? 0 : mri_
->getRegClass(SrcReg
);
1328 const TargetRegisterClass
*DstRC
= DstIsPhys
? 0 : mri_
->getRegClass(DstReg
);
1329 unsigned LargeReg
= SrcReg
;
1330 unsigned SmallReg
= DstReg
;
1333 // Now determine the register class of the joined register.
1335 if (SubIdx
&& DstRC
&& DstRC
->isASubClass()) {
1336 // This is a move to a sub-register class. However, the source is a
1337 // sub-register of a larger register class. We don't know what should
1338 // the register class be. FIXME.
1342 Limit
= allocatableRCRegs_
[DstRC
].count();
1343 } else if (!SrcIsPhys
&& !DstIsPhys
) {
1344 unsigned SrcSize
= SrcRC
->getSize();
1345 unsigned DstSize
= DstRC
->getSize();
1346 if (SrcSize
< DstSize
)
1347 // For example X86::MOVSD2PDrr copies from FR64 to VR128.
1349 else if (DstSize
> SrcSize
) {
1351 std::swap(LargeReg
, SmallReg
);
1353 unsigned SrcNumRegs
= SrcRC
->getNumRegs();
1354 unsigned DstNumRegs
= DstRC
->getNumRegs();
1355 if (DstNumRegs
< SrcNumRegs
)
1356 // Sub-register class?
1358 else if (SrcNumRegs
< DstNumRegs
) {
1360 std::swap(LargeReg
, SmallReg
);
1362 // No idea what's the right register class to use.
1367 // If we are joining two virtual registers and the resulting register
1368 // class is more restrictive (fewer register, smaller size). Check if it's
1369 // worth doing the merge.
1370 if (!SrcIsPhys
&& !DstIsPhys
&&
1371 (isExtSubReg
|| DstRC
->isASubClass()) &&
1372 !isWinToJoinCrossClass(LargeReg
, SmallReg
,
1373 allocatableRCRegs_
[NewRC
].count())) {
1374 DOUT
<< "\tSrc/Dest are different register classes.\n";
1375 // Allow the coalescer to try again in case either side gets coalesced to
1376 // a physical register that's compatible with the other side. e.g.
1377 // r1024 = MOV32to32_ r1025
1378 // But later r1024 is assigned EAX then r1025 may be coalesced with EAX.
1379 Again
= true; // May be possible to coalesce later.
1384 // Will it create illegal extract_subreg / insert_subreg?
1385 if (SrcIsPhys
&& HasIncompatibleSubRegDefUse(CopyMI
, DstReg
, SrcReg
))
1387 if (DstIsPhys
&& HasIncompatibleSubRegDefUse(CopyMI
, SrcReg
, DstReg
))
1390 LiveInterval
&SrcInt
= li_
->getInterval(SrcReg
);
1391 LiveInterval
&DstInt
= li_
->getInterval(DstReg
);
1392 assert(SrcInt
.reg
== SrcReg
&& DstInt
.reg
== DstReg
&&
1393 "Register mapping is horribly broken!");
1395 DOUT
<< "\t\tInspecting "; SrcInt
.print(DOUT
, tri_
);
1396 DOUT
<< " and "; DstInt
.print(DOUT
, tri_
);
1399 // Save a copy of the virtual register live interval. We'll manually
1400 // merge this into the "real" physical register live interval this is
1402 LiveInterval
*SavedLI
= 0;
1404 SavedLI
= li_
->dupInterval(&SrcInt
);
1405 else if (RealSrcReg
)
1406 SavedLI
= li_
->dupInterval(&DstInt
);
1408 // Check if it is necessary to propagate "isDead" property.
1409 if (!isExtSubReg
&& !isInsSubReg
&& !isSubRegToReg
) {
1410 MachineOperand
*mopd
= CopyMI
->findRegisterDefOperand(DstReg
, false);
1411 bool isDead
= mopd
->isDead();
1413 // We need to be careful about coalescing a source physical register with a
1414 // virtual register. Once the coalescing is done, it cannot be broken and
1415 // these are not spillable! If the destination interval uses are far away,
1416 // think twice about coalescing them!
1417 if (!isDead
&& (SrcIsPhys
|| DstIsPhys
)) {
1418 LiveInterval
&JoinVInt
= SrcIsPhys
? DstInt
: SrcInt
;
1419 unsigned JoinVReg
= SrcIsPhys
? DstReg
: SrcReg
;
1420 unsigned JoinPReg
= SrcIsPhys
? SrcReg
: DstReg
;
1421 const TargetRegisterClass
*RC
= mri_
->getRegClass(JoinVReg
);
1422 unsigned Threshold
= allocatableRCRegs_
[RC
].count() * 2;
1423 if (TheCopy
.isBackEdge
)
1424 Threshold
*= 2; // Favors back edge copies.
1426 // If the virtual register live interval is long but it has low use desity,
1427 // do not join them, instead mark the physical register as its allocation
1429 unsigned Length
= li_
->getApproximateInstructionCount(JoinVInt
);
1430 if (Length
> Threshold
&&
1431 (((float)std::distance(mri_
->use_begin(JoinVReg
), mri_
->use_end())
1432 / Length
) < (1.0 / Threshold
))) {
1433 JoinVInt
.preference
= JoinPReg
;
1435 DOUT
<< "\tMay tie down a physical register, abort!\n";
1436 Again
= true; // May be possible to coalesce later.
1442 // Okay, attempt to join these two intervals. On failure, this returns false.
1443 // Otherwise, if one of the intervals being joined is a physreg, this method
1444 // always canonicalizes DstInt to be it. The output "SrcInt" will not have
1445 // been modified, so we can use this information below to update aliases.
1446 bool Swapped
= false;
1447 // If SrcInt is implicitly defined, it's safe to coalesce.
1448 bool isEmpty
= SrcInt
.empty();
1449 if (isEmpty
&& !CanCoalesceWithImpDef(CopyMI
, DstInt
, SrcInt
)) {
1450 // Only coalesce an empty interval (defined by implicit_def) with
1451 // another interval which has a valno defined by the CopyMI and the CopyMI
1452 // is a kill of the implicit def.
1453 DOUT
<< "Not profitable!\n";
1457 if (!isEmpty
&& !JoinIntervals(DstInt
, SrcInt
, Swapped
)) {
1458 // Coalescing failed.
1460 // If definition of source is defined by trivial computation, try
1461 // rematerializing it.
1462 if (!isExtSubReg
&& !isInsSubReg
&& !isSubRegToReg
&&
1463 ReMaterializeTrivialDef(SrcInt
, DstInt
.reg
, CopyMI
))
1466 // If we can eliminate the copy without merging the live ranges, do so now.
1467 if (!isExtSubReg
&& !isInsSubReg
&& !isSubRegToReg
&&
1468 (AdjustCopiesBackFrom(SrcInt
, DstInt
, CopyMI
) ||
1469 RemoveCopyByCommutingDef(SrcInt
, DstInt
, CopyMI
))) {
1470 JoinedCopies
.insert(CopyMI
);
1474 // Otherwise, we are unable to join the intervals.
1475 DOUT
<< "Interference!\n";
1476 Again
= true; // May be possible to coalesce later.
1480 LiveInterval
*ResSrcInt
= &SrcInt
;
1481 LiveInterval
*ResDstInt
= &DstInt
;
1483 std::swap(SrcReg
, DstReg
);
1484 std::swap(ResSrcInt
, ResDstInt
);
1486 assert(TargetRegisterInfo::isVirtualRegister(SrcReg
) &&
1487 "LiveInterval::join didn't work right!");
1489 // If we're about to merge live ranges into a physical register live interval,
1490 // we have to update any aliased register's live ranges to indicate that they
1491 // have clobbered values for this range.
1492 if (TargetRegisterInfo::isPhysicalRegister(DstReg
)) {
1493 // If this is a extract_subreg where dst is a physical register, e.g.
1494 // cl = EXTRACT_SUBREG reg1024, 1
1495 // then create and update the actual physical register allocated to RHS.
1496 if (RealDstReg
|| RealSrcReg
) {
1497 LiveInterval
&RealInt
=
1498 li_
->getOrCreateInterval(RealDstReg
? RealDstReg
: RealSrcReg
);
1499 for (LiveInterval::const_vni_iterator I
= SavedLI
->vni_begin(),
1500 E
= SavedLI
->vni_end(); I
!= E
; ++I
) {
1501 const VNInfo
*ValNo
= *I
;
1502 VNInfo
*NewValNo
= RealInt
.getNextValue(ValNo
->def
, ValNo
->copy
,
1503 li_
->getVNInfoAllocator());
1504 NewValNo
->hasPHIKill
= ValNo
->hasPHIKill
;
1505 NewValNo
->redefByEC
= ValNo
->redefByEC
;
1506 RealInt
.addKills(NewValNo
, ValNo
->kills
);
1507 RealInt
.MergeValueInAsValue(*SavedLI
, ValNo
, NewValNo
);
1509 RealInt
.weight
+= SavedLI
->weight
;
1510 DstReg
= RealDstReg
? RealDstReg
: RealSrcReg
;
1513 // Update the liveintervals of sub-registers.
1514 for (const unsigned *AS
= tri_
->getSubRegisters(DstReg
); *AS
; ++AS
)
1515 li_
->getOrCreateInterval(*AS
).MergeInClobberRanges(*ResSrcInt
,
1516 li_
->getVNInfoAllocator());
1519 // If this is a EXTRACT_SUBREG, make sure the result of coalescing is the
1520 // larger super-register.
1521 if ((isExtSubReg
|| isInsSubReg
|| isSubRegToReg
) &&
1522 !SrcIsPhys
&& !DstIsPhys
) {
1523 if ((isExtSubReg
&& !Swapped
) ||
1524 ((isInsSubReg
|| isSubRegToReg
) && Swapped
)) {
1525 ResSrcInt
->Copy(*ResDstInt
, li_
->getVNInfoAllocator());
1526 std::swap(SrcReg
, DstReg
);
1527 std::swap(ResSrcInt
, ResDstInt
);
1531 // Coalescing to a virtual register that is of a sub-register class of the
1532 // other. Make sure the resulting register is set to the right register class.
1536 mri_
->setRegClass(DstReg
, NewRC
);
1540 // Add all copies that define val# in the source interval into the queue.
1541 for (LiveInterval::const_vni_iterator i
= ResSrcInt
->vni_begin(),
1542 e
= ResSrcInt
->vni_end(); i
!= e
; ++i
) {
1543 const VNInfo
*vni
= *i
;
1544 if (!vni
->def
|| vni
->def
== ~1U || vni
->def
== ~0U)
1546 MachineInstr
*CopyMI
= li_
->getInstructionFromIndex(vni
->def
);
1547 unsigned NewSrcReg
, NewDstReg
, NewSrcSubIdx
, NewDstSubIdx
;
1549 JoinedCopies
.count(CopyMI
) == 0 &&
1550 tii_
->isMoveInstr(*CopyMI
, NewSrcReg
, NewDstReg
,
1551 NewSrcSubIdx
, NewDstSubIdx
)) {
1552 unsigned LoopDepth
= loopInfo
->getLoopDepth(CopyMBB
);
1553 JoinQueue
->push(CopyRec(CopyMI
, LoopDepth
,
1554 isBackEdgeCopy(CopyMI
, DstReg
)));
1559 // Remember to delete the copy instruction.
1560 JoinedCopies
.insert(CopyMI
);
1562 // Some live range has been lengthened due to colaescing, eliminate the
1563 // unnecessary kills.
1564 RemoveUnnecessaryKills(SrcReg
, *ResDstInt
);
1565 if (TargetRegisterInfo::isVirtualRegister(DstReg
))
1566 RemoveUnnecessaryKills(DstReg
, *ResDstInt
);
1571 // r1024 = implicit_def
1574 RemoveDeadImpDef(DstReg
, *ResDstInt
);
1575 UpdateRegDefsUses(SrcReg
, DstReg
, SubIdx
);
1577 // SrcReg is guarateed to be the register whose live interval that is
1579 li_
->removeInterval(SrcReg
);
1581 // Manually deleted the live interval copy.
1588 // Now the copy is being coalesced away, the val# previously defined
1589 // by the copy is being defined by an IMPLICIT_DEF which defines a zero
1590 // length interval. Remove the val#.
1591 unsigned CopyIdx
= li_
->getDefIndex(li_
->getInstructionIndex(CopyMI
));
1592 const LiveRange
*LR
= ResDstInt
->getLiveRangeContaining(CopyIdx
);
1593 VNInfo
*ImpVal
= LR
->valno
;
1594 assert(ImpVal
->def
== CopyIdx
);
1595 unsigned NextDef
= LR
->end
;
1596 RemoveCopiesFromValNo(*ResDstInt
, ImpVal
);
1597 ResDstInt
->removeValNo(ImpVal
);
1598 LR
= ResDstInt
->FindLiveRangeContaining(NextDef
);
1599 if (LR
!= ResDstInt
->end() && LR
->valno
->def
== NextDef
) {
1600 // Special case: vr1024 = implicit_def
1601 // vr1024 = insert_subreg vr1024, vr1025, c
1602 // The insert_subreg becomes a "copy" that defines a val# which can itself
1603 // be coalesced away.
1604 MachineInstr
*DefMI
= li_
->getInstructionFromIndex(NextDef
);
1605 if (DefMI
->getOpcode() == TargetInstrInfo::INSERT_SUBREG
)
1606 LR
->valno
->copy
= DefMI
;
1610 // If resulting interval has a preference that no longer fits because of subreg
1611 // coalescing, just clear the preference.
1612 if (ResDstInt
->preference
&& (isExtSubReg
|| isInsSubReg
|| isSubRegToReg
) &&
1613 TargetRegisterInfo::isVirtualRegister(ResDstInt
->reg
)) {
1614 const TargetRegisterClass
*RC
= mri_
->getRegClass(ResDstInt
->reg
);
1615 if (!RC
->contains(ResDstInt
->preference
))
1616 ResDstInt
->preference
= 0;
1619 DOUT
<< "\n\t\tJoined. Result = "; ResDstInt
->print(DOUT
, tri_
);
1626 /// ComputeUltimateVN - Assuming we are going to join two live intervals,
1627 /// compute what the resultant value numbers for each value in the input two
1628 /// ranges will be. This is complicated by copies between the two which can
1629 /// and will commonly cause multiple value numbers to be merged into one.
1631 /// VN is the value number that we're trying to resolve. InstDefiningValue
1632 /// keeps track of the new InstDefiningValue assignment for the result
1633 /// LiveInterval. ThisFromOther/OtherFromThis are sets that keep track of
1634 /// whether a value in this or other is a copy from the opposite set.
1635 /// ThisValNoAssignments/OtherValNoAssignments keep track of value #'s that have
1636 /// already been assigned.
1638 /// ThisFromOther[x] - If x is defined as a copy from the other interval, this
1639 /// contains the value number the copy is from.
1641 static unsigned ComputeUltimateVN(VNInfo
*VNI
,
1642 SmallVector
<VNInfo
*, 16> &NewVNInfo
,
1643 DenseMap
<VNInfo
*, VNInfo
*> &ThisFromOther
,
1644 DenseMap
<VNInfo
*, VNInfo
*> &OtherFromThis
,
1645 SmallVector
<int, 16> &ThisValNoAssignments
,
1646 SmallVector
<int, 16> &OtherValNoAssignments
) {
1647 unsigned VN
= VNI
->id
;
1649 // If the VN has already been computed, just return it.
1650 if (ThisValNoAssignments
[VN
] >= 0)
1651 return ThisValNoAssignments
[VN
];
1652 // assert(ThisValNoAssignments[VN] != -2 && "Cyclic case?");
1654 // If this val is not a copy from the other val, then it must be a new value
1655 // number in the destination.
1656 DenseMap
<VNInfo
*, VNInfo
*>::iterator I
= ThisFromOther
.find(VNI
);
1657 if (I
== ThisFromOther
.end()) {
1658 NewVNInfo
.push_back(VNI
);
1659 return ThisValNoAssignments
[VN
] = NewVNInfo
.size()-1;
1661 VNInfo
*OtherValNo
= I
->second
;
1663 // Otherwise, this *is* a copy from the RHS. If the other side has already
1664 // been computed, return it.
1665 if (OtherValNoAssignments
[OtherValNo
->id
] >= 0)
1666 return ThisValNoAssignments
[VN
] = OtherValNoAssignments
[OtherValNo
->id
];
1668 // Mark this value number as currently being computed, then ask what the
1669 // ultimate value # of the other value is.
1670 ThisValNoAssignments
[VN
] = -2;
1671 unsigned UltimateVN
=
1672 ComputeUltimateVN(OtherValNo
, NewVNInfo
, OtherFromThis
, ThisFromOther
,
1673 OtherValNoAssignments
, ThisValNoAssignments
);
1674 return ThisValNoAssignments
[VN
] = UltimateVN
;
1677 static bool InVector(VNInfo
*Val
, const SmallVector
<VNInfo
*, 8> &V
) {
1678 return std::find(V
.begin(), V
.end(), Val
) != V
.end();
1681 /// RangeIsDefinedByCopyFromReg - Return true if the specified live range of
1682 /// the specified live interval is defined by a copy from the specified
1684 bool SimpleRegisterCoalescing::RangeIsDefinedByCopyFromReg(LiveInterval
&li
,
1687 unsigned SrcReg
= li_
->getVNInfoSourceReg(LR
->valno
);
1690 if (LR
->valno
->def
== ~0U &&
1691 TargetRegisterInfo::isPhysicalRegister(li
.reg
) &&
1692 *tri_
->getSuperRegisters(li
.reg
)) {
1693 // It's a sub-register live interval, we may not have precise information.
1695 MachineInstr
*DefMI
= li_
->getInstructionFromIndex(LR
->start
);
1696 unsigned SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
;
1698 tii_
->isMoveInstr(*DefMI
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
) &&
1699 DstReg
== li
.reg
&& SrcReg
== Reg
) {
1700 // Cache computed info.
1701 LR
->valno
->def
= LR
->start
;
1702 LR
->valno
->copy
= DefMI
;
1709 /// SimpleJoin - Attempt to joint the specified interval into this one. The
1710 /// caller of this method must guarantee that the RHS only contains a single
1711 /// value number and that the RHS is not defined by a copy from this
1712 /// interval. This returns false if the intervals are not joinable, or it
1713 /// joins them and returns true.
1714 bool SimpleRegisterCoalescing::SimpleJoin(LiveInterval
&LHS
, LiveInterval
&RHS
){
1715 assert(RHS
.containsOneValue());
1717 // Some number (potentially more than one) value numbers in the current
1718 // interval may be defined as copies from the RHS. Scan the overlapping
1719 // portions of the LHS and RHS, keeping track of this and looking for
1720 // overlapping live ranges that are NOT defined as copies. If these exist, we
1723 LiveInterval::iterator LHSIt
= LHS
.begin(), LHSEnd
= LHS
.end();
1724 LiveInterval::iterator RHSIt
= RHS
.begin(), RHSEnd
= RHS
.end();
1726 if (LHSIt
->start
< RHSIt
->start
) {
1727 LHSIt
= std::upper_bound(LHSIt
, LHSEnd
, RHSIt
->start
);
1728 if (LHSIt
!= LHS
.begin()) --LHSIt
;
1729 } else if (RHSIt
->start
< LHSIt
->start
) {
1730 RHSIt
= std::upper_bound(RHSIt
, RHSEnd
, LHSIt
->start
);
1731 if (RHSIt
!= RHS
.begin()) --RHSIt
;
1734 SmallVector
<VNInfo
*, 8> EliminatedLHSVals
;
1737 // Determine if these live intervals overlap.
1738 bool Overlaps
= false;
1739 if (LHSIt
->start
<= RHSIt
->start
)
1740 Overlaps
= LHSIt
->end
> RHSIt
->start
;
1742 Overlaps
= RHSIt
->end
> LHSIt
->start
;
1744 // If the live intervals overlap, there are two interesting cases: if the
1745 // LHS interval is defined by a copy from the RHS, it's ok and we record
1746 // that the LHS value # is the same as the RHS. If it's not, then we cannot
1747 // coalesce these live ranges and we bail out.
1749 // If we haven't already recorded that this value # is safe, check it.
1750 if (!InVector(LHSIt
->valno
, EliminatedLHSVals
)) {
1751 // Copy from the RHS?
1752 if (!RangeIsDefinedByCopyFromReg(LHS
, LHSIt
, RHS
.reg
))
1753 return false; // Nope, bail out.
1755 if (LHSIt
->contains(RHSIt
->valno
->def
))
1756 // Here is an interesting situation:
1758 // vr1025 = copy vr1024
1763 // Even though vr1025 is copied from vr1024, it's not safe to
1764 // coalesce them since the live range of vr1025 intersects the
1765 // def of vr1024. This happens because vr1025 is assigned the
1766 // value of the previous iteration of vr1024.
1768 EliminatedLHSVals
.push_back(LHSIt
->valno
);
1771 // We know this entire LHS live range is okay, so skip it now.
1772 if (++LHSIt
== LHSEnd
) break;
1776 if (LHSIt
->end
< RHSIt
->end
) {
1777 if (++LHSIt
== LHSEnd
) break;
1779 // One interesting case to check here. It's possible that we have
1780 // something like "X3 = Y" which defines a new value number in the LHS,
1781 // and is the last use of this liverange of the RHS. In this case, we
1782 // want to notice this copy (so that it gets coalesced away) even though
1783 // the live ranges don't actually overlap.
1784 if (LHSIt
->start
== RHSIt
->end
) {
1785 if (InVector(LHSIt
->valno
, EliminatedLHSVals
)) {
1786 // We already know that this value number is going to be merged in
1787 // if coalescing succeeds. Just skip the liverange.
1788 if (++LHSIt
== LHSEnd
) break;
1790 // Otherwise, if this is a copy from the RHS, mark it as being merged
1792 if (RangeIsDefinedByCopyFromReg(LHS
, LHSIt
, RHS
.reg
)) {
1793 if (LHSIt
->contains(RHSIt
->valno
->def
))
1794 // Here is an interesting situation:
1796 // vr1025 = copy vr1024
1801 // Even though vr1025 is copied from vr1024, it's not safe to
1802 // coalesced them since live range of vr1025 intersects the
1803 // def of vr1024. This happens because vr1025 is assigned the
1804 // value of the previous iteration of vr1024.
1806 EliminatedLHSVals
.push_back(LHSIt
->valno
);
1808 // We know this entire LHS live range is okay, so skip it now.
1809 if (++LHSIt
== LHSEnd
) break;
1814 if (++RHSIt
== RHSEnd
) break;
1818 // If we got here, we know that the coalescing will be successful and that
1819 // the value numbers in EliminatedLHSVals will all be merged together. Since
1820 // the most common case is that EliminatedLHSVals has a single number, we
1821 // optimize for it: if there is more than one value, we merge them all into
1822 // the lowest numbered one, then handle the interval as if we were merging
1823 // with one value number.
1824 VNInfo
*LHSValNo
= NULL
;
1825 if (EliminatedLHSVals
.size() > 1) {
1826 // Loop through all the equal value numbers merging them into the smallest
1828 VNInfo
*Smallest
= EliminatedLHSVals
[0];
1829 for (unsigned i
= 1, e
= EliminatedLHSVals
.size(); i
!= e
; ++i
) {
1830 if (EliminatedLHSVals
[i
]->id
< Smallest
->id
) {
1831 // Merge the current notion of the smallest into the smaller one.
1832 LHS
.MergeValueNumberInto(Smallest
, EliminatedLHSVals
[i
]);
1833 Smallest
= EliminatedLHSVals
[i
];
1835 // Merge into the smallest.
1836 LHS
.MergeValueNumberInto(EliminatedLHSVals
[i
], Smallest
);
1839 LHSValNo
= Smallest
;
1840 } else if (EliminatedLHSVals
.empty()) {
1841 if (TargetRegisterInfo::isPhysicalRegister(LHS
.reg
) &&
1842 *tri_
->getSuperRegisters(LHS
.reg
))
1843 // Imprecise sub-register information. Can't handle it.
1845 assert(0 && "No copies from the RHS?");
1847 LHSValNo
= EliminatedLHSVals
[0];
1850 // Okay, now that there is a single LHS value number that we're merging the
1851 // RHS into, update the value number info for the LHS to indicate that the
1852 // value number is defined where the RHS value number was.
1853 const VNInfo
*VNI
= RHS
.getValNumInfo(0);
1854 LHSValNo
->def
= VNI
->def
;
1855 LHSValNo
->copy
= VNI
->copy
;
1857 // Okay, the final step is to loop over the RHS live intervals, adding them to
1859 LHSValNo
->hasPHIKill
|= VNI
->hasPHIKill
;
1860 LHS
.addKills(LHSValNo
, VNI
->kills
);
1861 LHS
.MergeRangesInAsValue(RHS
, LHSValNo
);
1862 LHS
.weight
+= RHS
.weight
;
1863 if (RHS
.preference
&& !LHS
.preference
)
1864 LHS
.preference
= RHS
.preference
;
1866 // Update the liveintervals of sub-registers.
1867 if (TargetRegisterInfo::isPhysicalRegister(LHS
.reg
))
1868 for (const unsigned *AS
= tri_
->getSubRegisters(LHS
.reg
); *AS
; ++AS
)
1869 li_
->getOrCreateInterval(*AS
).MergeInClobberRanges(LHS
,
1870 li_
->getVNInfoAllocator());
1875 /// JoinIntervals - Attempt to join these two intervals. On failure, this
1876 /// returns false. Otherwise, if one of the intervals being joined is a
1877 /// physreg, this method always canonicalizes LHS to be it. The output
1878 /// "RHS" will not have been modified, so we can use this information
1879 /// below to update aliases.
1881 SimpleRegisterCoalescing::JoinIntervals(LiveInterval
&LHS
, LiveInterval
&RHS
,
1883 // Compute the final value assignment, assuming that the live ranges can be
1885 SmallVector
<int, 16> LHSValNoAssignments
;
1886 SmallVector
<int, 16> RHSValNoAssignments
;
1887 DenseMap
<VNInfo
*, VNInfo
*> LHSValsDefinedFromRHS
;
1888 DenseMap
<VNInfo
*, VNInfo
*> RHSValsDefinedFromLHS
;
1889 SmallVector
<VNInfo
*, 16> NewVNInfo
;
1891 // If a live interval is a physical register, conservatively check if any
1892 // of its sub-registers is overlapping the live interval of the virtual
1893 // register. If so, do not coalesce.
1894 if (TargetRegisterInfo::isPhysicalRegister(LHS
.reg
) &&
1895 *tri_
->getSubRegisters(LHS
.reg
)) {
1896 // If it's coalescing a virtual register to a physical register, estimate
1897 // its live interval length. This is the *cost* of scanning an entire live
1898 // interval. If the cost is low, we'll do an exhaustive check instead.
1900 // If this is something like this:
1908 // That is, the live interval of v1024 crosses a bb. Then we can't rely on
1909 // less conservative check. It's possible a sub-register is defined before
1910 // v1024 (or live in) and live out of BB1.
1911 if (RHS
.containsOneValue() &&
1912 li_
->intervalIsInOneMBB(RHS
) &&
1913 li_
->getApproximateInstructionCount(RHS
) <= 10) {
1914 // Perform a more exhaustive check for some common cases.
1915 if (li_
->conflictsWithPhysRegRef(RHS
, LHS
.reg
, true, JoinedCopies
))
1918 for (const unsigned* SR
= tri_
->getSubRegisters(LHS
.reg
); *SR
; ++SR
)
1919 if (li_
->hasInterval(*SR
) && RHS
.overlaps(li_
->getInterval(*SR
))) {
1920 DOUT
<< "Interfere with sub-register ";
1921 DEBUG(li_
->getInterval(*SR
).print(DOUT
, tri_
));
1925 } else if (TargetRegisterInfo::isPhysicalRegister(RHS
.reg
) &&
1926 *tri_
->getSubRegisters(RHS
.reg
)) {
1927 if (LHS
.containsOneValue() &&
1928 li_
->getApproximateInstructionCount(LHS
) <= 10) {
1929 // Perform a more exhaustive check for some common cases.
1930 if (li_
->conflictsWithPhysRegRef(LHS
, RHS
.reg
, false, JoinedCopies
))
1933 for (const unsigned* SR
= tri_
->getSubRegisters(RHS
.reg
); *SR
; ++SR
)
1934 if (li_
->hasInterval(*SR
) && LHS
.overlaps(li_
->getInterval(*SR
))) {
1935 DOUT
<< "Interfere with sub-register ";
1936 DEBUG(li_
->getInterval(*SR
).print(DOUT
, tri_
));
1942 // Compute ultimate value numbers for the LHS and RHS values.
1943 if (RHS
.containsOneValue()) {
1944 // Copies from a liveinterval with a single value are simple to handle and
1945 // very common, handle the special case here. This is important, because
1946 // often RHS is small and LHS is large (e.g. a physreg).
1948 // Find out if the RHS is defined as a copy from some value in the LHS.
1949 int RHSVal0DefinedFromLHS
= -1;
1951 VNInfo
*RHSValNoInfo
= NULL
;
1952 VNInfo
*RHSValNoInfo0
= RHS
.getValNumInfo(0);
1953 unsigned RHSSrcReg
= li_
->getVNInfoSourceReg(RHSValNoInfo0
);
1954 if (RHSSrcReg
== 0 || RHSSrcReg
!= LHS
.reg
) {
1955 // If RHS is not defined as a copy from the LHS, we can use simpler and
1956 // faster checks to see if the live ranges are coalescable. This joiner
1957 // can't swap the LHS/RHS intervals though.
1958 if (!TargetRegisterInfo::isPhysicalRegister(RHS
.reg
)) {
1959 return SimpleJoin(LHS
, RHS
);
1961 RHSValNoInfo
= RHSValNoInfo0
;
1964 // It was defined as a copy from the LHS, find out what value # it is.
1965 RHSValNoInfo
= LHS
.getLiveRangeContaining(RHSValNoInfo0
->def
-1)->valno
;
1966 RHSValID
= RHSValNoInfo
->id
;
1967 RHSVal0DefinedFromLHS
= RHSValID
;
1970 LHSValNoAssignments
.resize(LHS
.getNumValNums(), -1);
1971 RHSValNoAssignments
.resize(RHS
.getNumValNums(), -1);
1972 NewVNInfo
.resize(LHS
.getNumValNums(), NULL
);
1974 // Okay, *all* of the values in LHS that are defined as a copy from RHS
1975 // should now get updated.
1976 for (LiveInterval::vni_iterator i
= LHS
.vni_begin(), e
= LHS
.vni_end();
1979 unsigned VN
= VNI
->id
;
1980 if (unsigned LHSSrcReg
= li_
->getVNInfoSourceReg(VNI
)) {
1981 if (LHSSrcReg
!= RHS
.reg
) {
1982 // If this is not a copy from the RHS, its value number will be
1983 // unmodified by the coalescing.
1984 NewVNInfo
[VN
] = VNI
;
1985 LHSValNoAssignments
[VN
] = VN
;
1986 } else if (RHSValID
== -1) {
1987 // Otherwise, it is a copy from the RHS, and we don't already have a
1988 // value# for it. Keep the current value number, but remember it.
1989 LHSValNoAssignments
[VN
] = RHSValID
= VN
;
1990 NewVNInfo
[VN
] = RHSValNoInfo
;
1991 LHSValsDefinedFromRHS
[VNI
] = RHSValNoInfo0
;
1993 // Otherwise, use the specified value #.
1994 LHSValNoAssignments
[VN
] = RHSValID
;
1995 if (VN
== (unsigned)RHSValID
) { // Else this val# is dead.
1996 NewVNInfo
[VN
] = RHSValNoInfo
;
1997 LHSValsDefinedFromRHS
[VNI
] = RHSValNoInfo0
;
2001 NewVNInfo
[VN
] = VNI
;
2002 LHSValNoAssignments
[VN
] = VN
;
2006 assert(RHSValID
!= -1 && "Didn't find value #?");
2007 RHSValNoAssignments
[0] = RHSValID
;
2008 if (RHSVal0DefinedFromLHS
!= -1) {
2009 // This path doesn't go through ComputeUltimateVN so just set
2011 RHSValsDefinedFromLHS
[RHSValNoInfo0
] = (VNInfo
*)1;
2014 // Loop over the value numbers of the LHS, seeing if any are defined from
2016 for (LiveInterval::vni_iterator i
= LHS
.vni_begin(), e
= LHS
.vni_end();
2019 if (VNI
->def
== ~1U || VNI
->copy
== 0) // Src not defined by a copy?
2022 // DstReg is known to be a register in the LHS interval. If the src is
2023 // from the RHS interval, we can use its value #.
2024 if (li_
->getVNInfoSourceReg(VNI
) != RHS
.reg
)
2027 // Figure out the value # from the RHS.
2028 LHSValsDefinedFromRHS
[VNI
]=RHS
.getLiveRangeContaining(VNI
->def
-1)->valno
;
2031 // Loop over the value numbers of the RHS, seeing if any are defined from
2033 for (LiveInterval::vni_iterator i
= RHS
.vni_begin(), e
= RHS
.vni_end();
2036 if (VNI
->def
== ~1U || VNI
->copy
== 0) // Src not defined by a copy?
2039 // DstReg is known to be a register in the RHS interval. If the src is
2040 // from the LHS interval, we can use its value #.
2041 if (li_
->getVNInfoSourceReg(VNI
) != LHS
.reg
)
2044 // Figure out the value # from the LHS.
2045 RHSValsDefinedFromLHS
[VNI
]=LHS
.getLiveRangeContaining(VNI
->def
-1)->valno
;
2048 LHSValNoAssignments
.resize(LHS
.getNumValNums(), -1);
2049 RHSValNoAssignments
.resize(RHS
.getNumValNums(), -1);
2050 NewVNInfo
.reserve(LHS
.getNumValNums() + RHS
.getNumValNums());
2052 for (LiveInterval::vni_iterator i
= LHS
.vni_begin(), e
= LHS
.vni_end();
2055 unsigned VN
= VNI
->id
;
2056 if (LHSValNoAssignments
[VN
] >= 0 || VNI
->def
== ~1U)
2058 ComputeUltimateVN(VNI
, NewVNInfo
,
2059 LHSValsDefinedFromRHS
, RHSValsDefinedFromLHS
,
2060 LHSValNoAssignments
, RHSValNoAssignments
);
2062 for (LiveInterval::vni_iterator i
= RHS
.vni_begin(), e
= RHS
.vni_end();
2065 unsigned VN
= VNI
->id
;
2066 if (RHSValNoAssignments
[VN
] >= 0 || VNI
->def
== ~1U)
2068 // If this value number isn't a copy from the LHS, it's a new number.
2069 if (RHSValsDefinedFromLHS
.find(VNI
) == RHSValsDefinedFromLHS
.end()) {
2070 NewVNInfo
.push_back(VNI
);
2071 RHSValNoAssignments
[VN
] = NewVNInfo
.size()-1;
2075 ComputeUltimateVN(VNI
, NewVNInfo
,
2076 RHSValsDefinedFromLHS
, LHSValsDefinedFromRHS
,
2077 RHSValNoAssignments
, LHSValNoAssignments
);
2081 // Armed with the mappings of LHS/RHS values to ultimate values, walk the
2082 // interval lists to see if these intervals are coalescable.
2083 LiveInterval::const_iterator I
= LHS
.begin();
2084 LiveInterval::const_iterator IE
= LHS
.end();
2085 LiveInterval::const_iterator J
= RHS
.begin();
2086 LiveInterval::const_iterator JE
= RHS
.end();
2088 // Skip ahead until the first place of potential sharing.
2089 if (I
->start
< J
->start
) {
2090 I
= std::upper_bound(I
, IE
, J
->start
);
2091 if (I
!= LHS
.begin()) --I
;
2092 } else if (J
->start
< I
->start
) {
2093 J
= std::upper_bound(J
, JE
, I
->start
);
2094 if (J
!= RHS
.begin()) --J
;
2098 // Determine if these two live ranges overlap.
2100 if (I
->start
< J
->start
) {
2101 Overlaps
= I
->end
> J
->start
;
2103 Overlaps
= J
->end
> I
->start
;
2106 // If so, check value # info to determine if they are really different.
2108 // If the live range overlap will map to the same value number in the
2109 // result liverange, we can still coalesce them. If not, we can't.
2110 if (LHSValNoAssignments
[I
->valno
->id
] !=
2111 RHSValNoAssignments
[J
->valno
->id
])
2115 if (I
->end
< J
->end
) {
2124 // Update kill info. Some live ranges are extended due to copy coalescing.
2125 for (DenseMap
<VNInfo
*, VNInfo
*>::iterator I
= LHSValsDefinedFromRHS
.begin(),
2126 E
= LHSValsDefinedFromRHS
.end(); I
!= E
; ++I
) {
2127 VNInfo
*VNI
= I
->first
;
2128 unsigned LHSValID
= LHSValNoAssignments
[VNI
->id
];
2129 LiveInterval::removeKill(NewVNInfo
[LHSValID
], VNI
->def
);
2130 NewVNInfo
[LHSValID
]->hasPHIKill
|= VNI
->hasPHIKill
;
2131 RHS
.addKills(NewVNInfo
[LHSValID
], VNI
->kills
);
2134 // Update kill info. Some live ranges are extended due to copy coalescing.
2135 for (DenseMap
<VNInfo
*, VNInfo
*>::iterator I
= RHSValsDefinedFromLHS
.begin(),
2136 E
= RHSValsDefinedFromLHS
.end(); I
!= E
; ++I
) {
2137 VNInfo
*VNI
= I
->first
;
2138 unsigned RHSValID
= RHSValNoAssignments
[VNI
->id
];
2139 LiveInterval::removeKill(NewVNInfo
[RHSValID
], VNI
->def
);
2140 NewVNInfo
[RHSValID
]->hasPHIKill
|= VNI
->hasPHIKill
;
2141 LHS
.addKills(NewVNInfo
[RHSValID
], VNI
->kills
);
2144 // If we get here, we know that we can coalesce the live ranges. Ask the
2145 // intervals to coalesce themselves now.
2146 if ((RHS
.ranges
.size() > LHS
.ranges
.size() &&
2147 TargetRegisterInfo::isVirtualRegister(LHS
.reg
)) ||
2148 TargetRegisterInfo::isPhysicalRegister(RHS
.reg
)) {
2149 RHS
.join(LHS
, &RHSValNoAssignments
[0], &LHSValNoAssignments
[0], NewVNInfo
);
2152 LHS
.join(RHS
, &LHSValNoAssignments
[0], &RHSValNoAssignments
[0], NewVNInfo
);
2159 // DepthMBBCompare - Comparison predicate that sort first based on the loop
2160 // depth of the basic block (the unsigned), and then on the MBB number.
2161 struct DepthMBBCompare
{
2162 typedef std::pair
<unsigned, MachineBasicBlock
*> DepthMBBPair
;
2163 bool operator()(const DepthMBBPair
&LHS
, const DepthMBBPair
&RHS
) const {
2164 if (LHS
.first
> RHS
.first
) return true; // Deeper loops first
2165 return LHS
.first
== RHS
.first
&&
2166 LHS
.second
->getNumber() < RHS
.second
->getNumber();
2171 /// getRepIntervalSize - Returns the size of the interval that represents the
2172 /// specified register.
2174 unsigned JoinPriorityQueue
<SF
>::getRepIntervalSize(unsigned Reg
) {
2175 return Rc
->getRepIntervalSize(Reg
);
2178 /// CopyRecSort::operator - Join priority queue sorting function.
2180 bool CopyRecSort::operator()(CopyRec left
, CopyRec right
) const {
2181 // Inner loops first.
2182 if (left
.LoopDepth
> right
.LoopDepth
)
2184 else if (left
.LoopDepth
== right
.LoopDepth
)
2185 if (left
.isBackEdge
&& !right
.isBackEdge
)
2190 void SimpleRegisterCoalescing::CopyCoalesceInMBB(MachineBasicBlock
*MBB
,
2191 std::vector
<CopyRec
> &TryAgain
) {
2192 DOUT
<< ((Value
*)MBB
->getBasicBlock())->getName() << ":\n";
2194 std::vector
<CopyRec
> VirtCopies
;
2195 std::vector
<CopyRec
> PhysCopies
;
2196 std::vector
<CopyRec
> ImpDefCopies
;
2197 unsigned LoopDepth
= loopInfo
->getLoopDepth(MBB
);
2198 for (MachineBasicBlock::iterator MII
= MBB
->begin(), E
= MBB
->end();
2200 MachineInstr
*Inst
= MII
++;
2202 // If this isn't a copy nor a extract_subreg, we can't join intervals.
2203 unsigned SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
;
2204 if (Inst
->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG
) {
2205 DstReg
= Inst
->getOperand(0).getReg();
2206 SrcReg
= Inst
->getOperand(1).getReg();
2207 } else if (Inst
->getOpcode() == TargetInstrInfo::INSERT_SUBREG
||
2208 Inst
->getOpcode() == TargetInstrInfo::SUBREG_TO_REG
) {
2209 DstReg
= Inst
->getOperand(0).getReg();
2210 SrcReg
= Inst
->getOperand(2).getReg();
2211 } else if (!tii_
->isMoveInstr(*Inst
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
))
2214 bool SrcIsPhys
= TargetRegisterInfo::isPhysicalRegister(SrcReg
);
2215 bool DstIsPhys
= TargetRegisterInfo::isPhysicalRegister(DstReg
);
2217 JoinQueue
->push(CopyRec(Inst
, LoopDepth
, isBackEdgeCopy(Inst
, DstReg
)));
2219 if (li_
->hasInterval(SrcReg
) && li_
->getInterval(SrcReg
).empty())
2220 ImpDefCopies
.push_back(CopyRec(Inst
, 0, false));
2221 else if (SrcIsPhys
|| DstIsPhys
)
2222 PhysCopies
.push_back(CopyRec(Inst
, 0, false));
2224 VirtCopies
.push_back(CopyRec(Inst
, 0, false));
2231 // Try coalescing implicit copies first, followed by copies to / from
2232 // physical registers, then finally copies from virtual registers to
2233 // virtual registers.
2234 for (unsigned i
= 0, e
= ImpDefCopies
.size(); i
!= e
; ++i
) {
2235 CopyRec
&TheCopy
= ImpDefCopies
[i
];
2237 if (!JoinCopy(TheCopy
, Again
))
2239 TryAgain
.push_back(TheCopy
);
2241 for (unsigned i
= 0, e
= PhysCopies
.size(); i
!= e
; ++i
) {
2242 CopyRec
&TheCopy
= PhysCopies
[i
];
2244 if (!JoinCopy(TheCopy
, Again
))
2246 TryAgain
.push_back(TheCopy
);
2248 for (unsigned i
= 0, e
= VirtCopies
.size(); i
!= e
; ++i
) {
2249 CopyRec
&TheCopy
= VirtCopies
[i
];
2251 if (!JoinCopy(TheCopy
, Again
))
2253 TryAgain
.push_back(TheCopy
);
2257 void SimpleRegisterCoalescing::joinIntervals() {
2258 DOUT
<< "********** JOINING INTERVALS ***********\n";
2261 JoinQueue
= new JoinPriorityQueue
<CopyRecSort
>(this);
2263 std::vector
<CopyRec
> TryAgainList
;
2264 if (loopInfo
->empty()) {
2265 // If there are no loops in the function, join intervals in function order.
2266 for (MachineFunction::iterator I
= mf_
->begin(), E
= mf_
->end();
2268 CopyCoalesceInMBB(I
, TryAgainList
);
2270 // Otherwise, join intervals in inner loops before other intervals.
2271 // Unfortunately we can't just iterate over loop hierarchy here because
2272 // there may be more MBB's than BB's. Collect MBB's for sorting.
2274 // Join intervals in the function prolog first. We want to join physical
2275 // registers with virtual registers before the intervals got too long.
2276 std::vector
<std::pair
<unsigned, MachineBasicBlock
*> > MBBs
;
2277 for (MachineFunction::iterator I
= mf_
->begin(), E
= mf_
->end();I
!= E
;++I
){
2278 MachineBasicBlock
*MBB
= I
;
2279 MBBs
.push_back(std::make_pair(loopInfo
->getLoopDepth(MBB
), I
));
2282 // Sort by loop depth.
2283 std::sort(MBBs
.begin(), MBBs
.end(), DepthMBBCompare());
2285 // Finally, join intervals in loop nest order.
2286 for (unsigned i
= 0, e
= MBBs
.size(); i
!= e
; ++i
)
2287 CopyCoalesceInMBB(MBBs
[i
].second
, TryAgainList
);
2290 // Joining intervals can allow other intervals to be joined. Iteratively join
2291 // until we make no progress.
2293 SmallVector
<CopyRec
, 16> TryAgain
;
2294 bool ProgressMade
= true;
2295 while (ProgressMade
) {
2296 ProgressMade
= false;
2297 while (!JoinQueue
->empty()) {
2298 CopyRec R
= JoinQueue
->pop();
2300 bool Success
= JoinCopy(R
, Again
);
2302 ProgressMade
= true;
2304 TryAgain
.push_back(R
);
2308 while (!TryAgain
.empty()) {
2309 JoinQueue
->push(TryAgain
.back());
2310 TryAgain
.pop_back();
2315 bool ProgressMade
= true;
2316 while (ProgressMade
) {
2317 ProgressMade
= false;
2319 for (unsigned i
= 0, e
= TryAgainList
.size(); i
!= e
; ++i
) {
2320 CopyRec
&TheCopy
= TryAgainList
[i
];
2323 bool Success
= JoinCopy(TheCopy
, Again
);
2324 if (Success
|| !Again
) {
2325 TheCopy
.MI
= 0; // Mark this one as done.
2326 ProgressMade
= true;
2337 /// Return true if the two specified registers belong to different register
2338 /// classes. The registers may be either phys or virt regs.
2340 SimpleRegisterCoalescing::differingRegisterClasses(unsigned RegA
,
2341 unsigned RegB
) const {
2342 // Get the register classes for the first reg.
2343 if (TargetRegisterInfo::isPhysicalRegister(RegA
)) {
2344 assert(TargetRegisterInfo::isVirtualRegister(RegB
) &&
2345 "Shouldn't consider two physregs!");
2346 return !mri_
->getRegClass(RegB
)->contains(RegA
);
2349 // Compare against the regclass for the second reg.
2350 const TargetRegisterClass
*RegClassA
= mri_
->getRegClass(RegA
);
2351 if (TargetRegisterInfo::isVirtualRegister(RegB
)) {
2352 const TargetRegisterClass
*RegClassB
= mri_
->getRegClass(RegB
);
2353 return RegClassA
!= RegClassB
;
2355 return !RegClassA
->contains(RegB
);
2358 /// lastRegisterUse - Returns the last use of the specific register between
2359 /// cycles Start and End or NULL if there are no uses.
2361 SimpleRegisterCoalescing::lastRegisterUse(unsigned Start
, unsigned End
,
2362 unsigned Reg
, unsigned &UseIdx
) const{
2364 if (TargetRegisterInfo::isVirtualRegister(Reg
)) {
2365 MachineOperand
*LastUse
= NULL
;
2366 for (MachineRegisterInfo::use_iterator I
= mri_
->use_begin(Reg
),
2367 E
= mri_
->use_end(); I
!= E
; ++I
) {
2368 MachineOperand
&Use
= I
.getOperand();
2369 MachineInstr
*UseMI
= Use
.getParent();
2370 unsigned SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
;
2371 if (tii_
->isMoveInstr(*UseMI
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
) &&
2373 // Ignore identity copies.
2375 unsigned Idx
= li_
->getInstructionIndex(UseMI
);
2376 if (Idx
>= Start
&& Idx
< End
&& Idx
>= UseIdx
) {
2378 UseIdx
= li_
->getUseIndex(Idx
);
2384 int e
= (End
-1) / InstrSlots::NUM
* InstrSlots::NUM
;
2387 // Skip deleted instructions
2388 MachineInstr
*MI
= li_
->getInstructionFromIndex(e
);
2389 while ((e
- InstrSlots::NUM
) >= s
&& !MI
) {
2390 e
-= InstrSlots::NUM
;
2391 MI
= li_
->getInstructionFromIndex(e
);
2393 if (e
< s
|| MI
== NULL
)
2396 // Ignore identity copies.
2397 unsigned SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
;
2398 if (!(tii_
->isMoveInstr(*MI
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
) &&
2400 for (unsigned i
= 0, NumOps
= MI
->getNumOperands(); i
!= NumOps
; ++i
) {
2401 MachineOperand
&Use
= MI
->getOperand(i
);
2402 if (Use
.isReg() && Use
.isUse() && Use
.getReg() &&
2403 tri_
->regsOverlap(Use
.getReg(), Reg
)) {
2404 UseIdx
= li_
->getUseIndex(e
);
2409 e
-= InstrSlots::NUM
;
2416 void SimpleRegisterCoalescing::printRegName(unsigned reg
) const {
2417 if (TargetRegisterInfo::isPhysicalRegister(reg
))
2418 cerr
<< tri_
->getName(reg
);
2420 cerr
<< "%reg" << reg
;
2423 void SimpleRegisterCoalescing::releaseMemory() {
2424 JoinedCopies
.clear();
2425 ReMatCopies
.clear();
2429 static bool isZeroLengthInterval(LiveInterval
*li
) {
2430 for (LiveInterval::Ranges::const_iterator
2431 i
= li
->ranges
.begin(), e
= li
->ranges
.end(); i
!= e
; ++i
)
2432 if (i
->end
- i
->start
> LiveIntervals::InstrSlots::NUM
)
2437 /// TurnCopyIntoImpDef - If source of the specified copy is an implicit def,
2438 /// turn the copy into an implicit def.
2440 SimpleRegisterCoalescing::TurnCopyIntoImpDef(MachineBasicBlock::iterator
&I
,
2441 MachineBasicBlock
*MBB
,
2442 unsigned DstReg
, unsigned SrcReg
) {
2443 MachineInstr
*CopyMI
= &*I
;
2444 unsigned CopyIdx
= li_
->getDefIndex(li_
->getInstructionIndex(CopyMI
));
2445 if (!li_
->hasInterval(SrcReg
))
2447 LiveInterval
&SrcInt
= li_
->getInterval(SrcReg
);
2448 if (!SrcInt
.empty())
2450 if (!li_
->hasInterval(DstReg
))
2452 LiveInterval
&DstInt
= li_
->getInterval(DstReg
);
2453 const LiveRange
*DstLR
= DstInt
.getLiveRangeContaining(CopyIdx
);
2454 DstInt
.removeValNo(DstLR
->valno
);
2455 CopyMI
->setDesc(tii_
->get(TargetInstrInfo::IMPLICIT_DEF
));
2456 for (int i
= CopyMI
->getNumOperands() - 1, e
= 0; i
> e
; --i
)
2457 CopyMI
->RemoveOperand(i
);
2458 bool NoUse
= mri_
->use_empty(SrcReg
);
2460 for (MachineRegisterInfo::reg_iterator I
= mri_
->reg_begin(SrcReg
),
2461 E
= mri_
->reg_end(); I
!= E
; ) {
2462 assert(I
.getOperand().isDef());
2463 MachineInstr
*DefMI
= &*I
;
2465 // The implicit_def source has no other uses, delete it.
2466 assert(DefMI
->getOpcode() == TargetInstrInfo::IMPLICIT_DEF
);
2467 li_
->RemoveMachineInstrFromMaps(DefMI
);
2468 DefMI
->eraseFromParent();
2476 bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction
&fn
) {
2478 mri_
= &fn
.getRegInfo();
2479 tm_
= &fn
.getTarget();
2480 tri_
= tm_
->getRegisterInfo();
2481 tii_
= tm_
->getInstrInfo();
2482 li_
= &getAnalysis
<LiveIntervals
>();
2483 loopInfo
= &getAnalysis
<MachineLoopInfo
>();
2485 DOUT
<< "********** SIMPLE REGISTER COALESCING **********\n"
2486 << "********** Function: "
2487 << ((Value
*)mf_
->getFunction())->getName() << '\n';
2489 allocatableRegs_
= tri_
->getAllocatableSet(fn
);
2490 for (TargetRegisterInfo::regclass_iterator I
= tri_
->regclass_begin(),
2491 E
= tri_
->regclass_end(); I
!= E
; ++I
)
2492 allocatableRCRegs_
.insert(std::make_pair(*I
,
2493 tri_
->getAllocatableSet(fn
, *I
)));
2495 // Join (coalesce) intervals if requested.
2496 if (EnableJoining
) {
2499 DOUT
<< "********** INTERVALS POST JOINING **********\n";
2500 for (LiveIntervals::iterator I
= li_
->begin(), E
= li_
->end(); I
!= E
; ++I
){
2501 I
->second
->print(DOUT
, tri_
);
2507 // Perform a final pass over the instructions and compute spill weights
2508 // and remove identity moves.
2509 SmallVector
<unsigned, 4> DeadDefs
;
2510 for (MachineFunction::iterator mbbi
= mf_
->begin(), mbbe
= mf_
->end();
2511 mbbi
!= mbbe
; ++mbbi
) {
2512 MachineBasicBlock
* mbb
= mbbi
;
2513 unsigned loopDepth
= loopInfo
->getLoopDepth(mbb
);
2515 for (MachineBasicBlock::iterator mii
= mbb
->begin(), mie
= mbb
->end();
2517 MachineInstr
*MI
= mii
;
2518 unsigned SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
;
2519 if (JoinedCopies
.count(MI
)) {
2520 // Delete all coalesced copies.
2521 if (!tii_
->isMoveInstr(*MI
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
)) {
2522 assert((MI
->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG
||
2523 MI
->getOpcode() == TargetInstrInfo::INSERT_SUBREG
||
2524 MI
->getOpcode() == TargetInstrInfo::SUBREG_TO_REG
) &&
2525 "Unrecognized copy instruction");
2526 DstReg
= MI
->getOperand(0).getReg();
2528 if (MI
->registerDefIsDead(DstReg
)) {
2529 LiveInterval
&li
= li_
->getInterval(DstReg
);
2530 if (!ShortenDeadCopySrcLiveRange(li
, MI
))
2531 ShortenDeadCopyLiveRange(li
, MI
);
2533 li_
->RemoveMachineInstrFromMaps(MI
);
2534 mii
= mbbi
->erase(mii
);
2539 // Now check if this is a remat'ed def instruction which is now dead.
2540 if (ReMatDefs
.count(MI
)) {
2542 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
2543 const MachineOperand
&MO
= MI
->getOperand(i
);
2546 unsigned Reg
= MO
.getReg();
2549 if (TargetRegisterInfo::isVirtualRegister(Reg
))
2550 DeadDefs
.push_back(Reg
);
2553 if (TargetRegisterInfo::isPhysicalRegister(Reg
) ||
2554 !mri_
->use_empty(Reg
)) {
2560 while (!DeadDefs
.empty()) {
2561 unsigned DeadDef
= DeadDefs
.back();
2562 DeadDefs
.pop_back();
2563 RemoveDeadDef(li_
->getInterval(DeadDef
), MI
);
2565 li_
->RemoveMachineInstrFromMaps(mii
);
2566 mii
= mbbi
->erase(mii
);
2572 // If the move will be an identity move delete it
2573 bool isMove
= tii_
->isMoveInstr(*MI
, SrcReg
, DstReg
, SrcSubIdx
, DstSubIdx
);
2574 if (isMove
&& SrcReg
== DstReg
) {
2575 if (li_
->hasInterval(SrcReg
)) {
2576 LiveInterval
&RegInt
= li_
->getInterval(SrcReg
);
2577 // If def of this move instruction is dead, remove its live range
2578 // from the dstination register's live interval.
2579 if (MI
->registerDefIsDead(DstReg
)) {
2580 if (!ShortenDeadCopySrcLiveRange(RegInt
, MI
))
2581 ShortenDeadCopyLiveRange(RegInt
, MI
);
2584 li_
->RemoveMachineInstrFromMaps(MI
);
2585 mii
= mbbi
->erase(mii
);
2587 } else if (!isMove
|| !TurnCopyIntoImpDef(mii
, mbb
, DstReg
, SrcReg
)) {
2588 SmallSet
<unsigned, 4> UniqueUses
;
2589 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
2590 const MachineOperand
&mop
= MI
->getOperand(i
);
2591 if (mop
.isReg() && mop
.getReg() &&
2592 TargetRegisterInfo::isVirtualRegister(mop
.getReg())) {
2593 unsigned reg
= mop
.getReg();
2594 // Multiple uses of reg by the same instruction. It should not
2595 // contribute to spill weight again.
2596 if (UniqueUses
.count(reg
) != 0)
2598 LiveInterval
&RegInt
= li_
->getInterval(reg
);
2600 li_
->getSpillWeight(mop
.isDef(), mop
.isUse(), loopDepth
);
2601 UniqueUses
.insert(reg
);
2609 for (LiveIntervals::iterator I
= li_
->begin(), E
= li_
->end(); I
!= E
; ++I
) {
2610 LiveInterval
&LI
= *I
->second
;
2611 if (TargetRegisterInfo::isVirtualRegister(LI
.reg
)) {
2612 // If the live interval length is essentially zero, i.e. in every live
2613 // range the use follows def immediately, it doesn't make sense to spill
2614 // it and hope it will be easier to allocate for this li.
2615 if (isZeroLengthInterval(&LI
))
2616 LI
.weight
= HUGE_VALF
;
2618 bool isLoad
= false;
2619 SmallVector
<LiveInterval
*, 4> SpillIs
;
2620 if (li_
->isReMaterializable(LI
, SpillIs
, isLoad
)) {
2621 // If all of the definitions of the interval are re-materializable,
2622 // it is a preferred candidate for spilling. If non of the defs are
2623 // loads, then it's potentially very cheap to re-materialize.
2624 // FIXME: this gets much more complicated once we support non-trivial
2625 // re-materialization.
2633 // Slightly prefer live interval that has been assigned a preferred reg.
2637 // Divide the weight of the interval by its size. This encourages
2638 // spilling of intervals that are large and have few uses, and
2639 // discourages spilling of small intervals with many uses.
2640 LI
.weight
/= li_
->getApproximateInstructionCount(LI
) * InstrSlots::NUM
;
2648 /// print - Implement the dump method.
2649 void SimpleRegisterCoalescing::print(std::ostream
&O
, const Module
* m
) const {
2653 RegisterCoalescer
* llvm::createSimpleRegisterCoalescer() {
2654 return new SimpleRegisterCoalescing();
2657 // Make sure that anything that uses RegisterCoalescer pulls in this file...
2658 DEFINING_FILE_FOR(SimpleRegisterCoalescing
)