blob: ddee541ce6fd4be2f3731b089366e40f72dde146 [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();
Chris Lattner16dd09a2001-09-12 01:28:49 +0000543 switch(operand->getType()->getPrimitiveID()) {
544 case Type::FloatTyID: opCode = FCMPS; break;
545 case Type::DoubleTyID: opCode = FCMPD; break;
546 default: assert(0 && "Invalid type for FCMP instruction"); break;
547 }
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000548
549 return opCode;
550}
551
552
553// Assumes that leftArg and rightArg are both cast instructions.
554//
555static inline bool
556BothFloatToDouble(const InstructionNode* instrNode)
557{
558 InstrTreeNode* leftArg = instrNode->leftChild();
559 InstrTreeNode* rightArg = instrNode->rightChild();
560 InstrTreeNode* leftArgArg = leftArg->leftChild();
561 InstrTreeNode* rightArgArg = rightArg->leftChild();
562 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
563
564 // Check if both arguments are floats cast to double
565 return (leftArg->getValue()->getType() == Type::DoubleTy &&
566 leftArgArg->getValue()->getType() == Type::FloatTy &&
567 rightArgArg->getValue()->getType() == Type::FloatTy);
568}
569
570
571static inline MachineOpCode
572ChooseMulInstruction(const InstructionNode* instrNode,
573 bool checkCasts)
574{
575 MachineOpCode opCode = INVALID_OPCODE;
576
577 if (checkCasts && BothFloatToDouble(instrNode))
578 {
579 return opCode = FSMULD;
580 }
581 // else fall through and use the regular multiply instructions
582
583 const Type* resultType = instrNode->getInstruction()->getType();
584
585 if (resultType->isIntegral())
586 {
587 opCode = MULX;
588 }
589 else
590 {
591 switch(instrNode->leftChild()->getValue()->getType()->getPrimitiveID())
592 {
593 case Type::FloatTyID: opCode = FMULS; break;
594 case Type::DoubleTyID: opCode = FMULD; break;
595 default: assert(0 && "Invalid type for MUL instruction"); break;
596 }
597 }
598
599 return opCode;
600}
601
602
603static inline MachineInstr*
604CreateIntNegInstruction(Value* vreg)
605{
606 MachineInstr* minstr = new MachineInstr(SUB);
607 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
608 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
609 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
610 return minstr;
611}
612
613
614static inline MachineInstr*
615CreateMulConstInstruction(const InstructionNode* instrNode,
616 MachineInstr*& getMinstr2)
617{
618 MachineInstr* minstr = NULL;
619 getMinstr2 = NULL;
620 bool needNeg = false;
621
622 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere014d792001-09-10 20:10:26 +0000623 assert(constOp->isConstant());
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000624
625 // Cases worth optimizing are:
626 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
627 // (2) Multiply by 2^x for integer types: replace with Shift
628 //
629 const Type* resultType = instrNode->getInstruction()->getType();
630
631 if (resultType->isIntegral())
632 {
633 unsigned pow;
634 bool isValidConst;
635 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
636 if (isValidConst)
637 {
638 bool needNeg = false;
639 if (C < 0)
640 {
641 needNeg = true;
642 C = -C;
643 }
644
645 if (C == 0 || C == 1)
646 {
647 minstr = new MachineInstr(ADD);
648
649 if (C == 0)
650 minstr->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
651 else
652 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
653 instrNode->leftChild()->getValue());
654 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
655 }
656 else if (IsPowerOf2(C, pow))
657 {
658 minstr = new MachineInstr((resultType == Type::LongTy)
659 ? SLLX : SLL);
660 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
661 instrNode->leftChild()->getValue());
662 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
663 pow);
664 }
665
666 if (minstr && needNeg)
667 { // insert <reg = SUB 0, reg> after the instr to flip the sign
668 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
669 }
670 }
671 }
672 else
673 {
674 if (resultType == Type::FloatTy ||
675 resultType == Type::DoubleTy)
676 {
677 bool isValidConst;
678 double dval = ((ConstPoolFP*) constOp)->getValue();
679
680 if (isValidConst)
681 {
682 if (dval == 0)
683 {
684 minstr = new MachineInstr((resultType == Type::FloatTy)
685 ? FITOS : FITOD);
686 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
687 }
688 else if (fabs(dval) == 1)
689 {
690 bool needNeg = (dval < 0);
691
692 MachineOpCode opCode = needNeg
693 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
694 : (resultType == Type::FloatTy? FMOVS : FMOVD);
695
696 minstr = new MachineInstr(opCode);
697 minstr->SetMachineOperand(0,
698 MachineOperand::MO_VirtualRegister,
699 instrNode->leftChild()->getValue());
700 }
701 }
702 }
703 }
704
705 if (minstr != NULL)
706 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
707 instrNode->getValue());
708
709 return minstr;
710}
711
712
713static inline MachineOpCode
714ChooseDivInstruction(const InstructionNode* instrNode)
715{
716 MachineOpCode opCode = INVALID_OPCODE;
717
718 const Type* resultType = instrNode->getInstruction()->getType();
719
720 if (resultType->isIntegral())
721 {
722 opCode = resultType->isSigned()? SDIVX : UDIVX;
723 }
724 else
725 {
726 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
727 switch(operand->getType()->getPrimitiveID())
728 {
729 case Type::FloatTyID: opCode = FDIVS; break;
730 case Type::DoubleTyID: opCode = FDIVD; break;
731 default: assert(0 && "Invalid type for DIV instruction"); break;
732 }
733 }
734
735 return opCode;
736}
737
738
739static inline MachineInstr*
740CreateDivConstInstruction(const InstructionNode* instrNode,
741 MachineInstr*& getMinstr2)
742{
743 MachineInstr* minstr = NULL;
744 getMinstr2 = NULL;
745
746 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere014d792001-09-10 20:10:26 +0000747 assert(constOp->isConstant());
Vikram S. Adve245b5b02001-08-28 23:12:57 +0000748
749 // Cases worth optimizing are:
750 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
751 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
752 //
753 const Type* resultType = instrNode->getInstruction()->getType();
754
755 if (resultType->isIntegral())
756 {
757 unsigned pow;
758 bool isValidConst;
759 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
760 if (isValidConst)
761 {
762 bool needNeg = false;
763 if (C < 0)
764 {
765 needNeg = true;
766 C = -C;
767 }
768
769 if (C == 1)
770 {
771 minstr = new MachineInstr(ADD);
772 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
773 instrNode->leftChild()->getValue());
774 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
775 }
776 else if (IsPowerOf2(C, pow))
777 {
778 MachineOpCode opCode= ((resultType->isSigned())
779 ? (resultType==Type::LongTy)? SRAX : SRA
780 : (resultType==Type::LongTy)? SRLX : SRL);
781 minstr = new MachineInstr(opCode);
782 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
783 instrNode->leftChild()->getValue());
784 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
785 pow);
786 }
787
788 if (minstr && needNeg)
789 { // insert <reg = SUB 0, reg> after the instr to flip the sign
790 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
791 }
792 }
793 }
794 else
795 {
796 if (resultType == Type::FloatTy ||
797 resultType == Type::DoubleTy)
798 {
799 bool isValidConst;
800 double dval = ((ConstPoolFP*) constOp)->getValue();
801
802 if (isValidConst && fabs(dval) == 1)
803 {
804 bool needNeg = (dval < 0);
805
806 MachineOpCode opCode = needNeg
807 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
808 : (resultType == Type::FloatTy? FMOVS : FMOVD);
809
810 minstr = new MachineInstr(opCode);
811 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
812 instrNode->leftChild()->getValue());
813 }
814 }
815 }
816
817 if (minstr != NULL)
818 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
819 instrNode->getValue());
820
821 return minstr;
822}
823
824
825static inline MachineOpCode
826ChooseLoadInstruction(const Type* resultType)
827{
828 MachineOpCode opCode = INVALID_OPCODE;
829
830 switch (resultType->getPrimitiveID())
831 {
832 case Type::BoolTyID: opCode = LDUB; break;
833 case Type::UByteTyID: opCode = LDUB; break;
834 case Type::SByteTyID: opCode = LDSB; break;
835 case Type::UShortTyID: opCode = LDUH; break;
836 case Type::ShortTyID: opCode = LDSH; break;
837 case Type::UIntTyID: opCode = LDUW; break;
838 case Type::IntTyID: opCode = LDSW; break;
839 case Type::ULongTyID:
840 case Type::LongTyID: opCode = LDX; break;
841 case Type::FloatTyID: opCode = LD; break;
842 case Type::DoubleTyID: opCode = LDD; break;
843 default: assert(0 && "Invalid type for Load instruction"); break;
844 }
845
846 return opCode;
847}
848
849
850static inline MachineOpCode
851ChooseStoreInstruction(const Type* valueType)
852{
853 MachineOpCode opCode = INVALID_OPCODE;
854
855 switch (valueType->getPrimitiveID())
856 {
857 case Type::BoolTyID:
858 case Type::UByteTyID:
859 case Type::SByteTyID: opCode = STB; break;
860 case Type::UShortTyID:
861 case Type::ShortTyID: opCode = STH; break;
862 case Type::UIntTyID:
863 case Type::IntTyID: opCode = STW; break;
864 case Type::ULongTyID:
865 case Type::LongTyID: opCode = STX; break;
866 case Type::FloatTyID: opCode = ST; break;
867 case Type::DoubleTyID: opCode = STD; break;
868 default: assert(0 && "Invalid type for Store instruction"); break;
869 }
870
871 return opCode;
872}
873
874
875//------------------------------------------------------------------------
876// Function SetOperandsForMemInstr
877//
878// Choose addressing mode for the given load or store instruction.
879// Use [reg+reg] if it is an indexed reference, and the index offset is
880// not a constant or if it cannot fit in the offset field.
881// Use [reg+offset] in all other cases.
882//
883// This assumes that all array refs are "lowered" to one of these forms:
884// %x = load (subarray*) ptr, constant ; single constant offset
885// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
886// Generally, this should happen via strength reduction + LICM.
887// Also, strength reduction should take care of using the same register for
888// the loop index variable and an array index, when that is profitable.
889//------------------------------------------------------------------------
890
891static void
892SetOperandsForMemInstr(MachineInstr* minstr,
893 const InstructionNode* vmInstrNode,
894 const TargetMachine& target)
895{
896 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
897
898 // Variables to hold the index vector, ptr value, and offset value.
899 // The major work here is to extract these for all 3 instruction types
900 // and then call the common function SetMemOperands_Internal().
901 //
902 const vector<ConstPoolVal*>* idxVec = & memInst->getIndexVec();
903 vector<ConstPoolVal*>* newIdxVec = NULL;
904 Value* ptrVal;
905 Value* arrayOffsetVal = NULL;
906
907 // Test if a GetElemPtr instruction is being folded into this mem instrn.
908 // If so, it will be in the left child for Load and GetElemPtr,
909 // and in the right child for Store instructions.
910 //
911 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
912 ? vmInstrNode->rightChild()
913 : vmInstrNode->leftChild());
914
915 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
916 ptrChild->getOpLabel() == GetElemPtrIdx)
917 {
918 // There is a GetElemPtr instruction and there may be a chain of
919 // more than one. Use the pointer value of the last one in the chain.
920 // Fold the index vectors from the entire chain and from the mem
921 // instruction into one single index vector.
922 // Finally, we never fold for an array instruction so make that NULL.
923
924 newIdxVec = new vector<ConstPoolVal*>;
925 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
926
927 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
928 idxVec = newIdxVec;
929
930 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
931 && "GetElemPtr cannot be folded into array refs in selection");
932 }
933 else
934 {
935 // There is no GetElemPtr instruction.
936 // Use the pointer value and the index vector from the Mem instruction.
937 // If it is an array reference, get the array offset value.
938 //
939 ptrVal = memInst->getPtrOperand();
940
941 const Type* opType =
942 ((const PointerType*) ptrVal->getType())->getValueType();
943 if (opType->isArrayType())
944 {
945 assert((memInst->getNumOperands()
946 == (unsigned) 1 + memInst->getFirstOffsetIdx())
947 && "Array refs must be lowered before Instruction Selection");
948
949 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
950 }
951 }
952
953 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
954 *idxVec, target);
955
956 if (newIdxVec != NULL)
957 delete newIdxVec;
958}
959
960
961static void
962SetMemOperands_Internal(MachineInstr* minstr,
963 const InstructionNode* vmInstrNode,
964 Value* ptrVal,
965 Value* arrayOffsetVal,
966 const vector<ConstPoolVal*>& idxVec,
967 const TargetMachine& target)
968{
969 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
970
971 // Initialize so we default to storing the offset in a register.
972 int64_t smallConstOffset;
973 Value* valueForRegOffset = NULL;
974 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
975
976 // Check if there is an index vector and if so, if it translates to
977 // a small enough constant to fit in the immediate-offset field.
978 //
979 if (idxVec.size() > 0)
980 {
981 bool isConstantOffset = false;
982 unsigned offset;
983
984 const PointerType* ptrType = (PointerType*) ptrVal->getType();
985
986 if (ptrType->getValueType()->isStructType())
987 {
988 // the offset is always constant for structs
989 isConstantOffset = true;
990
991 // Compute the offset value using the index vector
992 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
993 }
994 else
995 {
996 // It must be an array ref. Check if the offset is a constant,
997 // and that the indexing has been lowered to a single offset.
998 //
999 assert(ptrType->getValueType()->isArrayType());
1000 assert(arrayOffsetVal != NULL
1001 && "Expect to be given Value* for array offsets");
1002
1003 if (ConstPoolVal *CPV = arrayOffsetVal->castConstant())
1004 {
1005 isConstantOffset = true; // always constant for structs
1006 assert(arrayOffsetVal->getType()->isIntegral());
1007 offset = (CPV->getType()->isSigned()
1008 ? ((ConstPoolSInt*)CPV)->getValue()
1009 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
1010 }
1011 else
1012 {
1013 valueForRegOffset = arrayOffsetVal;
1014 }
1015 }
1016
1017 if (isConstantOffset)
1018 {
1019 // create a virtual register for the constant
Chris Lattner064286b2001-09-07 21:22:57 +00001020 valueForRegOffset = ConstPoolSInt::get(Type::IntTy, offset);
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001021 }
1022 }
1023 else
1024 {
1025 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1026 smallConstOffset = 0;
1027 }
1028
1029 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
1030 // It is the left child in the instruction tree in all cases.
1031 Value* leftVal = vmInstrNode->leftChild()->getValue();
1032 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
1033
1034 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
1035 // Operand 3 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
1036 //
1037 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
1038 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1039 {
1040 assert(valueForRegOffset != NULL);
1041 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
1042 }
1043 else
1044 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
1045
1046 if (memInst->getOpcode() == Instruction::Store)
1047 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
1048 else
1049 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1050 vmInstrNode->getValue());
1051}
1052
1053
1054// Special handling for constant operands:
1055// -- if the constant is 0, use the hardwired 0 register, if any;
1056// -- if the constant is of float or double type but has an integer value,
1057// use int-to-float conversion instruction instead of generating a load;
1058// -- if the constant fits in the IMMEDIATE field, use that field;
1059// -- else insert instructions to put the constant into a register, either
1060// directly or by loading explicitly from the constant pool.
1061//
1062static unsigned
1063FixConstantOperands(const InstructionNode* vmInstrNode,
1064 MachineInstr** mvec,
1065 unsigned numInstr,
1066 TargetMachine& target)
1067{
1068 static MachineInstr* loadConstVec[MAX_INSTR_PER_VMINSTR];
1069
1070 unsigned numNew = 0;
1071 Instruction* vmInstr = vmInstrNode->getInstruction();
1072
1073 for (unsigned i=0; i < numInstr; i++)
1074 {
1075 MachineInstr* minstr = mvec[i];
1076 const MachineInstrDescriptor& instrDesc =
1077 target.getInstrInfo().getDescriptor(minstr->getOpCode());
1078
1079 for (unsigned op=0; op < minstr->getNumOperands(); op++)
1080 {
1081 const MachineOperand& mop = minstr->getOperand(op);
1082
1083 // skip the result position (for efficiency below) and any other
1084 // positions already marked as not a virtual register
1085 if (instrDesc.resultPos == (int) op ||
1086 mop.getOperandType() != MachineOperand::MO_VirtualRegister ||
1087 mop.getVRegValue() == NULL)
1088 {
1089 break;
1090 }
1091
1092 Value* opValue = mop.getVRegValue();
1093
Chris Lattnere014d792001-09-10 20:10:26 +00001094 if (opValue->isConstant())
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001095 {
1096 unsigned int machineRegNum;
1097 int64_t immedValue;
1098 MachineOperand::MachineOperandType opType =
1099 ChooseRegOrImmed(opValue, minstr->getOpCode(), target,
1100 /*canUseImmed*/ (op == 1),
1101 machineRegNum, immedValue);
1102
1103 if (opType == MachineOperand::MO_MachineRegister)
1104 minstr->SetMachineOperand(op, machineRegNum);
1105 else if (opType == MachineOperand::MO_VirtualRegister)
1106 {
1107 // value is constant and must be loaded into a register
1108 TmpInstruction* tmpReg;
1109 MachineInstr* minstr2;
1110 loadConstVec[numNew++] = MakeLoadConstInstr(vmInstr, opValue,
1111 tmpReg, minstr2);
1112 minstr->SetMachineOperand(op, opType, tmpReg);
1113 if (minstr2 != NULL)
1114 loadConstVec[numNew++] = minstr2;
1115 }
1116 else
1117 minstr->SetMachineOperand(op, opType, immedValue);
1118 }
1119 }
1120 }
1121
1122 if (numNew > 0)
1123 {
1124 // Insert the new instructions *before* the old ones by moving
1125 // the old ones over `numNew' positions (last-to-first, of course!).
1126 // We do check *after* returning that we did not exceed the vector mvec.
1127 for (int i=numInstr-1; i >= 0; i--)
1128 mvec[i+numNew] = mvec[i];
1129
1130 for (unsigned i=0; i < numNew; i++)
1131 mvec[i] = loadConstVec[i];
1132 }
1133
1134 return (numInstr + numNew);
1135}
1136
1137
1138static inline MachineInstr*
1139MakeIntSetInstruction(int64_t C, bool isSigned, Value* dest)
1140{
1141 MachineInstr* minstr;
1142 if (isSigned)
1143 {
1144 minstr = new MachineInstr(SETSW);
1145 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
1146 }
1147 else
1148 {
1149 minstr = new MachineInstr(SETUW);
1150 minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed, C);
1151 }
1152
1153 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
1154
1155 return minstr;
1156}
1157
1158
1159static MachineInstr*
1160MakeLoadConstInstr(Instruction* vmInstr,
1161 Value* val,
1162 TmpInstruction*& tmpReg,
1163 MachineInstr*& getMinstr2)
1164{
Chris Lattnere014d792001-09-10 20:10:26 +00001165 assert(val->isConstant());
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001166
1167 MachineInstr* minstr;
1168
1169 getMinstr2 = NULL;
1170
1171 // Create a TmpInstruction to mark the hidden register used for the constant
1172 tmpReg = new TmpInstruction(Instruction::UserOp1, val, NULL);
1173 vmInstr->getMachineInstrVec().addTempValue(tmpReg);
1174
1175 // Use a "set" instruction for known constants that can go in an integer reg.
1176 // Use a "set" instruction followed by a int-to-float conversion for known
1177 // constants that must go in a floating point reg but have an integer value.
1178 // Use a "load" instruction for all other constants, in particular,
1179 // floating point constants.
1180 //
1181 const Type* valType = val->getType();
1182
1183 if (valType->isIntegral() ||
1184 valType->isPointerType() ||
1185 valType == Type::BoolTy)
1186 {
1187 bool isValidConstant;
1188 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
1189 assert(isValidConstant && "Unrecognized constant");
1190
1191 minstr = MakeIntSetInstruction(C, valType->isSigned(), tmpReg);
1192 }
1193 else
1194 {
1195 assert(valType == Type::FloatTy || valType == Type::DoubleTy);
1196 double dval = ((ConstPoolFP*) val)->getValue();
1197 if (dval == (int64_t) dval)
1198 {
1199 // The constant actually has an integer value, so use a
1200 // [set; int-to-float] sequence instead of a load instruction.
1201 //
1202 TmpInstruction* tmpReg2 = NULL;
1203 if (dval != 0.0)
1204 { // First, create an integer constant of the same value as dval
Chris Lattner064286b2001-09-07 21:22:57 +00001205 ConstPoolSInt* ival = ConstPoolSInt::get(Type::IntTy,
1206 (int64_t) dval);
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001207 // Create another TmpInstruction for the hidden integer register
1208 TmpInstruction* tmpReg2 =
1209 new TmpInstruction(Instruction::UserOp1, ival, NULL);
1210 vmInstr->getMachineInstrVec().addTempValue(tmpReg2);
1211
1212 // Create the `SET' instruction
1213 minstr = MakeIntSetInstruction((int64_t)dval, true, tmpReg2);
1214 }
1215
1216 // In which variable do we put the second instruction?
1217 MachineInstr*& instr2 = (minstr)? getMinstr2 : minstr;
1218
1219 // Create the int-to-float instruction
1220 instr2 = new MachineInstr(valType == Type::FloatTy? FITOS : FITOD);
1221
1222 if (dval == 0.0)
1223 instr2->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
1224 else
1225 instr2->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
1226 tmpReg2);
1227
1228 instr2->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1229 tmpReg);
1230 }
1231 else
1232 {
1233 // Make a Load instruction, and make `val' both the ptr value *and*
1234 // the result value, and set the offset field to 0. Final code
1235 // generation will have to generate the base+offset for the constant.
1236 //
1237 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
1238 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
1239 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,val);
1240 minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1241 zeroOffset);
1242 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1243 tmpReg);
1244 }
1245 }
1246
1247 tmpReg->addMachineInstruction(minstr);
1248
1249 assert(minstr);
1250 return minstr;
1251}
1252
1253//
1254// Substitute operand `operandNum' of the instruction in node `treeNode'
1255// in place the use(s) of that instruction in node `parent'.
1256//
1257static void
1258ForwardOperand(InstructionNode* treeNode,
1259 InstructionNode* parent,
1260 int operandNum)
1261{
1262 Instruction* unusedOp = treeNode->getInstruction();
1263 Value* fwdOp = unusedOp->getOperand(operandNum);
1264 Instruction* userInstr = parent->getInstruction();
1265 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
1266 for (unsigned i=0, N=mvec.size(); i < N; i++)
1267 {
1268 MachineInstr* minstr = mvec[i];
1269 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; i++)
1270 {
1271 const MachineOperand& mop = minstr->getOperand(i);
1272 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1273 mop.getVRegValue() == unusedOp)
1274 {
1275 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
1276 fwdOp);
1277 }
1278 }
1279 }
1280}
1281
1282
1283// This function is currently unused and incomplete but will be
1284// used if we have a linear layout of basic blocks in LLVM code.
1285// It decides which branch should fall-through, and whether an
1286// extra unconditional branch is needed (when neither falls through).
1287//
1288void
1289ChooseBranchPattern(Instruction* vmInstr, BranchPattern& brPattern)
1290{
1291 BranchInst* brInstr = (BranchInst*) vmInstr;
1292
1293 brPattern.flipCondition = false;
1294 brPattern.targetBB = brInstr->getSuccessor(0);
1295 brPattern.extraBranch = NULL;
1296
1297 assert(brInstr->getNumSuccessors() > 1 &&
1298 "Unnecessary analysis for unconditional branch");
1299
1300 assert(0 && "Fold branches in peephole optimization");
1301}
1302
1303
1304//******************* Externally Visible Functions *************************/
1305
1306
Vikram S. Adve65682392001-07-21 12:42:19 +00001307//------------------------------------------------------------------------
1308// External Function: GetInstructionsByRule
1309//
1310// Purpose:
1311// Choose machine instructions for the SPARC according to the
1312// patterns chosen by the BURG-generated parser.
1313//------------------------------------------------------------------------
1314
1315unsigned
1316GetInstructionsByRule(InstructionNode* subtreeRoot,
1317 int ruleForNode,
1318 short* nts,
Vikram S. Adve15046032001-07-28 04:15:15 +00001319 TargetMachine &target,
Vikram S. Adve65682392001-07-21 12:42:19 +00001320 MachineInstr** mvec)
1321{
1322 int numInstr = 1; // initialize for common case
1323 bool checkCast = false; // initialize here to use fall-through
1324 Value *leftVal, *rightVal;
1325 const Type* opType;
1326 int nextRule;
Vikram S. Adve15046032001-07-28 04:15:15 +00001327 int forwardOperandNum = -1;
Vikram S. Adve65682392001-07-21 12:42:19 +00001328 BranchPattern brPattern;
Vikram S. Adve15046032001-07-28 04:15:15 +00001329 int64_t s0 = 0; // variables holding zero to avoid
1330 uint64_t u0 = 0; // overloading ambiguities below
Vikram S. Adve65682392001-07-21 12:42:19 +00001331
1332 mvec[0] = mvec[1] = mvec[2] = mvec[3] = NULL; // just for safety
1333
1334 switch(ruleForNode) {
1335 case 1: // stmt: Ret
1336 case 2: // stmt: RetValue(reg)
1337 // NOTE: Prepass of register allocation is responsible
1338 // for moving return value to appropriate register.
1339 // Mark the return-address register as a hidden virtual reg.
1340 {
1341 Instruction* returnReg = new TmpInstruction(Instruction::UserOp1,
1342 subtreeRoot->getInstruction(), NULL);
1343 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(returnReg);
1344
1345 mvec[0] = new MachineInstr(RETURN);
Vikram S. Adve15046032001-07-28 04:15:15 +00001346 mvec[0]->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,returnReg);
1347 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1348
1349 returnReg->addMachineInstruction(mvec[0]);
1350
Vikram S. Adve65682392001-07-21 12:42:19 +00001351 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1352 break;
1353 }
1354
1355 case 3: // stmt: Store(reg,reg)
1356 case 4: // stmt: Store(reg,ptrreg)
1357 mvec[0] = new MachineInstr(ChooseStoreInstruction(subtreeRoot->leftChild()->getValue()->getType()));
Vikram S. Adve15046032001-07-28 04:15:15 +00001358 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001359 break;
1360
1361 case 5: // stmt: BrUncond
1362 mvec[0] = new MachineInstr(BA);
Vikram S. Adve15046032001-07-28 04:15:15 +00001363 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, (Value*)NULL);
1364 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve65682392001-07-21 12:42:19 +00001365 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1366
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001367 // delay slot
1368 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adve65682392001-07-21 12:42:19 +00001369 break;
1370
1371 case 6: // stmt: BrCond(boolconst)
1372 // boolconst => boolean was computed with `%b = setCC type reg1 constant'
1373 // If the constant is ZERO, we can use the branch-on-integer-register
1374 // instructions and avoid the SUBcc instruction entirely.
1375 // Otherwise this is just the same as case 5, so just fall through.
1376 {
1377 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1378 assert(constNode && constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1379 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
Vikram S. Adve15046032001-07-28 04:15:15 +00001380 bool isValidConst;
Vikram S. Adve65682392001-07-21 12:42:19 +00001381
1382 if (constVal->getType()->isIntegral()
Vikram S. Adve15046032001-07-28 04:15:15 +00001383 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1384 && isValidConst)
Vikram S. Adve65682392001-07-21 12:42:19 +00001385 {
Vikram S. Adve15046032001-07-28 04:15:15 +00001386 // That constant ia a zero after all...
Vikram S. Adve65682392001-07-21 12:42:19 +00001387 // Use the left child of the setCC instruction as the first argument!
1388 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
Vikram S. Adve15046032001-07-28 04:15:15 +00001389 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve65682392001-07-21 12:42:19 +00001390 subtreeRoot->leftChild()->leftChild()->getValue());
1391 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1392 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1393
Vikram S. Adve15046032001-07-28 04:15:15 +00001394 // delay slot
1395 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adve65682392001-07-21 12:42:19 +00001396
Vikram S. Adve15046032001-07-28 04:15:15 +00001397 // false branch
1398 mvec[numInstr++] = new MachineInstr(BA);
1399 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1400 (Value*) NULL);
1401 mvec[numInstr-1]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001402
1403 // delay slot
1404 mvec[numInstr++] = new MachineInstr(NOP);
1405
Vikram S. Adve65682392001-07-21 12:42:19 +00001406 break;
1407 }
1408 // ELSE FALL THROUGH
1409 }
1410
1411 case 7: // stmt: BrCond(bool)
1412 // bool => boolean was computed with `%b = setcc type reg1 reg2'
1413 // Need to check whether the type was a FP, signed int or unsigned int,
Vikram S. Adve15046032001-07-28 04:15:15 +00001414 // and check the branching condition in order to choose the branch to use.
Vikram S. Adve65682392001-07-21 12:42:19 +00001415 //
1416 {
1417 bool isFPBranch;
1418 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve15046032001-07-28 04:15:15 +00001419 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1420 subtreeRoot->leftChild()->getValue());
1421 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve65682392001-07-21 12:42:19 +00001422 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1423
Vikram S. Adve15046032001-07-28 04:15:15 +00001424 // delay slot
1425 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adve65682392001-07-21 12:42:19 +00001426
Vikram S. Adve15046032001-07-28 04:15:15 +00001427 // false branch
1428 mvec[numInstr++] = new MachineInstr(BA);
1429 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1430 (Value*) NULL);
1431 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1432 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001433
1434 // delay slot
1435 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adve65682392001-07-21 12:42:19 +00001436 break;
1437 }
Vikram S. Adve15046032001-07-28 04:15:15 +00001438
Vikram S. Adve65682392001-07-21 12:42:19 +00001439 case 8: // stmt: BrCond(boolreg)
1440 // bool => boolean is stored in an existing register.
1441 // Just use the branch-on-integer-register instruction!
1442 //
1443 mvec[0] = new MachineInstr(BRNZ);
Vikram S. Adve15046032001-07-28 04:15:15 +00001444 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1445 subtreeRoot->leftChild()->getValue());
Vikram S. Adve65682392001-07-21 12:42:19 +00001446 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1447 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001448
1449 // delay slot
Vikram S. Adve65682392001-07-21 12:42:19 +00001450 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001451
1452 // false branch
1453 mvec[numInstr++] = new MachineInstr(BA);
1454 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1455 (Value*) NULL);
1456 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1457 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1458
1459 // delay slot
1460 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adve65682392001-07-21 12:42:19 +00001461 break;
1462
1463 case 9: // stmt: Switch(reg)
1464 assert(0 && "*** SWITCH instruction is not implemented yet.");
1465 numInstr = 0;
1466 break;
1467
1468 case 10: // reg: VRegList(reg, reg)
1469 assert(0 && "VRegList should never be the topmost non-chain rule");
1470 break;
1471
1472 case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0
1473 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve15046032001-07-28 04:15:15 +00001474 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve65682392001-07-21 12:42:19 +00001475 subtreeRoot->leftChild()->getValue());
1476 mvec[0]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve15046032001-07-28 04:15:15 +00001477 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve65682392001-07-21 12:42:19 +00001478 subtreeRoot->getValue());
1479 break;
1480
1481 case 22: // reg: ToBoolTy(reg):
1482 opType = subtreeRoot->leftChild()->getValue()->getType();
1483 assert(opType->isIntegral() || opType == Type::BoolTy);
1484 numInstr = 0;
Vikram S. Adve15046032001-07-28 04:15:15 +00001485 forwardOperandNum = 0;
Vikram S. Adve65682392001-07-21 12:42:19 +00001486 break;
1487
1488 case 23: // reg: ToUByteTy(reg)
1489 case 25: // reg: ToUShortTy(reg)
1490 case 27: // reg: ToUIntTy(reg)
1491 case 29: // reg: ToULongTy(reg)
1492 opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Advee3358212001-07-31 21:46:57 +00001493 assert(opType->isIntegral() ||
1494 opType->isPointerType() ||
1495 opType == Type::BoolTy && "Ignoring cast: illegal for other types");
Vikram S. Adve65682392001-07-21 12:42:19 +00001496 numInstr = 0;
Vikram S. Adve15046032001-07-28 04:15:15 +00001497 forwardOperandNum = 0;
Vikram S. Adve65682392001-07-21 12:42:19 +00001498 break;
1499
1500 case 24: // reg: ToSByteTy(reg)
1501 case 26: // reg: ToShortTy(reg)
1502 case 28: // reg: ToIntTy(reg)
1503 case 30: // reg: ToLongTy(reg)
1504 opType = subtreeRoot->leftChild()->getValue()->getType();
1505 if (opType->isIntegral() || opType == Type::BoolTy)
Vikram S. Adve15046032001-07-28 04:15:15 +00001506 {
1507 numInstr = 0;
1508 forwardOperandNum = 0;
1509 }
Vikram S. Adve65682392001-07-21 12:42:19 +00001510 else
1511 {
1512 mvec[0] =new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,opType));
Vikram S. Adve15046032001-07-28 04:15:15 +00001513 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001514 }
1515 break;
1516
1517 case 31: // reg: ToFloatTy(reg):
1518 case 32: // reg: ToDoubleTy(reg):
1519
1520 // If this instruction has a parent (a user) in the tree
1521 // and the user is translated as an FsMULd instruction,
1522 // then the cast is unnecessary. So check that first.
1523 // In the future, we'll want to do the same for the FdMULq instruction,
1524 // so do the check here instead of only for ToFloatTy(reg).
1525 //
1526 if (subtreeRoot->parent() != NULL &&
1527 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1528 {
1529 numInstr = 0;
Vikram S. Adve15046032001-07-28 04:15:15 +00001530 forwardOperandNum = 0;
Vikram S. Adve65682392001-07-21 12:42:19 +00001531 }
1532 else
1533 {
1534 opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adveb606fcb2001-09-09 20:35:34 +00001535 MachineOpCode opCode = ChooseConvertToFloatInstr(subtreeRoot, opType);
1536 if (opCode == INVALID_OPCODE) // no conversion needed
1537 {
1538 numInstr = 0;
1539 forwardOperandNum = 0;
1540 }
1541 else
1542 {
1543 mvec[0] = new MachineInstr(opCode);
1544 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1545 }
Vikram S. Adve65682392001-07-21 12:42:19 +00001546 }
1547 break;
1548
1549 case 19: // reg: ToArrayTy(reg):
1550 case 20: // reg: ToPointerTy(reg):
1551 numInstr = 0;
Vikram S. Adve15046032001-07-28 04:15:15 +00001552 forwardOperandNum = 0;
Vikram S. Adve65682392001-07-21 12:42:19 +00001553 break;
1554
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001555 case 233: // reg: Add(reg, Constant)
1556 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1557 if (mvec[0] != NULL)
1558 break;
1559 // ELSE FALL THROUGH
1560
Vikram S. Adve65682392001-07-21 12:42:19 +00001561 case 33: // reg: Add(reg, reg)
1562 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
Vikram S. Adve15046032001-07-28 04:15:15 +00001563 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001564 break;
1565
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001566 case 234: // reg: Sub(reg, Constant)
1567 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1568 if (mvec[0] != NULL)
1569 break;
1570 // ELSE FALL THROUGH
1571
Vikram S. Adve65682392001-07-21 12:42:19 +00001572 case 34: // reg: Sub(reg, reg)
1573 mvec[0] = new MachineInstr(ChooseSubInstruction(subtreeRoot));
Vikram S. Adve15046032001-07-28 04:15:15 +00001574 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001575 break;
1576
1577 case 135: // reg: Mul(todouble, todouble)
1578 checkCast = true;
1579 // FALL THROUGH
1580
1581 case 35: // reg: Mul(reg, reg)
1582 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
Vikram S. Adve15046032001-07-28 04:15:15 +00001583 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001584 break;
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001585
1586 case 335: // reg: Mul(todouble, todoubleConst)
1587 checkCast = true;
1588 // FALL THROUGH
1589
1590 case 235: // reg: Mul(reg, Constant)
1591 mvec[0] = CreateMulConstInstruction(subtreeRoot, mvec[1]);
1592 if (mvec[0] == NULL)
1593 {
1594 mvec[0]=new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
1595 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1596 }
1597 else
1598 if (mvec[1] != NULL)
1599 ++numInstr;
1600 break;
1601
1602 case 236: // reg: Div(reg, Constant)
1603 mvec[0] = CreateDivConstInstruction(subtreeRoot, mvec[1]);
1604 if (mvec[0] != NULL)
1605 {
1606 if (mvec[1] != NULL)
1607 ++numInstr;
1608 }
1609 else
1610 // ELSE FALL THROUGH
Vikram S. Adve65682392001-07-21 12:42:19 +00001611
1612 case 36: // reg: Div(reg, reg)
1613 mvec[0] = new MachineInstr(ChooseDivInstruction(subtreeRoot));
Vikram S. Adve15046032001-07-28 04:15:15 +00001614 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001615 break;
1616
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001617 case 37: // reg: Rem(reg, reg)
1618 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve65682392001-07-21 12:42:19 +00001619 assert(0 && "REM instruction unimplemented for the SPARC.");
1620 break;
1621
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001622 case 38: // reg: And(reg, reg)
1623 case 238: // reg: And(reg, Constant)
Vikram S. Adve65682392001-07-21 12:42:19 +00001624 mvec[0] = new MachineInstr(AND);
Vikram S. Adve15046032001-07-28 04:15:15 +00001625 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001626 break;
1627
1628 case 138: // reg: And(reg, not)
1629 mvec[0] = new MachineInstr(ANDN);
Vikram S. Adve15046032001-07-28 04:15:15 +00001630 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001631 break;
1632
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001633 case 39: // reg: Or(reg, reg)
1634 case 239: // reg: Or(reg, Constant)
Vikram S. Adve65682392001-07-21 12:42:19 +00001635 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve15046032001-07-28 04:15:15 +00001636 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001637 break;
1638
1639 case 139: // reg: Or(reg, not)
1640 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve15046032001-07-28 04:15:15 +00001641 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001642 break;
1643
Vikram S. Adve245b5b02001-08-28 23:12:57 +00001644 case 40: // reg: Xor(reg, reg)
1645 case 240: // reg: Xor(reg, Constant)
Vikram S. Adve65682392001-07-21 12:42:19 +00001646 mvec[0] = new MachineInstr(XOR);
Vikram S. Adve15046032001-07-28 04:15:15 +00001647 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001648 break;
1649
1650 case 140: // reg: Xor(reg, not)
1651 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve15046032001-07-28 04:15:15 +00001652 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Adve65682392001-07-21 12:42:19 +00001653 break;
1654
1655 case 41: // boolconst: SetCC(reg, Constant)
1656 // Check if this is an integer comparison, and
1657 // there is a parent, and the parent decided to use
1658 // a branch-on-integer-register instead of branch-on-condition-code.
1659 // If so, the SUBcc instruction is not required.
1660 // (However, we must still check for constants to be loaded from
1661 // the constant pool so that such a load can be associated with
1662 // this instruction.)
1663 //
Vikram S. Adve15046032001-07-28 04:15:15 +00001664 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Adve65682392001-07-21 12:42:19 +00001665 //
1666 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() &&
1667 subtreeRoot->parent() != NULL)
1668 {
1669 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1670 assert(parentNode->getNodeType() == InstrTreeNode::NTInstructionNode);
1671 const vector<MachineInstr*>&
1672 minstrVec = parentNode->getInstruction()->getMachineInstrVec();
1673 MachineOpCode parentOpCode;
Vikram S. Adve15046032001-07-28 04:15:15 +00001674 if (parentNode->getInstruction()->getOpcode() == Instruction::Br &&
Vikram S. Adve65682392001-07-21 12:42:19 +00001675 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1676 parentOpCode <= BRGEZ)
1677 {
Vikram S. Adve15046032001-07-28 04:15:15 +00001678 numInstr = 0; // don't forward the operand!
Vikram S. Adve65682392001-07-21 12:42:19 +00001679 break;
1680 }
1681 }
1682 // ELSE FALL THROUGH
1683
1684 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve15046032001-07-28 04:15:15 +00001685 {
1686 // If result of the SetCC is only used for a branch, we can
1687 // discard the result. otherwise, it must go into an integer register.
1688 // Note that the user may or may not be in the same tree, so we have
1689 // to follow SSA def-use edges here, not BURG tree edges.
1690 //
1691 Instruction* result = subtreeRoot->getInstruction();
1692 Value* firstUse = (Value*) * result->use_begin();
1693 bool discardResult =
1694 (result->use_size() == 1
Chris Lattnere014d792001-09-10 20:10:26 +00001695 && firstUse->isInstruction()
Vikram S. Adve15046032001-07-28 04:15:15 +00001696 && ((Instruction*) firstUse)->getOpcode() == Instruction::Br);
1697
1698 bool mustClearReg;
1699 int valueToMove;
1700 MachineOpCode movOpCode;
1701
Chris Lattner16dd09a2001-09-12 01:28:49 +00001702 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() ||
1703 subtreeRoot->leftChild()->getValue()->getType()->isPointerType())
Vikram S. Adve65682392001-07-21 12:42:19 +00001704 {
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