Vikram S. Adve | 39c94e1 | 2002-09-14 23:05:33 +0000 | [diff] [blame] | 1 | //===-- LiveRangeInfo.cpp -------------------------------------------------===// |
| 2 | // |
| 3 | // Live range construction for coloring-based register allocation for LLVM. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 7 | #include "llvm/CodeGen/LiveRangeInfo.h" |
Vikram S. Adve | 39c94e1 | 2002-09-14 23:05:33 +0000 | [diff] [blame] | 8 | #include "llvm/CodeGen/RegAllocCommon.h" |
Chris Lattner | 0a8ed94 | 2002-02-04 05:56:09 +0000 | [diff] [blame] | 9 | #include "llvm/CodeGen/RegClass.h" |
| 10 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 32be9f6 | 2002-10-28 01:41:27 +0000 | [diff] [blame] | 11 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chris Lattner | 0a8ed94 | 2002-02-04 05:56:09 +0000 | [diff] [blame] | 12 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 0be79c6 | 2002-10-28 02:28:39 +0000 | [diff] [blame] | 13 | #include "llvm/Target/MachineInstrInfo.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 14 | #include "llvm/Function.h" |
Chris Lattner | 7471a7b | 2002-02-05 03:35:53 +0000 | [diff] [blame] | 15 | #include "Support/SetOperations.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 16 | using std::cerr; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 18 | LiveRangeInfo::LiveRangeInfo(const Function *F, const TargetMachine &tm, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 19 | std::vector<RegClass *> &RCL) |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 20 | : Meth(F), TM(tm), RegClassList(RCL), MRI(tm.getRegInfo()) { } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 21 | |
| 22 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 23 | LiveRangeInfo::~LiveRangeInfo() { |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 24 | for (LiveRangeMapType::iterator MI = LiveRangeMap.begin(); |
| 25 | MI != LiveRangeMap.end(); ++MI) { |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 26 | |
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 | |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 35 | for(LiveRange::iterator LI = LR->begin(); LI != LR->end(); ++LI) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 36 | LiveRangeMap[*LI] = 0; |
| 37 | |
| 38 | delete LR; |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | |
| 44 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 45 | // 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] | 46 | // Note: the caller must make sure that L1 and L2 are distinct and both |
| 47 | // LRs don't have suggested colors |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 48 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 50 | void LiveRangeInfo::unionAndUpdateLRs(LiveRange *L1, LiveRange *L2) { |
| 51 | assert(L1 != L2 && (!L1->hasSuggestedColor() || !L2->hasSuggestedColor())); |
| 52 | set_union(*L1, *L2); // add elements of L2 to L1 |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 54 | for(ValueSet::iterator L2It = L2->begin(); L2It != L2->end(); ++L2It) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 55 | //assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types"); |
| 56 | |
Chris Lattner | 30adeb6 | 2002-02-04 16:36:59 +0000 | [diff] [blame] | 57 | L1->insert(*L2It); // add the var in L2 to L1 |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 58 | LiveRangeMap[*L2It] = L1; // now the elements in L2 should map |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 59 | //to L1 |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 60 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | // Now if LROfDef(L1) has a suggested color, it will remain. |
| 64 | // But, if LROfUse(L2) has a suggested color, the new range |
| 65 | // must have the same color. |
| 66 | |
| 67 | if(L2->hasSuggestedColor()) |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 68 | L1->setSuggestedColor(L2->getSuggestedColor()); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 69 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 71 | if (L2->isCallInterference()) |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 72 | L1->setCallInterference(); |
| 73 | |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 74 | // add the spill costs |
| 75 | L1->addSpillCost(L2->getSpillCost()); |
| 76 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 77 | delete L2; // delete L2 as it is no longer needed |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 81 | //--------------------------------------------------------------------------- |
| 82 | // Method for creating a single live range for a definition. |
| 83 | // The definition must be represented by a virtual register (a Value). |
| 84 | // Note: this function does *not* check that no live range exists for def. |
| 85 | //--------------------------------------------------------------------------- |
| 86 | |
| 87 | LiveRange* |
| 88 | LiveRangeInfo::createNewLiveRange(const Value* Def, bool isCC /* = false*/) |
| 89 | { |
| 90 | LiveRange* DefRange = new LiveRange(); // Create a new live range, |
| 91 | DefRange->insert(Def); // add Def to it, |
| 92 | LiveRangeMap[Def] = DefRange; // and update the map. |
| 93 | |
| 94 | // set the register class of the new live range |
| 95 | DefRange->setRegClass(RegClassList[MRI.getRegClassIDOfValue(Def, isCC)]); |
| 96 | |
| 97 | if (DEBUG_RA >= RA_DEBUG_LiveRanges) { |
| 98 | cerr << " Creating a LR for def "; |
| 99 | if (isCC) cerr << " (CC Register!)"; |
| 100 | cerr << " : " << RAV(Def) << "\n"; |
| 101 | } |
| 102 | return DefRange; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | LiveRange* |
| 107 | LiveRangeInfo::createOrAddToLiveRange(const Value* Def, bool isCC /* = false*/) |
| 108 | { |
| 109 | LiveRange *DefRange = LiveRangeMap[Def]; |
| 110 | |
| 111 | // check if the LR is already there (because of multiple defs) |
| 112 | if (!DefRange) { |
| 113 | DefRange = this->createNewLiveRange(Def, isCC); |
| 114 | } else { // live range already exists |
| 115 | DefRange->insert(Def); // add the operand to the range |
| 116 | LiveRangeMap[Def] = DefRange; // make operand point to merged set |
| 117 | if (DEBUG_RA >= RA_DEBUG_LiveRanges) |
| 118 | cerr << " Added to existing LR for def: " << RAV(Def) << "\n"; |
| 119 | } |
| 120 | return DefRange; |
| 121 | } |
| 122 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 123 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 124 | //--------------------------------------------------------------------------- |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 125 | // Method for constructing all live ranges in a function. It creates live |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 126 | // ranges for all values defined in the instruction stream. Also, it |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 127 | // creates live ranges for all incoming arguments of the function. |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 128 | //--------------------------------------------------------------------------- |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 129 | void LiveRangeInfo::constructLiveRanges() { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 130 | |
Vikram S. Adve | 39c94e1 | 2002-09-14 23:05:33 +0000 | [diff] [blame] | 131 | if (DEBUG_RA >= RA_DEBUG_LiveRanges) |
| 132 | cerr << "Constructing Live Ranges ...\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 134 | // first find the live ranges for all incoming args of the function since |
| 135 | // those LRs start from the start of the function |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 136 | for (Function::const_aiterator AI = Meth->abegin(); AI != Meth->aend(); ++AI) |
| 137 | this->createNewLiveRange(AI, /*isCC*/ false); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 139 | // Now suggest hardware registers for these function args |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 140 | MRI.suggestRegs4MethodArgs(Meth, *this); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 141 | |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 142 | // Now create LRs for machine instructions. A new LR will be created |
| 143 | // only for defs in the machine instr since, we assume that all Values are |
| 144 | // defined before they are used. However, there can be multiple defs for |
| 145 | // the same Value in machine instructions. |
| 146 | // |
| 147 | // Also, find CALL and RETURN instructions, which need extra work. |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 148 | // |
Vikram S. Adve | c9a0ca5 | 2002-07-08 23:07:26 +0000 | [diff] [blame] | 149 | for (Function::const_iterator BBI=Meth->begin(); BBI != Meth->end(); ++BBI){ |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 150 | // get the vector of machine instructions for this basic block. |
Chris Lattner | 32be9f6 | 2002-10-28 01:41:27 +0000 | [diff] [blame] | 151 | MachineBasicBlock& MIVec = MachineBasicBlock::get(BBI); |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 152 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 153 | // iterate over all the machine instructions in BB |
Chris Lattner | 32be9f6 | 2002-10-28 01:41:27 +0000 | [diff] [blame] | 154 | for(MachineBasicBlock::iterator MInstIterator = MIVec.begin(); |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 155 | MInstIterator != MIVec.end(); ++MInstIterator) { |
Vikram S. Adve | c9a0ca5 | 2002-07-08 23:07:26 +0000 | [diff] [blame] | 156 | MachineInstr *MInst = *MInstIterator; |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 157 | |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 158 | // If the machine instruction is a call/return instruction, add it to |
| 159 | // CallRetInstrList for processing its args, ret value, and ret addr. |
| 160 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 161 | if(TM.getInstrInfo().isReturn(MInst->getOpCode()) || |
| 162 | TM.getInstrInfo().isCall(MInst->getOpCode())) |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 163 | CallRetInstrList.push_back( MInst ); |
| 164 | |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 165 | // iterate over explicit MI operands and create a new LR |
| 166 | // for each operand that is defined by the instruction |
Vikram S. Adve | c9a0ca5 | 2002-07-08 23:07:26 +0000 | [diff] [blame] | 167 | for (MachineInstr::val_op_iterator OpI = MInst->begin(), |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 168 | OpE = MInst->end(); OpI != OpE; ++OpI) |
Chris Lattner | 30adeb6 | 2002-02-04 16:36:59 +0000 | [diff] [blame] | 169 | if (OpI.isDef()) { |
| 170 | const Value *Def = *OpI; |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 171 | bool isCC = (OpI.getMachineOperand().getOperandType() |
| 172 | == MachineOperand::MO_CCRegister); |
| 173 | this->createOrAddToLiveRange(Def, isCC); |
| 174 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 175 | |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 176 | // iterate over implicit MI operands and create a new LR |
| 177 | // for each operand that is defined by the instruction |
| 178 | for (unsigned i = 0; i < MInst->getNumImplicitRefs(); ++i) |
| 179 | if (MInst->implicitRefIsDefined(i)) { |
| 180 | const Value *Def = MInst->getImplicitRef(i); |
| 181 | this->createOrAddToLiveRange(Def, /*isCC*/ false); |
| 182 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 183 | |
| 184 | } // for all machine instructions in the BB |
| 185 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 186 | } // for all BBs in function |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 187 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 188 | // Now we have to suggest clors for call and return arg live ranges. |
| 189 | // Also, if there are implicit defs (e.g., retun value of a call inst) |
| 190 | // they must be added to the live range list |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 191 | // |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 192 | suggestRegs4CallRets(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 193 | |
Vikram S. Adve | 39c94e1 | 2002-09-14 23:05:33 +0000 | [diff] [blame] | 194 | if( DEBUG_RA >= RA_DEBUG_LiveRanges) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 195 | cerr << "Initial Live Ranges constructed!\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 199 | //--------------------------------------------------------------------------- |
| 200 | // If some live ranges must be colored with specific hardware registers |
| 201 | // (e.g., for outgoing call args), suggesting of colors for such live |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 202 | // ranges is done using target specific function. Those functions are called |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 203 | // from this function. The target specific methods must: |
| 204 | // 1) suggest colors for call and return args. |
| 205 | // 2) create new LRs for implicit defs in machine instructions |
| 206 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 207 | void LiveRangeInfo::suggestRegs4CallRets() |
| 208 | { |
Vikram S. Adve | c9a0ca5 | 2002-07-08 23:07:26 +0000 | [diff] [blame] | 209 | CallRetInstrListType::iterator It = CallRetInstrList.begin(); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 210 | for( ; It != CallRetInstrList.end(); ++It ) { |
| 211 | |
Vikram S. Adve | c9a0ca5 | 2002-07-08 23:07:26 +0000 | [diff] [blame] | 212 | MachineInstr *MInst = *It; |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 213 | MachineOpCode OpCode = MInst->getOpCode(); |
| 214 | |
| 215 | if( (TM.getInstrInfo()).isReturn(OpCode) ) |
| 216 | MRI.suggestReg4RetValue( MInst, *this); |
| 217 | |
| 218 | else if( (TM.getInstrInfo()).isCall( OpCode ) ) |
Vikram S. Adve | 9d67cd1 | 2002-09-28 17:05:22 +0000 | [diff] [blame] | 219 | MRI.suggestRegs4CallArgs( MInst, *this); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 220 | |
| 221 | else |
| 222 | assert( 0 && "Non call/ret instr in CallRetInstrList" ); |
| 223 | } |
| 224 | |
| 225 | } |
| 226 | |
| 227 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 228 | //-------------------------------------------------------------------------- |
| 229 | // The following method coalesces live ranges when possible. This method |
| 230 | // must be called after the interference graph has been constructed. |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 231 | |
| 232 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 233 | /* Algorithm: |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 234 | for each BB in function |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 235 | for each machine instruction (inst) |
| 236 | for each definition (def) in inst |
| 237 | for each operand (op) of inst that is a use |
Ruchira Sasanka | efaf9be | 2001-11-10 00:20:24 +0000 | [diff] [blame] | 238 | if the def and op are of the same register type |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 239 | if the def and op do not interfere //i.e., not simultaneously live |
| 240 | if (degree(LR of def) + degree(LR of op)) <= # avail regs |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 241 | if both LRs do not have suggested colors |
| 242 | merge2IGNodes(def, op) // i.e., merge 2 LRs |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 243 | |
| 244 | */ |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 245 | //--------------------------------------------------------------------------- |
| 246 | void LiveRangeInfo::coalesceLRs() |
| 247 | { |
Vikram S. Adve | 39c94e1 | 2002-09-14 23:05:33 +0000 | [diff] [blame] | 248 | if(DEBUG_RA >= RA_DEBUG_LiveRanges) |
| 249 | cerr << "\nCoalescing LRs ...\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 251 | for(Function::const_iterator BBI = Meth->begin(), BBE = Meth->end(); |
| 252 | BBI != BBE; ++BBI) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 253 | |
| 254 | // get the iterator for machine instructions |
Chris Lattner | 32be9f6 | 2002-10-28 01:41:27 +0000 | [diff] [blame] | 255 | const MachineBasicBlock& MIVec = MachineBasicBlock::get(BBI); |
| 256 | MachineBasicBlock::const_iterator MInstIterator = MIVec.begin(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 257 | |
| 258 | // iterate over all the machine instructions in BB |
| 259 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 260 | const MachineInstr * MInst = *MInstIterator; |
| 261 | |
Vikram S. Adve | 39c94e1 | 2002-09-14 23:05:33 +0000 | [diff] [blame] | 262 | if( DEBUG_RA >= RA_DEBUG_LiveRanges) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 263 | cerr << " *Iterating over machine instr "; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 264 | MInst->dump(); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 265 | cerr << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | // iterate over MI operands to find defs |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 270 | for(MachineInstr::const_val_op_iterator DefI = MInst->begin(), |
| 271 | DefE = MInst->end(); DefI != DefE; ++DefI) { |
| 272 | if (DefI.isDef()) { // iff this operand is a def |
| 273 | LiveRange *LROfDef = getLiveRangeForValue( *DefI ); |
| 274 | RegClass *RCOfDef = LROfDef->getRegClass(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 276 | MachineInstr::const_val_op_iterator UseI = MInst->begin(), |
| 277 | UseE = MInst->end(); |
| 278 | for( ; UseI != UseE; ++UseI){ // for all uses |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 280 | LiveRange *LROfUse = getLiveRangeForValue( *UseI ); |
| 281 | if (!LROfUse) { // if LR of use is not found |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 282 | //don't warn about labels |
Vikram S. Adve | 39c94e1 | 2002-09-14 23:05:33 +0000 | [diff] [blame] | 283 | if (!isa<BasicBlock>(*UseI) && DEBUG_RA >= RA_DEBUG_LiveRanges) |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 284 | cerr << " !! Warning: No LR for use " << RAV(*UseI) << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 285 | continue; // ignore and continue |
| 286 | } |
| 287 | |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 288 | if (LROfUse == LROfDef) // nothing to merge if they are same |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 289 | continue; |
| 290 | |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 291 | if (MRI.getRegType(LROfDef) == MRI.getRegType(LROfUse)) { |
Ruchira Sasanka | efaf9be | 2001-11-10 00:20:24 +0000 | [diff] [blame] | 292 | |
| 293 | // If the two RegTypes are the same |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 294 | if (!RCOfDef->getInterference(LROfDef, LROfUse) ) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 295 | |
| 296 | unsigned CombinedDegree = |
| 297 | LROfDef->getUserIGNode()->getNumOfNeighbors() + |
| 298 | LROfUse->getUserIGNode()->getNumOfNeighbors(); |
| 299 | |
Vikram S. Adve | 32f81a3 | 2002-09-20 00:45:47 +0000 | [diff] [blame] | 300 | if (CombinedDegree > RCOfDef->getNumOfAvailRegs()) { |
| 301 | // get more precise estimate of combined degree |
| 302 | CombinedDegree = LROfDef->getUserIGNode()-> |
| 303 | getCombinedDegree(LROfUse->getUserIGNode()); |
| 304 | } |
| 305 | |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 306 | if (CombinedDegree <= RCOfDef->getNumOfAvailRegs()) { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 307 | // if both LRs do not have suggested colors |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 308 | if (!(LROfDef->hasSuggestedColor() && |
| 309 | LROfUse->hasSuggestedColor())) { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 310 | |
| 311 | RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse); |
| 312 | unionAndUpdateLRs(LROfDef, LROfUse); |
| 313 | } |
| 314 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 315 | } // if combined degree is less than # of regs |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 316 | } // if def and use do not interfere |
Ruchira Sasanka | d33238b | 2001-10-12 17:48:18 +0000 | [diff] [blame] | 317 | }// if reg classes are the same |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 318 | } // for all uses |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 319 | } // if def |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 320 | } // for all defs |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 321 | } // for all machine instructions |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 322 | } // for all BBs |
| 323 | |
Vikram S. Adve | 39c94e1 | 2002-09-14 23:05:33 +0000 | [diff] [blame] | 324 | if (DEBUG_RA >= RA_DEBUG_LiveRanges) |
| 325 | cerr << "\nCoalescing Done!\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | /*--------------------------- Debug code for printing ---------------*/ |
| 333 | |
| 334 | |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 335 | void LiveRangeInfo::printLiveRanges() { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 336 | LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 337 | cerr << "\nPrinting Live Ranges from Hash Map:\n"; |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 338 | for( ; HMI != LiveRangeMap.end(); ++HMI) { |
| 339 | if (HMI->first && HMI->second) { |
Vikram S. Adve | 39c94e1 | 2002-09-14 23:05:33 +0000 | [diff] [blame] | 340 | cerr << " Value* " << RAV(HMI->first) << "\t: "; |
Vikram S. Adve | 32f81a3 | 2002-09-20 00:45:47 +0000 | [diff] [blame] | 341 | if (IGNode* igNode = HMI->second->getUserIGNode()) |
| 342 | cerr << "LR# " << igNode->getIndex(); |
| 343 | else |
| 344 | cerr << "LR# " << "<no-IGNode>"; |
Vikram S. Adve | 39c94e1 | 2002-09-14 23:05:33 +0000 | [diff] [blame] | 345 | cerr << "\t:Values = "; printSet(*HMI->second); cerr << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | } |