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