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