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