*** empty log message ***
[oota-llvm.git] / lib / Target / SparcV9 / RegAlloc / LiveRangeInfo.cpp
1 #include "llvm/CodeGen/LiveRangeInfo.h"
2
3 LiveRangeInfo::LiveRangeInfo(const Method *const M, 
4                              const TargetMachine& tm,
5                              vector<RegClass *> &RCL) 
6                              : Meth(M), LiveRangeMap(), 
7                                TM(tm), RegClassList(RCL)
8 { }
9
10
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
13
14 void LiveRangeInfo::unionAndUpdateLRs(LiveRange *const L1, LiveRange *L2)
15 {
16   assert( L1 != L2);
17   L1->setUnion( L2 );             // add elements of L2 to L1
18   ValueSet::iterator L2It;
19
20   for( L2It = L2->begin() ; L2It != L2->end(); ++L2It) {
21
22     //assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types");
23
24     L1->add( *L2It );            // add the var in L2 to L1
25     LiveRangeMap[ *L2It ] = L1;  // now the elements in L2 should map to L1    
26   }
27   delete ( L2 );                 // delete L2 as it is no longer needed
28 }
29
30
31
32                                  
33 void LiveRangeInfo::constructLiveRanges()
34 {  
35
36   if( DEBUG_RA) 
37     cout << "Consturcting Live Ranges ..." << endl;
38
39   // first find the live ranges for all incoming args of the method since
40   // those LRs start from the start of the method
41       
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(); 
46
47              
48   for( ; ArgIt != ArgList.end() ; ++ArgIt) {     // for each argument
49
50     LiveRange * ArgRange = new LiveRange();      // creates a new LR and 
51     const Value *const Val = (const Value *) *ArgIt;
52
53     assert( Val);
54
55     ArgRange->add( Val );     // add the arg (def) to it
56     LiveRangeMap[ Val ] = ArgRange;
57
58     // create a temp machine op to find the register class of value
59     //const MachineOperand Op(MachineOperand::MO_VirtualRegister);
60
61     unsigned rcid = (TM.getRegInfo()).getRegClassIDOfValue( Val );
62     ArgRange->setRegClass(RegClassList[ rcid ] );
63
64                            
65     if( DEBUG_RA > 1) {     
66       cout << " adding LiveRange for argument ";    
67       printValue( (const Value *) *ArgIt); cout  << endl;
68     }
69   }
70
71
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.
76
77   Method::const_iterator BBI = Meth->begin();    // random iterator for BBs   
78
79   for( ; BBI != Meth->end(); ++BBI) {            // go thru BBs in random order
80
81     // get the iterator for machine instructions
82     const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
83     MachineCodeForBasicBlock::const_iterator 
84       MInstIterator = MIVec.begin();
85
86     // iterate over all the machine instructions in BB
87     for( ; MInstIterator != MIVec.end(); MInstIterator++) {  
88       
89       const MachineInstr * MInst = *MInstIterator; 
90       
91       // iterate over  MI operands to find defs
92       for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); OpI++) {
93         
94         // create a new LR iff this operand is a def
95         if( OpI.isDef() ) {     
96           
97           const Value *const Def = *OpI;
98           LiveRange *DefRange = LiveRangeMap[Def]; 
99
100           // see LR already there (because of multiple defs)
101           
102           if( !DefRange) {                  // if it is not in LiveRangeMap
103             
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
107
108             if( DEBUG_RA > 1) {             
109               cout << "  creating a LR for def: ";    
110               printValue(Def); cout  << endl;
111             }
112
113             // set the register class of the new live range
114             //assert( RegClassList.size() );
115             MachineOperand::MachineOperandType OpTy = 
116               OpI.getMachineOperand().getOperandType();
117
118             bool isCC = ( OpTy == MachineOperand::MO_CCRegister);
119             unsigned rcid = (TM.getRegInfo()).getRegClassIDOfValue( 
120                             OpI.getMachineOperand().getVRegValue(), isCC );
121
122
123             if(isCC )
124               cout << "\a" << "**created a LR for a CC reg**" << cout;
125
126             DefRange->setRegClass( RegClassList[ rcid ] );
127
128           }
129           else {
130             DefRange->add( Def );           // add the opearand to def range
131                                             // update the map - Operand points 
132                                             // to the merged set
133             LiveRangeMap[ Def ] = DefRange; 
134
135             if( DEBUG_RA > 1) { 
136               cout << "   added to an existing LR for def: ";  
137               printValue( Def ); cout  << endl;
138             }
139           }
140
141
142
143
144         } // if isDef()
145         
146       } // for all opereands in machine instructions
147
148     } // for all machine instructions in the BB
149
150   } // for all BBs in method
151
152   if( DEBUG_RA) 
153     cout << "Initial Live Ranges constructed!" << endl;
154
155 }
156
157
158
159 void LiveRangeInfo::coalesceLRs()  
160 {
161
162 /* Algorithm:
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 
171
172 */
173
174   if( DEBUG_RA) 
175     cout << endl << "Coalscing LRs ..." << endl;
176
177   Method::const_iterator BBI = Meth->begin();  // random iterator for BBs   
178
179   for( ; BBI != Meth->end(); ++BBI) {          // traverse BBs in random order
180
181     // get the iterator for machine instructions
182     const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
183     MachineCodeForBasicBlock::const_iterator 
184       MInstIterator = MIVec.begin();
185
186     // iterate over all the machine instructions in BB
187     for( ; MInstIterator != MIVec.end(); ++MInstIterator) {  
188       
189       const MachineInstr * MInst = *MInstIterator; 
190
191       if( DEBUG_RA > 1) {
192         cout << " *Iterating over machine instr ";
193         MInst->dump();
194         cout << endl;
195       }
196
197
198       // iterate over  MI operands to find defs
199       for(MachineInstr::val_op_const_iterator DefI(MInst);!DefI.done();++DefI){
200         
201         if( DefI.isDef() ) {            // iff this operand is a def
202
203           LiveRange *const LROfDef = getLiveRangeForValue( *DefI );
204           assert( LROfDef );
205           RegClass *const RCOfDef = LROfDef->getRegClass();
206
207           MachineInstr::val_op_const_iterator UseI(MInst);
208           for( ; !UseI.done(); ++UseI){ // for all uses
209
210             LiveRange *const LROfUse = getLiveRangeForValue( *UseI );
211
212             if( ! LROfUse ) {           // if LR of use is not found
213
214               //don't warn about labels
215               if (!((*UseI)->getType())->isLabelType() && DEBUG_RA) {
216                 cout<<" !! Warning: No LR for use "; printValue(*UseI);
217                 cout << endl;
218               }
219               continue;                 // ignore and continue
220             }
221
222             if( LROfUse == LROfDef)     // nothing to merge if they are same
223               continue;
224
225             // RegClass *const RCOfUse = LROfUse->getRegClass();
226
227             //if( RCOfDef == RCOfUse ) {  // if the reg classes are the same
228
229
230             if( LROfUse->getTypeID() == LROfDef->getTypeID() ) { 
231
232               if( ! RCOfDef->getInterference(LROfDef, LROfUse) ) {
233
234                 unsigned CombinedDegree =
235                   LROfDef->getUserIGNode()->getNumOfNeighbors() + 
236                   LROfUse->getUserIGNode()->getNumOfNeighbors();
237
238                 if( CombinedDegree <= RCOfDef->getNumOfAvailRegs() ) {
239
240                   RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
241                   unionAndUpdateLRs(LROfDef, LROfUse);
242
243                 } // if combined degree is less than # of regs
244
245               } // if def and use do not interfere
246
247             } // if reg classes are the same
248
249           } // for all uses
250
251         } // if def
252
253       } // for all defs
254
255     } // for all machine instructions
256
257   } // for all BBs
258
259   if( DEBUG_RA) 
260     cout << endl << "Coalscing Done!" << endl;
261
262 }
263
264
265
266
267
268 /*--------------------------- Debug code for printing ---------------*/
269
270
271 void LiveRangeInfo::printLiveRanges()
272 {
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 ++ ) {
276     if( (*HMI).first ) {
277       cout <<" "; printValue((*HMI).first);  cout  << "\t: "; 
278       ((*HMI).second)->printSet(); cout << endl;
279     }
280   }
281 }
282
283