Ruchira Sasanka | 94d86e9 | 2001-09-14 20:31:39 +0000 | [diff] [blame] | 1 | #include "llvm/Target/Sparc.h" |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 2 | #include "SparcInternals.h" |
| 3 | #include "llvm/Method.h" |
| 4 | #include "llvm/iTerminators.h" |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 5 | #include "llvm/iOther.h" |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 6 | #include "llvm/CodeGen/InstrScheduling.h" |
| 7 | #include "llvm/CodeGen/InstrSelection.h" |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 8 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 9 | #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h" |
| 10 | #include "llvm/CodeGen/PhyRegAlloc.h" |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 11 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | |
| 15 | //--------------------------------------------------------------------------- |
| 16 | // UltraSparcRegInfo |
| 17 | //--------------------------------------------------------------------------- |
| 18 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 19 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 20 | // Finds the return value of a call instruction |
| 21 | //--------------------------------------------------------------------------- |
| 22 | |
| 23 | const Value * |
| 24 | UltraSparcRegInfo::getCallInstRetVal(const MachineInstr *CallMI) const{ |
| 25 | |
| 26 | unsigned OpCode = CallMI->getOpCode(); |
| 27 | unsigned NumOfImpRefs = CallMI->getNumImplicitRefs(); |
| 28 | |
| 29 | if( OpCode == CALL ) { |
| 30 | |
| 31 | // The one before the last implicit operand is the return value of |
| 32 | // a CALL instr |
| 33 | if( NumOfImpRefs > 1 ) |
| 34 | if( CallMI->implicitRefIsDefined(NumOfImpRefs-2) ) |
| 35 | return CallMI->getImplicitRef(NumOfImpRefs-2); |
| 36 | |
| 37 | } |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 38 | else if( OpCode == JMPLCALL) { |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 39 | |
| 40 | // The last implicit operand is the return value of a JMPL in |
| 41 | if( NumOfImpRefs > 0 ) |
| 42 | if( CallMI->implicitRefIsDefined(NumOfImpRefs-1) ) |
| 43 | return CallMI->getImplicitRef(NumOfImpRefs-1); |
| 44 | } |
| 45 | else |
| 46 | assert(0 && "OpCode must be CALL/JMPL for a call instr"); |
| 47 | |
| 48 | return NULL; |
| 49 | |
| 50 | } |
| 51 | |
| 52 | //--------------------------------------------------------------------------- |
| 53 | // Finds the return address of a call instruction |
| 54 | //--------------------------------------------------------------------------- |
| 55 | |
| 56 | const Value * |
| 57 | UltraSparcRegInfo::getCallInstRetAddr(const MachineInstr *CallMI)const { |
| 58 | |
| 59 | unsigned OpCode = CallMI->getOpCode(); |
| 60 | |
| 61 | if( OpCode == CALL) { |
| 62 | |
| 63 | unsigned NumOfImpRefs = CallMI->getNumImplicitRefs(); |
| 64 | |
| 65 | assert( NumOfImpRefs && "CALL instr must have at least on ImpRef"); |
| 66 | // The last implicit operand is the return address of a CALL instr |
| 67 | return CallMI->getImplicitRef(NumOfImpRefs-1); |
| 68 | |
| 69 | } |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 70 | else if( OpCode == JMPLCALL ) { |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 71 | |
| 72 | MachineOperand & MO = ( MachineOperand &) CallMI->getOperand(2); |
| 73 | return MO.getVRegValue(); |
| 74 | |
| 75 | } |
| 76 | else |
| 77 | assert(0 && "OpCode must be CALL/JMPL for a call instr"); |
| 78 | |
| 79 | assert(0 && "There must be a return addr for a call instr"); |
| 80 | |
| 81 | return NULL; |
| 82 | |
| 83 | } |
| 84 | |
| 85 | |
| 86 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 87 | // Finds the # of actual arguments of the call instruction |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 88 | //--------------------------------------------------------------------------- |
| 89 | |
| 90 | const unsigned |
| 91 | UltraSparcRegInfo::getCallInstNumArgs(const MachineInstr *CallMI) const { |
| 92 | |
| 93 | unsigned OpCode = CallMI->getOpCode(); |
| 94 | unsigned NumOfImpRefs = CallMI->getNumImplicitRefs(); |
| 95 | int NumArgs = -1; |
| 96 | |
| 97 | if( OpCode == CALL ) { |
| 98 | |
| 99 | switch( NumOfImpRefs ) { |
| 100 | |
| 101 | case 0: assert(0 && "A CALL inst must have at least one ImpRef (RetAddr)"); |
| 102 | |
| 103 | case 1: NumArgs = 0; |
| 104 | break; |
| 105 | |
| 106 | default: // two or more implicit refs |
| 107 | if( CallMI->implicitRefIsDefined(NumOfImpRefs-2) ) |
| 108 | NumArgs = NumOfImpRefs - 2; // i.e., NumOfImpRef-2 is the ret val |
| 109 | else |
| 110 | NumArgs = NumOfImpRefs - 1; |
| 111 | } |
| 112 | |
| 113 | } |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 114 | else if( OpCode == JMPLCALL ) { |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 115 | |
| 116 | // The last implicit operand is the return value of a JMPL instr |
| 117 | if( NumOfImpRefs > 0 ) { |
| 118 | if( CallMI->implicitRefIsDefined(NumOfImpRefs-1) ) |
| 119 | NumArgs = NumOfImpRefs - 1; // i.e., NumOfImpRef-1 is the ret val |
| 120 | else |
| 121 | NumArgs = NumOfImpRefs; |
| 122 | } |
| 123 | else |
| 124 | NumArgs = NumOfImpRefs; |
| 125 | } |
| 126 | else |
| 127 | assert(0 && "OpCode must be CALL/JMPL for a call instr"); |
| 128 | |
| 129 | assert( (NumArgs != -1) && "Internal error in getCallInstNumArgs" ); |
| 130 | return (unsigned) NumArgs; |
| 131 | |
| 132 | |
| 133 | } |
| 134 | |
| 135 | |
| 136 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 137 | // Suggests a register for the ret address in the RET machine instruction |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 138 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 139 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 140 | void UltraSparcRegInfo::suggestReg4RetAddr(const MachineInstr * RetMI, |
| 141 | LiveRangeInfo& LRI) const { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 142 | |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 143 | assert( (RetMI->getNumOperands() >= 2) |
| 144 | && "JMPL/RETURN must have 3 and 2 operands respectively"); |
| 145 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 146 | MachineOperand & MO = ( MachineOperand &) RetMI->getOperand(0); |
| 147 | |
| 148 | MO.setRegForValue( getUnifiedRegNum( IntRegClassID, SparcIntRegOrder::i7) ); |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 149 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 150 | // TODO (Optimize): |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 151 | // Instead of setting the color, we can suggest one. In that case, |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 152 | // we have to test later whether it received the suggested color. |
| 153 | // In that case, a LR has to be created at the start of method. |
| 154 | // It has to be done as follows (remove the setRegVal above): |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 155 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 156 | /* |
| 157 | const Value *RetAddrVal = MO.getVRegValue(); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 158 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 159 | assert( RetAddrVal && "LR for ret address must be created at start"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 160 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 161 | LiveRange * RetAddrLR = LRI.getLiveRangeForValue( RetAddrVal); |
| 162 | RetAddrLR->setSuggestedColor(getUnifiedRegNum( IntRegClassID, |
| 163 | SparcIntRegOrdr::i7) ); |
| 164 | */ |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 165 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 166 | |
| 167 | } |
| 168 | |
| 169 | |
| 170 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 171 | // Suggests a register for the ret address in the JMPL/CALL machine instr |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 172 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 173 | void UltraSparcRegInfo::suggestReg4CallAddr(const MachineInstr * CallMI, |
| 174 | LiveRangeInfo& LRI, |
| 175 | vector<RegClass *> RCList) const { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 176 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 177 | |
| 178 | const Value *RetAddrVal = getCallInstRetAddr( CallMI ); |
| 179 | |
| 180 | // RetAddrVal cannot be NULL (asserted in getCallInstRetAddr) |
| 181 | // create a new LR for the return address and color it |
| 182 | |
| 183 | LiveRange * RetAddrLR = new LiveRange(); |
| 184 | RetAddrLR->add( RetAddrVal ); |
| 185 | unsigned RegClassID = getRegClassIDOfValue( RetAddrVal ); |
| 186 | RetAddrLR->setRegClass( RCList[RegClassID] ); |
| 187 | RetAddrLR->setColor(getUnifiedRegNum(IntRegClassID,SparcIntRegOrder::o7)); |
| 188 | LRI.addLRToMap( RetAddrVal, RetAddrLR); |
| 189 | |
| 190 | |
| 191 | /* |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 192 | assert( (CallMI->getNumOperands() == 3) && "JMPL must have 3 operands"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 193 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 194 | // directly set color since the LR of ret address (if there were one) |
| 195 | // will not extend after the call instr |
| 196 | |
| 197 | MachineOperand & MO = ( MachineOperand &) CallMI->getOperand(2); |
| 198 | MO.setRegForValue( getUnifiedRegNum( IntRegClassID,SparcIntRegOrder::o7) ); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 199 | |
| 200 | */ |
| 201 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 204 | |
| 205 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 206 | |
| 207 | //--------------------------------------------------------------------------- |
| 208 | // This method will suggest colors to incoming args to a method. |
| 209 | // If the arg is passed on stack due to the lack of regs, NOTHING will be |
| 210 | // done - it will be colored (or spilled) as a normal value. |
| 211 | //--------------------------------------------------------------------------- |
| 212 | |
| 213 | void UltraSparcRegInfo::suggestRegs4MethodArgs(const Method *const Meth, |
| 214 | LiveRangeInfo& LRI) const |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 215 | { |
| 216 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 217 | // get the argument list |
| 218 | const Method::ArgumentListType& ArgList = Meth->getArgumentList(); |
| 219 | // get an iterator to arg list |
| 220 | Method::ArgumentListType::const_iterator ArgIt = ArgList.begin(); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 221 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 222 | // for each argument |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 223 | for( unsigned argNo=0; ArgIt != ArgList.end() ; ++ArgIt, ++argNo) { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 224 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 225 | // get the LR of arg |
| 226 | LiveRange *const LR = LRI.getLiveRangeForValue((const Value *) *ArgIt); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 227 | assert( LR && "No live range found for method arg"); |
| 228 | |
| 229 | unsigned RegType = getRegType( LR ); |
| 230 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 231 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 232 | // if the arg is in int class - allocate a reg for an int arg |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 233 | if( RegType == IntRegType ) { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 234 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 235 | if( argNo < NumOfIntArgRegs) { |
| 236 | LR->setSuggestedColor( SparcIntRegOrder::i0 + argNo ); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 237 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | else { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 241 | // Do NOTHING as this will be colored as a normal value. |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 242 | if (DEBUG_RA) cerr << " Int Regr not suggested for method arg\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 243 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 245 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 246 | else if( RegType==FPSingleRegType && (argNo*2+1) < NumOfFloatArgRegs) |
| 247 | LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2 + 1) ); |
| 248 | |
| 249 | |
| 250 | else if( RegType == FPDoubleRegType && (argNo*2) < NumOfFloatArgRegs) |
| 251 | LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2) ); |
| 252 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 254 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 258 | //--------------------------------------------------------------------------- |
| 259 | // |
| 260 | //--------------------------------------------------------------------------- |
| 261 | |
| 262 | void UltraSparcRegInfo::colorMethodArgs(const Method *const Meth, |
| 263 | LiveRangeInfo& LRI, |
| 264 | AddedInstrns *const FirstAI) const { |
| 265 | |
| 266 | // get the argument list |
| 267 | const Method::ArgumentListType& ArgList = Meth->getArgumentList(); |
| 268 | // get an iterator to arg list |
| 269 | Method::ArgumentListType::const_iterator ArgIt = ArgList.begin(); |
| 270 | |
| 271 | MachineInstr *AdMI; |
| 272 | |
| 273 | |
| 274 | // for each argument |
| 275 | for( unsigned argNo=0; ArgIt != ArgList.end() ; ++ArgIt, ++argNo) { |
| 276 | |
| 277 | // get the LR of arg |
| 278 | LiveRange *const LR = LRI.getLiveRangeForValue((const Value *) *ArgIt); |
| 279 | assert( LR && "No live range found for method arg"); |
| 280 | |
| 281 | |
| 282 | // if the LR received the suggested color, NOTHING to be done |
| 283 | if( LR->hasSuggestedColor() && LR->hasColor() ) |
| 284 | if( LR->getSuggestedColor() == LR->getColor() ) |
| 285 | continue; |
| 286 | |
| 287 | // We are here because the LR did not have a suggested |
| 288 | // color or did not receive the suggested color. Now handle |
| 289 | // individual cases. |
| 290 | |
| 291 | |
| 292 | unsigned RegType = getRegType( LR ); |
| 293 | unsigned RegClassID = (LR->getRegClass())->getID(); |
| 294 | |
| 295 | |
| 296 | // find whether this argument is coming in a register (if not, on stack) |
| 297 | |
| 298 | bool isArgInReg = false; |
| 299 | unsigned UniArgReg = InvalidRegNum; |
| 300 | |
| 301 | if( (RegType== IntRegType && argNo < NumOfIntArgRegs)) { |
| 302 | isArgInReg = true; |
| 303 | UniArgReg = getUnifiedRegNum( RegClassID, SparcIntRegOrder::o0 + argNo ); |
| 304 | } |
| 305 | else if(RegType == FPSingleRegType && argNo < NumOfFloatArgRegs) { |
| 306 | isArgInReg = true; |
| 307 | UniArgReg = getUnifiedRegNum( RegClassID, |
| 308 | SparcFloatRegOrder::f0 + argNo*2 + 1 ) ; |
| 309 | } |
| 310 | else if(RegType == FPDoubleRegType && argNo < NumOfFloatArgRegs) { |
| 311 | isArgInReg = true; |
| 312 | UniArgReg = getUnifiedRegNum(RegClassID, SparcFloatRegOrder::f0+argNo*2); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | if( LR->hasColor() ) { |
| 317 | |
| 318 | // We are here because the LR did not have a suggested |
| 319 | // color or did not receive the suggested color but LR got a register. |
| 320 | // Now we have to copy %ix reg (or stack pos of arg) |
| 321 | // to the register it was colored with. |
| 322 | |
| 323 | unsigned UniLRReg = getUnifiedRegNum( RegClassID, LR->getColor() ); |
| 324 | |
| 325 | // if the arg is coming in a register and goes into a register |
| 326 | if( isArgInReg ) |
| 327 | AdMI = cpReg2RegMI(UniArgReg, UniLRReg, RegType ); |
| 328 | |
| 329 | else |
| 330 | assert(0 && "TODO: Color an Incoming arg on stack"); |
| 331 | |
| 332 | // Now add the instruction |
| 333 | FirstAI->InstrnsBefore.push_back( AdMI ); |
| 334 | |
| 335 | } |
| 336 | |
| 337 | else { // LR is not colored (i.e., spilled) |
| 338 | |
| 339 | assert(0 && "TODO: Color a spilled arg "); |
| 340 | |
| 341 | } |
| 342 | |
| 343 | |
| 344 | } // for each incoming argument |
| 345 | |
| 346 | } |
| 347 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 348 | |
| 349 | |
| 350 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 351 | //--------------------------------------------------------------------------- |
| 352 | // This method is called before graph coloring to suggest colors to the |
| 353 | // outgoing call args and the return value of the call. |
| 354 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 355 | void UltraSparcRegInfo::suggestRegs4CallArgs(const MachineInstr *const CallMI, |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 356 | LiveRangeInfo& LRI, |
| 357 | vector<RegClass *> RCList) const { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 358 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 359 | assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) ); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 360 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 361 | suggestReg4CallAddr(CallMI, LRI, RCList); |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 363 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 364 | // First color the return value of the call instruction. The return value |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 365 | // will be in %o0 if the value is an integer type, or in %f0 if the |
| 366 | // value is a float type. |
| 367 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 368 | // the return value cannot have a LR in machine instruction since it is |
| 369 | // only defined by the call instruction |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 370 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 371 | // if type is not void, create a new live range and set its |
| 372 | // register class and add to LRI |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 373 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 374 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 375 | const Value *RetVal = getCallInstRetVal( CallMI ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 376 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 377 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 378 | if( RetVal ) { |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 379 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 380 | assert( (! LRI.getLiveRangeForValue( RetVal ) ) && |
| 381 | "LR for ret Value of call already definded!"); |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 382 | |
| 383 | |
| 384 | // create a new LR for the return value |
| 385 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 386 | LiveRange * RetValLR = new LiveRange(); |
| 387 | RetValLR->add( RetVal ); |
| 388 | unsigned RegClassID = getRegClassIDOfValue( RetVal ); |
| 389 | RetValLR->setRegClass( RCList[RegClassID] ); |
| 390 | LRI.addLRToMap( RetVal, RetValLR); |
| 391 | |
| 392 | // now suggest a register depending on the register class of ret arg |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 393 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 394 | if( RegClassID == IntRegClassID ) |
| 395 | RetValLR->setSuggestedColor(SparcIntRegOrder::o0); |
| 396 | else if (RegClassID == FloatRegClassID ) |
| 397 | RetValLR->setSuggestedColor(SparcFloatRegOrder::f0 ); |
| 398 | else assert( 0 && "Unknown reg class for return value of call\n"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 400 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 401 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 402 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 403 | // Now suggest colors for arguments (operands) of the call instruction. |
| 404 | // Colors are suggested only if the arg number is smaller than the |
| 405 | // the number of registers allocated for argument passing. |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 406 | // Now, go thru call args - implicit operands of the call MI |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 407 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 408 | unsigned NumOfCallArgs = getCallInstNumArgs( CallMI ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 409 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 410 | for(unsigned argNo=0, i=0; i < NumOfCallArgs; ++i, ++argNo ) { |
| 411 | |
| 412 | const Value *CallArg = CallMI->getImplicitRef(i); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 413 | |
| 414 | // get the LR of call operand (parameter) |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 415 | LiveRange *const LR = LRI.getLiveRangeForValue(CallArg); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 416 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 417 | // not possible to have a null LR since all args (even consts) |
| 418 | // must be defined before |
| 419 | if( !LR ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 420 | if( DEBUG_RA) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 421 | cerr << " ERROR: In call instr, no LR for arg: " ; |
| 422 | printValue(CallArg); cerr << endl; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 423 | } |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 424 | assert(0 && "NO LR for call arg"); |
| 425 | // continue; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | unsigned RegType = getRegType( LR ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 429 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 430 | // if the arg is in int class - allocate a reg for an int arg |
| 431 | if( RegType == IntRegType ) { |
| 432 | |
| 433 | if( argNo < NumOfIntArgRegs) |
| 434 | LR->setSuggestedColor( SparcIntRegOrder::o0 + argNo ); |
| 435 | |
| 436 | else if (DEBUG_RA) |
| 437 | // Do NOTHING as this will be colored as a normal value. |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 438 | cerr << " Regr not suggested for int call arg" << endl; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 439 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 440 | } |
| 441 | else if( RegType == FPSingleRegType && (argNo*2 +1)< NumOfFloatArgRegs) |
| 442 | LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2 + 1) ); |
| 443 | |
| 444 | |
| 445 | else if( RegType == FPDoubleRegType && (argNo*2) < NumOfFloatArgRegs) |
| 446 | LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2) ); |
| 447 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 448 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 449 | } // for all call arguments |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 454 | //--------------------------------------------------------------------------- |
| 455 | // After graph coloring, we have call this method to see whehter the return |
| 456 | // value and the call args received the correct colors. If not, we have |
| 457 | // to instert copy instructions. |
| 458 | //--------------------------------------------------------------------------- |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 459 | |
| 460 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 461 | void UltraSparcRegInfo::colorCallArgs(const MachineInstr *const CallMI, |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 462 | LiveRangeInfo& LRI, |
| 463 | AddedInstrns *const CallAI) const { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 464 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 465 | assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) ); |
| 466 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 467 | // First color the return value of the call. |
| 468 | // If there is a LR for the return value, it means this |
| 469 | // method returns a value |
| 470 | |
| 471 | MachineInstr *AdMI; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 472 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 473 | const Value *RetVal = getCallInstRetVal( CallMI ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 474 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 475 | if( RetVal ) { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 476 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 477 | LiveRange * RetValLR = LRI.getLiveRangeForValue( RetVal ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 478 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 479 | if( !RetValLR ) { |
| 480 | cerr << "\nNo LR for:"; |
| 481 | printValue( RetVal ); |
| 482 | cerr << endl; |
| 483 | assert( RetValLR && "ERR:No LR for non-void return value"); |
| 484 | //return; |
| 485 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 486 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 487 | bool recvSugColor = false; |
| 488 | |
| 489 | if( RetValLR->hasSuggestedColor() && RetValLR->hasColor() ) |
| 490 | if( RetValLR->getSuggestedColor() == RetValLR->getColor()) |
| 491 | recvSugColor = true; |
| 492 | |
| 493 | // if we didn't receive the suggested color for some reason, |
| 494 | // put copy instruction |
| 495 | |
| 496 | if( !recvSugColor ) { |
| 497 | |
| 498 | if( RetValLR->hasColor() ) { |
| 499 | |
| 500 | unsigned RegType = getRegType( RetValLR ); |
| 501 | unsigned RegClassID = (RetValLR->getRegClass())->getID(); |
| 502 | |
| 503 | unsigned |
| 504 | UniRetLRReg=getUnifiedRegNum(RegClassID,RetValLR->getColor()); |
| 505 | unsigned UniRetReg = InvalidRegNum; |
| 506 | |
| 507 | // find where we receive the return value depending on |
| 508 | // register class |
| 509 | |
| 510 | if(RegClassID == IntRegClassID) |
| 511 | UniRetReg = getUnifiedRegNum( RegClassID, SparcIntRegOrder::o0); |
| 512 | else if(RegClassID == FloatRegClassID) |
| 513 | UniRetReg = getUnifiedRegNum( RegClassID, SparcFloatRegOrder::f0); |
| 514 | |
| 515 | |
| 516 | AdMI = cpReg2RegMI(UniRetReg, UniRetLRReg, RegType ); |
| 517 | CallAI->InstrnsAfter.push_back( AdMI ); |
| 518 | |
| 519 | |
| 520 | } // if LR has color |
| 521 | else { |
| 522 | |
| 523 | assert(0 && "LR of return value is splilled"); |
| 524 | } |
| 525 | |
| 526 | |
| 527 | } // the LR didn't receive the suggested color |
| 528 | |
| 529 | } // if there a return value |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 530 | |
| 531 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 532 | // Now color all args of the call instruction |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 533 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 534 | unsigned NumOfCallArgs = getCallInstNumArgs( CallMI ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 535 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 536 | for(unsigned argNo=0, i=0; i < NumOfCallArgs; ++i, ++argNo ) { |
| 537 | |
| 538 | const Value *CallArg = CallMI->getImplicitRef(i); |
| 539 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 540 | // get the LR of call operand (parameter) |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 541 | LiveRange *const LR = LRI.getLiveRangeForValue(CallArg); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 542 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 543 | unsigned RegType = getRegType( CallArg ); |
| 544 | unsigned RegClassID = getRegClassIDOfValue( CallArg); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 545 | |
| 546 | // find whether this argument is coming in a register (if not, on stack) |
| 547 | |
| 548 | bool isArgInReg = false; |
| 549 | unsigned UniArgReg = InvalidRegNum; |
| 550 | |
| 551 | if( (RegType== IntRegType && argNo < NumOfIntArgRegs)) { |
| 552 | isArgInReg = true; |
| 553 | UniArgReg = getUnifiedRegNum(RegClassID, SparcIntRegOrder::o0 + argNo ); |
| 554 | } |
| 555 | else if(RegType == FPSingleRegType && argNo < NumOfFloatArgRegs) { |
| 556 | isArgInReg = true; |
| 557 | UniArgReg = getUnifiedRegNum(RegClassID, |
| 558 | SparcFloatRegOrder::f0 + (argNo*2 + 1) ); |
| 559 | } |
| 560 | else if(RegType == FPDoubleRegType && argNo < NumOfFloatArgRegs) { |
| 561 | isArgInReg = true; |
| 562 | UniArgReg = getUnifiedRegNum(RegClassID, SparcFloatRegOrder::f0+argNo*2); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 563 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 564 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 565 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 566 | // not possible to have a null LR since all args (even consts) |
| 567 | // must be defined before |
| 568 | if( !LR ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 569 | if( DEBUG_RA) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 570 | cerr << " ERROR: In call instr, no LR for arg: " ; |
| 571 | printValue(CallArg); cerr << endl; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 572 | } |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 573 | assert(0 && "NO LR for call arg"); |
| 574 | // continue; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 575 | } |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 576 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 577 | |
| 578 | // if the LR received the suggested color, NOTHING to do |
| 579 | |
| 580 | if( LR->hasSuggestedColor() && LR->hasColor() ) |
| 581 | if( LR->getSuggestedColor() == LR->getColor() ) |
| 582 | continue; |
| 583 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 584 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 585 | if( LR->hasColor() ) { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 586 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 587 | // We are here because though the LR is allocated a register, it |
| 588 | // was not allocated the suggested register. So, we have to copy %ix reg |
| 589 | // (or stack pos of arg) to the register it was colored with |
| 590 | |
| 591 | |
| 592 | unsigned UniLRReg = getUnifiedRegNum( RegClassID, LR->getColor() ); |
| 593 | |
| 594 | if( isArgInReg ) |
| 595 | AdMI = cpReg2RegMI(UniLRReg, UniArgReg, RegType ); |
| 596 | |
| 597 | else |
| 598 | assert(0 && "TODO: Push an outgoing arg on stack"); |
| 599 | |
| 600 | // Now add the instruction |
| 601 | CallAI->InstrnsBefore.push_back( AdMI ); |
| 602 | |
| 603 | } |
| 604 | |
| 605 | else { // LR is not colored (i.e., spilled) |
| 606 | |
| 607 | assert(0 && "TODO: Copy a spilled call arg to an output reg "); |
| 608 | |
| 609 | } |
| 610 | |
| 611 | } // for each parameter in call instruction |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 612 | |
| 613 | } |
| 614 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 615 | //--------------------------------------------------------------------------- |
| 616 | // This method is called for an LLVM return instruction to identify which |
| 617 | // values will be returned from this method and to suggest colors. |
| 618 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 619 | void UltraSparcRegInfo::suggestReg4RetValue(const MachineInstr *const RetMI, |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 620 | LiveRangeInfo& LRI) const { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 621 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 622 | assert( (UltraSparcInfo->getInstrInfo()).isReturn( RetMI->getOpCode() ) ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 623 | |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame^] | 624 | suggestReg4RetAddr(RetMI, LRI); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 625 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 626 | // if there is an implicit ref, that has to be the ret value |
| 627 | if( RetMI->getNumImplicitRefs() > 0 ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 628 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 629 | // The first implicit operand is the return value of a return instr |
| 630 | const Value *RetVal = RetMI->getImplicitRef(0); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 631 | |
| 632 | MachineInstr *AdMI; |
| 633 | LiveRange *const LR = LRI.getLiveRangeForValue( RetVal ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 634 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 635 | if( !LR ) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 636 | cerr << "\nNo LR for:"; |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 637 | printValue( RetVal ); |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 638 | cerr << endl; |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 639 | assert( LR && "No LR for return value of non-void method"); |
| 640 | //return; |
| 641 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 642 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 643 | unsigned RegClassID = (LR->getRegClass())->getID(); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 644 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 645 | if( RegClassID == IntRegClassID ) |
| 646 | LR->setSuggestedColor(SparcIntRegOrder::i0); |
| 647 | |
| 648 | else if ( RegClassID == FloatRegClassID ) |
| 649 | LR->setSuggestedColor(SparcFloatRegOrder::f0); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 650 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 651 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 652 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | //--------------------------------------------------------------------------- |
| 656 | |
| 657 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 658 | void UltraSparcRegInfo::colorRetValue(const MachineInstr *const RetMI, |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 659 | LiveRangeInfo& LRI, |
| 660 | AddedInstrns *const RetAI) const { |
| 661 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 662 | assert( (UltraSparcInfo->getInstrInfo()).isReturn( RetMI->getOpCode() ) ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 663 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 664 | // if there is an implicit ref, that has to be the ret value |
| 665 | if( RetMI->getNumImplicitRefs() > 0 ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 666 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 667 | // The first implicit operand is the return value of a return instr |
| 668 | const Value *RetVal = RetMI->getImplicitRef(0); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 669 | |
| 670 | MachineInstr *AdMI; |
| 671 | LiveRange *const LR = LRI.getLiveRangeForValue( RetVal ); |
| 672 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 673 | if( ! LR ) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 674 | cerr << "\nNo LR for:"; |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 675 | printValue( RetVal ); |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 676 | cerr << endl; |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 677 | // assert( LR && "No LR for return value of non-void method"); |
| 678 | return; |
| 679 | } |
| 680 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 681 | unsigned RegClassID = getRegClassIDOfValue(RetVal); |
| 682 | unsigned RegType = getRegType( RetVal ); |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame^] | 683 | |
| 684 | |
| 685 | unsigned CorrectCol; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 686 | if(RegClassID == IntRegClassID) |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame^] | 687 | CorrectCol = SparcIntRegOrder::i0; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 688 | else if(RegClassID == FloatRegClassID) |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame^] | 689 | CorrectCol = SparcFloatRegOrder::f0; |
| 690 | else |
| 691 | assert( 0 && "Unknown RegClass"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 692 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 693 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 694 | // if the LR received the suggested color, NOTHING to do |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 695 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 696 | if( LR->hasSuggestedColor() && LR->hasColor() ) |
| 697 | if( LR->getSuggestedColor() == LR->getColor() ) |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame^] | 698 | if( LR->getColor() == CorrectCol ) |
| 699 | return; |
| 700 | |
| 701 | unsigned UniRetReg = getUnifiedRegNum( RegClassID, CorrectCol ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 702 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 703 | if( LR->hasColor() ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 704 | |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame^] | 705 | // We are here because the LR was allocted a regiter |
| 706 | // It may be the suggested register or not |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 707 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 708 | // copy the LR of retun value to i0 or f0 |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 709 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 710 | unsigned UniLRReg =getUnifiedRegNum( RegClassID, LR->getColor()); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 711 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 712 | AdMI = cpReg2RegMI( UniLRReg, UniRetReg, RegType); |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame^] | 713 | RetAI->InstrnsBefore.push_back( AdMI ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 714 | } |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 715 | else |
| 716 | assert(0 && "TODO: Copy the return value from stack\n"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 717 | |
| 718 | } // if there is a return value |
| 719 | |
| 720 | } |
| 721 | |
| 722 | |
| 723 | //--------------------------------------------------------------------------- |
| 724 | // Copy from a register to register. Register number must be the unified |
| 725 | // register number |
| 726 | //--------------------------------------------------------------------------- |
| 727 | |
| 728 | |
| 729 | MachineInstr * UltraSparcRegInfo::cpReg2RegMI(const unsigned SrcReg, |
| 730 | const unsigned DestReg, |
| 731 | const int RegType) const { |
| 732 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 733 | assert( ((int)SrcReg != InvalidRegNum) && ((int)DestReg != InvalidRegNum) && |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 734 | "Invalid Register"); |
| 735 | |
| 736 | MachineInstr * MI = NULL; |
| 737 | |
| 738 | switch( RegType ) { |
| 739 | |
| 740 | case IntRegType: |
Ruchira Sasanka | 735d6e3 | 2001-10-18 22:38:52 +0000 | [diff] [blame] | 741 | case IntCCRegType: |
| 742 | case FloatCCRegType: |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 743 | MI = new MachineInstr(ADD, 3); |
| 744 | MI->SetMachineOperand(0, SrcReg, false); |
| 745 | MI->SetMachineOperand(1, SparcIntRegOrder::g0, false); |
| 746 | MI->SetMachineOperand(2, DestReg, true); |
| 747 | break; |
| 748 | |
| 749 | case FPSingleRegType: |
| 750 | MI = new MachineInstr(FMOVS, 2); |
| 751 | MI->SetMachineOperand(0, SrcReg, false); |
| 752 | MI->SetMachineOperand(1, DestReg, true); |
| 753 | break; |
| 754 | |
| 755 | case FPDoubleRegType: |
| 756 | MI = new MachineInstr(FMOVD, 2); |
| 757 | MI->SetMachineOperand(0, SrcReg, false); |
| 758 | MI->SetMachineOperand(1, DestReg, true); |
| 759 | break; |
| 760 | |
| 761 | default: |
| 762 | assert(0 && "Unknow RegType"); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | return MI; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 766 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 767 | |
| 768 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 769 | //--------------------------------------------------------------------------- |
| 770 | // Copy from a register to memory. Register number must be the unified |
| 771 | // register number |
| 772 | //--------------------------------------------------------------------------- |
| 773 | |
| 774 | |
| 775 | MachineInstr * UltraSparcRegInfo::cpReg2MemMI(const unsigned SrcReg, |
| 776 | const unsigned DestPtrReg, |
| 777 | const int Offset, |
| 778 | const int RegType) const { |
| 779 | |
| 780 | |
| 781 | MachineInstr * MI = NULL; |
| 782 | |
| 783 | switch( RegType ) { |
| 784 | |
| 785 | case IntRegType: |
Ruchira Sasanka | 735d6e3 | 2001-10-18 22:38:52 +0000 | [diff] [blame] | 786 | case IntCCRegType: |
| 787 | case FloatCCRegType: |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 788 | MI = new MachineInstr(STX, 3); |
| 789 | MI->SetMachineOperand(0, DestPtrReg, false); |
| 790 | MI->SetMachineOperand(1, SrcReg, false); |
| 791 | MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, |
| 792 | (int64_t) Offset, false); |
| 793 | break; |
| 794 | |
| 795 | case FPSingleRegType: |
| 796 | MI = new MachineInstr(ST, 3); |
| 797 | MI->SetMachineOperand(0, DestPtrReg, false); |
| 798 | MI->SetMachineOperand(1, SrcReg, false); |
| 799 | MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, |
| 800 | (int64_t) Offset, false); |
| 801 | break; |
| 802 | |
| 803 | case FPDoubleRegType: |
| 804 | MI = new MachineInstr(STD, 3); |
| 805 | MI->SetMachineOperand(0, DestPtrReg, false); |
| 806 | MI->SetMachineOperand(1, SrcReg, false); |
| 807 | MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, |
| 808 | (int64_t) Offset, false); |
| 809 | break; |
| 810 | |
| 811 | default: |
| 812 | assert(0 && "Unknow RegType"); |
| 813 | } |
| 814 | |
| 815 | return MI; |
| 816 | } |
| 817 | |
| 818 | |
| 819 | //--------------------------------------------------------------------------- |
| 820 | // Copy from memory to a reg. Register number must be the unified |
| 821 | // register number |
| 822 | //--------------------------------------------------------------------------- |
| 823 | |
| 824 | |
| 825 | MachineInstr * UltraSparcRegInfo::cpMem2RegMI(const unsigned SrcPtrReg, |
| 826 | const int Offset, |
| 827 | const unsigned DestReg, |
| 828 | const int RegType) const { |
| 829 | |
| 830 | MachineInstr * MI = NULL; |
| 831 | |
| 832 | switch( RegType ) { |
| 833 | |
| 834 | case IntRegType: |
Ruchira Sasanka | 735d6e3 | 2001-10-18 22:38:52 +0000 | [diff] [blame] | 835 | case IntCCRegType: |
| 836 | case FloatCCRegType: |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 837 | MI = new MachineInstr(LDX, 3); |
| 838 | MI->SetMachineOperand(0, SrcPtrReg, false); |
| 839 | MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, |
| 840 | (int64_t) Offset, false); |
| 841 | MI->SetMachineOperand(2, DestReg, false); |
| 842 | break; |
| 843 | |
| 844 | case FPSingleRegType: |
| 845 | MI = new MachineInstr(LD, 3); |
| 846 | MI->SetMachineOperand(0, SrcPtrReg, false); |
| 847 | MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, |
| 848 | (int64_t) Offset, false); |
| 849 | MI->SetMachineOperand(2, DestReg, false); |
| 850 | |
| 851 | break; |
| 852 | |
| 853 | case FPDoubleRegType: |
| 854 | MI = new MachineInstr(LDD, 3); |
| 855 | MI->SetMachineOperand(0, SrcPtrReg, false); |
| 856 | MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, |
| 857 | (int64_t) Offset, false); |
| 858 | MI->SetMachineOperand(2, DestReg, false); |
| 859 | break; |
| 860 | |
| 861 | default: |
| 862 | assert(0 && "Unknow RegType"); |
| 863 | } |
| 864 | |
| 865 | return MI; |
| 866 | } |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 874 | |
| 875 | |
| 876 | //--------------------------------------------------------------------------- |
| 877 | // Only constant/label values are accepted. |
| 878 | // ***This code is temporary *** |
| 879 | //--------------------------------------------------------------------------- |
| 880 | |
| 881 | |
| 882 | MachineInstr * UltraSparcRegInfo::cpValue2RegMI(Value * Val, |
| 883 | const unsigned DestReg, |
| 884 | const int RegType) const { |
| 885 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 886 | assert( ((int)DestReg != InvalidRegNum) && "Invalid Register"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 887 | |
| 888 | /* |
| 889 | unsigned MReg; |
| 890 | int64_t Imm; |
| 891 | |
| 892 | MachineOperand::MachineOperandType MOTypeInt = |
| 893 | ChooseRegOrImmed(Val, ADD, *UltraSparcInfo, true, MReg, Imm); |
| 894 | */ |
| 895 | |
| 896 | MachineOperand::MachineOperandType MOType; |
| 897 | |
| 898 | switch( Val->getValueType() ) { |
| 899 | |
| 900 | case Value::ConstantVal: |
Chris Lattner | ef9c23f | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 901 | case Value::GlobalVariableVal: |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 902 | MOType = MachineOperand:: MO_UnextendedImmed; // TODO**** correct??? |
| 903 | break; |
| 904 | |
| 905 | case Value::BasicBlockVal: |
| 906 | case Value::MethodVal: |
| 907 | MOType = MachineOperand::MO_PCRelativeDisp; |
| 908 | break; |
| 909 | |
| 910 | default: |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 911 | cerr << "Value Type: " << Val->getValueType() << endl; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 912 | assert(0 && "Unknown val type - Only constants/globals/labels are valid"); |
| 913 | } |
| 914 | |
| 915 | |
| 916 | |
| 917 | MachineInstr * MI = NULL; |
| 918 | |
| 919 | switch( RegType ) { |
| 920 | |
| 921 | case IntRegType: |
| 922 | MI = new MachineInstr(ADD); |
| 923 | MI->SetMachineOperand(0, MOType, Val, false); |
| 924 | MI->SetMachineOperand(1, SparcIntRegOrder::g0, false); |
| 925 | MI->SetMachineOperand(2, DestReg, true); |
| 926 | break; |
| 927 | |
| 928 | case FPSingleRegType: |
| 929 | assert(0 && "FP const move not yet implemented"); |
| 930 | MI = new MachineInstr(FMOVS); |
| 931 | MI->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, Val, false); |
| 932 | MI->SetMachineOperand(1, DestReg, true); |
| 933 | break; |
| 934 | |
| 935 | case FPDoubleRegType: |
| 936 | assert(0 && "FP const move not yet implemented"); |
| 937 | MI = new MachineInstr(FMOVD); |
| 938 | MI->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, Val, false); |
| 939 | MI->SetMachineOperand(1, DestReg, true); |
| 940 | break; |
| 941 | |
| 942 | default: |
| 943 | assert(0 && "Unknow RegType"); |
| 944 | } |
| 945 | |
| 946 | return MI; |
| 947 | } |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 955 | //--------------------------------------------------------------------------- |
| 956 | // Print the register assigned to a LR |
| 957 | //--------------------------------------------------------------------------- |
| 958 | |
| 959 | void UltraSparcRegInfo::printReg(const LiveRange *const LR) { |
| 960 | |
| 961 | unsigned RegClassID = (LR->getRegClass())->getID(); |
| 962 | |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 963 | cerr << " *Node " << (LR->getUserIGNode())->getIndex(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 964 | |
| 965 | if( ! LR->hasColor() ) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 966 | cerr << " - could not find a color" << endl; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 967 | return; |
| 968 | } |
| 969 | |
| 970 | // if a color is found |
| 971 | |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 972 | cerr << " colored with color "<< LR->getColor(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 973 | |
| 974 | if( RegClassID == IntRegClassID ) { |
| 975 | |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 976 | cerr<< " [" << SparcIntRegOrder::getRegName(LR->getColor()) ; |
| 977 | cerr << "]" << endl; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 978 | } |
| 979 | else if ( RegClassID == FloatRegClassID) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 980 | cerr << "[" << SparcFloatRegOrder::getRegName(LR->getColor()); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 981 | if( LR->getTypeID() == Type::DoubleTyID ) |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 982 | cerr << "+" << SparcFloatRegOrder::getRegName(LR->getColor()+1); |
| 983 | cerr << "]" << endl; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 984 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 985 | } |