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