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