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