blob: 6da3e0f524e6542d27815f1d642d54f9595767b3 [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"
Brian Gaeke3b2e5e62004-04-23 18:15:47 +000023#include "../SparcV9RegInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/SetOperations.h"
Reid Spencer954da372004-07-04 12:19:56 +000025#include <iostream>
Ruchira Sasanka8e604792001-09-14 21:18:34 +000026
Brian Gaeked0fde302003-11-11 22:41:34 +000027namespace llvm {
28
Chris Lattner9d4ed152003-01-15 21:14:01 +000029unsigned LiveRange::getRegClassID() const { return getRegClass()->getID(); }
30
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000031LiveRangeInfo::LiveRangeInfo(const Function *F, const TargetMachine &tm,
Chris Lattner697954c2002-01-20 22:54:45 +000032 std::vector<RegClass *> &RCL)
Chris Lattnerd029cd22004-06-02 05:55:25 +000033 : Meth(F), TM(tm), RegClassList(RCL), MRI(*tm.getRegInfo()) { }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000034
35
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000036LiveRangeInfo::~LiveRangeInfo() {
Chris Lattner2f898d22002-02-05 06:02:59 +000037 for (LiveRangeMapType::iterator MI = LiveRangeMap.begin();
38 MI != LiveRangeMap.end(); ++MI) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000039
Chris Lattner697954c2002-01-20 22:54:45 +000040 if (MI->first && MI->second) {
41 LiveRange *LR = MI->second;
42
43 // we need to be careful in deleting LiveRanges in LiveRangeMap
44 // since two/more Values in the live range map can point to the same
45 // live range. We have to make the other entries NULL when we delete
46 // a live range.
47
Chris Lattnercb6b4bd2002-10-29 16:51:05 +000048 for (LiveRange::iterator LI = LR->begin(); LI != LR->end(); ++LI)
Chris Lattner697954c2002-01-20 22:54:45 +000049 LiveRangeMap[*LI] = 0;
50
51 delete LR;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000052 }
53 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000054}
55
56
57//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000058// union two live ranges into one. The 2nd LR is deleted. Used for coalescing.
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000059// Note: the caller must make sure that L1 and L2 are distinct and both
60// LRs don't have suggested colors
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000061//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000062
Chris Lattner296b7732002-02-05 02:52:05 +000063void LiveRangeInfo::unionAndUpdateLRs(LiveRange *L1, LiveRange *L2) {
64 assert(L1 != L2 && (!L1->hasSuggestedColor() || !L2->hasSuggestedColor()));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000065 assert(! (L1->hasColor() && L2->hasColor()) ||
66 L1->getColor() == L2->getColor());
67
Brian Gaekedb7c40d2004-07-29 06:43:08 +000068 L2->insert (L1->begin(), L1->end()); // add elements of L2 to L1
Ruchira Sasanka8e604792001-09-14 21:18:34 +000069
Brian Gaekedb7c40d2004-07-29 06:43:08 +000070 for(LiveRange::iterator L2It = L2->begin(); L2It != L2->end(); ++L2It) {
Chris Lattner30adeb62002-02-04 16:36:59 +000071 L1->insert(*L2It); // add the var in L2 to L1
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000072 LiveRangeMap[*L2It] = L1; // now the elements in L2 should map
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000073 //to L1
Ruchira Sasanka8e604792001-09-14 21:18:34 +000074 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000075
76 // set call interference for L1 from L2
Chris Lattner296b7732002-02-05 02:52:05 +000077 if (L2->isCallInterference())
Ruchira Sasanka958faf32001-10-19 17:21:03 +000078 L1->setCallInterference();
79
Chris Lattner296b7732002-02-05 02:52:05 +000080 // add the spill costs
81 L1->addSpillCost(L2->getSpillCost());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000082
83 // If L2 has a color, give L1 that color. Note that L1 may have had the same
84 // color or none, but would not have a different color as asserted above.
85 if (L2->hasColor())
86 L1->setColor(L2->getColor());
87
88 // Similarly, if LROfUse(L2) has a suggested color, the new range
89 // must have the same color.
90 if (L2->hasSuggestedColor())
91 L1->setSuggestedColor(L2->getSuggestedColor());
Chris Lattner296b7732002-02-05 02:52:05 +000092
Chris Lattner697954c2002-01-20 22:54:45 +000093 delete L2; // delete L2 as it is no longer needed
Ruchira Sasanka8e604792001-09-14 21:18:34 +000094}
95
96
Vikram S. Adve9d67cd12002-09-28 17:05:22 +000097//---------------------------------------------------------------------------
98// Method for creating a single live range for a definition.
99// The definition must be represented by a virtual register (a Value).
100// Note: this function does *not* check that no live range exists for def.
101//---------------------------------------------------------------------------
102
103LiveRange*
104LiveRangeInfo::createNewLiveRange(const Value* Def, bool isCC /* = false*/)
105{
106 LiveRange* DefRange = new LiveRange(); // Create a new live range,
107 DefRange->insert(Def); // add Def to it,
108 LiveRangeMap[Def] = DefRange; // and update the map.
109
110 // set the register class of the new live range
Chris Lattner9d4ed152003-01-15 21:14:01 +0000111 DefRange->setRegClass(RegClassList[MRI.getRegClassIDOfType(Def->getType(),
112 isCC)]);
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000113
114 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000115 std::cerr << " Creating a LR for def ";
116 if (isCC) std::cerr << " (CC Register!)";
117 std::cerr << " : " << RAV(Def) << "\n";
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000118 }
119 return DefRange;
120}
121
122
123LiveRange*
124LiveRangeInfo::createOrAddToLiveRange(const Value* Def, bool isCC /* = false*/)
125{
126 LiveRange *DefRange = LiveRangeMap[Def];
127
128 // check if the LR is already there (because of multiple defs)
129 if (!DefRange) {
Chris Lattner133f0792002-10-28 04:45:29 +0000130 DefRange = createNewLiveRange(Def, isCC);
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000131 } else { // live range already exists
132 DefRange->insert(Def); // add the operand to the range
133 LiveRangeMap[Def] = DefRange; // make operand point to merged set
134 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000135 std::cerr << " Added to existing LR for def: " << RAV(Def) << "\n";
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000136 }
137 return DefRange;
138}
139
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000141//---------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000142// Method for constructing all live ranges in a function. It creates live
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000143// ranges for all values defined in the instruction stream. Also, it
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000144// creates live ranges for all incoming arguments of the function.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000145//---------------------------------------------------------------------------
Chris Lattner2f898d22002-02-05 06:02:59 +0000146void LiveRangeInfo::constructLiveRanges() {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000147
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000148 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000149 std::cerr << "Constructing Live Ranges ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000150
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000151 // first find the live ranges for all incoming args of the function since
152 // those LRs start from the start of the function
Chris Lattnere4d5c442005-03-15 04:54:21 +0000153 for (Function::const_arg_iterator AI = Meth->arg_begin(); AI != Meth->arg_end(); ++AI)
Chris Lattner133f0792002-10-28 04:45:29 +0000154 createNewLiveRange(AI, /*isCC*/ false);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000155
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000156 // Now suggest hardware registers for these function args
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000157 MRI.suggestRegs4MethodArgs(Meth, *this);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000158
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000159 // Now create LRs for machine instructions. A new LR will be created
160 // only for defs in the machine instr since, we assume that all Values are
161 // defined before they are used. However, there can be multiple defs for
162 // the same Value in machine instructions.
163 //
164 // Also, find CALL and RETURN instructions, which need extra work.
Chris Lattner2f898d22002-02-05 06:02:59 +0000165 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000166 MachineFunction &MF = MachineFunction::get(Meth);
167 for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
168 MachineBasicBlock &MBB = *BBI;
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000169
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000170 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000171 for(MachineBasicBlock::iterator MInstIterator = MBB.begin();
172 MInstIterator != MBB.end(); ++MInstIterator) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000173 MachineInstr *MInst = MInstIterator;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000174
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000175 // If the machine instruction is a call/return instruction, add it to
176 // CallRetInstrList for processing its args, ret value, and ret addr.
177 //
Chris Lattnerd029cd22004-06-02 05:55:25 +0000178 if(TM.getInstrInfo()->isReturn(MInst->getOpcode()) ||
179 TM.getInstrInfo()->isCall(MInst->getOpcode()))
Chris Lattner133f0792002-10-28 04:45:29 +0000180 CallRetInstrList.push_back(MInst);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000181
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000182 // iterate over explicit MI operands and create a new LR
183 // for each operand that is defined by the instruction
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000184 for (MachineInstr::val_op_iterator OpI = MInst->begin(),
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000185 OpE = MInst->end(); OpI != OpE; ++OpI)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000186 if (OpI.isDef()) {
Chris Lattner30adeb62002-02-04 16:36:59 +0000187 const Value *Def = *OpI;
Chris Lattner133f0792002-10-28 04:45:29 +0000188 bool isCC = (OpI.getMachineOperand().getType()
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000189 == MachineOperand::MO_CCRegister);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000190 LiveRange* LR = createOrAddToLiveRange(Def, isCC);
191
192 // If the operand has a pre-assigned register,
193 // set it directly in the LiveRange
194 if (OpI.getMachineOperand().hasAllocatedReg()) {
195 unsigned getClassId;
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000196 LR->setColor(MRI.getClassRegNum(OpI.getMachineOperand().getReg(),
197 getClassId));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000198 }
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000199 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000200
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000201 // iterate over implicit MI operands and create a new LR
202 // for each operand that is defined by the instruction
203 for (unsigned i = 0; i < MInst->getNumImplicitRefs(); ++i)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000204 if (MInst->getImplicitOp(i).isDef()) {
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000205 const Value *Def = MInst->getImplicitRef(i);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000206 LiveRange* LR = createOrAddToLiveRange(Def, /*isCC*/ false);
207
208 // If the implicit operand has a pre-assigned register,
209 // set it directly in the LiveRange
210 if (MInst->getImplicitOp(i).hasAllocatedReg()) {
211 unsigned getClassId;
212 LR->setColor(MRI.getClassRegNum(
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000213 MInst->getImplicitOp(i).getReg(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000214 getClassId));
215 }
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000216 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000217
218 } // for all machine instructions in the BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000219 } // for all BBs in function
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000220
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000221 // Now we have to suggest clors for call and return arg live ranges.
222 // Also, if there are implicit defs (e.g., retun value of a call inst)
223 // they must be added to the live range list
Vikram S. Adve9d67cd12002-09-28 17:05:22 +0000224 //
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000225 suggestRegs4CallRets();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000226
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000227 if( DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000228 std::cerr << "Initial Live Ranges constructed!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000229}
230
231
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000232//---------------------------------------------------------------------------
233// If some live ranges must be colored with specific hardware registers
234// (e.g., for outgoing call args), suggesting of colors for such live
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000235// ranges is done using target specific function. Those functions are called
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000236// from this function. The target specific methods must:
237// 1) suggest colors for call and return args.
238// 2) create new LRs for implicit defs in machine instructions
239//---------------------------------------------------------------------------
Chris Lattner88da77c2002-10-29 17:03:19 +0000240void LiveRangeInfo::suggestRegs4CallRets() {
241 std::vector<MachineInstr*>::iterator It = CallRetInstrList.begin();
242 for( ; It != CallRetInstrList.end(); ++It) {
Vikram S. Advec9a0ca52002-07-08 23:07:26 +0000243 MachineInstr *MInst = *It;
Brian Gaeke12c1d2c2004-02-11 20:47:34 +0000244 MachineOpCode OpCode = MInst->getOpcode();
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000245
Chris Lattnerd029cd22004-06-02 05:55:25 +0000246 if (TM.getInstrInfo()->isReturn(OpCode))
Chris Lattner88da77c2002-10-29 17:03:19 +0000247 MRI.suggestReg4RetValue(MInst, *this);
Chris Lattnerd029cd22004-06-02 05:55:25 +0000248 else if (TM.getInstrInfo()->isCall(OpCode))
Chris Lattner88da77c2002-10-29 17:03:19 +0000249 MRI.suggestRegs4CallArgs(MInst, *this);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000250 else
Chris Lattner88da77c2002-10-29 17:03:19 +0000251 assert( 0 && "Non call/ret instr in CallRetInstrList" );
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000252 }
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000253}
254
255
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000256//--------------------------------------------------------------------------
257// The following method coalesces live ranges when possible. This method
258// must be called after the interference graph has been constructed.
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000259
260
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000261/* Algorithm:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000262 for each BB in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000263 for each machine instruction (inst)
264 for each definition (def) in inst
265 for each operand (op) of inst that is a use
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000266 if the def and op are of the same register type
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000267 if the def and op do not interfere //i.e., not simultaneously live
268 if (degree(LR of def) + degree(LR of op)) <= # avail regs
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000269 if both LRs do not have suggested colors
270 merge2IGNodes(def, op) // i.e., merge 2 LRs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000271
272*/
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000273//---------------------------------------------------------------------------
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000274
275
276// Checks if live range LR interferes with any node assigned or suggested to
277// be assigned the specified color
278//
Misha Brukman0d82a542003-10-23 18:03:50 +0000279inline bool InterferesWithColor(const LiveRange& LR, unsigned color) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000280 IGNode* lrNode = LR.getUserIGNode();
281 for (unsigned n=0, NN = lrNode->getNumOfNeighbors(); n < NN; n++) {
282 LiveRange *neighLR = lrNode->getAdjIGNode(n)->getParentLR();
283 if (neighLR->hasColor() && neighLR->getColor() == color)
284 return true;
285 if (neighLR->hasSuggestedColor() && neighLR->getSuggestedColor() == color)
286 return true;
287 }
288 return false;
289}
290
291// Cannot coalesce if any of the following is true:
292// (1) Both LRs have suggested colors (should be "different suggested colors"?)
293// (2) Both LR1 and LR2 have colors and the colors are different
294// (but if the colors are the same, it is definitely safe to coalesce)
295// (3) LR1 has color and LR2 interferes with any LR that has the same color
296// (4) LR2 has color and LR1 interferes with any LR that has the same color
297//
298inline bool InterfsPreventCoalescing(const LiveRange& LROfDef,
Misha Brukman0d82a542003-10-23 18:03:50 +0000299 const LiveRange& LROfUse) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000300 // (4) if they have different suggested colors, cannot coalesce
301 if (LROfDef.hasSuggestedColor() && LROfUse.hasSuggestedColor())
302 return true;
303
304 // if neither has a color, nothing more to do.
305 if (! LROfDef.hasColor() && ! LROfUse.hasColor())
306 return false;
307
308 // (2, 3) if L1 has color...
309 if (LROfDef.hasColor()) {
310 if (LROfUse.hasColor())
311 return (LROfUse.getColor() != LROfDef.getColor());
312 return InterferesWithColor(LROfUse, LROfDef.getColor());
313 }
314
315 // (4) else only LROfUse has a color: check if that could interfere
316 return InterferesWithColor(LROfDef, LROfUse.getColor());
317}
318
319
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000320void LiveRangeInfo::coalesceLRs()
321{
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000322 if(DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000323 std::cerr << "\nCoalescing LRs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000324
Chris Lattnerf726e772002-10-28 19:22:04 +0000325 MachineFunction &MF = MachineFunction::get(Meth);
326 for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
327 MachineBasicBlock &MBB = *BBI;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000328
329 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000330 for(MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII){
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000331 const MachineInstr *MI = MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000332
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000333 if( DEBUG_RA >= RA_DEBUG_LiveRanges) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000334 std::cerr << " *Iterating over machine instr ";
Chris Lattnerf726e772002-10-28 19:22:04 +0000335 MI->dump();
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000336 std::cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000337 }
338
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000339 // iterate over MI operands to find defs
Chris Lattnerf726e772002-10-28 19:22:04 +0000340 for(MachineInstr::const_val_op_iterator DefI = MI->begin(),
341 DefE = MI->end(); DefI != DefE; ++DefI) {
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000342 if (DefI.isDef()) { // this operand is modified
Chris Lattner2f898d22002-02-05 06:02:59 +0000343 LiveRange *LROfDef = getLiveRangeForValue( *DefI );
344 RegClass *RCOfDef = LROfDef->getRegClass();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000345
Chris Lattnerf726e772002-10-28 19:22:04 +0000346 MachineInstr::const_val_op_iterator UseI = MI->begin(),
347 UseE = MI->end();
348 for( ; UseI != UseE; ++UseI) { // for all uses
Chris Lattner2f898d22002-02-05 06:02:59 +0000349 LiveRange *LROfUse = getLiveRangeForValue( *UseI );
350 if (!LROfUse) { // if LR of use is not found
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000351 //don't warn about labels
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000352 if (!isa<BasicBlock>(*UseI) && DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000353 std::cerr << " !! Warning: No LR for use " << RAV(*UseI)<< "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000354 continue; // ignore and continue
355 }
356
Chris Lattner2f898d22002-02-05 06:02:59 +0000357 if (LROfUse == LROfDef) // nothing to merge if they are same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000358 continue;
359
Vikram S. Advebc001b22003-07-25 21:06:09 +0000360 if (MRI.getRegTypeForLR(LROfDef) ==
361 MRI.getRegTypeForLR(LROfUse)) {
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000362 // If the two RegTypes are the same
Chris Lattner37730942002-02-05 03:52:29 +0000363 if (!RCOfDef->getInterference(LROfDef, LROfUse) ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000364
365 unsigned CombinedDegree =
366 LROfDef->getUserIGNode()->getNumOfNeighbors() +
367 LROfUse->getUserIGNode()->getNumOfNeighbors();
368
Vikram S. Adve32f81a32002-09-20 00:45:47 +0000369 if (CombinedDegree > RCOfDef->getNumOfAvailRegs()) {
370 // get more precise estimate of combined degree
371 CombinedDegree = LROfDef->getUserIGNode()->
372 getCombinedDegree(LROfUse->getUserIGNode());
373 }
374
Chris Lattner37730942002-02-05 03:52:29 +0000375 if (CombinedDegree <= RCOfDef->getNumOfAvailRegs()) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000376 // if both LRs do not have different pre-assigned colors
377 // and both LRs do not have suggested colors
378 if (! InterfsPreventCoalescing(*LROfDef, *LROfUse)) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000379 RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
380 unionAndUpdateLRs(LROfDef, LROfUse);
381 }
382
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000383 } // if combined degree is less than # of regs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000384 } // if def and use do not interfere
Ruchira Sasankad33238b2001-10-12 17:48:18 +0000385 }// if reg classes are the same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000386 } // for all uses
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000387 } // if def
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000388 } // for all defs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000389 } // for all machine instructions
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000390 } // for all BBs
391
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000392 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000393 std::cerr << "\nCoalescing Done!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000394}
395
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000396/*--------------------------- Debug code for printing ---------------*/
397
398
Chris Lattner0665a5f2002-02-05 01:43:49 +0000399void LiveRangeInfo::printLiveRanges() {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000400 LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000401 std::cerr << "\nPrinting Live Ranges from Hash Map:\n";
Chris Lattner0665a5f2002-02-05 01:43:49 +0000402 for( ; HMI != LiveRangeMap.end(); ++HMI) {
403 if (HMI->first && HMI->second) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000404 std::cerr << " Value* " << RAV(HMI->first) << "\t: ";
Vikram S. Adve32f81a32002-09-20 00:45:47 +0000405 if (IGNode* igNode = HMI->second->getUserIGNode())
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000406 std::cerr << "LR# " << igNode->getIndex();
Vikram S. Adve32f81a32002-09-20 00:45:47 +0000407 else
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000408 std::cerr << "LR# " << "<no-IGNode>";
Brian Gaekedb7c40d2004-07-29 06:43:08 +0000409 std::cerr << "\t:Values = " << *HMI->second << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000410 }
411 }
412}
Brian Gaeked0fde302003-11-11 22:41:34 +0000413
414} // End llvm namespace