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