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