blob: 921bcbc7ed725dcccc86347e42ba0b3167f7efa1 [file] [log] [blame]
Vikram S. Adve243dd452001-09-18 13:03:13 +00001// $Id$
Chris Lattner20b1ea02001-09-14 03:47:57 +00002//***************************************************************************
3// File:
4// SparcInstrSelection.cpp
5//
6// Purpose:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00007// BURS instruction selection for SPARC V9 architecture.
Chris Lattner20b1ea02001-09-14 03:47:57 +00008//
9// History:
10// 7/02/01 - Vikram Adve - Created
11//**************************************************************************/
12
13#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +000014#include "SparcInstrSelectionSupport.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000015#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000016#include "llvm/CodeGen/MachineInstr.h"
17#include "llvm/CodeGen/InstrForest.h"
18#include "llvm/CodeGen/InstrSelection.h"
19#include "llvm/Support/MathExtras.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/iTerminators.h"
22#include "llvm/iMemory.h"
23#include "llvm/iOther.h"
24#include "llvm/BasicBlock.h"
25#include "llvm/Method.h"
26#include "llvm/ConstPoolVals.h"
Chris Lattner749655f2001-10-13 06:54:30 +000027#include <math.h>
Chris Lattner20b1ea02001-09-14 03:47:57 +000028
29//******************** Internal Data Declarations ************************/
30
Chris Lattner20b1ea02001-09-14 03:47:57 +000031
32//************************* Forward Declarations ***************************/
33
34
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000035static void SetMemOperands_Internal (MachineInstr* minstr,
36 const InstructionNode* vmInstrNode,
37 Value* ptrVal,
38 Value* arrayOffsetVal,
39 const vector<ConstPoolVal*>& idxVec,
40 const TargetMachine& target);
Chris Lattner20b1ea02001-09-14 03:47:57 +000041
42
43//************************ Internal Functions ******************************/
44
Chris Lattner20b1ea02001-09-14 03:47:57 +000045
Chris Lattner20b1ea02001-09-14 03:47:57 +000046static inline MachineOpCode
47ChooseBprInstruction(const InstructionNode* instrNode)
48{
49 MachineOpCode opCode;
50
51 Instruction* setCCInstr =
52 ((InstructionNode*) instrNode->leftChild())->getInstruction();
53
54 switch(setCCInstr->getOpcode())
55 {
56 case Instruction::SetEQ: opCode = BRZ; break;
57 case Instruction::SetNE: opCode = BRNZ; break;
58 case Instruction::SetLE: opCode = BRLEZ; break;
59 case Instruction::SetGE: opCode = BRGEZ; break;
60 case Instruction::SetLT: opCode = BRLZ; break;
61 case Instruction::SetGT: opCode = BRGZ; break;
62 default:
63 assert(0 && "Unrecognized VM instruction!");
64 opCode = INVALID_OPCODE;
65 break;
66 }
67
68 return opCode;
69}
70
71
72static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000073ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000074 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000075{
76 MachineOpCode opCode = INVALID_OPCODE;
77
78 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
79
80 if (isSigned)
81 {
82 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000083 {
84 case Instruction::SetEQ: opCode = BE; break;
85 case Instruction::SetNE: opCode = BNE; break;
86 case Instruction::SetLE: opCode = BLE; break;
87 case Instruction::SetGE: opCode = BGE; break;
88 case Instruction::SetLT: opCode = BL; break;
89 case Instruction::SetGT: opCode = BG; break;
90 default:
91 assert(0 && "Unrecognized VM instruction!");
92 break;
93 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000094 }
95 else
96 {
97 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000098 {
99 case Instruction::SetEQ: opCode = BE; break;
100 case Instruction::SetNE: opCode = BNE; break;
101 case Instruction::SetLE: opCode = BLEU; break;
102 case Instruction::SetGE: opCode = BCC; break;
103 case Instruction::SetLT: opCode = BCS; break;
104 case Instruction::SetGT: opCode = BGU; break;
105 default:
106 assert(0 && "Unrecognized VM instruction!");
107 break;
108 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000109 }
110
111 return opCode;
112}
113
114static inline MachineOpCode
115ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000116 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000117{
118 MachineOpCode opCode = INVALID_OPCODE;
119
120 switch(setCCInstr->getOpcode())
121 {
122 case Instruction::SetEQ: opCode = FBE; break;
123 case Instruction::SetNE: opCode = FBNE; break;
124 case Instruction::SetLE: opCode = FBLE; break;
125 case Instruction::SetGE: opCode = FBGE; break;
126 case Instruction::SetLT: opCode = FBL; break;
127 case Instruction::SetGT: opCode = FBG; break;
128 default:
129 assert(0 && "Unrecognized VM instruction!");
130 break;
131 }
132
133 return opCode;
134}
135
136
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000137// Create a unique TmpInstruction for a boolean value,
138// representing the CC register used by a branch on that value.
139// For now, hack this using a little static cache of TmpInstructions.
140// Eventually the entire BURG instruction selection should be put
141// into a separate class that can hold such information.
142// The static cache is not too bad because that memory for these
143// TmpInstructions will be freed elsewhere in any case.
144//
145static TmpInstruction*
146GetTmpForCC(Value* boolVal, const Method* method)
147{
148 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
149 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
150 static const Method* lastMethod = NULL; // Use to flush cache between methods
151
152 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
153
154 if (lastMethod != method)
155 {
156 lastMethod = method;
157 boolToTmpCache.clear();
158 }
159
160 // Look for tmpI and create a new one otherswise.
161 // new value is directly written to map using
162 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
163 if (tmpI == NULL)
164 tmpI = new TmpInstruction(TMP_INSTRUCTION_OPCODE, boolVal, NULL);
165
166 return tmpI;
167}
168
169
Chris Lattner20b1ea02001-09-14 03:47:57 +0000170static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000171ChooseBccInstruction(const InstructionNode* instrNode,
172 bool& isFPBranch)
173{
174 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
175 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
176 const Type* setCCType = setCCInstr->getOperand(0)->getType();
177
178 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
179
180 if (isFPBranch)
181 return ChooseBFpccInstruction(instrNode, setCCInstr);
182 else
183 return ChooseBpccInstruction(instrNode, setCCInstr);
184}
185
186
187static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000188ChooseMovFpccInstruction(const InstructionNode* instrNode)
189{
190 MachineOpCode opCode = INVALID_OPCODE;
191
192 switch(instrNode->getInstruction()->getOpcode())
193 {
194 case Instruction::SetEQ: opCode = MOVFE; break;
195 case Instruction::SetNE: opCode = MOVFNE; break;
196 case Instruction::SetLE: opCode = MOVFLE; break;
197 case Instruction::SetGE: opCode = MOVFGE; break;
198 case Instruction::SetLT: opCode = MOVFL; break;
199 case Instruction::SetGT: opCode = MOVFG; break;
200 default:
201 assert(0 && "Unrecognized VM instruction!");
202 break;
203 }
204
205 return opCode;
206}
207
208
209// Assumes that SUBcc v1, v2 -> v3 has been executed.
210// In most cases, we want to clear v3 and then follow it by instruction
211// MOVcc 1 -> v3.
212// Set mustClearReg=false if v3 need not be cleared before conditional move.
213// Set valueToMove=0 if we want to conditionally move 0 instead of 1
214// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000215// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000216//
217static MachineOpCode
218ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000219 bool& mustClearReg,
220 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000221{
222 MachineOpCode opCode = INVALID_OPCODE;
223 mustClearReg = true;
224 valueToMove = 1;
225
226 switch(instrNode->getInstruction()->getOpcode())
227 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000228 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000229 case Instruction::SetLE: opCode = MOVLE; break;
230 case Instruction::SetGE: opCode = MOVGE; break;
231 case Instruction::SetLT: opCode = MOVL; break;
232 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000233 case Instruction::SetNE: assert(0 && "No move required!"); break;
234 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000235 }
236
237 return opCode;
238}
239
Chris Lattner20b1ea02001-09-14 03:47:57 +0000240static inline MachineOpCode
241ChooseConvertToFloatInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000242 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000243{
244 MachineOpCode opCode = INVALID_OPCODE;
245
246 switch(instrNode->getOpLabel())
247 {
248 case ToFloatTy:
249 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000250 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000251 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000252 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000253 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000254 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000255 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000256 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000257 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000258 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000259 break;
260
261 case ToDoubleTy:
262 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000263 opCode = FITOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000264 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000265 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000266 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000267 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000268 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000269 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000270 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000271 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000272 break;
273
274 default:
275 break;
276 }
277
278 return opCode;
279}
280
281static inline MachineOpCode
282ChooseConvertToIntInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000283 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000284{
285 MachineOpCode opCode = INVALID_OPCODE;;
286
287 int instrType = (int) instrNode->getOpLabel();
288
289 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
290 {
291 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000292 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000293 case Type::FloatTyID: opCode = FSTOI; break;
294 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000295 default:
296 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
297 break;
298 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000299 }
300 else if (instrType == ToLongTy)
301 {
302 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000303 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000304 case Type::FloatTyID: opCode = FSTOX; break;
305 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000306 default:
307 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
308 break;
309 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000310 }
311 else
312 assert(0 && "Should not get here, Mo!");
313
314 return opCode;
315}
316
317
318static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000319ChooseAddInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000320{
321 MachineOpCode opCode = INVALID_OPCODE;
322
Chris Lattner20b1ea02001-09-14 03:47:57 +0000323 if (resultType->isIntegral() ||
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000324 isa<PointerType>(resultType) ||
325 isa<MethodType>(resultType) ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000326 resultType->isLabelType() ||
327 resultType == Type::BoolTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000328 {
329 opCode = ADD;
330 }
331 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000332 switch(resultType->getPrimitiveID())
333 {
334 case Type::FloatTyID: opCode = FADDS; break;
335 case Type::DoubleTyID: opCode = FADDD; break;
336 default: assert(0 && "Invalid type for ADD instruction"); break;
337 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000338
339 return opCode;
340}
341
342
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000343static inline MachineOpCode
344ChooseAddInstruction(const InstructionNode* instrNode)
345{
346 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
347}
348
349
Chris Lattner20b1ea02001-09-14 03:47:57 +0000350static inline MachineInstr*
351CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000352 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000353{
354 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000355 ? FMOVS : FMOVD);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000356 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000357 instrNode->leftChild()->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000358 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000359 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000360 return minstr;
361}
362
363static inline MachineInstr*
364CreateAddConstInstruction(const InstructionNode* instrNode)
365{
366 MachineInstr* minstr = NULL;
367
368 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000369 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000370
371 // Cases worth optimizing are:
372 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
373 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
374 //
375 const Type* resultType = instrNode->getInstruction()->getType();
376
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000377 if (resultType == Type::FloatTy ||
378 resultType == Type::DoubleTy)
379 {
380 double dval = ((ConstPoolFP*) constOp)->getValue();
381 if (dval == 0.0)
382 minstr = CreateMovFloatInstruction(instrNode, resultType);
383 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000384
385 return minstr;
386}
387
388
389static inline MachineOpCode
390ChooseSubInstruction(const InstructionNode* instrNode)
391{
392 MachineOpCode opCode = INVALID_OPCODE;
393
394 const Type* resultType = instrNode->getInstruction()->getType();
395
396 if (resultType->isIntegral() ||
397 resultType->isPointerType())
398 {
399 opCode = SUB;
400 }
401 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000402 switch(resultType->getPrimitiveID())
403 {
404 case Type::FloatTyID: opCode = FSUBS; break;
405 case Type::DoubleTyID: opCode = FSUBD; break;
406 default: assert(0 && "Invalid type for SUB instruction"); break;
407 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000408
409 return opCode;
410}
411
412
413static inline MachineInstr*
414CreateSubConstInstruction(const InstructionNode* instrNode)
415{
416 MachineInstr* minstr = NULL;
417
418 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000419 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000420
421 // Cases worth optimizing are:
422 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
423 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
424 //
425 const Type* resultType = instrNode->getInstruction()->getType();
426
427 if (resultType == Type::FloatTy ||
428 resultType == Type::DoubleTy)
429 {
430 double dval = ((ConstPoolFP*) constOp)->getValue();
431 if (dval == 0.0)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000432 minstr = CreateMovFloatInstruction(instrNode, resultType);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000433 }
434
435 return minstr;
436}
437
438
439static inline MachineOpCode
440ChooseFcmpInstruction(const InstructionNode* instrNode)
441{
442 MachineOpCode opCode = INVALID_OPCODE;
443
444 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
445 switch(operand->getType()->getPrimitiveID()) {
446 case Type::FloatTyID: opCode = FCMPS; break;
447 case Type::DoubleTyID: opCode = FCMPD; break;
448 default: assert(0 && "Invalid type for FCMP instruction"); break;
449 }
450
451 return opCode;
452}
453
454
455// Assumes that leftArg and rightArg are both cast instructions.
456//
457static inline bool
458BothFloatToDouble(const InstructionNode* instrNode)
459{
460 InstrTreeNode* leftArg = instrNode->leftChild();
461 InstrTreeNode* rightArg = instrNode->rightChild();
462 InstrTreeNode* leftArgArg = leftArg->leftChild();
463 InstrTreeNode* rightArgArg = rightArg->leftChild();
464 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
465
466 // Check if both arguments are floats cast to double
467 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000468 leftArgArg->getValue()->getType() == Type::FloatTy &&
469 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000470}
471
472
473static inline MachineOpCode
474ChooseMulInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000475 bool checkCasts)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000476{
477 MachineOpCode opCode = INVALID_OPCODE;
478
479 if (checkCasts && BothFloatToDouble(instrNode))
480 {
481 return opCode = FSMULD;
482 }
483 // else fall through and use the regular multiply instructions
484
485 const Type* resultType = instrNode->getInstruction()->getType();
486
487 if (resultType->isIntegral())
488 {
489 opCode = MULX;
490 }
491 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000492 switch(resultType->getPrimitiveID())
493 {
494 case Type::FloatTyID: opCode = FMULS; break;
495 case Type::DoubleTyID: opCode = FMULD; break;
496 default: assert(0 && "Invalid type for MUL instruction"); break;
497 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000498
499 return opCode;
500}
501
502
503static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000504CreateIntNegInstruction(TargetMachine& target,
505 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000506{
507 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000508 minstr->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000509 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
510 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
511 return minstr;
512}
513
514
515static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000516CreateMulConstInstruction(TargetMachine &target,
517 const InstructionNode* instrNode,
518 MachineInstr*& getMinstr2)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000519{
520 MachineInstr* minstr = NULL;
521 getMinstr2 = NULL;
522 bool needNeg = false;
523
524 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000525 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000526
527 // Cases worth optimizing are:
528 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
529 // (2) Multiply by 2^x for integer types: replace with Shift
530 //
531 const Type* resultType = instrNode->getInstruction()->getType();
532
Vikram S. Adve243dd452001-09-18 13:03:13 +0000533 if (resultType->isIntegral() || resultType->isPointerType())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000534 {
535 unsigned pow;
536 bool isValidConst;
537 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
538 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000539 {
540 bool needNeg = false;
541 if (C < 0)
542 {
543 needNeg = true;
544 C = -C;
545 }
546
547 if (C == 0 || C == 1)
548 {
549 minstr = new MachineInstr(ADD);
550
551 if (C == 0)
552 minstr->SetMachineOperand(0,
553 target.getRegInfo().getZeroRegNum());
554 else
555 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
556 instrNode->leftChild()->getValue());
557 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
558 }
559 else if (IsPowerOf2(C, pow))
560 {
561 minstr = new MachineInstr((resultType == Type::LongTy)
562 ? SLLX : SLL);
563 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
564 instrNode->leftChild()->getValue());
565 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
566 pow);
567 }
568
569 if (minstr && needNeg)
570 { // insert <reg = SUB 0, reg> after the instr to flip the sign
571 getMinstr2 = CreateIntNegInstruction(target,
572 instrNode->getValue());
573 }
574 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000575 }
576 else
577 {
578 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000579 resultType == Type::DoubleTy)
580 {
581 bool isValidConst;
582 double dval = ((ConstPoolFP*) constOp)->getValue();
583
584 if (isValidConst)
585 {
586 if (dval == 0)
587 {
588 minstr = new MachineInstr((resultType == Type::FloatTy)
589 ? FITOS : FITOD);
590 minstr->SetMachineOperand(0,
591 target.getRegInfo().getZeroRegNum());
592 }
593 else if (fabs(dval) == 1)
594 {
595 bool needNeg = (dval < 0);
596
597 MachineOpCode opCode = needNeg
598 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
599 : (resultType == Type::FloatTy? FMOVS : FMOVD);
600
601 minstr = new MachineInstr(opCode);
602 minstr->SetMachineOperand(0,
603 MachineOperand::MO_VirtualRegister,
604 instrNode->leftChild()->getValue());
605 }
606 }
607 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000608 }
609
610 if (minstr != NULL)
611 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000612 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000613
614 return minstr;
615}
616
617
618static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000619ChooseDivInstruction(TargetMachine &target,
620 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000621{
622 MachineOpCode opCode = INVALID_OPCODE;
623
624 const Type* resultType = instrNode->getInstruction()->getType();
625
626 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000627 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000628 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000629 switch(resultType->getPrimitiveID())
630 {
631 case Type::FloatTyID: opCode = FDIVS; break;
632 case Type::DoubleTyID: opCode = FDIVD; break;
633 default: assert(0 && "Invalid type for DIV instruction"); break;
634 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000635
636 return opCode;
637}
638
639
640static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000641CreateDivConstInstruction(TargetMachine &target,
642 const InstructionNode* instrNode,
643 MachineInstr*& getMinstr2)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000644{
645 MachineInstr* minstr = NULL;
646 getMinstr2 = NULL;
647
648 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000649 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000650
651 // Cases worth optimizing are:
652 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
653 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
654 //
655 const Type* resultType = instrNode->getInstruction()->getType();
656
657 if (resultType->isIntegral())
658 {
659 unsigned pow;
660 bool isValidConst;
661 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
662 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000663 {
664 bool needNeg = false;
665 if (C < 0)
666 {
667 needNeg = true;
668 C = -C;
669 }
670
671 if (C == 1)
672 {
673 minstr = new MachineInstr(ADD);
674 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
675 instrNode->leftChild()->getValue());
676 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
677 }
678 else if (IsPowerOf2(C, pow))
679 {
680 MachineOpCode opCode= ((resultType->isSigned())
681 ? (resultType==Type::LongTy)? SRAX : SRA
682 : (resultType==Type::LongTy)? SRLX : SRL);
683 minstr = new MachineInstr(opCode);
684 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
685 instrNode->leftChild()->getValue());
686 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
687 pow);
688 }
689
690 if (minstr && needNeg)
691 { // insert <reg = SUB 0, reg> after the instr to flip the sign
692 getMinstr2 = CreateIntNegInstruction(target,
693 instrNode->getValue());
694 }
695 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000696 }
697 else
698 {
699 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000700 resultType == Type::DoubleTy)
701 {
702 bool isValidConst;
703 double dval = ((ConstPoolFP*) constOp)->getValue();
704
705 if (isValidConst && fabs(dval) == 1)
706 {
707 bool needNeg = (dval < 0);
708
709 MachineOpCode opCode = needNeg
710 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
711 : (resultType == Type::FloatTy? FMOVS : FMOVD);
712
713 minstr = new MachineInstr(opCode);
714 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
715 instrNode->leftChild()->getValue());
716 }
717 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000718 }
719
720 if (minstr != NULL)
721 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000722 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000723
724 return minstr;
725}
726
727
Chris Lattner20b1ea02001-09-14 03:47:57 +0000728//------------------------------------------------------------------------
729// Function SetOperandsForMemInstr
730//
731// Choose addressing mode for the given load or store instruction.
732// Use [reg+reg] if it is an indexed reference, and the index offset is
733// not a constant or if it cannot fit in the offset field.
734// Use [reg+offset] in all other cases.
735//
736// This assumes that all array refs are "lowered" to one of these forms:
737// %x = load (subarray*) ptr, constant ; single constant offset
738// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
739// Generally, this should happen via strength reduction + LICM.
740// Also, strength reduction should take care of using the same register for
741// the loop index variable and an array index, when that is profitable.
742//------------------------------------------------------------------------
743
744static void
745SetOperandsForMemInstr(MachineInstr* minstr,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000746 const InstructionNode* vmInstrNode,
747 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000748{
749 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
750
751 // Variables to hold the index vector, ptr value, and offset value.
752 // The major work here is to extract these for all 3 instruction types
753 // and then call the common function SetMemOperands_Internal().
754 //
Chris Lattner8e7f4092001-11-04 08:08:34 +0000755 const vector<ConstPoolVal*>* idxVec = &memInst->getIndices();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000756 vector<ConstPoolVal*>* newIdxVec = NULL;
757 Value* ptrVal;
758 Value* arrayOffsetVal = NULL;
759
760 // Test if a GetElemPtr instruction is being folded into this mem instrn.
761 // If so, it will be in the left child for Load and GetElemPtr,
762 // and in the right child for Store instructions.
763 //
764 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000765 ? vmInstrNode->rightChild()
766 : vmInstrNode->leftChild());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000767
768 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
769 ptrChild->getOpLabel() == GetElemPtrIdx)
770 {
771 // There is a GetElemPtr instruction and there may be a chain of
772 // more than one. Use the pointer value of the last one in the chain.
773 // Fold the index vectors from the entire chain and from the mem
774 // instruction into one single index vector.
775 // Finally, we never fold for an array instruction so make that NULL.
776
777 newIdxVec = new vector<ConstPoolVal*>;
778 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
779
780 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
781 idxVec = newIdxVec;
782
783 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000784 && "GetElemPtr cannot be folded into array refs in selection");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000785 }
786 else
787 {
788 // There is no GetElemPtr instruction.
789 // Use the pointer value and the index vector from the Mem instruction.
790 // If it is an array reference, get the array offset value.
791 //
792 ptrVal = memInst->getPtrOperand();
793
794 const Type* opType =
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000795 ((const PointerType*) ptrVal->getType())->getValueType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000796 if (opType->isArrayType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000797 {
798 assert((memInst->getNumOperands()
799 == (unsigned) 1 + memInst->getFirstOffsetIdx())
800 && "Array refs must be lowered before Instruction Selection");
801
802 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
803 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000804 }
805
806 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000807 *idxVec, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000808
809 if (newIdxVec != NULL)
810 delete newIdxVec;
811}
812
813
814static void
815SetMemOperands_Internal(MachineInstr* minstr,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000816 const InstructionNode* vmInstrNode,
817 Value* ptrVal,
818 Value* arrayOffsetVal,
819 const vector<ConstPoolVal*>& idxVec,
820 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000821{
822 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
823
824 // Initialize so we default to storing the offset in a register.
825 int64_t smallConstOffset;
826 Value* valueForRegOffset = NULL;
827 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
828
829 // Check if there is an index vector and if so, if it translates to
830 // a small enough constant to fit in the immediate-offset field.
831 //
832 if (idxVec.size() > 0)
833 {
834 bool isConstantOffset = false;
835 unsigned offset;
836
837 const PointerType* ptrType = (PointerType*) ptrVal->getType();
838
839 if (ptrType->getValueType()->isStructType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000840 {
841 // the offset is always constant for structs
842 isConstantOffset = true;
843
844 // Compute the offset value using the index vector
845 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
846 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000847 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000848 {
849 // It must be an array ref. Check if the offset is a constant,
850 // and that the indexing has been lowered to a single offset.
851 //
852 assert(ptrType->getValueType()->isArrayType());
853 assert(arrayOffsetVal != NULL
854 && "Expect to be given Value* for array offsets");
855
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000856 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(arrayOffsetVal))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000857 {
858 isConstantOffset = true; // always constant for structs
859 assert(arrayOffsetVal->getType()->isIntegral());
860 offset = (CPV->getType()->isSigned()
861 ? ((ConstPoolSInt*)CPV)->getValue()
862 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
863 }
864 else
865 {
866 valueForRegOffset = arrayOffsetVal;
867 }
868 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000869
870 if (isConstantOffset)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000871 {
872 // create a virtual register for the constant
873 valueForRegOffset = ConstPoolSInt::get(Type::IntTy, offset);
874 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000875 }
876 else
877 {
878 offsetOpType = MachineOperand::MO_SignExtendedImmed;
879 smallConstOffset = 0;
880 }
881
882 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
883 // It is the left child in the instruction tree in all cases.
884 Value* leftVal = vmInstrNode->leftChild()->getValue();
885 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
886
887 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000888 // Operand 2 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
Chris Lattner20b1ea02001-09-14 03:47:57 +0000889 //
890 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
891 if (offsetOpType == MachineOperand::MO_VirtualRegister)
892 {
893 assert(valueForRegOffset != NULL);
894 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
895 }
896 else
897 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
898
899 if (memInst->getOpcode() == Instruction::Store)
900 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
901 else
902 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000903 vmInstrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000904}
905
906
Chris Lattner20b1ea02001-09-14 03:47:57 +0000907//
908// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +0000909// in place of the use(s) of that instruction in node `parent'.
910// Check both explicit and implicit operands!
Chris Lattner20b1ea02001-09-14 03:47:57 +0000911//
912static void
913ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000914 InstrTreeNode* parent,
915 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000916{
Vikram S. Adve243dd452001-09-18 13:03:13 +0000917 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
918
Chris Lattner20b1ea02001-09-14 03:47:57 +0000919 Instruction* unusedOp = treeNode->getInstruction();
920 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +0000921
922 // The parent itself may be a list node, so find the real parent instruction
923 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
924 {
925 parent = parent->parent();
926 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
927 }
928 InstructionNode* parentInstrNode = (InstructionNode*) parent;
929
930 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000931 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
932 for (unsigned i=0, N=mvec.size(); i < N; i++)
933 {
934 MachineInstr* minstr = mvec[i];
Vikram S. Advec025fc12001-10-14 23:28:43 +0000935
936 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000937 {
938 const MachineOperand& mop = minstr->getOperand(i);
939 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
940 mop.getVRegValue() == unusedOp)
941 {
942 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
943 fwdOp);
944 }
945 }
Vikram S. Advec025fc12001-10-14 23:28:43 +0000946
947 for (unsigned i=0, numOps=minstr->getNumImplicitRefs(); i < numOps; ++i)
948 if (minstr->getImplicitRef(i) == unusedOp)
949 minstr->setImplicitRef(i, fwdOp, minstr->implicitRefIsDefined(i));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000950 }
951}
952
953
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000954void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000955CreateCopyInstructionsByType(const TargetMachine& target,
956 Value* src,
957 Instruction* dest,
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000958 vector<MachineInstr*>& minstrVec)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000959{
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000960 bool loadConstantToReg = false;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000961
962 const Type* resultType = dest->getType();
963
964 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
965 if (opCode == INVALID_OPCODE)
966 {
967 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000968 return;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000969 }
970
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000971 // if `src' is a constant that doesn't fit in the immed field or if it is
972 // a global variable (i.e., a constant address), generate a load
973 // instruction instead of an add
974 //
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000975 if (isa<ConstPoolVal>(src))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000976 {
977 unsigned int machineRegNum;
978 int64_t immedValue;
979 MachineOperand::MachineOperandType opType =
980 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
981 machineRegNum, immedValue);
982
983 if (opType == MachineOperand::MO_VirtualRegister)
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000984 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000985 }
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000986 else if (isa<GlobalValue>(src))
987 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000988
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000989 if (loadConstantToReg)
990 { // `src' is constant and cannot fit in immed field for the ADD
991 // Insert instructions to "load" the constant into a register
992 vector<TmpInstruction*> tempVec;
993 target.getInstrInfo().CreateCodeToLoadConst(src,dest,minstrVec,tempVec);
994 for (unsigned i=0; i < tempVec.size(); i++)
995 dest->getMachineInstrVec().addTempValue(tempVec[i]);
996 }
997 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000998 { // Create the appropriate add instruction.
999 // Make `src' the second operand, in case it is a constant
1000 // Use (unsigned long) 0 for a NULL pointer value.
1001 //
1002 const Type* nullValueType =
1003 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
1004 : resultType;
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001005 MachineInstr* minstr = new MachineInstr(opCode);
1006 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1007 ConstPoolVal::getNullConstant(nullValueType));
1008 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, src);
1009 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
1010 minstrVec.push_back(minstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001011 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001012}
1013
1014
Vikram S. Advefb361122001-10-22 13:36:31 +00001015//******************* Externally Visible Functions *************************/
1016
1017
1018//------------------------------------------------------------------------
1019// External Function: GetInstructionsForProlog
1020// External Function: GetInstructionsForEpilog
1021//
1022// Purpose:
1023// Create prolog and epilog code for procedure entry and exit
1024//------------------------------------------------------------------------
1025
1026extern unsigned
1027GetInstructionsForProlog(BasicBlock* entryBB,
1028 TargetMachine &target,
1029 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001030{
Vikram S. Advefb361122001-10-22 13:36:31 +00001031 int64_t s0=0; // used to avoid overloading ambiguity below
Chris Lattner20b1ea02001-09-14 03:47:57 +00001032
Vikram S. Advefb361122001-10-22 13:36:31 +00001033 // The second operand is the stack size. If it does not fit in the
1034 // immediate field, we either have to find an unused register in the
1035 // caller's window or move some elements to the dynamically allocated
1036 // area of the stack frame (just above save area and method args).
1037 Method* method = entryBB->getParent();
1038 MachineCodeForMethod& mcodeInfo = method->getMachineCode();
1039 unsigned int staticStackSize = mcodeInfo.getStaticStackSize();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001040
Vikram S. Advefb361122001-10-22 13:36:31 +00001041 assert(target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize)
1042 && "Stack size too large for immediate field of SAVE instruction. Need additional work as described in the comment above");
Chris Lattner20b1ea02001-09-14 03:47:57 +00001043
Vikram S. Advefb361122001-10-22 13:36:31 +00001044 mvec[0] = new MachineInstr(SAVE);
1045 mvec[0]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1046 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
Vikram S. Adve0589b2c2001-10-28 21:39:47 +00001047 - staticStackSize);
Vikram S. Advefb361122001-10-22 13:36:31 +00001048 mvec[0]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
1049
1050 return 1;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001051}
1052
1053
Vikram S. Advefb361122001-10-22 13:36:31 +00001054extern unsigned
1055GetInstructionsForEpilog(BasicBlock* anExitBB,
1056 TargetMachine &target,
1057 MachineInstr** mvec)
1058{
1059 int64_t s0=0; // used to avoid overloading ambiguity below
1060
1061 mvec[0] = new MachineInstr(RESTORE);
1062 mvec[0]->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
1063 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1064 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
1065
1066 return 1;
1067}
1068
1069
1070//------------------------------------------------------------------------
1071// External Function: ThisIsAChainRule
1072//
1073// Purpose:
1074// Check if a given BURG rule is a chain rule.
1075//------------------------------------------------------------------------
1076
1077extern bool
1078ThisIsAChainRule(int eruleno)
1079{
1080 switch(eruleno)
1081 {
1082 case 111: // stmt: reg
1083 case 113: // stmt: bool
1084 case 123:
1085 case 124:
1086 case 125:
1087 case 126:
1088 case 127:
1089 case 128:
1090 case 129:
1091 case 130:
1092 case 131:
1093 case 132:
1094 case 133:
1095 case 155:
1096 case 221:
1097 case 222:
1098 case 241:
1099 case 242:
1100 case 243:
1101 case 244:
1102 return true; break;
1103
1104 default:
1105 return false; break;
1106 }
1107}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001108
1109
1110//------------------------------------------------------------------------
1111// External Function: GetInstructionsByRule
1112//
1113// Purpose:
1114// Choose machine instructions for the SPARC according to the
1115// patterns chosen by the BURG-generated parser.
1116//------------------------------------------------------------------------
1117
1118unsigned
1119GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001120 int ruleForNode,
1121 short* nts,
Vikram S. Advefb361122001-10-22 13:36:31 +00001122 TargetMachine &tgt,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001123 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001124{
1125 int numInstr = 1; // initialize for common case
1126 bool checkCast = false; // initialize here to use fall-through
1127 Value *leftVal, *rightVal;
1128 const Type* opType;
1129 int nextRule;
1130 int forwardOperandNum = -1;
Vikram S. Adve8557b222001-10-10 20:56:33 +00001131 int64_t s0=0, s8=8; // variables holding constants to avoid
1132 uint64_t u0=0; // overloading ambiguities below
Chris Lattner20b1ea02001-09-14 03:47:57 +00001133
Vikram S. Advefb361122001-10-22 13:36:31 +00001134 UltraSparc& target = (UltraSparc&) tgt;
1135
1136 for (unsigned i=0; i < MAX_INSTR_PER_VMINSTR; i++)
1137 mvec[i] = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001138
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001139 //
1140 // Let's check for chain rules outside the switch so that we don't have
1141 // to duplicate the list of chain rule production numbers here again
1142 //
1143 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001144 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001145 // Chain rules have a single nonterminal on the RHS.
1146 // Get the rule that matches the RHS non-terminal and use that instead.
1147 //
1148 assert(nts[0] && ! nts[1]
1149 && "A chain rule should have only one RHS non-terminal!");
1150 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1151 nts = burm_nts[nextRule];
1152 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001153 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001154 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001155 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001156 switch(ruleForNode) {
1157 case 1: // stmt: Ret
1158 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001159 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001160 // for moving return value to appropriate register.
1161 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001162 // Mark the return value register as an implicit ref of
1163 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001164 // Finally put a NOP in the delay slot.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001165 ReturnInst* returnInstr = (ReturnInst*) subtreeRoot->getInstruction();
1166 assert(returnInstr->getOpcode() == Instruction::Ret);
Vikram S. Advefb361122001-10-22 13:36:31 +00001167 Method* method = returnInstr->getParent()->getParent();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001168
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001169 Instruction* returnReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001170 returnInstr, NULL);
1171 returnInstr->getMachineInstrVec().addTempValue(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001172
1173 mvec[0] = new MachineInstr(JMPLRET);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001174 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1175 returnReg);
Vikram S. Adve8557b222001-10-10 20:56:33 +00001176 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,s8);
Vikram S. Advefb361122001-10-22 13:36:31 +00001177 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001178
Vikram S. Advea995e602001-10-11 04:23:19 +00001179 if (returnInstr->getReturnValue() != NULL)
1180 mvec[0]->addImplicitRef(returnInstr->getReturnValue());
1181
Vikram S. Advefb361122001-10-22 13:36:31 +00001182 unsigned n = numInstr++; // delay slot
1183 mvec[n] = new MachineInstr(NOP);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001184
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001185 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001186 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001187
1188 case 3: // stmt: Store(reg,reg)
1189 case 4: // stmt: Store(reg,ptrreg)
1190 mvec[0] = new MachineInstr(
1191 ChooseStoreInstruction(
1192 subtreeRoot->leftChild()->getValue()->getType()));
1193 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1194 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001195
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001196 case 5: // stmt: BrUncond
1197 mvec[0] = new MachineInstr(BA);
1198 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1199 (Value*)NULL);
1200 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1201 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1202
1203 // delay slot
1204 mvec[numInstr++] = new MachineInstr(NOP);
1205 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001206
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001207 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001208 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001209 // If the constant is ZERO, we can use the branch-on-integer-register
1210 // instructions and avoid the SUBcc instruction entirely.
1211 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001212 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001213 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1214 assert(constNode &&
1215 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1216 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
1217 bool isValidConst;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001218
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001219 if ((constVal->getType()->isIntegral()
1220 || constVal->getType()->isPointerType())
1221 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1222 && isValidConst)
1223 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001224 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1225
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001226 // That constant is a zero after all...
1227 // Use the left child of setCC as the first argument!
1228 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1229 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1230 subtreeRoot->leftChild()->leftChild()->getValue());
1231 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001232 brInst->getSuccessor(0));
Chris Lattner20b1ea02001-09-14 03:47:57 +00001233
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001234 // delay slot
1235 mvec[numInstr++] = new MachineInstr(NOP);
1236
1237 // false branch
1238 int n = numInstr++;
1239 mvec[n] = new MachineInstr(BA);
1240 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1241 (Value*) NULL);
1242 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001243 brInst->getSuccessor(1));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001244
1245 // delay slot
1246 mvec[numInstr++] = new MachineInstr(NOP);
1247
1248 break;
1249 }
1250 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001251 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001252
1253 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001254 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001255 // (SetCC, Not, ...). We need to check whether the type was a FP,
1256 // signed int or unsigned int, and check the branching condition in
1257 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001258 // If it is an integer CC, we also need to find the unique
1259 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001260 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001261 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001262 bool isFPBranch;
1263 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot,
1264 isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001265
1266 Value* ccValue = isFPBranch? subtreeRoot->leftChild()->getValue()
1267 : GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1268 brInst->getParent()->getParent());
1269
1270 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, ccValue);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001271 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001272 brInst->getSuccessor(0));
1273
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001274 // delay slot
1275 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001276
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001277 // false branch
1278 int n = numInstr++;
1279 mvec[n] = new MachineInstr(BA);
1280 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1281 (Value*) NULL);
1282 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001283 brInst->getSuccessor(1));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001284
1285 // delay slot
1286 mvec[numInstr++] = new MachineInstr(NOP);
1287 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001288 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001289
1290 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001291 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001292 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnercfe26c92001-10-01 18:26:53 +00001293 ConstPoolVal* constVal =
1294 cast<ConstPoolVal>(subtreeRoot->leftChild()->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001295 unsigned dest = ((ConstPoolBool*) constVal)->getValue()? 0 : 1;
1296
1297 mvec[0] = new MachineInstr(BA);
1298 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1299 (Value*) NULL);
1300 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1301 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
1302
1303 // delay slot
1304 mvec[numInstr++] = new MachineInstr(NOP);
1305 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001306 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001307
1308 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001309 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001310 // Just use the branch-on-integer-register instruction!
1311 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001312 mvec[0] = new MachineInstr(BRNZ);
1313 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1314 subtreeRoot->leftChild()->getValue());
1315 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1316 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1317
1318 // delay slot
1319 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1320
1321 // false branch
1322 int n = numInstr++;
1323 mvec[n] = new MachineInstr(BA);
1324 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1325 (Value*) NULL);
1326 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1327 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1328
1329 // delay slot
1330 mvec[numInstr++] = new MachineInstr(NOP);
1331 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001332 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001333
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001334 case 9: // stmt: Switch(reg)
1335 assert(0 && "*** SWITCH instruction is not implemented yet.");
1336 numInstr = 0;
1337 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001338
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001339 case 10: // reg: VRegList(reg, reg)
1340 assert(0 && "VRegList should never be the topmost non-chain rule");
1341 break;
1342
1343 case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0
1344 mvec[0] = new MachineInstr(XNOR);
1345 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1346 subtreeRoot->leftChild()->getValue());
1347 mvec[0]->SetMachineOperand(1, target.getRegInfo().getZeroRegNum());
1348 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1349 subtreeRoot->getValue());
1350 break;
1351
1352 case 322: // reg: ToBoolTy(bool):
1353 case 22: // reg: ToBoolTy(reg):
1354 opType = subtreeRoot->leftChild()->getValue()->getType();
1355 assert(opType->isIntegral() || opType == Type::BoolTy);
1356 numInstr = 0;
1357 forwardOperandNum = 0;
1358 break;
1359
1360 case 23: // reg: ToUByteTy(reg)
1361 case 25: // reg: ToUShortTy(reg)
1362 case 27: // reg: ToUIntTy(reg)
1363 case 29: // reg: ToULongTy(reg)
1364 opType = subtreeRoot->leftChild()->getValue()->getType();
1365 assert(opType->isIntegral() ||
1366 opType->isPointerType() ||
1367 opType == Type::BoolTy && "Cast is illegal for other types");
1368 numInstr = 0;
1369 forwardOperandNum = 0;
1370 break;
1371
1372 case 24: // reg: ToSByteTy(reg)
1373 case 26: // reg: ToShortTy(reg)
1374 case 28: // reg: ToIntTy(reg)
1375 case 30: // reg: ToLongTy(reg)
1376 opType = subtreeRoot->leftChild()->getValue()->getType();
1377 if (opType->isIntegral() || opType == Type::BoolTy)
1378 {
1379 numInstr = 0;
1380 forwardOperandNum = 0;
1381 }
1382 else
1383 {
1384 mvec[0] = new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,
1385 opType));
1386 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1387 }
1388 break;
1389
1390 case 31: // reg: ToFloatTy(reg):
1391 case 32: // reg: ToDoubleTy(reg):
1392 case 232: // reg: ToDoubleTy(Constant):
1393
1394 // If this instruction has a parent (a user) in the tree
1395 // and the user is translated as an FsMULd instruction,
1396 // then the cast is unnecessary. So check that first.
1397 // In the future, we'll want to do the same for the FdMULq instruction,
1398 // so do the check here instead of only for ToFloatTy(reg).
1399 //
1400 if (subtreeRoot->parent() != NULL &&
1401 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1402 {
1403 numInstr = 0;
1404 forwardOperandNum = 0;
1405 }
1406 else
1407 {
1408 opType = subtreeRoot->leftChild()->getValue()->getType();
1409 MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType);
1410 if (opCode == INVALID_OPCODE) // no conversion needed
1411 {
1412 numInstr = 0;
1413 forwardOperandNum = 0;
1414 }
1415 else
1416 {
1417 mvec[0] = new MachineInstr(opCode);
1418 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1419 }
1420 }
1421 break;
1422
1423 case 19: // reg: ToArrayTy(reg):
1424 case 20: // reg: ToPointerTy(reg):
1425 numInstr = 0;
1426 forwardOperandNum = 0;
1427 break;
1428
1429 case 233: // reg: Add(reg, Constant)
1430 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1431 if (mvec[0] != NULL)
1432 break;
1433 // ELSE FALL THROUGH
1434
1435 case 33: // reg: Add(reg, reg)
1436 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
1437 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1438 break;
1439
1440 case 234: // reg: Sub(reg, Constant)
1441 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1442 if (mvec[0] != NULL)
1443 break;
1444 // ELSE FALL THROUGH
1445
1446 case 34: // reg: Sub(reg, reg)
1447 mvec[0] = new MachineInstr(ChooseSubInstruction(subtreeRoot));
1448 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1449 break;
1450
1451 case 135: // reg: Mul(todouble, todouble)
1452 checkCast = true;
1453 // FALL THROUGH
1454
1455 case 35: // reg: Mul(reg, reg)
1456 mvec[0] =new MachineInstr(ChooseMulInstruction(subtreeRoot,checkCast));
1457 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1458 break;
1459
1460 case 335: // reg: Mul(todouble, todoubleConst)
1461 checkCast = true;
1462 // FALL THROUGH
1463
1464 case 235: // reg: Mul(reg, Constant)
1465 mvec[0] = CreateMulConstInstruction(target, subtreeRoot, mvec[1]);
1466 if (mvec[0] == NULL)
1467 {
1468 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot,
1469 checkCast));
1470 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1471 }
1472 else
1473 if (mvec[1] != NULL)
1474 ++numInstr;
1475 break;
1476
1477 case 236: // reg: Div(reg, Constant)
1478 mvec[0] = CreateDivConstInstruction(target, subtreeRoot, mvec[1]);
1479 if (mvec[0] != NULL)
1480 {
1481 if (mvec[1] != NULL)
1482 ++numInstr;
1483 }
1484 else
1485 // ELSE FALL THROUGH
1486
1487 case 36: // reg: Div(reg, reg)
1488 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1489 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1490 break;
1491
1492 case 37: // reg: Rem(reg, reg)
1493 case 237: // reg: Rem(reg, Constant)
1494 assert(0 && "REM instruction unimplemented for the SPARC.");
1495 break;
1496
1497 case 38: // reg: And(reg, reg)
1498 case 238: // reg: And(reg, Constant)
1499 mvec[0] = new MachineInstr(AND);
1500 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1501 break;
1502
1503 case 138: // reg: And(reg, not)
1504 mvec[0] = new MachineInstr(ANDN);
1505 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1506 break;
1507
1508 case 39: // reg: Or(reg, reg)
1509 case 239: // reg: Or(reg, Constant)
1510 mvec[0] = new MachineInstr(ORN);
1511 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1512 break;
1513
1514 case 139: // reg: Or(reg, not)
1515 mvec[0] = new MachineInstr(ORN);
1516 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1517 break;
1518
1519 case 40: // reg: Xor(reg, reg)
1520 case 240: // reg: Xor(reg, Constant)
1521 mvec[0] = new MachineInstr(XOR);
1522 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1523 break;
1524
1525 case 140: // reg: Xor(reg, not)
1526 mvec[0] = new MachineInstr(XNOR);
1527 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1528 break;
1529
1530 case 41: // boolconst: SetCC(reg, Constant)
1531 // Check if this is an integer comparison, and
1532 // there is a parent, and the parent decided to use
1533 // a branch-on-integer-register instead of branch-on-condition-code.
1534 // If so, the SUBcc instruction is not required.
1535 // (However, we must still check for constants to be loaded from
1536 // the constant pool so that such a load can be associated with
1537 // this instruction.)
1538 //
1539 // Otherwise this is just the same as case 42, so just fall through.
1540 //
1541 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() &&
1542 subtreeRoot->parent() != NULL)
1543 {
1544 InstructionNode* parent = (InstructionNode*) subtreeRoot->parent();
1545 assert(parent->getNodeType() == InstrTreeNode::NTInstructionNode);
1546 const vector<MachineInstr*>&
1547 minstrVec = parent->getInstruction()->getMachineInstrVec();
1548 MachineOpCode parentOpCode;
1549 if (parent->getInstruction()->getOpcode() == Instruction::Br &&
1550 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1551 parentOpCode <= BRGEZ)
1552 {
1553 numInstr = 0; // don't forward the operand!
1554 break;
1555 }
1556 }
1557 // ELSE FALL THROUGH
1558
1559 case 42: // bool: SetCC(reg, reg):
1560 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001561 // This generates a SUBCC instruction, putting the difference in
1562 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001563 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001564 // If the boolean result of the SetCC is used by anything other
1565 // than a single branch instruction, the boolean must be
1566 // computed and stored in the result register. Otherwise, discard
1567 // the difference (by using %g0) and keep only the condition code.
1568 //
1569 // To compute the boolean result in a register we use a conditional
1570 // move, unless the result of the SUBCC instruction can be used as
1571 // the bool! This assumes that zero is FALSE and any non-zero
1572 // integer is TRUE.
1573 //
1574 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1575 Instruction* setCCInstr = subtreeRoot->getInstruction();
1576 bool keepBoolVal = (parentNode == NULL ||
1577 parentNode->getInstruction()->getOpcode()
1578 != Instruction::Br);
1579 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001580 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1581 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1582
1583 bool mustClearReg;
1584 int valueToMove;
1585 MachineOpCode movOpCode;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001586 Value* ccValue = NULL;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001587
1588 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() ||
1589 subtreeRoot->leftChild()->getValue()->getType()->isPointerType())
1590 {
1591 // Integer condition: dest. should be %g0 or an integer register.
1592 // If result must be saved but condition is not SetEQ then we need
1593 // a separate instruction to compute the bool result, so discard
1594 // result of SUBcc instruction anyway.
1595 //
1596 mvec[0] = new MachineInstr(SUBcc);
1597 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, ! keepSubVal);
1598
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001599 // Mark the 4th operand as being a CC register, and as a def
1600 // A TmpInstruction is created to represent the int CC "result".
1601 // Unlike other instances of TmpInstruction, this one is used by
1602 // used by machine code of multiple LLVM instructions, viz.,
1603 // the SetCC and the branch. Make sure to get the same one!
1604 //
1605 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1606 setCCInstr->getParent()->getParent());
1607 setCCInstr->getMachineInstrVec().addTempValue(tmpForCC);
1608
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001609 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001610 tmpForCC, /*def*/true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001611
1612 if (computeBoolVal)
1613 { // recompute bool using the integer condition codes
1614 movOpCode =
1615 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001616 ccValue = tmpForCC;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001617 }
1618 }
1619 else
1620 {
1621 // FP condition: dest of FCMP should be some FCCn register
1622 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001623 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1624 setCCInstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001625 mvec[0]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
1626 subtreeRoot->leftChild()->getValue());
1627 mvec[0]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,
1628 subtreeRoot->rightChild()->getValue());
1629
1630 if (computeBoolVal)
1631 {// recompute bool using the FP condition codes
1632 mustClearReg = true;
1633 valueToMove = 1;
1634 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001635 ccValue = setCCInstr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001636 }
1637 }
1638
1639 if (computeBoolVal)
1640 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001641 assert(ccValue && "Inconsistent logic above and here");
1642
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001643 if (mustClearReg)
1644 {// Unconditionally set register to 0
1645 int n = numInstr++;
1646 mvec[n] = new MachineInstr(SETHI);
1647 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,
1648 s0);
1649 mvec[n]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001650 setCCInstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001651 }
1652
1653 // Now conditionally move `valueToMove' (0 or 1) into the register
1654 int n = numInstr++;
1655 mvec[n] = new MachineInstr(movOpCode);
1656 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001657 ccValue);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001658 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1659 valueToMove);
1660 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001661 setCCInstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001662 }
1663 break;
1664 }
1665
1666 case 43: // boolreg: VReg
1667 case 44: // boolreg: Constant
1668 numInstr = 0;
1669 break;
1670
1671 case 51: // reg: Load(reg)
1672 case 52: // reg: Load(ptrreg)
1673 case 53: // reg: LoadIdx(reg,reg)
1674 case 54: // reg: LoadIdx(ptrreg,reg)
1675 mvec[0] = new MachineInstr(ChooseLoadInstruction(
1676 subtreeRoot->getValue()->getType()));
1677 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1678 break;
1679
1680 case 55: // reg: GetElemPtr(reg)
1681 case 56: // reg: GetElemPtrIdx(reg,reg)
1682 if (subtreeRoot->parent() != NULL)
1683 {
1684 // Check if the parent was an array access.
1685 // If so, we still need to generate this instruction.
1686 MemAccessInst* memInst = (MemAccessInst*)
1687 subtreeRoot->getInstruction();
1688 const PointerType* ptrType =
1689 (const PointerType*) memInst->getPtrOperand()->getType();
1690 if (! ptrType->getValueType()->isArrayType())
1691 {// we don't need a separate instr
1692 numInstr = 0; // don't forward operand!
1693 break;
1694 }
1695 }
1696 // else in all other cases we need to a separate ADD instruction
1697 mvec[0] = new MachineInstr(ADD);
1698 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1699 break;
1700
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001701 case 57: // reg: Alloca: Implement as 1 instruction:
1702 { // add %fp, offsetFromFP -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001703 Instruction* instr = subtreeRoot->getInstruction();
1704 const PointerType* instrType = (const PointerType*) instr->getType();
1705 assert(instrType->isPointerType());
1706 int tsize = (int)
1707 target.findOptimalStorageSize(instrType->getValueType());
1708 assert(tsize != 0 && "Just to check when this can happen");
1709
Vikram S. Advefb361122001-10-22 13:36:31 +00001710 Method* method = instr->getParent()->getParent();
1711 MachineCodeForMethod& mcode = method->getMachineCode();
1712 int offsetFromFP =
1713 target.getFrameInfo().getFirstAutomaticVarOffsetFromFP(method)
1714 - (tsize + mcode.getAutomaticVarsSize());
1715
1716 mcode.putLocalVarAtOffsetFromFP(instr, offsetFromFP, tsize);
1717
1718 // Create a temporary Value to hold the constant offset.
1719 // This is needed because it may not fit in the immediate field.
1720 ConstPoolSInt* offsetVal=ConstPoolSInt::get(Type::IntTy, offsetFromFP);
1721
1722 // Instruction 1: add %fp, offsetFromFP -> result
1723 mvec[0] = new MachineInstr(ADD);
1724 mvec[0]->SetMachineOperand(0, target.getRegInfo().getFramePointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001725 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001726 offsetVal);
1727 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001728 instr);
1729 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001730 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001731
1732 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1733 // mul num, typeSz -> tmp
1734 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001735 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001736 Instruction* instr = subtreeRoot->getInstruction();
1737 const PointerType* instrType = (const PointerType*) instr->getType();
1738 assert(instrType->isPointerType() &&
1739 instrType->getValueType()->isArrayType());
1740 const Type* eltType =
1741 ((ArrayType*) instrType->getValueType())->getElementType();
1742 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefb361122001-10-22 13:36:31 +00001743
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001744 assert(tsize != 0 && "Just to check when this can happen");
Vikram S. Advefb361122001-10-22 13:36:31 +00001745
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001746 // Create a temporary Value to hold the constant type-size
Vikram S. Advefb361122001-10-22 13:36:31 +00001747 ConstPoolSInt* tsizeVal = ConstPoolSInt::get(Type::IntTy, tsize);
1748
1749 // Create a temporary Value to hold the constant offset from SP
1750 Method* method = instr->getParent()->getParent();
1751 MachineCodeForMethod& mcode = method->getMachineCode();
1752 int frameSizeBelowDynamicArea =
1753 target.getFrameInfo().getFrameSizeBelowDynamicArea(method);
1754 ConstPoolSInt* lowerAreaSizeVal = ConstPoolSInt::get(Type::IntTy,
1755 frameSizeBelowDynamicArea);
1756 cerr << "***" << endl
1757 << "*** Variable-size ALLOCA operation needs more work:" << endl
1758 << "*** We have to precompute the size of "
1759 << " optional arguments in the stack frame" << endl
1760 << "***" << endl;
1761 assert(0 && "SEE MESSAGE ABOVE");
1762
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001763 // Create a temporary value to hold `tmp'
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001764 Instruction* tmpInstr = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001765 subtreeRoot->leftChild()->getValue(),
1766 NULL /*could insert tsize here*/);
1767 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1768
1769 // Instruction 1: mul numElements, typeSize -> tmp
1770 mvec[0] = new MachineInstr(MULX);
1771 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001772 subtreeRoot->leftChild()->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001773 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001774 tsizeVal);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001775 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1776 tmpInstr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001777
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001778 // Instruction 2: sub %sp, tmp -> %sp
1779 numInstr++;
1780 mvec[1] = new MachineInstr(SUB);
Vikram S. Advefb361122001-10-22 13:36:31 +00001781 mvec[1]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001782 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1783 tmpInstr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001784 mvec[1]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001785
Vikram S. Advefb361122001-10-22 13:36:31 +00001786 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001787 numInstr++;
1788 mvec[2] = new MachineInstr(ADD);
Vikram S. Advefb361122001-10-22 13:36:31 +00001789 mvec[2]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1790 mvec[2]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1791 lowerAreaSizeVal);
1792 mvec[2]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,instr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001793 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001794 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001795
1796 case 61: // reg: Call
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001797 { // Generate a call-indirect (i.e., jmpl) for now to expose
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001798 // the potential need for registers. If an absolute address
1799 // is available, replace this with a CALL instruction.
1800 // Mark both the indirection register and the return-address
1801 // register as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00001802 // Also, mark the operands of the Call and return value (if
1803 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001804 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001805 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001806 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001807
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001808 Instruction* retAddrReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001809 callInstr, NULL);
Vikram S. Adve8557b222001-10-10 20:56:33 +00001810
Vikram S. Advea995e602001-10-11 04:23:19 +00001811 // Note temporary values in the machineInstrVec for the VM instr.
Vikram S. Adve8557b222001-10-10 20:56:33 +00001812 //
1813 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
1814 // The result value must go in slot N. This is assumed
1815 // in register allocation.
1816 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001817 callInstr->getMachineInstrVec().addTempValue(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001818
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001819
1820 // Generate the machine instruction and its operands.
1821 // Use CALL for direct function calls; this optimistically assumes
1822 // the PC-relative address fits in the CALL address field (22 bits).
1823 // Use JMPL for indirect calls.
1824 //
1825 if (callee->getValueType() == Value::MethodVal)
1826 { // direct function call
1827 mvec[0] = new MachineInstr(CALL);
1828 mvec[0]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1829 callee);
1830 }
1831 else
1832 { // indirect function call
Vikram S. Advefb361122001-10-22 13:36:31 +00001833 mvec[0] = new MachineInstr(JMPLCALL);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001834 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1835 callee);
1836 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1837 (int64_t) 0);
1838 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1839 retAddrReg);
1840 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001841
Vikram S. Advea995e602001-10-11 04:23:19 +00001842 // Add the call operands and return value as implicit refs
1843 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
1844 if (callInstr->getOperand(i) != callee)
1845 mvec[0]->addImplicitRef(callInstr->getOperand(i));
1846
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001847 if (callInstr->getType() != Type::VoidTy)
Vikram S. Advea995e602001-10-11 04:23:19 +00001848 mvec[0]->addImplicitRef(callInstr, /*isDef*/ true);
1849
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001850 // For the CALL instruction, the ret. addr. reg. is also implicit
1851 if (callee->getValueType() == Value::MethodVal)
1852 mvec[0]->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001853
1854 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1855 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001856 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001857
1858 case 62: // reg: Shl(reg, reg)
1859 opType = subtreeRoot->leftChild()->getValue()->getType();
1860 assert(opType->isIntegral()
1861 || opType == Type::BoolTy
1862 || opType->isPointerType()&& "Shl unsupported for other types");
1863 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
1864 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1865 break;
1866
1867 case 63: // reg: Shr(reg, reg)
1868 opType = subtreeRoot->leftChild()->getValue()->getType();
1869 assert(opType->isIntegral()
1870 || opType == Type::BoolTy
1871 || opType->isPointerType() &&"Shr unsupported for other types");
1872 mvec[0] = new MachineInstr((opType->isSigned()
1873 ? ((opType == Type::LongTy)? SRAX : SRA)
1874 : ((opType == Type::LongTy)? SRLX : SRL)));
1875 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1876 break;
1877
1878 case 64: // reg: Phi(reg,reg)
1879 { // This instruction has variable #operands, so resultPos is 0.
1880 Instruction* phi = subtreeRoot->getInstruction();
1881 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
1882 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1883 subtreeRoot->getValue());
1884 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
1885 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
1886 phi->getOperand(i));
1887 break;
1888 }
1889 case 71: // reg: VReg
1890 case 72: // reg: Constant
1891 numInstr = 0; // don't forward the value
1892 break;
1893
1894 default:
1895 assert(0 && "Unrecognized BURG rule");
1896 numInstr = 0;
1897 break;
1898 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001899 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001900
1901 if (forwardOperandNum >= 0)
1902 { // We did not generate a machine instruction but need to use operand.
1903 // If user is in the same tree, replace Value in its machine operand.
1904 // If not, insert a copy instruction which should get coalesced away
1905 // by register allocation.
1906 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001907 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001908 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001909 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001910 vector<MachineInstr*> minstrVec;
1911 CreateCopyInstructionsByType(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001912 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001913 subtreeRoot->getInstruction(), minstrVec);
1914 assert(minstrVec.size() > 0);
1915 for (unsigned i=0; i < minstrVec.size(); ++i)
1916 mvec[numInstr++] = minstrVec[i];
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001917 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001918 }
1919
Chris Lattner20b1ea02001-09-14 03:47:57 +00001920 return numInstr;
1921}
1922
1923