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