blob: 590c88fbc496aa92c05125abe44b25c6b5ae53ad [file] [log] [blame]
Ruchira Sasanka8e604792001-09-14 21:18:34 +00001#include "llvm/CodeGen/LiveRangeInfo.h"
Chris Lattner0a8ed942002-02-04 05:56:09 +00002#include "llvm/CodeGen/RegClass.h"
3#include "llvm/CodeGen/MachineInstr.h"
4#include "llvm/Target/TargetMachine.h"
5#include "llvm/Method.h"
Chris Lattner697954c2002-01-20 22:54:45 +00006#include <iostream>
7using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +00008
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00009//---------------------------------------------------------------------------
10// Constructor
11//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000012LiveRangeInfo::LiveRangeInfo(const Method *const M,
13 const TargetMachine& tm,
Chris Lattner697954c2002-01-20 22:54:45 +000014 std::vector<RegClass *> &RCL)
15 : Meth(M), LiveRangeMap(), TM(tm),
16 RegClassList(RCL), MRI(tm.getRegInfo())
Ruchira Sasanka8e604792001-09-14 21:18:34 +000017{ }
18
19
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000020//---------------------------------------------------------------------------
21// Destructor: Deletes all LiveRanges in the LiveRangeMap
22//---------------------------------------------------------------------------
23LiveRangeInfo::~LiveRangeInfo() {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000024 LiveRangeMapType::iterator MI = LiveRangeMap.begin();
25
26 for( ; MI != LiveRangeMap.end() ; ++MI) {
Chris Lattner697954c2002-01-20 22:54:45 +000027 if (MI->first && MI->second) {
28 LiveRange *LR = MI->second;
29
30 // we need to be careful in deleting LiveRanges in LiveRangeMap
31 // since two/more Values in the live range map can point to the same
32 // live range. We have to make the other entries NULL when we delete
33 // a live range.
34
35 LiveRange::iterator LI = LR->begin();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000036
Chris Lattner697954c2002-01-20 22:54:45 +000037 for( ; LI != LR->end() ; ++LI)
38 LiveRangeMap[*LI] = 0;
39
40 delete LR;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000041 }
42 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000043}
44
45
46//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000047// union two live ranges into one. The 2nd LR is deleted. Used for coalescing.
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000048// Note: the caller must make sure that L1 and L2 are distinct and both
49// LRs don't have suggested colors
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000050//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000051void LiveRangeInfo::unionAndUpdateLRs(LiveRange *const L1, LiveRange *L2)
52{
53 assert( L1 != L2);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000054 L1->setUnion( L2 ); // add elements of L2 to L1
Ruchira Sasanka8e604792001-09-14 21:18:34 +000055 ValueSet::iterator L2It;
56
57 for( L2It = L2->begin() ; L2It != L2->end(); ++L2It) {
58
59 //assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types");
60
Chris Lattner30adeb62002-02-04 16:36:59 +000061 L1->insert(*L2It); // add the var in L2 to L1
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000062 LiveRangeMap[ *L2It ] = L1; // now the elements in L2 should map
63 //to L1
Ruchira Sasanka8e604792001-09-14 21:18:34 +000064 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000065
66
67 // Now if LROfDef(L1) has a suggested color, it will remain.
68 // But, if LROfUse(L2) has a suggested color, the new range
69 // must have the same color.
70
71 if(L2->hasSuggestedColor())
72 L1->setSuggestedColor( L2->getSuggestedColor() );
73
Ruchira Sasanka958faf32001-10-19 17:21:03 +000074
75 if( L2->isCallInterference() )
76 L1->setCallInterference();
77
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000078
79 L1->addSpillCost( L2->getSpillCost() ); // add the spill costs
Ruchira Sasanka958faf32001-10-19 17:21:03 +000080
Chris Lattner697954c2002-01-20 22:54:45 +000081 delete L2; // delete L2 as it is no longer needed
Ruchira Sasanka8e604792001-09-14 21:18:34 +000082}
83
84
85
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000086//---------------------------------------------------------------------------
87// Method for constructing all live ranges in a method. It creates live
88// ranges for all values defined in the instruction stream. Also, it
89// creates live ranges for all incoming arguments of the method.
90//---------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000091void LiveRangeInfo::constructLiveRanges()
92{
93
94 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +000095 cerr << "Consturcting Live Ranges ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000096
97 // first find the live ranges for all incoming args of the method since
98 // those LRs start from the start of the method
99
100 // get the argument list
101 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
102 // get an iterator to arg list
103 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
104
105
106 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000107 LiveRange * ArgRange = new LiveRange(); // creates a new LR and
Chris Lattner30adeb62002-02-04 16:36:59 +0000108 const Value *Val = (const Value *) *ArgIt;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000109
Chris Lattner30adeb62002-02-04 16:36:59 +0000110 ArgRange->insert(Val); // add the arg (def) to it
Chris Lattner697954c2002-01-20 22:54:45 +0000111 LiveRangeMap[Val] = ArgRange;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000112
113 // create a temp machine op to find the register class of value
114 //const MachineOperand Op(MachineOperand::MO_VirtualRegister);
115
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000116 unsigned rcid = MRI.getRegClassIDOfValue( Val );
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117 ArgRange->setRegClass(RegClassList[ rcid ] );
118
119
120 if( DEBUG_RA > 1) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000121 cerr << " adding LiveRange for argument "
122 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000123 }
124 }
125
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000126 // Now suggest hardware registers for these method args
127 MRI.suggestRegs4MethodArgs(Meth, *this);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000128
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000129
130
131 // Now find speical LLVM instructions (CALL, RET) and LRs in machine
132 // instructions.
133
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134
135 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000136 for( ; BBI != Meth->end(); ++BBI) { // go thru BBs in random order
137
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000138 // Now find all LRs for machine the instructions. A new LR will be created
139 // only for defs in the machine instr since, we assume that all Values are
140 // defined before they are used. However, there can be multiple defs for
141 // the same Value in machine instructions.
142
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000143 // get the iterator for machine instructions
144 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Chris Lattner697954c2002-01-20 22:54:45 +0000145 MachineCodeForBasicBlock::const_iterator MInstIterator = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000146
147 // iterate over all the machine instructions in BB
148 for( ; MInstIterator != MIVec.end(); MInstIterator++) {
149
150 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000151
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000152 // Now if the machine instruction is a call/return instruction,
153 // add it to CallRetInstrList for processing its implicit operands
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000154
Chris Lattner697954c2002-01-20 22:54:45 +0000155 if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ||
156 TM.getInstrInfo().isCall(MInst->getOpCode()))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000157 CallRetInstrList.push_back( MInst );
158
159
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000160 // iterate over MI operands to find defs
Chris Lattner697954c2002-01-20 22:54:45 +0000161 for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done(); ++OpI) {
162 if(DEBUG_RA) {
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000163 MachineOperand::MachineOperandType OpTyp =
164 OpI.getMachineOperand().getOperandType();
Ruchira Sasankae727f852001-09-18 22:43:57 +0000165
Chris Lattner0665a5f2002-02-05 01:43:49 +0000166 if (OpTyp == MachineOperand::MO_CCRegister)
167 cerr << "\n**CC reg found. Is Def=" << OpI.isDef() << " Val:"
168 << RAV(OpI.getMachineOperand().getVRegValue()) << "\n";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000169 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000170
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000171 // create a new LR iff this operand is a def
Chris Lattner30adeb62002-02-04 16:36:59 +0000172 if (OpI.isDef()) {
173 const Value *Def = *OpI;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000174
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000175 // Only instruction values are accepted for live ranges here
Chris Lattner0665a5f2002-02-05 01:43:49 +0000176 if (Def->getValueType() != Value::InstructionVal ) {
177 cerr << "\n**%%Error: Def is not an instruction val. Def="
178 << RAV(Def) << "\n";
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000179 continue;
180 }
181
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000182 LiveRange *DefRange = LiveRangeMap[Def];
183
184 // see LR already there (because of multiple defs)
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000185 if( !DefRange) { // if it is not in LiveRangeMap
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000186 DefRange = new LiveRange(); // creates a new live range and
Chris Lattner30adeb62002-02-04 16:36:59 +0000187 DefRange->insert(Def); // add the instruction (def) to it
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000188 LiveRangeMap[ Def ] = DefRange; // update the map
189
Chris Lattner0665a5f2002-02-05 01:43:49 +0000190 if (DEBUG_RA > 1)
191 cerr << " creating a LR for def: " << RAV(Def) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192
193 // set the register class of the new live range
194 //assert( RegClassList.size() );
195 MachineOperand::MachineOperandType OpTy =
196 OpI.getMachineOperand().getOperandType();
197
198 bool isCC = ( OpTy == MachineOperand::MO_CCRegister);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000199 unsigned rcid = MRI.getRegClassIDOfValue(
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000200 OpI.getMachineOperand().getVRegValue(), isCC );
201
202
Chris Lattner0665a5f2002-02-05 01:43:49 +0000203 if (isCC && DEBUG_RA)
204 cerr << "\a**created a LR for a CC reg:"
205 << RAV(OpI.getMachineOperand().getVRegValue());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000206
Chris Lattner0665a5f2002-02-05 01:43:49 +0000207 DefRange->setRegClass(RegClassList[rcid]);
208 } else {
Chris Lattner30adeb62002-02-04 16:36:59 +0000209 DefRange->insert(Def); // add the opearand to def range
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000210 // update the map - Operand points
211 // to the merged set
Chris Lattner0665a5f2002-02-05 01:43:49 +0000212 LiveRangeMap[Def] = DefRange;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000213
Chris Lattner0665a5f2002-02-05 01:43:49 +0000214 if (DEBUG_RA > 1)
215 cerr << " added to an existing LR for def: "
216 << RAV(Def) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000217 }
218
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000219 } // if isDef()
220
221 } // for all opereands in machine instructions
222
223 } // for all machine instructions in the BB
224
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000225 } // for all BBs in method
226
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000227
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000228 // Now we have to suggest clors for call and return arg live ranges.
229 // Also, if there are implicit defs (e.g., retun value of a call inst)
230 // they must be added to the live range list
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000231
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000232 suggestRegs4CallRets();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000233
234 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000235 cerr << "Initial Live Ranges constructed!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000236
237}
238
239
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000240//---------------------------------------------------------------------------
241// If some live ranges must be colored with specific hardware registers
242// (e.g., for outgoing call args), suggesting of colors for such live
243// ranges is done using target specific method. Those methods are called
244// from this function. The target specific methods must:
245// 1) suggest colors for call and return args.
246// 2) create new LRs for implicit defs in machine instructions
247//---------------------------------------------------------------------------
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000248void LiveRangeInfo::suggestRegs4CallRets()
249{
250
251 CallRetInstrListType::const_iterator It = CallRetInstrList.begin();
252
253 for( ; It != CallRetInstrList.end(); ++It ) {
254
255 const MachineInstr *MInst = *It;
256 MachineOpCode OpCode = MInst->getOpCode();
257
258 if( (TM.getInstrInfo()).isReturn(OpCode) )
259 MRI.suggestReg4RetValue( MInst, *this);
260
261 else if( (TM.getInstrInfo()).isCall( OpCode ) )
262 MRI.suggestRegs4CallArgs( MInst, *this, RegClassList );
263
264 else
265 assert( 0 && "Non call/ret instr in CallRetInstrList" );
266 }
267
268}
269
270
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000271//--------------------------------------------------------------------------
272// The following method coalesces live ranges when possible. This method
273// must be called after the interference graph has been constructed.
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000274
275
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000276/* Algorithm:
277 for each BB in method
278 for each machine instruction (inst)
279 for each definition (def) in inst
280 for each operand (op) of inst that is a use
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000281 if the def and op are of the same register type
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000282 if the def and op do not interfere //i.e., not simultaneously live
283 if (degree(LR of def) + degree(LR of op)) <= # avail regs
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000284 if both LRs do not have suggested colors
285 merge2IGNodes(def, op) // i.e., merge 2 LRs
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000286
287*/
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000288//---------------------------------------------------------------------------
289void LiveRangeInfo::coalesceLRs()
290{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000291 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000292 cerr << "\nCoalscing LRs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000293
294 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
295
296 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
297
298 // get the iterator for machine instructions
299 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Chris Lattner697954c2002-01-20 22:54:45 +0000300 MachineCodeForBasicBlock::const_iterator MInstIterator = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000301
302 // iterate over all the machine instructions in BB
303 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
304
305 const MachineInstr * MInst = *MInstIterator;
306
307 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000308 cerr << " *Iterating over machine instr ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000309 MInst->dump();
Chris Lattner697954c2002-01-20 22:54:45 +0000310 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000311 }
312
313
314 // iterate over MI operands to find defs
Chris Lattner7a176752001-12-04 00:03:30 +0000315 for(MachineInstr::val_const_op_iterator DefI(MInst);!DefI.done();++DefI){
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000316
317 if( DefI.isDef() ) { // iff this operand is a def
318
319 LiveRange *const LROfDef = getLiveRangeForValue( *DefI );
320 assert( LROfDef );
321 RegClass *const RCOfDef = LROfDef->getRegClass();
322
Chris Lattner7a176752001-12-04 00:03:30 +0000323 MachineInstr::val_const_op_iterator UseI(MInst);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000324 for( ; !UseI.done(); ++UseI){ // for all uses
325
326 LiveRange *const LROfUse = getLiveRangeForValue( *UseI );
327
328 if( ! LROfUse ) { // if LR of use is not found
329
330 //don't warn about labels
Chris Lattner0665a5f2002-02-05 01:43:49 +0000331 if (!((*UseI)->getType())->isLabelType() && DEBUG_RA)
332 cerr << " !! Warning: No LR for use " << RAV(*UseI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000333 continue; // ignore and continue
334 }
335
336 if( LROfUse == LROfDef) // nothing to merge if they are same
337 continue;
338
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000339 //RegClass *const RCOfUse = LROfUse->getRegClass();
340 //if( RCOfDef == RCOfUse ) { // if the reg classes are the same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000341
Ruchira Sasankaefaf9be2001-11-10 00:20:24 +0000342 if( MRI.getRegType(LROfDef) == MRI.getRegType(LROfUse) ) {
343
344 // If the two RegTypes are the same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000345
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000346 if( ! RCOfDef->getInterference(LROfDef, LROfUse) ) {
347
348 unsigned CombinedDegree =
349 LROfDef->getUserIGNode()->getNumOfNeighbors() +
350 LROfUse->getUserIGNode()->getNumOfNeighbors();
351
352 if( CombinedDegree <= RCOfDef->getNumOfAvailRegs() ) {
353
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000354 // if both LRs do not have suggested colors
355 if( ! (LROfDef->hasSuggestedColor() &&
356 LROfUse->hasSuggestedColor() ) ) {
357
358 RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
359 unionAndUpdateLRs(LROfDef, LROfUse);
360 }
361
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000362
363 } // if combined degree is less than # of regs
364
365 } // if def and use do not interfere
366
Ruchira Sasankad33238b2001-10-12 17:48:18 +0000367 }// if reg classes are the same
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000368
369 } // for all uses
370
371 } // if def
372
373 } // for all defs
374
375 } // for all machine instructions
376
377 } // for all BBs
378
379 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000380 cerr << "\nCoalscing Done!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000381
382}
383
384
385
386
387
388/*--------------------------- Debug code for printing ---------------*/
389
390
Chris Lattner0665a5f2002-02-05 01:43:49 +0000391void LiveRangeInfo::printLiveRanges() {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000392 LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
Chris Lattner697954c2002-01-20 22:54:45 +0000393 cerr << "\nPrinting Live Ranges from Hash Map:\n";
Chris Lattner0665a5f2002-02-05 01:43:49 +0000394 for( ; HMI != LiveRangeMap.end(); ++HMI) {
395 if (HMI->first && HMI->second) {
396 cerr << " " << RAV(HMI->first) << "\t: ";
Chris Lattner697954c2002-01-20 22:54:45 +0000397 HMI->second->printSet(); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000398 }
399 }
400}
401
402