Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 1 | #include "llvm/CodeGen/LiveRangeInfo.h" |
Chris Lattner | 0a8ed94 | 2002-02-04 05:56:09 +0000 | [diff] [blame] | 2 | #include "llvm/CodeGen/RegClass.h" |
| 3 | #include "llvm/CodeGen/MachineInstr.h" |
| 4 | #include "llvm/Target/TargetMachine.h" |
| 5 | #include "llvm/Method.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 6 | #include <iostream> |
| 7 | using std::cerr; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 8 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 9 | //--------------------------------------------------------------------------- |
| 10 | // Constructor |
| 11 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 12 | LiveRangeInfo::LiveRangeInfo(const Method *const M, |
| 13 | const TargetMachine& tm, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 14 | std::vector<RegClass *> &RCL) |
| 15 | : Meth(M), LiveRangeMap(), TM(tm), |
| 16 | RegClassList(RCL), MRI(tm.getRegInfo()) |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 17 | { } |
| 18 | |
| 19 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 20 | //--------------------------------------------------------------------------- |
| 21 | // Destructor: Deletes all LiveRanges in the LiveRangeMap |
| 22 | //--------------------------------------------------------------------------- |
| 23 | LiveRangeInfo::~LiveRangeInfo() { |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 24 | LiveRangeMapType::iterator MI = LiveRangeMap.begin(); |
| 25 | |
| 26 | for( ; MI != LiveRangeMap.end() ; ++MI) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 27 | if (MI->first && MI->second) { |
| 28 | LiveRange *LR = MI->second; |
| 29 | |
| 30 | // we need to be careful in deleting LiveRanges in LiveRangeMap |
| 31 | // since two/more Values in the live range map can point to the same |
| 32 | // live range. We have to make the other entries NULL when we delete |
| 33 | // a live range. |
| 34 | |
| 35 | LiveRange::iterator LI = LR->begin(); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 37 | for( ; LI != LR->end() ; ++LI) |
| 38 | LiveRangeMap[*LI] = 0; |
| 39 | |
| 40 | delete LR; |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | |
| 46 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 47 | // union two live ranges into one. The 2nd LR is deleted. Used for coalescing. |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 48 | // Note: the caller must make sure that L1 and L2 are distinct and both |
| 49 | // LRs don't have suggested colors |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 50 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 51 | void LiveRangeInfo::unionAndUpdateLRs(LiveRange *const L1, LiveRange *L2) |
| 52 | { |
| 53 | assert( L1 != L2); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 54 | L1->setUnion( L2 ); // add elements of L2 to L1 |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 55 | ValueSet::iterator L2It; |
| 56 | |
| 57 | for( L2It = L2->begin() ; L2It != L2->end(); ++L2It) { |
| 58 | |
| 59 | //assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types"); |
| 60 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 61 | L1->add( *L2It ); // add the var in L2 to L1 |
| 62 | LiveRangeMap[ *L2It ] = L1; // now the elements in L2 should map |
| 63 | //to L1 |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 64 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | // Now if LROfDef(L1) has a suggested color, it will remain. |
| 68 | // But, if LROfUse(L2) has a suggested color, the new range |
| 69 | // must have the same color. |
| 70 | |
| 71 | if(L2->hasSuggestedColor()) |
| 72 | L1->setSuggestedColor( L2->getSuggestedColor() ); |
| 73 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 74 | |
| 75 | if( L2->isCallInterference() ) |
| 76 | L1->setCallInterference(); |
| 77 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 78 | |
| 79 | L1->addSpillCost( L2->getSpillCost() ); // add the spill costs |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 81 | delete L2; // delete L2 as it is no longer needed |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 86 | //--------------------------------------------------------------------------- |
| 87 | // Method for constructing all live ranges in a method. It creates live |
| 88 | // ranges for all values defined in the instruction stream. Also, it |
| 89 | // creates live ranges for all incoming arguments of the method. |
| 90 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 91 | void LiveRangeInfo::constructLiveRanges() |
| 92 | { |
| 93 | |
| 94 | if( DEBUG_RA) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 95 | cerr << "Consturcting Live Ranges ...\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 96 | |
| 97 | // first find the live ranges for all incoming args of the method since |
| 98 | // those LRs start from the start of the method |
| 99 | |
| 100 | // get the argument list |
| 101 | const Method::ArgumentListType& ArgList = Meth->getArgumentList(); |
| 102 | // get an iterator to arg list |
| 103 | Method::ArgumentListType::const_iterator ArgIt = ArgList.begin(); |
| 104 | |
| 105 | |
| 106 | for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 107 | LiveRange * ArgRange = new LiveRange(); // creates a new LR and |
| 108 | const Value *const Val = (const Value *) *ArgIt; |
| 109 | |
| 110 | assert( Val); |
| 111 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 112 | ArgRange->add(Val); // add the arg (def) to it |
| 113 | LiveRangeMap[Val] = ArgRange; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 114 | |
| 115 | // create a temp machine op to find the register class of value |
| 116 | //const MachineOperand Op(MachineOperand::MO_VirtualRegister); |
| 117 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 118 | unsigned rcid = MRI.getRegClassIDOfValue( Val ); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 119 | ArgRange->setRegClass(RegClassList[ rcid ] ); |
| 120 | |
| 121 | |
| 122 | if( DEBUG_RA > 1) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 123 | cerr << " adding LiveRange for argument "; |
| 124 | printValue((const Value *) *ArgIt); cerr << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 128 | // Now suggest hardware registers for these method args |
| 129 | MRI.suggestRegs4MethodArgs(Meth, *this); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 130 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | // Now find speical LLVM instructions (CALL, RET) and LRs in machine |
| 134 | // instructions. |
| 135 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 136 | |
| 137 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 138 | for( ; BBI != Meth->end(); ++BBI) { // go thru BBs in random order |
| 139 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 140 | // Now find all LRs for machine the instructions. A new LR will be created |
| 141 | // only for defs in the machine instr since, we assume that all Values are |
| 142 | // defined before they are used. However, there can be multiple defs for |
| 143 | // the same Value in machine instructions. |
| 144 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 145 | // get the iterator for machine instructions |
| 146 | const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 147 | MachineCodeForBasicBlock::const_iterator MInstIterator = MIVec.begin(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 148 | |
| 149 | // iterate over all the machine instructions in BB |
| 150 | for( ; MInstIterator != MIVec.end(); MInstIterator++) { |
| 151 | |
| 152 | const MachineInstr * MInst = *MInstIterator; |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 153 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 154 | // Now if the machine instruction is a call/return instruction, |
| 155 | // add it to CallRetInstrList for processing its implicit operands |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 157 | if(TM.getInstrInfo().isReturn(MInst->getOpCode()) || |
| 158 | TM.getInstrInfo().isCall(MInst->getOpCode())) |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 159 | CallRetInstrList.push_back( MInst ); |
| 160 | |
| 161 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 162 | // iterate over MI operands to find defs |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 163 | for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done(); ++OpI) { |
| 164 | if(DEBUG_RA) { |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 165 | MachineOperand::MachineOperandType OpTyp = |
| 166 | OpI.getMachineOperand().getOperandType(); |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 168 | if (OpTyp == MachineOperand::MO_CCRegister) { |
| 169 | cerr << "\n**CC reg found. Is Def=" << OpI.isDef() << " Val:"; |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 170 | printValue( OpI.getMachineOperand().getVRegValue() ); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 171 | cerr << "\n"; |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 172 | } |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 173 | } |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 174 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 175 | // create a new LR iff this operand is a def |
| 176 | if( OpI.isDef() ) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 177 | const Value *const Def = *OpI; |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 178 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 179 | // Only instruction values are accepted for live ranges here |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 180 | if( Def->getValueType() != Value::InstructionVal ) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 181 | cerr << "\n**%%Error: Def is not an instruction val. Def="; |
| 182 | printValue( Def ); cerr << "\n"; |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 183 | continue; |
| 184 | } |
| 185 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 186 | LiveRange *DefRange = LiveRangeMap[Def]; |
| 187 | |
| 188 | // see LR already there (because of multiple defs) |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 189 | if( !DefRange) { // if it is not in LiveRangeMap |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 190 | DefRange = new LiveRange(); // creates a new live range and |
| 191 | DefRange->add( Def ); // add the instruction (def) to it |
| 192 | LiveRangeMap[ Def ] = DefRange; // update the map |
| 193 | |
| 194 | if( DEBUG_RA > 1) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 195 | cerr << " creating a LR for def: "; |
| 196 | printValue(Def); cerr << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // set the register class of the new live range |
| 200 | //assert( RegClassList.size() ); |
| 201 | MachineOperand::MachineOperandType OpTy = |
| 202 | OpI.getMachineOperand().getOperandType(); |
| 203 | |
| 204 | bool isCC = ( OpTy == MachineOperand::MO_CCRegister); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 205 | unsigned rcid = MRI.getRegClassIDOfValue( |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 206 | OpI.getMachineOperand().getVRegValue(), isCC ); |
| 207 | |
| 208 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 209 | if(isCC && DEBUG_RA) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 210 | cerr << "\a**created a LR for a CC reg:"; |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 211 | printValue( OpI.getMachineOperand().getVRegValue() ); |
| 212 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 213 | |
| 214 | DefRange->setRegClass( RegClassList[ rcid ] ); |
| 215 | |
| 216 | } |
| 217 | else { |
| 218 | DefRange->add( Def ); // add the opearand to def range |
| 219 | // update the map - Operand points |
| 220 | // to the merged set |
| 221 | LiveRangeMap[ Def ] = DefRange; |
| 222 | |
| 223 | if( DEBUG_RA > 1) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 224 | cerr << " added to an existing LR for def: "; |
| 225 | printValue( Def ); cerr << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 229 | } // if isDef() |
| 230 | |
| 231 | } // for all opereands in machine instructions |
| 232 | |
| 233 | } // for all machine instructions in the BB |
| 234 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 235 | } // for all BBs in method |
| 236 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 237 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 238 | // Now we have to suggest clors for call and return arg live ranges. |
| 239 | // Also, if there are implicit defs (e.g., retun value of a call inst) |
| 240 | // they must be added to the live range list |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 241 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 242 | suggestRegs4CallRets(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 243 | |
| 244 | if( DEBUG_RA) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 245 | cerr << "Initial Live Ranges constructed!\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 246 | |
| 247 | } |
| 248 | |
| 249 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 250 | //--------------------------------------------------------------------------- |
| 251 | // If some live ranges must be colored with specific hardware registers |
| 252 | // (e.g., for outgoing call args), suggesting of colors for such live |
| 253 | // ranges is done using target specific method. Those methods are called |
| 254 | // from this function. The target specific methods must: |
| 255 | // 1) suggest colors for call and return args. |
| 256 | // 2) create new LRs for implicit defs in machine instructions |
| 257 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 258 | void LiveRangeInfo::suggestRegs4CallRets() |
| 259 | { |
| 260 | |
| 261 | CallRetInstrListType::const_iterator It = CallRetInstrList.begin(); |
| 262 | |
| 263 | for( ; It != CallRetInstrList.end(); ++It ) { |
| 264 | |
| 265 | const MachineInstr *MInst = *It; |
| 266 | MachineOpCode OpCode = MInst->getOpCode(); |
| 267 | |
| 268 | if( (TM.getInstrInfo()).isReturn(OpCode) ) |
| 269 | MRI.suggestReg4RetValue( MInst, *this); |
| 270 | |
| 271 | else if( (TM.getInstrInfo()).isCall( OpCode ) ) |
| 272 | MRI.suggestRegs4CallArgs( MInst, *this, RegClassList ); |
| 273 | |
| 274 | else |
| 275 | assert( 0 && "Non call/ret instr in CallRetInstrList" ); |
| 276 | } |
| 277 | |
| 278 | } |
| 279 | |
| 280 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 281 | //-------------------------------------------------------------------------- |
| 282 | // The following method coalesces live ranges when possible. This method |
| 283 | // must be called after the interference graph has been constructed. |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 284 | |
| 285 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 286 | /* Algorithm: |
| 287 | for each BB in method |
| 288 | for each machine instruction (inst) |
| 289 | for each definition (def) in inst |
| 290 | for each operand (op) of inst that is a use |
Ruchira Sasanka | efaf9be | 2001-11-10 00:20:24 +0000 | [diff] [blame] | 291 | if the def and op are of the same register type |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 292 | if the def and op do not interfere //i.e., not simultaneously live |
| 293 | if (degree(LR of def) + degree(LR of op)) <= # avail regs |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 294 | if both LRs do not have suggested colors |
| 295 | merge2IGNodes(def, op) // i.e., merge 2 LRs |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 296 | |
| 297 | */ |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 298 | //--------------------------------------------------------------------------- |
| 299 | void LiveRangeInfo::coalesceLRs() |
| 300 | { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 301 | if( DEBUG_RA) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 302 | cerr << "\nCoalscing LRs ...\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 303 | |
| 304 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 305 | |
| 306 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 307 | |
| 308 | // get the iterator for machine instructions |
| 309 | const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 310 | MachineCodeForBasicBlock::const_iterator MInstIterator = MIVec.begin(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 311 | |
| 312 | // iterate over all the machine instructions in BB |
| 313 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
| 314 | |
| 315 | const MachineInstr * MInst = *MInstIterator; |
| 316 | |
| 317 | if( DEBUG_RA > 1) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 318 | cerr << " *Iterating over machine instr "; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 319 | MInst->dump(); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 320 | cerr << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | |
| 324 | // iterate over MI operands to find defs |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 325 | for(MachineInstr::val_const_op_iterator DefI(MInst);!DefI.done();++DefI){ |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 326 | |
| 327 | if( DefI.isDef() ) { // iff this operand is a def |
| 328 | |
| 329 | LiveRange *const LROfDef = getLiveRangeForValue( *DefI ); |
| 330 | assert( LROfDef ); |
| 331 | RegClass *const RCOfDef = LROfDef->getRegClass(); |
| 332 | |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 333 | MachineInstr::val_const_op_iterator UseI(MInst); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 334 | for( ; !UseI.done(); ++UseI){ // for all uses |
| 335 | |
| 336 | LiveRange *const LROfUse = getLiveRangeForValue( *UseI ); |
| 337 | |
| 338 | if( ! LROfUse ) { // if LR of use is not found |
| 339 | |
| 340 | //don't warn about labels |
| 341 | if (!((*UseI)->getType())->isLabelType() && DEBUG_RA) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 342 | cerr<<" !! Warning: No LR for use "; printValue(*UseI); |
| 343 | cerr << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 344 | } |
| 345 | continue; // ignore and continue |
| 346 | } |
| 347 | |
| 348 | if( LROfUse == LROfDef) // nothing to merge if they are same |
| 349 | continue; |
| 350 | |
Ruchira Sasanka | efaf9be | 2001-11-10 00:20:24 +0000 | [diff] [blame] | 351 | //RegClass *const RCOfUse = LROfUse->getRegClass(); |
| 352 | //if( RCOfDef == RCOfUse ) { // if the reg classes are the same |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 353 | |
Ruchira Sasanka | efaf9be | 2001-11-10 00:20:24 +0000 | [diff] [blame] | 354 | if( MRI.getRegType(LROfDef) == MRI.getRegType(LROfUse) ) { |
| 355 | |
| 356 | // If the two RegTypes are the same |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 357 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 358 | if( ! RCOfDef->getInterference(LROfDef, LROfUse) ) { |
| 359 | |
| 360 | unsigned CombinedDegree = |
| 361 | LROfDef->getUserIGNode()->getNumOfNeighbors() + |
| 362 | LROfUse->getUserIGNode()->getNumOfNeighbors(); |
| 363 | |
| 364 | if( CombinedDegree <= RCOfDef->getNumOfAvailRegs() ) { |
| 365 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 366 | // if both LRs do not have suggested colors |
| 367 | if( ! (LROfDef->hasSuggestedColor() && |
| 368 | LROfUse->hasSuggestedColor() ) ) { |
| 369 | |
| 370 | RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse); |
| 371 | unionAndUpdateLRs(LROfDef, LROfUse); |
| 372 | } |
| 373 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 374 | |
| 375 | } // if combined degree is less than # of regs |
| 376 | |
| 377 | } // if def and use do not interfere |
| 378 | |
Ruchira Sasanka | d33238b | 2001-10-12 17:48:18 +0000 | [diff] [blame] | 379 | }// if reg classes are the same |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 380 | |
| 381 | } // for all uses |
| 382 | |
| 383 | } // if def |
| 384 | |
| 385 | } // for all defs |
| 386 | |
| 387 | } // for all machine instructions |
| 388 | |
| 389 | } // for all BBs |
| 390 | |
| 391 | if( DEBUG_RA) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 392 | cerr << "\nCoalscing Done!\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 393 | |
| 394 | } |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | /*--------------------------- Debug code for printing ---------------*/ |
| 401 | |
| 402 | |
| 403 | void LiveRangeInfo::printLiveRanges() |
| 404 | { |
| 405 | LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 406 | cerr << "\nPrinting Live Ranges from Hash Map:\n"; |
| 407 | for( ; HMI != LiveRangeMap.end() ; ++HMI) { |
| 408 | if( HMI->first && HMI->second ) { |
| 409 | cerr <<" "; printValue((*HMI).first); cerr << "\t: "; |
| 410 | HMI->second->printSet(); cerr << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | |