blob: fd9c75cb9b620148af8d2717deefe47b03bf3cbc [file] [log] [blame]
Ruchira Sasanka94d86e92001-09-14 20:31:39 +00001#include "llvm/Target/Sparc.h"
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +00002#include "SparcInternals.h"
3#include "llvm/Method.h"
4#include "llvm/iTerminators.h"
Ruchira Sasanka91442282001-09-30 23:16:47 +00005#include "llvm/iOther.h"
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +00006#include "llvm/CodeGen/InstrScheduling.h"
7#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +00008
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +00009#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
10#include "llvm/CodeGen/PhyRegAlloc.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000011
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000012
13
14
15//---------------------------------------------------------------------------
16// UltraSparcRegInfo
17//---------------------------------------------------------------------------
18
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000019
20
21//---------------------------------------------------------------------------
Ruchira Sasanka91442282001-09-30 23:16:47 +000022// This method sets the hidden operand for return address in RETURN and
23// JMPL machine instructions.
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000024//---------------------------------------------------------------------------
25
Ruchira Sasanka91442282001-09-30 23:16:47 +000026bool UltraSparcRegInfo::handleSpecialMInstr(const MachineInstr * MInst,
27 LiveRangeInfo& LRI,
28 vector<RegClass *>RCList) const {
29
30 unsigned OpCode = MInst->getOpCode();
31
32
33 // if the instruction is a RETURN instruction, suggest %i7
34
35 if( (UltraSparcInfo->getInstrInfo()).isReturn( OpCode ) ) {
36
37 const Value *RetAddrVal = getValue4ReturnAddr(MInst);
38
39 if( (getRegClassIDOfValue( RetAddrVal) == IntRegClassID) ) {
40 if( DEBUG_RA) {
41 cout << "\n$?$Return Address Value is not of Integer Type. Type =";
42 cout << (RetAddrVal->getType())->getPrimitiveID() << endl;
43 }
44 }
45
46
47 LiveRange * RetAddrLR = new LiveRange();
48 RetAddrLR->add(RetAddrVal);
49 RetAddrLR->setRegClass( RCList[IntRegClassID] );
50 LRI.addLRToMap( RetAddrVal, RetAddrLR);
51
52 RetAddrLR->setSuggestedColor(SparcIntRegOrder::i7);
53
54 return true;
55 }
56
57 // else if the instruction is a JMPL instruction, color it with %o7
58 // this can be permenently colored since the LR is very short (one instr)
59 // TODO: Directly change the machine register instead of creating a LR
60
61 else if( (UltraSparcInfo->getInstrInfo()).isCall(MInst->getOpCode() ) ) {
62
63 const Value *RetAddrVal = getValue4ReturnAddr(MInst);
64
65 if( (getRegClassIDOfValue( RetAddrVal) == IntRegClassID) ) {
66 if( DEBUG_RA) {
67 cout << "\n$?$Return Address Value is not of Integer Type. Type =";
68 cout << (RetAddrVal->getType())->getPrimitiveID() << endl;
69 }
70 }
71
72 LiveRange * RetAddrLR = new LiveRange();
73 RetAddrLR->add(RetAddrVal);
74 RetAddrLR->setRegClass( RCList[IntRegClassID] );
75 LRI.addLRToMap( RetAddrVal, RetAddrLR);
76
77 RetAddrLR->setColor(SparcIntRegOrder::o7);
78
79 return true;
80
81 }
82
83 else return false; // not a special machine instruction
84
85}
86
87
88//---------------------------------------------------------------------------
89// This gets the hidden value in a return register which is used to
90// pass the return address.
91//---------------------------------------------------------------------------
92
93Value *
94UltraSparcRegInfo::getValue4ReturnAddr( const MachineInstr * MInst ) const {
95
96 if( (UltraSparcInfo->getInstrInfo()).isReturn(MInst->getOpCode()) ) {
97
98 assert( (MInst->getNumOperands() == 2) && "RETURN must have 2 operands");
99 const MachineOperand & MO = MInst->getOperand(0);
100 return MO.getVRegValue();
101
102 }
103 else if( (UltraSparcInfo->getInstrInfo()).isCall(MInst->getOpCode()) ) {
104
105 assert( (MInst->getNumOperands() == 3) && "JMPL must have 3 operands");
106 const MachineOperand & MO = MInst->getOperand(2);
107 return MO.getVRegValue();
108 }
109
110 else
111 assert(0 && "Machine Instr is not a CALL/RET");
112}
113
114
115
116//---------------------------------------------------------------------------
117// This method will suggest colors to incoming args to a method.
118// If the arg is passed on stack due to the lack of regs, NOTHING will be
119// done - it will be colored (or spilled) as a normal value.
120//---------------------------------------------------------------------------
121
122void UltraSparcRegInfo::suggestRegs4MethodArgs(const Method *const Meth,
123 LiveRangeInfo& LRI) const
Chris Lattner20b1ea02001-09-14 03:47:57 +0000124{
125
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000126 // get the argument list
127 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
128 // get an iterator to arg list
129 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000130
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000131 // for each argument
Ruchira Sasanka91442282001-09-30 23:16:47 +0000132 for( unsigned argNo=0; ArgIt != ArgList.end() ; ++ArgIt, ++argNo) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000133
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000134 // get the LR of arg
135 LiveRange *const LR = LRI.getLiveRangeForValue((const Value *) *ArgIt);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000136 assert( LR && "No live range found for method arg");
137
138 unsigned RegType = getRegType( LR );
139
Chris Lattner20b1ea02001-09-14 03:47:57 +0000140
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000141 // if the arg is in int class - allocate a reg for an int arg
Ruchira Sasanka91442282001-09-30 23:16:47 +0000142 if( RegType == IntRegType ) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000143
Ruchira Sasanka91442282001-09-30 23:16:47 +0000144 if( argNo < NumOfIntArgRegs) {
145 LR->setSuggestedColor( SparcIntRegOrder::i0 + argNo );
Chris Lattner20b1ea02001-09-14 03:47:57 +0000146
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000147 }
148
149 else {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000150 // Do NOTHING as this will be colored as a normal value.
151 if (DEBUG_RA) cout << " Int Regr not suggested for method arg\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000152 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000153
Chris Lattner20b1ea02001-09-14 03:47:57 +0000154 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000155 else if( RegType==FPSingleRegType && (argNo*2+1) < NumOfFloatArgRegs)
156 LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2 + 1) );
157
158
159 else if( RegType == FPDoubleRegType && (argNo*2) < NumOfFloatArgRegs)
160 LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2) );
161
Chris Lattner20b1ea02001-09-14 03:47:57 +0000162
Chris Lattner20b1ea02001-09-14 03:47:57 +0000163 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000164
Chris Lattner20b1ea02001-09-14 03:47:57 +0000165}
166
Ruchira Sasanka91442282001-09-30 23:16:47 +0000167//---------------------------------------------------------------------------
168//
169//---------------------------------------------------------------------------
170
171void UltraSparcRegInfo::colorMethodArgs(const Method *const Meth,
172 LiveRangeInfo& LRI,
173 AddedInstrns *const FirstAI) const {
174
175 // get the argument list
176 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
177 // get an iterator to arg list
178 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
179
180 MachineInstr *AdMI;
181
182
183 // for each argument
184 for( unsigned argNo=0; ArgIt != ArgList.end() ; ++ArgIt, ++argNo) {
185
186 // get the LR of arg
187 LiveRange *const LR = LRI.getLiveRangeForValue((const Value *) *ArgIt);
188 assert( LR && "No live range found for method arg");
189
190
191 // if the LR received the suggested color, NOTHING to be done
192 if( LR->hasSuggestedColor() && LR->hasColor() )
193 if( LR->getSuggestedColor() == LR->getColor() )
194 continue;
195
196 // We are here because the LR did not have a suggested
197 // color or did not receive the suggested color. Now handle
198 // individual cases.
199
200
201 unsigned RegType = getRegType( LR );
202 unsigned RegClassID = (LR->getRegClass())->getID();
203
204
205 // find whether this argument is coming in a register (if not, on stack)
206
207 bool isArgInReg = false;
208 unsigned UniArgReg = InvalidRegNum;
209
210 if( (RegType== IntRegType && argNo < NumOfIntArgRegs)) {
211 isArgInReg = true;
212 UniArgReg = getUnifiedRegNum( RegClassID, SparcIntRegOrder::o0 + argNo );
213 }
214 else if(RegType == FPSingleRegType && argNo < NumOfFloatArgRegs) {
215 isArgInReg = true;
216 UniArgReg = getUnifiedRegNum( RegClassID,
217 SparcFloatRegOrder::f0 + argNo*2 + 1 ) ;
218 }
219 else if(RegType == FPDoubleRegType && argNo < NumOfFloatArgRegs) {
220 isArgInReg = true;
221 UniArgReg = getUnifiedRegNum(RegClassID, SparcFloatRegOrder::f0+argNo*2);
222 }
223
224
225 if( LR->hasColor() ) {
226
227 // We are here because the LR did not have a suggested
228 // color or did not receive the suggested color but LR got a register.
229 // Now we have to copy %ix reg (or stack pos of arg)
230 // to the register it was colored with.
231
232 unsigned UniLRReg = getUnifiedRegNum( RegClassID, LR->getColor() );
233
234 // if the arg is coming in a register and goes into a register
235 if( isArgInReg )
236 AdMI = cpReg2RegMI(UniArgReg, UniLRReg, RegType );
237
238 else
239 assert(0 && "TODO: Color an Incoming arg on stack");
240
241 // Now add the instruction
242 FirstAI->InstrnsBefore.push_back( AdMI );
243
244 }
245
246 else { // LR is not colored (i.e., spilled)
247
248 assert(0 && "TODO: Color a spilled arg ");
249
250 }
251
252
253 } // for each incoming argument
254
255}
256
Chris Lattner20b1ea02001-09-14 03:47:57 +0000257
258
259
Ruchira Sasanka91442282001-09-30 23:16:47 +0000260//---------------------------------------------------------------------------
261// This method is called before graph coloring to suggest colors to the
262// outgoing call args and the return value of the call.
263//---------------------------------------------------------------------------
264void UltraSparcRegInfo::suggestRegs4CallArgs(const CallInst *const CallI,
265 LiveRangeInfo& LRI,
266 vector<RegClass *> RCList) const {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000267
268
Ruchira Sasanka91442282001-09-30 23:16:47 +0000269 assert( (CallI->getOpcode() == Instruction::Call) && "Not a call instr");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000270
Ruchira Sasanka91442282001-09-30 23:16:47 +0000271 // First color the return value of the call instruction. The return value
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000272 // will be in %o0 if the value is an integer type, or in %f0 if the
273 // value is a float type.
274
Ruchira Sasanka91442282001-09-30 23:16:47 +0000275 // the return value cannot have a LR in machine instruction since it is
276 // only defined by the call instruction
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000277
Ruchira Sasanka91442282001-09-30 23:16:47 +0000278 assert( (! LRI.getLiveRangeForValue( CallI ) ) &&
279 "LR for ret Value of call already definded!");
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000280
Ruchira Sasanka91442282001-09-30 23:16:47 +0000281 // if type is not void, create a new live range and set its
282 // register class and add to LRI
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000283
Ruchira Sasanka91442282001-09-30 23:16:47 +0000284 if( ! ((CallI->getType())->getPrimitiveID() == Type::VoidTyID) ) {
285
286 // create a new LR for the return value
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000287
Ruchira Sasanka91442282001-09-30 23:16:47 +0000288 LiveRange * RetValLR = new LiveRange();
289 RetValLR->add( CallI );
290 unsigned RegClassID = getRegClassIDOfValue( CallI );
291 RetValLR->setRegClass( RCList[RegClassID] );
292 LRI.addLRToMap( CallI, RetValLR);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000293
Ruchira Sasanka91442282001-09-30 23:16:47 +0000294 // now suggest a register depending on the register class of ret arg
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000295
Ruchira Sasanka91442282001-09-30 23:16:47 +0000296 if( RegClassID == IntRegClassID )
297 RetValLR->setSuggestedColor(SparcIntRegOrder::o0);
298 else if (RegClassID == FloatRegClassID )
299 RetValLR->setSuggestedColor(SparcFloatRegOrder::f0 );
300 else assert( 0 && "Unknown reg class for return value of call\n");
301
Chris Lattner20b1ea02001-09-14 03:47:57 +0000302 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000303
304
Ruchira Sasanka91442282001-09-30 23:16:47 +0000305 // Now suggest colors for arguments (operands) of the call instruction.
306 // Colors are suggested only if the arg number is smaller than the
307 // the number of registers allocated for argument passing.
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000308
Ruchira Sasanka91442282001-09-30 23:16:47 +0000309 Instruction::op_const_iterator OpIt = CallI->op_begin();
310 ++OpIt; // first operand is the called method - skip it
311
312 // go thru all the operands of LLVM instruction
313 for(unsigned argNo=0; OpIt != CallI->op_end(); ++OpIt, ++argNo ) {
314
315 // get the LR of call operand (parameter)
316 LiveRange *const LR = LRI.getLiveRangeForValue((const Value *) *OpIt);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000317
Ruchira Sasanka91442282001-09-30 23:16:47 +0000318 if( !LR ) { // possible because arg can be a const
319 if( DEBUG_RA) {
320 cout << " Warning: In call instr, no LR for arg: " ;
321 printValue(*OpIt); cout << endl;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000322 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000323 continue;
324 }
325
326 unsigned RegType = getRegType( LR );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000327
Ruchira Sasanka91442282001-09-30 23:16:47 +0000328 // if the arg is in int class - allocate a reg for an int arg
329 if( RegType == IntRegType ) {
330
331 if( argNo < NumOfIntArgRegs)
332 LR->setSuggestedColor( SparcIntRegOrder::o0 + argNo );
333
334 else if (DEBUG_RA)
335 // Do NOTHING as this will be colored as a normal value.
336 cout << " Regr not suggested for int call arg" << endl;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000337
Ruchira Sasanka91442282001-09-30 23:16:47 +0000338 }
339 else if( RegType == FPSingleRegType && (argNo*2 +1)< NumOfFloatArgRegs)
340 LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2 + 1) );
341
342
343 else if( RegType == FPDoubleRegType && (argNo*2) < NumOfFloatArgRegs)
344 LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2) );
345
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000346
Ruchira Sasanka91442282001-09-30 23:16:47 +0000347 } // for all call arguments
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000348
Chris Lattner20b1ea02001-09-14 03:47:57 +0000349}
350
351
Ruchira Sasanka91442282001-09-30 23:16:47 +0000352//---------------------------------------------------------------------------
353// After graph coloring, we have call this method to see whehter the return
354// value and the call args received the correct colors. If not, we have
355// to instert copy instructions.
356//---------------------------------------------------------------------------
Chris Lattner20b1ea02001-09-14 03:47:57 +0000357
358
Ruchira Sasanka91442282001-09-30 23:16:47 +0000359void UltraSparcRegInfo::colorCallArgs(const CallInst *const CallI,
360 LiveRangeInfo& LRI,
361 AddedInstrns *const CallAI) const {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000362
363
Ruchira Sasanka91442282001-09-30 23:16:47 +0000364 // First color the return value of the call.
365 // If there is a LR for the return value, it means this
366 // method returns a value
367
368 MachineInstr *AdMI;
369 LiveRange * RetValLR = LRI.getLiveRangeForValue( CallI );
Chris Lattner20b1ea02001-09-14 03:47:57 +0000370
Ruchira Sasanka91442282001-09-30 23:16:47 +0000371 if( RetValLR ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000372
Ruchira Sasanka91442282001-09-30 23:16:47 +0000373 bool recvSugColor = false;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000374
Ruchira Sasanka91442282001-09-30 23:16:47 +0000375 if( RetValLR->hasSuggestedColor() && RetValLR->hasColor() )
376 if( RetValLR->getSuggestedColor() == RetValLR->getColor())
377 recvSugColor = true;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000378
Ruchira Sasanka91442282001-09-30 23:16:47 +0000379 // if we didn't receive the suggested color for some reason,
380 // put copy instruction
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000381
Ruchira Sasanka91442282001-09-30 23:16:47 +0000382 if( !recvSugColor ) {
383
384 if( RetValLR->hasColor() ) {
385
386 unsigned RegType = getRegType( RetValLR );
387 unsigned RegClassID = (RetValLR->getRegClass())->getID();
388
389 unsigned UniRetLRReg=getUnifiedRegNum(RegClassID,RetValLR->getColor());
390 unsigned UniRetReg = InvalidRegNum;
391
392 // find where we receive the return value depending on
393 // register class
394
395 if(RegClassID == IntRegClassID)
396 UniRetReg = getUnifiedRegNum( RegClassID, SparcIntRegOrder::o0);
397 else if(RegClassID == FloatRegClassID)
398 UniRetReg = getUnifiedRegNum( RegClassID, SparcFloatRegOrder::f0);
399
400
401 AdMI = cpReg2RegMI(UniRetLRReg, UniRetReg, RegType );
402 CallAI->InstrnsAfter.push_back( AdMI );
Ruchira Sasankaa5564c62001-09-19 22:40:51 +0000403
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000404
Ruchira Sasanka91442282001-09-30 23:16:47 +0000405 } // if LR has color
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000406 else {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000407
408 assert(0 && "LR of return value is splilled");
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000409 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000410
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000411
Ruchira Sasanka91442282001-09-30 23:16:47 +0000412 } // the LR didn't receive the suggested color
413
414 } // if there is a LR - i.e., return value is not void
415
416
417
418 // Now color all the operands of the call instruction
419
420 Instruction::op_const_iterator OpIt = CallI->op_begin();
421 ++OpIt; // first operand is the called method - skip it
422
423 // go thru all the operands of LLVM instruction
424 for(unsigned argNo=0; OpIt != CallI->op_end(); ++OpIt, ++argNo ) {
425
426 // get the LR of call operand (parameter)
427 LiveRange *const LR = LRI.getLiveRangeForValue((const Value *) *OpIt);
428
429 Value *ArgVal = (Value *) *OpIt;
430
431 unsigned RegType = getRegType( ArgVal );
432 unsigned RegClassID = getRegClassIDOfValue( ArgVal );
433
434 // find whether this argument is coming in a register (if not, on stack)
435
436 bool isArgInReg = false;
437 unsigned UniArgReg = InvalidRegNum;
438
439 if( (RegType== IntRegType && argNo < NumOfIntArgRegs)) {
440 isArgInReg = true;
441 UniArgReg = getUnifiedRegNum(RegClassID, SparcIntRegOrder::o0 + argNo );
442 }
443 else if(RegType == FPSingleRegType && argNo < NumOfFloatArgRegs) {
444 isArgInReg = true;
445 UniArgReg = getUnifiedRegNum(RegClassID,
446 SparcFloatRegOrder::f0 + (argNo*2 + 1) );
447 }
448 else if(RegType == FPDoubleRegType && argNo < NumOfFloatArgRegs) {
449 isArgInReg = true;
450 UniArgReg = getUnifiedRegNum(RegClassID, SparcFloatRegOrder::f0+argNo*2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000451 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000452
Chris Lattner20b1ea02001-09-14 03:47:57 +0000453
Ruchira Sasanka91442282001-09-30 23:16:47 +0000454
455 if( !LR ) { // possible because arg can be a const
456
457 if( DEBUG_RA) {
458 cout << " Warning: In call instr, no LR for arg: " ;
459 printValue(*OpIt); cout << endl;
460 }
461
462 //AdMI = cpValue2RegMI( ArgVal, UniArgReg, RegType);
463 //(CallAI->InstrnsBefore).push_back( AdMI );
464 //cout << " *Constant moved to an output register\n";
465
466 continue;
467 }
468
469
470 // if the LR received the suggested color, NOTHING to do
471
472 if( LR->hasSuggestedColor() && LR->hasColor() )
473 if( LR->getSuggestedColor() == LR->getColor() )
474 continue;
475
Chris Lattner20b1ea02001-09-14 03:47:57 +0000476
477
478
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000479
Ruchira Sasanka91442282001-09-30 23:16:47 +0000480 if( LR->hasColor() ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000481
Ruchira Sasanka91442282001-09-30 23:16:47 +0000482 // We are here because though the LR is allocated a register, it
483 // was not allocated the suggested register. So, we have to copy %ix reg
484 // (or stack pos of arg) to the register it was colored with
485
486
487 unsigned UniLRReg = getUnifiedRegNum( RegClassID, LR->getColor() );
488
489 if( isArgInReg )
490 AdMI = cpReg2RegMI(UniLRReg, UniArgReg, RegType );
491
492 else
493 assert(0 && "TODO: Push an outgoing arg on stack");
494
495 // Now add the instruction
496 CallAI->InstrnsBefore.push_back( AdMI );
497
498 }
499
500 else { // LR is not colored (i.e., spilled)
501
502 assert(0 && "TODO: Copy a spilled call arg to an output reg ");
503
504 }
505
506 } // for each parameter in call instruction
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000507
508}
509
Ruchira Sasanka91442282001-09-30 23:16:47 +0000510//---------------------------------------------------------------------------
511// This method is called for an LLVM return instruction to identify which
512// values will be returned from this method and to suggest colors.
513//---------------------------------------------------------------------------
514void UltraSparcRegInfo::suggestReg4RetValue(const ReturnInst *const RetI,
515 LiveRangeInfo& LRI) const {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000516
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000517
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000518
Ruchira Sasanka91442282001-09-30 23:16:47 +0000519 assert( (RetI->getOpcode() == Instruction::Ret) && "Not a ret instr");
520
521 // get the return value of this return instruction
522
523 const Value *RetVal = (RetI)->getReturnValue();
524
525 // if the method returns a value
526 if( RetVal ) {
527
528 MachineInstr *AdMI;
529 LiveRange *const LR = LRI.getLiveRangeForValue( RetVal );
530
531
532 if ( LR ) {
533
534 unsigned RegClassID = (LR->getRegClass())->getID();
535
536 if( RegClassID == IntRegClassID )
537 LR->setSuggestedColor(SparcIntRegOrder::i0);
538
539 else if ( RegClassID == FloatRegClassID )
540 LR->setSuggestedColor(SparcFloatRegOrder::f0);
541
542 }
543 else {
544 if( DEBUG_RA )
545 cout << "Warning: No LR for return value" << endl;
546 // possible since this can be returning a constant
547 }
548
549
550
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000551 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000552
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000553
Ruchira Sasanka91442282001-09-30 23:16:47 +0000554
555}
556
557//---------------------------------------------------------------------------
558
559//---------------------------------------------------------------------------
560void UltraSparcRegInfo::colorRetValue(const ReturnInst *const RetI,
561 LiveRangeInfo& LRI,
562 AddedInstrns *const RetAI) const {
563
564
565 // get the return value of this return instruction
566 Value *RetVal = (Value *) (RetI)->getReturnValue();
567
568 // if the method returns a value
569 if( RetVal ) {
570
571 MachineInstr *AdMI;
572 LiveRange *const LR = LRI.getLiveRangeForValue( RetVal );
573
574 unsigned RegClassID = getRegClassIDOfValue(RetVal);
575 unsigned RegType = getRegType( RetVal );
576 unsigned UniRetReg = InvalidRegNum;
577
578 if(RegClassID == IntRegClassID)
579 UniRetReg = getUnifiedRegNum( RegClassID, SparcIntRegOrder::i0 );
580 else if(RegClassID == FloatRegClassID)
581 UniRetReg = getUnifiedRegNum( RegClassID, SparcFloatRegOrder::f0);
582
583 if ( LR ) {
584
585 // if the LR received the suggested color, NOTHING to do
586
587 if( LR->hasSuggestedColor() && LR->hasColor() )
588 if( LR->getSuggestedColor() == LR->getColor() )
589 return;
590
591 if( LR->hasColor() ) {
592
593 // We are here because the LR was allocted a regiter, but NOT
594 // the correct register.
595
596 // copy the LR of retun value to i0 or f0
597
598 unsigned UniLRReg =getUnifiedRegNum( RegClassID, LR->getColor());
599
600 if(RegClassID == IntRegClassID)
601 UniRetReg = getUnifiedRegNum( RegClassID, SparcIntRegOrder::i0);
602 else if(RegClassID == FloatRegClassID)
603 UniRetReg = getUnifiedRegNum( RegClassID, SparcFloatRegOrder::f0);
604
605 AdMI = cpReg2RegMI( UniLRReg, UniRetReg, RegType);
606
607 }
608 else
609 assert(0 && "TODO: Copy the return value from stack\n");
610
611 } else {
612
613 // if NO LR we have to add an explicit copy to move the value to
614 // the return register.
615
616 //AdMI = cpValue2RegMI( RetVal, UniRetReg, RegType);
617 //(RetAI->InstrnsBefore).push_back( AdMI );
618
619 // assert( 0 && "Returned constant must be moved to the ret reg\n");
620 }
621
622
623 } // if there is a return value
624
625}
626
627
628//---------------------------------------------------------------------------
629// Copy from a register to register. Register number must be the unified
630// register number
631//---------------------------------------------------------------------------
632
633
634MachineInstr * UltraSparcRegInfo::cpReg2RegMI(const unsigned SrcReg,
635 const unsigned DestReg,
636 const int RegType) const {
637
638 assert( (SrcReg != InvalidRegNum) && (DestReg != InvalidRegNum) &&
639 "Invalid Register");
640
641 MachineInstr * MI = NULL;
642
643 switch( RegType ) {
644
645 case IntRegType:
646 MI = new MachineInstr(ADD, 3);
647 MI->SetMachineOperand(0, SrcReg, false);
648 MI->SetMachineOperand(1, SparcIntRegOrder::g0, false);
649 MI->SetMachineOperand(2, DestReg, true);
650 break;
651
652 case FPSingleRegType:
653 MI = new MachineInstr(FMOVS, 2);
654 MI->SetMachineOperand(0, SrcReg, false);
655 MI->SetMachineOperand(1, DestReg, true);
656 break;
657
658 case FPDoubleRegType:
659 MI = new MachineInstr(FMOVD, 2);
660 MI->SetMachineOperand(0, SrcReg, false);
661 MI->SetMachineOperand(1, DestReg, true);
662 break;
663
664 default:
665 assert(0 && "Unknow RegType");
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000666 }
667
668 return MI;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000669}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000670
671
Ruchira Sasanka91442282001-09-30 23:16:47 +0000672
673
674//---------------------------------------------------------------------------
675// Only constant/label values are accepted.
676// ***This code is temporary ***
677//---------------------------------------------------------------------------
678
679
680MachineInstr * UltraSparcRegInfo::cpValue2RegMI(Value * Val,
681 const unsigned DestReg,
682 const int RegType) const {
683
684 assert( (DestReg != InvalidRegNum) && "Invalid Register");
685
686 /*
687 unsigned MReg;
688 int64_t Imm;
689
690 MachineOperand::MachineOperandType MOTypeInt =
691 ChooseRegOrImmed(Val, ADD, *UltraSparcInfo, true, MReg, Imm);
692 */
693
694 MachineOperand::MachineOperandType MOType;
695
696 switch( Val->getValueType() ) {
697
698 case Value::ConstantVal:
Chris Lattneref9c23f2001-10-03 14:53:21 +0000699 case Value::GlobalVariableVal:
Ruchira Sasanka91442282001-09-30 23:16:47 +0000700 MOType = MachineOperand:: MO_UnextendedImmed; // TODO**** correct???
701 break;
702
703 case Value::BasicBlockVal:
704 case Value::MethodVal:
705 MOType = MachineOperand::MO_PCRelativeDisp;
706 break;
707
708 default:
709 cout << "Value Type: " << Val->getValueType() << endl;
710 assert(0 && "Unknown val type - Only constants/globals/labels are valid");
711 }
712
713
714
715 MachineInstr * MI = NULL;
716
717 switch( RegType ) {
718
719 case IntRegType:
720 MI = new MachineInstr(ADD);
721 MI->SetMachineOperand(0, MOType, Val, false);
722 MI->SetMachineOperand(1, SparcIntRegOrder::g0, false);
723 MI->SetMachineOperand(2, DestReg, true);
724 break;
725
726 case FPSingleRegType:
727 assert(0 && "FP const move not yet implemented");
728 MI = new MachineInstr(FMOVS);
729 MI->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, Val, false);
730 MI->SetMachineOperand(1, DestReg, true);
731 break;
732
733 case FPDoubleRegType:
734 assert(0 && "FP const move not yet implemented");
735 MI = new MachineInstr(FMOVD);
736 MI->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, Val, false);
737 MI->SetMachineOperand(1, DestReg, true);
738 break;
739
740 default:
741 assert(0 && "Unknow RegType");
742 }
743
744 return MI;
745}
746
747
748
749
750
751
752
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000753//---------------------------------------------------------------------------
754// Print the register assigned to a LR
755//---------------------------------------------------------------------------
756
757void UltraSparcRegInfo::printReg(const LiveRange *const LR) {
758
759 unsigned RegClassID = (LR->getRegClass())->getID();
760
761 cout << " *Node " << (LR->getUserIGNode())->getIndex();
762
763 if( ! LR->hasColor() ) {
764 cout << " - could not find a color" << endl;
765 return;
766 }
767
768 // if a color is found
769
770 cout << " colored with color "<< LR->getColor();
771
772 if( RegClassID == IntRegClassID ) {
773
774 cout<< " [" << SparcIntRegOrder::getRegName(LR->getColor()) ;
775 cout << "]" << endl;
776 }
777 else if ( RegClassID == FloatRegClassID) {
778 cout << "[" << SparcFloatRegOrder::getRegName(LR->getColor());
779 if( LR->getTypeID() == Type::DoubleTyID )
780 cout << "+" << SparcFloatRegOrder::getRegName(LR->getColor()+1);
781 cout << "]" << endl;
782 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000783
Ruchira Sasanka94d86e92001-09-14 20:31:39 +0000784
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000785}
Ruchira Sasanka91442282001-09-30 23:16:47 +0000786
787
788
789
790