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