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