blob: 8f79f6674d230a3f3abcae788c96dfce10e65530 [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;
Vikram S. Adve9856e0c2001-09-09 20:35:34 +0000328 else if (opType == Type::FloatTy)
329 ;
Vikram S. Adved4228a52001-08-28 23:12:57 +0000330 else
Vikram S. Adve9856e0c2001-09-09 20:35:34 +0000331 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Vikram S. Adved4228a52001-08-28 23:12:57 +0000332 break;
333
334 case ToDoubleTy:
335 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
336 opCode = FITOD;
337 else if (opType == Type::LongTy)
338 opCode = FXTOD;
339 else if (opType == Type::FloatTy)
340 opCode = FSTOD;
Vikram S. Adve9856e0c2001-09-09 20:35:34 +0000341 else if (opType == Type::DoubleTy)
342 ;
Vikram S. Adved4228a52001-08-28 23:12:57 +0000343 else
Vikram S. Adve9856e0c2001-09-09 20:35:34 +0000344 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Vikram S. Adved4228a52001-08-28 23:12:57 +0000345 break;
346
347 default:
348 break;
349 }
350
351 return opCode;
352}
353
354static inline MachineOpCode
355ChooseConvertToIntInstr(const InstructionNode* instrNode,
356 const Type* opType)
357{
358 MachineOpCode opCode = INVALID_OPCODE;;
359
360 int instrType = (int) instrNode->getOpLabel();
361
362 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
363 {
364 switch (opType->getPrimitiveID())
365 {
366 case Type::FloatTyID: opCode = FSTOI; break;
367 case Type::DoubleTyID: opCode = FDTOI; break;
368 default:
369 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
370 break;
371 }
372 }
373 else if (instrType == ToLongTy)
374 {
375 switch (opType->getPrimitiveID())
376 {
377 case Type::FloatTyID: opCode = FSTOX; break;
378 case Type::DoubleTyID: opCode = FDTOX; break;
379 default:
380 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
381 break;
382 }
383 }
384 else
385 assert(0 && "Should not get here, Mo!");
386
387 return opCode;
388}
389
390
391static inline MachineOpCode
392ChooseAddInstruction(const InstructionNode* instrNode)
393{
394 MachineOpCode opCode = INVALID_OPCODE;
395
396 const Type* resultType = instrNode->getInstruction()->getType();
397
398 if (resultType->isIntegral() ||
399 resultType->isPointerType() ||
400 resultType->isMethodType() ||
401 resultType->isLabelType())
402 {
403 opCode = ADD;
404 }
405 else
406 {
407 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
408 switch(operand->getType()->getPrimitiveID())
409 {
410 case Type::FloatTyID: opCode = FADDS; break;
411 case Type::DoubleTyID: opCode = FADDD; break;
412 default: assert(0 && "Invalid type for ADD instruction"); break;
413 }
414 }
415
416 return opCode;
417}
418
419
420static inline MachineInstr*
421CreateMovFloatInstruction(const InstructionNode* instrNode,
422 const Type* resultType)
423{
424 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
425 ? FMOVS : FMOVD);
426 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
427 instrNode->leftChild()->getValue());
428 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
429 instrNode->getValue());
430 return minstr;
431}
432
433static inline MachineInstr*
434CreateAddConstInstruction(const InstructionNode* instrNode)
435{
436 MachineInstr* minstr = NULL;
437
438 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
439 assert(constOp->getValueType() == Value::ConstantVal);
440
441 // Cases worth optimizing are:
442 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
443 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
444 //
445 const Type* resultType = instrNode->getInstruction()->getType();
446
447 if (resultType == Type::FloatTy ||
448 resultType == Type::DoubleTy)
449 {
450 double dval = ((ConstPoolFP*) constOp)->getValue();
451 if (dval == 0.0)
452 minstr = CreateMovFloatInstruction(instrNode, resultType);
453 }
454
455 return minstr;
456}
457
458
459static inline MachineOpCode
460ChooseSubInstruction(const InstructionNode* instrNode)
461{
462 MachineOpCode opCode = INVALID_OPCODE;
463
464 const Type* resultType = instrNode->getInstruction()->getType();
465
466 if (resultType->isIntegral() ||
467 resultType->isPointerType())
468 {
469 opCode = SUB;
470 }
471 else
472 {
473 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
474 switch(operand->getType()->getPrimitiveID())
475 {
476 case Type::FloatTyID: opCode = FSUBS; break;
477 case Type::DoubleTyID: opCode = FSUBD; break;
478 default: assert(0 && "Invalid type for SUB instruction"); break;
479 }
480 }
481
482 return opCode;
483}
484
485
486static inline MachineInstr*
487CreateSubConstInstruction(const InstructionNode* instrNode)
488{
489 MachineInstr* minstr = NULL;
490
491 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
492 assert(constOp->getValueType() == Value::ConstantVal);
493
494 // Cases worth optimizing are:
495 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
496 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
497 //
498 const Type* resultType = instrNode->getInstruction()->getType();
499
500 if (resultType == Type::FloatTy ||
501 resultType == Type::DoubleTy)
502 {
503 double dval = ((ConstPoolFP*) constOp)->getValue();
504 if (dval == 0.0)
505 minstr = CreateMovFloatInstruction(instrNode, resultType);
506 }
507
508 return minstr;
509}
510
511
512static inline MachineOpCode
513ChooseFcmpInstruction(const InstructionNode* instrNode)
514{
515 MachineOpCode opCode = INVALID_OPCODE;
516
517 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
518 switch(operand->getType()->getPrimitiveID())
519 {
520 case Type::FloatTyID: opCode = FCMPS; break;
521 case Type::DoubleTyID: opCode = FCMPD; break;
522 default: assert(0 && "Invalid type for FCMP instruction"); break;
523 }
524
525 return opCode;
526}
527
528
529// Assumes that leftArg and rightArg are both cast instructions.
530//
531static inline bool
532BothFloatToDouble(const InstructionNode* instrNode)
533{
534 InstrTreeNode* leftArg = instrNode->leftChild();
535 InstrTreeNode* rightArg = instrNode->rightChild();
536 InstrTreeNode* leftArgArg = leftArg->leftChild();
537 InstrTreeNode* rightArgArg = rightArg->leftChild();
538 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
539
540 // Check if both arguments are floats cast to double
541 return (leftArg->getValue()->getType() == Type::DoubleTy &&
542 leftArgArg->getValue()->getType() == Type::FloatTy &&
543 rightArgArg->getValue()->getType() == Type::FloatTy);
544}
545
546
547static inline MachineOpCode
548ChooseMulInstruction(const InstructionNode* instrNode,
549 bool checkCasts)
550{
551 MachineOpCode opCode = INVALID_OPCODE;
552
553 if (checkCasts && BothFloatToDouble(instrNode))
554 {
555 return opCode = FSMULD;
556 }
557 // else fall through and use the regular multiply instructions
558
559 const Type* resultType = instrNode->getInstruction()->getType();
560
561 if (resultType->isIntegral())
562 {
563 opCode = MULX;
564 }
565 else
566 {
567 switch(instrNode->leftChild()->getValue()->getType()->getPrimitiveID())
568 {
569 case Type::FloatTyID: opCode = FMULS; break;
570 case Type::DoubleTyID: opCode = FMULD; break;
571 default: assert(0 && "Invalid type for MUL instruction"); break;
572 }
573 }
574
575 return opCode;
576}
577
578
579static inline MachineInstr*
580CreateIntNegInstruction(Value* vreg)
581{
582 MachineInstr* minstr = new MachineInstr(SUB);
583 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
584 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
585 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
586 return minstr;
587}
588
589
590static inline MachineInstr*
591CreateMulConstInstruction(const InstructionNode* instrNode,
592 MachineInstr*& getMinstr2)
593{
594 MachineInstr* minstr = NULL;
595 getMinstr2 = NULL;
596 bool needNeg = false;
597
598 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
599 assert(constOp->getValueType() == Value::ConstantVal);
600
601 // Cases worth optimizing are:
602 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
603 // (2) Multiply by 2^x for integer types: replace with Shift
604 //
605 const Type* resultType = instrNode->getInstruction()->getType();
606
607 if (resultType->isIntegral())
608 {
609 unsigned pow;
610 bool isValidConst;
611 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
612 if (isValidConst)
613 {
614 bool needNeg = false;
615 if (C < 0)
616 {
617 needNeg = true;
618 C = -C;
619 }
620
621 if (C == 0 || C == 1)
622 {
623 minstr = new MachineInstr(ADD);
624
625 if (C == 0)
626 minstr->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
627 else
628 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
629 instrNode->leftChild()->getValue());
630 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
631 }
632 else if (IsPowerOf2(C, pow))
633 {
634 minstr = new MachineInstr((resultType == Type::LongTy)
635 ? SLLX : SLL);
636 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
637 instrNode->leftChild()->getValue());
638 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
639 pow);
640 }
641
642 if (minstr && needNeg)
643 { // insert <reg = SUB 0, reg> after the instr to flip the sign
644 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
645 }
646 }
647 }
648 else
649 {
650 if (resultType == Type::FloatTy ||
651 resultType == Type::DoubleTy)
652 {
653 bool isValidConst;
654 double dval = ((ConstPoolFP*) constOp)->getValue();
655
656 if (isValidConst)
657 {
658 if (dval == 0)
659 {
660 minstr = new MachineInstr((resultType == Type::FloatTy)
661 ? FITOS : FITOD);
662 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
663 }
664 else if (fabs(dval) == 1)
665 {
666 bool needNeg = (dval < 0);
667
668 MachineOpCode opCode = needNeg
669 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
670 : (resultType == Type::FloatTy? FMOVS : FMOVD);
671
672 minstr = new MachineInstr(opCode);
673 minstr->SetMachineOperand(0,
674 MachineOperand::MO_VirtualRegister,
675 instrNode->leftChild()->getValue());
676 }
677 }
678 }
679 }
680
681 if (minstr != NULL)
682 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
683 instrNode->getValue());
684
685 return minstr;
686}
687
688
689static inline MachineOpCode
690ChooseDivInstruction(const InstructionNode* instrNode)
691{
692 MachineOpCode opCode = INVALID_OPCODE;
693
694 const Type* resultType = instrNode->getInstruction()->getType();
695
696 if (resultType->isIntegral())
697 {
698 opCode = resultType->isSigned()? SDIVX : UDIVX;
699 }
700 else
701 {
702 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
703 switch(operand->getType()->getPrimitiveID())
704 {
705 case Type::FloatTyID: opCode = FDIVS; break;
706 case Type::DoubleTyID: opCode = FDIVD; break;
707 default: assert(0 && "Invalid type for DIV instruction"); break;
708 }
709 }
710
711 return opCode;
712}
713
714
715static inline MachineInstr*
716CreateDivConstInstruction(const InstructionNode* instrNode,
717 MachineInstr*& getMinstr2)
718{
719 MachineInstr* minstr = NULL;
720 getMinstr2 = NULL;
721
722 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
723 assert(constOp->getValueType() == Value::ConstantVal);
724
725 // Cases worth optimizing are:
726 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
727 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
728 //
729 const Type* resultType = instrNode->getInstruction()->getType();
730
731 if (resultType->isIntegral())
732 {
733 unsigned pow;
734 bool isValidConst;
735 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
736 if (isValidConst)
737 {
738 bool needNeg = false;
739 if (C < 0)
740 {
741 needNeg = true;
742 C = -C;
743 }
744
745 if (C == 1)
746 {
747 minstr = new MachineInstr(ADD);
748 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
749 instrNode->leftChild()->getValue());
750 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
751 }
752 else if (IsPowerOf2(C, pow))
753 {
754 MachineOpCode opCode= ((resultType->isSigned())
755 ? (resultType==Type::LongTy)? SRAX : SRA
756 : (resultType==Type::LongTy)? SRLX : SRL);
757 minstr = new MachineInstr(opCode);
758 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
759 instrNode->leftChild()->getValue());
760 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
761 pow);
762 }
763
764 if (minstr && needNeg)
765 { // insert <reg = SUB 0, reg> after the instr to flip the sign
766 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
767 }
768 }
769 }
770 else
771 {
772 if (resultType == Type::FloatTy ||
773 resultType == Type::DoubleTy)
774 {
775 bool isValidConst;
776 double dval = ((ConstPoolFP*) constOp)->getValue();
777
778 if (isValidConst && fabs(dval) == 1)
779 {
780 bool needNeg = (dval < 0);
781
782 MachineOpCode opCode = needNeg
783 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
784 : (resultType == Type::FloatTy? FMOVS : FMOVD);
785
786 minstr = new MachineInstr(opCode);
787 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
788 instrNode->leftChild()->getValue());
789 }
790 }
791 }
792
793 if (minstr != NULL)
794 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
795 instrNode->getValue());
796
797 return minstr;
798}
799
800
801static inline MachineOpCode
802ChooseLoadInstruction(const Type* resultType)
803{
804 MachineOpCode opCode = INVALID_OPCODE;
805
806 switch (resultType->getPrimitiveID())
807 {
808 case Type::BoolTyID: opCode = LDUB; break;
809 case Type::UByteTyID: opCode = LDUB; break;
810 case Type::SByteTyID: opCode = LDSB; break;
811 case Type::UShortTyID: opCode = LDUH; break;
812 case Type::ShortTyID: opCode = LDSH; break;
813 case Type::UIntTyID: opCode = LDUW; break;
814 case Type::IntTyID: opCode = LDSW; break;
815 case Type::ULongTyID:
816 case Type::LongTyID: opCode = LDX; break;
817 case Type::FloatTyID: opCode = LD; break;
818 case Type::DoubleTyID: opCode = LDD; break;
819 default: assert(0 && "Invalid type for Load instruction"); break;
820 }
821
822 return opCode;
823}
824
825
826static inline MachineOpCode
827ChooseStoreInstruction(const Type* valueType)
828{
829 MachineOpCode opCode = INVALID_OPCODE;
830
831 switch (valueType->getPrimitiveID())
832 {
833 case Type::BoolTyID:
834 case Type::UByteTyID:
835 case Type::SByteTyID: opCode = STB; break;
836 case Type::UShortTyID:
837 case Type::ShortTyID: opCode = STH; break;
838 case Type::UIntTyID:
839 case Type::IntTyID: opCode = STW; break;
840 case Type::ULongTyID:
841 case Type::LongTyID: opCode = STX; break;
842 case Type::FloatTyID: opCode = ST; break;
843 case Type::DoubleTyID: opCode = STD; break;
844 default: assert(0 && "Invalid type for Store instruction"); break;
845 }
846
847 return opCode;
848}
849
850
851//------------------------------------------------------------------------
852// Function SetOperandsForMemInstr
853//
854// Choose addressing mode for the given load or store instruction.
855// Use [reg+reg] if it is an indexed reference, and the index offset is
856// not a constant or if it cannot fit in the offset field.
857// Use [reg+offset] in all other cases.
858//
859// This assumes that all array refs are "lowered" to one of these forms:
860// %x = load (subarray*) ptr, constant ; single constant offset
861// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
862// Generally, this should happen via strength reduction + LICM.
863// Also, strength reduction should take care of using the same register for
864// the loop index variable and an array index, when that is profitable.
865//------------------------------------------------------------------------
866
867static void
868SetOperandsForMemInstr(MachineInstr* minstr,
869 const InstructionNode* vmInstrNode,
870 const TargetMachine& target)
871{
872 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
873
874 // Variables to hold the index vector, ptr value, and offset value.
875 // The major work here is to extract these for all 3 instruction types
876 // and then call the common function SetMemOperands_Internal().
877 //
878 const vector<ConstPoolVal*>* idxVec = & memInst->getIndexVec();
879 vector<ConstPoolVal*>* newIdxVec = NULL;
880 Value* ptrVal;
881 Value* arrayOffsetVal = NULL;
882
883 // Test if a GetElemPtr instruction is being folded into this mem instrn.
884 // If so, it will be in the left child for Load and GetElemPtr,
885 // and in the right child for Store instructions.
886 //
887 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
888 ? vmInstrNode->rightChild()
889 : vmInstrNode->leftChild());
890
891 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
892 ptrChild->getOpLabel() == GetElemPtrIdx)
893 {
894 // There is a GetElemPtr instruction and there may be a chain of
895 // more than one. Use the pointer value of the last one in the chain.
896 // Fold the index vectors from the entire chain and from the mem
897 // instruction into one single index vector.
898 // Finally, we never fold for an array instruction so make that NULL.
899
900 newIdxVec = new vector<ConstPoolVal*>;
901 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
902
903 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
904 idxVec = newIdxVec;
905
906 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
907 && "GetElemPtr cannot be folded into array refs in selection");
908 }
909 else
910 {
911 // There is no GetElemPtr instruction.
912 // Use the pointer value and the index vector from the Mem instruction.
913 // If it is an array reference, get the array offset value.
914 //
915 ptrVal = memInst->getPtrOperand();
916
917 const Type* opType =
918 ((const PointerType*) ptrVal->getType())->getValueType();
919 if (opType->isArrayType())
920 {
921 assert((memInst->getNumOperands()
922 == (unsigned) 1 + memInst->getFirstOffsetIdx())
923 && "Array refs must be lowered before Instruction Selection");
924
925 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
926 }
927 }
928
929 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
930 *idxVec, target);
931
932 if (newIdxVec != NULL)
933 delete newIdxVec;
934}
935
936
937static void
938SetMemOperands_Internal(MachineInstr* minstr,
939 const InstructionNode* vmInstrNode,
940 Value* ptrVal,
941 Value* arrayOffsetVal,
942 const vector<ConstPoolVal*>& idxVec,
943 const TargetMachine& target)
944{
945 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
946
947 // Initialize so we default to storing the offset in a register.
948 int64_t smallConstOffset;
949 Value* valueForRegOffset = NULL;
950 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
951
952 // Check if there is an index vector and if so, if it translates to
953 // a small enough constant to fit in the immediate-offset field.
954 //
955 if (idxVec.size() > 0)
956 {
957 bool isConstantOffset = false;
958 unsigned offset;
959
960 const PointerType* ptrType = (PointerType*) ptrVal->getType();
961
962 if (ptrType->getValueType()->isStructType())
963 {
964 // the offset is always constant for structs
965 isConstantOffset = true;
966
967 // Compute the offset value using the index vector
968 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
969 }
970 else
971 {
972 // It must be an array ref. Check if the offset is a constant,
973 // and that the indexing has been lowered to a single offset.
974 //
975 assert(ptrType->getValueType()->isArrayType());
976 assert(arrayOffsetVal != NULL
977 && "Expect to be given Value* for array offsets");
978
979 if (ConstPoolVal *CPV = arrayOffsetVal->castConstant())
980 {
981 isConstantOffset = true; // always constant for structs
982 assert(arrayOffsetVal->getType()->isIntegral());
983 offset = (CPV->getType()->isSigned()
984 ? ((ConstPoolSInt*)CPV)->getValue()
985 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
986 }
987 else
988 {
989 valueForRegOffset = arrayOffsetVal;
990 }
991 }
992
993 if (isConstantOffset)
994 {
995 // create a virtual register for the constant
Chris Lattner1fa0c092001-09-07 21:22:57 +0000996 valueForRegOffset = ConstPoolSInt::get(Type::IntTy, offset);
Vikram S. Adved4228a52001-08-28 23:12:57 +0000997 }
998 }
999 else
1000 {
1001 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1002 smallConstOffset = 0;
1003 }
1004
1005 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
1006 // It is the left child in the instruction tree in all cases.
1007 Value* leftVal = vmInstrNode->leftChild()->getValue();
1008 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
1009
1010 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
1011 // Operand 3 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
1012 //
1013 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
1014 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1015 {
1016 assert(valueForRegOffset != NULL);
1017 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
1018 }
1019 else
1020 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
1021
1022 if (memInst->getOpcode() == Instruction::Store)
1023 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
1024 else
1025 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1026 vmInstrNode->getValue());
1027}
1028
1029
1030// Special handling for constant operands:
1031// -- if the constant is 0, use the hardwired 0 register, if any;
1032// -- if the constant is of float or double type but has an integer value,
1033// use int-to-float conversion instruction instead of generating a load;
1034// -- if the constant fits in the IMMEDIATE field, use that field;
1035// -- else insert instructions to put the constant into a register, either
1036// directly or by loading explicitly from the constant pool.
1037//
1038static unsigned
1039FixConstantOperands(const InstructionNode* vmInstrNode,
1040 MachineInstr** mvec,
1041 unsigned numInstr,
1042 TargetMachine& target)
1043{
1044 static MachineInstr* loadConstVec[MAX_INSTR_PER_VMINSTR];
1045
1046 unsigned numNew = 0;
1047 Instruction* vmInstr = vmInstrNode->getInstruction();
1048
1049 for (unsigned i=0; i < numInstr; i++)
1050 {
1051 MachineInstr* minstr = mvec[i];
1052 const MachineInstrDescriptor& instrDesc =
1053 target.getInstrInfo().getDescriptor(minstr->getOpCode());
1054
1055 for (unsigned op=0; op < minstr->getNumOperands(); op++)
1056 {
1057 const MachineOperand& mop = minstr->getOperand(op);
1058
1059 // skip the result position (for efficiency below) and any other
1060 // positions already marked as not a virtual register
1061 if (instrDesc.resultPos == (int) op ||
1062 mop.getOperandType() != MachineOperand::MO_VirtualRegister ||
1063 mop.getVRegValue() == NULL)
1064 {
1065 break;
1066 }
1067
1068 Value* opValue = mop.getVRegValue();
1069
1070 if (opValue->getValueType() == Value::ConstantVal)
1071 {
1072 unsigned int machineRegNum;
1073 int64_t immedValue;
1074 MachineOperand::MachineOperandType opType =
1075 ChooseRegOrImmed(opValue, minstr->getOpCode(), target,
1076 /*canUseImmed*/ (op == 1),
1077 machineRegNum, immedValue);
1078
1079 if (opType == MachineOperand::MO_MachineRegister)
1080 minstr->SetMachineOperand(op, machineRegNum);
1081 else if (opType == MachineOperand::MO_VirtualRegister)
1082 {
1083 // value is constant and must be loaded into a register
1084 TmpInstruction* tmpReg;
1085 MachineInstr* minstr2;
1086 loadConstVec[numNew++] = MakeLoadConstInstr(vmInstr, opValue,
1087 tmpReg, minstr2);
1088 minstr->SetMachineOperand(op, opType, tmpReg);
1089 if (minstr2 != NULL)
1090 loadConstVec[numNew++] = minstr2;
1091 }
1092 else
1093 minstr->SetMachineOperand(op, opType, immedValue);
1094 }
1095 }
1096 }
1097
1098 if (numNew > 0)
1099 {
1100 // Insert the new instructions *before* the old ones by moving
1101 // the old ones over `numNew' positions (last-to-first, of course!).
1102 // We do check *after* returning that we did not exceed the vector mvec.
1103 for (int i=numInstr-1; i >= 0; i--)
1104 mvec[i+numNew] = mvec[i];
1105
1106 for (unsigned i=0; i < numNew; i++)
1107 mvec[i] = loadConstVec[i];
1108 }
1109
1110 return (numInstr + numNew);
1111}
1112
1113
1114static inline MachineInstr*
1115MakeIntSetInstruction(int64_t C, bool isSigned, Value* dest)
1116{
1117 MachineInstr* minstr;
1118 if (isSigned)
1119 {
1120 minstr = new MachineInstr(SETSW);
1121 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
1122 }
1123 else
1124 {
1125 minstr = new MachineInstr(SETUW);
1126 minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed, C);
1127 }
1128
1129 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
1130
1131 return minstr;
1132}
1133
1134
1135static MachineInstr*
1136MakeLoadConstInstr(Instruction* vmInstr,
1137 Value* val,
1138 TmpInstruction*& tmpReg,
1139 MachineInstr*& getMinstr2)
1140{
1141 assert(val->getValueType() == Value::ConstantVal);
1142
1143 MachineInstr* minstr;
1144
1145 getMinstr2 = NULL;
1146
1147 // Create a TmpInstruction to mark the hidden register used for the constant
1148 tmpReg = new TmpInstruction(Instruction::UserOp1, val, NULL);
1149 vmInstr->getMachineInstrVec().addTempValue(tmpReg);
1150
1151 // Use a "set" instruction for known constants that can go in an integer reg.
1152 // Use a "set" instruction followed by a int-to-float conversion for known
1153 // constants that must go in a floating point reg but have an integer value.
1154 // Use a "load" instruction for all other constants, in particular,
1155 // floating point constants.
1156 //
1157 const Type* valType = val->getType();
1158
1159 if (valType->isIntegral() ||
1160 valType->isPointerType() ||
1161 valType == Type::BoolTy)
1162 {
1163 bool isValidConstant;
1164 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
1165 assert(isValidConstant && "Unrecognized constant");
1166
1167 minstr = MakeIntSetInstruction(C, valType->isSigned(), tmpReg);
1168 }
1169 else
1170 {
1171 assert(valType == Type::FloatTy || valType == Type::DoubleTy);
1172 double dval = ((ConstPoolFP*) val)->getValue();
1173 if (dval == (int64_t) dval)
1174 {
1175 // The constant actually has an integer value, so use a
1176 // [set; int-to-float] sequence instead of a load instruction.
1177 //
1178 TmpInstruction* tmpReg2 = NULL;
1179 if (dval != 0.0)
1180 { // First, create an integer constant of the same value as dval
Chris Lattner1fa0c092001-09-07 21:22:57 +00001181 ConstPoolSInt* ival = ConstPoolSInt::get(Type::IntTy,
1182 (int64_t) dval);
Vikram S. Adved4228a52001-08-28 23:12:57 +00001183 // Create another TmpInstruction for the hidden integer register
1184 TmpInstruction* tmpReg2 =
1185 new TmpInstruction(Instruction::UserOp1, ival, NULL);
1186 vmInstr->getMachineInstrVec().addTempValue(tmpReg2);
1187
1188 // Create the `SET' instruction
1189 minstr = MakeIntSetInstruction((int64_t)dval, true, tmpReg2);
1190 }
1191
1192 // In which variable do we put the second instruction?
1193 MachineInstr*& instr2 = (minstr)? getMinstr2 : minstr;
1194
1195 // Create the int-to-float instruction
1196 instr2 = new MachineInstr(valType == Type::FloatTy? FITOS : FITOD);
1197
1198 if (dval == 0.0)
1199 instr2->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
1200 else
1201 instr2->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
1202 tmpReg2);
1203
1204 instr2->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1205 tmpReg);
1206 }
1207 else
1208 {
1209 // Make a Load instruction, and make `val' both the ptr value *and*
1210 // the result value, and set the offset field to 0. Final code
1211 // generation will have to generate the base+offset for the constant.
1212 //
1213 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
1214 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
1215 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,val);
1216 minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1217 zeroOffset);
1218 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1219 tmpReg);
1220 }
1221 }
1222
1223 tmpReg->addMachineInstruction(minstr);
1224
1225 assert(minstr);
1226 return minstr;
1227}
1228
1229//
1230// Substitute operand `operandNum' of the instruction in node `treeNode'
1231// in place the use(s) of that instruction in node `parent'.
1232//
1233static void
1234ForwardOperand(InstructionNode* treeNode,
1235 InstructionNode* parent,
1236 int operandNum)
1237{
1238 Instruction* unusedOp = treeNode->getInstruction();
1239 Value* fwdOp = unusedOp->getOperand(operandNum);
1240 Instruction* userInstr = parent->getInstruction();
1241 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
1242 for (unsigned i=0, N=mvec.size(); i < N; i++)
1243 {
1244 MachineInstr* minstr = mvec[i];
1245 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; i++)
1246 {
1247 const MachineOperand& mop = minstr->getOperand(i);
1248 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1249 mop.getVRegValue() == unusedOp)
1250 {
1251 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
1252 fwdOp);
1253 }
1254 }
1255 }
1256}
1257
1258
1259// This function is currently unused and incomplete but will be
1260// used if we have a linear layout of basic blocks in LLVM code.
1261// It decides which branch should fall-through, and whether an
1262// extra unconditional branch is needed (when neither falls through).
1263//
1264void
1265ChooseBranchPattern(Instruction* vmInstr, BranchPattern& brPattern)
1266{
1267 BranchInst* brInstr = (BranchInst*) vmInstr;
1268
1269 brPattern.flipCondition = false;
1270 brPattern.targetBB = brInstr->getSuccessor(0);
1271 brPattern.extraBranch = NULL;
1272
1273 assert(brInstr->getNumSuccessors() > 1 &&
1274 "Unnecessary analysis for unconditional branch");
1275
1276 assert(0 && "Fold branches in peephole optimization");
1277}
1278
1279
1280//******************* Externally Visible Functions *************************/
1281
1282
Vikram S. Advea21cf202001-07-21 12:42:19 +00001283//------------------------------------------------------------------------
1284// External Function: GetInstructionsByRule
1285//
1286// Purpose:
1287// Choose machine instructions for the SPARC according to the
1288// patterns chosen by the BURG-generated parser.
1289//------------------------------------------------------------------------
1290
1291unsigned
1292GetInstructionsByRule(InstructionNode* subtreeRoot,
1293 int ruleForNode,
1294 short* nts,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001295 TargetMachine &target,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001296 MachineInstr** mvec)
1297{
1298 int numInstr = 1; // initialize for common case
1299 bool checkCast = false; // initialize here to use fall-through
1300 Value *leftVal, *rightVal;
1301 const Type* opType;
1302 int nextRule;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001303 int forwardOperandNum = -1;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001304 BranchPattern brPattern;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001305 int64_t s0 = 0; // variables holding zero to avoid
1306 uint64_t u0 = 0; // overloading ambiguities below
Vikram S. Advea21cf202001-07-21 12:42:19 +00001307
1308 mvec[0] = mvec[1] = mvec[2] = mvec[3] = NULL; // just for safety
1309
1310 switch(ruleForNode) {
1311 case 1: // stmt: Ret
1312 case 2: // stmt: RetValue(reg)
1313 // NOTE: Prepass of register allocation is responsible
1314 // for moving return value to appropriate register.
1315 // Mark the return-address register as a hidden virtual reg.
1316 {
1317 Instruction* returnReg = new TmpInstruction(Instruction::UserOp1,
1318 subtreeRoot->getInstruction(), NULL);
1319 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(returnReg);
1320
1321 mvec[0] = new MachineInstr(RETURN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001322 mvec[0]->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,returnReg);
1323 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1324
1325 returnReg->addMachineInstruction(mvec[0]);
1326
Vikram S. Advea21cf202001-07-21 12:42:19 +00001327 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1328 break;
1329 }
1330
1331 case 3: // stmt: Store(reg,reg)
1332 case 4: // stmt: Store(reg,ptrreg)
1333 mvec[0] = new MachineInstr(ChooseStoreInstruction(subtreeRoot->leftChild()->getValue()->getType()));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001334 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001335 break;
1336
1337 case 5: // stmt: BrUncond
1338 mvec[0] = new MachineInstr(BA);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001339 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, (Value*)NULL);
1340 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001341 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1342
Vikram S. Adved4228a52001-08-28 23:12:57 +00001343 // delay slot
1344 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001345 break;
1346
1347 case 6: // stmt: BrCond(boolconst)
1348 // boolconst => boolean was computed with `%b = setCC type reg1 constant'
1349 // If the constant is ZERO, we can use the branch-on-integer-register
1350 // instructions and avoid the SUBcc instruction entirely.
1351 // Otherwise this is just the same as case 5, so just fall through.
1352 {
1353 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1354 assert(constNode && constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1355 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
Vikram S. Adve4f231662001-07-28 04:15:15 +00001356 bool isValidConst;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001357
1358 if (constVal->getType()->isIntegral()
Vikram S. Adve4f231662001-07-28 04:15:15 +00001359 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1360 && isValidConst)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001361 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001362 // That constant ia a zero after all...
Vikram S. Advea21cf202001-07-21 12:42:19 +00001363 // Use the left child of the setCC instruction as the first argument!
1364 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001365 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001366 subtreeRoot->leftChild()->leftChild()->getValue());
1367 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1368 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1369
Vikram S. Adve4f231662001-07-28 04:15:15 +00001370 // delay slot
1371 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001372
Vikram S. Adve4f231662001-07-28 04:15:15 +00001373 // false branch
1374 mvec[numInstr++] = new MachineInstr(BA);
1375 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1376 (Value*) NULL);
1377 mvec[numInstr-1]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adved4228a52001-08-28 23:12:57 +00001378
1379 // delay slot
1380 mvec[numInstr++] = new MachineInstr(NOP);
1381
Vikram S. Advea21cf202001-07-21 12:42:19 +00001382 break;
1383 }
1384 // ELSE FALL THROUGH
1385 }
1386
1387 case 7: // stmt: BrCond(bool)
1388 // bool => boolean was computed with `%b = setcc type reg1 reg2'
1389 // Need to check whether the type was a FP, signed int or unsigned int,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001390 // and check the branching condition in order to choose the branch to use.
Vikram S. Advea21cf202001-07-21 12:42:19 +00001391 //
1392 {
1393 bool isFPBranch;
1394 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001395 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1396 subtreeRoot->leftChild()->getValue());
1397 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001398 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1399
Vikram S. Adve4f231662001-07-28 04:15:15 +00001400 // delay slot
1401 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001402
Vikram S. Adve4f231662001-07-28 04:15:15 +00001403 // false branch
1404 mvec[numInstr++] = new MachineInstr(BA);
1405 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1406 (Value*) NULL);
1407 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1408 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adved4228a52001-08-28 23:12:57 +00001409
1410 // delay slot
1411 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001412 break;
1413 }
Vikram S. Adve4f231662001-07-28 04:15:15 +00001414
Vikram S. Advea21cf202001-07-21 12:42:19 +00001415 case 8: // stmt: BrCond(boolreg)
1416 // bool => boolean is stored in an existing register.
1417 // Just use the branch-on-integer-register instruction!
1418 //
1419 mvec[0] = new MachineInstr(BRNZ);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001420 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1421 subtreeRoot->leftChild()->getValue());
Vikram S. Advea21cf202001-07-21 12:42:19 +00001422 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1423 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adved4228a52001-08-28 23:12:57 +00001424
1425 // delay slot
Vikram S. Advea21cf202001-07-21 12:42:19 +00001426 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
Vikram S. Adved4228a52001-08-28 23:12:57 +00001427
1428 // false branch
1429 mvec[numInstr++] = new MachineInstr(BA);
1430 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1431 (Value*) NULL);
1432 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1433 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1434
1435 // delay slot
1436 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001437 break;
1438
1439 case 9: // stmt: Switch(reg)
1440 assert(0 && "*** SWITCH instruction is not implemented yet.");
1441 numInstr = 0;
1442 break;
1443
1444 case 10: // reg: VRegList(reg, reg)
1445 assert(0 && "VRegList should never be the topmost non-chain rule");
1446 break;
1447
1448 case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0
1449 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001450 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001451 subtreeRoot->leftChild()->getValue());
1452 mvec[0]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001453 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001454 subtreeRoot->getValue());
1455 break;
1456
1457 case 22: // reg: ToBoolTy(reg):
1458 opType = subtreeRoot->leftChild()->getValue()->getType();
1459 assert(opType->isIntegral() || opType == Type::BoolTy);
1460 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001461 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001462 break;
1463
1464 case 23: // reg: ToUByteTy(reg)
1465 case 25: // reg: ToUShortTy(reg)
1466 case 27: // reg: ToUIntTy(reg)
1467 case 29: // reg: ToULongTy(reg)
1468 opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001469 assert(opType->isIntegral() ||
1470 opType->isPointerType() ||
1471 opType == Type::BoolTy && "Ignoring cast: illegal for other types");
Vikram S. Advea21cf202001-07-21 12:42:19 +00001472 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001473 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001474 break;
1475
1476 case 24: // reg: ToSByteTy(reg)
1477 case 26: // reg: ToShortTy(reg)
1478 case 28: // reg: ToIntTy(reg)
1479 case 30: // reg: ToLongTy(reg)
1480 opType = subtreeRoot->leftChild()->getValue()->getType();
1481 if (opType->isIntegral() || opType == Type::BoolTy)
Vikram S. Adve4f231662001-07-28 04:15:15 +00001482 {
1483 numInstr = 0;
1484 forwardOperandNum = 0;
1485 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001486 else
1487 {
1488 mvec[0] =new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,opType));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001489 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001490 }
1491 break;
1492
1493 case 31: // reg: ToFloatTy(reg):
1494 case 32: // reg: ToDoubleTy(reg):
1495
1496 // If this instruction has a parent (a user) in the tree
1497 // and the user is translated as an FsMULd instruction,
1498 // then the cast is unnecessary. So check that first.
1499 // In the future, we'll want to do the same for the FdMULq instruction,
1500 // so do the check here instead of only for ToFloatTy(reg).
1501 //
1502 if (subtreeRoot->parent() != NULL &&
1503 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1504 {
1505 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001506 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001507 }
1508 else
1509 {
1510 opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve9856e0c2001-09-09 20:35:34 +00001511 MachineOpCode opCode = ChooseConvertToFloatInstr(subtreeRoot, opType);
1512 if (opCode == INVALID_OPCODE) // no conversion needed
1513 {
1514 numInstr = 0;
1515 forwardOperandNum = 0;
1516 }
1517 else
1518 {
1519 mvec[0] = new MachineInstr(opCode);
1520 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1521 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001522 }
1523 break;
1524
1525 case 19: // reg: ToArrayTy(reg):
1526 case 20: // reg: ToPointerTy(reg):
1527 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001528 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001529 break;
1530
Vikram S. Adved4228a52001-08-28 23:12:57 +00001531 case 233: // reg: Add(reg, Constant)
1532 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1533 if (mvec[0] != NULL)
1534 break;
1535 // ELSE FALL THROUGH
1536
Vikram S. Advea21cf202001-07-21 12:42:19 +00001537 case 33: // reg: Add(reg, reg)
1538 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001539 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001540 break;
1541
Vikram S. Adved4228a52001-08-28 23:12:57 +00001542 case 234: // reg: Sub(reg, Constant)
1543 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1544 if (mvec[0] != NULL)
1545 break;
1546 // ELSE FALL THROUGH
1547
Vikram S. Advea21cf202001-07-21 12:42:19 +00001548 case 34: // reg: Sub(reg, reg)
1549 mvec[0] = new MachineInstr(ChooseSubInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001550 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001551 break;
1552
1553 case 135: // reg: Mul(todouble, todouble)
1554 checkCast = true;
1555 // FALL THROUGH
1556
1557 case 35: // reg: Mul(reg, reg)
1558 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001559 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001560 break;
Vikram S. Adved4228a52001-08-28 23:12:57 +00001561
1562 case 335: // reg: Mul(todouble, todoubleConst)
1563 checkCast = true;
1564 // FALL THROUGH
1565
1566 case 235: // reg: Mul(reg, Constant)
1567 mvec[0] = CreateMulConstInstruction(subtreeRoot, mvec[1]);
1568 if (mvec[0] == NULL)
1569 {
1570 mvec[0]=new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
1571 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1572 }
1573 else
1574 if (mvec[1] != NULL)
1575 ++numInstr;
1576 break;
1577
1578 case 236: // reg: Div(reg, Constant)
1579 mvec[0] = CreateDivConstInstruction(subtreeRoot, mvec[1]);
1580 if (mvec[0] != NULL)
1581 {
1582 if (mvec[1] != NULL)
1583 ++numInstr;
1584 }
1585 else
1586 // ELSE FALL THROUGH
Vikram S. Advea21cf202001-07-21 12:42:19 +00001587
1588 case 36: // reg: Div(reg, reg)
1589 mvec[0] = new MachineInstr(ChooseDivInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001590 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001591 break;
1592
Vikram S. Adved4228a52001-08-28 23:12:57 +00001593 case 37: // reg: Rem(reg, reg)
1594 case 237: // reg: Rem(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001595 assert(0 && "REM instruction unimplemented for the SPARC.");
1596 break;
1597
Vikram S. Adved4228a52001-08-28 23:12:57 +00001598 case 38: // reg: And(reg, reg)
1599 case 238: // reg: And(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001600 mvec[0] = new MachineInstr(AND);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001601 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001602 break;
1603
1604 case 138: // reg: And(reg, not)
1605 mvec[0] = new MachineInstr(ANDN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001606 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001607 break;
1608
Vikram S. Adved4228a52001-08-28 23:12:57 +00001609 case 39: // reg: Or(reg, reg)
1610 case 239: // reg: Or(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001611 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001612 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001613 break;
1614
1615 case 139: // reg: Or(reg, not)
1616 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001617 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001618 break;
1619
Vikram S. Adved4228a52001-08-28 23:12:57 +00001620 case 40: // reg: Xor(reg, reg)
1621 case 240: // reg: Xor(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001622 mvec[0] = new MachineInstr(XOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001623 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001624 break;
1625
1626 case 140: // reg: Xor(reg, not)
1627 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001628 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001629 break;
1630
1631 case 41: // boolconst: SetCC(reg, Constant)
1632 // Check if this is an integer comparison, and
1633 // there is a parent, and the parent decided to use
1634 // a branch-on-integer-register instead of branch-on-condition-code.
1635 // If so, the SUBcc instruction is not required.
1636 // (However, we must still check for constants to be loaded from
1637 // the constant pool so that such a load can be associated with
1638 // this instruction.)
1639 //
Vikram S. Adve4f231662001-07-28 04:15:15 +00001640 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Advea21cf202001-07-21 12:42:19 +00001641 //
1642 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() &&
1643 subtreeRoot->parent() != NULL)
1644 {
1645 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1646 assert(parentNode->getNodeType() == InstrTreeNode::NTInstructionNode);
1647 const vector<MachineInstr*>&
1648 minstrVec = parentNode->getInstruction()->getMachineInstrVec();
1649 MachineOpCode parentOpCode;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001650 if (parentNode->getInstruction()->getOpcode() == Instruction::Br &&
Vikram S. Advea21cf202001-07-21 12:42:19 +00001651 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1652 parentOpCode <= BRGEZ)
1653 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001654 numInstr = 0; // don't forward the operand!
Vikram S. Advea21cf202001-07-21 12:42:19 +00001655 break;
1656 }
1657 }
1658 // ELSE FALL THROUGH
1659
1660 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4f231662001-07-28 04:15:15 +00001661 {
1662 // If result of the SetCC is only used for a branch, we can
1663 // discard the result. otherwise, it must go into an integer register.
1664 // Note that the user may or may not be in the same tree, so we have
1665 // to follow SSA def-use edges here, not BURG tree edges.
1666 //
1667 Instruction* result = subtreeRoot->getInstruction();
1668 Value* firstUse = (Value*) * result->use_begin();
1669 bool discardResult =
1670 (result->use_size() == 1
1671 && firstUse->getValueType() == Value::InstructionVal
1672 && ((Instruction*) firstUse)->getOpcode() == Instruction::Br);
1673
1674 bool mustClearReg;
1675 int valueToMove;
1676 MachineOpCode movOpCode;
1677
Vikram S. Advea21cf202001-07-21 12:42:19 +00001678 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral())
1679 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001680 // integer condition: destination should be %g0 or integer register
1681 // if result must be saved but condition is not SetEQ then we need
1682 // a separate instruction to compute the bool result, so discard
1683 // result of SUBcc instruction anyway.
1684 //
Vikram S. Advea21cf202001-07-21 12:42:19 +00001685 mvec[0] = new MachineInstr(SUBcc);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001686 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, discardResult);
1687
Vikram S. Adve98a9c972001-08-06 21:06:10 +00001688 // mark the 4th operand as being a CC register, and a "result"
1689 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
1690 subtreeRoot->getValue(), /*def*/ true);
1691
1692 if (!discardResult)
Vikram S. Adve4f231662001-07-28 04:15:15 +00001693 { // recompute bool if needed, using the integer condition codes
1694 if (result->getOpcode() == Instruction::SetNE)
1695 discardResult = true;
1696 else
1697 movOpCode =
1698 ChooseMovpccAfterSub(subtreeRoot, mustClearReg, valueToMove);
1699 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001700 }
1701 else
1702 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001703 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Advea21cf202001-07-21 12:42:19 +00001704 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
Vikram S. Advea21cf202001-07-21 12:42:19 +00001705 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001706 subtreeRoot->getValue());
1707 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1708 subtreeRoot->leftChild()->getValue());
1709 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1710 subtreeRoot->rightChild()->getValue());
1711
1712 if (!discardResult)
1713 {// recompute bool using the FP condition codes
1714 mustClearReg = true;
1715 valueToMove = 1;
1716 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1717 }
1718 }
1719
1720 if (!discardResult)
1721 {
1722 if (mustClearReg)
1723 {// Unconditionally set register to 0
1724 int n = numInstr++;
1725 mvec[n] = new MachineInstr(SETHI);
1726 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,s0);
1727 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1728 subtreeRoot->getValue());
1729 }
1730
1731 // Now conditionally move `valueToMove' (0 or 1) into the register
1732 int n = numInstr++;
1733 mvec[n] = new MachineInstr(movOpCode);
1734 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1735 subtreeRoot->getValue());
1736 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1737 valueToMove);
1738 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1739 subtreeRoot->getValue());
Vikram S. Advea21cf202001-07-21 12:42:19 +00001740 }
1741 break;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001742 }
1743
Vikram S. Advea21cf202001-07-21 12:42:19 +00001744 case 43: // boolreg: VReg
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001745 case 44: // boolreg: Constant
Vikram S. Advea21cf202001-07-21 12:42:19 +00001746 numInstr = 0;
1747 break;
1748
1749 case 51: // reg: Load(reg)
1750 case 52: // reg: Load(ptrreg)
1751 case 53: // reg: LoadIdx(reg,reg)
1752 case 54: // reg: LoadIdx(ptrreg,reg)
1753 mvec[0] = new MachineInstr(ChooseLoadInstruction(subtreeRoot->getValue()->getType()));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001754 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001755 break;
1756
1757 case 55: // reg: GetElemPtr(reg)
1758 case 56: // reg: GetElemPtrIdx(reg,reg)
1759 if (subtreeRoot->parent() != NULL)
1760 {
1761 // Check if the parent was an array access.
1762 // If so, we still need to generate this instruction.
1763 MemAccessInst* memInst =(MemAccessInst*) subtreeRoot->getInstruction();
1764 const PointerType* ptrType =
1765 (const PointerType*) memInst->getPtrOperand()->getType();
1766 if (! ptrType->getValueType()->isArrayType())
1767 {// we don't need a separate instr
Vikram S. Adve4f231662001-07-28 04:15:15 +00001768 numInstr = 0; // don't forward operand!
Vikram S. Advea21cf202001-07-21 12:42:19 +00001769 break;
1770 }
1771 }
1772 // else in all other cases we need to a separate ADD instruction
1773 mvec[0] = new MachineInstr(ADD);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001774 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001775 break;
1776
1777 case 57: // reg: Alloca: Implement as 2 instructions:
1778 // sub %sp, tmp -> %sp
1779 { // add %sp, 0 -> result
1780 Instruction* instr = subtreeRoot->getInstruction();
1781 const PointerType* instrType = (const PointerType*) instr->getType();
1782 assert(instrType->isPointerType());
Vikram S. Adve4f231662001-07-28 04:15:15 +00001783 int tsize = (int) target.findOptimalStorageSize(instrType->getValueType());
1784 assert(tsize != 0 && "Just to check when this can happen");
1785 // if (tsize == 0)
1786 // {
1787 // numInstr = 0;
1788 // break;
1789 // }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001790 //else go on to create the instructions needed...
1791
1792 // Create a temporary Value to hold the constant type-size
Chris Lattner1fa0c092001-09-07 21:22:57 +00001793 ConstPoolSInt* valueForTSize = ConstPoolSInt::get(Type::IntTy, tsize);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001794
1795 // Instruction 1: sub %sp, tsize -> %sp
1796 // tsize is always constant, but it may have to be put into a
1797 // register if it doesn't fit in the immediate field.
1798 //
1799 mvec[0] = new MachineInstr(SUB);
1800 mvec[0]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001801 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001802 mvec[0]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1803
1804 // Instruction 2: add %sp, 0 -> result
1805 numInstr++;
1806 mvec[1] = new MachineInstr(ADD);
1807 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1808 mvec[1]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001809 mvec[1]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001810 break;
1811 }
1812
1813 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1814 // mul num, typeSz -> tmp
1815 // sub %sp, tmp -> %sp
1816 { // add %sp, 0 -> result
1817 Instruction* instr = subtreeRoot->getInstruction();
1818 const PointerType* instrType = (const PointerType*) instr->getType();
1819 assert(instrType->isPointerType() &&
1820 instrType->getValueType()->isArrayType());
1821 const Type* eltType =
1822 ((ArrayType*) instrType->getValueType())->getElementType();
Vikram S. Adve4f231662001-07-28 04:15:15 +00001823 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001824
Vikram S. Adve4f231662001-07-28 04:15:15 +00001825 assert(tsize != 0 && "Just to check when this can happen");
1826 // if (tsize == 0)
1827 // {
1828 // numInstr = 0;
1829 // break;
1830 // }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001831 //else go on to create the instructions needed...
1832
1833 // Create a temporary Value to hold the constant type-size
Chris Lattner1fa0c092001-09-07 21:22:57 +00001834 ConstPoolSInt* valueForTSize = ConstPoolSInt::get(Type::IntTy, tsize);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001835
1836 // Create a temporary value to hold `tmp'
1837 Instruction* tmpInstr = new TmpInstruction(Instruction::UserOp1,
1838 subtreeRoot->leftChild()->getValue(),
1839 NULL /*could insert tsize here*/);
1840 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1841
1842 // Instruction 1: mul numElements, typeSize -> tmp
1843 mvec[0] = new MachineInstr(MULX);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001844 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001845 subtreeRoot->leftChild()->getValue());
Vikram S. Adve4f231662001-07-28 04:15:15 +00001846 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
1847 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,tmpInstr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001848
Vikram S. Adve4f231662001-07-28 04:15:15 +00001849 tmpInstr->addMachineInstruction(mvec[0]);
1850
Vikram S. Advea21cf202001-07-21 12:42:19 +00001851 // Instruction 2: sub %sp, tmp -> %sp
1852 numInstr++;
1853 mvec[1] = new MachineInstr(SUB);
1854 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001855 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,tmpInstr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001856 mvec[1]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1857
1858 // Instruction 3: add %sp, 0 -> result
1859 numInstr++;
1860 mvec[2] = new MachineInstr(ADD);
1861 mvec[2]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1862 mvec[2]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001863 mvec[2]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001864 break;
1865 }
1866
1867 case 61: // reg: Call
1868 // Generate a call-indirect (i.e., JMPL) for now to expose
1869 // the potential need for registers. If an absolute address
1870 // is available, replace this with a CALL instruction.
1871 // Mark both the indirection register and the return-address
1872 { // register as hidden virtual registers.
1873
Vikram S. Adve4f231662001-07-28 04:15:15 +00001874 Instruction* jmpAddrReg = new TmpInstruction(Instruction::UserOp1,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001875 ((CallInst*) subtreeRoot->getInstruction())->getCalledMethod(), NULL);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001876 Instruction* retAddrReg = new TmpInstruction(Instruction::UserOp1,
1877 subtreeRoot->getValue(), NULL);
1878 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(jmpAddrReg);
1879 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(retAddrReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001880
1881 mvec[0] = new MachineInstr(JMPL);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001882 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, jmpAddrReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001883 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1884 (int64_t) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001885 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, retAddrReg);
1886
1887 // NOTE: jmpAddrReg will be loaded by a different instruction generated
1888 // by the final code generator, so we just mark the CALL instruction
1889 // as computing that value.
1890 // The retAddrReg is actually computed by the CALL instruction.
1891 //
1892 jmpAddrReg->addMachineInstruction(mvec[0]);
1893 retAddrReg->addMachineInstruction(mvec[0]);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001894
1895 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1896 break;
1897 }
1898
1899 case 62: // reg: Shl(reg, reg)
1900 opType = subtreeRoot->leftChild()->getValue()->getType();
1901 assert(opType->isIntegral() || opType == Type::BoolTy);
1902 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001903 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001904 break;
1905
1906 case 63: // reg: Shr(reg, reg)
1907 opType = subtreeRoot->leftChild()->getValue()->getType();
1908 assert(opType->isIntegral() || opType == Type::BoolTy);
1909 mvec[0] = new MachineInstr((opType->isSigned()
1910 ? ((opType == Type::LongTy)? SRAX : SRA)
1911 : ((opType == Type::LongTy)? SRLX : SRL)));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001912 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001913 break;
1914
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001915 case 64: // reg: Phi(reg,reg)
1916 { // This instruction has variable #operands, so resultPos is 0.
1917 Instruction* phi = subtreeRoot->getInstruction();
1918 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
1919 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adved4228a52001-08-28 23:12:57 +00001920 subtreeRoot->getValue());
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001921 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
1922 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
1923 phi->getOperand(i));
1924 break;
1925 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001926 case 71: // reg: VReg
1927 case 72: // reg: Constant
Vikram S. Adve4f231662001-07-28 04:15:15 +00001928 numInstr = 0; // don't forward the value
Vikram S. Advea21cf202001-07-21 12:42:19 +00001929 break;
1930
1931 case 111: // stmt: reg
1932 case 112: // stmt: boolconst
1933 case 113: // stmt: bool
1934 case 121:
1935 case 122:
1936 case 123:
1937 case 124:
1938 case 125:
1939 case 126:
1940 case 127:
1941 case 128:
1942 case 129:
1943 case 130:
1944 case 131:
1945 case 132:
1946 case 153:
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001947 case 155:
Vikram S. Advea21cf202001-07-21 12:42:19 +00001948 //
1949 // These are all chain rules, which have a single nonterminal on the RHS.
1950 // Get the rule that matches the RHS non-terminal and use that instead.
1951 //
1952 assert(ThisIsAChainRule(ruleForNode));
1953 assert(nts[0] && ! nts[1]
1954 && "A chain rule should have only one RHS non-terminal!");
1955 nextRule = burm_rule(subtreeRoot->getBasicNode()->state, nts[0]);
1956 nts = burm_nts[nextRule];
Vikram S. Adve4f231662001-07-28 04:15:15 +00001957 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001958 break;
1959
1960 default:
Vikram S. Adve4f231662001-07-28 04:15:15 +00001961 assert(0 && "Unrecognized BURG rule");
Vikram S. Advea21cf202001-07-21 12:42:19 +00001962 numInstr = 0;
1963 break;
1964 }
1965
Vikram S. Adve4f231662001-07-28 04:15:15 +00001966 if (forwardOperandNum >= 0)
1967 { // We did not generate a machine instruction but need to use operand.
1968 // If user is in the same tree, replace Value in its machine operand.
1969 // If not, insert a copy instruction which should get coalesced away
1970 // by register allocation.
1971 if (subtreeRoot->parent() != NULL)
1972 ForwardOperand(subtreeRoot, (InstructionNode*) subtreeRoot->parent(),
1973 forwardOperandNum);
1974 else
1975 {
1976 int n = numInstr++;
1977 mvec[n] = new MachineInstr(ADD);
1978 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1979 subtreeRoot->getInstruction()->getOperand(forwardOperandNum));
1980 mvec[n]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
1981 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1982 subtreeRoot->getInstruction());
1983 }
1984 }
1985
1986 if (! ThisIsAChainRule(ruleForNode))
1987 numInstr = FixConstantOperands(subtreeRoot, mvec, numInstr, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001988
1989 return numInstr;
1990}
1991
1992