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) |
| 29 | : MachineRegInfo(tgt), UltraSparcInfo(&tgt), NumOfIntArgRegs(6), |
| 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 | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 354 | void UltraSparcRegInfo::suggestReg4CallAddr(MachineInstr * CallMI, |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 355 | LiveRangeInfo& LRI, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 356 | std::vector<RegClass *> RCList) const { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 357 | CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI); |
| 358 | const Value *RetAddrVal = argDesc->getReturnAddrReg(); |
| 359 | assert(RetAddrVal && "Return address value is required"); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 360 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 361 | // create a new LR for the return address and color it |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 362 | LiveRange * RetAddrLR = new LiveRange(); |
Chris Lattner | d1b60fb | 2002-02-04 16:37:09 +0000 | [diff] [blame] | 363 | RetAddrLR->insert( RetAddrVal ); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 364 | unsigned RegClassID = getRegClassIDOfValue( RetAddrVal ); |
| 365 | RetAddrLR->setRegClass( RCList[RegClassID] ); |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 366 | RetAddrLR->setColor(getUnifiedRegNum(IntRegClassID,SparcIntRegClass::o7)); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 367 | LRI.addLRToMap( RetAddrVal, RetAddrLR); |
| 368 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 371 | |
| 372 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 373 | |
| 374 | //--------------------------------------------------------------------------- |
| 375 | // This method will suggest colors to incoming args to a method. |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 376 | // According to the Sparc ABI, the first 6 incoming args are in |
| 377 | // %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] | 378 | // 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] | 379 | // done - it will be colored (or spilled) as a normal live range. |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 380 | //--------------------------------------------------------------------------- |
Chris Lattner | b7653df | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 381 | void UltraSparcRegInfo::suggestRegs4MethodArgs(const Function *Meth, |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 382 | LiveRangeInfo& LRI) const |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 383 | { |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 384 | // check if this is a varArgs function. needed for choosing regs. |
| 385 | bool isVarArgs = isVarArgsFunction(Meth->getType()); |
| 386 | |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 387 | // for each argument. count INT and FP arguments separately. |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 388 | unsigned argNo=0, intArgNo=0, fpArgNo=0; |
| 389 | for(Function::const_aiterator I = Meth->abegin(), E = Meth->aend(); |
| 390 | I != E; ++I, ++argNo) { |
| 391 | // get the LR of arg |
| 392 | LiveRange *LR = LRI.getLiveRangeForValue(I); |
| 393 | assert(LR && "No live range found for method arg"); |
| 394 | |
| 395 | unsigned regType = getRegType(LR); |
| 396 | unsigned regClassIDOfArgReg = MAXINT; // reg class of chosen reg (unused) |
| 397 | |
| 398 | int regNum = (regType == IntRegType) |
| 399 | ? regNumForIntArg(/*inCallee*/ true, isVarArgs, |
| 400 | argNo, intArgNo++, fpArgNo, regClassIDOfArgReg) |
| 401 | : regNumForFPArg(regType, /*inCallee*/ true, isVarArgs, |
| 402 | argNo, intArgNo, fpArgNo++, regClassIDOfArgReg); |
| 403 | |
| 404 | if(regNum != InvalidRegNum) |
| 405 | LR->setSuggestedColor(regNum); |
| 406 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 409 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 410 | //--------------------------------------------------------------------------- |
| 411 | // This method is called after graph coloring to move incoming args to |
| 412 | // the correct hardware registers if they did not receive the correct |
| 413 | // (suggested) color through graph coloring. |
| 414 | //--------------------------------------------------------------------------- |
Chris Lattner | b7653df | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 415 | void UltraSparcRegInfo::colorMethodArgs(const Function *Meth, |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 416 | LiveRangeInfo &LRI, |
| 417 | AddedInstrns *FirstAI) const { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 418 | |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 419 | // check if this is a varArgs function. needed for choosing regs. |
| 420 | bool isVarArgs = isVarArgsFunction(Meth->getType()); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 421 | MachineInstr *AdMI; |
| 422 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 423 | // for each argument |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 424 | // for each argument. count INT and FP arguments separately. |
| 425 | unsigned argNo=0, intArgNo=0, fpArgNo=0; |
| 426 | for(Function::const_aiterator I = Meth->abegin(), E = Meth->aend(); |
| 427 | I != E; ++I, ++argNo) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 428 | // get the LR of arg |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 429 | LiveRange *LR = LRI.getLiveRangeForValue(I); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 430 | assert( LR && "No live range found for method arg"); |
| 431 | |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 432 | unsigned regType = getRegType( LR ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 433 | unsigned RegClassID = (LR->getRegClass())->getID(); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 434 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 435 | // 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] | 436 | // Also find the correct register the argument must use (UniArgReg) |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 437 | // |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 438 | bool isArgInReg = false; |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 439 | unsigned UniArgReg = InvalidRegNum; // reg that LR MUST be colored with |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 440 | unsigned regClassIDOfArgReg = MAXINT; // reg class of chosen reg |
| 441 | |
| 442 | int regNum = (regType == IntRegType) |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 443 | ? regNumForIntArg(/*inCallee*/ true, isVarArgs, |
| 444 | argNo, intArgNo++, fpArgNo, regClassIDOfArgReg) |
| 445 | : regNumForFPArg(regType, /*inCallee*/ true, isVarArgs, |
| 446 | argNo, intArgNo, fpArgNo++, regClassIDOfArgReg); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 447 | |
| 448 | if(regNum != InvalidRegNum) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 449 | isArgInReg = true; |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 450 | UniArgReg = getUnifiedRegNum( regClassIDOfArgReg, regNum); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 451 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 452 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 453 | if( LR->hasColor() ) { // if this arg received a register |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 454 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 455 | unsigned UniLRReg = getUnifiedRegNum( RegClassID, LR->getColor() ); |
| 456 | |
| 457 | // if LR received the correct color, nothing to do |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 458 | // |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 459 | if( UniLRReg == UniArgReg ) |
| 460 | continue; |
| 461 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 462 | // We are here because the LR did not receive the suggested |
| 463 | // but LR received another register. |
| 464 | // Now we have to copy the %i reg (or stack pos of arg) |
| 465 | // to the register the LR was colored with. |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 466 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 467 | // if the arg is coming in UniArgReg register, it MUST go into |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 468 | // the UniLRReg register |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 469 | // |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 470 | if( isArgInReg ) { |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 471 | if( regClassIDOfArgReg != RegClassID ) { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 472 | 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] | 473 | |
| 474 | // It is a variable argument call: the float reg must go in a %o reg. |
| 475 | // We have to move an int reg to a float reg via memory. |
| 476 | // |
| 477 | assert(isVarArgs && |
| 478 | RegClassID == FloatRegClassID && |
| 479 | regClassIDOfArgReg == IntRegClassID && |
| 480 | "This should only be an Int register for an FP argument"); |
| 481 | |
| 482 | int TmpOff = MachineCodeForMethod::get(Meth).pushTempValue(target, |
| 483 | getSpilledRegSize(regType)); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 484 | cpReg2MemMI(FirstAI->InstrnsBefore, |
| 485 | UniArgReg, getFramePointer(), TmpOff, IntRegType); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 486 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 487 | cpMem2RegMI(FirstAI->InstrnsBefore, |
| 488 | getFramePointer(), TmpOff, UniLRReg, regType); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 489 | } |
| 490 | else { |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 491 | cpReg2RegMI(FirstAI->InstrnsBefore, UniArgReg, UniLRReg, regType); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 494 | else { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 495 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 496 | // Now the arg is coming on stack. Since the LR recieved a register, |
| 497 | // we just have to load the arg on stack into that register |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 498 | // |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 499 | const MachineFrameInfo& frameInfo = target.getFrameInfo(); |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 500 | int offsetFromFP = |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 501 | frameInfo.getIncomingArgOffset(MachineCodeForMethod::get(Meth), |
| 502 | argNo); |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 503 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 504 | cpMem2RegMI(FirstAI->InstrnsBefore, |
| 505 | getFramePointer(), offsetFromFP, UniLRReg, regType); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 506 | } |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 507 | |
| 508 | } // if LR received a color |
| 509 | |
| 510 | else { |
| 511 | |
| 512 | // Now, the LR did not receive a color. But it has a stack offset for |
| 513 | // spilling. |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 514 | // So, if the arg is coming in UniArgReg register, we can just move |
| 515 | // that on to the stack pos of LR |
| 516 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 517 | if( isArgInReg ) { |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 518 | |
| 519 | if( regClassIDOfArgReg != RegClassID ) { |
| 520 | assert(0 && |
| 521 | "FP arguments to a varargs function should be explicitly " |
| 522 | "copied to/from int registers by instruction selection!"); |
| 523 | |
| 524 | // It must be a float arg for a variable argument call, which |
| 525 | // must come in a %o reg. Move the int reg to the stack. |
| 526 | // |
| 527 | assert(isVarArgs && regClassIDOfArgReg == IntRegClassID && |
| 528 | "This should only be an Int register for an FP argument"); |
| 529 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 530 | cpReg2MemMI(FirstAI->InstrnsBefore, UniArgReg, |
| 531 | getFramePointer(), LR->getSpillOffFromFP(), IntRegType); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 532 | } |
| 533 | else { |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 534 | cpReg2MemMI(FirstAI->InstrnsBefore, UniArgReg, |
| 535 | getFramePointer(), LR->getSpillOffFromFP(), regType); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 536 | } |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | else { |
| 540 | |
| 541 | // Now the arg is coming on stack. Since the LR did NOT |
| 542 | // 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] | 543 | // 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] | 544 | // since this method is called before any other method that makes |
| 545 | // uses of the stack pos of the LR (e.g., updateMachineInstr) |
| 546 | |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 547 | const MachineFrameInfo& frameInfo = target.getFrameInfo(); |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 548 | int offsetFromFP = |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 549 | frameInfo.getIncomingArgOffset(MachineCodeForMethod::get(Meth), |
| 550 | argNo); |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 551 | |
| 552 | LR->modifySpillOffFromFP( offsetFromFP ); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 553 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 554 | |
| 555 | } |
| 556 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 557 | } // for each incoming argument |
| 558 | |
| 559 | } |
| 560 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 561 | |
| 562 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 563 | //--------------------------------------------------------------------------- |
| 564 | // This method is called before graph coloring to suggest colors to the |
| 565 | // outgoing call args and the return value of the call. |
| 566 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 567 | void UltraSparcRegInfo::suggestRegs4CallArgs(MachineInstr *CallMI, |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 568 | LiveRangeInfo& LRI, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 569 | std::vector<RegClass *> RCList) const { |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 570 | assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) ); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 571 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 572 | CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 573 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 574 | suggestReg4CallAddr(CallMI, LRI, RCList); |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 575 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 576 | // First color the return value of the call instruction. The return value |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 577 | // will be in %o0 if the value is an integer type, or in %f0 if the |
| 578 | // value is a float type. |
| 579 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 580 | // the return value cannot have a LR in machine instruction since it is |
| 581 | // only defined by the call instruction |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 582 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 583 | // if type is not void, create a new live range and set its |
| 584 | // register class and add to LRI |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 585 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 586 | const Value *RetVal = argDesc->getReturnValue(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 588 | if (RetVal) { |
| 589 | assert ((!LRI.getLiveRangeForValue(RetVal)) && |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 590 | "LR for ret Value of call already definded!"); |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 592 | // create a new LR for the return value |
| 593 | LiveRange *RetValLR = new LiveRange(); |
Chris Lattner | d1b60fb | 2002-02-04 16:37:09 +0000 | [diff] [blame] | 594 | RetValLR->insert(RetVal); |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 595 | unsigned RegClassID = getRegClassIDOfValue(RetVal); |
| 596 | RetValLR->setRegClass(RCList[RegClassID]); |
| 597 | LRI.addLRToMap(RetVal, RetValLR); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 598 | |
| 599 | // now suggest a register depending on the register class of ret arg |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 600 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 601 | if( RegClassID == IntRegClassID ) |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 602 | RetValLR->setSuggestedColor(SparcIntRegClass::o0); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 603 | else if (RegClassID == FloatRegClassID ) |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 604 | RetValLR->setSuggestedColor(SparcFloatRegClass::f0 ); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 605 | else assert( 0 && "Unknown reg class for return value of call\n"); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 606 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 607 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 608 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 609 | // Now suggest colors for arguments (operands) of the call instruction. |
| 610 | // Colors are suggested only if the arg number is smaller than the |
| 611 | // the number of registers allocated for argument passing. |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 612 | // Now, go thru call args - implicit operands of the call MI |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 613 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 614 | unsigned NumOfCallArgs = argDesc->getNumArgs(); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 615 | |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 616 | for(unsigned argNo=0, i=0, intArgNo=0, fpArgNo=0; |
| 617 | i < NumOfCallArgs; ++i, ++argNo) { |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 618 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 619 | const Value *CallArg = argDesc->getArgInfo(i).getArgVal(); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 620 | |
| 621 | // get the LR of call operand (parameter) |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 622 | LiveRange *const LR = LRI.getLiveRangeForValue(CallArg); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 623 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 624 | // not possible to have a null LR since all args (even consts) |
| 625 | // must be defined before |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 626 | if (!LR) { |
| 627 | cerr << " ERROR: In call instr, no LR for arg: " << RAV(CallArg) << "\n"; |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 628 | assert(0 && "NO LR for call arg"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 631 | unsigned regType = getRegType( LR ); |
| 632 | unsigned regClassIDOfArgReg = MAXINT; // reg class of chosen reg (unused) |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 633 | |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 634 | // Choose a register for this arg depending on whether it is |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 635 | // an INT or FP value. Here we ignore whether or not it is a |
| 636 | // varargs calls, because FP arguments will be explicitly copied |
| 637 | // to an integer Value and handled under (argCopy != NULL) below. |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 638 | int regNum = (regType == IntRegType) |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 639 | ? regNumForIntArg(/*inCallee*/ false, /*isVarArgs*/ false, |
| 640 | argNo, intArgNo++, fpArgNo, regClassIDOfArgReg) |
| 641 | : regNumForFPArg(regType, /*inCallee*/ false, /*isVarArgs*/ false, |
| 642 | argNo, intArgNo, fpArgNo++, regClassIDOfArgReg); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 643 | |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 644 | // If a register could be allocated, use it. |
| 645 | // If not, do NOTHING as this will be colored as a normal value. |
| 646 | if(regNum != InvalidRegNum) |
| 647 | LR->setSuggestedColor(regNum); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 648 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 649 | // Repeat for the second copy of the argument, which would be |
| 650 | // an FP argument being passed to a function with no prototype |
| 651 | const Value *argCopy = argDesc->getArgInfo(i).getArgCopy(); |
| 652 | if (argCopy != NULL) |
| 653 | { |
| 654 | assert(regType != IntRegType && argCopy->getType()->isIntegral() |
| 655 | && "Must be passing copy of FP argument in int register"); |
| 656 | int copyRegNum = regNumForIntArg(/*inCallee*/false, /*isVarArgs*/false, |
| 657 | argNo, intArgNo, fpArgNo-1, |
| 658 | regClassIDOfArgReg); |
| 659 | assert(copyRegNum != InvalidRegNum); |
| 660 | LiveRange *const copyLR = LRI.getLiveRangeForValue(argCopy); |
| 661 | copyLR->setSuggestedColor(copyRegNum); |
| 662 | } |
| 663 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 664 | } // for all call arguments |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 665 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 669 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 670 | // Helper method for UltraSparcRegInfo::colorCallArgs(). |
| 671 | //--------------------------------------------------------------------------- |
| 672 | |
| 673 | void |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 674 | UltraSparcRegInfo::InitializeOutgoingArg(MachineInstr* CallMI, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 675 | AddedInstrns *CallAI, |
| 676 | PhyRegAlloc &PRA, LiveRange* LR, |
| 677 | unsigned regType, unsigned RegClassID, |
| 678 | int UniArgRegOrNone, unsigned int argNo, |
| 679 | std::vector<MachineInstr *>& AddedInstrnsBefore) |
| 680 | const |
| 681 | { |
| 682 | MachineInstr *AdMI; |
| 683 | bool isArgInReg = false; |
| 684 | unsigned UniArgReg = MAXINT; // unused unless initialized below |
| 685 | if (UniArgRegOrNone != InvalidRegNum) |
| 686 | { |
| 687 | isArgInReg = true; |
| 688 | UniArgReg = (unsigned) UniArgRegOrNone; |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 689 | CallMI->getRegsUsed().insert(UniArgReg); // mark the reg as used |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | if (LR->hasColor()) { |
| 693 | unsigned UniLRReg = getUnifiedRegNum(RegClassID, LR->getColor()); |
| 694 | |
| 695 | // if LR received the correct color, nothing to do |
| 696 | if( isArgInReg && UniArgReg == UniLRReg ) |
| 697 | return; |
| 698 | |
| 699 | // The LR is allocated to a register UniLRReg and must be copied |
| 700 | // to UniArgReg or to the stack slot. |
| 701 | // |
| 702 | if( isArgInReg ) { |
| 703 | // Copy UniLRReg to UniArgReg |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 704 | cpReg2RegMI(AddedInstrnsBefore, UniLRReg, UniArgReg, regType); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 705 | } |
| 706 | else { |
| 707 | // Copy UniLRReg to the stack to pass the arg on stack. |
| 708 | const MachineFrameInfo& frameInfo = target.getFrameInfo(); |
| 709 | int argOffset = frameInfo.getOutgoingArgOffset(PRA.mcInfo, argNo); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 710 | cpReg2MemMI(CallAI->InstrnsBefore, |
| 711 | UniLRReg, getStackPointer(), argOffset, regType); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | } else { // LR is not colored (i.e., spilled) |
| 715 | |
| 716 | if( isArgInReg ) { |
| 717 | // Insert a load instruction to load the LR to UniArgReg |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 718 | cpMem2RegMI(AddedInstrnsBefore, getFramePointer(), |
| 719 | LR->getSpillOffFromFP(), UniArgReg, regType); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 720 | // Now add the instruction |
| 721 | } |
| 722 | |
| 723 | else { |
| 724 | // Now, we have to pass the arg on stack. Since LR also did NOT |
| 725 | // receive a register we have to move an argument in memory to |
| 726 | // outgoing parameter on stack. |
| 727 | // Use TReg to load and store the value. |
| 728 | // Use TmpOff to save TReg, since that may have a live value. |
| 729 | // |
| 730 | int TReg = PRA.getUniRegNotUsedByThisInst( LR->getRegClass(), CallMI ); |
| 731 | int TmpOff = PRA.mcInfo.pushTempValue(target, |
| 732 | getSpilledRegSize(getRegType(LR))); |
| 733 | const MachineFrameInfo& frameInfo = target.getFrameInfo(); |
| 734 | int argOffset = frameInfo.getOutgoingArgOffset(PRA.mcInfo, argNo); |
| 735 | |
| 736 | MachineInstr *Ad1, *Ad2, *Ad3, *Ad4; |
| 737 | |
| 738 | // Sequence: |
| 739 | // (1) Save TReg on stack |
| 740 | // (2) Load LR value into TReg from stack pos of LR |
| 741 | // (3) Store Treg on outgoing Arg pos on stack |
| 742 | // (4) Load the old value of TReg from stack to TReg (restore it) |
| 743 | // |
| 744 | // OPTIMIZE THIS: |
| 745 | // When reverse pointers in MahineInstr are introduced: |
| 746 | // Call PRA.getUnusedRegAtMI(....) to get an unused reg. Step 1 is |
| 747 | // needed only if this fails. Currently, we cannot call the |
| 748 | // above method since we cannot find LVSetBefore without the BB |
| 749 | // |
| 750 | // NOTE: We directly add to CallAI->InstrnsBefore instead of adding to |
| 751 | // AddedInstrnsBefore since these instructions must not be reordered. |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 752 | cpReg2MemMI(CallAI->InstrnsBefore, |
| 753 | TReg, getFramePointer(), TmpOff, regType); |
| 754 | cpMem2RegMI(CallAI->InstrnsBefore, |
| 755 | getFramePointer(), LR->getSpillOffFromFP(), TReg, regType); |
| 756 | cpReg2MemMI(CallAI->InstrnsBefore, |
| 757 | TReg, getStackPointer(), argOffset, regType); |
| 758 | cpMem2RegMI(CallAI->InstrnsBefore, |
| 759 | getFramePointer(), TmpOff, TReg, regType); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 765 | // After graph coloring, we have call this method to see whehter the return |
| 766 | // value and the call args received the correct colors. If not, we have |
| 767 | // to instert copy instructions. |
| 768 | //--------------------------------------------------------------------------- |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 769 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 770 | void UltraSparcRegInfo::colorCallArgs(MachineInstr *CallMI, |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 771 | LiveRangeInfo &LRI, |
| 772 | AddedInstrns *CallAI, |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 773 | PhyRegAlloc &PRA, |
| 774 | const BasicBlock *BB) const { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 775 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 776 | assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) ); |
| 777 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 778 | CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI); |
| 779 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 780 | // First color the return value of the call. |
| 781 | // If there is a LR for the return value, it means this |
| 782 | // method returns a value |
| 783 | |
| 784 | MachineInstr *AdMI; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 785 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 786 | const Value *RetVal = argDesc->getReturnValue(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 787 | |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 788 | if (RetVal) { |
| 789 | LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 791 | if (!RetValLR) { |
| 792 | cerr << "\nNo LR for:" << RAV(RetVal) << "\n"; |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 793 | assert(RetValLR && "ERR:No LR for non-void return value"); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 794 | } |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 795 | |
| 796 | unsigned RegClassID = (RetValLR->getRegClass())->getID(); |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 797 | bool recvCorrectColor; |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 798 | unsigned CorrectCol; // correct color for ret value |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 799 | unsigned UniRetReg; // unified number for CorrectCol |
| 800 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 801 | if(RegClassID == IntRegClassID) |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 802 | CorrectCol = SparcIntRegClass::o0; |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 803 | else if(RegClassID == FloatRegClassID) |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 804 | CorrectCol = SparcFloatRegClass::f0; |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 805 | else { |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 806 | assert( 0 && "Unknown RegClass"); |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 807 | return; |
| 808 | } |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 809 | |
| 810 | // convert to unified number |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 811 | UniRetReg = getUnifiedRegNum(RegClassID, CorrectCol); |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 812 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 813 | // Mark the register as used by this instruction |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 814 | CallMI->getRegsUsed().insert(UniRetReg); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 815 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 816 | // if the LR received the correct color, NOTHING to do |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 817 | recvCorrectColor = RetValLR->hasColor()? RetValLR->getColor() == CorrectCol |
| 818 | : false; |
| 819 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 820 | // if we didn't receive the correct color for some reason, |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 821 | // put copy instruction |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 822 | if( !recvCorrectColor ) { |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 823 | |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 824 | unsigned regType = getRegType( RetValLR ); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 825 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 826 | if( RetValLR->hasColor() ) { |
| 827 | |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 828 | unsigned UniRetLRReg=getUnifiedRegNum(RegClassID,RetValLR->getColor()); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 829 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 830 | // the return value is coming in UniRetReg but has to go into |
| 831 | // the UniRetLRReg |
| 832 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 833 | cpReg2RegMI(CallAI->InstrnsAfter, UniRetReg, UniRetLRReg, regType); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 834 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 835 | } // if LR has color |
| 836 | else { |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 837 | |
| 838 | // if the LR did NOT receive a color, we have to move the return |
| 839 | // value coming in UniRetReg to the stack pos of spilled LR |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 840 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 841 | cpReg2MemMI(CallAI->InstrnsAfter, UniRetReg, |
| 842 | getFramePointer(),RetValLR->getSpillOffFromFP(), regType); |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 843 | } |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 844 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame] | 845 | } // the LR didn't receive the suggested color |
| 846 | |
| 847 | } // if there a return value |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 848 | |
| 849 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 850 | //------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 851 | // Now color all args of the call instruction |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 852 | //------------------------------------------- |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 853 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 854 | std::vector<MachineInstr *> AddedInstrnsBefore; |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 855 | |
| 856 | unsigned NumOfCallArgs = argDesc->getNumArgs(); |
| 857 | |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 858 | for(unsigned argNo=0, i=0, intArgNo=0, fpArgNo=0; |
| 859 | i < NumOfCallArgs; ++i, ++argNo) { |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 860 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 861 | const Value *CallArg = argDesc->getArgInfo(i).getArgVal(); |
| 862 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 863 | // get the LR of call operand (parameter) |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 864 | LiveRange *const LR = LRI.getLiveRangeForValue(CallArg); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 865 | |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 866 | unsigned RegClassID = getRegClassIDOfValue( CallArg); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 867 | unsigned regType = getRegType( RegClassID, CallArg->getType() ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 868 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 869 | // 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] | 870 | // Also find the correct register the argument must use (UniArgReg) |
| 871 | // |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 872 | bool isArgInReg = false; |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 873 | unsigned UniArgReg = InvalidRegNum; // reg that LR MUST be colored with |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 874 | unsigned regClassIDOfArgReg = MAXINT; // reg class of chosen reg |
| 875 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 876 | // Find the register that must be used for this arg, depending on |
| 877 | // whether it is an INT or FP value. Here we ignore whether or not it |
| 878 | // is a varargs calls, because FP arguments will be explicitly copied |
| 879 | // to an integer Value and handled under (argCopy != NULL) below. |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 880 | int regNum = (regType == IntRegType) |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 881 | ? regNumForIntArg(/*inCallee*/ false, /*isVarArgs*/ false, |
| 882 | argNo, intArgNo++, fpArgNo, regClassIDOfArgReg) |
| 883 | : regNumForFPArg(regType, /*inCallee*/ false, /*isVarArgs*/ false, |
| 884 | argNo, intArgNo, fpArgNo++, regClassIDOfArgReg); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 885 | |
| 886 | if(regNum != InvalidRegNum) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 887 | isArgInReg = true; |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 888 | UniArgReg = getUnifiedRegNum( regClassIDOfArgReg, regNum); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 889 | assert(regClassIDOfArgReg == RegClassID && |
| 890 | "Moving values between reg classes must happen during selection"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 891 | } |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 892 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 893 | // not possible to have a null LR since all args (even consts) |
| 894 | // must be defined before |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 895 | if (!LR) { |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 896 | 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] | 897 | assert(LR && "NO LR for call arg"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 898 | } |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 899 | |
| 900 | InitializeOutgoingArg(CallMI, CallAI, PRA, LR, regType, RegClassID, |
| 901 | UniArgReg, argNo, AddedInstrnsBefore); |
| 902 | |
| 903 | // Repeat for the second copy of the argument, which would be |
| 904 | // an FP argument being passed to a function with no prototype. |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 905 | // It may either be passed as a copy in an integer register |
| 906 | // (in argCopy), or on the stack (useStackSlot). |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 907 | const Value *argCopy = argDesc->getArgInfo(i).getArgCopy(); |
| 908 | if (argCopy != NULL) |
| 909 | { |
| 910 | assert(regType != IntRegType && argCopy->getType()->isIntegral() |
| 911 | && "Must be passing copy of FP argument in int register"); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 912 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 913 | unsigned copyRegClassID = getRegClassIDOfValue(argCopy); |
| 914 | unsigned copyRegType = getRegType(copyRegClassID, argCopy->getType()); |
| 915 | |
| 916 | int copyRegNum = regNumForIntArg(/*inCallee*/false, /*isVarArgs*/false, |
| 917 | argNo, intArgNo, fpArgNo-1, |
| 918 | regClassIDOfArgReg); |
| 919 | assert(copyRegNum != InvalidRegNum); |
| 920 | assert(regClassIDOfArgReg == copyRegClassID && |
| 921 | "Moving values between reg classes must happen during selection"); |
| 922 | |
| 923 | InitializeOutgoingArg(CallMI, CallAI, PRA, |
| 924 | LRI.getLiveRangeForValue(argCopy), copyRegType, |
| 925 | copyRegClassID, copyRegNum, argNo, |
| 926 | AddedInstrnsBefore); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 927 | } |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 928 | |
| 929 | if (regNum != InvalidRegNum && |
| 930 | argDesc->getArgInfo(i).usesStackSlot()) |
| 931 | { |
| 932 | // Pass the argument via the stack in addition to regNum |
| 933 | assert(regType != IntRegType && "Passing an integer arg. twice?"); |
| 934 | assert(!argCopy && "Passing FP arg in FP reg, INT reg, and stack?"); |
| 935 | InitializeOutgoingArg(CallMI, CallAI, PRA, LR, regType, RegClassID, |
| 936 | InvalidRegNum, argNo, AddedInstrnsBefore); |
| 937 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 938 | } // for each parameter in call instruction |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 939 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 940 | // If we added any instruction before the call instruction, verify |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 941 | // 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] | 942 | // |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 943 | std::vector<MachineInstr *> ReorderedVec; |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 944 | if (!AddedInstrnsBefore.empty()) { |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 945 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 946 | if (DEBUG_RA) { |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 947 | cerr << "\nCalling reorder with instrns: \n"; |
| 948 | for(unsigned i=0; i < AddedInstrnsBefore.size(); i++) |
| 949 | cerr << *(AddedInstrnsBefore[i]); |
| 950 | } |
| 951 | |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 952 | OrderAddedInstrns(AddedInstrnsBefore, ReorderedVec, PRA); |
| 953 | assert(ReorderedVec.size() >= AddedInstrnsBefore.size() |
| 954 | && "Dropped some instructions when reordering!"); |
| 955 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 956 | if (DEBUG_RA) { |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 957 | cerr << "\nAfter reordering instrns: \n"; |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 958 | for(unsigned i = 0; i < ReorderedVec.size(); i++) |
| 959 | cerr << *ReorderedVec[i]; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 960 | } |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 961 | } |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 962 | |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 963 | // Now insert caller saving code for this call instruction |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 964 | // |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 965 | insertCallerSavingCode(CallAI->InstrnsBefore, CallAI->InstrnsAfter, |
| 966 | CallMI, BB, PRA); |
| 967 | |
| 968 | // Then insert the final reordered code for the call arguments. |
| 969 | // |
| 970 | for(unsigned i=0; i < ReorderedVec.size(); i++) |
| 971 | CallAI->InstrnsBefore.push_back( ReorderedVec[i] ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 974 | //--------------------------------------------------------------------------- |
| 975 | // This method is called for an LLVM return instruction to identify which |
| 976 | // values will be returned from this method and to suggest colors. |
| 977 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 978 | void UltraSparcRegInfo::suggestReg4RetValue(MachineInstr *RetMI, |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 979 | LiveRangeInfo &LRI) const { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 980 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 981 | assert( (UltraSparcInfo->getInstrInfo()).isReturn( RetMI->getOpCode() ) ); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 982 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 983 | suggestReg4RetAddr(RetMI, LRI); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 984 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 985 | // if there is an implicit ref, that has to be the ret value |
| 986 | if( RetMI->getNumImplicitRefs() > 0 ) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 987 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 988 | // The first implicit operand is the return value of a return instr |
| 989 | const Value *RetVal = RetMI->getImplicitRef(0); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 990 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 991 | LiveRange *const LR = LRI.getLiveRangeForValue( RetVal ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 992 | |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 993 | if (!LR) { |
| 994 | cerr << "\nNo LR for:" << RAV(RetVal) << "\n"; |
| 995 | assert(0 && "No LR for return value of non-void method"); |
| 996 | } |
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 | unsigned RegClassID = (LR->getRegClass())->getID(); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 999 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1000 | if (RegClassID == IntRegClassID) |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1001 | LR->setSuggestedColor(SparcIntRegClass::i0); |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1002 | else if (RegClassID == FloatRegClassID) |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1003 | LR->setSuggestedColor(SparcFloatRegClass::f0); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1004 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1007 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1008 | |
| 1009 | //--------------------------------------------------------------------------- |
| 1010 | // Colors the return value of a method to %i0 or %f0, if possible. If it is |
| 1011 | // not possilbe to directly color the LR, insert a copy instruction to move |
| 1012 | // the LR to %i0 or %f0. When the LR is spilled, instead of the copy, we |
| 1013 | // have to put a load instruction. |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1014 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1015 | void UltraSparcRegInfo::colorRetValue(MachineInstr *RetMI, |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1016 | LiveRangeInfo &LRI, |
| 1017 | AddedInstrns *RetAI) const { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1018 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1019 | assert((UltraSparcInfo->getInstrInfo()).isReturn( RetMI->getOpCode())); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1020 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 1021 | // 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] | 1022 | if(RetMI->getNumImplicitRefs() > 0) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1023 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 1024 | // The first implicit operand is the return value of a return instr |
| 1025 | const Value *RetVal = RetMI->getImplicitRef(0); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1027 | LiveRange *LR = LRI.getLiveRangeForValue(RetVal); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 1029 | if (!LR) { |
| 1030 | cerr << "\nNo LR for:" << RAV(RetVal) << "\n"; |
| 1031 | // assert( LR && "No LR for return value of non-void method"); |
| 1032 | return; |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1033 | } |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 1034 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1035 | unsigned RegClassID = getRegClassIDOfValue(RetVal); |
Vikram S. Adve | 31f78c4 | 2002-04-25 04:42:21 +0000 | [diff] [blame] | 1036 | unsigned regType = getRegType( RetVal ); |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 1037 | |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 1038 | unsigned CorrectCol; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1039 | if(RegClassID == IntRegClassID) |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1040 | CorrectCol = SparcIntRegClass::i0; |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1041 | else if(RegClassID == FloatRegClassID) |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1042 | CorrectCol = SparcFloatRegClass::f0; |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 1043 | else { |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1044 | assert (0 && "Unknown RegClass"); |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 1045 | return; |
| 1046 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1047 | |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 1048 | // convert to unified number |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1049 | unsigned UniRetReg = getUnifiedRegNum(RegClassID, CorrectCol); |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 1050 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1051 | // Mark the register as used by this instruction |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 1052 | RetMI->getRegsUsed().insert(UniRetReg); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1053 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 1054 | // if the LR received the correct color, NOTHING to do |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1056 | if (LR->hasColor() && LR->getColor() == CorrectCol) |
| 1057 | return; |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 1058 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1059 | if (LR->hasColor()) { |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1060 | |
Ruchira Sasanka | 88dedc1 | 2001-10-23 21:40:39 +0000 | [diff] [blame] | 1061 | // We are here because the LR was allocted a regiter |
| 1062 | // It may be the suggested register or not |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1063 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 1064 | // copy the LR of retun value to i0 or f0 |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1065 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 1066 | unsigned UniLRReg =getUnifiedRegNum( RegClassID, LR->getColor()); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1067 | |
Ruchira Sasanka | c74a720 | 2001-10-24 15:56:58 +0000 | [diff] [blame] | 1068 | // the LR received UniLRReg but must be colored with UniRetReg |
| 1069 | // to pass as the return value |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1070 | cpReg2RegMI(RetAI->InstrnsBefore, UniLRReg, UniRetReg, regType); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1071 | } |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1072 | else { // if the LR is spilled |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1073 | cpMem2RegMI(RetAI->InstrnsBefore, getFramePointer(), |
| 1074 | LR->getSpillOffFromFP(), UniRetReg, regType); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1075 | cerr << "\nCopied the return value from stack\n"; |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1078 | } // if there is a return value |
| 1079 | |
| 1080 | } |
| 1081 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1082 | //--------------------------------------------------------------------------- |
| 1083 | // Check if a specified register type needs a scratch register to be |
| 1084 | // copied to/from memory. If it does, the reg. type that must be used |
| 1085 | // for scratch registers is returned in scratchRegType. |
| 1086 | // |
| 1087 | // Only the int CC register needs such a scratch register. |
| 1088 | // The FP CC registers can (and must) be copied directly to/from memory. |
| 1089 | //--------------------------------------------------------------------------- |
| 1090 | |
| 1091 | bool |
| 1092 | UltraSparcRegInfo::regTypeNeedsScratchReg(int RegType, |
| 1093 | int& scratchRegType) const |
| 1094 | { |
| 1095 | if (RegType == IntCCRegType) |
| 1096 | { |
| 1097 | scratchRegType = IntRegType; |
| 1098 | return true; |
| 1099 | } |
| 1100 | return false; |
| 1101 | } |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1102 | |
| 1103 | //--------------------------------------------------------------------------- |
| 1104 | // 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] | 1105 | // register number. |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1106 | //--------------------------------------------------------------------------- |
| 1107 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1108 | void |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1109 | UltraSparcRegInfo::cpReg2RegMI(vector<MachineInstr*>& mvec, |
| 1110 | unsigned SrcReg, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1111 | unsigned DestReg, |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1112 | int RegType) const { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1113 | assert( ((int)SrcReg != InvalidRegNum) && ((int)DestReg != InvalidRegNum) && |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1114 | "Invalid Register"); |
| 1115 | |
| 1116 | MachineInstr * MI = NULL; |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1117 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1118 | switch( RegType ) { |
| 1119 | |
Ruchira Sasanka | 735d6e3 | 2001-10-18 22:38:52 +0000 | [diff] [blame] | 1120 | case IntCCRegType: |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1121 | if (getRegType(DestReg) == IntRegType) |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1122 | { // copy intCC reg to int reg |
| 1123 | // Use SrcReg+1 to get the name "%ccr" instead of "%xcc" for RDCCR |
| 1124 | MI = Create2OperandInstr_Reg(RDCCR, SrcReg+1, DestReg); |
| 1125 | } |
| 1126 | else |
| 1127 | { // copy int reg to intCC reg |
| 1128 | // 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] | 1129 | assert(getRegType(SrcReg) == IntRegType |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1130 | && "Can only copy CC reg to/from integer reg"); |
| 1131 | MI = Create2OperandInstr_Reg(WRCCR, SrcReg, DestReg+1); |
| 1132 | } |
| 1133 | break; |
| 1134 | |
Ruchira Sasanka | 735d6e3 | 2001-10-18 22:38:52 +0000 | [diff] [blame] | 1135 | case FloatCCRegType: |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1136 | assert(0 && "Cannot copy FPCC register to any other register"); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1137 | break; |
| 1138 | |
| 1139 | case IntRegType: |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1140 | MI = Create3OperandInstr_Reg(ADD, SrcReg, getZeroRegNum(), DestReg); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1141 | break; |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1142 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1143 | case FPSingleRegType: |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1144 | MI = Create2OperandInstr_Reg(FMOVS, SrcReg, DestReg); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1145 | break; |
| 1146 | |
| 1147 | case FPDoubleRegType: |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1148 | MI = Create2OperandInstr_Reg(FMOVD, SrcReg, DestReg); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | |
| 1151 | default: |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1152 | assert(0 && "Unknown RegType"); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1153 | break; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1154 | } |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1155 | |
| 1156 | if (MI) |
| 1157 | mvec.push_back(MI); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1158 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1159 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1160 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 7dcd612 | 2001-10-24 22:05:34 +0000 | [diff] [blame] | 1161 | // Copy from a register to memory (i.e., Store). Register number must |
| 1162 | // be the unified register number |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1163 | //--------------------------------------------------------------------------- |
| 1164 | |
| 1165 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1166 | void |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1167 | UltraSparcRegInfo::cpReg2MemMI(vector<MachineInstr*>& mvec, |
| 1168 | unsigned SrcReg, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1169 | unsigned DestPtrReg, |
| 1170 | int Offset, int RegType, |
Chris Lattner | b82d97e | 2002-07-25 06:08:32 +0000 | [diff] [blame] | 1171 | int scratchReg) const { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1172 | MachineInstr * MI = NULL; |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1173 | switch( RegType ) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1174 | case IntRegType: |
| 1175 | MI = new MachineInstr(STX, 3); |
Vikram S. Adve | f1c15ee | 2002-03-18 03:12:16 +0000 | [diff] [blame] | 1176 | MI->SetMachineOperandReg(0, SrcReg, false); |
| 1177 | MI->SetMachineOperandReg(1, DestPtrReg, false); |
| 1178 | MI->SetMachineOperandConst(2, MachineOperand:: MO_SignExtendedImmed, |
| 1179 | (int64_t) Offset); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1180 | mvec.push_back(MI); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1181 | break; |
| 1182 | |
| 1183 | case FPSingleRegType: |
| 1184 | MI = new MachineInstr(ST, 3); |
Vikram S. Adve | f1c15ee | 2002-03-18 03:12:16 +0000 | [diff] [blame] | 1185 | MI->SetMachineOperandReg(0, SrcReg, false); |
| 1186 | MI->SetMachineOperandReg(1, DestPtrReg, false); |
| 1187 | MI->SetMachineOperandConst(2, MachineOperand:: MO_SignExtendedImmed, |
| 1188 | (int64_t) Offset); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1189 | mvec.push_back(MI); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1190 | break; |
| 1191 | |
| 1192 | case FPDoubleRegType: |
| 1193 | MI = new MachineInstr(STD, 3); |
Vikram S. Adve | f1c15ee | 2002-03-18 03:12:16 +0000 | [diff] [blame] | 1194 | MI->SetMachineOperandReg(0, SrcReg, false); |
| 1195 | MI->SetMachineOperandReg(1, DestPtrReg, false); |
| 1196 | MI->SetMachineOperandConst(2, MachineOperand:: MO_SignExtendedImmed, |
| 1197 | (int64_t) Offset); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1198 | mvec.push_back(MI); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1199 | break; |
| 1200 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1201 | case IntCCRegType: |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1202 | assert(scratchReg >= 0 && "Need scratch reg to store %ccr to memory"); |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1203 | assert(getRegType(scratchReg) ==IntRegType && "Invalid scratch reg"); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Use SrcReg+1 to get the name "%ccr" instead of "%xcc" for RDCCR |
| 1206 | MI = Create2OperandInstr_Reg(RDCCR, SrcReg+1, scratchReg); |
| 1207 | mvec.push_back(MI); |
| 1208 | |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1209 | cpReg2MemMI(mvec, scratchReg, DestPtrReg, Offset, IntRegType); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1210 | break; |
| 1211 | |
| 1212 | case FloatCCRegType: |
| 1213 | assert(0 && "Tell Vikram if this assertion fails: we may have to mask out the other bits here"); |
| 1214 | MI = new MachineInstr(STXFSR, 3); |
| 1215 | MI->SetMachineOperandReg(0, SrcReg, false); |
| 1216 | MI->SetMachineOperandReg(1, DestPtrReg, false); |
| 1217 | MI->SetMachineOperandConst(2, MachineOperand:: MO_SignExtendedImmed, |
| 1218 | (int64_t) Offset); |
| 1219 | mvec.push_back(MI); |
| 1220 | break; |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1221 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1222 | default: |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1223 | assert(0 && "Unknown RegType in cpReg2MemMI"); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1224 | } |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | |
| 1228 | //--------------------------------------------------------------------------- |
Ruchira Sasanka | 7dcd612 | 2001-10-24 22:05:34 +0000 | [diff] [blame] | 1229 | // 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] | 1230 | // register number |
| 1231 | //--------------------------------------------------------------------------- |
| 1232 | |
| 1233 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1234 | void |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1235 | UltraSparcRegInfo::cpMem2RegMI(vector<MachineInstr*>& mvec, |
| 1236 | unsigned SrcPtrReg, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1237 | int Offset, |
| 1238 | unsigned DestReg, |
| 1239 | int RegType, |
Chris Lattner | b82d97e | 2002-07-25 06:08:32 +0000 | [diff] [blame] | 1240 | int scratchReg) const { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1241 | MachineInstr * MI = NULL; |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1242 | switch (RegType) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1243 | case IntRegType: |
| 1244 | MI = new MachineInstr(LDX, 3); |
Vikram S. Adve | f1c15ee | 2002-03-18 03:12:16 +0000 | [diff] [blame] | 1245 | MI->SetMachineOperandReg(0, SrcPtrReg, false); |
| 1246 | MI->SetMachineOperandConst(1, MachineOperand:: MO_SignExtendedImmed, |
| 1247 | (int64_t) Offset); |
| 1248 | MI->SetMachineOperandReg(2, DestReg, true); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1249 | mvec.push_back(MI); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1250 | break; |
| 1251 | |
| 1252 | case FPSingleRegType: |
| 1253 | MI = new MachineInstr(LD, 3); |
Vikram S. Adve | f1c15ee | 2002-03-18 03:12:16 +0000 | [diff] [blame] | 1254 | MI->SetMachineOperandReg(0, SrcPtrReg, false); |
| 1255 | MI->SetMachineOperandConst(1, MachineOperand:: MO_SignExtendedImmed, |
| 1256 | (int64_t) Offset); |
| 1257 | MI->SetMachineOperandReg(2, DestReg, true); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1258 | mvec.push_back(MI); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1259 | break; |
| 1260 | |
| 1261 | case FPDoubleRegType: |
| 1262 | MI = new MachineInstr(LDD, 3); |
Vikram S. Adve | f1c15ee | 2002-03-18 03:12:16 +0000 | [diff] [blame] | 1263 | MI->SetMachineOperandReg(0, SrcPtrReg, false); |
| 1264 | MI->SetMachineOperandConst(1, MachineOperand:: MO_SignExtendedImmed, |
| 1265 | (int64_t) Offset); |
| 1266 | MI->SetMachineOperandReg(2, DestReg, true); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1267 | mvec.push_back(MI); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1270 | case IntCCRegType: |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1271 | assert(scratchReg >= 0 && "Need scratch reg to load %ccr from memory"); |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1272 | assert(getRegType(scratchReg) ==IntRegType && "Invalid scratch reg"); |
| 1273 | cpMem2RegMI(mvec, SrcPtrReg, Offset, scratchReg, IntRegType); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1274 | |
| 1275 | // Use DestReg+1 to get the name "%ccr" instead of "%xcc" for WRCCR |
| 1276 | MI = Create2OperandInstr_Reg(WRCCR, scratchReg, DestReg+1); |
| 1277 | mvec.push_back(MI); |
| 1278 | |
| 1279 | break; |
| 1280 | |
| 1281 | case FloatCCRegType: |
| 1282 | assert(0 && "Tell Vikram if this assertion fails: we may have to mask out the other bits here"); |
| 1283 | MI = new MachineInstr(LDXFSR, 3); |
| 1284 | MI->SetMachineOperandReg(0, SrcPtrReg, false); |
| 1285 | MI->SetMachineOperandConst(1, MachineOperand:: MO_SignExtendedImmed, |
| 1286 | (int64_t) Offset); |
| 1287 | MI->SetMachineOperandReg(2, DestReg, true); |
| 1288 | mvec.push_back(MI); |
| 1289 | break; |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1290 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1291 | default: |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1292 | assert(0 && "Unknown RegType in cpMem2RegMI"); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1293 | } |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1297 | //--------------------------------------------------------------------------- |
| 1298 | // Generate a copy instruction to copy a value to another. Temporarily |
| 1299 | // used by PhiElimination code. |
| 1300 | //--------------------------------------------------------------------------- |
| 1301 | |
| 1302 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1303 | void |
| 1304 | UltraSparcRegInfo::cpValue2Value(Value *Src, |
| 1305 | Value *Dest, |
| 1306 | vector<MachineInstr*>& mvec) const { |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1307 | int RegType = getRegType( Src ); |
| 1308 | |
| 1309 | assert( (RegType==getRegType(Src)) && "Src & Dest are diff types"); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1310 | |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 1311 | MachineInstr * MI = NULL; |
| 1312 | |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1313 | switch( RegType ) { |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1314 | case IntRegType: |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1315 | MI = new MachineInstr(ADD, 3); |
Vikram S. Adve | f1c15ee | 2002-03-18 03:12:16 +0000 | [diff] [blame] | 1316 | MI->SetMachineOperandVal(0, MachineOperand:: MO_VirtualRegister, Src, false); |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1317 | MI->SetMachineOperandReg(1, getZeroRegNum(), false); |
Vikram S. Adve | f1c15ee | 2002-03-18 03:12:16 +0000 | [diff] [blame] | 1318 | MI->SetMachineOperandVal(2, MachineOperand:: MO_VirtualRegister, Dest, true); |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1319 | break; |
| 1320 | |
| 1321 | case FPSingleRegType: |
| 1322 | MI = new MachineInstr(FMOVS, 2); |
Vikram S. Adve | f1c15ee | 2002-03-18 03:12:16 +0000 | [diff] [blame] | 1323 | MI->SetMachineOperandVal(0, MachineOperand:: MO_VirtualRegister, Src, false); |
| 1324 | MI->SetMachineOperandVal(1, MachineOperand:: MO_VirtualRegister, Dest, true); |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1325 | break; |
| 1326 | |
| 1327 | |
| 1328 | case FPDoubleRegType: |
| 1329 | MI = new MachineInstr(FMOVD, 2); |
Vikram S. Adve | f1c15ee | 2002-03-18 03:12:16 +0000 | [diff] [blame] | 1330 | MI->SetMachineOperandVal(0, MachineOperand:: MO_VirtualRegister, Src, false); |
| 1331 | MI->SetMachineOperandVal(1, MachineOperand:: MO_VirtualRegister, Dest, true); |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1332 | break; |
| 1333 | |
| 1334 | default: |
| 1335 | assert(0 && "Unknow RegType in CpValu2Value"); |
| 1336 | } |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 1337 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1338 | if (MI) |
| 1339 | mvec.push_back(MI); |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 1340 | } |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1341 | |
| 1342 | |
| 1343 | |
Ruchira Sasanka | 67a463a | 2001-11-12 14:45:33 +0000 | [diff] [blame] | 1344 | |
| 1345 | |
| 1346 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1347 | //---------------------------------------------------------------------------- |
| 1348 | // This method inserts caller saving/restoring instructons before/after |
Ruchira Sasanka | bf91552 | 2002-01-07 21:03:42 +0000 | [diff] [blame] | 1349 | // a call machine instruction. The caller saving/restoring instructions are |
| 1350 | // inserted like: |
| 1351 | // |
| 1352 | // ** caller saving instructions |
| 1353 | // other instructions inserted for the call by ColorCallArg |
| 1354 | // CALL instruction |
| 1355 | // other instructions inserted for the call ColorCallArg |
| 1356 | // ** caller restoring instructions |
| 1357 | // |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1358 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 1359 | |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1360 | |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 1361 | void |
| 1362 | UltraSparcRegInfo::insertCallerSavingCode(vector<MachineInstr*>& instrnsBefore, |
| 1363 | vector<MachineInstr*>& instrnsAfter, |
| 1364 | MachineInstr *CallMI, |
| 1365 | const BasicBlock *BB, |
| 1366 | PhyRegAlloc &PRA) const |
| 1367 | { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1368 | assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) ); |
| 1369 | |
Ruchira Sasanka | bf91552 | 2002-01-07 21:03:42 +0000 | [diff] [blame] | 1370 | // has set to record which registers were saved/restored |
| 1371 | // |
Chris Lattner | 09ff112 | 2002-07-24 21:21:32 +0000 | [diff] [blame] | 1372 | hash_set<unsigned> PushedRegSet; |
Ruchira Sasanka | bf91552 | 2002-01-07 21:03:42 +0000 | [diff] [blame] | 1373 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1374 | CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI); |
| 1375 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1376 | // Now find the LR of the return value of the call |
| 1377 | // The last *implicit operand* is the return value of a call |
| 1378 | // Insert it to to he PushedRegSet since we must not save that register |
| 1379 | // and restore it after the call. |
| 1380 | // We do this because, we look at the LV set *after* the instruction |
| 1381 | // to determine, which LRs must be saved across calls. The return value |
| 1382 | // 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] | 1383 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1384 | const Value *RetVal = argDesc->getReturnValue(); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1385 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1386 | if (RetVal) { |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1387 | LiveRange *RetValLR = PRA.LRI.getLiveRangeForValue( RetVal ); |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1388 | assert(RetValLR && "No LR for RetValue of call"); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1389 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1390 | if (RetValLR->hasColor()) |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1391 | PushedRegSet.insert( |
| 1392 | getUnifiedRegNum((RetValLR->getRegClass())->getID(), |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1393 | RetValLR->getColor() ) ); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1396 | const ValueSet &LVSetAft = PRA.LVI->getLiveVarSetAfterMInst(CallMI, BB); |
Chris Lattner | 748697d | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 1397 | ValueSet::const_iterator LIt = LVSetAft.begin(); |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1398 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1399 | // for each live var in live variable set after machine inst |
Chris Lattner | 748697d | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 1400 | for( ; LIt != LVSetAft.end(); ++LIt) { |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1401 | |
| 1402 | // get the live range corresponding to live var |
| 1403 | LiveRange *const LR = PRA.LRI.getLiveRangeForValue(*LIt ); |
| 1404 | |
| 1405 | // LR can be null if it is a const since a const |
| 1406 | // doesn't have a dominating def - see Assumptions above |
| 1407 | if( LR ) { |
| 1408 | |
| 1409 | if( LR->hasColor() ) { |
| 1410 | |
| 1411 | unsigned RCID = (LR->getRegClass())->getID(); |
| 1412 | unsigned Color = LR->getColor(); |
| 1413 | |
| 1414 | if ( isRegVolatile(RCID, Color) ) { |
| 1415 | |
| 1416 | // if the value is in both LV sets (i.e., live before and after |
| 1417 | // the call machine instruction) |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1418 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1419 | unsigned Reg = getUnifiedRegNum(RCID, Color); |
| 1420 | |
| 1421 | if( PushedRegSet.find(Reg) == PushedRegSet.end() ) { |
| 1422 | |
| 1423 | // if we haven't already pushed that register |
| 1424 | |
| 1425 | unsigned RegType = getRegType( LR ); |
| 1426 | |
| 1427 | // Now get two instructions - to push on stack and pop from stack |
| 1428 | // and add them to InstrnsBefore and InstrnsAfter of the |
| 1429 | // call instruction |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1430 | // |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 1431 | int StackOff = PRA.mcInfo.pushTempValue(target, |
| 1432 | getSpilledRegSize(RegType)); |
Vikram S. Adve | 1c0fba6 | 2001-11-08 04:56:41 +0000 | [diff] [blame] | 1433 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1434 | vector<MachineInstr*> AdIBef, AdIAft; |
| 1435 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1436 | //---- Insert code for pushing the reg on stack ---------- |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1437 | |
| 1438 | // We may need a scratch register to copy the saved value |
| 1439 | // to/from memory. This may itself have to insert code to |
| 1440 | // free up a scratch register. Any such code should go before |
| 1441 | // the save code. |
| 1442 | int scratchRegType = -1; |
| 1443 | int scratchReg = -1; |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1444 | if (regTypeNeedsScratchReg(RegType, scratchRegType)) |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1445 | { // Find a register not live in the LVSet before CallMI |
| 1446 | const ValueSet &LVSetBef = |
| 1447 | PRA.LVI->getLiveVarSetBeforeMInst(CallMI, BB); |
| 1448 | scratchReg = PRA.getUsableUniRegAtMI(scratchRegType, &LVSetBef, |
| 1449 | CallMI, AdIBef, AdIAft); |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1450 | assert(scratchReg != getInvalidRegNum()); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1451 | CallMI->getRegsUsed().insert(scratchReg); |
| 1452 | } |
| 1453 | |
| 1454 | if (AdIBef.size() > 0) |
| 1455 | instrnsBefore.insert(instrnsBefore.end(), |
| 1456 | AdIBef.begin(), AdIBef.end()); |
| 1457 | |
| 1458 | cpReg2MemMI(instrnsBefore, Reg,getFramePointer(),StackOff,RegType, |
| 1459 | scratchReg); |
| 1460 | |
Vikram S. Adve | dcde95f | 2002-07-10 21:36:00 +0000 | [diff] [blame] | 1461 | if (AdIAft.size() > 0) |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1462 | instrnsBefore.insert(instrnsBefore.end(), |
| 1463 | AdIAft.begin(), AdIAft.end()); |
| 1464 | |
Ruchira Sasanka | 3839e6e | 2001-11-03 19:59:59 +0000 | [diff] [blame] | 1465 | //---- Insert code for popping the reg from the stack ---------- |
| 1466 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1467 | // We may need a scratch register to copy the saved value |
| 1468 | // from memory. This may itself have to insert code to |
| 1469 | // free up a scratch register. Any such code should go |
| 1470 | // after the save code. |
| 1471 | // |
| 1472 | scratchRegType = -1; |
| 1473 | scratchReg = -1; |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1474 | if (regTypeNeedsScratchReg(RegType, scratchRegType)) |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1475 | { // Find a register not live in the LVSet after CallMI |
| 1476 | scratchReg = PRA.getUsableUniRegAtMI(scratchRegType, &LVSetAft, |
| 1477 | CallMI, AdIBef, AdIAft); |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1478 | assert(scratchReg != getInvalidRegNum()); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1479 | CallMI->getRegsUsed().insert(scratchReg); |
| 1480 | } |
| 1481 | |
| 1482 | if (AdIBef.size() > 0) |
| 1483 | instrnsAfter.insert(instrnsAfter.end(), |
| 1484 | AdIBef.begin(), AdIBef.end()); |
| 1485 | |
| 1486 | cpMem2RegMI(instrnsAfter, getFramePointer(), StackOff,Reg,RegType, |
| 1487 | scratchReg); |
| 1488 | |
| 1489 | if (AdIAft.size() > 0) |
| 1490 | instrnsAfter.insert(instrnsAfter.end(), |
| 1491 | AdIAft.begin(), AdIAft.end()); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1492 | |
Chris Lattner | 748697d | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 1493 | PushedRegSet.insert(Reg); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1494 | |
Ruchira Sasanka | aa12a78 | 2001-11-10 00:26:55 +0000 | [diff] [blame] | 1495 | if(DEBUG_RA) { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1496 | cerr << "\nFor call inst:" << *CallMI; |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1497 | cerr << " -inserted caller saving instrs: Before:\n\t "; |
| 1498 | for_each(instrnsBefore.begin(), instrnsBefore.end(), |
Anand Shukla | b0ad8b0 | 2002-07-09 19:16:59 +0000 | [diff] [blame] | 1499 | std::mem_fun(&MachineInstr::dump)); |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1500 | cerr << " -and After:\n\t "; |
| 1501 | for_each(instrnsAfter.begin(), instrnsAfter.end(), |
Anand Shukla | b0ad8b0 | 2002-07-09 19:16:59 +0000 | [diff] [blame] | 1502 | std::mem_fun(&MachineInstr::dump)); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1503 | } |
| 1504 | } // if not already pushed |
| 1505 | |
| 1506 | } // if LR has a volatile color |
| 1507 | |
| 1508 | } // if LR has color |
| 1509 | |
| 1510 | } // if there is a LR for Var |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1511 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1512 | } // for each value in the LV set after instruction |
Ruchira Sasanka | 9144228 | 2001-09-30 23:16:47 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 1515 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1516 | //--------------------------------------------------------------------------- |
| 1517 | // Print the register assigned to a LR |
| 1518 | //--------------------------------------------------------------------------- |
| 1519 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1520 | void UltraSparcRegInfo::printReg(const LiveRange *LR) { |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1521 | unsigned RegClassID = (LR->getRegClass())->getID(); |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 1522 | cerr << " *Node " << (LR->getUserIGNode())->getIndex(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1523 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1524 | if (!LR->hasColor()) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1525 | cerr << " - could not find a color\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1526 | return; |
| 1527 | } |
| 1528 | |
| 1529 | // if a color is found |
| 1530 | |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 1531 | cerr << " colored with color "<< LR->getColor(); |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1532 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1533 | if (RegClassID == IntRegClassID) { |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1534 | cerr<< " [" << SparcIntRegClass::getRegName(LR->getColor()) << "]\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1536 | } else if (RegClassID == FloatRegClassID) { |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1537 | cerr << "[" << SparcFloatRegClass::getRegName(LR->getColor()); |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 1538 | if( LR->getType() == Type::DoubleTy) |
Chris Lattner | 9568568 | 2002-08-12 21:25:05 +0000 | [diff] [blame] | 1539 | cerr << "+" << SparcFloatRegClass::getRegName(LR->getColor()+1); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1540 | cerr << "]\n"; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1541 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 1542 | } |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1543 | |
| 1544 | //--------------------------------------------------------------------------- |
| 1545 | // This method examines instructions inserted by RegAlloc code before a |
| 1546 | // machine instruction to detect invalid orders that destroy values before |
| 1547 | // they are used. If it detects such conditions, it reorders the instructions. |
| 1548 | // |
| 1549 | // The unordered instructions come in the UnordVec. These instructions are |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1550 | // instructions inserted by RegAlloc. All such instruction MUST have |
| 1551 | // their USES BEFORE THE DEFS after reordering. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1552 | // |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1553 | // The UnordVec & OrdVec must be DISTINCT. The OrdVec must be empty when |
| 1554 | // this method is called. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1555 | // |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1556 | // This method uses two vectors for efficiency in accessing |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1557 | // |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1558 | // Since instructions are inserted in RegAlloc, this assumes that the |
| 1559 | // 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] | 1560 | // 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] | 1561 | // |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1562 | // All the uses are before THE def to a register |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1563 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1564 | |
| 1565 | void UltraSparcRegInfo::OrderAddedInstrns(std::vector<MachineInstr*> &UnordVec, |
| 1566 | std::vector<MachineInstr*> &OrdVec, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1567 | PhyRegAlloc &PRA) const{ |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1568 | |
| 1569 | /* |
| 1570 | Problem: We can have instructions inserted by RegAlloc like |
| 1571 | 1. add %ox %g0 %oy |
| 1572 | 2. add %oy %g0 %oz, where z!=x or z==x |
| 1573 | |
| 1574 | This is wrong since %oy used by 2 is overwritten by 1 |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1575 | |
| 1576 | Solution: |
| 1577 | 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] | 1578 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1579 | Algorithm: |
| 1580 | |
| 1581 | do |
| 1582 | for each instruction 'DefInst' in the UnOrdVec |
| 1583 | for each instruction 'UseInst' that follows the DefInst |
| 1584 | if the reg defined by DefInst is used by UseInst |
| 1585 | mark DefInst as not movable in this iteration |
| 1586 | If DefInst is not marked as not-movable, move DefInst to OrdVec |
| 1587 | while all instructions in DefInst are moved to OrdVec |
| 1588 | |
| 1589 | For moving, we call the move2OrdVec(). It checks whether there is a def |
| 1590 | in it for the uses in the instruction to be added to OrdVec. If there |
| 1591 | are no preceding defs, it just appends the instruction. If there is a |
| 1592 | preceding def, it puts two instructions to save the reg on stack before |
| 1593 | the load and puts a restore at use. |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1594 | |
| 1595 | */ |
| 1596 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1597 | bool CouldMoveAll; |
| 1598 | bool DebugPrint = false; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1599 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1600 | do { |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1601 | CouldMoveAll = true; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1602 | std::vector<MachineInstr *>::iterator DefIt = UnordVec.begin(); |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1603 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1604 | for( ; DefIt != UnordVec.end(); ++DefIt ) { |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1605 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1606 | // for each instruction in the UnordVec do ... |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1607 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1608 | MachineInstr *DefInst = *DefIt; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1609 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1610 | if( DefInst == NULL) continue; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1611 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1612 | //cerr << "\nInst in UnordVec = " << *DefInst; |
| 1613 | |
| 1614 | // last operand is the def (unless for a store which has no def reg) |
| 1615 | MachineOperand& DefOp = DefInst->getOperand(DefInst->getNumOperands()-1); |
| 1616 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1617 | if( DefOp.opIsDef() && |
| 1618 | DefOp.getOperandType() == MachineOperand::MO_MachineRegister) { |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1619 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1620 | // If the operand in DefInst is a def ... |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1621 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1622 | bool DefEqUse = false; |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1624 | std::vector<MachineInstr *>::iterator UseIt = DefIt; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1625 | UseIt++; |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1626 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1627 | for( ; UseIt != UnordVec.end(); ++UseIt ) { |
| 1628 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1629 | MachineInstr *UseInst = *UseIt; |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1630 | if( UseInst == NULL) continue; |
| 1631 | |
| 1632 | // for each inst (UseInst) that is below the DefInst do ... |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1633 | MachineOperand& UseOp = UseInst->getOperand(0); |
| 1634 | |
| 1635 | if( ! UseOp.opIsDef() && |
| 1636 | UseOp.getOperandType() == MachineOperand::MO_MachineRegister) { |
| 1637 | |
| 1638 | // if use is a register ... |
| 1639 | |
| 1640 | if( DefOp.getMachineRegNum() == UseOp.getMachineRegNum() ) { |
| 1641 | |
| 1642 | // if Def and this use are the same, it means that this use |
| 1643 | // is destroyed by a def before it is used |
| 1644 | |
| 1645 | // cerr << "\nCouldn't move " << *DefInst; |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1646 | |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1647 | DefEqUse = true; |
| 1648 | CouldMoveAll = false; |
| 1649 | DebugPrint = true; |
| 1650 | break; |
| 1651 | } // if two registers are equal |
| 1652 | |
| 1653 | } // if use is a register |
| 1654 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1655 | }// for all use instructions |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1656 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1657 | if( ! DefEqUse ) { |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1658 | |
| 1659 | // after examining all the instructions that follow the DefInst |
| 1660 | // if there are no dependencies, we can move it to the OrdVec |
| 1661 | |
| 1662 | // cerr << "Moved to Ord: " << *DefInst; |
| 1663 | |
| 1664 | moveInst2OrdVec(OrdVec, DefInst, PRA); |
| 1665 | |
| 1666 | //OrdVec.push_back(DefInst); |
| 1667 | |
| 1668 | // mark the pos of DefInst with NULL to indicate that it is |
| 1669 | // empty |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1670 | *DefIt = NULL; |
| 1671 | } |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1672 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1673 | } // if Def is a machine register |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1674 | |
| 1675 | } // for all instructions in the UnordVec |
| 1676 | |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1677 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1678 | } while(!CouldMoveAll); |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1679 | |
Chris Lattner | 4401441 | 2002-06-04 03:09:57 +0000 | [diff] [blame] | 1680 | if (DebugPrint && DEBUG_RA) { |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1681 | cerr << "\nAdded instructions were reordered to:\n"; |
| 1682 | for(unsigned int i=0; i < OrdVec.size(); i++) |
| 1683 | cerr << *(OrdVec[i]); |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1684 | } |
Ruchira Sasanka | 868cf82 | 2001-11-09 23:49:14 +0000 | [diff] [blame] | 1685 | } |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1686 | |
| 1687 | |
| 1688 | |
| 1689 | |
| 1690 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1691 | void UltraSparcRegInfo::moveInst2OrdVec(std::vector<MachineInstr *> &OrdVec, |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1692 | MachineInstr *UnordInst, |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1693 | PhyRegAlloc &PRA) const { |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1694 | MachineOperand& UseOp = UnordInst->getOperand(0); |
| 1695 | |
| 1696 | if( ! UseOp.opIsDef() && |
| 1697 | UseOp.getOperandType() == MachineOperand::MO_MachineRegister) { |
| 1698 | |
| 1699 | // for the use of UnordInst, see whether there is a defining instr |
| 1700 | // before in the OrdVec |
| 1701 | bool DefEqUse = false; |
| 1702 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1703 | std::vector<MachineInstr *>::iterator OrdIt = OrdVec.begin(); |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1704 | |
| 1705 | for( ; OrdIt != OrdVec.end(); ++OrdIt ) { |
| 1706 | |
| 1707 | MachineInstr *OrdInst = *OrdIt ; |
| 1708 | |
| 1709 | MachineOperand& DefOp = |
| 1710 | OrdInst->getOperand(OrdInst->getNumOperands()-1); |
| 1711 | |
| 1712 | if( DefOp.opIsDef() && |
| 1713 | DefOp.getOperandType() == MachineOperand::MO_MachineRegister) { |
| 1714 | |
| 1715 | //cerr << "\nDefining Ord Inst: " << *OrdInst; |
| 1716 | |
| 1717 | if( DefOp.getMachineRegNum() == UseOp.getMachineRegNum() ) { |
| 1718 | |
| 1719 | // we are here because there is a preceding def in the OrdVec |
| 1720 | // for the use in this intr we are going to insert. This |
| 1721 | // happened because the original code was like: |
| 1722 | // 1. add %ox %g0 %oy |
| 1723 | // 2. add %oy %g0 %ox |
| 1724 | // In Round1, we added 2 to OrdVec but 1 remained in UnordVec |
| 1725 | // Now we are processing %ox of 1. |
| 1726 | // We have to |
| 1727 | |
| 1728 | const int UReg = DefOp.getMachineRegNum(); |
| 1729 | const int RegType = getRegType(UReg); |
| 1730 | MachineInstr *AdIBef, *AdIAft; |
| 1731 | |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 1732 | const int StackOff = PRA.mcInfo.pushTempValue(target, |
| 1733 | getSpilledRegSize(RegType)); |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1734 | |
| 1735 | // Save the UReg (%ox) on stack before it's destroyed |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1736 | vector<MachineInstr*> mvec; |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1737 | cpReg2MemMI(mvec, UReg, getFramePointer(), StackOff, RegType); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1738 | for (vector<MachineInstr*>::iterator MI=mvec.begin(); MI != mvec.end(); ++MI) { |
| 1739 | OrdIt = OrdVec.insert(OrdIt, *MI); |
| 1740 | ++OrdIt; // OrdIt must still point to current instr we processed |
| 1741 | } |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1742 | |
| 1743 | // Load directly into DReg (%oy) |
| 1744 | MachineOperand& DOp= |
| 1745 | (UnordInst->getOperand(UnordInst->getNumOperands()-1)); |
| 1746 | assert(DOp.opIsDef() && "Last operand is not the def"); |
| 1747 | const int DReg = DOp.getMachineRegNum(); |
| 1748 | |
Vikram S. Adve | 76ee6f7 | 2002-07-08 23:23:12 +0000 | [diff] [blame] | 1749 | cpMem2RegMI(OrdVec, getFramePointer(), StackOff, DReg, RegType); |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1750 | |
| 1751 | cerr << "\nFixed CIRCULAR references by reordering"; |
| 1752 | |
| 1753 | if( DEBUG_RA ) { |
| 1754 | cerr << "\nBefore CIRCULAR Reordering:\n"; |
| 1755 | cerr << *UnordInst; |
| 1756 | cerr << *OrdInst; |
| 1757 | |
| 1758 | cerr << "\nAfter CIRCULAR Reordering - All Inst so far:\n"; |
| 1759 | for(unsigned i=0; i < OrdVec.size(); i++) |
| 1760 | cerr << *(OrdVec[i]); |
| 1761 | } |
| 1762 | |
| 1763 | // Do not copy the UseInst to OrdVec |
| 1764 | DefEqUse = true; |
| 1765 | break; |
| 1766 | |
| 1767 | }// if two registers are equal |
| 1768 | |
| 1769 | } // if Def is a register |
| 1770 | |
| 1771 | } // for each instr in OrdVec |
| 1772 | |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 1773 | if(!DefEqUse) { |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1774 | |
| 1775 | // We didn't find a def in the OrdVec, so just append this inst |
| 1776 | OrdVec.push_back( UnordInst ); |
| 1777 | //cerr << "Reordered Inst (Moved Dn): " << *UnordInst; |
| 1778 | } |
| 1779 | |
| 1780 | }// if the operand in UnordInst is a use |
Ruchira Sasanka | ae4bcd7 | 2001-11-10 21:20:43 +0000 | [diff] [blame] | 1781 | } |