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++) {
95 // delete later from here ************
96 MachineOperand::MachineOperandType OpTyp =
97 OpI.getMachineOperand().getOperandType();
99 if ( OpTyp == MachineOperand::MO_CCRegister) {
100 cout << "\n**CC reg found. Is Def=" << OpI.isDef() << " Val:";
101 printValue( OpI.getMachineOperand().getVRegValue() );
104 // ************* to here
108 // create a new LR iff this operand is a def
111 const Value *const Def = *OpI;
112 LiveRange *DefRange = LiveRangeMap[Def];
114 // see LR already there (because of multiple defs)
116 if( !DefRange) { // if it is not in LiveRangeMap
118 DefRange = new LiveRange(); // creates a new live range and
119 DefRange->add( Def ); // add the instruction (def) to it
120 LiveRangeMap[ Def ] = DefRange; // update the map
123 cout << " creating a LR for def: ";
124 printValue(Def); cout << endl;
127 // set the register class of the new live range
128 //assert( RegClassList.size() );
129 MachineOperand::MachineOperandType OpTy =
130 OpI.getMachineOperand().getOperandType();
132 bool isCC = ( OpTy == MachineOperand::MO_CCRegister);
133 unsigned rcid = (TM.getRegInfo()).getRegClassIDOfValue(
134 OpI.getMachineOperand().getVRegValue(), isCC );
137 if(isCC && DEBUG_RA) {
138 cout << "\a**created a LR for a CC reg:";
139 printValue( OpI.getMachineOperand().getVRegValue() );
142 DefRange->setRegClass( RegClassList[ rcid ] );
146 DefRange->add( Def ); // add the opearand to def range
147 // update the map - Operand points
149 LiveRangeMap[ Def ] = DefRange;
152 cout << " added to an existing LR for def: ";
153 printValue( Def ); cout << endl;
162 } // for all opereands in machine instructions
164 } // for all machine instructions in the BB
166 } // for all BBs in method
169 cout << "Initial Live Ranges constructed!" << endl;
175 void LiveRangeInfo::coalesceLRs()
179 for each BB in method
180 for each machine instruction (inst)
181 for each definition (def) in inst
182 for each operand (op) of inst that is a use
183 if the def and op are of the same type
184 if the def and op do not interfere //i.e., not simultaneously live
185 if (degree(LR of def) + degree(LR of op)) <= # avail regs
186 merge2IGNodes(def, op) // i.e., merge 2 LRs
191 cout << endl << "Coalscing LRs ..." << endl;
193 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
195 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
197 // get the iterator for machine instructions
198 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
199 MachineCodeForBasicBlock::const_iterator
200 MInstIterator = MIVec.begin();
202 // iterate over all the machine instructions in BB
203 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
205 const MachineInstr * MInst = *MInstIterator;
208 cout << " *Iterating over machine instr ";
214 // iterate over MI operands to find defs
215 for(MachineInstr::val_op_const_iterator DefI(MInst);!DefI.done();++DefI){
217 if( DefI.isDef() ) { // iff this operand is a def
219 LiveRange *const LROfDef = getLiveRangeForValue( *DefI );
221 RegClass *const RCOfDef = LROfDef->getRegClass();
223 MachineInstr::val_op_const_iterator UseI(MInst);
224 for( ; !UseI.done(); ++UseI){ // for all uses
226 LiveRange *const LROfUse = getLiveRangeForValue( *UseI );
228 if( ! LROfUse ) { // if LR of use is not found
230 //don't warn about labels
231 if (!((*UseI)->getType())->isLabelType() && DEBUG_RA) {
232 cout<<" !! Warning: No LR for use "; printValue(*UseI);
235 continue; // ignore and continue
238 if( LROfUse == LROfDef) // nothing to merge if they are same
241 // RegClass *const RCOfUse = LROfUse->getRegClass();
243 //if( RCOfDef == RCOfUse ) { // if the reg classes are the same
246 if( LROfUse->getTypeID() == LROfDef->getTypeID() ) {
248 if( ! RCOfDef->getInterference(LROfDef, LROfUse) ) {
250 unsigned CombinedDegree =
251 LROfDef->getUserIGNode()->getNumOfNeighbors() +
252 LROfUse->getUserIGNode()->getNumOfNeighbors();
254 if( CombinedDegree <= RCOfDef->getNumOfAvailRegs() ) {
256 RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
257 unionAndUpdateLRs(LROfDef, LROfUse);
259 } // if combined degree is less than # of regs
261 } // if def and use do not interfere
263 } // if reg classes are the same
271 } // for all machine instructions
276 cout << endl << "Coalscing Done!" << endl;
284 /*--------------------------- Debug code for printing ---------------*/
287 void LiveRangeInfo::printLiveRanges()
289 LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
290 cout << endl << "Printing Live Ranges from Hash Map:" << endl;
291 for( ; HMI != LiveRangeMap.end() ; HMI ++ ) {
293 cout <<" "; printValue((*HMI).first); cout << "\t: ";
294 ((*HMI).second)->printSet(); cout << endl;