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