blob: 380680448d31e7c9105c8dec4846c052cf61cf32 [file] [log] [blame]
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001//===-- LiveRangeInfo.cpp -------------------------------------------------===//
2//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Vikram S. Adve39c94e12002-09-14 23:05:33 +000010// Live range construction for coloring-based register allocation for LLVM.
11//
12//===----------------------------------------------------------------------===//
13
Misha Brukman0d82a542003-10-23 18:03:50 +000014#include "IGNode.h"
Chris Lattner893f9542003-09-01 20:12:17 +000015#include "LiveRangeInfo.h"
Chris Lattner4309e732003-01-15 19:57:07 +000016#include "RegAllocCommon.h"
Chris Lattner9d4ed152003-01-15 21:14:01 +000017#include "RegClass.h"
Misha Brukman0d82a542003-10-23 18:03:50 +000018#include "llvm/Function.h"
Chris Lattner0a8ed942002-02-04 05:56:09 +000019#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerf726e772002-10-28 19:22:04 +000020#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner0a8ed942002-02-04 05:56:09 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000022#include "llvm/Target/TargetInstrInfo.h"
Vikram S. Advebc001b22003-07-25 21:06:09 +000023#include "llvm/Target/TargetRegInfo.h"
Chris Lattner7471a7b2002-02-05 03:35:53 +000024#include "Support/SetOperations.h"
Ruchira Sasanka8e604792001-09-14 21:18:34 +000025
Brian Gaeked0fde302003-11-11 22:41:34 +000026namespace llvm {
27
Chris Lattner9d4ed152003-01-15 21:14:01 +000028unsigned LiveRange::getRegClassID() const { return getRegClass()->getID(); }
29
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000030LiveRangeInfo::LiveRangeInfo(const Function *F, const TargetMachine &tm,
Chris Lattner697954c2002-01-20 22:54:45 +000031 std::vector<RegClass *> &RCL)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000032 : Meth(F), TM(tm), RegClassList(RCL), MRI(tm.getRegInfo()) { }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000033
34
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000035LiveRangeInfo::~LiveRangeInfo() {
Chris Lattner2f898d22002-02-05 06:02:59 +000036 for (LiveRangeMapType::iterator MI = LiveRangeMap.begin();
37 MI != LiveRangeMap.end(); ++MI) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000038
Chris Lattner697954c2002-01-20 22:54:45 +000039 if (MI->first && MI->second) {
40 LiveRange *LR = MI->second;
41
42 // we need to be careful in deleting LiveRanges in LiveRangeMap
43 // since two/more Values in the live range map can point to the same
44 // live range. We have to make the other entries NULL when we delete
45 // a live range.
46
Chris Lattnercb6b4bd2002-10-29 16:51:05 +000047 for (LiveRange::iterator LI = LR->begin(); LI != LR->end(); ++LI)
Chris Lattner697954c2002-01-20 22:54:45 +000048 LiveRangeMap[*LI] = 0;
49
50 delete LR;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000051 }
52 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000053}
54
55
56//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000057// union two live ranges into one. The 2nd LR is deleted. Used for coalescing.
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000058// Note: the caller must make sure that L1 and L2 are distinct and both
59// LRs don't have suggested colors
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000060//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000061
Chris Lattner296b7732002-02-05 02:52:05 +000062void LiveRangeInfo::unionAndUpdateLRs(LiveRange *L1, LiveRange *L2) {
63 assert(L1 != L2 && (!L1->hasSuggestedColor() || !L2->hasSuggestedColor()));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000064 assert(! (L1->hasColor() && L2->hasColor()) ||
65 L1->getColor() == L2->getColor());
66
Chris Lattner296b7732002-02-05 02:52:05 +000067 set_union(*L1, *L2); // add elements of L2 to L1
Ruchira Sasanka8e604792001-09-14 21:18:34 +000068
Chris Lattner296b7732002-02-05 02:52:05 +000069 for(ValueSet::iterator L2It = L2->begin(); L2It != L2->end(); ++L2It) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000070 //assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types");
71
Chris Lattner30adeb62002-02-04 16:36:59 +000072 L1->insert(*L2It); // add the var in L2 to L1
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000073 LiveRangeMap[*L2It] = L1; // now the elements in L2 should map
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000074 //to L1
Ruchira Sasanka8e604792001-09-14 21:18:34 +000075 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000076
77 // set call interference for L1 from L2
Chris Lattner296b7732002-02-05 02:52:05 +000078 if (L2->isCallInterference())
Ruchira Sasanka958faf32001-10-19 17:21:03 +000079 L1->setCallInterference();
80
Chris Lattner296b7732002-02-05 02:52:05 +000081 // add the spill costs
82 L1->addSpillCost(L2->getSpillCost());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000083
84 // If L2 has a color, give L1 that color. Note that L1 may have had the same
85 // color or none, but would not have a different color as asserted above.
86 if (L2->hasColor())
87 L1->setColor(L2->getColor());
88
89 // Similarly, if LROfUse(L2) has a suggested color, the new range
90 // must have the same color.
91 if (L2->hasSuggestedColor())
92 L1->setSuggestedColor(L2->getSuggestedColor());
Chris Lattner296b7732002-02-05 02:52:05 +000093
Chris Lattner697954c2002-01-20 22:54:45 +000094 delete L2; // delete L2 as it is no longer needed
Ruchira Sasanka8e604792001-09-14 21:18:34 +000095}
96
97
Vikram S. Adve9d67cd12002-09-28 17:05:22 +000098//---------------------------------------------------------------------------
99// Method for creating a single live range for a definition.
100// The definition must be represented by a virtual register (a Value).
101// Note: this function does *not* check that no live range exists for def.
102//---------------------------------------------------------------------------
103
104LiveRange*
105LiveRangeInfo::createNewLiveRange(const Value* Def, bool isCC /* = false*/)
106{
107 LiveRange* DefRange = new LiveRange(); // Create a new live range,
108 DefRange->insert(Def); // add Def to it,
109 LiveRangeMap[Def] = DefRange; // and update the map.
110
111 // set the register class of the new live range
Chris Lattner9d4ed152003-01-15 21:14:01 +0000112 DefRange->setRegClass(RegClassList[MRI.getRegClassIDOfType(Def->getType(),
113 isCC)]);
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000114
115 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000116 std::cerr << " Creating a LR for def ";
117 if (isCC) std::cerr << " (CC Register!)";
118 std::cerr << " : " << RAV(Def) << "\n";
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000119 }
120 return DefRange;
121}
122
123
124LiveRange*
125LiveRangeInfo::createOrAddToLiveRange(const Value* Def, bool isCC /* = false*/)
126{
127 LiveRange *DefRange = LiveRangeMap[Def];
128
129 // check if the LR is already there (because of multiple defs)
130 if (!DefRange) {
Chris Lattner133f0792002-10-28 04:45:29 +0000131 DefRange = createNewLiveRange(Def, isCC);
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000132 } else { // live range already exists
133 DefRange->insert(Def); // add the operand to the range
134 LiveRangeMap[Def] = DefRange; // make operand point to merged set
135 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000136 std::cerr << " Added to existing LR for def: " << RAV(Def) << "\n";
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000137 }
138 return DefRange;
139}
140
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000142//---------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000143// Method for constructing all live ranges in a function. It creates live
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000144// ranges for all values defined in the instruction stream. Also, it
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000145// creates live ranges for all incoming arguments of the function.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000146//---------------------------------------------------------------------------
Chris Lattner2f898d22002-02-05 06:02:59 +0000147void LiveRangeInfo::constructLiveRanges() {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000148
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000149 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000150 std::cerr << "Constructing Live Ranges ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000151
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000152 // first find the live ranges for all incoming args of the function since
153 // those LRs start from the start of the function
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000154 for (Function::const_aiterator AI = Meth->abegin(); AI != Meth->aend(); ++AI)
Chris Lattner133f0792002-10-28 04:45:29 +0000155 createNewLiveRange(AI, /*isCC*/ false);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000156
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000157 // Now suggest hardware registers for these function args
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000158 MRI.suggestRegs4MethodArgs(Meth, *this);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000160 // Now create LRs for machine instructions. A new LR will be created
161 // only for defs in the machine instr since, we assume that all Values are
162 // defined before they are used. However, there can be multiple defs for
163 // the same Value in machine instructions.
164 //
165 // Also, find CALL and RETURN instructions, which need extra work.
Chris Lattner2f898d22002-02-05 06:02:59 +0000166 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000167 MachineFunction &MF = MachineFunction::get(Meth);
168 for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
169 MachineBasicBlock &MBB = *BBI;
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000170
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000171 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000172 for(MachineBasicBlock::iterator MInstIterator = MBB.begin();
173 MInstIterator != MBB.end(); ++MInstIterator) {
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000174 MachineInstr *MInst = *MInstIterator;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000175
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000176 // If the machine instruction is a call/return instruction, add it to
177 // CallRetInstrList for processing its args, ret value, and ret addr.
178 //
Chris Lattner697954c2002-01-20 22:54:45 +0000179 if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ||
180 TM.getInstrInfo().isCall(MInst->getOpCode()))
Chris Lattner133f0792002-10-28 04:45:29 +0000181 CallRetInstrList.push_back(MInst);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000182
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000183 // iterate over explicit MI operands and create a new LR
184 // for each operand that is defined by the instruction
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000185 for (MachineInstr::val_op_iterator OpI = MInst->begin(),
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000186 OpE = MInst->end(); OpI != OpE; ++OpI)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000187 if (OpI.isDef()) {
Chris Lattner30adeb62002-02-04 16:36:59 +0000188 const Value *Def = *OpI;
Chris Lattner133f0792002-10-28 04:45:29 +0000189 bool isCC = (OpI.getMachineOperand().getType()
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000190 == MachineOperand::MO_CCRegister);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000191 LiveRange* LR = createOrAddToLiveRange(Def, isCC);
192
193 // If the operand has a pre-assigned register,
194 // set it directly in the LiveRange
195 if (OpI.getMachineOperand().hasAllocatedReg()) {
196 unsigned getClassId;
197 LR->setColor(MRI.getClassRegNum(
198 OpI.getMachineOperand().getAllocatedRegNum(),
199 getClassId));
200 }
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000201 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000202
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000203 // iterate over implicit MI operands and create a new LR
204 // for each operand that is defined by the instruction
205 for (unsigned i = 0; i < MInst->getNumImplicitRefs(); ++i)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000206 if (MInst->getImplicitOp(i).isDef()) {
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000207 const Value *Def = MInst->getImplicitRef(i);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000208 LiveRange* LR = createOrAddToLiveRange(Def, /*isCC*/ false);
209
210 // If the implicit operand has a pre-assigned register,
211 // set it directly in the LiveRange
212 if (MInst->getImplicitOp(i).hasAllocatedReg()) {
213 unsigned getClassId;
214 LR->setColor(MRI.getClassRegNum(
215 MInst->getImplicitOp(i).getAllocatedRegNum(),
216 getClassId));
217 }
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000218 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000219
220 } // for all machine instructions in the BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000221 } // for all BBs in function
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000222
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000223 // Now we have to suggest clors for call and return arg live ranges.
224 // Also, if there are implicit defs (e.g., retun value of a call inst)
225 // they must be added to the live range list
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000226 //
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000227 suggestRegs4CallRets();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000228
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000229 if( DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000230 std::cerr << "Initial Live Ranges constructed!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000231}
232
233
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000234//---------------------------------------------------------------------------
235// If some live ranges must be colored with specific hardware registers
236// (e.g., for outgoing call args), suggesting of colors for such live
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000237// ranges is done using target specific function. Those functions are called
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000238// from this function. The target specific methods must:
239// 1) suggest colors for call and return args.
240// 2) create new LRs for implicit defs in machine instructions
241//---------------------------------------------------------------------------
Chris Lattner88da77c2002-10-29 17:03:19 +0000242void LiveRangeInfo::suggestRegs4CallRets() {
243 std::vector<MachineInstr*>::iterator It = CallRetInstrList.begin();
244 for( ; It != CallRetInstrList.end(); ++It) {
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000245 MachineInstr *MInst = *It;
Chris Lattner88da77c2002-10-29 17:03:19 +0000246 MachineOpCode OpCode = MInst->getOpCode();
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000247
Chris Lattner88da77c2002-10-29 17:03:19 +0000248 if ((TM.getInstrInfo()).isReturn(OpCode))
249 MRI.suggestReg4RetValue(MInst, *this);
250 else if ((TM.getInstrInfo()).isCall(OpCode))
251 MRI.suggestRegs4CallArgs(MInst, *this);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000252 else
Chris Lattner88da77c2002-10-29 17:03:19 +0000253 assert( 0 && "Non call/ret instr in CallRetInstrList" );
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000254 }
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000255}
256
257
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000258//--------------------------------------------------------------------------
259// The following method coalesces live ranges when possible. This method
260// must be called after the interference graph has been constructed.
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000261
262
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000263/* Algorithm:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000264 for each BB in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000265 for each machine instruction (inst)
266 for each definition (def) in inst
267 for each operand (op) of inst that is a use
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000268 if the def and op are of the same register type
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000269 if the def and op do not interfere //i.e., not simultaneously live
270 if (degree(LR of def) + degree(LR of op)) <= # avail regs
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000271 if both LRs do not have suggested colors
272 merge2IGNodes(def, op) // i.e., merge 2 LRs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000273
274*/
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000275//---------------------------------------------------------------------------
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000276
277
278// Checks if live range LR interferes with any node assigned or suggested to
279// be assigned the specified color
280//
Misha Brukman0d82a542003-10-23 18:03:50 +0000281inline bool InterferesWithColor(const LiveRange& LR, unsigned color) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000282 IGNode* lrNode = LR.getUserIGNode();
283 for (unsigned n=0, NN = lrNode->getNumOfNeighbors(); n < NN; n++) {
284 LiveRange *neighLR = lrNode->getAdjIGNode(n)->getParentLR();
285 if (neighLR->hasColor() && neighLR->getColor() == color)
286 return true;
287 if (neighLR->hasSuggestedColor() && neighLR->getSuggestedColor() == color)
288 return true;
289 }
290 return false;
291}
292
293// Cannot coalesce if any of the following is true:
294// (1) Both LRs have suggested colors (should be "different suggested colors"?)
295// (2) Both LR1 and LR2 have colors and the colors are different
296// (but if the colors are the same, it is definitely safe to coalesce)
297// (3) LR1 has color and LR2 interferes with any LR that has the same color
298// (4) LR2 has color and LR1 interferes with any LR that has the same color
299//
300inline bool InterfsPreventCoalescing(const LiveRange& LROfDef,
Misha Brukman0d82a542003-10-23 18:03:50 +0000301 const LiveRange& LROfUse) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000302 // (4) if they have different suggested colors, cannot coalesce
303 if (LROfDef.hasSuggestedColor() && LROfUse.hasSuggestedColor())
304 return true;
305
306 // if neither has a color, nothing more to do.
307 if (! LROfDef.hasColor() && ! LROfUse.hasColor())
308 return false;
309
310 // (2, 3) if L1 has color...
311 if (LROfDef.hasColor()) {
312 if (LROfUse.hasColor())
313 return (LROfUse.getColor() != LROfDef.getColor());
314 return InterferesWithColor(LROfUse, LROfDef.getColor());
315 }
316
317 // (4) else only LROfUse has a color: check if that could interfere
318 return InterferesWithColor(LROfDef, LROfUse.getColor());
319}
320
321
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000322void LiveRangeInfo::coalesceLRs()
323{
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000324 if(DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000325 std::cerr << "\nCoalescing LRs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000326
Chris Lattnerf726e772002-10-28 19:22:04 +0000327 MachineFunction &MF = MachineFunction::get(Meth);
328 for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
329 MachineBasicBlock &MBB = *BBI;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000330
331 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000332 for(MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII){
333 const MachineInstr *MI = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000334
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000335 if( DEBUG_RA >= RA_DEBUG_LiveRanges) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000336 std::cerr << " *Iterating over machine instr ";
Chris Lattnerf726e772002-10-28 19:22:04 +0000337 MI->dump();
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000338 std::cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000339 }
340
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000341 // iterate over MI operands to find defs
Chris Lattnerf726e772002-10-28 19:22:04 +0000342 for(MachineInstr::const_val_op_iterator DefI = MI->begin(),
343 DefE = MI->end(); DefI != DefE; ++DefI) {
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000344 if (DefI.isDef()) { // this operand is modified
Chris Lattner2f898d22002-02-05 06:02:59 +0000345 LiveRange *LROfDef = getLiveRangeForValue( *DefI );
346 RegClass *RCOfDef = LROfDef->getRegClass();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000347
Chris Lattnerf726e772002-10-28 19:22:04 +0000348 MachineInstr::const_val_op_iterator UseI = MI->begin(),
349 UseE = MI->end();
350 for( ; UseI != UseE; ++UseI) { // for all uses
Chris Lattner2f898d22002-02-05 06:02:59 +0000351 LiveRange *LROfUse = getLiveRangeForValue( *UseI );
352 if (!LROfUse) { // if LR of use is not found
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000353 //don't warn about labels
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000354 if (!isa<BasicBlock>(*UseI) && DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000355 std::cerr << " !! Warning: No LR for use " << RAV(*UseI)<< "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000356 continue; // ignore and continue
357 }
358
Chris Lattner2f898d22002-02-05 06:02:59 +0000359 if (LROfUse == LROfDef) // nothing to merge if they are same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000360 continue;
361
Vikram S. Advebc001b22003-07-25 21:06:09 +0000362 if (MRI.getRegTypeForLR(LROfDef) ==
363 MRI.getRegTypeForLR(LROfUse)) {
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000364 // If the two RegTypes are the same
Chris Lattner37730942002-02-05 03:52:29 +0000365 if (!RCOfDef->getInterference(LROfDef, LROfUse) ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000366
367 unsigned CombinedDegree =
368 LROfDef->getUserIGNode()->getNumOfNeighbors() +
369 LROfUse->getUserIGNode()->getNumOfNeighbors();
370
Vikram S. Adve32f81a32002-09-20 00:45:47 +0000371 if (CombinedDegree > RCOfDef->getNumOfAvailRegs()) {
372 // get more precise estimate of combined degree
373 CombinedDegree = LROfDef->getUserIGNode()->
374 getCombinedDegree(LROfUse->getUserIGNode());
375 }
376
Chris Lattner37730942002-02-05 03:52:29 +0000377 if (CombinedDegree <= RCOfDef->getNumOfAvailRegs()) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000378 // if both LRs do not have different pre-assigned colors
379 // and both LRs do not have suggested colors
380 if (! InterfsPreventCoalescing(*LROfDef, *LROfUse)) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000381 RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
382 unionAndUpdateLRs(LROfDef, LROfUse);
383 }
384
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000385 } // if combined degree is less than # of regs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000386 } // if def and use do not interfere
Ruchira Sasankad33238b2001-10-12 17:48:18 +0000387 }// if reg classes are the same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000388 } // for all uses
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000389 } // if def
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000390 } // for all defs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000391 } // for all machine instructions
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000392 } // for all BBs
393
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000394 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000395 std::cerr << "\nCoalescing Done!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000396}
397
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000398/*--------------------------- Debug code for printing ---------------*/
399
400
Chris Lattner0665a5f2002-02-05 01:43:49 +0000401void LiveRangeInfo::printLiveRanges() {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000402 LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000403 std::cerr << "\nPrinting Live Ranges from Hash Map:\n";
Chris Lattner0665a5f2002-02-05 01:43:49 +0000404 for( ; HMI != LiveRangeMap.end(); ++HMI) {
405 if (HMI->first && HMI->second) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000406 std::cerr << " Value* " << RAV(HMI->first) << "\t: ";
Vikram S. Adve32f81a32002-09-20 00:45:47 +0000407 if (IGNode* igNode = HMI->second->getUserIGNode())
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000408 std::cerr << "LR# " << igNode->getIndex();
Vikram S. Adve32f81a32002-09-20 00:45:47 +0000409 else
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000410 std::cerr << "LR# " << "<no-IGNode>";
411 std::cerr << "\t:Values = "; printSet(*HMI->second); std::cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000412 }
413 }
414}
Brian Gaeked0fde302003-11-11 22:41:34 +0000415
416} // End llvm namespace