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() );
39 delete ( L2 ); // delete L2 as it is no longer needed
45 void LiveRangeInfo::constructLiveRanges()
49 cout << "Consturcting Live Ranges ..." << endl;
51 // first find the live ranges for all incoming args of the method since
52 // those LRs start from the start of the method
54 // get the argument list
55 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
56 // get an iterator to arg list
57 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
60 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
62 LiveRange * ArgRange = new LiveRange(); // creates a new LR and
63 const Value *const Val = (const Value *) *ArgIt;
67 ArgRange->add( Val ); // add the arg (def) to it
68 LiveRangeMap[ Val ] = ArgRange;
70 // create a temp machine op to find the register class of value
71 //const MachineOperand Op(MachineOperand::MO_VirtualRegister);
73 unsigned rcid = MRI.getRegClassIDOfValue( Val );
74 ArgRange->setRegClass(RegClassList[ rcid ] );
78 cout << " adding LiveRange for argument ";
79 printValue( (const Value *) *ArgIt); cout << endl;
83 // Now suggest hardware registers for these method args
84 MRI.suggestRegs4MethodArgs(Meth, *this);
88 // Now find speical LLVM instructions (CALL, RET) and LRs in machine
92 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
94 for( ; BBI != Meth->end(); ++BBI) { // go thru BBs in random order
96 // Now find all LRs for machine the instructions. A new LR will be created
97 // only for defs in the machine instr since, we assume that all Values are
98 // defined before they are used. However, there can be multiple defs for
99 // the same Value in machine instructions.
101 // get the iterator for machine instructions
102 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
103 MachineCodeForBasicBlock::const_iterator
104 MInstIterator = MIVec.begin();
106 // iterate over all the machine instructions in BB
107 for( ; MInstIterator != MIVec.end(); MInstIterator++) {
109 const MachineInstr * MInst = *MInstIterator;
111 // Now if the machine instruction has special operands that must be
112 // set with a "suggested color", do it here.
115 if( MRI.handleSpecialMInstr(MInst, *this, RegClassList) )
119 // iterate over MI operands to find defs
120 for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) {
123 // delete later from here ************
124 MachineOperand::MachineOperandType OpTyp =
125 OpI.getMachineOperand().getOperandType();
127 if (DEBUG_RA && OpTyp == MachineOperand::MO_CCRegister) {
128 cout << "\n**CC reg found. Is Def=" << OpI.isDef() << " Val:";
129 printValue( OpI.getMachineOperand().getVRegValue() );
132 // ************* to here
135 // create a new LR iff this operand is a def
138 const Value *const Def = *OpI;
141 // Only instruction values are accepted for live ranges here
143 if( Def->getValueType() != Value::InstructionVal ) {
144 cout << "\n**%%Error: Def is not an instruction val. Def=";
145 printValue( Def ); cout << endl;
150 LiveRange *DefRange = LiveRangeMap[Def];
152 // see LR already there (because of multiple defs)
154 if( !DefRange) { // if it is not in LiveRangeMap
156 DefRange = new LiveRange(); // creates a new live range and
157 DefRange->add( Def ); // add the instruction (def) to it
158 LiveRangeMap[ Def ] = DefRange; // update the map
161 cout << " creating a LR for def: ";
162 printValue(Def); cout << endl;
165 // set the register class of the new live range
166 //assert( RegClassList.size() );
167 MachineOperand::MachineOperandType OpTy =
168 OpI.getMachineOperand().getOperandType();
170 bool isCC = ( OpTy == MachineOperand::MO_CCRegister);
171 unsigned rcid = MRI.getRegClassIDOfValue(
172 OpI.getMachineOperand().getVRegValue(), isCC );
175 if(isCC && DEBUG_RA) {
176 cout << "\a**created a LR for a CC reg:";
177 printValue( OpI.getMachineOperand().getVRegValue() );
180 DefRange->setRegClass( RegClassList[ rcid ] );
184 DefRange->add( Def ); // add the opearand to def range
185 // update the map - Operand points
187 LiveRangeMap[ Def ] = DefRange;
190 cout << " added to an existing LR for def: ";
191 printValue( Def ); cout << endl;
200 } // for all opereands in machine instructions
202 } // for all machine instructions in the BB
205 } // for all BBs in method
207 // go thru LLVM instructions in the basic block and suggest colors
208 // for their args. Also record all CALL
209 // instructions and Return instructions in the CallRetInstrList
210 // This is done because since there are no reverse pointers in machine
211 // instructions to find the llvm instruction, when we encounter a call
212 // or a return whose args must be specailly colored (e.g., %o's for args)
213 // We have to makes sure that all LRs of call/ret args are added before
214 // doing this. But return value of call will not have a LR.
216 BBI = Meth->begin(); // random iterator for BBs
218 for( ; BBI != Meth->end(); ++BBI) { // go thru BBs in random order
220 BasicBlock::const_iterator InstIt = (*BBI)->begin();
222 for( ; InstIt != (*BBI)->end() ; ++InstIt) {
224 const Instruction *const CallRetI = *InstIt;
225 unsigned OpCode = (CallRetI)->getOpcode();
227 if( (OpCode == Instruction::Call) ) {
228 CallRetInstrList.push_back(CallRetI );
229 MRI.suggestRegs4CallArgs( (CallInst *) CallRetI, *this, RegClassList );
232 else if (OpCode == Instruction::Ret ) {
233 CallRetInstrList.push_back( CallRetI );
234 MRI.suggestReg4RetValue( (ReturnInst *) CallRetI, *this);
238 } // for each llvm instr in BB
240 } // for all BBs in method
243 cout << "Initial Live Ranges constructed!" << endl;
249 void LiveRangeInfo::coalesceLRs()
253 for each BB in method
254 for each machine instruction (inst)
255 for each definition (def) in inst
256 for each operand (op) of inst that is a use
257 if the def and op are of the same type
258 if the def and op do not interfere //i.e., not simultaneously live
259 if (degree(LR of def) + degree(LR of op)) <= # avail regs
260 if both LRs do not have suggested colors
261 merge2IGNodes(def, op) // i.e., merge 2 LRs
266 cout << endl << "Coalscing LRs ..." << endl;
268 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
270 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
272 // get the iterator for machine instructions
273 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
274 MachineCodeForBasicBlock::const_iterator
275 MInstIterator = MIVec.begin();
277 // iterate over all the machine instructions in BB
278 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
280 const MachineInstr * MInst = *MInstIterator;
283 cout << " *Iterating over machine instr ";
289 // iterate over MI operands to find defs
290 for(MachineInstr::val_op_const_iterator DefI(MInst);!DefI.done();++DefI){
292 if( DefI.isDef() ) { // iff this operand is a def
294 LiveRange *const LROfDef = getLiveRangeForValue( *DefI );
296 RegClass *const RCOfDef = LROfDef->getRegClass();
298 MachineInstr::val_op_const_iterator UseI(MInst);
299 for( ; !UseI.done(); ++UseI){ // for all uses
301 LiveRange *const LROfUse = getLiveRangeForValue( *UseI );
303 if( ! LROfUse ) { // if LR of use is not found
305 //don't warn about labels
306 if (!((*UseI)->getType())->isLabelType() && DEBUG_RA) {
307 cout<<" !! Warning: No LR for use "; printValue(*UseI);
310 continue; // ignore and continue
313 if( LROfUse == LROfDef) // nothing to merge if they are same
316 // RegClass *const RCOfUse = LROfUse->getRegClass();
318 //if( RCOfDef == RCOfUse ) { // if the reg classes are the same
321 if( LROfUse->getTypeID() == LROfDef->getTypeID() ) {
323 if( ! RCOfDef->getInterference(LROfDef, LROfUse) ) {
325 unsigned CombinedDegree =
326 LROfDef->getUserIGNode()->getNumOfNeighbors() +
327 LROfUse->getUserIGNode()->getNumOfNeighbors();
329 if( CombinedDegree <= RCOfDef->getNumOfAvailRegs() ) {
331 // if both LRs do not have suggested colors
332 if( ! (LROfDef->hasSuggestedColor() &&
333 LROfUse->hasSuggestedColor() ) ) {
335 RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
336 unionAndUpdateLRs(LROfDef, LROfUse);
340 } // if combined degree is less than # of regs
342 } // if def and use do not interfere
344 } // if reg classes are the same
352 } // for all machine instructions
357 cout << endl << "Coalscing Done!" << endl;
365 /*--------------------------- Debug code for printing ---------------*/
368 void LiveRangeInfo::printLiveRanges()
370 LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
371 cout << endl << "Printing Live Ranges from Hash Map:" << endl;
372 for( ; HMI != LiveRangeMap.end() ; HMI ++ ) {
373 if( (*HMI).first && (*HMI).second ) {
374 cout <<" "; printValue((*HMI).first); cout << "\t: ";
375 ((*HMI).second)->printSet(); cout << endl;