blob: c774f935e63d228bb128661d323beb7fda943e32 [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"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000015#include "llvm/Function.h"
Chris Lattner7471a7b2002-02-05 03:35:53 +000016#include "Support/SetOperations.h"
Chris Lattner697954c2002-01-20 22:54:45 +000017using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000018
Chris Lattner9d4ed152003-01-15 21:14:01 +000019unsigned LiveRange::getRegClassID() const { return getRegClass()->getID(); }
20
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000021LiveRangeInfo::LiveRangeInfo(const Function *F, const TargetMachine &tm,
Chris Lattner697954c2002-01-20 22:54:45 +000022 std::vector<RegClass *> &RCL)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000023 : Meth(F), TM(tm), RegClassList(RCL), MRI(tm.getRegInfo()) { }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000024
25
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000026LiveRangeInfo::~LiveRangeInfo() {
Chris Lattner2f898d22002-02-05 06:02:59 +000027 for (LiveRangeMapType::iterator MI = LiveRangeMap.begin();
28 MI != LiveRangeMap.end(); ++MI) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000029
Chris Lattner697954c2002-01-20 22:54:45 +000030 if (MI->first && MI->second) {
31 LiveRange *LR = MI->second;
32
33 // we need to be careful in deleting LiveRanges in LiveRangeMap
34 // since two/more Values in the live range map can point to the same
35 // live range. We have to make the other entries NULL when we delete
36 // a live range.
37
Chris Lattnercb6b4bd2002-10-29 16:51:05 +000038 for (LiveRange::iterator LI = LR->begin(); LI != LR->end(); ++LI)
Chris Lattner697954c2002-01-20 22:54:45 +000039 LiveRangeMap[*LI] = 0;
40
41 delete LR;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000042 }
43 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000044}
45
46
47//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000048// union two live ranges into one. The 2nd LR is deleted. Used for coalescing.
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000049// Note: the caller must make sure that L1 and L2 are distinct and both
50// LRs don't have suggested colors
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000051//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000052
Chris Lattner296b7732002-02-05 02:52:05 +000053void LiveRangeInfo::unionAndUpdateLRs(LiveRange *L1, LiveRange *L2) {
54 assert(L1 != L2 && (!L1->hasSuggestedColor() || !L2->hasSuggestedColor()));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000055 assert(! (L1->hasColor() && L2->hasColor()) ||
56 L1->getColor() == L2->getColor());
57
Chris Lattner296b7732002-02-05 02:52:05 +000058 set_union(*L1, *L2); // add elements of L2 to L1
Ruchira Sasanka8e604792001-09-14 21:18:34 +000059
Chris Lattner296b7732002-02-05 02:52:05 +000060 for(ValueSet::iterator L2It = L2->begin(); L2It != L2->end(); ++L2It) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000061 //assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types");
62
Chris Lattner30adeb62002-02-04 16:36:59 +000063 L1->insert(*L2It); // add the var in L2 to L1
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000064 LiveRangeMap[*L2It] = L1; // now the elements in L2 should map
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000065 //to L1
Ruchira Sasanka8e604792001-09-14 21:18:34 +000066 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000067
68 // set call interference for L1 from L2
Chris Lattner296b7732002-02-05 02:52:05 +000069 if (L2->isCallInterference())
Ruchira Sasanka958faf32001-10-19 17:21:03 +000070 L1->setCallInterference();
71
Chris Lattner296b7732002-02-05 02:52:05 +000072 // add the spill costs
73 L1->addSpillCost(L2->getSpillCost());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000074
75 // If L2 has a color, give L1 that color. Note that L1 may have had the same
76 // color or none, but would not have a different color as asserted above.
77 if (L2->hasColor())
78 L1->setColor(L2->getColor());
79
80 // Similarly, if LROfUse(L2) has a suggested color, the new range
81 // must have the same color.
82 if (L2->hasSuggestedColor())
83 L1->setSuggestedColor(L2->getSuggestedColor());
Chris Lattner296b7732002-02-05 02:52:05 +000084
Chris Lattner697954c2002-01-20 22:54:45 +000085 delete L2; // delete L2 as it is no longer needed
Ruchira Sasanka8e604792001-09-14 21:18:34 +000086}
87
88
Vikram S. Adve9d67cd12002-09-28 17:05:22 +000089//---------------------------------------------------------------------------
90// Method for creating a single live range for a definition.
91// The definition must be represented by a virtual register (a Value).
92// Note: this function does *not* check that no live range exists for def.
93//---------------------------------------------------------------------------
94
95LiveRange*
96LiveRangeInfo::createNewLiveRange(const Value* Def, bool isCC /* = false*/)
97{
98 LiveRange* DefRange = new LiveRange(); // Create a new live range,
99 DefRange->insert(Def); // add Def to it,
100 LiveRangeMap[Def] = DefRange; // and update the map.
101
102 // set the register class of the new live range
Chris Lattner9d4ed152003-01-15 21:14:01 +0000103 DefRange->setRegClass(RegClassList[MRI.getRegClassIDOfType(Def->getType(),
104 isCC)]);
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000105
106 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
107 cerr << " Creating a LR for def ";
108 if (isCC) cerr << " (CC Register!)";
109 cerr << " : " << RAV(Def) << "\n";
110 }
111 return DefRange;
112}
113
114
115LiveRange*
116LiveRangeInfo::createOrAddToLiveRange(const Value* Def, bool isCC /* = false*/)
117{
118 LiveRange *DefRange = LiveRangeMap[Def];
119
120 // check if the LR is already there (because of multiple defs)
121 if (!DefRange) {
Chris Lattner133f0792002-10-28 04:45:29 +0000122 DefRange = createNewLiveRange(Def, isCC);
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000123 } else { // live range already exists
124 DefRange->insert(Def); // add the operand to the range
125 LiveRangeMap[Def] = DefRange; // make operand point to merged set
126 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
127 cerr << " Added to existing LR for def: " << RAV(Def) << "\n";
128 }
129 return DefRange;
130}
131
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000132
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000133//---------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000134// Method for constructing all live ranges in a function. It creates live
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000135// ranges for all values defined in the instruction stream. Also, it
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000136// creates live ranges for all incoming arguments of the function.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000137//---------------------------------------------------------------------------
Chris Lattner2f898d22002-02-05 06:02:59 +0000138void LiveRangeInfo::constructLiveRanges() {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000140 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
141 cerr << "Constructing Live Ranges ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000143 // first find the live ranges for all incoming args of the function since
144 // those LRs start from the start of the function
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000145 for (Function::const_aiterator AI = Meth->abegin(); AI != Meth->aend(); ++AI)
Chris Lattner133f0792002-10-28 04:45:29 +0000146 createNewLiveRange(AI, /*isCC*/ false);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000147
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000148 // Now suggest hardware registers for these function args
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000149 MRI.suggestRegs4MethodArgs(Meth, *this);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000150
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000151 // Now create LRs for machine instructions. A new LR will be created
152 // only for defs in the machine instr since, we assume that all Values are
153 // defined before they are used. However, there can be multiple defs for
154 // the same Value in machine instructions.
155 //
156 // Also, find CALL and RETURN instructions, which need extra work.
Chris Lattner2f898d22002-02-05 06:02:59 +0000157 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000158 MachineFunction &MF = MachineFunction::get(Meth);
159 for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
160 MachineBasicBlock &MBB = *BBI;
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000161
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000162 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000163 for(MachineBasicBlock::iterator MInstIterator = MBB.begin();
164 MInstIterator != MBB.end(); ++MInstIterator) {
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000165 MachineInstr *MInst = *MInstIterator;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000166
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000167 // If the machine instruction is a call/return instruction, add it to
168 // CallRetInstrList for processing its args, ret value, and ret addr.
169 //
Chris Lattner697954c2002-01-20 22:54:45 +0000170 if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ||
171 TM.getInstrInfo().isCall(MInst->getOpCode()))
Chris Lattner133f0792002-10-28 04:45:29 +0000172 CallRetInstrList.push_back(MInst);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000173
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000174 // iterate over explicit MI operands and create a new LR
175 // for each operand that is defined by the instruction
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000176 for (MachineInstr::val_op_iterator OpI = MInst->begin(),
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000177 OpE = MInst->end(); OpI != OpE; ++OpI)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000178 if (OpI.isDefOnly() || OpI.isDefAndUse()) {
Chris Lattner30adeb62002-02-04 16:36:59 +0000179 const Value *Def = *OpI;
Chris Lattner133f0792002-10-28 04:45:29 +0000180 bool isCC = (OpI.getMachineOperand().getType()
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000181 == MachineOperand::MO_CCRegister);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000182 LiveRange* LR = createOrAddToLiveRange(Def, isCC);
183
184 // If the operand has a pre-assigned register,
185 // set it directly in the LiveRange
186 if (OpI.getMachineOperand().hasAllocatedReg()) {
187 unsigned getClassId;
188 LR->setColor(MRI.getClassRegNum(
189 OpI.getMachineOperand().getAllocatedRegNum(),
190 getClassId));
191 }
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000192 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000193
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000194 // iterate over implicit MI operands and create a new LR
195 // for each operand that is defined by the instruction
196 for (unsigned i = 0; i < MInst->getNumImplicitRefs(); ++i)
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000197 if (MInst->getImplicitOp(i).opIsDefOnly() ||
198 MInst->getImplicitOp(i).opIsDefAndUse()) {
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000199 const Value *Def = MInst->getImplicitRef(i);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000200 LiveRange* LR = createOrAddToLiveRange(Def, /*isCC*/ false);
201
202 // If the implicit operand has a pre-assigned register,
203 // set it directly in the LiveRange
204 if (MInst->getImplicitOp(i).hasAllocatedReg()) {
205 unsigned getClassId;
206 LR->setColor(MRI.getClassRegNum(
207 MInst->getImplicitOp(i).getAllocatedRegNum(),
208 getClassId));
209 }
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000210 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000211
212 } // for all machine instructions in the BB
213
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000214 } // for all BBs in function
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000215
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000216 // Now we have to suggest clors for call and return arg live ranges.
217 // Also, if there are implicit defs (e.g., retun value of a call inst)
218 // they must be added to the live range list
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000219 //
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000220 suggestRegs4CallRets();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000221
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000222 if( DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattner697954c2002-01-20 22:54:45 +0000223 cerr << "Initial Live Ranges constructed!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000224}
225
226
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000227//---------------------------------------------------------------------------
228// If some live ranges must be colored with specific hardware registers
229// (e.g., for outgoing call args), suggesting of colors for such live
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000230// ranges is done using target specific function. Those functions are called
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000231// from this function. The target specific methods must:
232// 1) suggest colors for call and return args.
233// 2) create new LRs for implicit defs in machine instructions
234//---------------------------------------------------------------------------
Chris Lattner88da77c2002-10-29 17:03:19 +0000235void LiveRangeInfo::suggestRegs4CallRets() {
236 std::vector<MachineInstr*>::iterator It = CallRetInstrList.begin();
237 for( ; It != CallRetInstrList.end(); ++It) {
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000238 MachineInstr *MInst = *It;
Chris Lattner88da77c2002-10-29 17:03:19 +0000239 MachineOpCode OpCode = MInst->getOpCode();
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000240
Chris Lattner88da77c2002-10-29 17:03:19 +0000241 if ((TM.getInstrInfo()).isReturn(OpCode))
242 MRI.suggestReg4RetValue(MInst, *this);
243 else if ((TM.getInstrInfo()).isCall(OpCode))
244 MRI.suggestRegs4CallArgs(MInst, *this);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000245 else
Chris Lattner88da77c2002-10-29 17:03:19 +0000246 assert( 0 && "Non call/ret instr in CallRetInstrList" );
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000247 }
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000248}
249
250
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000251//--------------------------------------------------------------------------
252// The following method coalesces live ranges when possible. This method
253// must be called after the interference graph has been constructed.
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000254
255
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000256/* Algorithm:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000257 for each BB in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000258 for each machine instruction (inst)
259 for each definition (def) in inst
260 for each operand (op) of inst that is a use
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000261 if the def and op are of the same register type
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000262 if the def and op do not interfere //i.e., not simultaneously live
263 if (degree(LR of def) + degree(LR of op)) <= # avail regs
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000264 if both LRs do not have suggested colors
265 merge2IGNodes(def, op) // i.e., merge 2 LRs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000266
267*/
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000268//---------------------------------------------------------------------------
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000269
270
271// Checks if live range LR interferes with any node assigned or suggested to
272// be assigned the specified color
273//
274inline bool InterferesWithColor(const LiveRange& LR, unsigned color)
275{
276 IGNode* lrNode = LR.getUserIGNode();
277 for (unsigned n=0, NN = lrNode->getNumOfNeighbors(); n < NN; n++) {
278 LiveRange *neighLR = lrNode->getAdjIGNode(n)->getParentLR();
279 if (neighLR->hasColor() && neighLR->getColor() == color)
280 return true;
281 if (neighLR->hasSuggestedColor() && neighLR->getSuggestedColor() == color)
282 return true;
283 }
284 return false;
285}
286
287// Cannot coalesce if any of the following is true:
288// (1) Both LRs have suggested colors (should be "different suggested colors"?)
289// (2) Both LR1 and LR2 have colors and the colors are different
290// (but if the colors are the same, it is definitely safe to coalesce)
291// (3) LR1 has color and LR2 interferes with any LR that has the same color
292// (4) LR2 has color and LR1 interferes with any LR that has the same color
293//
294inline bool InterfsPreventCoalescing(const LiveRange& LROfDef,
295 const LiveRange& LROfUse)
296{
297 // (4) if they have different suggested colors, cannot coalesce
298 if (LROfDef.hasSuggestedColor() && LROfUse.hasSuggestedColor())
299 return true;
300
301 // if neither has a color, nothing more to do.
302 if (! LROfDef.hasColor() && ! LROfUse.hasColor())
303 return false;
304
305 // (2, 3) if L1 has color...
306 if (LROfDef.hasColor()) {
307 if (LROfUse.hasColor())
308 return (LROfUse.getColor() != LROfDef.getColor());
309 return InterferesWithColor(LROfUse, LROfDef.getColor());
310 }
311
312 // (4) else only LROfUse has a color: check if that could interfere
313 return InterferesWithColor(LROfDef, LROfUse.getColor());
314}
315
316
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000317void LiveRangeInfo::coalesceLRs()
318{
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000319 if(DEBUG_RA >= RA_DEBUG_LiveRanges)
320 cerr << "\nCoalescing LRs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000321
Chris Lattnerf726e772002-10-28 19:22:04 +0000322 MachineFunction &MF = MachineFunction::get(Meth);
323 for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
324 MachineBasicBlock &MBB = *BBI;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000325
326 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000327 for(MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII){
328 const MachineInstr *MI = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000329
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000330 if( DEBUG_RA >= RA_DEBUG_LiveRanges) {
Chris Lattner697954c2002-01-20 22:54:45 +0000331 cerr << " *Iterating over machine instr ";
Chris Lattnerf726e772002-10-28 19:22:04 +0000332 MI->dump();
Chris Lattner697954c2002-01-20 22:54:45 +0000333 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000334 }
335
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000336 // iterate over MI operands to find defs
Chris Lattnerf726e772002-10-28 19:22:04 +0000337 for(MachineInstr::const_val_op_iterator DefI = MI->begin(),
338 DefE = MI->end(); DefI != DefE; ++DefI) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000339 if (DefI.isDefOnly() || DefI.isDefAndUse()) { // this operand is modified
Chris Lattner2f898d22002-02-05 06:02:59 +0000340 LiveRange *LROfDef = getLiveRangeForValue( *DefI );
341 RegClass *RCOfDef = LROfDef->getRegClass();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000342
Chris Lattnerf726e772002-10-28 19:22:04 +0000343 MachineInstr::const_val_op_iterator UseI = MI->begin(),
344 UseE = MI->end();
345 for( ; UseI != UseE; ++UseI) { // for all uses
Chris Lattner2f898d22002-02-05 06:02:59 +0000346 LiveRange *LROfUse = getLiveRangeForValue( *UseI );
347 if (!LROfUse) { // if LR of use is not found
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000348 //don't warn about labels
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000349 if (!isa<BasicBlock>(*UseI) && DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000350 cerr << " !! Warning: No LR for use " << RAV(*UseI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000351 continue; // ignore and continue
352 }
353
Chris Lattner2f898d22002-02-05 06:02:59 +0000354 if (LROfUse == LROfDef) // nothing to merge if they are same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000355 continue;
356
Chris Lattner37730942002-02-05 03:52:29 +0000357 if (MRI.getRegType(LROfDef) == MRI.getRegType(LROfUse)) {
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000358 // If the two RegTypes are the same
Chris Lattner37730942002-02-05 03:52:29 +0000359 if (!RCOfDef->getInterference(LROfDef, LROfUse) ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000360
361 unsigned CombinedDegree =
362 LROfDef->getUserIGNode()->getNumOfNeighbors() +
363 LROfUse->getUserIGNode()->getNumOfNeighbors();
364
Vikram S. Adve32f81a32002-09-20 00:45:47 +0000365 if (CombinedDegree > RCOfDef->getNumOfAvailRegs()) {
366 // get more precise estimate of combined degree
367 CombinedDegree = LROfDef->getUserIGNode()->
368 getCombinedDegree(LROfUse->getUserIGNode());
369 }
370
Chris Lattner37730942002-02-05 03:52:29 +0000371 if (CombinedDegree <= RCOfDef->getNumOfAvailRegs()) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000372 // if both LRs do not have different pre-assigned colors
373 // and both LRs do not have suggested colors
374 if (! InterfsPreventCoalescing(*LROfDef, *LROfUse)) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000375 RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
376 unionAndUpdateLRs(LROfDef, LROfUse);
377 }
378
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000379 } // if combined degree is less than # of regs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000380 } // if def and use do not interfere
Ruchira Sasankad33238b2001-10-12 17:48:18 +0000381 }// if reg classes are the same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000382 } // for all uses
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000383 } // if def
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000384 } // for all defs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000385 } // for all machine instructions
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000386 } // for all BBs
387
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000388 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
389 cerr << "\nCoalescing Done!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000390}
391
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000392/*--------------------------- Debug code for printing ---------------*/
393
394
Chris Lattner0665a5f2002-02-05 01:43:49 +0000395void LiveRangeInfo::printLiveRanges() {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000396 LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
Chris Lattner697954c2002-01-20 22:54:45 +0000397 cerr << "\nPrinting Live Ranges from Hash Map:\n";
Chris Lattner0665a5f2002-02-05 01:43:49 +0000398 for( ; HMI != LiveRangeMap.end(); ++HMI) {
399 if (HMI->first && HMI->second) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000400 cerr << " Value* " << RAV(HMI->first) << "\t: ";
Vikram S. Adve32f81a32002-09-20 00:45:47 +0000401 if (IGNode* igNode = HMI->second->getUserIGNode())
402 cerr << "LR# " << igNode->getIndex();
403 else
404 cerr << "LR# " << "<no-IGNode>";
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000405 cerr << "\t:Values = "; printSet(*HMI->second); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000406 }
407 }
408}