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