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" |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 8 | #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h" |
| 9 | #include "llvm/CodeGen/PhyRegAlloc.h" |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 10 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 11 | #include <iostream> |
| 12 | using std::cerr; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 13 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 14 | //--------------------------------------------------------------------------- |
| 15 | // Purpose: |
| 16 | // This file contains implementation of Sparc specific helper methods |
| 17 | // used for register allocation. |
| 18 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 22 | // Finds the return value of a sparc specific call instruction |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 23 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 24 | const Value * |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 25 | UltraSparcRegInfo::getCallInstRetVal(const MachineInstr *CallMI) const { |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 26 | unsigned OpCode = CallMI->getOpCode(); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 27 | unsigned NumOfImpRefs = CallMI->getNumImplicitRefs(); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 29 | if (OpCode == CALL) { |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 30 | |
| 31 | // The one before the last implicit operand is the return value of |
| 32 | // a CALL instr |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 33 | // |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 34 | if( NumOfImpRefs > 1 ) |
| 35 | if( CallMI->implicitRefIsDefined(NumOfImpRefs-2) ) |
| 36 | return CallMI->getImplicitRef(NumOfImpRefs-2); |
| 37 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 38 | } else if (OpCode == JMPLCALL) { |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 39 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 40 | // The last implicit operand is the return value of a JMPL |
| 41 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 42 | if(NumOfImpRefs > 0) |
| 43 | if (CallMI->implicitRefIsDefined(NumOfImpRefs-1)) |
| 44 | return CallMI->getImplicitRef(NumOfImpRefs-1); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 45 | } |
| 46 | else |
| 47 | assert(0 && "OpCode must be CALL/JMPL for a call instr"); |
| 48 | |
| 49 | return NULL; |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 52 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 53 | |
| 54 | //--------------------------------------------------------------------------- |
| 55 | // Finds the return address of a call sparc specific call instruction |
| 56 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 57 | const Value * |
| 58 | UltraSparcRegInfo::getCallInstRetAddr(const MachineInstr *CallMI)const { |
| 59 | |
| 60 | unsigned OpCode = CallMI->getOpCode(); |
| 61 | |
| 62 | if( OpCode == CALL) { |
| 63 | |
| 64 | unsigned NumOfImpRefs = CallMI->getNumImplicitRefs(); |
| 65 | |
| 66 | assert( NumOfImpRefs && "CALL instr must have at least on ImpRef"); |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 67 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 68 | // The last implicit operand is the return address of a CALL instr |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 69 | // |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 70 | return CallMI->getImplicitRef(NumOfImpRefs-1); |
| 71 | |
| 72 | } |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 73 | else if( OpCode == JMPLCALL ) { |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 74 | |
| 75 | MachineOperand & MO = ( MachineOperand &) CallMI->getOperand(2); |
| 76 | return MO.getVRegValue(); |
| 77 | |
| 78 | } |
| 79 | else |
| 80 | assert(0 && "OpCode must be CALL/JMPL for a call instr"); |
| 81 | |
| 82 | assert(0 && "There must be a return addr for a call instr"); |
| 83 | |
| 84 | return NULL; |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 88 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 89 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 90 | // Finds the # of actual arguments of the call instruction |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 91 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 92 | const unsigned |
| 93 | UltraSparcRegInfo::getCallInstNumArgs(const MachineInstr *CallMI) const { |
| 94 | |
| 95 | unsigned OpCode = CallMI->getOpCode(); |
| 96 | unsigned NumOfImpRefs = CallMI->getNumImplicitRefs(); |
| 97 | int NumArgs = -1; |
| 98 | |
| 99 | if( OpCode == CALL ) { |
| 100 | |
| 101 | switch( NumOfImpRefs ) { |
| 102 | |
| 103 | case 0: assert(0 && "A CALL inst must have at least one ImpRef (RetAddr)"); |
| 104 | |
| 105 | case 1: NumArgs = 0; |
| 106 | break; |
| 107 | |
| 108 | default: // two or more implicit refs |
| 109 | if( CallMI->implicitRefIsDefined(NumOfImpRefs-2) ) |
| 110 | NumArgs = NumOfImpRefs - 2; // i.e., NumOfImpRef-2 is the ret val |
| 111 | else |
| 112 | NumArgs = NumOfImpRefs - 1; |
| 113 | } |
| 114 | |
| 115 | } |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 116 | else if( OpCode == JMPLCALL ) { |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 117 | |
| 118 | // The last implicit operand is the return value of a JMPL instr |
| 119 | if( NumOfImpRefs > 0 ) { |
| 120 | if( CallMI->implicitRefIsDefined(NumOfImpRefs-1) ) |
| 121 | NumArgs = NumOfImpRefs - 1; // i.e., NumOfImpRef-1 is the ret val |
| 122 | else |
| 123 | NumArgs = NumOfImpRefs; |
| 124 | } |
| 125 | else |
| 126 | NumArgs = NumOfImpRefs; |
| 127 | } |
| 128 | else |
| 129 | assert(0 && "OpCode must be CALL/JMPL for a call instr"); |
| 130 | |
| 131 | assert( (NumArgs != -1) && "Internal error in getCallInstNumArgs" ); |
| 132 | return (unsigned) NumArgs; |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 136 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 137 | //--------------------------------------------------------------------------- |
| 138 | // Finds whether a call is an indirect call |
| 139 | //--------------------------------------------------------------------------- |
| 140 | bool UltraSparcRegInfo::isVarArgCall(const MachineInstr *CallMI) const { |
| 141 | |
| 142 | assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) ); |
| 143 | |
| 144 | const MachineOperand & calleeOp = CallMI->getOperand(0); |
| 145 | Value *calleeVal = calleeOp.getVRegValue(); |
| 146 | |
| 147 | PointerType *PT = cast<PointerType> (calleeVal->getType()); |
| 148 | MethodType *MT = cast<MethodType>(PT->getElementType()); |
| 149 | |
| 150 | return MT->isVarArg(); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | //--------------------------------------------------------------------------- |
| 157 | // Suggests a register for the ret address in the RET machine instruction. |
| 158 | // We always suggest %i7 by convention. |
| 159 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 160 | void UltraSparcRegInfo::suggestReg4RetAddr(const MachineInstr * RetMI, |
| 161 | LiveRangeInfo& LRI) const { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 162 | |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 163 | assert( (RetMI->getNumOperands() >= 2) |
| 164 | && "JMPL/RETURN must have 3 and 2 operands respectively"); |
| 165 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 166 | MachineOperand & MO = ( MachineOperand &) RetMI->getOperand(0); |
| 167 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 168 | // return address is always mapped to i7 |
| 169 | // |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 170 | MO.setRegForValue( getUnifiedRegNum( IntRegClassID, SparcIntRegOrder::i7) ); |
Vikram S. Adve | 53fec86 | 2001-10-22 13:41:12 +0000 | [diff] [blame] | 171 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 172 | // Possible Optimization: |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 173 | // Instead of setting the color, we can suggest one. In that case, |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 174 | // we have to test later whether it received the suggested color. |
| 175 | // In that case, a LR has to be created at the start of method. |
| 176 | // It has to be done as follows (remove the setRegVal above): |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 177 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 178 | // const Value *RetAddrVal = MO.getVRegValue(); |
| 179 | // assert( RetAddrVal && "LR for ret address must be created at start"); |
| 180 | // LiveRange * RetAddrLR = LRI.getLiveRangeForValue( RetAddrVal); |
| 181 | // RetAddrLR->setSuggestedColor(getUnifiedRegNum( IntRegClassID, |
| 182 | // SparcIntRegOrdr::i7) ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | |
| 186 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 187 | // Suggests a register for the ret address in the JMPL/CALL machine instr. |
| 188 | // Sparc ABI dictates that %o7 be used for this purpose. |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 189 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 190 | void UltraSparcRegInfo::suggestReg4CallAddr(const MachineInstr * CallMI, |
| 191 | LiveRangeInfo& LRI, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 192 | std::vector<RegClass *> RCList) const { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 193 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 194 | |
| 195 | const Value *RetAddrVal = getCallInstRetAddr( CallMI ); |
| 196 | |
| 197 | // RetAddrVal cannot be NULL (asserted in getCallInstRetAddr) |
| 198 | // create a new LR for the return address and color it |
| 199 | |
| 200 | LiveRange * RetAddrLR = new LiveRange(); |
| 201 | RetAddrLR->add( RetAddrVal ); |
| 202 | unsigned RegClassID = getRegClassIDOfValue( RetAddrVal ); |
| 203 | RetAddrLR->setRegClass( RCList[RegClassID] ); |
| 204 | RetAddrLR->setColor(getUnifiedRegNum(IntRegClassID,SparcIntRegOrder::o7)); |
| 205 | LRI.addLRToMap( RetAddrVal, RetAddrLR); |
| 206 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 209 | |
| 210 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 211 | |
| 212 | //--------------------------------------------------------------------------- |
| 213 | // This method will suggest colors to incoming args to a method. |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 214 | // According to the Sparc ABI, the first 6 incoming args are in |
| 215 | // %i0 - %i5 (if they are integer) OR in %f0 - %f31 (if they are float). |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 216 | // If the arg is passed on stack due to the lack of regs, NOTHING will be |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 217 | // done - it will be colored (or spilled) as a normal live range. |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 218 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 219 | void UltraSparcRegInfo::suggestRegs4MethodArgs(const Method *const Meth, |
| 220 | LiveRangeInfo& LRI) const |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 221 | { |
| 222 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 223 | // get the argument list |
| 224 | const Method::ArgumentListType& ArgList = Meth->getArgumentList(); |
| 225 | // get an iterator to arg list |
| 226 | Method::ArgumentListType::const_iterator ArgIt = ArgList.begin(); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 227 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 228 | // for each argument |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 229 | for( unsigned argNo=0; ArgIt != ArgList.end() ; ++ArgIt, ++argNo) { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 230 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 231 | // get the LR of arg |
| 232 | LiveRange *const LR = LRI.getLiveRangeForValue((const Value *) *ArgIt); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 233 | assert( LR && "No live range found for method arg"); |
| 234 | |
| 235 | unsigned RegType = getRegType( LR ); |
| 236 | |
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 | // if the arg is in int class - allocate a reg for an int arg |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 239 | // |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 240 | if( RegType == IntRegType ) { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 241 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 242 | if( argNo < NumOfIntArgRegs) { |
| 243 | LR->setSuggestedColor( SparcIntRegOrder::i0 + argNo ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 244 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 245 | else { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 246 | // Do NOTHING as this will be colored as a normal value. |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 247 | if (DEBUG_RA) cerr << " Int Regr not suggested for method arg\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 248 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 250 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 251 | else if( RegType==FPSingleRegType && (argNo*2+1) < NumOfFloatArgRegs) |
| 252 | LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2 + 1) ); |
| 253 | |
| 254 | |
| 255 | else if( RegType == FPDoubleRegType && (argNo*2) < NumOfFloatArgRegs) |
| 256 | LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2) ); |
| 257 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 258 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 261 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 262 | |
| 263 | //--------------------------------------------------------------------------- |
| 264 | // This method is called after graph coloring to move incoming args to |
| 265 | // the correct hardware registers if they did not receive the correct |
| 266 | // (suggested) color through graph coloring. |
| 267 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 268 | void UltraSparcRegInfo::colorMethodArgs(const Method *const Meth, |
| 269 | LiveRangeInfo& LRI, |
| 270 | AddedInstrns *const FirstAI) const { |
| 271 | |
| 272 | // get the argument list |
| 273 | const Method::ArgumentListType& ArgList = Meth->getArgumentList(); |
| 274 | // get an iterator to arg list |
| 275 | Method::ArgumentListType::const_iterator ArgIt = ArgList.begin(); |
| 276 | |
| 277 | MachineInstr *AdMI; |
| 278 | |
| 279 | |
| 280 | // for each argument |
| 281 | for( unsigned argNo=0; ArgIt != ArgList.end() ; ++ArgIt, ++argNo) { |
| 282 | |
| 283 | // get the LR of arg |
| 284 | LiveRange *const LR = LRI.getLiveRangeForValue((const Value *) *ArgIt); |
| 285 | assert( LR && "No live range found for method arg"); |
| 286 | |
| 287 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 288 | unsigned RegType = getRegType( LR ); |
| 289 | unsigned RegClassID = (LR->getRegClass())->getID(); |
| 290 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 291 | // Find whether this argument is coming in a register (if not, on stack) |
| 292 | // Also find the correct register that the argument must go (UniArgReg) |
| 293 | // |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 294 | bool isArgInReg = false; |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 295 | unsigned UniArgReg = InvalidRegNum; // reg that LR MUST be colored with |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 296 | |
| 297 | if( (RegType== IntRegType && argNo < NumOfIntArgRegs)) { |
| 298 | isArgInReg = true; |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 299 | UniArgReg = getUnifiedRegNum( RegClassID, SparcIntRegOrder::i0 + argNo ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 300 | } |
| 301 | else if(RegType == FPSingleRegType && argNo < NumOfFloatArgRegs) { |
| 302 | isArgInReg = true; |
| 303 | UniArgReg = getUnifiedRegNum( RegClassID, |
| 304 | SparcFloatRegOrder::f0 + argNo*2 + 1 ) ; |
| 305 | } |
| 306 | else if(RegType == FPDoubleRegType && argNo < NumOfFloatArgRegs) { |
| 307 | isArgInReg = true; |
| 308 | UniArgReg = getUnifiedRegNum(RegClassID, SparcFloatRegOrder::f0+argNo*2); |
| 309 | } |
| 310 | |
| 311 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 312 | if( LR->hasColor() ) { // if this arg received a register |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 313 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 314 | unsigned UniLRReg = getUnifiedRegNum( RegClassID, LR->getColor() ); |
| 315 | |
| 316 | // if LR received the correct color, nothing to do |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 317 | // |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 318 | if( UniLRReg == UniArgReg ) |
| 319 | continue; |
| 320 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 321 | // We are here because the LR did not receive the suggested |
| 322 | // but LR received another register. |
| 323 | // Now we have to copy the %i reg (or stack pos of arg) |
| 324 | // to the register the LR was colored with. |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 325 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 326 | // if the arg is coming in UniArgReg register, it MUST go into |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 327 | // the UniLRReg register |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 328 | // |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 329 | if( isArgInReg ) |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 330 | AdMI = cpReg2RegMI( UniArgReg, UniLRReg, RegType ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 331 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 332 | else { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 333 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 334 | // Now the arg is coming on stack. Since the LR recieved a register, |
| 335 | // we just have to load the arg on stack into that register |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 336 | // |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 337 | const MachineFrameInfo& frameInfo = target.getFrameInfo(); |
| 338 | assert(frameInfo.argsOnStackHaveFixedSize()); |
| 339 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 340 | bool growUp; // find the offset of arg in stack frame |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 341 | int firstArg = |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 342 | frameInfo.getFirstIncomingArgOffset(MachineCodeForMethod::get(Meth), |
| 343 | growUp); |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 344 | int offsetFromFP = |
| 345 | growUp? firstArg + argNo * frameInfo.getSizeOfEachArgOnStack() |
| 346 | : firstArg - argNo * frameInfo.getSizeOfEachArgOnStack(); |
| 347 | |
| 348 | AdMI = cpMem2RegMI(getFramePointer(), offsetFromFP, |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 349 | UniLRReg, RegType ); |
| 350 | } |
| 351 | |
| 352 | FirstAI->InstrnsBefore.push_back( AdMI ); |
| 353 | |
| 354 | } // if LR received a color |
| 355 | |
| 356 | else { |
| 357 | |
| 358 | // Now, the LR did not receive a color. But it has a stack offset for |
| 359 | // spilling. |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 360 | // So, if the arg is coming in UniArgReg register, we can just move |
| 361 | // that on to the stack pos of LR |
| 362 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 363 | if( isArgInReg ) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 364 | cpReg2MemMI(UniArgReg, getFramePointer(), |
| 365 | LR->getSpillOffFromFP(), RegType ); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 366 | |
| 367 | FirstAI->InstrnsBefore.push_back( AdMI ); |
| 368 | } |
| 369 | |
| 370 | else { |
| 371 | |
| 372 | // Now the arg is coming on stack. Since the LR did NOT |
| 373 | // recieved a register as well, it is allocated a stack position. We |
| 374 | // can simply change the stack poistion of the LR. We can do this, |
| 375 | // since this method is called before any other method that makes |
| 376 | // uses of the stack pos of the LR (e.g., updateMachineInstr) |
| 377 | |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 378 | const MachineFrameInfo& frameInfo = target.getFrameInfo(); |
| 379 | assert(frameInfo.argsOnStackHaveFixedSize()); |
| 380 | |
| 381 | bool growUp; |
| 382 | int firstArg = frameInfo.getFirstIncomingArgOffset(MachineCodeForMethod::get(Meth), growUp); |
| 383 | int offsetFromFP = |
| 384 | growUp? firstArg + argNo * frameInfo.getSizeOfEachArgOnStack() |
| 385 | : firstArg - argNo * frameInfo.getSizeOfEachArgOnStack(); |
| 386 | |
| 387 | LR->modifySpillOffFromFP( offsetFromFP ); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 388 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 389 | |
| 390 | } |
| 391 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 392 | } // for each incoming argument |
| 393 | |
| 394 | } |
| 395 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 396 | |
| 397 | |
| 398 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 399 | //--------------------------------------------------------------------------- |
| 400 | // This method is called before graph coloring to suggest colors to the |
| 401 | // outgoing call args and the return value of the call. |
| 402 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 403 | void UltraSparcRegInfo::suggestRegs4CallArgs(const MachineInstr *const CallMI, |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 404 | LiveRangeInfo& LRI, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 405 | std::vector<RegClass *> RCList) const { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 406 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 407 | assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) ); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 408 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 409 | suggestReg4CallAddr(CallMI, LRI, RCList); |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 411 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 412 | // First color the return value of the call instruction. The return value |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 413 | // will be in %o0 if the value is an integer type, or in %f0 if the |
| 414 | // value is a float type. |
| 415 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 416 | // the return value cannot have a LR in machine instruction since it is |
| 417 | // only defined by the call instruction |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 418 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 419 | // if type is not void, create a new live range and set its |
| 420 | // register class and add to LRI |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 421 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 422 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 423 | const Value *RetVal = getCallInstRetVal( CallMI ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 424 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 425 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 426 | if( RetVal ) { |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 427 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 428 | assert( (! LRI.getLiveRangeForValue( RetVal ) ) && |
| 429 | "LR for ret Value of call already definded!"); |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 430 | |
| 431 | |
| 432 | // create a new LR for the return value |
| 433 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 434 | LiveRange * RetValLR = new LiveRange(); |
| 435 | RetValLR->add( RetVal ); |
| 436 | unsigned RegClassID = getRegClassIDOfValue( RetVal ); |
| 437 | RetValLR->setRegClass( RCList[RegClassID] ); |
| 438 | LRI.addLRToMap( RetVal, RetValLR); |
| 439 | |
| 440 | // now suggest a register depending on the register class of ret arg |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 441 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 442 | if( RegClassID == IntRegClassID ) |
| 443 | RetValLR->setSuggestedColor(SparcIntRegOrder::o0); |
| 444 | else if (RegClassID == FloatRegClassID ) |
| 445 | RetValLR->setSuggestedColor(SparcFloatRegOrder::f0 ); |
| 446 | else assert( 0 && "Unknown reg class for return value of call\n"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 448 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 449 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 450 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 451 | // Now suggest colors for arguments (operands) of the call instruction. |
| 452 | // Colors are suggested only if the arg number is smaller than the |
| 453 | // the number of registers allocated for argument passing. |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 454 | // Now, go thru call args - implicit operands of the call MI |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 455 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 456 | unsigned NumOfCallArgs = getCallInstNumArgs( CallMI ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 457 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 458 | for(unsigned argNo=0, i=0; i < NumOfCallArgs; ++i, ++argNo ) { |
| 459 | |
| 460 | const Value *CallArg = CallMI->getImplicitRef(i); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 461 | |
| 462 | // get the LR of call operand (parameter) |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 463 | LiveRange *const LR = LRI.getLiveRangeForValue(CallArg); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 464 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 465 | // not possible to have a null LR since all args (even consts) |
| 466 | // must be defined before |
| 467 | if( !LR ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 468 | if( DEBUG_RA) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 469 | cerr << " ERROR: In call instr, no LR for arg: " ; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 470 | printValue(CallArg); cerr << "\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 471 | } |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 472 | assert(0 && "NO LR for call arg"); |
| 473 | // continue; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | unsigned RegType = getRegType( LR ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 477 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 478 | // if the arg is in int class - allocate a reg for an int arg |
| 479 | if( RegType == IntRegType ) { |
| 480 | |
| 481 | if( argNo < NumOfIntArgRegs) |
| 482 | LR->setSuggestedColor( SparcIntRegOrder::o0 + argNo ); |
| 483 | |
| 484 | else if (DEBUG_RA) |
| 485 | // Do NOTHING as this will be colored as a normal value. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 486 | cerr << " Regr not suggested for int call arg\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 487 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 488 | } |
| 489 | else if( RegType == FPSingleRegType && (argNo*2 +1)< NumOfFloatArgRegs) |
| 490 | LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2 + 1) ); |
| 491 | |
| 492 | |
| 493 | else if( RegType == FPDoubleRegType && (argNo*2) < NumOfFloatArgRegs) |
| 494 | LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2) ); |
| 495 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 496 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 497 | } // for all call arguments |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 502 | //--------------------------------------------------------------------------- |
| 503 | // After graph coloring, we have call this method to see whehter the return |
| 504 | // value and the call args received the correct colors. If not, we have |
| 505 | // to instert copy instructions. |
| 506 | //--------------------------------------------------------------------------- |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 507 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 508 | void UltraSparcRegInfo::colorCallArgs(const MachineInstr *const CallMI, |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 509 | LiveRangeInfo& LRI, |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 510 | AddedInstrns *const CallAI, |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 511 | PhyRegAlloc &PRA, |
| 512 | const BasicBlock *BB) const { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 513 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 514 | assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) ); |
| 515 | |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 516 | // Reset the optional args area in the stack frame |
| 517 | // since that is reused for each call |
| 518 | // |
| 519 | PRA.mcInfo.resetOptionalArgs(target); |
| 520 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 521 | // First color the return value of the call. |
| 522 | // If there is a LR for the return value, it means this |
| 523 | // method returns a value |
| 524 | |
| 525 | MachineInstr *AdMI; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 526 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 527 | const Value *RetVal = getCallInstRetVal( CallMI ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 528 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 529 | if( RetVal ) { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 530 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 531 | LiveRange * RetValLR = LRI.getLiveRangeForValue( RetVal ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 532 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 533 | if( !RetValLR ) { |
| 534 | cerr << "\nNo LR for:"; |
| 535 | printValue( RetVal ); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 536 | cerr << "\n"; |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 537 | assert( RetValLR && "ERR:No LR for non-void return value"); |
| 538 | //return; |
| 539 | } |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 540 | |
| 541 | unsigned RegClassID = (RetValLR->getRegClass())->getID(); |
| 542 | bool recvCorrectColor = false; |
| 543 | |
| 544 | unsigned CorrectCol; // correct color for ret value |
| 545 | if(RegClassID == IntRegClassID) |
| 546 | CorrectCol = SparcIntRegOrder::o0; |
| 547 | else if(RegClassID == FloatRegClassID) |
| 548 | CorrectCol = SparcFloatRegOrder::f0; |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 549 | else { |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 550 | assert( 0 && "Unknown RegClass"); |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 551 | return; |
| 552 | } |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 553 | |
| 554 | // if the LR received the correct color, NOTHING to do |
| 555 | |
| 556 | if( RetValLR->hasColor() ) |
| 557 | if( RetValLR->getColor() == CorrectCol ) |
| 558 | recvCorrectColor = true; |
| 559 | |
| 560 | |
| 561 | // if we didn't receive the correct color for some reason, |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 562 | // put copy instruction |
| 563 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 564 | if( !recvCorrectColor ) { |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 565 | |
| 566 | unsigned RegType = getRegType( RetValLR ); |
| 567 | |
| 568 | // the reg that LR must be colored with |
| 569 | unsigned UniRetReg = getUnifiedRegNum( RegClassID, CorrectCol); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 570 | |
| 571 | if( RetValLR->hasColor() ) { |
| 572 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 573 | unsigned |
| 574 | UniRetLRReg=getUnifiedRegNum(RegClassID,RetValLR->getColor()); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 575 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 576 | // the return value is coming in UniRetReg but has to go into |
| 577 | // the UniRetLRReg |
| 578 | |
| 579 | AdMI = cpReg2RegMI( UniRetReg, UniRetLRReg, RegType ); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 580 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 581 | } // if LR has color |
| 582 | else { |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 583 | |
| 584 | // if the LR did NOT receive a color, we have to move the return |
| 585 | // value coming in UniRetReg to the stack pos of spilled LR |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 586 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 587 | AdMI = cpReg2MemMI(UniRetReg, getFramePointer(), |
| 588 | RetValLR->getSpillOffFromFP(), RegType ); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 589 | } |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 590 | |
| 591 | CallAI->InstrnsAfter.push_back( AdMI ); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 592 | |
| 593 | } // the LR didn't receive the suggested color |
| 594 | |
| 595 | } // if there a return value |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 596 | |
| 597 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 598 | //------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 599 | // Now color all args of the call instruction |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 600 | //------------------------------------------- |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 601 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 602 | std::vector<MachineInstr *> AddedInstrnsBefore; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 603 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 604 | unsigned NumOfCallArgs = getCallInstNumArgs( CallMI ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 605 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 606 | bool VarArgCall = isVarArgCall( CallMI ); |
| 607 | |
| 608 | if(VarArgCall) cerr << "\nVar arg call found!!\n"; |
| 609 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 610 | for(unsigned argNo=0, i=0; i < NumOfCallArgs; ++i, ++argNo ) { |
| 611 | |
| 612 | const Value *CallArg = CallMI->getImplicitRef(i); |
| 613 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 614 | // get the LR of call operand (parameter) |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 615 | LiveRange *const LR = LRI.getLiveRangeForValue(CallArg); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 616 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 617 | unsigned RegType = getRegType( CallArg ); |
| 618 | unsigned RegClassID = getRegClassIDOfValue( CallArg); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 619 | |
| 620 | // find whether this argument is coming in a register (if not, on stack) |
| 621 | |
| 622 | bool isArgInReg = false; |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 623 | unsigned UniArgReg = InvalidRegNum; // reg that LR must be colored with |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 624 | |
| 625 | if( (RegType== IntRegType && argNo < NumOfIntArgRegs)) { |
| 626 | isArgInReg = true; |
| 627 | UniArgReg = getUnifiedRegNum(RegClassID, SparcIntRegOrder::o0 + argNo ); |
| 628 | } |
| 629 | else if(RegType == FPSingleRegType && argNo < NumOfFloatArgRegs) { |
| 630 | isArgInReg = true; |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 631 | |
| 632 | if( !VarArgCall ) |
| 633 | UniArgReg = getUnifiedRegNum(RegClassID, |
| 634 | SparcFloatRegOrder::f0 + (argNo*2 + 1) ); |
| 635 | else { |
| 636 | // a variable argument call - must pass float arg in %o's |
| 637 | if( argNo < NumOfIntArgRegs) |
| 638 | UniArgReg=getUnifiedRegNum(IntRegClassID,SparcIntRegOrder::o0+argNo); |
| 639 | else |
| 640 | isArgInReg = false; |
| 641 | } |
| 642 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 643 | } |
| 644 | else if(RegType == FPDoubleRegType && argNo < NumOfFloatArgRegs) { |
| 645 | isArgInReg = true; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 646 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 647 | if( !VarArgCall ) |
| 648 | UniArgReg =getUnifiedRegNum(RegClassID,SparcFloatRegOrder::f0+argNo*2); |
| 649 | else { |
| 650 | // a variable argument call - must pass float arg in %o's |
| 651 | if( argNo < NumOfIntArgRegs) |
| 652 | UniArgReg=getUnifiedRegNum(IntRegClassID,SparcIntRegOrder::o0+argNo); |
| 653 | else |
| 654 | isArgInReg = false; |
| 655 | } |
| 656 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 657 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 658 | // not possible to have a null LR since all args (even consts) |
| 659 | // must be defined before |
| 660 | if( !LR ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 661 | if( DEBUG_RA) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 662 | cerr << " ERROR: In call instr, no LR for arg: " ; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 663 | printValue(CallArg); cerr << "\n"; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 664 | } |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 665 | assert(0 && "NO LR for call arg"); |
| 666 | // continue; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 667 | } |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 668 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 669 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 670 | if( LR->hasColor() ) { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 671 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 672 | |
| 673 | unsigned UniLRReg = getUnifiedRegNum( RegClassID, LR->getColor() ); |
| 674 | |
| 675 | // if LR received the correct color, nothing to do |
| 676 | if( UniLRReg == UniArgReg ) |
| 677 | continue; |
| 678 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 679 | // We are here because though the LR is allocated a register, it |
| 680 | // was not allocated the suggested register. So, we have to copy %ix reg |
| 681 | // (or stack pos of arg) to the register it was colored with |
| 682 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 683 | // the LR is colored with UniLRReg but has to go into UniArgReg |
| 684 | // to pass it as an argument |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 685 | |
Ruchira Sasanka | 9d47866 | 2001-11-12 20:54:19 +0000 | [diff] [blame] | 686 | if( isArgInReg ) { |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 687 | |
| 688 | if( VarArgCall && RegClassID == FloatRegClassID ) { |
| 689 | |
| 690 | |
| 691 | // for a variable argument call, the float reg must go in a %o reg. |
| 692 | // We have to move a float reg to an int reg via memory. |
| 693 | // The store instruction will be directly added to |
| 694 | // CallAI->InstrnsBefore since it does not need reordering |
| 695 | // |
| 696 | int TmpOff = PRA.mcInfo.pushTempValue(target, |
| 697 | getSpilledRegSize(RegType)); |
| 698 | |
| 699 | AdMI = cpReg2MemMI(UniLRReg, getFramePointer(), TmpOff, RegType ); |
| 700 | CallAI->InstrnsBefore.push_back( AdMI ); |
| 701 | |
| 702 | AdMI = cpMem2RegMI(getFramePointer(), TmpOff, UniArgReg, IntRegType); |
| 703 | AddedInstrnsBefore.push_back( AdMI ); |
| 704 | } |
| 705 | |
| 706 | else { |
| 707 | AdMI = cpReg2RegMI(UniLRReg, UniArgReg, RegType ); |
| 708 | AddedInstrnsBefore.push_back( AdMI ); |
| 709 | } |
| 710 | |
Ruchira Sasanka | 9d47866 | 2001-11-12 20:54:19 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 713 | else { |
| 714 | // Now, we have to pass the arg on stack. Since LR received a register |
| 715 | // we just have to move that register to the stack position where |
| 716 | // the argument must be passed |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 717 | |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 718 | int argOffset = PRA.mcInfo.allocateOptionalArg(target, LR->getType()); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 719 | |
Ruchira Sasanka | c56e5c1 | 2001-11-11 22:37:51 +0000 | [diff] [blame] | 720 | AdMI = cpReg2MemMI(UniLRReg, getStackPointer(), argOffset, RegType ); |
Ruchira Sasanka | 9d47866 | 2001-11-12 20:54:19 +0000 | [diff] [blame] | 721 | |
| 722 | // Now add the instruction. We can directly add to |
| 723 | // CallAI->InstrnsBefore since we are just saving a reg on stack |
| 724 | // |
| 725 | CallAI->InstrnsBefore.push_back( AdMI ); |
| 726 | |
| 727 | //cerr << "\nCaution: Passing a reg on stack"; |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Ruchira Sasanka | 9d47866 | 2001-11-12 20:54:19 +0000 | [diff] [blame] | 730 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 733 | else { // LR is not colored (i.e., spilled) |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 734 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 735 | if( isArgInReg ) { |
| 736 | |
| 737 | // Now the LR did NOT recieve a register but has a stack poistion. |
| 738 | // Since, the outgoing arg goes in a register we just have to insert |
| 739 | // a load instruction to load the LR to outgoing register |
| 740 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 741 | if( VarArgCall && RegClassID == FloatRegClassID ) |
| 742 | AdMI = cpMem2RegMI(getFramePointer(), LR->getSpillOffFromFP(), |
| 743 | UniArgReg, IntRegType ); |
| 744 | else |
| 745 | AdMI = cpMem2RegMI(getFramePointer(), LR->getSpillOffFromFP(), |
| 746 | UniArgReg, RegType ); |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 747 | |
Ruchira Sasanka | 91014f6 | 2001-11-12 20:31:47 +0000 | [diff] [blame] | 748 | cerr << "\nCaution: Loading a spilled val to a reg as a call arg"; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 749 | AddedInstrnsBefore.push_back( AdMI ); // Now add the instruction |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 750 | } |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 751 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 752 | else { |
| 753 | // Now, we have to pass the arg on stack. Since LR also did NOT |
| 754 | // receive a register we have to move an argument in memory to |
| 755 | // outgoing parameter on stack. |
| 756 | |
| 757 | // Optoimize: Optimize when reverse pointers in MahineInstr are |
| 758 | // introduced. |
| 759 | // call PRA.getUnusedRegAtMI(....) to get an unused reg. Only if this |
| 760 | // fails, then use the following code. Currently, we cannot call the |
| 761 | // above method since we cannot find LVSetBefore without the BB |
| 762 | |
Ruchira Sasanka | 295264d | 2001-11-15 20:25:07 +0000 | [diff] [blame] | 763 | int TReg = PRA.getUniRegNotUsedByThisInst( LR->getRegClass(), CallMI ); |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 764 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 765 | int TmpOff = PRA.mcInfo.pushTempValue(target, |
| 766 | getSpilledRegSize(getRegType(LR)) ); |
| 767 | |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 768 | |
| 769 | int argOffset = PRA.mcInfo.allocateOptionalArg(target, LR->getType()); |
| 770 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 771 | MachineInstr *Ad1, *Ad2, *Ad3, *Ad4; |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 772 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 773 | // Sequence: |
| 774 | // (1) Save TReg on stack |
| 775 | // (2) Load LR value into TReg from stack pos of LR |
| 776 | // (3) Store Treg on outgoing Arg pos on stack |
| 777 | // (4) Load the old value of TReg from stack to TReg (restore it) |
| 778 | |
| 779 | Ad1 = cpReg2MemMI(TReg, getFramePointer(), TmpOff, RegType ); |
| 780 | Ad2 = cpMem2RegMI(getFramePointer(), LR->getSpillOffFromFP(), |
| 781 | TReg, RegType ); |
Ruchira Sasanka | c56e5c1 | 2001-11-11 22:37:51 +0000 | [diff] [blame] | 782 | Ad3 = cpReg2MemMI(TReg, getStackPointer(), argOffset, RegType ); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 783 | Ad4 = cpMem2RegMI(getFramePointer(), TmpOff, TReg, RegType ); |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 784 | |
| 785 | // We directly add to CallAI->InstrnsBefore instead of adding to |
| 786 | // AddedInstrnsBefore since these instructions must not be |
| 787 | // reordered. |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 788 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 789 | CallAI->InstrnsBefore.push_back( Ad1 ); |
| 790 | CallAI->InstrnsBefore.push_back( Ad2 ); |
| 791 | CallAI->InstrnsBefore.push_back( Ad3 ); |
| 792 | CallAI->InstrnsBefore.push_back( Ad4 ); |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 793 | |
Ruchira Sasanka | 91014f6 | 2001-11-12 20:31:47 +0000 | [diff] [blame] | 794 | cerr << "\nCaution: Call arg moved from stack2stack for: " << *CallMI ; |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | } // for each parameter in call instruction |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 800 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 801 | |
| 802 | // if we added any instruction before the call instruction, verify |
| 803 | // that they are in the proper order and if not, reorder them |
| 804 | |
| 805 | if( ! AddedInstrnsBefore.empty() ) { |
| 806 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 807 | if( DEBUG_RA ) { |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 808 | cerr << "\nCalling reorder with instrns: \n"; |
| 809 | for(unsigned i=0; i < AddedInstrnsBefore.size(); i++) |
| 810 | cerr << *(AddedInstrnsBefore[i]); |
| 811 | } |
| 812 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 813 | std::vector<MachineInstr *> TmpVec; |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 814 | OrderAddedInstrns(AddedInstrnsBefore, TmpVec, PRA); |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 815 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 816 | if( DEBUG_RA ) { |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 817 | cerr << "\nAfter reordering instrns: \n"; |
| 818 | for(unsigned i=0; i < TmpVec.size(); i++) |
| 819 | cerr << *(TmpVec[i]); |
| 820 | } |
| 821 | |
| 822 | // copy the results back from TmpVec to InstrnsBefore |
| 823 | for(unsigned i=0; i < TmpVec.size(); i++) |
| 824 | CallAI->InstrnsBefore.push_back( TmpVec[i] ); |
| 825 | } |
| 826 | |
| 827 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 828 | // now insert caller saving code for this call instruction |
| 829 | // |
| 830 | insertCallerSavingCode(CallMI, BB, PRA); |
| 831 | |
| 832 | |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 833 | // Reset optional args area again to be safe |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 834 | PRA.mcInfo.resetOptionalArgs(target); |
| 835 | |
| 836 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 839 | //--------------------------------------------------------------------------- |
| 840 | // This method is called for an LLVM return instruction to identify which |
| 841 | // values will be returned from this method and to suggest colors. |
| 842 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 843 | void UltraSparcRegInfo::suggestReg4RetValue(const MachineInstr *const RetMI, |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 844 | LiveRangeInfo& LRI) const { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 845 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 846 | assert( (UltraSparcInfo->getInstrInfo()).isReturn( RetMI->getOpCode() ) ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 847 | |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 848 | suggestReg4RetAddr(RetMI, LRI); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 849 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 850 | // if there is an implicit ref, that has to be the ret value |
| 851 | if( RetMI->getNumImplicitRefs() > 0 ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 852 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 853 | // The first implicit operand is the return value of a return instr |
| 854 | const Value *RetVal = RetMI->getImplicitRef(0); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 855 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 856 | LiveRange *const LR = LRI.getLiveRangeForValue( RetVal ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 857 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 858 | if( !LR ) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 859 | cerr << "\nNo LR for:"; |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 860 | printValue( RetVal ); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 861 | cerr << "\n"; |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 862 | assert( LR && "No LR for return value of non-void method"); |
| 863 | //return; |
| 864 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 865 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 866 | unsigned RegClassID = (LR->getRegClass())->getID(); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 867 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 868 | if( RegClassID == IntRegClassID ) |
| 869 | LR->setSuggestedColor(SparcIntRegOrder::i0); |
| 870 | |
| 871 | else if ( RegClassID == FloatRegClassID ) |
| 872 | LR->setSuggestedColor(SparcFloatRegOrder::f0); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 873 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 874 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 875 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 878 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 879 | |
| 880 | //--------------------------------------------------------------------------- |
| 881 | // Colors the return value of a method to %i0 or %f0, if possible. If it is |
| 882 | // not possilbe to directly color the LR, insert a copy instruction to move |
| 883 | // the LR to %i0 or %f0. When the LR is spilled, instead of the copy, we |
| 884 | // have to put a load instruction. |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 885 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 886 | void UltraSparcRegInfo::colorRetValue(const MachineInstr *const RetMI, |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 887 | LiveRangeInfo& LRI, |
| 888 | AddedInstrns *const RetAI) const { |
| 889 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 890 | assert( (UltraSparcInfo->getInstrInfo()).isReturn( RetMI->getOpCode() ) ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 891 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 892 | // if there is an implicit ref, that has to be the ret value |
| 893 | if( RetMI->getNumImplicitRefs() > 0 ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 894 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 895 | // The first implicit operand is the return value of a return instr |
| 896 | const Value *RetVal = RetMI->getImplicitRef(0); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 897 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 898 | LiveRange *const LR = LRI.getLiveRangeForValue( RetVal ); |
| 899 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 900 | if( ! LR ) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 901 | cerr << "\nNo LR for:"; |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 902 | printValue( RetVal ); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 903 | cerr << "\n"; |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 904 | // assert( LR && "No LR for return value of non-void method"); |
| 905 | return; |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 906 | } |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 907 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 908 | unsigned RegClassID = getRegClassIDOfValue(RetVal); |
| 909 | unsigned RegType = getRegType( RetVal ); |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 910 | |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 911 | unsigned CorrectCol; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 912 | if(RegClassID == IntRegClassID) |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 913 | CorrectCol = SparcIntRegOrder::i0; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 914 | else if(RegClassID == FloatRegClassID) |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 915 | CorrectCol = SparcFloatRegOrder::f0; |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 916 | else { |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 917 | assert( 0 && "Unknown RegClass"); |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 918 | return; |
| 919 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 920 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 921 | // if the LR received the correct color, NOTHING to do |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 922 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 923 | if( LR->hasColor() ) |
| 924 | if( LR->getColor() == CorrectCol ) |
| 925 | return; |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 926 | |
| 927 | unsigned UniRetReg = getUnifiedRegNum( RegClassID, CorrectCol ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 928 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 929 | if( LR->hasColor() ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 930 | |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 931 | // We are here because the LR was allocted a regiter |
| 932 | // It may be the suggested register or not |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 933 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 934 | // copy the LR of retun value to i0 or f0 |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 935 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 936 | unsigned UniLRReg =getUnifiedRegNum( RegClassID, LR->getColor()); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 937 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 938 | // the LR received UniLRReg but must be colored with UniRetReg |
| 939 | // to pass as the return value |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 940 | RetAI->InstrnsBefore.push_back(cpReg2RegMI(UniLRReg, UniRetReg, RegType)); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 941 | } |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 942 | else { // if the LR is spilled |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 943 | MachineInstr *AdMI = cpMem2RegMI(getFramePointer(), |
| 944 | LR->getSpillOffFromFP(), |
| 945 | UniRetReg, RegType); |
| 946 | RetAI->InstrnsBefore.push_back(AdMI); |
| 947 | cerr << "\nCopied the return value from stack\n"; |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 950 | } // if there is a return value |
| 951 | |
| 952 | } |
| 953 | |
| 954 | |
| 955 | //--------------------------------------------------------------------------- |
| 956 | // Copy from a register to register. Register number must be the unified |
| 957 | // register number |
| 958 | //--------------------------------------------------------------------------- |
| 959 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 960 | MachineInstr * UltraSparcRegInfo::cpReg2RegMI(const unsigned SrcReg, |
| 961 | const unsigned DestReg, |
| 962 | const int RegType) const { |
| 963 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 964 | assert( ((int)SrcReg != InvalidRegNum) && ((int)DestReg != InvalidRegNum) && |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 965 | "Invalid Register"); |
| 966 | |
| 967 | MachineInstr * MI = NULL; |
| 968 | |
| 969 | switch( RegType ) { |
| 970 | |
| 971 | case IntRegType: |
Ruchira Sasanka | 735d6e3 | 2001-10-18 22:38:52 +0000 | [diff] [blame] | 972 | case IntCCRegType: |
| 973 | case FloatCCRegType: |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 974 | MI = new MachineInstr(ADD, 3); |
| 975 | MI->SetMachineOperand(0, SrcReg, false); |
| 976 | MI->SetMachineOperand(1, SparcIntRegOrder::g0, false); |
| 977 | MI->SetMachineOperand(2, DestReg, true); |
| 978 | break; |
| 979 | |
| 980 | case FPSingleRegType: |
| 981 | MI = new MachineInstr(FMOVS, 2); |
| 982 | MI->SetMachineOperand(0, SrcReg, false); |
| 983 | MI->SetMachineOperand(1, DestReg, true); |
| 984 | break; |
| 985 | |
| 986 | case FPDoubleRegType: |
| 987 | MI = new MachineInstr(FMOVD, 2); |
| 988 | MI->SetMachineOperand(0, SrcReg, false); |
| 989 | MI->SetMachineOperand(1, DestReg, true); |
| 990 | break; |
| 991 | |
| 992 | default: |
| 993 | assert(0 && "Unknow RegType"); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | return MI; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 997 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 998 | |
| 999 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1000 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 7dcd612 | 2001-10-24 22:05:34 +0000 | [diff] [blame] | 1001 | // Copy from a register to memory (i.e., Store). Register number must |
| 1002 | // be the unified register number |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1003 | //--------------------------------------------------------------------------- |
| 1004 | |
| 1005 | |
| 1006 | MachineInstr * UltraSparcRegInfo::cpReg2MemMI(const unsigned SrcReg, |
| 1007 | const unsigned DestPtrReg, |
| 1008 | const int Offset, |
| 1009 | const int RegType) const { |
| 1010 | |
| 1011 | |
| 1012 | MachineInstr * MI = NULL; |
| 1013 | |
| 1014 | switch( RegType ) { |
| 1015 | |
| 1016 | case IntRegType: |
Ruchira Sasanka | 735d6e3 | 2001-10-18 22:38:52 +0000 | [diff] [blame] | 1017 | case FloatCCRegType: |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1018 | MI = new MachineInstr(STX, 3); |
Ruchira Sasanka | 7dcd612 | 2001-10-24 22:05:34 +0000 | [diff] [blame] | 1019 | MI->SetMachineOperand(0, SrcReg, false); |
| 1020 | MI->SetMachineOperand(1, DestPtrReg, false); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1021 | MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, |
| 1022 | (int64_t) Offset, false); |
| 1023 | break; |
| 1024 | |
| 1025 | case FPSingleRegType: |
| 1026 | MI = new MachineInstr(ST, 3); |
Ruchira Sasanka | 7dcd612 | 2001-10-24 22:05:34 +0000 | [diff] [blame] | 1027 | MI->SetMachineOperand(0, SrcReg, false); |
| 1028 | MI->SetMachineOperand(1, DestPtrReg, false); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1029 | MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, |
| 1030 | (int64_t) Offset, false); |
| 1031 | break; |
| 1032 | |
| 1033 | case FPDoubleRegType: |
| 1034 | MI = new MachineInstr(STD, 3); |
Ruchira Sasanka | 7dcd612 | 2001-10-24 22:05:34 +0000 | [diff] [blame] | 1035 | MI->SetMachineOperand(0, SrcReg, false); |
| 1036 | MI->SetMachineOperand(1, DestPtrReg, false); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1037 | MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, |
| 1038 | (int64_t) Offset, false); |
| 1039 | break; |
| 1040 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1041 | case IntCCRegType: |
| 1042 | assert( 0 && "Cannot directly store %ccr to memory"); |
| 1043 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1044 | default: |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1045 | assert(0 && "Unknow RegType in cpReg2MemMI"); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | return MI; |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 7dcd612 | 2001-10-24 22:05:34 +0000 | [diff] [blame] | 1053 | // Copy from memory to a reg (i.e., Load) Register number must be the unified |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1054 | // register number |
| 1055 | //--------------------------------------------------------------------------- |
| 1056 | |
| 1057 | |
| 1058 | MachineInstr * UltraSparcRegInfo::cpMem2RegMI(const unsigned SrcPtrReg, |
| 1059 | const int Offset, |
| 1060 | const unsigned DestReg, |
| 1061 | const int RegType) const { |
| 1062 | |
| 1063 | MachineInstr * MI = NULL; |
| 1064 | |
| 1065 | switch( RegType ) { |
| 1066 | |
| 1067 | case IntRegType: |
Ruchira Sasanka | 735d6e3 | 2001-10-18 22:38:52 +0000 | [diff] [blame] | 1068 | case FloatCCRegType: |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1069 | MI = new MachineInstr(LDX, 3); |
| 1070 | MI->SetMachineOperand(0, SrcPtrReg, false); |
| 1071 | MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, |
| 1072 | (int64_t) Offset, false); |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1073 | MI->SetMachineOperand(2, DestReg, true); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1074 | break; |
| 1075 | |
| 1076 | case FPSingleRegType: |
| 1077 | MI = new MachineInstr(LD, 3); |
| 1078 | MI->SetMachineOperand(0, SrcPtrReg, false); |
| 1079 | MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, |
| 1080 | (int64_t) Offset, false); |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1081 | MI->SetMachineOperand(2, DestReg, true); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1082 | |
| 1083 | break; |
| 1084 | |
| 1085 | case FPDoubleRegType: |
| 1086 | MI = new MachineInstr(LDD, 3); |
| 1087 | MI->SetMachineOperand(0, SrcPtrReg, false); |
| 1088 | MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, |
| 1089 | (int64_t) Offset, false); |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1090 | MI->SetMachineOperand(2, DestReg, true); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1091 | break; |
| 1092 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1093 | case IntCCRegType: |
| 1094 | assert( 0 && "Cannot directly load into %ccr from memory"); |
| 1095 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1096 | default: |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1097 | assert(0 && "Unknown RegType in cpMem2RegMI"); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | return MI; |
| 1101 | } |
| 1102 | |
| 1103 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1104 | |
| 1105 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1106 | |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1107 | //--------------------------------------------------------------------------- |
| 1108 | // Generate a copy instruction to copy a value to another. Temporarily |
| 1109 | // used by PhiElimination code. |
| 1110 | //--------------------------------------------------------------------------- |
| 1111 | |
| 1112 | |
| 1113 | MachineInstr * UltraSparcRegInfo::cpValue2Value(Value *Src, Value *Dest) const{ |
| 1114 | |
| 1115 | int RegType = getRegType( Src ); |
| 1116 | |
| 1117 | assert( (RegType==getRegType(Src)) && "Src & Dest are diff types"); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1118 | |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 1119 | MachineInstr * MI = NULL; |
| 1120 | |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1121 | switch( RegType ) { |
| 1122 | |
| 1123 | case IntRegType: |
| 1124 | |
| 1125 | MI = new MachineInstr(ADD, 3); |
| 1126 | MI->SetMachineOperand(0, MachineOperand:: MO_VirtualRegister, Src, false); |
| 1127 | MI->SetMachineOperand(1, SparcIntRegOrder::g0, false); |
| 1128 | MI->SetMachineOperand(2, MachineOperand:: MO_VirtualRegister, Dest, true); |
| 1129 | break; |
| 1130 | |
| 1131 | case FPSingleRegType: |
| 1132 | MI = new MachineInstr(FMOVS, 2); |
| 1133 | MI->SetMachineOperand(0, MachineOperand:: MO_VirtualRegister, Src, false); |
| 1134 | MI->SetMachineOperand(1, MachineOperand:: MO_VirtualRegister, Dest, true); |
| 1135 | break; |
| 1136 | |
| 1137 | |
| 1138 | case FPDoubleRegType: |
| 1139 | MI = new MachineInstr(FMOVD, 2); |
| 1140 | MI->SetMachineOperand(0, MachineOperand:: MO_VirtualRegister, Src, false); |
| 1141 | MI->SetMachineOperand(1, MachineOperand:: MO_VirtualRegister, Dest, true); |
| 1142 | break; |
| 1143 | |
| 1144 | default: |
| 1145 | assert(0 && "Unknow RegType in CpValu2Value"); |
| 1146 | } |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 1147 | |
| 1148 | return MI; |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 1149 | } |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1150 | |
| 1151 | |
| 1152 | |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1153 | |
| 1154 | |
| 1155 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1156 | //---------------------------------------------------------------------------- |
| 1157 | // This method inserts caller saving/restoring instructons before/after |
Ruchira Sasanka | bf91552 | 2002-01-07 21:03:42 +0000 | [diff] [blame] | 1158 | // a call machine instruction. The caller saving/restoring instructions are |
| 1159 | // inserted like: |
| 1160 | // |
| 1161 | // ** caller saving instructions |
| 1162 | // other instructions inserted for the call by ColorCallArg |
| 1163 | // CALL instruction |
| 1164 | // other instructions inserted for the call ColorCallArg |
| 1165 | // ** caller restoring instructions |
| 1166 | // |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1167 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1168 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1169 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1170 | void UltraSparcRegInfo::insertCallerSavingCode(const MachineInstr *MInst, |
| 1171 | const BasicBlock *BB, |
| 1172 | PhyRegAlloc &PRA) const { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1173 | |
Ruchira Sasanka | bf91552 | 2002-01-07 21:03:42 +0000 | [diff] [blame] | 1174 | // has set to record which registers were saved/restored |
| 1175 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 1176 | std::hash_set<unsigned> PushedRegSet; |
Ruchira Sasanka | bf91552 | 2002-01-07 21:03:42 +0000 | [diff] [blame] | 1177 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1178 | // Now find the LR of the return value of the call |
| 1179 | // The last *implicit operand* is the return value of a call |
| 1180 | // Insert it to to he PushedRegSet since we must not save that register |
| 1181 | // and restore it after the call. |
| 1182 | // We do this because, we look at the LV set *after* the instruction |
| 1183 | // to determine, which LRs must be saved across calls. The return value |
| 1184 | // of the call is live in this set - but we must not save/restore it. |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1185 | |
| 1186 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1187 | const Value *RetVal = getCallInstRetVal( MInst ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1188 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1189 | if( RetVal ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1190 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1191 | LiveRange *RetValLR = PRA.LRI.getLiveRangeForValue( RetVal ); |
| 1192 | assert( RetValLR && "No LR for RetValue of call"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1193 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1194 | if( RetValLR->hasColor()) |
| 1195 | PushedRegSet.insert( |
| 1196 | getUnifiedRegNum((RetValLR->getRegClass())->getID(), |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1197 | RetValLR->getColor() ) ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1201 | const LiveVarSet *LVSetAft = PRA.LVI->getLiveVarSetAfterMInst(MInst, BB); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1202 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1203 | LiveVarSet::const_iterator LIt = LVSetAft->begin(); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1204 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1205 | // for each live var in live variable set after machine inst |
| 1206 | for( ; LIt != LVSetAft->end(); ++LIt) { |
| 1207 | |
| 1208 | // get the live range corresponding to live var |
| 1209 | LiveRange *const LR = PRA.LRI.getLiveRangeForValue(*LIt ); |
| 1210 | |
| 1211 | // LR can be null if it is a const since a const |
| 1212 | // doesn't have a dominating def - see Assumptions above |
| 1213 | if( LR ) { |
| 1214 | |
| 1215 | if( LR->hasColor() ) { |
| 1216 | |
| 1217 | unsigned RCID = (LR->getRegClass())->getID(); |
| 1218 | unsigned Color = LR->getColor(); |
| 1219 | |
| 1220 | if ( isRegVolatile(RCID, Color) ) { |
| 1221 | |
| 1222 | // if the value is in both LV sets (i.e., live before and after |
| 1223 | // the call machine instruction) |
| 1224 | |
| 1225 | unsigned Reg = getUnifiedRegNum(RCID, Color); |
| 1226 | |
| 1227 | if( PushedRegSet.find(Reg) == PushedRegSet.end() ) { |
| 1228 | |
| 1229 | // if we haven't already pushed that register |
| 1230 | |
| 1231 | unsigned RegType = getRegType( LR ); |
| 1232 | |
| 1233 | // Now get two instructions - to push on stack and pop from stack |
| 1234 | // and add them to InstrnsBefore and InstrnsAfter of the |
| 1235 | // call instruction |
| 1236 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 1237 | |
| 1238 | int StackOff = PRA.mcInfo.pushTempValue(target, |
| 1239 | getSpilledRegSize(RegType)); |
| 1240 | |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 1241 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1242 | MachineInstr *AdIBefCC, *AdIAftCC, *AdICpCC; |
Vikram S. Adve | 9576b15 | 2001-11-06 05:01:54 +0000 | [diff] [blame] | 1243 | MachineInstr *AdIBef, *AdIAft; |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1244 | |
| 1245 | |
| 1246 | //---- Insert code for pushing the reg on stack ---------- |
| 1247 | |
| 1248 | if( RegType == IntCCRegType ) { |
| 1249 | |
| 1250 | // Handle IntCCRegType specially since we cannot directly |
| 1251 | // push %ccr on to the stack |
| 1252 | |
| 1253 | const LiveVarSet *LVSetBef = |
| 1254 | PRA.LVI->getLiveVarSetBeforeMInst(MInst, BB); |
| 1255 | |
| 1256 | // get a free INTEGER register |
| 1257 | int FreeIntReg = |
Ruchira Sasanka | 295264d | 2001-11-15 20:25:07 +0000 | [diff] [blame] | 1258 | PRA.getUsableUniRegAtMI(LR->getRegClass(), IntRegType, MInst, |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1259 | LVSetBef, AdIBefCC, AdIAftCC); |
| 1260 | |
| 1261 | // insert the instructions in reverse order since we are |
| 1262 | // adding them to the front of InstrnsBefore |
| 1263 | |
| 1264 | if(AdIAftCC) |
| 1265 | (PRA.AddedInstrMap[MInst]->InstrnsBefore).push_front(AdIAftCC); |
| 1266 | |
| 1267 | AdICpCC = cpCCR2IntMI(FreeIntReg); |
| 1268 | (PRA.AddedInstrMap[MInst]->InstrnsBefore).push_front(AdICpCC); |
| 1269 | |
| 1270 | if(AdIBefCC) |
| 1271 | (PRA.AddedInstrMap[MInst]->InstrnsBefore).push_front(AdIBefCC); |
| 1272 | |
Ruchira Sasanka | aa12a78 | 2001-11-10 00:26:55 +0000 | [diff] [blame] | 1273 | if(DEBUG_RA) { |
| 1274 | cerr << "\n!! Inserted caller saving (push) inst for %ccr:"; |
| 1275 | if(AdIBefCC) cerr << "\t" << *(AdIBefCC); |
| 1276 | cerr << "\t" << *AdICpCC; |
| 1277 | if(AdIAftCC) cerr << "\t" << *(AdIAftCC); |
| 1278 | } |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1279 | |
| 1280 | } else { |
| 1281 | // for any other register type, just add the push inst |
Ruchira Sasanka | 6beb013 | 2001-11-11 21:49:37 +0000 | [diff] [blame] | 1282 | AdIBef = cpReg2MemMI(Reg, getFramePointer(), StackOff, RegType ); |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1283 | ((PRA.AddedInstrMap[MInst])->InstrnsBefore).push_front(AdIBef); |
| 1284 | } |
| 1285 | |
| 1286 | |
| 1287 | //---- Insert code for popping the reg from the stack ---------- |
| 1288 | |
| 1289 | if( RegType == IntCCRegType ) { |
| 1290 | |
| 1291 | // Handle IntCCRegType specially since we cannot directly |
| 1292 | // pop %ccr on from the stack |
| 1293 | |
| 1294 | // get a free INT register |
| 1295 | int FreeIntReg = |
Ruchira Sasanka | 295264d | 2001-11-15 20:25:07 +0000 | [diff] [blame] | 1296 | PRA.getUsableUniRegAtMI(LR->getRegClass(), IntRegType, MInst, |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1297 | LVSetAft, AdIBefCC, AdIAftCC); |
| 1298 | |
| 1299 | if(AdIBefCC) |
| 1300 | (PRA.AddedInstrMap[MInst]->InstrnsAfter).push_back(AdIBefCC); |
| 1301 | |
| 1302 | AdICpCC = cpInt2CCRMI(FreeIntReg); |
| 1303 | (PRA.AddedInstrMap[MInst]->InstrnsAfter).push_back(AdICpCC); |
| 1304 | |
| 1305 | if(AdIAftCC) |
| 1306 | (PRA.AddedInstrMap[MInst]->InstrnsAfter).push_back(AdIAftCC); |
| 1307 | |
Ruchira Sasanka | aa12a78 | 2001-11-10 00:26:55 +0000 | [diff] [blame] | 1308 | if(DEBUG_RA) { |
| 1309 | |
| 1310 | cerr << "\n!! Inserted caller saving (pop) inst for %ccr:"; |
| 1311 | if(AdIBefCC) cerr << "\t" << *(AdIBefCC); |
| 1312 | cerr << "\t" << *AdICpCC; |
| 1313 | if(AdIAftCC) cerr << "\t" << *(AdIAftCC); |
| 1314 | } |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1315 | |
| 1316 | } else { |
| 1317 | // for any other register type, just add the pop inst |
Ruchira Sasanka | 6beb013 | 2001-11-11 21:49:37 +0000 | [diff] [blame] | 1318 | AdIAft = cpMem2RegMI(getFramePointer(), StackOff, Reg, RegType ); |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1319 | ((PRA.AddedInstrMap[MInst])->InstrnsAfter).push_back(AdIAft); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1320 | } |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1321 | |
| 1322 | PushedRegSet.insert( Reg ); |
| 1323 | |
Ruchira Sasanka | aa12a78 | 2001-11-10 00:26:55 +0000 | [diff] [blame] | 1324 | if(DEBUG_RA) { |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1325 | cerr << "\nFor call inst:" << *MInst; |
Ruchira Sasanka | aa12a78 | 2001-11-10 00:26:55 +0000 | [diff] [blame] | 1326 | cerr << " -inserted caller saving instrs:\n\t "; |
Vikram S. Adve | 9576b15 | 2001-11-06 05:01:54 +0000 | [diff] [blame] | 1327 | if( RegType == IntCCRegType ) |
Ruchira Sasanka | aa12a78 | 2001-11-10 00:26:55 +0000 | [diff] [blame] | 1328 | cerr << *AdIBefCC << "\t" << *AdIAftCC ; |
Vikram S. Adve | 9576b15 | 2001-11-06 05:01:54 +0000 | [diff] [blame] | 1329 | else |
Ruchira Sasanka | aa12a78 | 2001-11-10 00:26:55 +0000 | [diff] [blame] | 1330 | cerr << *AdIBef << "\t" << *AdIAft ; |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } // if not already pushed |
| 1333 | |
| 1334 | } // if LR has a volatile color |
| 1335 | |
| 1336 | } // if LR has color |
| 1337 | |
| 1338 | } // if there is a LR for Var |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1339 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1340 | } // for each value in the LV set after instruction |
| 1341 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1344 | //--------------------------------------------------------------------------- |
| 1345 | // Copies %ccr into an integer register. IntReg is the UNIFIED register |
| 1346 | // number. |
| 1347 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1348 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1349 | MachineInstr * UltraSparcRegInfo::cpCCR2IntMI(const unsigned IntReg) const { |
| 1350 | MachineInstr * MI = NULL; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1351 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1352 | MI = new MachineInstr(RDCCR, 2); |
| 1353 | MI->SetMachineOperand(0, SparcIntCCRegOrder::ccr, false); |
| 1354 | MI->SetMachineOperand(1, IntReg, true); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1355 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1356 | return MI; |
| 1357 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1358 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1359 | //--------------------------------------------------------------------------- |
| 1360 | // Copies an integer register into %ccr. IntReg is the UNIFIED register |
| 1361 | // number. |
| 1362 | //--------------------------------------------------------------------------- |
| 1363 | |
| 1364 | MachineInstr * UltraSparcRegInfo::cpInt2CCRMI(const unsigned IntReg) const { |
| 1365 | MachineInstr * MI = NULL; |
| 1366 | |
| 1367 | MI = new MachineInstr(WRCCR, 3); |
| 1368 | MI->SetMachineOperand(0, IntReg, false); |
| 1369 | MI->SetMachineOperand(1, SparcIntRegOrder::g0, false); |
| 1370 | MI->SetMachineOperand(2, SparcIntCCRegOrder::ccr, true); |
| 1371 | |
| 1372 | return MI; |
| 1373 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1374 | |
| 1375 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1376 | |
| 1377 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1378 | //--------------------------------------------------------------------------- |
| 1379 | // Print the register assigned to a LR |
| 1380 | //--------------------------------------------------------------------------- |
| 1381 | |
| 1382 | void UltraSparcRegInfo::printReg(const LiveRange *const LR) { |
| 1383 | |
| 1384 | unsigned RegClassID = (LR->getRegClass())->getID(); |
| 1385 | |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 1386 | cerr << " *Node " << (LR->getUserIGNode())->getIndex(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1387 | |
| 1388 | if( ! LR->hasColor() ) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 1389 | cerr << " - could not find a color\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | // if a color is found |
| 1394 | |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 1395 | cerr << " colored with color "<< LR->getColor(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1396 | |
| 1397 | if( RegClassID == IntRegClassID ) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 1398 | cerr<< " [" << SparcIntRegOrder::getRegName(LR->getColor()) << "]\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1399 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 1400 | } else if ( RegClassID == FloatRegClassID) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 1401 | cerr << "[" << SparcFloatRegOrder::getRegName(LR->getColor()); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1402 | if( LR->getTypeID() == Type::DoubleTyID ) |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 1403 | cerr << "+" << SparcFloatRegOrder::getRegName(LR->getColor()+1); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 1404 | cerr << "]\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1405 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1406 | } |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1407 | |
| 1408 | //--------------------------------------------------------------------------- |
| 1409 | // This method examines instructions inserted by RegAlloc code before a |
| 1410 | // machine instruction to detect invalid orders that destroy values before |
| 1411 | // they are used. If it detects such conditions, it reorders the instructions. |
| 1412 | // |
| 1413 | // The unordered instructions come in the UnordVec. These instructions are |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1414 | // instructions inserted by RegAlloc. All such instruction MUST have |
| 1415 | // their USES BEFORE THE DEFS after reordering. |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1416 | |
| 1417 | // The UnordVec & OrdVec must be DISTINCT. The OrdVec must be empty when |
| 1418 | // this method is called. |
| 1419 | |
| 1420 | // This method uses two vectors for efficiency in accessing |
| 1421 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1422 | // Since instructions are inserted in RegAlloc, this assumes that the |
| 1423 | // first operand is the source reg and the last operand is the dest reg. |
| 1424 | |
| 1425 | // All the uses are before THE def to a register |
| 1426 | |
| 1427 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1428 | //--------------------------------------------------------------------------- |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 1429 | void UltraSparcRegInfo::OrderAddedInstrns(std::vector<MachineInstr *> &UnordVec, |
| 1430 | std::vector<MachineInstr *> &OrdVec, |
| 1431 | PhyRegAlloc &PRA) const{ |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1432 | |
| 1433 | /* |
| 1434 | Problem: We can have instructions inserted by RegAlloc like |
| 1435 | 1. add %ox %g0 %oy |
| 1436 | 2. add %oy %g0 %oz, where z!=x or z==x |
| 1437 | |
| 1438 | This is wrong since %oy used by 2 is overwritten by 1 |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1439 | |
| 1440 | Solution: |
| 1441 | We re-order the instructions so that the uses are before the defs |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1442 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1443 | Algorithm: |
| 1444 | |
| 1445 | do |
| 1446 | for each instruction 'DefInst' in the UnOrdVec |
| 1447 | for each instruction 'UseInst' that follows the DefInst |
| 1448 | if the reg defined by DefInst is used by UseInst |
| 1449 | mark DefInst as not movable in this iteration |
| 1450 | If DefInst is not marked as not-movable, move DefInst to OrdVec |
| 1451 | while all instructions in DefInst are moved to OrdVec |
| 1452 | |
| 1453 | For moving, we call the move2OrdVec(). It checks whether there is a def |
| 1454 | in it for the uses in the instruction to be added to OrdVec. If there |
| 1455 | are no preceding defs, it just appends the instruction. If there is a |
| 1456 | preceding def, it puts two instructions to save the reg on stack before |
| 1457 | the load and puts a restore at use. |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1458 | |
| 1459 | */ |
| 1460 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1461 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1462 | bool CouldMoveAll; |
| 1463 | bool DebugPrint = false; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1464 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1465 | do { |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1466 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1467 | CouldMoveAll = true; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1468 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 1469 | std::vector<MachineInstr *>::iterator DefIt = UnordVec.begin(); |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1470 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1471 | for( ; DefIt != UnordVec.end(); ++DefIt ) { |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1472 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1473 | // for each instruction in the UnordVec do ... |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1474 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1475 | MachineInstr *DefInst = *DefIt; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1476 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1477 | if( DefInst == NULL) continue; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1478 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1479 | //cerr << "\nInst in UnordVec = " << *DefInst; |
| 1480 | |
| 1481 | // last operand is the def (unless for a store which has no def reg) |
| 1482 | MachineOperand& DefOp = DefInst->getOperand(DefInst->getNumOperands()-1); |
| 1483 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1484 | if( DefOp.opIsDef() && |
| 1485 | DefOp.getOperandType() == MachineOperand::MO_MachineRegister) { |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1486 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1487 | // If the operand in DefInst is a def ... |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1488 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1489 | bool DefEqUse = false; |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1490 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 1491 | std::vector<MachineInstr *>::iterator UseIt = DefIt; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1492 | UseIt++; |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1493 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1494 | for( ; UseIt != UnordVec.end(); ++UseIt ) { |
| 1495 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1496 | MachineInstr *UseInst = *UseIt; |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1497 | if( UseInst == NULL) continue; |
| 1498 | |
| 1499 | // for each inst (UseInst) that is below the DefInst do ... |
| 1500 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1501 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1502 | MachineOperand& UseOp = UseInst->getOperand(0); |
| 1503 | |
| 1504 | if( ! UseOp.opIsDef() && |
| 1505 | UseOp.getOperandType() == MachineOperand::MO_MachineRegister) { |
| 1506 | |
| 1507 | // if use is a register ... |
| 1508 | |
| 1509 | if( DefOp.getMachineRegNum() == UseOp.getMachineRegNum() ) { |
| 1510 | |
| 1511 | // if Def and this use are the same, it means that this use |
| 1512 | // is destroyed by a def before it is used |
| 1513 | |
| 1514 | // cerr << "\nCouldn't move " << *DefInst; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1515 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1516 | DefEqUse = true; |
| 1517 | CouldMoveAll = false; |
| 1518 | DebugPrint = true; |
| 1519 | break; |
| 1520 | } // if two registers are equal |
| 1521 | |
| 1522 | } // if use is a register |
| 1523 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1524 | }// for all use instructions |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1525 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1526 | if( ! DefEqUse ) { |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1527 | |
| 1528 | // after examining all the instructions that follow the DefInst |
| 1529 | // if there are no dependencies, we can move it to the OrdVec |
| 1530 | |
| 1531 | // cerr << "Moved to Ord: " << *DefInst; |
| 1532 | |
| 1533 | moveInst2OrdVec(OrdVec, DefInst, PRA); |
| 1534 | |
| 1535 | //OrdVec.push_back(DefInst); |
| 1536 | |
| 1537 | // mark the pos of DefInst with NULL to indicate that it is |
| 1538 | // empty |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1539 | *DefIt = NULL; |
| 1540 | } |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1541 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1542 | } // if Def is a machine register |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1543 | |
| 1544 | } // for all instructions in the UnordVec |
| 1545 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1546 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1547 | } while( !CouldMoveAll); |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1548 | |
| 1549 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1550 | if(DebugPrint) { |
| 1551 | cerr << "\nAdded instructions were reordered to:\n"; |
| 1552 | for(unsigned int i=0; i < OrdVec.size(); i++) |
| 1553 | cerr << *(OrdVec[i]); |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1556 | } |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1557 | |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 1565 | void UltraSparcRegInfo::moveInst2OrdVec(std::vector<MachineInstr *> &OrdVec, |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1566 | MachineInstr *UnordInst, |
| 1567 | PhyRegAlloc &PRA ) const { |
| 1568 | |
| 1569 | MachineOperand& UseOp = UnordInst->getOperand(0); |
| 1570 | |
| 1571 | if( ! UseOp.opIsDef() && |
| 1572 | UseOp.getOperandType() == MachineOperand::MO_MachineRegister) { |
| 1573 | |
| 1574 | // for the use of UnordInst, see whether there is a defining instr |
| 1575 | // before in the OrdVec |
| 1576 | bool DefEqUse = false; |
| 1577 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 1578 | std::vector<MachineInstr *>::iterator OrdIt = OrdVec.begin(); |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1579 | |
| 1580 | for( ; OrdIt != OrdVec.end(); ++OrdIt ) { |
| 1581 | |
| 1582 | MachineInstr *OrdInst = *OrdIt ; |
| 1583 | |
| 1584 | MachineOperand& DefOp = |
| 1585 | OrdInst->getOperand(OrdInst->getNumOperands()-1); |
| 1586 | |
| 1587 | if( DefOp.opIsDef() && |
| 1588 | DefOp.getOperandType() == MachineOperand::MO_MachineRegister) { |
| 1589 | |
| 1590 | //cerr << "\nDefining Ord Inst: " << *OrdInst; |
| 1591 | |
| 1592 | if( DefOp.getMachineRegNum() == UseOp.getMachineRegNum() ) { |
| 1593 | |
| 1594 | // we are here because there is a preceding def in the OrdVec |
| 1595 | // for the use in this intr we are going to insert. This |
| 1596 | // happened because the original code was like: |
| 1597 | // 1. add %ox %g0 %oy |
| 1598 | // 2. add %oy %g0 %ox |
| 1599 | // In Round1, we added 2 to OrdVec but 1 remained in UnordVec |
| 1600 | // Now we are processing %ox of 1. |
| 1601 | // We have to |
| 1602 | |
| 1603 | const int UReg = DefOp.getMachineRegNum(); |
| 1604 | const int RegType = getRegType(UReg); |
| 1605 | MachineInstr *AdIBef, *AdIAft; |
| 1606 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 1607 | const int StackOff = PRA.mcInfo.pushTempValue(target, |
| 1608 | getSpilledRegSize(RegType)); |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1609 | |
| 1610 | // Save the UReg (%ox) on stack before it's destroyed |
Ruchira Sasanka | 6beb013 | 2001-11-11 21:49:37 +0000 | [diff] [blame] | 1611 | AdIBef=cpReg2MemMI(UReg, getFramePointer(), StackOff, RegType); |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1612 | OrdIt = OrdVec.insert( OrdIt, AdIBef); |
| 1613 | OrdIt++; // points to current instr we processed |
| 1614 | |
| 1615 | // Load directly into DReg (%oy) |
| 1616 | MachineOperand& DOp= |
| 1617 | (UnordInst->getOperand(UnordInst->getNumOperands()-1)); |
| 1618 | assert(DOp.opIsDef() && "Last operand is not the def"); |
| 1619 | const int DReg = DOp.getMachineRegNum(); |
| 1620 | |
Ruchira Sasanka | 6beb013 | 2001-11-11 21:49:37 +0000 | [diff] [blame] | 1621 | AdIAft=cpMem2RegMI(getFramePointer(), StackOff, DReg, RegType); |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1622 | OrdVec.push_back(AdIAft); |
| 1623 | |
| 1624 | cerr << "\nFixed CIRCULAR references by reordering"; |
| 1625 | |
| 1626 | if( DEBUG_RA ) { |
| 1627 | cerr << "\nBefore CIRCULAR Reordering:\n"; |
| 1628 | cerr << *UnordInst; |
| 1629 | cerr << *OrdInst; |
| 1630 | |
| 1631 | cerr << "\nAfter CIRCULAR Reordering - All Inst so far:\n"; |
| 1632 | for(unsigned i=0; i < OrdVec.size(); i++) |
| 1633 | cerr << *(OrdVec[i]); |
| 1634 | } |
| 1635 | |
| 1636 | // Do not copy the UseInst to OrdVec |
| 1637 | DefEqUse = true; |
| 1638 | break; |
| 1639 | |
| 1640 | }// if two registers are equal |
| 1641 | |
| 1642 | } // if Def is a register |
| 1643 | |
| 1644 | } // for each instr in OrdVec |
| 1645 | |
| 1646 | if( !DefEqUse ) { |
| 1647 | |
| 1648 | // We didn't find a def in the OrdVec, so just append this inst |
| 1649 | OrdVec.push_back( UnordInst ); |
| 1650 | //cerr << "Reordered Inst (Moved Dn): " << *UnordInst; |
| 1651 | } |
| 1652 | |
| 1653 | }// if the operand in UnordInst is a use |
| 1654 | |
| 1655 | } |
| 1656 | |
| 1657 | |
| 1658 | |
| 1659 | |
| 1660 | |
| 1661 | |