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