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