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