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