fixed printing messages
[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:";
125               printValue( OpI.getMachineOperand().getVRegValue() );
126             }
127
128             DefRange->setRegClass( RegClassList[ rcid ] );
129
130           }
131           else {
132             DefRange->add( Def );           // add the opearand to def range
133                                             // update the map - Operand points 
134                                             // to the merged set
135             LiveRangeMap[ Def ] = DefRange; 
136
137             if( DEBUG_RA > 1) { 
138               cout << "   added to an existing LR for def: ";  
139               printValue( Def ); cout  << endl;
140             }
141           }
142
143
144
145
146         } // if isDef()
147         
148       } // for all opereands in machine instructions
149
150     } // for all machine instructions in the BB
151
152   } // for all BBs in method
153
154   if( DEBUG_RA) 
155     cout << "Initial Live Ranges constructed!" << endl;
156
157 }
158
159
160
161 void LiveRangeInfo::coalesceLRs()  
162 {
163
164 /* Algorithm:
165    for each BB in method
166      for each machine instruction (inst)
167        for each definition (def) in inst
168          for each operand (op) of inst that is a use
169            if the def and op are of the same type
170              if the def and op do not interfere //i.e., not simultaneously live
171                if (degree(LR of def) + degree(LR of op)) <= # avail regs
172                  merge2IGNodes(def, op) // i.e., merge 2 LRs 
173
174 */
175
176   if( DEBUG_RA) 
177     cout << endl << "Coalscing LRs ..." << endl;
178
179   Method::const_iterator BBI = Meth->begin();  // random iterator for BBs   
180
181   for( ; BBI != Meth->end(); ++BBI) {          // traverse BBs in random order
182
183     // get the iterator for machine instructions
184     const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
185     MachineCodeForBasicBlock::const_iterator 
186       MInstIterator = MIVec.begin();
187
188     // iterate over all the machine instructions in BB
189     for( ; MInstIterator != MIVec.end(); ++MInstIterator) {  
190       
191       const MachineInstr * MInst = *MInstIterator; 
192
193       if( DEBUG_RA > 1) {
194         cout << " *Iterating over machine instr ";
195         MInst->dump();
196         cout << endl;
197       }
198
199
200       // iterate over  MI operands to find defs
201       for(MachineInstr::val_op_const_iterator DefI(MInst);!DefI.done();++DefI){
202         
203         if( DefI.isDef() ) {            // iff this operand is a def
204
205           LiveRange *const LROfDef = getLiveRangeForValue( *DefI );
206           assert( LROfDef );
207           RegClass *const RCOfDef = LROfDef->getRegClass();
208
209           MachineInstr::val_op_const_iterator UseI(MInst);
210           for( ; !UseI.done(); ++UseI){ // for all uses
211
212             LiveRange *const LROfUse = getLiveRangeForValue( *UseI );
213
214             if( ! LROfUse ) {           // if LR of use is not found
215
216               //don't warn about labels
217               if (!((*UseI)->getType())->isLabelType() && DEBUG_RA) {
218                 cout<<" !! Warning: No LR for use "; printValue(*UseI);
219                 cout << endl;
220               }
221               continue;                 // ignore and continue
222             }
223
224             if( LROfUse == LROfDef)     // nothing to merge if they are same
225               continue;
226
227             // RegClass *const RCOfUse = LROfUse->getRegClass();
228
229             //if( RCOfDef == RCOfUse ) {  // if the reg classes are the same
230
231
232             if( LROfUse->getTypeID() == LROfDef->getTypeID() ) { 
233
234               if( ! RCOfDef->getInterference(LROfDef, LROfUse) ) {
235
236                 unsigned CombinedDegree =
237                   LROfDef->getUserIGNode()->getNumOfNeighbors() + 
238                   LROfUse->getUserIGNode()->getNumOfNeighbors();
239
240                 if( CombinedDegree <= RCOfDef->getNumOfAvailRegs() ) {
241
242                   RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
243                   unionAndUpdateLRs(LROfDef, LROfUse);
244
245                 } // if combined degree is less than # of regs
246
247               } // if def and use do not interfere
248
249             } // if reg classes are the same
250
251           } // for all uses
252
253         } // if def
254
255       } // for all defs
256
257     } // for all machine instructions
258
259   } // for all BBs
260
261   if( DEBUG_RA) 
262     cout << endl << "Coalscing Done!" << endl;
263
264 }
265
266
267
268
269
270 /*--------------------------- Debug code for printing ---------------*/
271
272
273 void LiveRangeInfo::printLiveRanges()
274 {
275   LiveRangeMapType::iterator HMI = LiveRangeMap.begin();   // hash map iterator
276   cout << endl << "Printing Live Ranges from Hash Map:" << endl;
277   for( ; HMI != LiveRangeMap.end() ; HMI ++ ) {
278     if( (*HMI).first ) {
279       cout <<" "; printValue((*HMI).first);  cout  << "\t: "; 
280       ((*HMI).second)->printSet(); cout << endl;
281     }
282   }
283 }
284
285