1 #include "llvm/CodeGen/LiveRangeInfo.h"
3 LiveRangeInfo::LiveRangeInfo(const Method *const M,
4 const TargetMachine& tm,
5 vector<RegClass *> &RCL)
6 : Meth(M), LiveRangeMap(),
7 TM(tm), RegClassList(RCL),
13 // union two live ranges into one. The 2nd LR is deleted. Used for coalescing.
14 // Note: the caller must make sure that L1 and L2 are distinct and both
15 // LRs don't have suggested colors
17 void LiveRangeInfo::unionAndUpdateLRs(LiveRange *const L1, LiveRange *L2)
20 L1->setUnion( L2 ); // add elements of L2 to L1
21 ValueSet::iterator L2It;
23 for( L2It = L2->begin() ; L2It != L2->end(); ++L2It) {
25 //assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types");
27 L1->add( *L2It ); // add the var in L2 to L1
28 LiveRangeMap[ *L2It ] = L1; // now the elements in L2 should map to L1
32 // Now if LROfDef(L1) has a suggested color, it will remain.
33 // But, if LROfUse(L2) has a suggested color, the new range
34 // must have the same color.
36 if(L2->hasSuggestedColor())
37 L1->setSuggestedColor( L2->getSuggestedColor() );
40 if( L2->isCallInterference() )
41 L1->setCallInterference();
44 delete ( L2 ); // delete L2 as it is no longer needed
50 void LiveRangeInfo::constructLiveRanges()
54 cout << "Consturcting Live Ranges ..." << endl;
56 // first find the live ranges for all incoming args of the method since
57 // those LRs start from the start of the method
59 // get the argument list
60 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
61 // get an iterator to arg list
62 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
65 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
67 LiveRange * ArgRange = new LiveRange(); // creates a new LR and
68 const Value *const Val = (const Value *) *ArgIt;
72 ArgRange->add( Val ); // add the arg (def) to it
73 LiveRangeMap[ Val ] = ArgRange;
75 // create a temp machine op to find the register class of value
76 //const MachineOperand Op(MachineOperand::MO_VirtualRegister);
78 unsigned rcid = MRI.getRegClassIDOfValue( Val );
79 ArgRange->setRegClass(RegClassList[ rcid ] );
83 cout << " adding LiveRange for argument ";
84 printValue( (const Value *) *ArgIt); cout << endl;
88 // Now suggest hardware registers for these method args
89 MRI.suggestRegs4MethodArgs(Meth, *this);
93 // Now find speical LLVM instructions (CALL, RET) and LRs in machine
97 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
99 for( ; BBI != Meth->end(); ++BBI) { // go thru BBs in random order
101 // Now find all LRs for machine the instructions. A new LR will be created
102 // only for defs in the machine instr since, we assume that all Values are
103 // defined before they are used. However, there can be multiple defs for
104 // the same Value in machine instructions.
106 // get the iterator for machine instructions
107 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
108 MachineCodeForBasicBlock::const_iterator
109 MInstIterator = MIVec.begin();
111 // iterate over all the machine instructions in BB
112 for( ; MInstIterator != MIVec.end(); MInstIterator++) {
114 const MachineInstr * MInst = *MInstIterator;
116 // Now if the machine instruction is a call/return instruction,
117 // add it to CallRetInstrList for processing its implicit operands
119 if( (TM.getInstrInfo()).isReturn( MInst->getOpCode()) ||
120 (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
121 CallRetInstrList.push_back( MInst );
124 // iterate over MI operands to find defs
125 for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) {
128 MachineOperand::MachineOperandType OpTyp =
129 OpI.getMachineOperand().getOperandType();
131 if ( OpTyp == MachineOperand::MO_CCRegister) {
132 cout << "\n**CC reg found. Is Def=" << OpI.isDef() << " Val:";
133 printValue( OpI.getMachineOperand().getVRegValue() );
138 // create a new LR iff this operand is a def
141 const Value *const Def = *OpI;
144 // Only instruction values are accepted for live ranges here
146 if( Def->getValueType() != Value::InstructionVal ) {
147 cout << "\n**%%Error: Def is not an instruction val. Def=";
148 printValue( Def ); cout << endl;
153 LiveRange *DefRange = LiveRangeMap[Def];
155 // see LR already there (because of multiple defs)
157 if( !DefRange) { // if it is not in LiveRangeMap
159 DefRange = new LiveRange(); // creates a new live range and
160 DefRange->add( Def ); // add the instruction (def) to it
161 LiveRangeMap[ Def ] = DefRange; // update the map
164 cout << " creating a LR for def: ";
165 printValue(Def); cout << endl;
168 // set the register class of the new live range
169 //assert( RegClassList.size() );
170 MachineOperand::MachineOperandType OpTy =
171 OpI.getMachineOperand().getOperandType();
173 bool isCC = ( OpTy == MachineOperand::MO_CCRegister);
174 unsigned rcid = MRI.getRegClassIDOfValue(
175 OpI.getMachineOperand().getVRegValue(), isCC );
178 if(isCC && DEBUG_RA) {
179 cout << "\a**created a LR for a CC reg:";
180 printValue( OpI.getMachineOperand().getVRegValue() );
183 DefRange->setRegClass( RegClassList[ rcid ] );
187 DefRange->add( Def ); // add the opearand to def range
188 // update the map - Operand points
190 LiveRangeMap[ Def ] = DefRange;
193 cout << " added to an existing LR for def: ";
194 printValue( Def ); cout << endl;
200 } // for all opereands in machine instructions
202 } // for all machine instructions in the BB
204 } // for all BBs in method
207 // Now we have to suggest clors for call and return arg live ranges.
208 // Also, if there are implicit defs (e.g., retun value of a call inst)
209 // they must be added to the live range list
211 suggestRegs4CallRets();
214 cout << "Initial Live Ranges constructed!" << endl;
220 // Suggest colors for call and return args.
221 // Also create new LRs for implicit defs
223 void LiveRangeInfo::suggestRegs4CallRets()
226 CallRetInstrListType::const_iterator It = CallRetInstrList.begin();
228 for( ; It != CallRetInstrList.end(); ++It ) {
230 const MachineInstr *MInst = *It;
231 MachineOpCode OpCode = MInst->getOpCode();
233 if( (TM.getInstrInfo()).isReturn(OpCode) )
234 MRI.suggestReg4RetValue( MInst, *this);
236 else if( (TM.getInstrInfo()).isCall( OpCode ) )
237 MRI.suggestRegs4CallArgs( MInst, *this, RegClassList );
240 assert( 0 && "Non call/ret instr in CallRetInstrList" );
248 void LiveRangeInfo::coalesceLRs()
252 for each BB in method
253 for each machine instruction (inst)
254 for each definition (def) in inst
255 for each operand (op) of inst that is a use
256 if the def and op are of the same register type
257 if the def and op do not interfere //i.e., not simultaneously live
258 if (degree(LR of def) + degree(LR of op)) <= # avail regs
259 if both LRs do not have suggested colors
260 merge2IGNodes(def, op) // i.e., merge 2 LRs
265 cout << endl << "Coalscing LRs ..." << endl;
267 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
269 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
271 // get the iterator for machine instructions
272 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
273 MachineCodeForBasicBlock::const_iterator
274 MInstIterator = MIVec.begin();
276 // iterate over all the machine instructions in BB
277 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
279 const MachineInstr * MInst = *MInstIterator;
282 cout << " *Iterating over machine instr ";
288 // iterate over MI operands to find defs
289 for(MachineInstr::val_op_const_iterator DefI(MInst);!DefI.done();++DefI){
291 if( DefI.isDef() ) { // iff this operand is a def
293 LiveRange *const LROfDef = getLiveRangeForValue( *DefI );
295 RegClass *const RCOfDef = LROfDef->getRegClass();
297 MachineInstr::val_op_const_iterator UseI(MInst);
298 for( ; !UseI.done(); ++UseI){ // for all uses
300 LiveRange *const LROfUse = getLiveRangeForValue( *UseI );
302 if( ! LROfUse ) { // if LR of use is not found
304 //don't warn about labels
305 if (!((*UseI)->getType())->isLabelType() && DEBUG_RA) {
306 cout<<" !! Warning: No LR for use "; printValue(*UseI);
309 continue; // ignore and continue
312 if( LROfUse == LROfDef) // nothing to merge if they are same
315 //RegClass *const RCOfUse = LROfUse->getRegClass();
316 //if( RCOfDef == RCOfUse ) { // if the reg classes are the same
318 if( MRI.getRegType(LROfDef) == MRI.getRegType(LROfUse) ) {
320 // If the two RegTypes are the same
322 if( ! RCOfDef->getInterference(LROfDef, LROfUse) ) {
324 unsigned CombinedDegree =
325 LROfDef->getUserIGNode()->getNumOfNeighbors() +
326 LROfUse->getUserIGNode()->getNumOfNeighbors();
328 if( CombinedDegree <= RCOfDef->getNumOfAvailRegs() ) {
330 // if both LRs do not have suggested colors
331 if( ! (LROfDef->hasSuggestedColor() &&
332 LROfUse->hasSuggestedColor() ) ) {
334 RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
335 unionAndUpdateLRs(LROfDef, LROfUse);
339 } // if combined degree is less than # of regs
341 } // if def and use do not interfere
343 }// if reg classes are the same
351 } // for all machine instructions
356 cout << endl << "Coalscing Done!" << endl;
364 /*--------------------------- Debug code for printing ---------------*/
367 void LiveRangeInfo::printLiveRanges()
369 LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
370 cout << endl << "Printing Live Ranges from Hash Map:" << endl;
371 for( ; HMI != LiveRangeMap.end() ; HMI ++ ) {
372 if( (*HMI).first && (*HMI).second ) {
373 cout <<" "; printValue((*HMI).first); cout << "\t: ";
374 ((*HMI).second)->printSet(); cout << endl;