blob: 08dbbc53551f8d3c427bb7812cc03fb51011b397 [file] [log] [blame]
Vikram S. Advea21cf202001-07-21 12:42:19 +00001// $Id$
2//***************************************************************************
3// File:
4// SparcInstrSelection.cpp
5//
6// Purpose:
7//
8// History:
9// 7/02/01 - Vikram Adve - Created
Vikram S. Adved4228a52001-08-28 23:12:57 +000010//**************************************************************************/
Vikram S. Advea21cf202001-07-21 12:42:19 +000011
Vikram S. Adved4228a52001-08-28 23:12:57 +000012#include "llvm/Support/MathExtras.h"
Vikram S. Advea21cf202001-07-21 12:42:19 +000013#include "llvm/Type.h"
14#include "llvm/DerivedTypes.h"
15#include "llvm/SymbolTable.h"
16#include "llvm/Value.h"
17#include "llvm/Instruction.h"
18#include "llvm/InstrTypes.h"
19#include "llvm/iTerminators.h"
20#include "llvm/iMemory.h"
21#include "llvm/iOther.h"
22#include "llvm/BasicBlock.h"
23#include "llvm/Method.h"
24#include "llvm/ConstPoolVals.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000025#include "llvm/CodeGen/Sparc.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/InstrForest.h"
28#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Advea21cf202001-07-21 12:42:19 +000029
30
31//******************** Internal Data Declarations ************************/
32
33// to be used later
34struct BranchPattern {
35 bool flipCondition; // should the sense of the test be reversed
36 BasicBlock* targetBB; // which basic block to branch to
37 MachineInstr* extraBranch; // if neither branch is fall-through, then this
38 // BA must be inserted after the cond'l one
39};
40
41//************************* Forward Declarations ***************************/
42
43
Vikram S. Adve4f231662001-07-28 04:15:15 +000044static MachineOpCode ChooseBprInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000045
Vikram S. Adve4f231662001-07-28 04:15:15 +000046static MachineOpCode ChooseBccInstruction (const InstructionNode* instrNode,
47 bool& isFPBranch);
Vikram S. Advea21cf202001-07-21 12:42:19 +000048
Vikram S. Adve4f231662001-07-28 04:15:15 +000049static MachineOpCode ChooseBpccInstruction (const InstructionNode* instrNode,
50 const BinaryOperator* setCCInst);
Vikram S. Advea21cf202001-07-21 12:42:19 +000051
Vikram S. Adve4f231662001-07-28 04:15:15 +000052static MachineOpCode ChooseBFpccInstruction (const InstructionNode* instrNode,
53 const BinaryOperator* setCCInst);
Vikram S. Advea21cf202001-07-21 12:42:19 +000054
Vikram S. Adve4f231662001-07-28 04:15:15 +000055static MachineOpCode ChooseMovFpccInstruction(const InstructionNode*);
56
57static MachineOpCode ChooseMovpccAfterSub (const InstructionNode* instrNode,
58 bool& mustClearReg,
59 int& valueToMove);
60
61static MachineOpCode ChooseConvertToFloatInstr(const InstructionNode*,
Chris Lattner51a9ad92001-07-21 22:57:05 +000062 const Type* opType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000063
Vikram S. Adve4f231662001-07-28 04:15:15 +000064static MachineOpCode ChooseConvertToIntInstr(const InstructionNode* instrNode,
65 const Type* opType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000066
Vikram S. Adve4f231662001-07-28 04:15:15 +000067static MachineOpCode ChooseAddInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000068
Vikram S. Adve4f231662001-07-28 04:15:15 +000069static MachineOpCode ChooseSubInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000070
Vikram S. Adve4f231662001-07-28 04:15:15 +000071static MachineOpCode ChooseFcmpInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000072
Vikram S. Adve4f231662001-07-28 04:15:15 +000073static MachineOpCode ChooseMulInstruction (const InstructionNode* instrNode,
74 bool checkCasts);
Vikram S. Advea21cf202001-07-21 12:42:19 +000075
Vikram S. Adve4f231662001-07-28 04:15:15 +000076static MachineOpCode ChooseDivInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000077
Vikram S. Adve4f231662001-07-28 04:15:15 +000078static MachineOpCode ChooseLoadInstruction (const Type* resultType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000079
Vikram S. Adve4f231662001-07-28 04:15:15 +000080static MachineOpCode ChooseStoreInstruction (const Type* valueType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000081
Vikram S. Adve4f231662001-07-28 04:15:15 +000082static void SetOperandsForMemInstr(MachineInstr* minstr,
Vikram S. Advea21cf202001-07-21 12:42:19 +000083 const InstructionNode* vmInstrNode,
Vikram S. Adve4f231662001-07-28 04:15:15 +000084 const TargetMachine& target);
Vikram S. Advea21cf202001-07-21 12:42:19 +000085
86static void SetMemOperands_Internal (MachineInstr* minstr,
87 const InstructionNode* vmInstrNode,
88 Value* ptrVal,
89 Value* arrayOffsetVal,
90 const vector<ConstPoolVal*>& idxVec,
Vikram S. Adve4f231662001-07-28 04:15:15 +000091 const TargetMachine& target);
Vikram S. Advea21cf202001-07-21 12:42:19 +000092
93static unsigned FixConstantOperands(const InstructionNode* vmInstrNode,
Vikram S. Adve4f231662001-07-28 04:15:15 +000094 MachineInstr** mvec,
95 unsigned numInstr,
96 TargetMachine& target);
Vikram S. Advea21cf202001-07-21 12:42:19 +000097
Vikram S. Adved4228a52001-08-28 23:12:57 +000098static MachineInstr* MakeLoadConstInstr(Instruction* vmInstr,
99 Value* val,
100 TmpInstruction*& tmpReg,
101 MachineInstr*& getMinstr2);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000102
103static void ForwardOperand (InstructionNode* treeNode,
104 InstructionNode* parent,
105 int operandNum);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000106
107
Vikram S. Adved4228a52001-08-28 23:12:57 +0000108//************************ Internal Functions ******************************/
Vikram S. Advea21cf202001-07-21 12:42:19 +0000109
110
111//------------------------------------------------------------------------
112// External Function: ThisIsAChainRule
113//
114// Purpose:
115// Check if a given BURG rule is a chain rule.
116//------------------------------------------------------------------------
117
118extern bool
119ThisIsAChainRule(int eruleno)
120{
121 switch(eruleno)
122 {
123 case 111: // stmt: reg
124 case 112: // stmt: boolconst
125 case 113: // stmt: bool
126 case 121:
127 case 122:
128 case 123:
129 case 124:
130 case 125:
131 case 126:
132 case 127:
133 case 128:
134 case 129:
135 case 130:
136 case 131:
137 case 132:
Vikram S. Adve74f4a132001-07-31 21:46:57 +0000138 case 153:
139 case 155: return true; break;
Vikram S. Advea21cf202001-07-21 12:42:19 +0000140
141 default: return false; break;
142 }
143}
144
Vikram S. Adved4228a52001-08-28 23:12:57 +0000145
146static inline MachineOpCode
147ChooseBprInstruction(const InstructionNode* instrNode)
148{
149 MachineOpCode opCode;
150
151 Instruction* setCCInstr =
152 ((InstructionNode*) instrNode->leftChild())->getInstruction();
153
154 switch(setCCInstr->getOpcode())
155 {
156 case Instruction::SetEQ: opCode = BRZ; break;
157 case Instruction::SetNE: opCode = BRNZ; break;
158 case Instruction::SetLE: opCode = BRLEZ; break;
159 case Instruction::SetGE: opCode = BRGEZ; break;
160 case Instruction::SetLT: opCode = BRLZ; break;
161 case Instruction::SetGT: opCode = BRGZ; break;
162 default:
163 assert(0 && "Unrecognized VM instruction!");
164 opCode = INVALID_OPCODE;
165 break;
166 }
167
168 return opCode;
169}
170
171
172static inline MachineOpCode
173ChooseBccInstruction(const InstructionNode* instrNode,
174 bool& isFPBranch)
175{
176 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
177 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
178 const Type* setCCType = setCCInstr->getOperand(0)->getType();
179
180 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
181
182 if (isFPBranch)
183 return ChooseBFpccInstruction(instrNode, setCCInstr);
184 else
185 return ChooseBpccInstruction(instrNode, setCCInstr);
186}
187
188
189static inline MachineOpCode
190ChooseBpccInstruction(const InstructionNode* instrNode,
191 const BinaryOperator* setCCInstr)
192{
193 MachineOpCode opCode = INVALID_OPCODE;
194
195 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
196
197 if (isSigned)
198 {
199 switch(setCCInstr->getOpcode())
200 {
201 case Instruction::SetEQ: opCode = BE; break;
202 case Instruction::SetNE: opCode = BNE; break;
203 case Instruction::SetLE: opCode = BLE; break;
204 case Instruction::SetGE: opCode = BGE; break;
205 case Instruction::SetLT: opCode = BL; break;
206 case Instruction::SetGT: opCode = BG; break;
207 default:
208 assert(0 && "Unrecognized VM instruction!");
209 break;
210 }
211 }
212 else
213 {
214 switch(setCCInstr->getOpcode())
215 {
216 case Instruction::SetEQ: opCode = BE; break;
217 case Instruction::SetNE: opCode = BNE; break;
218 case Instruction::SetLE: opCode = BLEU; break;
219 case Instruction::SetGE: opCode = BCC; break;
220 case Instruction::SetLT: opCode = BCS; break;
221 case Instruction::SetGT: opCode = BGU; break;
222 default:
223 assert(0 && "Unrecognized VM instruction!");
224 break;
225 }
226 }
227
228 return opCode;
229}
230
231static inline MachineOpCode
232ChooseBFpccInstruction(const InstructionNode* instrNode,
233 const BinaryOperator* setCCInstr)
234{
235 MachineOpCode opCode = INVALID_OPCODE;
236
237 switch(setCCInstr->getOpcode())
238 {
239 case Instruction::SetEQ: opCode = FBE; break;
240 case Instruction::SetNE: opCode = FBNE; break;
241 case Instruction::SetLE: opCode = FBLE; break;
242 case Instruction::SetGE: opCode = FBGE; break;
243 case Instruction::SetLT: opCode = FBL; break;
244 case Instruction::SetGT: opCode = FBG; break;
245 default:
246 assert(0 && "Unrecognized VM instruction!");
247 break;
248 }
249
250 return opCode;
251}
252
253
254static inline MachineOpCode
255ChooseMovFpccInstruction(const InstructionNode* instrNode)
256{
257 MachineOpCode opCode = INVALID_OPCODE;
258
259 switch(instrNode->getInstruction()->getOpcode())
260 {
261 case Instruction::SetEQ: opCode = MOVFE; break;
262 case Instruction::SetNE: opCode = MOVFNE; break;
263 case Instruction::SetLE: opCode = MOVFLE; break;
264 case Instruction::SetGE: opCode = MOVFGE; break;
265 case Instruction::SetLT: opCode = MOVFL; break;
266 case Instruction::SetGT: opCode = MOVFG; break;
267 default:
268 assert(0 && "Unrecognized VM instruction!");
269 break;
270 }
271
272 return opCode;
273}
274
275
276// Assumes that SUBcc v1, v2 -> v3 has been executed.
277// In most cases, we want to clear v3 and then follow it by instruction
278// MOVcc 1 -> v3.
279// Set mustClearReg=false if v3 need not be cleared before conditional move.
280// Set valueToMove=0 if we want to conditionally move 0 instead of 1
281// (i.e., we want to test inverse of a condition)
282//
283//
284static MachineOpCode
285ChooseMovpccAfterSub(const InstructionNode* instrNode,
286 bool& mustClearReg,
287 int& valueToMove)
288{
289 MachineOpCode opCode = INVALID_OPCODE;
290 mustClearReg = true;
291 valueToMove = 1;
292
293 switch(instrNode->getInstruction()->getOpcode())
294 {
295 case Instruction::SetEQ: opCode = MOVNE; mustClearReg = false;
296 valueToMove = 0; break;
297 case Instruction::SetLE: opCode = MOVLE; break;
298 case Instruction::SetGE: opCode = MOVGE; break;
299 case Instruction::SetLT: opCode = MOVL; break;
300 case Instruction::SetGT: opCode = MOVG; break;
301
302 case Instruction::SetNE: assert(0 && "No move required!");
303
304 default:
305 assert(0 && "Unrecognized VM instruction!");
306 break;
307 }
308
309 return opCode;
310}
311
312
313static inline MachineOpCode
314ChooseConvertToFloatInstr(const InstructionNode* instrNode,
315 const Type* opType)
316{
317 MachineOpCode opCode = INVALID_OPCODE;
318
319 switch(instrNode->getOpLabel())
320 {
321 case ToFloatTy:
322 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
323 opCode = FITOS;
324 else if (opType == Type::LongTy)
325 opCode = FXTOS;
326 else if (opType == Type::DoubleTy)
327 opCode = FDTOS;
328 else
329 assert(0 && "Cannot convert this type to FLOAT on SPARC");
330 break;
331
332 case ToDoubleTy:
333 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
334 opCode = FITOD;
335 else if (opType == Type::LongTy)
336 opCode = FXTOD;
337 else if (opType == Type::FloatTy)
338 opCode = FSTOD;
339 else
340 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
341 break;
342
343 default:
344 break;
345 }
346
347 return opCode;
348}
349
350static inline MachineOpCode
351ChooseConvertToIntInstr(const InstructionNode* instrNode,
352 const Type* opType)
353{
354 MachineOpCode opCode = INVALID_OPCODE;;
355
356 int instrType = (int) instrNode->getOpLabel();
357
358 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
359 {
360 switch (opType->getPrimitiveID())
361 {
362 case Type::FloatTyID: opCode = FSTOI; break;
363 case Type::DoubleTyID: opCode = FDTOI; break;
364 default:
365 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
366 break;
367 }
368 }
369 else if (instrType == ToLongTy)
370 {
371 switch (opType->getPrimitiveID())
372 {
373 case Type::FloatTyID: opCode = FSTOX; break;
374 case Type::DoubleTyID: opCode = FDTOX; break;
375 default:
376 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
377 break;
378 }
379 }
380 else
381 assert(0 && "Should not get here, Mo!");
382
383 return opCode;
384}
385
386
387static inline MachineOpCode
388ChooseAddInstruction(const InstructionNode* instrNode)
389{
390 MachineOpCode opCode = INVALID_OPCODE;
391
392 const Type* resultType = instrNode->getInstruction()->getType();
393
394 if (resultType->isIntegral() ||
395 resultType->isPointerType() ||
396 resultType->isMethodType() ||
397 resultType->isLabelType())
398 {
399 opCode = ADD;
400 }
401 else
402 {
403 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
404 switch(operand->getType()->getPrimitiveID())
405 {
406 case Type::FloatTyID: opCode = FADDS; break;
407 case Type::DoubleTyID: opCode = FADDD; break;
408 default: assert(0 && "Invalid type for ADD instruction"); break;
409 }
410 }
411
412 return opCode;
413}
414
415
416static inline MachineInstr*
417CreateMovFloatInstruction(const InstructionNode* instrNode,
418 const Type* resultType)
419{
420 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
421 ? FMOVS : FMOVD);
422 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
423 instrNode->leftChild()->getValue());
424 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
425 instrNode->getValue());
426 return minstr;
427}
428
429static inline MachineInstr*
430CreateAddConstInstruction(const InstructionNode* instrNode)
431{
432 MachineInstr* minstr = NULL;
433
434 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
435 assert(constOp->getValueType() == Value::ConstantVal);
436
437 // Cases worth optimizing are:
438 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
439 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
440 //
441 const Type* resultType = instrNode->getInstruction()->getType();
442
443 if (resultType == Type::FloatTy ||
444 resultType == Type::DoubleTy)
445 {
446 double dval = ((ConstPoolFP*) constOp)->getValue();
447 if (dval == 0.0)
448 minstr = CreateMovFloatInstruction(instrNode, resultType);
449 }
450
451 return minstr;
452}
453
454
455static inline MachineOpCode
456ChooseSubInstruction(const InstructionNode* instrNode)
457{
458 MachineOpCode opCode = INVALID_OPCODE;
459
460 const Type* resultType = instrNode->getInstruction()->getType();
461
462 if (resultType->isIntegral() ||
463 resultType->isPointerType())
464 {
465 opCode = SUB;
466 }
467 else
468 {
469 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
470 switch(operand->getType()->getPrimitiveID())
471 {
472 case Type::FloatTyID: opCode = FSUBS; break;
473 case Type::DoubleTyID: opCode = FSUBD; break;
474 default: assert(0 && "Invalid type for SUB instruction"); break;
475 }
476 }
477
478 return opCode;
479}
480
481
482static inline MachineInstr*
483CreateSubConstInstruction(const InstructionNode* instrNode)
484{
485 MachineInstr* minstr = NULL;
486
487 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
488 assert(constOp->getValueType() == Value::ConstantVal);
489
490 // Cases worth optimizing are:
491 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
492 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
493 //
494 const Type* resultType = instrNode->getInstruction()->getType();
495
496 if (resultType == Type::FloatTy ||
497 resultType == Type::DoubleTy)
498 {
499 double dval = ((ConstPoolFP*) constOp)->getValue();
500 if (dval == 0.0)
501 minstr = CreateMovFloatInstruction(instrNode, resultType);
502 }
503
504 return minstr;
505}
506
507
508static inline MachineOpCode
509ChooseFcmpInstruction(const InstructionNode* instrNode)
510{
511 MachineOpCode opCode = INVALID_OPCODE;
512
513 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
514 switch(operand->getType()->getPrimitiveID())
515 {
516 case Type::FloatTyID: opCode = FCMPS; break;
517 case Type::DoubleTyID: opCode = FCMPD; break;
518 default: assert(0 && "Invalid type for FCMP instruction"); break;
519 }
520
521 return opCode;
522}
523
524
525// Assumes that leftArg and rightArg are both cast instructions.
526//
527static inline bool
528BothFloatToDouble(const InstructionNode* instrNode)
529{
530 InstrTreeNode* leftArg = instrNode->leftChild();
531 InstrTreeNode* rightArg = instrNode->rightChild();
532 InstrTreeNode* leftArgArg = leftArg->leftChild();
533 InstrTreeNode* rightArgArg = rightArg->leftChild();
534 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
535
536 // Check if both arguments are floats cast to double
537 return (leftArg->getValue()->getType() == Type::DoubleTy &&
538 leftArgArg->getValue()->getType() == Type::FloatTy &&
539 rightArgArg->getValue()->getType() == Type::FloatTy);
540}
541
542
543static inline MachineOpCode
544ChooseMulInstruction(const InstructionNode* instrNode,
545 bool checkCasts)
546{
547 MachineOpCode opCode = INVALID_OPCODE;
548
549 if (checkCasts && BothFloatToDouble(instrNode))
550 {
551 return opCode = FSMULD;
552 }
553 // else fall through and use the regular multiply instructions
554
555 const Type* resultType = instrNode->getInstruction()->getType();
556
557 if (resultType->isIntegral())
558 {
559 opCode = MULX;
560 }
561 else
562 {
563 switch(instrNode->leftChild()->getValue()->getType()->getPrimitiveID())
564 {
565 case Type::FloatTyID: opCode = FMULS; break;
566 case Type::DoubleTyID: opCode = FMULD; break;
567 default: assert(0 && "Invalid type for MUL instruction"); break;
568 }
569 }
570
571 return opCode;
572}
573
574
575static inline MachineInstr*
576CreateIntNegInstruction(Value* vreg)
577{
578 MachineInstr* minstr = new MachineInstr(SUB);
579 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
580 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
581 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
582 return minstr;
583}
584
585
586static inline MachineInstr*
587CreateMulConstInstruction(const InstructionNode* instrNode,
588 MachineInstr*& getMinstr2)
589{
590 MachineInstr* minstr = NULL;
591 getMinstr2 = NULL;
592 bool needNeg = false;
593
594 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
595 assert(constOp->getValueType() == Value::ConstantVal);
596
597 // Cases worth optimizing are:
598 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
599 // (2) Multiply by 2^x for integer types: replace with Shift
600 //
601 const Type* resultType = instrNode->getInstruction()->getType();
602
603 if (resultType->isIntegral())
604 {
605 unsigned pow;
606 bool isValidConst;
607 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
608 if (isValidConst)
609 {
610 bool needNeg = false;
611 if (C < 0)
612 {
613 needNeg = true;
614 C = -C;
615 }
616
617 if (C == 0 || C == 1)
618 {
619 minstr = new MachineInstr(ADD);
620
621 if (C == 0)
622 minstr->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
623 else
624 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
625 instrNode->leftChild()->getValue());
626 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
627 }
628 else if (IsPowerOf2(C, pow))
629 {
630 minstr = new MachineInstr((resultType == Type::LongTy)
631 ? SLLX : SLL);
632 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
633 instrNode->leftChild()->getValue());
634 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
635 pow);
636 }
637
638 if (minstr && needNeg)
639 { // insert <reg = SUB 0, reg> after the instr to flip the sign
640 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
641 }
642 }
643 }
644 else
645 {
646 if (resultType == Type::FloatTy ||
647 resultType == Type::DoubleTy)
648 {
649 bool isValidConst;
650 double dval = ((ConstPoolFP*) constOp)->getValue();
651
652 if (isValidConst)
653 {
654 if (dval == 0)
655 {
656 minstr = new MachineInstr((resultType == Type::FloatTy)
657 ? FITOS : FITOD);
658 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
659 }
660 else if (fabs(dval) == 1)
661 {
662 bool needNeg = (dval < 0);
663
664 MachineOpCode opCode = needNeg
665 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
666 : (resultType == Type::FloatTy? FMOVS : FMOVD);
667
668 minstr = new MachineInstr(opCode);
669 minstr->SetMachineOperand(0,
670 MachineOperand::MO_VirtualRegister,
671 instrNode->leftChild()->getValue());
672 }
673 }
674 }
675 }
676
677 if (minstr != NULL)
678 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
679 instrNode->getValue());
680
681 return minstr;
682}
683
684
685static inline MachineOpCode
686ChooseDivInstruction(const InstructionNode* instrNode)
687{
688 MachineOpCode opCode = INVALID_OPCODE;
689
690 const Type* resultType = instrNode->getInstruction()->getType();
691
692 if (resultType->isIntegral())
693 {
694 opCode = resultType->isSigned()? SDIVX : UDIVX;
695 }
696 else
697 {
698 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
699 switch(operand->getType()->getPrimitiveID())
700 {
701 case Type::FloatTyID: opCode = FDIVS; break;
702 case Type::DoubleTyID: opCode = FDIVD; break;
703 default: assert(0 && "Invalid type for DIV instruction"); break;
704 }
705 }
706
707 return opCode;
708}
709
710
711static inline MachineInstr*
712CreateDivConstInstruction(const InstructionNode* instrNode,
713 MachineInstr*& getMinstr2)
714{
715 MachineInstr* minstr = NULL;
716 getMinstr2 = NULL;
717
718 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
719 assert(constOp->getValueType() == Value::ConstantVal);
720
721 // Cases worth optimizing are:
722 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
723 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
724 //
725 const Type* resultType = instrNode->getInstruction()->getType();
726
727 if (resultType->isIntegral())
728 {
729 unsigned pow;
730 bool isValidConst;
731 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
732 if (isValidConst)
733 {
734 bool needNeg = false;
735 if (C < 0)
736 {
737 needNeg = true;
738 C = -C;
739 }
740
741 if (C == 1)
742 {
743 minstr = new MachineInstr(ADD);
744 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
745 instrNode->leftChild()->getValue());
746 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
747 }
748 else if (IsPowerOf2(C, pow))
749 {
750 MachineOpCode opCode= ((resultType->isSigned())
751 ? (resultType==Type::LongTy)? SRAX : SRA
752 : (resultType==Type::LongTy)? SRLX : SRL);
753 minstr = new MachineInstr(opCode);
754 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
755 instrNode->leftChild()->getValue());
756 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
757 pow);
758 }
759
760 if (minstr && needNeg)
761 { // insert <reg = SUB 0, reg> after the instr to flip the sign
762 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
763 }
764 }
765 }
766 else
767 {
768 if (resultType == Type::FloatTy ||
769 resultType == Type::DoubleTy)
770 {
771 bool isValidConst;
772 double dval = ((ConstPoolFP*) constOp)->getValue();
773
774 if (isValidConst && fabs(dval) == 1)
775 {
776 bool needNeg = (dval < 0);
777
778 MachineOpCode opCode = needNeg
779 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
780 : (resultType == Type::FloatTy? FMOVS : FMOVD);
781
782 minstr = new MachineInstr(opCode);
783 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
784 instrNode->leftChild()->getValue());
785 }
786 }
787 }
788
789 if (minstr != NULL)
790 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
791 instrNode->getValue());
792
793 return minstr;
794}
795
796
797static inline MachineOpCode
798ChooseLoadInstruction(const Type* resultType)
799{
800 MachineOpCode opCode = INVALID_OPCODE;
801
802 switch (resultType->getPrimitiveID())
803 {
804 case Type::BoolTyID: opCode = LDUB; break;
805 case Type::UByteTyID: opCode = LDUB; break;
806 case Type::SByteTyID: opCode = LDSB; break;
807 case Type::UShortTyID: opCode = LDUH; break;
808 case Type::ShortTyID: opCode = LDSH; break;
809 case Type::UIntTyID: opCode = LDUW; break;
810 case Type::IntTyID: opCode = LDSW; break;
811 case Type::ULongTyID:
812 case Type::LongTyID: opCode = LDX; break;
813 case Type::FloatTyID: opCode = LD; break;
814 case Type::DoubleTyID: opCode = LDD; break;
815 default: assert(0 && "Invalid type for Load instruction"); break;
816 }
817
818 return opCode;
819}
820
821
822static inline MachineOpCode
823ChooseStoreInstruction(const Type* valueType)
824{
825 MachineOpCode opCode = INVALID_OPCODE;
826
827 switch (valueType->getPrimitiveID())
828 {
829 case Type::BoolTyID:
830 case Type::UByteTyID:
831 case Type::SByteTyID: opCode = STB; break;
832 case Type::UShortTyID:
833 case Type::ShortTyID: opCode = STH; break;
834 case Type::UIntTyID:
835 case Type::IntTyID: opCode = STW; break;
836 case Type::ULongTyID:
837 case Type::LongTyID: opCode = STX; break;
838 case Type::FloatTyID: opCode = ST; break;
839 case Type::DoubleTyID: opCode = STD; break;
840 default: assert(0 && "Invalid type for Store instruction"); break;
841 }
842
843 return opCode;
844}
845
846
847//------------------------------------------------------------------------
848// Function SetOperandsForMemInstr
849//
850// Choose addressing mode for the given load or store instruction.
851// Use [reg+reg] if it is an indexed reference, and the index offset is
852// not a constant or if it cannot fit in the offset field.
853// Use [reg+offset] in all other cases.
854//
855// This assumes that all array refs are "lowered" to one of these forms:
856// %x = load (subarray*) ptr, constant ; single constant offset
857// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
858// Generally, this should happen via strength reduction + LICM.
859// Also, strength reduction should take care of using the same register for
860// the loop index variable and an array index, when that is profitable.
861//------------------------------------------------------------------------
862
863static void
864SetOperandsForMemInstr(MachineInstr* minstr,
865 const InstructionNode* vmInstrNode,
866 const TargetMachine& target)
867{
868 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
869
870 // Variables to hold the index vector, ptr value, and offset value.
871 // The major work here is to extract these for all 3 instruction types
872 // and then call the common function SetMemOperands_Internal().
873 //
874 const vector<ConstPoolVal*>* idxVec = & memInst->getIndexVec();
875 vector<ConstPoolVal*>* newIdxVec = NULL;
876 Value* ptrVal;
877 Value* arrayOffsetVal = NULL;
878
879 // Test if a GetElemPtr instruction is being folded into this mem instrn.
880 // If so, it will be in the left child for Load and GetElemPtr,
881 // and in the right child for Store instructions.
882 //
883 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
884 ? vmInstrNode->rightChild()
885 : vmInstrNode->leftChild());
886
887 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
888 ptrChild->getOpLabel() == GetElemPtrIdx)
889 {
890 // There is a GetElemPtr instruction and there may be a chain of
891 // more than one. Use the pointer value of the last one in the chain.
892 // Fold the index vectors from the entire chain and from the mem
893 // instruction into one single index vector.
894 // Finally, we never fold for an array instruction so make that NULL.
895
896 newIdxVec = new vector<ConstPoolVal*>;
897 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
898
899 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
900 idxVec = newIdxVec;
901
902 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
903 && "GetElemPtr cannot be folded into array refs in selection");
904 }
905 else
906 {
907 // There is no GetElemPtr instruction.
908 // Use the pointer value and the index vector from the Mem instruction.
909 // If it is an array reference, get the array offset value.
910 //
911 ptrVal = memInst->getPtrOperand();
912
913 const Type* opType =
914 ((const PointerType*) ptrVal->getType())->getValueType();
915 if (opType->isArrayType())
916 {
917 assert((memInst->getNumOperands()
918 == (unsigned) 1 + memInst->getFirstOffsetIdx())
919 && "Array refs must be lowered before Instruction Selection");
920
921 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
922 }
923 }
924
925 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
926 *idxVec, target);
927
928 if (newIdxVec != NULL)
929 delete newIdxVec;
930}
931
932
933static void
934SetMemOperands_Internal(MachineInstr* minstr,
935 const InstructionNode* vmInstrNode,
936 Value* ptrVal,
937 Value* arrayOffsetVal,
938 const vector<ConstPoolVal*>& idxVec,
939 const TargetMachine& target)
940{
941 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
942
943 // Initialize so we default to storing the offset in a register.
944 int64_t smallConstOffset;
945 Value* valueForRegOffset = NULL;
946 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
947
948 // Check if there is an index vector and if so, if it translates to
949 // a small enough constant to fit in the immediate-offset field.
950 //
951 if (idxVec.size() > 0)
952 {
953 bool isConstantOffset = false;
954 unsigned offset;
955
956 const PointerType* ptrType = (PointerType*) ptrVal->getType();
957
958 if (ptrType->getValueType()->isStructType())
959 {
960 // the offset is always constant for structs
961 isConstantOffset = true;
962
963 // Compute the offset value using the index vector
964 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
965 }
966 else
967 {
968 // It must be an array ref. Check if the offset is a constant,
969 // and that the indexing has been lowered to a single offset.
970 //
971 assert(ptrType->getValueType()->isArrayType());
972 assert(arrayOffsetVal != NULL
973 && "Expect to be given Value* for array offsets");
974
975 if (ConstPoolVal *CPV = arrayOffsetVal->castConstant())
976 {
977 isConstantOffset = true; // always constant for structs
978 assert(arrayOffsetVal->getType()->isIntegral());
979 offset = (CPV->getType()->isSigned()
980 ? ((ConstPoolSInt*)CPV)->getValue()
981 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
982 }
983 else
984 {
985 valueForRegOffset = arrayOffsetVal;
986 }
987 }
988
989 if (isConstantOffset)
990 {
991 // create a virtual register for the constant
Chris Lattner1fa0c092001-09-07 21:22:57 +0000992 valueForRegOffset = ConstPoolSInt::get(Type::IntTy, offset);
Vikram S. Adved4228a52001-08-28 23:12:57 +0000993 }
994 }
995 else
996 {
997 offsetOpType = MachineOperand::MO_SignExtendedImmed;
998 smallConstOffset = 0;
999 }
1000
1001 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
1002 // It is the left child in the instruction tree in all cases.
1003 Value* leftVal = vmInstrNode->leftChild()->getValue();
1004 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
1005
1006 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
1007 // Operand 3 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
1008 //
1009 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
1010 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1011 {
1012 assert(valueForRegOffset != NULL);
1013 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
1014 }
1015 else
1016 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
1017
1018 if (memInst->getOpcode() == Instruction::Store)
1019 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
1020 else
1021 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1022 vmInstrNode->getValue());
1023}
1024
1025
1026// Special handling for constant operands:
1027// -- if the constant is 0, use the hardwired 0 register, if any;
1028// -- if the constant is of float or double type but has an integer value,
1029// use int-to-float conversion instruction instead of generating a load;
1030// -- if the constant fits in the IMMEDIATE field, use that field;
1031// -- else insert instructions to put the constant into a register, either
1032// directly or by loading explicitly from the constant pool.
1033//
1034static unsigned
1035FixConstantOperands(const InstructionNode* vmInstrNode,
1036 MachineInstr** mvec,
1037 unsigned numInstr,
1038 TargetMachine& target)
1039{
1040 static MachineInstr* loadConstVec[MAX_INSTR_PER_VMINSTR];
1041
1042 unsigned numNew = 0;
1043 Instruction* vmInstr = vmInstrNode->getInstruction();
1044
1045 for (unsigned i=0; i < numInstr; i++)
1046 {
1047 MachineInstr* minstr = mvec[i];
1048 const MachineInstrDescriptor& instrDesc =
1049 target.getInstrInfo().getDescriptor(minstr->getOpCode());
1050
1051 for (unsigned op=0; op < minstr->getNumOperands(); op++)
1052 {
1053 const MachineOperand& mop = minstr->getOperand(op);
1054
1055 // skip the result position (for efficiency below) and any other
1056 // positions already marked as not a virtual register
1057 if (instrDesc.resultPos == (int) op ||
1058 mop.getOperandType() != MachineOperand::MO_VirtualRegister ||
1059 mop.getVRegValue() == NULL)
1060 {
1061 break;
1062 }
1063
1064 Value* opValue = mop.getVRegValue();
1065
1066 if (opValue->getValueType() == Value::ConstantVal)
1067 {
1068 unsigned int machineRegNum;
1069 int64_t immedValue;
1070 MachineOperand::MachineOperandType opType =
1071 ChooseRegOrImmed(opValue, minstr->getOpCode(), target,
1072 /*canUseImmed*/ (op == 1),
1073 machineRegNum, immedValue);
1074
1075 if (opType == MachineOperand::MO_MachineRegister)
1076 minstr->SetMachineOperand(op, machineRegNum);
1077 else if (opType == MachineOperand::MO_VirtualRegister)
1078 {
1079 // value is constant and must be loaded into a register
1080 TmpInstruction* tmpReg;
1081 MachineInstr* minstr2;
1082 loadConstVec[numNew++] = MakeLoadConstInstr(vmInstr, opValue,
1083 tmpReg, minstr2);
1084 minstr->SetMachineOperand(op, opType, tmpReg);
1085 if (minstr2 != NULL)
1086 loadConstVec[numNew++] = minstr2;
1087 }
1088 else
1089 minstr->SetMachineOperand(op, opType, immedValue);
1090 }
1091 }
1092 }
1093
1094 if (numNew > 0)
1095 {
1096 // Insert the new instructions *before* the old ones by moving
1097 // the old ones over `numNew' positions (last-to-first, of course!).
1098 // We do check *after* returning that we did not exceed the vector mvec.
1099 for (int i=numInstr-1; i >= 0; i--)
1100 mvec[i+numNew] = mvec[i];
1101
1102 for (unsigned i=0; i < numNew; i++)
1103 mvec[i] = loadConstVec[i];
1104 }
1105
1106 return (numInstr + numNew);
1107}
1108
1109
1110static inline MachineInstr*
1111MakeIntSetInstruction(int64_t C, bool isSigned, Value* dest)
1112{
1113 MachineInstr* minstr;
1114 if (isSigned)
1115 {
1116 minstr = new MachineInstr(SETSW);
1117 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
1118 }
1119 else
1120 {
1121 minstr = new MachineInstr(SETUW);
1122 minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed, C);
1123 }
1124
1125 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
1126
1127 return minstr;
1128}
1129
1130
1131static MachineInstr*
1132MakeLoadConstInstr(Instruction* vmInstr,
1133 Value* val,
1134 TmpInstruction*& tmpReg,
1135 MachineInstr*& getMinstr2)
1136{
1137 assert(val->getValueType() == Value::ConstantVal);
1138
1139 MachineInstr* minstr;
1140
1141 getMinstr2 = NULL;
1142
1143 // Create a TmpInstruction to mark the hidden register used for the constant
1144 tmpReg = new TmpInstruction(Instruction::UserOp1, val, NULL);
1145 vmInstr->getMachineInstrVec().addTempValue(tmpReg);
1146
1147 // Use a "set" instruction for known constants that can go in an integer reg.
1148 // Use a "set" instruction followed by a int-to-float conversion for known
1149 // constants that must go in a floating point reg but have an integer value.
1150 // Use a "load" instruction for all other constants, in particular,
1151 // floating point constants.
1152 //
1153 const Type* valType = val->getType();
1154
1155 if (valType->isIntegral() ||
1156 valType->isPointerType() ||
1157 valType == Type::BoolTy)
1158 {
1159 bool isValidConstant;
1160 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
1161 assert(isValidConstant && "Unrecognized constant");
1162
1163 minstr = MakeIntSetInstruction(C, valType->isSigned(), tmpReg);
1164 }
1165 else
1166 {
1167 assert(valType == Type::FloatTy || valType == Type::DoubleTy);
1168 double dval = ((ConstPoolFP*) val)->getValue();
1169 if (dval == (int64_t) dval)
1170 {
1171 // The constant actually has an integer value, so use a
1172 // [set; int-to-float] sequence instead of a load instruction.
1173 //
1174 TmpInstruction* tmpReg2 = NULL;
1175 if (dval != 0.0)
1176 { // First, create an integer constant of the same value as dval
Chris Lattner1fa0c092001-09-07 21:22:57 +00001177 ConstPoolSInt* ival = ConstPoolSInt::get(Type::IntTy,
1178 (int64_t) dval);
Vikram S. Adved4228a52001-08-28 23:12:57 +00001179 // Create another TmpInstruction for the hidden integer register
1180 TmpInstruction* tmpReg2 =
1181 new TmpInstruction(Instruction::UserOp1, ival, NULL);
1182 vmInstr->getMachineInstrVec().addTempValue(tmpReg2);
1183
1184 // Create the `SET' instruction
1185 minstr = MakeIntSetInstruction((int64_t)dval, true, tmpReg2);
1186 }
1187
1188 // In which variable do we put the second instruction?
1189 MachineInstr*& instr2 = (minstr)? getMinstr2 : minstr;
1190
1191 // Create the int-to-float instruction
1192 instr2 = new MachineInstr(valType == Type::FloatTy? FITOS : FITOD);
1193
1194 if (dval == 0.0)
1195 instr2->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
1196 else
1197 instr2->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
1198 tmpReg2);
1199
1200 instr2->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1201 tmpReg);
1202 }
1203 else
1204 {
1205 // Make a Load instruction, and make `val' both the ptr value *and*
1206 // the result value, and set the offset field to 0. Final code
1207 // generation will have to generate the base+offset for the constant.
1208 //
1209 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
1210 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
1211 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,val);
1212 minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1213 zeroOffset);
1214 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1215 tmpReg);
1216 }
1217 }
1218
1219 tmpReg->addMachineInstruction(minstr);
1220
1221 assert(minstr);
1222 return minstr;
1223}
1224
1225//
1226// Substitute operand `operandNum' of the instruction in node `treeNode'
1227// in place the use(s) of that instruction in node `parent'.
1228//
1229static void
1230ForwardOperand(InstructionNode* treeNode,
1231 InstructionNode* parent,
1232 int operandNum)
1233{
1234 Instruction* unusedOp = treeNode->getInstruction();
1235 Value* fwdOp = unusedOp->getOperand(operandNum);
1236 Instruction* userInstr = parent->getInstruction();
1237 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
1238 for (unsigned i=0, N=mvec.size(); i < N; i++)
1239 {
1240 MachineInstr* minstr = mvec[i];
1241 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; i++)
1242 {
1243 const MachineOperand& mop = minstr->getOperand(i);
1244 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1245 mop.getVRegValue() == unusedOp)
1246 {
1247 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
1248 fwdOp);
1249 }
1250 }
1251 }
1252}
1253
1254
1255// This function is currently unused and incomplete but will be
1256// used if we have a linear layout of basic blocks in LLVM code.
1257// It decides which branch should fall-through, and whether an
1258// extra unconditional branch is needed (when neither falls through).
1259//
1260void
1261ChooseBranchPattern(Instruction* vmInstr, BranchPattern& brPattern)
1262{
1263 BranchInst* brInstr = (BranchInst*) vmInstr;
1264
1265 brPattern.flipCondition = false;
1266 brPattern.targetBB = brInstr->getSuccessor(0);
1267 brPattern.extraBranch = NULL;
1268
1269 assert(brInstr->getNumSuccessors() > 1 &&
1270 "Unnecessary analysis for unconditional branch");
1271
1272 assert(0 && "Fold branches in peephole optimization");
1273}
1274
1275
1276//******************* Externally Visible Functions *************************/
1277
1278
Vikram S. Advea21cf202001-07-21 12:42:19 +00001279//------------------------------------------------------------------------
1280// External Function: GetInstructionsByRule
1281//
1282// Purpose:
1283// Choose machine instructions for the SPARC according to the
1284// patterns chosen by the BURG-generated parser.
1285//------------------------------------------------------------------------
1286
1287unsigned
1288GetInstructionsByRule(InstructionNode* subtreeRoot,
1289 int ruleForNode,
1290 short* nts,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001291 TargetMachine &target,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001292 MachineInstr** mvec)
1293{
1294 int numInstr = 1; // initialize for common case
1295 bool checkCast = false; // initialize here to use fall-through
1296 Value *leftVal, *rightVal;
1297 const Type* opType;
1298 int nextRule;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001299 int forwardOperandNum = -1;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001300 BranchPattern brPattern;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001301 int64_t s0 = 0; // variables holding zero to avoid
1302 uint64_t u0 = 0; // overloading ambiguities below
Vikram S. Advea21cf202001-07-21 12:42:19 +00001303
1304 mvec[0] = mvec[1] = mvec[2] = mvec[3] = NULL; // just for safety
1305
1306 switch(ruleForNode) {
1307 case 1: // stmt: Ret
1308 case 2: // stmt: RetValue(reg)
1309 // NOTE: Prepass of register allocation is responsible
1310 // for moving return value to appropriate register.
1311 // Mark the return-address register as a hidden virtual reg.
1312 {
1313 Instruction* returnReg = new TmpInstruction(Instruction::UserOp1,
1314 subtreeRoot->getInstruction(), NULL);
1315 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(returnReg);
1316
1317 mvec[0] = new MachineInstr(RETURN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001318 mvec[0]->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,returnReg);
1319 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1320
1321 returnReg->addMachineInstruction(mvec[0]);
1322
Vikram S. Advea21cf202001-07-21 12:42:19 +00001323 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1324 break;
1325 }
1326
1327 case 3: // stmt: Store(reg,reg)
1328 case 4: // stmt: Store(reg,ptrreg)
1329 mvec[0] = new MachineInstr(ChooseStoreInstruction(subtreeRoot->leftChild()->getValue()->getType()));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001330 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001331 break;
1332
1333 case 5: // stmt: BrUncond
1334 mvec[0] = new MachineInstr(BA);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001335 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, (Value*)NULL);
1336 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001337 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1338
Vikram S. Adved4228a52001-08-28 23:12:57 +00001339 // delay slot
1340 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001341 break;
1342
1343 case 6: // stmt: BrCond(boolconst)
1344 // boolconst => boolean was computed with `%b = setCC type reg1 constant'
1345 // If the constant is ZERO, we can use the branch-on-integer-register
1346 // instructions and avoid the SUBcc instruction entirely.
1347 // Otherwise this is just the same as case 5, so just fall through.
1348 {
1349 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1350 assert(constNode && constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1351 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
Vikram S. Adve4f231662001-07-28 04:15:15 +00001352 bool isValidConst;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001353
1354 if (constVal->getType()->isIntegral()
Vikram S. Adve4f231662001-07-28 04:15:15 +00001355 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1356 && isValidConst)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001357 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001358 // That constant ia a zero after all...
Vikram S. Advea21cf202001-07-21 12:42:19 +00001359 // Use the left child of the setCC instruction as the first argument!
1360 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001361 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001362 subtreeRoot->leftChild()->leftChild()->getValue());
1363 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1364 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1365
Vikram S. Adve4f231662001-07-28 04:15:15 +00001366 // delay slot
1367 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001368
Vikram S. Adve4f231662001-07-28 04:15:15 +00001369 // false branch
1370 mvec[numInstr++] = new MachineInstr(BA);
1371 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1372 (Value*) NULL);
1373 mvec[numInstr-1]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adved4228a52001-08-28 23:12:57 +00001374
1375 // delay slot
1376 mvec[numInstr++] = new MachineInstr(NOP);
1377
Vikram S. Advea21cf202001-07-21 12:42:19 +00001378 break;
1379 }
1380 // ELSE FALL THROUGH
1381 }
1382
1383 case 7: // stmt: BrCond(bool)
1384 // bool => boolean was computed with `%b = setcc type reg1 reg2'
1385 // Need to check whether the type was a FP, signed int or unsigned int,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001386 // and check the branching condition in order to choose the branch to use.
Vikram S. Advea21cf202001-07-21 12:42:19 +00001387 //
1388 {
1389 bool isFPBranch;
1390 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001391 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1392 subtreeRoot->leftChild()->getValue());
1393 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001394 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1395
Vikram S. Adve4f231662001-07-28 04:15:15 +00001396 // delay slot
1397 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001398
Vikram S. Adve4f231662001-07-28 04:15:15 +00001399 // false branch
1400 mvec[numInstr++] = new MachineInstr(BA);
1401 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1402 (Value*) NULL);
1403 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1404 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adved4228a52001-08-28 23:12:57 +00001405
1406 // delay slot
1407 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001408 break;
1409 }
Vikram S. Adve4f231662001-07-28 04:15:15 +00001410
Vikram S. Advea21cf202001-07-21 12:42:19 +00001411 case 8: // stmt: BrCond(boolreg)
1412 // bool => boolean is stored in an existing register.
1413 // Just use the branch-on-integer-register instruction!
1414 //
1415 mvec[0] = new MachineInstr(BRNZ);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001416 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1417 subtreeRoot->leftChild()->getValue());
Vikram S. Advea21cf202001-07-21 12:42:19 +00001418 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1419 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adved4228a52001-08-28 23:12:57 +00001420
1421 // delay slot
Vikram S. Advea21cf202001-07-21 12:42:19 +00001422 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
Vikram S. Adved4228a52001-08-28 23:12:57 +00001423
1424 // false branch
1425 mvec[numInstr++] = new MachineInstr(BA);
1426 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1427 (Value*) NULL);
1428 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1429 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1430
1431 // delay slot
1432 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001433 break;
1434
1435 case 9: // stmt: Switch(reg)
1436 assert(0 && "*** SWITCH instruction is not implemented yet.");
1437 numInstr = 0;
1438 break;
1439
1440 case 10: // reg: VRegList(reg, reg)
1441 assert(0 && "VRegList should never be the topmost non-chain rule");
1442 break;
1443
1444 case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0
1445 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001446 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001447 subtreeRoot->leftChild()->getValue());
1448 mvec[0]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001449 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001450 subtreeRoot->getValue());
1451 break;
1452
1453 case 22: // reg: ToBoolTy(reg):
1454 opType = subtreeRoot->leftChild()->getValue()->getType();
1455 assert(opType->isIntegral() || opType == Type::BoolTy);
1456 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001457 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001458 break;
1459
1460 case 23: // reg: ToUByteTy(reg)
1461 case 25: // reg: ToUShortTy(reg)
1462 case 27: // reg: ToUIntTy(reg)
1463 case 29: // reg: ToULongTy(reg)
1464 opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001465 assert(opType->isIntegral() ||
1466 opType->isPointerType() ||
1467 opType == Type::BoolTy && "Ignoring cast: illegal for other types");
Vikram S. Advea21cf202001-07-21 12:42:19 +00001468 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001469 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001470 break;
1471
1472 case 24: // reg: ToSByteTy(reg)
1473 case 26: // reg: ToShortTy(reg)
1474 case 28: // reg: ToIntTy(reg)
1475 case 30: // reg: ToLongTy(reg)
1476 opType = subtreeRoot->leftChild()->getValue()->getType();
1477 if (opType->isIntegral() || opType == Type::BoolTy)
Vikram S. Adve4f231662001-07-28 04:15:15 +00001478 {
1479 numInstr = 0;
1480 forwardOperandNum = 0;
1481 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001482 else
1483 {
1484 mvec[0] =new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,opType));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001485 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001486 }
1487 break;
1488
1489 case 31: // reg: ToFloatTy(reg):
1490 case 32: // reg: ToDoubleTy(reg):
1491
1492 // If this instruction has a parent (a user) in the tree
1493 // and the user is translated as an FsMULd instruction,
1494 // then the cast is unnecessary. So check that first.
1495 // In the future, we'll want to do the same for the FdMULq instruction,
1496 // so do the check here instead of only for ToFloatTy(reg).
1497 //
1498 if (subtreeRoot->parent() != NULL &&
1499 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1500 {
1501 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001502 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001503 }
1504 else
1505 {
1506 opType = subtreeRoot->leftChild()->getValue()->getType();
1507 mvec[0] = new MachineInstr(ChooseConvertToFloatInstr(subtreeRoot, opType));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001508 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001509 }
1510 break;
1511
1512 case 19: // reg: ToArrayTy(reg):
1513 case 20: // reg: ToPointerTy(reg):
1514 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001515 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001516 break;
1517
Vikram S. Adved4228a52001-08-28 23:12:57 +00001518 case 233: // reg: Add(reg, Constant)
1519 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1520 if (mvec[0] != NULL)
1521 break;
1522 // ELSE FALL THROUGH
1523
Vikram S. Advea21cf202001-07-21 12:42:19 +00001524 case 33: // reg: Add(reg, reg)
1525 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001526 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001527 break;
1528
Vikram S. Adved4228a52001-08-28 23:12:57 +00001529 case 234: // reg: Sub(reg, Constant)
1530 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1531 if (mvec[0] != NULL)
1532 break;
1533 // ELSE FALL THROUGH
1534
Vikram S. Advea21cf202001-07-21 12:42:19 +00001535 case 34: // reg: Sub(reg, reg)
1536 mvec[0] = new MachineInstr(ChooseSubInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001537 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001538 break;
1539
1540 case 135: // reg: Mul(todouble, todouble)
1541 checkCast = true;
1542 // FALL THROUGH
1543
1544 case 35: // reg: Mul(reg, reg)
1545 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001546 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001547 break;
Vikram S. Adved4228a52001-08-28 23:12:57 +00001548
1549 case 335: // reg: Mul(todouble, todoubleConst)
1550 checkCast = true;
1551 // FALL THROUGH
1552
1553 case 235: // reg: Mul(reg, Constant)
1554 mvec[0] = CreateMulConstInstruction(subtreeRoot, mvec[1]);
1555 if (mvec[0] == NULL)
1556 {
1557 mvec[0]=new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
1558 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1559 }
1560 else
1561 if (mvec[1] != NULL)
1562 ++numInstr;
1563 break;
1564
1565 case 236: // reg: Div(reg, Constant)
1566 mvec[0] = CreateDivConstInstruction(subtreeRoot, mvec[1]);
1567 if (mvec[0] != NULL)
1568 {
1569 if (mvec[1] != NULL)
1570 ++numInstr;
1571 }
1572 else
1573 // ELSE FALL THROUGH
Vikram S. Advea21cf202001-07-21 12:42:19 +00001574
1575 case 36: // reg: Div(reg, reg)
1576 mvec[0] = new MachineInstr(ChooseDivInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001577 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001578 break;
1579
Vikram S. Adved4228a52001-08-28 23:12:57 +00001580 case 37: // reg: Rem(reg, reg)
1581 case 237: // reg: Rem(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001582 assert(0 && "REM instruction unimplemented for the SPARC.");
1583 break;
1584
Vikram S. Adved4228a52001-08-28 23:12:57 +00001585 case 38: // reg: And(reg, reg)
1586 case 238: // reg: And(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001587 mvec[0] = new MachineInstr(AND);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001588 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001589 break;
1590
1591 case 138: // reg: And(reg, not)
1592 mvec[0] = new MachineInstr(ANDN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001593 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001594 break;
1595
Vikram S. Adved4228a52001-08-28 23:12:57 +00001596 case 39: // reg: Or(reg, reg)
1597 case 239: // reg: Or(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001598 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001599 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001600 break;
1601
1602 case 139: // reg: Or(reg, not)
1603 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001604 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001605 break;
1606
Vikram S. Adved4228a52001-08-28 23:12:57 +00001607 case 40: // reg: Xor(reg, reg)
1608 case 240: // reg: Xor(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001609 mvec[0] = new MachineInstr(XOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001610 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001611 break;
1612
1613 case 140: // reg: Xor(reg, not)
1614 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001615 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001616 break;
1617
1618 case 41: // boolconst: SetCC(reg, Constant)
1619 // Check if this is an integer comparison, and
1620 // there is a parent, and the parent decided to use
1621 // a branch-on-integer-register instead of branch-on-condition-code.
1622 // If so, the SUBcc instruction is not required.
1623 // (However, we must still check for constants to be loaded from
1624 // the constant pool so that such a load can be associated with
1625 // this instruction.)
1626 //
Vikram S. Adve4f231662001-07-28 04:15:15 +00001627 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Advea21cf202001-07-21 12:42:19 +00001628 //
1629 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() &&
1630 subtreeRoot->parent() != NULL)
1631 {
1632 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1633 assert(parentNode->getNodeType() == InstrTreeNode::NTInstructionNode);
1634 const vector<MachineInstr*>&
1635 minstrVec = parentNode->getInstruction()->getMachineInstrVec();
1636 MachineOpCode parentOpCode;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001637 if (parentNode->getInstruction()->getOpcode() == Instruction::Br &&
Vikram S. Advea21cf202001-07-21 12:42:19 +00001638 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1639 parentOpCode <= BRGEZ)
1640 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001641 numInstr = 0; // don't forward the operand!
Vikram S. Advea21cf202001-07-21 12:42:19 +00001642 break;
1643 }
1644 }
1645 // ELSE FALL THROUGH
1646
1647 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4f231662001-07-28 04:15:15 +00001648 {
1649 // If result of the SetCC is only used for a branch, we can
1650 // discard the result. otherwise, it must go into an integer register.
1651 // Note that the user may or may not be in the same tree, so we have
1652 // to follow SSA def-use edges here, not BURG tree edges.
1653 //
1654 Instruction* result = subtreeRoot->getInstruction();
1655 Value* firstUse = (Value*) * result->use_begin();
1656 bool discardResult =
1657 (result->use_size() == 1
1658 && firstUse->getValueType() == Value::InstructionVal
1659 && ((Instruction*) firstUse)->getOpcode() == Instruction::Br);
1660
1661 bool mustClearReg;
1662 int valueToMove;
1663 MachineOpCode movOpCode;
1664
Vikram S. Advea21cf202001-07-21 12:42:19 +00001665 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral())
1666 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001667 // integer condition: destination should be %g0 or integer register
1668 // if result must be saved but condition is not SetEQ then we need
1669 // a separate instruction to compute the bool result, so discard
1670 // result of SUBcc instruction anyway.
1671 //
Vikram S. Advea21cf202001-07-21 12:42:19 +00001672 mvec[0] = new MachineInstr(SUBcc);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001673 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, discardResult);
1674
Vikram S. Adve98a9c972001-08-06 21:06:10 +00001675 // mark the 4th operand as being a CC register, and a "result"
1676 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
1677 subtreeRoot->getValue(), /*def*/ true);
1678
1679 if (!discardResult)
Vikram S. Adve4f231662001-07-28 04:15:15 +00001680 { // recompute bool if needed, using the integer condition codes
1681 if (result->getOpcode() == Instruction::SetNE)
1682 discardResult = true;
1683 else
1684 movOpCode =
1685 ChooseMovpccAfterSub(subtreeRoot, mustClearReg, valueToMove);
1686 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001687 }
1688 else
1689 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001690 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Advea21cf202001-07-21 12:42:19 +00001691 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
Vikram S. Advea21cf202001-07-21 12:42:19 +00001692 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001693 subtreeRoot->getValue());
1694 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1695 subtreeRoot->leftChild()->getValue());
1696 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1697 subtreeRoot->rightChild()->getValue());
1698
1699 if (!discardResult)
1700 {// recompute bool using the FP condition codes
1701 mustClearReg = true;
1702 valueToMove = 1;
1703 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1704 }
1705 }
1706
1707 if (!discardResult)
1708 {
1709 if (mustClearReg)
1710 {// Unconditionally set register to 0
1711 int n = numInstr++;
1712 mvec[n] = new MachineInstr(SETHI);
1713 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,s0);
1714 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1715 subtreeRoot->getValue());
1716 }
1717
1718 // Now conditionally move `valueToMove' (0 or 1) into the register
1719 int n = numInstr++;
1720 mvec[n] = new MachineInstr(movOpCode);
1721 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1722 subtreeRoot->getValue());
1723 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1724 valueToMove);
1725 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1726 subtreeRoot->getValue());
Vikram S. Advea21cf202001-07-21 12:42:19 +00001727 }
1728 break;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001729 }
1730
Vikram S. Advea21cf202001-07-21 12:42:19 +00001731 case 43: // boolreg: VReg
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001732 case 44: // boolreg: Constant
Vikram S. Advea21cf202001-07-21 12:42:19 +00001733 numInstr = 0;
1734 break;
1735
1736 case 51: // reg: Load(reg)
1737 case 52: // reg: Load(ptrreg)
1738 case 53: // reg: LoadIdx(reg,reg)
1739 case 54: // reg: LoadIdx(ptrreg,reg)
1740 mvec[0] = new MachineInstr(ChooseLoadInstruction(subtreeRoot->getValue()->getType()));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001741 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001742 break;
1743
1744 case 55: // reg: GetElemPtr(reg)
1745 case 56: // reg: GetElemPtrIdx(reg,reg)
1746 if (subtreeRoot->parent() != NULL)
1747 {
1748 // Check if the parent was an array access.
1749 // If so, we still need to generate this instruction.
1750 MemAccessInst* memInst =(MemAccessInst*) subtreeRoot->getInstruction();
1751 const PointerType* ptrType =
1752 (const PointerType*) memInst->getPtrOperand()->getType();
1753 if (! ptrType->getValueType()->isArrayType())
1754 {// we don't need a separate instr
Vikram S. Adve4f231662001-07-28 04:15:15 +00001755 numInstr = 0; // don't forward operand!
Vikram S. Advea21cf202001-07-21 12:42:19 +00001756 break;
1757 }
1758 }
1759 // else in all other cases we need to a separate ADD instruction
1760 mvec[0] = new MachineInstr(ADD);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001761 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001762 break;
1763
1764 case 57: // reg: Alloca: Implement as 2 instructions:
1765 // sub %sp, tmp -> %sp
1766 { // add %sp, 0 -> result
1767 Instruction* instr = subtreeRoot->getInstruction();
1768 const PointerType* instrType = (const PointerType*) instr->getType();
1769 assert(instrType->isPointerType());
Vikram S. Adve4f231662001-07-28 04:15:15 +00001770 int tsize = (int) target.findOptimalStorageSize(instrType->getValueType());
1771 assert(tsize != 0 && "Just to check when this can happen");
1772 // if (tsize == 0)
1773 // {
1774 // numInstr = 0;
1775 // break;
1776 // }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001777 //else go on to create the instructions needed...
1778
1779 // Create a temporary Value to hold the constant type-size
Chris Lattner1fa0c092001-09-07 21:22:57 +00001780 ConstPoolSInt* valueForTSize = ConstPoolSInt::get(Type::IntTy, tsize);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001781
1782 // Instruction 1: sub %sp, tsize -> %sp
1783 // tsize is always constant, but it may have to be put into a
1784 // register if it doesn't fit in the immediate field.
1785 //
1786 mvec[0] = new MachineInstr(SUB);
1787 mvec[0]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001788 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001789 mvec[0]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1790
1791 // Instruction 2: add %sp, 0 -> result
1792 numInstr++;
1793 mvec[1] = new MachineInstr(ADD);
1794 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1795 mvec[1]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001796 mvec[1]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001797 break;
1798 }
1799
1800 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1801 // mul num, typeSz -> tmp
1802 // sub %sp, tmp -> %sp
1803 { // add %sp, 0 -> result
1804 Instruction* instr = subtreeRoot->getInstruction();
1805 const PointerType* instrType = (const PointerType*) instr->getType();
1806 assert(instrType->isPointerType() &&
1807 instrType->getValueType()->isArrayType());
1808 const Type* eltType =
1809 ((ArrayType*) instrType->getValueType())->getElementType();
Vikram S. Adve4f231662001-07-28 04:15:15 +00001810 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001811
Vikram S. Adve4f231662001-07-28 04:15:15 +00001812 assert(tsize != 0 && "Just to check when this can happen");
1813 // if (tsize == 0)
1814 // {
1815 // numInstr = 0;
1816 // break;
1817 // }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001818 //else go on to create the instructions needed...
1819
1820 // Create a temporary Value to hold the constant type-size
Chris Lattner1fa0c092001-09-07 21:22:57 +00001821 ConstPoolSInt* valueForTSize = ConstPoolSInt::get(Type::IntTy, tsize);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001822
1823 // Create a temporary value to hold `tmp'
1824 Instruction* tmpInstr = new TmpInstruction(Instruction::UserOp1,
1825 subtreeRoot->leftChild()->getValue(),
1826 NULL /*could insert tsize here*/);
1827 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1828
1829 // Instruction 1: mul numElements, typeSize -> tmp
1830 mvec[0] = new MachineInstr(MULX);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001831 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001832 subtreeRoot->leftChild()->getValue());
Vikram S. Adve4f231662001-07-28 04:15:15 +00001833 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
1834 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,tmpInstr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001835
Vikram S. Adve4f231662001-07-28 04:15:15 +00001836 tmpInstr->addMachineInstruction(mvec[0]);
1837
Vikram S. Advea21cf202001-07-21 12:42:19 +00001838 // Instruction 2: sub %sp, tmp -> %sp
1839 numInstr++;
1840 mvec[1] = new MachineInstr(SUB);
1841 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001842 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,tmpInstr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001843 mvec[1]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1844
1845 // Instruction 3: add %sp, 0 -> result
1846 numInstr++;
1847 mvec[2] = new MachineInstr(ADD);
1848 mvec[2]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1849 mvec[2]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001850 mvec[2]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001851 break;
1852 }
1853
1854 case 61: // reg: Call
1855 // Generate a call-indirect (i.e., JMPL) for now to expose
1856 // the potential need for registers. If an absolute address
1857 // is available, replace this with a CALL instruction.
1858 // Mark both the indirection register and the return-address
1859 { // register as hidden virtual registers.
1860
Vikram S. Adve4f231662001-07-28 04:15:15 +00001861 Instruction* jmpAddrReg = new TmpInstruction(Instruction::UserOp1,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001862 ((CallInst*) subtreeRoot->getInstruction())->getCalledMethod(), NULL);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001863 Instruction* retAddrReg = new TmpInstruction(Instruction::UserOp1,
1864 subtreeRoot->getValue(), NULL);
1865 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(jmpAddrReg);
1866 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(retAddrReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001867
1868 mvec[0] = new MachineInstr(JMPL);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001869 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, jmpAddrReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001870 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1871 (int64_t) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001872 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, retAddrReg);
1873
1874 // NOTE: jmpAddrReg will be loaded by a different instruction generated
1875 // by the final code generator, so we just mark the CALL instruction
1876 // as computing that value.
1877 // The retAddrReg is actually computed by the CALL instruction.
1878 //
1879 jmpAddrReg->addMachineInstruction(mvec[0]);
1880 retAddrReg->addMachineInstruction(mvec[0]);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001881
1882 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1883 break;
1884 }
1885
1886 case 62: // reg: Shl(reg, reg)
1887 opType = subtreeRoot->leftChild()->getValue()->getType();
1888 assert(opType->isIntegral() || opType == Type::BoolTy);
1889 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001890 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001891 break;
1892
1893 case 63: // reg: Shr(reg, reg)
1894 opType = subtreeRoot->leftChild()->getValue()->getType();
1895 assert(opType->isIntegral() || opType == Type::BoolTy);
1896 mvec[0] = new MachineInstr((opType->isSigned()
1897 ? ((opType == Type::LongTy)? SRAX : SRA)
1898 : ((opType == Type::LongTy)? SRLX : SRL)));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001899 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001900 break;
1901
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001902 case 64: // reg: Phi(reg,reg)
1903 { // This instruction has variable #operands, so resultPos is 0.
1904 Instruction* phi = subtreeRoot->getInstruction();
1905 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
1906 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adved4228a52001-08-28 23:12:57 +00001907 subtreeRoot->getValue());
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001908 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
1909 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
1910 phi->getOperand(i));
1911 break;
1912 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001913 case 71: // reg: VReg
1914 case 72: // reg: Constant
Vikram S. Adve4f231662001-07-28 04:15:15 +00001915 numInstr = 0; // don't forward the value
Vikram S. Advea21cf202001-07-21 12:42:19 +00001916 break;
1917
1918 case 111: // stmt: reg
1919 case 112: // stmt: boolconst
1920 case 113: // stmt: bool
1921 case 121:
1922 case 122:
1923 case 123:
1924 case 124:
1925 case 125:
1926 case 126:
1927 case 127:
1928 case 128:
1929 case 129:
1930 case 130:
1931 case 131:
1932 case 132:
1933 case 153:
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001934 case 155:
Vikram S. Advea21cf202001-07-21 12:42:19 +00001935 //
1936 // These are all chain rules, which have a single nonterminal on the RHS.
1937 // Get the rule that matches the RHS non-terminal and use that instead.
1938 //
1939 assert(ThisIsAChainRule(ruleForNode));
1940 assert(nts[0] && ! nts[1]
1941 && "A chain rule should have only one RHS non-terminal!");
1942 nextRule = burm_rule(subtreeRoot->getBasicNode()->state, nts[0]);
1943 nts = burm_nts[nextRule];
Vikram S. Adve4f231662001-07-28 04:15:15 +00001944 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001945 break;
1946
1947 default:
Vikram S. Adve4f231662001-07-28 04:15:15 +00001948 assert(0 && "Unrecognized BURG rule");
Vikram S. Advea21cf202001-07-21 12:42:19 +00001949 numInstr = 0;
1950 break;
1951 }
1952
Vikram S. Adve4f231662001-07-28 04:15:15 +00001953 if (forwardOperandNum >= 0)
1954 { // We did not generate a machine instruction but need to use operand.
1955 // If user is in the same tree, replace Value in its machine operand.
1956 // If not, insert a copy instruction which should get coalesced away
1957 // by register allocation.
1958 if (subtreeRoot->parent() != NULL)
1959 ForwardOperand(subtreeRoot, (InstructionNode*) subtreeRoot->parent(),
1960 forwardOperandNum);
1961 else
1962 {
1963 int n = numInstr++;
1964 mvec[n] = new MachineInstr(ADD);
1965 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1966 subtreeRoot->getInstruction()->getOperand(forwardOperandNum));
1967 mvec[n]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
1968 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1969 subtreeRoot->getInstruction());
1970 }
1971 }
1972
1973 if (! ThisIsAChainRule(ruleForNode))
1974 numInstr = FixConstantOperands(subtreeRoot, mvec, numInstr, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001975
1976 return numInstr;
1977}
1978
1979