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