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