blob: d8682e0b5ae7a8db583859859bf3875d5b4d550a [file] [log] [blame]
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001//===-- LiveRangeInfo.cpp -------------------------------------------------===//
2//
3// Live range construction for coloring-based register allocation for LLVM.
4//
5//===----------------------------------------------------------------------===//
6
Ruchira Sasanka8e604792001-09-14 21:18:34 +00007#include "llvm/CodeGen/LiveRangeInfo.h"
Chris Lattner4309e732003-01-15 19:57:07 +00008#include "RegAllocCommon.h"
Chris Lattner9d4ed152003-01-15 21:14:01 +00009#include "RegClass.h"
Chris Lattnercb6b4bd2002-10-29 16:51:05 +000010#include "llvm/CodeGen/IGNode.h"
Chris Lattner0a8ed942002-02-04 05:56:09 +000011#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerf726e772002-10-28 19:22:04 +000012#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner0a8ed942002-02-04 05:56:09 +000013#include "llvm/Target/TargetMachine.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000014#include "llvm/Target/TargetInstrInfo.h"
Vikram S. Advebc001b22003-07-25 21:06:09 +000015#include "llvm/Target/TargetRegInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000016#include "llvm/Function.h"
Chris Lattner7471a7b2002-02-05 03:35:53 +000017#include "Support/SetOperations.h"
Chris Lattner697954c2002-01-20 22:54:45 +000018using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000019
Chris Lattner9d4ed152003-01-15 21:14:01 +000020unsigned LiveRange::getRegClassID() const { return getRegClass()->getID(); }
21
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022LiveRangeInfo::LiveRangeInfo(const Function *F, const TargetMachine &tm,
Chris Lattner697954c2002-01-20 22:54:45 +000023 std::vector<RegClass *> &RCL)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000024 : Meth(F), TM(tm), RegClassList(RCL), MRI(tm.getRegInfo()) { }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000025
26
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000027LiveRangeInfo::~LiveRangeInfo() {
Chris Lattner2f898d22002-02-05 06:02:59 +000028 for (LiveRangeMapType::iterator MI = LiveRangeMap.begin();
29 MI != LiveRangeMap.end(); ++MI) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000030
Chris Lattner697954c2002-01-20 22:54:45 +000031 if (MI->first && MI->second) {
32 LiveRange *LR = MI->second;
33
34 // we need to be careful in deleting LiveRanges in LiveRangeMap
35 // since two/more Values in the live range map can point to the same
36 // live range. We have to make the other entries NULL when we delete
37 // a live range.
38
Chris Lattnercb6b4bd2002-10-29 16:51:05 +000039 for (LiveRange::iterator LI = LR->begin(); LI != LR->end(); ++LI)
Chris Lattner697954c2002-01-20 22:54:45 +000040 LiveRangeMap[*LI] = 0;
41
42 delete LR;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000043 }
44 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000045}
46
47
48//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000049// union two live ranges into one. The 2nd LR is deleted. Used for coalescing.
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000050// Note: the caller must make sure that L1 and L2 are distinct and both
51// LRs don't have suggested colors
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000052//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000053
Chris Lattner296b7732002-02-05 02:52:05 +000054void LiveRangeInfo::unionAndUpdateLRs(LiveRange *L1, LiveRange *L2) {
55 assert(L1 != L2 && (!L1->hasSuggestedColor() || !L2->hasSuggestedColor()));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000056 assert(! (L1->hasColor() && L2->hasColor()) ||
57 L1->getColor() == L2->getColor());
58
Chris Lattner296b7732002-02-05 02:52:05 +000059 set_union(*L1, *L2); // add elements of L2 to L1
Ruchira Sasanka8e604792001-09-14 21:18:34 +000060
Chris Lattner296b7732002-02-05 02:52:05 +000061 for(ValueSet::iterator L2It = L2->begin(); L2It != L2->end(); ++L2It) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000062 //assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types");
63
Chris Lattner30adeb62002-02-04 16:36:59 +000064 L1->insert(*L2It); // add the var in L2 to L1
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000065 LiveRangeMap[*L2It] = L1; // now the elements in L2 should map
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000066 //to L1
Ruchira Sasanka8e604792001-09-14 21:18:34 +000067 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000068
69 // set call interference for L1 from L2
Chris Lattner296b7732002-02-05 02:52:05 +000070 if (L2->isCallInterference())
Ruchira Sasanka958faf32001-10-19 17:21:03 +000071 L1->setCallInterference();
72
Chris Lattner296b7732002-02-05 02:52:05 +000073 // add the spill costs
74 L1->addSpillCost(L2->getSpillCost());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000075
76 // If L2 has a color, give L1 that color. Note that L1 may have had the same
77 // color or none, but would not have a different color as asserted above.
78 if (L2->hasColor())
79 L1->setColor(L2->getColor());
80
81 // Similarly, if LROfUse(L2) has a suggested color, the new range
82 // must have the same color.
83 if (L2->hasSuggestedColor())
84 L1->setSuggestedColor(L2->getSuggestedColor());
Chris Lattner296b7732002-02-05 02:52:05 +000085
Chris Lattner697954c2002-01-20 22:54:45 +000086 delete L2; // delete L2 as it is no longer needed
Ruchira Sasanka8e604792001-09-14 21:18:34 +000087}
88
89
Vikram S. Adve9d67cd12002-09-28 17:05:22 +000090//---------------------------------------------------------------------------
91// Method for creating a single live range for a definition.
92// The definition must be represented by a virtual register (a Value).
93// Note: this function does *not* check that no live range exists for def.
94//---------------------------------------------------------------------------
95
96LiveRange*
97LiveRangeInfo::createNewLiveRange(const Value* Def, bool isCC /* = false*/)
98{
99 LiveRange* DefRange = new LiveRange(); // Create a new live range,
100 DefRange->insert(Def); // add Def to it,
101 LiveRangeMap[Def] = DefRange; // and update the map.
102
103 // set the register class of the new live range
Chris Lattner9d4ed152003-01-15 21:14:01 +0000104 DefRange->setRegClass(RegClassList[MRI.getRegClassIDOfType(Def->getType(),
105 isCC)]);
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000106
107 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
108 cerr << " Creating a LR for def ";
109 if (isCC) cerr << " (CC Register!)";
110 cerr << " : " << RAV(Def) << "\n";
111 }
112 return DefRange;
113}
114
115
116LiveRange*
117LiveRangeInfo::createOrAddToLiveRange(const Value* Def, bool isCC /* = false*/)
118{
119 LiveRange *DefRange = LiveRangeMap[Def];
120
121 // check if the LR is already there (because of multiple defs)
122 if (!DefRange) {
Chris Lattner133f0792002-10-28 04:45:29 +0000123 DefRange = createNewLiveRange(Def, isCC);
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000124 } else { // live range already exists
125 DefRange->insert(Def); // add the operand to the range
126 LiveRangeMap[Def] = DefRange; // make operand point to merged set
127 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
128 cerr << " Added to existing LR for def: " << RAV(Def) << "\n";
129 }
130 return DefRange;
131}
132
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000133
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000134//---------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000135// Method for constructing all live ranges in a function. It creates live
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000136// ranges for all values defined in the instruction stream. Also, it
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000137// creates live ranges for all incoming arguments of the function.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000138//---------------------------------------------------------------------------
Chris Lattner2f898d22002-02-05 06:02:59 +0000139void LiveRangeInfo::constructLiveRanges() {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000141 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
142 cerr << "Constructing Live Ranges ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000143
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000144 // first find the live ranges for all incoming args of the function since
145 // those LRs start from the start of the function
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000146 for (Function::const_aiterator AI = Meth->abegin(); AI != Meth->aend(); ++AI)
Chris Lattner133f0792002-10-28 04:45:29 +0000147 createNewLiveRange(AI, /*isCC*/ false);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000148
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000149 // Now suggest hardware registers for these function args
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000150 MRI.suggestRegs4MethodArgs(Meth, *this);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000151
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000152 // Now create LRs for machine instructions. A new LR will be created
153 // only for defs in the machine instr since, we assume that all Values are
154 // defined before they are used. However, there can be multiple defs for
155 // the same Value in machine instructions.
156 //
157 // Also, find CALL and RETURN instructions, which need extra work.
Chris Lattner2f898d22002-02-05 06:02:59 +0000158 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000159 MachineFunction &MF = MachineFunction::get(Meth);
160 for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
161 MachineBasicBlock &MBB = *BBI;
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000162
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000163 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000164 for(MachineBasicBlock::iterator MInstIterator = MBB.begin();
165 MInstIterator != MBB.end(); ++MInstIterator) {
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000166 MachineInstr *MInst = *MInstIterator;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000167
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000168 // If the machine instruction is a call/return instruction, add it to
169 // CallRetInstrList for processing its args, ret value, and ret addr.
170 //
Chris Lattner697954c2002-01-20 22:54:45 +0000171 if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ||
172 TM.getInstrInfo().isCall(MInst->getOpCode()))
Chris Lattner133f0792002-10-28 04:45:29 +0000173 CallRetInstrList.push_back(MInst);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000174
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000175 // iterate over explicit MI operands and create a new LR
176 // for each operand that is defined by the instruction
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000177 for (MachineInstr::val_op_iterator OpI = MInst->begin(),
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000178 OpE = MInst->end(); OpI != OpE; ++OpI)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000179 if (OpI.isDefOnly() || OpI.isDefAndUse()) {
Chris Lattner30adeb62002-02-04 16:36:59 +0000180 const Value *Def = *OpI;
Chris Lattner133f0792002-10-28 04:45:29 +0000181 bool isCC = (OpI.getMachineOperand().getType()
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000182 == MachineOperand::MO_CCRegister);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000183 LiveRange* LR = createOrAddToLiveRange(Def, isCC);
184
185 // If the operand has a pre-assigned register,
186 // set it directly in the LiveRange
187 if (OpI.getMachineOperand().hasAllocatedReg()) {
188 unsigned getClassId;
189 LR->setColor(MRI.getClassRegNum(
190 OpI.getMachineOperand().getAllocatedRegNum(),
191 getClassId));
192 }
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000193 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000194
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000195 // iterate over implicit MI operands and create a new LR
196 // for each operand that is defined by the instruction
197 for (unsigned i = 0; i < MInst->getNumImplicitRefs(); ++i)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000198 if (MInst->getImplicitOp(i).opIsDefOnly() ||
199 MInst->getImplicitOp(i).opIsDefAndUse()) {
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000200 const Value *Def = MInst->getImplicitRef(i);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000201 LiveRange* LR = createOrAddToLiveRange(Def, /*isCC*/ false);
202
203 // If the implicit operand has a pre-assigned register,
204 // set it directly in the LiveRange
205 if (MInst->getImplicitOp(i).hasAllocatedReg()) {
206 unsigned getClassId;
207 LR->setColor(MRI.getClassRegNum(
208 MInst->getImplicitOp(i).getAllocatedRegNum(),
209 getClassId));
210 }
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000211 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000212
213 } // for all machine instructions in the BB
214
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000215 } // for all BBs in function
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000216
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000217 // Now we have to suggest clors for call and return arg live ranges.
218 // Also, if there are implicit defs (e.g., retun value of a call inst)
219 // they must be added to the live range list
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000220 //
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000221 suggestRegs4CallRets();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000222
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000223 if( DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattner697954c2002-01-20 22:54:45 +0000224 cerr << "Initial Live Ranges constructed!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000225}
226
227
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000228//---------------------------------------------------------------------------
229// If some live ranges must be colored with specific hardware registers
230// (e.g., for outgoing call args), suggesting of colors for such live
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000231// ranges is done using target specific function. Those functions are called
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000232// from this function. The target specific methods must:
233// 1) suggest colors for call and return args.
234// 2) create new LRs for implicit defs in machine instructions
235//---------------------------------------------------------------------------
Chris Lattner88da77c2002-10-29 17:03:19 +0000236void LiveRangeInfo::suggestRegs4CallRets() {
237 std::vector<MachineInstr*>::iterator It = CallRetInstrList.begin();
238 for( ; It != CallRetInstrList.end(); ++It) {
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000239 MachineInstr *MInst = *It;
Chris Lattner88da77c2002-10-29 17:03:19 +0000240 MachineOpCode OpCode = MInst->getOpCode();
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000241
Chris Lattner88da77c2002-10-29 17:03:19 +0000242 if ((TM.getInstrInfo()).isReturn(OpCode))
243 MRI.suggestReg4RetValue(MInst, *this);
244 else if ((TM.getInstrInfo()).isCall(OpCode))
245 MRI.suggestRegs4CallArgs(MInst, *this);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000246 else
Chris Lattner88da77c2002-10-29 17:03:19 +0000247 assert( 0 && "Non call/ret instr in CallRetInstrList" );
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000248 }
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000249}
250
251
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000252//--------------------------------------------------------------------------
253// The following method coalesces live ranges when possible. This method
254// must be called after the interference graph has been constructed.
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000255
256
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000257/* Algorithm:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000258 for each BB in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000259 for each machine instruction (inst)
260 for each definition (def) in inst
261 for each operand (op) of inst that is a use
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000262 if the def and op are of the same register type
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000263 if the def and op do not interfere //i.e., not simultaneously live
264 if (degree(LR of def) + degree(LR of op)) <= # avail regs
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000265 if both LRs do not have suggested colors
266 merge2IGNodes(def, op) // i.e., merge 2 LRs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000267
268*/
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000269//---------------------------------------------------------------------------
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000270
271
272// Checks if live range LR interferes with any node assigned or suggested to
273// be assigned the specified color
274//
275inline bool InterferesWithColor(const LiveRange& LR, unsigned color)
276{
277 IGNode* lrNode = LR.getUserIGNode();
278 for (unsigned n=0, NN = lrNode->getNumOfNeighbors(); n < NN; n++) {
279 LiveRange *neighLR = lrNode->getAdjIGNode(n)->getParentLR();
280 if (neighLR->hasColor() && neighLR->getColor() == color)
281 return true;
282 if (neighLR->hasSuggestedColor() && neighLR->getSuggestedColor() == color)
283 return true;
284 }
285 return false;
286}
287
288// Cannot coalesce if any of the following is true:
289// (1) Both LRs have suggested colors (should be "different suggested colors"?)
290// (2) Both LR1 and LR2 have colors and the colors are different
291// (but if the colors are the same, it is definitely safe to coalesce)
292// (3) LR1 has color and LR2 interferes with any LR that has the same color
293// (4) LR2 has color and LR1 interferes with any LR that has the same color
294//
295inline bool InterfsPreventCoalescing(const LiveRange& LROfDef,
296 const LiveRange& LROfUse)
297{
298 // (4) if they have different suggested colors, cannot coalesce
299 if (LROfDef.hasSuggestedColor() && LROfUse.hasSuggestedColor())
300 return true;
301
302 // if neither has a color, nothing more to do.
303 if (! LROfDef.hasColor() && ! LROfUse.hasColor())
304 return false;
305
306 // (2, 3) if L1 has color...
307 if (LROfDef.hasColor()) {
308 if (LROfUse.hasColor())
309 return (LROfUse.getColor() != LROfDef.getColor());
310 return InterferesWithColor(LROfUse, LROfDef.getColor());
311 }
312
313 // (4) else only LROfUse has a color: check if that could interfere
314 return InterferesWithColor(LROfDef, LROfUse.getColor());
315}
316
317
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000318void LiveRangeInfo::coalesceLRs()
319{
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000320 if(DEBUG_RA >= RA_DEBUG_LiveRanges)
321 cerr << "\nCoalescing LRs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000322
Chris Lattnerf726e772002-10-28 19:22:04 +0000323 MachineFunction &MF = MachineFunction::get(Meth);
324 for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
325 MachineBasicBlock &MBB = *BBI;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000326
327 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000328 for(MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII){
329 const MachineInstr *MI = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000330
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000331 if( DEBUG_RA >= RA_DEBUG_LiveRanges) {
Chris Lattner697954c2002-01-20 22:54:45 +0000332 cerr << " *Iterating over machine instr ";
Chris Lattnerf726e772002-10-28 19:22:04 +0000333 MI->dump();
Chris Lattner697954c2002-01-20 22:54:45 +0000334 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000335 }
336
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000337 // iterate over MI operands to find defs
Chris Lattnerf726e772002-10-28 19:22:04 +0000338 for(MachineInstr::const_val_op_iterator DefI = MI->begin(),
339 DefE = MI->end(); DefI != DefE; ++DefI) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000340 if (DefI.isDefOnly() || DefI.isDefAndUse()) { // this operand is modified
Chris Lattner2f898d22002-02-05 06:02:59 +0000341 LiveRange *LROfDef = getLiveRangeForValue( *DefI );
342 RegClass *RCOfDef = LROfDef->getRegClass();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000343
Chris Lattnerf726e772002-10-28 19:22:04 +0000344 MachineInstr::const_val_op_iterator UseI = MI->begin(),
345 UseE = MI->end();
346 for( ; UseI != UseE; ++UseI) { // for all uses
Chris Lattner2f898d22002-02-05 06:02:59 +0000347 LiveRange *LROfUse = getLiveRangeForValue( *UseI );
348 if (!LROfUse) { // if LR of use is not found
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000349 //don't warn about labels
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000350 if (!isa<BasicBlock>(*UseI) && DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000351 cerr << " !! Warning: No LR for use " << RAV(*UseI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000352 continue; // ignore and continue
353 }
354
Chris Lattner2f898d22002-02-05 06:02:59 +0000355 if (LROfUse == LROfDef) // nothing to merge if they are same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000356 continue;
357
Vikram S. Advebc001b22003-07-25 21:06:09 +0000358 if (MRI.getRegTypeForLR(LROfDef) ==
359 MRI.getRegTypeForLR(LROfUse)) {
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000360 // If the two RegTypes are the same
Chris Lattner37730942002-02-05 03:52:29 +0000361 if (!RCOfDef->getInterference(LROfDef, LROfUse) ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000362
363 unsigned CombinedDegree =
364 LROfDef->getUserIGNode()->getNumOfNeighbors() +
365 LROfUse->getUserIGNode()->getNumOfNeighbors();
366
Vikram S. Adve32f81a32002-09-20 00:45:47 +0000367 if (CombinedDegree > RCOfDef->getNumOfAvailRegs()) {
368 // get more precise estimate of combined degree
369 CombinedDegree = LROfDef->getUserIGNode()->
370 getCombinedDegree(LROfUse->getUserIGNode());
371 }
372
Chris Lattner37730942002-02-05 03:52:29 +0000373 if (CombinedDegree <= RCOfDef->getNumOfAvailRegs()) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000374 // if both LRs do not have different pre-assigned colors
375 // and both LRs do not have suggested colors
376 if (! InterfsPreventCoalescing(*LROfDef, *LROfUse)) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000377 RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
378 unionAndUpdateLRs(LROfDef, LROfUse);
379 }
380
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000381 } // if combined degree is less than # of regs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000382 } // if def and use do not interfere
Ruchira Sasankad33238b2001-10-12 17:48:18 +0000383 }// if reg classes are the same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000384 } // for all uses
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000385 } // if def
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000386 } // for all defs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000387 } // for all machine instructions
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000388 } // for all BBs
389
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000390 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
391 cerr << "\nCoalescing Done!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000392}
393
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000394/*--------------------------- Debug code for printing ---------------*/
395
396
Chris Lattner0665a5f2002-02-05 01:43:49 +0000397void LiveRangeInfo::printLiveRanges() {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000398 LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
Chris Lattner697954c2002-01-20 22:54:45 +0000399 cerr << "\nPrinting Live Ranges from Hash Map:\n";
Chris Lattner0665a5f2002-02-05 01:43:49 +0000400 for( ; HMI != LiveRangeMap.end(); ++HMI) {
401 if (HMI->first && HMI->second) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000402 cerr << " Value* " << RAV(HMI->first) << "\t: ";
Vikram S. Adve32f81a32002-09-20 00:45:47 +0000403 if (IGNode* igNode = HMI->second->getUserIGNode())
404 cerr << "LR# " << igNode->getIndex();
405 else
406 cerr << "LR# " << "<no-IGNode>";
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000407 cerr << "\t:Values = "; printSet(*HMI->second); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000408 }
409 }
410}