blob: ca2bca65847594ec9b462fd1837aa0b8979ab089 [file] [log] [blame]
Vikram S. Adve65682392001-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. Adve245b5b02001-08-28 23:12:57 +000010//**************************************************************************/
Vikram S. Adve65682392001-07-21 12:42:19 +000011
Chris Lattnercc38da72001-09-11 23:52:11 +000012#include "llvm/CodeGen/Sparc.h"
13#include "llvm/CodeGen/MachineInstr.h"
14#include "llvm/CodeGen/InstrForest.h"
15#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Adve245b5b02001-08-28 23:12:57 +000016#include "llvm/Support/MathExtras.h"
Vikram S. Adve65682392001-07-21 12:42:19 +000017#include "llvm/DerivedTypes.h"
Vikram S. Adve65682392001-07-21 12:42:19 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
20#include "llvm/iOther.h"
21#include "llvm/BasicBlock.h"
22#include "llvm/Method.h"
23#include "llvm/ConstPoolVals.h"
Vikram S. Adve65682392001-07-21 12:42:19 +000024
25
26//******************** Internal Data Declarations ************************/
27
28// to be used later
29struct BranchPattern {
30 bool flipCondition; // should the sense of the test be reversed
31 BasicBlock* targetBB; // which basic block to branch to
32 MachineInstr* extraBranch; // if neither branch is fall-through, then this
33 // BA must be inserted after the cond'l one
34};
35
36//************************* Forward Declarations ***************************/
37
38
Vikram S. Adve15046032001-07-28 04:15:15 +000039static MachineOpCode ChooseBprInstruction (const InstructionNode* instrNode);
Vikram S. Adve65682392001-07-21 12:42:19 +000040
Vikram S. Adve15046032001-07-28 04:15:15 +000041static MachineOpCode ChooseBccInstruction (const InstructionNode* instrNode,
42 bool& isFPBranch);
Vikram S. Adve65682392001-07-21 12:42:19 +000043
Vikram S. Adve15046032001-07-28 04:15:15 +000044static MachineOpCode ChooseBpccInstruction (const InstructionNode* instrNode,
45 const BinaryOperator* setCCInst);
Vikram S. Adve65682392001-07-21 12:42:19 +000046
Vikram S. Adve15046032001-07-28 04:15:15 +000047static MachineOpCode ChooseBFpccInstruction (const InstructionNode* instrNode,
48 const BinaryOperator* setCCInst);
Vikram S. Adve65682392001-07-21 12:42:19 +000049
Vikram S. Adve15046032001-07-28 04:15:15 +000050static MachineOpCode ChooseMovFpccInstruction(const InstructionNode*);
51
52static MachineOpCode ChooseMovpccAfterSub (const InstructionNode* instrNode,
53 bool& mustClearReg,
54 int& valueToMove);
55
56static MachineOpCode ChooseConvertToFloatInstr(const InstructionNode*,
Chris Lattner7cad6f12001-07-21 22:57:05 +000057 const Type* opType);
Vikram S. Adve65682392001-07-21 12:42:19 +000058
Vikram S. Adve15046032001-07-28 04:15:15 +000059static MachineOpCode ChooseConvertToIntInstr(const InstructionNode* instrNode,
60 const Type* opType);
Vikram S. Adve65682392001-07-21 12:42:19 +000061
Vikram S. Adve15046032001-07-28 04:15:15 +000062static MachineOpCode ChooseAddInstruction (const InstructionNode* instrNode);
Vikram S. Adve65682392001-07-21 12:42:19 +000063
Vikram S. Adve15046032001-07-28 04:15:15 +000064static MachineOpCode ChooseSubInstruction (const InstructionNode* instrNode);
Vikram S. Adve65682392001-07-21 12:42:19 +000065
Vikram S. Adve15046032001-07-28 04:15:15 +000066static MachineOpCode ChooseFcmpInstruction (const InstructionNode* instrNode);
Vikram S. Adve65682392001-07-21 12:42:19 +000067
Vikram S. Adve15046032001-07-28 04:15:15 +000068static MachineOpCode ChooseMulInstruction (const InstructionNode* instrNode,
69 bool checkCasts);
Vikram S. Adve65682392001-07-21 12:42:19 +000070
Vikram S. Adve15046032001-07-28 04:15:15 +000071static MachineOpCode ChooseDivInstruction (const InstructionNode* instrNode);
Vikram S. Adve65682392001-07-21 12:42:19 +000072
Vikram S. Adve15046032001-07-28 04:15:15 +000073static MachineOpCode ChooseLoadInstruction (const Type* resultType);
Vikram S. Adve65682392001-07-21 12:42:19 +000074
Vikram S. Adve15046032001-07-28 04:15:15 +000075static MachineOpCode ChooseStoreInstruction (const Type* valueType);
Vikram S. Adve65682392001-07-21 12:42:19 +000076
Vikram S. Adve15046032001-07-28 04:15:15 +000077static void SetOperandsForMemInstr(MachineInstr* minstr,
Vikram S. Adve65682392001-07-21 12:42:19 +000078 const InstructionNode* vmInstrNode,
Vikram S. Adve15046032001-07-28 04:15:15 +000079 const TargetMachine& target);
Vikram S. Adve65682392001-07-21 12:42:19 +000080
81static void SetMemOperands_Internal (MachineInstr* minstr,
82 const InstructionNode* vmInstrNode,
83 Value* ptrVal,
84 Value* arrayOffsetVal,
85 const vector<ConstPoolVal*>& idxVec,
Vikram S. Adve15046032001-07-28 04:15:15 +000086 const TargetMachine& target);
Vikram S. Adve65682392001-07-21 12:42:19 +000087
88static unsigned FixConstantOperands(const InstructionNode* vmInstrNode,
Vikram S. Adve15046032001-07-28 04:15:15 +000089 MachineInstr** mvec,
90 unsigned numInstr,
91 TargetMachine& target);
Vikram S. Adve65682392001-07-21 12:42:19 +000092
Vikram S. Adve245b5b02001-08-28 23:12:57 +000093static MachineInstr* MakeLoadConstInstr(Instruction* vmInstr,
94 Value* val,
95 TmpInstruction*& tmpReg,
96 MachineInstr*& getMinstr2);
Vikram S. Adve15046032001-07-28 04:15:15 +000097
98static void ForwardOperand (InstructionNode* treeNode,
99 InstructionNode* parent,
100 int operandNum);
Vikram S. Adve65682392001-07-21 12:42:19 +0000101
102
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000103//************************ Internal Functions ******************************/
Vikram S. Adve65682392001-07-21 12:42:19 +0000104
Chris Lattnera461b052001-09-09 23:01:47 +0000105// Convenience function to get the value of an integer constant, for an
106// appropriate integer or non-integer type that can be held in an integer.
107// The type of the argument must be the following:
108// GetConstantValueAsSignedInt: any of the above, but the value
109// must fit into a int64_t.
110//
111// isValidConstant is set to true if a valid constant was found.
112//
113
114static int64_t GetConstantValueAsSignedInt(const Value *V,
115 bool &isValidConstant) {
116 if (!V->isConstant()) { isValidConstant = false; return 0; }
117 isValidConstant = true;
118
119 if (V->getType() == Type::BoolTy)
120 return ((ConstPoolBool*)V)->getValue();
121 if (V->getType()->isIntegral()) {
122 if (V->getType()->isSigned())
123 return ((ConstPoolSInt*)V)->getValue();
124
125 assert(V->getType()->isUnsigned());
126 uint64_t Val = ((ConstPoolUInt*)V)->getValue();
127
128 if (Val < INT64_MAX) // then safe to cast to signed
129 return (int64_t)Val;
130 }
131
132 isValidConstant = false;
133 return 0;
134}
135
136
Vikram S. Adve65682392001-07-21 12:42:19 +0000137
138//------------------------------------------------------------------------
139// External Function: ThisIsAChainRule
140//
141// Purpose:
142// Check if a given BURG rule is a chain rule.
143//------------------------------------------------------------------------
144
145extern bool
146ThisIsAChainRule(int eruleno)
147{
148 switch(eruleno)
149 {
150 case 111: // stmt: reg
151 case 112: // stmt: boolconst
152 case 113: // stmt: bool
153 case 121:
154 case 122:
155 case 123:
156 case 124:
157 case 125:
158 case 126:
159 case 127:
160 case 128:
161 case 129:
162 case 130:
163 case 131:
164 case 132:
Vikram S. Advee3358212001-07-31 21:46:57 +0000165 case 153:
166 case 155: return true; break;
Vikram S. Adve65682392001-07-21 12:42:19 +0000167
168 default: return false; break;
169 }
170}
171
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000172
173static inline MachineOpCode
174ChooseBprInstruction(const InstructionNode* instrNode)
175{
176 MachineOpCode opCode;
177
178 Instruction* setCCInstr =
179 ((InstructionNode*) instrNode->leftChild())->getInstruction();
180
181 switch(setCCInstr->getOpcode())
182 {
183 case Instruction::SetEQ: opCode = BRZ; break;
184 case Instruction::SetNE: opCode = BRNZ; break;
185 case Instruction::SetLE: opCode = BRLEZ; break;
186 case Instruction::SetGE: opCode = BRGEZ; break;
187 case Instruction::SetLT: opCode = BRLZ; break;
188 case Instruction::SetGT: opCode = BRGZ; break;
189 default:
190 assert(0 && "Unrecognized VM instruction!");
191 opCode = INVALID_OPCODE;
192 break;
193 }
194
195 return opCode;
196}
197
198
199static inline MachineOpCode
200ChooseBccInstruction(const InstructionNode* instrNode,
201 bool& isFPBranch)
202{
203 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
204 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
205 const Type* setCCType = setCCInstr->getOperand(0)->getType();
206
207 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
208
209 if (isFPBranch)
210 return ChooseBFpccInstruction(instrNode, setCCInstr);
211 else
212 return ChooseBpccInstruction(instrNode, setCCInstr);
213}
214
215
216static inline MachineOpCode
217ChooseBpccInstruction(const InstructionNode* instrNode,
218 const BinaryOperator* setCCInstr)
219{
220 MachineOpCode opCode = INVALID_OPCODE;
221
222 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
223
224 if (isSigned)
225 {
226 switch(setCCInstr->getOpcode())
227 {
228 case Instruction::SetEQ: opCode = BE; break;
229 case Instruction::SetNE: opCode = BNE; break;
230 case Instruction::SetLE: opCode = BLE; break;
231 case Instruction::SetGE: opCode = BGE; break;
232 case Instruction::SetLT: opCode = BL; break;
233 case Instruction::SetGT: opCode = BG; break;
234 default:
235 assert(0 && "Unrecognized VM instruction!");
236 break;
237 }
238 }
239 else
240 {
241 switch(setCCInstr->getOpcode())
242 {
243 case Instruction::SetEQ: opCode = BE; break;
244 case Instruction::SetNE: opCode = BNE; break;
245 case Instruction::SetLE: opCode = BLEU; break;
246 case Instruction::SetGE: opCode = BCC; break;
247 case Instruction::SetLT: opCode = BCS; break;
248 case Instruction::SetGT: opCode = BGU; break;
249 default:
250 assert(0 && "Unrecognized VM instruction!");
251 break;
252 }
253 }
254
255 return opCode;
256}
257
258static inline MachineOpCode
259ChooseBFpccInstruction(const InstructionNode* instrNode,
260 const BinaryOperator* setCCInstr)
261{
262 MachineOpCode opCode = INVALID_OPCODE;
263
264 switch(setCCInstr->getOpcode())
265 {
266 case Instruction::SetEQ: opCode = FBE; break;
267 case Instruction::SetNE: opCode = FBNE; break;
268 case Instruction::SetLE: opCode = FBLE; break;
269 case Instruction::SetGE: opCode = FBGE; break;
270 case Instruction::SetLT: opCode = FBL; break;
271 case Instruction::SetGT: opCode = FBG; break;
272 default:
273 assert(0 && "Unrecognized VM instruction!");
274 break;
275 }
276
277 return opCode;
278}
279
280
281static inline MachineOpCode
282ChooseMovFpccInstruction(const InstructionNode* instrNode)
283{
284 MachineOpCode opCode = INVALID_OPCODE;
285
286 switch(instrNode->getInstruction()->getOpcode())
287 {
288 case Instruction::SetEQ: opCode = MOVFE; break;
289 case Instruction::SetNE: opCode = MOVFNE; break;
290 case Instruction::SetLE: opCode = MOVFLE; break;
291 case Instruction::SetGE: opCode = MOVFGE; break;
292 case Instruction::SetLT: opCode = MOVFL; break;
293 case Instruction::SetGT: opCode = MOVFG; break;
294 default:
295 assert(0 && "Unrecognized VM instruction!");
296 break;
297 }
298
299 return opCode;
300}
301
302
303// Assumes that SUBcc v1, v2 -> v3 has been executed.
304// In most cases, we want to clear v3 and then follow it by instruction
305// MOVcc 1 -> v3.
306// Set mustClearReg=false if v3 need not be cleared before conditional move.
307// Set valueToMove=0 if we want to conditionally move 0 instead of 1
308// (i.e., we want to test inverse of a condition)
309//
310//
311static MachineOpCode
312ChooseMovpccAfterSub(const InstructionNode* instrNode,
313 bool& mustClearReg,
314 int& valueToMove)
315{
316 MachineOpCode opCode = INVALID_OPCODE;
317 mustClearReg = true;
318 valueToMove = 1;
319
320 switch(instrNode->getInstruction()->getOpcode())
321 {
322 case Instruction::SetEQ: opCode = MOVNE; mustClearReg = false;
323 valueToMove = 0; break;
324 case Instruction::SetLE: opCode = MOVLE; break;
325 case Instruction::SetGE: opCode = MOVGE; break;
326 case Instruction::SetLT: opCode = MOVL; break;
327 case Instruction::SetGT: opCode = MOVG; break;
328
329 case Instruction::SetNE: assert(0 && "No move required!");
330
331 default:
332 assert(0 && "Unrecognized VM instruction!");
333 break;
334 }
335
336 return opCode;
337}
338
339
340static inline MachineOpCode
341ChooseConvertToFloatInstr(const InstructionNode* instrNode,
342 const Type* opType)
343{
344 MachineOpCode opCode = INVALID_OPCODE;
345
346 switch(instrNode->getOpLabel())
347 {
348 case ToFloatTy:
349 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
350 opCode = FITOS;
351 else if (opType == Type::LongTy)
352 opCode = FXTOS;
353 else if (opType == Type::DoubleTy)
354 opCode = FDTOS;
Vikram S. Adveb606fcb2001-09-09 20:35:34 +0000355 else if (opType == Type::FloatTy)
356 ;
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000357 else
Vikram S. Adveb606fcb2001-09-09 20:35:34 +0000358 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000359 break;
360
361 case ToDoubleTy:
362 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
363 opCode = FITOD;
364 else if (opType == Type::LongTy)
365 opCode = FXTOD;
366 else if (opType == Type::FloatTy)
367 opCode = FSTOD;
Vikram S. Adveb606fcb2001-09-09 20:35:34 +0000368 else if (opType == Type::DoubleTy)
369 ;
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000370 else
Vikram S. Adveb606fcb2001-09-09 20:35:34 +0000371 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000372 break;
373
374 default:
375 break;
376 }
377
378 return opCode;
379}
380
381static inline MachineOpCode
382ChooseConvertToIntInstr(const InstructionNode* instrNode,
383 const Type* opType)
384{
385 MachineOpCode opCode = INVALID_OPCODE;;
386
387 int instrType = (int) instrNode->getOpLabel();
388
389 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
390 {
391 switch (opType->getPrimitiveID())
392 {
393 case Type::FloatTyID: opCode = FSTOI; break;
394 case Type::DoubleTyID: opCode = FDTOI; break;
395 default:
396 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
397 break;
398 }
399 }
400 else if (instrType == ToLongTy)
401 {
402 switch (opType->getPrimitiveID())
403 {
404 case Type::FloatTyID: opCode = FSTOX; break;
405 case Type::DoubleTyID: opCode = FDTOX; break;
406 default:
407 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
408 break;
409 }
410 }
411 else
412 assert(0 && "Should not get here, Mo!");
413
414 return opCode;
415}
416
417
418static inline MachineOpCode
419ChooseAddInstruction(const InstructionNode* instrNode)
420{
421 MachineOpCode opCode = INVALID_OPCODE;
422
423 const Type* resultType = instrNode->getInstruction()->getType();
424
425 if (resultType->isIntegral() ||
426 resultType->isPointerType() ||
427 resultType->isMethodType() ||
428 resultType->isLabelType())
429 {
430 opCode = ADD;
431 }
432 else
433 {
434 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
435 switch(operand->getType()->getPrimitiveID())
436 {
437 case Type::FloatTyID: opCode = FADDS; break;
438 case Type::DoubleTyID: opCode = FADDD; break;
439 default: assert(0 && "Invalid type for ADD instruction"); break;
440 }
441 }
442
443 return opCode;
444}
445
446
447static inline MachineInstr*
448CreateMovFloatInstruction(const InstructionNode* instrNode,
449 const Type* resultType)
450{
451 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
452 ? FMOVS : FMOVD);
453 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
454 instrNode->leftChild()->getValue());
455 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
456 instrNode->getValue());
457 return minstr;
458}
459
460static inline MachineInstr*
461CreateAddConstInstruction(const InstructionNode* instrNode)
462{
463 MachineInstr* minstr = NULL;
464
465 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere014d792001-09-10 20:10:26 +0000466 assert(constOp->isConstant());
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000467
468 // Cases worth optimizing are:
469 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
470 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
471 //
472 const Type* resultType = instrNode->getInstruction()->getType();
473
Chris Lattnere014d792001-09-10 20:10:26 +0000474 if (resultType == Type::FloatTy || resultType == Type::DoubleTy) {
475 double dval = ((ConstPoolFP*) constOp)->getValue();
476 if (dval == 0.0)
477 minstr = CreateMovFloatInstruction(instrNode, resultType);
478 }
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000479
480 return minstr;
481}
482
483
484static inline MachineOpCode
485ChooseSubInstruction(const InstructionNode* instrNode)
486{
487 MachineOpCode opCode = INVALID_OPCODE;
488
489 const Type* resultType = instrNode->getInstruction()->getType();
490
491 if (resultType->isIntegral() ||
492 resultType->isPointerType())
493 {
494 opCode = SUB;
495 }
496 else
497 {
498 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
499 switch(operand->getType()->getPrimitiveID())
500 {
501 case Type::FloatTyID: opCode = FSUBS; break;
502 case Type::DoubleTyID: opCode = FSUBD; break;
503 default: assert(0 && "Invalid type for SUB instruction"); break;
504 }
505 }
506
507 return opCode;
508}
509
510
511static inline MachineInstr*
512CreateSubConstInstruction(const InstructionNode* instrNode)
513{
514 MachineInstr* minstr = NULL;
515
516 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere014d792001-09-10 20:10:26 +0000517 assert(constOp->isConstant());
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000518
519 // Cases worth optimizing are:
520 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
521 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
522 //
523 const Type* resultType = instrNode->getInstruction()->getType();
524
525 if (resultType == Type::FloatTy ||
526 resultType == Type::DoubleTy)
527 {
528 double dval = ((ConstPoolFP*) constOp)->getValue();
529 if (dval == 0.0)
530 minstr = CreateMovFloatInstruction(instrNode, resultType);
531 }
532
533 return minstr;
534}
535
536
537static inline MachineOpCode
538ChooseFcmpInstruction(const InstructionNode* instrNode)
539{
540 MachineOpCode opCode = INVALID_OPCODE;
541
542 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
543 switch(operand->getType()->getPrimitiveID())
544 {
545 case Type::FloatTyID: opCode = FCMPS; break;
546 case Type::DoubleTyID: opCode = FCMPD; break;
547 default: assert(0 && "Invalid type for FCMP instruction"); break;
548 }
549
550 return opCode;
551}
552
553
554// Assumes that leftArg and rightArg are both cast instructions.
555//
556static inline bool
557BothFloatToDouble(const InstructionNode* instrNode)
558{
559 InstrTreeNode* leftArg = instrNode->leftChild();
560 InstrTreeNode* rightArg = instrNode->rightChild();
561 InstrTreeNode* leftArgArg = leftArg->leftChild();
562 InstrTreeNode* rightArgArg = rightArg->leftChild();
563 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
564
565 // Check if both arguments are floats cast to double
566 return (leftArg->getValue()->getType() == Type::DoubleTy &&
567 leftArgArg->getValue()->getType() == Type::FloatTy &&
568 rightArgArg->getValue()->getType() == Type::FloatTy);
569}
570
571
572static inline MachineOpCode
573ChooseMulInstruction(const InstructionNode* instrNode,
574 bool checkCasts)
575{
576 MachineOpCode opCode = INVALID_OPCODE;
577
578 if (checkCasts && BothFloatToDouble(instrNode))
579 {
580 return opCode = FSMULD;
581 }
582 // else fall through and use the regular multiply instructions
583
584 const Type* resultType = instrNode->getInstruction()->getType();
585
586 if (resultType->isIntegral())
587 {
588 opCode = MULX;
589 }
590 else
591 {
592 switch(instrNode->leftChild()->getValue()->getType()->getPrimitiveID())
593 {
594 case Type::FloatTyID: opCode = FMULS; break;
595 case Type::DoubleTyID: opCode = FMULD; break;
596 default: assert(0 && "Invalid type for MUL instruction"); break;
597 }
598 }
599
600 return opCode;
601}
602
603
604static inline MachineInstr*
605CreateIntNegInstruction(Value* vreg)
606{
607 MachineInstr* minstr = new MachineInstr(SUB);
608 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
609 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
610 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
611 return minstr;
612}
613
614
615static inline MachineInstr*
616CreateMulConstInstruction(const InstructionNode* instrNode,
617 MachineInstr*& getMinstr2)
618{
619 MachineInstr* minstr = NULL;
620 getMinstr2 = NULL;
621 bool needNeg = false;
622
623 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere014d792001-09-10 20:10:26 +0000624 assert(constOp->isConstant());
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000625
626 // Cases worth optimizing are:
627 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
628 // (2) Multiply by 2^x for integer types: replace with Shift
629 //
630 const Type* resultType = instrNode->getInstruction()->getType();
631
632 if (resultType->isIntegral())
633 {
634 unsigned pow;
635 bool isValidConst;
636 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
637 if (isValidConst)
638 {
639 bool needNeg = false;
640 if (C < 0)
641 {
642 needNeg = true;
643 C = -C;
644 }
645
646 if (C == 0 || C == 1)
647 {
648 minstr = new MachineInstr(ADD);
649
650 if (C == 0)
651 minstr->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
652 else
653 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
654 instrNode->leftChild()->getValue());
655 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
656 }
657 else if (IsPowerOf2(C, pow))
658 {
659 minstr = new MachineInstr((resultType == Type::LongTy)
660 ? SLLX : SLL);
661 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
662 instrNode->leftChild()->getValue());
663 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
664 pow);
665 }
666
667 if (minstr && needNeg)
668 { // insert <reg = SUB 0, reg> after the instr to flip the sign
669 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
670 }
671 }
672 }
673 else
674 {
675 if (resultType == Type::FloatTy ||
676 resultType == Type::DoubleTy)
677 {
678 bool isValidConst;
679 double dval = ((ConstPoolFP*) constOp)->getValue();
680
681 if (isValidConst)
682 {
683 if (dval == 0)
684 {
685 minstr = new MachineInstr((resultType == Type::FloatTy)
686 ? FITOS : FITOD);
687 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
688 }
689 else if (fabs(dval) == 1)
690 {
691 bool needNeg = (dval < 0);
692
693 MachineOpCode opCode = needNeg
694 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
695 : (resultType == Type::FloatTy? FMOVS : FMOVD);
696
697 minstr = new MachineInstr(opCode);
698 minstr->SetMachineOperand(0,
699 MachineOperand::MO_VirtualRegister,
700 instrNode->leftChild()->getValue());
701 }
702 }
703 }
704 }
705
706 if (minstr != NULL)
707 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
708 instrNode->getValue());
709
710 return minstr;
711}
712
713
714static inline MachineOpCode
715ChooseDivInstruction(const InstructionNode* instrNode)
716{
717 MachineOpCode opCode = INVALID_OPCODE;
718
719 const Type* resultType = instrNode->getInstruction()->getType();
720
721 if (resultType->isIntegral())
722 {
723 opCode = resultType->isSigned()? SDIVX : UDIVX;
724 }
725 else
726 {
727 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
728 switch(operand->getType()->getPrimitiveID())
729 {
730 case Type::FloatTyID: opCode = FDIVS; break;
731 case Type::DoubleTyID: opCode = FDIVD; break;
732 default: assert(0 && "Invalid type for DIV instruction"); break;
733 }
734 }
735
736 return opCode;
737}
738
739
740static inline MachineInstr*
741CreateDivConstInstruction(const InstructionNode* instrNode,
742 MachineInstr*& getMinstr2)
743{
744 MachineInstr* minstr = NULL;
745 getMinstr2 = NULL;
746
747 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere014d792001-09-10 20:10:26 +0000748 assert(constOp->isConstant());
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000749
750 // Cases worth optimizing are:
751 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
752 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
753 //
754 const Type* resultType = instrNode->getInstruction()->getType();
755
756 if (resultType->isIntegral())
757 {
758 unsigned pow;
759 bool isValidConst;
760 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
761 if (isValidConst)
762 {
763 bool needNeg = false;
764 if (C < 0)
765 {
766 needNeg = true;
767 C = -C;
768 }
769
770 if (C == 1)
771 {
772 minstr = new MachineInstr(ADD);
773 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
774 instrNode->leftChild()->getValue());
775 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
776 }
777 else if (IsPowerOf2(C, pow))
778 {
779 MachineOpCode opCode= ((resultType->isSigned())
780 ? (resultType==Type::LongTy)? SRAX : SRA
781 : (resultType==Type::LongTy)? SRLX : SRL);
782 minstr = new MachineInstr(opCode);
783 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
784 instrNode->leftChild()->getValue());
785 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
786 pow);
787 }
788
789 if (minstr && needNeg)
790 { // insert <reg = SUB 0, reg> after the instr to flip the sign
791 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
792 }
793 }
794 }
795 else
796 {
797 if (resultType == Type::FloatTy ||
798 resultType == Type::DoubleTy)
799 {
800 bool isValidConst;
801 double dval = ((ConstPoolFP*) constOp)->getValue();
802
803 if (isValidConst && fabs(dval) == 1)
804 {
805 bool needNeg = (dval < 0);
806
807 MachineOpCode opCode = needNeg
808 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
809 : (resultType == Type::FloatTy? FMOVS : FMOVD);
810
811 minstr = new MachineInstr(opCode);
812 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
813 instrNode->leftChild()->getValue());
814 }
815 }
816 }
817
818 if (minstr != NULL)
819 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
820 instrNode->getValue());
821
822 return minstr;
823}
824
825
826static inline MachineOpCode
827ChooseLoadInstruction(const Type* resultType)
828{
829 MachineOpCode opCode = INVALID_OPCODE;
830
831 switch (resultType->getPrimitiveID())
832 {
833 case Type::BoolTyID: opCode = LDUB; break;
834 case Type::UByteTyID: opCode = LDUB; break;
835 case Type::SByteTyID: opCode = LDSB; break;
836 case Type::UShortTyID: opCode = LDUH; break;
837 case Type::ShortTyID: opCode = LDSH; break;
838 case Type::UIntTyID: opCode = LDUW; break;
839 case Type::IntTyID: opCode = LDSW; break;
840 case Type::ULongTyID:
841 case Type::LongTyID: opCode = LDX; break;
842 case Type::FloatTyID: opCode = LD; break;
843 case Type::DoubleTyID: opCode = LDD; break;
844 default: assert(0 && "Invalid type for Load instruction"); break;
845 }
846
847 return opCode;
848}
849
850
851static inline MachineOpCode
852ChooseStoreInstruction(const Type* valueType)
853{
854 MachineOpCode opCode = INVALID_OPCODE;
855
856 switch (valueType->getPrimitiveID())
857 {
858 case Type::BoolTyID:
859 case Type::UByteTyID:
860 case Type::SByteTyID: opCode = STB; break;
861 case Type::UShortTyID:
862 case Type::ShortTyID: opCode = STH; break;
863 case Type::UIntTyID:
864 case Type::IntTyID: opCode = STW; break;
865 case Type::ULongTyID:
866 case Type::LongTyID: opCode = STX; break;
867 case Type::FloatTyID: opCode = ST; break;
868 case Type::DoubleTyID: opCode = STD; break;
869 default: assert(0 && "Invalid type for Store instruction"); break;
870 }
871
872 return opCode;
873}
874
875
876//------------------------------------------------------------------------
877// Function SetOperandsForMemInstr
878//
879// Choose addressing mode for the given load or store instruction.
880// Use [reg+reg] if it is an indexed reference, and the index offset is
881// not a constant or if it cannot fit in the offset field.
882// Use [reg+offset] in all other cases.
883//
884// This assumes that all array refs are "lowered" to one of these forms:
885// %x = load (subarray*) ptr, constant ; single constant offset
886// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
887// Generally, this should happen via strength reduction + LICM.
888// Also, strength reduction should take care of using the same register for
889// the loop index variable and an array index, when that is profitable.
890//------------------------------------------------------------------------
891
892static void
893SetOperandsForMemInstr(MachineInstr* minstr,
894 const InstructionNode* vmInstrNode,
895 const TargetMachine& target)
896{
897 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
898
899 // Variables to hold the index vector, ptr value, and offset value.
900 // The major work here is to extract these for all 3 instruction types
901 // and then call the common function SetMemOperands_Internal().
902 //
903 const vector<ConstPoolVal*>* idxVec = & memInst->getIndexVec();
904 vector<ConstPoolVal*>* newIdxVec = NULL;
905 Value* ptrVal;
906 Value* arrayOffsetVal = NULL;
907
908 // Test if a GetElemPtr instruction is being folded into this mem instrn.
909 // If so, it will be in the left child for Load and GetElemPtr,
910 // and in the right child for Store instructions.
911 //
912 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
913 ? vmInstrNode->rightChild()
914 : vmInstrNode->leftChild());
915
916 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
917 ptrChild->getOpLabel() == GetElemPtrIdx)
918 {
919 // There is a GetElemPtr instruction and there may be a chain of
920 // more than one. Use the pointer value of the last one in the chain.
921 // Fold the index vectors from the entire chain and from the mem
922 // instruction into one single index vector.
923 // Finally, we never fold for an array instruction so make that NULL.
924
925 newIdxVec = new vector<ConstPoolVal*>;
926 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
927
928 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
929 idxVec = newIdxVec;
930
931 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
932 && "GetElemPtr cannot be folded into array refs in selection");
933 }
934 else
935 {
936 // There is no GetElemPtr instruction.
937 // Use the pointer value and the index vector from the Mem instruction.
938 // If it is an array reference, get the array offset value.
939 //
940 ptrVal = memInst->getPtrOperand();
941
942 const Type* opType =
943 ((const PointerType*) ptrVal->getType())->getValueType();
944 if (opType->isArrayType())
945 {
946 assert((memInst->getNumOperands()
947 == (unsigned) 1 + memInst->getFirstOffsetIdx())
948 && "Array refs must be lowered before Instruction Selection");
949
950 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
951 }
952 }
953
954 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
955 *idxVec, target);
956
957 if (newIdxVec != NULL)
958 delete newIdxVec;
959}
960
961
962static void
963SetMemOperands_Internal(MachineInstr* minstr,
964 const InstructionNode* vmInstrNode,
965 Value* ptrVal,
966 Value* arrayOffsetVal,
967 const vector<ConstPoolVal*>& idxVec,
968 const TargetMachine& target)
969{
970 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
971
972 // Initialize so we default to storing the offset in a register.
973 int64_t smallConstOffset;
974 Value* valueForRegOffset = NULL;
975 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
976
977 // Check if there is an index vector and if so, if it translates to
978 // a small enough constant to fit in the immediate-offset field.
979 //
980 if (idxVec.size() > 0)
981 {
982 bool isConstantOffset = false;
983 unsigned offset;
984
985 const PointerType* ptrType = (PointerType*) ptrVal->getType();
986
987 if (ptrType->getValueType()->isStructType())
988 {
989 // the offset is always constant for structs
990 isConstantOffset = true;
991
992 // Compute the offset value using the index vector
993 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
994 }
995 else
996 {
997 // It must be an array ref. Check if the offset is a constant,
998 // and that the indexing has been lowered to a single offset.
999 //
1000 assert(ptrType->getValueType()->isArrayType());
1001 assert(arrayOffsetVal != NULL
1002 && "Expect to be given Value* for array offsets");
1003
1004 if (ConstPoolVal *CPV = arrayOffsetVal->castConstant())
1005 {
1006 isConstantOffset = true; // always constant for structs
1007 assert(arrayOffsetVal->getType()->isIntegral());
1008 offset = (CPV->getType()->isSigned()
1009 ? ((ConstPoolSInt*)CPV)->getValue()
1010 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
1011 }
1012 else
1013 {
1014 valueForRegOffset = arrayOffsetVal;
1015 }
1016 }
1017
1018 if (isConstantOffset)
1019 {
1020 // create a virtual register for the constant
Chris Lattner064286b2001-09-07 21:22:57 +00001021 valueForRegOffset = ConstPoolSInt::get(Type::IntTy, offset);
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001022 }
1023 }
1024 else
1025 {
1026 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1027 smallConstOffset = 0;
1028 }
1029
1030 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
1031 // It is the left child in the instruction tree in all cases.
1032 Value* leftVal = vmInstrNode->leftChild()->getValue();
1033 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
1034
1035 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
1036 // Operand 3 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
1037 //
1038 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
1039 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1040 {
1041 assert(valueForRegOffset != NULL);
1042 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
1043 }
1044 else
1045 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
1046
1047 if (memInst->getOpcode() == Instruction::Store)
1048 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
1049 else
1050 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1051 vmInstrNode->getValue());
1052}
1053
1054
1055// Special handling for constant operands:
1056// -- if the constant is 0, use the hardwired 0 register, if any;
1057// -- if the constant is of float or double type but has an integer value,
1058// use int-to-float conversion instruction instead of generating a load;
1059// -- if the constant fits in the IMMEDIATE field, use that field;
1060// -- else insert instructions to put the constant into a register, either
1061// directly or by loading explicitly from the constant pool.
1062//
1063static unsigned
1064FixConstantOperands(const InstructionNode* vmInstrNode,
1065 MachineInstr** mvec,
1066 unsigned numInstr,
1067 TargetMachine& target)
1068{
1069 static MachineInstr* loadConstVec[MAX_INSTR_PER_VMINSTR];
1070
1071 unsigned numNew = 0;
1072 Instruction* vmInstr = vmInstrNode->getInstruction();
1073
1074 for (unsigned i=0; i < numInstr; i++)
1075 {
1076 MachineInstr* minstr = mvec[i];
1077 const MachineInstrDescriptor& instrDesc =
1078 target.getInstrInfo().getDescriptor(minstr->getOpCode());
1079
1080 for (unsigned op=0; op < minstr->getNumOperands(); op++)
1081 {
1082 const MachineOperand& mop = minstr->getOperand(op);
1083
1084 // skip the result position (for efficiency below) and any other
1085 // positions already marked as not a virtual register
1086 if (instrDesc.resultPos == (int) op ||
1087 mop.getOperandType() != MachineOperand::MO_VirtualRegister ||
1088 mop.getVRegValue() == NULL)
1089 {
1090 break;
1091 }
1092
1093 Value* opValue = mop.getVRegValue();
1094
Chris Lattnere014d792001-09-10 20:10:26 +00001095 if (opValue->isConstant())
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001096 {
1097 unsigned int machineRegNum;
1098 int64_t immedValue;
1099 MachineOperand::MachineOperandType opType =
1100 ChooseRegOrImmed(opValue, minstr->getOpCode(), target,
1101 /*canUseImmed*/ (op == 1),
1102 machineRegNum, immedValue);
1103
1104 if (opType == MachineOperand::MO_MachineRegister)
1105 minstr->SetMachineOperand(op, machineRegNum);
1106 else if (opType == MachineOperand::MO_VirtualRegister)
1107 {
1108 // value is constant and must be loaded into a register
1109 TmpInstruction* tmpReg;
1110 MachineInstr* minstr2;
1111 loadConstVec[numNew++] = MakeLoadConstInstr(vmInstr, opValue,
1112 tmpReg, minstr2);
1113 minstr->SetMachineOperand(op, opType, tmpReg);
1114 if (minstr2 != NULL)
1115 loadConstVec[numNew++] = minstr2;
1116 }
1117 else
1118 minstr->SetMachineOperand(op, opType, immedValue);
1119 }
1120 }
1121 }
1122
1123 if (numNew > 0)
1124 {
1125 // Insert the new instructions *before* the old ones by moving
1126 // the old ones over `numNew' positions (last-to-first, of course!).
1127 // We do check *after* returning that we did not exceed the vector mvec.
1128 for (int i=numInstr-1; i >= 0; i--)
1129 mvec[i+numNew] = mvec[i];
1130
1131 for (unsigned i=0; i < numNew; i++)
1132 mvec[i] = loadConstVec[i];
1133 }
1134
1135 return (numInstr + numNew);
1136}
1137
1138
1139static inline MachineInstr*
1140MakeIntSetInstruction(int64_t C, bool isSigned, Value* dest)
1141{
1142 MachineInstr* minstr;
1143 if (isSigned)
1144 {
1145 minstr = new MachineInstr(SETSW);
1146 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
1147 }
1148 else
1149 {
1150 minstr = new MachineInstr(SETUW);
1151 minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed, C);
1152 }
1153
1154 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
1155
1156 return minstr;
1157}
1158
1159
1160static MachineInstr*
1161MakeLoadConstInstr(Instruction* vmInstr,
1162 Value* val,
1163 TmpInstruction*& tmpReg,
1164 MachineInstr*& getMinstr2)
1165{
Chris Lattnere014d792001-09-10 20:10:26 +00001166 assert(val->isConstant());
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001167
1168 MachineInstr* minstr;
1169
1170 getMinstr2 = NULL;
1171
1172 // Create a TmpInstruction to mark the hidden register used for the constant
1173 tmpReg = new TmpInstruction(Instruction::UserOp1, val, NULL);
1174 vmInstr->getMachineInstrVec().addTempValue(tmpReg);
1175
1176 // Use a "set" instruction for known constants that can go in an integer reg.
1177 // Use a "set" instruction followed by a int-to-float conversion for known
1178 // constants that must go in a floating point reg but have an integer value.
1179 // Use a "load" instruction for all other constants, in particular,
1180 // floating point constants.
1181 //
1182 const Type* valType = val->getType();
1183
1184 if (valType->isIntegral() ||
1185 valType->isPointerType() ||
1186 valType == Type::BoolTy)
1187 {
1188 bool isValidConstant;
1189 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
1190 assert(isValidConstant && "Unrecognized constant");
1191
1192 minstr = MakeIntSetInstruction(C, valType->isSigned(), tmpReg);
1193 }
1194 else
1195 {
1196 assert(valType == Type::FloatTy || valType == Type::DoubleTy);
1197 double dval = ((ConstPoolFP*) val)->getValue();
1198 if (dval == (int64_t) dval)
1199 {
1200 // The constant actually has an integer value, so use a
1201 // [set; int-to-float] sequence instead of a load instruction.
1202 //
1203 TmpInstruction* tmpReg2 = NULL;
1204 if (dval != 0.0)
1205 { // First, create an integer constant of the same value as dval
Chris Lattner064286b2001-09-07 21:22:57 +00001206 ConstPoolSInt* ival = ConstPoolSInt::get(Type::IntTy,
1207 (int64_t) dval);
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001208 // Create another TmpInstruction for the hidden integer register
1209 TmpInstruction* tmpReg2 =
1210 new TmpInstruction(Instruction::UserOp1, ival, NULL);
1211 vmInstr->getMachineInstrVec().addTempValue(tmpReg2);
1212
1213 // Create the `SET' instruction
1214 minstr = MakeIntSetInstruction((int64_t)dval, true, tmpReg2);
1215 }
1216
1217 // In which variable do we put the second instruction?
1218 MachineInstr*& instr2 = (minstr)? getMinstr2 : minstr;
1219
1220 // Create the int-to-float instruction
1221 instr2 = new MachineInstr(valType == Type::FloatTy? FITOS : FITOD);
1222
1223 if (dval == 0.0)
1224 instr2->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
1225 else
1226 instr2->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
1227 tmpReg2);
1228
1229 instr2->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1230 tmpReg);
1231 }
1232 else
1233 {
1234 // Make a Load instruction, and make `val' both the ptr value *and*
1235 // the result value, and set the offset field to 0. Final code
1236 // generation will have to generate the base+offset for the constant.
1237 //
1238 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
1239 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
1240 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,val);
1241 minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1242 zeroOffset);
1243 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1244 tmpReg);
1245 }
1246 }
1247
1248 tmpReg->addMachineInstruction(minstr);
1249
1250 assert(minstr);
1251 return minstr;
1252}
1253
1254//
1255// Substitute operand `operandNum' of the instruction in node `treeNode'
1256// in place the use(s) of that instruction in node `parent'.
1257//
1258static void
1259ForwardOperand(InstructionNode* treeNode,
1260 InstructionNode* parent,
1261 int operandNum)
1262{
1263 Instruction* unusedOp = treeNode->getInstruction();
1264 Value* fwdOp = unusedOp->getOperand(operandNum);
1265 Instruction* userInstr = parent->getInstruction();
1266 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
1267 for (unsigned i=0, N=mvec.size(); i < N; i++)
1268 {
1269 MachineInstr* minstr = mvec[i];
1270 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; i++)
1271 {
1272 const MachineOperand& mop = minstr->getOperand(i);
1273 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1274 mop.getVRegValue() == unusedOp)
1275 {
1276 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
1277 fwdOp);
1278 }
1279 }
1280 }
1281}
1282
1283
1284// This function is currently unused and incomplete but will be
1285// used if we have a linear layout of basic blocks in LLVM code.
1286// It decides which branch should fall-through, and whether an
1287// extra unconditional branch is needed (when neither falls through).
1288//
1289void
1290ChooseBranchPattern(Instruction* vmInstr, BranchPattern& brPattern)
1291{
1292 BranchInst* brInstr = (BranchInst*) vmInstr;
1293
1294 brPattern.flipCondition = false;
1295 brPattern.targetBB = brInstr->getSuccessor(0);
1296 brPattern.extraBranch = NULL;
1297
1298 assert(brInstr->getNumSuccessors() > 1 &&
1299 "Unnecessary analysis for unconditional branch");
1300
1301 assert(0 && "Fold branches in peephole optimization");
1302}
1303
1304
1305//******************* Externally Visible Functions *************************/
1306
1307
Vikram S. Adve65682392001-07-21 12:42:19 +00001308//------------------------------------------------------------------------
1309// External Function: GetInstructionsByRule
1310//
1311// Purpose:
1312// Choose machine instructions for the SPARC according to the
1313// patterns chosen by the BURG-generated parser.
1314//------------------------------------------------------------------------
1315
1316unsigned
1317GetInstructionsByRule(InstructionNode* subtreeRoot,
1318 int ruleForNode,
1319 short* nts,
Vikram S. Adve15046032001-07-28 04:15:15 +00001320 TargetMachine &target,
Vikram S. Adve65682392001-07-21 12:42:19 +00001321 MachineInstr** mvec)
1322{
1323 int numInstr = 1; // initialize for common case
1324 bool checkCast = false; // initialize here to use fall-through
1325 Value *leftVal, *rightVal;
1326 const Type* opType;
1327 int nextRule;
Vikram S. Adve15046032001-07-28 04:15:15 +00001328 int forwardOperandNum = -1;
Vikram S. Adve65682392001-07-21 12:42:19 +00001329 BranchPattern brPattern;
Vikram S. Adve15046032001-07-28 04:15:15 +00001330 int64_t s0 = 0; // variables holding zero to avoid
1331 uint64_t u0 = 0; // overloading ambiguities below
Vikram S. Adve65682392001-07-21 12:42:19 +00001332
1333 mvec[0] = mvec[1] = mvec[2] = mvec[3] = NULL; // just for safety
1334
1335 switch(ruleForNode) {
1336 case 1: // stmt: Ret
1337 case 2: // stmt: RetValue(reg)
1338 // NOTE: Prepass of register allocation is responsible
1339 // for moving return value to appropriate register.
1340 // Mark the return-address register as a hidden virtual reg.
1341 {
1342 Instruction* returnReg = new TmpInstruction(Instruction::UserOp1,
1343 subtreeRoot->getInstruction(), NULL);
1344 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(returnReg);
1345
1346 mvec[0] = new MachineInstr(RETURN);
Vikram S. Adve15046032001-07-28 04:15:15 +00001347 mvec[0]->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,returnReg);
1348 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1349
1350 returnReg->addMachineInstruction(mvec[0]);
1351
Vikram S. Adve65682392001-07-21 12:42:19 +00001352 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1353 break;
1354 }
1355
1356 case 3: // stmt: Store(reg,reg)
1357 case 4: // stmt: Store(reg,ptrreg)
1358 mvec[0] = new MachineInstr(ChooseStoreInstruction(subtreeRoot->leftChild()->getValue()->getType()));
Vikram S. Adve15046032001-07-28 04:15:15 +00001359 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001360 break;
1361
1362 case 5: // stmt: BrUncond
1363 mvec[0] = new MachineInstr(BA);
Vikram S. Adve15046032001-07-28 04:15:15 +00001364 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, (Value*)NULL);
1365 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve65682392001-07-21 12:42:19 +00001366 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1367
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001368 // delay slot
1369 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adve65682392001-07-21 12:42:19 +00001370 break;
1371
1372 case 6: // stmt: BrCond(boolconst)
1373 // boolconst => boolean was computed with `%b = setCC type reg1 constant'
1374 // If the constant is ZERO, we can use the branch-on-integer-register
1375 // instructions and avoid the SUBcc instruction entirely.
1376 // Otherwise this is just the same as case 5, so just fall through.
1377 {
1378 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1379 assert(constNode && constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1380 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
Vikram S. Adve15046032001-07-28 04:15:15 +00001381 bool isValidConst;
Vikram S. Adve65682392001-07-21 12:42:19 +00001382
1383 if (constVal->getType()->isIntegral()
Vikram S. Adve15046032001-07-28 04:15:15 +00001384 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1385 && isValidConst)
Vikram S. Adve65682392001-07-21 12:42:19 +00001386 {
Vikram S. Adve15046032001-07-28 04:15:15 +00001387 // That constant ia a zero after all...
Vikram S. Adve65682392001-07-21 12:42:19 +00001388 // Use the left child of the setCC instruction as the first argument!
1389 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
Vikram S. Adve15046032001-07-28 04:15:15 +00001390 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve65682392001-07-21 12:42:19 +00001391 subtreeRoot->leftChild()->leftChild()->getValue());
1392 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1393 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1394
Vikram S. Adve15046032001-07-28 04:15:15 +00001395 // delay slot
1396 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adve65682392001-07-21 12:42:19 +00001397
Vikram S. Adve15046032001-07-28 04:15:15 +00001398 // false branch
1399 mvec[numInstr++] = new MachineInstr(BA);
1400 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1401 (Value*) NULL);
1402 mvec[numInstr-1]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001403
1404 // delay slot
1405 mvec[numInstr++] = new MachineInstr(NOP);
1406
Vikram S. Adve65682392001-07-21 12:42:19 +00001407 break;
1408 }
1409 // ELSE FALL THROUGH
1410 }
1411
1412 case 7: // stmt: BrCond(bool)
1413 // bool => boolean was computed with `%b = setcc type reg1 reg2'
1414 // Need to check whether the type was a FP, signed int or unsigned int,
Vikram S. Adve15046032001-07-28 04:15:15 +00001415 // and check the branching condition in order to choose the branch to use.
Vikram S. Adve65682392001-07-21 12:42:19 +00001416 //
1417 {
1418 bool isFPBranch;
1419 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve15046032001-07-28 04:15:15 +00001420 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1421 subtreeRoot->leftChild()->getValue());
1422 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve65682392001-07-21 12:42:19 +00001423 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1424
Vikram S. Adve15046032001-07-28 04:15:15 +00001425 // delay slot
1426 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adve65682392001-07-21 12:42:19 +00001427
Vikram S. Adve15046032001-07-28 04:15:15 +00001428 // 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));
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001434
1435 // delay slot
1436 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adve65682392001-07-21 12:42:19 +00001437 break;
1438 }
Vikram S. Adve15046032001-07-28 04:15:15 +00001439
Vikram S. Adve65682392001-07-21 12:42:19 +00001440 case 8: // stmt: BrCond(boolreg)
1441 // bool => boolean is stored in an existing register.
1442 // Just use the branch-on-integer-register instruction!
1443 //
1444 mvec[0] = new MachineInstr(BRNZ);
Vikram S. Adve15046032001-07-28 04:15:15 +00001445 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1446 subtreeRoot->leftChild()->getValue());
Vikram S. Adve65682392001-07-21 12:42:19 +00001447 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1448 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001449
1450 // delay slot
Vikram S. Adve65682392001-07-21 12:42:19 +00001451 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001452
1453 // false branch
1454 mvec[numInstr++] = new MachineInstr(BA);
1455 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1456 (Value*) NULL);
1457 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1458 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1459
1460 // delay slot
1461 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adve65682392001-07-21 12:42:19 +00001462 break;
1463
1464 case 9: // stmt: Switch(reg)
1465 assert(0 && "*** SWITCH instruction is not implemented yet.");
1466 numInstr = 0;
1467 break;
1468
1469 case 10: // reg: VRegList(reg, reg)
1470 assert(0 && "VRegList should never be the topmost non-chain rule");
1471 break;
1472
1473 case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0
1474 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve15046032001-07-28 04:15:15 +00001475 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve65682392001-07-21 12:42:19 +00001476 subtreeRoot->leftChild()->getValue());
1477 mvec[0]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve15046032001-07-28 04:15:15 +00001478 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve65682392001-07-21 12:42:19 +00001479 subtreeRoot->getValue());
1480 break;
1481
1482 case 22: // reg: ToBoolTy(reg):
1483 opType = subtreeRoot->leftChild()->getValue()->getType();
1484 assert(opType->isIntegral() || opType == Type::BoolTy);
1485 numInstr = 0;
Vikram S. Adve15046032001-07-28 04:15:15 +00001486 forwardOperandNum = 0;
Vikram S. Adve65682392001-07-21 12:42:19 +00001487 break;
1488
1489 case 23: // reg: ToUByteTy(reg)
1490 case 25: // reg: ToUShortTy(reg)
1491 case 27: // reg: ToUIntTy(reg)
1492 case 29: // reg: ToULongTy(reg)
1493 opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Advee3358212001-07-31 21:46:57 +00001494 assert(opType->isIntegral() ||
1495 opType->isPointerType() ||
1496 opType == Type::BoolTy && "Ignoring cast: illegal for other types");
Vikram S. Adve65682392001-07-21 12:42:19 +00001497 numInstr = 0;
Vikram S. Adve15046032001-07-28 04:15:15 +00001498 forwardOperandNum = 0;
Vikram S. Adve65682392001-07-21 12:42:19 +00001499 break;
1500
1501 case 24: // reg: ToSByteTy(reg)
1502 case 26: // reg: ToShortTy(reg)
1503 case 28: // reg: ToIntTy(reg)
1504 case 30: // reg: ToLongTy(reg)
1505 opType = subtreeRoot->leftChild()->getValue()->getType();
1506 if (opType->isIntegral() || opType == Type::BoolTy)
Vikram S. Adve15046032001-07-28 04:15:15 +00001507 {
1508 numInstr = 0;
1509 forwardOperandNum = 0;
1510 }
Vikram S. Adve65682392001-07-21 12:42:19 +00001511 else
1512 {
1513 mvec[0] =new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,opType));
Vikram S. Adve15046032001-07-28 04:15:15 +00001514 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001515 }
1516 break;
1517
1518 case 31: // reg: ToFloatTy(reg):
1519 case 32: // reg: ToDoubleTy(reg):
1520
1521 // If this instruction has a parent (a user) in the tree
1522 // and the user is translated as an FsMULd instruction,
1523 // then the cast is unnecessary. So check that first.
1524 // In the future, we'll want to do the same for the FdMULq instruction,
1525 // so do the check here instead of only for ToFloatTy(reg).
1526 //
1527 if (subtreeRoot->parent() != NULL &&
1528 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1529 {
1530 numInstr = 0;
Vikram S. Adve15046032001-07-28 04:15:15 +00001531 forwardOperandNum = 0;
Vikram S. Adve65682392001-07-21 12:42:19 +00001532 }
1533 else
1534 {
1535 opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adveb606fcb2001-09-09 20:35:34 +00001536 MachineOpCode opCode = ChooseConvertToFloatInstr(subtreeRoot, opType);
1537 if (opCode == INVALID_OPCODE) // no conversion needed
1538 {
1539 numInstr = 0;
1540 forwardOperandNum = 0;
1541 }
1542 else
1543 {
1544 mvec[0] = new MachineInstr(opCode);
1545 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1546 }
Vikram S. Adve65682392001-07-21 12:42:19 +00001547 }
1548 break;
1549
1550 case 19: // reg: ToArrayTy(reg):
1551 case 20: // reg: ToPointerTy(reg):
1552 numInstr = 0;
Vikram S. Adve15046032001-07-28 04:15:15 +00001553 forwardOperandNum = 0;
Vikram S. Adve65682392001-07-21 12:42:19 +00001554 break;
1555
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001556 case 233: // reg: Add(reg, Constant)
1557 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1558 if (mvec[0] != NULL)
1559 break;
1560 // ELSE FALL THROUGH
1561
Vikram S. Adve65682392001-07-21 12:42:19 +00001562 case 33: // reg: Add(reg, reg)
1563 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
Vikram S. Adve15046032001-07-28 04:15:15 +00001564 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001565 break;
1566
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001567 case 234: // reg: Sub(reg, Constant)
1568 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1569 if (mvec[0] != NULL)
1570 break;
1571 // ELSE FALL THROUGH
1572
Vikram S. Adve65682392001-07-21 12:42:19 +00001573 case 34: // reg: Sub(reg, reg)
1574 mvec[0] = new MachineInstr(ChooseSubInstruction(subtreeRoot));
Vikram S. Adve15046032001-07-28 04:15:15 +00001575 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001576 break;
1577
1578 case 135: // reg: Mul(todouble, todouble)
1579 checkCast = true;
1580 // FALL THROUGH
1581
1582 case 35: // reg: Mul(reg, reg)
1583 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
Vikram S. Adve15046032001-07-28 04:15:15 +00001584 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001585 break;
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001586
1587 case 335: // reg: Mul(todouble, todoubleConst)
1588 checkCast = true;
1589 // FALL THROUGH
1590
1591 case 235: // reg: Mul(reg, Constant)
1592 mvec[0] = CreateMulConstInstruction(subtreeRoot, mvec[1]);
1593 if (mvec[0] == NULL)
1594 {
1595 mvec[0]=new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
1596 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1597 }
1598 else
1599 if (mvec[1] != NULL)
1600 ++numInstr;
1601 break;
1602
1603 case 236: // reg: Div(reg, Constant)
1604 mvec[0] = CreateDivConstInstruction(subtreeRoot, mvec[1]);
1605 if (mvec[0] != NULL)
1606 {
1607 if (mvec[1] != NULL)
1608 ++numInstr;
1609 }
1610 else
1611 // ELSE FALL THROUGH
Vikram S. Adve65682392001-07-21 12:42:19 +00001612
1613 case 36: // reg: Div(reg, reg)
1614 mvec[0] = new MachineInstr(ChooseDivInstruction(subtreeRoot));
Vikram S. Adve15046032001-07-28 04:15:15 +00001615 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001616 break;
1617
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001618 case 37: // reg: Rem(reg, reg)
1619 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve65682392001-07-21 12:42:19 +00001620 assert(0 && "REM instruction unimplemented for the SPARC.");
1621 break;
1622
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001623 case 38: // reg: And(reg, reg)
1624 case 238: // reg: And(reg, Constant)
Vikram S. Adve65682392001-07-21 12:42:19 +00001625 mvec[0] = new MachineInstr(AND);
Vikram S. Adve15046032001-07-28 04:15:15 +00001626 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001627 break;
1628
1629 case 138: // reg: And(reg, not)
1630 mvec[0] = new MachineInstr(ANDN);
Vikram S. Adve15046032001-07-28 04:15:15 +00001631 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001632 break;
1633
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001634 case 39: // reg: Or(reg, reg)
1635 case 239: // reg: Or(reg, Constant)
Vikram S. Adve65682392001-07-21 12:42:19 +00001636 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve15046032001-07-28 04:15:15 +00001637 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001638 break;
1639
1640 case 139: // reg: Or(reg, not)
1641 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve15046032001-07-28 04:15:15 +00001642 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001643 break;
1644
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001645 case 40: // reg: Xor(reg, reg)
1646 case 240: // reg: Xor(reg, Constant)
Vikram S. Adve65682392001-07-21 12:42:19 +00001647 mvec[0] = new MachineInstr(XOR);
Vikram S. Adve15046032001-07-28 04:15:15 +00001648 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001649 break;
1650
1651 case 140: // reg: Xor(reg, not)
1652 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve15046032001-07-28 04:15:15 +00001653 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001654 break;
1655
1656 case 41: // boolconst: SetCC(reg, Constant)
1657 // Check if this is an integer comparison, and
1658 // there is a parent, and the parent decided to use
1659 // a branch-on-integer-register instead of branch-on-condition-code.
1660 // If so, the SUBcc instruction is not required.
1661 // (However, we must still check for constants to be loaded from
1662 // the constant pool so that such a load can be associated with
1663 // this instruction.)
1664 //
Vikram S. Adve15046032001-07-28 04:15:15 +00001665 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Adve65682392001-07-21 12:42:19 +00001666 //
1667 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() &&
1668 subtreeRoot->parent() != NULL)
1669 {
1670 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1671 assert(parentNode->getNodeType() == InstrTreeNode::NTInstructionNode);
1672 const vector<MachineInstr*>&
1673 minstrVec = parentNode->getInstruction()->getMachineInstrVec();
1674 MachineOpCode parentOpCode;
Vikram S. Adve15046032001-07-28 04:15:15 +00001675 if (parentNode->getInstruction()->getOpcode() == Instruction::Br &&
Vikram S. Adve65682392001-07-21 12:42:19 +00001676 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1677 parentOpCode <= BRGEZ)
1678 {
Vikram S. Adve15046032001-07-28 04:15:15 +00001679 numInstr = 0; // don't forward the operand!
Vikram S. Adve65682392001-07-21 12:42:19 +00001680 break;
1681 }
1682 }
1683 // ELSE FALL THROUGH
1684
1685 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve15046032001-07-28 04:15:15 +00001686 {
1687 // If result of the SetCC is only used for a branch, we can
1688 // discard the result. otherwise, it must go into an integer register.
1689 // Note that the user may or may not be in the same tree, so we have
1690 // to follow SSA def-use edges here, not BURG tree edges.
1691 //
1692 Instruction* result = subtreeRoot->getInstruction();
1693 Value* firstUse = (Value*) * result->use_begin();
1694 bool discardResult =
1695 (result->use_size() == 1
Chris Lattnere014d792001-09-10 20:10:26 +00001696 && firstUse->isInstruction()
Vikram S. Adve15046032001-07-28 04:15:15 +00001697 && ((Instruction*) firstUse)->getOpcode() == Instruction::Br);
1698
1699 bool mustClearReg;
1700 int valueToMove;
1701 MachineOpCode movOpCode;
1702
Vikram S. Adve65682392001-07-21 12:42:19 +00001703 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral())
1704 {
Vikram S. Adve15046032001-07-28 04:15:15 +00001705 // integer condition: destination should be %g0 or integer register
1706 // if result must be saved but condition is not SetEQ then we need
1707 // a separate instruction to compute the bool result, so discard
1708 // result of SUBcc instruction anyway.
1709 //
Vikram S. Adve65682392001-07-21 12:42:19 +00001710 mvec[0] = new MachineInstr(SUBcc);
Vikram S. Adve15046032001-07-28 04:15:15 +00001711 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, discardResult);
1712
Vikram S. Adve22db4f92001-08-06 21:06:10 +00001713 // mark the 4th operand as being a CC register, and a "result"
1714 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
1715 subtreeRoot->getValue(), /*def*/ true);
1716
1717 if (!discardResult)
Vikram S. Adve15046032001-07-28 04:15:15 +00001718 { // recompute bool if needed, using the integer condition codes
1719 if (result->getOpcode() == Instruction::SetNE)
1720 discardResult = true;
1721 else
1722 movOpCode =
1723 ChooseMovpccAfterSub(subtreeRoot, mustClearReg, valueToMove);
1724 }
Vikram S. Adve65682392001-07-21 12:42:19 +00001725 }
1726 else
1727 {
Vikram S. Adve15046032001-07-28 04:15:15 +00001728 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve65682392001-07-21 12:42:19 +00001729 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
Vikram S. Adve65682392001-07-21 12:42:19 +00001730 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adve15046032001-07-28 04:15:15 +00001731 subtreeRoot->getValue());
1732 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1733 subtreeRoot->leftChild()->getValue());
1734 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1735 subtreeRoot->rightChild()->getValue());
1736
1737 if (!discardResult)
1738 {// recompute bool using the FP condition codes
1739 mustClearReg = true;
1740 valueToMove = 1;
1741 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1742 }
1743 }
1744
1745 if (!discardResult)
1746 {
1747 if (mustClearReg)
1748 {// Unconditionally set register to 0
1749 int n = numInstr++;
1750 mvec[n] = new MachineInstr(SETHI);
1751 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,s0);
1752 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1753 subtreeRoot->getValue());
1754 }
1755
1756 // Now conditionally move `valueToMove' (0 or 1) into the register
1757 int n = numInstr++;
1758 mvec[n] = new MachineInstr(movOpCode);
1759 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1760 subtreeRoot->getValue());
1761 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1762 valueToMove);
1763 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1764 subtreeRoot->getValue());
Vikram S. Adve65682392001-07-21 12:42:19 +00001765 }
1766 break;
Vikram S. Adve15046032001-07-28 04:15:15 +00001767 }
1768
Vikram S. Adve65682392001-07-21 12:42:19 +00001769 case 43: // boolreg: VReg
Vikram S. Advee3358212001-07-31 21:46:57 +00001770 case 44: // boolreg: Constant
Vikram S. Adve65682392001-07-21 12:42:19 +00001771 numInstr = 0;
1772 break;
1773
1774 case 51: // reg: Load(reg)
1775 case 52: // reg: Load(ptrreg)
1776 case 53: // reg: LoadIdx(reg,reg)
1777 case 54: // reg: LoadIdx(ptrreg,reg)
1778 mvec[0] = new MachineInstr(ChooseLoadInstruction(subtreeRoot->getValue()->getType()));
Vikram S. Adve15046032001-07-28 04:15:15 +00001779 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001780 break;
1781
1782 case 55: // reg: GetElemPtr(reg)
1783 case 56: // reg: GetElemPtrIdx(reg,reg)
1784 if (subtreeRoot->parent() != NULL)
1785 {
1786 // Check if the parent was an array access.
1787 // If so, we still need to generate this instruction.
1788 MemAccessInst* memInst =(MemAccessInst*) subtreeRoot->getInstruction();
1789 const PointerType* ptrType =
1790 (const PointerType*) memInst->getPtrOperand()->getType();
1791 if (! ptrType->getValueType()->isArrayType())
1792 {// we don't need a separate instr
Vikram S. Adve15046032001-07-28 04:15:15 +00001793 numInstr = 0; // don't forward operand!
Vikram S. Adve65682392001-07-21 12:42:19 +00001794 break;
1795 }
1796 }
1797 // else in all other cases we need to a separate ADD instruction
1798 mvec[0] = new MachineInstr(ADD);
Vikram S. Adve15046032001-07-28 04:15:15 +00001799 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001800 break;
1801
1802 case 57: // reg: Alloca: Implement as 2 instructions:
1803 // sub %sp, tmp -> %sp
1804 { // add %sp, 0 -> result
1805 Instruction* instr = subtreeRoot->getInstruction();
1806 const PointerType* instrType = (const PointerType*) instr->getType();
1807 assert(instrType->isPointerType());
Vikram S. Adve15046032001-07-28 04:15:15 +00001808 int tsize = (int) target.findOptimalStorageSize(instrType->getValueType());
1809 assert(tsize != 0 && "Just to check when this can happen");
1810 // if (tsize == 0)
1811 // {
1812 // numInstr = 0;
1813 // break;
1814 // }
Vikram S. Adve65682392001-07-21 12:42:19 +00001815 //else go on to create the instructions needed...
1816
1817 // Create a temporary Value to hold the constant type-size
Chris Lattner064286b2001-09-07 21:22:57 +00001818 ConstPoolSInt* valueForTSize = ConstPoolSInt::get(Type::IntTy, tsize);
Vikram S. Adve65682392001-07-21 12:42:19 +00001819
1820 // Instruction 1: sub %sp, tsize -> %sp
1821 // tsize is always constant, but it may have to be put into a
1822 // register if it doesn't fit in the immediate field.
1823 //
1824 mvec[0] = new MachineInstr(SUB);
1825 mvec[0]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
Vikram S. Adve15046032001-07-28 04:15:15 +00001826 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
Vikram S. Adve65682392001-07-21 12:42:19 +00001827 mvec[0]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1828
1829 // Instruction 2: add %sp, 0 -> result
1830 numInstr++;
1831 mvec[1] = new MachineInstr(ADD);
1832 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1833 mvec[1]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve15046032001-07-28 04:15:15 +00001834 mvec[1]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
Vikram S. Adve65682392001-07-21 12:42:19 +00001835 break;
1836 }
1837
1838 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1839 // mul num, typeSz -> tmp
1840 // sub %sp, tmp -> %sp
1841 { // add %sp, 0 -> result
1842 Instruction* instr = subtreeRoot->getInstruction();
1843 const PointerType* instrType = (const PointerType*) instr->getType();
1844 assert(instrType->isPointerType() &&
1845 instrType->getValueType()->isArrayType());
1846 const Type* eltType =
1847 ((ArrayType*) instrType->getValueType())->getElementType();
Vikram S. Adve15046032001-07-28 04:15:15 +00001848 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Adve65682392001-07-21 12:42:19 +00001849
Vikram S. Adve15046032001-07-28 04:15:15 +00001850 assert(tsize != 0 && "Just to check when this can happen");
1851 // if (tsize == 0)
1852 // {
1853 // numInstr = 0;
1854 // break;
1855 // }
Vikram S. Adve65682392001-07-21 12:42:19 +00001856 //else go on to create the instructions needed...
1857
1858 // Create a temporary Value to hold the constant type-size
Chris Lattner064286b2001-09-07 21:22:57 +00001859 ConstPoolSInt* valueForTSize = ConstPoolSInt::get(Type::IntTy, tsize);
Vikram S. Adve65682392001-07-21 12:42:19 +00001860
1861 // Create a temporary value to hold `tmp'
1862 Instruction* tmpInstr = new TmpInstruction(Instruction::UserOp1,
1863 subtreeRoot->leftChild()->getValue(),
1864 NULL /*could insert tsize here*/);
1865 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1866
1867 // Instruction 1: mul numElements, typeSize -> tmp
1868 mvec[0] = new MachineInstr(MULX);
Vikram S. Adve15046032001-07-28 04:15:15 +00001869 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve65682392001-07-21 12:42:19 +00001870 subtreeRoot->leftChild()->getValue());
Vikram S. Adve15046032001-07-28 04:15:15 +00001871 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
1872 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,tmpInstr);
Vikram S. Adve65682392001-07-21 12:42:19 +00001873
Vikram S. Adve15046032001-07-28 04:15:15 +00001874 tmpInstr->addMachineInstruction(mvec[0]);
1875
Vikram S. Adve65682392001-07-21 12:42:19 +00001876 // Instruction 2: sub %sp, tmp -> %sp
1877 numInstr++;
1878 mvec[1] = new MachineInstr(SUB);
1879 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
Vikram S. Adve15046032001-07-28 04:15:15 +00001880 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,tmpInstr);
Vikram S. Adve65682392001-07-21 12:42:19 +00001881 mvec[1]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1882
1883 // Instruction 3: add %sp, 0 -> result
1884 numInstr++;
1885 mvec[2] = new MachineInstr(ADD);
1886 mvec[2]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1887 mvec[2]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve15046032001-07-28 04:15:15 +00001888 mvec[2]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
Vikram S. Adve65682392001-07-21 12:42:19 +00001889 break;
1890 }
1891
1892 case 61: // reg: Call
1893 // Generate a call-indirect (i.e., JMPL) for now to expose
1894 // the potential need for registers. If an absolute address
1895 // is available, replace this with a CALL instruction.
1896 // Mark both the indirection register and the return-address
1897 { // register as hidden virtual registers.
1898
Vikram S. Adve15046032001-07-28 04:15:15 +00001899 Instruction* jmpAddrReg = new TmpInstruction(Instruction::UserOp1,
Vikram S. Adve65682392001-07-21 12:42:19 +00001900 ((CallInst*) subtreeRoot->getInstruction())->getCalledMethod(), NULL);
Vikram S. Adve15046032001-07-28 04:15:15 +00001901 Instruction* retAddrReg = new TmpInstruction(Instruction::UserOp1,
1902 subtreeRoot->getValue(), NULL);
1903 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(jmpAddrReg);
1904 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(retAddrReg);
Vikram S. Adve65682392001-07-21 12:42:19 +00001905
1906 mvec[0] = new MachineInstr(JMPL);
Vikram S. Adve15046032001-07-28 04:15:15 +00001907 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, jmpAddrReg);
Vikram S. Adve65682392001-07-21 12:42:19 +00001908 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1909 (int64_t) 0);
Vikram S. Adve15046032001-07-28 04:15:15 +00001910 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, retAddrReg);
1911
1912 // NOTE: jmpAddrReg will be loaded by a different instruction generated
1913 // by the final code generator, so we just mark the CALL instruction
1914 // as computing that value.
1915 // The retAddrReg is actually computed by the CALL instruction.
1916 //
1917 jmpAddrReg->addMachineInstruction(mvec[0]);
1918 retAddrReg->addMachineInstruction(mvec[0]);
Vikram S. Adve65682392001-07-21 12:42:19 +00001919
1920 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1921 break;
1922 }
1923
1924 case 62: // reg: Shl(reg, reg)
1925 opType = subtreeRoot->leftChild()->getValue()->getType();
1926 assert(opType->isIntegral() || opType == Type::BoolTy);
1927 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
Vikram S. Adve15046032001-07-28 04:15:15 +00001928 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001929 break;
1930
1931 case 63: // reg: Shr(reg, reg)
1932 opType = subtreeRoot->leftChild()->getValue()->getType();
1933 assert(opType->isIntegral() || opType == Type::BoolTy);
1934 mvec[0] = new MachineInstr((opType->isSigned()
1935 ? ((opType == Type::LongTy)? SRAX : SRA)
1936 : ((opType == Type::LongTy)? SRLX : SRL)));
Vikram S. Adve15046032001-07-28 04:15:15 +00001937 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001938 break;
1939
Vikram S. Advee3358212001-07-31 21:46:57 +00001940 case 64: // reg: Phi(reg,reg)
1941 { // This instruction has variable #operands, so resultPos is 0.
1942 Instruction* phi = subtreeRoot->getInstruction();
1943 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
1944 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001945 subtreeRoot->getValue());
Vikram S. Advee3358212001-07-31 21:46:57 +00001946 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
1947 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
1948 phi->getOperand(i));
1949 break;
1950 }
Vikram S. Adve65682392001-07-21 12:42:19 +00001951 case 71: // reg: VReg
1952 case 72: // reg: Constant
Vikram S. Adve15046032001-07-28 04:15:15 +00001953 numInstr = 0; // don't forward the value
Vikram S. Adve65682392001-07-21 12:42:19 +00001954 break;
1955
1956 case 111: // stmt: reg
1957 case 112: // stmt: boolconst
1958 case 113: // stmt: bool
1959 case 121:
1960 case 122:
1961 case 123:
1962 case 124:
1963 case 125:
1964 case 126:
1965 case 127:
1966 case 128:
1967 case 129:
1968 case 130:
1969 case 131:
1970 case 132:
1971 case 153:
Vikram S. Advee3358212001-07-31 21:46:57 +00001972 case 155:
Vikram S. Adve65682392001-07-21 12:42:19 +00001973 //
1974 // These are all chain rules, which have a single nonterminal on the RHS.
1975 // Get the rule that matches the RHS non-terminal and use that instead.
1976 //
1977 assert(ThisIsAChainRule(ruleForNode));
1978 assert(nts[0] && ! nts[1]
1979 && "A chain rule should have only one RHS non-terminal!");
Chris Lattnercc38da72001-09-11 23:52:11 +00001980 nextRule = burm_rule(subtreeRoot->state, nts[0]);
Vikram S. Adve65682392001-07-21 12:42:19 +00001981 nts = burm_nts[nextRule];
Vikram S. Adve15046032001-07-28 04:15:15 +00001982 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
Vikram S. Adve65682392001-07-21 12:42:19 +00001983 break;
1984
1985 default:
Vikram S. Adve15046032001-07-28 04:15:15 +00001986 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve65682392001-07-21 12:42:19 +00001987 numInstr = 0;
1988 break;
1989 }
1990
Vikram S. Adve15046032001-07-28 04:15:15 +00001991 if (forwardOperandNum >= 0)
1992 { // We did not generate a machine instruction but need to use operand.
1993 // If user is in the same tree, replace Value in its machine operand.
1994 // If not, insert a copy instruction which should get coalesced away
1995 // by register allocation.
1996 if (subtreeRoot->parent() != NULL)
1997 ForwardOperand(subtreeRoot, (InstructionNode*) subtreeRoot->parent(),
1998 forwardOperandNum);
1999 else
2000 {
2001 int n = numInstr++;
2002 mvec[n] = new MachineInstr(ADD);
2003 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
2004 subtreeRoot->getInstruction()->getOperand(forwardOperandNum));
2005 mvec[n]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
2006 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
2007 subtreeRoot->getInstruction());
2008 }
2009 }
2010
2011 if (! ThisIsAChainRule(ruleForNode))
2012 numInstr = FixConstantOperands(subtreeRoot, mvec, numInstr, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00002013
2014 return numInstr;
2015}
2016
2017