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