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)
11 // union two live ranges into one. The 2nd LR is deleted. Used for coalescing.
12 // Note: the caller must make sure that L1 and L2 are distinct
14 void LiveRangeInfo::unionAndUpdateLRs(LiveRange *const L1, LiveRange *L2)
17 L1->setUnion( L2 ); // add elements of L2 to L1
18 ValueSet::iterator L2It;
20 for( L2It = L2->begin() ; L2It != L2->end(); ++L2It) {
22 //assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types");
24 L1->add( *L2It ); // add the var in L2 to L1
25 LiveRangeMap[ *L2It ] = L1; // now the elements in L2 should map to L1
27 delete ( L2 ); // delete L2 as it is no longer needed
33 void LiveRangeInfo::constructLiveRanges()
37 cout << "Consturcting Live Ranges ..." << endl;
39 // first find the live ranges for all incoming args of the method since
40 // those LRs start from the start of the method
42 // get the argument list
43 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
44 // get an iterator to arg list
45 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
48 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
50 LiveRange * ArgRange = new LiveRange(); // creates a new LR and
51 const Value *const Val = (const Value *) *ArgIt;
55 ArgRange->add( Val ); // add the arg (def) to it
56 LiveRangeMap[ Val ] = ArgRange;
58 // create a temp machine op to find the register class of value
59 //const MachineOperand Op(MachineOperand::MO_VirtualRegister);
61 unsigned rcid = (TM.getRegInfo()).getRegClassIDOfValue( Val );
62 ArgRange->setRegClass(RegClassList[ rcid ] );
66 cout << " adding LiveRange for argument ";
67 printValue( (const Value *) *ArgIt); cout << endl;
72 // Now find all LRs for machine the instructions. A new LR will be created
73 // only for defs in the machine instr since, we assume that all Values are
74 // defined before they are used. However, there can be multiple defs for
75 // the same Value in machine instructions.
77 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
79 for( ; BBI != Meth->end(); ++BBI) { // go thru BBs in random order
81 // get the iterator for machine instructions
82 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
83 MachineCodeForBasicBlock::const_iterator
84 MInstIterator = MIVec.begin();
86 // iterate over all the machine instructions in BB
87 for( ; MInstIterator != MIVec.end(); MInstIterator++) {
89 const MachineInstr * MInst = *MInstIterator;
91 // iterate over MI operands to find defs
92 for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); OpI++) {
94 // create a new LR iff this operand is a def
97 const Value *const Def = *OpI;
98 LiveRange *DefRange = LiveRangeMap[Def];
100 // see LR already there (because of multiple defs)
102 if( !DefRange) { // if it is not in LiveRangeMap
104 DefRange = new LiveRange(); // creates a new live range and
105 DefRange->add( Def ); // add the instruction (def) to it
106 LiveRangeMap[ Def ] = DefRange; // update the map
109 cout << " creating a LR for def: ";
110 printValue(Def); cout << endl;
113 // set the register class of the new live range
114 //assert( RegClassList.size() );
115 MachineOperand::MachineOperandType OpTy =
116 OpI.getMachineOperand().getOperandType();
118 bool isCC = ( OpTy == MachineOperand::MO_CCRegister);
119 unsigned rcid = (TM.getRegInfo()).getRegClassIDOfValue(
120 OpI.getMachineOperand().getVRegValue(), isCC );
124 cout << "\a" << "**created a LR for a CC reg**" << cout;
126 DefRange->setRegClass( RegClassList[ rcid ] );
130 DefRange->add( Def ); // add the opearand to def range
131 // update the map - Operand points
133 LiveRangeMap[ Def ] = DefRange;
136 cout << " added to an existing LR for def: ";
137 printValue( Def ); cout << endl;
146 } // for all opereands in machine instructions
148 } // for all machine instructions in the BB
150 } // for all BBs in method
153 cout << "Initial Live Ranges constructed!" << endl;
159 void LiveRangeInfo::coalesceLRs()
163 for each BB in method
164 for each machine instruction (inst)
165 for each definition (def) in inst
166 for each operand (op) of inst that is a use
167 if the def and op are of the same type
168 if the def and op do not interfere //i.e., not simultaneously live
169 if (degree(LR of def) + degree(LR of op)) <= # avail regs
170 merge2IGNodes(def, op) // i.e., merge 2 LRs
175 cout << endl << "Coalscing LRs ..." << endl;
177 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
179 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
181 // get the iterator for machine instructions
182 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
183 MachineCodeForBasicBlock::const_iterator
184 MInstIterator = MIVec.begin();
186 // iterate over all the machine instructions in BB
187 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
189 const MachineInstr * MInst = *MInstIterator;
192 cout << " *Iterating over machine instr ";
198 // iterate over MI operands to find defs
199 for(MachineInstr::val_op_const_iterator DefI(MInst);!DefI.done();++DefI){
201 if( DefI.isDef() ) { // iff this operand is a def
203 LiveRange *const LROfDef = getLiveRangeForValue( *DefI );
205 RegClass *const RCOfDef = LROfDef->getRegClass();
207 MachineInstr::val_op_const_iterator UseI(MInst);
208 for( ; !UseI.done(); ++UseI){ // for all uses
210 LiveRange *const LROfUse = getLiveRangeForValue( *UseI );
212 if( ! LROfUse ) { // if LR of use is not found
214 //don't warn about labels
215 if (!((*UseI)->getType())->isLabelType() && DEBUG_RA) {
216 cout<<" !! Warning: No LR for use "; printValue(*UseI);
219 continue; // ignore and continue
222 if( LROfUse == LROfDef) // nothing to merge if they are same
225 // RegClass *const RCOfUse = LROfUse->getRegClass();
227 //if( RCOfDef == RCOfUse ) { // if the reg classes are the same
230 if( LROfUse->getTypeID() == LROfDef->getTypeID() ) {
232 if( ! RCOfDef->getInterference(LROfDef, LROfUse) ) {
234 unsigned CombinedDegree =
235 LROfDef->getUserIGNode()->getNumOfNeighbors() +
236 LROfUse->getUserIGNode()->getNumOfNeighbors();
238 if( CombinedDegree <= RCOfDef->getNumOfAvailRegs() ) {
240 RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
241 unionAndUpdateLRs(LROfDef, LROfUse);
243 } // if combined degree is less than # of regs
245 } // if def and use do not interfere
247 } // if reg classes are the same
255 } // for all machine instructions
260 cout << endl << "Coalscing Done!" << endl;
268 /*--------------------------- Debug code for printing ---------------*/
271 void LiveRangeInfo::printLiveRanges()
273 LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
274 cout << endl << "Printing Live Ranges from Hash Map:" << endl;
275 for( ; HMI != LiveRangeMap.end() ; HMI ++ ) {
277 cout <<" "; printValue((*HMI).first); cout << "\t: ";
278 ((*HMI).second)->printSet(); cout << endl;